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