[3.7] bpo-34922: Fix integer overflow in the digest() and hexdigest() methods (GH-9751) (GH-9798)

for the SHAKE algorithm in the hashlib module.
(cherry picked from commit 9b8c2e7676)
This commit is contained in:
Serhiy Storchaka 2018-10-11 08:06:36 +03:00 committed by GitHub
parent 57038bcb24
commit 8b040e5539
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 1 deletions

View File

@ -230,6 +230,20 @@ class HashLibTestCase(unittest.TestCase):
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:
with self.assertRaises((ValueError, OverflowError)):
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()

View File

@ -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.

View File

@ -594,7 +594,10 @@ _SHAKE_digest(SHA3object *self, PyObject *digestlen_obj, int hex)
if (digestlen == (unsigned long) -1 && PyErr_Occurred()) {
return 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.
*/