tcrypt.c 33 KB

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