tcrypt.c 44 KB

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