blob: 4ff700c495e6fc81e68b260d917d52c548cbcff6 [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
Denis Mingulovfaf2dd12023-09-26 09:22:44 +03005import binascii
6import io
7import os
David Brown5e7c6dd2017-11-16 14:47:16 -07008import sys
Bence Balogh97a20f12023-07-18 15:59:33 +02009from cryptography.hazmat.primitives.hashes import Hash, SHA256
David Brown5e7c6dd2017-11-16 14:47:16 -070010
11AUTOGEN_MESSAGE = "/* Autogenerated by imgtool.py, do not edit. */"
12
Antonio de Angelisc6e7e9b2022-11-15 15:06:40 +000013
Denis Mingulovfaf2dd12023-09-26 09:22:44 +030014class FileHandler(object):
15 def __init__(self, file, *args, **kwargs):
16 self.file_in = file
17 self.args = args
18 self.kwargs = kwargs
19
20 def __enter__(self):
21 if isinstance(self.file_in, (str, bytes, os.PathLike)):
22 self.file = open(self.file_in, *self.args, **self.kwargs)
23 else:
24 self.file = self.file_in
25 return self.file
26
27 def __exit__(self, *args):
28 if self.file != self.file_in:
29 self.file.close()
30
31
David Brown5e7c6dd2017-11-16 14:47:16 -070032class KeyClass(object):
Bence Balogh367aefb2023-07-18 15:51:54 +020033 def _emit(self, header, trailer, encoded_bytes, indent, file=sys.stdout,
34 len_format=None):
Denis Mingulovfaf2dd12023-09-26 09:22:44 +030035 with FileHandler(file, 'w') as file:
Bence Balogh367aefb2023-07-18 15:51:54 +020036 self._emit_to_output(header, trailer, encoded_bytes, indent,
Denis Mingulovfaf2dd12023-09-26 09:22:44 +030037 file, len_format)
Bence Balogh367aefb2023-07-18 15:51:54 +020038
39 def _emit_to_output(self, header, trailer, encoded_bytes, indent, file,
40 len_format):
David Brown5e7c6dd2017-11-16 14:47:16 -070041 print(AUTOGEN_MESSAGE, file=file)
42 print(header, end='', file=file)
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020043 for count, b in enumerate(encoded_bytes):
David Brown5e7c6dd2017-11-16 14:47:16 -070044 if count % 8 == 0:
45 print("\n" + indent, end='', file=file)
46 else:
47 print(" ", end='', file=file)
48 print("0x{:02x},".format(b), end='', file=file)
49 print("\n" + trailer, file=file)
50 if len_format is not None:
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020051 print(len_format.format(len(encoded_bytes)), file=file)
David Brown5e7c6dd2017-11-16 14:47:16 -070052
Denis Mingulovfaf2dd12023-09-26 09:22:44 +030053 def _emit_raw(self, encoded_bytes, file):
54 with FileHandler(file, 'wb') as file:
55 try:
56 # file.buffer is not part of the TextIOBase API
57 # and may not exist in some implementations.
58 file.buffer.write(encoded_bytes)
59 except AttributeError:
60 # raw binary data, can be for example io.BytesIO
61 file.write(encoded_bytes)
62
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020063 def emit_c_public(self, file=sys.stdout):
64 self._emit(
Bence Balogh97a20f12023-07-18 15:59:33 +020065 header="const unsigned char {}_pub_key[] = {{"
66 .format(self.shortname()),
David Brown5e7c6dd2017-11-16 14:47:16 -070067 trailer="};",
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020068 encoded_bytes=self.get_public_bytes(),
David Brown5e7c6dd2017-11-16 14:47:16 -070069 indent=" ",
Bence Balogh97a20f12023-07-18 15:59:33 +020070 len_format="const unsigned int {}_pub_key_len = {{}};"
71 .format(self.shortname()),
72 file=file)
73
74 def emit_c_public_hash(self, file=sys.stdout):
75 digest = Hash(SHA256())
76 digest.update(self.get_public_bytes())
77 self._emit(
78 header="const unsigned char {}_pub_key_hash[] = {{"
79 .format(self.shortname()),
80 trailer="};",
81 encoded_bytes=digest.finalize(),
82 indent=" ",
83 len_format="const unsigned int {}_pub_key_hash_len = {{}};"
84 .format(self.shortname()),
David Brown5e7c6dd2017-11-16 14:47:16 -070085 file=file)
86
Bence Baloghed8d68a2023-07-18 15:57:52 +020087 def emit_raw_public(self, file=sys.stdout):
Denis Mingulovfaf2dd12023-09-26 09:22:44 +030088 self._emit_raw(self.get_public_bytes(), file=file)
Bence Baloghed8d68a2023-07-18 15:57:52 +020089
Bence Balogh97a20f12023-07-18 15:59:33 +020090 def emit_raw_public_hash(self, file=sys.stdout):
91 digest = Hash(SHA256())
92 digest.update(self.get_public_bytes())
Denis Mingulovfaf2dd12023-09-26 09:22:44 +030093 self._emit_raw(digest.finalize(), file=file)
Bence Balogh97a20f12023-07-18 15:59:33 +020094
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020095 def emit_rust_public(self, file=sys.stdout):
96 self._emit(
Bence Balogh97a20f12023-07-18 15:59:33 +020097 header="static {}_PUB_KEY: &[u8] = &["
98 .format(self.shortname().upper()),
David Brown5e7c6dd2017-11-16 14:47:16 -070099 trailer="];",
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +0200100 encoded_bytes=self.get_public_bytes(),
David Brown5e7c6dd2017-11-16 14:47:16 -0700101 indent=" ",
102 file=file)
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +0200103
Fabio Utzig6f286772022-09-04 20:03:11 -0300104 def emit_public_pem(self, file=sys.stdout):
Denis Mingulovfaf2dd12023-09-26 09:22:44 +0300105 with FileHandler(file, 'w') as file:
106 print(str(self.get_public_pem(), 'utf-8'), file=file, end='')
Fabio Utzig6f286772022-09-04 20:03:11 -0300107
Antonio de Angelisc6e7e9b2022-11-15 15:06:40 +0000108 def emit_private(self, minimal, format, file=sys.stdout):
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +0200109 self._emit(
110 header="const unsigned char enc_priv_key[] = {",
111 trailer="};",
Antonio de Angelisc6e7e9b2022-11-15 15:06:40 +0000112 encoded_bytes=self.get_private_bytes(minimal, format),
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +0200113 indent=" ",
114 len_format="const unsigned int enc_priv_key_len = {};",
115 file=file)