Skip to content

Commit df58c4d

Browse files
committed
tfm fp_exptmod_nct: handle special cases better
1 parent 1afc0df commit df58c4d

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

wolfcrypt/src/tfm.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3171,16 +3171,21 @@ int fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)
31713171
int x = fp_count_bits (X);
31723172
#endif
31733173

3174-
/* 0^X mod P = 0 mod P = 0.
3175-
* Set result to 0 and return early. */
3176-
if (fp_iszero(G)) {
3174+
/* handle modulus of zero and prevent overflows */
3175+
if (fp_iszero(P) || (P->used > (FP_SIZE/2))) {
3176+
return FP_VAL;
3177+
}
3178+
if (fp_isone(P)) {
31773179
fp_set(Y, 0);
31783180
return FP_OKAY;
31793181
}
3180-
3181-
/* prevent overflows */
3182-
if (P->used > (FP_SIZE/2)) {
3183-
return FP_VAL;
3182+
if (fp_iszero(X)) {
3183+
fp_set(Y, 1);
3184+
return FP_OKAY;
3185+
}
3186+
if (fp_iszero(G)) {
3187+
fp_set(Y, 0);
3188+
return FP_OKAY;
31843189
}
31853190

31863191
#if defined(WOLFSSL_ESP32_CRYPT_RSA_PRI) && \

0 commit comments

Comments
 (0)