From 452ba5b5024461d286b59acda5c1e24afecbd328 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Thu, 21 Nov 2019 13:47:43 -0800 Subject: [PATCH 1/5] Maintenance: Prime 1. Prime test should return NO for 1. (normal math and fast math) --- wolfcrypt/src/integer.c | 5 +++++ wolfcrypt/src/tfm.c | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/wolfcrypt/src/integer.c b/wolfcrypt/src/integer.c index 668253303..12b5b8059 100644 --- a/wolfcrypt/src/integer.c +++ b/wolfcrypt/src/integer.c @@ -4715,6 +4715,11 @@ int mp_prime_is_prime (mp_int * a, int t, int *result) return MP_VAL; } + if (mp_isone(a)) { + *result = 0; + return MP_OKAY; + } + /* is the input equal to one of the primes in the table? */ for (ix = 0; ix < PRIME_SIZE; ix++) { if (mp_cmp_d(a, ltm_prime_tab[ix]) == MP_EQ) { diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c index a5c409f47..2f7d347fa 100644 --- a/wolfcrypt/src/tfm.c +++ b/wolfcrypt/src/tfm.c @@ -4074,6 +4074,11 @@ int fp_isprime_ex(fp_int *a, int t, int* result) return FP_VAL; } + if (fp_isone(a)) { + *result = FP_NO; + return FP_OKAY; + } + /* check against primes table */ for (r = 0; r < FP_PRIME_SIZE; r++) { if (fp_cmp_d(a, primes[r]) == FP_EQ) { From 0a924af89435e628ddecdfc658fa088a6acfb8f6 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Fri, 22 Nov 2019 10:01:21 -0800 Subject: [PATCH 2/5] Maintenance: Prime 1. Prime test should return NO for 1. (sp math) --- wolfcrypt/src/sp_int.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/wolfcrypt/src/sp_int.c b/wolfcrypt/src/sp_int.c index 9e9b99530..422614e3d 100644 --- a/wolfcrypt/src/sp_int.c +++ b/wolfcrypt/src/sp_int.c @@ -1703,6 +1703,11 @@ int sp_prime_is_prime(sp_int *a, int t, int* result) err = MP_VAL; } + if (sp_isone(a)) { + *result = MP_NO; + return MP_OKAY; + } + if (err == MP_OKAY && a->used == 1) { /* check against primes table */ for (i = 0; i < SP_PRIME_SIZE; i++) { @@ -1783,6 +1788,11 @@ int sp_prime_is_prime_ex(sp_int* a, int t, int* result, WC_RNG* rng) if (a == NULL || result == NULL || rng == NULL) err = MP_VAL; + if (sp_isone(a)) { + *result = MP_NO; + return MP_OKAY; + } + if (err == MP_OKAY && a->used == 1) { /* check against primes table */ for (i = 0; i < SP_PRIME_SIZE; i++) { From 481da3dcc199dc8384d75f9b182b5203dfeee40f Mon Sep 17 00:00:00 2001 From: John Safranek Date: Fri, 22 Nov 2019 11:06:09 -0800 Subject: [PATCH 3/5] Maintenance: Prime 1. Added a test case for checking "1" as a prime number to the wolfCrypt test. 2. Allow the wolfCrypt prime test for SP builds. 3. Modify the prime test to use mp_mul rather than mp_mul_d, as the SP math library doesn't export sp_mul_d. --- wolfcrypt/test/test.c | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index a750ac373..ab3324219 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -1080,7 +1080,7 @@ initDefaultName(); test_pass("mp test passed!\n"); #endif -#if defined(WOLFSSL_PUBLIC_MP) && !defined(WOLFSSL_SP_MATH) +#if defined(WOLFSSL_PUBLIC_MP) if ( (ret = prime_test()) != 0) return err_sys("prime test failed!\n", ret); else @@ -24568,7 +24568,7 @@ done: #endif -#if defined(WOLFSSL_PUBLIC_MP) && !defined(WOLFSSL_SP_MATH) +#if defined(WOLFSSL_PUBLIC_MP) typedef struct pairs_t { const unsigned char* coeff; @@ -24623,16 +24623,22 @@ static const unsigned char controlPrime[] = { 0xe6, 0xd1, 0x1b, 0x5d, 0x5e, 0x96, 0xfa, 0x53 }; +static const unsigned char testOne[] = { 1 }; + static int GenerateNextP(mp_int* p1, mp_int* p2, int k) { int ret; + mp_int ki; - ret = mp_sub_d(p1, 1, p2); + ret = mp_set(&ki, k); if (ret == 0) - ret = mp_mul_d(p2, k, p2); + ret = mp_sub_d(p1, 1, p2); + if (ret == 0) + ret = mp_mul(p2, &ki, p2); if (ret == 0) ret = mp_add_d(p2, 1, p2); + mp_clear(&ki); return ret; } @@ -24738,6 +24744,23 @@ int prime_test(void) if (!isPrime) return -9661; + ret = mp_read_unsigned_bin(&n, testOne, sizeof(testOne)); + if (ret != 0) + return -9662; + + /* This test result should indicate the value as not prime. */ + ret = mp_prime_is_prime_ex(&n, 8, &isPrime, &rng); + if (ret != 0) + return -9663; + if (isPrime) + return -9664; + + ret = mp_prime_is_prime(&n, 8, &isPrime); + if (ret != 0) + return -9665; + if (isPrime) + return -9666; + mp_clear(&p3); mp_clear(&p2); mp_clear(&p1); From 3432a8a1fc7c79559abcb687332e91d74730214a Mon Sep 17 00:00:00 2001 From: John Safranek Date: Fri, 22 Nov 2019 13:02:59 -0800 Subject: [PATCH 4/5] Maintenance: Prime 1. Prime test should return NO for 1. (ex function, normal math and fast math) 2. Call mp_init() on the k value for the primality test case in the wolfCrypt test. --- wolfcrypt/src/integer.c | 5 +++++ wolfcrypt/src/tfm.c | 5 +++++ wolfcrypt/test/test.c | 4 +++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/wolfcrypt/src/integer.c b/wolfcrypt/src/integer.c index 12b5b8059..fad07663e 100644 --- a/wolfcrypt/src/integer.c +++ b/wolfcrypt/src/integer.c @@ -4783,6 +4783,11 @@ int mp_prime_is_prime_ex (mp_int * a, int t, int *result, WC_RNG *rng) return MP_VAL; } + if (mp_isone(a)) { + *result = MP_NO; + return MP_OKAY; + } + /* is the input equal to one of the primes in the table? */ for (ix = 0; ix < PRIME_SIZE; ix++) { if (mp_cmp_d(a, ltm_prime_tab[ix]) == MP_EQ) { diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c index 2f7d347fa..c4a79f451 100644 --- a/wolfcrypt/src/tfm.c +++ b/wolfcrypt/src/tfm.c @@ -4129,6 +4129,11 @@ int mp_prime_is_prime_ex(mp_int* a, int t, int* result, WC_RNG* rng) if (a == NULL || result == NULL || rng == NULL) return FP_VAL; + if (fp_isone(a)) { + *result = FP_NO; + return FP_OKAY; + } + if (ret == FP_YES) { fp_digit d; int r; diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index ab3324219..b329cefeb 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -24631,7 +24631,9 @@ static int GenerateNextP(mp_int* p1, mp_int* p2, int k) int ret; mp_int ki; - ret = mp_set(&ki, k); + ret = mp_init(&ki); + if (ret == 0) + ret = mp_set(&ki, k); if (ret == 0) ret = mp_sub_d(p1, 1, p2); if (ret == 0) From 2de52c766655b47735709957e212e821e6e99626 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Tue, 26 Nov 2019 15:44:30 -0800 Subject: [PATCH 5/5] Maintenance: Prime When returning a result from mp_prime_is_prime for normal math, the result should be MP_YES or MP_NO, not a bare number (1 or 0). --- wolfcrypt/src/integer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wolfcrypt/src/integer.c b/wolfcrypt/src/integer.c index fad07663e..4f87ab2e0 100644 --- a/wolfcrypt/src/integer.c +++ b/wolfcrypt/src/integer.c @@ -4716,14 +4716,14 @@ int mp_prime_is_prime (mp_int * a, int t, int *result) } if (mp_isone(a)) { - *result = 0; + *result = MP_NO; return MP_OKAY; } /* is the input equal to one of the primes in the table? */ for (ix = 0; ix < PRIME_SIZE; ix++) { if (mp_cmp_d(a, ltm_prime_tab[ix]) == MP_EQ) { - *result = 1; + *result = MP_YES; return MP_OKAY; } }