[CRYPTO] tcrypt: Use block ciphers where applicable
[linux-2.6.git] / crypto / tcrypt.c
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  * 2004-08-09 Added cipher speed tests (Reyk Floeter <reyk@vantronix.net>)
16  * 2003-09-14 Rewritten by Kartikey Mahendra Bhatt
17  *
18  */
19
20 #include <linux/err.h>
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/mm.h>
24 #include <linux/slab.h>
25 #include <linux/scatterlist.h>
26 #include <linux/string.h>
27 #include <linux/crypto.h>
28 #include <linux/highmem.h>
29 #include <linux/moduleparam.h>
30 #include <linux/jiffies.h>
31 #include <linux/timex.h>
32 #include <linux/interrupt.h>
33 #include "tcrypt.h"
34
35 /*
36  * Need to kmalloc() memory for testing kmap().
37  */
38 #define TVMEMSIZE       16384
39 #define XBUFSIZE        32768
40
41 /*
42  * Indexes into the xbuf to simulate cross-page access.
43  */
44 #define IDX1            37
45 #define IDX2            32400
46 #define IDX3            1
47 #define IDX4            8193
48 #define IDX5            22222
49 #define IDX6            17101
50 #define IDX7            27333
51 #define IDX8            3000
52
53 /*
54 * Used by test_cipher()
55 */
56 #define ENCRYPT 1
57 #define DECRYPT 0
58
59 static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
60
61 /*
62  * Used by test_cipher_speed()
63  */
64 static unsigned int sec;
65
66 static int mode;
67 static char *xbuf;
68 static char *tvmem;
69
70 static char *check[] = {
71         "des", "md5", "des3_ede", "rot13", "sha1", "sha256", "blowfish",
72         "twofish", "serpent", "sha384", "sha512", "md4", "aes", "cast6",
73         "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
74         "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", NULL
75 };
76
77 static void hexdump(unsigned char *buf, unsigned int len)
78 {
79         while (len--)
80                 printk("%02x", *buf++);
81
82         printk("\n");
83 }
84
85 static void test_hash(char *algo, struct hash_testvec *template,
86                       unsigned int tcount)
87 {
88         unsigned int i, j, k, temp;
89         struct scatterlist sg[8];
90         char result[64];
91         struct crypto_tfm *tfm;
92         struct hash_testvec *hash_tv;
93         unsigned int tsize;
94
95         printk("\ntesting %s\n", algo);
96
97         tsize = sizeof(struct hash_testvec);
98         tsize *= tcount;
99
100         if (tsize > TVMEMSIZE) {
101                 printk("template (%u) too big for tvmem (%u)\n", tsize, TVMEMSIZE);
102                 return;
103         }
104
105         memcpy(tvmem, template, tsize);
106         hash_tv = (void *)tvmem;
107         tfm = crypto_alloc_tfm(algo, 0);
108         if (tfm == NULL) {
109                 printk("failed to load transform for %s\n", algo);
110                 return;
111         }
112
113         for (i = 0; i < tcount; i++) {
114                 printk("test %u:\n", i + 1);
115                 memset(result, 0, 64);
116
117                 sg_set_buf(&sg[0], hash_tv[i].plaintext, hash_tv[i].psize);
118
119                 crypto_digest_init(tfm);
120                 crypto_digest_setkey(tfm, hash_tv[i].key, hash_tv[i].ksize);
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)) ?
128                        "fail" : "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]],
146                                        hash_tv[i].plaintext + temp,
147                                        hash_tv[i].tap[k]);
148                                 temp += hash_tv[i].tap[k];
149                                 sg_set_buf(&sg[k], &xbuf[IDX[k]],
150                                             hash_tv[i].tap[k]);
151                         }
152
153                         crypto_digest_digest(tfm, sg, hash_tv[i].np, result);
154
155                         hexdump(result, crypto_tfm_alg_digestsize(tfm));
156                         printk("%s\n",
157                                memcmp(result, hash_tv[i].digest,
158                                       crypto_tfm_alg_digestsize(tfm)) ?
159                                "fail" : "pass");
160                 }
161         }
162
163         crypto_free_tfm(tfm);
164 }
165
166
167 #ifdef CONFIG_CRYPTO_HMAC
168
169 static void test_hmac(char *algo, struct hmac_testvec *template,
170                       unsigned int tcount)
171 {
172         unsigned int i, j, k, temp;
173         struct scatterlist sg[8];
174         char result[64];
175         struct crypto_tfm *tfm;
176         struct hmac_testvec *hmac_tv;
177         unsigned int tsize, klen;
178
179         tfm = crypto_alloc_tfm(algo, 0);
180         if (tfm == NULL) {
181                 printk("failed to load transform for %s\n", algo);
182                 return;
183         }
184
185         printk("\ntesting hmac_%s\n", algo);
186
187         tsize = sizeof(struct hmac_testvec);
188         tsize *= tcount;
189         if (tsize > TVMEMSIZE) {
190                 printk("template (%u) too big for tvmem (%u)\n", tsize,
191                        TVMEMSIZE);
192                 goto out;
193         }
194
195         memcpy(tvmem, template, tsize);
196         hmac_tv = (void *)tvmem;
197
198         for (i = 0; i < tcount; i++) {
199                 printk("test %u:\n", i + 1);
200                 memset(result, 0, sizeof (result));
201
202                 klen = hmac_tv[i].ksize;
203                 sg_set_buf(&sg[0], hmac_tv[i].plaintext, hmac_tv[i].psize);
204
205                 crypto_hmac(tfm, hmac_tv[i].key, &klen, sg, 1, result);
206
207                 hexdump(result, crypto_tfm_alg_digestsize(tfm));
208                 printk("%s\n",
209                        memcmp(result, hmac_tv[i].digest,
210                               crypto_tfm_alg_digestsize(tfm)) ? "fail" :
211                        "pass");
212         }
213
214         printk("\ntesting hmac_%s across pages\n", algo);
215
216         memset(xbuf, 0, XBUFSIZE);
217
218         j = 0;
219         for (i = 0; i < tcount; i++) {
220                 if (hmac_tv[i].np) {
221                         j++;
222                         printk("test %u:\n",j);
223                         memset(result, 0, 64);
224
225                         temp = 0;
226                         klen = hmac_tv[i].ksize;
227                         for (k = 0; k < hmac_tv[i].np; k++) {
228                                 memcpy(&xbuf[IDX[k]],
229                                        hmac_tv[i].plaintext + temp,
230                                        hmac_tv[i].tap[k]);
231                                 temp += hmac_tv[i].tap[k];
232                                 sg_set_buf(&sg[k], &xbuf[IDX[k]],
233                                             hmac_tv[i].tap[k]);
234                         }
235
236                         crypto_hmac(tfm, hmac_tv[i].key, &klen, sg,
237                                     hmac_tv[i].np, result);
238                         hexdump(result, crypto_tfm_alg_digestsize(tfm));
239
240                         printk("%s\n",
241                                memcmp(result, hmac_tv[i].digest,
242                                       crypto_tfm_alg_digestsize(tfm)) ?
243                                "fail" : "pass");
244                 }
245         }
246 out:
247         crypto_free_tfm(tfm);
248 }
249
250 #endif  /* CONFIG_CRYPTO_HMAC */
251
252 static void test_cipher(char *algo, int enc,
253                         struct cipher_testvec *template, unsigned int tcount)
254 {
255         unsigned int ret, i, j, k, temp;
256         unsigned int tsize;
257         unsigned int iv_len;
258         unsigned int len;
259         char *q;
260         struct crypto_blkcipher *tfm;
261         char *key;
262         struct cipher_testvec *cipher_tv;
263         struct blkcipher_desc desc;
264         struct scatterlist sg[8];
265         const char *e;
266
267         if (enc == ENCRYPT)
268                 e = "encryption";
269         else
270                 e = "decryption";
271
272         printk("\ntesting %s %s\n", algo, e);
273
274         tsize = sizeof (struct cipher_testvec);
275         tsize *= tcount;
276
277         if (tsize > TVMEMSIZE) {
278                 printk("template (%u) too big for tvmem (%u)\n", tsize,
279                        TVMEMSIZE);
280                 return;
281         }
282
283         memcpy(tvmem, template, tsize);
284         cipher_tv = (void *)tvmem;
285
286         tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
287
288         if (IS_ERR(tfm)) {
289                 printk("failed to load transform for %s: %ld\n", algo,
290                        PTR_ERR(tfm));
291                 return;
292         }
293         desc.tfm = tfm;
294         desc.flags = 0;
295
296         j = 0;
297         for (i = 0; i < tcount; i++) {
298                 if (!(cipher_tv[i].np)) {
299                         j++;
300                         printk("test %u (%d bit key):\n",
301                         j, cipher_tv[i].klen * 8);
302
303                         crypto_blkcipher_clear_flags(tfm, ~0);
304                         if (cipher_tv[i].wk)
305                                 crypto_blkcipher_set_flags(
306                                         tfm, CRYPTO_TFM_REQ_WEAK_KEY);
307                         key = cipher_tv[i].key;
308
309                         ret = crypto_blkcipher_setkey(tfm, key,
310                                                       cipher_tv[i].klen);
311                         if (ret) {
312                                 printk("setkey() failed flags=%x\n",
313                                        crypto_blkcipher_get_flags(tfm));
314
315                                 if (!cipher_tv[i].fail)
316                                         goto out;
317                         }
318
319                         sg_set_buf(&sg[0], cipher_tv[i].input,
320                                    cipher_tv[i].ilen);
321
322                         iv_len = crypto_blkcipher_ivsize(tfm);
323                         if (iv_len)
324                                 crypto_blkcipher_set_iv(tfm, cipher_tv[i].iv,
325                                                         iv_len);
326
327                         len = cipher_tv[i].ilen;
328                         ret = enc ?
329                                 crypto_blkcipher_encrypt(&desc, sg, sg, len) :
330                                 crypto_blkcipher_decrypt(&desc, sg, sg, len);
331
332                         if (ret) {
333                                 printk("%s () failed flags=%x\n", e,
334                                        desc.flags);
335                                 goto out;
336                         }
337
338                         q = kmap(sg[0].page) + sg[0].offset;
339                         hexdump(q, cipher_tv[i].rlen);
340
341                         printk("%s\n",
342                                memcmp(q, cipher_tv[i].result,
343                                       cipher_tv[i].rlen) ? "fail" : "pass");
344                 }
345         }
346
347         printk("\ntesting %s %s across pages (chunking)\n", algo, e);
348         memset(xbuf, 0, XBUFSIZE);
349
350         j = 0;
351         for (i = 0; i < tcount; i++) {
352                 if (cipher_tv[i].np) {
353                         j++;
354                         printk("test %u (%d bit key):\n",
355                         j, cipher_tv[i].klen * 8);
356
357                         crypto_blkcipher_clear_flags(tfm, ~0);
358                         if (cipher_tv[i].wk)
359                                 crypto_blkcipher_set_flags(
360                                         tfm, CRYPTO_TFM_REQ_WEAK_KEY);
361                         key = cipher_tv[i].key;
362
363                         ret = crypto_blkcipher_setkey(tfm, key,
364                                                       cipher_tv[i].klen);
365                         if (ret) {
366                                 printk("setkey() failed flags=%x\n",
367                                        crypto_blkcipher_get_flags(tfm));
368
369                                 if (!cipher_tv[i].fail)
370                                         goto out;
371                         }
372
373                         temp = 0;
374                         for (k = 0; k < cipher_tv[i].np; k++) {
375                                 memcpy(&xbuf[IDX[k]],
376                                        cipher_tv[i].input + temp,
377                                        cipher_tv[i].tap[k]);
378                                 temp += cipher_tv[i].tap[k];
379                                 sg_set_buf(&sg[k], &xbuf[IDX[k]],
380                                            cipher_tv[i].tap[k]);
381                         }
382
383                         iv_len = crypto_blkcipher_ivsize(tfm);
384                         if (iv_len)
385                                 crypto_blkcipher_set_iv(tfm, cipher_tv[i].iv,
386                                                         iv_len);
387
388                         len = cipher_tv[i].ilen;
389                         ret = enc ?
390                                 crypto_blkcipher_encrypt(&desc, sg, sg, len) :
391                                 crypto_blkcipher_decrypt(&desc, sg, sg, len);
392
393                         if (ret) {
394                                 printk("%s () failed flags=%x\n", e,
395                                        desc.flags);
396                                 goto out;
397                         }
398
399                         temp = 0;
400                         for (k = 0; k < cipher_tv[i].np; k++) {
401                                 printk("page %u\n", k);
402                                 q = kmap(sg[k].page) + sg[k].offset;
403                                 hexdump(q, cipher_tv[i].tap[k]);
404                                 printk("%s\n",
405                                         memcmp(q, cipher_tv[i].result + temp,
406                                                 cipher_tv[i].tap[k]) ? "fail" :
407                                         "pass");
408                                 temp += cipher_tv[i].tap[k];
409                         }
410                 }
411         }
412
413 out:
414         crypto_free_blkcipher(tfm);
415 }
416
417 static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc, char *p,
418                                int blen, int sec)
419 {
420         struct scatterlist sg[1];
421         unsigned long start, end;
422         int bcount;
423         int ret;
424
425         sg_set_buf(sg, p, blen);
426
427         for (start = jiffies, end = start + sec * HZ, bcount = 0;
428              time_before(jiffies, end); bcount++) {
429                 if (enc)
430                         ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
431                 else
432                         ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
433
434                 if (ret)
435                         return ret;
436         }
437
438         printk("%d operations in %d seconds (%ld bytes)\n",
439                bcount, sec, (long)bcount * blen);
440         return 0;
441 }
442
443 static int test_cipher_cycles(struct blkcipher_desc *desc, int enc, char *p,
444                               int blen)
445 {
446         struct scatterlist sg[1];
447         unsigned long cycles = 0;
448         int ret = 0;
449         int i;
450
451         sg_set_buf(sg, p, blen);
452
453         local_bh_disable();
454         local_irq_disable();
455
456         /* Warm-up run. */
457         for (i = 0; i < 4; i++) {
458                 if (enc)
459                         ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
460                 else
461                         ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
462
463                 if (ret)
464                         goto out;
465         }
466
467         /* The real thing. */
468         for (i = 0; i < 8; i++) {
469                 cycles_t start, end;
470
471                 start = get_cycles();
472                 if (enc)
473                         ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
474                 else
475                         ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
476                 end = get_cycles();
477
478                 if (ret)
479                         goto out;
480
481                 cycles += end - start;
482         }
483
484 out:
485         local_irq_enable();
486         local_bh_enable();
487
488         if (ret == 0)
489                 printk("1 operation in %lu cycles (%d bytes)\n",
490                        (cycles + 4) / 8, blen);
491
492         return ret;
493 }
494
495 static void test_cipher_speed(char *algo, int enc, unsigned int sec,
496                               struct cipher_testvec *template,
497                               unsigned int tcount, struct cipher_speed *speed)
498 {
499         unsigned int ret, i, j, iv_len;
500         unsigned char *key, *p, iv[128];
501         struct crypto_blkcipher *tfm;
502         struct blkcipher_desc desc;
503         const char *e;
504
505         if (enc == ENCRYPT)
506                 e = "encryption";
507         else
508                 e = "decryption";
509
510         printk("\ntesting speed of %s %s\n", algo, e);
511
512         tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
513
514         if (IS_ERR(tfm)) {
515                 printk("failed to load transform for %s: %ld\n", algo,
516                        PTR_ERR(tfm));
517                 return;
518         }
519         desc.tfm = tfm;
520         desc.flags = 0;
521
522         for (i = 0; speed[i].klen != 0; i++) {
523                 if ((speed[i].blen + speed[i].klen) > TVMEMSIZE) {
524                         printk("template (%u) too big for tvmem (%u)\n",
525                                speed[i].blen + speed[i].klen, TVMEMSIZE);
526                         goto out;
527                 }
528
529                 printk("test %u (%d bit key, %d byte blocks): ", i,
530                        speed[i].klen * 8, speed[i].blen);
531
532                 memset(tvmem, 0xff, speed[i].klen + speed[i].blen);
533
534                 /* set key, plain text and IV */
535                 key = (unsigned char *)tvmem;
536                 for (j = 0; j < tcount; j++) {
537                         if (template[j].klen == speed[i].klen) {
538                                 key = template[j].key;
539                                 break;
540                         }
541                 }
542                 p = (unsigned char *)tvmem + speed[i].klen;
543
544                 ret = crypto_blkcipher_setkey(tfm, key, speed[i].klen);
545                 if (ret) {
546                         printk("setkey() failed flags=%x\n",
547                                crypto_blkcipher_get_flags(tfm));
548                         goto out;
549                 }
550
551                 iv_len = crypto_blkcipher_ivsize(tfm);
552                 if (iv_len) {
553                         memset(&iv, 0xff, iv_len);
554                         crypto_blkcipher_set_iv(tfm, iv, iv_len);
555                 }
556
557                 if (sec)
558                         ret = test_cipher_jiffies(&desc, enc, p, speed[i].blen,
559                                                   sec);
560                 else
561                         ret = test_cipher_cycles(&desc, enc, p, speed[i].blen);
562
563                 if (ret) {
564                         printk("%s() failed flags=%x\n", e, desc.flags);
565                         break;
566                 }
567         }
568
569 out:
570         crypto_free_blkcipher(tfm);
571 }
572
573 static void test_digest_jiffies(struct crypto_tfm *tfm, char *p, int blen,
574                                 int plen, char *out, int sec)
575 {
576         struct scatterlist sg[1];
577         unsigned long start, end;
578         int bcount, pcount;
579
580         for (start = jiffies, end = start + sec * HZ, bcount = 0;
581              time_before(jiffies, end); bcount++) {
582                 crypto_digest_init(tfm);
583                 for (pcount = 0; pcount < blen; pcount += plen) {
584                         sg_set_buf(sg, p + pcount, plen);
585                         crypto_digest_update(tfm, sg, 1);
586                 }
587                 /* we assume there is enough space in 'out' for the result */
588                 crypto_digest_final(tfm, out);
589         }
590
591         printk("%6u opers/sec, %9lu bytes/sec\n",
592                bcount / sec, ((long)bcount * blen) / sec);
593
594         return;
595 }
596
597 static void test_digest_cycles(struct crypto_tfm *tfm, char *p, int blen,
598                                int plen, char *out)
599 {
600         struct scatterlist sg[1];
601         unsigned long cycles = 0;
602         int i, pcount;
603
604         local_bh_disable();
605         local_irq_disable();
606
607         /* Warm-up run. */
608         for (i = 0; i < 4; i++) {
609                 crypto_digest_init(tfm);
610                 for (pcount = 0; pcount < blen; pcount += plen) {
611                         sg_set_buf(sg, p + pcount, plen);
612                         crypto_digest_update(tfm, sg, 1);
613                 }
614                 crypto_digest_final(tfm, out);
615         }
616
617         /* The real thing. */
618         for (i = 0; i < 8; i++) {
619                 cycles_t start, end;
620
621                 crypto_digest_init(tfm);
622
623                 start = get_cycles();
624
625                 for (pcount = 0; pcount < blen; pcount += plen) {
626                         sg_set_buf(sg, p + pcount, plen);
627                         crypto_digest_update(tfm, sg, 1);
628                 }
629                 crypto_digest_final(tfm, out);
630
631                 end = get_cycles();
632
633                 cycles += end - start;
634         }
635
636         local_irq_enable();
637         local_bh_enable();
638
639         printk("%6lu cycles/operation, %4lu cycles/byte\n",
640                cycles / 8, cycles / (8 * blen));
641
642         return;
643 }
644
645 static void test_digest_speed(char *algo, unsigned int sec,
646                               struct digest_speed *speed)
647 {
648         struct crypto_tfm *tfm;
649         char output[1024];
650         int i;
651
652         printk("\ntesting speed of %s\n", algo);
653
654         tfm = crypto_alloc_tfm(algo, 0);
655
656         if (tfm == NULL) {
657                 printk("failed to load transform for %s\n", algo);
658                 return;
659         }
660
661         if (crypto_tfm_alg_digestsize(tfm) > sizeof(output)) {
662                 printk("digestsize(%u) > outputbuffer(%zu)\n",
663                        crypto_tfm_alg_digestsize(tfm), sizeof(output));
664                 goto out;
665         }
666
667         for (i = 0; speed[i].blen != 0; i++) {
668                 if (speed[i].blen > TVMEMSIZE) {
669                         printk("template (%u) too big for tvmem (%u)\n",
670                                speed[i].blen, TVMEMSIZE);
671                         goto out;
672                 }
673
674                 printk("test%3u (%5u byte blocks,%5u bytes per update,%4u updates): ",
675                        i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
676
677                 memset(tvmem, 0xff, speed[i].blen);
678
679                 if (sec)
680                         test_digest_jiffies(tfm, tvmem, speed[i].blen, speed[i].plen, output, sec);
681                 else
682                         test_digest_cycles(tfm, tvmem, speed[i].blen, speed[i].plen, output);
683         }
684
685 out:
686         crypto_free_tfm(tfm);
687 }
688
689 static void test_deflate(void)
690 {
691         unsigned int i;
692         char result[COMP_BUF_SIZE];
693         struct crypto_tfm *tfm;
694         struct comp_testvec *tv;
695         unsigned int tsize;
696
697         printk("\ntesting deflate compression\n");
698
699         tsize = sizeof (deflate_comp_tv_template);
700         if (tsize > TVMEMSIZE) {
701                 printk("template (%u) too big for tvmem (%u)\n", tsize,
702                        TVMEMSIZE);
703                 return;
704         }
705
706         memcpy(tvmem, deflate_comp_tv_template, tsize);
707         tv = (void *)tvmem;
708
709         tfm = crypto_alloc_tfm("deflate", 0);
710         if (tfm == NULL) {
711                 printk("failed to load transform for deflate\n");
712                 return;
713         }
714
715         for (i = 0; i < DEFLATE_COMP_TEST_VECTORS; i++) {
716                 int ilen, ret, dlen = COMP_BUF_SIZE;
717
718                 printk("test %u:\n", i + 1);
719                 memset(result, 0, sizeof (result));
720
721                 ilen = tv[i].inlen;
722                 ret = crypto_comp_compress(tfm, tv[i].input,
723                                            ilen, result, &dlen);
724                 if (ret) {
725                         printk("fail: ret=%d\n", ret);
726                         continue;
727                 }
728                 hexdump(result, dlen);
729                 printk("%s (ratio %d:%d)\n",
730                        memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
731                        ilen, dlen);
732         }
733
734         printk("\ntesting deflate decompression\n");
735
736         tsize = sizeof (deflate_decomp_tv_template);
737         if (tsize > TVMEMSIZE) {
738                 printk("template (%u) too big for tvmem (%u)\n", tsize,
739                        TVMEMSIZE);
740                 goto out;
741         }
742
743         memcpy(tvmem, deflate_decomp_tv_template, tsize);
744         tv = (void *)tvmem;
745
746         for (i = 0; i < DEFLATE_DECOMP_TEST_VECTORS; i++) {
747                 int ilen, ret, dlen = COMP_BUF_SIZE;
748
749                 printk("test %u:\n", i + 1);
750                 memset(result, 0, sizeof (result));
751
752                 ilen = tv[i].inlen;
753                 ret = crypto_comp_decompress(tfm, tv[i].input,
754                                              ilen, result, &dlen);
755                 if (ret) {
756                         printk("fail: ret=%d\n", ret);
757                         continue;
758                 }
759                 hexdump(result, dlen);
760                 printk("%s (ratio %d:%d)\n",
761                        memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
762                        ilen, dlen);
763         }
764 out:
765         crypto_free_tfm(tfm);
766 }
767
768 static void test_available(void)
769 {
770         char **name = check;
771
772         while (*name) {
773                 printk("alg %s ", *name);
774                 printk((crypto_alg_available(*name, 0)) ?
775                         "found\n" : "not found\n");
776                 name++;
777         }
778 }
779
780 static void do_test(void)
781 {
782         switch (mode) {
783
784         case 0:
785                 test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
786
787                 test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
788
789                 //DES
790                 test_cipher("ecb(des)", ENCRYPT, des_enc_tv_template,
791                             DES_ENC_TEST_VECTORS);
792                 test_cipher("ecb(des)", DECRYPT, des_dec_tv_template,
793                             DES_DEC_TEST_VECTORS);
794                 test_cipher("cbc(des)", ENCRYPT, des_cbc_enc_tv_template,
795                             DES_CBC_ENC_TEST_VECTORS);
796                 test_cipher("cbc(des)", DECRYPT, des_cbc_dec_tv_template,
797                             DES_CBC_DEC_TEST_VECTORS);
798
799                 //DES3_EDE
800                 test_cipher("ecb(des3_ede)", ENCRYPT, des3_ede_enc_tv_template,
801                             DES3_EDE_ENC_TEST_VECTORS);
802                 test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
803                             DES3_EDE_DEC_TEST_VECTORS);
804
805                 test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
806
807                 test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
808
809                 //BLOWFISH
810                 test_cipher("ecb(blowfish)", ENCRYPT, bf_enc_tv_template,
811                             BF_ENC_TEST_VECTORS);
812                 test_cipher("ecb(blowfish)", DECRYPT, bf_dec_tv_template,
813                             BF_DEC_TEST_VECTORS);
814                 test_cipher("cbc(blowfish)", ENCRYPT, bf_cbc_enc_tv_template,
815                             BF_CBC_ENC_TEST_VECTORS);
816                 test_cipher("cbc(blowfish)", DECRYPT, bf_cbc_dec_tv_template,
817                             BF_CBC_DEC_TEST_VECTORS);
818
819                 //TWOFISH
820                 test_cipher("ecb(twofish)", ENCRYPT, tf_enc_tv_template,
821                             TF_ENC_TEST_VECTORS);
822                 test_cipher("ecb(twofish)", DECRYPT, tf_dec_tv_template,
823                             TF_DEC_TEST_VECTORS);
824                 test_cipher("cbc(twofish)", ENCRYPT, tf_cbc_enc_tv_template,
825                             TF_CBC_ENC_TEST_VECTORS);
826                 test_cipher("cbc(twofish)", DECRYPT, tf_cbc_dec_tv_template,
827                             TF_CBC_DEC_TEST_VECTORS);
828
829                 //SERPENT
830                 test_cipher("ecb(serpent)", ENCRYPT, serpent_enc_tv_template,
831                             SERPENT_ENC_TEST_VECTORS);
832                 test_cipher("ecb(serpent)", DECRYPT, serpent_dec_tv_template,
833                             SERPENT_DEC_TEST_VECTORS);
834
835                 //TNEPRES
836                 test_cipher("ecb(tnepres)", ENCRYPT, tnepres_enc_tv_template,
837                             TNEPRES_ENC_TEST_VECTORS);
838                 test_cipher("ecb(tnepres)", DECRYPT, tnepres_dec_tv_template,
839                             TNEPRES_DEC_TEST_VECTORS);
840
841                 //AES
842                 test_cipher("ecb(aes)", ENCRYPT, aes_enc_tv_template,
843                             AES_ENC_TEST_VECTORS);
844                 test_cipher("ecb(aes)", DECRYPT, aes_dec_tv_template,
845                             AES_DEC_TEST_VECTORS);
846                 test_cipher("cbc(aes)", ENCRYPT, aes_cbc_enc_tv_template,
847                             AES_CBC_ENC_TEST_VECTORS);
848                 test_cipher("cbc(aes)", DECRYPT, aes_cbc_dec_tv_template,
849                             AES_CBC_DEC_TEST_VECTORS);
850
851                 //CAST5
852                 test_cipher("ecb(cast5)", ENCRYPT, cast5_enc_tv_template,
853                             CAST5_ENC_TEST_VECTORS);
854                 test_cipher("ecb(cast5)", DECRYPT, cast5_dec_tv_template,
855                             CAST5_DEC_TEST_VECTORS);
856
857                 //CAST6
858                 test_cipher("ecb(cast6)", ENCRYPT, cast6_enc_tv_template,
859                             CAST6_ENC_TEST_VECTORS);
860                 test_cipher("ecb(cast6)", DECRYPT, cast6_dec_tv_template,
861                             CAST6_DEC_TEST_VECTORS);
862
863                 //ARC4
864                 test_cipher("ecb(arc4)", ENCRYPT, arc4_enc_tv_template,
865                             ARC4_ENC_TEST_VECTORS);
866                 test_cipher("ecb(arc4)", DECRYPT, arc4_dec_tv_template,
867                             ARC4_DEC_TEST_VECTORS);
868
869                 //TEA
870                 test_cipher("ecb(tea)", ENCRYPT, tea_enc_tv_template,
871                             TEA_ENC_TEST_VECTORS);
872                 test_cipher("ecb(tea)", DECRYPT, tea_dec_tv_template,
873                             TEA_DEC_TEST_VECTORS);
874
875
876                 //XTEA
877                 test_cipher("ecb(xtea)", ENCRYPT, xtea_enc_tv_template,
878                             XTEA_ENC_TEST_VECTORS);
879                 test_cipher("ecb(xtea)", DECRYPT, xtea_dec_tv_template,
880                             XTEA_DEC_TEST_VECTORS);
881
882                 //KHAZAD
883                 test_cipher("ecb(khazad)", ENCRYPT, khazad_enc_tv_template,
884                             KHAZAD_ENC_TEST_VECTORS);
885                 test_cipher("ecb(khazad)", DECRYPT, khazad_dec_tv_template,
886                             KHAZAD_DEC_TEST_VECTORS);
887
888                 //ANUBIS
889                 test_cipher("ecb(anubis)", ENCRYPT, anubis_enc_tv_template,
890                             ANUBIS_ENC_TEST_VECTORS);
891                 test_cipher("ecb(anubis)", DECRYPT, anubis_dec_tv_template,
892                             ANUBIS_DEC_TEST_VECTORS);
893                 test_cipher("cbc(anubis)", ENCRYPT, anubis_cbc_enc_tv_template,
894                             ANUBIS_CBC_ENC_TEST_VECTORS);
895                 test_cipher("cbc(anubis)", DECRYPT, anubis_cbc_dec_tv_template,
896                             ANUBIS_CBC_ENC_TEST_VECTORS);
897
898                 //XETA
899                 test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
900                             XETA_ENC_TEST_VECTORS);
901                 test_cipher("ecb(xeta)", DECRYPT, xeta_dec_tv_template,
902                             XETA_DEC_TEST_VECTORS);
903
904                 test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
905                 test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
906                 test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
907                 test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
908                 test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
909                 test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
910                 test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
911                 test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
912                 test_deflate();
913                 test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
914 #ifdef CONFIG_CRYPTO_HMAC
915                 test_hmac("md5", hmac_md5_tv_template, HMAC_MD5_TEST_VECTORS);
916                 test_hmac("sha1", hmac_sha1_tv_template, HMAC_SHA1_TEST_VECTORS);
917                 test_hmac("sha256", hmac_sha256_tv_template, HMAC_SHA256_TEST_VECTORS);
918 #endif
919
920                 test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
921                 break;
922
923         case 1:
924                 test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
925                 break;
926
927         case 2:
928                 test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
929                 break;
930
931         case 3:
932                 test_cipher("ecb(des)", ENCRYPT, des_enc_tv_template,
933                             DES_ENC_TEST_VECTORS);
934                 test_cipher("ecb(des)", DECRYPT, des_dec_tv_template,
935                             DES_DEC_TEST_VECTORS);
936                 test_cipher("cbc(des)", ENCRYPT, des_cbc_enc_tv_template,
937                             DES_CBC_ENC_TEST_VECTORS);
938                 test_cipher("cbc(des)", DECRYPT, des_cbc_dec_tv_template,
939                             DES_CBC_DEC_TEST_VECTORS);
940                 break;
941
942         case 4:
943                 test_cipher("ecb(des3_ede)", ENCRYPT, des3_ede_enc_tv_template,
944                             DES3_EDE_ENC_TEST_VECTORS);
945                 test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
946                             DES3_EDE_DEC_TEST_VECTORS);
947                 break;
948
949         case 5:
950                 test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
951                 break;
952
953         case 6:
954                 test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
955                 break;
956
957         case 7:
958                 test_cipher("ecb(blowfish)", ENCRYPT, bf_enc_tv_template,
959                             BF_ENC_TEST_VECTORS);
960                 test_cipher("ecb(blowfish)", DECRYPT, bf_dec_tv_template,
961                             BF_DEC_TEST_VECTORS);
962                 test_cipher("cbc(blowfish)", ENCRYPT, bf_cbc_enc_tv_template,
963                             BF_CBC_ENC_TEST_VECTORS);
964                 test_cipher("cbc(blowfish)", DECRYPT, bf_cbc_dec_tv_template,
965                             BF_CBC_DEC_TEST_VECTORS);
966                 break;
967
968         case 8:
969                 test_cipher("ecb(twofish)", ENCRYPT, tf_enc_tv_template,
970                             TF_ENC_TEST_VECTORS);
971                 test_cipher("ecb(twofish)", DECRYPT, tf_dec_tv_template,
972                             TF_DEC_TEST_VECTORS);
973                 test_cipher("cbc(twofish)", ENCRYPT, tf_cbc_enc_tv_template,
974                             TF_CBC_ENC_TEST_VECTORS);
975                 test_cipher("cbc(twofish)", DECRYPT, tf_cbc_dec_tv_template,
976                             TF_CBC_DEC_TEST_VECTORS);
977                 break;
978
979         case 9:
980                 test_cipher("ecb(serpent)", ENCRYPT, serpent_enc_tv_template,
981                             SERPENT_ENC_TEST_VECTORS);
982                 test_cipher("ecb(serpent)", DECRYPT, serpent_dec_tv_template,
983                             SERPENT_DEC_TEST_VECTORS);
984                 break;
985
986         case 10:
987                 test_cipher("ecb(aes)", ENCRYPT, aes_enc_tv_template,
988                             AES_ENC_TEST_VECTORS);
989                 test_cipher("ecb(aes)", DECRYPT, aes_dec_tv_template,
990                             AES_DEC_TEST_VECTORS);
991                 test_cipher("cbc(aes)", ENCRYPT, aes_cbc_enc_tv_template,
992                             AES_CBC_ENC_TEST_VECTORS);
993                 test_cipher("cbc(aes)", DECRYPT, aes_cbc_dec_tv_template,
994                             AES_CBC_DEC_TEST_VECTORS);
995                 break;
996
997         case 11:
998                 test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
999                 break;
1000
1001         case 12:
1002                 test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
1003                 break;
1004
1005         case 13:
1006                 test_deflate();
1007                 break;
1008
1009         case 14:
1010                 test_cipher("ecb(cast5)", ENCRYPT, cast5_enc_tv_template,
1011                             CAST5_ENC_TEST_VECTORS);
1012                 test_cipher("ecb(cast5)", DECRYPT, cast5_dec_tv_template,
1013                             CAST5_DEC_TEST_VECTORS);
1014                 break;
1015
1016         case 15:
1017                 test_cipher("ecb(cast6)", ENCRYPT, cast6_enc_tv_template,
1018                             CAST6_ENC_TEST_VECTORS);
1019                 test_cipher("ecb(cast6)", DECRYPT, cast6_dec_tv_template,
1020                             CAST6_DEC_TEST_VECTORS);
1021                 break;
1022
1023         case 16:
1024                 test_cipher("ecb(arc4)", ENCRYPT, arc4_enc_tv_template,
1025                             ARC4_ENC_TEST_VECTORS);
1026                 test_cipher("ecb(arc4)", DECRYPT, arc4_dec_tv_template,
1027                             ARC4_DEC_TEST_VECTORS);
1028                 break;
1029
1030         case 17:
1031                 test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
1032                 break;
1033
1034         case 18:
1035                 test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
1036                 break;
1037
1038         case 19:
1039                 test_cipher("ecb(tea)", ENCRYPT, tea_enc_tv_template,
1040                             TEA_ENC_TEST_VECTORS);
1041                 test_cipher("ecb(tea)", DECRYPT, tea_dec_tv_template,
1042                             TEA_DEC_TEST_VECTORS);
1043                 break;
1044
1045         case 20:
1046                 test_cipher("ecb(xtea)", ENCRYPT, xtea_enc_tv_template,
1047                             XTEA_ENC_TEST_VECTORS);
1048                 test_cipher("ecb(xtea)", DECRYPT, xtea_dec_tv_template,
1049                             XTEA_DEC_TEST_VECTORS);
1050                 break;
1051
1052         case 21:
1053                 test_cipher("ecb(khazad)", ENCRYPT, khazad_enc_tv_template,
1054                             KHAZAD_ENC_TEST_VECTORS);
1055                 test_cipher("ecb(khazad)", DECRYPT, khazad_dec_tv_template,
1056                             KHAZAD_DEC_TEST_VECTORS);
1057                 break;
1058
1059         case 22:
1060                 test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
1061                 break;
1062
1063         case 23:
1064                 test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
1065                 break;
1066
1067         case 24:
1068                 test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
1069                 break;
1070
1071         case 25:
1072                 test_cipher("ecb(tnepres)", ENCRYPT, tnepres_enc_tv_template,
1073                             TNEPRES_ENC_TEST_VECTORS);
1074                 test_cipher("ecb(tnepres)", DECRYPT, tnepres_dec_tv_template,
1075                             TNEPRES_DEC_TEST_VECTORS);
1076                 break;
1077
1078         case 26:
1079                 test_cipher("ecb(anubis)", ENCRYPT, anubis_enc_tv_template,
1080                             ANUBIS_ENC_TEST_VECTORS);
1081                 test_cipher("ecb(anubis)", DECRYPT, anubis_dec_tv_template,
1082                             ANUBIS_DEC_TEST_VECTORS);
1083                 test_cipher("cbc(anubis)", ENCRYPT, anubis_cbc_enc_tv_template,
1084                             ANUBIS_CBC_ENC_TEST_VECTORS);
1085                 test_cipher("cbc(anubis)", DECRYPT, anubis_cbc_dec_tv_template,
1086                             ANUBIS_CBC_ENC_TEST_VECTORS);
1087                 break;
1088
1089         case 27:
1090                 test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
1091                 break;
1092
1093         case 28:
1094
1095                 test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
1096                 break;
1097
1098         case 29:
1099                 test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
1100                 break;
1101                 
1102         case 30:
1103                 test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
1104                             XETA_ENC_TEST_VECTORS);
1105                 test_cipher("ecb(xeta)", DECRYPT, xeta_dec_tv_template,
1106                             XETA_DEC_TEST_VECTORS);
1107                 break;
1108
1109 #ifdef CONFIG_CRYPTO_HMAC
1110         case 100:
1111                 test_hmac("md5", hmac_md5_tv_template, HMAC_MD5_TEST_VECTORS);
1112                 break;
1113
1114         case 101:
1115                 test_hmac("sha1", hmac_sha1_tv_template, HMAC_SHA1_TEST_VECTORS);
1116                 break;
1117
1118         case 102:
1119                 test_hmac("sha256", hmac_sha256_tv_template, HMAC_SHA256_TEST_VECTORS);
1120                 break;
1121
1122 #endif
1123
1124         case 200:
1125                 test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
1126                                   aes_speed_template);
1127                 test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
1128                                   aes_speed_template);
1129                 test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
1130                                   aes_speed_template);
1131                 test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
1132                                   aes_speed_template);
1133                 break;
1134
1135         case 201:
1136                 test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
1137                                   des3_ede_enc_tv_template,
1138                                   DES3_EDE_ENC_TEST_VECTORS,
1139                                   des3_ede_speed_template);
1140                 test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
1141                                   des3_ede_dec_tv_template,
1142                                   DES3_EDE_DEC_TEST_VECTORS,
1143                                   des3_ede_speed_template);
1144                 test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
1145                                   des3_ede_enc_tv_template,
1146                                   DES3_EDE_ENC_TEST_VECTORS,
1147                                   des3_ede_speed_template);
1148                 test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
1149                                   des3_ede_dec_tv_template,
1150                                   DES3_EDE_DEC_TEST_VECTORS,
1151                                   des3_ede_speed_template);
1152                 break;
1153
1154         case 202:
1155                 test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
1156                                   twofish_speed_template);
1157                 test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
1158                                   twofish_speed_template);
1159                 test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
1160                                   twofish_speed_template);
1161                 test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
1162                                   twofish_speed_template);
1163                 break;
1164
1165         case 203:
1166                 test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
1167                                   blowfish_speed_template);
1168                 test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
1169                                   blowfish_speed_template);
1170                 test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
1171                                   blowfish_speed_template);
1172                 test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
1173                                   blowfish_speed_template);
1174                 break;
1175
1176         case 204:
1177                 test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
1178                                   des_speed_template);
1179                 test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
1180                                   des_speed_template);
1181                 test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
1182                                   des_speed_template);
1183                 test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
1184                                   des_speed_template);
1185                 break;
1186
1187         case 300:
1188                 /* fall through */
1189
1190         case 301:
1191                 test_digest_speed("md4", sec, generic_digest_speed_template);
1192                 if (mode > 300 && mode < 400) break;
1193
1194         case 302:
1195                 test_digest_speed("md5", sec, generic_digest_speed_template);
1196                 if (mode > 300 && mode < 400) break;
1197
1198         case 303:
1199                 test_digest_speed("sha1", sec, generic_digest_speed_template);
1200                 if (mode > 300 && mode < 400) break;
1201
1202         case 304:
1203                 test_digest_speed("sha256", sec, generic_digest_speed_template);
1204                 if (mode > 300 && mode < 400) break;
1205
1206         case 305:
1207                 test_digest_speed("sha384", sec, generic_digest_speed_template);
1208                 if (mode > 300 && mode < 400) break;
1209
1210         case 306:
1211                 test_digest_speed("sha512", sec, generic_digest_speed_template);
1212                 if (mode > 300 && mode < 400) break;
1213
1214         case 307:
1215                 test_digest_speed("wp256", sec, generic_digest_speed_template);
1216                 if (mode > 300 && mode < 400) break;
1217
1218         case 308:
1219                 test_digest_speed("wp384", sec, generic_digest_speed_template);
1220                 if (mode > 300 && mode < 400) break;
1221
1222         case 309:
1223                 test_digest_speed("wp512", sec, generic_digest_speed_template);
1224                 if (mode > 300 && mode < 400) break;
1225
1226         case 310:
1227                 test_digest_speed("tgr128", sec, generic_digest_speed_template);
1228                 if (mode > 300 && mode < 400) break;
1229
1230         case 311:
1231                 test_digest_speed("tgr160", sec, generic_digest_speed_template);
1232                 if (mode > 300 && mode < 400) break;
1233
1234         case 312:
1235                 test_digest_speed("tgr192", sec, generic_digest_speed_template);
1236                 if (mode > 300 && mode < 400) break;
1237
1238         case 399:
1239                 break;
1240
1241         case 1000:
1242                 test_available();
1243                 break;
1244
1245         default:
1246                 /* useful for debugging */
1247                 printk("not testing anything\n");
1248                 break;
1249         }
1250 }
1251
1252 static int __init init(void)
1253 {
1254         tvmem = kmalloc(TVMEMSIZE, GFP_KERNEL);
1255         if (tvmem == NULL)
1256                 return -ENOMEM;
1257
1258         xbuf = kmalloc(XBUFSIZE, GFP_KERNEL);
1259         if (xbuf == NULL) {
1260                 kfree(tvmem);
1261                 return -ENOMEM;
1262         }
1263
1264         do_test();
1265
1266         kfree(xbuf);
1267         kfree(tvmem);
1268
1269         /* We intentionaly return -EAGAIN to prevent keeping
1270          * the module. It does all its work from init()
1271          * and doesn't offer any runtime functionality 
1272          * => we don't need it in the memory, do we?
1273          *                                        -- mludvig
1274          */
1275         return -EAGAIN;
1276 }
1277
1278 /*
1279  * If an init function is provided, an exit function must also be provided
1280  * to allow module unload.
1281  */
1282 static void __exit fini(void) { }
1283
1284 module_init(init);
1285 module_exit(fini);
1286
1287 module_param(mode, int, 0);
1288 module_param(sec, uint, 0);
1289 MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
1290                       "(defaults to zero which uses CPU cycles instead)");
1291
1292 MODULE_LICENSE("GPL");
1293 MODULE_DESCRIPTION("Quick & dirty crypto testing module");
1294 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");