David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 1 | """General key class.""" |
| 2 | |
David Brown | 79c4fcf | 2021-01-26 15:04:05 -0700 | [diff] [blame] | 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 5 | import sys |
Bence Balogh | 97a20f1 | 2023-07-18 15:59:33 +0200 | [diff] [blame] | 6 | from cryptography.hazmat.primitives.hashes import Hash, SHA256 |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 7 | |
| 8 | AUTOGEN_MESSAGE = "/* Autogenerated by imgtool.py, do not edit. */" |
| 9 | |
Antonio de Angelis | c6e7e9b | 2022-11-15 15:06:40 +0000 | [diff] [blame] | 10 | |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 11 | class KeyClass(object): |
Bence Balogh | 367aefb | 2023-07-18 15:51:54 +0200 | [diff] [blame] | 12 | def _emit(self, header, trailer, encoded_bytes, indent, file=sys.stdout, |
| 13 | len_format=None): |
| 14 | if file and file is not sys.stdout: |
| 15 | with open(file, 'w') as file: |
| 16 | self._emit_to_output(header, trailer, encoded_bytes, indent, |
| 17 | file, len_format) |
| 18 | else: |
| 19 | self._emit_to_output(header, trailer, encoded_bytes, indent, |
| 20 | sys.stdout, len_format) |
| 21 | |
| 22 | def _emit_to_output(self, header, trailer, encoded_bytes, indent, file, |
| 23 | len_format): |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 24 | print(AUTOGEN_MESSAGE, file=file) |
| 25 | print(header, end='', file=file) |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 26 | for count, b in enumerate(encoded_bytes): |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 27 | if count % 8 == 0: |
| 28 | print("\n" + indent, end='', file=file) |
| 29 | else: |
| 30 | print(" ", end='', file=file) |
| 31 | print("0x{:02x},".format(b), end='', file=file) |
| 32 | print("\n" + trailer, file=file) |
| 33 | if len_format is not None: |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 34 | print(len_format.format(len(encoded_bytes)), file=file) |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 35 | |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 36 | def emit_c_public(self, file=sys.stdout): |
| 37 | self._emit( |
Bence Balogh | 97a20f1 | 2023-07-18 15:59:33 +0200 | [diff] [blame] | 38 | header="const unsigned char {}_pub_key[] = {{" |
| 39 | .format(self.shortname()), |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 40 | trailer="};", |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 41 | encoded_bytes=self.get_public_bytes(), |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 42 | indent=" ", |
Bence Balogh | 97a20f1 | 2023-07-18 15:59:33 +0200 | [diff] [blame] | 43 | len_format="const unsigned int {}_pub_key_len = {{}};" |
| 44 | .format(self.shortname()), |
| 45 | file=file) |
| 46 | |
| 47 | def emit_c_public_hash(self, file=sys.stdout): |
| 48 | digest = Hash(SHA256()) |
| 49 | digest.update(self.get_public_bytes()) |
| 50 | self._emit( |
| 51 | header="const unsigned char {}_pub_key_hash[] = {{" |
| 52 | .format(self.shortname()), |
| 53 | trailer="};", |
| 54 | encoded_bytes=digest.finalize(), |
| 55 | indent=" ", |
| 56 | len_format="const unsigned int {}_pub_key_hash_len = {{}};" |
| 57 | .format(self.shortname()), |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 58 | file=file) |
| 59 | |
Bence Balogh | ed8d68a | 2023-07-18 15:57:52 +0200 | [diff] [blame] | 60 | def emit_raw_public(self, file=sys.stdout): |
| 61 | if file and file is not sys.stdout: |
| 62 | with open(file, 'wb') as file: |
| 63 | file.write(self.get_public_bytes()) |
| 64 | else: |
| 65 | sys.stdout.buffer.write(self.get_public_bytes()) |
| 66 | |
Bence Balogh | 97a20f1 | 2023-07-18 15:59:33 +0200 | [diff] [blame] | 67 | def emit_raw_public_hash(self, file=sys.stdout): |
| 68 | digest = Hash(SHA256()) |
| 69 | digest.update(self.get_public_bytes()) |
| 70 | if file and file is not sys.stdout: |
| 71 | with open(file, 'wb') as file: |
| 72 | file.write(digest.finalize()) |
| 73 | else: |
| 74 | sys.stdout.buffer.write(digest.finalize()) |
| 75 | |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 76 | def emit_rust_public(self, file=sys.stdout): |
| 77 | self._emit( |
Bence Balogh | 97a20f1 | 2023-07-18 15:59:33 +0200 | [diff] [blame] | 78 | header="static {}_PUB_KEY: &[u8] = &[" |
| 79 | .format(self.shortname().upper()), |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 80 | trailer="];", |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 81 | encoded_bytes=self.get_public_bytes(), |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 82 | indent=" ", |
| 83 | file=file) |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 84 | |
Fabio Utzig | 6f28677 | 2022-09-04 20:03:11 -0300 | [diff] [blame] | 85 | def emit_public_pem(self, file=sys.stdout): |
Bence Balogh | 367aefb | 2023-07-18 15:51:54 +0200 | [diff] [blame] | 86 | if file and file is not sys.stdout: |
| 87 | with open(file, 'w') as file: |
| 88 | print(str(self.get_public_pem(), 'utf-8'), file=file, end='') |
| 89 | else: |
| 90 | print(str(self.get_public_pem(), 'utf-8'), file=sys.stdout, end='') |
Fabio Utzig | 6f28677 | 2022-09-04 20:03:11 -0300 | [diff] [blame] | 91 | |
Antonio de Angelis | c6e7e9b | 2022-11-15 15:06:40 +0000 | [diff] [blame] | 92 | def emit_private(self, minimal, format, file=sys.stdout): |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 93 | self._emit( |
| 94 | header="const unsigned char enc_priv_key[] = {", |
| 95 | trailer="};", |
Antonio de Angelis | c6e7e9b | 2022-11-15 15:06:40 +0000 | [diff] [blame] | 96 | encoded_bytes=self.get_private_bytes(minimal, format), |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 97 | indent=" ", |
| 98 | len_format="const unsigned int enc_priv_key_len = {};", |
| 99 | file=file) |