Skip to content

Commit cffa084

Browse files
committed
test one-shot HMAC-BLAKE2
1 parent a763884 commit cffa084

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

Lib/test/test_hmac.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import warnings
1111
from _operator import _compare_digest as operator_compare_digest
1212
from test.support import check_disallow_instantiation
13-
from test.support.import_helper import import_fresh_module
13+
from test.support.import_helper import import_fresh_module, import_module
1414

1515
try:
1616
import _hashlib
@@ -1007,7 +1007,7 @@ def test_hmac_digest_digestmod_parameter(self):
10071007
with (
10081008
self.subTest(value=value),
10091009
self.assert_raises_unknown_digestmod(),
1010-
):
1010+
):
10111011
self.hmac_digest(b'key', b'msg', value)
10121012

10131013

@@ -1453,5 +1453,35 @@ def test_with_fallback(self):
14531453
cache.pop('foo')
14541454

14551455

1456+
class BuiiltinMiscellaneousTests(BuiltinModuleMixin, unittest.TestCase):
1457+
"""HMAC-BLAKE2 is not standardized as BLAKE2 is a keyed hash function.
1458+
1459+
In particular, there is no official test vectors for HMAC-BLAKE2.
1460+
However, we can test that the HACL* interface is correctly used by
1461+
checking against the pure Python implementation output.
1462+
"""
1463+
1464+
@classmethod
1465+
def setUpClass(cls):
1466+
cls.blake2 = import_module("_blake2")
1467+
cls.blake2b = blake2.blake2b
1468+
cls.blake2s = blake2.blake2s
1469+
1470+
def assert_hmac_blake_correctness(self, digest, key, msg, hashfunc):
1471+
self.assertIsInstance(digest, bytes)
1472+
expect = hmac._compute_digest_fallback(key, msg, hashfunc)
1473+
self.assertEqual(actual, expect)
1474+
1475+
def test_compute_blake2b_32(self):
1476+
key, msg = os.urandom(8), os.urandom(16)
1477+
digest = self.hmac.compute_blake2b_32(key, msg)
1478+
self.assert_hmac_blake_correctness(digest, key, msg, self.blake2b)
1479+
1480+
def test_compute_blake2s_32(self):
1481+
key, msg = os.urandom(8), os.urandom(16)
1482+
digest = self.hmac.compute_blake2s_32(key, msg)
1483+
self.assert_hmac_blake_correctness(digest, key, msg, self.blake2s)
1484+
1485+
14561486
if __name__ == "__main__":
14571487
unittest.main()

0 commit comments

Comments
 (0)