1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright (c) 2015, Joyent, Inc.
14  */
15 
16 /*
17  * Test chacha20 key stream with known TVs.
18  *
19  * Based on data from:
20  * https://tools.ietf.org/html/draft-strombergson-chacha-test-vectors-01
21  */
22 
23 #include <strings.h>
24 #include <chacha.h>
25 #include <assert.h>
26 
27 /* Test Vector 1 */
28 
29 static uint8_t cha_key1[16] = {
30 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
31 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
32 };
33 static uint8_t cha_iv1[8] = {
34 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
35 };
36 static uint8_t cha_fbl1[] = {
37 	0x89, 0x67, 0x09, 0x52, 0x60, 0x83, 0x64, 0xfd,
38 	0x00, 0xb2, 0xf9, 0x09, 0x36, 0xf0, 0x31, 0xc8,
39 	0xe7, 0x56, 0xe1, 0x5d, 0xba, 0x04, 0xb8, 0x49,
40 	0x3d, 0x00, 0x42, 0x92, 0x59, 0xb2, 0x0f, 0x46,
41 	0xcc, 0x04, 0xf1, 0x11, 0x24, 0x6b, 0x6c, 0x2c,
42 	0xe0, 0x66, 0xbe, 0x3b, 0xfb, 0x32, 0xd9, 0xaa,
43 	0x0f, 0xdd, 0xfb, 0xc1, 0x21, 0x23, 0xd4, 0xb9,
44 	0xe4, 0x4f, 0x34, 0xdc, 0xa0, 0x5a, 0x10, 0x3f
45 };
46 static uint8_t cha_sbl1[] = {
47 	0x6c, 0xd1, 0x35, 0xc2, 0x87, 0x8c, 0x83, 0x2b,
48 	0x58, 0x96, 0xb1, 0x34, 0xf6, 0x14, 0x2a, 0x9d,
49 	0x4d, 0x8d, 0x0d, 0x8f, 0x10, 0x26, 0xd2, 0x0a,
50 	0x0a, 0x81, 0x51, 0x2c, 0xbc, 0xe6, 0xe9, 0x75,
51 	0x8a, 0x71, 0x43, 0xd0, 0x21, 0x97, 0x80, 0x22,
52 	0xa3, 0x84, 0x14, 0x1a, 0x80, 0xce, 0xa3, 0x06,
53 	0x2f, 0x41, 0xf6, 0x7a, 0x75, 0x2e, 0x66, 0xad,
54 	0x34, 0x11, 0x98, 0x4c, 0x78, 0x7e, 0x30, 0xad
55 };
56 
57 /* Test Vector 2 */
58 static uint8_t cha_key2[16] = {
59 	0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
61 };
62 static uint8_t cha_iv2[8] = {
63 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
64 };
65 
66 static uint8_t cha_fbl2[] = {
67 	0xae, 0x56, 0x06, 0x0d, 0x04, 0xf5, 0xb5, 0x97,
68 	0x89, 0x7f, 0xf2, 0xaf, 0x13, 0x88, 0xdb, 0xce,
69 	0xff, 0x5a, 0x2a, 0x49, 0x20, 0x33, 0x5d, 0xc1,
70 	0x7a, 0x3c, 0xb1, 0xb1, 0xb1, 0x0f, 0xbe, 0x70,
71 	0xec, 0xe8, 0xf4, 0x86, 0x4d, 0x8c, 0x7c, 0xdf,
72 	0x00, 0x76, 0x45, 0x3a, 0x82, 0x91, 0xc7, 0xdb,
73 	0xeb, 0x3a, 0xa9, 0xc9, 0xd1, 0x0e, 0x8c, 0xa3,
74 	0x6b, 0xe4, 0x44, 0x93, 0x76, 0xed, 0x7c, 0x42
75 };
76 
77 static uint8_t cha_sbl2[] = {
78 	0xfc, 0x3d, 0x47, 0x1c, 0x34, 0xa3, 0x6f, 0xbb,
79 	0xf6, 0x16, 0xbc, 0x0a, 0x0e, 0x7c, 0x52, 0x30,
80 	0x30, 0xd9, 0x44, 0xf4, 0x3e, 0xc3, 0xe7, 0x8d,
81 	0xd6, 0xa1, 0x24, 0x66, 0x54, 0x7c, 0xb4, 0xf7,
82 	0xb3, 0xce, 0xbd, 0x0a, 0x50, 0x05, 0xe7, 0x62,
83 	0xe5, 0x62, 0xd1, 0x37, 0x5b, 0x7a, 0xc4, 0x45,
84 	0x93, 0xa9, 0x91, 0xb8, 0x5d, 0x1a, 0x60, 0xfb,
85 	0xa2, 0x03, 0x5d, 0xfa, 0xa2, 0xa6, 0x42, 0xd5
86 };
87 
88 /* Test Vector 3 */
89 static uint8_t cha_key3[] = {
90 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
92 };
93 static uint8_t cha_iv3[] = {
94 	0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
95 };
96 
97 static uint8_t cha_fbl3[] = {
98 	0x16, 0x63, 0x87, 0x9e, 0xb3, 0xf2, 0xc9, 0x94,
99 	0x9e, 0x23, 0x88, 0xca, 0xa3, 0x43, 0xd3, 0x61,
100 	0xbb, 0x13, 0x27, 0x71, 0x24, 0x5a, 0xe6, 0xd0,
101 	0x27, 0xca, 0x9c, 0xb0, 0x10, 0xdc, 0x1f, 0xa7,
102 	0x17, 0x8d, 0xc4, 0x1f, 0x82, 0x78, 0xbc, 0x1f,
103 	0x64, 0xb3, 0xf1, 0x27, 0x69, 0xa2, 0x40, 0x97,
104 	0xf4, 0x0d, 0x63, 0xa8, 0x63, 0x66, 0xbd, 0xb3,
105 	0x6a, 0xc0, 0x8a, 0xbe, 0x60, 0xc0, 0x7f, 0xe8
106 };
107 
108 static uint8_t cha_sbl3[] = {
109 	0xb0, 0x57, 0x37, 0x5c, 0x89, 0x14, 0x44, 0x08,
110 	0xcc, 0x74, 0x46, 0x24, 0xf6, 0x9f, 0x7f, 0x4c,
111 	0xcb, 0xd9, 0x33, 0x66, 0xc9, 0x2f, 0xc4, 0xdf,
112 	0xca, 0xda, 0x65, 0xf1, 0xb9, 0x59, 0xd8, 0xc6,
113 	0x4d, 0xfc, 0x50, 0xde, 0x71, 0x1f, 0xb4, 0x64,
114 	0x16, 0xc2, 0x55, 0x3c, 0xc6, 0x0f, 0x21, 0xbb,
115 	0xfd, 0x00, 0x64, 0x91, 0xcb, 0x17, 0x88, 0x8b,
116 	0x4f, 0xb3, 0x52, 0x1c, 0x4f, 0xdd, 0x87, 0x45
117 };
118 
119 /* Test Vector 4 */
120 static uint8_t cha_key4[] = {
121 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
122 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
123 };
124 
125 static uint8_t cha_iv4[] = {
126 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
127 };
128 
129 static uint8_t cha_fbl4[] = {
130 	0x99, 0x29, 0x47, 0xc3, 0x96, 0x61, 0x26, 0xa0,
131 	0xe6, 0x60, 0xa3, 0xe9, 0x5d, 0xb0, 0x48, 0xde,
132 	0x09, 0x1f, 0xb9, 0xe0, 0x18, 0x5b, 0x1e, 0x41,
133 	0xe4, 0x10, 0x15, 0xbb, 0x7e, 0xe5, 0x01, 0x50,
134 	0x39, 0x9e, 0x47, 0x60, 0xb2, 0x62, 0xf9, 0xd5,
135 	0x3f, 0x26, 0xd8, 0xdd, 0x19, 0xe5, 0x6f, 0x5c,
136 	0x50, 0x6a, 0xe0, 0xc3, 0x61, 0x9f, 0xa6, 0x7f,
137 	0xb0, 0xc4, 0x08, 0x10, 0x6d, 0x02, 0x03, 0xee
138 };
139 
140 static uint8_t cha_sbl4[] = {
141 	0x40, 0xea, 0x3c, 0xfa, 0x61, 0xfa, 0x32, 0xa2,
142 	0xfd, 0xa8, 0xd1, 0x23, 0x8a, 0x21, 0x35, 0xd9,
143 	0xd4, 0x17, 0x87, 0x75, 0x24, 0x0f, 0x99, 0x00,
144 	0x70, 0x64, 0xa6, 0xa7, 0xf0, 0xc7, 0x31, 0xb6,
145 	0x7c, 0x22, 0x7c, 0x52, 0xef, 0x79, 0x6b, 0x6b,
146 	0xed, 0x9f, 0x90, 0x59, 0xba, 0x06, 0x14, 0xbc,
147 	0xf6, 0xdd, 0x6e, 0x38, 0x91, 0x7f, 0x3b, 0x15,
148 	0x0e, 0x57, 0x63, 0x75, 0xbe, 0x50, 0xed, 0x67
149 };
150 
151 /* Test Vector 5 */
152 static uint8_t cha_key5[] = {
153 	0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
154 	0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55
155 };
156 static uint8_t cha_iv5[] = {
157 	0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55
158 };
159 
160 static uint8_t cha_fbl5[] = {
161 	0x35, 0x7d, 0x7d, 0x94, 0xf9, 0x66, 0x77, 0x8f,
162 	0x58, 0x15, 0xa2, 0x05, 0x1d, 0xcb, 0x04, 0x13,
163 	0x3b, 0x26, 0xb0, 0xea, 0xd9, 0xf5, 0x7d, 0xd0,
164 	0x99, 0x27, 0x83, 0x7b, 0xc3, 0x06, 0x7e, 0x4b,
165 	0x6b, 0xf2, 0x99, 0xad, 0x81, 0xf7, 0xf5, 0x0c,
166 	0x8d, 0xa8, 0x3c, 0x78, 0x10, 0xbf, 0xc1, 0x7b,
167 	0xb6, 0xf4, 0x81, 0x3a, 0xb6, 0xc3, 0x26, 0x95,
168 	0x70, 0x45, 0xfd, 0x3f, 0xd5, 0xe1, 0x99, 0x15
169 };
170 
171 static uint8_t cha_sbl5[] = {
172 	0xec, 0x74, 0x4a, 0x6b, 0x9b, 0xf8, 0xcb, 0xdc,
173 	0xb3, 0x6d, 0x8b, 0x6a, 0x54, 0x99, 0xc6, 0x8a,
174 	0x08, 0xef, 0x7b, 0xe6, 0xcc, 0x1e, 0x93, 0xf2,
175 	0xf5, 0xbc, 0xd2, 0xca, 0xd4, 0xe4, 0x7c, 0x18,
176 	0xa3, 0xe5, 0xd9, 0x4b, 0x56, 0x66, 0x38, 0x2c,
177 	0x6d, 0x13, 0x0d, 0x82, 0x2d, 0xd5, 0x6a, 0xac,
178 	0xb0, 0xf8, 0x19, 0x52, 0x78, 0xe7, 0xb2, 0x92,
179 	0x49, 0x5f, 0x09, 0x86, 0x8d, 0xdf, 0x12, 0xcc
180 };
181 
182 /* Test Vector 6 */
183 
184 static uint8_t cha_key6[] = {
185 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
186 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
187 };
188 static uint8_t cha_iv6[] = {
189 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
190 };
191 
192 static uint8_t cha_fbl6[] = {
193 	0xfc, 0x79, 0xac, 0xbd, 0x58, 0x52, 0x61, 0x03,
194 	0x86, 0x27, 0x76, 0xaa, 0xb2, 0x0f, 0x3b, 0x7d,
195 	0x8d, 0x31, 0x49, 0xb2, 0xfa, 0xb6, 0x57, 0x66,
196 	0x29, 0x93, 0x16, 0xb6, 0xe5, 0xb1, 0x66, 0x84,
197 	0xde, 0x5d, 0xe5, 0x48, 0xc1, 0xb7, 0xd0, 0x83,
198 	0xef, 0xd9, 0xe3, 0x05, 0x23, 0x19, 0xe0, 0xc6,
199 	0x25, 0x41, 0x41, 0xda, 0x04, 0xa6, 0x58, 0x6d,
200 	0xf8, 0x00, 0xf6, 0x4d, 0x46, 0xb0, 0x1c, 0x87
201 };
202 
203 static uint8_t cha_sbl6[] = {
204 	0x1f, 0x05, 0xbc, 0x67, 0xe0, 0x76, 0x28, 0xeb,
205 	0xe6, 0xf6, 0x86, 0x5a, 0x21, 0x77, 0xe0, 0xb6,
206 	0x6a, 0x55, 0x8a, 0xa7, 0xcc, 0x1e, 0x8f, 0xf1,
207 	0xa9, 0x8d, 0x27, 0xf7, 0x07, 0x1f, 0x83, 0x35,
208 	0xef, 0xce, 0x45, 0x37, 0xbb, 0x0e, 0xf7, 0xb5,
209 	0x73, 0xb3, 0x2f, 0x32, 0x76, 0x5f, 0x29, 0x00,
210 	0x7d, 0xa5, 0x3b, 0xba, 0x62, 0xe7, 0xa4, 0x4d,
211 	0x00, 0x6f, 0x41, 0xeb, 0x28, 0xfe, 0x15, 0xd6
212 };
213 
214 /* Test Vector 7 */
215 static uint8_t cha_key7[] = {
216 	0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
217 	0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
218 };
219 static uint8_t cha_iv7[] = {
220 	0x0f, 0x1e, 0x2d, 0x3c, 0x4b, 0x5a, 0x69, 0x78
221 };
222 
223 static uint8_t cha_fbl7[] = {
224 	0xd1, 0xab, 0xf6, 0x30, 0x46, 0x7e, 0xb4, 0xf6,
225 	0x7f, 0x1c, 0xfb, 0x47, 0xcd, 0x62, 0x6a, 0xae,
226 	0x8a, 0xfe, 0xdb, 0xbe, 0x4f, 0xf8, 0xfc, 0x5f,
227 	0xe9, 0xcf, 0xae, 0x30, 0x7e, 0x74, 0xed, 0x45,
228 	0x1f, 0x14, 0x04, 0x42, 0x5a, 0xd2, 0xb5, 0x45,
229 	0x69, 0xd5, 0xf1, 0x81, 0x48, 0x93, 0x99, 0x71,
230 	0xab, 0xb8, 0xfa, 0xfc, 0x88, 0xce, 0x4a, 0xc7,
231 	0xfe, 0x1c, 0x3d, 0x1f, 0x7a, 0x1e, 0xb7, 0xca
232 };
233 
234 static uint8_t cha_sbl7[] = {
235 	0xe7, 0x6c, 0xa8, 0x7b, 0x61, 0xa9, 0x71, 0x35,
236 	0x41, 0x49, 0x77, 0x60, 0xdd, 0x9a, 0xe0, 0x59,
237 	0x35, 0x0c, 0xad, 0x0d, 0xce, 0xdf, 0xaa, 0x80,
238 	0xa8, 0x83, 0x11, 0x9a, 0x1a, 0x6f, 0x98, 0x7f,
239 	0xd1, 0xce, 0x91, 0xfd, 0x8e, 0xe0, 0x82, 0x80,
240 	0x34, 0xb4, 0x11, 0x20, 0x0a, 0x97, 0x45, 0xa2,
241 	0x85, 0x55, 0x44, 0x75, 0xd1, 0x2a, 0xfc, 0x04,
242 	0x88, 0x7f, 0xef, 0x35, 0x16, 0xd1, 0x2a, 0x2c
243 };
244 
245 /* Test Vector 8 */
246 static uint8_t cha_key8[] = {
247 	0xc4, 0x6e, 0xc1, 0xb1, 0x8c, 0xe8, 0xa8, 0x78,
248 	0x72, 0x5a, 0x37, 0xe7, 0x80, 0xdf, 0xb7, 0x35
249 };
250 static uint8_t cha_iv8[] = {
251 	0x1a, 0xda, 0x31, 0xd5, 0xcf, 0x68, 0x82, 0x21
252 };
253 
254 static uint8_t cha_fbl8[] = {
255 	0x82, 0x6a, 0xbd, 0xd8, 0x44, 0x60, 0xe2, 0xe9,
256 	0x34, 0x9f, 0x0e, 0xf4, 0xaf, 0x5b, 0x17, 0x9b,
257 	0x42, 0x6e, 0x4b, 0x2d, 0x10, 0x9a, 0x9c, 0x5b,
258 	0xb4, 0x40, 0x00, 0xae, 0x51, 0xbe, 0xa9, 0x0a,
259 	0x49, 0x6b, 0xee, 0xef, 0x62, 0xa7, 0x68, 0x50,
260 	0xff, 0x3f, 0x04, 0x02, 0xc4, 0xdd, 0xc9, 0x9f,
261 	0x6d, 0xb0, 0x7f, 0x15, 0x1c, 0x1c, 0x0d, 0xfa,
262 	0xc2, 0xe5, 0x65, 0x65, 0xd6, 0x28, 0x96, 0x25
263 };
264 
265 static uint8_t cha_sbl8[] = {
266 	0x5b, 0x23, 0x13, 0x2e, 0x7b, 0x46, 0x9c, 0x7b,
267 	0xfb, 0x88, 0xfa, 0x95, 0xd4, 0x4c, 0xa5, 0xae,
268 	0x3e, 0x45, 0xe8, 0x48, 0xa4, 0x10, 0x8e, 0x98,
269 	0xba, 0xd7, 0xa9, 0xeb, 0x15, 0x51, 0x27, 0x84,
270 	0xa6, 0xa9, 0xe6, 0xe5, 0x91, 0xdc, 0xe6, 0x74,
271 	0x12, 0x0a, 0xca, 0xf9, 0x04, 0x0f, 0xf5, 0x0f,
272 	0xf3, 0xac, 0x30, 0xcc, 0xfb, 0x5e, 0x14, 0x20,
273 	0x4f, 0x5e, 0x42, 0x68, 0xb9, 0x0a, 0x88, 0x04
274 };
275 
276 static void
chacha_test(uint8_t * key,uint8_t keylen,uint8_t * iv,uint8_t * bl0,uint8_t * bl1)277 chacha_test(uint8_t *key, uint8_t keylen, uint8_t *iv, uint8_t *bl0,
278     uint8_t *bl1)
279 {
280 	uint8_t res0[64], res1[64];
281 	chacha_ctx_t ctx;
282 	int ret;
283 
284 	bzero(&ctx, sizeof (ctx));
285 	bzero(res0, sizeof (res0));
286 	bzero(res1, sizeof (res1));
287 	chacha_keysetup(&ctx, key, keylen * 8, 0);
288 	chacha_ivsetup(&ctx, iv);
289 	chacha_encrypt_bytes(&ctx, res0, res0, sizeof (res0));
290 	chacha_encrypt_bytes(&ctx, res1, res1, sizeof (res1));
291 	ret = bcmp(res0, bl0, sizeof (res0));
292 	assert(ret == 0);
293 	ret = bcmp(res1, bl1, sizeof (res0));
294 	assert(ret == 0);
295 }
296 
297 int
main(void)298 main(void)
299 {
300 	chacha_test(cha_key1, sizeof (cha_key1), cha_iv1, cha_fbl1, cha_sbl1);
301 	chacha_test(cha_key2, sizeof (cha_key2), cha_iv2, cha_fbl2, cha_sbl2);
302 	chacha_test(cha_key3, sizeof (cha_key3), cha_iv3, cha_fbl3, cha_sbl3);
303 	chacha_test(cha_key4, sizeof (cha_key4), cha_iv4, cha_fbl4, cha_sbl4);
304 	chacha_test(cha_key5, sizeof (cha_key5), cha_iv5, cha_fbl5, cha_sbl5);
305 	chacha_test(cha_key6, sizeof (cha_key6), cha_iv6, cha_fbl6, cha_sbl6);
306 	chacha_test(cha_key7, sizeof (cha_key7), cha_iv7, cha_fbl7, cha_sbl7);
307 	chacha_test(cha_key8, sizeof (cha_key8), cha_iv8, cha_fbl8, cha_sbl8);
308 	return (0);
309 }
310