tcrypt.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  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. * Copyright (c) 2007 Nokia Siemens Networks
  10. *
  11. * Updated RFC4106 AES-GCM testing.
  12. * Authors: Aidan O'Mahony (aidan.o.mahony@intel.com)
  13. * Adrian Hoban <adrian.hoban@intel.com>
  14. * Gabriele Paoloni <gabriele.paoloni@intel.com>
  15. * Tadeusz Struk (tadeusz.struk@intel.com)
  16. * Copyright (c) 2010, Intel Corporation.
  17. *
  18. * This program is free software; you can redistribute it and/or modify it
  19. * under the terms of the GNU General Public License as published by the Free
  20. * Software Foundation; either version 2 of the License, or (at your option)
  21. * any later version.
  22. *
  23. */
  24. #include <crypto/hash.h>
  25. #include <linux/err.h>
  26. #include <linux/init.h>
  27. #include <linux/gfp.h>
  28. #include <linux/module.h>
  29. #include <linux/scatterlist.h>
  30. #include <linux/string.h>
  31. #include <linux/moduleparam.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/timex.h>
  34. #include <linux/interrupt.h>
  35. #include "tcrypt.h"
  36. #include "internal.h"
  37. /*
  38. * Need slab memory for testing (size in number of pages).
  39. */
  40. #define TVMEMSIZE 4
  41. /*
  42. * Used by test_cipher_speed()
  43. */
  44. #define ENCRYPT 1
  45. #define DECRYPT 0
  46. /*
  47. * Used by test_cipher_speed()
  48. */
  49. static unsigned int sec;
  50. static char *alg = NULL;
  51. static u32 type;
  52. static u32 mask;
  53. static int mode;
  54. static char *tvmem[TVMEMSIZE];
  55. static char *check[] = {
  56. "des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
  57. "blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
  58. "cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
  59. "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", "fcrypt",
  60. "camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
  61. "lzo", "cts", "zlib", NULL
  62. };
  63. static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc,
  64. struct scatterlist *sg, int blen, int sec)
  65. {
  66. unsigned long start, end;
  67. int bcount;
  68. int ret;
  69. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  70. time_before(jiffies, end); bcount++) {
  71. if (enc)
  72. ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
  73. else
  74. ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
  75. if (ret)
  76. return ret;
  77. }
  78. printk("%d operations in %d seconds (%ld bytes)\n",
  79. bcount, sec, (long)bcount * blen);
  80. return 0;
  81. }
  82. static int test_cipher_cycles(struct blkcipher_desc *desc, int enc,
  83. struct scatterlist *sg, int blen)
  84. {
  85. unsigned long cycles = 0;
  86. int ret = 0;
  87. int i;
  88. local_bh_disable();
  89. local_irq_disable();
  90. /* Warm-up run. */
  91. for (i = 0; i < 4; i++) {
  92. if (enc)
  93. ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
  94. else
  95. ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
  96. if (ret)
  97. goto out;
  98. }
  99. /* The real thing. */
  100. for (i = 0; i < 8; i++) {
  101. cycles_t start, end;
  102. start = get_cycles();
  103. if (enc)
  104. ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
  105. else
  106. ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
  107. end = get_cycles();
  108. if (ret)
  109. goto out;
  110. cycles += end - start;
  111. }
  112. out:
  113. local_irq_enable();
  114. local_bh_enable();
  115. if (ret == 0)
  116. printk("1 operation in %lu cycles (%d bytes)\n",
  117. (cycles + 4) / 8, blen);
  118. return ret;
  119. }
  120. static u32 block_sizes[] = { 16, 64, 256, 1024, 8192, 0 };
  121. static void test_cipher_speed(const char *algo, int enc, unsigned int sec,
  122. struct cipher_speed_template *template,
  123. unsigned int tcount, u8 *keysize)
  124. {
  125. unsigned int ret, i, j, iv_len;
  126. const char *key;
  127. char iv[128];
  128. struct crypto_blkcipher *tfm;
  129. struct blkcipher_desc desc;
  130. const char *e;
  131. u32 *b_size;
  132. if (enc == ENCRYPT)
  133. e = "encryption";
  134. else
  135. e = "decryption";
  136. printk("\ntesting speed of %s %s\n", algo, e);
  137. tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
  138. if (IS_ERR(tfm)) {
  139. printk("failed to load transform for %s: %ld\n", algo,
  140. PTR_ERR(tfm));
  141. return;
  142. }
  143. desc.tfm = tfm;
  144. desc.flags = 0;
  145. i = 0;
  146. do {
  147. b_size = block_sizes;
  148. do {
  149. struct scatterlist sg[TVMEMSIZE];
  150. if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
  151. printk("template (%u) too big for "
  152. "tvmem (%lu)\n", *keysize + *b_size,
  153. TVMEMSIZE * PAGE_SIZE);
  154. goto out;
  155. }
  156. printk("test %u (%d bit key, %d byte blocks): ", i,
  157. *keysize * 8, *b_size);
  158. memset(tvmem[0], 0xff, PAGE_SIZE);
  159. /* set key, plain text and IV */
  160. key = tvmem[0];
  161. for (j = 0; j < tcount; j++) {
  162. if (template[j].klen == *keysize) {
  163. key = template[j].key;
  164. break;
  165. }
  166. }
  167. ret = crypto_blkcipher_setkey(tfm, key, *keysize);
  168. if (ret) {
  169. printk("setkey() failed flags=%x\n",
  170. crypto_blkcipher_get_flags(tfm));
  171. goto out;
  172. }
  173. sg_init_table(sg, TVMEMSIZE);
  174. sg_set_buf(sg, tvmem[0] + *keysize,
  175. PAGE_SIZE - *keysize);
  176. for (j = 1; j < TVMEMSIZE; j++) {
  177. sg_set_buf(sg + j, tvmem[j], PAGE_SIZE);
  178. memset (tvmem[j], 0xff, PAGE_SIZE);
  179. }
  180. iv_len = crypto_blkcipher_ivsize(tfm);
  181. if (iv_len) {
  182. memset(&iv, 0xff, iv_len);
  183. crypto_blkcipher_set_iv(tfm, iv, iv_len);
  184. }
  185. if (sec)
  186. ret = test_cipher_jiffies(&desc, enc, sg,
  187. *b_size, sec);
  188. else
  189. ret = test_cipher_cycles(&desc, enc, sg,
  190. *b_size);
  191. if (ret) {
  192. printk("%s() failed flags=%x\n", e, desc.flags);
  193. break;
  194. }
  195. b_size++;
  196. i++;
  197. } while (*b_size);
  198. keysize++;
  199. } while (*keysize);
  200. out:
  201. crypto_free_blkcipher(tfm);
  202. }
  203. static int test_hash_jiffies_digest(struct hash_desc *desc,
  204. struct scatterlist *sg, int blen,
  205. char *out, int sec)
  206. {
  207. unsigned long start, end;
  208. int bcount;
  209. int ret;
  210. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  211. time_before(jiffies, end); bcount++) {
  212. ret = crypto_hash_digest(desc, sg, blen, out);
  213. if (ret)
  214. return ret;
  215. }
  216. printk("%6u opers/sec, %9lu bytes/sec\n",
  217. bcount / sec, ((long)bcount * blen) / sec);
  218. return 0;
  219. }
  220. static int test_hash_jiffies(struct hash_desc *desc, struct scatterlist *sg,
  221. int blen, int plen, char *out, int sec)
  222. {
  223. unsigned long start, end;
  224. int bcount, pcount;
  225. int ret;
  226. if (plen == blen)
  227. return test_hash_jiffies_digest(desc, sg, blen, out, sec);
  228. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  229. time_before(jiffies, end); bcount++) {
  230. ret = crypto_hash_init(desc);
  231. if (ret)
  232. return ret;
  233. for (pcount = 0; pcount < blen; pcount += plen) {
  234. ret = crypto_hash_update(desc, sg, plen);
  235. if (ret)
  236. return ret;
  237. }
  238. /* we assume there is enough space in 'out' for the result */
  239. ret = crypto_hash_final(desc, out);
  240. if (ret)
  241. return ret;
  242. }
  243. printk("%6u opers/sec, %9lu bytes/sec\n",
  244. bcount / sec, ((long)bcount * blen) / sec);
  245. return 0;
  246. }
  247. static int test_hash_cycles_digest(struct hash_desc *desc,
  248. struct scatterlist *sg, int blen, char *out)
  249. {
  250. unsigned long cycles = 0;
  251. int i;
  252. int ret;
  253. local_bh_disable();
  254. local_irq_disable();
  255. /* Warm-up run. */
  256. for (i = 0; i < 4; i++) {
  257. ret = crypto_hash_digest(desc, sg, blen, out);
  258. if (ret)
  259. goto out;
  260. }
  261. /* The real thing. */
  262. for (i = 0; i < 8; i++) {
  263. cycles_t start, end;
  264. start = get_cycles();
  265. ret = crypto_hash_digest(desc, sg, blen, out);
  266. if (ret)
  267. goto out;
  268. end = get_cycles();
  269. cycles += end - start;
  270. }
  271. out:
  272. local_irq_enable();
  273. local_bh_enable();
  274. if (ret)
  275. return ret;
  276. printk("%6lu cycles/operation, %4lu cycles/byte\n",
  277. cycles / 8, cycles / (8 * blen));
  278. return 0;
  279. }
  280. static int test_hash_cycles(struct hash_desc *desc, struct scatterlist *sg,
  281. int blen, int plen, char *out)
  282. {
  283. unsigned long cycles = 0;
  284. int i, pcount;
  285. int ret;
  286. if (plen == blen)
  287. return test_hash_cycles_digest(desc, sg, blen, out);
  288. local_bh_disable();
  289. local_irq_disable();
  290. /* Warm-up run. */
  291. for (i = 0; i < 4; i++) {
  292. ret = crypto_hash_init(desc);
  293. if (ret)
  294. goto out;
  295. for (pcount = 0; pcount < blen; pcount += plen) {
  296. ret = crypto_hash_update(desc, sg, plen);
  297. if (ret)
  298. goto out;
  299. }
  300. ret = crypto_hash_final(desc, out);
  301. if (ret)
  302. goto out;
  303. }
  304. /* The real thing. */
  305. for (i = 0; i < 8; i++) {
  306. cycles_t start, end;
  307. start = get_cycles();
  308. ret = crypto_hash_init(desc);
  309. if (ret)
  310. goto out;
  311. for (pcount = 0; pcount < blen; pcount += plen) {
  312. ret = crypto_hash_update(desc, sg, plen);
  313. if (ret)
  314. goto out;
  315. }
  316. ret = crypto_hash_final(desc, out);
  317. if (ret)
  318. goto out;
  319. end = get_cycles();
  320. cycles += end - start;
  321. }
  322. out:
  323. local_irq_enable();
  324. local_bh_enable();
  325. if (ret)
  326. return ret;
  327. printk("%6lu cycles/operation, %4lu cycles/byte\n",
  328. cycles / 8, cycles / (8 * blen));
  329. return 0;
  330. }
  331. static void test_hash_sg_init(struct scatterlist *sg)
  332. {
  333. int i;
  334. sg_init_table(sg, TVMEMSIZE);
  335. for (i = 0; i < TVMEMSIZE; i++) {
  336. sg_set_buf(sg + i, tvmem[i], PAGE_SIZE);
  337. memset(tvmem[i], 0xff, PAGE_SIZE);
  338. }
  339. }
  340. static void test_hash_speed(const char *algo, unsigned int sec,
  341. struct hash_speed *speed)
  342. {
  343. struct scatterlist sg[TVMEMSIZE];
  344. struct crypto_hash *tfm;
  345. struct hash_desc desc;
  346. static char output[1024];
  347. int i;
  348. int ret;
  349. printk(KERN_INFO "\ntesting speed of %s\n", algo);
  350. tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
  351. if (IS_ERR(tfm)) {
  352. printk(KERN_ERR "failed to load transform for %s: %ld\n", algo,
  353. PTR_ERR(tfm));
  354. return;
  355. }
  356. desc.tfm = tfm;
  357. desc.flags = 0;
  358. if (crypto_hash_digestsize(tfm) > sizeof(output)) {
  359. printk(KERN_ERR "digestsize(%u) > outputbuffer(%zu)\n",
  360. crypto_hash_digestsize(tfm), sizeof(output));
  361. goto out;
  362. }
  363. test_hash_sg_init(sg);
  364. for (i = 0; speed[i].blen != 0; i++) {
  365. if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
  366. printk(KERN_ERR
  367. "template (%u) too big for tvmem (%lu)\n",
  368. speed[i].blen, TVMEMSIZE * PAGE_SIZE);
  369. goto out;
  370. }
  371. if (speed[i].klen)
  372. crypto_hash_setkey(tfm, tvmem[0], speed[i].klen);
  373. printk(KERN_INFO "test%3u "
  374. "(%5u byte blocks,%5u bytes per update,%4u updates): ",
  375. i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
  376. if (sec)
  377. ret = test_hash_jiffies(&desc, sg, speed[i].blen,
  378. speed[i].plen, output, sec);
  379. else
  380. ret = test_hash_cycles(&desc, sg, speed[i].blen,
  381. speed[i].plen, output);
  382. if (ret) {
  383. printk(KERN_ERR "hashing failed ret=%d\n", ret);
  384. break;
  385. }
  386. }
  387. out:
  388. crypto_free_hash(tfm);
  389. }
  390. struct tcrypt_result {
  391. struct completion completion;
  392. int err;
  393. };
  394. static void tcrypt_complete(struct crypto_async_request *req, int err)
  395. {
  396. struct tcrypt_result *res = req->data;
  397. if (err == -EINPROGRESS)
  398. return;
  399. res->err = err;
  400. complete(&res->completion);
  401. }
  402. static inline int do_one_ahash_op(struct ahash_request *req, int ret)
  403. {
  404. if (ret == -EINPROGRESS || ret == -EBUSY) {
  405. struct tcrypt_result *tr = req->base.data;
  406. ret = wait_for_completion_interruptible(&tr->completion);
  407. if (!ret)
  408. ret = tr->err;
  409. INIT_COMPLETION(tr->completion);
  410. }
  411. return ret;
  412. }
  413. static int test_ahash_jiffies_digest(struct ahash_request *req, int blen,
  414. char *out, int sec)
  415. {
  416. unsigned long start, end;
  417. int bcount;
  418. int ret;
  419. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  420. time_before(jiffies, end); bcount++) {
  421. ret = do_one_ahash_op(req, crypto_ahash_digest(req));
  422. if (ret)
  423. return ret;
  424. }
  425. printk("%6u opers/sec, %9lu bytes/sec\n",
  426. bcount / sec, ((long)bcount * blen) / sec);
  427. return 0;
  428. }
  429. static int test_ahash_jiffies(struct ahash_request *req, int blen,
  430. int plen, char *out, int sec)
  431. {
  432. unsigned long start, end;
  433. int bcount, pcount;
  434. int ret;
  435. if (plen == blen)
  436. return test_ahash_jiffies_digest(req, blen, out, sec);
  437. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  438. time_before(jiffies, end); bcount++) {
  439. ret = crypto_ahash_init(req);
  440. if (ret)
  441. return ret;
  442. for (pcount = 0; pcount < blen; pcount += plen) {
  443. ret = do_one_ahash_op(req, crypto_ahash_update(req));
  444. if (ret)
  445. return ret;
  446. }
  447. /* we assume there is enough space in 'out' for the result */
  448. ret = do_one_ahash_op(req, crypto_ahash_final(req));
  449. if (ret)
  450. return ret;
  451. }
  452. pr_cont("%6u opers/sec, %9lu bytes/sec\n",
  453. bcount / sec, ((long)bcount * blen) / sec);
  454. return 0;
  455. }
  456. static int test_ahash_cycles_digest(struct ahash_request *req, int blen,
  457. char *out)
  458. {
  459. unsigned long cycles = 0;
  460. int ret, i;
  461. /* Warm-up run. */
  462. for (i = 0; i < 4; i++) {
  463. ret = do_one_ahash_op(req, crypto_ahash_digest(req));
  464. if (ret)
  465. goto out;
  466. }
  467. /* The real thing. */
  468. for (i = 0; i < 8; i++) {
  469. cycles_t start, end;
  470. start = get_cycles();
  471. ret = do_one_ahash_op(req, crypto_ahash_digest(req));
  472. if (ret)
  473. goto out;
  474. end = get_cycles();
  475. cycles += end - start;
  476. }
  477. out:
  478. if (ret)
  479. return ret;
  480. pr_cont("%6lu cycles/operation, %4lu cycles/byte\n",
  481. cycles / 8, cycles / (8 * blen));
  482. return 0;
  483. }
  484. static int test_ahash_cycles(struct ahash_request *req, int blen,
  485. int plen, char *out)
  486. {
  487. unsigned long cycles = 0;
  488. int i, pcount, ret;
  489. if (plen == blen)
  490. return test_ahash_cycles_digest(req, blen, out);
  491. /* Warm-up run. */
  492. for (i = 0; i < 4; i++) {
  493. ret = crypto_ahash_init(req);
  494. if (ret)
  495. goto out;
  496. for (pcount = 0; pcount < blen; pcount += plen) {
  497. ret = do_one_ahash_op(req, crypto_ahash_update(req));
  498. if (ret)
  499. goto out;
  500. }
  501. ret = do_one_ahash_op(req, crypto_ahash_final(req));
  502. if (ret)
  503. goto out;
  504. }
  505. /* The real thing. */
  506. for (i = 0; i < 8; i++) {
  507. cycles_t start, end;
  508. start = get_cycles();
  509. ret = crypto_ahash_init(req);
  510. if (ret)
  511. goto out;
  512. for (pcount = 0; pcount < blen; pcount += plen) {
  513. ret = do_one_ahash_op(req, crypto_ahash_update(req));
  514. if (ret)
  515. goto out;
  516. }
  517. ret = do_one_ahash_op(req, crypto_ahash_final(req));
  518. if (ret)
  519. goto out;
  520. end = get_cycles();
  521. cycles += end - start;
  522. }
  523. out:
  524. if (ret)
  525. return ret;
  526. pr_cont("%6lu cycles/operation, %4lu cycles/byte\n",
  527. cycles / 8, cycles / (8 * blen));
  528. return 0;
  529. }
  530. static void test_ahash_speed(const char *algo, unsigned int sec,
  531. struct hash_speed *speed)
  532. {
  533. struct scatterlist sg[TVMEMSIZE];
  534. struct tcrypt_result tresult;
  535. struct ahash_request *req;
  536. struct crypto_ahash *tfm;
  537. static char output[1024];
  538. int i, ret;
  539. printk(KERN_INFO "\ntesting speed of async %s\n", algo);
  540. tfm = crypto_alloc_ahash(algo, 0, 0);
  541. if (IS_ERR(tfm)) {
  542. pr_err("failed to load transform for %s: %ld\n",
  543. algo, PTR_ERR(tfm));
  544. return;
  545. }
  546. if (crypto_ahash_digestsize(tfm) > sizeof(output)) {
  547. pr_err("digestsize(%u) > outputbuffer(%zu)\n",
  548. crypto_ahash_digestsize(tfm), sizeof(output));
  549. goto out;
  550. }
  551. test_hash_sg_init(sg);
  552. req = ahash_request_alloc(tfm, GFP_KERNEL);
  553. if (!req) {
  554. pr_err("ahash request allocation failure\n");
  555. goto out;
  556. }
  557. init_completion(&tresult.completion);
  558. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  559. tcrypt_complete, &tresult);
  560. for (i = 0; speed[i].blen != 0; i++) {
  561. if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
  562. pr_err("template (%u) too big for tvmem (%lu)\n",
  563. speed[i].blen, TVMEMSIZE * PAGE_SIZE);
  564. break;
  565. }
  566. pr_info("test%3u "
  567. "(%5u byte blocks,%5u bytes per update,%4u updates): ",
  568. i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
  569. ahash_request_set_crypt(req, sg, output, speed[i].plen);
  570. if (sec)
  571. ret = test_ahash_jiffies(req, speed[i].blen,
  572. speed[i].plen, output, sec);
  573. else
  574. ret = test_ahash_cycles(req, speed[i].blen,
  575. speed[i].plen, output);
  576. if (ret) {
  577. pr_err("hashing failed ret=%d\n", ret);
  578. break;
  579. }
  580. }
  581. ahash_request_free(req);
  582. out:
  583. crypto_free_ahash(tfm);
  584. }
  585. static void test_available(void)
  586. {
  587. char **name = check;
  588. while (*name) {
  589. printk("alg %s ", *name);
  590. printk(crypto_has_alg(*name, 0, 0) ?
  591. "found\n" : "not found\n");
  592. name++;
  593. }
  594. }
  595. static inline int tcrypt_test(const char *alg)
  596. {
  597. int ret;
  598. ret = alg_test(alg, alg, 0, 0);
  599. /* non-fips algs return -EINVAL in fips mode */
  600. if (fips_enabled && ret == -EINVAL)
  601. ret = 0;
  602. return ret;
  603. }
  604. static int do_test(int m)
  605. {
  606. int i;
  607. int ret = 0;
  608. switch (m) {
  609. case 0:
  610. for (i = 1; i < 200; i++)
  611. ret += do_test(i);
  612. break;
  613. case 1:
  614. ret += tcrypt_test("md5");
  615. break;
  616. case 2:
  617. ret += tcrypt_test("sha1");
  618. break;
  619. case 3:
  620. ret += tcrypt_test("ecb(des)");
  621. ret += tcrypt_test("cbc(des)");
  622. break;
  623. case 4:
  624. ret += tcrypt_test("ecb(des3_ede)");
  625. ret += tcrypt_test("cbc(des3_ede)");
  626. break;
  627. case 5:
  628. ret += tcrypt_test("md4");
  629. break;
  630. case 6:
  631. ret += tcrypt_test("sha256");
  632. break;
  633. case 7:
  634. ret += tcrypt_test("ecb(blowfish)");
  635. ret += tcrypt_test("cbc(blowfish)");
  636. break;
  637. case 8:
  638. ret += tcrypt_test("ecb(twofish)");
  639. ret += tcrypt_test("cbc(twofish)");
  640. break;
  641. case 9:
  642. ret += tcrypt_test("ecb(serpent)");
  643. break;
  644. case 10:
  645. ret += tcrypt_test("ecb(aes)");
  646. ret += tcrypt_test("cbc(aes)");
  647. ret += tcrypt_test("lrw(aes)");
  648. ret += tcrypt_test("xts(aes)");
  649. ret += tcrypt_test("ctr(aes)");
  650. ret += tcrypt_test("rfc3686(ctr(aes))");
  651. break;
  652. case 11:
  653. ret += tcrypt_test("sha384");
  654. break;
  655. case 12:
  656. ret += tcrypt_test("sha512");
  657. break;
  658. case 13:
  659. ret += tcrypt_test("deflate");
  660. break;
  661. case 14:
  662. ret += tcrypt_test("ecb(cast5)");
  663. break;
  664. case 15:
  665. ret += tcrypt_test("ecb(cast6)");
  666. break;
  667. case 16:
  668. ret += tcrypt_test("ecb(arc4)");
  669. break;
  670. case 17:
  671. ret += tcrypt_test("michael_mic");
  672. break;
  673. case 18:
  674. ret += tcrypt_test("crc32c");
  675. break;
  676. case 19:
  677. ret += tcrypt_test("ecb(tea)");
  678. break;
  679. case 20:
  680. ret += tcrypt_test("ecb(xtea)");
  681. break;
  682. case 21:
  683. ret += tcrypt_test("ecb(khazad)");
  684. break;
  685. case 22:
  686. ret += tcrypt_test("wp512");
  687. break;
  688. case 23:
  689. ret += tcrypt_test("wp384");
  690. break;
  691. case 24:
  692. ret += tcrypt_test("wp256");
  693. break;
  694. case 25:
  695. ret += tcrypt_test("ecb(tnepres)");
  696. break;
  697. case 26:
  698. ret += tcrypt_test("ecb(anubis)");
  699. ret += tcrypt_test("cbc(anubis)");
  700. break;
  701. case 27:
  702. ret += tcrypt_test("tgr192");
  703. break;
  704. case 28:
  705. ret += tcrypt_test("tgr160");
  706. break;
  707. case 29:
  708. ret += tcrypt_test("tgr128");
  709. break;
  710. case 30:
  711. ret += tcrypt_test("ecb(xeta)");
  712. break;
  713. case 31:
  714. ret += tcrypt_test("pcbc(fcrypt)");
  715. break;
  716. case 32:
  717. ret += tcrypt_test("ecb(camellia)");
  718. ret += tcrypt_test("cbc(camellia)");
  719. break;
  720. case 33:
  721. ret += tcrypt_test("sha224");
  722. break;
  723. case 34:
  724. ret += tcrypt_test("salsa20");
  725. break;
  726. case 35:
  727. ret += tcrypt_test("gcm(aes)");
  728. break;
  729. case 36:
  730. ret += tcrypt_test("lzo");
  731. break;
  732. case 37:
  733. ret += tcrypt_test("ccm(aes)");
  734. break;
  735. case 38:
  736. ret += tcrypt_test("cts(cbc(aes))");
  737. break;
  738. case 39:
  739. ret += tcrypt_test("rmd128");
  740. break;
  741. case 40:
  742. ret += tcrypt_test("rmd160");
  743. break;
  744. case 41:
  745. ret += tcrypt_test("rmd256");
  746. break;
  747. case 42:
  748. ret += tcrypt_test("rmd320");
  749. break;
  750. case 43:
  751. ret += tcrypt_test("ecb(seed)");
  752. break;
  753. case 44:
  754. ret += tcrypt_test("zlib");
  755. break;
  756. case 45:
  757. ret += tcrypt_test("rfc4309(ccm(aes))");
  758. break;
  759. case 100:
  760. ret += tcrypt_test("hmac(md5)");
  761. break;
  762. case 101:
  763. ret += tcrypt_test("hmac(sha1)");
  764. break;
  765. case 102:
  766. ret += tcrypt_test("hmac(sha256)");
  767. break;
  768. case 103:
  769. ret += tcrypt_test("hmac(sha384)");
  770. break;
  771. case 104:
  772. ret += tcrypt_test("hmac(sha512)");
  773. break;
  774. case 105:
  775. ret += tcrypt_test("hmac(sha224)");
  776. break;
  777. case 106:
  778. ret += tcrypt_test("xcbc(aes)");
  779. break;
  780. case 107:
  781. ret += tcrypt_test("hmac(rmd128)");
  782. break;
  783. case 108:
  784. ret += tcrypt_test("hmac(rmd160)");
  785. break;
  786. case 109:
  787. ret += tcrypt_test("vmac(aes)");
  788. break;
  789. case 150:
  790. ret += tcrypt_test("ansi_cprng");
  791. break;
  792. case 151:
  793. ret += tcrypt_test("rfc4106(gcm(aes))");
  794. break;
  795. case 200:
  796. test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
  797. speed_template_16_24_32);
  798. test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
  799. speed_template_16_24_32);
  800. test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
  801. speed_template_16_24_32);
  802. test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
  803. speed_template_16_24_32);
  804. test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
  805. speed_template_32_40_48);
  806. test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
  807. speed_template_32_40_48);
  808. test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
  809. speed_template_32_48_64);
  810. test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
  811. speed_template_32_48_64);
  812. break;
  813. case 201:
  814. test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
  815. des3_speed_template, DES3_SPEED_VECTORS,
  816. speed_template_24);
  817. test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
  818. des3_speed_template, DES3_SPEED_VECTORS,
  819. speed_template_24);
  820. test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
  821. des3_speed_template, DES3_SPEED_VECTORS,
  822. speed_template_24);
  823. test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
  824. des3_speed_template, DES3_SPEED_VECTORS,
  825. speed_template_24);
  826. break;
  827. case 202:
  828. test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
  829. speed_template_16_24_32);
  830. test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
  831. speed_template_16_24_32);
  832. test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
  833. speed_template_16_24_32);
  834. test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
  835. speed_template_16_24_32);
  836. break;
  837. case 203:
  838. test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
  839. speed_template_8_32);
  840. test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
  841. speed_template_8_32);
  842. test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
  843. speed_template_8_32);
  844. test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
  845. speed_template_8_32);
  846. break;
  847. case 204:
  848. test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
  849. speed_template_8);
  850. test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
  851. speed_template_8);
  852. test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
  853. speed_template_8);
  854. test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
  855. speed_template_8);
  856. break;
  857. case 205:
  858. test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
  859. speed_template_16_24_32);
  860. test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
  861. speed_template_16_24_32);
  862. test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
  863. speed_template_16_24_32);
  864. test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
  865. speed_template_16_24_32);
  866. break;
  867. case 206:
  868. test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
  869. speed_template_16_32);
  870. break;
  871. case 300:
  872. /* fall through */
  873. case 301:
  874. test_hash_speed("md4", sec, generic_hash_speed_template);
  875. if (mode > 300 && mode < 400) break;
  876. case 302:
  877. test_hash_speed("md5", sec, generic_hash_speed_template);
  878. if (mode > 300 && mode < 400) break;
  879. case 303:
  880. test_hash_speed("sha1", sec, generic_hash_speed_template);
  881. if (mode > 300 && mode < 400) break;
  882. case 304:
  883. test_hash_speed("sha256", sec, generic_hash_speed_template);
  884. if (mode > 300 && mode < 400) break;
  885. case 305:
  886. test_hash_speed("sha384", sec, generic_hash_speed_template);
  887. if (mode > 300 && mode < 400) break;
  888. case 306:
  889. test_hash_speed("sha512", sec, generic_hash_speed_template);
  890. if (mode > 300 && mode < 400) break;
  891. case 307:
  892. test_hash_speed("wp256", sec, generic_hash_speed_template);
  893. if (mode > 300 && mode < 400) break;
  894. case 308:
  895. test_hash_speed("wp384", sec, generic_hash_speed_template);
  896. if (mode > 300 && mode < 400) break;
  897. case 309:
  898. test_hash_speed("wp512", sec, generic_hash_speed_template);
  899. if (mode > 300 && mode < 400) break;
  900. case 310:
  901. test_hash_speed("tgr128", sec, generic_hash_speed_template);
  902. if (mode > 300 && mode < 400) break;
  903. case 311:
  904. test_hash_speed("tgr160", sec, generic_hash_speed_template);
  905. if (mode > 300 && mode < 400) break;
  906. case 312:
  907. test_hash_speed("tgr192", sec, generic_hash_speed_template);
  908. if (mode > 300 && mode < 400) break;
  909. case 313:
  910. test_hash_speed("sha224", sec, generic_hash_speed_template);
  911. if (mode > 300 && mode < 400) break;
  912. case 314:
  913. test_hash_speed("rmd128", sec, generic_hash_speed_template);
  914. if (mode > 300 && mode < 400) break;
  915. case 315:
  916. test_hash_speed("rmd160", sec, generic_hash_speed_template);
  917. if (mode > 300 && mode < 400) break;
  918. case 316:
  919. test_hash_speed("rmd256", sec, generic_hash_speed_template);
  920. if (mode > 300 && mode < 400) break;
  921. case 317:
  922. test_hash_speed("rmd320", sec, generic_hash_speed_template);
  923. if (mode > 300 && mode < 400) break;
  924. case 318:
  925. test_hash_speed("ghash-generic", sec, hash_speed_template_16);
  926. if (mode > 300 && mode < 400) break;
  927. case 399:
  928. break;
  929. case 400:
  930. /* fall through */
  931. case 401:
  932. test_ahash_speed("md4", sec, generic_hash_speed_template);
  933. if (mode > 400 && mode < 500) break;
  934. case 402:
  935. test_ahash_speed("md5", sec, generic_hash_speed_template);
  936. if (mode > 400 && mode < 500) break;
  937. case 403:
  938. test_ahash_speed("sha1", sec, generic_hash_speed_template);
  939. if (mode > 400 && mode < 500) break;
  940. case 404:
  941. test_ahash_speed("sha256", sec, generic_hash_speed_template);
  942. if (mode > 400 && mode < 500) break;
  943. case 405:
  944. test_ahash_speed("sha384", sec, generic_hash_speed_template);
  945. if (mode > 400 && mode < 500) break;
  946. case 406:
  947. test_ahash_speed("sha512", sec, generic_hash_speed_template);
  948. if (mode > 400 && mode < 500) break;
  949. case 407:
  950. test_ahash_speed("wp256", sec, generic_hash_speed_template);
  951. if (mode > 400 && mode < 500) break;
  952. case 408:
  953. test_ahash_speed("wp384", sec, generic_hash_speed_template);
  954. if (mode > 400 && mode < 500) break;
  955. case 409:
  956. test_ahash_speed("wp512", sec, generic_hash_speed_template);
  957. if (mode > 400 && mode < 500) break;
  958. case 410:
  959. test_ahash_speed("tgr128", sec, generic_hash_speed_template);
  960. if (mode > 400 && mode < 500) break;
  961. case 411:
  962. test_ahash_speed("tgr160", sec, generic_hash_speed_template);
  963. if (mode > 400 && mode < 500) break;
  964. case 412:
  965. test_ahash_speed("tgr192", sec, generic_hash_speed_template);
  966. if (mode > 400 && mode < 500) break;
  967. case 413:
  968. test_ahash_speed("sha224", sec, generic_hash_speed_template);
  969. if (mode > 400 && mode < 500) break;
  970. case 414:
  971. test_ahash_speed("rmd128", sec, generic_hash_speed_template);
  972. if (mode > 400 && mode < 500) break;
  973. case 415:
  974. test_ahash_speed("rmd160", sec, generic_hash_speed_template);
  975. if (mode > 400 && mode < 500) break;
  976. case 416:
  977. test_ahash_speed("rmd256", sec, generic_hash_speed_template);
  978. if (mode > 400 && mode < 500) break;
  979. case 417:
  980. test_ahash_speed("rmd320", sec, generic_hash_speed_template);
  981. if (mode > 400 && mode < 500) break;
  982. case 499:
  983. break;
  984. case 1000:
  985. test_available();
  986. break;
  987. }
  988. return ret;
  989. }
  990. static int do_alg_test(const char *alg, u32 type, u32 mask)
  991. {
  992. return crypto_has_alg(alg, type, mask ?: CRYPTO_ALG_TYPE_MASK) ?
  993. 0 : -ENOENT;
  994. }
  995. static int __init tcrypt_mod_init(void)
  996. {
  997. int err = -ENOMEM;
  998. int i;
  999. for (i = 0; i < TVMEMSIZE; i++) {
  1000. tvmem[i] = (void *)__get_free_page(GFP_KERNEL);
  1001. if (!tvmem[i])
  1002. goto err_free_tv;
  1003. }
  1004. if (alg)
  1005. err = do_alg_test(alg, type, mask);
  1006. else
  1007. err = do_test(mode);
  1008. if (err) {
  1009. printk(KERN_ERR "tcrypt: one or more tests failed!\n");
  1010. goto err_free_tv;
  1011. }
  1012. /* We intentionaly return -EAGAIN to prevent keeping the module,
  1013. * unless we're running in fips mode. It does all its work from
  1014. * init() and doesn't offer any runtime functionality, but in
  1015. * the fips case, checking for a successful load is helpful.
  1016. * => we don't need it in the memory, do we?
  1017. * -- mludvig
  1018. */
  1019. if (!fips_enabled)
  1020. err = -EAGAIN;
  1021. err_free_tv:
  1022. for (i = 0; i < TVMEMSIZE && tvmem[i]; i++)
  1023. free_page((unsigned long)tvmem[i]);
  1024. return err;
  1025. }
  1026. /*
  1027. * If an init function is provided, an exit function must also be provided
  1028. * to allow module unload.
  1029. */
  1030. static void __exit tcrypt_mod_fini(void) { }
  1031. module_init(tcrypt_mod_init);
  1032. module_exit(tcrypt_mod_fini);
  1033. module_param(alg, charp, 0);
  1034. module_param(type, uint, 0);
  1035. module_param(mask, uint, 0);
  1036. module_param(mode, int, 0);
  1037. module_param(sec, uint, 0);
  1038. MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
  1039. "(defaults to zero which uses CPU cycles instead)");
  1040. MODULE_LICENSE("GPL");
  1041. MODULE_DESCRIPTION("Quick & dirty crypto testing module");
  1042. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");