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