From 7076675fec1d342b3bebc1e412d7afe46ca97e2b Mon Sep 17 00:00:00 2001 From: ccli8 Date: Tue, 26 Sep 2017 17:32:52 +0800 Subject: [PATCH] [NUC472/M487] Optimize AES alter. code --- .../TARGET_NUVOTON/TARGET_M480/aes/aes_alt.c | 33 +++++++++++-------- .../TARGET_NUC472/aes/aes_alt.c | 33 +++++++++++-------- 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/aes/aes_alt.c b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/aes/aes_alt.c index 5e54e767ad..60e70b1685 100644 --- a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/aes/aes_alt.c +++ b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/aes/aes_alt.c @@ -255,7 +255,6 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx, const unsigned char *input, unsigned char *output ) { - unsigned char temp[16]; int length = len; int blockChainLen; @@ -263,31 +262,39 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx, return( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH ); + ctx->opMode = AES_MODE_CBC; + /* Fetch IV byte data in big-endian */ + ctx->iv[0] = nu_get32_be(iv); + ctx->iv[1] = nu_get32_be(iv + 4); + ctx->iv[2] = nu_get32_be(iv + 8); + ctx->iv[3] = nu_get32_be(iv + 12); + + if( mode == MBEDTLS_AES_ENCRYPT ) { + ctx->encDec = 1; + } + else { + ctx->encDec = 0; + } + while( length > 0 ) { blockChainLen = (length > MAX_DMA_CHAIN_SIZE) ? MAX_DMA_CHAIN_SIZE : length; - - ctx->opMode = AES_MODE_CBC; - /* Fetch IV byte data in big-endian */ - ctx->iv[0] = nu_get32_be(iv); - ctx->iv[1] = nu_get32_be(iv + 4); - ctx->iv[2] = nu_get32_be(iv + 8); - ctx->iv[3] = nu_get32_be(iv + 12); if( mode == MBEDTLS_AES_ENCRYPT ) { - ctx->encDec = 1; __nvt_aes_crypt(ctx, input, output, blockChainLen); - memcpy( iv, output+blockChainLen-16, 16 ); } else { - memcpy( temp, input+blockChainLen-16, 16 ); - ctx->encDec = 0; __nvt_aes_crypt(ctx, input, output, blockChainLen); - memcpy( iv, temp, 16 ); } length -= blockChainLen; input += blockChainLen; output += blockChainLen; } + /* Save IV for next block cipher */ + nu_set32_be(iv, ctx->iv[0]); + nu_set32_be(iv + 4, ctx->iv[1]); + nu_set32_be(iv + 8, ctx->iv[2]); + nu_set32_be(iv + 12, ctx->iv[3]); + return( 0 ); } #endif /* MBEDTLS_CIPHER_MODE_CBC */ diff --git a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/aes/aes_alt.c b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/aes/aes_alt.c index b2c77afa8a..3acf43d1fb 100644 --- a/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/aes/aes_alt.c +++ b/features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/aes/aes_alt.c @@ -255,7 +255,6 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx, const unsigned char *input, unsigned char *output ) { - unsigned char temp[16]; int length = len; int blockChainLen; @@ -263,31 +262,39 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx, return( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH ); + ctx->opMode = AES_MODE_CBC; + /* Fetch IV byte data in big-endian */ + ctx->iv[0] = nu_get32_be(iv); + ctx->iv[1] = nu_get32_be(iv + 4); + ctx->iv[2] = nu_get32_be(iv + 8); + ctx->iv[3] = nu_get32_be(iv + 12); + + if( mode == MBEDTLS_AES_ENCRYPT ) { + ctx->encDec = 1; + } + else { + ctx->encDec = 0; + } + while( length > 0 ) { blockChainLen = (length > MAX_DMA_CHAIN_SIZE) ? MAX_DMA_CHAIN_SIZE : length; - - ctx->opMode = AES_MODE_CBC; - /* Fetch IV byte data in big-endian */ - ctx->iv[0] = nu_get32_be(iv); - ctx->iv[1] = nu_get32_be(iv + 4); - ctx->iv[2] = nu_get32_be(iv + 8); - ctx->iv[3] = nu_get32_be(iv + 12); if( mode == MBEDTLS_AES_ENCRYPT ) { - ctx->encDec = 1; __nvt_aes_crypt(ctx, input, output, blockChainLen); - memcpy( iv, output+blockChainLen-16, 16 ); } else { - memcpy( temp, input+blockChainLen-16, 16 ); - ctx->encDec = 0; __nvt_aes_crypt(ctx, input, output, blockChainLen); - memcpy( iv, temp, 16 ); } length -= blockChainLen; input += blockChainLen; output += blockChainLen; } + /* Save IV for next block cipher */ + nu_set32_be(iv, ctx->iv[0]); + nu_set32_be(iv + 4, ctx->iv[1]); + nu_set32_be(iv + 8, ctx->iv[2]); + nu_set32_be(iv + 12, ctx->iv[3]); + return( 0 ); } #endif /* MBEDTLS_CIPHER_MODE_CBC */