Skip to content

Digest truncation should happen according to bitlength not bytelength #197

@kyrkonos

Description

@kyrkonos

FIPS 186.4 states that:

If the length of the output of the hash function is greater than the bit length of n, then the leftmost n bits of the hash function output block shall be used in any calculation using the hash function output during the generation or verification of a digital signature.

(6.1.1 https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf)

However in python-ecdsa/src/ecdsa/keys.py 686-688, 1359-1361:

digest = normalise_bytes(digest)
if allow_truncate:
digest = digest[: self.curve.baselen]

truncation is based on the baselen value, which is calculated by python-ecdsa/src/ecdsa/util.py 62-63:

def orderlen(order):
return (1 + len("%x" % order)) // 2 # bytes

which returns the length in bytes and not bits as it should. If the bitlength happens to be a multiple of 8 (as usual) then the issue remains hidden.

Metadata

Metadata

Assignees

Labels

bugunintended behaviour in ecdsa code

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions