diff --git a/wolfcrypt/src/dh.c b/wolfcrypt/src/dh.c index 475bdd008..a8aa85357 100644 --- a/wolfcrypt/src/dh.c +++ b/wolfcrypt/src/dh.c @@ -1974,6 +1974,7 @@ 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) @@ -1984,6 +1985,10 @@ 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; diff --git a/wolfcrypt/src/sp_c32.c b/wolfcrypt/src/sp_c32.c index 0052deace..7834a66c4 100644 --- a/wolfcrypt/src/sp_c32.c +++ b/wolfcrypt/src/sp_c32.c @@ -5363,7 +5363,7 @@ static int sp_3072_mod_exp_68(sp_digit* r, sp_digit* a, sp_digit* e, int bits, n |= e[i--] << (9 - c); c += 23; } - y = n >> 27; + y = (n >> 27) & 0x1f; n <<= 5; c -= 5; XMEMCPY(rt, t[y], sizeof(rt)); @@ -6387,7 +6387,7 @@ static int sp_3072_mod_exp_136(sp_digit* r, sp_digit* a, sp_digit* e, int bits, n |= e[i--] << (9 - c); c += 23; } - y = n >> 27; + y = (n >> 27) & 0x1f; n <<= 5; c -= 5; XMEMCPY(rt, t[y], sizeof(rt)); @@ -8708,7 +8708,7 @@ static int sp_256_ecc_mulmod_10(sp_point* r, sp_point* g, sp_digit* k, if (td == NULL) err = MEMORY_E; tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 10 * 5, heap, - DYNAMIC_TYPE_ECC); + DYNAMIC_TYPE_ECC); if (tmp == NULL) err = MEMORY_E; diff --git a/wolfcrypt/src/sp_c64.c b/wolfcrypt/src/sp_c64.c index 5bdeb7f7f..551b2afcd 100644 --- a/wolfcrypt/src/sp_c64.c +++ b/wolfcrypt/src/sp_c64.c @@ -4999,7 +4999,7 @@ static int sp_3072_mod_exp_27(sp_digit* r, sp_digit* a, sp_digit* e, int bits, n |= e[i--] << (7 - c); c += 57; } - y = n >> 59; + y = (n >> 59) & 0x1f; n <<= 5; c -= 5; XMEMCPY(rt, t[y], sizeof(rt)); @@ -5824,7 +5824,7 @@ static int sp_3072_mod_exp_54(sp_digit* r, sp_digit* a, sp_digit* e, int bits, n |= e[i--] << (7 - c); c += 57; } - y = n >> 59; + y = (n >> 59) & 0x1f; n <<= 5; c -= 5; XMEMCPY(rt, t[y], sizeof(rt)); @@ -7930,7 +7930,7 @@ static int sp_256_ecc_mulmod_5(sp_point* r, sp_point* g, sp_digit* k, if (td == NULL) err = MEMORY_E; tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 5 * 5, heap, - DYNAMIC_TYPE_ECC); + DYNAMIC_TYPE_ECC); if (tmp == NULL) err = MEMORY_E; diff --git a/wolfcrypt/src/sp_int.c b/wolfcrypt/src/sp_int.c index 83cff49ba..bce761e7a 100644 --- a/wolfcrypt/src/sp_int.c +++ b/wolfcrypt/src/sp_int.c @@ -649,6 +649,47 @@ int sp_set_int(sp_int* a, unsigned long b) } #endif +#ifdef WC_MP_TO_RADIX +/* Hex string characters. */ +static const char sp_hex_char[16] = { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' +}; + +/* Put the hex string version, big-endian, of a in str. + * + * a SP integer. + * str Hex string is stored here. + * returns MP_OKAY always. + */ +int sp_tohex(sp_int* a, char* str) +{ + int i, j; + + /* quick out if its zero */ + if (sp_iszero(a) == MP_YES) { + *str++ = '0'; + *str = '\0'; + return MP_OKAY; + } + + i = a->used - 1; + for (j = SP_WORD_SIZE - 4; j >= 0; j -= 4) { + if (((a->dp[i] >> j) & 0xf) != 0) + break; + } + for (; j >= 0; j -= 4) + *(str++) = sp_hex_char[(a->dp[i] >> j) & 0xf]; + for (--i; i >= 0; i--) { + for (j = SP_WORD_SIZE - 4; j >= 0; j -= 4) + *(str++) = sp_hex_char[(a->dp[i] >> j) & 0xf]; + } + *str = '\0'; + + return MP_OKAY; +} +#endif + #if !defined(USE_FAST_MATH) /* Returns the run time settings. * diff --git a/wolfcrypt/src/wolfmath.c b/wolfcrypt/src/wolfmath.c index b9fc47877..2cdff1539 100644 --- a/wolfcrypt/src/wolfmath.c +++ b/wolfcrypt/src/wolfmath.c @@ -29,11 +29,7 @@ /* in case user set USE_FAST_MATH there */ #include -#ifdef USE_FAST_MATH - #include -#else - #include -#endif +#include #include #include diff --git a/wolfssl/wolfcrypt/sp_int.h b/wolfssl/wolfcrypt/sp_int.h index d11357949..cbd82fb01 100644 --- a/wolfssl/wolfcrypt/sp_int.h +++ b/wolfssl/wolfcrypt/sp_int.h @@ -94,7 +94,7 @@ #else #define SP_INT_DIGITS ((256 + SP_WORD_SIZE) / SP_WORD_SIZE) #endif -#elif !defined(WOLFSSL_SP_NO_3072) +#elif defined(WOLFSSL_SP_NO_3072) #define SP_INT_DIGITS ((2048 + SP_WORD_SIZE) / SP_WORD_SIZE) #else #define SP_INT_DIGITS ((3072 + SP_WORD_SIZE) / SP_WORD_SIZE) @@ -134,6 +134,7 @@ MP_API int sp_add_d(sp_int* a, sp_int_digit d, sp_int* r); 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); typedef sp_int mp_int; typedef sp_digit mp_digit; @@ -182,6 +183,7 @@ typedef sp_digit mp_digit; #define mp_add sp_add #define mp_isodd sp_isodd #define mp_set_int sp_set_int +#define mp_tohex sp_tohex #define MP_INT_DEFINED