Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Lib/test/test_hmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,14 @@ def test_hmac_digest_digestmod_parameter(self):
):
self.hmac_digest(b'key', b'msg', value)

def test_hmac_new_xof_digestmod(self):
# gh-145200: XOF digests (SHAKE) are not supported by HMAC.
# Verify that the error path does not leak the EVP_MAC_CTX.
for xof_name in ('shake_128', 'shake_256'):
with self.subTest(digestmod=xof_name):
with self.assertRaises(_hashlib.UnsupportedDigestmodError):
self.hmac_new(b'key', digestmod=xof_name)


class BuiltinConstructorTestCase(ThroughBuiltinAPIMixin,
ExtensionConstructorTestCaseMixin,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix memory leak in :mod:`hashlib` HMAC when ``EVP_MAC_init()`` or
``HMAC_Init_ex()`` fails (e.g., with an XOF digest such as SHAKE). The
``EVP_MAC_CTX`` is now freed on the error path.
1 change: 1 addition & 0 deletions Modules/_hashopenssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2103,6 +2103,7 @@ hashlib_HMAC_CTX_new_from_digestmod(_hashlibstate *state,
PY_EVP_MD_free(md);
#endif
if (r == 0) {
hashlib_openssl_HMAC_CTX_free(ctx);
if (is_xof) {
/* use a better default error message if an XOF is used */
raise_unsupported_algorithm_error(state, digestmod);
Expand Down
Loading