Handle more values in mp_exptmod
Handle prime (modulus) of 0 and 1. Handle exponent of 0. Fix for base of 0 in fp_exptmod and hadnle base of 0 in mp_exptmod. fp_exptmod - Don't modify X's sign during operation when passed in as negative.
This commit is contained in:
parent
7e45ae2ec6
commit
c5f9a601e8
@ -843,9 +843,21 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
|
||||
int dr;
|
||||
|
||||
/* modulus P must be positive */
|
||||
if (P->sign == MP_NEG) {
|
||||
if (mp_iszero(P) || P->sign == MP_NEG) {
|
||||
return MP_VAL;
|
||||
}
|
||||
if (mp_isone(P)) {
|
||||
mp_set(Y, 0);
|
||||
return MP_OKAY;
|
||||
}
|
||||
if (mp_iszero(X)) {
|
||||
mp_set(Y, 1);
|
||||
return MP_OKAY;
|
||||
}
|
||||
if (mp_iszero(G)) {
|
||||
mp_set(Y, 0);
|
||||
return MP_OKAY;
|
||||
}
|
||||
|
||||
/* if exponent X is negative we have to recurse */
|
||||
if (X->sign == MP_NEG) {
|
||||
|
@ -2183,14 +2183,21 @@ int fp_exptmod(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)
|
||||
int x = fp_count_bits (X);
|
||||
#endif
|
||||
|
||||
if (fp_iszero(G)) {
|
||||
fp_set(G, 0);
|
||||
/* 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) && \
|
||||
@ -2221,11 +2228,9 @@ int fp_exptmod(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)
|
||||
tmp[1].sign = FP_ZPOS;
|
||||
err = fp_invmod(&tmp[0], &tmp[1], &tmp[0]);
|
||||
if (err == FP_OKAY) {
|
||||
X->sign = FP_ZPOS;
|
||||
err = _fp_exptmod(&tmp[0], X, X->used, P, Y);
|
||||
if (X != Y) {
|
||||
X->sign = FP_NEG;
|
||||
}
|
||||
fp_copy(X, &tmp[1]);
|
||||
tmp[1].sign = FP_ZPOS;
|
||||
err = _fp_exptmod(&tmp[0], &tmp[1], tmp[1].used, P, Y);
|
||||
if (P->sign == FP_NEG) {
|
||||
fp_add(Y, P, Y);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user