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 8import os 9 10import pytest 11 12from cryptography.hazmat.backends.interfaces import HashBackend 13from cryptography.hazmat.primitives import hashes 14 15from .utils import _load_all_params, generate_hash_test 16from ...utils import load_hash_vectors, load_nist_vectors 17 18 19@pytest.mark.supported( 20 only_if=lambda backend: backend.hash_supported(hashes.SHA1()), 21 skip_message="Does not support SHA1", 22) 23@pytest.mark.requires_backend_interface(interface=HashBackend) 24class TestSHA1(object): 25 test_SHA1 = generate_hash_test( 26 load_hash_vectors, 27 os.path.join("hashes", "SHA1"), 28 [ 29 "SHA1LongMsg.rsp", 30 "SHA1ShortMsg.rsp", 31 ], 32 hashes.SHA1(), 33 ) 34 35 36@pytest.mark.supported( 37 only_if=lambda backend: backend.hash_supported(hashes.SHA224()), 38 skip_message="Does not support SHA224", 39) 40@pytest.mark.requires_backend_interface(interface=HashBackend) 41class TestSHA224(object): 42 test_SHA224 = generate_hash_test( 43 load_hash_vectors, 44 os.path.join("hashes", "SHA2"), 45 [ 46 "SHA224LongMsg.rsp", 47 "SHA224ShortMsg.rsp", 48 ], 49 hashes.SHA224(), 50 ) 51 52 53@pytest.mark.supported( 54 only_if=lambda backend: backend.hash_supported(hashes.SHA256()), 55 skip_message="Does not support SHA256", 56) 57@pytest.mark.requires_backend_interface(interface=HashBackend) 58class TestSHA256(object): 59 test_SHA256 = generate_hash_test( 60 load_hash_vectors, 61 os.path.join("hashes", "SHA2"), 62 [ 63 "SHA256LongMsg.rsp", 64 "SHA256ShortMsg.rsp", 65 ], 66 hashes.SHA256(), 67 ) 68 69 70@pytest.mark.supported( 71 only_if=lambda backend: backend.hash_supported(hashes.SHA384()), 72 skip_message="Does not support SHA384", 73) 74@pytest.mark.requires_backend_interface(interface=HashBackend) 75class TestSHA384(object): 76 test_SHA384 = generate_hash_test( 77 load_hash_vectors, 78 os.path.join("hashes", "SHA2"), 79 [ 80 "SHA384LongMsg.rsp", 81 "SHA384ShortMsg.rsp", 82 ], 83 hashes.SHA384(), 84 ) 85 86 87@pytest.mark.supported( 88 only_if=lambda backend: backend.hash_supported(hashes.SHA512()), 89 skip_message="Does not support SHA512", 90) 91@pytest.mark.requires_backend_interface(interface=HashBackend) 92class TestSHA512(object): 93 test_SHA512 = generate_hash_test( 94 load_hash_vectors, 95 os.path.join("hashes", "SHA2"), 96 [ 97 "SHA512LongMsg.rsp", 98 "SHA512ShortMsg.rsp", 99 ], 100 hashes.SHA512(), 101 ) 102 103 104@pytest.mark.supported( 105 only_if=lambda backend: backend.hash_supported(hashes.SHA512_224()), 106 skip_message="Does not support SHA512/224", 107) 108@pytest.mark.requires_backend_interface(interface=HashBackend) 109class TestSHA512224(object): 110 test_SHA512_224 = generate_hash_test( 111 load_hash_vectors, 112 os.path.join("hashes", "SHA2"), 113 [ 114 "SHA512_224LongMsg.rsp", 115 "SHA512_224ShortMsg.rsp", 116 ], 117 hashes.SHA512_224(), 118 ) 119 120 121@pytest.mark.supported( 122 only_if=lambda backend: backend.hash_supported(hashes.SHA512_256()), 123 skip_message="Does not support SHA512/256", 124) 125@pytest.mark.requires_backend_interface(interface=HashBackend) 126class TestSHA512256(object): 127 test_SHA512_256 = generate_hash_test( 128 load_hash_vectors, 129 os.path.join("hashes", "SHA2"), 130 [ 131 "SHA512_256LongMsg.rsp", 132 "SHA512_256ShortMsg.rsp", 133 ], 134 hashes.SHA512_256(), 135 ) 136 137 138@pytest.mark.supported( 139 only_if=lambda backend: backend.hash_supported(hashes.MD5()), 140 skip_message="Does not support MD5", 141) 142@pytest.mark.requires_backend_interface(interface=HashBackend) 143class TestMD5(object): 144 test_md5 = generate_hash_test( 145 load_hash_vectors, 146 os.path.join("hashes", "MD5"), 147 [ 148 "rfc-1321.txt", 149 ], 150 hashes.MD5(), 151 ) 152 153 154@pytest.mark.supported( 155 only_if=lambda backend: backend.hash_supported( 156 hashes.BLAKE2b(digest_size=64)), 157 skip_message="Does not support BLAKE2b", 158) 159@pytest.mark.requires_backend_interface(interface=HashBackend) 160class TestBLAKE2b(object): 161 test_b2b = generate_hash_test( 162 load_hash_vectors, 163 os.path.join("hashes", "blake2"), 164 [ 165 "blake2b.txt", 166 ], 167 hashes.BLAKE2b(digest_size=64), 168 ) 169 170 171@pytest.mark.supported( 172 only_if=lambda backend: backend.hash_supported( 173 hashes.BLAKE2s(digest_size=32)), 174 skip_message="Does not support BLAKE2s", 175) 176@pytest.mark.requires_backend_interface(interface=HashBackend) 177class TestBLAKE2s256(object): 178 test_b2s = generate_hash_test( 179 load_hash_vectors, 180 os.path.join("hashes", "blake2"), 181 [ 182 "blake2s.txt", 183 ], 184 hashes.BLAKE2s(digest_size=32), 185 ) 186 187 188@pytest.mark.supported( 189 only_if=lambda backend: backend.hash_supported(hashes.SHA3_224()), 190 skip_message="Does not support SHA3_224", 191) 192@pytest.mark.requires_backend_interface(interface=HashBackend) 193class TestSHA3224(object): 194 test_SHA3_224 = generate_hash_test( 195 load_hash_vectors, 196 os.path.join("hashes", "SHA3"), 197 [ 198 "SHA3_224LongMsg.rsp", 199 "SHA3_224ShortMsg.rsp", 200 ], 201 hashes.SHA3_224(), 202 ) 203 204 205@pytest.mark.supported( 206 only_if=lambda backend: backend.hash_supported(hashes.SHA3_256()), 207 skip_message="Does not support SHA3_256", 208) 209@pytest.mark.requires_backend_interface(interface=HashBackend) 210class TestSHA3256(object): 211 test_SHA3_256 = generate_hash_test( 212 load_hash_vectors, 213 os.path.join("hashes", "SHA3"), 214 [ 215 "SHA3_256LongMsg.rsp", 216 "SHA3_256ShortMsg.rsp", 217 ], 218 hashes.SHA3_256(), 219 ) 220 221 222@pytest.mark.supported( 223 only_if=lambda backend: backend.hash_supported(hashes.SHA3_384()), 224 skip_message="Does not support SHA3_384", 225) 226@pytest.mark.requires_backend_interface(interface=HashBackend) 227class TestSHA3384(object): 228 test_SHA3_384 = generate_hash_test( 229 load_hash_vectors, 230 os.path.join("hashes", "SHA3"), 231 [ 232 "SHA3_384LongMsg.rsp", 233 "SHA3_384ShortMsg.rsp", 234 ], 235 hashes.SHA3_384(), 236 ) 237 238 239@pytest.mark.supported( 240 only_if=lambda backend: backend.hash_supported(hashes.SHA3_512()), 241 skip_message="Does not support SHA3_512", 242) 243@pytest.mark.requires_backend_interface(interface=HashBackend) 244class TestSHA3512(object): 245 test_SHA3_512 = generate_hash_test( 246 load_hash_vectors, 247 os.path.join("hashes", "SHA3"), 248 [ 249 "SHA3_512LongMsg.rsp", 250 "SHA3_512ShortMsg.rsp", 251 ], 252 hashes.SHA3_512(), 253 ) 254 255 256@pytest.mark.supported( 257 only_if=lambda backend: backend.hash_supported( 258 hashes.SHAKE128(digest_size=16)), 259 skip_message="Does not support SHAKE128", 260) 261@pytest.mark.requires_backend_interface(interface=HashBackend) 262class TestSHAKE128(object): 263 test_shake128 = generate_hash_test( 264 load_hash_vectors, 265 os.path.join("hashes", "SHAKE"), 266 [ 267 "SHAKE128LongMsg.rsp", 268 "SHAKE128ShortMsg.rsp", 269 ], 270 hashes.SHAKE128(digest_size=16), 271 ) 272 273 @pytest.mark.parametrize( 274 "vector", 275 _load_all_params( 276 os.path.join("hashes", "SHAKE"), 277 [ 278 "SHAKE128VariableOut.rsp", 279 ], 280 load_nist_vectors, 281 ) 282 ) 283 def test_shake128_variable(self, vector, backend): 284 output_length = int(vector['outputlen']) // 8 285 msg = binascii.unhexlify(vector['msg']) 286 shake = hashes.SHAKE128(digest_size=output_length) 287 m = hashes.Hash(shake, backend=backend) 288 m.update(msg) 289 assert m.finalize() == binascii.unhexlify(vector['output']) 290 291 292@pytest.mark.supported( 293 only_if=lambda backend: backend.hash_supported( 294 hashes.SHAKE256(digest_size=32)), 295 skip_message="Does not support SHAKE256", 296) 297@pytest.mark.requires_backend_interface(interface=HashBackend) 298class TestSHAKE256(object): 299 test_shake256 = generate_hash_test( 300 load_hash_vectors, 301 os.path.join("hashes", "SHAKE"), 302 [ 303 "SHAKE256LongMsg.rsp", 304 "SHAKE256ShortMsg.rsp", 305 ], 306 hashes.SHAKE256(digest_size=32), 307 ) 308 309 @pytest.mark.parametrize( 310 "vector", 311 _load_all_params( 312 os.path.join("hashes", "SHAKE"), 313 [ 314 "SHAKE256VariableOut.rsp", 315 ], 316 load_nist_vectors, 317 ) 318 ) 319 def test_shake256_variable(self, vector, backend): 320 output_length = int(vector['outputlen']) // 8 321 msg = binascii.unhexlify(vector['msg']) 322 shake = hashes.SHAKE256(digest_size=output_length) 323 m = hashes.Hash(shake, backend=backend) 324 m.update(msg) 325 assert m.finalize() == binascii.unhexlify(vector['output']) 326