From 43e1eee55d4640579d279a9b97f8ed5a77545a73 Mon Sep 17 00:00:00 2001 From: Guido Vranken Date: Wed, 8 Jul 2020 01:04:37 +0200 Subject: [PATCH 1/2] Return MEMORY_E from wc_scrypt if allocation fails --- wolfcrypt/src/pwdbased.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/wolfcrypt/src/pwdbased.c b/wolfcrypt/src/pwdbased.c index c672c2285..cf82b1597 100644 --- a/wolfcrypt/src/pwdbased.c +++ b/wolfcrypt/src/pwdbased.c @@ -721,16 +721,22 @@ int wc_scrypt(byte* output, const byte* passwd, int passLen, bSz = 128 * blockSize; blocksSz = bSz * parallel; blocks = (byte*)XMALLOC(blocksSz, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (blocks == NULL) + if (blocks == NULL) { + ret = MEMORY_E; goto end; + } /* Temporary for scryptROMix. */ v = (byte*)XMALLOC((1 << cost) * bSz, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (v == NULL) + if (v == NULL) { + ret = MEMORY_E; goto end; + } /* Temporary for scryptBlockMix. */ y = (byte*)XMALLOC(blockSize * 128, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (y == NULL) + if (y == NULL) { + ret = MEMORY_E; goto end; + } /* Step 1. */ ret = wc_PBKDF2(blocks, passwd, passLen, salt, saltLen, 1, blocksSz, From 7f6667144900dead5f13be22e7886eaa183d9a4b Mon Sep 17 00:00:00 2001 From: Guido Vranken Date: Wed, 8 Jul 2020 01:38:02 +0200 Subject: [PATCH 2/2] In wc_PKCS12_PBKDF_ex, break out of outer loop on error --- wolfcrypt/src/pwdbased.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/wolfcrypt/src/pwdbased.c b/wolfcrypt/src/pwdbased.c index cf82b1597..541ebca67 100644 --- a/wolfcrypt/src/pwdbased.c +++ b/wolfcrypt/src/pwdbased.c @@ -501,6 +501,8 @@ int wc_PKCS12_PBKDF_ex(byte* output, const byte* passwd, int passLen, if (ret < 0) break; } + if (ret < 0) break; + currentLen = min(kLen, (int)u); XMEMCPY(output, Ai, currentLen); output += currentLen;