Skip to content

bpo-34922: Fixed integer overflow in the digest() and hexdigest() methods #9751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
13 changes: 13 additions & 0 deletions Lib/test/test_hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,19 @@ def test_hexdigest(self):
self.assertIsInstance(h.digest(), bytes)
self.assertEqual(hexstr(h.digest()), h.hexdigest())

def test_digest_length_overflow(self):
# See issue #34922
large_sizes = (2**29, 2**32-10, 2**32+10, 2**61, 2**64-10, 2**64+10)
for cons in self.hash_constructors:
h = cons()
if h.name not in self.shakes:
continue
for digest in h.digest, h.hexdigest:
self.assertRaises(ValueError, digest, -10)
for length in large_sizes:
with self.assertRaises((ValueError, OverflowError)):
digest(length)

def test_name_attribute(self):
for cons in self.hash_constructors:
h = cons()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed integer overflow in the :meth:`~hashlib.shake.digest()` and
:meth:`~hashlib.shake.hexdigest()` methods for the SHAKE algorithm
in the :mod:`hashlib` module.
4 changes: 4 additions & 0 deletions Modules/_sha3/sha3module.c
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,10 @@ _SHAKE_digest(SHA3object *self, unsigned long digestlen, int hex)
int res;
PyObject *result = NULL;

if (digestlen >= (1 << 29)) {
PyErr_SetString(PyExc_ValueError, "length is too large");
return NULL;
}
/* ExtractLane needs at least SHA3_MAX_DIGESTSIZE + SHA3_LANESIZE and
* SHA3_LANESIZE extra space.
*/
Expand Down