tcrypt.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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. * 2004-08-09 Added cipher speed tests (Reyk Floeter <reyk@vantronix.net>)
  16. * 2003-09-14 Rewritten by Kartikey Mahendra Bhatt
  17. *
  18. */
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/mm.h>
  22. #include <linux/slab.h>
  23. #include <asm/scatterlist.h>
  24. #include <linux/string.h>
  25. #include <linux/crypto.h>
  26. #include <linux/highmem.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/jiffies.h>
  29. #include "tcrypt.h"
  30. /*
  31. * Need to kmalloc() memory for testing kmap().
  32. */
  33. #define TVMEMSIZE 16384
  34. #define XBUFSIZE 32768
  35. /*
  36. * Indexes into the xbuf to simulate cross-page access.
  37. */
  38. #define IDX1 37
  39. #define IDX2 32400
  40. #define IDX3 1
  41. #define IDX4 8193
  42. #define IDX5 22222
  43. #define IDX6 17101
  44. #define IDX7 27333
  45. #define IDX8 3000
  46. /*
  47. * Used by test_cipher()
  48. */
  49. #define ENCRYPT 1
  50. #define DECRYPT 0
  51. #define MODE_ECB 1
  52. #define MODE_CBC 0
  53. static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
  54. /*
  55. * Used by test_cipher_speed()
  56. */
  57. static unsigned int sec = 10;
  58. static int mode;
  59. static char *xbuf;
  60. static char *tvmem;
  61. static char *check[] = {
  62. "des", "md5", "des3_ede", "rot13", "sha1", "sha256", "blowfish",
  63. "twofish", "serpent", "sha384", "sha512", "md4", "aes", "cast6",
  64. "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
  65. "khazad", "wp512", "wp384", "wp256", "tnepres", NULL
  66. };
  67. static void hexdump(unsigned char *buf, unsigned int len)
  68. {
  69. while (len--)
  70. printk("%02x", *buf++);
  71. printk("\n");
  72. }
  73. static void test_hash(char *algo, struct hash_testvec *template,
  74. unsigned int tcount)
  75. {
  76. char *p;
  77. unsigned int i, j, k, temp;
  78. struct scatterlist sg[8];
  79. char result[64];
  80. struct crypto_tfm *tfm;
  81. struct hash_testvec *hash_tv;
  82. unsigned int tsize;
  83. printk("\ntesting %s\n", algo);
  84. tsize = sizeof(struct hash_testvec);
  85. tsize *= tcount;
  86. if (tsize > TVMEMSIZE) {
  87. printk("template (%u) too big for tvmem (%u)\n", tsize, TVMEMSIZE);
  88. return;
  89. }
  90. memcpy(tvmem, template, tsize);
  91. hash_tv = (void *)tvmem;
  92. tfm = crypto_alloc_tfm(algo, 0);
  93. if (tfm == NULL) {
  94. printk("failed to load transform for %s\n", algo);
  95. return;
  96. }
  97. for (i = 0; i < tcount; i++) {
  98. printk("test %u:\n", i + 1);
  99. memset(result, 0, 64);
  100. p = hash_tv[i].plaintext;
  101. sg[0].page = virt_to_page(p);
  102. sg[0].offset = offset_in_page(p);
  103. sg[0].length = hash_tv[i].psize;
  104. crypto_digest_init(tfm);
  105. if (tfm->crt_u.digest.dit_setkey) {
  106. crypto_digest_setkey(tfm, hash_tv[i].key,
  107. hash_tv[i].ksize);
  108. }
  109. crypto_digest_update(tfm, sg, 1);
  110. crypto_digest_final(tfm, result);
  111. hexdump(result, crypto_tfm_alg_digestsize(tfm));
  112. printk("%s\n",
  113. memcmp(result, hash_tv[i].digest,
  114. crypto_tfm_alg_digestsize(tfm)) ?
  115. "fail" : "pass");
  116. }
  117. printk("testing %s across pages\n", algo);
  118. /* setup the dummy buffer first */
  119. memset(xbuf, 0, XBUFSIZE);
  120. j = 0;
  121. for (i = 0; i < tcount; i++) {
  122. if (hash_tv[i].np) {
  123. j++;
  124. printk("test %u:\n", j);
  125. memset(result, 0, 64);
  126. temp = 0;
  127. for (k = 0; k < hash_tv[i].np; k++) {
  128. memcpy(&xbuf[IDX[k]],
  129. hash_tv[i].plaintext + temp,
  130. hash_tv[i].tap[k]);
  131. temp += hash_tv[i].tap[k];
  132. p = &xbuf[IDX[k]];
  133. sg[k].page = virt_to_page(p);
  134. sg[k].offset = offset_in_page(p);
  135. sg[k].length = hash_tv[i].tap[k];
  136. }
  137. crypto_digest_digest(tfm, sg, hash_tv[i].np, result);
  138. hexdump(result, crypto_tfm_alg_digestsize(tfm));
  139. printk("%s\n",
  140. memcmp(result, hash_tv[i].digest,
  141. crypto_tfm_alg_digestsize(tfm)) ?
  142. "fail" : "pass");
  143. }
  144. }
  145. crypto_free_tfm(tfm);
  146. }
  147. #ifdef CONFIG_CRYPTO_HMAC
  148. static void test_hmac(char *algo, struct hmac_testvec *template,
  149. unsigned int tcount)
  150. {
  151. char *p;
  152. unsigned int i, j, k, temp;
  153. struct scatterlist sg[8];
  154. char result[64];
  155. struct crypto_tfm *tfm;
  156. struct hmac_testvec *hmac_tv;
  157. unsigned int tsize, klen;
  158. tfm = crypto_alloc_tfm(algo, 0);
  159. if (tfm == NULL) {
  160. printk("failed to load transform for %s\n", algo);
  161. return;
  162. }
  163. printk("\ntesting hmac_%s\n", algo);
  164. tsize = sizeof(struct hmac_testvec);
  165. tsize *= tcount;
  166. if (tsize > TVMEMSIZE) {
  167. printk("template (%u) too big for tvmem (%u)\n", tsize,
  168. TVMEMSIZE);
  169. goto out;
  170. }
  171. memcpy(tvmem, template, tsize);
  172. hmac_tv = (void *)tvmem;
  173. for (i = 0; i < tcount; i++) {
  174. printk("test %u:\n", i + 1);
  175. memset(result, 0, sizeof (result));
  176. p = hmac_tv[i].plaintext;
  177. klen = hmac_tv[i].ksize;
  178. sg[0].page = virt_to_page(p);
  179. sg[0].offset = offset_in_page(p);
  180. sg[0].length = hmac_tv[i].psize;
  181. crypto_hmac(tfm, hmac_tv[i].key, &klen, sg, 1, result);
  182. hexdump(result, crypto_tfm_alg_digestsize(tfm));
  183. printk("%s\n",
  184. memcmp(result, hmac_tv[i].digest,
  185. crypto_tfm_alg_digestsize(tfm)) ? "fail" :
  186. "pass");
  187. }
  188. printk("\ntesting hmac_%s across pages\n", algo);
  189. memset(xbuf, 0, XBUFSIZE);
  190. j = 0;
  191. for (i = 0; i < tcount; i++) {
  192. if (hmac_tv[i].np) {
  193. j++;
  194. printk("test %u:\n",j);
  195. memset(result, 0, 64);
  196. temp = 0;
  197. klen = hmac_tv[i].ksize;
  198. for (k = 0; k < hmac_tv[i].np; k++) {
  199. memcpy(&xbuf[IDX[k]],
  200. hmac_tv[i].plaintext + temp,
  201. hmac_tv[i].tap[k]);
  202. temp += hmac_tv[i].tap[k];
  203. p = &xbuf[IDX[k]];
  204. sg[k].page = virt_to_page(p);
  205. sg[k].offset = offset_in_page(p);
  206. sg[k].length = hmac_tv[i].tap[k];
  207. }
  208. crypto_hmac(tfm, hmac_tv[i].key, &klen, sg,
  209. hmac_tv[i].np, result);
  210. hexdump(result, crypto_tfm_alg_digestsize(tfm));
  211. printk("%s\n",
  212. memcmp(result, hmac_tv[i].digest,
  213. crypto_tfm_alg_digestsize(tfm)) ?
  214. "fail" : "pass");
  215. }
  216. }
  217. out:
  218. crypto_free_tfm(tfm);
  219. }
  220. #endif /* CONFIG_CRYPTO_HMAC */
  221. static void test_cipher(char *algo, int mode, int enc,
  222. struct cipher_testvec *template, unsigned int tcount)
  223. {
  224. unsigned int ret, i, j, k, temp;
  225. unsigned int tsize;
  226. char *p, *q;
  227. struct crypto_tfm *tfm;
  228. char *key;
  229. struct cipher_testvec *cipher_tv;
  230. struct scatterlist sg[8];
  231. const char *e, *m;
  232. if (enc == ENCRYPT)
  233. e = "encryption";
  234. else
  235. e = "decryption";
  236. if (mode == MODE_ECB)
  237. m = "ECB";
  238. else
  239. m = "CBC";
  240. printk("\ntesting %s %s %s\n", algo, m, e);
  241. tsize = sizeof (struct cipher_testvec);
  242. tsize *= tcount;
  243. if (tsize > TVMEMSIZE) {
  244. printk("template (%u) too big for tvmem (%u)\n", tsize,
  245. TVMEMSIZE);
  246. return;
  247. }
  248. memcpy(tvmem, template, tsize);
  249. cipher_tv = (void *)tvmem;
  250. if (mode)
  251. tfm = crypto_alloc_tfm(algo, 0);
  252. else
  253. tfm = crypto_alloc_tfm(algo, CRYPTO_TFM_MODE_CBC);
  254. if (tfm == NULL) {
  255. printk("failed to load transform for %s %s\n", algo, m);
  256. return;
  257. }
  258. j = 0;
  259. for (i = 0; i < tcount; i++) {
  260. if (!(cipher_tv[i].np)) {
  261. j++;
  262. printk("test %u (%d bit key):\n",
  263. j, cipher_tv[i].klen * 8);
  264. tfm->crt_flags = 0;
  265. if (cipher_tv[i].wk)
  266. tfm->crt_flags |= CRYPTO_TFM_REQ_WEAK_KEY;
  267. key = cipher_tv[i].key;
  268. ret = crypto_cipher_setkey(tfm, key, cipher_tv[i].klen);
  269. if (ret) {
  270. printk("setkey() failed flags=%x\n", tfm->crt_flags);
  271. if (!cipher_tv[i].fail)
  272. goto out;
  273. }
  274. p = cipher_tv[i].input;
  275. sg[0].page = virt_to_page(p);
  276. sg[0].offset = offset_in_page(p);
  277. sg[0].length = cipher_tv[i].ilen;
  278. if (!mode) {
  279. crypto_cipher_set_iv(tfm, cipher_tv[i].iv,
  280. crypto_tfm_alg_ivsize(tfm));
  281. }
  282. if (enc)
  283. ret = crypto_cipher_encrypt(tfm, sg, sg, cipher_tv[i].ilen);
  284. else
  285. ret = crypto_cipher_decrypt(tfm, sg, sg, cipher_tv[i].ilen);
  286. if (ret) {
  287. printk("%s () failed flags=%x\n", e, tfm->crt_flags);
  288. goto out;
  289. }
  290. q = kmap(sg[0].page) + sg[0].offset;
  291. hexdump(q, cipher_tv[i].rlen);
  292. printk("%s\n",
  293. memcmp(q, cipher_tv[i].result,
  294. cipher_tv[i].rlen) ? "fail" : "pass");
  295. }
  296. }
  297. printk("\ntesting %s %s %s across pages (chunking)\n", algo, m, e);
  298. memset(xbuf, 0, XBUFSIZE);
  299. j = 0;
  300. for (i = 0; i < tcount; i++) {
  301. if (cipher_tv[i].np) {
  302. j++;
  303. printk("test %u (%d bit key):\n",
  304. j, cipher_tv[i].klen * 8);
  305. tfm->crt_flags = 0;
  306. if (cipher_tv[i].wk)
  307. tfm->crt_flags |= CRYPTO_TFM_REQ_WEAK_KEY;
  308. key = cipher_tv[i].key;
  309. ret = crypto_cipher_setkey(tfm, key, cipher_tv[i].klen);
  310. if (ret) {
  311. printk("setkey() failed flags=%x\n", tfm->crt_flags);
  312. if (!cipher_tv[i].fail)
  313. goto out;
  314. }
  315. temp = 0;
  316. for (k = 0; k < cipher_tv[i].np; k++) {
  317. memcpy(&xbuf[IDX[k]],
  318. cipher_tv[i].input + temp,
  319. cipher_tv[i].tap[k]);
  320. temp += cipher_tv[i].tap[k];
  321. p = &xbuf[IDX[k]];
  322. sg[k].page = virt_to_page(p);
  323. sg[k].offset = offset_in_page(p);
  324. sg[k].length = cipher_tv[i].tap[k];
  325. }
  326. if (!mode) {
  327. crypto_cipher_set_iv(tfm, cipher_tv[i].iv,
  328. crypto_tfm_alg_ivsize(tfm));
  329. }
  330. if (enc)
  331. ret = crypto_cipher_encrypt(tfm, sg, sg, cipher_tv[i].ilen);
  332. else
  333. ret = crypto_cipher_decrypt(tfm, sg, sg, cipher_tv[i].ilen);
  334. if (ret) {
  335. printk("%s () failed flags=%x\n", e, tfm->crt_flags);
  336. goto out;
  337. }
  338. temp = 0;
  339. for (k = 0; k < cipher_tv[i].np; k++) {
  340. printk("page %u\n", k);
  341. q = kmap(sg[k].page) + sg[k].offset;
  342. hexdump(q, cipher_tv[i].tap[k]);
  343. printk("%s\n",
  344. memcmp(q, cipher_tv[i].result + temp,
  345. cipher_tv[i].tap[k]) ? "fail" :
  346. "pass");
  347. temp += cipher_tv[i].tap[k];
  348. }
  349. }
  350. }
  351. out:
  352. crypto_free_tfm(tfm);
  353. }
  354. static void test_cipher_speed(char *algo, int mode, int enc, unsigned int sec,
  355. struct cipher_speed *speed)
  356. {
  357. unsigned int ret, i, iv_len;
  358. unsigned char *key, *p, iv[128];
  359. struct crypto_tfm *tfm;
  360. struct scatterlist sg[8];
  361. unsigned long start, bcount;
  362. const char *e, *m;
  363. if (enc == ENCRYPT)
  364. e = "encryption";
  365. else
  366. e = "decryption";
  367. if (mode == MODE_ECB)
  368. m = "ECB";
  369. else
  370. m = "CBC";
  371. printk("\ntesting speed of %s %s %s\n", algo, m, e);
  372. if (mode)
  373. tfm = crypto_alloc_tfm(algo, 0);
  374. else
  375. tfm = crypto_alloc_tfm(algo, CRYPTO_TFM_MODE_CBC);
  376. if (tfm == NULL) {
  377. printk("failed to load transform for %s %s\n", algo, m);
  378. return;
  379. }
  380. for (i = 0; speed[i].klen != 0; i++) {
  381. if ((speed[i].blen + speed[i].klen) > TVMEMSIZE) {
  382. printk("template (%u) too big for tvmem (%u)\n",
  383. speed[i].blen + speed[i].klen, TVMEMSIZE);
  384. goto out;
  385. }
  386. printk("test %u (%d bit key, %d byte blocks): ", i,
  387. speed[i].klen * 8, speed[i].blen);
  388. memset(tvmem, 0xff, speed[i].klen + speed[i].blen);
  389. /* set key, plain text and IV */
  390. key = (unsigned char *)tvmem;
  391. p = (unsigned char *)tvmem + speed[i].klen;
  392. ret = crypto_cipher_setkey(tfm, key, speed[i].klen);
  393. if (ret) {
  394. printk("setkey() failed flags=%x\n", tfm->crt_flags);
  395. goto out;
  396. }
  397. if (!mode) {
  398. iv_len = crypto_tfm_alg_ivsize(tfm);
  399. memset(&iv, 0xff, iv_len);
  400. crypto_cipher_set_iv(tfm, iv, iv_len);
  401. }
  402. for (start = jiffies, bcount = 0;
  403. ((jiffies - start) / HZ) < sec; bcount++) {
  404. sg[0].page = virt_to_page(p);
  405. sg[0].offset = offset_in_page(p);
  406. sg[0].length = speed[i].blen;
  407. if (enc)
  408. ret = crypto_cipher_encrypt(tfm, sg, sg, speed[i].blen);
  409. else
  410. ret = crypto_cipher_decrypt(tfm, sg, sg, speed[i].blen);
  411. if (ret) {
  412. printk("%s () failed flags=%x\n", e, tfm->crt_flags);
  413. goto out;
  414. }
  415. }
  416. printk("%lu operations in %u seconds (%lu bytes)\n",
  417. bcount, sec, bcount * speed[i].blen);
  418. }
  419. out:
  420. crypto_free_tfm(tfm);
  421. }
  422. static void test_deflate(void)
  423. {
  424. unsigned int i;
  425. char result[COMP_BUF_SIZE];
  426. struct crypto_tfm *tfm;
  427. struct comp_testvec *tv;
  428. unsigned int tsize;
  429. printk("\ntesting deflate compression\n");
  430. tsize = sizeof (deflate_comp_tv_template);
  431. if (tsize > TVMEMSIZE) {
  432. printk("template (%u) too big for tvmem (%u)\n", tsize,
  433. TVMEMSIZE);
  434. return;
  435. }
  436. memcpy(tvmem, deflate_comp_tv_template, tsize);
  437. tv = (void *)tvmem;
  438. tfm = crypto_alloc_tfm("deflate", 0);
  439. if (tfm == NULL) {
  440. printk("failed to load transform for deflate\n");
  441. return;
  442. }
  443. for (i = 0; i < DEFLATE_COMP_TEST_VECTORS; i++) {
  444. int ilen, ret, dlen = COMP_BUF_SIZE;
  445. printk("test %u:\n", i + 1);
  446. memset(result, 0, sizeof (result));
  447. ilen = tv[i].inlen;
  448. ret = crypto_comp_compress(tfm, tv[i].input,
  449. ilen, result, &dlen);
  450. if (ret) {
  451. printk("fail: ret=%d\n", ret);
  452. continue;
  453. }
  454. hexdump(result, dlen);
  455. printk("%s (ratio %d:%d)\n",
  456. memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
  457. ilen, dlen);
  458. }
  459. printk("\ntesting deflate decompression\n");
  460. tsize = sizeof (deflate_decomp_tv_template);
  461. if (tsize > TVMEMSIZE) {
  462. printk("template (%u) too big for tvmem (%u)\n", tsize,
  463. TVMEMSIZE);
  464. goto out;
  465. }
  466. memcpy(tvmem, deflate_decomp_tv_template, tsize);
  467. tv = (void *)tvmem;
  468. for (i = 0; i < DEFLATE_DECOMP_TEST_VECTORS; i++) {
  469. int ilen, ret, dlen = COMP_BUF_SIZE;
  470. printk("test %u:\n", i + 1);
  471. memset(result, 0, sizeof (result));
  472. ilen = tv[i].inlen;
  473. ret = crypto_comp_decompress(tfm, tv[i].input,
  474. ilen, result, &dlen);
  475. if (ret) {
  476. printk("fail: ret=%d\n", ret);
  477. continue;
  478. }
  479. hexdump(result, dlen);
  480. printk("%s (ratio %d:%d)\n",
  481. memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
  482. ilen, dlen);
  483. }
  484. out:
  485. crypto_free_tfm(tfm);
  486. }
  487. static void test_crc32c(void)
  488. {
  489. #define NUMVEC 6
  490. #define VECSIZE 40
  491. int i, j, pass;
  492. u32 crc;
  493. u8 b, test_vec[NUMVEC][VECSIZE];
  494. static u32 vec_results[NUMVEC] = {
  495. 0x0e2c157f, 0xe980ebf6, 0xde74bded,
  496. 0xd579c862, 0xba979ad0, 0x2b29d913
  497. };
  498. static u32 tot_vec_results = 0x24c5d375;
  499. struct scatterlist sg[NUMVEC];
  500. struct crypto_tfm *tfm;
  501. char *fmtdata = "testing crc32c initialized to %08x: %s\n";
  502. #define SEEDTESTVAL 0xedcba987
  503. u32 seed;
  504. printk("\ntesting crc32c\n");
  505. tfm = crypto_alloc_tfm("crc32c", 0);
  506. if (tfm == NULL) {
  507. printk("failed to load transform for crc32c\n");
  508. return;
  509. }
  510. crypto_digest_init(tfm);
  511. crypto_digest_final(tfm, (u8*)&crc);
  512. printk(fmtdata, crc, (crc == 0) ? "pass" : "ERROR");
  513. /*
  514. * stuff test_vec with known values, simple incrementing
  515. * byte values.
  516. */
  517. b = 0;
  518. for (i = 0; i < NUMVEC; i++) {
  519. for (j = 0; j < VECSIZE; j++)
  520. test_vec[i][j] = ++b;
  521. sg[i].page = virt_to_page(test_vec[i]);
  522. sg[i].offset = offset_in_page(test_vec[i]);
  523. sg[i].length = VECSIZE;
  524. }
  525. seed = SEEDTESTVAL;
  526. (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32));
  527. crypto_digest_final(tfm, (u8*)&crc);
  528. printk("testing crc32c setkey returns %08x : %s\n", crc, (crc == (SEEDTESTVAL ^ ~(u32)0)) ?
  529. "pass" : "ERROR");
  530. printk("testing crc32c using update/final:\n");
  531. pass = 1; /* assume all is well */
  532. for (i = 0; i < NUMVEC; i++) {
  533. seed = ~(u32)0;
  534. (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32));
  535. crypto_digest_update(tfm, &sg[i], 1);
  536. crypto_digest_final(tfm, (u8*)&crc);
  537. if (crc == vec_results[i]) {
  538. printk(" %08x:OK", crc);
  539. } else {
  540. printk(" %08x:BAD, wanted %08x\n", crc, vec_results[i]);
  541. pass = 0;
  542. }
  543. }
  544. printk("\ntesting crc32c using incremental accumulator:\n");
  545. crc = 0;
  546. for (i = 0; i < NUMVEC; i++) {
  547. seed = (crc ^ ~(u32)0);
  548. (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32));
  549. crypto_digest_update(tfm, &sg[i], 1);
  550. crypto_digest_final(tfm, (u8*)&crc);
  551. }
  552. if (crc == tot_vec_results) {
  553. printk(" %08x:OK", crc);
  554. } else {
  555. printk(" %08x:BAD, wanted %08x\n", crc, tot_vec_results);
  556. pass = 0;
  557. }
  558. printk("\ntesting crc32c using digest:\n");
  559. seed = ~(u32)0;
  560. (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32));
  561. crypto_digest_digest(tfm, sg, NUMVEC, (u8*)&crc);
  562. if (crc == tot_vec_results) {
  563. printk(" %08x:OK", crc);
  564. } else {
  565. printk(" %08x:BAD, wanted %08x\n", crc, tot_vec_results);
  566. pass = 0;
  567. }
  568. printk("\n%s\n", pass ? "pass" : "ERROR");
  569. crypto_free_tfm(tfm);
  570. printk("crc32c test complete\n");
  571. }
  572. static void test_available(void)
  573. {
  574. char **name = check;
  575. while (*name) {
  576. printk("alg %s ", *name);
  577. printk((crypto_alg_available(*name, 0)) ?
  578. "found\n" : "not found\n");
  579. name++;
  580. }
  581. }
  582. static void do_test(void)
  583. {
  584. switch (mode) {
  585. case 0:
  586. test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
  587. test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
  588. //DES
  589. test_cipher ("des", MODE_ECB, ENCRYPT, des_enc_tv_template, DES_ENC_TEST_VECTORS);
  590. test_cipher ("des", MODE_ECB, DECRYPT, des_dec_tv_template, DES_DEC_TEST_VECTORS);
  591. test_cipher ("des", MODE_CBC, ENCRYPT, des_cbc_enc_tv_template, DES_CBC_ENC_TEST_VECTORS);
  592. test_cipher ("des", MODE_CBC, DECRYPT, des_cbc_dec_tv_template, DES_CBC_DEC_TEST_VECTORS);
  593. //DES3_EDE
  594. test_cipher ("des3_ede", MODE_ECB, ENCRYPT, des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS);
  595. test_cipher ("des3_ede", MODE_ECB, DECRYPT, des3_ede_dec_tv_template, DES3_EDE_DEC_TEST_VECTORS);
  596. test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
  597. test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
  598. //BLOWFISH
  599. test_cipher ("blowfish", MODE_ECB, ENCRYPT, bf_enc_tv_template, BF_ENC_TEST_VECTORS);
  600. test_cipher ("blowfish", MODE_ECB, DECRYPT, bf_dec_tv_template, BF_DEC_TEST_VECTORS);
  601. test_cipher ("blowfish", MODE_CBC, ENCRYPT, bf_cbc_enc_tv_template, BF_CBC_ENC_TEST_VECTORS);
  602. test_cipher ("blowfish", MODE_CBC, DECRYPT, bf_cbc_dec_tv_template, BF_CBC_DEC_TEST_VECTORS);
  603. //TWOFISH
  604. test_cipher ("twofish", MODE_ECB, ENCRYPT, tf_enc_tv_template, TF_ENC_TEST_VECTORS);
  605. test_cipher ("twofish", MODE_ECB, DECRYPT, tf_dec_tv_template, TF_DEC_TEST_VECTORS);
  606. test_cipher ("twofish", MODE_CBC, ENCRYPT, tf_cbc_enc_tv_template, TF_CBC_ENC_TEST_VECTORS);
  607. test_cipher ("twofish", MODE_CBC, DECRYPT, tf_cbc_dec_tv_template, TF_CBC_DEC_TEST_VECTORS);
  608. //SERPENT
  609. test_cipher ("serpent", MODE_ECB, ENCRYPT, serpent_enc_tv_template, SERPENT_ENC_TEST_VECTORS);
  610. test_cipher ("serpent", MODE_ECB, DECRYPT, serpent_dec_tv_template, SERPENT_DEC_TEST_VECTORS);
  611. //TNEPRES
  612. test_cipher ("tnepres", MODE_ECB, ENCRYPT, tnepres_enc_tv_template, TNEPRES_ENC_TEST_VECTORS);
  613. test_cipher ("tnepres", MODE_ECB, DECRYPT, tnepres_dec_tv_template, TNEPRES_DEC_TEST_VECTORS);
  614. //AES
  615. test_cipher ("aes", MODE_ECB, ENCRYPT, aes_enc_tv_template, AES_ENC_TEST_VECTORS);
  616. test_cipher ("aes", MODE_ECB, DECRYPT, aes_dec_tv_template, AES_DEC_TEST_VECTORS);
  617. //CAST5
  618. test_cipher ("cast5", MODE_ECB, ENCRYPT, cast5_enc_tv_template, CAST5_ENC_TEST_VECTORS);
  619. test_cipher ("cast5", MODE_ECB, DECRYPT, cast5_dec_tv_template, CAST5_DEC_TEST_VECTORS);
  620. //CAST6
  621. test_cipher ("cast6", MODE_ECB, ENCRYPT, cast6_enc_tv_template, CAST6_ENC_TEST_VECTORS);
  622. test_cipher ("cast6", MODE_ECB, DECRYPT, cast6_dec_tv_template, CAST6_DEC_TEST_VECTORS);
  623. //ARC4
  624. test_cipher ("arc4", MODE_ECB, ENCRYPT, arc4_enc_tv_template, ARC4_ENC_TEST_VECTORS);
  625. test_cipher ("arc4", MODE_ECB, DECRYPT, arc4_dec_tv_template, ARC4_DEC_TEST_VECTORS);
  626. //TEA
  627. test_cipher ("tea", MODE_ECB, ENCRYPT, tea_enc_tv_template, TEA_ENC_TEST_VECTORS);
  628. test_cipher ("tea", MODE_ECB, DECRYPT, tea_dec_tv_template, TEA_DEC_TEST_VECTORS);
  629. //XTEA
  630. test_cipher ("xtea", MODE_ECB, ENCRYPT, xtea_enc_tv_template, XTEA_ENC_TEST_VECTORS);
  631. test_cipher ("xtea", MODE_ECB, DECRYPT, xtea_dec_tv_template, XTEA_DEC_TEST_VECTORS);
  632. //KHAZAD
  633. test_cipher ("khazad", MODE_ECB, ENCRYPT, khazad_enc_tv_template, KHAZAD_ENC_TEST_VECTORS);
  634. test_cipher ("khazad", MODE_ECB, DECRYPT, khazad_dec_tv_template, KHAZAD_DEC_TEST_VECTORS);
  635. //ANUBIS
  636. test_cipher ("anubis", MODE_ECB, ENCRYPT, anubis_enc_tv_template, ANUBIS_ENC_TEST_VECTORS);
  637. test_cipher ("anubis", MODE_ECB, DECRYPT, anubis_dec_tv_template, ANUBIS_DEC_TEST_VECTORS);
  638. test_cipher ("anubis", MODE_CBC, ENCRYPT, anubis_cbc_enc_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
  639. test_cipher ("anubis", MODE_CBC, DECRYPT, anubis_cbc_dec_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
  640. test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
  641. test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
  642. test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
  643. test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
  644. test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
  645. test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
  646. test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
  647. test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
  648. test_deflate();
  649. test_crc32c();
  650. #ifdef CONFIG_CRYPTO_HMAC
  651. test_hmac("md5", hmac_md5_tv_template, HMAC_MD5_TEST_VECTORS);
  652. test_hmac("sha1", hmac_sha1_tv_template, HMAC_SHA1_TEST_VECTORS);
  653. test_hmac("sha256", hmac_sha256_tv_template, HMAC_SHA256_TEST_VECTORS);
  654. #endif
  655. test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
  656. break;
  657. case 1:
  658. test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
  659. break;
  660. case 2:
  661. test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
  662. break;
  663. case 3:
  664. test_cipher ("des", MODE_ECB, ENCRYPT, des_enc_tv_template, DES_ENC_TEST_VECTORS);
  665. test_cipher ("des", MODE_ECB, DECRYPT, des_dec_tv_template, DES_DEC_TEST_VECTORS);
  666. test_cipher ("des", MODE_CBC, ENCRYPT, des_cbc_enc_tv_template, DES_CBC_ENC_TEST_VECTORS);
  667. test_cipher ("des", MODE_CBC, DECRYPT, des_cbc_dec_tv_template, DES_CBC_DEC_TEST_VECTORS);
  668. break;
  669. case 4:
  670. test_cipher ("des3_ede", MODE_ECB, ENCRYPT, des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS);
  671. test_cipher ("des3_ede", MODE_ECB, DECRYPT, des3_ede_dec_tv_template, DES3_EDE_DEC_TEST_VECTORS);
  672. break;
  673. case 5:
  674. test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
  675. break;
  676. case 6:
  677. test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
  678. break;
  679. case 7:
  680. test_cipher ("blowfish", MODE_ECB, ENCRYPT, bf_enc_tv_template, BF_ENC_TEST_VECTORS);
  681. test_cipher ("blowfish", MODE_ECB, DECRYPT, bf_dec_tv_template, BF_DEC_TEST_VECTORS);
  682. test_cipher ("blowfish", MODE_CBC, ENCRYPT, bf_cbc_enc_tv_template, BF_CBC_ENC_TEST_VECTORS);
  683. test_cipher ("blowfish", MODE_CBC, DECRYPT, bf_cbc_dec_tv_template, BF_CBC_DEC_TEST_VECTORS);
  684. break;
  685. case 8:
  686. test_cipher ("twofish", MODE_ECB, ENCRYPT, tf_enc_tv_template, TF_ENC_TEST_VECTORS);
  687. test_cipher ("twofish", MODE_ECB, DECRYPT, tf_dec_tv_template, TF_DEC_TEST_VECTORS);
  688. test_cipher ("twofish", MODE_CBC, ENCRYPT, tf_cbc_enc_tv_template, TF_CBC_ENC_TEST_VECTORS);
  689. test_cipher ("twofish", MODE_CBC, DECRYPT, tf_cbc_dec_tv_template, TF_CBC_DEC_TEST_VECTORS);
  690. break;
  691. case 9:
  692. test_cipher ("serpent", MODE_ECB, ENCRYPT, serpent_enc_tv_template, SERPENT_ENC_TEST_VECTORS);
  693. test_cipher ("serpent", MODE_ECB, DECRYPT, serpent_dec_tv_template, SERPENT_DEC_TEST_VECTORS);
  694. break;
  695. case 10:
  696. test_cipher ("aes", MODE_ECB, ENCRYPT, aes_enc_tv_template, AES_ENC_TEST_VECTORS);
  697. test_cipher ("aes", MODE_ECB, DECRYPT, aes_dec_tv_template, AES_DEC_TEST_VECTORS);
  698. break;
  699. case 11:
  700. test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
  701. break;
  702. case 12:
  703. test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
  704. break;
  705. case 13:
  706. test_deflate();
  707. break;
  708. case 14:
  709. test_cipher ("cast5", MODE_ECB, ENCRYPT, cast5_enc_tv_template, CAST5_ENC_TEST_VECTORS);
  710. test_cipher ("cast5", MODE_ECB, DECRYPT, cast5_dec_tv_template, CAST5_DEC_TEST_VECTORS);
  711. break;
  712. case 15:
  713. test_cipher ("cast6", MODE_ECB, ENCRYPT, cast6_enc_tv_template, CAST6_ENC_TEST_VECTORS);
  714. test_cipher ("cast6", MODE_ECB, DECRYPT, cast6_dec_tv_template, CAST6_DEC_TEST_VECTORS);
  715. break;
  716. case 16:
  717. test_cipher ("arc4", MODE_ECB, ENCRYPT, arc4_enc_tv_template, ARC4_ENC_TEST_VECTORS);
  718. test_cipher ("arc4", MODE_ECB, DECRYPT, arc4_dec_tv_template, ARC4_DEC_TEST_VECTORS);
  719. break;
  720. case 17:
  721. test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
  722. break;
  723. case 18:
  724. test_crc32c();
  725. break;
  726. case 19:
  727. test_cipher ("tea", MODE_ECB, ENCRYPT, tea_enc_tv_template, TEA_ENC_TEST_VECTORS);
  728. test_cipher ("tea", MODE_ECB, DECRYPT, tea_dec_tv_template, TEA_DEC_TEST_VECTORS);
  729. break;
  730. case 20:
  731. test_cipher ("xtea", MODE_ECB, ENCRYPT, xtea_enc_tv_template, XTEA_ENC_TEST_VECTORS);
  732. test_cipher ("xtea", MODE_ECB, DECRYPT, xtea_dec_tv_template, XTEA_DEC_TEST_VECTORS);
  733. break;
  734. case 21:
  735. test_cipher ("khazad", MODE_ECB, ENCRYPT, khazad_enc_tv_template, KHAZAD_ENC_TEST_VECTORS);
  736. test_cipher ("khazad", MODE_ECB, DECRYPT, khazad_dec_tv_template, KHAZAD_DEC_TEST_VECTORS);
  737. break;
  738. case 22:
  739. test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
  740. break;
  741. case 23:
  742. test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
  743. break;
  744. case 24:
  745. test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
  746. break;
  747. case 25:
  748. test_cipher ("tnepres", MODE_ECB, ENCRYPT, tnepres_enc_tv_template, TNEPRES_ENC_TEST_VECTORS);
  749. test_cipher ("tnepres", MODE_ECB, DECRYPT, tnepres_dec_tv_template, TNEPRES_DEC_TEST_VECTORS);
  750. break;
  751. case 26:
  752. test_cipher ("anubis", MODE_ECB, ENCRYPT, anubis_enc_tv_template, ANUBIS_ENC_TEST_VECTORS);
  753. test_cipher ("anubis", MODE_ECB, DECRYPT, anubis_dec_tv_template, ANUBIS_DEC_TEST_VECTORS);
  754. test_cipher ("anubis", MODE_CBC, ENCRYPT, anubis_cbc_enc_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
  755. test_cipher ("anubis", MODE_CBC, DECRYPT, anubis_cbc_dec_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
  756. break;
  757. case 27:
  758. test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
  759. break;
  760. case 28:
  761. test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
  762. break;
  763. case 29:
  764. test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
  765. break;
  766. #ifdef CONFIG_CRYPTO_HMAC
  767. case 100:
  768. test_hmac("md5", hmac_md5_tv_template, HMAC_MD5_TEST_VECTORS);
  769. break;
  770. case 101:
  771. test_hmac("sha1", hmac_sha1_tv_template, HMAC_SHA1_TEST_VECTORS);
  772. break;
  773. case 102:
  774. test_hmac("sha256", hmac_sha256_tv_template, HMAC_SHA256_TEST_VECTORS);
  775. break;
  776. #endif
  777. case 200:
  778. test_cipher_speed("aes", MODE_ECB, ENCRYPT, sec, aes_speed_template);
  779. test_cipher_speed("aes", MODE_ECB, DECRYPT, sec, aes_speed_template);
  780. test_cipher_speed("aes", MODE_CBC, ENCRYPT, sec, aes_speed_template);
  781. test_cipher_speed("aes", MODE_CBC, DECRYPT, sec, aes_speed_template);
  782. break;
  783. case 201:
  784. test_cipher_speed("des3_ede", MODE_ECB, ENCRYPT, sec, des3_ede_speed_template);
  785. test_cipher_speed("des3_ede", MODE_ECB, DECRYPT, sec, des3_ede_speed_template);
  786. test_cipher_speed("des3_ede", MODE_CBC, ENCRYPT, sec, des3_ede_speed_template);
  787. test_cipher_speed("des3_ede", MODE_CBC, DECRYPT, sec, des3_ede_speed_template);
  788. break;
  789. case 202:
  790. test_cipher_speed("twofish", MODE_ECB, ENCRYPT, sec, twofish_speed_template);
  791. test_cipher_speed("twofish", MODE_ECB, DECRYPT, sec, twofish_speed_template);
  792. test_cipher_speed("twofish", MODE_CBC, ENCRYPT, sec, twofish_speed_template);
  793. test_cipher_speed("twofish", MODE_CBC, DECRYPT, sec, twofish_speed_template);
  794. break;
  795. case 203:
  796. test_cipher_speed("blowfish", MODE_ECB, ENCRYPT, sec, blowfish_speed_template);
  797. test_cipher_speed("blowfish", MODE_ECB, DECRYPT, sec, blowfish_speed_template);
  798. test_cipher_speed("blowfish", MODE_CBC, ENCRYPT, sec, blowfish_speed_template);
  799. test_cipher_speed("blowfish", MODE_CBC, DECRYPT, sec, blowfish_speed_template);
  800. break;
  801. case 204:
  802. test_cipher_speed("des", MODE_ECB, ENCRYPT, sec, des_speed_template);
  803. test_cipher_speed("des", MODE_ECB, DECRYPT, sec, des_speed_template);
  804. test_cipher_speed("des", MODE_CBC, ENCRYPT, sec, des_speed_template);
  805. test_cipher_speed("des", MODE_CBC, DECRYPT, sec, des_speed_template);
  806. break;
  807. case 1000:
  808. test_available();
  809. break;
  810. default:
  811. /* useful for debugging */
  812. printk("not testing anything\n");
  813. break;
  814. }
  815. }
  816. static int __init init(void)
  817. {
  818. tvmem = kmalloc(TVMEMSIZE, GFP_KERNEL);
  819. if (tvmem == NULL)
  820. return -ENOMEM;
  821. xbuf = kmalloc(XBUFSIZE, GFP_KERNEL);
  822. if (xbuf == NULL) {
  823. kfree(tvmem);
  824. return -ENOMEM;
  825. }
  826. do_test();
  827. kfree(xbuf);
  828. kfree(tvmem);
  829. return 0;
  830. }
  831. /*
  832. * If an init function is provided, an exit function must also be provided
  833. * to allow module unload.
  834. */
  835. static void __exit fini(void) { }
  836. module_init(init);
  837. module_exit(fini);
  838. module_param(mode, int, 0);
  839. module_param(sec, uint, 0);
  840. MODULE_PARM_DESC(sec, "Length in seconds of speed tests");
  841. MODULE_LICENSE("GPL");
  842. MODULE_DESCRIPTION("Quick & dirty crypto testing module");
  843. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");