tcrypt.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  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. #ifdef CONFIG_CRYPTO_FIPS_OLD_INTEGRITY_CHECK
  47. extern long integrity_mem_reservoir;
  48. extern void free_bootmem_late(unsigned long addr, unsigned long size);
  49. #endif
  50. /*
  51. * Used by test_cipher_speed()
  52. */
  53. static unsigned int sec;
  54. static char *alg = NULL;
  55. static u32 type;
  56. static u32 mask;
  57. static int mode;
  58. static char *tvmem[TVMEMSIZE];
  59. static char *check[] = {
  60. "des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
  61. "blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
  62. "cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
  63. "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", "fcrypt",
  64. "camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
  65. "lzo", "cts", "zlib", NULL
  66. };
  67. #if defined(CONFIG_CRYPTO_DRBG) && defined(CONFIG_CRYPTO_FIPS)
  68. static char *drbg_cores[] = {
  69. #ifdef CONFIG_CRYPTO_DRBG_CTR
  70. "ctr_aes128",
  71. "ctr_aes192",
  72. "ctr_aes256",
  73. #endif /* CONFIG_CRYPTO_DRBG_CTR */
  74. #ifdef CONFIG_CRYPTO_DRBG_HASH
  75. "sha1",
  76. "sha384",
  77. "sha512",
  78. "sha256",
  79. #endif /* CONFIG_CRYPTO_DRBG_HASH */
  80. #ifdef CONFIG_CRYPTO_DRBG_HMAC
  81. "hmac_sha1",
  82. "hmac_sha384",
  83. "hmac_sha512",
  84. "hmac_sha256",
  85. #endif /* CONFIG_CRYPTO_DRBG_HMAC */
  86. };
  87. #endif /* CONFIG_CRYPTO_DRBG && CONFIG_CRYPTO_FIPS */
  88. static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc,
  89. struct scatterlist *sg, int blen, int sec)
  90. {
  91. unsigned long start, end;
  92. int bcount;
  93. int ret;
  94. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  95. time_before(jiffies, end); bcount++) {
  96. if (enc)
  97. ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
  98. else
  99. ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
  100. if (ret)
  101. return ret;
  102. }
  103. printk("%d operations in %d seconds (%ld bytes)\n",
  104. bcount, sec, (long)bcount * blen);
  105. return 0;
  106. }
  107. static int test_cipher_cycles(struct blkcipher_desc *desc, int enc,
  108. struct scatterlist *sg, int blen)
  109. {
  110. unsigned long cycles = 0;
  111. int ret = 0;
  112. int i;
  113. local_bh_disable();
  114. local_irq_disable();
  115. /* Warm-up run. */
  116. for (i = 0; i < 4; i++) {
  117. if (enc)
  118. ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
  119. else
  120. ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
  121. if (ret)
  122. goto out;
  123. }
  124. /* The real thing. */
  125. for (i = 0; i < 8; i++) {
  126. cycles_t start, end;
  127. start = get_cycles();
  128. if (enc)
  129. ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
  130. else
  131. ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
  132. end = get_cycles();
  133. if (ret)
  134. goto out;
  135. cycles += end - start;
  136. }
  137. out:
  138. local_irq_enable();
  139. local_bh_enable();
  140. if (ret == 0)
  141. printk("1 operation in %lu cycles (%d bytes)\n",
  142. (cycles + 4) / 8, blen);
  143. return ret;
  144. }
  145. static u32 block_sizes[] = { 16, 64, 256, 1024, 8192, 0 };
  146. static void test_cipher_speed(const char *algo, int enc, unsigned int sec,
  147. struct cipher_speed_template *template,
  148. unsigned int tcount, u8 *keysize)
  149. {
  150. unsigned int ret, i, j, iv_len;
  151. const char *key;
  152. char iv[128];
  153. struct crypto_blkcipher *tfm;
  154. struct blkcipher_desc desc;
  155. const char *e;
  156. u32 *b_size;
  157. if (enc == ENCRYPT)
  158. e = "encryption";
  159. else
  160. e = "decryption";
  161. printk("\ntesting speed of %s %s\n", algo, e);
  162. tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
  163. if (IS_ERR(tfm)) {
  164. printk("failed to load transform for %s: %ld\n", algo,
  165. PTR_ERR(tfm));
  166. return;
  167. }
  168. desc.tfm = tfm;
  169. desc.flags = 0;
  170. i = 0;
  171. do {
  172. b_size = block_sizes;
  173. do {
  174. struct scatterlist sg[TVMEMSIZE];
  175. if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
  176. printk("template (%u) too big for "
  177. "tvmem (%lu)\n", *keysize + *b_size,
  178. TVMEMSIZE * PAGE_SIZE);
  179. goto out;
  180. }
  181. printk("test %u (%d bit key, %d byte blocks): ", i,
  182. *keysize * 8, *b_size);
  183. memset(tvmem[0], 0xff, PAGE_SIZE);
  184. /* set key, plain text and IV */
  185. key = tvmem[0];
  186. for (j = 0; j < tcount; j++) {
  187. if (template[j].klen == *keysize) {
  188. key = template[j].key;
  189. break;
  190. }
  191. }
  192. ret = crypto_blkcipher_setkey(tfm, key, *keysize);
  193. if (ret) {
  194. printk("setkey() failed flags=%x\n",
  195. crypto_blkcipher_get_flags(tfm));
  196. goto out;
  197. }
  198. sg_init_table(sg, TVMEMSIZE);
  199. sg_set_buf(sg, tvmem[0] + *keysize,
  200. PAGE_SIZE - *keysize);
  201. for (j = 1; j < TVMEMSIZE; j++) {
  202. sg_set_buf(sg + j, tvmem[j], PAGE_SIZE);
  203. memset (tvmem[j], 0xff, PAGE_SIZE);
  204. }
  205. iv_len = crypto_blkcipher_ivsize(tfm);
  206. if (iv_len) {
  207. memset(&iv, 0xff, iv_len);
  208. crypto_blkcipher_set_iv(tfm, iv, iv_len);
  209. }
  210. if (sec)
  211. ret = test_cipher_jiffies(&desc, enc, sg,
  212. *b_size, sec);
  213. else
  214. ret = test_cipher_cycles(&desc, enc, sg,
  215. *b_size);
  216. if (ret) {
  217. printk("%s() failed flags=%x\n", e, desc.flags);
  218. break;
  219. }
  220. b_size++;
  221. i++;
  222. } while (*b_size);
  223. keysize++;
  224. } while (*keysize);
  225. out:
  226. crypto_free_blkcipher(tfm);
  227. }
  228. static int test_hash_jiffies_digest(struct hash_desc *desc,
  229. struct scatterlist *sg, int blen,
  230. char *out, int sec)
  231. {
  232. unsigned long start, end;
  233. int bcount;
  234. int ret;
  235. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  236. time_before(jiffies, end); bcount++) {
  237. ret = crypto_hash_digest(desc, sg, blen, out);
  238. if (ret)
  239. return ret;
  240. }
  241. printk("%6u opers/sec, %9lu bytes/sec\n",
  242. bcount / sec, ((long)bcount * blen) / sec);
  243. return 0;
  244. }
  245. static int test_hash_jiffies(struct hash_desc *desc, struct scatterlist *sg,
  246. int blen, int plen, char *out, int sec)
  247. {
  248. unsigned long start, end;
  249. int bcount, pcount;
  250. int ret;
  251. if (plen == blen)
  252. return test_hash_jiffies_digest(desc, sg, blen, out, sec);
  253. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  254. time_before(jiffies, end); bcount++) {
  255. ret = crypto_hash_init(desc);
  256. if (ret)
  257. return ret;
  258. for (pcount = 0; pcount < blen; pcount += plen) {
  259. ret = crypto_hash_update(desc, sg, plen);
  260. if (ret)
  261. return ret;
  262. }
  263. /* we assume there is enough space in 'out' for the result */
  264. ret = crypto_hash_final(desc, out);
  265. if (ret)
  266. return ret;
  267. }
  268. printk("%6u opers/sec, %9lu bytes/sec\n",
  269. bcount / sec, ((long)bcount * blen) / sec);
  270. return 0;
  271. }
  272. static int test_hash_cycles_digest(struct hash_desc *desc,
  273. struct scatterlist *sg, int blen, char *out)
  274. {
  275. unsigned long cycles = 0;
  276. int i;
  277. int ret;
  278. local_bh_disable();
  279. local_irq_disable();
  280. /* Warm-up run. */
  281. for (i = 0; i < 4; i++) {
  282. ret = crypto_hash_digest(desc, sg, blen, out);
  283. if (ret)
  284. goto out;
  285. }
  286. /* The real thing. */
  287. for (i = 0; i < 8; i++) {
  288. cycles_t start, end;
  289. start = get_cycles();
  290. ret = crypto_hash_digest(desc, sg, blen, out);
  291. if (ret)
  292. goto out;
  293. end = get_cycles();
  294. cycles += end - start;
  295. }
  296. out:
  297. local_irq_enable();
  298. local_bh_enable();
  299. if (ret)
  300. return ret;
  301. printk("%6lu cycles/operation, %4lu cycles/byte\n",
  302. cycles / 8, cycles / (8 * blen));
  303. return 0;
  304. }
  305. static int test_hash_cycles(struct hash_desc *desc, struct scatterlist *sg,
  306. int blen, int plen, char *out)
  307. {
  308. unsigned long cycles = 0;
  309. int i, pcount;
  310. int ret;
  311. if (plen == blen)
  312. return test_hash_cycles_digest(desc, sg, blen, out);
  313. local_bh_disable();
  314. local_irq_disable();
  315. /* Warm-up run. */
  316. for (i = 0; i < 4; i++) {
  317. ret = crypto_hash_init(desc);
  318. if (ret)
  319. goto out;
  320. for (pcount = 0; pcount < blen; pcount += plen) {
  321. ret = crypto_hash_update(desc, sg, plen);
  322. if (ret)
  323. goto out;
  324. }
  325. ret = crypto_hash_final(desc, out);
  326. if (ret)
  327. goto out;
  328. }
  329. /* The real thing. */
  330. for (i = 0; i < 8; i++) {
  331. cycles_t start, end;
  332. start = get_cycles();
  333. ret = crypto_hash_init(desc);
  334. if (ret)
  335. goto out;
  336. for (pcount = 0; pcount < blen; pcount += plen) {
  337. ret = crypto_hash_update(desc, sg, plen);
  338. if (ret)
  339. goto out;
  340. }
  341. ret = crypto_hash_final(desc, out);
  342. if (ret)
  343. goto out;
  344. end = get_cycles();
  345. cycles += end - start;
  346. }
  347. out:
  348. local_irq_enable();
  349. local_bh_enable();
  350. if (ret)
  351. return ret;
  352. printk("%6lu cycles/operation, %4lu cycles/byte\n",
  353. cycles / 8, cycles / (8 * blen));
  354. return 0;
  355. }
  356. static void test_hash_sg_init(struct scatterlist *sg)
  357. {
  358. int i;
  359. sg_init_table(sg, TVMEMSIZE);
  360. for (i = 0; i < TVMEMSIZE; i++) {
  361. sg_set_buf(sg + i, tvmem[i], PAGE_SIZE);
  362. memset(tvmem[i], 0xff, PAGE_SIZE);
  363. }
  364. }
  365. static void test_hash_speed(const char *algo, unsigned int sec,
  366. struct hash_speed *speed)
  367. {
  368. struct scatterlist sg[TVMEMSIZE];
  369. struct crypto_hash *tfm;
  370. struct hash_desc desc;
  371. static char output[1024];
  372. int i;
  373. int ret;
  374. printk(KERN_INFO "\ntesting speed of %s\n", algo);
  375. tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
  376. if (IS_ERR(tfm)) {
  377. printk(KERN_ERR "failed to load transform for %s: %ld\n", algo,
  378. PTR_ERR(tfm));
  379. return;
  380. }
  381. desc.tfm = tfm;
  382. desc.flags = 0;
  383. if (crypto_hash_digestsize(tfm) > sizeof(output)) {
  384. printk(KERN_ERR "digestsize(%u) > outputbuffer(%zu)\n",
  385. crypto_hash_digestsize(tfm), sizeof(output));
  386. goto out;
  387. }
  388. test_hash_sg_init(sg);
  389. for (i = 0; speed[i].blen != 0; i++) {
  390. if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
  391. printk(KERN_ERR
  392. "template (%u) too big for tvmem (%lu)\n",
  393. speed[i].blen, TVMEMSIZE * PAGE_SIZE);
  394. goto out;
  395. }
  396. if (speed[i].klen)
  397. crypto_hash_setkey(tfm, tvmem[0], speed[i].klen);
  398. printk(KERN_INFO "test%3u "
  399. "(%5u byte blocks,%5u bytes per update,%4u updates): ",
  400. i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
  401. if (sec)
  402. ret = test_hash_jiffies(&desc, sg, speed[i].blen,
  403. speed[i].plen, output, sec);
  404. else
  405. ret = test_hash_cycles(&desc, sg, speed[i].blen,
  406. speed[i].plen, output);
  407. if (ret) {
  408. printk(KERN_ERR "hashing failed ret=%d\n", ret);
  409. break;
  410. }
  411. }
  412. out:
  413. crypto_free_hash(tfm);
  414. }
  415. struct tcrypt_result {
  416. struct completion completion;
  417. int err;
  418. };
  419. static void tcrypt_complete(struct crypto_async_request *req, int err)
  420. {
  421. struct tcrypt_result *res = req->data;
  422. if (err == -EINPROGRESS)
  423. return;
  424. res->err = err;
  425. complete(&res->completion);
  426. }
  427. static inline int do_one_ahash_op(struct ahash_request *req, int ret)
  428. {
  429. if (ret == -EINPROGRESS || ret == -EBUSY) {
  430. struct tcrypt_result *tr = req->base.data;
  431. ret = wait_for_completion_interruptible(&tr->completion);
  432. if (!ret)
  433. ret = tr->err;
  434. INIT_COMPLETION(tr->completion);
  435. }
  436. return ret;
  437. }
  438. static int test_ahash_jiffies_digest(struct ahash_request *req, int blen,
  439. char *out, int sec)
  440. {
  441. unsigned long start, end;
  442. int bcount;
  443. int ret;
  444. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  445. time_before(jiffies, end); bcount++) {
  446. ret = do_one_ahash_op(req, crypto_ahash_digest(req));
  447. if (ret)
  448. return ret;
  449. }
  450. printk("%6u opers/sec, %9lu bytes/sec\n",
  451. bcount / sec, ((long)bcount * blen) / sec);
  452. return 0;
  453. }
  454. static int test_ahash_jiffies(struct ahash_request *req, int blen,
  455. int plen, char *out, int sec)
  456. {
  457. unsigned long start, end;
  458. int bcount, pcount;
  459. int ret;
  460. if (plen == blen)
  461. return test_ahash_jiffies_digest(req, blen, out, sec);
  462. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  463. time_before(jiffies, end); bcount++) {
  464. ret = crypto_ahash_init(req);
  465. if (ret)
  466. return ret;
  467. for (pcount = 0; pcount < blen; pcount += plen) {
  468. ret = do_one_ahash_op(req, crypto_ahash_update(req));
  469. if (ret)
  470. return ret;
  471. }
  472. /* we assume there is enough space in 'out' for the result */
  473. ret = do_one_ahash_op(req, crypto_ahash_final(req));
  474. if (ret)
  475. return ret;
  476. }
  477. pr_cont("%6u opers/sec, %9lu bytes/sec\n",
  478. bcount / sec, ((long)bcount * blen) / sec);
  479. return 0;
  480. }
  481. static int test_ahash_cycles_digest(struct ahash_request *req, int blen,
  482. char *out)
  483. {
  484. unsigned long cycles = 0;
  485. int ret, i;
  486. /* Warm-up run. */
  487. for (i = 0; i < 4; i++) {
  488. ret = do_one_ahash_op(req, crypto_ahash_digest(req));
  489. if (ret)
  490. goto out;
  491. }
  492. /* The real thing. */
  493. for (i = 0; i < 8; i++) {
  494. cycles_t start, end;
  495. start = get_cycles();
  496. ret = do_one_ahash_op(req, crypto_ahash_digest(req));
  497. if (ret)
  498. goto out;
  499. end = get_cycles();
  500. cycles += end - start;
  501. }
  502. out:
  503. if (ret)
  504. return ret;
  505. pr_cont("%6lu cycles/operation, %4lu cycles/byte\n",
  506. cycles / 8, cycles / (8 * blen));
  507. return 0;
  508. }
  509. static int test_ahash_cycles(struct ahash_request *req, int blen,
  510. int plen, char *out)
  511. {
  512. unsigned long cycles = 0;
  513. int i, pcount, ret;
  514. if (plen == blen)
  515. return test_ahash_cycles_digest(req, blen, out);
  516. /* Warm-up run. */
  517. for (i = 0; i < 4; i++) {
  518. ret = crypto_ahash_init(req);
  519. if (ret)
  520. goto out;
  521. for (pcount = 0; pcount < blen; pcount += plen) {
  522. ret = do_one_ahash_op(req, crypto_ahash_update(req));
  523. if (ret)
  524. goto out;
  525. }
  526. ret = do_one_ahash_op(req, crypto_ahash_final(req));
  527. if (ret)
  528. goto out;
  529. }
  530. /* The real thing. */
  531. for (i = 0; i < 8; i++) {
  532. cycles_t start, end;
  533. start = get_cycles();
  534. ret = crypto_ahash_init(req);
  535. if (ret)
  536. goto out;
  537. for (pcount = 0; pcount < blen; pcount += plen) {
  538. ret = do_one_ahash_op(req, crypto_ahash_update(req));
  539. if (ret)
  540. goto out;
  541. }
  542. ret = do_one_ahash_op(req, crypto_ahash_final(req));
  543. if (ret)
  544. goto out;
  545. end = get_cycles();
  546. cycles += end - start;
  547. }
  548. out:
  549. if (ret)
  550. return ret;
  551. pr_cont("%6lu cycles/operation, %4lu cycles/byte\n",
  552. cycles / 8, cycles / (8 * blen));
  553. return 0;
  554. }
  555. static void test_ahash_speed(const char *algo, unsigned int sec,
  556. struct hash_speed *speed)
  557. {
  558. struct scatterlist sg[TVMEMSIZE];
  559. struct tcrypt_result tresult;
  560. struct ahash_request *req;
  561. struct crypto_ahash *tfm;
  562. static char output[1024];
  563. int i, ret;
  564. printk(KERN_INFO "\ntesting speed of async %s\n", algo);
  565. tfm = crypto_alloc_ahash(algo, 0, 0);
  566. if (IS_ERR(tfm)) {
  567. pr_err("failed to load transform for %s: %ld\n",
  568. algo, PTR_ERR(tfm));
  569. return;
  570. }
  571. if (crypto_ahash_digestsize(tfm) > sizeof(output)) {
  572. pr_err("digestsize(%u) > outputbuffer(%zu)\n",
  573. crypto_ahash_digestsize(tfm), sizeof(output));
  574. goto out;
  575. }
  576. test_hash_sg_init(sg);
  577. req = ahash_request_alloc(tfm, GFP_KERNEL);
  578. if (!req) {
  579. pr_err("ahash request allocation failure\n");
  580. goto out;
  581. }
  582. init_completion(&tresult.completion);
  583. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  584. tcrypt_complete, &tresult);
  585. for (i = 0; speed[i].blen != 0; i++) {
  586. if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
  587. pr_err("template (%u) too big for tvmem (%lu)\n",
  588. speed[i].blen, TVMEMSIZE * PAGE_SIZE);
  589. break;
  590. }
  591. pr_info("test%3u "
  592. "(%5u byte blocks,%5u bytes per update,%4u updates): ",
  593. i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
  594. ahash_request_set_crypt(req, sg, output, speed[i].plen);
  595. if (sec)
  596. ret = test_ahash_jiffies(req, speed[i].blen,
  597. speed[i].plen, output, sec);
  598. else
  599. ret = test_ahash_cycles(req, speed[i].blen,
  600. speed[i].plen, output);
  601. if (ret) {
  602. pr_err("hashing failed ret=%d\n", ret);
  603. break;
  604. }
  605. }
  606. ahash_request_free(req);
  607. out:
  608. crypto_free_ahash(tfm);
  609. }
  610. static inline int do_one_acipher_op(struct ablkcipher_request *req, int ret)
  611. {
  612. if (ret == -EINPROGRESS || ret == -EBUSY) {
  613. struct tcrypt_result *tr = req->base.data;
  614. ret = wait_for_completion_interruptible(&tr->completion);
  615. if (!ret)
  616. ret = tr->err;
  617. INIT_COMPLETION(tr->completion);
  618. }
  619. return ret;
  620. }
  621. static int test_acipher_jiffies(struct ablkcipher_request *req, int enc,
  622. int blen, int sec)
  623. {
  624. unsigned long start, end;
  625. int bcount;
  626. int ret;
  627. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  628. time_before(jiffies, end); bcount++) {
  629. if (enc)
  630. ret = do_one_acipher_op(req,
  631. crypto_ablkcipher_encrypt(req));
  632. else
  633. ret = do_one_acipher_op(req,
  634. crypto_ablkcipher_decrypt(req));
  635. if (ret)
  636. return ret;
  637. }
  638. pr_cont("%d operations in %d seconds (%ld bytes)\n",
  639. bcount, sec, (long)bcount * blen);
  640. return 0;
  641. }
  642. static int test_acipher_cycles(struct ablkcipher_request *req, int enc,
  643. int blen)
  644. {
  645. unsigned long cycles = 0;
  646. int ret = 0;
  647. int i;
  648. /* Warm-up run. */
  649. for (i = 0; i < 4; i++) {
  650. if (enc)
  651. ret = do_one_acipher_op(req,
  652. crypto_ablkcipher_encrypt(req));
  653. else
  654. ret = do_one_acipher_op(req,
  655. crypto_ablkcipher_decrypt(req));
  656. if (ret)
  657. goto out;
  658. }
  659. /* The real thing. */
  660. for (i = 0; i < 8; i++) {
  661. cycles_t start, end;
  662. start = get_cycles();
  663. if (enc)
  664. ret = do_one_acipher_op(req,
  665. crypto_ablkcipher_encrypt(req));
  666. else
  667. ret = do_one_acipher_op(req,
  668. crypto_ablkcipher_decrypt(req));
  669. end = get_cycles();
  670. if (ret)
  671. goto out;
  672. cycles += end - start;
  673. }
  674. out:
  675. if (ret == 0)
  676. pr_cont("1 operation in %lu cycles (%d bytes)\n",
  677. (cycles + 4) / 8, blen);
  678. return ret;
  679. }
  680. static void test_acipher_speed(const char *algo, int enc, unsigned int sec,
  681. struct cipher_speed_template *template,
  682. unsigned int tcount, u8 *keysize)
  683. {
  684. unsigned int ret, i, j, iv_len;
  685. struct tcrypt_result tresult;
  686. const char *key;
  687. char iv[128];
  688. struct ablkcipher_request *req;
  689. struct crypto_ablkcipher *tfm;
  690. const char *e;
  691. u32 *b_size;
  692. if (enc == ENCRYPT)
  693. e = "encryption";
  694. else
  695. e = "decryption";
  696. pr_info("\ntesting speed of async %s %s\n", algo, e);
  697. init_completion(&tresult.completion);
  698. tfm = crypto_alloc_ablkcipher(algo, 0, 0);
  699. if (IS_ERR(tfm)) {
  700. pr_err("failed to load transform for %s: %ld\n", algo,
  701. PTR_ERR(tfm));
  702. return;
  703. }
  704. req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
  705. if (!req) {
  706. pr_err("tcrypt: skcipher: Failed to allocate request for %s\n",
  707. algo);
  708. goto out;
  709. }
  710. ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  711. tcrypt_complete, &tresult);
  712. i = 0;
  713. do {
  714. b_size = block_sizes;
  715. do {
  716. struct scatterlist sg[TVMEMSIZE];
  717. if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
  718. pr_err("template (%u) too big for "
  719. "tvmem (%lu)\n", *keysize + *b_size,
  720. TVMEMSIZE * PAGE_SIZE);
  721. goto out_free_req;
  722. }
  723. pr_info("test %u (%d bit key, %d byte blocks): ", i,
  724. *keysize * 8, *b_size);
  725. memset(tvmem[0], 0xff, PAGE_SIZE);
  726. /* set key, plain text and IV */
  727. key = tvmem[0];
  728. for (j = 0; j < tcount; j++) {
  729. if (template[j].klen == *keysize) {
  730. key = template[j].key;
  731. break;
  732. }
  733. }
  734. crypto_ablkcipher_clear_flags(tfm, ~0);
  735. ret = crypto_ablkcipher_setkey(tfm, key, *keysize);
  736. if (ret) {
  737. pr_err("setkey() failed flags=%x\n",
  738. crypto_ablkcipher_get_flags(tfm));
  739. goto out_free_req;
  740. }
  741. sg_init_table(sg, TVMEMSIZE);
  742. sg_set_buf(sg, tvmem[0] + *keysize,
  743. PAGE_SIZE - *keysize);
  744. for (j = 1; j < TVMEMSIZE; j++) {
  745. sg_set_buf(sg + j, tvmem[j], PAGE_SIZE);
  746. memset(tvmem[j], 0xff, PAGE_SIZE);
  747. }
  748. iv_len = crypto_ablkcipher_ivsize(tfm);
  749. if (iv_len)
  750. memset(&iv, 0xff, iv_len);
  751. ablkcipher_request_set_crypt(req, sg, sg, *b_size, iv);
  752. if (sec)
  753. ret = test_acipher_jiffies(req, enc,
  754. *b_size, sec);
  755. else
  756. ret = test_acipher_cycles(req, enc,
  757. *b_size);
  758. if (ret) {
  759. pr_err("%s() failed flags=%x\n", e,
  760. crypto_ablkcipher_get_flags(tfm));
  761. break;
  762. }
  763. b_size++;
  764. i++;
  765. } while (*b_size);
  766. keysize++;
  767. } while (*keysize);
  768. out_free_req:
  769. ablkcipher_request_free(req);
  770. out:
  771. crypto_free_ablkcipher(tfm);
  772. }
  773. #ifdef CONFIG_CRYPTO_DRBG
  774. static inline int test_drbg(const char *drbg_core, int pr)
  775. {
  776. int pos = 0;
  777. char cra_driver_name[CRYPTO_MAX_ALG_NAME] = "";
  778. if(!drbg_core)
  779. return -EINVAL;
  780. if (pr) { /* with prediction resistance */
  781. memcpy(cra_driver_name, "drbg_pr_", 8);
  782. pos = 8;
  783. } else {
  784. memcpy(cra_driver_name, "drbg_nopr_", 10);
  785. pos = 10;
  786. }
  787. memcpy(cra_driver_name + pos, drbg_core, strlen(drbg_core));
  788. return alg_test(cra_driver_name, "stdrng", 0, 0);
  789. }
  790. #endif /* CONFIG_CRYPTO_DRBG */
  791. static void test_available(void)
  792. {
  793. char **name = check;
  794. while (*name) {
  795. printk("alg %s ", *name);
  796. printk(crypto_has_alg(*name, 0, 0) ?
  797. "found\n" : "not found\n");
  798. name++;
  799. }
  800. }
  801. static inline int tcrypt_test(const char *alg)
  802. {
  803. int ret;
  804. ret = alg_test(alg, alg, 0, 0);
  805. /* non-fips algs return -EINVAL in fips mode */
  806. if (fips_enabled && ret == -EINVAL)
  807. ret = 0;
  808. return ret;
  809. }
  810. static int do_test(int m)
  811. {
  812. int i;
  813. int ret = 0;
  814. switch (m) {
  815. case 0:
  816. for (i = 1; i < 200; i++)
  817. ret += do_test(i);
  818. break;
  819. case 1:
  820. ret += tcrypt_test("md5");
  821. break;
  822. case 2:
  823. ret += tcrypt_test("sha1");
  824. break;
  825. case 3:
  826. ret += tcrypt_test("ecb(des)");
  827. ret += tcrypt_test("cbc(des)");
  828. break;
  829. case 4:
  830. ret += tcrypt_test("ecb(des3_ede)");
  831. ret += tcrypt_test("cbc(des3_ede)");
  832. break;
  833. case 5:
  834. ret += tcrypt_test("md4");
  835. break;
  836. case 6:
  837. ret += tcrypt_test("sha256");
  838. break;
  839. case 7:
  840. ret += tcrypt_test("ecb(blowfish)");
  841. ret += tcrypt_test("cbc(blowfish)");
  842. ret += tcrypt_test("ctr(blowfish)");
  843. break;
  844. case 8:
  845. ret += tcrypt_test("ecb(twofish)");
  846. ret += tcrypt_test("cbc(twofish)");
  847. ret += tcrypt_test("ctr(twofish)");
  848. ret += tcrypt_test("lrw(twofish)");
  849. ret += tcrypt_test("xts(twofish)");
  850. break;
  851. case 9:
  852. ret += tcrypt_test("ecb(serpent)");
  853. ret += tcrypt_test("cbc(serpent)");
  854. ret += tcrypt_test("ctr(serpent)");
  855. ret += tcrypt_test("lrw(serpent)");
  856. ret += tcrypt_test("xts(serpent)");
  857. break;
  858. case 10:
  859. ret += tcrypt_test("ecb(aes)");
  860. ret += tcrypt_test("cbc(aes)");
  861. ret += tcrypt_test("lrw(aes)");
  862. #ifdef CONFIG_CRYPTO_XTS
  863. ret += tcrypt_test("xts(aes)");
  864. #endif
  865. #ifdef CONFIG_CRYPTO_CTR
  866. ret += tcrypt_test("ctr(aes)");
  867. ret += tcrypt_test("rfc3686(ctr(aes))");
  868. #endif
  869. break;
  870. case 11:
  871. ret += tcrypt_test("sha384");
  872. break;
  873. case 12:
  874. ret += tcrypt_test("sha512");
  875. break;
  876. case 13:
  877. ret += tcrypt_test("deflate");
  878. break;
  879. case 14:
  880. ret += tcrypt_test("ecb(cast5)");
  881. break;
  882. case 15:
  883. ret += tcrypt_test("ecb(cast6)");
  884. break;
  885. case 16:
  886. ret += tcrypt_test("ecb(arc4)");
  887. break;
  888. case 17:
  889. ret += tcrypt_test("michael_mic");
  890. break;
  891. case 18:
  892. ret += tcrypt_test("crc32c");
  893. break;
  894. case 19:
  895. ret += tcrypt_test("ecb(tea)");
  896. break;
  897. case 20:
  898. ret += tcrypt_test("ecb(xtea)");
  899. break;
  900. case 21:
  901. ret += tcrypt_test("ecb(khazad)");
  902. break;
  903. case 22:
  904. ret += tcrypt_test("wp512");
  905. break;
  906. case 23:
  907. ret += tcrypt_test("wp384");
  908. break;
  909. case 24:
  910. ret += tcrypt_test("wp256");
  911. break;
  912. case 25:
  913. ret += tcrypt_test("ecb(tnepres)");
  914. break;
  915. case 26:
  916. ret += tcrypt_test("ecb(anubis)");
  917. ret += tcrypt_test("cbc(anubis)");
  918. break;
  919. case 27:
  920. ret += tcrypt_test("tgr192");
  921. break;
  922. case 28:
  923. ret += tcrypt_test("tgr160");
  924. break;
  925. case 29:
  926. ret += tcrypt_test("tgr128");
  927. break;
  928. case 30:
  929. ret += tcrypt_test("ecb(xeta)");
  930. break;
  931. case 31:
  932. ret += tcrypt_test("pcbc(fcrypt)");
  933. break;
  934. case 32:
  935. ret += tcrypt_test("ecb(camellia)");
  936. ret += tcrypt_test("cbc(camellia)");
  937. break;
  938. case 33:
  939. ret += tcrypt_test("sha224");
  940. break;
  941. case 34:
  942. ret += tcrypt_test("salsa20");
  943. break;
  944. case 35:
  945. #ifdef CONFIG_CRYPTO_GCM
  946. ret += tcrypt_test("gcm(aes)");
  947. #endif
  948. break;
  949. case 36:
  950. ret += tcrypt_test("lzo");
  951. break;
  952. case 37:
  953. #ifdef CONFIG_CRYPTO_CCM
  954. ret += tcrypt_test("ccm(aes)");
  955. break;
  956. #endif
  957. case 38:
  958. ret += tcrypt_test("cts(cbc(aes))");
  959. break;
  960. case 39:
  961. ret += tcrypt_test("rmd128");
  962. break;
  963. case 40:
  964. ret += tcrypt_test("rmd160");
  965. break;
  966. case 41:
  967. ret += tcrypt_test("rmd256");
  968. break;
  969. case 42:
  970. ret += tcrypt_test("rmd320");
  971. break;
  972. case 43:
  973. ret += tcrypt_test("ecb(seed)");
  974. break;
  975. case 44:
  976. ret += tcrypt_test("zlib");
  977. break;
  978. case 45:
  979. #ifdef CONFIG_CRYPTO_CCM
  980. ret += tcrypt_test("rfc4309(ccm(aes))");
  981. break;
  982. #endif
  983. case 100:
  984. ret += tcrypt_test("hmac(md5)");
  985. break;
  986. case 101:
  987. ret += tcrypt_test("hmac(sha1)");
  988. break;
  989. case 102:
  990. ret += tcrypt_test("hmac(sha256)");
  991. break;
  992. case 103:
  993. ret += tcrypt_test("hmac(sha384)");
  994. break;
  995. case 104:
  996. ret += tcrypt_test("hmac(sha512)");
  997. break;
  998. case 105:
  999. ret += tcrypt_test("hmac(sha224)");
  1000. break;
  1001. case 106:
  1002. ret += tcrypt_test("xcbc(aes)");
  1003. break;
  1004. case 107:
  1005. ret += tcrypt_test("hmac(rmd128)");
  1006. break;
  1007. case 108:
  1008. ret += tcrypt_test("hmac(rmd160)");
  1009. break;
  1010. case 109:
  1011. ret += tcrypt_test("vmac(aes)");
  1012. break;
  1013. case 150:
  1014. ret += tcrypt_test("ansi_cprng");
  1015. break;
  1016. case 151:
  1017. #ifdef CONFIG_CRYPTO_GCM
  1018. ret += tcrypt_test("rfc4106(gcm(aes))");
  1019. #endif
  1020. break;
  1021. case 200:
  1022. test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
  1023. speed_template_16_24_32);
  1024. test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
  1025. speed_template_16_24_32);
  1026. test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
  1027. speed_template_16_24_32);
  1028. test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
  1029. speed_template_16_24_32);
  1030. test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
  1031. speed_template_32_40_48);
  1032. test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
  1033. speed_template_32_40_48);
  1034. test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
  1035. speed_template_32_48_64);
  1036. test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
  1037. speed_template_32_48_64);
  1038. test_cipher_speed("ctr(aes)", ENCRYPT, sec, NULL, 0,
  1039. speed_template_16_24_32);
  1040. test_cipher_speed("ctr(aes)", DECRYPT, sec, NULL, 0,
  1041. speed_template_16_24_32);
  1042. break;
  1043. case 201:
  1044. test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
  1045. des3_speed_template, DES3_SPEED_VECTORS,
  1046. speed_template_24);
  1047. test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
  1048. des3_speed_template, DES3_SPEED_VECTORS,
  1049. speed_template_24);
  1050. test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
  1051. des3_speed_template, DES3_SPEED_VECTORS,
  1052. speed_template_24);
  1053. test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
  1054. des3_speed_template, DES3_SPEED_VECTORS,
  1055. speed_template_24);
  1056. break;
  1057. case 202:
  1058. test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
  1059. speed_template_16_24_32);
  1060. test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
  1061. speed_template_16_24_32);
  1062. test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
  1063. speed_template_16_24_32);
  1064. test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
  1065. speed_template_16_24_32);
  1066. test_cipher_speed("ctr(twofish)", ENCRYPT, sec, NULL, 0,
  1067. speed_template_16_24_32);
  1068. test_cipher_speed("ctr(twofish)", DECRYPT, sec, NULL, 0,
  1069. speed_template_16_24_32);
  1070. test_cipher_speed("lrw(twofish)", ENCRYPT, sec, NULL, 0,
  1071. speed_template_32_40_48);
  1072. test_cipher_speed("lrw(twofish)", DECRYPT, sec, NULL, 0,
  1073. speed_template_32_40_48);
  1074. test_cipher_speed("xts(twofish)", ENCRYPT, sec, NULL, 0,
  1075. speed_template_32_48_64);
  1076. test_cipher_speed("xts(twofish)", DECRYPT, sec, NULL, 0,
  1077. speed_template_32_48_64);
  1078. break;
  1079. case 203:
  1080. test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
  1081. speed_template_8_32);
  1082. test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
  1083. speed_template_8_32);
  1084. test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
  1085. speed_template_8_32);
  1086. test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
  1087. speed_template_8_32);
  1088. test_cipher_speed("ctr(blowfish)", ENCRYPT, sec, NULL, 0,
  1089. speed_template_8_32);
  1090. test_cipher_speed("ctr(blowfish)", DECRYPT, sec, NULL, 0,
  1091. speed_template_8_32);
  1092. break;
  1093. case 204:
  1094. test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
  1095. speed_template_8);
  1096. test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
  1097. speed_template_8);
  1098. test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
  1099. speed_template_8);
  1100. test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
  1101. speed_template_8);
  1102. break;
  1103. case 205:
  1104. test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
  1105. speed_template_16_24_32);
  1106. test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
  1107. speed_template_16_24_32);
  1108. test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
  1109. speed_template_16_24_32);
  1110. test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
  1111. speed_template_16_24_32);
  1112. test_cipher_speed("ctr(camellia)", ENCRYPT, sec, NULL, 0,
  1113. speed_template_16_24_32);
  1114. test_cipher_speed("ctr(camellia)", DECRYPT, sec, NULL, 0,
  1115. speed_template_16_24_32);
  1116. test_cipher_speed("lrw(camellia)", ENCRYPT, sec, NULL, 0,
  1117. speed_template_32_40_48);
  1118. test_cipher_speed("lrw(camellia)", DECRYPT, sec, NULL, 0,
  1119. speed_template_32_40_48);
  1120. test_cipher_speed("xts(camellia)", ENCRYPT, sec, NULL, 0,
  1121. speed_template_32_48_64);
  1122. test_cipher_speed("xts(camellia)", DECRYPT, sec, NULL, 0,
  1123. speed_template_32_48_64);
  1124. break;
  1125. case 206:
  1126. test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
  1127. speed_template_16_32);
  1128. break;
  1129. case 207:
  1130. test_cipher_speed("ecb(serpent)", ENCRYPT, sec, NULL, 0,
  1131. speed_template_16_32);
  1132. test_cipher_speed("ecb(serpent)", DECRYPT, sec, NULL, 0,
  1133. speed_template_16_32);
  1134. test_cipher_speed("cbc(serpent)", ENCRYPT, sec, NULL, 0,
  1135. speed_template_16_32);
  1136. test_cipher_speed("cbc(serpent)", DECRYPT, sec, NULL, 0,
  1137. speed_template_16_32);
  1138. test_cipher_speed("ctr(serpent)", ENCRYPT, sec, NULL, 0,
  1139. speed_template_16_32);
  1140. test_cipher_speed("ctr(serpent)", DECRYPT, sec, NULL, 0,
  1141. speed_template_16_32);
  1142. test_cipher_speed("lrw(serpent)", ENCRYPT, sec, NULL, 0,
  1143. speed_template_32_48);
  1144. test_cipher_speed("lrw(serpent)", DECRYPT, sec, NULL, 0,
  1145. speed_template_32_48);
  1146. test_cipher_speed("xts(serpent)", ENCRYPT, sec, NULL, 0,
  1147. speed_template_32_64);
  1148. test_cipher_speed("xts(serpent)", DECRYPT, sec, NULL, 0,
  1149. speed_template_32_64);
  1150. break;
  1151. case 300:
  1152. /* fall through */
  1153. case 301:
  1154. test_hash_speed("md4", sec, generic_hash_speed_template);
  1155. if (mode > 300 && mode < 400) break;
  1156. case 302:
  1157. test_hash_speed("md5", sec, generic_hash_speed_template);
  1158. if (mode > 300 && mode < 400) break;
  1159. case 303:
  1160. test_hash_speed("sha1", sec, generic_hash_speed_template);
  1161. if (mode > 300 && mode < 400) break;
  1162. case 304:
  1163. test_hash_speed("sha256", sec, generic_hash_speed_template);
  1164. if (mode > 300 && mode < 400) break;
  1165. case 305:
  1166. test_hash_speed("sha384", sec, generic_hash_speed_template);
  1167. if (mode > 300 && mode < 400) break;
  1168. case 306:
  1169. test_hash_speed("sha512", sec, generic_hash_speed_template);
  1170. if (mode > 300 && mode < 400) break;
  1171. case 307:
  1172. test_hash_speed("wp256", sec, generic_hash_speed_template);
  1173. if (mode > 300 && mode < 400) break;
  1174. case 308:
  1175. test_hash_speed("wp384", sec, generic_hash_speed_template);
  1176. if (mode > 300 && mode < 400) break;
  1177. case 309:
  1178. test_hash_speed("wp512", sec, generic_hash_speed_template);
  1179. if (mode > 300 && mode < 400) break;
  1180. case 310:
  1181. test_hash_speed("tgr128", sec, generic_hash_speed_template);
  1182. if (mode > 300 && mode < 400) break;
  1183. case 311:
  1184. test_hash_speed("tgr160", sec, generic_hash_speed_template);
  1185. if (mode > 300 && mode < 400) break;
  1186. case 312:
  1187. test_hash_speed("tgr192", sec, generic_hash_speed_template);
  1188. if (mode > 300 && mode < 400) break;
  1189. case 313:
  1190. test_hash_speed("sha224", sec, generic_hash_speed_template);
  1191. if (mode > 300 && mode < 400) break;
  1192. case 314:
  1193. test_hash_speed("rmd128", sec, generic_hash_speed_template);
  1194. if (mode > 300 && mode < 400) break;
  1195. case 315:
  1196. test_hash_speed("rmd160", sec, generic_hash_speed_template);
  1197. if (mode > 300 && mode < 400) break;
  1198. case 316:
  1199. test_hash_speed("rmd256", sec, generic_hash_speed_template);
  1200. if (mode > 300 && mode < 400) break;
  1201. case 317:
  1202. test_hash_speed("rmd320", sec, generic_hash_speed_template);
  1203. if (mode > 300 && mode < 400) break;
  1204. case 318:
  1205. test_hash_speed("ghash-generic", sec, hash_speed_template_16);
  1206. if (mode > 300 && mode < 400) break;
  1207. case 399:
  1208. break;
  1209. case 400:
  1210. /* fall through */
  1211. case 401:
  1212. test_ahash_speed("md4", sec, generic_hash_speed_template);
  1213. if (mode > 400 && mode < 500) break;
  1214. case 402:
  1215. test_ahash_speed("md5", sec, generic_hash_speed_template);
  1216. if (mode > 400 && mode < 500) break;
  1217. case 403:
  1218. test_ahash_speed("sha1", sec, generic_hash_speed_template);
  1219. if (mode > 400 && mode < 500) break;
  1220. case 404:
  1221. test_ahash_speed("sha256", sec, generic_hash_speed_template);
  1222. if (mode > 400 && mode < 500) break;
  1223. case 405:
  1224. test_ahash_speed("sha384", sec, generic_hash_speed_template);
  1225. if (mode > 400 && mode < 500) break;
  1226. case 406:
  1227. test_ahash_speed("sha512", sec, generic_hash_speed_template);
  1228. if (mode > 400 && mode < 500) break;
  1229. case 407:
  1230. test_ahash_speed("wp256", sec, generic_hash_speed_template);
  1231. if (mode > 400 && mode < 500) break;
  1232. case 408:
  1233. test_ahash_speed("wp384", sec, generic_hash_speed_template);
  1234. if (mode > 400 && mode < 500) break;
  1235. case 409:
  1236. test_ahash_speed("wp512", sec, generic_hash_speed_template);
  1237. if (mode > 400 && mode < 500) break;
  1238. case 410:
  1239. test_ahash_speed("tgr128", sec, generic_hash_speed_template);
  1240. if (mode > 400 && mode < 500) break;
  1241. case 411:
  1242. test_ahash_speed("tgr160", sec, generic_hash_speed_template);
  1243. if (mode > 400 && mode < 500) break;
  1244. case 412:
  1245. test_ahash_speed("tgr192", sec, generic_hash_speed_template);
  1246. if (mode > 400 && mode < 500) break;
  1247. case 413:
  1248. test_ahash_speed("sha224", sec, generic_hash_speed_template);
  1249. if (mode > 400 && mode < 500) break;
  1250. case 414:
  1251. test_ahash_speed("rmd128", sec, generic_hash_speed_template);
  1252. if (mode > 400 && mode < 500) break;
  1253. case 415:
  1254. test_ahash_speed("rmd160", sec, generic_hash_speed_template);
  1255. if (mode > 400 && mode < 500) break;
  1256. case 416:
  1257. test_ahash_speed("rmd256", sec, generic_hash_speed_template);
  1258. if (mode > 400 && mode < 500) break;
  1259. case 417:
  1260. test_ahash_speed("rmd320", sec, generic_hash_speed_template);
  1261. if (mode > 400 && mode < 500) break;
  1262. case 499:
  1263. break;
  1264. case 500:
  1265. test_acipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
  1266. speed_template_16_24_32);
  1267. test_acipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
  1268. speed_template_16_24_32);
  1269. test_acipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
  1270. speed_template_16_24_32);
  1271. test_acipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
  1272. speed_template_16_24_32);
  1273. test_acipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
  1274. speed_template_32_40_48);
  1275. test_acipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
  1276. speed_template_32_40_48);
  1277. test_acipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
  1278. speed_template_32_48_64);
  1279. test_acipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
  1280. speed_template_32_48_64);
  1281. test_acipher_speed("ctr(aes)", ENCRYPT, sec, NULL, 0,
  1282. speed_template_16_24_32);
  1283. test_acipher_speed("ctr(aes)", DECRYPT, sec, NULL, 0,
  1284. speed_template_16_24_32);
  1285. break;
  1286. case 501:
  1287. test_acipher_speed("ecb(des3_ede)", ENCRYPT, sec,
  1288. des3_speed_template, DES3_SPEED_VECTORS,
  1289. speed_template_24);
  1290. test_acipher_speed("ecb(des3_ede)", DECRYPT, sec,
  1291. des3_speed_template, DES3_SPEED_VECTORS,
  1292. speed_template_24);
  1293. test_acipher_speed("cbc(des3_ede)", ENCRYPT, sec,
  1294. des3_speed_template, DES3_SPEED_VECTORS,
  1295. speed_template_24);
  1296. test_acipher_speed("cbc(des3_ede)", DECRYPT, sec,
  1297. des3_speed_template, DES3_SPEED_VECTORS,
  1298. speed_template_24);
  1299. break;
  1300. case 502:
  1301. test_acipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
  1302. speed_template_8);
  1303. test_acipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
  1304. speed_template_8);
  1305. test_acipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
  1306. speed_template_8);
  1307. test_acipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
  1308. speed_template_8);
  1309. break;
  1310. case 503:
  1311. test_acipher_speed("ecb(serpent)", ENCRYPT, sec, NULL, 0,
  1312. speed_template_16_32);
  1313. test_acipher_speed("ecb(serpent)", DECRYPT, sec, NULL, 0,
  1314. speed_template_16_32);
  1315. test_acipher_speed("cbc(serpent)", ENCRYPT, sec, NULL, 0,
  1316. speed_template_16_32);
  1317. test_acipher_speed("cbc(serpent)", DECRYPT, sec, NULL, 0,
  1318. speed_template_16_32);
  1319. test_acipher_speed("ctr(serpent)", ENCRYPT, sec, NULL, 0,
  1320. speed_template_16_32);
  1321. test_acipher_speed("ctr(serpent)", DECRYPT, sec, NULL, 0,
  1322. speed_template_16_32);
  1323. test_acipher_speed("lrw(serpent)", ENCRYPT, sec, NULL, 0,
  1324. speed_template_32_48);
  1325. test_acipher_speed("lrw(serpent)", DECRYPT, sec, NULL, 0,
  1326. speed_template_32_48);
  1327. test_acipher_speed("xts(serpent)", ENCRYPT, sec, NULL, 0,
  1328. speed_template_32_64);
  1329. test_acipher_speed("xts(serpent)", DECRYPT, sec, NULL, 0,
  1330. speed_template_32_64);
  1331. break;
  1332. case 1000:
  1333. test_available();
  1334. break;
  1335. #ifdef CONFIG_CRYPTO_FIPS
  1336. case 1402 : //For FIPS 140-2
  1337. printk(KERN_ERR "FIPS : Tcrypt Tests Start\n");
  1338. /* AES */
  1339. ret += alg_test("ecb(aes-generic)", "ecb(aes)", 0, 0);
  1340. ret += alg_test("cbc(aes-generic)", "cbc(aes)", 0, 0);
  1341. #ifdef CONFIG_CRYPTO_AES_ARM
  1342. ret += alg_test("ecb(aes-asm)", "ecb(aes)", 0, 0);
  1343. ret += alg_test("cbc(aes-asm)", "cbc(aes)", 0, 0);
  1344. #endif
  1345. /* 3DES */
  1346. ret += alg_test("ecb(des3_ede-generic)", "ecb(des3_ede)", 0, 0);
  1347. ret += alg_test("cbc(des3_ede-generic)", "cbc(des3_ede)", 0, 0);
  1348. /* SHA */
  1349. ret += alg_test("sha1-generic", "sha1", 0, 0);
  1350. ret += alg_test("sha224-generic", "sha224", 0, 0);
  1351. ret += alg_test("sha256-generic", "sha256", 0, 0);
  1352. ret += alg_test("sha384-generic", "sha384", 0, 0);
  1353. ret += alg_test("sha512-generic", "sha512", 0, 0);
  1354. #ifdef CONFIG_CRYPTO_SHA1_ARM
  1355. ret += alg_test("sha1-asm", "sha1", 0, 0);
  1356. ret += alg_test("hmac(sha1-asm)", "hmac(sha1)", 0, 0);
  1357. #endif
  1358. /* HMAC */
  1359. ret += alg_test("hmac(sha1-generic)", "hmac(sha1)", 0, 0);
  1360. ret += alg_test("hmac(sha224-generic)", "hmac(sha224)", 0, 0);
  1361. ret += alg_test("hmac(sha256-generic)", "hmac(sha256)", 0, 0);
  1362. ret += alg_test("hmac(sha384-generic)", "hmac(sha384)", 0, 0);
  1363. ret += alg_test("hmac(sha512-generic)", "hmac(sha512)", 0, 0);
  1364. #ifdef CONFIG_CRYPTO_ANSI_CPRNG
  1365. /* RNG */
  1366. ret += alg_test("fips_ansi_cprng", "ansi_cprng", 0, 0);
  1367. #endif
  1368. #ifdef CONFIG_CRYPTO_DRBG
  1369. /* DRBG */
  1370. for (i = 0; ARRAY_SIZE(drbg_cores) > i; i++)
  1371. ret += test_drbg(drbg_cores[i], 0); /* no prediction resistance */
  1372. for (i = 0; ARRAY_SIZE(drbg_cores) > i; i++)
  1373. ret += test_drbg(drbg_cores[i], 1); /* with prediction resistance */
  1374. #endif
  1375. printk(KERN_ERR "FIPS : Tcrypt Tests End\n");
  1376. break;
  1377. #endif //CONFIG_CRYPTO_FIPS
  1378. }
  1379. return ret;
  1380. }
  1381. static int do_alg_test(const char *alg, u32 type, u32 mask)
  1382. {
  1383. return crypto_has_alg(alg, type, mask ?: CRYPTO_ALG_TYPE_MASK) ?
  1384. 0 : -ENOENT;
  1385. }
  1386. static int __init tcrypt_mod_init(void)
  1387. {
  1388. int err = -ENOMEM;
  1389. int i;
  1390. for (i = 0; i < TVMEMSIZE; i++) {
  1391. tvmem[i] = (void *)__get_free_page(GFP_KERNEL);
  1392. if (!tvmem[i])
  1393. goto err_free_tv;
  1394. }
  1395. #ifdef CONFIG_CRYPTO_FIPS
  1396. testmgr_crypto_proc_init();
  1397. mode = 1402; //For FIPS 140-2
  1398. #endif
  1399. if (alg)
  1400. err = do_alg_test(alg, type, mask);
  1401. else
  1402. err = do_test(mode);
  1403. #if FIPS_FUNC_TEST == 1
  1404. printk(KERN_ERR "FIPS FUNC TEST: Do test again\n");
  1405. do_test(mode);
  1406. #else
  1407. if (err) {
  1408. printk(KERN_ERR "tcrypt: one or more tests failed!\n");
  1409. goto err_free_tv;
  1410. #ifndef CONFIG_CRYPTO_FIPS
  1411. }
  1412. #else
  1413. } else {
  1414. do_integrity_check();
  1415. if(in_fips_err()) {
  1416. printk(KERN_ERR "tcrypt: CRYPTO API in FIPS Error!!!\n");
  1417. } else {
  1418. printk(KERN_ERR "tcrypt: CRYPTO API started in FIPS mode!!!\n");
  1419. }
  1420. #ifdef CONFIG_CRYPTO_FIPS_OLD_INTEGRITY_CHECK
  1421. if (integrity_mem_reservoir != 0) {
  1422. printk(KERN_NOTICE "FIPS free integrity_mem_reservoir = %ld\n", integrity_mem_reservoir);
  1423. free_bootmem_late((unsigned long)CONFIG_CRYPTO_FIPS_INTEG_COPY_ADDRESS, integrity_mem_reservoir);
  1424. integrity_mem_reservoir = 0;
  1425. }
  1426. #endif
  1427. }
  1428. #endif
  1429. #endif /* FIPS_FUNC_TEST */
  1430. /* We intentionaly return -EAGAIN to prevent keeping the module,
  1431. * unless we're running in fips mode. It does all its work from
  1432. * init() and doesn't offer any runtime functionality, but in
  1433. * the fips case, checking for a successful load is helpful.
  1434. * => we don't need it in the memory, do we?
  1435. * -- mludvig
  1436. */
  1437. if (!fips_enabled)
  1438. err = -EAGAIN;
  1439. err_free_tv:
  1440. for (i = 0; i < TVMEMSIZE && tvmem[i]; i++)
  1441. free_page((unsigned long)tvmem[i]);
  1442. return err;
  1443. }
  1444. /*
  1445. * If an init function is provided, an exit function must also be provided
  1446. * to allow module unload.
  1447. */
  1448. static void __exit tcrypt_mod_fini(void) { }
  1449. module_init(tcrypt_mod_init);
  1450. module_exit(tcrypt_mod_fini);
  1451. module_param(alg, charp, 0);
  1452. module_param(type, uint, 0);
  1453. module_param(mask, uint, 0);
  1454. module_param(mode, int, 0);
  1455. module_param(sec, uint, 0);
  1456. MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
  1457. "(defaults to zero which uses CPU cycles instead)");
  1458. MODULE_LICENSE("GPL");
  1459. MODULE_DESCRIPTION("Quick & dirty crypto testing module");
  1460. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");