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

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

commit b663bfed8eab1026b88446f7605593694981a17c
Author: makejian <[email protected]>
AuthorDate: Wed Sep 17 21:39:18 2025 +0800

    crypto/keymgmt: return actual length if key exported successfully
    
    When a key is successfully exported, the return value should reflect the 
actual length of the exported key data.
    
    Signed-off-by: makejian <[email protected]>
---
 crypto/cryptosoft.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/crypto/cryptosoft.c b/crypto/cryptosoft.c
index 9f4815219fd..0aca9ac6e56 100644
--- a/crypto/cryptosoft.c
+++ b/crypto/cryptosoft.c
@@ -504,9 +504,9 @@ static int swkey_export(FAR struct swkey_context_s *ctx,
           return -ENOBUFS;
         }
 
-      memcpy(buf, data->buf, buflen);
+      memcpy(buf, data->buf, data->size);
       swkey_promote_cache_data(ctx, data);
-      return OK;
+      return data->size;
     }
 
   /* Key not in cache, get key from flash */
@@ -545,7 +545,7 @@ static int swkey_export(FAR struct swkey_context_s *ctx,
       swkey_promote_cache_data(ctx, data);
     }
 
-  return OK;
+  return ret;
 }
 
 /****************************************************************************
@@ -777,6 +777,7 @@ static int swkey_kprocess(FAR struct cryptkop *krp)
   uint32_t pub_keyid;
   uint32_t keylen;
   uint32_t keyid;
+  int ret;
 
   /* Sanity check */
 
@@ -823,9 +824,18 @@ static int swkey_kprocess(FAR struct cryptkop *krp)
         krp->krp_status = swkey_delete(ctx, keyid);
         break;
       case CRK_EXPORT_KEY:
-        krp->krp_status = swkey_export(ctx, keyid,
-                                       krp->krp_param[1].crp_p,
-                                       krp->krp_param[1].crp_nbits / 8);
+        ret = swkey_export(ctx, keyid,
+                           krp->krp_param[1].crp_p,
+                           krp->krp_param[1].crp_nbits / 8);
+        if (ret < 0)
+          {
+            krp->krp_status = ret;
+          }
+        else
+          {
+            krp->krp_param[1].crp_nbits = ret * 8;
+          }
+
         break;
       case CRK_GENERATE_AES_KEY:
         if (krp->krp_param[1].crp_nbits != sizeof(uint32_t) * 8)

Reply via email to