tcrypt.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  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. test_cipher_speed("ctr(aes)", ENCRYPT, sec, NULL, 0,
  813. speed_template_16_24_32);
  814. test_cipher_speed("ctr(aes)", DECRYPT, sec, NULL, 0,
  815. speed_template_16_24_32);
  816. break;
  817. case 201:
  818. test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
  819. des3_speed_template, DES3_SPEED_VECTORS,
  820. speed_template_24);
  821. test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
  822. des3_speed_template, DES3_SPEED_VECTORS,
  823. speed_template_24);
  824. test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
  825. des3_speed_template, DES3_SPEED_VECTORS,
  826. speed_template_24);
  827. test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
  828. des3_speed_template, DES3_SPEED_VECTORS,
  829. speed_template_24);
  830. break;
  831. case 202:
  832. test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
  833. speed_template_16_24_32);
  834. test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
  835. speed_template_16_24_32);
  836. test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
  837. speed_template_16_24_32);
  838. test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
  839. speed_template_16_24_32);
  840. break;
  841. case 203:
  842. test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
  843. speed_template_8_32);
  844. test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
  845. speed_template_8_32);
  846. test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
  847. speed_template_8_32);
  848. test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
  849. speed_template_8_32);
  850. break;
  851. case 204:
  852. test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
  853. speed_template_8);
  854. test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
  855. speed_template_8);
  856. test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
  857. speed_template_8);
  858. test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
  859. speed_template_8);
  860. break;
  861. case 205:
  862. test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
  863. speed_template_16_24_32);
  864. test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
  865. speed_template_16_24_32);
  866. test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
  867. speed_template_16_24_32);
  868. test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
  869. speed_template_16_24_32);
  870. break;
  871. case 206:
  872. test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
  873. speed_template_16_32);
  874. break;
  875. case 300:
  876. /* fall through */
  877. case 301:
  878. test_hash_speed("md4", sec, generic_hash_speed_template);
  879. if (mode > 300 && mode < 400) break;
  880. case 302:
  881. test_hash_speed("md5", sec, generic_hash_speed_template);
  882. if (mode > 300 && mode < 400) break;
  883. case 303:
  884. test_hash_speed("sha1", sec, generic_hash_speed_template);
  885. if (mode > 300 && mode < 400) break;
  886. case 304:
  887. test_hash_speed("sha256", sec, generic_hash_speed_template);
  888. if (mode > 300 && mode < 400) break;
  889. case 305:
  890. test_hash_speed("sha384", sec, generic_hash_speed_template);
  891. if (mode > 300 && mode < 400) break;
  892. case 306:
  893. test_hash_speed("sha512", sec, generic_hash_speed_template);
  894. if (mode > 300 && mode < 400) break;
  895. case 307:
  896. test_hash_speed("wp256", sec, generic_hash_speed_template);
  897. if (mode > 300 && mode < 400) break;
  898. case 308:
  899. test_hash_speed("wp384", sec, generic_hash_speed_template);
  900. if (mode > 300 && mode < 400) break;
  901. case 309:
  902. test_hash_speed("wp512", sec, generic_hash_speed_template);
  903. if (mode > 300 && mode < 400) break;
  904. case 310:
  905. test_hash_speed("tgr128", sec, generic_hash_speed_template);
  906. if (mode > 300 && mode < 400) break;
  907. case 311:
  908. test_hash_speed("tgr160", sec, generic_hash_speed_template);
  909. if (mode > 300 && mode < 400) break;
  910. case 312:
  911. test_hash_speed("tgr192", sec, generic_hash_speed_template);
  912. if (mode > 300 && mode < 400) break;
  913. case 313:
  914. test_hash_speed("sha224", sec, generic_hash_speed_template);
  915. if (mode > 300 && mode < 400) break;
  916. case 314:
  917. test_hash_speed("rmd128", sec, generic_hash_speed_template);
  918. if (mode > 300 && mode < 400) break;
  919. case 315:
  920. test_hash_speed("rmd160", sec, generic_hash_speed_template);
  921. if (mode > 300 && mode < 400) break;
  922. case 316:
  923. test_hash_speed("rmd256", sec, generic_hash_speed_template);
  924. if (mode > 300 && mode < 400) break;
  925. case 317:
  926. test_hash_speed("rmd320", sec, generic_hash_speed_template);
  927. if (mode > 300 && mode < 400) break;
  928. case 318:
  929. test_hash_speed("ghash-generic", sec, hash_speed_template_16);
  930. if (mode > 300 && mode < 400) break;
  931. case 399:
  932. break;
  933. case 400:
  934. /* fall through */
  935. case 401:
  936. test_ahash_speed("md4", sec, generic_hash_speed_template);
  937. if (mode > 400 && mode < 500) break;
  938. case 402:
  939. test_ahash_speed("md5", sec, generic_hash_speed_template);
  940. if (mode > 400 && mode < 500) break;
  941. case 403:
  942. test_ahash_speed("sha1", sec, generic_hash_speed_template);
  943. if (mode > 400 && mode < 500) break;
  944. case 404:
  945. test_ahash_speed("sha256", sec, generic_hash_speed_template);
  946. if (mode > 400 && mode < 500) break;
  947. case 405:
  948. test_ahash_speed("sha384", sec, generic_hash_speed_template);
  949. if (mode > 400 && mode < 500) break;
  950. case 406:
  951. test_ahash_speed("sha512", sec, generic_hash_speed_template);
  952. if (mode > 400 && mode < 500) break;
  953. case 407:
  954. test_ahash_speed("wp256", sec, generic_hash_speed_template);
  955. if (mode > 400 && mode < 500) break;
  956. case 408:
  957. test_ahash_speed("wp384", sec, generic_hash_speed_template);
  958. if (mode > 400 && mode < 500) break;
  959. case 409:
  960. test_ahash_speed("wp512", sec, generic_hash_speed_template);
  961. if (mode > 400 && mode < 500) break;
  962. case 410:
  963. test_ahash_speed("tgr128", sec, generic_hash_speed_template);
  964. if (mode > 400 && mode < 500) break;
  965. case 411:
  966. test_ahash_speed("tgr160", sec, generic_hash_speed_template);
  967. if (mode > 400 && mode < 500) break;
  968. case 412:
  969. test_ahash_speed("tgr192", sec, generic_hash_speed_template);
  970. if (mode > 400 && mode < 500) break;
  971. case 413:
  972. test_ahash_speed("sha224", sec, generic_hash_speed_template);
  973. if (mode > 400 && mode < 500) break;
  974. case 414:
  975. test_ahash_speed("rmd128", sec, generic_hash_speed_template);
  976. if (mode > 400 && mode < 500) break;
  977. case 415:
  978. test_ahash_speed("rmd160", sec, generic_hash_speed_template);
  979. if (mode > 400 && mode < 500) break;
  980. case 416:
  981. test_ahash_speed("rmd256", sec, generic_hash_speed_template);
  982. if (mode > 400 && mode < 500) break;
  983. case 417:
  984. test_ahash_speed("rmd320", sec, generic_hash_speed_template);
  985. if (mode > 400 && mode < 500) break;
  986. case 499:
  987. break;
  988. case 1000:
  989. test_available();
  990. break;
  991. }
  992. return ret;
  993. }
  994. static int do_alg_test(const char *alg, u32 type, u32 mask)
  995. {
  996. return crypto_has_alg(alg, type, mask ?: CRYPTO_ALG_TYPE_MASK) ?
  997. 0 : -ENOENT;
  998. }
  999. static int __init tcrypt_mod_init(void)
  1000. {
  1001. int err = -ENOMEM;
  1002. int i;
  1003. for (i = 0; i < TVMEMSIZE; i++) {
  1004. tvmem[i] = (void *)__get_free_page(GFP_KERNEL);
  1005. if (!tvmem[i])
  1006. goto err_free_tv;
  1007. }
  1008. if (alg)
  1009. err = do_alg_test(alg, type, mask);
  1010. else
  1011. err = do_test(mode);
  1012. if (err) {
  1013. printk(KERN_ERR "tcrypt: one or more tests failed!\n");
  1014. goto err_free_tv;
  1015. }
  1016. /* We intentionaly return -EAGAIN to prevent keeping the module,
  1017. * unless we're running in fips mode. It does all its work from
  1018. * init() and doesn't offer any runtime functionality, but in
  1019. * the fips case, checking for a successful load is helpful.
  1020. * => we don't need it in the memory, do we?
  1021. * -- mludvig
  1022. */
  1023. if (!fips_enabled)
  1024. err = -EAGAIN;
  1025. err_free_tv:
  1026. for (i = 0; i < TVMEMSIZE && tvmem[i]; i++)
  1027. free_page((unsigned long)tvmem[i]);
  1028. return err;
  1029. }
  1030. /*
  1031. * If an init function is provided, an exit function must also be provided
  1032. * to allow module unload.
  1033. */
  1034. static void __exit tcrypt_mod_fini(void) { }
  1035. module_init(tcrypt_mod_init);
  1036. module_exit(tcrypt_mod_fini);
  1037. module_param(alg, charp, 0);
  1038. module_param(type, uint, 0);
  1039. module_param(mask, uint, 0);
  1040. module_param(mode, int, 0);
  1041. module_param(sec, uint, 0);
  1042. MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
  1043. "(defaults to zero which uses CPU cycles instead)");
  1044. MODULE_LICENSE("GPL");
  1045. MODULE_DESCRIPTION("Quick & dirty crypto testing module");
  1046. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");