forked from wolfSSL/wolfssl
Merge pull request #2516 from danielinux/freescale-ltc-spmath
Fixed Freescale LTC crypto module to compile with SP math
This commit is contained in:
@ -5543,26 +5543,26 @@ int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash,
|
||||
CRYS_ECDSA_VerifyUserContext_t sigCtxTemp;
|
||||
word32 msgLenInBytes = hashlen;
|
||||
CRYS_ECPKI_HASH_OpMode_t hash_mode;
|
||||
#elif !defined(WOLFSSL_SP_MATH)
|
||||
#elif !defined(WOLFSSL_SP_MATH) || defined(FREESCALE_LTC_ECC)
|
||||
int did_init = 0;
|
||||
ecc_point *mG = NULL, *mQ = NULL;
|
||||
#ifdef WOLFSSL_SMALL_STACK
|
||||
#ifdef WOLFSSL_SMALL_STACK
|
||||
mp_int* v = NULL;
|
||||
mp_int* w = NULL;
|
||||
mp_int* u1 = NULL;
|
||||
mp_int* u2 = NULL;
|
||||
#if !defined(WOLFSSL_ASYNC_CRYPT) || !defined(HAVE_CAVIUM_V)
|
||||
#if !defined(WOLFSSL_ASYNC_CRYPT) || !defined(HAVE_CAVIUM_V)
|
||||
mp_int* e_lcl = NULL;
|
||||
#endif
|
||||
#else /* WOLFSSL_SMALL_STACK */
|
||||
#endif
|
||||
#else /* WOLFSSL_SMALL_STACK */
|
||||
mp_int v[1];
|
||||
mp_int w[1];
|
||||
mp_int u1[1];
|
||||
mp_int u2[1];
|
||||
#if !defined(WOLFSSL_ASYNC_CRYPT) || !defined(HAVE_CAVIUM_V)
|
||||
#if !defined(WOLFSSL_ASYNC_CRYPT) || !defined(HAVE_CAVIUM_V)
|
||||
mp_int e_lcl[1];
|
||||
#endif
|
||||
#endif /* WOLFSSL_SMALL_STACK */
|
||||
#endif
|
||||
#endif /* WOLFSSL_SMALL_STACK */
|
||||
mp_int* e;
|
||||
DECLARE_CURVE_SPECS(curve, ECC_CURVE_FIELD_COUNT);
|
||||
#endif
|
||||
@ -5661,7 +5661,7 @@ int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash,
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef WOLFSSL_SP_MATH
|
||||
#if defined(WOLFSSL_SP_MATH) && !defined(FREESCALE_LTC_ECC)
|
||||
if (key->idx != ECC_CUSTOM_IDX && ecc_sets[key->idx].id == ECC_SECP256R1) {
|
||||
return sp_ecc_verify_256(hash, hashlen, key->pubkey.x, key->pubkey.y,
|
||||
key->pubkey.z, r, s, res, key->heap);
|
||||
@ -5669,7 +5669,7 @@ int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash,
|
||||
else
|
||||
return WC_KEY_SIZE_E;
|
||||
#else
|
||||
#ifdef WOLFSSL_HAVE_SP_ECC
|
||||
#if defined WOLFSSL_HAVE_SP_ECC && !defined(FREESCALE_LTC_ECC)
|
||||
#ifndef WOLFSSL_SP_NO_256
|
||||
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
|
||||
if (key->asyncDev.marker != WOLFSSL_ASYNC_MARKER_ECC)
|
||||
|
@ -52,14 +52,10 @@ int ksdk_port_init(void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* LTC TFM */
|
||||
#if defined(FREESCALE_LTC_TFM)
|
||||
|
||||
/* Reverse array in memory (in place) */
|
||||
static void ltc_reverse_array(uint8_t *src, size_t src_len)
|
||||
{
|
||||
int i;
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < src_len / 2; i++) {
|
||||
uint8_t tmp;
|
||||
@ -70,6 +66,8 @@ static void ltc_reverse_array(uint8_t *src, size_t src_len)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#ifndef WOLFSSL_SP_MATH
|
||||
/* same as mp_to_unsigned_bin() with mp_reverse() skipped */
|
||||
static int mp_to_unsigned_lsb_bin(mp_int *a, unsigned char *b)
|
||||
{
|
||||
@ -88,6 +86,7 @@ static int mp_to_unsigned_lsb_bin(mp_int *a, unsigned char *b)
|
||||
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int ltc_get_lsb_bin_from_mp_int(uint8_t *dst, mp_int *A, uint16_t *psz)
|
||||
{
|
||||
@ -95,12 +94,22 @@ static int ltc_get_lsb_bin_from_mp_int(uint8_t *dst, mp_int *A, uint16_t *psz)
|
||||
uint16_t sz;
|
||||
|
||||
sz = mp_unsigned_bin_size(A);
|
||||
#ifndef WOLFSSL_SP_MATH
|
||||
res = mp_to_unsigned_lsb_bin(A, dst); /* result is lsbyte at lowest addr as required by LTC */
|
||||
#else
|
||||
res = mp_to_unsigned_bin(A, dst);
|
||||
if (res == MP_OKAY) {
|
||||
ltc_reverse_array(dst, sz);
|
||||
}
|
||||
#endif
|
||||
*psz = sz;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/* LTC TFM */
|
||||
#if defined(FREESCALE_LTC_TFM)
|
||||
|
||||
|
||||
/* these function are used by wolfSSL upper layers (like RSA) */
|
||||
|
||||
/* c = a * b */
|
||||
@ -113,9 +122,11 @@ int mp_mul(mp_int *A, mp_int *B, mp_int *C)
|
||||
|
||||
/* if unsigned mul can fit into LTC PKHA let's use it, otherwise call software mul */
|
||||
if ((szA <= LTC_MAX_INT_BYTES / 2) && (szB <= LTC_MAX_INT_BYTES / 2)) {
|
||||
int neg;
|
||||
int neg = 0;
|
||||
|
||||
#ifndef WOLFSSL_SP_MATH
|
||||
neg = (A->sign == B->sign) ? MP_ZPOS : MP_NEG;
|
||||
#endif
|
||||
|
||||
/* unsigned multiply */
|
||||
uint8_t *ptrA = (uint8_t *)XMALLOC(LTC_MAX_INT_BYTES, 0, DYNAMIC_TYPE_BIGINT);
|
||||
@ -140,8 +151,10 @@ int mp_mul(mp_int *A, mp_int *B, mp_int *C)
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef WOLFSSL_SP_MATH
|
||||
/* fix sign */
|
||||
C->sign = neg;
|
||||
#endif
|
||||
if (ptrA) {
|
||||
XFREE(ptrA, NULL, DYNAMIC_TYPE_BIGINT);
|
||||
}
|
||||
@ -153,7 +166,11 @@ int mp_mul(mp_int *A, mp_int *B, mp_int *C)
|
||||
}
|
||||
}
|
||||
else {
|
||||
#ifdef WOLFSSL_SP_MATH
|
||||
res = sp_mul(A, B, C);
|
||||
#else
|
||||
res = wolfcrypt_mp_mul(A, B, C);
|
||||
#endif
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@ -169,13 +186,15 @@ int mp_mod(mp_int *a, mp_int *b, mp_int *c)
|
||||
if ((szA <= LTC_MAX_INT_BYTES) && (szB <= LTC_MAX_INT_BYTES))
|
||||
{
|
||||
#endif /* FREESCALE_LTC_TFM_RSA_4096_ENABLE */
|
||||
int neg;
|
||||
int neg = 0;
|
||||
uint8_t *ptrA = (uint8_t *)XMALLOC(LTC_MAX_INT_BYTES, 0, DYNAMIC_TYPE_BIGINT);
|
||||
uint8_t *ptrB = (uint8_t *)XMALLOC(LTC_MAX_INT_BYTES, 0, DYNAMIC_TYPE_BIGINT);
|
||||
uint8_t *ptrC = (uint8_t *)XMALLOC(LTC_MAX_INT_BYTES, 0, DYNAMIC_TYPE_BIGINT);
|
||||
|
||||
#ifndef WOLFSSL_SP_MATH
|
||||
/* get sign for the result */
|
||||
neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
|
||||
#endif
|
||||
|
||||
/* get remainder of unsigned a divided by unsigned b */
|
||||
if (ptrA && ptrB && ptrC) {
|
||||
@ -200,8 +219,10 @@ int mp_mod(mp_int *a, mp_int *b, mp_int *c)
|
||||
res = MP_MEM;
|
||||
}
|
||||
|
||||
#ifndef WOLFSSL_SP_MATH
|
||||
/* fix sign */
|
||||
c->sign = neg;
|
||||
#endif
|
||||
|
||||
if (ptrA) {
|
||||
XFREE(ptrA, NULL, DYNAMIC_TYPE_BIGINT);
|
||||
@ -257,7 +278,9 @@ int mp_invmod(mp_int *a, mp_int *b, mp_int *c)
|
||||
res = MP_MEM;
|
||||
}
|
||||
|
||||
#ifndef WOLFSSL_SP_MATH
|
||||
c->sign = a->sign;
|
||||
#endif
|
||||
if (ptrA) {
|
||||
XFREE(ptrA, NULL, DYNAMIC_TYPE_BIGINT);
|
||||
}
|
||||
@ -297,6 +320,7 @@ int mp_mulmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d)
|
||||
/* if A or B is negative, subtract abs(A) or abs(B) from modulus to get positive integer representation of the
|
||||
* same number */
|
||||
res = mp_init(&t);
|
||||
#ifndef WOLFSSL_SP_MATH
|
||||
if (a->sign) {
|
||||
if (res == MP_OKAY)
|
||||
res = mp_add(a, c, &t);
|
||||
@ -309,6 +333,7 @@ int mp_mulmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d)
|
||||
if (res == MP_OKAY)
|
||||
res = mp_copy(&t, b);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (res == MP_OKAY && ptrA && ptrB && ptrC && ptrD) {
|
||||
uint16_t sizeA, sizeB, sizeC, sizeD;
|
||||
@ -413,12 +438,14 @@ int mp_exptmod(mp_int *G, mp_int *X, mp_int *P, mp_int *Y)
|
||||
|
||||
/* if G is negative, add modulus to convert to positive number for LTC */
|
||||
res = mp_init(&t);
|
||||
#ifndef WOLFSSL_SP_MATH
|
||||
if (G->sign) {
|
||||
if (res == MP_OKAY)
|
||||
res = mp_add(G, P, &t);
|
||||
if (res == MP_OKAY)
|
||||
res = mp_copy(&t, G);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (res == MP_OKAY && ptrG && ptrX && ptrP) {
|
||||
res = ltc_get_lsb_bin_from_mp_int(ptrG, G, &sizeG);
|
||||
@ -731,7 +758,9 @@ int wc_ecc_mulmod_ex(mp_int *k, ecc_point *G, ecc_point *R, mp_int* a,
|
||||
/* if k is negative, we compute the multiplication with abs(-k)
|
||||
* with result (x, y) and modify the result to (x, -y)
|
||||
*/
|
||||
#ifndef WOLFSSL_SP_MATH
|
||||
R->y->sign = k->sign;
|
||||
#endif
|
||||
}
|
||||
if (res == MP_OKAY)
|
||||
res = mp_set(R->z, 1);
|
||||
|
@ -52,6 +52,16 @@ WOLFSSL_LOCAL int sp_ModExp_3072(sp_int* base, sp_int* exp, sp_int* mod,
|
||||
|
||||
#endif
|
||||
|
||||
int sp_get_digit_count(sp_int *a)
|
||||
{
|
||||
int ret;
|
||||
if (!a)
|
||||
ret = 0;
|
||||
else
|
||||
ret = a->used;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Initialize the big number to be zero.
|
||||
*
|
||||
* a SP integer.
|
||||
@ -388,6 +398,18 @@ int sp_copy(sp_int* a, sp_int* r)
|
||||
}
|
||||
return MP_OKAY;
|
||||
}
|
||||
|
||||
/* creates "a" then copies b into it */
|
||||
int sp_init_copy (sp_int * a, sp_int * b)
|
||||
{
|
||||
int res;
|
||||
if ((res = sp_init(a)) == MP_OKAY) {
|
||||
if((res = sp_copy (b, a)) != MP_OKAY) {
|
||||
sp_clear(a);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Set the big number to be the value of the digit.
|
||||
@ -561,7 +583,7 @@ int sp_sub(sp_int* a, sp_int* b, sp_int* r)
|
||||
* n Number of bits to shift.
|
||||
* r SP integer result.
|
||||
*/
|
||||
static void sp_rshb(sp_int* a, int n, sp_int* r)
|
||||
void sp_rshb(sp_int* a, int n, sp_int* r)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
@ -704,6 +726,8 @@ static int sp_div(sp_int* a, sp_int* d, sp_int* r, sp_int* rem)
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
#ifndef FREESCALE_LTC_TFM
|
||||
/* Calculate the remainder of dividing a by m: r = a mod m.
|
||||
*
|
||||
* a SP integer.
|
||||
@ -716,6 +740,7 @@ int sp_mod(sp_int* a, sp_int* m, sp_int* r)
|
||||
return sp_div(a, m, NULL, r);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* Clear all data in the big number and sets value to zero.
|
||||
*
|
||||
|
@ -25,6 +25,8 @@
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#ifdef USE_FAST_MATH
|
||||
#include <wolfssl/wolfcrypt/tfm.h>
|
||||
#elif defined WOLFSSL_SP_MATH
|
||||
#include <wolfssl/wolfcrypt/sp_int.h>
|
||||
#else
|
||||
#include <wolfssl/wolfcrypt/integer.h>
|
||||
#endif
|
||||
@ -44,6 +46,10 @@ int ksdk_port_init(void);
|
||||
int wolfcrypt_mp_mod(mp_int *a, mp_int *b, mp_int *c);
|
||||
int wolfcrypt_mp_invmod(mp_int *a, mp_int *b, mp_int *c);
|
||||
int wolfcrypt_mp_exptmod(mp_int *G, mp_int *X, mp_int *P, mp_int *Y);
|
||||
|
||||
/* Exported mp_mulmod function */
|
||||
int mp_mulmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
|
||||
|
||||
#endif /* FREESCALE_LTC_TFM */
|
||||
|
||||
#if defined(FREESCALE_LTC_ECC)
|
||||
|
@ -187,6 +187,10 @@ 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);
|
||||
MP_API int sp_get_digit_count(sp_int *a);
|
||||
MP_API int sp_init_copy (sp_int * a, sp_int * b);
|
||||
MP_API void sp_rshb(sp_int* a, int n, sp_int* r);
|
||||
|
||||
|
||||
#define MP_OKAY 0
|
||||
#define MP_NO 0
|
||||
@ -249,6 +253,9 @@ MP_API int sp_exch(sp_int* a, sp_int* b);
|
||||
#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
|
||||
#define get_digit_count sp_get_digit_count
|
||||
#define mp_init_copy sp_init_copy
|
||||
#define mp_rshb(A,x) sp_rshb(A,x,A)
|
||||
|
||||
#endif
|
||||
|
||||
|
Reference in New Issue
Block a user