Debian Codegraph @

src/nitrokey/trussed/_bootloader/lpc55_upload/utils/crypto/rkht.py

raw
1 #!/usr/bin/env python
2 # -*- coding: UTF-8 -*-
3 #
4 # Copyright 2022-2024 NXP
5 #
6 # SPDX-License-Identifier: BSD-3-Clause
7
8 """The module provides support for Root Key Hash table."""
9
10 from abc import abstractmethod
11 from typing import TYPE_CHECKING, List
12
13 from ...crypto.hash import EnumHashAlgorithm, get_hash
14 from ...exceptions import SPSDKError
15
16 if TYPE_CHECKING:
17 from typing_extensions import Self
18
19
20 class RKHT:
21 """Root Key Hash Table class."""
22
23 def __init__(self, rkh_list: List[bytes]) -> None:
24 """Initialization of Root Key Hash Table class.
25
26 :param rkh_list: List of Root Key Hashes
27 """
28 if len(rkh_list) > 4:
29 raise SPSDKError("Number of Root Key Hashes can not be larger than 4.")
30 self.rkh_list = rkh_list
31
32 @abstractmethod
33 def rkth(self) -> bytes:
34 """Root Key Table Hash.
35
36 :return: Hash of hashes of public keys.
37 """
38
39 @property
40 def hash_algorithm(self) -> EnumHashAlgorithm:
41 """Used hash algorithm name."""
42 if not len(self.rkh_list) > 0:
43 raise SPSDKError("Unknown hash algorighm name. No root key hashes.")
44 return EnumHashAlgorithm.from_label(f"sha{self.hash_algorithm_size}")
45
46 @property
47 def hash_algorithm_size(self) -> int:
48 """Used hash algorithm size in bites."""
49 if not len(self.rkh_list) > 0:
50 raise SPSDKError("Unknown hash algorithm size. No public keys provided.")
51 return len(self.rkh_list[0]) * 8
52
53
54 class RKHTv1(RKHT):
55 """Root Key Hash Table class for cert block v1."""
56
57 RKHT_SIZE = 4
58 RKH_SIZE = 32
59
60 def __init__(self, rkh_list: List[bytes]) -> None:
61 """Initialization of Root Key Hash Table class.
62
63 :param rkh_list: List of Root Key Hashes
64 """
65 for key_hash in rkh_list:
66 if len(key_hash) != self.RKH_SIZE:
67 raise SPSDKError(f"Invalid key hash size: {len(key_hash)}")
68 super().__init__(rkh_list)
69
70 @property
71 def hash_algorithm(self) -> EnumHashAlgorithm:
72 """Used Hash algorithm name."""
73 return EnumHashAlgorithm.SHA256
74
75 def export(self) -> bytes:
76 """Export RKHT as bytes."""
77 rotk_table = b""
78 for i in range(self.RKHT_SIZE):
79 if i < len(self.rkh_list) and self.rkh_list[i]:
80 rotk_table += self.rkh_list[i]
81 else:
82 rotk_table += bytes(self.RKH_SIZE)
83 if len(rotk_table) != self.RKH_SIZE * self.RKHT_SIZE:
84 raise SPSDKError("Invalid length of data.")
85 return rotk_table
86
87 @classmethod
88 def parse(cls, rkht: bytes) -> "Self":
89 """Parse Root Key Hash Table into RKHTv1 object.
90
91 :param rkht: Valid RKHT table
92 """
93 rotkh_len = len(rkht) // cls.RKHT_SIZE
94 offset = 0
95 key_hashes = []
96 for _ in range(cls.RKHT_SIZE):
97 key_hashes.append(rkht[offset : offset + rotkh_len])
98 offset += rotkh_len
99 return cls(key_hashes)
100
101 def rkth(self) -> bytes:
102 """Root Key Table Hash.
103
104 :return: Hash of Hashes of public key.
105 """
106 rotkh = get_hash(self.export(), self.hash_algorithm)
107 return rotkh