blob: b415f47487d54276e8c7b246156c484aee3c6ce8 [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
Bence Balogh97a20f12023-07-18 15:59:33 +02006from cryptography.hazmat.primitives.hashes import Hash, SHA256
David Brown5e7c6dd2017-11-16 14:47:16 -07007
8AUTOGEN_MESSAGE = "/* Autogenerated by imgtool.py, do not edit. */"
9
Antonio de Angelisc6e7e9b2022-11-15 15:06:40 +000010
David Brown5e7c6dd2017-11-16 14:47:16 -070011class KeyClass(object):
Bence Balogh367aefb2023-07-18 15:51:54 +020012 def _emit(self, header, trailer, encoded_bytes, indent, file=sys.stdout,
13 len_format=None):
14 if file and file is not sys.stdout:
15 with open(file, 'w') as file:
16 self._emit_to_output(header, trailer, encoded_bytes, indent,
17 file, len_format)
18 else:
19 self._emit_to_output(header, trailer, encoded_bytes, indent,
20 sys.stdout, len_format)
21
22 def _emit_to_output(self, header, trailer, encoded_bytes, indent, file,
23 len_format):
David Brown5e7c6dd2017-11-16 14:47:16 -070024 print(AUTOGEN_MESSAGE, file=file)
25 print(header, end='', file=file)
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020026 for count, b in enumerate(encoded_bytes):
David Brown5e7c6dd2017-11-16 14:47:16 -070027 if count % 8 == 0:
28 print("\n" + indent, end='', file=file)
29 else:
30 print(" ", end='', file=file)
31 print("0x{:02x},".format(b), end='', file=file)
32 print("\n" + trailer, file=file)
33 if len_format is not None:
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020034 print(len_format.format(len(encoded_bytes)), file=file)
David Brown5e7c6dd2017-11-16 14:47:16 -070035
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020036 def emit_c_public(self, file=sys.stdout):
37 self._emit(
Bence Balogh97a20f12023-07-18 15:59:33 +020038 header="const unsigned char {}_pub_key[] = {{"
39 .format(self.shortname()),
David Brown5e7c6dd2017-11-16 14:47:16 -070040 trailer="};",
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020041 encoded_bytes=self.get_public_bytes(),
David Brown5e7c6dd2017-11-16 14:47:16 -070042 indent=" ",
Bence Balogh97a20f12023-07-18 15:59:33 +020043 len_format="const unsigned int {}_pub_key_len = {{}};"
44 .format(self.shortname()),
45 file=file)
46
47 def emit_c_public_hash(self, file=sys.stdout):
48 digest = Hash(SHA256())
49 digest.update(self.get_public_bytes())
50 self._emit(
51 header="const unsigned char {}_pub_key_hash[] = {{"
52 .format(self.shortname()),
53 trailer="};",
54 encoded_bytes=digest.finalize(),
55 indent=" ",
56 len_format="const unsigned int {}_pub_key_hash_len = {{}};"
57 .format(self.shortname()),
David Brown5e7c6dd2017-11-16 14:47:16 -070058 file=file)
59
Bence Baloghed8d68a2023-07-18 15:57:52 +020060 def emit_raw_public(self, file=sys.stdout):
61 if file and file is not sys.stdout:
62 with open(file, 'wb') as file:
63 file.write(self.get_public_bytes())
64 else:
65 sys.stdout.buffer.write(self.get_public_bytes())
66
Bence Balogh97a20f12023-07-18 15:59:33 +020067 def emit_raw_public_hash(self, file=sys.stdout):
68 digest = Hash(SHA256())
69 digest.update(self.get_public_bytes())
70 if file and file is not sys.stdout:
71 with open(file, 'wb') as file:
72 file.write(digest.finalize())
73 else:
74 sys.stdout.buffer.write(digest.finalize())
75
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020076 def emit_rust_public(self, file=sys.stdout):
77 self._emit(
Bence Balogh97a20f12023-07-18 15:59:33 +020078 header="static {}_PUB_KEY: &[u8] = &["
79 .format(self.shortname().upper()),
David Brown5e7c6dd2017-11-16 14:47:16 -070080 trailer="];",
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020081 encoded_bytes=self.get_public_bytes(),
David Brown5e7c6dd2017-11-16 14:47:16 -070082 indent=" ",
83 file=file)
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020084
Fabio Utzig6f286772022-09-04 20:03:11 -030085 def emit_public_pem(self, file=sys.stdout):
Bence Balogh367aefb2023-07-18 15:51:54 +020086 if file and file is not sys.stdout:
87 with open(file, 'w') as file:
88 print(str(self.get_public_pem(), 'utf-8'), file=file, end='')
89 else:
90 print(str(self.get_public_pem(), 'utf-8'), file=sys.stdout, end='')
Fabio Utzig6f286772022-09-04 20:03:11 -030091
Antonio de Angelisc6e7e9b2022-11-15 15:06:40 +000092 def emit_private(self, minimal, format, file=sys.stdout):
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020093 self._emit(
94 header="const unsigned char enc_priv_key[] = {",
95 trailer="};",
Antonio de Angelisc6e7e9b2022-11-15 15:06:40 +000096 encoded_bytes=self.get_private_bytes(minimal, format),
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020097 indent=" ",
98 len_format="const unsigned int enc_priv_key_len = {};",
99 file=file)