tcrypt.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713
  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 inline int do_one_acipher_op(struct ablkcipher_request *req, int ret)
  586. {
  587. if (ret == -EINPROGRESS || ret == -EBUSY) {
  588. struct tcrypt_result *tr = req->base.data;
  589. ret = wait_for_completion_interruptible(&tr->completion);
  590. if (!ret)
  591. ret = tr->err;
  592. INIT_COMPLETION(tr->completion);
  593. }
  594. return ret;
  595. }
  596. static int test_acipher_jiffies(struct ablkcipher_request *req, int enc,
  597. int blen, int sec)
  598. {
  599. unsigned long start, end;
  600. int bcount;
  601. int ret;
  602. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  603. time_before(jiffies, end); bcount++) {
  604. if (enc)
  605. ret = do_one_acipher_op(req,
  606. crypto_ablkcipher_encrypt(req));
  607. else
  608. ret = do_one_acipher_op(req,
  609. crypto_ablkcipher_decrypt(req));
  610. if (ret)
  611. return ret;
  612. }
  613. pr_cont("%d operations in %d seconds (%ld bytes)\n",
  614. bcount, sec, (long)bcount * blen);
  615. return 0;
  616. }
  617. static int test_acipher_cycles(struct ablkcipher_request *req, int enc,
  618. int blen)
  619. {
  620. unsigned long cycles = 0;
  621. int ret = 0;
  622. int i;
  623. /* Warm-up run. */
  624. for (i = 0; i < 4; i++) {
  625. if (enc)
  626. ret = do_one_acipher_op(req,
  627. crypto_ablkcipher_encrypt(req));
  628. else
  629. ret = do_one_acipher_op(req,
  630. crypto_ablkcipher_decrypt(req));
  631. if (ret)
  632. goto out;
  633. }
  634. /* The real thing. */
  635. for (i = 0; i < 8; i++) {
  636. cycles_t start, end;
  637. start = get_cycles();
  638. if (enc)
  639. ret = do_one_acipher_op(req,
  640. crypto_ablkcipher_encrypt(req));
  641. else
  642. ret = do_one_acipher_op(req,
  643. crypto_ablkcipher_decrypt(req));
  644. end = get_cycles();
  645. if (ret)
  646. goto out;
  647. cycles += end - start;
  648. }
  649. out:
  650. if (ret == 0)
  651. pr_cont("1 operation in %lu cycles (%d bytes)\n",
  652. (cycles + 4) / 8, blen);
  653. return ret;
  654. }
  655. static void test_acipher_speed(const char *algo, int enc, unsigned int sec,
  656. struct cipher_speed_template *template,
  657. unsigned int tcount, u8 *keysize)
  658. {
  659. unsigned int ret, i, j, k, iv_len;
  660. struct tcrypt_result tresult;
  661. const char *key;
  662. char iv[128];
  663. struct ablkcipher_request *req;
  664. struct crypto_ablkcipher *tfm;
  665. const char *e;
  666. u32 *b_size;
  667. if (enc == ENCRYPT)
  668. e = "encryption";
  669. else
  670. e = "decryption";
  671. pr_info("\ntesting speed of async %s %s\n", algo, e);
  672. init_completion(&tresult.completion);
  673. tfm = crypto_alloc_ablkcipher(algo, 0, 0);
  674. if (IS_ERR(tfm)) {
  675. pr_err("failed to load transform for %s: %ld\n", algo,
  676. PTR_ERR(tfm));
  677. return;
  678. }
  679. req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
  680. if (!req) {
  681. pr_err("tcrypt: skcipher: Failed to allocate request for %s\n",
  682. algo);
  683. goto out;
  684. }
  685. ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  686. tcrypt_complete, &tresult);
  687. i = 0;
  688. do {
  689. b_size = block_sizes;
  690. do {
  691. struct scatterlist sg[TVMEMSIZE];
  692. if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
  693. pr_err("template (%u) too big for "
  694. "tvmem (%lu)\n", *keysize + *b_size,
  695. TVMEMSIZE * PAGE_SIZE);
  696. goto out_free_req;
  697. }
  698. pr_info("test %u (%d bit key, %d byte blocks): ", i,
  699. *keysize * 8, *b_size);
  700. memset(tvmem[0], 0xff, PAGE_SIZE);
  701. /* set key, plain text and IV */
  702. key = tvmem[0];
  703. for (j = 0; j < tcount; j++) {
  704. if (template[j].klen == *keysize) {
  705. key = template[j].key;
  706. break;
  707. }
  708. }
  709. crypto_ablkcipher_clear_flags(tfm, ~0);
  710. ret = crypto_ablkcipher_setkey(tfm, key, *keysize);
  711. if (ret) {
  712. pr_err("setkey() failed flags=%x\n",
  713. crypto_ablkcipher_get_flags(tfm));
  714. goto out_free_req;
  715. }
  716. sg_init_table(sg, TVMEMSIZE);
  717. k = *keysize + *b_size;
  718. if (k > PAGE_SIZE) {
  719. sg_set_buf(sg, tvmem[0] + *keysize,
  720. PAGE_SIZE - *keysize);
  721. k -= PAGE_SIZE;
  722. j = 1;
  723. while (k > PAGE_SIZE) {
  724. sg_set_buf(sg + j, tvmem[j], PAGE_SIZE);
  725. memset(tvmem[j], 0xff, PAGE_SIZE);
  726. j++;
  727. k -= PAGE_SIZE;
  728. }
  729. sg_set_buf(sg + j, tvmem[j], k);
  730. memset(tvmem[j], 0xff, k);
  731. } else {
  732. sg_set_buf(sg, tvmem[0] + *keysize, *b_size);
  733. }
  734. iv_len = crypto_ablkcipher_ivsize(tfm);
  735. if (iv_len)
  736. memset(&iv, 0xff, iv_len);
  737. ablkcipher_request_set_crypt(req, sg, sg, *b_size, iv);
  738. if (sec)
  739. ret = test_acipher_jiffies(req, enc,
  740. *b_size, sec);
  741. else
  742. ret = test_acipher_cycles(req, enc,
  743. *b_size);
  744. if (ret) {
  745. pr_err("%s() failed flags=%x\n", e,
  746. crypto_ablkcipher_get_flags(tfm));
  747. break;
  748. }
  749. b_size++;
  750. i++;
  751. } while (*b_size);
  752. keysize++;
  753. } while (*keysize);
  754. out_free_req:
  755. ablkcipher_request_free(req);
  756. out:
  757. crypto_free_ablkcipher(tfm);
  758. }
  759. static void test_available(void)
  760. {
  761. char **name = check;
  762. while (*name) {
  763. printk("alg %s ", *name);
  764. printk(crypto_has_alg(*name, 0, 0) ?
  765. "found\n" : "not found\n");
  766. name++;
  767. }
  768. }
  769. static inline int tcrypt_test(const char *alg)
  770. {
  771. int ret;
  772. ret = alg_test(alg, alg, 0, 0);
  773. /* non-fips algs return -EINVAL in fips mode */
  774. if (fips_enabled && ret == -EINVAL)
  775. ret = 0;
  776. return ret;
  777. }
  778. static int do_test(int m)
  779. {
  780. int i;
  781. int ret = 0;
  782. switch (m) {
  783. case 0:
  784. for (i = 1; i < 200; i++)
  785. ret += do_test(i);
  786. break;
  787. case 1:
  788. ret += tcrypt_test("md5");
  789. break;
  790. case 2:
  791. ret += tcrypt_test("sha1");
  792. break;
  793. case 3:
  794. ret += tcrypt_test("ecb(des)");
  795. ret += tcrypt_test("cbc(des)");
  796. break;
  797. case 4:
  798. ret += tcrypt_test("ecb(des3_ede)");
  799. ret += tcrypt_test("cbc(des3_ede)");
  800. break;
  801. case 5:
  802. ret += tcrypt_test("md4");
  803. break;
  804. case 6:
  805. ret += tcrypt_test("sha256");
  806. break;
  807. case 7:
  808. ret += tcrypt_test("ecb(blowfish)");
  809. ret += tcrypt_test("cbc(blowfish)");
  810. ret += tcrypt_test("ctr(blowfish)");
  811. break;
  812. case 8:
  813. ret += tcrypt_test("ecb(twofish)");
  814. ret += tcrypt_test("cbc(twofish)");
  815. ret += tcrypt_test("ctr(twofish)");
  816. ret += tcrypt_test("lrw(twofish)");
  817. ret += tcrypt_test("xts(twofish)");
  818. break;
  819. case 9:
  820. ret += tcrypt_test("ecb(serpent)");
  821. ret += tcrypt_test("cbc(serpent)");
  822. ret += tcrypt_test("ctr(serpent)");
  823. ret += tcrypt_test("lrw(serpent)");
  824. ret += tcrypt_test("xts(serpent)");
  825. break;
  826. case 10:
  827. ret += tcrypt_test("ecb(aes)");
  828. ret += tcrypt_test("cbc(aes)");
  829. ret += tcrypt_test("lrw(aes)");
  830. ret += tcrypt_test("xts(aes)");
  831. ret += tcrypt_test("ctr(aes)");
  832. ret += tcrypt_test("rfc3686(ctr(aes))");
  833. break;
  834. case 11:
  835. ret += tcrypt_test("sha384");
  836. break;
  837. case 12:
  838. ret += tcrypt_test("sha512");
  839. break;
  840. case 13:
  841. ret += tcrypt_test("deflate");
  842. break;
  843. case 14:
  844. ret += tcrypt_test("ecb(cast5)");
  845. break;
  846. case 15:
  847. ret += tcrypt_test("ecb(cast6)");
  848. break;
  849. case 16:
  850. ret += tcrypt_test("ecb(arc4)");
  851. break;
  852. case 17:
  853. ret += tcrypt_test("michael_mic");
  854. break;
  855. case 18:
  856. ret += tcrypt_test("crc32c");
  857. break;
  858. case 19:
  859. ret += tcrypt_test("ecb(tea)");
  860. break;
  861. case 20:
  862. ret += tcrypt_test("ecb(xtea)");
  863. break;
  864. case 21:
  865. ret += tcrypt_test("ecb(khazad)");
  866. break;
  867. case 22:
  868. ret += tcrypt_test("wp512");
  869. break;
  870. case 23:
  871. ret += tcrypt_test("wp384");
  872. break;
  873. case 24:
  874. ret += tcrypt_test("wp256");
  875. break;
  876. case 25:
  877. ret += tcrypt_test("ecb(tnepres)");
  878. break;
  879. case 26:
  880. ret += tcrypt_test("ecb(anubis)");
  881. ret += tcrypt_test("cbc(anubis)");
  882. break;
  883. case 27:
  884. ret += tcrypt_test("tgr192");
  885. break;
  886. case 28:
  887. ret += tcrypt_test("tgr160");
  888. break;
  889. case 29:
  890. ret += tcrypt_test("tgr128");
  891. break;
  892. case 30:
  893. ret += tcrypt_test("ecb(xeta)");
  894. break;
  895. case 31:
  896. ret += tcrypt_test("pcbc(fcrypt)");
  897. break;
  898. case 32:
  899. ret += tcrypt_test("ecb(camellia)");
  900. ret += tcrypt_test("cbc(camellia)");
  901. break;
  902. case 33:
  903. ret += tcrypt_test("sha224");
  904. break;
  905. case 34:
  906. ret += tcrypt_test("salsa20");
  907. break;
  908. case 35:
  909. ret += tcrypt_test("gcm(aes)");
  910. break;
  911. case 36:
  912. ret += tcrypt_test("lzo");
  913. break;
  914. case 37:
  915. ret += tcrypt_test("ccm(aes)");
  916. break;
  917. case 38:
  918. ret += tcrypt_test("cts(cbc(aes))");
  919. break;
  920. case 39:
  921. ret += tcrypt_test("rmd128");
  922. break;
  923. case 40:
  924. ret += tcrypt_test("rmd160");
  925. break;
  926. case 41:
  927. ret += tcrypt_test("rmd256");
  928. break;
  929. case 42:
  930. ret += tcrypt_test("rmd320");
  931. break;
  932. case 43:
  933. ret += tcrypt_test("ecb(seed)");
  934. break;
  935. case 44:
  936. ret += tcrypt_test("zlib");
  937. break;
  938. case 45:
  939. ret += tcrypt_test("rfc4309(ccm(aes))");
  940. break;
  941. case 100:
  942. ret += tcrypt_test("hmac(md5)");
  943. break;
  944. case 101:
  945. ret += tcrypt_test("hmac(sha1)");
  946. break;
  947. case 102:
  948. ret += tcrypt_test("hmac(sha256)");
  949. break;
  950. case 103:
  951. ret += tcrypt_test("hmac(sha384)");
  952. break;
  953. case 104:
  954. ret += tcrypt_test("hmac(sha512)");
  955. break;
  956. case 105:
  957. ret += tcrypt_test("hmac(sha224)");
  958. break;
  959. case 106:
  960. ret += tcrypt_test("xcbc(aes)");
  961. break;
  962. case 107:
  963. ret += tcrypt_test("hmac(rmd128)");
  964. break;
  965. case 108:
  966. ret += tcrypt_test("hmac(rmd160)");
  967. break;
  968. case 109:
  969. ret += tcrypt_test("vmac(aes)");
  970. break;
  971. case 110:
  972. ret += tcrypt_test("hmac(crc32)");
  973. break;
  974. case 150:
  975. ret += tcrypt_test("ansi_cprng");
  976. break;
  977. case 151:
  978. ret += tcrypt_test("rfc4106(gcm(aes))");
  979. break;
  980. case 200:
  981. test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
  982. speed_template_16_24_32);
  983. test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
  984. speed_template_16_24_32);
  985. test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
  986. speed_template_16_24_32);
  987. test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
  988. speed_template_16_24_32);
  989. test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
  990. speed_template_32_40_48);
  991. test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
  992. speed_template_32_40_48);
  993. test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
  994. speed_template_32_48_64);
  995. test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
  996. speed_template_32_48_64);
  997. test_cipher_speed("ctr(aes)", ENCRYPT, sec, NULL, 0,
  998. speed_template_16_24_32);
  999. test_cipher_speed("ctr(aes)", DECRYPT, sec, NULL, 0,
  1000. speed_template_16_24_32);
  1001. break;
  1002. case 201:
  1003. test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
  1004. des3_speed_template, DES3_SPEED_VECTORS,
  1005. speed_template_24);
  1006. test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
  1007. des3_speed_template, DES3_SPEED_VECTORS,
  1008. speed_template_24);
  1009. test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
  1010. des3_speed_template, DES3_SPEED_VECTORS,
  1011. speed_template_24);
  1012. test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
  1013. des3_speed_template, DES3_SPEED_VECTORS,
  1014. speed_template_24);
  1015. break;
  1016. case 202:
  1017. test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
  1018. speed_template_16_24_32);
  1019. test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
  1020. speed_template_16_24_32);
  1021. test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
  1022. speed_template_16_24_32);
  1023. test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
  1024. speed_template_16_24_32);
  1025. test_cipher_speed("ctr(twofish)", ENCRYPT, sec, NULL, 0,
  1026. speed_template_16_24_32);
  1027. test_cipher_speed("ctr(twofish)", DECRYPT, sec, NULL, 0,
  1028. speed_template_16_24_32);
  1029. test_cipher_speed("lrw(twofish)", ENCRYPT, sec, NULL, 0,
  1030. speed_template_32_40_48);
  1031. test_cipher_speed("lrw(twofish)", DECRYPT, sec, NULL, 0,
  1032. speed_template_32_40_48);
  1033. test_cipher_speed("xts(twofish)", ENCRYPT, sec, NULL, 0,
  1034. speed_template_32_48_64);
  1035. test_cipher_speed("xts(twofish)", DECRYPT, sec, NULL, 0,
  1036. speed_template_32_48_64);
  1037. break;
  1038. case 203:
  1039. test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
  1040. speed_template_8_32);
  1041. test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
  1042. speed_template_8_32);
  1043. test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
  1044. speed_template_8_32);
  1045. test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
  1046. speed_template_8_32);
  1047. test_cipher_speed("ctr(blowfish)", ENCRYPT, sec, NULL, 0,
  1048. speed_template_8_32);
  1049. test_cipher_speed("ctr(blowfish)", DECRYPT, sec, NULL, 0,
  1050. speed_template_8_32);
  1051. break;
  1052. case 204:
  1053. test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
  1054. speed_template_8);
  1055. test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
  1056. speed_template_8);
  1057. test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
  1058. speed_template_8);
  1059. test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
  1060. speed_template_8);
  1061. break;
  1062. case 205:
  1063. test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
  1064. speed_template_16_24_32);
  1065. test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
  1066. speed_template_16_24_32);
  1067. test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
  1068. speed_template_16_24_32);
  1069. test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
  1070. speed_template_16_24_32);
  1071. test_cipher_speed("ctr(camellia)", ENCRYPT, sec, NULL, 0,
  1072. speed_template_16_24_32);
  1073. test_cipher_speed("ctr(camellia)", DECRYPT, sec, NULL, 0,
  1074. speed_template_16_24_32);
  1075. test_cipher_speed("lrw(camellia)", ENCRYPT, sec, NULL, 0,
  1076. speed_template_32_40_48);
  1077. test_cipher_speed("lrw(camellia)", DECRYPT, sec, NULL, 0,
  1078. speed_template_32_40_48);
  1079. test_cipher_speed("xts(camellia)", ENCRYPT, sec, NULL, 0,
  1080. speed_template_32_48_64);
  1081. test_cipher_speed("xts(camellia)", DECRYPT, sec, NULL, 0,
  1082. speed_template_32_48_64);
  1083. break;
  1084. case 206:
  1085. test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
  1086. speed_template_16_32);
  1087. break;
  1088. case 207:
  1089. test_cipher_speed("ecb(serpent)", ENCRYPT, sec, NULL, 0,
  1090. speed_template_16_32);
  1091. test_cipher_speed("ecb(serpent)", DECRYPT, sec, NULL, 0,
  1092. speed_template_16_32);
  1093. test_cipher_speed("cbc(serpent)", ENCRYPT, sec, NULL, 0,
  1094. speed_template_16_32);
  1095. test_cipher_speed("cbc(serpent)", DECRYPT, sec, NULL, 0,
  1096. speed_template_16_32);
  1097. test_cipher_speed("ctr(serpent)", ENCRYPT, sec, NULL, 0,
  1098. speed_template_16_32);
  1099. test_cipher_speed("ctr(serpent)", DECRYPT, sec, NULL, 0,
  1100. speed_template_16_32);
  1101. test_cipher_speed("lrw(serpent)", ENCRYPT, sec, NULL, 0,
  1102. speed_template_32_48);
  1103. test_cipher_speed("lrw(serpent)", DECRYPT, sec, NULL, 0,
  1104. speed_template_32_48);
  1105. test_cipher_speed("xts(serpent)", ENCRYPT, sec, NULL, 0,
  1106. speed_template_32_64);
  1107. test_cipher_speed("xts(serpent)", DECRYPT, sec, NULL, 0,
  1108. speed_template_32_64);
  1109. break;
  1110. case 208:
  1111. test_cipher_speed("ecb(arc4)", ENCRYPT, sec, NULL, 0,
  1112. speed_template_8);
  1113. break;
  1114. case 300:
  1115. /* fall through */
  1116. case 301:
  1117. test_hash_speed("md4", sec, generic_hash_speed_template);
  1118. if (mode > 300 && mode < 400) break;
  1119. case 302:
  1120. test_hash_speed("md5", sec, generic_hash_speed_template);
  1121. if (mode > 300 && mode < 400) break;
  1122. case 303:
  1123. test_hash_speed("sha1", sec, generic_hash_speed_template);
  1124. if (mode > 300 && mode < 400) break;
  1125. case 304:
  1126. test_hash_speed("sha256", sec, generic_hash_speed_template);
  1127. if (mode > 300 && mode < 400) break;
  1128. case 305:
  1129. test_hash_speed("sha384", sec, generic_hash_speed_template);
  1130. if (mode > 300 && mode < 400) break;
  1131. case 306:
  1132. test_hash_speed("sha512", sec, generic_hash_speed_template);
  1133. if (mode > 300 && mode < 400) break;
  1134. case 307:
  1135. test_hash_speed("wp256", sec, generic_hash_speed_template);
  1136. if (mode > 300 && mode < 400) break;
  1137. case 308:
  1138. test_hash_speed("wp384", sec, generic_hash_speed_template);
  1139. if (mode > 300 && mode < 400) break;
  1140. case 309:
  1141. test_hash_speed("wp512", sec, generic_hash_speed_template);
  1142. if (mode > 300 && mode < 400) break;
  1143. case 310:
  1144. test_hash_speed("tgr128", sec, generic_hash_speed_template);
  1145. if (mode > 300 && mode < 400) break;
  1146. case 311:
  1147. test_hash_speed("tgr160", sec, generic_hash_speed_template);
  1148. if (mode > 300 && mode < 400) break;
  1149. case 312:
  1150. test_hash_speed("tgr192", sec, generic_hash_speed_template);
  1151. if (mode > 300 && mode < 400) break;
  1152. case 313:
  1153. test_hash_speed("sha224", sec, generic_hash_speed_template);
  1154. if (mode > 300 && mode < 400) break;
  1155. case 314:
  1156. test_hash_speed("rmd128", sec, generic_hash_speed_template);
  1157. if (mode > 300 && mode < 400) break;
  1158. case 315:
  1159. test_hash_speed("rmd160", sec, generic_hash_speed_template);
  1160. if (mode > 300 && mode < 400) break;
  1161. case 316:
  1162. test_hash_speed("rmd256", sec, generic_hash_speed_template);
  1163. if (mode > 300 && mode < 400) break;
  1164. case 317:
  1165. test_hash_speed("rmd320", sec, generic_hash_speed_template);
  1166. if (mode > 300 && mode < 400) break;
  1167. case 318:
  1168. test_hash_speed("ghash-generic", sec, hash_speed_template_16);
  1169. if (mode > 300 && mode < 400) break;
  1170. case 399:
  1171. break;
  1172. case 400:
  1173. /* fall through */
  1174. case 401:
  1175. test_ahash_speed("md4", sec, generic_hash_speed_template);
  1176. if (mode > 400 && mode < 500) break;
  1177. case 402:
  1178. test_ahash_speed("md5", sec, generic_hash_speed_template);
  1179. if (mode > 400 && mode < 500) break;
  1180. case 403:
  1181. test_ahash_speed("sha1", sec, generic_hash_speed_template);
  1182. if (mode > 400 && mode < 500) break;
  1183. case 404:
  1184. test_ahash_speed("sha256", sec, generic_hash_speed_template);
  1185. if (mode > 400 && mode < 500) break;
  1186. case 405:
  1187. test_ahash_speed("sha384", sec, generic_hash_speed_template);
  1188. if (mode > 400 && mode < 500) break;
  1189. case 406:
  1190. test_ahash_speed("sha512", sec, generic_hash_speed_template);
  1191. if (mode > 400 && mode < 500) break;
  1192. case 407:
  1193. test_ahash_speed("wp256", sec, generic_hash_speed_template);
  1194. if (mode > 400 && mode < 500) break;
  1195. case 408:
  1196. test_ahash_speed("wp384", sec, generic_hash_speed_template);
  1197. if (mode > 400 && mode < 500) break;
  1198. case 409:
  1199. test_ahash_speed("wp512", sec, generic_hash_speed_template);
  1200. if (mode > 400 && mode < 500) break;
  1201. case 410:
  1202. test_ahash_speed("tgr128", sec, generic_hash_speed_template);
  1203. if (mode > 400 && mode < 500) break;
  1204. case 411:
  1205. test_ahash_speed("tgr160", sec, generic_hash_speed_template);
  1206. if (mode > 400 && mode < 500) break;
  1207. case 412:
  1208. test_ahash_speed("tgr192", sec, generic_hash_speed_template);
  1209. if (mode > 400 && mode < 500) break;
  1210. case 413:
  1211. test_ahash_speed("sha224", sec, generic_hash_speed_template);
  1212. if (mode > 400 && mode < 500) break;
  1213. case 414:
  1214. test_ahash_speed("rmd128", sec, generic_hash_speed_template);
  1215. if (mode > 400 && mode < 500) break;
  1216. case 415:
  1217. test_ahash_speed("rmd160", sec, generic_hash_speed_template);
  1218. if (mode > 400 && mode < 500) break;
  1219. case 416:
  1220. test_ahash_speed("rmd256", sec, generic_hash_speed_template);
  1221. if (mode > 400 && mode < 500) break;
  1222. case 417:
  1223. test_ahash_speed("rmd320", sec, generic_hash_speed_template);
  1224. if (mode > 400 && mode < 500) break;
  1225. case 499:
  1226. break;
  1227. case 500:
  1228. test_acipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
  1229. speed_template_16_24_32);
  1230. test_acipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
  1231. speed_template_16_24_32);
  1232. test_acipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
  1233. speed_template_16_24_32);
  1234. test_acipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
  1235. speed_template_16_24_32);
  1236. test_acipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
  1237. speed_template_32_40_48);
  1238. test_acipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
  1239. speed_template_32_40_48);
  1240. test_acipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
  1241. speed_template_32_48_64);
  1242. test_acipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
  1243. speed_template_32_48_64);
  1244. test_acipher_speed("ctr(aes)", ENCRYPT, sec, NULL, 0,
  1245. speed_template_16_24_32);
  1246. test_acipher_speed("ctr(aes)", DECRYPT, sec, NULL, 0,
  1247. speed_template_16_24_32);
  1248. test_acipher_speed("cfb(aes)", ENCRYPT, sec, NULL, 0,
  1249. speed_template_16_24_32);
  1250. test_acipher_speed("cfb(aes)", DECRYPT, sec, NULL, 0,
  1251. speed_template_16_24_32);
  1252. test_acipher_speed("ofb(aes)", ENCRYPT, sec, NULL, 0,
  1253. speed_template_16_24_32);
  1254. test_acipher_speed("ofb(aes)", DECRYPT, sec, NULL, 0,
  1255. speed_template_16_24_32);
  1256. break;
  1257. case 501:
  1258. test_acipher_speed("ecb(des3_ede)", ENCRYPT, sec,
  1259. des3_speed_template, DES3_SPEED_VECTORS,
  1260. speed_template_24);
  1261. test_acipher_speed("ecb(des3_ede)", DECRYPT, sec,
  1262. des3_speed_template, DES3_SPEED_VECTORS,
  1263. speed_template_24);
  1264. test_acipher_speed("cbc(des3_ede)", ENCRYPT, sec,
  1265. des3_speed_template, DES3_SPEED_VECTORS,
  1266. speed_template_24);
  1267. test_acipher_speed("cbc(des3_ede)", DECRYPT, sec,
  1268. des3_speed_template, DES3_SPEED_VECTORS,
  1269. speed_template_24);
  1270. test_acipher_speed("cfb(des3_ede)", ENCRYPT, sec,
  1271. des3_speed_template, DES3_SPEED_VECTORS,
  1272. speed_template_24);
  1273. test_acipher_speed("cfb(des3_ede)", DECRYPT, sec,
  1274. des3_speed_template, DES3_SPEED_VECTORS,
  1275. speed_template_24);
  1276. test_acipher_speed("ofb(des3_ede)", ENCRYPT, sec,
  1277. des3_speed_template, DES3_SPEED_VECTORS,
  1278. speed_template_24);
  1279. test_acipher_speed("ofb(des3_ede)", DECRYPT, sec,
  1280. des3_speed_template, DES3_SPEED_VECTORS,
  1281. speed_template_24);
  1282. break;
  1283. case 502:
  1284. test_acipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
  1285. speed_template_8);
  1286. test_acipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
  1287. speed_template_8);
  1288. test_acipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
  1289. speed_template_8);
  1290. test_acipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
  1291. speed_template_8);
  1292. test_acipher_speed("cfb(des)", ENCRYPT, sec, NULL, 0,
  1293. speed_template_8);
  1294. test_acipher_speed("cfb(des)", DECRYPT, sec, NULL, 0,
  1295. speed_template_8);
  1296. test_acipher_speed("ofb(des)", ENCRYPT, sec, NULL, 0,
  1297. speed_template_8);
  1298. test_acipher_speed("ofb(des)", DECRYPT, sec, NULL, 0,
  1299. speed_template_8);
  1300. break;
  1301. case 503:
  1302. test_acipher_speed("ecb(serpent)", ENCRYPT, sec, NULL, 0,
  1303. speed_template_16_32);
  1304. test_acipher_speed("ecb(serpent)", DECRYPT, sec, NULL, 0,
  1305. speed_template_16_32);
  1306. test_acipher_speed("cbc(serpent)", ENCRYPT, sec, NULL, 0,
  1307. speed_template_16_32);
  1308. test_acipher_speed("cbc(serpent)", DECRYPT, sec, NULL, 0,
  1309. speed_template_16_32);
  1310. test_acipher_speed("ctr(serpent)", ENCRYPT, sec, NULL, 0,
  1311. speed_template_16_32);
  1312. test_acipher_speed("ctr(serpent)", DECRYPT, sec, NULL, 0,
  1313. speed_template_16_32);
  1314. test_acipher_speed("lrw(serpent)", ENCRYPT, sec, NULL, 0,
  1315. speed_template_32_48);
  1316. test_acipher_speed("lrw(serpent)", DECRYPT, sec, NULL, 0,
  1317. speed_template_32_48);
  1318. test_acipher_speed("xts(serpent)", ENCRYPT, sec, NULL, 0,
  1319. speed_template_32_64);
  1320. test_acipher_speed("xts(serpent)", DECRYPT, sec, NULL, 0,
  1321. speed_template_32_64);
  1322. break;
  1323. case 504:
  1324. test_acipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
  1325. speed_template_16_24_32);
  1326. test_acipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
  1327. speed_template_16_24_32);
  1328. test_acipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
  1329. speed_template_16_24_32);
  1330. test_acipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
  1331. speed_template_16_24_32);
  1332. test_acipher_speed("ctr(twofish)", ENCRYPT, sec, NULL, 0,
  1333. speed_template_16_24_32);
  1334. test_acipher_speed("ctr(twofish)", DECRYPT, sec, NULL, 0,
  1335. speed_template_16_24_32);
  1336. test_acipher_speed("lrw(twofish)", ENCRYPT, sec, NULL, 0,
  1337. speed_template_32_40_48);
  1338. test_acipher_speed("lrw(twofish)", DECRYPT, sec, NULL, 0,
  1339. speed_template_32_40_48);
  1340. test_acipher_speed("xts(twofish)", ENCRYPT, sec, NULL, 0,
  1341. speed_template_32_48_64);
  1342. test_acipher_speed("xts(twofish)", DECRYPT, sec, NULL, 0,
  1343. speed_template_32_48_64);
  1344. break;
  1345. case 505:
  1346. test_acipher_speed("ecb(arc4)", ENCRYPT, sec, NULL, 0,
  1347. speed_template_8);
  1348. break;
  1349. case 1000:
  1350. test_available();
  1351. break;
  1352. }
  1353. return ret;
  1354. }
  1355. static int do_alg_test(const char *alg, u32 type, u32 mask)
  1356. {
  1357. return crypto_has_alg(alg, type, mask ?: CRYPTO_ALG_TYPE_MASK) ?
  1358. 0 : -ENOENT;
  1359. }
  1360. static int __init tcrypt_mod_init(void)
  1361. {
  1362. int err = -ENOMEM;
  1363. int i;
  1364. for (i = 0; i < TVMEMSIZE; i++) {
  1365. tvmem[i] = (void *)__get_free_page(GFP_KERNEL);
  1366. if (!tvmem[i])
  1367. goto err_free_tv;
  1368. }
  1369. if (alg)
  1370. err = do_alg_test(alg, type, mask);
  1371. else
  1372. err = do_test(mode);
  1373. if (err) {
  1374. printk(KERN_ERR "tcrypt: one or more tests failed!\n");
  1375. goto err_free_tv;
  1376. }
  1377. /* We intentionaly return -EAGAIN to prevent keeping the module,
  1378. * unless we're running in fips mode. It does all its work from
  1379. * init() and doesn't offer any runtime functionality, but in
  1380. * the fips case, checking for a successful load is helpful.
  1381. * => we don't need it in the memory, do we?
  1382. * -- mludvig
  1383. */
  1384. if (!fips_enabled)
  1385. err = -EAGAIN;
  1386. err_free_tv:
  1387. for (i = 0; i < TVMEMSIZE && tvmem[i]; i++)
  1388. free_page((unsigned long)tvmem[i]);
  1389. return err;
  1390. }
  1391. /*
  1392. * If an init function is provided, an exit function must also be provided
  1393. * to allow module unload.
  1394. */
  1395. static void __exit tcrypt_mod_fini(void) { }
  1396. module_init(tcrypt_mod_init);
  1397. module_exit(tcrypt_mod_fini);
  1398. module_param(alg, charp, 0);
  1399. module_param(type, uint, 0);
  1400. module_param(mask, uint, 0);
  1401. module_param(mode, int, 0);
  1402. module_param(sec, uint, 0);
  1403. MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
  1404. "(defaults to zero which uses CPU cycles instead)");
  1405. MODULE_LICENSE("GPL");
  1406. MODULE_DESCRIPTION("Quick & dirty crypto testing module");
  1407. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");