Skip to content

Commit

Permalink
Backport hkdf short output (#3216)
Browse files Browse the repository at this point in the history
* Fixes #3211 -- fixed hkdf's output with short length (#3215)

* added a changelog
  • Loading branch information
alex authored and reaperhulk committed Nov 6, 2016
1 parent cad7774 commit aebfba2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
@@ -1,6 +1,13 @@
Changelog
=========

1.5.3 - 2016-11-05
~~~~~~~~~~~~~~~~~~

* **SECURITY ISSUE**: Fixed a bug where ``HKDF`` would return an empty
byte-string if used with a ``length`` less than ``algorithm.digest_size``.
Credit to **Markus Döring** for reporting the issue.

1.5.2 - 2016-09-26
~~~~~~~~~~~~~~~~~~

Expand Down
2 changes: 1 addition & 1 deletion src/cryptography/hazmat/primitives/kdf/hkdf.py
Expand Up @@ -91,7 +91,7 @@ def _expand(self, key_material):
output = [b""]
counter = 1

while (self._algorithm.digest_size // 8) * len(output) < self._length:
while self._algorithm.digest_size * (len(output) - 1) < self._length:
h = hmac.HMAC(key_material, self._algorithm, backend=self._backend)
h.update(output[-1])
h.update(self._info)
Expand Down
11 changes: 11 additions & 0 deletions tests/hazmat/primitives/test_hkdf.py
Expand Up @@ -142,6 +142,17 @@ def test_unicode_typeerror(self, backend):

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

def test_derive_short_output(self, backend):
hkdf = HKDF(
hashes.SHA256(),
4,
salt=None,
info=None,
backend=backend
)

assert hkdf.derive(b"\x01" * 16) == b"gJ\xfb{"


@pytest.mark.requires_backend_interface(interface=HMACBackend)
class TestHKDFExpand(object):
Expand Down

0 comments on commit aebfba2

Please sign in to comment.