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 |
| 6 | |
| 7 | AUTOGEN_MESSAGE = "/* Autogenerated by imgtool.py, do not edit. */" |
| 8 | |
| 9 | class KeyClass(object): |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 10 | def _emit(self, header, trailer, encoded_bytes, indent, file=sys.stdout, len_format=None): |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 11 | print(AUTOGEN_MESSAGE, file=file) |
| 12 | print(header, end='', file=file) |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 13 | for count, b in enumerate(encoded_bytes): |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 14 | if count % 8 == 0: |
| 15 | print("\n" + indent, end='', file=file) |
| 16 | else: |
| 17 | print(" ", end='', file=file) |
| 18 | print("0x{:02x},".format(b), end='', file=file) |
| 19 | print("\n" + trailer, file=file) |
| 20 | if len_format is not None: |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 21 | print(len_format.format(len(encoded_bytes)), file=file) |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 22 | |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 23 | def emit_c_public(self, file=sys.stdout): |
| 24 | self._emit( |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 25 | header="const unsigned char {}_pub_key[] = {{".format(self.shortname()), |
| 26 | trailer="};", |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 27 | encoded_bytes=self.get_public_bytes(), |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 28 | indent=" ", |
| 29 | len_format="const unsigned int {}_pub_key_len = {{}};".format(self.shortname()), |
| 30 | file=file) |
| 31 | |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 32 | def emit_rust_public(self, file=sys.stdout): |
| 33 | self._emit( |
David Brown | 1997f53 | 2021-03-10 05:04:05 -0700 | [diff] [blame] | 34 | header="static {}_PUB_KEY: &[u8] = &[".format(self.shortname().upper()), |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 35 | trailer="];", |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 36 | encoded_bytes=self.get_public_bytes(), |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 37 | indent=" ", |
| 38 | file=file) |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 39 | |
| 40 | def emit_private(self, minimal, file=sys.stdout): |
| 41 | self._emit( |
| 42 | header="const unsigned char enc_priv_key[] = {", |
| 43 | trailer="};", |
| 44 | encoded_bytes=self.get_private_bytes(minimal), |
| 45 | indent=" ", |
| 46 | len_format="const unsigned int enc_priv_key_len = {};", |
| 47 | file=file) |