tcrypt.c 37 KB

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