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