MCPcopy
hub / github.com/pyca/cryptography / test_hmac

Function test_hmac

tests/wycheproof/test_hmac.py:38–65  ·  view source on GitHub ↗
(backend, wycheproof)

Source from the content-addressed store, hash-verified

36 "hmac_sha512_test.json",
37)
38def test_hmac(backend, wycheproof):
39 hash_algo = _HMAC_ALGORITHMS[wycheproof.testfiledata["algorithm"]]
40 if wycheproof.testgroup["tagSize"] // 8 != hash_algo.digest_size:
41 pytest.skip("Truncated HMAC not supported")
42 if not backend.hmac_supported(hash_algo):
43 pytest.skip(f"Hash {hash_algo.name} not supported")
44
45 h = hmac.HMAC(
46 key=binascii.unhexlify(wycheproof.testcase["key"]),
47 algorithm=hash_algo,
48 backend=backend,
49 )
50 h.update(binascii.unhexlify(wycheproof.testcase["msg"]))
51
52 if wycheproof.invalid:
53 with pytest.raises(InvalidSignature):
54 h.verify(binascii.unhexlify(wycheproof.testcase["tag"]))
55 else:
56 tag = h.finalize()
57 assert tag == binascii.unhexlify(wycheproof.testcase["tag"])
58
59 h = hmac.HMAC(
60 key=binascii.unhexlify(wycheproof.testcase["key"]),
61 algorithm=hash_algo,
62 backend=backend,
63 )
64 h.update(binascii.unhexlify(wycheproof.testcase["msg"]))
65 h.verify(binascii.unhexlify(wycheproof.testcase["tag"]))

Callers

nothing calls this directly

Calls 5

hmac_supportedMethod · 0.80
unhexlifyMethod · 0.80
updateMethod · 0.45
verifyMethod · 0.45
finalizeMethod · 0.45

Tested by

no test coverage detected