gh-127298: Refactor test_hashlib for better usedforsecurity & openssl fips mode env support.#127492
gh-127298: Refactor test_hashlib for better usedforsecurity & openssl fips mode env support.#127492gpshead wants to merge 8 commits into
Conversation
…environment support.
|
!buildbot FIPS |
Sorry, something went wrong.
|
🤖 New build scheduled with the buildbot fleet by @gpshead for commit 59d9a85 🤖 The command will test the builders whose names match following regular expression: The builders matched are:
|
Sorry, something went wrong.
|
!buildbot FIPS |
Sorry, something went wrong.
|
🤖 New build scheduled with the buildbot fleet by @gpshead for commit bd46651 🤖 The command will test the builders whose names match following regular expression: The builders matched are:
|
Sorry, something went wrong.
7a5ab6c to
bd46651
Compare
December 2, 2024 03:42
|
#127467 is follow-on work to this that combined gets the FIPS mode buildbots passing in main. |
Sorry, something went wrong.
vstinner
left a comment
There was a problem hiding this comment.
Sorry, something went wrong.
Co-authored-by: Victor Stinner <vstinner@python.org>
|
Thanks for the PR! I'll have a look at it and I can also provide ssh access to the FIPS buildbots if that would make things easier to debug. |
Sorry, something went wrong.
|
I tested manually the change on RHEL8. 3 MD5 tests of test_hashlib are failing: |
Sorry, something went wrong.
|
I built Python with |
Sorry, something went wrong.
|
Suggested fix for file digest: diff --git a/Lib/hashlib.py b/Lib/hashlib.py
index 44656c33a..93d602571 100644
--- a/Lib/hashlib.py
+++ b/Lib/hashlib.py
@@ -192,7 +192,7 @@ def __hash_new(name, data=b'', **kwargs):
pass
-def file_digest(fileobj, digest, /, *, _bufsize=2**18):
+def file_digest(fileobj, digest, /, *, usedforsecurity=True, _bufsize=2**18):
"""Hash the contents of a file-like object. Returns a digest object.
*fileobj* must be a file-like object opened for reading in binary mode.
@@ -206,9 +206,9 @@ def file_digest(fileobj, digest, /, *, _bufsize=2**18):
# On Linux we could use AF_ALG sockets and sendfile() to archive zero-copy
# hashing with hardware acceleration.
if isinstance(digest, str):
- digestobj = new(digest)
+ digestobj = new(digest, usedforsecurity=usedforsecurity)
else:
- digestobj = digest()
+ digestobj = digest(usedforsecurity=usedforsecurity)
if hasattr(fileobj, "getbuffer"):
# io.BytesIO object, use zero-copy buffer
diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py
index 1c1a0396c..a7cca0ba5 100644
--- a/Lib/test/test_hashlib.py
+++ b/Lib/test/test_hashlib.py
@@ -443,11 +443,13 @@ def check_file_digest(self, name, data, hexdigest):
for digest in digests:
buf = io.BytesIO(data)
buf.seek(0)
- self.assertEqual(
- hashlib.file_digest(buf, digest).hexdigest(), hexdigest
- )
+ digestobj = hashlib.file_digest(buf, digest,
+ usedforsecurity=False)
+ self.assertEqual(digestobj.hexdigest(), hexdigest)
+
with open(os_helper.TESTFN, "rb") as f:
- digestobj = hashlib.file_digest(f, digest)
+ digestobj = hashlib.file_digest(f, digest,
+ usedforsecurity=False)
self.assertEqual(digestobj.hexdigest(), hexdigest)
finally:
os.unlink(os_helper.TESTFN) |
Sorry, something went wrong.
|
@gpshead the suggestion from @vstinner looks reasonable in #127492 (comment) will you push that to this pr? Or should that change be done separately, with a separate blurb entry? |
Sorry, something went wrong.
|
@gpshead hi, my PRs had comments that you are taking over with these changes instead, but they now seem to have stalled. Are you still actively working on this and related PRs? |
Sorry, something went wrong.
|
Note that changing how we materialize a digest object should done carefully (see PEP-452). For buf = io.BytesIO(b"somedata")
mac1 = hmac.HMAC(b"key", digestmod=hashlib.sha512)
digest = hashlib.file_digest(buf, lambda: mac1) |
Sorry, something went wrong.
|
This PR is stale because it has been open for 30 days with no activity. |
Sorry, something went wrong.
edited by bedevere-app
Bot
LoadingUh oh!
There was an error while loading. Please reload this page.
Copy link Copy MarkdownSorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.