tcrypt.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784
  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. * Copyright (c) 2007 Nokia Siemens Networks
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the Free
  13. * Software Foundation; either version 2 of the License, or (at your option)
  14. * any later version.
  15. *
  16. * 2007-11-13 Added GCM tests
  17. * 2007-11-13 Added AEAD support
  18. * 2007-11-06 Added SHA-224 and SHA-224-HMAC tests
  19. * 2006-12-07 Added SHA384 HMAC and SHA512 HMAC tests
  20. * 2004-08-09 Added cipher speed tests (Reyk Floeter <reyk@vantronix.net>)
  21. * 2003-09-14 Rewritten by Kartikey Mahendra Bhatt
  22. *
  23. */
  24. #include <linux/err.h>
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/mm.h>
  28. #include <linux/slab.h>
  29. #include <linux/scatterlist.h>
  30. #include <linux/string.h>
  31. #include <linux/crypto.h>
  32. #include <linux/highmem.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/jiffies.h>
  35. #include <linux/timex.h>
  36. #include <linux/interrupt.h>
  37. #include "tcrypt.h"
  38. /*
  39. * Need to kmalloc() memory for testing kmap().
  40. */
  41. #define TVMEMSIZE 16384
  42. #define XBUFSIZE 32768
  43. /*
  44. * Indexes into the xbuf to simulate cross-page access.
  45. */
  46. #define IDX1 37
  47. #define IDX2 32400
  48. #define IDX3 1
  49. #define IDX4 8193
  50. #define IDX5 22222
  51. #define IDX6 17101
  52. #define IDX7 27333
  53. #define IDX8 3000
  54. /*
  55. * Used by test_cipher()
  56. */
  57. #define ENCRYPT 1
  58. #define DECRYPT 0
  59. struct tcrypt_result {
  60. struct completion completion;
  61. int err;
  62. };
  63. static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
  64. /*
  65. * Used by test_cipher_speed()
  66. */
  67. static unsigned int sec;
  68. static int mode;
  69. static char *xbuf;
  70. static char *axbuf;
  71. static char *tvmem;
  72. static char *check[] = {
  73. "des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
  74. "blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
  75. "cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
  76. "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
  77. "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", "fcrypt",
  78. "camellia", "seed", "salsa20", NULL
  79. };
  80. static void hexdump(unsigned char *buf, unsigned int len)
  81. {
  82. print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
  83. 16, 1,
  84. buf, len, false);
  85. }
  86. static void tcrypt_complete(struct crypto_async_request *req, int err)
  87. {
  88. struct tcrypt_result *res = req->data;
  89. if (err == -EINPROGRESS)
  90. return;
  91. res->err = err;
  92. complete(&res->completion);
  93. }
  94. static void test_hash(char *algo, struct hash_testvec *template,
  95. unsigned int tcount)
  96. {
  97. unsigned int i, j, k, temp;
  98. struct scatterlist sg[8];
  99. char result[64];
  100. struct crypto_hash *tfm;
  101. struct hash_desc desc;
  102. struct hash_testvec *hash_tv;
  103. unsigned int tsize;
  104. int ret;
  105. printk("\ntesting %s\n", algo);
  106. tsize = sizeof(struct hash_testvec);
  107. tsize *= tcount;
  108. if (tsize > TVMEMSIZE) {
  109. printk("template (%u) too big for tvmem (%u)\n", tsize, TVMEMSIZE);
  110. return;
  111. }
  112. memcpy(tvmem, template, tsize);
  113. hash_tv = (void *)tvmem;
  114. tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
  115. if (IS_ERR(tfm)) {
  116. printk("failed to load transform for %s: %ld\n", algo,
  117. PTR_ERR(tfm));
  118. return;
  119. }
  120. desc.tfm = tfm;
  121. desc.flags = 0;
  122. for (i = 0; i < tcount; i++) {
  123. printk("test %u:\n", i + 1);
  124. memset(result, 0, 64);
  125. sg_init_one(&sg[0], hash_tv[i].plaintext, hash_tv[i].psize);
  126. if (hash_tv[i].ksize) {
  127. ret = crypto_hash_setkey(tfm, hash_tv[i].key,
  128. hash_tv[i].ksize);
  129. if (ret) {
  130. printk("setkey() failed ret=%d\n", ret);
  131. goto out;
  132. }
  133. }
  134. ret = crypto_hash_digest(&desc, sg, hash_tv[i].psize, result);
  135. if (ret) {
  136. printk("digest () failed ret=%d\n", ret);
  137. goto out;
  138. }
  139. hexdump(result, crypto_hash_digestsize(tfm));
  140. printk("%s\n",
  141. memcmp(result, hash_tv[i].digest,
  142. crypto_hash_digestsize(tfm)) ?
  143. "fail" : "pass");
  144. }
  145. printk("testing %s across pages\n", algo);
  146. /* setup the dummy buffer first */
  147. memset(xbuf, 0, XBUFSIZE);
  148. memset(axbuf, 0, XBUFSIZE);
  149. j = 0;
  150. for (i = 0; i < tcount; i++) {
  151. if (hash_tv[i].np) {
  152. j++;
  153. printk("test %u:\n", j);
  154. memset(result, 0, 64);
  155. temp = 0;
  156. sg_init_table(sg, hash_tv[i].np);
  157. for (k = 0; k < hash_tv[i].np; k++) {
  158. memcpy(&xbuf[IDX[k]],
  159. hash_tv[i].plaintext + temp,
  160. hash_tv[i].tap[k]);
  161. temp += hash_tv[i].tap[k];
  162. sg_set_buf(&sg[k], &xbuf[IDX[k]],
  163. hash_tv[i].tap[k]);
  164. }
  165. if (hash_tv[i].ksize) {
  166. ret = crypto_hash_setkey(tfm, hash_tv[i].key,
  167. hash_tv[i].ksize);
  168. if (ret) {
  169. printk("setkey() failed ret=%d\n", ret);
  170. goto out;
  171. }
  172. }
  173. ret = crypto_hash_digest(&desc, sg, hash_tv[i].psize,
  174. result);
  175. if (ret) {
  176. printk("digest () failed ret=%d\n", ret);
  177. goto out;
  178. }
  179. hexdump(result, crypto_hash_digestsize(tfm));
  180. printk("%s\n",
  181. memcmp(result, hash_tv[i].digest,
  182. crypto_hash_digestsize(tfm)) ?
  183. "fail" : "pass");
  184. }
  185. }
  186. out:
  187. crypto_free_hash(tfm);
  188. }
  189. static void test_aead(char *algo, int enc, struct aead_testvec *template,
  190. unsigned int tcount)
  191. {
  192. unsigned int ret, i, j, k, temp;
  193. unsigned int tsize;
  194. char *q;
  195. struct crypto_aead *tfm;
  196. char *key;
  197. struct aead_testvec *aead_tv;
  198. struct aead_request *req;
  199. struct scatterlist sg[8];
  200. struct scatterlist asg[8];
  201. const char *e;
  202. struct tcrypt_result result;
  203. unsigned int authsize;
  204. if (enc == ENCRYPT)
  205. e = "encryption";
  206. else
  207. e = "decryption";
  208. printk(KERN_INFO "\ntesting %s %s\n", algo, e);
  209. tsize = sizeof(struct aead_testvec);
  210. tsize *= tcount;
  211. if (tsize > TVMEMSIZE) {
  212. printk(KERN_INFO "template (%u) too big for tvmem (%u)\n",
  213. tsize, TVMEMSIZE);
  214. return;
  215. }
  216. memcpy(tvmem, template, tsize);
  217. aead_tv = (void *)tvmem;
  218. init_completion(&result.completion);
  219. tfm = crypto_alloc_aead(algo, 0, 0);
  220. if (IS_ERR(tfm)) {
  221. printk(KERN_INFO "failed to load transform for %s: %ld\n",
  222. algo, PTR_ERR(tfm));
  223. return;
  224. }
  225. authsize = crypto_aead_authsize(tfm);
  226. req = aead_request_alloc(tfm, GFP_KERNEL);
  227. if (!req) {
  228. printk(KERN_INFO "failed to allocate request for %s\n", algo);
  229. goto out;
  230. }
  231. aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  232. tcrypt_complete, &result);
  233. for (i = 0, j = 0; i < tcount; i++) {
  234. if (!aead_tv[i].np) {
  235. printk(KERN_INFO "test %u (%d bit key):\n",
  236. ++j, aead_tv[i].klen * 8);
  237. crypto_aead_clear_flags(tfm, ~0);
  238. if (aead_tv[i].wk)
  239. crypto_aead_set_flags(
  240. tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  241. key = aead_tv[i].key;
  242. ret = crypto_aead_setkey(tfm, key,
  243. aead_tv[i].klen);
  244. if (ret) {
  245. printk(KERN_INFO "setkey() failed flags=%x\n",
  246. crypto_aead_get_flags(tfm));
  247. if (!aead_tv[i].fail)
  248. goto out;
  249. }
  250. sg_init_one(&sg[0], aead_tv[i].input,
  251. aead_tv[i].ilen + (enc ? authsize : 0));
  252. sg_init_one(&asg[0], aead_tv[i].assoc,
  253. aead_tv[i].alen);
  254. aead_request_set_crypt(req, sg, sg,
  255. aead_tv[i].ilen,
  256. aead_tv[i].iv);
  257. aead_request_set_assoc(req, asg, aead_tv[i].alen);
  258. ret = enc ?
  259. crypto_aead_encrypt(req) :
  260. crypto_aead_decrypt(req);
  261. switch (ret) {
  262. case 0:
  263. break;
  264. case -EINPROGRESS:
  265. case -EBUSY:
  266. ret = wait_for_completion_interruptible(
  267. &result.completion);
  268. if (!ret && !(ret = result.err)) {
  269. INIT_COMPLETION(result.completion);
  270. break;
  271. }
  272. /* fall through */
  273. default:
  274. printk(KERN_INFO "%s () failed err=%d\n",
  275. e, -ret);
  276. goto out;
  277. }
  278. q = kmap(sg_page(&sg[0])) + sg[0].offset;
  279. hexdump(q, aead_tv[i].rlen);
  280. printk(KERN_INFO "enc/dec: %s\n",
  281. memcmp(q, aead_tv[i].result,
  282. aead_tv[i].rlen) ? "fail" : "pass");
  283. }
  284. }
  285. printk(KERN_INFO "\ntesting %s %s across pages (chunking)\n", algo, e);
  286. memset(xbuf, 0, XBUFSIZE);
  287. for (i = 0, j = 0; i < tcount; i++) {
  288. if (aead_tv[i].np) {
  289. printk(KERN_INFO "test %u (%d bit key):\n",
  290. ++j, aead_tv[i].klen * 8);
  291. crypto_aead_clear_flags(tfm, ~0);
  292. if (aead_tv[i].wk)
  293. crypto_aead_set_flags(
  294. tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  295. key = aead_tv[i].key;
  296. ret = crypto_aead_setkey(tfm, key, aead_tv[i].klen);
  297. if (ret) {
  298. printk(KERN_INFO "setkey() failed flags=%x\n",
  299. crypto_aead_get_flags(tfm));
  300. if (!aead_tv[i].fail)
  301. goto out;
  302. }
  303. sg_init_table(sg, aead_tv[i].np);
  304. for (k = 0, temp = 0; k < aead_tv[i].np; k++) {
  305. memcpy(&xbuf[IDX[k]],
  306. aead_tv[i].input + temp,
  307. aead_tv[i].tap[k]);
  308. temp += aead_tv[i].tap[k];
  309. sg_set_buf(&sg[k], &xbuf[IDX[k]],
  310. aead_tv[i].tap[k]);
  311. }
  312. if (enc)
  313. sg[k - 1].length += authsize;
  314. sg_init_table(asg, aead_tv[i].anp);
  315. for (k = 0, temp = 0; k < aead_tv[i].anp; k++) {
  316. memcpy(&axbuf[IDX[k]],
  317. aead_tv[i].assoc + temp,
  318. aead_tv[i].atap[k]);
  319. temp += aead_tv[i].atap[k];
  320. sg_set_buf(&asg[k], &axbuf[IDX[k]],
  321. aead_tv[i].atap[k]);
  322. }
  323. aead_request_set_crypt(req, sg, sg,
  324. aead_tv[i].ilen,
  325. aead_tv[i].iv);
  326. aead_request_set_assoc(req, asg, aead_tv[i].alen);
  327. ret = enc ?
  328. crypto_aead_encrypt(req) :
  329. crypto_aead_decrypt(req);
  330. switch (ret) {
  331. case 0:
  332. break;
  333. case -EINPROGRESS:
  334. case -EBUSY:
  335. ret = wait_for_completion_interruptible(
  336. &result.completion);
  337. if (!ret && !(ret = result.err)) {
  338. INIT_COMPLETION(result.completion);
  339. break;
  340. }
  341. /* fall through */
  342. default:
  343. printk(KERN_INFO "%s () failed err=%d\n",
  344. e, -ret);
  345. goto out;
  346. }
  347. for (k = 0, temp = 0; k < aead_tv[i].np; k++) {
  348. printk(KERN_INFO "page %u\n", k);
  349. q = kmap(sg_page(&sg[k])) + sg[k].offset;
  350. hexdump(q, aead_tv[i].tap[k]);
  351. printk(KERN_INFO "%s\n",
  352. memcmp(q, aead_tv[i].result + temp,
  353. aead_tv[i].tap[k] -
  354. (k < aead_tv[i].np - 1 || enc ?
  355. 0 : authsize)) ?
  356. "fail" : "pass");
  357. temp += aead_tv[i].tap[k];
  358. }
  359. }
  360. }
  361. out:
  362. crypto_free_aead(tfm);
  363. aead_request_free(req);
  364. }
  365. static void test_cipher(char *algo, int enc,
  366. struct cipher_testvec *template, unsigned int tcount)
  367. {
  368. unsigned int ret, i, j, k, temp;
  369. unsigned int tsize;
  370. char *q;
  371. struct crypto_ablkcipher *tfm;
  372. char *key;
  373. struct cipher_testvec *cipher_tv;
  374. struct ablkcipher_request *req;
  375. struct scatterlist sg[8];
  376. const char *e;
  377. struct tcrypt_result result;
  378. if (enc == ENCRYPT)
  379. e = "encryption";
  380. else
  381. e = "decryption";
  382. printk("\ntesting %s %s\n", algo, e);
  383. tsize = sizeof (struct cipher_testvec);
  384. if (tsize > TVMEMSIZE) {
  385. printk("template (%u) too big for tvmem (%u)\n", tsize,
  386. TVMEMSIZE);
  387. return;
  388. }
  389. cipher_tv = (void *)tvmem;
  390. init_completion(&result.completion);
  391. tfm = crypto_alloc_ablkcipher(algo, 0, 0);
  392. if (IS_ERR(tfm)) {
  393. printk("failed to load transform for %s: %ld\n", algo,
  394. PTR_ERR(tfm));
  395. return;
  396. }
  397. req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
  398. if (!req) {
  399. printk("failed to allocate request for %s\n", algo);
  400. goto out;
  401. }
  402. ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  403. tcrypt_complete, &result);
  404. j = 0;
  405. for (i = 0; i < tcount; i++) {
  406. memcpy(cipher_tv, &template[i], tsize);
  407. if (!(cipher_tv->np)) {
  408. j++;
  409. printk("test %u (%d bit key):\n",
  410. j, cipher_tv->klen * 8);
  411. crypto_ablkcipher_clear_flags(tfm, ~0);
  412. if (cipher_tv->wk)
  413. crypto_ablkcipher_set_flags(
  414. tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  415. key = cipher_tv->key;
  416. ret = crypto_ablkcipher_setkey(tfm, key,
  417. cipher_tv->klen);
  418. if (ret) {
  419. printk("setkey() failed flags=%x\n",
  420. crypto_ablkcipher_get_flags(tfm));
  421. if (!cipher_tv->fail)
  422. goto out;
  423. }
  424. sg_init_one(&sg[0], cipher_tv->input,
  425. cipher_tv->ilen);
  426. ablkcipher_request_set_crypt(req, sg, sg,
  427. cipher_tv->ilen,
  428. cipher_tv->iv);
  429. ret = enc ?
  430. crypto_ablkcipher_encrypt(req) :
  431. crypto_ablkcipher_decrypt(req);
  432. switch (ret) {
  433. case 0:
  434. break;
  435. case -EINPROGRESS:
  436. case -EBUSY:
  437. ret = wait_for_completion_interruptible(
  438. &result.completion);
  439. if (!ret && !((ret = result.err))) {
  440. INIT_COMPLETION(result.completion);
  441. break;
  442. }
  443. /* fall through */
  444. default:
  445. printk("%s () failed err=%d\n", e, -ret);
  446. goto out;
  447. }
  448. q = kmap(sg_page(&sg[0])) + sg[0].offset;
  449. hexdump(q, cipher_tv->rlen);
  450. printk("%s\n",
  451. memcmp(q, cipher_tv->result,
  452. cipher_tv->rlen) ? "fail" : "pass");
  453. }
  454. }
  455. printk("\ntesting %s %s across pages (chunking)\n", algo, e);
  456. memset(xbuf, 0, XBUFSIZE);
  457. j = 0;
  458. for (i = 0; i < tcount; i++) {
  459. memcpy(cipher_tv, &template[i], tsize);
  460. if (cipher_tv->np) {
  461. j++;
  462. printk("test %u (%d bit key):\n",
  463. j, cipher_tv->klen * 8);
  464. crypto_ablkcipher_clear_flags(tfm, ~0);
  465. if (cipher_tv->wk)
  466. crypto_ablkcipher_set_flags(
  467. tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  468. key = cipher_tv->key;
  469. ret = crypto_ablkcipher_setkey(tfm, key,
  470. cipher_tv->klen);
  471. if (ret) {
  472. printk("setkey() failed flags=%x\n",
  473. crypto_ablkcipher_get_flags(tfm));
  474. if (!cipher_tv->fail)
  475. goto out;
  476. }
  477. temp = 0;
  478. sg_init_table(sg, cipher_tv->np);
  479. for (k = 0; k < cipher_tv->np; k++) {
  480. memcpy(&xbuf[IDX[k]],
  481. cipher_tv->input + temp,
  482. cipher_tv->tap[k]);
  483. temp += cipher_tv->tap[k];
  484. sg_set_buf(&sg[k], &xbuf[IDX[k]],
  485. cipher_tv->tap[k]);
  486. }
  487. ablkcipher_request_set_crypt(req, sg, sg,
  488. cipher_tv->ilen,
  489. cipher_tv->iv);
  490. ret = enc ?
  491. crypto_ablkcipher_encrypt(req) :
  492. crypto_ablkcipher_decrypt(req);
  493. switch (ret) {
  494. case 0:
  495. break;
  496. case -EINPROGRESS:
  497. case -EBUSY:
  498. ret = wait_for_completion_interruptible(
  499. &result.completion);
  500. if (!ret && !((ret = result.err))) {
  501. INIT_COMPLETION(result.completion);
  502. break;
  503. }
  504. /* fall through */
  505. default:
  506. printk("%s () failed err=%d\n", e, -ret);
  507. goto out;
  508. }
  509. temp = 0;
  510. for (k = 0; k < cipher_tv->np; k++) {
  511. printk("page %u\n", k);
  512. q = kmap(sg_page(&sg[k])) + sg[k].offset;
  513. hexdump(q, cipher_tv->tap[k]);
  514. printk("%s\n",
  515. memcmp(q, cipher_tv->result + temp,
  516. cipher_tv->tap[k]) ? "fail" :
  517. "pass");
  518. temp += cipher_tv->tap[k];
  519. }
  520. }
  521. }
  522. out:
  523. crypto_free_ablkcipher(tfm);
  524. ablkcipher_request_free(req);
  525. }
  526. static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc, char *p,
  527. int blen, int sec)
  528. {
  529. struct scatterlist sg[1];
  530. unsigned long start, end;
  531. int bcount;
  532. int ret;
  533. sg_init_one(sg, p, blen);
  534. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  535. time_before(jiffies, end); bcount++) {
  536. if (enc)
  537. ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
  538. else
  539. ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
  540. if (ret)
  541. return ret;
  542. }
  543. printk("%d operations in %d seconds (%ld bytes)\n",
  544. bcount, sec, (long)bcount * blen);
  545. return 0;
  546. }
  547. static int test_cipher_cycles(struct blkcipher_desc *desc, int enc, char *p,
  548. int blen)
  549. {
  550. struct scatterlist sg[1];
  551. unsigned long cycles = 0;
  552. int ret = 0;
  553. int i;
  554. sg_init_one(sg, p, blen);
  555. local_bh_disable();
  556. local_irq_disable();
  557. /* Warm-up run. */
  558. for (i = 0; i < 4; i++) {
  559. if (enc)
  560. ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
  561. else
  562. ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
  563. if (ret)
  564. goto out;
  565. }
  566. /* The real thing. */
  567. for (i = 0; i < 8; i++) {
  568. cycles_t start, end;
  569. start = get_cycles();
  570. if (enc)
  571. ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
  572. else
  573. ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
  574. end = get_cycles();
  575. if (ret)
  576. goto out;
  577. cycles += end - start;
  578. }
  579. out:
  580. local_irq_enable();
  581. local_bh_enable();
  582. if (ret == 0)
  583. printk("1 operation in %lu cycles (%d bytes)\n",
  584. (cycles + 4) / 8, blen);
  585. return ret;
  586. }
  587. static void test_cipher_speed(char *algo, int enc, unsigned int sec,
  588. struct cipher_testvec *template,
  589. unsigned int tcount, struct cipher_speed *speed)
  590. {
  591. unsigned int ret, i, j, iv_len;
  592. unsigned char *key, *p, iv[128];
  593. struct crypto_blkcipher *tfm;
  594. struct blkcipher_desc desc;
  595. const char *e;
  596. if (enc == ENCRYPT)
  597. e = "encryption";
  598. else
  599. e = "decryption";
  600. printk("\ntesting speed of %s %s\n", algo, e);
  601. tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
  602. if (IS_ERR(tfm)) {
  603. printk("failed to load transform for %s: %ld\n", algo,
  604. PTR_ERR(tfm));
  605. return;
  606. }
  607. desc.tfm = tfm;
  608. desc.flags = 0;
  609. for (i = 0; speed[i].klen != 0; i++) {
  610. if ((speed[i].blen + speed[i].klen) > TVMEMSIZE) {
  611. printk("template (%u) too big for tvmem (%u)\n",
  612. speed[i].blen + speed[i].klen, TVMEMSIZE);
  613. goto out;
  614. }
  615. printk("test %u (%d bit key, %d byte blocks): ", i,
  616. speed[i].klen * 8, speed[i].blen);
  617. memset(tvmem, 0xff, speed[i].klen + speed[i].blen);
  618. /* set key, plain text and IV */
  619. key = (unsigned char *)tvmem;
  620. for (j = 0; j < tcount; j++) {
  621. if (template[j].klen == speed[i].klen) {
  622. key = template[j].key;
  623. break;
  624. }
  625. }
  626. p = (unsigned char *)tvmem + speed[i].klen;
  627. ret = crypto_blkcipher_setkey(tfm, key, speed[i].klen);
  628. if (ret) {
  629. printk("setkey() failed flags=%x\n",
  630. crypto_blkcipher_get_flags(tfm));
  631. goto out;
  632. }
  633. iv_len = crypto_blkcipher_ivsize(tfm);
  634. if (iv_len) {
  635. memset(&iv, 0xff, iv_len);
  636. crypto_blkcipher_set_iv(tfm, iv, iv_len);
  637. }
  638. if (sec)
  639. ret = test_cipher_jiffies(&desc, enc, p, speed[i].blen,
  640. sec);
  641. else
  642. ret = test_cipher_cycles(&desc, enc, p, speed[i].blen);
  643. if (ret) {
  644. printk("%s() failed flags=%x\n", e, desc.flags);
  645. break;
  646. }
  647. }
  648. out:
  649. crypto_free_blkcipher(tfm);
  650. }
  651. static int test_hash_jiffies_digest(struct hash_desc *desc, char *p, int blen,
  652. char *out, int sec)
  653. {
  654. struct scatterlist sg[1];
  655. unsigned long start, end;
  656. int bcount;
  657. int ret;
  658. sg_init_table(sg, 1);
  659. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  660. time_before(jiffies, end); bcount++) {
  661. sg_set_buf(sg, p, blen);
  662. ret = crypto_hash_digest(desc, sg, blen, out);
  663. if (ret)
  664. return ret;
  665. }
  666. printk("%6u opers/sec, %9lu bytes/sec\n",
  667. bcount / sec, ((long)bcount * blen) / sec);
  668. return 0;
  669. }
  670. static int test_hash_jiffies(struct hash_desc *desc, char *p, int blen,
  671. int plen, char *out, int sec)
  672. {
  673. struct scatterlist sg[1];
  674. unsigned long start, end;
  675. int bcount, pcount;
  676. int ret;
  677. if (plen == blen)
  678. return test_hash_jiffies_digest(desc, p, blen, out, sec);
  679. sg_init_table(sg, 1);
  680. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  681. time_before(jiffies, end); bcount++) {
  682. ret = crypto_hash_init(desc);
  683. if (ret)
  684. return ret;
  685. for (pcount = 0; pcount < blen; pcount += plen) {
  686. sg_set_buf(sg, p + pcount, plen);
  687. ret = crypto_hash_update(desc, sg, plen);
  688. if (ret)
  689. return ret;
  690. }
  691. /* we assume there is enough space in 'out' for the result */
  692. ret = crypto_hash_final(desc, out);
  693. if (ret)
  694. return ret;
  695. }
  696. printk("%6u opers/sec, %9lu bytes/sec\n",
  697. bcount / sec, ((long)bcount * blen) / sec);
  698. return 0;
  699. }
  700. static int test_hash_cycles_digest(struct hash_desc *desc, char *p, int blen,
  701. char *out)
  702. {
  703. struct scatterlist sg[1];
  704. unsigned long cycles = 0;
  705. int i;
  706. int ret;
  707. sg_init_table(sg, 1);
  708. local_bh_disable();
  709. local_irq_disable();
  710. /* Warm-up run. */
  711. for (i = 0; i < 4; i++) {
  712. sg_set_buf(sg, p, blen);
  713. ret = crypto_hash_digest(desc, sg, blen, out);
  714. if (ret)
  715. goto out;
  716. }
  717. /* The real thing. */
  718. for (i = 0; i < 8; i++) {
  719. cycles_t start, end;
  720. start = get_cycles();
  721. sg_set_buf(sg, p, blen);
  722. ret = crypto_hash_digest(desc, sg, blen, out);
  723. if (ret)
  724. goto out;
  725. end = get_cycles();
  726. cycles += end - start;
  727. }
  728. out:
  729. local_irq_enable();
  730. local_bh_enable();
  731. if (ret)
  732. return ret;
  733. printk("%6lu cycles/operation, %4lu cycles/byte\n",
  734. cycles / 8, cycles / (8 * blen));
  735. return 0;
  736. }
  737. static int test_hash_cycles(struct hash_desc *desc, char *p, int blen,
  738. int plen, char *out)
  739. {
  740. struct scatterlist sg[1];
  741. unsigned long cycles = 0;
  742. int i, pcount;
  743. int ret;
  744. if (plen == blen)
  745. return test_hash_cycles_digest(desc, p, blen, out);
  746. sg_init_table(sg, 1);
  747. local_bh_disable();
  748. local_irq_disable();
  749. /* Warm-up run. */
  750. for (i = 0; i < 4; i++) {
  751. ret = crypto_hash_init(desc);
  752. if (ret)
  753. goto out;
  754. for (pcount = 0; pcount < blen; pcount += plen) {
  755. sg_set_buf(sg, p + pcount, plen);
  756. ret = crypto_hash_update(desc, sg, plen);
  757. if (ret)
  758. goto out;
  759. }
  760. ret = crypto_hash_final(desc, out);
  761. if (ret)
  762. goto out;
  763. }
  764. /* The real thing. */
  765. for (i = 0; i < 8; i++) {
  766. cycles_t start, end;
  767. start = get_cycles();
  768. ret = crypto_hash_init(desc);
  769. if (ret)
  770. goto out;
  771. for (pcount = 0; pcount < blen; pcount += plen) {
  772. sg_set_buf(sg, p + pcount, plen);
  773. ret = crypto_hash_update(desc, sg, plen);
  774. if (ret)
  775. goto out;
  776. }
  777. ret = crypto_hash_final(desc, out);
  778. if (ret)
  779. goto out;
  780. end = get_cycles();
  781. cycles += end - start;
  782. }
  783. out:
  784. local_irq_enable();
  785. local_bh_enable();
  786. if (ret)
  787. return ret;
  788. printk("%6lu cycles/operation, %4lu cycles/byte\n",
  789. cycles / 8, cycles / (8 * blen));
  790. return 0;
  791. }
  792. static void test_hash_speed(char *algo, unsigned int sec,
  793. struct hash_speed *speed)
  794. {
  795. struct crypto_hash *tfm;
  796. struct hash_desc desc;
  797. char output[1024];
  798. int i;
  799. int ret;
  800. printk("\ntesting speed of %s\n", algo);
  801. tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
  802. if (IS_ERR(tfm)) {
  803. printk("failed to load transform for %s: %ld\n", algo,
  804. PTR_ERR(tfm));
  805. return;
  806. }
  807. desc.tfm = tfm;
  808. desc.flags = 0;
  809. if (crypto_hash_digestsize(tfm) > sizeof(output)) {
  810. printk("digestsize(%u) > outputbuffer(%zu)\n",
  811. crypto_hash_digestsize(tfm), sizeof(output));
  812. goto out;
  813. }
  814. for (i = 0; speed[i].blen != 0; i++) {
  815. if (speed[i].blen > TVMEMSIZE) {
  816. printk("template (%u) too big for tvmem (%u)\n",
  817. speed[i].blen, TVMEMSIZE);
  818. goto out;
  819. }
  820. printk("test%3u (%5u byte blocks,%5u bytes per update,%4u updates): ",
  821. i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
  822. memset(tvmem, 0xff, speed[i].blen);
  823. if (sec)
  824. ret = test_hash_jiffies(&desc, tvmem, speed[i].blen,
  825. speed[i].plen, output, sec);
  826. else
  827. ret = test_hash_cycles(&desc, tvmem, speed[i].blen,
  828. speed[i].plen, output);
  829. if (ret) {
  830. printk("hashing failed ret=%d\n", ret);
  831. break;
  832. }
  833. }
  834. out:
  835. crypto_free_hash(tfm);
  836. }
  837. static void test_comp(char *algo, struct comp_testvec *ctemplate,
  838. struct comp_testvec *dtemplate, int ctcount, int dtcount)
  839. {
  840. unsigned int i;
  841. char result[COMP_BUF_SIZE];
  842. struct crypto_comp *tfm;
  843. struct comp_testvec *tv;
  844. unsigned int tsize;
  845. printk("\ntesting %s compression\n", algo);
  846. tsize = sizeof(struct comp_testvec);
  847. tsize *= ctcount;
  848. if (tsize > TVMEMSIZE) {
  849. printk("template (%u) too big for tvmem (%u)\n", tsize,
  850. TVMEMSIZE);
  851. return;
  852. }
  853. memcpy(tvmem, ctemplate, tsize);
  854. tv = (void *)tvmem;
  855. tfm = crypto_alloc_comp(algo, 0, CRYPTO_ALG_ASYNC);
  856. if (IS_ERR(tfm)) {
  857. printk("failed to load transform for %s\n", algo);
  858. return;
  859. }
  860. for (i = 0; i < ctcount; i++) {
  861. int ilen, ret, dlen = COMP_BUF_SIZE;
  862. printk("test %u:\n", i + 1);
  863. memset(result, 0, sizeof (result));
  864. ilen = tv[i].inlen;
  865. ret = crypto_comp_compress(tfm, tv[i].input,
  866. ilen, result, &dlen);
  867. if (ret) {
  868. printk("fail: ret=%d\n", ret);
  869. continue;
  870. }
  871. hexdump(result, dlen);
  872. printk("%s (ratio %d:%d)\n",
  873. memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
  874. ilen, dlen);
  875. }
  876. printk("\ntesting %s decompression\n", algo);
  877. tsize = sizeof(struct comp_testvec);
  878. tsize *= dtcount;
  879. if (tsize > TVMEMSIZE) {
  880. printk("template (%u) too big for tvmem (%u)\n", tsize,
  881. TVMEMSIZE);
  882. goto out;
  883. }
  884. memcpy(tvmem, dtemplate, tsize);
  885. tv = (void *)tvmem;
  886. for (i = 0; i < dtcount; i++) {
  887. int ilen, ret, dlen = COMP_BUF_SIZE;
  888. printk("test %u:\n", i + 1);
  889. memset(result, 0, sizeof (result));
  890. ilen = tv[i].inlen;
  891. ret = crypto_comp_decompress(tfm, tv[i].input,
  892. ilen, result, &dlen);
  893. if (ret) {
  894. printk("fail: ret=%d\n", ret);
  895. continue;
  896. }
  897. hexdump(result, dlen);
  898. printk("%s (ratio %d:%d)\n",
  899. memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
  900. ilen, dlen);
  901. }
  902. out:
  903. crypto_free_comp(tfm);
  904. }
  905. static void test_available(void)
  906. {
  907. char **name = check;
  908. while (*name) {
  909. printk("alg %s ", *name);
  910. printk(crypto_has_alg(*name, 0, 0) ?
  911. "found\n" : "not found\n");
  912. name++;
  913. }
  914. }
  915. static void do_test(void)
  916. {
  917. switch (mode) {
  918. case 0:
  919. test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
  920. test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
  921. //DES
  922. test_cipher("ecb(des)", ENCRYPT, des_enc_tv_template,
  923. DES_ENC_TEST_VECTORS);
  924. test_cipher("ecb(des)", DECRYPT, des_dec_tv_template,
  925. DES_DEC_TEST_VECTORS);
  926. test_cipher("cbc(des)", ENCRYPT, des_cbc_enc_tv_template,
  927. DES_CBC_ENC_TEST_VECTORS);
  928. test_cipher("cbc(des)", DECRYPT, des_cbc_dec_tv_template,
  929. DES_CBC_DEC_TEST_VECTORS);
  930. //DES3_EDE
  931. test_cipher("ecb(des3_ede)", ENCRYPT, des3_ede_enc_tv_template,
  932. DES3_EDE_ENC_TEST_VECTORS);
  933. test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
  934. DES3_EDE_DEC_TEST_VECTORS);
  935. test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
  936. test_hash("sha224", sha224_tv_template, SHA224_TEST_VECTORS);
  937. test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
  938. //BLOWFISH
  939. test_cipher("ecb(blowfish)", ENCRYPT, bf_enc_tv_template,
  940. BF_ENC_TEST_VECTORS);
  941. test_cipher("ecb(blowfish)", DECRYPT, bf_dec_tv_template,
  942. BF_DEC_TEST_VECTORS);
  943. test_cipher("cbc(blowfish)", ENCRYPT, bf_cbc_enc_tv_template,
  944. BF_CBC_ENC_TEST_VECTORS);
  945. test_cipher("cbc(blowfish)", DECRYPT, bf_cbc_dec_tv_template,
  946. BF_CBC_DEC_TEST_VECTORS);
  947. //TWOFISH
  948. test_cipher("ecb(twofish)", ENCRYPT, tf_enc_tv_template,
  949. TF_ENC_TEST_VECTORS);
  950. test_cipher("ecb(twofish)", DECRYPT, tf_dec_tv_template,
  951. TF_DEC_TEST_VECTORS);
  952. test_cipher("cbc(twofish)", ENCRYPT, tf_cbc_enc_tv_template,
  953. TF_CBC_ENC_TEST_VECTORS);
  954. test_cipher("cbc(twofish)", DECRYPT, tf_cbc_dec_tv_template,
  955. TF_CBC_DEC_TEST_VECTORS);
  956. //SERPENT
  957. test_cipher("ecb(serpent)", ENCRYPT, serpent_enc_tv_template,
  958. SERPENT_ENC_TEST_VECTORS);
  959. test_cipher("ecb(serpent)", DECRYPT, serpent_dec_tv_template,
  960. SERPENT_DEC_TEST_VECTORS);
  961. //TNEPRES
  962. test_cipher("ecb(tnepres)", ENCRYPT, tnepres_enc_tv_template,
  963. TNEPRES_ENC_TEST_VECTORS);
  964. test_cipher("ecb(tnepres)", DECRYPT, tnepres_dec_tv_template,
  965. TNEPRES_DEC_TEST_VECTORS);
  966. //AES
  967. test_cipher("ecb(aes)", ENCRYPT, aes_enc_tv_template,
  968. AES_ENC_TEST_VECTORS);
  969. test_cipher("ecb(aes)", DECRYPT, aes_dec_tv_template,
  970. AES_DEC_TEST_VECTORS);
  971. test_cipher("cbc(aes)", ENCRYPT, aes_cbc_enc_tv_template,
  972. AES_CBC_ENC_TEST_VECTORS);
  973. test_cipher("cbc(aes)", DECRYPT, aes_cbc_dec_tv_template,
  974. AES_CBC_DEC_TEST_VECTORS);
  975. test_cipher("lrw(aes)", ENCRYPT, aes_lrw_enc_tv_template,
  976. AES_LRW_ENC_TEST_VECTORS);
  977. test_cipher("lrw(aes)", DECRYPT, aes_lrw_dec_tv_template,
  978. AES_LRW_DEC_TEST_VECTORS);
  979. test_cipher("xts(aes)", ENCRYPT, aes_xts_enc_tv_template,
  980. AES_XTS_ENC_TEST_VECTORS);
  981. test_cipher("xts(aes)", DECRYPT, aes_xts_dec_tv_template,
  982. AES_XTS_DEC_TEST_VECTORS);
  983. test_cipher("ctr(aes,4,8,4)", ENCRYPT, aes_ctr_enc_tv_template,
  984. AES_CTR_ENC_TEST_VECTORS);
  985. test_cipher("ctr(aes,4,8,4)", DECRYPT, aes_ctr_dec_tv_template,
  986. AES_CTR_DEC_TEST_VECTORS);
  987. test_aead("gcm(aes)", ENCRYPT, aes_gcm_enc_tv_template,
  988. AES_GCM_ENC_TEST_VECTORS);
  989. test_aead("gcm(aes)", DECRYPT, aes_gcm_dec_tv_template,
  990. AES_GCM_DEC_TEST_VECTORS);
  991. //CAST5
  992. test_cipher("ecb(cast5)", ENCRYPT, cast5_enc_tv_template,
  993. CAST5_ENC_TEST_VECTORS);
  994. test_cipher("ecb(cast5)", DECRYPT, cast5_dec_tv_template,
  995. CAST5_DEC_TEST_VECTORS);
  996. //CAST6
  997. test_cipher("ecb(cast6)", ENCRYPT, cast6_enc_tv_template,
  998. CAST6_ENC_TEST_VECTORS);
  999. test_cipher("ecb(cast6)", DECRYPT, cast6_dec_tv_template,
  1000. CAST6_DEC_TEST_VECTORS);
  1001. //ARC4
  1002. test_cipher("ecb(arc4)", ENCRYPT, arc4_enc_tv_template,
  1003. ARC4_ENC_TEST_VECTORS);
  1004. test_cipher("ecb(arc4)", DECRYPT, arc4_dec_tv_template,
  1005. ARC4_DEC_TEST_VECTORS);
  1006. //TEA
  1007. test_cipher("ecb(tea)", ENCRYPT, tea_enc_tv_template,
  1008. TEA_ENC_TEST_VECTORS);
  1009. test_cipher("ecb(tea)", DECRYPT, tea_dec_tv_template,
  1010. TEA_DEC_TEST_VECTORS);
  1011. //XTEA
  1012. test_cipher("ecb(xtea)", ENCRYPT, xtea_enc_tv_template,
  1013. XTEA_ENC_TEST_VECTORS);
  1014. test_cipher("ecb(xtea)", DECRYPT, xtea_dec_tv_template,
  1015. XTEA_DEC_TEST_VECTORS);
  1016. //KHAZAD
  1017. test_cipher("ecb(khazad)", ENCRYPT, khazad_enc_tv_template,
  1018. KHAZAD_ENC_TEST_VECTORS);
  1019. test_cipher("ecb(khazad)", DECRYPT, khazad_dec_tv_template,
  1020. KHAZAD_DEC_TEST_VECTORS);
  1021. //ANUBIS
  1022. test_cipher("ecb(anubis)", ENCRYPT, anubis_enc_tv_template,
  1023. ANUBIS_ENC_TEST_VECTORS);
  1024. test_cipher("ecb(anubis)", DECRYPT, anubis_dec_tv_template,
  1025. ANUBIS_DEC_TEST_VECTORS);
  1026. test_cipher("cbc(anubis)", ENCRYPT, anubis_cbc_enc_tv_template,
  1027. ANUBIS_CBC_ENC_TEST_VECTORS);
  1028. test_cipher("cbc(anubis)", DECRYPT, anubis_cbc_dec_tv_template,
  1029. ANUBIS_CBC_ENC_TEST_VECTORS);
  1030. //XETA
  1031. test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
  1032. XETA_ENC_TEST_VECTORS);
  1033. test_cipher("ecb(xeta)", DECRYPT, xeta_dec_tv_template,
  1034. XETA_DEC_TEST_VECTORS);
  1035. //FCrypt
  1036. test_cipher("pcbc(fcrypt)", ENCRYPT, fcrypt_pcbc_enc_tv_template,
  1037. FCRYPT_ENC_TEST_VECTORS);
  1038. test_cipher("pcbc(fcrypt)", DECRYPT, fcrypt_pcbc_dec_tv_template,
  1039. FCRYPT_DEC_TEST_VECTORS);
  1040. //CAMELLIA
  1041. test_cipher("ecb(camellia)", ENCRYPT,
  1042. camellia_enc_tv_template,
  1043. CAMELLIA_ENC_TEST_VECTORS);
  1044. test_cipher("ecb(camellia)", DECRYPT,
  1045. camellia_dec_tv_template,
  1046. CAMELLIA_DEC_TEST_VECTORS);
  1047. test_cipher("cbc(camellia)", ENCRYPT,
  1048. camellia_cbc_enc_tv_template,
  1049. CAMELLIA_CBC_ENC_TEST_VECTORS);
  1050. test_cipher("cbc(camellia)", DECRYPT,
  1051. camellia_cbc_dec_tv_template,
  1052. CAMELLIA_CBC_DEC_TEST_VECTORS);
  1053. //SEED
  1054. test_cipher("ecb(seed)", ENCRYPT, seed_enc_tv_template,
  1055. SEED_ENC_TEST_VECTORS);
  1056. test_cipher("ecb(seed)", DECRYPT, seed_dec_tv_template,
  1057. SEED_DEC_TEST_VECTORS);
  1058. test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
  1059. test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
  1060. test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
  1061. test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
  1062. test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
  1063. test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
  1064. test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
  1065. test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
  1066. test_comp("deflate", deflate_comp_tv_template,
  1067. deflate_decomp_tv_template, DEFLATE_COMP_TEST_VECTORS,
  1068. DEFLATE_DECOMP_TEST_VECTORS);
  1069. test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
  1070. test_hash("hmac(md5)", hmac_md5_tv_template,
  1071. HMAC_MD5_TEST_VECTORS);
  1072. test_hash("hmac(sha1)", hmac_sha1_tv_template,
  1073. HMAC_SHA1_TEST_VECTORS);
  1074. test_hash("hmac(sha224)", hmac_sha224_tv_template,
  1075. HMAC_SHA224_TEST_VECTORS);
  1076. test_hash("hmac(sha256)", hmac_sha256_tv_template,
  1077. HMAC_SHA256_TEST_VECTORS);
  1078. test_hash("hmac(sha384)", hmac_sha384_tv_template,
  1079. HMAC_SHA384_TEST_VECTORS);
  1080. test_hash("hmac(sha512)", hmac_sha512_tv_template,
  1081. HMAC_SHA512_TEST_VECTORS);
  1082. test_hash("xcbc(aes)", aes_xcbc128_tv_template,
  1083. XCBC_AES_TEST_VECTORS);
  1084. test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
  1085. break;
  1086. case 1:
  1087. test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
  1088. break;
  1089. case 2:
  1090. test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
  1091. break;
  1092. case 3:
  1093. test_cipher("ecb(des)", ENCRYPT, des_enc_tv_template,
  1094. DES_ENC_TEST_VECTORS);
  1095. test_cipher("ecb(des)", DECRYPT, des_dec_tv_template,
  1096. DES_DEC_TEST_VECTORS);
  1097. test_cipher("cbc(des)", ENCRYPT, des_cbc_enc_tv_template,
  1098. DES_CBC_ENC_TEST_VECTORS);
  1099. test_cipher("cbc(des)", DECRYPT, des_cbc_dec_tv_template,
  1100. DES_CBC_DEC_TEST_VECTORS);
  1101. break;
  1102. case 4:
  1103. test_cipher("ecb(des3_ede)", ENCRYPT, des3_ede_enc_tv_template,
  1104. DES3_EDE_ENC_TEST_VECTORS);
  1105. test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
  1106. DES3_EDE_DEC_TEST_VECTORS);
  1107. break;
  1108. case 5:
  1109. test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
  1110. break;
  1111. case 6:
  1112. test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
  1113. break;
  1114. case 7:
  1115. test_cipher("ecb(blowfish)", ENCRYPT, bf_enc_tv_template,
  1116. BF_ENC_TEST_VECTORS);
  1117. test_cipher("ecb(blowfish)", DECRYPT, bf_dec_tv_template,
  1118. BF_DEC_TEST_VECTORS);
  1119. test_cipher("cbc(blowfish)", ENCRYPT, bf_cbc_enc_tv_template,
  1120. BF_CBC_ENC_TEST_VECTORS);
  1121. test_cipher("cbc(blowfish)", DECRYPT, bf_cbc_dec_tv_template,
  1122. BF_CBC_DEC_TEST_VECTORS);
  1123. break;
  1124. case 8:
  1125. test_cipher("ecb(twofish)", ENCRYPT, tf_enc_tv_template,
  1126. TF_ENC_TEST_VECTORS);
  1127. test_cipher("ecb(twofish)", DECRYPT, tf_dec_tv_template,
  1128. TF_DEC_TEST_VECTORS);
  1129. test_cipher("cbc(twofish)", ENCRYPT, tf_cbc_enc_tv_template,
  1130. TF_CBC_ENC_TEST_VECTORS);
  1131. test_cipher("cbc(twofish)", DECRYPT, tf_cbc_dec_tv_template,
  1132. TF_CBC_DEC_TEST_VECTORS);
  1133. break;
  1134. case 9:
  1135. test_cipher("ecb(serpent)", ENCRYPT, serpent_enc_tv_template,
  1136. SERPENT_ENC_TEST_VECTORS);
  1137. test_cipher("ecb(serpent)", DECRYPT, serpent_dec_tv_template,
  1138. SERPENT_DEC_TEST_VECTORS);
  1139. break;
  1140. case 10:
  1141. test_cipher("ecb(aes)", ENCRYPT, aes_enc_tv_template,
  1142. AES_ENC_TEST_VECTORS);
  1143. test_cipher("ecb(aes)", DECRYPT, aes_dec_tv_template,
  1144. AES_DEC_TEST_VECTORS);
  1145. test_cipher("cbc(aes)", ENCRYPT, aes_cbc_enc_tv_template,
  1146. AES_CBC_ENC_TEST_VECTORS);
  1147. test_cipher("cbc(aes)", DECRYPT, aes_cbc_dec_tv_template,
  1148. AES_CBC_DEC_TEST_VECTORS);
  1149. test_cipher("lrw(aes)", ENCRYPT, aes_lrw_enc_tv_template,
  1150. AES_LRW_ENC_TEST_VECTORS);
  1151. test_cipher("lrw(aes)", DECRYPT, aes_lrw_dec_tv_template,
  1152. AES_LRW_DEC_TEST_VECTORS);
  1153. test_cipher("xts(aes)", ENCRYPT, aes_xts_enc_tv_template,
  1154. AES_XTS_ENC_TEST_VECTORS);
  1155. test_cipher("xts(aes)", DECRYPT, aes_xts_dec_tv_template,
  1156. AES_XTS_DEC_TEST_VECTORS);
  1157. test_cipher("ctr(aes,4,8,4)", ENCRYPT, aes_ctr_enc_tv_template,
  1158. AES_CTR_ENC_TEST_VECTORS);
  1159. test_cipher("ctr(aes,4,8,4)", DECRYPT, aes_ctr_dec_tv_template,
  1160. AES_CTR_DEC_TEST_VECTORS);
  1161. break;
  1162. case 11:
  1163. test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
  1164. break;
  1165. case 12:
  1166. test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
  1167. break;
  1168. case 13:
  1169. test_comp("deflate", deflate_comp_tv_template,
  1170. deflate_decomp_tv_template, DEFLATE_COMP_TEST_VECTORS,
  1171. DEFLATE_DECOMP_TEST_VECTORS);
  1172. break;
  1173. case 14:
  1174. test_cipher("ecb(cast5)", ENCRYPT, cast5_enc_tv_template,
  1175. CAST5_ENC_TEST_VECTORS);
  1176. test_cipher("ecb(cast5)", DECRYPT, cast5_dec_tv_template,
  1177. CAST5_DEC_TEST_VECTORS);
  1178. break;
  1179. case 15:
  1180. test_cipher("ecb(cast6)", ENCRYPT, cast6_enc_tv_template,
  1181. CAST6_ENC_TEST_VECTORS);
  1182. test_cipher("ecb(cast6)", DECRYPT, cast6_dec_tv_template,
  1183. CAST6_DEC_TEST_VECTORS);
  1184. break;
  1185. case 16:
  1186. test_cipher("ecb(arc4)", ENCRYPT, arc4_enc_tv_template,
  1187. ARC4_ENC_TEST_VECTORS);
  1188. test_cipher("ecb(arc4)", DECRYPT, arc4_dec_tv_template,
  1189. ARC4_DEC_TEST_VECTORS);
  1190. break;
  1191. case 17:
  1192. test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
  1193. break;
  1194. case 18:
  1195. test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
  1196. break;
  1197. case 19:
  1198. test_cipher("ecb(tea)", ENCRYPT, tea_enc_tv_template,
  1199. TEA_ENC_TEST_VECTORS);
  1200. test_cipher("ecb(tea)", DECRYPT, tea_dec_tv_template,
  1201. TEA_DEC_TEST_VECTORS);
  1202. break;
  1203. case 20:
  1204. test_cipher("ecb(xtea)", ENCRYPT, xtea_enc_tv_template,
  1205. XTEA_ENC_TEST_VECTORS);
  1206. test_cipher("ecb(xtea)", DECRYPT, xtea_dec_tv_template,
  1207. XTEA_DEC_TEST_VECTORS);
  1208. break;
  1209. case 21:
  1210. test_cipher("ecb(khazad)", ENCRYPT, khazad_enc_tv_template,
  1211. KHAZAD_ENC_TEST_VECTORS);
  1212. test_cipher("ecb(khazad)", DECRYPT, khazad_dec_tv_template,
  1213. KHAZAD_DEC_TEST_VECTORS);
  1214. break;
  1215. case 22:
  1216. test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
  1217. break;
  1218. case 23:
  1219. test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
  1220. break;
  1221. case 24:
  1222. test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
  1223. break;
  1224. case 25:
  1225. test_cipher("ecb(tnepres)", ENCRYPT, tnepres_enc_tv_template,
  1226. TNEPRES_ENC_TEST_VECTORS);
  1227. test_cipher("ecb(tnepres)", DECRYPT, tnepres_dec_tv_template,
  1228. TNEPRES_DEC_TEST_VECTORS);
  1229. break;
  1230. case 26:
  1231. test_cipher("ecb(anubis)", ENCRYPT, anubis_enc_tv_template,
  1232. ANUBIS_ENC_TEST_VECTORS);
  1233. test_cipher("ecb(anubis)", DECRYPT, anubis_dec_tv_template,
  1234. ANUBIS_DEC_TEST_VECTORS);
  1235. test_cipher("cbc(anubis)", ENCRYPT, anubis_cbc_enc_tv_template,
  1236. ANUBIS_CBC_ENC_TEST_VECTORS);
  1237. test_cipher("cbc(anubis)", DECRYPT, anubis_cbc_dec_tv_template,
  1238. ANUBIS_CBC_ENC_TEST_VECTORS);
  1239. break;
  1240. case 27:
  1241. test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
  1242. break;
  1243. case 28:
  1244. test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
  1245. break;
  1246. case 29:
  1247. test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
  1248. break;
  1249. case 30:
  1250. test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
  1251. XETA_ENC_TEST_VECTORS);
  1252. test_cipher("ecb(xeta)", DECRYPT, xeta_dec_tv_template,
  1253. XETA_DEC_TEST_VECTORS);
  1254. break;
  1255. case 31:
  1256. test_cipher("pcbc(fcrypt)", ENCRYPT, fcrypt_pcbc_enc_tv_template,
  1257. FCRYPT_ENC_TEST_VECTORS);
  1258. test_cipher("pcbc(fcrypt)", DECRYPT, fcrypt_pcbc_dec_tv_template,
  1259. FCRYPT_DEC_TEST_VECTORS);
  1260. break;
  1261. case 32:
  1262. test_cipher("ecb(camellia)", ENCRYPT,
  1263. camellia_enc_tv_template,
  1264. CAMELLIA_ENC_TEST_VECTORS);
  1265. test_cipher("ecb(camellia)", DECRYPT,
  1266. camellia_dec_tv_template,
  1267. CAMELLIA_DEC_TEST_VECTORS);
  1268. test_cipher("cbc(camellia)", ENCRYPT,
  1269. camellia_cbc_enc_tv_template,
  1270. CAMELLIA_CBC_ENC_TEST_VECTORS);
  1271. test_cipher("cbc(camellia)", DECRYPT,
  1272. camellia_cbc_dec_tv_template,
  1273. CAMELLIA_CBC_DEC_TEST_VECTORS);
  1274. break;
  1275. case 33:
  1276. test_hash("sha224", sha224_tv_template, SHA224_TEST_VECTORS);
  1277. break;
  1278. case 34:
  1279. test_cipher("salsa20", ENCRYPT,
  1280. salsa20_stream_enc_tv_template,
  1281. SALSA20_STREAM_ENC_TEST_VECTORS);
  1282. break;
  1283. case 35:
  1284. test_aead("gcm(aes)", ENCRYPT, aes_gcm_enc_tv_template,
  1285. AES_GCM_ENC_TEST_VECTORS);
  1286. test_aead("gcm(aes)", DECRYPT, aes_gcm_dec_tv_template,
  1287. AES_GCM_DEC_TEST_VECTORS);
  1288. break;
  1289. case 100:
  1290. test_hash("hmac(md5)", hmac_md5_tv_template,
  1291. HMAC_MD5_TEST_VECTORS);
  1292. break;
  1293. case 101:
  1294. test_hash("hmac(sha1)", hmac_sha1_tv_template,
  1295. HMAC_SHA1_TEST_VECTORS);
  1296. break;
  1297. case 102:
  1298. test_hash("hmac(sha256)", hmac_sha256_tv_template,
  1299. HMAC_SHA256_TEST_VECTORS);
  1300. break;
  1301. case 103:
  1302. test_hash("hmac(sha384)", hmac_sha384_tv_template,
  1303. HMAC_SHA384_TEST_VECTORS);
  1304. break;
  1305. case 104:
  1306. test_hash("hmac(sha512)", hmac_sha512_tv_template,
  1307. HMAC_SHA512_TEST_VECTORS);
  1308. break;
  1309. case 105:
  1310. test_hash("hmac(sha224)", hmac_sha224_tv_template,
  1311. HMAC_SHA224_TEST_VECTORS);
  1312. break;
  1313. case 200:
  1314. test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
  1315. aes_speed_template);
  1316. test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
  1317. aes_speed_template);
  1318. test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
  1319. aes_speed_template);
  1320. test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
  1321. aes_speed_template);
  1322. test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
  1323. aes_lrw_speed_template);
  1324. test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
  1325. aes_lrw_speed_template);
  1326. test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
  1327. aes_xts_speed_template);
  1328. test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
  1329. aes_xts_speed_template);
  1330. break;
  1331. case 201:
  1332. test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
  1333. des3_ede_enc_tv_template,
  1334. DES3_EDE_ENC_TEST_VECTORS,
  1335. des3_ede_speed_template);
  1336. test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
  1337. des3_ede_dec_tv_template,
  1338. DES3_EDE_DEC_TEST_VECTORS,
  1339. des3_ede_speed_template);
  1340. test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
  1341. des3_ede_enc_tv_template,
  1342. DES3_EDE_ENC_TEST_VECTORS,
  1343. des3_ede_speed_template);
  1344. test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
  1345. des3_ede_dec_tv_template,
  1346. DES3_EDE_DEC_TEST_VECTORS,
  1347. des3_ede_speed_template);
  1348. break;
  1349. case 202:
  1350. test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
  1351. twofish_speed_template);
  1352. test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
  1353. twofish_speed_template);
  1354. test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
  1355. twofish_speed_template);
  1356. test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
  1357. twofish_speed_template);
  1358. break;
  1359. case 203:
  1360. test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
  1361. blowfish_speed_template);
  1362. test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
  1363. blowfish_speed_template);
  1364. test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
  1365. blowfish_speed_template);
  1366. test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
  1367. blowfish_speed_template);
  1368. break;
  1369. case 204:
  1370. test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
  1371. des_speed_template);
  1372. test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
  1373. des_speed_template);
  1374. test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
  1375. des_speed_template);
  1376. test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
  1377. des_speed_template);
  1378. break;
  1379. case 205:
  1380. test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
  1381. camellia_speed_template);
  1382. test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
  1383. camellia_speed_template);
  1384. test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
  1385. camellia_speed_template);
  1386. test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
  1387. camellia_speed_template);
  1388. break;
  1389. case 300:
  1390. /* fall through */
  1391. case 301:
  1392. test_hash_speed("md4", sec, generic_hash_speed_template);
  1393. if (mode > 300 && mode < 400) break;
  1394. case 302:
  1395. test_hash_speed("md5", sec, generic_hash_speed_template);
  1396. if (mode > 300 && mode < 400) break;
  1397. case 303:
  1398. test_hash_speed("sha1", sec, generic_hash_speed_template);
  1399. if (mode > 300 && mode < 400) break;
  1400. case 304:
  1401. test_hash_speed("sha256", sec, generic_hash_speed_template);
  1402. if (mode > 300 && mode < 400) break;
  1403. case 305:
  1404. test_hash_speed("sha384", sec, generic_hash_speed_template);
  1405. if (mode > 300 && mode < 400) break;
  1406. case 306:
  1407. test_hash_speed("sha512", sec, generic_hash_speed_template);
  1408. if (mode > 300 && mode < 400) break;
  1409. case 307:
  1410. test_hash_speed("wp256", sec, generic_hash_speed_template);
  1411. if (mode > 300 && mode < 400) break;
  1412. case 308:
  1413. test_hash_speed("wp384", sec, generic_hash_speed_template);
  1414. if (mode > 300 && mode < 400) break;
  1415. case 309:
  1416. test_hash_speed("wp512", sec, generic_hash_speed_template);
  1417. if (mode > 300 && mode < 400) break;
  1418. case 310:
  1419. test_hash_speed("tgr128", sec, generic_hash_speed_template);
  1420. if (mode > 300 && mode < 400) break;
  1421. case 311:
  1422. test_hash_speed("tgr160", sec, generic_hash_speed_template);
  1423. if (mode > 300 && mode < 400) break;
  1424. case 312:
  1425. test_hash_speed("tgr192", sec, generic_hash_speed_template);
  1426. if (mode > 300 && mode < 400) break;
  1427. case 313:
  1428. test_hash_speed("sha224", sec, generic_hash_speed_template);
  1429. if (mode > 300 && mode < 400) break;
  1430. case 399:
  1431. break;
  1432. case 1000:
  1433. test_available();
  1434. break;
  1435. default:
  1436. /* useful for debugging */
  1437. printk("not testing anything\n");
  1438. break;
  1439. }
  1440. }
  1441. static int __init init(void)
  1442. {
  1443. int err = -ENOMEM;
  1444. tvmem = kmalloc(TVMEMSIZE, GFP_KERNEL);
  1445. if (tvmem == NULL)
  1446. return err;
  1447. xbuf = kmalloc(XBUFSIZE, GFP_KERNEL);
  1448. if (xbuf == NULL)
  1449. goto err_free_tv;
  1450. axbuf = kmalloc(XBUFSIZE, GFP_KERNEL);
  1451. if (axbuf == NULL)
  1452. goto err_free_xbuf;
  1453. do_test();
  1454. /* We intentionaly return -EAGAIN to prevent keeping
  1455. * the module. It does all its work from init()
  1456. * and doesn't offer any runtime functionality
  1457. * => we don't need it in the memory, do we?
  1458. * -- mludvig
  1459. */
  1460. err = -EAGAIN;
  1461. kfree(axbuf);
  1462. err_free_xbuf:
  1463. kfree(xbuf);
  1464. err_free_tv:
  1465. kfree(tvmem);
  1466. return err;
  1467. }
  1468. /*
  1469. * If an init function is provided, an exit function must also be provided
  1470. * to allow module unload.
  1471. */
  1472. static void __exit fini(void) { }
  1473. module_init(init);
  1474. module_exit(fini);
  1475. module_param(mode, int, 0);
  1476. module_param(sec, uint, 0);
  1477. MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
  1478. "(defaults to zero which uses CPU cycles instead)");
  1479. MODULE_LICENSE("GPL");
  1480. MODULE_DESCRIPTION("Quick & dirty crypto testing module");
  1481. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");