blob: e47f6e02133c155622d477567d88f890489f151f [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 *
Mikko Herranen28db8e32007-11-26 22:24:11 +080016 * 2007-11-13 Added GCM tests
Mikko Herranene3a4ea42007-11-26 22:12:07 +080017 * 2007-11-13 Added AEAD support
Jonathan Lynchcd12fb92007-11-10 20:08:25 +080018 * 2007-11-06 Added SHA-224 and SHA-224-HMAC tests
Andrew Donofrioa28091a2006-12-10 12:10:20 +110019 * 2006-12-07 Added SHA384 HMAC and SHA512 HMAC tests
Harald Welteebfd9bc2005-06-22 13:27:23 -070020 * 2004-08-09 Added cipher speed tests (Reyk Floeter <reyk@vantronix.net>)
21 * 2003-09-14 Rewritten by Kartikey Mahendra Bhatt
22 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
Herbert Xucba83562006-08-13 08:26:09 +100025#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/init.h>
27#include <linux/module.h>
28#include <linux/mm.h>
29#include <linux/slab.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>
32#include <linux/crypto.h>
33#include <linux/highmem.h>
34#include <linux/moduleparam.h>
Harald Welteebfd9bc2005-06-22 13:27:23 -070035#include <linux/jiffies.h>
Herbert Xu6a179442005-06-22 13:29:03 -070036#include <linux/timex.h>
37#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "tcrypt.h"
39
40/*
41 * Need to kmalloc() memory for testing kmap().
42 */
Harald Welteebfd9bc2005-06-22 13:27:23 -070043#define TVMEMSIZE 16384
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define XBUFSIZE 32768
45
46/*
47 * Indexes into the xbuf to simulate cross-page access.
48 */
49#define IDX1 37
50#define IDX2 32400
51#define IDX3 1
52#define IDX4 8193
53#define IDX5 22222
54#define IDX6 17101
55#define IDX7 27333
56#define IDX8 3000
57
58/*
59* Used by test_cipher()
60*/
61#define ENCRYPT 1
62#define DECRYPT 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Herbert Xu6158efc2007-04-04 17:41:07 +100064struct tcrypt_result {
65 struct completion completion;
66 int err;
67};
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
70
Harald Welteebfd9bc2005-06-22 13:27:23 -070071/*
72 * Used by test_cipher_speed()
73 */
Herbert Xu6a179442005-06-22 13:29:03 -070074static unsigned int sec;
Harald Welteebfd9bc2005-06-22 13:27:23 -070075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static int mode;
77static char *xbuf;
Mikko Herranene3a4ea42007-11-26 22:12:07 +080078static char *axbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static char *tvmem;
80
81static char *check[] = {
Jonathan Lynchcd12fb92007-11-10 20:08:25 +080082 "des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
83 "blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
84 "cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
David Howells90831632006-12-16 12:13:14 +110085 "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", "fcrypt",
Kevin Coffman76cb9522008-03-24 21:26:16 +080086 "camellia", "seed", "salsa20", "lzo", "cts", NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -070087};
88
Herbert Xuef2736f2005-06-22 13:26:03 -070089static void hexdump(unsigned char *buf, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Denis Chenga10e1192007-11-30 16:59:30 +110091 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
92 16, 1,
93 buf, len, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Herbert Xu6158efc2007-04-04 17:41:07 +100096static void tcrypt_complete(struct crypto_async_request *req, int err)
97{
98 struct tcrypt_result *res = req->data;
99
100 if (err == -EINPROGRESS)
101 return;
102
103 res->err = err;
104 complete(&res->completion);
105}
106
Herbert Xuef2736f2005-06-22 13:26:03 -0700107static void test_hash(char *algo, struct hash_testvec *template,
108 unsigned int tcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Herbert Xuef2736f2005-06-22 13:26:03 -0700110 unsigned int i, j, k, temp;
111 struct scatterlist sg[8];
112 char result[64];
Herbert Xue9d41162006-08-19 21:38:49 +1000113 struct crypto_hash *tfm;
114 struct hash_desc desc;
Herbert Xue9d41162006-08-19 21:38:49 +1000115 int ret;
Sebastian Siewior562954d2008-03-13 20:20:28 +0800116 void *hash_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Herbert Xuef2736f2005-06-22 13:26:03 -0700118 printk("\ntesting %s\n", algo);
119
Herbert Xue9d41162006-08-19 21:38:49 +1000120 tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
121 if (IS_ERR(tfm)) {
122 printk("failed to load transform for %s: %ld\n", algo,
123 PTR_ERR(tfm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return;
125 }
126
Herbert Xue9d41162006-08-19 21:38:49 +1000127 desc.tfm = tfm;
128 desc.flags = 0;
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 for (i = 0; i < tcount; i++) {
Herbert Xuef2736f2005-06-22 13:26:03 -0700131 printk("test %u:\n", i + 1);
132 memset(result, 0, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Sebastian Siewior562954d2008-03-13 20:20:28 +0800134 hash_buff = kzalloc(template[i].psize, GFP_KERNEL);
135 if (!hash_buff)
136 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Sebastian Siewior562954d2008-03-13 20:20:28 +0800138 memcpy(hash_buff, template[i].plaintext, template[i].psize);
139 sg_init_one(&sg[0], hash_buff, template[i].psize);
140
141 if (template[i].ksize) {
142 ret = crypto_hash_setkey(tfm, template[i].key,
143 template[i].ksize);
Herbert Xue9d41162006-08-19 21:38:49 +1000144 if (ret) {
145 printk("setkey() failed ret=%d\n", ret);
Sebastian Siewior562954d2008-03-13 20:20:28 +0800146 kfree(hash_buff);
Herbert Xue9d41162006-08-19 21:38:49 +1000147 goto out;
148 }
149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Sebastian Siewior562954d2008-03-13 20:20:28 +0800151 ret = crypto_hash_digest(&desc, sg, template[i].psize, result);
Herbert Xue9d41162006-08-19 21:38:49 +1000152 if (ret) {
153 printk("digest () failed ret=%d\n", ret);
Sebastian Siewior562954d2008-03-13 20:20:28 +0800154 kfree(hash_buff);
Herbert Xue9d41162006-08-19 21:38:49 +1000155 goto out;
156 }
157
158 hexdump(result, crypto_hash_digestsize(tfm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 printk("%s\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +0800160 memcmp(result, template[i].digest,
Herbert Xue9d41162006-08-19 21:38:49 +1000161 crypto_hash_digestsize(tfm)) ?
Herbert Xuef2736f2005-06-22 13:26:03 -0700162 "fail" : "pass");
Sebastian Siewior562954d2008-03-13 20:20:28 +0800163 kfree(hash_buff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165
Herbert Xuef2736f2005-06-22 13:26:03 -0700166 printk("testing %s across pages\n", algo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 /* setup the dummy buffer first */
Herbert Xuef2736f2005-06-22 13:26:03 -0700169 memset(xbuf, 0, XBUFSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 j = 0;
172 for (i = 0; i < tcount; i++) {
Sebastian Siewior562954d2008-03-13 20:20:28 +0800173 if (template[i].np) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 j++;
Herbert Xuef2736f2005-06-22 13:26:03 -0700175 printk("test %u:\n", j);
176 memset(result, 0, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 temp = 0;
Sebastian Siewior562954d2008-03-13 20:20:28 +0800179 sg_init_table(sg, template[i].np);
180 for (k = 0; k < template[i].np; k++) {
Herbert Xuef2736f2005-06-22 13:26:03 -0700181 memcpy(&xbuf[IDX[k]],
Sebastian Siewior562954d2008-03-13 20:20:28 +0800182 template[i].plaintext + temp,
183 template[i].tap[k]);
184 temp += template[i].tap[k];
David Hardeman378f0582005-09-17 17:55:31 +1000185 sg_set_buf(&sg[k], &xbuf[IDX[k]],
Sebastian Siewior562954d2008-03-13 20:20:28 +0800186 template[i].tap[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188
Sebastian Siewior562954d2008-03-13 20:20:28 +0800189 if (template[i].ksize) {
190 ret = crypto_hash_setkey(tfm, template[i].key,
191 template[i].ksize);
Herbert Xuef2736f2005-06-22 13:26:03 -0700192
Herbert Xue9d41162006-08-19 21:38:49 +1000193 if (ret) {
194 printk("setkey() failed ret=%d\n", ret);
195 goto out;
196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198
Sebastian Siewior562954d2008-03-13 20:20:28 +0800199 ret = crypto_hash_digest(&desc, sg, template[i].psize,
Herbert Xue9d41162006-08-19 21:38:49 +1000200 result);
201 if (ret) {
202 printk("digest () failed ret=%d\n", ret);
203 goto out;
204 }
Herbert Xuef2736f2005-06-22 13:26:03 -0700205
Herbert Xue9d41162006-08-19 21:38:49 +1000206 hexdump(result, crypto_hash_digestsize(tfm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 printk("%s\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +0800208 memcmp(result, template[i].digest,
Herbert Xue9d41162006-08-19 21:38:49 +1000209 crypto_hash_digestsize(tfm)) ?
Herbert Xuef2736f2005-06-22 13:26:03 -0700210 "fail" : "pass");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Herbert Xue9d41162006-08-19 21:38:49 +1000214out:
215 crypto_free_hash(tfm);
216}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800218static void test_aead(char *algo, int enc, struct aead_testvec *template,
219 unsigned int tcount)
220{
221 unsigned int ret, i, j, k, temp;
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800222 char *q;
223 struct crypto_aead *tfm;
224 char *key;
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800225 struct aead_request *req;
226 struct scatterlist sg[8];
227 struct scatterlist asg[8];
228 const char *e;
229 struct tcrypt_result result;
Herbert Xu6160b282007-12-04 19:17:50 +1100230 unsigned int authsize;
Sebastian Siewior562954d2008-03-13 20:20:28 +0800231 void *input;
232 void *assoc;
233 char iv[MAX_IVLEN];
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800234
235 if (enc == ENCRYPT)
236 e = "encryption";
237 else
238 e = "decryption";
239
240 printk(KERN_INFO "\ntesting %s %s\n", algo, e);
241
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800242 init_completion(&result.completion);
243
244 tfm = crypto_alloc_aead(algo, 0, 0);
245
246 if (IS_ERR(tfm)) {
247 printk(KERN_INFO "failed to load transform for %s: %ld\n",
248 algo, PTR_ERR(tfm));
249 return;
250 }
251
252 req = aead_request_alloc(tfm, GFP_KERNEL);
253 if (!req) {
254 printk(KERN_INFO "failed to allocate request for %s\n", algo);
255 goto out;
256 }
257
258 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
259 tcrypt_complete, &result);
260
261 for (i = 0, j = 0; i < tcount; i++) {
Sebastian Siewior562954d2008-03-13 20:20:28 +0800262 if (!template[i].np) {
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800263 printk(KERN_INFO "test %u (%d bit key):\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +0800264 ++j, template[i].klen * 8);
265
266 /* some tepmplates have no input data but they will
267 * touch input
268 */
269 input = kzalloc(template[i].ilen + template[i].rlen, GFP_KERNEL);
270 if (!input)
271 continue;
272
273 assoc = kzalloc(template[i].alen, GFP_KERNEL);
274 if (!assoc) {
275 kfree(input);
276 continue;
277 }
278
279 memcpy(input, template[i].input, template[i].ilen);
280 memcpy(assoc, template[i].assoc, template[i].alen);
281 if (template[i].iv)
282 memcpy(iv, template[i].iv, MAX_IVLEN);
283 else
284 memset(iv, 0, MAX_IVLEN);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800285
286 crypto_aead_clear_flags(tfm, ~0);
Sebastian Siewior562954d2008-03-13 20:20:28 +0800287 if (template[i].wk)
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800288 crypto_aead_set_flags(
289 tfm, CRYPTO_TFM_REQ_WEAK_KEY);
Sebastian Siewior562954d2008-03-13 20:20:28 +0800290
291 if (template[i].key)
292 key = template[i].key;
293 else
294 key = kzalloc(template[i].klen, GFP_KERNEL);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800295
296 ret = crypto_aead_setkey(tfm, key,
Sebastian Siewior562954d2008-03-13 20:20:28 +0800297 template[i].klen);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800298 if (ret) {
299 printk(KERN_INFO "setkey() failed flags=%x\n",
300 crypto_aead_get_flags(tfm));
301
Sebastian Siewior562954d2008-03-13 20:20:28 +0800302 if (!template[i].fail)
303 goto next_one;
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800304 }
305
Sebastian Siewior562954d2008-03-13 20:20:28 +0800306 authsize = abs(template[i].rlen - template[i].ilen);
Joy Latten93cc74e2007-12-12 20:24:22 +0800307 ret = crypto_aead_setauthsize(tfm, authsize);
308 if (ret) {
309 printk(KERN_INFO
310 "failed to set authsize = %u\n",
311 authsize);
Sebastian Siewior562954d2008-03-13 20:20:28 +0800312 goto next_one;
Joy Latten93cc74e2007-12-12 20:24:22 +0800313 }
314
Sebastian Siewior562954d2008-03-13 20:20:28 +0800315 sg_init_one(&sg[0], input,
316 template[i].ilen + (enc ? authsize : 0));
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800317
Sebastian Siewior562954d2008-03-13 20:20:28 +0800318 sg_init_one(&asg[0], assoc, template[i].alen);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800319
320 aead_request_set_crypt(req, sg, sg,
Sebastian Siewior562954d2008-03-13 20:20:28 +0800321 template[i].ilen, iv);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800322
Sebastian Siewior562954d2008-03-13 20:20:28 +0800323 aead_request_set_assoc(req, asg, template[i].alen);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800324
Herbert Xu6160b282007-12-04 19:17:50 +1100325 ret = enc ?
326 crypto_aead_encrypt(req) :
327 crypto_aead_decrypt(req);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800328
329 switch (ret) {
330 case 0:
331 break;
332 case -EINPROGRESS:
333 case -EBUSY:
334 ret = wait_for_completion_interruptible(
335 &result.completion);
336 if (!ret && !(ret = result.err)) {
337 INIT_COMPLETION(result.completion);
338 break;
339 }
340 /* fall through */
341 default:
342 printk(KERN_INFO "%s () failed err=%d\n",
343 e, -ret);
Sebastian Siewior562954d2008-03-13 20:20:28 +0800344 goto next_one;
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800345 }
346
347 q = kmap(sg_page(&sg[0])) + sg[0].offset;
Sebastian Siewior562954d2008-03-13 20:20:28 +0800348 hexdump(q, template[i].rlen);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800349
350 printk(KERN_INFO "enc/dec: %s\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +0800351 memcmp(q, template[i].result,
352 template[i].rlen) ? "fail" : "pass");
353 kunmap(sg_page(&sg[0]));
354next_one:
355 if (!template[i].key)
356 kfree(key);
357 kfree(assoc);
358 kfree(input);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800359 }
360 }
361
362 printk(KERN_INFO "\ntesting %s %s across pages (chunking)\n", algo, e);
363 memset(xbuf, 0, XBUFSIZE);
Herbert Xu2a999a32007-12-30 20:24:11 +1100364 memset(axbuf, 0, XBUFSIZE);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800365
366 for (i = 0, j = 0; i < tcount; i++) {
Sebastian Siewior562954d2008-03-13 20:20:28 +0800367 if (template[i].np) {
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800368 printk(KERN_INFO "test %u (%d bit key):\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +0800369 ++j, template[i].klen * 8);
370
371 if (template[i].iv)
372 memcpy(iv, template[i].iv, MAX_IVLEN);
373 else
374 memset(iv, 0, MAX_IVLEN);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800375
376 crypto_aead_clear_flags(tfm, ~0);
Sebastian Siewior562954d2008-03-13 20:20:28 +0800377 if (template[i].wk)
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800378 crypto_aead_set_flags(
379 tfm, CRYPTO_TFM_REQ_WEAK_KEY);
Sebastian Siewior562954d2008-03-13 20:20:28 +0800380 key = template[i].key;
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800381
Sebastian Siewior562954d2008-03-13 20:20:28 +0800382 ret = crypto_aead_setkey(tfm, key, template[i].klen);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800383 if (ret) {
384 printk(KERN_INFO "setkey() failed flags=%x\n",
385 crypto_aead_get_flags(tfm));
386
Sebastian Siewior562954d2008-03-13 20:20:28 +0800387 if (!template[i].fail)
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800388 goto out;
389 }
390
Sebastian Siewior562954d2008-03-13 20:20:28 +0800391 sg_init_table(sg, template[i].np);
392 for (k = 0, temp = 0; k < template[i].np; k++) {
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800393 memcpy(&xbuf[IDX[k]],
Sebastian Siewior562954d2008-03-13 20:20:28 +0800394 template[i].input + temp,
395 template[i].tap[k]);
396 temp += template[i].tap[k];
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800397 sg_set_buf(&sg[k], &xbuf[IDX[k]],
Sebastian Siewior562954d2008-03-13 20:20:28 +0800398 template[i].tap[k]);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800399 }
400
Sebastian Siewior562954d2008-03-13 20:20:28 +0800401 authsize = abs(template[i].rlen - template[i].ilen);
Joy Latten93cc74e2007-12-12 20:24:22 +0800402 ret = crypto_aead_setauthsize(tfm, authsize);
403 if (ret) {
404 printk(KERN_INFO
405 "failed to set authsize = %u\n",
406 authsize);
407 goto out;
408 }
409
Herbert Xu6160b282007-12-04 19:17:50 +1100410 if (enc)
411 sg[k - 1].length += authsize;
412
Sebastian Siewior562954d2008-03-13 20:20:28 +0800413 sg_init_table(asg, template[i].anp);
414 for (k = 0, temp = 0; k < template[i].anp; k++) {
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800415 memcpy(&axbuf[IDX[k]],
Sebastian Siewior562954d2008-03-13 20:20:28 +0800416 template[i].assoc + temp,
417 template[i].atap[k]);
418 temp += template[i].atap[k];
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800419 sg_set_buf(&asg[k], &axbuf[IDX[k]],
Sebastian Siewior562954d2008-03-13 20:20:28 +0800420 template[i].atap[k]);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800421 }
422
423 aead_request_set_crypt(req, sg, sg,
Sebastian Siewior562954d2008-03-13 20:20:28 +0800424 template[i].ilen,
425 iv);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800426
Sebastian Siewior562954d2008-03-13 20:20:28 +0800427 aead_request_set_assoc(req, asg, template[i].alen);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800428
Herbert Xu6160b282007-12-04 19:17:50 +1100429 ret = enc ?
430 crypto_aead_encrypt(req) :
431 crypto_aead_decrypt(req);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800432
433 switch (ret) {
434 case 0:
435 break;
436 case -EINPROGRESS:
437 case -EBUSY:
438 ret = wait_for_completion_interruptible(
439 &result.completion);
440 if (!ret && !(ret = result.err)) {
441 INIT_COMPLETION(result.completion);
442 break;
443 }
444 /* fall through */
445 default:
446 printk(KERN_INFO "%s () failed err=%d\n",
447 e, -ret);
448 goto out;
449 }
450
Sebastian Siewior562954d2008-03-13 20:20:28 +0800451 for (k = 0, temp = 0; k < template[i].np; k++) {
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800452 printk(KERN_INFO "page %u\n", k);
453 q = kmap(sg_page(&sg[k])) + sg[k].offset;
Sebastian Siewior562954d2008-03-13 20:20:28 +0800454 hexdump(q, template[i].tap[k]);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800455 printk(KERN_INFO "%s\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +0800456 memcmp(q, template[i].result + temp,
457 template[i].tap[k] -
458 (k < template[i].np - 1 || enc ?
Herbert Xu6160b282007-12-04 19:17:50 +1100459 0 : authsize)) ?
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800460 "fail" : "pass");
461
Sebastian Siewior562954d2008-03-13 20:20:28 +0800462 temp += template[i].tap[k];
463 kunmap(sg_page(&sg[k]));
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800464 }
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800465 }
466 }
467
468out:
469 crypto_free_aead(tfm);
470 aead_request_free(req);
471}
472
Herbert Xucba83562006-08-13 08:26:09 +1000473static void test_cipher(char *algo, int enc,
Herbert Xuef2736f2005-06-22 13:26:03 -0700474 struct cipher_testvec *template, unsigned int tcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 unsigned int ret, i, j, k, temp;
David Hardeman378f0582005-09-17 17:55:31 +1000477 char *q;
Herbert Xu6158efc2007-04-04 17:41:07 +1000478 struct crypto_ablkcipher *tfm;
Herbert Xu6158efc2007-04-04 17:41:07 +1000479 struct ablkcipher_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 struct scatterlist sg[8];
Herbert Xucba83562006-08-13 08:26:09 +1000481 const char *e;
Herbert Xu6158efc2007-04-04 17:41:07 +1000482 struct tcrypt_result result;
Sebastian Siewior562954d2008-03-13 20:20:28 +0800483 void *data;
484 char iv[MAX_IVLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 if (enc == ENCRYPT)
Herbert Xu3cc38162005-06-22 13:26:36 -0700487 e = "encryption";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 else
Herbert Xu3cc38162005-06-22 13:26:36 -0700489 e = "decryption";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Herbert Xucba83562006-08-13 08:26:09 +1000491 printk("\ntesting %s %s\n", algo, e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Herbert Xu6158efc2007-04-04 17:41:07 +1000493 init_completion(&result.completion);
Herbert Xu6158efc2007-04-04 17:41:07 +1000494 tfm = crypto_alloc_ablkcipher(algo, 0, 0);
Herbert Xuef2736f2005-06-22 13:26:03 -0700495
Herbert Xucba83562006-08-13 08:26:09 +1000496 if (IS_ERR(tfm)) {
497 printk("failed to load transform for %s: %ld\n", algo,
498 PTR_ERR(tfm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return;
500 }
Herbert Xu6158efc2007-04-04 17:41:07 +1000501
502 req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
503 if (!req) {
504 printk("failed to allocate request for %s\n", algo);
505 goto out;
506 }
507
508 ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
509 tcrypt_complete, &result);
Herbert Xuef2736f2005-06-22 13:26:03 -0700510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 j = 0;
512 for (i = 0; i < tcount; i++) {
Sebastian Siewior562954d2008-03-13 20:20:28 +0800513
514 data = kzalloc(template[i].ilen, GFP_KERNEL);
515 if (!data)
516 continue;
517
518 memcpy(data, template[i].input, template[i].ilen);
519 if (template[i].iv)
520 memcpy(iv, template[i].iv, MAX_IVLEN);
521 else
522 memset(iv, 0, MAX_IVLEN);
523
524 if (!(template[i].np)) {
Herbert Xuef2736f2005-06-22 13:26:03 -0700525 j++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 printk("test %u (%d bit key):\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +0800527 j, template[i].klen * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Herbert Xu6158efc2007-04-04 17:41:07 +1000529 crypto_ablkcipher_clear_flags(tfm, ~0);
Sebastian Siewior562954d2008-03-13 20:20:28 +0800530 if (template[i].wk)
Herbert Xu6158efc2007-04-04 17:41:07 +1000531 crypto_ablkcipher_set_flags(
Herbert Xucba83562006-08-13 08:26:09 +1000532 tfm, CRYPTO_TFM_REQ_WEAK_KEY);
Herbert Xuef2736f2005-06-22 13:26:03 -0700533
Sebastian Siewior562954d2008-03-13 20:20:28 +0800534 ret = crypto_ablkcipher_setkey(tfm, template[i].key,
535 template[i].klen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (ret) {
Herbert Xucba83562006-08-13 08:26:09 +1000537 printk("setkey() failed flags=%x\n",
Herbert Xu6158efc2007-04-04 17:41:07 +1000538 crypto_ablkcipher_get_flags(tfm));
Herbert Xuef2736f2005-06-22 13:26:03 -0700539
Sebastian Siewior562954d2008-03-13 20:20:28 +0800540 if (!template[i].fail) {
541 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 goto out;
Sebastian Siewior562954d2008-03-13 20:20:28 +0800543 }
Herbert Xuef2736f2005-06-22 13:26:03 -0700544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Sebastian Siewior562954d2008-03-13 20:20:28 +0800546 sg_init_one(&sg[0], data, template[i].ilen);
Herbert Xuef2736f2005-06-22 13:26:03 -0700547
Herbert Xu6158efc2007-04-04 17:41:07 +1000548 ablkcipher_request_set_crypt(req, sg, sg,
Sebastian Siewior562954d2008-03-13 20:20:28 +0800549 template[i].ilen, iv);
Herbert Xucba83562006-08-13 08:26:09 +1000550 ret = enc ?
Herbert Xu6158efc2007-04-04 17:41:07 +1000551 crypto_ablkcipher_encrypt(req) :
552 crypto_ablkcipher_decrypt(req);
Herbert Xuef2736f2005-06-22 13:26:03 -0700553
Herbert Xu6158efc2007-04-04 17:41:07 +1000554 switch (ret) {
555 case 0:
556 break;
557 case -EINPROGRESS:
558 case -EBUSY:
559 ret = wait_for_completion_interruptible(
560 &result.completion);
561 if (!ret && !((ret = result.err))) {
562 INIT_COMPLETION(result.completion);
563 break;
564 }
565 /* fall through */
566 default:
567 printk("%s () failed err=%d\n", e, -ret);
Sebastian Siewior562954d2008-03-13 20:20:28 +0800568 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 goto out;
Herbert Xuef2736f2005-06-22 13:26:03 -0700570 }
571
Jens Axboe78c2f0b2007-10-22 19:40:16 +0200572 q = kmap(sg_page(&sg[0])) + sg[0].offset;
Sebastian Siewior562954d2008-03-13 20:20:28 +0800573 hexdump(q, template[i].rlen);
Herbert Xuef2736f2005-06-22 13:26:03 -0700574
575 printk("%s\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +0800576 memcmp(q, template[i].result,
577 template[i].rlen) ? "fail" : "pass");
578 kunmap(sg_page(&sg[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
Sebastian Siewior562954d2008-03-13 20:20:28 +0800580 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Herbert Xuef2736f2005-06-22 13:26:03 -0700582
Herbert Xucba83562006-08-13 08:26:09 +1000583 printk("\ntesting %s %s across pages (chunking)\n", algo, e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 memset(xbuf, 0, XBUFSIZE);
Herbert Xuef2736f2005-06-22 13:26:03 -0700585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 j = 0;
587 for (i = 0; i < tcount; i++) {
Sebastian Siewior562954d2008-03-13 20:20:28 +0800588
Sebastian Siewior562954d2008-03-13 20:20:28 +0800589 if (template[i].iv)
590 memcpy(iv, template[i].iv, MAX_IVLEN);
591 else
592 memset(iv, 0, MAX_IVLEN);
593
594 if (template[i].np) {
Herbert Xuef2736f2005-06-22 13:26:03 -0700595 j++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 printk("test %u (%d bit key):\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +0800597 j, template[i].klen * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Herbert Xu6158efc2007-04-04 17:41:07 +1000599 crypto_ablkcipher_clear_flags(tfm, ~0);
Sebastian Siewior562954d2008-03-13 20:20:28 +0800600 if (template[i].wk)
Herbert Xu6158efc2007-04-04 17:41:07 +1000601 crypto_ablkcipher_set_flags(
Herbert Xucba83562006-08-13 08:26:09 +1000602 tfm, CRYPTO_TFM_REQ_WEAK_KEY);
Herbert Xuef2736f2005-06-22 13:26:03 -0700603
Sebastian Siewior562954d2008-03-13 20:20:28 +0800604 ret = crypto_ablkcipher_setkey(tfm, template[i].key,
605 template[i].klen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (ret) {
Herbert Xucba83562006-08-13 08:26:09 +1000607 printk("setkey() failed flags=%x\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +0800608 crypto_ablkcipher_get_flags(tfm));
Herbert Xuef2736f2005-06-22 13:26:03 -0700609
Darren Jenkinsdbb018c2008-07-08 15:51:44 +0800610 if (!template[i].fail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 goto out;
612 }
613
614 temp = 0;
Sebastian Siewior562954d2008-03-13 20:20:28 +0800615 sg_init_table(sg, template[i].np);
616 for (k = 0; k < template[i].np; k++) {
Herbert Xuef2736f2005-06-22 13:26:03 -0700617 memcpy(&xbuf[IDX[k]],
Sebastian Siewior562954d2008-03-13 20:20:28 +0800618 template[i].input + temp,
619 template[i].tap[k]);
620 temp += template[i].tap[k];
David Hardeman378f0582005-09-17 17:55:31 +1000621 sg_set_buf(&sg[k], &xbuf[IDX[k]],
Sebastian Siewior562954d2008-03-13 20:20:28 +0800622 template[i].tap[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
Herbert Xuef2736f2005-06-22 13:26:03 -0700624
Herbert Xu6158efc2007-04-04 17:41:07 +1000625 ablkcipher_request_set_crypt(req, sg, sg,
Sebastian Siewior562954d2008-03-13 20:20:28 +0800626 template[i].ilen, iv);
Herbert Xuef2736f2005-06-22 13:26:03 -0700627
Herbert Xucba83562006-08-13 08:26:09 +1000628 ret = enc ?
Herbert Xu6158efc2007-04-04 17:41:07 +1000629 crypto_ablkcipher_encrypt(req) :
630 crypto_ablkcipher_decrypt(req);
Herbert Xuef2736f2005-06-22 13:26:03 -0700631
Herbert Xu6158efc2007-04-04 17:41:07 +1000632 switch (ret) {
633 case 0:
634 break;
635 case -EINPROGRESS:
636 case -EBUSY:
637 ret = wait_for_completion_interruptible(
638 &result.completion);
639 if (!ret && !((ret = result.err))) {
640 INIT_COMPLETION(result.completion);
641 break;
642 }
643 /* fall through */
644 default:
645 printk("%s () failed err=%d\n", e, -ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 goto out;
647 }
648
649 temp = 0;
Sebastian Siewior562954d2008-03-13 20:20:28 +0800650 for (k = 0; k < template[i].np; k++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 printk("page %u\n", k);
Jens Axboe78c2f0b2007-10-22 19:40:16 +0200652 q = kmap(sg_page(&sg[k])) + sg[k].offset;
Sebastian Siewior562954d2008-03-13 20:20:28 +0800653 hexdump(q, template[i].tap[k]);
Herbert Xuef2736f2005-06-22 13:26:03 -0700654 printk("%s\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +0800655 memcmp(q, template[i].result + temp,
656 template[i].tap[k]) ? "fail" :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 "pass");
Sebastian Siewior562954d2008-03-13 20:20:28 +0800658 temp += template[i].tap[k];
659 kunmap(sg_page(&sg[k]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661 }
662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663out:
Herbert Xu6158efc2007-04-04 17:41:07 +1000664 crypto_free_ablkcipher(tfm);
665 ablkcipher_request_free(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
Herbert Xucba83562006-08-13 08:26:09 +1000668static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc, char *p,
Herbert Xu6a179442005-06-22 13:29:03 -0700669 int blen, int sec)
670{
Herbert Xu6df5b9f2005-09-19 22:30:11 +1000671 struct scatterlist sg[1];
Herbert Xu6a179442005-06-22 13:29:03 -0700672 unsigned long start, end;
673 int bcount;
674 int ret;
675
David S. Millerb7335882007-10-26 00:38:10 -0700676 sg_init_one(sg, p, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700677
678 for (start = jiffies, end = start + sec * HZ, bcount = 0;
679 time_before(jiffies, end); bcount++) {
680 if (enc)
Herbert Xucba83562006-08-13 08:26:09 +1000681 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700682 else
Herbert Xucba83562006-08-13 08:26:09 +1000683 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700684
685 if (ret)
686 return ret;
687 }
688
689 printk("%d operations in %d seconds (%ld bytes)\n",
690 bcount, sec, (long)bcount * blen);
691 return 0;
692}
693
Herbert Xucba83562006-08-13 08:26:09 +1000694static int test_cipher_cycles(struct blkcipher_desc *desc, int enc, char *p,
Herbert Xu6a179442005-06-22 13:29:03 -0700695 int blen)
696{
Herbert Xu6df5b9f2005-09-19 22:30:11 +1000697 struct scatterlist sg[1];
Herbert Xu6a179442005-06-22 13:29:03 -0700698 unsigned long cycles = 0;
699 int ret = 0;
700 int i;
701
David S. Millerb7335882007-10-26 00:38:10 -0700702 sg_init_one(sg, p, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700703
704 local_bh_disable();
705 local_irq_disable();
706
707 /* Warm-up run. */
708 for (i = 0; i < 4; i++) {
709 if (enc)
Herbert Xucba83562006-08-13 08:26:09 +1000710 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700711 else
Herbert Xucba83562006-08-13 08:26:09 +1000712 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700713
714 if (ret)
715 goto out;
716 }
717
718 /* The real thing. */
719 for (i = 0; i < 8; i++) {
720 cycles_t start, end;
721
722 start = get_cycles();
723 if (enc)
Herbert Xucba83562006-08-13 08:26:09 +1000724 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700725 else
Herbert Xucba83562006-08-13 08:26:09 +1000726 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700727 end = get_cycles();
728
729 if (ret)
730 goto out;
731
732 cycles += end - start;
733 }
734
735out:
736 local_irq_enable();
737 local_bh_enable();
738
739 if (ret == 0)
740 printk("1 operation in %lu cycles (%d bytes)\n",
741 (cycles + 4) / 8, blen);
742
743 return ret;
744}
745
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800746static u32 block_sizes[] = { 16, 64, 256, 1024, 8192, 0 };
747
Herbert Xucba83562006-08-13 08:26:09 +1000748static void test_cipher_speed(char *algo, int enc, unsigned int sec,
Herbert Xudce907c2005-06-22 13:27:51 -0700749 struct cipher_testvec *template,
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800750 unsigned int tcount, u8 *keysize)
Harald Welteebfd9bc2005-06-22 13:27:23 -0700751{
Herbert Xudce907c2005-06-22 13:27:51 -0700752 unsigned int ret, i, j, iv_len;
Harald Welteebfd9bc2005-06-22 13:27:23 -0700753 unsigned char *key, *p, iv[128];
Herbert Xucba83562006-08-13 08:26:09 +1000754 struct crypto_blkcipher *tfm;
755 struct blkcipher_desc desc;
756 const char *e;
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800757 u32 *b_size;
Harald Welteebfd9bc2005-06-22 13:27:23 -0700758
759 if (enc == ENCRYPT)
760 e = "encryption";
761 else
762 e = "decryption";
Harald Welteebfd9bc2005-06-22 13:27:23 -0700763
Herbert Xucba83562006-08-13 08:26:09 +1000764 printk("\ntesting speed of %s %s\n", algo, e);
Harald Welteebfd9bc2005-06-22 13:27:23 -0700765
Herbert Xucba83562006-08-13 08:26:09 +1000766 tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
Harald Welteebfd9bc2005-06-22 13:27:23 -0700767
Herbert Xucba83562006-08-13 08:26:09 +1000768 if (IS_ERR(tfm)) {
769 printk("failed to load transform for %s: %ld\n", algo,
770 PTR_ERR(tfm));
Harald Welteebfd9bc2005-06-22 13:27:23 -0700771 return;
772 }
Herbert Xucba83562006-08-13 08:26:09 +1000773 desc.tfm = tfm;
774 desc.flags = 0;
Harald Welteebfd9bc2005-06-22 13:27:23 -0700775
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800776 i = 0;
777 do {
Harald Welteebfd9bc2005-06-22 13:27:23 -0700778
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800779 b_size = block_sizes;
780 do {
Harald Welteebfd9bc2005-06-22 13:27:23 -0700781
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800782 if ((*keysize + *b_size) > TVMEMSIZE) {
783 printk("template (%u) too big for tvmem (%u)\n",
784 *keysize + *b_size, TVMEMSIZE);
785 goto out;
786 }
Harald Welteebfd9bc2005-06-22 13:27:23 -0700787
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800788 printk("test %u (%d bit key, %d byte blocks): ", i,
789 *keysize * 8, *b_size);
790
791 memset(tvmem, 0xff, *keysize + *b_size);
792
793 /* set key, plain text and IV */
794 key = (unsigned char *)tvmem;
795 for (j = 0; j < tcount; j++) {
796 if (template[j].klen == *keysize) {
797 key = template[j].key;
798 break;
799 }
800 }
801 p = (unsigned char *)tvmem + *keysize;
802
803 ret = crypto_blkcipher_setkey(tfm, key, *keysize);
804 if (ret) {
805 printk("setkey() failed flags=%x\n",
806 crypto_blkcipher_get_flags(tfm));
807 goto out;
808 }
809
810 iv_len = crypto_blkcipher_ivsize(tfm);
811 if (iv_len) {
812 memset(&iv, 0xff, iv_len);
813 crypto_blkcipher_set_iv(tfm, iv, iv_len);
814 }
815
816 if (sec)
817 ret = test_cipher_jiffies(&desc, enc, p, *b_size, sec);
818 else
819 ret = test_cipher_cycles(&desc, enc, p, *b_size);
820
821 if (ret) {
822 printk("%s() failed flags=%x\n", e, desc.flags);
Herbert Xudce907c2005-06-22 13:27:51 -0700823 break;
824 }
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800825 b_size++;
826 i++;
827 } while (*b_size);
828 keysize++;
829 } while (*keysize);
Harald Welteebfd9bc2005-06-22 13:27:23 -0700830
831out:
Herbert Xucba83562006-08-13 08:26:09 +1000832 crypto_free_blkcipher(tfm);
Harald Welteebfd9bc2005-06-22 13:27:23 -0700833}
834
Herbert Xue9d41162006-08-19 21:38:49 +1000835static int test_hash_jiffies_digest(struct hash_desc *desc, char *p, int blen,
836 char *out, int sec)
Michal Ludvige8057922006-05-30 22:04:19 +1000837{
838 struct scatterlist sg[1];
839 unsigned long start, end;
Herbert Xue9d41162006-08-19 21:38:49 +1000840 int bcount;
841 int ret;
Michal Ludvige8057922006-05-30 22:04:19 +1000842
Herbert Xua5a613a2007-10-27 00:51:21 -0700843 sg_init_table(sg, 1);
844
Michal Ludvige8057922006-05-30 22:04:19 +1000845 for (start = jiffies, end = start + sec * HZ, bcount = 0;
846 time_before(jiffies, end); bcount++) {
Herbert Xua5a613a2007-10-27 00:51:21 -0700847 sg_set_buf(sg, p, blen);
Herbert Xue9d41162006-08-19 21:38:49 +1000848 ret = crypto_hash_digest(desc, sg, blen, out);
849 if (ret)
850 return ret;
Michal Ludvige8057922006-05-30 22:04:19 +1000851 }
852
853 printk("%6u opers/sec, %9lu bytes/sec\n",
854 bcount / sec, ((long)bcount * blen) / sec);
855
Herbert Xue9d41162006-08-19 21:38:49 +1000856 return 0;
Michal Ludvige8057922006-05-30 22:04:19 +1000857}
858
Herbert Xue9d41162006-08-19 21:38:49 +1000859static int test_hash_jiffies(struct hash_desc *desc, char *p, int blen,
860 int plen, char *out, int sec)
861{
862 struct scatterlist sg[1];
863 unsigned long start, end;
864 int bcount, pcount;
865 int ret;
866
867 if (plen == blen)
868 return test_hash_jiffies_digest(desc, p, blen, out, sec);
869
Herbert Xua5a613a2007-10-27 00:51:21 -0700870 sg_init_table(sg, 1);
871
Herbert Xue9d41162006-08-19 21:38:49 +1000872 for (start = jiffies, end = start + sec * HZ, bcount = 0;
873 time_before(jiffies, end); bcount++) {
874 ret = crypto_hash_init(desc);
875 if (ret)
876 return ret;
877 for (pcount = 0; pcount < blen; pcount += plen) {
Herbert Xua5a613a2007-10-27 00:51:21 -0700878 sg_set_buf(sg, p + pcount, plen);
Herbert Xue9d41162006-08-19 21:38:49 +1000879 ret = crypto_hash_update(desc, sg, plen);
880 if (ret)
881 return ret;
882 }
883 /* we assume there is enough space in 'out' for the result */
884 ret = crypto_hash_final(desc, out);
885 if (ret)
886 return ret;
887 }
888
889 printk("%6u opers/sec, %9lu bytes/sec\n",
890 bcount / sec, ((long)bcount * blen) / sec);
891
892 return 0;
893}
894
895static int test_hash_cycles_digest(struct hash_desc *desc, char *p, int blen,
896 char *out)
Michal Ludvige8057922006-05-30 22:04:19 +1000897{
898 struct scatterlist sg[1];
899 unsigned long cycles = 0;
Herbert Xue9d41162006-08-19 21:38:49 +1000900 int i;
901 int ret;
Michal Ludvige8057922006-05-30 22:04:19 +1000902
Herbert Xua5a613a2007-10-27 00:51:21 -0700903 sg_init_table(sg, 1);
904
Michal Ludvige8057922006-05-30 22:04:19 +1000905 local_bh_disable();
906 local_irq_disable();
907
908 /* Warm-up run. */
909 for (i = 0; i < 4; i++) {
Herbert Xua5a613a2007-10-27 00:51:21 -0700910 sg_set_buf(sg, p, blen);
Herbert Xue9d41162006-08-19 21:38:49 +1000911 ret = crypto_hash_digest(desc, sg, blen, out);
912 if (ret)
913 goto out;
Michal Ludvige8057922006-05-30 22:04:19 +1000914 }
915
916 /* The real thing. */
917 for (i = 0; i < 8; i++) {
918 cycles_t start, end;
919
Michal Ludvige8057922006-05-30 22:04:19 +1000920 start = get_cycles();
921
Herbert Xua5a613a2007-10-27 00:51:21 -0700922 sg_set_buf(sg, p, blen);
Herbert Xue9d41162006-08-19 21:38:49 +1000923 ret = crypto_hash_digest(desc, sg, blen, out);
924 if (ret)
925 goto out;
Michal Ludvige8057922006-05-30 22:04:19 +1000926
927 end = get_cycles();
928
929 cycles += end - start;
930 }
931
Herbert Xue9d41162006-08-19 21:38:49 +1000932out:
Michal Ludvige8057922006-05-30 22:04:19 +1000933 local_irq_enable();
934 local_bh_enable();
935
Herbert Xue9d41162006-08-19 21:38:49 +1000936 if (ret)
937 return ret;
938
Michal Ludvige8057922006-05-30 22:04:19 +1000939 printk("%6lu cycles/operation, %4lu cycles/byte\n",
940 cycles / 8, cycles / (8 * blen));
941
Herbert Xue9d41162006-08-19 21:38:49 +1000942 return 0;
Michal Ludvige8057922006-05-30 22:04:19 +1000943}
944
Herbert Xue9d41162006-08-19 21:38:49 +1000945static int test_hash_cycles(struct hash_desc *desc, char *p, int blen,
946 int plen, char *out)
Michal Ludvige8057922006-05-30 22:04:19 +1000947{
Herbert Xue9d41162006-08-19 21:38:49 +1000948 struct scatterlist sg[1];
949 unsigned long cycles = 0;
950 int i, pcount;
951 int ret;
952
953 if (plen == blen)
954 return test_hash_cycles_digest(desc, p, blen, out);
955
Herbert Xua5a613a2007-10-27 00:51:21 -0700956 sg_init_table(sg, 1);
957
Herbert Xue9d41162006-08-19 21:38:49 +1000958 local_bh_disable();
959 local_irq_disable();
960
961 /* Warm-up run. */
962 for (i = 0; i < 4; i++) {
963 ret = crypto_hash_init(desc);
964 if (ret)
965 goto out;
966 for (pcount = 0; pcount < blen; pcount += plen) {
Herbert Xua5a613a2007-10-27 00:51:21 -0700967 sg_set_buf(sg, p + pcount, plen);
Herbert Xue9d41162006-08-19 21:38:49 +1000968 ret = crypto_hash_update(desc, sg, plen);
969 if (ret)
970 goto out;
971 }
Herbert Xu29059d12007-05-18 16:25:19 +1000972 ret = crypto_hash_final(desc, out);
Herbert Xue9d41162006-08-19 21:38:49 +1000973 if (ret)
974 goto out;
975 }
976
977 /* The real thing. */
978 for (i = 0; i < 8; i++) {
979 cycles_t start, end;
980
981 start = get_cycles();
982
983 ret = crypto_hash_init(desc);
984 if (ret)
985 goto out;
986 for (pcount = 0; pcount < blen; pcount += plen) {
Herbert Xua5a613a2007-10-27 00:51:21 -0700987 sg_set_buf(sg, p + pcount, plen);
Herbert Xue9d41162006-08-19 21:38:49 +1000988 ret = crypto_hash_update(desc, sg, plen);
989 if (ret)
990 goto out;
991 }
992 ret = crypto_hash_final(desc, out);
993 if (ret)
994 goto out;
995
996 end = get_cycles();
997
998 cycles += end - start;
999 }
1000
1001out:
1002 local_irq_enable();
1003 local_bh_enable();
1004
1005 if (ret)
1006 return ret;
1007
1008 printk("%6lu cycles/operation, %4lu cycles/byte\n",
1009 cycles / 8, cycles / (8 * blen));
1010
1011 return 0;
1012}
1013
1014static void test_hash_speed(char *algo, unsigned int sec,
1015 struct hash_speed *speed)
1016{
1017 struct crypto_hash *tfm;
1018 struct hash_desc desc;
Michal Ludvige8057922006-05-30 22:04:19 +10001019 char output[1024];
1020 int i;
Herbert Xue9d41162006-08-19 21:38:49 +10001021 int ret;
Michal Ludvige8057922006-05-30 22:04:19 +10001022
1023 printk("\ntesting speed of %s\n", algo);
1024
Herbert Xue9d41162006-08-19 21:38:49 +10001025 tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
Michal Ludvige8057922006-05-30 22:04:19 +10001026
Herbert Xue9d41162006-08-19 21:38:49 +10001027 if (IS_ERR(tfm)) {
1028 printk("failed to load transform for %s: %ld\n", algo,
1029 PTR_ERR(tfm));
Michal Ludvige8057922006-05-30 22:04:19 +10001030 return;
1031 }
1032
Herbert Xue9d41162006-08-19 21:38:49 +10001033 desc.tfm = tfm;
1034 desc.flags = 0;
1035
1036 if (crypto_hash_digestsize(tfm) > sizeof(output)) {
Michal Ludvige8057922006-05-30 22:04:19 +10001037 printk("digestsize(%u) > outputbuffer(%zu)\n",
Herbert Xue9d41162006-08-19 21:38:49 +10001038 crypto_hash_digestsize(tfm), sizeof(output));
Michal Ludvige8057922006-05-30 22:04:19 +10001039 goto out;
1040 }
1041
1042 for (i = 0; speed[i].blen != 0; i++) {
1043 if (speed[i].blen > TVMEMSIZE) {
1044 printk("template (%u) too big for tvmem (%u)\n",
1045 speed[i].blen, TVMEMSIZE);
1046 goto out;
1047 }
1048
1049 printk("test%3u (%5u byte blocks,%5u bytes per update,%4u updates): ",
1050 i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
1051
1052 memset(tvmem, 0xff, speed[i].blen);
1053
1054 if (sec)
Herbert Xue9d41162006-08-19 21:38:49 +10001055 ret = test_hash_jiffies(&desc, tvmem, speed[i].blen,
1056 speed[i].plen, output, sec);
Michal Ludvige8057922006-05-30 22:04:19 +10001057 else
Herbert Xue9d41162006-08-19 21:38:49 +10001058 ret = test_hash_cycles(&desc, tvmem, speed[i].blen,
1059 speed[i].plen, output);
1060
1061 if (ret) {
1062 printk("hashing failed ret=%d\n", ret);
1063 break;
1064 }
Michal Ludvige8057922006-05-30 22:04:19 +10001065 }
1066
1067out:
Herbert Xue9d41162006-08-19 21:38:49 +10001068 crypto_free_hash(tfm);
Michal Ludvige8057922006-05-30 22:04:19 +10001069}
1070
Zoltan Sogor91755a92007-12-07 16:48:11 +08001071static void test_comp(char *algo, struct comp_testvec *ctemplate,
1072 struct comp_testvec *dtemplate, int ctcount, int dtcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
1074 unsigned int i;
1075 char result[COMP_BUF_SIZE];
Herbert Xue4d5b792006-08-26 18:12:40 +10001076 struct crypto_comp *tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 unsigned int tsize;
1078
Zoltan Sogor91755a92007-12-07 16:48:11 +08001079 printk("\ntesting %s compression\n", algo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Zoltan Sogor91755a92007-12-07 16:48:11 +08001081 tfm = crypto_alloc_comp(algo, 0, CRYPTO_ALG_ASYNC);
Sebastian Siewior7bc301e2007-03-21 08:58:43 +11001082 if (IS_ERR(tfm)) {
Zoltan Sogor91755a92007-12-07 16:48:11 +08001083 printk("failed to load transform for %s\n", algo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return;
1085 }
1086
Zoltan Sogor91755a92007-12-07 16:48:11 +08001087 for (i = 0; i < ctcount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 int ilen, ret, dlen = COMP_BUF_SIZE;
Herbert Xuef2736f2005-06-22 13:26:03 -07001089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 printk("test %u:\n", i + 1);
1091 memset(result, 0, sizeof (result));
1092
Sebastian Siewior562954d2008-03-13 20:20:28 +08001093 ilen = ctemplate[i].inlen;
1094 ret = crypto_comp_compress(tfm, ctemplate[i].input,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 ilen, result, &dlen);
1096 if (ret) {
1097 printk("fail: ret=%d\n", ret);
1098 continue;
1099 }
1100 hexdump(result, dlen);
1101 printk("%s (ratio %d:%d)\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +08001102 memcmp(result, ctemplate[i].output, dlen) ? "fail" : "pass",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 ilen, dlen);
1104 }
1105
Zoltan Sogor91755a92007-12-07 16:48:11 +08001106 printk("\ntesting %s decompression\n", algo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Zoltan Sogor91755a92007-12-07 16:48:11 +08001108 tsize = sizeof(struct comp_testvec);
1109 tsize *= dtcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (tsize > TVMEMSIZE) {
1111 printk("template (%u) too big for tvmem (%u)\n", tsize,
1112 TVMEMSIZE);
1113 goto out;
1114 }
1115
Zoltan Sogor91755a92007-12-07 16:48:11 +08001116 for (i = 0; i < dtcount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 int ilen, ret, dlen = COMP_BUF_SIZE;
Herbert Xuef2736f2005-06-22 13:26:03 -07001118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 printk("test %u:\n", i + 1);
1120 memset(result, 0, sizeof (result));
1121
Sebastian Siewior562954d2008-03-13 20:20:28 +08001122 ilen = dtemplate[i].inlen;
1123 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 ilen, result, &dlen);
1125 if (ret) {
1126 printk("fail: ret=%d\n", ret);
1127 continue;
1128 }
1129 hexdump(result, dlen);
1130 printk("%s (ratio %d:%d)\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +08001131 memcmp(result, dtemplate[i].output, dlen) ? "fail" : "pass",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 ilen, dlen);
1133 }
1134out:
Herbert Xue4d5b792006-08-26 18:12:40 +10001135 crypto_free_comp(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
Herbert Xuef2736f2005-06-22 13:26:03 -07001138static void test_available(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
1140 char **name = check;
Herbert Xuef2736f2005-06-22 13:26:03 -07001141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 while (*name) {
1143 printk("alg %s ", *name);
Herbert Xu6158efc2007-04-04 17:41:07 +10001144 printk(crypto_has_alg(*name, 0, 0) ?
Herbert Xue4d5b792006-08-26 18:12:40 +10001145 "found\n" : "not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 name++;
Herbert Xuef2736f2005-06-22 13:26:03 -07001147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
Herbert Xuef2736f2005-06-22 13:26:03 -07001150static void do_test(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 switch (mode) {
1153
1154 case 0:
1155 test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -07001156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -07001158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 //DES
Herbert Xucba83562006-08-13 08:26:09 +10001160 test_cipher("ecb(des)", ENCRYPT, des_enc_tv_template,
1161 DES_ENC_TEST_VECTORS);
1162 test_cipher("ecb(des)", DECRYPT, des_dec_tv_template,
1163 DES_DEC_TEST_VECTORS);
1164 test_cipher("cbc(des)", ENCRYPT, des_cbc_enc_tv_template,
1165 DES_CBC_ENC_TEST_VECTORS);
1166 test_cipher("cbc(des)", DECRYPT, des_cbc_dec_tv_template,
1167 DES_CBC_DEC_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -07001168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 //DES3_EDE
Herbert Xucba83562006-08-13 08:26:09 +10001170 test_cipher("ecb(des3_ede)", ENCRYPT, des3_ede_enc_tv_template,
1171 DES3_EDE_ENC_TEST_VECTORS);
1172 test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
1173 DES3_EDE_DEC_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -07001174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -07001176
Jonathan Lynchcd12fb92007-11-10 20:08:25 +08001177 test_hash("sha224", sha224_tv_template, SHA224_TEST_VECTORS);
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -07001180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 //BLOWFISH
Herbert Xucba83562006-08-13 08:26:09 +10001182 test_cipher("ecb(blowfish)", ENCRYPT, bf_enc_tv_template,
1183 BF_ENC_TEST_VECTORS);
1184 test_cipher("ecb(blowfish)", DECRYPT, bf_dec_tv_template,
1185 BF_DEC_TEST_VECTORS);
1186 test_cipher("cbc(blowfish)", ENCRYPT, bf_cbc_enc_tv_template,
1187 BF_CBC_ENC_TEST_VECTORS);
1188 test_cipher("cbc(blowfish)", DECRYPT, bf_cbc_dec_tv_template,
1189 BF_CBC_DEC_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -07001190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 //TWOFISH
Herbert Xucba83562006-08-13 08:26:09 +10001192 test_cipher("ecb(twofish)", ENCRYPT, tf_enc_tv_template,
1193 TF_ENC_TEST_VECTORS);
1194 test_cipher("ecb(twofish)", DECRYPT, tf_dec_tv_template,
1195 TF_DEC_TEST_VECTORS);
1196 test_cipher("cbc(twofish)", ENCRYPT, tf_cbc_enc_tv_template,
1197 TF_CBC_ENC_TEST_VECTORS);
1198 test_cipher("cbc(twofish)", DECRYPT, tf_cbc_dec_tv_template,
1199 TF_CBC_DEC_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -07001200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 //SERPENT
Herbert Xucba83562006-08-13 08:26:09 +10001202 test_cipher("ecb(serpent)", ENCRYPT, serpent_enc_tv_template,
1203 SERPENT_ENC_TEST_VECTORS);
1204 test_cipher("ecb(serpent)", DECRYPT, serpent_dec_tv_template,
1205 SERPENT_DEC_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -07001206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 //TNEPRES
Herbert Xucba83562006-08-13 08:26:09 +10001208 test_cipher("ecb(tnepres)", ENCRYPT, tnepres_enc_tv_template,
1209 TNEPRES_ENC_TEST_VECTORS);
1210 test_cipher("ecb(tnepres)", DECRYPT, tnepres_dec_tv_template,
1211 TNEPRES_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 //AES
Herbert Xucba83562006-08-13 08:26:09 +10001214 test_cipher("ecb(aes)", ENCRYPT, aes_enc_tv_template,
1215 AES_ENC_TEST_VECTORS);
1216 test_cipher("ecb(aes)", DECRYPT, aes_dec_tv_template,
1217 AES_DEC_TEST_VECTORS);
1218 test_cipher("cbc(aes)", ENCRYPT, aes_cbc_enc_tv_template,
1219 AES_CBC_ENC_TEST_VECTORS);
1220 test_cipher("cbc(aes)", DECRYPT, aes_cbc_dec_tv_template,
1221 AES_CBC_DEC_TEST_VECTORS);
Rik Snelf3d10442006-11-29 19:01:41 +11001222 test_cipher("lrw(aes)", ENCRYPT, aes_lrw_enc_tv_template,
1223 AES_LRW_ENC_TEST_VECTORS);
1224 test_cipher("lrw(aes)", DECRYPT, aes_lrw_dec_tv_template,
1225 AES_LRW_DEC_TEST_VECTORS);
Rik Snelf19f5112007-09-19 20:23:13 +08001226 test_cipher("xts(aes)", ENCRYPT, aes_xts_enc_tv_template,
1227 AES_XTS_ENC_TEST_VECTORS);
1228 test_cipher("xts(aes)", DECRYPT, aes_xts_dec_tv_template,
1229 AES_XTS_DEC_TEST_VECTORS);
Herbert Xu5311f242007-12-17 21:34:32 +08001230 test_cipher("rfc3686(ctr(aes))", ENCRYPT, aes_ctr_enc_tv_template,
Joy Latten23e353c2007-10-23 08:50:32 +08001231 AES_CTR_ENC_TEST_VECTORS);
Herbert Xu5311f242007-12-17 21:34:32 +08001232 test_cipher("rfc3686(ctr(aes))", DECRYPT, aes_ctr_dec_tv_template,
Joy Latten23e353c2007-10-23 08:50:32 +08001233 AES_CTR_DEC_TEST_VECTORS);
Mikko Herranen28db8e32007-11-26 22:24:11 +08001234 test_aead("gcm(aes)", ENCRYPT, aes_gcm_enc_tv_template,
1235 AES_GCM_ENC_TEST_VECTORS);
1236 test_aead("gcm(aes)", DECRYPT, aes_gcm_dec_tv_template,
1237 AES_GCM_DEC_TEST_VECTORS);
Joy Latten93cc74e2007-12-12 20:24:22 +08001238 test_aead("ccm(aes)", ENCRYPT, aes_ccm_enc_tv_template,
1239 AES_CCM_ENC_TEST_VECTORS);
1240 test_aead("ccm(aes)", DECRYPT, aes_ccm_dec_tv_template,
1241 AES_CCM_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 //CAST5
Herbert Xucba83562006-08-13 08:26:09 +10001244 test_cipher("ecb(cast5)", ENCRYPT, cast5_enc_tv_template,
1245 CAST5_ENC_TEST_VECTORS);
1246 test_cipher("ecb(cast5)", DECRYPT, cast5_dec_tv_template,
1247 CAST5_DEC_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -07001248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 //CAST6
Herbert Xucba83562006-08-13 08:26:09 +10001250 test_cipher("ecb(cast6)", ENCRYPT, cast6_enc_tv_template,
1251 CAST6_ENC_TEST_VECTORS);
1252 test_cipher("ecb(cast6)", DECRYPT, cast6_dec_tv_template,
1253 CAST6_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 //ARC4
Herbert Xucba83562006-08-13 08:26:09 +10001256 test_cipher("ecb(arc4)", ENCRYPT, arc4_enc_tv_template,
1257 ARC4_ENC_TEST_VECTORS);
1258 test_cipher("ecb(arc4)", DECRYPT, arc4_dec_tv_template,
1259 ARC4_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 //TEA
Herbert Xucba83562006-08-13 08:26:09 +10001262 test_cipher("ecb(tea)", ENCRYPT, tea_enc_tv_template,
1263 TEA_ENC_TEST_VECTORS);
1264 test_cipher("ecb(tea)", DECRYPT, tea_dec_tv_template,
1265 TEA_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267
1268 //XTEA
Herbert Xucba83562006-08-13 08:26:09 +10001269 test_cipher("ecb(xtea)", ENCRYPT, xtea_enc_tv_template,
1270 XTEA_ENC_TEST_VECTORS);
1271 test_cipher("ecb(xtea)", DECRYPT, xtea_dec_tv_template,
1272 XTEA_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 //KHAZAD
Herbert Xucba83562006-08-13 08:26:09 +10001275 test_cipher("ecb(khazad)", ENCRYPT, khazad_enc_tv_template,
1276 KHAZAD_ENC_TEST_VECTORS);
1277 test_cipher("ecb(khazad)", DECRYPT, khazad_dec_tv_template,
1278 KHAZAD_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 //ANUBIS
Herbert Xucba83562006-08-13 08:26:09 +10001281 test_cipher("ecb(anubis)", ENCRYPT, anubis_enc_tv_template,
1282 ANUBIS_ENC_TEST_VECTORS);
1283 test_cipher("ecb(anubis)", DECRYPT, anubis_dec_tv_template,
1284 ANUBIS_DEC_TEST_VECTORS);
1285 test_cipher("cbc(anubis)", ENCRYPT, anubis_cbc_enc_tv_template,
1286 ANUBIS_CBC_ENC_TEST_VECTORS);
1287 test_cipher("cbc(anubis)", DECRYPT, anubis_cbc_dec_tv_template,
1288 ANUBIS_CBC_ENC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Aaron Grothefb4f10e2005-09-01 17:42:46 -07001290 //XETA
Herbert Xucba83562006-08-13 08:26:09 +10001291 test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
1292 XETA_ENC_TEST_VECTORS);
1293 test_cipher("ecb(xeta)", DECRYPT, xeta_dec_tv_template,
1294 XETA_DEC_TEST_VECTORS);
Aaron Grothefb4f10e2005-09-01 17:42:46 -07001295
David Howells90831632006-12-16 12:13:14 +11001296 //FCrypt
1297 test_cipher("pcbc(fcrypt)", ENCRYPT, fcrypt_pcbc_enc_tv_template,
1298 FCRYPT_ENC_TEST_VECTORS);
1299 test_cipher("pcbc(fcrypt)", DECRYPT, fcrypt_pcbc_dec_tv_template,
1300 FCRYPT_DEC_TEST_VECTORS);
1301
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +11001302 //CAMELLIA
1303 test_cipher("ecb(camellia)", ENCRYPT,
1304 camellia_enc_tv_template,
1305 CAMELLIA_ENC_TEST_VECTORS);
1306 test_cipher("ecb(camellia)", DECRYPT,
1307 camellia_dec_tv_template,
1308 CAMELLIA_DEC_TEST_VECTORS);
1309 test_cipher("cbc(camellia)", ENCRYPT,
1310 camellia_cbc_enc_tv_template,
1311 CAMELLIA_CBC_ENC_TEST_VECTORS);
1312 test_cipher("cbc(camellia)", DECRYPT,
1313 camellia_cbc_dec_tv_template,
1314 CAMELLIA_CBC_DEC_TEST_VECTORS);
1315
Hye-Shik Change2ee95b2007-08-21 20:01:03 +08001316 //SEED
1317 test_cipher("ecb(seed)", ENCRYPT, seed_enc_tv_template,
1318 SEED_ENC_TEST_VECTORS);
1319 test_cipher("ecb(seed)", DECRYPT, seed_dec_tv_template,
1320 SEED_DEC_TEST_VECTORS);
1321
Kevin Coffman76cb9522008-03-24 21:26:16 +08001322 //CTS
1323 test_cipher("cts(cbc(aes))", ENCRYPT, cts_mode_enc_tv_template,
1324 CTS_MODE_ENC_TEST_VECTORS);
1325 test_cipher("cts(cbc(aes))", DECRYPT, cts_mode_dec_tv_template,
1326 CTS_MODE_DEC_TEST_VECTORS);
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
1329 test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
1330 test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
1331 test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
1332 test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
1333 test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
1334 test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
1335 test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
Zoltan Sogor91755a92007-12-07 16:48:11 +08001336 test_comp("deflate", deflate_comp_tv_template,
1337 deflate_decomp_tv_template, DEFLATE_COMP_TEST_VECTORS,
1338 DEFLATE_DECOMP_TEST_VECTORS);
Zoltan Sogor0b77abb2007-12-07 16:53:23 +08001339 test_comp("lzo", lzo_comp_tv_template, lzo_decomp_tv_template,
1340 LZO_COMP_TEST_VECTORS, LZO_DECOMP_TEST_VECTORS);
Herbert Xuc907ee72006-08-21 22:04:03 +10001341 test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
Herbert Xue9d41162006-08-19 21:38:49 +10001342 test_hash("hmac(md5)", hmac_md5_tv_template,
1343 HMAC_MD5_TEST_VECTORS);
1344 test_hash("hmac(sha1)", hmac_sha1_tv_template,
1345 HMAC_SHA1_TEST_VECTORS);
Jonathan Lynchcd12fb92007-11-10 20:08:25 +08001346 test_hash("hmac(sha224)", hmac_sha224_tv_template,
1347 HMAC_SHA224_TEST_VECTORS);
Herbert Xue9d41162006-08-19 21:38:49 +10001348 test_hash("hmac(sha256)", hmac_sha256_tv_template,
1349 HMAC_SHA256_TEST_VECTORS);
Andrew Donofrioa28091a2006-12-10 12:10:20 +11001350 test_hash("hmac(sha384)", hmac_sha384_tv_template,
1351 HMAC_SHA384_TEST_VECTORS);
1352 test_hash("hmac(sha512)", hmac_sha512_tv_template,
1353 HMAC_SHA512_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Kazunori MIYAZAWA5b2becf2006-10-28 13:18:53 +10001355 test_hash("xcbc(aes)", aes_xcbc128_tv_template,
1356 XCBC_AES_TEST_VECTORS);
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
1359 break;
1360
1361 case 1:
1362 test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
1363 break;
1364
1365 case 2:
1366 test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
1367 break;
1368
1369 case 3:
Herbert Xucba83562006-08-13 08:26:09 +10001370 test_cipher("ecb(des)", ENCRYPT, des_enc_tv_template,
1371 DES_ENC_TEST_VECTORS);
1372 test_cipher("ecb(des)", DECRYPT, des_dec_tv_template,
1373 DES_DEC_TEST_VECTORS);
1374 test_cipher("cbc(des)", ENCRYPT, des_cbc_enc_tv_template,
1375 DES_CBC_ENC_TEST_VECTORS);
1376 test_cipher("cbc(des)", DECRYPT, des_cbc_dec_tv_template,
1377 DES_CBC_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 break;
1379
1380 case 4:
Herbert Xucba83562006-08-13 08:26:09 +10001381 test_cipher("ecb(des3_ede)", ENCRYPT, des3_ede_enc_tv_template,
1382 DES3_EDE_ENC_TEST_VECTORS);
1383 test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
1384 DES3_EDE_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 break;
1386
1387 case 5:
1388 test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
1389 break;
Herbert Xuef2736f2005-06-22 13:26:03 -07001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 case 6:
1392 test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
1393 break;
Herbert Xuef2736f2005-06-22 13:26:03 -07001394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 case 7:
Herbert Xucba83562006-08-13 08:26:09 +10001396 test_cipher("ecb(blowfish)", ENCRYPT, bf_enc_tv_template,
1397 BF_ENC_TEST_VECTORS);
1398 test_cipher("ecb(blowfish)", DECRYPT, bf_dec_tv_template,
1399 BF_DEC_TEST_VECTORS);
1400 test_cipher("cbc(blowfish)", ENCRYPT, bf_cbc_enc_tv_template,
1401 BF_CBC_ENC_TEST_VECTORS);
1402 test_cipher("cbc(blowfish)", DECRYPT, bf_cbc_dec_tv_template,
1403 BF_CBC_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 break;
1405
1406 case 8:
Herbert Xucba83562006-08-13 08:26:09 +10001407 test_cipher("ecb(twofish)", ENCRYPT, tf_enc_tv_template,
1408 TF_ENC_TEST_VECTORS);
1409 test_cipher("ecb(twofish)", DECRYPT, tf_dec_tv_template,
1410 TF_DEC_TEST_VECTORS);
1411 test_cipher("cbc(twofish)", ENCRYPT, tf_cbc_enc_tv_template,
1412 TF_CBC_ENC_TEST_VECTORS);
1413 test_cipher("cbc(twofish)", DECRYPT, tf_cbc_dec_tv_template,
1414 TF_CBC_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 break;
Herbert Xuef2736f2005-06-22 13:26:03 -07001416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 case 9:
Herbert Xucba83562006-08-13 08:26:09 +10001418 test_cipher("ecb(serpent)", ENCRYPT, serpent_enc_tv_template,
1419 SERPENT_ENC_TEST_VECTORS);
1420 test_cipher("ecb(serpent)", DECRYPT, serpent_dec_tv_template,
1421 SERPENT_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 break;
1423
1424 case 10:
Herbert Xucba83562006-08-13 08:26:09 +10001425 test_cipher("ecb(aes)", ENCRYPT, aes_enc_tv_template,
1426 AES_ENC_TEST_VECTORS);
1427 test_cipher("ecb(aes)", DECRYPT, aes_dec_tv_template,
1428 AES_DEC_TEST_VECTORS);
1429 test_cipher("cbc(aes)", ENCRYPT, aes_cbc_enc_tv_template,
1430 AES_CBC_ENC_TEST_VECTORS);
1431 test_cipher("cbc(aes)", DECRYPT, aes_cbc_dec_tv_template,
1432 AES_CBC_DEC_TEST_VECTORS);
Rik Snelf3d10442006-11-29 19:01:41 +11001433 test_cipher("lrw(aes)", ENCRYPT, aes_lrw_enc_tv_template,
1434 AES_LRW_ENC_TEST_VECTORS);
1435 test_cipher("lrw(aes)", DECRYPT, aes_lrw_dec_tv_template,
1436 AES_LRW_DEC_TEST_VECTORS);
Rik Snelf19f5112007-09-19 20:23:13 +08001437 test_cipher("xts(aes)", ENCRYPT, aes_xts_enc_tv_template,
1438 AES_XTS_ENC_TEST_VECTORS);
1439 test_cipher("xts(aes)", DECRYPT, aes_xts_dec_tv_template,
1440 AES_XTS_DEC_TEST_VECTORS);
Herbert Xu5311f242007-12-17 21:34:32 +08001441 test_cipher("rfc3686(ctr(aes))", ENCRYPT, aes_ctr_enc_tv_template,
Joy Latten23e353c2007-10-23 08:50:32 +08001442 AES_CTR_ENC_TEST_VECTORS);
Herbert Xu5311f242007-12-17 21:34:32 +08001443 test_cipher("rfc3686(ctr(aes))", DECRYPT, aes_ctr_dec_tv_template,
Joy Latten23e353c2007-10-23 08:50:32 +08001444 AES_CTR_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 break;
1446
1447 case 11:
1448 test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
1449 break;
Herbert Xuef2736f2005-06-22 13:26:03 -07001450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 case 12:
1452 test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
1453 break;
1454
1455 case 13:
Zoltan Sogor91755a92007-12-07 16:48:11 +08001456 test_comp("deflate", deflate_comp_tv_template,
1457 deflate_decomp_tv_template, DEFLATE_COMP_TEST_VECTORS,
1458 DEFLATE_DECOMP_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 break;
1460
1461 case 14:
Herbert Xucba83562006-08-13 08:26:09 +10001462 test_cipher("ecb(cast5)", ENCRYPT, cast5_enc_tv_template,
1463 CAST5_ENC_TEST_VECTORS);
1464 test_cipher("ecb(cast5)", DECRYPT, cast5_dec_tv_template,
1465 CAST5_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 break;
1467
1468 case 15:
Herbert Xucba83562006-08-13 08:26:09 +10001469 test_cipher("ecb(cast6)", ENCRYPT, cast6_enc_tv_template,
1470 CAST6_ENC_TEST_VECTORS);
1471 test_cipher("ecb(cast6)", DECRYPT, cast6_dec_tv_template,
1472 CAST6_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 break;
1474
1475 case 16:
Herbert Xucba83562006-08-13 08:26:09 +10001476 test_cipher("ecb(arc4)", ENCRYPT, arc4_enc_tv_template,
1477 ARC4_ENC_TEST_VECTORS);
1478 test_cipher("ecb(arc4)", DECRYPT, arc4_dec_tv_template,
1479 ARC4_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 break;
1481
1482 case 17:
1483 test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
1484 break;
1485
1486 case 18:
Herbert Xuc907ee72006-08-21 22:04:03 +10001487 test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 break;
1489
1490 case 19:
Herbert Xucba83562006-08-13 08:26:09 +10001491 test_cipher("ecb(tea)", ENCRYPT, tea_enc_tv_template,
1492 TEA_ENC_TEST_VECTORS);
1493 test_cipher("ecb(tea)", DECRYPT, tea_dec_tv_template,
1494 TEA_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 break;
1496
1497 case 20:
Herbert Xucba83562006-08-13 08:26:09 +10001498 test_cipher("ecb(xtea)", ENCRYPT, xtea_enc_tv_template,
1499 XTEA_ENC_TEST_VECTORS);
1500 test_cipher("ecb(xtea)", DECRYPT, xtea_dec_tv_template,
1501 XTEA_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 break;
1503
1504 case 21:
Herbert Xucba83562006-08-13 08:26:09 +10001505 test_cipher("ecb(khazad)", ENCRYPT, khazad_enc_tv_template,
1506 KHAZAD_ENC_TEST_VECTORS);
1507 test_cipher("ecb(khazad)", DECRYPT, khazad_dec_tv_template,
1508 KHAZAD_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 break;
1510
1511 case 22:
1512 test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
1513 break;
1514
1515 case 23:
1516 test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
1517 break;
1518
1519 case 24:
1520 test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
1521 break;
1522
1523 case 25:
Herbert Xucba83562006-08-13 08:26:09 +10001524 test_cipher("ecb(tnepres)", ENCRYPT, tnepres_enc_tv_template,
1525 TNEPRES_ENC_TEST_VECTORS);
1526 test_cipher("ecb(tnepres)", DECRYPT, tnepres_dec_tv_template,
1527 TNEPRES_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 break;
1529
1530 case 26:
Herbert Xucba83562006-08-13 08:26:09 +10001531 test_cipher("ecb(anubis)", ENCRYPT, anubis_enc_tv_template,
1532 ANUBIS_ENC_TEST_VECTORS);
1533 test_cipher("ecb(anubis)", DECRYPT, anubis_dec_tv_template,
1534 ANUBIS_DEC_TEST_VECTORS);
1535 test_cipher("cbc(anubis)", ENCRYPT, anubis_cbc_enc_tv_template,
1536 ANUBIS_CBC_ENC_TEST_VECTORS);
1537 test_cipher("cbc(anubis)", DECRYPT, anubis_cbc_dec_tv_template,
1538 ANUBIS_CBC_ENC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 break;
1540
1541 case 27:
1542 test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
1543 break;
1544
1545 case 28:
1546
1547 test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
1548 break;
1549
1550 case 29:
1551 test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
1552 break;
Aaron Grothefb4f10e2005-09-01 17:42:46 -07001553
1554 case 30:
Herbert Xucba83562006-08-13 08:26:09 +10001555 test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
1556 XETA_ENC_TEST_VECTORS);
1557 test_cipher("ecb(xeta)", DECRYPT, xeta_dec_tv_template,
1558 XETA_DEC_TEST_VECTORS);
Aaron Grothefb4f10e2005-09-01 17:42:46 -07001559 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
David Howells90831632006-12-16 12:13:14 +11001561 case 31:
1562 test_cipher("pcbc(fcrypt)", ENCRYPT, fcrypt_pcbc_enc_tv_template,
1563 FCRYPT_ENC_TEST_VECTORS);
1564 test_cipher("pcbc(fcrypt)", DECRYPT, fcrypt_pcbc_dec_tv_template,
1565 FCRYPT_DEC_TEST_VECTORS);
1566 break;
1567
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +11001568 case 32:
1569 test_cipher("ecb(camellia)", ENCRYPT,
1570 camellia_enc_tv_template,
1571 CAMELLIA_ENC_TEST_VECTORS);
1572 test_cipher("ecb(camellia)", DECRYPT,
1573 camellia_dec_tv_template,
1574 CAMELLIA_DEC_TEST_VECTORS);
1575 test_cipher("cbc(camellia)", ENCRYPT,
1576 camellia_cbc_enc_tv_template,
1577 CAMELLIA_CBC_ENC_TEST_VECTORS);
1578 test_cipher("cbc(camellia)", DECRYPT,
1579 camellia_cbc_dec_tv_template,
1580 CAMELLIA_CBC_DEC_TEST_VECTORS);
1581 break;
Jonathan Lynchcd12fb92007-11-10 20:08:25 +08001582 case 33:
1583 test_hash("sha224", sha224_tv_template, SHA224_TEST_VECTORS);
1584 break;
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +11001585
Tan Swee Heng2407d602007-11-23 19:45:00 +08001586 case 34:
1587 test_cipher("salsa20", ENCRYPT,
1588 salsa20_stream_enc_tv_template,
1589 SALSA20_STREAM_ENC_TEST_VECTORS);
1590 break;
1591
Herbert Xu8df213d2007-12-02 14:55:47 +11001592 case 35:
1593 test_aead("gcm(aes)", ENCRYPT, aes_gcm_enc_tv_template,
1594 AES_GCM_ENC_TEST_VECTORS);
1595 test_aead("gcm(aes)", DECRYPT, aes_gcm_dec_tv_template,
1596 AES_GCM_DEC_TEST_VECTORS);
1597 break;
1598
Zoltan Sogor0b77abb2007-12-07 16:53:23 +08001599 case 36:
1600 test_comp("lzo", lzo_comp_tv_template, lzo_decomp_tv_template,
1601 LZO_COMP_TEST_VECTORS, LZO_DECOMP_TEST_VECTORS);
1602 break;
1603
Joy Latten93cc74e2007-12-12 20:24:22 +08001604 case 37:
1605 test_aead("ccm(aes)", ENCRYPT, aes_ccm_enc_tv_template,
1606 AES_CCM_ENC_TEST_VECTORS);
1607 test_aead("ccm(aes)", DECRYPT, aes_ccm_dec_tv_template,
1608 AES_CCM_DEC_TEST_VECTORS);
1609 break;
1610
Kevin Coffman76cb9522008-03-24 21:26:16 +08001611 case 38:
1612 test_cipher("cts(cbc(aes))", ENCRYPT, cts_mode_enc_tv_template,
1613 CTS_MODE_ENC_TEST_VECTORS);
1614 test_cipher("cts(cbc(aes))", DECRYPT, cts_mode_dec_tv_template,
1615 CTS_MODE_DEC_TEST_VECTORS);
1616 break;
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 case 100:
Herbert Xue9d41162006-08-19 21:38:49 +10001619 test_hash("hmac(md5)", hmac_md5_tv_template,
1620 HMAC_MD5_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 break;
Herbert Xuef2736f2005-06-22 13:26:03 -07001622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 case 101:
Herbert Xue9d41162006-08-19 21:38:49 +10001624 test_hash("hmac(sha1)", hmac_sha1_tv_template,
1625 HMAC_SHA1_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 break;
Herbert Xuef2736f2005-06-22 13:26:03 -07001627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 case 102:
Herbert Xue9d41162006-08-19 21:38:49 +10001629 test_hash("hmac(sha256)", hmac_sha256_tv_template,
1630 HMAC_SHA256_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 break;
1632
Andrew Donofrioa28091a2006-12-10 12:10:20 +11001633 case 103:
1634 test_hash("hmac(sha384)", hmac_sha384_tv_template,
1635 HMAC_SHA384_TEST_VECTORS);
1636 break;
1637
1638 case 104:
1639 test_hash("hmac(sha512)", hmac_sha512_tv_template,
1640 HMAC_SHA512_TEST_VECTORS);
1641 break;
Herbert Xu38ed9ab2008-01-01 15:59:28 +11001642
Jonathan Lynchcd12fb92007-11-10 20:08:25 +08001643 case 105:
1644 test_hash("hmac(sha224)", hmac_sha224_tv_template,
1645 HMAC_SHA224_TEST_VECTORS);
1646 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Herbert Xu38ed9ab2008-01-01 15:59:28 +11001648 case 106:
1649 test_hash("xcbc(aes)", aes_xcbc128_tv_template,
1650 XCBC_AES_TEST_VECTORS);
1651 break;
1652
Harald Welteebfd9bc2005-06-22 13:27:23 -07001653 case 200:
Herbert Xucba83562006-08-13 08:26:09 +10001654 test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001655 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +10001656 test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001657 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +10001658 test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001659 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +10001660 test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001661 speed_template_16_24_32);
Rik Snelf3d10442006-11-29 19:01:41 +11001662 test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001663 speed_template_32_40_48);
Rik Snelf3d10442006-11-29 19:01:41 +11001664 test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001665 speed_template_32_40_48);
Rik Snelf19f5112007-09-19 20:23:13 +08001666 test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001667 speed_template_32_48_64);
Rik Snelf19f5112007-09-19 20:23:13 +08001668 test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001669 speed_template_32_48_64);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001670 break;
1671
1672 case 201:
Herbert Xucba83562006-08-13 08:26:09 +10001673 test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001674 des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS,
1675 speed_template_24);
Herbert Xucba83562006-08-13 08:26:09 +10001676 test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001677 des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS,
1678 speed_template_24);
Herbert Xucba83562006-08-13 08:26:09 +10001679 test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001680 des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS,
1681 speed_template_24);
Herbert Xucba83562006-08-13 08:26:09 +10001682 test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001683 des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS,
1684 speed_template_24);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001685 break;
1686
1687 case 202:
Herbert Xucba83562006-08-13 08:26:09 +10001688 test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001689 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +10001690 test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001691 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +10001692 test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001693 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +10001694 test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001695 speed_template_16_24_32);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001696 break;
1697
1698 case 203:
Herbert Xucba83562006-08-13 08:26:09 +10001699 test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001700 speed_template_8_32);
Herbert Xucba83562006-08-13 08:26:09 +10001701 test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001702 speed_template_8_32);
Herbert Xucba83562006-08-13 08:26:09 +10001703 test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001704 speed_template_8_32);
Herbert Xucba83562006-08-13 08:26:09 +10001705 test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001706 speed_template_8_32);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001707 break;
1708
1709 case 204:
Herbert Xucba83562006-08-13 08:26:09 +10001710 test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001711 speed_template_8);
Herbert Xucba83562006-08-13 08:26:09 +10001712 test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001713 speed_template_8);
Herbert Xucba83562006-08-13 08:26:09 +10001714 test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001715 speed_template_8);
Herbert Xucba83562006-08-13 08:26:09 +10001716 test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001717 speed_template_8);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001718 break;
1719
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +11001720 case 205:
1721 test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001722 speed_template_16_24_32);
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +11001723 test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001724 speed_template_16_24_32);
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +11001725 test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001726 speed_template_16_24_32);
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +11001727 test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001728 speed_template_16_24_32);
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +11001729 break;
1730
Tan Swee Heng5de8f1b2007-12-07 17:17:43 +08001731 case 206:
1732 test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001733 speed_template_16_32);
Tan Swee Heng5de8f1b2007-12-07 17:17:43 +08001734 break;
1735
Michal Ludvige8057922006-05-30 22:04:19 +10001736 case 300:
1737 /* fall through */
1738
1739 case 301:
Herbert Xue9d41162006-08-19 21:38:49 +10001740 test_hash_speed("md4", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001741 if (mode > 300 && mode < 400) break;
1742
1743 case 302:
Herbert Xue9d41162006-08-19 21:38:49 +10001744 test_hash_speed("md5", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001745 if (mode > 300 && mode < 400) break;
1746
1747 case 303:
Herbert Xue9d41162006-08-19 21:38:49 +10001748 test_hash_speed("sha1", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001749 if (mode > 300 && mode < 400) break;
1750
1751 case 304:
Herbert Xue9d41162006-08-19 21:38:49 +10001752 test_hash_speed("sha256", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001753 if (mode > 300 && mode < 400) break;
1754
1755 case 305:
Herbert Xue9d41162006-08-19 21:38:49 +10001756 test_hash_speed("sha384", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001757 if (mode > 300 && mode < 400) break;
1758
1759 case 306:
Herbert Xue9d41162006-08-19 21:38:49 +10001760 test_hash_speed("sha512", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001761 if (mode > 300 && mode < 400) break;
1762
1763 case 307:
Herbert Xue9d41162006-08-19 21:38:49 +10001764 test_hash_speed("wp256", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001765 if (mode > 300 && mode < 400) break;
1766
1767 case 308:
Herbert Xue9d41162006-08-19 21:38:49 +10001768 test_hash_speed("wp384", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001769 if (mode > 300 && mode < 400) break;
1770
1771 case 309:
Herbert Xue9d41162006-08-19 21:38:49 +10001772 test_hash_speed("wp512", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001773 if (mode > 300 && mode < 400) break;
1774
1775 case 310:
Herbert Xue9d41162006-08-19 21:38:49 +10001776 test_hash_speed("tgr128", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001777 if (mode > 300 && mode < 400) break;
1778
1779 case 311:
Herbert Xue9d41162006-08-19 21:38:49 +10001780 test_hash_speed("tgr160", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001781 if (mode > 300 && mode < 400) break;
1782
1783 case 312:
Herbert Xue9d41162006-08-19 21:38:49 +10001784 test_hash_speed("tgr192", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001785 if (mode > 300 && mode < 400) break;
1786
Jonathan Lynchcd12fb92007-11-10 20:08:25 +08001787 case 313:
1788 test_hash_speed("sha224", sec, generic_hash_speed_template);
1789 if (mode > 300 && mode < 400) break;
1790
Michal Ludvige8057922006-05-30 22:04:19 +10001791 case 399:
1792 break;
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 case 1000:
1795 test_available();
1796 break;
Herbert Xuef2736f2005-06-22 13:26:03 -07001797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 default:
1799 /* useful for debugging */
1800 printk("not testing anything\n");
1801 break;
1802 }
1803}
1804
Kamalesh Babulal3af5b902008-04-05 21:00:57 +08001805static int __init tcrypt_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
Mikko Herranene3a4ea42007-11-26 22:12:07 +08001807 int err = -ENOMEM;
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 tvmem = kmalloc(TVMEMSIZE, GFP_KERNEL);
1810 if (tvmem == NULL)
Mikko Herranene3a4ea42007-11-26 22:12:07 +08001811 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
1813 xbuf = kmalloc(XBUFSIZE, GFP_KERNEL);
Mikko Herranene3a4ea42007-11-26 22:12:07 +08001814 if (xbuf == NULL)
1815 goto err_free_tv;
1816
1817 axbuf = kmalloc(XBUFSIZE, GFP_KERNEL);
1818 if (axbuf == NULL)
1819 goto err_free_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
1821 do_test();
1822
Michal Ludvig14fdf472006-05-30 14:49:38 +10001823 /* We intentionaly return -EAGAIN to prevent keeping
1824 * the module. It does all its work from init()
1825 * and doesn't offer any runtime functionality
1826 * => we don't need it in the memory, do we?
1827 * -- mludvig
1828 */
Mikko Herranene3a4ea42007-11-26 22:12:07 +08001829 err = -EAGAIN;
1830
1831 kfree(axbuf);
1832 err_free_xbuf:
1833 kfree(xbuf);
1834 err_free_tv:
1835 kfree(tvmem);
1836
1837 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
1840/*
1841 * If an init function is provided, an exit function must also be provided
1842 * to allow module unload.
1843 */
Kamalesh Babulal3af5b902008-04-05 21:00:57 +08001844static void __exit tcrypt_mod_fini(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Kamalesh Babulal3af5b902008-04-05 21:00:57 +08001846module_init(tcrypt_mod_init);
1847module_exit(tcrypt_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849module_param(mode, int, 0);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001850module_param(sec, uint, 0);
Herbert Xu6a179442005-06-22 13:29:03 -07001851MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
1852 "(defaults to zero which uses CPU cycles instead)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
1854MODULE_LICENSE("GPL");
1855MODULE_DESCRIPTION("Quick & dirty crypto testing module");
1856MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");