xref: /freebsd/tests/sys/opencrypto/blake2_test.c (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /*-
2  * Copyright (c) 2018 Conrad Meyer <cem@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 /*
30  * Derived from blake2b-test.c and blake2s-test.c:
31  *
32  * BLAKE2 reference source code package - optimized C implementations
33  *
34  * Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
35  *
36  * To the extent possible under law, the author(s) have dedicated all copyright
37  * and related and neighboring rights to this software to the public domain
38  * worldwide. This software is distributed without any warranty.
39  *
40  * You should have received a copy of the CC0 Public Domain Dedication along with
41  * this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
42  */
43 
44 #include <sys/param.h>
45 
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <string.h>
49 
50 #include <atf-c.h>
51 
52 /* Be sure to include tree copy rather than system copy. */
53 #include "cryptodev.h"
54 
55 #include "freebsd_test_suite/macros.h"
56 
57 #include <blake2.h>
58 #include "blake2-kat.h"
59 
60 static uint8_t key2b[BLAKE2B_KEYBYTES];
61 static uint8_t key2s[BLAKE2S_KEYBYTES];
62 static uint8_t katbuf[KAT_LENGTH];
63 
64 static void
65 initialize_constant_buffers(void)
66 {
67 	size_t i;
68 
69 	for (i = 0; i < sizeof(key2b); i++)
70 		key2b[i] = (uint8_t)i;
71 	for (i = 0; i < sizeof(key2s); i++)
72 		key2s[i] = (uint8_t)i;
73 	for (i = 0; i < sizeof(katbuf); i++)
74 		katbuf[i] = (uint8_t)i;
75 }
76 
77 static int
78 lookup_crid(int fd, const char *devname)
79 {
80 	struct crypt_find_op find;
81 
82 	find.crid = -1;
83 	strlcpy(find.name, devname, sizeof(find.name));
84 	ATF_REQUIRE(ioctl(fd, CIOCFINDDEV, &find) != -1);
85 	return (find.crid);
86 }
87 
88 static int
89 get_handle_fd(void)
90 {
91 	int fd;
92 
93 	fd = open("/dev/crypto", O_RDWR);
94 	ATF_REQUIRE(fd >= 0);
95 	return (fd);
96 }
97 
98 static int
99 create_session(int fd, int alg, int crid, const void *key, size_t klen)
100 {
101 	struct session2_op sop;
102 
103 	memset(&sop, 0, sizeof(sop));
104 
105 	sop.mac = alg;
106 	sop.mackey = key;
107 	sop.mackeylen = klen;
108 	sop.crid = crid;
109 
110 	ATF_REQUIRE_MSG(ioctl(fd, CIOCGSESSION2, &sop) >= 0,
111 	    "alg %d keylen %zu, errno=%d (%s)", alg, klen, errno,
112 	    strerror(errno));
113 	return (sop.ses);
114 }
115 
116 static void
117 do_cryptop(int fd, int ses, size_t inlen, void *out)
118 {
119 	struct crypt_op cop;
120 
121 	memset(&cop, 0, sizeof(cop));
122 
123 	cop.ses = ses;
124 	cop.len = inlen;
125 	cop.src = katbuf;
126 	cop.mac = out;
127 	ATF_CHECK_MSG(ioctl(fd, CIOCCRYPT, &cop) >= 0, "ioctl(CIOCCRYPT)");
128 }
129 
130 static void
131 test_blake2b_vectors(const char *devname, const char *modname)
132 {
133 	uint8_t hash[BLAKE2B_OUTBYTES];
134 	int crid, fd, ses;
135 	size_t i;
136 
137 	ATF_REQUIRE_KERNEL_MODULE(modname);
138 	ATF_REQUIRE_KERNEL_MODULE("cryptodev");
139 
140 	initialize_constant_buffers();
141 	fd = get_handle_fd();
142 	crid = lookup_crid(fd, devname);
143 	ses = create_session(fd, CRYPTO_BLAKE2B, crid, key2b, sizeof(key2b));
144 
145 	for (i = 0; i < sizeof(katbuf); i++) {
146 		do_cryptop(fd, ses, i, hash);
147 		ATF_CHECK_EQ_MSG(
148 		    memcmp(hash, blake2b_keyed_kat[i], sizeof(hash)),
149 		    0,
150 		    "different at %zu", i);
151 	}
152 }
153 
154 static void
155 test_blake2s_vectors(const char *devname, const char *modname)
156 {
157 	uint8_t hash[BLAKE2S_OUTBYTES];
158 	int crid, fd, ses;
159 	size_t i;
160 
161 	ATF_REQUIRE_KERNEL_MODULE(modname);
162 	ATF_REQUIRE_KERNEL_MODULE("cryptodev");
163 
164 	initialize_constant_buffers();
165 	fd = get_handle_fd();
166 	crid = lookup_crid(fd, devname);
167 	ses = create_session(fd, CRYPTO_BLAKE2S, crid, key2s, sizeof(key2s));
168 
169 	for (i = 0; i < sizeof(katbuf); i++) {
170 		do_cryptop(fd, ses, i, hash);
171 		ATF_CHECK_EQ_MSG(
172 		    memcmp(hash, blake2s_keyed_kat[i], sizeof(hash)),
173 		    0,
174 		    "different at %zu", i);
175 	}
176 }
177 
178 ATF_TC_WITHOUT_HEAD(blake2b_vectors);
179 ATF_TC_BODY(blake2b_vectors, tc)
180 {
181 	ATF_REQUIRE_SYSCTL_INT("kern.crypto.allow_soft", 1);
182 	test_blake2b_vectors("cryptosoft0", "nexus/cryptosoft");
183 }
184 
185 ATF_TC_WITHOUT_HEAD(blake2s_vectors);
186 ATF_TC_BODY(blake2s_vectors, tc)
187 {
188 	ATF_REQUIRE_SYSCTL_INT("kern.crypto.allow_soft", 1);
189 	test_blake2s_vectors("cryptosoft0", "nexus/cryptosoft");
190 }
191 
192 #if defined(__i386__) || defined(__amd64__)
193 ATF_TC_WITHOUT_HEAD(blake2b_vectors_x86);
194 ATF_TC_BODY(blake2b_vectors_x86, tc)
195 {
196 	ATF_REQUIRE_SYSCTL_INT("kern.crypto.allow_soft", 1);
197 	test_blake2b_vectors("blaketwo0", "nexus/blake2");
198 }
199 
200 ATF_TC_WITHOUT_HEAD(blake2s_vectors_x86);
201 ATF_TC_BODY(blake2s_vectors_x86, tc)
202 {
203 	ATF_REQUIRE_SYSCTL_INT("kern.crypto.allow_soft", 1);
204 	test_blake2s_vectors("blaketwo0", "nexus/blake2");
205 }
206 #endif
207 
208 ATF_TP_ADD_TCS(tp)
209 {
210 
211 	ATF_TP_ADD_TC(tp, blake2b_vectors);
212 	ATF_TP_ADD_TC(tp, blake2s_vectors);
213 #if defined(__i386__) || defined(__amd64__)
214 	ATF_TP_ADD_TC(tp, blake2b_vectors_x86);
215 	ATF_TP_ADD_TC(tp, blake2s_vectors_x86);
216 #endif
217 
218 	return (atf_no_error());
219 }
220