tcrypt.c 28 KB

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