forked from TimothyClaeys/pycose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa.py
284 lines (229 loc) · 8.51 KB
/
rsa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
from binascii import hexlify
from typing import Optional, List, TYPE_CHECKING, Type
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from cose.exceptions import CoseIllegalKeyType, CoseInvalidKey
from cose.keys.cosekey import CoseKey, KeyParam, KpKty
from cose.keys.keyparam import (RSAKeyParam, RSAKpN, RSAKpE, RSAKpD, RSAKpP,
RSAKpQ, RSAKpDP, RSAKpDQ, RSAKpQInv)
from cose.keys.keytype import KtyRSA
if TYPE_CHECKING:
from cose.algorithms import CoseAlg
from cose.keys.cosekey import KEYOPS
def from_bstr(enc):
return int.from_bytes(enc, byteorder='big')
def to_bstr(dec):
blen = (dec.bit_length() + 7) // 8
return dec.to_bytes(blen, byteorder='big')
#: Map from kwarg key to parameter object
PARAM = {
'e': RSAKpE,
'n': RSAKpN,
'd': RSAKpD,
'p': RSAKpP,
'q': RSAKpQ,
'dp': RSAKpDP,
'dq': RSAKpDQ,
'qinv': RSAKpQInv,
}
@CoseKey.record_kty(KtyRSA)
class RSAKey(CoseKey):
@classmethod
def from_dict(cls, cose_key: dict) -> 'RSAKey':
"""
Returns an initialized COSE Key object of type RSAKey.
:param cose_key: Dict containing COSE Key parameters and there values.
:return: an initialized RSAKey key
"""
kwargs = {}
for (attr, kob) in PARAM.items():
if kob in cose_key:
val = cose_key[kob]
elif kob.identifier in cose_key:
val = cose_key[kob.identifier]
elif kob.fullname in cose_key:
val = cose_key[kob.fullname]
else:
val = b''
kwargs[attr] = val
kwargs['optional_params'] = cose_key
return cls(**kwargs)
@classmethod
def from_cryptograpy_key_obj(cls, ext_key) -> 'RSAKey':
"""
Returns an initialized COSE Key object of type RSAKey.
:param ext_key: Python cryptography key.
:return: an initialized RSA key
"""
if hasattr(ext_key, 'private_numbers'):
priv_nums = ext_key.private_numbers()
pub_nums = priv_nums.public_numbers
else:
priv_nums = None
pub_nums = ext_key.public_numbers()
cose_key = {}
if pub_nums:
cose_key.update({
RSAKpE: to_bstr(pub_nums.e),
RSAKpN: to_bstr(pub_nums.n),
})
if priv_nums:
cose_key.update({
RSAKpD: to_bstr(priv_nums.d),
RSAKpP: to_bstr(priv_nums.p),
RSAKpQ: to_bstr(priv_nums.q),
RSAKpDP: to_bstr(priv_nums.dmp1),
RSAKpDQ: to_bstr(priv_nums.dmq1),
RSAKpQInv: to_bstr(priv_nums.iqmp),
})
return RSAKey.from_dict(cose_key)
def __init__(self, e: bytes=b'', n: bytes=b'',
d: bytes=b'', p: bytes=b'', q: bytes=b'',
dp: bytes=b'', dq: bytes=b'', qinv: bytes=b'',
optional_params: Optional[dict]=None):
transformed_dict = {}
if len(e) == 0 and len(n) == 0:
raise CoseInvalidKey("Either the public values or the private value must be specified")
new_dict = dict({KpKty: KtyRSA, RSAKpE: e, RSAKpN: n})
if len(d) != 0:
new_dict.update({RSAKpD: d})
if len(p) != 0:
new_dict.update({RSAKpP: p})
if len(q) != 0:
new_dict.update({RSAKpQ: q})
if len(dp) != 0:
new_dict.update({RSAKpDP: dp})
if len(dq) != 0:
new_dict.update({RSAKpDQ: dq})
if len(qinv) != 0:
new_dict.update({RSAKpQInv: qinv})
if optional_params is not None:
new_dict.update(optional_params)
for _key_attribute, _value in new_dict.items():
try:
# translate the key_attribute
kp = RSAKeyParam.from_id(_key_attribute)
# parse the value of the key attribute if possible
if hasattr(kp.value_parser, '__call__'):
_value = kp.value_parser(_value)
# store in new dict
transformed_dict[kp] = _value
except ValueError:
transformed_dict[_key_attribute] = _value
# final check if key type is correct
if transformed_dict.get(KpKty) != KtyRSA:
raise CoseIllegalKeyType(f"Illegal key type in RSA COSE Key: {transformed_dict.get(KpKty)}")
super(RSAKey, self).__init__(transformed_dict)
@property
def n(self) -> bytes:
return self.store.get(RSAKpN, b'')
@n.setter
def n(self, n: bytes):
if type(n) is not bytes:
raise TypeError("parameter n must be of type 'bytes'")
self.store[RSAKpN] = n
@property
def e(self) -> bytes:
return self.store.get(RSAKpE, b'')
@e.setter
def e(self, e: bytes):
if type(e) is not bytes:
raise TypeError("parameter e must be of type 'bytes'")
self.store[RSAKpE] = e
@property
def d(self) -> bytes:
return self.store.get(RSAKpD, b'')
@d.setter
def d(self, d: bytes):
if type(d) is not bytes:
raise TypeError("parameter d must be of type 'bytes'")
self.store[RSAKpD] = d
@property
def p(self) -> bytes:
return self.store.get(RSAKpP, b'')
@p.setter
def p(self, p: bytes):
if type(p) is not bytes:
raise TypeError("parameter p must be of type 'bytes'")
self.store[RSAKpP] = p
@property
def q(self) -> bytes:
return self.store.get(RSAKpQ, b'')
@q.setter
def q(self, q: bytes):
if type(q) is not bytes:
raise TypeError("parameter q must be of type 'bytes'")
self.store[RSAKpQ] = q
@property
def dP(self) -> bytes:
return self.store.get(RSAKpDP, b'')
@dP.setter
def dP(self, dp: bytes):
if type(dp) is not bytes:
raise TypeError("parameter dp must be of type 'bytes'")
self.store[RSAKpDP] = dp
@property
def dQ(self) -> bytes:
return self.store.get(RSAKpDQ, b'')
@dQ.setter
def dQ(self, dq: bytes):
if type(dq) is not bytes:
raise TypeError("parameter dq must be of type 'bytes'")
self.store[RSAKpDQ] = dq
@property
def qInv(self) -> bytes:
return self.store.get(RSAKpQInv, b'')
@qInv.setter
def qInv(self, qinv: bytes):
if type(qinv) is not bytes:
raise TypeError("parameter dq must be of type 'bytes'")
self.store[RSAKpQInv] = qinv
def verify(self, key_type: Type['RSAKey'], algorithm: Type['CoseAlg'], key_ops: List[Type['KEYOPS']]):
super(RSAKey, self).verify(key_type, algorithm, key_ops)
@property
def is_valid_key(self):
pub_attrs = {'e', 'n'}
priv_attrs = {'d', 'p', 'q', 'dP', 'dQ', 'qInv'}
pub_set = [bool(getattr(self, name)) for name in pub_attrs]
priv_set = [bool(getattr(self, name)) for name in priv_attrs]
if not all(pub_set):
return False
if any(priv_set) and not all(priv_set):
return False
return True
@staticmethod
def generate_key(key_bits: int) -> 'RSAKey':
"""
Generate a random RSAKey COSE key object.
:param key_bits: Specify the number of private key bits.
:raises CoseIllegalKeyOps: Invalid key operation for this key type.
:return: An COSE `RSAKey` key.
"""
ext_key = rsa.generate_private_key(public_exponent=65537, key_size=key_bits, backend=default_backend())
return RSAKey.from_cryptograpy_key_obj(ext_key)
def __repr__(self):
hdr = f'<COSE_Key(RSAKey): {self._key_repr()}>'
return hdr
def to_cryptograpy_key_obj(self):
''' Get the `crypographic` library representation of this key.
:return: An :py:cls:`RSAPublicKey` or :py:cls:`RSAPrivateKey` object.
'''
pub_nums = rsa.RSAPublicNumbers(
e=from_bstr(self.e),
n=from_bstr(self.n)
)
if self.d:
priv_nums = rsa.RSAPrivateNumbers(
p=from_bstr(self.p),
q=from_bstr(self.q),
d=from_bstr(self.d),
dmp1=from_bstr(self.dP),
dmq1=from_bstr(self.dQ),
iqmp=from_bstr(self.qInv),
public_numbers=pub_nums
)
ext_key = priv_nums.private_key(default_backend())
else:
ext_key = pub_nums.public_key(default_backend())
return ext_key
RSA = RSAKey