hmac algorithm fallback is broken #103256
Labels
3.11
only security fixes
extension-modules
C modules in the Modules dir
topic-SSL
type-bug
An unexpected behavior, bug, or error
hmac
won't fall back if OpenSSL is available, the requested algorithm isn't in OpenSSL, but the algorithm is inhashlib
.If you [monkey]patch
hashlib
to include a new algorithm, you can't use that algorithm fromhmac
by name.It appears that the OpenSSL implementation (known as
_hashlib
from insidehashlib
, or_hashopenssl
from insidehmac
) doesn't actually return anUnsupportedDigestmodError
, but rather it's base classValueError
.MRE
The exception goes unhandled at
cpython/Lib/hmac.py
Line 199 in 933dfd7
hashlib
handle it.This also shows up in the stateful (non-oneshot) code at
cpython/Lib/hmac.py
Line 61 in 933dfd7
Passing a callable works as intended with my monkeypatch, so I have a workaround. However, I'd argue that either
hmac
is trying to catch the wrong thing, or OpenSSL is throwing the wrong thing, so some sort of fix is called for.Environment
Windows 10 64-bit
Python 3.11.2
Possible fixes
_hashopenssl.hmac_digest
to correctly raise anUnsupportedDigestmodError
(this looks like what was intended, given bpo-40645: use C implementation of HMAC #24920)ValueError
instead (asUnsupportedDigestmodError
is derived fromValueError
this would work, but may not be what is truly intended)Linked PRs
The text was updated successfully, but these errors were encountered: