From 0241f81cbc059d0b3b1541db19212142476c8c5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tero=20J=C3=A4=C3=A4sk=C3=B6?= Date: Fri, 15 Jan 2021 17:02:37 +0200 Subject: [PATCH 1/2] AES: masked config: increase total transfer speed by 5% by removing zeroing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On MBEDTLS_AES_128_BIT_MASKED config the overall DTLS transfer speed can be increased by 3..5% or so on a Cortex-M4 by removing 472 bytes worth of buffer zeroing done on each block {en|de}cryption. The buffer zeroings were done before overwriting them on in masking code. Signed-off-by: Tero Jääskö --- library/aes.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/aes.c b/library/aes.c index 50f970227..061b6fd59 100644 --- a/library/aes.c +++ b/library/aes.c @@ -1421,9 +1421,9 @@ int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx, uint8_t round_ctrl_table[( 14 + AES_SCA_CM_ROUNDS + 2 )]; #if defined MBEDTLS_AES_128_BIT_MASKED - uint32_t rk_masked[MBEDTLS_AES_128_EXPANDED_KEY_SIZE_IN_WORDS] = {0}; - uint8_t sbox_masked[256] = {0}; - uint32_t mask[10] = {0}; + uint32_t rk_masked[MBEDTLS_AES_128_EXPANDED_KEY_SIZE_IN_WORDS]; + uint8_t sbox_masked[256]; + uint32_t mask[10]; #endif #if defined(MBEDTLS_VALIDATE_AES_KEYS_INTEGRITY) From ada61f05b370f588c06371c4977be0267a800ce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tero=20J=C3=A4=C3=A4sk=C3=B6?= Date: Mon, 18 Jan 2021 17:53:48 +0200 Subject: [PATCH 2/2] AES: masked config: remove 176 bytes of zeroing on each AES finalize round MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On MBEDTLS_AES_128_BIT_MASKED+FI_COUNTERMEASURES config the rk_mask array was unconditionally zeroed on each dummy finalization round even though it will be zeroed after the loop too. Remove the repeated zeroing to increase overall transfer speed on DTLS session by 17..20% on a Cortex-M4. Signed-off-by: Tero Jääskö --- library/aes.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/library/aes.c b/library/aes.c index 061b6fd59..72cfc104d 100644 --- a/library/aes.c +++ b/library/aes.c @@ -1560,8 +1560,6 @@ int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx, { flow_control++; } - //Cleanup the masked key - mbedtls_platform_memset( rk_masked, 0, sizeof(rk_masked) ); #else aes_fround_final( aes_data_ptr->rk_ptr, &aes_data_ptr->xy_values[0],