Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions wolfcrypt/src/port/Espressif/esp32_mp.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
* See ESP32 Technical Reference Manual - RSA Accelerator Chapter
*
* esp_mp_exptmod() Large Number Modular Exponentiation Z = X^Y mod M
* esp_mp_mulmod() Large Number Modular Multiplication Z = X × Y mod M
* esp_mp_mul() Large Number Multiplication Z = X × Y
* esp_mp_mulmod() Large Number Modular Multiplication Z = X * Y mod M
* esp_mp_mul() Large Number Multiplication Z = X * Y
*
* The ESP32 RSA Accelerator supports operand lengths of:
* N {512, 1024, 1536, 2048, 2560, 3072, 3584, 4096} bits. The bit length
* N in {512, 1024, 1536, 2048, 2560, 3072, 3584, 4096} bits. The bit length
* of arguments Z, X, Y , M, and r can be any one from the N set, but all
* numbers in a calculation must be of the same length.
*
* The bit length of M is always 32.
* The bit length of M' is always 32.
*
* Also, beware: "we have uint32_t == unsigned long for both Xtensa and RISC-V"
* see https://github.com/espressif/esp-idf/issues/9511#issuecomment-1207342464
Expand Down Expand Up @@ -1285,8 +1285,8 @@ int esp_mp_mul(MATH_INT_T* X, MATH_INT_T* Y, MATH_INT_T* Z)
Zs = Xs + Ys;

/* RSA Accelerator only supports Large Number Multiplication
* with operand length N = 32 × x,
* where x {1, 2, 3, . . . , 64} */
* with operand length N = 32 * x,
* where x in {1, 2, 3, . . . , 64} */
if (Xs > 64 || Ys > 64) {
return MP_HW_FALLBACK; /* TODO add count metric on size fallback */
}
Expand Down Expand Up @@ -1334,7 +1334,7 @@ int esp_mp_mul(MATH_INT_T* X, MATH_INT_T* Y, MATH_INT_T* Z)

/* Y (left-extend)
* Accelerator supports large-number multiplication with only
* four operand lengths of N {512, 1024, 1536, 2048} */
* four operand lengths of N in {512, 1024, 1536, 2048} */
left_pad_offset = maxWords_sz << 2;
if (left_pad_offset <= 512 >> 3) {
left_pad_offset = 512 >> 3; /* 64 bytes (16 words) */
Expand Down Expand Up @@ -1583,10 +1583,10 @@ int esp_mp_mul(MATH_INT_T* X, MATH_INT_T* Y, MATH_INT_T* Z)
* 0 => no interrupt; 1 => interrupt on completion. */
DPORT_REG_WRITE(RSA_INT_ENA_REG, 0);
/* 2. Write number of words required for result. */
/* see 21.3.3 Write (/N16 1) to the RSA_MODE_REG register */
/* see 21.3.3 Write (/N16 - 1) to the RSA_MODE_REG register */
DPORT_REG_WRITE(RSA_MODE_REG, (hwWords_sz * 2 - 1));

/* 3. Write Xi and Yi for {0, 1, . . . , n 1} to memory blocks
/* 3. Write Xi and Yi for {0, 1, . . . , n - 1} to memory blocks
* RSA_X_MEM and RSA_Z_MEM
* Maximum is 64 words (64*8*4 = 2048 bits) */
esp_mpint_to_memblock(RSA_X_MEM,
Expand Down Expand Up @@ -1796,7 +1796,7 @@ int esp_mp_mul(MATH_INT_T* X, MATH_INT_T* Y, MATH_INT_T* Z)
*
* See 24.3.3 of the ESP32 Technical Reference Manual
*
* Z = X × Y mod M */
* Z = X * Y mod M */
int esp_mp_mulmod(MATH_INT_T* X, MATH_INT_T* Y, MATH_INT_T* M, MATH_INT_T* Z)
{
struct esp_mp_helper mph[1]; /* we'll save some values in this mp helper */
Expand Down Expand Up @@ -1839,7 +1839,7 @@ int esp_mp_mulmod(MATH_INT_T* X, MATH_INT_T* Y, MATH_INT_T* M, MATH_INT_T* Z)
/* do we have an even moduli? */
if ((M->dp[0] & 1) == 0) {
#ifndef NO_ESP_MP_MUL_EVEN_ALT_CALC
/* Z = X × Y mod M in mixed HW & SW*/
/* Z = X * Y mod M in mixed HW & SW*/
ret = esp_mp_mul(X, Y, tmpZ); /* HW X * Y */
if (ret == MP_OKAY) {
/* z = tmpZ mod M, 0 <= Z < M */
Expand Down Expand Up @@ -1973,13 +1973,13 @@ int esp_mp_mulmod(MATH_INT_T* X, MATH_INT_T* Y, MATH_INT_T* M, MATH_INT_T* Z)
* or until the RSA_INTR interrupt is generated.
* (Or until the INTER interrupt is generated.)
* 6. Write 1 to RSA_INTERRUPT_REG to clear the interrupt.
* 7. Write Yi (i [0, n) N) to RSA_X_MEM
* 7. Write Yi (i in [0, n) intersect N) to RSA_X_MEM
* Users need to write to the memory block only according to the length
* of the number. Data beyond this length is ignored.
* 8. Write 1 to RSA_MULT_START_REG
* 9. Wait for the second operation to be completed.
* Poll INTERRUPT_REG until it reads 1.
* 10. Read the Zi (i [0, n) N) from RSA_Z_MEM
* 10. Read the Zi (i in [0, n) intersect N) from RSA_Z_MEM
* 11. Write 1 to RSA_INTERUPT_REG to clear the interrupt.
*
* post: Release the HW engine
Expand Down Expand Up @@ -2500,15 +2500,15 @@ int esp_mp_mulmod(MATH_INT_T* X, MATH_INT_T* Y, MATH_INT_T* M, MATH_INT_T* Z)
* ESP32S3, Section 20.3.1, https://www.espressif.com/sites/default/files/documentation/esp32-s3_technical_reference_manual_en.pdf
*
* The operation is based on Montgomery multiplication. Aside from the
* arguments X, Y , and M, two additional ones are needed r and M
* arguments X, Y , and M, two additional ones are needed -r and M'
.* These arguments are calculated in advance by software.
.*
.* The RSA Accelerator supports operand lengths of N {512, 1024, 1536, 2048,
.* 2560, 3072, 3584, 4096} bits on the ESP32 and N [32, 4096] bits
.* The RSA Accelerator supports operand lengths of N in {512, 1024, 1536, 2048,
.* 2560, 3072, 3584, 4096} bits on the ESP32 and N in [32, 4096] bits
* on the ESP32s3.
.* The bit length of arguments Z, X, Y , M, and r can be any one from
* the N set, but all numbers in a calculation must be of the same length.
.* The bit length of M is always 32.
.* The bit length of M' is always 32.
.*
* Z = (X ^ Y) mod M : Espressif generic notation
* Y = (G ^ X) mod P : wolfSSL DH reference notation */
Expand Down
2 changes: 1 addition & 1 deletion wolfcrypt/src/port/Renesas/renesas_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static int gdevId = 7890; /* initial dev Id for Crypt Callback */
defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
FSPSM_ST *gCbCtx[MAX_FSPSM_CBINDEX];
#elif defined(WOLFSSL_RENESAS_TSIP_TLS) || \
defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
#define FSPSM_ST TsipUserCtx;
#define MAX_FSPSM_CBINDEX 5
TsipUserCtx *gCbCtx[MAX_FSPSM_CBINDEX];
Expand Down
40 changes: 20 additions & 20 deletions wolfcrypt/src/port/Renesas/renesas_fspsm_aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ typedef fsp_err_t (*aesGcmDecFinalFn)

#if defined(WOLFSSL_RENESAS_RSIP)
/* wrapper for Gcm encrypt init */
static fsp_err_t _R_RSIP_AES_GCM_EncryptInit(FSPSM_AESGCM_HANDLE* h,
static fsp_err_t _R_RSIP_AES_GCM_EncryptInit(FSPSM_AESGCM_HANDLE* h,
FSPSM_AES_PWKEY k, uint8_t* iv,
uint32_t iv_l)
{
Expand All @@ -81,8 +81,8 @@ static fsp_err_t _R_RSIP_AES_GCM_EncryptInit(FSPSM_AESGCM_HANDLE* h,
(uint8_t* const)iv, iv_l);
}
/* wrapper for Gcm encrypt update */
static fsp_err_t _R_RSIP_AES_GCM_EncryptUpdate(FSPSM_AESGCM_HANDLE* h,
uint8_t* p_plain, uint8_t* p_cipher, uint32_t plain_length,
static fsp_err_t _R_RSIP_AES_GCM_EncryptUpdate(FSPSM_AESGCM_HANDLE* h,
uint8_t* p_plain, uint8_t* p_cipher, uint32_t plain_length,
uint8_t* p_add, uint32_t add_len)
{
(void) h;
Expand All @@ -93,8 +93,8 @@ static fsp_err_t _R_RSIP_AES_GCM_EncryptUpdate(FSPSM_AESGCM_HANDLE* h,
(uint32_t const) add_len);
}
/* wrapper for Gcm encrypt final */
static fsp_err_t _R_RSIP_AES_GCM_EncryptFinal(FSPSM_AESGCM_HANDLE* h,
uint8_t* p_cipher, uint32_t* c_len,
static fsp_err_t _R_RSIP_AES_GCM_EncryptFinal(FSPSM_AESGCM_HANDLE* h,
uint8_t* p_cipher, uint32_t* c_len,
uint8_t* p_atag)
{
(void) h;
Expand All @@ -103,16 +103,16 @@ static fsp_err_t _R_RSIP_AES_GCM_EncryptFinal(FSPSM_AESGCM_HANDLE* h,
(uint8_t* const) p_atag);
}
/* wrapper for Gcm decrypt init */
static fsp_err_t _R_RSIP_AES_GCM_DecryptInit(FSPSM_AESGCM_HANDLE* h,
static fsp_err_t _R_RSIP_AES_GCM_DecryptInit(FSPSM_AESGCM_HANDLE* h,
FSPSM_AES_PWKEY k, uint8_t* iv, uint32_t iv_l)
{
(void) h;
return R_RSIP_AES_GCM_DecryptInit(&gFSPSM_ctrl, (FSPSM_AES_PWKEY const)k,
(uint8_t* const)iv, iv_l);
}
/* wrapper for Gcm decrypt update */
static fsp_err_t _R_RSIP_AES_GCM_DecryptUpdate(FSPSM_AESGCM_HANDLE* h,
uint8_t* p_cipher, uint8_t* p_plain, uint32_t c_length,
static fsp_err_t _R_RSIP_AES_GCM_DecryptUpdate(FSPSM_AESGCM_HANDLE* h,
uint8_t* p_cipher, uint8_t* p_plain, uint32_t c_length,
uint8_t* p_add, uint32_t add_len)
{
(void) h;
Expand All @@ -123,8 +123,8 @@ static fsp_err_t _R_RSIP_AES_GCM_DecryptUpdate(FSPSM_AESGCM_HANDLE* h,
(uint32_t const) add_len);
}
/* wrapper for Gcm decrypt final */
static fsp_err_t _R_RSIP_AES_GCM_DecryptFinal(FSPSM_AESGCM_HANDLE* h,
uint8_t* p_plain, uint32_t* plain_len,
static fsp_err_t _R_RSIP_AES_GCM_DecryptFinal(FSPSM_AESGCM_HANDLE* h,
uint8_t* p_plain, uint32_t* plain_len,
uint8_t* p_atag, uint32_t atag_len)
{
(void) h;
Expand Down Expand Up @@ -241,7 +241,7 @@ WOLFSSL_LOCAL int wc_fspsm_AesGcmEncrypt(struct Aes* aes, byte* out,
FSPSM_AES_PWKEY key_client_aes = NULL;
FSPSM_AES_PWKEY key_server_aes = NULL;
(void) key_server_aes;

/* sanity check */
if (aes == NULL || authTagSz > AES_BLOCK_SIZE || ivSz == 0 || ctx == NULL) {
return BAD_FUNC_ARG;
Expand Down Expand Up @@ -282,7 +282,7 @@ WOLFSSL_LOCAL int wc_fspsm_AesGcmEncrypt(struct Aes* aes, byte* out,
aTagBuf = XMALLOC(SCE_AES_GCM_AUTH_TAG_SIZE, aes->heap,
DYNAMIC_TYPE_AES);

if ((sz > 0 && plainBuf == NULL) ||
if ((sz > 0 && plainBuf == NULL) ||
((sz + delta) > 0 && cipherBuf == NULL) || aTagBuf == NULL) {
WOLFSSL_MSG("wc_fspsm_AesGcmEncrypt: buffer allocation failed");
ret = -1;
Expand All @@ -293,7 +293,7 @@ WOLFSSL_LOCAL int wc_fspsm_AesGcmEncrypt(struct Aes* aes, byte* out,
XMEMSET((void*)cipherBuf, 0, sz + delta);
XMEMSET((void*)authTag, 0, authTagSz);
}

#if defined(WOLFSSL_RENESAS_FSPSM_TLS)
if (ret == 0 &&
info->keyflgs_tls.bits.session_key_set == 1) {
Expand All @@ -310,7 +310,7 @@ WOLFSSL_LOCAL int wc_fspsm_AesGcmEncrypt(struct Aes* aes, byte* out,
XFREE(aTagBuf, aes->heap, DYNAMIC_TYPE_AES);
return MEMORY_E;
}

ret = FSPSM_SESSIONKEY_GEN_FUNC(
info->cipher,
(uint32_t*)info->masterSecret,
Expand Down Expand Up @@ -514,7 +514,7 @@ WOLFSSL_LOCAL int wc_fspsm_AesGcmDecrypt(struct Aes* aes, byte* out,
XFREE(aTagBuf, aes->heap, DYNAMIC_TYPE_AES);
return MEMORY_E;
}

ret = FSPSM_SESSIONKEY_GEN_FUNC(
info->cipher,
(uint32_t*)info->masterSecret,
Expand Down Expand Up @@ -547,7 +547,7 @@ WOLFSSL_LOCAL int wc_fspsm_AesGcmDecrypt(struct Aes* aes, byte* out,
ret = -1;
}
}

if (ret == 0) {
/* since key_index has iv and ivSz in it, no need to pass them init
* func. Pass NULL and 0 as 3rd and 4th parameter respectively.
Expand Down Expand Up @@ -769,7 +769,7 @@ WOLFSSL_LOCAL void wc_fspsm_Aesfree(Aes* aes)
}
#else
if (aes->ctx.wrapped_key) {
/* aes ctx just points user created wrapped key
/* aes ctx just points user created wrapped key
* in the case of CryptOnly Mode
* therefore, it just sets pointing to NULL.
* user key should be freed by owner(user)
Expand All @@ -785,8 +785,8 @@ int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
{
(void) userKey;
(void) dir;
if (aes == NULL || userKey == NULL ||

if (aes == NULL || userKey == NULL ||
!((keylen == 16) || (keylen == 32))) {
return BAD_FUNC_ARG;
}
Expand All @@ -806,7 +806,7 @@ int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
aes->ctx.wrapped_key = (FSPSM_AES_PWKEY)userKey;
aes->keylen = (int)keylen;
aes->ctx.keySize = keylen;

return wc_AesSetIV(aes, iv);
}
#endif
Expand Down
Loading