Module Name: src
Committed By: pgoyette
Date: Tue Jan 14 17:51:39 UTC 2014
Modified Files:
src/tests/crypto: Makefile
Added Files:
src/tests/crypto/opencrypto: Makefile Makefile.inc h_aesctr1.c
h_aesctr2.c h_arc4.c h_camellia.c h_cbcdes.c h_comp.c h_comp_zlib.c
h_comp_zlib_rnd.c h_gcm.c h_md5.c h_md5hmac.c h_null.c h_xcbcmac.c
t_opencrypto.sh
Log Message:
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.
To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/tests/crypto/Makefile
cvs rdiff -u -r0 -r1.1 src/tests/crypto/opencrypto/Makefile \
src/tests/crypto/opencrypto/Makefile.inc \
src/tests/crypto/opencrypto/h_aesctr1.c \
src/tests/crypto/opencrypto/h_aesctr2.c \
src/tests/crypto/opencrypto/h_arc4.c \
src/tests/crypto/opencrypto/h_camellia.c \
src/tests/crypto/opencrypto/h_cbcdes.c \
src/tests/crypto/opencrypto/h_comp.c \
src/tests/crypto/opencrypto/h_comp_zlib.c \
src/tests/crypto/opencrypto/h_comp_zlib_rnd.c \
src/tests/crypto/opencrypto/h_gcm.c src/tests/crypto/opencrypto/h_md5.c \
src/tests/crypto/opencrypto/h_md5hmac.c \
src/tests/crypto/opencrypto/h_null.c \
src/tests/crypto/opencrypto/h_xcbcmac.c \
src/tests/crypto/opencrypto/t_opencrypto.sh
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/tests/crypto/Makefile
diff -u src/tests/crypto/Makefile:1.2 src/tests/crypto/Makefile:1.3
--- src/tests/crypto/Makefile:1.2 Tue Jul 13 21:13:21 2010
+++ src/tests/crypto/Makefile Tue Jan 14 17:51:39 2014
@@ -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>
Added files:
Index: src/tests/crypto/opencrypto/Makefile
diff -u /dev/null src/tests/crypto/opencrypto/Makefile:1.1
--- /dev/null Tue Jan 14 17:51:39 2014
+++ src/tests/crypto/opencrypto/Makefile Tue Jan 14 17:51:39 2014
@@ -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>
+
Index: src/tests/crypto/opencrypto/Makefile.inc
diff -u /dev/null src/tests/crypto/opencrypto/Makefile.inc:1.1
--- /dev/null Tue Jan 14 17:51:39 2014
+++ src/tests/crypto/opencrypto/Makefile.inc Tue Jan 14 17:51:39 2014
@@ -0,0 +1,3 @@
+# $NetBSD: Makefile.inc,v 1.1 2014/01/14 17:51:39 pgoyette Exp $
+
+.include "../Makefile.inc"
Index: src/tests/crypto/opencrypto/h_aesctr1.c
diff -u /dev/null src/tests/crypto/opencrypto/h_aesctr1.c:1.1
--- /dev/null Tue Jan 14 17:51:39 2014
+++ src/tests/crypto/opencrypto/h_aesctr1.c Tue Jan 14 17:51:39 2014
@@ -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;
+}
Index: src/tests/crypto/opencrypto/h_aesctr2.c
diff -u /dev/null src/tests/crypto/opencrypto/h_aesctr2.c:1.1
--- /dev/null Tue Jan 14 17:51:39 2014
+++ src/tests/crypto/opencrypto/h_aesctr2.c Tue Jan 14 17:51:39 2014
@@ -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;
+}
Index: src/tests/crypto/opencrypto/h_arc4.c
diff -u /dev/null src/tests/crypto/opencrypto/h_arc4.c:1.1
--- /dev/null Tue Jan 14 17:51:39 2014
+++ src/tests/crypto/opencrypto/h_arc4.c Tue Jan 14 17:51:39 2014
@@ -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>
+
+unsigned char key[] = "abcdefgh";
+char plaintx[16] = "1234567890123456";
+const unsigned char ciphertx[16] = {
+ 0x21, 0xc6, 0x0d, 0xa5, 0x34, 0x24, 0x8b, 0xce,
+ 0x95, 0x86, 0x64, 0xb3, 0x66, 0x77, 0x9b, 0x4c
+};
+
+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_ARC4;
+ cs.keylen = 8;
+ 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);
+ res = ioctl(fd, CIOCCRYPT, &co);
+ if (res < 0)
+ err(1, "CIOCCRYPT");
+#if 1
+ if (memcmp(co.dst, ciphertx, sizeof(ciphertx)))
+ errx(1, "verification failed");
+#else
+ {
+ int i;
+ for (i = 0; i < sizeof(buf); i++)
+ printf("%02x ", buf[i]);
+ printf("\n");
+ }
+#endif
+ return 0;
+}
Index: src/tests/crypto/opencrypto/h_camellia.c
diff -u /dev/null src/tests/crypto/opencrypto/h_camellia.c:1.1
--- /dev/null Tue Jan 14 17:51:39 2014
+++ src/tests/crypto/opencrypto/h_camellia.c Tue Jan 14 17:51:39 2014
@@ -0,0 +1,93 @@
+/* $NetBSD: h_camellia.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[32] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
+ 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
+ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+ 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
+unsigned char iv[16] = {0};
+char plaintx[16] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
+ 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
+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_CAMELLIA_CBC;
+ cs.keylen = 32;
+ 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 0
+ if (memcmp(co.dst, ciphertx, sizeof(ciphertx)))
+ warnx("verification failed");
+#else
+ {
+ unsigned int i;
+ for (i = 0; i < sizeof(buf); i++)
+ printf("%02x ", buf[i]);
+ printf("\n");
+ }
+#endif
+ return 0;
+}
Index: src/tests/crypto/opencrypto/h_cbcdes.c
diff -u /dev/null src/tests/crypto/opencrypto/h_cbcdes.c:1.1
--- /dev/null Tue Jan 14 17:51:39 2014
+++ src/tests/crypto/opencrypto/h_cbcdes.c Tue Jan 14 17:51:39 2014
@@ -0,0 +1,89 @@
+/* $NetBSD: h_cbcdes.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[] = "abcdefgh";
+unsigned char iv[8] = {0};
+char plaintx[16] = "1234567890123456";
+const unsigned char ciphertx[16] = {
+ 0x21, 0xc6, 0x0d, 0xa5, 0x34, 0x24, 0x8b, 0xce,
+ 0x95, 0x86, 0x64, 0xb3, 0x66, 0x77, 0x9b, 0x4c
+};
+
+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_DES_CBC;
+ cs.keylen = 8;
+ 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)))
+ errx(1, "verification failed");
+#else
+ {
+ int i;
+ for (i = 0; i < sizeof(buf); i++)
+ printf("%02x ", buf[i]);
+ printf("\n");
+ }
+#endif
+ return 0;
+}
Index: src/tests/crypto/opencrypto/h_comp.c
diff -u /dev/null src/tests/crypto/opencrypto/h_comp.c:1.1
--- /dev/null Tue Jan 14 17:51:39 2014
+++ src/tests/crypto/opencrypto/h_comp.c Tue Jan 14 17:51:39 2014
@@ -0,0 +1,90 @@
+/* $NetBSD: h_comp.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 <unistd.h>
+
+#include <sys/ioctl.h>
+#include <sys/time.h>
+
+#include <crypto/cryptodev.h>
+
+char text[100000] = {0};
+
+int
+main(void)
+{
+ int fd, res;
+ struct session_op cs;
+ struct crypt_op co1, co2;
+ unsigned char buf1[10000], buf2[100000];
+
+ fd = open("/dev/crypto", O_RDWR, 0);
+ if (fd < 0)
+ err(1, "open");
+ memset(&cs, 0, sizeof(cs));
+ cs.comp_alg = CRYPTO_GZIP_COMP;
+ res = ioctl(fd, CIOCGSESSION, &cs);
+ if (res < 0)
+ err(1, "CIOCGSESSION");
+
+ memset(&co1, 0, sizeof(co1));
+ co1.ses = cs.ses;
+ co1.op = COP_COMP;
+ co1.len = sizeof(text);
+ co1.src = text;
+ co1.dst = buf1;
+ co1.dst_len = sizeof(buf1);
+ res = ioctl(fd, CIOCCRYPT, &co1);
+ if (res < 0)
+ err(1, "CIOCCRYPT1");
+ fprintf(stderr, "len %d/%d\n", co1.len, co1.dst_len);
+#if 0
+ buf1[co1.dst_len - 8]++; /* modify CRC */
+#endif
+ write(1, buf1, co1.dst_len);
+ memset(&co2, 0, sizeof(co2));
+ co2.ses = cs.ses;
+ co2.op = COP_DECOMP;
+ co2.len = co1.dst_len;
+ co2.src = buf1;
+ co2.dst = buf2;
+ co2.dst_len = sizeof(buf2);
+ buf2[10] = 0x33;
+ res = ioctl(fd, CIOCCRYPT, &co2);
+ fprintf(stderr, "canary: %x\n", buf2[10]);
+ if (res < 0)
+ err(1, "CIOCCRYPT2");
+ fprintf(stderr, "len %d/%d\n", co2.len, co2.dst_len);
+ if (memcmp(text, buf2, co2.dst_len))
+ errx(1, "memcmp");
+ return 0;
+}
Index: src/tests/crypto/opencrypto/h_comp_zlib.c
diff -u /dev/null src/tests/crypto/opencrypto/h_comp_zlib.c:1.1
--- /dev/null Tue Jan 14 17:51:39 2014
+++ src/tests/crypto/opencrypto/h_comp_zlib.c Tue Jan 14 17:51:39 2014
@@ -0,0 +1,92 @@
+/* $NetBSD: h_comp_zlib.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 <string.h>
+#include <zlib.h>
+
+#include <sys/ioctl.h>
+#include <sys/time.h>
+
+#include <crypto/cryptodev.h>
+
+char text[10000] = {0};
+
+int
+main(void)
+{
+ int fd, res;
+ struct session_op cs;
+ struct crypt_op co1;
+ unsigned char buf1[10000], buf2[10000];
+ z_stream z;
+
+ fd = open("/dev/crypto", O_RDWR, 0);
+ if (fd < 0)
+ err(1, "open");
+ memset(&cs, 0, sizeof(cs));
+ cs.comp_alg = CRYPTO_DEFLATE_COMP;
+ res = ioctl(fd, CIOCGSESSION, &cs);
+ if (res < 0)
+ err(1, "CIOCGSESSION");
+
+ memset(&co1, 0, sizeof(co1));
+ co1.ses = cs.ses;
+ co1.op = COP_COMP;
+ co1.len = sizeof(text);
+ co1.src = text;
+ co1.dst = buf1;
+ co1.dst_len = sizeof(buf1);
+ co1.flags = COP_F_BATCH;
+ res = ioctl(fd, CIOCCRYPT, &co1);
+ if (res < 0)
+ err(1, "CIOCCRYPT");
+
+ memset(&z, 0, sizeof(z));
+ z.next_in = buf1;
+ z.avail_in = co1.dst_len;
+ z.zalloc = Z_NULL;
+ z.zfree = Z_NULL;
+ z.opaque = 0;
+ z.next_out = buf2;
+ z.avail_out = sizeof(buf2);
+ res = inflateInit2(&z, -15);
+ if (res != Z_OK)
+ errx(1, "inflateInit: %d", res);
+ do {
+ res = inflate(&z, Z_SYNC_FLUSH);
+ } while (res == Z_OK);
+ if (res != Z_STREAM_END)
+ errx(1, "inflate: %d", res);
+ if (z.total_out != sizeof(text))
+ errx(1, "decomp len %lu", z.total_out);
+ if (memcmp(buf2, text, sizeof(text)))
+ errx(1, "decomp data mismatch");
+ return 0;
+}
Index: src/tests/crypto/opencrypto/h_comp_zlib_rnd.c
diff -u /dev/null src/tests/crypto/opencrypto/h_comp_zlib_rnd.c:1.1
--- /dev/null Tue Jan 14 17:51:39 2014
+++ src/tests/crypto/opencrypto/h_comp_zlib_rnd.c Tue Jan 14 17:51:39 2014
@@ -0,0 +1,96 @@
+/* $NetBSD: h_comp_zlib_rnd.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 <stdlib.h>
+#include <string.h>
+#include <zlib.h>
+
+#include <sys/ioctl.h>
+#include <sys/time.h>
+
+#include <crypto/cryptodev.h>
+
+char text[1000];
+
+int
+main(void)
+{
+ unsigned int i;
+ int fd, res;
+ struct session_op cs;
+ struct crypt_op co1;
+ unsigned char buf1[10000], buf2[10000];
+ z_stream z;
+
+ for (i = 0; i < sizeof(text)/sizeof(long); i++)
+ *(long *)(text + i * sizeof(long)) = random();
+
+ fd = open("/dev/crypto", O_RDWR, 0);
+ if (fd < 0)
+ err(1, "open");
+ memset(&cs, 0, sizeof(cs));
+ cs.comp_alg = CRYPTO_DEFLATE_COMP;
+ res = ioctl(fd, CIOCGSESSION, &cs);
+ if (res < 0)
+ err(1, "CIOCGSESSION");
+
+ memset(&co1, 0, sizeof(co1));
+ co1.ses = cs.ses;
+ co1.op = COP_COMP;
+ co1.len = sizeof(text);
+ co1.src = text;
+ co1.dst = buf1;
+ co1.dst_len = sizeof(buf1);
+ res = ioctl(fd, CIOCCRYPT, &co1);
+ if (res < 0)
+ err(1, "CIOCCRYPT");
+
+ memset(&z, 0, sizeof(z));
+ z.next_in = buf1;
+ z.avail_in = co1.dst_len;
+ z.zalloc = Z_NULL;
+ z.zfree = Z_NULL;
+ z.opaque = 0;
+ z.next_out = buf2;
+ z.avail_out = sizeof(buf2);
+ res = inflateInit2(&z, -15);
+ if (res != Z_OK)
+ errx(1, "inflateInit: %d", res);
+ do {
+ res = inflate(&z, Z_SYNC_FLUSH);
+ } while (res == Z_OK);
+ if (res != Z_STREAM_END)
+ errx(1, "inflate: %d", res);
+ if (z.total_out != sizeof(text))
+ errx(1, "decomp len %lu", z.total_out);
+ if (memcmp(buf2, text, sizeof(text)))
+ errx(1, "decomp data mismatch");
+ return 0;
+}
Index: src/tests/crypto/opencrypto/h_gcm.c
diff -u /dev/null src/tests/crypto/opencrypto/h_gcm.c:1.1
--- /dev/null Tue Jan 14 17:51:39 2014
+++ src/tests/crypto/opencrypto/h_gcm.c Tue Jan 14 17:51:39 2014
@@ -0,0 +1,139 @@
+/* $NetBSD: h_gcm.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] = { 0 };
+char plaintx[16] = { 0 };
+unsigned char iv[16] = { 0 };
+const unsigned char ciphertx[16] = {
+ 0x03, 0x88, 0xda, 0xce, 0x60, 0xb6, 0xa3, 0x92,
+ 0xf3, 0x28, 0xc2, 0xb9, 0x71, 0xb2, 0xfe, 0x78
+};
+const unsigned char hash[16] = {
+ 0xab, 0x6e, 0x47, 0xd4, 0x2c, 0xec, 0x13, 0xbd,
+ 0xf5, 0x3a, 0x67, 0xb2, 0x12, 0x57, 0xbd, 0xdf
+};
+
+int
+main(void)
+{
+ int fd, res;
+ struct session_op cs;
+ struct crypt_op co;
+ unsigned char databuf[16];
+ unsigned char macbuf[16];
+ unsigned char databuf2[16];
+
+ fd = open("/dev/crypto", O_RDWR, 0);
+ if (fd < 0)
+ err(1, "open");
+ memset(&cs, 0, sizeof(cs));
+ cs.mac = CRYPTO_AES_128_GMAC;
+ cs.mackeylen = sizeof(key);
+ cs.mackey = key;
+ cs.cipher = CRYPTO_AES_GCM_16;
+ cs.key = key;
+ cs.keylen = sizeof(key);
+ res = ioctl(fd, CIOCGSESSION, &cs);
+ if (res < 0)
+ err(1, "CIOCGSESSION");
+
+ memset(&co, 0, sizeof(co));
+ memset(databuf, 0, sizeof(databuf));
+ memset(macbuf, 0, sizeof(macbuf));
+ co.ses = cs.ses;
+ co.op = COP_ENCRYPT;
+ co.len = sizeof(plaintx);
+ co.src = plaintx;
+ co.dst = databuf;
+ co.mac = macbuf;
+ co.iv = iv;
+ res = ioctl(fd, CIOCCRYPT, &co);
+ if (res < 0)
+ err(1, "CIOCCRYPT");
+#if 1
+ if (memcmp(co.dst, ciphertx, sizeof(ciphertx)))
+ errx(1, "verification failed");
+ if (memcmp(macbuf, hash, sizeof(hash)))
+ errx(1, "hash failed");
+#else
+ {
+ int i;
+ for (i = 0; i < sizeof(databuf); i++)
+ printf("%02x ", databuf[i]);
+ printf("\n");
+ }
+ {
+ int i;
+ for (i = 0; i < sizeof(macbuf); i++)
+ printf("%02x ", macbuf[i]);
+ printf("\n");
+ }
+#endif
+ memset(databuf2, 0, sizeof(databuf2));
+ memset(macbuf, 0, sizeof(macbuf));
+ co.ses = cs.ses;
+ co.op = COP_DECRYPT;
+ co.len = sizeof(databuf);
+ co.src = databuf;
+ co.dst = databuf2;
+ co.mac = macbuf;
+ co.iv = iv;
+ res = ioctl(fd, CIOCCRYPT, &co);
+ if (res < 0)
+ err(1, "CIOCCRYPT");
+#if 1
+ if (memcmp(co.dst, plaintx, sizeof(plaintx)))
+ errx(1, "verification failed");
+ if (memcmp(macbuf, hash, sizeof(hash)))
+ errx(1, "hash failed");
+#else
+ {
+ int i;
+ for (i = 0; i < sizeof(databuf2); i++)
+ printf("%02x ", databuf2[i]);
+ printf("\n");
+ }
+ {
+ int i;
+ for (i = 0; i < sizeof(macbuf); i++)
+ printf("%02x ", macbuf[i]);
+ printf("\n");
+ }
+#endif
+ return 0;
+}
Index: src/tests/crypto/opencrypto/h_md5.c
diff -u /dev/null src/tests/crypto/opencrypto/h_md5.c:1.1
--- /dev/null Tue Jan 14 17:51:39 2014
+++ src/tests/crypto/opencrypto/h_md5.c Tue Jan 14 17:51:39 2014
@@ -0,0 +1,89 @@
+/* $NetBSD: h_md5.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[] = "abcdefgh";
+unsigned char iv[8] = {0};
+char plaintx[16] = "1234567890123456";
+const unsigned char ciphertx[16] = {
+ 0x21, 0xc6, 0x0d, 0xa5, 0x34, 0x24, 0x8b, 0xce,
+ 0x95, 0x86, 0x64, 0xb3, 0x66, 0x77, 0x9b, 0x4c
+};
+
+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.mac = CRYPTO_MD5;
+#if 0
+ cs.mackeylen = 8;
+ cs.mackey = key;
+#endif
+ 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.mac = buf;
+ res = ioctl(fd, CIOCCRYPT, &co);
+ if (res < 0)
+ err(1, "CIOCCRYPT");
+#if 0
+ if (memcmp(co.dst, ciphertx, sizeof(ciphertx)))
+ errx(1, "verification failed");
+#else
+ {
+ unsigned int i;
+ for (i = 0; i < sizeof(buf); i++)
+ printf("%02x ", buf[i]);
+ printf("\n");
+ }
+#endif
+ return 0;
+}
Index: src/tests/crypto/opencrypto/h_md5hmac.c
diff -u /dev/null src/tests/crypto/opencrypto/h_md5hmac.c:1.1
--- /dev/null Tue Jan 14 17:51:39 2014
+++ src/tests/crypto/opencrypto/h_md5hmac.c Tue Jan 14 17:51:39 2014
@@ -0,0 +1,90 @@
+/* $NetBSD: h_md5hmac.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[16] = {
+ 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
+ 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b
+};
+char plaintx[8] = "Hi There";
+const unsigned char ciphertx[16] = {
+ 0x21, 0xc6, 0x0d, 0xa5, 0x34, 0x24, 0x8b, 0xce,
+ 0x95, 0x86, 0x64, 0xb3, 0x66, 0x77, 0x9b, 0x4c
+};
+
+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.mac = CRYPTO_MD5_HMAC;
+ cs.mackeylen = sizeof(key);
+ cs.mackey = key;
+ res = ioctl(fd, CIOCGSESSION, &cs);
+ if (res < 0)
+ err(1, "CIOCGSESSION");
+
+ memset(&co, 0, sizeof(co));
+ memset(buf, 0, sizeof(buf));
+ co.ses = cs.ses;
+ co.op = COP_ENCRYPT;
+ co.len = sizeof(plaintx);
+ co.src = plaintx;
+ co.mac = buf;
+ res = ioctl(fd, CIOCCRYPT, &co);
+ if (res < 0)
+ err(1, "CIOCCRYPT");
+#if 0
+ if (memcmp(co.dst, ciphertx, sizeof(ciphertx)))
+ errx(1, "verification failed");
+#else
+ {
+ unsigned int i;
+ for (i = 0; i < sizeof(buf); i++)
+ printf("%02x ", buf[i]);
+ printf("\n");
+ }
+#endif
+ return 0;
+}
Index: src/tests/crypto/opencrypto/h_null.c
diff -u /dev/null src/tests/crypto/opencrypto/h_null.c:1.1
--- /dev/null Tue Jan 14 17:51:39 2014
+++ src/tests/crypto/opencrypto/h_null.c Tue Jan 14 17:51:39 2014
@@ -0,0 +1,89 @@
+/* $NetBSD: h_null.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[] = "abcdefgh";
+unsigned char iv[8] = {0};
+char plaintx[16] = "1234567890123456";
+const unsigned char ciphertx[16] = {
+ 0x21, 0xc6, 0x0d, 0xa5, 0x34, 0x24, 0x8b, 0xce,
+ 0x95, 0x86, 0x64, 0xb3, 0x66, 0x77, 0x9b, 0x4c
+};
+
+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_NULL_CBC;
+ cs.keylen = 8;
+ 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 0
+ if (memcmp(co.dst, ciphertx, sizeof(ciphertx)))
+ errx(1, "verification failed");
+#else
+ {
+ unsigned int i;
+ for (i = 0; i < sizeof(buf); i++)
+ printf("%02x ", buf[i]);
+ printf("\n");
+ }
+#endif
+ return 0;
+}
Index: src/tests/crypto/opencrypto/h_xcbcmac.c
diff -u /dev/null src/tests/crypto/opencrypto/h_xcbcmac.c:1.1
--- /dev/null Tue Jan 14 17:51:39 2014
+++ src/tests/crypto/opencrypto/h_xcbcmac.c Tue Jan 14 17:51:39 2014
@@ -0,0 +1,90 @@
+/* $NetBSD: h_xcbcmac.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[16] = {
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
+};
+char plaintx[] = {0x00, 0x01, 0x02};
+const unsigned char ciphertx[16] = {
+ 0x21, 0xc6, 0x0d, 0xa5, 0x34, 0x24, 0x8b, 0xce,
+ 0x95, 0x86, 0x64, 0xb3, 0x66, 0x77, 0x9b, 0x4c
+};
+
+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.mac = CRYPTO_AES_XCBC_MAC_96;
+ cs.mackeylen = sizeof(key);
+ cs.mackey = key;
+ res = ioctl(fd, CIOCGSESSION, &cs);
+ if (res < 0)
+ err(1, "CIOCGSESSION");
+
+ memset(&co, 0, sizeof(co));
+ memset(buf, 0, sizeof(buf));
+ co.ses = cs.ses;
+ co.op = COP_ENCRYPT;
+ co.len = 0 /*sizeof(plaintx)*/;
+ co.src = plaintx;
+ co.mac = buf;
+ res = ioctl(fd, CIOCCRYPT, &co);
+ if (res < 0)
+ err(1, "CIOCCRYPT");
+#if 0
+ if (memcmp(co.dst, ciphertx, sizeof(ciphertx)))
+ errx(1, "verification failed");
+#else
+ {
+ unsigned int i;
+ for (i = 0; i < sizeof(buf); i++)
+ printf("%02x ", buf[i]);
+ printf("\n");
+ }
+#endif
+ return 0;
+}
Index: src/tests/crypto/opencrypto/t_opencrypto.sh
diff -u /dev/null src/tests/crypto/opencrypto/t_opencrypto.sh:1.1
--- /dev/null Tue Jan 14 17:51:39 2014
+++ src/tests/crypto/opencrypto/t_opencrypto.sh Tue Jan 14 17:51:39 2014
@@ -0,0 +1,257 @@
+# $NetBSD: t_opencrypto.sh,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.
+#
+
+# Start a rumpserver, load required modules, and set requires sysctl vars
+
+start_rump() {
+ rump_libs="-l rumpvfs -l rumpdev -l rumpdev_opencrypto"
+ rump_libs="${rump_libs} -l rumpkern_z -l rumpkern_crypto"
+
+ rump_server ${rump_libs} ${RUMP_SERVER} || \
+ return 1
+
+ rump.sysctl -w kern.cryptodevallowsoft=-1 && \
+ return 0
+
+ rump.halt
+
+ return 1
+}
+
+common_head() {
+ atf_set descr "$1"
+ atf_set timeout 10
+ atf_set require.progs rump_server rump.sysctl rump.halt
+}
+
+common_body() {
+ local status
+
+ start_rump || atf_skip "Cannot set-up rump environment"
+ LD_PRELOAD="/usr/lib/librumphijack.so" ; export LD_PRELOAD
+ RUMPHIJACK="blanket=/dev/crypto" ; export RUMPHIJACK
+ $(atf_get_srcdir)/$1
+ status=$?
+ unset RUMPHIJACK
+ unset LD_PRELOAD
+ if [ $status -ne 0 ] ; then
+ atf_fail "$1 returned non-zero status, check output/error"
+ fi
+}
+
+common_cleanup() {
+ unset RUMPHIJACK
+ unset LD_PRELOAD
+ rump.halt
+}
+
+atf_test_case arc4 cleanup
+arc4_head() {
+ common_head "Test ARC4 crypto"
+}
+
+arc4_body() {
+ common_body h_arc4
+}
+
+arc4_cleanup() {
+ common_cleanup
+}
+
+atf_test_case camellia cleanup
+camellia_head() {
+ common_head "Test CAMELLIA_CBC crypto"
+}
+
+camellia_body() {
+ common_body h_camellia
+}
+
+camellia_cleanup() {
+ common_cleanup
+}
+
+atf_test_case cbcdes cleanup
+cbcdes_head() {
+ common_head "Test ARC4 crypto"
+}
+
+cbcdes_body() {
+ common_body h_cbcdes
+}
+
+cbcdes_cleanup() {
+ common_cleanup
+}
+
+atf_test_case comp cleanup
+comp_head() {
+ common_head "Test GZIP_COMP Compression"
+}
+
+comp_body() {
+ common_body h_comp
+}
+
+comp_cleanup() {
+ common_cleanup
+}
+
+atf_test_case comp_deflate cleanup
+comp_deflate_head() {
+ common_head "Test DEFLATE_COMP Compression"
+}
+
+comp_deflate_body() {
+ common_body h_comp_zlib
+}
+
+comp_deflate_cleanup() {
+ common_cleanup
+}
+
+atf_test_case comp_zlib_rnd cleanup
+comp_zlib_rnd_head() {
+ common_head "Test DEFLATE_COMP Compression with random data"
+}
+
+comp_zlib_rnd_body() {
+ common_body h_comp_zlib_rnd
+}
+
+comp_zlib_rnd_cleanup() {
+ common_cleanup
+}
+
+atf_test_case aesctr1 cleanup
+aesctr1_head() {
+ common_head "Test AES_CTR crypto"
+}
+
+aesctr1_body() {
+ common_body h_aesctr1
+}
+
+aesctr1_cleanup() {
+ common_cleanup
+}
+
+atf_test_case aesctr2 cleanup
+aesctr2_head() {
+ common_head "Test AES_CTR crypto"
+}
+
+aesctr2_body() {
+ common_body h_aesctr2
+}
+
+aesctr2_cleanup() {
+ common_cleanup
+}
+
+atf_test_case gcm cleanup
+gcm_head() {
+ common_head "Test AES_GCM_16 crypto"
+}
+
+gcm_body() {
+ common_body h_gcm
+}
+
+gcm_cleanup() {
+ common_cleanup
+}
+
+atf_test_case md5 cleanup
+md5_head() {
+ common_head "Test MD5 crypto"
+}
+
+md5_body() {
+ common_body h_md5
+}
+
+md5_cleanup() {
+ common_cleanup
+}
+
+atf_test_case md5_hmac cleanup
+md5_hmac_head() {
+ common_head "Test MD5_HMAC crypto"
+}
+
+md5_hmac_body() {
+ common_body h_md5hmac
+}
+
+md5_hmac_cleanup() {
+ common_cleanup
+}
+
+atf_test_case null cleanup
+null_head() {
+ common_head "Test NULL_CBC crypto"
+}
+
+null_body() {
+ common_body h_null
+}
+
+null_cleanup() {
+ common_cleanup
+}
+
+atf_test_case xcbcmac cleanup
+xcbcmac_head() {
+ common_head "Test XCBC_MAC_96 crypto"
+}
+
+xcbcmac_body() {
+ common_body h_xcbcmac
+}
+
+xcbcmac_cleanup() {
+ common_cleanup
+}
+
+atf_init_test_cases() {
+ RUMP_SERVER="unix://t_opencrypto_socket" ; export RUMP_SERVER
+
+ atf_add_test_case arc4
+ atf_add_test_case camellia
+ atf_add_test_case cbcdes
+ atf_add_test_case comp
+ atf_add_test_case comp_deflate
+ atf_add_test_case comp_zlib_rnd
+ atf_add_test_case aesctr1
+ atf_add_test_case aesctr2
+ atf_add_test_case gcm
+ atf_add_test_case md5
+ atf_add_test_case md5_hmac
+ atf_add_test_case null
+ atf_add_test_case xcbcmac
+}