diff --git a/wolfcrypt/src/sp_arm32.c b/wolfcrypt/src/sp_arm32.c index 97dbcb030..debadba77 100644 --- a/wolfcrypt/src/sp_arm32.c +++ b/wolfcrypt/src/sp_arm32.c @@ -4721,7 +4721,9 @@ static WC_INLINE int sp_2048_mod_32(sp_digit* r, const sp_digit* a, const sp_dig * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -4741,11 +4743,20 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 64), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 64), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -4861,7 +4872,9 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -4881,11 +4894,20 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 64), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 64), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -7331,7 +7353,9 @@ static WC_INLINE int sp_2048_mod_64(sp_digit* r, const sp_digit* a, const sp_dig * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -7351,11 +7375,20 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 128), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 128), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -7462,7 +7495,9 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -7482,11 +7517,20 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 128), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 128), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -8644,7 +8688,9 @@ static void sp_2048_lshift_64(sp_digit* r, sp_digit* a, byte n) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) @@ -8665,11 +8711,17 @@ static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 193, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 193, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -15630,7 +15682,9 @@ static WC_INLINE int sp_3072_mod_48(sp_digit* r, const sp_digit* a, const sp_dig * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -15650,11 +15704,20 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 96), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 96), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -15770,7 +15833,9 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -15790,11 +15855,20 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 96), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 96), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -19168,7 +19242,9 @@ static WC_INLINE int sp_3072_mod_96(sp_digit* r, const sp_digit* a, const sp_dig * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -19188,11 +19264,20 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 192), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 192), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -19299,7 +19384,9 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -19319,11 +19406,20 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 192), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 192), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -20753,7 +20849,9 @@ static void sp_3072_lshift_96(sp_digit* r, sp_digit* a, byte n) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) @@ -20774,11 +20872,17 @@ static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 289, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 289, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -27647,7 +27751,9 @@ static WC_INLINE int sp_4096_mod_128(sp_digit* r, const sp_digit* a, const sp_di * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -27667,11 +27773,20 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 256), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 256), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -27778,7 +27893,9 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -27798,11 +27915,20 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 256), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 256), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -29504,7 +29630,9 @@ static void sp_4096_lshift_128(sp_digit* r, sp_digit* a, byte n) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) @@ -29525,11 +29653,17 @@ static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 385, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 385, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { diff --git a/wolfcrypt/src/sp_arm64.c b/wolfcrypt/src/sp_arm64.c index 6df8b49f9..fc1752b9d 100644 --- a/wolfcrypt/src/sp_arm64.c +++ b/wolfcrypt/src/sp_arm64.c @@ -3223,7 +3223,9 @@ static WC_INLINE int sp_2048_mod_16(sp_digit* r, const sp_digit* a, const sp_dig * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -3243,11 +3245,20 @@ static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 32), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 32), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -3363,7 +3374,9 @@ static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -3383,11 +3396,20 @@ static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 32), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 32), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -4788,7 +4810,9 @@ static WC_INLINE int sp_2048_mod_32(sp_digit* r, const sp_digit* a, const sp_dig * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -4808,11 +4832,20 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 64), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 64), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -4919,7 +4952,9 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -4939,11 +4974,20 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 64), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 64), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -5806,7 +5850,9 @@ static void sp_2048_lshift_32(sp_digit* r, sp_digit* a, byte n) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_2048_mod_exp_2_32(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) @@ -5827,11 +5873,17 @@ static int sp_2048_mod_exp_2_32(sp_digit* r, const sp_digit* e, int bits, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 97, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 97, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -10794,7 +10846,9 @@ static WC_INLINE int sp_3072_mod_24(sp_digit* r, const sp_digit* a, const sp_dig * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -10814,11 +10868,20 @@ static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 48), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 48), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -10934,7 +10997,9 @@ static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -10954,11 +11019,20 @@ static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 48), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 48), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -12783,7 +12857,9 @@ static WC_INLINE int sp_3072_mod_48(sp_digit* r, const sp_digit* a, const sp_dig * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -12803,11 +12879,20 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 96), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 96), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -12914,7 +12999,9 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -12934,11 +13021,20 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 96), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 96), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -13925,7 +14021,9 @@ static void sp_3072_lshift_48(sp_digit* r, sp_digit* a, byte n) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_3072_mod_exp_2_48(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) @@ -13946,11 +14044,17 @@ static int sp_3072_mod_exp_2_48(sp_digit* r, const sp_digit* e, int bits, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 145, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 145, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -17963,7 +18067,9 @@ static WC_INLINE int sp_4096_mod_64(sp_digit* r, const sp_digit* a, const sp_dig * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -17983,11 +18089,20 @@ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 128), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 128), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -18094,7 +18209,9 @@ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -18114,11 +18231,20 @@ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 128), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 128), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -19229,7 +19355,9 @@ static void sp_4096_lshift_64(sp_digit* r, sp_digit* a, byte n) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_4096_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) @@ -19250,11 +19378,17 @@ static int sp_4096_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 193, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 193, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { diff --git a/wolfcrypt/src/sp_armthumb.c b/wolfcrypt/src/sp_armthumb.c index 85e2bb832..d2d8f6276 100644 --- a/wolfcrypt/src/sp_armthumb.c +++ b/wolfcrypt/src/sp_armthumb.c @@ -23780,7 +23780,9 @@ static WC_INLINE int sp_2048_mod_32(sp_digit* r, const sp_digit* a, const sp_dig * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -23800,11 +23802,20 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 64), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 64), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -23920,7 +23931,9 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -23940,11 +23953,20 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 64), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 64), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -27211,7 +27233,9 @@ static WC_INLINE int sp_2048_mod_64(sp_digit* r, const sp_digit* a, const sp_dig * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -27231,11 +27255,20 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 128), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 128), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -27342,7 +27375,9 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -27362,11 +27397,20 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 128), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 128), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -29583,7 +29627,9 @@ static void sp_2048_lshift_64(sp_digit* r, const sp_digit* a, byte n) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) @@ -29604,11 +29650,17 @@ static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 193, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 193, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -74780,7 +74832,9 @@ static WC_INLINE int sp_3072_mod_48(sp_digit* r, const sp_digit* a, const sp_dig * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -74800,11 +74854,20 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 96), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 96), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -74920,7 +74983,9 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -74940,11 +75005,20 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 96), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 96), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -79042,7 +79116,9 @@ static WC_INLINE int sp_3072_mod_96(sp_digit* r, const sp_digit* a, const sp_dig * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -79062,11 +79138,20 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 192), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 192), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -79173,7 +79258,9 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -79193,11 +79280,20 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 192), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 192), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -82212,7 +82308,9 @@ static void sp_3072_lshift_96(sp_digit* r, const sp_digit* a, byte n) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) @@ -82233,11 +82331,17 @@ static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 289, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 289, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -91386,7 +91490,9 @@ static WC_INLINE int sp_4096_mod_128(sp_digit* r, const sp_digit* a, const sp_di * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -91406,11 +91512,20 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 256), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 256), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -91517,7 +91632,9 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -91537,11 +91654,20 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 256), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 256), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -95349,7 +95475,9 @@ static void sp_4096_lshift_128(sp_digit* r, const sp_digit* a, byte n) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) @@ -95370,11 +95498,17 @@ static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 385, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 385, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { diff --git a/wolfcrypt/src/sp_c32.c b/wolfcrypt/src/sp_c32.c index 31a6d14aa..40b3334ff 100644 --- a/wolfcrypt/src/sp_c32.c +++ b/wolfcrypt/src/sp_c32.c @@ -2138,7 +2138,9 @@ static int sp_2048_mod_36(sp_digit* r, const sp_digit* a, const sp_digit* m) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -2158,11 +2160,20 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 36 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 36 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -2244,11 +2255,20 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 36 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 36 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -2330,11 +2350,20 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 72) + 72), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 72) + 72), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -3233,7 +3262,9 @@ static int sp_2048_mod_72(sp_digit* r, const sp_digit* a, const sp_digit* m) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_72(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -3253,11 +3284,20 @@ static int sp_2048_mod_exp_72(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 72 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 72 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -3339,11 +3379,20 @@ static int sp_2048_mod_exp_72(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 72 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 72 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -3425,11 +3474,20 @@ static int sp_2048_mod_exp_72(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 144) + 144), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 144) + 144), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -4518,7 +4576,9 @@ SP_NOINLINE static void sp_2048_lshift_72(sp_digit* r, const sp_digit* a, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_2048_mod_exp_2_72(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { @@ -4537,11 +4597,17 @@ static int sp_2048_mod_exp_2_72(sp_digit* r, const sp_digit* e, int bits, const byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 217, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 217, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -5867,7 +5933,9 @@ static int sp_3072_mod_53(sp_digit* r, const sp_digit* a, const sp_digit* m) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_53(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -5887,11 +5955,20 @@ static int sp_3072_mod_exp_53(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 53 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 53 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -5973,11 +6050,20 @@ static int sp_3072_mod_exp_53(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 53 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 53 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -6059,11 +6145,20 @@ static int sp_3072_mod_exp_53(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 106) + 106), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 106) + 106), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -6742,7 +6837,9 @@ static int sp_3072_mod_106(sp_digit* r, const sp_digit* a, const sp_digit* m) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_106(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -6762,11 +6859,20 @@ static int sp_3072_mod_exp_106(sp_digit* r, const sp_digit* a, const sp_digit* e byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 106 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 106 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -6848,11 +6954,20 @@ static int sp_3072_mod_exp_106(sp_digit* r, const sp_digit* a, const sp_digit* e byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 106 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 106 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -6934,11 +7049,20 @@ static int sp_3072_mod_exp_106(sp_digit* r, const sp_digit* a, const sp_digit* e byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 212) + 212), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 212) + 212), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -7875,7 +7999,9 @@ SP_NOINLINE static void sp_3072_lshift_106(sp_digit* r, const sp_digit* a, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_3072_mod_exp_2_106(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { @@ -7894,11 +8020,17 @@ static int sp_3072_mod_exp_2_106(sp_digit* r, const sp_digit* e, int bits, const byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 319, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 319, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -9909,7 +10041,9 @@ static int sp_3072_mod_56(sp_digit* r, const sp_digit* a, const sp_digit* m) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_56(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -9929,11 +10063,20 @@ static int sp_3072_mod_exp_56(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 56 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 56 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -10015,11 +10158,20 @@ static int sp_3072_mod_exp_56(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 56 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 56 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -10101,11 +10253,20 @@ static int sp_3072_mod_exp_56(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 112) + 112), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 112) + 112), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -10832,7 +10993,9 @@ static int sp_3072_mod_112(sp_digit* r, const sp_digit* a, const sp_digit* m) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_112(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -10852,11 +11015,20 @@ static int sp_3072_mod_exp_112(sp_digit* r, const sp_digit* a, const sp_digit* e byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 112 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 112 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -10938,11 +11110,20 @@ static int sp_3072_mod_exp_112(sp_digit* r, const sp_digit* a, const sp_digit* e byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 112 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 112 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -11024,11 +11205,20 @@ static int sp_3072_mod_exp_112(sp_digit* r, const sp_digit* a, const sp_digit* e byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 224) + 224), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 224) + 224), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -12188,7 +12378,9 @@ SP_NOINLINE static void sp_3072_lshift_112(sp_digit* r, const sp_digit* a, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_3072_mod_exp_2_112(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { @@ -12207,11 +12399,17 @@ static int sp_3072_mod_exp_2_112(sp_digit* r, const sp_digit* e, int bits, const byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 337, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 337, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -13547,7 +13745,9 @@ static int sp_4096_mod_71(sp_digit* r, const sp_digit* a, const sp_digit* m) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_4096_mod_exp_71(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -13567,11 +13767,20 @@ static int sp_4096_mod_exp_71(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 71 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 71 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -13653,11 +13862,20 @@ static int sp_4096_mod_exp_71(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 71 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 71 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -13739,11 +13957,20 @@ static int sp_4096_mod_exp_71(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 142) + 142), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 142) + 142), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -14423,7 +14650,9 @@ static int sp_4096_mod_142(sp_digit* r, const sp_digit* a, const sp_digit* m) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_4096_mod_exp_142(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -14443,11 +14672,20 @@ static int sp_4096_mod_exp_142(sp_digit* r, const sp_digit* a, const sp_digit* e byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 142 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 142 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -14529,11 +14767,20 @@ static int sp_4096_mod_exp_142(sp_digit* r, const sp_digit* a, const sp_digit* e byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 142 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 142 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -14615,11 +14862,20 @@ static int sp_4096_mod_exp_142(sp_digit* r, const sp_digit* a, const sp_digit* e byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 284) + 284), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 284) + 284), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -15556,7 +15812,9 @@ SP_NOINLINE static void sp_4096_lshift_142(sp_digit* r, const sp_digit* a, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_4096_mod_exp_2_142(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { @@ -15575,11 +15833,17 @@ static int sp_4096_mod_exp_2_142(sp_digit* r, const sp_digit* e, int bits, const byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 427, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 427, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -17445,7 +17709,9 @@ static int sp_4096_mod_81(sp_digit* r, const sp_digit* a, const sp_digit* m) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_4096_mod_exp_81(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -17465,11 +17731,20 @@ static int sp_4096_mod_exp_81(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 81 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 81 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -17551,11 +17826,20 @@ static int sp_4096_mod_exp_81(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 81 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 81 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -17637,11 +17921,20 @@ static int sp_4096_mod_exp_81(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 162) + 162), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 162) + 162), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -18321,7 +18614,9 @@ static int sp_4096_mod_162(sp_digit* r, const sp_digit* a, const sp_digit* m) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_4096_mod_exp_162(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -18341,11 +18636,20 @@ static int sp_4096_mod_exp_162(sp_digit* r, const sp_digit* a, const sp_digit* e byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 162 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 162 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -18427,11 +18731,20 @@ static int sp_4096_mod_exp_162(sp_digit* r, const sp_digit* a, const sp_digit* e byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 162 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 162 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -18513,11 +18826,20 @@ static int sp_4096_mod_exp_162(sp_digit* r, const sp_digit* a, const sp_digit* e byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 324) + 324), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 324) + 324), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -19777,7 +20099,9 @@ SP_NOINLINE static void sp_4096_lshift_162(sp_digit* r, const sp_digit* a, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_4096_mod_exp_2_162(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { @@ -19796,11 +20120,17 @@ static int sp_4096_mod_exp_2_162(sp_digit* r, const sp_digit* e, int bits, const byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 487, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 487, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { diff --git a/wolfcrypt/src/sp_c64.c b/wolfcrypt/src/sp_c64.c index 67ad87b03..da17fd50d 100644 --- a/wolfcrypt/src/sp_c64.c +++ b/wolfcrypt/src/sp_c64.c @@ -1198,7 +1198,9 @@ static int sp_2048_mod_17(sp_digit* r, const sp_digit* a, const sp_digit* m) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_17(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -1218,11 +1220,20 @@ static int sp_2048_mod_exp_17(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 17 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 17 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -1304,11 +1315,20 @@ static int sp_2048_mod_exp_17(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 17 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 17 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -1390,11 +1410,20 @@ static int sp_2048_mod_exp_17(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 34) + 34), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 34) + 34), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -2187,7 +2216,9 @@ static int sp_2048_mod_34(sp_digit* r, const sp_digit* a, const sp_digit* m) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_34(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -2207,11 +2238,20 @@ static int sp_2048_mod_exp_34(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 34 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 34 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -2293,11 +2333,20 @@ static int sp_2048_mod_exp_34(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 34 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 34 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -2379,11 +2428,20 @@ static int sp_2048_mod_exp_34(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 68) + 68), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 68) + 68), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -3320,7 +3378,9 @@ SP_NOINLINE static void sp_2048_lshift_34(sp_digit* r, const sp_digit* a, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_2048_mod_exp_2_34(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { @@ -3339,11 +3399,17 @@ static int sp_2048_mod_exp_2_34(sp_digit* r, const sp_digit* e, int bits, const byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 103, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 103, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -4862,7 +4928,9 @@ static int sp_2048_mod_18(sp_digit* r, const sp_digit* a, const sp_digit* m) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_18(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -4882,11 +4950,20 @@ static int sp_2048_mod_exp_18(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 18 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 18 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -4968,11 +5045,20 @@ static int sp_2048_mod_exp_18(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 18 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 18 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -5054,11 +5140,20 @@ static int sp_2048_mod_exp_18(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 36) + 36), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 36) + 36), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -5746,7 +5841,9 @@ static int sp_2048_mod_36(sp_digit* r, const sp_digit* a, const sp_digit* m) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -5766,11 +5863,20 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 36 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 36 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -5852,11 +5958,20 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 36 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 36 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -5938,11 +6053,20 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 72) + 72), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 72) + 72), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -6950,7 +7074,9 @@ SP_NOINLINE static void sp_2048_lshift_36(sp_digit* r, const sp_digit* a, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_2048_mod_exp_2_36(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { @@ -6969,11 +7095,17 @@ static int sp_2048_mod_exp_2_36(sp_digit* r, const sp_digit* e, int bits, const byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 109, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 109, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -8253,7 +8385,9 @@ static int sp_3072_mod_26(sp_digit* r, const sp_digit* a, const sp_digit* m) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_26(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -8273,11 +8407,20 @@ static int sp_3072_mod_exp_26(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 26 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 26 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -8359,11 +8502,20 @@ static int sp_3072_mod_exp_26(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 26 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 26 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -8445,11 +8597,20 @@ static int sp_3072_mod_exp_26(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 52) + 52), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 52) + 52), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -9160,7 +9321,9 @@ static int sp_3072_mod_52(sp_digit* r, const sp_digit* a, const sp_digit* m) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_52(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -9180,11 +9343,20 @@ static int sp_3072_mod_exp_52(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 52 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 52 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -9266,11 +9438,20 @@ static int sp_3072_mod_exp_52(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 52 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 52 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -9352,11 +9533,20 @@ static int sp_3072_mod_exp_52(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 104) + 104), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 104) + 104), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -10293,7 +10483,9 @@ SP_NOINLINE static void sp_3072_lshift_52(sp_digit* r, const sp_digit* a, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_3072_mod_exp_2_52(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { @@ -10312,11 +10504,17 @@ static int sp_3072_mod_exp_2_52(sp_digit* r, const sp_digit* e, int bits, const byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 157, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 157, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -11973,7 +12171,9 @@ static int sp_3072_mod_27(sp_digit* r, const sp_digit* a, const sp_digit* m) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_27(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -11993,11 +12193,20 @@ static int sp_3072_mod_exp_27(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 27 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 27 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -12079,11 +12288,20 @@ static int sp_3072_mod_exp_27(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 27 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 27 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -12165,11 +12383,20 @@ static int sp_3072_mod_exp_27(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 54) + 54), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 54) + 54), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -12868,7 +13095,9 @@ static int sp_3072_mod_54(sp_digit* r, const sp_digit* a, const sp_digit* m) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_54(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -12888,11 +13117,20 @@ static int sp_3072_mod_exp_54(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 54 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 54 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -12974,11 +13212,20 @@ static int sp_3072_mod_exp_54(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 54 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 54 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -13060,11 +13307,20 @@ static int sp_3072_mod_exp_54(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 108) + 108), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 108) + 108), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -14108,7 +14364,9 @@ SP_NOINLINE static void sp_3072_lshift_54(sp_digit* r, const sp_digit* a, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_3072_mod_exp_2_54(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { @@ -14127,11 +14385,17 @@ static int sp_3072_mod_exp_2_54(sp_digit* r, const sp_digit* e, int bits, const byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 163, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 163, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -15377,7 +15641,9 @@ static int sp_4096_mod_35(sp_digit* r, const sp_digit* a, const sp_digit* m) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_4096_mod_exp_35(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -15397,11 +15663,20 @@ static int sp_4096_mod_exp_35(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 35 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 35 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -15483,11 +15758,20 @@ static int sp_4096_mod_exp_35(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 35 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 35 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -15569,11 +15853,20 @@ static int sp_4096_mod_exp_35(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 70) + 70), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 70) + 70), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -16239,7 +16532,9 @@ static int sp_4096_mod_70(sp_digit* r, const sp_digit* a, const sp_digit* m) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_4096_mod_exp_70(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -16259,11 +16554,20 @@ static int sp_4096_mod_exp_70(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 70 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 70 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -16345,11 +16649,20 @@ static int sp_4096_mod_exp_70(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 70 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 70 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -16431,11 +16744,20 @@ static int sp_4096_mod_exp_70(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 140) + 140), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 140) + 140), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -17372,7 +17694,9 @@ SP_NOINLINE static void sp_4096_lshift_70(sp_digit* r, const sp_digit* a, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_4096_mod_exp_2_70(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { @@ -17391,11 +17715,17 @@ static int sp_4096_mod_exp_2_70(sp_digit* r, const sp_digit* e, int bits, const byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 211, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 211, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -19075,7 +19405,9 @@ static int sp_4096_mod_39(sp_digit* r, const sp_digit* a, const sp_digit* m) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_4096_mod_exp_39(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -19095,11 +19427,20 @@ static int sp_4096_mod_exp_39(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 39 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 39 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -19181,11 +19522,20 @@ static int sp_4096_mod_exp_39(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 39 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 39 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -19267,11 +19617,20 @@ static int sp_4096_mod_exp_39(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 78) + 78), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 78) + 78), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -19939,7 +20298,9 @@ static int sp_4096_mod_78(sp_digit* r, const sp_digit* a, const sp_digit* m) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_4096_mod_exp_78(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -19959,11 +20320,20 @@ static int sp_4096_mod_exp_78(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 78 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 78 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -20045,11 +20415,20 @@ static int sp_4096_mod_exp_78(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 78 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 78 * 2, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -20131,11 +20510,20 @@ static int sp_4096_mod_exp_78(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 156) + 156), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 156) + 156), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -21227,7 +21615,9 @@ SP_NOINLINE static void sp_4096_lshift_78(sp_digit* r, const sp_digit* a, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_4096_mod_exp_2_78(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { @@ -21246,11 +21636,17 @@ static int sp_4096_mod_exp_2_78(sp_digit* r, const sp_digit* e, int bits, const byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 235, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 235, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { diff --git a/wolfcrypt/src/sp_cortexm.c b/wolfcrypt/src/sp_cortexm.c index 19dfdb270..610f4e367 100644 --- a/wolfcrypt/src/sp_cortexm.c +++ b/wolfcrypt/src/sp_cortexm.c @@ -3183,7 +3183,9 @@ static WC_INLINE int sp_2048_mod_32(sp_digit* r, const sp_digit* a, const sp_dig * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -3203,11 +3205,20 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 64), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 64), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -3323,7 +3334,9 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -3343,11 +3356,20 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 64), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 64), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -4244,7 +4266,9 @@ static WC_INLINE int sp_2048_mod_64(sp_digit* r, const sp_digit* a, const sp_dig * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -4264,11 +4288,20 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 128), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 128), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -4375,7 +4408,9 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -4395,11 +4430,20 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 128), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 128), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -5400,7 +5444,9 @@ static void sp_2048_lshift_64(sp_digit* r, sp_digit* a, byte n) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) @@ -5421,11 +5467,17 @@ static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 193, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 193, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -8402,7 +8454,9 @@ static WC_INLINE int sp_3072_mod_48(sp_digit* r, const sp_digit* a, const sp_dig * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -8422,11 +8476,20 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 96), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 96), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -8542,7 +8605,9 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -8562,11 +8627,20 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 96), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 96), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -9601,7 +9675,9 @@ static WC_INLINE int sp_3072_mod_96(sp_digit* r, const sp_digit* a, const sp_dig * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -9621,11 +9697,20 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 192), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 192), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -9732,7 +9817,9 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -9752,11 +9839,20 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 192), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 192), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -10953,7 +11049,9 @@ static void sp_3072_lshift_96(sp_digit* r, sp_digit* a, byte n) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) @@ -10974,11 +11072,17 @@ static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 289, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 289, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -13516,7 +13620,9 @@ static WC_INLINE int sp_4096_mod_128(sp_digit* r, const sp_digit* a, const sp_di * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -13536,11 +13642,20 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 256), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 256), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -13647,7 +13762,9 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) @@ -13667,11 +13784,20 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 256), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 256), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { @@ -15065,7 +15191,9 @@ static void sp_4096_lshift_128(sp_digit* r, sp_digit* a, byte n) * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) @@ -15086,11 +15214,17 @@ static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, byte y; int err = MP_OKAY; + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 385, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 385, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) + err = MEMORY_E; + } #endif if (err == MP_OKAY) { diff --git a/wolfcrypt/src/sp_x86_64.c b/wolfcrypt/src/sp_x86_64.c index eedaa9652..d608fe2ac 100644 --- a/wolfcrypt/src/sp_x86_64.c +++ b/wolfcrypt/src/sp_x86_64.c @@ -459,13 +459,15 @@ static WC_INLINE int sp_2048_mod_16(sp_digit* r, const sp_digit* a, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - sp_digit* td; + sp_digit* td = NULL; #else sp_digit td[(33 * 32) + 32]; #endif @@ -482,11 +484,20 @@ static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, ASSERT_SAVED_VECTOR_REGISTERS(); + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 * 32) + 32, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 * 32) + 32, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) { + err = MEMORY_E; + } } #endif @@ -671,13 +682,15 @@ static void sp_2048_mont_sqr_avx2_16(sp_digit* r, const sp_digit* a, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_avx2_16(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - sp_digit* td; + sp_digit* td = NULL; #else sp_digit td[(33 * 32) + 32]; #endif @@ -694,11 +707,20 @@ static int sp_2048_mod_exp_avx2_16(sp_digit* r, const sp_digit* a, const sp_digi ASSERT_SAVED_VECTOR_REGISTERS(); + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 * 32) + 32, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 * 32) + 32, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) { + err = MEMORY_E; + } } #endif @@ -1138,13 +1160,15 @@ static WC_INLINE int sp_2048_mod_32(sp_digit* r, const sp_digit* a, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - sp_digit* td; + sp_digit* td = NULL; #else sp_digit td[(17 * 64) + 64]; #endif @@ -1161,11 +1185,20 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, ASSERT_SAVED_VECTOR_REGISTERS(); + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (17 * 64) + 64, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (17 * 64) + 64, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) { + err = MEMORY_E; + } } #endif @@ -1334,13 +1367,15 @@ static void sp_2048_mont_sqr_avx2_32(sp_digit* r, const sp_digit* a, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_2048_mod_exp_avx2_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - sp_digit* td; + sp_digit* td = NULL; #else sp_digit td[(17 * 64) + 64]; #endif @@ -1357,11 +1392,20 @@ static int sp_2048_mod_exp_avx2_32(sp_digit* r, const sp_digit* a, const sp_digi ASSERT_SAVED_VECTOR_REGISTERS(); + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (17 * 64) + 64, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (17 * 64) + 64, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) { + err = MEMORY_E; + } } #endif @@ -2069,13 +2113,15 @@ extern void sp_2048_lshift_32(sp_digit* r, const sp_digit* a, int n); * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_2048_mod_exp_2_avx2_32(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - sp_digit* td; + sp_digit* td = NULL; #else sp_digit td[33 + 64]; #endif @@ -2092,11 +2138,17 @@ static int sp_2048_mod_exp_2_avx2_32(sp_digit* r, const sp_digit* e, int bits, ASSERT_SAVED_VECTOR_REGISTERS(); + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 + 64), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 + 64), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) { + err = MEMORY_E; + } } #endif @@ -2196,13 +2248,15 @@ static int sp_2048_mod_exp_2_avx2_32(sp_digit* r, const sp_digit* e, int bits, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_2048_mod_exp_2_32(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - sp_digit* td; + sp_digit* td = NULL; #else sp_digit td[33 + 64]; #endif @@ -2219,11 +2273,17 @@ static int sp_2048_mod_exp_2_32(sp_digit* r, const sp_digit* e, int bits, ASSERT_SAVED_VECTOR_REGISTERS(); + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 + 64), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 + 64), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) { + err = MEMORY_E; + } } #endif @@ -2924,13 +2984,15 @@ static WC_INLINE int sp_3072_mod_24(sp_digit* r, const sp_digit* a, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - sp_digit* td; + sp_digit* td = NULL; #else sp_digit td[(33 * 48) + 48]; #endif @@ -2947,11 +3009,20 @@ static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, ASSERT_SAVED_VECTOR_REGISTERS(); + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 * 48) + 48, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 * 48) + 48, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) { + err = MEMORY_E; + } } #endif @@ -3136,13 +3207,15 @@ static void sp_3072_mont_sqr_avx2_24(sp_digit* r, const sp_digit* a, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_avx2_24(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - sp_digit* td; + sp_digit* td = NULL; #else sp_digit td[(33 * 48) + 48]; #endif @@ -3159,11 +3232,20 @@ static int sp_3072_mod_exp_avx2_24(sp_digit* r, const sp_digit* a, const sp_digi ASSERT_SAVED_VECTOR_REGISTERS(); + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 * 48) + 48, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 * 48) + 48, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) { + err = MEMORY_E; + } } #endif @@ -3603,13 +3685,15 @@ static WC_INLINE int sp_3072_mod_48(sp_digit* r, const sp_digit* a, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - sp_digit* td; + sp_digit* td = NULL; #else sp_digit td[(17 * 96) + 96]; #endif @@ -3626,11 +3710,20 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, ASSERT_SAVED_VECTOR_REGISTERS(); + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (17 * 96) + 96, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (17 * 96) + 96, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) { + err = MEMORY_E; + } } #endif @@ -3799,13 +3892,15 @@ static void sp_3072_mont_sqr_avx2_48(sp_digit* r, const sp_digit* a, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_3072_mod_exp_avx2_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - sp_digit* td; + sp_digit* td = NULL; #else sp_digit td[(17 * 96) + 96]; #endif @@ -3822,11 +3917,20 @@ static int sp_3072_mod_exp_avx2_48(sp_digit* r, const sp_digit* a, const sp_digi ASSERT_SAVED_VECTOR_REGISTERS(); + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (17 * 96) + 96, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (17 * 96) + 96, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) { + err = MEMORY_E; + } } #endif @@ -4534,13 +4638,15 @@ extern void sp_3072_lshift_48(sp_digit* r, const sp_digit* a, int n); * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_3072_mod_exp_2_avx2_48(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - sp_digit* td; + sp_digit* td = NULL; #else sp_digit td[49 + 96]; #endif @@ -4557,11 +4663,17 @@ static int sp_3072_mod_exp_2_avx2_48(sp_digit* r, const sp_digit* e, int bits, ASSERT_SAVED_VECTOR_REGISTERS(); + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (49 + 96), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (49 + 96), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) { + err = MEMORY_E; + } } #endif @@ -4661,13 +4773,15 @@ static int sp_3072_mod_exp_2_avx2_48(sp_digit* r, const sp_digit* e, int bits, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_3072_mod_exp_2_48(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - sp_digit* td; + sp_digit* td = NULL; #else sp_digit td[49 + 96]; #endif @@ -4684,11 +4798,17 @@ static int sp_3072_mod_exp_2_48(sp_digit* r, const sp_digit* e, int bits, ASSERT_SAVED_VECTOR_REGISTERS(); + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (49 + 96), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (49 + 96), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) { + err = MEMORY_E; + } } #endif @@ -5455,13 +5575,15 @@ static WC_INLINE int sp_4096_mod_64(sp_digit* r, const sp_digit* a, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - sp_digit* td; + sp_digit* td = NULL; #else sp_digit td[(17 * 128) + 128]; #endif @@ -5478,11 +5600,20 @@ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, ASSERT_SAVED_VECTOR_REGISTERS(); + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (17 * 128) + 128, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (17 * 128) + 128, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) { + err = MEMORY_E; + } } #endif @@ -5651,13 +5782,15 @@ static void sp_4096_mont_sqr_avx2_64(sp_digit* r, const sp_digit* a, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even or exponent is 0. */ static int sp_4096_mod_exp_avx2_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - sp_digit* td; + sp_digit* td = NULL; #else sp_digit td[(17 * 128) + 128]; #endif @@ -5674,11 +5807,20 @@ static int sp_4096_mod_exp_avx2_64(sp_digit* r, const sp_digit* a, const sp_digi ASSERT_SAVED_VECTOR_REGISTERS(); + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + else if (bits == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (17 * 128) + 128, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (17 * 128) + 128, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) { + err = MEMORY_E; + } } #endif @@ -6386,13 +6528,15 @@ extern void sp_4096_lshift_64(sp_digit* r, const sp_digit* a, int n); * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_4096_mod_exp_2_avx2_64(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - sp_digit* td; + sp_digit* td = NULL; #else sp_digit td[65 + 128]; #endif @@ -6409,11 +6553,17 @@ static int sp_4096_mod_exp_2_avx2_64(sp_digit* r, const sp_digit* e, int bits, ASSERT_SAVED_VECTOR_REGISTERS(); + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (65 + 128), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (65 + 128), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) { + err = MEMORY_E; + } } #endif @@ -6513,13 +6663,15 @@ static int sp_4096_mod_exp_2_avx2_64(sp_digit* r, const sp_digit* e, int bits, * e A single precision number that is the exponent. * bits The number of bits in the exponent. * m A single precision number that is the modulus. - * returns 0 on success and MEMORY_E on dynamic memory allocation failure. + * returns 0 on success. + * returns MEMORY_E on dynamic memory allocation failure. + * returns MP_VAL when base is even. */ static int sp_4096_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - sp_digit* td; + sp_digit* td = NULL; #else sp_digit td[65 + 128]; #endif @@ -6536,11 +6688,17 @@ static int sp_4096_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, ASSERT_SAVED_VECTOR_REGISTERS(); + if ((m[0] & 1) == 0) { + err = MP_VAL; + } + #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (65 + 128), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; + if (err == MP_OKAY) { + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (65 + 128), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (td == NULL) { + err = MEMORY_E; + } } #endif