You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2024/09/13 15:10:23 UTC

(nuttx-apps) branch master updated (524caec5a -> 7cd9919a4)

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


    from 524caec5a nxcodec: Modify the logic of judging the correctness of the incoming pixformat
     new 6db82199a testing/crypto: add crc32 testcases
     new b53ee36d4 testing/crypto: add aes-128-cmac testcase
     new ce58e85aa testing/crypto: add ecdsa testing
     new b1f2e63a6 testing/crypto: Add test cases for Diffie-Hellman algorithm
     new 7cd9919a4 testing/crypto: add rsa testcase

The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 testing/crypto/CMakeLists.txt |  70 ++++++++
 testing/crypto/Kconfig        |  20 +++
 testing/crypto/Makefile       |  25 +++
 testing/crypto/aescmac.c      | 220 +++++++++++++++++++++++
 testing/crypto/crc32.c        | 298 +++++++++++++++++++++++++++++++
 testing/crypto/dhm.c          | 310 ++++++++++++++++++++++++++++++++
 testing/crypto/ecdsa.c        | 326 ++++++++++++++++++++++++++++++++++
 testing/crypto/rsa.c          | 402 ++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 1671 insertions(+)
 create mode 100644 testing/crypto/aescmac.c
 create mode 100644 testing/crypto/crc32.c
 create mode 100644 testing/crypto/dhm.c
 create mode 100644 testing/crypto/ecdsa.c
 create mode 100644 testing/crypto/rsa.c


(nuttx-apps) 05/05: testing/crypto: add rsa testcase

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit 7cd9919a46c4c550fe400ff44584e988181375aa
Author: makejian <ma...@xiaomi.com>
AuthorDate: Wed Aug 7 21:58:52 2024 +0800

    testing/crypto: add rsa testcase
    
    Signed-off-by: makejian <ma...@xiaomi.com>
---
 testing/crypto/CMakeLists.txt |  14 ++
 testing/crypto/Kconfig        |   4 +
 testing/crypto/Makefile       |   5 +
 testing/crypto/rsa.c          | 402 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 425 insertions(+)

diff --git a/testing/crypto/CMakeLists.txt b/testing/crypto/CMakeLists.txt
index 36f07b63a..84b0f008d 100644
--- a/testing/crypto/CMakeLists.txt
+++ b/testing/crypto/CMakeLists.txt
@@ -159,4 +159,18 @@ if(CONFIG_TESTING_CRYPTO)
       dhm.c)
   endif()
 
+  if(CONFIG_TESTING_CRYPTO_RSA)
+    nuttx_add_application(
+      NAME
+      rsa
+      PRIORITY
+      ${CONFIG_TESTING_CRYPTO_PRIORITY}
+      STACKSIZE
+      ${CONFIG_TESTING_CRYPTO_STACKSIZE}
+      MODULE
+      ${CONFIG_TESTING_CRYPTO}
+      SRCS
+      rsa.c)
+  endif()
+
 endif()
diff --git a/testing/crypto/Kconfig b/testing/crypto/Kconfig
index 9bf14ed1e..c07a557d0 100644
--- a/testing/crypto/Kconfig
+++ b/testing/crypto/Kconfig
@@ -46,6 +46,10 @@ config TESTING_CRYPTO_ECDH
 	bool "ecdh crypto test"
 	default n
 
+config TESTING_CRYPTO_RSA
+	bool "rsa crypto test"
+	default n
+
 config TESTING_CRYPTO_PRIORITY
 	int "crypto test task priority"
 	default 100
diff --git a/testing/crypto/Makefile b/testing/crypto/Makefile
index 950428e83..00a1ab03e 100644
--- a/testing/crypto/Makefile
+++ b/testing/crypto/Makefile
@@ -71,6 +71,11 @@ PROGNAME += dhm
 MAINSRC +=  dhm.c
 endif
 
+ifeq ($(CONFIG_TESTING_CRYPTO_RSA),y)
+PROGNAME += rsa
+MAINSRC +=  rsa.c
+endif
+
 PRIORITY = $(CONFIG_TESTING_CRYPTO_PRIORITY)
 STACKSIZE = $(CONFIG_TESTING_CRYPTO_STACKSIZE)
 MODULE = $(CONFIG_TESTING_CRYPTO)
diff --git a/testing/crypto/rsa.c b/testing/crypto/rsa.c
new file mode 100644
index 000000000..32a93866b
--- /dev/null
+++ b/testing/crypto/rsa.c
@@ -0,0 +1,402 @@
+/****************************************************************************
+ * apps/testing/crypto/rsa.c
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/param.h>
+#include <sys/endian.h>
+#include <crypto/cryptodev.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MOD_LEN        128
+#define PRIVATE_KEYLEN 128
+#define PUBLIC_KEYLEN  5
+#define PLAIN_LEN      24
+#define PADDING_LEN    (MOD_LEN - PLAIN_LEN)
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+typedef struct rsa_data_s
+{
+  unsigned char e[PUBLIC_KEYLEN];
+  unsigned char d[PRIVATE_KEYLEN];
+  unsigned char n[MOD_LEN];
+}
+rsa_data_t;
+
+typedef struct crypto_context_s
+{
+  int fd;
+  int cryptodev_fd;
+}
+crypto_context_t;
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* Example RSA-1024 keypair, for test purposes */
+
+static const unsigned char g_rsa_n[] =
+"9292758453063D803DD603D5E777D788"
+"8ED1D5BF35786190FA2F23EBC0848AEA"
+"DDA92CA6C3D80B32C4D109BE0F36D6AE"
+"7130B9CED7ACDF54CFC7555AC14EEBAB"
+"93A89813FBF3C4F8066D2D800F7C38A8"
+"1AE31942917403FF4946B0A83D3D3E05"
+"EE57C6F5F5606FB5D4BC6CD34EE0801A"
+"5E94BB77B07507233A0BC7BAC8F90F79";
+
+static const unsigned char g_rsa_e[] = "10001";
+
+static const unsigned char g_rsa_d[] =
+"24BF6185468786FDD303083D25E64EFC"
+"66CA472BC44D253102F8B4A9D3BFA750"
+"91386C0077937FE33FA3252D28855837"
+"AE1B484A8A9A45F7EE8C0C634F99E8CD"
+"DF79C5CE07EE72C7F123142198164234"
+"CABB724CF78B8173B9F880FC86322407"
+"AF1FEDFDDE2BEB674CA15F3E81A1521E"
+"071513A1E85B5DFA031F21ECAE91A34D";
+
+static const unsigned char g_message[] =
+"\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF"
+"\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD";
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void pkcs1_v15_padding(FAR unsigned char *padding,
+                              size_t pad_len)
+{
+  int i;
+
+  /* this padding is only for rsa sign */
+
+  *padding++ = 0;
+  *padding++ = 0x02;
+  for (i = 0; i < pad_len - 3; i++)
+    {
+      *padding++ = rand() % 256;
+    }
+
+  *padding++ = 0;
+}
+
+static void rsa_be32toh(FAR uint8_t *dst, size_t len, FAR const uint8_t *src)
+{
+  int i;
+  size_t rlen = len / 4;
+  uint32_t *dst32 = (uint32_t *)dst;
+  uint32_t *src32 = (uint32_t *)src;
+
+  for (i = 0; i < rlen; i++)
+    {
+      dst32[i] = be32toh(src32[rlen - i - 1]);
+    }
+}
+
+static void mpi_get_digit(FAR uint8_t *d, char c)
+{
+  if (c >= '0' && c <= '9')
+    {
+      *d = c - '0';
+    }
+
+  if (c >= 'A' && c <= 'F')
+    {
+      *d = c - 'A' + 10;
+    }
+
+  if (c >= 'a' && c <= 'f')
+    {
+      *d = c - 'a' + 10;
+    }
+}
+
+static void mpi_from_string(FAR unsigned char *buf,
+                            FAR const unsigned char *s,
+                            size_t slen)
+{
+  int i;
+  int j;
+  uint8_t d = 0;
+
+  for (i = slen, j = 0; i > 0; i--, j++)
+    {
+      mpi_get_digit(&d, s[i - 1]);
+      buf[j / 2] |= d << ((j % 2) << 2);
+    }
+}
+
+static int rsa_data_init(FAR rsa_data_t *data,
+                         FAR const unsigned char *e, size_t elen,
+                         FAR const unsigned char *d, size_t dlen,
+                         FAR const unsigned char *n, size_t nlen)
+{
+  memset(data, 0, sizeof(rsa_data_t));
+  mpi_from_string(data->e, e, elen);
+  mpi_from_string(data->d, d, dlen);
+  mpi_from_string(data->n, n, nlen);
+  return 0;
+}
+
+static void cryptodev_free(FAR crypto_context_t *ctx)
+{
+  if (ctx->cryptodev_fd != 0)
+    {
+      close(ctx->cryptodev_fd);
+      ctx->cryptodev_fd = 0;
+    }
+
+  if (ctx->fd != 0)
+    {
+      close(ctx->fd);
+      ctx->fd = 0;
+    }
+}
+
+static int cryptodev_init(FAR crypto_context_t *ctx)
+{
+  memset(ctx, 0, sizeof(crypto_context_t));
+  if ((ctx->fd = open("/dev/crypto", O_RDWR, 0)) < 0)
+    {
+      perror("CRIOGET");
+      cryptodev_free(ctx);
+      return 1;
+    }
+
+  if (ioctl(ctx->fd, CRIOGET, &ctx->cryptodev_fd) == -1)
+    {
+      perror("CRIOGET");
+      cryptodev_free(ctx);
+      return 1;
+    }
+
+  return 0;
+}
+
+static int cryptodev_rsa_calc(FAR crypto_context_t *ctx,
+                              FAR rsa_data_t *data,
+                              FAR const unsigned char *input, size_t ilen,
+                              FAR const unsigned char *output, size_t olen,
+                              bool is_priv)
+{
+  struct crypt_kop cryptk;
+
+  memset(&cryptk, 0, sizeof(struct crypt_kop));
+  cryptk.crk_op = CRK_MOD_EXP;
+  cryptk.crk_iparams = 3;
+  cryptk.crk_oparams = 1;
+  cryptk.crk_param[0].crp_p = (caddr_t)input;
+  cryptk.crk_param[0].crp_nbits = ilen * 8;
+  if (is_priv)
+    {
+      cryptk.crk_param[1].crp_p = (caddr_t)data->d;
+      cryptk.crk_param[1].crp_nbits = sizeof(data->d) * 8;
+    }
+  else
+    {
+      cryptk.crk_param[1].crp_p = (caddr_t)data->e;
+      cryptk.crk_param[1].crp_nbits = sizeof(data->e) * 8;
+    }
+
+  cryptk.crk_param[2].crp_p = (caddr_t)data->n;
+  cryptk.crk_param[2].crp_nbits = sizeof(data->n) * 8;
+  cryptk.crk_param[3].crp_p = (caddr_t)output;
+  cryptk.crk_param[3].crp_nbits = olen * 8;
+
+  if (ioctl(ctx->cryptodev_fd, CIOCKEY, &cryptk) == -1)
+    {
+      perror("rsa calculate failed");
+      return -1;
+    }
+
+  return cryptk.crk_status;
+}
+
+static int cryptodev_rsa_verify(
+            FAR crypto_context_t *ctx, FAR rsa_data_t *data,
+            FAR const unsigned char *sig, size_t sig_len,
+            FAR const unsigned char *hash, size_t hash_len,
+            FAR const unsigned char *padding, size_t pad_len)
+{
+  struct crypt_kop cryptk;
+
+  memset(&cryptk, 0, sizeof(struct crypt_kop));
+  cryptk.crk_op = CRK_RSA_PKCS15_VERIFY;
+  cryptk.crk_iparams = 5;
+  cryptk.crk_oparams = 0;
+
+  cryptk.crk_param[0].crp_p = (caddr_t)data->e;
+  cryptk.crk_param[0].crp_nbits = sizeof(data->e) * 8;
+  cryptk.crk_param[1].crp_p = (caddr_t)data->n;
+  cryptk.crk_param[1].crp_nbits = sizeof(data->n) * 8;
+  cryptk.crk_param[2].crp_p = (caddr_t)sig;
+  cryptk.crk_param[2].crp_nbits = sig_len * 8;
+  cryptk.crk_param[3].crp_p = (caddr_t)hash;
+  cryptk.crk_param[3].crp_nbits = hash_len * 8;
+  cryptk.crk_param[4].crp_p = (caddr_t)padding;
+  cryptk.crk_param[4].crp_nbits = pad_len * 8;
+
+  if (ioctl(ctx->cryptodev_fd, CIOCKEY, &cryptk) == -1)
+    {
+      perror("CIOCKEY");
+      return -1;
+    }
+
+  return cryptk.crk_status;
+}
+
+static int rsa_test(void)
+{
+  crypto_context_t ctx;
+  rsa_data_t rsa_data;
+  unsigned char cipher[MOD_LEN];
+  unsigned char plain[PLAIN_LEN];
+  unsigned char data[MOD_LEN];
+  unsigned char padding[PADDING_LEN];
+  unsigned char sig[MOD_LEN];
+  unsigned char data_he[MOD_LEN];
+  unsigned char pad_he[PADDING_LEN];
+  unsigned char hash_he[MOD_LEN];
+  int ret = 0;
+
+  ret = cryptodev_init(&ctx);
+  if (ret != 0)
+    {
+      goto free;
+    }
+
+  ret = rsa_data_init(&rsa_data, g_rsa_e, sizeof(g_rsa_e) - 1,
+                      g_rsa_d, sizeof(g_rsa_d) - 1,
+                      g_rsa_n, sizeof(g_rsa_n) - 1);
+  if (ret != 0)
+    {
+      goto free;
+    }
+
+  /* test 1: encrypt with private key and decrypt with public key */
+
+  memset(cipher, 0, MOD_LEN);
+  memset(plain, 0, PLAIN_LEN);
+
+  /* encrypt with private key: g_message ^ ctx.data.d % n = cipher */
+
+  ret = cryptodev_rsa_calc(&ctx, &rsa_data, g_message, PLAIN_LEN,
+                           cipher, MOD_LEN, TRUE);
+  if (ret != 0)
+    {
+      printf("rsa encrypt with private key failed\n");
+      goto free;
+    }
+
+  /* dencrypt with public key: cipher ^ ctx.data.e % n = plain */
+
+  ret = cryptodev_rsa_calc(&ctx, &rsa_data, cipher, MOD_LEN, plain,
+                           PLAIN_LEN, FALSE);
+  if (ret != 0)
+    {
+      printf("rsa decrypt with pulic key failed\n");
+      goto free;
+    }
+
+  ret = memcmp(g_message, plain, PLAIN_LEN);
+  if (ret != 0)
+    {
+      printf("rsa test case 1 failed\n");
+      goto free;
+    }
+  else
+    {
+      printf("rsa test case 1 success\n");
+    }
+
+  /* test 2: rsa sign and verify */
+
+  memset(data, 0, MOD_LEN);
+  memset(padding, 0, PADDING_LEN);
+  memset(sig, 0, MOD_LEN);
+
+  /* pkcs15 v1 padding for sign: 0x00 0x02 (... random) 0x00 */
+
+  pkcs1_v15_padding(padding, PADDING_LEN);
+  memcpy(data, padding, PADDING_LEN);
+  memcpy(data + PADDING_LEN, g_message, PLAIN_LEN);
+
+  rsa_be32toh(data_he, MOD_LEN, data);
+  rsa_be32toh(pad_he, PADDING_LEN, padding);
+  rsa_be32toh(hash_he, PLAIN_LEN, g_message);
+
+  /* sign with private key: (hash + padding) ^ d % n = sig */
+
+  ret = cryptodev_rsa_calc(&ctx, &rsa_data, data_he, MOD_LEN, sig,
+                           MOD_LEN, TRUE);
+  if (ret != 0)
+    {
+      printf("rsa sign failed\n");
+      goto free;
+    }
+
+  /* verify with public key: sig ^ e % n = (hash + padding) */
+
+  ret = cryptodev_rsa_verify(&ctx, &rsa_data, sig, MOD_LEN, hash_he,
+                             PLAIN_LEN, pad_he, PADDING_LEN);
+  if (ret != 0)
+    {
+      printf("rsa verify failed\n");
+    }
+  else
+    {
+      printf("rsa test case 2 success\n");
+    }
+
+free:
+  cryptodev_free(&ctx);
+  return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int main(void)
+{
+  if (rsa_test() == 0)
+    {
+      printf("rsa test success\n");
+    }
+  else
+    {
+      printf("rsa test failed\n");
+    }
+
+  return 0;
+}


(nuttx-apps) 02/05: testing/crypto: add aes-128-cmac testcase

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit b53ee36d479b3b0397c447d38e01b8db1e45e832
Author: makejian <ma...@xiaomi.com>
AuthorDate: Wed Jul 10 21:22:22 2024 +0800

    testing/crypto: add aes-128-cmac testcase
    
    Signed-off-by: makejian <ma...@xiaomi.com>
---
 testing/crypto/CMakeLists.txt |  14 +++
 testing/crypto/Kconfig        |   4 +
 testing/crypto/Makefile       |   5 +
 testing/crypto/aescmac.c      | 220 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 243 insertions(+)

diff --git a/testing/crypto/CMakeLists.txt b/testing/crypto/CMakeLists.txt
index d252d7d6a..a1c6c776f 100644
--- a/testing/crypto/CMakeLists.txt
+++ b/testing/crypto/CMakeLists.txt
@@ -117,4 +117,18 @@ if(CONFIG_TESTING_CRYPTO)
       crc32.c)
   endif()
 
+  if(CONFIG_TESTING_CRYPTO_AES_CMAC)
+    nuttx_add_application(
+      NAME
+      aescmac
+      PRIORITY
+      ${CONFIG_TESTING_CRYPTO_PRIORITY}
+      STACKSIZE
+      ${CONFIG_TESTING_CRYPTO_STACKSIZE}
+      MODULE
+      ${CONFIG_TESTING_CRYPTO}
+      SRCS
+      aescmac.c)
+  endif()
+
 endif()
diff --git a/testing/crypto/Kconfig b/testing/crypto/Kconfig
index 0ed53e6b9..485f7f15f 100644
--- a/testing/crypto/Kconfig
+++ b/testing/crypto/Kconfig
@@ -34,6 +34,10 @@ config TESTING_CRYPTO_CRC32
 	bool "crc32 crypto test"
 	default n
 
+config TESTING_CRYPTO_AES_CMAC
+	bool "aes-cmac crypto test"
+	default n
+
 config TESTING_CRYPTO_PRIORITY
 	int "crypto test task priority"
 	default 100
diff --git a/testing/crypto/Makefile b/testing/crypto/Makefile
index 30d93ee9e..d76800371 100644
--- a/testing/crypto/Makefile
+++ b/testing/crypto/Makefile
@@ -56,6 +56,11 @@ PROGNAME += crc32
 MAINSRC +=  crc32.c
 endif
 
+ifeq ($(CONFIG_TESTING_CRYPTO_AES_CMAC),y)
+PROGNAME += aescmac
+MAINSRC +=  aescmac.c
+endif
+
 PRIORITY = $(CONFIG_TESTING_CRYPTO_PRIORITY)
 STACKSIZE = $(CONFIG_TESTING_CRYPTO_STACKSIZE)
 MODULE = $(CONFIG_TESTING_CRYPTO)
diff --git a/testing/crypto/aescmac.c b/testing/crypto/aescmac.c
new file mode 100644
index 000000000..abf5cf83f
--- /dev/null
+++ b/testing/crypto/aescmac.c
@@ -0,0 +1,220 @@
+/****************************************************************************
+ * apps/testing/crypto/aescmac.c
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <err.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <sys/param.h>
+#include <crypto/cryptodev.h>
+
+/* AES-CMAC test vector from
+ * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/AES_CMAC.pdf
+ */
+
+static const unsigned char g_test_message[] =
+{
+    0x6b, 0xc1, 0xbe, 0xe2,     0x2e, 0x40, 0x9f, 0x96,
+    0xe9, 0x3d, 0x7e, 0x11,     0x73, 0x93, 0x17, 0x2a,
+    0xae, 0x2d, 0x8a, 0x57,     0x1e, 0x03, 0xac, 0x9c,
+    0x9e, 0xb7, 0x6f, 0xac,     0x45, 0xaf, 0x8e, 0x51,
+    0x30, 0xc8, 0x1c, 0x46,     0xa3, 0x5c, 0xe4, 0x11,
+    0xe5, 0xfb, 0xc1, 0x19,     0x1a, 0x0a, 0x52, 0xef,
+    0xf6, 0x9f, 0x24, 0x45,     0xdf, 0x4f, 0x9b, 0x17,
+    0xad, 0x2b, 0x41, 0x7b,     0xe6, 0x6c, 0x37, 0x10
+};
+
+static const unsigned char g_aes_128_key[16] =
+{
+    0x2b, 0x7e, 0x15, 0x16,     0x28, 0xae, 0xd2, 0xa6,
+    0xab, 0xf7, 0x15, 0x88,     0x09, 0xcf, 0x4f, 0x3c
+};
+
+static const  unsigned int g_message_lengths[4] =
+{
+    0,
+    16,
+    20,
+    64
+};
+
+static const unsigned char g_aes_128_expected_result[4][16] =
+{
+    {
+        /* Example #1 */
+
+        0xbb, 0x1d, 0x69, 0x29,     0xe9, 0x59, 0x37, 0x28,
+        0x7f, 0xa3, 0x7d, 0x12,     0x9b, 0x75, 0x67, 0x46
+    },
+    {
+        /* Example #2 */
+
+        0x07, 0x0a, 0x16, 0xb4,     0x6b, 0x4d, 0x41, 0x44,
+        0xf7, 0x9b, 0xdd, 0x9d,     0xd0, 0x4a, 0x28, 0x7c
+    },
+    {
+        /* Example #3 */
+
+        0x7d, 0x85, 0x44, 0x9e,     0xa6, 0xea, 0x19, 0xc8,
+        0x23, 0xa7, 0xbf, 0x78,     0x83, 0x7d, 0xfa, 0xde
+    },
+    {
+        /* Example #4 */
+
+        0x51, 0xf0, 0xbe, 0xbf,     0x7e, 0x3b, 0x9d, 0x92,
+        0xfc, 0x49, 0x74, 0x17,     0x79, 0x36, 0x3c, 0xfe
+    }
+};
+
+static int syscrypt(FAR const unsigned char *key, size_t klen,
+                    FAR const unsigned char *message, size_t len,
+                    FAR unsigned char *output, size_t outlen)
+{
+  struct session_op session;
+  struct crypt_op cryp;
+  int cryptodev_fd = -1;
+  int fd = -1;
+
+  if ((fd = open("/dev/crypto", O_RDWR, 0)) < 0)
+    {
+      perror("/dev/crypto");
+      goto err;
+    }
+
+  if (ioctl(fd, CRIOGET, &cryptodev_fd) == -1)
+    {
+      perror("CRIOGET");
+      goto err;
+    }
+
+  memset(&session, 0, sizeof(session));
+  session.cipher = CRYPTO_AES_CMAC;
+  session.key = (caddr_t)key;
+  session.keylen = klen;
+  session.mac = CRYPTO_AES_128_CMAC;
+  session.mackey = (caddr_t)key;
+  session.mackeylen = klen;
+
+  if (ioctl(cryptodev_fd, CIOCGSESSION, &session) == -1)
+    {
+      perror("CIOCGSESSION");
+      goto err;
+    }
+
+  memset(&cryp, 0, sizeof(cryp));
+  cryp.ses = session.ses;
+  cryp.op = COP_ENCRYPT;
+  cryp.flags = 0;
+  cryp.len = len;
+  cryp.src = (caddr_t)message;
+  cryp.mac = (caddr_t)output;
+
+  if (ioctl(cryptodev_fd, CIOCCRYPT, &cryp) == -1)
+    {
+      perror("CIOCCRYPT");
+      goto err;
+    }
+
+  if (ioctl(cryptodev_fd, CIOCFSESSION, &session.ses) == -1)
+    {
+      perror("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 i;
+
+  if (memcmp(a, b, len) == 0)
+    {
+      return 0;
+    }
+
+  warnx("ciphertext mismatch");
+
+  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;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int main(int argc, FAR char *argv[])
+{
+  int ret;
+  unsigned char out[64];
+
+  for (int i = 0; i < 4; i++)
+    {
+      ret = syscrypt(g_aes_128_key, sizeof(g_aes_128_key), g_test_message,
+                     g_message_lengths[i], out, sizeof(out));
+      if (ret)
+        {
+          printf("aes cmac failed in testcase:%d\n", i);
+          return -1;
+        }
+
+      ret = match(out, g_aes_128_expected_result[i], 16);
+      if (ret)
+        {
+          printf("aes cmac failed in testcase:%d\n", i);
+          return -1;
+        }
+
+      printf("aescbc test %d ok\n", i);
+    }
+
+  return 0;
+}


(nuttx-apps) 03/05: testing/crypto: add ecdsa testing

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit ce58e85aa66849b81b2c575c41a130df6dbc985e
Author: makejian <ma...@xiaomi.com>
AuthorDate: Wed Jul 24 21:46:07 2024 +0800

    testing/crypto: add ecdsa testing
    
    Signed-off-by: makejian <ma...@xiaomi.com>
---
 testing/crypto/CMakeLists.txt |  14 ++
 testing/crypto/Kconfig        |   4 +
 testing/crypto/Makefile       |   5 +
 testing/crypto/ecdsa.c        | 326 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 349 insertions(+)

diff --git a/testing/crypto/CMakeLists.txt b/testing/crypto/CMakeLists.txt
index a1c6c776f..06c4f4d7f 100644
--- a/testing/crypto/CMakeLists.txt
+++ b/testing/crypto/CMakeLists.txt
@@ -131,4 +131,18 @@ if(CONFIG_TESTING_CRYPTO)
       aescmac.c)
   endif()
 
+  if(CONFIG_TESTING_CRYPTO_ECDSA)
+    nuttx_add_application(
+      NAME
+      ecdsa
+      PRIORITY
+      ${CONFIG_TESTING_CRYPTO_PRIORITY}
+      STACKSIZE
+      ${CONFIG_TESTING_CRYPTO_STACKSIZE}
+      MODULE
+      ${CONFIG_TESTING_CRYPTO}
+      SRCS
+      ecdsa.c)
+  endif()
+
 endif()
diff --git a/testing/crypto/Kconfig b/testing/crypto/Kconfig
index 485f7f15f..4e322994a 100644
--- a/testing/crypto/Kconfig
+++ b/testing/crypto/Kconfig
@@ -38,6 +38,10 @@ config TESTING_CRYPTO_AES_CMAC
 	bool "aes-cmac crypto test"
 	default n
 
+config TESTING_CRYPTO_ECDSA
+	bool "ecdsa crypto test"
+	default n
+
 config TESTING_CRYPTO_PRIORITY
 	int "crypto test task priority"
 	default 100
diff --git a/testing/crypto/Makefile b/testing/crypto/Makefile
index d76800371..3169efb5f 100644
--- a/testing/crypto/Makefile
+++ b/testing/crypto/Makefile
@@ -61,6 +61,11 @@ PROGNAME += aescmac
 MAINSRC +=  aescmac.c
 endif
 
+ifeq ($(CONFIG_TESTING_CRYPTO_ECDSA),y)
+PROGNAME += ecdsa
+MAINSRC +=  ecdsa.c
+endif
+
 PRIORITY = $(CONFIG_TESTING_CRYPTO_PRIORITY)
 STACKSIZE = $(CONFIG_TESTING_CRYPTO_STACKSIZE)
 MODULE = $(CONFIG_TESTING_CRYPTO)
diff --git a/testing/crypto/ecdsa.c b/testing/crypto/ecdsa.c
new file mode 100644
index 000000000..6a62c247a
--- /dev/null
+++ b/testing/crypto/ecdsa.c
@@ -0,0 +1,326 @@
+/****************************************************************************
+ * apps/testing/crypto/ecdsa.c
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <sys/param.h>
+#include <crypto/cryptodev.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define SECP256R1_KEYLEN 32
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static unsigned char sha256_message[32] =
+{
+  0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
+  0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
+  0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
+  0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int ecdsa_sign(int op, FAR unsigned char *r, size_t rlen,
+                      FAR unsigned char *s, size_t slen,
+                      FAR unsigned char *d, size_t dlen,
+                      FAR const unsigned char *buf, size_t buflen)
+{
+  struct crypt_kop cryptk;
+  int cryptodev_fd = -1;
+  int fd = -1;
+
+  if ((fd = open("/dev/crypto", O_RDWR, 0)) < 0)
+    {
+      perror("/dev/crypto");
+      goto err;
+    }
+
+  if (ioctl(fd, CRIOGET, &cryptodev_fd) == -1)
+    {
+      perror("CRIOGET");
+      goto err;
+    }
+
+  memset(&cryptk, 0, sizeof(cryptk));
+  cryptk.crk_op = op;
+  cryptk.crk_iparams = 2;
+  cryptk.crk_oparams = 2;
+  cryptk.crk_param[0].crp_p = (caddr_t)d;
+  cryptk.crk_param[0].crp_nbits = dlen * 8;
+  cryptk.crk_param[1].crp_p = (caddr_t)buf;
+  cryptk.crk_param[1].crp_nbits = buflen * 8;
+  cryptk.crk_param[2].crp_p = (caddr_t)r;
+  cryptk.crk_param[2].crp_nbits = rlen * 8;
+  cryptk.crk_param[3].crp_p = (caddr_t)s;
+  cryptk.crk_param[3].crp_nbits = slen * 8;
+
+  if (ioctl(cryptodev_fd, CIOCKEY, &cryptk) == -1)
+    {
+      perror("CIOCKEY");
+      goto err;
+    }
+
+  close(cryptodev_fd);
+  close(fd);
+  return cryptk.crk_status;
+
+err:
+  if (cryptodev_fd != -1)
+    {
+      close(cryptodev_fd);
+    }
+
+  if (fd != -1)
+    {
+      close(fd);
+    }
+
+  return -1;
+}
+
+static int ecdsa_verify(int op, FAR const unsigned char *qx, size_t qxlen,
+                        FAR const unsigned char *qy, size_t qylen,
+                        FAR const unsigned char *qz, size_t qzlen,
+                        FAR const unsigned char *r, size_t rlen,
+                        FAR const unsigned char *s, size_t slen,
+                        FAR const unsigned char *buf, size_t buflen)
+{
+  struct crypt_kop cryptk;
+  int cryptodev_fd = -1;
+  int fd = -1;
+
+  if ((fd = open("/dev/crypto", O_RDWR, 0)) < 0)
+    {
+      perror("/dev/crypto");
+      goto err;
+    }
+
+  if (ioctl(fd, CRIOGET, &cryptodev_fd) == -1)
+    {
+      perror("CRIOGET");
+      goto err;
+    }
+
+  memset(&cryptk, 0, sizeof(cryptk));
+  cryptk.crk_op = op;
+  cryptk.crk_iparams = 6;
+  cryptk.crk_oparams = 0;
+  cryptk.crk_param[0].crp_p = (caddr_t)qx;
+  cryptk.crk_param[0].crp_nbits = qxlen * 8;
+  cryptk.crk_param[1].crp_p = (caddr_t)qy;
+  cryptk.crk_param[1].crp_nbits = qylen * 8;
+  cryptk.crk_param[2].crp_p = (caddr_t)qz;
+  cryptk.crk_param[2].crp_nbits = qzlen * 8;
+  cryptk.crk_param[3].crp_p = (caddr_t)r;
+  cryptk.crk_param[3].crp_nbits = rlen * 8;
+  cryptk.crk_param[4].crp_p = (caddr_t)s;
+  cryptk.crk_param[4].crp_nbits = slen * 8;
+  cryptk.crk_param[5].crp_p = (caddr_t)buf;
+  cryptk.crk_param[5].crp_nbits = buflen * 8;
+
+  if (ioctl(cryptodev_fd, CIOCKEY, &cryptk) == -1)
+    {
+      perror("CIOCKEY");
+      goto err;
+    }
+
+  close(cryptodev_fd);
+  close(fd);
+  return cryptk.crk_status;
+
+err:
+  if (cryptodev_fd != -1)
+    {
+      close(cryptodev_fd);
+    }
+
+  if (fd != -1)
+    {
+      close(fd);
+    }
+
+  return -1;
+}
+
+static int ecdsa_genkey(int op, FAR const unsigned char *d, size_t dlen,
+                        FAR const unsigned char *qx, size_t qxlen,
+                        FAR const unsigned char *qy, size_t qylen,
+                        FAR const unsigned char *qz, size_t qzlen)
+{
+  struct crypt_kop cryptk;
+  int cryptodev_fd = -1;
+  int fd = -1;
+
+  if ((fd = open("/dev/crypto", O_RDWR, 0)) < 0)
+    {
+      perror("/dev/crypto");
+      goto err;
+    }
+
+  if (ioctl(fd, CRIOGET, &cryptodev_fd) == -1)
+    {
+      perror("CRIOGET");
+      goto err;
+    }
+
+  memset(&cryptk, 0, sizeof(cryptk));
+  cryptk.crk_op = op;
+  cryptk.crk_iparams = 0;
+  cryptk.crk_oparams = 4;
+  cryptk.crk_param[0].crp_p = (caddr_t)d;
+  cryptk.crk_param[0].crp_nbits = dlen * 8;
+  cryptk.crk_param[1].crp_p = (caddr_t)qx;
+  cryptk.crk_param[1].crp_nbits = qxlen * 8;
+  cryptk.crk_param[2].crp_p = (caddr_t)qy;
+  cryptk.crk_param[2].crp_nbits = qylen * 8;
+  cryptk.crk_param[3].crp_p = (caddr_t)qz;
+  cryptk.crk_param[3].crp_nbits = qzlen * 8;
+
+  if (ioctl(cryptodev_fd, CIOCKEY, &cryptk) == -1)
+    {
+      perror("CIOCKEY");
+      goto err;
+    }
+
+  close(cryptodev_fd);
+  close(fd);
+  return cryptk.crk_status;
+
+err:
+  if (cryptodev_fd != -1)
+    {
+      close(cryptodev_fd);
+    }
+
+  if (fd != -1)
+    {
+      close(fd);
+    }
+
+  return -1;
+}
+
+/* Test curve SECP256R1 */
+
+static int test_p256 (void)
+{
+  int ret = 0;
+  int len = SECP256R1_KEYLEN;
+  unsigned char *x = (unsigned char *)malloc(len);
+  unsigned char *y = (unsigned char *)malloc(len);
+  unsigned char *z = (unsigned char *)malloc(len);
+  unsigned char *d = (unsigned char *)malloc(len);
+  unsigned char *r = (unsigned char *)malloc(len);
+  unsigned char *s = (unsigned char *)malloc(len);
+
+  if (x == NULL || y == NULL || z == NULL ||
+      d == NULL || r == NULL || s == NULL)
+    {
+      perror("Error no enough buffer");
+      goto free;
+    }
+
+  memset(x, 0, len);
+  memset(y, 0, len);
+  memset(z, 0, len);
+  memset(d, 0, len);
+
+  ret = ecdsa_genkey(CRK_ECDSA_SECP256R1_GENKEY, x, len,
+                     y, len, z, len, d, len);
+  if (ret != 0)
+    {
+      printf("p256 generate key failed\n");
+      goto free;
+    }
+
+  ret = ecdsa_sign(CRK_ECDSA_SECP256R1_SIGN, r, len, s, len,
+                   d, len, sha256_message, len);
+  if (ret != 0)
+    {
+      printf("p256 sign failed\n");
+      goto free;
+    }
+
+  ret = ecdsa_verify(CRK_ECDSA_SECP256R1_VERIFY, x, len, y, len,
+                     z, len, r, len, s, len, sha256_message, len);
+  if (ret != 0)
+    {
+      printf("p256 verify failed\n");
+    }
+
+free:
+  if (x)
+    {
+      free(x);
+    }
+
+  if (y)
+    {
+      free(y);
+    }
+
+  if (z)
+    {
+      free(z);
+    }
+
+  if (d)
+    {
+      free(d);
+    }
+
+  if (r)
+    {
+      free(r);
+    }
+
+  if (s)
+    {
+      free(s);
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int main(void)
+{
+  if (test_p256() == 0)
+    {
+      printf("p256 test success\n");
+    }
+
+  return 0;
+}


(nuttx-apps) 01/05: testing/crypto: add crc32 testcases

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit 6db82199a6557efd55b4d7261e744f7fc85ab594
Author: makejian <ma...@xiaomi.com>
AuthorDate: Tue Jul 2 14:01:23 2024 +0800

    testing/crypto: add crc32 testcases
    
    Signed-off-by: makejian <ma...@xiaomi.com>
---
 testing/crypto/CMakeLists.txt |  14 ++
 testing/crypto/Kconfig        |   4 +
 testing/crypto/Makefile       |   5 +
 testing/crypto/crc32.c        | 298 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 321 insertions(+)

diff --git a/testing/crypto/CMakeLists.txt b/testing/crypto/CMakeLists.txt
index 5db24386f..d252d7d6a 100644
--- a/testing/crypto/CMakeLists.txt
+++ b/testing/crypto/CMakeLists.txt
@@ -103,4 +103,18 @@ if(CONFIG_TESTING_CRYPTO)
       hash.c)
   endif()
 
+  if(CONFIG_TESTING_CRYPTO_CRC32)
+    nuttx_add_application(
+      NAME
+      crc32
+      PRIORITY
+      ${CONFIG_TESTING_CRYPTO_PRIORITY}
+      STACKSIZE
+      ${CONFIG_TESTING_CRYPTO_STACKSIZE}
+      MODULE
+      ${CONFIG_TESTING_CRYPTO}
+      SRCS
+      crc32.c)
+  endif()
+
 endif()
diff --git a/testing/crypto/Kconfig b/testing/crypto/Kconfig
index 76ec23a68..0ed53e6b9 100644
--- a/testing/crypto/Kconfig
+++ b/testing/crypto/Kconfig
@@ -30,6 +30,10 @@ config TESTING_CRYPTO_HASH
 	bool "hash crypto test"
 	default n
 
+config TESTING_CRYPTO_CRC32
+	bool "crc32 crypto test"
+	default n
+
 config TESTING_CRYPTO_PRIORITY
 	int "crypto test task priority"
 	default 100
diff --git a/testing/crypto/Makefile b/testing/crypto/Makefile
index 0c71750cd..30d93ee9e 100644
--- a/testing/crypto/Makefile
+++ b/testing/crypto/Makefile
@@ -51,6 +51,11 @@ PROGNAME += hash
 MAINSRC +=  hash.c
 endif
 
+ifeq ($(CONFIG_TESTING_CRYPTO_CRC32),y)
+PROGNAME += crc32
+MAINSRC +=  crc32.c
+endif
+
 PRIORITY = $(CONFIG_TESTING_CRYPTO_PRIORITY)
 STACKSIZE = $(CONFIG_TESTING_CRYPTO_STACKSIZE)
 MODULE = $(CONFIG_TESTING_CRYPTO)
diff --git a/testing/crypto/crc32.c b/testing/crypto/crc32.c
new file mode 100644
index 000000000..6e7cde53a
--- /dev/null
+++ b/testing/crypto/crc32.c
@@ -0,0 +1,298 @@
+/****************************************************************************
+ * apps/testing/crypto/crc32.c
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <err.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <sys/param.h>
+#include <crypto/cryptodev.h>
+
+typedef struct crypto_context
+{
+  int fd;
+  int crypto_fd;
+  struct session_op session;
+  struct crypt_op cryp;
+}
+crypto_context;
+
+typedef struct tb
+{
+  FAR char *data;
+  int datalen;
+  uint32_t result;
+}
+tb;
+
+static tb g_crc32_testcase[] =
+{
+    /* testcase 1-7: Individual testing */
+
+    {
+      "",
+      0,
+      0,
+    },
+    {
+      "a",
+      1,
+      0xe8b7be43,
+    },
+    {
+      "abc",
+      3,
+      0x352441c2,
+    },
+    {
+      "message digest",
+      14,
+      0x20159d7f,
+    },
+    {
+      "abcdefghijklmnopqrstuvwxyz",
+      26,
+      0x4c2750bd,
+    },
+    {
+      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
+      62,
+      0x1fc2e6d2,
+    },
+    {
+      "123456789012345678901234567890123456789"
+      "01234567890123456789012345678901234567890",
+      80,
+      0x7ca94a72,
+    },
+
+    /* testcase 8: test case 7 is divided into 8 parts */
+
+    {
+      "1234567890",
+      10,
+      0x7ca94a72,
+    }
+};
+
+static void syscrc32_free(FAR crypto_context *ctx)
+{
+  if (ctx->crypto_fd != 0)
+    {
+      close(ctx->crypto_fd);
+      ctx->crypto_fd = 0;
+    }
+
+  if (ctx->fd != 0)
+    {
+      close(ctx->fd);
+      ctx->fd = 0;
+    }
+}
+
+static int syscrc32_init(FAR crypto_context *ctx)
+{
+  memset(ctx, 0, sizeof(crypto_context));
+  if ((ctx->fd = open("/dev/crypto", O_RDWR, 0)) < 0)
+    {
+      warn("CRIOGET");
+      syscrc32_free(ctx);
+      return 1;
+    }
+
+  if (ioctl(ctx->fd, CRIOGET, &ctx->crypto_fd) == -1)
+    {
+      warn("CRIOGET");
+      syscrc32_free(ctx);
+      return 1;
+    }
+
+  return 0;
+}
+
+static int syscrc32_start(FAR crypto_context *ctx, uint32_t *key)
+{
+  ctx->session.mac = CRYPTO_CRC32;
+  ctx->session.mackey = (caddr_t) key;
+  ctx->session.mackeylen = sizeof(uint32_t);
+  if (ioctl(ctx->crypto_fd, CIOCGSESSION, &ctx->session) == -1)
+    {
+      warn("CIOCGSESSION");
+      syscrc32_free(ctx);
+      return 1;
+    }
+
+  ctx->cryp.ses = ctx->session.ses;
+  return 0;
+}
+
+static int syscrc32_update(FAR crypto_context *ctx, FAR const char *s,
+                           size_t len)
+{
+  ctx->cryp.op = COP_ENCRYPT;
+  ctx->cryp.flags |= COP_FLAG_UPDATE;
+  ctx->cryp.src = (caddr_t) s;
+  ctx->cryp.len = len;
+  if (ioctl(ctx->crypto_fd, CIOCCRYPT, &ctx->cryp) == -1)
+    {
+      warn("CIOCCRYPT");
+      return 1;
+    }
+
+  return 0;
+}
+
+static int syscrc32_finish(FAR crypto_context *ctx, FAR uint32_t *out)
+{
+  ctx->cryp.flags = 0;
+  ctx->cryp.mac = (caddr_t) out;
+
+  if (ioctl(ctx->crypto_fd, CIOCCRYPT, &ctx->cryp) == -1)
+    {
+      warn("CIOCCRYPT");
+      return 1;
+    }
+
+  if (ioctl(ctx->crypto_fd, CIOCFSESSION, &ctx->session.ses) == -1)
+    {
+      warn("CIOCFSESSION");
+      return 1;
+    };
+
+  return 0;
+}
+
+static int match(const uint32_t a, const uint32_t b)
+{
+  if (a == b)
+    {
+      return 0;
+    }
+
+  warnx("crc32 mismatch");
+
+  printf("%02lx", a);
+  printf("%02lx", b);
+  printf("\n");
+
+  return 1;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int main(void)
+{
+  crypto_context crc32_ctx;
+  uint32_t output;
+  uint32_t startval = 0;
+  int ret = 0;
+  int i;
+
+  ret = syscrc32_init(&crc32_ctx);
+  if (ret != 0)
+    {
+      printf("syscrc32 init failed\n");
+    }
+
+  /* testcase 1-7: test crc32 vector */
+
+  for (i = 0; i < sizeof(g_crc32_testcase) / sizeof(tb) - 1; i++)
+    {
+      ret = syscrc32_start(&crc32_ctx, &startval);
+      if (ret != 0)
+        {
+          printf("syscrc32 start failed\n");
+          goto err;
+        }
+
+      ret = syscrc32_update(&crc32_ctx, g_crc32_testcase[i].data,
+                            g_crc32_testcase[i].datalen);
+      if (ret)
+        {
+          printf("syscrc32 update failed\n");
+          goto err;
+        }
+
+      ret = syscrc32_finish(&crc32_ctx, &output);
+      if (ret)
+        {
+          printf("syscrc32 finish failed\n");
+          goto err;
+        }
+
+      ret = match(g_crc32_testcase[i].result, output);
+      if (ret)
+        {
+          printf("match crc32 test case %d failed\n", i + 1);
+          goto err;
+        }
+      else
+        {
+          printf("crc32 test case %d success\n", i + 1);
+        }
+    }
+
+  /* testcase 8: test segmented computing capabilities in crc32 mode */
+
+  for (i = 0; i < 8; i++)
+    {
+      ret = syscrc32_start(&crc32_ctx, &startval);
+      if (ret != 0)
+        {
+          printf("syscrc32 start failed\n");
+          goto err;
+        }
+
+      ret = syscrc32_update(&crc32_ctx, g_crc32_testcase[7].data,
+                            g_crc32_testcase[7].datalen);
+      if (ret)
+        {
+          printf("syscrc32 update failed\n");
+          goto err;
+        }
+
+      ret = syscrc32_finish(&crc32_ctx, &startval);
+      if (ret)
+        {
+          printf("syscrc32 finish failed\n");
+          goto err;
+        }
+    }
+
+  ret = match(g_crc32_testcase[7].result, startval);
+  if (ret)
+    {
+      printf("match crc32 testcase 8 failed\n");
+      goto err;
+    }
+  else
+    {
+      printf("crc32 test case 8 success\n");
+    }
+
+err:
+  syscrc32_free(&crc32_ctx);
+  return 0;
+}


(nuttx-apps) 04/05: testing/crypto: Add test cases for Diffie-Hellman algorithm

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit b1f2e63a6bb9a747cd0ef9bf82d1c1e67534f1a2
Author: makejian <ma...@xiaomi.com>
AuthorDate: Fri Sep 6 14:05:29 2024 +0800

    testing/crypto: Add test cases for Diffie-Hellman algorithm
    
    Signed-off-by: makejian <ma...@xiaomi.com>
---
 testing/crypto/CMakeLists.txt |  14 ++
 testing/crypto/Kconfig        |   4 +
 testing/crypto/Makefile       |   5 +
 testing/crypto/dhm.c          | 310 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 333 insertions(+)

diff --git a/testing/crypto/CMakeLists.txt b/testing/crypto/CMakeLists.txt
index 06c4f4d7f..36f07b63a 100644
--- a/testing/crypto/CMakeLists.txt
+++ b/testing/crypto/CMakeLists.txt
@@ -145,4 +145,18 @@ if(CONFIG_TESTING_CRYPTO)
       ecdsa.c)
   endif()
 
+  if(CONFIG_TESTING_CRYPTO_ECDH)
+    nuttx_add_application(
+      NAME
+      dhm
+      PRIORITY
+      ${CONFIG_TESTING_CRYPTO_PRIORITY}
+      STACKSIZE
+      ${CONFIG_TESTING_CRYPTO_STACKSIZE}
+      MODULE
+      ${CONFIG_TESTING_CRYPTO}
+      SRCS
+      dhm.c)
+  endif()
+
 endif()
diff --git a/testing/crypto/Kconfig b/testing/crypto/Kconfig
index 4e322994a..9bf14ed1e 100644
--- a/testing/crypto/Kconfig
+++ b/testing/crypto/Kconfig
@@ -42,6 +42,10 @@ config TESTING_CRYPTO_ECDSA
 	bool "ecdsa crypto test"
 	default n
 
+config TESTING_CRYPTO_ECDH
+	bool "ecdh crypto test"
+	default n
+
 config TESTING_CRYPTO_PRIORITY
 	int "crypto test task priority"
 	default 100
diff --git a/testing/crypto/Makefile b/testing/crypto/Makefile
index 3169efb5f..950428e83 100644
--- a/testing/crypto/Makefile
+++ b/testing/crypto/Makefile
@@ -66,6 +66,11 @@ PROGNAME += ecdsa
 MAINSRC +=  ecdsa.c
 endif
 
+ifeq ($(CONFIG_TESTING_CRYPTO_ECDH),y)
+PROGNAME += dhm
+MAINSRC +=  dhm.c
+endif
+
 PRIORITY = $(CONFIG_TESTING_CRYPTO_PRIORITY)
 STACKSIZE = $(CONFIG_TESTING_CRYPTO_STACKSIZE)
 MODULE = $(CONFIG_TESTING_CRYPTO)
diff --git a/testing/crypto/dhm.c b/testing/crypto/dhm.c
new file mode 100644
index 000000000..efb483f66
--- /dev/null
+++ b/testing/crypto/dhm.c
@@ -0,0 +1,310 @@
+/****************************************************************************
+ * apps/testing/crypto/dhm.c
+ * Copyright (C) 2023 Xiaomi Corporation
+ *
+ * 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 <fcntl.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <sys/ioctl.h>
+#include <crypto/cryptodev.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* RFC 5114 section 2.1:
+ * 1024-bit MODP Group with 160-bit Prime Order Subgroup
+ */
+
+#define DHM_RFC5114_MODP_1024_P_BIN {                   \
+        0xB1, 0x0B, 0x8F, 0x96, 0xA0, 0x80, 0xE0, 0x1D, \
+        0xDE, 0x92, 0xDE, 0x5E, 0xAE, 0x5D, 0x54, 0xEC, \
+        0x52, 0xC9, 0x9F, 0xBC, 0xFB, 0x06, 0xA3, 0xC6, \
+        0x9A, 0x6A, 0x9D, 0xCA, 0x52, 0xD2, 0x3B, 0x61, \
+        0x60, 0x73, 0xE2, 0x86, 0x75, 0xA2, 0x3D, 0x18, \
+        0x98, 0x38, 0xEF, 0x1E, 0x2E, 0xE6, 0x52, 0xC0, \
+        0x13, 0xEC, 0xB4, 0xAE, 0xA9, 0x06, 0x11, 0x23, \
+        0x24, 0x97, 0x5C, 0x3C, 0xD4, 0x9B, 0x83, 0xBF, \
+        0xAC, 0xCB, 0xDD, 0x7D, 0x90, 0xC4, 0xBD, 0x70, \
+        0x98, 0x48, 0x8E, 0x9C, 0x21, 0x9A, 0x73, 0x72, \
+        0x4E, 0xFF, 0xD6, 0xFA, 0xE5, 0x64, 0x47, 0x38, \
+        0xFA, 0xA3, 0x1A, 0x4F, 0xF5, 0x5B, 0xCC, 0xC0, \
+        0xA1, 0x51, 0xAF, 0x5F, 0x0D, 0xC8, 0xB4, 0xBD, \
+        0x45, 0xBF, 0x37, 0xDF, 0x36, 0x5C, 0x1A, 0x65, \
+        0xE6, 0x8C, 0xFD, 0xA7, 0x6D, 0x4D, 0xA7, 0x08, \
+        0xDF, 0x1F, 0xB2, 0xBC, 0x2E, 0x4A, 0x43, 0x71 }
+
+#define DHM_RFC5114_MODP_1024_G_BIN {                   \
+        0xA4, 0xD1, 0xCB, 0xD5, 0xC3, 0xFD, 0x34, 0x12, \
+        0x67, 0x65, 0xA4, 0x42, 0xEF, 0xB9, 0x99, 0x05, \
+        0xF8, 0x10, 0x4D, 0xD2, 0x58, 0xAC, 0x50, 0x7F, \
+        0xD6, 0x40, 0x6C, 0xFF, 0x14, 0x26, 0x6D, 0x31, \
+        0x26, 0x6F, 0xEA, 0x1E, 0x5C, 0x41, 0x56, 0x4B, \
+        0x77, 0x7E, 0x69, 0x0F, 0x55, 0x04, 0xF2, 0x13, \
+        0x16, 0x02, 0x17, 0xB4, 0xB0, 0x1B, 0x88, 0x6A, \
+        0x5E, 0x91, 0x54, 0x7F, 0x9E, 0x27, 0x49, 0xF4, \
+        0xD7, 0xFB, 0xD7, 0xD3, 0xB9, 0xA9, 0x2E, 0xE1, \
+        0x90, 0x9D, 0x0D, 0x22, 0x63, 0xF8, 0x0A, 0x76, \
+        0xA6, 0xA2, 0x4C, 0x08, 0x7A, 0x09, 0x1F, 0x53, \
+        0x1D, 0xBF, 0x0A, 0x01, 0x69, 0xB6, 0xA2, 0x8A, \
+        0xD6, 0x62, 0xA4, 0xD1, 0x8E, 0x73, 0xAF, 0xA3, \
+        0x2D, 0x77, 0x9D, 0x59, 0x18, 0xD0, 0x8B, 0xC8, \
+        0x85, 0x8F, 0x4D, 0xCE, 0xF9, 0x7C, 0x2A, 0x24, \
+        0x85, 0x5E, 0x6E, 0xEB, 0x22, 0xB3, 0xB2, 0xE5 }
+
+#define DHM_SIZE 128
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+typedef struct cryptodev_context
+{
+  int fd;
+  int cryptodev_fd;
+}
+cryptodev_context;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int match(FAR unsigned char *a, FAR unsigned char *b, size_t len)
+{
+  int i;
+
+  if (memcmp(a, b, len) == 0)
+    {
+      return 0;
+    }
+
+  perror("dhm mismatch");
+
+  for (i = 0; i < len; i++)
+    {
+      printf("%02x", a[i]);
+    }
+
+  printf("\n");
+  for (i = 0; i < len; i++)
+    {
+      printf("%02x", b[i]);
+    }
+
+  printf("\n");
+
+  return 1;
+}
+
+static void cryptodev_free(FAR cryptodev_context *ctx)
+{
+  if (ctx->cryptodev_fd != 0)
+    {
+      close(ctx->cryptodev_fd);
+      ctx->cryptodev_fd = 0;
+    }
+
+  if (ctx->fd != 0)
+    {
+      close(ctx->fd);
+      ctx->fd = 0;
+    }
+}
+
+static int cryptodev_init(FAR cryptodev_context *ctx)
+{
+  memset(ctx, 0, sizeof(cryptodev_context));
+  if ((ctx->fd = open("/dev/crypto", O_RDWR, 0)) < 0)
+    {
+      perror("cryptodev_init open /dev/crypto failed");
+      cryptodev_free(ctx);
+      return 1;
+    }
+
+  if (ioctl(ctx->fd, CRIOGET, &ctx->cryptodev_fd) == -1)
+    {
+      perror("cryptodev_init CRIOGET failed");
+      cryptodev_free(ctx);
+      return 1;
+    }
+
+  return 0;
+}
+
+static int dh_make_public(FAR cryptodev_context *ctx,
+                          FAR const unsigned char *p, size_t p_size,
+                          FAR const unsigned char *g, size_t g_size,
+                          FAR unsigned char *x,  size_t x_size,
+                          FAR unsigned char *gx, size_t gx_size)
+{
+  struct crypt_kop cryptk;
+
+  memset(&cryptk, 0, sizeof(struct crypt_kop));
+  cryptk.crk_op = CRK_DH_MAKE_PUBLIC;
+  cryptk.crk_iparams = 2;
+  cryptk.crk_oparams = 2;
+
+  /* inputs: g p
+   * outputs: x gx
+   */
+
+  cryptk.crk_param[0].crp_p = (caddr_t) g;
+  cryptk.crk_param[0].crp_nbits = g_size * 8;
+  cryptk.crk_param[1].crp_p = (caddr_t) p;
+  cryptk.crk_param[1].crp_nbits = p_size * 8;
+  cryptk.crk_param[2].crp_p = (caddr_t) x;
+  cryptk.crk_param[2].crp_nbits = x_size * 8;
+  cryptk.crk_param[3].crp_p = (caddr_t) gx;
+  cryptk.crk_param[3].crp_nbits = gx_size * 8;
+  if (ioctl(ctx->cryptodev_fd, CIOCKEY, &cryptk) == -1)
+    {
+      perror("dh_make_public failed");
+      return 1;
+    }
+
+  return 0;
+}
+
+static int dh_compute_key(FAR cryptodev_context *ctx,
+                          FAR const unsigned char *gy, size_t gy_size,
+                          FAR const unsigned char *x, size_t x_size,
+                          FAR const unsigned char *p, size_t p_size,
+                          FAR unsigned char *k, size_t k_size)
+{
+  struct crypt_kop cryptk;
+
+  memset(&cryptk, 0, sizeof(struct crypt_kop));
+  cryptk.crk_op = CRK_DH_COMPUTE_KEY;
+  cryptk.crk_iparams = 3;
+  cryptk.crk_oparams = 1;
+
+  /* inputs: pub_key priv_key p */
+
+  cryptk.crk_param[0].crp_p = (caddr_t) gy;
+  cryptk.crk_param[0].crp_nbits = gy_size * 8;
+  cryptk.crk_param[1].crp_p = (caddr_t) x;
+  cryptk.crk_param[1].crp_nbits = x_size * 8;
+  cryptk.crk_param[2].crp_p = (caddr_t) p;
+  cryptk.crk_param[2].crp_nbits = p_size * 8;
+  cryptk.crk_param[3].crp_p = (caddr_t) k;
+  cryptk.crk_param[3].crp_nbits = k_size * 8;
+  if (ioctl(ctx->cryptodev_fd, CIOCKEY, &cryptk) == -1)
+    {
+      perror("dh_compute_key failed");
+      return 1;
+    }
+
+  return 0;
+}
+
+static int test_dh_compute_key(void)
+{
+  int ret;
+  cryptodev_context ctx;
+  static const unsigned char dhm_p_1024[] =
+      DHM_RFC5114_MODP_1024_P_BIN;
+  static const unsigned char dhm_g_1024[] =
+      DHM_RFC5114_MODP_1024_G_BIN;
+  unsigned char x1[DHM_SIZE];
+  unsigned char gx1[DHM_SIZE];
+  unsigned char k1[DHM_SIZE];
+  unsigned char x2[DHM_SIZE];
+  unsigned char gx2[DHM_SIZE];
+  unsigned char k2[DHM_SIZE];
+
+  ret = cryptodev_init(&ctx);
+  if (ret != 0)
+    {
+      goto free;
+    }
+
+  /* X-private key  GX-pulic key  K-share key */
+
+  memset(x1, 0, DHM_SIZE);
+  memset(gx1, 0, DHM_SIZE);
+  memset(k1, 0, DHM_SIZE);
+  memset(x2, 0, DHM_SIZE);
+  memset(gx2, 0, DHM_SIZE);
+  memset(k2, 0, DHM_SIZE);
+
+  ret = dh_make_public(&ctx, dhm_p_1024, DHM_SIZE, dhm_g_1024,
+                       DHM_SIZE, x1, DHM_SIZE, gx1, DHM_SIZE);
+  if (ret != 0)
+    {
+      printf("DH make Alice's public key failed\n");
+      goto free;
+    }
+
+  ret = dh_make_public(&ctx, dhm_p_1024, DHM_SIZE, dhm_g_1024,
+                       DHM_SIZE, x2, DHM_SIZE, gx2, DHM_SIZE);
+  if (ret != 0)
+    {
+      printf("DH make Bob's public failed\n");
+      goto free;
+    }
+
+  ret = dh_compute_key(&ctx, gx2, DHM_SIZE, x1, DHM_SIZE,
+                       dhm_p_1024, DHM_SIZE, k1, DHM_SIZE);
+  if (ret != 0)
+    {
+      printf("DH compute Alice's share key failed\n");
+      goto free;
+    }
+
+  ret = dh_compute_key(&ctx, gx1, DHM_SIZE, x2, DHM_SIZE,
+                       dhm_p_1024, DHM_SIZE, k2, DHM_SIZE);
+  if (ret != 0)
+    {
+      printf("DH compute Bob's share key failed\n");
+      goto free;
+    }
+
+  ret = match(k1, k2, DHM_SIZE);
+  if (ret != 0)
+    {
+      printf("mismatch share key\n");
+    }
+
+free:
+  cryptodev_free(&ctx);
+  return 0;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int main(void)
+{
+  if (test_dh_compute_key() != 0)
+    {
+      printf("test dh compute key failed\n");
+    }
+  else
+    {
+      printf("test dh compute key ok\n");
+    }
+
+  return 0;
+}