blob: 0393a9a53d4268b88a6c2f1f6312b294d961d59f [file] [log] [blame]
David Brown5e7c6dd2017-11-16 14:47:16 -07001"""General key class."""
2
David Brown79c4fcf2021-01-26 15:04:05 -07003# SPDX-License-Identifier: Apache-2.0
4
David Brown5e7c6dd2017-11-16 14:47:16 -07005import sys
6
7AUTOGEN_MESSAGE = "/* Autogenerated by imgtool.py, do not edit. */"
8
Antonio de Angelisc6e7e9b2022-11-15 15:06:40 +00009
David Brown5e7c6dd2017-11-16 14:47:16 -070010class KeyClass(object):
Bence Balogh367aefb2023-07-18 15:51:54 +020011 def _emit(self, header, trailer, encoded_bytes, indent, file=sys.stdout,
12 len_format=None):
13 if file and file is not sys.stdout:
14 with open(file, 'w') as file:
15 self._emit_to_output(header, trailer, encoded_bytes, indent,
16 file, len_format)
17 else:
18 self._emit_to_output(header, trailer, encoded_bytes, indent,
19 sys.stdout, len_format)
20
21 def _emit_to_output(self, header, trailer, encoded_bytes, indent, file,
22 len_format):
David Brown5e7c6dd2017-11-16 14:47:16 -070023 print(AUTOGEN_MESSAGE, file=file)
24 print(header, end='', file=file)
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020025 for count, b in enumerate(encoded_bytes):
David Brown5e7c6dd2017-11-16 14:47:16 -070026 if count % 8 == 0:
27 print("\n" + indent, end='', file=file)
28 else:
29 print(" ", end='', file=file)
30 print("0x{:02x},".format(b), end='', file=file)
31 print("\n" + trailer, file=file)
32 if len_format is not None:
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020033 print(len_format.format(len(encoded_bytes)), file=file)
David Brown5e7c6dd2017-11-16 14:47:16 -070034
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020035 def emit_c_public(self, file=sys.stdout):
36 self._emit(
David Brown5e7c6dd2017-11-16 14:47:16 -070037 header="const unsigned char {}_pub_key[] = {{".format(self.shortname()),
38 trailer="};",
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020039 encoded_bytes=self.get_public_bytes(),
David Brown5e7c6dd2017-11-16 14:47:16 -070040 indent=" ",
41 len_format="const unsigned int {}_pub_key_len = {{}};".format(self.shortname()),
42 file=file)
43
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020044 def emit_rust_public(self, file=sys.stdout):
45 self._emit(
David Brown1997f532021-03-10 05:04:05 -070046 header="static {}_PUB_KEY: &[u8] = &[".format(self.shortname().upper()),
David Brown5e7c6dd2017-11-16 14:47:16 -070047 trailer="];",
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020048 encoded_bytes=self.get_public_bytes(),
David Brown5e7c6dd2017-11-16 14:47:16 -070049 indent=" ",
50 file=file)
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020051
Fabio Utzig6f286772022-09-04 20:03:11 -030052 def emit_public_pem(self, file=sys.stdout):
Bence Balogh367aefb2023-07-18 15:51:54 +020053 if file and file is not sys.stdout:
54 with open(file, 'w') as file:
55 print(str(self.get_public_pem(), 'utf-8'), file=file, end='')
56 else:
57 print(str(self.get_public_pem(), 'utf-8'), file=sys.stdout, end='')
Fabio Utzig6f286772022-09-04 20:03:11 -030058
Antonio de Angelisc6e7e9b2022-11-15 15:06:40 +000059 def emit_private(self, minimal, format, file=sys.stdout):
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020060 self._emit(
61 header="const unsigned char enc_priv_key[] = {",
62 trailer="};",
Antonio de Angelisc6e7e9b2022-11-15 15:06:40 +000063 encoded_bytes=self.get_private_bytes(minimal, format),
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020064 indent=" ",
65 len_format="const unsigned int enc_priv_key_len = {};",
66 file=file)