tcrypt.c 37 KB

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