Skip to content

Commit 9b8c2e7

Browse files
bpo-34922: Fix integer overflow in the digest() and hexdigest() methods (GH-9751)
for the SHAKE algorithm in the hashlib module.
1 parent f1aa8ae commit 9b8c2e7

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

Lib/test/test_hashlib.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,19 @@ def test_hexdigest(self):
230230
self.assertIsInstance(h.digest(), bytes)
231231
self.assertEqual(hexstr(h.digest()), h.hexdigest())
232232

233+
def test_digest_length_overflow(self):
234+
# See issue #34922
235+
large_sizes = (2**29, 2**32-10, 2**32+10, 2**61, 2**64-10, 2**64+10)
236+
for cons in self.hash_constructors:
237+
h = cons()
238+
if h.name not in self.shakes:
239+
continue
240+
for digest in h.digest, h.hexdigest:
241+
self.assertRaises(ValueError, digest, -10)
242+
for length in large_sizes:
243+
with self.assertRaises((ValueError, OverflowError)):
244+
digest(length)
245+
233246
def test_name_attribute(self):
234247
for cons in self.hash_constructors:
235248
h = cons()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed integer overflow in the :meth:`~hashlib.shake.digest()` and
2+
:meth:`~hashlib.shake.hexdigest()` methods for the SHAKE algorithm
3+
in the :mod:`hashlib` module.

Modules/_sha3/sha3module.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,10 @@ _SHAKE_digest(SHA3object *self, unsigned long digestlen, int hex)
589589
int res;
590590
PyObject *result = NULL;
591591

592+
if (digestlen >= (1 << 29)) {
593+
PyErr_SetString(PyExc_ValueError, "length is too large");
594+
return NULL;
595+
}
592596
/* ExtractLane needs at least SHA3_MAX_DIGESTSIZE + SHA3_LANESIZE and
593597
* SHA3_LANESIZE extra space.
594598
*/

0 commit comments

Comments
 (0)