Skip to content

Commit b924696

Browse files
alexreaperhulk
authored andcommitted
Fixes #3211 -- fixed hkdf's output with short length (#3215)
1 parent 21ac453 commit b924696

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/cryptography/hazmat/primitives/kdf/hkdf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def _expand(self, key_material):
9191
output = [b""]
9292
counter = 1
9393

94-
while (self._algorithm.digest_size // 8) * len(output) < self._length:
94+
while self._algorithm.digest_size * (len(output) - 1) < self._length:
9595
h = hmac.HMAC(key_material, self._algorithm, backend=self._backend)
9696
h.update(output[-1])
9797
h.update(self._info)

tests/hazmat/primitives/test_hkdf.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,17 @@ def test_unicode_typeerror(self, backend):
142142

143143
hkdf.verify(b"foo", u"bar")
144144

145+
def test_derive_short_output(self, backend):
146+
hkdf = HKDF(
147+
hashes.SHA256(),
148+
4,
149+
salt=None,
150+
info=None,
151+
backend=backend
152+
)
153+
154+
assert hkdf.derive(b"\x01" * 16) == b"gJ\xfb{"
155+
145156

146157
@pytest.mark.requires_backend_interface(interface=HMACBackend)
147158
class TestHKDFExpand(object):

0 commit comments

Comments
 (0)