2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2019 Conrad Meyer <cem@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/random.h>
38 #include <crypto/chacha20/chacha.h>
39 #include <crypto/rijndael/rijndael-api-fst.h>
40 #include <crypto/sha2/sha256.h>
42 #include <dev/random/hash.h>
43 #include <dev/random/uint128.h>
48 vec_u32_tole128(uint8_t dst[static 16], const uint32_t src[static 4])
51 le32enc(&dst[4], src[1]);
52 le32enc(&dst[8], src[2]);
53 le32enc(&dst[12], src[3]);
57 le128_to_vec_u32(uint32_t dst[static 4], const uint8_t src[static 16])
59 dst[0] = le32dec(src);
60 dst[1] = le32dec(&src[4]);
61 dst[2] = le32dec(&src[8]);
62 dst[3] = le32dec(&src[12]);
66 formatu128(char buf[static 52], uint128_t x)
74 le128_to_vec_u32(vx, le128x);
77 for (i = 0; i < 4; i++) {
78 rc = snprintf(buf, sz, "0x%x ", vx[i]);
79 ATF_REQUIRE(rc > 0 && (size_t)rc < sz);
84 /* Delete last trailing space */
89 u128_check_equality(uint128_t a, uint128_t b, const char *descr)
91 char fmtbufa[52], fmtbufb[52];
93 formatu128(fmtbufa, a);
94 formatu128(fmtbufb, b);
96 ATF_CHECK_MSG(uint128_equals(a, b),
97 "Expected: [%s] != Actual: [%s]: %s", fmtbufa, fmtbufb, descr);
100 ATF_TC_WITHOUT_HEAD(uint128_inc);
101 ATF_TC_BODY(uint128_inc, tc)
103 static const struct u128_inc_tc {
105 uint32_t expected[4];
109 .input = { 0, 0, 0, 0 },
110 .expected = { 1, 0, 0, 0 },
114 .input = { 1, 0, 0, 0 },
115 .expected = { 2, 0, 0, 0 },
119 .input = { 0xff, 0, 0, 0 },
120 .expected = { 0x100, 0, 0, 0 },
121 .descr = "0xff -> 0x100 (byte carry)",
124 .input = { UINT32_MAX, 0, 0, 0 },
125 .expected = { 0, 1, 0, 0 },
126 .descr = "2^32 - 1 -> 2^32 (word carry)",
129 .input = { UINT32_MAX, UINT32_MAX, 0, 0 },
130 .expected = { 0, 0, 1, 0 },
131 .descr = "2^64 - 1 -> 2^64 (u128t_word0 carry)",
134 .input = { UINT32_MAX, UINT32_MAX, UINT32_MAX, 0 },
135 .expected = { 0, 0, 0, 1 },
136 .descr = "2^96 - 1 -> 2^96 (word carry)",
139 uint8_t inputle[16], expectedle[16];
143 for (i = 0; i < nitems(tests); i++) {
144 vec_u32_tole128(inputle, tests[i].input);
145 vec_u32_tole128(expectedle, tests[i].expected);
147 a = le128dec(inputle);
148 uint128_increment(&a);
149 u128_check_equality(le128dec(expectedle), a, tests[i].descr);
153 ATF_TC_WITHOUT_HEAD(uint128_add64);
154 ATF_TC_BODY(uint128_add64, tc)
156 static const struct u128_add64_tc {
159 uint32_t expected[4];
163 .input = { 0, 0, 0, 0 },
165 .expected = { 1, 0, 0, 0 },
166 .descr = "0 + 1 -> 1",
169 .input = { 1, 0, 0, 0 },
170 .addend = UINT32_MAX,
171 .expected = { 0, 1, 0, 0 },
172 .descr = "1 + (2^32 - 1) -> 2^32 (word carry)",
175 .input = { 1, 0, 0, 0 },
176 .addend = UINT64_MAX,
177 .expected = { 0, 0, 1, 0 },
178 .descr = "1 + (2^64 - 1) -> 2^64 (u128t_word0 carry)",
181 .input = { 0x11111111, 0x11111111, 0, 0 },
182 .addend = 0xf0123456789abcdeULL,
183 .expected = { 0x89abcdef, 0x01234567, 1, 0 },
184 .descr = "0x1111_1111_1111_1111 +"
185 "0xf012_3456_789a_bcde ->"
186 "0x1_0123_4567_89ab_cdef",
189 .input = { 1, 0, UINT32_MAX, 0 },
190 .addend = UINT64_MAX,
191 .expected = { 0, 0, 0, 1 },
192 .descr = "Carry ~2^96",
195 uint8_t inputle[16], expectedle[16];
199 for (i = 0; i < nitems(tests); i++) {
200 vec_u32_tole128(inputle, tests[i].input);
201 vec_u32_tole128(expectedle, tests[i].expected);
203 a = le128dec(inputle);
204 uint128_add64(&a, tests[i].addend);
205 u128_check_equality(le128dec(expectedle), a, tests[i].descr);
210 * Test assumptions about Chacha incrementing counter in the same way as
213 ATF_TC_WITHOUT_HEAD(uint128_chacha_ctr);
214 ATF_TC_BODY(uint128_chacha_ctr, tc)
216 static const struct u128_chacha_tc {
218 uint32_t expected[4];
222 .input = { 0, 0, 0, 0 },
223 .expected = { 1, 0, 0, 0 },
224 .descr = "Single block",
227 .input = { 1, 0, 0, 0 },
228 .expected = { 2, 0, 0, 0 },
232 .input = { 0xff, 0, 0, 0 },
233 .expected = { 0x100, 0, 0, 0 },
234 .descr = "0xff -> 0x100 (byte carry)",
237 .input = { UINT32_MAX, 0, 0, 0 },
238 .expected = { 0, 1, 0, 0 },
239 .descr = "2^32 - 1 -> 2^32 (word carry)",
242 .input = { UINT32_MAX, UINT32_MAX, 0, 0 },
243 .expected = { 0, 0, 1, 0 },
244 .descr = "2^64 - 1 -> 2^64 (u128t_word0 carry)",
247 .input = { UINT32_MAX, UINT32_MAX, UINT32_MAX, 0 },
248 .expected = { 0, 0, 0, 1 },
249 .descr = "2^96 - 1 -> 2^96 (word carry)",
252 union randomdev_key context;
253 uint8_t inputle[16], expectedle[16], trash[CHACHA_BLOCKLEN];
254 uint8_t notrandomkey[RANDOM_KEYSIZE] = { 0 };
258 random_chachamode = true;
259 randomdev_encrypt_init(&context, notrandomkey);
261 for (i = 0; i < nitems(tests); i++) {
262 vec_u32_tole128(inputle, tests[i].input);
263 vec_u32_tole128(expectedle, tests[i].expected);
265 a = le128dec(inputle);
266 randomdev_keystream(&context, &a, trash, sizeof(trash));
267 u128_check_equality(le128dec(expectedle), a, tests[i].descr);
275 ATF_TP_ADD_TC(tp, uint128_inc);
276 ATF_TP_ADD_TC(tp, uint128_add64);
277 ATF_TP_ADD_TC(tp, uint128_chacha_ctr);
278 return (atf_no_error());