tcrypt.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  1. /*
  2. * 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
  12. * Software Foundation; either version 2 of the License, or (at your option)
  13. * any later version.
  14. *
  15. * 2007-11-06 Added SHA-224 and SHA-224-HMAC tests
  16. * 2006-12-07 Added SHA384 HMAC and SHA512 HMAC tests
  17. * 2004-08-09 Added cipher speed tests (Reyk Floeter <reyk@vantronix.net>)
  18. * 2003-09-14 Rewritten by Kartikey Mahendra Bhatt
  19. *
  20. */
  21. #include <linux/err.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/mm.h>
  25. #include <linux/slab.h>
  26. #include <linux/scatterlist.h>
  27. #include <linux/string.h>
  28. #include <linux/crypto.h>
  29. #include <linux/highmem.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/jiffies.h>
  32. #include <linux/timex.h>
  33. #include <linux/interrupt.h>
  34. #include "tcrypt.h"
  35. /*
  36. * Need to kmalloc() memory for testing kmap().
  37. */
  38. #define TVMEMSIZE 16384
  39. #define XBUFSIZE 32768
  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. * Used by test_cipher()
  53. */
  54. #define ENCRYPT 1
  55. #define DECRYPT 0
  56. struct tcrypt_result {
  57. struct completion completion;
  58. int err;
  59. };
  60. static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
  61. /*
  62. * Used by test_cipher_speed()
  63. */
  64. static unsigned int sec;
  65. static int mode;
  66. static char *xbuf;
  67. static char *tvmem;
  68. static char *check[] = {
  69. "des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
  70. "blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
  71. "cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
  72. "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
  73. "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", "fcrypt",
  74. "camellia", "seed", "salsa20", NULL
  75. };
  76. static void hexdump(unsigned char *buf, unsigned int len)
  77. {
  78. while (len--)
  79. printk("%02x", *buf++);
  80. printk("\n");
  81. }
  82. static void tcrypt_complete(struct crypto_async_request *req, int err)
  83. {
  84. struct tcrypt_result *res = req->data;
  85. if (err == -EINPROGRESS)
  86. return;
  87. res->err = err;
  88. complete(&res->completion);
  89. }
  90. static void test_hash(char *algo, struct hash_testvec *template,
  91. unsigned int tcount)
  92. {
  93. unsigned int i, j, k, temp;
  94. struct scatterlist sg[8];
  95. char result[64];
  96. struct crypto_hash *tfm;
  97. struct hash_desc desc;
  98. struct hash_testvec *hash_tv;
  99. unsigned int tsize;
  100. int ret;
  101. printk("\ntesting %s\n", algo);
  102. tsize = sizeof(struct hash_testvec);
  103. tsize *= tcount;
  104. if (tsize > TVMEMSIZE) {
  105. printk("template (%u) too big for tvmem (%u)\n", tsize, TVMEMSIZE);
  106. return;
  107. }
  108. memcpy(tvmem, template, tsize);
  109. hash_tv = (void *)tvmem;
  110. tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
  111. if (IS_ERR(tfm)) {
  112. printk("failed to load transform for %s: %ld\n", algo,
  113. PTR_ERR(tfm));
  114. return;
  115. }
  116. desc.tfm = tfm;
  117. desc.flags = 0;
  118. for (i = 0; i < tcount; i++) {
  119. printk("test %u:\n", i + 1);
  120. memset(result, 0, 64);
  121. sg_init_one(&sg[0], hash_tv[i].plaintext, hash_tv[i].psize);
  122. if (hash_tv[i].ksize) {
  123. ret = crypto_hash_setkey(tfm, hash_tv[i].key,
  124. hash_tv[i].ksize);
  125. if (ret) {
  126. printk("setkey() failed ret=%d\n", ret);
  127. goto out;
  128. }
  129. }
  130. ret = crypto_hash_digest(&desc, sg, hash_tv[i].psize, result);
  131. if (ret) {
  132. printk("digest () failed ret=%d\n", ret);
  133. goto out;
  134. }
  135. hexdump(result, crypto_hash_digestsize(tfm));
  136. printk("%s\n",
  137. memcmp(result, hash_tv[i].digest,
  138. crypto_hash_digestsize(tfm)) ?
  139. "fail" : "pass");
  140. }
  141. printk("testing %s across pages\n", algo);
  142. /* setup the dummy buffer first */
  143. memset(xbuf, 0, XBUFSIZE);
  144. j = 0;
  145. for (i = 0; i < tcount; i++) {
  146. if (hash_tv[i].np) {
  147. j++;
  148. printk("test %u:\n", j);
  149. memset(result, 0, 64);
  150. temp = 0;
  151. sg_init_table(sg, hash_tv[i].np);
  152. for (k = 0; k < hash_tv[i].np; k++) {
  153. memcpy(&xbuf[IDX[k]],
  154. hash_tv[i].plaintext + temp,
  155. hash_tv[i].tap[k]);
  156. temp += hash_tv[i].tap[k];
  157. sg_set_buf(&sg[k], &xbuf[IDX[k]],
  158. hash_tv[i].tap[k]);
  159. }
  160. if (hash_tv[i].ksize) {
  161. ret = crypto_hash_setkey(tfm, hash_tv[i].key,
  162. hash_tv[i].ksize);
  163. if (ret) {
  164. printk("setkey() failed ret=%d\n", ret);
  165. goto out;
  166. }
  167. }
  168. ret = crypto_hash_digest(&desc, sg, hash_tv[i].psize,
  169. result);
  170. if (ret) {
  171. printk("digest () failed ret=%d\n", ret);
  172. goto out;
  173. }
  174. hexdump(result, crypto_hash_digestsize(tfm));
  175. printk("%s\n",
  176. memcmp(result, hash_tv[i].digest,
  177. crypto_hash_digestsize(tfm)) ?
  178. "fail" : "pass");
  179. }
  180. }
  181. out:
  182. crypto_free_hash(tfm);
  183. }
  184. static void test_cipher(char *algo, int enc,
  185. struct cipher_testvec *template, unsigned int tcount)
  186. {
  187. unsigned int ret, i, j, k, temp;
  188. unsigned int tsize;
  189. char *q;
  190. struct crypto_ablkcipher *tfm;
  191. char *key;
  192. struct cipher_testvec *cipher_tv;
  193. struct ablkcipher_request *req;
  194. struct scatterlist sg[8];
  195. const char *e;
  196. struct tcrypt_result result;
  197. if (enc == ENCRYPT)
  198. e = "encryption";
  199. else
  200. e = "decryption";
  201. printk("\ntesting %s %s\n", algo, e);
  202. tsize = sizeof (struct cipher_testvec);
  203. tsize *= tcount;
  204. if (tsize > TVMEMSIZE) {
  205. printk("template (%u) too big for tvmem (%u)\n", tsize,
  206. TVMEMSIZE);
  207. return;
  208. }
  209. memcpy(tvmem, template, tsize);
  210. cipher_tv = (void *)tvmem;
  211. init_completion(&result.completion);
  212. tfm = crypto_alloc_ablkcipher(algo, 0, 0);
  213. if (IS_ERR(tfm)) {
  214. printk("failed to load transform for %s: %ld\n", algo,
  215. PTR_ERR(tfm));
  216. return;
  217. }
  218. req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
  219. if (!req) {
  220. printk("failed to allocate request for %s\n", algo);
  221. goto out;
  222. }
  223. ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  224. tcrypt_complete, &result);
  225. j = 0;
  226. for (i = 0; i < tcount; i++) {
  227. if (!(cipher_tv[i].np)) {
  228. j++;
  229. printk("test %u (%d bit key):\n",
  230. j, cipher_tv[i].klen * 8);
  231. crypto_ablkcipher_clear_flags(tfm, ~0);
  232. if (cipher_tv[i].wk)
  233. crypto_ablkcipher_set_flags(
  234. tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  235. key = cipher_tv[i].key;
  236. ret = crypto_ablkcipher_setkey(tfm, key,
  237. cipher_tv[i].klen);
  238. if (ret) {
  239. printk("setkey() failed flags=%x\n",
  240. crypto_ablkcipher_get_flags(tfm));
  241. if (!cipher_tv[i].fail)
  242. goto out;
  243. }
  244. sg_init_one(&sg[0], cipher_tv[i].input,
  245. cipher_tv[i].ilen);
  246. ablkcipher_request_set_crypt(req, sg, sg,
  247. cipher_tv[i].ilen,
  248. cipher_tv[i].iv);
  249. ret = enc ?
  250. crypto_ablkcipher_encrypt(req) :
  251. crypto_ablkcipher_decrypt(req);
  252. switch (ret) {
  253. case 0:
  254. break;
  255. case -EINPROGRESS:
  256. case -EBUSY:
  257. ret = wait_for_completion_interruptible(
  258. &result.completion);
  259. if (!ret && !((ret = result.err))) {
  260. INIT_COMPLETION(result.completion);
  261. break;
  262. }
  263. /* fall through */
  264. default:
  265. printk("%s () failed err=%d\n", e, -ret);
  266. goto out;
  267. }
  268. q = kmap(sg_page(&sg[0])) + sg[0].offset;
  269. hexdump(q, cipher_tv[i].rlen);
  270. printk("%s\n",
  271. memcmp(q, cipher_tv[i].result,
  272. cipher_tv[i].rlen) ? "fail" : "pass");
  273. }
  274. }
  275. printk("\ntesting %s %s across pages (chunking)\n", algo, e);
  276. memset(xbuf, 0, XBUFSIZE);
  277. j = 0;
  278. for (i = 0; i < tcount; i++) {
  279. if (cipher_tv[i].np) {
  280. j++;
  281. printk("test %u (%d bit key):\n",
  282. j, cipher_tv[i].klen * 8);
  283. crypto_ablkcipher_clear_flags(tfm, ~0);
  284. if (cipher_tv[i].wk)
  285. crypto_ablkcipher_set_flags(
  286. tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  287. key = cipher_tv[i].key;
  288. ret = crypto_ablkcipher_setkey(tfm, key,
  289. cipher_tv[i].klen);
  290. if (ret) {
  291. printk("setkey() failed flags=%x\n",
  292. crypto_ablkcipher_get_flags(tfm));
  293. if (!cipher_tv[i].fail)
  294. goto out;
  295. }
  296. temp = 0;
  297. sg_init_table(sg, cipher_tv[i].np);
  298. for (k = 0; k < cipher_tv[i].np; k++) {
  299. memcpy(&xbuf[IDX[k]],
  300. cipher_tv[i].input + temp,
  301. cipher_tv[i].tap[k]);
  302. temp += cipher_tv[i].tap[k];
  303. sg_set_buf(&sg[k], &xbuf[IDX[k]],
  304. cipher_tv[i].tap[k]);
  305. }
  306. ablkcipher_request_set_crypt(req, sg, sg,
  307. cipher_tv[i].ilen,
  308. cipher_tv[i].iv);
  309. ret = enc ?
  310. crypto_ablkcipher_encrypt(req) :
  311. crypto_ablkcipher_decrypt(req);
  312. switch (ret) {
  313. case 0:
  314. break;
  315. case -EINPROGRESS:
  316. case -EBUSY:
  317. ret = wait_for_completion_interruptible(
  318. &result.completion);
  319. if (!ret && !((ret = result.err))) {
  320. INIT_COMPLETION(result.completion);
  321. break;
  322. }
  323. /* fall through */
  324. default:
  325. printk("%s () failed err=%d\n", e, -ret);
  326. goto out;
  327. }
  328. temp = 0;
  329. for (k = 0; k < cipher_tv[i].np; k++) {
  330. printk("page %u\n", k);
  331. q = kmap(sg_page(&sg[k])) + sg[k].offset;
  332. hexdump(q, cipher_tv[i].tap[k]);
  333. printk("%s\n",
  334. memcmp(q, cipher_tv[i].result + temp,
  335. cipher_tv[i].tap[k]) ? "fail" :
  336. "pass");
  337. temp += cipher_tv[i].tap[k];
  338. }
  339. }
  340. }
  341. out:
  342. crypto_free_ablkcipher(tfm);
  343. ablkcipher_request_free(req);
  344. }
  345. static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc, char *p,
  346. int blen, int sec)
  347. {
  348. struct scatterlist sg[1];
  349. unsigned long start, end;
  350. int bcount;
  351. int ret;
  352. sg_init_one(sg, p, blen);
  353. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  354. time_before(jiffies, end); bcount++) {
  355. if (enc)
  356. ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
  357. else
  358. ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
  359. if (ret)
  360. return ret;
  361. }
  362. printk("%d operations in %d seconds (%ld bytes)\n",
  363. bcount, sec, (long)bcount * blen);
  364. return 0;
  365. }
  366. static int test_cipher_cycles(struct blkcipher_desc *desc, int enc, char *p,
  367. int blen)
  368. {
  369. struct scatterlist sg[1];
  370. unsigned long cycles = 0;
  371. int ret = 0;
  372. int i;
  373. sg_init_one(sg, p, blen);
  374. local_bh_disable();
  375. local_irq_disable();
  376. /* Warm-up run. */
  377. for (i = 0; i < 4; i++) {
  378. if (enc)
  379. ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
  380. else
  381. ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
  382. if (ret)
  383. goto out;
  384. }
  385. /* The real thing. */
  386. for (i = 0; i < 8; i++) {
  387. cycles_t start, end;
  388. start = get_cycles();
  389. if (enc)
  390. ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
  391. else
  392. ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
  393. end = get_cycles();
  394. if (ret)
  395. goto out;
  396. cycles += end - start;
  397. }
  398. out:
  399. local_irq_enable();
  400. local_bh_enable();
  401. if (ret == 0)
  402. printk("1 operation in %lu cycles (%d bytes)\n",
  403. (cycles + 4) / 8, blen);
  404. return ret;
  405. }
  406. static void test_cipher_speed(char *algo, int enc, unsigned int sec,
  407. struct cipher_testvec *template,
  408. unsigned int tcount, struct cipher_speed *speed)
  409. {
  410. unsigned int ret, i, j, iv_len;
  411. unsigned char *key, *p, iv[128];
  412. struct crypto_blkcipher *tfm;
  413. struct blkcipher_desc desc;
  414. const char *e;
  415. if (enc == ENCRYPT)
  416. e = "encryption";
  417. else
  418. e = "decryption";
  419. printk("\ntesting speed of %s %s\n", algo, e);
  420. tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
  421. if (IS_ERR(tfm)) {
  422. printk("failed to load transform for %s: %ld\n", algo,
  423. PTR_ERR(tfm));
  424. return;
  425. }
  426. desc.tfm = tfm;
  427. desc.flags = 0;
  428. for (i = 0; speed[i].klen != 0; i++) {
  429. if ((speed[i].blen + speed[i].klen) > TVMEMSIZE) {
  430. printk("template (%u) too big for tvmem (%u)\n",
  431. speed[i].blen + speed[i].klen, TVMEMSIZE);
  432. goto out;
  433. }
  434. printk("test %u (%d bit key, %d byte blocks): ", i,
  435. speed[i].klen * 8, speed[i].blen);
  436. memset(tvmem, 0xff, speed[i].klen + speed[i].blen);
  437. /* set key, plain text and IV */
  438. key = (unsigned char *)tvmem;
  439. for (j = 0; j < tcount; j++) {
  440. if (template[j].klen == speed[i].klen) {
  441. key = template[j].key;
  442. break;
  443. }
  444. }
  445. p = (unsigned char *)tvmem + speed[i].klen;
  446. ret = crypto_blkcipher_setkey(tfm, key, speed[i].klen);
  447. if (ret) {
  448. printk("setkey() failed flags=%x\n",
  449. crypto_blkcipher_get_flags(tfm));
  450. goto out;
  451. }
  452. iv_len = crypto_blkcipher_ivsize(tfm);
  453. if (iv_len) {
  454. memset(&iv, 0xff, iv_len);
  455. crypto_blkcipher_set_iv(tfm, iv, iv_len);
  456. }
  457. if (sec)
  458. ret = test_cipher_jiffies(&desc, enc, p, speed[i].blen,
  459. sec);
  460. else
  461. ret = test_cipher_cycles(&desc, enc, p, speed[i].blen);
  462. if (ret) {
  463. printk("%s() failed flags=%x\n", e, desc.flags);
  464. break;
  465. }
  466. }
  467. out:
  468. crypto_free_blkcipher(tfm);
  469. }
  470. static int test_hash_jiffies_digest(struct hash_desc *desc, char *p, int blen,
  471. char *out, int sec)
  472. {
  473. struct scatterlist sg[1];
  474. unsigned long start, end;
  475. int bcount;
  476. int ret;
  477. sg_init_table(sg, 1);
  478. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  479. time_before(jiffies, end); bcount++) {
  480. sg_set_buf(sg, p, blen);
  481. ret = crypto_hash_digest(desc, sg, blen, out);
  482. if (ret)
  483. return ret;
  484. }
  485. printk("%6u opers/sec, %9lu bytes/sec\n",
  486. bcount / sec, ((long)bcount * blen) / sec);
  487. return 0;
  488. }
  489. static int test_hash_jiffies(struct hash_desc *desc, char *p, int blen,
  490. int plen, char *out, int sec)
  491. {
  492. struct scatterlist sg[1];
  493. unsigned long start, end;
  494. int bcount, pcount;
  495. int ret;
  496. if (plen == blen)
  497. return test_hash_jiffies_digest(desc, p, blen, out, sec);
  498. sg_init_table(sg, 1);
  499. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  500. time_before(jiffies, end); bcount++) {
  501. ret = crypto_hash_init(desc);
  502. if (ret)
  503. return ret;
  504. for (pcount = 0; pcount < blen; pcount += plen) {
  505. sg_set_buf(sg, p + pcount, plen);
  506. ret = crypto_hash_update(desc, sg, plen);
  507. if (ret)
  508. return ret;
  509. }
  510. /* we assume there is enough space in 'out' for the result */
  511. ret = crypto_hash_final(desc, out);
  512. if (ret)
  513. return ret;
  514. }
  515. printk("%6u opers/sec, %9lu bytes/sec\n",
  516. bcount / sec, ((long)bcount * blen) / sec);
  517. return 0;
  518. }
  519. static int test_hash_cycles_digest(struct hash_desc *desc, char *p, int blen,
  520. char *out)
  521. {
  522. struct scatterlist sg[1];
  523. unsigned long cycles = 0;
  524. int i;
  525. int ret;
  526. sg_init_table(sg, 1);
  527. local_bh_disable();
  528. local_irq_disable();
  529. /* Warm-up run. */
  530. for (i = 0; i < 4; i++) {
  531. sg_set_buf(sg, p, blen);
  532. ret = crypto_hash_digest(desc, sg, blen, out);
  533. if (ret)
  534. goto out;
  535. }
  536. /* The real thing. */
  537. for (i = 0; i < 8; i++) {
  538. cycles_t start, end;
  539. start = get_cycles();
  540. sg_set_buf(sg, p, blen);
  541. ret = crypto_hash_digest(desc, sg, blen, out);
  542. if (ret)
  543. goto out;
  544. end = get_cycles();
  545. cycles += end - start;
  546. }
  547. out:
  548. local_irq_enable();
  549. local_bh_enable();
  550. if (ret)
  551. return ret;
  552. printk("%6lu cycles/operation, %4lu cycles/byte\n",
  553. cycles / 8, cycles / (8 * blen));
  554. return 0;
  555. }
  556. static int test_hash_cycles(struct hash_desc *desc, char *p, int blen,
  557. int plen, char *out)
  558. {
  559. struct scatterlist sg[1];
  560. unsigned long cycles = 0;
  561. int i, pcount;
  562. int ret;
  563. if (plen == blen)
  564. return test_hash_cycles_digest(desc, p, blen, out);
  565. sg_init_table(sg, 1);
  566. local_bh_disable();
  567. local_irq_disable();
  568. /* Warm-up run. */
  569. for (i = 0; i < 4; i++) {
  570. ret = crypto_hash_init(desc);
  571. if (ret)
  572. goto out;
  573. for (pcount = 0; pcount < blen; pcount += plen) {
  574. sg_set_buf(sg, p + pcount, plen);
  575. ret = crypto_hash_update(desc, sg, plen);
  576. if (ret)
  577. goto out;
  578. }
  579. ret = crypto_hash_final(desc, out);
  580. if (ret)
  581. goto out;
  582. }
  583. /* The real thing. */
  584. for (i = 0; i < 8; i++) {
  585. cycles_t start, end;
  586. start = get_cycles();
  587. ret = crypto_hash_init(desc);
  588. if (ret)
  589. goto out;
  590. for (pcount = 0; pcount < blen; pcount += plen) {
  591. sg_set_buf(sg, p + pcount, plen);
  592. ret = crypto_hash_update(desc, sg, plen);
  593. if (ret)
  594. goto out;
  595. }
  596. ret = crypto_hash_final(desc, out);
  597. if (ret)
  598. goto out;
  599. end = get_cycles();
  600. cycles += end - start;
  601. }
  602. out:
  603. local_irq_enable();
  604. local_bh_enable();
  605. if (ret)
  606. return ret;
  607. printk("%6lu cycles/operation, %4lu cycles/byte\n",
  608. cycles / 8, cycles / (8 * blen));
  609. return 0;
  610. }
  611. static void test_hash_speed(char *algo, unsigned int sec,
  612. struct hash_speed *speed)
  613. {
  614. struct crypto_hash *tfm;
  615. struct hash_desc desc;
  616. char output[1024];
  617. int i;
  618. int ret;
  619. printk("\ntesting speed of %s\n", algo);
  620. tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
  621. if (IS_ERR(tfm)) {
  622. printk("failed to load transform for %s: %ld\n", algo,
  623. PTR_ERR(tfm));
  624. return;
  625. }
  626. desc.tfm = tfm;
  627. desc.flags = 0;
  628. if (crypto_hash_digestsize(tfm) > sizeof(output)) {
  629. printk("digestsize(%u) > outputbuffer(%zu)\n",
  630. crypto_hash_digestsize(tfm), sizeof(output));
  631. goto out;
  632. }
  633. for (i = 0; speed[i].blen != 0; i++) {
  634. if (speed[i].blen > TVMEMSIZE) {
  635. printk("template (%u) too big for tvmem (%u)\n",
  636. speed[i].blen, TVMEMSIZE);
  637. goto out;
  638. }
  639. printk("test%3u (%5u byte blocks,%5u bytes per update,%4u updates): ",
  640. i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
  641. memset(tvmem, 0xff, speed[i].blen);
  642. if (sec)
  643. ret = test_hash_jiffies(&desc, tvmem, speed[i].blen,
  644. speed[i].plen, output, sec);
  645. else
  646. ret = test_hash_cycles(&desc, tvmem, speed[i].blen,
  647. speed[i].plen, output);
  648. if (ret) {
  649. printk("hashing failed ret=%d\n", ret);
  650. break;
  651. }
  652. }
  653. out:
  654. crypto_free_hash(tfm);
  655. }
  656. static void test_deflate(void)
  657. {
  658. unsigned int i;
  659. char result[COMP_BUF_SIZE];
  660. struct crypto_comp *tfm;
  661. struct comp_testvec *tv;
  662. unsigned int tsize;
  663. printk("\ntesting deflate compression\n");
  664. tsize = sizeof (deflate_comp_tv_template);
  665. if (tsize > TVMEMSIZE) {
  666. printk("template (%u) too big for tvmem (%u)\n", tsize,
  667. TVMEMSIZE);
  668. return;
  669. }
  670. memcpy(tvmem, deflate_comp_tv_template, tsize);
  671. tv = (void *)tvmem;
  672. tfm = crypto_alloc_comp("deflate", 0, CRYPTO_ALG_ASYNC);
  673. if (IS_ERR(tfm)) {
  674. printk("failed to load transform for deflate\n");
  675. return;
  676. }
  677. for (i = 0; i < DEFLATE_COMP_TEST_VECTORS; i++) {
  678. int ilen, ret, dlen = COMP_BUF_SIZE;
  679. printk("test %u:\n", i + 1);
  680. memset(result, 0, sizeof (result));
  681. ilen = tv[i].inlen;
  682. ret = crypto_comp_compress(tfm, tv[i].input,
  683. ilen, result, &dlen);
  684. if (ret) {
  685. printk("fail: ret=%d\n", ret);
  686. continue;
  687. }
  688. hexdump(result, dlen);
  689. printk("%s (ratio %d:%d)\n",
  690. memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
  691. ilen, dlen);
  692. }
  693. printk("\ntesting deflate decompression\n");
  694. tsize = sizeof (deflate_decomp_tv_template);
  695. if (tsize > TVMEMSIZE) {
  696. printk("template (%u) too big for tvmem (%u)\n", tsize,
  697. TVMEMSIZE);
  698. goto out;
  699. }
  700. memcpy(tvmem, deflate_decomp_tv_template, tsize);
  701. tv = (void *)tvmem;
  702. for (i = 0; i < DEFLATE_DECOMP_TEST_VECTORS; i++) {
  703. int ilen, ret, dlen = COMP_BUF_SIZE;
  704. printk("test %u:\n", i + 1);
  705. memset(result, 0, sizeof (result));
  706. ilen = tv[i].inlen;
  707. ret = crypto_comp_decompress(tfm, tv[i].input,
  708. ilen, result, &dlen);
  709. if (ret) {
  710. printk("fail: ret=%d\n", ret);
  711. continue;
  712. }
  713. hexdump(result, dlen);
  714. printk("%s (ratio %d:%d)\n",
  715. memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
  716. ilen, dlen);
  717. }
  718. out:
  719. crypto_free_comp(tfm);
  720. }
  721. static void test_available(void)
  722. {
  723. char **name = check;
  724. while (*name) {
  725. printk("alg %s ", *name);
  726. printk(crypto_has_alg(*name, 0, 0) ?
  727. "found\n" : "not found\n");
  728. name++;
  729. }
  730. }
  731. static void do_test(void)
  732. {
  733. switch (mode) {
  734. case 0:
  735. test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
  736. test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
  737. //DES
  738. test_cipher("ecb(des)", ENCRYPT, des_enc_tv_template,
  739. DES_ENC_TEST_VECTORS);
  740. test_cipher("ecb(des)", DECRYPT, des_dec_tv_template,
  741. DES_DEC_TEST_VECTORS);
  742. test_cipher("cbc(des)", ENCRYPT, des_cbc_enc_tv_template,
  743. DES_CBC_ENC_TEST_VECTORS);
  744. test_cipher("cbc(des)", DECRYPT, des_cbc_dec_tv_template,
  745. DES_CBC_DEC_TEST_VECTORS);
  746. //DES3_EDE
  747. test_cipher("ecb(des3_ede)", ENCRYPT, des3_ede_enc_tv_template,
  748. DES3_EDE_ENC_TEST_VECTORS);
  749. test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
  750. DES3_EDE_DEC_TEST_VECTORS);
  751. test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
  752. test_hash("sha224", sha224_tv_template, SHA224_TEST_VECTORS);
  753. test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
  754. //BLOWFISH
  755. test_cipher("ecb(blowfish)", ENCRYPT, bf_enc_tv_template,
  756. BF_ENC_TEST_VECTORS);
  757. test_cipher("ecb(blowfish)", DECRYPT, bf_dec_tv_template,
  758. BF_DEC_TEST_VECTORS);
  759. test_cipher("cbc(blowfish)", ENCRYPT, bf_cbc_enc_tv_template,
  760. BF_CBC_ENC_TEST_VECTORS);
  761. test_cipher("cbc(blowfish)", DECRYPT, bf_cbc_dec_tv_template,
  762. BF_CBC_DEC_TEST_VECTORS);
  763. //TWOFISH
  764. test_cipher("ecb(twofish)", ENCRYPT, tf_enc_tv_template,
  765. TF_ENC_TEST_VECTORS);
  766. test_cipher("ecb(twofish)", DECRYPT, tf_dec_tv_template,
  767. TF_DEC_TEST_VECTORS);
  768. test_cipher("cbc(twofish)", ENCRYPT, tf_cbc_enc_tv_template,
  769. TF_CBC_ENC_TEST_VECTORS);
  770. test_cipher("cbc(twofish)", DECRYPT, tf_cbc_dec_tv_template,
  771. TF_CBC_DEC_TEST_VECTORS);
  772. //SERPENT
  773. test_cipher("ecb(serpent)", ENCRYPT, serpent_enc_tv_template,
  774. SERPENT_ENC_TEST_VECTORS);
  775. test_cipher("ecb(serpent)", DECRYPT, serpent_dec_tv_template,
  776. SERPENT_DEC_TEST_VECTORS);
  777. //TNEPRES
  778. test_cipher("ecb(tnepres)", ENCRYPT, tnepres_enc_tv_template,
  779. TNEPRES_ENC_TEST_VECTORS);
  780. test_cipher("ecb(tnepres)", DECRYPT, tnepres_dec_tv_template,
  781. TNEPRES_DEC_TEST_VECTORS);
  782. //AES
  783. test_cipher("ecb(aes)", ENCRYPT, aes_enc_tv_template,
  784. AES_ENC_TEST_VECTORS);
  785. test_cipher("ecb(aes)", DECRYPT, aes_dec_tv_template,
  786. AES_DEC_TEST_VECTORS);
  787. test_cipher("cbc(aes)", ENCRYPT, aes_cbc_enc_tv_template,
  788. AES_CBC_ENC_TEST_VECTORS);
  789. test_cipher("cbc(aes)", DECRYPT, aes_cbc_dec_tv_template,
  790. AES_CBC_DEC_TEST_VECTORS);
  791. test_cipher("lrw(aes)", ENCRYPT, aes_lrw_enc_tv_template,
  792. AES_LRW_ENC_TEST_VECTORS);
  793. test_cipher("lrw(aes)", DECRYPT, aes_lrw_dec_tv_template,
  794. AES_LRW_DEC_TEST_VECTORS);
  795. test_cipher("xts(aes)", ENCRYPT, aes_xts_enc_tv_template,
  796. AES_XTS_ENC_TEST_VECTORS);
  797. test_cipher("xts(aes)", DECRYPT, aes_xts_dec_tv_template,
  798. AES_XTS_DEC_TEST_VECTORS);
  799. test_cipher("ctr(aes,4,8,4)", ENCRYPT, aes_ctr_enc_tv_template,
  800. AES_CTR_ENC_TEST_VECTORS);
  801. test_cipher("ctr(aes,4,8,4)", DECRYPT, aes_ctr_dec_tv_template,
  802. AES_CTR_DEC_TEST_VECTORS);
  803. //CAST5
  804. test_cipher("ecb(cast5)", ENCRYPT, cast5_enc_tv_template,
  805. CAST5_ENC_TEST_VECTORS);
  806. test_cipher("ecb(cast5)", DECRYPT, cast5_dec_tv_template,
  807. CAST5_DEC_TEST_VECTORS);
  808. //CAST6
  809. test_cipher("ecb(cast6)", ENCRYPT, cast6_enc_tv_template,
  810. CAST6_ENC_TEST_VECTORS);
  811. test_cipher("ecb(cast6)", DECRYPT, cast6_dec_tv_template,
  812. CAST6_DEC_TEST_VECTORS);
  813. //ARC4
  814. test_cipher("ecb(arc4)", ENCRYPT, arc4_enc_tv_template,
  815. ARC4_ENC_TEST_VECTORS);
  816. test_cipher("ecb(arc4)", DECRYPT, arc4_dec_tv_template,
  817. ARC4_DEC_TEST_VECTORS);
  818. //TEA
  819. test_cipher("ecb(tea)", ENCRYPT, tea_enc_tv_template,
  820. TEA_ENC_TEST_VECTORS);
  821. test_cipher("ecb(tea)", DECRYPT, tea_dec_tv_template,
  822. TEA_DEC_TEST_VECTORS);
  823. //XTEA
  824. test_cipher("ecb(xtea)", ENCRYPT, xtea_enc_tv_template,
  825. XTEA_ENC_TEST_VECTORS);
  826. test_cipher("ecb(xtea)", DECRYPT, xtea_dec_tv_template,
  827. XTEA_DEC_TEST_VECTORS);
  828. //KHAZAD
  829. test_cipher("ecb(khazad)", ENCRYPT, khazad_enc_tv_template,
  830. KHAZAD_ENC_TEST_VECTORS);
  831. test_cipher("ecb(khazad)", DECRYPT, khazad_dec_tv_template,
  832. KHAZAD_DEC_TEST_VECTORS);
  833. //ANUBIS
  834. test_cipher("ecb(anubis)", ENCRYPT, anubis_enc_tv_template,
  835. ANUBIS_ENC_TEST_VECTORS);
  836. test_cipher("ecb(anubis)", DECRYPT, anubis_dec_tv_template,
  837. ANUBIS_DEC_TEST_VECTORS);
  838. test_cipher("cbc(anubis)", ENCRYPT, anubis_cbc_enc_tv_template,
  839. ANUBIS_CBC_ENC_TEST_VECTORS);
  840. test_cipher("cbc(anubis)", DECRYPT, anubis_cbc_dec_tv_template,
  841. ANUBIS_CBC_ENC_TEST_VECTORS);
  842. //XETA
  843. test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
  844. XETA_ENC_TEST_VECTORS);
  845. test_cipher("ecb(xeta)", DECRYPT, xeta_dec_tv_template,
  846. XETA_DEC_TEST_VECTORS);
  847. //FCrypt
  848. test_cipher("pcbc(fcrypt)", ENCRYPT, fcrypt_pcbc_enc_tv_template,
  849. FCRYPT_ENC_TEST_VECTORS);
  850. test_cipher("pcbc(fcrypt)", DECRYPT, fcrypt_pcbc_dec_tv_template,
  851. FCRYPT_DEC_TEST_VECTORS);
  852. //CAMELLIA
  853. test_cipher("ecb(camellia)", ENCRYPT,
  854. camellia_enc_tv_template,
  855. CAMELLIA_ENC_TEST_VECTORS);
  856. test_cipher("ecb(camellia)", DECRYPT,
  857. camellia_dec_tv_template,
  858. CAMELLIA_DEC_TEST_VECTORS);
  859. test_cipher("cbc(camellia)", ENCRYPT,
  860. camellia_cbc_enc_tv_template,
  861. CAMELLIA_CBC_ENC_TEST_VECTORS);
  862. test_cipher("cbc(camellia)", DECRYPT,
  863. camellia_cbc_dec_tv_template,
  864. CAMELLIA_CBC_DEC_TEST_VECTORS);
  865. //SEED
  866. test_cipher("ecb(seed)", ENCRYPT, seed_enc_tv_template,
  867. SEED_ENC_TEST_VECTORS);
  868. test_cipher("ecb(seed)", DECRYPT, seed_dec_tv_template,
  869. SEED_DEC_TEST_VECTORS);
  870. test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
  871. test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
  872. test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
  873. test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
  874. test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
  875. test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
  876. test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
  877. test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
  878. test_deflate();
  879. test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
  880. test_hash("hmac(md5)", hmac_md5_tv_template,
  881. HMAC_MD5_TEST_VECTORS);
  882. test_hash("hmac(sha1)", hmac_sha1_tv_template,
  883. HMAC_SHA1_TEST_VECTORS);
  884. test_hash("hmac(sha224)", hmac_sha224_tv_template,
  885. HMAC_SHA224_TEST_VECTORS);
  886. test_hash("hmac(sha256)", hmac_sha256_tv_template,
  887. HMAC_SHA256_TEST_VECTORS);
  888. test_hash("hmac(sha384)", hmac_sha384_tv_template,
  889. HMAC_SHA384_TEST_VECTORS);
  890. test_hash("hmac(sha512)", hmac_sha512_tv_template,
  891. HMAC_SHA512_TEST_VECTORS);
  892. test_hash("xcbc(aes)", aes_xcbc128_tv_template,
  893. XCBC_AES_TEST_VECTORS);
  894. test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
  895. break;
  896. case 1:
  897. test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
  898. break;
  899. case 2:
  900. test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
  901. break;
  902. case 3:
  903. test_cipher("ecb(des)", ENCRYPT, des_enc_tv_template,
  904. DES_ENC_TEST_VECTORS);
  905. test_cipher("ecb(des)", DECRYPT, des_dec_tv_template,
  906. DES_DEC_TEST_VECTORS);
  907. test_cipher("cbc(des)", ENCRYPT, des_cbc_enc_tv_template,
  908. DES_CBC_ENC_TEST_VECTORS);
  909. test_cipher("cbc(des)", DECRYPT, des_cbc_dec_tv_template,
  910. DES_CBC_DEC_TEST_VECTORS);
  911. break;
  912. case 4:
  913. test_cipher("ecb(des3_ede)", ENCRYPT, des3_ede_enc_tv_template,
  914. DES3_EDE_ENC_TEST_VECTORS);
  915. test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
  916. DES3_EDE_DEC_TEST_VECTORS);
  917. break;
  918. case 5:
  919. test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
  920. break;
  921. case 6:
  922. test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
  923. break;
  924. case 7:
  925. test_cipher("ecb(blowfish)", ENCRYPT, bf_enc_tv_template,
  926. BF_ENC_TEST_VECTORS);
  927. test_cipher("ecb(blowfish)", DECRYPT, bf_dec_tv_template,
  928. BF_DEC_TEST_VECTORS);
  929. test_cipher("cbc(blowfish)", ENCRYPT, bf_cbc_enc_tv_template,
  930. BF_CBC_ENC_TEST_VECTORS);
  931. test_cipher("cbc(blowfish)", DECRYPT, bf_cbc_dec_tv_template,
  932. BF_CBC_DEC_TEST_VECTORS);
  933. break;
  934. case 8:
  935. test_cipher("ecb(twofish)", ENCRYPT, tf_enc_tv_template,
  936. TF_ENC_TEST_VECTORS);
  937. test_cipher("ecb(twofish)", DECRYPT, tf_dec_tv_template,
  938. TF_DEC_TEST_VECTORS);
  939. test_cipher("cbc(twofish)", ENCRYPT, tf_cbc_enc_tv_template,
  940. TF_CBC_ENC_TEST_VECTORS);
  941. test_cipher("cbc(twofish)", DECRYPT, tf_cbc_dec_tv_template,
  942. TF_CBC_DEC_TEST_VECTORS);
  943. break;
  944. case 9:
  945. test_cipher("ecb(serpent)", ENCRYPT, serpent_enc_tv_template,
  946. SERPENT_ENC_TEST_VECTORS);
  947. test_cipher("ecb(serpent)", DECRYPT, serpent_dec_tv_template,
  948. SERPENT_DEC_TEST_VECTORS);
  949. break;
  950. case 10:
  951. test_cipher("ecb(aes)", ENCRYPT, aes_enc_tv_template,
  952. AES_ENC_TEST_VECTORS);
  953. test_cipher("ecb(aes)", DECRYPT, aes_dec_tv_template,
  954. AES_DEC_TEST_VECTORS);
  955. test_cipher("cbc(aes)", ENCRYPT, aes_cbc_enc_tv_template,
  956. AES_CBC_ENC_TEST_VECTORS);
  957. test_cipher("cbc(aes)", DECRYPT, aes_cbc_dec_tv_template,
  958. AES_CBC_DEC_TEST_VECTORS);
  959. test_cipher("lrw(aes)", ENCRYPT, aes_lrw_enc_tv_template,
  960. AES_LRW_ENC_TEST_VECTORS);
  961. test_cipher("lrw(aes)", DECRYPT, aes_lrw_dec_tv_template,
  962. AES_LRW_DEC_TEST_VECTORS);
  963. test_cipher("xts(aes)", ENCRYPT, aes_xts_enc_tv_template,
  964. AES_XTS_ENC_TEST_VECTORS);
  965. test_cipher("xts(aes)", DECRYPT, aes_xts_dec_tv_template,
  966. AES_XTS_DEC_TEST_VECTORS);
  967. test_cipher("ctr(aes,4,8,4)", ENCRYPT, aes_ctr_enc_tv_template,
  968. AES_CTR_ENC_TEST_VECTORS);
  969. test_cipher("ctr(aes,4,8,4)", DECRYPT, aes_ctr_dec_tv_template,
  970. AES_CTR_DEC_TEST_VECTORS);
  971. break;
  972. case 11:
  973. test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
  974. break;
  975. case 12:
  976. test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
  977. break;
  978. case 13:
  979. test_deflate();
  980. break;
  981. case 14:
  982. test_cipher("ecb(cast5)", ENCRYPT, cast5_enc_tv_template,
  983. CAST5_ENC_TEST_VECTORS);
  984. test_cipher("ecb(cast5)", DECRYPT, cast5_dec_tv_template,
  985. CAST5_DEC_TEST_VECTORS);
  986. break;
  987. case 15:
  988. test_cipher("ecb(cast6)", ENCRYPT, cast6_enc_tv_template,
  989. CAST6_ENC_TEST_VECTORS);
  990. test_cipher("ecb(cast6)", DECRYPT, cast6_dec_tv_template,
  991. CAST6_DEC_TEST_VECTORS);
  992. break;
  993. case 16:
  994. test_cipher("ecb(arc4)", ENCRYPT, arc4_enc_tv_template,
  995. ARC4_ENC_TEST_VECTORS);
  996. test_cipher("ecb(arc4)", DECRYPT, arc4_dec_tv_template,
  997. ARC4_DEC_TEST_VECTORS);
  998. break;
  999. case 17:
  1000. test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
  1001. break;
  1002. case 18:
  1003. test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
  1004. break;
  1005. case 19:
  1006. test_cipher("ecb(tea)", ENCRYPT, tea_enc_tv_template,
  1007. TEA_ENC_TEST_VECTORS);
  1008. test_cipher("ecb(tea)", DECRYPT, tea_dec_tv_template,
  1009. TEA_DEC_TEST_VECTORS);
  1010. break;
  1011. case 20:
  1012. test_cipher("ecb(xtea)", ENCRYPT, xtea_enc_tv_template,
  1013. XTEA_ENC_TEST_VECTORS);
  1014. test_cipher("ecb(xtea)", DECRYPT, xtea_dec_tv_template,
  1015. XTEA_DEC_TEST_VECTORS);
  1016. break;
  1017. case 21:
  1018. test_cipher("ecb(khazad)", ENCRYPT, khazad_enc_tv_template,
  1019. KHAZAD_ENC_TEST_VECTORS);
  1020. test_cipher("ecb(khazad)", DECRYPT, khazad_dec_tv_template,
  1021. KHAZAD_DEC_TEST_VECTORS);
  1022. break;
  1023. case 22:
  1024. test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
  1025. break;
  1026. case 23:
  1027. test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
  1028. break;
  1029. case 24:
  1030. test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
  1031. break;
  1032. case 25:
  1033. test_cipher("ecb(tnepres)", ENCRYPT, tnepres_enc_tv_template,
  1034. TNEPRES_ENC_TEST_VECTORS);
  1035. test_cipher("ecb(tnepres)", DECRYPT, tnepres_dec_tv_template,
  1036. TNEPRES_DEC_TEST_VECTORS);
  1037. break;
  1038. case 26:
  1039. test_cipher("ecb(anubis)", ENCRYPT, anubis_enc_tv_template,
  1040. ANUBIS_ENC_TEST_VECTORS);
  1041. test_cipher("ecb(anubis)", DECRYPT, anubis_dec_tv_template,
  1042. ANUBIS_DEC_TEST_VECTORS);
  1043. test_cipher("cbc(anubis)", ENCRYPT, anubis_cbc_enc_tv_template,
  1044. ANUBIS_CBC_ENC_TEST_VECTORS);
  1045. test_cipher("cbc(anubis)", DECRYPT, anubis_cbc_dec_tv_template,
  1046. ANUBIS_CBC_ENC_TEST_VECTORS);
  1047. break;
  1048. case 27:
  1049. test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
  1050. break;
  1051. case 28:
  1052. test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
  1053. break;
  1054. case 29:
  1055. test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
  1056. break;
  1057. case 30:
  1058. test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
  1059. XETA_ENC_TEST_VECTORS);
  1060. test_cipher("ecb(xeta)", DECRYPT, xeta_dec_tv_template,
  1061. XETA_DEC_TEST_VECTORS);
  1062. break;
  1063. case 31:
  1064. test_cipher("pcbc(fcrypt)", ENCRYPT, fcrypt_pcbc_enc_tv_template,
  1065. FCRYPT_ENC_TEST_VECTORS);
  1066. test_cipher("pcbc(fcrypt)", DECRYPT, fcrypt_pcbc_dec_tv_template,
  1067. FCRYPT_DEC_TEST_VECTORS);
  1068. break;
  1069. case 32:
  1070. test_cipher("ecb(camellia)", ENCRYPT,
  1071. camellia_enc_tv_template,
  1072. CAMELLIA_ENC_TEST_VECTORS);
  1073. test_cipher("ecb(camellia)", DECRYPT,
  1074. camellia_dec_tv_template,
  1075. CAMELLIA_DEC_TEST_VECTORS);
  1076. test_cipher("cbc(camellia)", ENCRYPT,
  1077. camellia_cbc_enc_tv_template,
  1078. CAMELLIA_CBC_ENC_TEST_VECTORS);
  1079. test_cipher("cbc(camellia)", DECRYPT,
  1080. camellia_cbc_dec_tv_template,
  1081. CAMELLIA_CBC_DEC_TEST_VECTORS);
  1082. break;
  1083. case 33:
  1084. test_hash("sha224", sha224_tv_template, SHA224_TEST_VECTORS);
  1085. break;
  1086. case 34:
  1087. test_cipher("salsa20", ENCRYPT,
  1088. salsa20_stream_enc_tv_template,
  1089. SALSA20_STREAM_ENC_TEST_VECTORS);
  1090. break;
  1091. case 100:
  1092. test_hash("hmac(md5)", hmac_md5_tv_template,
  1093. HMAC_MD5_TEST_VECTORS);
  1094. break;
  1095. case 101:
  1096. test_hash("hmac(sha1)", hmac_sha1_tv_template,
  1097. HMAC_SHA1_TEST_VECTORS);
  1098. break;
  1099. case 102:
  1100. test_hash("hmac(sha256)", hmac_sha256_tv_template,
  1101. HMAC_SHA256_TEST_VECTORS);
  1102. break;
  1103. case 103:
  1104. test_hash("hmac(sha384)", hmac_sha384_tv_template,
  1105. HMAC_SHA384_TEST_VECTORS);
  1106. break;
  1107. case 104:
  1108. test_hash("hmac(sha512)", hmac_sha512_tv_template,
  1109. HMAC_SHA512_TEST_VECTORS);
  1110. break;
  1111. case 105:
  1112. test_hash("hmac(sha224)", hmac_sha224_tv_template,
  1113. HMAC_SHA224_TEST_VECTORS);
  1114. break;
  1115. case 200:
  1116. test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
  1117. aes_speed_template);
  1118. test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
  1119. aes_speed_template);
  1120. test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
  1121. aes_speed_template);
  1122. test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
  1123. aes_speed_template);
  1124. test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
  1125. aes_lrw_speed_template);
  1126. test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
  1127. aes_lrw_speed_template);
  1128. test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
  1129. aes_xts_speed_template);
  1130. test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
  1131. aes_xts_speed_template);
  1132. break;
  1133. case 201:
  1134. test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
  1135. des3_ede_enc_tv_template,
  1136. DES3_EDE_ENC_TEST_VECTORS,
  1137. des3_ede_speed_template);
  1138. test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
  1139. des3_ede_dec_tv_template,
  1140. DES3_EDE_DEC_TEST_VECTORS,
  1141. des3_ede_speed_template);
  1142. test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
  1143. des3_ede_enc_tv_template,
  1144. DES3_EDE_ENC_TEST_VECTORS,
  1145. des3_ede_speed_template);
  1146. test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
  1147. des3_ede_dec_tv_template,
  1148. DES3_EDE_DEC_TEST_VECTORS,
  1149. des3_ede_speed_template);
  1150. break;
  1151. case 202:
  1152. test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
  1153. twofish_speed_template);
  1154. test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
  1155. twofish_speed_template);
  1156. test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
  1157. twofish_speed_template);
  1158. test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
  1159. twofish_speed_template);
  1160. break;
  1161. case 203:
  1162. test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
  1163. blowfish_speed_template);
  1164. test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
  1165. blowfish_speed_template);
  1166. test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
  1167. blowfish_speed_template);
  1168. test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
  1169. blowfish_speed_template);
  1170. break;
  1171. case 204:
  1172. test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
  1173. des_speed_template);
  1174. test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
  1175. des_speed_template);
  1176. test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
  1177. des_speed_template);
  1178. test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
  1179. des_speed_template);
  1180. break;
  1181. case 205:
  1182. test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
  1183. camellia_speed_template);
  1184. test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
  1185. camellia_speed_template);
  1186. test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
  1187. camellia_speed_template);
  1188. test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
  1189. camellia_speed_template);
  1190. break;
  1191. case 300:
  1192. /* fall through */
  1193. case 301:
  1194. test_hash_speed("md4", sec, generic_hash_speed_template);
  1195. if (mode > 300 && mode < 400) break;
  1196. case 302:
  1197. test_hash_speed("md5", sec, generic_hash_speed_template);
  1198. if (mode > 300 && mode < 400) break;
  1199. case 303:
  1200. test_hash_speed("sha1", sec, generic_hash_speed_template);
  1201. if (mode > 300 && mode < 400) break;
  1202. case 304:
  1203. test_hash_speed("sha256", sec, generic_hash_speed_template);
  1204. if (mode > 300 && mode < 400) break;
  1205. case 305:
  1206. test_hash_speed("sha384", sec, generic_hash_speed_template);
  1207. if (mode > 300 && mode < 400) break;
  1208. case 306:
  1209. test_hash_speed("sha512", sec, generic_hash_speed_template);
  1210. if (mode > 300 && mode < 400) break;
  1211. case 307:
  1212. test_hash_speed("wp256", sec, generic_hash_speed_template);
  1213. if (mode > 300 && mode < 400) break;
  1214. case 308:
  1215. test_hash_speed("wp384", sec, generic_hash_speed_template);
  1216. if (mode > 300 && mode < 400) break;
  1217. case 309:
  1218. test_hash_speed("wp512", sec, generic_hash_speed_template);
  1219. if (mode > 300 && mode < 400) break;
  1220. case 310:
  1221. test_hash_speed("tgr128", sec, generic_hash_speed_template);
  1222. if (mode > 300 && mode < 400) break;
  1223. case 311:
  1224. test_hash_speed("tgr160", sec, generic_hash_speed_template);
  1225. if (mode > 300 && mode < 400) break;
  1226. case 312:
  1227. test_hash_speed("tgr192", sec, generic_hash_speed_template);
  1228. if (mode > 300 && mode < 400) break;
  1229. case 313:
  1230. test_hash_speed("sha224", sec, generic_hash_speed_template);
  1231. if (mode > 300 && mode < 400) break;
  1232. case 399:
  1233. break;
  1234. case 1000:
  1235. test_available();
  1236. break;
  1237. default:
  1238. /* useful for debugging */
  1239. printk("not testing anything\n");
  1240. break;
  1241. }
  1242. }
  1243. static int __init init(void)
  1244. {
  1245. tvmem = kmalloc(TVMEMSIZE, GFP_KERNEL);
  1246. if (tvmem == NULL)
  1247. return -ENOMEM;
  1248. xbuf = kmalloc(XBUFSIZE, GFP_KERNEL);
  1249. if (xbuf == NULL) {
  1250. kfree(tvmem);
  1251. return -ENOMEM;
  1252. }
  1253. do_test();
  1254. kfree(xbuf);
  1255. kfree(tvmem);
  1256. /* We intentionaly return -EAGAIN to prevent keeping
  1257. * the module. It does all its work from init()
  1258. * and doesn't offer any runtime functionality
  1259. * => we don't need it in the memory, do we?
  1260. * -- mludvig
  1261. */
  1262. return -EAGAIN;
  1263. }
  1264. /*
  1265. * If an init function is provided, an exit function must also be provided
  1266. * to allow module unload.
  1267. */
  1268. static void __exit fini(void) { }
  1269. module_init(init);
  1270. module_exit(fini);
  1271. module_param(mode, int, 0);
  1272. module_param(sec, uint, 0);
  1273. MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
  1274. "(defaults to zero which uses CPU cycles instead)");
  1275. MODULE_LICENSE("GPL");
  1276. MODULE_DESCRIPTION("Quick & dirty crypto testing module");
  1277. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");