1"""General key class.""" 2 3# SPDX-License-Identifier: Apache-2.0 4 5import sys 6 7AUTOGEN_MESSAGE = "/* Autogenerated by imgtool.py, do not edit. */" 8 9class KeyClass(object): 10 def _emit(self, header, trailer, encoded_bytes, indent, file=sys.stdout, len_format=None): 11 print(AUTOGEN_MESSAGE, file=file) 12 print(header, end='', file=file) 13 for count, b in enumerate(encoded_bytes): 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: 21 print(len_format.format(len(encoded_bytes)), file=file) 22 23 def emit_c_public(self, file=sys.stdout): 24 self._emit( 25 header="const unsigned char {}_pub_key[] = {{".format(self.shortname()), 26 trailer="};", 27 encoded_bytes=self.get_public_bytes(), 28 indent=" ", 29 len_format="const unsigned int {}_pub_key_len = {{}};".format(self.shortname()), 30 file=file) 31 32 def emit_rust_public(self, file=sys.stdout): 33 self._emit( 34 header="static {}_PUB_KEY: &[u8] = &[".format(self.shortname().upper()), 35 trailer="];", 36 encoded_bytes=self.get_public_bytes(), 37 indent=" ", 38 file=file) 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) 48