Skip to content

Commit 67d35ea

Browse files
Merge pull request #6622 from philljj/zd16426
tfm fp_exptmod_nct: set result to zero when base is zero
2 parents 31aac92 + df58c4d commit 67d35ea

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

wolfcrypt/src/tfm.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3171,14 +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-
if (fp_iszero(G)) {
3175-
fp_set(G, 0);
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)) {
3179+
fp_set(Y, 0);
31763180
return FP_OKAY;
31773181
}
3178-
3179-
/* prevent overflows */
3180-
if (P->used > (FP_SIZE/2)) {
3181-
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;
31823189
}
31833190

31843191
#if defined(WOLFSSL_ESP32_CRYPT_RSA_PRI) && \

0 commit comments

Comments
 (0)