Branch data Line data Source code
1 : : /* low-crypto.c --- Shishi crypto primitives self tests.
2 : : * Copyright (C) 2002, 2003, 2004, 2006, 2007 Simon Josefsson
3 : : *
4 : : * This file is part of Shishi.
5 : : *
6 : : * Shishi is free software; you can redistribute it and/or modify
7 : : * it under the terms of the GNU General Public License as published by
8 : : * the Free Software Foundation; either version 3 of the License, or
9 : : * (at your option) any later version.
10 : : *
11 : : * Shishi is distributed in the hope that it will be useful,
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : : * GNU General Public License for more details.
15 : : *
16 : : * You should have received a copy of the GNU General Public License
17 : : * along with Shishi; if not, see http://www.gnu.org/licenses or write
18 : : * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
19 : : * Floor, Boston, MA 02110-1301, USA
20 : : *
21 : : */
22 : :
23 : : #include "utils.c"
24 : :
25 : : void
26 : 1 : test (Shishi * handle)
27 : : {
28 : : char *out, *ivout;
29 : : int err;
30 : :
31 [ - + ]: 1 : if (debug)
32 : 0 : shishi_cfg (handle, strdup ("verbose-crypto"));
33 : :
34 : 1 : err = shishi_crc (handle, "abcdefgh", 8, &out);
35 [ - + ]: 1 : if (err)
36 : 0 : fail ("shishi_crc() failed: %d\n", err);
37 : : else
38 : : {
39 [ + - ]: 1 : if (memcmp (out, "\x39\xf5\xcd\xcb", 4) == 0)
40 : 1 : success ("shishi_crc() OK\n");
41 : : else
42 : : {
43 : 0 : hexprint (out, 4);
44 : 0 : fail ("shishi_crc() failure\n");
45 : : }
46 : 1 : free (out);
47 : : }
48 : :
49 : 1 : err = shishi_md4 (handle, "abcdefgh", 8, &out);
50 [ - + ]: 1 : if (err)
51 : 0 : fail ("shishi_md4() failed: %d\n", err);
52 : : else
53 : : {
54 [ + - ]: 1 : if (memcmp (out, "\xad\x9d\xaf\x8d\x49\xd8\x19\x88"
55 : : "\x59\x0a\x6f\x0e\x74\x5d\x15\xdd", 16) == 0)
56 : 1 : success ("shishi_md4() OK\n");
57 : : else
58 : : {
59 : 0 : hexprint (out, 16);
60 : 0 : fail ("shishi_md4() failure\n");
61 : : }
62 : 1 : free (out);
63 : : }
64 : :
65 : 1 : err = shishi_md5 (handle, "abcdefgh", 8, &out);
66 [ - + ]: 1 : if (err)
67 : 0 : fail ("shishi_md5() failed: %d\n", err);
68 : : else
69 : : {
70 [ + - ]: 1 : if (memcmp (out, "\xe8\xdc\x40\x81\xb1\x34\x34\xb4"
71 : : "\x51\x89\xa7\x20\xb7\x7b\x68\x18", 16) == 0)
72 : 1 : success ("shishi_md5() OK\n");
73 : : else
74 : : {
75 : 0 : hexprint (out, 16);
76 : 0 : fail ("shishi_md5() failure\n");
77 : : }
78 : 1 : free (out);
79 : : }
80 : :
81 : 1 : err = shishi_hmac_md5 (handle, "keykeykey", 9, "abcdefgh", 8, &out);
82 [ - + ]: 1 : if (err)
83 : 0 : fail ("shishi_hmac_md5() failed: %d\n", err);
84 : : else
85 : : {
86 [ + - ]: 1 : if (memcmp (out, "\x3c\xb0\x9d\x83\x28\x01\xef\xc0"
87 : : "\x7b\xb3\xaf\x42\x69\xe5\x93\x9a", 16) == 0)
88 : 1 : success ("shishi_hmac_md5() OK\n");
89 : : else
90 : : {
91 : 0 : hexprint (out, 16);
92 : 0 : fail ("shishi_hmac_md5() failure\n");
93 : : }
94 : 1 : free (out);
95 : : }
96 : :
97 : 1 : err = shishi_hmac_sha1 (handle, "keykeykey", 9, "abcdefgh", 8, &out);
98 [ - + ]: 1 : if (err)
99 : 0 : fail ("shishi_hmac_sha1() failed: %d\n", err);
100 : : else
101 : : {
102 [ + - ]: 1 : if (memcmp (out, "\x58\x93\x7a\x58\xfe\xea\x82\xf8"
103 : : "\x0e\x64\x62\x01\x40\x2b\x2c\xed\x5d\x54\xc1\xfa",
104 : : 20) == 0)
105 : 1 : success ("shishi_hmac_sha1() OK\n");
106 : : else
107 : : {
108 : 0 : hexprint (out, 20);
109 : 0 : fail ("shishi_hmac_sha1() failure\n");
110 : : }
111 : 1 : free (out);
112 : : }
113 : :
114 : 1 : err = shishi_des_cbc_mac (handle, "kdykdykd", NULL, "abcdefgh", 8, &out);
115 [ - + ]: 1 : if (err)
116 : 0 : fail ("shishi_des_cbc_mac() failed: %d\n", err);
117 : : else
118 : : {
119 [ + - ]: 1 : if (memcmp (out, "\xb9\xf1\x38\x36\x37\x7a\x6f\x4c", 8) == 0)
120 : 1 : success ("shishi_des_cbc_mac() OK\n");
121 : : else
122 : : {
123 : 0 : hexprint (out, 8);
124 : 0 : fail ("shishi_des_cbc_mac() failure\n");
125 : : }
126 : 1 : free (out);
127 : : }
128 : :
129 : 1 : err = shishi_des_cbc_mac (handle, "kdykdykd", "iviviviv",
130 : : "abcdefgh", 8, &out);
131 [ - + ]: 1 : if (err)
132 : 0 : fail ("shishi_des_cbc_mac() failed: %d\n", err);
133 : : else
134 : : {
135 [ + - ]: 1 : if (memcmp (out, "\x7b\x66\x2d\x4d\x54\xc9\xc1\x01", 8) == 0)
136 : 1 : success ("shishi_des_cbc_mac() OK\n");
137 : : else
138 : : {
139 : 0 : hexprint (out, 8);
140 : 0 : fail ("shishi_des_cbc_mac() failure\n");
141 : : }
142 : 1 : free (out);
143 : : }
144 : :
145 : 1 : err = shishi_arcfour (handle, 0, "keykeykey", 9, NULL, NULL,
146 : : "abcdefgh", 8, &out);
147 [ - + ]: 1 : if (err)
148 : 0 : fail ("shishi_arcfour() failed: %d\n", err);
149 : : else
150 : : {
151 [ + - ]: 1 : if (memcmp (out, "\x6a\x0e\x57\x89\x41\xe9\x1c\x22", 8) == 0)
152 : 1 : success ("shishi_arcfour() OK\n");
153 : : else
154 : : {
155 : 0 : hexprint (out, 8);
156 : 0 : fail ("shishi_arcfour() failure\n");
157 : : }
158 : 1 : free (out);
159 : : }
160 : :
161 : 1 : err = shishi_arcfour (handle, 0, "keykeyke", 8, NULL, &ivout,
162 : : "abcdefghi", 9, &out);
163 [ - + ]: 1 : if (err)
164 : 0 : fail ("shishi_arcfour() failed: %d\n", err);
165 : : else
166 : : {
167 [ + - ]: 1 : if (memcmp (out, "\x17\x52\xf3\xd8\x61\x14\xe6\x76", 8) == 0)
168 : 1 : success ("shishi_arcfour() OK\n");
169 : : else
170 : : {
171 : 0 : hexprint (out, 8);
172 : 0 : fail ("shishi_arcfour() failure\n");
173 : : }
174 [ + - ]: 1 : if (memcmp (ivout,
175 : : "\x6b\xdf\xca\xe7\x4c\xe8\x79\x53"
176 : : "\xd0\x4e\xe2\x37\xc9\x52\xc6\x3c"
177 : : "\x24\xf2\x9e\x5b\x32\x50\x07\x2e"
178 : : "\xee\x0f\xc2\x38\x86\x89\x4b\x21"
179 : : "\xd2\xc0\xa2\x7a\xb7\xae\xf1\xcb"
180 : : "\x03\x19\x78\x41\x9f\x74\xab\x35"
181 : : "\x12\x30\xe9\x04\x1c\x05\x66\x58"
182 : : "\x25\x62\x77\xa5\x42\x44\xd6\x6d"
183 : : "\x85\xc8\x43\x94\xcf\xfb\x06\x0b"
184 : : "\xde\x7f\x15\xa1\x8e\xaa\x70\x1b"
185 : : "\x98\xb0\x13\x27\x73\x4f\x2a\x3d"
186 : : "\x81\x29\x83\xd8\x99\x36\xd3\x54"
187 : : "\x4a\x31\xf0\xbe\x18\xe1\x6f\x28"
188 : : "\x3a\x64\x6a\x68\xef\x59\x22\xbc"
189 : : "\xb5\x47\x76\x63\xec\x48\x3b\x71"
190 : : "\x10\xc4\x87\x5f\xea\xc1\xf6\x5d"
191 : : "\xc5\x8b\xda\xac\xe0\xa9\x8a\xa6"
192 : : "\x11\x09\x0c\x72\xad\xb3\x46\xe5"
193 : : "\x9b\x91\x16\x93\x51\x49\xe6\xbf"
194 : : "\x95\xf5\xd1\x20\xe3\x90\x5a\x39"
195 : : "\x7b\x7e\x7c\xb9\x40\xbd\x08\x9a"
196 : : "\x45\xb4\xd7\x1f\x2c\x61\xb8\xcc"
197 : : "\xb6\x33\x92\x0e\xf9\x0a\xba\x55"
198 : : "\x75\x14\x5e\xb1\x26\xf8\x84\xed"
199 : : "\xa4\x1e\x7d\x60\xe4\xdd\x2b\xff"
200 : : "\xeb\xfe\xd4\x57\x8c\xa0\x88\x8d"
201 : : "\xdc\x00\x34\x23\xc7\xfd\x0d\x97"
202 : : "\x56\x96\xaf\xcd\x3f\xf7\xc3\xa7"
203 : : "\x6c\x65\xbb\xf3\x3e\xdb\x4d\xd9"
204 : : "\x1d\xa3\x9d\xf4\x17\x69\x6e\x82"
205 : : "\x02\xa8\x2d\x9c\xce\x1a\xb2\xfc"
206 : : "\xfa\x5c\x67\x2f\x8f\x01\x80\xd5" "\x09\xa2", 258) == 0)
207 : 1 : success ("shishi_arcfour() OK IV\n");
208 : : else
209 : : {
210 : 0 : hexprint (ivout, 258);
211 : 0 : fail ("shishi_arcfour() failure IV\n");
212 : : }
213 : 1 : free (out);
214 : 1 : free (ivout);
215 : : }
216 : :
217 : 1 : err = shishi_des (handle, 0, "kdykdykd", NULL, NULL, "abcdefgh", 8, &out);
218 [ - + ]: 1 : if (err)
219 : 0 : fail ("shishi_des() failed: %d\n", err);
220 : : else
221 : : {
222 [ + - ]: 1 : if (memcmp (out, "\xb9\xf1\x38\x36\x37\x7a\x6f\x4c", 8) == 0)
223 : 1 : success ("shishi_des() OK\n");
224 : : else
225 : : {
226 : 0 : hexprint (out, 8);
227 : 0 : fail ("shishi_des() failure\n");
228 : : }
229 : 1 : free (out);
230 : : }
231 : :
232 : 1 : err = shishi_des (handle, 0, "kdykdykd", "iviviviv", NULL,
233 : : "abcdefgh", 8, &out);
234 [ - + ]: 1 : if (err)
235 : 0 : fail ("shishi_des() failed: %d\n", err);
236 : : else
237 : : {
238 [ + - ]: 1 : if (memcmp (out, "\x7b\x66\x2d\x4d\x54\xc9\xc1\x01", 8) == 0)
239 : 1 : success ("shishi_des() OK\n");
240 : : else
241 : : {
242 : 0 : hexprint (out, 8);
243 : 0 : fail ("shishi_des() failure\n");
244 : : }
245 : 1 : free (out);
246 : : }
247 : :
248 : 1 : err = shishi_des (handle, 0, "kdykdykd", "iviviviv", &ivout,
249 : : "abcdefgh", 8, &out);
250 [ - + ]: 1 : if (err)
251 : 0 : fail ("shishi_des() failed: %d\n", err);
252 : : else
253 : : {
254 [ + - ]: 1 : if (memcmp (out, "\x7b\x66\x2d\x4d\x54\xc9\xc1\x01", 8) == 0)
255 : 1 : success ("shishi_des() OK\n");
256 : : else
257 : : {
258 : 0 : hexprint (out, 8);
259 : 0 : fail ("shishi_des() failure\n");
260 : : }
261 : :
262 [ + - ]: 1 : if (memcmp (ivout, "\x7b\x66\x2d\x4d\x54\xc9\xc1\x01", 8) == 0)
263 : 1 : success ("shishi_des() OK IV\n");
264 : : else
265 : : {
266 : 0 : hexprint (ivout, 8);
267 : 0 : fail ("shishi_des() failure IV\n");
268 : : }
269 : 1 : free (out);
270 : 1 : free (ivout);
271 : : }
272 : :
273 : 1 : err = shishi_3des (handle, 0, "kdykdykdykdykdykdykdykdy", NULL, NULL,
274 : : "abcdefgh", 8, &out);
275 [ - + ]: 1 : if (err)
276 : 0 : fail ("shishi_3des() failed: %d\n", err);
277 : : else
278 : : {
279 [ + - ]: 1 : if (memcmp (out, "\xd9\x4a\xd9\xa4\x92\xb1\x70\x60", 8) == 0)
280 : 1 : success ("shishi_3des() OK\n");
281 : : else
282 : : {
283 : 0 : hexprint (out, 8);
284 : 0 : fail ("shishi_3des() failure\n");
285 : : }
286 : 1 : free (out);
287 : : }
288 : :
289 : 1 : err = shishi_3des (handle, 0, "kdykdykdykdykdykdykdykdy",
290 : : "iviviviviviviviviviviviv", NULL, "abcdefgh", 8, &out);
291 [ - + ]: 1 : if (err)
292 : 0 : fail ("shishi_3des() failed: %d\n", err);
293 : : else
294 : : {
295 [ + - ]: 1 : if (memcmp (out, "\x45\xba\x34\xb4\xda\xd7\x53\x6f", 8) == 0)
296 : 1 : success ("shishi_3des() OK\n");
297 : : else
298 : : {
299 : 0 : hexprint (out, 8);
300 : 0 : fail ("shishi_3des() failure\n");
301 : : }
302 : 1 : free (out);
303 : : }
304 : :
305 : 1 : err = shishi_3des (handle, 0, "kdykdykdykdykdykdykdykdy",
306 : : "iviviviviviviviviviviviv", &ivout,
307 : : "abcdefghijklmnopqrstuvxy", 24, &out);
308 [ - + ]: 1 : if (err)
309 : 0 : fail ("shishi_3des() failed: %d\n", err);
310 : : else
311 : : {
312 [ + - ]: 1 : if (memcmp (out, "\x45\xba\x34\xb4\xda\xd7\x53\x6f"
313 : : "\x4e\x4b\xe8\x14\x44\x25\xf2\x19"
314 : : "\x46\x57\x6b\x16\xd9\x5d\xf2\x38", 24) == 0)
315 : 1 : success ("shishi_3des() OK\n");
316 : : else
317 : : {
318 : 0 : hexprint (out, 24);
319 : 0 : fail ("shishi_3des() failure\n");
320 : : }
321 : :
322 [ + - ]: 1 : if (memcmp (ivout, "\x46\x57\x6b\x16\xd9\x5d\xf2\x38", 8) == 0)
323 : 1 : success ("shishi_3des() OK IV\n");
324 : : else
325 : : {
326 : 0 : hexprint (ivout, 8);
327 : 0 : fail ("shishi_3des() failure IV\n");
328 : : }
329 : 1 : free (out);
330 : 1 : free (ivout);
331 : : }
332 : :
333 : 1 : err = shishi_aes_cts (handle, 0, "keykeykeykeykeyk", 16,
334 : : "iviviviviviviviv", &ivout,
335 : : "abcdefghijklmnop", 16, &out);
336 [ - + ]: 1 : if (err)
337 : 0 : fail ("shishi_aes_cts(16) failed: %d\n", err);
338 : : else
339 : : {
340 [ + - ]: 1 : if (memcmp (out, "\x89\xee\x53\x33\x54\xa8\xb0\xb7"
341 : : "\xb6\x36\xbf\x80\xb0\xba\x6a\x4a", 16) == 0)
342 : 1 : success ("shishi_aes_cts(16) OK\n");
343 : : else
344 : : {
345 : 0 : hexprint (out, 16);
346 : 0 : fail ("shishi_aes_cts(16) failure\n");
347 : : }
348 : :
349 [ + - ]: 1 : if (memcmp (ivout, "\x89\xee\x53\x33\x54\xa8\xb0\xb7"
350 : : "\xb6\x36\xbf\x80\xb0\xba\x6a\x4a", 16) == 0)
351 : 1 : success ("shishi_aes_cts(16) OK IV\n");
352 : : else
353 : : {
354 : 0 : hexprint (ivout, 16);
355 : 0 : fail ("shishi_aes_cts(16) failure IV\n");
356 : : }
357 : 1 : free (out);
358 : 1 : free (ivout);
359 : : }
360 : :
361 : 1 : err = shishi_aes_cts (handle, 0, "keykeykeykeykeyk", 16,
362 : : "iviviviviviviviv", &ivout,
363 : : "abcdefghijklmnopqrstuvxy", 24, &out);
364 [ - + ]: 1 : if (err)
365 : 0 : fail ("shishi_aes_cts(24) failed: %d\n", err);
366 : : else
367 : : {
368 [ + - ]: 1 : if (memcmp (out, "\x22\x8f\x1a\xc5\xd4\x74\xd2\x74"
369 : : "\x96\x4d\x2d\xcd\x0b\xa3\x0d\x8f"
370 : : "\x89\xee\x53\x33\x54\xa8\xb0\xb7", 24) == 0)
371 : 1 : success ("shishi_aes_cts(24) OK\n");
372 : : else
373 : : {
374 : 0 : hexprint (out, 24);
375 : 0 : fail ("shishi_aes_cts(24) failure\n");
376 : : }
377 : :
378 [ + - ]: 1 : if (memcmp (ivout, "\x22\x8f\x1a\xc5\xd4\x74\xd2\x74"
379 : : "\x96\x4d\x2d\xcd\x0b\xa3\x0d\x8f", 16) == 0)
380 : 1 : success ("shishi_aes_cts(24) OK IV\n");
381 : : else
382 : : {
383 : 0 : hexprint (ivout, 16);
384 : 0 : fail ("shishi_aes_cts(24) failure IV\n");
385 : : }
386 : 1 : free (out);
387 : 1 : free (ivout);
388 : : }
389 : :
390 : 1 : err = shishi_aes_cts (handle, 0, "keykeykeykeykeyk", 16,
391 : : "iviviviviviviviv", &ivout,
392 : : "abcdefghijklmnopqrstuvx", 23, &out);
393 [ - + ]: 1 : if (err)
394 : 0 : fail ("shishi_aes_cts(23) failed: %d\n", err);
395 : : else
396 : : {
397 [ + - ]: 1 : if (memcmp (out, "\x45\x23\x5a\x0c\x6b\x8a\x0c\xad"
398 : : "\xe6\x50\xff\xe1\x08\x17\x9a\x6d"
399 : : "\x89\xee\x53\x33\x54\xa8\xb0", 23) == 0)
400 : 1 : success ("shishi_aes_cts(23) OK\n");
401 : : else
402 : : {
403 : 0 : hexprint (out, 23);
404 : 0 : fail ("shishi_aes_cts(23) failure\n");
405 : : }
406 : :
407 [ + - ]: 1 : if (memcmp (ivout, "\x45\x23\x5a\x0c\x6b\x8a\x0c\xad"
408 : : "\xe6\x50\xff\xe1\x08\x17\x9a\x6d", 16) == 0)
409 : 1 : success ("shishi_aes_cts(23) OK IV\n");
410 : : else
411 : : {
412 : 0 : hexprint (ivout, 16);
413 : 0 : fail ("shishi_aes_cts(23) failure IV\n");
414 : : }
415 : 1 : free (out);
416 : 1 : free (ivout);
417 : : }
418 : :
419 : 1 : err = shishi_aes_cts (handle, 0, "keykeykeykeykeyk", 16,
420 : : "iviviviviviviviv", &ivout,
421 : : "abcdefghijklmnopqrstuvxyz", 25, &out);
422 [ - + ]: 1 : if (err)
423 : 0 : fail ("shishi_aes_cts(25) failed: %d\n", err);
424 : : else
425 : : {
426 [ + - ]: 1 : if (memcmp (out, "\xa9\x50\xdd\xcb\xa8\x5b\x5c\xb6"
427 : : "\x84\x7d\x38\x65\x4a\xc1\x63\xd7"
428 : : "\x89\xee\x53\x33\x54\xa8\xb0\xb7\xb6", 25) == 0)
429 : 1 : success ("shishi_aes_cts(25) OK\n");
430 : : else
431 : : {
432 : 0 : hexprint (out, 25);
433 : 0 : fail ("shishi_aes_cts(25) failure\n");
434 : : }
435 : :
436 [ + - ]: 1 : if (memcmp (ivout, "\xa9\x50\xdd\xcb\xa8\x5b\x5c\xb6"
437 : : "\x84\x7d\x38\x65\x4a\xc1\x63\xd7", 16) == 0)
438 : 1 : success ("shishi_aes_cts(25) OK IV\n");
439 : : else
440 : : {
441 : 0 : hexprint (ivout, 16);
442 : 0 : fail ("shishi_aes_cts(25) failure IV\n");
443 : : }
444 : 1 : free (out);
445 : 1 : free (ivout);
446 : : }
447 : 1 : }
|