Debian Codegraph @

Lib/test/test_hashlib.py

raw
1 # Test the hashlib module.
2 #
3 # Copyright (C) 2005-2010 Gregory P. Smith ([email protected])
4 # Licensed to PSF under a Contributor Agreement.
5 #
6
7 import array
8 from binascii import unhexlify
9 import hashlib
10 import importlib
11 import io
12 import itertools
13 import logging
14 import os
15 import re
16 import sys
17 import sysconfig
18 import tempfile
19 import threading
20 import unittest
21 import warnings
22 from test import support
23 from test.support import _4G, bigmemtest
24 from test.support import hashlib_helper
25 from test.support.import_helper import import_fresh_module
26 from test.support import requires_resource
27 from test.support import threading_helper
28 from http.client import HTTPException
29
30
31 default_builtin_hashes = {'md5', 'sha1', 'sha2', 'sha3', 'blake2'}
32 # --with-builtin-hashlib-hashes override
33 builtin_hashes = sysconfig.get_config_var("PY_BUILTIN_HASHLIB_HASHES")
34 if builtin_hashes is None:
35 builtin_hashes = default_builtin_hashes
36 else:
37 builtin_hash_names = builtin_hashes.strip('"').lower().split(",")
38 builtin_hashes = set(map(str.strip, builtin_hash_names))
39
40 # Public 'hashlib' module with OpenSSL backend for PBKDF2.
41 openssl_hashlib = import_fresh_module('hashlib', fresh=['_hashlib'])
42
43 try:
44 import _hashlib
45 except ImportError:
46 _hashlib = None
47 # The extension module may exist but only define some of these. gh-141907
48 HASH = getattr(_hashlib, 'HASH', None)
49 HASHXOF = getattr(_hashlib, 'HASHXOF', None)
50 openssl_md_meth_names = getattr(_hashlib, 'openssl_md_meth_names', frozenset())
51 get_fips_mode = getattr(_hashlib, 'get_fips_mode', None)
52 if not get_fips_mode:
53 def get_fips_mode():
54 return 0
55
56 try:
57 import _blake2
58 except ImportError:
59 _blake2 = None
60
61 requires_blake2 = unittest.skipUnless(_blake2, 'requires _blake2')
62
63 try:
64 import _sha3
65 except ImportError:
66 _sha3 = None
67
68 requires_sha3 = unittest.skipUnless(_sha3, 'requires _sha3')
69
70
71 def hexstr(s):
72 assert isinstance(s, bytes), repr(s)
73 h = "0123456789abcdef"
74 r = ''
75 for i in s:
76 r += h[(i >> 4) & 0xF] + h[i & 0xF]
77 return r
78
79
80 URL = "http://www.pythontest.net/hashlib/{}.txt"
81
82 def read_vectors(hash_name):
83 url = URL.format(hash_name)
84 try:
85 testdata = support.open_urlresource(url, encoding="utf-8")
86 except (OSError, HTTPException):
87 raise unittest.SkipTest("Could not retrieve {}".format(url))
88 with testdata:
89 for line in testdata:
90 line = line.strip()
91 if line.startswith('#') or not line:
92 continue
93 parts = line.split(',')
94 parts[0] = bytes.fromhex(parts[0])
95 yield parts
96
97
98 class HashLibTestCase(unittest.TestCase):
99 supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1',
100 'sha224', 'SHA224', 'sha256', 'SHA256',
101 'sha384', 'SHA384', 'sha512', 'SHA512',
102 'blake2b', 'blake2s',
103 'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512',
104 'shake_128', 'shake_256')
105
106 shakes = {'shake_128', 'shake_256'}
107
108 # gh-58898: Fallback modules are always compiled under POSIX.
109 _warn_on_extension_import = (os.name == 'posix' or support.Py_DEBUG)
110
111 def _conditional_import_module(self, module_name):
112 """Import a module and return a reference to it or None on failure."""
113 try:
114 return importlib.import_module(module_name)
115 except ModuleNotFoundError as error:
116 if self._warn_on_extension_import and module_name in builtin_hashes:
117 logging.getLogger(__name__).warning(
118 'Did a C extension fail to compile? %s',
119 error,
120 exc_info=error,
121 )
122 return None
123
124 def __init__(self, *args, **kwargs):
125 algorithms = set()
126 for algorithm in self.supported_hash_names:
127 algorithms.add(algorithm.lower())
128
129 _blake2 = self._conditional_import_module('_blake2')
130 blake2_hashes = {'blake2b', 'blake2s'}
131 if _blake2:
132 algorithms.update(blake2_hashes)
133 else:
134 algorithms.difference_update(blake2_hashes)
135
136 self.constructors_to_test = {}
137 for algorithm in algorithms:
138 self.constructors_to_test[algorithm] = set()
139
140 # For each algorithm, test the direct constructor and the use
141 # of hashlib.new given the algorithm name.
142 for algorithm, constructors in self.constructors_to_test.items():
143 constructors.add(getattr(hashlib, algorithm))
144 def c(*args, __algorithm_name=algorithm, **kwargs):
145 return hashlib.new(__algorithm_name, *args, **kwargs)
146 c.__name__ = f'do_test_algorithm_via_hashlib_new_{algorithm}'
147 constructors.add(c)
148
149 _hashlib = self._conditional_import_module('_hashlib')
150 self._hashlib = _hashlib
151 if _hashlib:
152 # These algorithms should always be present when this module
153 # is compiled. If not, something was compiled wrong.
154 self.assertHasAttr(_hashlib, 'openssl_md5')
155 self.assertHasAttr(_hashlib, 'openssl_sha1')
156 for algorithm, constructors in self.constructors_to_test.items():
157 constructor = getattr(_hashlib, 'openssl_'+algorithm, None)
158 if constructor:
159 try:
160 constructor()
161 except ValueError:
162 # default constructor blocked by crypto policy
163 pass
164 else:
165 constructors.add(constructor)
166
167 def add_builtin_constructor(name):
168 constructor = getattr(hashlib, "__get_builtin_constructor")(name)
169 self.constructors_to_test[name].add(constructor)
170
171 _md5 = self._conditional_import_module('_md5')
172 if _md5:
173 add_builtin_constructor('md5')
174 _sha1 = self._conditional_import_module('_sha1')
175 if _sha1:
176 add_builtin_constructor('sha1')
177 _sha2 = self._conditional_import_module('_sha2')
178 if _sha2:
179 add_builtin_constructor('sha224')
180 add_builtin_constructor('sha256')
181 add_builtin_constructor('sha384')
182 add_builtin_constructor('sha512')
183 _sha3 = self._conditional_import_module('_sha3')
184 if _sha3:
185 add_builtin_constructor('sha3_224')
186 add_builtin_constructor('sha3_256')
187 add_builtin_constructor('sha3_384')
188 add_builtin_constructor('sha3_512')
189 add_builtin_constructor('shake_128')
190 add_builtin_constructor('shake_256')
191 if _blake2:
192 add_builtin_constructor('blake2s')
193 add_builtin_constructor('blake2b')
194
195 super(HashLibTestCase, self).__init__(*args, **kwargs)
196
197 @property
198 def hash_constructors(self):
199 constructors = self.constructors_to_test.values()
200 return itertools.chain.from_iterable(constructors)
201
202 @property
203 def is_fips_mode(self):
204 return get_fips_mode()
205
206 def test_hash_array(self):
207 a = array.array("b", range(10))
208 for cons in self.hash_constructors:
209 c = cons(a, usedforsecurity=False)
210 if c.name in self.shakes:
211 c.hexdigest(16)
212 else:
213 c.hexdigest()
214
215 def test_algorithms_guaranteed(self):
216 self.assertEqual(hashlib.algorithms_guaranteed,
217 set(_algo for _algo in self.supported_hash_names
218 if _algo.islower()))
219
220 def test_algorithms_available(self):
221 self.assertTrue(set(hashlib.algorithms_guaranteed).
222 issubset(hashlib.algorithms_available))
223 # all available algorithms must be loadable, bpo-47101
224 self.assertNotIn("undefined", hashlib.algorithms_available)
225 for name in hashlib.algorithms_available:
226 with self.subTest(name):
227 try:
228 _ = hashlib.new(name, usedforsecurity=False)
229 except ValueError as exc:
230 self.skip_if_blake2_not_builtin(name, exc)
231 raise
232
233 def test_usedforsecurity_true(self):
234 hashlib.new("sha256", usedforsecurity=True)
235 if self.is_fips_mode:
236 self.skipTest("skip in FIPS mode")
237 for cons in self.hash_constructors:
238 cons(usedforsecurity=True)
239 cons(b'', usedforsecurity=True)
240 hashlib.new("md5", usedforsecurity=True)
241 hashlib.md5(usedforsecurity=True)
242 if self._hashlib is not None:
243 self._hashlib.new("md5", usedforsecurity=True)
244 self._hashlib.openssl_md5(usedforsecurity=True)
245
246 def test_usedforsecurity_false(self):
247 hashlib.new("sha256", usedforsecurity=False)
248 for cons in self.hash_constructors:
249 cons(usedforsecurity=False)
250 cons(b'', usedforsecurity=False)
251 hashlib.new("md5", usedforsecurity=False)
252 hashlib.md5(usedforsecurity=False)
253 if self._hashlib is not None:
254 self._hashlib.new("md5", usedforsecurity=False)
255 self._hashlib.openssl_md5(usedforsecurity=False)
256
257 @unittest.skipIf(get_fips_mode(), "skip in FIPS mode")
258 def test_clinic_signature(self):
259 for constructor in self.hash_constructors:
260 with self.subTest(constructor.__name__):
261 constructor(b'')
262 constructor(data=b'')
263 constructor(string=b'') # should be deprecated in the future
264
265 digest_name = constructor(b'').name
266 with self.subTest(digest_name):
267 hashlib.new(digest_name, b'')
268 hashlib.new(digest_name, data=b'')
269 hashlib.new(digest_name, string=b'')
270 # Make sure that _hashlib contains the constructor
271 # to test when using a combination of libcrypto and
272 # interned hash implementations.
273 if self._hashlib and digest_name in self._hashlib._constructors:
274 self._hashlib.new(digest_name, b'')
275 self._hashlib.new(digest_name, data=b'')
276 self._hashlib.new(digest_name, string=b'')
277
278 @unittest.skipIf(get_fips_mode(), "skip in FIPS mode")
279 def test_clinic_signature_errors(self):
280 nomsg = b''
281 mymsg = b'msg'
282 conflicting_call = re.escape(
283 "'data' and 'string' are mutually exclusive "
284 "and support for 'string' keyword parameter "
285 "is slated for removal in a future version."
286 )
287 duplicated_param = re.escape("given by name ('data') and position")
288 unexpected_param = re.escape("got an unexpected keyword argument '_'")
289 for args, kwds, errmsg in [
290 # Reject duplicated arguments before unknown keyword arguments.
291 ((nomsg,), dict(data=nomsg, _=nomsg), duplicated_param),
292 ((mymsg,), dict(data=nomsg, _=nomsg), duplicated_param),
293 # Reject duplicated arguments before conflicting ones.
294 *itertools.product(
295 [[nomsg], [mymsg]],
296 [dict(data=nomsg), dict(data=nomsg, string=nomsg)],
297 [duplicated_param]
298 ),
299 # Reject unknown keyword arguments before conflicting ones.
300 *itertools.product(
301 [()],
302 [
303 dict(_=None),
304 dict(data=nomsg, _=None),
305 dict(string=nomsg, _=None),
306 dict(string=nomsg, data=nomsg, _=None),
307 ],
308 [unexpected_param]
309 ),
310 ((nomsg,), dict(_=None), unexpected_param),
311 ((mymsg,), dict(_=None), unexpected_param),
312 # Reject conflicting arguments.
313 [(nomsg,), dict(string=nomsg), conflicting_call],
314 [(mymsg,), dict(string=nomsg), conflicting_call],
315 [(), dict(data=nomsg, string=nomsg), conflicting_call],
316 ]:
317 for constructor in self.hash_constructors:
318 digest_name = constructor(b'').name
319 with self.subTest(constructor.__name__, args=args, kwds=kwds):
320 with self.assertRaisesRegex(TypeError, errmsg):
321 constructor(*args, **kwds)
322 with self.subTest(digest_name, args=args, kwds=kwds):
323 with self.assertRaisesRegex(TypeError, errmsg):
324 hashlib.new(digest_name, *args, **kwds)
325 if (self._hashlib and
326 digest_name in self._hashlib._constructors):
327 with self.assertRaisesRegex(TypeError, errmsg):
328 self._hashlib.new(digest_name, *args, **kwds)
329
330 def test_unknown_hash(self):
331 self.assertRaises(ValueError, hashlib.new, 'spam spam spam spam spam')
332 self.assertRaises(TypeError, hashlib.new, 1)
333
334 def test_new_upper_to_lower(self):
335 self.assertEqual(hashlib.new("SHA256").name, "sha256")
336
337 @support.thread_unsafe("modifies sys.modules")
338 def test_get_builtin_constructor(self):
339 get_builtin_constructor = getattr(hashlib,
340 '__get_builtin_constructor')
341 builtin_constructor_cache = getattr(hashlib,
342 '__builtin_constructor_cache')
343 self.assertRaises(ValueError, get_builtin_constructor, 'test')
344 try:
345 import _md5
346 except ImportError:
347 self.skipTest("_md5 module not available")
348 # This forces an ImportError for "import _md5" statements
349 sys.modules['_md5'] = None
350 # clear the cache
351 builtin_constructor_cache.clear()
352 try:
353 self.assertRaises(ValueError, get_builtin_constructor, 'md5')
354 finally:
355 if '_md5' in locals():
356 sys.modules['_md5'] = _md5
357 else:
358 del sys.modules['_md5']
359 self.assertRaises(TypeError, get_builtin_constructor, 3)
360 constructor = get_builtin_constructor('md5')
361 self.assertIs(constructor, _md5.md5)
362 self.assertEqual(sorted(builtin_constructor_cache), ['MD5', 'md5'])
363
364 def test_hexdigest(self):
365 for cons in self.hash_constructors:
366 h = cons(usedforsecurity=False)
367 if h.name in self.shakes:
368 self.assertIsInstance(h.digest(16), bytes)
369 self.assertEqual(hexstr(h.digest(16)), h.hexdigest(16))
370 else:
371 self.assertIsInstance(h.digest(), bytes)
372 self.assertEqual(hexstr(h.digest()), h.hexdigest())
373
374 def test_digest_length_overflow(self):
375 # See issue #34922
376 large_sizes = (2**29, 2**32-10, 2**32+10, 2**61, 2**64-10, 2**64+10)
377 for cons in self.hash_constructors:
378 h = cons(usedforsecurity=False)
379 if h.name not in self.shakes:
380 continue
381 if HASH is not None and isinstance(h, HASH):
382 # _hashopenssl's take a size_t
383 continue
384 for digest in h.digest, h.hexdigest:
385 self.assertRaises(ValueError, digest, -10)
386 for length in large_sizes:
387 with self.assertRaises((ValueError, OverflowError)):
388 digest(length)
389
390 def test_name_attribute(self):
391 for cons in self.hash_constructors:
392 h = cons(usedforsecurity=False)
393 self.assertIsInstance(h.name, str)
394 if h.name in self.supported_hash_names:
395 self.assertIn(h.name, self.supported_hash_names)
396 else:
397 self.assertNotIn(h.name, self.supported_hash_names)
398 self.assertEqual(
399 h.name,
400 hashlib.new(h.name, usedforsecurity=False).name
401 )
402
403 def test_large_update(self):
404 aas = b'a' * 128
405 bees = b'b' * 127
406 cees = b'c' * 126
407 dees = b'd' * 2048 # HASHLIB_GIL_MINSIZE
408
409 for cons in self.hash_constructors:
410 m1 = cons(usedforsecurity=False)
411 m1.update(aas)
412 m1.update(bees)
413 m1.update(cees)
414 m1.update(dees)
415 if m1.name in self.shakes:
416 args = (16,)
417 else:
418 args = ()
419
420 m2 = cons(usedforsecurity=False)
421 m2.update(aas + bees + cees + dees)
422 self.assertEqual(m1.digest(*args), m2.digest(*args))
423
424 m3 = cons(aas + bees + cees + dees, usedforsecurity=False)
425 self.assertEqual(m1.digest(*args), m3.digest(*args))
426
427 # verify copy() doesn't touch original
428 m4 = cons(aas + bees + cees, usedforsecurity=False)
429 m4_digest = m4.digest(*args)
430 m4_copy = m4.copy()
431 m4_copy.update(dees)
432 self.assertEqual(m1.digest(*args), m4_copy.digest(*args))
433 self.assertEqual(m4.digest(*args), m4_digest)
434
435 @requires_resource('cpu')
436 def test_sha256_update_over_4gb(self):
437 zero_1mb = b"\0" * 1024 * 1024
438 h = hashlib.sha256()
439 for i in range(0, 4096):
440 h.update(zero_1mb)
441 h.update(b"hello world")
442 self.assertEqual(h.hexdigest(), "a5364f7a52ebe2e25f1838a4ca715a893b6fd7a23f2a0d9e9762120da8b1bf53")
443
444 @requires_resource('cpu')
445 def test_sha3_256_update_over_4gb(self):
446 zero_1mb = b"\0" * 1024 * 1024
447 h = hashlib.sha3_256()
448 for i in range(0, 4096):
449 h.update(zero_1mb)
450 h.update(b"hello world")
451 self.assertEqual(h.hexdigest(), "e2d4535e3b613135c14f2fe4e026d7ad8d569db44901740beffa30d430acb038")
452
453 @requires_resource('cpu')
454 @requires_blake2
455 def test_blake2_update_over_4gb(self):
456 # blake2s or blake2b doesn't matter based on how our C code is structured, this tests the
457 # common loop macro logic.
458 zero_1mb = b"\0" * 1024 * 1024
459 h = hashlib.blake2s()
460 for i in range(0, 4096):
461 h.update(zero_1mb)
462 h.update(b"hello world")
463 self.assertEqual(h.hexdigest(), "8a268e83dd30528bc0907fa2008c91de8f090a0b6e0e60a5ff0d999d8485526f")
464
465 def check(self, name, data, hexdigest, shake=False, **kwargs):
466 length = len(hexdigest)//2
467 hexdigest = hexdigest.lower()
468 constructors = self.constructors_to_test[name]
469 # 2 is for hashlib.name(...) and hashlib.new(name, ...)
470 self.assertGreaterEqual(len(constructors), 2)
471 for hash_object_constructor in constructors:
472 m = hash_object_constructor(data, **kwargs)
473 computed = m.hexdigest() if not shake else m.hexdigest(length)
474 self.assertEqual(
475 computed, hexdigest,
476 "Hash algorithm %s constructed using %s returned hexdigest"
477 " %r for %d byte input data that should have hashed to %r."
478 % (name, hash_object_constructor,
479 computed, len(data), hexdigest))
480 computed = m.digest() if not shake else m.digest(length)
481 digest = bytes.fromhex(hexdigest)
482 self.assertEqual(computed, digest)
483 if not shake:
484 self.assertEqual(len(digest), m.digest_size)
485
486 if not shake and kwargs.get("key") is None:
487 # skip shake and blake2 extended parameter tests
488 self.check_file_digest(name, data, hexdigest)
489
490 def check_file_digest(self, name, data, hexdigest):
491 hexdigest = hexdigest.lower()
492 try:
493 hashlib.new(name)
494 except ValueError:
495 # skip, algorithm is blocked by security policy.
496 return
497 digests = [name]
498 digests.extend(self.constructors_to_test[name])
499
500 with tempfile.TemporaryFile() as f:
501 f.write(data)
502
503 for digest in digests:
504 buf = io.BytesIO(data)
505 buf.seek(0)
506 self.assertEqual(
507 hashlib.file_digest(buf, digest).hexdigest(), hexdigest
508 )
509 f.seek(0)
510 digestobj = hashlib.file_digest(f, digest)
511 self.assertEqual(digestobj.hexdigest(), hexdigest)
512
513 def check_no_unicode(self, algorithm_name):
514 # Unicode objects are not allowed as input.
515 constructors = self.constructors_to_test[algorithm_name]
516 for hash_object_constructor in constructors:
517 self.assertRaises(TypeError, hash_object_constructor, 'spam')
518
519 def test_no_unicode(self):
520 self.check_no_unicode('md5')
521 self.check_no_unicode('sha1')
522 self.check_no_unicode('sha224')
523 self.check_no_unicode('sha256')
524 self.check_no_unicode('sha384')
525 self.check_no_unicode('sha512')
526
527 @requires_blake2
528 def test_no_unicode_blake2(self):
529 self.check_no_unicode('blake2b')
530 self.check_no_unicode('blake2s')
531
532 @requires_sha3
533 def test_no_unicode_sha3(self):
534 self.check_no_unicode('sha3_224')
535 self.check_no_unicode('sha3_256')
536 self.check_no_unicode('sha3_384')
537 self.check_no_unicode('sha3_512')
538 self.check_no_unicode('shake_128')
539 self.check_no_unicode('shake_256')
540
541 def check_blocksize_name(self, name, block_size=0, digest_size=0,
542 digest_length=None):
543 constructors = self.constructors_to_test[name]
544 for hash_object_constructor in constructors:
545 m = hash_object_constructor(usedforsecurity=False)
546 self.assertEqual(m.block_size, block_size)
547 self.assertEqual(m.digest_size, digest_size)
548 if digest_length:
549 self.assertEqual(len(m.digest(digest_length)),
550 digest_length)
551 self.assertEqual(len(m.hexdigest(digest_length)),
552 2*digest_length)
553 else:
554 self.assertEqual(len(m.digest()), digest_size)
555 self.assertEqual(len(m.hexdigest()), 2*digest_size)
556 self.assertEqual(m.name, name)
557 # split for sha3_512 / _sha3.sha3 object
558 self.assertIn(name.split("_")[0], repr(m).lower())
559
560 def test_blocksize_and_name(self):
561 self.check_blocksize_name('md5', 64, 16)
562 self.check_blocksize_name('sha1', 64, 20)
563 self.check_blocksize_name('sha224', 64, 28)
564 self.check_blocksize_name('sha256', 64, 32)
565 self.check_blocksize_name('sha384', 128, 48)
566 self.check_blocksize_name('sha512', 128, 64)
567
568 @requires_sha3
569 def test_blocksize_name_sha3(self):
570 self.check_blocksize_name('sha3_224', 144, 28)
571 self.check_blocksize_name('sha3_256', 136, 32)
572 self.check_blocksize_name('sha3_384', 104, 48)
573 self.check_blocksize_name('sha3_512', 72, 64)
574 self.check_blocksize_name('shake_128', 168, 0, 32)
575 self.check_blocksize_name('shake_256', 136, 0, 64)
576
577 def check_sha3(self, name, capacity, rate, suffix):
578 constructors = self.constructors_to_test[name]
579 for hash_object_constructor in constructors:
580 m = hash_object_constructor()
581 if name.startswith('shake_'):
582 if HASHXOF is not None and isinstance(m, HASHXOF):
583 # _hashopenssl's variant does not have extra SHA3 attributes
584 continue
585 else:
586 if HASH is not None and isinstance(m, HASH):
587 # _hashopenssl's variant does not have extra SHA3 attributes
588 continue
589 self.assertEqual(capacity + rate, 1600)
590 self.assertEqual(m._capacity_bits, capacity)
591 self.assertEqual(m._rate_bits, rate)
592 self.assertEqual(m._suffix, suffix)
593
594 @requires_sha3
595 def test_extra_sha3(self):
596 self.check_sha3('sha3_224', 448, 1152, b'\x06')
597 self.check_sha3('sha3_256', 512, 1088, b'\x06')
598 self.check_sha3('sha3_384', 768, 832, b'\x06')
599 self.check_sha3('sha3_512', 1024, 576, b'\x06')
600 self.check_sha3('shake_128', 256, 1344, b'\x1f')
601 self.check_sha3('shake_256', 512, 1088, b'\x1f')
602
603 @requires_blake2
604 def test_blocksize_name_blake2(self):
605 self.check_blocksize_name('blake2b', 128, 64)
606 self.check_blocksize_name('blake2s', 64, 32)
607
608 def test_case_md5_0(self):
609 self.check(
610 'md5', b'', 'd41d8cd98f00b204e9800998ecf8427e',
611 usedforsecurity=False
612 )
613
614 def test_case_md5_1(self):
615 self.check(
616 'md5', b'abc', '900150983cd24fb0d6963f7d28e17f72',
617 usedforsecurity=False
618 )
619
620 def test_case_md5_2(self):
621 self.check(
622 'md5',
623 b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
624 'd174ab98d277d9f5a5611c2c9f419d9f',
625 usedforsecurity=False
626 )
627
628 @unittest.skipIf(sys.maxsize < _4G + 5, 'test cannot run on 32-bit systems')
629 @bigmemtest(size=_4G + 5, memuse=1, dry_run=False)
630 def test_case_md5_huge(self, size):
631 self.check('md5', b'A'*size, 'c9af2dff37468ce5dfee8f2cfc0a9c6d')
632
633 @unittest.skipIf(sys.maxsize < _4G - 1, 'test cannot run on 32-bit systems')
634 @bigmemtest(size=_4G - 1, memuse=1, dry_run=False)
635 def test_case_md5_uintmax(self, size):
636 self.check('md5', b'A'*size, '28138d306ff1b8281f1a9067e1a1a2b3')
637
638 # use the three examples from Federal Information Processing Standards
639 # Publication 180-1, Secure Hash Standard, 1995 April 17
640 # http://www.itl.nist.gov/div897/pubs/fip180-1.htm
641
642 def test_case_sha1_0(self):
643 self.check('sha1', b"",
644 "da39a3ee5e6b4b0d3255bfef95601890afd80709")
645
646 def test_case_sha1_1(self):
647 self.check('sha1', b"abc",
648 "a9993e364706816aba3e25717850c26c9cd0d89d")
649
650 def test_case_sha1_2(self):
651 self.check('sha1',
652 b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
653 "84983e441c3bd26ebaae4aa1f95129e5e54670f1")
654
655 def test_case_sha1_3(self):
656 self.check('sha1', b"a" * 1000000,
657 "34aa973cd4c4daa4f61eeb2bdbad27316534016f")
658
659
660 # use the examples from Federal Information Processing Standards
661 # Publication 180-2, Secure Hash Standard, 2002 August 1
662 # http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
663
664 def test_case_sha224_0(self):
665 self.check('sha224', b"",
666 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f")
667
668 def test_case_sha224_1(self):
669 self.check('sha224', b"abc",
670 "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7")
671
672 def test_case_sha224_2(self):
673 self.check('sha224',
674 b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
675 "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525")
676
677 def test_case_sha224_3(self):
678 self.check('sha224', b"a" * 1000000,
679 "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67")
680
681
682 def test_case_sha256_0(self):
683 self.check('sha256', b"",
684 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
685
686 def test_case_sha256_1(self):
687 self.check('sha256', b"abc",
688 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
689
690 def test_case_sha256_2(self):
691 self.check('sha256',
692 b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
693 "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1")
694
695 def test_case_sha256_3(self):
696 self.check('sha256', b"a" * 1000000,
697 "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0")
698
699
700 def test_case_sha384_0(self):
701 self.check('sha384', b"",
702 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da"+
703 "274edebfe76f65fbd51ad2f14898b95b")
704
705 def test_case_sha384_1(self):
706 self.check('sha384', b"abc",
707 "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed"+
708 "8086072ba1e7cc2358baeca134c825a7")
709
710 def test_case_sha384_2(self):
711 self.check('sha384',
712 b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+
713 b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
714 "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712"+
715 "fcc7c71a557e2db966c3e9fa91746039")
716
717 def test_case_sha384_3(self):
718 self.check('sha384', b"a" * 1000000,
719 "9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b"+
720 "07b8b3dc38ecc4ebae97ddd87f3d8985")
721
722
723 def test_case_sha512_0(self):
724 self.check('sha512', b"",
725 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"+
726 "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e")
727
728 def test_case_sha512_1(self):
729 self.check('sha512', b"abc",
730 "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"+
731 "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f")
732
733 def test_case_sha512_2(self):
734 self.check('sha512',
735 b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+
736 b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
737 "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"+
738 "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909")
739
740 def test_case_sha512_3(self):
741 self.check('sha512', b"a" * 1000000,
742 "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+
743 "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b")
744
745 def skip_if_blake2_not_builtin(self, name, skip_reason):
746 # blake2 builtins may be absent if python built with
747 # a subset of --with-builtin-hashlib-hashes or none.
748 if "blake2" in name and "blake2" not in builtin_hashes:
749 self.skipTest(skip_reason)
750
751 def check_blake2(self, constructor, salt_size, person_size, key_size,
752 digest_size, max_offset):
753 self.assertEqual(constructor.SALT_SIZE, salt_size)
754 for i in range(salt_size + 1):
755 constructor(salt=b'a' * i)
756 salt = b'a' * (salt_size + 1)
757 self.assertRaises(ValueError, constructor, salt=salt)
758
759 self.assertEqual(constructor.PERSON_SIZE, person_size)
760 for i in range(person_size+1):
761 constructor(person=b'a' * i)
762 person = b'a' * (person_size + 1)
763 self.assertRaises(ValueError, constructor, person=person)
764
765 self.assertEqual(constructor.MAX_DIGEST_SIZE, digest_size)
766 for i in range(1, digest_size + 1):
767 constructor(digest_size=i)
768 self.assertRaises(ValueError, constructor, digest_size=-1)
769 self.assertRaises(ValueError, constructor, digest_size=0)
770 self.assertRaises(ValueError, constructor, digest_size=digest_size+1)
771
772 self.assertEqual(constructor.MAX_KEY_SIZE, key_size)
773 for i in range(key_size+1):
774 constructor(key=b'a' * i)
775 key = b'a' * (key_size + 1)
776 self.assertRaises(ValueError, constructor, key=key)
777 self.assertEqual(constructor().hexdigest(),
778 constructor(key=b'').hexdigest())
779
780 for i in range(0, 256):
781 constructor(fanout=i)
782 self.assertRaises(ValueError, constructor, fanout=-1)
783 self.assertRaises(ValueError, constructor, fanout=256)
784
785 for i in range(1, 256):
786 constructor(depth=i)
787 self.assertRaises(ValueError, constructor, depth=-1)
788 self.assertRaises(ValueError, constructor, depth=0)
789 self.assertRaises(ValueError, constructor, depth=256)
790
791 for i in range(0, 256):
792 constructor(node_depth=i)
793 self.assertRaises(ValueError, constructor, node_depth=-1)
794 self.assertRaises(ValueError, constructor, node_depth=256)
795
796 for i in range(0, digest_size + 1):
797 constructor(inner_size=i)
798 self.assertRaises(ValueError, constructor, inner_size=-1)
799 self.assertRaises(ValueError, constructor, inner_size=digest_size+1)
800
801 constructor(leaf_size=0)
802 constructor(leaf_size=(1<<32)-1)
803 self.assertRaises(ValueError, constructor, leaf_size=-1)
804 self.assertRaises(OverflowError, constructor, leaf_size=1<<32)
805
806 constructor(node_offset=0)
807 constructor(node_offset=max_offset)
808 self.assertRaises(ValueError, constructor, node_offset=-1)
809 self.assertRaises(OverflowError, constructor, node_offset=max_offset+1)
810
811 self.assertRaises(TypeError, constructor, '')
812
813 constructor(
814 b'',
815 key=b'',
816 salt=b'',
817 person=b'',
818 digest_size=17,
819 fanout=1,
820 depth=1,
821 leaf_size=256,
822 node_offset=512,
823 node_depth=1,
824 inner_size=7,
825 last_node=True
826 )
827
828 def blake2_rfc7693(self, constructor, md_len, in_len):
829 def selftest_seq(length, seed):
830 mask = (1<<32)-1
831 a = (0xDEAD4BAD * seed) & mask
832 b = 1
833 out = bytearray(length)
834 for i in range(length):
835 t = (a + b) & mask
836 a, b = b, t
837 out[i] = (t >> 24) & 0xFF
838 return out
839 outer = constructor(digest_size=32)
840 for outlen in md_len:
841 for inlen in in_len:
842 indata = selftest_seq(inlen, inlen)
843 key = selftest_seq(outlen, outlen)
844 unkeyed = constructor(indata, digest_size=outlen)
845 outer.update(unkeyed.digest())
846 keyed = constructor(indata, key=key, digest_size=outlen)
847 outer.update(keyed.digest())
848 return outer.hexdigest()
849
850 @requires_blake2
851 def test_blake2b(self):
852 self.check_blake2(hashlib.blake2b, 16, 16, 64, 64, (1<<64)-1)
853 b2b_md_len = [20, 32, 48, 64]
854 b2b_in_len = [0, 3, 128, 129, 255, 1024]
855 self.assertEqual(
856 self.blake2_rfc7693(hashlib.blake2b, b2b_md_len, b2b_in_len),
857 "c23a7800d98123bd10f506c61e29da5603d763b8bbad2e737f5e765a7bccd475")
858
859 @requires_blake2
860 def test_case_blake2b_0(self):
861 self.check('blake2b', b"",
862 "786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419"+
863 "d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce")
864
865 @requires_blake2
866 def test_case_blake2b_1(self):
867 self.check('blake2b', b"abc",
868 "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d1"+
869 "7d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923")
870
871 @requires_blake2
872 def test_case_blake2b_all_parameters(self):
873 # This checks that all the parameters work in general, and also that
874 # parameter byte order doesn't get confused on big endian platforms.
875 self.check('blake2b', b"foo",
876 "920568b0c5873b2f0ab67bedb6cf1b2b",
877 digest_size=16,
878 key=b"bar",
879 salt=b"baz",
880 person=b"bing",
881 fanout=2,
882 depth=3,
883 leaf_size=4,
884 node_offset=5,
885 node_depth=6,
886 inner_size=7,
887 last_node=True)
888
889 @requires_blake2
890 def test_blake2b_vectors(self):
891 for msg, key, md in read_vectors('blake2b'):
892 key = bytes.fromhex(key)
893 self.check('blake2b', msg, md, key=key)
894
895 @requires_blake2
896 def test_blake2s(self):
897 self.check_blake2(hashlib.blake2s, 8, 8, 32, 32, (1<<48)-1)
898 b2s_md_len = [16, 20, 28, 32]
899 b2s_in_len = [0, 3, 64, 65, 255, 1024]
900 self.assertEqual(
901 self.blake2_rfc7693(hashlib.blake2s, b2s_md_len, b2s_in_len),
902 "6a411f08ce25adcdfb02aba641451cec53c598b24f4fc787fbdc88797f4c1dfe")
903
904 @requires_blake2
905 def test_case_blake2s_0(self):
906 self.check('blake2s', b"",
907 "69217a3079908094e11121d042354a7c1f55b6482ca1a51e1b250dfd1ed0eef9")
908
909 @requires_blake2
910 def test_case_blake2s_1(self):
911 self.check('blake2s', b"abc",
912 "508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982")
913
914 @requires_blake2
915 def test_case_blake2s_all_parameters(self):
916 # This checks that all the parameters work in general, and also that
917 # parameter byte order doesn't get confused on big endian platforms.
918 self.check('blake2s', b"foo",
919 "bf2a8f7fe3c555012a6f8046e646bc75",
920 digest_size=16,
921 key=b"bar",
922 salt=b"baz",
923 person=b"bing",
924 fanout=2,
925 depth=3,
926 leaf_size=4,
927 node_offset=5,
928 node_depth=6,
929 inner_size=7,
930 last_node=True)
931
932 @requires_blake2
933 def test_blake2s_vectors(self):
934 for msg, key, md in read_vectors('blake2s'):
935 key = bytes.fromhex(key)
936 self.check('blake2s', msg, md, key=key)
937
938 @requires_sha3
939 def test_case_sha3_224_0(self):
940 self.check('sha3_224', b"",
941 "6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7")
942
943 @requires_sha3
944 def test_case_sha3_224_vector(self):
945 for msg, md in read_vectors('sha3_224'):
946 self.check('sha3_224', msg, md)
947
948 @requires_sha3
949 def test_case_sha3_256_0(self):
950 self.check('sha3_256', b"",
951 "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a")
952
953 @requires_sha3
954 def test_case_sha3_256_vector(self):
955 for msg, md in read_vectors('sha3_256'):
956 self.check('sha3_256', msg, md)
957
958 @requires_sha3
959 def test_case_sha3_384_0(self):
960 self.check('sha3_384', b"",
961 "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2a"+
962 "c3713831264adb47fb6bd1e058d5f004")
963
964 @requires_sha3
965 def test_case_sha3_384_vector(self):
966 for msg, md in read_vectors('sha3_384'):
967 self.check('sha3_384', msg, md)
968
969 @requires_sha3
970 def test_case_sha3_512_0(self):
971 self.check('sha3_512', b"",
972 "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a6"+
973 "15b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26")
974
975 @requires_sha3
976 def test_case_sha3_512_vector(self):
977 for msg, md in read_vectors('sha3_512'):
978 self.check('sha3_512', msg, md)
979
980 def test_case_shake_128_0(self):
981 self.check('shake_128', b"",
982 "7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26",
983 True)
984 self.check('shake_128', b"", "7f9c", True)
985
986 def test_case_shake128_vector(self):
987 for msg, md in read_vectors('shake_128'):
988 self.check('shake_128', msg, md, True)
989
990 def test_case_shake_256_0(self):
991 self.check('shake_256', b"",
992 "46b9dd2b0ba88d13233b3feb743eeb243fcd52ea62b81b82b50c27646ed5762f",
993 True)
994 self.check('shake_256', b"", "46b9", True)
995
996 def test_case_shake256_vector(self):
997 for msg, md in read_vectors('shake_256'):
998 self.check('shake_256', msg, md, True)
999
1000 def test_gil(self):
1001 # Check things work fine with an input larger than the size required
1002 # for multithreaded operation. Currently, all cryptographic modules
1003 # have the same constant value (2048) but in the future it might not
1004 # be the case.
1005 mods = ['_md5', '_sha1', '_sha2', '_sha3', '_blake2', '_hashlib']
1006 gil_minsize = hashlib_helper.find_gil_minsize(mods)
1007 for cons in self.hash_constructors:
1008 # constructors belong to one of the above modules
1009 m = cons(usedforsecurity=False)
1010 m.update(b'1')
1011 m.update(b'#' * gil_minsize)
1012 m.update(b'1')
1013
1014 m = cons(b'x' * gil_minsize, usedforsecurity=False)
1015 m.update(b'1')
1016
1017 def test_sha256_gil(self):
1018 gil_minsize = hashlib_helper.find_gil_minsize(['_sha2', '_hashlib'])
1019 m = hashlib.sha256()
1020 m.update(b'1')
1021 m.update(b'#' * gil_minsize)
1022 m.update(b'1')
1023 self.assertEqual(
1024 m.hexdigest(),
1025 '1cfceca95989f51f658e3f3ffe7f1cd43726c9e088c13ee10b46f57cef135b94'
1026 )
1027
1028 m = hashlib.sha256(b'1' + b'#' * gil_minsize + b'1')
1029 self.assertEqual(
1030 m.hexdigest(),
1031 '1cfceca95989f51f658e3f3ffe7f1cd43726c9e088c13ee10b46f57cef135b94'
1032 )
1033
1034 @threading_helper.reap_threads
1035 @threading_helper.requires_working_threading()
1036 def test_threaded_hashing(self):
1037 # Updating the same hash object from several threads at once
1038 # using data chunk sizes containing the same byte sequences.
1039 #
1040 # If the internal locks are working to prevent multiple
1041 # updates on the same object from running at once, the resulting
1042 # hash will be the same as doing it single threaded upfront.
1043 hasher = hashlib.sha1()
1044 num_threads = 5
1045 smallest_data = b'swineflu'
1046 data = smallest_data * 200000
1047 expected_hash = hashlib.sha1(data*num_threads).hexdigest()
1048
1049 def hash_in_chunks(chunk_size):
1050 index = 0
1051 while index < len(data):
1052 hasher.update(data[index:index + chunk_size])
1053 index += chunk_size
1054
1055 threads = []
1056 for threadnum in range(num_threads):
1057 chunk_size = len(data) // (10 ** threadnum)
1058 self.assertGreater(chunk_size, 0)
1059 self.assertEqual(chunk_size % len(smallest_data), 0)
1060 thread = threading.Thread(target=hash_in_chunks,
1061 args=(chunk_size,))
1062 threads.append(thread)
1063
1064 for thread in threads:
1065 thread.start()
1066 for thread in threads:
1067 thread.join()
1068
1069 self.assertEqual(expected_hash, hasher.hexdigest())
1070
1071 def test_get_fips_mode(self):
1072 fips_mode = self.is_fips_mode
1073 if fips_mode is not None:
1074 self.assertIsInstance(fips_mode, int)
1075
1076 @support.cpython_only
1077 def test_disallow_instantiation(self):
1078 for algorithm, constructors in self.constructors_to_test.items():
1079 if algorithm.startswith(("sha3_", "shake", "blake")):
1080 # _sha3 and _blake types can be instantiated
1081 continue
1082 # all other types have DISALLOW_INSTANTIATION
1083 for constructor in constructors:
1084 # In FIPS mode some algorithms are not available raising ValueError
1085 try:
1086 h = constructor()
1087 except ValueError:
1088 continue
1089 with self.subTest(constructor=constructor):
1090 support.check_disallow_instantiation(self, type(h))
1091
1092 @unittest.skipUnless(HASH is not None, 'need _hashlib')
1093 def test_hash_disallow_instantiation(self):
1094 # internal types like _hashlib.HASH are not constructable
1095 support.check_disallow_instantiation(self, HASH)
1096 if HASHXOF is not None:
1097 support.check_disallow_instantiation(self, HASHXOF)
1098
1099 def test_readonly_types(self):
1100 for algorithm, constructors in self.constructors_to_test.items():
1101 # all other types have DISALLOW_INSTANTIATION
1102 for constructor in constructors:
1103 # In FIPS mode some algorithms are not available raising ValueError
1104 try:
1105 hash_type = type(constructor())
1106 except ValueError:
1107 continue
1108 with self.subTest(hash_type=hash_type):
1109 with self.assertRaisesRegex(TypeError, "immutable type"):
1110 hash_type.value = False
1111
1112
1113 class KDFTests(unittest.TestCase):
1114
1115 pbkdf2_test_vectors = [
1116 (b'password', b'salt', 1, None),
1117 (b'password', b'salt', 2, None),
1118 (b'password', b'salt', 4096, None),
1119 # too slow, it takes over a minute on a fast CPU.
1120 #(b'password', b'salt', 16777216, None),
1121 (b'passwordPASSWORDpassword', b'saltSALTsaltSALTsaltSALTsaltSALTsalt',
1122 4096, -1),
1123 (b'pass\0word', b'sa\0lt', 4096, 16),
1124 ]
1125
1126 scrypt_test_vectors = [
1127 (b'', b'', 16, 1, 1, unhexlify('77d6576238657b203b19ca42c18a0497f16b4844e3074ae8dfdffa3fede21442fcd0069ded0948f8326a753a0fc81f17e8d3e0fb2e0d3628cf35e20c38d18906')),
1128 (b'password', b'NaCl', 1024, 8, 16, unhexlify('fdbabe1c9d3472007856e7190d01e9fe7c6ad7cbc8237830e77376634b3731622eaf30d92e22a3886ff109279d9830dac727afb94a83ee6d8360cbdfa2cc0640')),
1129 (b'pleaseletmein', b'SodiumChloride', 16384, 8, 1, unhexlify('7023bdcb3afd7348461c06cd81fd38ebfda8fbba904f8e3ea9b543f6545da1f2d5432955613f0fcf62d49705242a9af9e61e85dc0d651e40dfcf017b45575887')),
1130 ]
1131
1132 pbkdf2_results = {
1133 "sha1": [
1134 # official test vectors from RFC 6070
1135 (bytes.fromhex('0c60c80f961f0e71f3a9b524af6012062fe037a6'), None),
1136 (bytes.fromhex('ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957'), None),
1137 (bytes.fromhex('4b007901b765489abead49d926f721d065a429c1'), None),
1138 #(bytes.fromhex('eefe3d61cd4da4e4e9945b3d6ba2158c2634e984'), None),
1139 (bytes.fromhex('3d2eec4fe41c849b80c8d83662c0e44a8b291a964c'
1140 'f2f07038'), 25),
1141 (bytes.fromhex('56fa6aa75548099dcc37d7f03425e0c3'), None),],
1142 "sha256": [
1143 (bytes.fromhex('120fb6cffcf8b32c43e7225256c4f837'
1144 'a86548c92ccc35480805987cb70be17b'), None),
1145 (bytes.fromhex('ae4d0c95af6b46d32d0adff928f06dd0'
1146 '2a303f8ef3c251dfd6e2d85a95474c43'), None),
1147 (bytes.fromhex('c5e478d59288c841aa530db6845c4c8d'
1148 '962893a001ce4e11a4963873aa98134a'), None),
1149 #(bytes.fromhex('cf81c66fe8cfc04d1f31ecb65dab4089'
1150 # 'f7f179e89b3b0bcb17ad10e3ac6eba46'), None),
1151 (bytes.fromhex('348c89dbcbd32b2f32d814b8116e84cf2b17'
1152 '347ebc1800181c4e2a1fb8dd53e1c635518c7dac47e9'), 40),
1153 (bytes.fromhex('89b69d0516f829893c696226650a8687'), None),],
1154 "sha512": [
1155 (bytes.fromhex('867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5'
1156 'd513554e1c8cf252c02d470a285a0501bad999bfe943c08f'
1157 '050235d7d68b1da55e63f73b60a57fce'), None),
1158 (bytes.fromhex('e1d9c16aa681708a45f5c7c4e215ceb66e011a2e9f004071'
1159 '3f18aefdb866d53cf76cab2868a39b9f7840edce4fef5a82'
1160 'be67335c77a6068e04112754f27ccf4e'), None),
1161 (bytes.fromhex('d197b1b33db0143e018b12f3d1d1479e6cdebdcc97c5c0f8'
1162 '7f6902e072f457b5143f30602641b3d55cd335988cb36b84'
1163 '376060ecd532e039b742a239434af2d5'), None),
1164 (bytes.fromhex('8c0511f4c6e597c6ac6315d8f0362e225f3c501495ba23b8'
1165 '68c005174dc4ee71115b59f9e60cd9532fa33e0f75aefe30'
1166 '225c583a186cd82bd4daea9724a3d3b8'), 64),
1167 (bytes.fromhex('9d9e9c4cd21fe4be24d5b8244c759665'), None),],
1168 }
1169
1170 def _test_pbkdf2_hmac(self, pbkdf2, supported):
1171 for digest_name, results in self.pbkdf2_results.items():
1172 if digest_name not in supported:
1173 continue
1174 for i, vector in enumerate(self.pbkdf2_test_vectors):
1175 password, salt, rounds, dklen = vector
1176 expected, overwrite_dklen = results[i]
1177 if overwrite_dklen:
1178 dklen = overwrite_dklen
1179 out = pbkdf2(digest_name, password, salt, rounds, dklen)
1180 self.assertEqual(out, expected,
1181 (digest_name, password, salt, rounds, dklen))
1182 out = pbkdf2(digest_name, memoryview(password),
1183 memoryview(salt), rounds, dklen)
1184 self.assertEqual(out, expected)
1185 out = pbkdf2(digest_name, bytearray(password),
1186 bytearray(salt), rounds, dklen)
1187 self.assertEqual(out, expected)
1188 if dklen is None:
1189 out = pbkdf2(digest_name, password, salt, rounds)
1190 self.assertEqual(out, expected,
1191 (digest_name, password, salt, rounds))
1192
1193 with self.assertRaisesRegex(ValueError, '.*unsupported.*'):
1194 pbkdf2('unknown', b'pass', b'salt', 1)
1195
1196 if 'sha1' in supported:
1197 self.assertRaises(
1198 TypeError, pbkdf2, b'sha1', b'pass', b'salt', 1
1199 )
1200 self.assertRaises(
1201 TypeError, pbkdf2, 'sha1', 'pass', 'salt', 1
1202 )
1203 self.assertRaises(
1204 ValueError, pbkdf2, 'sha1', b'pass', b'salt', 0
1205 )
1206 self.assertRaises(
1207 ValueError, pbkdf2, 'sha1', b'pass', b'salt', -1
1208 )
1209 self.assertRaises(
1210 ValueError, pbkdf2, 'sha1', b'pass', b'salt', 1, 0
1211 )
1212 self.assertRaises(
1213 ValueError, pbkdf2, 'sha1', b'pass', b'salt', 1, -1
1214 )
1215 out = pbkdf2(hash_name='sha1', password=b'password', salt=b'salt',
1216 iterations=1, dklen=None)
1217 self.assertEqual(out, self.pbkdf2_results['sha1'][0][0])
1218
1219 @unittest.skipIf(openssl_hashlib is None, "requires OpenSSL bindings")
1220 def test_pbkdf2_hmac_c(self):
1221 self._test_pbkdf2_hmac(openssl_hashlib.pbkdf2_hmac, openssl_md_meth_names)
1222
1223 @unittest.skipUnless(hasattr(hashlib, 'scrypt'),
1224 ' test requires OpenSSL > 1.1')
1225 @unittest.skipIf(get_fips_mode(), reason="scrypt is blocked in FIPS mode")
1226 def test_scrypt(self):
1227 for password, salt, n, r, p, expected in self.scrypt_test_vectors:
1228 result = hashlib.scrypt(password, salt=salt, n=n, r=r, p=p)
1229 self.assertEqual(result, expected)
1230
1231 # this values should work
1232 hashlib.scrypt(b'password', salt=b'salt', n=2, r=8, p=1)
1233 # password and salt must be bytes-like
1234 with self.assertRaises(TypeError):
1235 hashlib.scrypt('password', salt=b'salt', n=2, r=8, p=1)
1236 with self.assertRaises(TypeError):
1237 hashlib.scrypt(b'password', salt='salt', n=2, r=8, p=1)
1238 # require keyword args
1239 with self.assertRaises(TypeError):
1240 hashlib.scrypt(b'password')
1241 with self.assertRaises(TypeError):
1242 hashlib.scrypt(b'password', b'salt')
1243 with self.assertRaises(TypeError):
1244 hashlib.scrypt(b'password', 2, 8, 1, salt=b'salt')
1245 for n in [-1, 0, 1, None]:
1246 with self.assertRaises((ValueError, OverflowError, TypeError)):
1247 hashlib.scrypt(b'password', salt=b'salt', n=n, r=8, p=1)
1248 for r in [-1, 0, None]:
1249 with self.assertRaises((ValueError, OverflowError, TypeError)):
1250 hashlib.scrypt(b'password', salt=b'salt', n=2, r=r, p=1)
1251 for p in [-1, 0, None]:
1252 with self.assertRaises((ValueError, OverflowError, TypeError)):
1253 hashlib.scrypt(b'password', salt=b'salt', n=2, r=8, p=p)
1254 for maxmem in [-1, None]:
1255 with self.assertRaises((ValueError, OverflowError, TypeError)):
1256 hashlib.scrypt(b'password', salt=b'salt', n=2, r=8, p=1,
1257 maxmem=maxmem)
1258 for dklen in [-1, None]:
1259 with self.assertRaises((ValueError, OverflowError, TypeError)):
1260 hashlib.scrypt(b'password', salt=b'salt', n=2, r=8, p=1,
1261 dklen=dklen)
1262
1263 def test_normalized_name(self):
1264 self.assertNotIn("blake2b512", hashlib.algorithms_available)
1265 self.assertNotIn("sha3-512", hashlib.algorithms_available)
1266
1267 def test_file_digest(self):
1268 data = b'a' * 65536
1269 d1 = hashlib.sha256()
1270 with tempfile.NamedTemporaryFile(delete_on_close=False) as fp:
1271 for _ in range(10):
1272 d1.update(data)
1273 fp.write(data)
1274 fp.close()
1275
1276 with open(fp.name, "rb") as f:
1277 d2 = hashlib.file_digest(f, hashlib.sha256)
1278
1279 self.assertEqual(d1.hexdigest(), d2.hexdigest())
1280 self.assertEqual(d1.name, d2.name)
1281 self.assertIs(type(d1), type(d2))
1282
1283 with self.assertRaises(ValueError):
1284 with open(fp.name, "r") as f:
1285 hashlib.file_digest(f, "sha256")
1286
1287 with self.assertRaises(ValueError):
1288 with open(fp.name, "wb") as f:
1289 hashlib.file_digest(f, "sha256")
1290
1291 with self.assertRaises(ValueError):
1292 hashlib.file_digest(None, "sha256")
1293
1294 class NonBlocking:
1295 def readinto(self, buf):
1296 return None
1297 def readable(self):
1298 return True
1299
1300 with self.assertRaises(BlockingIOError):
1301 hashlib.file_digest(NonBlocking(), hashlib.sha256)
1302
1303
1304 if __name__ == "__main__":
1305 unittest.main()