22
33# SPDX-License-Identifier: Apache-2.0
44
5- import binascii
6- import io
75import os
86import sys
7+
8+ from cryptography .hazmat .primitives .asymmetric .types import PrivateKeyTypes , PublicKeyTypes
99from cryptography .hazmat .primitives .hashes import Hash , SHA256
1010
11+ from imgtool import keys
12+
1113AUTOGEN_MESSAGE = "/* Autogenerated by imgtool.py, do not edit. */"
1214
1315
16+ def key_types_matching (key : PrivateKeyTypes , enckey : PublicKeyTypes ):
17+ type_dict = {keys .ECDSA256P1 : keys .ECDSA256P1Public ,
18+ keys .ECDSA384P1 : keys .ECDSA384P1Public ,
19+ keys .Ed25519 : keys .X25519Public ,
20+ keys .RSA : keys .RSAPublic }
21+ return type_dict [type (key )] == type (enckey )
22+
23+
1424class FileHandler (object ):
1525 def __init__ (self , file , * args , ** kwargs ):
1626 self .file_in = file
@@ -34,7 +44,7 @@ def _emit(self, header, trailer, encoded_bytes, indent, file=sys.stdout,
3444 len_format = None ):
3545 with FileHandler (file , 'w' ) as file :
3646 self ._emit_to_output (header , trailer , encoded_bytes , indent ,
37- file , len_format )
47+ file , len_format )
3848
3949 def _emit_to_output (self , header , trailer , encoded_bytes , indent , file ,
4050 len_format ):
@@ -62,27 +72,27 @@ def _emit_raw(self, encoded_bytes, file):
6272
6373 def emit_c_public (self , file = sys .stdout ):
6474 self ._emit (
65- header = "const unsigned char {}_pub_key[] = {{"
66- .format (self .shortname ()),
67- trailer = "};" ,
68- encoded_bytes = self .get_public_bytes (),
69- indent = " " ,
70- len_format = "const unsigned int {}_pub_key_len = {{}};"
71- .format (self .shortname ()),
72- file = file )
75+ header = "const unsigned char {}_pub_key[] = {{"
76+ .format (self .shortname ()),
77+ trailer = "};" ,
78+ encoded_bytes = self .get_public_bytes (),
79+ indent = " " ,
80+ len_format = "const unsigned int {}_pub_key_len = {{}};"
81+ .format (self .shortname ()),
82+ file = file )
7383
7484 def emit_c_public_hash (self , file = sys .stdout ):
7585 digest = Hash (SHA256 ())
7686 digest .update (self .get_public_bytes ())
7787 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 ()),
85- file = file )
88+ header = "const unsigned char {}_pub_key_hash[] = {{"
89+ .format (self .shortname ()),
90+ trailer = "};" ,
91+ encoded_bytes = digest .finalize (),
92+ indent = " " ,
93+ len_format = "const unsigned int {}_pub_key_hash_len = {{}};"
94+ .format (self .shortname ()),
95+ file = file )
8696
8797 def emit_raw_public (self , file = sys .stdout ):
8898 self ._emit_raw (self .get_public_bytes (), file = file )
@@ -94,22 +104,22 @@ def emit_raw_public_hash(self, file=sys.stdout):
94104
95105 def emit_rust_public (self , file = sys .stdout ):
96106 self ._emit (
97- header = "static {}_PUB_KEY: &[u8] = &["
98- .format (self .shortname ().upper ()),
99- trailer = "];" ,
100- encoded_bytes = self .get_public_bytes (),
101- indent = " " ,
102- file = file )
107+ header = "static {}_PUB_KEY: &[u8] = &["
108+ .format (self .shortname ().upper ()),
109+ trailer = "];" ,
110+ encoded_bytes = self .get_public_bytes (),
111+ indent = " " ,
112+ file = file )
103113
104114 def emit_public_pem (self , file = sys .stdout ):
105115 with FileHandler (file , 'w' ) as file :
106116 print (str (self .get_public_pem (), 'utf-8' ), file = file , end = '' )
107117
108118 def emit_private (self , minimal , format , file = sys .stdout ):
109119 self ._emit (
110- header = "const unsigned char enc_priv_key[] = {" ,
111- trailer = "};" ,
112- encoded_bytes = self .get_private_bytes (minimal , format ),
113- indent = " " ,
114- len_format = "const unsigned int enc_priv_key_len = {};" ,
115- file = file )
120+ header = "const unsigned char enc_priv_key[] = {" ,
121+ trailer = "};" ,
122+ encoded_bytes = self .get_private_bytes (minimal , format ),
123+ indent = " " ,
124+ len_format = "const unsigned int enc_priv_key_len = {};" ,
125+ file = file )
0 commit comments