1
1
from __future__ import division
2
2
3
3
from six import PY2
4
- from . import der , ecdsa , ellipticcurve
4
+ from . import der , ecdsa , ellipticcurve , eddsa
5
5
from .util import orderlen , number_to_string , string_to_number
6
- from ._compat import normalise_bytes
6
+ from ._compat import normalise_bytes , bit_length
7
7
8
8
9
9
# orderlen was defined in this module previously, so keep it in __all__,
33
33
"BRAINPOOLP512r1" ,
34
34
"PRIME_FIELD_OID" ,
35
35
"CHARACTERISTIC_TWO_FIELD_OID" ,
36
+ "Ed25519" ,
37
+ "Ed448" ,
36
38
]
37
39
38
40
@@ -51,8 +53,16 @@ def __init__(self, name, curve, generator, oid, openssl_name=None):
51
53
self .curve = curve
52
54
self .generator = generator
53
55
self .order = generator .order ()
54
- self .baselen = orderlen (self .order )
55
- self .verifying_key_length = 2 * orderlen (curve .p ())
56
+ if isinstance (curve , ellipticcurve .CurveEdTw ):
57
+ # EdDSA keys are special in that both private and public
58
+ # are the same size (as it's defined only with compressed points)
59
+
60
+ # +1 for the sign bit and then round up
61
+ self .baselen = (bit_length (curve .p ()) + 1 + 7 ) // 8
62
+ self .verifying_key_length = self .baselen
63
+ else :
64
+ self .baselen = orderlen (self .order )
65
+ self .verifying_key_length = 2 * orderlen (curve .p ())
56
66
self .signature_length = 2 * self .baselen
57
67
self .oid = oid
58
68
if oid :
@@ -90,13 +100,23 @@ def to_der(self, encoding=None, point_encoding="uncompressed"):
90
100
else :
91
101
encoding = "explicit"
92
102
103
+ if encoding not in ("named_curve" , "explicit" ):
104
+ raise ValueError (
105
+ "Only 'named_curve' and 'explicit' encodings supported"
106
+ )
107
+
93
108
if encoding == "named_curve" :
94
109
if not self .oid :
95
110
raise UnknownCurveError (
96
111
"Can't encode curve using named_curve encoding without "
97
112
"associated curve OID"
98
113
)
99
114
return der .encode_oid (* self .oid )
115
+ elif isinstance (self .curve , ellipticcurve .CurveEdTw ):
116
+ assert encoding == "explicit"
117
+ raise UnknownCurveError (
118
+ "Twisted Edwards curves don't support explicit encoding"
119
+ )
100
120
101
121
# encode the ECParameters sequence
102
122
curve_p = self .curve .p ()
@@ -408,6 +428,16 @@ def from_pem(cls, string, valid_encodings=None):
408
428
)
409
429
410
430
431
+ Ed25519 = Curve (
432
+ "Ed25519" , eddsa .curve_ed25519 , eddsa .generator_ed25519 , (1 , 3 , 101 , 112 ),
433
+ )
434
+
435
+
436
+ Ed448 = Curve (
437
+ "Ed448" , eddsa .curve_ed448 , eddsa .generator_ed448 , (1 , 3 , 101 , 113 ),
438
+ )
439
+
440
+
411
441
# no order in particular, but keep previously added curves first
412
442
curves = [
413
443
NIST192p ,
@@ -427,6 +457,8 @@ def from_pem(cls, string, valid_encodings=None):
427
457
SECP112r2 ,
428
458
SECP128r1 ,
429
459
SECP160r1 ,
460
+ Ed25519 ,
461
+ Ed448 ,
430
462
]
431
463
432
464
0 commit comments