Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(27430)

Delta Between Two Patch Sets: Lib/test/test_hashlib.py

Issue 16113: Add SHA-3 (Keccak) support
Left Patch Set: Created 6 years, 11 months ago
Right Patch Set: Created 3 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/hashlib.py ('k') | Modules/hashlib.h » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # Test hashlib module 1 # Test hashlib module
2 # 2 #
3 # $Id$ 3 # $Id$
4 # 4 #
5 # Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org) 5 # Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org)
6 # Licensed to PSF under a Contributor Agreement. 6 # Licensed to PSF under a Contributor Agreement.
7 # 7 #
8 8
9 import array 9 import array
10 import hashlib 10 import hashlib
11 import itertools 11 import itertools
12 import os 12 import os
13 import sys 13 import sys
14 try: 14 try:
15 import threading 15 import threading
16 except ImportError: 16 except ImportError:
17 threading = None 17 threading = None
18 import unittest 18 import unittest
19 import warnings 19 import warnings
20 from test import support 20 from test import support
21 from test.support import _4G, bigmemtest 21 from test.support import _4G, bigmemtest, import_fresh_module
22 22
23 # Were we compiled --with-pydebug or with #define Py_DEBUG? 23 # Were we compiled --with-pydebug or with #define Py_DEBUG?
24 COMPILED_WITH_PYDEBUG = hasattr(sys, 'gettotalrefcount') 24 COMPILED_WITH_PYDEBUG = hasattr(sys, 'gettotalrefcount')
25
26 c_hashlib = import_fresh_module('hashlib', fresh=['_hashlib'])
27 py_hashlib = import_fresh_module('hashlib', blocked=['_hashlib'])
28
29 try:
30 import _sha3
31 except ImportError:
32 _sha3 = None
33
34 requires_sha3 = unittest.skipUnless(_sha3, 'requires _sha3')
25 35
26 36
27 def hexstr(s): 37 def hexstr(s):
28 assert isinstance(s, bytes), repr(s) 38 assert isinstance(s, bytes), repr(s)
29 h = "0123456789abcdef" 39 h = "0123456789abcdef"
30 r = '' 40 r = ''
31 for i in s: 41 for i in s:
32 r += h[(i >> 4) & 0xF] + h[i & 0xF] 42 r += h[(i >> 4) & 0xF] + h[i & 0xF]
33 return r 43 return r
34 44
35 45
46 URL = "https://raw.githubusercontent.com/tiran/python_vectors/master/{}.txt"
47
48 def read_vectors(hash_name):
49 with support.open_urlresource(URL.format(hash_name)) as f:
50 for line in f:
51 line = line.strip()
52 if line.startswith('#') or not line:
53 continue
54 msg, md = line.split(',')
55 yield bytes.fromhex(msg), md
56
57
36 class HashLibTestCase(unittest.TestCase): 58 class HashLibTestCase(unittest.TestCase):
37 supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1', 59 supported_hash_names = {'md5', 'MD5', 'sha1', 'SHA1',
38 'sha224', 'SHA224', 'sha256', 'SHA256', 60 'sha224', 'SHA224', 'sha256', 'SHA256',
39 'sha384', 'SHA384', 'sha512', 'SHA512', 61 'sha384', 'SHA384', 'sha512', 'SHA512',
40 'sha3_224', 'sha3_256', 'sha3_384', 62 'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512',
41 'sha3_512', 'SHA3_224', 'SHA3_256', 63 'shake_128', 'shake_256'}
42 'SHA3_384', 'SHA3_512' ) 64
65 shakes = {'shake_128', 'shake_256'}
43 66
44 # Issue #14693: fallback modules are always compiled under POSIX 67 # Issue #14693: fallback modules are always compiled under POSIX
45 _warn_on_extension_import = os.name == 'posix' or COMPILED_WITH_PYDEBUG 68 _warn_on_extension_import = os.name == 'posix' or COMPILED_WITH_PYDEBUG
46 69
47 def _conditional_import_module(self, module_name): 70 def _conditional_import_module(self, module_name):
48 """Import a module and return a reference to it or None on failure.""" 71 """Import a module and return a reference to it or None on failure."""
49 try: 72 try:
50 exec('import '+module_name) 73 exec('import '+module_name)
51 except ImportError as error: 74 except ImportError as error:
52 if self._warn_on_extension_import: 75 if self._warn_on_extension_import:
53 warnings.warn('Did a C extension fail to compile? %s' % error) 76 warnings.warn('Did a C extension fail to compile? %s' % error)
54 return locals().get(module_name) 77 return locals().get(module_name)
55 78
56 def __init__(self, *args, **kwargs): 79 def __init__(self, *args, **kwargs):
57 algorithms = set() 80 algorithms = set()
58 for algorithm in self.supported_hash_names: 81 for algorithm in self.supported_hash_names:
59 algorithms.add(algorithm.lower()) 82 algorithms.add(algorithm.lower())
83
60 self.constructors_to_test = {} 84 self.constructors_to_test = {}
61 for algorithm in algorithms: 85 for algorithm in algorithms:
62 self.constructors_to_test[algorithm] = set() 86 self.constructors_to_test[algorithm] = set()
63 87
64 # For each algorithm, test the direct constructor and the use 88 # For each algorithm, test the direct constructor and the use
65 # of hashlib.new given the algorithm name. 89 # of hashlib.new given the algorithm name.
66 for algorithm, constructors in self.constructors_to_test.items(): 90 for algorithm, constructors in self.constructors_to_test.items():
67 constructors.add(getattr(hashlib, algorithm)) 91 constructors.add(getattr(hashlib, algorithm))
68 def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm): 92 def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm):
69 if data is None: 93 if data is None:
70 return hashlib.new(_alg) 94 return hashlib.new(_alg)
71 return hashlib.new(_alg, data) 95 return hashlib.new(_alg, data)
72 constructors.add(_test_algorithm_via_hashlib_new) 96 constructors.add(_test_algorithm_via_hashlib_new)
73 97
74 _hashlib = self._conditional_import_module('_hashlib') 98 _hashlib = self._conditional_import_module('_hashlib')
75 if _hashlib: 99 if _hashlib:
76 # These two algorithms should always be present when this module 100 # These two algorithms should always be present when this module
77 # is compiled. If not, something was compiled wrong. 101 # is compiled. If not, something was compiled wrong.
78 assert hasattr(_hashlib, 'openssl_md5') 102 self.assertTrue(hasattr(_hashlib, 'openssl_md5'))
79 assert hasattr(_hashlib, 'openssl_sha1') 103 self.assertTrue(hasattr(_hashlib, 'openssl_sha1'))
80 for algorithm, constructors in self.constructors_to_test.items(): 104 for algorithm, constructors in self.constructors_to_test.items():
81 constructor = getattr(_hashlib, 'openssl_'+algorithm, None) 105 constructor = getattr(_hashlib, 'openssl_'+algorithm, None)
82 if constructor: 106 if constructor:
83 constructors.add(constructor) 107 constructors.add(constructor)
84 108
109 def add_builtin_constructor(name):
110 constructor = getattr(hashlib, "__get_builtin_constructor")(name)
111 self.constructors_to_test[name].add(constructor)
112
85 _md5 = self._conditional_import_module('_md5') 113 _md5 = self._conditional_import_module('_md5')
86 if _md5: 114 if _md5:
87 self.constructors_to_test['md5'].add(_md5.md5) 115 add_builtin_constructor('md5')
88 _sha1 = self._conditional_import_module('_sha1') 116 _sha1 = self._conditional_import_module('_sha1')
89 if _sha1: 117 if _sha1:
90 self.constructors_to_test['sha1'].add(_sha1.sha1) 118 add_builtin_constructor('sha1')
91 _sha256 = self._conditional_import_module('_sha256') 119 _sha256 = self._conditional_import_module('_sha256')
92 if _sha256: 120 if _sha256:
93 self.constructors_to_test['sha224'].add(_sha256.sha224) 121 add_builtin_constructor('sha224')
94 self.constructors_to_test['sha256'].add(_sha256.sha256) 122 add_builtin_constructor('sha256')
95 _sha512 = self._conditional_import_module('_sha512') 123 _sha512 = self._conditional_import_module('_sha512')
96 if _sha512: 124 if _sha512:
97 self.constructors_to_test['sha384'].add(_sha512.sha384) 125 add_builtin_constructor('sha384')
98 self.constructors_to_test['sha512'].add(_sha512.sha512) 126 add_builtin_constructor('sha512')
127
99 _sha3 = self._conditional_import_module('_sha3') 128 _sha3 = self._conditional_import_module('_sha3')
100 if _sha3: 129 if _sha3:
101 self.constructors_to_test['sha3_224'].add(_sha3.sha3_224) 130 add_builtin_constructor('sha3_224')
102 self.constructors_to_test['sha3_256'].add(_sha3.sha3_256) 131 add_builtin_constructor('sha3_256')
103 self.constructors_to_test['sha3_384'].add(_sha3.sha3_384) 132 add_builtin_constructor('sha3_384')
104 self.constructors_to_test['sha3_512'].add(_sha3.sha3_512) 133 add_builtin_constructor('sha3_512')
134 add_builtin_constructor('shake_128')
135 add_builtin_constructor('shake_256')
105 136
106 super(HashLibTestCase, self).__init__(*args, **kwargs) 137 super(HashLibTestCase, self).__init__(*args, **kwargs)
138
139 @property
140 def hash_constructors(self):
141 constructors = self.constructors_to_test.values()
142 return itertools.chain.from_iterable(constructors)
107 143
108 def test_hash_array(self): 144 def test_hash_array(self):
109 a = array.array("b", range(10)) 145 a = array.array("b", range(10))
110 constructors = self.constructors_to_test.values() 146 for cons in self.hash_constructors:
111 for cons in itertools.chain.from_iterable(constructors):
112 c = cons(a) 147 c = cons(a)
113 c.hexdigest() 148 if c.name in self.shakes:
149 c.hexdigest(16)
150 else:
151 c.hexdigest()
114 152
115 def test_algorithms_guaranteed(self): 153 def test_algorithms_guaranteed(self):
116 self.assertEqual(hashlib.algorithms_guaranteed, 154 self.assertEqual(hashlib.algorithms_guaranteed,
117 set(_algo for _algo in self.supported_hash_names 155 set(_algo for _algo in self.supported_hash_names
118 if _algo.islower())) 156 if _algo.islower()))
119 157
120 def test_algorithms_available(self): 158 def test_algorithms_available(self):
121 self.assertTrue(set(hashlib.algorithms_guaranteed). 159 self.assertTrue(set(hashlib.algorithms_guaranteed).
122 issubset(hashlib.algorithms_available)) 160 issubset(hashlib.algorithms_available))
123 161
124 def test_unknown_hash(self): 162 def test_unknown_hash(self):
125 self.assertRaises(ValueError, hashlib.new, 'spam spam spam spam spam') 163 self.assertRaises(ValueError, hashlib.new, 'spam spam spam spam spam')
126 self.assertRaises(TypeError, hashlib.new, 1) 164 self.assertRaises(TypeError, hashlib.new, 1)
127 165
128 def test_get_builtin_constructor(self): 166 def test_get_builtin_constructor(self):
129 get_builtin_constructor = hashlib.__dict__[ 167 get_builtin_constructor = getattr(hashlib,
130 '__get_builtin_constructor'] 168 '__get_builtin_constructor')
169 builtin_constructor_cache = getattr(hashlib,
170 '__builtin_constructor_cache')
131 self.assertRaises(ValueError, get_builtin_constructor, 'test') 171 self.assertRaises(ValueError, get_builtin_constructor, 'test')
132 try: 172 try:
133 import _md5 173 import _md5
134 except ImportError: 174 except ImportError:
135 pass 175 pass
136 # This forces an ImportError for "import _md5" statements 176 # This forces an ImportError for "import _md5" statements
137 sys.modules['_md5'] = None 177 sys.modules['_md5'] = None
178 # clear the cache
179 builtin_constructor_cache.clear()
138 try: 180 try:
139 self.assertRaises(ValueError, get_builtin_constructor, 'md5') 181 self.assertRaises(ValueError, get_builtin_constructor, 'md5')
140 finally: 182 finally:
141 if '_md5' in locals(): 183 if '_md5' in locals():
142 sys.modules['_md5'] = _md5 184 sys.modules['_md5'] = _md5
143 else: 185 else:
144 del sys.modules['_md5'] 186 del sys.modules['_md5']
145 self.assertRaises(TypeError, get_builtin_constructor, 3) 187 self.assertRaises(TypeError, get_builtin_constructor, 3)
188 constructor = get_builtin_constructor('md5')
189 self.assertIs(constructor, _md5.md5)
190 self.assertEqual(sorted(builtin_constructor_cache), ['MD5', 'md5'])
146 191
147 def test_hexdigest(self): 192 def test_hexdigest(self):
148 for name in self.supported_hash_names: 193 for cons in self.hash_constructors:
149 h = hashlib.new(name) 194 h = cons()
150 assert isinstance(h.digest(), bytes), name 195 if h.name in self.shakes:
151 self.assertEqual(hexstr(h.digest()), h.hexdigest()) 196 self.assertIsInstance(h.digest(16), bytes)
152 197 self.assertEqual(hexstr(h.digest(16)), h.hexdigest(16))
198 else:
199 self.assertIsInstance(h.digest(), bytes)
200 self.assertEqual(hexstr(h.digest()), h.hexdigest())
201
202 def test_name_attribute(self):
203 for cons in self.hash_constructors:
204 h = cons()
205 self.assertIsInstance(h.name, str)
206 if h.name in self.supported_hash_names:
207 self.assertIn(h.name, self.supported_hash_names)
208 else:
209 self.assertNotIn(h.name, self.supported_hash_names)
210 self.assertEqual(h.name, hashlib.new(h.name).name)
153 211
154 def test_large_update(self): 212 def test_large_update(self):
155 aas = b'a' * 128 213 aas = b'a' * 128
156 bees = b'b' * 127 214 bees = b'b' * 127
157 cees = b'c' * 126 215 cees = b'c' * 126
158 216 dees = b'd' * 2048 # HASHLIB_GIL_MINSIZE
159 for name in self.supported_hash_names: 217
160 m1 = hashlib.new(name) 218 for cons in self.hash_constructors:
219 m1 = cons()
161 m1.update(aas) 220 m1.update(aas)
162 m1.update(bees) 221 m1.update(bees)
163 m1.update(cees) 222 m1.update(cees)
164 223 m1.update(dees)
165 m2 = hashlib.new(name) 224 if m1.name in self.shakes:
166 m2.update(aas + bees + cees) 225 args = (16,)
167 self.assertEqual(m1.digest(), m2.digest()) 226 else:
168 227 args = ()
169 def check(self, name, data, digest): 228
170 digest = digest.lower() 229 m2 = cons()
230 m2.update(aas + bees + cees + dees)
231 self.assertEqual(m1.digest(*args), m2.digest(*args))
232
233 m3 = cons(aas + bees + cees + dees)
234 self.assertEqual(m1.digest(*args), m3.digest(*args))
235
236 # verify copy() doesn't touch original
237 m4 = cons(aas + bees + cees)
238 m4_digest = m4.digest(*args)
239 m4_copy = m4.copy()
240 m4_copy.update(dees)
241 self.assertEqual(m1.digest(*args), m4_copy.digest(*args))
242 self.assertEqual(m4.digest(*args), m4_digest)
243
244 def check(self, name, data, hexdigest, shake=False):
245 length = len(hexdigest)//2
246 hexdigest = hexdigest.lower()
171 constructors = self.constructors_to_test[name] 247 constructors = self.constructors_to_test[name]
172 # 2 is for hashlib.name(...) and hashlib.new(name, ...) 248 # 2 is for hashlib.name(...) and hashlib.new(name, ...)
173 self.assertGreaterEqual(len(constructors), 2) 249 self.assertGreaterEqual(len(constructors), 2)
174 for hash_object_constructor in constructors: 250 for hash_object_constructor in constructors:
175 computed = hash_object_constructor(data).hexdigest() 251 m = hash_object_constructor(data)
252 computed = m.hexdigest() if not shake else m.hexdigest(length)
176 self.assertEqual( 253 self.assertEqual(
177 computed, digest, 254 computed, hexdigest,
178 "Hash algorithm %s constructed using %s returned hexdigest" 255 "Hash algorithm %s constructed using %s returned hexdigest"
179 " %r for %d byte input data that should have hashed to %r." 256 " %r for %d byte input data that should have hashed to %r."
180 % (name, hash_object_constructor, 257 % (name, hash_object_constructor,
181 computed, len(data), digest)) 258 computed, len(data), hexdigest))
259 computed = m.digest() if not shake else m.digest(length)
260 digest = bytes.fromhex(hexdigest)
261 self.assertEqual(computed, digest)
262 if not shake:
263 self.assertEqual(len(digest), m.digest_size)
182 264
183 def check_no_unicode(self, algorithm_name): 265 def check_no_unicode(self, algorithm_name):
184 # Unicode objects are not allowed as input. 266 # Unicode objects are not allowed as input.
185 constructors = self.constructors_to_test[algorithm_name] 267 constructors = self.constructors_to_test[algorithm_name]
186 for hash_object_constructor in constructors: 268 for hash_object_constructor in constructors:
187 self.assertRaises(TypeError, hash_object_constructor, 'spam') 269 self.assertRaises(TypeError, hash_object_constructor, 'spam')
188 270
189 def test_no_unicode(self): 271 def test_no_unicode(self):
190 self.check_no_unicode('md5') 272 self.check_no_unicode('md5')
191 self.check_no_unicode('sha1') 273 self.check_no_unicode('sha1')
192 self.check_no_unicode('sha224') 274 self.check_no_unicode('sha224')
193 self.check_no_unicode('sha256') 275 self.check_no_unicode('sha256')
194 self.check_no_unicode('sha384') 276 self.check_no_unicode('sha384')
195 self.check_no_unicode('sha512') 277 self.check_no_unicode('sha512')
278
279 @requires_sha3
280 def test_no_unicode_sha3(self):
196 self.check_no_unicode('sha3_224') 281 self.check_no_unicode('sha3_224')
197 self.check_no_unicode('sha3_256') 282 self.check_no_unicode('sha3_256')
198 self.check_no_unicode('sha3_384') 283 self.check_no_unicode('sha3_384')
199 self.check_no_unicode('sha3_512') 284 self.check_no_unicode('sha3_512')
285 self.check_no_unicode('shake_128')
286 self.check_no_unicode('shake_256')
287
288 def check_blocksize_name(self, name, block_size=0, digest_size=0,
289 digest_length=None):
290 constructors = self.constructors_to_test[name]
291 for hash_object_constructor in constructors:
292 m = hash_object_constructor()
293 self.assertEqual(m.block_size, block_size)
294 self.assertEqual(m.digest_size, digest_size)
295 if digest_length:
296 self.assertEqual(len(m.digest(digest_length)),
297 digest_length)
298 self.assertEqual(len(m.hexdigest(digest_length)),
299 2*digest_length)
300 else:
301 self.assertEqual(len(m.digest()), digest_size)
302 self.assertEqual(len(m.hexdigest()), 2*digest_size)
303 self.assertEqual(m.name, name)
304 # split for sha3_512 / _sha3.sha3 object
305 self.assertIn(name.split("_")[0], repr(m))
306
307 def test_blocksize_name(self):
308 self.check_blocksize_name('md5', 64, 16)
309 self.check_blocksize_name('sha1', 64, 20)
310 self.check_blocksize_name('sha224', 64, 28)
311 self.check_blocksize_name('sha256', 64, 32)
312 self.check_blocksize_name('sha384', 128, 48)
313 self.check_blocksize_name('sha512', 128, 64)
314 self.check_blocksize_name('sha3_224', 144, 28)
315 self.check_blocksize_name('sha3_256', 136, 32)
316 self.check_blocksize_name('sha3_384', 104, 48)
317 self.check_blocksize_name('sha3_512', 72, 64)
318 self.check_blocksize_name('shake_128', 168, 0, 32)
319 self.check_blocksize_name('shake_256', 136, 0, 64)
200 320
201 def test_case_md5_0(self): 321 def test_case_md5_0(self):
202 self.check('md5', b'', 'd41d8cd98f00b204e9800998ecf8427e') 322 self.check('md5', b'', 'd41d8cd98f00b204e9800998ecf8427e')
203 323
204 def test_case_md5_1(self): 324 def test_case_md5_1(self):
205 self.check('md5', b'abc', '900150983cd24fb0d6963f7d28e17f72') 325 self.check('md5', b'abc', '900150983cd24fb0d6963f7d28e17f72')
206 326
207 def test_case_md5_2(self): 327 def test_case_md5_2(self):
208 self.check('md5', 328 self.check('md5',
209 b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456 789', 329 b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456 789',
210 'd174ab98d277d9f5a5611c2c9f419d9f') 330 'd174ab98d277d9f5a5611c2c9f419d9f')
211 331
212 @bigmemtest(size=_4G + 5, memuse=1) 332 @unittest.skipIf(sys.maxsize < _4G + 5, 'test cannot run on 32-bit systems')
333 @bigmemtest(size=_4G + 5, memuse=1, dry_run=False)
213 def test_case_md5_huge(self, size): 334 def test_case_md5_huge(self, size):
214 if size == _4G + 5: 335 self.check('md5', b'A'*size, 'c9af2dff37468ce5dfee8f2cfc0a9c6d')
215 try: 336
216 self.check('md5', b'A'*size, 'c9af2dff37468ce5dfee8f2cfc0a9c6d') 337 @unittest.skipIf(sys.maxsize < _4G - 1, 'test cannot run on 32-bit systems')
217 except OverflowError: 338 @bigmemtest(size=_4G - 1, memuse=1, dry_run=False)
218 pass # 32-bit arch
219
220 @bigmemtest(size=_4G - 1, memuse=1)
221 def test_case_md5_uintmax(self, size): 339 def test_case_md5_uintmax(self, size):
222 if size == _4G - 1: 340 self.check('md5', b'A'*size, '28138d306ff1b8281f1a9067e1a1a2b3')
223 try:
224 self.check('md5', b'A'*size, '28138d306ff1b8281f1a9067e1a1a2b3')
225 except OverflowError:
226 pass # 32-bit arch
227 341
228 # use the three examples from Federal Information Processing Standards 342 # use the three examples from Federal Information Processing Standards
229 # Publication 180-1, Secure Hash Standard, 1995 April 17 343 # Publication 180-1, Secure Hash Standard, 1995 April 17
230 # http://www.itl.nist.gov/div897/pubs/fip180-1.htm 344 # http://www.itl.nist.gov/div897/pubs/fip180-1.htm
231 345
232 def test_case_sha1_0(self): 346 def test_case_sha1_0(self):
233 self.check('sha1', b"", 347 self.check('sha1', b"",
234 "da39a3ee5e6b4b0d3255bfef95601890afd80709") 348 "da39a3ee5e6b4b0d3255bfef95601890afd80709")
235 349
236 def test_case_sha1_1(self): 350 def test_case_sha1_1(self):
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+ 439 b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+
326 b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 440 b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
327 "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"+ 441 "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"+
328 "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909") 442 "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909")
329 443
330 def test_case_sha512_3(self): 444 def test_case_sha512_3(self):
331 self.check('sha512', b"a" * 1000000, 445 self.check('sha512', b"a" * 1000000,
332 "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+ 446 "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+
333 "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b") 447 "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b")
334 448
335 # SHA-3 family 449 @requires_sha3
336 def test_case_sha3_224_0(self): 450 def test_case_sha3_224_0(self):
337 self.check('sha3_224', b"", 451 self.check('sha3_224', b"",
338 "F71837502BA8E10837BDD8D365ADB85591895602FC552B48B7390ABD") 452 "6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7")
339 453
340 def test_case_sha3_224_1(self): 454 @requires_sha3
341 self.check('sha3_224', bytes.fromhex("CC"), 455 def test_case_sha3_224_vector(self):
342 "A9CAB59EB40A10B246290F2D6086E32E3689FAF1D26B470C899F2802") 456 for msg, md in read_vectors('sha3_224'):
343 457 self.check('sha3_224', msg, md)
344 def test_case_sha3_224_2(self): 458
345 self.check('sha3_224', bytes.fromhex("41FB"), 459 @requires_sha3
346 "615BA367AFDC35AAC397BC7EB5D58D106A734B24986D5D978FEFD62C")
347
348 def test_case_sha3_224_3(self):
349 self.check('sha3_224', bytes.fromhex(
350 "433C5303131624C0021D868A30825475E8D0BD3052A022180398F4CA4423B9"+
351 "8214B6BEAAC21C8807A2C33F8C93BD42B092CC1B06CEDF3224D5ED1EC29784"+
352 "444F22E08A55AA58542B524B02CD3D5D5F6907AFE71C5D7462224A3F9D9E53"+
353 "E7E0846DCBB4CE"),
354 "62B10F1B6236EBC2DA72957742A8D4E48E213B5F8934604BFD4D2C3A")
355
356 @bigmemtest(size=_4G + 5, memuse=1)
357 def test_case_sha3_224_huge(self, size):
358 if size == _4G + 5:
359 try:
360 self.check('sha3_224', b'A'*size,
361 '58ef60057c9dddb6a87477e9ace5a26f0d9db01881cf9b10a9f8 c224')
362 except OverflowError:
363 pass # 32-bit arch
364
365
366 def test_case_sha3_256_0(self): 460 def test_case_sha3_256_0(self):
367 self.check('sha3_256', b"", 461 self.check('sha3_256', b"",
368 "C5D2460186F7233C927E7DB2DCC703C0E500B653CA82273B7BFAD8045D85A470") 462 "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a")
369 463
370 def test_case_sha3_256_1(self): 464 @requires_sha3
371 self.check('sha3_256', bytes.fromhex("CC"), 465 def test_case_sha3_256_vector(self):
372 "EEAD6DBFC7340A56CAEDC044696A168870549A6A7F6F56961E84A54BD9970B8A") 466 for msg, md in read_vectors('sha3_256'):
373 467 self.check('sha3_256', msg, md)
374 def test_case_sha3_256_2(self): 468
375 self.check('sha3_256', bytes.fromhex("41FB"), 469 @requires_sha3
376 "A8EACEDA4D47B3281A795AD9E1EA2122B407BAF9AABCB9E18B5717B7873537D2")
377
378 def test_case_sha3_256_3(self):
379 self.check('sha3_256', bytes.fromhex(
380 "433C5303131624C0021D868A30825475E8D0BD3052A022180398F4CA4423B9"+
381 "8214B6BEAAC21C8807A2C33F8C93BD42B092CC1B06CEDF3224D5ED1EC29784"+
382 "444F22E08A55AA58542B524B02CD3D5D5F6907AFE71C5D7462224A3F9D9E53"+
383 "E7E0846DCBB4CE"),
384 "CE87A5173BFFD92399221658F801D45C294D9006EE9F3F9D419C8D427748DC41")
385
386
387 def test_case_sha3_384_0(self): 470 def test_case_sha3_384_0(self):
388 self.check('sha3_384', b"", 471 self.check('sha3_384', b"",
389 "2C23146A63A29ACF99E73B88F8C24EAA7DC60AA771780CCC006AFBFA8FE2479B"+ 472 "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2a"+
390 "2DD2B21362337441AC12B515911957FF") 473 "c3713831264adb47fb6bd1e058d5f004")
391 474
392 def test_case_sha3_384_1(self): 475 @requires_sha3
393 self.check('sha3_384', bytes.fromhex("CC"), 476 def test_case_sha3_384_vector(self):
394 "1B84E62A46E5A201861754AF5DC95C4A1A69CAF4A796AE405680161E29572641"+ 477 for msg, md in read_vectors('sha3_384'):
395 "F5FA1E8641D7958336EE7B11C58F73E9") 478 self.check('sha3_384', msg, md)
396 479
397 def test_case_sha3_384_2(self): 480 @requires_sha3
398 self.check('sha3_384', bytes.fromhex("41FB"),
399 "495CCE2714CD72C8C53C3363D22C58B55960FE26BE0BF3BBC7A3316DD563AD1D"+
400 "B8410E75EEFEA655E39D4670EC0B1792")
401
402 def test_case_sha3_384_3(self):
403 self.check('sha3_384', bytes.fromhex(
404 "433C5303131624C0021D868A30825475E8D0BD3052A022180398F4CA4423B9"+
405 "8214B6BEAAC21C8807A2C33F8C93BD42B092CC1B06CEDF3224D5ED1EC29784"+
406 "444F22E08A55AA58542B524B02CD3D5D5F6907AFE71C5D7462224A3F9D9E53"+
407 "E7E0846DCBB4CE"),
408 "135114508DD63E279E709C26F7817C0482766CDE49132E3EDF2EEDD8996F4E35"+
409 "96D184100B384868249F1D8B8FDAA2C9")
410
411
412 def test_case_sha3_512_0(self): 481 def test_case_sha3_512_0(self):
413 self.check('sha3_512', b"", 482 self.check('sha3_512', b"",
414 "0EAB42DE4C3CEB9235FC91ACFFE746B29C29A8C366B7C60E4E67C466F36A4304"+ 483 "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a6"+
415 "C00FA9CAF9D87976BA469BCBE06713B435F091EF2769FB160CDAB33D3670680E") 484 "15b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26")
416 485
417 def test_case_sha3_512_1(self): 486 @requires_sha3
418 self.check('sha3_512', bytes.fromhex("CC"), 487 def test_case_sha3_512_vector(self):
419 "8630C13CBD066EA74BBE7FE468FEC1DEE10EDC1254FB4C1B7C5FD69B646E4416"+ 488 for msg, md in read_vectors('sha3_512'):
420 "0B8CE01D05A0908CA790DFB080F4B513BC3B6225ECE7A810371441A5AC666EB9") 489 self.check('sha3_512', msg, md)
421 490
422 def test_case_sha3_512_2(self): 491 @requires_sha3
423 self.check('sha3_512', bytes.fromhex("41FB"), 492 def test_case_shake_128_0(self):
424 "551DA6236F8B96FCE9F97F1190E901324F0B45E06DBBB5CDB8355D6ED1DC34B3"+ 493 self.check('shake_128', b"",
425 "F0EAE7DCB68622FF232FA3CECE0D4616CDEB3931F93803662A28DF1CD535B731") 494 "7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26",
426 495 True)
427 def test_case_sha3_512_3(self): 496 self.check('shake_128', b"", "7f9c", True)
428 self.check('sha3_512', bytes.fromhex( 497
429 "433C5303131624C0021D868A30825475E8D0BD3052A022180398F4CA4423B9"+ 498 @requires_sha3
430 "8214B6BEAAC21C8807A2C33F8C93BD42B092CC1B06CEDF3224D5ED1EC29784"+ 499 def test_case_shake128_vector(self):
431 "444F22E08A55AA58542B524B02CD3D5D5F6907AFE71C5D7462224A3F9D9E53"+ 500 for msg, md in read_vectors('shake_128'):
432 "E7E0846DCBB4CE"), 501 self.check('shake_128', msg, md, True)
433 "527D28E341E6B14F4684ADB4B824C496C6482E51149565D3D17226828884306B"+ 502
434 "51D6148A72622C2B75F5D3510B799D8BDC03EAEDE453676A6EC8FE03A1AD0EAB") 503 @requires_sha3
435 504 def test_case_shake_256_0(self):
505 self.check('shake_256', b"",
506 "46b9dd2b0ba88d13233b3feb743eeb243fcd52ea62b81b82b50c27646ed5762f",
507 True)
508 self.check('shake_256', b"", "46b9", True)
509
510 @requires_sha3
511 def test_case_shake128_vector(self):
512 for msg, md in read_vectors('shake_128'):
513 self.check('shake_128', msg, md, True)
436 514
437 def test_gil(self): 515 def test_gil(self):
438 # Check things work fine with an input larger than the size required 516 # Check things work fine with an input larger than the size required
439 # for multithreaded operation (which is hardwired to 2048). 517 # for multithreaded operation (which is hardwired to 2048).
440 gil_minsize = 2048 518 gil_minsize = 2048
441 519
442 for name in self.supported_hash_names: 520 for cons in self.hash_constructors:
443 m = hashlib.new(name) 521 m = cons()
444 m.update(b'1') 522 m.update(b'1')
445 m.update(b'#' * gil_minsize) 523 m.update(b'#' * gil_minsize)
446 m.update(b'1') 524 m.update(b'1')
447 525
448 m = hashlib.new(name, b'x' * gil_minsize) 526 m = cons(b'x' * gil_minsize)
449 m.update(b'1') 527 m.update(b'1')
450 528
451 m = hashlib.md5() 529 m = hashlib.md5()
452 m.update(b'1') 530 m.update(b'1')
453 m.update(b'#' * gil_minsize) 531 m.update(b'#' * gil_minsize)
454 m.update(b'1') 532 m.update(b'1')
455 self.assertEqual(m.hexdigest(), 'cb1e1a2cbc80be75e19935d621fb9b21') 533 self.assertEqual(m.hexdigest(), 'cb1e1a2cbc80be75e19935d621fb9b21')
456 534
457 m = hashlib.md5(b'x' * gil_minsize) 535 m = hashlib.md5(b'x' * gil_minsize)
458 self.assertEqual(m.hexdigest(), 'cfb767f225d58469c5de3632a8803958') 536 self.assertEqual(m.hexdigest(), 'cfb767f225d58469c5de3632a8803958')
(...skipping 16 matching lines...) Expand all
475 def hash_in_chunks(chunk_size, event): 553 def hash_in_chunks(chunk_size, event):
476 index = 0 554 index = 0
477 while index < len(data): 555 while index < len(data):
478 hasher.update(data[index:index+chunk_size]) 556 hasher.update(data[index:index+chunk_size])
479 index += chunk_size 557 index += chunk_size
480 event.set() 558 event.set()
481 559
482 events = [] 560 events = []
483 for threadnum in range(num_threads): 561 for threadnum in range(num_threads):
484 chunk_size = len(data) // (10**threadnum) 562 chunk_size = len(data) // (10**threadnum)
485 assert chunk_size > 0 563 self.assertGreater(chunk_size, 0)
486 assert chunk_size % len(smallest_data) == 0 564 self.assertEqual(chunk_size % len(smallest_data), 0)
487 event = threading.Event() 565 event = threading.Event()
488 events.append(event) 566 events.append(event)
489 threading.Thread(target=hash_in_chunks, 567 threading.Thread(target=hash_in_chunks,
490 args=(chunk_size, event)).start() 568 args=(chunk_size, event)).start()
491 569
492 for event in events: 570 for event in events:
493 event.wait() 571 event.wait()
494 572
495 self.assertEqual(expected_hash, hasher.hexdigest()) 573 self.assertEqual(expected_hash, hasher.hexdigest())
496 574
497 def test_main(): 575
498 support.run_unittest(HashLibTestCase) 576 class KDFTests(unittest.TestCase):
577
578 pbkdf2_test_vectors = [
579 (b'password', b'salt', 1, None),
580 (b'password', b'salt', 2, None),
581 (b'password', b'salt', 4096, None),
582 # too slow, it takes over a minute on a fast CPU.
583 #(b'password', b'salt', 16777216, None),
584 (b'passwordPASSWORDpassword', b'saltSALTsaltSALTsaltSALTsaltSALTsalt',
585 4096, -1),
586 (b'pass\0word', b'sa\0lt', 4096, 16),
587 ]
588
589 pbkdf2_results = {
590 "sha1": [
591 # official test vectors from RFC 6070
592 (bytes.fromhex('0c60c80f961f0e71f3a9b524af6012062fe037a6'), None),
593 (bytes.fromhex('ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957'), None),
594 (bytes.fromhex('4b007901b765489abead49d926f721d065a429c1'), None),
595 #(bytes.fromhex('eefe3d61cd4da4e4e9945b3d6ba2158c2634e984'), None),
596 (bytes.fromhex('3d2eec4fe41c849b80c8d83662c0e44a8b291a964c'
597 'f2f07038'), 25),
598 (bytes.fromhex('56fa6aa75548099dcc37d7f03425e0c3'), None),],
599 "sha256": [
600 (bytes.fromhex('120fb6cffcf8b32c43e7225256c4f837'
601 'a86548c92ccc35480805987cb70be17b'), None),
602 (bytes.fromhex('ae4d0c95af6b46d32d0adff928f06dd0'
603 '2a303f8ef3c251dfd6e2d85a95474c43'), None),
604 (bytes.fromhex('c5e478d59288c841aa530db6845c4c8d'
605 '962893a001ce4e11a4963873aa98134a'), None),
606 #(bytes.fromhex('cf81c66fe8cfc04d1f31ecb65dab4089'
607 # 'f7f179e89b3b0bcb17ad10e3ac6eba46'), None),
608 (bytes.fromhex('348c89dbcbd32b2f32d814b8116e84cf2b17'
609 '347ebc1800181c4e2a1fb8dd53e1c635518c7dac47e9'), 40),
610 (bytes.fromhex('89b69d0516f829893c696226650a8687'), None),],
611 "sha512": [
612 (bytes.fromhex('867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5'
613 'd513554e1c8cf252c02d470a285a0501bad999bfe943c08f'
614 '050235d7d68b1da55e63f73b60a57fce'), None),
615 (bytes.fromhex('e1d9c16aa681708a45f5c7c4e215ceb66e011a2e9f004071'
616 '3f18aefdb866d53cf76cab2868a39b9f7840edce4fef5a82'
617 'be67335c77a6068e04112754f27ccf4e'), None),
618 (bytes.fromhex('d197b1b33db0143e018b12f3d1d1479e6cdebdcc97c5c0f8'
619 '7f6902e072f457b5143f30602641b3d55cd335988cb36b84'
620 '376060ecd532e039b742a239434af2d5'), None),
621 (bytes.fromhex('8c0511f4c6e597c6ac6315d8f0362e225f3c501495ba23b8'
622 '68c005174dc4ee71115b59f9e60cd9532fa33e0f75aefe30'
623 '225c583a186cd82bd4daea9724a3d3b8'), 64),
624 (bytes.fromhex('9d9e9c4cd21fe4be24d5b8244c759665'), None),],
625 }
626
627 def _test_pbkdf2_hmac(self, pbkdf2):
628 for digest_name, results in self.pbkdf2_results.items():
629 for i, vector in enumerate(self.pbkdf2_test_vectors):
630 password, salt, rounds, dklen = vector
631 expected, overwrite_dklen = results[i]
632 if overwrite_dklen:
633 dklen = overwrite_dklen
634 out = pbkdf2(digest_name, password, salt, rounds, dklen)
635 self.assertEqual(out, expected,
636 (digest_name, password, salt, rounds, dklen))
637 out = pbkdf2(digest_name, memoryview(password),
638 memoryview(salt), rounds, dklen)
639 out = pbkdf2(digest_name, bytearray(password),
640 bytearray(salt), rounds, dklen)
641 self.assertEqual(out, expected)
642 if dklen is None:
643 out = pbkdf2(digest_name, password, salt, rounds)
644 self.assertEqual(out, expected,
645 (digest_name, password, salt, rounds))
646
647 self.assertRaises(TypeError, pbkdf2, b'sha1', b'pass', b'salt', 1)
648 self.assertRaises(TypeError, pbkdf2, 'sha1', 'pass', 'salt', 1)
649 self.assertRaises(ValueError, pbkdf2, 'sha1', b'pass', b'salt', 0)
650 self.assertRaises(ValueError, pbkdf2, 'sha1', b'pass', b'salt', -1)
651 self.assertRaises(ValueError, pbkdf2, 'sha1', b'pass', b'salt', 1, 0)
652 self.assertRaises(ValueError, pbkdf2, 'sha1', b'pass', b'salt', 1, -1)
653 with self.assertRaisesRegex(ValueError, 'unsupported hash type'):
654 pbkdf2('unknown', b'pass', b'salt', 1)
655 out = pbkdf2(hash_name='sha1', password=b'password', salt=b'salt',
656 iterations=1, dklen=None)
657 self.assertEqual(out, self.pbkdf2_results['sha1'][0][0])
658
659 def test_pbkdf2_hmac_py(self):
660 self._test_pbkdf2_hmac(py_hashlib.pbkdf2_hmac)
661
662 @unittest.skipUnless(hasattr(c_hashlib, 'pbkdf2_hmac'),
663 ' test requires OpenSSL > 1.0')
664 def test_pbkdf2_hmac_c(self):
665 self._test_pbkdf2_hmac(c_hashlib.pbkdf2_hmac)
666
499 667
500 if __name__ == "__main__": 668 if __name__ == "__main__":
501 test_main() 669 unittest.main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+