From 8a016879f0a1bf715c6f841aaf2e682c39ba620c Mon Sep 17 00:00:00 2001 From: David Garske Date: Fri, 15 Sep 2017 10:26:37 -0700 Subject: [PATCH] Fixes to `WOLFSSL_ATECC508A` support to enable use of `wc_ecc_export_x963_ex` and `wc_ecc_import_x963_ex`. These changes are experimental (builds, but not tested). --- wolfcrypt/src/ecc.c | 72 ++++++++++++++++------------------------- wolfssl/wolfcrypt/ecc.h | 10 +++--- 2 files changed, 33 insertions(+), 49 deletions(-) diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index d55bce4f5..437a74f58 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -1292,6 +1292,16 @@ int wc_ecc_set_curve(ecc_key* key, int keysize, int curve_id) return 0; } + +#ifdef ALT_ECC_SIZE +static void alt_fp_init(fp_int* a) +{ + a->size = FP_SIZE_ECC; + fp_zero(a); +} +#endif /* ALT_ECC_SIZE */ + + #ifndef WOLFSSL_ATECC508A /** @@ -2326,17 +2336,7 @@ int wc_ecc_mulmod(mp_int* k, ecc_point *G, ecc_point *R, mp_int* a, return wc_ecc_mulmod_ex(k, G, R, a, modulus, map, NULL); } - -#ifdef ALT_ECC_SIZE - -static void alt_fp_init(fp_int* a) -{ - a->size = FP_SIZE_ECC; - fp_zero(a); -} - -#endif /* ALT_ECC_SIZE */ - +#endif /* !WOLFSSL_ATECC508A */ /** * use a heap hint when creating new ecc_point @@ -2454,8 +2454,6 @@ int wc_ecc_cmp_point(ecc_point* a, ecc_point *b) return MP_EQ; } -#endif /* !WOLFSSL_ATECC508A */ - /** Returns whether an ECC idx is valid or not n The idx number to check @@ -2710,7 +2708,7 @@ int wc_ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out, } #ifdef WOLFSSL_ATECC508A - err = atcatls_ecdh(private_key->slot, public_key->pubkey, out); + err = atcatls_ecdh(private_key->slot, public_key->pubkey_raw, out); if (err != ATCA_SUCCESS) { err = BAD_COND_E; } @@ -3190,10 +3188,16 @@ int wc_ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key, int curve_id) #endif /* WOLFSSL_ASYNC_CRYPT */ #ifdef WOLFSSL_ATECC508A - key->type = ECC_PRIVATEKEY; - err = atcatls_create_key(key->slot, key->pubkey); - if (err != ATCA_SUCCESS) - err = BAD_COND_E; + key->type = ECC_PRIVATEKEY; + err = atcatls_create_key(key->slot, key->pubkey_raw); + if (err != ATCA_SUCCESS) { + err = BAD_COND_E; + } + + /* populate key->pubkey */ + err = mp_read_unsigned_bin(key->pubkey.x, key->pubkey_raw, 32); + if (err = MP_OKAY) + err = mp_read_unsigned_bin(key->pubkey.y, key->pubkey_raw + 32, 32); #else #ifdef WOLFSSL_HAVE_SP_ECC @@ -4176,7 +4180,7 @@ int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash, return err; } - err = atcatls_verify(hash, sigRS, key->pubkey, (bool*)res); + err = atcatls_verify(hash, sigRS, key->pubkey_raw, (bool*)res); if (err != ATCA_SUCCESS) { return BAD_COND_E; } @@ -4426,6 +4430,11 @@ int wc_ecc_import_point_der(byte* in, word32 inLen, const int curve_idx, #endif } +#ifdef WOLFSSL_ATECC508A + /* populate key->pubkey_raw */ + XMEMCPY(key->pubkey_raw, (byte*)in+1, PUB_KEY_SIZE); +#endif + /* read data */ if (err == MP_OKAY) err = mp_read_unsigned_bin(point->x, (byte*)in+1, (inLen-1)>>1); @@ -4587,14 +4596,12 @@ int wc_ecc_export_x963(ecc_key* key, byte* out, word32* outLen) { int ret = MP_OKAY; word32 numlen; -#ifndef WOLFSSL_ATECC508A #ifdef WOLFSSL_SMALL_STACK byte* buf; #else byte buf[ECC_BUFSIZE]; #endif word32 pubxlen, pubylen; -#endif /* WOLFSSL_ATECC508A */ /* return length needed only */ if (key != NULL && out == NULL && outLen != NULL) { @@ -4620,12 +4627,6 @@ int wc_ecc_export_x963(ecc_key* key, byte* out, word32* outLen) return BUFFER_E; } -#ifdef WOLFSSL_ATECC508A - /* TODO: Implement equiv call to ATECC508A */ - ret = BAD_COND_E; - -#else - /* verify public key length is less than key size */ pubxlen = mp_unsigned_bin_size(key->pubkey.x); pubylen = mp_unsigned_bin_size(key->pubkey.y); @@ -4663,7 +4664,6 @@ done: #ifdef WOLFSSL_SMALL_STACK XFREE(buf, NULL, DYNAMIC_TYPE_ECC_BUFFER); #endif -#endif /* WOLFSSL_ATECC508A */ return ret; } @@ -4994,9 +4994,7 @@ int wc_ecc_import_x963_ex(const byte* in, word32 inLen, ecc_key* key, int curve_id) { int err = MP_OKAY; -#ifndef WOLFSSL_ATECC508A int compressed = 0; -#endif /* !WOLFSSL_ATECC508A */ if (in == NULL || key == NULL) return BAD_FUNC_ARG; @@ -5009,12 +5007,6 @@ int wc_ecc_import_x963_ex(const byte* in, word32 inLen, ecc_key* key, /* make sure required variables are reset */ wc_ecc_reset(key); -#ifdef WOLFSSL_ATECC508A - /* TODO: Implement equiv call to ATECC508A */ - err = BAD_COND_E; - -#else - /* init key */ #ifdef ALT_ECC_SIZE key->pubkey.x = (mp_int*)&key->pubkey.xyz[0]; @@ -5139,7 +5131,6 @@ int wc_ecc_import_x963_ex(const byte* in, word32 inLen, ecc_key* key, mp_clear(key->pubkey.z); mp_clear(&key->k); } -#endif /* WOLFSSL_ATECC508A */ return err; } @@ -5253,12 +5244,6 @@ static int wc_ecc_export_raw(ecc_key* key, byte* qx, word32* qxLen, #endif /* WOLFSSL_ATECC508A */ } -#ifdef WOLFSSL_ATECC508A - /* TODO: Implement equiv call to ATECC508A */ - return BAD_COND_E; - -#else - /* public x component */ err = mp_to_unsigned_bin(key->pubkey.x, qx + (numLen - mp_unsigned_bin_size(key->pubkey.x))); @@ -5272,7 +5257,6 @@ static int wc_ecc_export_raw(ecc_key* key, byte* qx, word32* qxLen, return err; return 0; -#endif /* WOLFSSL_ATECC508A */ } diff --git a/wolfssl/wolfcrypt/ecc.h b/wolfssl/wolfcrypt/ecc.h index 5bae502bb..f60aeae56 100644 --- a/wolfssl/wolfcrypt/ecc.h +++ b/wolfssl/wolfcrypt/ecc.h @@ -285,12 +285,11 @@ struct ecc_key { const ecc_set_type* dp; /* domain parameters, either points to NIST curves (idx >= 0) or user supplied */ void* heap; /* heap hint */ -#ifdef WOLFSSL_ATECC508A - int slot; /* Key Slot Number (-1 unknown) */ - byte pubkey[PUB_KEY_SIZE]; -#else ecc_point pubkey; /* public key */ mp_int k; /* private key */ +#ifdef WOLFSSL_ATECC508A + int slot; /* Key Slot Number (-1 unknown) */ + byte pubkey_raw[PUB_KEY_SIZE]; #endif #ifdef WOLFSSL_ASYNC_CRYPT mp_int* r; /* sign/verify temps */ @@ -409,7 +408,6 @@ int wc_ecc_get_curve_id_from_params(int fieldSize, const byte* Bf, word32 BfSz, const byte* order, word32 orderSz, const byte* Gx, word32 GxSz, const byte* Gy, word32 GySz, int cofactor); -#ifndef WOLFSSL_ATECC508A WOLFSSL_API ecc_point* wc_ecc_new_point(void); @@ -425,6 +423,8 @@ WOLFSSL_API int wc_ecc_cmp_point(ecc_point* a, ecc_point *b); WOLFSSL_API int wc_ecc_point_is_at_infinity(ecc_point *p); + +#ifndef WOLFSSL_ATECC508A WOLFSSL_API int wc_ecc_mulmod(mp_int* k, ecc_point *G, ecc_point *R, mp_int* a, mp_int* modulus, int map);