[CRYPTO]: Use template keys for speed tests if possible
[pandora-kernel.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  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 2 of the License, or (at your option)
13  * any later version.
14  *
15  * 2004-08-09 Added cipher speed tests (Reyk Floeter <reyk@vantronix.net>)
16  * 2003-09-14 Rewritten by Kartikey Mahendra Bhatt
17  *
18  */
19
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/mm.h>
23 #include <linux/slab.h>
24 #include <asm/scatterlist.h>
25 #include <linux/string.h>
26 #include <linux/crypto.h>
27 #include <linux/highmem.h>
28 #include <linux/moduleparam.h>
29 #include <linux/jiffies.h>
30 #include "tcrypt.h"
31
32 /*
33  * Need to kmalloc() memory for testing kmap().
34  */
35 #define TVMEMSIZE       16384
36 #define XBUFSIZE        32768
37
38 /*
39  * Indexes into the xbuf to simulate cross-page access.
40  */
41 #define IDX1            37
42 #define IDX2            32400
43 #define IDX3            1
44 #define IDX4            8193
45 #define IDX5            22222
46 #define IDX6            17101
47 #define IDX7            27333
48 #define IDX8            3000
49
50 /*
51 * Used by test_cipher()
52 */
53 #define ENCRYPT 1
54 #define DECRYPT 0
55 #define MODE_ECB 1
56 #define MODE_CBC 0
57
58 static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
59
60 /*
61  * Used by test_cipher_speed()
62  */
63 static unsigned int sec = 10;
64
65 static int mode;
66 static char *xbuf;
67 static char *tvmem;
68
69 static char *check[] = {
70         "des", "md5", "des3_ede", "rot13", "sha1", "sha256", "blowfish",
71         "twofish", "serpent", "sha384", "sha512", "md4", "aes", "cast6",
72         "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
73         "khazad", "wp512", "wp384", "wp256", "tnepres", NULL
74 };
75
76 static void hexdump(unsigned char *buf, unsigned int len)
77 {
78         while (len--)
79                 printk("%02x", *buf++);
80
81         printk("\n");
82 }
83
84 static void test_hash(char *algo, struct hash_testvec *template,
85                       unsigned int tcount)
86 {
87         char *p;
88         unsigned int i, j, k, temp;
89         struct scatterlist sg[8];
90         char result[64];
91         struct crypto_tfm *tfm;
92         struct hash_testvec *hash_tv;
93         unsigned int tsize;
94
95         printk("\ntesting %s\n", algo);
96
97         tsize = sizeof(struct hash_testvec);
98         tsize *= tcount;
99
100         if (tsize > TVMEMSIZE) {
101                 printk("template (%u) too big for tvmem (%u)\n", tsize, TVMEMSIZE);
102                 return;
103         }
104
105         memcpy(tvmem, template, tsize);
106         hash_tv = (void *)tvmem;
107         tfm = crypto_alloc_tfm(algo, 0);
108         if (tfm == NULL) {
109                 printk("failed to load transform for %s\n", algo);
110                 return;
111         }
112
113         for (i = 0; i < tcount; i++) {
114                 printk("test %u:\n", i + 1);
115                 memset(result, 0, 64);
116
117                 p = hash_tv[i].plaintext;
118                 sg[0].page = virt_to_page(p);
119                 sg[0].offset = offset_in_page(p);
120                 sg[0].length = hash_tv[i].psize;
121
122                 crypto_digest_init(tfm);
123                 if (tfm->crt_u.digest.dit_setkey) {
124                         crypto_digest_setkey(tfm, hash_tv[i].key,
125                                              hash_tv[i].ksize);
126                 }
127                 crypto_digest_update(tfm, sg, 1);
128                 crypto_digest_final(tfm, result);
129
130                 hexdump(result, crypto_tfm_alg_digestsize(tfm));
131                 printk("%s\n",
132                        memcmp(result, hash_tv[i].digest,
133                               crypto_tfm_alg_digestsize(tfm)) ?
134                        "fail" : "pass");
135         }
136
137         printk("testing %s across pages\n", algo);
138
139         /* setup the dummy buffer first */
140         memset(xbuf, 0, XBUFSIZE);
141
142         j = 0;
143         for (i = 0; i < tcount; i++) {
144                 if (hash_tv[i].np) {
145                         j++;
146                         printk("test %u:\n", j);
147                         memset(result, 0, 64);
148
149                         temp = 0;
150                         for (k = 0; k < hash_tv[i].np; k++) {
151                                 memcpy(&xbuf[IDX[k]],
152                                        hash_tv[i].plaintext + temp,
153                                        hash_tv[i].tap[k]);
154                                 temp += hash_tv[i].tap[k];
155                                 p = &xbuf[IDX[k]];
156                                 sg[k].page = virt_to_page(p);
157                                 sg[k].offset = offset_in_page(p);
158                                 sg[k].length = hash_tv[i].tap[k];
159                         }
160
161                         crypto_digest_digest(tfm, sg, hash_tv[i].np, result);
162
163                         hexdump(result, crypto_tfm_alg_digestsize(tfm));
164                         printk("%s\n",
165                                memcmp(result, hash_tv[i].digest,
166                                       crypto_tfm_alg_digestsize(tfm)) ?
167                                "fail" : "pass");
168                 }
169         }
170
171         crypto_free_tfm(tfm);
172 }
173
174
175 #ifdef CONFIG_CRYPTO_HMAC
176
177 static void test_hmac(char *algo, struct hmac_testvec *template,
178                       unsigned int tcount)
179 {
180         char *p;
181         unsigned int i, j, k, temp;
182         struct scatterlist sg[8];
183         char result[64];
184         struct crypto_tfm *tfm;
185         struct hmac_testvec *hmac_tv;
186         unsigned int tsize, klen;
187
188         tfm = crypto_alloc_tfm(algo, 0);
189         if (tfm == NULL) {
190                 printk("failed to load transform for %s\n", algo);
191                 return;
192         }
193
194         printk("\ntesting hmac_%s\n", algo);
195
196         tsize = sizeof(struct hmac_testvec);
197         tsize *= tcount;
198         if (tsize > TVMEMSIZE) {
199                 printk("template (%u) too big for tvmem (%u)\n", tsize,
200                        TVMEMSIZE);
201                 goto out;
202         }
203
204         memcpy(tvmem, template, tsize);
205         hmac_tv = (void *)tvmem;
206
207         for (i = 0; i < tcount; i++) {
208                 printk("test %u:\n", i + 1);
209                 memset(result, 0, sizeof (result));
210
211                 p = hmac_tv[i].plaintext;
212                 klen = hmac_tv[i].ksize;
213                 sg[0].page = virt_to_page(p);
214                 sg[0].offset = offset_in_page(p);
215                 sg[0].length = hmac_tv[i].psize;
216
217                 crypto_hmac(tfm, hmac_tv[i].key, &klen, sg, 1, result);
218
219                 hexdump(result, crypto_tfm_alg_digestsize(tfm));
220                 printk("%s\n",
221                        memcmp(result, hmac_tv[i].digest,
222                               crypto_tfm_alg_digestsize(tfm)) ? "fail" :
223                        "pass");
224         }
225
226         printk("\ntesting hmac_%s across pages\n", algo);
227
228         memset(xbuf, 0, XBUFSIZE);
229
230         j = 0;
231         for (i = 0; i < tcount; i++) {
232                 if (hmac_tv[i].np) {
233                         j++;
234                         printk("test %u:\n",j);
235                         memset(result, 0, 64);
236
237                         temp = 0;
238                         klen = hmac_tv[i].ksize;
239                         for (k = 0; k < hmac_tv[i].np; k++) {
240                                 memcpy(&xbuf[IDX[k]],
241                                        hmac_tv[i].plaintext + temp,
242                                        hmac_tv[i].tap[k]);
243                                 temp += hmac_tv[i].tap[k];
244                                 p = &xbuf[IDX[k]];
245                                 sg[k].page = virt_to_page(p);
246                                 sg[k].offset = offset_in_page(p);
247                                 sg[k].length = hmac_tv[i].tap[k];
248                         }
249
250                         crypto_hmac(tfm, hmac_tv[i].key, &klen, sg,
251                                     hmac_tv[i].np, result);
252                         hexdump(result, crypto_tfm_alg_digestsize(tfm));
253
254                         printk("%s\n",
255                                memcmp(result, hmac_tv[i].digest,
256                                       crypto_tfm_alg_digestsize(tfm)) ?
257                                "fail" : "pass");
258                 }
259         }
260 out:
261         crypto_free_tfm(tfm);
262 }
263
264 #endif  /* CONFIG_CRYPTO_HMAC */
265
266 static void test_cipher(char *algo, int mode, int enc,
267                         struct cipher_testvec *template, unsigned int tcount)
268 {
269         unsigned int ret, i, j, k, temp;
270         unsigned int tsize;
271         char *p, *q;
272         struct crypto_tfm *tfm;
273         char *key;
274         struct cipher_testvec *cipher_tv;
275         struct scatterlist sg[8];
276         const char *e, *m;
277
278         if (enc == ENCRYPT)
279                 e = "encryption";
280         else
281                 e = "decryption";
282         if (mode == MODE_ECB)
283                 m = "ECB";
284         else
285                 m = "CBC";
286
287         printk("\ntesting %s %s %s\n", algo, m, e);
288
289         tsize = sizeof (struct cipher_testvec);
290         tsize *= tcount;
291
292         if (tsize > TVMEMSIZE) {
293                 printk("template (%u) too big for tvmem (%u)\n", tsize,
294                        TVMEMSIZE);
295                 return;
296         }
297
298         memcpy(tvmem, template, tsize);
299         cipher_tv = (void *)tvmem;
300
301         if (mode)
302                 tfm = crypto_alloc_tfm(algo, 0);
303         else
304                 tfm = crypto_alloc_tfm(algo, CRYPTO_TFM_MODE_CBC);
305
306         if (tfm == NULL) {
307                 printk("failed to load transform for %s %s\n", algo, m);
308                 return;
309         }
310
311         j = 0;
312         for (i = 0; i < tcount; i++) {
313                 if (!(cipher_tv[i].np)) {
314                         j++;
315                         printk("test %u (%d bit key):\n",
316                         j, cipher_tv[i].klen * 8);
317
318                         tfm->crt_flags = 0;
319                         if (cipher_tv[i].wk)
320                                 tfm->crt_flags |= CRYPTO_TFM_REQ_WEAK_KEY;
321                         key = cipher_tv[i].key;
322
323                         ret = crypto_cipher_setkey(tfm, key, cipher_tv[i].klen);
324                         if (ret) {
325                                 printk("setkey() failed flags=%x\n", tfm->crt_flags);
326
327                                 if (!cipher_tv[i].fail)
328                                         goto out;
329                         }
330
331                         p = cipher_tv[i].input;
332                         sg[0].page = virt_to_page(p);
333                         sg[0].offset = offset_in_page(p);
334                         sg[0].length = cipher_tv[i].ilen;
335
336                         if (!mode) {
337                                 crypto_cipher_set_iv(tfm, cipher_tv[i].iv,
338                                         crypto_tfm_alg_ivsize(tfm));
339                         }
340
341                         if (enc)
342                                 ret = crypto_cipher_encrypt(tfm, sg, sg, cipher_tv[i].ilen);
343                         else
344                                 ret = crypto_cipher_decrypt(tfm, sg, sg, cipher_tv[i].ilen);
345
346
347                         if (ret) {
348                                 printk("%s () failed flags=%x\n", e, tfm->crt_flags);
349                                 goto out;
350                         }
351
352                         q = kmap(sg[0].page) + sg[0].offset;
353                         hexdump(q, cipher_tv[i].rlen);
354
355                         printk("%s\n",
356                                memcmp(q, cipher_tv[i].result,
357                                       cipher_tv[i].rlen) ? "fail" : "pass");
358                 }
359         }
360
361         printk("\ntesting %s %s %s across pages (chunking)\n", algo, m, e);
362         memset(xbuf, 0, XBUFSIZE);
363
364         j = 0;
365         for (i = 0; i < tcount; i++) {
366                 if (cipher_tv[i].np) {
367                         j++;
368                         printk("test %u (%d bit key):\n",
369                         j, cipher_tv[i].klen * 8);
370
371                         tfm->crt_flags = 0;
372                         if (cipher_tv[i].wk)
373                                 tfm->crt_flags |= CRYPTO_TFM_REQ_WEAK_KEY;
374                         key = cipher_tv[i].key;
375
376                         ret = crypto_cipher_setkey(tfm, key, cipher_tv[i].klen);
377                         if (ret) {
378                                 printk("setkey() failed flags=%x\n", tfm->crt_flags);
379
380                                 if (!cipher_tv[i].fail)
381                                         goto out;
382                         }
383
384                         temp = 0;
385                         for (k = 0; k < cipher_tv[i].np; k++) {
386                                 memcpy(&xbuf[IDX[k]],
387                                        cipher_tv[i].input + temp,
388                                        cipher_tv[i].tap[k]);
389                                 temp += cipher_tv[i].tap[k];
390                                 p = &xbuf[IDX[k]];
391                                 sg[k].page = virt_to_page(p);
392                                 sg[k].offset = offset_in_page(p);
393                                 sg[k].length = cipher_tv[i].tap[k];
394                         }
395
396                         if (!mode) {
397                                 crypto_cipher_set_iv(tfm, cipher_tv[i].iv,
398                                                 crypto_tfm_alg_ivsize(tfm));
399                         }
400
401                         if (enc)
402                                 ret = crypto_cipher_encrypt(tfm, sg, sg, cipher_tv[i].ilen);
403                         else
404                                 ret = crypto_cipher_decrypt(tfm, sg, sg, cipher_tv[i].ilen);
405
406                         if (ret) {
407                                 printk("%s () failed flags=%x\n", e, tfm->crt_flags);
408                                 goto out;
409                         }
410
411                         temp = 0;
412                         for (k = 0; k < cipher_tv[i].np; k++) {
413                                 printk("page %u\n", k);
414                                 q = kmap(sg[k].page) + sg[k].offset;
415                                 hexdump(q, cipher_tv[i].tap[k]);
416                                 printk("%s\n",
417                                         memcmp(q, cipher_tv[i].result + temp,
418                                                 cipher_tv[i].tap[k]) ? "fail" :
419                                         "pass");
420                                 temp += cipher_tv[i].tap[k];
421                         }
422                 }
423         }
424
425 out:
426         crypto_free_tfm(tfm);
427 }
428
429 static void test_cipher_speed(char *algo, int mode, int enc, unsigned int sec,
430                               struct cipher_testvec *template,
431                               unsigned int tcount, struct cipher_speed *speed)
432 {
433         unsigned int ret, i, j, iv_len;
434         unsigned char *key, *p, iv[128];
435         struct crypto_tfm *tfm;
436         struct scatterlist sg[8];
437         unsigned long start, bcount;
438         const char *e, *m;
439
440         if (enc == ENCRYPT)
441                 e = "encryption";
442         else
443                 e = "decryption";
444         if (mode == MODE_ECB)
445                 m = "ECB";
446         else
447                 m = "CBC";
448
449         printk("\ntesting speed of %s %s %s\n", algo, m, e);
450
451         if (mode)
452                 tfm = crypto_alloc_tfm(algo, 0);
453         else
454                 tfm = crypto_alloc_tfm(algo, CRYPTO_TFM_MODE_CBC);
455
456         if (tfm == NULL) {
457                 printk("failed to load transform for %s %s\n", algo, m);
458                 return;
459         }
460
461         for (i = 0; speed[i].klen != 0; i++) {
462                 if ((speed[i].blen + speed[i].klen) > TVMEMSIZE) {
463                         printk("template (%u) too big for tvmem (%u)\n",
464                                speed[i].blen + speed[i].klen, TVMEMSIZE);
465                         goto out;
466                 }
467
468                 printk("test %u (%d bit key, %d byte blocks): ", i,
469                        speed[i].klen * 8, speed[i].blen);
470
471                 memset(tvmem, 0xff, speed[i].klen + speed[i].blen);
472
473                 /* set key, plain text and IV */
474                 key = (unsigned char *)tvmem;
475                 for (j = 0; j < tcount; j++) {
476                         if (template[j].klen == speed[i].klen) {
477                                 key = template[j].key;
478                                 break;
479                         }
480                 }
481                 p = (unsigned char *)tvmem + speed[i].klen;
482
483                 ret = crypto_cipher_setkey(tfm, key, speed[i].klen);
484                 if (ret) {
485                         printk("setkey() failed flags=%x\n", tfm->crt_flags);
486                         goto out;
487                 }
488
489                 if (!mode) {
490                         iv_len = crypto_tfm_alg_ivsize(tfm);
491                         memset(&iv, 0xff, iv_len);
492                         crypto_cipher_set_iv(tfm, iv, iv_len);
493                 }
494
495                 for (start = jiffies, bcount = 0;
496                     ((jiffies - start) / HZ) < sec; bcount++) {
497                         sg[0].page = virt_to_page(p);
498                         sg[0].offset = offset_in_page(p);
499                         sg[0].length = speed[i].blen;
500
501                         if (enc)
502                                 ret = crypto_cipher_encrypt(tfm, sg, sg, speed[i].blen);
503                         else
504                                 ret = crypto_cipher_decrypt(tfm, sg, sg, speed[i].blen);
505
506                         if (ret) {
507                                 printk("%s () failed flags=%x\n", e, tfm->crt_flags);
508                                 goto out;
509                         }
510                 }
511
512                 printk("%lu operations in %u seconds (%lu bytes)\n",
513                        bcount, sec, bcount * speed[i].blen);
514         }
515
516 out:
517         crypto_free_tfm(tfm);
518 }
519
520 static void test_deflate(void)
521 {
522         unsigned int i;
523         char result[COMP_BUF_SIZE];
524         struct crypto_tfm *tfm;
525         struct comp_testvec *tv;
526         unsigned int tsize;
527
528         printk("\ntesting deflate compression\n");
529
530         tsize = sizeof (deflate_comp_tv_template);
531         if (tsize > TVMEMSIZE) {
532                 printk("template (%u) too big for tvmem (%u)\n", tsize,
533                        TVMEMSIZE);
534                 return;
535         }
536
537         memcpy(tvmem, deflate_comp_tv_template, tsize);
538         tv = (void *)tvmem;
539
540         tfm = crypto_alloc_tfm("deflate", 0);
541         if (tfm == NULL) {
542                 printk("failed to load transform for deflate\n");
543                 return;
544         }
545
546         for (i = 0; i < DEFLATE_COMP_TEST_VECTORS; i++) {
547                 int ilen, ret, dlen = COMP_BUF_SIZE;
548
549                 printk("test %u:\n", i + 1);
550                 memset(result, 0, sizeof (result));
551
552                 ilen = tv[i].inlen;
553                 ret = crypto_comp_compress(tfm, tv[i].input,
554                                            ilen, result, &dlen);
555                 if (ret) {
556                         printk("fail: ret=%d\n", ret);
557                         continue;
558                 }
559                 hexdump(result, dlen);
560                 printk("%s (ratio %d:%d)\n",
561                        memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
562                        ilen, dlen);
563         }
564
565         printk("\ntesting deflate decompression\n");
566
567         tsize = sizeof (deflate_decomp_tv_template);
568         if (tsize > TVMEMSIZE) {
569                 printk("template (%u) too big for tvmem (%u)\n", tsize,
570                        TVMEMSIZE);
571                 goto out;
572         }
573
574         memcpy(tvmem, deflate_decomp_tv_template, tsize);
575         tv = (void *)tvmem;
576
577         for (i = 0; i < DEFLATE_DECOMP_TEST_VECTORS; i++) {
578                 int ilen, ret, dlen = COMP_BUF_SIZE;
579
580                 printk("test %u:\n", i + 1);
581                 memset(result, 0, sizeof (result));
582
583                 ilen = tv[i].inlen;
584                 ret = crypto_comp_decompress(tfm, tv[i].input,
585                                              ilen, result, &dlen);
586                 if (ret) {
587                         printk("fail: ret=%d\n", ret);
588                         continue;
589                 }
590                 hexdump(result, dlen);
591                 printk("%s (ratio %d:%d)\n",
592                        memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
593                        ilen, dlen);
594         }
595 out:
596         crypto_free_tfm(tfm);
597 }
598
599 static void test_crc32c(void)
600 {
601 #define NUMVEC 6
602 #define VECSIZE 40
603
604         int i, j, pass;
605         u32 crc;
606         u8 b, test_vec[NUMVEC][VECSIZE];
607         static u32 vec_results[NUMVEC] = {
608                 0x0e2c157f, 0xe980ebf6, 0xde74bded,
609                 0xd579c862, 0xba979ad0, 0x2b29d913
610         };
611         static u32 tot_vec_results = 0x24c5d375;
612
613         struct scatterlist sg[NUMVEC];
614         struct crypto_tfm *tfm;
615         char *fmtdata = "testing crc32c initialized to %08x: %s\n";
616 #define SEEDTESTVAL 0xedcba987
617         u32 seed;
618
619         printk("\ntesting crc32c\n");
620
621         tfm = crypto_alloc_tfm("crc32c", 0);
622         if (tfm == NULL) {
623                 printk("failed to load transform for crc32c\n");
624                 return;
625         }
626
627         crypto_digest_init(tfm);
628         crypto_digest_final(tfm, (u8*)&crc);
629         printk(fmtdata, crc, (crc == 0) ? "pass" : "ERROR");
630
631         /*
632          * stuff test_vec with known values, simple incrementing
633          * byte values.
634          */
635         b = 0;
636         for (i = 0; i < NUMVEC; i++) {
637                 for (j = 0; j < VECSIZE; j++)
638                         test_vec[i][j] = ++b;
639                 sg[i].page = virt_to_page(test_vec[i]);
640                 sg[i].offset = offset_in_page(test_vec[i]);
641                 sg[i].length = VECSIZE;
642         }
643
644         seed = SEEDTESTVAL;
645         (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32));
646         crypto_digest_final(tfm, (u8*)&crc);
647         printk("testing crc32c setkey returns %08x : %s\n", crc, (crc == (SEEDTESTVAL ^ ~(u32)0)) ?
648                "pass" : "ERROR");
649
650         printk("testing crc32c using update/final:\n");
651
652         pass = 1;                   /* assume all is well */
653
654         for (i = 0; i < NUMVEC; i++) {
655                 seed = ~(u32)0;
656                 (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32));
657                 crypto_digest_update(tfm, &sg[i], 1);
658                 crypto_digest_final(tfm, (u8*)&crc);
659                 if (crc == vec_results[i]) {
660                         printk(" %08x:OK", crc);
661                 } else {
662                         printk(" %08x:BAD, wanted %08x\n", crc, vec_results[i]);
663                         pass = 0;
664                 }
665         }
666
667         printk("\ntesting crc32c using incremental accumulator:\n");
668         crc = 0;
669         for (i = 0; i < NUMVEC; i++) {
670                 seed = (crc ^ ~(u32)0);
671                 (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32));
672                 crypto_digest_update(tfm, &sg[i], 1);
673                 crypto_digest_final(tfm, (u8*)&crc);
674         }
675         if (crc == tot_vec_results) {
676                 printk(" %08x:OK", crc);
677         } else {
678                 printk(" %08x:BAD, wanted %08x\n", crc, tot_vec_results);
679                 pass = 0;
680         }
681
682         printk("\ntesting crc32c using digest:\n");
683         seed = ~(u32)0;
684         (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32));
685         crypto_digest_digest(tfm, sg, NUMVEC, (u8*)&crc);
686         if (crc == tot_vec_results) {
687                 printk(" %08x:OK", crc);
688         } else {
689                 printk(" %08x:BAD, wanted %08x\n", crc, tot_vec_results);
690                 pass = 0;
691         }
692
693         printk("\n%s\n", pass ? "pass" : "ERROR");
694
695         crypto_free_tfm(tfm);
696         printk("crc32c test complete\n");
697 }
698
699 static void test_available(void)
700 {
701         char **name = check;
702
703         while (*name) {
704                 printk("alg %s ", *name);
705                 printk((crypto_alg_available(*name, 0)) ?
706                         "found\n" : "not found\n");
707                 name++;
708         }
709 }
710
711 static void do_test(void)
712 {
713         switch (mode) {
714
715         case 0:
716                 test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
717
718                 test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
719
720                 //DES
721                 test_cipher ("des", MODE_ECB, ENCRYPT, des_enc_tv_template, DES_ENC_TEST_VECTORS);
722                 test_cipher ("des", MODE_ECB, DECRYPT, des_dec_tv_template, DES_DEC_TEST_VECTORS);
723                 test_cipher ("des", MODE_CBC, ENCRYPT, des_cbc_enc_tv_template, DES_CBC_ENC_TEST_VECTORS);
724                 test_cipher ("des", MODE_CBC, DECRYPT, des_cbc_dec_tv_template, DES_CBC_DEC_TEST_VECTORS);
725
726                 //DES3_EDE
727                 test_cipher ("des3_ede", MODE_ECB, ENCRYPT, des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS);
728                 test_cipher ("des3_ede", MODE_ECB, DECRYPT, des3_ede_dec_tv_template, DES3_EDE_DEC_TEST_VECTORS);
729
730                 test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
731
732                 test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
733
734                 //BLOWFISH
735                 test_cipher ("blowfish", MODE_ECB, ENCRYPT, bf_enc_tv_template, BF_ENC_TEST_VECTORS);
736                 test_cipher ("blowfish", MODE_ECB, DECRYPT, bf_dec_tv_template, BF_DEC_TEST_VECTORS);
737                 test_cipher ("blowfish", MODE_CBC, ENCRYPT, bf_cbc_enc_tv_template, BF_CBC_ENC_TEST_VECTORS);
738                 test_cipher ("blowfish", MODE_CBC, DECRYPT, bf_cbc_dec_tv_template, BF_CBC_DEC_TEST_VECTORS);
739
740                 //TWOFISH
741                 test_cipher ("twofish", MODE_ECB, ENCRYPT, tf_enc_tv_template, TF_ENC_TEST_VECTORS);
742                 test_cipher ("twofish", MODE_ECB, DECRYPT, tf_dec_tv_template, TF_DEC_TEST_VECTORS);
743                 test_cipher ("twofish", MODE_CBC, ENCRYPT, tf_cbc_enc_tv_template, TF_CBC_ENC_TEST_VECTORS);
744                 test_cipher ("twofish", MODE_CBC, DECRYPT, tf_cbc_dec_tv_template, TF_CBC_DEC_TEST_VECTORS);
745
746                 //SERPENT
747                 test_cipher ("serpent", MODE_ECB, ENCRYPT, serpent_enc_tv_template, SERPENT_ENC_TEST_VECTORS);
748                 test_cipher ("serpent", MODE_ECB, DECRYPT, serpent_dec_tv_template, SERPENT_DEC_TEST_VECTORS);
749
750                 //TNEPRES
751                 test_cipher ("tnepres", MODE_ECB, ENCRYPT, tnepres_enc_tv_template, TNEPRES_ENC_TEST_VECTORS);
752                 test_cipher ("tnepres", MODE_ECB, DECRYPT, tnepres_dec_tv_template, TNEPRES_DEC_TEST_VECTORS);
753
754                 //AES
755                 test_cipher ("aes", MODE_ECB, ENCRYPT, aes_enc_tv_template, AES_ENC_TEST_VECTORS);
756                 test_cipher ("aes", MODE_ECB, DECRYPT, aes_dec_tv_template, AES_DEC_TEST_VECTORS);
757
758                 //CAST5
759                 test_cipher ("cast5", MODE_ECB, ENCRYPT, cast5_enc_tv_template, CAST5_ENC_TEST_VECTORS);
760                 test_cipher ("cast5", MODE_ECB, DECRYPT, cast5_dec_tv_template, CAST5_DEC_TEST_VECTORS);
761
762                 //CAST6
763                 test_cipher ("cast6", MODE_ECB, ENCRYPT, cast6_enc_tv_template, CAST6_ENC_TEST_VECTORS);
764                 test_cipher ("cast6", MODE_ECB, DECRYPT, cast6_dec_tv_template, CAST6_DEC_TEST_VECTORS);
765
766                 //ARC4
767                 test_cipher ("arc4", MODE_ECB, ENCRYPT, arc4_enc_tv_template, ARC4_ENC_TEST_VECTORS);
768                 test_cipher ("arc4", MODE_ECB, DECRYPT, arc4_dec_tv_template, ARC4_DEC_TEST_VECTORS);
769
770                 //TEA
771                 test_cipher ("tea", MODE_ECB, ENCRYPT, tea_enc_tv_template, TEA_ENC_TEST_VECTORS);
772                 test_cipher ("tea", MODE_ECB, DECRYPT, tea_dec_tv_template, TEA_DEC_TEST_VECTORS);
773
774
775                 //XTEA
776                 test_cipher ("xtea", MODE_ECB, ENCRYPT, xtea_enc_tv_template, XTEA_ENC_TEST_VECTORS);
777                 test_cipher ("xtea", MODE_ECB, DECRYPT, xtea_dec_tv_template, XTEA_DEC_TEST_VECTORS);
778
779                 //KHAZAD
780                 test_cipher ("khazad", MODE_ECB, ENCRYPT, khazad_enc_tv_template, KHAZAD_ENC_TEST_VECTORS);
781                 test_cipher ("khazad", MODE_ECB, DECRYPT, khazad_dec_tv_template, KHAZAD_DEC_TEST_VECTORS);
782
783                 //ANUBIS
784                 test_cipher ("anubis", MODE_ECB, ENCRYPT, anubis_enc_tv_template, ANUBIS_ENC_TEST_VECTORS);
785                 test_cipher ("anubis", MODE_ECB, DECRYPT, anubis_dec_tv_template, ANUBIS_DEC_TEST_VECTORS);
786                 test_cipher ("anubis", MODE_CBC, ENCRYPT, anubis_cbc_enc_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
787                 test_cipher ("anubis", MODE_CBC, DECRYPT, anubis_cbc_dec_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
788
789                 test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
790                 test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
791                 test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
792                 test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
793                 test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
794                 test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
795                 test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
796                 test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
797                 test_deflate();
798                 test_crc32c();
799 #ifdef CONFIG_CRYPTO_HMAC
800                 test_hmac("md5", hmac_md5_tv_template, HMAC_MD5_TEST_VECTORS);
801                 test_hmac("sha1", hmac_sha1_tv_template, HMAC_SHA1_TEST_VECTORS);
802                 test_hmac("sha256", hmac_sha256_tv_template, HMAC_SHA256_TEST_VECTORS);
803 #endif
804
805                 test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
806                 break;
807
808         case 1:
809                 test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
810                 break;
811
812         case 2:
813                 test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
814                 break;
815
816         case 3:
817                 test_cipher ("des", MODE_ECB, ENCRYPT, des_enc_tv_template, DES_ENC_TEST_VECTORS);
818                 test_cipher ("des", MODE_ECB, DECRYPT, des_dec_tv_template, DES_DEC_TEST_VECTORS);
819                 test_cipher ("des", MODE_CBC, ENCRYPT, des_cbc_enc_tv_template, DES_CBC_ENC_TEST_VECTORS);
820                 test_cipher ("des", MODE_CBC, DECRYPT, des_cbc_dec_tv_template, DES_CBC_DEC_TEST_VECTORS);
821                 break;
822
823         case 4:
824                 test_cipher ("des3_ede", MODE_ECB, ENCRYPT, des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS);
825                 test_cipher ("des3_ede", MODE_ECB, DECRYPT, des3_ede_dec_tv_template, DES3_EDE_DEC_TEST_VECTORS);
826                 break;
827
828         case 5:
829                 test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
830                 break;
831
832         case 6:
833                 test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
834                 break;
835
836         case 7:
837                 test_cipher ("blowfish", MODE_ECB, ENCRYPT, bf_enc_tv_template, BF_ENC_TEST_VECTORS);
838                 test_cipher ("blowfish", MODE_ECB, DECRYPT, bf_dec_tv_template, BF_DEC_TEST_VECTORS);
839                 test_cipher ("blowfish", MODE_CBC, ENCRYPT, bf_cbc_enc_tv_template, BF_CBC_ENC_TEST_VECTORS);
840                 test_cipher ("blowfish", MODE_CBC, DECRYPT, bf_cbc_dec_tv_template, BF_CBC_DEC_TEST_VECTORS);
841                 break;
842
843         case 8:
844                 test_cipher ("twofish", MODE_ECB, ENCRYPT, tf_enc_tv_template, TF_ENC_TEST_VECTORS);
845                 test_cipher ("twofish", MODE_ECB, DECRYPT, tf_dec_tv_template, TF_DEC_TEST_VECTORS);
846                 test_cipher ("twofish", MODE_CBC, ENCRYPT, tf_cbc_enc_tv_template, TF_CBC_ENC_TEST_VECTORS);
847                 test_cipher ("twofish", MODE_CBC, DECRYPT, tf_cbc_dec_tv_template, TF_CBC_DEC_TEST_VECTORS);
848                 break;
849
850         case 9:
851                 test_cipher ("serpent", MODE_ECB, ENCRYPT, serpent_enc_tv_template, SERPENT_ENC_TEST_VECTORS);
852                 test_cipher ("serpent", MODE_ECB, DECRYPT, serpent_dec_tv_template, SERPENT_DEC_TEST_VECTORS);
853                 break;
854
855         case 10:
856                 test_cipher ("aes", MODE_ECB, ENCRYPT, aes_enc_tv_template, AES_ENC_TEST_VECTORS);
857                 test_cipher ("aes", MODE_ECB, DECRYPT, aes_dec_tv_template, AES_DEC_TEST_VECTORS);
858                 break;
859
860         case 11:
861                 test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
862                 break;
863
864         case 12:
865                 test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
866                 break;
867
868         case 13:
869                 test_deflate();
870                 break;
871
872         case 14:
873                 test_cipher ("cast5", MODE_ECB, ENCRYPT, cast5_enc_tv_template, CAST5_ENC_TEST_VECTORS);
874                 test_cipher ("cast5", MODE_ECB, DECRYPT, cast5_dec_tv_template, CAST5_DEC_TEST_VECTORS);
875                 break;
876
877         case 15:
878                 test_cipher ("cast6", MODE_ECB, ENCRYPT, cast6_enc_tv_template, CAST6_ENC_TEST_VECTORS);
879                 test_cipher ("cast6", MODE_ECB, DECRYPT, cast6_dec_tv_template, CAST6_DEC_TEST_VECTORS);
880                 break;
881
882         case 16:
883                 test_cipher ("arc4", MODE_ECB, ENCRYPT, arc4_enc_tv_template, ARC4_ENC_TEST_VECTORS);
884                 test_cipher ("arc4", MODE_ECB, DECRYPT, arc4_dec_tv_template, ARC4_DEC_TEST_VECTORS);
885                 break;
886
887         case 17:
888                 test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
889                 break;
890
891         case 18:
892                 test_crc32c();
893                 break;
894
895         case 19:
896                 test_cipher ("tea", MODE_ECB, ENCRYPT, tea_enc_tv_template, TEA_ENC_TEST_VECTORS);
897                 test_cipher ("tea", MODE_ECB, DECRYPT, tea_dec_tv_template, TEA_DEC_TEST_VECTORS);
898                 break;
899
900         case 20:
901                 test_cipher ("xtea", MODE_ECB, ENCRYPT, xtea_enc_tv_template, XTEA_ENC_TEST_VECTORS);
902                 test_cipher ("xtea", MODE_ECB, DECRYPT, xtea_dec_tv_template, XTEA_DEC_TEST_VECTORS);
903                 break;
904
905         case 21:
906                 test_cipher ("khazad", MODE_ECB, ENCRYPT, khazad_enc_tv_template, KHAZAD_ENC_TEST_VECTORS);
907                 test_cipher ("khazad", MODE_ECB, DECRYPT, khazad_dec_tv_template, KHAZAD_DEC_TEST_VECTORS);
908                 break;
909
910         case 22:
911                 test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
912                 break;
913
914         case 23:
915                 test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
916                 break;
917
918         case 24:
919                 test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
920                 break;
921
922         case 25:
923                 test_cipher ("tnepres", MODE_ECB, ENCRYPT, tnepres_enc_tv_template, TNEPRES_ENC_TEST_VECTORS);
924                 test_cipher ("tnepres", MODE_ECB, DECRYPT, tnepres_dec_tv_template, TNEPRES_DEC_TEST_VECTORS);
925                 break;
926
927         case 26:
928                 test_cipher ("anubis", MODE_ECB, ENCRYPT, anubis_enc_tv_template, ANUBIS_ENC_TEST_VECTORS);
929                 test_cipher ("anubis", MODE_ECB, DECRYPT, anubis_dec_tv_template, ANUBIS_DEC_TEST_VECTORS);
930                 test_cipher ("anubis", MODE_CBC, ENCRYPT, anubis_cbc_enc_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
931                 test_cipher ("anubis", MODE_CBC, DECRYPT, anubis_cbc_dec_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
932                 break;
933
934         case 27:
935                 test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
936                 break;
937
938         case 28:
939
940                 test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
941                 break;
942
943         case 29:
944                 test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
945                 break;
946
947 #ifdef CONFIG_CRYPTO_HMAC
948         case 100:
949                 test_hmac("md5", hmac_md5_tv_template, HMAC_MD5_TEST_VECTORS);
950                 break;
951
952         case 101:
953                 test_hmac("sha1", hmac_sha1_tv_template, HMAC_SHA1_TEST_VECTORS);
954                 break;
955
956         case 102:
957                 test_hmac("sha256", hmac_sha256_tv_template, HMAC_SHA256_TEST_VECTORS);
958                 break;
959
960 #endif
961
962         case 200:
963                 test_cipher_speed("aes", MODE_ECB, ENCRYPT, sec, NULL, 0,
964                                   aes_speed_template);
965                 test_cipher_speed("aes", MODE_ECB, DECRYPT, sec, NULL, 0,
966                                   aes_speed_template);
967                 test_cipher_speed("aes", MODE_CBC, ENCRYPT, sec, NULL, 0,
968                                   aes_speed_template);
969                 test_cipher_speed("aes", MODE_CBC, DECRYPT, sec, NULL, 0,
970                                   aes_speed_template);
971                 break;
972
973         case 201:
974                 test_cipher_speed("des3_ede", MODE_ECB, ENCRYPT, sec,
975                                   des3_ede_enc_tv_template,
976                                   DES3_EDE_ENC_TEST_VECTORS,
977                                   des3_ede_speed_template);
978                 test_cipher_speed("des3_ede", MODE_ECB, DECRYPT, sec,
979                                   des3_ede_dec_tv_template,
980                                   DES3_EDE_DEC_TEST_VECTORS,
981                                   des3_ede_speed_template);
982                 test_cipher_speed("des3_ede", MODE_CBC, ENCRYPT, sec,
983                                   des3_ede_enc_tv_template,
984                                   DES3_EDE_ENC_TEST_VECTORS,
985                                   des3_ede_speed_template);
986                 test_cipher_speed("des3_ede", MODE_CBC, DECRYPT, sec,
987                                   des3_ede_dec_tv_template,
988                                   DES3_EDE_DEC_TEST_VECTORS,
989                                   des3_ede_speed_template);
990                 break;
991
992         case 202:
993                 test_cipher_speed("twofish", MODE_ECB, ENCRYPT, sec, NULL, 0,
994                                   twofish_speed_template);
995                 test_cipher_speed("twofish", MODE_ECB, DECRYPT, sec, NULL, 0,
996                                   twofish_speed_template);
997                 test_cipher_speed("twofish", MODE_CBC, ENCRYPT, sec, NULL, 0,
998                                   twofish_speed_template);
999                 test_cipher_speed("twofish", MODE_CBC, DECRYPT, sec, NULL, 0,
1000                                   twofish_speed_template);
1001                 break;
1002
1003         case 203:
1004                 test_cipher_speed("blowfish", MODE_ECB, ENCRYPT, sec, NULL, 0,
1005                                   blowfish_speed_template);
1006                 test_cipher_speed("blowfish", MODE_ECB, DECRYPT, sec, NULL, 0,
1007                                   blowfish_speed_template);
1008                 test_cipher_speed("blowfish", MODE_CBC, ENCRYPT, sec, NULL, 0,
1009                                   blowfish_speed_template);
1010                 test_cipher_speed("blowfish", MODE_CBC, DECRYPT, sec, NULL, 0,
1011                                   blowfish_speed_template);
1012                 break;
1013
1014         case 204:
1015                 test_cipher_speed("des", MODE_ECB, ENCRYPT, sec, NULL, 0,
1016                                   des_speed_template);
1017                 test_cipher_speed("des", MODE_ECB, DECRYPT, sec, NULL, 0,
1018                                   des_speed_template);
1019                 test_cipher_speed("des", MODE_CBC, ENCRYPT, sec, NULL, 0,
1020                                   des_speed_template);
1021                 test_cipher_speed("des", MODE_CBC, DECRYPT, sec, NULL, 0,
1022                                   des_speed_template);
1023                 break;
1024
1025         case 1000:
1026                 test_available();
1027                 break;
1028
1029         default:
1030                 /* useful for debugging */
1031                 printk("not testing anything\n");
1032                 break;
1033         }
1034 }
1035
1036 static int __init init(void)
1037 {
1038         tvmem = kmalloc(TVMEMSIZE, GFP_KERNEL);
1039         if (tvmem == NULL)
1040                 return -ENOMEM;
1041
1042         xbuf = kmalloc(XBUFSIZE, GFP_KERNEL);
1043         if (xbuf == NULL) {
1044                 kfree(tvmem);
1045                 return -ENOMEM;
1046         }
1047
1048         do_test();
1049
1050         kfree(xbuf);
1051         kfree(tvmem);
1052         return 0;
1053 }
1054
1055 /*
1056  * If an init function is provided, an exit function must also be provided
1057  * to allow module unload.
1058  */
1059 static void __exit fini(void) { }
1060
1061 module_init(init);
1062 module_exit(fini);
1063
1064 module_param(mode, int, 0);
1065 module_param(sec, uint, 0);
1066 MODULE_PARM_DESC(sec, "Length in seconds of speed tests");
1067
1068 MODULE_LICENSE("GPL");
1069 MODULE_DESCRIPTION("Quick & dirty crypto testing module");
1070 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");