Skip to content

Commit 62a4329

Browse files
authored
Merge pull request #6379 from SparkiDev/sp_int_gcd_fix
SP int; fix sp_gcd error checking
2 parents ae844b4 + d342e34 commit 62a4329

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

wolfcrypt/src/sp_int.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18697,6 +18697,9 @@ int sp_prime_is_prime_ex(const sp_int* a, int trials, int* result, WC_RNG* rng)
1869718697
#if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
1869818698

1869918699
/* Calculates the Greatest Common Denominator (GCD) of a and b into r.
18700+
*
18701+
* Find the largest number that divides both a and b without remainder.
18702+
* r <= a, r <= b, a % r == 0, b % r == 0
1870018703
*
1870118704
* a and b are positive integers.
1870218705
*
@@ -18800,6 +18803,9 @@ static WC_INLINE int _sp_gcd(const sp_int* a, const sp_int* b, sp_int* r)
1880018803
}
1880118804

1880218805
/* Calculates the Greatest Common Denominator (GCD) of a and b into r.
18806+
*
18807+
* Find the largest number that divides both a and b without remainder.
18808+
* r <= a, r <= b, a % r == 0, b % r == 0
1880318809
*
1880418810
* a and b are positive integers.
1880518811
*
@@ -18823,8 +18829,14 @@ int sp_gcd(const sp_int* a, const sp_int* b, sp_int* r)
1882318829
else if ((a->used >= SP_INT_DIGITS) || (b->used >= SP_INT_DIGITS)) {
1882418830
err = MP_VAL;
1882518831
}
18832+
/* Check that r is large enough to hold maximum sized result. */
18833+
else if (((a->used <= b->used) && (r->size < a->used)) ||
18834+
((b->used < a->used) && (r->size < b->used))) {
18835+
err = MP_VAL;
18836+
}
1882618837
#ifdef WOLFSSL_SP_INT_NEGATIVE
18827-
else if ((a->sign == MP_NEG) || (b->sign >= MP_NEG)) {
18838+
/* Algorithm doesn't work with negative numbers. */
18839+
else if ((a->sign == MP_NEG) || (b->sign == MP_NEG)) {
1882818840
err = MP_VAL;
1882918841
}
1883018842
#endif

0 commit comments

Comments
 (0)