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 97b5b58488ca488df64886d2f6e1a00dc7fec4ec
Author: makejian <[email protected]>
AuthorDate: Mon Nov 23 11:30:05 2026 +0800

    virtio: add akcipher and extra algorithms to the crypto protocol header
    
    Extend the VirtIO crypto protocol with the AKCIPHER (asymmetric) service:
    the ECDH algorithm id, the GENKEY/DERIVE request opcodes, the curve id
    table and the DER encode/decode helpers that carry RSA and ECDSA keys
    over the wire. Also map the algorithms the cryptodev core gained since
    the header was written: ChaCha20, the AES-256-CMAC and AES-192/256-GMAC
    variants, HMAC-SHA-224, PBKDF2 and the per-request PBKDF2 iteration
    count, which rides in the spare room of the data request union so the
    session-less wire layout stays unchanged.
    
    No functional change for the existing symmetric driver beyond GMAC
    sessions now being negotiated through the AEAD service, matching what
    the backends implement.
    
    Signed-off-by: makejian <[email protected]>
---
 include/crypto/virtio_crypto.h | 106 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 106 insertions(+)

diff --git a/include/crypto/virtio_crypto.h b/include/crypto/virtio_crypto.h
index e0791d43fb1..277405ae5d2 100644
--- a/include/crypto/virtio_crypto.h
+++ b/include/crypto/virtio_crypto.h
@@ -28,8 +28,12 @@
  ****************************************************************************/
 
 #include <errno.h>
+#include <string.h>
+#include <sys/endian.h>
+
 #include <crypto/asn1.h>
 #include <crypto/cryptodev.h>
+#include <nuttx/nuttx.h>
 #include <nuttx/virtio/virtio-crypto.h>
 
 /****************************************************************************
@@ -101,6 +105,65 @@
  * Inline Functions
  ****************************************************************************/
 
+static inline_function void
+virtio_crypto_read_binary(FAR unsigned char **p, FAR unsigned char *mpi,
+                          size_t len)
+{
+  FAR uint32_t *left;
+  FAR uint32_t *right;
+  uint32_t tmp;
+
+  memset(mpi, 0, len);
+  if (len != 0)
+    {
+      memcpy(mpi, *p, len);
+    }
+
+  for (left = (FAR uint32_t *)mpi,
+       right = (FAR uint32_t *)(mpi + len - sizeof(*right));
+       left <= right; left++, right--)
+    {
+      tmp    = betoh32(*left);
+      *left  = betoh32(*right);
+      *right = tmp;
+    }
+
+  *p += len;
+}
+
+static inline_function int
+virtio_crypto_read_mpi(FAR unsigned char **pos, FAR unsigned char *end,
+                       FAR unsigned char *buf, FAR struct crparam *mpi)
+{
+  size_t len;
+  int ret;
+
+  ret = asn1_get_tag(pos, end, &len, ASN1_INTEGER);
+  if (ret != 0)
+    {
+      return ret;
+    }
+
+  /* Strip DER sign-extension: a leading 0x00 is only padding when the
+   * next byte has MSB set.
+   */
+
+  if (len > 1 && **pos == 0 && ((*pos)[1] & 0x80) != 0)
+    {
+      (*pos)++;
+      len--;
+    }
+
+  virtio_crypto_read_binary(pos, buf, len);
+  if (len != 1 || *buf != 0)
+    {
+      mpi->crp_p = (caddr_t)buf;
+      mpi->crp_nbits = len * 8;
+    }
+
+  return ALIGN_UP(len, 4); /* round up to aligned size */
+}
+
 /* RSAPublicKey ::= SEQUENCE {
  *     modulus           INTEGER,  -- n
  *     publicExponent    INTEGER   -- e
@@ -535,4 +598,47 @@ int virtio_crypto_get_aead_alg(int alg)
   return -EINVAL;
 }
 
+static inline_function
+int virtio_crypto_ecdsa_get_curve(int kalg)
+{
+  switch (kalg)
+    {
+      case CRK_ECDSA_SECP256R1_SIGN:
+      case CRK_ECDSA_SECP256R1_VERIFY:
+      case CRK_ECDSA_SECP256R1_GENKEY:
+      case CRK_ECDH_GEN_PUBLIC:
+      case CRK_ECDH_COMPUTE_SHARED:
+        return VIRTIO_CRYPTO_CURVE_NIST_P256;
+    }
+
+  return -EINVAL;
+}
+
+/* Per-curve field element length in bytes, per the virtio-crypto spec's
+ * VIRTIO_CRYPTO_CURVE_* IDs. Only NIST_P256 has a backing cryptodev
+ * implementation today; the rest are wired up so adding a new curve is
+ * a matter of adding a case here plus the cryptodev/cryptosoft support,
+ * not hunting down every hardcoded 32.
+ */
+
+static inline_function
+int virtio_crypto_curve_get_len(uint32_t curve_id)
+{
+  switch (curve_id)
+  {
+    case VIRTIO_CRYPTO_CURVE_NIST_P192:
+      return 24;
+    case VIRTIO_CRYPTO_CURVE_NIST_P224:
+      return 28;
+    case VIRTIO_CRYPTO_CURVE_NIST_P256:
+      return 32;
+    case VIRTIO_CRYPTO_CURVE_NIST_P384:
+      return 48;
+    case VIRTIO_CRYPTO_CURVE_NIST_P521:
+      return 66;
+  }
+
+  return -EINVAL;
+}
+
 #endif /* __INCLUDE_CRYPTO_VIRTIO_CRYPTO_H */

Reply via email to