Debian Codegraph @

lib/Crypto/SelfTest/Hash/test_HMAC.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # SelfTest/Hash/HMAC.py: Self-test for the HMAC module
4 #
5 # Written in 2008 by Dwayne C. Litzenberger <[email protected]>
6 #
7 # ===================================================================
8 # The contents of this file are dedicated to the public domain. To
9 # the extent that dedication to the public domain is not available,
10 # everyone is granted a worldwide, perpetual, royalty-free,
11 # non-exclusive license to exercise all rights associated with the
12 # contents of this file for any purpose whatsoever.
13 # No rights are reserved.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 # SOFTWARE.
23 # ===================================================================
24
25 """Self-test suite for Crypto.Hash.HMAC"""
26
27 import unittest
28 from binascii import hexlify
29 from Crypto.Util.py3compat import tostr, tobytes
30
31 from Crypto.Hash import (HMAC, MD5, SHA1, SHA256,
32 SHA224, SHA384, SHA512,
33 RIPEMD160,
34 SHA3_224, SHA3_256, SHA3_384, SHA3_512)
35
36
37 hash_modules = dict(MD5=MD5, SHA1=SHA1, SHA256=SHA256,
38 SHA224=SHA224, SHA384=SHA384, SHA512=SHA512,
39 RIPEMD160=RIPEMD160,
40 SHA3_224=SHA3_224, SHA3_256=SHA3_256,
41 SHA3_384=SHA3_384, SHA3_512=SHA3_512)
42
43 default_hash = None
44
45 def xl(text):
46 return tostr(hexlify(tobytes(text)))
47
48 # This is a list of (key, data, results, description) tuples.
49 test_data = [
50 ## Test vectors from RFC 2202 ##
51 # Test that the default hashmod is MD5
52 ('0b' * 16,
53 '4869205468657265',
54 dict(default_hash='9294727a3638bb1c13f48ef8158bfc9d'),
55 'default-is-MD5'),
56
57 # Test case 1 (MD5)
58 ('0b' * 16,
59 '4869205468657265',
60 dict(MD5='9294727a3638bb1c13f48ef8158bfc9d'),
61 'RFC 2202 #1-MD5 (HMAC-MD5)'),
62
63 # Test case 1 (SHA1)
64 ('0b' * 20,
65 '4869205468657265',
66 dict(SHA1='b617318655057264e28bc0b6fb378c8ef146be00'),
67 'RFC 2202 #1-SHA1 (HMAC-SHA1)'),
68
69 # Test case 2
70 ('4a656665',
71 '7768617420646f2079612077616e7420666f72206e6f7468696e673f',
72 dict(MD5='750c783e6ab0b503eaa86e310a5db738',
73 SHA1='effcdf6ae5eb2fa2d27416d5f184df9c259a7c79'),
74 'RFC 2202 #2 (HMAC-MD5/SHA1)'),
75
76 # Test case 3 (MD5)
77 ('aa' * 16,
78 'dd' * 50,
79 dict(MD5='56be34521d144c88dbb8c733f0e8b3f6'),
80 'RFC 2202 #3-MD5 (HMAC-MD5)'),
81
82 # Test case 3 (SHA1)
83 ('aa' * 20,
84 'dd' * 50,
85 dict(SHA1='125d7342b9ac11cd91a39af48aa17b4f63f175d3'),
86 'RFC 2202 #3-SHA1 (HMAC-SHA1)'),
87
88 # Test case 4
89 ('0102030405060708090a0b0c0d0e0f10111213141516171819',
90 'cd' * 50,
91 dict(MD5='697eaf0aca3a3aea3a75164746ffaa79',
92 SHA1='4c9007f4026250c6bc8414f9bf50c86c2d7235da'),
93 'RFC 2202 #4 (HMAC-MD5/SHA1)'),
94
95 # Test case 5 (MD5)
96 ('0c' * 16,
97 '546573742057697468205472756e636174696f6e',
98 dict(MD5='56461ef2342edc00f9bab995690efd4c'),
99 'RFC 2202 #5-MD5 (HMAC-MD5)'),
100
101 # Test case 5 (SHA1)
102 # NB: We do not implement hash truncation, so we only test the full hash here.
103 ('0c' * 20,
104 '546573742057697468205472756e636174696f6e',
105 dict(SHA1='4c1a03424b55e07fe7f27be1d58bb9324a9a5a04'),
106 'RFC 2202 #5-SHA1 (HMAC-SHA1)'),
107
108 # Test case 6
109 ('aa' * 80,
110 '54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a'
111 + '65204b6579202d2048617368204b6579204669727374',
112 dict(MD5='6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd',
113 SHA1='aa4ae5e15272d00e95705637ce8a3b55ed402112'),
114 'RFC 2202 #6 (HMAC-MD5/SHA1)'),
115
116 # Test case 7
117 ('aa' * 80,
118 '54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a'
119 + '65204b657920616e64204c6172676572205468616e204f6e6520426c6f636b2d'
120 + '53697a652044617461',
121 dict(MD5='6f630fad67cda0ee1fb1f562db3aa53e',
122 SHA1='e8e99d0f45237d786d6bbaa7965c7808bbff1a91'),
123 'RFC 2202 #7 (HMAC-MD5/SHA1)'),
124
125 ## Test vectors from RFC 4231 ##
126 # 4.2. Test Case 1
127 ('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b',
128 '4869205468657265',
129 dict(SHA256='''
130 b0344c61d8db38535ca8afceaf0bf12b
131 881dc200c9833da726e9376c2e32cff7
132 '''),
133 'RFC 4231 #1 (HMAC-SHA256)'),
134
135 # 4.3. Test Case 2 - Test with a key shorter than the length of the HMAC
136 # output.
137 ('4a656665',
138 '7768617420646f2079612077616e7420666f72206e6f7468696e673f',
139 dict(SHA256='''
140 5bdcc146bf60754e6a042426089575c7
141 5a003f089d2739839dec58b964ec3843
142 '''),
143 'RFC 4231 #2 (HMAC-SHA256)'),
144
145 # 4.4. Test Case 3 - Test with a combined length of key and data that is
146 # larger than 64 bytes (= block-size of SHA-224 and SHA-256).
147 ('aa' * 20,
148 'dd' * 50,
149 dict(SHA256='''
150 773ea91e36800e46854db8ebd09181a7
151 2959098b3ef8c122d9635514ced565fe
152 '''),
153 'RFC 4231 #3 (HMAC-SHA256)'),
154
155 # 4.5. Test Case 4 - Test with a combined length of key and data that is
156 # larger than 64 bytes (= block-size of SHA-224 and SHA-256).
157 ('0102030405060708090a0b0c0d0e0f10111213141516171819',
158 'cd' * 50,
159 dict(SHA256='''
160 82558a389a443c0ea4cc819899f2083a
161 85f0faa3e578f8077a2e3ff46729665b
162 '''),
163 'RFC 4231 #4 (HMAC-SHA256)'),
164
165 # 4.6. Test Case 5 - Test with a truncation of output to 128 bits.
166 #
167 # Not included because we do not implement hash truncation.
168 #
169
170 # 4.7. Test Case 6 - Test with a key larger than 128 bytes (= block-size of
171 # SHA-384 and SHA-512).
172 ('aa' * 131,
173 '54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a'
174 + '65204b6579202d2048617368204b6579204669727374',
175 dict(SHA256='''
176 60e431591ee0b67f0d8a26aacbf5b77f
177 8e0bc6213728c5140546040f0ee37f54
178 '''),
179 'RFC 4231 #6 (HMAC-SHA256)'),
180
181 # 4.8. Test Case 7 - Test with a key and data that is larger than 128 bytes
182 # (= block-size of SHA-384 and SHA-512).
183 ('aa' * 131,
184 '5468697320697320612074657374207573696e672061206c6172676572207468'
185 + '616e20626c6f636b2d73697a65206b657920616e642061206c61726765722074'
186 + '68616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565'
187 + '647320746f20626520686173686564206265666f7265206265696e6720757365'
188 + '642062792074686520484d414320616c676f726974686d2e',
189 dict(SHA256='''
190 9b09ffa71b942fcb27635fbcd5b0e944
191 bfdc63644f0713938a7f51535c3a35e2
192 '''),
193 'RFC 4231 #7 (HMAC-SHA256)'),
194
195 # Test case 8 (SHA224)
196 ('4a656665',
197 '7768617420646f2079612077616e74'
198 + '20666f72206e6f7468696e673f',
199 dict(SHA224='a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44'),
200 'RFC 4634 8.4 SHA224 (HMAC-SHA224)'),
201
202 # Test case 9 (SHA384)
203 ('4a656665',
204 '7768617420646f2079612077616e74'
205 + '20666f72206e6f7468696e673f',
206 dict(SHA384='af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec3736322445e8e2240ca5e69e2c78b3239ecfab21649'),
207 'RFC 4634 8.4 SHA384 (HMAC-SHA384)'),
208
209 # Test case 10 (SHA512)
210 ('4a656665',
211 '7768617420646f2079612077616e74'
212 + '20666f72206e6f7468696e673f',
213 dict(SHA512='164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737'),
214 'RFC 4634 8.4 SHA512 (HMAC-SHA512)'),
215
216 # Test case 11 (RIPEMD)
217 ('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b',
218 xl("Hi There"),
219 dict(RIPEMD160='24cb4bd67d20fc1a5d2ed7732dcc39377f0a5668'),
220 'RFC 2286 #1 (HMAC-RIPEMD)'),
221
222 # Test case 12 (RIPEMD)
223 (xl("Jefe"),
224 xl("what do ya want for nothing?"),
225 dict(RIPEMD160='dda6c0213a485a9e24f4742064a7f033b43c4069'),
226 'RFC 2286 #2 (HMAC-RIPEMD)'),
227
228 # Test case 13 (RIPEMD)
229 ('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
230 'dd' * 50,
231 dict(RIPEMD160='b0b105360de759960ab4f35298e116e295d8e7c1'),
232 'RFC 2286 #3 (HMAC-RIPEMD)'),
233
234 # Test case 14 (RIPEMD)
235 ('0102030405060708090a0b0c0d0e0f10111213141516171819',
236 'cd' * 50,
237 dict(RIPEMD160='d5ca862f4d21d5e610e18b4cf1beb97a4365ecf4'),
238 'RFC 2286 #4 (HMAC-RIPEMD)'),
239
240 # Test case 15 (RIPEMD)
241 ('0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c',
242 xl("Test With Truncation"),
243 dict(RIPEMD160='7619693978f91d90539ae786500ff3d8e0518e39'),
244 'RFC 2286 #5 (HMAC-RIPEMD)'),
245
246 # Test case 16 (RIPEMD)
247 ('aa' * 80,
248 xl("Test Using Larger Than Block-Size Key - Hash Key First"),
249 dict(RIPEMD160='6466ca07ac5eac29e1bd523e5ada7605b791fd8b'),
250 'RFC 2286 #6 (HMAC-RIPEMD)'),
251
252 # Test case 17 (RIPEMD)
253 ('aa' * 80,
254 xl("Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"),
255 dict(RIPEMD160='69ea60798d71616cce5fd0871e23754cd75d5a0a'),
256 'RFC 2286 #7 (HMAC-RIPEMD)'),
257
258 # From https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/HMAC_SHA3-224.pdf
259 (
260 '000102030405060708090a0b0c0d0e0f'
261 '101112131415161718191a1b',
262 xl('Sample message for keylen<blocklen'),
263 dict(SHA3_224='332cfd59347fdb8e576e77260be4aba2d6dc53117b3bfb52c6d18c04'),
264 'NIST CSRC Sample #1 (SHA3-224)'
265 ),
266 (
267 '000102030405060708090a0b0c0d0e0f'\
268 '101112131415161718191a1b1c1d1e1f'\
269 '202122232425262728292a2b2c2d2e2f'\
270 '303132333435363738393a3b3c3d3e3f'\
271 '404142434445464748494a4b4c4d4e4f'\
272 '505152535455565758595a5b5c5d5e5f'\
273 '606162636465666768696a6b6c6d6e6f'\
274 '707172737475767778797a7b7c7d7e7f'\
275 '808182838485868788898a8b8c8d8e8f',
276 xl('Sample message for keylen=blocklen'),
277 dict(SHA3_224='d8b733bcf66c644a12323d564e24dcf3fc75f231f3b67968359100c7'),
278 'NIST CSRC Sample #2 (SHA3-224)'
279 ),
280 (
281 '000102030405060708090a0b0c0d0e0f'\
282 '101112131415161718191a1b1c1d1e1f'\
283 '202122232425262728292a2b2c2d2e2f'\
284 '303132333435363738393a3b3c3d3e3f'\
285 '404142434445464748494a4b4c4d4e4f'\
286 '505152535455565758595a5b5c5d5e5f'\
287 '606162636465666768696a6b6c6d6e6f'\
288 '707172737475767778797a7b7c7d7e7f'\
289 '808182838485868788898a8b8c8d8e8f'\
290 '909192939495969798999a9b9c9d9e9f'\
291 'a0a1a2a3a4a5a6a7a8a9aaab',
292 xl('Sample message for keylen>blocklen'),
293 dict(SHA3_224='078695eecc227c636ad31d063a15dd05a7e819a66ec6d8de1e193e59'),
294 'NIST CSRC Sample #3 (SHA3-224)'
295 ),
296
297 # From https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/HMAC_SHA3-256.pdf
298 (
299 '000102030405060708090a0b0c0d0e0f'\
300 '101112131415161718191a1b1c1d1e1f',
301 xl('Sample message for keylen<blocklen'),
302 dict(SHA3_256='4fe8e202c4f058e8dddc23d8c34e467343e23555e24fc2f025d598f558f67205'),
303 'NIST CSRC Sample #1 (SHA3-256)'
304 ),
305 (
306 '000102030405060708090a0b0c0d0e0f'\
307 '101112131415161718191a1b1c1d1e1f'\
308 '202122232425262728292a2b2c2d2e2f'\
309 '303132333435363738393a3b3c3d3e3f'\
310 '404142434445464748494a4b4c4d4e4f'\
311 '505152535455565758595a5b5c5d5e5f'\
312 '606162636465666768696a6b6c6d6e6f'\
313 '707172737475767778797a7b7c7d7e7f'\
314 '8081828384858687',
315 xl('Sample message for keylen=blocklen'),
316 dict(SHA3_256='68b94e2e538a9be4103bebb5aa016d47961d4d1aa906061313b557f8af2c3faa'),
317 'NIST CSRC Sample #2 (SHA3-256)'
318 ),
319 (
320 '000102030405060708090a0b0c0d0e0f'\
321 '101112131415161718191a1b1c1d1e1f'\
322 '202122232425262728292a2b2c2d2e2f'\
323 '303132333435363738393a3b3c3d3e3f'\
324 '404142434445464748494a4b4c4d4e4f'\
325 '505152535455565758595a5b5c5d5e5f'\
326 '606162636465666768696a6b6c6d6e6f'\
327 '707172737475767778797a7b7c7d7e7f'\
328 '808182838485868788898a8b8c8d8e8f'\
329 '909192939495969798999a9b9c9d9e9f'\
330 'a0a1a2a3a4a5a6a7',
331 xl('Sample message for keylen>blocklen'),
332 dict(SHA3_256='9bcf2c238e235c3ce88404e813bd2f3a97185ac6f238c63d6229a00b07974258'),
333 'NIST CSRC Sample #3 (SHA3-256)'
334 ),
335
336 # From https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/HMAC_SHA3-384.pdf
337 (
338 '000102030405060708090a0b0c0d0e0f'\
339 '101112131415161718191a1b1c1d1e1f'
340 '202122232425262728292a2b2c2d2e2f',
341 xl('Sample message for keylen<blocklen'),
342 dict(SHA3_384='d588a3c51f3f2d906e8298c1199aa8ff6296218127f6b38a90b6afe2c5617725bc99987f79b22a557b6520db710b7f42'),
343 'NIST CSRC Sample #1 (SHA3-384)'
344 ),
345 (
346 '000102030405060708090a0b0c0d0e0f'\
347 '101112131415161718191a1b1c1d1e1f'\
348 '202122232425262728292a2b2c2d2e2f'\
349 '303132333435363738393a3b3c3d3e3f'\
350 '404142434445464748494a4b4c4d4e4f'\
351 '505152535455565758595a5b5c5d5e5f'\
352 '6061626364656667',
353 xl('Sample message for keylen=blocklen'),
354 dict(SHA3_384='a27d24b592e8c8cbf6d4ce6fc5bf62d8fc98bf2d486640d9eb8099e24047837f5f3bffbe92dcce90b4ed5b1e7e44fa90'),
355 'NIST CSRC Sample #2 (SHA3-384)'
356 ),
357 (
358 '000102030405060708090a0b0c0d0e0f'\
359 '101112131415161718191a1b1c1d1e1f'\
360 '202122232425262728292a2b2c2d2e2f'\
361 '303132333435363738393a3b3c3d3e3f'\
362 '404142434445464748494a4b4c4d4e4f'\
363 '505152535455565758595a5b5c5d5e5f'\
364 '606162636465666768696a6b6c6d6e6f'\
365 '707172737475767778797a7b7c7d7e7f'\
366 '808182838485868788898a8b8c8d8e8f'\
367 '9091929394959697',
368 xl('Sample message for keylen>blocklen'),
369 dict(SHA3_384='e5ae4c739f455279368ebf36d4f5354c95aa184c899d3870e460ebc288ef1f9470053f73f7c6da2a71bcaec38ce7d6ac'),
370 'NIST CSRC Sample #3 (SHA3-384)'
371 ),
372
373 # From https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/HMAC_SHA3-512.pdf
374 (
375 '000102030405060708090a0b0c0d0e0f'\
376 '101112131415161718191a1b1c1d1e1f'\
377 '202122232425262728292a2b2c2d2e2f'\
378 '303132333435363738393a3b3c3d3e3f',
379 xl('Sample message for keylen<blocklen'),
380 dict(SHA3_512='4efd629d6c71bf86162658f29943b1c308ce27cdfa6db0d9c3ce81763f9cbce5f7ebe9868031db1a8f8eb7b6b95e5c5e3f657a8996c86a2f6527e307f0213196'),
381 'NIST CSRC Sample #1 (SHA3-512)'
382 ),
383 (
384 '000102030405060708090a0b0c0d0e0f'\
385 '101112131415161718191a1b1c1d1e1f'\
386 '202122232425262728292a2b2c2d2e2f'\
387 '303132333435363738393a3b3c3d3e3f'\
388 '4041424344454647',
389 xl('Sample message for keylen=blocklen'),
390 dict(SHA3_512='544e257ea2a3e5ea19a590e6a24b724ce6327757723fe2751b75bf007d80f6b360744bf1b7a88ea585f9765b47911976d3191cf83c039f5ffab0d29cc9d9b6da'),
391 'NIST CSRC Sample #2 (SHA3-512)'
392 ),
393 (
394 '000102030405060708090a0b0c0d0e0f'\
395 '101112131415161718191a1b1c1d1e1f'\
396 '202122232425262728292a2b2c2d2e2f'\
397 '303132333435363738393a3b3c3d3e3f'\
398 '404142434445464748494a4b4c4d4e4f'\
399 '505152535455565758595a5b5c5d5e5f'\
400 '606162636465666768696a6b6c6d6e6f'\
401 '707172737475767778797a7b7c7d7e7f'\
402 '8081828384858687',
403 xl('Sample message for keylen>blocklen'),
404 dict(SHA3_512='5f464f5e5b7848e3885e49b2c385f0694985d0e38966242dc4a5fe3fea4b37d46b65ceced5dcf59438dd840bab22269f0ba7febdb9fcf74602a35666b2a32915'),
405 'NIST CSRC Sample #3 (SHA3-512)'
406 ),
407
408 ]
409
410
411 class HMAC_Module_and_Instance_Test(unittest.TestCase):
412 """Test the HMAC construction and verify that it does not
413 matter if you initialize it with a hash module or
414 with an hash instance.
415
416 See https://bugs.launchpad.net/pycrypto/+bug/1209399
417 """
418
419 def __init__(self, hashmods):
420 """Initialize the test with a dictionary of hash modules
421 indexed by their names"""
422
423 unittest.TestCase.__init__(self)
424 self.hashmods = hashmods
425 self.description = ""
426
427 def shortDescription(self):
428 return self.description
429
430 def runTest(self):
431 key = b"\x90\x91\x92\x93" * 4
432 payload = b"\x00" * 100
433
434 for hashname, hashmod in self.hashmods.items():
435 if hashmod is None:
436 continue
437 self.description = "Test HMAC in combination with " + hashname
438 one = HMAC.new(key, payload, hashmod).digest()
439 two = HMAC.new(key, payload, hashmod.new()).digest()
440 self.assertEqual(one, two)
441
442
443 class HMAC_None(unittest.TestCase):
444
445 def runTest(self):
446
447 key = b"\x04" * 20
448 one = HMAC.new(key, b"", SHA1).digest()
449 two = HMAC.new(key, None, SHA1).digest()
450 self.assertEqual(one, two)
451
452
453 class ByteArrayTests(unittest.TestCase):
454
455 def runTest(self):
456
457 key = b"0" * 16
458 data = b"\x00\x01\x02"
459
460 # Data and key can be a bytearray (during initialization)
461 key_ba = bytearray(key)
462 data_ba = bytearray(data)
463
464 h1 = HMAC.new(key, data)
465 h2 = HMAC.new(key_ba, data_ba)
466 key_ba[:1] = b'\xFF'
467 data_ba[:1] = b'\xFF'
468 self.assertEqual(h1.digest(), h2.digest())
469
470 # Data can be a bytearray (during operation)
471 key_ba = bytearray(key)
472 data_ba = bytearray(data)
473
474 h1 = HMAC.new(key)
475 h2 = HMAC.new(key)
476 h1.update(data)
477 h2.update(data_ba)
478 data_ba[:1] = b'\xFF'
479 self.assertEqual(h1.digest(), h2.digest())
480
481
482 class MemoryViewTests(unittest.TestCase):
483
484 def runTest(self):
485
486 key = b"0" * 16
487 data = b"\x00\x01\x02"
488
489 def get_mv_ro(data):
490 return memoryview(data)
491
492 def get_mv_rw(data):
493 return memoryview(bytearray(data))
494
495 for get_mv in (get_mv_ro, get_mv_rw):
496
497 # Data and key can be a memoryview (during initialization)
498 key_mv = get_mv(key)
499 data_mv = get_mv(data)
500
501 h1 = HMAC.new(key, data)
502 h2 = HMAC.new(key_mv, data_mv)
503 if not data_mv.readonly:
504 key_mv[:1] = b'\xFF'
505 data_mv[:1] = b'\xFF'
506 self.assertEqual(h1.digest(), h2.digest())
507
508 # Data can be a memoryview (during operation)
509 data_mv = get_mv(data)
510
511 h1 = HMAC.new(key)
512 h2 = HMAC.new(key)
513 h1.update(data)
514 h2.update(data_mv)
515 if not data_mv.readonly:
516 data_mv[:1] = b'\xFF'
517 self.assertEqual(h1.digest(), h2.digest())
518
519
520 def get_tests(config={}):
521 global test_data
522 import types
523 from .common import make_mac_tests
524
525 # A test vector contains multiple results, each one for a
526 # different hash algorithm.
527 # Here we expand each test vector into multiple ones,
528 # and add the relevant parameters that will be passed to new()
529 exp_test_data = []
530 for row in test_data:
531 for modname in row[2].keys():
532 t = list(row)
533 t[2] = row[2][modname]
534 t.append(dict(digestmod=globals()[modname]))
535 exp_test_data.append(t)
536 tests = make_mac_tests(HMAC, "HMAC", exp_test_data)
537 tests.append(HMAC_Module_and_Instance_Test(hash_modules))
538 tests.append(HMAC_None())
539
540 tests.append(ByteArrayTests())
541 tests.append(MemoryViewTests())
542
543 return tests
544
545
546 if __name__ == '__main__':
547 suite = lambda: unittest.TestSuite(get_tests())
548 unittest.main(defaultTest='suite')