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