diff --git a/wolfcrypt/src/port/Espressif/esp32_aes.c b/wolfcrypt/src/port/Espressif/esp32_aes.c index 66ec98884..e49a19146 100644 --- a/wolfcrypt/src/port/Espressif/esp32_aes.c +++ b/wolfcrypt/src/port/Espressif/esp32_aes.c @@ -27,6 +27,8 @@ #endif #include +#ifndef NO_AES + #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \ !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_AES) @@ -44,8 +46,9 @@ static int espaes_CryptHwMutexInit = 0; */ static int esp_aes_hw_InUse() { - ESP_LOGV(TAG, "enter esp_aes_hw_InUse"); int ret = 0; + + ESP_LOGV(TAG, "enter esp_aes_hw_InUse"); if(espaes_CryptHwMutexInit == 0) { ret = esp_CryptHwMutexInit(&aes_mutex); @@ -74,7 +77,7 @@ static int esp_aes_hw_InUse() */ static void esp_aes_hw_Leave( void ) { - ESP_LOGV(TAG, "enter esp_aes_hw_InUse"); + ESP_LOGV(TAG, "enter esp_aes_hw_Leave"); /* Disable AES hardware */ periph_module_disable(PERIPH_AES_MODULE); @@ -89,8 +92,10 @@ static void esp_aes_hw_Leave( void ) */ static void esp_aes_hw_Set_KeyMode(Aes *ctx, ESP32_AESPROCESS mode) { - ESP_LOGV(TAG, "enter esp_aes_hw_InUse"); + int i; word32 mode_ = 0; + + ESP_LOGV(TAG, "enter esp_aes_hw_Set_KeyMode"); /* check mode */ if(mode == ESP32_AES_UPDATEKEY_ENCRYPT) { @@ -103,7 +108,7 @@ static void esp_aes_hw_Set_KeyMode(Aes *ctx, ESP32_AESPROCESS mode) } /* update key */ - for(int i=0;i<(ctx->keylen)/sizeof(word32);i++){ + for(i=0;i<(ctx->keylen)/sizeof(word32);i++){ DPORT_REG_WRITE(AES_KEY_BASE + (i*4), *(((word32*)ctx->key) + i)); } @@ -130,9 +135,10 @@ static void esp_aes_hw_Set_KeyMode(Aes *ctx, ESP32_AESPROCESS mode) */ static void esp_aes_bk(const byte* in, byte* out) { - ESP_LOGV(TAG, "enter esp_aes_hw_InUse"); const word32 *inwords = (const word32 *)in; word32 *outwords = (word32 *)out; + + ESP_LOGV(TAG, "enter esp_aes_bk"); /* copy text for encrypting/decrypting blocks */ DPORT_REG_WRITE(AES_TEXT_BASE, inwords[0]); @@ -209,12 +215,13 @@ int wc_esp32AesDecrypt(Aes *aes, const byte* in, byte* out) */ int wc_esp32AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz) { - ESP_LOGV(TAG, "enter wc_esp32AesCbcEncrypt"); int i; int offset = 0; word32 blocks = (sz / AES_BLOCK_SIZE); byte *iv; byte temp_block[AES_BLOCK_SIZE]; + + ESP_LOGV(TAG, "enter wc_esp32AesCbcEncrypt"); iv = (byte*)aes->reg; @@ -254,13 +261,14 @@ int wc_esp32AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz) */ int wc_esp32AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz) { - ESP_LOGV(TAG, "enter wc_esp32AesCbcDecrypt"); int i; int offset = 0; word32 blocks = (sz / AES_BLOCK_SIZE); byte* iv; byte temp_block[AES_BLOCK_SIZE]; + ESP_LOGV(TAG, "enter wc_esp32AesCbcDecrypt"); + iv = (byte*)aes->reg; esp_aes_hw_InUse(); @@ -288,3 +296,4 @@ int wc_esp32AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz) } #endif /* WOLFSSL_ESP32WROOM32_CRYPT */ +#endif /* NO_AES */ diff --git a/wolfcrypt/src/port/Espressif/esp32_sha.c b/wolfcrypt/src/port/Espressif/esp32_sha.c index ef3d68b83..cddac7c5a 100644 --- a/wolfcrypt/src/port/Espressif/esp32_sha.c +++ b/wolfcrypt/src/port/Espressif/esp32_sha.c @@ -1,5 +1,5 @@ /** - * sha.c + * esp32_sha.c * Copyright (C) 2006-2018 wolfSSL Inc. * * This file is part of wolfSSL. @@ -25,6 +25,10 @@ #include #endif #include + +#if !defined(NO_SHA) || !defined(NO_SHA256) || defined(WC_SHA384) || \ + defined(WC_SHA512) + #include "wolfssl/wolfcrypt/logging.h" @@ -65,13 +69,17 @@ static int espsha_CryptHwMutexInit = 0; */ static word32 esp_sha_digest_size(enum SHA_TYPE type) { - ESP_LOGV(TAG, "enter esp_sha_digest_sit"); + ESP_LOGV(TAG, "enter esp_sha_digest_size"); switch(type){ +#ifndef NO_SHA case SHA1: return WC_SHA_DIGEST_SIZE; +#endif +#ifndef NO_SHA256 case SHA2_256: return WC_SHA256_DIGEST_SIZE; +#endif #ifdef WOLFSSL_SHA384 case SHA2_384: return WC_SHA384_DIGEST_SIZE; @@ -80,9 +88,19 @@ static word32 esp_sha_digest_size(enum SHA_TYPE type) case SHA2_512: return WC_SHA512_DIGEST_SIZE; #endif +#ifndef NO_SHA default:return WC_SHA_DIGEST_SIZE; +#elif !defined(NO_SHA256) + default:return WC_SHA256_DIGEST_SIZE; +#elif defined(WOLFSSL_SHA384) + default:return WC_SHA384_DIGEST_SIZE; +#elif defined(WOLFSSL_SHA512) + default:return WC_SHA512_DIGEST_SIZE; +#else + default:return 20;/* WC_SHA_DIGEST_SIZE */ +#endif } - ESP_LOGV(TAG, "leave esp_sha_digest_sit"); + ESP_LOGV(TAG, "leave esp_sha_digest_size"); } /* * wait until engines becomes idle @@ -100,9 +118,10 @@ void esp_wait_until_idle() */ int esp_sha_try_hw_lock(WC_ESP32SHA* ctx) { - ESP_LOGV(TAG, "enter esp_sha_hw_Init"); - int ret = 0; + + ESP_LOGV(TAG, "enter esp_sha_hw_lock"); + /* Init mutex */ #if defined(SINGLE_THREADED) if(ctx->mode == ESP32_SHA_INIT) { @@ -148,7 +167,7 @@ int esp_sha_try_hw_lock(WC_ESP32SHA* ctx) /* Enable SHA hardware */ periph_module_enable(PERIPH_SHA_MODULE); - ESP_LOGV(TAG, "leave esp_sha_hw_Init"); + ESP_LOGV(TAG, "leave esp_sha_hw_lock"); return ret; } /* @@ -194,10 +213,12 @@ void esp_process_block(WC_ESP32SHA* ctx, word32 address, { ESP_LOGV(TAG, "enter esp_process_block"); + int i; + /* check if there are any busy engine */ esp_wait_until_idle(); /* load message data into hw */ - for(int i=0;i<((len)/(sizeof(word32)));++i){ + for(i=0;i<((len)/(sizeof(word32)));++i){ DPORT_REG_WRITE(SHA_TEXT_BASE+(i*4),*(data+i)); } /* notify hw to start process */ @@ -210,10 +231,20 @@ void esp_process_block(WC_ESP32SHA* ctx, word32 address, */ void esp_digest_state(WC_ESP32SHA* ctx, byte* hash, enum SHA_TYPE sha_type) { - ESP_LOGV(TAG, "enter esp_digest_state"); /* registers */ - word32 SHA_LOAD_REG = SHA_1_LOAD_REG + sha_type * 0x10; - word32 SHA_BUSY_REG = SHA_1_BUSY_REG + sha_type * 0x10; + word32 SHA_LOAD_REG = SHA_1_LOAD_REG; + word32 SHA_BUSY_REG = SHA_1_BUSY_REG; + + ESP_LOGV(TAG, "enter esp_digest_state"); + + /* sanity check */ + if(sha_type == SHA_INVALID) { + ESP_LOGE(TAG, "unexpected error. sha_type is invalid."); + return; + } + + SHA_LOAD_REG += (sha_type << 4); + SHA_BUSY_REG += (sha_type << 4); if(ctx->isfirstblock == 1){ /* no hardware use yet. Nothing to do yet */ @@ -230,7 +261,7 @@ void esp_digest_state(WC_ESP32SHA* ctx, byte* hash, enum SHA_TYPE sha_type) while(DPORT_REG_READ(SHA_BUSY_REG) == 1){ } esp_dport_access_read_buffer((word32*)(hash), SHA_TEXT_BASE, - esp_sha_digest_size(sha_type)/4); + esp_sha_digest_size(sha_type)/sizeof(word32)); #if defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384) if(sha_type==SHA2_384||sha_type==SHA2_512) { @@ -255,11 +286,12 @@ void esp_digest_state(WC_ESP32SHA* ctx, byte* hash, enum SHA_TYPE sha_type) */ int esp_sha_process(struct wc_Sha* sha) { - ESP_LOGV(TAG, "enter esp_sha_process"); - int ret = 0; - word32 SHA_START_REG = SHA_1_START_REG + SHA1 * 0x10; + ESP_LOGV(TAG, "enter esp_sha_process"); + + word32 SHA_START_REG = SHA_1_START_REG; + esp_process_block(&sha->ctx, SHA_START_REG, sha->buffer, WC_SHA_BLOCK_SIZE); @@ -271,11 +303,12 @@ int esp_sha_process(struct wc_Sha* sha) */ int esp_sha_digest_process(struct wc_Sha* sha, byte blockproc) { - ESP_LOGV(TAG, "enter esp_sha_digest_process"); int ret = 0; + + ESP_LOGV(TAG, "enter esp_sha_digest_process"); if(blockproc) { - word32 SHA_START_REG = SHA_1_START_REG + SHA1 * 0x10; + word32 SHA_START_REG = SHA_1_START_REG; esp_process_block(&sha->ctx, SHA_START_REG, sha->buffer, WC_SHA_BLOCK_SIZE); @@ -296,12 +329,14 @@ int esp_sha_digest_process(struct wc_Sha* sha, byte blockproc) */ int esp_sha256_process(struct wc_Sha256* sha) { - ESP_LOGV(TAG, "enter esp_sha256_process"); int ret = 0; - word32 SHA_START_REG = SHA_1_START_REG + SHA2_256 * 0x10; + + ESP_LOGV(TAG, "enter esp_sha256_process"); + + word32 SHA_START_REG = SHA_1_START_REG + 16; esp_process_block(&sha->ctx, SHA_START_REG, sha->buffer, - WC_SHA_BLOCK_SIZE); + WC_SHA256_BLOCK_SIZE); ESP_LOGV(TAG, "leave esp_sha256_process"); @@ -312,14 +347,15 @@ int esp_sha256_process(struct wc_Sha256* sha) */ int esp_sha256_digest_process(struct wc_Sha256* sha, byte blockproc) { - ESP_LOGV(TAG, "enter esp_sha256_digest_process"); int ret = 0; + + ESP_LOGV(TAG, "enter esp_sha256_digest_process"); if(blockproc) { word32 SHA_START_REG = SHA_1_START_REG + SHA2_256 * 0x10; esp_process_block(&sha->ctx, SHA_START_REG, sha->buffer, - WC_SHA_BLOCK_SIZE); + WC_SHA256_BLOCK_SIZE); } esp_digest_state(&sha->ctx, (byte*)sha->digest, SHA2_256); @@ -335,12 +371,12 @@ int esp_sha256_digest_process(struct wc_Sha256* sha, byte blockproc) */ void esp_sha512_block(struct wc_Sha512* sha, const word32* data, byte isfinal) { - ESP_LOGV(TAG, "enter esp_sha512_block"); - enum SHA_TYPE sha_type = sha->ctx.sha_type; word32 SHA_START_REG = SHA_1_START_REG; - SHA_START_REG += sha_type * 0x10; + ESP_LOGV(TAG, "enter esp_sha512_block"); + + SHA_START_REG += (sha_type << 4); if(sha->ctx.mode == ESP32_SHA_SW){ ByteReverseWords64(sha->buffer, sha->buffer, @@ -369,9 +405,10 @@ void esp_sha512_block(struct wc_Sha512* sha, const word32* data, byte isfinal) */ int esp_sha512_process(struct wc_Sha512* sha) { - ESP_LOGV(TAG, "enter esp_sha512_process"); - word32 *data = (word32*)sha->buffer; + + ESP_LOGV(TAG, "enter esp_sha512_process"); + esp_sha512_block(sha, data, 0); ESP_LOGV(TAG, "leave esp_sha512_process"); @@ -397,3 +434,4 @@ int esp_sha512_digest_process(struct wc_Sha512* sha, byte blockproc) } #endif /* WOLFSSL_SHA512 || WOLFSSL_SHA384 */ #endif /* WOLFSSL_ESP32WROOM32_CRYPT */ +#endif /* !defined(NO_SHA) ||... */ diff --git a/wolfcrypt/src/port/Espressif/esp32_util.c b/wolfcrypt/src/port/Espressif/esp32_util.c index d24562e43..494abc29c 100644 --- a/wolfcrypt/src/port/Espressif/esp32_util.c +++ b/wolfcrypt/src/port/Espressif/esp32_util.c @@ -1,5 +1,5 @@ /** - * util.c + * esp32_util.c * Copyright (C) 2006-2018 wolfSSL Inc. * * This file is part of wolfSSL. @@ -25,13 +25,18 @@ defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)) #include +#include int esp_CryptHwMutexInit(wolfSSL_Mutex* mutex) { return wc_InitMutex(mutex); } -int esp_CryptHwMutexLock(wolfSSL_Mutex* mutex, TickType_t xBloxkTime) { - return wc_LockMutex_ex(mutex, xBloxkTime); +int esp_CryptHwMutexLock(wolfSSL_Mutex* mutex, TickType_t xBlockTime) { +#ifdef SINGLE_THREADED + return wc_LockMutex(mutex); +#else + return ((xSemaphoreTake( *mutex, xBlockTime ) == pdTRUE) ? 0 : BAD_MUTEX_E); +#endif } int esp_CryptHwMutexUnLock(wolfSSL_Mutex* mutex) { @@ -45,7 +50,7 @@ int esp_CryptHwMutexUnLock(wolfSSL_Mutex* mutex) { #include "esp_timer.h" #include "esp_log.h" -uint64_t startTime = 0; +static uint64_t startTime = 0; void wc_esp32TimerStart() diff --git a/wolfcrypt/src/wc_port.c b/wolfcrypt/src/wc_port.c index 2009881d4..75ffaf01b 100644 --- a/wolfcrypt/src/wc_port.c +++ b/wolfcrypt/src/wc_port.c @@ -614,13 +614,8 @@ int wolfSSL_CryptHwMutexUnLock(void) { (void)m; return 0; } -#if defined(WOLFSSL_ESP32WROOM32_CRYPT) - int wc_LockMutex_ex(wolfSSL_Mutex *m, TickType_t xBlockTime) - { - (void)m; - return 0; - } -#endif + + int wc_UnLockMutex(wolfSSL_Mutex *m) { (void)m; @@ -655,12 +650,7 @@ int wolfSSL_CryptHwMutexUnLock(void) { xSemaphoreTake( *m, portMAX_DELAY ); return 0; } -#if defined(WOLFSSL_ESP32WROOM32_CRYPT) - int wc_LockMutex_ex(wolfSSL_Mutex* m, TickType_t xBlockTime) - { - return ((xSemaphoreTake( *m, xBlockTime ) == pdTRUE) ? 0 : BAD_MUTEX_E); - } -#endif + int wc_UnLockMutex(wolfSSL_Mutex* m) { xSemaphoreGive( *m ); diff --git a/wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h b/wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h index bddc9d845..80ea15222 100644 --- a/wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h +++ b/wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h @@ -33,9 +33,6 @@ #define LOG_LOCAL_LEVEL ESP_LOG_ERROR #endif -#ifndef NO_SHA -#include "rom/sha.h" -#endif #include #include "soc/dport_reg.h" #include "soc/hwcrypto_reg.h" @@ -78,9 +75,12 @@ uint64_t wc_esp32elapsedTime(); #endif /* WOLFSSL_ESP32WROOM32_CRYPT_DEBUG */ -#if !defined(NO_SHA) && \ +#if (!defined(NO_SHA) || !defined(NO_SHA256) || defined(WOLFSSL_SHA384) || \ + defined(WOLFSSL_SHA512)) && \ !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH) +#include "rom/sha.h" + typedef enum { ESP32_SHA_INIT = 0, ESP32_SHA_HW = 1, diff --git a/wolfssl/wolfcrypt/wc_port.h b/wolfssl/wolfcrypt/wc_port.h index 10e1408d0..d05d0862b 100755 --- a/wolfssl/wolfcrypt/wc_port.h +++ b/wolfssl/wolfcrypt/wc_port.h @@ -214,9 +214,6 @@ /* Mutex functions */ WOLFSSL_API int wc_InitMutex(wolfSSL_Mutex*); -#if defined(WOLFSSL_ESP32WROOM32_CRYPT) -WOLFSSL_API int wc_LockMutex_ex(wolfSSL_Mutex* m, TickType_t xBlockTime); -#endif WOLFSSL_API wolfSSL_Mutex* wc_InitAndAllocMutex(void); WOLFSSL_API int wc_FreeMutex(wolfSSL_Mutex*); WOLFSSL_API int wc_LockMutex(wolfSSL_Mutex*);