tcrypt.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  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. * 2004-08-09 Added cipher speed tests (Reyk Floeter <reyk@vantronix.net>)
  16. * 2003-09-14 Rewritten by Kartikey Mahendra Bhatt
  17. *
  18. */
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/mm.h>
  22. #include <linux/slab.h>
  23. #include <linux/scatterlist.h>
  24. #include <linux/string.h>
  25. #include <linux/crypto.h>
  26. #include <linux/highmem.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/jiffies.h>
  29. #include <linux/timex.h>
  30. #include <linux/interrupt.h>
  31. #include "tcrypt.h"
  32. /*
  33. * Need to kmalloc() memory for testing kmap().
  34. */
  35. #define TVMEMSIZE 16384
  36. #define XBUFSIZE 32768
  37. /*
  38. * Indexes into the xbuf to simulate cross-page access.
  39. */
  40. #define IDX1 37
  41. #define IDX2 32400
  42. #define IDX3 1
  43. #define IDX4 8193
  44. #define IDX5 22222
  45. #define IDX6 17101
  46. #define IDX7 27333
  47. #define IDX8 3000
  48. /*
  49. * Used by test_cipher()
  50. */
  51. #define ENCRYPT 1
  52. #define DECRYPT 0
  53. #define MODE_ECB 1
  54. #define MODE_CBC 0
  55. static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
  56. /*
  57. * Used by test_cipher_speed()
  58. */
  59. static unsigned int sec;
  60. static int mode;
  61. static char *xbuf;
  62. static char *tvmem;
  63. static char *check[] = {
  64. "des", "md5", "des3_ede", "rot13", "sha1", "sha256", "blowfish",
  65. "twofish", "serpent", "sha384", "sha512", "md4", "aes", "cast6",
  66. "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
  67. "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", NULL
  68. };
  69. static void hexdump(unsigned char *buf, unsigned int len)
  70. {
  71. while (len--)
  72. printk("%02x", *buf++);
  73. printk("\n");
  74. }
  75. static void test_hash(char *algo, struct hash_testvec *template,
  76. unsigned int tcount)
  77. {
  78. unsigned int i, j, k, temp;
  79. struct scatterlist sg[8];
  80. char result[64];
  81. struct crypto_tfm *tfm;
  82. struct hash_testvec *hash_tv;
  83. unsigned int tsize;
  84. printk("\ntesting %s\n", algo);
  85. tsize = sizeof(struct hash_testvec);
  86. tsize *= tcount;
  87. if (tsize > TVMEMSIZE) {
  88. printk("template (%u) too big for tvmem (%u)\n", tsize, TVMEMSIZE);
  89. return;
  90. }
  91. memcpy(tvmem, template, tsize);
  92. hash_tv = (void *)tvmem;
  93. tfm = crypto_alloc_tfm(algo, 0);
  94. if (tfm == NULL) {
  95. printk("failed to load transform for %s\n", algo);
  96. return;
  97. }
  98. for (i = 0; i < tcount; i++) {
  99. printk("test %u:\n", i + 1);
  100. memset(result, 0, 64);
  101. sg_set_buf(&sg[0], hash_tv[i].plaintext, hash_tv[i].psize);
  102. crypto_digest_init(tfm);
  103. if (tfm->crt_u.digest.dit_setkey) {
  104. crypto_digest_setkey(tfm, hash_tv[i].key,
  105. hash_tv[i].ksize);
  106. }
  107. crypto_digest_update(tfm, sg, 1);
  108. crypto_digest_final(tfm, result);
  109. hexdump(result, crypto_tfm_alg_digestsize(tfm));
  110. printk("%s\n",
  111. memcmp(result, hash_tv[i].digest,
  112. crypto_tfm_alg_digestsize(tfm)) ?
  113. "fail" : "pass");
  114. }
  115. printk("testing %s across pages\n", algo);
  116. /* setup the dummy buffer first */
  117. memset(xbuf, 0, XBUFSIZE);
  118. j = 0;
  119. for (i = 0; i < tcount; i++) {
  120. if (hash_tv[i].np) {
  121. j++;
  122. printk("test %u:\n", j);
  123. memset(result, 0, 64);
  124. temp = 0;
  125. for (k = 0; k < hash_tv[i].np; k++) {
  126. memcpy(&xbuf[IDX[k]],
  127. hash_tv[i].plaintext + temp,
  128. hash_tv[i].tap[k]);
  129. temp += hash_tv[i].tap[k];
  130. sg_set_buf(&sg[k], &xbuf[IDX[k]],
  131. hash_tv[i].tap[k]);
  132. }
  133. crypto_digest_digest(tfm, sg, hash_tv[i].np, result);
  134. hexdump(result, crypto_tfm_alg_digestsize(tfm));
  135. printk("%s\n",
  136. memcmp(result, hash_tv[i].digest,
  137. crypto_tfm_alg_digestsize(tfm)) ?
  138. "fail" : "pass");
  139. }
  140. }
  141. crypto_free_tfm(tfm);
  142. }
  143. #ifdef CONFIG_CRYPTO_HMAC
  144. static void test_hmac(char *algo, struct hmac_testvec *template,
  145. unsigned int tcount)
  146. {
  147. unsigned int i, j, k, temp;
  148. struct scatterlist sg[8];
  149. char result[64];
  150. struct crypto_tfm *tfm;
  151. struct hmac_testvec *hmac_tv;
  152. unsigned int tsize, klen;
  153. tfm = crypto_alloc_tfm(algo, 0);
  154. if (tfm == NULL) {
  155. printk("failed to load transform for %s\n", algo);
  156. return;
  157. }
  158. printk("\ntesting hmac_%s\n", algo);
  159. tsize = sizeof(struct hmac_testvec);
  160. tsize *= tcount;
  161. if (tsize > TVMEMSIZE) {
  162. printk("template (%u) too big for tvmem (%u)\n", tsize,
  163. TVMEMSIZE);
  164. goto out;
  165. }
  166. memcpy(tvmem, template, tsize);
  167. hmac_tv = (void *)tvmem;
  168. for (i = 0; i < tcount; i++) {
  169. printk("test %u:\n", i + 1);
  170. memset(result, 0, sizeof (result));
  171. klen = hmac_tv[i].ksize;
  172. sg_set_buf(&sg[0], hmac_tv[i].plaintext, hmac_tv[i].psize);
  173. crypto_hmac(tfm, hmac_tv[i].key, &klen, sg, 1, result);
  174. hexdump(result, crypto_tfm_alg_digestsize(tfm));
  175. printk("%s\n",
  176. memcmp(result, hmac_tv[i].digest,
  177. crypto_tfm_alg_digestsize(tfm)) ? "fail" :
  178. "pass");
  179. }
  180. printk("\ntesting hmac_%s across pages\n", algo);
  181. memset(xbuf, 0, XBUFSIZE);
  182. j = 0;
  183. for (i = 0; i < tcount; i++) {
  184. if (hmac_tv[i].np) {
  185. j++;
  186. printk("test %u:\n",j);
  187. memset(result, 0, 64);
  188. temp = 0;
  189. klen = hmac_tv[i].ksize;
  190. for (k = 0; k < hmac_tv[i].np; k++) {
  191. memcpy(&xbuf[IDX[k]],
  192. hmac_tv[i].plaintext + temp,
  193. hmac_tv[i].tap[k]);
  194. temp += hmac_tv[i].tap[k];
  195. sg_set_buf(&sg[k], &xbuf[IDX[k]],
  196. hmac_tv[i].tap[k]);
  197. }
  198. crypto_hmac(tfm, hmac_tv[i].key, &klen, sg,
  199. hmac_tv[i].np, result);
  200. hexdump(result, crypto_tfm_alg_digestsize(tfm));
  201. printk("%s\n",
  202. memcmp(result, hmac_tv[i].digest,
  203. crypto_tfm_alg_digestsize(tfm)) ?
  204. "fail" : "pass");
  205. }
  206. }
  207. out:
  208. crypto_free_tfm(tfm);
  209. }
  210. #endif /* CONFIG_CRYPTO_HMAC */
  211. static void test_cipher(char *algo, int mode, int enc,
  212. struct cipher_testvec *template, unsigned int tcount)
  213. {
  214. unsigned int ret, i, j, k, temp;
  215. unsigned int tsize;
  216. char *q;
  217. struct crypto_tfm *tfm;
  218. char *key;
  219. struct cipher_testvec *cipher_tv;
  220. struct scatterlist sg[8];
  221. const char *e, *m;
  222. if (enc == ENCRYPT)
  223. e = "encryption";
  224. else
  225. e = "decryption";
  226. if (mode == MODE_ECB)
  227. m = "ECB";
  228. else
  229. m = "CBC";
  230. printk("\ntesting %s %s %s\n", algo, m, e);
  231. tsize = sizeof (struct cipher_testvec);
  232. tsize *= tcount;
  233. if (tsize > TVMEMSIZE) {
  234. printk("template (%u) too big for tvmem (%u)\n", tsize,
  235. TVMEMSIZE);
  236. return;
  237. }
  238. memcpy(tvmem, template, tsize);
  239. cipher_tv = (void *)tvmem;
  240. if (mode)
  241. tfm = crypto_alloc_tfm(algo, 0);
  242. else
  243. tfm = crypto_alloc_tfm(algo, CRYPTO_TFM_MODE_CBC);
  244. if (tfm == NULL) {
  245. printk("failed to load transform for %s %s\n", algo, m);
  246. return;
  247. }
  248. j = 0;
  249. for (i = 0; i < tcount; i++) {
  250. if (!(cipher_tv[i].np)) {
  251. j++;
  252. printk("test %u (%d bit key):\n",
  253. j, cipher_tv[i].klen * 8);
  254. tfm->crt_flags = 0;
  255. if (cipher_tv[i].wk)
  256. tfm->crt_flags |= CRYPTO_TFM_REQ_WEAK_KEY;
  257. key = cipher_tv[i].key;
  258. ret = crypto_cipher_setkey(tfm, key, cipher_tv[i].klen);
  259. if (ret) {
  260. printk("setkey() failed flags=%x\n", tfm->crt_flags);
  261. if (!cipher_tv[i].fail)
  262. goto out;
  263. }
  264. sg_set_buf(&sg[0], cipher_tv[i].input,
  265. cipher_tv[i].ilen);
  266. if (!mode) {
  267. crypto_cipher_set_iv(tfm, cipher_tv[i].iv,
  268. crypto_tfm_alg_ivsize(tfm));
  269. }
  270. if (enc)
  271. ret = crypto_cipher_encrypt(tfm, sg, sg, cipher_tv[i].ilen);
  272. else
  273. ret = crypto_cipher_decrypt(tfm, sg, sg, cipher_tv[i].ilen);
  274. if (ret) {
  275. printk("%s () failed flags=%x\n", e, tfm->crt_flags);
  276. goto out;
  277. }
  278. q = kmap(sg[0].page) + sg[0].offset;
  279. hexdump(q, cipher_tv[i].rlen);
  280. printk("%s\n",
  281. memcmp(q, cipher_tv[i].result,
  282. cipher_tv[i].rlen) ? "fail" : "pass");
  283. }
  284. }
  285. printk("\ntesting %s %s %s across pages (chunking)\n", algo, m, e);
  286. memset(xbuf, 0, XBUFSIZE);
  287. j = 0;
  288. for (i = 0; i < tcount; i++) {
  289. if (cipher_tv[i].np) {
  290. j++;
  291. printk("test %u (%d bit key):\n",
  292. j, cipher_tv[i].klen * 8);
  293. tfm->crt_flags = 0;
  294. if (cipher_tv[i].wk)
  295. tfm->crt_flags |= CRYPTO_TFM_REQ_WEAK_KEY;
  296. key = cipher_tv[i].key;
  297. ret = crypto_cipher_setkey(tfm, key, cipher_tv[i].klen);
  298. if (ret) {
  299. printk("setkey() failed flags=%x\n", tfm->crt_flags);
  300. if (!cipher_tv[i].fail)
  301. goto out;
  302. }
  303. temp = 0;
  304. for (k = 0; k < cipher_tv[i].np; k++) {
  305. memcpy(&xbuf[IDX[k]],
  306. cipher_tv[i].input + temp,
  307. cipher_tv[i].tap[k]);
  308. temp += cipher_tv[i].tap[k];
  309. sg_set_buf(&sg[k], &xbuf[IDX[k]],
  310. cipher_tv[i].tap[k]);
  311. }
  312. if (!mode) {
  313. crypto_cipher_set_iv(tfm, cipher_tv[i].iv,
  314. crypto_tfm_alg_ivsize(tfm));
  315. }
  316. if (enc)
  317. ret = crypto_cipher_encrypt(tfm, sg, sg, cipher_tv[i].ilen);
  318. else
  319. ret = crypto_cipher_decrypt(tfm, sg, sg, cipher_tv[i].ilen);
  320. if (ret) {
  321. printk("%s () failed flags=%x\n", e, tfm->crt_flags);
  322. goto out;
  323. }
  324. temp = 0;
  325. for (k = 0; k < cipher_tv[i].np; k++) {
  326. printk("page %u\n", k);
  327. q = kmap(sg[k].page) + sg[k].offset;
  328. hexdump(q, cipher_tv[i].tap[k]);
  329. printk("%s\n",
  330. memcmp(q, cipher_tv[i].result + temp,
  331. cipher_tv[i].tap[k]) ? "fail" :
  332. "pass");
  333. temp += cipher_tv[i].tap[k];
  334. }
  335. }
  336. }
  337. out:
  338. crypto_free_tfm(tfm);
  339. }
  340. static int test_cipher_jiffies(struct crypto_tfm *tfm, int enc, char *p,
  341. int blen, int sec)
  342. {
  343. struct scatterlist sg[1];
  344. unsigned long start, end;
  345. int bcount;
  346. int ret;
  347. sg_set_buf(sg, p, blen);
  348. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  349. time_before(jiffies, end); bcount++) {
  350. if (enc)
  351. ret = crypto_cipher_encrypt(tfm, sg, sg, blen);
  352. else
  353. ret = crypto_cipher_decrypt(tfm, sg, sg, blen);
  354. if (ret)
  355. return ret;
  356. }
  357. printk("%d operations in %d seconds (%ld bytes)\n",
  358. bcount, sec, (long)bcount * blen);
  359. return 0;
  360. }
  361. static int test_cipher_cycles(struct crypto_tfm *tfm, int enc, char *p,
  362. int blen)
  363. {
  364. struct scatterlist sg[1];
  365. unsigned long cycles = 0;
  366. int ret = 0;
  367. int i;
  368. sg_set_buf(sg, p, blen);
  369. local_bh_disable();
  370. local_irq_disable();
  371. /* Warm-up run. */
  372. for (i = 0; i < 4; i++) {
  373. if (enc)
  374. ret = crypto_cipher_encrypt(tfm, sg, sg, blen);
  375. else
  376. ret = crypto_cipher_decrypt(tfm, sg, sg, blen);
  377. if (ret)
  378. goto out;
  379. }
  380. /* The real thing. */
  381. for (i = 0; i < 8; i++) {
  382. cycles_t start, end;
  383. start = get_cycles();
  384. if (enc)
  385. ret = crypto_cipher_encrypt(tfm, sg, sg, blen);
  386. else
  387. ret = crypto_cipher_decrypt(tfm, sg, sg, blen);
  388. end = get_cycles();
  389. if (ret)
  390. goto out;
  391. cycles += end - start;
  392. }
  393. out:
  394. local_irq_enable();
  395. local_bh_enable();
  396. if (ret == 0)
  397. printk("1 operation in %lu cycles (%d bytes)\n",
  398. (cycles + 4) / 8, blen);
  399. return ret;
  400. }
  401. static void test_cipher_speed(char *algo, int mode, int enc, unsigned int sec,
  402. struct cipher_testvec *template,
  403. unsigned int tcount, struct cipher_speed *speed)
  404. {
  405. unsigned int ret, i, j, iv_len;
  406. unsigned char *key, *p, iv[128];
  407. struct crypto_tfm *tfm;
  408. const char *e, *m;
  409. if (enc == ENCRYPT)
  410. e = "encryption";
  411. else
  412. e = "decryption";
  413. if (mode == MODE_ECB)
  414. m = "ECB";
  415. else
  416. m = "CBC";
  417. printk("\ntesting speed of %s %s %s\n", algo, m, e);
  418. if (mode)
  419. tfm = crypto_alloc_tfm(algo, 0);
  420. else
  421. tfm = crypto_alloc_tfm(algo, CRYPTO_TFM_MODE_CBC);
  422. if (tfm == NULL) {
  423. printk("failed to load transform for %s %s\n", algo, m);
  424. return;
  425. }
  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_cipher_setkey(tfm, key, speed[i].klen);
  445. if (ret) {
  446. printk("setkey() failed flags=%x\n", tfm->crt_flags);
  447. goto out;
  448. }
  449. if (!mode) {
  450. iv_len = crypto_tfm_alg_ivsize(tfm);
  451. memset(&iv, 0xff, iv_len);
  452. crypto_cipher_set_iv(tfm, iv, iv_len);
  453. }
  454. if (sec)
  455. ret = test_cipher_jiffies(tfm, enc, p, speed[i].blen,
  456. sec);
  457. else
  458. ret = test_cipher_cycles(tfm, enc, p, speed[i].blen);
  459. if (ret) {
  460. printk("%s() failed flags=%x\n", e, tfm->crt_flags);
  461. break;
  462. }
  463. }
  464. out:
  465. crypto_free_tfm(tfm);
  466. }
  467. static void test_digest_jiffies(struct crypto_tfm *tfm, char *p, int blen,
  468. int plen, char *out, int sec)
  469. {
  470. struct scatterlist sg[1];
  471. unsigned long start, end;
  472. int bcount, pcount;
  473. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  474. time_before(jiffies, end); bcount++) {
  475. crypto_digest_init(tfm);
  476. for (pcount = 0; pcount < blen; pcount += plen) {
  477. sg_set_buf(sg, p + pcount, plen);
  478. crypto_digest_update(tfm, sg, 1);
  479. }
  480. /* we assume there is enough space in 'out' for the result */
  481. crypto_digest_final(tfm, out);
  482. }
  483. printk("%6u opers/sec, %9lu bytes/sec\n",
  484. bcount / sec, ((long)bcount * blen) / sec);
  485. return;
  486. }
  487. static void test_digest_cycles(struct crypto_tfm *tfm, char *p, int blen,
  488. int plen, char *out)
  489. {
  490. struct scatterlist sg[1];
  491. unsigned long cycles = 0;
  492. int i, pcount;
  493. local_bh_disable();
  494. local_irq_disable();
  495. /* Warm-up run. */
  496. for (i = 0; i < 4; i++) {
  497. crypto_digest_init(tfm);
  498. for (pcount = 0; pcount < blen; pcount += plen) {
  499. sg_set_buf(sg, p + pcount, plen);
  500. crypto_digest_update(tfm, sg, 1);
  501. }
  502. crypto_digest_final(tfm, out);
  503. }
  504. /* The real thing. */
  505. for (i = 0; i < 8; i++) {
  506. cycles_t start, end;
  507. crypto_digest_init(tfm);
  508. start = get_cycles();
  509. for (pcount = 0; pcount < blen; pcount += plen) {
  510. sg_set_buf(sg, p + pcount, plen);
  511. crypto_digest_update(tfm, sg, 1);
  512. }
  513. crypto_digest_final(tfm, out);
  514. end = get_cycles();
  515. cycles += end - start;
  516. }
  517. local_irq_enable();
  518. local_bh_enable();
  519. printk("%6lu cycles/operation, %4lu cycles/byte\n",
  520. cycles / 8, cycles / (8 * blen));
  521. return;
  522. }
  523. static void test_digest_speed(char *algo, unsigned int sec,
  524. struct digest_speed *speed)
  525. {
  526. struct crypto_tfm *tfm;
  527. char output[1024];
  528. int i;
  529. printk("\ntesting speed of %s\n", algo);
  530. tfm = crypto_alloc_tfm(algo, 0);
  531. if (tfm == NULL) {
  532. printk("failed to load transform for %s\n", algo);
  533. return;
  534. }
  535. if (crypto_tfm_alg_digestsize(tfm) > sizeof(output)) {
  536. printk("digestsize(%u) > outputbuffer(%zu)\n",
  537. crypto_tfm_alg_digestsize(tfm), sizeof(output));
  538. goto out;
  539. }
  540. for (i = 0; speed[i].blen != 0; i++) {
  541. if (speed[i].blen > TVMEMSIZE) {
  542. printk("template (%u) too big for tvmem (%u)\n",
  543. speed[i].blen, TVMEMSIZE);
  544. goto out;
  545. }
  546. printk("test%3u (%5u byte blocks,%5u bytes per update,%4u updates): ",
  547. i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
  548. memset(tvmem, 0xff, speed[i].blen);
  549. if (sec)
  550. test_digest_jiffies(tfm, tvmem, speed[i].blen, speed[i].plen, output, sec);
  551. else
  552. test_digest_cycles(tfm, tvmem, speed[i].blen, speed[i].plen, output);
  553. }
  554. out:
  555. crypto_free_tfm(tfm);
  556. }
  557. static void test_deflate(void)
  558. {
  559. unsigned int i;
  560. char result[COMP_BUF_SIZE];
  561. struct crypto_tfm *tfm;
  562. struct comp_testvec *tv;
  563. unsigned int tsize;
  564. printk("\ntesting deflate compression\n");
  565. tsize = sizeof (deflate_comp_tv_template);
  566. if (tsize > TVMEMSIZE) {
  567. printk("template (%u) too big for tvmem (%u)\n", tsize,
  568. TVMEMSIZE);
  569. return;
  570. }
  571. memcpy(tvmem, deflate_comp_tv_template, tsize);
  572. tv = (void *)tvmem;
  573. tfm = crypto_alloc_tfm("deflate", 0);
  574. if (tfm == NULL) {
  575. printk("failed to load transform for deflate\n");
  576. return;
  577. }
  578. for (i = 0; i < DEFLATE_COMP_TEST_VECTORS; i++) {
  579. int ilen, ret, dlen = COMP_BUF_SIZE;
  580. printk("test %u:\n", i + 1);
  581. memset(result, 0, sizeof (result));
  582. ilen = tv[i].inlen;
  583. ret = crypto_comp_compress(tfm, tv[i].input,
  584. ilen, result, &dlen);
  585. if (ret) {
  586. printk("fail: ret=%d\n", ret);
  587. continue;
  588. }
  589. hexdump(result, dlen);
  590. printk("%s (ratio %d:%d)\n",
  591. memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
  592. ilen, dlen);
  593. }
  594. printk("\ntesting deflate decompression\n");
  595. tsize = sizeof (deflate_decomp_tv_template);
  596. if (tsize > TVMEMSIZE) {
  597. printk("template (%u) too big for tvmem (%u)\n", tsize,
  598. TVMEMSIZE);
  599. goto out;
  600. }
  601. memcpy(tvmem, deflate_decomp_tv_template, tsize);
  602. tv = (void *)tvmem;
  603. for (i = 0; i < DEFLATE_DECOMP_TEST_VECTORS; i++) {
  604. int ilen, ret, dlen = COMP_BUF_SIZE;
  605. printk("test %u:\n", i + 1);
  606. memset(result, 0, sizeof (result));
  607. ilen = tv[i].inlen;
  608. ret = crypto_comp_decompress(tfm, tv[i].input,
  609. ilen, result, &dlen);
  610. if (ret) {
  611. printk("fail: ret=%d\n", ret);
  612. continue;
  613. }
  614. hexdump(result, dlen);
  615. printk("%s (ratio %d:%d)\n",
  616. memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
  617. ilen, dlen);
  618. }
  619. out:
  620. crypto_free_tfm(tfm);
  621. }
  622. static void test_crc32c(void)
  623. {
  624. #define NUMVEC 6
  625. #define VECSIZE 40
  626. int i, j, pass;
  627. u32 crc;
  628. u8 b, test_vec[NUMVEC][VECSIZE];
  629. static u32 vec_results[NUMVEC] = {
  630. 0x0e2c157f, 0xe980ebf6, 0xde74bded,
  631. 0xd579c862, 0xba979ad0, 0x2b29d913
  632. };
  633. static u32 tot_vec_results = 0x24c5d375;
  634. struct scatterlist sg[NUMVEC];
  635. struct crypto_tfm *tfm;
  636. char *fmtdata = "testing crc32c initialized to %08x: %s\n";
  637. #define SEEDTESTVAL 0xedcba987
  638. u32 seed;
  639. printk("\ntesting crc32c\n");
  640. tfm = crypto_alloc_tfm("crc32c", 0);
  641. if (tfm == NULL) {
  642. printk("failed to load transform for crc32c\n");
  643. return;
  644. }
  645. crypto_digest_init(tfm);
  646. crypto_digest_final(tfm, (u8*)&crc);
  647. printk(fmtdata, crc, (crc == 0) ? "pass" : "ERROR");
  648. /*
  649. * stuff test_vec with known values, simple incrementing
  650. * byte values.
  651. */
  652. b = 0;
  653. for (i = 0; i < NUMVEC; i++) {
  654. for (j = 0; j < VECSIZE; j++)
  655. test_vec[i][j] = ++b;
  656. sg_set_buf(&sg[i], test_vec[i], VECSIZE);
  657. }
  658. seed = SEEDTESTVAL;
  659. (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32));
  660. crypto_digest_final(tfm, (u8*)&crc);
  661. printk("testing crc32c setkey returns %08x : %s\n", crc, (crc == (SEEDTESTVAL ^ ~(u32)0)) ?
  662. "pass" : "ERROR");
  663. printk("testing crc32c using update/final:\n");
  664. pass = 1; /* assume all is well */
  665. for (i = 0; i < NUMVEC; i++) {
  666. seed = ~(u32)0;
  667. (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32));
  668. crypto_digest_update(tfm, &sg[i], 1);
  669. crypto_digest_final(tfm, (u8*)&crc);
  670. if (crc == vec_results[i]) {
  671. printk(" %08x:OK", crc);
  672. } else {
  673. printk(" %08x:BAD, wanted %08x\n", crc, vec_results[i]);
  674. pass = 0;
  675. }
  676. }
  677. printk("\ntesting crc32c using incremental accumulator:\n");
  678. crc = 0;
  679. for (i = 0; i < NUMVEC; i++) {
  680. seed = (crc ^ ~(u32)0);
  681. (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32));
  682. crypto_digest_update(tfm, &sg[i], 1);
  683. crypto_digest_final(tfm, (u8*)&crc);
  684. }
  685. if (crc == tot_vec_results) {
  686. printk(" %08x:OK", crc);
  687. } else {
  688. printk(" %08x:BAD, wanted %08x\n", crc, tot_vec_results);
  689. pass = 0;
  690. }
  691. printk("\ntesting crc32c using digest:\n");
  692. seed = ~(u32)0;
  693. (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32));
  694. crypto_digest_digest(tfm, sg, NUMVEC, (u8*)&crc);
  695. if (crc == tot_vec_results) {
  696. printk(" %08x:OK", crc);
  697. } else {
  698. printk(" %08x:BAD, wanted %08x\n", crc, tot_vec_results);
  699. pass = 0;
  700. }
  701. printk("\n%s\n", pass ? "pass" : "ERROR");
  702. crypto_free_tfm(tfm);
  703. printk("crc32c test complete\n");
  704. }
  705. static void test_available(void)
  706. {
  707. char **name = check;
  708. while (*name) {
  709. printk("alg %s ", *name);
  710. printk((crypto_alg_available(*name, 0)) ?
  711. "found\n" : "not found\n");
  712. name++;
  713. }
  714. }
  715. static void do_test(void)
  716. {
  717. switch (mode) {
  718. case 0:
  719. test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
  720. test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
  721. //DES
  722. test_cipher ("des", MODE_ECB, ENCRYPT, des_enc_tv_template, DES_ENC_TEST_VECTORS);
  723. test_cipher ("des", MODE_ECB, DECRYPT, des_dec_tv_template, DES_DEC_TEST_VECTORS);
  724. test_cipher ("des", MODE_CBC, ENCRYPT, des_cbc_enc_tv_template, DES_CBC_ENC_TEST_VECTORS);
  725. test_cipher ("des", MODE_CBC, DECRYPT, des_cbc_dec_tv_template, DES_CBC_DEC_TEST_VECTORS);
  726. //DES3_EDE
  727. test_cipher ("des3_ede", MODE_ECB, ENCRYPT, des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS);
  728. test_cipher ("des3_ede", MODE_ECB, DECRYPT, des3_ede_dec_tv_template, DES3_EDE_DEC_TEST_VECTORS);
  729. test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
  730. test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
  731. //BLOWFISH
  732. test_cipher ("blowfish", MODE_ECB, ENCRYPT, bf_enc_tv_template, BF_ENC_TEST_VECTORS);
  733. test_cipher ("blowfish", MODE_ECB, DECRYPT, bf_dec_tv_template, BF_DEC_TEST_VECTORS);
  734. test_cipher ("blowfish", MODE_CBC, ENCRYPT, bf_cbc_enc_tv_template, BF_CBC_ENC_TEST_VECTORS);
  735. test_cipher ("blowfish", MODE_CBC, DECRYPT, bf_cbc_dec_tv_template, BF_CBC_DEC_TEST_VECTORS);
  736. //TWOFISH
  737. test_cipher ("twofish", MODE_ECB, ENCRYPT, tf_enc_tv_template, TF_ENC_TEST_VECTORS);
  738. test_cipher ("twofish", MODE_ECB, DECRYPT, tf_dec_tv_template, TF_DEC_TEST_VECTORS);
  739. test_cipher ("twofish", MODE_CBC, ENCRYPT, tf_cbc_enc_tv_template, TF_CBC_ENC_TEST_VECTORS);
  740. test_cipher ("twofish", MODE_CBC, DECRYPT, tf_cbc_dec_tv_template, TF_CBC_DEC_TEST_VECTORS);
  741. //SERPENT
  742. test_cipher ("serpent", MODE_ECB, ENCRYPT, serpent_enc_tv_template, SERPENT_ENC_TEST_VECTORS);
  743. test_cipher ("serpent", MODE_ECB, DECRYPT, serpent_dec_tv_template, SERPENT_DEC_TEST_VECTORS);
  744. //TNEPRES
  745. test_cipher ("tnepres", MODE_ECB, ENCRYPT, tnepres_enc_tv_template, TNEPRES_ENC_TEST_VECTORS);
  746. test_cipher ("tnepres", MODE_ECB, DECRYPT, tnepres_dec_tv_template, TNEPRES_DEC_TEST_VECTORS);
  747. //AES
  748. test_cipher ("aes", MODE_ECB, ENCRYPT, aes_enc_tv_template, AES_ENC_TEST_VECTORS);
  749. test_cipher ("aes", MODE_ECB, DECRYPT, aes_dec_tv_template, AES_DEC_TEST_VECTORS);
  750. test_cipher ("aes", MODE_CBC, ENCRYPT, aes_cbc_enc_tv_template, AES_CBC_ENC_TEST_VECTORS);
  751. test_cipher ("aes", MODE_CBC, DECRYPT, aes_cbc_dec_tv_template, AES_CBC_DEC_TEST_VECTORS);
  752. //CAST5
  753. test_cipher ("cast5", MODE_ECB, ENCRYPT, cast5_enc_tv_template, CAST5_ENC_TEST_VECTORS);
  754. test_cipher ("cast5", MODE_ECB, DECRYPT, cast5_dec_tv_template, CAST5_DEC_TEST_VECTORS);
  755. //CAST6
  756. test_cipher ("cast6", MODE_ECB, ENCRYPT, cast6_enc_tv_template, CAST6_ENC_TEST_VECTORS);
  757. test_cipher ("cast6", MODE_ECB, DECRYPT, cast6_dec_tv_template, CAST6_DEC_TEST_VECTORS);
  758. //ARC4
  759. test_cipher ("arc4", MODE_ECB, ENCRYPT, arc4_enc_tv_template, ARC4_ENC_TEST_VECTORS);
  760. test_cipher ("arc4", MODE_ECB, DECRYPT, arc4_dec_tv_template, ARC4_DEC_TEST_VECTORS);
  761. //TEA
  762. test_cipher ("tea", MODE_ECB, ENCRYPT, tea_enc_tv_template, TEA_ENC_TEST_VECTORS);
  763. test_cipher ("tea", MODE_ECB, DECRYPT, tea_dec_tv_template, TEA_DEC_TEST_VECTORS);
  764. //XTEA
  765. test_cipher ("xtea", MODE_ECB, ENCRYPT, xtea_enc_tv_template, XTEA_ENC_TEST_VECTORS);
  766. test_cipher ("xtea", MODE_ECB, DECRYPT, xtea_dec_tv_template, XTEA_DEC_TEST_VECTORS);
  767. //KHAZAD
  768. test_cipher ("khazad", MODE_ECB, ENCRYPT, khazad_enc_tv_template, KHAZAD_ENC_TEST_VECTORS);
  769. test_cipher ("khazad", MODE_ECB, DECRYPT, khazad_dec_tv_template, KHAZAD_DEC_TEST_VECTORS);
  770. //ANUBIS
  771. test_cipher ("anubis", MODE_ECB, ENCRYPT, anubis_enc_tv_template, ANUBIS_ENC_TEST_VECTORS);
  772. test_cipher ("anubis", MODE_ECB, DECRYPT, anubis_dec_tv_template, ANUBIS_DEC_TEST_VECTORS);
  773. test_cipher ("anubis", MODE_CBC, ENCRYPT, anubis_cbc_enc_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
  774. test_cipher ("anubis", MODE_CBC, DECRYPT, anubis_cbc_dec_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
  775. //XETA
  776. test_cipher ("xeta", MODE_ECB, ENCRYPT, xeta_enc_tv_template, XETA_ENC_TEST_VECTORS);
  777. test_cipher ("xeta", MODE_ECB, DECRYPT, xeta_dec_tv_template, XETA_DEC_TEST_VECTORS);
  778. test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
  779. test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
  780. test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
  781. test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
  782. test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
  783. test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
  784. test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
  785. test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
  786. test_deflate();
  787. test_crc32c();
  788. #ifdef CONFIG_CRYPTO_HMAC
  789. test_hmac("md5", hmac_md5_tv_template, HMAC_MD5_TEST_VECTORS);
  790. test_hmac("sha1", hmac_sha1_tv_template, HMAC_SHA1_TEST_VECTORS);
  791. test_hmac("sha256", hmac_sha256_tv_template, HMAC_SHA256_TEST_VECTORS);
  792. #endif
  793. test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
  794. break;
  795. case 1:
  796. test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
  797. break;
  798. case 2:
  799. test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
  800. break;
  801. case 3:
  802. test_cipher ("des", MODE_ECB, ENCRYPT, des_enc_tv_template, DES_ENC_TEST_VECTORS);
  803. test_cipher ("des", MODE_ECB, DECRYPT, des_dec_tv_template, DES_DEC_TEST_VECTORS);
  804. test_cipher ("des", MODE_CBC, ENCRYPT, des_cbc_enc_tv_template, DES_CBC_ENC_TEST_VECTORS);
  805. test_cipher ("des", MODE_CBC, DECRYPT, des_cbc_dec_tv_template, DES_CBC_DEC_TEST_VECTORS);
  806. break;
  807. case 4:
  808. test_cipher ("des3_ede", MODE_ECB, ENCRYPT, des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS);
  809. test_cipher ("des3_ede", MODE_ECB, DECRYPT, des3_ede_dec_tv_template, DES3_EDE_DEC_TEST_VECTORS);
  810. break;
  811. case 5:
  812. test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
  813. break;
  814. case 6:
  815. test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
  816. break;
  817. case 7:
  818. test_cipher ("blowfish", MODE_ECB, ENCRYPT, bf_enc_tv_template, BF_ENC_TEST_VECTORS);
  819. test_cipher ("blowfish", MODE_ECB, DECRYPT, bf_dec_tv_template, BF_DEC_TEST_VECTORS);
  820. test_cipher ("blowfish", MODE_CBC, ENCRYPT, bf_cbc_enc_tv_template, BF_CBC_ENC_TEST_VECTORS);
  821. test_cipher ("blowfish", MODE_CBC, DECRYPT, bf_cbc_dec_tv_template, BF_CBC_DEC_TEST_VECTORS);
  822. break;
  823. case 8:
  824. test_cipher ("twofish", MODE_ECB, ENCRYPT, tf_enc_tv_template, TF_ENC_TEST_VECTORS);
  825. test_cipher ("twofish", MODE_ECB, DECRYPT, tf_dec_tv_template, TF_DEC_TEST_VECTORS);
  826. test_cipher ("twofish", MODE_CBC, ENCRYPT, tf_cbc_enc_tv_template, TF_CBC_ENC_TEST_VECTORS);
  827. test_cipher ("twofish", MODE_CBC, DECRYPT, tf_cbc_dec_tv_template, TF_CBC_DEC_TEST_VECTORS);
  828. break;
  829. case 9:
  830. test_cipher ("serpent", MODE_ECB, ENCRYPT, serpent_enc_tv_template, SERPENT_ENC_TEST_VECTORS);
  831. test_cipher ("serpent", MODE_ECB, DECRYPT, serpent_dec_tv_template, SERPENT_DEC_TEST_VECTORS);
  832. break;
  833. case 10:
  834. test_cipher ("aes", MODE_ECB, ENCRYPT, aes_enc_tv_template, AES_ENC_TEST_VECTORS);
  835. test_cipher ("aes", MODE_ECB, DECRYPT, aes_dec_tv_template, AES_DEC_TEST_VECTORS);
  836. test_cipher ("aes", MODE_CBC, ENCRYPT, aes_cbc_enc_tv_template, AES_CBC_ENC_TEST_VECTORS);
  837. test_cipher ("aes", MODE_CBC, DECRYPT, aes_cbc_dec_tv_template, AES_CBC_DEC_TEST_VECTORS);
  838. break;
  839. case 11:
  840. test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
  841. break;
  842. case 12:
  843. test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
  844. break;
  845. case 13:
  846. test_deflate();
  847. break;
  848. case 14:
  849. test_cipher ("cast5", MODE_ECB, ENCRYPT, cast5_enc_tv_template, CAST5_ENC_TEST_VECTORS);
  850. test_cipher ("cast5", MODE_ECB, DECRYPT, cast5_dec_tv_template, CAST5_DEC_TEST_VECTORS);
  851. break;
  852. case 15:
  853. test_cipher ("cast6", MODE_ECB, ENCRYPT, cast6_enc_tv_template, CAST6_ENC_TEST_VECTORS);
  854. test_cipher ("cast6", MODE_ECB, DECRYPT, cast6_dec_tv_template, CAST6_DEC_TEST_VECTORS);
  855. break;
  856. case 16:
  857. test_cipher ("arc4", MODE_ECB, ENCRYPT, arc4_enc_tv_template, ARC4_ENC_TEST_VECTORS);
  858. test_cipher ("arc4", MODE_ECB, DECRYPT, arc4_dec_tv_template, ARC4_DEC_TEST_VECTORS);
  859. break;
  860. case 17:
  861. test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
  862. break;
  863. case 18:
  864. test_crc32c();
  865. break;
  866. case 19:
  867. test_cipher ("tea", MODE_ECB, ENCRYPT, tea_enc_tv_template, TEA_ENC_TEST_VECTORS);
  868. test_cipher ("tea", MODE_ECB, DECRYPT, tea_dec_tv_template, TEA_DEC_TEST_VECTORS);
  869. break;
  870. case 20:
  871. test_cipher ("xtea", MODE_ECB, ENCRYPT, xtea_enc_tv_template, XTEA_ENC_TEST_VECTORS);
  872. test_cipher ("xtea", MODE_ECB, DECRYPT, xtea_dec_tv_template, XTEA_DEC_TEST_VECTORS);
  873. break;
  874. case 21:
  875. test_cipher ("khazad", MODE_ECB, ENCRYPT, khazad_enc_tv_template, KHAZAD_ENC_TEST_VECTORS);
  876. test_cipher ("khazad", MODE_ECB, DECRYPT, khazad_dec_tv_template, KHAZAD_DEC_TEST_VECTORS);
  877. break;
  878. case 22:
  879. test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
  880. break;
  881. case 23:
  882. test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
  883. break;
  884. case 24:
  885. test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
  886. break;
  887. case 25:
  888. test_cipher ("tnepres", MODE_ECB, ENCRYPT, tnepres_enc_tv_template, TNEPRES_ENC_TEST_VECTORS);
  889. test_cipher ("tnepres", MODE_ECB, DECRYPT, tnepres_dec_tv_template, TNEPRES_DEC_TEST_VECTORS);
  890. break;
  891. case 26:
  892. test_cipher ("anubis", MODE_ECB, ENCRYPT, anubis_enc_tv_template, ANUBIS_ENC_TEST_VECTORS);
  893. test_cipher ("anubis", MODE_ECB, DECRYPT, anubis_dec_tv_template, ANUBIS_DEC_TEST_VECTORS);
  894. test_cipher ("anubis", MODE_CBC, ENCRYPT, anubis_cbc_enc_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
  895. test_cipher ("anubis", MODE_CBC, DECRYPT, anubis_cbc_dec_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
  896. break;
  897. case 27:
  898. test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
  899. break;
  900. case 28:
  901. test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
  902. break;
  903. case 29:
  904. test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
  905. break;
  906. case 30:
  907. test_cipher ("xeta", MODE_ECB, ENCRYPT, xeta_enc_tv_template, XETA_ENC_TEST_VECTORS);
  908. test_cipher ("xeta", MODE_ECB, DECRYPT, xeta_dec_tv_template, XETA_DEC_TEST_VECTORS);
  909. break;
  910. #ifdef CONFIG_CRYPTO_HMAC
  911. case 100:
  912. test_hmac("md5", hmac_md5_tv_template, HMAC_MD5_TEST_VECTORS);
  913. break;
  914. case 101:
  915. test_hmac("sha1", hmac_sha1_tv_template, HMAC_SHA1_TEST_VECTORS);
  916. break;
  917. case 102:
  918. test_hmac("sha256", hmac_sha256_tv_template, HMAC_SHA256_TEST_VECTORS);
  919. break;
  920. #endif
  921. case 200:
  922. test_cipher_speed("aes", MODE_ECB, ENCRYPT, sec, NULL, 0,
  923. aes_speed_template);
  924. test_cipher_speed("aes", MODE_ECB, DECRYPT, sec, NULL, 0,
  925. aes_speed_template);
  926. test_cipher_speed("aes", MODE_CBC, ENCRYPT, sec, NULL, 0,
  927. aes_speed_template);
  928. test_cipher_speed("aes", MODE_CBC, DECRYPT, sec, NULL, 0,
  929. aes_speed_template);
  930. break;
  931. case 201:
  932. test_cipher_speed("des3_ede", MODE_ECB, ENCRYPT, sec,
  933. des3_ede_enc_tv_template,
  934. DES3_EDE_ENC_TEST_VECTORS,
  935. des3_ede_speed_template);
  936. test_cipher_speed("des3_ede", MODE_ECB, DECRYPT, sec,
  937. des3_ede_dec_tv_template,
  938. DES3_EDE_DEC_TEST_VECTORS,
  939. des3_ede_speed_template);
  940. test_cipher_speed("des3_ede", MODE_CBC, ENCRYPT, sec,
  941. des3_ede_enc_tv_template,
  942. DES3_EDE_ENC_TEST_VECTORS,
  943. des3_ede_speed_template);
  944. test_cipher_speed("des3_ede", MODE_CBC, DECRYPT, sec,
  945. des3_ede_dec_tv_template,
  946. DES3_EDE_DEC_TEST_VECTORS,
  947. des3_ede_speed_template);
  948. break;
  949. case 202:
  950. test_cipher_speed("twofish", MODE_ECB, ENCRYPT, sec, NULL, 0,
  951. twofish_speed_template);
  952. test_cipher_speed("twofish", MODE_ECB, DECRYPT, sec, NULL, 0,
  953. twofish_speed_template);
  954. test_cipher_speed("twofish", MODE_CBC, ENCRYPT, sec, NULL, 0,
  955. twofish_speed_template);
  956. test_cipher_speed("twofish", MODE_CBC, DECRYPT, sec, NULL, 0,
  957. twofish_speed_template);
  958. break;
  959. case 203:
  960. test_cipher_speed("blowfish", MODE_ECB, ENCRYPT, sec, NULL, 0,
  961. blowfish_speed_template);
  962. test_cipher_speed("blowfish", MODE_ECB, DECRYPT, sec, NULL, 0,
  963. blowfish_speed_template);
  964. test_cipher_speed("blowfish", MODE_CBC, ENCRYPT, sec, NULL, 0,
  965. blowfish_speed_template);
  966. test_cipher_speed("blowfish", MODE_CBC, DECRYPT, sec, NULL, 0,
  967. blowfish_speed_template);
  968. break;
  969. case 204:
  970. test_cipher_speed("des", MODE_ECB, ENCRYPT, sec, NULL, 0,
  971. des_speed_template);
  972. test_cipher_speed("des", MODE_ECB, DECRYPT, sec, NULL, 0,
  973. des_speed_template);
  974. test_cipher_speed("des", MODE_CBC, ENCRYPT, sec, NULL, 0,
  975. des_speed_template);
  976. test_cipher_speed("des", MODE_CBC, DECRYPT, sec, NULL, 0,
  977. des_speed_template);
  978. break;
  979. case 300:
  980. /* fall through */
  981. case 301:
  982. test_digest_speed("md4", sec, generic_digest_speed_template);
  983. if (mode > 300 && mode < 400) break;
  984. case 302:
  985. test_digest_speed("md5", sec, generic_digest_speed_template);
  986. if (mode > 300 && mode < 400) break;
  987. case 303:
  988. test_digest_speed("sha1", sec, generic_digest_speed_template);
  989. if (mode > 300 && mode < 400) break;
  990. case 304:
  991. test_digest_speed("sha256", sec, generic_digest_speed_template);
  992. if (mode > 300 && mode < 400) break;
  993. case 305:
  994. test_digest_speed("sha384", sec, generic_digest_speed_template);
  995. if (mode > 300 && mode < 400) break;
  996. case 306:
  997. test_digest_speed("sha512", sec, generic_digest_speed_template);
  998. if (mode > 300 && mode < 400) break;
  999. case 307:
  1000. test_digest_speed("wp256", sec, generic_digest_speed_template);
  1001. if (mode > 300 && mode < 400) break;
  1002. case 308:
  1003. test_digest_speed("wp384", sec, generic_digest_speed_template);
  1004. if (mode > 300 && mode < 400) break;
  1005. case 309:
  1006. test_digest_speed("wp512", sec, generic_digest_speed_template);
  1007. if (mode > 300 && mode < 400) break;
  1008. case 310:
  1009. test_digest_speed("tgr128", sec, generic_digest_speed_template);
  1010. if (mode > 300 && mode < 400) break;
  1011. case 311:
  1012. test_digest_speed("tgr160", sec, generic_digest_speed_template);
  1013. if (mode > 300 && mode < 400) break;
  1014. case 312:
  1015. test_digest_speed("tgr192", sec, generic_digest_speed_template);
  1016. if (mode > 300 && mode < 400) break;
  1017. case 399:
  1018. break;
  1019. case 1000:
  1020. test_available();
  1021. break;
  1022. default:
  1023. /* useful for debugging */
  1024. printk("not testing anything\n");
  1025. break;
  1026. }
  1027. }
  1028. static int __init init(void)
  1029. {
  1030. tvmem = kmalloc(TVMEMSIZE, GFP_KERNEL);
  1031. if (tvmem == NULL)
  1032. return -ENOMEM;
  1033. xbuf = kmalloc(XBUFSIZE, GFP_KERNEL);
  1034. if (xbuf == NULL) {
  1035. kfree(tvmem);
  1036. return -ENOMEM;
  1037. }
  1038. do_test();
  1039. kfree(xbuf);
  1040. kfree(tvmem);
  1041. /* We intentionaly return -EAGAIN to prevent keeping
  1042. * the module. It does all its work from init()
  1043. * and doesn't offer any runtime functionality
  1044. * => we don't need it in the memory, do we?
  1045. * -- mludvig
  1046. */
  1047. return -EAGAIN;
  1048. }
  1049. /*
  1050. * If an init function is provided, an exit function must also be provided
  1051. * to allow module unload.
  1052. */
  1053. static void __exit fini(void) { }
  1054. module_init(init);
  1055. module_exit(fini);
  1056. module_param(mode, int, 0);
  1057. module_param(sec, uint, 0);
  1058. MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
  1059. "(defaults to zero which uses CPU cycles instead)");
  1060. MODULE_LICENSE("GPL");
  1061. MODULE_DESCRIPTION("Quick & dirty crypto testing module");
  1062. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");