Skip to content

[3.11] gh-103256: Fix hmac algorithm to support fallback implementation (gh-103286) #103328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions Lib/test/test_hmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,16 @@ def test_with_digestmod_no_default(self):
with self.assertRaisesRegex(TypeError, r'required.*digestmod'):
hmac.HMAC(key, msg=data, digestmod='')

def test_with_fallback(self):
cache = getattr(hashlib, '__builtin_constructor_cache')
try:
cache['foo'] = hashlib.sha256
hexdigest = hmac.digest(b'key', b'message', 'foo').hex()
expected = '6e9ef29b75fffc5b7abae527d58fdadb2fe42e7219011976917343065f58ed4a'
self.assertEqual(hexdigest, expected)
finally:
cache.pop('foo')


class ConstructorTestCase(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Fixed a bug that caused :mod:`hmac` to raise an exception when the requested
hash algorithm was not available in OpenSSL despite being available
separately as part of ``hashlib`` itself. It now falls back properly to the
built-in. This could happen when, for example, your OpenSSL does not include
SHA3 support and you want to compute ``hmac.digest(b'K', b'M',
'sha3_256')``.
2 changes: 1 addition & 1 deletion Modules/_hashopenssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ py_digest_by_name(PyObject *module, const char *name, enum Py_hash_type py_ht)
}
}
if (digest == NULL) {
_setException(PyExc_ValueError, "unsupported hash type %s", name);
_setException(state->unsupported_digestmod_error, "unsupported hash type %s", name);
return NULL;
}
return digest;
Expand Down