tcrypt.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  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", "seed", 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. test_cipher("xts(aes)", ENCRYPT, aes_xts_enc_tv_template,
  787. AES_XTS_ENC_TEST_VECTORS);
  788. test_cipher("xts(aes)", DECRYPT, aes_xts_dec_tv_template,
  789. AES_XTS_DEC_TEST_VECTORS);
  790. //CAST5
  791. test_cipher("ecb(cast5)", ENCRYPT, cast5_enc_tv_template,
  792. CAST5_ENC_TEST_VECTORS);
  793. test_cipher("ecb(cast5)", DECRYPT, cast5_dec_tv_template,
  794. CAST5_DEC_TEST_VECTORS);
  795. //CAST6
  796. test_cipher("ecb(cast6)", ENCRYPT, cast6_enc_tv_template,
  797. CAST6_ENC_TEST_VECTORS);
  798. test_cipher("ecb(cast6)", DECRYPT, cast6_dec_tv_template,
  799. CAST6_DEC_TEST_VECTORS);
  800. //ARC4
  801. test_cipher("ecb(arc4)", ENCRYPT, arc4_enc_tv_template,
  802. ARC4_ENC_TEST_VECTORS);
  803. test_cipher("ecb(arc4)", DECRYPT, arc4_dec_tv_template,
  804. ARC4_DEC_TEST_VECTORS);
  805. //TEA
  806. test_cipher("ecb(tea)", ENCRYPT, tea_enc_tv_template,
  807. TEA_ENC_TEST_VECTORS);
  808. test_cipher("ecb(tea)", DECRYPT, tea_dec_tv_template,
  809. TEA_DEC_TEST_VECTORS);
  810. //XTEA
  811. test_cipher("ecb(xtea)", ENCRYPT, xtea_enc_tv_template,
  812. XTEA_ENC_TEST_VECTORS);
  813. test_cipher("ecb(xtea)", DECRYPT, xtea_dec_tv_template,
  814. XTEA_DEC_TEST_VECTORS);
  815. //KHAZAD
  816. test_cipher("ecb(khazad)", ENCRYPT, khazad_enc_tv_template,
  817. KHAZAD_ENC_TEST_VECTORS);
  818. test_cipher("ecb(khazad)", DECRYPT, khazad_dec_tv_template,
  819. KHAZAD_DEC_TEST_VECTORS);
  820. //ANUBIS
  821. test_cipher("ecb(anubis)", ENCRYPT, anubis_enc_tv_template,
  822. ANUBIS_ENC_TEST_VECTORS);
  823. test_cipher("ecb(anubis)", DECRYPT, anubis_dec_tv_template,
  824. ANUBIS_DEC_TEST_VECTORS);
  825. test_cipher("cbc(anubis)", ENCRYPT, anubis_cbc_enc_tv_template,
  826. ANUBIS_CBC_ENC_TEST_VECTORS);
  827. test_cipher("cbc(anubis)", DECRYPT, anubis_cbc_dec_tv_template,
  828. ANUBIS_CBC_ENC_TEST_VECTORS);
  829. //XETA
  830. test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
  831. XETA_ENC_TEST_VECTORS);
  832. test_cipher("ecb(xeta)", DECRYPT, xeta_dec_tv_template,
  833. XETA_DEC_TEST_VECTORS);
  834. //FCrypt
  835. test_cipher("pcbc(fcrypt)", ENCRYPT, fcrypt_pcbc_enc_tv_template,
  836. FCRYPT_ENC_TEST_VECTORS);
  837. test_cipher("pcbc(fcrypt)", DECRYPT, fcrypt_pcbc_dec_tv_template,
  838. FCRYPT_DEC_TEST_VECTORS);
  839. //CAMELLIA
  840. test_cipher("ecb(camellia)", ENCRYPT,
  841. camellia_enc_tv_template,
  842. CAMELLIA_ENC_TEST_VECTORS);
  843. test_cipher("ecb(camellia)", DECRYPT,
  844. camellia_dec_tv_template,
  845. CAMELLIA_DEC_TEST_VECTORS);
  846. test_cipher("cbc(camellia)", ENCRYPT,
  847. camellia_cbc_enc_tv_template,
  848. CAMELLIA_CBC_ENC_TEST_VECTORS);
  849. test_cipher("cbc(camellia)", DECRYPT,
  850. camellia_cbc_dec_tv_template,
  851. CAMELLIA_CBC_DEC_TEST_VECTORS);
  852. //SEED
  853. test_cipher("ecb(seed)", ENCRYPT, seed_enc_tv_template,
  854. SEED_ENC_TEST_VECTORS);
  855. test_cipher("ecb(seed)", DECRYPT, seed_dec_tv_template,
  856. SEED_DEC_TEST_VECTORS);
  857. test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
  858. test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
  859. test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
  860. test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
  861. test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
  862. test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
  863. test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
  864. test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
  865. test_deflate();
  866. test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
  867. test_hash("hmac(md5)", hmac_md5_tv_template,
  868. HMAC_MD5_TEST_VECTORS);
  869. test_hash("hmac(sha1)", hmac_sha1_tv_template,
  870. HMAC_SHA1_TEST_VECTORS);
  871. test_hash("hmac(sha256)", hmac_sha256_tv_template,
  872. HMAC_SHA256_TEST_VECTORS);
  873. test_hash("hmac(sha384)", hmac_sha384_tv_template,
  874. HMAC_SHA384_TEST_VECTORS);
  875. test_hash("hmac(sha512)", hmac_sha512_tv_template,
  876. HMAC_SHA512_TEST_VECTORS);
  877. test_hash("xcbc(aes)", aes_xcbc128_tv_template,
  878. XCBC_AES_TEST_VECTORS);
  879. test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
  880. break;
  881. case 1:
  882. test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
  883. break;
  884. case 2:
  885. test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
  886. break;
  887. case 3:
  888. test_cipher("ecb(des)", ENCRYPT, des_enc_tv_template,
  889. DES_ENC_TEST_VECTORS);
  890. test_cipher("ecb(des)", DECRYPT, des_dec_tv_template,
  891. DES_DEC_TEST_VECTORS);
  892. test_cipher("cbc(des)", ENCRYPT, des_cbc_enc_tv_template,
  893. DES_CBC_ENC_TEST_VECTORS);
  894. test_cipher("cbc(des)", DECRYPT, des_cbc_dec_tv_template,
  895. DES_CBC_DEC_TEST_VECTORS);
  896. break;
  897. case 4:
  898. test_cipher("ecb(des3_ede)", ENCRYPT, des3_ede_enc_tv_template,
  899. DES3_EDE_ENC_TEST_VECTORS);
  900. test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
  901. DES3_EDE_DEC_TEST_VECTORS);
  902. break;
  903. case 5:
  904. test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
  905. break;
  906. case 6:
  907. test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
  908. break;
  909. case 7:
  910. test_cipher("ecb(blowfish)", ENCRYPT, bf_enc_tv_template,
  911. BF_ENC_TEST_VECTORS);
  912. test_cipher("ecb(blowfish)", DECRYPT, bf_dec_tv_template,
  913. BF_DEC_TEST_VECTORS);
  914. test_cipher("cbc(blowfish)", ENCRYPT, bf_cbc_enc_tv_template,
  915. BF_CBC_ENC_TEST_VECTORS);
  916. test_cipher("cbc(blowfish)", DECRYPT, bf_cbc_dec_tv_template,
  917. BF_CBC_DEC_TEST_VECTORS);
  918. break;
  919. case 8:
  920. test_cipher("ecb(twofish)", ENCRYPT, tf_enc_tv_template,
  921. TF_ENC_TEST_VECTORS);
  922. test_cipher("ecb(twofish)", DECRYPT, tf_dec_tv_template,
  923. TF_DEC_TEST_VECTORS);
  924. test_cipher("cbc(twofish)", ENCRYPT, tf_cbc_enc_tv_template,
  925. TF_CBC_ENC_TEST_VECTORS);
  926. test_cipher("cbc(twofish)", DECRYPT, tf_cbc_dec_tv_template,
  927. TF_CBC_DEC_TEST_VECTORS);
  928. break;
  929. case 9:
  930. test_cipher("ecb(serpent)", ENCRYPT, serpent_enc_tv_template,
  931. SERPENT_ENC_TEST_VECTORS);
  932. test_cipher("ecb(serpent)", DECRYPT, serpent_dec_tv_template,
  933. SERPENT_DEC_TEST_VECTORS);
  934. break;
  935. case 10:
  936. test_cipher("ecb(aes)", ENCRYPT, aes_enc_tv_template,
  937. AES_ENC_TEST_VECTORS);
  938. test_cipher("ecb(aes)", DECRYPT, aes_dec_tv_template,
  939. AES_DEC_TEST_VECTORS);
  940. test_cipher("cbc(aes)", ENCRYPT, aes_cbc_enc_tv_template,
  941. AES_CBC_ENC_TEST_VECTORS);
  942. test_cipher("cbc(aes)", DECRYPT, aes_cbc_dec_tv_template,
  943. AES_CBC_DEC_TEST_VECTORS);
  944. test_cipher("lrw(aes)", ENCRYPT, aes_lrw_enc_tv_template,
  945. AES_LRW_ENC_TEST_VECTORS);
  946. test_cipher("lrw(aes)", DECRYPT, aes_lrw_dec_tv_template,
  947. AES_LRW_DEC_TEST_VECTORS);
  948. test_cipher("xts(aes)", ENCRYPT, aes_xts_enc_tv_template,
  949. AES_XTS_ENC_TEST_VECTORS);
  950. test_cipher("xts(aes)", DECRYPT, aes_xts_dec_tv_template,
  951. AES_XTS_DEC_TEST_VECTORS);
  952. break;
  953. case 11:
  954. test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
  955. break;
  956. case 12:
  957. test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
  958. break;
  959. case 13:
  960. test_deflate();
  961. break;
  962. case 14:
  963. test_cipher("ecb(cast5)", ENCRYPT, cast5_enc_tv_template,
  964. CAST5_ENC_TEST_VECTORS);
  965. test_cipher("ecb(cast5)", DECRYPT, cast5_dec_tv_template,
  966. CAST5_DEC_TEST_VECTORS);
  967. break;
  968. case 15:
  969. test_cipher("ecb(cast6)", ENCRYPT, cast6_enc_tv_template,
  970. CAST6_ENC_TEST_VECTORS);
  971. test_cipher("ecb(cast6)", DECRYPT, cast6_dec_tv_template,
  972. CAST6_DEC_TEST_VECTORS);
  973. break;
  974. case 16:
  975. test_cipher("ecb(arc4)", ENCRYPT, arc4_enc_tv_template,
  976. ARC4_ENC_TEST_VECTORS);
  977. test_cipher("ecb(arc4)", DECRYPT, arc4_dec_tv_template,
  978. ARC4_DEC_TEST_VECTORS);
  979. break;
  980. case 17:
  981. test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
  982. break;
  983. case 18:
  984. test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
  985. break;
  986. case 19:
  987. test_cipher("ecb(tea)", ENCRYPT, tea_enc_tv_template,
  988. TEA_ENC_TEST_VECTORS);
  989. test_cipher("ecb(tea)", DECRYPT, tea_dec_tv_template,
  990. TEA_DEC_TEST_VECTORS);
  991. break;
  992. case 20:
  993. test_cipher("ecb(xtea)", ENCRYPT, xtea_enc_tv_template,
  994. XTEA_ENC_TEST_VECTORS);
  995. test_cipher("ecb(xtea)", DECRYPT, xtea_dec_tv_template,
  996. XTEA_DEC_TEST_VECTORS);
  997. break;
  998. case 21:
  999. test_cipher("ecb(khazad)", ENCRYPT, khazad_enc_tv_template,
  1000. KHAZAD_ENC_TEST_VECTORS);
  1001. test_cipher("ecb(khazad)", DECRYPT, khazad_dec_tv_template,
  1002. KHAZAD_DEC_TEST_VECTORS);
  1003. break;
  1004. case 22:
  1005. test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
  1006. break;
  1007. case 23:
  1008. test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
  1009. break;
  1010. case 24:
  1011. test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
  1012. break;
  1013. case 25:
  1014. test_cipher("ecb(tnepres)", ENCRYPT, tnepres_enc_tv_template,
  1015. TNEPRES_ENC_TEST_VECTORS);
  1016. test_cipher("ecb(tnepres)", DECRYPT, tnepres_dec_tv_template,
  1017. TNEPRES_DEC_TEST_VECTORS);
  1018. break;
  1019. case 26:
  1020. test_cipher("ecb(anubis)", ENCRYPT, anubis_enc_tv_template,
  1021. ANUBIS_ENC_TEST_VECTORS);
  1022. test_cipher("ecb(anubis)", DECRYPT, anubis_dec_tv_template,
  1023. ANUBIS_DEC_TEST_VECTORS);
  1024. test_cipher("cbc(anubis)", ENCRYPT, anubis_cbc_enc_tv_template,
  1025. ANUBIS_CBC_ENC_TEST_VECTORS);
  1026. test_cipher("cbc(anubis)", DECRYPT, anubis_cbc_dec_tv_template,
  1027. ANUBIS_CBC_ENC_TEST_VECTORS);
  1028. break;
  1029. case 27:
  1030. test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
  1031. break;
  1032. case 28:
  1033. test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
  1034. break;
  1035. case 29:
  1036. test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
  1037. break;
  1038. case 30:
  1039. test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
  1040. XETA_ENC_TEST_VECTORS);
  1041. test_cipher("ecb(xeta)", DECRYPT, xeta_dec_tv_template,
  1042. XETA_DEC_TEST_VECTORS);
  1043. break;
  1044. case 31:
  1045. test_cipher("pcbc(fcrypt)", ENCRYPT, fcrypt_pcbc_enc_tv_template,
  1046. FCRYPT_ENC_TEST_VECTORS);
  1047. test_cipher("pcbc(fcrypt)", DECRYPT, fcrypt_pcbc_dec_tv_template,
  1048. FCRYPT_DEC_TEST_VECTORS);
  1049. break;
  1050. case 32:
  1051. test_cipher("ecb(camellia)", ENCRYPT,
  1052. camellia_enc_tv_template,
  1053. CAMELLIA_ENC_TEST_VECTORS);
  1054. test_cipher("ecb(camellia)", DECRYPT,
  1055. camellia_dec_tv_template,
  1056. CAMELLIA_DEC_TEST_VECTORS);
  1057. test_cipher("cbc(camellia)", ENCRYPT,
  1058. camellia_cbc_enc_tv_template,
  1059. CAMELLIA_CBC_ENC_TEST_VECTORS);
  1060. test_cipher("cbc(camellia)", DECRYPT,
  1061. camellia_cbc_dec_tv_template,
  1062. CAMELLIA_CBC_DEC_TEST_VECTORS);
  1063. break;
  1064. case 100:
  1065. test_hash("hmac(md5)", hmac_md5_tv_template,
  1066. HMAC_MD5_TEST_VECTORS);
  1067. break;
  1068. case 101:
  1069. test_hash("hmac(sha1)", hmac_sha1_tv_template,
  1070. HMAC_SHA1_TEST_VECTORS);
  1071. break;
  1072. case 102:
  1073. test_hash("hmac(sha256)", hmac_sha256_tv_template,
  1074. HMAC_SHA256_TEST_VECTORS);
  1075. break;
  1076. case 103:
  1077. test_hash("hmac(sha384)", hmac_sha384_tv_template,
  1078. HMAC_SHA384_TEST_VECTORS);
  1079. break;
  1080. case 104:
  1081. test_hash("hmac(sha512)", hmac_sha512_tv_template,
  1082. HMAC_SHA512_TEST_VECTORS);
  1083. break;
  1084. case 200:
  1085. test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
  1086. aes_speed_template);
  1087. test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
  1088. aes_speed_template);
  1089. test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
  1090. aes_speed_template);
  1091. test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
  1092. aes_speed_template);
  1093. test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
  1094. aes_lrw_speed_template);
  1095. test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
  1096. aes_lrw_speed_template);
  1097. test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
  1098. aes_xts_speed_template);
  1099. test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
  1100. aes_xts_speed_template);
  1101. break;
  1102. case 201:
  1103. test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
  1104. des3_ede_enc_tv_template,
  1105. DES3_EDE_ENC_TEST_VECTORS,
  1106. des3_ede_speed_template);
  1107. test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
  1108. des3_ede_dec_tv_template,
  1109. DES3_EDE_DEC_TEST_VECTORS,
  1110. des3_ede_speed_template);
  1111. test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
  1112. des3_ede_enc_tv_template,
  1113. DES3_EDE_ENC_TEST_VECTORS,
  1114. des3_ede_speed_template);
  1115. test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
  1116. des3_ede_dec_tv_template,
  1117. DES3_EDE_DEC_TEST_VECTORS,
  1118. des3_ede_speed_template);
  1119. break;
  1120. case 202:
  1121. test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
  1122. twofish_speed_template);
  1123. test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
  1124. twofish_speed_template);
  1125. test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
  1126. twofish_speed_template);
  1127. test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
  1128. twofish_speed_template);
  1129. break;
  1130. case 203:
  1131. test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
  1132. blowfish_speed_template);
  1133. test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
  1134. blowfish_speed_template);
  1135. test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
  1136. blowfish_speed_template);
  1137. test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
  1138. blowfish_speed_template);
  1139. break;
  1140. case 204:
  1141. test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
  1142. des_speed_template);
  1143. test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
  1144. des_speed_template);
  1145. test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
  1146. des_speed_template);
  1147. test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
  1148. des_speed_template);
  1149. break;
  1150. case 205:
  1151. test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
  1152. camellia_speed_template);
  1153. test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
  1154. camellia_speed_template);
  1155. test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
  1156. camellia_speed_template);
  1157. test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
  1158. camellia_speed_template);
  1159. break;
  1160. case 300:
  1161. /* fall through */
  1162. case 301:
  1163. test_hash_speed("md4", sec, generic_hash_speed_template);
  1164. if (mode > 300 && mode < 400) break;
  1165. case 302:
  1166. test_hash_speed("md5", sec, generic_hash_speed_template);
  1167. if (mode > 300 && mode < 400) break;
  1168. case 303:
  1169. test_hash_speed("sha1", sec, generic_hash_speed_template);
  1170. if (mode > 300 && mode < 400) break;
  1171. case 304:
  1172. test_hash_speed("sha256", sec, generic_hash_speed_template);
  1173. if (mode > 300 && mode < 400) break;
  1174. case 305:
  1175. test_hash_speed("sha384", sec, generic_hash_speed_template);
  1176. if (mode > 300 && mode < 400) break;
  1177. case 306:
  1178. test_hash_speed("sha512", sec, generic_hash_speed_template);
  1179. if (mode > 300 && mode < 400) break;
  1180. case 307:
  1181. test_hash_speed("wp256", sec, generic_hash_speed_template);
  1182. if (mode > 300 && mode < 400) break;
  1183. case 308:
  1184. test_hash_speed("wp384", sec, generic_hash_speed_template);
  1185. if (mode > 300 && mode < 400) break;
  1186. case 309:
  1187. test_hash_speed("wp512", sec, generic_hash_speed_template);
  1188. if (mode > 300 && mode < 400) break;
  1189. case 310:
  1190. test_hash_speed("tgr128", sec, generic_hash_speed_template);
  1191. if (mode > 300 && mode < 400) break;
  1192. case 311:
  1193. test_hash_speed("tgr160", sec, generic_hash_speed_template);
  1194. if (mode > 300 && mode < 400) break;
  1195. case 312:
  1196. test_hash_speed("tgr192", sec, generic_hash_speed_template);
  1197. if (mode > 300 && mode < 400) break;
  1198. case 399:
  1199. break;
  1200. case 1000:
  1201. test_available();
  1202. break;
  1203. default:
  1204. /* useful for debugging */
  1205. printk("not testing anything\n");
  1206. break;
  1207. }
  1208. }
  1209. static int __init init(void)
  1210. {
  1211. tvmem = kmalloc(TVMEMSIZE, GFP_KERNEL);
  1212. if (tvmem == NULL)
  1213. return -ENOMEM;
  1214. xbuf = kmalloc(XBUFSIZE, GFP_KERNEL);
  1215. if (xbuf == NULL) {
  1216. kfree(tvmem);
  1217. return -ENOMEM;
  1218. }
  1219. do_test();
  1220. kfree(xbuf);
  1221. kfree(tvmem);
  1222. /* We intentionaly return -EAGAIN to prevent keeping
  1223. * the module. It does all its work from init()
  1224. * and doesn't offer any runtime functionality
  1225. * => we don't need it in the memory, do we?
  1226. * -- mludvig
  1227. */
  1228. return -EAGAIN;
  1229. }
  1230. /*
  1231. * If an init function is provided, an exit function must also be provided
  1232. * to allow module unload.
  1233. */
  1234. static void __exit fini(void) { }
  1235. module_init(init);
  1236. module_exit(fini);
  1237. module_param(mode, int, 0);
  1238. module_param(sec, uint, 0);
  1239. MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
  1240. "(defaults to zero which uses CPU cycles instead)");
  1241. MODULE_LICENSE("GPL");
  1242. MODULE_DESCRIPTION("Quick & dirty crypto testing module");
  1243. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");