Skip to content

Commit 9080d1d

Browse files
committed
fix length decoding
the same issues as with decoding integers happen with the NIST521p curve as it's large enough that the length encoding of some fields needs to use multi-byte encoding backport of a655d6f
1 parent 897178c commit 9080d1d

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

ecdsa/der.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,17 @@ def encode_length(l):
148148
return int2byte(0x80|llen) + s
149149

150150
def read_length(string):
151+
if not string:
152+
raise UnexpectedDER("Empty string can't encode valid length value")
151153
num = string[0] if isinstance(string[0], integer_types) else ord(string[0])
152154
if not (num & 0x80):
153155
# short form
154156
return (num & 0x7f), 1
155157
# else long-form: b0&0x7f is number of additional base256 length bytes,
156158
# big-endian
157159
llen = num & 0x7f
160+
if not llen:
161+
raise UnexpectedDER("Invalid length encoding, length byte is 0")
158162
if llen > len(string)-1:
159163
raise UnexpectedDER("ran out of length bytes")
160164
return int(binascii.hexlify(string[1:1+llen]), 16), 1+llen

ecdsa/test_malformed_sigs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# few thousand slow test cases; execute the most interesting only
1818

1919
#for curve in curves:
20-
for curve in [NIST256p]:
20+
for curve in [NIST521p]:
2121
#for hash_alg in ["md5", "sha1", "sha224", "sha256", "sha384", "sha512"]:
2222
for hash_alg in ["sha256"]:
2323
key = SigningKey.generate(curve)

0 commit comments

Comments
 (0)