tcrypt.c 24 KB

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