Skip to content

Commit f631476

Browse files
committed
use 'defining_class' when copying HACL*-based hashes
1 parent 0d9d489 commit f631476

File tree

5 files changed

+54
-17
lines changed

5 files changed

+54
-17
lines changed

Lib/test/test_hashlib.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,24 @@ def shake_constructors(self):
216216
def is_fips_mode(self):
217217
return get_fips_mode()
218218

219+
def match_digest(self, h1, h2, *, shake_size=16):
220+
self.assertIs(type(h1), type(h2))
221+
self.assertEqual(h1.name, h2.name)
222+
223+
if h1.name in self.shakes:
224+
d1, h1 = h1.digest(shake_size), h1.hexdigest(shake_size)
225+
d2, h2 = h2.digest(shake_size), h2.hexdigest(shake_size)
226+
else:
227+
d1, h1 = h1.digest(), h1.hexdigest()
228+
d2, h2 = h2.digest(), h2.hexdigest()
229+
230+
self.assertIsInstance(d1, bytes)
231+
self.assertIsInstance(h1, str)
232+
self.assertEqual(d1.hex(), h1)
233+
234+
self.assertEqual(d1, d2)
235+
self.assertEqual(h1, h2)
236+
219237
def test_hash_array(self):
220238
a = array.array("b", range(10))
221239
for cons in self.hash_constructors:
@@ -371,6 +389,14 @@ def test_get_builtin_constructor(self):
371389
self.assertIs(constructor, _md5.md5)
372390
self.assertEqual(sorted(builtin_constructor_cache), ['MD5', 'md5'])
373391

392+
def test_copy(self):
393+
for cons in self.hash_constructors:
394+
h1 = cons(usedforsecurity=False)
395+
h1.update(os.urandom(16))
396+
h2 = h1.copy()
397+
self.assertIs(type(h1), type(h2))
398+
self.match_digest(h1, h2)
399+
374400
def test_hexdigest(self):
375401
for cons in self.hash_constructors:
376402
h = cons(usedforsecurity=False)

Modules/blake2module.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -787,17 +787,19 @@ blake2_blake2b_copy_unlocked(Blake2Object *self, Blake2Object *cpy)
787787
/*[clinic input]
788788
_blake2.blake2b.copy
789789
790+
cls: defining_class
791+
790792
Return a copy of the hash object.
791793
[clinic start generated code]*/
792794

793795
static PyObject *
794-
_blake2_blake2b_copy_impl(Blake2Object *self)
795-
/*[clinic end generated code: output=622d1c56b91c50d8 input=e383c2d199fd8a2e]*/
796+
_blake2_blake2b_copy_impl(Blake2Object *self, PyTypeObject *cls)
797+
/*[clinic end generated code: output=5f8ea31c56c52287 input=f38f3475e9aec98d]*/
796798
{
797799
int rc;
798800
Blake2Object *cpy;
799801

800-
if ((cpy = new_Blake2Object(Py_TYPE(self))) == NULL) {
802+
if ((cpy = new_Blake2Object(cls)) == NULL) {
801803
return NULL;
802804
}
803805

Modules/clinic/blake2module.c.h

Lines changed: 9 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/clinic/sha3module.c.h

Lines changed: 9 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/sha3module.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,17 @@ SHA3_traverse(PyObject *self, visitproc visit, void *arg)
239239
/*[clinic input]
240240
_sha3.sha3_224.copy
241241
242+
cls: defining_class
243+
242244
Return a copy of the hash object.
243245
[clinic start generated code]*/
244246

245247
static PyObject *
246-
_sha3_sha3_224_copy_impl(SHA3object *self)
247-
/*[clinic end generated code: output=6c537411ecdcda4c input=93a44aaebea51ba8]*/
248+
_sha3_sha3_224_copy_impl(SHA3object *self, PyTypeObject *cls)
249+
/*[clinic end generated code: output=13958b44c244013e input=7134b4dc0a2fbcac]*/
248250
{
249251
SHA3object *newobj;
250-
251-
if ((newobj = newSHA3object(Py_TYPE(self))) == NULL) {
252+
if ((newobj = newSHA3object(cls)) == NULL) {
252253
return NULL;
253254
}
254255
HASHLIB_ACQUIRE_LOCK(self);

0 commit comments

Comments
 (0)