pyca / cryptography

cryptography is a package designed to expose cryptographic primitives and recipes to Python developers.
https://cryptography.io
Other
6.68k stars 1.53k forks source link

Bug in HKDF? #4032

Closed wilko77 closed 6 years ago

wilko77 commented 6 years ago

I think the computation of max_length in src/cryptography/hazmat/primitives/kdf/hkdf.py is wrong.

RFC5869 states on page 3 that the input L of the HKDF-Expand function describes the "length of output keying material in octets (<= 255*HashLen)". An octet consists of 8 bit.

Currently, max_length is computed as:

max_length = 255 * (algorithm.digest_size // 8)

The problem is, that algorithm.digest_size returns the size of the digest in bytes. (There are 8 bits per byte). Therefore, the division by 8 is wrong, and thus, max_length is unnecessarily small.

(same applies for the computation of salt as well (line 33), in the case where salt is None)

reaperhulk commented 6 years ago

Thanks for the report. This is indeed a bug. We should allow longer output lengths. As for salt, that is a bug as well, but (fortuitously) is benign. An explanation:

So we passed too few null bytes and OpenSSL obligingly (and in accordance with the spec) padded it out to the correct number of null bytes, thus making the call return the correct value despite the wrong input. (We will still fix it though!)

A PR will be forthcoming.