From a936c609f3d6e44d46c362f6cd0b15fe1831a251 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 6 Dec 2018 13:11:14 -0800 Subject: [PATCH] Fixes for ATECC508A/608A CryptoAuthLib demo. --- wolfcrypt/src/ecc.c | 14 +++++++++----- wolfcrypt/src/port/atmel/atmel.c | 20 +++++++++++++++++--- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index a9263b3a1..58562bc2a 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -3978,15 +3978,19 @@ int wc_ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key, int curve_id) #ifdef WOLFSSL_ATECC508A key->type = ECC_PRIVATEKEY; + key->slot = atmel_ecc_alloc(ATMEL_SLOT_ECDHE); err = atmel_ecc_create_key(key->slot, key->pubkey_raw); /* populate key->pubkey */ - err = mp_read_unsigned_bin(key->pubkey.x, key->pubkey_raw, - ECC_MAX_CRYPTO_HW_SIZE); - if (err == MP_OKAY) + if (err == 0 && key->pubkey.x) { + err = mp_read_unsigned_bin(key->pubkey.x, key->pubkey_raw, + ECC_MAX_CRYPTO_HW_SIZE); + } + if (err == 0 && key->pubkey.y) { err = mp_read_unsigned_bin(key->pubkey.y, key->pubkey_raw + ECC_MAX_CRYPTO_HW_SIZE, ECC_MAX_CRYPTO_HW_SIZE); + } #else #ifdef WOLFSSL_HAVE_SP_ECC @@ -4145,7 +4149,7 @@ int wc_ecc_init_ex(ecc_key* key, void* heap, int devId) #endif #ifdef WOLFSSL_ATECC508A - key->slot = -1; + key->slot = ATECC_INVALID_SLOT; #else #ifdef ALT_ECC_SIZE key->pubkey.x = (mp_int*)&key->pubkey.xyz[0]; @@ -4789,7 +4793,7 @@ int wc_ecc_free(ecc_key* key) #ifdef WOLFSSL_ATECC508A atmel_ecc_free(key->slot); - key->slot = -1; + key->slot = ATECC_INVALID_SLOT; #else mp_clear(key->pubkey.x); diff --git a/wolfcrypt/src/port/atmel/atmel.c b/wolfcrypt/src/port/atmel/atmel.c index e6a2ef92c..d5934a42d 100644 --- a/wolfcrypt/src/port/atmel/atmel.c +++ b/wolfcrypt/src/port/atmel/atmel.c @@ -52,6 +52,10 @@ #ifdef WOLFSSL_ATECC508A +#ifdef WOLFSSL_ATECC508A_TLS + extern ATCA_STATUS device_init_default(void); +#endif + static int mAtcaInitDone = 0; /* ATECC slotId handling */ @@ -350,6 +354,11 @@ int atmel_ecc_create_key(int slotId, byte* peerKey) { int ret; + /* verify provided slotId */ + if (slotId == ATECC_INVALID_SLOT) { + return WC_HW_WAIT_E; + } + /* generate new ephemeral key on device */ ret = atcab_genkey(slotId, peerKey); ret = atmel_ecc_translate_err(ret); @@ -420,6 +429,14 @@ int atmel_init(void) return WC_HW_E; } + /* show revision information */ + atmel_show_rev_info(); + + #ifdef WOLFSSL_ATECC508A_TLS + /* Configure the ECC508 for use with TLS API functions */ + device_init_default(); + #endif + /* Init the I2C pipe encryption key. */ /* Value is generated/stored during pair for the ATECC508A and stored on micro flash */ @@ -429,9 +446,6 @@ int atmel_init(void) return WC_HW_E; } - /* show revision information */ - atmel_show_rev_info(); - mAtcaInitDone = 1; } #endif /* WOLFSSL_ATECC508A */