blob: 9aac5e58be94063a9502156cbe032905669d8038 [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 *
Adrian Hoban69435b92010-11-04 15:02:04 -040011 * Updated RFC4106 AES-GCM testing.
12 * Authors: Aidan O'Mahony (aidan.o.mahony@intel.com)
13 * Adrian Hoban <adrian.hoban@intel.com>
14 * Gabriele Paoloni <gabriele.paoloni@intel.com>
15 * Tadeusz Struk (tadeusz.struk@intel.com)
16 * Copyright (c) 2010, Intel Corporation.
17 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * This program is free software; you can redistribute it and/or modify it
19 * under the terms of the GNU General Public License as published by the Free
Herbert Xuef2736f2005-06-22 13:26:03 -070020 * Software Foundation; either version 2 of the License, or (at your option)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * any later version.
22 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
Herbert Xu18e33e62008-07-10 16:01:22 +080025#include <crypto/hash.h>
Herbert Xucba83562006-08-13 08:26:09 +100026#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
David Hardeman378f0582005-09-17 17:55:31 +100030#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/moduleparam.h>
Harald Welteebfd9bc2005-06-22 13:27:23 -070033#include <linux/jiffies.h>
Herbert Xu6a179442005-06-22 13:29:03 -070034#include <linux/timex.h>
35#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "tcrypt.h"
Jarod Wilson4e033a62009-05-27 15:10:21 +100037#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/*
Herbert Xuf139cfa2008-07-31 12:23:53 +080040 * Need slab memory for testing (size in number of pages).
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 */
Herbert Xuf139cfa2008-07-31 12:23:53 +080042#define TVMEMSIZE 4
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/*
Herbert Xuda7f0332008-07-31 17:08:25 +080045* Used by test_cipher_speed()
Linus Torvalds1da177e2005-04-16 15:20:36 -070046*/
47#define ENCRYPT 1
48#define DECRYPT 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Harald Welteebfd9bc2005-06-22 13:27:23 -070050/*
51 * Used by test_cipher_speed()
52 */
Herbert Xu6a179442005-06-22 13:29:03 -070053static unsigned int sec;
Harald Welteebfd9bc2005-06-22 13:27:23 -070054
Steffen Klasserta873a5f2009-06-19 19:46:53 +080055static char *alg = NULL;
56static u32 type;
Herbert Xu7be380f2009-07-14 16:06:54 +080057static u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static int mode;
Herbert Xuf139cfa2008-07-31 12:23:53 +080059static char *tvmem[TVMEMSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61static char *check[] = {
Jonathan Lynchcd12fb902007-11-10 20:08:25 +080062 "des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
63 "blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
64 "cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
David Howells90831632006-12-16 12:13:14 +110065 "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", "fcrypt",
Adrian-Ken Rueegsegger2998db32008-05-09 21:29:35 +080066 "camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
Geert Uytterhoeven0c01aed2009-03-04 15:42:15 +080067 "lzo", "cts", "zlib", NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
Herbert Xuf139cfa2008-07-31 12:23:53 +080070static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc,
71 struct scatterlist *sg, int blen, int sec)
Herbert Xu6a179442005-06-22 13:29:03 -070072{
Herbert Xu6a179442005-06-22 13:29:03 -070073 unsigned long start, end;
74 int bcount;
75 int ret;
76
Herbert Xu6a179442005-06-22 13:29:03 -070077 for (start = jiffies, end = start + sec * HZ, bcount = 0;
78 time_before(jiffies, end); bcount++) {
79 if (enc)
Herbert Xucba83562006-08-13 08:26:09 +100080 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
Herbert Xu6a179442005-06-22 13:29:03 -070081 else
Herbert Xucba83562006-08-13 08:26:09 +100082 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
Herbert Xu6a179442005-06-22 13:29:03 -070083
84 if (ret)
85 return ret;
86 }
87
88 printk("%d operations in %d seconds (%ld bytes)\n",
89 bcount, sec, (long)bcount * blen);
90 return 0;
91}
92
Herbert Xuf139cfa2008-07-31 12:23:53 +080093static int test_cipher_cycles(struct blkcipher_desc *desc, int enc,
94 struct scatterlist *sg, int blen)
Herbert Xu6a179442005-06-22 13:29:03 -070095{
Herbert Xu6a179442005-06-22 13:29:03 -070096 unsigned long cycles = 0;
97 int ret = 0;
98 int i;
99
Herbert Xu6a179442005-06-22 13:29:03 -0700100 local_bh_disable();
101 local_irq_disable();
102
103 /* Warm-up run. */
104 for (i = 0; i < 4; i++) {
105 if (enc)
Herbert Xucba83562006-08-13 08:26:09 +1000106 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700107 else
Herbert Xucba83562006-08-13 08:26:09 +1000108 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700109
110 if (ret)
111 goto out;
112 }
113
114 /* The real thing. */
115 for (i = 0; i < 8; i++) {
116 cycles_t start, end;
117
118 start = get_cycles();
119 if (enc)
Herbert Xucba83562006-08-13 08:26:09 +1000120 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700121 else
Herbert Xucba83562006-08-13 08:26:09 +1000122 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700123 end = get_cycles();
124
125 if (ret)
126 goto out;
127
128 cycles += end - start;
129 }
130
131out:
132 local_irq_enable();
133 local_bh_enable();
134
135 if (ret == 0)
136 printk("1 operation in %lu cycles (%d bytes)\n",
137 (cycles + 4) / 8, blen);
138
139 return ret;
140}
141
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800142static u32 block_sizes[] = { 16, 64, 256, 1024, 8192, 0 };
143
Herbert Xu01b32322008-07-31 15:41:55 +0800144static void test_cipher_speed(const char *algo, int enc, unsigned int sec,
Herbert Xuda7f0332008-07-31 17:08:25 +0800145 struct cipher_speed_template *template,
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800146 unsigned int tcount, u8 *keysize)
Harald Welteebfd9bc2005-06-22 13:27:23 -0700147{
Herbert Xudce907c2005-06-22 13:27:51 -0700148 unsigned int ret, i, j, iv_len;
Herbert Xuda7f0332008-07-31 17:08:25 +0800149 const char *key, iv[128];
Herbert Xucba83562006-08-13 08:26:09 +1000150 struct crypto_blkcipher *tfm;
151 struct blkcipher_desc desc;
152 const char *e;
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800153 u32 *b_size;
Harald Welteebfd9bc2005-06-22 13:27:23 -0700154
155 if (enc == ENCRYPT)
156 e = "encryption";
157 else
158 e = "decryption";
Harald Welteebfd9bc2005-06-22 13:27:23 -0700159
Herbert Xucba83562006-08-13 08:26:09 +1000160 printk("\ntesting speed of %s %s\n", algo, e);
Harald Welteebfd9bc2005-06-22 13:27:23 -0700161
Herbert Xucba83562006-08-13 08:26:09 +1000162 tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
Harald Welteebfd9bc2005-06-22 13:27:23 -0700163
Herbert Xucba83562006-08-13 08:26:09 +1000164 if (IS_ERR(tfm)) {
165 printk("failed to load transform for %s: %ld\n", algo,
166 PTR_ERR(tfm));
Harald Welteebfd9bc2005-06-22 13:27:23 -0700167 return;
168 }
Herbert Xucba83562006-08-13 08:26:09 +1000169 desc.tfm = tfm;
170 desc.flags = 0;
Harald Welteebfd9bc2005-06-22 13:27:23 -0700171
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800172 i = 0;
173 do {
Harald Welteebfd9bc2005-06-22 13:27:23 -0700174
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800175 b_size = block_sizes;
176 do {
Herbert Xuf139cfa2008-07-31 12:23:53 +0800177 struct scatterlist sg[TVMEMSIZE];
Harald Welteebfd9bc2005-06-22 13:27:23 -0700178
Herbert Xuf139cfa2008-07-31 12:23:53 +0800179 if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
180 printk("template (%u) too big for "
181 "tvmem (%lu)\n", *keysize + *b_size,
182 TVMEMSIZE * PAGE_SIZE);
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800183 goto out;
184 }
Harald Welteebfd9bc2005-06-22 13:27:23 -0700185
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800186 printk("test %u (%d bit key, %d byte blocks): ", i,
187 *keysize * 8, *b_size);
188
Herbert Xuf139cfa2008-07-31 12:23:53 +0800189 memset(tvmem[0], 0xff, PAGE_SIZE);
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800190
191 /* set key, plain text and IV */
Herbert Xuda7f0332008-07-31 17:08:25 +0800192 key = tvmem[0];
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800193 for (j = 0; j < tcount; j++) {
194 if (template[j].klen == *keysize) {
195 key = template[j].key;
196 break;
197 }
198 }
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800199
200 ret = crypto_blkcipher_setkey(tfm, key, *keysize);
201 if (ret) {
202 printk("setkey() failed flags=%x\n",
203 crypto_blkcipher_get_flags(tfm));
204 goto out;
205 }
206
Herbert Xuf139cfa2008-07-31 12:23:53 +0800207 sg_init_table(sg, TVMEMSIZE);
208 sg_set_buf(sg, tvmem[0] + *keysize,
209 PAGE_SIZE - *keysize);
210 for (j = 1; j < TVMEMSIZE; j++) {
211 sg_set_buf(sg + j, tvmem[j], PAGE_SIZE);
212 memset (tvmem[j], 0xff, PAGE_SIZE);
213 }
214
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800215 iv_len = crypto_blkcipher_ivsize(tfm);
216 if (iv_len) {
217 memset(&iv, 0xff, iv_len);
218 crypto_blkcipher_set_iv(tfm, iv, iv_len);
219 }
220
221 if (sec)
Herbert Xuf139cfa2008-07-31 12:23:53 +0800222 ret = test_cipher_jiffies(&desc, enc, sg,
223 *b_size, sec);
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800224 else
Herbert Xuf139cfa2008-07-31 12:23:53 +0800225 ret = test_cipher_cycles(&desc, enc, sg,
226 *b_size);
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800227
228 if (ret) {
229 printk("%s() failed flags=%x\n", e, desc.flags);
Herbert Xudce907c2005-06-22 13:27:51 -0700230 break;
231 }
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800232 b_size++;
233 i++;
234 } while (*b_size);
235 keysize++;
236 } while (*keysize);
Harald Welteebfd9bc2005-06-22 13:27:23 -0700237
238out:
Herbert Xucba83562006-08-13 08:26:09 +1000239 crypto_free_blkcipher(tfm);
Harald Welteebfd9bc2005-06-22 13:27:23 -0700240}
241
Herbert Xuf139cfa2008-07-31 12:23:53 +0800242static int test_hash_jiffies_digest(struct hash_desc *desc,
243 struct scatterlist *sg, int blen,
Herbert Xue9d41162006-08-19 21:38:49 +1000244 char *out, int sec)
Michal Ludvige8057922006-05-30 22:04:19 +1000245{
Michal Ludvige8057922006-05-30 22:04:19 +1000246 unsigned long start, end;
Herbert Xue9d41162006-08-19 21:38:49 +1000247 int bcount;
248 int ret;
Michal Ludvige8057922006-05-30 22:04:19 +1000249
250 for (start = jiffies, end = start + sec * HZ, bcount = 0;
251 time_before(jiffies, end); bcount++) {
Herbert Xue9d41162006-08-19 21:38:49 +1000252 ret = crypto_hash_digest(desc, sg, blen, out);
253 if (ret)
254 return ret;
Michal Ludvige8057922006-05-30 22:04:19 +1000255 }
256
257 printk("%6u opers/sec, %9lu bytes/sec\n",
258 bcount / sec, ((long)bcount * blen) / sec);
259
Herbert Xue9d41162006-08-19 21:38:49 +1000260 return 0;
Michal Ludvige8057922006-05-30 22:04:19 +1000261}
262
Herbert Xuf139cfa2008-07-31 12:23:53 +0800263static int test_hash_jiffies(struct hash_desc *desc, struct scatterlist *sg,
264 int blen, int plen, char *out, int sec)
Herbert Xue9d41162006-08-19 21:38:49 +1000265{
Herbert Xue9d41162006-08-19 21:38:49 +1000266 unsigned long start, end;
267 int bcount, pcount;
268 int ret;
269
270 if (plen == blen)
Herbert Xuf139cfa2008-07-31 12:23:53 +0800271 return test_hash_jiffies_digest(desc, sg, blen, out, sec);
Herbert Xua5a613a2007-10-27 00:51:21 -0700272
Herbert Xue9d41162006-08-19 21:38:49 +1000273 for (start = jiffies, end = start + sec * HZ, bcount = 0;
274 time_before(jiffies, end); bcount++) {
275 ret = crypto_hash_init(desc);
276 if (ret)
277 return ret;
278 for (pcount = 0; pcount < blen; pcount += plen) {
Herbert Xue9d41162006-08-19 21:38:49 +1000279 ret = crypto_hash_update(desc, sg, plen);
280 if (ret)
281 return ret;
282 }
283 /* we assume there is enough space in 'out' for the result */
284 ret = crypto_hash_final(desc, out);
285 if (ret)
286 return ret;
287 }
288
289 printk("%6u opers/sec, %9lu bytes/sec\n",
290 bcount / sec, ((long)bcount * blen) / sec);
291
292 return 0;
293}
294
Herbert Xuf139cfa2008-07-31 12:23:53 +0800295static int test_hash_cycles_digest(struct hash_desc *desc,
296 struct scatterlist *sg, int blen, char *out)
Michal Ludvige8057922006-05-30 22:04:19 +1000297{
Michal Ludvige8057922006-05-30 22:04:19 +1000298 unsigned long cycles = 0;
Herbert Xue9d41162006-08-19 21:38:49 +1000299 int i;
300 int ret;
Michal Ludvige8057922006-05-30 22:04:19 +1000301
302 local_bh_disable();
303 local_irq_disable();
304
305 /* Warm-up run. */
306 for (i = 0; i < 4; i++) {
Herbert Xue9d41162006-08-19 21:38:49 +1000307 ret = crypto_hash_digest(desc, sg, blen, out);
308 if (ret)
309 goto out;
Michal Ludvige8057922006-05-30 22:04:19 +1000310 }
311
312 /* The real thing. */
313 for (i = 0; i < 8; i++) {
314 cycles_t start, end;
315
Michal Ludvige8057922006-05-30 22:04:19 +1000316 start = get_cycles();
317
Herbert Xue9d41162006-08-19 21:38:49 +1000318 ret = crypto_hash_digest(desc, sg, blen, out);
319 if (ret)
320 goto out;
Michal Ludvige8057922006-05-30 22:04:19 +1000321
322 end = get_cycles();
323
324 cycles += end - start;
325 }
326
Herbert Xue9d41162006-08-19 21:38:49 +1000327out:
Michal Ludvige8057922006-05-30 22:04:19 +1000328 local_irq_enable();
329 local_bh_enable();
330
Herbert Xue9d41162006-08-19 21:38:49 +1000331 if (ret)
332 return ret;
333
Michal Ludvige8057922006-05-30 22:04:19 +1000334 printk("%6lu cycles/operation, %4lu cycles/byte\n",
335 cycles / 8, cycles / (8 * blen));
336
Herbert Xue9d41162006-08-19 21:38:49 +1000337 return 0;
Michal Ludvige8057922006-05-30 22:04:19 +1000338}
339
Herbert Xuf139cfa2008-07-31 12:23:53 +0800340static int test_hash_cycles(struct hash_desc *desc, struct scatterlist *sg,
341 int blen, int plen, char *out)
Michal Ludvige8057922006-05-30 22:04:19 +1000342{
Herbert Xue9d41162006-08-19 21:38:49 +1000343 unsigned long cycles = 0;
344 int i, pcount;
345 int ret;
346
347 if (plen == blen)
Herbert Xuf139cfa2008-07-31 12:23:53 +0800348 return test_hash_cycles_digest(desc, sg, blen, out);
Herbert Xua5a613a2007-10-27 00:51:21 -0700349
Herbert Xue9d41162006-08-19 21:38:49 +1000350 local_bh_disable();
351 local_irq_disable();
352
353 /* Warm-up run. */
354 for (i = 0; i < 4; i++) {
355 ret = crypto_hash_init(desc);
356 if (ret)
357 goto out;
358 for (pcount = 0; pcount < blen; pcount += plen) {
Herbert Xue9d41162006-08-19 21:38:49 +1000359 ret = crypto_hash_update(desc, sg, plen);
360 if (ret)
361 goto out;
362 }
Herbert Xu29059d12007-05-18 16:25:19 +1000363 ret = crypto_hash_final(desc, out);
Herbert Xue9d41162006-08-19 21:38:49 +1000364 if (ret)
365 goto out;
366 }
367
368 /* The real thing. */
369 for (i = 0; i < 8; i++) {
370 cycles_t start, end;
371
372 start = get_cycles();
373
374 ret = crypto_hash_init(desc);
375 if (ret)
376 goto out;
377 for (pcount = 0; pcount < blen; pcount += plen) {
Herbert Xue9d41162006-08-19 21:38:49 +1000378 ret = crypto_hash_update(desc, sg, plen);
379 if (ret)
380 goto out;
381 }
382 ret = crypto_hash_final(desc, out);
383 if (ret)
384 goto out;
385
386 end = get_cycles();
387
388 cycles += end - start;
389 }
390
391out:
392 local_irq_enable();
393 local_bh_enable();
394
395 if (ret)
396 return ret;
397
398 printk("%6lu cycles/operation, %4lu cycles/byte\n",
399 cycles / 8, cycles / (8 * blen));
400
401 return 0;
402}
403
David S. Millerbeb63da2010-05-19 14:11:21 +1000404static void test_hash_sg_init(struct scatterlist *sg)
405{
406 int i;
407
408 sg_init_table(sg, TVMEMSIZE);
409 for (i = 0; i < TVMEMSIZE; i++) {
410 sg_set_buf(sg + i, tvmem[i], PAGE_SIZE);
411 memset(tvmem[i], 0xff, PAGE_SIZE);
412 }
413}
414
Herbert Xu01b32322008-07-31 15:41:55 +0800415static void test_hash_speed(const char *algo, unsigned int sec,
416 struct hash_speed *speed)
Herbert Xue9d41162006-08-19 21:38:49 +1000417{
Herbert Xuf139cfa2008-07-31 12:23:53 +0800418 struct scatterlist sg[TVMEMSIZE];
Herbert Xue9d41162006-08-19 21:38:49 +1000419 struct crypto_hash *tfm;
420 struct hash_desc desc;
Frank Seidel376bacb2009-03-29 15:18:39 +0800421 static char output[1024];
Michal Ludvige8057922006-05-30 22:04:19 +1000422 int i;
Herbert Xue9d41162006-08-19 21:38:49 +1000423 int ret;
Michal Ludvige8057922006-05-30 22:04:19 +1000424
Frank Seidel376bacb2009-03-29 15:18:39 +0800425 printk(KERN_INFO "\ntesting speed of %s\n", algo);
Michal Ludvige8057922006-05-30 22:04:19 +1000426
Herbert Xue9d41162006-08-19 21:38:49 +1000427 tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
Michal Ludvige8057922006-05-30 22:04:19 +1000428
Herbert Xue9d41162006-08-19 21:38:49 +1000429 if (IS_ERR(tfm)) {
Frank Seidel376bacb2009-03-29 15:18:39 +0800430 printk(KERN_ERR "failed to load transform for %s: %ld\n", algo,
Herbert Xue9d41162006-08-19 21:38:49 +1000431 PTR_ERR(tfm));
Michal Ludvige8057922006-05-30 22:04:19 +1000432 return;
433 }
434
Herbert Xue9d41162006-08-19 21:38:49 +1000435 desc.tfm = tfm;
436 desc.flags = 0;
437
438 if (crypto_hash_digestsize(tfm) > sizeof(output)) {
Frank Seidel376bacb2009-03-29 15:18:39 +0800439 printk(KERN_ERR "digestsize(%u) > outputbuffer(%zu)\n",
Herbert Xue9d41162006-08-19 21:38:49 +1000440 crypto_hash_digestsize(tfm), sizeof(output));
Michal Ludvige8057922006-05-30 22:04:19 +1000441 goto out;
442 }
443
David S. Millerbeb63da2010-05-19 14:11:21 +1000444 test_hash_sg_init(sg);
Michal Ludvige8057922006-05-30 22:04:19 +1000445 for (i = 0; speed[i].blen != 0; i++) {
Herbert Xuf139cfa2008-07-31 12:23:53 +0800446 if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
Frank Seidel376bacb2009-03-29 15:18:39 +0800447 printk(KERN_ERR
448 "template (%u) too big for tvmem (%lu)\n",
Herbert Xuf139cfa2008-07-31 12:23:53 +0800449 speed[i].blen, TVMEMSIZE * PAGE_SIZE);
Michal Ludvige8057922006-05-30 22:04:19 +1000450 goto out;
451 }
452
Huang Ying18bcc912010-03-10 18:30:32 +0800453 if (speed[i].klen)
454 crypto_hash_setkey(tfm, tvmem[0], speed[i].klen);
455
Frank Seidel376bacb2009-03-29 15:18:39 +0800456 printk(KERN_INFO "test%3u "
457 "(%5u byte blocks,%5u bytes per update,%4u updates): ",
Michal Ludvige8057922006-05-30 22:04:19 +1000458 i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
459
Michal Ludvige8057922006-05-30 22:04:19 +1000460 if (sec)
Herbert Xuf139cfa2008-07-31 12:23:53 +0800461 ret = test_hash_jiffies(&desc, sg, speed[i].blen,
Herbert Xue9d41162006-08-19 21:38:49 +1000462 speed[i].plen, output, sec);
Michal Ludvige8057922006-05-30 22:04:19 +1000463 else
Herbert Xuf139cfa2008-07-31 12:23:53 +0800464 ret = test_hash_cycles(&desc, sg, speed[i].blen,
Herbert Xue9d41162006-08-19 21:38:49 +1000465 speed[i].plen, output);
466
467 if (ret) {
Frank Seidel376bacb2009-03-29 15:18:39 +0800468 printk(KERN_ERR "hashing failed ret=%d\n", ret);
Herbert Xue9d41162006-08-19 21:38:49 +1000469 break;
470 }
Michal Ludvige8057922006-05-30 22:04:19 +1000471 }
472
473out:
Herbert Xue9d41162006-08-19 21:38:49 +1000474 crypto_free_hash(tfm);
Michal Ludvige8057922006-05-30 22:04:19 +1000475}
476
David S. Millerbeb63da2010-05-19 14:11:21 +1000477struct tcrypt_result {
478 struct completion completion;
479 int err;
480};
481
482static void tcrypt_complete(struct crypto_async_request *req, int err)
483{
484 struct tcrypt_result *res = req->data;
485
486 if (err == -EINPROGRESS)
487 return;
488
489 res->err = err;
490 complete(&res->completion);
491}
492
493static inline int do_one_ahash_op(struct ahash_request *req, int ret)
494{
495 if (ret == -EINPROGRESS || ret == -EBUSY) {
496 struct tcrypt_result *tr = req->base.data;
497
498 ret = wait_for_completion_interruptible(&tr->completion);
499 if (!ret)
500 ret = tr->err;
501 INIT_COMPLETION(tr->completion);
502 }
503 return ret;
504}
505
506static int test_ahash_jiffies_digest(struct ahash_request *req, int blen,
507 char *out, int sec)
508{
509 unsigned long start, end;
510 int bcount;
511 int ret;
512
513 for (start = jiffies, end = start + sec * HZ, bcount = 0;
514 time_before(jiffies, end); bcount++) {
515 ret = do_one_ahash_op(req, crypto_ahash_digest(req));
516 if (ret)
517 return ret;
518 }
519
520 printk("%6u opers/sec, %9lu bytes/sec\n",
521 bcount / sec, ((long)bcount * blen) / sec);
522
523 return 0;
524}
525
526static int test_ahash_jiffies(struct ahash_request *req, int blen,
527 int plen, char *out, int sec)
528{
529 unsigned long start, end;
530 int bcount, pcount;
531 int ret;
532
533 if (plen == blen)
534 return test_ahash_jiffies_digest(req, blen, out, sec);
535
536 for (start = jiffies, end = start + sec * HZ, bcount = 0;
537 time_before(jiffies, end); bcount++) {
538 ret = crypto_ahash_init(req);
539 if (ret)
540 return ret;
541 for (pcount = 0; pcount < blen; pcount += plen) {
542 ret = do_one_ahash_op(req, crypto_ahash_update(req));
543 if (ret)
544 return ret;
545 }
546 /* we assume there is enough space in 'out' for the result */
547 ret = do_one_ahash_op(req, crypto_ahash_final(req));
548 if (ret)
549 return ret;
550 }
551
552 pr_cont("%6u opers/sec, %9lu bytes/sec\n",
553 bcount / sec, ((long)bcount * blen) / sec);
554
555 return 0;
556}
557
558static int test_ahash_cycles_digest(struct ahash_request *req, int blen,
559 char *out)
560{
561 unsigned long cycles = 0;
562 int ret, i;
563
564 /* Warm-up run. */
565 for (i = 0; i < 4; i++) {
566 ret = do_one_ahash_op(req, crypto_ahash_digest(req));
567 if (ret)
568 goto out;
569 }
570
571 /* The real thing. */
572 for (i = 0; i < 8; i++) {
573 cycles_t start, end;
574
575 start = get_cycles();
576
577 ret = do_one_ahash_op(req, crypto_ahash_digest(req));
578 if (ret)
579 goto out;
580
581 end = get_cycles();
582
583 cycles += end - start;
584 }
585
586out:
587 if (ret)
588 return ret;
589
590 pr_cont("%6lu cycles/operation, %4lu cycles/byte\n",
591 cycles / 8, cycles / (8 * blen));
592
593 return 0;
594}
595
596static int test_ahash_cycles(struct ahash_request *req, int blen,
597 int plen, char *out)
598{
599 unsigned long cycles = 0;
600 int i, pcount, ret;
601
602 if (plen == blen)
603 return test_ahash_cycles_digest(req, blen, out);
604
605 /* Warm-up run. */
606 for (i = 0; i < 4; i++) {
607 ret = crypto_ahash_init(req);
608 if (ret)
609 goto out;
610 for (pcount = 0; pcount < blen; pcount += plen) {
611 ret = do_one_ahash_op(req, crypto_ahash_update(req));
612 if (ret)
613 goto out;
614 }
615 ret = do_one_ahash_op(req, crypto_ahash_final(req));
616 if (ret)
617 goto out;
618 }
619
620 /* The real thing. */
621 for (i = 0; i < 8; i++) {
622 cycles_t start, end;
623
624 start = get_cycles();
625
626 ret = crypto_ahash_init(req);
627 if (ret)
628 goto out;
629 for (pcount = 0; pcount < blen; pcount += plen) {
630 ret = do_one_ahash_op(req, crypto_ahash_update(req));
631 if (ret)
632 goto out;
633 }
634 ret = do_one_ahash_op(req, crypto_ahash_final(req));
635 if (ret)
636 goto out;
637
638 end = get_cycles();
639
640 cycles += end - start;
641 }
642
643out:
644 if (ret)
645 return ret;
646
647 pr_cont("%6lu cycles/operation, %4lu cycles/byte\n",
648 cycles / 8, cycles / (8 * blen));
649
650 return 0;
651}
652
653static void test_ahash_speed(const char *algo, unsigned int sec,
654 struct hash_speed *speed)
655{
656 struct scatterlist sg[TVMEMSIZE];
657 struct tcrypt_result tresult;
658 struct ahash_request *req;
659 struct crypto_ahash *tfm;
660 static char output[1024];
661 int i, ret;
662
663 printk(KERN_INFO "\ntesting speed of async %s\n", algo);
664
665 tfm = crypto_alloc_ahash(algo, 0, 0);
666 if (IS_ERR(tfm)) {
667 pr_err("failed to load transform for %s: %ld\n",
668 algo, PTR_ERR(tfm));
669 return;
670 }
671
672 if (crypto_ahash_digestsize(tfm) > sizeof(output)) {
673 pr_err("digestsize(%u) > outputbuffer(%zu)\n",
674 crypto_ahash_digestsize(tfm), sizeof(output));
675 goto out;
676 }
677
678 test_hash_sg_init(sg);
679 req = ahash_request_alloc(tfm, GFP_KERNEL);
680 if (!req) {
681 pr_err("ahash request allocation failure\n");
682 goto out;
683 }
684
685 init_completion(&tresult.completion);
686 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
687 tcrypt_complete, &tresult);
688
689 for (i = 0; speed[i].blen != 0; i++) {
690 if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
691 pr_err("template (%u) too big for tvmem (%lu)\n",
692 speed[i].blen, TVMEMSIZE * PAGE_SIZE);
693 break;
694 }
695
696 pr_info("test%3u "
697 "(%5u byte blocks,%5u bytes per update,%4u updates): ",
698 i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
699
700 ahash_request_set_crypt(req, sg, output, speed[i].plen);
701
702 if (sec)
703 ret = test_ahash_jiffies(req, speed[i].blen,
704 speed[i].plen, output, sec);
705 else
706 ret = test_ahash_cycles(req, speed[i].blen,
707 speed[i].plen, output);
708
709 if (ret) {
710 pr_err("hashing failed ret=%d\n", ret);
711 break;
712 }
713 }
714
715 ahash_request_free(req);
716
717out:
718 crypto_free_ahash(tfm);
719}
720
Herbert Xuef2736f2005-06-22 13:26:03 -0700721static void test_available(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 char **name = check;
Herbert Xuef2736f2005-06-22 13:26:03 -0700724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 while (*name) {
726 printk("alg %s ", *name);
Herbert Xu6158efc2007-04-04 17:41:07 +1000727 printk(crypto_has_alg(*name, 0, 0) ?
Herbert Xue4d5b792006-08-26 18:12:40 +1000728 "found\n" : "not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 name++;
Herbert Xuef2736f2005-06-22 13:26:03 -0700730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Herbert Xu01b32322008-07-31 15:41:55 +0800733static inline int tcrypt_test(const char *alg)
734{
Jarod Wilson4e033a62009-05-27 15:10:21 +1000735 int ret;
736
737 ret = alg_test(alg, alg, 0, 0);
738 /* non-fips algs return -EINVAL in fips mode */
739 if (fips_enabled && ret == -EINVAL)
740 ret = 0;
741 return ret;
Herbert Xu01b32322008-07-31 15:41:55 +0800742}
743
Jarod Wilson4e033a62009-05-27 15:10:21 +1000744static int do_test(int m)
Herbert Xu01b32322008-07-31 15:41:55 +0800745{
746 int i;
Jarod Wilson4e033a62009-05-27 15:10:21 +1000747 int ret = 0;
Herbert Xu01b32322008-07-31 15:41:55 +0800748
749 switch (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 case 0:
Herbert Xu01b32322008-07-31 15:41:55 +0800751 for (i = 1; i < 200; i++)
Jarod Wilson4e033a62009-05-27 15:10:21 +1000752 ret += do_test(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 break;
754
755 case 1:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000756 ret += tcrypt_test("md5");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 break;
758
759 case 2:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000760 ret += tcrypt_test("sha1");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 break;
762
763 case 3:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000764 ret += tcrypt_test("ecb(des)");
765 ret += tcrypt_test("cbc(des)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 break;
767
768 case 4:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000769 ret += tcrypt_test("ecb(des3_ede)");
770 ret += tcrypt_test("cbc(des3_ede)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 break;
772
773 case 5:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000774 ret += tcrypt_test("md4");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 break;
Herbert Xuef2736f2005-06-22 13:26:03 -0700776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 case 6:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000778 ret += tcrypt_test("sha256");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 break;
Herbert Xuef2736f2005-06-22 13:26:03 -0700780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 case 7:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000782 ret += tcrypt_test("ecb(blowfish)");
783 ret += tcrypt_test("cbc(blowfish)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 break;
785
786 case 8:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000787 ret += tcrypt_test("ecb(twofish)");
788 ret += tcrypt_test("cbc(twofish)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 break;
Herbert Xuef2736f2005-06-22 13:26:03 -0700790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 case 9:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000792 ret += tcrypt_test("ecb(serpent)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 break;
794
795 case 10:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000796 ret += tcrypt_test("ecb(aes)");
797 ret += tcrypt_test("cbc(aes)");
798 ret += tcrypt_test("lrw(aes)");
799 ret += tcrypt_test("xts(aes)");
800 ret += tcrypt_test("ctr(aes)");
801 ret += tcrypt_test("rfc3686(ctr(aes))");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 break;
803
804 case 11:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000805 ret += tcrypt_test("sha384");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 break;
Herbert Xuef2736f2005-06-22 13:26:03 -0700807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 case 12:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000809 ret += tcrypt_test("sha512");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 break;
811
812 case 13:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000813 ret += tcrypt_test("deflate");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 break;
815
816 case 14:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000817 ret += tcrypt_test("ecb(cast5)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 break;
819
820 case 15:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000821 ret += tcrypt_test("ecb(cast6)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 break;
823
824 case 16:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000825 ret += tcrypt_test("ecb(arc4)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 break;
827
828 case 17:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000829 ret += tcrypt_test("michael_mic");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 break;
831
832 case 18:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000833 ret += tcrypt_test("crc32c");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 break;
835
836 case 19:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000837 ret += tcrypt_test("ecb(tea)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 break;
839
840 case 20:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000841 ret += tcrypt_test("ecb(xtea)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 break;
843
844 case 21:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000845 ret += tcrypt_test("ecb(khazad)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 break;
847
848 case 22:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000849 ret += tcrypt_test("wp512");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 break;
851
852 case 23:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000853 ret += tcrypt_test("wp384");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 break;
855
856 case 24:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000857 ret += tcrypt_test("wp256");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 break;
859
860 case 25:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000861 ret += tcrypt_test("ecb(tnepres)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 break;
863
864 case 26:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000865 ret += tcrypt_test("ecb(anubis)");
866 ret += tcrypt_test("cbc(anubis)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 break;
868
869 case 27:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000870 ret += tcrypt_test("tgr192");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 break;
872
873 case 28:
874
Jarod Wilson4e033a62009-05-27 15:10:21 +1000875 ret += tcrypt_test("tgr160");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 break;
877
878 case 29:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000879 ret += tcrypt_test("tgr128");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 break;
Adrian-Ken Rueegsegger2998db32008-05-09 21:29:35 +0800881
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700882 case 30:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000883 ret += tcrypt_test("ecb(xeta)");
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700884 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
David Howells90831632006-12-16 12:13:14 +1100886 case 31:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000887 ret += tcrypt_test("pcbc(fcrypt)");
David Howells90831632006-12-16 12:13:14 +1100888 break;
889
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +1100890 case 32:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000891 ret += tcrypt_test("ecb(camellia)");
892 ret += tcrypt_test("cbc(camellia)");
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +1100893 break;
Jonathan Lynchcd12fb902007-11-10 20:08:25 +0800894 case 33:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000895 ret += tcrypt_test("sha224");
Jonathan Lynchcd12fb902007-11-10 20:08:25 +0800896 break;
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +1100897
Tan Swee Heng2407d602007-11-23 19:45:00 +0800898 case 34:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000899 ret += tcrypt_test("salsa20");
Tan Swee Heng2407d602007-11-23 19:45:00 +0800900 break;
901
Herbert Xu8df213d2007-12-02 14:55:47 +1100902 case 35:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000903 ret += tcrypt_test("gcm(aes)");
Herbert Xu8df213d2007-12-02 14:55:47 +1100904 break;
905
Zoltan Sogor0b77abb2007-12-07 16:53:23 +0800906 case 36:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000907 ret += tcrypt_test("lzo");
Zoltan Sogor0b77abb2007-12-07 16:53:23 +0800908 break;
909
Joy Latten93cc74e2007-12-12 20:24:22 +0800910 case 37:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000911 ret += tcrypt_test("ccm(aes)");
Joy Latten93cc74e2007-12-12 20:24:22 +0800912 break;
913
Kevin Coffman76cb9522008-03-24 21:26:16 +0800914 case 38:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000915 ret += tcrypt_test("cts(cbc(aes))");
Kevin Coffman76cb9522008-03-24 21:26:16 +0800916 break;
917
Adrian-Ken Rueegseggerfd4adf12008-05-07 22:16:36 +0800918 case 39:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000919 ret += tcrypt_test("rmd128");
Adrian-Ken Rueegseggerfd4adf12008-05-07 22:16:36 +0800920 break;
921
922 case 40:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000923 ret += tcrypt_test("rmd160");
Adrian-Ken Rueegseggerfd4adf12008-05-07 22:16:36 +0800924 break;
925
Adrian-Ken Rueegsegger2998db32008-05-09 21:29:35 +0800926 case 41:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000927 ret += tcrypt_test("rmd256");
Adrian-Ken Rueegsegger2998db32008-05-09 21:29:35 +0800928 break;
929
930 case 42:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000931 ret += tcrypt_test("rmd320");
Herbert Xu01b32322008-07-31 15:41:55 +0800932 break;
933
934 case 43:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000935 ret += tcrypt_test("ecb(seed)");
Adrian-Ken Rueegsegger2998db32008-05-09 21:29:35 +0800936 break;
937
Geert Uytterhoeven0c01aed2009-03-04 15:42:15 +0800938 case 44:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000939 ret += tcrypt_test("zlib");
Geert Uytterhoeven0c01aed2009-03-04 15:42:15 +0800940 break;
941
Jarod Wilson5d667322009-05-04 19:23:40 +0800942 case 45:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000943 ret += tcrypt_test("rfc4309(ccm(aes))");
Jarod Wilson5d667322009-05-04 19:23:40 +0800944 break;
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 case 100:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000947 ret += tcrypt_test("hmac(md5)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 break;
Herbert Xuef2736f2005-06-22 13:26:03 -0700949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 case 101:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000951 ret += tcrypt_test("hmac(sha1)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 break;
Herbert Xuef2736f2005-06-22 13:26:03 -0700953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 case 102:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000955 ret += tcrypt_test("hmac(sha256)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 break;
957
Andrew Donofrioa28091a2006-12-10 12:10:20 +1100958 case 103:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000959 ret += tcrypt_test("hmac(sha384)");
Andrew Donofrioa28091a2006-12-10 12:10:20 +1100960 break;
961
962 case 104:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000963 ret += tcrypt_test("hmac(sha512)");
Andrew Donofrioa28091a2006-12-10 12:10:20 +1100964 break;
Herbert Xu38ed9ab2008-01-01 15:59:28 +1100965
Jonathan Lynchcd12fb902007-11-10 20:08:25 +0800966 case 105:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000967 ret += tcrypt_test("hmac(sha224)");
Jonathan Lynchcd12fb902007-11-10 20:08:25 +0800968 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Herbert Xu38ed9ab2008-01-01 15:59:28 +1100970 case 106:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000971 ret += tcrypt_test("xcbc(aes)");
Herbert Xu38ed9ab2008-01-01 15:59:28 +1100972 break;
973
Adrian-Ken Rueegseggerfd4adf12008-05-07 22:16:36 +0800974 case 107:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000975 ret += tcrypt_test("hmac(rmd128)");
Adrian-Ken Rueegseggerfd4adf12008-05-07 22:16:36 +0800976 break;
977
978 case 108:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000979 ret += tcrypt_test("hmac(rmd160)");
Adrian-Ken Rueegseggerfd4adf12008-05-07 22:16:36 +0800980 break;
981
Shane Wangf1939f72009-09-02 20:05:22 +1000982 case 109:
983 ret += tcrypt_test("vmac(aes)");
984 break;
985
Jarod Wilsone08ca2d2009-05-04 19:46:29 +0800986 case 150:
Jarod Wilson4e033a62009-05-27 15:10:21 +1000987 ret += tcrypt_test("ansi_cprng");
Jarod Wilsone08ca2d2009-05-04 19:46:29 +0800988 break;
989
Adrian Hoban69435b92010-11-04 15:02:04 -0400990 case 151:
991 ret += tcrypt_test("rfc4106(gcm(aes))");
992 break;
993
Harald Welteebfd9bc2005-06-22 13:27:23 -0700994 case 200:
Herbert Xucba83562006-08-13 08:26:09 +1000995 test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800996 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +1000997 test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +0800998 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +1000999 test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001000 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +10001001 test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001002 speed_template_16_24_32);
Rik Snelf3d10442006-11-29 19:01:41 +11001003 test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001004 speed_template_32_40_48);
Rik Snelf3d10442006-11-29 19:01:41 +11001005 test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001006 speed_template_32_40_48);
Rik Snelf19f5112007-09-19 20:23:13 +08001007 test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001008 speed_template_32_48_64);
Rik Snelf19f5112007-09-19 20:23:13 +08001009 test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001010 speed_template_32_48_64);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001011 break;
1012
1013 case 201:
Herbert Xucba83562006-08-13 08:26:09 +10001014 test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
Herbert Xuda7f0332008-07-31 17:08:25 +08001015 des3_speed_template, DES3_SPEED_VECTORS,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001016 speed_template_24);
Herbert Xucba83562006-08-13 08:26:09 +10001017 test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
Herbert Xuda7f0332008-07-31 17:08:25 +08001018 des3_speed_template, DES3_SPEED_VECTORS,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001019 speed_template_24);
Herbert Xucba83562006-08-13 08:26:09 +10001020 test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
Herbert Xuda7f0332008-07-31 17:08:25 +08001021 des3_speed_template, DES3_SPEED_VECTORS,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001022 speed_template_24);
Herbert Xucba83562006-08-13 08:26:09 +10001023 test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
Herbert Xuda7f0332008-07-31 17:08:25 +08001024 des3_speed_template, DES3_SPEED_VECTORS,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001025 speed_template_24);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001026 break;
1027
1028 case 202:
Herbert Xucba83562006-08-13 08:26:09 +10001029 test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001030 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +10001031 test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001032 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +10001033 test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001034 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +10001035 test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001036 speed_template_16_24_32);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001037 break;
1038
1039 case 203:
Herbert Xucba83562006-08-13 08:26:09 +10001040 test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001041 speed_template_8_32);
Herbert Xucba83562006-08-13 08:26:09 +10001042 test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001043 speed_template_8_32);
Herbert Xucba83562006-08-13 08:26:09 +10001044 test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001045 speed_template_8_32);
Herbert Xucba83562006-08-13 08:26:09 +10001046 test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001047 speed_template_8_32);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001048 break;
1049
1050 case 204:
Herbert Xucba83562006-08-13 08:26:09 +10001051 test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001052 speed_template_8);
Herbert Xucba83562006-08-13 08:26:09 +10001053 test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001054 speed_template_8);
Herbert Xucba83562006-08-13 08:26:09 +10001055 test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001056 speed_template_8);
Herbert Xucba83562006-08-13 08:26:09 +10001057 test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001058 speed_template_8);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001059 break;
1060
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +11001061 case 205:
1062 test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001063 speed_template_16_24_32);
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +11001064 test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001065 speed_template_16_24_32);
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +11001066 test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001067 speed_template_16_24_32);
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +11001068 test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001069 speed_template_16_24_32);
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +11001070 break;
1071
Tan Swee Heng5de8f1b2007-12-07 17:17:43 +08001072 case 206:
1073 test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001074 speed_template_16_32);
Tan Swee Heng5de8f1b2007-12-07 17:17:43 +08001075 break;
1076
Michal Ludvige8057922006-05-30 22:04:19 +10001077 case 300:
1078 /* fall through */
1079
1080 case 301:
Herbert Xue9d41162006-08-19 21:38:49 +10001081 test_hash_speed("md4", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001082 if (mode > 300 && mode < 400) break;
1083
1084 case 302:
Herbert Xue9d41162006-08-19 21:38:49 +10001085 test_hash_speed("md5", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001086 if (mode > 300 && mode < 400) break;
1087
1088 case 303:
Herbert Xue9d41162006-08-19 21:38:49 +10001089 test_hash_speed("sha1", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001090 if (mode > 300 && mode < 400) break;
1091
1092 case 304:
Herbert Xue9d41162006-08-19 21:38:49 +10001093 test_hash_speed("sha256", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001094 if (mode > 300 && mode < 400) break;
1095
1096 case 305:
Herbert Xue9d41162006-08-19 21:38:49 +10001097 test_hash_speed("sha384", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001098 if (mode > 300 && mode < 400) break;
1099
1100 case 306:
Herbert Xue9d41162006-08-19 21:38:49 +10001101 test_hash_speed("sha512", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001102 if (mode > 300 && mode < 400) break;
1103
1104 case 307:
Herbert Xue9d41162006-08-19 21:38:49 +10001105 test_hash_speed("wp256", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001106 if (mode > 300 && mode < 400) break;
1107
1108 case 308:
Herbert Xue9d41162006-08-19 21:38:49 +10001109 test_hash_speed("wp384", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001110 if (mode > 300 && mode < 400) break;
1111
1112 case 309:
Herbert Xue9d41162006-08-19 21:38:49 +10001113 test_hash_speed("wp512", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001114 if (mode > 300 && mode < 400) break;
1115
1116 case 310:
Herbert Xue9d41162006-08-19 21:38:49 +10001117 test_hash_speed("tgr128", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001118 if (mode > 300 && mode < 400) break;
1119
1120 case 311:
Herbert Xue9d41162006-08-19 21:38:49 +10001121 test_hash_speed("tgr160", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001122 if (mode > 300 && mode < 400) break;
1123
1124 case 312:
Herbert Xue9d41162006-08-19 21:38:49 +10001125 test_hash_speed("tgr192", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001126 if (mode > 300 && mode < 400) break;
1127
Jonathan Lynchcd12fb902007-11-10 20:08:25 +08001128 case 313:
1129 test_hash_speed("sha224", sec, generic_hash_speed_template);
1130 if (mode > 300 && mode < 400) break;
1131
Adrian-Ken Rueegseggerfd4adf12008-05-07 22:16:36 +08001132 case 314:
1133 test_hash_speed("rmd128", sec, generic_hash_speed_template);
1134 if (mode > 300 && mode < 400) break;
1135
1136 case 315:
1137 test_hash_speed("rmd160", sec, generic_hash_speed_template);
1138 if (mode > 300 && mode < 400) break;
1139
Adrian-Ken Rueegsegger2998db32008-05-09 21:29:35 +08001140 case 316:
1141 test_hash_speed("rmd256", sec, generic_hash_speed_template);
1142 if (mode > 300 && mode < 400) break;
1143
1144 case 317:
1145 test_hash_speed("rmd320", sec, generic_hash_speed_template);
1146 if (mode > 300 && mode < 400) break;
1147
Huang Ying18bcc912010-03-10 18:30:32 +08001148 case 318:
1149 test_hash_speed("ghash-generic", sec, hash_speed_template_16);
1150 if (mode > 300 && mode < 400) break;
1151
Michal Ludvige8057922006-05-30 22:04:19 +10001152 case 399:
1153 break;
1154
David S. Millerbeb63da2010-05-19 14:11:21 +10001155 case 400:
1156 /* fall through */
1157
1158 case 401:
1159 test_ahash_speed("md4", sec, generic_hash_speed_template);
1160 if (mode > 400 && mode < 500) break;
1161
1162 case 402:
1163 test_ahash_speed("md5", sec, generic_hash_speed_template);
1164 if (mode > 400 && mode < 500) break;
1165
1166 case 403:
1167 test_ahash_speed("sha1", sec, generic_hash_speed_template);
1168 if (mode > 400 && mode < 500) break;
1169
1170 case 404:
1171 test_ahash_speed("sha256", sec, generic_hash_speed_template);
1172 if (mode > 400 && mode < 500) break;
1173
1174 case 405:
1175 test_ahash_speed("sha384", sec, generic_hash_speed_template);
1176 if (mode > 400 && mode < 500) break;
1177
1178 case 406:
1179 test_ahash_speed("sha512", sec, generic_hash_speed_template);
1180 if (mode > 400 && mode < 500) break;
1181
1182 case 407:
1183 test_ahash_speed("wp256", sec, generic_hash_speed_template);
1184 if (mode > 400 && mode < 500) break;
1185
1186 case 408:
1187 test_ahash_speed("wp384", sec, generic_hash_speed_template);
1188 if (mode > 400 && mode < 500) break;
1189
1190 case 409:
1191 test_ahash_speed("wp512", sec, generic_hash_speed_template);
1192 if (mode > 400 && mode < 500) break;
1193
1194 case 410:
1195 test_ahash_speed("tgr128", sec, generic_hash_speed_template);
1196 if (mode > 400 && mode < 500) break;
1197
1198 case 411:
1199 test_ahash_speed("tgr160", sec, generic_hash_speed_template);
1200 if (mode > 400 && mode < 500) break;
1201
1202 case 412:
1203 test_ahash_speed("tgr192", sec, generic_hash_speed_template);
1204 if (mode > 400 && mode < 500) break;
1205
1206 case 413:
1207 test_ahash_speed("sha224", sec, generic_hash_speed_template);
1208 if (mode > 400 && mode < 500) break;
1209
1210 case 414:
1211 test_ahash_speed("rmd128", sec, generic_hash_speed_template);
1212 if (mode > 400 && mode < 500) break;
1213
1214 case 415:
1215 test_ahash_speed("rmd160", sec, generic_hash_speed_template);
1216 if (mode > 400 && mode < 500) break;
1217
1218 case 416:
1219 test_ahash_speed("rmd256", sec, generic_hash_speed_template);
1220 if (mode > 400 && mode < 500) break;
1221
1222 case 417:
1223 test_ahash_speed("rmd320", sec, generic_hash_speed_template);
1224 if (mode > 400 && mode < 500) break;
1225
1226 case 499:
1227 break;
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 case 1000:
1230 test_available();
1231 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
Jarod Wilson4e033a62009-05-27 15:10:21 +10001233
1234 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
Herbert Xu7be380f2009-07-14 16:06:54 +08001237static int do_alg_test(const char *alg, u32 type, u32 mask)
Steffen Klasserta873a5f2009-06-19 19:46:53 +08001238{
Herbert Xu7be380f2009-07-14 16:06:54 +08001239 return crypto_has_alg(alg, type, mask ?: CRYPTO_ALG_TYPE_MASK) ?
1240 0 : -ENOENT;
Steffen Klasserta873a5f2009-06-19 19:46:53 +08001241}
1242
Kamalesh Babulal3af5b902008-04-05 21:00:57 +08001243static int __init tcrypt_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Mikko Herranene3a4ea42007-11-26 22:12:07 +08001245 int err = -ENOMEM;
Herbert Xuf139cfa2008-07-31 12:23:53 +08001246 int i;
Mikko Herranene3a4ea42007-11-26 22:12:07 +08001247
Herbert Xuf139cfa2008-07-31 12:23:53 +08001248 for (i = 0; i < TVMEMSIZE; i++) {
1249 tvmem[i] = (void *)__get_free_page(GFP_KERNEL);
1250 if (!tvmem[i])
1251 goto err_free_tv;
1252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Steffen Klasserta873a5f2009-06-19 19:46:53 +08001254 if (alg)
Herbert Xu7be380f2009-07-14 16:06:54 +08001255 err = do_alg_test(alg, type, mask);
Steffen Klasserta873a5f2009-06-19 19:46:53 +08001256 else
1257 err = do_test(mode);
1258
Jarod Wilson4e033a62009-05-27 15:10:21 +10001259 if (err) {
1260 printk(KERN_ERR "tcrypt: one or more tests failed!\n");
1261 goto err_free_tv;
1262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Jarod Wilson4e033a62009-05-27 15:10:21 +10001264 /* We intentionaly return -EAGAIN to prevent keeping the module,
1265 * unless we're running in fips mode. It does all its work from
1266 * init() and doesn't offer any runtime functionality, but in
1267 * the fips case, checking for a successful load is helpful.
Michal Ludvig14fdf472006-05-30 14:49:38 +10001268 * => we don't need it in the memory, do we?
1269 * -- mludvig
1270 */
Jarod Wilson4e033a62009-05-27 15:10:21 +10001271 if (!fips_enabled)
1272 err = -EAGAIN;
Mikko Herranene3a4ea42007-11-26 22:12:07 +08001273
Herbert Xuf139cfa2008-07-31 12:23:53 +08001274err_free_tv:
1275 for (i = 0; i < TVMEMSIZE && tvmem[i]; i++)
1276 free_page((unsigned long)tvmem[i]);
Mikko Herranene3a4ea42007-11-26 22:12:07 +08001277
1278 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
1281/*
1282 * If an init function is provided, an exit function must also be provided
1283 * to allow module unload.
1284 */
Kamalesh Babulal3af5b902008-04-05 21:00:57 +08001285static void __exit tcrypt_mod_fini(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Kamalesh Babulal3af5b902008-04-05 21:00:57 +08001287module_init(tcrypt_mod_init);
1288module_exit(tcrypt_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Steffen Klasserta873a5f2009-06-19 19:46:53 +08001290module_param(alg, charp, 0);
1291module_param(type, uint, 0);
Herbert Xu7be380f2009-07-14 16:06:54 +08001292module_param(mask, uint, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293module_param(mode, int, 0);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001294module_param(sec, uint, 0);
Herbert Xu6a179442005-06-22 13:29:03 -07001295MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
1296 "(defaults to zero which uses CPU cycles instead)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298MODULE_LICENSE("GPL");
1299MODULE_DESCRIPTION("Quick & dirty crypto testing module");
1300MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");