Add.
[shishi.git] / tests / low-crypto.c
blobd4c0416a1f857a4c216f349667e8627522cc6d6a
1 /* low-crypto.c --- Shishi crypto primitives self tests.
2 * Copyright (C) 2002, 2003, 2004, 2006 Simon Josefsson
4 * This file is part of Shishi.
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 2 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with Shishi; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "utils.c"
24 void
25 test (Shishi * handle)
27 char *out, *ivout;
28 int err;
30 if (debug)
31 shishi_cfg (handle, strdup ("verbose-crypto"));
33 err = shishi_crc (handle, "abcdefgh", 8, &out);
34 if (err)
35 fail ("shishi_crc() failed: %d\n", err);
36 else
38 if (memcmp (out, "\x39\xf5\xcd\xcb", 4) == 0)
39 success ("shishi_crc() OK\n");
40 else
42 hexprint (out, 4);
43 fail ("shishi_crc() failure\n");
45 free (out);
48 err = shishi_md4 (handle, "abcdefgh", 8, &out);
49 if (err)
50 fail ("shishi_md4() failed: %d\n", err);
51 else
53 if (memcmp (out, "\xad\x9d\xaf\x8d\x49\xd8\x19\x88"
54 "\x59\x0a\x6f\x0e\x74\x5d\x15\xdd", 16) == 0)
55 success ("shishi_md4() OK\n");
56 else
58 hexprint (out, 16);
59 fail ("shishi_md4() failure\n");
61 free (out);
64 err = shishi_md5 (handle, "abcdefgh", 8, &out);
65 if (err)
66 fail ("shishi_md5() failed: %d\n", err);
67 else
69 if (memcmp (out, "\xe8\xdc\x40\x81\xb1\x34\x34\xb4"
70 "\x51\x89\xa7\x20\xb7\x7b\x68\x18", 16) == 0)
71 success ("shishi_md5() OK\n");
72 else
74 hexprint (out, 16);
75 fail ("shishi_md5() failure\n");
77 free (out);
80 err = shishi_hmac_md5 (handle, "keykeykey", 9, "abcdefgh", 8, &out);
81 if (err)
82 fail ("shishi_hmac_md5() failed: %d\n", err);
83 else
85 if (memcmp (out, "\x3c\xb0\x9d\x83\x28\x01\xef\xc0"
86 "\x7b\xb3\xaf\x42\x69\xe5\x93\x9a", 16) == 0)
87 success ("shishi_hmac_md5() OK\n");
88 else
90 hexprint (out, 16);
91 fail ("shishi_hmac_md5() failure\n");
93 free (out);
96 err = shishi_hmac_sha1 (handle, "keykeykey", 9, "abcdefgh", 8, &out);
97 if (err)
98 fail ("shishi_hmac_sha1() failed: %d\n", err);
99 else
101 if (memcmp (out, "\x58\x93\x7a\x58\xfe\xea\x82\xf8"
102 "\x0e\x64\x62\x01\x40\x2b\x2c\xed\x5d\x54\xc1\xfa",
103 20) == 0)
104 success ("shishi_hmac_sha1() OK\n");
105 else
107 hexprint (out, 20);
108 fail ("shishi_hmac_sha1() failure\n");
110 free (out);
113 err = shishi_des_cbc_mac (handle, "kdykdykd", NULL, "abcdefgh", 8, &out);
114 if (err)
115 fail ("shishi_des_cbc_mac() failed: %d\n", err);
116 else
118 if (memcmp (out, "\xb9\xf1\x38\x36\x37\x7a\x6f\x4c", 8) == 0)
119 success ("shishi_des_cbc_mac() OK\n");
120 else
122 hexprint (out, 8);
123 fail ("shishi_des_cbc_mac() failure\n");
125 free (out);
128 err = shishi_des_cbc_mac (handle, "kdykdykd", "iviviviv",
129 "abcdefgh", 8, &out);
130 if (err)
131 fail ("shishi_des_cbc_mac() failed: %d\n", err);
132 else
134 if (memcmp (out, "\x7b\x66\x2d\x4d\x54\xc9\xc1\x01", 8) == 0)
135 success ("shishi_des_cbc_mac() OK\n");
136 else
138 hexprint (out, 8);
139 fail ("shishi_des_cbc_mac() failure\n");
141 free (out);
144 err = shishi_arcfour (handle, 0, "keykeykey", 9, NULL, NULL,
145 "abcdefgh", 8, &out);
146 if (err)
147 fail ("shishi_arcfour() failed: %d\n", err);
148 else
150 if (memcmp (out, "\x6a\x0e\x57\x89\x41\xe9\x1c\x22", 8) == 0)
151 success ("shishi_arcfour() OK\n");
152 else
154 hexprint (out, 8);
155 fail ("shishi_arcfour() failure\n");
157 free (out);
160 err = shishi_arcfour (handle, 0, "keykeyke", 8, NULL, &ivout,
161 "abcdefghi", 9, &out);
162 if (err)
163 fail ("shishi_arcfour() failed: %d\n", err);
164 else
166 if (memcmp (out, "\x17\x52\xf3\xd8\x61\x14\xe6\x76", 8) == 0)
167 success ("shishi_arcfour() OK\n");
168 else
170 hexprint (out, 8);
171 fail ("shishi_arcfour() failure\n");
173 if (memcmp (ivout,
174 "\x6b\xdf\xca\xe7\x4c\xe8\x79\x53"
175 "\xd0\x4e\xe2\x37\xc9\x52\xc6\x3c"
176 "\x24\xf2\x9e\x5b\x32\x50\x07\x2e"
177 "\xee\x0f\xc2\x38\x86\x89\x4b\x21"
178 "\xd2\xc0\xa2\x7a\xb7\xae\xf1\xcb"
179 "\x03\x19\x78\x41\x9f\x74\xab\x35"
180 "\x12\x30\xe9\x04\x1c\x05\x66\x58"
181 "\x25\x62\x77\xa5\x42\x44\xd6\x6d"
182 "\x85\xc8\x43\x94\xcf\xfb\x06\x0b"
183 "\xde\x7f\x15\xa1\x8e\xaa\x70\x1b"
184 "\x98\xb0\x13\x27\x73\x4f\x2a\x3d"
185 "\x81\x29\x83\xd8\x99\x36\xd3\x54"
186 "\x4a\x31\xf0\xbe\x18\xe1\x6f\x28"
187 "\x3a\x64\x6a\x68\xef\x59\x22\xbc"
188 "\xb5\x47\x76\x63\xec\x48\x3b\x71"
189 "\x10\xc4\x87\x5f\xea\xc1\xf6\x5d"
190 "\xc5\x8b\xda\xac\xe0\xa9\x8a\xa6"
191 "\x11\x09\x0c\x72\xad\xb3\x46\xe5"
192 "\x9b\x91\x16\x93\x51\x49\xe6\xbf"
193 "\x95\xf5\xd1\x20\xe3\x90\x5a\x39"
194 "\x7b\x7e\x7c\xb9\x40\xbd\x08\x9a"
195 "\x45\xb4\xd7\x1f\x2c\x61\xb8\xcc"
196 "\xb6\x33\x92\x0e\xf9\x0a\xba\x55"
197 "\x75\x14\x5e\xb1\x26\xf8\x84\xed"
198 "\xa4\x1e\x7d\x60\xe4\xdd\x2b\xff"
199 "\xeb\xfe\xd4\x57\x8c\xa0\x88\x8d"
200 "\xdc\x00\x34\x23\xc7\xfd\x0d\x97"
201 "\x56\x96\xaf\xcd\x3f\xf7\xc3\xa7"
202 "\x6c\x65\xbb\xf3\x3e\xdb\x4d\xd9"
203 "\x1d\xa3\x9d\xf4\x17\x69\x6e\x82"
204 "\x02\xa8\x2d\x9c\xce\x1a\xb2\xfc"
205 "\xfa\x5c\x67\x2f\x8f\x01\x80\xd5" "\x09\xa2", 258) == 0)
206 success ("shishi_arcfour() OK IV\n");
207 else
209 hexprint (ivout, 258);
210 fail ("shishi_arcfour() failure IV\n");
212 free (out);
213 free (ivout);
216 err = shishi_des (handle, 0, "kdykdykd", NULL, NULL, "abcdefgh", 8, &out);
217 if (err)
218 fail ("shishi_des() failed: %d\n", err);
219 else
221 if (memcmp (out, "\xb9\xf1\x38\x36\x37\x7a\x6f\x4c", 8) == 0)
222 success ("shishi_des() OK\n");
223 else
225 hexprint (out, 8);
226 fail ("shishi_des() failure\n");
228 free (out);
231 err = shishi_des (handle, 0, "kdykdykd", "iviviviv", NULL,
232 "abcdefgh", 8, &out);
233 if (err)
234 fail ("shishi_des() failed: %d\n", err);
235 else
237 if (memcmp (out, "\x7b\x66\x2d\x4d\x54\xc9\xc1\x01", 8) == 0)
238 success ("shishi_des() OK\n");
239 else
241 hexprint (out, 8);
242 fail ("shishi_des() failure\n");
244 free (out);
247 err = shishi_des (handle, 0, "kdykdykd", "iviviviv", &ivout,
248 "abcdefgh", 8, &out);
249 if (err)
250 fail ("shishi_des() failed: %d\n", err);
251 else
253 if (memcmp (out, "\x7b\x66\x2d\x4d\x54\xc9\xc1\x01", 8) == 0)
254 success ("shishi_des() OK\n");
255 else
257 hexprint (out, 8);
258 fail ("shishi_des() failure\n");
261 if (memcmp (ivout, "\x7b\x66\x2d\x4d\x54\xc9\xc1\x01", 8) == 0)
262 success ("shishi_des() OK IV\n");
263 else
265 hexprint (ivout, 8);
266 fail ("shishi_des() failure IV\n");
268 free (out);
269 free (ivout);
272 err = shishi_3des (handle, 0, "kdykdykdykdykdykdykdykdy", NULL, NULL,
273 "abcdefgh", 8, &out);
274 if (err)
275 fail ("shishi_3des() failed: %d\n", err);
276 else
278 if (memcmp (out, "\xd9\x4a\xd9\xa4\x92\xb1\x70\x60", 8) == 0)
279 success ("shishi_3des() OK\n");
280 else
282 hexprint (out, 8);
283 fail ("shishi_3des() failure\n");
285 free (out);
288 err = shishi_3des (handle, 0, "kdykdykdykdykdykdykdykdy",
289 "iviviviviviviviviviviviv", NULL, "abcdefgh", 8, &out);
290 if (err)
291 fail ("shishi_3des() failed: %d\n", err);
292 else
294 if (memcmp (out, "\x45\xba\x34\xb4\xda\xd7\x53\x6f", 8) == 0)
295 success ("shishi_3des() OK\n");
296 else
298 hexprint (out, 8);
299 fail ("shishi_3des() failure\n");
301 free (out);
304 err = shishi_3des (handle, 0, "kdykdykdykdykdykdykdykdy",
305 "iviviviviviviviviviviviv", &ivout,
306 "abcdefghijklmnopqrstuvxy", 24, &out);
307 if (err)
308 fail ("shishi_3des() failed: %d\n", err);
309 else
311 if (memcmp (out, "\x45\xba\x34\xb4\xda\xd7\x53\x6f"
312 "\x4e\x4b\xe8\x14\x44\x25\xf2\x19"
313 "\x46\x57\x6b\x16\xd9\x5d\xf2\x38", 24) == 0)
314 success ("shishi_3des() OK\n");
315 else
317 hexprint (out, 24);
318 fail ("shishi_3des() failure\n");
321 if (memcmp (ivout, "\x46\x57\x6b\x16\xd9\x5d\xf2\x38", 8) == 0)
322 success ("shishi_3des() OK IV\n");
323 else
325 hexprint (ivout, 8);
326 fail ("shishi_3des() failure IV\n");
328 free (out);
329 free (ivout);
332 err = shishi_aes_cts (handle, 0, "keykeykeykeykeyk", 16,
333 "iviviviviviviviv", &ivout,
334 "abcdefghijklmnop", 16, &out);
335 if (err)
336 fail ("shishi_aes_cts(16) failed: %d\n", err);
337 else
339 if (memcmp (out, "\x89\xee\x53\x33\x54\xa8\xb0\xb7"
340 "\xb6\x36\xbf\x80\xb0\xba\x6a\x4a", 16) == 0)
341 success ("shishi_aes_cts(16) OK\n");
342 else
344 hexprint (out, 16);
345 fail ("shishi_aes_cts(16) failure\n");
348 if (memcmp (ivout, "\x89\xee\x53\x33\x54\xa8\xb0\xb7"
349 "\xb6\x36\xbf\x80\xb0\xba\x6a\x4a", 16) == 0)
350 success ("shishi_aes_cts(16) OK IV\n");
351 else
353 hexprint (ivout, 16);
354 fail ("shishi_aes_cts(16) failure IV\n");
356 free (out);
357 free (ivout);
360 err = shishi_aes_cts (handle, 0, "keykeykeykeykeyk", 16,
361 "iviviviviviviviv", &ivout,
362 "abcdefghijklmnopqrstuvxy", 24, &out);
363 if (err)
364 fail ("shishi_aes_cts(24) failed: %d\n", err);
365 else
367 if (memcmp (out, "\x22\x8f\x1a\xc5\xd4\x74\xd2\x74"
368 "\x96\x4d\x2d\xcd\x0b\xa3\x0d\x8f"
369 "\x89\xee\x53\x33\x54\xa8\xb0\xb7", 24) == 0)
370 success ("shishi_aes_cts(24) OK\n");
371 else
373 hexprint (out, 24);
374 fail ("shishi_aes_cts(24) failure\n");
377 if (memcmp (ivout, "\x22\x8f\x1a\xc5\xd4\x74\xd2\x74"
378 "\x96\x4d\x2d\xcd\x0b\xa3\x0d\x8f", 16) == 0)
379 success ("shishi_aes_cts(24) OK IV\n");
380 else
382 hexprint (ivout, 16);
383 fail ("shishi_aes_cts(24) failure IV\n");
385 free (out);
386 free (ivout);
389 err = shishi_aes_cts (handle, 0, "keykeykeykeykeyk", 16,
390 "iviviviviviviviv", &ivout,
391 "abcdefghijklmnopqrstuvx", 23, &out);
392 if (err)
393 fail ("shishi_aes_cts(23) failed: %d\n", err);
394 else
396 if (memcmp (out, "\x45\x23\x5a\x0c\x6b\x8a\x0c\xad"
397 "\xe6\x50\xff\xe1\x08\x17\x9a\x6d"
398 "\x89\xee\x53\x33\x54\xa8\xb0", 23) == 0)
399 success ("shishi_aes_cts(23) OK\n");
400 else
402 hexprint (out, 23);
403 fail ("shishi_aes_cts(23) failure\n");
406 if (memcmp (ivout, "\x45\x23\x5a\x0c\x6b\x8a\x0c\xad"
407 "\xe6\x50\xff\xe1\x08\x17\x9a\x6d", 16) == 0)
408 success ("shishi_aes_cts(23) OK IV\n");
409 else
411 hexprint (ivout, 16);
412 fail ("shishi_aes_cts(23) failure IV\n");
414 free (out);
415 free (ivout);
418 err = shishi_aes_cts (handle, 0, "keykeykeykeykeyk", 16,
419 "iviviviviviviviv", &ivout,
420 "abcdefghijklmnopqrstuvxyz", 25, &out);
421 if (err)
422 fail ("shishi_aes_cts(25) failed: %d\n", err);
423 else
425 if (memcmp (out, "\xa9\x50\xdd\xcb\xa8\x5b\x5c\xb6"
426 "\x84\x7d\x38\x65\x4a\xc1\x63\xd7"
427 "\x89\xee\x53\x33\x54\xa8\xb0\xb7\xb6", 25) == 0)
428 success ("shishi_aes_cts(25) OK\n");
429 else
431 hexprint (out, 25);
432 fail ("shishi_aes_cts(25) failure\n");
435 if (memcmp (ivout, "\xa9\x50\xdd\xcb\xa8\x5b\x5c\xb6"
436 "\x84\x7d\x38\x65\x4a\xc1\x63\xd7", 16) == 0)
437 success ("shishi_aes_cts(25) OK IV\n");
438 else
440 hexprint (ivout, 16);
441 fail ("shishi_aes_cts(25) failure IV\n");
443 free (out);
444 free (ivout);