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