This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit 9c195ac099438629746bf4362170f4e8b91ba415
Author: makejian <[email protected]>
AuthorDate: Tue Nov 10 18:01:31 2026 +0800

    crypto/mbedtls: fix the AES-CBC algorithm id and IV chaining in aes_alt
    
    mbedtls_aes_crypt_cbc() had two defects in the cryptodev backend:
    
    The session cipher was hardcoded to CRYPTO_AES_CBC, which is the
    AES-128-CBC id, so a session created with a 192/256-bit key ran the
    wrong algorithm and produced garbage. Derive the CBC id from the key
    length stored in the session: 24 bytes selects CRYPTO_AES_192_CBC and
    32 bytes CRYPTO_AES_256_CBC, everything else keeps CRYPTO_AES_CBC.
    
    The IV writeback read the last ciphertext block from the output
    buffer, but after a decryption that buffer holds plaintext. Keep a
    copy of the last input block before the operation and write that back
    as the chain IV on decryption. Also reject a zero length, which the
    block check below otherwise accepts.
    
    Signed-off-by: makejian <[email protected]>
---
 crypto/mbedtls/source/aes_alt.c | 36 ++++++++++++++++++++++++++++++++++--
 1 file changed, 34 insertions(+), 2 deletions(-)

diff --git a/crypto/mbedtls/source/aes_alt.c b/crypto/mbedtls/source/aes_alt.c
index 23baa1959..649e6f792 100644
--- a/crypto/mbedtls/source/aes_alt.c
+++ b/crypto/mbedtls/source/aes_alt.c
@@ -117,6 +117,25 @@ int mbedtls_aes_crypt_ecb(FAR mbedtls_aes_context *ctx,
 
 /* AES-CBC buffer encryption/decryption */
 
+/* The cryptodev AES-128-CBC id is CRYPTO_AES_CBC; pick the variant that
+ * matches the key length of the session.
+ */
+
+static uint32_t aes_cbc_cipher_id(unsigned int keylen)
+{
+  if (keylen == 24)
+    {
+      return CRYPTO_AES_192_CBC;
+    }
+
+  if (keylen == 32)
+    {
+      return CRYPTO_AES_256_CBC;
+    }
+
+  return CRYPTO_AES_CBC;
+}
+
 int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
                           int mode,
                           size_t length,
@@ -124,6 +143,7 @@ int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
                           const unsigned char *input,
                           unsigned char *output)
 {
+  unsigned char temp[16];
   int ret;
 
   if (mode != MBEDTLS_AES_ENCRYPT && mode != MBEDTLS_AES_DECRYPT)
@@ -131,12 +151,17 @@ int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
       return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
     }
 
-  if ((length % 16) != 0)
+  if (length == 0 || (length % 16) != 0)
     {
       return MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH;
     }
 
-  ctx->dev.session.cipher = CRYPTO_AES_CBC;
+  if (mode == MBEDTLS_AES_DECRYPT)
+    {
+      memcpy(temp, input + length - 16, 16);
+    }
+
+  ctx->dev.session.cipher = aes_cbc_cipher_id(ctx->dev.session.keylen);
   ret = cryptodev_get_session(&ctx->dev);
   if (ret != 0)
     {
@@ -152,6 +177,13 @@ int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
   ctx->dev.crypt.iv = (caddr_t)iv;
   ret = cryptodev_crypt(&ctx->dev);
   cryptodev_free_session(&ctx->dev);
+
+  if (ret == 0)
+    {
+      memcpy(iv, mode == MBEDTLS_AES_ENCRYPT ?
+                 output + length - 16 : temp, 16);
+    }
+
   return ret;
 }
 #endif /* MBEDTLS_CIPHER_MODE_CBC */

Reply via email to