1 /*
2 * Quick & dirty crypto testing module.
3 *
4 * This will only exist until we have a better testing mechanism
5 * (e.g. a char device).
6 *
7 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
8 * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
14 *
15 * 14 - 09 - 2003
16 * Rewritten by Kartikey Mahendra Bhatt
17 */
18
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/mm.h>
22 #include <linux/slab.h>
23 #include <asm/scatterlist.h>
24 #include <linux/string.h>
25 #include <linux/crypto.h>
26 #include <linux/highmem.h>
27 #include "tcrypt.h"
28
29 #define offset_in_page(p) ((unsigned long)(p) & ~PAGE_MASK)
30
31 /*
32 * Need to kmalloc() memory for testing kmap().
33 */
34 #define TVMEMSIZE 4096
35 #define XBUFSIZE 32768
36
37 /*
38 * Indexes into the xbuf to simulate cross-page access.
39 */
40 #define IDX1 37
41 #define IDX2 32400
42 #define IDX3 1
43 #define IDX4 8193
44 #define IDX5 22222
45 #define IDX6 17101
46 #define IDX7 27333
47 #define IDX8 3000
48
49 /*
50 * Used by test_cipher()
51 */
52 #define ENCRYPT 1
53 #define DECRYPT 0
54 #define MODE_ECB 1
55 #define MODE_CBC 0
56
57 static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
58
59 static int mode;
60 static char *xbuf;
61 static char *tvmem;
62
63 static char *check[] = {
64 "des", "md5", "des3_ede", "rot13", "sha1", "sha256", "blowfish",
65 "twofish", "serpent", "sha384", "sha512", "md4", "aes", "cast6",
66 "arc4", "michael_mic", "deflate", "tea", "xtea", "khazad",
67 "wp512", "wp384", "wp256", "tnepres", "anubis", "xeta", NULL
68 };
69
70 static void
hexdump(unsigned char * buf,unsigned int len)71 hexdump(unsigned char *buf, unsigned int len)
72 {
73 while (len--)
74 printk("%02x", *buf++);
75
76 printk("\n");
77 }
78
79 static void
test_hash(char * algo,struct hash_testvec * template,unsigned int tcount)80 test_hash (char * algo, struct hash_testvec * template, unsigned int tcount)
81 {
82 char *p;
83 unsigned int i, j, k, temp;
84 struct scatterlist sg[8];
85 char result[64];
86 struct crypto_tfm *tfm;
87 struct hash_testvec *hash_tv;
88 unsigned int tsize;
89
90 printk("\ntesting %s\n", algo);
91
92 tsize = sizeof (struct hash_testvec);
93 tsize *= tcount;
94
95 if (tsize > TVMEMSIZE) {
96 printk("template (%u) too big for tvmem (%u)\n", tsize, TVMEMSIZE);
97 return;
98 }
99
100 memcpy(tvmem, template, tsize);
101 hash_tv = (void *) tvmem;
102 tfm = crypto_alloc_tfm(algo, 0);
103 if (tfm == NULL) {
104 printk("failed to load transform for %s\n", algo);
105 return;
106 }
107
108 for (i = 0; i < tcount; i++) {
109 printk ("test %u:\n", i + 1);
110 memset (result, 0, 64);
111
112 p = hash_tv[i].plaintext;
113 sg[0].page = virt_to_page (p);
114 sg[0].offset = offset_in_page (p);
115 sg[0].length = hash_tv[i].psize;
116
117 crypto_digest_init (tfm);
118 if (tfm->crt_u.digest.dit_setkey) {
119 crypto_digest_setkey (tfm, hash_tv[i].key,
120 hash_tv[i].ksize);
121 }
122 crypto_digest_update (tfm, sg, 1);
123 crypto_digest_final (tfm, result);
124
125 hexdump (result, crypto_tfm_alg_digestsize (tfm));
126 printk("%s\n",
127 memcmp(result, hash_tv[i].digest,
128 crypto_tfm_alg_digestsize(tfm)) ? "fail" :
129 "pass");
130 }
131
132 printk ("testing %s across pages\n", algo);
133
134 /* setup the dummy buffer first */
135 memset(xbuf, 0, XBUFSIZE);
136
137 j = 0;
138 for (i = 0; i < tcount; i++) {
139 if (hash_tv[i].np) {
140 j++;
141 printk ("test %u:\n", j);
142 memset (result, 0, 64);
143
144 temp = 0;
145 for (k = 0; k < hash_tv[i].np; k++) {
146 memcpy (&xbuf[IDX[k]], hash_tv[i].plaintext + temp,
147 hash_tv[i].tap[k]);
148 temp += hash_tv[i].tap[k];
149 p = &xbuf[IDX[k]];
150 sg[k].page = virt_to_page (p);
151 sg[k].offset = offset_in_page (p);
152 sg[k].length = hash_tv[i].tap[k];
153 }
154
155 crypto_digest_digest (tfm, sg, hash_tv[i].np, result);
156
157 hexdump (result, crypto_tfm_alg_digestsize (tfm));
158 printk("%s\n",
159 memcmp(result, hash_tv[i].digest,
160 crypto_tfm_alg_digestsize(tfm)) ? "fail" :
161 "pass");
162 }
163 }
164
165 crypto_free_tfm (tfm);
166 }
167
168
169 #ifdef CONFIG_CRYPTO_HMAC
170
171 static void
test_hmac(char * algo,struct hmac_testvec * template,unsigned int tcount)172 test_hmac(char *algo, struct hmac_testvec * template, unsigned int tcount)
173 {
174 char *p;
175 unsigned int i, j, k, temp;
176 struct scatterlist sg[8];
177 char result[64];
178 struct crypto_tfm *tfm;
179 struct hmac_testvec *hmac_tv;
180 unsigned int tsize, klen;
181
182 tfm = crypto_alloc_tfm(algo, 0);
183 if (tfm == NULL) {
184 printk("failed to load transform for %s\n", algo);
185 return;
186 }
187
188 printk("\ntesting hmac_%s\n", algo);
189
190 tsize = sizeof (struct hmac_testvec);
191 tsize *= tcount;
192 if (tsize > TVMEMSIZE) {
193 printk("template (%u) too big for tvmem (%u)\n", tsize,
194 TVMEMSIZE);
195 goto out;
196 }
197
198 memcpy(tvmem, template, tsize);
199 hmac_tv = (void *) tvmem;
200
201 for (i = 0; i < tcount; i++) {
202 printk("test %u:\n", i + 1);
203 memset(result, 0, sizeof (result));
204
205 p = hmac_tv[i].plaintext;
206 klen = hmac_tv[i].ksize;
207 sg[0].page = virt_to_page(p);
208 sg[0].offset = offset_in_page(p);
209 sg[0].length = hmac_tv[i].psize;
210
211 crypto_hmac(tfm, hmac_tv[i].key, &klen, sg, 1, result);
212
213 hexdump(result, crypto_tfm_alg_digestsize(tfm));
214 printk("%s\n",
215 memcmp(result, hmac_tv[i].digest,
216 crypto_tfm_alg_digestsize(tfm)) ? "fail" :
217 "pass");
218 }
219
220 printk("\ntesting hmac_%s across pages\n", algo);
221
222 memset(xbuf, 0, XBUFSIZE);
223
224 j = 0;
225 for (i = 0; i < tcount; i++) {
226 if (hmac_tv[i].np) {
227 j++;
228 printk ("test %u:\n",j);
229 memset (result, 0, 64);
230
231 temp = 0;
232 klen = hmac_tv[i].ksize;
233 for (k = 0; k < hmac_tv[i].np; k++) {
234 memcpy (&xbuf[IDX[k]], hmac_tv[i].plaintext + temp,
235 hmac_tv[i].tap[k]);
236 temp += hmac_tv[i].tap[k];
237 p = &xbuf[IDX[k]];
238 sg[k].page = virt_to_page (p);
239 sg[k].offset = offset_in_page (p);
240 sg[k].length = hmac_tv[i].tap[k];
241 }
242
243 crypto_hmac(tfm, hmac_tv[i].key, &klen, sg, hmac_tv[i].np,
244 result);
245 hexdump(result, crypto_tfm_alg_digestsize(tfm));
246
247 printk("%s\n",
248 memcmp(result, hmac_tv[i].digest,
249 crypto_tfm_alg_digestsize(tfm)) ? "fail" :
250 "pass");
251 }
252 }
253 out:
254 crypto_free_tfm(tfm);
255 }
256
257 #endif /* CONFIG_CRYPTO_HMAC */
258
259 void
test_cipher(char * algo,int mode,int enc,struct cipher_testvec * template,unsigned int tcount)260 test_cipher(char * algo, int mode, int enc, struct cipher_testvec * template, unsigned int tcount)
261 {
262 unsigned int ret, i, j, k, temp;
263 unsigned int tsize;
264 char *p, *q;
265 struct crypto_tfm *tfm;
266 char *key;
267 struct cipher_testvec *cipher_tv;
268 struct scatterlist sg[8];
269 char e[11], m[4];
270
271 if (enc == ENCRYPT)
272 strncpy(e, "encryption", 11);
273 else
274 strncpy(e, "decryption", 11);
275 if (mode == MODE_ECB)
276 strncpy(m, "ECB", 4);
277 else
278 strncpy(m, "CBC", 4);
279
280 printk("\ntesting %s %s %s \n", algo, m, e);
281
282 tsize = sizeof (struct cipher_testvec);
283 tsize *= tcount;
284
285 if (tsize > TVMEMSIZE) {
286 printk("template (%u) too big for tvmem (%u)\n", tsize,
287 TVMEMSIZE);
288 return;
289 }
290
291 memcpy(tvmem, template, tsize);
292 cipher_tv = (void *) tvmem;
293
294 if (mode)
295 tfm = crypto_alloc_tfm (algo, 0);
296 else
297 tfm = crypto_alloc_tfm (algo, CRYPTO_TFM_MODE_CBC);
298
299 if (tfm == NULL) {
300 printk("failed to load transform for %s %s\n", algo, m);
301 return;
302 }
303
304 j = 0;
305 for (i = 0; i < tcount; i++) {
306 if (!(cipher_tv[i].np)) {
307 j++;
308 printk("test %u (%d bit key):\n",
309 j, cipher_tv[i].klen * 8);
310
311 tfm->crt_flags = 0;
312 if (cipher_tv[i].wk)
313 tfm->crt_flags |= CRYPTO_TFM_REQ_WEAK_KEY;
314 key = cipher_tv[i].key;
315
316 ret = crypto_cipher_setkey(tfm, key, cipher_tv[i].klen);
317 if (ret) {
318 printk("setkey() failed flags=%x\n", tfm->crt_flags);
319
320 if (!cipher_tv[i].fail)
321 goto out;
322 }
323
324 p = cipher_tv[i].input;
325 sg[0].page = virt_to_page(p);
326 sg[0].offset = offset_in_page(p);
327 sg[0].length = cipher_tv[i].ilen;
328
329 if (!mode) {
330 crypto_cipher_set_iv(tfm, cipher_tv[i].iv,
331 crypto_tfm_alg_ivsize (tfm));
332 }
333
334 if (enc)
335 ret = crypto_cipher_encrypt(tfm, sg, sg, cipher_tv[i].ilen);
336 else
337 ret = crypto_cipher_decrypt(tfm, sg, sg, cipher_tv[i].ilen);
338
339
340 if (ret) {
341 printk("%s () failed flags=%x\n", e, tfm->crt_flags);
342 goto out;
343 }
344
345 q = kmap(sg[0].page) + sg[0].offset;
346 hexdump(q, cipher_tv[i].rlen);
347
348 printk("%s\n",
349 memcmp(q, cipher_tv[i].result, cipher_tv[i].rlen) ? "fail" :
350 "pass");
351 }
352 }
353
354 printk("\ntesting %s %s %s across pages (chunking) \n", algo, m, e);
355 memset(xbuf, 0, XBUFSIZE);
356
357 j = 0;
358 for (i = 0; i < tcount; i++) {
359 if (cipher_tv[i].np) {
360 j++;
361 printk("test %u (%d bit key):\n",
362 j, cipher_tv[i].klen * 8);
363
364 tfm->crt_flags = 0;
365 if (cipher_tv[i].wk)
366 tfm->crt_flags |= CRYPTO_TFM_REQ_WEAK_KEY;
367 key = cipher_tv[i].key;
368
369 ret = crypto_cipher_setkey(tfm, key, cipher_tv[i].klen);
370 if (ret) {
371 printk("setkey() failed flags=%x\n", tfm->crt_flags);
372
373 if (!cipher_tv[i].fail)
374 goto out;
375 }
376
377 temp = 0;
378 for (k = 0; k < cipher_tv[i].np; k++) {
379 memcpy (&xbuf[IDX[k]], cipher_tv[i].input + temp,
380 cipher_tv[i].tap[k]);
381 temp += cipher_tv[i].tap[k];
382 p = &xbuf[IDX[k]];
383 sg[k].page = virt_to_page (p);
384 sg[k].offset = offset_in_page (p);
385 sg[k].length = cipher_tv[i].tap[k];
386 }
387
388 if (!mode) {
389 crypto_cipher_set_iv(tfm, cipher_tv[i].iv,
390 crypto_tfm_alg_ivsize (tfm));
391 }
392
393 if (enc)
394 ret = crypto_cipher_encrypt(tfm, sg, sg, cipher_tv[i].ilen);
395 else
396 ret = crypto_cipher_decrypt(tfm, sg, sg, cipher_tv[i].ilen);
397
398 if (ret) {
399 printk("%s () failed flags=%x\n", e, tfm->crt_flags);
400 goto out;
401 }
402
403 temp = 0;
404 for (k = 0; k < cipher_tv[i].np; k++) {
405 printk("page %u\n", k);
406 q = kmap(sg[k].page) + sg[k].offset;
407 hexdump(q, cipher_tv[i].tap[k]);
408 printk("%s\n",
409 memcmp(q, cipher_tv[i].result + temp,
410 cipher_tv[i].tap[k]) ? "fail" :
411 "pass");
412 temp += cipher_tv[i].tap[k];
413 }
414 }
415 }
416
417 out:
418 crypto_free_tfm(tfm);
419 }
420
421 static void
test_deflate(void)422 test_deflate(void)
423 {
424 unsigned int i;
425 char result[COMP_BUF_SIZE];
426 struct crypto_tfm *tfm;
427 struct comp_testvec *tv;
428 unsigned int tsize;
429
430 printk("\ntesting deflate compression\n");
431
432 tsize = sizeof (deflate_comp_tv_template);
433 if (tsize > TVMEMSIZE) {
434 printk("template (%u) too big for tvmem (%u)\n", tsize,
435 TVMEMSIZE);
436 return;
437 }
438
439 memcpy(tvmem, deflate_comp_tv_template, tsize);
440 tv = (void *) tvmem;
441
442 tfm = crypto_alloc_tfm("deflate", 0);
443 if (tfm == NULL) {
444 printk("failed to load transform for deflate\n");
445 return;
446 }
447
448 for (i = 0; i < DEFLATE_COMP_TEST_VECTORS; i++) {
449 int ilen, ret, dlen = COMP_BUF_SIZE;
450
451 printk("test %u:\n", i + 1);
452 memset(result, 0, sizeof (result));
453
454 ilen = tv[i].inlen;
455 ret = crypto_comp_compress(tfm, tv[i].input,
456 ilen, result, &dlen);
457 if (ret) {
458 printk("fail: ret=%d\n", ret);
459 continue;
460 }
461 hexdump(result, dlen);
462 printk("%s (ratio %d:%d)\n",
463 memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
464 ilen, dlen);
465 }
466
467 printk("\ntesting deflate decompression\n");
468
469 tsize = sizeof (deflate_decomp_tv_template);
470 if (tsize > TVMEMSIZE) {
471 printk("template (%u) too big for tvmem (%u)\n", tsize,
472 TVMEMSIZE);
473 goto out;
474 }
475
476 memcpy(tvmem, deflate_decomp_tv_template, tsize);
477 tv = (void *) tvmem;
478
479 for (i = 0; i < DEFLATE_DECOMP_TEST_VECTORS; i++) {
480 int ilen, ret, dlen = COMP_BUF_SIZE;
481
482 printk("test %u:\n", i + 1);
483 memset(result, 0, sizeof (result));
484
485 ilen = tv[i].inlen;
486 ret = crypto_comp_decompress(tfm, tv[i].input,
487 ilen, result, &dlen);
488 if (ret) {
489 printk("fail: ret=%d\n", ret);
490 continue;
491 }
492 hexdump(result, dlen);
493 printk("%s (ratio %d:%d)\n",
494 memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
495 ilen, dlen);
496 }
497 out:
498 crypto_free_tfm(tfm);
499 }
500
501 static void
test_available(void)502 test_available(void)
503 {
504 char **name = check;
505
506 while (*name) {
507 printk("alg %s ", *name);
508 printk((crypto_alg_available(*name, 0)) ?
509 "found\n" : "not found\n");
510 name++;
511 }
512 }
513
514 static void
do_test(void)515 do_test(void)
516 {
517 switch (mode) {
518
519 case 0:
520 test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
521
522 test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
523
524 //DES
525 test_cipher ("des", MODE_ECB, ENCRYPT, des_enc_tv_template, DES_ENC_TEST_VECTORS);
526 test_cipher ("des", MODE_ECB, DECRYPT, des_dec_tv_template, DES_DEC_TEST_VECTORS);
527 test_cipher ("des", MODE_CBC, ENCRYPT, des_cbc_enc_tv_template, DES_CBC_ENC_TEST_VECTORS);
528 test_cipher ("des", MODE_CBC, DECRYPT, des_cbc_dec_tv_template, DES_CBC_DEC_TEST_VECTORS);
529
530 //DES3_EDE
531 test_cipher ("des3_ede", MODE_ECB, ENCRYPT, des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS);
532 test_cipher ("des3_ede", MODE_ECB, DECRYPT, des3_ede_dec_tv_template, DES3_EDE_DEC_TEST_VECTORS);
533
534 test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
535
536 test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
537
538 //BLOWFISH
539 test_cipher ("blowfish", MODE_ECB, ENCRYPT, bf_enc_tv_template, BF_ENC_TEST_VECTORS);
540 test_cipher ("blowfish", MODE_ECB, DECRYPT, bf_dec_tv_template, BF_DEC_TEST_VECTORS);
541 test_cipher ("blowfish", MODE_CBC, ENCRYPT, bf_cbc_enc_tv_template, BF_CBC_ENC_TEST_VECTORS);
542 test_cipher ("blowfish", MODE_CBC, DECRYPT, bf_cbc_dec_tv_template, BF_CBC_DEC_TEST_VECTORS);
543
544 //TWOFISH
545 test_cipher ("twofish", MODE_ECB, ENCRYPT, tf_enc_tv_template, TF_ENC_TEST_VECTORS);
546 test_cipher ("twofish", MODE_ECB, DECRYPT, tf_dec_tv_template, TF_DEC_TEST_VECTORS);
547 test_cipher ("twofish", MODE_CBC, ENCRYPT, tf_cbc_enc_tv_template, TF_CBC_ENC_TEST_VECTORS);
548 test_cipher ("twofish", MODE_CBC, DECRYPT, tf_cbc_dec_tv_template, TF_CBC_DEC_TEST_VECTORS);
549
550 //SERPENT
551 test_cipher ("serpent", MODE_ECB, ENCRYPT, serpent_enc_tv_template, SERPENT_ENC_TEST_VECTORS);
552 test_cipher ("serpent", MODE_ECB, DECRYPT, serpent_dec_tv_template, SERPENT_DEC_TEST_VECTORS);
553
554 //TNEPRES
555 test_cipher ("tnepres", MODE_ECB, ENCRYPT, tnepres_enc_tv_template, TNEPRES_ENC_TEST_VECTORS);
556 test_cipher ("tnepres", MODE_ECB, DECRYPT, tnepres_dec_tv_template, TNEPRES_DEC_TEST_VECTORS);
557
558 //AES
559 test_cipher ("aes", MODE_ECB, ENCRYPT, aes_enc_tv_template, AES_ENC_TEST_VECTORS);
560 test_cipher ("aes", MODE_ECB, DECRYPT, aes_dec_tv_template, AES_DEC_TEST_VECTORS);
561
562 //CAST5
563 test_cipher ("cast5", MODE_ECB, ENCRYPT, cast5_enc_tv_template, CAST5_ENC_TEST_VECTORS);
564 test_cipher ("cast5", MODE_ECB, DECRYPT, cast5_dec_tv_template, CAST5_DEC_TEST_VECTORS);
565
566 //CAST6
567 test_cipher ("cast6", MODE_ECB, ENCRYPT, cast6_enc_tv_template, CAST6_ENC_TEST_VECTORS);
568 test_cipher ("cast6", MODE_ECB, DECRYPT, cast6_dec_tv_template, CAST6_DEC_TEST_VECTORS);
569
570 //ARC4
571 test_cipher ("arc4", MODE_ECB, ENCRYPT, arc4_enc_tv_template, ARC4_ENC_TEST_VECTORS);
572 test_cipher ("arc4", MODE_ECB, DECRYPT, arc4_dec_tv_template, ARC4_DEC_TEST_VECTORS);
573
574 //TEA
575 test_cipher ("tea", MODE_ECB, ENCRYPT, tea_enc_tv_template, TEA_ENC_TEST_VECTORS);
576 test_cipher ("tea", MODE_ECB, DECRYPT, tea_dec_tv_template, TEA_DEC_TEST_VECTORS);
577
578
579 //XTEA
580 test_cipher ("xtea", MODE_ECB, ENCRYPT, xtea_enc_tv_template, XTEA_ENC_TEST_VECTORS);
581 test_cipher ("xtea", MODE_ECB, DECRYPT, xtea_dec_tv_template, XTEA_DEC_TEST_VECTORS);
582
583 //KHAZAD
584 test_cipher ("khazad", MODE_ECB, ENCRYPT, khazad_enc_tv_template, KHAZAD_ENC_TEST_VECTORS);
585 test_cipher ("khazad", MODE_ECB, DECRYPT, khazad_dec_tv_template, KHAZAD_DEC_TEST_VECTORS);
586
587 //ANUBIS
588 test_cipher ("anubis", MODE_ECB, ENCRYPT, anubis_enc_tv_template, ANUBIS_ENC_TEST_VECTORS);
589 test_cipher ("anubis", MODE_ECB, DECRYPT, anubis_dec_tv_template, ANUBIS_DEC_TEST_VECTORS);
590 test_cipher ("anubis", MODE_CBC, ENCRYPT, anubis_cbc_enc_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
591 test_cipher ("anubis", MODE_CBC, DECRYPT, anubis_cbc_dec_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
592
593 //XETA
594 test_cipher ("xeta", MODE_ECB, ENCRYPT, xeta_enc_tv_template, XETA_ENC_TEST_VECTORS);
595 test_cipher ("xeta", MODE_ECB, DECRYPT, xeta_dec_tv_template, XETA_DEC_TEST_VECTORS);
596
597 test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
598 test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
599 test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
600 test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
601 test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
602 test_deflate();
603 #ifdef CONFIG_CRYPTO_HMAC
604 test_hmac("md5", hmac_md5_tv_template, HMAC_MD5_TEST_VECTORS);
605 test_hmac("sha1", hmac_sha1_tv_template, HMAC_SHA1_TEST_VECTORS);
606 test_hmac("sha256", hmac_sha256_tv_template, HMAC_SHA256_TEST_VECTORS);
607 #endif
608
609 test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
610 break;
611
612 case 1:
613 test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
614 break;
615
616 case 2:
617 test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
618 break;
619
620 case 3:
621 test_cipher ("des", MODE_ECB, ENCRYPT, des_enc_tv_template, DES_ENC_TEST_VECTORS);
622 test_cipher ("des", MODE_ECB, DECRYPT, des_dec_tv_template, DES_DEC_TEST_VECTORS);
623 test_cipher ("des", MODE_CBC, ENCRYPT, des_cbc_enc_tv_template, DES_CBC_ENC_TEST_VECTORS);
624 test_cipher ("des", MODE_CBC, DECRYPT, des_cbc_dec_tv_template, DES_CBC_DEC_TEST_VECTORS);
625 break;
626
627 case 4:
628 test_cipher ("des3_ede", MODE_ECB, ENCRYPT, des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS);
629 test_cipher ("des3_ede", MODE_ECB, DECRYPT, des3_ede_dec_tv_template, DES3_EDE_DEC_TEST_VECTORS);
630 break;
631
632 case 5:
633 test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
634 break;
635
636 case 6:
637 test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
638 break;
639
640 case 7:
641 test_cipher ("blowfish", MODE_ECB, ENCRYPT, bf_enc_tv_template, BF_ENC_TEST_VECTORS);
642 test_cipher ("blowfish", MODE_ECB, DECRYPT, bf_dec_tv_template, BF_DEC_TEST_VECTORS);
643 test_cipher ("blowfish", MODE_CBC, ENCRYPT, bf_cbc_enc_tv_template, BF_CBC_ENC_TEST_VECTORS);
644 test_cipher ("blowfish", MODE_CBC, DECRYPT, bf_cbc_dec_tv_template, BF_CBC_DEC_TEST_VECTORS);
645 break;
646
647 case 8:
648 test_cipher ("twofish", MODE_ECB, ENCRYPT, tf_enc_tv_template, TF_ENC_TEST_VECTORS);
649 test_cipher ("twofish", MODE_ECB, DECRYPT, tf_dec_tv_template, TF_DEC_TEST_VECTORS);
650 test_cipher ("twofish", MODE_CBC, ENCRYPT, tf_cbc_enc_tv_template, TF_CBC_ENC_TEST_VECTORS);
651 test_cipher ("twofish", MODE_CBC, DECRYPT, tf_cbc_dec_tv_template, TF_CBC_DEC_TEST_VECTORS);
652 break;
653
654 case 9:
655 test_cipher ("serpent", MODE_ECB, ENCRYPT, serpent_enc_tv_template, SERPENT_ENC_TEST_VECTORS);
656 test_cipher ("serpent", MODE_ECB, DECRYPT, serpent_dec_tv_template, SERPENT_DEC_TEST_VECTORS);
657 break;
658
659 case 10:
660 test_cipher ("aes", MODE_ECB, ENCRYPT, aes_enc_tv_template, AES_ENC_TEST_VECTORS);
661 test_cipher ("aes", MODE_ECB, DECRYPT, aes_dec_tv_template, AES_DEC_TEST_VECTORS);
662 break;
663
664 case 11:
665 test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
666 break;
667
668 case 12:
669 test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
670 break;
671
672 case 13:
673 test_deflate();
674 break;
675
676 case 14:
677 test_cipher ("cast5", MODE_ECB, ENCRYPT, cast5_enc_tv_template, CAST5_ENC_TEST_VECTORS);
678 test_cipher ("cast5", MODE_ECB, DECRYPT, cast5_dec_tv_template, CAST5_DEC_TEST_VECTORS);
679 break;
680
681 case 15:
682 test_cipher ("cast6", MODE_ECB, ENCRYPT, cast6_enc_tv_template, CAST6_ENC_TEST_VECTORS);
683 test_cipher ("cast6", MODE_ECB, DECRYPT, cast6_dec_tv_template, CAST6_DEC_TEST_VECTORS);
684 break;
685
686 case 16:
687 test_cipher ("arc4", MODE_ECB, ENCRYPT, arc4_enc_tv_template, ARC4_ENC_TEST_VECTORS);
688 test_cipher ("arc4", MODE_ECB, DECRYPT, arc4_dec_tv_template, ARC4_DEC_TEST_VECTORS);
689 break;
690
691 case 17:
692 test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
693 break;
694
695 case 19:
696 test_cipher ("tea", MODE_ECB, ENCRYPT, tea_enc_tv_template, TEA_ENC_TEST_VECTORS);
697 test_cipher ("tea", MODE_ECB, DECRYPT, tea_dec_tv_template, TEA_DEC_TEST_VECTORS);
698 break;
699
700 case 20:
701 test_cipher ("xtea", MODE_ECB, ENCRYPT, xtea_enc_tv_template, XTEA_ENC_TEST_VECTORS);
702 test_cipher ("xtea", MODE_ECB, DECRYPT, xtea_dec_tv_template, XTEA_DEC_TEST_VECTORS);
703 break;
704
705 case 21:
706 test_cipher ("khazad", MODE_ECB, ENCRYPT, khazad_enc_tv_template, KHAZAD_ENC_TEST_VECTORS);
707 test_cipher ("khazad", MODE_ECB, DECRYPT, khazad_dec_tv_template, KHAZAD_DEC_TEST_VECTORS);
708 case 22:
709 test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
710 break;
711
712 case 23:
713 test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
714 break;
715
716 case 24:
717 test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
718 break;
719
720 case 25:
721 test_cipher ("tnepres", MODE_ECB, ENCRYPT, tnepres_enc_tv_template, TNEPRES_ENC_TEST_VECTORS);
722 test_cipher ("tnepres", MODE_ECB, DECRYPT, tnepres_dec_tv_template, TNEPRES_DEC_TEST_VECTORS);
723 break;
724
725 case 26:
726 test_cipher ("anubis", MODE_ECB, ENCRYPT, anubis_enc_tv_template, ANUBIS_ENC_TEST_VECTORS);
727 test_cipher ("anubis", MODE_ECB, DECRYPT, anubis_dec_tv_template, ANUBIS_DEC_TEST_VECTORS);
728 test_cipher ("anubis", MODE_CBC, ENCRYPT, anubis_cbc_enc_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
729 test_cipher ("anubis", MODE_CBC, DECRYPT, anubis_cbc_dec_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
730 break;
731
732 case 30:
733 test_cipher ("xeta", MODE_ECB, ENCRYPT, xeta_enc_tv_template, XETA_ENC_TEST_VECTORS);
734 test_cipher ("xeta", MODE_ECB, DECRYPT, xeta_dec_tv_template, XETA_DEC_TEST_VECTORS);
735 break;
736
737 #ifdef CONFIG_CRYPTO_HMAC
738 case 100:
739 test_hmac("md5", hmac_md5_tv_template, HMAC_MD5_TEST_VECTORS);
740 break;
741
742 case 101:
743 test_hmac("sha1", hmac_sha1_tv_template, HMAC_SHA1_TEST_VECTORS);
744 break;
745
746 case 102:
747 test_hmac("sha256", hmac_sha256_tv_template, HMAC_SHA256_TEST_VECTORS);
748 break;
749
750 #endif
751
752 case 1000:
753 test_available();
754 break;
755
756 default:
757 /* useful for debugging */
758 printk("not testing anything\n");
759 break;
760 }
761 }
762
763 static int __init
init(void)764 init(void)
765 {
766 tvmem = kmalloc(TVMEMSIZE, GFP_KERNEL);
767 if (tvmem == NULL)
768 return -ENOMEM;
769
770 xbuf = kmalloc(XBUFSIZE, GFP_KERNEL);
771 if (xbuf == NULL) {
772 kfree(tvmem);
773 return -ENOMEM;
774 }
775
776 do_test();
777
778 kfree(xbuf);
779 kfree(tvmem);
780 return 0;
781 }
782
783 /*
784 * If an init function is provided, an exit function must also be provided
785 * to allow module unload.
786 */
fini(void)787 static void __exit fini(void) { }
788
789 module_init(init);
790 module_exit(fini);
791
792 MODULE_PARM(mode, "i");
793
794 MODULE_LICENSE("GPL");
795 MODULE_DESCRIPTION("Quick & dirty crypto testing module");
796 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
797