xref: /freebsd/tests/sys/opencrypto/blake2_test.c (revision 3877025f52ee205fe99ad4ff68229933d57e4bcb)
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 
27 /*
28  * Derived from blake2b-test.c and blake2s-test.c:
29  *
30  * BLAKE2 reference source code package - optimized C implementations
31  *
32  * Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
33  *
34  * To the extent possible under law, the author(s) have dedicated all copyright
35  * and related and neighboring rights to this software to the public domain
36  * worldwide. This software is distributed without any warranty.
37  *
38  * You should have received a copy of the CC0 Public Domain Dedication along with
39  * this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
40  */
41 
42 #include <sys/param.h>
43 
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <string.h>
47 
48 #include <atf-c.h>
49 
50 /* Be sure to include tree copy rather than system copy. */
51 #include "cryptodev.h"
52 
53 #include "freebsd_test_suite/macros.h"
54 
55 #include <blake2.h>
56 #include "blake2-kat.h"
57 
58 static uint8_t key2b[BLAKE2B_KEYBYTES];
59 static uint8_t key2s[BLAKE2S_KEYBYTES];
60 static uint8_t katbuf[KAT_LENGTH];
61 
62 static void
63 initialize_constant_buffers(void)
64 {
65 	size_t i;
66 
67 	for (i = 0; i < sizeof(key2b); i++)
68 		key2b[i] = (uint8_t)i;
69 	for (i = 0; i < sizeof(key2s); i++)
70 		key2s[i] = (uint8_t)i;
71 	for (i = 0; i < sizeof(katbuf); i++)
72 		katbuf[i] = (uint8_t)i;
73 }
74 
75 static int
76 lookup_crid(int fd, const char *devname)
77 {
78 	struct crypt_find_op find;
79 
80 	find.crid = -1;
81 	strlcpy(find.name, devname, sizeof(find.name));
82 	ATF_REQUIRE(ioctl(fd, CIOCFINDDEV, &find) != -1);
83 	return (find.crid);
84 }
85 
86 static int
87 get_handle_fd(void)
88 {
89 	int fd;
90 
91 	fd = open("/dev/crypto", O_RDWR);
92 	ATF_REQUIRE(fd >= 0);
93 	return (fd);
94 }
95 
96 static int
97 create_session(int fd, int alg, int crid, const void *key, size_t klen)
98 {
99 	struct session2_op sop;
100 
101 	memset(&sop, 0, sizeof(sop));
102 
103 	sop.mac = alg;
104 	sop.mackey = key;
105 	sop.mackeylen = klen;
106 	sop.crid = crid;
107 
108 	ATF_REQUIRE_MSG(ioctl(fd, CIOCGSESSION2, &sop) >= 0,
109 	    "alg %d keylen %zu, errno=%d (%s)", alg, klen, errno,
110 	    strerror(errno));
111 	return (sop.ses);
112 }
113 
114 static void
115 do_cryptop(int fd, int ses, size_t inlen, void *out)
116 {
117 	struct crypt_op cop;
118 
119 	memset(&cop, 0, sizeof(cop));
120 
121 	cop.ses = ses;
122 	cop.len = inlen;
123 	cop.src = katbuf;
124 	cop.mac = out;
125 	ATF_CHECK_MSG(ioctl(fd, CIOCCRYPT, &cop) >= 0, "ioctl(CIOCCRYPT)");
126 }
127 
128 static void
129 test_blake2b_vectors(const char *devname)
130 {
131 	uint8_t hash[BLAKE2B_OUTBYTES];
132 	int crid, fd, ses;
133 	size_t i;
134 
135 	initialize_constant_buffers();
136 	fd = get_handle_fd();
137 	crid = lookup_crid(fd, devname);
138 	ses = create_session(fd, CRYPTO_BLAKE2B, crid, key2b, sizeof(key2b));
139 
140 	for (i = 0; i < sizeof(katbuf); i++) {
141 		do_cryptop(fd, ses, i, hash);
142 		ATF_CHECK_EQ_MSG(
143 		    memcmp(hash, blake2b_keyed_kat[i], sizeof(hash)),
144 		    0,
145 		    "different at %zu", i);
146 	}
147 }
148 
149 static void
150 test_blake2s_vectors(const char *devname)
151 {
152 	uint8_t hash[BLAKE2S_OUTBYTES];
153 	int crid, fd, ses;
154 	size_t i;
155 
156 	initialize_constant_buffers();
157 	fd = get_handle_fd();
158 	crid = lookup_crid(fd, devname);
159 	ses = create_session(fd, CRYPTO_BLAKE2S, crid, key2s, sizeof(key2s));
160 
161 	for (i = 0; i < sizeof(katbuf); i++) {
162 		do_cryptop(fd, ses, i, hash);
163 		ATF_CHECK_EQ_MSG(
164 		    memcmp(hash, blake2s_keyed_kat[i], sizeof(hash)),
165 		    0,
166 		    "different at %zu", i);
167 	}
168 }
169 
170 ATF_TC(blake2b_vectors);
171 ATF_TC_HEAD(blake2b_vectors, tc)
172 {
173 	atf_tc_set_md_var(tc, "require.kmods", "nexus/cryptosoft cryptodev");
174 }
175 ATF_TC_BODY(blake2b_vectors, tc)
176 {
177 	ATF_REQUIRE_SYSCTL_INT("kern.crypto.allow_soft", 1);
178 	test_blake2b_vectors("cryptosoft0");
179 }
180 
181 ATF_TC(blake2s_vectors);
182 ATF_TC_HEAD(blake2s_vectors, tc)
183 {
184 	atf_tc_set_md_var(tc, "require.kmods", "nexus/cryptosoft cryptodev");
185 }
186 ATF_TC_BODY(blake2s_vectors, tc)
187 {
188 	ATF_REQUIRE_SYSCTL_INT("kern.crypto.allow_soft", 1);
189 	test_blake2s_vectors("cryptosoft0");
190 }
191 
192 #if defined(__i386__) || defined(__amd64__)
193 ATF_TC(blake2b_vectors_x86);
194 ATF_TC_HEAD(blake2b_vectors_x86, tc)
195 {
196 	atf_tc_set_md_var(tc, "require.kmods", "nexus/blake2 cryptodev");
197 }
198 ATF_TC_BODY(blake2b_vectors_x86, tc)
199 {
200 	ATF_REQUIRE_SYSCTL_INT("kern.crypto.allow_soft", 1);
201 	test_blake2b_vectors("blaketwo0");
202 }
203 
204 ATF_TC(blake2s_vectors_x86);
205 ATF_TC_HEAD(blake2s_vectors_x86, tc)
206 {
207 	atf_tc_set_md_var(tc, "require.kmods", "nexus/blake2 cryptodev");
208 }
209 ATF_TC_BODY(blake2s_vectors_x86, tc)
210 {
211 	ATF_REQUIRE_SYSCTL_INT("kern.crypto.allow_soft", 1);
212 	test_blake2s_vectors("blaketwo0");
213 }
214 #endif
215 
216 ATF_TP_ADD_TCS(tp)
217 {
218 
219 	ATF_TP_ADD_TC(tp, blake2b_vectors);
220 	ATF_TP_ADD_TC(tp, blake2s_vectors);
221 #if defined(__i386__) || defined(__amd64__)
222 	ATF_TP_ADD_TC(tp, blake2b_vectors_x86);
223 	ATF_TP_ADD_TC(tp, blake2s_vectors_x86);
224 #endif
225 
226 	return (atf_no_error());
227 }
228