Skip to content

Commit cf45ee1

Browse files
committed
Issue #16113: SHA3: allocate extra memory for lane extraction and check return value of PyModule_Create()
1 parent 9dda0c9 commit cf45ee1

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

Modules/_sha3/sha3module.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
#endif
115115

116116
#define SHA3_MAX_DIGESTSIZE 64 /* 64 Bytes (512 Bits) for 224 to 512 */
117+
#define SHA3_LANESIZE 96 /* ExtractLane needs an extra 96 bytes */
117118
#define SHA3_state Keccak_HashInstance
118119
#define SHA3_init Keccak_HashInitialize
119120
#define SHA3_process Keccak_HashUpdate
@@ -310,7 +311,7 @@ static PyObject *
310311
_sha3_sha3_224_digest_impl(SHA3object *self)
311312
/*[clinic end generated code: output=fd531842e20b2d5b input=a5807917d219b30e]*/
312313
{
313-
unsigned char digest[SHA3_MAX_DIGESTSIZE];
314+
unsigned char digest[SHA3_MAX_DIGESTSIZE + SHA3_LANESIZE];
314315
SHA3_state temp;
315316
HashReturn res;
316317

@@ -337,7 +338,7 @@ static PyObject *
337338
_sha3_sha3_224_hexdigest_impl(SHA3object *self)
338339
/*[clinic end generated code: output=75ad03257906918d input=2d91bb6e0d114ee3]*/
339340
{
340-
unsigned char digest[SHA3_MAX_DIGESTSIZE];
341+
unsigned char digest[SHA3_MAX_DIGESTSIZE + SHA3_LANESIZE];
341342
SHA3_state temp;
342343
HashReturn res;
343344

@@ -601,7 +602,12 @@ _SHAKE_digest(SHA3object *self, unsigned long digestlen, int hex)
601602
int res;
602603
PyObject *result = NULL;
603604

604-
if ((digest = (unsigned char*)PyMem_Malloc(digestlen)) == NULL) {
605+
/* ExtractLane needs at least SHA3_MAX_DIGESTSIZE + SHA3_LANESIZE and
606+
* SHA3_LANESIZE extra space.
607+
*/
608+
digest = (unsigned char*)PyMem_Malloc(SHA3_LANESIZE +
609+
((digestlen > SHA3_MAX_DIGESTSIZE) ? digestlen : SHA3_MAX_DIGESTSIZE));
610+
if (digest == NULL) {
605611
return PyErr_NoMemory();
606612
}
607613

@@ -708,7 +714,9 @@ PyInit__sha3(void)
708714
{
709715
PyObject *m = NULL;
710716

711-
m = PyModule_Create(&_SHA3module);
717+
if ((m = PyModule_Create(&_SHA3module)) == NULL) {
718+
return NULL;
719+
}
712720

713721
#define init_sha3type(name, type) \
714722
do { \

0 commit comments

Comments
 (0)