@@ -30,10 +30,11 @@ def __init__(self, algorithm='ES256'):
30
30
31
31
def sign (self , msg , key ):
32
32
"""
33
- Signs a message using a Elliptic curve key.
33
+ Create a signature over a message as defined in RFC7515 using an
34
+ Elliptic curve key
34
35
35
36
:param msg: The message
36
- :param key: An ec.EllipticCurvePrivateKey
37
+ :param key: An ec.EllipticCurvePrivateKey instance
37
38
:return:
38
39
"""
39
40
@@ -43,12 +44,13 @@ def sign(self, msg, key):
43
44
"ec.EllipticCurvePrivateKey" )
44
45
45
46
self ._cross_check (key .public_key ())
46
-
47
+ num_bits = key .curve .key_size
48
+ num_bytes = (num_bits + 7 ) // 8
47
49
asn1sig = key .sign (msg , ec .ECDSA (self .hash_algorithm ()))
48
50
# Cryptography returns ASN.1-encoded signature data; decode as JWS
49
51
# uses raw signatures (r||s)
50
52
(r , s ) = decode_dss_signature (asn1sig )
51
- return int_to_bytes (r ) + int_to_bytes (s )
53
+ return int_to_bytes (r , num_bytes ) + int_to_bytes (s , num_bytes )
52
54
53
55
def verify (self , msg , sig , key ):
54
56
"""
@@ -66,6 +68,11 @@ def verify(self, msg, sig, key):
66
68
"ec.EllipticCurvePublicKey" )
67
69
self ._cross_check (key )
68
70
71
+ num_bits = key .curve .key_size
72
+ num_bytes = (num_bits + 7 ) // 8
73
+ if len (sig ) != 2 * num_bytes :
74
+ raise ValueError ('Invalid signature' )
75
+
69
76
try :
70
77
# cryptography uses ASN.1-encoded signature data; split JWS
71
78
# signature (r||s) and encode before verification
0 commit comments