You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2026/01/20 16:25:12 UTC

(nuttx) branch master updated (66f9ced0cb7 -> 5b52a32f5a0)

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

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


    from 66f9ced0cb7 arch: default enable TLS_ALIGNED when BUILD_PROTECTED=y
     new 143547128b2 crypto/cryptodev: add encrypt op and olen for support virtio mode
     new 66f93298397 crypto/cryptodev: export ivlen to support different cipher algs
     new e23dd613c91 crypto/cryptodev: optimize without dynamic memory in crypto process
     new 5b52a32f5a0 crypto/crypto.c: Determine the order of obtained crypto drivers

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 crypto/crypto.c            |  75 ++-------------------
 crypto/cryptodev.c         | 161 ++++++++++++++-------------------------------
 include/crypto/cryptodev.h |  10 +--
 3 files changed, 63 insertions(+), 183 deletions(-)


(nuttx) 03/04: crypto/cryptodev: optimize without dynamic memory in crypto process

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit e23dd613c91329ed9ff5cc807a7897e8132ec881
Author: makejian <ma...@xiaomi.com>
AuthorDate: Mon Jun 16 22:40:42 2025 +0800

    crypto/cryptodev: optimize without dynamic memory in crypto process
    
    Replace dynamic memory allocation with stack-based variables in cryptodev_op().
    This eliminates kmm_malloc/kmm_free overhead and simplifies error handling
    by removing the need for goto bail cleanup paths.
    
    Signed-off-by: makejian <ma...@xiaomi.com>
---
 crypto/crypto.c            |  59 -----------------
 crypto/cryptodev.c         | 162 ++++++++++++++-------------------------------
 include/crypto/cryptodev.h |   4 --
 3 files changed, 50 insertions(+), 175 deletions(-)

diff --git a/crypto/crypto.c b/crypto/crypto.c
index 7cefc768ad2..8766581b931 100644
--- a/crypto/crypto.c
+++ b/crypto/crypto.c
@@ -624,65 +624,6 @@ migrate:
   return 0;
 }
 
-/* Release a set of crypto descriptors. */
-
-void crypto_freereq(FAR struct cryptop *crp)
-{
-  FAR struct cryptodesc *crd;
-
-  if (crp == NULL)
-    {
-      return;
-    }
-
-  nxmutex_lock(&g_crypto_lock);
-
-  while ((crd = crp->crp_desc) != NULL)
-    {
-      crp->crp_desc = crd->crd_next;
-      kmm_free(crd);
-    }
-
-  kmm_free(crp);
-  nxmutex_unlock(&g_crypto_lock);
-}
-
-/* Acquire a set of crypto descriptors. */
-
-FAR struct cryptop *crypto_getreq(int num)
-{
-  FAR struct cryptodesc *crd;
-  FAR struct cryptop *crp;
-
-  nxmutex_lock(&g_crypto_lock);
-
-  crp = kmm_malloc(sizeof(struct cryptop));
-  if (crp == NULL)
-    {
-      nxmutex_unlock(&g_crypto_lock);
-      return NULL;
-    }
-
-  bzero(crp, sizeof(struct cryptop));
-
-  while (num--)
-    {
-      crd = kmm_calloc(1, sizeof(struct cryptodesc));
-      if (crd == NULL)
-        {
-          nxmutex_unlock(&g_crypto_lock);
-          crypto_freereq(crp);
-          return NULL;
-        }
-
-      crd->crd_next = crp->crp_desc;
-      crp->crp_desc = crd;
-    }
-
-  nxmutex_unlock(&g_crypto_lock);
-  return crp;
-}
-
 int crypto_getfeat(FAR int *featp)
 {
   extern int cryptodevallowsoft;
diff --git a/crypto/cryptodev.c b/crypto/cryptodev.c
index 9ec57d9bbef..40cf9c11d4f 100644
--- a/crypto/cryptodev.c
+++ b/crypto/cryptodev.c
@@ -390,172 +390,110 @@ bail:
 static int cryptodev_op(FAR struct csession *cse,
                         FAR struct crypt_op *cop)
 {
-  FAR struct cryptop *crp = NULL;
-  FAR struct cryptodesc *crde = NULL;
-  FAR struct cryptodesc *crda = NULL;
+  struct cryptop crp;
+  struct cryptodesc crda;
+  struct cryptodesc crde;
   int error = OK;
   uint32_t hid;
 
   /* number of requests, not logical and */
 
-  crp = crypto_getreq(cse->txform + cse->thash);
-  if (crp == NULL)
-    {
-      error = -ENOMEM;
-      goto bail;
-    }
-
+  bzero(&crp, sizeof(struct cryptop));
+  bzero(&crda, sizeof(struct cryptodesc));
+  bzero(&crde, sizeof(struct cryptodesc));
   if (cse->thash)
     {
-      crda = crp->crp_desc;
-      if (cse->txform)
-        crde = crda->crd_next;
-    }
-  else
-    {
-      if (cse->txform)
+      crp.crp_desc = &crda;
+      crda.crd_skip = 0;
+      crda.crd_len = cop->len;
+      crda.crd_inject = 0;
+
+      crda.crd_alg = cse->mac;
+      crda.crd_key = cse->mackey;
+      crda.crd_klen = cse->mackeylen * 8;
+      if (cop->flags & COP_FLAG_UPDATE)
         {
-          crde = crp->crp_desc;
+          crda.crd_flags |= CRD_F_UPDATE;
         }
       else
         {
-          error = -EINVAL;
-          goto bail;
+          crda.crd_flags &= ~CRD_F_UPDATE;
         }
     }
 
-  if (crda)
+  if (cse->txform)
     {
-      crda->crd_skip = 0;
-      crda->crd_len = cop->len;
-      crda->crd_inject = 0;
-
-      crda->crd_alg = cse->mac;
-      crda->crd_key = cse->mackey;
-      crda->crd_klen = cse->mackeylen * 8;
-      if (cop->flags & COP_FLAG_UPDATE)
+      if (cse->thash)
         {
-          crda->crd_flags |= CRD_F_UPDATE;
+          crda.crd_next = &crde;
         }
       else
         {
-          crda->crd_flags &= ~CRD_F_UPDATE;
+          crp.crp_desc = &crde;
         }
-    }
 
-  if (crde)
-    {
       if (cop->op == COP_ENCRYPT)
         {
-          crde->crd_flags |= CRD_F_ENCRYPT;
+          crde.crd_flags |= CRD_F_ENCRYPT;
         }
       else
         {
-          crde->crd_flags &= ~CRD_F_ENCRYPT;
+          crde.crd_flags &= ~CRD_F_ENCRYPT;
         }
 
-      crde->crd_len = cop->len;
-      crde->crd_inject = 0;
-      crde->crd_alg = cse->cipher;
-      crde->crd_key = cse->key;
-      crde->crd_klen = cse->keylen * 8;
+      crde.crd_len = cop->len;
+      crde.crd_inject = 0;
+      crde.crd_alg = cse->cipher;
+      crde.crd_key = cse->key;
+      crde.crd_klen = cse->keylen * 8;
     }
 
-  crp->crp_ilen = cop->len;
-  crp->crp_olen = cop->olen;
-  crp->crp_buf = cop->src;
-  crp->crp_sid = cse->sid;
-  crp->crp_opaque = cse;
+  crp.crp_ilen = cop->len;
+  crp.crp_olen = cop->olen;
+  crp.crp_buf = cop->src;
+  crp.crp_sid = cse->sid;
+  crp.crp_opaque = cse;
 
   if (cop->iv)
     {
-      if (crde == NULL)
-        {
-          error = -EINVAL;
-          goto bail;
-        }
-
-      crp->crp_iv = cop->iv;
-      crp->crp_ivlen = cop->ivlen;
+      crp.crp_iv = cop->iv;
+      crp.crp_ivlen = cop->ivlen;
     }
 
   if (cop->dst)
     {
-      if (crde == NULL)
-        {
-          error = -EINVAL;
-          goto bail;
-        }
-
-      crp->crp_dst = cop->dst;
+      crp.crp_dst = cop->dst;
     }
 
   if (cop->mac)
     {
-      if (crda == NULL)
-        {
-          error = -EINVAL;
-          goto bail;
-        }
-
-      crp->crp_mac = cop->mac;
+      crp.crp_mac = cop->mac;
     }
 
   /* try the fast path first */
 
-  crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_NOQUEUE;
-  hid = (crp->crp_sid >> 32) & 0xffffffff;
-  if (hid >= crypto_drivers_num)
-    {
-      goto dispatch;
-    }
-
-  if (crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE)
-    {
-      goto dispatch;
-    }
-
-  if (crypto_drivers[hid].cc_process == NULL)
-    {
-      goto dispatch;
-    }
-
-  error = crypto_drivers[hid].cc_process(crp);
-  if (error)
+  crp.crp_flags = CRYPTO_F_IOV | CRYPTO_F_NOQUEUE;
+  hid = (crp.crp_sid >> 32) & 0xffffffff;
+  if (hid >= crypto_drivers_num ||
+      (crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE) ||
+      crypto_drivers[hid].cc_process == NULL)
     {
-      /* clear error */
-
-      crp->crp_etype = 0;
-      goto dispatch;
+      crp.crp_flags = CRYPTO_F_IOV;
+      crypto_invoke(&crp);
     }
-
-  goto processed;
-dispatch:
-  crp->crp_flags = CRYPTO_F_IOV;
-  crypto_invoke(crp);
-processed:
-
-  if (crde && (cop->flags & COP_FLAG_UPDATE) == 0)
-    {
-      crde->crd_flags &= ~CRD_F_IV_EXPLICIT;
-    }
-
-  if (cse->error)
+  else
     {
-      error = cse->error;
-      goto bail;
+      error = crypto_drivers[hid].cc_process(&crp);
     }
 
-  if (crp->crp_etype != 0)
+  if ((cop->flags & COP_FLAG_UPDATE) == 0)
     {
-      error = crp->crp_etype;
-      goto bail;
+      crde.crd_flags &= ~CRD_F_IV_EXPLICIT;
     }
 
-bail:
-  if (crp)
+  if (!error && crp.crp_etype != 0)
     {
-      crypto_freereq(crp);
+      error = crp.crp_etype;
     }
 
   return error;
diff --git a/include/crypto/cryptodev.h b/include/crypto/cryptodev.h
index 762d567ff28..aaec0790dbd 100644
--- a/include/crypto/cryptodev.h
+++ b/include/crypto/cryptodev.h
@@ -445,10 +445,6 @@ int crypto_kinvoke(FAR struct cryptkop *);
 int crypto_getfeat(FAR int *);
 int crypto_driver_set_priv(uint32_t, FAR void *);
 FAR void *crypto_driver_get_priv(uint32_t);
-
-FAR struct cryptop *crypto_getreq(int);
-void crypto_freereq(FAR struct cryptop *);
-
 #ifdef CONFIG_CRYPTO_CRYPTODEV_HARDWARE
 void hwcr_init(void);
 #endif


(nuttx) 02/04: crypto/cryptodev: export ivlen to support different cipher algs

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 66f932983971e1c9d93c71b876a29695f6ff4099
Author: makejian <ma...@xiaomi.com>
AuthorDate: Wed May 21 14:25:51 2025 +0800

    crypto/cryptodev: export ivlen to support different cipher algs
    
    Add ivlen field to crypt_op and crp_ivlen to cryptop structure to support
    cipher algorithms with different IV lengths.
    
    Signed-off-by: makejian <ma...@xiaomi.com>
---
 crypto/cryptodev.c         | 1 +
 include/crypto/cryptodev.h | 2 ++
 2 files changed, 3 insertions(+)

diff --git a/crypto/cryptodev.c b/crypto/cryptodev.c
index 9235cf4bb60..9ec57d9bbef 100644
--- a/crypto/cryptodev.c
+++ b/crypto/cryptodev.c
@@ -476,6 +476,7 @@ static int cryptodev_op(FAR struct csession *cse,
         }
 
       crp->crp_iv = cop->iv;
+      crp->crp_ivlen = cop->ivlen;
     }
 
   if (cop->dst)
diff --git a/include/crypto/cryptodev.h b/include/crypto/cryptodev.h
index bd4c9a0d970..762d567ff28 100644
--- a/include/crypto/cryptodev.h
+++ b/include/crypto/cryptodev.h
@@ -201,6 +201,7 @@ struct cryptop
   uint64_t crp_sid;  /* Session ID */
   int crp_ilen;      /* Input data total length */
   int crp_olen;      /* Result total length */
+  int crp_ivlen;     /* IV length */
   int crp_alloctype; /* Type of buf to allocate if needed */
   int crp_etype;     /* Error type (zero means no error).
                       * All error codes except EAGAIN
@@ -399,6 +400,7 @@ struct crypt_op
   uint16_t flags;
   unsigned len;
   unsigned olen;
+  unsigned ivlen;
   unsigned aadlen;
   caddr_t src, dst;   /* become iov[] inside kernel */
   caddr_t mac;        /* must be big enough for chosen MAC */


(nuttx) 04/04: crypto/crypto.c: Determine the order of obtained crypto drivers

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 5b52a32f5a024ff91fad33cf65f8c4e8a9708eba
Author: makejian <ma...@xiaomi.com>
AuthorDate: Fri May 30 15:07:32 2025 +0800

    crypto/crypto.c: Determine the order of obtained crypto drivers
    
    After adding the cross-core crypto driver, there are now three encryption modes:
    1. Hardware driver in local core
    2. Crypto driver in remote core
    3. Software encryption in local core
    
    This prioritizes local hardware driver first, then remote driver (typically hardware),
    and finally local software encryption as a fallback.
    
    Signed-off-by: makejian <ma...@xiaomi.com>
---
 crypto/crypto.c            | 16 ++++++----------
 include/crypto/cryptodev.h |  1 +
 2 files changed, 7 insertions(+), 10 deletions(-)

diff --git a/crypto/crypto.c b/crypto/crypto.c
index 8766581b931..e463f3204cb 100644
--- a/crypto/crypto.c
+++ b/crypto/crypto.c
@@ -106,22 +106,18 @@ int crypto_newsession(FAR uint64_t *sid,
 
           if (cpc->cc_flags & CRYPTOCAP_F_SOFTWARE)
             {
-              /* First round of search, ignore
-               * software drivers.
-               */
+              /* Thread round of search only for software */
 
-              if (turn == 0)
+              if (turn != 2)
                 {
                   continue;
                 }
             }
-          else
+          else if (cpc->cc_flags & CRYPTOCAP_F_REMOTE)
             {
-              /* !CRYPTOCAP_F_SOFTWARE
-               * Second round of search, only software.
-               */
+              /* Second round of search only for remote */
 
-              if (turn == 1)
+              if (turn != 1)
                 {
                   continue;
                 }
@@ -189,7 +185,7 @@ int crypto_newsession(FAR uint64_t *sid,
 
       /* If we only want hardware drivers, don't do second pass. */
     }
-  while (turn <= 2 && hard == 0);
+  while (turn < 2 || (turn == 2 && !hard));
 
   hid = hid2;
 
diff --git a/include/crypto/cryptodev.h b/include/crypto/cryptodev.h
index aaec0790dbd..c26810f3d6a 100644
--- a/include/crypto/cryptodev.h
+++ b/include/crypto/cryptodev.h
@@ -352,6 +352,7 @@ struct cryptocap
 #define CRYPTOCAP_F_SOFTWARE    0x02
 #define CRYPTOCAP_F_ENCRYPT_MAC 0x04 /* Can do encrypt-then-MAC (IPsec) */
 #define CRYPTOCAP_F_MAC_ENCRYPT 0x08 /* Can do MAC-then-encrypt (TLS) */
+#define CRYPTOCAP_F_REMOTE      0x10 /* Remote core driver  */
 
   CODE int (*cc_newsession)(FAR uint32_t *, FAR struct cryptoini *);
   CODE int (*cc_process)(FAR struct cryptop *);


(nuttx) 01/04: crypto/cryptodev: add encrypt op and olen for support virtio mode

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 143547128b2f0a9ba2846077b9344f5641ff4639
Author: makejian <ma...@xiaomi.com>
AuthorDate: Tue Apr 22 21:13:11 2025 +0800

    crypto/cryptodev: add encrypt op and olen for support virtio mode
    
    Add olen field to crypt_op structure and crp_olen to cryptop structure
    to support output length tracking in virtio crypto mode.
    
    Signed-off-by: makejian <ma...@xiaomi.com>
---
 crypto/cryptodev.c         | 2 ++
 include/crypto/cryptodev.h | 3 +++
 2 files changed, 5 insertions(+)

diff --git a/crypto/cryptodev.c b/crypto/cryptodev.c
index 2bcd8a4b4c0..9235cf4bb60 100644
--- a/crypto/cryptodev.c
+++ b/crypto/cryptodev.c
@@ -277,6 +277,7 @@ static int cryptof_ioctl(FAR struct file *filep,
           {
             crie.cri_alg = sop->cipher;
             crie.cri_klen = sop->keylen * 8;
+            crie.cri_op = sop->op;
 
             crie.cri_key = kmm_malloc(crie.cri_klen / 8);
             if (crie.cri_key == NULL)
@@ -461,6 +462,7 @@ static int cryptodev_op(FAR struct csession *cse,
     }
 
   crp->crp_ilen = cop->len;
+  crp->crp_olen = cop->olen;
   crp->crp_buf = cop->src;
   crp->crp_sid = cse->sid;
   crp->crp_opaque = cse;
diff --git a/include/crypto/cryptodev.h b/include/crypto/cryptodev.h
index 9fbf8e9af31..bd4c9a0d970 100644
--- a/include/crypto/cryptodev.h
+++ b/include/crypto/cryptodev.h
@@ -152,6 +152,7 @@ struct cryptoini
   int cri_klen;      /* Key length, in bits */
   int cri_rnd;       /* Algorithm rounds, where relevant */
   int cri_sid;
+  int cri_op;
   caddr_t cri_key;   /* key to use */
   union
   {
@@ -363,6 +364,7 @@ struct cryptocap
 struct session_op
 {
   uint32_t cipher;    /* ie. CRYPTO_AES_EBC */
+  uint16_t op;        /* i.e. COP_ENCRYPT */
   uint32_t mac;
   uint32_t keylen;    /* cipher key */
   caddr_t key;
@@ -396,6 +398,7 @@ struct crypt_op
 
   uint16_t flags;
   unsigned len;
+  unsigned olen;
   unsigned aadlen;
   caddr_t src, dst;   /* become iov[] inside kernel */
   caddr_t mac;        /* must be big enough for chosen MAC */