tcrypt.c 38 KB

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