Skip to content

Commit 941a8dd

Browse files
committed
Expose allow_truncate option in SigningKey.sign() and VerifyingKey.verify()
1 parent 14e673d commit 941a8dd

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/ecdsa/keys.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,8 @@ def to_der(self, point_encoding="uncompressed"):
602602
)
603603

604604
def verify(
605-
self, signature, data, hashfunc=None, sigdecode=sigdecode_string
605+
self, signature, data, hashfunc=None, sigdecode=sigdecode_string,
606+
allow_truncate=True
606607
):
607608
"""
608609
Verify a signature made over provided data.
@@ -629,6 +630,11 @@ def verify(
629630
second one. See :func:`ecdsa.util.sigdecode_string` and
630631
:func:`ecdsa.util.sigdecode_der` for examples.
631632
:type sigdecode: callable
633+
:param bool allow_truncate: if True, the provided digest can have
634+
bigger bit-size than the order of the curve, the extra bits (at
635+
the end of the digest) will be truncated. Use it when verifying
636+
SHA-384 output using NIST256p or in similar situations. Defaults to
637+
True.
632638
633639
:raises BadSignatureError: if the signature is invalid or malformed
634640
@@ -641,7 +647,7 @@ def verify(
641647

642648
hashfunc = hashfunc or self.default_hashfunc
643649
digest = hashfunc(data).digest()
644-
return self.verify_digest(signature, digest, sigdecode, True)
650+
return self.verify_digest(signature, digest, sigdecode, allow_truncate)
645651

646652
def verify_digest(
647653
self,
@@ -1262,6 +1268,7 @@ def sign(
12621268
hashfunc=None,
12631269
sigencode=sigencode_string,
12641270
k=None,
1271+
allow_truncate=True,
12651272
):
12661273
"""
12671274
Create signature over data using the probabilistic ECDSA algorithm.
@@ -1298,6 +1305,11 @@ def sign(
12981305
:param int k: a pre-selected nonce for calculating the signature.
12991306
In typical use cases, it should be set to None (the default) to
13001307
allow its generation from an entropy source.
1308+
:param bool allow_truncate: if True, the provided digest can have
1309+
bigger bit-size than the order of the curve, the extra bits (at
1310+
the end of the digest) will be truncated. Use it when signing
1311+
SHA-384 output using NIST256p or in similar situations. True by
1312+
default.
13011313
13021314
:raises RSZeroError: in the unlikely event when "r" parameter or
13031315
"s" parameter is equal 0 as that would leak the key. Calee should
@@ -1309,7 +1321,7 @@ def sign(
13091321
hashfunc = hashfunc or self.default_hashfunc
13101322
data = normalise_bytes(data)
13111323
h = hashfunc(data).digest()
1312-
return self.sign_digest(h, entropy, sigencode, k, allow_truncate=True)
1324+
return self.sign_digest(h, entropy, sigencode, k, allow_truncate)
13131325

13141326
def sign_digest(
13151327
self,

0 commit comments

Comments
 (0)