From 30612ad3ed29604bb8188fc0e9d75d3419bae125 Mon Sep 17 00:00:00 2001 From: Hideki Miyazaki Date: Fri, 28 Dec 2018 11:48:39 +0900 Subject: [PATCH] addressed review comments --- wolfcrypt/src/port/Espressif/esp32_sha.c | 87 +++++++++---------- wolfcrypt/src/port/Espressif/esp32_util.c | 2 +- wolfcrypt/src/sha.c | 2 +- wolfcrypt/src/sha256.c | 2 +- wolfcrypt/src/sha512.c | 4 +- .../wolfcrypt/port/Espressif/esp32-crypt.h | 6 +- wolfssl/wolfcrypt/sha512.h | 8 +- 7 files changed, 53 insertions(+), 58 deletions(-) diff --git a/wolfcrypt/src/port/Espressif/esp32_sha.c b/wolfcrypt/src/port/Espressif/esp32_sha.c index 2f35eea1d..ab1dc0259 100644 --- a/wolfcrypt/src/port/Espressif/esp32_sha.c +++ b/wolfcrypt/src/port/Espressif/esp32_sha.c @@ -50,7 +50,12 @@ #endif static const char* TAG = "wolf_hw_sha"; +/* continue register offset */ +#define CONTINUE_REG_OFFSET (0x04) /* start_reg + 0x04 */ +#ifdef NO_SHA + #define WC_SHA_DIGEST_SIZE 20 +#endif /* mutex */ #if defined(SINGLE_THREADED) static int InUse = 0; @@ -88,24 +93,16 @@ 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 + default: + ESP_LOGE(TAG, "Bad sha type"); + return WC_SHA_DIGEST_SIZE; } ESP_LOGV(TAG, "leave esp_sha_digest_size"); } /* * wait until engines becomes idle */ -void esp_wait_until_idle() +static void esp_wait_until_idle() { while((DPORT_REG_READ(SHA_1_BUSY_REG) !=0) || (DPORT_REG_READ(SHA_256_BUSY_REG)!=0) || @@ -173,9 +170,9 @@ int esp_sha_try_hw_lock(WC_ESP32SHA* ctx) /* * release hw engine */ -void esp_sha_hw_Release( void ) +void esp_sha_hw_unlock( void ) { - ESP_LOGV(TAG, "enter esp_sha_hw_Release"); + ESP_LOGV(TAG, "enter esp_sha_hw_unlock"); /* Disable AES hardware */ periph_module_disable(PERIPH_SHA_MODULE); @@ -185,12 +182,12 @@ void esp_sha_hw_Release( void ) /* unlock hw engine for next use */ esp_CryptHwMutexUnLock(&sha_mutex); #endif - ESP_LOGV(TAG, "leave esp_sha_hw_Release"); + ESP_LOGV(TAG, "leave esp_sha_hw_unlock"); } /* * start sha process by using hw engine */ -void esp_sha_start_process(WC_ESP32SHA* sha, word32 address) +static void esp_sha_start_process(WC_ESP32SHA* sha, word32 address) { ESP_LOGV(TAG, "enter esp_sha_start_process"); @@ -200,7 +197,7 @@ void esp_sha_start_process(WC_ESP32SHA* sha, word32 address) sha->isfirstblock = 0; } else { /* CONTINU_REG */ - DPORT_REG_WRITE(address + 0x04, 1); + DPORT_REG_WRITE(address + CONTINUE_REG_OFFSET , 1); } ESP_LOGV(TAG, "leave esp_sha_start_process"); @@ -208,18 +205,18 @@ void esp_sha_start_process(WC_ESP32SHA* sha, word32 address) /* * process message block */ -void esp_process_block(WC_ESP32SHA* ctx, word32 address, +static void esp_process_block(WC_ESP32SHA* ctx, word32 address, const word32* data, word32 len) { - ESP_LOGV(TAG, "enter esp_process_block"); - int i; + + ESP_LOGV(TAG, "enter esp_process_block"); /* check if there are any busy engine */ esp_wait_until_idle(); /* load message data into hw */ for(i=0;i<((len)/(sizeof(word32)));++i){ - DPORT_REG_WRITE(SHA_TEXT_BASE+(i*4),*(data+i)); + DPORT_REG_WRITE(SHA_TEXT_BASE+(i*sizeof(word32)),*(data+i)); } /* notify hw to start process */ esp_sha_start_process(ctx, address); @@ -229,7 +226,7 @@ void esp_process_block(WC_ESP32SHA* ctx, word32 address, /* * retreive sha digest from memory */ -void esp_digest_state(WC_ESP32SHA* ctx, byte* hash, enum SHA_TYPE sha_type) +static void esp_digest_state(WC_ESP32SHA* ctx, byte* hash, enum SHA_TYPE sha_type) { /* registers */ word32 SHA_LOAD_REG = SHA_1_LOAD_REG; @@ -251,31 +248,29 @@ void esp_digest_state(WC_ESP32SHA* ctx, byte* hash, enum SHA_TYPE sha_type) return ; } - { - /* wait until idle */ - esp_wait_until_idle(); + /* wait until idle */ + esp_wait_until_idle(); - /* LOAD final digest */ - DPORT_REG_WRITE(SHA_LOAD_REG, 1); - /* wait until done */ - while(DPORT_REG_READ(SHA_BUSY_REG) == 1){ } + /* LOAD final digest */ + DPORT_REG_WRITE(SHA_LOAD_REG, 1); + /* wait until done */ + while(DPORT_REG_READ(SHA_BUSY_REG) == 1){ } - esp_dport_access_read_buffer((word32*)(hash), SHA_TEXT_BASE, + esp_dport_access_read_buffer((word32*)(hash), SHA_TEXT_BASE, esp_sha_digest_size(sha_type)/sizeof(word32)); #if defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384) - if(sha_type==SHA2_384||sha_type==SHA2_512) { - word32 i; - word32* pwrd1 = (word32*)(hash); - /* swap value */ - for(i = 0; i ctx, SHA_START_REG, sha->buffer, WC_SHA256_BLOCK_SIZE); @@ -352,7 +349,7 @@ int esp_sha256_digest_process(struct wc_Sha256* sha, byte blockproc) ESP_LOGV(TAG, "enter esp_sha256_digest_process"); if(blockproc) { - word32 SHA_START_REG = SHA_1_START_REG + SHA2_256 * 0x10; + word32 SHA_START_REG = SHA_1_START_REG + (SHA2_256 << 4); esp_process_block(&sha->ctx, SHA_START_REG, sha->buffer, WC_SHA256_BLOCK_SIZE); @@ -363,7 +360,7 @@ int esp_sha256_digest_process(struct wc_Sha256* sha, byte blockproc) ESP_LOGV(TAG, "leave esp_sha256_digest_process"); return ret; } -#endif /* NO_WOLFSSL_SHA256 */ +#endif /* NO_SHA256 */ #if defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384) /* @@ -375,7 +372,7 @@ void esp_sha512_block(struct wc_Sha512* sha, const word32* data, byte isfinal) word32 SHA_START_REG = SHA_1_START_REG; ESP_LOGV(TAG, "enter esp_sha512_block"); - + /* start register offset */ SHA_START_REG += (sha_type << 4); if(sha->ctx.mode == ESP32_SHA_SW){ diff --git a/wolfcrypt/src/port/Espressif/esp32_util.c b/wolfcrypt/src/port/Espressif/esp32_util.c index e8574718a..fcbaf6581 100644 --- a/wolfcrypt/src/port/Espressif/esp32_util.c +++ b/wolfcrypt/src/port/Espressif/esp32_util.c @@ -21,7 +21,7 @@ #include #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \ - (!defined(NO_AES) || !defined(NO_SHA) || !defined(NO_WOLFSSL_SHA256) ||\ + (!defined(NO_AES) || !defined(NO_SHA) || !defined(NO_SHA256) ||\ defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)) #include diff --git a/wolfcrypt/src/sha.c b/wolfcrypt/src/sha.c index 887a4f8dd..49a3737f1 100644 --- a/wolfcrypt/src/sha.c +++ b/wolfcrypt/src/sha.c @@ -266,7 +266,7 @@ sha->ctx.sha_type = SHA1; if(sha->ctx.mode == ESP32_SHA_HW){ /* release hw engine */ - esp_sha_hw_Release(); + esp_sha_hw_unlock(); } /* always set mode as INIT * whether using HW or SW is detemined at first call of update() diff --git a/wolfcrypt/src/sha256.c b/wolfcrypt/src/sha256.c index 13e0b4443..70922f90d 100644 --- a/wolfcrypt/src/sha256.c +++ b/wolfcrypt/src/sha256.c @@ -483,7 +483,7 @@ static int InitSha256(wc_Sha256* sha256) sha256->ctx.sha_type = SHA2_256; if(sha256->ctx.mode == ESP32_SHA_HW) { /* release hw */ - esp_sha_hw_Release(); + esp_sha_hw_unlock(); } /* always set mode as INIT * whether using HW or SW is detemined at first call of update() diff --git a/wolfcrypt/src/sha512.c b/wolfcrypt/src/sha512.c index f0d5cb04a..5fa3b9048 100644 --- a/wolfcrypt/src/sha512.c +++ b/wolfcrypt/src/sha512.c @@ -217,7 +217,7 @@ static int InitSha512(wc_Sha512* sha512) sha512->ctx.isfirstblock = 1; if(sha512->ctx.mode == ESP32_SHA_HW) { /* release hw */ - esp_sha_hw_Release(); + esp_sha_hw_unlock(); } /* always set mode as INIT * whether using HW or SW is detemined at first call of update() @@ -2717,7 +2717,7 @@ static int InitSha384(wc_Sha384* sha384) sha384->ctx.isfirstblock = 1; if(sha384->ctx.mode == ESP32_SHA_HW) { /* release hw */ - esp_sha_hw_Release(); + esp_sha_hw_unlock(); } /* always set mode as INIT * whether using HW or SW is detemined at first call of update() diff --git a/wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h b/wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h index 6f8b01901..55db0ddbd 100644 --- a/wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h +++ b/wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h @@ -38,7 +38,6 @@ #include "soc/hwcrypto_reg.h" #include "soc/cpu.h" #include "driver/periph_ctrl.h" -#include #include #ifdef __cplusplus @@ -96,14 +95,13 @@ typedef struct { } WC_ESP32SHA; int esp_sha_try_hw_lock(WC_ESP32SHA* ctx); -void esp_sha_hw_Release( void ); +void esp_sha_hw_unlock( void ); struct wc_Sha; int esp_sha_digest_process(struct wc_Sha* sha, byte bockprocess); int esp_sha_process(struct wc_Sha* sha); -void esp_sha_digest_state(struct wc_Sha* sha); -#ifndef NO_WOLFSSL_SHA256 +#ifndef NO_SHA256 struct wc_Sha256; int esp_sha256_digest_process(struct wc_Sha256* sha, byte bockprocess); int esp_sha256_process(struct wc_Sha256* sha); diff --git a/wolfssl/wolfcrypt/sha512.h b/wolfssl/wolfcrypt/sha512.h index cbe18d37c..c917437a6 100644 --- a/wolfssl/wolfcrypt/sha512.h +++ b/wolfssl/wolfcrypt/sha512.h @@ -114,10 +114,6 @@ enum { #else /* wc_Sha512 digest */ typedef struct wc_Sha512 { -#if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \ - !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH) - WC_ESP32SHA ctx; -#endif word64 digest[WC_SHA512_DIGEST_SIZE / sizeof(word64)]; word64 buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64)]; word32 buffLen; /* in bytes */ @@ -133,6 +129,10 @@ typedef struct wc_Sha512 { #ifdef WOLFSSL_SMALL_STACK_CACHE word64* W; #endif +#if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \ + !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH) + WC_ESP32SHA ctx; +#endif } wc_Sha512; #endif