forked from wolfSSL/wolfssl
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:
@ -843,9 +843,21 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
|
|||||||
int dr;
|
int dr;
|
||||||
|
|
||||||
/* modulus P must be positive */
|
/* modulus P must be positive */
|
||||||
if (P->sign == MP_NEG) {
|
if (mp_iszero(P) || P->sign == MP_NEG) {
|
||||||
return MP_VAL;
|
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 exponent X is negative we have to recurse */
|
||||||
if (X->sign == MP_NEG) {
|
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);
|
int x = fp_count_bits (X);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (fp_iszero(G)) {
|
/* handle modulus of zero and prevent overflows */
|
||||||
fp_set(G, 0);
|
if (fp_iszero(P) || (P->used > (FP_SIZE/2))) {
|
||||||
|
return FP_VAL;
|
||||||
|
}
|
||||||
|
if (fp_isone(P)) {
|
||||||
|
fp_set(Y, 0);
|
||||||
return FP_OKAY;
|
return FP_OKAY;
|
||||||
}
|
}
|
||||||
|
if (fp_iszero(X)) {
|
||||||
/* prevent overflows */
|
fp_set(Y, 1);
|
||||||
if (P->used > (FP_SIZE/2)) {
|
return FP_OKAY;
|
||||||
return FP_VAL;
|
}
|
||||||
|
if (fp_iszero(G)) {
|
||||||
|
fp_set(Y, 0);
|
||||||
|
return FP_OKAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI) && \
|
#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;
|
tmp[1].sign = FP_ZPOS;
|
||||||
err = fp_invmod(&tmp[0], &tmp[1], &tmp[0]);
|
err = fp_invmod(&tmp[0], &tmp[1], &tmp[0]);
|
||||||
if (err == FP_OKAY) {
|
if (err == FP_OKAY) {
|
||||||
X->sign = FP_ZPOS;
|
fp_copy(X, &tmp[1]);
|
||||||
err = _fp_exptmod(&tmp[0], X, X->used, P, Y);
|
tmp[1].sign = FP_ZPOS;
|
||||||
if (X != Y) {
|
err = _fp_exptmod(&tmp[0], &tmp[1], tmp[1].used, P, Y);
|
||||||
X->sign = FP_NEG;
|
|
||||||
}
|
|
||||||
if (P->sign == FP_NEG) {
|
if (P->sign == FP_NEG) {
|
||||||
fp_add(Y, P, Y);
|
fp_add(Y, P, Y);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user