From 8145ee6cef54c80feae280577e26136e227ff2e0 Mon Sep 17 00:00:00 2001 From: Sean Parkinson Date: Thu, 16 Jun 2022 10:39:23 +1000 Subject: [PATCH] TFM: mp_exptmod_ex didn't handle exceptional cases fp_exptmod_ex() changed to match execptional case handling in fp_exptmod(). --- wolfcrypt/src/tfm.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) 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) && \