|
10 | 10 | import warnings
|
11 | 11 | from _operator import _compare_digest as operator_compare_digest
|
12 | 12 | 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 |
14 | 14 |
|
15 | 15 | try:
|
16 | 16 | import _hashlib
|
@@ -1007,7 +1007,7 @@ def test_hmac_digest_digestmod_parameter(self):
|
1007 | 1007 | with (
|
1008 | 1008 | self.subTest(value=value),
|
1009 | 1009 | self.assert_raises_unknown_digestmod(),
|
1010 |
| - ): |
| 1010 | + ): |
1011 | 1011 | self.hmac_digest(b'key', b'msg', value)
|
1012 | 1012 |
|
1013 | 1013 |
|
@@ -1453,5 +1453,35 @@ def test_with_fallback(self):
|
1453 | 1453 | cache.pop('foo')
|
1454 | 1454 |
|
1455 | 1455 |
|
| 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 | + |
1456 | 1486 | if __name__ == "__main__":
|
1457 | 1487 | unittest.main()
|
0 commit comments