2
2
3
3
# SPDX-License-Identifier: Apache-2.0
4
4
5
- import binascii
6
- import io
7
5
import os
8
6
import sys
7
+
8
+ from cryptography .hazmat .primitives .asymmetric .types import PrivateKeyTypes , PublicKeyTypes
9
9
from cryptography .hazmat .primitives .hashes import Hash , SHA256
10
10
11
+ from imgtool import keys
12
+
11
13
AUTOGEN_MESSAGE = "/* Autogenerated by imgtool.py, do not edit. */"
12
14
13
15
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
+
14
24
class FileHandler (object ):
15
25
def __init__ (self , file , * args , ** kwargs ):
16
26
self .file_in = file
@@ -34,7 +44,7 @@ def _emit(self, header, trailer, encoded_bytes, indent, file=sys.stdout,
34
44
len_format = None ):
35
45
with FileHandler (file , 'w' ) as file :
36
46
self ._emit_to_output (header , trailer , encoded_bytes , indent ,
37
- file , len_format )
47
+ file , len_format )
38
48
39
49
def _emit_to_output (self , header , trailer , encoded_bytes , indent , file ,
40
50
len_format ):
@@ -62,27 +72,27 @@ def _emit_raw(self, encoded_bytes, file):
62
72
63
73
def emit_c_public (self , file = sys .stdout ):
64
74
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 )
73
83
74
84
def emit_c_public_hash (self , file = sys .stdout ):
75
85
digest = Hash (SHA256 ())
76
86
digest .update (self .get_public_bytes ())
77
87
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 )
86
96
87
97
def emit_raw_public (self , file = sys .stdout ):
88
98
self ._emit_raw (self .get_public_bytes (), file = file )
@@ -94,22 +104,22 @@ def emit_raw_public_hash(self, file=sys.stdout):
94
104
95
105
def emit_rust_public (self , file = sys .stdout ):
96
106
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 )
103
113
104
114
def emit_public_pem (self , file = sys .stdout ):
105
115
with FileHandler (file , 'w' ) as file :
106
116
print (str (self .get_public_pem (), 'utf-8' ), file = file , end = '' )
107
117
108
118
def emit_private (self , minimal , format , file = sys .stdout ):
109
119
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