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 cd9b2b9eb0f38792661461c1021bfa676e1734df
Author: makejian <[email protected]>
AuthorDate: Wed Nov 25 13:27:09 2026 +0800

    crypto/mbedtls: reset len/src and set olen in the alt finish paths
    
    The hash and MAC finish calls leave crypt.len and crypt.src set from
    the last update, so the final cryptodev request re-processes the
    message a second time: mbedtls_md5_finish() hashes the data twice and
    mbedtls_cipher_cmac() computes CMAC(M, M) for any non-empty message.
    RFC 4493 vectors for the 16/40/64-byte messages confirm the wrong
    tags.
    
    Clear len/src in every finish path (md5, sha1, sha256, sha512,
    poly1305, ripemd160, cmac) so the finalize request carries no payload,
    and set crypt.olen to the digest size.  olen is how many MAC bytes
    the kernel copies back on the asynchronous completion path
    (CIOCCRYPTRET); leaving it at 0 would make that copy vanish.
    
    Verified on sim:crypto + software cryptodev with the alt layer
    enabled: mbedtls_cipher_cmac() now returns the RFC 4493 values for
    all four vector lengths (0/16/40/64 bytes), the SP 800-38B AES-256
    vector, and the split update/finish flow.
    
    Signed-off-by: makejian <[email protected]>
---
 crypto/mbedtls/source/cmac_alt.c      | 3 +++
 crypto/mbedtls/source/md5_alt.c       | 3 +++
 crypto/mbedtls/source/poly1305_alt.c  | 3 +++
 crypto/mbedtls/source/ripemd160_alt.c | 3 +++
 crypto/mbedtls/source/sha1_alt.c      | 3 +++
 crypto/mbedtls/source/sha256_alt.c    | 3 +++
 crypto/mbedtls/source/sha512_alt.c    | 3 +++
 7 files changed, 21 insertions(+)

diff --git a/crypto/mbedtls/source/cmac_alt.c b/crypto/mbedtls/source/cmac_alt.c
index 7632d828c..b88f58fb9 100644
--- a/crypto/mbedtls/source/cmac_alt.c
+++ b/crypto/mbedtls/source/cmac_alt.c
@@ -154,6 +154,9 @@ int mbedtls_cipher_cmac_finish(FAR mbedtls_cipher_context_t 
*ctx,
     }
 
   cmac_ctx->dev.crypt.flags = 0;
+  cmac_ctx->dev.crypt.len = 0;
+  cmac_ctx->dev.crypt.src = NULL;
+  cmac_ctx->dev.crypt.olen = 16;
   cmac_ctx->dev.crypt.mac = (caddr_t)output;
   ret = cryptodev_crypt(&cmac_ctx->dev);
   if (ret != 0)
diff --git a/crypto/mbedtls/source/md5_alt.c b/crypto/mbedtls/source/md5_alt.c
index 0a9886a7b..2a37fc9a7 100644
--- a/crypto/mbedtls/source/md5_alt.c
+++ b/crypto/mbedtls/source/md5_alt.c
@@ -69,6 +69,9 @@ int mbedtls_md5_finish(FAR mbedtls_md5_context *ctx,
 
   ctx->crypt.op = COP_ENCRYPT;
   ctx->crypt.flags = 0;
+  ctx->crypt.len = 0;
+  ctx->crypt.src = NULL;
+  ctx->crypt.olen = 16;
   ctx->crypt.mac = (caddr_t)output;
   ret = cryptodev_crypt(ctx);
   cryptodev_free_session(ctx);
diff --git a/crypto/mbedtls/source/poly1305_alt.c 
b/crypto/mbedtls/source/poly1305_alt.c
index 0df9ddf3e..be2b96eee 100644
--- a/crypto/mbedtls/source/poly1305_alt.c
+++ b/crypto/mbedtls/source/poly1305_alt.c
@@ -77,6 +77,9 @@ int mbedtls_poly1305_finish(FAR mbedtls_poly1305_context *ctx,
 
   ctx->crypt.op = COP_ENCRYPT;
   ctx->crypt.flags = 0;
+  ctx->crypt.len = 0;
+  ctx->crypt.src = NULL;
+  ctx->crypt.olen = 16;
   ctx->crypt.mac = (caddr_t)mac;
   ret = cryptodev_crypt(ctx);
   if (ret != 0)
diff --git a/crypto/mbedtls/source/ripemd160_alt.c 
b/crypto/mbedtls/source/ripemd160_alt.c
index 2107a63d1..268ad7d34 100644
--- a/crypto/mbedtls/source/ripemd160_alt.c
+++ b/crypto/mbedtls/source/ripemd160_alt.c
@@ -69,6 +69,9 @@ int mbedtls_ripemd160_finish(FAR mbedtls_ripemd160_context 
*ctx,
 
   ctx->crypt.op = COP_ENCRYPT;
   ctx->crypt.flags = 0;
+  ctx->crypt.len = 0;
+  ctx->crypt.src = NULL;
+  ctx->crypt.olen = 20;
   ctx->crypt.mac = (caddr_t)output;
   ret = cryptodev_crypt(ctx);
   cryptodev_free_session(ctx);
diff --git a/crypto/mbedtls/source/sha1_alt.c b/crypto/mbedtls/source/sha1_alt.c
index 04e7c511a..8c8e6ef7b 100644
--- a/crypto/mbedtls/source/sha1_alt.c
+++ b/crypto/mbedtls/source/sha1_alt.c
@@ -79,6 +79,9 @@ int mbedtls_sha1_finish(FAR mbedtls_sha1_context *ctx,
 
   ctx->crypt.op = COP_ENCRYPT;
   ctx->crypt.flags = 0;
+  ctx->crypt.len = 0;
+  ctx->crypt.src = NULL;
+  ctx->crypt.olen = 20;
   ctx->crypt.mac = (caddr_t)output;
   ret = cryptodev_crypt(ctx);
   if (ret != 0)
diff --git a/crypto/mbedtls/source/sha256_alt.c 
b/crypto/mbedtls/source/sha256_alt.c
index 4de03a342..a2a54da00 100644
--- a/crypto/mbedtls/source/sha256_alt.c
+++ b/crypto/mbedtls/source/sha256_alt.c
@@ -92,6 +92,9 @@ int mbedtls_sha256_finish(FAR mbedtls_sha256_context *ctx,
 
   ctx->crypt.op = COP_ENCRYPT;
   ctx->crypt.flags = 0;
+  ctx->crypt.len = 0;
+  ctx->crypt.src = NULL;
+  ctx->crypt.olen = 32;
   ctx->crypt.mac = (caddr_t)output;
   ret = cryptodev_crypt(ctx);
   if (ret != 0)
diff --git a/crypto/mbedtls/source/sha512_alt.c 
b/crypto/mbedtls/source/sha512_alt.c
index 4fb3ed850..f9bef835a 100644
--- a/crypto/mbedtls/source/sha512_alt.c
+++ b/crypto/mbedtls/source/sha512_alt.c
@@ -92,6 +92,9 @@ int mbedtls_sha512_finish(FAR mbedtls_sha512_context *ctx,
 
   ctx->crypt.op = COP_ENCRYPT;
   ctx->crypt.flags = 0;
+  ctx->crypt.len = 0;
+  ctx->crypt.src = NULL;
+  ctx->crypt.olen = 64;
   ctx->crypt.mac = (caddr_t)output;
   ret = cryptodev_crypt(ctx);
   if (ret != 0)

Reply via email to