Skip to content
Open
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
14 changes: 14 additions & 0 deletions Lib/test/test_hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from binascii import unhexlify
import hashlib
import importlib
import inspect
import io
import itertools
import logging
Expand Down Expand Up @@ -316,6 +317,18 @@ def test_clinic_signature(self):
DEPRECATED_STRING_PARAMETER):
self._hashlib.new(digest_name, string=b'')

def test_clinic_signature_apply_defaults(self):
# gh-162714: the defaults of the signature form a valid call
for constructor in self.hash_constructors:
try:
signature = inspect.signature(constructor)
except ValueError: # built-in SHA-3 types have no signature
continue
with self.subTest(constructor.__name__):
bound = signature.bind(b'')
bound.apply_defaults()
constructor(*bound.args, **bound.kwargs)

@unittest.skipIf(get_fips_mode(), "skip in FIPS mode")
def test_clinic_signature_errors(self):
nomsg = b''
Expand Down Expand Up @@ -354,6 +367,7 @@ def test_clinic_signature_errors(self):
[(nomsg,), dict(string=nomsg), conflicting_call],
[(mymsg,), dict(string=nomsg), conflicting_call],
[(), dict(data=nomsg, string=nomsg), conflicting_call],
[(nomsg,), dict(string=None), conflicting_call],
]:
for constructor in self.hash_constructors:
digest_name = constructor(b'').name
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:mod:`hashlib`: the deprecated *string* keyword parameter of hash object
constructors is no longer part of their signature, so that calls built with
:meth:`inspect.BoundArguments.apply_defaults` are valid.
Patch by Bénédikt Tran.
39 changes: 26 additions & 13 deletions Modules/_hashopenssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,7 @@ _hashlib_HASH(_hashlibstate *state, const char *digestname, PyObject *data_obj,
/* The module-level function: new() */

/*[clinic input]
@text_signature "($module, /, name, data=b'', *, usedforsecurity=True)"
_hashlib.new as _hashlib_HASH_new

name: str
Expand All @@ -1361,7 +1362,7 @@ The MD5 and SHA1 algorithms are always supported.
static PyObject *
_hashlib_HASH_new_impl(PyObject *module, const char *name, PyObject *data,
int usedforsecurity, PyObject *string)
/*[clinic end generated code: output=b905aaf9840c1bbd input=c34af6c6e696d44e]*/
/*[clinic end generated code: output=b905aaf9840c1bbd input=8f9d08a66387e4d8]*/
{
PyObject *data_obj;
if (_Py_hashlib_data_argument(&data_obj, data, string) < 0) {
Expand All @@ -1373,6 +1374,7 @@ _hashlib_HASH_new_impl(PyObject *module, const char *name, PyObject *data,


/*[clinic input]
@text_signature "($module, /, data=b'', *, usedforsecurity=True)"
_hashlib.openssl_md5

data: object(c_default="NULL") = b''
Expand All @@ -1387,13 +1389,14 @@ Returns a md5 hash object; optionally initialized with a string
static PyObject *
_hashlib_openssl_md5_impl(PyObject *module, PyObject *data,
int usedforsecurity, PyObject *string)
/*[clinic end generated code: output=ca8cf184d90f7432 input=e7c0adbd6a867db1]*/
/*[clinic end generated code: output=ca8cf184d90f7432 input=1242105b6a5176d6]*/
{
CALL_HASHLIB_NEW(module, Py_hash_md5, data, string, usedforsecurity);
}


/*[clinic input]
@text_signature "($module, /, data=b'', *, usedforsecurity=True)"
_hashlib.openssl_sha1

data: object(c_default="NULL") = b''
Expand All @@ -1408,13 +1411,14 @@ Returns a sha1 hash object; optionally initialized with a string
static PyObject *
_hashlib_openssl_sha1_impl(PyObject *module, PyObject *data,
int usedforsecurity, PyObject *string)
/*[clinic end generated code: output=1736fb7b310d64be input=f7e5bb1711e952d8]*/
/*[clinic end generated code: output=1736fb7b310d64be input=eca1e1e061f67ba8]*/
{
CALL_HASHLIB_NEW(module, Py_hash_sha1, data, string, usedforsecurity);
}


/*[clinic input]
@text_signature "($module, /, data=b'', *, usedforsecurity=True)"
_hashlib.openssl_sha224

data: object(c_default="NULL") = b''
Expand All @@ -1429,13 +1433,14 @@ Returns a sha224 hash object; optionally initialized with a string
static PyObject *
_hashlib_openssl_sha224_impl(PyObject *module, PyObject *data,
int usedforsecurity, PyObject *string)
/*[clinic end generated code: output=0d6ff57be5e5c140 input=3820fff7ed3a53b8]*/
/*[clinic end generated code: output=0d6ff57be5e5c140 input=c6bb80a6e9b8e982]*/
{
CALL_HASHLIB_NEW(module, Py_hash_sha224, data, string, usedforsecurity);
}


/*[clinic input]
@text_signature "($module, /, data=b'', *, usedforsecurity=True)"
_hashlib.openssl_sha256

data: object(c_default="NULL") = b''
Expand All @@ -1450,13 +1455,14 @@ Returns a sha256 hash object; optionally initialized with a string
static PyObject *
_hashlib_openssl_sha256_impl(PyObject *module, PyObject *data,
int usedforsecurity, PyObject *string)
/*[clinic end generated code: output=412ea7111555b6e7 input=9a2f115cf1f7e0eb]*/
/*[clinic end generated code: output=412ea7111555b6e7 input=2f0e3f6ed9e05e89]*/
{
CALL_HASHLIB_NEW(module, Py_hash_sha256, data, string, usedforsecurity);
}


/*[clinic input]
@text_signature "($module, /, data=b'', *, usedforsecurity=True)"
_hashlib.openssl_sha384

data: object(c_default="NULL") = b''
Expand All @@ -1471,13 +1477,14 @@ Returns a sha384 hash object; optionally initialized with a string
static PyObject *
_hashlib_openssl_sha384_impl(PyObject *module, PyObject *data,
int usedforsecurity, PyObject *string)
/*[clinic end generated code: output=2e0dc395b59ed726 input=1ea48f6f01e77cfb]*/
/*[clinic end generated code: output=2e0dc395b59ed726 input=5691ef74159f0bbc]*/
{
CALL_HASHLIB_NEW(module, Py_hash_sha384, data, string, usedforsecurity);
}


/*[clinic input]
@text_signature "($module, /, data=b'', *, usedforsecurity=True)"
_hashlib.openssl_sha512

data: object(c_default="NULL") = b''
Expand All @@ -1492,7 +1499,7 @@ Returns a sha512 hash object; optionally initialized with a string
static PyObject *
_hashlib_openssl_sha512_impl(PyObject *module, PyObject *data,
int usedforsecurity, PyObject *string)
/*[clinic end generated code: output=4bdd760388dbfc0f input=3cf56903e07d1f5c]*/
/*[clinic end generated code: output=4bdd760388dbfc0f input=0b97ed23dc54ab56]*/
{
CALL_HASHLIB_NEW(module, Py_hash_sha512, data, string, usedforsecurity);
}
Expand All @@ -1501,6 +1508,7 @@ _hashlib_openssl_sha512_impl(PyObject *module, PyObject *data,
#ifdef PY_OPENSSL_HAS_SHA3

/*[clinic input]
@text_signature "($module, /, data=b'', *, usedforsecurity=True)"
_hashlib.openssl_sha3_224

data: object(c_default="NULL") = b''
Expand All @@ -1515,12 +1523,13 @@ Returns a sha3-224 hash object; optionally initialized with a string
static PyObject *
_hashlib_openssl_sha3_224_impl(PyObject *module, PyObject *data,
int usedforsecurity, PyObject *string)
/*[clinic end generated code: output=6d8dc2a924f3ba35 input=7f14f16a9f6a3158]*/
/*[clinic end generated code: output=6d8dc2a924f3ba35 input=7107e36646dc3a8e]*/
{
CALL_HASHLIB_NEW(module, Py_hash_sha3_224, data, string, usedforsecurity);
}

/*[clinic input]
@text_signature "($module, /, data=b'', *, usedforsecurity=True)"
_hashlib.openssl_sha3_256

data: object(c_default="NULL") = b''
Expand All @@ -1535,12 +1544,13 @@ Returns a sha3-256 hash object; optionally initialized with a string
static PyObject *
_hashlib_openssl_sha3_256_impl(PyObject *module, PyObject *data,
int usedforsecurity, PyObject *string)
/*[clinic end generated code: output=9e520f537b3a4622 input=7987150939d5e352]*/
/*[clinic end generated code: output=9e520f537b3a4622 input=a0de473419ff4593]*/
{
CALL_HASHLIB_NEW(module, Py_hash_sha3_256, data, string, usedforsecurity);
}

/*[clinic input]
@text_signature "($module, /, data=b'', *, usedforsecurity=True)"
_hashlib.openssl_sha3_384

data: object(c_default="NULL") = b''
Expand All @@ -1555,12 +1565,13 @@ Returns a sha3-384 hash object; optionally initialized with a string
static PyObject *
_hashlib_openssl_sha3_384_impl(PyObject *module, PyObject *data,
int usedforsecurity, PyObject *string)
/*[clinic end generated code: output=d239ba0463fd6138 input=fc943401f67e3b81]*/
/*[clinic end generated code: output=d239ba0463fd6138 input=1c9c36f4b0fb859e]*/
{
CALL_HASHLIB_NEW(module, Py_hash_sha3_384, data, string, usedforsecurity);
}

/*[clinic input]
@text_signature "($module, /, data=b'', *, usedforsecurity=True)"
_hashlib.openssl_sha3_512

data: object(c_default="NULL") = b''
Expand All @@ -1575,7 +1586,7 @@ Returns a sha3-512 hash object; optionally initialized with a string
static PyObject *
_hashlib_openssl_sha3_512_impl(PyObject *module, PyObject *data,
int usedforsecurity, PyObject *string)
/*[clinic end generated code: output=17662f21038c2278 input=6601ddd2c6c1516d]*/
/*[clinic end generated code: output=17662f21038c2278 input=eb09a63a3d478630]*/
{
CALL_HASHLIB_NEW(module, Py_hash_sha3_512, data, string, usedforsecurity);
}
Expand All @@ -1584,6 +1595,7 @@ _hashlib_openssl_sha3_512_impl(PyObject *module, PyObject *data,
#ifdef PY_OPENSSL_HAS_SHAKE
/*[clinic input]
@permit_long_summary
@text_signature "($module, /, data=b'', *, usedforsecurity=True)"
_hashlib.openssl_shake_128

data: object(c_default="NULL") = b''
Expand All @@ -1598,13 +1610,14 @@ Returns a shake-128 variable hash object; optionally initialized with a string
static PyObject *
_hashlib_openssl_shake_128_impl(PyObject *module, PyObject *data,
int usedforsecurity, PyObject *string)
/*[clinic end generated code: output=4e6afed8d18980ad input=0d2803af1158b23c]*/
/*[clinic end generated code: output=4e6afed8d18980ad input=ef04b0f787e1f5f0]*/
{
CALL_HASHLIB_NEW(module, Py_hash_shake_128, data, string, usedforsecurity);
}

/*[clinic input]
@permit_long_summary
@text_signature "($module, /, data=b'', *, usedforsecurity=True)"
_hashlib.openssl_shake_256

data: object(c_default="NULL") = b''
Expand All @@ -1619,7 +1632,7 @@ Returns a shake-256 variable hash object; optionally initialized with a string
static PyObject *
_hashlib_openssl_shake_256_impl(PyObject *module, PyObject *data,
int usedforsecurity, PyObject *string)
/*[clinic end generated code: output=62481bce4a77d16c input=f27b98d9c749f55d]*/
/*[clinic end generated code: output=62481bce4a77d16c input=38a2429ec6143f98]*/
{
CALL_HASHLIB_NEW(module, Py_hash_shake_256, data, string, usedforsecurity);
}
Expand Down
6 changes: 4 additions & 2 deletions Modules/blake2module.c
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ py_blake2_new(PyTypeObject *type, PyObject *data, int digest_size,

/*[clinic input]
@classmethod
@text_signature "(data=b'', *, digest_size=_blake2.blake2b.MAX_DIGEST_SIZE, key=b'', salt=b'', person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, node_depth=0, inner_size=0, last_node=False, usedforsecurity=True)"
_blake2.blake2b.__new__ as py_blake2b_new
data as data_obj: object(c_default="NULL") = b''
*
Expand Down Expand Up @@ -693,7 +694,7 @@ py_blake2b_new_impl(PyTypeObject *type, PyObject *data_obj, int digest_size,
unsigned long long node_offset, int node_depth,
int inner_size, int last_node, int usedforsecurity,
PyObject *string)
/*[clinic end generated code: output=de64bd850606b6a0 input=32832fb37d13c03d]*/
/*[clinic end generated code: output=de64bd850606b6a0 input=3910410fa9a13dea]*/
{
PyObject *data;
if (_Py_hashlib_data_argument(&data, data_obj, string) < 0) {
Expand All @@ -706,6 +707,7 @@ py_blake2b_new_impl(PyTypeObject *type, PyObject *data_obj, int digest_size,

/*[clinic input]
@classmethod
@text_signature "(data=b'', *, digest_size=_blake2.blake2s.MAX_DIGEST_SIZE, key=b'', salt=b'', person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, node_depth=0, inner_size=0, last_node=False, usedforsecurity=True)"
_blake2.blake2s.__new__ as py_blake2s_new
data as data_obj: object(c_default="NULL") = b''
*
Expand Down Expand Up @@ -733,7 +735,7 @@ py_blake2s_new_impl(PyTypeObject *type, PyObject *data_obj, int digest_size,
unsigned long long node_offset, int node_depth,
int inner_size, int last_node, int usedforsecurity,
PyObject *string)
/*[clinic end generated code: output=582a0c4295cc3a3c input=da467fc9dae646bb]*/
/*[clinic end generated code: output=582a0c4295cc3a3c input=a95acd771b2aa71f]*/
{
PyObject *data;
if (_Py_hashlib_data_argument(&data, data_obj, string) < 0) {
Expand Down
Loading
Loading