tcrypt.c 35 KB

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