SP int; fix sp_gcd error checking

r can be as large as the smaller of a and b.
Fix sign check.
Add comments as to what GCD does.
This commit is contained in:
Sean Parkinson
2023-05-05 09:11:47 +10:00
parent c3e4da9f66
commit d342e341d8

View File

@ -18697,6 +18697,9 @@ int sp_prime_is_prime_ex(const sp_int* a, int trials, int* result, WC_RNG* rng)
#if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
/* Calculates the Greatest Common Denominator (GCD) of a and b into r.
*
* Find the largest number that divides both a and b without remainder.
* r <= a, r <= b, a % r == 0, b % r == 0
*
* a and b are positive integers.
*
@ -18800,6 +18803,9 @@ static WC_INLINE int _sp_gcd(const sp_int* a, const sp_int* b, sp_int* r)
}
/* Calculates the Greatest Common Denominator (GCD) of a and b into r.
*
* Find the largest number that divides both a and b without remainder.
* r <= a, r <= b, a % r == 0, b % r == 0
*
* a and b are positive integers.
*
@ -18823,8 +18829,14 @@ int sp_gcd(const sp_int* a, const sp_int* b, sp_int* r)
else if ((a->used >= SP_INT_DIGITS) || (b->used >= SP_INT_DIGITS)) {
err = MP_VAL;
}
/* Check that r is large enough to hold maximum sized result. */
else if (((a->used <= b->used) && (r->size < a->used)) ||
((b->used < a->used) && (r->size < b->used))) {
err = MP_VAL;
}
#ifdef WOLFSSL_SP_INT_NEGATIVE
else if ((a->sign == MP_NEG) || (b->sign >= MP_NEG)) {
/* Algorithm doesn't work with negative numbers. */
else if ((a->sign == MP_NEG) || (b->sign == MP_NEG)) {
err = MP_VAL;
}
#endif