Skip to content

Commit 5572288

Browse files
committed
cleanup HMAC info database
1 parent 3cb6066 commit 5572288

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

Lib/test/support/hashlib_helper.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -467,19 +467,25 @@ def _iter_hash_func_info(excluded):
467467
# Mapping from canonical hash names to their explicit HACL* HMAC constructor.
468468
# There is currently no OpenSSL one-shot named function and there will likely
469469
# be none in the future.
470-
_EXPLICIT_HMAC_CONSTRUCTORS = {
471-
HashId(name): f"_hmac.compute_{name}"
472-
for name in CANONICAL_DIGEST_NAMES
470+
_HMACINFO_DATABASE = {
471+
HashId(canonical_name): _HashInfoItem(f"_hmac.compute_{canonical_name}")
472+
for canonical_name in CANONICAL_DIGEST_NAMES
473473
}
474474
# Neither HACL* nor OpenSSL supports HMAC over XOFs.
475-
_EXPLICIT_HMAC_CONSTRUCTORS[HashId.shake_128] = None
476-
_EXPLICIT_HMAC_CONSTRUCTORS[HashId.shake_256] = None
475+
_HMACINFO_DATABASE[HashId.shake_128] = _HashInfoItem()
476+
_HMACINFO_DATABASE[HashId.shake_256] = _HashInfoItem()
477477
# Strictly speaking, HMAC-BLAKE is meaningless as BLAKE2 is already a
478478
# keyed hash function. However, as it's exposed by HACL*, we test it.
479-
_EXPLICIT_HMAC_CONSTRUCTORS[HashId.blake2s] = '_hmac.compute_blake2s_32'
480-
_EXPLICIT_HMAC_CONSTRUCTORS[HashId.blake2b] = '_hmac.compute_blake2b_32'
481-
_EXPLICIT_HMAC_CONSTRUCTORS = MappingProxyType(_EXPLICIT_HMAC_CONSTRUCTORS)
482-
assert _EXPLICIT_HMAC_CONSTRUCTORS.keys() == CANONICAL_DIGEST_NAMES
479+
_HMACINFO_DATABASE[HashId.blake2s] = _HashInfoItem('_hmac.compute_blake2s_32')
480+
_HMACINFO_DATABASE[HashId.blake2b] = _HashInfoItem('_hmac.compute_blake2b_32')
481+
_HMACINFO_DATABASE = MappingProxyType(_HMACINFO_DATABASE)
482+
assert _HMACINFO_DATABASE.keys() == CANONICAL_DIGEST_NAMES
483+
484+
485+
def get_hmac_info_item(name):
486+
info = _HMACINFO_DATABASE[name]
487+
assert isinstance(info, _HashInfoItem), info
488+
return info
483489

484490

485491
def _decorate_func_or_class(decorator_func, func_or_class):
@@ -1011,7 +1017,7 @@ def dummy(data=b'', *, usedforsecurity=True, string=b''):
10111017

10121018
def _block_builtin_hmac_constructor(name):
10131019
"""Block explicit HACL* HMAC constructors."""
1014-
info = _HashInfoItem(_EXPLICIT_HMAC_CONSTRUCTORS[name])
1020+
info = get_hmac_info_item(name)
10151021
assert info.module_name is None or info.module_name == "_hmac", info
10161022
if (wrapped := info.import_member()) is None:
10171023
# function shouldn't exist for this implementation

Lib/test/test_support.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -879,8 +879,8 @@ def fetch_hash_function(self, name, implementation):
879879
return self.try_import_attribute(fullname)
880880

881881
def fetch_hmac_function(self, name):
882-
fullname = hashlib_helper._EXPLICIT_HMAC_CONSTRUCTORS[name]
883-
return self.try_import_attribute(fullname)
882+
target = hashlib_helper.get_hmac_info_item(name)
883+
return target.import_member()
884884

885885
def check_openssl_hash(self, name, *, disabled=True):
886886
"""Check that OpenSSL HASH interface is enabled/disabled."""

0 commit comments

Comments
 (0)