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.git

commit 7a8c8d6bf18da0d9b2cfc696ededfa5c7bd2a2de
Author: makejian <[email protected]>
AuthorDate: Thu Nov 19 20:02:25 2026 +0800

    crypto/cryptodev: add ECDH key agreement commands
    
    Add CRK_ECDH_GEN_PUBLIC and CRK_ECDH_COMPUTE_SHARED for ECDH (secp256r1)
    key agreement, used by TLS ECDHE. The commands are appended at the tail of
    the CRK command space (26/27) so existing key management command numbers
    stay stable and the ABI is preserved for already-built binaries.
    
     - cryptodev.h: define the two commands, bump CRK_ALGORITHM_MAX to 27 and
       add matching CRF_ECDH_* capability flags.
     - cryptodev.c: validate param counts (gen_public in0/out4,
       compute_shared in3/out1) in cryptodev_key.
     - cryptosoft.c: software fallback via ecc_make_key_uncomp /
       ecdh_shared_secret and register both algorithms in swcr_init.
    
    The integers cross the boundary little-endian, like every other
    asymmetric operand of the driver.
    
    Signed-off-by: makejian <[email protected]>
---
 crypto/cryptodev.c         | 14 +++++++++
 crypto/cryptosoft.c        | 77 ++++++++++++++++++++++++++++++++++++++++++++++
 include/crypto/cryptodev.h |  6 +++-
 3 files changed, 96 insertions(+), 1 deletion(-)

diff --git a/crypto/cryptodev.c b/crypto/cryptodev.c
index c181bc228fa..1035f93a1aa 100644
--- a/crypto/cryptodev.c
+++ b/crypto/cryptodev.c
@@ -667,6 +667,20 @@ static int cryptodev_key(FAR struct fcrypt *fcr, FAR 
struct crypt_kop *kop)
           }
 
         return -EINVAL;
+      case CRK_ECDH_GEN_PUBLIC:
+        if (in == 0 && out == 4)
+          {
+            break;
+          }
+
+        return -EINVAL;
+      case CRK_ECDH_COMPUTE_SHARED:
+        if (in == 3 && out == 1)
+          {
+            break;
+          }
+
+        return -EINVAL;
 
       /* key management */
 
diff --git a/crypto/cryptosoft.c b/crypto/cryptosoft.c
index 0dc9d494224..f182dca223c 100644
--- a/crypto/cryptosoft.c
+++ b/crypto/cryptosoft.c
@@ -2759,6 +2759,67 @@ static int swcr_ecc256_verify(FAR struct cryptkop *krp)
   return OK;
 }
 
+static int swcr_ecdh256_gen_public(FAR struct cryptkop *krp)
+{
+  /* Generating an ephemeral ECDH keypair is identical to ECDSA key
+   * generation: both produce a SECP256R1 keypair in param[0]=d,
+   * param[1]=x, param[2]=y.
+   */
+
+  return swcr_ecc256_genkey(krp);
+}
+
+static int swcr_ecdh256_compute_shared(FAR struct cryptkop *krp)
+{
+  uint8_t publickey[secp256r1 + 1];
+  uint8_t privatekey[secp256r1];
+  uint8_t secret[secp256r1];
+  uint8_t x[secp256r1];
+  uint8_t y[secp256r1];
+  int ret;
+
+  /* Private key: param[0]; peer public key: param[1]=Q.X, param[2]=Q.Y. */
+
+  ret = swcr_ecc_copy(x, krp->krp_param[1].crp_p,
+                      krp->krp_param[1].crp_nbits / 8);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = swcr_ecc_copy(y, krp->krp_param[2].crp_p,
+                      krp->krp_param[2].crp_nbits / 8);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = swcr_ecc_copy(privatekey, krp->krp_param[0].crp_p,
+                      krp->krp_param[0].crp_nbits / 8);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Compress the peer public key: SEC1 prefix (0x02/0x03 by Y parity) + X */
+
+  publickey[0] = 2 + (y[secp256r1 - 1] & 0x01);
+  memcpy(publickey + 1, x, secp256r1);
+
+  if (ecdh_shared_secret(publickey, privatekey, secret) == 0)
+    {
+      ret = -EINVAL;
+    }
+  else
+    {
+      swcr_ecc_copy(krp->krp_param[3].crp_p, secret, secp256r1);
+    }
+
+  explicit_bzero(privatekey, sizeof(privatekey));
+  explicit_bzero(secret, sizeof(secret));
+  return ret;
+}
+
 int swcr_kprocess(struct cryptkop *krp)
 {
   /* Sanity check */
@@ -2820,6 +2881,20 @@ int swcr_kprocess(struct cryptkop *krp)
             goto done;
           }
 
+        break;
+      case CRK_ECDH_GEN_PUBLIC:
+        if ((krp->krp_status = swcr_ecdh256_gen_public(krp)) != 0)
+          {
+            goto done;
+          }
+
+        break;
+      case CRK_ECDH_COMPUTE_SHARED:
+        if ((krp->krp_status = swcr_ecdh256_compute_shared(krp)) != 0)
+          {
+            goto done;
+          }
+
         break;
       default:
 
@@ -2907,6 +2982,8 @@ void swcr_init(void)
   kalgs[CRK_ECDSA_SECP256R1_SIGN] = CRYPTO_ALG_FLAG_SUPPORTED;
   kalgs[CRK_ECDSA_SECP256R1_VERIFY] = CRYPTO_ALG_FLAG_SUPPORTED;
   kalgs[CRK_ECDSA_SECP256R1_GENKEY] = CRYPTO_ALG_FLAG_SUPPORTED;
+  kalgs[CRK_ECDH_GEN_PUBLIC] = CRYPTO_ALG_FLAG_SUPPORTED;
+  kalgs[CRK_ECDH_COMPUTE_SHARED] = CRYPTO_ALG_FLAG_SUPPORTED;
   crypto_kregister(swcr_id, kalgs, swcr_kprocess);
 }
 
diff --git a/include/crypto/cryptodev.h b/include/crypto/cryptodev.h
index 3fe7458468a..51f09efd7b8 100644
--- a/include/crypto/cryptodev.h
+++ b/include/crypto/cryptodev.h
@@ -333,7 +333,9 @@ struct crypt_kop
 #define CRK_SAVE_KEY               23 /* Save key data into FLASH */
 #define CRK_LOAD_KEY               24 /* Load key data from FLASH into RAM */
 #define CRK_UNLOAD_KEY             25 /* Unload key data from RAM */
-#define CRK_ALGORITHM_MAX          25 /* Keep updated */
+#define CRK_ECDH_GEN_PUBLIC        26 /* Generate an ephemeral ECDH keypair */
+#define CRK_ECDH_COMPUTE_SHARED    27 /* Compute ECDH shared secret */
+#define CRK_ALGORITHM_MAX          27 /* Keep updated */
 
 #define CRF_MOD_EXP                (1 << CRK_MOD_EXP)
 #define CRF_MOD_EXP_CRT            (1 << CRK_MOD_EXP_CRT)
@@ -347,6 +349,8 @@ struct crypt_kop
 #define CRF_ECDSA_SECP256R1_SIGN   (1 << CRK_ECDSA_SECP256R1_SIGN)
 #define CRF_ECDSA_SECP256R1_VERIFY (1 << CRK_ECDSA_SECP256R1_VERIFY)
 #define CRF_ECDSA_SECP256R1_GENKEY (1 << CRK_ECDSA_SECP256R1_GENKEY)
+#define CRF_ECDH_GEN_PUBLIC        (1 << CRK_ECDH_GEN_PUBLIC)
+#define CRF_ECDH_COMPUTE_SHARED    (1 << CRK_ECDH_COMPUTE_SHARED)
 #define CRF_ALLOCATE_KEY           (1 << CRK_ALLOCATE_KEY)
 #define CRF_VALIDATE_KEYID         (1 << CRK_VALIDATE_KEYID)
 #define CRF_IMPORT_KEY             (1 << CRK_IMPORT_KEY)

Reply via email to