From 289bbf0ec788b3015fe62aeea9f709adf158f366 Mon Sep 17 00:00:00 2001 From: ccli8 Date: Wed, 27 Sep 2017 10:19:46 +0800 Subject: [PATCH] [NUC472/M487] Fix AES alter. CFB128 error --- .../TARGET_NUVOTON/TARGET_M480/aes/aes_alt.c | 32 +++++++++---------- .../TARGET_NUC472/aes/aes_alt.c | 32 +++++++++---------- 2 files changed, 32 insertions(+), 32 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 60e70b1685..ddb8fcda24 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 @@ -343,31 +343,31 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx, ctx->encDec = 1; } + /* 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); + while (block_chain_len) { size_t block_chain_len2 = (block_chain_len > MAX_DMA_CHAIN_SIZE) ? MAX_DMA_CHAIN_SIZE : block_chain_len; - - /* 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); - + __nvt_aes_crypt(ctx, input, output, block_chain_len2); input += block_chain_len2; output += block_chain_len2; length -= block_chain_len2; - - /* NOTE: Buffers input/output could overlap. See ctx->iv rather than input/output - * for iv of next block cipher. */ - /* 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); - + block_chain_len -= block_chain_len2; } + + /* NOTE: Buffers input/output could overlap. See ctx->iv rather than input/output + * for iv of next block cipher. */ + /* Fetch IV byte data in big-endian */ + 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]); } /* Last incomplete block */ 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 3acf43d1fb..be03f0cdb5 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 @@ -343,31 +343,31 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx, ctx->encDec = 1; } + /* 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); + while (block_chain_len) { size_t block_chain_len2 = (block_chain_len > MAX_DMA_CHAIN_SIZE) ? MAX_DMA_CHAIN_SIZE : block_chain_len; - - /* 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); - + __nvt_aes_crypt(ctx, input, output, block_chain_len2); input += block_chain_len2; output += block_chain_len2; length -= block_chain_len2; - - /* NOTE: Buffers input/output could overlap. See ctx->iv rather than input/output - * for iv of next block cipher. */ - /* 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); - + block_chain_len -= block_chain_len2; } + + /* NOTE: Buffers input/output could overlap. See ctx->iv rather than input/output + * for iv of next block cipher. */ + /* Fetch IV byte data in big-endian */ + 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]); } /* Last incomplete block */