blob: a35159947a262f779041fe7bff86671cac00ed98 [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 Lynchcd12fb902007-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
Herbert Xu01b32322008-07-31 15:41:55 +0800397static void test_hash_speed(const char *algo, unsigned int sec,
398 struct hash_speed *speed)
Herbert Xue9d41162006-08-19 21:38:49 +1000399{
Herbert Xuf139cfa2008-07-31 12:23:53 +0800400 struct scatterlist sg[TVMEMSIZE];
Herbert Xue9d41162006-08-19 21:38:49 +1000401 struct crypto_hash *tfm;
402 struct hash_desc desc;
Frank Seidel376bacb2009-03-29 15:18:39 +0800403 static char output[1024];
Michal Ludvige8057922006-05-30 22:04:19 +1000404 int i;
Herbert Xue9d41162006-08-19 21:38:49 +1000405 int ret;
Michal Ludvige8057922006-05-30 22:04:19 +1000406
Frank Seidel376bacb2009-03-29 15:18:39 +0800407 printk(KERN_INFO "\ntesting speed of %s\n", algo);
Michal Ludvige8057922006-05-30 22:04:19 +1000408
Herbert Xue9d41162006-08-19 21:38:49 +1000409 tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
Michal Ludvige8057922006-05-30 22:04:19 +1000410
Herbert Xue9d41162006-08-19 21:38:49 +1000411 if (IS_ERR(tfm)) {
Frank Seidel376bacb2009-03-29 15:18:39 +0800412 printk(KERN_ERR "failed to load transform for %s: %ld\n", algo,
Herbert Xue9d41162006-08-19 21:38:49 +1000413 PTR_ERR(tfm));
Michal Ludvige8057922006-05-30 22:04:19 +1000414 return;
415 }
416
Herbert Xue9d41162006-08-19 21:38:49 +1000417 desc.tfm = tfm;
418 desc.flags = 0;
419
420 if (crypto_hash_digestsize(tfm) > sizeof(output)) {
Frank Seidel376bacb2009-03-29 15:18:39 +0800421 printk(KERN_ERR "digestsize(%u) > outputbuffer(%zu)\n",
Herbert Xue9d41162006-08-19 21:38:49 +1000422 crypto_hash_digestsize(tfm), sizeof(output));
Michal Ludvige8057922006-05-30 22:04:19 +1000423 goto out;
424 }
425
Herbert Xuf139cfa2008-07-31 12:23:53 +0800426 sg_init_table(sg, TVMEMSIZE);
427 for (i = 0; i < TVMEMSIZE; i++) {
428 sg_set_buf(sg + i, tvmem[i], PAGE_SIZE);
429 memset(tvmem[i], 0xff, PAGE_SIZE);
430 }
431
Michal Ludvige8057922006-05-30 22:04:19 +1000432 for (i = 0; speed[i].blen != 0; i++) {
Herbert Xuf139cfa2008-07-31 12:23:53 +0800433 if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
Frank Seidel376bacb2009-03-29 15:18:39 +0800434 printk(KERN_ERR
435 "template (%u) too big for tvmem (%lu)\n",
Herbert Xuf139cfa2008-07-31 12:23:53 +0800436 speed[i].blen, TVMEMSIZE * PAGE_SIZE);
Michal Ludvige8057922006-05-30 22:04:19 +1000437 goto out;
438 }
439
Frank Seidel376bacb2009-03-29 15:18:39 +0800440 printk(KERN_INFO "test%3u "
441 "(%5u byte blocks,%5u bytes per update,%4u updates): ",
Michal Ludvige8057922006-05-30 22:04:19 +1000442 i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
443
Michal Ludvige8057922006-05-30 22:04:19 +1000444 if (sec)
Herbert Xuf139cfa2008-07-31 12:23:53 +0800445 ret = test_hash_jiffies(&desc, sg, speed[i].blen,
Herbert Xue9d41162006-08-19 21:38:49 +1000446 speed[i].plen, output, sec);
Michal Ludvige8057922006-05-30 22:04:19 +1000447 else
Herbert Xuf139cfa2008-07-31 12:23:53 +0800448 ret = test_hash_cycles(&desc, sg, speed[i].blen,
Herbert Xue9d41162006-08-19 21:38:49 +1000449 speed[i].plen, output);
450
451 if (ret) {
Frank Seidel376bacb2009-03-29 15:18:39 +0800452 printk(KERN_ERR "hashing failed ret=%d\n", ret);
Herbert Xue9d41162006-08-19 21:38:49 +1000453 break;
454 }
Michal Ludvige8057922006-05-30 22:04:19 +1000455 }
456
457out:
Herbert Xue9d41162006-08-19 21:38:49 +1000458 crypto_free_hash(tfm);
Michal Ludvige8057922006-05-30 22:04:19 +1000459}
460
Herbert Xuef2736f2005-06-22 13:26:03 -0700461static void test_available(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 char **name = check;
Herbert Xuef2736f2005-06-22 13:26:03 -0700464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 while (*name) {
466 printk("alg %s ", *name);
Herbert Xu6158efc2007-04-04 17:41:07 +1000467 printk(crypto_has_alg(*name, 0, 0) ?
Herbert Xue4d5b792006-08-26 18:12:40 +1000468 "found\n" : "not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 name++;
Herbert Xuef2736f2005-06-22 13:26:03 -0700470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Herbert Xu01b32322008-07-31 15:41:55 +0800473static inline int tcrypt_test(const char *alg)
474{
Jarod Wilson4e033a62009-05-27 15:10:21 +1000475 int ret;
476
477 ret = alg_test(alg, alg, 0, 0);
478 /* non-fips algs return -EINVAL in fips mode */
479 if (fips_enabled && ret == -EINVAL)
480 ret = 0;
481 return ret;
Herbert Xu01b32322008-07-31 15:41:55 +0800482}
483
Jarod Wilson4e033a62009-05-27 15:10:21 +1000484static int do_test(int m)
Herbert Xu01b32322008-07-31 15:41:55 +0800485{
486 int i;
Jarod Wilson4e033a62009-05-27 15:10:21 +1000487 int ret = 0;
Herbert Xu01b32322008-07-31 15:41:55 +0800488
489 switch (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 case 0:
Herbert Xu01b32322008-07-31 15:41:55 +0800491 for (i = 1; i < 200; i++)
Jarod Wilson4e033a62009-05-27 15:10:21 +1000492 ret += do_test(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 break;
494
495 case 1:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000496 ret += tcrypt_test("md5");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 break;
498
499 case 2:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000500 ret += tcrypt_test("sha1");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 break;
502
503 case 3:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000504 ret += tcrypt_test("ecb(des)");
505 ret += tcrypt_test("cbc(des)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 break;
507
508 case 4:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000509 ret += tcrypt_test("ecb(des3_ede)");
510 ret += tcrypt_test("cbc(des3_ede)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 break;
512
513 case 5:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000514 ret += tcrypt_test("md4");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 break;
Herbert Xuef2736f2005-06-22 13:26:03 -0700516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 case 6:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000518 ret += tcrypt_test("sha256");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 break;
Herbert Xuef2736f2005-06-22 13:26:03 -0700520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 case 7:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000522 ret += tcrypt_test("ecb(blowfish)");
523 ret += tcrypt_test("cbc(blowfish)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 break;
525
526 case 8:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000527 ret += tcrypt_test("ecb(twofish)");
528 ret += tcrypt_test("cbc(twofish)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 break;
Herbert Xuef2736f2005-06-22 13:26:03 -0700530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 case 9:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000532 ret += tcrypt_test("ecb(serpent)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 break;
534
535 case 10:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000536 ret += tcrypt_test("ecb(aes)");
537 ret += tcrypt_test("cbc(aes)");
538 ret += tcrypt_test("lrw(aes)");
539 ret += tcrypt_test("xts(aes)");
540 ret += tcrypt_test("ctr(aes)");
541 ret += tcrypt_test("rfc3686(ctr(aes))");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 break;
543
544 case 11:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000545 ret += tcrypt_test("sha384");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 break;
Herbert Xuef2736f2005-06-22 13:26:03 -0700547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 case 12:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000549 ret += tcrypt_test("sha512");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 break;
551
552 case 13:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000553 ret += tcrypt_test("deflate");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 break;
555
556 case 14:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000557 ret += tcrypt_test("ecb(cast5)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 break;
559
560 case 15:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000561 ret += tcrypt_test("ecb(cast6)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 break;
563
564 case 16:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000565 ret += tcrypt_test("ecb(arc4)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 break;
567
568 case 17:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000569 ret += tcrypt_test("michael_mic");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 break;
571
572 case 18:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000573 ret += tcrypt_test("crc32c");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 break;
575
576 case 19:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000577 ret += tcrypt_test("ecb(tea)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 break;
579
580 case 20:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000581 ret += tcrypt_test("ecb(xtea)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 break;
583
584 case 21:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000585 ret += tcrypt_test("ecb(khazad)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 break;
587
588 case 22:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000589 ret += tcrypt_test("wp512");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 break;
591
592 case 23:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000593 ret += tcrypt_test("wp384");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 break;
595
596 case 24:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000597 ret += tcrypt_test("wp256");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 break;
599
600 case 25:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000601 ret += tcrypt_test("ecb(tnepres)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 break;
603
604 case 26:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000605 ret += tcrypt_test("ecb(anubis)");
606 ret += tcrypt_test("cbc(anubis)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 break;
608
609 case 27:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000610 ret += tcrypt_test("tgr192");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 break;
612
613 case 28:
614
Jarod Wilson4e033a62009-05-27 15:10:21 +1000615 ret += tcrypt_test("tgr160");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 break;
617
618 case 29:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000619 ret += tcrypt_test("tgr128");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 break;
Adrian-Ken Rueegsegger2998db32008-05-09 21:29:35 +0800621
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700622 case 30:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000623 ret += tcrypt_test("ecb(xeta)");
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700624 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
David Howells90831632006-12-16 12:13:14 +1100626 case 31:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000627 ret += tcrypt_test("pcbc(fcrypt)");
David Howells90831632006-12-16 12:13:14 +1100628 break;
629
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +1100630 case 32:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000631 ret += tcrypt_test("ecb(camellia)");
632 ret += tcrypt_test("cbc(camellia)");
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +1100633 break;
Jonathan Lynchcd12fb902007-11-10 20:08:25 +0800634 case 33:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000635 ret += tcrypt_test("sha224");
Jonathan Lynchcd12fb902007-11-10 20:08:25 +0800636 break;
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +1100637
Tan Swee Heng2407d602007-11-23 19:45:00 +0800638 case 34:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000639 ret += tcrypt_test("salsa20");
Tan Swee Heng2407d602007-11-23 19:45:00 +0800640 break;
641
Herbert Xu8df213d2007-12-02 14:55:47 +1100642 case 35:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000643 ret += tcrypt_test("gcm(aes)");
Herbert Xu8df213d2007-12-02 14:55:47 +1100644 break;
645
Zoltan Sogor0b77abb2007-12-07 16:53:23 +0800646 case 36:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000647 ret += tcrypt_test("lzo");
Zoltan Sogor0b77abb2007-12-07 16:53:23 +0800648 break;
649
Joy Latten93cc74e2007-12-12 20:24:22 +0800650 case 37:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000651 ret += tcrypt_test("ccm(aes)");
Joy Latten93cc74e2007-12-12 20:24:22 +0800652 break;
653
Kevin Coffman76cb9522008-03-24 21:26:16 +0800654 case 38:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000655 ret += tcrypt_test("cts(cbc(aes))");
Kevin Coffman76cb9522008-03-24 21:26:16 +0800656 break;
657
Adrian-Ken Rueegseggerfd4adf12008-05-07 22:16:36 +0800658 case 39:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000659 ret += tcrypt_test("rmd128");
Adrian-Ken Rueegseggerfd4adf12008-05-07 22:16:36 +0800660 break;
661
662 case 40:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000663 ret += tcrypt_test("rmd160");
Adrian-Ken Rueegseggerfd4adf12008-05-07 22:16:36 +0800664 break;
665
Adrian-Ken Rueegsegger2998db32008-05-09 21:29:35 +0800666 case 41:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000667 ret += tcrypt_test("rmd256");
Adrian-Ken Rueegsegger2998db32008-05-09 21:29:35 +0800668 break;
669
670 case 42:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000671 ret += tcrypt_test("rmd320");
Herbert Xu01b32322008-07-31 15:41:55 +0800672 break;
673
674 case 43:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000675 ret += tcrypt_test("ecb(seed)");
Adrian-Ken Rueegsegger2998db32008-05-09 21:29:35 +0800676 break;
677
Geert Uytterhoeven0c01aed2009-03-04 15:42:15 +0800678 case 44:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000679 ret += tcrypt_test("zlib");
Geert Uytterhoeven0c01aed2009-03-04 15:42:15 +0800680 break;
681
Jarod Wilson5d667322009-05-04 19:23:40 +0800682 case 45:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000683 ret += tcrypt_test("rfc4309(ccm(aes))");
Jarod Wilson5d667322009-05-04 19:23:40 +0800684 break;
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 case 100:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000687 ret += tcrypt_test("hmac(md5)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 break;
Herbert Xuef2736f2005-06-22 13:26:03 -0700689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 case 101:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000691 ret += tcrypt_test("hmac(sha1)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 break;
Herbert Xuef2736f2005-06-22 13:26:03 -0700693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 case 102:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000695 ret += tcrypt_test("hmac(sha256)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 break;
697
Andrew Donofrioa28091a2006-12-10 12:10:20 +1100698 case 103:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000699 ret += tcrypt_test("hmac(sha384)");
Andrew Donofrioa28091a2006-12-10 12:10:20 +1100700 break;
701
702 case 104:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000703 ret += tcrypt_test("hmac(sha512)");
Andrew Donofrioa28091a2006-12-10 12:10:20 +1100704 break;
Herbert Xu38ed9ab2008-01-01 15:59:28 +1100705
Jonathan Lynchcd12fb902007-11-10 20:08:25 +0800706 case 105:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000707 ret += tcrypt_test("hmac(sha224)");
Jonathan Lynchcd12fb902007-11-10 20:08:25 +0800708 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Herbert Xu38ed9ab2008-01-01 15:59:28 +1100710 case 106:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000711 ret += tcrypt_test("xcbc(aes)");
Herbert Xu38ed9ab2008-01-01 15:59:28 +1100712 break;
713
Adrian-Ken Rueegseggerfd4adf12008-05-07 22:16:36 +0800714 case 107:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000715 ret += tcrypt_test("hmac(rmd128)");
Adrian-Ken Rueegseggerfd4adf12008-05-07 22:16:36 +0800716 break;
717
718 case 108:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000719 ret += tcrypt_test("hmac(rmd160)");
Adrian-Ken Rueegseggerfd4adf12008-05-07 22:16:36 +0800720 break;
721
Shane Wangf1939f72009-09-02 20:05:22 +1000722 case 109:
723 ret += tcrypt_test("vmac(aes)");
724 break;
725
Jarod Wilsone08ca2d2009-05-04 19:46:29 +0800726 case 150:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000727 ret += tcrypt_test("ansi_cprng");
Jarod Wilsone08ca2d2009-05-04 19:46:29 +0800728 break;
729
Harald Welteebfd9bc2005-06-22 13:27:23 -0700730 case 200:
Herbert Xucba83562006-08-13 08:26:09 +1000731 test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800732 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +1000733 test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800734 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +1000735 test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800736 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +1000737 test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800738 speed_template_16_24_32);
Rik Snelf3d10442006-11-29 19:01:41 +1100739 test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800740 speed_template_32_40_48);
Rik Snelf3d10442006-11-29 19:01:41 +1100741 test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800742 speed_template_32_40_48);
Rik Snelf19f5112007-09-19 20:23:13 +0800743 test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800744 speed_template_32_48_64);
Rik Snelf19f5112007-09-19 20:23:13 +0800745 test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800746 speed_template_32_48_64);
Harald Welteebfd9bc2005-06-22 13:27:23 -0700747 break;
748
749 case 201:
Herbert Xucba83562006-08-13 08:26:09 +1000750 test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
Herbert Xuda7f0332008-07-31 17:08:25 +0800751 des3_speed_template, DES3_SPEED_VECTORS,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800752 speed_template_24);
Herbert Xucba83562006-08-13 08:26:09 +1000753 test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
Herbert Xuda7f0332008-07-31 17:08:25 +0800754 des3_speed_template, DES3_SPEED_VECTORS,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800755 speed_template_24);
Herbert Xucba83562006-08-13 08:26:09 +1000756 test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
Herbert Xuda7f0332008-07-31 17:08:25 +0800757 des3_speed_template, DES3_SPEED_VECTORS,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800758 speed_template_24);
Herbert Xucba83562006-08-13 08:26:09 +1000759 test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
Herbert Xuda7f0332008-07-31 17:08:25 +0800760 des3_speed_template, DES3_SPEED_VECTORS,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800761 speed_template_24);
Harald Welteebfd9bc2005-06-22 13:27:23 -0700762 break;
763
764 case 202:
Herbert Xucba83562006-08-13 08:26:09 +1000765 test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800766 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +1000767 test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800768 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +1000769 test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800770 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +1000771 test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800772 speed_template_16_24_32);
Harald Welteebfd9bc2005-06-22 13:27:23 -0700773 break;
774
775 case 203:
Herbert Xucba83562006-08-13 08:26:09 +1000776 test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800777 speed_template_8_32);
Herbert Xucba83562006-08-13 08:26:09 +1000778 test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800779 speed_template_8_32);
Herbert Xucba83562006-08-13 08:26:09 +1000780 test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800781 speed_template_8_32);
Herbert Xucba83562006-08-13 08:26:09 +1000782 test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800783 speed_template_8_32);
Harald Welteebfd9bc2005-06-22 13:27:23 -0700784 break;
785
786 case 204:
Herbert Xucba83562006-08-13 08:26:09 +1000787 test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800788 speed_template_8);
Herbert Xucba83562006-08-13 08:26:09 +1000789 test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800790 speed_template_8);
Herbert Xucba83562006-08-13 08:26:09 +1000791 test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800792 speed_template_8);
Herbert Xucba83562006-08-13 08:26:09 +1000793 test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800794 speed_template_8);
Harald Welteebfd9bc2005-06-22 13:27:23 -0700795 break;
796
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +1100797 case 205:
798 test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800799 speed_template_16_24_32);
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +1100800 test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800801 speed_template_16_24_32);
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +1100802 test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800803 speed_template_16_24_32);
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +1100804 test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800805 speed_template_16_24_32);
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +1100806 break;
807
Tan Swee Heng5de8f1b2007-12-07 17:17:43 +0800808 case 206:
809 test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800810 speed_template_16_32);
Tan Swee Heng5de8f1b2007-12-07 17:17:43 +0800811 break;
812
Michal Ludvige8057922006-05-30 22:04:19 +1000813 case 300:
814 /* fall through */
815
816 case 301:
Herbert Xue9d41162006-08-19 21:38:49 +1000817 test_hash_speed("md4", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +1000818 if (mode > 300 && mode < 400) break;
819
820 case 302:
Herbert Xue9d41162006-08-19 21:38:49 +1000821 test_hash_speed("md5", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +1000822 if (mode > 300 && mode < 400) break;
823
824 case 303:
Herbert Xue9d41162006-08-19 21:38:49 +1000825 test_hash_speed("sha1", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +1000826 if (mode > 300 && mode < 400) break;
827
828 case 304:
Herbert Xue9d41162006-08-19 21:38:49 +1000829 test_hash_speed("sha256", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +1000830 if (mode > 300 && mode < 400) break;
831
832 case 305:
Herbert Xue9d41162006-08-19 21:38:49 +1000833 test_hash_speed("sha384", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +1000834 if (mode > 300 && mode < 400) break;
835
836 case 306:
Herbert Xue9d41162006-08-19 21:38:49 +1000837 test_hash_speed("sha512", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +1000838 if (mode > 300 && mode < 400) break;
839
840 case 307:
Herbert Xue9d41162006-08-19 21:38:49 +1000841 test_hash_speed("wp256", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +1000842 if (mode > 300 && mode < 400) break;
843
844 case 308:
Herbert Xue9d41162006-08-19 21:38:49 +1000845 test_hash_speed("wp384", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +1000846 if (mode > 300 && mode < 400) break;
847
848 case 309:
Herbert Xue9d41162006-08-19 21:38:49 +1000849 test_hash_speed("wp512", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +1000850 if (mode > 300 && mode < 400) break;
851
852 case 310:
Herbert Xue9d41162006-08-19 21:38:49 +1000853 test_hash_speed("tgr128", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +1000854 if (mode > 300 && mode < 400) break;
855
856 case 311:
Herbert Xue9d41162006-08-19 21:38:49 +1000857 test_hash_speed("tgr160", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +1000858 if (mode > 300 && mode < 400) break;
859
860 case 312:
Herbert Xue9d41162006-08-19 21:38:49 +1000861 test_hash_speed("tgr192", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +1000862 if (mode > 300 && mode < 400) break;
863
Jonathan Lynchcd12fb902007-11-10 20:08:25 +0800864 case 313:
865 test_hash_speed("sha224", sec, generic_hash_speed_template);
866 if (mode > 300 && mode < 400) break;
867
Adrian-Ken Rueegseggerfd4adf12008-05-07 22:16:36 +0800868 case 314:
869 test_hash_speed("rmd128", sec, generic_hash_speed_template);
870 if (mode > 300 && mode < 400) break;
871
872 case 315:
873 test_hash_speed("rmd160", sec, generic_hash_speed_template);
874 if (mode > 300 && mode < 400) break;
875
Adrian-Ken Rueegsegger2998db32008-05-09 21:29:35 +0800876 case 316:
877 test_hash_speed("rmd256", sec, generic_hash_speed_template);
878 if (mode > 300 && mode < 400) break;
879
880 case 317:
881 test_hash_speed("rmd320", sec, generic_hash_speed_template);
882 if (mode > 300 && mode < 400) break;
883
Michal Ludvige8057922006-05-30 22:04:19 +1000884 case 399:
885 break;
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 case 1000:
888 test_available();
889 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
Jarod Wilson4e033a62009-05-27 15:10:21 +1000891
892 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
Herbert Xu7be380f2009-07-14 16:06:54 +0800895static int do_alg_test(const char *alg, u32 type, u32 mask)
Steffen Klasserta873a5f2009-06-19 19:46:53 +0800896{
Herbert Xu7be380f2009-07-14 16:06:54 +0800897 return crypto_has_alg(alg, type, mask ?: CRYPTO_ALG_TYPE_MASK) ?
898 0 : -ENOENT;
Steffen Klasserta873a5f2009-06-19 19:46:53 +0800899}
900
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800901static int __init tcrypt_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800903 int err = -ENOMEM;
Herbert Xuf139cfa2008-07-31 12:23:53 +0800904 int i;
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800905
Herbert Xuf139cfa2008-07-31 12:23:53 +0800906 for (i = 0; i < TVMEMSIZE; i++) {
907 tvmem[i] = (void *)__get_free_page(GFP_KERNEL);
908 if (!tvmem[i])
909 goto err_free_tv;
910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Steffen Klasserta873a5f2009-06-19 19:46:53 +0800912 if (alg)
Herbert Xu7be380f2009-07-14 16:06:54 +0800913 err = do_alg_test(alg, type, mask);
Steffen Klasserta873a5f2009-06-19 19:46:53 +0800914 else
915 err = do_test(mode);
916
Jarod Wilson4e033a62009-05-27 15:10:21 +1000917 if (err) {
918 printk(KERN_ERR "tcrypt: one or more tests failed!\n");
919 goto err_free_tv;
920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Jarod Wilson4e033a62009-05-27 15:10:21 +1000922 /* We intentionaly return -EAGAIN to prevent keeping the module,
923 * unless we're running in fips mode. It does all its work from
924 * init() and doesn't offer any runtime functionality, but in
925 * the fips case, checking for a successful load is helpful.
Michal Ludvig14fdf472006-05-30 14:49:38 +1000926 * => we don't need it in the memory, do we?
927 * -- mludvig
928 */
Jarod Wilson4e033a62009-05-27 15:10:21 +1000929 if (!fips_enabled)
930 err = -EAGAIN;
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800931
Herbert Xuf139cfa2008-07-31 12:23:53 +0800932err_free_tv:
933 for (i = 0; i < TVMEMSIZE && tvmem[i]; i++)
934 free_page((unsigned long)tvmem[i]);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800935
936 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
939/*
940 * If an init function is provided, an exit function must also be provided
941 * to allow module unload.
942 */
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800943static void __exit tcrypt_mod_fini(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800945module_init(tcrypt_mod_init);
946module_exit(tcrypt_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Steffen Klasserta873a5f2009-06-19 19:46:53 +0800948module_param(alg, charp, 0);
949module_param(type, uint, 0);
Herbert Xu7be380f2009-07-14 16:06:54 +0800950module_param(mask, uint, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951module_param(mode, int, 0);
Harald Welteebfd9bc2005-06-22 13:27:23 -0700952module_param(sec, uint, 0);
Herbert Xu6a179442005-06-22 13:29:03 -0700953MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
954 "(defaults to zero which uses CPU cycles instead)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956MODULE_LICENSE("GPL");
957MODULE_DESCRIPTION("Quick & dirty crypto testing module");
958MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");