tcrypt.c 34 KB

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