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 33f3dce58e1921cfc8ffce04752c6ac265f938e7
Author: makejian <[email protected]>
AuthorDate: Tue Nov 10 15:49:40 2026 +0800

    testing/crypto: add CAST-128 CBC test
    
    Known-answer test for the cryptodev CAST CBC cipher, using the three
    key-length examples of RFC 2144 appendix B.1 (128, 80 and 40 bit
    keys) run as one CBC block with an all-zero IV.  Each vector is
    verified in both the encrypt and the decrypt direction.
    
    Signed-off-by: makejian <[email protected]>
---
 testing/drivers/crypto/CMakeLists.txt |  14 ++
 testing/drivers/crypto/Kconfig        |   4 +
 testing/drivers/crypto/Makefile       |   5 +
 testing/drivers/crypto/castcbc.c      | 248 ++++++++++++++++++++++++++++++++++
 4 files changed, 271 insertions(+)

diff --git a/testing/drivers/crypto/CMakeLists.txt 
b/testing/drivers/crypto/CMakeLists.txt
index 0789e902f..9ef95ec69 100644
--- a/testing/drivers/crypto/CMakeLists.txt
+++ b/testing/drivers/crypto/CMakeLists.txt
@@ -231,4 +231,18 @@ if(CONFIG_TESTING_CRYPTO)
       poly1305.c)
   endif()
 
+  if(CONFIG_TESTING_CRYPTO_CAST_CBC)
+    nuttx_add_application(
+      NAME
+      castcbc
+      PRIORITY
+      ${CONFIG_TESTING_CRYPTO_PRIORITY}
+      STACKSIZE
+      ${CONFIG_TESTING_CRYPTO_STACKSIZE}
+      MODULE
+      ${CONFIG_TESTING_CRYPTO}
+      SRCS
+      castcbc.c)
+  endif()
+
 endif()
diff --git a/testing/drivers/crypto/Kconfig b/testing/drivers/crypto/Kconfig
index 3d176cc67..b41a1ea2a 100644
--- a/testing/drivers/crypto/Kconfig
+++ b/testing/drivers/crypto/Kconfig
@@ -136,4 +136,8 @@ config TESTING_CRYPTO_POLY1305
        bool "poly1305 crypto test"
        default n
 
+config TESTING_CRYPTO_CAST_CBC
+       bool "cast-cbc crypto test"
+       default n
+
 endif # TESTING_CRYPTO
diff --git a/testing/drivers/crypto/Makefile b/testing/drivers/crypto/Makefile
index cb7d7ca80..69e9467f2 100644
--- a/testing/drivers/crypto/Makefile
+++ b/testing/drivers/crypto/Makefile
@@ -98,6 +98,11 @@ PROGNAME += poly1305
 MAINSRC  += poly1305.c
 endif
 
+ifeq ($(CONFIG_TESTING_CRYPTO_CAST_CBC),y)
+PROGNAME += castcbc
+MAINSRC  += castcbc.c
+endif
+
 PRIORITY = $(CONFIG_TESTING_CRYPTO_PRIORITY)
 STACKSIZE = $(CONFIG_TESTING_CRYPTO_STACKSIZE)
 MODULE = $(CONFIG_TESTING_CRYPTO)
diff --git a/testing/drivers/crypto/castcbc.c b/testing/drivers/crypto/castcbc.c
new file mode 100644
index 000000000..897aa8dd0
--- /dev/null
+++ b/testing/drivers/crypto/castcbc.c
@@ -0,0 +1,248 @@
+/****************************************************************************
+ * apps/testing/drivers/crypto/castcbc.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <err.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <sys/param.h>
+#include <crypto/cryptodev.h>
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct tb
+{
+  FAR const char *key;
+  size_t keylen;
+  FAR const char *iv;
+  FAR const char *plain;
+  FAR const char *cipher;
+  size_t len;
+};
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* CAST-128 vectors are taken from RFC 2144 Appendix B.1, run as one CBC
+ * block with an all-zero IV.  Keys of 80 bits or less use 12 rounds.
+ */
+
+static const struct tb g_testcase[] =
+{
+  {
+    /* RFC 2144 B.1, 128-bit key */
+
+    "\x01\x23\x45\x67\x12\x34\x56\x78\x23\x45\x67\x89\x34\x56\x78\x9a",
+    16,
+    "\x00\x00\x00\x00\x00\x00\x00\x00",
+    "\x01\x23\x45\x67\x89\xab\xcd\xef",
+    "\x23\x8b\x4f\xe5\x84\x7e\x44\xb2",
+    8
+  },
+  {
+    /* RFC 2144 B.1, 80-bit key */
+
+    "\x01\x23\x45\x67\x12\x34\x56\x78\x23\x45",
+    10,
+    "\x00\x00\x00\x00\x00\x00\x00\x00",
+    "\x01\x23\x45\x67\x89\xab\xcd\xef",
+    "\xeb\x6a\x71\x1a\x2c\x02\x27\x1b",
+    8
+  },
+  {
+    /* RFC 2144 B.1, 40-bit key */
+
+    "\x01\x23\x45\x67\x12",
+    5,
+    "\x00\x00\x00\x00\x00\x00\x00\x00",
+    "\x01\x23\x45\x67\x89\xab\xcd\xef",
+    "\x7a\xc8\x16\xd1\x6e\x9b\x30\x2e",
+    8
+  }
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int syscrypt(FAR const char *key, size_t klen,
+                    FAR const char *iv, FAR const char *in,
+                    FAR unsigned char *out, size_t len, int encrypt)
+{
+  struct session_op session;
+  struct crypt_op cryp;
+  int cryptodev_fd = -1;
+  int fd = -1;
+  char tmp_iv[CAST128_BLOCK_LEN];
+
+  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_CAST_CBC;
+  session.key = (caddr_t)key;
+  session.op = encrypt ? COP_ENCRYPT : COP_DECRYPT;
+  session.keylen = klen;
+  if (ioctl(cryptodev_fd, CIOCGSESSION, &session) == -1)
+    {
+      warn("CIOCGSESSION");
+      goto err;
+    }
+
+  memset(&cryp, 0, sizeof(cryp));
+  memcpy(tmp_iv, iv, CAST128_BLOCK_LEN);
+  cryp.ses = session.ses;
+  cryp.op = encrypt ? COP_ENCRYPT : COP_DECRYPT;
+  cryp.flags = 0;
+  cryp.mac = 0;
+  cryp.ivlen = CAST128_BLOCK_LEN;
+  cryp.iv = (caddr_t)tmp_iv;
+  cryp.len = len;
+  cryp.olen = len;
+  cryp.src = (caddr_t)in;
+  cryp.dst = (caddr_t)out;
+  if (ioctl(cryptodev_fd, CIOCCRYPT, &cryp) == -1)
+    {
+      warn("CIOCCRYPT");
+      goto err;
+    }
+
+  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 const unsigned char *a, FAR const unsigned char *b,
+                 size_t len, int idx)
+{
+  size_t i;
+
+  if (memcmp(a, b, len) == 0)
+    {
+      return 0;
+    }
+
+  warnx("vector %d: mismatch", idx);
+
+  for (i = 0; i < len; i++)
+    {
+      printf("%2.2x", a[i]);
+    }
+
+  printf("\n");
+  for (i = 0; i < len; i++)
+    {
+      printf("%2.2x", b[i]);
+    }
+
+  printf("\n");
+
+  return -1;
+}
+
+static int check_vector(FAR const struct tb *t, int idx)
+{
+  unsigned char out[8];
+
+  if (syscrypt(t->key, t->keylen, t->iv, t->plain, out, t->len, 1) != 0 ||
+      match(out, (FAR const unsigned char *)t->cipher, t->len, idx) != 0)
+    {
+      return -1;
+    }
+
+  if (syscrypt(t->key, t->keylen, t->iv, t->cipher, out, t->len, 0) != 0 ||
+      match(out, (FAR const unsigned char *)t->plain, t->len, idx) != 0)
+    {
+      return -1;
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int main(int argc, FAR char *argv[])
+{
+  int fail = 0;
+  int i;
+
+  for (i = 0; i < nitems(g_testcase); i++)
+    {
+      if (check_vector(&g_testcase[i], i) != 0)
+        {
+          fail++;
+        }
+      else
+        {
+          printf("castcbc test vector %d ok\n", i);
+        }
+    }
+
+  if (fail)
+    {
+      printf("castcbc: %d test(s) failed\n", fail);
+    }
+  else
+    {
+      printf("castcbc: all tests passed\n");
+    }
+
+  return fail;
+}

Reply via email to