blob: e52f56c5bd5e44320f6f49b70e252680f014f222 [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>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
Herbert Xuef2736f2005-06-22 13:26:03 -070012 * Software Foundation; either version 2 of the License, or (at your option)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * any later version.
14 *
Harald Welteebfd9bc2005-06-22 13:27:23 -070015 * 2004-08-09 Added cipher speed tests (Reyk Floeter <reyk@vantronix.net>)
16 * 2003-09-14 Rewritten by Kartikey Mahendra Bhatt
17 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
20#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/mm.h>
23#include <linux/slab.h>
David Hardeman378f0582005-09-17 17:55:31 +100024#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/string.h>
26#include <linux/crypto.h>
27#include <linux/highmem.h>
28#include <linux/moduleparam.h>
Harald Welteebfd9bc2005-06-22 13:27:23 -070029#include <linux/jiffies.h>
Herbert Xu6a179442005-06-22 13:29:03 -070030#include <linux/timex.h>
31#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "tcrypt.h"
33
34/*
35 * Need to kmalloc() memory for testing kmap().
36 */
Harald Welteebfd9bc2005-06-22 13:27:23 -070037#define TVMEMSIZE 16384
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define XBUFSIZE 32768
39
40/*
41 * Indexes into the xbuf to simulate cross-page access.
42 */
43#define IDX1 37
44#define IDX2 32400
45#define IDX3 1
46#define IDX4 8193
47#define IDX5 22222
48#define IDX6 17101
49#define IDX7 27333
50#define IDX8 3000
51
52/*
53* Used by test_cipher()
54*/
55#define ENCRYPT 1
56#define DECRYPT 0
57#define MODE_ECB 1
58#define MODE_CBC 0
59
60static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
61
Harald Welteebfd9bc2005-06-22 13:27:23 -070062/*
63 * Used by test_cipher_speed()
64 */
Herbert Xu6a179442005-06-22 13:29:03 -070065static unsigned int sec;
Harald Welteebfd9bc2005-06-22 13:27:23 -070066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static int mode;
68static char *xbuf;
69static char *tvmem;
70
71static char *check[] = {
72 "des", "md5", "des3_ede", "rot13", "sha1", "sha256", "blowfish",
Herbert Xuef2736f2005-06-22 13:26:03 -070073 "twofish", "serpent", "sha384", "sha512", "md4", "aes", "cast6",
74 "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
Aaron Grothefb4f10e2005-09-01 17:42:46 -070075 "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -070076};
77
Herbert Xuef2736f2005-06-22 13:26:03 -070078static void hexdump(unsigned char *buf, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 while (len--)
81 printk("%02x", *buf++);
82
83 printk("\n");
84}
85
Herbert Xuef2736f2005-06-22 13:26:03 -070086static void test_hash(char *algo, struct hash_testvec *template,
87 unsigned int tcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Herbert Xuef2736f2005-06-22 13:26:03 -070089 unsigned int i, j, k, temp;
90 struct scatterlist sg[8];
91 char result[64];
92 struct crypto_tfm *tfm;
93 struct hash_testvec *hash_tv;
94 unsigned int tsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Herbert Xuef2736f2005-06-22 13:26:03 -070096 printk("\ntesting %s\n", algo);
97
98 tsize = sizeof(struct hash_testvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 tsize *= tcount;
Herbert Xuef2736f2005-06-22 13:26:03 -0700100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (tsize > TVMEMSIZE) {
102 printk("template (%u) too big for tvmem (%u)\n", tsize, TVMEMSIZE);
103 return;
104 }
105
106 memcpy(tvmem, template, tsize);
Herbert Xuef2736f2005-06-22 13:26:03 -0700107 hash_tv = (void *)tvmem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 tfm = crypto_alloc_tfm(algo, 0);
109 if (tfm == NULL) {
110 printk("failed to load transform for %s\n", algo);
111 return;
112 }
113
114 for (i = 0; i < tcount; i++) {
Herbert Xuef2736f2005-06-22 13:26:03 -0700115 printk("test %u:\n", i + 1);
116 memset(result, 0, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
David Hardeman378f0582005-09-17 17:55:31 +1000118 sg_set_buf(&sg[0], hash_tv[i].plaintext, hash_tv[i].psize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Herbert Xuef2736f2005-06-22 13:26:03 -0700120 crypto_digest_init(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (tfm->crt_u.digest.dit_setkey) {
Herbert Xuef2736f2005-06-22 13:26:03 -0700122 crypto_digest_setkey(tfm, hash_tv[i].key,
123 hash_tv[i].ksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
Herbert Xuef2736f2005-06-22 13:26:03 -0700125 crypto_digest_update(tfm, sg, 1);
126 crypto_digest_final(tfm, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Herbert Xuef2736f2005-06-22 13:26:03 -0700128 hexdump(result, crypto_tfm_alg_digestsize(tfm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 printk("%s\n",
Herbert Xuef2736f2005-06-22 13:26:03 -0700130 memcmp(result, hash_tv[i].digest,
131 crypto_tfm_alg_digestsize(tfm)) ?
132 "fail" : "pass");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
134
Herbert Xuef2736f2005-06-22 13:26:03 -0700135 printk("testing %s across pages\n", algo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 /* setup the dummy buffer first */
Herbert Xuef2736f2005-06-22 13:26:03 -0700138 memset(xbuf, 0, XBUFSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 j = 0;
141 for (i = 0; i < tcount; i++) {
142 if (hash_tv[i].np) {
143 j++;
Herbert Xuef2736f2005-06-22 13:26:03 -0700144 printk("test %u:\n", j);
145 memset(result, 0, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 temp = 0;
148 for (k = 0; k < hash_tv[i].np; k++) {
Herbert Xuef2736f2005-06-22 13:26:03 -0700149 memcpy(&xbuf[IDX[k]],
150 hash_tv[i].plaintext + temp,
151 hash_tv[i].tap[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 temp += hash_tv[i].tap[k];
David Hardeman378f0582005-09-17 17:55:31 +1000153 sg_set_buf(&sg[k], &xbuf[IDX[k]],
154 hash_tv[i].tap[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156
Herbert Xuef2736f2005-06-22 13:26:03 -0700157 crypto_digest_digest(tfm, sg, hash_tv[i].np, result);
158
159 hexdump(result, crypto_tfm_alg_digestsize(tfm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 printk("%s\n",
Herbert Xuef2736f2005-06-22 13:26:03 -0700161 memcmp(result, hash_tv[i].digest,
162 crypto_tfm_alg_digestsize(tfm)) ?
163 "fail" : "pass");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165 }
Herbert Xuef2736f2005-06-22 13:26:03 -0700166
167 crypto_free_tfm(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170
171#ifdef CONFIG_CRYPTO_HMAC
172
Herbert Xuef2736f2005-06-22 13:26:03 -0700173static void test_hmac(char *algo, struct hmac_testvec *template,
174 unsigned int tcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 unsigned int i, j, k, temp;
177 struct scatterlist sg[8];
178 char result[64];
179 struct crypto_tfm *tfm;
180 struct hmac_testvec *hmac_tv;
181 unsigned int tsize, klen;
182
183 tfm = crypto_alloc_tfm(algo, 0);
184 if (tfm == NULL) {
185 printk("failed to load transform for %s\n", algo);
186 return;
187 }
188
189 printk("\ntesting hmac_%s\n", algo);
Herbert Xuef2736f2005-06-22 13:26:03 -0700190
191 tsize = sizeof(struct hmac_testvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 tsize *= tcount;
193 if (tsize > TVMEMSIZE) {
194 printk("template (%u) too big for tvmem (%u)\n", tsize,
195 TVMEMSIZE);
196 goto out;
197 }
198
199 memcpy(tvmem, template, tsize);
Herbert Xuef2736f2005-06-22 13:26:03 -0700200 hmac_tv = (void *)tvmem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 for (i = 0; i < tcount; i++) {
203 printk("test %u:\n", i + 1);
204 memset(result, 0, sizeof (result));
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 klen = hmac_tv[i].ksize;
David Hardeman378f0582005-09-17 17:55:31 +1000207 sg_set_buf(&sg[0], hmac_tv[i].plaintext, hmac_tv[i].psize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 crypto_hmac(tfm, hmac_tv[i].key, &klen, sg, 1, result);
210
211 hexdump(result, crypto_tfm_alg_digestsize(tfm));
212 printk("%s\n",
213 memcmp(result, hmac_tv[i].digest,
214 crypto_tfm_alg_digestsize(tfm)) ? "fail" :
215 "pass");
216 }
217
218 printk("\ntesting hmac_%s across pages\n", algo);
219
220 memset(xbuf, 0, XBUFSIZE);
Herbert Xuef2736f2005-06-22 13:26:03 -0700221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 j = 0;
223 for (i = 0; i < tcount; i++) {
224 if (hmac_tv[i].np) {
225 j++;
Herbert Xuef2736f2005-06-22 13:26:03 -0700226 printk("test %u:\n",j);
227 memset(result, 0, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 temp = 0;
230 klen = hmac_tv[i].ksize;
231 for (k = 0; k < hmac_tv[i].np; k++) {
Herbert Xuef2736f2005-06-22 13:26:03 -0700232 memcpy(&xbuf[IDX[k]],
233 hmac_tv[i].plaintext + temp,
234 hmac_tv[i].tap[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 temp += hmac_tv[i].tap[k];
David Hardeman378f0582005-09-17 17:55:31 +1000236 sg_set_buf(&sg[k], &xbuf[IDX[k]],
237 hmac_tv[i].tap[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239
Herbert Xuef2736f2005-06-22 13:26:03 -0700240 crypto_hmac(tfm, hmac_tv[i].key, &klen, sg,
241 hmac_tv[i].np, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 hexdump(result, crypto_tfm_alg_digestsize(tfm));
Herbert Xuef2736f2005-06-22 13:26:03 -0700243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 printk("%s\n",
Herbert Xuef2736f2005-06-22 13:26:03 -0700245 memcmp(result, hmac_tv[i].digest,
246 crypto_tfm_alg_digestsize(tfm)) ?
247 "fail" : "pass");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249 }
250out:
251 crypto_free_tfm(tfm);
252}
253
254#endif /* CONFIG_CRYPTO_HMAC */
255
Herbert Xuef2736f2005-06-22 13:26:03 -0700256static void test_cipher(char *algo, int mode, int enc,
257 struct cipher_testvec *template, unsigned int tcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 unsigned int ret, i, j, k, temp;
260 unsigned int tsize;
David Hardeman378f0582005-09-17 17:55:31 +1000261 char *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 struct crypto_tfm *tfm;
263 char *key;
264 struct cipher_testvec *cipher_tv;
265 struct scatterlist sg[8];
Herbert Xu3cc38162005-06-22 13:26:36 -0700266 const char *e, *m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 if (enc == ENCRYPT)
Herbert Xu3cc38162005-06-22 13:26:36 -0700269 e = "encryption";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 else
Herbert Xu3cc38162005-06-22 13:26:36 -0700271 e = "decryption";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (mode == MODE_ECB)
Herbert Xu3cc38162005-06-22 13:26:36 -0700273 m = "ECB";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 else
Herbert Xu3cc38162005-06-22 13:26:36 -0700275 m = "CBC";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Herbert Xuef2736f2005-06-22 13:26:03 -0700277 printk("\ntesting %s %s %s\n", algo, m, e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Herbert Xuef2736f2005-06-22 13:26:03 -0700279 tsize = sizeof (struct cipher_testvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 tsize *= tcount;
Herbert Xuef2736f2005-06-22 13:26:03 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (tsize > TVMEMSIZE) {
283 printk("template (%u) too big for tvmem (%u)\n", tsize,
284 TVMEMSIZE);
285 return;
286 }
287
288 memcpy(tvmem, template, tsize);
Herbert Xuef2736f2005-06-22 13:26:03 -0700289 cipher_tv = (void *)tvmem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Herbert Xuef2736f2005-06-22 13:26:03 -0700291 if (mode)
292 tfm = crypto_alloc_tfm(algo, 0);
293 else
294 tfm = crypto_alloc_tfm(algo, CRYPTO_TFM_MODE_CBC);
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (tfm == NULL) {
297 printk("failed to load transform for %s %s\n", algo, m);
298 return;
299 }
Herbert Xuef2736f2005-06-22 13:26:03 -0700300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 j = 0;
302 for (i = 0; i < tcount; i++) {
303 if (!(cipher_tv[i].np)) {
Herbert Xuef2736f2005-06-22 13:26:03 -0700304 j++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 printk("test %u (%d bit key):\n",
306 j, cipher_tv[i].klen * 8);
307
308 tfm->crt_flags = 0;
Herbert Xuef2736f2005-06-22 13:26:03 -0700309 if (cipher_tv[i].wk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 tfm->crt_flags |= CRYPTO_TFM_REQ_WEAK_KEY;
311 key = cipher_tv[i].key;
Herbert Xuef2736f2005-06-22 13:26:03 -0700312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 ret = crypto_cipher_setkey(tfm, key, cipher_tv[i].klen);
314 if (ret) {
315 printk("setkey() failed flags=%x\n", tfm->crt_flags);
Herbert Xuef2736f2005-06-22 13:26:03 -0700316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (!cipher_tv[i].fail)
318 goto out;
Herbert Xuef2736f2005-06-22 13:26:03 -0700319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
David Hardeman378f0582005-09-17 17:55:31 +1000321 sg_set_buf(&sg[0], cipher_tv[i].input,
322 cipher_tv[i].ilen);
Herbert Xuef2736f2005-06-22 13:26:03 -0700323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (!mode) {
325 crypto_cipher_set_iv(tfm, cipher_tv[i].iv,
Herbert Xuef2736f2005-06-22 13:26:03 -0700326 crypto_tfm_alg_ivsize(tfm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
Herbert Xuef2736f2005-06-22 13:26:03 -0700328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (enc)
330 ret = crypto_cipher_encrypt(tfm, sg, sg, cipher_tv[i].ilen);
331 else
332 ret = crypto_cipher_decrypt(tfm, sg, sg, cipher_tv[i].ilen);
Herbert Xuef2736f2005-06-22 13:26:03 -0700333
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (ret) {
336 printk("%s () failed flags=%x\n", e, tfm->crt_flags);
337 goto out;
Herbert Xuef2736f2005-06-22 13:26:03 -0700338 }
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 q = kmap(sg[0].page) + sg[0].offset;
341 hexdump(q, cipher_tv[i].rlen);
Herbert Xuef2736f2005-06-22 13:26:03 -0700342
343 printk("%s\n",
344 memcmp(q, cipher_tv[i].result,
345 cipher_tv[i].rlen) ? "fail" : "pass");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347 }
Herbert Xuef2736f2005-06-22 13:26:03 -0700348
349 printk("\ntesting %s %s %s across pages (chunking)\n", algo, m, e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 memset(xbuf, 0, XBUFSIZE);
Herbert Xuef2736f2005-06-22 13:26:03 -0700351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 j = 0;
353 for (i = 0; i < tcount; i++) {
354 if (cipher_tv[i].np) {
Herbert Xuef2736f2005-06-22 13:26:03 -0700355 j++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 printk("test %u (%d bit key):\n",
357 j, cipher_tv[i].klen * 8);
358
Herbert Xuef2736f2005-06-22 13:26:03 -0700359 tfm->crt_flags = 0;
360 if (cipher_tv[i].wk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 tfm->crt_flags |= CRYPTO_TFM_REQ_WEAK_KEY;
362 key = cipher_tv[i].key;
Herbert Xuef2736f2005-06-22 13:26:03 -0700363
364 ret = crypto_cipher_setkey(tfm, key, cipher_tv[i].klen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (ret) {
366 printk("setkey() failed flags=%x\n", tfm->crt_flags);
Herbert Xuef2736f2005-06-22 13:26:03 -0700367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (!cipher_tv[i].fail)
369 goto out;
370 }
371
372 temp = 0;
373 for (k = 0; k < cipher_tv[i].np; k++) {
Herbert Xuef2736f2005-06-22 13:26:03 -0700374 memcpy(&xbuf[IDX[k]],
375 cipher_tv[i].input + temp,
376 cipher_tv[i].tap[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 temp += cipher_tv[i].tap[k];
David Hardeman378f0582005-09-17 17:55:31 +1000378 sg_set_buf(&sg[k], &xbuf[IDX[k]],
379 cipher_tv[i].tap[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
Herbert Xuef2736f2005-06-22 13:26:03 -0700381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (!mode) {
383 crypto_cipher_set_iv(tfm, cipher_tv[i].iv,
Herbert Xuef2736f2005-06-22 13:26:03 -0700384 crypto_tfm_alg_ivsize(tfm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
Herbert Xuef2736f2005-06-22 13:26:03 -0700386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (enc)
388 ret = crypto_cipher_encrypt(tfm, sg, sg, cipher_tv[i].ilen);
389 else
390 ret = crypto_cipher_decrypt(tfm, sg, sg, cipher_tv[i].ilen);
Herbert Xuef2736f2005-06-22 13:26:03 -0700391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (ret) {
393 printk("%s () failed flags=%x\n", e, tfm->crt_flags);
394 goto out;
395 }
396
397 temp = 0;
398 for (k = 0; k < cipher_tv[i].np; k++) {
399 printk("page %u\n", k);
400 q = kmap(sg[k].page) + sg[k].offset;
401 hexdump(q, cipher_tv[i].tap[k]);
Herbert Xuef2736f2005-06-22 13:26:03 -0700402 printk("%s\n",
403 memcmp(q, cipher_tv[i].result + temp,
404 cipher_tv[i].tap[k]) ? "fail" :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 "pass");
406 temp += cipher_tv[i].tap[k];
407 }
408 }
409 }
410
411out:
412 crypto_free_tfm(tfm);
413}
414
Herbert Xu6a179442005-06-22 13:29:03 -0700415static int test_cipher_jiffies(struct crypto_tfm *tfm, int enc, char *p,
416 int blen, int sec)
417{
Herbert Xu6df5b9f2005-09-19 22:30:11 +1000418 struct scatterlist sg[1];
Herbert Xu6a179442005-06-22 13:29:03 -0700419 unsigned long start, end;
420 int bcount;
421 int ret;
422
Herbert Xu6df5b9f2005-09-19 22:30:11 +1000423 sg_set_buf(sg, p, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700424
425 for (start = jiffies, end = start + sec * HZ, bcount = 0;
426 time_before(jiffies, end); bcount++) {
427 if (enc)
428 ret = crypto_cipher_encrypt(tfm, sg, sg, blen);
429 else
430 ret = crypto_cipher_decrypt(tfm, sg, sg, blen);
431
432 if (ret)
433 return ret;
434 }
435
436 printk("%d operations in %d seconds (%ld bytes)\n",
437 bcount, sec, (long)bcount * blen);
438 return 0;
439}
440
441static int test_cipher_cycles(struct crypto_tfm *tfm, int enc, char *p,
442 int blen)
443{
Herbert Xu6df5b9f2005-09-19 22:30:11 +1000444 struct scatterlist sg[1];
Herbert Xu6a179442005-06-22 13:29:03 -0700445 unsigned long cycles = 0;
446 int ret = 0;
447 int i;
448
Herbert Xu6df5b9f2005-09-19 22:30:11 +1000449 sg_set_buf(sg, p, blen);
Herbert Xu6a179442005-06-22 13:29:03 -0700450
451 local_bh_disable();
452 local_irq_disable();
453
454 /* Warm-up run. */
455 for (i = 0; i < 4; i++) {
456 if (enc)
457 ret = crypto_cipher_encrypt(tfm, sg, sg, blen);
458 else
459 ret = crypto_cipher_decrypt(tfm, sg, sg, blen);
460
461 if (ret)
462 goto out;
463 }
464
465 /* The real thing. */
466 for (i = 0; i < 8; i++) {
467 cycles_t start, end;
468
469 start = get_cycles();
470 if (enc)
471 ret = crypto_cipher_encrypt(tfm, sg, sg, blen);
472 else
473 ret = crypto_cipher_decrypt(tfm, sg, sg, blen);
474 end = get_cycles();
475
476 if (ret)
477 goto out;
478
479 cycles += end - start;
480 }
481
482out:
483 local_irq_enable();
484 local_bh_enable();
485
486 if (ret == 0)
487 printk("1 operation in %lu cycles (%d bytes)\n",
488 (cycles + 4) / 8, blen);
489
490 return ret;
491}
492
Harald Welteebfd9bc2005-06-22 13:27:23 -0700493static void test_cipher_speed(char *algo, int mode, int enc, unsigned int sec,
Herbert Xudce907c2005-06-22 13:27:51 -0700494 struct cipher_testvec *template,
495 unsigned int tcount, struct cipher_speed *speed)
Harald Welteebfd9bc2005-06-22 13:27:23 -0700496{
Herbert Xudce907c2005-06-22 13:27:51 -0700497 unsigned int ret, i, j, iv_len;
Harald Welteebfd9bc2005-06-22 13:27:23 -0700498 unsigned char *key, *p, iv[128];
499 struct crypto_tfm *tfm;
Harald Welteebfd9bc2005-06-22 13:27:23 -0700500 const char *e, *m;
501
502 if (enc == ENCRYPT)
503 e = "encryption";
504 else
505 e = "decryption";
506 if (mode == MODE_ECB)
507 m = "ECB";
508 else
509 m = "CBC";
510
511 printk("\ntesting speed of %s %s %s\n", algo, m, e);
512
513 if (mode)
514 tfm = crypto_alloc_tfm(algo, 0);
515 else
516 tfm = crypto_alloc_tfm(algo, CRYPTO_TFM_MODE_CBC);
517
518 if (tfm == NULL) {
519 printk("failed to load transform for %s %s\n", algo, m);
520 return;
521 }
522
523 for (i = 0; speed[i].klen != 0; i++) {
524 if ((speed[i].blen + speed[i].klen) > TVMEMSIZE) {
525 printk("template (%u) too big for tvmem (%u)\n",
526 speed[i].blen + speed[i].klen, TVMEMSIZE);
527 goto out;
528 }
529
530 printk("test %u (%d bit key, %d byte blocks): ", i,
531 speed[i].klen * 8, speed[i].blen);
532
533 memset(tvmem, 0xff, speed[i].klen + speed[i].blen);
534
535 /* set key, plain text and IV */
536 key = (unsigned char *)tvmem;
Herbert Xudce907c2005-06-22 13:27:51 -0700537 for (j = 0; j < tcount; j++) {
538 if (template[j].klen == speed[i].klen) {
539 key = template[j].key;
540 break;
541 }
542 }
Harald Welteebfd9bc2005-06-22 13:27:23 -0700543 p = (unsigned char *)tvmem + speed[i].klen;
544
545 ret = crypto_cipher_setkey(tfm, key, speed[i].klen);
546 if (ret) {
547 printk("setkey() failed flags=%x\n", tfm->crt_flags);
548 goto out;
549 }
550
551 if (!mode) {
552 iv_len = crypto_tfm_alg_ivsize(tfm);
553 memset(&iv, 0xff, iv_len);
554 crypto_cipher_set_iv(tfm, iv, iv_len);
555 }
556
Herbert Xu6a179442005-06-22 13:29:03 -0700557 if (sec)
558 ret = test_cipher_jiffies(tfm, enc, p, speed[i].blen,
559 sec);
560 else
561 ret = test_cipher_cycles(tfm, enc, p, speed[i].blen);
Harald Welteebfd9bc2005-06-22 13:27:23 -0700562
Herbert Xu6a179442005-06-22 13:29:03 -0700563 if (ret) {
564 printk("%s() failed flags=%x\n", e, tfm->crt_flags);
565 break;
Harald Welteebfd9bc2005-06-22 13:27:23 -0700566 }
Harald Welteebfd9bc2005-06-22 13:27:23 -0700567 }
568
569out:
570 crypto_free_tfm(tfm);
571}
572
Michal Ludvige8057922006-05-30 22:04:19 +1000573static void test_digest_jiffies(struct crypto_tfm *tfm, char *p, int blen,
574 int plen, char *out, int sec)
575{
576 struct scatterlist sg[1];
577 unsigned long start, end;
578 int bcount, pcount;
579
580 for (start = jiffies, end = start + sec * HZ, bcount = 0;
581 time_before(jiffies, end); bcount++) {
582 crypto_digest_init(tfm);
583 for (pcount = 0; pcount < blen; pcount += plen) {
584 sg_set_buf(sg, p + pcount, plen);
585 crypto_digest_update(tfm, sg, 1);
586 }
587 /* we assume there is enough space in 'out' for the result */
588 crypto_digest_final(tfm, out);
589 }
590
591 printk("%6u opers/sec, %9lu bytes/sec\n",
592 bcount / sec, ((long)bcount * blen) / sec);
593
594 return;
595}
596
597static void test_digest_cycles(struct crypto_tfm *tfm, char *p, int blen,
598 int plen, char *out)
599{
600 struct scatterlist sg[1];
601 unsigned long cycles = 0;
602 int i, pcount;
603
604 local_bh_disable();
605 local_irq_disable();
606
607 /* Warm-up run. */
608 for (i = 0; i < 4; i++) {
609 crypto_digest_init(tfm);
610 for (pcount = 0; pcount < blen; pcount += plen) {
611 sg_set_buf(sg, p + pcount, plen);
612 crypto_digest_update(tfm, sg, 1);
613 }
614 crypto_digest_final(tfm, out);
615 }
616
617 /* The real thing. */
618 for (i = 0; i < 8; i++) {
619 cycles_t start, end;
620
621 crypto_digest_init(tfm);
622
623 start = get_cycles();
624
625 for (pcount = 0; pcount < blen; pcount += plen) {
626 sg_set_buf(sg, p + pcount, plen);
627 crypto_digest_update(tfm, sg, 1);
628 }
629 crypto_digest_final(tfm, out);
630
631 end = get_cycles();
632
633 cycles += end - start;
634 }
635
636 local_irq_enable();
637 local_bh_enable();
638
639 printk("%6lu cycles/operation, %4lu cycles/byte\n",
640 cycles / 8, cycles / (8 * blen));
641
642 return;
643}
644
645static void test_digest_speed(char *algo, unsigned int sec,
646 struct digest_speed *speed)
647{
648 struct crypto_tfm *tfm;
649 char output[1024];
650 int i;
651
652 printk("\ntesting speed of %s\n", algo);
653
654 tfm = crypto_alloc_tfm(algo, 0);
655
656 if (tfm == NULL) {
657 printk("failed to load transform for %s\n", algo);
658 return;
659 }
660
661 if (crypto_tfm_alg_digestsize(tfm) > sizeof(output)) {
662 printk("digestsize(%u) > outputbuffer(%zu)\n",
663 crypto_tfm_alg_digestsize(tfm), sizeof(output));
664 goto out;
665 }
666
667 for (i = 0; speed[i].blen != 0; i++) {
668 if (speed[i].blen > TVMEMSIZE) {
669 printk("template (%u) too big for tvmem (%u)\n",
670 speed[i].blen, TVMEMSIZE);
671 goto out;
672 }
673
674 printk("test%3u (%5u byte blocks,%5u bytes per update,%4u updates): ",
675 i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
676
677 memset(tvmem, 0xff, speed[i].blen);
678
679 if (sec)
680 test_digest_jiffies(tfm, tvmem, speed[i].blen, speed[i].plen, output, sec);
681 else
682 test_digest_cycles(tfm, tvmem, speed[i].blen, speed[i].plen, output);
683 }
684
685out:
686 crypto_free_tfm(tfm);
687}
688
Herbert Xuef2736f2005-06-22 13:26:03 -0700689static void test_deflate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
691 unsigned int i;
692 char result[COMP_BUF_SIZE];
693 struct crypto_tfm *tfm;
694 struct comp_testvec *tv;
695 unsigned int tsize;
696
697 printk("\ntesting deflate compression\n");
698
699 tsize = sizeof (deflate_comp_tv_template);
700 if (tsize > TVMEMSIZE) {
701 printk("template (%u) too big for tvmem (%u)\n", tsize,
702 TVMEMSIZE);
703 return;
704 }
705
706 memcpy(tvmem, deflate_comp_tv_template, tsize);
Herbert Xuef2736f2005-06-22 13:26:03 -0700707 tv = (void *)tvmem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 tfm = crypto_alloc_tfm("deflate", 0);
710 if (tfm == NULL) {
711 printk("failed to load transform for deflate\n");
712 return;
713 }
714
715 for (i = 0; i < DEFLATE_COMP_TEST_VECTORS; i++) {
716 int ilen, ret, dlen = COMP_BUF_SIZE;
Herbert Xuef2736f2005-06-22 13:26:03 -0700717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 printk("test %u:\n", i + 1);
719 memset(result, 0, sizeof (result));
720
721 ilen = tv[i].inlen;
722 ret = crypto_comp_compress(tfm, tv[i].input,
723 ilen, result, &dlen);
724 if (ret) {
725 printk("fail: ret=%d\n", ret);
726 continue;
727 }
728 hexdump(result, dlen);
729 printk("%s (ratio %d:%d)\n",
730 memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
731 ilen, dlen);
732 }
733
734 printk("\ntesting deflate decompression\n");
735
736 tsize = sizeof (deflate_decomp_tv_template);
737 if (tsize > TVMEMSIZE) {
738 printk("template (%u) too big for tvmem (%u)\n", tsize,
739 TVMEMSIZE);
740 goto out;
741 }
742
743 memcpy(tvmem, deflate_decomp_tv_template, tsize);
Herbert Xuef2736f2005-06-22 13:26:03 -0700744 tv = (void *)tvmem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 for (i = 0; i < DEFLATE_DECOMP_TEST_VECTORS; i++) {
747 int ilen, ret, dlen = COMP_BUF_SIZE;
Herbert Xuef2736f2005-06-22 13:26:03 -0700748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 printk("test %u:\n", i + 1);
750 memset(result, 0, sizeof (result));
751
752 ilen = tv[i].inlen;
753 ret = crypto_comp_decompress(tfm, tv[i].input,
754 ilen, result, &dlen);
755 if (ret) {
756 printk("fail: ret=%d\n", ret);
757 continue;
758 }
759 hexdump(result, dlen);
760 printk("%s (ratio %d:%d)\n",
761 memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
762 ilen, dlen);
763 }
764out:
765 crypto_free_tfm(tfm);
766}
767
Herbert Xuef2736f2005-06-22 13:26:03 -0700768static void test_crc32c(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770#define NUMVEC 6
771#define VECSIZE 40
772
773 int i, j, pass;
774 u32 crc;
775 u8 b, test_vec[NUMVEC][VECSIZE];
776 static u32 vec_results[NUMVEC] = {
777 0x0e2c157f, 0xe980ebf6, 0xde74bded,
778 0xd579c862, 0xba979ad0, 0x2b29d913
779 };
780 static u32 tot_vec_results = 0x24c5d375;
Herbert Xuef2736f2005-06-22 13:26:03 -0700781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 struct scatterlist sg[NUMVEC];
783 struct crypto_tfm *tfm;
784 char *fmtdata = "testing crc32c initialized to %08x: %s\n";
785#define SEEDTESTVAL 0xedcba987
786 u32 seed;
787
788 printk("\ntesting crc32c\n");
789
790 tfm = crypto_alloc_tfm("crc32c", 0);
791 if (tfm == NULL) {
792 printk("failed to load transform for crc32c\n");
793 return;
794 }
Herbert Xuef2736f2005-06-22 13:26:03 -0700795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 crypto_digest_init(tfm);
797 crypto_digest_final(tfm, (u8*)&crc);
798 printk(fmtdata, crc, (crc == 0) ? "pass" : "ERROR");
Herbert Xuef2736f2005-06-22 13:26:03 -0700799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /*
801 * stuff test_vec with known values, simple incrementing
802 * byte values.
803 */
804 b = 0;
805 for (i = 0; i < NUMVEC; i++) {
Herbert Xuef2736f2005-06-22 13:26:03 -0700806 for (j = 0; j < VECSIZE; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 test_vec[i][j] = ++b;
David Hardeman378f0582005-09-17 17:55:31 +1000808 sg_set_buf(&sg[i], test_vec[i], VECSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810
811 seed = SEEDTESTVAL;
812 (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32));
813 crypto_digest_final(tfm, (u8*)&crc);
814 printk("testing crc32c setkey returns %08x : %s\n", crc, (crc == (SEEDTESTVAL ^ ~(u32)0)) ?
815 "pass" : "ERROR");
Herbert Xuef2736f2005-06-22 13:26:03 -0700816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 printk("testing crc32c using update/final:\n");
818
819 pass = 1; /* assume all is well */
Herbert Xuef2736f2005-06-22 13:26:03 -0700820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 for (i = 0; i < NUMVEC; i++) {
822 seed = ~(u32)0;
823 (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32));
824 crypto_digest_update(tfm, &sg[i], 1);
825 crypto_digest_final(tfm, (u8*)&crc);
826 if (crc == vec_results[i]) {
827 printk(" %08x:OK", crc);
828 } else {
829 printk(" %08x:BAD, wanted %08x\n", crc, vec_results[i]);
830 pass = 0;
831 }
832 }
833
834 printk("\ntesting crc32c using incremental accumulator:\n");
835 crc = 0;
836 for (i = 0; i < NUMVEC; i++) {
837 seed = (crc ^ ~(u32)0);
838 (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32));
839 crypto_digest_update(tfm, &sg[i], 1);
840 crypto_digest_final(tfm, (u8*)&crc);
841 }
842 if (crc == tot_vec_results) {
843 printk(" %08x:OK", crc);
844 } else {
845 printk(" %08x:BAD, wanted %08x\n", crc, tot_vec_results);
846 pass = 0;
847 }
848
849 printk("\ntesting crc32c using digest:\n");
850 seed = ~(u32)0;
851 (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32));
852 crypto_digest_digest(tfm, sg, NUMVEC, (u8*)&crc);
853 if (crc == tot_vec_results) {
854 printk(" %08x:OK", crc);
855 } else {
856 printk(" %08x:BAD, wanted %08x\n", crc, tot_vec_results);
857 pass = 0;
858 }
Herbert Xuef2736f2005-06-22 13:26:03 -0700859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 printk("\n%s\n", pass ? "pass" : "ERROR");
861
862 crypto_free_tfm(tfm);
863 printk("crc32c test complete\n");
864}
865
Herbert Xuef2736f2005-06-22 13:26:03 -0700866static void test_available(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
868 char **name = check;
Herbert Xuef2736f2005-06-22 13:26:03 -0700869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 while (*name) {
871 printk("alg %s ", *name);
872 printk((crypto_alg_available(*name, 0)) ?
873 "found\n" : "not found\n");
874 name++;
Herbert Xuef2736f2005-06-22 13:26:03 -0700875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
Herbert Xuef2736f2005-06-22 13:26:03 -0700878static void do_test(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 switch (mode) {
881
882 case 0:
883 test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -0700884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -0700886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 //DES
888 test_cipher ("des", MODE_ECB, ENCRYPT, des_enc_tv_template, DES_ENC_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -0700889 test_cipher ("des", MODE_ECB, DECRYPT, des_dec_tv_template, DES_DEC_TEST_VECTORS);
890 test_cipher ("des", MODE_CBC, ENCRYPT, des_cbc_enc_tv_template, DES_CBC_ENC_TEST_VECTORS);
891 test_cipher ("des", MODE_CBC, DECRYPT, des_cbc_dec_tv_template, DES_CBC_DEC_TEST_VECTORS);
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 //DES3_EDE
894 test_cipher ("des3_ede", MODE_ECB, ENCRYPT, des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -0700895 test_cipher ("des3_ede", MODE_ECB, DECRYPT, des3_ede_dec_tv_template, DES3_EDE_DEC_TEST_VECTORS);
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -0700898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -0700900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 //BLOWFISH
902 test_cipher ("blowfish", MODE_ECB, ENCRYPT, bf_enc_tv_template, BF_ENC_TEST_VECTORS);
903 test_cipher ("blowfish", MODE_ECB, DECRYPT, bf_dec_tv_template, BF_DEC_TEST_VECTORS);
904 test_cipher ("blowfish", MODE_CBC, ENCRYPT, bf_cbc_enc_tv_template, BF_CBC_ENC_TEST_VECTORS);
905 test_cipher ("blowfish", MODE_CBC, DECRYPT, bf_cbc_dec_tv_template, BF_CBC_DEC_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -0700906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 //TWOFISH
908 test_cipher ("twofish", MODE_ECB, ENCRYPT, tf_enc_tv_template, TF_ENC_TEST_VECTORS);
909 test_cipher ("twofish", MODE_ECB, DECRYPT, tf_dec_tv_template, TF_DEC_TEST_VECTORS);
910 test_cipher ("twofish", MODE_CBC, ENCRYPT, tf_cbc_enc_tv_template, TF_CBC_ENC_TEST_VECTORS);
911 test_cipher ("twofish", MODE_CBC, DECRYPT, tf_cbc_dec_tv_template, TF_CBC_DEC_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -0700912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 //SERPENT
914 test_cipher ("serpent", MODE_ECB, ENCRYPT, serpent_enc_tv_template, SERPENT_ENC_TEST_VECTORS);
915 test_cipher ("serpent", MODE_ECB, DECRYPT, serpent_dec_tv_template, SERPENT_DEC_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -0700916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 //TNEPRES
918 test_cipher ("tnepres", MODE_ECB, ENCRYPT, tnepres_enc_tv_template, TNEPRES_ENC_TEST_VECTORS);
919 test_cipher ("tnepres", MODE_ECB, DECRYPT, tnepres_dec_tv_template, TNEPRES_DEC_TEST_VECTORS);
920
921 //AES
922 test_cipher ("aes", MODE_ECB, ENCRYPT, aes_enc_tv_template, AES_ENC_TEST_VECTORS);
923 test_cipher ("aes", MODE_ECB, DECRYPT, aes_dec_tv_template, AES_DEC_TEST_VECTORS);
Jan Glauber05f29fc2006-01-06 00:19:19 -0800924 test_cipher ("aes", MODE_CBC, ENCRYPT, aes_cbc_enc_tv_template, AES_CBC_ENC_TEST_VECTORS);
925 test_cipher ("aes", MODE_CBC, DECRYPT, aes_cbc_dec_tv_template, AES_CBC_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 //CAST5
928 test_cipher ("cast5", MODE_ECB, ENCRYPT, cast5_enc_tv_template, CAST5_ENC_TEST_VECTORS);
929 test_cipher ("cast5", MODE_ECB, DECRYPT, cast5_dec_tv_template, CAST5_DEC_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -0700930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 //CAST6
932 test_cipher ("cast6", MODE_ECB, ENCRYPT, cast6_enc_tv_template, CAST6_ENC_TEST_VECTORS);
933 test_cipher ("cast6", MODE_ECB, DECRYPT, cast6_dec_tv_template, CAST6_DEC_TEST_VECTORS);
934
935 //ARC4
936 test_cipher ("arc4", MODE_ECB, ENCRYPT, arc4_enc_tv_template, ARC4_ENC_TEST_VECTORS);
937 test_cipher ("arc4", MODE_ECB, DECRYPT, arc4_dec_tv_template, ARC4_DEC_TEST_VECTORS);
938
939 //TEA
940 test_cipher ("tea", MODE_ECB, ENCRYPT, tea_enc_tv_template, TEA_ENC_TEST_VECTORS);
941 test_cipher ("tea", MODE_ECB, DECRYPT, tea_dec_tv_template, TEA_DEC_TEST_VECTORS);
942
943
944 //XTEA
945 test_cipher ("xtea", MODE_ECB, ENCRYPT, xtea_enc_tv_template, XTEA_ENC_TEST_VECTORS);
946 test_cipher ("xtea", MODE_ECB, DECRYPT, xtea_dec_tv_template, XTEA_DEC_TEST_VECTORS);
947
948 //KHAZAD
949 test_cipher ("khazad", MODE_ECB, ENCRYPT, khazad_enc_tv_template, KHAZAD_ENC_TEST_VECTORS);
950 test_cipher ("khazad", MODE_ECB, DECRYPT, khazad_dec_tv_template, KHAZAD_DEC_TEST_VECTORS);
951
952 //ANUBIS
953 test_cipher ("anubis", MODE_ECB, ENCRYPT, anubis_enc_tv_template, ANUBIS_ENC_TEST_VECTORS);
954 test_cipher ("anubis", MODE_ECB, DECRYPT, anubis_dec_tv_template, ANUBIS_DEC_TEST_VECTORS);
955 test_cipher ("anubis", MODE_CBC, ENCRYPT, anubis_cbc_enc_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
956 test_cipher ("anubis", MODE_CBC, DECRYPT, anubis_cbc_dec_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
957
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700958 //XETA
959 test_cipher ("xeta", MODE_ECB, ENCRYPT, xeta_enc_tv_template, XETA_ENC_TEST_VECTORS);
960 test_cipher ("xeta", MODE_ECB, DECRYPT, xeta_dec_tv_template, XETA_DEC_TEST_VECTORS);
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
963 test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
964 test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
965 test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
966 test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
967 test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
968 test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
969 test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
970 test_deflate();
971 test_crc32c();
972#ifdef CONFIG_CRYPTO_HMAC
973 test_hmac("md5", hmac_md5_tv_template, HMAC_MD5_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -0700974 test_hmac("sha1", hmac_sha1_tv_template, HMAC_SHA1_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 test_hmac("sha256", hmac_sha256_tv_template, HMAC_SHA256_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -0700976#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
979 break;
980
981 case 1:
982 test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
983 break;
984
985 case 2:
986 test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
987 break;
988
989 case 3:
990 test_cipher ("des", MODE_ECB, ENCRYPT, des_enc_tv_template, DES_ENC_TEST_VECTORS);
991 test_cipher ("des", MODE_ECB, DECRYPT, des_dec_tv_template, DES_DEC_TEST_VECTORS);
992 test_cipher ("des", MODE_CBC, ENCRYPT, des_cbc_enc_tv_template, DES_CBC_ENC_TEST_VECTORS);
993 test_cipher ("des", MODE_CBC, DECRYPT, des_cbc_dec_tv_template, DES_CBC_DEC_TEST_VECTORS);
994 break;
995
996 case 4:
997 test_cipher ("des3_ede", MODE_ECB, ENCRYPT, des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -0700998 test_cipher ("des3_ede", MODE_ECB, DECRYPT, des3_ede_dec_tv_template, DES3_EDE_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 break;
1000
1001 case 5:
1002 test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
1003 break;
Herbert Xuef2736f2005-06-22 13:26:03 -07001004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 case 6:
1006 test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
1007 break;
Herbert Xuef2736f2005-06-22 13:26:03 -07001008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 case 7:
1010 test_cipher ("blowfish", MODE_ECB, ENCRYPT, bf_enc_tv_template, BF_ENC_TEST_VECTORS);
1011 test_cipher ("blowfish", MODE_ECB, DECRYPT, bf_dec_tv_template, BF_DEC_TEST_VECTORS);
1012 test_cipher ("blowfish", MODE_CBC, ENCRYPT, bf_cbc_enc_tv_template, BF_CBC_ENC_TEST_VECTORS);
1013 test_cipher ("blowfish", MODE_CBC, DECRYPT, bf_cbc_dec_tv_template, BF_CBC_DEC_TEST_VECTORS);
1014 break;
1015
1016 case 8:
1017 test_cipher ("twofish", MODE_ECB, ENCRYPT, tf_enc_tv_template, TF_ENC_TEST_VECTORS);
1018 test_cipher ("twofish", MODE_ECB, DECRYPT, tf_dec_tv_template, TF_DEC_TEST_VECTORS);
1019 test_cipher ("twofish", MODE_CBC, ENCRYPT, tf_cbc_enc_tv_template, TF_CBC_ENC_TEST_VECTORS);
1020 test_cipher ("twofish", MODE_CBC, DECRYPT, tf_cbc_dec_tv_template, TF_CBC_DEC_TEST_VECTORS);
1021 break;
Herbert Xuef2736f2005-06-22 13:26:03 -07001022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 case 9:
1024 test_cipher ("serpent", MODE_ECB, ENCRYPT, serpent_enc_tv_template, SERPENT_ENC_TEST_VECTORS);
1025 test_cipher ("serpent", MODE_ECB, DECRYPT, serpent_dec_tv_template, SERPENT_DEC_TEST_VECTORS);
1026 break;
1027
1028 case 10:
1029 test_cipher ("aes", MODE_ECB, ENCRYPT, aes_enc_tv_template, AES_ENC_TEST_VECTORS);
Herbert Xuef2736f2005-06-22 13:26:03 -07001030 test_cipher ("aes", MODE_ECB, DECRYPT, aes_dec_tv_template, AES_DEC_TEST_VECTORS);
Jan Glauber05f29fc2006-01-06 00:19:19 -08001031 test_cipher ("aes", MODE_CBC, ENCRYPT, aes_cbc_enc_tv_template, AES_CBC_ENC_TEST_VECTORS);
1032 test_cipher ("aes", MODE_CBC, DECRYPT, aes_cbc_dec_tv_template, AES_CBC_DEC_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 break;
1034
1035 case 11:
1036 test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
1037 break;
Herbert Xuef2736f2005-06-22 13:26:03 -07001038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 case 12:
1040 test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
1041 break;
1042
1043 case 13:
1044 test_deflate();
1045 break;
1046
1047 case 14:
1048 test_cipher ("cast5", MODE_ECB, ENCRYPT, cast5_enc_tv_template, CAST5_ENC_TEST_VECTORS);
1049 test_cipher ("cast5", MODE_ECB, DECRYPT, cast5_dec_tv_template, CAST5_DEC_TEST_VECTORS);
1050 break;
1051
1052 case 15:
1053 test_cipher ("cast6", MODE_ECB, ENCRYPT, cast6_enc_tv_template, CAST6_ENC_TEST_VECTORS);
1054 test_cipher ("cast6", MODE_ECB, DECRYPT, cast6_dec_tv_template, CAST6_DEC_TEST_VECTORS);
1055 break;
1056
1057 case 16:
1058 test_cipher ("arc4", MODE_ECB, ENCRYPT, arc4_enc_tv_template, ARC4_ENC_TEST_VECTORS);
1059 test_cipher ("arc4", MODE_ECB, DECRYPT, arc4_dec_tv_template, ARC4_DEC_TEST_VECTORS);
1060 break;
1061
1062 case 17:
1063 test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
1064 break;
1065
1066 case 18:
1067 test_crc32c();
1068 break;
1069
1070 case 19:
1071 test_cipher ("tea", MODE_ECB, ENCRYPT, tea_enc_tv_template, TEA_ENC_TEST_VECTORS);
1072 test_cipher ("tea", MODE_ECB, DECRYPT, tea_dec_tv_template, TEA_DEC_TEST_VECTORS);
1073 break;
1074
1075 case 20:
1076 test_cipher ("xtea", MODE_ECB, ENCRYPT, xtea_enc_tv_template, XTEA_ENC_TEST_VECTORS);
1077 test_cipher ("xtea", MODE_ECB, DECRYPT, xtea_dec_tv_template, XTEA_DEC_TEST_VECTORS);
1078 break;
1079
1080 case 21:
1081 test_cipher ("khazad", MODE_ECB, ENCRYPT, khazad_enc_tv_template, KHAZAD_ENC_TEST_VECTORS);
1082 test_cipher ("khazad", MODE_ECB, DECRYPT, khazad_dec_tv_template, KHAZAD_DEC_TEST_VECTORS);
1083 break;
1084
1085 case 22:
1086 test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
1087 break;
1088
1089 case 23:
1090 test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
1091 break;
1092
1093 case 24:
1094 test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
1095 break;
1096
1097 case 25:
1098 test_cipher ("tnepres", MODE_ECB, ENCRYPT, tnepres_enc_tv_template, TNEPRES_ENC_TEST_VECTORS);
1099 test_cipher ("tnepres", MODE_ECB, DECRYPT, tnepres_dec_tv_template, TNEPRES_DEC_TEST_VECTORS);
1100 break;
1101
1102 case 26:
1103 test_cipher ("anubis", MODE_ECB, ENCRYPT, anubis_enc_tv_template, ANUBIS_ENC_TEST_VECTORS);
1104 test_cipher ("anubis", MODE_ECB, DECRYPT, anubis_dec_tv_template, ANUBIS_DEC_TEST_VECTORS);
1105 test_cipher ("anubis", MODE_CBC, ENCRYPT, anubis_cbc_enc_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
1106 test_cipher ("anubis", MODE_CBC, DECRYPT, anubis_cbc_dec_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
1107 break;
1108
1109 case 27:
1110 test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
1111 break;
1112
1113 case 28:
1114
1115 test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
1116 break;
1117
1118 case 29:
1119 test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
1120 break;
Aaron Grothefb4f10e2005-09-01 17:42:46 -07001121
1122 case 30:
1123 test_cipher ("xeta", MODE_ECB, ENCRYPT, xeta_enc_tv_template, XETA_ENC_TEST_VECTORS);
1124 test_cipher ("xeta", MODE_ECB, DECRYPT, xeta_dec_tv_template, XETA_DEC_TEST_VECTORS);
1125 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127#ifdef CONFIG_CRYPTO_HMAC
1128 case 100:
1129 test_hmac("md5", hmac_md5_tv_template, HMAC_MD5_TEST_VECTORS);
1130 break;
Herbert Xuef2736f2005-06-22 13:26:03 -07001131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 case 101:
Herbert Xuef2736f2005-06-22 13:26:03 -07001133 test_hmac("sha1", hmac_sha1_tv_template, HMAC_SHA1_TEST_VECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 break;
Herbert Xuef2736f2005-06-22 13:26:03 -07001135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 case 102:
1137 test_hmac("sha256", hmac_sha256_tv_template, HMAC_SHA256_TEST_VECTORS);
1138 break;
1139
1140#endif
1141
Harald Welteebfd9bc2005-06-22 13:27:23 -07001142 case 200:
Herbert Xudce907c2005-06-22 13:27:51 -07001143 test_cipher_speed("aes", MODE_ECB, ENCRYPT, sec, NULL, 0,
1144 aes_speed_template);
1145 test_cipher_speed("aes", MODE_ECB, DECRYPT, sec, NULL, 0,
1146 aes_speed_template);
1147 test_cipher_speed("aes", MODE_CBC, ENCRYPT, sec, NULL, 0,
1148 aes_speed_template);
1149 test_cipher_speed("aes", MODE_CBC, DECRYPT, sec, NULL, 0,
1150 aes_speed_template);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001151 break;
1152
1153 case 201:
Herbert Xudce907c2005-06-22 13:27:51 -07001154 test_cipher_speed("des3_ede", MODE_ECB, ENCRYPT, sec,
1155 des3_ede_enc_tv_template,
1156 DES3_EDE_ENC_TEST_VECTORS,
1157 des3_ede_speed_template);
1158 test_cipher_speed("des3_ede", MODE_ECB, DECRYPT, sec,
1159 des3_ede_dec_tv_template,
1160 DES3_EDE_DEC_TEST_VECTORS,
1161 des3_ede_speed_template);
1162 test_cipher_speed("des3_ede", MODE_CBC, ENCRYPT, sec,
1163 des3_ede_enc_tv_template,
1164 DES3_EDE_ENC_TEST_VECTORS,
1165 des3_ede_speed_template);
1166 test_cipher_speed("des3_ede", MODE_CBC, DECRYPT, sec,
1167 des3_ede_dec_tv_template,
1168 DES3_EDE_DEC_TEST_VECTORS,
1169 des3_ede_speed_template);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001170 break;
1171
1172 case 202:
Herbert Xudce907c2005-06-22 13:27:51 -07001173 test_cipher_speed("twofish", MODE_ECB, ENCRYPT, sec, NULL, 0,
1174 twofish_speed_template);
1175 test_cipher_speed("twofish", MODE_ECB, DECRYPT, sec, NULL, 0,
1176 twofish_speed_template);
1177 test_cipher_speed("twofish", MODE_CBC, ENCRYPT, sec, NULL, 0,
1178 twofish_speed_template);
1179 test_cipher_speed("twofish", MODE_CBC, DECRYPT, sec, NULL, 0,
1180 twofish_speed_template);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001181 break;
1182
1183 case 203:
Herbert Xudce907c2005-06-22 13:27:51 -07001184 test_cipher_speed("blowfish", MODE_ECB, ENCRYPT, sec, NULL, 0,
1185 blowfish_speed_template);
1186 test_cipher_speed("blowfish", MODE_ECB, DECRYPT, sec, NULL, 0,
1187 blowfish_speed_template);
1188 test_cipher_speed("blowfish", MODE_CBC, ENCRYPT, sec, NULL, 0,
1189 blowfish_speed_template);
1190 test_cipher_speed("blowfish", MODE_CBC, DECRYPT, sec, NULL, 0,
1191 blowfish_speed_template);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001192 break;
1193
1194 case 204:
Herbert Xudce907c2005-06-22 13:27:51 -07001195 test_cipher_speed("des", MODE_ECB, ENCRYPT, sec, NULL, 0,
1196 des_speed_template);
1197 test_cipher_speed("des", MODE_ECB, DECRYPT, sec, NULL, 0,
1198 des_speed_template);
1199 test_cipher_speed("des", MODE_CBC, ENCRYPT, sec, NULL, 0,
1200 des_speed_template);
1201 test_cipher_speed("des", MODE_CBC, DECRYPT, sec, NULL, 0,
1202 des_speed_template);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001203 break;
1204
Michal Ludvige8057922006-05-30 22:04:19 +10001205 case 300:
1206 /* fall through */
1207
1208 case 301:
1209 test_digest_speed("md4", sec, generic_digest_speed_template);
1210 if (mode > 300 && mode < 400) break;
1211
1212 case 302:
1213 test_digest_speed("md5", sec, generic_digest_speed_template);
1214 if (mode > 300 && mode < 400) break;
1215
1216 case 303:
1217 test_digest_speed("sha1", sec, generic_digest_speed_template);
1218 if (mode > 300 && mode < 400) break;
1219
1220 case 304:
1221 test_digest_speed("sha256", sec, generic_digest_speed_template);
1222 if (mode > 300 && mode < 400) break;
1223
1224 case 305:
1225 test_digest_speed("sha384", sec, generic_digest_speed_template);
1226 if (mode > 300 && mode < 400) break;
1227
1228 case 306:
1229 test_digest_speed("sha512", sec, generic_digest_speed_template);
1230 if (mode > 300 && mode < 400) break;
1231
1232 case 307:
1233 test_digest_speed("wp256", sec, generic_digest_speed_template);
1234 if (mode > 300 && mode < 400) break;
1235
1236 case 308:
1237 test_digest_speed("wp384", sec, generic_digest_speed_template);
1238 if (mode > 300 && mode < 400) break;
1239
1240 case 309:
1241 test_digest_speed("wp512", sec, generic_digest_speed_template);
1242 if (mode > 300 && mode < 400) break;
1243
1244 case 310:
1245 test_digest_speed("tgr128", sec, generic_digest_speed_template);
1246 if (mode > 300 && mode < 400) break;
1247
1248 case 311:
1249 test_digest_speed("tgr160", sec, generic_digest_speed_template);
1250 if (mode > 300 && mode < 400) break;
1251
1252 case 312:
1253 test_digest_speed("tgr192", sec, generic_digest_speed_template);
1254 if (mode > 300 && mode < 400) break;
1255
1256 case 399:
1257 break;
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 case 1000:
1260 test_available();
1261 break;
Herbert Xuef2736f2005-06-22 13:26:03 -07001262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 default:
1264 /* useful for debugging */
1265 printk("not testing anything\n");
1266 break;
1267 }
1268}
1269
Herbert Xuef2736f2005-06-22 13:26:03 -07001270static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
1272 tvmem = kmalloc(TVMEMSIZE, GFP_KERNEL);
1273 if (tvmem == NULL)
1274 return -ENOMEM;
1275
1276 xbuf = kmalloc(XBUFSIZE, GFP_KERNEL);
1277 if (xbuf == NULL) {
1278 kfree(tvmem);
1279 return -ENOMEM;
1280 }
1281
1282 do_test();
1283
1284 kfree(xbuf);
1285 kfree(tvmem);
Michal Ludvig14fdf472006-05-30 14:49:38 +10001286
1287 /* We intentionaly return -EAGAIN to prevent keeping
1288 * the module. It does all its work from init()
1289 * and doesn't offer any runtime functionality
1290 * => we don't need it in the memory, do we?
1291 * -- mludvig
1292 */
1293 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
1296/*
1297 * If an init function is provided, an exit function must also be provided
1298 * to allow module unload.
1299 */
1300static void __exit fini(void) { }
1301
1302module_init(init);
1303module_exit(fini);
1304
1305module_param(mode, int, 0);
Harald Welteebfd9bc2005-06-22 13:27:23 -07001306module_param(sec, uint, 0);
Herbert Xu6a179442005-06-22 13:29:03 -07001307MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
1308 "(defaults to zero which uses CPU cycles instead)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310MODULE_LICENSE("GPL");
1311MODULE_DESCRIPTION("Quick & dirty crypto testing module");
1312MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");