Merge branch 'ecc2'

This commit is contained in:
John Safranek
2015-01-23 10:37:55 -08:00
7 changed files with 410 additions and 255 deletions

View File

@ -1391,6 +1391,7 @@ void bench_eccKeyGen(void)
start = current_time(1); start = current_time(1);
for(i = 0; i < genTimes; i++) { for(i = 0; i < genTimes; i++) {
wc_ecc_init(&genKey);
wc_ecc_make_key(&rng, 32, &genKey); wc_ecc_make_key(&rng, 32, &genKey);
wc_ecc_free(&genKey); wc_ecc_free(&genKey);
} }

View File

@ -3150,6 +3150,10 @@ static int ConfirmSignature(const byte* buf, word32 bufSz,
} }
#endif #endif
if (wc_ecc_init(pubKey) < 0) {
WOLFSSL_MSG("Failed to initialize key");
break; /* not confirmed */
}
if (wc_ecc_import_x963(key, keySz, pubKey) < 0) { if (wc_ecc_import_x963(key, keySz, pubKey) < 0) {
WOLFSSL_MSG("ASN Key import error ECC"); WOLFSSL_MSG("ASN Key import error ECC");
} }
@ -3163,8 +3167,9 @@ static int ConfirmSignature(const byte* buf, word32 bufSz,
} else } else
ret = 1; /* match */ ret = 1; /* match */
wc_ecc_free(pubKey);
} }
wc_ecc_free(pubKey);
#ifdef WOLFSSL_SMALL_STACK #ifdef WOLFSSL_SMALL_STACK
XFREE(pubKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pubKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif #endif

File diff suppressed because it is too large Load Diff

View File

@ -180,6 +180,15 @@ int wc_InitRsaKey(RsaKey* key, void* heap)
key->d.dp = key->p.dp = 0; /* private alloc parts */ key->d.dp = key->p.dp = 0; /* private alloc parts */
key->q.dp = key->dP.dp = 0; key->q.dp = key->dP.dp = 0;
key->u.dp = key->dQ.dp = 0; key->u.dp = key->dQ.dp = 0;
#else
mp_init(&key->n);
mp_init(&key->e);
mp_init(&key->d);
mp_init(&key->p);
mp_init(&key->q);
mp_init(&key->dP);
mp_init(&key->dQ);
mp_init(&key->u);
#endif #endif
return 0; return 0;

View File

@ -417,7 +417,7 @@ void fp_mul_comba(fp_int *A, fp_int *B, fp_int *C)
} }
if (A == C || B == C) { if (A == C || B == C) {
fp_zero(&tmp); fp_init(&tmp);
dst = &tmp; dst = &tmp;
} else { } else {
fp_zero(C); fp_zero(C);
@ -685,7 +685,7 @@ int fp_mod(fp_int *a, fp_int *b, fp_int *c)
fp_int t; fp_int t;
int err; int err;
fp_zero(&t); fp_init(&t);
if ((err = fp_div(a, b, NULL, &t)) != FP_OKAY) { if ((err = fp_div(a, b, NULL, &t)) != FP_OKAY) {
return err; return err;
} }
@ -922,7 +922,7 @@ top:
int fp_mulmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d) int fp_mulmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
{ {
fp_int tmp; fp_int tmp;
fp_zero(&tmp); fp_init(&tmp);
fp_mul(a, b, &tmp); fp_mul(a, b, &tmp);
return fp_mod(&tmp, c, d); return fp_mod(&tmp, c, d);
} }
@ -1339,7 +1339,7 @@ void fp_sqr_comba(fp_int *A, fp_int *B)
COMBA_CLEAR; COMBA_CLEAR;
if (A == B) { if (A == B) {
fp_zero(&tmp); fp_init(&tmp);
dst = &tmp; dst = &tmp;
} else { } else {
fp_zero(B); fp_zero(B);
@ -1844,6 +1844,22 @@ int mp_init (mp_int * a)
return MP_OKAY; return MP_OKAY;
} }
#ifdef ALT_ECC_SIZE
void fp_init(fp_int *a)
{
a->size = FP_SIZE;
fp_zero(a);
}
void fp_zero(fp_int *a)
{
a->used = 0;
a->sign = FP_ZPOS;
XMEMSET(a->dp, 0, a->size * sizeof(fp_digit));
}
#endif
/* clear one (frees) */ /* clear one (frees) */
void mp_clear (mp_int * a) void mp_clear (mp_int * a)
{ {
@ -1958,6 +1974,17 @@ int mp_sub_d(fp_int *a, fp_digit b, fp_int *c)
} }
#ifdef ALT_ECC_SIZE
void fp_copy(fp_int *a, fp_int* b)
{
if (a != b) {
b->used = a->used;
b->sign = a->sign;
XMEMCPY(b->dp, a->dp, a->used * sizeof(fp_digit));
}
}
#endif
/* fast math conversion */ /* fast math conversion */
int mp_copy(fp_int* a, fp_int* b) int mp_copy(fp_int* a, fp_int* b)
{ {
@ -2014,7 +2041,7 @@ int mp_set_int(fp_int *a, fp_digit b)
int fp_sqrmod(fp_int *a, fp_int *b, fp_int *c) int fp_sqrmod(fp_int *a, fp_int *b, fp_int *c)
{ {
fp_int tmp; fp_int tmp;
fp_zero(&tmp); fp_init(&tmp);
fp_sqr(a, &tmp); fp_sqr(a, &tmp);
return fp_mod(&tmp, b, c); return fp_mod(&tmp, b, c);
} }
@ -2376,7 +2403,7 @@ void fp_gcd(fp_int *a, fp_int *b, fp_int *c)
fp_init_copy(&v, a); fp_init_copy(&v, a);
} }
fp_zero(&r); fp_init(&r);
while (fp_iszero(&v) == FP_NO) { while (fp_iszero(&v) == FP_NO) {
fp_mod(&u, &v, &r); fp_mod(&u, &v, &r);
fp_copy(&v, &u); fp_copy(&v, &u);
@ -2393,6 +2420,7 @@ void fp_gcd(fp_int *a, fp_int *b, fp_int *c)
void fp_add_d(fp_int *a, fp_digit b, fp_int *c) void fp_add_d(fp_int *a, fp_digit b, fp_int *c)
{ {
fp_int tmp; fp_int tmp;
fp_init(&tmp);
fp_set(&tmp, b); fp_set(&tmp, b);
fp_add(a,&tmp,c); fp_add(a,&tmp,c);
} }

View File

@ -57,12 +57,37 @@ typedef struct {
} ecc_set_type; } ecc_set_type;
#ifdef ALT_ECC_SIZE
#ifndef FP_MAX_BITS_ECC
#define FP_MAX_BITS_ECC 512
#endif
#define FP_MAX_SIZE_ECC (FP_MAX_BITS_ECC+(8*DIGIT_BIT))
#if FP_MAX_BITS_ECC % CHAR_BIT
#error FP_MAX_BITS_ECC must be a multiple of CHAR_BIT
#endif
#define FP_SIZE_ECC (FP_MAX_SIZE_ECC/DIGIT_BIT)
/* This needs to match the size of the fp_int struct, except the
* fp_digit array will be shorter. */
typedef struct alt_fp_int {
int used, sign, size;
fp_digit dp[FP_SIZE_ECC];
} alt_fp_int;
#endif
/* A point on an ECC curve, stored in Jacbobian format such that (x,y,z) => /* A point on an ECC curve, stored in Jacbobian format such that (x,y,z) =>
(x/z^2, y/z^3, 1) when interpreted as affine */ (x/z^2, y/z^3, 1) when interpreted as affine */
typedef struct { typedef struct {
mp_int x; /* The x coordinate */ #ifndef ALT_ECC_SIZE
mp_int y; /* The y coordinate */ mp_int x[1]; /* The x coordinate */
mp_int z; /* The z coordinate */ mp_int y[1]; /* The y coordinate */
mp_int z[1]; /* The z coordinate */
#else
mp_int* x; /* The x coordinate */
mp_int* y; /* The y coordinate */
mp_int* z; /* The z coordinate */
alt_fp_int xyz[3];
#endif
} ecc_point; } ecc_point;
@ -95,7 +120,7 @@ WOLFSSL_API
int wc_ecc_verify_hash(const byte* sig, word32 siglen, const byte* hash, int wc_ecc_verify_hash(const byte* sig, word32 siglen, const byte* hash,
word32 hashlen, int* stat, ecc_key* key); word32 hashlen, int* stat, ecc_key* key);
WOLFSSL_API WOLFSSL_API
void wc_ecc_init(ecc_key* key); int wc_ecc_init(ecc_key* key);
WOLFSSL_API WOLFSSL_API
void wc_ecc_free(ecc_key* key); void wc_ecc_free(ecc_key* key);
WOLFSSL_API WOLFSSL_API

View File

@ -270,9 +270,12 @@
/* a FP type */ /* a FP type */
typedef struct { typedef struct {
fp_digit dp[FP_SIZE]; int used,
int used,
sign; sign;
#ifdef ALT_ECC_SIZE
int size;
#endif
fp_digit dp[FP_SIZE];
} fp_int; } fp_int;
/* externally define this symbol to ignore the default settings, useful for changing the build from the make process */ /* externally define this symbol to ignore the default settings, useful for changing the build from the make process */
@ -353,8 +356,13 @@ typedef struct {
/*const char *fp_ident(void);*/ /*const char *fp_ident(void);*/
/* initialize [or zero] an fp int */ /* initialize [or zero] an fp int */
#define fp_init(a) (void)XMEMSET((a), 0, sizeof(fp_int)) #ifdef ALT_ECC_SIZE
#define fp_zero(a) fp_init(a) void fp_init(fp_int *a);
void fp_zero(fp_int *a);
#else
#define fp_init(a) (void)XMEMSET((a), 0, sizeof(fp_int))
#define fp_zero(a) fp_init(a)
#endif
/* zero/even/odd ? */ /* zero/even/odd ? */
#define fp_iszero(a) (((a)->used == 0) ? FP_YES : FP_NO) #define fp_iszero(a) (((a)->used == 0) ? FP_YES : FP_NO)
@ -365,7 +373,11 @@ typedef struct {
void fp_set(fp_int *a, fp_digit b); void fp_set(fp_int *a, fp_digit b);
/* copy from a to b */ /* copy from a to b */
#define fp_copy(a, b) (void)(((a) != (b)) ? ((void)XMEMCPY((b), (a), sizeof(fp_int))) : (void)0) #ifndef ALT_ECC_SIZE
#define fp_copy(a, b) (void)(((a) != (b)) ? ((void)XMEMCPY((b), (a), sizeof(fp_int))) : (void)0)
#else
void fp_copy(fp_int *a, fp_int *b);
#endif
#define fp_init_copy(a, b) fp_copy(b, a) #define fp_init_copy(a, b) fp_copy(b, a)
/* clamp digits */ /* clamp digits */