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

commit fc8787eb36c2127c5903a3fab4cdd18ec4746bdd
Author: makejian <[email protected]>
AuthorDate: Tue Nov 17 21:02:33 2026 +0800

    testing/crypto: add stream mode test to aesctr
    
    Run every RFC 3686 vector a second time feeding 8 bytes per CIOCCRYPT
    chained with COP_FLAG_UPDATE. 8-byte chunks end mid-keystream-block, so
    matching the one-shot result verifies that the unused keystream tail is
    kept in the session key schedule.
    
    Signed-off-by: makejian <[email protected]>
---
 testing/drivers/crypto/aesctr.c | 94 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 94 insertions(+)

diff --git a/testing/drivers/crypto/aesctr.c b/testing/drivers/crypto/aesctr.c
index 25bbc8600..087304ab8 100644
--- a/testing/drivers/crypto/aesctr.c
+++ b/testing/drivers/crypto/aesctr.c
@@ -242,6 +242,89 @@ err:
   return (-1);
 }
 
+/* Half a block per CIOCCRYPT, chained with COP_FLAG_UPDATE; the result
+ * must match the one-shot run above.
+ */
+
+static int syscrypt_stream(FAR const unsigned char *key, size_t klen,
+                           FAR const unsigned char *iv,
+                           FAR const unsigned char *in,
+                           FAR unsigned char *out, size_t len, int encrypt)
+{
+  struct session_op session;
+  struct crypt_op cryp;
+  size_t offset = 0;
+  int cryptodev_fd = -1;
+  int fd = -1;
+
+  if ((fd = open("/dev/crypto", O_RDWR, 0)) < 0)
+    {
+      warn("/dev/crypto");
+      goto err;
+    }
+
+  if (ioctl(fd, CRIOGET, &cryptodev_fd) == -1)
+    {
+      warn("CRIOGET");
+      goto err;
+    }
+
+  memset(&session, 0, sizeof(session));
+  session.cipher = CRYPTO_AES_CTR;
+  session.key = (caddr_t) key;
+  session.keylen = klen;
+  if (ioctl(cryptodev_fd, CIOCGSESSION, &session) == -1)
+    {
+      warn("CIOCGSESSION");
+      goto err;
+    }
+
+  memset(&cryp, 0, sizeof(cryp));
+  cryp.ses = session.ses;
+  cryp.op = encrypt ? COP_ENCRYPT : COP_DECRYPT;
+  cryp.flags = 0;
+  cryp.mac = 0;
+  cryp.iv = (caddr_t) iv;
+  while (len > 0)
+    {
+      cryp.len = MIN(len, 8);
+      cryp.src = (caddr_t) in + offset;
+      cryp.dst = (caddr_t) out + offset;
+      if (ioctl(cryptodev_fd, CIOCCRYPT, &cryp) == -1)
+        {
+          warn("CIOCCRYPT");
+          goto err;
+        }
+
+      cryp.flags |= COP_FLAG_UPDATE;
+      len -= cryp.len;
+      offset += cryp.len;
+    }
+
+  if (ioctl(cryptodev_fd, CIOCFSESSION, &session.ses) == -1)
+    {
+      warn("CIOCFSESSION");
+      goto err;
+    }
+
+  close(cryptodev_fd);
+  close(fd);
+  return (0);
+
+err:
+  if (cryptodev_fd != -1)
+    {
+      close(cryptodev_fd);
+    }
+
+  if (fd != -1)
+    {
+      close(fd);
+    }
+
+  return (-1);
+}
+
 static int match(FAR unsigned char *a, FAR unsigned char *b, size_t len)
 {
   int i;
@@ -332,6 +415,17 @@ static int run(int num)
 
   fail = !match(data[TST_CIPHER], p, len);
   printf("%s test vector %d\n", fail ? "FAILED" : "OK", num);
+
+  if (syscrypt_stream(data[TST_KEY], length[TST_KEY],
+      data[TST_IV], data[TST_PLAIN], p,
+      length[TST_PLAIN], 0) < 0)
+    {
+      warnx("crypt with /dev/crypto failed");
+      goto done;
+    }
+
+  fail = !match(data[TST_CIPHER], p, len);
+  printf("%s test vector %d stream mode\n", fail ? "FAILED" : "OK", num);
 done:
   for (i = 0; i < TST_NUM; i++)
     {

Reply via email to