tcrypt.c 32 KB

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