blob: 45cddc6f04b35763a8c178c93d08a9543e1d3aac [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
Bence Baloghed8d68a2023-07-18 15:57:52 +020044 def emit_raw_public(self, file=sys.stdout):
45 if file and file is not sys.stdout:
46 with open(file, 'wb') as file:
47 file.write(self.get_public_bytes())
48 else:
49 sys.stdout.buffer.write(self.get_public_bytes())
50
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020051 def emit_rust_public(self, file=sys.stdout):
52 self._emit(
David Brown1997f532021-03-10 05:04:05 -070053 header="static {}_PUB_KEY: &[u8] = &[".format(self.shortname().upper()),
David Brown5e7c6dd2017-11-16 14:47:16 -070054 trailer="];",
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020055 encoded_bytes=self.get_public_bytes(),
David Brown5e7c6dd2017-11-16 14:47:16 -070056 indent=" ",
57 file=file)
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020058
Fabio Utzig6f286772022-09-04 20:03:11 -030059 def emit_public_pem(self, file=sys.stdout):
Bence Balogh367aefb2023-07-18 15:51:54 +020060 if file and file is not sys.stdout:
61 with open(file, 'w') as file:
62 print(str(self.get_public_pem(), 'utf-8'), file=file, end='')
63 else:
64 print(str(self.get_public_pem(), 'utf-8'), file=sys.stdout, end='')
Fabio Utzig6f286772022-09-04 20:03:11 -030065
Antonio de Angelisc6e7e9b2022-11-15 15:06:40 +000066 def emit_private(self, minimal, format, file=sys.stdout):
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020067 self._emit(
68 header="const unsigned char enc_priv_key[] = {",
69 trailer="};",
Antonio de Angelisc6e7e9b2022-11-15 15:06:40 +000070 encoded_bytes=self.get_private_bytes(minimal, format),
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020071 indent=" ",
72 len_format="const unsigned int enc_priv_key_len = {};",
73 file=file)