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 HMACBackend 13from cryptography.hazmat.backends.interfaces import HashBackend 14from cryptography.hazmat.primitives import hashes 15from cryptography.hazmat.primitives.kdf.concatkdf import ConcatKDFHMAC 16from cryptography.hazmat.primitives.kdf.concatkdf import ConcatKDFHash 17 18from ...utils import raises_unsupported_algorithm 19 20 21@pytest.mark.requires_backend_interface(interface=HashBackend) 22class TestConcatKDFHash(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 ConcatKDFHash(hashes.SHA256(), big_length, None, backend) 28 29 def test_already_finalized(self, backend): 30 ckdf = ConcatKDFHash(hashes.SHA256(), 16, None, backend) 31 32 ckdf.derive(b"\x01" * 16) 33 34 with pytest.raises(AlreadyFinalized): 35 ckdf.derive(b"\x02" * 16) 36 37 def test_derive(self, backend): 38 prk = binascii.unhexlify( 39 b"52169af5c485dcc2321eb8d26d5efa21fb9b93c98e38412ee2484cf14f0d0d23" 40 ) 41 42 okm = binascii.unhexlify(b"1c3bc9e7c4547c5191c0d478cccaed55") 43 44 oinfo = binascii.unhexlify( 45 b"a1b2c3d4e53728157e634612c12d6d5223e204aeea4341565369647bd184bcd2" 46 b"46f72971f292badaa2fe4124612cba" 47 ) 48 49 ckdf = ConcatKDFHash(hashes.SHA256(), 16, oinfo, backend) 50 51 assert ckdf.derive(prk) == okm 52 53 def test_buffer_protocol(self, backend): 54 prk = binascii.unhexlify( 55 b"52169af5c485dcc2321eb8d26d5efa21fb9b93c98e38412ee2484cf14f0d0d23" 56 ) 57 58 okm = binascii.unhexlify(b"1c3bc9e7c4547c5191c0d478cccaed55") 59 60 oinfo = binascii.unhexlify( 61 b"a1b2c3d4e53728157e634612c12d6d5223e204aeea4341565369647bd184bcd2" 62 b"46f72971f292badaa2fe4124612cba" 63 ) 64 65 ckdf = ConcatKDFHash(hashes.SHA256(), 16, oinfo, backend) 66 67 assert ckdf.derive(bytearray(prk)) == okm 68 69 def test_verify(self, backend): 70 prk = binascii.unhexlify( 71 b"52169af5c485dcc2321eb8d26d5efa21fb9b93c98e38412ee2484cf14f0d0d23" 72 ) 73 74 okm = binascii.unhexlify(b"1c3bc9e7c4547c5191c0d478cccaed55") 75 76 oinfo = binascii.unhexlify( 77 b"a1b2c3d4e53728157e634612c12d6d5223e204aeea4341565369647bd184bcd2" 78 b"46f72971f292badaa2fe4124612cba" 79 ) 80 81 ckdf = ConcatKDFHash(hashes.SHA256(), 16, oinfo, backend) 82 83 assert ckdf.verify(prk, okm) is None 84 85 def test_invalid_verify(self, backend): 86 prk = binascii.unhexlify( 87 b"52169af5c485dcc2321eb8d26d5efa21fb9b93c98e38412ee2484cf14f0d0d23" 88 ) 89 90 oinfo = binascii.unhexlify( 91 b"a1b2c3d4e53728157e634612c12d6d5223e204aeea4341565369647bd184bcd2" 92 b"46f72971f292badaa2fe4124612cba" 93 ) 94 95 ckdf = ConcatKDFHash(hashes.SHA256(), 16, oinfo, backend) 96 97 with pytest.raises(InvalidKey): 98 ckdf.verify(prk, b"wrong key") 99 100 def test_unicode_typeerror(self, backend): 101 with pytest.raises(TypeError): 102 ConcatKDFHash( 103 hashes.SHA256(), 16, otherinfo=u"foo", backend=backend 104 ) 105 106 with pytest.raises(TypeError): 107 ckdf = ConcatKDFHash( 108 hashes.SHA256(), 16, otherinfo=None, backend=backend 109 ) 110 111 ckdf.derive(u"foo") 112 113 with pytest.raises(TypeError): 114 ckdf = ConcatKDFHash( 115 hashes.SHA256(), 16, otherinfo=None, backend=backend 116 ) 117 118 ckdf.verify(u"foo", b"bar") 119 120 with pytest.raises(TypeError): 121 ckdf = ConcatKDFHash( 122 hashes.SHA256(), 16, otherinfo=None, backend=backend 123 ) 124 125 ckdf.verify(b"foo", u"bar") 126 127 128@pytest.mark.requires_backend_interface(interface=HMACBackend) 129class TestConcatKDFHMAC(object): 130 def test_length_limit(self, backend): 131 big_length = hashes.SHA256().digest_size * (2 ** 32 - 1) + 1 132 133 with pytest.raises(ValueError): 134 ConcatKDFHMAC(hashes.SHA256(), big_length, None, None, backend) 135 136 def test_already_finalized(self, backend): 137 ckdf = ConcatKDFHMAC(hashes.SHA256(), 16, None, None, backend) 138 139 ckdf.derive(b"\x01" * 16) 140 141 with pytest.raises(AlreadyFinalized): 142 ckdf.derive(b"\x02" * 16) 143 144 def test_derive(self, backend): 145 prk = binascii.unhexlify( 146 b"013951627c1dea63ea2d7702dd24e963eef5faac6b4af7e4" 147 b"b831cde499dff1ce45f6179f741c728aa733583b02409208" 148 b"8f0af7fce1d045edbc5790931e8d5ca79c73" 149 ) 150 151 okm = binascii.unhexlify( 152 b"64ce901db10d558661f10b6836a122a7" 153 b"605323ce2f39bf27eaaac8b34cf89f2f" 154 ) 155 156 oinfo = binascii.unhexlify( 157 b"a1b2c3d4e55e600be5f367e0e8a465f4bf2704db00c9325c" 158 b"9fbd216d12b49160b2ae5157650f43415653696421e68e" 159 ) 160 161 ckdf = ConcatKDFHMAC(hashes.SHA512(), 32, None, oinfo, backend) 162 163 assert ckdf.derive(prk) == okm 164 165 def test_buffer_protocol(self, backend): 166 prk = binascii.unhexlify( 167 b"013951627c1dea63ea2d7702dd24e963eef5faac6b4af7e4" 168 b"b831cde499dff1ce45f6179f741c728aa733583b02409208" 169 b"8f0af7fce1d045edbc5790931e8d5ca79c73" 170 ) 171 172 okm = binascii.unhexlify( 173 b"64ce901db10d558661f10b6836a122a7" 174 b"605323ce2f39bf27eaaac8b34cf89f2f" 175 ) 176 177 oinfo = binascii.unhexlify( 178 b"a1b2c3d4e55e600be5f367e0e8a465f4bf2704db00c9325c" 179 b"9fbd216d12b49160b2ae5157650f43415653696421e68e" 180 ) 181 182 ckdf = ConcatKDFHMAC(hashes.SHA512(), 32, None, oinfo, backend) 183 184 assert ckdf.derive(bytearray(prk)) == okm 185 186 def test_derive_explicit_salt(self, backend): 187 prk = binascii.unhexlify( 188 b"013951627c1dea63ea2d7702dd24e963eef5faac6b4af7e4" 189 b"b831cde499dff1ce45f6179f741c728aa733583b02409208" 190 b"8f0af7fce1d045edbc5790931e8d5ca79c73" 191 ) 192 193 okm = binascii.unhexlify( 194 b"64ce901db10d558661f10b6836a122a7" 195 b"605323ce2f39bf27eaaac8b34cf89f2f" 196 ) 197 198 oinfo = binascii.unhexlify( 199 b"a1b2c3d4e55e600be5f367e0e8a465f4bf2704db00c9325c" 200 b"9fbd216d12b49160b2ae5157650f43415653696421e68e" 201 ) 202 203 ckdf = ConcatKDFHMAC( 204 hashes.SHA512(), 32, b"\x00" * 128, oinfo, backend 205 ) 206 207 assert ckdf.derive(prk) == okm 208 209 def test_verify(self, backend): 210 prk = binascii.unhexlify( 211 b"013951627c1dea63ea2d7702dd24e963eef5faac6b4af7e4" 212 b"b831cde499dff1ce45f6179f741c728aa733583b02409208" 213 b"8f0af7fce1d045edbc5790931e8d5ca79c73" 214 ) 215 216 okm = binascii.unhexlify( 217 b"64ce901db10d558661f10b6836a122a7" 218 b"605323ce2f39bf27eaaac8b34cf89f2f" 219 ) 220 221 oinfo = binascii.unhexlify( 222 b"a1b2c3d4e55e600be5f367e0e8a465f4bf2704db00c9325c" 223 b"9fbd216d12b49160b2ae5157650f43415653696421e68e" 224 ) 225 226 ckdf = ConcatKDFHMAC(hashes.SHA512(), 32, None, oinfo, backend) 227 228 assert ckdf.verify(prk, okm) is None 229 230 def test_invalid_verify(self, backend): 231 prk = binascii.unhexlify( 232 b"013951627c1dea63ea2d7702dd24e963eef5faac6b4af7e4" 233 b"b831cde499dff1ce45f6179f741c728aa733583b02409208" 234 b"8f0af7fce1d045edbc5790931e8d5ca79c73" 235 ) 236 237 oinfo = binascii.unhexlify( 238 b"a1b2c3d4e55e600be5f367e0e8a465f4bf2704db00c9325c" 239 b"9fbd216d12b49160b2ae5157650f43415653696421e68e" 240 ) 241 242 ckdf = ConcatKDFHMAC(hashes.SHA512(), 32, None, oinfo, backend) 243 244 with pytest.raises(InvalidKey): 245 ckdf.verify(prk, b"wrong key") 246 247 def test_unicode_typeerror(self, backend): 248 with pytest.raises(TypeError): 249 ConcatKDFHMAC( 250 hashes.SHA256(), 251 16, 252 salt=u"foo", 253 otherinfo=None, 254 backend=backend, 255 ) 256 257 with pytest.raises(TypeError): 258 ConcatKDFHMAC( 259 hashes.SHA256(), 260 16, 261 salt=None, 262 otherinfo=u"foo", 263 backend=backend, 264 ) 265 266 with pytest.raises(TypeError): 267 ckdf = ConcatKDFHMAC( 268 hashes.SHA256(), 16, salt=None, otherinfo=None, backend=backend 269 ) 270 271 ckdf.derive(u"foo") 272 273 with pytest.raises(TypeError): 274 ckdf = ConcatKDFHMAC( 275 hashes.SHA256(), 16, salt=None, otherinfo=None, backend=backend 276 ) 277 278 ckdf.verify(u"foo", b"bar") 279 280 with pytest.raises(TypeError): 281 ckdf = ConcatKDFHMAC( 282 hashes.SHA256(), 16, salt=None, otherinfo=None, backend=backend 283 ) 284 285 ckdf.verify(b"foo", u"bar") 286 287 288def test_invalid_backend(): 289 pretend_backend = object() 290 291 with raises_unsupported_algorithm(_Reasons.BACKEND_MISSING_INTERFACE): 292 ConcatKDFHash(hashes.SHA256(), 16, None, pretend_backend) 293 with raises_unsupported_algorithm(_Reasons.BACKEND_MISSING_INTERFACE): 294 ConcatKDFHMAC(hashes.SHA256(), 16, None, None, pretend_backend) 295