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