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.exceptions import AlreadyFinalized, InvalidKey, _Reasons
12from cryptography.hazmat.backends.interfaces import HashBackend
13from cryptography.hazmat.primitives import hashes
14from cryptography.hazmat.primitives.kdf.x963kdf import X963KDF
15
16from ...utils import raises_unsupported_algorithm
17
18
19@pytest.mark.requires_backend_interface(interface=HashBackend)
20class TestX963KDF(object):
21    def test_length_limit(self, backend):
22        big_length = hashes.SHA256().digest_size * (2 ** 32 - 1) + 1
23
24        with pytest.raises(ValueError):
25            X963KDF(hashes.SHA256(), big_length, None, backend)
26
27    def test_already_finalized(self, backend):
28        xkdf = X963KDF(hashes.SHA256(), 16, None, backend)
29
30        xkdf.derive(b"\x01" * 16)
31
32        with pytest.raises(AlreadyFinalized):
33            xkdf.derive(b"\x02" * 16)
34
35    def test_derive(self, backend):
36        key = binascii.unhexlify(
37            b"96c05619d56c328ab95fe84b18264b08725b85e33fd34f08"
38        )
39
40        derivedkey = binascii.unhexlify(b"443024c3dae66b95e6f5670601558f71")
41
42        xkdf = X963KDF(hashes.SHA256(), 16, None, backend)
43
44        assert xkdf.derive(key) == derivedkey
45
46    def test_buffer_protocol(self, backend):
47        key = bytearray(
48            binascii.unhexlify(
49                b"96c05619d56c328ab95fe84b18264b08725b85e33fd34f08"
50            )
51        )
52
53        derivedkey = binascii.unhexlify(b"443024c3dae66b95e6f5670601558f71")
54
55        xkdf = X963KDF(hashes.SHA256(), 16, None, backend)
56
57        assert xkdf.derive(key) == derivedkey
58
59    def test_verify(self, backend):
60        key = binascii.unhexlify(
61            b"22518b10e70f2a3f243810ae3254139efbee04aa57c7af7d"
62        )
63
64        sharedinfo = binascii.unhexlify(b"75eef81aa3041e33b80971203d2c0c52")
65
66        derivedkey = binascii.unhexlify(
67            b"c498af77161cc59f2962b9a713e2b215152d139766ce34a776df11866a69bf2e"
68            b"52a13d9c7c6fc878c50c5ea0bc7b00e0da2447cfd874f6cf92f30d0097111485"
69            b"500c90c3af8b487872d04685d14c8d1dc8d7fa08beb0ce0ababc11f0bd496269"
70            b"142d43525a78e5bc79a17f59676a5706dc54d54d4d1f0bd7e386128ec26afc21"
71        )
72
73        xkdf = X963KDF(hashes.SHA256(), 128, sharedinfo, backend)
74
75        assert xkdf.verify(key, derivedkey) is None
76
77    def test_invalid_verify(self, backend):
78        key = binascii.unhexlify(
79            b"96c05619d56c328ab95fe84b18264b08725b85e33fd34f08"
80        )
81
82        xkdf = X963KDF(hashes.SHA256(), 16, None, backend)
83
84        with pytest.raises(InvalidKey):
85            xkdf.verify(key, b"wrong derived key")
86
87    def test_unicode_typeerror(self, backend):
88        with pytest.raises(TypeError):
89            X963KDF(hashes.SHA256(), 16, sharedinfo=u"foo", backend=backend)
90
91        with pytest.raises(TypeError):
92            xkdf = X963KDF(
93                hashes.SHA256(), 16, sharedinfo=None, backend=backend
94            )
95
96            xkdf.derive(u"foo")
97
98        with pytest.raises(TypeError):
99            xkdf = X963KDF(
100                hashes.SHA256(), 16, sharedinfo=None, backend=backend
101            )
102
103            xkdf.verify(u"foo", b"bar")
104
105        with pytest.raises(TypeError):
106            xkdf = X963KDF(
107                hashes.SHA256(), 16, sharedinfo=None, backend=backend
108            )
109
110            xkdf.verify(b"foo", u"bar")
111
112
113def test_invalid_backend():
114    pretend_backend = object()
115
116    with raises_unsupported_algorithm(_Reasons.BACKEND_MISSING_INTERFACE):
117        X963KDF(hashes.SHA256(), 16, None, pretend_backend)
118