blob: 3ca68f9fc14d84029603640cc765ca70584a01ee [file] [log] [blame]
Herbert Xuef2736f2005-06-22 13:26:03 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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>
Mikko Herranene3a4ea42007-11-26 22:12:07 +08009 * Copyright (c) 2007 Nokia Siemens Networks
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
Herbert Xuef2736f2005-06-22 13:26:03 -070013 * Software Foundation; either version 2 of the License, or (at your option)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * any later version.
15 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Herbert Xu18e33e62008-07-10 16:01:22 +080018#include <crypto/hash.h>
Herbert Xucba83562006-08-13 08:26:09 +100019#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
David Hardeman378f0582005-09-17 17:55:31 +100023#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/moduleparam.h>
Harald Welteebfd9bc2005-06-22 13:27:23 -070026#include <linux/jiffies.h>
Herbert Xu6a179442005-06-22 13:29:03 -070027#include <linux/timex.h>
28#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "tcrypt.h"
Jarod Wilson4e033a62009-05-27 15:10:21 +100030#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/*
Herbert Xuf139cfa2008-07-31 12:23:53 +080033 * Need slab memory for testing (size in number of pages).
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
Herbert Xuf139cfa2008-07-31 12:23:53 +080035#define TVMEMSIZE 4
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/*
Herbert Xuda7f0332008-07-31 17:08:25 +080038* Used by test_cipher_speed()
Linus Torvalds1da177e2005-04-16 15:20:36 -070039*/
40#define ENCRYPT 1
41#define DECRYPT 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Harald Welteebfd9bc2005-06-22 13:27:23 -070043/*
44 * Used by test_cipher_speed()
45 */
Herbert Xu6a179442005-06-22 13:29:03 -070046static unsigned int sec;
Harald Welteebfd9bc2005-06-22 13:27:23 -070047
Steffen Klasserta873a5f2009-06-19 19:46:53 +080048static char *alg = NULL;
49static u32 type;
Herbert Xu7be380f2009-07-14 16:06:54 +080050static u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static int mode;
Herbert Xuf139cfa2008-07-31 12:23:53 +080052static char *tvmem[TVMEMSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54static char *check[] = {
Jonathan Lynchcd12fb92007-11-10 20:08:25 +080055 "des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
56 "blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
57 "cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
David Howells90831632006-12-16 12:13:14 +110058 "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", "fcrypt",
Adrian-Ken Rueegsegger2998db32008-05-09 21:29:35 +080059 "camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
Geert Uytterhoeven0c01aed2009-03-04 15:42:15 +080060 "lzo", "cts", "zlib", NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
Herbert Xuf139cfa2008-07-31 12:23:53 +080063static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc,
64 struct scatterlist *sg, int blen, int sec)
Herbert Xu6a179442005-06-22 13:29:03 -070065{
Herbert Xu6a179442005-06-22 13:29:03 -070066 unsigned long start, end;
67 int bcount;
68 int ret;
69
Herbert Xu6a179442005-06-22 13:29:03 -070070 for (start = jiffies, end = start + sec * HZ, bcount = 0;
71 time_before(jiffies, end); bcount++) {
72 if (enc)
Herbert Xucba83562006-08-13 08:26:09 +100073 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
Herbert Xu6a179442005-06-22 13:29:03 -070074 else
Herbert Xucba83562006-08-13 08:26:09 +100075 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
Herbert Xu6a179442005-06-22 13:29:03 -070076
77 if (ret)
78 return ret;
79 }
80
81 printk("%d operations in %d seconds (%ld bytes)\n",
82 bcount, sec, (long)bcount * blen);
83 return 0;
84}
85
Herbert Xuf139cfa2008-07-31 12:23:53 +080086static int test_cipher_cycles(struct blkcipher_desc *desc, int enc,
87 struct scatterlist *sg, int blen)
Herbert Xu6a179442005-06-22 13:29:03 -070088{
Herbert Xu6a179442005-06-22 13:29:03 -070089 unsigned long cycles = 0;
90 int ret = 0;
91 int i;
92
Herbert Xu6a179442005-06-22 13:29:03 -070093 local_bh_disable();
94 local_irq_disable();
95
96 /* Warm-up run. */
97 for (i = 0; i < 4; i++) {
98 if (enc)
Herbert Xucba83562006-08-13 08:26:09 +100099 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700100 else
Herbert Xucba83562006-08-13 08:26:09 +1000101 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700102
103 if (ret)
104 goto out;
105 }
106
107 /* The real thing. */
108 for (i = 0; i < 8; i++) {
109 cycles_t start, end;
110
111 start = get_cycles();
112 if (enc)
Herbert Xucba83562006-08-13 08:26:09 +1000113 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700114 else
Herbert Xucba83562006-08-13 08:26:09 +1000115 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700116 end = get_cycles();
117
118 if (ret)
119 goto out;
120
121 cycles += end - start;
122 }
123
124out:
125 local_irq_enable();
126 local_bh_enable();
127
128 if (ret == 0)
129 printk("1 operation in %lu cycles (%d bytes)\n",
130 (cycles + 4) / 8, blen);
131
132 return ret;
133}
134
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800135static u32 block_sizes[] = { 16, 64, 256, 1024, 8192, 0 };
136
Herbert Xu01b32322008-07-31 15:41:55 +0800137static void test_cipher_speed(const char *algo, int enc, unsigned int sec,
Herbert Xuda7f0332008-07-31 17:08:25 +0800138 struct cipher_speed_template *template,
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800139 unsigned int tcount, u8 *keysize)
Harald Welteebfd9bc2005-06-22 13:27:23 -0700140{
Herbert Xudce907c2005-06-22 13:27:51 -0700141 unsigned int ret, i, j, iv_len;
Herbert Xuda7f0332008-07-31 17:08:25 +0800142 const char *key, iv[128];
Herbert Xucba83562006-08-13 08:26:09 +1000143 struct crypto_blkcipher *tfm;
144 struct blkcipher_desc desc;
145 const char *e;
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800146 u32 *b_size;
Harald Welteebfd9bc2005-06-22 13:27:23 -0700147
148 if (enc == ENCRYPT)
149 e = "encryption";
150 else
151 e = "decryption";
Harald Welteebfd9bc2005-06-22 13:27:23 -0700152
Herbert Xucba83562006-08-13 08:26:09 +1000153 printk("\ntesting speed of %s %s\n", algo, e);
Harald Welteebfd9bc2005-06-22 13:27:23 -0700154
Herbert Xucba83562006-08-13 08:26:09 +1000155 tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
Harald Welteebfd9bc2005-06-22 13:27:23 -0700156
Herbert Xucba83562006-08-13 08:26:09 +1000157 if (IS_ERR(tfm)) {
158 printk("failed to load transform for %s: %ld\n", algo,
159 PTR_ERR(tfm));
Harald Welteebfd9bc2005-06-22 13:27:23 -0700160 return;
161 }
Herbert Xucba83562006-08-13 08:26:09 +1000162 desc.tfm = tfm;
163 desc.flags = 0;
Harald Welteebfd9bc2005-06-22 13:27:23 -0700164
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800165 i = 0;
166 do {
Harald Welteebfd9bc2005-06-22 13:27:23 -0700167
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800168 b_size = block_sizes;
169 do {
Herbert Xuf139cfa2008-07-31 12:23:53 +0800170 struct scatterlist sg[TVMEMSIZE];
Harald Welteebfd9bc2005-06-22 13:27:23 -0700171
Herbert Xuf139cfa2008-07-31 12:23:53 +0800172 if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
173 printk("template (%u) too big for "
174 "tvmem (%lu)\n", *keysize + *b_size,
175 TVMEMSIZE * PAGE_SIZE);
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800176 goto out;
177 }
Harald Welteebfd9bc2005-06-22 13:27:23 -0700178
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800179 printk("test %u (%d bit key, %d byte blocks): ", i,
180 *keysize * 8, *b_size);
181
Herbert Xuf139cfa2008-07-31 12:23:53 +0800182 memset(tvmem[0], 0xff, PAGE_SIZE);
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800183
184 /* set key, plain text and IV */
Herbert Xuda7f0332008-07-31 17:08:25 +0800185 key = tvmem[0];
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800186 for (j = 0; j < tcount; j++) {
187 if (template[j].klen == *keysize) {
188 key = template[j].key;
189 break;
190 }
191 }
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800192
193 ret = crypto_blkcipher_setkey(tfm, key, *keysize);
194 if (ret) {
195 printk("setkey() failed flags=%x\n",
196 crypto_blkcipher_get_flags(tfm));
197 goto out;
198 }
199
Herbert Xuf139cfa2008-07-31 12:23:53 +0800200 sg_init_table(sg, TVMEMSIZE);
201 sg_set_buf(sg, tvmem[0] + *keysize,
202 PAGE_SIZE - *keysize);
203 for (j = 1; j < TVMEMSIZE; j++) {
204 sg_set_buf(sg + j, tvmem[j], PAGE_SIZE);
205 memset (tvmem[j], 0xff, PAGE_SIZE);
206 }
207
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800208 iv_len = crypto_blkcipher_ivsize(tfm);
209 if (iv_len) {
210 memset(&iv, 0xff, iv_len);
211 crypto_blkcipher_set_iv(tfm, iv, iv_len);
212 }
213
214 if (sec)
Herbert Xuf139cfa2008-07-31 12:23:53 +0800215 ret = test_cipher_jiffies(&desc, enc, sg,
216 *b_size, sec);
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800217 else
Herbert Xuf139cfa2008-07-31 12:23:53 +0800218 ret = test_cipher_cycles(&desc, enc, sg,
219 *b_size);
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800220
221 if (ret) {
222 printk("%s() failed flags=%x\n", e, desc.flags);
Herbert Xudce907c2005-06-22 13:27:51 -0700223 break;
224 }
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800225 b_size++;
226 i++;
227 } while (*b_size);
228 keysize++;
229 } while (*keysize);
Harald Welteebfd9bc2005-06-22 13:27:23 -0700230
231out:
Herbert Xucba83562006-08-13 08:26:09 +1000232 crypto_free_blkcipher(tfm);
Harald Welteebfd9bc2005-06-22 13:27:23 -0700233}
234
Herbert Xuf139cfa2008-07-31 12:23:53 +0800235static int test_hash_jiffies_digest(struct hash_desc *desc,
236 struct scatterlist *sg, int blen,
Herbert Xue9d41162006-08-19 21:38:49 +1000237 char *out, int sec)
Michal Ludvige8057922006-05-30 22:04:19 +1000238{
Michal Ludvige8057922006-05-30 22:04:19 +1000239 unsigned long start, end;
Herbert Xue9d41162006-08-19 21:38:49 +1000240 int bcount;
241 int ret;
Michal Ludvige8057922006-05-30 22:04:19 +1000242
243 for (start = jiffies, end = start + sec * HZ, bcount = 0;
244 time_before(jiffies, end); bcount++) {
Herbert Xue9d41162006-08-19 21:38:49 +1000245 ret = crypto_hash_digest(desc, sg, blen, out);
246 if (ret)
247 return ret;
Michal Ludvige8057922006-05-30 22:04:19 +1000248 }
249
250 printk("%6u opers/sec, %9lu bytes/sec\n",
251 bcount / sec, ((long)bcount * blen) / sec);
252
Herbert Xue9d41162006-08-19 21:38:49 +1000253 return 0;
Michal Ludvige8057922006-05-30 22:04:19 +1000254}
255
Herbert Xuf139cfa2008-07-31 12:23:53 +0800256static int test_hash_jiffies(struct hash_desc *desc, struct scatterlist *sg,
257 int blen, int plen, char *out, int sec)
Herbert Xue9d41162006-08-19 21:38:49 +1000258{
Herbert Xue9d41162006-08-19 21:38:49 +1000259 unsigned long start, end;
260 int bcount, pcount;
261 int ret;
262
263 if (plen == blen)
Herbert Xuf139cfa2008-07-31 12:23:53 +0800264 return test_hash_jiffies_digest(desc, sg, blen, out, sec);
Herbert Xua5a613a2007-10-27 00:51:21 -0700265
Herbert Xue9d41162006-08-19 21:38:49 +1000266 for (start = jiffies, end = start + sec * HZ, bcount = 0;
267 time_before(jiffies, end); bcount++) {
268 ret = crypto_hash_init(desc);
269 if (ret)
270 return ret;
271 for (pcount = 0; pcount < blen; pcount += plen) {
Herbert Xue9d41162006-08-19 21:38:49 +1000272 ret = crypto_hash_update(desc, sg, plen);
273 if (ret)
274 return ret;
275 }
276 /* we assume there is enough space in 'out' for the result */
277 ret = crypto_hash_final(desc, out);
278 if (ret)
279 return ret;
280 }
281
282 printk("%6u opers/sec, %9lu bytes/sec\n",
283 bcount / sec, ((long)bcount * blen) / sec);
284
285 return 0;
286}
287
Herbert Xuf139cfa2008-07-31 12:23:53 +0800288static int test_hash_cycles_digest(struct hash_desc *desc,
289 struct scatterlist *sg, int blen, char *out)
Michal Ludvige8057922006-05-30 22:04:19 +1000290{
Michal Ludvige8057922006-05-30 22:04:19 +1000291 unsigned long cycles = 0;
Herbert Xue9d41162006-08-19 21:38:49 +1000292 int i;
293 int ret;
Michal Ludvige8057922006-05-30 22:04:19 +1000294
295 local_bh_disable();
296 local_irq_disable();
297
298 /* Warm-up run. */
299 for (i = 0; i < 4; i++) {
Herbert Xue9d41162006-08-19 21:38:49 +1000300 ret = crypto_hash_digest(desc, sg, blen, out);
301 if (ret)
302 goto out;
Michal Ludvige8057922006-05-30 22:04:19 +1000303 }
304
305 /* The real thing. */
306 for (i = 0; i < 8; i++) {
307 cycles_t start, end;
308
Michal Ludvige8057922006-05-30 22:04:19 +1000309 start = get_cycles();
310
Herbert Xue9d41162006-08-19 21:38:49 +1000311 ret = crypto_hash_digest(desc, sg, blen, out);
312 if (ret)
313 goto out;
Michal Ludvige8057922006-05-30 22:04:19 +1000314
315 end = get_cycles();
316
317 cycles += end - start;
318 }
319
Herbert Xue9d41162006-08-19 21:38:49 +1000320out:
Michal Ludvige8057922006-05-30 22:04:19 +1000321 local_irq_enable();
322 local_bh_enable();
323
Herbert Xue9d41162006-08-19 21:38:49 +1000324 if (ret)
325 return ret;
326
Michal Ludvige8057922006-05-30 22:04:19 +1000327 printk("%6lu cycles/operation, %4lu cycles/byte\n",
328 cycles / 8, cycles / (8 * blen));
329
Herbert Xue9d41162006-08-19 21:38:49 +1000330 return 0;
Michal Ludvige8057922006-05-30 22:04:19 +1000331}
332
Herbert Xuf139cfa2008-07-31 12:23:53 +0800333static int test_hash_cycles(struct hash_desc *desc, struct scatterlist *sg,
334 int blen, int plen, char *out)
Michal Ludvige8057922006-05-30 22:04:19 +1000335{
Herbert Xue9d41162006-08-19 21:38:49 +1000336 unsigned long cycles = 0;
337 int i, pcount;
338 int ret;
339
340 if (plen == blen)
Herbert Xuf139cfa2008-07-31 12:23:53 +0800341 return test_hash_cycles_digest(desc, sg, blen, out);
Herbert Xua5a613a2007-10-27 00:51:21 -0700342
Herbert Xue9d41162006-08-19 21:38:49 +1000343 local_bh_disable();
344 local_irq_disable();
345
346 /* Warm-up run. */
347 for (i = 0; i < 4; i++) {
348 ret = crypto_hash_init(desc);
349 if (ret)
350 goto out;
351 for (pcount = 0; pcount < blen; pcount += plen) {
Herbert Xue9d41162006-08-19 21:38:49 +1000352 ret = crypto_hash_update(desc, sg, plen);
353 if (ret)
354 goto out;
355 }
Herbert Xu29059d12007-05-18 16:25:19 +1000356 ret = crypto_hash_final(desc, out);
Herbert Xue9d41162006-08-19 21:38:49 +1000357 if (ret)
358 goto out;
359 }
360
361 /* The real thing. */
362 for (i = 0; i < 8; i++) {
363 cycles_t start, end;
364
365 start = get_cycles();
366
367 ret = crypto_hash_init(desc);
368 if (ret)
369 goto out;
370 for (pcount = 0; pcount < blen; pcount += plen) {
Herbert Xue9d41162006-08-19 21:38:49 +1000371 ret = crypto_hash_update(desc, sg, plen);
372 if (ret)
373 goto out;
374 }
375 ret = crypto_hash_final(desc, out);
376 if (ret)
377 goto out;
378
379 end = get_cycles();
380
381 cycles += end - start;
382 }
383
384out:
385 local_irq_enable();
386 local_bh_enable();
387
388 if (ret)
389 return ret;
390
391 printk("%6lu cycles/operation, %4lu cycles/byte\n",
392 cycles / 8, cycles / (8 * blen));
393
394 return 0;
395}
396
David S. Millerbeb63da2010-05-19 14:11:21 +1000397static void test_hash_sg_init(struct scatterlist *sg)
398{
399 int i;
400
401 sg_init_table(sg, TVMEMSIZE);
402 for (i = 0; i < TVMEMSIZE; i++) {
403 sg_set_buf(sg + i, tvmem[i], PAGE_SIZE);
404 memset(tvmem[i], 0xff, PAGE_SIZE);
405 }
406}
407
Herbert Xu01b32322008-07-31 15:41:55 +0800408static void test_hash_speed(const char *algo, unsigned int sec,
409 struct hash_speed *speed)
Herbert Xue9d41162006-08-19 21:38:49 +1000410{
Herbert Xuf139cfa2008-07-31 12:23:53 +0800411 struct scatterlist sg[TVMEMSIZE];
Herbert Xue9d41162006-08-19 21:38:49 +1000412 struct crypto_hash *tfm;
413 struct hash_desc desc;
Frank Seidel376bacb2009-03-29 15:18:39 +0800414 static char output[1024];
Michal Ludvige8057922006-05-30 22:04:19 +1000415 int i;
Herbert Xue9d41162006-08-19 21:38:49 +1000416 int ret;
Michal Ludvige8057922006-05-30 22:04:19 +1000417
Frank Seidel376bacb2009-03-29 15:18:39 +0800418 printk(KERN_INFO "\ntesting speed of %s\n", algo);
Michal Ludvige8057922006-05-30 22:04:19 +1000419
Herbert Xue9d41162006-08-19 21:38:49 +1000420 tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
Michal Ludvige8057922006-05-30 22:04:19 +1000421
Herbert Xue9d41162006-08-19 21:38:49 +1000422 if (IS_ERR(tfm)) {
Frank Seidel376bacb2009-03-29 15:18:39 +0800423 printk(KERN_ERR "failed to load transform for %s: %ld\n", algo,
Herbert Xue9d41162006-08-19 21:38:49 +1000424 PTR_ERR(tfm));
Michal Ludvige8057922006-05-30 22:04:19 +1000425 return;
426 }
427
Herbert Xue9d41162006-08-19 21:38:49 +1000428 desc.tfm = tfm;
429 desc.flags = 0;
430
431 if (crypto_hash_digestsize(tfm) > sizeof(output)) {
Frank Seidel376bacb2009-03-29 15:18:39 +0800432 printk(KERN_ERR "digestsize(%u) > outputbuffer(%zu)\n",
Herbert Xue9d41162006-08-19 21:38:49 +1000433 crypto_hash_digestsize(tfm), sizeof(output));
Michal Ludvige8057922006-05-30 22:04:19 +1000434 goto out;
435 }
436
David S. Millerbeb63da2010-05-19 14:11:21 +1000437 test_hash_sg_init(sg);
Michal Ludvige8057922006-05-30 22:04:19 +1000438 for (i = 0; speed[i].blen != 0; i++) {
Herbert Xuf139cfa2008-07-31 12:23:53 +0800439 if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
Frank Seidel376bacb2009-03-29 15:18:39 +0800440 printk(KERN_ERR
441 "template (%u) too big for tvmem (%lu)\n",
Herbert Xuf139cfa2008-07-31 12:23:53 +0800442 speed[i].blen, TVMEMSIZE * PAGE_SIZE);
Michal Ludvige8057922006-05-30 22:04:19 +1000443 goto out;
444 }
445
Huang Ying18bcc912010-03-10 18:30:32 +0800446 if (speed[i].klen)
447 crypto_hash_setkey(tfm, tvmem[0], speed[i].klen);
448
Frank Seidel376bacb2009-03-29 15:18:39 +0800449 printk(KERN_INFO "test%3u "
450 "(%5u byte blocks,%5u bytes per update,%4u updates): ",
Michal Ludvige8057922006-05-30 22:04:19 +1000451 i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
452
Michal Ludvige8057922006-05-30 22:04:19 +1000453 if (sec)
Herbert Xuf139cfa2008-07-31 12:23:53 +0800454 ret = test_hash_jiffies(&desc, sg, speed[i].blen,
Herbert Xue9d41162006-08-19 21:38:49 +1000455 speed[i].plen, output, sec);
Michal Ludvige8057922006-05-30 22:04:19 +1000456 else
Herbert Xuf139cfa2008-07-31 12:23:53 +0800457 ret = test_hash_cycles(&desc, sg, speed[i].blen,
Herbert Xue9d41162006-08-19 21:38:49 +1000458 speed[i].plen, output);
459
460 if (ret) {
Frank Seidel376bacb2009-03-29 15:18:39 +0800461 printk(KERN_ERR "hashing failed ret=%d\n", ret);
Herbert Xue9d41162006-08-19 21:38:49 +1000462 break;
463 }
Michal Ludvige8057922006-05-30 22:04:19 +1000464 }
465
466out:
Herbert Xue9d41162006-08-19 21:38:49 +1000467 crypto_free_hash(tfm);
Michal Ludvige8057922006-05-30 22:04:19 +1000468}
469
David S. Millerbeb63da2010-05-19 14:11:21 +1000470struct tcrypt_result {
471 struct completion completion;
472 int err;
473};
474
475static void tcrypt_complete(struct crypto_async_request *req, int err)
476{
477 struct tcrypt_result *res = req->data;
478
479 if (err == -EINPROGRESS)
480 return;
481
482 res->err = err;
483 complete(&res->completion);
484}
485
486static inline int do_one_ahash_op(struct ahash_request *req, int ret)
487{
488 if (ret == -EINPROGRESS || ret == -EBUSY) {
489 struct tcrypt_result *tr = req->base.data;
490
491 ret = wait_for_completion_interruptible(&tr->completion);
492 if (!ret)
493 ret = tr->err;
494 INIT_COMPLETION(tr->completion);
495 }
496 return ret;
497}
498
499static int test_ahash_jiffies_digest(struct ahash_request *req, int blen,
500 char *out, int sec)
501{
502 unsigned long start, end;
503 int bcount;
504 int ret;
505
506 for (start = jiffies, end = start + sec * HZ, bcount = 0;
507 time_before(jiffies, end); bcount++) {
508 ret = do_one_ahash_op(req, crypto_ahash_digest(req));
509 if (ret)
510 return ret;
511 }
512
513 printk("%6u opers/sec, %9lu bytes/sec\n",
514 bcount / sec, ((long)bcount * blen) / sec);
515
516 return 0;
517}
518
519static int test_ahash_jiffies(struct ahash_request *req, int blen,
520 int plen, char *out, int sec)
521{
522 unsigned long start, end;
523 int bcount, pcount;
524 int ret;
525
526 if (plen == blen)
527 return test_ahash_jiffies_digest(req, blen, out, sec);
528
529 for (start = jiffies, end = start + sec * HZ, bcount = 0;
530 time_before(jiffies, end); bcount++) {
531 ret = crypto_ahash_init(req);
532 if (ret)
533 return ret;
534 for (pcount = 0; pcount < blen; pcount += plen) {
535 ret = do_one_ahash_op(req, crypto_ahash_update(req));
536 if (ret)
537 return ret;
538 }
539 /* we assume there is enough space in 'out' for the result */
540 ret = do_one_ahash_op(req, crypto_ahash_final(req));
541 if (ret)
542 return ret;
543 }
544
545 pr_cont("%6u opers/sec, %9lu bytes/sec\n",
546 bcount / sec, ((long)bcount * blen) / sec);
547
548 return 0;
549}
550
551static int test_ahash_cycles_digest(struct ahash_request *req, int blen,
552 char *out)
553{
554 unsigned long cycles = 0;
555 int ret, i;
556
557 /* Warm-up run. */
558 for (i = 0; i < 4; i++) {
559 ret = do_one_ahash_op(req, crypto_ahash_digest(req));
560 if (ret)
561 goto out;
562 }
563
564 /* The real thing. */
565 for (i = 0; i < 8; i++) {
566 cycles_t start, end;
567
568 start = get_cycles();
569
570 ret = do_one_ahash_op(req, crypto_ahash_digest(req));
571 if (ret)
572 goto out;
573
574 end = get_cycles();
575
576 cycles += end - start;
577 }
578
579out:
580 if (ret)
581 return ret;
582
583 pr_cont("%6lu cycles/operation, %4lu cycles/byte\n",
584 cycles / 8, cycles / (8 * blen));
585
586 return 0;
587}
588
589static int test_ahash_cycles(struct ahash_request *req, int blen,
590 int plen, char *out)
591{
592 unsigned long cycles = 0;
593 int i, pcount, ret;
594
595 if (plen == blen)
596 return test_ahash_cycles_digest(req, blen, out);
597
598 /* Warm-up run. */
599 for (i = 0; i < 4; i++) {
600 ret = crypto_ahash_init(req);
601 if (ret)
602 goto out;
603 for (pcount = 0; pcount < blen; pcount += plen) {
604 ret = do_one_ahash_op(req, crypto_ahash_update(req));
605 if (ret)
606 goto out;
607 }
608 ret = do_one_ahash_op(req, crypto_ahash_final(req));
609 if (ret)
610 goto out;
611 }
612
613 /* The real thing. */
614 for (i = 0; i < 8; i++) {
615 cycles_t start, end;
616
617 start = get_cycles();
618
619 ret = crypto_ahash_init(req);
620 if (ret)
621 goto out;
622 for (pcount = 0; pcount < blen; pcount += plen) {
623 ret = do_one_ahash_op(req, crypto_ahash_update(req));
624 if (ret)
625 goto out;
626 }
627 ret = do_one_ahash_op(req, crypto_ahash_final(req));
628 if (ret)
629 goto out;
630
631 end = get_cycles();
632
633 cycles += end - start;
634 }
635
636out:
637 if (ret)
638 return ret;
639
640 pr_cont("%6lu cycles/operation, %4lu cycles/byte\n",
641 cycles / 8, cycles / (8 * blen));
642
643 return 0;
644}
645
646static void test_ahash_speed(const char *algo, unsigned int sec,
647 struct hash_speed *speed)
648{
649 struct scatterlist sg[TVMEMSIZE];
650 struct tcrypt_result tresult;
651 struct ahash_request *req;
652 struct crypto_ahash *tfm;
653 static char output[1024];
654 int i, ret;
655
656 printk(KERN_INFO "\ntesting speed of async %s\n", algo);
657
658 tfm = crypto_alloc_ahash(algo, 0, 0);
659 if (IS_ERR(tfm)) {
660 pr_err("failed to load transform for %s: %ld\n",
661 algo, PTR_ERR(tfm));
662 return;
663 }
664
665 if (crypto_ahash_digestsize(tfm) > sizeof(output)) {
666 pr_err("digestsize(%u) > outputbuffer(%zu)\n",
667 crypto_ahash_digestsize(tfm), sizeof(output));
668 goto out;
669 }
670
671 test_hash_sg_init(sg);
672 req = ahash_request_alloc(tfm, GFP_KERNEL);
673 if (!req) {
674 pr_err("ahash request allocation failure\n");
675 goto out;
676 }
677
678 init_completion(&tresult.completion);
679 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
680 tcrypt_complete, &tresult);
681
682 for (i = 0; speed[i].blen != 0; i++) {
683 if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
684 pr_err("template (%u) too big for tvmem (%lu)\n",
685 speed[i].blen, TVMEMSIZE * PAGE_SIZE);
686 break;
687 }
688
689 pr_info("test%3u "
690 "(%5u byte blocks,%5u bytes per update,%4u updates): ",
691 i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
692
693 ahash_request_set_crypt(req, sg, output, speed[i].plen);
694
695 if (sec)
696 ret = test_ahash_jiffies(req, speed[i].blen,
697 speed[i].plen, output, sec);
698 else
699 ret = test_ahash_cycles(req, speed[i].blen,
700 speed[i].plen, output);
701
702 if (ret) {
703 pr_err("hashing failed ret=%d\n", ret);
704 break;
705 }
706 }
707
708 ahash_request_free(req);
709
710out:
711 crypto_free_ahash(tfm);
712}
713
Herbert Xuef2736f2005-06-22 13:26:03 -0700714static void test_available(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 char **name = check;
Herbert Xuef2736f2005-06-22 13:26:03 -0700717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 while (*name) {
719 printk("alg %s ", *name);
Herbert Xu6158efc2007-04-04 17:41:07 +1000720 printk(crypto_has_alg(*name, 0, 0) ?
Herbert Xue4d5b792006-08-26 18:12:40 +1000721 "found\n" : "not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 name++;
Herbert Xuef2736f2005-06-22 13:26:03 -0700723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
Herbert Xu01b32322008-07-31 15:41:55 +0800726static inline int tcrypt_test(const char *alg)
727{
Jarod Wilson4e033a62009-05-27 15:10:21 +1000728 int ret;
729
730 ret = alg_test(alg, alg, 0, 0);
731 /* non-fips algs return -EINVAL in fips mode */
732 if (fips_enabled && ret == -EINVAL)
733 ret = 0;
734 return ret;
Herbert Xu01b32322008-07-31 15:41:55 +0800735}
736
Jarod Wilson4e033a62009-05-27 15:10:21 +1000737static int do_test(int m)
Herbert Xu01b32322008-07-31 15:41:55 +0800738{
739 int i;
Jarod Wilson4e033a62009-05-27 15:10:21 +1000740 int ret = 0;
Herbert Xu01b32322008-07-31 15:41:55 +0800741
742 switch (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 case 0:
Herbert Xu01b32322008-07-31 15:41:55 +0800744 for (i = 1; i < 200; i++)
Jarod Wilson4e033a62009-05-27 15:10:21 +1000745 ret += do_test(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 break;
747
748 case 1:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000749 ret += tcrypt_test("md5");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 break;
751
752 case 2:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000753 ret += tcrypt_test("sha1");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 break;
755
756 case 3:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000757 ret += tcrypt_test("ecb(des)");
758 ret += tcrypt_test("cbc(des)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 break;
760
761 case 4:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000762 ret += tcrypt_test("ecb(des3_ede)");
763 ret += tcrypt_test("cbc(des3_ede)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 break;
765
766 case 5:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000767 ret += tcrypt_test("md4");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 break;
Herbert Xuef2736f2005-06-22 13:26:03 -0700769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 case 6:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000771 ret += tcrypt_test("sha256");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 break;
Herbert Xuef2736f2005-06-22 13:26:03 -0700773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 case 7:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000775 ret += tcrypt_test("ecb(blowfish)");
776 ret += tcrypt_test("cbc(blowfish)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 break;
778
779 case 8:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000780 ret += tcrypt_test("ecb(twofish)");
781 ret += tcrypt_test("cbc(twofish)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 break;
Herbert Xuef2736f2005-06-22 13:26:03 -0700783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 case 9:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000785 ret += tcrypt_test("ecb(serpent)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 break;
787
788 case 10:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000789 ret += tcrypt_test("ecb(aes)");
790 ret += tcrypt_test("cbc(aes)");
791 ret += tcrypt_test("lrw(aes)");
792 ret += tcrypt_test("xts(aes)");
793 ret += tcrypt_test("ctr(aes)");
794 ret += tcrypt_test("rfc3686(ctr(aes))");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 break;
796
797 case 11:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000798 ret += tcrypt_test("sha384");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 break;
Herbert Xuef2736f2005-06-22 13:26:03 -0700800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 case 12:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000802 ret += tcrypt_test("sha512");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 break;
804
805 case 13:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000806 ret += tcrypt_test("deflate");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 break;
808
809 case 14:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000810 ret += tcrypt_test("ecb(cast5)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 break;
812
813 case 15:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000814 ret += tcrypt_test("ecb(cast6)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 break;
816
817 case 16:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000818 ret += tcrypt_test("ecb(arc4)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 break;
820
821 case 17:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000822 ret += tcrypt_test("michael_mic");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 break;
824
825 case 18:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000826 ret += tcrypt_test("crc32c");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 break;
828
829 case 19:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000830 ret += tcrypt_test("ecb(tea)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 break;
832
833 case 20:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000834 ret += tcrypt_test("ecb(xtea)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 break;
836
837 case 21:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000838 ret += tcrypt_test("ecb(khazad)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 break;
840
841 case 22:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000842 ret += tcrypt_test("wp512");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 break;
844
845 case 23:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000846 ret += tcrypt_test("wp384");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 break;
848
849 case 24:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000850 ret += tcrypt_test("wp256");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 break;
852
853 case 25:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000854 ret += tcrypt_test("ecb(tnepres)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 break;
856
857 case 26:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000858 ret += tcrypt_test("ecb(anubis)");
859 ret += tcrypt_test("cbc(anubis)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 break;
861
862 case 27:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000863 ret += tcrypt_test("tgr192");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 break;
865
866 case 28:
867
Jarod Wilson4e033a62009-05-27 15:10:21 +1000868 ret += tcrypt_test("tgr160");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 break;
870
871 case 29:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000872 ret += tcrypt_test("tgr128");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 break;
Adrian-Ken Rueegsegger2998db32008-05-09 21:29:35 +0800874
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700875 case 30:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000876 ret += tcrypt_test("ecb(xeta)");
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700877 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
David Howells90831632006-12-16 12:13:14 +1100879 case 31:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000880 ret += tcrypt_test("pcbc(fcrypt)");
David Howells90831632006-12-16 12:13:14 +1100881 break;
882
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +1100883 case 32:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000884 ret += tcrypt_test("ecb(camellia)");
885 ret += tcrypt_test("cbc(camellia)");
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +1100886 break;
Jonathan Lynchcd12fb92007-11-10 20:08:25 +0800887 case 33:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000888 ret += tcrypt_test("sha224");
Jonathan Lynchcd12fb92007-11-10 20:08:25 +0800889 break;
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +1100890
Tan Swee Heng2407d602007-11-23 19:45:00 +0800891 case 34:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000892 ret += tcrypt_test("salsa20");
Tan Swee Heng2407d602007-11-23 19:45:00 +0800893 break;
894
Herbert Xu8df213d2007-12-02 14:55:47 +1100895 case 35:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000896 ret += tcrypt_test("gcm(aes)");
Herbert Xu8df213d2007-12-02 14:55:47 +1100897 break;
898
Zoltan Sogor0b77abb2007-12-07 16:53:23 +0800899 case 36:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000900 ret += tcrypt_test("lzo");
Zoltan Sogor0b77abb2007-12-07 16:53:23 +0800901 break;
902
Joy Latten93cc74e2007-12-12 20:24:22 +0800903 case 37:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000904 ret += tcrypt_test("ccm(aes)");
Joy Latten93cc74e2007-12-12 20:24:22 +0800905 break;
906
Kevin Coffman76cb9522008-03-24 21:26:16 +0800907 case 38:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000908 ret += tcrypt_test("cts(cbc(aes))");
Kevin Coffman76cb9522008-03-24 21:26:16 +0800909 break;
910
Adrian-Ken Rueegseggerfd4adf12008-05-07 22:16:36 +0800911 case 39:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000912 ret += tcrypt_test("rmd128");
Adrian-Ken Rueegseggerfd4adf12008-05-07 22:16:36 +0800913 break;
914
915 case 40:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000916 ret += tcrypt_test("rmd160");
Adrian-Ken Rueegseggerfd4adf12008-05-07 22:16:36 +0800917 break;
918
Adrian-Ken Rueegsegger2998db32008-05-09 21:29:35 +0800919 case 41:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000920 ret += tcrypt_test("rmd256");
Adrian-Ken Rueegsegger2998db32008-05-09 21:29:35 +0800921 break;
922
923 case 42:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000924 ret += tcrypt_test("rmd320");
Herbert Xu01b32322008-07-31 15:41:55 +0800925 break;
926
927 case 43:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000928 ret += tcrypt_test("ecb(seed)");
Adrian-Ken Rueegsegger2998db32008-05-09 21:29:35 +0800929 break;
930
Geert Uytterhoeven0c01aed2009-03-04 15:42:15 +0800931 case 44:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000932 ret += tcrypt_test("zlib");
Geert Uytterhoeven0c01aed2009-03-04 15:42:15 +0800933 break;
934
Jarod Wilson5d667322009-05-04 19:23:40 +0800935 case 45:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000936 ret += tcrypt_test("rfc4309(ccm(aes))");
Jarod Wilson5d667322009-05-04 19:23:40 +0800937 break;
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 case 100:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000940 ret += tcrypt_test("hmac(md5)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 break;
Herbert Xuef2736f2005-06-22 13:26:03 -0700942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 case 101:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000944 ret += tcrypt_test("hmac(sha1)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 break;
Herbert Xuef2736f2005-06-22 13:26:03 -0700946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 case 102:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000948 ret += tcrypt_test("hmac(sha256)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 break;
950
Andrew Donofrioa28091a2006-12-10 12:10:20 +1100951 case 103:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000952 ret += tcrypt_test("hmac(sha384)");
Andrew Donofrioa28091a2006-12-10 12:10:20 +1100953 break;
954
955 case 104:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000956 ret += tcrypt_test("hmac(sha512)");
Andrew Donofrioa28091a2006-12-10 12:10:20 +1100957 break;
Herbert Xu38ed9ab2008-01-01 15:59:28 +1100958
Jonathan Lynchcd12fb92007-11-10 20:08:25 +0800959 case 105:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000960 ret += tcrypt_test("hmac(sha224)");
Jonathan Lynchcd12fb92007-11-10 20:08:25 +0800961 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Herbert Xu38ed9ab2008-01-01 15:59:28 +1100963 case 106:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000964 ret += tcrypt_test("xcbc(aes)");
Herbert Xu38ed9ab2008-01-01 15:59:28 +1100965 break;
966
Adrian-Ken Rueegseggerfd4adf12008-05-07 22:16:36 +0800967 case 107:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000968 ret += tcrypt_test("hmac(rmd128)");
Adrian-Ken Rueegseggerfd4adf12008-05-07 22:16:36 +0800969 break;
970
971 case 108:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000972 ret += tcrypt_test("hmac(rmd160)");
Adrian-Ken Rueegseggerfd4adf12008-05-07 22:16:36 +0800973 break;
974
Shane Wangf1939f72009-09-02 20:05:22 +1000975 case 109:
976 ret += tcrypt_test("vmac(aes)");
977 break;
978
Jarod Wilsone08ca2d2009-05-04 19:46:29 +0800979 case 150:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000980 ret += tcrypt_test("ansi_cprng");
Jarod Wilsone08ca2d2009-05-04 19:46:29 +0800981 break;
982
Harald Welteebfd9bc2005-06-22 13:27:23 -0700983 case 200:
Herbert Xucba83562006-08-13 08:26:09 +1000984 test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800985 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +1000986 test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800987 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +1000988 test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800989 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +1000990 test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800991 speed_template_16_24_32);
Rik Snelf3d10442006-11-29 19:01:41 +1100992 test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800993 speed_template_32_40_48);
Rik Snelf3d10442006-11-29 19:01:41 +1100994 test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800995 speed_template_32_40_48);
Rik Snelf19f5112007-09-19 20:23:13 +0800996 test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800997 speed_template_32_48_64);
Rik Snelf19f5112007-09-19 20:23:13 +0800998 test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800999 speed_template_32_48_64);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001000 break;
1001
1002 case 201:
Herbert Xucba83562006-08-13 08:26:09 +10001003 test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
Herbert Xuda7f0332008-07-31 17:08:25 +08001004 des3_speed_template, DES3_SPEED_VECTORS,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001005 speed_template_24);
Herbert Xucba83562006-08-13 08:26:09 +10001006 test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
Herbert Xuda7f0332008-07-31 17:08:25 +08001007 des3_speed_template, DES3_SPEED_VECTORS,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001008 speed_template_24);
Herbert Xucba83562006-08-13 08:26:09 +10001009 test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
Herbert Xuda7f0332008-07-31 17:08:25 +08001010 des3_speed_template, DES3_SPEED_VECTORS,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001011 speed_template_24);
Herbert Xucba83562006-08-13 08:26:09 +10001012 test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
Herbert Xuda7f0332008-07-31 17:08:25 +08001013 des3_speed_template, DES3_SPEED_VECTORS,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001014 speed_template_24);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001015 break;
1016
1017 case 202:
Herbert Xucba83562006-08-13 08:26:09 +10001018 test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001019 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +10001020 test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001021 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +10001022 test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001023 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +10001024 test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001025 speed_template_16_24_32);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001026 break;
1027
1028 case 203:
Herbert Xucba83562006-08-13 08:26:09 +10001029 test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001030 speed_template_8_32);
Herbert Xucba83562006-08-13 08:26:09 +10001031 test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001032 speed_template_8_32);
Herbert Xucba83562006-08-13 08:26:09 +10001033 test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001034 speed_template_8_32);
Herbert Xucba83562006-08-13 08:26:09 +10001035 test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001036 speed_template_8_32);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001037 break;
1038
1039 case 204:
Herbert Xucba83562006-08-13 08:26:09 +10001040 test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001041 speed_template_8);
Herbert Xucba83562006-08-13 08:26:09 +10001042 test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001043 speed_template_8);
Herbert Xucba83562006-08-13 08:26:09 +10001044 test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001045 speed_template_8);
Herbert Xucba83562006-08-13 08:26:09 +10001046 test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001047 speed_template_8);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001048 break;
1049
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +11001050 case 205:
1051 test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001052 speed_template_16_24_32);
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +11001053 test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001054 speed_template_16_24_32);
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +11001055 test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001056 speed_template_16_24_32);
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +11001057 test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001058 speed_template_16_24_32);
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +11001059 break;
1060
Tan Swee Heng5de8f1b2007-12-07 17:17:43 +08001061 case 206:
1062 test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001063 speed_template_16_32);
Tan Swee Heng5de8f1b2007-12-07 17:17:43 +08001064 break;
1065
Michal Ludvige8057922006-05-30 22:04:19 +10001066 case 300:
1067 /* fall through */
1068
1069 case 301:
Herbert Xue9d41162006-08-19 21:38:49 +10001070 test_hash_speed("md4", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001071 if (mode > 300 && mode < 400) break;
1072
1073 case 302:
Herbert Xue9d41162006-08-19 21:38:49 +10001074 test_hash_speed("md5", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001075 if (mode > 300 && mode < 400) break;
1076
1077 case 303:
Herbert Xue9d41162006-08-19 21:38:49 +10001078 test_hash_speed("sha1", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001079 if (mode > 300 && mode < 400) break;
1080
1081 case 304:
Herbert Xue9d41162006-08-19 21:38:49 +10001082 test_hash_speed("sha256", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001083 if (mode > 300 && mode < 400) break;
1084
1085 case 305:
Herbert Xue9d41162006-08-19 21:38:49 +10001086 test_hash_speed("sha384", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001087 if (mode > 300 && mode < 400) break;
1088
1089 case 306:
Herbert Xue9d41162006-08-19 21:38:49 +10001090 test_hash_speed("sha512", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001091 if (mode > 300 && mode < 400) break;
1092
1093 case 307:
Herbert Xue9d41162006-08-19 21:38:49 +10001094 test_hash_speed("wp256", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001095 if (mode > 300 && mode < 400) break;
1096
1097 case 308:
Herbert Xue9d41162006-08-19 21:38:49 +10001098 test_hash_speed("wp384", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001099 if (mode > 300 && mode < 400) break;
1100
1101 case 309:
Herbert Xue9d41162006-08-19 21:38:49 +10001102 test_hash_speed("wp512", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001103 if (mode > 300 && mode < 400) break;
1104
1105 case 310:
Herbert Xue9d41162006-08-19 21:38:49 +10001106 test_hash_speed("tgr128", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001107 if (mode > 300 && mode < 400) break;
1108
1109 case 311:
Herbert Xue9d41162006-08-19 21:38:49 +10001110 test_hash_speed("tgr160", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001111 if (mode > 300 && mode < 400) break;
1112
1113 case 312:
Herbert Xue9d41162006-08-19 21:38:49 +10001114 test_hash_speed("tgr192", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001115 if (mode > 300 && mode < 400) break;
1116
Jonathan Lynchcd12fb92007-11-10 20:08:25 +08001117 case 313:
1118 test_hash_speed("sha224", sec, generic_hash_speed_template);
1119 if (mode > 300 && mode < 400) break;
1120
Adrian-Ken Rueegseggerfd4adf12008-05-07 22:16:36 +08001121 case 314:
1122 test_hash_speed("rmd128", sec, generic_hash_speed_template);
1123 if (mode > 300 && mode < 400) break;
1124
1125 case 315:
1126 test_hash_speed("rmd160", sec, generic_hash_speed_template);
1127 if (mode > 300 && mode < 400) break;
1128
Adrian-Ken Rueegsegger2998db32008-05-09 21:29:35 +08001129 case 316:
1130 test_hash_speed("rmd256", sec, generic_hash_speed_template);
1131 if (mode > 300 && mode < 400) break;
1132
1133 case 317:
1134 test_hash_speed("rmd320", sec, generic_hash_speed_template);
1135 if (mode > 300 && mode < 400) break;
1136
Huang Ying18bcc912010-03-10 18:30:32 +08001137 case 318:
1138 test_hash_speed("ghash-generic", sec, hash_speed_template_16);
1139 if (mode > 300 && mode < 400) break;
1140
Michal Ludvige8057922006-05-30 22:04:19 +10001141 case 399:
1142 break;
1143
David S. Millerbeb63da2010-05-19 14:11:21 +10001144 case 400:
1145 /* fall through */
1146
1147 case 401:
1148 test_ahash_speed("md4", sec, generic_hash_speed_template);
1149 if (mode > 400 && mode < 500) break;
1150
1151 case 402:
1152 test_ahash_speed("md5", sec, generic_hash_speed_template);
1153 if (mode > 400 && mode < 500) break;
1154
1155 case 403:
1156 test_ahash_speed("sha1", sec, generic_hash_speed_template);
1157 if (mode > 400 && mode < 500) break;
1158
1159 case 404:
1160 test_ahash_speed("sha256", sec, generic_hash_speed_template);
1161 if (mode > 400 && mode < 500) break;
1162
1163 case 405:
1164 test_ahash_speed("sha384", sec, generic_hash_speed_template);
1165 if (mode > 400 && mode < 500) break;
1166
1167 case 406:
1168 test_ahash_speed("sha512", sec, generic_hash_speed_template);
1169 if (mode > 400 && mode < 500) break;
1170
1171 case 407:
1172 test_ahash_speed("wp256", sec, generic_hash_speed_template);
1173 if (mode > 400 && mode < 500) break;
1174
1175 case 408:
1176 test_ahash_speed("wp384", sec, generic_hash_speed_template);
1177 if (mode > 400 && mode < 500) break;
1178
1179 case 409:
1180 test_ahash_speed("wp512", sec, generic_hash_speed_template);
1181 if (mode > 400 && mode < 500) break;
1182
1183 case 410:
1184 test_ahash_speed("tgr128", sec, generic_hash_speed_template);
1185 if (mode > 400 && mode < 500) break;
1186
1187 case 411:
1188 test_ahash_speed("tgr160", sec, generic_hash_speed_template);
1189 if (mode > 400 && mode < 500) break;
1190
1191 case 412:
1192 test_ahash_speed("tgr192", sec, generic_hash_speed_template);
1193 if (mode > 400 && mode < 500) break;
1194
1195 case 413:
1196 test_ahash_speed("sha224", sec, generic_hash_speed_template);
1197 if (mode > 400 && mode < 500) break;
1198
1199 case 414:
1200 test_ahash_speed("rmd128", sec, generic_hash_speed_template);
1201 if (mode > 400 && mode < 500) break;
1202
1203 case 415:
1204 test_ahash_speed("rmd160", sec, generic_hash_speed_template);
1205 if (mode > 400 && mode < 500) break;
1206
1207 case 416:
1208 test_ahash_speed("rmd256", sec, generic_hash_speed_template);
1209 if (mode > 400 && mode < 500) break;
1210
1211 case 417:
1212 test_ahash_speed("rmd320", sec, generic_hash_speed_template);
1213 if (mode > 400 && mode < 500) break;
1214
1215 case 499:
1216 break;
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 case 1000:
1219 test_available();
1220 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
Jarod Wilson4e033a62009-05-27 15:10:21 +10001222
1223 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
Herbert Xu7be380f2009-07-14 16:06:54 +08001226static int do_alg_test(const char *alg, u32 type, u32 mask)
Steffen Klasserta873a5f2009-06-19 19:46:53 +08001227{
Herbert Xu7be380f2009-07-14 16:06:54 +08001228 return crypto_has_alg(alg, type, mask ?: CRYPTO_ALG_TYPE_MASK) ?
1229 0 : -ENOENT;
Steffen Klasserta873a5f2009-06-19 19:46:53 +08001230}
1231
Kamalesh Babulal3af5b902008-04-05 21:00:57 +08001232static int __init tcrypt_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Mikko Herranene3a4ea42007-11-26 22:12:07 +08001234 int err = -ENOMEM;
Herbert Xuf139cfa2008-07-31 12:23:53 +08001235 int i;
Mikko Herranene3a4ea42007-11-26 22:12:07 +08001236
Herbert Xuf139cfa2008-07-31 12:23:53 +08001237 for (i = 0; i < TVMEMSIZE; i++) {
1238 tvmem[i] = (void *)__get_free_page(GFP_KERNEL);
1239 if (!tvmem[i])
1240 goto err_free_tv;
1241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Steffen Klasserta873a5f2009-06-19 19:46:53 +08001243 if (alg)
Herbert Xu7be380f2009-07-14 16:06:54 +08001244 err = do_alg_test(alg, type, mask);
Steffen Klasserta873a5f2009-06-19 19:46:53 +08001245 else
1246 err = do_test(mode);
1247
Jarod Wilson4e033a62009-05-27 15:10:21 +10001248 if (err) {
1249 printk(KERN_ERR "tcrypt: one or more tests failed!\n");
1250 goto err_free_tv;
1251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Jarod Wilson4e033a62009-05-27 15:10:21 +10001253 /* We intentionaly return -EAGAIN to prevent keeping the module,
1254 * unless we're running in fips mode. It does all its work from
1255 * init() and doesn't offer any runtime functionality, but in
1256 * the fips case, checking for a successful load is helpful.
Michal Ludvig14fdf472006-05-30 14:49:38 +10001257 * => we don't need it in the memory, do we?
1258 * -- mludvig
1259 */
Jarod Wilson4e033a62009-05-27 15:10:21 +10001260 if (!fips_enabled)
1261 err = -EAGAIN;
Mikko Herranene3a4ea42007-11-26 22:12:07 +08001262
Herbert Xuf139cfa2008-07-31 12:23:53 +08001263err_free_tv:
1264 for (i = 0; i < TVMEMSIZE && tvmem[i]; i++)
1265 free_page((unsigned long)tvmem[i]);
Mikko Herranene3a4ea42007-11-26 22:12:07 +08001266
1267 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
1270/*
1271 * If an init function is provided, an exit function must also be provided
1272 * to allow module unload.
1273 */
Kamalesh Babulal3af5b902008-04-05 21:00:57 +08001274static void __exit tcrypt_mod_fini(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Kamalesh Babulal3af5b902008-04-05 21:00:57 +08001276module_init(tcrypt_mod_init);
1277module_exit(tcrypt_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Steffen Klasserta873a5f2009-06-19 19:46:53 +08001279module_param(alg, charp, 0);
1280module_param(type, uint, 0);
Herbert Xu7be380f2009-07-14 16:06:54 +08001281module_param(mask, uint, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282module_param(mode, int, 0);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001283module_param(sec, uint, 0);
Herbert Xu6a179442005-06-22 13:29:03 -07001284MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
1285 "(defaults to zero which uses CPU cycles instead)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287MODULE_LICENSE("GPL");
1288MODULE_DESCRIPTION("Quick & dirty crypto testing module");
1289MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");