Browse Source

Merge pull request #5816 from SparkiDev/sp_int_rework

SP int: Comment and rework some code
David Garske 1 year ago
parent
commit
7697337703
5 changed files with 358 additions and 156 deletions
  1. 3 2
      wolfcrypt/benchmark/benchmark.c
  2. 5 4
      wolfcrypt/src/rsa.c
  3. 289 133
      wolfcrypt/src/sp_int.c
  4. 47 3
      wolfcrypt/test/test.c
  5. 14 14
      wolfssl/wolfcrypt/sp_int.h

+ 3 - 2
wolfcrypt/benchmark/benchmark.c

@@ -1076,11 +1076,12 @@ static const char* bench_desc_words[][15] = {
 #endif
 #endif
 
-#if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WC_NO_RNG)) \
+#if !defined(WC_NO_RNG) && \
+        ((!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) \
         || !defined(NO_DH) || defined(WOLFSSL_KEY_GEN) || defined(HAVE_ECC) \
         || defined(HAVE_CURVE25519) || defined(HAVE_ED25519) \
         || defined(HAVE_CURVE448) || defined(HAVE_ED448) \
-        || defined(WOLFSSL_HAVE_KYBER)
+        || defined(WOLFSSL_HAVE_KYBER))
     #define HAVE_LOCAL_RNG
     static THREAD_LS_T WC_RNG gRng;
     #define GLOBAL_RNG &gRng

+ 5 - 4
wolfcrypt/src/rsa.c

@@ -4802,7 +4802,7 @@ int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng)
     err = mp_init_multi(p, q, tmp1, tmp2, tmp3, NULL);
 
     if (err == MP_OKAY)
-        err = mp_set_int(tmp3, e);
+        err = mp_set_int(tmp3, (unsigned long)e);
 
     /* The failCount value comes from NIST FIPS 186-4, section B.3.3,
      * process steps 4.7 and 5.8. */
@@ -4945,7 +4945,7 @@ int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng)
 #endif
     /* make key */
     if (err == MP_OKAY)                /* key->e = e */
-        err = mp_set_int(&key->e, (mp_digit)e);
+        err = mp_set_int(&key->e, (unsigned long)e);
 #ifdef WC_RSA_BLINDING
     /* Blind the inverse operation with a value that is invertable */
     if (err == MP_OKAY) {
@@ -4960,8 +4960,9 @@ int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng)
         }
         while ((err == MP_OKAY) && !mp_isone(&key->q));
     }
+    /* 8/16-bit word size requires a full multiply when e=0x10001 */
     if (err == MP_OKAY)
-        err = mp_mul_d(&key->p, (mp_digit)e, &key->e);
+        err = mp_mul(&key->p, &key->e, &key->e);
 #endif
     if (err == MP_OKAY)                /* key->d = 1/e mod lcm(p-1, q-1) */
         err = mp_invmod(&key->e, tmp3, &key->d);
@@ -4970,7 +4971,7 @@ int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng)
     if (err == MP_OKAY)
         err = mp_mulmod(&key->d, &key->p, tmp3, &key->d);
     if (err == MP_OKAY)
-        err = mp_set_int(&key->e, (mp_digit)e);
+        err = mp_set_int(&key->e, (unsigned long)e);
 #endif
     if (err == MP_OKAY)                /* key->n = pq */
         err = mp_mul(p, q, &key->n);

File diff suppressed because it is too large
+ 289 - 133
wolfcrypt/src/sp_int.c


+ 47 - 3
wolfcrypt/test/test.c

@@ -42671,7 +42671,7 @@ static int mp_test_shbd(mp_int* a, mp_int* b, WC_RNG* rng)
 }
 #endif
 
-#if !defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)
+#if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
 static int mp_test_div(mp_int* a, mp_int* d, mp_int* r, mp_int* rem,
                        WC_RNG* rng)
 {
@@ -42787,6 +42787,24 @@ static int mp_test_div(mp_int* a, mp_int* d, mp_int* r, mp_int* rem,
     if (ret != MP_OKAY)
         return -13053;
 
+    /* Make sure [d | d] / d is handled. */
+    mp_zero(a);
+    mp_set_bit(a, DIGIT_BIT * 2 - 1);
+    mp_set_bit(a, DIGIT_BIT * 1 - 1);
+    mp_zero(d);
+    mp_set_bit(d, DIGIT_BIT - 1);
+    ret = mp_div(a, d, r, rem);
+    if (ret != MP_OKAY)
+        return -13054;
+    mp_zero(a);
+    mp_set_bit(a, DIGIT_BIT);
+    mp_set_bit(a, 0);
+    mp_zero(d);
+    if (mp_cmp(r, a) != MP_EQ)
+        return -13055;
+    if (mp_cmp(rem, d) != MP_EQ)
+        return -13056;
+
     return 0;
 }
 #endif
@@ -42808,7 +42826,7 @@ static int mp_test_prime(mp_int* a, WC_RNG* rng)
 #endif
 #ifndef WOLFSSL_SP_MATH
     ret = mp_rand_prime(a, -5, rng, NULL);
-    if (ret != 0)
+    if (ret != 0 || (a->dp[0] & 3) != 3)
         return -13061;
 #endif
     ret = mp_prime_is_prime(a, 1, &res);
@@ -43642,6 +43660,21 @@ WOLFSSL_TEST_SUBROUTINE int mp_test(void)
         }
     }
 
+    /* Test adding and subtracting zero from zero. */
+    mp_zero(&a);
+    ret = mp_add_d(&a, 0, &r1);
+    if (ret != 0)
+        return -13329;
+    if (!mp_iszero(&r1)) {
+        return -13330;
+    }
+    ret = mp_sub_d(&a, 0, &r2);
+    if (ret != 0)
+        return -13331;
+    if (!mp_iszero(&r2)) {
+        return -13332;
+    }
+
 #if DIGIT_BIT >= 32
     /* Check that setting a 32-bit digit works. */
     d &= 0xffffffffU;
@@ -43668,6 +43701,17 @@ WOLFSSL_TEST_SUBROUTINE int mp_test(void)
     i = mp_cnt_lsb(&a);
     if (i != 0)
         return -13327;
+
+    mp_set(&a, 32);
+    i = mp_cnt_lsb(&a);
+    if (i != 5)
+        return -13328;
+
+    mp_zero(&a);
+    mp_set_bit(&a, 129);
+    i = mp_cnt_lsb(&a);
+    if (i != 129)
+        return -13328;
 #endif
 
 #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
@@ -43712,7 +43756,7 @@ WOLFSSL_TEST_SUBROUTINE int mp_test(void)
     if ((ret = mp_test_set_is_bit(&a)) != 0)
         return ret;
 #endif
-#if !defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)
+#if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
     if ((ret = mp_test_div(&a, &b, &r1, &r2, &rng)) != 0)
         return ret;
 #endif

+ 14 - 14
wolfssl/wolfcrypt/sp_int.h

@@ -252,29 +252,29 @@ extern "C" {
 #endif
 
 #if SP_WORD_SIZE == 8
-    typedef   sp_uint8  sp_int_digit;
-    typedef    sp_int8 sp_sint_digit;
-    typedef  sp_uint16  sp_int_word;
-    typedef   sp_int16  sp_int_sword;
+    typedef   sp_uint8 sp_int_digit;
+    typedef    sp_int8 sp_int_sdigit;
+    typedef  sp_uint16 sp_int_word;
+    typedef   sp_int16 sp_int_sword;
 
     #define SP_MASK         0xffU
 #elif SP_WORD_SIZE == 16
-    typedef  sp_uint16  sp_int_digit;
-    typedef   sp_int16 sp_sint_digit;
-    typedef  sp_uint32  sp_int_word;
-    typedef   sp_int32  sp_int_sword;
+    typedef  sp_uint16 sp_int_digit;
+    typedef   sp_int16 sp_int_sdigit;
+    typedef  sp_uint32 sp_int_word;
+    typedef   sp_int32 sp_int_sword;
 
     #define SP_MASK         0xffffU
 #elif SP_WORD_SIZE == 32
-    typedef  sp_uint32  sp_int_digit;
-    typedef   sp_int32 sp_sint_digit;
-    typedef  sp_uint64  sp_int_word;
-    typedef   sp_int64  sp_int_sword;
+    typedef  sp_uint32 sp_int_digit;
+    typedef   sp_int32 sp_int_sdigit;
+    typedef  sp_uint64 sp_int_word;
+    typedef   sp_int64 sp_int_sword;
 
     #define SP_MASK         0xffffffffU
 #elif SP_WORD_SIZE == 64
-    typedef  sp_uint64  sp_int_digit;
-    typedef   sp_int64 sp_sint_digit;
+    typedef  sp_uint64 sp_int_digit;
+    typedef   sp_int64 sp_int_sdigit;
 #if (defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
     !defined(_WIN64) && defined(WOLFSSL_UINT128_T_DEFINED)
     typedef sp_uint128  sp_int_word;

Some files were not shown because too many files changed in this diff