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 | |
Denis Mingulov | faf2dd1 | 2023-09-26 09:22:44 +0300 | [diff] [blame^] | 5 | import binascii |
| 6 | import io |
| 7 | import os |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 8 | import sys |
Bence Balogh | 97a20f1 | 2023-07-18 15:59:33 +0200 | [diff] [blame] | 9 | from cryptography.hazmat.primitives.hashes import Hash, SHA256 |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 10 | |
| 11 | AUTOGEN_MESSAGE = "/* Autogenerated by imgtool.py, do not edit. */" |
| 12 | |
Antonio de Angelis | c6e7e9b | 2022-11-15 15:06:40 +0000 | [diff] [blame] | 13 | |
Denis Mingulov | faf2dd1 | 2023-09-26 09:22:44 +0300 | [diff] [blame^] | 14 | class FileHandler(object): |
| 15 | def __init__(self, file, *args, **kwargs): |
| 16 | self.file_in = file |
| 17 | self.args = args |
| 18 | self.kwargs = kwargs |
| 19 | |
| 20 | def __enter__(self): |
| 21 | if isinstance(self.file_in, (str, bytes, os.PathLike)): |
| 22 | self.file = open(self.file_in, *self.args, **self.kwargs) |
| 23 | else: |
| 24 | self.file = self.file_in |
| 25 | return self.file |
| 26 | |
| 27 | def __exit__(self, *args): |
| 28 | if self.file != self.file_in: |
| 29 | self.file.close() |
| 30 | |
| 31 | |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 32 | class KeyClass(object): |
Bence Balogh | 367aefb | 2023-07-18 15:51:54 +0200 | [diff] [blame] | 33 | def _emit(self, header, trailer, encoded_bytes, indent, file=sys.stdout, |
| 34 | len_format=None): |
Denis Mingulov | faf2dd1 | 2023-09-26 09:22:44 +0300 | [diff] [blame^] | 35 | with FileHandler(file, 'w') as file: |
Bence Balogh | 367aefb | 2023-07-18 15:51:54 +0200 | [diff] [blame] | 36 | self._emit_to_output(header, trailer, encoded_bytes, indent, |
Denis Mingulov | faf2dd1 | 2023-09-26 09:22:44 +0300 | [diff] [blame^] | 37 | file, len_format) |
Bence Balogh | 367aefb | 2023-07-18 15:51:54 +0200 | [diff] [blame] | 38 | |
| 39 | def _emit_to_output(self, header, trailer, encoded_bytes, indent, file, |
| 40 | len_format): |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 41 | print(AUTOGEN_MESSAGE, file=file) |
| 42 | print(header, end='', file=file) |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 43 | for count, b in enumerate(encoded_bytes): |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 44 | if count % 8 == 0: |
| 45 | print("\n" + indent, end='', file=file) |
| 46 | else: |
| 47 | print(" ", end='', file=file) |
| 48 | print("0x{:02x},".format(b), end='', file=file) |
| 49 | print("\n" + trailer, file=file) |
| 50 | if len_format is not None: |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 51 | print(len_format.format(len(encoded_bytes)), file=file) |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 52 | |
Denis Mingulov | faf2dd1 | 2023-09-26 09:22:44 +0300 | [diff] [blame^] | 53 | def _emit_raw(self, encoded_bytes, file): |
| 54 | with FileHandler(file, 'wb') as file: |
| 55 | try: |
| 56 | # file.buffer is not part of the TextIOBase API |
| 57 | # and may not exist in some implementations. |
| 58 | file.buffer.write(encoded_bytes) |
| 59 | except AttributeError: |
| 60 | # raw binary data, can be for example io.BytesIO |
| 61 | file.write(encoded_bytes) |
| 62 | |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 63 | def emit_c_public(self, file=sys.stdout): |
| 64 | self._emit( |
Bence Balogh | 97a20f1 | 2023-07-18 15:59:33 +0200 | [diff] [blame] | 65 | header="const unsigned char {}_pub_key[] = {{" |
| 66 | .format(self.shortname()), |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 67 | trailer="};", |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 68 | encoded_bytes=self.get_public_bytes(), |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 69 | indent=" ", |
Bence Balogh | 97a20f1 | 2023-07-18 15:59:33 +0200 | [diff] [blame] | 70 | len_format="const unsigned int {}_pub_key_len = {{}};" |
| 71 | .format(self.shortname()), |
| 72 | file=file) |
| 73 | |
| 74 | def emit_c_public_hash(self, file=sys.stdout): |
| 75 | digest = Hash(SHA256()) |
| 76 | digest.update(self.get_public_bytes()) |
| 77 | self._emit( |
| 78 | header="const unsigned char {}_pub_key_hash[] = {{" |
| 79 | .format(self.shortname()), |
| 80 | trailer="};", |
| 81 | encoded_bytes=digest.finalize(), |
| 82 | indent=" ", |
| 83 | len_format="const unsigned int {}_pub_key_hash_len = {{}};" |
| 84 | .format(self.shortname()), |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 85 | file=file) |
| 86 | |
Bence Balogh | ed8d68a | 2023-07-18 15:57:52 +0200 | [diff] [blame] | 87 | def emit_raw_public(self, file=sys.stdout): |
Denis Mingulov | faf2dd1 | 2023-09-26 09:22:44 +0300 | [diff] [blame^] | 88 | self._emit_raw(self.get_public_bytes(), file=file) |
Bence Balogh | ed8d68a | 2023-07-18 15:57:52 +0200 | [diff] [blame] | 89 | |
Bence Balogh | 97a20f1 | 2023-07-18 15:59:33 +0200 | [diff] [blame] | 90 | def emit_raw_public_hash(self, file=sys.stdout): |
| 91 | digest = Hash(SHA256()) |
| 92 | digest.update(self.get_public_bytes()) |
Denis Mingulov | faf2dd1 | 2023-09-26 09:22:44 +0300 | [diff] [blame^] | 93 | self._emit_raw(digest.finalize(), file=file) |
Bence Balogh | 97a20f1 | 2023-07-18 15:59:33 +0200 | [diff] [blame] | 94 | |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 95 | def emit_rust_public(self, file=sys.stdout): |
| 96 | self._emit( |
Bence Balogh | 97a20f1 | 2023-07-18 15:59:33 +0200 | [diff] [blame] | 97 | header="static {}_PUB_KEY: &[u8] = &[" |
| 98 | .format(self.shortname().upper()), |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 99 | trailer="];", |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 100 | encoded_bytes=self.get_public_bytes(), |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 101 | indent=" ", |
| 102 | file=file) |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 103 | |
Fabio Utzig | 6f28677 | 2022-09-04 20:03:11 -0300 | [diff] [blame] | 104 | def emit_public_pem(self, file=sys.stdout): |
Denis Mingulov | faf2dd1 | 2023-09-26 09:22:44 +0300 | [diff] [blame^] | 105 | with FileHandler(file, 'w') as file: |
| 106 | print(str(self.get_public_pem(), 'utf-8'), file=file, end='') |
Fabio Utzig | 6f28677 | 2022-09-04 20:03:11 -0300 | [diff] [blame] | 107 | |
Antonio de Angelis | c6e7e9b | 2022-11-15 15:06:40 +0000 | [diff] [blame] | 108 | def emit_private(self, minimal, format, file=sys.stdout): |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 109 | self._emit( |
| 110 | header="const unsigned char enc_priv_key[] = {", |
| 111 | trailer="};", |
Antonio de Angelis | c6e7e9b | 2022-11-15 15:06:40 +0000 | [diff] [blame] | 112 | encoded_bytes=self.get_private_bytes(minimal, format), |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 113 | indent=" ", |
| 114 | len_format="const unsigned int enc_priv_key_len = {};", |
| 115 | file=file) |