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

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', 'sha3_224', 'sha3_256', 'sha3_384', 62 'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512',
41 'sha3_512', 'SHA3', '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'].add(_sha3.sha3_512) 130 add_builtin_constructor('sha3_224')
102 self.constructors_to_test['sha3_224'].add(_sha3.sha3_224) 131 add_builtin_constructor('sha3_256')
103 self.constructors_to_test['sha3_256'].add(_sha3.sha3_256) 132 add_builtin_constructor('sha3_384')
104 self.constructors_to_test['sha3_384'].add(_sha3.sha3_384) 133 add_builtin_constructor('sha3_512')
105 self.constructors_to_test['sha3_512'].add(_sha3.sha3_512) 134 add_builtin_constructor('shake_128')
135 add_builtin_constructor('shake_256')
106 136
107 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)
108 143
109 def test_hash_array(self): 144 def test_hash_array(self):
110 a = array.array("b", range(10)) 145 a = array.array("b", range(10))
111 constructors = self.constructors_to_test.values() 146 for cons in self.hash_constructors:
112 for cons in itertools.chain.from_iterable(constructors):
113 c = cons(a) 147 c = cons(a)
114 c.hexdigest() 148 if c.name in self.shakes:
149 c.hexdigest(16)
150 else:
151 c.hexdigest()
115 152
116 def test_algorithms_guaranteed(self): 153 def test_algorithms_guaranteed(self):
117 self.assertEqual(hashlib.algorithms_guaranteed, 154 self.assertEqual(hashlib.algorithms_guaranteed,
118 set(_algo for _algo in self.supported_hash_names 155 set(_algo for _algo in self.supported_hash_names
119 if _algo.islower())) 156 if _algo.islower()))
120 157
121 def test_algorithms_available(self): 158 def test_algorithms_available(self):
122 self.assertTrue(set(hashlib.algorithms_guaranteed). 159 self.assertTrue(set(hashlib.algorithms_guaranteed).
123 issubset(hashlib.algorithms_available)) 160 issubset(hashlib.algorithms_available))
124 161
125 def test_unknown_hash(self): 162 def test_unknown_hash(self):
126 self.assertRaises(ValueError, hashlib.new, 'spam spam spam spam spam') 163 self.assertRaises(ValueError, hashlib.new, 'spam spam spam spam spam')
127 self.assertRaises(TypeError, hashlib.new, 1) 164 self.assertRaises(TypeError, hashlib.new, 1)
128 165
129 def test_get_builtin_constructor(self): 166 def test_get_builtin_constructor(self):
130 get_builtin_constructor = hashlib.__dict__[ 167 get_builtin_constructor = getattr(hashlib,
131 '__get_builtin_constructor'] 168 '__get_builtin_constructor')
169 builtin_constructor_cache = getattr(hashlib,
170 '__builtin_constructor_cache')
132 self.assertRaises(ValueError, get_builtin_constructor, 'test') 171 self.assertRaises(ValueError, get_builtin_constructor, 'test')
133 try: 172 try:
134 import _md5 173 import _md5
135 except ImportError: 174 except ImportError:
136 pass 175 pass
137 # This forces an ImportError for "import _md5" statements 176 # This forces an ImportError for "import _md5" statements
138 sys.modules['_md5'] = None 177 sys.modules['_md5'] = None
178 # clear the cache
179 builtin_constructor_cache.clear()
139 try: 180 try:
140 self.assertRaises(ValueError, get_builtin_constructor, 'md5') 181 self.assertRaises(ValueError, get_builtin_constructor, 'md5')
141 finally: 182 finally:
142 if '_md5' in locals(): 183 if '_md5' in locals():
143 sys.modules['_md5'] = _md5 184 sys.modules['_md5'] = _md5
144 else: 185 else:
145 del sys.modules['_md5'] 186 del sys.modules['_md5']
146 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'])
147 191
148 def test_hexdigest(self): 192 def test_hexdigest(self):
149 for name in self.supported_hash_names: 193 for cons in self.hash_constructors:
150 h = hashlib.new(name) 194 h = cons()
151 assert isinstance(h.digest(), bytes), name 195 if h.name in self.shakes:
152 self.assertEqual(hexstr(h.digest()), h.hexdigest()) 196 self.assertIsInstance(h.digest(16), bytes)
153 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)
154 211
155 def test_large_update(self): 212 def test_large_update(self):
156 aas = b'a' * 128 213 aas = b'a' * 128
157 bees = b'b' * 127 214 bees = b'b' * 127
158 cees = b'c' * 126 215 cees = b'c' * 126
159 216 dees = b'd' * 2048 # HASHLIB_GIL_MINSIZE
160 for name in self.supported_hash_names: 217
161 m1 = hashlib.new(name) 218 for cons in self.hash_constructors:
219 m1 = cons()
162 m1.update(aas) 220 m1.update(aas)
163 m1.update(bees) 221 m1.update(bees)
164 m1.update(cees) 222 m1.update(cees)
165 223 m1.update(dees)
166 m2 = hashlib.new(name) 224 if m1.name in self.shakes:
167 m2.update(aas + bees + cees) 225 args = (16,)
168 self.assertEqual(m1.digest(), m2.digest()) 226 else:
169 227 args = ()
170 def check(self, name, data, digest): 228
171 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()
172 constructors = self.constructors_to_test[name] 247 constructors = self.constructors_to_test[name]
173 # 2 is for hashlib.name(...) and hashlib.new(name, ...) 248 # 2 is for hashlib.name(...) and hashlib.new(name, ...)
174 self.assertGreaterEqual(len(constructors), 2) 249 self.assertGreaterEqual(len(constructors), 2)
175 for hash_object_constructor in constructors: 250 for hash_object_constructor in constructors:
176 computed = hash_object_constructor(data).hexdigest() 251 m = hash_object_constructor(data)
252 computed = m.hexdigest() if not shake else m.hexdigest(length)
177 self.assertEqual( 253 self.assertEqual(
178 computed, digest, 254 computed, hexdigest,
179 "Hash algorithm %s constructed using %s returned hexdigest" 255 "Hash algorithm %s constructed using %s returned hexdigest"
180 " %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."
181 % (name, hash_object_constructor, 257 % (name, hash_object_constructor,
182 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)
183 264
184 def check_no_unicode(self, algorithm_name): 265 def check_no_unicode(self, algorithm_name):
185 # Unicode objects are not allowed as input. 266 # Unicode objects are not allowed as input.
186 constructors = self.constructors_to_test[algorithm_name] 267 constructors = self.constructors_to_test[algorithm_name]
187 for hash_object_constructor in constructors: 268 for hash_object_constructor in constructors:
188 self.assertRaises(TypeError, hash_object_constructor, 'spam') 269 self.assertRaises(TypeError, hash_object_constructor, 'spam')
189 270
190 def test_no_unicode(self): 271 def test_no_unicode(self):
191 self.check_no_unicode('md5') 272 self.check_no_unicode('md5')
192 self.check_no_unicode('sha1') 273 self.check_no_unicode('sha1')
193 self.check_no_unicode('sha224') 274 self.check_no_unicode('sha224')
194 self.check_no_unicode('sha256') 275 self.check_no_unicode('sha256')
195 self.check_no_unicode('sha384') 276 self.check_no_unicode('sha384')
196 self.check_no_unicode('sha512') 277 self.check_no_unicode('sha512')
197 self.check_no_unicode('sha3') 278
279 @requires_sha3
280 def test_no_unicode_sha3(self):
198 self.check_no_unicode('sha3_224') 281 self.check_no_unicode('sha3_224')
199 self.check_no_unicode('sha3_256') 282 self.check_no_unicode('sha3_256')
200 self.check_no_unicode('sha3_384') 283 self.check_no_unicode('sha3_384')
201 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)
202 320
203 def test_case_md5_0(self): 321 def test_case_md5_0(self):
204 self.check('md5', b'', 'd41d8cd98f00b204e9800998ecf8427e') 322 self.check('md5', b'', 'd41d8cd98f00b204e9800998ecf8427e')
205 323
206 def test_case_md5_1(self): 324 def test_case_md5_1(self):
207 self.check('md5', b'abc', '900150983cd24fb0d6963f7d28e17f72') 325 self.check('md5', b'abc', '900150983cd24fb0d6963f7d28e17f72')
208 326
209 def test_case_md5_2(self): 327 def test_case_md5_2(self):
210 self.check('md5', 328 self.check('md5',
211 b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456 789', 329 b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456 789',
212 'd174ab98d277d9f5a5611c2c9f419d9f') 330 'd174ab98d277d9f5a5611c2c9f419d9f')
213 331
214 @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)
215 def test_case_md5_huge(self, size): 334 def test_case_md5_huge(self, size):
216 if size == _4G + 5: 335 self.check('md5', b'A'*size, 'c9af2dff37468ce5dfee8f2cfc0a9c6d')
217 try: 336
218 self.check('md5', b'A'*size, 'c9af2dff37468ce5dfee8f2cfc0a9c6d') 337 @unittest.skipIf(sys.maxsize < _4G - 1, 'test cannot run on 32-bit systems')
219 except OverflowError: 338 @bigmemtest(size=_4G - 1, memuse=1, dry_run=False)
220 pass # 32-bit arch
221
222 @bigmemtest(size=_4G - 1, memuse=1)
223 def test_case_md5_uintmax(self, size): 339 def test_case_md5_uintmax(self, size):
224 if size == _4G - 1: 340 self.check('md5', b'A'*size, '28138d306ff1b8281f1a9067e1a1a2b3')
225 try:
226 self.check('md5', b'A'*size, '28138d306ff1b8281f1a9067e1a1a2b3')
227 except OverflowError:
228 pass # 32-bit arch
229 341
230 # use the three examples from Federal Information Processing Standards 342 # use the three examples from Federal Information Processing Standards
231 # Publication 180-1, Secure Hash Standard, 1995 April 17 343 # Publication 180-1, Secure Hash Standard, 1995 April 17
232 # http://www.itl.nist.gov/div897/pubs/fip180-1.htm 344 # http://www.itl.nist.gov/div897/pubs/fip180-1.htm
233 345
234 def test_case_sha1_0(self): 346 def test_case_sha1_0(self):
235 self.check('sha1', b"", 347 self.check('sha1', b"",
236 "da39a3ee5e6b4b0d3255bfef95601890afd80709") 348 "da39a3ee5e6b4b0d3255bfef95601890afd80709")
237 349
238 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
327 b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+ 439 b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+
328 b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 440 b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
329 "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"+ 441 "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"+
330 "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909") 442 "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909")
331 443
332 def test_case_sha512_3(self): 444 def test_case_sha512_3(self):
333 self.check('sha512', b"a" * 1000000, 445 self.check('sha512', b"a" * 1000000,
334 "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+ 446 "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+
335 "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b") 447 "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b")
336 448
449 @requires_sha3
337 def test_case_sha3_224_0(self): 450 def test_case_sha3_224_0(self):
338 self.check('sha3_224', b"", 451 self.check('sha3_224', b"",
339 "F71837502BA8E10837BDD8D365ADB85591895602FC552B48B7390ABD") 452 "6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7")
340 453
454 @requires_sha3
455 def test_case_sha3_224_vector(self):
456 for msg, md in read_vectors('sha3_224'):
457 self.check('sha3_224', msg, md)
458
459 @requires_sha3
341 def test_case_sha3_256_0(self): 460 def test_case_sha3_256_0(self):
342 self.check('sha3_256', b"", 461 self.check('sha3_256', b"",
343 "C5D2460186F7233C927E7DB2DCC703C0E500B653CA82273B7BFAD8045D85A470") 462 "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a")
344 463
464 @requires_sha3
465 def test_case_sha3_256_vector(self):
466 for msg, md in read_vectors('sha3_256'):
467 self.check('sha3_256', msg, md)
468
469 @requires_sha3
345 def test_case_sha3_384_0(self): 470 def test_case_sha3_384_0(self):
346 self.check('sha3_384', b"", 471 self.check('sha3_384', b"",
347 "2C23146A63A29ACF99E73B88F8C24EAA7DC60AA771780CCC006AFBFA8FE2479B"+ 472 "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2a"+
348 "2DD2B21362337441AC12B515911957FF") 473 "c3713831264adb47fb6bd1e058d5f004")
349 474
475 @requires_sha3
476 def test_case_sha3_384_vector(self):
477 for msg, md in read_vectors('sha3_384'):
478 self.check('sha3_384', msg, md)
479
480 @requires_sha3
350 def test_case_sha3_512_0(self): 481 def test_case_sha3_512_0(self):
351 self.check('sha3_512', b"", 482 self.check('sha3_512', b"",
352 "0EAB42DE4C3CEB9235FC91ACFFE746B29C29A8C366B7C60E4E67C466F36A4304C"+ 483 "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a6"+
353 "00FA9CAF9D87976BA469BCBE06713B435F091EF2769FB160CDAB33D3670680E") 484 "15b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26")
485
486 @requires_sha3
487 def test_case_sha3_512_vector(self):
488 for msg, md in read_vectors('sha3_512'):
489 self.check('sha3_512', msg, md)
490
491 @requires_sha3
492 def test_case_shake_128_0(self):
493 self.check('shake_128', b"",
494 "7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26",
495 True)
496 self.check('shake_128', b"", "7f9c", True)
497
498 @requires_sha3
499 def test_case_shake128_vector(self):
500 for msg, md in read_vectors('shake_128'):
501 self.check('shake_128', msg, md, True)
502
503 @requires_sha3
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)
354 514
355 def test_gil(self): 515 def test_gil(self):
356 # 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
357 # for multithreaded operation (which is hardwired to 2048). 517 # for multithreaded operation (which is hardwired to 2048).
358 gil_minsize = 2048 518 gil_minsize = 2048
519
520 for cons in self.hash_constructors:
521 m = cons()
522 m.update(b'1')
523 m.update(b'#' * gil_minsize)
524 m.update(b'1')
525
526 m = cons(b'x' * gil_minsize)
527 m.update(b'1')
359 528
360 m = hashlib.md5() 529 m = hashlib.md5()
361 m.update(b'1') 530 m.update(b'1')
362 m.update(b'#' * gil_minsize) 531 m.update(b'#' * gil_minsize)
363 m.update(b'1') 532 m.update(b'1')
364 self.assertEqual(m.hexdigest(), 'cb1e1a2cbc80be75e19935d621fb9b21') 533 self.assertEqual(m.hexdigest(), 'cb1e1a2cbc80be75e19935d621fb9b21')
365 534
366 m = hashlib.md5(b'x' * gil_minsize) 535 m = hashlib.md5(b'x' * gil_minsize)
367 self.assertEqual(m.hexdigest(), 'cfb767f225d58469c5de3632a8803958') 536 self.assertEqual(m.hexdigest(), 'cfb767f225d58469c5de3632a8803958')
368 537
(...skipping 15 matching lines...) Expand all
384 def hash_in_chunks(chunk_size, event): 553 def hash_in_chunks(chunk_size, event):
385 index = 0 554 index = 0
386 while index < len(data): 555 while index < len(data):
387 hasher.update(data[index:index+chunk_size]) 556 hasher.update(data[index:index+chunk_size])
388 index += chunk_size 557 index += chunk_size
389 event.set() 558 event.set()
390 559
391 events = [] 560 events = []
392 for threadnum in range(num_threads): 561 for threadnum in range(num_threads):
393 chunk_size = len(data) // (10**threadnum) 562 chunk_size = len(data) // (10**threadnum)
394 assert chunk_size > 0 563 self.assertGreater(chunk_size, 0)
395 assert chunk_size % len(smallest_data) == 0 564 self.assertEqual(chunk_size % len(smallest_data), 0)
396 event = threading.Event() 565 event = threading.Event()
397 events.append(event) 566 events.append(event)
398 threading.Thread(target=hash_in_chunks, 567 threading.Thread(target=hash_in_chunks,
399 args=(chunk_size, event)).start() 568 args=(chunk_size, event)).start()
400 569
401 for event in events: 570 for event in events:
402 event.wait() 571 event.wait()
403 572
404 self.assertEqual(expected_hash, hasher.hexdigest()) 573 self.assertEqual(expected_hash, hasher.hexdigest())
405 574
406 def test_main(): 575
407 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
408 667
409 if __name__ == "__main__": 668 if __name__ == "__main__":
410 test_main() 669 unittest.main()
LEFTRIGHT

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