Skip to content

Commit 56f0f7a

Browse files
committed
avoid repeated calls, reword comment.
1 parent 8ec61b5 commit 56f0f7a

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

Lib/hashlib.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ def __usedforsecurity_check(md, name, *args, **kwargs):
8686
raise ValueError(name + " is blocked when usedforsecurity=True")
8787
return md(*args, **kwargs)
8888

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.
9393
def __get_wrapped_builtin(md, name):
94-
if _hashlib is not None and _hashlib.get_fips_mode() != 0:
94+
if __openssl_fips_mode != 0:
9595
from functools import partial
9696
return partial(__usedforsecurity_check, md, name)
9797
return md
@@ -188,10 +188,15 @@ def __hash_new(name, data=b'', **kwargs):
188188
__get_hash = __get_openssl_constructor
189189
algorithms_available = algorithms_available.union(
190190
_hashlib.openssl_md_meth_names)
191+
try:
192+
__openssl_fips_mode = _hashlib.get_fips_mode()
193+
except ValueError:
194+
__openssl_fips_mode = 0
191195
except ImportError:
192196
_hashlib = None
193197
new = __py_new
194198
__get_hash = __get_builtin_constructor
199+
__openssl_fips_mode = 0
195200

196201
try:
197202
# OpenSSL's PKCS5_PBKDF2_HMAC requires OpenSSL 1.0+ with HMAC and SHA

0 commit comments

Comments
 (0)