8000 gh-131402: add HMAC-SHA3 NIST test cases (#131407) · python/cpython@8cb57dc · GitHub
[go: up one dir, main page]

Skip to content

Commit 8cb57dc

Browse files
authored
gh-131402: add HMAC-SHA3 NIST test cases (#131407)
Add NIST [1] tests for `HMAC-SHA-3/{224,256,384,512}`. [1]: https://csrc.nist.gov/Projects/cryptographic-standards-and-guidelines/example-values
1 parent 83479c2 commit 8cb57dc

File tree

1 file changed

+137
-21
lines changed

1 file changed

+137
-21
lines changed

Lib/test/test_hmac.py

Lines changed: 137 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,15 @@ class HashFunctionsTrait:
341341
ALGORITHMS = [
342342
'md5', 'sha1',
343343
'sha224', 'sha256', 'sha384', 'sha512',
344+
'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512',
344345
]
345346

346347
# By default, a missing algorithm skips the test that uses it.
347-
md5 = sha1 = sha224 = sha256 = sha384 = sha512 = property(
348-
lambda self: self.skipTest("missing hash function")
349-
)
348+
_ = property(lambda self: self.skipTest("missing hash function"))
349+
md5 = sha1 = _
350+
sha224 = sha256 = sha384 = sha512 = _
351+
sha3_224 = sha3_256 = sha3_384 = sha3_512 = _
352+
del _
350353

351354

352355
class WithOpenSSLHashFunctions(HashFunctionsTrait):
@@ -375,16 +378,23 @@ def setUpClass(cls):
375378
setattr(cls, name, name)
376379

377380

378-
class RFCTestCaseMixin(HashFunctionsTrait):
379-
"""Test HMAC implementations against test vectors from the RFC."""
381+
class RFCTestCaseMixin(AssertersMixin, HashFunctionsTrait):
382+
"""Test HMAC implementations against RFC 2202/4231 and NIST test vectors.
380383
381-
def test_md5(self):
384+
- Test vectors for MD5 and SHA-1 are taken from RFC 2202.
385+
- Test vectors for SHA-2 are taken from RFC 4231.
386+
- Test vectors for SHA-3 are NIST's test vectors [1].
387+
388+
[1] https://csrc.nist.gov/projects/message-authentication-codes
389+
"""
390+
391+
def test_md5_rfc2202(self):
382392
def md5test(key, msg, hexdigest):
383393
self.assert_hmac(key, msg, hexdigest, self.md5, "md5", 16, 64)
384394

385395
md5test(b"\x0b" * 16,
386396
b"Hi There",
387-
"9294727A3638BB1C13F48EF8158BFC9D")
397+
"9294727a3638bb1c13f48ef8158bfc9d")
388398

389399
md5test(b"Jefe",
390400
b"what do ya want for nothing?",
@@ -411,7 +421,7 @@ def md5test(key, msg, hexdigest):
411421
b"and Larger Than One Block-Size Data"),
412422
"6f630fad67cda0ee1fb1f562db3aa53e")
413423

414-
def test_sha1(self):
424+
def test_sha1_rfc2202(self):
415425
def shatest(key, msg, hexdigest):
416426
self.assert_hmac(key, msg, hexdigest, self.sha1, "sha1", 20, 64)
417427

@@ -457,11 +467,11 @@ def test_sha2_512_rfc4231(self):
457467
self._test_sha2_rfc4231(self.sha512, 'sha512', 64, 128)
458468

459469
def _test_sha2_rfc4231(self, hashfunc, hashname, digest_size, block_size):
460-
def hmactest(key, data, hexdigests):
470+
def hmactest(key, msg, hexdigests):
461471
hexdigest = hexdigests[hashname]
462472

463473
self.assert_hmac(
464-
key, data, hexdigest,
474+
key, msg, hexdigest,
465475
hashfunc=hashfunc,
466476
hashname=hashname,
467477
digest_size=digest_size,
@@ -470,7 +480,7 @@ def hmactest(key, data, hexdigests):
470480

471481
# 4.2. Test Case 1
472482
hmactest(key=b'\x0b' * 20,
473-
data=b'Hi There',
483+
msg=b'Hi There',
474484
hexdigests={
475485
'sha224': '896fb1128abbdf196832107cd49df33f'
476486
'47b4b1169912ba4f53684b22',
@@ -487,7 +497,7 @@ def hmactest(key, data, hexdigests):
487497

488498
# 4.3. Test Case 2
489499
hmactest(key=b'Jefe',
490-
data=b'what do ya want for nothing?',
500+
msg=b'what do ya want for nothing?',
491501
hexdigests={
492502
'sha224': 'a30e01098bc6dbbf45690f3a7e9e6d0f'
493503
'8bbea2a39e6148008fd05e44',
@@ -504,7 +514,7 @@ def hmactest(key, data, hexdigests):
504514

505515
# 4.4. Test Case 3
506516
hmactest(key=b'\xaa' * 20,
507-
data=b'\xdd' * 50,
517+
msg=b'\xdd' * 50,
508518
hexdigests={
509519
'sha224': '7fb3cb3588c6c1f6ffa9694d7d6ad264'
510520
'9365b0c1f65d69d1ec8333ea',
@@ -521,7 +531,7 @@ def hmactest(key, data, hexdigests):
521531

522532
# 4.5. Test Case 4
523533
hmactest(key=bytes(x for x in range(0x01, 0x19 + 1)),
524-
data=b'\xcd' * 50,
534+
msg=b'\xcd' * 50,
525535
hexdigests={
526536
'sha224': '6c11506874013cac6a2abc1bb382627c'
527537
'ec6a90d86efc012de7afec5a',
@@ -538,8 +548,8 @@ def hmactest(key, data, hexdigests):
538548

539549
# 4.7. Test Case 6
540550
hmactest(key=b'\xaa' * 131,
541-
data=b'Test Using Larger Than Block-Siz'
542-
b'e Key - Hash Key First',
551+
msg=b'Test Using Larger Than Block-Siz'
552+
b'e Key - Hash Key First',
543553
hexdigests={
544554
'sha224': '95e9a0db962095adaebe9b2d6f0dbce2'
545555
'd499f112f2d2b7273fa6870e',
@@ -556,11 +566,11 @@ def hmactest(key, data, hexdigests):
556566

557567
# 4.8. Test Case 7
558568
hmactest(key=b'\xaa' * 131,
559-
data=b'This is a test using a larger th'
560-
b'an block-size key and a larger t'
561-
b'han block-size data. The key nee'
562-
b'ds to be hashed before being use'
563-
b'd by the HMAC algorithm.',
569+
msg=b'This is a test using a larger th'
570+
b'an block-size key and a larger t'
571+
b'han block-size data. The key nee'
572+
b'ds to be hashed before being use'
573+
b'd by the HMAC algorithm.',
564574
hexdigests={
565575
'sha224': '3a854166ac5d9f023f54d517d0b39dbd'
566576
'946770db9c2b95c9f6f565d1',
@@ -575,6 +585,112 @@ def hmactest(key, data, hexdigests):
575585
'134676fb6de0446065c97440fa8c6a58',
576586
})
577587

588+
def test_sha3_224_nist(self):
589+
for key, msg, hexdigest in [
590+
(
591+
bytes(range(28)),
592+
b'Sample message for keylen<blocklen',
593+
'332cfd59347fdb8e576e77260be4aba2d6dc53117b3bfb52c6d18c04'
594+
), (
595+
bytes(range(144)),
596+
b'Sample message for keylen=blocklen',
597+
'd8b733bcf66c644a12323d564e24dcf3fc75f231f3b67968359100c7'
598+
), (
599+
bytes(range(172)),
600+
b'Sample message for keylen>blocklen',
601+
'078695eecc227c636ad31d063a15dd05a7e819a66ec6d8de1e193e59'
602+
)
603+
]:
604+
self.assert_hmac(
605+
key, msg, hexdigest,
606+
hashfunc=self.sha3_224, hashname='sha3_224',
607+
digest_size=28, block_size=144
608+
)
609+
610+
def test_sha3_256_nist(self):
611+
for key, msg, hexdigest in [
612+
(
613+
bytes(range(32)),
614+
b'Sample message for keylen<blocklen',
615+
'4fe8e202c4f058e8dddc23d8c34e4673'
616+
'43e23555e24fc2f025d598f558f67205'
617+
), (
618+
bytes(range(136)),
619+
b'Sample message for keylen=blocklen',
620+
'68b94e2e538a9be4103bebb5aa016d47'
621+
'961d4d1aa906061313b557f8af2c3faa'
622+
), (
623+
bytes(range(168)),
624+
b'Sample message for keylen>blocklen',
625+
'9bcf2c238e235c3ce88404e813bd2f3a'
626+
'97185ac6f238c63d6229a00b07974258'
627+
)
628+
]:
629+
self.assert_hmac(
630+
key, msg, hexdigest,
631+
hashfunc=self.sha3_256, hashname='sha3_256',
632+
digest_size=32, block_size=136
633+
)
634+
635+
def test_sha3_384_nist(self):
636+
for key, msg, hexdigest in [
637+
(
638+
bytes(range(48)),
639+
b'Sample message for keylen<blocklen',
640+
'd588a3c51f3f2d906e8298c1199aa8ff'
641+
'6296218127f6b38a90b6afe2c5617725'
642+
'bc99987f79b22a557b6520db710b7f42'
643+
), (
644+
bytes(range(104)),
645+
b'Sample message for keylen=blocklen',
646+
'a27d24b592e8c8cbf6d4ce6fc5bf62d8'
647+
'fc98bf2d486640d9eb8099e24047837f'
648+
'5f3bffbe92dcce90b4ed5b1e7e44fa90'
649+
), (
650+
bytes(range(152)),
651+
b'Sample message for keylen>blocklen',
652+
'e5ae4c739f455279368ebf36d4f5354c'
653+
'95aa184c899d3870e460ebc288ef1f94'
654+
'70053f73f7c6da2a71bcaec38ce7d6ac'
655+
)
656+
]:
657+
self.assert_hmac(
658+
key, msg, hexdigest,
659+
hashfunc=self.sha3_384, hashname='sha3_384',
660+
digest_size=48, block_size=104
661+
)
662+
663+
def test_sha3_512_nist(self):
664+
for key, msg, hexdigest in [
665+
(
666+
bytes(range(64)),
667+
b'Sample message for keylen<blocklen',
668+
'4efd629d6c71bf86162658f29943b1c3'
669+
'08ce27cdfa6db0d9c3ce81763f9cbce5'
670+
'f7ebe9868031db1a8f8eb7b6b95e5c5e'
671+
'3f657a8996c86a2f6527e307f0213196'
672+
), (
673+
bytes(range(72)),
674+
b'Sample message for keylen=blocklen',
675+
'544e257ea2a3e5ea19a590e6a24b724c'
676+
'e6327757723fe2751b75bf007d80f6b3'
677+
'60744bf1b7a88ea585f9765b47911976'
678+
'd3191cf83c039f5ffab0d29cc9d9b6da'
679+
), (
680+
bytes(range(136)),
681+
b'Sample message for keylen>blocklen',
682+
'5f464f5e5b7848e3885e49b2c385f069'
683+
'4985d0e38966242dc4a5fe3fea4b37d4'
684+
'6b65ceced5dcf59438dd840bab22269f'
685+
'0ba7febdb9fcf74602a35666b2a32915'
686+
)
687+
]:
688+
self.assert_hmac(
689+
key, msg, hexdigest,
690+
hashfunc=self.sha3_512, hashname='sha3_512',
691+
digest_size=64, block_size=72
692+
)
693+
578694

579695
class PyRFCTestCase(ThroughObjectMixin, PyAssertersMixin,
580696
WithOpenSSLHashFunctions, RFCTestCaseMixin,

0 commit comments

Comments
 (0)
0