Skip to content

Commit 996d232

Browse files
committed
align clinic signatures of HACL* hash functions with OpenSSL ones
1 parent 3bffada commit 996d232

File tree

14 files changed

+229
-147
lines changed

14 files changed

+229
-147
lines changed

Lib/test/test_hashlib.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ def test_usedforsecurity_false(self):
249249
self._hashlib.new("md5", usedforsecurity=False)
250250
self._hashlib.openssl_md5(usedforsecurity=False)
251251

252+
def test_clinic_signature(self):
253+
for constructor in self.hash_constructors:
254+
with self.subTest(constructor):
255+
constructor(data=b'')
256+
252257
def test_unknown_hash(self):
253258
self.assertRaises(ValueError, hashlib.new, 'spam spam spam spam spam')
254259
self.assertRaises(TypeError, hashlib.new, 1)
@@ -718,7 +723,6 @@ def check_blake2(self, constructor, salt_size, person_size, key_size,
718723
self.assertRaises(ValueError, constructor, node_offset=-1)
719724
self.assertRaises(OverflowError, constructor, node_offset=max_offset+1)
720725

721-
self.assertRaises(TypeError, constructor, data=b'')
722726
self.assertRaises(TypeError, constructor, string=b'')
723727
self.assertRaises(TypeError, constructor, '')
724728

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

Modules/_hashopenssl.c

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ py_evp_fromname(PyObject *module, const char *digestname, PyObject *data_obj,
10241024
_hashlib.new as EVP_new
10251025
10261026
name as name_obj: object
1027-
string as data_obj: object(c_default="NULL") = b''
1027+
data as data_obj: object(c_default="NULL") = b''
10281028
*
10291029
usedforsecurity: bool = True
10301030
@@ -1039,7 +1039,7 @@ The MD5 and SHA1 algorithms are always supported.
10391039
static PyObject *
10401040
EVP_new_impl(PyObject *module, PyObject *name_obj, PyObject *data_obj,
10411041
int usedforsecurity)
1042-
/*[clinic end generated code: output=ddd5053f92dffe90 input=c24554d0337be1b0]*/
1042+
/*[clinic end generated code: output=ddd5053f92dffe90 input=fc1f4513bde332d1]*/
10431043
{
10441044
char *name;
10451045
if (!PyArg_Parse(name_obj, "s", &name)) {
@@ -1053,7 +1053,7 @@ EVP_new_impl(PyObject *module, PyObject *name_obj, PyObject *data_obj,
10531053
/*[clinic input]
10541054
_hashlib.openssl_md5
10551055
1056-
string as data_obj: object(py_default="b''") = NULL
1056+
data as data_obj: object(py_default="b''") = NULL
10571057
*
10581058
usedforsecurity: bool = True
10591059
@@ -1064,7 +1064,7 @@ Returns a md5 hash object; optionally initialized with a string
10641064
static PyObject *
10651065
_hashlib_openssl_md5_impl(PyObject *module, PyObject *data_obj,
10661066
int usedforsecurity)
1067-
/*[clinic end generated code: output=87b0186440a44f8c input=990e36d5e689b16e]*/
1067+
/*[clinic end generated code: output=87b0186440a44f8c input=541743b9302c3a1d]*/
10681068
{
10691069
return py_evp_fromname(module, Py_hash_md5, data_obj, usedforsecurity);
10701070
}
@@ -1073,7 +1073,7 @@ _hashlib_openssl_md5_impl(PyObject *module, PyObject *data_obj,
10731073
/*[clinic input]
10741074
_hashlib.openssl_sha1
10751075
1076-
string as data_obj: object(py_default="b''") = NULL
1076+
data as data_obj: object(py_default="b''") = NULL
10771077
*
10781078
usedforsecurity: bool = True
10791079
@@ -1084,7 +1084,7 @@ Returns a sha1 hash object; optionally initialized with a string
10841084
static PyObject *
10851085
_hashlib_openssl_sha1_impl(PyObject *module, PyObject *data_obj,
10861086
int usedforsecurity)
1087-
/*[clinic end generated code: output=6813024cf690670d input=948f2f4b6deabc10]*/
1087+
/*[clinic end generated code: output=6813024cf690670d input=5cbd1a022f397432]*/
10881088
{
10891089
return py_evp_fromname(module, Py_hash_sha1, data_obj, usedforsecurity);
10901090
}
@@ -1093,7 +1093,7 @@ _hashlib_openssl_sha1_impl(PyObject *module, PyObject *data_obj,
10931093
/*[clinic input]
10941094
_hashlib.openssl_sha224
10951095
1096-
string as data_obj: object(py_default="b''") = NULL
1096+
data as data_obj: object(py_default="b''") = NULL
10971097
*
10981098
usedforsecurity: bool = True
10991099
@@ -1104,7 +1104,7 @@ Returns a sha224 hash object; optionally initialized with a string
11041104
static PyObject *
11051105
_hashlib_openssl_sha224_impl(PyObject *module, PyObject *data_obj,
11061106
int usedforsecurity)
1107-
/*[clinic end generated code: output=a2dfe7cc4eb14ebb input=f9272821fadca505]*/
1107+
/*[clinic end generated code: output=a2dfe7cc4eb14ebb input=9d70f1b767e0da1a]*/
11081108
{
11091109
return py_evp_fromname(module, Py_hash_sha224, data_obj, usedforsecurity);
11101110
}
@@ -1113,7 +1113,7 @@ _hashlib_openssl_sha224_impl(PyObject *module, PyObject *data_obj,
11131113
/*[clinic input]
11141114
_hashlib.openssl_sha256
11151115
1116-
string as data_obj: object(py_default="b''") = NULL
1116+
data as data_obj: object(py_default="b''") = NULL
11171117
*
11181118
usedforsecurity: bool = True
11191119
@@ -1124,7 +1124,7 @@ Returns a sha256 hash object; optionally initialized with a string
11241124
static PyObject *
11251125
_hashlib_openssl_sha256_impl(PyObject *module, PyObject *data_obj,
11261126
int usedforsecurity)
1127-
/*[clinic end generated code: output=1f874a34870f0a68 input=549fad9d2930d4c5]*/
1127+
/*[clinic end generated code: output=1f874a34870f0a68 input=6baf2fb84aa40b28]*/
11281128
{
11291129
return py_evp_fromname(module, Py_hash_sha256, data_obj, usedforsecurity);
11301130
}
@@ -1133,7 +1133,7 @@ _hashlib_openssl_sha256_impl(PyObject *module, PyObject *data_obj,
11331133
/*[clinic input]
11341134
_hashlib.openssl_sha384
11351135
1136-
string as data_obj: object(py_default="b''") = NULL
1136+
data as data_obj: object(py_default="b''") = NULL
11371137
*
11381138
usedforsecurity: bool = True
11391139
@@ -1144,7 +1144,7 @@ Returns a sha384 hash object; optionally initialized with a string
11441144
static PyObject *
11451145
_hashlib_openssl_sha384_impl(PyObject *module, PyObject *data_obj,
11461146
int usedforsecurity)
1147-
/*[clinic end generated code: output=58529eff9ca457b2 input=48601a6e3bf14ad7]*/
1147+
/*[clinic end generated code: output=58529eff9ca457b2 input=416c85fc491fd51a]*/
11481148
{
11491149
return py_evp_fromname(module, Py_hash_sha384, data_obj, usedforsecurity);
11501150
}
@@ -1153,7 +1153,7 @@ _hashlib_openssl_sha384_impl(PyObject *module, PyObject *data_obj,
11531153
/*[clinic input]
11541154
_hashlib.openssl_sha512
11551155
1156-
string as data_obj: object(py_default="b''") = NULL
1156+
data as data_obj: object(py_default="b''") = NULL
11571157
*
11581158
usedforsecurity: bool = True
11591159
@@ -1164,7 +1164,7 @@ Returns a sha512 hash object; optionally initialized with a string
11641164
static PyObject *
11651165
_hashlib_openssl_sha512_impl(PyObject *module, PyObject *data_obj,
11661166
int usedforsecurity)
1167-
/*[clinic end generated code: output=2c744c9e4a40d5f6 input=c5c46a2a817aa98f]*/
1167+
/*[clinic end generated code: output=2c744c9e4a40d5f6 input=db07e145351f0eb3]*/
11681168
{
11691169
return py_evp_fromname(module, Py_hash_sha512, data_obj, usedforsecurity);
11701170
}
@@ -1175,7 +1175,7 @@ _hashlib_openssl_sha512_impl(PyObject *module, PyObject *data_obj,
11751175
/*[clinic input]
11761176
_hashlib.openssl_sha3_224
11771177
1178-
string as data_obj: object(py_default="b''") = NULL
1178+
data as data_obj: object(py_default="b''") = NULL
11791179
*
11801180
usedforsecurity: bool = True
11811181
@@ -1186,15 +1186,15 @@ Returns a sha3-224 hash object; optionally initialized with a string
11861186
static PyObject *
11871187
_hashlib_openssl_sha3_224_impl(PyObject *module, PyObject *data_obj,
11881188
int usedforsecurity)
1189-
/*[clinic end generated code: output=144641c1d144b974 input=e3a01b2888916157]*/
1189+
/*[clinic end generated code: output=144641c1d144b974 input=8473784706f2bcfa]*/
11901190
{
11911191
return py_evp_fromname(module, Py_hash_sha3_224, data_obj, usedforsecurity);
11921192
}
11931193

11941194
/*[clinic input]
11951195
_hashlib.openssl_sha3_256
11961196
1197-
string as data_obj: object(py_default="b''") = NULL
1197+
data as data_obj: object(py_default="b''") = NULL
11981198
*
11991199
usedforsecurity: bool = True
12001200
@@ -1205,15 +1205,15 @@ Returns a sha3-256 hash object; optionally initialized with a string
12051205
static PyObject *
12061206
_hashlib_openssl_sha3_256_impl(PyObject *module, PyObject *data_obj,
12071207
int usedforsecurity)
1208-
/*[clinic end generated code: output=c61f1ab772d06668 input=e2908126c1b6deed]*/
1208+
/*[clinic end generated code: output=c61f1ab772d06668 input=2c0433e4af5389af]*/
12091209
{
12101210
return py_evp_fromname(module, Py_hash_sha3_256, data_obj , usedforsecurity);
12111211
}
12121212

12131213
/*[clinic input]
12141214
_hashlib.openssl_sha3_384
12151215
1216-
string as data_obj: object(py_default="b''") = NULL
1216+
data as data_obj: object(py_default="b''") = NULL
12171217
*
12181218
usedforsecurity: bool = True
12191219
@@ -1224,15 +1224,15 @@ Returns a sha3-384 hash object; optionally initialized with a string
12241224
static PyObject *
12251225
_hashlib_openssl_sha3_384_impl(PyObject *module, PyObject *data_obj,
12261226
int usedforsecurity)
1227-
/*[clinic end generated code: output=f68e4846858cf0ee input=ec0edf5c792f8252]*/
1227+
/*[clinic end generated code: output=f68e4846858cf0ee input=15cf1a0f31210933]*/
12281228
{
12291229
return py_evp_fromname(module, Py_hash_sha3_384, data_obj , usedforsecurity);
12301230
}
12311231

12321232
/*[clinic input]
12331233
_hashlib.openssl_sha3_512
12341234
1235-
string as data_obj: object(py_default="b''") = NULL
1235+
data as data_obj: object(py_default="b''") = NULL
12361236
*
12371237
usedforsecurity: bool = True
12381238
@@ -1243,7 +1243,7 @@ Returns a sha3-512 hash object; optionally initialized with a string
12431243
static PyObject *
12441244
_hashlib_openssl_sha3_512_impl(PyObject *module, PyObject *data_obj,
12451245
int usedforsecurity)
1246-
/*[clinic end generated code: output=2eede478c159354a input=64e2cc0c094d56f4]*/
1246+
/*[clinic end generated code: output=2eede478c159354a input=3c49cbee1c406eb8]*/
12471247
{
12481248
return py_evp_fromname(module, Py_hash_sha3_512, data_obj , usedforsecurity);
12491249
}
@@ -1253,7 +1253,7 @@ _hashlib_openssl_sha3_512_impl(PyObject *module, PyObject *data_obj,
12531253
/*[clinic input]
12541254
_hashlib.openssl_shake_128
12551255
1256-
string as data_obj: object(py_default="b''") = NULL
1256+
data as data_obj: object(py_default="b''") = NULL
12571257
*
12581258
usedforsecurity: bool = True
12591259
@@ -1264,15 +1264,15 @@ Returns a shake-128 variable hash object; optionally initialized with a string
12641264
static PyObject *
12651265
_hashlib_openssl_shake_128_impl(PyObject *module, PyObject *data_obj,
12661266
int usedforsecurity)
1267-
/*[clinic end generated code: output=bc49cdd8ada1fa97 input=6c9d67440eb33ec8]*/
1267+
/*[clinic end generated code: output=bc49cdd8ada1fa97 input=ee01cb8679a1e230]*/
12681268
{
12691269
return py_evp_fromname(module, Py_hash_shake_128, data_obj , usedforsecurity);
12701270
}
12711271

12721272
/*[clinic input]
12731273
_hashlib.openssl_shake_256
12741274
1275-
string as data_obj: object(py_default="b''") = NULL
1275+
data as data_obj: object(py_default="b''") = NULL
12761276
*
12771277
usedforsecurity: bool = True
12781278
@@ -1283,7 +1283,7 @@ Returns a shake-256 variable hash object; optionally initialized with a string
12831283
static PyObject *
12841284
_hashlib_openssl_shake_256_impl(PyObject *module, PyObject *data_obj,
12851285
int usedforsecurity)
1286-
/*[clinic end generated code: output=358d213be8852df7 input=479cbe9fefd4a9f8]*/
1286+
/*[clinic end generated code: output=358d213be8852df7 input=92e304b3733f14c8]*/
12871287
{
12881288
return py_evp_fromname(module, Py_hash_shake_256, data_obj , usedforsecurity);
12891289
}

Modules/blake2module.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,6 @@ py_blake2b_or_s_new(PyTypeObject *type, PyObject *data, int digest_size,
656656
@classmethod
657657
_blake2.blake2b.__new__ as py_blake2b_new
658658
data: object(c_default="NULL") = b''
659-
/
660659
*
661660
digest_size: int(c_default="HACL_HASH_BLAKE2B_OUT_BYTES") = _blake2.blake2b.MAX_DIGEST_SIZE
662661
key: Py_buffer(c_default="NULL", py_default="b''") = None
@@ -680,7 +679,7 @@ py_blake2b_new_impl(PyTypeObject *type, PyObject *data, int digest_size,
680679
int fanout, int depth, unsigned long leaf_size,
681680
unsigned long long node_offset, int node_depth,
682681
int inner_size, int last_node, int usedforsecurity)
683-
/*[clinic end generated code: output=32bfd8f043c6896f input=8fee2b7b11428b2d]*/
682+
/*[clinic end generated code: output=32bfd8f043c6896f input=02d342d22847290e]*/
684683
{
685684
return py_blake2b_or_s_new(type, data, digest_size, key, salt, person, fanout, depth, leaf_size, node_offset, node_depth, inner_size, last_node, usedforsecurity);
686685
}
@@ -689,7 +688,6 @@ py_blake2b_new_impl(PyTypeObject *type, PyObject *data, int digest_size,
689688
@classmethod
690689
_blake2.blake2s.__new__ as py_blake2s_new
691690
data: object(c_default="NULL") = b''
692-
/
693691
*
694692
digest_size: int(c_default="HACL_HASH_BLAKE2S_OUT_BYTES") = _blake2.blake2s.MAX_DIGEST_SIZE
695693
key: Py_buffer(c_default="NULL", py_default="b''") = None
@@ -713,7 +711,7 @@ py_blake2s_new_impl(PyTypeObject *type, PyObject *data, int digest_size,
713711
int fanout, int depth, unsigned long leaf_size,
714712
unsigned long long node_offset, int node_depth,
715713
int inner_size, int last_node, int usedforsecurity)
716-
/*[clinic end generated code: output=556181f73905c686 input=8165a11980eac7f3]*/
714+
/*[clinic end generated code: output=556181f73905c686 input=d88d8c06e859b073]*/
717715
{
718716
return py_blake2b_or_s_new(type, data, digest_size, key, salt, person, fanout, depth, leaf_size, node_offset, node_depth, inner_size, last_node, usedforsecurity);
719717
}

0 commit comments

Comments
 (0)