• 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.backends.interfaces import HMACBackend
12from cryptography.hazmat.primitives import hashes, hmac
13
14from .utils import generate_hmac_test
15from ...utils import load_hash_vectors
16
17
18@pytest.mark.supported(
19    only_if=lambda backend: backend.hmac_supported(hashes.MD5()),
20    skip_message="Does not support MD5",
21)
22@pytest.mark.requires_backend_interface(interface=HMACBackend)
23class TestHMACMD5(object):
24    test_hmac_md5 = generate_hmac_test(
25        load_hash_vectors,
26        "HMAC",
27        [
28            "rfc-2202-md5.txt",
29        ],
30        hashes.MD5(),
31    )
32
33
34@pytest.mark.supported(
35    only_if=lambda backend: backend.hmac_supported(hashes.SHA1()),
36    skip_message="Does not support SHA1",
37)
38@pytest.mark.requires_backend_interface(interface=HMACBackend)
39class TestHMACSHA1(object):
40    test_hmac_sha1 = generate_hmac_test(
41        load_hash_vectors,
42        "HMAC",
43        [
44            "rfc-2202-sha1.txt",
45        ],
46        hashes.SHA1(),
47    )
48
49
50@pytest.mark.supported(
51    only_if=lambda backend: backend.hmac_supported(hashes.SHA224()),
52    skip_message="Does not support SHA224",
53)
54@pytest.mark.requires_backend_interface(interface=HMACBackend)
55class TestHMACSHA224(object):
56    test_hmac_sha224 = generate_hmac_test(
57        load_hash_vectors,
58        "HMAC",
59        [
60            "rfc-4231-sha224.txt",
61        ],
62        hashes.SHA224(),
63    )
64
65
66@pytest.mark.supported(
67    only_if=lambda backend: backend.hmac_supported(hashes.SHA256()),
68    skip_message="Does not support SHA256",
69)
70@pytest.mark.requires_backend_interface(interface=HMACBackend)
71class TestHMACSHA256(object):
72    test_hmac_sha256 = generate_hmac_test(
73        load_hash_vectors,
74        "HMAC",
75        [
76            "rfc-4231-sha256.txt",
77        ],
78        hashes.SHA256(),
79    )
80
81
82@pytest.mark.supported(
83    only_if=lambda backend: backend.hmac_supported(hashes.SHA384()),
84    skip_message="Does not support SHA384",
85)
86@pytest.mark.requires_backend_interface(interface=HMACBackend)
87class TestHMACSHA384(object):
88    test_hmac_sha384 = generate_hmac_test(
89        load_hash_vectors,
90        "HMAC",
91        [
92            "rfc-4231-sha384.txt",
93        ],
94        hashes.SHA384(),
95    )
96
97
98@pytest.mark.supported(
99    only_if=lambda backend: backend.hmac_supported(hashes.SHA512()),
100    skip_message="Does not support SHA512",
101)
102@pytest.mark.requires_backend_interface(interface=HMACBackend)
103class TestHMACSHA512(object):
104    test_hmac_sha512 = generate_hmac_test(
105        load_hash_vectors,
106        "HMAC",
107        [
108            "rfc-4231-sha512.txt",
109        ],
110        hashes.SHA512(),
111    )
112
113
114@pytest.mark.supported(
115    only_if=lambda backend: backend.hmac_supported(hashes.BLAKE2b(
116        digest_size=64
117    )),
118    skip_message="Does not support BLAKE2",
119)
120@pytest.mark.requires_backend_interface(interface=HMACBackend)
121class TestHMACBLAKE2(object):
122    def test_blake2b(self, backend):
123        h = hmac.HMAC(b"0" * 64, hashes.BLAKE2b(digest_size=64), backend)
124        h.update(b"test")
125        digest = h.finalize()
126        assert digest == binascii.unhexlify(
127            b"b5319122f8a24ba134a0c9851922448104e25be5d1b91265c0c68b22722f0f29"
128            b"87dba4aeaa69e6bed7edc44f48d6b1be493a3ce583f9c737c53d6bacc09e2f32"
129        )
130
131    def test_blake2s(self, backend):
132        h = hmac.HMAC(b"0" * 32, hashes.BLAKE2s(digest_size=32), backend)
133        h.update(b"test")
134        digest = h.finalize()
135        assert digest == binascii.unhexlify(
136            b"51477cc5bdf1faf952cf97bb934ee936de1f4d5d7448a84eeb6f98d23b392166"
137        )
138