-
Notifications
You must be signed in to change notification settings - Fork 25
/
curves.py
256 lines (171 loc) · 5.13 KB
/
curves.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
from abc import ABC, abstractmethod
from typing import Union, Type, ClassVar
from cryptography.hazmat.primitives.asymmetric import ed25519, ed448, x25519, x448, ec
from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurve
from pycose.keys.keytype import KtyEC2, KtyOKP
from pycose.utils import _CoseAttribute
EdwardsPrivateKey = Union[ed25519.Ed25519PrivateKey, ed448.Ed448PrivateKey, x25519.X25519PrivateKey, x448.X448PrivateKey]
class CoseCurve(_CoseAttribute, ABC):
"""
Base class for all COSE curves
Attributes:
curve_obj: A curve object from the cryptography package.
key_type: The key type associated with the curve.
size: The size of the coordinates over the curve.
"""
_registered_curves = {}
@classmethod
def get_registered_classes(cls):
return cls._registered_curves
curve_obj: ClassVar[Union[EllipticCurve, Type[EdwardsPrivateKey], None]]
key_type: ClassVar[Union[Type[KtyEC2], Type[KtyOKP], None]]
size: ClassVar[int]
##################################################
# SUPPORTED COSE CURVES #
##################################################
@CoseCurve.register_attribute()
class Reserved(CoseCurve):
"""
Reserved
Attributes:
**identifier** *0*
**fullname** *RESERVED*
**curve_obj** *None*
**key_type** *None*
**size** *0*
"""
identifier = 0
fullname = "RESERVED"
curve_obj = None
key_type = None
size = 0
@CoseCurve.register_attribute()
class P256(CoseCurve):
"""
Curve NIST P-256
Attributes:
**identifier** *1*
**fullname** *P_256*
**curve_obj** *SECP256R1 from the cryptography package*
**key_type** *KtyEC2*
**size** *32*
"""
identifier = 1
fullname = "P_256"
curve_obj = ec.SECP256R1()
key_type = KtyEC2
size = 32
@CoseCurve.register_attribute()
class P384(CoseCurve):
"""
Curve NIST P-384
Attributes:
**identifier** *2*
**fullname** *P_384*
**curve_obj** *SECP384R1 from the cryptography package*
**key_type** *KtyEC2*
**size** *48*
"""
identifier = 2
fullname = "P_384"
curve_obj = ec.SECP384R1()
key_type = KtyEC2
size = 48
@CoseCurve.register_attribute()
class P521(CoseCurve):
"""
Curve NIST P-521
Attributes:
**identifier** *3*
**fullname** *P_521*
**curve_obj** *SECP521R1 from the cryptography package*
**key_type** *KtyEC2*
**size** *66*
"""
identifier = 3
fullname = "P_521"
curve_obj = ec.SECP521R1()
key_type = KtyEC2
size = 66
@CoseCurve.register_attribute()
class X25519(CoseCurve):
"""
Curve25519 for the Diffie-Hellman function (X25519)
Attributes:
**identifier** *4*
**fullname** *X25519*
**curve_obj** *X25519PrivateKey from the cryptography package*
**key_type** *KtyOKP*
**size** *32*
"""
identifier = 4
fullname = "X25519"
curve_obj = x25519.X25519PrivateKey
key_type = KtyOKP
size = 32
@CoseCurve.register_attribute()
class X448(CoseCurve):
"""
Curve448 for the Diffie-Hellman function (X448)
Attributes:
**identifier** *5*
**fullname** *X448*
**curve_obj** *X25519PrivateKey from the cryptography package*
**key_type** *KtyOKP*
**size** *57*
"""
identifier = 5
fullname = "X448"
curve_obj = x448.X448PrivateKey
key_type = KtyOKP
size = 57
@CoseCurve.register_attribute()
class Ed25519(CoseCurve):
"""
Curve25519 for the EdDSA algorithm (Ed25519)
Attributes:
**identifier** *6*
**fullname** *ED25519*
**curve_obj** *Ed25519PrivateKey from the cryptography package*
**key_type** *KtyOKP*
**size** *32*
"""
identifier = 6
fullname = "ED25519"
curve_obj = ed25519.Ed25519PrivateKey
key_type = KtyOKP
size = 32
@CoseCurve.register_attribute()
class Ed448(CoseCurve):
"""
Curve448 for the EdDSA algorithm (Ed448)
Attributes:
**identifier** *7*
**fullname** *ED448*
**curve_obj** *Ed448PrivateKey from the cryptography package*
**key_type** *KtyOKP*
**size** *57*
"""
identifier = 7
fullname = "ED448"
curve_obj = ed448.Ed448PrivateKey
key_type = KtyOKP
size = 57
@CoseCurve.register_attribute()
class SECP256K1(CoseCurve):
"""
Curve secp256k1
Attributes:
**identifier** *8*
**fullname** *SECP256K1*
**curve_obj** *SECP256K1 from the cryptography package*
**key_type** *KtyEC2*
**size** *32*
"""
identifier = 8
fullname = "SECP256K1"
curve_obj = ec.SECP256K1()
key_type = KtyEC2
size = 32
if __name__ == '__main__':
print(CoseCurve.get_registered_classes())