@@ -66,7 +66,7 @@ class Implementation(enum.StrEnum):
6666 hashlib = enum .auto ()
6767
6868
69- class HashId (enum .StrEnum ):
69+ class _HashId (enum .StrEnum ):
7070 """Enumeration containing the canonical digest names.
7171
7272 Those names should only be used by hashlib.new() or hmac.new().
@@ -106,10 +106,10 @@ def is_keyed(self):
106106 return self .startswith ("blake2" )
107107
108108
109- CANONICAL_DIGEST_NAMES = frozenset (map (str , HashId .__members__ ))
109+ CANONICAL_DIGEST_NAMES = frozenset (map (str , _HashId .__members__ ))
110110NON_HMAC_DIGEST_NAMES = frozenset ((
111- HashId .shake_128 , HashId .shake_256 ,
112- HashId .blake2s , HashId .blake2b ,
111+ _HashId .shake_128 , _HashId .shake_256 ,
112+ _HashId .blake2s , _HashId .blake2b ,
113113))
114114
115115
@@ -331,112 +331,112 @@ def __init__(
331331
332332
333333_HASHINFO_DATABASE = MappingProxyType ({
334- HashId .md5 : _HashInfo (
335- HashId .md5 ,
334+ _HashId .md5 : _HashInfo (
335+ _HashId .md5 ,
336336 "_md5.MD5Type" ,
337337 "_hashlib.HASH" ,
338338 "_md5.md5" ,
339339 "_hashlib.openssl_md5" ,
340340 "hashlib.md5" ,
341341 ),
342- HashId .sha1 : _HashInfo (
343- HashId .sha1 ,
342+ _HashId .sha1 : _HashInfo (
343+ _HashId .sha1 ,
344344 "_sha1.SHA1Type" ,
345345 "_hashlib.HASH" ,
346346 "_sha1.sha1" ,
347347 "_hashlib.openssl_sha1" ,
348348 "hashlib.sha1" ,
349349 ),
350- HashId .sha224 : _HashInfo (
351- HashId .sha224 ,
350+ _HashId .sha224 : _HashInfo (
351+ _HashId .sha224 ,
352352 "_sha2.SHA224Type" ,
353353 "_hashlib.HASH" ,
354354 "_sha2.sha224" ,
355355 "_hashlib.openssl_sha224" ,
356356 "hashlib.sha224" ,
357357 ),
358- HashId .sha256 : _HashInfo (
359- HashId .sha256 ,
358+ _HashId .sha256 : _HashInfo (
359+ _HashId .sha256 ,
360360 "_sha2.SHA256Type" ,
361361 "_hashlib.HASH" ,
362362 "_sha2.sha256" ,
363363 "_hashlib.openssl_sha256" ,
364364 "hashlib.sha256" ,
365365 ),
366- HashId .sha384 : _HashInfo (
367- HashId .sha384 ,
366+ _HashId .sha384 : _HashInfo (
367+ _HashId .sha384 ,
368368 "_sha2.SHA384Type" ,
369369 "_hashlib.HASH" ,
370370 "_sha2.sha384" ,
371371 "_hashlib.openssl_sha384" ,
372372 "hashlib.sha384" ,
373373 ),
374- HashId .sha512 : _HashInfo (
375- HashId .sha512 ,
374+ _HashId .sha512 : _HashInfo (
375+ _HashId .sha512 ,
376376 "_sha2.SHA512Type" ,
377377 "_hashlib.HASH" ,
378378 "_sha2.sha512" ,
379379 "_hashlib.openssl_sha512" ,
380380 "hashlib.sha512" ,
381381 ),
382- HashId .sha3_224 : _HashInfo (
383- HashId .sha3_224 ,
382+ _HashId .sha3_224 : _HashInfo (
383+ _HashId .sha3_224 ,
384384 "_sha3.sha3_224" ,
385385 "_hashlib.HASH" ,
386386 "_sha3.sha3_224" ,
387387 "_hashlib.openssl_sha3_224" ,
388388 "hashlib.sha3_224" ,
389389 ),
390- HashId .sha3_256 : _HashInfo (
391- HashId .sha3_256 ,
390+ _HashId .sha3_256 : _HashInfo (
391+ _HashId .sha3_256 ,
392392 "_sha3.sha3_256" ,
393393 "_hashlib.HASH" ,
394394 "_sha3.sha3_256" ,
395395 "_hashlib.openssl_sha3_256" ,
396396 "hashlib.sha3_256" ,
397397 ),
398- HashId .sha3_384 : _HashInfo (
399- HashId .sha3_384 ,
398+ _HashId .sha3_384 : _HashInfo (
399+ _HashId .sha3_384 ,
400400 "_sha3.sha3_384" ,
401401 "_hashlib.HASH" ,
402402 "_sha3.sha3_384" ,
403403 "_hashlib.openssl_sha3_384" ,
404404 "hashlib.sha3_384" ,
405405 ),
406- HashId .sha3_512 : _HashInfo (
407- HashId .sha3_512 ,
406+ _HashId .sha3_512 : _HashInfo (
407+ _HashId .sha3_512 ,
408408 "_sha3.sha3_512" ,
409409 "_hashlib.HASH" ,
410410 "_sha3.sha3_512" ,
411411 "_hashlib.openssl_sha3_512" ,
412412 "hashlib.sha3_512" ,
413413 ),
414- HashId .shake_128 : _HashInfo (
415- HashId .shake_128 ,
414+ _HashId .shake_128 : _HashInfo (
415+ _HashId .shake_128 ,
416416 "_sha3.shake_128" ,
417417 "_hashlib.HASHXOF" ,
418418 "_sha3.shake_128" ,
419419 "_hashlib.openssl_shake_128" ,
420420 "hashlib.shake_128" ,
421421 ),
422- HashId .shake_256 : _HashInfo (
423- HashId .shake_256 ,
422+ _HashId .shake_256 : _HashInfo (
423+ _HashId .shake_256 ,
424424 "_sha3.shake_256" ,
425425 "_hashlib.HASHXOF" ,
426426 "_sha3.shake_256" ,
427427 "_hashlib.openssl_shake_256" ,
428428 "hashlib.shake_256" ,
429429 ),
430- HashId .blake2s : _HashInfo (
431- HashId .blake2s ,
430+ _HashId .blake2s : _HashInfo (
431+ _HashId .blake2s ,
432432 "_blake2.blake2s" ,
433433 "_hashlib.HASH" ,
434434 "_blake2.blake2s" ,
435435 None ,
436436 "hashlib.blake2s" ,
437437 ),
438- HashId .blake2b : _HashInfo (
439- HashId .blake2b ,
438+ _HashId .blake2b : _HashInfo (
439+ _HashId .blake2b ,
440440 "_blake2.blake2b" ,
441441 "_hashlib.HASH" ,
442442 "_blake2.blake2b" ,
@@ -469,21 +469,21 @@ def _iter_hash_func_info(excluded):
469469# There is currently no OpenSSL one-shot named function and there will likely
470470# be none in the future.
471471_HMACINFO_DATABASE = {
472- HashId (canonical_name ): _HashInfoItem (f"_hmac.compute_{ canonical_name } " )
472+ _HashId (canonical_name ): _HashInfoItem (f"_hmac.compute_{ canonical_name } " )
473473 for canonical_name in CANONICAL_DIGEST_NAMES
474474}
475475# Neither HACL* nor OpenSSL supports HMAC over XOFs.
476- _HMACINFO_DATABASE [HashId .shake_128 ] = _HashInfoItem ()
477- _HMACINFO_DATABASE [HashId .shake_256 ] = _HashInfoItem ()
476+ _HMACINFO_DATABASE [_HashId .shake_128 ] = _HashInfoItem ()
477+ _HMACINFO_DATABASE [_HashId .shake_256 ] = _HashInfoItem ()
478478# Strictly speaking, HMAC-BLAKE is meaningless as BLAKE2 is already a
479479# keyed hash function. However, as it's exposed by HACL*, we test it.
480- _HMACINFO_DATABASE [HashId .blake2s ] = _HashInfoItem ('_hmac.compute_blake2s_32' )
481- _HMACINFO_DATABASE [HashId .blake2b ] = _HashInfoItem ('_hmac.compute_blake2b_32' )
480+ _HMACINFO_DATABASE [_HashId .blake2s ] = _HashInfoItem ('_hmac.compute_blake2s_32' )
481+ _HMACINFO_DATABASE [_HashId .blake2b ] = _HashInfoItem ('_hmac.compute_blake2b_32' )
482482_HMACINFO_DATABASE = MappingProxyType (_HMACINFO_DATABASE )
483483assert _HMACINFO_DATABASE .keys () == CANONICAL_DIGEST_NAMES
484484
485485
486- def get_hmac_func_info (name ):
486+ def get_hmac_item_info (name ):
487487 info = _HMACINFO_DATABASE [name ]
488488 assert isinstance (info , _HashInfoItem ), info
489489 return info
@@ -538,6 +538,16 @@ def _ensure_wrapper_signature(wrapper, wrapped):
538538 )
539539
540540
541+ def _make_conditional_decorator (test , / , * test_args , ** test_kwargs ):
542+ def decorator_func (func ):
543+ @functools .wraps (func )
544+ def wrapper (* args , ** kwargs ):
545+ test (* test_args , ** test_kwargs )
546+ return func (* args , ** kwargs )
547+ return wrapper
548+ return functools .partial (_decorate_func_or_class , decorator_func )
549+
550+
541551def requires_openssl_hashlib ():
542552 _hashlib = _import_module ("_hashlib" )
543553 return unittest .skipIf (_hashlib is None , "requires _hashlib" )
@@ -657,16 +667,6 @@ def _openssl_hash(digestname, /, **kwargs):
657667 return constructor
658668
659669
660- def _make_requires_hashdigest_decorator (test , / , * test_args , ** test_kwargs ):
661- def decorator_func (func ):
662- @functools .wraps (func )
663- def wrapper (* args , ** kwargs ):
664- test (* test_args , ** test_kwargs )
665- return func (* args , ** kwargs )
666- return wrapper
667- return functools .partial (_decorate_func_or_class , decorator_func )
668-
669-
670670def requires_hashdigest (digestname , openssl = None , * , usedforsecurity = True ):
671671 """Decorator raising SkipTest if a hashing algorithm is not available.
672672
@@ -684,7 +684,7 @@ def requires_hashdigest(digestname, openssl=None, *, usedforsecurity=True):
684684 ValueError: [digital envelope routines: EVP_DigestInit_ex] disabled for FIPS
685685 ValueError: unsupported hash type md4
686686 """
687- return _make_requires_hashdigest_decorator (
687+ return _make_conditional_decorator (
688688 _hashlib_new , digestname , openssl , usedforsecurity = usedforsecurity
689689 )
690690
@@ -694,14 +694,14 @@ def requires_openssl_hashdigest(digestname, *, usedforsecurity=True):
694694
695695 The hashing algorithm may be missing or blocked by a strict crypto policy.
696696 """
697- return _make_requires_hashdigest_decorator (
697+ return _make_conditional_decorator (
698698 _openssl_new , digestname , usedforsecurity = usedforsecurity
699699 )
700700
701701
702702def _make_requires_builtin_hashdigest_decorator (item , * , usedforsecurity = True ):
703703 assert isinstance (item , _HashInfoItem ), item
704- return _make_requires_hashdigest_decorator (
704+ return _make_conditional_decorator (
705705 _builtin_hash ,
706706 item .module_name ,
707707 item .member_name ,
@@ -739,24 +739,13 @@ class HashFunctionsTrait:
739739 implementation of HMAC).
740740 """
741741
742- DIGEST_NAMES = [
743- 'md5' , 'sha1' ,
744- 'sha224' , 'sha256' , 'sha384' , 'sha512' ,
745- 'sha3_224' , 'sha3_256' , 'sha3_384' , 'sha3_512' ,
746- ]
747-
748742 # Default 'usedforsecurity' to use when checking a hash function.
749743 # When the trait properties are callables (e.g., _md5.md5) and
750744 # not strings, they must be called with the same 'usedforsecurity'.
751745 usedforsecurity = True
752746
753- @classmethod
754- def setUpClass (cls ):
755- super ().setUpClass ()
756- assert CANONICAL_DIGEST_NAMES .issuperset (cls .DIGEST_NAMES )
757-
758747 def is_valid_digest_name (self , digestname ):
759- self .assertIn (digestname , self . DIGEST_NAMES )
748+ self .assertIn (digestname , _HashId )
760749
761750 def _find_constructor (self , digestname ):
762751 # By default, a missing algorithm skips the test that uses it.
@@ -922,7 +911,7 @@ def _block_builtin_hash_new(name):
922911 """Block a buitin-in hash name from the hashlib.new() interface."""
923912 assert isinstance (name , str ), name
924913 assert name .lower () == name , f"invalid name: { name } "
925- assert name in HashId , f"invalid hash: { name } "
914+ assert name in _HashId , f"invalid hash: { name } "
926915
927916 # Re-import 'hashlib' in case it was mocked
928917 hashlib = importlib .import_module ('hashlib' )
@@ -947,7 +936,7 @@ def get_builtin_constructor_mock(name):
947936 return unittest .mock .patch .multiple (
948937 hashlib ,
949938 __get_builtin_constructor = get_builtin_constructor_mock ,
950- __builtin_constructor_cache = builtin_constructor_cache_mock
939+ __builtin_constructor_cache = builtin_constructor_cache_mock ,
951940 )
952941
953942
@@ -1018,7 +1007,7 @@ def dummy(data=b'', *, usedforsecurity=True, string=b''):
10181007
10191008def _block_builtin_hmac_constructor (name ):
10201009 """Block explicit HACL* HMAC constructors."""
1021- info = get_hmac_func_info (name )
1010+ info = get_hmac_item_info (name )
10221011 assert info .module_name is None or info .module_name == "_hmac" , info
10231012 if (wrapped := info .import_member ()) is None :
10241013 # function shouldn't exist for this implementation
0 commit comments