• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# This file is dual licensed under the terms of the Apache License, Version
2# 2.0, and the BSD License. See the LICENSE file in the root of this repository
3# for complete details.
4
5from __future__ import absolute_import, division, print_function
6
7import binascii
8
9import pytest
10
11from cryptography.hazmat.primitives import hashes
12from cryptography.hazmat.primitives.kdf.hkdf import HKDF
13
14
15_HASH_ALGORITHMS = {
16    "HKDF-SHA-1": hashes.SHA1(),
17    "HKDF-SHA-256": hashes.SHA256(),
18    "HKDF-SHA-384": hashes.SHA384(),
19    "HKDF-SHA-512": hashes.SHA512(),
20}
21
22
23@pytest.mark.wycheproof_tests(
24    "hkdf_sha1_test.json",
25    "hkdf_sha256_test.json",
26    "hkdf_sha384_test.json",
27    "hkdf_sha512_test.json",
28)
29def test_hkdf(backend, wycheproof):
30    hash_algo = _HASH_ALGORITHMS[wycheproof.testfiledata["algorithm"]]
31    if wycheproof.invalid:
32        with pytest.raises(ValueError):
33            HKDF(
34                algorithm=hash_algo,
35                length=wycheproof.testcase["size"],
36                salt=binascii.unhexlify(wycheproof.testcase["salt"]),
37                info=binascii.unhexlify(wycheproof.testcase["info"]),
38                backend=backend,
39            )
40        return
41
42    h = HKDF(
43        algorithm=hash_algo,
44        length=wycheproof.testcase["size"],
45        salt=binascii.unhexlify(wycheproof.testcase["salt"]),
46        info=binascii.unhexlify(wycheproof.testcase["info"]),
47        backend=backend,
48    )
49    result = h.derive(binascii.unhexlify(wycheproof.testcase["ikm"]))
50    assert result == binascii.unhexlify(wycheproof.testcase["okm"])
51