Merge pull request #2474 from SparkiDev/sp_int_prime

Add support for prime checking to sp_int.c
This commit is contained in:
toddouska 2019-09-19 13:44:22 -07:00 committed by GitHub
commit 2dbf3f81fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 1376 additions and 169 deletions

View File

@ -4037,6 +4037,9 @@ if test "$ENABLED_SP_MATH" = "yes"; then
if test "$ENABLED_SP_DH" = "no" && test "$ENABLED_DH" = "yes"; then
AC_MSG_ERROR([Cannot use P256 single precision only math and DH])
fi
ENABLED_FASTMATH="no"
ENABLED_SLOWMATH="no"
fi
if test "$ENABLED_SP_MATH" = "yes"; then
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_SP_MATH"
@ -4538,9 +4541,9 @@ then
fi
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_HAVE_WOLFSCEP"
fi
if test "$ENABLED_SP_MATH" = "yes" && test "$ENABLED_KEYGEN" = "yes"; then
AC_MSG_ERROR([Cannot use single precision math and key generation])
fi
#if test "$ENABLED_SP_MATH" = "yes" && test "$ENABLED_KEYGEN" = "yes"; then
# AC_MSG_ERROR([Cannot use single precision math and key generation])
#fi
if test "x$ENABLED_PKCS7" = "xyes"
then

View File

@ -11621,6 +11621,11 @@ static int test_wc_MakeRsaKey (void)
RsaKey genKey;
WC_RNG rng;
#ifndef WOLFSSL_SP_MATH
int bits = 1024;
#else
int bits = 2048;
#endif
printf(testingFmt, "wc_MakeRsaKey()");
@ -11628,7 +11633,7 @@ static int test_wc_MakeRsaKey (void)
if (ret == 0) {
ret = wc_InitRng(&rng);
if (ret == 0) {
ret = MAKE_RSA_KEY(&genKey, 1024, WC_RSA_EXPONENT, &rng);
ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
if (ret == 0 && wc_FreeRsaKey(&genKey) != 0) {
ret = WOLFSSL_FATAL_ERROR;
}
@ -11637,17 +11642,17 @@ static int test_wc_MakeRsaKey (void)
#ifndef HAVE_USER_RSA
/* Test bad args. */
if (ret == 0) {
ret = MAKE_RSA_KEY(NULL, 1024, WC_RSA_EXPONENT, &rng);
ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
if (ret == BAD_FUNC_ARG) {
ret = MAKE_RSA_KEY(&genKey, 1024, WC_RSA_EXPONENT, NULL);
ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
}
if (ret == BAD_FUNC_ARG) {
/* e < 3 */
ret = MAKE_RSA_KEY(&genKey, 1024, 2, &rng);
ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
}
if (ret == BAD_FUNC_ARG) {
/* e & 1 == 0 */
ret = MAKE_RSA_KEY(&genKey, 1024, 6, &rng);
ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
}
if (ret == BAD_FUNC_ARG) {
ret = 0;
@ -11658,17 +11663,17 @@ static int test_wc_MakeRsaKey (void)
#else
/* Test bad args. */
if (ret == 0) {
ret = MAKE_RSA_KEY(NULL, 1024, WC_RSA_EXPONENT, &rng);
ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
if (ret == USER_CRYPTO_ERROR) {
ret = MAKE_RSA_KEY(&genKey, 1024, WC_RSA_EXPONENT, NULL);
ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
}
if (ret == USER_CRYPTO_ERROR) {
/* e < 3 */
ret = MAKE_RSA_KEY(&genKey, 1024, 2, &rng);
ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
}
if (ret == USER_CRYPTO_ERROR) {
/* e & 1 == 0 */
ret = MAKE_RSA_KEY(&genKey, 1024, 6, &rng);
ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
}
if (ret == USER_CRYPTO_ERROR) {
ret = 0;
@ -11832,10 +11837,18 @@ static int test_wc_RsaKeyToDer (void)
RsaKey genKey;
WC_RNG rng;
byte* der;
#ifndef WOLFSSL_SP_MATH
int bits = 1024;
word32 derSz = 611;
/* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
+ 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
#else
int bits = 2048;
word32 derSz = 1196;
/* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
+ 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
#endif
der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (der == NULL) {
ret = WOLFSSL_FATAL_ERROR;
@ -11849,7 +11862,7 @@ static int test_wc_RsaKeyToDer (void)
}
/* Make key. */
if (ret == 0) {
ret = MAKE_RSA_KEY(&genKey, 1024, WC_RSA_EXPONENT, &rng);
ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
if (ret != 0) {
ret = WOLFSSL_FATAL_ERROR;
}
@ -11930,7 +11943,13 @@ static int test_wc_RsaKeyToPublicDer (void)
RsaKey key;
WC_RNG rng;
byte* der;
#ifndef WOLFSSL_SP_MATH
int bits = 1024;
word32 derLen = 162;
#else
int bits = 2048;
word32 derLen = 290;
#endif
der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (der == NULL) {
@ -11943,7 +11962,7 @@ static int test_wc_RsaKeyToPublicDer (void)
ret = wc_InitRng(&rng);
}
if (ret == 0) {
ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
}
printf(testingFmt, "wc_RsaKeyToPublicDer()");
@ -12018,9 +12037,15 @@ static int test_wc_RsaPublicEncryptDecrypt (void)
RsaKey key;
WC_RNG rng;
const char* inStr = "Everyone gets Friday off.";
word32 cipherLen = 128;
word32 plainLen = 25;
word32 inLen = (word32)XSTRLEN(inStr);
#ifndef WOLFSSL_SP_MATH
int bits = 1024;
word32 cipherLen = 128;
#else
int bits = 2048;
word32 cipherLen = 256;
#endif
DECLARE_VAR_INIT(in, byte, inLen, inStr, NULL);
DECLARE_VAR(plain, byte, plainLen, NULL);
@ -12031,7 +12056,7 @@ static int test_wc_RsaPublicEncryptDecrypt (void)
ret = wc_InitRng(&rng);
}
if (ret == 0) {
ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
}
/* Encrypt. */
printf(testingFmt, "wc_RsaPublicEncrypt()");
@ -12101,10 +12126,16 @@ static int test_wc_RsaPublicEncryptDecrypt_ex (void)
WC_RNG rng;
const char* inStr = "Everyone gets Friday off.";
word32 inLen = (word32)XSTRLEN(inStr);
const word32 cipherSz = 128;
const word32 plainSz = 25;
byte* res = NULL;
int idx = 0;
#ifndef WOLFSSL_SP_MATH
int bits = 1024;
const word32 cipherSz = 128;
#else
int bits = 2048;
const word32 cipherSz = 256;
#endif
DECLARE_VAR_INIT(in, byte, inLen, inStr, NULL);
DECLARE_VAR(plain, byte, plainSz, NULL);
@ -12119,7 +12150,7 @@ static int test_wc_RsaPublicEncryptDecrypt_ex (void)
ret = wc_InitRng(&rng);
}
if (ret == 0) {
ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
}
/* Encrypt */
printf(testingFmt, "wc_RsaPublicEncrypt_ex()");
@ -12210,10 +12241,16 @@ static int test_wc_RsaSSL_SignVerify (void)
RsaKey key;
WC_RNG rng;
const char* inStr = "Everyone gets Friday off.";
const word32 outSz = 128;
const word32 plainSz = 25;
word32 inLen = (word32)XSTRLEN(inStr);
word32 idx = 0;
#ifndef WOLFSSL_SP_MATH
int bits = 1024;
const word32 outSz = 128;
#else
int bits = 2048;
const word32 outSz = 256;
#endif
DECLARE_VAR_INIT(in, byte, inLen, inStr, NULL);
DECLARE_VAR(out, byte, outSz, NULL);
@ -12226,7 +12263,7 @@ static int test_wc_RsaSSL_SignVerify (void)
}
if (ret == 0) {
ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
}
/* Sign. */
printf(testingFmt, "wc_RsaSSL_Sign()");
@ -12369,6 +12406,7 @@ static int test_wc_RsaEncryptSize (void)
}
printf(testingFmt, "wc_RsaEncryptSize()");
#ifndef WOLFSSL_SP_MATH
if (ret == 0) {
ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
if (ret == 0) {
@ -12385,6 +12423,7 @@ static int test_wc_RsaEncryptSize (void)
} else {
ret = 0;
}
#endif
if (ret == 0) {
ret = MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng);
@ -12437,6 +12476,11 @@ static int test_wc_RsaFlattenPublicKey (void)
byte n[256];
word32 eSz = sizeof(e);
word32 nSz = sizeof(n);
#ifndef WOLFSSL_SP_MATH
int bits = 1024;
#else
int bits = 2048;
#endif
ret = wc_InitRsaKey(&key, NULL);
if (ret == 0) {
@ -12444,7 +12488,7 @@ static int test_wc_RsaFlattenPublicKey (void)
}
if (ret == 0) {
ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
if (ret >= 0) {
ret = 0;
} else {

View File

@ -4164,7 +4164,11 @@ exit:
void bench_rsaKeyGen(int doAsync)
{
int k, keySz;
#ifndef WOLFSSL_SP_MATH
const int keySizes[2] = {1024, 2048};
#else
const int keySizes[1] = {2048};
#endif
for (k = 0; k < (int)(sizeof(keySizes)/sizeof(int)); k++) {
keySz = keySizes[k];

View File

@ -2060,7 +2060,6 @@ static int _DhSetKey(DhKey* key, const byte* p, word32 pSz, const byte* g,
keyP = &key->p;
}
#ifndef WOLFSSL_SP_MATH
if (ret == 0 && !trusted) {
int isPrime = 0;
if (rng != NULL)
@ -2071,10 +2070,6 @@ static int _DhSetKey(DhKey* key, const byte* p, word32 pSz, const byte* g,
if (ret == 0 && isPrime == 0)
ret = DH_CHECK_PUB_E;
}
#else
(void)trusted;
(void)rng;
#endif
if (ret == 0 && mp_init(&key->g) != MP_OKAY)
ret = MP_INIT_E;

View File

@ -606,7 +606,7 @@ int wc_CheckRsaKey(RsaKey* key)
ret = MP_READ_E;
}
#ifdef WOLFSSL_SP_RSA
#ifdef WOLFSSL_HAVE_SP_RSA
#ifndef WOLFSSL_SP_NO_2048
if (mp_count_bits(&key->n) == 2048) {
ret = sp_ModExp_2048(k, &key->e, &key->n, tmp);

File diff suppressed because it is too large Load Diff

View File

@ -1774,6 +1774,7 @@ int sp_ModExp_2048(mp_int* base, mp_int* exp, mp_int* mod, mp_int* res)
return err;
}
#ifdef WOLFSSL_HAVE_SP_DH
#ifdef HAVE_FFDHE_2048
extern void sp_2048_lshift_32(sp_digit* r, const sp_digit* a, int n);
#ifdef HAVE_INTEL_AVX2
@ -2055,6 +2056,7 @@ int sp_DhExp_2048(mp_int* base, const byte* exp, word32 expLen,
return err;
}
#endif
/* Perform the modular exponentiation for Diffie-Hellman.
*
* base Base. MP integer.
@ -3832,6 +3834,7 @@ int sp_ModExp_3072(mp_int* base, mp_int* exp, mp_int* mod, mp_int* res)
return err;
}
#ifdef WOLFSSL_HAVE_SP_DH
#ifdef HAVE_FFDHE_3072
extern void sp_3072_lshift_48(sp_digit* r, const sp_digit* a, int n);
#ifdef HAVE_INTEL_AVX2
@ -4113,6 +4116,7 @@ int sp_DhExp_3072(mp_int* base, const byte* exp, word32 expLen,
return err;
}
#endif
/* Perform the modular exponentiation for Diffie-Hellman.
*
* base Base. MP integer.

View File

@ -4063,7 +4063,8 @@ int fp_isprime_ex(fp_int *a, int t, int* result)
int r, res;
if (t <= 0 || t > FP_PRIME_SIZE) {
return FP_NO;
*result = FP_NO;
return FP_VAL;
}
/* check against primes table */

View File

@ -11470,7 +11470,11 @@ static int rsa_keygen_test(WC_RNG* rng)
byte* pem = NULL;
word32 idx = 0;
int derSz = 0;
#ifndef WOLFSSL_SP_MATH
int keySz = 1024;
#else
int keySz = 2048;
#endif
XMEMSET(&genKey, 0, sizeof(genKey));

View File

@ -60,11 +60,13 @@
#if SP_WORD_SIZE == 32
typedef int32_t sp_digit;
typedef uint32_t sp_int_digit;
typedef uint64_t sp_int_word;
#elif SP_WORD_SIZE == 64
typedef int64_t sp_digit;
typedef uint64_t sp_int_digit;
typedef unsigned long uint128_t __attribute__ ((mode(TI)));
typedef long int128_t __attribute__ ((mode(TI)));
typedef uint128_t sp_int_word;
#else
#error Word size not defined
#endif
@ -72,11 +74,13 @@
#if SP_WORD_SIZE == 32
typedef uint32_t sp_digit;
typedef uint32_t sp_int_digit;
typedef uint64_t sp_int_word;
#elif SP_WORD_SIZE == 64
typedef uint64_t sp_digit;
typedef uint64_t sp_int_digit;
typedef unsigned long uint128_t __attribute__ ((mode(TI)));
typedef long int128_t __attribute__ ((mode(TI)));
typedef uint128_t sp_int_word;
#else
#error Word size not defined
#endif
@ -92,12 +96,24 @@
#define SP_INT_DIGITS ((256 + SP_WORD_SIZE) / SP_WORD_SIZE)
#endif
#elif defined(WOLFSSL_SP_NO_3072)
#define SP_INT_DIGITS ((2048 + SP_WORD_SIZE) / SP_WORD_SIZE)
#if defined(WOLFSSL_HAVE_SP_DH) && defined(WOLFSSL_KEY_GEN)
#define SP_INT_DIGITS ((4096 + SP_WORD_SIZE) / SP_WORD_SIZE)
#else
#define SP_INT_DIGITS ((2048 + SP_WORD_SIZE) / SP_WORD_SIZE)
#endif
#else
#define SP_INT_DIGITS ((3072 + SP_WORD_SIZE) / SP_WORD_SIZE)
#if defined(WOLFSSL_HAVE_SP_DH) && defined(WOLFSSL_KEY_GEN)
#define SP_INT_DIGITS ((6144 + SP_WORD_SIZE) / SP_WORD_SIZE)
#else
#define SP_INT_DIGITS ((3072 + SP_WORD_SIZE) / SP_WORD_SIZE)
#endif
#endif
#define sp_isodd(a) (a->used != 0 && (a->dp[0] & 1))
#define sp_isodd(a) ((a)->used != 0 && ((a)->dp[0] & 1))
#define sp_iseven(a) ((a)->used != 0 && ((a)->dp[0] & 1) == 0)
#define sp_iszero(a) ((a)->used == 0)
#define sp_isone(a) ((a)->used == 1 && (a)->dp[0] == 1)
#define sp_abs(a, b) sp_copy(a, b)
#ifdef HAVE_WOLF_BIGINT
/* raw big integer */
@ -137,13 +153,13 @@ MP_API int sp_leading_bit(sp_int* a);
MP_API int sp_to_unsigned_bin(sp_int* a, byte* out);
MP_API int sp_to_unsigned_bin_len(sp_int* a, byte* out, int outSz);
MP_API void sp_forcezero(sp_int* a);
MP_API int sp_copy(sp_int* a, sp_int* b);
MP_API int sp_copy(sp_int* a, sp_int* r);
MP_API int sp_set(sp_int* a, sp_int_digit d);
MP_API int sp_iszero(sp_int* a);
MP_API void sp_clamp(sp_int* a);
MP_API int sp_grow(sp_int* a, int l);
MP_API int sp_sub_d(sp_int* a, sp_int_digit d, sp_int* r);
MP_API int sp_cmp_d(sp_int* a, sp_int_digit d);
MP_API int sp_sub(sp_int* a, sp_int* b, sp_int* r);
MP_API int sp_mod(sp_int* a, sp_int* m, sp_int* r);
MP_API void sp_zero(sp_int* a);
MP_API int sp_add_d(sp_int* a, sp_int_digit d, sp_int* r);
@ -151,7 +167,16 @@ MP_API int sp_lshd(sp_int* a, int s);
MP_API int sp_add(sp_int* a, sp_int* b, sp_int* r);
MP_API int sp_set_int(sp_int* a, unsigned long b);
MP_API int sp_tohex(sp_int* a, char* str);
MP_API int sp_2expt(sp_int* a, int e);
MP_API int sp_rand_prime(sp_int* r, int len, WC_RNG* rng, void* heap);
MP_API int sp_mul(sp_int* a, sp_int* b, sp_int* r);
MP_API int sp_gcd(sp_int* a, sp_int* b, sp_int* r);
MP_API int sp_invmod(sp_int* a, sp_int* m, sp_int* r);
MP_API int sp_lcm(sp_int* a, sp_int* b, sp_int* r);
MP_API int sp_exptmod(sp_int* b, sp_int* e, sp_int* m, sp_int* r);
MP_API int sp_prime_is_prime(mp_int* a, int t, int* result);
MP_API int sp_prime_is_prime_ex(mp_int* a, int t, int* result, WC_RNG* rng);
MP_API int sp_exch(sp_int* a, sp_int* b);
#define MP_OKAY 0
#define MP_NO 0
@ -172,6 +197,12 @@ MP_API int sp_tohex(sp_int* a, char* str);
#define mp_free(a)
#define mp_isodd sp_isodd
#define mp_iseven sp_iseven
#define mp_iszero sp_iszero
#define mp_isone sp_isone
#define mp_abs sp_abs
#define mp_init sp_init
#define mp_init_multi sp_init_multi
#define mp_clear sp_clear
@ -186,19 +217,28 @@ MP_API int sp_tohex(sp_int* a, char* str);
#define mp_forcezero sp_forcezero
#define mp_copy sp_copy
#define mp_set sp_set
#define mp_iszero sp_iszero
#define mp_clamp sp_clamp
#define mp_grow sp_grow
#define mp_sub_d sp_sub_d
#define mp_cmp_d sp_cmp_d
#define mp_sub sp_sub
#define mp_mod sp_mod
#define mp_zero sp_zero
#define mp_add_d sp_add_d
#define mp_lshd sp_lshd
#define mp_add sp_add
#define mp_isodd sp_isodd
#define mp_set_int sp_set_int
#define mp_tohex sp_tohex
#define mp_2expt sp_2expt
#define mp_rand_prime sp_rand_prime
#define mp_mul sp_mul
#define mp_gcd sp_gcd
#define mp_invmod sp_invmod
#define mp_lcm sp_lcm
#define mp_exptmod sp_exptmod
#define mp_prime_is_prime sp_prime_is_prime
#define mp_prime_is_prime_ex sp_prime_is_prime_ex
#define mp_exch sp_exch
#endif