diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c index f9115f1c4..cc2f9554c 100644 --- a/wolfcrypt/src/tfm.c +++ b/wolfcrypt/src/tfm.c @@ -2895,14 +2895,21 @@ int fp_exptmod_ex(fp_int * G, fp_int * X, int digits, fp_int * P, fp_int * Y) int x = fp_count_bits (X); #endif - if (fp_iszero(G)) { + /* handle modulus of zero and prevent overflows */ + if (fp_iszero(P) || (P->used > (FP_SIZE/2))) { + return FP_VAL; + } + if (fp_isone(P)) { fp_set(Y, 0); return FP_OKAY; } - - /* prevent overflows */ - if (P->used > (FP_SIZE/2)) { - return FP_VAL; + if (fp_iszero(X)) { + fp_set(Y, 1); + return FP_OKAY; + } + if (fp_iszero(G)) { + fp_set(Y, 0); + return FP_OKAY; } #if defined(WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI) && \