Browse Source

Merge pull request #6379 from SparkiDev/sp_int_gcd_fix

SP int; fix sp_gcd error checking
David Garske 11 months ago
parent
commit
62a4329f8e
1 changed files with 13 additions and 1 deletions
  1. 13 1
      wolfcrypt/src/sp_int.c

+ 13 - 1
wolfcrypt/src/sp_int.c

@@ -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