Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 1 | # Test hashlib module |
| 2 | # |
| 3 | # $Id$ |
| 4 | # |
Benjamin Peterson | 46a9900 | 2010-01-09 18:45:30 +0000 | [diff] [blame] | 5 | # Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 6 | # Licensed to PSF under a Contributor Agreement. |
| 7 | # |
| 8 | |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 9 | import array |
Christian Heimes | 39093e9 | 2016-09-06 20:22:28 +0200 | [diff] [blame] | 10 | from binascii import unhexlify |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 11 | import hashlib |
Benjamin Peterson | 77526f0 | 2018-01-29 18:03:01 -0800 | [diff] [blame] | 12 | import importlib |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 13 | import itertools |
Antoine Pitrou | 019ff19 | 2012-05-16 16:41:26 +0200 | [diff] [blame] | 14 | import os |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 15 | import sys |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 16 | import sysconfig |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 17 | import threading |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 18 | import unittest |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 19 | import warnings |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 20 | from test import support |
Christian Heimes | 3626a50 | 2013-10-19 14:12:02 +0200 | [diff] [blame] | 21 | from test.support import _4G, bigmemtest, import_fresh_module |
Christian Heimes | 8118824 | 2016-09-08 10:53:40 +0200 | [diff] [blame] | 22 | from http.client import HTTPException |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 23 | |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 24 | # Were we compiled --with-pydebug or with #define Py_DEBUG? |
| 25 | COMPILED_WITH_PYDEBUG = hasattr(sys, 'gettotalrefcount') |
| 26 | |
Christian Heimes | 3626a50 | 2013-10-19 14:12:02 +0200 | [diff] [blame] | 27 | c_hashlib = import_fresh_module('hashlib', fresh=['_hashlib']) |
| 28 | py_hashlib = import_fresh_module('hashlib', blocked=['_hashlib']) |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 29 | |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 30 | builtin_hashes = sysconfig.get_config_var("PY_BUILTIN_HASHLIB_HASHES") |
| 31 | if builtin_hashes is None: |
| 32 | builtin_hashes = {'md5', 'sha1', 'sha256', 'sha512', 'sha3', 'blake2'} |
| 33 | else: |
| 34 | builtin_hashes = { |
| 35 | m.strip() for m in builtin_hashes.strip('"').lower().split(",") |
| 36 | } |
| 37 | |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 38 | try: |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 39 | from _hashlib import HASH, HASHXOF, openssl_md_meth_names |
Christian Heimes | 995b5d3 | 2019-09-13 15:31:19 +0200 | [diff] [blame] | 40 | except ImportError: |
| 41 | HASH = None |
Christian Heimes | d5b3f6b | 2020-05-16 22:27:06 +0200 | [diff] [blame] | 42 | HASHXOF = None |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 43 | openssl_md_meth_names = frozenset() |
Christian Heimes | 995b5d3 | 2019-09-13 15:31:19 +0200 | [diff] [blame] | 44 | |
| 45 | try: |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 46 | import _blake2 |
| 47 | except ImportError: |
| 48 | _blake2 = None |
| 49 | |
| 50 | requires_blake2 = unittest.skipUnless(_blake2, 'requires _blake2') |
| 51 | |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 52 | try: |
| 53 | import _sha3 |
| 54 | except ImportError: |
| 55 | _sha3 = None |
| 56 | |
| 57 | requires_sha3 = unittest.skipUnless(_sha3, 'requires _sha3') |
| 58 | |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 59 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 60 | def hexstr(s): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 61 | assert isinstance(s, bytes), repr(s) |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 62 | h = "0123456789abcdef" |
| 63 | r = '' |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 64 | for i in s: |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 65 | r += h[(i >> 4) & 0xF] + h[i & 0xF] |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 66 | return r |
| 67 | |
| 68 | |
Christian Heimes | 59a0464 | 2016-09-07 01:21:14 +0200 | [diff] [blame] | 69 | URL = "http://www.pythontest.net/hashlib/{}.txt" |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 70 | |
| 71 | def read_vectors(hash_name): |
Christian Heimes | 8118824 | 2016-09-08 10:53:40 +0200 | [diff] [blame] | 72 | url = URL.format(hash_name) |
| 73 | try: |
| 74 | testdata = support.open_urlresource(url) |
| 75 | except (OSError, HTTPException): |
| 76 | raise unittest.SkipTest("Could not retrieve {}".format(url)) |
| 77 | with testdata: |
| 78 | for line in testdata: |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 79 | line = line.strip() |
| 80 | if line.startswith('#') or not line: |
| 81 | continue |
| 82 | parts = line.split(',') |
| 83 | parts[0] = bytes.fromhex(parts[0]) |
| 84 | yield parts |
| 85 | |
| 86 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 87 | class HashLibTestCase(unittest.TestCase): |
| 88 | supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1', |
| 89 | 'sha224', 'SHA224', 'sha256', 'SHA256', |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 90 | 'sha384', 'SHA384', 'sha512', 'SHA512', |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 91 | 'blake2b', 'blake2s', |
| 92 | 'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512', |
| 93 | 'shake_128', 'shake_256') |
| 94 | |
| 95 | shakes = {'shake_128', 'shake_256'} |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 96 | |
Antoine Pitrou | 019ff19 | 2012-05-16 16:41:26 +0200 | [diff] [blame] | 97 | # Issue #14693: fallback modules are always compiled under POSIX |
| 98 | _warn_on_extension_import = os.name == 'posix' or COMPILED_WITH_PYDEBUG |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 99 | |
| 100 | def _conditional_import_module(self, module_name): |
| 101 | """Import a module and return a reference to it or None on failure.""" |
| 102 | try: |
Benjamin Peterson | 77526f0 | 2018-01-29 18:03:01 -0800 | [diff] [blame] | 103 | return importlib.import_module(module_name) |
| 104 | except ModuleNotFoundError as error: |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 105 | if self._warn_on_extension_import: |
| 106 | warnings.warn('Did a C extension fail to compile? %s' % error) |
Benjamin Peterson | 77526f0 | 2018-01-29 18:03:01 -0800 | [diff] [blame] | 107 | return None |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 108 | |
| 109 | def __init__(self, *args, **kwargs): |
| 110 | algorithms = set() |
| 111 | for algorithm in self.supported_hash_names: |
| 112 | algorithms.add(algorithm.lower()) |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 113 | |
| 114 | _blake2 = self._conditional_import_module('_blake2') |
| 115 | if _blake2: |
| 116 | algorithms.update({'blake2b', 'blake2s'}) |
| 117 | |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 118 | self.constructors_to_test = {} |
| 119 | for algorithm in algorithms: |
| 120 | self.constructors_to_test[algorithm] = set() |
| 121 | |
| 122 | # For each algorithm, test the direct constructor and the use |
| 123 | # of hashlib.new given the algorithm name. |
| 124 | for algorithm, constructors in self.constructors_to_test.items(): |
| 125 | constructors.add(getattr(hashlib, algorithm)) |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 126 | def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm, **kwargs): |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 127 | if data is None: |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 128 | return hashlib.new(_alg, **kwargs) |
| 129 | return hashlib.new(_alg, data, **kwargs) |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 130 | constructors.add(_test_algorithm_via_hashlib_new) |
| 131 | |
| 132 | _hashlib = self._conditional_import_module('_hashlib') |
Christian Heimes | 9055815 | 2019-09-27 15:03:53 +0200 | [diff] [blame] | 133 | self._hashlib = _hashlib |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 134 | if _hashlib: |
| 135 | # These two algorithms should always be present when this module |
| 136 | # is compiled. If not, something was compiled wrong. |
Gregory P. Smith | 914061a | 2013-08-05 13:14:37 -0700 | [diff] [blame] | 137 | self.assertTrue(hasattr(_hashlib, 'openssl_md5')) |
| 138 | self.assertTrue(hasattr(_hashlib, 'openssl_sha1')) |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 139 | for algorithm, constructors in self.constructors_to_test.items(): |
| 140 | constructor = getattr(_hashlib, 'openssl_'+algorithm, None) |
| 141 | if constructor: |
Christian Heimes | 9055815 | 2019-09-27 15:03:53 +0200 | [diff] [blame] | 142 | try: |
| 143 | constructor() |
| 144 | except ValueError: |
| 145 | # default constructor blocked by crypto policy |
| 146 | pass |
| 147 | else: |
| 148 | constructors.add(constructor) |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 149 | |
Christian Heimes | e535107 | 2013-10-22 14:59:12 +0200 | [diff] [blame] | 150 | def add_builtin_constructor(name): |
| 151 | constructor = getattr(hashlib, "__get_builtin_constructor")(name) |
| 152 | self.constructors_to_test[name].add(constructor) |
| 153 | |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 154 | _md5 = self._conditional_import_module('_md5') |
| 155 | if _md5: |
Christian Heimes | e535107 | 2013-10-22 14:59:12 +0200 | [diff] [blame] | 156 | add_builtin_constructor('md5') |
Gregory P. Smith | b04ded4 | 2010-01-03 00:38:10 +0000 | [diff] [blame] | 157 | _sha1 = self._conditional_import_module('_sha1') |
| 158 | if _sha1: |
Christian Heimes | e535107 | 2013-10-22 14:59:12 +0200 | [diff] [blame] | 159 | add_builtin_constructor('sha1') |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 160 | _sha256 = self._conditional_import_module('_sha256') |
| 161 | if _sha256: |
Christian Heimes | e535107 | 2013-10-22 14:59:12 +0200 | [diff] [blame] | 162 | add_builtin_constructor('sha224') |
| 163 | add_builtin_constructor('sha256') |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 164 | _sha512 = self._conditional_import_module('_sha512') |
| 165 | if _sha512: |
Christian Heimes | e535107 | 2013-10-22 14:59:12 +0200 | [diff] [blame] | 166 | add_builtin_constructor('sha384') |
| 167 | add_builtin_constructor('sha512') |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 168 | if _blake2: |
| 169 | add_builtin_constructor('blake2s') |
| 170 | add_builtin_constructor('blake2b') |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 171 | |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 172 | _sha3 = self._conditional_import_module('_sha3') |
| 173 | if _sha3: |
| 174 | add_builtin_constructor('sha3_224') |
| 175 | add_builtin_constructor('sha3_256') |
| 176 | add_builtin_constructor('sha3_384') |
| 177 | add_builtin_constructor('sha3_512') |
| 178 | add_builtin_constructor('shake_128') |
| 179 | add_builtin_constructor('shake_256') |
| 180 | |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 181 | super(HashLibTestCase, self).__init__(*args, **kwargs) |
| 182 | |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 183 | @property |
| 184 | def hash_constructors(self): |
| 185 | constructors = self.constructors_to_test.values() |
| 186 | return itertools.chain.from_iterable(constructors) |
| 187 | |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 188 | @property |
| 189 | def is_fips_mode(self): |
| 190 | if hasattr(self._hashlib, "get_fips_mode"): |
| 191 | return self._hashlib.get_fips_mode() |
| 192 | else: |
| 193 | return None |
| 194 | |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 195 | def test_hash_array(self): |
| 196 | a = array.array("b", range(10)) |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 197 | for cons in self.hash_constructors: |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 198 | c = cons(a, usedforsecurity=False) |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 199 | if c.name in self.shakes: |
| 200 | c.hexdigest(16) |
| 201 | else: |
| 202 | c.hexdigest() |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 203 | |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 204 | def test_algorithms_guaranteed(self): |
| 205 | self.assertEqual(hashlib.algorithms_guaranteed, |
Raymond Hettinger | bf1d2bc | 2011-01-24 04:52:27 +0000 | [diff] [blame] | 206 | set(_algo for _algo in self.supported_hash_names |
Gregory P. Smith | 86508cc | 2010-03-01 02:05:26 +0000 | [diff] [blame] | 207 | if _algo.islower())) |
| 208 | |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 209 | def test_algorithms_available(self): |
| 210 | self.assertTrue(set(hashlib.algorithms_guaranteed). |
| 211 | issubset(hashlib.algorithms_available)) |
| 212 | |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 213 | def test_usedforsecurity_true(self): |
| 214 | hashlib.new("sha256", usedforsecurity=True) |
| 215 | if self.is_fips_mode: |
| 216 | self.skipTest("skip in FIPS mode") |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 217 | for cons in self.hash_constructors: |
| 218 | cons(usedforsecurity=True) |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 219 | cons(b'', usedforsecurity=True) |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 220 | hashlib.new("md5", usedforsecurity=True) |
| 221 | hashlib.md5(usedforsecurity=True) |
| 222 | if self._hashlib is not None: |
| 223 | self._hashlib.new("md5", usedforsecurity=True) |
| 224 | self._hashlib.openssl_md5(usedforsecurity=True) |
| 225 | |
| 226 | def test_usedforsecurity_false(self): |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 227 | hashlib.new("sha256", usedforsecurity=False) |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 228 | for cons in self.hash_constructors: |
| 229 | cons(usedforsecurity=False) |
| 230 | cons(b'', usedforsecurity=False) |
| 231 | hashlib.new("md5", usedforsecurity=False) |
| 232 | hashlib.md5(usedforsecurity=False) |
Christian Heimes | 9055815 | 2019-09-27 15:03:53 +0200 | [diff] [blame] | 233 | if self._hashlib is not None: |
| 234 | self._hashlib.new("md5", usedforsecurity=False) |
| 235 | self._hashlib.openssl_md5(usedforsecurity=False) |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 236 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 237 | def test_unknown_hash(self): |
Amaury Forgeot d'Arc | 3a3dc17 | 2012-06-29 01:53:13 +0200 | [diff] [blame] | 238 | self.assertRaises(ValueError, hashlib.new, 'spam spam spam spam spam') |
| 239 | self.assertRaises(TypeError, hashlib.new, 1) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 240 | |
Christian Heimes | 5a4f82f | 2019-09-12 14:42:07 +0200 | [diff] [blame] | 241 | def test_new_upper_to_lower(self): |
| 242 | self.assertEqual(hashlib.new("SHA256").name, "sha256") |
| 243 | |
Gregory P. Smith | 12c9d02 | 2011-05-14 15:15:49 -0700 | [diff] [blame] | 244 | def test_get_builtin_constructor(self): |
Christian Heimes | e535107 | 2013-10-22 14:59:12 +0200 | [diff] [blame] | 245 | get_builtin_constructor = getattr(hashlib, |
| 246 | '__get_builtin_constructor') |
| 247 | builtin_constructor_cache = getattr(hashlib, |
| 248 | '__builtin_constructor_cache') |
Gregory P. Smith | 12c9d02 | 2011-05-14 15:15:49 -0700 | [diff] [blame] | 249 | self.assertRaises(ValueError, get_builtin_constructor, 'test') |
| 250 | try: |
| 251 | import _md5 |
| 252 | except ImportError: |
Benjamin Peterson | 9544180 | 2018-01-29 22:14:17 -0800 | [diff] [blame] | 253 | self.skipTest("_md5 module not available") |
Gregory P. Smith | 12c9d02 | 2011-05-14 15:15:49 -0700 | [diff] [blame] | 254 | # This forces an ImportError for "import _md5" statements |
| 255 | sys.modules['_md5'] = None |
Christian Heimes | e535107 | 2013-10-22 14:59:12 +0200 | [diff] [blame] | 256 | # clear the cache |
| 257 | builtin_constructor_cache.clear() |
Gregory P. Smith | 12c9d02 | 2011-05-14 15:15:49 -0700 | [diff] [blame] | 258 | try: |
| 259 | self.assertRaises(ValueError, get_builtin_constructor, 'md5') |
| 260 | finally: |
| 261 | if '_md5' in locals(): |
| 262 | sys.modules['_md5'] = _md5 |
| 263 | else: |
| 264 | del sys.modules['_md5'] |
Gregory P. Smith | 76c28f7 | 2012-07-21 21:19:53 -0700 | [diff] [blame] | 265 | self.assertRaises(TypeError, get_builtin_constructor, 3) |
Christian Heimes | e535107 | 2013-10-22 14:59:12 +0200 | [diff] [blame] | 266 | constructor = get_builtin_constructor('md5') |
| 267 | self.assertIs(constructor, _md5.md5) |
| 268 | self.assertEqual(sorted(builtin_constructor_cache), ['MD5', 'md5']) |
Gregory P. Smith | 12c9d02 | 2011-05-14 15:15:49 -0700 | [diff] [blame] | 269 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 270 | def test_hexdigest(self): |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 271 | for cons in self.hash_constructors: |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 272 | h = cons(usedforsecurity=False) |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 273 | if h.name in self.shakes: |
| 274 | self.assertIsInstance(h.digest(16), bytes) |
| 275 | self.assertEqual(hexstr(h.digest(16)), h.hexdigest(16)) |
| 276 | else: |
| 277 | self.assertIsInstance(h.digest(), bytes) |
| 278 | self.assertEqual(hexstr(h.digest()), h.hexdigest()) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 279 | |
Serhiy Storchaka | 9b8c2e7 | 2018-10-11 07:41:00 +0300 | [diff] [blame] | 280 | def test_digest_length_overflow(self): |
| 281 | # See issue #34922 |
| 282 | large_sizes = (2**29, 2**32-10, 2**32+10, 2**61, 2**64-10, 2**64+10) |
| 283 | for cons in self.hash_constructors: |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 284 | h = cons(usedforsecurity=False) |
Serhiy Storchaka | 9b8c2e7 | 2018-10-11 07:41:00 +0300 | [diff] [blame] | 285 | if h.name not in self.shakes: |
| 286 | continue |
Christian Heimes | d5b3f6b | 2020-05-16 22:27:06 +0200 | [diff] [blame] | 287 | if HASH is not None and isinstance(h, HASH): |
| 288 | # _hashopenssl's take a size_t |
| 289 | continue |
Serhiy Storchaka | 9b8c2e7 | 2018-10-11 07:41:00 +0300 | [diff] [blame] | 290 | for digest in h.digest, h.hexdigest: |
| 291 | self.assertRaises(ValueError, digest, -10) |
| 292 | for length in large_sizes: |
| 293 | with self.assertRaises((ValueError, OverflowError)): |
| 294 | digest(length) |
| 295 | |
Jason R. Coombs | b2aa6f4 | 2013-08-03 11:39:39 +0200 | [diff] [blame] | 296 | def test_name_attribute(self): |
| 297 | for cons in self.hash_constructors: |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 298 | h = cons(usedforsecurity=False) |
Gregory P. Smith | 914061a | 2013-08-05 13:14:37 -0700 | [diff] [blame] | 299 | self.assertIsInstance(h.name, str) |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 300 | if h.name in self.supported_hash_names: |
| 301 | self.assertIn(h.name, self.supported_hash_names) |
| 302 | else: |
| 303 | self.assertNotIn(h.name, self.supported_hash_names) |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 304 | self.assertEqual( |
| 305 | h.name, |
| 306 | hashlib.new(h.name, usedforsecurity=False).name |
| 307 | ) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 308 | |
| 309 | def test_large_update(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 310 | aas = b'a' * 128 |
| 311 | bees = b'b' * 127 |
| 312 | cees = b'c' * 126 |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 313 | dees = b'd' * 2048 # HASHLIB_GIL_MINSIZE |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 314 | |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 315 | for cons in self.hash_constructors: |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 316 | m1 = cons(usedforsecurity=False) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 317 | m1.update(aas) |
| 318 | m1.update(bees) |
| 319 | m1.update(cees) |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 320 | m1.update(dees) |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 321 | if m1.name in self.shakes: |
| 322 | args = (16,) |
| 323 | else: |
| 324 | args = () |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 325 | |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 326 | m2 = cons(usedforsecurity=False) |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 327 | m2.update(aas + bees + cees + dees) |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 328 | self.assertEqual(m1.digest(*args), m2.digest(*args)) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 329 | |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 330 | m3 = cons(aas + bees + cees + dees, usedforsecurity=False) |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 331 | self.assertEqual(m1.digest(*args), m3.digest(*args)) |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 332 | |
| 333 | # verify copy() doesn't touch original |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 334 | m4 = cons(aas + bees + cees, usedforsecurity=False) |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 335 | m4_digest = m4.digest(*args) |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 336 | m4_copy = m4.copy() |
| 337 | m4_copy.update(dees) |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 338 | self.assertEqual(m1.digest(*args), m4_copy.digest(*args)) |
| 339 | self.assertEqual(m4.digest(*args), m4_digest) |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 340 | |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 341 | def check(self, name, data, hexdigest, shake=False, **kwargs): |
| 342 | length = len(hexdigest)//2 |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 343 | hexdigest = hexdigest.lower() |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 344 | constructors = self.constructors_to_test[name] |
| 345 | # 2 is for hashlib.name(...) and hashlib.new(name, ...) |
| 346 | self.assertGreaterEqual(len(constructors), 2) |
| 347 | for hash_object_constructor in constructors: |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 348 | m = hash_object_constructor(data, **kwargs) |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 349 | computed = m.hexdigest() if not shake else m.hexdigest(length) |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 350 | self.assertEqual( |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 351 | computed, hexdigest, |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 352 | "Hash algorithm %s constructed using %s returned hexdigest" |
| 353 | " %r for %d byte input data that should have hashed to %r." |
| 354 | % (name, hash_object_constructor, |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 355 | computed, len(data), hexdigest)) |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 356 | computed = m.digest() if not shake else m.digest(length) |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 357 | digest = bytes.fromhex(hexdigest) |
| 358 | self.assertEqual(computed, digest) |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 359 | if not shake: |
| 360 | self.assertEqual(len(digest), m.digest_size) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 361 | |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 362 | def check_no_unicode(self, algorithm_name): |
| 363 | # Unicode objects are not allowed as input. |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 364 | constructors = self.constructors_to_test[algorithm_name] |
| 365 | for hash_object_constructor in constructors: |
| 366 | self.assertRaises(TypeError, hash_object_constructor, 'spam') |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 367 | |
| 368 | def test_no_unicode(self): |
| 369 | self.check_no_unicode('md5') |
| 370 | self.check_no_unicode('sha1') |
| 371 | self.check_no_unicode('sha224') |
| 372 | self.check_no_unicode('sha256') |
| 373 | self.check_no_unicode('sha384') |
| 374 | self.check_no_unicode('sha512') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 375 | |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 376 | @requires_blake2 |
| 377 | def test_no_unicode_blake2(self): |
| 378 | self.check_no_unicode('blake2b') |
| 379 | self.check_no_unicode('blake2s') |
| 380 | |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 381 | @requires_sha3 |
| 382 | def test_no_unicode_sha3(self): |
| 383 | self.check_no_unicode('sha3_224') |
| 384 | self.check_no_unicode('sha3_256') |
| 385 | self.check_no_unicode('sha3_384') |
| 386 | self.check_no_unicode('sha3_512') |
| 387 | self.check_no_unicode('shake_128') |
| 388 | self.check_no_unicode('shake_256') |
| 389 | |
| 390 | def check_blocksize_name(self, name, block_size=0, digest_size=0, |
| 391 | digest_length=None): |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 392 | constructors = self.constructors_to_test[name] |
| 393 | for hash_object_constructor in constructors: |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 394 | m = hash_object_constructor(usedforsecurity=False) |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 395 | self.assertEqual(m.block_size, block_size) |
| 396 | self.assertEqual(m.digest_size, digest_size) |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 397 | if digest_length: |
| 398 | self.assertEqual(len(m.digest(digest_length)), |
| 399 | digest_length) |
| 400 | self.assertEqual(len(m.hexdigest(digest_length)), |
| 401 | 2*digest_length) |
| 402 | else: |
| 403 | self.assertEqual(len(m.digest()), digest_size) |
| 404 | self.assertEqual(len(m.hexdigest()), 2*digest_size) |
Christian Heimes | 37d5ceb | 2013-08-15 18:31:48 +0200 | [diff] [blame] | 405 | self.assertEqual(m.name, name) |
Christian Heimes | d49a371 | 2013-07-30 15:35:54 +0200 | [diff] [blame] | 406 | # split for sha3_512 / _sha3.sha3 object |
Christian Heimes | 37d5ceb | 2013-08-15 18:31:48 +0200 | [diff] [blame] | 407 | self.assertIn(name.split("_")[0], repr(m)) |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 408 | |
| 409 | def test_blocksize_name(self): |
| 410 | self.check_blocksize_name('md5', 64, 16) |
| 411 | self.check_blocksize_name('sha1', 64, 20) |
| 412 | self.check_blocksize_name('sha224', 64, 28) |
| 413 | self.check_blocksize_name('sha256', 64, 32) |
| 414 | self.check_blocksize_name('sha384', 128, 48) |
| 415 | self.check_blocksize_name('sha512', 128, 64) |
Christian Heimes | e370409 | 2016-09-23 11:32:30 +0200 | [diff] [blame] | 416 | |
| 417 | @requires_sha3 |
| 418 | def test_blocksize_name_sha3(self): |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 419 | self.check_blocksize_name('sha3_224', 144, 28) |
| 420 | self.check_blocksize_name('sha3_256', 136, 32) |
| 421 | self.check_blocksize_name('sha3_384', 104, 48) |
| 422 | self.check_blocksize_name('sha3_512', 72, 64) |
| 423 | self.check_blocksize_name('shake_128', 168, 0, 32) |
| 424 | self.check_blocksize_name('shake_256', 136, 0, 64) |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 425 | |
Christian Heimes | e370409 | 2016-09-23 11:32:30 +0200 | [diff] [blame] | 426 | def check_sha3(self, name, capacity, rate, suffix): |
| 427 | constructors = self.constructors_to_test[name] |
| 428 | for hash_object_constructor in constructors: |
| 429 | m = hash_object_constructor() |
Christian Heimes | 995b5d3 | 2019-09-13 15:31:19 +0200 | [diff] [blame] | 430 | if HASH is not None and isinstance(m, HASH): |
| 431 | # _hashopenssl's variant does not have extra SHA3 attributes |
| 432 | continue |
Christian Heimes | e370409 | 2016-09-23 11:32:30 +0200 | [diff] [blame] | 433 | self.assertEqual(capacity + rate, 1600) |
| 434 | self.assertEqual(m._capacity_bits, capacity) |
| 435 | self.assertEqual(m._rate_bits, rate) |
| 436 | self.assertEqual(m._suffix, suffix) |
| 437 | |
| 438 | @requires_sha3 |
| 439 | def test_extra_sha3(self): |
| 440 | self.check_sha3('sha3_224', 448, 1152, b'\x06') |
| 441 | self.check_sha3('sha3_256', 512, 1088, b'\x06') |
| 442 | self.check_sha3('sha3_384', 768, 832, b'\x06') |
| 443 | self.check_sha3('sha3_512', 1024, 576, b'\x06') |
| 444 | self.check_sha3('shake_128', 256, 1344, b'\x1f') |
| 445 | self.check_sha3('shake_256', 512, 1088, b'\x1f') |
| 446 | |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 447 | @requires_blake2 |
| 448 | def test_blocksize_name_blake2(self): |
| 449 | self.check_blocksize_name('blake2b', 128, 64) |
| 450 | self.check_blocksize_name('blake2s', 64, 32) |
| 451 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 452 | def test_case_md5_0(self): |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 453 | self.check( |
| 454 | 'md5', b'', 'd41d8cd98f00b204e9800998ecf8427e', |
| 455 | usedforsecurity=False |
| 456 | ) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 457 | |
| 458 | def test_case_md5_1(self): |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 459 | self.check( |
| 460 | 'md5', b'abc', '900150983cd24fb0d6963f7d28e17f72', |
| 461 | usedforsecurity=False |
| 462 | ) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 463 | |
| 464 | def test_case_md5_2(self): |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 465 | self.check( |
| 466 | 'md5', |
| 467 | b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', |
| 468 | 'd174ab98d277d9f5a5611c2c9f419d9f', |
| 469 | usedforsecurity=False |
| 470 | ) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 471 | |
Serhiy Storchaka | 4847e4e | 2014-01-10 13:37:54 +0200 | [diff] [blame] | 472 | @unittest.skipIf(sys.maxsize < _4G + 5, 'test cannot run on 32-bit systems') |
| 473 | @bigmemtest(size=_4G + 5, memuse=1, dry_run=False) |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 474 | def test_case_md5_huge(self, size): |
Serhiy Storchaka | 4847e4e | 2014-01-10 13:37:54 +0200 | [diff] [blame] | 475 | self.check('md5', b'A'*size, 'c9af2dff37468ce5dfee8f2cfc0a9c6d') |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 476 | |
Serhiy Storchaka | 4847e4e | 2014-01-10 13:37:54 +0200 | [diff] [blame] | 477 | @unittest.skipIf(sys.maxsize < _4G - 1, 'test cannot run on 32-bit systems') |
| 478 | @bigmemtest(size=_4G - 1, memuse=1, dry_run=False) |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 479 | def test_case_md5_uintmax(self, size): |
Serhiy Storchaka | 4847e4e | 2014-01-10 13:37:54 +0200 | [diff] [blame] | 480 | self.check('md5', b'A'*size, '28138d306ff1b8281f1a9067e1a1a2b3') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 481 | |
| 482 | # use the three examples from Federal Information Processing Standards |
| 483 | # Publication 180-1, Secure Hash Standard, 1995 April 17 |
| 484 | # http://www.itl.nist.gov/div897/pubs/fip180-1.htm |
| 485 | |
| 486 | def test_case_sha1_0(self): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 487 | self.check('sha1', b"", |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 488 | "da39a3ee5e6b4b0d3255bfef95601890afd80709") |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 489 | |
| 490 | def test_case_sha1_1(self): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 491 | self.check('sha1', b"abc", |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 492 | "a9993e364706816aba3e25717850c26c9cd0d89d") |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 493 | |
| 494 | def test_case_sha1_2(self): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 495 | self.check('sha1', |
| 496 | b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 497 | "84983e441c3bd26ebaae4aa1f95129e5e54670f1") |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 498 | |
| 499 | def test_case_sha1_3(self): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 500 | self.check('sha1', b"a" * 1000000, |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 501 | "34aa973cd4c4daa4f61eeb2bdbad27316534016f") |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 502 | |
| 503 | |
| 504 | # use the examples from Federal Information Processing Standards |
| 505 | # Publication 180-2, Secure Hash Standard, 2002 August 1 |
| 506 | # http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf |
| 507 | |
| 508 | def test_case_sha224_0(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 509 | self.check('sha224', b"", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 510 | "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f") |
| 511 | |
| 512 | def test_case_sha224_1(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 513 | self.check('sha224', b"abc", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 514 | "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7") |
| 515 | |
| 516 | def test_case_sha224_2(self): |
| 517 | self.check('sha224', |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 518 | b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 519 | "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525") |
| 520 | |
| 521 | def test_case_sha224_3(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 522 | self.check('sha224', b"a" * 1000000, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 523 | "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67") |
| 524 | |
| 525 | |
| 526 | def test_case_sha256_0(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 527 | self.check('sha256', b"", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 528 | "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") |
| 529 | |
| 530 | def test_case_sha256_1(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 531 | self.check('sha256', b"abc", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 532 | "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") |
| 533 | |
| 534 | def test_case_sha256_2(self): |
| 535 | self.check('sha256', |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 536 | b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 537 | "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1") |
| 538 | |
| 539 | def test_case_sha256_3(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 540 | self.check('sha256', b"a" * 1000000, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 541 | "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0") |
| 542 | |
| 543 | |
| 544 | def test_case_sha384_0(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 545 | self.check('sha384', b"", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 546 | "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da"+ |
| 547 | "274edebfe76f65fbd51ad2f14898b95b") |
| 548 | |
| 549 | def test_case_sha384_1(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 550 | self.check('sha384', b"abc", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 551 | "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed"+ |
| 552 | "8086072ba1e7cc2358baeca134c825a7") |
| 553 | |
| 554 | def test_case_sha384_2(self): |
| 555 | self.check('sha384', |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 556 | b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+ |
| 557 | b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 558 | "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712"+ |
| 559 | "fcc7c71a557e2db966c3e9fa91746039") |
| 560 | |
| 561 | def test_case_sha384_3(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 562 | self.check('sha384', b"a" * 1000000, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 563 | "9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b"+ |
| 564 | "07b8b3dc38ecc4ebae97ddd87f3d8985") |
| 565 | |
| 566 | |
| 567 | def test_case_sha512_0(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 568 | self.check('sha512', b"", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 569 | "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"+ |
| 570 | "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e") |
| 571 | |
| 572 | def test_case_sha512_1(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 573 | self.check('sha512', b"abc", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 574 | "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"+ |
| 575 | "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f") |
| 576 | |
| 577 | def test_case_sha512_2(self): |
| 578 | self.check('sha512', |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 579 | b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+ |
| 580 | b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 581 | "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"+ |
| 582 | "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909") |
| 583 | |
| 584 | def test_case_sha512_3(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 585 | self.check('sha512', b"a" * 1000000, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 586 | "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+ |
| 587 | "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b") |
| 588 | |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 589 | def check_blake2(self, constructor, salt_size, person_size, key_size, |
| 590 | digest_size, max_offset): |
| 591 | self.assertEqual(constructor.SALT_SIZE, salt_size) |
| 592 | for i in range(salt_size + 1): |
| 593 | constructor(salt=b'a' * i) |
| 594 | salt = b'a' * (salt_size + 1) |
| 595 | self.assertRaises(ValueError, constructor, salt=salt) |
| 596 | |
| 597 | self.assertEqual(constructor.PERSON_SIZE, person_size) |
| 598 | for i in range(person_size+1): |
| 599 | constructor(person=b'a' * i) |
| 600 | person = b'a' * (person_size + 1) |
| 601 | self.assertRaises(ValueError, constructor, person=person) |
| 602 | |
| 603 | self.assertEqual(constructor.MAX_DIGEST_SIZE, digest_size) |
| 604 | for i in range(1, digest_size + 1): |
| 605 | constructor(digest_size=i) |
| 606 | self.assertRaises(ValueError, constructor, digest_size=-1) |
| 607 | self.assertRaises(ValueError, constructor, digest_size=0) |
| 608 | self.assertRaises(ValueError, constructor, digest_size=digest_size+1) |
| 609 | |
| 610 | self.assertEqual(constructor.MAX_KEY_SIZE, key_size) |
| 611 | for i in range(key_size+1): |
| 612 | constructor(key=b'a' * i) |
| 613 | key = b'a' * (key_size + 1) |
| 614 | self.assertRaises(ValueError, constructor, key=key) |
| 615 | self.assertEqual(constructor().hexdigest(), |
| 616 | constructor(key=b'').hexdigest()) |
| 617 | |
| 618 | for i in range(0, 256): |
| 619 | constructor(fanout=i) |
| 620 | self.assertRaises(ValueError, constructor, fanout=-1) |
| 621 | self.assertRaises(ValueError, constructor, fanout=256) |
| 622 | |
| 623 | for i in range(1, 256): |
| 624 | constructor(depth=i) |
| 625 | self.assertRaises(ValueError, constructor, depth=-1) |
| 626 | self.assertRaises(ValueError, constructor, depth=0) |
| 627 | self.assertRaises(ValueError, constructor, depth=256) |
| 628 | |
| 629 | for i in range(0, 256): |
| 630 | constructor(node_depth=i) |
| 631 | self.assertRaises(ValueError, constructor, node_depth=-1) |
| 632 | self.assertRaises(ValueError, constructor, node_depth=256) |
| 633 | |
| 634 | for i in range(0, digest_size + 1): |
| 635 | constructor(inner_size=i) |
| 636 | self.assertRaises(ValueError, constructor, inner_size=-1) |
| 637 | self.assertRaises(ValueError, constructor, inner_size=digest_size+1) |
| 638 | |
| 639 | constructor(leaf_size=0) |
| 640 | constructor(leaf_size=(1<<32)-1) |
Serhiy Storchaka | 7cb7bcf | 2018-07-26 13:22:16 +0300 | [diff] [blame] | 641 | self.assertRaises(ValueError, constructor, leaf_size=-1) |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 642 | self.assertRaises(OverflowError, constructor, leaf_size=1<<32) |
| 643 | |
| 644 | constructor(node_offset=0) |
| 645 | constructor(node_offset=max_offset) |
Serhiy Storchaka | 7cb7bcf | 2018-07-26 13:22:16 +0300 | [diff] [blame] | 646 | self.assertRaises(ValueError, constructor, node_offset=-1) |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 647 | self.assertRaises(OverflowError, constructor, node_offset=max_offset+1) |
| 648 | |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 649 | self.assertRaises(TypeError, constructor, data=b'') |
| 650 | self.assertRaises(TypeError, constructor, string=b'') |
| 651 | self.assertRaises(TypeError, constructor, '') |
| 652 | |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 653 | constructor( |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 654 | b'', |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 655 | key=b'', |
| 656 | salt=b'', |
| 657 | person=b'', |
| 658 | digest_size=17, |
| 659 | fanout=1, |
| 660 | depth=1, |
| 661 | leaf_size=256, |
| 662 | node_offset=512, |
| 663 | node_depth=1, |
| 664 | inner_size=7, |
| 665 | last_node=True |
| 666 | ) |
| 667 | |
| 668 | def blake2_rfc7693(self, constructor, md_len, in_len): |
| 669 | def selftest_seq(length, seed): |
| 670 | mask = (1<<32)-1 |
| 671 | a = (0xDEAD4BAD * seed) & mask |
| 672 | b = 1 |
| 673 | out = bytearray(length) |
| 674 | for i in range(length): |
| 675 | t = (a + b) & mask |
| 676 | a, b = b, t |
| 677 | out[i] = (t >> 24) & 0xFF |
| 678 | return out |
| 679 | outer = constructor(digest_size=32) |
| 680 | for outlen in md_len: |
| 681 | for inlen in in_len: |
| 682 | indata = selftest_seq(inlen, inlen) |
| 683 | key = selftest_seq(outlen, outlen) |
| 684 | unkeyed = constructor(indata, digest_size=outlen) |
| 685 | outer.update(unkeyed.digest()) |
| 686 | keyed = constructor(indata, key=key, digest_size=outlen) |
| 687 | outer.update(keyed.digest()) |
| 688 | return outer.hexdigest() |
| 689 | |
| 690 | @requires_blake2 |
| 691 | def test_blake2b(self): |
| 692 | self.check_blake2(hashlib.blake2b, 16, 16, 64, 64, (1<<64)-1) |
| 693 | b2b_md_len = [20, 32, 48, 64] |
| 694 | b2b_in_len = [0, 3, 128, 129, 255, 1024] |
| 695 | self.assertEqual( |
| 696 | self.blake2_rfc7693(hashlib.blake2b, b2b_md_len, b2b_in_len), |
| 697 | "c23a7800d98123bd10f506c61e29da5603d763b8bbad2e737f5e765a7bccd475") |
| 698 | |
| 699 | @requires_blake2 |
| 700 | def test_case_blake2b_0(self): |
| 701 | self.check('blake2b', b"", |
| 702 | "786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419"+ |
| 703 | "d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce") |
| 704 | |
| 705 | @requires_blake2 |
| 706 | def test_case_blake2b_1(self): |
| 707 | self.check('blake2b', b"abc", |
| 708 | "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d1"+ |
| 709 | "7d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923") |
| 710 | |
| 711 | @requires_blake2 |
Jack O'Connor | dcfb0e3 | 2017-11-03 15:02:41 -0400 | [diff] [blame] | 712 | def test_case_blake2b_all_parameters(self): |
| 713 | # This checks that all the parameters work in general, and also that |
| 714 | # parameter byte order doesn't get confused on big endian platforms. |
| 715 | self.check('blake2b', b"foo", |
| 716 | "920568b0c5873b2f0ab67bedb6cf1b2b", |
| 717 | digest_size=16, |
| 718 | key=b"bar", |
| 719 | salt=b"baz", |
| 720 | person=b"bing", |
| 721 | fanout=2, |
| 722 | depth=3, |
| 723 | leaf_size=4, |
| 724 | node_offset=5, |
| 725 | node_depth=6, |
| 726 | inner_size=7, |
| 727 | last_node=True) |
| 728 | |
| 729 | @requires_blake2 |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 730 | def test_blake2b_vectors(self): |
| 731 | for msg, key, md in read_vectors('blake2b'): |
| 732 | key = bytes.fromhex(key) |
| 733 | self.check('blake2b', msg, md, key=key) |
| 734 | |
| 735 | @requires_blake2 |
| 736 | def test_blake2s(self): |
| 737 | self.check_blake2(hashlib.blake2s, 8, 8, 32, 32, (1<<48)-1) |
| 738 | b2s_md_len = [16, 20, 28, 32] |
| 739 | b2s_in_len = [0, 3, 64, 65, 255, 1024] |
| 740 | self.assertEqual( |
| 741 | self.blake2_rfc7693(hashlib.blake2s, b2s_md_len, b2s_in_len), |
| 742 | "6a411f08ce25adcdfb02aba641451cec53c598b24f4fc787fbdc88797f4c1dfe") |
| 743 | |
| 744 | @requires_blake2 |
| 745 | def test_case_blake2s_0(self): |
| 746 | self.check('blake2s', b"", |
| 747 | "69217a3079908094e11121d042354a7c1f55b6482ca1a51e1b250dfd1ed0eef9") |
| 748 | |
| 749 | @requires_blake2 |
| 750 | def test_case_blake2s_1(self): |
| 751 | self.check('blake2s', b"abc", |
| 752 | "508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982") |
| 753 | |
| 754 | @requires_blake2 |
Jack O'Connor | dcfb0e3 | 2017-11-03 15:02:41 -0400 | [diff] [blame] | 755 | def test_case_blake2s_all_parameters(self): |
| 756 | # This checks that all the parameters work in general, and also that |
| 757 | # parameter byte order doesn't get confused on big endian platforms. |
| 758 | self.check('blake2s', b"foo", |
| 759 | "bf2a8f7fe3c555012a6f8046e646bc75", |
| 760 | digest_size=16, |
| 761 | key=b"bar", |
| 762 | salt=b"baz", |
| 763 | person=b"bing", |
| 764 | fanout=2, |
| 765 | depth=3, |
| 766 | leaf_size=4, |
| 767 | node_offset=5, |
| 768 | node_depth=6, |
| 769 | inner_size=7, |
| 770 | last_node=True) |
| 771 | |
| 772 | @requires_blake2 |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 773 | def test_blake2s_vectors(self): |
| 774 | for msg, key, md in read_vectors('blake2s'): |
| 775 | key = bytes.fromhex(key) |
| 776 | self.check('blake2s', msg, md, key=key) |
| 777 | |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 778 | @requires_sha3 |
| 779 | def test_case_sha3_224_0(self): |
| 780 | self.check('sha3_224', b"", |
| 781 | "6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7") |
| 782 | |
| 783 | @requires_sha3 |
| 784 | def test_case_sha3_224_vector(self): |
| 785 | for msg, md in read_vectors('sha3_224'): |
| 786 | self.check('sha3_224', msg, md) |
| 787 | |
| 788 | @requires_sha3 |
| 789 | def test_case_sha3_256_0(self): |
| 790 | self.check('sha3_256', b"", |
| 791 | "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a") |
| 792 | |
| 793 | @requires_sha3 |
| 794 | def test_case_sha3_256_vector(self): |
| 795 | for msg, md in read_vectors('sha3_256'): |
| 796 | self.check('sha3_256', msg, md) |
| 797 | |
| 798 | @requires_sha3 |
| 799 | def test_case_sha3_384_0(self): |
| 800 | self.check('sha3_384', b"", |
| 801 | "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2a"+ |
| 802 | "c3713831264adb47fb6bd1e058d5f004") |
| 803 | |
| 804 | @requires_sha3 |
| 805 | def test_case_sha3_384_vector(self): |
| 806 | for msg, md in read_vectors('sha3_384'): |
| 807 | self.check('sha3_384', msg, md) |
| 808 | |
| 809 | @requires_sha3 |
| 810 | def test_case_sha3_512_0(self): |
| 811 | self.check('sha3_512', b"", |
| 812 | "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a6"+ |
| 813 | "15b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26") |
| 814 | |
| 815 | @requires_sha3 |
| 816 | def test_case_sha3_512_vector(self): |
| 817 | for msg, md in read_vectors('sha3_512'): |
| 818 | self.check('sha3_512', msg, md) |
| 819 | |
| 820 | @requires_sha3 |
| 821 | def test_case_shake_128_0(self): |
| 822 | self.check('shake_128', b"", |
| 823 | "7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26", |
| 824 | True) |
| 825 | self.check('shake_128', b"", "7f9c", True) |
| 826 | |
| 827 | @requires_sha3 |
| 828 | def test_case_shake128_vector(self): |
| 829 | for msg, md in read_vectors('shake_128'): |
| 830 | self.check('shake_128', msg, md, True) |
| 831 | |
| 832 | @requires_sha3 |
| 833 | def test_case_shake_256_0(self): |
| 834 | self.check('shake_256', b"", |
| 835 | "46b9dd2b0ba88d13233b3feb743eeb243fcd52ea62b81b82b50c27646ed5762f", |
| 836 | True) |
| 837 | self.check('shake_256', b"", "46b9", True) |
| 838 | |
| 839 | @requires_sha3 |
| 840 | def test_case_shake256_vector(self): |
| 841 | for msg, md in read_vectors('shake_256'): |
| 842 | self.check('shake_256', msg, md, True) |
| 843 | |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 844 | def test_gil(self): |
| 845 | # Check things work fine with an input larger than the size required |
| 846 | # for multithreaded operation (which is hardwired to 2048). |
| 847 | gil_minsize = 2048 |
| 848 | |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 849 | for cons in self.hash_constructors: |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 850 | m = cons(usedforsecurity=False) |
Christian Heimes | 4a0270d | 2012-10-06 02:23:36 +0200 | [diff] [blame] | 851 | m.update(b'1') |
| 852 | m.update(b'#' * gil_minsize) |
| 853 | m.update(b'1') |
| 854 | |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 855 | m = cons(b'x' * gil_minsize, usedforsecurity=False) |
Christian Heimes | 4a0270d | 2012-10-06 02:23:36 +0200 | [diff] [blame] | 856 | m.update(b'1') |
| 857 | |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 858 | m = hashlib.sha256() |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 859 | m.update(b'1') |
| 860 | m.update(b'#' * gil_minsize) |
| 861 | m.update(b'1') |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 862 | self.assertEqual( |
| 863 | m.hexdigest(), |
| 864 | '1cfceca95989f51f658e3f3ffe7f1cd43726c9e088c13ee10b46f57cef135b94' |
| 865 | ) |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 866 | |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 867 | m = hashlib.sha256(b'1' + b'#' * gil_minsize + b'1') |
| 868 | self.assertEqual( |
| 869 | m.hexdigest(), |
| 870 | '1cfceca95989f51f658e3f3ffe7f1cd43726c9e088c13ee10b46f57cef135b94' |
| 871 | ) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 872 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 873 | @support.reap_threads |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 874 | def test_threaded_hashing(self): |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 875 | # Updating the same hash object from several threads at once |
| 876 | # using data chunk sizes containing the same byte sequences. |
| 877 | # |
| 878 | # If the internal locks are working to prevent multiple |
| 879 | # updates on the same object from running at once, the resulting |
| 880 | # hash will be the same as doing it single threaded upfront. |
| 881 | hasher = hashlib.sha1() |
| 882 | num_threads = 5 |
| 883 | smallest_data = b'swineflu' |
Victor Stinner | 8dcf22f | 2017-09-14 08:43:22 -0700 | [diff] [blame] | 884 | data = smallest_data * 200000 |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 885 | expected_hash = hashlib.sha1(data*num_threads).hexdigest() |
| 886 | |
Victor Stinner | 8dcf22f | 2017-09-14 08:43:22 -0700 | [diff] [blame] | 887 | def hash_in_chunks(chunk_size): |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 888 | index = 0 |
| 889 | while index < len(data): |
Victor Stinner | 8dcf22f | 2017-09-14 08:43:22 -0700 | [diff] [blame] | 890 | hasher.update(data[index:index + chunk_size]) |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 891 | index += chunk_size |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 892 | |
Victor Stinner | 8dcf22f | 2017-09-14 08:43:22 -0700 | [diff] [blame] | 893 | threads = [] |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 894 | for threadnum in range(num_threads): |
Victor Stinner | 8dcf22f | 2017-09-14 08:43:22 -0700 | [diff] [blame] | 895 | chunk_size = len(data) // (10 ** threadnum) |
Gregory P. Smith | 914061a | 2013-08-05 13:14:37 -0700 | [diff] [blame] | 896 | self.assertGreater(chunk_size, 0) |
| 897 | self.assertEqual(chunk_size % len(smallest_data), 0) |
Victor Stinner | 8dcf22f | 2017-09-14 08:43:22 -0700 | [diff] [blame] | 898 | thread = threading.Thread(target=hash_in_chunks, |
| 899 | args=(chunk_size,)) |
| 900 | threads.append(thread) |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 901 | |
Victor Stinner | 8dcf22f | 2017-09-14 08:43:22 -0700 | [diff] [blame] | 902 | for thread in threads: |
| 903 | thread.start() |
| 904 | for thread in threads: |
| 905 | thread.join() |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 906 | |
| 907 | self.assertEqual(expected_hash, hasher.hexdigest()) |
| 908 | |
Victor Stinner | e3dfb9b | 2020-04-29 18:04:22 +0200 | [diff] [blame] | 909 | def test_get_fips_mode(self): |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 910 | fips_mode = self.is_fips_mode |
| 911 | if fips_mode is not None: |
| 912 | self.assertIsInstance(fips_mode, int) |
Victor Stinner | e3dfb9b | 2020-04-29 18:04:22 +0200 | [diff] [blame] | 913 | |
Christian Heimes | d5b3f6b | 2020-05-16 22:27:06 +0200 | [diff] [blame] | 914 | @unittest.skipUnless(HASH is not None, 'need _hashlib') |
| 915 | def test_internal_types(self): |
| 916 | # internal types like _hashlib.HASH are not constructable |
| 917 | with self.assertRaisesRegex( |
| 918 | TypeError, "cannot create 'HASH' instance" |
| 919 | ): |
| 920 | HASH() |
| 921 | with self.assertRaisesRegex( |
| 922 | TypeError, "cannot create 'HASHXOF' instance" |
| 923 | ): |
| 924 | HASHXOF() |
| 925 | |
Christian Heimes | 3626a50 | 2013-10-19 14:12:02 +0200 | [diff] [blame] | 926 | |
Christian Heimes | 0fbd94c | 2013-10-19 19:40:49 +0200 | [diff] [blame] | 927 | class KDFTests(unittest.TestCase): |
Christian Heimes | 3626a50 | 2013-10-19 14:12:02 +0200 | [diff] [blame] | 928 | |
Christian Heimes | e92ef13 | 2013-10-13 00:52:43 +0200 | [diff] [blame] | 929 | pbkdf2_test_vectors = [ |
| 930 | (b'password', b'salt', 1, None), |
| 931 | (b'password', b'salt', 2, None), |
| 932 | (b'password', b'salt', 4096, None), |
| 933 | # too slow, it takes over a minute on a fast CPU. |
| 934 | #(b'password', b'salt', 16777216, None), |
| 935 | (b'passwordPASSWORDpassword', b'saltSALTsaltSALTsaltSALTsaltSALTsalt', |
| 936 | 4096, -1), |
| 937 | (b'pass\0word', b'sa\0lt', 4096, 16), |
| 938 | ] |
| 939 | |
Christian Heimes | 39093e9 | 2016-09-06 20:22:28 +0200 | [diff] [blame] | 940 | scrypt_test_vectors = [ |
| 941 | (b'', b'', 16, 1, 1, unhexlify('77d6576238657b203b19ca42c18a0497f16b4844e3074ae8dfdffa3fede21442fcd0069ded0948f8326a753a0fc81f17e8d3e0fb2e0d3628cf35e20c38d18906')), |
| 942 | (b'password', b'NaCl', 1024, 8, 16, unhexlify('fdbabe1c9d3472007856e7190d01e9fe7c6ad7cbc8237830e77376634b3731622eaf30d92e22a3886ff109279d9830dac727afb94a83ee6d8360cbdfa2cc0640')), |
| 943 | (b'pleaseletmein', b'SodiumChloride', 16384, 8, 1, unhexlify('7023bdcb3afd7348461c06cd81fd38ebfda8fbba904f8e3ea9b543f6545da1f2d5432955613f0fcf62d49705242a9af9e61e85dc0d651e40dfcf017b45575887')), |
| 944 | ] |
| 945 | |
Christian Heimes | e92ef13 | 2013-10-13 00:52:43 +0200 | [diff] [blame] | 946 | pbkdf2_results = { |
| 947 | "sha1": [ |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 948 | # official test vectors from RFC 6070 |
Christian Heimes | e92ef13 | 2013-10-13 00:52:43 +0200 | [diff] [blame] | 949 | (bytes.fromhex('0c60c80f961f0e71f3a9b524af6012062fe037a6'), None), |
| 950 | (bytes.fromhex('ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957'), None), |
| 951 | (bytes.fromhex('4b007901b765489abead49d926f721d065a429c1'), None), |
| 952 | #(bytes.fromhex('eefe3d61cd4da4e4e9945b3d6ba2158c2634e984'), None), |
| 953 | (bytes.fromhex('3d2eec4fe41c849b80c8d83662c0e44a8b291a964c' |
| 954 | 'f2f07038'), 25), |
| 955 | (bytes.fromhex('56fa6aa75548099dcc37d7f03425e0c3'), None),], |
| 956 | "sha256": [ |
| 957 | (bytes.fromhex('120fb6cffcf8b32c43e7225256c4f837' |
| 958 | 'a86548c92ccc35480805987cb70be17b'), None), |
| 959 | (bytes.fromhex('ae4d0c95af6b46d32d0adff928f06dd0' |
| 960 | '2a303f8ef3c251dfd6e2d85a95474c43'), None), |
| 961 | (bytes.fromhex('c5e478d59288c841aa530db6845c4c8d' |
| 962 | '962893a001ce4e11a4963873aa98134a'), None), |
| 963 | #(bytes.fromhex('cf81c66fe8cfc04d1f31ecb65dab4089' |
| 964 | # 'f7f179e89b3b0bcb17ad10e3ac6eba46'), None), |
| 965 | (bytes.fromhex('348c89dbcbd32b2f32d814b8116e84cf2b17' |
| 966 | '347ebc1800181c4e2a1fb8dd53e1c635518c7dac47e9'), 40), |
| 967 | (bytes.fromhex('89b69d0516f829893c696226650a8687'), None),], |
| 968 | "sha512": [ |
| 969 | (bytes.fromhex('867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5' |
| 970 | 'd513554e1c8cf252c02d470a285a0501bad999bfe943c08f' |
| 971 | '050235d7d68b1da55e63f73b60a57fce'), None), |
| 972 | (bytes.fromhex('e1d9c16aa681708a45f5c7c4e215ceb66e011a2e9f004071' |
| 973 | '3f18aefdb866d53cf76cab2868a39b9f7840edce4fef5a82' |
| 974 | 'be67335c77a6068e04112754f27ccf4e'), None), |
| 975 | (bytes.fromhex('d197b1b33db0143e018b12f3d1d1479e6cdebdcc97c5c0f8' |
| 976 | '7f6902e072f457b5143f30602641b3d55cd335988cb36b84' |
| 977 | '376060ecd532e039b742a239434af2d5'), None), |
| 978 | (bytes.fromhex('8c0511f4c6e597c6ac6315d8f0362e225f3c501495ba23b8' |
| 979 | '68c005174dc4ee71115b59f9e60cd9532fa33e0f75aefe30' |
| 980 | '225c583a186cd82bd4daea9724a3d3b8'), 64), |
| 981 | (bytes.fromhex('9d9e9c4cd21fe4be24d5b8244c759665'), None),], |
| 982 | } |
| 983 | |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 984 | def _test_pbkdf2_hmac(self, pbkdf2, supported): |
Christian Heimes | e92ef13 | 2013-10-13 00:52:43 +0200 | [diff] [blame] | 985 | for digest_name, results in self.pbkdf2_results.items(): |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 986 | if digest_name not in supported: |
| 987 | continue |
Christian Heimes | e92ef13 | 2013-10-13 00:52:43 +0200 | [diff] [blame] | 988 | for i, vector in enumerate(self.pbkdf2_test_vectors): |
| 989 | password, salt, rounds, dklen = vector |
| 990 | expected, overwrite_dklen = results[i] |
| 991 | if overwrite_dklen: |
| 992 | dklen = overwrite_dklen |
| 993 | out = pbkdf2(digest_name, password, salt, rounds, dklen) |
| 994 | self.assertEqual(out, expected, |
| 995 | (digest_name, password, salt, rounds, dklen)) |
| 996 | out = pbkdf2(digest_name, memoryview(password), |
| 997 | memoryview(salt), rounds, dklen) |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 998 | self.assertEqual(out, expected) |
Christian Heimes | e92ef13 | 2013-10-13 00:52:43 +0200 | [diff] [blame] | 999 | out = pbkdf2(digest_name, bytearray(password), |
| 1000 | bytearray(salt), rounds, dklen) |
| 1001 | self.assertEqual(out, expected) |
| 1002 | if dklen is None: |
| 1003 | out = pbkdf2(digest_name, password, salt, rounds) |
| 1004 | self.assertEqual(out, expected, |
| 1005 | (digest_name, password, salt, rounds)) |
| 1006 | |
| 1007 | self.assertRaises(TypeError, pbkdf2, b'sha1', b'pass', b'salt', 1) |
| 1008 | self.assertRaises(TypeError, pbkdf2, 'sha1', 'pass', 'salt', 1) |
| 1009 | self.assertRaises(ValueError, pbkdf2, 'sha1', b'pass', b'salt', 0) |
| 1010 | self.assertRaises(ValueError, pbkdf2, 'sha1', b'pass', b'salt', -1) |
| 1011 | self.assertRaises(ValueError, pbkdf2, 'sha1', b'pass', b'salt', 1, 0) |
| 1012 | self.assertRaises(ValueError, pbkdf2, 'sha1', b'pass', b'salt', 1, -1) |
| 1013 | with self.assertRaisesRegex(ValueError, 'unsupported hash type'): |
| 1014 | pbkdf2('unknown', b'pass', b'salt', 1) |
Martin Panter | bc85e35 | 2016-02-22 09:21:49 +0000 | [diff] [blame] | 1015 | out = pbkdf2(hash_name='sha1', password=b'password', salt=b'salt', |
| 1016 | iterations=1, dklen=None) |
| 1017 | self.assertEqual(out, self.pbkdf2_results['sha1'][0][0]) |
Christian Heimes | e92ef13 | 2013-10-13 00:52:43 +0200 | [diff] [blame] | 1018 | |
Christian Heimes | 0fbd94c | 2013-10-19 19:40:49 +0200 | [diff] [blame] | 1019 | def test_pbkdf2_hmac_py(self): |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 1020 | self._test_pbkdf2_hmac(py_hashlib.pbkdf2_hmac, builtin_hashes) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 1021 | |
Christian Heimes | 0fbd94c | 2013-10-19 19:40:49 +0200 | [diff] [blame] | 1022 | @unittest.skipUnless(hasattr(c_hashlib, 'pbkdf2_hmac'), |
| 1023 | ' test requires OpenSSL > 1.0') |
| 1024 | def test_pbkdf2_hmac_c(self): |
Christian Heimes | 909b571 | 2020-05-22 20:04:33 +0200 | [diff] [blame^] | 1025 | self._test_pbkdf2_hmac(c_hashlib.pbkdf2_hmac, openssl_md_meth_names) |
Christian Heimes | 3626a50 | 2013-10-19 14:12:02 +0200 | [diff] [blame] | 1026 | |
| 1027 | |
Christian Heimes | 39093e9 | 2016-09-06 20:22:28 +0200 | [diff] [blame] | 1028 | @unittest.skipUnless(hasattr(c_hashlib, 'scrypt'), |
| 1029 | ' test requires OpenSSL > 1.1') |
| 1030 | def test_scrypt(self): |
| 1031 | for password, salt, n, r, p, expected in self.scrypt_test_vectors: |
| 1032 | result = hashlib.scrypt(password, salt=salt, n=n, r=r, p=p) |
| 1033 | self.assertEqual(result, expected) |
| 1034 | |
| 1035 | # this values should work |
| 1036 | hashlib.scrypt(b'password', salt=b'salt', n=2, r=8, p=1) |
| 1037 | # password and salt must be bytes-like |
| 1038 | with self.assertRaises(TypeError): |
| 1039 | hashlib.scrypt('password', salt=b'salt', n=2, r=8, p=1) |
| 1040 | with self.assertRaises(TypeError): |
| 1041 | hashlib.scrypt(b'password', salt='salt', n=2, r=8, p=1) |
| 1042 | # require keyword args |
| 1043 | with self.assertRaises(TypeError): |
| 1044 | hashlib.scrypt(b'password') |
| 1045 | with self.assertRaises(TypeError): |
| 1046 | hashlib.scrypt(b'password', b'salt') |
| 1047 | with self.assertRaises(TypeError): |
| 1048 | hashlib.scrypt(b'password', 2, 8, 1, salt=b'salt') |
| 1049 | for n in [-1, 0, 1, None]: |
| 1050 | with self.assertRaises((ValueError, OverflowError, TypeError)): |
| 1051 | hashlib.scrypt(b'password', salt=b'salt', n=n, r=8, p=1) |
| 1052 | for r in [-1, 0, None]: |
| 1053 | with self.assertRaises((ValueError, OverflowError, TypeError)): |
| 1054 | hashlib.scrypt(b'password', salt=b'salt', n=2, r=r, p=1) |
| 1055 | for p in [-1, 0, None]: |
| 1056 | with self.assertRaises((ValueError, OverflowError, TypeError)): |
| 1057 | hashlib.scrypt(b'password', salt=b'salt', n=2, r=8, p=p) |
| 1058 | for maxmem in [-1, None]: |
| 1059 | with self.assertRaises((ValueError, OverflowError, TypeError)): |
| 1060 | hashlib.scrypt(b'password', salt=b'salt', n=2, r=8, p=1, |
| 1061 | maxmem=maxmem) |
| 1062 | for dklen in [-1, None]: |
| 1063 | with self.assertRaises((ValueError, OverflowError, TypeError)): |
| 1064 | hashlib.scrypt(b'password', salt=b'salt', n=2, r=8, p=1, |
| 1065 | dklen=dklen) |
| 1066 | |
Christian Heimes | 995b5d3 | 2019-09-13 15:31:19 +0200 | [diff] [blame] | 1067 | def test_normalized_name(self): |
| 1068 | self.assertNotIn("blake2b512", hashlib.algorithms_available) |
| 1069 | self.assertNotIn("sha3-512", hashlib.algorithms_available) |
| 1070 | |
Christian Heimes | 39093e9 | 2016-09-06 20:22:28 +0200 | [diff] [blame] | 1071 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 1072 | if __name__ == "__main__": |
Brett Cannon | 3e9a9ae | 2013-06-12 21:25:59 -0400 | [diff] [blame] | 1073 | unittest.main() |