Skip to content

Commit 69e6ad6

Browse files
[3.6] bpo-34922: Fix integer overflow in the digest() and hexdigest() methods (GH-9751) (GH-9798) (GH-9801)
for the SHAKE algorithm in the hashlib module. (cherry picked from commit 9b8c2e7) (cherry picked from commit 8b040e5) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 1a23abe commit 69e6ad6

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

Lib/test/test_hashlib.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,20 @@ def test_hexdigest(self):
233233
self.assertIsInstance(h.digest(), bytes)
234234
self.assertEqual(hexstr(h.digest()), h.hexdigest())
235235

236+
def test_digest_length_overflow(self):
237+
# See issue #34922
238+
large_sizes = (2**29, 2**32-10, 2**32+10, 2**61, 2**64-10, 2**64+10)
239+
for cons in self.hash_constructors:
240+
h = cons()
241+
if h.name not in self.shakes:
242+
continue
243+
for digest in h.digest, h.hexdigest:
244+
with self.assertRaises((ValueError, OverflowError)):
245+
digest(-10)
246+
for length in large_sizes:
247+
with self.assertRaises((ValueError, OverflowError)):
248+
digest(length)
249+
236250
def test_name_attribute(self):
237251
for cons in self.hash_constructors:
238252
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,10 @@ _SHAKE_digest(SHA3object *self, PyObject *digestlen_obj, int hex)
609609
if (digestlen == (unsigned long) -1 && PyErr_Occurred()) {
610610
return NULL;
611611
}
612-
612+
if (digestlen >= (1 << 29)) {
613+
PyErr_SetString(PyExc_ValueError, "length is too large");
614+
return NULL;
615+
}
613616
/* ExtractLane needs at least SHA3_MAX_DIGESTSIZE + SHA3_LANESIZE and
614617
* SHA3_LANESIZE extra space.
615618
*/

0 commit comments

Comments
 (0)