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 2023/01/16 17:57:45 UTC

[nuttx-apps] 01/02: crypto:add getfd for crypto testcase

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 b366a31644d6098e2926b2a35c29e7d38cd035d3
Author: anjiahao <an...@xiaomi.com>
AuthorDate: Mon Jan 16 16:57:18 2023 +0800

    crypto:add getfd for crypto testcase
    
    Signed-off-by: anjiahao <an...@xiaomi.com>
---
 testing/crypto/3descbc.c | 15 ++++++++++++++-
 testing/crypto/aesctr.c  | 15 ++++++++++++++-
 testing/crypto/aesxts.c  | 15 ++++++++++++++-
 testing/crypto/hmac.c    | 15 ++++++++++++++-
 4 files changed, 56 insertions(+), 4 deletions(-)

diff --git a/testing/crypto/3descbc.c b/testing/crypto/3descbc.c
index 369187183..2e2d0c2fe 100644
--- a/testing/crypto/3descbc.c
+++ b/testing/crypto/3descbc.c
@@ -48,13 +48,20 @@ static int syscrypt(FAR const unsigned char *key, size_t klen,
   struct session_op session;
   struct crypt_op cryp;
   int cryptodev_fd = -1;
+  int fd = -1;
 
-  if ((cryptodev_fd = open("/dev/crypto", O_RDWR, 0)) < 0)
+  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_3DES_CBC;
   session.key = (caddr_t) key;
@@ -88,6 +95,7 @@ static int syscrypt(FAR const unsigned char *key, size_t klen,
     }
 
   close(cryptodev_fd);
+  close(fd);
   return (0);
 
 err:
@@ -96,6 +104,11 @@ err:
       close(cryptodev_fd);
     }
 
+  if (fd != -1)
+    {
+      close(fd);
+    }
+
   return (-1);
 }
 
diff --git a/testing/crypto/aesctr.c b/testing/crypto/aesctr.c
index 9718c5b48..4d65ddbd5 100644
--- a/testing/crypto/aesctr.c
+++ b/testing/crypto/aesctr.c
@@ -179,13 +179,20 @@ static int syscrypt(FAR const unsigned char *key, size_t klen,
   struct session_op session;
   struct crypt_op cryp;
   int cryptodev_fd = -1;
+  int fd = -1;
 
-  if ((cryptodev_fd = open("/dev/crypto", O_RDWR, 0)) < 0)
+  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;
@@ -218,6 +225,7 @@ static int syscrypt(FAR const unsigned char *key, size_t klen,
     }
 
   close(cryptodev_fd);
+  close(fd);
   return (0);
 
 err:
@@ -226,6 +234,11 @@ err:
       close(cryptodev_fd);
     }
 
+  if (fd != -1)
+    {
+      close(fd);
+    }
+
   return (-1);
 }
 
diff --git a/testing/crypto/aesxts.c b/testing/crypto/aesxts.c
index 399f349bc..b99698db3 100644
--- a/testing/crypto/aesxts.c
+++ b/testing/crypto/aesxts.c
@@ -1763,13 +1763,20 @@ static int syscrypt(FAR const unsigned char *key, size_t klen,
   struct session_op session;
   struct crypt_op cryp;
   int cryptodev_fd = -1;
+  int fd = -1;
 
-  if ((cryptodev_fd = open("/dev/crypto", O_RDWR, 0)) < 0)
+  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_XTS;
   session.key = (caddr_t) key;
@@ -1802,6 +1809,7 @@ static int syscrypt(FAR const unsigned char *key, size_t klen,
     }
 
   close(cryptodev_fd);
+  close(fd);
   return (0);
 
 err:
@@ -1810,6 +1818,11 @@ err:
       close(cryptodev_fd);
     }
 
+  if (fd != -1)
+    {
+      close(fd);
+    }
+
   return (-1);
 }
 
diff --git a/testing/crypto/hmac.c b/testing/crypto/hmac.c
index a28ee406b..3816664a8 100644
--- a/testing/crypto/hmac.c
+++ b/testing/crypto/hmac.c
@@ -95,13 +95,20 @@ int syshmac(int mac, FAR const char *key, size_t keylen,
   struct session_op session;
   struct crypt_op cryp;
   int cryptodev_fd = -1;
+  int fd = -1;
 
-  if ((cryptodev_fd = open("/dev/crypto", O_RDWR, 0)) < 0)
+  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 = 0;
   session.mac = mac;
@@ -135,6 +142,7 @@ int syshmac(int mac, FAR const char *key, size_t keylen,
     };
 
   close(cryptodev_fd);
+  close(fd);
   return 0;
 err:
   if (cryptodev_fd != -1)
@@ -142,6 +150,11 @@ err:
       close(cryptodev_fd);
     }
 
+  if (fd != -1)
+    {
+      close(fd);
+    }
+
   return 1;
 }