summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@gnutls.org>2010-05-27 12:32:28 +0200
committerNikos Mavrogiannopoulos <nmav@gnutls.org>2010-06-17 20:47:37 +0200
commit02eb90375d9975252647ac285d0ba9fa61435640 (patch)
treeb15be668291bc941e56cebba0baf591372569921 /examples
parent71eb8aa3b9c05b5ab8b8ab9cc0e245349dc2402d (diff)
downloadcryptodev-linux-02eb90375d9975252647ac285d0ba9fa61435640.tar.gz
cryptodev-linux-02eb90375d9975252647ac285d0ba9fa61435640.tar.xz
cryptodev-linux-02eb90375d9975252647ac285d0ba9fa61435640.zip
Added data functionality and a test program.
Diffstat (limited to 'examples')
-rw-r--r--examples/Makefile6
-rw-r--r--examples/cipher.c2
-rw-r--r--examples/hmac.c2
-rw-r--r--examples/new.c126
4 files changed, 132 insertions, 4 deletions
diff --git a/examples/Makefile b/examples/Makefile
index 601fb07..3190c4a 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -1,12 +1,14 @@
KERNEL_DIR ?= /lib/modules/$(shell uname -r)/build
-hostprogs := cipher hmac
+hostprogs := cipher hmac new
example-cipher-objs := cipher.o
example-hmac-objs := hmac.o
+new-objs := new.o
check: $(hostprogs)
+ ./new
./cipher
./hmac
clean:
- rm -f *.o *~ hmac cipher
+ rm -f *.o *~ hmac cipher new
diff --git a/examples/cipher.c b/examples/cipher.c
index d7982ae..c7ce2c2 100644
--- a/examples/cipher.c
+++ b/examples/cipher.c
@@ -10,7 +10,7 @@
#include <fcntl.h>
#include <sys/ioctl.h>
-#include <crypto/cryptodev.h>
+#include "../cryptodev.h"
#define DATA_SIZE 4096
#define BLOCK_SIZE 16
diff --git a/examples/hmac.c b/examples/hmac.c
index 9975792..9757f90 100644
--- a/examples/hmac.c
+++ b/examples/hmac.c
@@ -10,7 +10,7 @@
#include <fcntl.h>
#include <sys/ioctl.h>
-#include <crypto/cryptodev.h>
+#include "../cryptodev.h"
#define DATA_SIZE 4096
#define BLOCK_SIZE 16
diff --git a/examples/new.c b/examples/new.c
new file mode 100644
index 0000000..60f9437
--- /dev/null
+++ b/examples/new.c
@@ -0,0 +1,126 @@
+/*
+ * Demo on how to use /dev/crypto device for HMAC.
+ *
+ * Placed under public domain.
+ *
+ */
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include <sys/ioctl.h>
+#include "../ncr.h"
+#include <stdlib.h>
+
+#define DATA_SIZE 4096
+
+static void randomize_data(uint8_t * data, size_t data_size)
+{
+int i;
+
+ srand(time(0)*getpid());
+ for (i=0;i<data_size;i++) {
+ data[i] = rand() & 0xff;
+ }
+}
+
+static int
+test_ncr_data(int cfd)
+{
+ struct ncr_data_init_st init;
+ struct ncr_data_st kdata;
+ uint8_t data[DATA_SIZE];
+ uint8_t data_bak[DATA_SIZE];
+ int i;
+
+ randomize_data(data, sizeof(data));
+ memcpy(data_bak, data, sizeof(data));
+
+ init.max_object_size = DATA_SIZE;
+ init.flags = NCR_DATA_FLAG_EXPORTABLE;
+ init.initial_data = data;
+ init.initial_data_size = sizeof(data);
+
+ if (ioctl(cfd, NCRIO_DATA_INIT, &init)) {
+ perror("ioctl(NCRIO_DATA_INIT)");
+ return 1;
+ }
+
+ memset(data, 0, sizeof(data));
+
+ kdata.desc = init.desc;
+ kdata.data = data;
+ kdata.data_size = sizeof(data);
+ kdata.append_flag = 0;
+
+ if (ioctl(cfd, NCRIO_DATA_GET, &kdata)) {
+ perror("ioctl(NCRIO_DATA_GET)");
+ return 1;
+ }
+
+ if (memcmp(data, data_bak, sizeof(data))!=0) {
+ fprintf(stderr, "data returned but differ!\n");
+ return 1;
+ }
+
+ /* test set */
+ memset(data, 0xf1, sizeof(data));
+
+ kdata.desc = init.desc;
+ kdata.data = data;
+ kdata.data_size = sizeof(data);
+ kdata.append_flag = 0;
+
+ if (ioctl(cfd, NCRIO_DATA_SET, &kdata)) {
+ perror("ioctl(NCRIO_DATA_SET)");
+ return 1;
+ }
+
+ /* test get after set */
+ memset(data, 0, sizeof(data));
+
+ kdata.desc = init.desc;
+ kdata.data = data;
+ kdata.data_size = sizeof(data);
+ kdata.append_flag = 0;
+
+ if (ioctl(cfd, NCRIO_DATA_GET, &kdata)) {
+ perror("ioctl(NCRIO_DATA_GET)");
+ return 1;
+ }
+
+ for(i=0;i<kdata.data_size;i++) {
+ if (((uint8_t*)kdata.data)[i] != 0xf1) {
+ fprintf(stderr, "data returned but differ!\n");
+ return 1;
+ }
+ }
+
+ return 0; /* ok */
+}
+
+int
+main()
+{
+ int fd = -1, cfd = -1;
+
+ /* Open the crypto device */
+ fd = open("/dev/crypto", O_RDWR, 0);
+ if (fd < 0) {
+ perror("open(/dev/crypto)");
+ return 1;
+ }
+
+ /* Run the test itself */
+ if (test_ncr_data(cfd))
+ return 1;
+
+ /* Close the original descriptor */
+ if (close(fd)) {
+ perror("close(fd)");
+ return 1;
+ }
+
+ return 0;
+}