tcrypt.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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_testvec *template,
  356. unsigned int tcount, struct cipher_speed *speed)
  357. {
  358. unsigned int ret, i, j, iv_len;
  359. unsigned char *key, *p, iv[128];
  360. struct crypto_tfm *tfm;
  361. struct scatterlist sg[8];
  362. unsigned long start, bcount;
  363. const char *e, *m;
  364. if (enc == ENCRYPT)
  365. e = "encryption";
  366. else
  367. e = "decryption";
  368. if (mode == MODE_ECB)
  369. m = "ECB";
  370. else
  371. m = "CBC";
  372. printk("\ntesting speed of %s %s %s\n", algo, m, e);
  373. if (mode)
  374. tfm = crypto_alloc_tfm(algo, 0);
  375. else
  376. tfm = crypto_alloc_tfm(algo, CRYPTO_TFM_MODE_CBC);
  377. if (tfm == NULL) {
  378. printk("failed to load transform for %s %s\n", algo, m);
  379. return;
  380. }
  381. for (i = 0; speed[i].klen != 0; i++) {
  382. if ((speed[i].blen + speed[i].klen) > TVMEMSIZE) {
  383. printk("template (%u) too big for tvmem (%u)\n",
  384. speed[i].blen + speed[i].klen, TVMEMSIZE);
  385. goto out;
  386. }
  387. printk("test %u (%d bit key, %d byte blocks): ", i,
  388. speed[i].klen * 8, speed[i].blen);
  389. memset(tvmem, 0xff, speed[i].klen + speed[i].blen);
  390. /* set key, plain text and IV */
  391. key = (unsigned char *)tvmem;
  392. for (j = 0; j < tcount; j++) {
  393. if (template[j].klen == speed[i].klen) {
  394. key = template[j].key;
  395. break;
  396. }
  397. }
  398. p = (unsigned char *)tvmem + speed[i].klen;
  399. ret = crypto_cipher_setkey(tfm, key, speed[i].klen);
  400. if (ret) {
  401. printk("setkey() failed flags=%x\n", tfm->crt_flags);
  402. goto out;
  403. }
  404. if (!mode) {
  405. iv_len = crypto_tfm_alg_ivsize(tfm);
  406. memset(&iv, 0xff, iv_len);
  407. crypto_cipher_set_iv(tfm, iv, iv_len);
  408. }
  409. for (start = jiffies, bcount = 0;
  410. ((jiffies - start) / HZ) < sec; bcount++) {
  411. sg[0].page = virt_to_page(p);
  412. sg[0].offset = offset_in_page(p);
  413. sg[0].length = speed[i].blen;
  414. if (enc)
  415. ret = crypto_cipher_encrypt(tfm, sg, sg, speed[i].blen);
  416. else
  417. ret = crypto_cipher_decrypt(tfm, sg, sg, speed[i].blen);
  418. if (ret) {
  419. printk("%s () failed flags=%x\n", e, tfm->crt_flags);
  420. goto out;
  421. }
  422. }
  423. printk("%lu operations in %u seconds (%lu bytes)\n",
  424. bcount, sec, bcount * speed[i].blen);
  425. }
  426. out:
  427. crypto_free_tfm(tfm);
  428. }
  429. static void test_deflate(void)
  430. {
  431. unsigned int i;
  432. char result[COMP_BUF_SIZE];
  433. struct crypto_tfm *tfm;
  434. struct comp_testvec *tv;
  435. unsigned int tsize;
  436. printk("\ntesting deflate compression\n");
  437. tsize = sizeof (deflate_comp_tv_template);
  438. if (tsize > TVMEMSIZE) {
  439. printk("template (%u) too big for tvmem (%u)\n", tsize,
  440. TVMEMSIZE);
  441. return;
  442. }
  443. memcpy(tvmem, deflate_comp_tv_template, tsize);
  444. tv = (void *)tvmem;
  445. tfm = crypto_alloc_tfm("deflate", 0);
  446. if (tfm == NULL) {
  447. printk("failed to load transform for deflate\n");
  448. return;
  449. }
  450. for (i = 0; i < DEFLATE_COMP_TEST_VECTORS; i++) {
  451. int ilen, ret, dlen = COMP_BUF_SIZE;
  452. printk("test %u:\n", i + 1);
  453. memset(result, 0, sizeof (result));
  454. ilen = tv[i].inlen;
  455. ret = crypto_comp_compress(tfm, tv[i].input,
  456. ilen, result, &dlen);
  457. if (ret) {
  458. printk("fail: ret=%d\n", ret);
  459. continue;
  460. }
  461. hexdump(result, dlen);
  462. printk("%s (ratio %d:%d)\n",
  463. memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
  464. ilen, dlen);
  465. }
  466. printk("\ntesting deflate decompression\n");
  467. tsize = sizeof (deflate_decomp_tv_template);
  468. if (tsize > TVMEMSIZE) {
  469. printk("template (%u) too big for tvmem (%u)\n", tsize,
  470. TVMEMSIZE);
  471. goto out;
  472. }
  473. memcpy(tvmem, deflate_decomp_tv_template, tsize);
  474. tv = (void *)tvmem;
  475. for (i = 0; i < DEFLATE_DECOMP_TEST_VECTORS; i++) {
  476. int ilen, ret, dlen = COMP_BUF_SIZE;
  477. printk("test %u:\n", i + 1);
  478. memset(result, 0, sizeof (result));
  479. ilen = tv[i].inlen;
  480. ret = crypto_comp_decompress(tfm, tv[i].input,
  481. ilen, result, &dlen);
  482. if (ret) {
  483. printk("fail: ret=%d\n", ret);
  484. continue;
  485. }
  486. hexdump(result, dlen);
  487. printk("%s (ratio %d:%d)\n",
  488. memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
  489. ilen, dlen);
  490. }
  491. out:
  492. crypto_free_tfm(tfm);
  493. }
  494. static void test_crc32c(void)
  495. {
  496. #define NUMVEC 6
  497. #define VECSIZE 40
  498. int i, j, pass;
  499. u32 crc;
  500. u8 b, test_vec[NUMVEC][VECSIZE];
  501. static u32 vec_results[NUMVEC] = {
  502. 0x0e2c157f, 0xe980ebf6, 0xde74bded,
  503. 0xd579c862, 0xba979ad0, 0x2b29d913
  504. };
  505. static u32 tot_vec_results = 0x24c5d375;
  506. struct scatterlist sg[NUMVEC];
  507. struct crypto_tfm *tfm;
  508. char *fmtdata = "testing crc32c initialized to %08x: %s\n";
  509. #define SEEDTESTVAL 0xedcba987
  510. u32 seed;
  511. printk("\ntesting crc32c\n");
  512. tfm = crypto_alloc_tfm("crc32c", 0);
  513. if (tfm == NULL) {
  514. printk("failed to load transform for crc32c\n");
  515. return;
  516. }
  517. crypto_digest_init(tfm);
  518. crypto_digest_final(tfm, (u8*)&crc);
  519. printk(fmtdata, crc, (crc == 0) ? "pass" : "ERROR");
  520. /*
  521. * stuff test_vec with known values, simple incrementing
  522. * byte values.
  523. */
  524. b = 0;
  525. for (i = 0; i < NUMVEC; i++) {
  526. for (j = 0; j < VECSIZE; j++)
  527. test_vec[i][j] = ++b;
  528. sg[i].page = virt_to_page(test_vec[i]);
  529. sg[i].offset = offset_in_page(test_vec[i]);
  530. sg[i].length = VECSIZE;
  531. }
  532. seed = SEEDTESTVAL;
  533. (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32));
  534. crypto_digest_final(tfm, (u8*)&crc);
  535. printk("testing crc32c setkey returns %08x : %s\n", crc, (crc == (SEEDTESTVAL ^ ~(u32)0)) ?
  536. "pass" : "ERROR");
  537. printk("testing crc32c using update/final:\n");
  538. pass = 1; /* assume all is well */
  539. for (i = 0; i < NUMVEC; i++) {
  540. seed = ~(u32)0;
  541. (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32));
  542. crypto_digest_update(tfm, &sg[i], 1);
  543. crypto_digest_final(tfm, (u8*)&crc);
  544. if (crc == vec_results[i]) {
  545. printk(" %08x:OK", crc);
  546. } else {
  547. printk(" %08x:BAD, wanted %08x\n", crc, vec_results[i]);
  548. pass = 0;
  549. }
  550. }
  551. printk("\ntesting crc32c using incremental accumulator:\n");
  552. crc = 0;
  553. for (i = 0; i < NUMVEC; i++) {
  554. seed = (crc ^ ~(u32)0);
  555. (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32));
  556. crypto_digest_update(tfm, &sg[i], 1);
  557. crypto_digest_final(tfm, (u8*)&crc);
  558. }
  559. if (crc == tot_vec_results) {
  560. printk(" %08x:OK", crc);
  561. } else {
  562. printk(" %08x:BAD, wanted %08x\n", crc, tot_vec_results);
  563. pass = 0;
  564. }
  565. printk("\ntesting crc32c using digest:\n");
  566. seed = ~(u32)0;
  567. (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32));
  568. crypto_digest_digest(tfm, sg, NUMVEC, (u8*)&crc);
  569. if (crc == tot_vec_results) {
  570. printk(" %08x:OK", crc);
  571. } else {
  572. printk(" %08x:BAD, wanted %08x\n", crc, tot_vec_results);
  573. pass = 0;
  574. }
  575. printk("\n%s\n", pass ? "pass" : "ERROR");
  576. crypto_free_tfm(tfm);
  577. printk("crc32c test complete\n");
  578. }
  579. static void test_available(void)
  580. {
  581. char **name = check;
  582. while (*name) {
  583. printk("alg %s ", *name);
  584. printk((crypto_alg_available(*name, 0)) ?
  585. "found\n" : "not found\n");
  586. name++;
  587. }
  588. }
  589. static void do_test(void)
  590. {
  591. switch (mode) {
  592. case 0:
  593. test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
  594. test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
  595. //DES
  596. test_cipher ("des", MODE_ECB, ENCRYPT, des_enc_tv_template, DES_ENC_TEST_VECTORS);
  597. test_cipher ("des", MODE_ECB, DECRYPT, des_dec_tv_template, DES_DEC_TEST_VECTORS);
  598. test_cipher ("des", MODE_CBC, ENCRYPT, des_cbc_enc_tv_template, DES_CBC_ENC_TEST_VECTORS);
  599. test_cipher ("des", MODE_CBC, DECRYPT, des_cbc_dec_tv_template, DES_CBC_DEC_TEST_VECTORS);
  600. //DES3_EDE
  601. test_cipher ("des3_ede", MODE_ECB, ENCRYPT, des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS);
  602. test_cipher ("des3_ede", MODE_ECB, DECRYPT, des3_ede_dec_tv_template, DES3_EDE_DEC_TEST_VECTORS);
  603. test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
  604. test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
  605. //BLOWFISH
  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. //TWOFISH
  611. test_cipher ("twofish", MODE_ECB, ENCRYPT, tf_enc_tv_template, TF_ENC_TEST_VECTORS);
  612. test_cipher ("twofish", MODE_ECB, DECRYPT, tf_dec_tv_template, TF_DEC_TEST_VECTORS);
  613. test_cipher ("twofish", MODE_CBC, ENCRYPT, tf_cbc_enc_tv_template, TF_CBC_ENC_TEST_VECTORS);
  614. test_cipher ("twofish", MODE_CBC, DECRYPT, tf_cbc_dec_tv_template, TF_CBC_DEC_TEST_VECTORS);
  615. //SERPENT
  616. test_cipher ("serpent", MODE_ECB, ENCRYPT, serpent_enc_tv_template, SERPENT_ENC_TEST_VECTORS);
  617. test_cipher ("serpent", MODE_ECB, DECRYPT, serpent_dec_tv_template, SERPENT_DEC_TEST_VECTORS);
  618. //TNEPRES
  619. test_cipher ("tnepres", MODE_ECB, ENCRYPT, tnepres_enc_tv_template, TNEPRES_ENC_TEST_VECTORS);
  620. test_cipher ("tnepres", MODE_ECB, DECRYPT, tnepres_dec_tv_template, TNEPRES_DEC_TEST_VECTORS);
  621. //AES
  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. //CAST5
  625. test_cipher ("cast5", MODE_ECB, ENCRYPT, cast5_enc_tv_template, CAST5_ENC_TEST_VECTORS);
  626. test_cipher ("cast5", MODE_ECB, DECRYPT, cast5_dec_tv_template, CAST5_DEC_TEST_VECTORS);
  627. //CAST6
  628. test_cipher ("cast6", MODE_ECB, ENCRYPT, cast6_enc_tv_template, CAST6_ENC_TEST_VECTORS);
  629. test_cipher ("cast6", MODE_ECB, DECRYPT, cast6_dec_tv_template, CAST6_DEC_TEST_VECTORS);
  630. //ARC4
  631. test_cipher ("arc4", MODE_ECB, ENCRYPT, arc4_enc_tv_template, ARC4_ENC_TEST_VECTORS);
  632. test_cipher ("arc4", MODE_ECB, DECRYPT, arc4_dec_tv_template, ARC4_DEC_TEST_VECTORS);
  633. //TEA
  634. test_cipher ("tea", MODE_ECB, ENCRYPT, tea_enc_tv_template, TEA_ENC_TEST_VECTORS);
  635. test_cipher ("tea", MODE_ECB, DECRYPT, tea_dec_tv_template, TEA_DEC_TEST_VECTORS);
  636. //XTEA
  637. test_cipher ("xtea", MODE_ECB, ENCRYPT, xtea_enc_tv_template, XTEA_ENC_TEST_VECTORS);
  638. test_cipher ("xtea", MODE_ECB, DECRYPT, xtea_dec_tv_template, XTEA_DEC_TEST_VECTORS);
  639. //KHAZAD
  640. test_cipher ("khazad", MODE_ECB, ENCRYPT, khazad_enc_tv_template, KHAZAD_ENC_TEST_VECTORS);
  641. test_cipher ("khazad", MODE_ECB, DECRYPT, khazad_dec_tv_template, KHAZAD_DEC_TEST_VECTORS);
  642. //ANUBIS
  643. test_cipher ("anubis", MODE_ECB, ENCRYPT, anubis_enc_tv_template, ANUBIS_ENC_TEST_VECTORS);
  644. test_cipher ("anubis", MODE_ECB, DECRYPT, anubis_dec_tv_template, ANUBIS_DEC_TEST_VECTORS);
  645. test_cipher ("anubis", MODE_CBC, ENCRYPT, anubis_cbc_enc_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
  646. test_cipher ("anubis", MODE_CBC, DECRYPT, anubis_cbc_dec_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
  647. test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
  648. test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
  649. test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
  650. test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
  651. test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
  652. test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
  653. test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
  654. test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
  655. test_deflate();
  656. test_crc32c();
  657. #ifdef CONFIG_CRYPTO_HMAC
  658. test_hmac("md5", hmac_md5_tv_template, HMAC_MD5_TEST_VECTORS);
  659. test_hmac("sha1", hmac_sha1_tv_template, HMAC_SHA1_TEST_VECTORS);
  660. test_hmac("sha256", hmac_sha256_tv_template, HMAC_SHA256_TEST_VECTORS);
  661. #endif
  662. test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
  663. break;
  664. case 1:
  665. test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
  666. break;
  667. case 2:
  668. test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
  669. break;
  670. case 3:
  671. test_cipher ("des", MODE_ECB, ENCRYPT, des_enc_tv_template, DES_ENC_TEST_VECTORS);
  672. test_cipher ("des", MODE_ECB, DECRYPT, des_dec_tv_template, DES_DEC_TEST_VECTORS);
  673. test_cipher ("des", MODE_CBC, ENCRYPT, des_cbc_enc_tv_template, DES_CBC_ENC_TEST_VECTORS);
  674. test_cipher ("des", MODE_CBC, DECRYPT, des_cbc_dec_tv_template, DES_CBC_DEC_TEST_VECTORS);
  675. break;
  676. case 4:
  677. test_cipher ("des3_ede", MODE_ECB, ENCRYPT, des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS);
  678. test_cipher ("des3_ede", MODE_ECB, DECRYPT, des3_ede_dec_tv_template, DES3_EDE_DEC_TEST_VECTORS);
  679. break;
  680. case 5:
  681. test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
  682. break;
  683. case 6:
  684. test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
  685. break;
  686. case 7:
  687. test_cipher ("blowfish", MODE_ECB, ENCRYPT, bf_enc_tv_template, BF_ENC_TEST_VECTORS);
  688. test_cipher ("blowfish", MODE_ECB, DECRYPT, bf_dec_tv_template, BF_DEC_TEST_VECTORS);
  689. test_cipher ("blowfish", MODE_CBC, ENCRYPT, bf_cbc_enc_tv_template, BF_CBC_ENC_TEST_VECTORS);
  690. test_cipher ("blowfish", MODE_CBC, DECRYPT, bf_cbc_dec_tv_template, BF_CBC_DEC_TEST_VECTORS);
  691. break;
  692. case 8:
  693. test_cipher ("twofish", MODE_ECB, ENCRYPT, tf_enc_tv_template, TF_ENC_TEST_VECTORS);
  694. test_cipher ("twofish", MODE_ECB, DECRYPT, tf_dec_tv_template, TF_DEC_TEST_VECTORS);
  695. test_cipher ("twofish", MODE_CBC, ENCRYPT, tf_cbc_enc_tv_template, TF_CBC_ENC_TEST_VECTORS);
  696. test_cipher ("twofish", MODE_CBC, DECRYPT, tf_cbc_dec_tv_template, TF_CBC_DEC_TEST_VECTORS);
  697. break;
  698. case 9:
  699. test_cipher ("serpent", MODE_ECB, ENCRYPT, serpent_enc_tv_template, SERPENT_ENC_TEST_VECTORS);
  700. test_cipher ("serpent", MODE_ECB, DECRYPT, serpent_dec_tv_template, SERPENT_DEC_TEST_VECTORS);
  701. break;
  702. case 10:
  703. test_cipher ("aes", MODE_ECB, ENCRYPT, aes_enc_tv_template, AES_ENC_TEST_VECTORS);
  704. test_cipher ("aes", MODE_ECB, DECRYPT, aes_dec_tv_template, AES_DEC_TEST_VECTORS);
  705. break;
  706. case 11:
  707. test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
  708. break;
  709. case 12:
  710. test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
  711. break;
  712. case 13:
  713. test_deflate();
  714. break;
  715. case 14:
  716. test_cipher ("cast5", MODE_ECB, ENCRYPT, cast5_enc_tv_template, CAST5_ENC_TEST_VECTORS);
  717. test_cipher ("cast5", MODE_ECB, DECRYPT, cast5_dec_tv_template, CAST5_DEC_TEST_VECTORS);
  718. break;
  719. case 15:
  720. test_cipher ("cast6", MODE_ECB, ENCRYPT, cast6_enc_tv_template, CAST6_ENC_TEST_VECTORS);
  721. test_cipher ("cast6", MODE_ECB, DECRYPT, cast6_dec_tv_template, CAST6_DEC_TEST_VECTORS);
  722. break;
  723. case 16:
  724. test_cipher ("arc4", MODE_ECB, ENCRYPT, arc4_enc_tv_template, ARC4_ENC_TEST_VECTORS);
  725. test_cipher ("arc4", MODE_ECB, DECRYPT, arc4_dec_tv_template, ARC4_DEC_TEST_VECTORS);
  726. break;
  727. case 17:
  728. test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
  729. break;
  730. case 18:
  731. test_crc32c();
  732. break;
  733. case 19:
  734. test_cipher ("tea", MODE_ECB, ENCRYPT, tea_enc_tv_template, TEA_ENC_TEST_VECTORS);
  735. test_cipher ("tea", MODE_ECB, DECRYPT, tea_dec_tv_template, TEA_DEC_TEST_VECTORS);
  736. break;
  737. case 20:
  738. test_cipher ("xtea", MODE_ECB, ENCRYPT, xtea_enc_tv_template, XTEA_ENC_TEST_VECTORS);
  739. test_cipher ("xtea", MODE_ECB, DECRYPT, xtea_dec_tv_template, XTEA_DEC_TEST_VECTORS);
  740. break;
  741. case 21:
  742. test_cipher ("khazad", MODE_ECB, ENCRYPT, khazad_enc_tv_template, KHAZAD_ENC_TEST_VECTORS);
  743. test_cipher ("khazad", MODE_ECB, DECRYPT, khazad_dec_tv_template, KHAZAD_DEC_TEST_VECTORS);
  744. break;
  745. case 22:
  746. test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
  747. break;
  748. case 23:
  749. test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
  750. break;
  751. case 24:
  752. test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
  753. break;
  754. case 25:
  755. test_cipher ("tnepres", MODE_ECB, ENCRYPT, tnepres_enc_tv_template, TNEPRES_ENC_TEST_VECTORS);
  756. test_cipher ("tnepres", MODE_ECB, DECRYPT, tnepres_dec_tv_template, TNEPRES_DEC_TEST_VECTORS);
  757. break;
  758. case 26:
  759. test_cipher ("anubis", MODE_ECB, ENCRYPT, anubis_enc_tv_template, ANUBIS_ENC_TEST_VECTORS);
  760. test_cipher ("anubis", MODE_ECB, DECRYPT, anubis_dec_tv_template, ANUBIS_DEC_TEST_VECTORS);
  761. test_cipher ("anubis", MODE_CBC, ENCRYPT, anubis_cbc_enc_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
  762. test_cipher ("anubis", MODE_CBC, DECRYPT, anubis_cbc_dec_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
  763. break;
  764. case 27:
  765. test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
  766. break;
  767. case 28:
  768. test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
  769. break;
  770. case 29:
  771. test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
  772. break;
  773. #ifdef CONFIG_CRYPTO_HMAC
  774. case 100:
  775. test_hmac("md5", hmac_md5_tv_template, HMAC_MD5_TEST_VECTORS);
  776. break;
  777. case 101:
  778. test_hmac("sha1", hmac_sha1_tv_template, HMAC_SHA1_TEST_VECTORS);
  779. break;
  780. case 102:
  781. test_hmac("sha256", hmac_sha256_tv_template, HMAC_SHA256_TEST_VECTORS);
  782. break;
  783. #endif
  784. case 200:
  785. test_cipher_speed("aes", MODE_ECB, ENCRYPT, sec, NULL, 0,
  786. aes_speed_template);
  787. test_cipher_speed("aes", MODE_ECB, DECRYPT, sec, NULL, 0,
  788. aes_speed_template);
  789. test_cipher_speed("aes", MODE_CBC, ENCRYPT, sec, NULL, 0,
  790. aes_speed_template);
  791. test_cipher_speed("aes", MODE_CBC, DECRYPT, sec, NULL, 0,
  792. aes_speed_template);
  793. break;
  794. case 201:
  795. test_cipher_speed("des3_ede", MODE_ECB, ENCRYPT, sec,
  796. des3_ede_enc_tv_template,
  797. DES3_EDE_ENC_TEST_VECTORS,
  798. des3_ede_speed_template);
  799. test_cipher_speed("des3_ede", MODE_ECB, DECRYPT, sec,
  800. des3_ede_dec_tv_template,
  801. DES3_EDE_DEC_TEST_VECTORS,
  802. des3_ede_speed_template);
  803. test_cipher_speed("des3_ede", MODE_CBC, ENCRYPT, sec,
  804. des3_ede_enc_tv_template,
  805. DES3_EDE_ENC_TEST_VECTORS,
  806. des3_ede_speed_template);
  807. test_cipher_speed("des3_ede", MODE_CBC, DECRYPT, sec,
  808. des3_ede_dec_tv_template,
  809. DES3_EDE_DEC_TEST_VECTORS,
  810. des3_ede_speed_template);
  811. break;
  812. case 202:
  813. test_cipher_speed("twofish", MODE_ECB, ENCRYPT, sec, NULL, 0,
  814. twofish_speed_template);
  815. test_cipher_speed("twofish", MODE_ECB, DECRYPT, sec, NULL, 0,
  816. twofish_speed_template);
  817. test_cipher_speed("twofish", MODE_CBC, ENCRYPT, sec, NULL, 0,
  818. twofish_speed_template);
  819. test_cipher_speed("twofish", MODE_CBC, DECRYPT, sec, NULL, 0,
  820. twofish_speed_template);
  821. break;
  822. case 203:
  823. test_cipher_speed("blowfish", MODE_ECB, ENCRYPT, sec, NULL, 0,
  824. blowfish_speed_template);
  825. test_cipher_speed("blowfish", MODE_ECB, DECRYPT, sec, NULL, 0,
  826. blowfish_speed_template);
  827. test_cipher_speed("blowfish", MODE_CBC, ENCRYPT, sec, NULL, 0,
  828. blowfish_speed_template);
  829. test_cipher_speed("blowfish", MODE_CBC, DECRYPT, sec, NULL, 0,
  830. blowfish_speed_template);
  831. break;
  832. case 204:
  833. test_cipher_speed("des", MODE_ECB, ENCRYPT, sec, NULL, 0,
  834. des_speed_template);
  835. test_cipher_speed("des", MODE_ECB, DECRYPT, sec, NULL, 0,
  836. des_speed_template);
  837. test_cipher_speed("des", MODE_CBC, ENCRYPT, sec, NULL, 0,
  838. des_speed_template);
  839. test_cipher_speed("des", MODE_CBC, DECRYPT, sec, NULL, 0,
  840. des_speed_template);
  841. break;
  842. case 1000:
  843. test_available();
  844. break;
  845. default:
  846. /* useful for debugging */
  847. printk("not testing anything\n");
  848. break;
  849. }
  850. }
  851. static int __init init(void)
  852. {
  853. tvmem = kmalloc(TVMEMSIZE, GFP_KERNEL);
  854. if (tvmem == NULL)
  855. return -ENOMEM;
  856. xbuf = kmalloc(XBUFSIZE, GFP_KERNEL);
  857. if (xbuf == NULL) {
  858. kfree(tvmem);
  859. return -ENOMEM;
  860. }
  861. do_test();
  862. kfree(xbuf);
  863. kfree(tvmem);
  864. return 0;
  865. }
  866. /*
  867. * If an init function is provided, an exit function must also be provided
  868. * to allow module unload.
  869. */
  870. static void __exit fini(void) { }
  871. module_init(init);
  872. module_exit(fini);
  873. module_param(mode, int, 0);
  874. module_param(sec, uint, 0);
  875. MODULE_PARM_DESC(sec, "Length in seconds of speed tests");
  876. MODULE_LICENSE("GPL");
  877. MODULE_DESCRIPTION("Quick & dirty crypto testing module");
  878. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");