Skip to content

Commit d7156a7

Browse files
committed
Make sure to pad the encoding to the size of the order of the curve.
By https://github.com/jschlyter
1 parent d5e57e6 commit d7156a7

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/cryptojwt/jws/dsa.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ def __init__(self, algorithm='ES256'):
3030

3131
def sign(self, msg, key):
3232
"""
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
3435
3536
:param msg: The message
36-
:param key: An ec.EllipticCurvePrivateKey
37+
:param key: An ec.EllipticCurvePrivateKey instance
3738
:return:
3839
"""
3940

@@ -43,12 +44,13 @@ def sign(self, msg, key):
4344
"ec.EllipticCurvePrivateKey")
4445

4546
self._cross_check(key.public_key())
46-
47+
num_bits = key.curve.key_size
48+
num_bytes = (num_bits + 7) // 8
4749
asn1sig = key.sign(msg, ec.ECDSA(self.hash_algorithm()))
4850
# Cryptography returns ASN.1-encoded signature data; decode as JWS
4951
# uses raw signatures (r||s)
5052
(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)
5254

5355
def verify(self, msg, sig, key):
5456
"""
@@ -66,6 +68,11 @@ def verify(self, msg, sig, key):
6668
"ec.EllipticCurvePublicKey")
6769
self._cross_check(key)
6870

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+
6976
try:
7077
# cryptography uses ASN.1-encoded signature data; split JWS
7178
# signature (r||s) and encode before verification

0 commit comments

Comments
 (0)