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