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