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