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 |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 10 | import hashlib |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 11 | import itertools |
Antoine Pitrou | 019ff19 | 2012-05-16 16:41:26 +0200 | [diff] [blame] | 12 | import os |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 13 | import sys |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 14 | try: |
| 15 | import threading |
| 16 | except ImportError: |
| 17 | threading = None |
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 |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 22 | |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 23 | # Were we compiled --with-pydebug or with #define Py_DEBUG? |
| 24 | COMPILED_WITH_PYDEBUG = hasattr(sys, 'gettotalrefcount') |
| 25 | |
Christian Heimes | 3626a50 | 2013-10-19 14:12:02 +0200 | [diff] [blame] | 26 | c_hashlib = import_fresh_module('hashlib', fresh=['_hashlib']) |
| 27 | py_hashlib = import_fresh_module('hashlib', blocked=['_hashlib']) |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 28 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 29 | def hexstr(s): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 30 | assert isinstance(s, bytes), repr(s) |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 31 | h = "0123456789abcdef" |
| 32 | r = '' |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 33 | for i in s: |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 34 | r += h[(i >> 4) & 0xF] + h[i & 0xF] |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 35 | return r |
| 36 | |
| 37 | |
| 38 | class HashLibTestCase(unittest.TestCase): |
| 39 | supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1', |
| 40 | 'sha224', 'SHA224', 'sha256', 'SHA256', |
Martin v. Löwis | 24e4330 | 2014-01-03 14:05:06 +0100 | [diff] [blame] | 41 | 'sha384', 'SHA384', 'sha512', 'SHA512') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 42 | |
Antoine Pitrou | 019ff19 | 2012-05-16 16:41:26 +0200 | [diff] [blame] | 43 | # Issue #14693: fallback modules are always compiled under POSIX |
| 44 | _warn_on_extension_import = os.name == 'posix' or COMPILED_WITH_PYDEBUG |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 45 | |
| 46 | def _conditional_import_module(self, module_name): |
| 47 | """Import a module and return a reference to it or None on failure.""" |
| 48 | try: |
| 49 | exec('import '+module_name) |
| 50 | except ImportError as error: |
| 51 | if self._warn_on_extension_import: |
| 52 | warnings.warn('Did a C extension fail to compile? %s' % error) |
| 53 | return locals().get(module_name) |
| 54 | |
| 55 | def __init__(self, *args, **kwargs): |
| 56 | algorithms = set() |
| 57 | for algorithm in self.supported_hash_names: |
| 58 | algorithms.add(algorithm.lower()) |
| 59 | self.constructors_to_test = {} |
| 60 | for algorithm in algorithms: |
| 61 | self.constructors_to_test[algorithm] = set() |
| 62 | |
| 63 | # For each algorithm, test the direct constructor and the use |
| 64 | # of hashlib.new given the algorithm name. |
| 65 | for algorithm, constructors in self.constructors_to_test.items(): |
| 66 | constructors.add(getattr(hashlib, algorithm)) |
| 67 | def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm): |
| 68 | if data is None: |
| 69 | return hashlib.new(_alg) |
| 70 | return hashlib.new(_alg, data) |
| 71 | constructors.add(_test_algorithm_via_hashlib_new) |
| 72 | |
| 73 | _hashlib = self._conditional_import_module('_hashlib') |
| 74 | if _hashlib: |
| 75 | # These two algorithms should always be present when this module |
| 76 | # is compiled. If not, something was compiled wrong. |
Gregory P. Smith | 914061a | 2013-08-05 13:14:37 -0700 | [diff] [blame] | 77 | self.assertTrue(hasattr(_hashlib, 'openssl_md5')) |
| 78 | self.assertTrue(hasattr(_hashlib, 'openssl_sha1')) |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 79 | for algorithm, constructors in self.constructors_to_test.items(): |
| 80 | constructor = getattr(_hashlib, 'openssl_'+algorithm, None) |
| 81 | if constructor: |
| 82 | constructors.add(constructor) |
| 83 | |
Christian Heimes | e535107 | 2013-10-22 14:59:12 +0200 | [diff] [blame] | 84 | def add_builtin_constructor(name): |
| 85 | constructor = getattr(hashlib, "__get_builtin_constructor")(name) |
| 86 | self.constructors_to_test[name].add(constructor) |
| 87 | |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 88 | _md5 = self._conditional_import_module('_md5') |
| 89 | if _md5: |
Christian Heimes | e535107 | 2013-10-22 14:59:12 +0200 | [diff] [blame] | 90 | add_builtin_constructor('md5') |
Gregory P. Smith | b04ded4 | 2010-01-03 00:38:10 +0000 | [diff] [blame] | 91 | _sha1 = self._conditional_import_module('_sha1') |
| 92 | if _sha1: |
Christian Heimes | e535107 | 2013-10-22 14:59:12 +0200 | [diff] [blame] | 93 | add_builtin_constructor('sha1') |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 94 | _sha256 = self._conditional_import_module('_sha256') |
| 95 | if _sha256: |
Christian Heimes | e535107 | 2013-10-22 14:59:12 +0200 | [diff] [blame] | 96 | add_builtin_constructor('sha224') |
| 97 | add_builtin_constructor('sha256') |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 98 | _sha512 = self._conditional_import_module('_sha512') |
| 99 | if _sha512: |
Christian Heimes | e535107 | 2013-10-22 14:59:12 +0200 | [diff] [blame] | 100 | add_builtin_constructor('sha384') |
| 101 | add_builtin_constructor('sha512') |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 102 | |
| 103 | super(HashLibTestCase, self).__init__(*args, **kwargs) |
| 104 | |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 105 | @property |
| 106 | def hash_constructors(self): |
| 107 | constructors = self.constructors_to_test.values() |
| 108 | return itertools.chain.from_iterable(constructors) |
| 109 | |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 110 | def test_hash_array(self): |
| 111 | a = array.array("b", range(10)) |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 112 | for cons in self.hash_constructors: |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 113 | c = cons(a) |
| 114 | c.hexdigest() |
| 115 | |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 116 | def test_algorithms_guaranteed(self): |
| 117 | self.assertEqual(hashlib.algorithms_guaranteed, |
Raymond Hettinger | bf1d2bc | 2011-01-24 04:52:27 +0000 | [diff] [blame] | 118 | set(_algo for _algo in self.supported_hash_names |
Gregory P. Smith | 86508cc | 2010-03-01 02:05:26 +0000 | [diff] [blame] | 119 | if _algo.islower())) |
| 120 | |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 121 | def test_algorithms_available(self): |
| 122 | self.assertTrue(set(hashlib.algorithms_guaranteed). |
| 123 | issubset(hashlib.algorithms_available)) |
| 124 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 125 | def test_unknown_hash(self): |
Amaury Forgeot d'Arc | 3a3dc17 | 2012-06-29 01:53:13 +0200 | [diff] [blame] | 126 | self.assertRaises(ValueError, hashlib.new, 'spam spam spam spam spam') |
| 127 | self.assertRaises(TypeError, hashlib.new, 1) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 128 | |
Gregory P. Smith | 12c9d02 | 2011-05-14 15:15:49 -0700 | [diff] [blame] | 129 | def test_get_builtin_constructor(self): |
Christian Heimes | e535107 | 2013-10-22 14:59:12 +0200 | [diff] [blame] | 130 | get_builtin_constructor = getattr(hashlib, |
| 131 | '__get_builtin_constructor') |
| 132 | builtin_constructor_cache = getattr(hashlib, |
| 133 | '__builtin_constructor_cache') |
Gregory P. Smith | 12c9d02 | 2011-05-14 15:15:49 -0700 | [diff] [blame] | 134 | self.assertRaises(ValueError, get_builtin_constructor, 'test') |
| 135 | try: |
| 136 | import _md5 |
| 137 | except ImportError: |
| 138 | pass |
| 139 | # This forces an ImportError for "import _md5" statements |
| 140 | sys.modules['_md5'] = None |
Christian Heimes | e535107 | 2013-10-22 14:59:12 +0200 | [diff] [blame] | 141 | # clear the cache |
| 142 | builtin_constructor_cache.clear() |
Gregory P. Smith | 12c9d02 | 2011-05-14 15:15:49 -0700 | [diff] [blame] | 143 | try: |
| 144 | self.assertRaises(ValueError, get_builtin_constructor, 'md5') |
| 145 | finally: |
| 146 | if '_md5' in locals(): |
| 147 | sys.modules['_md5'] = _md5 |
| 148 | else: |
| 149 | del sys.modules['_md5'] |
Gregory P. Smith | 76c28f7 | 2012-07-21 21:19:53 -0700 | [diff] [blame] | 150 | self.assertRaises(TypeError, get_builtin_constructor, 3) |
Christian Heimes | e535107 | 2013-10-22 14:59:12 +0200 | [diff] [blame] | 151 | constructor = get_builtin_constructor('md5') |
| 152 | self.assertIs(constructor, _md5.md5) |
| 153 | self.assertEqual(sorted(builtin_constructor_cache), ['MD5', 'md5']) |
Gregory P. Smith | 12c9d02 | 2011-05-14 15:15:49 -0700 | [diff] [blame] | 154 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 155 | def test_hexdigest(self): |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 156 | for cons in self.hash_constructors: |
| 157 | h = cons() |
Gregory P. Smith | 914061a | 2013-08-05 13:14:37 -0700 | [diff] [blame] | 158 | self.assertIsInstance(h.digest(), bytes) |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 159 | self.assertEqual(hexstr(h.digest()), h.hexdigest()) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 160 | |
Jason R. Coombs | b2aa6f4 | 2013-08-03 11:39:39 +0200 | [diff] [blame] | 161 | def test_name_attribute(self): |
| 162 | for cons in self.hash_constructors: |
| 163 | h = cons() |
Gregory P. Smith | 914061a | 2013-08-05 13:14:37 -0700 | [diff] [blame] | 164 | self.assertIsInstance(h.name, str) |
| 165 | self.assertIn(h.name, self.supported_hash_names) |
| 166 | self.assertEqual(h.name, hashlib.new(h.name).name) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 167 | |
| 168 | def test_large_update(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 169 | aas = b'a' * 128 |
| 170 | bees = b'b' * 127 |
| 171 | cees = b'c' * 126 |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 172 | dees = b'd' * 2048 # HASHLIB_GIL_MINSIZE |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 173 | |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 174 | for cons in self.hash_constructors: |
| 175 | m1 = cons() |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 176 | m1.update(aas) |
| 177 | m1.update(bees) |
| 178 | m1.update(cees) |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 179 | m1.update(dees) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 180 | |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 181 | m2 = cons() |
| 182 | m2.update(aas + bees + cees + dees) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 183 | self.assertEqual(m1.digest(), m2.digest()) |
| 184 | |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 185 | m3 = cons(aas + bees + cees + dees) |
| 186 | self.assertEqual(m1.digest(), m3.digest()) |
| 187 | |
| 188 | # verify copy() doesn't touch original |
| 189 | m4 = cons(aas + bees + cees) |
| 190 | m4_digest = m4.digest() |
| 191 | m4_copy = m4.copy() |
| 192 | m4_copy.update(dees) |
| 193 | self.assertEqual(m1.digest(), m4_copy.digest()) |
| 194 | self.assertEqual(m4.digest(), m4_digest) |
| 195 | |
| 196 | def check(self, name, data, hexdigest): |
| 197 | hexdigest = hexdigest.lower() |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 198 | constructors = self.constructors_to_test[name] |
| 199 | # 2 is for hashlib.name(...) and hashlib.new(name, ...) |
| 200 | self.assertGreaterEqual(len(constructors), 2) |
| 201 | for hash_object_constructor in constructors: |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 202 | m = hash_object_constructor(data) |
| 203 | computed = m.hexdigest() |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 204 | self.assertEqual( |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 205 | computed, hexdigest, |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 206 | "Hash algorithm %s constructed using %s returned hexdigest" |
| 207 | " %r for %d byte input data that should have hashed to %r." |
| 208 | % (name, hash_object_constructor, |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 209 | computed, len(data), hexdigest)) |
| 210 | computed = m.digest() |
| 211 | digest = bytes.fromhex(hexdigest) |
| 212 | self.assertEqual(computed, digest) |
| 213 | self.assertEqual(len(digest), m.digest_size) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 214 | |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 215 | def check_no_unicode(self, algorithm_name): |
| 216 | # Unicode objects are not allowed as input. |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 217 | constructors = self.constructors_to_test[algorithm_name] |
| 218 | for hash_object_constructor in constructors: |
| 219 | self.assertRaises(TypeError, hash_object_constructor, 'spam') |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 220 | |
| 221 | def test_no_unicode(self): |
| 222 | self.check_no_unicode('md5') |
| 223 | self.check_no_unicode('sha1') |
| 224 | self.check_no_unicode('sha224') |
| 225 | self.check_no_unicode('sha256') |
| 226 | self.check_no_unicode('sha384') |
| 227 | self.check_no_unicode('sha512') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 228 | |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 229 | def check_blocksize_name(self, name, block_size=0, digest_size=0): |
| 230 | constructors = self.constructors_to_test[name] |
| 231 | for hash_object_constructor in constructors: |
| 232 | m = hash_object_constructor() |
| 233 | self.assertEqual(m.block_size, block_size) |
| 234 | self.assertEqual(m.digest_size, digest_size) |
| 235 | self.assertEqual(len(m.digest()), digest_size) |
Christian Heimes | 37d5ceb | 2013-08-15 18:31:48 +0200 | [diff] [blame] | 236 | self.assertEqual(m.name, name) |
Christian Heimes | d49a371 | 2013-07-30 15:35:54 +0200 | [diff] [blame] | 237 | # split for sha3_512 / _sha3.sha3 object |
Christian Heimes | 37d5ceb | 2013-08-15 18:31:48 +0200 | [diff] [blame] | 238 | self.assertIn(name.split("_")[0], repr(m)) |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 239 | |
| 240 | def test_blocksize_name(self): |
| 241 | self.check_blocksize_name('md5', 64, 16) |
| 242 | self.check_blocksize_name('sha1', 64, 20) |
| 243 | self.check_blocksize_name('sha224', 64, 28) |
| 244 | self.check_blocksize_name('sha256', 64, 32) |
| 245 | self.check_blocksize_name('sha384', 128, 48) |
| 246 | self.check_blocksize_name('sha512', 128, 64) |
| 247 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 248 | def test_case_md5_0(self): |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 249 | self.check('md5', b'', 'd41d8cd98f00b204e9800998ecf8427e') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 250 | |
| 251 | def test_case_md5_1(self): |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 252 | self.check('md5', b'abc', '900150983cd24fb0d6963f7d28e17f72') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 253 | |
| 254 | def test_case_md5_2(self): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 255 | self.check('md5', |
| 256 | b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 257 | 'd174ab98d277d9f5a5611c2c9f419d9f') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 258 | |
Serhiy Storchaka | 4847e4e | 2014-01-10 13:37:54 +0200 | [diff] [blame] | 259 | @unittest.skipIf(sys.maxsize < _4G + 5, 'test cannot run on 32-bit systems') |
| 260 | @bigmemtest(size=_4G + 5, memuse=1, dry_run=False) |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 261 | def test_case_md5_huge(self, size): |
Serhiy Storchaka | 4847e4e | 2014-01-10 13:37:54 +0200 | [diff] [blame] | 262 | self.check('md5', b'A'*size, 'c9af2dff37468ce5dfee8f2cfc0a9c6d') |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 263 | |
Serhiy Storchaka | 4847e4e | 2014-01-10 13:37:54 +0200 | [diff] [blame] | 264 | @unittest.skipIf(sys.maxsize < _4G - 1, 'test cannot run on 32-bit systems') |
| 265 | @bigmemtest(size=_4G - 1, memuse=1, dry_run=False) |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 266 | def test_case_md5_uintmax(self, size): |
Serhiy Storchaka | 4847e4e | 2014-01-10 13:37:54 +0200 | [diff] [blame] | 267 | self.check('md5', b'A'*size, '28138d306ff1b8281f1a9067e1a1a2b3') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 268 | |
| 269 | # use the three examples from Federal Information Processing Standards |
| 270 | # Publication 180-1, Secure Hash Standard, 1995 April 17 |
| 271 | # http://www.itl.nist.gov/div897/pubs/fip180-1.htm |
| 272 | |
| 273 | def test_case_sha1_0(self): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 274 | self.check('sha1', b"", |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 275 | "da39a3ee5e6b4b0d3255bfef95601890afd80709") |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 276 | |
| 277 | def test_case_sha1_1(self): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 278 | self.check('sha1', b"abc", |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 279 | "a9993e364706816aba3e25717850c26c9cd0d89d") |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 280 | |
| 281 | def test_case_sha1_2(self): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 282 | self.check('sha1', |
| 283 | b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 284 | "84983e441c3bd26ebaae4aa1f95129e5e54670f1") |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 285 | |
| 286 | def test_case_sha1_3(self): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 287 | self.check('sha1', b"a" * 1000000, |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 288 | "34aa973cd4c4daa4f61eeb2bdbad27316534016f") |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 289 | |
| 290 | |
| 291 | # use the examples from Federal Information Processing Standards |
| 292 | # Publication 180-2, Secure Hash Standard, 2002 August 1 |
| 293 | # http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf |
| 294 | |
| 295 | def test_case_sha224_0(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 296 | self.check('sha224', b"", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 297 | "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f") |
| 298 | |
| 299 | def test_case_sha224_1(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 300 | self.check('sha224', b"abc", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 301 | "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7") |
| 302 | |
| 303 | def test_case_sha224_2(self): |
| 304 | self.check('sha224', |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 305 | b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 306 | "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525") |
| 307 | |
| 308 | def test_case_sha224_3(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 309 | self.check('sha224', b"a" * 1000000, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 310 | "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67") |
| 311 | |
| 312 | |
| 313 | def test_case_sha256_0(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 314 | self.check('sha256', b"", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 315 | "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") |
| 316 | |
| 317 | def test_case_sha256_1(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 318 | self.check('sha256', b"abc", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 319 | "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") |
| 320 | |
| 321 | def test_case_sha256_2(self): |
| 322 | self.check('sha256', |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 323 | b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 324 | "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1") |
| 325 | |
| 326 | def test_case_sha256_3(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 327 | self.check('sha256', b"a" * 1000000, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 328 | "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0") |
| 329 | |
| 330 | |
| 331 | def test_case_sha384_0(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 332 | self.check('sha384', b"", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 333 | "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da"+ |
| 334 | "274edebfe76f65fbd51ad2f14898b95b") |
| 335 | |
| 336 | def test_case_sha384_1(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 337 | self.check('sha384', b"abc", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 338 | "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed"+ |
| 339 | "8086072ba1e7cc2358baeca134c825a7") |
| 340 | |
| 341 | def test_case_sha384_2(self): |
| 342 | self.check('sha384', |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 343 | b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+ |
| 344 | b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 345 | "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712"+ |
| 346 | "fcc7c71a557e2db966c3e9fa91746039") |
| 347 | |
| 348 | def test_case_sha384_3(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 349 | self.check('sha384', b"a" * 1000000, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 350 | "9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b"+ |
| 351 | "07b8b3dc38ecc4ebae97ddd87f3d8985") |
| 352 | |
| 353 | |
| 354 | def test_case_sha512_0(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 355 | self.check('sha512', b"", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 356 | "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"+ |
| 357 | "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e") |
| 358 | |
| 359 | def test_case_sha512_1(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 360 | self.check('sha512', b"abc", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 361 | "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"+ |
| 362 | "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f") |
| 363 | |
| 364 | def test_case_sha512_2(self): |
| 365 | self.check('sha512', |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 366 | b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+ |
| 367 | b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 368 | "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"+ |
| 369 | "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909") |
| 370 | |
| 371 | def test_case_sha512_3(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 372 | self.check('sha512', b"a" * 1000000, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 373 | "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+ |
| 374 | "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b") |
| 375 | |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 376 | def test_gil(self): |
| 377 | # Check things work fine with an input larger than the size required |
| 378 | # for multithreaded operation (which is hardwired to 2048). |
| 379 | gil_minsize = 2048 |
| 380 | |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 381 | for cons in self.hash_constructors: |
| 382 | m = cons() |
Christian Heimes | 4a0270d | 2012-10-06 02:23:36 +0200 | [diff] [blame] | 383 | m.update(b'1') |
| 384 | m.update(b'#' * gil_minsize) |
| 385 | m.update(b'1') |
| 386 | |
Christian Heimes | 65aa573 | 2013-07-30 15:33:30 +0200 | [diff] [blame] | 387 | m = cons(b'x' * gil_minsize) |
Christian Heimes | 4a0270d | 2012-10-06 02:23:36 +0200 | [diff] [blame] | 388 | m.update(b'1') |
| 389 | |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 390 | m = hashlib.md5() |
| 391 | m.update(b'1') |
| 392 | m.update(b'#' * gil_minsize) |
| 393 | m.update(b'1') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 394 | self.assertEqual(m.hexdigest(), 'cb1e1a2cbc80be75e19935d621fb9b21') |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 395 | |
| 396 | m = hashlib.md5(b'x' * gil_minsize) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 397 | self.assertEqual(m.hexdigest(), 'cfb767f225d58469c5de3632a8803958') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 398 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 399 | @unittest.skipUnless(threading, 'Threading required for this test.') |
| 400 | @support.reap_threads |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 401 | def test_threaded_hashing(self): |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 402 | # Updating the same hash object from several threads at once |
| 403 | # using data chunk sizes containing the same byte sequences. |
| 404 | # |
| 405 | # If the internal locks are working to prevent multiple |
| 406 | # updates on the same object from running at once, the resulting |
| 407 | # hash will be the same as doing it single threaded upfront. |
| 408 | hasher = hashlib.sha1() |
| 409 | num_threads = 5 |
| 410 | smallest_data = b'swineflu' |
| 411 | data = smallest_data*200000 |
| 412 | expected_hash = hashlib.sha1(data*num_threads).hexdigest() |
| 413 | |
| 414 | def hash_in_chunks(chunk_size, event): |
| 415 | index = 0 |
| 416 | while index < len(data): |
| 417 | hasher.update(data[index:index+chunk_size]) |
| 418 | index += chunk_size |
| 419 | event.set() |
| 420 | |
| 421 | events = [] |
| 422 | for threadnum in range(num_threads): |
| 423 | chunk_size = len(data) // (10**threadnum) |
Gregory P. Smith | 914061a | 2013-08-05 13:14:37 -0700 | [diff] [blame] | 424 | self.assertGreater(chunk_size, 0) |
| 425 | self.assertEqual(chunk_size % len(smallest_data), 0) |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 426 | event = threading.Event() |
| 427 | events.append(event) |
| 428 | threading.Thread(target=hash_in_chunks, |
| 429 | args=(chunk_size, event)).start() |
| 430 | |
| 431 | for event in events: |
| 432 | event.wait() |
| 433 | |
| 434 | self.assertEqual(expected_hash, hasher.hexdigest()) |
| 435 | |
Christian Heimes | 3626a50 | 2013-10-19 14:12:02 +0200 | [diff] [blame] | 436 | |
Christian Heimes | 0fbd94c | 2013-10-19 19:40:49 +0200 | [diff] [blame] | 437 | class KDFTests(unittest.TestCase): |
Christian Heimes | 3626a50 | 2013-10-19 14:12:02 +0200 | [diff] [blame] | 438 | |
Christian Heimes | e92ef13 | 2013-10-13 00:52:43 +0200 | [diff] [blame] | 439 | pbkdf2_test_vectors = [ |
| 440 | (b'password', b'salt', 1, None), |
| 441 | (b'password', b'salt', 2, None), |
| 442 | (b'password', b'salt', 4096, None), |
| 443 | # too slow, it takes over a minute on a fast CPU. |
| 444 | #(b'password', b'salt', 16777216, None), |
| 445 | (b'passwordPASSWORDpassword', b'saltSALTsaltSALTsaltSALTsaltSALTsalt', |
| 446 | 4096, -1), |
| 447 | (b'pass\0word', b'sa\0lt', 4096, 16), |
| 448 | ] |
| 449 | |
| 450 | pbkdf2_results = { |
| 451 | "sha1": [ |
| 452 | # offical test vectors from RFC 6070 |
| 453 | (bytes.fromhex('0c60c80f961f0e71f3a9b524af6012062fe037a6'), None), |
| 454 | (bytes.fromhex('ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957'), None), |
| 455 | (bytes.fromhex('4b007901b765489abead49d926f721d065a429c1'), None), |
| 456 | #(bytes.fromhex('eefe3d61cd4da4e4e9945b3d6ba2158c2634e984'), None), |
| 457 | (bytes.fromhex('3d2eec4fe41c849b80c8d83662c0e44a8b291a964c' |
| 458 | 'f2f07038'), 25), |
| 459 | (bytes.fromhex('56fa6aa75548099dcc37d7f03425e0c3'), None),], |
| 460 | "sha256": [ |
| 461 | (bytes.fromhex('120fb6cffcf8b32c43e7225256c4f837' |
| 462 | 'a86548c92ccc35480805987cb70be17b'), None), |
| 463 | (bytes.fromhex('ae4d0c95af6b46d32d0adff928f06dd0' |
| 464 | '2a303f8ef3c251dfd6e2d85a95474c43'), None), |
| 465 | (bytes.fromhex('c5e478d59288c841aa530db6845c4c8d' |
| 466 | '962893a001ce4e11a4963873aa98134a'), None), |
| 467 | #(bytes.fromhex('cf81c66fe8cfc04d1f31ecb65dab4089' |
| 468 | # 'f7f179e89b3b0bcb17ad10e3ac6eba46'), None), |
| 469 | (bytes.fromhex('348c89dbcbd32b2f32d814b8116e84cf2b17' |
| 470 | '347ebc1800181c4e2a1fb8dd53e1c635518c7dac47e9'), 40), |
| 471 | (bytes.fromhex('89b69d0516f829893c696226650a8687'), None),], |
| 472 | "sha512": [ |
| 473 | (bytes.fromhex('867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5' |
| 474 | 'd513554e1c8cf252c02d470a285a0501bad999bfe943c08f' |
| 475 | '050235d7d68b1da55e63f73b60a57fce'), None), |
| 476 | (bytes.fromhex('e1d9c16aa681708a45f5c7c4e215ceb66e011a2e9f004071' |
| 477 | '3f18aefdb866d53cf76cab2868a39b9f7840edce4fef5a82' |
| 478 | 'be67335c77a6068e04112754f27ccf4e'), None), |
| 479 | (bytes.fromhex('d197b1b33db0143e018b12f3d1d1479e6cdebdcc97c5c0f8' |
| 480 | '7f6902e072f457b5143f30602641b3d55cd335988cb36b84' |
| 481 | '376060ecd532e039b742a239434af2d5'), None), |
| 482 | (bytes.fromhex('8c0511f4c6e597c6ac6315d8f0362e225f3c501495ba23b8' |
| 483 | '68c005174dc4ee71115b59f9e60cd9532fa33e0f75aefe30' |
| 484 | '225c583a186cd82bd4daea9724a3d3b8'), 64), |
| 485 | (bytes.fromhex('9d9e9c4cd21fe4be24d5b8244c759665'), None),], |
| 486 | } |
| 487 | |
Christian Heimes | 0fbd94c | 2013-10-19 19:40:49 +0200 | [diff] [blame] | 488 | def _test_pbkdf2_hmac(self, pbkdf2): |
Christian Heimes | e92ef13 | 2013-10-13 00:52:43 +0200 | [diff] [blame] | 489 | for digest_name, results in self.pbkdf2_results.items(): |
| 490 | for i, vector in enumerate(self.pbkdf2_test_vectors): |
| 491 | password, salt, rounds, dklen = vector |
| 492 | expected, overwrite_dklen = results[i] |
| 493 | if overwrite_dklen: |
| 494 | dklen = overwrite_dklen |
| 495 | out = pbkdf2(digest_name, password, salt, rounds, dklen) |
| 496 | self.assertEqual(out, expected, |
| 497 | (digest_name, password, salt, rounds, dklen)) |
| 498 | out = pbkdf2(digest_name, memoryview(password), |
| 499 | memoryview(salt), rounds, dklen) |
| 500 | out = pbkdf2(digest_name, bytearray(password), |
| 501 | bytearray(salt), rounds, dklen) |
| 502 | self.assertEqual(out, expected) |
| 503 | if dklen is None: |
| 504 | out = pbkdf2(digest_name, password, salt, rounds) |
| 505 | self.assertEqual(out, expected, |
| 506 | (digest_name, password, salt, rounds)) |
| 507 | |
| 508 | self.assertRaises(TypeError, pbkdf2, b'sha1', b'pass', b'salt', 1) |
| 509 | self.assertRaises(TypeError, pbkdf2, 'sha1', 'pass', 'salt', 1) |
| 510 | self.assertRaises(ValueError, pbkdf2, 'sha1', b'pass', b'salt', 0) |
| 511 | self.assertRaises(ValueError, pbkdf2, 'sha1', b'pass', b'salt', -1) |
| 512 | self.assertRaises(ValueError, pbkdf2, 'sha1', b'pass', b'salt', 1, 0) |
| 513 | self.assertRaises(ValueError, pbkdf2, 'sha1', b'pass', b'salt', 1, -1) |
| 514 | with self.assertRaisesRegex(ValueError, 'unsupported hash type'): |
| 515 | pbkdf2('unknown', b'pass', b'salt', 1) |
| 516 | |
Christian Heimes | 0fbd94c | 2013-10-19 19:40:49 +0200 | [diff] [blame] | 517 | def test_pbkdf2_hmac_py(self): |
| 518 | self._test_pbkdf2_hmac(py_hashlib.pbkdf2_hmac) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 519 | |
Christian Heimes | 0fbd94c | 2013-10-19 19:40:49 +0200 | [diff] [blame] | 520 | @unittest.skipUnless(hasattr(c_hashlib, 'pbkdf2_hmac'), |
| 521 | ' test requires OpenSSL > 1.0') |
| 522 | def test_pbkdf2_hmac_c(self): |
| 523 | self._test_pbkdf2_hmac(c_hashlib.pbkdf2_hmac) |
Christian Heimes | 3626a50 | 2013-10-19 14:12:02 +0200 | [diff] [blame] | 524 | |
| 525 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 526 | if __name__ == "__main__": |
Brett Cannon | 3e9a9ae | 2013-06-12 21:25:59 -0400 | [diff] [blame] | 527 | unittest.main() |