Skip to content

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

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 threading
Expand Down Expand Up @@ -140,11 +141,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 @@ -251,6 +251,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 @@ -711,8 +780,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.
15 changes: 10 additions & 5 deletions Modules/_blake2/blake2b_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ new_BLAKE2bObject(PyTypeObject *type)
/*[clinic input]
@classmethod
_blake2.blake2b.__new__ as py_blake2b_new
data: object(c_default="NULL") = b''
/
data as data_obj: object(c_default="NULL") = b''
*
digest_size: int(c_default="BLAKE2B_OUTBYTES") = _blake2.blake2b.MAX_DIGEST_SIZE
key: Py_buffer(c_default="NULL", py_default="b''") = None
Expand All @@ -88,20 +87,26 @@ _blake2.blake2b.__new__ as py_blake2b_new
inner_size: int = 0
last_node: bool = False
usedforsecurity: bool = True
string: object(c_default="NULL") = None

Return a new BLAKE2b hash object.
[clinic start generated code]*/

static PyObject *
py_blake2b_new_impl(PyTypeObject *type, PyObject *data, int digest_size,
py_blake2b_new_impl(PyTypeObject *type, PyObject *data_obj, int digest_size,
Py_buffer *key, Py_buffer *salt, Py_buffer *person,
int fanout, int depth, unsigned long leaf_size,
unsigned long long node_offset, int node_depth,
int inner_size, int last_node, int usedforsecurity)
/*[clinic end generated code: output=32bfd8f043c6896f input=b947312abff46977]*/
int inner_size, int last_node, int usedforsecurity,
PyObject *string)
/*[clinic end generated code: output=de64bd850606b6a0 input=a876354eae7e3c39]*/
{
BLAKE2bObject *self = NULL;
PyObject *data;
Py_buffer buf;
if (_Py_hashlib_data_argument(&data, data_obj, string) < 0) {
return NULL;
}

self = new_BLAKE2bObject(type);
if (self == NULL) {
Expand Down
15 changes: 10 additions & 5 deletions Modules/_blake2/blake2s_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ new_BLAKE2sObject(PyTypeObject *type)
/*[clinic input]
@classmethod
_blake2.blake2s.__new__ as py_blake2s_new
data: object(c_default="NULL") = b''
/
data as data_obj: object(c_default="NULL") = b''
*
digest_size: int(c_default="BLAKE2S_OUTBYTES") = _blake2.blake2s.MAX_DIGEST_SIZE
key: Py_buffer(c_default="NULL", py_default="b''") = None
Expand All @@ -88,19 +87,25 @@ _blake2.blake2s.__new__ as py_blake2s_new
inner_size: int = 0
last_node: bool = False
usedforsecurity: bool = True
string: object(c_default="NULL") = None

Return a new BLAKE2s hash object.
[clinic start generated code]*/

static PyObject *
py_blake2s_new_impl(PyTypeObject *type, PyObject *data, int digest_size,
py_blake2s_new_impl(PyTypeObject *type, PyObject *data_obj, int digest_size,
Py_buffer *key, Py_buffer *salt, Py_buffer *person,
int fanout, int depth, unsigned long leaf_size,
unsigned long long node_offset, int node_depth,
int inner_size, int last_node, int usedforsecurity)
/*[clinic end generated code: output=556181f73905c686 input=4dda87723f23abb0]*/
int inner_size, int last_node, int usedforsecurity,
PyObject *string)
/*[clinic end generated code: output=582a0c4295cc3a3c input=308c3421c9c57f03]*/
{
BLAKE2sObject *self = NULL;
PyObject *data;
if (_Py_hashlib_data_argument(&data, data_obj, string) < 0) {
return NULL;
}
Py_buffer buf;

self = new_BLAKE2sObject(type);
Expand Down
50 changes: 31 additions & 19 deletions Modules/_blake2/clinic/blake2b_impl.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading