2017-11-17 05:47:16 +08:00
|
|
|
"""General key class."""
|
|
|
|
|
2021-01-27 06:04:05 +08:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2023-09-26 14:22:44 +08:00
|
|
|
import binascii
|
|
|
|
import io
|
|
|
|
import os
|
2017-11-17 05:47:16 +08:00
|
|
|
import sys
|
2023-07-18 21:59:33 +08:00
|
|
|
from cryptography.hazmat.primitives.hashes import Hash, SHA256
|
2017-11-17 05:47:16 +08:00
|
|
|
|
|
|
|
AUTOGEN_MESSAGE = "/* Autogenerated by imgtool.py, do not edit. */"
|
|
|
|
|
2022-11-15 23:06:40 +08:00
|
|
|
|
2023-09-26 14:22:44 +08:00
|
|
|
class FileHandler(object):
|
|
|
|
def __init__(self, file, *args, **kwargs):
|
|
|
|
self.file_in = file
|
|
|
|
self.args = args
|
|
|
|
self.kwargs = kwargs
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
if isinstance(self.file_in, (str, bytes, os.PathLike)):
|
|
|
|
self.file = open(self.file_in, *self.args, **self.kwargs)
|
|
|
|
else:
|
|
|
|
self.file = self.file_in
|
|
|
|
return self.file
|
|
|
|
|
|
|
|
def __exit__(self, *args):
|
|
|
|
if self.file != self.file_in:
|
|
|
|
self.file.close()
|
|
|
|
|
|
|
|
|
2017-11-17 05:47:16 +08:00
|
|
|
class KeyClass(object):
|
2023-07-18 21:51:54 +08:00
|
|
|
def _emit(self, header, trailer, encoded_bytes, indent, file=sys.stdout,
|
|
|
|
len_format=None):
|
2023-09-26 14:22:44 +08:00
|
|
|
with FileHandler(file, 'w') as file:
|
2023-07-18 21:51:54 +08:00
|
|
|
self._emit_to_output(header, trailer, encoded_bytes, indent,
|
2023-09-26 14:22:44 +08:00
|
|
|
file, len_format)
|
2023-07-18 21:51:54 +08:00
|
|
|
|
|
|
|
def _emit_to_output(self, header, trailer, encoded_bytes, indent, file,
|
|
|
|
len_format):
|
2017-11-17 05:47:16 +08:00
|
|
|
print(AUTOGEN_MESSAGE, file=file)
|
|
|
|
print(header, end='', file=file)
|
2019-11-28 22:06:12 +08:00
|
|
|
for count, b in enumerate(encoded_bytes):
|
2017-11-17 05:47:16 +08:00
|
|
|
if count % 8 == 0:
|
|
|
|
print("\n" + indent, end='', file=file)
|
|
|
|
else:
|
|
|
|
print(" ", end='', file=file)
|
|
|
|
print("0x{:02x},".format(b), end='', file=file)
|
|
|
|
print("\n" + trailer, file=file)
|
|
|
|
if len_format is not None:
|
2019-11-28 22:06:12 +08:00
|
|
|
print(len_format.format(len(encoded_bytes)), file=file)
|
2017-11-17 05:47:16 +08:00
|
|
|
|
2023-09-26 14:22:44 +08:00
|
|
|
def _emit_raw(self, encoded_bytes, file):
|
|
|
|
with FileHandler(file, 'wb') as file:
|
|
|
|
try:
|
|
|
|
# file.buffer is not part of the TextIOBase API
|
|
|
|
# and may not exist in some implementations.
|
|
|
|
file.buffer.write(encoded_bytes)
|
|
|
|
except AttributeError:
|
|
|
|
# raw binary data, can be for example io.BytesIO
|
|
|
|
file.write(encoded_bytes)
|
|
|
|
|
2019-11-28 22:06:12 +08:00
|
|
|
def emit_c_public(self, file=sys.stdout):
|
|
|
|
self._emit(
|
2023-07-18 21:59:33 +08:00
|
|
|
header="const unsigned char {}_pub_key[] = {{"
|
|
|
|
.format(self.shortname()),
|
2017-11-17 05:47:16 +08:00
|
|
|
trailer="};",
|
2019-11-28 22:06:12 +08:00
|
|
|
encoded_bytes=self.get_public_bytes(),
|
2017-11-17 05:47:16 +08:00
|
|
|
indent=" ",
|
2023-07-18 21:59:33 +08:00
|
|
|
len_format="const unsigned int {}_pub_key_len = {{}};"
|
|
|
|
.format(self.shortname()),
|
|
|
|
file=file)
|
|
|
|
|
|
|
|
def emit_c_public_hash(self, file=sys.stdout):
|
|
|
|
digest = Hash(SHA256())
|
|
|
|
digest.update(self.get_public_bytes())
|
|
|
|
self._emit(
|
|
|
|
header="const unsigned char {}_pub_key_hash[] = {{"
|
|
|
|
.format(self.shortname()),
|
|
|
|
trailer="};",
|
|
|
|
encoded_bytes=digest.finalize(),
|
|
|
|
indent=" ",
|
|
|
|
len_format="const unsigned int {}_pub_key_hash_len = {{}};"
|
|
|
|
.format(self.shortname()),
|
2017-11-17 05:47:16 +08:00
|
|
|
file=file)
|
|
|
|
|
2023-07-18 21:57:52 +08:00
|
|
|
def emit_raw_public(self, file=sys.stdout):
|
2023-09-26 14:22:44 +08:00
|
|
|
self._emit_raw(self.get_public_bytes(), file=file)
|
2023-07-18 21:57:52 +08:00
|
|
|
|
2023-07-18 21:59:33 +08:00
|
|
|
def emit_raw_public_hash(self, file=sys.stdout):
|
|
|
|
digest = Hash(SHA256())
|
|
|
|
digest.update(self.get_public_bytes())
|
2023-09-26 14:22:44 +08:00
|
|
|
self._emit_raw(digest.finalize(), file=file)
|
2023-07-18 21:59:33 +08:00
|
|
|
|
2019-11-28 22:06:12 +08:00
|
|
|
def emit_rust_public(self, file=sys.stdout):
|
|
|
|
self._emit(
|
2023-07-18 21:59:33 +08:00
|
|
|
header="static {}_PUB_KEY: &[u8] = &["
|
|
|
|
.format(self.shortname().upper()),
|
2017-11-17 05:47:16 +08:00
|
|
|
trailer="];",
|
2019-11-28 22:06:12 +08:00
|
|
|
encoded_bytes=self.get_public_bytes(),
|
2017-11-17 05:47:16 +08:00
|
|
|
indent=" ",
|
|
|
|
file=file)
|
2019-11-28 22:06:12 +08:00
|
|
|
|
2022-09-05 07:03:11 +08:00
|
|
|
def emit_public_pem(self, file=sys.stdout):
|
2023-09-26 14:22:44 +08:00
|
|
|
with FileHandler(file, 'w') as file:
|
|
|
|
print(str(self.get_public_pem(), 'utf-8'), file=file, end='')
|
2022-09-05 07:03:11 +08:00
|
|
|
|
2022-11-15 23:06:40 +08:00
|
|
|
def emit_private(self, minimal, format, file=sys.stdout):
|
2019-11-28 22:06:12 +08:00
|
|
|
self._emit(
|
|
|
|
header="const unsigned char enc_priv_key[] = {",
|
|
|
|
trailer="};",
|
2022-11-15 23:06:40 +08:00
|
|
|
encoded_bytes=self.get_private_bytes(minimal, format),
|
2019-11-28 22:06:12 +08:00
|
|
|
indent=" ",
|
|
|
|
len_format="const unsigned int enc_priv_key_len = {};",
|
|
|
|
file=file)
|