Skip to content

[3.14] gh-134696: align OpenSSL and HACL*-based hash functions constructors AC signatures (GH-134713) #134961

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Lib/hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,29 +141,29 @@ def __get_openssl_constructor(name):
return __get_builtin_constructor(name)


def __py_new(name, data=b'', **kwargs):
def __py_new(name, *args, **kwargs):
"""new(name, data=b'', **kwargs) - Return a new hashing object using the
named algorithm; optionally initialized with data (which must be
a bytes-like object).
"""
return __get_builtin_constructor(name)(data, **kwargs)
return __get_builtin_constructor(name)(*args, **kwargs)


def __hash_new(name, data=b'', **kwargs):
def __hash_new(name, *args, **kwargs):
"""new(name, data=b'') - Return a new hashing object using the named algorithm;
optionally initialized with data (which must be a bytes-like object).
"""
if name in __block_openssl_constructor:
# Prefer our builtin blake2 implementation.
return __get_builtin_constructor(name)(data, **kwargs)
return __get_builtin_constructor(name)(*args, **kwargs)
try:
return _hashlib.new(name, data, **kwargs)
return _hashlib.new(name, *args, **kwargs)
except ValueError:
# If the _hashlib module (OpenSSL) doesn't support the named
# hash, try using our builtin implementations.
# This allows for SHA224/256 and SHA384/512 support even though
# the OpenSSL library prior to 0.9.8 doesn't provide them.
return __get_builtin_constructor(name)(data)
return __get_builtin_constructor(name)(*args, **kwargs)


try:
Expand Down
81 changes: 74 additions & 7 deletions Lib/test/test_hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import itertools
import logging
import os
import re
import sys
import sysconfig
import tempfile
Expand Down Expand Up @@ -141,11 +142,10 @@ def __init__(self, *args, **kwargs):
# of hashlib.new given the algorithm name.
for algorithm, constructors in self.constructors_to_test.items():
constructors.add(getattr(hashlib, algorithm))
def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm, **kwargs):
if data is None:
return hashlib.new(_alg, **kwargs)
return hashlib.new(_alg, data, **kwargs)
constructors.add(_test_algorithm_via_hashlib_new)
def c(*args, __algorithm_name=algorithm, **kwargs):
return hashlib.new(__algorithm_name, *args, **kwargs)
c.__name__ = f'do_test_algorithm_via_hashlib_new_{algorithm}'
constructors.add(c)

_hashlib = self._conditional_import_module('_hashlib')
self._hashlib = _hashlib
Expand Down Expand Up @@ -250,6 +250,75 @@ def test_usedforsecurity_false(self):
self._hashlib.new("md5", usedforsecurity=False)
self._hashlib.openssl_md5(usedforsecurity=False)

@unittest.skipIf(get_fips_mode(), "skip in FIPS mode")
def test_clinic_signature(self):
for constructor in self.hash_constructors:
with self.subTest(constructor.__name__):
constructor(b'')
constructor(data=b'')
constructor(string=b'') # should be deprecated in the future

digest_name = constructor(b'').name
with self.subTest(digest_name):
hashlib.new(digest_name, b'')
hashlib.new(digest_name, data=b'')
hashlib.new(digest_name, string=b'')
if self._hashlib:
self._hashlib.new(digest_name, b'')
self._hashlib.new(digest_name, data=b'')
self._hashlib.new(digest_name, string=b'')

@unittest.skipIf(get_fips_mode(), "skip in FIPS mode")
def test_clinic_signature_errors(self):
nomsg = b''
mymsg = b'msg'
conflicting_call = re.escape(
"'data' and 'string' are mutually exclusive "
"and support for 'string' keyword parameter "
"is slated for removal in a future version."
)
duplicated_param = re.escape("given by name ('data') and position")
unexpected_param = re.escape("got an unexpected keyword argument '_'")
for args, kwds, errmsg in [
# Reject duplicated arguments before unknown keyword arguments.
((nomsg,), dict(data=nomsg, _=nomsg), duplicated_param),
((mymsg,), dict(data=nomsg, _=nomsg), duplicated_param),
# Reject duplicated arguments before conflicting ones.
*itertools.product(
[[nomsg], [mymsg]],
[dict(data=nomsg), dict(data=nomsg, string=nomsg)],
[duplicated_param]
),
# Reject unknown keyword arguments before conflicting ones.
*itertools.product(
[()],
[
dict(_=None),
dict(data=nomsg, _=None),
dict(string=nomsg, _=None),
dict(string=nomsg, data=nomsg, _=None),
],
[unexpected_param]
),
((nomsg,), dict(_=None), unexpected_param),
((mymsg,), dict(_=None), unexpected_param),
# Reject conflicting arguments.
[(nomsg,), dict(string=nomsg), conflicting_call],
[(mymsg,), dict(string=nomsg), conflicting_call],
[(), dict(data=nomsg, string=nomsg), conflicting_call],
]:
for constructor in self.hash_constructors:
digest_name = constructor(b'').name
with self.subTest(constructor.__name__, args=args, kwds=kwds):
with self.assertRaisesRegex(TypeError, errmsg):
constructor(*args, **kwds)
with self.subTest(digest_name, args=args, kwds=kwds):
with self.assertRaisesRegex(TypeError, errmsg):
hashlib.new(digest_name, *args, **kwds)
if self._hashlib:
with self.assertRaisesRegex(TypeError, errmsg):
self._hashlib.new(digest_name, *args, **kwds)

def test_unknown_hash(self):
self.assertRaises(ValueError, hashlib.new, 'spam spam spam spam spam')
self.assertRaises(TypeError, hashlib.new, 1)
Expand Down Expand Up @@ -719,8 +788,6 @@ def check_blake2(self, constructor, salt_size, person_size, key_size,
self.assertRaises(ValueError, constructor, node_offset=-1)
self.assertRaises(OverflowError, constructor, node_offset=max_offset+1)

self.assertRaises(TypeError, constructor, data=b'')
self.assertRaises(TypeError, constructor, string=b'')
self.assertRaises(TypeError, constructor, '')

constructor(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Built-in HACL* and OpenSSL implementations of hash function constructors
now correctly accept the same *documented* named arguments. For instance,
:func:`~hashlib.md5` could be previously invoked as ``md5(data=data)``
or ``md5(string=string)`` depending on the underlying implementation
but these calls were not compatible. Patch by Bénédikt Tran.
Loading
Loading