]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - crypto/tcrypt.c
[CRYPTO] hmac: Add crypto template implementation
[mirror_ubuntu-zesty-kernel.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
cba83564 20#include <linux/err.h>
1da177e4
LT
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/mm.h>
24#include <linux/slab.h>
378f058c 25#include <linux/scatterlist.h>
1da177e4
LT
26#include <linux/string.h>
27#include <linux/crypto.h>
28#include <linux/highmem.h>
29#include <linux/moduleparam.h>
ebfd9bcf 30#include <linux/jiffies.h>
6a17944c
HX
31#include <linux/timex.h>
32#include <linux/interrupt.h>
1da177e4
LT
33#include "tcrypt.h"
34
35/*
36 * Need to kmalloc() memory for testing kmap().
37 */
ebfd9bcf 38#define TVMEMSIZE 16384
1da177e4
LT
39#define XBUFSIZE 32768
40
41/*
42 * Indexes into the xbuf to simulate cross-page access.
43 */
44#define IDX1 37
45#define IDX2 32400
46#define IDX3 1
47#define IDX4 8193
48#define IDX5 22222
49#define IDX6 17101
50#define IDX7 27333
51#define IDX8 3000
52
53/*
54* Used by test_cipher()
55*/
56#define ENCRYPT 1
57#define DECRYPT 0
1da177e4
LT
58
59static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
60
ebfd9bcf
HW
61/*
62 * Used by test_cipher_speed()
63 */
6a17944c 64static unsigned int sec;
ebfd9bcf 65
1da177e4
LT
66static int mode;
67static char *xbuf;
68static char *tvmem;
69
70static char *check[] = {
71 "des", "md5", "des3_ede", "rot13", "sha1", "sha256", "blowfish",
ef2736fc
HX
72 "twofish", "serpent", "sha384", "sha512", "md4", "aes", "cast6",
73 "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
fb4f10ed 74 "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", NULL
1da177e4
LT
75};
76
ef2736fc 77static void hexdump(unsigned char *buf, unsigned int len)
1da177e4
LT
78{
79 while (len--)
80 printk("%02x", *buf++);
81
82 printk("\n");
83}
84
ef2736fc
HX
85static void test_hash(char *algo, struct hash_testvec *template,
86 unsigned int tcount)
1da177e4 87{
ef2736fc
HX
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 116
378f058c 117 sg_set_buf(&sg[0], hash_tv[i].plaintext, hash_tv[i].psize);
1da177e4 118
ef2736fc 119 crypto_digest_init(tfm);
560c06ae 120 crypto_digest_setkey(tfm, hash_tv[i].key, hash_tv[i].ksize);
ef2736fc
HX
121 crypto_digest_update(tfm, sg, 1);
122 crypto_digest_final(tfm, result);
1da177e4 123
ef2736fc 124 hexdump(result, crypto_tfm_alg_digestsize(tfm));
1da177e4 125 printk("%s\n",
ef2736fc
HX
126 memcmp(result, hash_tv[i].digest,
127 crypto_tfm_alg_digestsize(tfm)) ?
128 "fail" : "pass");
1da177e4
LT
129 }
130
ef2736fc 131 printk("testing %s across pages\n", algo);
1da177e4
LT
132
133 /* setup the dummy buffer first */
ef2736fc 134 memset(xbuf, 0, XBUFSIZE);
1da177e4
LT
135
136 j = 0;
137 for (i = 0; i < tcount; i++) {
138 if (hash_tv[i].np) {
139 j++;
ef2736fc
HX
140 printk("test %u:\n", j);
141 memset(result, 0, 64);
1da177e4
LT
142
143 temp = 0;
144 for (k = 0; k < hash_tv[i].np; k++) {
ef2736fc
HX
145 memcpy(&xbuf[IDX[k]],
146 hash_tv[i].plaintext + temp,
147 hash_tv[i].tap[k]);
1da177e4 148 temp += hash_tv[i].tap[k];
378f058c
DH
149 sg_set_buf(&sg[k], &xbuf[IDX[k]],
150 hash_tv[i].tap[k]);
1da177e4
LT
151 }
152
ef2736fc
HX
153 crypto_digest_digest(tfm, sg, hash_tv[i].np, result);
154
155 hexdump(result, crypto_tfm_alg_digestsize(tfm));
1da177e4 156 printk("%s\n",
ef2736fc
HX
157 memcmp(result, hash_tv[i].digest,
158 crypto_tfm_alg_digestsize(tfm)) ?
159 "fail" : "pass");
1da177e4
LT
160 }
161 }
ef2736fc
HX
162
163 crypto_free_tfm(tfm);
1da177e4
LT
164}
165
166
167#ifdef CONFIG_CRYPTO_HMAC
168
ef2736fc
HX
169static void test_hmac(char *algo, struct hmac_testvec *template,
170 unsigned int tcount)
1da177e4 171{
1da177e4
LT
172 unsigned int i, j, k, temp;
173 struct scatterlist sg[8];
174 char result[64];
175 struct crypto_tfm *tfm;
176 struct hmac_testvec *hmac_tv;
177 unsigned int tsize, klen;
178
179 tfm = crypto_alloc_tfm(algo, 0);
180 if (tfm == NULL) {
181 printk("failed to load transform for %s\n", algo);
182 return;
183 }
184
185 printk("\ntesting hmac_%s\n", algo);
ef2736fc
HX
186
187 tsize = sizeof(struct hmac_testvec);
1da177e4
LT
188 tsize *= tcount;
189 if (tsize > TVMEMSIZE) {
190 printk("template (%u) too big for tvmem (%u)\n", tsize,
191 TVMEMSIZE);
192 goto out;
193 }
194
195 memcpy(tvmem, template, tsize);
ef2736fc 196 hmac_tv = (void *)tvmem;
1da177e4
LT
197
198 for (i = 0; i < tcount; i++) {
199 printk("test %u:\n", i + 1);
200 memset(result, 0, sizeof (result));
201
1da177e4 202 klen = hmac_tv[i].ksize;
378f058c 203 sg_set_buf(&sg[0], hmac_tv[i].plaintext, hmac_tv[i].psize);
1da177e4
LT
204
205 crypto_hmac(tfm, hmac_tv[i].key, &klen, sg, 1, result);
206
207 hexdump(result, crypto_tfm_alg_digestsize(tfm));
208 printk("%s\n",
209 memcmp(result, hmac_tv[i].digest,
210 crypto_tfm_alg_digestsize(tfm)) ? "fail" :
211 "pass");
212 }
213
214 printk("\ntesting hmac_%s across pages\n", algo);
215
216 memset(xbuf, 0, XBUFSIZE);
ef2736fc 217
1da177e4
LT
218 j = 0;
219 for (i = 0; i < tcount; i++) {
220 if (hmac_tv[i].np) {
221 j++;
ef2736fc
HX
222 printk("test %u:\n",j);
223 memset(result, 0, 64);
1da177e4
LT
224
225 temp = 0;
226 klen = hmac_tv[i].ksize;
227 for (k = 0; k < hmac_tv[i].np; k++) {
ef2736fc
HX
228 memcpy(&xbuf[IDX[k]],
229 hmac_tv[i].plaintext + temp,
230 hmac_tv[i].tap[k]);
1da177e4 231 temp += hmac_tv[i].tap[k];
378f058c
DH
232 sg_set_buf(&sg[k], &xbuf[IDX[k]],
233 hmac_tv[i].tap[k]);
1da177e4
LT
234 }
235
ef2736fc
HX
236 crypto_hmac(tfm, hmac_tv[i].key, &klen, sg,
237 hmac_tv[i].np, result);
1da177e4 238 hexdump(result, crypto_tfm_alg_digestsize(tfm));
ef2736fc 239
1da177e4 240 printk("%s\n",
ef2736fc
HX
241 memcmp(result, hmac_tv[i].digest,
242 crypto_tfm_alg_digestsize(tfm)) ?
243 "fail" : "pass");
1da177e4
LT
244 }
245 }
246out:
247 crypto_free_tfm(tfm);
248}
249
250#endif /* CONFIG_CRYPTO_HMAC */
251
cba83564 252static void test_cipher(char *algo, int enc,
ef2736fc 253 struct cipher_testvec *template, unsigned int tcount)
1da177e4
LT
254{
255 unsigned int ret, i, j, k, temp;
256 unsigned int tsize;
cba83564
HX
257 unsigned int iv_len;
258 unsigned int len;
378f058c 259 char *q;
cba83564 260 struct crypto_blkcipher *tfm;
1da177e4
LT
261 char *key;
262 struct cipher_testvec *cipher_tv;
cba83564 263 struct blkcipher_desc desc;
1da177e4 264 struct scatterlist sg[8];
cba83564 265 const char *e;
1da177e4
LT
266
267 if (enc == ENCRYPT)
3cc3816f 268 e = "encryption";
1da177e4 269 else
3cc3816f 270 e = "decryption";
1da177e4 271
cba83564 272 printk("\ntesting %s %s\n", algo, e);
1da177e4 273
ef2736fc 274 tsize = sizeof (struct cipher_testvec);
1da177e4 275 tsize *= tcount;
ef2736fc 276
1da177e4
LT
277 if (tsize > TVMEMSIZE) {
278 printk("template (%u) too big for tvmem (%u)\n", tsize,
279 TVMEMSIZE);
280 return;
281 }
282
283 memcpy(tvmem, template, tsize);
ef2736fc
HX
284 cipher_tv = (void *)tvmem;
285
cba83564 286 tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
1da177e4 287
cba83564
HX
288 if (IS_ERR(tfm)) {
289 printk("failed to load transform for %s: %ld\n", algo,
290 PTR_ERR(tfm));
1da177e4
LT
291 return;
292 }
cba83564
HX
293 desc.tfm = tfm;
294 desc.flags = 0;
ef2736fc 295
1da177e4
LT
296 j = 0;
297 for (i = 0; i < tcount; i++) {
298 if (!(cipher_tv[i].np)) {
ef2736fc 299 j++;
1da177e4
LT
300 printk("test %u (%d bit key):\n",
301 j, cipher_tv[i].klen * 8);
302
cba83564 303 crypto_blkcipher_clear_flags(tfm, ~0);
ef2736fc 304 if (cipher_tv[i].wk)
cba83564
HX
305 crypto_blkcipher_set_flags(
306 tfm, CRYPTO_TFM_REQ_WEAK_KEY);
1da177e4 307 key = cipher_tv[i].key;
ef2736fc 308
cba83564
HX
309 ret = crypto_blkcipher_setkey(tfm, key,
310 cipher_tv[i].klen);
1da177e4 311 if (ret) {
cba83564
HX
312 printk("setkey() failed flags=%x\n",
313 crypto_blkcipher_get_flags(tfm));
ef2736fc 314
1da177e4
LT
315 if (!cipher_tv[i].fail)
316 goto out;
ef2736fc 317 }
1da177e4 318
378f058c
DH
319 sg_set_buf(&sg[0], cipher_tv[i].input,
320 cipher_tv[i].ilen);
ef2736fc 321
cba83564
HX
322 iv_len = crypto_blkcipher_ivsize(tfm);
323 if (iv_len)
324 crypto_blkcipher_set_iv(tfm, cipher_tv[i].iv,
325 iv_len);
ef2736fc 326
cba83564
HX
327 len = cipher_tv[i].ilen;
328 ret = enc ?
329 crypto_blkcipher_encrypt(&desc, sg, sg, len) :
330 crypto_blkcipher_decrypt(&desc, sg, sg, len);
ef2736fc 331
1da177e4 332 if (ret) {
cba83564
HX
333 printk("%s () failed flags=%x\n", e,
334 desc.flags);
1da177e4 335 goto out;
ef2736fc
HX
336 }
337
1da177e4
LT
338 q = kmap(sg[0].page) + sg[0].offset;
339 hexdump(q, cipher_tv[i].rlen);
ef2736fc
HX
340
341 printk("%s\n",
342 memcmp(q, cipher_tv[i].result,
343 cipher_tv[i].rlen) ? "fail" : "pass");
1da177e4
LT
344 }
345 }
ef2736fc 346
cba83564 347 printk("\ntesting %s %s across pages (chunking)\n", algo, e);
1da177e4 348 memset(xbuf, 0, XBUFSIZE);
ef2736fc 349
1da177e4
LT
350 j = 0;
351 for (i = 0; i < tcount; i++) {
352 if (cipher_tv[i].np) {
ef2736fc 353 j++;
1da177e4
LT
354 printk("test %u (%d bit key):\n",
355 j, cipher_tv[i].klen * 8);
356
cba83564 357 crypto_blkcipher_clear_flags(tfm, ~0);
ef2736fc 358 if (cipher_tv[i].wk)
cba83564
HX
359 crypto_blkcipher_set_flags(
360 tfm, CRYPTO_TFM_REQ_WEAK_KEY);
1da177e4 361 key = cipher_tv[i].key;
ef2736fc 362
cba83564
HX
363 ret = crypto_blkcipher_setkey(tfm, key,
364 cipher_tv[i].klen);
1da177e4 365 if (ret) {
cba83564
HX
366 printk("setkey() failed flags=%x\n",
367 crypto_blkcipher_get_flags(tfm));
ef2736fc 368
1da177e4
LT
369 if (!cipher_tv[i].fail)
370 goto out;
371 }
372
373 temp = 0;
374 for (k = 0; k < cipher_tv[i].np; k++) {
ef2736fc
HX
375 memcpy(&xbuf[IDX[k]],
376 cipher_tv[i].input + temp,
377 cipher_tv[i].tap[k]);
1da177e4 378 temp += cipher_tv[i].tap[k];
378f058c
DH
379 sg_set_buf(&sg[k], &xbuf[IDX[k]],
380 cipher_tv[i].tap[k]);
1da177e4 381 }
ef2736fc 382
cba83564
HX
383 iv_len = crypto_blkcipher_ivsize(tfm);
384 if (iv_len)
385 crypto_blkcipher_set_iv(tfm, cipher_tv[i].iv,
386 iv_len);
ef2736fc 387
cba83564
HX
388 len = cipher_tv[i].ilen;
389 ret = enc ?
390 crypto_blkcipher_encrypt(&desc, sg, sg, len) :
391 crypto_blkcipher_decrypt(&desc, sg, sg, len);
ef2736fc 392
1da177e4 393 if (ret) {
cba83564
HX
394 printk("%s () failed flags=%x\n", e,
395 desc.flags);
1da177e4
LT
396 goto out;
397 }
398
399 temp = 0;
400 for (k = 0; k < cipher_tv[i].np; k++) {
401 printk("page %u\n", k);
402 q = kmap(sg[k].page) + sg[k].offset;
403 hexdump(q, cipher_tv[i].tap[k]);
ef2736fc
HX
404 printk("%s\n",
405 memcmp(q, cipher_tv[i].result + temp,
406 cipher_tv[i].tap[k]) ? "fail" :
1da177e4
LT
407 "pass");
408 temp += cipher_tv[i].tap[k];
409 }
410 }
411 }
412
413out:
cba83564 414 crypto_free_blkcipher(tfm);
1da177e4
LT
415}
416
cba83564 417static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc, char *p,
6a17944c
HX
418 int blen, int sec)
419{
6df5b9f4 420 struct scatterlist sg[1];
6a17944c
HX
421 unsigned long start, end;
422 int bcount;
423 int ret;
424
6df5b9f4 425 sg_set_buf(sg, p, blen);
6a17944c
HX
426
427 for (start = jiffies, end = start + sec * HZ, bcount = 0;
428 time_before(jiffies, end); bcount++) {
429 if (enc)
cba83564 430 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
6a17944c 431 else
cba83564 432 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
6a17944c
HX
433
434 if (ret)
435 return ret;
436 }
437
438 printk("%d operations in %d seconds (%ld bytes)\n",
439 bcount, sec, (long)bcount * blen);
440 return 0;
441}
442
cba83564 443static int test_cipher_cycles(struct blkcipher_desc *desc, int enc, char *p,
6a17944c
HX
444 int blen)
445{
6df5b9f4 446 struct scatterlist sg[1];
6a17944c
HX
447 unsigned long cycles = 0;
448 int ret = 0;
449 int i;
450
6df5b9f4 451 sg_set_buf(sg, p, blen);
6a17944c
HX
452
453 local_bh_disable();
454 local_irq_disable();
455
456 /* Warm-up run. */
457 for (i = 0; i < 4; i++) {
458 if (enc)
cba83564 459 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
6a17944c 460 else
cba83564 461 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
6a17944c
HX
462
463 if (ret)
464 goto out;
465 }
466
467 /* The real thing. */
468 for (i = 0; i < 8; i++) {
469 cycles_t start, end;
470
471 start = get_cycles();
472 if (enc)
cba83564 473 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
6a17944c 474 else
cba83564 475 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
6a17944c
HX
476 end = get_cycles();
477
478 if (ret)
479 goto out;
480
481 cycles += end - start;
482 }
483
484out:
485 local_irq_enable();
486 local_bh_enable();
487
488 if (ret == 0)
489 printk("1 operation in %lu cycles (%d bytes)\n",
490 (cycles + 4) / 8, blen);
491
492 return ret;
493}
494
cba83564 495static void test_cipher_speed(char *algo, int enc, unsigned int sec,
dce907c0
HX
496 struct cipher_testvec *template,
497 unsigned int tcount, struct cipher_speed *speed)
ebfd9bcf 498{
dce907c0 499 unsigned int ret, i, j, iv_len;
ebfd9bcf 500 unsigned char *key, *p, iv[128];
cba83564
HX
501 struct crypto_blkcipher *tfm;
502 struct blkcipher_desc desc;
503 const char *e;
ebfd9bcf
HW
504
505 if (enc == ENCRYPT)
506 e = "encryption";
507 else
508 e = "decryption";
ebfd9bcf 509
cba83564 510 printk("\ntesting speed of %s %s\n", algo, e);
ebfd9bcf 511
cba83564 512 tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
ebfd9bcf 513
cba83564
HX
514 if (IS_ERR(tfm)) {
515 printk("failed to load transform for %s: %ld\n", algo,
516 PTR_ERR(tfm));
ebfd9bcf
HW
517 return;
518 }
cba83564
HX
519 desc.tfm = tfm;
520 desc.flags = 0;
ebfd9bcf
HW
521
522 for (i = 0; speed[i].klen != 0; i++) {
523 if ((speed[i].blen + speed[i].klen) > TVMEMSIZE) {
524 printk("template (%u) too big for tvmem (%u)\n",
525 speed[i].blen + speed[i].klen, TVMEMSIZE);
526 goto out;
527 }
528
529 printk("test %u (%d bit key, %d byte blocks): ", i,
530 speed[i].klen * 8, speed[i].blen);
531
532 memset(tvmem, 0xff, speed[i].klen + speed[i].blen);
533
534 /* set key, plain text and IV */
535 key = (unsigned char *)tvmem;
dce907c0
HX
536 for (j = 0; j < tcount; j++) {
537 if (template[j].klen == speed[i].klen) {
538 key = template[j].key;
539 break;
540 }
541 }
ebfd9bcf
HW
542 p = (unsigned char *)tvmem + speed[i].klen;
543
cba83564 544 ret = crypto_blkcipher_setkey(tfm, key, speed[i].klen);
ebfd9bcf 545 if (ret) {
cba83564
HX
546 printk("setkey() failed flags=%x\n",
547 crypto_blkcipher_get_flags(tfm));
ebfd9bcf
HW
548 goto out;
549 }
550
cba83564
HX
551 iv_len = crypto_blkcipher_ivsize(tfm);
552 if (iv_len) {
ebfd9bcf 553 memset(&iv, 0xff, iv_len);
cba83564 554 crypto_blkcipher_set_iv(tfm, iv, iv_len);
ebfd9bcf
HW
555 }
556
6a17944c 557 if (sec)
cba83564 558 ret = test_cipher_jiffies(&desc, enc, p, speed[i].blen,
6a17944c
HX
559 sec);
560 else
cba83564 561 ret = test_cipher_cycles(&desc, enc, p, speed[i].blen);
ebfd9bcf 562
6a17944c 563 if (ret) {
cba83564 564 printk("%s() failed flags=%x\n", e, desc.flags);
6a17944c 565 break;
ebfd9bcf 566 }
ebfd9bcf
HW
567 }
568
569out:
cba83564 570 crypto_free_blkcipher(tfm);
ebfd9bcf
HW
571}
572
e8057928
ML
573static void test_digest_jiffies(struct crypto_tfm *tfm, char *p, int blen,
574 int plen, char *out, int sec)
575{
576 struct scatterlist sg[1];
577 unsigned long start, end;
578 int bcount, pcount;
579
580 for (start = jiffies, end = start + sec * HZ, bcount = 0;
581 time_before(jiffies, end); bcount++) {
582 crypto_digest_init(tfm);
583 for (pcount = 0; pcount < blen; pcount += plen) {
584 sg_set_buf(sg, p + pcount, plen);
585 crypto_digest_update(tfm, sg, 1);
586 }
587 /* we assume there is enough space in 'out' for the result */
588 crypto_digest_final(tfm, out);
589 }
590
591 printk("%6u opers/sec, %9lu bytes/sec\n",
592 bcount / sec, ((long)bcount * blen) / sec);
593
594 return;
595}
596
597static void test_digest_cycles(struct crypto_tfm *tfm, char *p, int blen,
598 int plen, char *out)
599{
600 struct scatterlist sg[1];
601 unsigned long cycles = 0;
602 int i, pcount;
603
604 local_bh_disable();
605 local_irq_disable();
606
607 /* Warm-up run. */
608 for (i = 0; i < 4; i++) {
609 crypto_digest_init(tfm);
610 for (pcount = 0; pcount < blen; pcount += plen) {
611 sg_set_buf(sg, p + pcount, plen);
612 crypto_digest_update(tfm, sg, 1);
613 }
614 crypto_digest_final(tfm, out);
615 }
616
617 /* The real thing. */
618 for (i = 0; i < 8; i++) {
619 cycles_t start, end;
620
621 crypto_digest_init(tfm);
622
623 start = get_cycles();
624
625 for (pcount = 0; pcount < blen; pcount += plen) {
626 sg_set_buf(sg, p + pcount, plen);
627 crypto_digest_update(tfm, sg, 1);
628 }
629 crypto_digest_final(tfm, out);
630
631 end = get_cycles();
632
633 cycles += end - start;
634 }
635
636 local_irq_enable();
637 local_bh_enable();
638
639 printk("%6lu cycles/operation, %4lu cycles/byte\n",
640 cycles / 8, cycles / (8 * blen));
641
642 return;
643}
644
645static void test_digest_speed(char *algo, unsigned int sec,
646 struct digest_speed *speed)
647{
648 struct crypto_tfm *tfm;
649 char output[1024];
650 int i;
651
652 printk("\ntesting speed of %s\n", algo);
653
654 tfm = crypto_alloc_tfm(algo, 0);
655
656 if (tfm == NULL) {
657 printk("failed to load transform for %s\n", algo);
658 return;
659 }
660
661 if (crypto_tfm_alg_digestsize(tfm) > sizeof(output)) {
662 printk("digestsize(%u) > outputbuffer(%zu)\n",
663 crypto_tfm_alg_digestsize(tfm), sizeof(output));
664 goto out;
665 }
666
667 for (i = 0; speed[i].blen != 0; i++) {
668 if (speed[i].blen > TVMEMSIZE) {
669 printk("template (%u) too big for tvmem (%u)\n",
670 speed[i].blen, TVMEMSIZE);
671 goto out;
672 }
673
674 printk("test%3u (%5u byte blocks,%5u bytes per update,%4u updates): ",
675 i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
676
677 memset(tvmem, 0xff, speed[i].blen);
678
679 if (sec)
680 test_digest_jiffies(tfm, tvmem, speed[i].blen, speed[i].plen, output, sec);
681 else
682 test_digest_cycles(tfm, tvmem, speed[i].blen, speed[i].plen, output);
683 }
684
685out:
686 crypto_free_tfm(tfm);
687}
688
ef2736fc 689static void test_deflate(void)
1da177e4
LT
690{
691 unsigned int i;
692 char result[COMP_BUF_SIZE];
693 struct crypto_tfm *tfm;
694 struct comp_testvec *tv;
695 unsigned int tsize;
696
697 printk("\ntesting deflate compression\n");
698
699 tsize = sizeof (deflate_comp_tv_template);
700 if (tsize > TVMEMSIZE) {
701 printk("template (%u) too big for tvmem (%u)\n", tsize,
702 TVMEMSIZE);
703 return;
704 }
705
706 memcpy(tvmem, deflate_comp_tv_template, tsize);
ef2736fc 707 tv = (void *)tvmem;
1da177e4
LT
708
709 tfm = crypto_alloc_tfm("deflate", 0);
710 if (tfm == NULL) {
711 printk("failed to load transform for deflate\n");
712 return;
713 }
714
715 for (i = 0; i < DEFLATE_COMP_TEST_VECTORS; i++) {
716 int ilen, ret, dlen = COMP_BUF_SIZE;
ef2736fc 717
1da177e4
LT
718 printk("test %u:\n", i + 1);
719 memset(result, 0, sizeof (result));
720
721 ilen = tv[i].inlen;
722 ret = crypto_comp_compress(tfm, tv[i].input,
723 ilen, result, &dlen);
724 if (ret) {
725 printk("fail: ret=%d\n", ret);
726 continue;
727 }
728 hexdump(result, dlen);
729 printk("%s (ratio %d:%d)\n",
730 memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
731 ilen, dlen);
732 }
733
734 printk("\ntesting deflate decompression\n");
735
736 tsize = sizeof (deflate_decomp_tv_template);
737 if (tsize > TVMEMSIZE) {
738 printk("template (%u) too big for tvmem (%u)\n", tsize,
739 TVMEMSIZE);
740 goto out;
741 }
742
743 memcpy(tvmem, deflate_decomp_tv_template, tsize);
ef2736fc 744 tv = (void *)tvmem;
1da177e4
LT
745
746 for (i = 0; i < DEFLATE_DECOMP_TEST_VECTORS; i++) {
747 int ilen, ret, dlen = COMP_BUF_SIZE;
ef2736fc 748
1da177e4
LT
749 printk("test %u:\n", i + 1);
750 memset(result, 0, sizeof (result));
751
752 ilen = tv[i].inlen;
753 ret = crypto_comp_decompress(tfm, tv[i].input,
754 ilen, result, &dlen);
755 if (ret) {
756 printk("fail: ret=%d\n", ret);
757 continue;
758 }
759 hexdump(result, dlen);
760 printk("%s (ratio %d:%d)\n",
761 memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
762 ilen, dlen);
763 }
764out:
765 crypto_free_tfm(tfm);
766}
767
ef2736fc 768static void test_available(void)
1da177e4
LT
769{
770 char **name = check;
ef2736fc 771
1da177e4
LT
772 while (*name) {
773 printk("alg %s ", *name);
774 printk((crypto_alg_available(*name, 0)) ?
775 "found\n" : "not found\n");
776 name++;
ef2736fc 777 }
1da177e4
LT
778}
779
ef2736fc 780static void do_test(void)
1da177e4
LT
781{
782 switch (mode) {
783
784 case 0:
785 test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
ef2736fc 786
1da177e4 787 test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
ef2736fc 788
1da177e4 789 //DES
cba83564
HX
790 test_cipher("ecb(des)", ENCRYPT, des_enc_tv_template,
791 DES_ENC_TEST_VECTORS);
792 test_cipher("ecb(des)", DECRYPT, des_dec_tv_template,
793 DES_DEC_TEST_VECTORS);
794 test_cipher("cbc(des)", ENCRYPT, des_cbc_enc_tv_template,
795 DES_CBC_ENC_TEST_VECTORS);
796 test_cipher("cbc(des)", DECRYPT, des_cbc_dec_tv_template,
797 DES_CBC_DEC_TEST_VECTORS);
ef2736fc 798
1da177e4 799 //DES3_EDE
cba83564
HX
800 test_cipher("ecb(des3_ede)", ENCRYPT, des3_ede_enc_tv_template,
801 DES3_EDE_ENC_TEST_VECTORS);
802 test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
803 DES3_EDE_DEC_TEST_VECTORS);
ef2736fc 804
1da177e4 805 test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
ef2736fc 806
1da177e4 807 test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
ef2736fc 808
1da177e4 809 //BLOWFISH
cba83564
HX
810 test_cipher("ecb(blowfish)", ENCRYPT, bf_enc_tv_template,
811 BF_ENC_TEST_VECTORS);
812 test_cipher("ecb(blowfish)", DECRYPT, bf_dec_tv_template,
813 BF_DEC_TEST_VECTORS);
814 test_cipher("cbc(blowfish)", ENCRYPT, bf_cbc_enc_tv_template,
815 BF_CBC_ENC_TEST_VECTORS);
816 test_cipher("cbc(blowfish)", DECRYPT, bf_cbc_dec_tv_template,
817 BF_CBC_DEC_TEST_VECTORS);
ef2736fc 818
1da177e4 819 //TWOFISH
cba83564
HX
820 test_cipher("ecb(twofish)", ENCRYPT, tf_enc_tv_template,
821 TF_ENC_TEST_VECTORS);
822 test_cipher("ecb(twofish)", DECRYPT, tf_dec_tv_template,
823 TF_DEC_TEST_VECTORS);
824 test_cipher("cbc(twofish)", ENCRYPT, tf_cbc_enc_tv_template,
825 TF_CBC_ENC_TEST_VECTORS);
826 test_cipher("cbc(twofish)", DECRYPT, tf_cbc_dec_tv_template,
827 TF_CBC_DEC_TEST_VECTORS);
ef2736fc 828
1da177e4 829 //SERPENT
cba83564
HX
830 test_cipher("ecb(serpent)", ENCRYPT, serpent_enc_tv_template,
831 SERPENT_ENC_TEST_VECTORS);
832 test_cipher("ecb(serpent)", DECRYPT, serpent_dec_tv_template,
833 SERPENT_DEC_TEST_VECTORS);
ef2736fc 834
1da177e4 835 //TNEPRES
cba83564
HX
836 test_cipher("ecb(tnepres)", ENCRYPT, tnepres_enc_tv_template,
837 TNEPRES_ENC_TEST_VECTORS);
838 test_cipher("ecb(tnepres)", DECRYPT, tnepres_dec_tv_template,
839 TNEPRES_DEC_TEST_VECTORS);
1da177e4
LT
840
841 //AES
cba83564
HX
842 test_cipher("ecb(aes)", ENCRYPT, aes_enc_tv_template,
843 AES_ENC_TEST_VECTORS);
844 test_cipher("ecb(aes)", DECRYPT, aes_dec_tv_template,
845 AES_DEC_TEST_VECTORS);
846 test_cipher("cbc(aes)", ENCRYPT, aes_cbc_enc_tv_template,
847 AES_CBC_ENC_TEST_VECTORS);
848 test_cipher("cbc(aes)", DECRYPT, aes_cbc_dec_tv_template,
849 AES_CBC_DEC_TEST_VECTORS);
1da177e4
LT
850
851 //CAST5
cba83564
HX
852 test_cipher("ecb(cast5)", ENCRYPT, cast5_enc_tv_template,
853 CAST5_ENC_TEST_VECTORS);
854 test_cipher("ecb(cast5)", DECRYPT, cast5_dec_tv_template,
855 CAST5_DEC_TEST_VECTORS);
ef2736fc 856
1da177e4 857 //CAST6
cba83564
HX
858 test_cipher("ecb(cast6)", ENCRYPT, cast6_enc_tv_template,
859 CAST6_ENC_TEST_VECTORS);
860 test_cipher("ecb(cast6)", DECRYPT, cast6_dec_tv_template,
861 CAST6_DEC_TEST_VECTORS);
1da177e4
LT
862
863 //ARC4
cba83564
HX
864 test_cipher("ecb(arc4)", ENCRYPT, arc4_enc_tv_template,
865 ARC4_ENC_TEST_VECTORS);
866 test_cipher("ecb(arc4)", DECRYPT, arc4_dec_tv_template,
867 ARC4_DEC_TEST_VECTORS);
1da177e4
LT
868
869 //TEA
cba83564
HX
870 test_cipher("ecb(tea)", ENCRYPT, tea_enc_tv_template,
871 TEA_ENC_TEST_VECTORS);
872 test_cipher("ecb(tea)", DECRYPT, tea_dec_tv_template,
873 TEA_DEC_TEST_VECTORS);
1da177e4
LT
874
875
876 //XTEA
cba83564
HX
877 test_cipher("ecb(xtea)", ENCRYPT, xtea_enc_tv_template,
878 XTEA_ENC_TEST_VECTORS);
879 test_cipher("ecb(xtea)", DECRYPT, xtea_dec_tv_template,
880 XTEA_DEC_TEST_VECTORS);
1da177e4
LT
881
882 //KHAZAD
cba83564
HX
883 test_cipher("ecb(khazad)", ENCRYPT, khazad_enc_tv_template,
884 KHAZAD_ENC_TEST_VECTORS);
885 test_cipher("ecb(khazad)", DECRYPT, khazad_dec_tv_template,
886 KHAZAD_DEC_TEST_VECTORS);
1da177e4
LT
887
888 //ANUBIS
cba83564
HX
889 test_cipher("ecb(anubis)", ENCRYPT, anubis_enc_tv_template,
890 ANUBIS_ENC_TEST_VECTORS);
891 test_cipher("ecb(anubis)", DECRYPT, anubis_dec_tv_template,
892 ANUBIS_DEC_TEST_VECTORS);
893 test_cipher("cbc(anubis)", ENCRYPT, anubis_cbc_enc_tv_template,
894 ANUBIS_CBC_ENC_TEST_VECTORS);
895 test_cipher("cbc(anubis)", DECRYPT, anubis_cbc_dec_tv_template,
896 ANUBIS_CBC_ENC_TEST_VECTORS);
1da177e4 897
fb4f10ed 898 //XETA
cba83564
HX
899 test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
900 XETA_ENC_TEST_VECTORS);
901 test_cipher("ecb(xeta)", DECRYPT, xeta_dec_tv_template,
902 XETA_DEC_TEST_VECTORS);
fb4f10ed 903
1da177e4
LT
904 test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
905 test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
906 test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
907 test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
908 test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
909 test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
910 test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
911 test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
912 test_deflate();
c907ee76 913 test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
1da177e4
LT
914#ifdef CONFIG_CRYPTO_HMAC
915 test_hmac("md5", hmac_md5_tv_template, HMAC_MD5_TEST_VECTORS);
ef2736fc 916 test_hmac("sha1", hmac_sha1_tv_template, HMAC_SHA1_TEST_VECTORS);
1da177e4 917 test_hmac("sha256", hmac_sha256_tv_template, HMAC_SHA256_TEST_VECTORS);
ef2736fc 918#endif
1da177e4
LT
919
920 test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
921 break;
922
923 case 1:
924 test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
925 break;
926
927 case 2:
928 test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
929 break;
930
931 case 3:
cba83564
HX
932 test_cipher("ecb(des)", ENCRYPT, des_enc_tv_template,
933 DES_ENC_TEST_VECTORS);
934 test_cipher("ecb(des)", DECRYPT, des_dec_tv_template,
935 DES_DEC_TEST_VECTORS);
936 test_cipher("cbc(des)", ENCRYPT, des_cbc_enc_tv_template,
937 DES_CBC_ENC_TEST_VECTORS);
938 test_cipher("cbc(des)", DECRYPT, des_cbc_dec_tv_template,
939 DES_CBC_DEC_TEST_VECTORS);
1da177e4
LT
940 break;
941
942 case 4:
cba83564
HX
943 test_cipher("ecb(des3_ede)", ENCRYPT, des3_ede_enc_tv_template,
944 DES3_EDE_ENC_TEST_VECTORS);
945 test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
946 DES3_EDE_DEC_TEST_VECTORS);
1da177e4
LT
947 break;
948
949 case 5:
950 test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
951 break;
ef2736fc 952
1da177e4
LT
953 case 6:
954 test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
955 break;
ef2736fc 956
1da177e4 957 case 7:
cba83564
HX
958 test_cipher("ecb(blowfish)", ENCRYPT, bf_enc_tv_template,
959 BF_ENC_TEST_VECTORS);
960 test_cipher("ecb(blowfish)", DECRYPT, bf_dec_tv_template,
961 BF_DEC_TEST_VECTORS);
962 test_cipher("cbc(blowfish)", ENCRYPT, bf_cbc_enc_tv_template,
963 BF_CBC_ENC_TEST_VECTORS);
964 test_cipher("cbc(blowfish)", DECRYPT, bf_cbc_dec_tv_template,
965 BF_CBC_DEC_TEST_VECTORS);
1da177e4
LT
966 break;
967
968 case 8:
cba83564
HX
969 test_cipher("ecb(twofish)", ENCRYPT, tf_enc_tv_template,
970 TF_ENC_TEST_VECTORS);
971 test_cipher("ecb(twofish)", DECRYPT, tf_dec_tv_template,
972 TF_DEC_TEST_VECTORS);
973 test_cipher("cbc(twofish)", ENCRYPT, tf_cbc_enc_tv_template,
974 TF_CBC_ENC_TEST_VECTORS);
975 test_cipher("cbc(twofish)", DECRYPT, tf_cbc_dec_tv_template,
976 TF_CBC_DEC_TEST_VECTORS);
1da177e4 977 break;
ef2736fc 978
1da177e4 979 case 9:
cba83564
HX
980 test_cipher("ecb(serpent)", ENCRYPT, serpent_enc_tv_template,
981 SERPENT_ENC_TEST_VECTORS);
982 test_cipher("ecb(serpent)", DECRYPT, serpent_dec_tv_template,
983 SERPENT_DEC_TEST_VECTORS);
1da177e4
LT
984 break;
985
986 case 10:
cba83564
HX
987 test_cipher("ecb(aes)", ENCRYPT, aes_enc_tv_template,
988 AES_ENC_TEST_VECTORS);
989 test_cipher("ecb(aes)", DECRYPT, aes_dec_tv_template,
990 AES_DEC_TEST_VECTORS);
991 test_cipher("cbc(aes)", ENCRYPT, aes_cbc_enc_tv_template,
992 AES_CBC_ENC_TEST_VECTORS);
993 test_cipher("cbc(aes)", DECRYPT, aes_cbc_dec_tv_template,
994 AES_CBC_DEC_TEST_VECTORS);
1da177e4
LT
995 break;
996
997 case 11:
998 test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
999 break;
ef2736fc 1000
1da177e4
LT
1001 case 12:
1002 test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
1003 break;
1004
1005 case 13:
1006 test_deflate();
1007 break;
1008
1009 case 14:
cba83564
HX
1010 test_cipher("ecb(cast5)", ENCRYPT, cast5_enc_tv_template,
1011 CAST5_ENC_TEST_VECTORS);
1012 test_cipher("ecb(cast5)", DECRYPT, cast5_dec_tv_template,
1013 CAST5_DEC_TEST_VECTORS);
1da177e4
LT
1014 break;
1015
1016 case 15:
cba83564
HX
1017 test_cipher("ecb(cast6)", ENCRYPT, cast6_enc_tv_template,
1018 CAST6_ENC_TEST_VECTORS);
1019 test_cipher("ecb(cast6)", DECRYPT, cast6_dec_tv_template,
1020 CAST6_DEC_TEST_VECTORS);
1da177e4
LT
1021 break;
1022
1023 case 16:
cba83564
HX
1024 test_cipher("ecb(arc4)", ENCRYPT, arc4_enc_tv_template,
1025 ARC4_ENC_TEST_VECTORS);
1026 test_cipher("ecb(arc4)", DECRYPT, arc4_dec_tv_template,
1027 ARC4_DEC_TEST_VECTORS);
1da177e4
LT
1028 break;
1029
1030 case 17:
1031 test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
1032 break;
1033
1034 case 18:
c907ee76 1035 test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
1da177e4
LT
1036 break;
1037
1038 case 19:
cba83564
HX
1039 test_cipher("ecb(tea)", ENCRYPT, tea_enc_tv_template,
1040 TEA_ENC_TEST_VECTORS);
1041 test_cipher("ecb(tea)", DECRYPT, tea_dec_tv_template,
1042 TEA_DEC_TEST_VECTORS);
1da177e4
LT
1043 break;
1044
1045 case 20:
cba83564
HX
1046 test_cipher("ecb(xtea)", ENCRYPT, xtea_enc_tv_template,
1047 XTEA_ENC_TEST_VECTORS);
1048 test_cipher("ecb(xtea)", DECRYPT, xtea_dec_tv_template,
1049 XTEA_DEC_TEST_VECTORS);
1da177e4
LT
1050 break;
1051
1052 case 21:
cba83564
HX
1053 test_cipher("ecb(khazad)", ENCRYPT, khazad_enc_tv_template,
1054 KHAZAD_ENC_TEST_VECTORS);
1055 test_cipher("ecb(khazad)", DECRYPT, khazad_dec_tv_template,
1056 KHAZAD_DEC_TEST_VECTORS);
1da177e4
LT
1057 break;
1058
1059 case 22:
1060 test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
1061 break;
1062
1063 case 23:
1064 test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
1065 break;
1066
1067 case 24:
1068 test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
1069 break;
1070
1071 case 25:
cba83564
HX
1072 test_cipher("ecb(tnepres)", ENCRYPT, tnepres_enc_tv_template,
1073 TNEPRES_ENC_TEST_VECTORS);
1074 test_cipher("ecb(tnepres)", DECRYPT, tnepres_dec_tv_template,
1075 TNEPRES_DEC_TEST_VECTORS);
1da177e4
LT
1076 break;
1077
1078 case 26:
cba83564
HX
1079 test_cipher("ecb(anubis)", ENCRYPT, anubis_enc_tv_template,
1080 ANUBIS_ENC_TEST_VECTORS);
1081 test_cipher("ecb(anubis)", DECRYPT, anubis_dec_tv_template,
1082 ANUBIS_DEC_TEST_VECTORS);
1083 test_cipher("cbc(anubis)", ENCRYPT, anubis_cbc_enc_tv_template,
1084 ANUBIS_CBC_ENC_TEST_VECTORS);
1085 test_cipher("cbc(anubis)", DECRYPT, anubis_cbc_dec_tv_template,
1086 ANUBIS_CBC_ENC_TEST_VECTORS);
1da177e4
LT
1087 break;
1088
1089 case 27:
1090 test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
1091 break;
1092
1093 case 28:
1094
1095 test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
1096 break;
1097
1098 case 29:
1099 test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
1100 break;
fb4f10ed
AG
1101
1102 case 30:
cba83564
HX
1103 test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
1104 XETA_ENC_TEST_VECTORS);
1105 test_cipher("ecb(xeta)", DECRYPT, xeta_dec_tv_template,
1106 XETA_DEC_TEST_VECTORS);
fb4f10ed 1107 break;
1da177e4
LT
1108
1109#ifdef CONFIG_CRYPTO_HMAC
1110 case 100:
1111 test_hmac("md5", hmac_md5_tv_template, HMAC_MD5_TEST_VECTORS);
1112 break;
ef2736fc 1113
1da177e4 1114 case 101:
ef2736fc 1115 test_hmac("sha1", hmac_sha1_tv_template, HMAC_SHA1_TEST_VECTORS);
1da177e4 1116 break;
ef2736fc 1117
1da177e4
LT
1118 case 102:
1119 test_hmac("sha256", hmac_sha256_tv_template, HMAC_SHA256_TEST_VECTORS);
1120 break;
1121
1122#endif
1123
ebfd9bcf 1124 case 200:
cba83564 1125 test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
dce907c0 1126 aes_speed_template);
cba83564 1127 test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
dce907c0 1128 aes_speed_template);
cba83564 1129 test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
dce907c0 1130 aes_speed_template);
cba83564 1131 test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
dce907c0 1132 aes_speed_template);
ebfd9bcf
HW
1133 break;
1134
1135 case 201:
cba83564 1136 test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
dce907c0
HX
1137 des3_ede_enc_tv_template,
1138 DES3_EDE_ENC_TEST_VECTORS,
1139 des3_ede_speed_template);
cba83564 1140 test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
dce907c0
HX
1141 des3_ede_dec_tv_template,
1142 DES3_EDE_DEC_TEST_VECTORS,
1143 des3_ede_speed_template);
cba83564 1144 test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
dce907c0
HX
1145 des3_ede_enc_tv_template,
1146 DES3_EDE_ENC_TEST_VECTORS,
1147 des3_ede_speed_template);
cba83564 1148 test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
dce907c0
HX
1149 des3_ede_dec_tv_template,
1150 DES3_EDE_DEC_TEST_VECTORS,
1151 des3_ede_speed_template);
ebfd9bcf
HW
1152 break;
1153
1154 case 202:
cba83564 1155 test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
dce907c0 1156 twofish_speed_template);
cba83564 1157 test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
dce907c0 1158 twofish_speed_template);
cba83564 1159 test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
dce907c0 1160 twofish_speed_template);
cba83564 1161 test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
dce907c0 1162 twofish_speed_template);
ebfd9bcf
HW
1163 break;
1164
1165 case 203:
cba83564 1166 test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
dce907c0 1167 blowfish_speed_template);
cba83564 1168 test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
dce907c0 1169 blowfish_speed_template);
cba83564 1170 test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
dce907c0 1171 blowfish_speed_template);
cba83564 1172 test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
dce907c0 1173 blowfish_speed_template);
ebfd9bcf
HW
1174 break;
1175
1176 case 204:
cba83564 1177 test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
dce907c0 1178 des_speed_template);
cba83564 1179 test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
dce907c0 1180 des_speed_template);
cba83564 1181 test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
dce907c0 1182 des_speed_template);
cba83564 1183 test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
dce907c0 1184 des_speed_template);
ebfd9bcf
HW
1185 break;
1186
e8057928
ML
1187 case 300:
1188 /* fall through */
1189
1190 case 301:
1191 test_digest_speed("md4", sec, generic_digest_speed_template);
1192 if (mode > 300 && mode < 400) break;
1193
1194 case 302:
1195 test_digest_speed("md5", sec, generic_digest_speed_template);
1196 if (mode > 300 && mode < 400) break;
1197
1198 case 303:
1199 test_digest_speed("sha1", sec, generic_digest_speed_template);
1200 if (mode > 300 && mode < 400) break;
1201
1202 case 304:
1203 test_digest_speed("sha256", sec, generic_digest_speed_template);
1204 if (mode > 300 && mode < 400) break;
1205
1206 case 305:
1207 test_digest_speed("sha384", sec, generic_digest_speed_template);
1208 if (mode > 300 && mode < 400) break;
1209
1210 case 306:
1211 test_digest_speed("sha512", sec, generic_digest_speed_template);
1212 if (mode > 300 && mode < 400) break;
1213
1214 case 307:
1215 test_digest_speed("wp256", sec, generic_digest_speed_template);
1216 if (mode > 300 && mode < 400) break;
1217
1218 case 308:
1219 test_digest_speed("wp384", sec, generic_digest_speed_template);
1220 if (mode > 300 && mode < 400) break;
1221
1222 case 309:
1223 test_digest_speed("wp512", sec, generic_digest_speed_template);
1224 if (mode > 300 && mode < 400) break;
1225
1226 case 310:
1227 test_digest_speed("tgr128", sec, generic_digest_speed_template);
1228 if (mode > 300 && mode < 400) break;
1229
1230 case 311:
1231 test_digest_speed("tgr160", sec, generic_digest_speed_template);
1232 if (mode > 300 && mode < 400) break;
1233
1234 case 312:
1235 test_digest_speed("tgr192", sec, generic_digest_speed_template);
1236 if (mode > 300 && mode < 400) break;
1237
1238 case 399:
1239 break;
1240
1da177e4
LT
1241 case 1000:
1242 test_available();
1243 break;
ef2736fc 1244
1da177e4
LT
1245 default:
1246 /* useful for debugging */
1247 printk("not testing anything\n");
1248 break;
1249 }
1250}
1251
ef2736fc 1252static int __init init(void)
1da177e4
LT
1253{
1254 tvmem = kmalloc(TVMEMSIZE, GFP_KERNEL);
1255 if (tvmem == NULL)
1256 return -ENOMEM;
1257
1258 xbuf = kmalloc(XBUFSIZE, GFP_KERNEL);
1259 if (xbuf == NULL) {
1260 kfree(tvmem);
1261 return -ENOMEM;
1262 }
1263
1264 do_test();
1265
1266 kfree(xbuf);
1267 kfree(tvmem);
14fdf477
ML
1268
1269 /* We intentionaly return -EAGAIN to prevent keeping
1270 * the module. It does all its work from init()
1271 * and doesn't offer any runtime functionality
1272 * => we don't need it in the memory, do we?
1273 * -- mludvig
1274 */
1275 return -EAGAIN;
1da177e4
LT
1276}
1277
1278/*
1279 * If an init function is provided, an exit function must also be provided
1280 * to allow module unload.
1281 */
1282static void __exit fini(void) { }
1283
1284module_init(init);
1285module_exit(fini);
1286
1287module_param(mode, int, 0);
ebfd9bcf 1288module_param(sec, uint, 0);
6a17944c
HX
1289MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
1290 "(defaults to zero which uses CPU cycles instead)");
1da177e4
LT
1291
1292MODULE_LICENSE("GPL");
1293MODULE_DESCRIPTION("Quick & dirty crypto testing module");
1294MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");