chacha_selftest.c revision 1.1 1 1.1 riastrad /* $NetBSD: chacha_selftest.c,v 1.1 2020/07/25 22:46:34 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * Redistribution and use in source and binary forms, with or without
8 1.1 riastrad * modification, are permitted provided that the following conditions
9 1.1 riastrad * are met:
10 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
11 1.1 riastrad * notice, this list of conditions and the following disclaimer.
12 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
14 1.1 riastrad * documentation and/or other materials provided with the distribution.
15 1.1 riastrad *
16 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
27 1.1 riastrad */
28 1.1 riastrad
29 1.1 riastrad #include "chacha_impl.h"
30 1.1 riastrad
31 1.1 riastrad #ifdef _KERNEL
33 1.1 riastrad
34 1.1 riastrad #include <lib/libkern/libkern.h>
35 1.1 riastrad
36 1.1 riastrad #else
37 1.1 riastrad
38 1.1 riastrad #include <stdio.h>
39 1.1 riastrad #include <string.h>
40 1.1 riastrad
41 1.1 riastrad static void
42 1.1 riastrad hexdump(int (*prf)(const char *, ...) __printflike(1,2), const char *prefix,
43 1.1 riastrad const void *buf, size_t len)
44 1.1 riastrad {
45 1.1 riastrad const uint8_t *p = buf;
46 1.1 riastrad size_t i;
47 1.1 riastrad
48 1.1 riastrad (*prf)("%s (%zu bytes @ %p)\n", prefix, len, buf);
49 1.1 riastrad for (i = 0; i < len; i++) {
50 1.1 riastrad if (i % 16 == 8)
51 1.1 riastrad (*prf)(" ");
52 1.1 riastrad else
53 1.1 riastrad (*prf)(" ");
54 1.1 riastrad (*prf)("%02hhx", p[i]);
55 1.1 riastrad if ((i + 1) % 16 == 0)
56 1.1 riastrad (*prf)("\n");
57 1.1 riastrad }
58 1.1 riastrad if (i % 16)
59 1.1 riastrad (*prf)("\n");
60 1.1 riastrad }
61 1.1 riastrad
62 1.1 riastrad #endif
63 1.1 riastrad
64 1.1 riastrad /* https://tools.ietf.org/html/draft-strombergson-chacha-test-vectors-00 */
66 1.1 riastrad static int
67 1.1 riastrad chacha_core_selftest(const struct chacha_impl *ci)
68 1.1 riastrad {
69 1.1 riastrad /* TC1, 32-byte key, rounds=12, keystream block 1 */
70 1.1 riastrad static const uint8_t zero[32];
71 1.1 riastrad static const uint8_t expected0[64] = {
72 1.1 riastrad 0x9b,0xf4,0x9a,0x6a, 0x07,0x55,0xf9,0x53,
73 1.1 riastrad 0x81,0x1f,0xce,0x12, 0x5f,0x26,0x83,0xd5,
74 1.1 riastrad 0x04,0x29,0xc3,0xbb, 0x49,0xe0,0x74,0x14,
75 1.1 riastrad 0x7e,0x00,0x89,0xa5, 0x2e,0xae,0x15,0x5f,
76 1.1 riastrad 0x05,0x64,0xf8,0x79, 0xd2,0x7a,0xe3,0xc0,
77 1.1 riastrad 0x2c,0xe8,0x28,0x34, 0xac,0xfa,0x8c,0x79,
78 1.1 riastrad 0x3a,0x62,0x9f,0x2c, 0xa0,0xde,0x69,0x19,
79 1.1 riastrad 0x61,0x0b,0xe8,0x2f, 0x41,0x13,0x26,0xbe,
80 1.1 riastrad };
81 1.1 riastrad /* TC7, 32-byte key, rounds=12, keystream block 2 */
82 1.1 riastrad static const uint8_t k1[32] = {
83 1.1 riastrad 0x00,0x11,0x22,0x33, 0x44,0x55,0x66,0x77,
84 1.1 riastrad 0x88,0x99,0xaa,0xbb, 0xcc,0xdd,0xee,0xff,
85 1.1 riastrad 0xff,0xee,0xdd,0xcc, 0xbb,0xaa,0x99,0x88,
86 1.1 riastrad 0x77,0x66,0x55,0x44, 0x33,0x22,0x11,0x00,
87 1.1 riastrad };
88 1.1 riastrad static const uint8_t in1[16] = {
89 1.1 riastrad 0x01,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
90 1.1 riastrad 0x0f,0x1e,0x2d,0x3c, 0x4b,0x59,0x68,0x77,
91 1.1 riastrad };
92 1.1 riastrad static const uint8_t expected1[64] = {
93 1.1 riastrad 0xcd,0x9a,0x2a,0xa9, 0xea,0x93,0xc2,0x67,
94 1.1 riastrad 0x5e,0x82,0x88,0x14, 0x08,0xde,0x85,0x2c,
95 1.1 riastrad 0x62,0xfa,0x74,0x6a, 0x30,0xe5,0x2b,0x45,
96 1.1 riastrad 0xa2,0x69,0x62,0xcf, 0x43,0x51,0xe3,0x04,
97 1.1 riastrad 0xd3,0x13,0x20,0xbb, 0xd6,0xaa,0x6c,0xc8,
98 1.1 riastrad 0xf3,0x26,0x37,0xf9, 0x59,0x34,0xe4,0xc1,
99 1.1 riastrad 0x45,0xef,0xd5,0x62, 0x31,0xef,0x31,0x61,
100 1.1 riastrad 0x03,0x28,0x36,0xf4, 0x96,0x71,0x83,0x3e,
101 1.1 riastrad };
102 1.1 riastrad uint8_t out[64];
103 1.1 riastrad int result = 0;
104 1.1 riastrad
105 1.1 riastrad (*ci->ci_chacha_core)(out, zero, zero, chacha_const32, 12);
106 1.1 riastrad if (memcmp(out, expected0, 64)) {
107 1.1 riastrad hexdump(printf, "chacha core 1", out, sizeof out);
108 1.1 riastrad result = -1;
109 1.1 riastrad }
110 1.1 riastrad
111 1.1 riastrad (*ci->ci_chacha_core)(out, in1, k1, chacha_const32, 12);
112 1.1 riastrad if (memcmp(out, expected1, 64)) {
113 1.1 riastrad hexdump(printf, "chacha core 2", out, sizeof out);
114 1.1 riastrad result = -1;
115 1.1 riastrad }
116 1.1 riastrad
117 1.1 riastrad return result;
118 1.1 riastrad }
119 1.1 riastrad
120 1.1 riastrad static int
122 1.1 riastrad hchacha_selftest(const struct chacha_impl *ci)
123 1.1 riastrad {
124 1.1 riastrad /* https://tools.ietf.org/html/draft-irtf-cfrg-xchacha-03, 2.2.1 */
125 1.1 riastrad static const uint8_t k[32] = {
126 1.1 riastrad 0x00,0x01,0x02,0x03, 0x04,0x05,0x06,0x07,
127 1.1 riastrad 0x08,0x09,0x0a,0x0b, 0x0c,0x0d,0x0e,0x0f,
128 1.1 riastrad 0x10,0x11,0x12,0x13, 0x14,0x15,0x16,0x17,
129 1.1 riastrad 0x18,0x19,0x1a,0x1b, 0x1c,0x1d,0x1e,0x1f,
130 1.1 riastrad };
131 1.1 riastrad static const uint8_t in[16] = {
132 1.1 riastrad 0x00,0x00,0x00,0x09, 0x00,0x00,0x00,0x4a,
133 1.1 riastrad 0x00,0x00,0x00,0x00, 0x31,0x41,0x59,0x27,
134 1.1 riastrad };
135 1.1 riastrad static const uint8_t expected[32] = {
136 1.1 riastrad 0x82,0x41,0x3b,0x42, 0x27,0xb2,0x7b,0xfe,
137 1.1 riastrad 0xd3,0x0e,0x42,0x50, 0x8a,0x87,0x7d,0x73,
138 1.1 riastrad 0xa0,0xf9,0xe4,0xd5, 0x8a,0x74,0xa8,0x53,
139 1.1 riastrad 0xc1,0x2e,0xc4,0x13, 0x26,0xd3,0xec,0xdc,
140 1.1 riastrad };
141 1.1 riastrad uint8_t out[32];
142 1.1 riastrad int result = 0;
143 1.1 riastrad
144 1.1 riastrad (*ci->ci_hchacha)(out, in, k, chacha_const32, 20);
145 1.1 riastrad if (memcmp(out, expected, 32)) {
146 1.1 riastrad hexdump(printf, "hchacha", out, sizeof out);
147 1.1 riastrad result = -1;
148 1.1 riastrad }
149 1.1 riastrad
150 1.1 riastrad return result;
151 1.1 riastrad }
152 1.1 riastrad
153 1.1 riastrad static int
155 1.1 riastrad chacha_stream_selftest(const struct chacha_impl *ci)
156 1.1 riastrad {
157 1.1 riastrad
158 1.1 riastrad /* XXX */
159 1.1 riastrad return 0;
160 1.1 riastrad }
161 1.1 riastrad
162 1.1 riastrad static int
164 1.1 riastrad xchacha_stream_selftest(const struct chacha_impl *ci)
165 1.1 riastrad {
166 1.1 riastrad /* https://tools.ietf.org/html/draft-irtf-cfrg-xchacha-03, A.2.1 */
167 1.1 riastrad static const uint8_t k[32] = {
168 1.1 riastrad 0x80,0x81,0x82,0x83, 0x84,0x85,0x86,0x87,
169 1.1 riastrad 0x88,0x89,0x8a,0x8b, 0x8c,0x8d,0x8e,0x8f,
170 1.1 riastrad 0x90,0x91,0x92,0x93, 0x94,0x95,0x96,0x97,
171 1.1 riastrad 0x98,0x99,0x9a,0x9b, 0x9c,0x9d,0x9e,0x9f,
172 1.1 riastrad };
173 1.1 riastrad static const uint8_t nonce[24] = {
174 1.1 riastrad 0x40,0x41,0x42,0x43, 0x44,0x45,0x46,0x47,
175 1.1 riastrad 0x48,0x49,0x4a,0x4b, 0x4c,0x4d,0x4e,0x4f,
176 1.1 riastrad 0x50,0x51,0x52,0x53, 0x54,0x55,0x56,0x58,
177 1.1 riastrad };
178 1.1 riastrad static const uint8_t p[608] = {
179 1.1 riastrad 0x54,0x68,0x65,0x20, 0x64,0x68,0x6f,0x6c,
180 1.1 riastrad 0x65,0x20,0x28,0x70, 0x72,0x6f,0x6e,0x6f,
181 1.1 riastrad 0x75,0x6e,0x63,0x65, 0x64,0x20,0x22,0x64,
182 1.1 riastrad 0x6f,0x6c,0x65,0x22, 0x29,0x20,0x69,0x73,
183 1.1 riastrad 0x20,0x61,0x6c,0x73, 0x6f,0x20,0x6b,0x6e,
184 1.1 riastrad 0x6f,0x77,0x6e,0x20, 0x61,0x73,0x20,0x74,
185 1.1 riastrad 0x68,0x65,0x20,0x41, 0x73,0x69,0x61,0x74,
186 1.1 riastrad 0x69,0x63,0x20,0x77, 0x69,0x6c,0x64,0x20,
187 1.1 riastrad 0x64,0x6f,0x67,0x2c, 0x20,0x72,0x65,0x64,
188 1.1 riastrad 0x20,0x64,0x6f,0x67, 0x2c,0x20,0x61,0x6e,
189 1.1 riastrad 0x64,0x20,0x77,0x68, 0x69,0x73,0x74,0x6c,
190 1.1 riastrad 0x69,0x6e,0x67,0x20, 0x64,0x6f,0x67,0x2e,
191 1.1 riastrad 0x20,0x49,0x74,0x20, 0x69,0x73,0x20,0x61,
192 1.1 riastrad 0x62,0x6f,0x75,0x74, 0x20,0x74,0x68,0x65,
193 1.1 riastrad 0x20,0x73,0x69,0x7a, 0x65,0x20,0x6f,0x66,
194 1.1 riastrad 0x20,0x61,0x20,0x47, 0x65,0x72,0x6d,0x61,
195 1.1 riastrad 0x6e,0x20,0x73,0x68, 0x65,0x70,0x68,0x65,
196 1.1 riastrad 0x72,0x64,0x20,0x62, 0x75,0x74,0x20,0x6c,
197 1.1 riastrad 0x6f,0x6f,0x6b,0x73, 0x20,0x6d,0x6f,0x72,
198 1.1 riastrad 0x65,0x20,0x6c,0x69, 0x6b,0x65,0x20,0x61,
199 1.1 riastrad 0x20,0x6c,0x6f,0x6e, 0x67,0x2d,0x6c,0x65,
200 1.1 riastrad 0x67,0x67,0x65,0x64, 0x20,0x66,0x6f,0x78,
201 1.1 riastrad 0x2e,0x20,0x54,0x68, 0x69,0x73,0x20,0x68,
202 1.1 riastrad 0x69,0x67,0x68,0x6c, 0x79,0x20,0x65,0x6c,
203 1.1 riastrad 0x75,0x73,0x69,0x76, 0x65,0x20,0x61,0x6e,
204 1.1 riastrad 0x64,0x20,0x73,0x6b, 0x69,0x6c,0x6c,0x65,
205 1.1 riastrad 0x64,0x20,0x6a,0x75, 0x6d,0x70,0x65,0x72,
206 1.1 riastrad 0x20,0x69,0x73,0x20, 0x63,0x6c,0x61,0x73,
207 1.1 riastrad 0x73,0x69,0x66,0x69, 0x65,0x64,0x20,0x77,
208 1.1 riastrad 0x69,0x74,0x68,0x20, 0x77,0x6f,0x6c,0x76,
209 1.1 riastrad 0x65,0x73,0x2c,0x20, 0x63,0x6f,0x79,0x6f,
210 1.1 riastrad 0x74,0x65,0x73,0x2c, 0x20,0x6a,0x61,0x63,
211 1.1 riastrad 0x6b,0x61,0x6c,0x73, 0x2c,0x20,0x61,0x6e,
212 1.1 riastrad 0x64,0x20,0x66,0x6f, 0x78,0x65,0x73,0x20,
213 1.1 riastrad 0x69,0x6e,0x20,0x74, 0x68,0x65,0x20,0x74,
214 1.1 riastrad 0x61,0x78,0x6f,0x6e, 0x6f,0x6d,0x69,0x63,
215 1.1 riastrad 0x20,0x66,0x61,0x6d, 0x69,0x6c,0x79,0x20,
216 1.1 riastrad 0x43,0x61,0x6e,0x69, 0x64,0x61,0x65,0x2e,
217 1.1 riastrad
218 1.1 riastrad 0x54,0x68,0x65,0x20, 0x64,0x68,0x6f,0x6c,
219 1.1 riastrad 0x65,0x20,0x28,0x70, 0x72,0x6f,0x6e,0x6f,
220 1.1 riastrad 0x75,0x6e,0x63,0x65, 0x64,0x20,0x22,0x64,
221 1.1 riastrad 0x6f,0x6c,0x65,0x22, 0x29,0x20,0x69,0x73,
222 1.1 riastrad 0x20,0x61,0x6c,0x73, 0x6f,0x20,0x6b,0x6e,
223 1.1 riastrad 0x6f,0x77,0x6e,0x20, 0x61,0x73,0x20,0x74,
224 1.1 riastrad 0x68,0x65,0x20,0x41, 0x73,0x69,0x61,0x74,
225 1.1 riastrad 0x69,0x63,0x20,0x77, 0x69,0x6c,0x64,0x20,
226 1.1 riastrad 0x64,0x6f,0x67,0x2c, 0x20,0x72,0x65,0x64,
227 1.1 riastrad 0x20,0x64,0x6f,0x67, 0x2c,0x20,0x61,0x6e,
228 1.1 riastrad 0x64,0x20,0x77,0x68, 0x69,0x73,0x74,0x6c,
229 1.1 riastrad 0x69,0x6e,0x67,0x20, 0x64,0x6f,0x67,0x2e,
230 1.1 riastrad 0x20,0x49,0x74,0x20, 0x69,0x73,0x20,0x61,
231 1.1 riastrad 0x62,0x6f,0x75,0x74, 0x20,0x74,0x68,0x65,
232 1.1 riastrad 0x20,0x73,0x69,0x7a, 0x65,0x20,0x6f,0x66,
233 1.1 riastrad 0x20,0x61,0x20,0x47, 0x65,0x72,0x6d,0x61,
234 1.1 riastrad 0x6e,0x20,0x73,0x68, 0x65,0x70,0x68,0x65,
235 1.1 riastrad 0x72,0x64,0x20,0x62, 0x75,0x74,0x20,0x6c,
236 1.1 riastrad 0x6f,0x6f,0x6b,0x73, 0x20,0x6d,0x6f,0x72,
237 1.1 riastrad 0x65,0x20,0x6c,0x69, 0x6b,0x65,0x20,0x61,
238 1.1 riastrad 0x20,0x6c,0x6f,0x6e, 0x67,0x2d,0x6c,0x65,
239 1.1 riastrad 0x67,0x67,0x65,0x64, 0x20,0x66,0x6f,0x78,
240 1.1 riastrad 0x2e,0x20,0x54,0x68, 0x69,0x73,0x20,0x68,
241 1.1 riastrad 0x69,0x67,0x68,0x6c, 0x79,0x20,0x65,0x6c,
242 1.1 riastrad 0x75,0x73,0x69,0x76, 0x65,0x20,0x61,0x6e,
243 1.1 riastrad 0x64,0x20,0x73,0x6b, 0x69,0x6c,0x6c,0x65,
244 1.1 riastrad 0x64,0x20,0x6a,0x75, 0x6d,0x70,0x65,0x72,
245 1.1 riastrad 0x20,0x69,0x73,0x20, 0x63,0x6c,0x61,0x73,
246 1.1 riastrad 0x73,0x69,0x66,0x69, 0x65,0x64,0x20,0x77,
247 1.1 riastrad 0x69,0x74,0x68,0x20, 0x77,0x6f,0x6c,0x76,
248 1.1 riastrad 0x65,0x73,0x2c,0x20, 0x63,0x6f,0x79,0x6f,
249 1.1 riastrad 0x74,0x65,0x73,0x2c, 0x20,0x6a,0x61,0x63,
250 1.1 riastrad 0x6b,0x61,0x6c,0x73, 0x2c,0x20,0x61,0x6e,
251 1.1 riastrad 0x64,0x20,0x66,0x6f, 0x78,0x65,0x73,0x20,
252 1.1 riastrad 0x69,0x6e,0x20,0x74, 0x68,0x65,0x20,0x74,
253 1.1 riastrad 0x61,0x78,0x6f,0x6e, 0x6f,0x6d,0x69,0x63,
254 1.1 riastrad 0x20,0x66,0x61,0x6d, 0x69,0x6c,0x79,0x20,
255 1.1 riastrad 0x43,0x61,0x6e,0x69, 0x64,0x61,0x65,0x2e,
256 1.1 riastrad };
257 1.1 riastrad static const uint8_t expected[608] = {
258 1.1 riastrad 0x45,0x59,0xab,0xba, 0x4e,0x48,0xc1,0x61,
259 1.1 riastrad 0x02,0xe8,0xbb,0x2c, 0x05,0xe6,0x94,0x7f,
260 1.1 riastrad 0x50,0xa7,0x86,0xde, 0x16,0x2f,0x9b,0x0b,
261 1.1 riastrad 0x7e,0x59,0x2a,0x9b, 0x53,0xd0,0xd4,0xe9,
262 1.1 riastrad 0x8d,0x8d,0x64,0x10, 0xd5,0x40,0xa1,0xa6,
263 1.1 riastrad 0x37,0x5b,0x26,0xd8, 0x0d,0xac,0xe4,0xfa,
264 1.1 riastrad 0xb5,0x23,0x84,0xc7, 0x31,0xac,0xbf,0x16,
265 1.1 riastrad 0xa5,0x92,0x3c,0x0c, 0x48,0xd3,0x57,0x5d,
266 1.1 riastrad 0x4d,0x0d,0x2c,0x67, 0x3b,0x66,0x6f,0xaa,
267 1.1 riastrad 0x73,0x10,0x61,0x27, 0x77,0x01,0x09,0x3a,
268 1.1 riastrad 0x6b,0xf7,0xa1,0x58, 0xa8,0x86,0x42,0x92,
269 1.1 riastrad 0xa4,0x1c,0x48,0xe3, 0xa9,0xb4,0xc0,0xda,
270 1.1 riastrad 0xec,0xe0,0xf8,0xd9, 0x8d,0x0d,0x7e,0x05,
271 1.1 riastrad 0xb3,0x7a,0x30,0x7b, 0xbb,0x66,0x33,0x31,
272 1.1 riastrad 0x64,0xec,0x9e,0x1b, 0x24,0xea,0x0d,0x6c,
273 1.1 riastrad 0x3f,0xfd,0xdc,0xec, 0x4f,0x68,0xe7,0x44,
274 1.1 riastrad 0x30,0x56,0x19,0x3a, 0x03,0xc8,0x10,0xe1,
275 1.1 riastrad 0x13,0x44,0xca,0x06, 0xd8,0xed,0x8a,0x2b,
276 1.1 riastrad 0xfb,0x1e,0x8d,0x48, 0xcf,0xa6,0xbc,0x0e,
277 1.1 riastrad 0xb4,0xe2,0x46,0x4b, 0x74,0x81,0x42,0x40,
278 1.1 riastrad 0x7c,0x9f,0x43,0x1a, 0xee,0x76,0x99,0x60,
279 1.1 riastrad 0xe1,0x5b,0xa8,0xb9, 0x68,0x90,0x46,0x6e,
280 1.1 riastrad 0xf2,0x45,0x75,0x99, 0x85,0x23,0x85,0xc6,
281 1.1 riastrad 0x61,0xf7,0x52,0xce, 0x20,0xf9,0xda,0x0c,
282 1.1 riastrad 0x09,0xab,0x6b,0x19, 0xdf,0x74,0xe7,0x6a,
283 1.1 riastrad 0x95,0x96,0x74,0x46, 0xf8,0xd0,0xfd,0x41,
284 1.1 riastrad 0x5e,0x7b,0xee,0x2a, 0x12,0xa1,0x14,0xc2,
285 1.1 riastrad 0x0e,0xb5,0x29,0x2a, 0xe7,0xa3,0x49,0xae,
286 1.1 riastrad 0x57,0x78,0x20,0xd5, 0x52,0x0a,0x1f,0x3f,
287 1.1 riastrad 0xb6,0x2a,0x17,0xce, 0x6a,0x7e,0x68,0xfa,
288 1.1 riastrad 0x7c,0x79,0x11,0x1d, 0x88,0x60,0x92,0x0b,
289 1.1 riastrad 0xc0,0x48,0xef,0x43, 0xfe,0x84,0x48,0x6c,
290 1.1 riastrad 0xcb,0x87,0xc2,0x5f, 0x0a,0xe0,0x45,0xf0,
291 1.1 riastrad 0xcc,0xe1,0xe7,0x98, 0x9a,0x9a,0xa2,0x20,
292 1.1 riastrad 0xa2,0x8b,0xdd,0x48, 0x27,0xe7,0x51,0xa2,
293 1.1 riastrad 0x4a,0x6d,0x5c,0x62, 0xd7,0x90,0xa6,0x63,
294 1.1 riastrad 0x93,0xb9,0x31,0x11, 0xc1,0xa5,0x5d,0xd7,
295 1.1 riastrad 0x42,0x1a,0x10,0x18, 0x49,0x74,0xc7,0xc5,
296 1.1 riastrad
297 1.1 riastrad 0x08,0x38,0x2d,0x64, 0x35,0x8d,0x21,0x77,
298 1.1 riastrad 0x2e,0xb9,0x73,0xa8, 0x8f,0xb6,0x2b,0xf8,
299 1.1 riastrad 0xce,0xfa,0xb4,0xca, 0x6f,0x0c,0x26,0xbb,
300 1.1 riastrad 0x7f,0xd6,0x6d,0xb2, 0xa0,0xbe,0xb0,0x5a,
301 1.1 riastrad 0x1a,0x6e,0x39,0xcb, 0xd5,0xda,0xf2,0xfc,
302 1.1 riastrad 0x0b,0x74,0x31,0x3d, 0x2e,0xcd,0x5f,0x94,
303 1.1 riastrad 0xc2,0x9f,0x30,0xdb, 0x11,0x5e,0x41,0x53,
304 1.1 riastrad 0x8c,0x6d,0x30,0xba, 0x97,0xa0,0xc5,0x07,
305 1.1 riastrad 0x70,0x78,0x02,0x5a, 0xc1,0x69,0x70,0x8f,
306 1.1 riastrad 0x22,0x85,0xcb,0x98, 0xbc,0x6a,0x51,0xfb,
307 1.1 riastrad 0xc6,0xa7,0xc3,0x3d, 0x76,0xe4,0x93,0x9a,
308 1.1 riastrad 0x21,0xe2,0xc6,0x12, 0xe1,0x3a,0xcc,0xfb,
309 1.1 riastrad 0x6f,0xa6,0x57,0xc0, 0x09,0x8c,0x6f,0xf3,
310 1.1 riastrad 0x8d,0x83,0x21,0x1b, 0x71,0xa9,0xc1,0x93,
311 1.1 riastrad 0x88,0x35,0xfc,0x18, 0x1f,0x94,0xa2,0x57,
312 1.1 riastrad 0x3a,0x4e,0xd0,0xc0, 0xbc,0x92,0xa7,0x9c,
313 1.1 riastrad 0x52,0x8a,0x82,0x9d, 0x44,0x75,0x7b,0xa0,
314 1.1 riastrad 0xcf,0x3d,0x2d,0xbf, 0xf9,0x6f,0x71,0x56,
315 1.1 riastrad 0x38,0xb0,0x63,0x5e, 0x55,0xcd,0x28,0x12,
316 1.1 riastrad 0xc5,0xea,0x52,0xf4, 0xdc,0xf7,0xdc,0x3d,
317 1.1 riastrad 0xd8,0x96,0x09,0xe8, 0x2a,0xcc,0x00,0x16,
318 1.1 riastrad 0x88,0x77,0x82,0x10, 0xed,0x7d,0xd8,0x8b,
319 1.1 riastrad 0xf5,0xd3,0xe1,0xfc, 0x49,0x66,0x36,0x8d,
320 1.1 riastrad 0x55,0xd2,0x33,0xb8, 0x6d,0xff,0xe3,0xd3,
321 1.1 riastrad 0x55,0x80,0x0e,0xd8, 0x95,0x32,0x32,0x55,
322 1.1 riastrad 0x83,0xe7,0x58,0x6f, 0xec,0xc3,0x8c,0xf8,
323 1.1 riastrad 0x52,0x16,0xdc,0x0d, 0x29,0x02,0xe5,0x27,
324 1.1 riastrad 0x35,0xc2,0xbb,0xe2, 0xe2,0x3b,0xf5,0x19,
325 1.1 riastrad 0xcd,0x44,0x83,0xe8, 0x21,0x55,0xd0,0x10,
326 1.1 riastrad 0x15,0x68,0x8e,0x46, 0xa3,0x2f,0xa5,0x7c,
327 1.1 riastrad 0xa8,0x2c,0xc6,0x8f, 0x14,0xcd,0xb3,0x79,
328 1.1 riastrad 0x92,0x32,0x71,0xac, 0xd9,0xaf,0x9c,0x4d,
329 1.1 riastrad 0x00,0x88,0xd1,0x42, 0xd5,0x23,0xfa,0xe6,
330 1.1 riastrad 0x7f,0x38,0xa2,0x56, 0x99,0xbe,0x6f,0xcf,
331 1.1 riastrad 0xe0,0xaa,0x44,0x11, 0x8a,0xc8,0x3a,0x99,
332 1.1 riastrad 0x48,0x6d,0x33,0x0e, 0x94,0xf2,0xb9,0x87,
333 1.1 riastrad 0xed,0x4f,0x6a,0x9c, 0x33,0x93,0x6d,0xe4,
334 1.1 riastrad 0x92,0x76,0xab,0xfa, 0xce,0x5b,0x17,0x14,
335 1.1 riastrad };
336 1.1 riastrad uint8_t c[608];
337 1.1 riastrad unsigned i;
338 1.1 riastrad int result = 0;
339 1.1 riastrad
340 1.1 riastrad /*
341 1.1 riastrad * 608 = 96 (mod 256)
342 1.1 riastrad * 607 = 95 (mod 256), = 7 (mod 8)
343 1.1 riastrad * 543 = 31 (mod 256), = 7 (mod 8)
344 1.1 riastrad * 511 = 255 (mod 256), = 7 (mod 8)
345 1.1 riastrad *
346 1.1 riastrad * This exercises several branches when there are special cases
347 1.1 riastrad * for integral numbers of 4-byte words, integral numbers of
348 1.1 riastrad * 64-byte blocks, and integral numbers of 256-byte chunks.
349 1.1 riastrad */
350 1.1 riastrad
351 1.1 riastrad (*ci->ci_xchacha_stream)(c, 608, 0, nonce, k, 20);
352 1.1 riastrad for (i = 0; i < 608; i++)
353 1.1 riastrad c[i] ^= p[i];
354 1.1 riastrad if (memcmp(c, expected, 608)) {
355 1.1 riastrad for (i = 0; i < 608; i++)
356 1.1 riastrad c[i] ^= p[i];
357 1.1 riastrad hexdump(printf, "xchacha_stream", c, 608);
358 1.1 riastrad for (i = 0; i < 608; i++)
359 1.1 riastrad c[i] = expected[i] ^ p[i];
360 1.1 riastrad hexdump(printf, "expected", c, 608);
361 1.1 riastrad result = -1;
362 1.1 riastrad }
363 1.1 riastrad
364 1.1 riastrad (*ci->ci_xchacha_stream)(c, 607, 0, nonce, k, 20);
365 1.1 riastrad for (i = 0; i < 607; i++)
366 1.1 riastrad c[i] ^= p[i];
367 1.1 riastrad if (memcmp(c, expected, 607)) {
368 1.1 riastrad for (i = 0; i < 607; i++)
369 1.1 riastrad c[i] ^= p[i];
370 1.1 riastrad hexdump(printf, "xchacha_stream", c, 607);
371 1.1 riastrad for (i = 0; i < 607; i++)
372 1.1 riastrad c[i] = expected[i] ^ p[i];
373 1.1 riastrad hexdump(printf, "expected", c, 607);
374 1.1 riastrad result = -1;
375 1.1 riastrad }
376 1.1 riastrad
377 1.1 riastrad (*ci->ci_xchacha_stream)(c, 543, 0, nonce, k, 20);
378 1.1 riastrad for (i = 0; i < 543; i++)
379 1.1 riastrad c[i] ^= p[i];
380 1.1 riastrad if (memcmp(c, expected, 543)) {
381 1.1 riastrad for (i = 0; i < 543; i++)
382 1.1 riastrad c[i] ^= p[i];
383 1.1 riastrad hexdump(printf, "xchacha_stream", c, 543);
384 1.1 riastrad for (i = 0; i < 543; i++)
385 1.1 riastrad c[i] = expected[i] ^ p[i];
386 1.1 riastrad hexdump(printf, "expected", c, 543);
387 1.1 riastrad result = -1;
388 1.1 riastrad }
389 1.1 riastrad
390 1.1 riastrad (*ci->ci_xchacha_stream)(c, 511, 0, nonce, k, 20);
391 1.1 riastrad for (i = 0; i < 511; i++)
392 1.1 riastrad c[i] ^= p[i];
393 1.1 riastrad if (memcmp(c, expected, 511)) {
394 1.1 riastrad for (i = 0; i < 511; i++)
395 1.1 riastrad c[i] ^= p[i];
396 1.1 riastrad hexdump(printf, "xchacha_stream", c, 511);
397 1.1 riastrad for (i = 0; i < 511; i++)
398 1.1 riastrad c[i] = expected[i] ^ p[i];
399 1.1 riastrad hexdump(printf, "expected", c, 511);
400 1.1 riastrad result = -1;
401 1.1 riastrad }
402 1.1 riastrad
403 1.1 riastrad (*ci->ci_xchacha_stream)(c, 63, 0, nonce, k, 20);
404 1.1 riastrad for (i = 0; i < 63; i++)
405 1.1 riastrad c[i] ^= p[i];
406 1.1 riastrad if (memcmp(c, expected, 63)) {
407 1.1 riastrad for (i = 0; i < 63; i++)
408 1.1 riastrad c[i] ^= p[i];
409 1.1 riastrad hexdump(printf, "xchacha_stream", c, 63);
410 1.1 riastrad for (i = 0; i < 63; i++)
411 1.1 riastrad c[i] = expected[i] ^ p[i];
412 1.1 riastrad hexdump(printf, "expected", c, 63);
413 1.1 riastrad result = -1;
414 1.1 riastrad }
415 1.1 riastrad
416 1.1 riastrad (*ci->ci_xchacha_stream_xor)(c, p, 608, 0, nonce, k, 20);
417 1.1 riastrad if (memcmp(c, expected, 608)) {
418 1.1 riastrad hexdump(printf, "xchacha_stream_xor", c, 608);
419 1.1 riastrad hexdump(printf, "expected", expected, 608);
420 1.1 riastrad result = -1;
421 1.1 riastrad }
422 1.1 riastrad
423 1.1 riastrad memset(c, 0, sizeof c);
424 1.1 riastrad (*ci->ci_xchacha_stream_xor)(c, p, 607, 0, nonce, k, 20);
425 1.1 riastrad if (memcmp(c, expected, 607)) {
426 1.1 riastrad hexdump(printf, "xchacha_stream_xor", c, 607);
427 1.1 riastrad hexdump(printf, "expected", expected, 607);
428 1.1 riastrad result = -1;
429 1.1 riastrad }
430 1.1 riastrad
431 1.1 riastrad memset(c, 0, sizeof c);
432 1.1 riastrad (*ci->ci_xchacha_stream_xor)(c, p, 543, 0, nonce, k, 20);
433 1.1 riastrad if (memcmp(c, expected, 543)) {
434 1.1 riastrad hexdump(printf, "xchacha_stream_xor", c, 543);
435 1.1 riastrad hexdump(printf, "expected", expected, 543);
436 1.1 riastrad result = -1;
437 1.1 riastrad }
438 1.1 riastrad
439 1.1 riastrad memset(c, 0, sizeof c);
440 1.1 riastrad (*ci->ci_xchacha_stream_xor)(c, p, 511, 0, nonce, k, 20);
441 1.1 riastrad if (memcmp(c, expected, 511)) {
442 1.1 riastrad hexdump(printf, "xchacha_stream_xor", c, 511);
443 1.1 riastrad hexdump(printf, "expected", expected, 511);
444 1.1 riastrad result = -1;
445 1.1 riastrad }
446 1.1 riastrad
447 1.1 riastrad memset(c, 0, sizeof c);
448 1.1 riastrad (*ci->ci_xchacha_stream_xor)(c, p, 63, 0, nonce, k, 20);
449 1.1 riastrad if (memcmp(c, expected, 63)) {
450 1.1 riastrad hexdump(printf, "xchacha_stream_xor", c, 63);
451 1.1 riastrad hexdump(printf, "expected", expected, 63);
452 1.1 riastrad result = -1;
453 1.1 riastrad }
454 1.1 riastrad
455 1.1 riastrad return result;
456 1.1 riastrad }
457 1.1 riastrad
458 1.1 riastrad int
460 1.1 riastrad chacha_selftest(const struct chacha_impl *ci)
461 1.1 riastrad {
462 1.1 riastrad int result = 0;
463 1.1 riastrad
464 1.1 riastrad result |= chacha_core_selftest(ci);
465 result |= chacha_stream_selftest(ci);
466 result |= hchacha_selftest(ci);
467 result |= xchacha_stream_selftest(ci);
468
469 return result;
470 }
471