crypto: tcrypt - Avoid using contiguous pages
[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  * Copyright (c) 2007 Nokia Siemens Networks
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU General Public License as published by the Free
13  * Software Foundation; either version 2 of the License, or (at your option)
14  * any later version.
15  *
16  */
17
18 #include <crypto/hash.h>
19 #include <linux/err.h>
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/mm.h>
23 #include <linux/slab.h>
24 #include <linux/scatterlist.h>
25 #include <linux/string.h>
26 #include <linux/crypto.h>
27 #include <linux/moduleparam.h>
28 #include <linux/jiffies.h>
29 #include <linux/timex.h>
30 #include <linux/interrupt.h>
31 #include "tcrypt.h"
32
33 /*
34  * Need slab memory for testing (size in number of pages).
35  */
36 #define TVMEMSIZE       4
37 #define XBUFSIZE        8
38
39 /*
40  * Indexes into the xbuf to simulate cross-page access.
41  */
42 #define IDX1            32
43 #define IDX2            32400
44 #define IDX3            1
45 #define IDX4            8193
46 #define IDX5            22222
47 #define IDX6            17101
48 #define IDX7            27333
49 #define IDX8            3000
50
51 /*
52 * Used by test_cipher()
53 */
54 #define ENCRYPT 1
55 #define DECRYPT 0
56
57 struct tcrypt_result {
58         struct completion completion;
59         int err;
60 };
61
62 static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
63
64 /*
65  * Used by test_cipher_speed()
66  */
67 static unsigned int sec;
68
69 static int mode;
70 static char *xbuf[XBUFSIZE];
71 static char *axbuf[XBUFSIZE];
72 static char *tvmem[TVMEMSIZE];
73
74 static char *check[] = {
75         "des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
76         "blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
77         "cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
78         "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta",  "fcrypt",
79         "camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
80         "lzo", "cts", NULL
81 };
82
83 static void hexdump(unsigned char *buf, unsigned int len)
84 {
85         print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
86                         16, 1,
87                         buf, len, false);
88 }
89
90 static void tcrypt_complete(struct crypto_async_request *req, int err)
91 {
92         struct tcrypt_result *res = req->data;
93
94         if (err == -EINPROGRESS)
95                 return;
96
97         res->err = err;
98         complete(&res->completion);
99 }
100
101 static void test_hash(char *algo, struct hash_testvec *template,
102                       unsigned int tcount)
103 {
104         unsigned int i, j, k, temp;
105         struct scatterlist sg[8];
106         char result[64];
107         struct crypto_ahash *tfm;
108         struct ahash_request *req;
109         struct tcrypt_result tresult;
110         int ret;
111         void *hash_buff;
112
113         printk("\ntesting %s\n", algo);
114
115         init_completion(&tresult.completion);
116
117         tfm = crypto_alloc_ahash(algo, 0, 0);
118         if (IS_ERR(tfm)) {
119                 printk("failed to load transform for %s: %ld\n", algo,
120                        PTR_ERR(tfm));
121                 return;
122         }
123
124         req = ahash_request_alloc(tfm, GFP_KERNEL);
125         if (!req) {
126                 printk(KERN_ERR "failed to allocate request for %s\n", algo);
127                 goto out_noreq;
128         }
129         ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
130                                    tcrypt_complete, &tresult);
131
132         for (i = 0; i < tcount; i++) {
133                 printk("test %u:\n", i + 1);
134                 memset(result, 0, 64);
135
136                 hash_buff = xbuf[0];
137
138                 memcpy(hash_buff, template[i].plaintext, template[i].psize);
139                 sg_init_one(&sg[0], hash_buff, template[i].psize);
140
141                 if (template[i].ksize) {
142                         crypto_ahash_clear_flags(tfm, ~0);
143                         ret = crypto_ahash_setkey(tfm, template[i].key,
144                                                   template[i].ksize);
145                         if (ret) {
146                                 printk("setkey() failed ret=%d\n", ret);
147                                 goto out;
148                         }
149                 }
150
151                 ahash_request_set_crypt(req, sg, result, template[i].psize);
152                 ret = crypto_ahash_digest(req);
153                 switch (ret) {
154                 case 0:
155                         break;
156                 case -EINPROGRESS:
157                 case -EBUSY:
158                         ret = wait_for_completion_interruptible(
159                                 &tresult.completion);
160                         if (!ret && !(ret = tresult.err)) {
161                                 INIT_COMPLETION(tresult.completion);
162                                 break;
163                         }
164                         /* fall through */
165                 default:
166                         printk("digest () failed ret=%d\n", ret);
167                         goto out;
168                 }
169
170                 hexdump(result, crypto_ahash_digestsize(tfm));
171                 printk("%s\n",
172                        memcmp(result, template[i].digest,
173                               crypto_ahash_digestsize(tfm)) ?
174                        "fail" : "pass");
175         }
176
177         printk("testing %s across pages\n", algo);
178
179         j = 0;
180         for (i = 0; i < tcount; i++) {
181                 if (template[i].np) {
182                         j++;
183                         printk("test %u:\n", j);
184                         memset(result, 0, 64);
185
186                         temp = 0;
187                         sg_init_table(sg, template[i].np);
188                         for (k = 0; k < template[i].np; k++) {
189                                 sg_set_buf(&sg[k],
190                                            memcpy(xbuf[IDX[k] >> PAGE_SHIFT] +
191                                                   offset_in_page(IDX[k]),
192                                                   template[i].plaintext + temp,
193                                                   template[i].tap[k]),
194                                            template[i].tap[k]);
195                                 temp += template[i].tap[k];
196                         }
197
198                         if (template[i].ksize) {
199                                 crypto_ahash_clear_flags(tfm, ~0);
200                                 ret = crypto_ahash_setkey(tfm, template[i].key,
201                                                           template[i].ksize);
202
203                                 if (ret) {
204                                         printk("setkey() failed ret=%d\n", ret);
205                                         goto out;
206                                 }
207                         }
208
209                         ahash_request_set_crypt(req, sg, result,
210                                                 template[i].psize);
211                         ret = crypto_ahash_digest(req);
212                         switch (ret) {
213                         case 0:
214                                 break;
215                         case -EINPROGRESS:
216                         case -EBUSY:
217                                 ret = wait_for_completion_interruptible(
218                                         &tresult.completion);
219                                 if (!ret && !(ret = tresult.err)) {
220                                         INIT_COMPLETION(tresult.completion);
221                                         break;
222                                 }
223                                 /* fall through */
224                         default:
225                                 printk("digest () failed ret=%d\n", ret);
226                                 goto out;
227                         }
228
229                         hexdump(result, crypto_ahash_digestsize(tfm));
230                         printk("%s\n",
231                                memcmp(result, template[i].digest,
232                                       crypto_ahash_digestsize(tfm)) ?
233                                "fail" : "pass");
234                 }
235         }
236
237 out:
238         ahash_request_free(req);
239 out_noreq:
240         crypto_free_ahash(tfm);
241 }
242
243 static void test_aead(char *algo, int enc, struct aead_testvec *template,
244                       unsigned int tcount)
245 {
246         unsigned int ret, i, j, k, n, temp;
247         char *q;
248         struct crypto_aead *tfm;
249         char *key;
250         struct aead_request *req;
251         struct scatterlist sg[8];
252         struct scatterlist asg[8];
253         const char *e;
254         struct tcrypt_result result;
255         unsigned int authsize;
256         void *input;
257         void *assoc;
258         char iv[MAX_IVLEN];
259
260         if (enc == ENCRYPT)
261                 e = "encryption";
262         else
263                 e = "decryption";
264
265         printk(KERN_INFO "\ntesting %s %s\n", algo, e);
266
267         init_completion(&result.completion);
268
269         tfm = crypto_alloc_aead(algo, 0, 0);
270
271         if (IS_ERR(tfm)) {
272                 printk(KERN_INFO "failed to load transform for %s: %ld\n",
273                        algo, PTR_ERR(tfm));
274                 return;
275         }
276
277         req = aead_request_alloc(tfm, GFP_KERNEL);
278         if (!req) {
279                 printk(KERN_INFO "failed to allocate request for %s\n", algo);
280                 goto out;
281         }
282
283         aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
284                                   tcrypt_complete, &result);
285
286         for (i = 0, j = 0; i < tcount; i++) {
287                 if (!template[i].np) {
288                         printk(KERN_INFO "test %u (%d bit key):\n",
289                                ++j, template[i].klen * 8);
290
291                         /* some tepmplates have no input data but they will
292                          * touch input
293                          */
294                         input = xbuf[0];
295                         assoc = axbuf[0];
296
297                         memcpy(input, template[i].input, template[i].ilen);
298                         memcpy(assoc, template[i].assoc, template[i].alen);
299                         if (template[i].iv)
300                                 memcpy(iv, template[i].iv, MAX_IVLEN);
301                         else
302                                 memset(iv, 0, MAX_IVLEN);
303
304                         crypto_aead_clear_flags(tfm, ~0);
305                         if (template[i].wk)
306                                 crypto_aead_set_flags(
307                                         tfm, CRYPTO_TFM_REQ_WEAK_KEY);
308
309                         key = template[i].key;
310
311                         ret = crypto_aead_setkey(tfm, key,
312                                                  template[i].klen);
313                         if (ret) {
314                                 printk(KERN_INFO "setkey() failed flags=%x\n",
315                                        crypto_aead_get_flags(tfm));
316
317                                 if (!template[i].fail)
318                                         continue;
319                         }
320
321                         authsize = abs(template[i].rlen - template[i].ilen);
322                         ret = crypto_aead_setauthsize(tfm, authsize);
323                         if (ret) {
324                                 printk(KERN_INFO
325                                        "failed to set authsize = %u\n",
326                                        authsize);
327                                 continue;
328                         }
329
330                         sg_init_one(&sg[0], input,
331                                     template[i].ilen + (enc ? authsize : 0));
332
333                         sg_init_one(&asg[0], assoc, template[i].alen);
334
335                         aead_request_set_crypt(req, sg, sg,
336                                                template[i].ilen, iv);
337
338                         aead_request_set_assoc(req, asg, template[i].alen);
339
340                         ret = enc ?
341                                 crypto_aead_encrypt(req) :
342                                 crypto_aead_decrypt(req);
343
344                         switch (ret) {
345                         case 0:
346                                 break;
347                         case -EINPROGRESS:
348                         case -EBUSY:
349                                 ret = wait_for_completion_interruptible(
350                                         &result.completion);
351                                 if (!ret && !(ret = result.err)) {
352                                         INIT_COMPLETION(result.completion);
353                                         break;
354                                 }
355                                 /* fall through */
356                         default:
357                                 printk(KERN_INFO "%s () failed err=%d\n",
358                                        e, -ret);
359                                 continue;
360                         }
361
362                         q = input;
363                         hexdump(q, template[i].rlen);
364
365                         printk(KERN_INFO "enc/dec: %s\n",
366                                memcmp(q, template[i].result,
367                                       template[i].rlen) ? "fail" : "pass");
368                 }
369         }
370
371         printk(KERN_INFO "\ntesting %s %s across pages (chunking)\n", algo, e);
372
373         for (i = 0, j = 0; i < tcount; i++) {
374                 if (template[i].np) {
375                         printk(KERN_INFO "test %u (%d bit key):\n",
376                                ++j, template[i].klen * 8);
377
378                         if (template[i].iv)
379                                 memcpy(iv, template[i].iv, MAX_IVLEN);
380                         else
381                                 memset(iv, 0, MAX_IVLEN);
382
383                         crypto_aead_clear_flags(tfm, ~0);
384                         if (template[i].wk)
385                                 crypto_aead_set_flags(
386                                         tfm, CRYPTO_TFM_REQ_WEAK_KEY);
387                         key = template[i].key;
388
389                         ret = crypto_aead_setkey(tfm, key, template[i].klen);
390                         if (ret) {
391                                 printk(KERN_INFO "setkey() failed flags=%x\n",
392                                        crypto_aead_get_flags(tfm));
393
394                                 if (!template[i].fail)
395                                         goto out;
396                         }
397
398                         authsize = abs(template[i].rlen - template[i].ilen);
399
400                         sg_init_table(sg, template[i].np);
401                         for (k = 0, temp = 0; k < template[i].np; k++) {
402                                 if (WARN_ON(offset_in_page(IDX[k]) +
403                                             template[i].tap[k] > PAGE_SIZE))
404                                         goto out;
405
406                                 q = xbuf[IDX[k] >> PAGE_SHIFT] +
407                                     offset_in_page(IDX[k]);
408
409                                 memcpy(q, template[i].input + temp,
410                                        template[i].tap[k]);
411
412                                 n = template[i].tap[k];
413                                 if (k == template[i].np - 1 && enc)
414                                         n += authsize;
415                                 if (offset_in_page(q) + n < PAGE_SIZE)
416                                         q[n] = 0;
417
418                                 sg_set_buf(&sg[k], q, template[i].tap[k]);
419                                 temp += template[i].tap[k];
420                         }
421
422                         ret = crypto_aead_setauthsize(tfm, authsize);
423                         if (ret) {
424                                 printk(KERN_INFO
425                                        "failed to set authsize = %u\n",
426                                        authsize);
427                                 goto out;
428                         }
429
430                         if (enc) {
431                                 if (WARN_ON(sg[k - 1].offset +
432                                             sg[k - 1].length + authsize >
433                                             PAGE_SIZE))
434                                         goto out;
435
436                                 sg[k - 1].length += authsize;
437                         }
438
439                         sg_init_table(asg, template[i].anp);
440                         for (k = 0, temp = 0; k < template[i].anp; k++) {
441                                 sg_set_buf(&asg[k],
442                                            memcpy(axbuf[IDX[k] >> PAGE_SHIFT] +
443                                                   offset_in_page(IDX[k]),
444                                                   template[i].assoc + temp,
445                                                   template[i].atap[k]),
446                                            template[i].atap[k]);
447                                 temp += template[i].atap[k];
448                         }
449
450                         aead_request_set_crypt(req, sg, sg,
451                                                template[i].ilen,
452                                                iv);
453
454                         aead_request_set_assoc(req, asg, template[i].alen);
455
456                         ret = enc ?
457                                 crypto_aead_encrypt(req) :
458                                 crypto_aead_decrypt(req);
459
460                         switch (ret) {
461                         case 0:
462                                 break;
463                         case -EINPROGRESS:
464                         case -EBUSY:
465                                 ret = wait_for_completion_interruptible(
466                                         &result.completion);
467                                 if (!ret && !(ret = result.err)) {
468                                         INIT_COMPLETION(result.completion);
469                                         break;
470                                 }
471                                 /* fall through */
472                         default:
473                                 printk(KERN_INFO "%s () failed err=%d\n",
474                                        e, -ret);
475                                 goto out;
476                         }
477
478                         for (k = 0, temp = 0; k < template[i].np; k++) {
479                                 printk(KERN_INFO "page %u\n", k);
480                                 q = xbuf[IDX[k] >> PAGE_SHIFT] +
481                                     offset_in_page(IDX[k]);
482
483                                 n = template[i].tap[k];
484                                 if (k == template[i].np - 1)
485                                         n += enc ? authsize : -authsize;
486                                 hexdump(q, n);
487                                 printk(KERN_INFO "%s\n",
488                                        memcmp(q, template[i].result + temp, n) ?
489                                        "fail" : "pass");
490
491                                 q += n;
492                                 if (k == template[i].np - 1 && !enc) {
493                                         if (memcmp(q, template[i].input +
494                                                       temp + n, authsize))
495                                                 n = authsize;
496                                         else
497                                                 n = 0;
498                                 } else {
499                                         for (n = 0; offset_in_page(q + n) &&
500                                                     q[n]; n++)
501                                                 ;
502                                 }
503                                 if (n) {
504                                         printk("Result buffer corruption %u "
505                                                "bytes:\n", n);
506                                         hexdump(q, n);
507                                 }
508
509                                 temp += template[i].tap[k];
510                         }
511                 }
512         }
513
514 out:
515         crypto_free_aead(tfm);
516         aead_request_free(req);
517 }
518
519 static void test_cipher(char *algo, int enc,
520                         struct cipher_testvec *template, unsigned int tcount)
521 {
522         unsigned int ret, i, j, k, n, temp;
523         char *q;
524         struct crypto_ablkcipher *tfm;
525         struct ablkcipher_request *req;
526         struct scatterlist sg[8];
527         const char *e;
528         struct tcrypt_result result;
529         void *data;
530         char iv[MAX_IVLEN];
531
532         if (enc == ENCRYPT)
533                 e = "encryption";
534         else
535                 e = "decryption";
536
537         printk("\ntesting %s %s\n", algo, e);
538
539         init_completion(&result.completion);
540         tfm = crypto_alloc_ablkcipher(algo, 0, 0);
541
542         if (IS_ERR(tfm)) {
543                 printk("failed to load transform for %s: %ld\n", algo,
544                        PTR_ERR(tfm));
545                 return;
546         }
547
548         req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
549         if (!req) {
550                 printk("failed to allocate request for %s\n", algo);
551                 goto out;
552         }
553
554         ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
555                                         tcrypt_complete, &result);
556
557         j = 0;
558         for (i = 0; i < tcount; i++) {
559                 if (template[i].iv)
560                         memcpy(iv, template[i].iv, MAX_IVLEN);
561                 else
562                         memset(iv, 0, MAX_IVLEN);
563
564                 if (!(template[i].np)) {
565                         j++;
566                         printk("test %u (%d bit key):\n",
567                         j, template[i].klen * 8);
568
569                         data = xbuf[0];
570                         memcpy(data, template[i].input, template[i].ilen);
571
572                         crypto_ablkcipher_clear_flags(tfm, ~0);
573                         if (template[i].wk)
574                                 crypto_ablkcipher_set_flags(
575                                         tfm, CRYPTO_TFM_REQ_WEAK_KEY);
576
577                         ret = crypto_ablkcipher_setkey(tfm, template[i].key,
578                                                        template[i].klen);
579                         if (ret) {
580                                 printk("setkey() failed flags=%x\n",
581                                        crypto_ablkcipher_get_flags(tfm));
582
583                                 if (!template[i].fail)
584                                         goto out;
585                         }
586
587                         sg_init_one(&sg[0], data, template[i].ilen);
588
589                         ablkcipher_request_set_crypt(req, sg, sg,
590                                                      template[i].ilen, iv);
591                         ret = enc ?
592                                 crypto_ablkcipher_encrypt(req) :
593                                 crypto_ablkcipher_decrypt(req);
594
595                         switch (ret) {
596                         case 0:
597                                 break;
598                         case -EINPROGRESS:
599                         case -EBUSY:
600                                 ret = wait_for_completion_interruptible(
601                                         &result.completion);
602                                 if (!ret && !((ret = result.err))) {
603                                         INIT_COMPLETION(result.completion);
604                                         break;
605                                 }
606                                 /* fall through */
607                         default:
608                                 printk("%s () failed err=%d\n", e, -ret);
609                                 goto out;
610                         }
611
612                         q = data;
613                         hexdump(q, template[i].rlen);
614
615                         printk("%s\n",
616                                memcmp(q, template[i].result,
617                                       template[i].rlen) ? "fail" : "pass");
618                 }
619         }
620
621         printk("\ntesting %s %s across pages (chunking)\n", algo, e);
622
623         j = 0;
624         for (i = 0; i < tcount; i++) {
625
626                 if (template[i].iv)
627                         memcpy(iv, template[i].iv, MAX_IVLEN);
628                 else
629                         memset(iv, 0, MAX_IVLEN);
630
631                 if (template[i].np) {
632                         j++;
633                         printk("test %u (%d bit key):\n",
634                         j, template[i].klen * 8);
635
636                         crypto_ablkcipher_clear_flags(tfm, ~0);
637                         if (template[i].wk)
638                                 crypto_ablkcipher_set_flags(
639                                         tfm, CRYPTO_TFM_REQ_WEAK_KEY);
640
641                         ret = crypto_ablkcipher_setkey(tfm, template[i].key,
642                                                        template[i].klen);
643                         if (ret) {
644                                 printk("setkey() failed flags=%x\n",
645                                                 crypto_ablkcipher_get_flags(tfm));
646
647                                 if (!template[i].fail)
648                                         goto out;
649                         }
650
651                         temp = 0;
652                         sg_init_table(sg, template[i].np);
653                         for (k = 0; k < template[i].np; k++) {
654                                 if (WARN_ON(offset_in_page(IDX[k]) +
655                                             template[i].tap[k] > PAGE_SIZE))
656                                         goto out;
657
658                                 q = xbuf[IDX[k] >> PAGE_SHIFT] +
659                                     offset_in_page(IDX[k]);
660
661                                 memcpy(q, template[i].input + temp,
662                                        template[i].tap[k]);
663
664                                 if (offset_in_page(q) + template[i].tap[k] <
665                                     PAGE_SIZE)
666                                         q[template[i].tap[k]] = 0;
667
668                                 sg_set_buf(&sg[k], q, template[i].tap[k]);
669
670                                 temp += template[i].tap[k];
671                         }
672
673                         ablkcipher_request_set_crypt(req, sg, sg,
674                                         template[i].ilen, iv);
675
676                         ret = enc ?
677                                 crypto_ablkcipher_encrypt(req) :
678                                 crypto_ablkcipher_decrypt(req);
679
680                         switch (ret) {
681                         case 0:
682                                 break;
683                         case -EINPROGRESS:
684                         case -EBUSY:
685                                 ret = wait_for_completion_interruptible(
686                                         &result.completion);
687                                 if (!ret && !((ret = result.err))) {
688                                         INIT_COMPLETION(result.completion);
689                                         break;
690                                 }
691                                 /* fall through */
692                         default:
693                                 printk("%s () failed err=%d\n", e, -ret);
694                                 goto out;
695                         }
696
697                         temp = 0;
698                         for (k = 0; k < template[i].np; k++) {
699                                 printk("page %u\n", k);
700                                 q = xbuf[IDX[k] >> PAGE_SHIFT] +
701                                     offset_in_page(IDX[k]);
702                                 hexdump(q, template[i].tap[k]);
703                                 printk("%s\n",
704                                         memcmp(q, template[i].result + temp,
705                                                 template[i].tap[k]) ? "fail" :
706                                         "pass");
707
708                                 q += template[i].tap[k];
709                                 for (n = 0; offset_in_page(q + n) && q[n]; n++)
710                                         ;
711                                 if (n) {
712                                         printk("Result buffer corruption %u "
713                                                "bytes:\n", n);
714                                         hexdump(q, n);
715                                 }
716                                 temp += template[i].tap[k];
717                         }
718                 }
719         }
720 out:
721         crypto_free_ablkcipher(tfm);
722         ablkcipher_request_free(req);
723 }
724
725 static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc,
726                                struct scatterlist *sg, int blen, int sec)
727 {
728         unsigned long start, end;
729         int bcount;
730         int ret;
731
732         for (start = jiffies, end = start + sec * HZ, bcount = 0;
733              time_before(jiffies, end); bcount++) {
734                 if (enc)
735                         ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
736                 else
737                         ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
738
739                 if (ret)
740                         return ret;
741         }
742
743         printk("%d operations in %d seconds (%ld bytes)\n",
744                bcount, sec, (long)bcount * blen);
745         return 0;
746 }
747
748 static int test_cipher_cycles(struct blkcipher_desc *desc, int enc,
749                               struct scatterlist *sg, int blen)
750 {
751         unsigned long cycles = 0;
752         int ret = 0;
753         int i;
754
755         local_bh_disable();
756         local_irq_disable();
757
758         /* Warm-up run. */
759         for (i = 0; i < 4; i++) {
760                 if (enc)
761                         ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
762                 else
763                         ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
764
765                 if (ret)
766                         goto out;
767         }
768
769         /* The real thing. */
770         for (i = 0; i < 8; i++) {
771                 cycles_t start, end;
772
773                 start = get_cycles();
774                 if (enc)
775                         ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
776                 else
777                         ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
778                 end = get_cycles();
779
780                 if (ret)
781                         goto out;
782
783                 cycles += end - start;
784         }
785
786 out:
787         local_irq_enable();
788         local_bh_enable();
789
790         if (ret == 0)
791                 printk("1 operation in %lu cycles (%d bytes)\n",
792                        (cycles + 4) / 8, blen);
793
794         return ret;
795 }
796
797 static u32 block_sizes[] = { 16, 64, 256, 1024, 8192, 0 };
798
799 static void test_cipher_speed(char *algo, int enc, unsigned int sec,
800                               struct cipher_testvec *template,
801                               unsigned int tcount, u8 *keysize)
802 {
803         unsigned int ret, i, j, iv_len;
804         unsigned char *key, iv[128];
805         struct crypto_blkcipher *tfm;
806         struct blkcipher_desc desc;
807         const char *e;
808         u32 *b_size;
809
810         if (enc == ENCRYPT)
811                 e = "encryption";
812         else
813                 e = "decryption";
814
815         printk("\ntesting speed of %s %s\n", algo, e);
816
817         tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
818
819         if (IS_ERR(tfm)) {
820                 printk("failed to load transform for %s: %ld\n", algo,
821                        PTR_ERR(tfm));
822                 return;
823         }
824         desc.tfm = tfm;
825         desc.flags = 0;
826
827         i = 0;
828         do {
829
830                 b_size = block_sizes;
831                 do {
832                         struct scatterlist sg[TVMEMSIZE];
833
834                         if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
835                                 printk("template (%u) too big for "
836                                        "tvmem (%lu)\n", *keysize + *b_size,
837                                        TVMEMSIZE * PAGE_SIZE);
838                                 goto out;
839                         }
840
841                         printk("test %u (%d bit key, %d byte blocks): ", i,
842                                         *keysize * 8, *b_size);
843
844                         memset(tvmem[0], 0xff, PAGE_SIZE);
845
846                         /* set key, plain text and IV */
847                         key = (unsigned char *)tvmem[0];
848                         for (j = 0; j < tcount; j++) {
849                                 if (template[j].klen == *keysize) {
850                                         key = template[j].key;
851                                         break;
852                                 }
853                         }
854
855                         ret = crypto_blkcipher_setkey(tfm, key, *keysize);
856                         if (ret) {
857                                 printk("setkey() failed flags=%x\n",
858                                                 crypto_blkcipher_get_flags(tfm));
859                                 goto out;
860                         }
861
862                         sg_init_table(sg, TVMEMSIZE);
863                         sg_set_buf(sg, tvmem[0] + *keysize,
864                                    PAGE_SIZE - *keysize);
865                         for (j = 1; j < TVMEMSIZE; j++) {
866                                 sg_set_buf(sg + j, tvmem[j], PAGE_SIZE);
867                                 memset (tvmem[j], 0xff, PAGE_SIZE);
868                         }
869
870                         iv_len = crypto_blkcipher_ivsize(tfm);
871                         if (iv_len) {
872                                 memset(&iv, 0xff, iv_len);
873                                 crypto_blkcipher_set_iv(tfm, iv, iv_len);
874                         }
875
876                         if (sec)
877                                 ret = test_cipher_jiffies(&desc, enc, sg,
878                                                           *b_size, sec);
879                         else
880                                 ret = test_cipher_cycles(&desc, enc, sg,
881                                                          *b_size);
882
883                         if (ret) {
884                                 printk("%s() failed flags=%x\n", e, desc.flags);
885                                 break;
886                         }
887                         b_size++;
888                         i++;
889                 } while (*b_size);
890                 keysize++;
891         } while (*keysize);
892
893 out:
894         crypto_free_blkcipher(tfm);
895 }
896
897 static int test_hash_jiffies_digest(struct hash_desc *desc,
898                                     struct scatterlist *sg, int blen,
899                                     char *out, int sec)
900 {
901         unsigned long start, end;
902         int bcount;
903         int ret;
904
905         for (start = jiffies, end = start + sec * HZ, bcount = 0;
906              time_before(jiffies, end); bcount++) {
907                 ret = crypto_hash_digest(desc, sg, blen, out);
908                 if (ret)
909                         return ret;
910         }
911
912         printk("%6u opers/sec, %9lu bytes/sec\n",
913                bcount / sec, ((long)bcount * blen) / sec);
914
915         return 0;
916 }
917
918 static int test_hash_jiffies(struct hash_desc *desc, struct scatterlist *sg,
919                              int blen, int plen, char *out, int sec)
920 {
921         unsigned long start, end;
922         int bcount, pcount;
923         int ret;
924
925         if (plen == blen)
926                 return test_hash_jiffies_digest(desc, sg, blen, out, sec);
927
928         for (start = jiffies, end = start + sec * HZ, bcount = 0;
929              time_before(jiffies, end); bcount++) {
930                 ret = crypto_hash_init(desc);
931                 if (ret)
932                         return ret;
933                 for (pcount = 0; pcount < blen; pcount += plen) {
934                         ret = crypto_hash_update(desc, sg, plen);
935                         if (ret)
936                                 return ret;
937                 }
938                 /* we assume there is enough space in 'out' for the result */
939                 ret = crypto_hash_final(desc, out);
940                 if (ret)
941                         return ret;
942         }
943
944         printk("%6u opers/sec, %9lu bytes/sec\n",
945                bcount / sec, ((long)bcount * blen) / sec);
946
947         return 0;
948 }
949
950 static int test_hash_cycles_digest(struct hash_desc *desc,
951                                    struct scatterlist *sg, int blen, char *out)
952 {
953         unsigned long cycles = 0;
954         int i;
955         int ret;
956
957         local_bh_disable();
958         local_irq_disable();
959
960         /* Warm-up run. */
961         for (i = 0; i < 4; i++) {
962                 ret = crypto_hash_digest(desc, sg, blen, out);
963                 if (ret)
964                         goto out;
965         }
966
967         /* The real thing. */
968         for (i = 0; i < 8; i++) {
969                 cycles_t start, end;
970
971                 start = get_cycles();
972
973                 ret = crypto_hash_digest(desc, sg, blen, out);
974                 if (ret)
975                         goto out;
976
977                 end = get_cycles();
978
979                 cycles += end - start;
980         }
981
982 out:
983         local_irq_enable();
984         local_bh_enable();
985
986         if (ret)
987                 return ret;
988
989         printk("%6lu cycles/operation, %4lu cycles/byte\n",
990                cycles / 8, cycles / (8 * blen));
991
992         return 0;
993 }
994
995 static int test_hash_cycles(struct hash_desc *desc, struct scatterlist *sg,
996                             int blen, int plen, char *out)
997 {
998         unsigned long cycles = 0;
999         int i, pcount;
1000         int ret;
1001
1002         if (plen == blen)
1003                 return test_hash_cycles_digest(desc, sg, blen, out);
1004
1005         local_bh_disable();
1006         local_irq_disable();
1007
1008         /* Warm-up run. */
1009         for (i = 0; i < 4; i++) {
1010                 ret = crypto_hash_init(desc);
1011                 if (ret)
1012                         goto out;
1013                 for (pcount = 0; pcount < blen; pcount += plen) {
1014                         ret = crypto_hash_update(desc, sg, plen);
1015                         if (ret)
1016                                 goto out;
1017                 }
1018                 ret = crypto_hash_final(desc, out);
1019                 if (ret)
1020                         goto out;
1021         }
1022
1023         /* The real thing. */
1024         for (i = 0; i < 8; i++) {
1025                 cycles_t start, end;
1026
1027                 start = get_cycles();
1028
1029                 ret = crypto_hash_init(desc);
1030                 if (ret)
1031                         goto out;
1032                 for (pcount = 0; pcount < blen; pcount += plen) {
1033                         ret = crypto_hash_update(desc, sg, plen);
1034                         if (ret)
1035                                 goto out;
1036                 }
1037                 ret = crypto_hash_final(desc, out);
1038                 if (ret)
1039                         goto out;
1040
1041                 end = get_cycles();
1042
1043                 cycles += end - start;
1044         }
1045
1046 out:
1047         local_irq_enable();
1048         local_bh_enable();
1049
1050         if (ret)
1051                 return ret;
1052
1053         printk("%6lu cycles/operation, %4lu cycles/byte\n",
1054                cycles / 8, cycles / (8 * blen));
1055
1056         return 0;
1057 }
1058
1059 static void test_hash_speed(char *algo, unsigned int sec,
1060                               struct hash_speed *speed)
1061 {
1062         struct scatterlist sg[TVMEMSIZE];
1063         struct crypto_hash *tfm;
1064         struct hash_desc desc;
1065         char output[1024];
1066         int i;
1067         int ret;
1068
1069         printk("\ntesting speed of %s\n", algo);
1070
1071         tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
1072
1073         if (IS_ERR(tfm)) {
1074                 printk("failed to load transform for %s: %ld\n", algo,
1075                        PTR_ERR(tfm));
1076                 return;
1077         }
1078
1079         desc.tfm = tfm;
1080         desc.flags = 0;
1081
1082         if (crypto_hash_digestsize(tfm) > sizeof(output)) {
1083                 printk("digestsize(%u) > outputbuffer(%zu)\n",
1084                        crypto_hash_digestsize(tfm), sizeof(output));
1085                 goto out;
1086         }
1087
1088         sg_init_table(sg, TVMEMSIZE);
1089         for (i = 0; i < TVMEMSIZE; i++) {
1090                 sg_set_buf(sg + i, tvmem[i], PAGE_SIZE);
1091                 memset(tvmem[i], 0xff, PAGE_SIZE);
1092         }
1093
1094         for (i = 0; speed[i].blen != 0; i++) {
1095                 if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
1096                         printk("template (%u) too big for tvmem (%lu)\n",
1097                                speed[i].blen, TVMEMSIZE * PAGE_SIZE);
1098                         goto out;
1099                 }
1100
1101                 printk("test%3u (%5u byte blocks,%5u bytes per update,%4u updates): ",
1102                        i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
1103
1104                 if (sec)
1105                         ret = test_hash_jiffies(&desc, sg, speed[i].blen,
1106                                                 speed[i].plen, output, sec);
1107                 else
1108                         ret = test_hash_cycles(&desc, sg, speed[i].blen,
1109                                                speed[i].plen, output);
1110
1111                 if (ret) {
1112                         printk("hashing failed ret=%d\n", ret);
1113                         break;
1114                 }
1115         }
1116
1117 out:
1118         crypto_free_hash(tfm);
1119 }
1120
1121 static void test_comp(char *algo, struct comp_testvec *ctemplate,
1122                        struct comp_testvec *dtemplate, int ctcount, int dtcount)
1123 {
1124         unsigned int i;
1125         char result[COMP_BUF_SIZE];
1126         struct crypto_comp *tfm;
1127
1128         printk("\ntesting %s compression\n", algo);
1129
1130         tfm = crypto_alloc_comp(algo, 0, CRYPTO_ALG_ASYNC);
1131         if (IS_ERR(tfm)) {
1132                 printk("failed to load transform for %s\n", algo);
1133                 return;
1134         }
1135
1136         for (i = 0; i < ctcount; i++) {
1137                 int ilen, ret, dlen = COMP_BUF_SIZE;
1138
1139                 printk("test %u:\n", i + 1);
1140                 memset(result, 0, sizeof (result));
1141
1142                 ilen = ctemplate[i].inlen;
1143                 ret = crypto_comp_compress(tfm, ctemplate[i].input,
1144                                            ilen, result, &dlen);
1145                 if (ret) {
1146                         printk("fail: ret=%d\n", ret);
1147                         continue;
1148                 }
1149                 hexdump(result, dlen);
1150                 printk("%s (ratio %d:%d)\n",
1151                        memcmp(result, ctemplate[i].output, dlen) ? "fail" : "pass",
1152                        ilen, dlen);
1153         }
1154
1155         printk("\ntesting %s decompression\n", algo);
1156
1157         for (i = 0; i < dtcount; i++) {
1158                 int ilen, ret, dlen = COMP_BUF_SIZE;
1159
1160                 printk("test %u:\n", i + 1);
1161                 memset(result, 0, sizeof (result));
1162
1163                 ilen = dtemplate[i].inlen;
1164                 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
1165                                              ilen, result, &dlen);
1166                 if (ret) {
1167                         printk("fail: ret=%d\n", ret);
1168                         continue;
1169                 }
1170                 hexdump(result, dlen);
1171                 printk("%s (ratio %d:%d)\n",
1172                        memcmp(result, dtemplate[i].output, dlen) ? "fail" : "pass",
1173                        ilen, dlen);
1174         }
1175
1176         crypto_free_comp(tfm);
1177 }
1178
1179 static void test_available(void)
1180 {
1181         char **name = check;
1182
1183         while (*name) {
1184                 printk("alg %s ", *name);
1185                 printk(crypto_has_alg(*name, 0, 0) ?
1186                        "found\n" : "not found\n");
1187                 name++;
1188         }
1189 }
1190
1191 static void do_test(void)
1192 {
1193         switch (mode) {
1194
1195         case 0:
1196                 test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
1197
1198                 test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
1199
1200                 //DES
1201                 test_cipher("ecb(des)", ENCRYPT, des_enc_tv_template,
1202                             DES_ENC_TEST_VECTORS);
1203                 test_cipher("ecb(des)", DECRYPT, des_dec_tv_template,
1204                             DES_DEC_TEST_VECTORS);
1205                 test_cipher("cbc(des)", ENCRYPT, des_cbc_enc_tv_template,
1206                             DES_CBC_ENC_TEST_VECTORS);
1207                 test_cipher("cbc(des)", DECRYPT, des_cbc_dec_tv_template,
1208                             DES_CBC_DEC_TEST_VECTORS);
1209
1210                 //DES3_EDE
1211                 test_cipher("ecb(des3_ede)", ENCRYPT, des3_ede_enc_tv_template,
1212                             DES3_EDE_ENC_TEST_VECTORS);
1213                 test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
1214                             DES3_EDE_DEC_TEST_VECTORS);
1215
1216                 test_cipher("cbc(des3_ede)", ENCRYPT,
1217                             des3_ede_cbc_enc_tv_template,
1218                             DES3_EDE_CBC_ENC_TEST_VECTORS);
1219
1220                 test_cipher("cbc(des3_ede)", DECRYPT,
1221                             des3_ede_cbc_dec_tv_template,
1222                             DES3_EDE_CBC_DEC_TEST_VECTORS);
1223
1224                 test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
1225
1226                 test_hash("sha224", sha224_tv_template, SHA224_TEST_VECTORS);
1227
1228                 test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
1229
1230                 //BLOWFISH
1231                 test_cipher("ecb(blowfish)", ENCRYPT, bf_enc_tv_template,
1232                             BF_ENC_TEST_VECTORS);
1233                 test_cipher("ecb(blowfish)", DECRYPT, bf_dec_tv_template,
1234                             BF_DEC_TEST_VECTORS);
1235                 test_cipher("cbc(blowfish)", ENCRYPT, bf_cbc_enc_tv_template,
1236                             BF_CBC_ENC_TEST_VECTORS);
1237                 test_cipher("cbc(blowfish)", DECRYPT, bf_cbc_dec_tv_template,
1238                             BF_CBC_DEC_TEST_VECTORS);
1239
1240                 //TWOFISH
1241                 test_cipher("ecb(twofish)", ENCRYPT, tf_enc_tv_template,
1242                             TF_ENC_TEST_VECTORS);
1243                 test_cipher("ecb(twofish)", DECRYPT, tf_dec_tv_template,
1244                             TF_DEC_TEST_VECTORS);
1245                 test_cipher("cbc(twofish)", ENCRYPT, tf_cbc_enc_tv_template,
1246                             TF_CBC_ENC_TEST_VECTORS);
1247                 test_cipher("cbc(twofish)", DECRYPT, tf_cbc_dec_tv_template,
1248                             TF_CBC_DEC_TEST_VECTORS);
1249
1250                 //SERPENT
1251                 test_cipher("ecb(serpent)", ENCRYPT, serpent_enc_tv_template,
1252                             SERPENT_ENC_TEST_VECTORS);
1253                 test_cipher("ecb(serpent)", DECRYPT, serpent_dec_tv_template,
1254                             SERPENT_DEC_TEST_VECTORS);
1255
1256                 //TNEPRES
1257                 test_cipher("ecb(tnepres)", ENCRYPT, tnepres_enc_tv_template,
1258                             TNEPRES_ENC_TEST_VECTORS);
1259                 test_cipher("ecb(tnepres)", DECRYPT, tnepres_dec_tv_template,
1260                             TNEPRES_DEC_TEST_VECTORS);
1261
1262                 //AES
1263                 test_cipher("ecb(aes)", ENCRYPT, aes_enc_tv_template,
1264                             AES_ENC_TEST_VECTORS);
1265                 test_cipher("ecb(aes)", DECRYPT, aes_dec_tv_template,
1266                             AES_DEC_TEST_VECTORS);
1267                 test_cipher("cbc(aes)", ENCRYPT, aes_cbc_enc_tv_template,
1268                             AES_CBC_ENC_TEST_VECTORS);
1269                 test_cipher("cbc(aes)", DECRYPT, aes_cbc_dec_tv_template,
1270                             AES_CBC_DEC_TEST_VECTORS);
1271                 test_cipher("lrw(aes)", ENCRYPT, aes_lrw_enc_tv_template,
1272                             AES_LRW_ENC_TEST_VECTORS);
1273                 test_cipher("lrw(aes)", DECRYPT, aes_lrw_dec_tv_template,
1274                             AES_LRW_DEC_TEST_VECTORS);
1275                 test_cipher("xts(aes)", ENCRYPT, aes_xts_enc_tv_template,
1276                             AES_XTS_ENC_TEST_VECTORS);
1277                 test_cipher("xts(aes)", DECRYPT, aes_xts_dec_tv_template,
1278                             AES_XTS_DEC_TEST_VECTORS);
1279                 test_cipher("rfc3686(ctr(aes))", ENCRYPT, aes_ctr_enc_tv_template,
1280                             AES_CTR_ENC_TEST_VECTORS);
1281                 test_cipher("rfc3686(ctr(aes))", DECRYPT, aes_ctr_dec_tv_template,
1282                             AES_CTR_DEC_TEST_VECTORS);
1283                 test_aead("gcm(aes)", ENCRYPT, aes_gcm_enc_tv_template,
1284                           AES_GCM_ENC_TEST_VECTORS);
1285                 test_aead("gcm(aes)", DECRYPT, aes_gcm_dec_tv_template,
1286                           AES_GCM_DEC_TEST_VECTORS);
1287                 test_aead("ccm(aes)", ENCRYPT, aes_ccm_enc_tv_template,
1288                           AES_CCM_ENC_TEST_VECTORS);
1289                 test_aead("ccm(aes)", DECRYPT, aes_ccm_dec_tv_template,
1290                           AES_CCM_DEC_TEST_VECTORS);
1291
1292                 //CAST5
1293                 test_cipher("ecb(cast5)", ENCRYPT, cast5_enc_tv_template,
1294                             CAST5_ENC_TEST_VECTORS);
1295                 test_cipher("ecb(cast5)", DECRYPT, cast5_dec_tv_template,
1296                             CAST5_DEC_TEST_VECTORS);
1297
1298                 //CAST6
1299                 test_cipher("ecb(cast6)", ENCRYPT, cast6_enc_tv_template,
1300                             CAST6_ENC_TEST_VECTORS);
1301                 test_cipher("ecb(cast6)", DECRYPT, cast6_dec_tv_template,
1302                             CAST6_DEC_TEST_VECTORS);
1303
1304                 //ARC4
1305                 test_cipher("ecb(arc4)", ENCRYPT, arc4_enc_tv_template,
1306                             ARC4_ENC_TEST_VECTORS);
1307                 test_cipher("ecb(arc4)", DECRYPT, arc4_dec_tv_template,
1308                             ARC4_DEC_TEST_VECTORS);
1309
1310                 //TEA
1311                 test_cipher("ecb(tea)", ENCRYPT, tea_enc_tv_template,
1312                             TEA_ENC_TEST_VECTORS);
1313                 test_cipher("ecb(tea)", DECRYPT, tea_dec_tv_template,
1314                             TEA_DEC_TEST_VECTORS);
1315
1316
1317                 //XTEA
1318                 test_cipher("ecb(xtea)", ENCRYPT, xtea_enc_tv_template,
1319                             XTEA_ENC_TEST_VECTORS);
1320                 test_cipher("ecb(xtea)", DECRYPT, xtea_dec_tv_template,
1321                             XTEA_DEC_TEST_VECTORS);
1322
1323                 //KHAZAD
1324                 test_cipher("ecb(khazad)", ENCRYPT, khazad_enc_tv_template,
1325                             KHAZAD_ENC_TEST_VECTORS);
1326                 test_cipher("ecb(khazad)", DECRYPT, khazad_dec_tv_template,
1327                             KHAZAD_DEC_TEST_VECTORS);
1328
1329                 //ANUBIS
1330                 test_cipher("ecb(anubis)", ENCRYPT, anubis_enc_tv_template,
1331                             ANUBIS_ENC_TEST_VECTORS);
1332                 test_cipher("ecb(anubis)", DECRYPT, anubis_dec_tv_template,
1333                             ANUBIS_DEC_TEST_VECTORS);
1334                 test_cipher("cbc(anubis)", ENCRYPT, anubis_cbc_enc_tv_template,
1335                             ANUBIS_CBC_ENC_TEST_VECTORS);
1336                 test_cipher("cbc(anubis)", DECRYPT, anubis_cbc_dec_tv_template,
1337                             ANUBIS_CBC_ENC_TEST_VECTORS);
1338
1339                 //XETA
1340                 test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
1341                             XETA_ENC_TEST_VECTORS);
1342                 test_cipher("ecb(xeta)", DECRYPT, xeta_dec_tv_template,
1343                             XETA_DEC_TEST_VECTORS);
1344
1345                 //FCrypt
1346                 test_cipher("pcbc(fcrypt)", ENCRYPT, fcrypt_pcbc_enc_tv_template,
1347                             FCRYPT_ENC_TEST_VECTORS);
1348                 test_cipher("pcbc(fcrypt)", DECRYPT, fcrypt_pcbc_dec_tv_template,
1349                             FCRYPT_DEC_TEST_VECTORS);
1350
1351                 //CAMELLIA
1352                 test_cipher("ecb(camellia)", ENCRYPT,
1353                             camellia_enc_tv_template,
1354                             CAMELLIA_ENC_TEST_VECTORS);
1355                 test_cipher("ecb(camellia)", DECRYPT,
1356                             camellia_dec_tv_template,
1357                             CAMELLIA_DEC_TEST_VECTORS);
1358                 test_cipher("cbc(camellia)", ENCRYPT,
1359                             camellia_cbc_enc_tv_template,
1360                             CAMELLIA_CBC_ENC_TEST_VECTORS);
1361                 test_cipher("cbc(camellia)", DECRYPT,
1362                             camellia_cbc_dec_tv_template,
1363                             CAMELLIA_CBC_DEC_TEST_VECTORS);
1364
1365                 //SEED
1366                 test_cipher("ecb(seed)", ENCRYPT, seed_enc_tv_template,
1367                             SEED_ENC_TEST_VECTORS);
1368                 test_cipher("ecb(seed)", DECRYPT, seed_dec_tv_template,
1369                             SEED_DEC_TEST_VECTORS);
1370
1371                 //CTS
1372                 test_cipher("cts(cbc(aes))", ENCRYPT, cts_mode_enc_tv_template,
1373                             CTS_MODE_ENC_TEST_VECTORS);
1374                 test_cipher("cts(cbc(aes))", DECRYPT, cts_mode_dec_tv_template,
1375                             CTS_MODE_DEC_TEST_VECTORS);
1376
1377                 test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
1378                 test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
1379                 test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
1380                 test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
1381                 test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
1382                 test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
1383                 test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
1384                 test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
1385                 test_comp("deflate", deflate_comp_tv_template,
1386                           deflate_decomp_tv_template, DEFLATE_COMP_TEST_VECTORS,
1387                           DEFLATE_DECOMP_TEST_VECTORS);
1388                 test_comp("lzo", lzo_comp_tv_template, lzo_decomp_tv_template,
1389                           LZO_COMP_TEST_VECTORS, LZO_DECOMP_TEST_VECTORS);
1390                 test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
1391                 test_hash("hmac(md5)", hmac_md5_tv_template,
1392                           HMAC_MD5_TEST_VECTORS);
1393                 test_hash("hmac(sha1)", hmac_sha1_tv_template,
1394                           HMAC_SHA1_TEST_VECTORS);
1395                 test_hash("hmac(sha224)", hmac_sha224_tv_template,
1396                           HMAC_SHA224_TEST_VECTORS);
1397                 test_hash("hmac(sha256)", hmac_sha256_tv_template,
1398                           HMAC_SHA256_TEST_VECTORS);
1399                 test_hash("hmac(sha384)", hmac_sha384_tv_template,
1400                           HMAC_SHA384_TEST_VECTORS);
1401                 test_hash("hmac(sha512)", hmac_sha512_tv_template,
1402                           HMAC_SHA512_TEST_VECTORS);
1403
1404                 test_hash("xcbc(aes)", aes_xcbc128_tv_template,
1405                           XCBC_AES_TEST_VECTORS);
1406
1407                 test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
1408                 break;
1409
1410         case 1:
1411                 test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
1412                 break;
1413
1414         case 2:
1415                 test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
1416                 break;
1417
1418         case 3:
1419                 test_cipher("ecb(des)", ENCRYPT, des_enc_tv_template,
1420                             DES_ENC_TEST_VECTORS);
1421                 test_cipher("ecb(des)", DECRYPT, des_dec_tv_template,
1422                             DES_DEC_TEST_VECTORS);
1423                 test_cipher("cbc(des)", ENCRYPT, des_cbc_enc_tv_template,
1424                             DES_CBC_ENC_TEST_VECTORS);
1425                 test_cipher("cbc(des)", DECRYPT, des_cbc_dec_tv_template,
1426                             DES_CBC_DEC_TEST_VECTORS);
1427                 break;
1428
1429         case 4:
1430                 test_cipher("ecb(des3_ede)", ENCRYPT, des3_ede_enc_tv_template,
1431                             DES3_EDE_ENC_TEST_VECTORS);
1432                 test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
1433                             DES3_EDE_DEC_TEST_VECTORS);
1434
1435                 test_cipher("cbc(des3_ede)", ENCRYPT,
1436                             des3_ede_cbc_enc_tv_template,
1437                             DES3_EDE_CBC_ENC_TEST_VECTORS);
1438
1439                 test_cipher("cbc(des3_ede)", DECRYPT,
1440                             des3_ede_cbc_dec_tv_template,
1441                             DES3_EDE_CBC_DEC_TEST_VECTORS);
1442                 break;
1443
1444         case 5:
1445                 test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
1446                 break;
1447
1448         case 6:
1449                 test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
1450                 break;
1451
1452         case 7:
1453                 test_cipher("ecb(blowfish)", ENCRYPT, bf_enc_tv_template,
1454                             BF_ENC_TEST_VECTORS);
1455                 test_cipher("ecb(blowfish)", DECRYPT, bf_dec_tv_template,
1456                             BF_DEC_TEST_VECTORS);
1457                 test_cipher("cbc(blowfish)", ENCRYPT, bf_cbc_enc_tv_template,
1458                             BF_CBC_ENC_TEST_VECTORS);
1459                 test_cipher("cbc(blowfish)", DECRYPT, bf_cbc_dec_tv_template,
1460                             BF_CBC_DEC_TEST_VECTORS);
1461                 break;
1462
1463         case 8:
1464                 test_cipher("ecb(twofish)", ENCRYPT, tf_enc_tv_template,
1465                             TF_ENC_TEST_VECTORS);
1466                 test_cipher("ecb(twofish)", DECRYPT, tf_dec_tv_template,
1467                             TF_DEC_TEST_VECTORS);
1468                 test_cipher("cbc(twofish)", ENCRYPT, tf_cbc_enc_tv_template,
1469                             TF_CBC_ENC_TEST_VECTORS);
1470                 test_cipher("cbc(twofish)", DECRYPT, tf_cbc_dec_tv_template,
1471                             TF_CBC_DEC_TEST_VECTORS);
1472                 break;
1473
1474         case 9:
1475                 test_cipher("ecb(serpent)", ENCRYPT, serpent_enc_tv_template,
1476                             SERPENT_ENC_TEST_VECTORS);
1477                 test_cipher("ecb(serpent)", DECRYPT, serpent_dec_tv_template,
1478                             SERPENT_DEC_TEST_VECTORS);
1479                 break;
1480
1481         case 10:
1482                 test_cipher("ecb(aes)", ENCRYPT, aes_enc_tv_template,
1483                             AES_ENC_TEST_VECTORS);
1484                 test_cipher("ecb(aes)", DECRYPT, aes_dec_tv_template,
1485                             AES_DEC_TEST_VECTORS);
1486                 test_cipher("cbc(aes)", ENCRYPT, aes_cbc_enc_tv_template,
1487                             AES_CBC_ENC_TEST_VECTORS);
1488                 test_cipher("cbc(aes)", DECRYPT, aes_cbc_dec_tv_template,
1489                             AES_CBC_DEC_TEST_VECTORS);
1490                 test_cipher("lrw(aes)", ENCRYPT, aes_lrw_enc_tv_template,
1491                             AES_LRW_ENC_TEST_VECTORS);
1492                 test_cipher("lrw(aes)", DECRYPT, aes_lrw_dec_tv_template,
1493                             AES_LRW_DEC_TEST_VECTORS);
1494                 test_cipher("xts(aes)", ENCRYPT, aes_xts_enc_tv_template,
1495                             AES_XTS_ENC_TEST_VECTORS);
1496                 test_cipher("xts(aes)", DECRYPT, aes_xts_dec_tv_template,
1497                             AES_XTS_DEC_TEST_VECTORS);
1498                 test_cipher("rfc3686(ctr(aes))", ENCRYPT, aes_ctr_enc_tv_template,
1499                             AES_CTR_ENC_TEST_VECTORS);
1500                 test_cipher("rfc3686(ctr(aes))", DECRYPT, aes_ctr_dec_tv_template,
1501                             AES_CTR_DEC_TEST_VECTORS);
1502                 break;
1503
1504         case 11:
1505                 test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
1506                 break;
1507
1508         case 12:
1509                 test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
1510                 break;
1511
1512         case 13:
1513                 test_comp("deflate", deflate_comp_tv_template,
1514                           deflate_decomp_tv_template, DEFLATE_COMP_TEST_VECTORS,
1515                           DEFLATE_DECOMP_TEST_VECTORS);
1516                 break;
1517
1518         case 14:
1519                 test_cipher("ecb(cast5)", ENCRYPT, cast5_enc_tv_template,
1520                             CAST5_ENC_TEST_VECTORS);
1521                 test_cipher("ecb(cast5)", DECRYPT, cast5_dec_tv_template,
1522                             CAST5_DEC_TEST_VECTORS);
1523                 break;
1524
1525         case 15:
1526                 test_cipher("ecb(cast6)", ENCRYPT, cast6_enc_tv_template,
1527                             CAST6_ENC_TEST_VECTORS);
1528                 test_cipher("ecb(cast6)", DECRYPT, cast6_dec_tv_template,
1529                             CAST6_DEC_TEST_VECTORS);
1530                 break;
1531
1532         case 16:
1533                 test_cipher("ecb(arc4)", ENCRYPT, arc4_enc_tv_template,
1534                             ARC4_ENC_TEST_VECTORS);
1535                 test_cipher("ecb(arc4)", DECRYPT, arc4_dec_tv_template,
1536                             ARC4_DEC_TEST_VECTORS);
1537                 break;
1538
1539         case 17:
1540                 test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
1541                 break;
1542
1543         case 18:
1544                 test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
1545                 break;
1546
1547         case 19:
1548                 test_cipher("ecb(tea)", ENCRYPT, tea_enc_tv_template,
1549                             TEA_ENC_TEST_VECTORS);
1550                 test_cipher("ecb(tea)", DECRYPT, tea_dec_tv_template,
1551                             TEA_DEC_TEST_VECTORS);
1552                 break;
1553
1554         case 20:
1555                 test_cipher("ecb(xtea)", ENCRYPT, xtea_enc_tv_template,
1556                             XTEA_ENC_TEST_VECTORS);
1557                 test_cipher("ecb(xtea)", DECRYPT, xtea_dec_tv_template,
1558                             XTEA_DEC_TEST_VECTORS);
1559                 break;
1560
1561         case 21:
1562                 test_cipher("ecb(khazad)", ENCRYPT, khazad_enc_tv_template,
1563                             KHAZAD_ENC_TEST_VECTORS);
1564                 test_cipher("ecb(khazad)", DECRYPT, khazad_dec_tv_template,
1565                             KHAZAD_DEC_TEST_VECTORS);
1566                 break;
1567
1568         case 22:
1569                 test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
1570                 break;
1571
1572         case 23:
1573                 test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
1574                 break;
1575
1576         case 24:
1577                 test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
1578                 break;
1579
1580         case 25:
1581                 test_cipher("ecb(tnepres)", ENCRYPT, tnepres_enc_tv_template,
1582                             TNEPRES_ENC_TEST_VECTORS);
1583                 test_cipher("ecb(tnepres)", DECRYPT, tnepres_dec_tv_template,
1584                             TNEPRES_DEC_TEST_VECTORS);
1585                 break;
1586
1587         case 26:
1588                 test_cipher("ecb(anubis)", ENCRYPT, anubis_enc_tv_template,
1589                             ANUBIS_ENC_TEST_VECTORS);
1590                 test_cipher("ecb(anubis)", DECRYPT, anubis_dec_tv_template,
1591                             ANUBIS_DEC_TEST_VECTORS);
1592                 test_cipher("cbc(anubis)", ENCRYPT, anubis_cbc_enc_tv_template,
1593                             ANUBIS_CBC_ENC_TEST_VECTORS);
1594                 test_cipher("cbc(anubis)", DECRYPT, anubis_cbc_dec_tv_template,
1595                             ANUBIS_CBC_ENC_TEST_VECTORS);
1596                 break;
1597
1598         case 27:
1599                 test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
1600                 break;
1601
1602         case 28:
1603
1604                 test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
1605                 break;
1606
1607         case 29:
1608                 test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
1609                 break;
1610
1611         case 30:
1612                 test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
1613                             XETA_ENC_TEST_VECTORS);
1614                 test_cipher("ecb(xeta)", DECRYPT, xeta_dec_tv_template,
1615                             XETA_DEC_TEST_VECTORS);
1616                 break;
1617
1618         case 31:
1619                 test_cipher("pcbc(fcrypt)", ENCRYPT, fcrypt_pcbc_enc_tv_template,
1620                             FCRYPT_ENC_TEST_VECTORS);
1621                 test_cipher("pcbc(fcrypt)", DECRYPT, fcrypt_pcbc_dec_tv_template,
1622                             FCRYPT_DEC_TEST_VECTORS);
1623                 break;
1624
1625         case 32:
1626                 test_cipher("ecb(camellia)", ENCRYPT,
1627                             camellia_enc_tv_template,
1628                             CAMELLIA_ENC_TEST_VECTORS);
1629                 test_cipher("ecb(camellia)", DECRYPT,
1630                             camellia_dec_tv_template,
1631                             CAMELLIA_DEC_TEST_VECTORS);
1632                 test_cipher("cbc(camellia)", ENCRYPT,
1633                             camellia_cbc_enc_tv_template,
1634                             CAMELLIA_CBC_ENC_TEST_VECTORS);
1635                 test_cipher("cbc(camellia)", DECRYPT,
1636                             camellia_cbc_dec_tv_template,
1637                             CAMELLIA_CBC_DEC_TEST_VECTORS);
1638                 break;
1639         case 33:
1640                 test_hash("sha224", sha224_tv_template, SHA224_TEST_VECTORS);
1641                 break;
1642
1643         case 34:
1644                 test_cipher("salsa20", ENCRYPT,
1645                             salsa20_stream_enc_tv_template,
1646                             SALSA20_STREAM_ENC_TEST_VECTORS);
1647                 break;
1648
1649         case 35:
1650                 test_aead("gcm(aes)", ENCRYPT, aes_gcm_enc_tv_template,
1651                           AES_GCM_ENC_TEST_VECTORS);
1652                 test_aead("gcm(aes)", DECRYPT, aes_gcm_dec_tv_template,
1653                           AES_GCM_DEC_TEST_VECTORS);
1654                 break;
1655
1656         case 36:
1657                 test_comp("lzo", lzo_comp_tv_template, lzo_decomp_tv_template,
1658                           LZO_COMP_TEST_VECTORS, LZO_DECOMP_TEST_VECTORS);
1659                 break;
1660
1661         case 37:
1662                 test_aead("ccm(aes)", ENCRYPT, aes_ccm_enc_tv_template,
1663                           AES_CCM_ENC_TEST_VECTORS);
1664                 test_aead("ccm(aes)", DECRYPT, aes_ccm_dec_tv_template,
1665                           AES_CCM_DEC_TEST_VECTORS);
1666                 break;
1667
1668         case 38:
1669                 test_cipher("cts(cbc(aes))", ENCRYPT, cts_mode_enc_tv_template,
1670                             CTS_MODE_ENC_TEST_VECTORS);
1671                 test_cipher("cts(cbc(aes))", DECRYPT, cts_mode_dec_tv_template,
1672                             CTS_MODE_DEC_TEST_VECTORS);
1673                 break;
1674
1675         case 39:
1676                 test_hash("rmd128", rmd128_tv_template, RMD128_TEST_VECTORS);
1677                 break;
1678
1679         case 40:
1680                 test_hash("rmd160", rmd160_tv_template, RMD160_TEST_VECTORS);
1681                 break;
1682
1683         case 41:
1684                 test_hash("rmd256", rmd256_tv_template, RMD256_TEST_VECTORS);
1685                 break;
1686
1687         case 42:
1688                 test_hash("rmd320", rmd320_tv_template, RMD320_TEST_VECTORS);
1689                 break;
1690
1691         case 100:
1692                 test_hash("hmac(md5)", hmac_md5_tv_template,
1693                           HMAC_MD5_TEST_VECTORS);
1694                 break;
1695
1696         case 101:
1697                 test_hash("hmac(sha1)", hmac_sha1_tv_template,
1698                           HMAC_SHA1_TEST_VECTORS);
1699                 break;
1700
1701         case 102:
1702                 test_hash("hmac(sha256)", hmac_sha256_tv_template,
1703                           HMAC_SHA256_TEST_VECTORS);
1704                 break;
1705
1706         case 103:
1707                 test_hash("hmac(sha384)", hmac_sha384_tv_template,
1708                           HMAC_SHA384_TEST_VECTORS);
1709                 break;
1710
1711         case 104:
1712                 test_hash("hmac(sha512)", hmac_sha512_tv_template,
1713                           HMAC_SHA512_TEST_VECTORS);
1714                 break;
1715
1716         case 105:
1717                 test_hash("hmac(sha224)", hmac_sha224_tv_template,
1718                           HMAC_SHA224_TEST_VECTORS);
1719                 break;
1720
1721         case 106:
1722                 test_hash("xcbc(aes)", aes_xcbc128_tv_template,
1723                           XCBC_AES_TEST_VECTORS);
1724                 break;
1725
1726         case 107:
1727                 test_hash("hmac(rmd128)", hmac_rmd128_tv_template,
1728                           HMAC_RMD128_TEST_VECTORS);
1729                 break;
1730
1731         case 108:
1732                 test_hash("hmac(rmd160)", hmac_rmd160_tv_template,
1733                           HMAC_RMD160_TEST_VECTORS);
1734                 break;
1735
1736         case 200:
1737                 test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
1738                                 speed_template_16_24_32);
1739                 test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
1740                                 speed_template_16_24_32);
1741                 test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
1742                                 speed_template_16_24_32);
1743                 test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
1744                                 speed_template_16_24_32);
1745                 test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
1746                                 speed_template_32_40_48);
1747                 test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
1748                                 speed_template_32_40_48);
1749                 test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
1750                                 speed_template_32_48_64);
1751                 test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
1752                                 speed_template_32_48_64);
1753                 break;
1754
1755         case 201:
1756                 test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
1757                                 des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS,
1758                                 speed_template_24);
1759                 test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
1760                                 des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS,
1761                                 speed_template_24);
1762                 test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
1763                                 des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS,
1764                                 speed_template_24);
1765                 test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
1766                                 des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS,
1767                                 speed_template_24);
1768                 break;
1769
1770         case 202:
1771                 test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
1772                                 speed_template_16_24_32);
1773                 test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
1774                                 speed_template_16_24_32);
1775                 test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
1776                                 speed_template_16_24_32);
1777                 test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
1778                                 speed_template_16_24_32);
1779                 break;
1780
1781         case 203:
1782                 test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
1783                                   speed_template_8_32);
1784                 test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
1785                                   speed_template_8_32);
1786                 test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
1787                                   speed_template_8_32);
1788                 test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
1789                                   speed_template_8_32);
1790                 break;
1791
1792         case 204:
1793                 test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
1794                                   speed_template_8);
1795                 test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
1796                                   speed_template_8);
1797                 test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
1798                                   speed_template_8);
1799                 test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
1800                                   speed_template_8);
1801                 break;
1802
1803         case 205:
1804                 test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
1805                                 speed_template_16_24_32);
1806                 test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
1807                                 speed_template_16_24_32);
1808                 test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
1809                                 speed_template_16_24_32);
1810                 test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
1811                                 speed_template_16_24_32);
1812                 break;
1813
1814         case 206:
1815                 test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
1816                                   speed_template_16_32);
1817                 break;
1818
1819         case 300:
1820                 /* fall through */
1821
1822         case 301:
1823                 test_hash_speed("md4", sec, generic_hash_speed_template);
1824                 if (mode > 300 && mode < 400) break;
1825
1826         case 302:
1827                 test_hash_speed("md5", sec, generic_hash_speed_template);
1828                 if (mode > 300 && mode < 400) break;
1829
1830         case 303:
1831                 test_hash_speed("sha1", sec, generic_hash_speed_template);
1832                 if (mode > 300 && mode < 400) break;
1833
1834         case 304:
1835                 test_hash_speed("sha256", sec, generic_hash_speed_template);
1836                 if (mode > 300 && mode < 400) break;
1837
1838         case 305:
1839                 test_hash_speed("sha384", sec, generic_hash_speed_template);
1840                 if (mode > 300 && mode < 400) break;
1841
1842         case 306:
1843                 test_hash_speed("sha512", sec, generic_hash_speed_template);
1844                 if (mode > 300 && mode < 400) break;
1845
1846         case 307:
1847                 test_hash_speed("wp256", sec, generic_hash_speed_template);
1848                 if (mode > 300 && mode < 400) break;
1849
1850         case 308:
1851                 test_hash_speed("wp384", sec, generic_hash_speed_template);
1852                 if (mode > 300 && mode < 400) break;
1853
1854         case 309:
1855                 test_hash_speed("wp512", sec, generic_hash_speed_template);
1856                 if (mode > 300 && mode < 400) break;
1857
1858         case 310:
1859                 test_hash_speed("tgr128", sec, generic_hash_speed_template);
1860                 if (mode > 300 && mode < 400) break;
1861
1862         case 311:
1863                 test_hash_speed("tgr160", sec, generic_hash_speed_template);
1864                 if (mode > 300 && mode < 400) break;
1865
1866         case 312:
1867                 test_hash_speed("tgr192", sec, generic_hash_speed_template);
1868                 if (mode > 300 && mode < 400) break;
1869
1870         case 313:
1871                 test_hash_speed("sha224", sec, generic_hash_speed_template);
1872                 if (mode > 300 && mode < 400) break;
1873
1874         case 314:
1875                 test_hash_speed("rmd128", sec, generic_hash_speed_template);
1876                 if (mode > 300 && mode < 400) break;
1877
1878         case 315:
1879                 test_hash_speed("rmd160", sec, generic_hash_speed_template);
1880                 if (mode > 300 && mode < 400) break;
1881
1882         case 316:
1883                 test_hash_speed("rmd256", sec, generic_hash_speed_template);
1884                 if (mode > 300 && mode < 400) break;
1885
1886         case 317:
1887                 test_hash_speed("rmd320", sec, generic_hash_speed_template);
1888                 if (mode > 300 && mode < 400) break;
1889
1890         case 399:
1891                 break;
1892
1893         case 1000:
1894                 test_available();
1895                 break;
1896
1897         default:
1898                 /* useful for debugging */
1899                 printk("not testing anything\n");
1900                 break;
1901         }
1902 }
1903
1904 static int __init tcrypt_mod_init(void)
1905 {
1906         int err = -ENOMEM;
1907         int i;
1908
1909         for (i = 0; i < TVMEMSIZE; i++) {
1910                 tvmem[i] = (void *)__get_free_page(GFP_KERNEL);
1911                 if (!tvmem[i])
1912                         goto err_free_tv;
1913         }
1914
1915         for (i = 0; i < XBUFSIZE; i++) {
1916                 xbuf[i] = (void *)__get_free_page(GFP_KERNEL);
1917                 if (!xbuf[i])
1918                         goto err_free_xbuf;
1919         }
1920
1921         for (i = 0; i < XBUFSIZE; i++) {
1922                 axbuf[i] = (void *)__get_free_page(GFP_KERNEL);
1923                 if (!axbuf[i])
1924                         goto err_free_axbuf;
1925         }
1926
1927         do_test();
1928
1929         /* We intentionaly return -EAGAIN to prevent keeping
1930          * the module. It does all its work from init()
1931          * and doesn't offer any runtime functionality 
1932          * => we don't need it in the memory, do we?
1933          *                                        -- mludvig
1934          */
1935         err = -EAGAIN;
1936
1937 err_free_axbuf:
1938         for (i = 0; i < XBUFSIZE && axbuf[i]; i++)
1939                 free_page((unsigned long)axbuf[i]);
1940 err_free_xbuf:
1941         for (i = 0; i < XBUFSIZE && xbuf[i]; i++)
1942                 free_page((unsigned long)xbuf[i]);
1943 err_free_tv:
1944         for (i = 0; i < TVMEMSIZE && tvmem[i]; i++)
1945                 free_page((unsigned long)tvmem[i]);
1946
1947         return err;
1948 }
1949
1950 /*
1951  * If an init function is provided, an exit function must also be provided
1952  * to allow module unload.
1953  */
1954 static void __exit tcrypt_mod_fini(void) { }
1955
1956 module_init(tcrypt_mod_init);
1957 module_exit(tcrypt_mod_fini);
1958
1959 module_param(mode, int, 0);
1960 module_param(sec, uint, 0);
1961 MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
1962                       "(defaults to zero which uses CPU cycles instead)");
1963
1964 MODULE_LICENSE("GPL");
1965 MODULE_DESCRIPTION("Quick & dirty crypto testing module");
1966 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");