Skip to content

Commit a9e9cd6

Browse files
Fix detection of FIPS mode for blake2b (#879)
* Fix detection of FIPS mode for blake2b Blake2 algorithms are disabled on FIPS mode on OpenSSL level and preferred on Python level which cause the check of API (attributes) to fail sooner than OpenSSL raises ValueError for unavailable function. * Update test * Add changelog entry Co-authored-by: Brian Rutledge <brian@bhrutledge.com>
1 parent f69d4b7 commit a9e9cd6

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
lines changed

changelog/879.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve detection of disabled BLAKE2 hashing due to FIPS mode.

tests/test_package.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,10 @@ def test_fips_hash_manager_md5(monkeypatch):
307307
assert hasher.hexdigest() == hashes
308308

309309

310-
def test_fips_hash_manager_blake2(monkeypatch):
310+
@pytest.mark.parametrize("exception_class", [TypeError, ValueError])
311+
def test_fips_hash_manager_blake2(exception_class, monkeypatch):
311312
"""Generate hexdigest without BLAKE2 when hashlib is using FIPS mode."""
312-
replaced_blake2b = pretend.raiser(ValueError("fipsmode"))
313+
replaced_blake2b = pretend.raiser(exception_class("fipsmode"))
313314
monkeypatch.setattr(package_file.hashlib, "blake2b", replaced_blake2b)
314315

315316
filename = "tests/fixtures/twine-1.5.0-py2.py3-none-any.whl"

twine/package.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def __init__(self, filename: str) -> None:
268268
self._blake_hasher = None
269269
try:
270270
self._blake_hasher = hashlib.blake2b(digest_size=256 // 8)
271-
except ValueError:
271+
except (ValueError, TypeError):
272272
# FIPS mode disables blake2
273273
pass
274274

0 commit comments

Comments
 (0)