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 (
12    AlreadyFinalized, InvalidKey, _Reasons
13)
14from cryptography.hazmat.backends.interfaces import HashBackend
15from cryptography.hazmat.primitives import hashes
16from cryptography.hazmat.primitives.kdf.x963kdf import X963KDF
17
18from ...utils import raises_unsupported_algorithm
19
20
21@pytest.mark.requires_backend_interface(interface=HashBackend)
22class TestX963KDF(object):
23    def test_length_limit(self, backend):
24        big_length = hashes.SHA256().digest_size * (2 ** 32 - 1) + 1
25
26        with pytest.raises(ValueError):
27            X963KDF(hashes.SHA256(), big_length, None, backend)
28
29    def test_already_finalized(self, backend):
30        xkdf = X963KDF(hashes.SHA256(), 16, None, backend)
31
32        xkdf.derive(b"\x01" * 16)
33
34        with pytest.raises(AlreadyFinalized):
35            xkdf.derive(b"\x02" * 16)
36
37    def test_derive(self, backend):
38        key = binascii.unhexlify(
39            b"96c05619d56c328ab95fe84b18264b08725b85e33fd34f08"
40        )
41
42        derivedkey = binascii.unhexlify(b"443024c3dae66b95e6f5670601558f71")
43
44        xkdf = X963KDF(hashes.SHA256(), 16, None, backend)
45
46        assert xkdf.derive(key) == derivedkey
47
48    def test_buffer_protocol(self, backend):
49        key = bytearray(binascii.unhexlify(
50            b"96c05619d56c328ab95fe84b18264b08725b85e33fd34f08"
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(
90                hashes.SHA256(),
91                16,
92                sharedinfo=u"foo",
93                backend=backend
94            )
95
96        with pytest.raises(TypeError):
97            xkdf = X963KDF(
98                hashes.SHA256(),
99                16,
100                sharedinfo=None,
101                backend=backend
102            )
103
104            xkdf.derive(u"foo")
105
106        with pytest.raises(TypeError):
107            xkdf = X963KDF(
108                hashes.SHA256(),
109                16,
110                sharedinfo=None,
111                backend=backend
112            )
113
114            xkdf.verify(u"foo", b"bar")
115
116        with pytest.raises(TypeError):
117            xkdf = X963KDF(
118                hashes.SHA256(),
119                16,
120                sharedinfo=None,
121                backend=backend
122            )
123
124            xkdf.verify(b"foo", u"bar")
125
126
127def test_invalid_backend():
128    pretend_backend = object()
129
130    with raises_unsupported_algorithm(_Reasons.BACKEND_MISSING_INTERFACE):
131        X963KDF(hashes.SHA256(), 16, None, pretend_backend)
132