@@ -86,12 +86,12 @@ def __usedforsecurity_check(md, name, *args, **kwargs):
86
86
raise ValueError (name + " is blocked when usedforsecurity=True" )
87
87
return md (* args , ** kwargs )
88
88
89
- # If _hashlib is in FIPS mode, use the above wrapper to ensure builtin
90
- # implementation checks usedforsecurity kwarg. It means all builtin
91
- # implementations are treated as an unapproved implementation, as they
92
- # are unlikely to have been certified by NIST .
89
+ # If the _hashlib OpenSSL wrapper is in FIPS mode, wrap other implementations
90
+ # to check the usedforsecurity kwarg. All builtin implementations are treated
91
+ # as only available for useforsecurity=False purposes in the presence of such
92
+ # a configured and linked OpenSSL .
93
93
def __get_wrapped_builtin (md , name ):
94
- if _hashlib is not None and _hashlib . get_fips_mode () != 0 :
94
+ if __openssl_fips_mode != 0 :
95
95
from functools import partial
96
96
return partial (__usedforsecurity_check , md , name )
97
97
return md
@@ -188,10 +188,15 @@ def __hash_new(name, data=b'', **kwargs):
188
188
__get_hash = __get_openssl_constructor
189
189
algorithms_available = algorithms_available .union (
190
190
_hashlib .openssl_md_meth_names )
191
+ try :
192
+ __openssl_fips_mode = _hashlib .get_fips_mode ()
193
+ except ValueError :
194
+ __openssl_fips_mode = 0
191
195
except ImportError :
192
196
_hashlib = None
193
197
new = __py_new
194
198
__get_hash = __get_builtin_constructor
199
+ __openssl_fips_mode = 0
195
200
196
201
try :
197
202
# OpenSSL's PKCS5_PBKDF2_HMAC requires OpenSSL 1.0+ with HMAC and SHA
0 commit comments