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 9baa0cc6adb77bf9be1e1bb49551fbad50fabbad
Author: makejian <[email protected]>
AuthorDate: Thu Nov 19 19:59:26 2026 +0800

    crypto/cryptosoft: pass ECC integers little-endian
    
    cryptodev carries asymmetric integers little-endian: the RSA path in
    cryptosoft already converts them, while the ECC path handed the raw
    buffers to ecc.c, which reads big-endian bytes.  A self-contained
    genkey/sign/verify round trip worked either way, but a key or signature
    coming from anywhere else failed to verify.
    
    Convert d, hash, Q.x/Q.y and r/s between little- and big-endian at the
    cryptosoft boundary and store generated SECP256R1 key pairs little-endian
    as well.  The operands now go through a helper that bounds them to 32
    bytes instead of trusting the caller, and the byte swap helper is shared
    with RSA.  An invalid ECDSA or RSA PKCS#1 v1.5 signature reports -EBADMSG
    so callers can tell a bad signature from a bad request.
    
    Signed-off-by: makejian <[email protected]>
---
 crypto/cryptosoft.c | 183 +++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 138 insertions(+), 45 deletions(-)

diff --git a/crypto/cryptosoft.c b/crypto/cryptosoft.c
index 791ea1af06b..0dc9d494224 100644
--- a/crypto/cryptosoft.c
+++ b/crypto/cryptosoft.c
@@ -119,6 +119,45 @@ int swcr_id = -1;
  * Private Functions
  ****************************************************************************/
 
+/* Reverse a byte string in place: cryptodev carries asymmetric integers
+ * little-endian, crypto/rsa.c and crypto/ecc.c in big-endian.
+ */
+
+static void swcr_reverse(FAR uint8_t *buf, size_t len)
+{
+  uint8_t tmp;
+  size_t i;
+
+  for (i = 0; i < len / 2; i++)
+    {
+      tmp = buf[i];
+      buf[i] = buf[len - 1 - i];
+      buf[len - 1 - i] = tmp;
+    }
+}
+
+#ifdef CONFIG_CRYPTO_CRYPTODEV_SOFTWARE_CRYPTO
+
+/* cryptodev passes ECC integers little-endian while ecc.c works on
+ * big-endian bytes, so the same byte-reversing copy serves both directions.
+ * A short operand is missing its high bytes, which are zero-filled.
+ */
+
+static int swcr_ecc_copy(FAR void *dst, FAR const void *src, size_t len)
+{
+  if (src == NULL || len == 0 || len > secp256r1)
+    {
+      return -EINVAL;
+    }
+
+  memset(dst, 0, secp256r1);
+  memcpy(dst, src, len);
+  swcr_reverse(dst, secp256r1);
+  return OK;
+}
+
+#endif /* CONFIG_CRYPTO_CRYPTODEV_SOFTWARE_CRYPTO */
+
 #ifdef CONFIG_CRYPTO_CRYPTODEV_SOFTWARE_KEYMGMT
 
 /* key data operations in flash */
@@ -597,6 +636,10 @@ static int swkey_gen_secp256r1_key(FAR struct 
swkey_context_s *ctx,
       return ret;
     }
 
+  swcr_reverse(priv, secp256r1);
+  swcr_reverse(pub, secp256r1);
+  swcr_reverse(pub + secp256r1, secp256r1);
+
   /* Private keys cannot be exported */
 
   ret = swkey_write(&ctx->file, priv_keyid, priv, secp256r1,
@@ -2389,23 +2432,6 @@ int swcr_pbkdf2(FAR struct cryptop *crp,
   return 0;
 }
 
-/* Reverse a byte string in place: cryptodev carries RSA integers in
- * little-endian order, crypto/rsa.c in big-endian.
- */
-
-static void swcr_rsa_reverse(FAR uint8_t *buf, size_t len)
-{
-  uint8_t tmp;
-  size_t i;
-
-  for (i = 0; i < len / 2; i++)
-    {
-      tmp = buf[i];
-      buf[i] = buf[len - 1 - i];
-      buf[len - 1 - i] = tmp;
-    }
-}
-
 /* Load krp_param[slot] into rsa->comp[comp], big-endian.  The component
  * is required and must fit RSA_MAX_BYTES.
  */
@@ -2433,7 +2459,7 @@ static int swcr_rsa_load(FAR struct cryptkop *krp,
     }
 
   memcpy(rsa->comp[comp], param->crp_p, len);
-  swcr_rsa_reverse(rsa->comp[comp], len);
+  swcr_reverse(rsa->comp[comp], len);
   rsa->len[comp] = len;
   return OK;
 }
@@ -2498,7 +2524,7 @@ static int swcr_rsa_run(FAR struct cryptkop *krp, int 
eslot, int nslot,
 
   x = rsa->comp[SWCR_RSA_X];
   memcpy(x, in, MIN(inlen, modlen));
-  swcr_rsa_reverse(x, modlen);
+  swcr_reverse(x, modlen);
 
   ret = rsa_public(x, modlen, &key);
   if (ret < 0)
@@ -2506,7 +2532,7 @@ static int swcr_rsa_run(FAR struct cryptkop *krp, int 
eslot, int nslot,
       goto out;
     }
 
-  swcr_rsa_reverse(x, modlen);
+  swcr_reverse(x, modlen);
   memcpy(out, x, MIN(outlen, modlen));
   if (outlen > modlen)
     {
@@ -2602,11 +2628,19 @@ int swcr_rsa_verify(FAR struct cryptkop *krp)
                      sig->crp_nbits / 8, em, RSA_MAX_BYTES);
   if (ret >= 0)
     {
-      ret = -EINVAL;
-      if (hash->crp_p != NULL && padding->crp_p != NULL &&
-          hashlen + padlen <= RSA_MAX_BYTES &&
-          timingsafe_bcmp(em, hash->crp_p, hashlen) == 0 &&
-          timingsafe_bcmp(em + hashlen, padding->crp_p, padlen) == 0)
+      if (hash->crp_p == NULL || padding->crp_p == NULL ||
+          hashlen + padlen > RSA_MAX_BYTES)
+        {
+          ret = -EINVAL;
+        }
+      else if (timingsafe_bcmp(em, hash->crp_p, hashlen) != 0 ||
+               timingsafe_bcmp(em + hashlen, padding->crp_p, padlen) != 0)
+        {
+          /* Not the signed hash: a bad signature, not a bad request */
+
+          ret = -EBADMSG;
+        }
+      else
         {
           ret = OK;
         }
@@ -2627,43 +2661,102 @@ static int swcr_ecc256_genkey(FAR struct cryptkop *krp)
       return -EINVAL;
     }
 
-  memcpy(krp->krp_param[0].crp_p, d, secp256r1);
-  memcpy(krp->krp_param[1].crp_p, x, secp256r1);
-  memcpy(krp->krp_param[2].crp_p, y, secp256r1);
+  swcr_ecc_copy(krp->krp_param[0].crp_p, d, secp256r1);
+  swcr_ecc_copy(krp->krp_param[1].crp_p, x, secp256r1);
+  swcr_ecc_copy(krp->krp_param[2].crp_p, y, secp256r1);
+  explicit_bzero(d, sizeof(d));
   return OK;
 }
 
-static int swcr_ecc256_sign(struct cryptkop *krp)
+static int swcr_ecc256_sign(FAR struct cryptkop *krp)
 {
-  uint8_t *d = (uint8_t *)krp->krp_param[0].crp_p;
-  uint8_t *hash = (uint8_t *)krp->krp_param[1].crp_p;
   uint8_t sig[secp256r1 * 2];
+  uint8_t hash[secp256r1];
+  uint8_t d[secp256r1];
+  int ret;
+
+  ret = swcr_ecc_copy(d, krp->krp_param[0].crp_p,
+                      krp->krp_param[0].crp_nbits / 8);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = swcr_ecc_copy(hash, krp->krp_param[1].crp_p,
+                      krp->krp_param[1].crp_nbits / 8);
+  if (ret < 0)
+    {
+      goto out;
+    }
 
   if (ecdsa_sign(d, hash, sig) == 0)
     {
-      return -EINVAL;
+      ret = -EINVAL;
+      goto out;
     }
 
-  memcpy(krp->krp_param[2].crp_p, sig, secp256r1);
-  memcpy(krp->krp_param[3].crp_p, sig + secp256r1, secp256r1);
-  return OK;
+  swcr_ecc_copy(krp->krp_param[2].crp_p, sig, secp256r1);
+  swcr_ecc_copy(krp->krp_param[3].crp_p, sig + secp256r1, secp256r1);
+
+out:
+  explicit_bzero(d, sizeof(d));
+  return ret;
 }
 
-static int swcr_ecc256_verify(struct cryptkop *krp)
+static int swcr_ecc256_verify(FAR struct cryptkop *krp)
 {
-  uint8_t *x = (uint8_t *)krp->krp_param[0].crp_p;
-  uint8_t *y = (uint8_t *)krp->krp_param[1].crp_p;
-  uint8_t *r = (uint8_t *)krp->krp_param[3].crp_p;
-  uint8_t *s = (uint8_t *)krp->krp_param[4].crp_p;
-  uint8_t *hash = (uint8_t *)krp->krp_param[5].crp_p;
   uint8_t publickey[secp256r1 + 1];
   uint8_t signature[secp256r1 * 2];
+  uint8_t hash[secp256r1];
+  uint8_t x[secp256r1];
+  uint8_t y[secp256r1];
+  int ret;
+
+  ret = swcr_ecc_copy(x, krp->krp_param[0].crp_p,
+                      krp->krp_param[0].crp_nbits / 8);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = swcr_ecc_copy(y, krp->krp_param[1].crp_p,
+                      krp->krp_param[1].crp_nbits / 8);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = swcr_ecc_copy(signature, krp->krp_param[3].crp_p,
+                      krp->krp_param[3].crp_nbits / 8);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = swcr_ecc_copy(signature + secp256r1, krp->krp_param[4].crp_p,
+                      krp->krp_param[4].crp_nbits / 8);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = swcr_ecc_copy(hash, krp->krp_param[5].crp_p,
+                      krp->krp_param[5].crp_nbits / 8);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Compressed public key: SEC1 prefix (0x02/0x03 by Y parity) + X */
 
-  memcpy(publickey + 1, x, secp256r1);
   publickey[0] = 2 + (y[secp256r1 - 1] & 0x01);
-  memcpy(signature, r, secp256r1);
-  memcpy(signature + secp256r1, s, secp256r1);
-  return ecdsa_verify(publickey, hash, signature) == 0;
+  memcpy(publickey + 1, x, secp256r1);
+  if (ecdsa_verify(publickey, hash, signature) == 0)
+    {
+      return -EBADMSG;
+    }
+
+  return OK;
 }
 
 int swcr_kprocess(struct cryptkop *krp)

Reply via email to