blob: 30e75d49f35af2ffd9ad9b63ce57ca3c4e7f91f5 [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",
Herbert Xuef2736f2005-06-22 13:26:03 -070085 "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
David Howells90831632006-12-16 12:13:14 +110086 "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", "fcrypt",
Zoltan Sogor0b77abb2007-12-07 16:53:23 +080087 "camellia", "seed", "salsa20", "lzo", NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
89
Herbert Xuef2736f2005-06-22 13:26:03 -070090static void hexdump(unsigned char *buf, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Denis Chenga10e1192007-11-30 16:59:30 +110092 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
93 16, 1,
94 buf, len, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Herbert Xu6158efc2007-04-04 17:41:07 +100097static void tcrypt_complete(struct crypto_async_request *req, int err)
98{
99 struct tcrypt_result *res = req->data;
100
101 if (err == -EINPROGRESS)
102 return;
103
104 res->err = err;
105 complete(&res->completion);
106}
107
Herbert Xuef2736f2005-06-22 13:26:03 -0700108static void test_hash(char *algo, struct hash_testvec *template,
109 unsigned int tcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Herbert Xuef2736f2005-06-22 13:26:03 -0700111 unsigned int i, j, k, temp;
112 struct scatterlist sg[8];
113 char result[64];
Herbert Xue9d41162006-08-19 21:38:49 +1000114 struct crypto_hash *tfm;
115 struct hash_desc desc;
Herbert Xue9d41162006-08-19 21:38:49 +1000116 int ret;
Sebastian Siewior562954d2008-03-13 20:20:28 +0800117 void *hash_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Herbert Xuef2736f2005-06-22 13:26:03 -0700119 printk("\ntesting %s\n", algo);
120
Herbert Xue9d41162006-08-19 21:38:49 +1000121 tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
122 if (IS_ERR(tfm)) {
123 printk("failed to load transform for %s: %ld\n", algo,
124 PTR_ERR(tfm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return;
126 }
127
Herbert Xue9d41162006-08-19 21:38:49 +1000128 desc.tfm = tfm;
129 desc.flags = 0;
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 for (i = 0; i < tcount; i++) {
Herbert Xuef2736f2005-06-22 13:26:03 -0700132 printk("test %u:\n", i + 1);
133 memset(result, 0, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Sebastian Siewior562954d2008-03-13 20:20:28 +0800135 hash_buff = kzalloc(template[i].psize, GFP_KERNEL);
136 if (!hash_buff)
137 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Sebastian Siewior562954d2008-03-13 20:20:28 +0800139 memcpy(hash_buff, template[i].plaintext, template[i].psize);
140 sg_init_one(&sg[0], hash_buff, template[i].psize);
141
142 if (template[i].ksize) {
143 ret = crypto_hash_setkey(tfm, template[i].key,
144 template[i].ksize);
Herbert Xue9d41162006-08-19 21:38:49 +1000145 if (ret) {
146 printk("setkey() failed ret=%d\n", ret);
Sebastian Siewior562954d2008-03-13 20:20:28 +0800147 kfree(hash_buff);
Herbert Xue9d41162006-08-19 21:38:49 +1000148 goto out;
149 }
150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Sebastian Siewior562954d2008-03-13 20:20:28 +0800152 ret = crypto_hash_digest(&desc, sg, template[i].psize, result);
Herbert Xue9d41162006-08-19 21:38:49 +1000153 if (ret) {
154 printk("digest () failed ret=%d\n", ret);
Sebastian Siewior562954d2008-03-13 20:20:28 +0800155 kfree(hash_buff);
Herbert Xue9d41162006-08-19 21:38:49 +1000156 goto out;
157 }
158
159 hexdump(result, crypto_hash_digestsize(tfm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 printk("%s\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +0800161 memcmp(result, template[i].digest,
Herbert Xue9d41162006-08-19 21:38:49 +1000162 crypto_hash_digestsize(tfm)) ?
Herbert Xuef2736f2005-06-22 13:26:03 -0700163 "fail" : "pass");
Sebastian Siewior562954d2008-03-13 20:20:28 +0800164 kfree(hash_buff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166
Herbert Xuef2736f2005-06-22 13:26:03 -0700167 printk("testing %s across pages\n", algo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 /* setup the dummy buffer first */
Herbert Xuef2736f2005-06-22 13:26:03 -0700170 memset(xbuf, 0, XBUFSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 j = 0;
173 for (i = 0; i < tcount; i++) {
Sebastian Siewior562954d2008-03-13 20:20:28 +0800174 if (template[i].np) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 j++;
Herbert Xuef2736f2005-06-22 13:26:03 -0700176 printk("test %u:\n", j);
177 memset(result, 0, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 temp = 0;
Sebastian Siewior562954d2008-03-13 20:20:28 +0800180 sg_init_table(sg, template[i].np);
181 for (k = 0; k < template[i].np; k++) {
Herbert Xuef2736f2005-06-22 13:26:03 -0700182 memcpy(&xbuf[IDX[k]],
Sebastian Siewior562954d2008-03-13 20:20:28 +0800183 template[i].plaintext + temp,
184 template[i].tap[k]);
185 temp += template[i].tap[k];
David Hardeman378f0582005-09-17 17:55:31 +1000186 sg_set_buf(&sg[k], &xbuf[IDX[k]],
Sebastian Siewior562954d2008-03-13 20:20:28 +0800187 template[i].tap[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189
Sebastian Siewior562954d2008-03-13 20:20:28 +0800190 if (template[i].ksize) {
191 ret = crypto_hash_setkey(tfm, template[i].key,
192 template[i].ksize);
Herbert Xuef2736f2005-06-22 13:26:03 -0700193
Herbert Xue9d41162006-08-19 21:38:49 +1000194 if (ret) {
195 printk("setkey() failed ret=%d\n", ret);
196 goto out;
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199
Sebastian Siewior562954d2008-03-13 20:20:28 +0800200 ret = crypto_hash_digest(&desc, sg, template[i].psize,
Herbert Xue9d41162006-08-19 21:38:49 +1000201 result);
202 if (ret) {
203 printk("digest () failed ret=%d\n", ret);
204 goto out;
205 }
Herbert Xuef2736f2005-06-22 13:26:03 -0700206
Herbert Xue9d41162006-08-19 21:38:49 +1000207 hexdump(result, crypto_hash_digestsize(tfm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 printk("%s\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +0800209 memcmp(result, template[i].digest,
Herbert Xue9d41162006-08-19 21:38:49 +1000210 crypto_hash_digestsize(tfm)) ?
Herbert Xuef2736f2005-06-22 13:26:03 -0700211 "fail" : "pass");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Herbert Xue9d41162006-08-19 21:38:49 +1000215out:
216 crypto_free_hash(tfm);
217}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800219static void test_aead(char *algo, int enc, struct aead_testvec *template,
220 unsigned int tcount)
221{
222 unsigned int ret, i, j, k, temp;
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800223 char *q;
224 struct crypto_aead *tfm;
225 char *key;
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800226 struct aead_request *req;
227 struct scatterlist sg[8];
228 struct scatterlist asg[8];
229 const char *e;
230 struct tcrypt_result result;
Herbert Xu6160b282007-12-04 19:17:50 +1100231 unsigned int authsize;
Sebastian Siewior562954d2008-03-13 20:20:28 +0800232 void *input;
233 void *assoc;
234 char iv[MAX_IVLEN];
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800235
236 if (enc == ENCRYPT)
237 e = "encryption";
238 else
239 e = "decryption";
240
241 printk(KERN_INFO "\ntesting %s %s\n", algo, e);
242
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800243 init_completion(&result.completion);
244
245 tfm = crypto_alloc_aead(algo, 0, 0);
246
247 if (IS_ERR(tfm)) {
248 printk(KERN_INFO "failed to load transform for %s: %ld\n",
249 algo, PTR_ERR(tfm));
250 return;
251 }
252
253 req = aead_request_alloc(tfm, GFP_KERNEL);
254 if (!req) {
255 printk(KERN_INFO "failed to allocate request for %s\n", algo);
256 goto out;
257 }
258
259 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
260 tcrypt_complete, &result);
261
262 for (i = 0, j = 0; i < tcount; i++) {
Sebastian Siewior562954d2008-03-13 20:20:28 +0800263 if (!template[i].np) {
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800264 printk(KERN_INFO "test %u (%d bit key):\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +0800265 ++j, template[i].klen * 8);
266
267 /* some tepmplates have no input data but they will
268 * touch input
269 */
270 input = kzalloc(template[i].ilen + template[i].rlen, GFP_KERNEL);
271 if (!input)
272 continue;
273
274 assoc = kzalloc(template[i].alen, GFP_KERNEL);
275 if (!assoc) {
276 kfree(input);
277 continue;
278 }
279
280 memcpy(input, template[i].input, template[i].ilen);
281 memcpy(assoc, template[i].assoc, template[i].alen);
282 if (template[i].iv)
283 memcpy(iv, template[i].iv, MAX_IVLEN);
284 else
285 memset(iv, 0, MAX_IVLEN);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800286
287 crypto_aead_clear_flags(tfm, ~0);
Sebastian Siewior562954d2008-03-13 20:20:28 +0800288 if (template[i].wk)
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800289 crypto_aead_set_flags(
290 tfm, CRYPTO_TFM_REQ_WEAK_KEY);
Sebastian Siewior562954d2008-03-13 20:20:28 +0800291
292 if (template[i].key)
293 key = template[i].key;
294 else
295 key = kzalloc(template[i].klen, GFP_KERNEL);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800296
297 ret = crypto_aead_setkey(tfm, key,
Sebastian Siewior562954d2008-03-13 20:20:28 +0800298 template[i].klen);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800299 if (ret) {
300 printk(KERN_INFO "setkey() failed flags=%x\n",
301 crypto_aead_get_flags(tfm));
302
Sebastian Siewior562954d2008-03-13 20:20:28 +0800303 if (!template[i].fail)
304 goto next_one;
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800305 }
306
Sebastian Siewior562954d2008-03-13 20:20:28 +0800307 authsize = abs(template[i].rlen - template[i].ilen);
Joy Latten93cc74e2007-12-12 20:24:22 +0800308 ret = crypto_aead_setauthsize(tfm, authsize);
309 if (ret) {
310 printk(KERN_INFO
311 "failed to set authsize = %u\n",
312 authsize);
Sebastian Siewior562954d2008-03-13 20:20:28 +0800313 goto next_one;
Joy Latten93cc74e2007-12-12 20:24:22 +0800314 }
315
Sebastian Siewior562954d2008-03-13 20:20:28 +0800316 sg_init_one(&sg[0], input,
317 template[i].ilen + (enc ? authsize : 0));
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800318
Sebastian Siewior562954d2008-03-13 20:20:28 +0800319 sg_init_one(&asg[0], assoc, template[i].alen);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800320
321 aead_request_set_crypt(req, sg, sg,
Sebastian Siewior562954d2008-03-13 20:20:28 +0800322 template[i].ilen, iv);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800323
Sebastian Siewior562954d2008-03-13 20:20:28 +0800324 aead_request_set_assoc(req, asg, template[i].alen);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800325
Herbert Xu6160b282007-12-04 19:17:50 +1100326 ret = enc ?
327 crypto_aead_encrypt(req) :
328 crypto_aead_decrypt(req);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800329
330 switch (ret) {
331 case 0:
332 break;
333 case -EINPROGRESS:
334 case -EBUSY:
335 ret = wait_for_completion_interruptible(
336 &result.completion);
337 if (!ret && !(ret = result.err)) {
338 INIT_COMPLETION(result.completion);
339 break;
340 }
341 /* fall through */
342 default:
343 printk(KERN_INFO "%s () failed err=%d\n",
344 e, -ret);
Sebastian Siewior562954d2008-03-13 20:20:28 +0800345 goto next_one;
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800346 }
347
348 q = kmap(sg_page(&sg[0])) + sg[0].offset;
Sebastian Siewior562954d2008-03-13 20:20:28 +0800349 hexdump(q, template[i].rlen);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800350
351 printk(KERN_INFO "enc/dec: %s\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +0800352 memcmp(q, template[i].result,
353 template[i].rlen) ? "fail" : "pass");
354 kunmap(sg_page(&sg[0]));
355next_one:
356 if (!template[i].key)
357 kfree(key);
358 kfree(assoc);
359 kfree(input);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800360 }
361 }
362
363 printk(KERN_INFO "\ntesting %s %s across pages (chunking)\n", algo, e);
364 memset(xbuf, 0, XBUFSIZE);
Herbert Xu2a999a32007-12-30 20:24:11 +1100365 memset(axbuf, 0, XBUFSIZE);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800366
367 for (i = 0, j = 0; i < tcount; i++) {
Sebastian Siewior562954d2008-03-13 20:20:28 +0800368 if (template[i].np) {
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800369 printk(KERN_INFO "test %u (%d bit key):\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +0800370 ++j, template[i].klen * 8);
371
372 if (template[i].iv)
373 memcpy(iv, template[i].iv, MAX_IVLEN);
374 else
375 memset(iv, 0, MAX_IVLEN);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800376
377 crypto_aead_clear_flags(tfm, ~0);
Sebastian Siewior562954d2008-03-13 20:20:28 +0800378 if (template[i].wk)
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800379 crypto_aead_set_flags(
380 tfm, CRYPTO_TFM_REQ_WEAK_KEY);
Sebastian Siewior562954d2008-03-13 20:20:28 +0800381 key = template[i].key;
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800382
Sebastian Siewior562954d2008-03-13 20:20:28 +0800383 ret = crypto_aead_setkey(tfm, key, template[i].klen);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800384 if (ret) {
385 printk(KERN_INFO "setkey() failed flags=%x\n",
386 crypto_aead_get_flags(tfm));
387
Sebastian Siewior562954d2008-03-13 20:20:28 +0800388 if (!template[i].fail)
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800389 goto out;
390 }
391
Sebastian Siewior562954d2008-03-13 20:20:28 +0800392 sg_init_table(sg, template[i].np);
393 for (k = 0, temp = 0; k < template[i].np; k++) {
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800394 memcpy(&xbuf[IDX[k]],
Sebastian Siewior562954d2008-03-13 20:20:28 +0800395 template[i].input + temp,
396 template[i].tap[k]);
397 temp += template[i].tap[k];
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800398 sg_set_buf(&sg[k], &xbuf[IDX[k]],
Sebastian Siewior562954d2008-03-13 20:20:28 +0800399 template[i].tap[k]);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800400 }
401
Sebastian Siewior562954d2008-03-13 20:20:28 +0800402 authsize = abs(template[i].rlen - template[i].ilen);
Joy Latten93cc74e2007-12-12 20:24:22 +0800403 ret = crypto_aead_setauthsize(tfm, authsize);
404 if (ret) {
405 printk(KERN_INFO
406 "failed to set authsize = %u\n",
407 authsize);
408 goto out;
409 }
410
Herbert Xu6160b282007-12-04 19:17:50 +1100411 if (enc)
412 sg[k - 1].length += authsize;
413
Sebastian Siewior562954d2008-03-13 20:20:28 +0800414 sg_init_table(asg, template[i].anp);
415 for (k = 0, temp = 0; k < template[i].anp; k++) {
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800416 memcpy(&axbuf[IDX[k]],
Sebastian Siewior562954d2008-03-13 20:20:28 +0800417 template[i].assoc + temp,
418 template[i].atap[k]);
419 temp += template[i].atap[k];
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800420 sg_set_buf(&asg[k], &axbuf[IDX[k]],
Sebastian Siewior562954d2008-03-13 20:20:28 +0800421 template[i].atap[k]);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800422 }
423
424 aead_request_set_crypt(req, sg, sg,
Sebastian Siewior562954d2008-03-13 20:20:28 +0800425 template[i].ilen,
426 iv);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800427
Sebastian Siewior562954d2008-03-13 20:20:28 +0800428 aead_request_set_assoc(req, asg, template[i].alen);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800429
Herbert Xu6160b282007-12-04 19:17:50 +1100430 ret = enc ?
431 crypto_aead_encrypt(req) :
432 crypto_aead_decrypt(req);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800433
434 switch (ret) {
435 case 0:
436 break;
437 case -EINPROGRESS:
438 case -EBUSY:
439 ret = wait_for_completion_interruptible(
440 &result.completion);
441 if (!ret && !(ret = result.err)) {
442 INIT_COMPLETION(result.completion);
443 break;
444 }
445 /* fall through */
446 default:
447 printk(KERN_INFO "%s () failed err=%d\n",
448 e, -ret);
449 goto out;
450 }
451
Sebastian Siewior562954d2008-03-13 20:20:28 +0800452 for (k = 0, temp = 0; k < template[i].np; k++) {
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800453 printk(KERN_INFO "page %u\n", k);
454 q = kmap(sg_page(&sg[k])) + sg[k].offset;
Sebastian Siewior562954d2008-03-13 20:20:28 +0800455 hexdump(q, template[i].tap[k]);
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800456 printk(KERN_INFO "%s\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +0800457 memcmp(q, template[i].result + temp,
458 template[i].tap[k] -
459 (k < template[i].np - 1 || enc ?
Herbert Xu6160b282007-12-04 19:17:50 +1100460 0 : authsize)) ?
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800461 "fail" : "pass");
462
Sebastian Siewior562954d2008-03-13 20:20:28 +0800463 temp += template[i].tap[k];
464 kunmap(sg_page(&sg[k]));
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800465 }
Mikko Herranene3a4ea42007-11-26 22:12:07 +0800466 }
467 }
468
469out:
470 crypto_free_aead(tfm);
471 aead_request_free(req);
472}
473
Herbert Xucba83562006-08-13 08:26:09 +1000474static void test_cipher(char *algo, int enc,
Herbert Xuef2736f2005-06-22 13:26:03 -0700475 struct cipher_testvec *template, unsigned int tcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 unsigned int ret, i, j, k, temp;
David Hardeman378f0582005-09-17 17:55:31 +1000478 char *q;
Herbert Xu6158efc2007-04-04 17:41:07 +1000479 struct crypto_ablkcipher *tfm;
Herbert Xu6158efc2007-04-04 17:41:07 +1000480 struct ablkcipher_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 struct scatterlist sg[8];
Herbert Xucba83562006-08-13 08:26:09 +1000482 const char *e;
Herbert Xu6158efc2007-04-04 17:41:07 +1000483 struct tcrypt_result result;
Sebastian Siewior562954d2008-03-13 20:20:28 +0800484 void *data;
485 char iv[MAX_IVLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 if (enc == ENCRYPT)
Herbert Xu3cc38162005-06-22 13:26:36 -0700488 e = "encryption";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 else
Herbert Xu3cc38162005-06-22 13:26:36 -0700490 e = "decryption";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Herbert Xucba83562006-08-13 08:26:09 +1000492 printk("\ntesting %s %s\n", algo, e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Herbert Xu6158efc2007-04-04 17:41:07 +1000494 init_completion(&result.completion);
Herbert Xu6158efc2007-04-04 17:41:07 +1000495 tfm = crypto_alloc_ablkcipher(algo, 0, 0);
Herbert Xuef2736f2005-06-22 13:26:03 -0700496
Herbert Xucba83562006-08-13 08:26:09 +1000497 if (IS_ERR(tfm)) {
498 printk("failed to load transform for %s: %ld\n", algo,
499 PTR_ERR(tfm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return;
501 }
Herbert Xu6158efc2007-04-04 17:41:07 +1000502
503 req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
504 if (!req) {
505 printk("failed to allocate request for %s\n", algo);
506 goto out;
507 }
508
509 ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
510 tcrypt_complete, &result);
Herbert Xuef2736f2005-06-22 13:26:03 -0700511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 j = 0;
513 for (i = 0; i < tcount; i++) {
Sebastian Siewior562954d2008-03-13 20:20:28 +0800514
515 data = kzalloc(template[i].ilen, GFP_KERNEL);
516 if (!data)
517 continue;
518
519 memcpy(data, template[i].input, template[i].ilen);
520 if (template[i].iv)
521 memcpy(iv, template[i].iv, MAX_IVLEN);
522 else
523 memset(iv, 0, MAX_IVLEN);
524
525 if (!(template[i].np)) {
Herbert Xuef2736f2005-06-22 13:26:03 -0700526 j++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 printk("test %u (%d bit key):\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +0800528 j, template[i].klen * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Herbert Xu6158efc2007-04-04 17:41:07 +1000530 crypto_ablkcipher_clear_flags(tfm, ~0);
Sebastian Siewior562954d2008-03-13 20:20:28 +0800531 if (template[i].wk)
Herbert Xu6158efc2007-04-04 17:41:07 +1000532 crypto_ablkcipher_set_flags(
Herbert Xucba83562006-08-13 08:26:09 +1000533 tfm, CRYPTO_TFM_REQ_WEAK_KEY);
Herbert Xuef2736f2005-06-22 13:26:03 -0700534
Sebastian Siewior562954d2008-03-13 20:20:28 +0800535 ret = crypto_ablkcipher_setkey(tfm, template[i].key,
536 template[i].klen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (ret) {
Herbert Xucba83562006-08-13 08:26:09 +1000538 printk("setkey() failed flags=%x\n",
Herbert Xu6158efc2007-04-04 17:41:07 +1000539 crypto_ablkcipher_get_flags(tfm));
Herbert Xuef2736f2005-06-22 13:26:03 -0700540
Sebastian Siewior562954d2008-03-13 20:20:28 +0800541 if (!template[i].fail) {
542 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 goto out;
Sebastian Siewior562954d2008-03-13 20:20:28 +0800544 }
Herbert Xuef2736f2005-06-22 13:26:03 -0700545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Sebastian Siewior562954d2008-03-13 20:20:28 +0800547 sg_init_one(&sg[0], data, template[i].ilen);
Herbert Xuef2736f2005-06-22 13:26:03 -0700548
Herbert Xu6158efc2007-04-04 17:41:07 +1000549 ablkcipher_request_set_crypt(req, sg, sg,
Sebastian Siewior562954d2008-03-13 20:20:28 +0800550 template[i].ilen, iv);
Herbert Xucba83562006-08-13 08:26:09 +1000551 ret = enc ?
Herbert Xu6158efc2007-04-04 17:41:07 +1000552 crypto_ablkcipher_encrypt(req) :
553 crypto_ablkcipher_decrypt(req);
Herbert Xuef2736f2005-06-22 13:26:03 -0700554
Herbert Xu6158efc2007-04-04 17:41:07 +1000555 switch (ret) {
556 case 0:
557 break;
558 case -EINPROGRESS:
559 case -EBUSY:
560 ret = wait_for_completion_interruptible(
561 &result.completion);
562 if (!ret && !((ret = result.err))) {
563 INIT_COMPLETION(result.completion);
564 break;
565 }
566 /* fall through */
567 default:
568 printk("%s () failed err=%d\n", e, -ret);
Sebastian Siewior562954d2008-03-13 20:20:28 +0800569 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 goto out;
Herbert Xuef2736f2005-06-22 13:26:03 -0700571 }
572
Jens Axboe78c2f0b2007-10-22 19:40:16 +0200573 q = kmap(sg_page(&sg[0])) + sg[0].offset;
Sebastian Siewior562954d2008-03-13 20:20:28 +0800574 hexdump(q, template[i].rlen);
Herbert Xuef2736f2005-06-22 13:26:03 -0700575
576 printk("%s\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +0800577 memcmp(q, template[i].result,
578 template[i].rlen) ? "fail" : "pass");
579 kunmap(sg_page(&sg[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Sebastian Siewior562954d2008-03-13 20:20:28 +0800581 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
Herbert Xuef2736f2005-06-22 13:26:03 -0700583
Herbert Xucba83562006-08-13 08:26:09 +1000584 printk("\ntesting %s %s across pages (chunking)\n", algo, e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 memset(xbuf, 0, XBUFSIZE);
Herbert Xuef2736f2005-06-22 13:26:03 -0700586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 j = 0;
588 for (i = 0; i < tcount; i++) {
Sebastian Siewior562954d2008-03-13 20:20:28 +0800589
590 data = kzalloc(template[i].ilen, GFP_KERNEL);
591 if (!data)
592 continue;
593
594 memcpy(data, template[i].input, template[i].ilen);
595
596 if (template[i].iv)
597 memcpy(iv, template[i].iv, MAX_IVLEN);
598 else
599 memset(iv, 0, MAX_IVLEN);
600
601 if (template[i].np) {
Herbert Xuef2736f2005-06-22 13:26:03 -0700602 j++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 printk("test %u (%d bit key):\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +0800604 j, template[i].klen * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Herbert Xu6158efc2007-04-04 17:41:07 +1000606 crypto_ablkcipher_clear_flags(tfm, ~0);
Sebastian Siewior562954d2008-03-13 20:20:28 +0800607 if (template[i].wk)
Herbert Xu6158efc2007-04-04 17:41:07 +1000608 crypto_ablkcipher_set_flags(
Herbert Xucba83562006-08-13 08:26:09 +1000609 tfm, CRYPTO_TFM_REQ_WEAK_KEY);
Herbert Xuef2736f2005-06-22 13:26:03 -0700610
Sebastian Siewior562954d2008-03-13 20:20:28 +0800611 ret = crypto_ablkcipher_setkey(tfm, template[i].key,
612 template[i].klen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (ret) {
Herbert Xucba83562006-08-13 08:26:09 +1000614 printk("setkey() failed flags=%x\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +0800615 crypto_ablkcipher_get_flags(tfm));
Herbert Xuef2736f2005-06-22 13:26:03 -0700616
Sebastian Siewior562954d2008-03-13 20:20:28 +0800617 if (!template[i].fail) {
618 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 goto out;
Sebastian Siewior562954d2008-03-13 20:20:28 +0800620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622
623 temp = 0;
Sebastian Siewior562954d2008-03-13 20:20:28 +0800624 sg_init_table(sg, template[i].np);
625 for (k = 0; k < template[i].np; k++) {
Herbert Xuef2736f2005-06-22 13:26:03 -0700626 memcpy(&xbuf[IDX[k]],
Sebastian Siewior562954d2008-03-13 20:20:28 +0800627 template[i].input + temp,
628 template[i].tap[k]);
629 temp += template[i].tap[k];
David Hardeman378f0582005-09-17 17:55:31 +1000630 sg_set_buf(&sg[k], &xbuf[IDX[k]],
Sebastian Siewior562954d2008-03-13 20:20:28 +0800631 template[i].tap[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
Herbert Xuef2736f2005-06-22 13:26:03 -0700633
Herbert Xu6158efc2007-04-04 17:41:07 +1000634 ablkcipher_request_set_crypt(req, sg, sg,
Sebastian Siewior562954d2008-03-13 20:20:28 +0800635 template[i].ilen, iv);
Herbert Xuef2736f2005-06-22 13:26:03 -0700636
Herbert Xucba83562006-08-13 08:26:09 +1000637 ret = enc ?
Herbert Xu6158efc2007-04-04 17:41:07 +1000638 crypto_ablkcipher_encrypt(req) :
639 crypto_ablkcipher_decrypt(req);
Herbert Xuef2736f2005-06-22 13:26:03 -0700640
Herbert Xu6158efc2007-04-04 17:41:07 +1000641 switch (ret) {
642 case 0:
643 break;
644 case -EINPROGRESS:
645 case -EBUSY:
646 ret = wait_for_completion_interruptible(
647 &result.completion);
648 if (!ret && !((ret = result.err))) {
649 INIT_COMPLETION(result.completion);
650 break;
651 }
652 /* fall through */
653 default:
654 printk("%s () failed err=%d\n", e, -ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 goto out;
656 }
657
658 temp = 0;
Sebastian Siewior562954d2008-03-13 20:20:28 +0800659 for (k = 0; k < template[i].np; k++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 printk("page %u\n", k);
Jens Axboe78c2f0b2007-10-22 19:40:16 +0200661 q = kmap(sg_page(&sg[k])) + sg[k].offset;
Sebastian Siewior562954d2008-03-13 20:20:28 +0800662 hexdump(q, template[i].tap[k]);
Herbert Xuef2736f2005-06-22 13:26:03 -0700663 printk("%s\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +0800664 memcmp(q, template[i].result + temp,
665 template[i].tap[k]) ? "fail" :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 "pass");
Sebastian Siewior562954d2008-03-13 20:20:28 +0800667 temp += template[i].tap[k];
668 kunmap(sg_page(&sg[k]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
670 }
671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672out:
Herbert Xu6158efc2007-04-04 17:41:07 +1000673 crypto_free_ablkcipher(tfm);
674 ablkcipher_request_free(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
Herbert Xucba83562006-08-13 08:26:09 +1000677static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc, char *p,
Herbert Xu6a179442005-06-22 13:29:03 -0700678 int blen, int sec)
679{
Herbert Xu6df5b9f2005-09-19 22:30:11 +1000680 struct scatterlist sg[1];
Herbert Xu6a179442005-06-22 13:29:03 -0700681 unsigned long start, end;
682 int bcount;
683 int ret;
684
David S. Millerb7335882007-10-26 00:38:10 -0700685 sg_init_one(sg, p, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700686
687 for (start = jiffies, end = start + sec * HZ, bcount = 0;
688 time_before(jiffies, end); bcount++) {
689 if (enc)
Herbert Xucba83562006-08-13 08:26:09 +1000690 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700691 else
Herbert Xucba83562006-08-13 08:26:09 +1000692 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700693
694 if (ret)
695 return ret;
696 }
697
698 printk("%d operations in %d seconds (%ld bytes)\n",
699 bcount, sec, (long)bcount * blen);
700 return 0;
701}
702
Herbert Xucba83562006-08-13 08:26:09 +1000703static int test_cipher_cycles(struct blkcipher_desc *desc, int enc, char *p,
Herbert Xu6a179442005-06-22 13:29:03 -0700704 int blen)
705{
Herbert Xu6df5b9f2005-09-19 22:30:11 +1000706 struct scatterlist sg[1];
Herbert Xu6a179442005-06-22 13:29:03 -0700707 unsigned long cycles = 0;
708 int ret = 0;
709 int i;
710
David S. Millerb7335882007-10-26 00:38:10 -0700711 sg_init_one(sg, p, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700712
713 local_bh_disable();
714 local_irq_disable();
715
716 /* Warm-up run. */
717 for (i = 0; i < 4; i++) {
718 if (enc)
Herbert Xucba83562006-08-13 08:26:09 +1000719 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700720 else
Herbert Xucba83562006-08-13 08:26:09 +1000721 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700722
723 if (ret)
724 goto out;
725 }
726
727 /* The real thing. */
728 for (i = 0; i < 8; i++) {
729 cycles_t start, end;
730
731 start = get_cycles();
732 if (enc)
Herbert Xucba83562006-08-13 08:26:09 +1000733 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700734 else
Herbert Xucba83562006-08-13 08:26:09 +1000735 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700736 end = get_cycles();
737
738 if (ret)
739 goto out;
740
741 cycles += end - start;
742 }
743
744out:
745 local_irq_enable();
746 local_bh_enable();
747
748 if (ret == 0)
749 printk("1 operation in %lu cycles (%d bytes)\n",
750 (cycles + 4) / 8, blen);
751
752 return ret;
753}
754
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800755static u32 block_sizes[] = { 16, 64, 256, 1024, 8192, 0 };
756
Herbert Xucba83562006-08-13 08:26:09 +1000757static void test_cipher_speed(char *algo, int enc, unsigned int sec,
Herbert Xudce907c2005-06-22 13:27:51 -0700758 struct cipher_testvec *template,
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800759 unsigned int tcount, u8 *keysize)
Harald Welteebfd9bc2005-06-22 13:27:23 -0700760{
Herbert Xudce907c2005-06-22 13:27:51 -0700761 unsigned int ret, i, j, iv_len;
Harald Welteebfd9bc2005-06-22 13:27:23 -0700762 unsigned char *key, *p, iv[128];
Herbert Xucba83562006-08-13 08:26:09 +1000763 struct crypto_blkcipher *tfm;
764 struct blkcipher_desc desc;
765 const char *e;
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800766 u32 *b_size;
Harald Welteebfd9bc2005-06-22 13:27:23 -0700767
768 if (enc == ENCRYPT)
769 e = "encryption";
770 else
771 e = "decryption";
Harald Welteebfd9bc2005-06-22 13:27:23 -0700772
Herbert Xucba83562006-08-13 08:26:09 +1000773 printk("\ntesting speed of %s %s\n", algo, e);
Harald Welteebfd9bc2005-06-22 13:27:23 -0700774
Herbert Xucba83562006-08-13 08:26:09 +1000775 tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
Harald Welteebfd9bc2005-06-22 13:27:23 -0700776
Herbert Xucba83562006-08-13 08:26:09 +1000777 if (IS_ERR(tfm)) {
778 printk("failed to load transform for %s: %ld\n", algo,
779 PTR_ERR(tfm));
Harald Welteebfd9bc2005-06-22 13:27:23 -0700780 return;
781 }
Herbert Xucba83562006-08-13 08:26:09 +1000782 desc.tfm = tfm;
783 desc.flags = 0;
Harald Welteebfd9bc2005-06-22 13:27:23 -0700784
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800785 i = 0;
786 do {
Harald Welteebfd9bc2005-06-22 13:27:23 -0700787
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800788 b_size = block_sizes;
789 do {
Harald Welteebfd9bc2005-06-22 13:27:23 -0700790
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800791 if ((*keysize + *b_size) > TVMEMSIZE) {
792 printk("template (%u) too big for tvmem (%u)\n",
793 *keysize + *b_size, TVMEMSIZE);
794 goto out;
795 }
Harald Welteebfd9bc2005-06-22 13:27:23 -0700796
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800797 printk("test %u (%d bit key, %d byte blocks): ", i,
798 *keysize * 8, *b_size);
799
800 memset(tvmem, 0xff, *keysize + *b_size);
801
802 /* set key, plain text and IV */
803 key = (unsigned char *)tvmem;
804 for (j = 0; j < tcount; j++) {
805 if (template[j].klen == *keysize) {
806 key = template[j].key;
807 break;
808 }
809 }
810 p = (unsigned char *)tvmem + *keysize;
811
812 ret = crypto_blkcipher_setkey(tfm, key, *keysize);
813 if (ret) {
814 printk("setkey() failed flags=%x\n",
815 crypto_blkcipher_get_flags(tfm));
816 goto out;
817 }
818
819 iv_len = crypto_blkcipher_ivsize(tfm);
820 if (iv_len) {
821 memset(&iv, 0xff, iv_len);
822 crypto_blkcipher_set_iv(tfm, iv, iv_len);
823 }
824
825 if (sec)
826 ret = test_cipher_jiffies(&desc, enc, p, *b_size, sec);
827 else
828 ret = test_cipher_cycles(&desc, enc, p, *b_size);
829
830 if (ret) {
831 printk("%s() failed flags=%x\n", e, desc.flags);
Herbert Xudce907c2005-06-22 13:27:51 -0700832 break;
833 }
Sebastian Siewiord5dc3922008-03-11 21:27:11 +0800834 b_size++;
835 i++;
836 } while (*b_size);
837 keysize++;
838 } while (*keysize);
Harald Welteebfd9bc2005-06-22 13:27:23 -0700839
840out:
Herbert Xucba83562006-08-13 08:26:09 +1000841 crypto_free_blkcipher(tfm);
Harald Welteebfd9bc2005-06-22 13:27:23 -0700842}
843
Herbert Xue9d41162006-08-19 21:38:49 +1000844static int test_hash_jiffies_digest(struct hash_desc *desc, char *p, int blen,
845 char *out, int sec)
Michal Ludvige8057922006-05-30 22:04:19 +1000846{
847 struct scatterlist sg[1];
848 unsigned long start, end;
Herbert Xue9d41162006-08-19 21:38:49 +1000849 int bcount;
850 int ret;
Michal Ludvige8057922006-05-30 22:04:19 +1000851
Herbert Xua5a613a2007-10-27 00:51:21 -0700852 sg_init_table(sg, 1);
853
Michal Ludvige8057922006-05-30 22:04:19 +1000854 for (start = jiffies, end = start + sec * HZ, bcount = 0;
855 time_before(jiffies, end); bcount++) {
Herbert Xua5a613a2007-10-27 00:51:21 -0700856 sg_set_buf(sg, p, blen);
Herbert Xue9d41162006-08-19 21:38:49 +1000857 ret = crypto_hash_digest(desc, sg, blen, out);
858 if (ret)
859 return ret;
Michal Ludvige8057922006-05-30 22:04:19 +1000860 }
861
862 printk("%6u opers/sec, %9lu bytes/sec\n",
863 bcount / sec, ((long)bcount * blen) / sec);
864
Herbert Xue9d41162006-08-19 21:38:49 +1000865 return 0;
Michal Ludvige8057922006-05-30 22:04:19 +1000866}
867
Herbert Xue9d41162006-08-19 21:38:49 +1000868static int test_hash_jiffies(struct hash_desc *desc, char *p, int blen,
869 int plen, char *out, int sec)
870{
871 struct scatterlist sg[1];
872 unsigned long start, end;
873 int bcount, pcount;
874 int ret;
875
876 if (plen == blen)
877 return test_hash_jiffies_digest(desc, p, blen, out, sec);
878
Herbert Xua5a613a2007-10-27 00:51:21 -0700879 sg_init_table(sg, 1);
880
Herbert Xue9d41162006-08-19 21:38:49 +1000881 for (start = jiffies, end = start + sec * HZ, bcount = 0;
882 time_before(jiffies, end); bcount++) {
883 ret = crypto_hash_init(desc);
884 if (ret)
885 return ret;
886 for (pcount = 0; pcount < blen; pcount += plen) {
Herbert Xua5a613a2007-10-27 00:51:21 -0700887 sg_set_buf(sg, p + pcount, plen);
Herbert Xue9d41162006-08-19 21:38:49 +1000888 ret = crypto_hash_update(desc, sg, plen);
889 if (ret)
890 return ret;
891 }
892 /* we assume there is enough space in 'out' for the result */
893 ret = crypto_hash_final(desc, out);
894 if (ret)
895 return ret;
896 }
897
898 printk("%6u opers/sec, %9lu bytes/sec\n",
899 bcount / sec, ((long)bcount * blen) / sec);
900
901 return 0;
902}
903
904static int test_hash_cycles_digest(struct hash_desc *desc, char *p, int blen,
905 char *out)
Michal Ludvige8057922006-05-30 22:04:19 +1000906{
907 struct scatterlist sg[1];
908 unsigned long cycles = 0;
Herbert Xue9d41162006-08-19 21:38:49 +1000909 int i;
910 int ret;
Michal Ludvige8057922006-05-30 22:04:19 +1000911
Herbert Xua5a613a2007-10-27 00:51:21 -0700912 sg_init_table(sg, 1);
913
Michal Ludvige8057922006-05-30 22:04:19 +1000914 local_bh_disable();
915 local_irq_disable();
916
917 /* Warm-up run. */
918 for (i = 0; i < 4; i++) {
Herbert Xua5a613a2007-10-27 00:51:21 -0700919 sg_set_buf(sg, p, blen);
Herbert Xue9d41162006-08-19 21:38:49 +1000920 ret = crypto_hash_digest(desc, sg, blen, out);
921 if (ret)
922 goto out;
Michal Ludvige8057922006-05-30 22:04:19 +1000923 }
924
925 /* The real thing. */
926 for (i = 0; i < 8; i++) {
927 cycles_t start, end;
928
Michal Ludvige8057922006-05-30 22:04:19 +1000929 start = get_cycles();
930
Herbert Xua5a613a2007-10-27 00:51:21 -0700931 sg_set_buf(sg, p, blen);
Herbert Xue9d41162006-08-19 21:38:49 +1000932 ret = crypto_hash_digest(desc, sg, blen, out);
933 if (ret)
934 goto out;
Michal Ludvige8057922006-05-30 22:04:19 +1000935
936 end = get_cycles();
937
938 cycles += end - start;
939 }
940
Herbert Xue9d41162006-08-19 21:38:49 +1000941out:
Michal Ludvige8057922006-05-30 22:04:19 +1000942 local_irq_enable();
943 local_bh_enable();
944
Herbert Xue9d41162006-08-19 21:38:49 +1000945 if (ret)
946 return ret;
947
Michal Ludvige8057922006-05-30 22:04:19 +1000948 printk("%6lu cycles/operation, %4lu cycles/byte\n",
949 cycles / 8, cycles / (8 * blen));
950
Herbert Xue9d41162006-08-19 21:38:49 +1000951 return 0;
Michal Ludvige8057922006-05-30 22:04:19 +1000952}
953
Herbert Xue9d41162006-08-19 21:38:49 +1000954static int test_hash_cycles(struct hash_desc *desc, char *p, int blen,
955 int plen, char *out)
Michal Ludvige8057922006-05-30 22:04:19 +1000956{
Herbert Xue9d41162006-08-19 21:38:49 +1000957 struct scatterlist sg[1];
958 unsigned long cycles = 0;
959 int i, pcount;
960 int ret;
961
962 if (plen == blen)
963 return test_hash_cycles_digest(desc, p, blen, out);
964
Herbert Xua5a613a2007-10-27 00:51:21 -0700965 sg_init_table(sg, 1);
966
Herbert Xue9d41162006-08-19 21:38:49 +1000967 local_bh_disable();
968 local_irq_disable();
969
970 /* Warm-up run. */
971 for (i = 0; i < 4; i++) {
972 ret = crypto_hash_init(desc);
973 if (ret)
974 goto out;
975 for (pcount = 0; pcount < blen; pcount += plen) {
Herbert Xua5a613a2007-10-27 00:51:21 -0700976 sg_set_buf(sg, p + pcount, plen);
Herbert Xue9d41162006-08-19 21:38:49 +1000977 ret = crypto_hash_update(desc, sg, plen);
978 if (ret)
979 goto out;
980 }
Herbert Xu29059d12007-05-18 16:25:19 +1000981 ret = crypto_hash_final(desc, out);
Herbert Xue9d41162006-08-19 21:38:49 +1000982 if (ret)
983 goto out;
984 }
985
986 /* The real thing. */
987 for (i = 0; i < 8; i++) {
988 cycles_t start, end;
989
990 start = get_cycles();
991
992 ret = crypto_hash_init(desc);
993 if (ret)
994 goto out;
995 for (pcount = 0; pcount < blen; pcount += plen) {
Herbert Xua5a613a2007-10-27 00:51:21 -0700996 sg_set_buf(sg, p + pcount, plen);
Herbert Xue9d41162006-08-19 21:38:49 +1000997 ret = crypto_hash_update(desc, sg, plen);
998 if (ret)
999 goto out;
1000 }
1001 ret = crypto_hash_final(desc, out);
1002 if (ret)
1003 goto out;
1004
1005 end = get_cycles();
1006
1007 cycles += end - start;
1008 }
1009
1010out:
1011 local_irq_enable();
1012 local_bh_enable();
1013
1014 if (ret)
1015 return ret;
1016
1017 printk("%6lu cycles/operation, %4lu cycles/byte\n",
1018 cycles / 8, cycles / (8 * blen));
1019
1020 return 0;
1021}
1022
1023static void test_hash_speed(char *algo, unsigned int sec,
1024 struct hash_speed *speed)
1025{
1026 struct crypto_hash *tfm;
1027 struct hash_desc desc;
Michal Ludvige8057922006-05-30 22:04:19 +10001028 char output[1024];
1029 int i;
Herbert Xue9d41162006-08-19 21:38:49 +10001030 int ret;
Michal Ludvige8057922006-05-30 22:04:19 +10001031
1032 printk("\ntesting speed of %s\n", algo);
1033
Herbert Xue9d41162006-08-19 21:38:49 +10001034 tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
Michal Ludvige8057922006-05-30 22:04:19 +10001035
Herbert Xue9d41162006-08-19 21:38:49 +10001036 if (IS_ERR(tfm)) {
1037 printk("failed to load transform for %s: %ld\n", algo,
1038 PTR_ERR(tfm));
Michal Ludvige8057922006-05-30 22:04:19 +10001039 return;
1040 }
1041
Herbert Xue9d41162006-08-19 21:38:49 +10001042 desc.tfm = tfm;
1043 desc.flags = 0;
1044
1045 if (crypto_hash_digestsize(tfm) > sizeof(output)) {
Michal Ludvige8057922006-05-30 22:04:19 +10001046 printk("digestsize(%u) > outputbuffer(%zu)\n",
Herbert Xue9d41162006-08-19 21:38:49 +10001047 crypto_hash_digestsize(tfm), sizeof(output));
Michal Ludvige8057922006-05-30 22:04:19 +10001048 goto out;
1049 }
1050
1051 for (i = 0; speed[i].blen != 0; i++) {
1052 if (speed[i].blen > TVMEMSIZE) {
1053 printk("template (%u) too big for tvmem (%u)\n",
1054 speed[i].blen, TVMEMSIZE);
1055 goto out;
1056 }
1057
1058 printk("test%3u (%5u byte blocks,%5u bytes per update,%4u updates): ",
1059 i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
1060
1061 memset(tvmem, 0xff, speed[i].blen);
1062
1063 if (sec)
Herbert Xue9d41162006-08-19 21:38:49 +10001064 ret = test_hash_jiffies(&desc, tvmem, speed[i].blen,
1065 speed[i].plen, output, sec);
Michal Ludvige8057922006-05-30 22:04:19 +10001066 else
Herbert Xue9d41162006-08-19 21:38:49 +10001067 ret = test_hash_cycles(&desc, tvmem, speed[i].blen,
1068 speed[i].plen, output);
1069
1070 if (ret) {
1071 printk("hashing failed ret=%d\n", ret);
1072 break;
1073 }
Michal Ludvige8057922006-05-30 22:04:19 +10001074 }
1075
1076out:
Herbert Xue9d41162006-08-19 21:38:49 +10001077 crypto_free_hash(tfm);
Michal Ludvige8057922006-05-30 22:04:19 +10001078}
1079
Zoltan Sogor91755a92007-12-07 16:48:11 +08001080static void test_comp(char *algo, struct comp_testvec *ctemplate,
1081 struct comp_testvec *dtemplate, int ctcount, int dtcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 unsigned int i;
1084 char result[COMP_BUF_SIZE];
Herbert Xue4d5b792006-08-26 18:12:40 +10001085 struct crypto_comp *tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 unsigned int tsize;
1087
Zoltan Sogor91755a92007-12-07 16:48:11 +08001088 printk("\ntesting %s compression\n", algo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Zoltan Sogor91755a92007-12-07 16:48:11 +08001090 tfm = crypto_alloc_comp(algo, 0, CRYPTO_ALG_ASYNC);
Sebastian Siewior7bc301e2007-03-21 08:58:43 +11001091 if (IS_ERR(tfm)) {
Zoltan Sogor91755a92007-12-07 16:48:11 +08001092 printk("failed to load transform for %s\n", algo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return;
1094 }
1095
Zoltan Sogor91755a92007-12-07 16:48:11 +08001096 for (i = 0; i < ctcount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 int ilen, ret, dlen = COMP_BUF_SIZE;
Herbert Xuef2736f2005-06-22 13:26:03 -07001098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 printk("test %u:\n", i + 1);
1100 memset(result, 0, sizeof (result));
1101
Sebastian Siewior562954d2008-03-13 20:20:28 +08001102 ilen = ctemplate[i].inlen;
1103 ret = crypto_comp_compress(tfm, ctemplate[i].input,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 ilen, result, &dlen);
1105 if (ret) {
1106 printk("fail: ret=%d\n", ret);
1107 continue;
1108 }
1109 hexdump(result, dlen);
1110 printk("%s (ratio %d:%d)\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +08001111 memcmp(result, ctemplate[i].output, dlen) ? "fail" : "pass",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 ilen, dlen);
1113 }
1114
Zoltan Sogor91755a92007-12-07 16:48:11 +08001115 printk("\ntesting %s decompression\n", algo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Zoltan Sogor91755a92007-12-07 16:48:11 +08001117 tsize = sizeof(struct comp_testvec);
1118 tsize *= dtcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (tsize > TVMEMSIZE) {
1120 printk("template (%u) too big for tvmem (%u)\n", tsize,
1121 TVMEMSIZE);
1122 goto out;
1123 }
1124
Zoltan Sogor91755a92007-12-07 16:48:11 +08001125 for (i = 0; i < dtcount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 int ilen, ret, dlen = COMP_BUF_SIZE;
Herbert Xuef2736f2005-06-22 13:26:03 -07001127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 printk("test %u:\n", i + 1);
1129 memset(result, 0, sizeof (result));
1130
Sebastian Siewior562954d2008-03-13 20:20:28 +08001131 ilen = dtemplate[i].inlen;
1132 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 ilen, result, &dlen);
1134 if (ret) {
1135 printk("fail: ret=%d\n", ret);
1136 continue;
1137 }
1138 hexdump(result, dlen);
1139 printk("%s (ratio %d:%d)\n",
Sebastian Siewior562954d2008-03-13 20:20:28 +08001140 memcmp(result, dtemplate[i].output, dlen) ? "fail" : "pass",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 ilen, dlen);
1142 }
1143out:
Herbert Xue4d5b792006-08-26 18:12:40 +10001144 crypto_free_comp(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146
Herbert Xuef2736f2005-06-22 13:26:03 -07001147static void test_available(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
1149 char **name = check;
Herbert Xuef2736f2005-06-22 13:26:03 -07001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 while (*name) {
1152 printk("alg %s ", *name);
Herbert Xu6158efc2007-04-04 17:41:07 +10001153 printk(crypto_has_alg(*name, 0, 0) ?
Herbert Xue4d5b792006-08-26 18:12:40 +10001154 "found\n" : "not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 name++;
Herbert Xuef2736f2005-06-22 13:26:03 -07001156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
Herbert Xuef2736f2005-06-22 13:26:03 -07001159static void do_test(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 switch (mode) {
1162
1163 case 0:
1164 test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -07001165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -07001167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 //DES
Herbert Xucba83562006-08-13 08:26:09 +10001169 test_cipher("ecb(des)", ENCRYPT, des_enc_tv_template,
1170 DES_ENC_TEST_VECTORS);
1171 test_cipher("ecb(des)", DECRYPT, des_dec_tv_template,
1172 DES_DEC_TEST_VECTORS);
1173 test_cipher("cbc(des)", ENCRYPT, des_cbc_enc_tv_template,
1174 DES_CBC_ENC_TEST_VECTORS);
1175 test_cipher("cbc(des)", DECRYPT, des_cbc_dec_tv_template,
1176 DES_CBC_DEC_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -07001177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 //DES3_EDE
Herbert Xucba83562006-08-13 08:26:09 +10001179 test_cipher("ecb(des3_ede)", ENCRYPT, des3_ede_enc_tv_template,
1180 DES3_EDE_ENC_TEST_VECTORS);
1181 test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
1182 DES3_EDE_DEC_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -07001183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -07001185
Jonathan Lynchcd12fb92007-11-10 20:08:25 +08001186 test_hash("sha224", sha224_tv_template, SHA224_TEST_VECTORS);
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -07001189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 //BLOWFISH
Herbert Xucba83562006-08-13 08:26:09 +10001191 test_cipher("ecb(blowfish)", ENCRYPT, bf_enc_tv_template,
1192 BF_ENC_TEST_VECTORS);
1193 test_cipher("ecb(blowfish)", DECRYPT, bf_dec_tv_template,
1194 BF_DEC_TEST_VECTORS);
1195 test_cipher("cbc(blowfish)", ENCRYPT, bf_cbc_enc_tv_template,
1196 BF_CBC_ENC_TEST_VECTORS);
1197 test_cipher("cbc(blowfish)", DECRYPT, bf_cbc_dec_tv_template,
1198 BF_CBC_DEC_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -07001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 //TWOFISH
Herbert Xucba83562006-08-13 08:26:09 +10001201 test_cipher("ecb(twofish)", ENCRYPT, tf_enc_tv_template,
1202 TF_ENC_TEST_VECTORS);
1203 test_cipher("ecb(twofish)", DECRYPT, tf_dec_tv_template,
1204 TF_DEC_TEST_VECTORS);
1205 test_cipher("cbc(twofish)", ENCRYPT, tf_cbc_enc_tv_template,
1206 TF_CBC_ENC_TEST_VECTORS);
1207 test_cipher("cbc(twofish)", DECRYPT, tf_cbc_dec_tv_template,
1208 TF_CBC_DEC_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -07001209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 //SERPENT
Herbert Xucba83562006-08-13 08:26:09 +10001211 test_cipher("ecb(serpent)", ENCRYPT, serpent_enc_tv_template,
1212 SERPENT_ENC_TEST_VECTORS);
1213 test_cipher("ecb(serpent)", DECRYPT, serpent_dec_tv_template,
1214 SERPENT_DEC_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -07001215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 //TNEPRES
Herbert Xucba83562006-08-13 08:26:09 +10001217 test_cipher("ecb(tnepres)", ENCRYPT, tnepres_enc_tv_template,
1218 TNEPRES_ENC_TEST_VECTORS);
1219 test_cipher("ecb(tnepres)", DECRYPT, tnepres_dec_tv_template,
1220 TNEPRES_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 //AES
Herbert Xucba83562006-08-13 08:26:09 +10001223 test_cipher("ecb(aes)", ENCRYPT, aes_enc_tv_template,
1224 AES_ENC_TEST_VECTORS);
1225 test_cipher("ecb(aes)", DECRYPT, aes_dec_tv_template,
1226 AES_DEC_TEST_VECTORS);
1227 test_cipher("cbc(aes)", ENCRYPT, aes_cbc_enc_tv_template,
1228 AES_CBC_ENC_TEST_VECTORS);
1229 test_cipher("cbc(aes)", DECRYPT, aes_cbc_dec_tv_template,
1230 AES_CBC_DEC_TEST_VECTORS);
Rik Snelf3d10442006-11-29 19:01:41 +11001231 test_cipher("lrw(aes)", ENCRYPT, aes_lrw_enc_tv_template,
1232 AES_LRW_ENC_TEST_VECTORS);
1233 test_cipher("lrw(aes)", DECRYPT, aes_lrw_dec_tv_template,
1234 AES_LRW_DEC_TEST_VECTORS);
Rik Snelf19f5112007-09-19 20:23:13 +08001235 test_cipher("xts(aes)", ENCRYPT, aes_xts_enc_tv_template,
1236 AES_XTS_ENC_TEST_VECTORS);
1237 test_cipher("xts(aes)", DECRYPT, aes_xts_dec_tv_template,
1238 AES_XTS_DEC_TEST_VECTORS);
Herbert Xu5311f242007-12-17 21:34:32 +08001239 test_cipher("rfc3686(ctr(aes))", ENCRYPT, aes_ctr_enc_tv_template,
Joy Latten23e353c2007-10-23 08:50:32 +08001240 AES_CTR_ENC_TEST_VECTORS);
Herbert Xu5311f242007-12-17 21:34:32 +08001241 test_cipher("rfc3686(ctr(aes))", DECRYPT, aes_ctr_dec_tv_template,
Joy Latten23e353c2007-10-23 08:50:32 +08001242 AES_CTR_DEC_TEST_VECTORS);
Mikko Herranen28db8e32007-11-26 22:24:11 +08001243 test_aead("gcm(aes)", ENCRYPT, aes_gcm_enc_tv_template,
1244 AES_GCM_ENC_TEST_VECTORS);
1245 test_aead("gcm(aes)", DECRYPT, aes_gcm_dec_tv_template,
1246 AES_GCM_DEC_TEST_VECTORS);
Joy Latten93cc74e2007-12-12 20:24:22 +08001247 test_aead("ccm(aes)", ENCRYPT, aes_ccm_enc_tv_template,
1248 AES_CCM_ENC_TEST_VECTORS);
1249 test_aead("ccm(aes)", DECRYPT, aes_ccm_dec_tv_template,
1250 AES_CCM_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 //CAST5
Herbert Xucba83562006-08-13 08:26:09 +10001253 test_cipher("ecb(cast5)", ENCRYPT, cast5_enc_tv_template,
1254 CAST5_ENC_TEST_VECTORS);
1255 test_cipher("ecb(cast5)", DECRYPT, cast5_dec_tv_template,
1256 CAST5_DEC_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -07001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 //CAST6
Herbert Xucba83562006-08-13 08:26:09 +10001259 test_cipher("ecb(cast6)", ENCRYPT, cast6_enc_tv_template,
1260 CAST6_ENC_TEST_VECTORS);
1261 test_cipher("ecb(cast6)", DECRYPT, cast6_dec_tv_template,
1262 CAST6_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 //ARC4
Herbert Xucba83562006-08-13 08:26:09 +10001265 test_cipher("ecb(arc4)", ENCRYPT, arc4_enc_tv_template,
1266 ARC4_ENC_TEST_VECTORS);
1267 test_cipher("ecb(arc4)", DECRYPT, arc4_dec_tv_template,
1268 ARC4_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 //TEA
Herbert Xucba83562006-08-13 08:26:09 +10001271 test_cipher("ecb(tea)", ENCRYPT, tea_enc_tv_template,
1272 TEA_ENC_TEST_VECTORS);
1273 test_cipher("ecb(tea)", DECRYPT, tea_dec_tv_template,
1274 TEA_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276
1277 //XTEA
Herbert Xucba83562006-08-13 08:26:09 +10001278 test_cipher("ecb(xtea)", ENCRYPT, xtea_enc_tv_template,
1279 XTEA_ENC_TEST_VECTORS);
1280 test_cipher("ecb(xtea)", DECRYPT, xtea_dec_tv_template,
1281 XTEA_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 //KHAZAD
Herbert Xucba83562006-08-13 08:26:09 +10001284 test_cipher("ecb(khazad)", ENCRYPT, khazad_enc_tv_template,
1285 KHAZAD_ENC_TEST_VECTORS);
1286 test_cipher("ecb(khazad)", DECRYPT, khazad_dec_tv_template,
1287 KHAZAD_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 //ANUBIS
Herbert Xucba83562006-08-13 08:26:09 +10001290 test_cipher("ecb(anubis)", ENCRYPT, anubis_enc_tv_template,
1291 ANUBIS_ENC_TEST_VECTORS);
1292 test_cipher("ecb(anubis)", DECRYPT, anubis_dec_tv_template,
1293 ANUBIS_DEC_TEST_VECTORS);
1294 test_cipher("cbc(anubis)", ENCRYPT, anubis_cbc_enc_tv_template,
1295 ANUBIS_CBC_ENC_TEST_VECTORS);
1296 test_cipher("cbc(anubis)", DECRYPT, anubis_cbc_dec_tv_template,
1297 ANUBIS_CBC_ENC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Aaron Grothefb4f10e2005-09-01 17:42:46 -07001299 //XETA
Herbert Xucba83562006-08-13 08:26:09 +10001300 test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
1301 XETA_ENC_TEST_VECTORS);
1302 test_cipher("ecb(xeta)", DECRYPT, xeta_dec_tv_template,
1303 XETA_DEC_TEST_VECTORS);
Aaron Grothefb4f10e2005-09-01 17:42:46 -07001304
David Howells90831632006-12-16 12:13:14 +11001305 //FCrypt
1306 test_cipher("pcbc(fcrypt)", ENCRYPT, fcrypt_pcbc_enc_tv_template,
1307 FCRYPT_ENC_TEST_VECTORS);
1308 test_cipher("pcbc(fcrypt)", DECRYPT, fcrypt_pcbc_dec_tv_template,
1309 FCRYPT_DEC_TEST_VECTORS);
1310
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +11001311 //CAMELLIA
1312 test_cipher("ecb(camellia)", ENCRYPT,
1313 camellia_enc_tv_template,
1314 CAMELLIA_ENC_TEST_VECTORS);
1315 test_cipher("ecb(camellia)", DECRYPT,
1316 camellia_dec_tv_template,
1317 CAMELLIA_DEC_TEST_VECTORS);
1318 test_cipher("cbc(camellia)", ENCRYPT,
1319 camellia_cbc_enc_tv_template,
1320 CAMELLIA_CBC_ENC_TEST_VECTORS);
1321 test_cipher("cbc(camellia)", DECRYPT,
1322 camellia_cbc_dec_tv_template,
1323 CAMELLIA_CBC_DEC_TEST_VECTORS);
1324
Hye-Shik Change2ee95b2007-08-21 20:01:03 +08001325 //SEED
1326 test_cipher("ecb(seed)", ENCRYPT, seed_enc_tv_template,
1327 SEED_ENC_TEST_VECTORS);
1328 test_cipher("ecb(seed)", DECRYPT, seed_dec_tv_template,
1329 SEED_DEC_TEST_VECTORS);
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
1332 test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
1333 test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
1334 test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
1335 test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
1336 test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
1337 test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
1338 test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
Zoltan Sogor91755a92007-12-07 16:48:11 +08001339 test_comp("deflate", deflate_comp_tv_template,
1340 deflate_decomp_tv_template, DEFLATE_COMP_TEST_VECTORS,
1341 DEFLATE_DECOMP_TEST_VECTORS);
Zoltan Sogor0b77abb2007-12-07 16:53:23 +08001342 test_comp("lzo", lzo_comp_tv_template, lzo_decomp_tv_template,
1343 LZO_COMP_TEST_VECTORS, LZO_DECOMP_TEST_VECTORS);
Herbert Xuc907ee72006-08-21 22:04:03 +10001344 test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
Herbert Xue9d41162006-08-19 21:38:49 +10001345 test_hash("hmac(md5)", hmac_md5_tv_template,
1346 HMAC_MD5_TEST_VECTORS);
1347 test_hash("hmac(sha1)", hmac_sha1_tv_template,
1348 HMAC_SHA1_TEST_VECTORS);
Jonathan Lynchcd12fb92007-11-10 20:08:25 +08001349 test_hash("hmac(sha224)", hmac_sha224_tv_template,
1350 HMAC_SHA224_TEST_VECTORS);
Herbert Xue9d41162006-08-19 21:38:49 +10001351 test_hash("hmac(sha256)", hmac_sha256_tv_template,
1352 HMAC_SHA256_TEST_VECTORS);
Andrew Donofrioa28091a2006-12-10 12:10:20 +11001353 test_hash("hmac(sha384)", hmac_sha384_tv_template,
1354 HMAC_SHA384_TEST_VECTORS);
1355 test_hash("hmac(sha512)", hmac_sha512_tv_template,
1356 HMAC_SHA512_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Kazunori MIYAZAWA5b2becf2006-10-28 13:18:53 +10001358 test_hash("xcbc(aes)", aes_xcbc128_tv_template,
1359 XCBC_AES_TEST_VECTORS);
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
1362 break;
1363
1364 case 1:
1365 test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
1366 break;
1367
1368 case 2:
1369 test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
1370 break;
1371
1372 case 3:
Herbert Xucba83562006-08-13 08:26:09 +10001373 test_cipher("ecb(des)", ENCRYPT, des_enc_tv_template,
1374 DES_ENC_TEST_VECTORS);
1375 test_cipher("ecb(des)", DECRYPT, des_dec_tv_template,
1376 DES_DEC_TEST_VECTORS);
1377 test_cipher("cbc(des)", ENCRYPT, des_cbc_enc_tv_template,
1378 DES_CBC_ENC_TEST_VECTORS);
1379 test_cipher("cbc(des)", DECRYPT, des_cbc_dec_tv_template,
1380 DES_CBC_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 break;
1382
1383 case 4:
Herbert Xucba83562006-08-13 08:26:09 +10001384 test_cipher("ecb(des3_ede)", ENCRYPT, des3_ede_enc_tv_template,
1385 DES3_EDE_ENC_TEST_VECTORS);
1386 test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
1387 DES3_EDE_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 break;
1389
1390 case 5:
1391 test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
1392 break;
Herbert Xuef2736f2005-06-22 13:26:03 -07001393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 case 6:
1395 test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
1396 break;
Herbert Xuef2736f2005-06-22 13:26:03 -07001397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 case 7:
Herbert Xucba83562006-08-13 08:26:09 +10001399 test_cipher("ecb(blowfish)", ENCRYPT, bf_enc_tv_template,
1400 BF_ENC_TEST_VECTORS);
1401 test_cipher("ecb(blowfish)", DECRYPT, bf_dec_tv_template,
1402 BF_DEC_TEST_VECTORS);
1403 test_cipher("cbc(blowfish)", ENCRYPT, bf_cbc_enc_tv_template,
1404 BF_CBC_ENC_TEST_VECTORS);
1405 test_cipher("cbc(blowfish)", DECRYPT, bf_cbc_dec_tv_template,
1406 BF_CBC_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 break;
1408
1409 case 8:
Herbert Xucba83562006-08-13 08:26:09 +10001410 test_cipher("ecb(twofish)", ENCRYPT, tf_enc_tv_template,
1411 TF_ENC_TEST_VECTORS);
1412 test_cipher("ecb(twofish)", DECRYPT, tf_dec_tv_template,
1413 TF_DEC_TEST_VECTORS);
1414 test_cipher("cbc(twofish)", ENCRYPT, tf_cbc_enc_tv_template,
1415 TF_CBC_ENC_TEST_VECTORS);
1416 test_cipher("cbc(twofish)", DECRYPT, tf_cbc_dec_tv_template,
1417 TF_CBC_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 break;
Herbert Xuef2736f2005-06-22 13:26:03 -07001419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 case 9:
Herbert Xucba83562006-08-13 08:26:09 +10001421 test_cipher("ecb(serpent)", ENCRYPT, serpent_enc_tv_template,
1422 SERPENT_ENC_TEST_VECTORS);
1423 test_cipher("ecb(serpent)", DECRYPT, serpent_dec_tv_template,
1424 SERPENT_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 break;
1426
1427 case 10:
Herbert Xucba83562006-08-13 08:26:09 +10001428 test_cipher("ecb(aes)", ENCRYPT, aes_enc_tv_template,
1429 AES_ENC_TEST_VECTORS);
1430 test_cipher("ecb(aes)", DECRYPT, aes_dec_tv_template,
1431 AES_DEC_TEST_VECTORS);
1432 test_cipher("cbc(aes)", ENCRYPT, aes_cbc_enc_tv_template,
1433 AES_CBC_ENC_TEST_VECTORS);
1434 test_cipher("cbc(aes)", DECRYPT, aes_cbc_dec_tv_template,
1435 AES_CBC_DEC_TEST_VECTORS);
Rik Snelf3d10442006-11-29 19:01:41 +11001436 test_cipher("lrw(aes)", ENCRYPT, aes_lrw_enc_tv_template,
1437 AES_LRW_ENC_TEST_VECTORS);
1438 test_cipher("lrw(aes)", DECRYPT, aes_lrw_dec_tv_template,
1439 AES_LRW_DEC_TEST_VECTORS);
Rik Snelf19f5112007-09-19 20:23:13 +08001440 test_cipher("xts(aes)", ENCRYPT, aes_xts_enc_tv_template,
1441 AES_XTS_ENC_TEST_VECTORS);
1442 test_cipher("xts(aes)", DECRYPT, aes_xts_dec_tv_template,
1443 AES_XTS_DEC_TEST_VECTORS);
Herbert Xu5311f242007-12-17 21:34:32 +08001444 test_cipher("rfc3686(ctr(aes))", ENCRYPT, aes_ctr_enc_tv_template,
Joy Latten23e353c2007-10-23 08:50:32 +08001445 AES_CTR_ENC_TEST_VECTORS);
Herbert Xu5311f242007-12-17 21:34:32 +08001446 test_cipher("rfc3686(ctr(aes))", DECRYPT, aes_ctr_dec_tv_template,
Joy Latten23e353c2007-10-23 08:50:32 +08001447 AES_CTR_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 break;
1449
1450 case 11:
1451 test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
1452 break;
Herbert Xuef2736f2005-06-22 13:26:03 -07001453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 case 12:
1455 test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
1456 break;
1457
1458 case 13:
Zoltan Sogor91755a92007-12-07 16:48:11 +08001459 test_comp("deflate", deflate_comp_tv_template,
1460 deflate_decomp_tv_template, DEFLATE_COMP_TEST_VECTORS,
1461 DEFLATE_DECOMP_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 break;
1463
1464 case 14:
Herbert Xucba83562006-08-13 08:26:09 +10001465 test_cipher("ecb(cast5)", ENCRYPT, cast5_enc_tv_template,
1466 CAST5_ENC_TEST_VECTORS);
1467 test_cipher("ecb(cast5)", DECRYPT, cast5_dec_tv_template,
1468 CAST5_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 break;
1470
1471 case 15:
Herbert Xucba83562006-08-13 08:26:09 +10001472 test_cipher("ecb(cast6)", ENCRYPT, cast6_enc_tv_template,
1473 CAST6_ENC_TEST_VECTORS);
1474 test_cipher("ecb(cast6)", DECRYPT, cast6_dec_tv_template,
1475 CAST6_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 break;
1477
1478 case 16:
Herbert Xucba83562006-08-13 08:26:09 +10001479 test_cipher("ecb(arc4)", ENCRYPT, arc4_enc_tv_template,
1480 ARC4_ENC_TEST_VECTORS);
1481 test_cipher("ecb(arc4)", DECRYPT, arc4_dec_tv_template,
1482 ARC4_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 break;
1484
1485 case 17:
1486 test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
1487 break;
1488
1489 case 18:
Herbert Xuc907ee72006-08-21 22:04:03 +10001490 test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 break;
1492
1493 case 19:
Herbert Xucba83562006-08-13 08:26:09 +10001494 test_cipher("ecb(tea)", ENCRYPT, tea_enc_tv_template,
1495 TEA_ENC_TEST_VECTORS);
1496 test_cipher("ecb(tea)", DECRYPT, tea_dec_tv_template,
1497 TEA_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 break;
1499
1500 case 20:
Herbert Xucba83562006-08-13 08:26:09 +10001501 test_cipher("ecb(xtea)", ENCRYPT, xtea_enc_tv_template,
1502 XTEA_ENC_TEST_VECTORS);
1503 test_cipher("ecb(xtea)", DECRYPT, xtea_dec_tv_template,
1504 XTEA_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 break;
1506
1507 case 21:
Herbert Xucba83562006-08-13 08:26:09 +10001508 test_cipher("ecb(khazad)", ENCRYPT, khazad_enc_tv_template,
1509 KHAZAD_ENC_TEST_VECTORS);
1510 test_cipher("ecb(khazad)", DECRYPT, khazad_dec_tv_template,
1511 KHAZAD_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 break;
1513
1514 case 22:
1515 test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
1516 break;
1517
1518 case 23:
1519 test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
1520 break;
1521
1522 case 24:
1523 test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
1524 break;
1525
1526 case 25:
Herbert Xucba83562006-08-13 08:26:09 +10001527 test_cipher("ecb(tnepres)", ENCRYPT, tnepres_enc_tv_template,
1528 TNEPRES_ENC_TEST_VECTORS);
1529 test_cipher("ecb(tnepres)", DECRYPT, tnepres_dec_tv_template,
1530 TNEPRES_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 break;
1532
1533 case 26:
Herbert Xucba83562006-08-13 08:26:09 +10001534 test_cipher("ecb(anubis)", ENCRYPT, anubis_enc_tv_template,
1535 ANUBIS_ENC_TEST_VECTORS);
1536 test_cipher("ecb(anubis)", DECRYPT, anubis_dec_tv_template,
1537 ANUBIS_DEC_TEST_VECTORS);
1538 test_cipher("cbc(anubis)", ENCRYPT, anubis_cbc_enc_tv_template,
1539 ANUBIS_CBC_ENC_TEST_VECTORS);
1540 test_cipher("cbc(anubis)", DECRYPT, anubis_cbc_dec_tv_template,
1541 ANUBIS_CBC_ENC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 break;
1543
1544 case 27:
1545 test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
1546 break;
1547
1548 case 28:
1549
1550 test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
1551 break;
1552
1553 case 29:
1554 test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
1555 break;
Aaron Grothefb4f10e2005-09-01 17:42:46 -07001556
1557 case 30:
Herbert Xucba83562006-08-13 08:26:09 +10001558 test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
1559 XETA_ENC_TEST_VECTORS);
1560 test_cipher("ecb(xeta)", DECRYPT, xeta_dec_tv_template,
1561 XETA_DEC_TEST_VECTORS);
Aaron Grothefb4f10e2005-09-01 17:42:46 -07001562 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
David Howells90831632006-12-16 12:13:14 +11001564 case 31:
1565 test_cipher("pcbc(fcrypt)", ENCRYPT, fcrypt_pcbc_enc_tv_template,
1566 FCRYPT_ENC_TEST_VECTORS);
1567 test_cipher("pcbc(fcrypt)", DECRYPT, fcrypt_pcbc_dec_tv_template,
1568 FCRYPT_DEC_TEST_VECTORS);
1569 break;
1570
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +11001571 case 32:
1572 test_cipher("ecb(camellia)", ENCRYPT,
1573 camellia_enc_tv_template,
1574 CAMELLIA_ENC_TEST_VECTORS);
1575 test_cipher("ecb(camellia)", DECRYPT,
1576 camellia_dec_tv_template,
1577 CAMELLIA_DEC_TEST_VECTORS);
1578 test_cipher("cbc(camellia)", ENCRYPT,
1579 camellia_cbc_enc_tv_template,
1580 CAMELLIA_CBC_ENC_TEST_VECTORS);
1581 test_cipher("cbc(camellia)", DECRYPT,
1582 camellia_cbc_dec_tv_template,
1583 CAMELLIA_CBC_DEC_TEST_VECTORS);
1584 break;
Jonathan Lynchcd12fb92007-11-10 20:08:25 +08001585 case 33:
1586 test_hash("sha224", sha224_tv_template, SHA224_TEST_VECTORS);
1587 break;
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +11001588
Tan Swee Heng2407d602007-11-23 19:45:00 +08001589 case 34:
1590 test_cipher("salsa20", ENCRYPT,
1591 salsa20_stream_enc_tv_template,
1592 SALSA20_STREAM_ENC_TEST_VECTORS);
1593 break;
1594
Herbert Xu8df213d2007-12-02 14:55:47 +11001595 case 35:
1596 test_aead("gcm(aes)", ENCRYPT, aes_gcm_enc_tv_template,
1597 AES_GCM_ENC_TEST_VECTORS);
1598 test_aead("gcm(aes)", DECRYPT, aes_gcm_dec_tv_template,
1599 AES_GCM_DEC_TEST_VECTORS);
1600 break;
1601
Zoltan Sogor0b77abb2007-12-07 16:53:23 +08001602 case 36:
1603 test_comp("lzo", lzo_comp_tv_template, lzo_decomp_tv_template,
1604 LZO_COMP_TEST_VECTORS, LZO_DECOMP_TEST_VECTORS);
1605 break;
1606
Joy Latten93cc74e2007-12-12 20:24:22 +08001607 case 37:
1608 test_aead("ccm(aes)", ENCRYPT, aes_ccm_enc_tv_template,
1609 AES_CCM_ENC_TEST_VECTORS);
1610 test_aead("ccm(aes)", DECRYPT, aes_ccm_dec_tv_template,
1611 AES_CCM_DEC_TEST_VECTORS);
1612 break;
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 case 100:
Herbert Xue9d41162006-08-19 21:38:49 +10001615 test_hash("hmac(md5)", hmac_md5_tv_template,
1616 HMAC_MD5_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 break;
Herbert Xuef2736f2005-06-22 13:26:03 -07001618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 case 101:
Herbert Xue9d41162006-08-19 21:38:49 +10001620 test_hash("hmac(sha1)", hmac_sha1_tv_template,
1621 HMAC_SHA1_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 break;
Herbert Xuef2736f2005-06-22 13:26:03 -07001623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 case 102:
Herbert Xue9d41162006-08-19 21:38:49 +10001625 test_hash("hmac(sha256)", hmac_sha256_tv_template,
1626 HMAC_SHA256_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 break;
1628
Andrew Donofrioa28091a2006-12-10 12:10:20 +11001629 case 103:
1630 test_hash("hmac(sha384)", hmac_sha384_tv_template,
1631 HMAC_SHA384_TEST_VECTORS);
1632 break;
1633
1634 case 104:
1635 test_hash("hmac(sha512)", hmac_sha512_tv_template,
1636 HMAC_SHA512_TEST_VECTORS);
1637 break;
Herbert Xu38ed9ab2008-01-01 15:59:28 +11001638
Jonathan Lynchcd12fb92007-11-10 20:08:25 +08001639 case 105:
1640 test_hash("hmac(sha224)", hmac_sha224_tv_template,
1641 HMAC_SHA224_TEST_VECTORS);
1642 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Herbert Xu38ed9ab2008-01-01 15:59:28 +11001644 case 106:
1645 test_hash("xcbc(aes)", aes_xcbc128_tv_template,
1646 XCBC_AES_TEST_VECTORS);
1647 break;
1648
Harald Welteebfd9bc2005-06-22 13:27:23 -07001649 case 200:
Herbert Xucba83562006-08-13 08:26:09 +10001650 test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001651 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +10001652 test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001653 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +10001654 test_cipher_speed("cbc(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("cbc(aes)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001657 speed_template_16_24_32);
Rik Snelf3d10442006-11-29 19:01:41 +11001658 test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001659 speed_template_32_40_48);
Rik Snelf3d10442006-11-29 19:01:41 +11001660 test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001661 speed_template_32_40_48);
Rik Snelf19f5112007-09-19 20:23:13 +08001662 test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001663 speed_template_32_48_64);
Rik Snelf19f5112007-09-19 20:23:13 +08001664 test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001665 speed_template_32_48_64);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001666 break;
1667
1668 case 201:
Herbert Xucba83562006-08-13 08:26:09 +10001669 test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001670 des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS,
1671 speed_template_24);
Herbert Xucba83562006-08-13 08:26:09 +10001672 test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001673 des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS,
1674 speed_template_24);
Herbert Xucba83562006-08-13 08:26:09 +10001675 test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001676 des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS,
1677 speed_template_24);
Herbert Xucba83562006-08-13 08:26:09 +10001678 test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001679 des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS,
1680 speed_template_24);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001681 break;
1682
1683 case 202:
Herbert Xucba83562006-08-13 08:26:09 +10001684 test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001685 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +10001686 test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001687 speed_template_16_24_32);
Herbert Xucba83562006-08-13 08:26:09 +10001688 test_cipher_speed("cbc(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("cbc(twofish)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001691 speed_template_16_24_32);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001692 break;
1693
1694 case 203:
Herbert Xucba83562006-08-13 08:26:09 +10001695 test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001696 speed_template_8_32);
Herbert Xucba83562006-08-13 08:26:09 +10001697 test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001698 speed_template_8_32);
Herbert Xucba83562006-08-13 08:26:09 +10001699 test_cipher_speed("cbc(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("cbc(blowfish)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001702 speed_template_8_32);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001703 break;
1704
1705 case 204:
Herbert Xucba83562006-08-13 08:26:09 +10001706 test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001707 speed_template_8);
Herbert Xucba83562006-08-13 08:26:09 +10001708 test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001709 speed_template_8);
Herbert Xucba83562006-08-13 08:26:09 +10001710 test_cipher_speed("cbc(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("cbc(des)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001713 speed_template_8);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001714 break;
1715
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +11001716 case 205:
1717 test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001718 speed_template_16_24_32);
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +11001719 test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001720 speed_template_16_24_32);
Noriaki TAKAMIYA02ab5a72007-01-24 21:48:19 +11001721 test_cipher_speed("cbc(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("cbc(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 break;
1726
Tan Swee Heng5de8f1b2007-12-07 17:17:43 +08001727 case 206:
1728 test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
Sebastian Siewior477035c2008-03-11 21:24:26 +08001729 speed_template_16_32);
Tan Swee Heng5de8f1b2007-12-07 17:17:43 +08001730 break;
1731
Michal Ludvige8057922006-05-30 22:04:19 +10001732 case 300:
1733 /* fall through */
1734
1735 case 301:
Herbert Xue9d41162006-08-19 21:38:49 +10001736 test_hash_speed("md4", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001737 if (mode > 300 && mode < 400) break;
1738
1739 case 302:
Herbert Xue9d41162006-08-19 21:38:49 +10001740 test_hash_speed("md5", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001741 if (mode > 300 && mode < 400) break;
1742
1743 case 303:
Herbert Xue9d41162006-08-19 21:38:49 +10001744 test_hash_speed("sha1", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001745 if (mode > 300 && mode < 400) break;
1746
1747 case 304:
Herbert Xue9d41162006-08-19 21:38:49 +10001748 test_hash_speed("sha256", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001749 if (mode > 300 && mode < 400) break;
1750
1751 case 305:
Herbert Xue9d41162006-08-19 21:38:49 +10001752 test_hash_speed("sha384", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001753 if (mode > 300 && mode < 400) break;
1754
1755 case 306:
Herbert Xue9d41162006-08-19 21:38:49 +10001756 test_hash_speed("sha512", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001757 if (mode > 300 && mode < 400) break;
1758
1759 case 307:
Herbert Xue9d41162006-08-19 21:38:49 +10001760 test_hash_speed("wp256", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001761 if (mode > 300 && mode < 400) break;
1762
1763 case 308:
Herbert Xue9d41162006-08-19 21:38:49 +10001764 test_hash_speed("wp384", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001765 if (mode > 300 && mode < 400) break;
1766
1767 case 309:
Herbert Xue9d41162006-08-19 21:38:49 +10001768 test_hash_speed("wp512", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001769 if (mode > 300 && mode < 400) break;
1770
1771 case 310:
Herbert Xue9d41162006-08-19 21:38:49 +10001772 test_hash_speed("tgr128", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001773 if (mode > 300 && mode < 400) break;
1774
1775 case 311:
Herbert Xue9d41162006-08-19 21:38:49 +10001776 test_hash_speed("tgr160", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001777 if (mode > 300 && mode < 400) break;
1778
1779 case 312:
Herbert Xue9d41162006-08-19 21:38:49 +10001780 test_hash_speed("tgr192", sec, generic_hash_speed_template);
Michal Ludvige8057922006-05-30 22:04:19 +10001781 if (mode > 300 && mode < 400) break;
1782
Jonathan Lynchcd12fb92007-11-10 20:08:25 +08001783 case 313:
1784 test_hash_speed("sha224", sec, generic_hash_speed_template);
1785 if (mode > 300 && mode < 400) break;
1786
Michal Ludvige8057922006-05-30 22:04:19 +10001787 case 399:
1788 break;
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 case 1000:
1791 test_available();
1792 break;
Herbert Xuef2736f2005-06-22 13:26:03 -07001793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 default:
1795 /* useful for debugging */
1796 printk("not testing anything\n");
1797 break;
1798 }
1799}
1800
Herbert Xuef2736f2005-06-22 13:26:03 -07001801static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Mikko Herranene3a4ea42007-11-26 22:12:07 +08001803 int err = -ENOMEM;
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 tvmem = kmalloc(TVMEMSIZE, GFP_KERNEL);
1806 if (tvmem == NULL)
Mikko Herranene3a4ea42007-11-26 22:12:07 +08001807 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 xbuf = kmalloc(XBUFSIZE, GFP_KERNEL);
Mikko Herranene3a4ea42007-11-26 22:12:07 +08001810 if (xbuf == NULL)
1811 goto err_free_tv;
1812
1813 axbuf = kmalloc(XBUFSIZE, GFP_KERNEL);
1814 if (axbuf == NULL)
1815 goto err_free_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 do_test();
1818
Michal Ludvig14fdf472006-05-30 14:49:38 +10001819 /* We intentionaly return -EAGAIN to prevent keeping
1820 * the module. It does all its work from init()
1821 * and doesn't offer any runtime functionality
1822 * => we don't need it in the memory, do we?
1823 * -- mludvig
1824 */
Mikko Herranene3a4ea42007-11-26 22:12:07 +08001825 err = -EAGAIN;
1826
1827 kfree(axbuf);
1828 err_free_xbuf:
1829 kfree(xbuf);
1830 err_free_tv:
1831 kfree(tvmem);
1832
1833 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
1835
1836/*
1837 * If an init function is provided, an exit function must also be provided
1838 * to allow module unload.
1839 */
1840static void __exit fini(void) { }
1841
1842module_init(init);
1843module_exit(fini);
1844
1845module_param(mode, int, 0);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001846module_param(sec, uint, 0);
Herbert Xu6a179442005-06-22 13:29:03 -07001847MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
1848 "(defaults to zero which uses CPU cycles instead)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850MODULE_LICENSE("GPL");
1851MODULE_DESCRIPTION("Quick & dirty crypto testing module");
1852MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");