tcrypt.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  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. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the Free
  12. * Software Foundation; either version 2 of the License, or (at your option)
  13. * any later version.
  14. *
  15. * 2006-12-07 Added SHA384 HMAC and SHA512 HMAC tests
  16. * 2004-08-09 Added cipher speed tests (Reyk Floeter <reyk@vantronix.net>)
  17. * 2003-09-14 Rewritten by Kartikey Mahendra Bhatt
  18. *
  19. */
  20. #include <linux/err.h>
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/mm.h>
  24. #include <linux/slab.h>
  25. #include <linux/scatterlist.h>
  26. #include <linux/string.h>
  27. #include <linux/crypto.h>
  28. #include <linux/highmem.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/jiffies.h>
  31. #include <linux/timex.h>
  32. #include <linux/interrupt.h>
  33. #include "tcrypt.h"
  34. /*
  35. * Need to kmalloc() memory for testing kmap().
  36. */
  37. #define TVMEMSIZE 16384
  38. #define XBUFSIZE 32768
  39. /*
  40. * Indexes into the xbuf to simulate cross-page access.
  41. */
  42. #define IDX1 37
  43. #define IDX2 32400
  44. #define IDX3 1
  45. #define IDX4 8193
  46. #define IDX5 22222
  47. #define IDX6 17101
  48. #define IDX7 27333
  49. #define IDX8 3000
  50. /*
  51. * Used by test_cipher()
  52. */
  53. #define ENCRYPT 1
  54. #define DECRYPT 0
  55. struct tcrypt_result {
  56. struct completion completion;
  57. int err;
  58. };
  59. static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
  60. /*
  61. * Used by test_cipher_speed()
  62. */
  63. static unsigned int sec;
  64. static int mode;
  65. static char *xbuf;
  66. static char *tvmem;
  67. static char *check[] = {
  68. "des", "md5", "des3_ede", "rot13", "sha1", "sha256", "blowfish",
  69. "twofish", "serpent", "sha384", "sha512", "md4", "aes", "cast6",
  70. "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
  71. "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", "fcrypt",
  72. "camellia", NULL
  73. };
  74. static void hexdump(unsigned char *buf, unsigned int len)
  75. {
  76. while (len--)
  77. printk("%02x", *buf++);
  78. printk("\n");
  79. }
  80. static void tcrypt_complete(struct crypto_async_request *req, int err)
  81. {
  82. struct tcrypt_result *res = req->data;
  83. if (err == -EINPROGRESS)
  84. return;
  85. res->err = err;
  86. complete(&res->completion);
  87. }
  88. static void test_hash(char *algo, struct hash_testvec *template,
  89. unsigned int tcount)
  90. {
  91. unsigned int i, j, k, temp;
  92. struct scatterlist sg[8];
  93. char result[64];
  94. struct crypto_hash *tfm;
  95. struct hash_desc desc;
  96. struct hash_testvec *hash_tv;
  97. unsigned int tsize;
  98. int ret;
  99. printk("\ntesting %s\n", algo);
  100. tsize = sizeof(struct hash_testvec);
  101. tsize *= tcount;
  102. if (tsize > TVMEMSIZE) {
  103. printk("template (%u) too big for tvmem (%u)\n", tsize, TVMEMSIZE);
  104. return;
  105. }
  106. memcpy(tvmem, template, tsize);
  107. hash_tv = (void *)tvmem;
  108. tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
  109. if (IS_ERR(tfm)) {
  110. printk("failed to load transform for %s: %ld\n", algo,
  111. PTR_ERR(tfm));
  112. return;
  113. }
  114. desc.tfm = tfm;
  115. desc.flags = 0;
  116. for (i = 0; i < tcount; i++) {
  117. printk("test %u:\n", i + 1);
  118. memset(result, 0, 64);
  119. sg_set_buf(&sg[0], hash_tv[i].plaintext, hash_tv[i].psize);
  120. if (hash_tv[i].ksize) {
  121. ret = crypto_hash_setkey(tfm, hash_tv[i].key,
  122. hash_tv[i].ksize);
  123. if (ret) {
  124. printk("setkey() failed ret=%d\n", ret);
  125. goto out;
  126. }
  127. }
  128. ret = crypto_hash_digest(&desc, sg, hash_tv[i].psize, result);
  129. if (ret) {
  130. printk("digest () failed ret=%d\n", ret);
  131. goto out;
  132. }
  133. hexdump(result, crypto_hash_digestsize(tfm));
  134. printk("%s\n",
  135. memcmp(result, hash_tv[i].digest,
  136. crypto_hash_digestsize(tfm)) ?
  137. "fail" : "pass");
  138. }
  139. printk("testing %s across pages\n", algo);
  140. /* setup the dummy buffer first */
  141. memset(xbuf, 0, XBUFSIZE);
  142. j = 0;
  143. for (i = 0; i < tcount; i++) {
  144. if (hash_tv[i].np) {
  145. j++;
  146. printk("test %u:\n", j);
  147. memset(result, 0, 64);
  148. temp = 0;
  149. for (k = 0; k < hash_tv[i].np; k++) {
  150. memcpy(&xbuf[IDX[k]],
  151. hash_tv[i].plaintext + temp,
  152. hash_tv[i].tap[k]);
  153. temp += hash_tv[i].tap[k];
  154. sg_set_buf(&sg[k], &xbuf[IDX[k]],
  155. hash_tv[i].tap[k]);
  156. }
  157. if (hash_tv[i].ksize) {
  158. ret = crypto_hash_setkey(tfm, hash_tv[i].key,
  159. hash_tv[i].ksize);
  160. if (ret) {
  161. printk("setkey() failed ret=%d\n", ret);
  162. goto out;
  163. }
  164. }
  165. ret = crypto_hash_digest(&desc, sg, hash_tv[i].psize,
  166. result);
  167. if (ret) {
  168. printk("digest () failed ret=%d\n", ret);
  169. goto out;
  170. }
  171. hexdump(result, crypto_hash_digestsize(tfm));
  172. printk("%s\n",
  173. memcmp(result, hash_tv[i].digest,
  174. crypto_hash_digestsize(tfm)) ?
  175. "fail" : "pass");
  176. }
  177. }
  178. out:
  179. crypto_free_hash(tfm);
  180. }
  181. static void test_cipher(char *algo, int enc,
  182. struct cipher_testvec *template, unsigned int tcount)
  183. {
  184. unsigned int ret, i, j, k, temp;
  185. unsigned int tsize;
  186. char *q;
  187. struct crypto_ablkcipher *tfm;
  188. char *key;
  189. struct cipher_testvec *cipher_tv;
  190. struct ablkcipher_request *req;
  191. struct scatterlist sg[8];
  192. const char *e;
  193. struct tcrypt_result result;
  194. if (enc == ENCRYPT)
  195. e = "encryption";
  196. else
  197. e = "decryption";
  198. printk("\ntesting %s %s\n", algo, e);
  199. tsize = sizeof (struct cipher_testvec);
  200. tsize *= tcount;
  201. if (tsize > TVMEMSIZE) {
  202. printk("template (%u) too big for tvmem (%u)\n", tsize,
  203. TVMEMSIZE);
  204. return;
  205. }
  206. memcpy(tvmem, template, tsize);
  207. cipher_tv = (void *)tvmem;
  208. init_completion(&result.completion);
  209. tfm = crypto_alloc_ablkcipher(algo, 0, 0);
  210. if (IS_ERR(tfm)) {
  211. printk("failed to load transform for %s: %ld\n", algo,
  212. PTR_ERR(tfm));
  213. return;
  214. }
  215. req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
  216. if (!req) {
  217. printk("failed to allocate request for %s\n", algo);
  218. goto out;
  219. }
  220. ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  221. tcrypt_complete, &result);
  222. j = 0;
  223. for (i = 0; i < tcount; i++) {
  224. if (!(cipher_tv[i].np)) {
  225. j++;
  226. printk("test %u (%d bit key):\n",
  227. j, cipher_tv[i].klen * 8);
  228. crypto_ablkcipher_clear_flags(tfm, ~0);
  229. if (cipher_tv[i].wk)
  230. crypto_ablkcipher_set_flags(
  231. tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  232. key = cipher_tv[i].key;
  233. ret = crypto_ablkcipher_setkey(tfm, key,
  234. cipher_tv[i].klen);
  235. if (ret) {
  236. printk("setkey() failed flags=%x\n",
  237. crypto_ablkcipher_get_flags(tfm));
  238. if (!cipher_tv[i].fail)
  239. goto out;
  240. }
  241. sg_set_buf(&sg[0], cipher_tv[i].input,
  242. cipher_tv[i].ilen);
  243. ablkcipher_request_set_crypt(req, sg, sg,
  244. cipher_tv[i].ilen,
  245. cipher_tv[i].iv);
  246. ret = enc ?
  247. crypto_ablkcipher_encrypt(req) :
  248. crypto_ablkcipher_decrypt(req);
  249. switch (ret) {
  250. case 0:
  251. break;
  252. case -EINPROGRESS:
  253. case -EBUSY:
  254. ret = wait_for_completion_interruptible(
  255. &result.completion);
  256. if (!ret && !((ret = result.err))) {
  257. INIT_COMPLETION(result.completion);
  258. break;
  259. }
  260. /* fall through */
  261. default:
  262. printk("%s () failed err=%d\n", e, -ret);
  263. goto out;
  264. }
  265. q = kmap(sg[0].page) + sg[0].offset;
  266. hexdump(q, cipher_tv[i].rlen);
  267. printk("%s\n",
  268. memcmp(q, cipher_tv[i].result,
  269. cipher_tv[i].rlen) ? "fail" : "pass");
  270. }
  271. }
  272. printk("\ntesting %s %s across pages (chunking)\n", algo, e);
  273. memset(xbuf, 0, XBUFSIZE);
  274. j = 0;
  275. for (i = 0; i < tcount; i++) {
  276. if (cipher_tv[i].np) {
  277. j++;
  278. printk("test %u (%d bit key):\n",
  279. j, cipher_tv[i].klen * 8);
  280. crypto_ablkcipher_clear_flags(tfm, ~0);
  281. if (cipher_tv[i].wk)
  282. crypto_ablkcipher_set_flags(
  283. tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  284. key = cipher_tv[i].key;
  285. ret = crypto_ablkcipher_setkey(tfm, key,
  286. cipher_tv[i].klen);
  287. if (ret) {
  288. printk("setkey() failed flags=%x\n",
  289. crypto_ablkcipher_get_flags(tfm));
  290. if (!cipher_tv[i].fail)
  291. goto out;
  292. }
  293. temp = 0;
  294. for (k = 0; k < cipher_tv[i].np; k++) {
  295. memcpy(&xbuf[IDX[k]],
  296. cipher_tv[i].input + temp,
  297. cipher_tv[i].tap[k]);
  298. temp += cipher_tv[i].tap[k];
  299. sg_set_buf(&sg[k], &xbuf[IDX[k]],
  300. cipher_tv[i].tap[k]);
  301. }
  302. ablkcipher_request_set_crypt(req, sg, sg,
  303. cipher_tv[i].ilen,
  304. cipher_tv[i].iv);
  305. ret = enc ?
  306. crypto_ablkcipher_encrypt(req) :
  307. crypto_ablkcipher_decrypt(req);
  308. switch (ret) {
  309. case 0:
  310. break;
  311. case -EINPROGRESS:
  312. case -EBUSY:
  313. ret = wait_for_completion_interruptible(
  314. &result.completion);
  315. if (!ret && !((ret = result.err))) {
  316. INIT_COMPLETION(result.completion);
  317. break;
  318. }
  319. /* fall through */
  320. default:
  321. printk("%s () failed err=%d\n", e, -ret);
  322. goto out;
  323. }
  324. temp = 0;
  325. for (k = 0; k < cipher_tv[i].np; k++) {
  326. printk("page %u\n", k);
  327. q = kmap(sg[k].page) + sg[k].offset;
  328. hexdump(q, cipher_tv[i].tap[k]);
  329. printk("%s\n",
  330. memcmp(q, cipher_tv[i].result + temp,
  331. cipher_tv[i].tap[k]) ? "fail" :
  332. "pass");
  333. temp += cipher_tv[i].tap[k];
  334. }
  335. }
  336. }
  337. out:
  338. crypto_free_ablkcipher(tfm);
  339. ablkcipher_request_free(req);
  340. }
  341. static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc, char *p,
  342. int blen, int sec)
  343. {
  344. struct scatterlist sg[1];
  345. unsigned long start, end;
  346. int bcount;
  347. int ret;
  348. sg_set_buf(sg, p, blen);
  349. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  350. time_before(jiffies, end); bcount++) {
  351. if (enc)
  352. ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
  353. else
  354. ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
  355. if (ret)
  356. return ret;
  357. }
  358. printk("%d operations in %d seconds (%ld bytes)\n",
  359. bcount, sec, (long)bcount * blen);
  360. return 0;
  361. }
  362. static int test_cipher_cycles(struct blkcipher_desc *desc, int enc, char *p,
  363. int blen)
  364. {
  365. struct scatterlist sg[1];
  366. unsigned long cycles = 0;
  367. int ret = 0;
  368. int i;
  369. sg_set_buf(sg, p, blen);
  370. local_bh_disable();
  371. local_irq_disable();
  372. /* Warm-up run. */
  373. for (i = 0; i < 4; i++) {
  374. if (enc)
  375. ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
  376. else
  377. ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
  378. if (ret)
  379. goto out;
  380. }
  381. /* The real thing. */
  382. for (i = 0; i < 8; i++) {
  383. cycles_t start, end;
  384. start = get_cycles();
  385. if (enc)
  386. ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
  387. else
  388. ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
  389. end = get_cycles();
  390. if (ret)
  391. goto out;
  392. cycles += end - start;
  393. }
  394. out:
  395. local_irq_enable();
  396. local_bh_enable();
  397. if (ret == 0)
  398. printk("1 operation in %lu cycles (%d bytes)\n",
  399. (cycles + 4) / 8, blen);
  400. return ret;
  401. }
  402. static void test_cipher_speed(char *algo, int enc, unsigned int sec,
  403. struct cipher_testvec *template,
  404. unsigned int tcount, struct cipher_speed *speed)
  405. {
  406. unsigned int ret, i, j, iv_len;
  407. unsigned char *key, *p, iv[128];
  408. struct crypto_blkcipher *tfm;
  409. struct blkcipher_desc desc;
  410. const char *e;
  411. if (enc == ENCRYPT)
  412. e = "encryption";
  413. else
  414. e = "decryption";
  415. printk("\ntesting speed of %s %s\n", algo, e);
  416. tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
  417. if (IS_ERR(tfm)) {
  418. printk("failed to load transform for %s: %ld\n", algo,
  419. PTR_ERR(tfm));
  420. return;
  421. }
  422. desc.tfm = tfm;
  423. desc.flags = 0;
  424. for (i = 0; speed[i].klen != 0; i++) {
  425. if ((speed[i].blen + speed[i].klen) > TVMEMSIZE) {
  426. printk("template (%u) too big for tvmem (%u)\n",
  427. speed[i].blen + speed[i].klen, TVMEMSIZE);
  428. goto out;
  429. }
  430. printk("test %u (%d bit key, %d byte blocks): ", i,
  431. speed[i].klen * 8, speed[i].blen);
  432. memset(tvmem, 0xff, speed[i].klen + speed[i].blen);
  433. /* set key, plain text and IV */
  434. key = (unsigned char *)tvmem;
  435. for (j = 0; j < tcount; j++) {
  436. if (template[j].klen == speed[i].klen) {
  437. key = template[j].key;
  438. break;
  439. }
  440. }
  441. p = (unsigned char *)tvmem + speed[i].klen;
  442. ret = crypto_blkcipher_setkey(tfm, key, speed[i].klen);
  443. if (ret) {
  444. printk("setkey() failed flags=%x\n",
  445. crypto_blkcipher_get_flags(tfm));
  446. goto out;
  447. }
  448. iv_len = crypto_blkcipher_ivsize(tfm);
  449. if (iv_len) {
  450. memset(&iv, 0xff, iv_len);
  451. crypto_blkcipher_set_iv(tfm, iv, iv_len);
  452. }
  453. if (sec)
  454. ret = test_cipher_jiffies(&desc, enc, p, speed[i].blen,
  455. sec);
  456. else
  457. ret = test_cipher_cycles(&desc, enc, p, speed[i].blen);
  458. if (ret) {
  459. printk("%s() failed flags=%x\n", e, desc.flags);
  460. break;
  461. }
  462. }
  463. out:
  464. crypto_free_blkcipher(tfm);
  465. }
  466. static int test_hash_jiffies_digest(struct hash_desc *desc, char *p, int blen,
  467. char *out, int sec)
  468. {
  469. struct scatterlist sg[1];
  470. unsigned long start, end;
  471. int bcount;
  472. int ret;
  473. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  474. time_before(jiffies, end); bcount++) {
  475. sg_set_buf(sg, p, blen);
  476. ret = crypto_hash_digest(desc, sg, blen, out);
  477. if (ret)
  478. return ret;
  479. }
  480. printk("%6u opers/sec, %9lu bytes/sec\n",
  481. bcount / sec, ((long)bcount * blen) / sec);
  482. return 0;
  483. }
  484. static int test_hash_jiffies(struct hash_desc *desc, char *p, int blen,
  485. int plen, char *out, int sec)
  486. {
  487. struct scatterlist sg[1];
  488. unsigned long start, end;
  489. int bcount, pcount;
  490. int ret;
  491. if (plen == blen)
  492. return test_hash_jiffies_digest(desc, p, blen, out, sec);
  493. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  494. time_before(jiffies, end); bcount++) {
  495. ret = crypto_hash_init(desc);
  496. if (ret)
  497. return ret;
  498. for (pcount = 0; pcount < blen; pcount += plen) {
  499. sg_set_buf(sg, p + pcount, plen);
  500. ret = crypto_hash_update(desc, sg, plen);
  501. if (ret)
  502. return ret;
  503. }
  504. /* we assume there is enough space in 'out' for the result */
  505. ret = crypto_hash_final(desc, out);
  506. if (ret)
  507. return ret;
  508. }
  509. printk("%6u opers/sec, %9lu bytes/sec\n",
  510. bcount / sec, ((long)bcount * blen) / sec);
  511. return 0;
  512. }
  513. static int test_hash_cycles_digest(struct hash_desc *desc, char *p, int blen,
  514. char *out)
  515. {
  516. struct scatterlist sg[1];
  517. unsigned long cycles = 0;
  518. int i;
  519. int ret;
  520. local_bh_disable();
  521. local_irq_disable();
  522. /* Warm-up run. */
  523. for (i = 0; i < 4; i++) {
  524. sg_set_buf(sg, p, blen);
  525. ret = crypto_hash_digest(desc, sg, blen, out);
  526. if (ret)
  527. goto out;
  528. }
  529. /* The real thing. */
  530. for (i = 0; i < 8; i++) {
  531. cycles_t start, end;
  532. start = get_cycles();
  533. sg_set_buf(sg, p, blen);
  534. ret = crypto_hash_digest(desc, sg, blen, out);
  535. if (ret)
  536. goto out;
  537. end = get_cycles();
  538. cycles += end - start;
  539. }
  540. out:
  541. local_irq_enable();
  542. local_bh_enable();
  543. if (ret)
  544. return ret;
  545. printk("%6lu cycles/operation, %4lu cycles/byte\n",
  546. cycles / 8, cycles / (8 * blen));
  547. return 0;
  548. }
  549. static int test_hash_cycles(struct hash_desc *desc, char *p, int blen,
  550. int plen, char *out)
  551. {
  552. struct scatterlist sg[1];
  553. unsigned long cycles = 0;
  554. int i, pcount;
  555. int ret;
  556. if (plen == blen)
  557. return test_hash_cycles_digest(desc, p, blen, out);
  558. local_bh_disable();
  559. local_irq_disable();
  560. /* Warm-up run. */
  561. for (i = 0; i < 4; i++) {
  562. ret = crypto_hash_init(desc);
  563. if (ret)
  564. goto out;
  565. for (pcount = 0; pcount < blen; pcount += plen) {
  566. sg_set_buf(sg, p + pcount, plen);
  567. ret = crypto_hash_update(desc, sg, plen);
  568. if (ret)
  569. goto out;
  570. }
  571. ret = crypto_hash_final(desc, out);
  572. if (ret)
  573. goto out;
  574. }
  575. /* The real thing. */
  576. for (i = 0; i < 8; i++) {
  577. cycles_t start, end;
  578. start = get_cycles();
  579. ret = crypto_hash_init(desc);
  580. if (ret)
  581. goto out;
  582. for (pcount = 0; pcount < blen; pcount += plen) {
  583. sg_set_buf(sg, p + pcount, plen);
  584. ret = crypto_hash_update(desc, sg, plen);
  585. if (ret)
  586. goto out;
  587. }
  588. ret = crypto_hash_final(desc, out);
  589. if (ret)
  590. goto out;
  591. end = get_cycles();
  592. cycles += end - start;
  593. }
  594. out:
  595. local_irq_enable();
  596. local_bh_enable();
  597. if (ret)
  598. return ret;
  599. printk("%6lu cycles/operation, %4lu cycles/byte\n",
  600. cycles / 8, cycles / (8 * blen));
  601. return 0;
  602. }
  603. static void test_hash_speed(char *algo, unsigned int sec,
  604. struct hash_speed *speed)
  605. {
  606. struct crypto_hash *tfm;
  607. struct hash_desc desc;
  608. char output[1024];
  609. int i;
  610. int ret;
  611. printk("\ntesting speed of %s\n", algo);
  612. tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
  613. if (IS_ERR(tfm)) {
  614. printk("failed to load transform for %s: %ld\n", algo,
  615. PTR_ERR(tfm));
  616. return;
  617. }
  618. desc.tfm = tfm;
  619. desc.flags = 0;
  620. if (crypto_hash_digestsize(tfm) > sizeof(output)) {
  621. printk("digestsize(%u) > outputbuffer(%zu)\n",
  622. crypto_hash_digestsize(tfm), sizeof(output));
  623. goto out;
  624. }
  625. for (i = 0; speed[i].blen != 0; i++) {
  626. if (speed[i].blen > TVMEMSIZE) {
  627. printk("template (%u) too big for tvmem (%u)\n",
  628. speed[i].blen, TVMEMSIZE);
  629. goto out;
  630. }
  631. printk("test%3u (%5u byte blocks,%5u bytes per update,%4u updates): ",
  632. i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
  633. memset(tvmem, 0xff, speed[i].blen);
  634. if (sec)
  635. ret = test_hash_jiffies(&desc, tvmem, speed[i].blen,
  636. speed[i].plen, output, sec);
  637. else
  638. ret = test_hash_cycles(&desc, tvmem, speed[i].blen,
  639. speed[i].plen, output);
  640. if (ret) {
  641. printk("hashing failed ret=%d\n", ret);
  642. break;
  643. }
  644. }
  645. out:
  646. crypto_free_hash(tfm);
  647. }
  648. static void test_deflate(void)
  649. {
  650. unsigned int i;
  651. char result[COMP_BUF_SIZE];
  652. struct crypto_comp *tfm;
  653. struct comp_testvec *tv;
  654. unsigned int tsize;
  655. printk("\ntesting deflate compression\n");
  656. tsize = sizeof (deflate_comp_tv_template);
  657. if (tsize > TVMEMSIZE) {
  658. printk("template (%u) too big for tvmem (%u)\n", tsize,
  659. TVMEMSIZE);
  660. return;
  661. }
  662. memcpy(tvmem, deflate_comp_tv_template, tsize);
  663. tv = (void *)tvmem;
  664. tfm = crypto_alloc_comp("deflate", 0, CRYPTO_ALG_ASYNC);
  665. if (IS_ERR(tfm)) {
  666. printk("failed to load transform for deflate\n");
  667. return;
  668. }
  669. for (i = 0; i < DEFLATE_COMP_TEST_VECTORS; i++) {
  670. int ilen, ret, dlen = COMP_BUF_SIZE;
  671. printk("test %u:\n", i + 1);
  672. memset(result, 0, sizeof (result));
  673. ilen = tv[i].inlen;
  674. ret = crypto_comp_compress(tfm, tv[i].input,
  675. ilen, result, &dlen);
  676. if (ret) {
  677. printk("fail: ret=%d\n", ret);
  678. continue;
  679. }
  680. hexdump(result, dlen);
  681. printk("%s (ratio %d:%d)\n",
  682. memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
  683. ilen, dlen);
  684. }
  685. printk("\ntesting deflate decompression\n");
  686. tsize = sizeof (deflate_decomp_tv_template);
  687. if (tsize > TVMEMSIZE) {
  688. printk("template (%u) too big for tvmem (%u)\n", tsize,
  689. TVMEMSIZE);
  690. goto out;
  691. }
  692. memcpy(tvmem, deflate_decomp_tv_template, tsize);
  693. tv = (void *)tvmem;
  694. for (i = 0; i < DEFLATE_DECOMP_TEST_VECTORS; i++) {
  695. int ilen, ret, dlen = COMP_BUF_SIZE;
  696. printk("test %u:\n", i + 1);
  697. memset(result, 0, sizeof (result));
  698. ilen = tv[i].inlen;
  699. ret = crypto_comp_decompress(tfm, tv[i].input,
  700. ilen, result, &dlen);
  701. if (ret) {
  702. printk("fail: ret=%d\n", ret);
  703. continue;
  704. }
  705. hexdump(result, dlen);
  706. printk("%s (ratio %d:%d)\n",
  707. memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
  708. ilen, dlen);
  709. }
  710. out:
  711. crypto_free_comp(tfm);
  712. }
  713. static void test_available(void)
  714. {
  715. char **name = check;
  716. while (*name) {
  717. printk("alg %s ", *name);
  718. printk(crypto_has_alg(*name, 0, 0) ?
  719. "found\n" : "not found\n");
  720. name++;
  721. }
  722. }
  723. static void do_test(void)
  724. {
  725. switch (mode) {
  726. case 0:
  727. test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
  728. test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
  729. //DES
  730. test_cipher("ecb(des)", ENCRYPT, des_enc_tv_template,
  731. DES_ENC_TEST_VECTORS);
  732. test_cipher("ecb(des)", DECRYPT, des_dec_tv_template,
  733. DES_DEC_TEST_VECTORS);
  734. test_cipher("cbc(des)", ENCRYPT, des_cbc_enc_tv_template,
  735. DES_CBC_ENC_TEST_VECTORS);
  736. test_cipher("cbc(des)", DECRYPT, des_cbc_dec_tv_template,
  737. DES_CBC_DEC_TEST_VECTORS);
  738. //DES3_EDE
  739. test_cipher("ecb(des3_ede)", ENCRYPT, des3_ede_enc_tv_template,
  740. DES3_EDE_ENC_TEST_VECTORS);
  741. test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
  742. DES3_EDE_DEC_TEST_VECTORS);
  743. test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
  744. test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
  745. //BLOWFISH
  746. test_cipher("ecb(blowfish)", ENCRYPT, bf_enc_tv_template,
  747. BF_ENC_TEST_VECTORS);
  748. test_cipher("ecb(blowfish)", DECRYPT, bf_dec_tv_template,
  749. BF_DEC_TEST_VECTORS);
  750. test_cipher("cbc(blowfish)", ENCRYPT, bf_cbc_enc_tv_template,
  751. BF_CBC_ENC_TEST_VECTORS);
  752. test_cipher("cbc(blowfish)", DECRYPT, bf_cbc_dec_tv_template,
  753. BF_CBC_DEC_TEST_VECTORS);
  754. //TWOFISH
  755. test_cipher("ecb(twofish)", ENCRYPT, tf_enc_tv_template,
  756. TF_ENC_TEST_VECTORS);
  757. test_cipher("ecb(twofish)", DECRYPT, tf_dec_tv_template,
  758. TF_DEC_TEST_VECTORS);
  759. test_cipher("cbc(twofish)", ENCRYPT, tf_cbc_enc_tv_template,
  760. TF_CBC_ENC_TEST_VECTORS);
  761. test_cipher("cbc(twofish)", DECRYPT, tf_cbc_dec_tv_template,
  762. TF_CBC_DEC_TEST_VECTORS);
  763. //SERPENT
  764. test_cipher("ecb(serpent)", ENCRYPT, serpent_enc_tv_template,
  765. SERPENT_ENC_TEST_VECTORS);
  766. test_cipher("ecb(serpent)", DECRYPT, serpent_dec_tv_template,
  767. SERPENT_DEC_TEST_VECTORS);
  768. //TNEPRES
  769. test_cipher("ecb(tnepres)", ENCRYPT, tnepres_enc_tv_template,
  770. TNEPRES_ENC_TEST_VECTORS);
  771. test_cipher("ecb(tnepres)", DECRYPT, tnepres_dec_tv_template,
  772. TNEPRES_DEC_TEST_VECTORS);
  773. //AES
  774. test_cipher("ecb(aes)", ENCRYPT, aes_enc_tv_template,
  775. AES_ENC_TEST_VECTORS);
  776. test_cipher("ecb(aes)", DECRYPT, aes_dec_tv_template,
  777. AES_DEC_TEST_VECTORS);
  778. test_cipher("cbc(aes)", ENCRYPT, aes_cbc_enc_tv_template,
  779. AES_CBC_ENC_TEST_VECTORS);
  780. test_cipher("cbc(aes)", DECRYPT, aes_cbc_dec_tv_template,
  781. AES_CBC_DEC_TEST_VECTORS);
  782. test_cipher("lrw(aes)", ENCRYPT, aes_lrw_enc_tv_template,
  783. AES_LRW_ENC_TEST_VECTORS);
  784. test_cipher("lrw(aes)", DECRYPT, aes_lrw_dec_tv_template,
  785. AES_LRW_DEC_TEST_VECTORS);
  786. //CAST5
  787. test_cipher("ecb(cast5)", ENCRYPT, cast5_enc_tv_template,
  788. CAST5_ENC_TEST_VECTORS);
  789. test_cipher("ecb(cast5)", DECRYPT, cast5_dec_tv_template,
  790. CAST5_DEC_TEST_VECTORS);
  791. //CAST6
  792. test_cipher("ecb(cast6)", ENCRYPT, cast6_enc_tv_template,
  793. CAST6_ENC_TEST_VECTORS);
  794. test_cipher("ecb(cast6)", DECRYPT, cast6_dec_tv_template,
  795. CAST6_DEC_TEST_VECTORS);
  796. //ARC4
  797. test_cipher("ecb(arc4)", ENCRYPT, arc4_enc_tv_template,
  798. ARC4_ENC_TEST_VECTORS);
  799. test_cipher("ecb(arc4)", DECRYPT, arc4_dec_tv_template,
  800. ARC4_DEC_TEST_VECTORS);
  801. //TEA
  802. test_cipher("ecb(tea)", ENCRYPT, tea_enc_tv_template,
  803. TEA_ENC_TEST_VECTORS);
  804. test_cipher("ecb(tea)", DECRYPT, tea_dec_tv_template,
  805. TEA_DEC_TEST_VECTORS);
  806. //XTEA
  807. test_cipher("ecb(xtea)", ENCRYPT, xtea_enc_tv_template,
  808. XTEA_ENC_TEST_VECTORS);
  809. test_cipher("ecb(xtea)", DECRYPT, xtea_dec_tv_template,
  810. XTEA_DEC_TEST_VECTORS);
  811. //KHAZAD
  812. test_cipher("ecb(khazad)", ENCRYPT, khazad_enc_tv_template,
  813. KHAZAD_ENC_TEST_VECTORS);
  814. test_cipher("ecb(khazad)", DECRYPT, khazad_dec_tv_template,
  815. KHAZAD_DEC_TEST_VECTORS);
  816. //ANUBIS
  817. test_cipher("ecb(anubis)", ENCRYPT, anubis_enc_tv_template,
  818. ANUBIS_ENC_TEST_VECTORS);
  819. test_cipher("ecb(anubis)", DECRYPT, anubis_dec_tv_template,
  820. ANUBIS_DEC_TEST_VECTORS);
  821. test_cipher("cbc(anubis)", ENCRYPT, anubis_cbc_enc_tv_template,
  822. ANUBIS_CBC_ENC_TEST_VECTORS);
  823. test_cipher("cbc(anubis)", DECRYPT, anubis_cbc_dec_tv_template,
  824. ANUBIS_CBC_ENC_TEST_VECTORS);
  825. //XETA
  826. test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
  827. XETA_ENC_TEST_VECTORS);
  828. test_cipher("ecb(xeta)", DECRYPT, xeta_dec_tv_template,
  829. XETA_DEC_TEST_VECTORS);
  830. //FCrypt
  831. test_cipher("pcbc(fcrypt)", ENCRYPT, fcrypt_pcbc_enc_tv_template,
  832. FCRYPT_ENC_TEST_VECTORS);
  833. test_cipher("pcbc(fcrypt)", DECRYPT, fcrypt_pcbc_dec_tv_template,
  834. FCRYPT_DEC_TEST_VECTORS);
  835. //CAMELLIA
  836. test_cipher("ecb(camellia)", ENCRYPT,
  837. camellia_enc_tv_template,
  838. CAMELLIA_ENC_TEST_VECTORS);
  839. test_cipher("ecb(camellia)", DECRYPT,
  840. camellia_dec_tv_template,
  841. CAMELLIA_DEC_TEST_VECTORS);
  842. test_cipher("cbc(camellia)", ENCRYPT,
  843. camellia_cbc_enc_tv_template,
  844. CAMELLIA_CBC_ENC_TEST_VECTORS);
  845. test_cipher("cbc(camellia)", DECRYPT,
  846. camellia_cbc_dec_tv_template,
  847. CAMELLIA_CBC_DEC_TEST_VECTORS);
  848. test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
  849. test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
  850. test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
  851. test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
  852. test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
  853. test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
  854. test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
  855. test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
  856. test_deflate();
  857. test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
  858. test_hash("hmac(md5)", hmac_md5_tv_template,
  859. HMAC_MD5_TEST_VECTORS);
  860. test_hash("hmac(sha1)", hmac_sha1_tv_template,
  861. HMAC_SHA1_TEST_VECTORS);
  862. test_hash("hmac(sha256)", hmac_sha256_tv_template,
  863. HMAC_SHA256_TEST_VECTORS);
  864. test_hash("hmac(sha384)", hmac_sha384_tv_template,
  865. HMAC_SHA384_TEST_VECTORS);
  866. test_hash("hmac(sha512)", hmac_sha512_tv_template,
  867. HMAC_SHA512_TEST_VECTORS);
  868. test_hash("xcbc(aes)", aes_xcbc128_tv_template,
  869. XCBC_AES_TEST_VECTORS);
  870. test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
  871. break;
  872. case 1:
  873. test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
  874. break;
  875. case 2:
  876. test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
  877. break;
  878. case 3:
  879. test_cipher("ecb(des)", ENCRYPT, des_enc_tv_template,
  880. DES_ENC_TEST_VECTORS);
  881. test_cipher("ecb(des)", DECRYPT, des_dec_tv_template,
  882. DES_DEC_TEST_VECTORS);
  883. test_cipher("cbc(des)", ENCRYPT, des_cbc_enc_tv_template,
  884. DES_CBC_ENC_TEST_VECTORS);
  885. test_cipher("cbc(des)", DECRYPT, des_cbc_dec_tv_template,
  886. DES_CBC_DEC_TEST_VECTORS);
  887. break;
  888. case 4:
  889. test_cipher("ecb(des3_ede)", ENCRYPT, des3_ede_enc_tv_template,
  890. DES3_EDE_ENC_TEST_VECTORS);
  891. test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
  892. DES3_EDE_DEC_TEST_VECTORS);
  893. break;
  894. case 5:
  895. test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
  896. break;
  897. case 6:
  898. test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
  899. break;
  900. case 7:
  901. test_cipher("ecb(blowfish)", ENCRYPT, bf_enc_tv_template,
  902. BF_ENC_TEST_VECTORS);
  903. test_cipher("ecb(blowfish)", DECRYPT, bf_dec_tv_template,
  904. BF_DEC_TEST_VECTORS);
  905. test_cipher("cbc(blowfish)", ENCRYPT, bf_cbc_enc_tv_template,
  906. BF_CBC_ENC_TEST_VECTORS);
  907. test_cipher("cbc(blowfish)", DECRYPT, bf_cbc_dec_tv_template,
  908. BF_CBC_DEC_TEST_VECTORS);
  909. break;
  910. case 8:
  911. test_cipher("ecb(twofish)", ENCRYPT, tf_enc_tv_template,
  912. TF_ENC_TEST_VECTORS);
  913. test_cipher("ecb(twofish)", DECRYPT, tf_dec_tv_template,
  914. TF_DEC_TEST_VECTORS);
  915. test_cipher("cbc(twofish)", ENCRYPT, tf_cbc_enc_tv_template,
  916. TF_CBC_ENC_TEST_VECTORS);
  917. test_cipher("cbc(twofish)", DECRYPT, tf_cbc_dec_tv_template,
  918. TF_CBC_DEC_TEST_VECTORS);
  919. break;
  920. case 9:
  921. test_cipher("ecb(serpent)", ENCRYPT, serpent_enc_tv_template,
  922. SERPENT_ENC_TEST_VECTORS);
  923. test_cipher("ecb(serpent)", DECRYPT, serpent_dec_tv_template,
  924. SERPENT_DEC_TEST_VECTORS);
  925. break;
  926. case 10:
  927. test_cipher("ecb(aes)", ENCRYPT, aes_enc_tv_template,
  928. AES_ENC_TEST_VECTORS);
  929. test_cipher("ecb(aes)", DECRYPT, aes_dec_tv_template,
  930. AES_DEC_TEST_VECTORS);
  931. test_cipher("cbc(aes)", ENCRYPT, aes_cbc_enc_tv_template,
  932. AES_CBC_ENC_TEST_VECTORS);
  933. test_cipher("cbc(aes)", DECRYPT, aes_cbc_dec_tv_template,
  934. AES_CBC_DEC_TEST_VECTORS);
  935. test_cipher("lrw(aes)", ENCRYPT, aes_lrw_enc_tv_template,
  936. AES_LRW_ENC_TEST_VECTORS);
  937. test_cipher("lrw(aes)", DECRYPT, aes_lrw_dec_tv_template,
  938. AES_LRW_DEC_TEST_VECTORS);
  939. break;
  940. case 11:
  941. test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
  942. break;
  943. case 12:
  944. test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
  945. break;
  946. case 13:
  947. test_deflate();
  948. break;
  949. case 14:
  950. test_cipher("ecb(cast5)", ENCRYPT, cast5_enc_tv_template,
  951. CAST5_ENC_TEST_VECTORS);
  952. test_cipher("ecb(cast5)", DECRYPT, cast5_dec_tv_template,
  953. CAST5_DEC_TEST_VECTORS);
  954. break;
  955. case 15:
  956. test_cipher("ecb(cast6)", ENCRYPT, cast6_enc_tv_template,
  957. CAST6_ENC_TEST_VECTORS);
  958. test_cipher("ecb(cast6)", DECRYPT, cast6_dec_tv_template,
  959. CAST6_DEC_TEST_VECTORS);
  960. break;
  961. case 16:
  962. test_cipher("ecb(arc4)", ENCRYPT, arc4_enc_tv_template,
  963. ARC4_ENC_TEST_VECTORS);
  964. test_cipher("ecb(arc4)", DECRYPT, arc4_dec_tv_template,
  965. ARC4_DEC_TEST_VECTORS);
  966. break;
  967. case 17:
  968. test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
  969. break;
  970. case 18:
  971. test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
  972. break;
  973. case 19:
  974. test_cipher("ecb(tea)", ENCRYPT, tea_enc_tv_template,
  975. TEA_ENC_TEST_VECTORS);
  976. test_cipher("ecb(tea)", DECRYPT, tea_dec_tv_template,
  977. TEA_DEC_TEST_VECTORS);
  978. break;
  979. case 20:
  980. test_cipher("ecb(xtea)", ENCRYPT, xtea_enc_tv_template,
  981. XTEA_ENC_TEST_VECTORS);
  982. test_cipher("ecb(xtea)", DECRYPT, xtea_dec_tv_template,
  983. XTEA_DEC_TEST_VECTORS);
  984. break;
  985. case 21:
  986. test_cipher("ecb(khazad)", ENCRYPT, khazad_enc_tv_template,
  987. KHAZAD_ENC_TEST_VECTORS);
  988. test_cipher("ecb(khazad)", DECRYPT, khazad_dec_tv_template,
  989. KHAZAD_DEC_TEST_VECTORS);
  990. break;
  991. case 22:
  992. test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
  993. break;
  994. case 23:
  995. test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
  996. break;
  997. case 24:
  998. test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
  999. break;
  1000. case 25:
  1001. test_cipher("ecb(tnepres)", ENCRYPT, tnepres_enc_tv_template,
  1002. TNEPRES_ENC_TEST_VECTORS);
  1003. test_cipher("ecb(tnepres)", DECRYPT, tnepres_dec_tv_template,
  1004. TNEPRES_DEC_TEST_VECTORS);
  1005. break;
  1006. case 26:
  1007. test_cipher("ecb(anubis)", ENCRYPT, anubis_enc_tv_template,
  1008. ANUBIS_ENC_TEST_VECTORS);
  1009. test_cipher("ecb(anubis)", DECRYPT, anubis_dec_tv_template,
  1010. ANUBIS_DEC_TEST_VECTORS);
  1011. test_cipher("cbc(anubis)", ENCRYPT, anubis_cbc_enc_tv_template,
  1012. ANUBIS_CBC_ENC_TEST_VECTORS);
  1013. test_cipher("cbc(anubis)", DECRYPT, anubis_cbc_dec_tv_template,
  1014. ANUBIS_CBC_ENC_TEST_VECTORS);
  1015. break;
  1016. case 27:
  1017. test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
  1018. break;
  1019. case 28:
  1020. test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
  1021. break;
  1022. case 29:
  1023. test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
  1024. break;
  1025. case 30:
  1026. test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
  1027. XETA_ENC_TEST_VECTORS);
  1028. test_cipher("ecb(xeta)", DECRYPT, xeta_dec_tv_template,
  1029. XETA_DEC_TEST_VECTORS);
  1030. break;
  1031. case 31:
  1032. test_cipher("pcbc(fcrypt)", ENCRYPT, fcrypt_pcbc_enc_tv_template,
  1033. FCRYPT_ENC_TEST_VECTORS);
  1034. test_cipher("pcbc(fcrypt)", DECRYPT, fcrypt_pcbc_dec_tv_template,
  1035. FCRYPT_DEC_TEST_VECTORS);
  1036. break;
  1037. case 32:
  1038. test_cipher("ecb(camellia)", ENCRYPT,
  1039. camellia_enc_tv_template,
  1040. CAMELLIA_ENC_TEST_VECTORS);
  1041. test_cipher("ecb(camellia)", DECRYPT,
  1042. camellia_dec_tv_template,
  1043. CAMELLIA_DEC_TEST_VECTORS);
  1044. test_cipher("cbc(camellia)", ENCRYPT,
  1045. camellia_cbc_enc_tv_template,
  1046. CAMELLIA_CBC_ENC_TEST_VECTORS);
  1047. test_cipher("cbc(camellia)", DECRYPT,
  1048. camellia_cbc_dec_tv_template,
  1049. CAMELLIA_CBC_DEC_TEST_VECTORS);
  1050. break;
  1051. case 100:
  1052. test_hash("hmac(md5)", hmac_md5_tv_template,
  1053. HMAC_MD5_TEST_VECTORS);
  1054. break;
  1055. case 101:
  1056. test_hash("hmac(sha1)", hmac_sha1_tv_template,
  1057. HMAC_SHA1_TEST_VECTORS);
  1058. break;
  1059. case 102:
  1060. test_hash("hmac(sha256)", hmac_sha256_tv_template,
  1061. HMAC_SHA256_TEST_VECTORS);
  1062. break;
  1063. case 103:
  1064. test_hash("hmac(sha384)", hmac_sha384_tv_template,
  1065. HMAC_SHA384_TEST_VECTORS);
  1066. break;
  1067. case 104:
  1068. test_hash("hmac(sha512)", hmac_sha512_tv_template,
  1069. HMAC_SHA512_TEST_VECTORS);
  1070. break;
  1071. case 200:
  1072. test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
  1073. aes_speed_template);
  1074. test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
  1075. aes_speed_template);
  1076. test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
  1077. aes_speed_template);
  1078. test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
  1079. aes_speed_template);
  1080. test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
  1081. aes_lrw_speed_template);
  1082. test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
  1083. aes_lrw_speed_template);
  1084. break;
  1085. case 201:
  1086. test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
  1087. des3_ede_enc_tv_template,
  1088. DES3_EDE_ENC_TEST_VECTORS,
  1089. des3_ede_speed_template);
  1090. test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
  1091. des3_ede_dec_tv_template,
  1092. DES3_EDE_DEC_TEST_VECTORS,
  1093. des3_ede_speed_template);
  1094. test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
  1095. des3_ede_enc_tv_template,
  1096. DES3_EDE_ENC_TEST_VECTORS,
  1097. des3_ede_speed_template);
  1098. test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
  1099. des3_ede_dec_tv_template,
  1100. DES3_EDE_DEC_TEST_VECTORS,
  1101. des3_ede_speed_template);
  1102. break;
  1103. case 202:
  1104. test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
  1105. twofish_speed_template);
  1106. test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
  1107. twofish_speed_template);
  1108. test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
  1109. twofish_speed_template);
  1110. test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
  1111. twofish_speed_template);
  1112. break;
  1113. case 203:
  1114. test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
  1115. blowfish_speed_template);
  1116. test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
  1117. blowfish_speed_template);
  1118. test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
  1119. blowfish_speed_template);
  1120. test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
  1121. blowfish_speed_template);
  1122. break;
  1123. case 204:
  1124. test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
  1125. des_speed_template);
  1126. test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
  1127. des_speed_template);
  1128. test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
  1129. des_speed_template);
  1130. test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
  1131. des_speed_template);
  1132. break;
  1133. case 205:
  1134. test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
  1135. camellia_speed_template);
  1136. test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
  1137. camellia_speed_template);
  1138. test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
  1139. camellia_speed_template);
  1140. test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
  1141. camellia_speed_template);
  1142. break;
  1143. case 300:
  1144. /* fall through */
  1145. case 301:
  1146. test_hash_speed("md4", sec, generic_hash_speed_template);
  1147. if (mode > 300 && mode < 400) break;
  1148. case 302:
  1149. test_hash_speed("md5", sec, generic_hash_speed_template);
  1150. if (mode > 300 && mode < 400) break;
  1151. case 303:
  1152. test_hash_speed("sha1", sec, generic_hash_speed_template);
  1153. if (mode > 300 && mode < 400) break;
  1154. case 304:
  1155. test_hash_speed("sha256", sec, generic_hash_speed_template);
  1156. if (mode > 300 && mode < 400) break;
  1157. case 305:
  1158. test_hash_speed("sha384", sec, generic_hash_speed_template);
  1159. if (mode > 300 && mode < 400) break;
  1160. case 306:
  1161. test_hash_speed("sha512", sec, generic_hash_speed_template);
  1162. if (mode > 300 && mode < 400) break;
  1163. case 307:
  1164. test_hash_speed("wp256", sec, generic_hash_speed_template);
  1165. if (mode > 300 && mode < 400) break;
  1166. case 308:
  1167. test_hash_speed("wp384", sec, generic_hash_speed_template);
  1168. if (mode > 300 && mode < 400) break;
  1169. case 309:
  1170. test_hash_speed("wp512", sec, generic_hash_speed_template);
  1171. if (mode > 300 && mode < 400) break;
  1172. case 310:
  1173. test_hash_speed("tgr128", sec, generic_hash_speed_template);
  1174. if (mode > 300 && mode < 400) break;
  1175. case 311:
  1176. test_hash_speed("tgr160", sec, generic_hash_speed_template);
  1177. if (mode > 300 && mode < 400) break;
  1178. case 312:
  1179. test_hash_speed("tgr192", sec, generic_hash_speed_template);
  1180. if (mode > 300 && mode < 400) break;
  1181. case 399:
  1182. break;
  1183. case 1000:
  1184. test_available();
  1185. break;
  1186. default:
  1187. /* useful for debugging */
  1188. printk("not testing anything\n");
  1189. break;
  1190. }
  1191. }
  1192. static int __init init(void)
  1193. {
  1194. tvmem = kmalloc(TVMEMSIZE, GFP_KERNEL);
  1195. if (tvmem == NULL)
  1196. return -ENOMEM;
  1197. xbuf = kmalloc(XBUFSIZE, GFP_KERNEL);
  1198. if (xbuf == NULL) {
  1199. kfree(tvmem);
  1200. return -ENOMEM;
  1201. }
  1202. do_test();
  1203. kfree(xbuf);
  1204. kfree(tvmem);
  1205. /* We intentionaly return -EAGAIN to prevent keeping
  1206. * the module. It does all its work from init()
  1207. * and doesn't offer any runtime functionality
  1208. * => we don't need it in the memory, do we?
  1209. * -- mludvig
  1210. */
  1211. return -EAGAIN;
  1212. }
  1213. /*
  1214. * If an init function is provided, an exit function must also be provided
  1215. * to allow module unload.
  1216. */
  1217. static void __exit fini(void) { }
  1218. module_init(init);
  1219. module_exit(fini);
  1220. module_param(mode, int, 0);
  1221. module_param(sec, uint, 0);
  1222. MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
  1223. "(defaults to zero which uses CPU cycles instead)");
  1224. MODULE_LICENSE("GPL");
  1225. MODULE_DESCRIPTION("Quick & dirty crypto testing module");
  1226. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");