From cce06e8c501c3cec7d9e8318d36a504b92d6369a Mon Sep 17 00:00:00 2001 From: Hideki Miyazaki Date: Tue, 26 Feb 2019 12:10:29 +0900 Subject: [PATCH] Addressed to review comments --- wolfcrypt/src/port/Espressif/esp32_mp.c | 23 ++++++++++++++++++----- wolfcrypt/src/tfm.c | 2 +- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/wolfcrypt/src/port/Espressif/esp32_mp.c b/wolfcrypt/src/port/Espressif/esp32_mp.c index 3b527e57f..0b231e2d1 100644 --- a/wolfcrypt/src/port/Espressif/esp32_mp.c +++ b/wolfcrypt/src/port/Espressif/esp32_mp.c @@ -57,9 +57,16 @@ static int espmp_CryptHwMutexInit = 0; /* * check if the hw is ready before accessing it */ -static void esp_mp_hw_wait_clean() +static int esp_mp_hw_wait_clean() { - while(DPORT_REG_READ(RSA_CLEAN_REG) != 1){} + int timeout = 0; + while(++timeout < ESP_RSA_TIMEOUT && DPORT_REG_READ(RSA_CLEAN_REG) != 1){} + + if(timeout >= ESP_RSA_TIMEOUT) { + ESP_LOGE(TAG, "waiting hw ready is time-outed."); + return MP_NG; + } + return MP_OKAY; } /* * lock hw engine. @@ -263,7 +270,9 @@ int esp_mp_mul(fp_int* X, fp_int* Y, fp_int* Z) if((ret = esp_mp_hw_lock()) != MP_OKAY) return ret; - esp_mp_hw_wait_clean(); + if((ret = esp_mp_hw_wait_clean()) != MP_OKAY){ + return ret; + } /* step.1 (2*N/512) => N/256. 512 bits => 16 words */ DPORT_REG_WRITE(RSA_MULT_MODE_REG, (hwWords_sz >> 3) - 1 + 8); @@ -364,7 +373,9 @@ int esp_mp_mulmod(fp_int* X, fp_int* Y, fp_int* M, fp_int* Z) * 13. Release the hw engine */ - esp_mp_hw_wait_clean(); + if((ret = esp_mp_hw_wait_clean()) != MP_OKAY){ + return ret; + } /* step.1 512 bits => 16 words */ DPORT_REG_WRITE(RSA_MULT_MODE_REG, (hwWords_sz >> 4) - 1); @@ -471,7 +482,9 @@ int esp_mp_exptmod(fp_int* X, fp_int* Y, word32 Ys, fp_int* M, fp_int* Z) * 6. Read the result Z(=Y) from Z_MEM * 7. Write 1 to INTERRUPT_REG to clear the interrupt. */ - esp_mp_hw_wait_clean(); + if((ret = esp_mp_hw_wait_clean()) != MP_OKAY){ + return ret; + } /* step.1 */ DPORT_REG_WRITE(RSA_MODEXP_MODE_REG, (hwWords_sz >> 4) - 1); diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c index 25dd05e36..c34e5fb8b 100644 --- a/wolfcrypt/src/tfm.c +++ b/wolfcrypt/src/tfm.c @@ -3026,7 +3026,7 @@ int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d) int B = fp_count_bits (b); if( A >= ESP_RSA_MULM_BITS && B >= ESP_RSA_MULM_BITS) - return esp_mp_mulmod(a, b, c, d); + return esp_mp_mulmod(a, b, c, d); else #endif return fp_mulmod(a, b, c, d);