Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/crypto Add some trivial tests for the in-kernel opencr...



details:   https://anonhg.NetBSD.org/src/rev/9f79dbb35e6d
branches:  trunk
changeset: 325971:9f79dbb35e6d
user:      pgoyette <pgoyette%NetBSD.org@localhost>
date:      Tue Jan 14 17:51:39 2014 +0000

description:
Add some trivial tests for the in-kernel opencrypto framework, and link
them into the build.  (Thanks to Matthias Drochner for the test programs!)

XXX The arc4 and null tests currently fail - under investigation.

diffstat:

 tests/crypto/Makefile                     |    3 +-
 tests/crypto/opencrypto/Makefile          |   30 +++
 tests/crypto/opencrypto/Makefile.inc      |    3 +
 tests/crypto/opencrypto/h_aesctr1.c       |   91 ++++++++++
 tests/crypto/opencrypto/h_aesctr2.c       |  104 ++++++++++++
 tests/crypto/opencrypto/h_arc4.c          |   87 ++++++++++
 tests/crypto/opencrypto/h_camellia.c      |   93 ++++++++++
 tests/crypto/opencrypto/h_cbcdes.c        |   89 ++++++++++
 tests/crypto/opencrypto/h_comp.c          |   90 ++++++++++
 tests/crypto/opencrypto/h_comp_zlib.c     |   92 ++++++++++
 tests/crypto/opencrypto/h_comp_zlib_rnd.c |   96 +++++++++++
 tests/crypto/opencrypto/h_gcm.c           |  139 ++++++++++++++++
 tests/crypto/opencrypto/h_md5.c           |   89 ++++++++++
 tests/crypto/opencrypto/h_md5hmac.c       |   90 ++++++++++
 tests/crypto/opencrypto/h_null.c          |   89 ++++++++++
 tests/crypto/opencrypto/h_xcbcmac.c       |   90 ++++++++++
 tests/crypto/opencrypto/t_opencrypto.sh   |  257 ++++++++++++++++++++++++++++++
 17 files changed, 1531 insertions(+), 1 deletions(-)

diffs (truncated from 1608 to 300 lines):

diff -r e5acf650ba63 -r 9f79dbb35e6d tests/crypto/Makefile
--- a/tests/crypto/Makefile     Tue Jan 14 17:20:57 2014 +0000
+++ b/tests/crypto/Makefile     Tue Jan 14 17:51:39 2014 +0000
@@ -1,9 +1,10 @@
-# $NetBSD: Makefile,v 1.2 2010/07/13 21:13:21 jmmv Exp $
+# $NetBSD: Makefile,v 1.3 2014/01/14 17:51:39 pgoyette Exp $
 
 .include <bsd.own.mk>
 
 TESTS_SUBDIRS=         libcrypto
 
 TESTSDIR=      ${TESTSBASE}/crypto
+TESTSDIR+=     ${TESTSBASE}/opencrypto
 
 .include <bsd.test.mk>
diff -r e5acf650ba63 -r 9f79dbb35e6d tests/crypto/opencrypto/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/crypto/opencrypto/Makefile  Tue Jan 14 17:51:39 2014 +0000
@@ -0,0 +1,30 @@
+# $NetBSD: Makefile,v 1.1 2014/01/14 17:51:39 pgoyette Exp $
+
+.include <bsd.own.mk>
+
+TESTSDIR=      ${TESTSBASE}/crypto/opencrypto
+
+TESTS_SH+=     t_opencrypto
+
+BINDIR=                ${TESTSDIR}
+MKMAN=         no
+
+PROGS+=                h_aesctr1
+PROGS+=                h_aesctr2
+PROGS+=                h_arc4
+PROGS+=                h_camellia
+PROGS+=                h_cbcdes
+PROGS+=                h_comp
+PROGS+=                h_comp_zlib
+PROGS+=                h_comp_zlib_rnd
+PROGS+=                h_gcm
+PROGS+=                h_md5
+PROGS+=                h_md5hmac
+PROGS+=                h_null
+PROGS+=                h_xcbcmac
+
+LDADD.h_comp_zlib+=    -lz
+LDADD.h_comp_zlib_rnd+=        -lz
+
+.include <bsd.test.mk>
+
diff -r e5acf650ba63 -r 9f79dbb35e6d tests/crypto/opencrypto/Makefile.inc
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/crypto/opencrypto/Makefile.inc      Tue Jan 14 17:51:39 2014 +0000
@@ -0,0 +1,3 @@
+# $NetBSD: Makefile.inc,v 1.1 2014/01/14 17:51:39 pgoyette Exp $
+
+.include "../Makefile.inc"
diff -r e5acf650ba63 -r 9f79dbb35e6d tests/crypto/opencrypto/h_aesctr1.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/crypto/opencrypto/h_aesctr1.c       Tue Jan 14 17:51:39 2014 +0000
@@ -0,0 +1,91 @@
+/* $NetBSD: h_aesctr1.c,v 1.1 2014/01/14 17:51:39 pgoyette Exp $ */
+
+/*-
+ * Copyright (c) 2014 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <sys/ioctl.h>
+#include <sys/time.h>
+
+#include <crypto/cryptodev.h>
+
+unsigned char key[20] = {0xae, 0x68, 0x52, 0xf8, 0x12, 0x10, 0x67, 0xcc,
+                        0x4b, 0xf7, 0xa5, 0x76, 0x55, 0x77, 0xf3, 0x9e,
+       0x00, 0x00, 0x00, 0x30};
+unsigned char iv[8] = {0};
+char plaintx[16] = "Single block msg";
+const unsigned char ciphertx[16] = {
+       0xe4, 0x09, 0x5d, 0x4f, 0xb7, 0xa7, 0xb3, 0x79,
+       0x2d, 0x61, 0x75, 0xa3, 0x26, 0x13, 0x11, 0xb8
+};
+
+int
+main(void)
+{
+       int fd, res;
+       struct session_op cs;
+       struct crypt_op co;
+       unsigned char buf[16];
+
+       fd = open("/dev/crypto", O_RDWR, 0);
+       if (fd < 0)
+               err(1, "open");
+       memset(&cs, 0, sizeof(cs));
+       cs.cipher = CRYPTO_AES_CTR;
+       cs.keylen = 20;
+       cs.key = key;
+       res = ioctl(fd, CIOCGSESSION, &cs);
+       if (res < 0)
+               err(1, "CIOCGSESSION");
+
+       memset(&co, 0, sizeof(co));
+       co.ses = cs.ses;
+       co.op = COP_ENCRYPT;
+       co.len = sizeof(plaintx);
+       co.src = plaintx;
+       co.dst = buf;
+       co.dst_len = sizeof(buf);
+       co.iv = iv;
+       res = ioctl(fd, CIOCCRYPT, &co);
+       if (res < 0)
+               err(1, "CIOCCRYPT");
+#if 1
+       if (memcmp(co.dst, ciphertx, sizeof(ciphertx)))
+               warnx("verification failed");
+#else
+       {
+               int i;
+               for (i = 0; i < sizeof(buf); i++)
+                       printf("%02x ", buf[i]);
+               printf("\n");
+       }
+#endif
+       return 0;
+}
diff -r e5acf650ba63 -r 9f79dbb35e6d tests/crypto/opencrypto/h_aesctr2.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/crypto/opencrypto/h_aesctr2.c       Tue Jan 14 17:51:39 2014 +0000
@@ -0,0 +1,104 @@
+/* $NetBSD: h_aesctr2.c,v 1.1 2014/01/14 17:51:39 pgoyette Exp $ */
+
+/*-
+ * Copyright (c) 2014 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <sys/ioctl.h>
+#include <sys/time.h>
+
+#include <crypto/cryptodev.h>
+
+unsigned char key[20] = {0xae, 0x68, 0x52, 0xf8, 0x12, 0x10, 0x67, 0xcc,
+                        0x4b, 0xf7, 0xa5, 0x76, 0x55, 0x77, 0xf3, 0x9e,
+       0x00, 0x00, 0x00, 0x30};
+unsigned char iv[8] = {0};
+char plaintx[16] = "Single block msg";
+const unsigned char ciphertx[16] = {
+       0xe4, 0x09, 0x5d, 0x4f, 0xb7, 0xa7, 0xb3, 0x79,
+       0x2d, 0x61, 0x75, 0xa3, 0x26, 0x13, 0x11, 0xb8
+};
+
+int
+main(void)
+{
+       int fd, res;
+       struct session_op cs;
+       struct crypt_op co;
+       unsigned char ibuf[24];
+       unsigned char obuf[24];
+
+       fd = open("/dev/crypto", O_RDWR, 0);
+       if (fd < 0)
+               err(1, "open");
+       memset(&cs, 0, sizeof(cs));
+       cs.cipher = CRYPTO_AES_CTR;
+       cs.keylen = 20;
+       cs.key = key;
+       res = ioctl(fd, CIOCGSESSION, &cs);
+       if (res < 0)
+               err(1, "CIOCGSESSION");
+
+       memcpy(ibuf, iv, 8);
+       memcpy(ibuf + 8, plaintx, 16);
+       memset(&co, 0, sizeof(co));
+       co.ses = cs.ses;
+       co.op = COP_ENCRYPT;
+       co.len = sizeof(ibuf);
+       co.src = ibuf;
+       co.dst = obuf;
+       co.dst_len = sizeof(obuf);
+       res = ioctl(fd, CIOCCRYPT, &co);
+       if (res < 0)
+               err(1, "CIOCCRYPT");
+       memset(ibuf, 0, sizeof(ibuf));
+       memset(&co, 0, sizeof(co));
+       co.ses = cs.ses;
+       co.op = COP_DECRYPT;
+       co.len = sizeof(obuf);
+       co.src = obuf;
+       co.dst = ibuf;
+       co.dst_len = sizeof(ibuf);
+       res = ioctl(fd, CIOCCRYPT, &co);
+       if (res < 0)
+               err(1, "CIOCCRYPT");
+#if 1
+       if (memcmp((char *)co.dst + 8, plaintx, sizeof(plaintx)))
+               warnx("verification failed");
+#else
+       {
+               int i;
+               for (i = 0; i < sizeof(ibuf); i++)
+                       printf("%02x ", ibuf[i]);
+               printf("\n");
+       }
+#endif
+       return 0;
+}
diff -r e5acf650ba63 -r 9f79dbb35e6d tests/crypto/opencrypto/h_arc4.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/crypto/opencrypto/h_arc4.c  Tue Jan 14 17:51:39 2014 +0000
@@ -0,0 +1,87 @@
+/* $NetBSD: h_arc4.c,v 1.1 2014/01/14 17:51:39 pgoyette Exp $ */
+
+/*-
+ * Copyright (c) 2014 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <sys/ioctl.h>
+#include <sys/time.h>
+
+#include <crypto/cryptodev.h>



Home | Main Index | Thread Index | Old Index