tcrypt.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the Free
  13. * Software Foundation; either version 2 of the License, or (at your option)
  14. * any later version.
  15. *
  16. */
  17. #include <crypto/hash.h>
  18. #include <linux/err.h>
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/string.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/timex.h>
  27. #include <linux/interrupt.h>
  28. #include "tcrypt.h"
  29. #include "internal.h"
  30. /*
  31. * Need slab memory for testing (size in number of pages).
  32. */
  33. #define TVMEMSIZE 4
  34. /*
  35. * Used by test_cipher_speed()
  36. */
  37. #define ENCRYPT 1
  38. #define DECRYPT 0
  39. /*
  40. * Used by test_cipher_speed()
  41. */
  42. static unsigned int sec;
  43. static char *alg = NULL;
  44. static u32 type;
  45. static u32 mask;
  46. static int mode;
  47. static char *tvmem[TVMEMSIZE];
  48. static char *check[] = {
  49. "des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
  50. "blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
  51. "cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
  52. "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", "fcrypt",
  53. "camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
  54. "lzo", "cts", "zlib", NULL
  55. };
  56. static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc,
  57. struct scatterlist *sg, int blen, int sec)
  58. {
  59. unsigned long start, end;
  60. int bcount;
  61. int ret;
  62. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  63. time_before(jiffies, end); bcount++) {
  64. if (enc)
  65. ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
  66. else
  67. ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
  68. if (ret)
  69. return ret;
  70. }
  71. printk("%d operations in %d seconds (%ld bytes)\n",
  72. bcount, sec, (long)bcount * blen);
  73. return 0;
  74. }
  75. static int test_cipher_cycles(struct blkcipher_desc *desc, int enc,
  76. struct scatterlist *sg, int blen)
  77. {
  78. unsigned long cycles = 0;
  79. int ret = 0;
  80. int i;
  81. local_bh_disable();
  82. local_irq_disable();
  83. /* Warm-up run. */
  84. for (i = 0; i < 4; i++) {
  85. if (enc)
  86. ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
  87. else
  88. ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
  89. if (ret)
  90. goto out;
  91. }
  92. /* The real thing. */
  93. for (i = 0; i < 8; i++) {
  94. cycles_t start, end;
  95. start = get_cycles();
  96. if (enc)
  97. ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
  98. else
  99. ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
  100. end = get_cycles();
  101. if (ret)
  102. goto out;
  103. cycles += end - start;
  104. }
  105. out:
  106. local_irq_enable();
  107. local_bh_enable();
  108. if (ret == 0)
  109. printk("1 operation in %lu cycles (%d bytes)\n",
  110. (cycles + 4) / 8, blen);
  111. return ret;
  112. }
  113. static u32 block_sizes[] = { 16, 64, 256, 1024, 8192, 0 };
  114. static void test_cipher_speed(const char *algo, int enc, unsigned int sec,
  115. struct cipher_speed_template *template,
  116. unsigned int tcount, u8 *keysize)
  117. {
  118. unsigned int ret, i, j, iv_len;
  119. const char *key, iv[128];
  120. struct crypto_blkcipher *tfm;
  121. struct blkcipher_desc desc;
  122. const char *e;
  123. u32 *b_size;
  124. if (enc == ENCRYPT)
  125. e = "encryption";
  126. else
  127. e = "decryption";
  128. printk("\ntesting speed of %s %s\n", algo, e);
  129. tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
  130. if (IS_ERR(tfm)) {
  131. printk("failed to load transform for %s: %ld\n", algo,
  132. PTR_ERR(tfm));
  133. return;
  134. }
  135. desc.tfm = tfm;
  136. desc.flags = 0;
  137. i = 0;
  138. do {
  139. b_size = block_sizes;
  140. do {
  141. struct scatterlist sg[TVMEMSIZE];
  142. if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
  143. printk("template (%u) too big for "
  144. "tvmem (%lu)\n", *keysize + *b_size,
  145. TVMEMSIZE * PAGE_SIZE);
  146. goto out;
  147. }
  148. printk("test %u (%d bit key, %d byte blocks): ", i,
  149. *keysize * 8, *b_size);
  150. memset(tvmem[0], 0xff, PAGE_SIZE);
  151. /* set key, plain text and IV */
  152. key = tvmem[0];
  153. for (j = 0; j < tcount; j++) {
  154. if (template[j].klen == *keysize) {
  155. key = template[j].key;
  156. break;
  157. }
  158. }
  159. ret = crypto_blkcipher_setkey(tfm, key, *keysize);
  160. if (ret) {
  161. printk("setkey() failed flags=%x\n",
  162. crypto_blkcipher_get_flags(tfm));
  163. goto out;
  164. }
  165. sg_init_table(sg, TVMEMSIZE);
  166. sg_set_buf(sg, tvmem[0] + *keysize,
  167. PAGE_SIZE - *keysize);
  168. for (j = 1; j < TVMEMSIZE; j++) {
  169. sg_set_buf(sg + j, tvmem[j], PAGE_SIZE);
  170. memset (tvmem[j], 0xff, PAGE_SIZE);
  171. }
  172. iv_len = crypto_blkcipher_ivsize(tfm);
  173. if (iv_len) {
  174. memset(&iv, 0xff, iv_len);
  175. crypto_blkcipher_set_iv(tfm, iv, iv_len);
  176. }
  177. if (sec)
  178. ret = test_cipher_jiffies(&desc, enc, sg,
  179. *b_size, sec);
  180. else
  181. ret = test_cipher_cycles(&desc, enc, sg,
  182. *b_size);
  183. if (ret) {
  184. printk("%s() failed flags=%x\n", e, desc.flags);
  185. break;
  186. }
  187. b_size++;
  188. i++;
  189. } while (*b_size);
  190. keysize++;
  191. } while (*keysize);
  192. out:
  193. crypto_free_blkcipher(tfm);
  194. }
  195. static int test_hash_jiffies_digest(struct hash_desc *desc,
  196. struct scatterlist *sg, int blen,
  197. char *out, int sec)
  198. {
  199. unsigned long start, end;
  200. int bcount;
  201. int ret;
  202. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  203. time_before(jiffies, end); bcount++) {
  204. ret = crypto_hash_digest(desc, sg, blen, out);
  205. if (ret)
  206. return ret;
  207. }
  208. printk("%6u opers/sec, %9lu bytes/sec\n",
  209. bcount / sec, ((long)bcount * blen) / sec);
  210. return 0;
  211. }
  212. static int test_hash_jiffies(struct hash_desc *desc, struct scatterlist *sg,
  213. int blen, int plen, char *out, int sec)
  214. {
  215. unsigned long start, end;
  216. int bcount, pcount;
  217. int ret;
  218. if (plen == blen)
  219. return test_hash_jiffies_digest(desc, sg, blen, out, sec);
  220. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  221. time_before(jiffies, end); bcount++) {
  222. ret = crypto_hash_init(desc);
  223. if (ret)
  224. return ret;
  225. for (pcount = 0; pcount < blen; pcount += plen) {
  226. ret = crypto_hash_update(desc, sg, plen);
  227. if (ret)
  228. return ret;
  229. }
  230. /* we assume there is enough space in 'out' for the result */
  231. ret = crypto_hash_final(desc, out);
  232. if (ret)
  233. return ret;
  234. }
  235. printk("%6u opers/sec, %9lu bytes/sec\n",
  236. bcount / sec, ((long)bcount * blen) / sec);
  237. return 0;
  238. }
  239. static int test_hash_cycles_digest(struct hash_desc *desc,
  240. struct scatterlist *sg, int blen, char *out)
  241. {
  242. unsigned long cycles = 0;
  243. int i;
  244. int ret;
  245. local_bh_disable();
  246. local_irq_disable();
  247. /* Warm-up run. */
  248. for (i = 0; i < 4; i++) {
  249. ret = crypto_hash_digest(desc, sg, blen, out);
  250. if (ret)
  251. goto out;
  252. }
  253. /* The real thing. */
  254. for (i = 0; i < 8; i++) {
  255. cycles_t start, end;
  256. start = get_cycles();
  257. ret = crypto_hash_digest(desc, sg, blen, out);
  258. if (ret)
  259. goto out;
  260. end = get_cycles();
  261. cycles += end - start;
  262. }
  263. out:
  264. local_irq_enable();
  265. local_bh_enable();
  266. if (ret)
  267. return ret;
  268. printk("%6lu cycles/operation, %4lu cycles/byte\n",
  269. cycles / 8, cycles / (8 * blen));
  270. return 0;
  271. }
  272. static int test_hash_cycles(struct hash_desc *desc, struct scatterlist *sg,
  273. int blen, int plen, char *out)
  274. {
  275. unsigned long cycles = 0;
  276. int i, pcount;
  277. int ret;
  278. if (plen == blen)
  279. return test_hash_cycles_digest(desc, sg, blen, out);
  280. local_bh_disable();
  281. local_irq_disable();
  282. /* Warm-up run. */
  283. for (i = 0; i < 4; i++) {
  284. ret = crypto_hash_init(desc);
  285. if (ret)
  286. goto out;
  287. for (pcount = 0; pcount < blen; pcount += plen) {
  288. ret = crypto_hash_update(desc, sg, plen);
  289. if (ret)
  290. goto out;
  291. }
  292. ret = crypto_hash_final(desc, out);
  293. if (ret)
  294. goto out;
  295. }
  296. /* The real thing. */
  297. for (i = 0; i < 8; i++) {
  298. cycles_t start, end;
  299. start = get_cycles();
  300. ret = crypto_hash_init(desc);
  301. if (ret)
  302. goto out;
  303. for (pcount = 0; pcount < blen; pcount += plen) {
  304. ret = crypto_hash_update(desc, sg, plen);
  305. if (ret)
  306. goto out;
  307. }
  308. ret = crypto_hash_final(desc, out);
  309. if (ret)
  310. goto out;
  311. end = get_cycles();
  312. cycles += end - start;
  313. }
  314. out:
  315. local_irq_enable();
  316. local_bh_enable();
  317. if (ret)
  318. return ret;
  319. printk("%6lu cycles/operation, %4lu cycles/byte\n",
  320. cycles / 8, cycles / (8 * blen));
  321. return 0;
  322. }
  323. static void test_hash_speed(const char *algo, unsigned int sec,
  324. struct hash_speed *speed)
  325. {
  326. struct scatterlist sg[TVMEMSIZE];
  327. struct crypto_hash *tfm;
  328. struct hash_desc desc;
  329. static char output[1024];
  330. int i;
  331. int ret;
  332. printk(KERN_INFO "\ntesting speed of %s\n", algo);
  333. tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
  334. if (IS_ERR(tfm)) {
  335. printk(KERN_ERR "failed to load transform for %s: %ld\n", algo,
  336. PTR_ERR(tfm));
  337. return;
  338. }
  339. desc.tfm = tfm;
  340. desc.flags = 0;
  341. if (crypto_hash_digestsize(tfm) > sizeof(output)) {
  342. printk(KERN_ERR "digestsize(%u) > outputbuffer(%zu)\n",
  343. crypto_hash_digestsize(tfm), sizeof(output));
  344. goto out;
  345. }
  346. sg_init_table(sg, TVMEMSIZE);
  347. for (i = 0; i < TVMEMSIZE; i++) {
  348. sg_set_buf(sg + i, tvmem[i], PAGE_SIZE);
  349. memset(tvmem[i], 0xff, PAGE_SIZE);
  350. }
  351. for (i = 0; speed[i].blen != 0; i++) {
  352. if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
  353. printk(KERN_ERR
  354. "template (%u) too big for tvmem (%lu)\n",
  355. speed[i].blen, TVMEMSIZE * PAGE_SIZE);
  356. goto out;
  357. }
  358. if (speed[i].klen)
  359. crypto_hash_setkey(tfm, tvmem[0], speed[i].klen);
  360. printk(KERN_INFO "test%3u "
  361. "(%5u byte blocks,%5u bytes per update,%4u updates): ",
  362. i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
  363. if (sec)
  364. ret = test_hash_jiffies(&desc, sg, speed[i].blen,
  365. speed[i].plen, output, sec);
  366. else
  367. ret = test_hash_cycles(&desc, sg, speed[i].blen,
  368. speed[i].plen, output);
  369. if (ret) {
  370. printk(KERN_ERR "hashing failed ret=%d\n", ret);
  371. break;
  372. }
  373. }
  374. out:
  375. crypto_free_hash(tfm);
  376. }
  377. static void test_available(void)
  378. {
  379. char **name = check;
  380. while (*name) {
  381. printk("alg %s ", *name);
  382. printk(crypto_has_alg(*name, 0, 0) ?
  383. "found\n" : "not found\n");
  384. name++;
  385. }
  386. }
  387. static inline int tcrypt_test(const char *alg)
  388. {
  389. int ret;
  390. ret = alg_test(alg, alg, 0, 0);
  391. /* non-fips algs return -EINVAL in fips mode */
  392. if (fips_enabled && ret == -EINVAL)
  393. ret = 0;
  394. return ret;
  395. }
  396. static int do_test(int m)
  397. {
  398. int i;
  399. int ret = 0;
  400. switch (m) {
  401. case 0:
  402. for (i = 1; i < 200; i++)
  403. ret += do_test(i);
  404. break;
  405. case 1:
  406. ret += tcrypt_test("md5");
  407. break;
  408. case 2:
  409. ret += tcrypt_test("sha1");
  410. break;
  411. case 3:
  412. ret += tcrypt_test("ecb(des)");
  413. ret += tcrypt_test("cbc(des)");
  414. break;
  415. case 4:
  416. ret += tcrypt_test("ecb(des3_ede)");
  417. ret += tcrypt_test("cbc(des3_ede)");
  418. break;
  419. case 5:
  420. ret += tcrypt_test("md4");
  421. break;
  422. case 6:
  423. ret += tcrypt_test("sha256");
  424. break;
  425. case 7:
  426. ret += tcrypt_test("ecb(blowfish)");
  427. ret += tcrypt_test("cbc(blowfish)");
  428. break;
  429. case 8:
  430. ret += tcrypt_test("ecb(twofish)");
  431. ret += tcrypt_test("cbc(twofish)");
  432. break;
  433. case 9:
  434. ret += tcrypt_test("ecb(serpent)");
  435. break;
  436. case 10:
  437. ret += tcrypt_test("ecb(aes)");
  438. ret += tcrypt_test("cbc(aes)");
  439. ret += tcrypt_test("lrw(aes)");
  440. ret += tcrypt_test("xts(aes)");
  441. ret += tcrypt_test("ctr(aes)");
  442. ret += tcrypt_test("rfc3686(ctr(aes))");
  443. break;
  444. case 11:
  445. ret += tcrypt_test("sha384");
  446. break;
  447. case 12:
  448. ret += tcrypt_test("sha512");
  449. break;
  450. case 13:
  451. ret += tcrypt_test("deflate");
  452. break;
  453. case 14:
  454. ret += tcrypt_test("ecb(cast5)");
  455. break;
  456. case 15:
  457. ret += tcrypt_test("ecb(cast6)");
  458. break;
  459. case 16:
  460. ret += tcrypt_test("ecb(arc4)");
  461. break;
  462. case 17:
  463. ret += tcrypt_test("michael_mic");
  464. break;
  465. case 18:
  466. ret += tcrypt_test("crc32c");
  467. break;
  468. case 19:
  469. ret += tcrypt_test("ecb(tea)");
  470. break;
  471. case 20:
  472. ret += tcrypt_test("ecb(xtea)");
  473. break;
  474. case 21:
  475. ret += tcrypt_test("ecb(khazad)");
  476. break;
  477. case 22:
  478. ret += tcrypt_test("wp512");
  479. break;
  480. case 23:
  481. ret += tcrypt_test("wp384");
  482. break;
  483. case 24:
  484. ret += tcrypt_test("wp256");
  485. break;
  486. case 25:
  487. ret += tcrypt_test("ecb(tnepres)");
  488. break;
  489. case 26:
  490. ret += tcrypt_test("ecb(anubis)");
  491. ret += tcrypt_test("cbc(anubis)");
  492. break;
  493. case 27:
  494. ret += tcrypt_test("tgr192");
  495. break;
  496. case 28:
  497. ret += tcrypt_test("tgr160");
  498. break;
  499. case 29:
  500. ret += tcrypt_test("tgr128");
  501. break;
  502. case 30:
  503. ret += tcrypt_test("ecb(xeta)");
  504. break;
  505. case 31:
  506. ret += tcrypt_test("pcbc(fcrypt)");
  507. break;
  508. case 32:
  509. ret += tcrypt_test("ecb(camellia)");
  510. ret += tcrypt_test("cbc(camellia)");
  511. break;
  512. case 33:
  513. ret += tcrypt_test("sha224");
  514. break;
  515. case 34:
  516. ret += tcrypt_test("salsa20");
  517. break;
  518. case 35:
  519. ret += tcrypt_test("gcm(aes)");
  520. break;
  521. case 36:
  522. ret += tcrypt_test("lzo");
  523. break;
  524. case 37:
  525. ret += tcrypt_test("ccm(aes)");
  526. break;
  527. case 38:
  528. ret += tcrypt_test("cts(cbc(aes))");
  529. break;
  530. case 39:
  531. ret += tcrypt_test("rmd128");
  532. break;
  533. case 40:
  534. ret += tcrypt_test("rmd160");
  535. break;
  536. case 41:
  537. ret += tcrypt_test("rmd256");
  538. break;
  539. case 42:
  540. ret += tcrypt_test("rmd320");
  541. break;
  542. case 43:
  543. ret += tcrypt_test("ecb(seed)");
  544. break;
  545. case 44:
  546. ret += tcrypt_test("zlib");
  547. break;
  548. case 45:
  549. ret += tcrypt_test("rfc4309(ccm(aes))");
  550. break;
  551. case 100:
  552. ret += tcrypt_test("hmac(md5)");
  553. break;
  554. case 101:
  555. ret += tcrypt_test("hmac(sha1)");
  556. break;
  557. case 102:
  558. ret += tcrypt_test("hmac(sha256)");
  559. break;
  560. case 103:
  561. ret += tcrypt_test("hmac(sha384)");
  562. break;
  563. case 104:
  564. ret += tcrypt_test("hmac(sha512)");
  565. break;
  566. case 105:
  567. ret += tcrypt_test("hmac(sha224)");
  568. break;
  569. case 106:
  570. ret += tcrypt_test("xcbc(aes)");
  571. break;
  572. case 107:
  573. ret += tcrypt_test("hmac(rmd128)");
  574. break;
  575. case 108:
  576. ret += tcrypt_test("hmac(rmd160)");
  577. break;
  578. case 109:
  579. ret += tcrypt_test("vmac(aes)");
  580. break;
  581. case 150:
  582. ret += tcrypt_test("ansi_cprng");
  583. break;
  584. case 200:
  585. test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
  586. speed_template_16_24_32);
  587. test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
  588. speed_template_16_24_32);
  589. test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
  590. speed_template_16_24_32);
  591. test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
  592. speed_template_16_24_32);
  593. test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
  594. speed_template_32_40_48);
  595. test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
  596. speed_template_32_40_48);
  597. test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
  598. speed_template_32_48_64);
  599. test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
  600. speed_template_32_48_64);
  601. break;
  602. case 201:
  603. test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
  604. des3_speed_template, DES3_SPEED_VECTORS,
  605. speed_template_24);
  606. test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
  607. des3_speed_template, DES3_SPEED_VECTORS,
  608. speed_template_24);
  609. test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
  610. des3_speed_template, DES3_SPEED_VECTORS,
  611. speed_template_24);
  612. test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
  613. des3_speed_template, DES3_SPEED_VECTORS,
  614. speed_template_24);
  615. break;
  616. case 202:
  617. test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
  618. speed_template_16_24_32);
  619. test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
  620. speed_template_16_24_32);
  621. test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
  622. speed_template_16_24_32);
  623. test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
  624. speed_template_16_24_32);
  625. break;
  626. case 203:
  627. test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
  628. speed_template_8_32);
  629. test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
  630. speed_template_8_32);
  631. test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
  632. speed_template_8_32);
  633. test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
  634. speed_template_8_32);
  635. break;
  636. case 204:
  637. test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
  638. speed_template_8);
  639. test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
  640. speed_template_8);
  641. test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
  642. speed_template_8);
  643. test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
  644. speed_template_8);
  645. break;
  646. case 205:
  647. test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
  648. speed_template_16_24_32);
  649. test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
  650. speed_template_16_24_32);
  651. test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
  652. speed_template_16_24_32);
  653. test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
  654. speed_template_16_24_32);
  655. break;
  656. case 206:
  657. test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
  658. speed_template_16_32);
  659. break;
  660. case 300:
  661. /* fall through */
  662. case 301:
  663. test_hash_speed("md4", sec, generic_hash_speed_template);
  664. if (mode > 300 && mode < 400) break;
  665. case 302:
  666. test_hash_speed("md5", sec, generic_hash_speed_template);
  667. if (mode > 300 && mode < 400) break;
  668. case 303:
  669. test_hash_speed("sha1", sec, generic_hash_speed_template);
  670. if (mode > 300 && mode < 400) break;
  671. case 304:
  672. test_hash_speed("sha256", sec, generic_hash_speed_template);
  673. if (mode > 300 && mode < 400) break;
  674. case 305:
  675. test_hash_speed("sha384", sec, generic_hash_speed_template);
  676. if (mode > 300 && mode < 400) break;
  677. case 306:
  678. test_hash_speed("sha512", sec, generic_hash_speed_template);
  679. if (mode > 300 && mode < 400) break;
  680. case 307:
  681. test_hash_speed("wp256", sec, generic_hash_speed_template);
  682. if (mode > 300 && mode < 400) break;
  683. case 308:
  684. test_hash_speed("wp384", sec, generic_hash_speed_template);
  685. if (mode > 300 && mode < 400) break;
  686. case 309:
  687. test_hash_speed("wp512", sec, generic_hash_speed_template);
  688. if (mode > 300 && mode < 400) break;
  689. case 310:
  690. test_hash_speed("tgr128", sec, generic_hash_speed_template);
  691. if (mode > 300 && mode < 400) break;
  692. case 311:
  693. test_hash_speed("tgr160", sec, generic_hash_speed_template);
  694. if (mode > 300 && mode < 400) break;
  695. case 312:
  696. test_hash_speed("tgr192", sec, generic_hash_speed_template);
  697. if (mode > 300 && mode < 400) break;
  698. case 313:
  699. test_hash_speed("sha224", sec, generic_hash_speed_template);
  700. if (mode > 300 && mode < 400) break;
  701. case 314:
  702. test_hash_speed("rmd128", sec, generic_hash_speed_template);
  703. if (mode > 300 && mode < 400) break;
  704. case 315:
  705. test_hash_speed("rmd160", sec, generic_hash_speed_template);
  706. if (mode > 300 && mode < 400) break;
  707. case 316:
  708. test_hash_speed("rmd256", sec, generic_hash_speed_template);
  709. if (mode > 300 && mode < 400) break;
  710. case 317:
  711. test_hash_speed("rmd320", sec, generic_hash_speed_template);
  712. if (mode > 300 && mode < 400) break;
  713. case 318:
  714. test_hash_speed("ghash-generic", sec, hash_speed_template_16);
  715. if (mode > 300 && mode < 400) break;
  716. case 399:
  717. break;
  718. case 1000:
  719. test_available();
  720. break;
  721. }
  722. return ret;
  723. }
  724. static int do_alg_test(const char *alg, u32 type, u32 mask)
  725. {
  726. return crypto_has_alg(alg, type, mask ?: CRYPTO_ALG_TYPE_MASK) ?
  727. 0 : -ENOENT;
  728. }
  729. static int __init tcrypt_mod_init(void)
  730. {
  731. int err = -ENOMEM;
  732. int i;
  733. for (i = 0; i < TVMEMSIZE; i++) {
  734. tvmem[i] = (void *)__get_free_page(GFP_KERNEL);
  735. if (!tvmem[i])
  736. goto err_free_tv;
  737. }
  738. if (alg)
  739. err = do_alg_test(alg, type, mask);
  740. else
  741. err = do_test(mode);
  742. if (err) {
  743. printk(KERN_ERR "tcrypt: one or more tests failed!\n");
  744. goto err_free_tv;
  745. }
  746. /* We intentionaly return -EAGAIN to prevent keeping the module,
  747. * unless we're running in fips mode. It does all its work from
  748. * init() and doesn't offer any runtime functionality, but in
  749. * the fips case, checking for a successful load is helpful.
  750. * => we don't need it in the memory, do we?
  751. * -- mludvig
  752. */
  753. if (!fips_enabled)
  754. err = -EAGAIN;
  755. err_free_tv:
  756. for (i = 0; i < TVMEMSIZE && tvmem[i]; i++)
  757. free_page((unsigned long)tvmem[i]);
  758. return err;
  759. }
  760. /*
  761. * If an init function is provided, an exit function must also be provided
  762. * to allow module unload.
  763. */
  764. static void __exit tcrypt_mod_fini(void) { }
  765. module_init(tcrypt_mod_init);
  766. module_exit(tcrypt_mod_fini);
  767. module_param(alg, charp, 0);
  768. module_param(type, uint, 0);
  769. module_param(mask, uint, 0);
  770. module_param(mode, int, 0);
  771. module_param(sec, uint, 0);
  772. MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
  773. "(defaults to zero which uses CPU cycles instead)");
  774. MODULE_LICENSE("GPL");
  775. MODULE_DESCRIPTION("Quick & dirty crypto testing module");
  776. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");