tcrypt.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599
  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. */
  17. #include <crypto/hash.h>
  18. #include <linux/err.h>
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/mm.h>
  22. #include <linux/slab.h>
  23. #include <linux/scatterlist.h>
  24. #include <linux/string.h>
  25. #include <linux/crypto.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/timex.h>
  29. #include <linux/interrupt.h>
  30. #include "tcrypt.h"
  31. /*
  32. * Need slab memory for testing (size in number of pages).
  33. */
  34. #define TVMEMSIZE 4
  35. #define XBUFSIZE 8
  36. /*
  37. * Indexes into the xbuf to simulate cross-page access.
  38. */
  39. #define IDX1 32
  40. #define IDX2 32400
  41. #define IDX3 1
  42. #define IDX4 8193
  43. #define IDX5 22222
  44. #define IDX6 17101
  45. #define IDX7 27333
  46. #define IDX8 3000
  47. /*
  48. * Used by test_cipher()
  49. */
  50. #define ENCRYPT 1
  51. #define DECRYPT 0
  52. struct tcrypt_result {
  53. struct completion completion;
  54. int err;
  55. };
  56. struct aead_test_suite {
  57. struct {
  58. struct aead_testvec *vecs;
  59. unsigned int count;
  60. } enc, dec;
  61. };
  62. struct cipher_test_suite {
  63. struct {
  64. struct cipher_testvec *vecs;
  65. unsigned int count;
  66. } enc, dec;
  67. };
  68. struct comp_test_suite {
  69. struct {
  70. struct comp_testvec *vecs;
  71. unsigned int count;
  72. } comp, decomp;
  73. };
  74. struct hash_test_suite {
  75. struct hash_testvec *vecs;
  76. unsigned int count;
  77. };
  78. struct alg_test_desc {
  79. const char *alg;
  80. int (*test)(const struct alg_test_desc *desc, const char *driver,
  81. u32 type, u32 mask);
  82. union {
  83. struct aead_test_suite aead;
  84. struct cipher_test_suite cipher;
  85. struct comp_test_suite comp;
  86. struct hash_test_suite hash;
  87. } suite;
  88. };
  89. static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
  90. /*
  91. * Used by test_cipher_speed()
  92. */
  93. static unsigned int sec;
  94. static int mode;
  95. static char *xbuf[XBUFSIZE];
  96. static char *axbuf[XBUFSIZE];
  97. static char *tvmem[TVMEMSIZE];
  98. static char *check[] = {
  99. "des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
  100. "blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
  101. "cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
  102. "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", "fcrypt",
  103. "camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
  104. "lzo", "cts", NULL
  105. };
  106. static void hexdump(unsigned char *buf, unsigned int len)
  107. {
  108. print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
  109. 16, 1,
  110. buf, len, false);
  111. }
  112. static void tcrypt_complete(struct crypto_async_request *req, int err)
  113. {
  114. struct tcrypt_result *res = req->data;
  115. if (err == -EINPROGRESS)
  116. return;
  117. res->err = err;
  118. complete(&res->completion);
  119. }
  120. static int test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
  121. unsigned int tcount)
  122. {
  123. const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
  124. unsigned int i, j, k, temp;
  125. struct scatterlist sg[8];
  126. char result[64];
  127. struct ahash_request *req;
  128. struct tcrypt_result tresult;
  129. int ret;
  130. void *hash_buff;
  131. init_completion(&tresult.completion);
  132. req = ahash_request_alloc(tfm, GFP_KERNEL);
  133. if (!req) {
  134. printk(KERN_ERR "alg: hash: Failed to allocate request for "
  135. "%s\n", algo);
  136. ret = -ENOMEM;
  137. goto out_noreq;
  138. }
  139. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  140. tcrypt_complete, &tresult);
  141. for (i = 0; i < tcount; i++) {
  142. memset(result, 0, 64);
  143. hash_buff = xbuf[0];
  144. memcpy(hash_buff, template[i].plaintext, template[i].psize);
  145. sg_init_one(&sg[0], hash_buff, template[i].psize);
  146. if (template[i].ksize) {
  147. crypto_ahash_clear_flags(tfm, ~0);
  148. ret = crypto_ahash_setkey(tfm, template[i].key,
  149. template[i].ksize);
  150. if (ret) {
  151. printk(KERN_ERR "alg: hash: setkey failed on "
  152. "test %d for %s: ret=%d\n", i + 1, algo,
  153. -ret);
  154. goto out;
  155. }
  156. }
  157. ahash_request_set_crypt(req, sg, result, template[i].psize);
  158. ret = crypto_ahash_digest(req);
  159. switch (ret) {
  160. case 0:
  161. break;
  162. case -EINPROGRESS:
  163. case -EBUSY:
  164. ret = wait_for_completion_interruptible(
  165. &tresult.completion);
  166. if (!ret && !(ret = tresult.err)) {
  167. INIT_COMPLETION(tresult.completion);
  168. break;
  169. }
  170. /* fall through */
  171. default:
  172. printk(KERN_ERR "alg: hash: digest failed on test %d "
  173. "for %s: ret=%d\n", i + 1, algo, -ret);
  174. goto out;
  175. }
  176. if (memcmp(result, template[i].digest,
  177. crypto_ahash_digestsize(tfm))) {
  178. printk(KERN_ERR "alg: hash: Test %d failed for %s\n",
  179. i + 1, algo);
  180. hexdump(result, crypto_ahash_digestsize(tfm));
  181. ret = -EINVAL;
  182. goto out;
  183. }
  184. }
  185. j = 0;
  186. for (i = 0; i < tcount; i++) {
  187. if (template[i].np) {
  188. j++;
  189. memset(result, 0, 64);
  190. temp = 0;
  191. sg_init_table(sg, template[i].np);
  192. for (k = 0; k < template[i].np; k++) {
  193. sg_set_buf(&sg[k],
  194. memcpy(xbuf[IDX[k] >> PAGE_SHIFT] +
  195. offset_in_page(IDX[k]),
  196. template[i].plaintext + temp,
  197. template[i].tap[k]),
  198. template[i].tap[k]);
  199. temp += template[i].tap[k];
  200. }
  201. if (template[i].ksize) {
  202. crypto_ahash_clear_flags(tfm, ~0);
  203. ret = crypto_ahash_setkey(tfm, template[i].key,
  204. template[i].ksize);
  205. if (ret) {
  206. printk(KERN_ERR "alg: hash: setkey "
  207. "failed on chunking test %d "
  208. "for %s: ret=%d\n", j, algo,
  209. -ret);
  210. goto out;
  211. }
  212. }
  213. ahash_request_set_crypt(req, sg, result,
  214. template[i].psize);
  215. ret = crypto_ahash_digest(req);
  216. switch (ret) {
  217. case 0:
  218. break;
  219. case -EINPROGRESS:
  220. case -EBUSY:
  221. ret = wait_for_completion_interruptible(
  222. &tresult.completion);
  223. if (!ret && !(ret = tresult.err)) {
  224. INIT_COMPLETION(tresult.completion);
  225. break;
  226. }
  227. /* fall through */
  228. default:
  229. printk(KERN_ERR "alg: hash: digest failed "
  230. "on chunking test %d for %s: "
  231. "ret=%d\n", j, algo, -ret);
  232. goto out;
  233. }
  234. if (memcmp(result, template[i].digest,
  235. crypto_ahash_digestsize(tfm))) {
  236. printk(KERN_ERR "alg: hash: Chunking test %d "
  237. "failed for %s\n", j, algo);
  238. hexdump(result, crypto_ahash_digestsize(tfm));
  239. ret = -EINVAL;
  240. goto out;
  241. }
  242. }
  243. }
  244. ret = 0;
  245. out:
  246. ahash_request_free(req);
  247. out_noreq:
  248. return ret;
  249. }
  250. static int test_aead(struct crypto_aead *tfm, int enc,
  251. struct aead_testvec *template, unsigned int tcount)
  252. {
  253. const char *algo = crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));
  254. unsigned int i, j, k, n, temp;
  255. int ret = 0;
  256. char *q;
  257. char *key;
  258. struct aead_request *req;
  259. struct scatterlist sg[8];
  260. struct scatterlist asg[8];
  261. const char *e;
  262. struct tcrypt_result result;
  263. unsigned int authsize;
  264. void *input;
  265. void *assoc;
  266. char iv[MAX_IVLEN];
  267. if (enc == ENCRYPT)
  268. e = "encryption";
  269. else
  270. e = "decryption";
  271. init_completion(&result.completion);
  272. req = aead_request_alloc(tfm, GFP_KERNEL);
  273. if (!req) {
  274. printk(KERN_ERR "alg: aead: Failed to allocate request for "
  275. "%s\n", algo);
  276. ret = -ENOMEM;
  277. goto out;
  278. }
  279. aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  280. tcrypt_complete, &result);
  281. for (i = 0, j = 0; i < tcount; i++) {
  282. if (!template[i].np) {
  283. j++;
  284. /* some tepmplates have no input data but they will
  285. * touch input
  286. */
  287. input = xbuf[0];
  288. assoc = axbuf[0];
  289. memcpy(input, template[i].input, template[i].ilen);
  290. memcpy(assoc, template[i].assoc, template[i].alen);
  291. if (template[i].iv)
  292. memcpy(iv, template[i].iv, MAX_IVLEN);
  293. else
  294. memset(iv, 0, MAX_IVLEN);
  295. crypto_aead_clear_flags(tfm, ~0);
  296. if (template[i].wk)
  297. crypto_aead_set_flags(
  298. tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  299. key = template[i].key;
  300. ret = crypto_aead_setkey(tfm, key,
  301. template[i].klen);
  302. if (!ret == template[i].fail) {
  303. printk(KERN_ERR "alg: aead: setkey failed on "
  304. "test %d for %s: flags=%x\n", j, algo,
  305. crypto_aead_get_flags(tfm));
  306. goto out;
  307. } else if (ret)
  308. continue;
  309. authsize = abs(template[i].rlen - template[i].ilen);
  310. ret = crypto_aead_setauthsize(tfm, authsize);
  311. if (ret) {
  312. printk(KERN_ERR "alg: aead: Failed to set "
  313. "authsize to %u on test %d for %s\n",
  314. authsize, j, algo);
  315. goto out;
  316. }
  317. sg_init_one(&sg[0], input,
  318. template[i].ilen + (enc ? authsize : 0));
  319. sg_init_one(&asg[0], assoc, template[i].alen);
  320. aead_request_set_crypt(req, sg, sg,
  321. template[i].ilen, iv);
  322. aead_request_set_assoc(req, asg, template[i].alen);
  323. ret = enc ?
  324. crypto_aead_encrypt(req) :
  325. crypto_aead_decrypt(req);
  326. switch (ret) {
  327. case 0:
  328. break;
  329. case -EINPROGRESS:
  330. case -EBUSY:
  331. ret = wait_for_completion_interruptible(
  332. &result.completion);
  333. if (!ret && !(ret = result.err)) {
  334. INIT_COMPLETION(result.completion);
  335. break;
  336. }
  337. /* fall through */
  338. default:
  339. printk(KERN_ERR "alg: aead: %s failed on test "
  340. "%d for %s: ret=%d\n", e, j, algo, -ret);
  341. goto out;
  342. }
  343. q = input;
  344. if (memcmp(q, template[i].result, template[i].rlen)) {
  345. printk(KERN_ERR "alg: aead: Test %d failed on "
  346. "%s for %s\n", j, e, algo);
  347. hexdump(q, template[i].rlen);
  348. ret = -EINVAL;
  349. goto out;
  350. }
  351. }
  352. }
  353. for (i = 0, j = 0; i < tcount; i++) {
  354. if (template[i].np) {
  355. j++;
  356. if (template[i].iv)
  357. memcpy(iv, template[i].iv, MAX_IVLEN);
  358. else
  359. memset(iv, 0, MAX_IVLEN);
  360. crypto_aead_clear_flags(tfm, ~0);
  361. if (template[i].wk)
  362. crypto_aead_set_flags(
  363. tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  364. key = template[i].key;
  365. ret = crypto_aead_setkey(tfm, key, template[i].klen);
  366. if (!ret == template[i].fail) {
  367. printk(KERN_ERR "alg: aead: setkey failed on "
  368. "chunk test %d for %s: flags=%x\n", j,
  369. algo, crypto_aead_get_flags(tfm));
  370. goto out;
  371. } else if (ret)
  372. continue;
  373. authsize = abs(template[i].rlen - template[i].ilen);
  374. ret = -EINVAL;
  375. sg_init_table(sg, template[i].np);
  376. for (k = 0, temp = 0; k < template[i].np; k++) {
  377. if (WARN_ON(offset_in_page(IDX[k]) +
  378. template[i].tap[k] > PAGE_SIZE))
  379. goto out;
  380. q = xbuf[IDX[k] >> PAGE_SHIFT] +
  381. offset_in_page(IDX[k]);
  382. memcpy(q, template[i].input + temp,
  383. template[i].tap[k]);
  384. n = template[i].tap[k];
  385. if (k == template[i].np - 1 && enc)
  386. n += authsize;
  387. if (offset_in_page(q) + n < PAGE_SIZE)
  388. q[n] = 0;
  389. sg_set_buf(&sg[k], q, template[i].tap[k]);
  390. temp += template[i].tap[k];
  391. }
  392. ret = crypto_aead_setauthsize(tfm, authsize);
  393. if (ret) {
  394. printk(KERN_ERR "alg: aead: Failed to set "
  395. "authsize to %u on chunk test %d for "
  396. "%s\n", authsize, j, algo);
  397. goto out;
  398. }
  399. if (enc) {
  400. if (WARN_ON(sg[k - 1].offset +
  401. sg[k - 1].length + authsize >
  402. PAGE_SIZE)) {
  403. ret = -EINVAL;
  404. goto out;
  405. }
  406. sg[k - 1].length += authsize;
  407. }
  408. sg_init_table(asg, template[i].anp);
  409. for (k = 0, temp = 0; k < template[i].anp; k++) {
  410. sg_set_buf(&asg[k],
  411. memcpy(axbuf[IDX[k] >> PAGE_SHIFT] +
  412. offset_in_page(IDX[k]),
  413. template[i].assoc + temp,
  414. template[i].atap[k]),
  415. template[i].atap[k]);
  416. temp += template[i].atap[k];
  417. }
  418. aead_request_set_crypt(req, sg, sg,
  419. template[i].ilen,
  420. iv);
  421. aead_request_set_assoc(req, asg, template[i].alen);
  422. ret = enc ?
  423. crypto_aead_encrypt(req) :
  424. crypto_aead_decrypt(req);
  425. switch (ret) {
  426. case 0:
  427. break;
  428. case -EINPROGRESS:
  429. case -EBUSY:
  430. ret = wait_for_completion_interruptible(
  431. &result.completion);
  432. if (!ret && !(ret = result.err)) {
  433. INIT_COMPLETION(result.completion);
  434. break;
  435. }
  436. /* fall through */
  437. default:
  438. printk(KERN_ERR "alg: aead: %s failed on "
  439. "chunk test %d for %s: ret=%d\n", e, j,
  440. algo, -ret);
  441. goto out;
  442. }
  443. ret = -EINVAL;
  444. for (k = 0, temp = 0; k < template[i].np; k++) {
  445. q = xbuf[IDX[k] >> PAGE_SHIFT] +
  446. offset_in_page(IDX[k]);
  447. n = template[i].tap[k];
  448. if (k == template[i].np - 1)
  449. n += enc ? authsize : -authsize;
  450. if (memcmp(q, template[i].result + temp, n)) {
  451. printk(KERN_ERR "alg: aead: Chunk "
  452. "test %d failed on %s at page "
  453. "%u for %s\n", j, e, k, algo);
  454. hexdump(q, n);
  455. goto out;
  456. }
  457. q += n;
  458. if (k == template[i].np - 1 && !enc) {
  459. if (memcmp(q, template[i].input +
  460. temp + n, authsize))
  461. n = authsize;
  462. else
  463. n = 0;
  464. } else {
  465. for (n = 0; offset_in_page(q + n) &&
  466. q[n]; n++)
  467. ;
  468. }
  469. if (n) {
  470. printk(KERN_ERR "alg: aead: Result "
  471. "buffer corruption in chunk "
  472. "test %d on %s at page %u for "
  473. "%s: %u bytes:\n", j, e, k,
  474. algo, n);
  475. hexdump(q, n);
  476. goto out;
  477. }
  478. temp += template[i].tap[k];
  479. }
  480. }
  481. }
  482. ret = 0;
  483. out:
  484. aead_request_free(req);
  485. return ret;
  486. }
  487. static int test_cipher(struct crypto_ablkcipher *tfm, int enc,
  488. struct cipher_testvec *template, unsigned int tcount)
  489. {
  490. const char *algo =
  491. crypto_tfm_alg_driver_name(crypto_ablkcipher_tfm(tfm));
  492. unsigned int i, j, k, n, temp;
  493. int ret;
  494. char *q;
  495. struct ablkcipher_request *req;
  496. struct scatterlist sg[8];
  497. const char *e;
  498. struct tcrypt_result result;
  499. void *data;
  500. char iv[MAX_IVLEN];
  501. if (enc == ENCRYPT)
  502. e = "encryption";
  503. else
  504. e = "decryption";
  505. init_completion(&result.completion);
  506. req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
  507. if (!req) {
  508. printk(KERN_ERR "alg: cipher: Failed to allocate request for "
  509. "%s\n", algo);
  510. ret = -ENOMEM;
  511. goto out;
  512. }
  513. ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  514. tcrypt_complete, &result);
  515. j = 0;
  516. for (i = 0; i < tcount; i++) {
  517. if (template[i].iv)
  518. memcpy(iv, template[i].iv, MAX_IVLEN);
  519. else
  520. memset(iv, 0, MAX_IVLEN);
  521. if (!(template[i].np)) {
  522. j++;
  523. data = xbuf[0];
  524. memcpy(data, template[i].input, template[i].ilen);
  525. crypto_ablkcipher_clear_flags(tfm, ~0);
  526. if (template[i].wk)
  527. crypto_ablkcipher_set_flags(
  528. tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  529. ret = crypto_ablkcipher_setkey(tfm, template[i].key,
  530. template[i].klen);
  531. if (!ret == template[i].fail) {
  532. printk(KERN_ERR "alg: cipher: setkey failed "
  533. "on test %d for %s: flags=%x\n", j,
  534. algo, crypto_ablkcipher_get_flags(tfm));
  535. goto out;
  536. } else if (ret)
  537. continue;
  538. sg_init_one(&sg[0], data, template[i].ilen);
  539. ablkcipher_request_set_crypt(req, sg, sg,
  540. template[i].ilen, iv);
  541. ret = enc ?
  542. crypto_ablkcipher_encrypt(req) :
  543. crypto_ablkcipher_decrypt(req);
  544. switch (ret) {
  545. case 0:
  546. break;
  547. case -EINPROGRESS:
  548. case -EBUSY:
  549. ret = wait_for_completion_interruptible(
  550. &result.completion);
  551. if (!ret && !((ret = result.err))) {
  552. INIT_COMPLETION(result.completion);
  553. break;
  554. }
  555. /* fall through */
  556. default:
  557. printk(KERN_ERR "alg: cipher: %s failed on "
  558. "test %d for %s: ret=%d\n", e, j, algo,
  559. -ret);
  560. goto out;
  561. }
  562. q = data;
  563. if (memcmp(q, template[i].result, template[i].rlen)) {
  564. printk(KERN_ERR "alg: cipher: Test %d failed "
  565. "on %s for %s\n", j, e, algo);
  566. hexdump(q, template[i].rlen);
  567. ret = -EINVAL;
  568. goto out;
  569. }
  570. }
  571. }
  572. j = 0;
  573. for (i = 0; i < tcount; i++) {
  574. if (template[i].iv)
  575. memcpy(iv, template[i].iv, MAX_IVLEN);
  576. else
  577. memset(iv, 0, MAX_IVLEN);
  578. if (template[i].np) {
  579. j++;
  580. crypto_ablkcipher_clear_flags(tfm, ~0);
  581. if (template[i].wk)
  582. crypto_ablkcipher_set_flags(
  583. tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  584. ret = crypto_ablkcipher_setkey(tfm, template[i].key,
  585. template[i].klen);
  586. if (!ret == template[i].fail) {
  587. printk(KERN_ERR "alg: cipher: setkey failed "
  588. "on chunk test %d for %s: flags=%x\n",
  589. j, algo,
  590. crypto_ablkcipher_get_flags(tfm));
  591. goto out;
  592. } else if (ret)
  593. continue;
  594. temp = 0;
  595. ret = -EINVAL;
  596. sg_init_table(sg, template[i].np);
  597. for (k = 0; k < template[i].np; k++) {
  598. if (WARN_ON(offset_in_page(IDX[k]) +
  599. template[i].tap[k] > PAGE_SIZE))
  600. goto out;
  601. q = xbuf[IDX[k] >> PAGE_SHIFT] +
  602. offset_in_page(IDX[k]);
  603. memcpy(q, template[i].input + temp,
  604. template[i].tap[k]);
  605. if (offset_in_page(q) + template[i].tap[k] <
  606. PAGE_SIZE)
  607. q[template[i].tap[k]] = 0;
  608. sg_set_buf(&sg[k], q, template[i].tap[k]);
  609. temp += template[i].tap[k];
  610. }
  611. ablkcipher_request_set_crypt(req, sg, sg,
  612. template[i].ilen, iv);
  613. ret = enc ?
  614. crypto_ablkcipher_encrypt(req) :
  615. crypto_ablkcipher_decrypt(req);
  616. switch (ret) {
  617. case 0:
  618. break;
  619. case -EINPROGRESS:
  620. case -EBUSY:
  621. ret = wait_for_completion_interruptible(
  622. &result.completion);
  623. if (!ret && !((ret = result.err))) {
  624. INIT_COMPLETION(result.completion);
  625. break;
  626. }
  627. /* fall through */
  628. default:
  629. printk(KERN_ERR "alg: cipher: %s failed on "
  630. "chunk test %d for %s: ret=%d\n", e, j,
  631. algo, -ret);
  632. goto out;
  633. }
  634. temp = 0;
  635. ret = -EINVAL;
  636. for (k = 0; k < template[i].np; k++) {
  637. q = xbuf[IDX[k] >> PAGE_SHIFT] +
  638. offset_in_page(IDX[k]);
  639. if (memcmp(q, template[i].result + temp,
  640. template[i].tap[k])) {
  641. printk(KERN_ERR "alg: cipher: Chunk "
  642. "test %d failed on %s at page "
  643. "%u for %s\n", j, e, k, algo);
  644. hexdump(q, template[i].tap[k]);
  645. goto out;
  646. }
  647. q += template[i].tap[k];
  648. for (n = 0; offset_in_page(q + n) && q[n]; n++)
  649. ;
  650. if (n) {
  651. printk(KERN_ERR "alg: cipher: "
  652. "Result buffer corruption in "
  653. "chunk test %d on %s at page "
  654. "%u for %s: %u bytes:\n", j, e,
  655. k, algo, n);
  656. hexdump(q, n);
  657. goto out;
  658. }
  659. temp += template[i].tap[k];
  660. }
  661. }
  662. }
  663. ret = 0;
  664. out:
  665. ablkcipher_request_free(req);
  666. return ret;
  667. }
  668. static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc,
  669. struct scatterlist *sg, int blen, int sec)
  670. {
  671. unsigned long start, end;
  672. int bcount;
  673. int ret;
  674. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  675. time_before(jiffies, end); bcount++) {
  676. if (enc)
  677. ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
  678. else
  679. ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
  680. if (ret)
  681. return ret;
  682. }
  683. printk("%d operations in %d seconds (%ld bytes)\n",
  684. bcount, sec, (long)bcount * blen);
  685. return 0;
  686. }
  687. static int test_cipher_cycles(struct blkcipher_desc *desc, int enc,
  688. struct scatterlist *sg, int blen)
  689. {
  690. unsigned long cycles = 0;
  691. int ret = 0;
  692. int i;
  693. local_bh_disable();
  694. local_irq_disable();
  695. /* Warm-up run. */
  696. for (i = 0; i < 4; i++) {
  697. if (enc)
  698. ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
  699. else
  700. ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
  701. if (ret)
  702. goto out;
  703. }
  704. /* The real thing. */
  705. for (i = 0; i < 8; i++) {
  706. cycles_t start, end;
  707. start = get_cycles();
  708. if (enc)
  709. ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
  710. else
  711. ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
  712. end = get_cycles();
  713. if (ret)
  714. goto out;
  715. cycles += end - start;
  716. }
  717. out:
  718. local_irq_enable();
  719. local_bh_enable();
  720. if (ret == 0)
  721. printk("1 operation in %lu cycles (%d bytes)\n",
  722. (cycles + 4) / 8, blen);
  723. return ret;
  724. }
  725. static u32 block_sizes[] = { 16, 64, 256, 1024, 8192, 0 };
  726. static void test_cipher_speed(const char *algo, int enc, unsigned int sec,
  727. struct cipher_testvec *template,
  728. unsigned int tcount, u8 *keysize)
  729. {
  730. unsigned int ret, i, j, iv_len;
  731. unsigned char *key, iv[128];
  732. struct crypto_blkcipher *tfm;
  733. struct blkcipher_desc desc;
  734. const char *e;
  735. u32 *b_size;
  736. if (enc == ENCRYPT)
  737. e = "encryption";
  738. else
  739. e = "decryption";
  740. printk("\ntesting speed of %s %s\n", algo, e);
  741. tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
  742. if (IS_ERR(tfm)) {
  743. printk("failed to load transform for %s: %ld\n", algo,
  744. PTR_ERR(tfm));
  745. return;
  746. }
  747. desc.tfm = tfm;
  748. desc.flags = 0;
  749. i = 0;
  750. do {
  751. b_size = block_sizes;
  752. do {
  753. struct scatterlist sg[TVMEMSIZE];
  754. if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
  755. printk("template (%u) too big for "
  756. "tvmem (%lu)\n", *keysize + *b_size,
  757. TVMEMSIZE * PAGE_SIZE);
  758. goto out;
  759. }
  760. printk("test %u (%d bit key, %d byte blocks): ", i,
  761. *keysize * 8, *b_size);
  762. memset(tvmem[0], 0xff, PAGE_SIZE);
  763. /* set key, plain text and IV */
  764. key = (unsigned char *)tvmem[0];
  765. for (j = 0; j < tcount; j++) {
  766. if (template[j].klen == *keysize) {
  767. key = template[j].key;
  768. break;
  769. }
  770. }
  771. ret = crypto_blkcipher_setkey(tfm, key, *keysize);
  772. if (ret) {
  773. printk("setkey() failed flags=%x\n",
  774. crypto_blkcipher_get_flags(tfm));
  775. goto out;
  776. }
  777. sg_init_table(sg, TVMEMSIZE);
  778. sg_set_buf(sg, tvmem[0] + *keysize,
  779. PAGE_SIZE - *keysize);
  780. for (j = 1; j < TVMEMSIZE; j++) {
  781. sg_set_buf(sg + j, tvmem[j], PAGE_SIZE);
  782. memset (tvmem[j], 0xff, PAGE_SIZE);
  783. }
  784. iv_len = crypto_blkcipher_ivsize(tfm);
  785. if (iv_len) {
  786. memset(&iv, 0xff, iv_len);
  787. crypto_blkcipher_set_iv(tfm, iv, iv_len);
  788. }
  789. if (sec)
  790. ret = test_cipher_jiffies(&desc, enc, sg,
  791. *b_size, sec);
  792. else
  793. ret = test_cipher_cycles(&desc, enc, sg,
  794. *b_size);
  795. if (ret) {
  796. printk("%s() failed flags=%x\n", e, desc.flags);
  797. break;
  798. }
  799. b_size++;
  800. i++;
  801. } while (*b_size);
  802. keysize++;
  803. } while (*keysize);
  804. out:
  805. crypto_free_blkcipher(tfm);
  806. }
  807. static int test_hash_jiffies_digest(struct hash_desc *desc,
  808. struct scatterlist *sg, int blen,
  809. char *out, int sec)
  810. {
  811. unsigned long start, end;
  812. int bcount;
  813. int ret;
  814. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  815. time_before(jiffies, end); bcount++) {
  816. ret = crypto_hash_digest(desc, sg, blen, out);
  817. if (ret)
  818. return ret;
  819. }
  820. printk("%6u opers/sec, %9lu bytes/sec\n",
  821. bcount / sec, ((long)bcount * blen) / sec);
  822. return 0;
  823. }
  824. static int test_hash_jiffies(struct hash_desc *desc, struct scatterlist *sg,
  825. int blen, int plen, char *out, int sec)
  826. {
  827. unsigned long start, end;
  828. int bcount, pcount;
  829. int ret;
  830. if (plen == blen)
  831. return test_hash_jiffies_digest(desc, sg, blen, out, sec);
  832. for (start = jiffies, end = start + sec * HZ, bcount = 0;
  833. time_before(jiffies, end); bcount++) {
  834. ret = crypto_hash_init(desc);
  835. if (ret)
  836. return ret;
  837. for (pcount = 0; pcount < blen; pcount += plen) {
  838. ret = crypto_hash_update(desc, sg, plen);
  839. if (ret)
  840. return ret;
  841. }
  842. /* we assume there is enough space in 'out' for the result */
  843. ret = crypto_hash_final(desc, out);
  844. if (ret)
  845. return ret;
  846. }
  847. printk("%6u opers/sec, %9lu bytes/sec\n",
  848. bcount / sec, ((long)bcount * blen) / sec);
  849. return 0;
  850. }
  851. static int test_hash_cycles_digest(struct hash_desc *desc,
  852. struct scatterlist *sg, int blen, char *out)
  853. {
  854. unsigned long cycles = 0;
  855. int i;
  856. int ret;
  857. local_bh_disable();
  858. local_irq_disable();
  859. /* Warm-up run. */
  860. for (i = 0; i < 4; i++) {
  861. ret = crypto_hash_digest(desc, sg, blen, out);
  862. if (ret)
  863. goto out;
  864. }
  865. /* The real thing. */
  866. for (i = 0; i < 8; i++) {
  867. cycles_t start, end;
  868. start = get_cycles();
  869. ret = crypto_hash_digest(desc, sg, blen, out);
  870. if (ret)
  871. goto out;
  872. end = get_cycles();
  873. cycles += end - start;
  874. }
  875. out:
  876. local_irq_enable();
  877. local_bh_enable();
  878. if (ret)
  879. return ret;
  880. printk("%6lu cycles/operation, %4lu cycles/byte\n",
  881. cycles / 8, cycles / (8 * blen));
  882. return 0;
  883. }
  884. static int test_hash_cycles(struct hash_desc *desc, struct scatterlist *sg,
  885. int blen, int plen, char *out)
  886. {
  887. unsigned long cycles = 0;
  888. int i, pcount;
  889. int ret;
  890. if (plen == blen)
  891. return test_hash_cycles_digest(desc, sg, blen, out);
  892. local_bh_disable();
  893. local_irq_disable();
  894. /* Warm-up run. */
  895. for (i = 0; i < 4; i++) {
  896. ret = crypto_hash_init(desc);
  897. if (ret)
  898. goto out;
  899. for (pcount = 0; pcount < blen; pcount += plen) {
  900. ret = crypto_hash_update(desc, sg, plen);
  901. if (ret)
  902. goto out;
  903. }
  904. ret = crypto_hash_final(desc, out);
  905. if (ret)
  906. goto out;
  907. }
  908. /* The real thing. */
  909. for (i = 0; i < 8; i++) {
  910. cycles_t start, end;
  911. start = get_cycles();
  912. ret = crypto_hash_init(desc);
  913. if (ret)
  914. goto out;
  915. for (pcount = 0; pcount < blen; pcount += plen) {
  916. ret = crypto_hash_update(desc, sg, plen);
  917. if (ret)
  918. goto out;
  919. }
  920. ret = crypto_hash_final(desc, out);
  921. if (ret)
  922. goto out;
  923. end = get_cycles();
  924. cycles += end - start;
  925. }
  926. out:
  927. local_irq_enable();
  928. local_bh_enable();
  929. if (ret)
  930. return ret;
  931. printk("%6lu cycles/operation, %4lu cycles/byte\n",
  932. cycles / 8, cycles / (8 * blen));
  933. return 0;
  934. }
  935. static void test_hash_speed(const char *algo, unsigned int sec,
  936. struct hash_speed *speed)
  937. {
  938. struct scatterlist sg[TVMEMSIZE];
  939. struct crypto_hash *tfm;
  940. struct hash_desc desc;
  941. char output[1024];
  942. int i;
  943. int ret;
  944. printk("\ntesting speed of %s\n", algo);
  945. tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
  946. if (IS_ERR(tfm)) {
  947. printk("failed to load transform for %s: %ld\n", algo,
  948. PTR_ERR(tfm));
  949. return;
  950. }
  951. desc.tfm = tfm;
  952. desc.flags = 0;
  953. if (crypto_hash_digestsize(tfm) > sizeof(output)) {
  954. printk("digestsize(%u) > outputbuffer(%zu)\n",
  955. crypto_hash_digestsize(tfm), sizeof(output));
  956. goto out;
  957. }
  958. sg_init_table(sg, TVMEMSIZE);
  959. for (i = 0; i < TVMEMSIZE; i++) {
  960. sg_set_buf(sg + i, tvmem[i], PAGE_SIZE);
  961. memset(tvmem[i], 0xff, PAGE_SIZE);
  962. }
  963. for (i = 0; speed[i].blen != 0; i++) {
  964. if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
  965. printk("template (%u) too big for tvmem (%lu)\n",
  966. speed[i].blen, TVMEMSIZE * PAGE_SIZE);
  967. goto out;
  968. }
  969. printk("test%3u (%5u byte blocks,%5u bytes per update,%4u updates): ",
  970. i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
  971. if (sec)
  972. ret = test_hash_jiffies(&desc, sg, speed[i].blen,
  973. speed[i].plen, output, sec);
  974. else
  975. ret = test_hash_cycles(&desc, sg, speed[i].blen,
  976. speed[i].plen, output);
  977. if (ret) {
  978. printk("hashing failed ret=%d\n", ret);
  979. break;
  980. }
  981. }
  982. out:
  983. crypto_free_hash(tfm);
  984. }
  985. static int test_comp(struct crypto_comp *tfm, struct comp_testvec *ctemplate,
  986. struct comp_testvec *dtemplate, int ctcount, int dtcount)
  987. {
  988. const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
  989. unsigned int i;
  990. char result[COMP_BUF_SIZE];
  991. int ret;
  992. for (i = 0; i < ctcount; i++) {
  993. int ilen, dlen = COMP_BUF_SIZE;
  994. memset(result, 0, sizeof (result));
  995. ilen = ctemplate[i].inlen;
  996. ret = crypto_comp_compress(tfm, ctemplate[i].input,
  997. ilen, result, &dlen);
  998. if (ret) {
  999. printk(KERN_ERR "alg: comp: compression failed "
  1000. "on test %d for %s: ret=%d\n", i + 1, algo,
  1001. -ret);
  1002. goto out;
  1003. }
  1004. if (memcmp(result, ctemplate[i].output, dlen)) {
  1005. printk(KERN_ERR "alg: comp: Compression test %d "
  1006. "failed for %s\n", i + 1, algo);
  1007. hexdump(result, dlen);
  1008. ret = -EINVAL;
  1009. goto out;
  1010. }
  1011. }
  1012. for (i = 0; i < dtcount; i++) {
  1013. int ilen, ret, dlen = COMP_BUF_SIZE;
  1014. memset(result, 0, sizeof (result));
  1015. ilen = dtemplate[i].inlen;
  1016. ret = crypto_comp_decompress(tfm, dtemplate[i].input,
  1017. ilen, result, &dlen);
  1018. if (ret) {
  1019. printk(KERN_ERR "alg: comp: decompression failed "
  1020. "on test %d for %s: ret=%d\n", i + 1, algo,
  1021. -ret);
  1022. goto out;
  1023. }
  1024. if (memcmp(result, dtemplate[i].output, dlen)) {
  1025. printk(KERN_ERR "alg: comp: Decompression test %d "
  1026. "failed for %s\n", i + 1, algo);
  1027. hexdump(result, dlen);
  1028. ret = -EINVAL;
  1029. goto out;
  1030. }
  1031. }
  1032. ret = 0;
  1033. out:
  1034. return ret;
  1035. }
  1036. static void test_available(void)
  1037. {
  1038. char **name = check;
  1039. while (*name) {
  1040. printk("alg %s ", *name);
  1041. printk(crypto_has_alg(*name, 0, 0) ?
  1042. "found\n" : "not found\n");
  1043. name++;
  1044. }
  1045. }
  1046. static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
  1047. u32 type, u32 mask)
  1048. {
  1049. struct crypto_aead *tfm;
  1050. int err = 0;
  1051. tfm = crypto_alloc_aead(driver, type, mask);
  1052. if (IS_ERR(tfm)) {
  1053. printk(KERN_ERR "alg: aead: Failed to load transform for %s: "
  1054. "%ld\n", driver, PTR_ERR(tfm));
  1055. return PTR_ERR(tfm);
  1056. }
  1057. if (desc->suite.aead.enc.vecs) {
  1058. err = test_aead(tfm, ENCRYPT, desc->suite.aead.enc.vecs,
  1059. desc->suite.aead.enc.count);
  1060. if (err)
  1061. goto out;
  1062. }
  1063. if (!err && desc->suite.aead.dec.vecs)
  1064. err = test_aead(tfm, DECRYPT, desc->suite.aead.dec.vecs,
  1065. desc->suite.aead.dec.count);
  1066. out:
  1067. crypto_free_aead(tfm);
  1068. return err;
  1069. }
  1070. static int alg_test_cipher(const struct alg_test_desc *desc,
  1071. const char *driver, u32 type, u32 mask)
  1072. {
  1073. struct crypto_ablkcipher *tfm;
  1074. int err = 0;
  1075. tfm = crypto_alloc_ablkcipher(driver, type, mask);
  1076. if (IS_ERR(tfm)) {
  1077. printk(KERN_ERR "alg: cipher: Failed to load transform for "
  1078. "%s: %ld\n", driver, PTR_ERR(tfm));
  1079. return PTR_ERR(tfm);
  1080. }
  1081. if (desc->suite.cipher.enc.vecs) {
  1082. err = test_cipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
  1083. desc->suite.cipher.enc.count);
  1084. if (err)
  1085. goto out;
  1086. }
  1087. if (desc->suite.cipher.dec.vecs)
  1088. err = test_cipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
  1089. desc->suite.cipher.dec.count);
  1090. out:
  1091. crypto_free_ablkcipher(tfm);
  1092. return err;
  1093. }
  1094. static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
  1095. u32 type, u32 mask)
  1096. {
  1097. struct crypto_comp *tfm;
  1098. int err;
  1099. tfm = crypto_alloc_comp(driver, type, mask);
  1100. if (IS_ERR(tfm)) {
  1101. printk(KERN_ERR "alg: comp: Failed to load transform for %s: "
  1102. "%ld\n", driver, PTR_ERR(tfm));
  1103. return PTR_ERR(tfm);
  1104. }
  1105. err = test_comp(tfm, desc->suite.comp.comp.vecs,
  1106. desc->suite.comp.decomp.vecs,
  1107. desc->suite.comp.comp.count,
  1108. desc->suite.comp.decomp.count);
  1109. crypto_free_comp(tfm);
  1110. return err;
  1111. }
  1112. static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
  1113. u32 type, u32 mask)
  1114. {
  1115. struct crypto_ahash *tfm;
  1116. int err;
  1117. tfm = crypto_alloc_ahash(driver, type, mask);
  1118. if (IS_ERR(tfm)) {
  1119. printk(KERN_ERR "alg: hash: Failed to load transform for %s: "
  1120. "%ld\n", driver, PTR_ERR(tfm));
  1121. return PTR_ERR(tfm);
  1122. }
  1123. err = test_hash(tfm, desc->suite.hash.vecs, desc->suite.hash.count);
  1124. crypto_free_ahash(tfm);
  1125. return err;
  1126. }
  1127. /* Please keep this list sorted by algorithm name. */
  1128. static const struct alg_test_desc alg_test_descs[] = {
  1129. {
  1130. .alg = "cbc(aes)",
  1131. .test = alg_test_cipher,
  1132. .suite = {
  1133. .cipher = {
  1134. .enc = {
  1135. .vecs = aes_cbc_enc_tv_template,
  1136. .count = AES_CBC_ENC_TEST_VECTORS
  1137. },
  1138. .dec = {
  1139. .vecs = aes_cbc_dec_tv_template,
  1140. .count = AES_CBC_DEC_TEST_VECTORS
  1141. }
  1142. }
  1143. }
  1144. }, {
  1145. .alg = "cbc(anubis)",
  1146. .test = alg_test_cipher,
  1147. .suite = {
  1148. .cipher = {
  1149. .enc = {
  1150. .vecs = anubis_cbc_enc_tv_template,
  1151. .count = ANUBIS_CBC_ENC_TEST_VECTORS
  1152. },
  1153. .dec = {
  1154. .vecs = anubis_cbc_dec_tv_template,
  1155. .count = ANUBIS_CBC_DEC_TEST_VECTORS
  1156. }
  1157. }
  1158. }
  1159. }, {
  1160. .alg = "cbc(blowfish)",
  1161. .test = alg_test_cipher,
  1162. .suite = {
  1163. .cipher = {
  1164. .enc = {
  1165. .vecs = bf_cbc_enc_tv_template,
  1166. .count = BF_CBC_ENC_TEST_VECTORS
  1167. },
  1168. .dec = {
  1169. .vecs = bf_cbc_dec_tv_template,
  1170. .count = BF_CBC_DEC_TEST_VECTORS
  1171. }
  1172. }
  1173. }
  1174. }, {
  1175. .alg = "cbc(camellia)",
  1176. .test = alg_test_cipher,
  1177. .suite = {
  1178. .cipher = {
  1179. .enc = {
  1180. .vecs = camellia_cbc_enc_tv_template,
  1181. .count = CAMELLIA_CBC_ENC_TEST_VECTORS
  1182. },
  1183. .dec = {
  1184. .vecs = camellia_cbc_dec_tv_template,
  1185. .count = CAMELLIA_CBC_DEC_TEST_VECTORS
  1186. }
  1187. }
  1188. }
  1189. }, {
  1190. .alg = "cbc(des)",
  1191. .test = alg_test_cipher,
  1192. .suite = {
  1193. .cipher = {
  1194. .enc = {
  1195. .vecs = des_cbc_enc_tv_template,
  1196. .count = DES_CBC_ENC_TEST_VECTORS
  1197. },
  1198. .dec = {
  1199. .vecs = des_cbc_dec_tv_template,
  1200. .count = DES_CBC_DEC_TEST_VECTORS
  1201. }
  1202. }
  1203. }
  1204. }, {
  1205. .alg = "cbc(des3_ede)",
  1206. .test = alg_test_cipher,
  1207. .suite = {
  1208. .cipher = {
  1209. .enc = {
  1210. .vecs = des3_ede_cbc_enc_tv_template,
  1211. .count = DES3_EDE_CBC_ENC_TEST_VECTORS
  1212. },
  1213. .dec = {
  1214. .vecs = des3_ede_cbc_dec_tv_template,
  1215. .count = DES3_EDE_CBC_DEC_TEST_VECTORS
  1216. }
  1217. }
  1218. }
  1219. }, {
  1220. .alg = "cbc(twofish)",
  1221. .test = alg_test_cipher,
  1222. .suite = {
  1223. .cipher = {
  1224. .enc = {
  1225. .vecs = tf_cbc_enc_tv_template,
  1226. .count = TF_CBC_ENC_TEST_VECTORS
  1227. },
  1228. .dec = {
  1229. .vecs = tf_cbc_dec_tv_template,
  1230. .count = TF_CBC_DEC_TEST_VECTORS
  1231. }
  1232. }
  1233. }
  1234. }, {
  1235. .alg = "ccm(aes)",
  1236. .test = alg_test_aead,
  1237. .suite = {
  1238. .aead = {
  1239. .enc = {
  1240. .vecs = aes_ccm_enc_tv_template,
  1241. .count = AES_CCM_ENC_TEST_VECTORS
  1242. },
  1243. .dec = {
  1244. .vecs = aes_ccm_dec_tv_template,
  1245. .count = AES_CCM_DEC_TEST_VECTORS
  1246. }
  1247. }
  1248. }
  1249. }, {
  1250. .alg = "crc32c",
  1251. .test = alg_test_hash,
  1252. .suite = {
  1253. .hash = {
  1254. .vecs = crc32c_tv_template,
  1255. .count = CRC32C_TEST_VECTORS
  1256. }
  1257. }
  1258. }, {
  1259. .alg = "cts(cbc(aes))",
  1260. .test = alg_test_cipher,
  1261. .suite = {
  1262. .cipher = {
  1263. .enc = {
  1264. .vecs = cts_mode_enc_tv_template,
  1265. .count = CTS_MODE_ENC_TEST_VECTORS
  1266. },
  1267. .dec = {
  1268. .vecs = cts_mode_dec_tv_template,
  1269. .count = CTS_MODE_DEC_TEST_VECTORS
  1270. }
  1271. }
  1272. }
  1273. }, {
  1274. .alg = "deflate",
  1275. .test = alg_test_comp,
  1276. .suite = {
  1277. .comp = {
  1278. .comp = {
  1279. .vecs = deflate_comp_tv_template,
  1280. .count = DEFLATE_COMP_TEST_VECTORS
  1281. },
  1282. .decomp = {
  1283. .vecs = deflate_decomp_tv_template,
  1284. .count = DEFLATE_DECOMP_TEST_VECTORS
  1285. }
  1286. }
  1287. }
  1288. }, {
  1289. .alg = "ecb(aes)",
  1290. .test = alg_test_cipher,
  1291. .suite = {
  1292. .cipher = {
  1293. .enc = {
  1294. .vecs = aes_enc_tv_template,
  1295. .count = AES_ENC_TEST_VECTORS
  1296. },
  1297. .dec = {
  1298. .vecs = aes_dec_tv_template,
  1299. .count = AES_DEC_TEST_VECTORS
  1300. }
  1301. }
  1302. }
  1303. }, {
  1304. .alg = "ecb(anubis)",
  1305. .test = alg_test_cipher,
  1306. .suite = {
  1307. .cipher = {
  1308. .enc = {
  1309. .vecs = anubis_enc_tv_template,
  1310. .count = ANUBIS_ENC_TEST_VECTORS
  1311. },
  1312. .dec = {
  1313. .vecs = anubis_dec_tv_template,
  1314. .count = ANUBIS_DEC_TEST_VECTORS
  1315. }
  1316. }
  1317. }
  1318. }, {
  1319. .alg = "ecb(arc4)",
  1320. .test = alg_test_cipher,
  1321. .suite = {
  1322. .cipher = {
  1323. .enc = {
  1324. .vecs = arc4_enc_tv_template,
  1325. .count = ARC4_ENC_TEST_VECTORS
  1326. },
  1327. .dec = {
  1328. .vecs = arc4_dec_tv_template,
  1329. .count = ARC4_DEC_TEST_VECTORS
  1330. }
  1331. }
  1332. }
  1333. }, {
  1334. .alg = "ecb(blowfish)",
  1335. .test = alg_test_cipher,
  1336. .suite = {
  1337. .cipher = {
  1338. .enc = {
  1339. .vecs = bf_enc_tv_template,
  1340. .count = BF_ENC_TEST_VECTORS
  1341. },
  1342. .dec = {
  1343. .vecs = bf_dec_tv_template,
  1344. .count = BF_DEC_TEST_VECTORS
  1345. }
  1346. }
  1347. }
  1348. }, {
  1349. .alg = "ecb(camellia)",
  1350. .test = alg_test_cipher,
  1351. .suite = {
  1352. .cipher = {
  1353. .enc = {
  1354. .vecs = camellia_enc_tv_template,
  1355. .count = CAMELLIA_ENC_TEST_VECTORS
  1356. },
  1357. .dec = {
  1358. .vecs = camellia_dec_tv_template,
  1359. .count = CAMELLIA_DEC_TEST_VECTORS
  1360. }
  1361. }
  1362. }
  1363. }, {
  1364. .alg = "ecb(cast5)",
  1365. .test = alg_test_cipher,
  1366. .suite = {
  1367. .cipher = {
  1368. .enc = {
  1369. .vecs = cast5_enc_tv_template,
  1370. .count = CAST5_ENC_TEST_VECTORS
  1371. },
  1372. .dec = {
  1373. .vecs = cast5_dec_tv_template,
  1374. .count = CAST5_DEC_TEST_VECTORS
  1375. }
  1376. }
  1377. }
  1378. }, {
  1379. .alg = "ecb(cast6)",
  1380. .test = alg_test_cipher,
  1381. .suite = {
  1382. .cipher = {
  1383. .enc = {
  1384. .vecs = cast6_enc_tv_template,
  1385. .count = CAST6_ENC_TEST_VECTORS
  1386. },
  1387. .dec = {
  1388. .vecs = cast6_dec_tv_template,
  1389. .count = CAST6_DEC_TEST_VECTORS
  1390. }
  1391. }
  1392. }
  1393. }, {
  1394. .alg = "ecb(des)",
  1395. .test = alg_test_cipher,
  1396. .suite = {
  1397. .cipher = {
  1398. .enc = {
  1399. .vecs = des_enc_tv_template,
  1400. .count = DES_ENC_TEST_VECTORS
  1401. },
  1402. .dec = {
  1403. .vecs = des_dec_tv_template,
  1404. .count = DES_DEC_TEST_VECTORS
  1405. }
  1406. }
  1407. }
  1408. }, {
  1409. .alg = "ecb(des3_ede)",
  1410. .test = alg_test_cipher,
  1411. .suite = {
  1412. .cipher = {
  1413. .enc = {
  1414. .vecs = des3_ede_enc_tv_template,
  1415. .count = DES3_EDE_ENC_TEST_VECTORS
  1416. },
  1417. .dec = {
  1418. .vecs = des3_ede_dec_tv_template,
  1419. .count = DES3_EDE_DEC_TEST_VECTORS
  1420. }
  1421. }
  1422. }
  1423. }, {
  1424. .alg = "ecb(khazad)",
  1425. .test = alg_test_cipher,
  1426. .suite = {
  1427. .cipher = {
  1428. .enc = {
  1429. .vecs = khazad_enc_tv_template,
  1430. .count = KHAZAD_ENC_TEST_VECTORS
  1431. },
  1432. .dec = {
  1433. .vecs = khazad_dec_tv_template,
  1434. .count = KHAZAD_DEC_TEST_VECTORS
  1435. }
  1436. }
  1437. }
  1438. }, {
  1439. .alg = "ecb(seed)",
  1440. .test = alg_test_cipher,
  1441. .suite = {
  1442. .cipher = {
  1443. .enc = {
  1444. .vecs = seed_enc_tv_template,
  1445. .count = SEED_ENC_TEST_VECTORS
  1446. },
  1447. .dec = {
  1448. .vecs = seed_dec_tv_template,
  1449. .count = SEED_DEC_TEST_VECTORS
  1450. }
  1451. }
  1452. }
  1453. }, {
  1454. .alg = "ecb(serpent)",
  1455. .test = alg_test_cipher,
  1456. .suite = {
  1457. .cipher = {
  1458. .enc = {
  1459. .vecs = serpent_enc_tv_template,
  1460. .count = SERPENT_ENC_TEST_VECTORS
  1461. },
  1462. .dec = {
  1463. .vecs = serpent_dec_tv_template,
  1464. .count = SERPENT_DEC_TEST_VECTORS
  1465. }
  1466. }
  1467. }
  1468. }, {
  1469. .alg = "ecb(tea)",
  1470. .test = alg_test_cipher,
  1471. .suite = {
  1472. .cipher = {
  1473. .enc = {
  1474. .vecs = tea_enc_tv_template,
  1475. .count = TEA_ENC_TEST_VECTORS
  1476. },
  1477. .dec = {
  1478. .vecs = tea_dec_tv_template,
  1479. .count = TEA_DEC_TEST_VECTORS
  1480. }
  1481. }
  1482. }
  1483. }, {
  1484. .alg = "ecb(tnepres)",
  1485. .test = alg_test_cipher,
  1486. .suite = {
  1487. .cipher = {
  1488. .enc = {
  1489. .vecs = tnepres_enc_tv_template,
  1490. .count = TNEPRES_ENC_TEST_VECTORS
  1491. },
  1492. .dec = {
  1493. .vecs = tnepres_dec_tv_template,
  1494. .count = TNEPRES_DEC_TEST_VECTORS
  1495. }
  1496. }
  1497. }
  1498. }, {
  1499. .alg = "ecb(twofish)",
  1500. .test = alg_test_cipher,
  1501. .suite = {
  1502. .cipher = {
  1503. .enc = {
  1504. .vecs = tf_enc_tv_template,
  1505. .count = TF_ENC_TEST_VECTORS
  1506. },
  1507. .dec = {
  1508. .vecs = tf_dec_tv_template,
  1509. .count = TF_DEC_TEST_VECTORS
  1510. }
  1511. }
  1512. }
  1513. }, {
  1514. .alg = "ecb(xeta)",
  1515. .test = alg_test_cipher,
  1516. .suite = {
  1517. .cipher = {
  1518. .enc = {
  1519. .vecs = xeta_enc_tv_template,
  1520. .count = XETA_ENC_TEST_VECTORS
  1521. },
  1522. .dec = {
  1523. .vecs = xeta_dec_tv_template,
  1524. .count = XETA_DEC_TEST_VECTORS
  1525. }
  1526. }
  1527. }
  1528. }, {
  1529. .alg = "ecb(xtea)",
  1530. .test = alg_test_cipher,
  1531. .suite = {
  1532. .cipher = {
  1533. .enc = {
  1534. .vecs = xtea_enc_tv_template,
  1535. .count = XTEA_ENC_TEST_VECTORS
  1536. },
  1537. .dec = {
  1538. .vecs = xtea_dec_tv_template,
  1539. .count = XTEA_DEC_TEST_VECTORS
  1540. }
  1541. }
  1542. }
  1543. }, {
  1544. .alg = "gcm(aes)",
  1545. .test = alg_test_aead,
  1546. .suite = {
  1547. .aead = {
  1548. .enc = {
  1549. .vecs = aes_gcm_enc_tv_template,
  1550. .count = AES_GCM_ENC_TEST_VECTORS
  1551. },
  1552. .dec = {
  1553. .vecs = aes_gcm_dec_tv_template,
  1554. .count = AES_GCM_DEC_TEST_VECTORS
  1555. }
  1556. }
  1557. }
  1558. }, {
  1559. .alg = "hmac(md5)",
  1560. .test = alg_test_hash,
  1561. .suite = {
  1562. .hash = {
  1563. .vecs = hmac_md5_tv_template,
  1564. .count = HMAC_MD5_TEST_VECTORS
  1565. }
  1566. }
  1567. }, {
  1568. .alg = "hmac(rmd128)",
  1569. .test = alg_test_hash,
  1570. .suite = {
  1571. .hash = {
  1572. .vecs = hmac_rmd128_tv_template,
  1573. .count = HMAC_RMD128_TEST_VECTORS
  1574. }
  1575. }
  1576. }, {
  1577. .alg = "hmac(rmd160)",
  1578. .test = alg_test_hash,
  1579. .suite = {
  1580. .hash = {
  1581. .vecs = hmac_rmd160_tv_template,
  1582. .count = HMAC_RMD160_TEST_VECTORS
  1583. }
  1584. }
  1585. }, {
  1586. .alg = "hmac(sha1)",
  1587. .test = alg_test_hash,
  1588. .suite = {
  1589. .hash = {
  1590. .vecs = hmac_sha1_tv_template,
  1591. .count = HMAC_SHA1_TEST_VECTORS
  1592. }
  1593. }
  1594. }, {
  1595. .alg = "hmac(sha224)",
  1596. .test = alg_test_hash,
  1597. .suite = {
  1598. .hash = {
  1599. .vecs = hmac_sha224_tv_template,
  1600. .count = HMAC_SHA224_TEST_VECTORS
  1601. }
  1602. }
  1603. }, {
  1604. .alg = "hmac(sha256)",
  1605. .test = alg_test_hash,
  1606. .suite = {
  1607. .hash = {
  1608. .vecs = hmac_sha256_tv_template,
  1609. .count = HMAC_SHA256_TEST_VECTORS
  1610. }
  1611. }
  1612. }, {
  1613. .alg = "hmac(sha384)",
  1614. .test = alg_test_hash,
  1615. .suite = {
  1616. .hash = {
  1617. .vecs = hmac_sha384_tv_template,
  1618. .count = HMAC_SHA384_TEST_VECTORS
  1619. }
  1620. }
  1621. }, {
  1622. .alg = "hmac(sha512)",
  1623. .test = alg_test_hash,
  1624. .suite = {
  1625. .hash = {
  1626. .vecs = hmac_sha512_tv_template,
  1627. .count = HMAC_SHA512_TEST_VECTORS
  1628. }
  1629. }
  1630. }, {
  1631. .alg = "lrw(aes)",
  1632. .test = alg_test_cipher,
  1633. .suite = {
  1634. .cipher = {
  1635. .enc = {
  1636. .vecs = aes_lrw_enc_tv_template,
  1637. .count = AES_LRW_ENC_TEST_VECTORS
  1638. },
  1639. .dec = {
  1640. .vecs = aes_lrw_dec_tv_template,
  1641. .count = AES_LRW_DEC_TEST_VECTORS
  1642. }
  1643. }
  1644. }
  1645. }, {
  1646. .alg = "lzo",
  1647. .test = alg_test_comp,
  1648. .suite = {
  1649. .comp = {
  1650. .comp = {
  1651. .vecs = lzo_comp_tv_template,
  1652. .count = LZO_COMP_TEST_VECTORS
  1653. },
  1654. .decomp = {
  1655. .vecs = lzo_decomp_tv_template,
  1656. .count = LZO_DECOMP_TEST_VECTORS
  1657. }
  1658. }
  1659. }
  1660. }, {
  1661. .alg = "md4",
  1662. .test = alg_test_hash,
  1663. .suite = {
  1664. .hash = {
  1665. .vecs = md4_tv_template,
  1666. .count = MD4_TEST_VECTORS
  1667. }
  1668. }
  1669. }, {
  1670. .alg = "md5",
  1671. .test = alg_test_hash,
  1672. .suite = {
  1673. .hash = {
  1674. .vecs = md5_tv_template,
  1675. .count = MD5_TEST_VECTORS
  1676. }
  1677. }
  1678. }, {
  1679. .alg = "michael_mic",
  1680. .test = alg_test_hash,
  1681. .suite = {
  1682. .hash = {
  1683. .vecs = michael_mic_tv_template,
  1684. .count = MICHAEL_MIC_TEST_VECTORS
  1685. }
  1686. }
  1687. }, {
  1688. .alg = "pcbc(fcrypt)",
  1689. .test = alg_test_cipher,
  1690. .suite = {
  1691. .cipher = {
  1692. .enc = {
  1693. .vecs = fcrypt_pcbc_enc_tv_template,
  1694. .count = FCRYPT_ENC_TEST_VECTORS
  1695. },
  1696. .dec = {
  1697. .vecs = fcrypt_pcbc_dec_tv_template,
  1698. .count = FCRYPT_DEC_TEST_VECTORS
  1699. }
  1700. }
  1701. }
  1702. }, {
  1703. .alg = "rfc3686(ctr(aes))",
  1704. .test = alg_test_cipher,
  1705. .suite = {
  1706. .cipher = {
  1707. .enc = {
  1708. .vecs = aes_ctr_enc_tv_template,
  1709. .count = AES_CTR_ENC_TEST_VECTORS
  1710. },
  1711. .dec = {
  1712. .vecs = aes_ctr_dec_tv_template,
  1713. .count = AES_CTR_DEC_TEST_VECTORS
  1714. }
  1715. }
  1716. }
  1717. }, {
  1718. .alg = "rmd128",
  1719. .test = alg_test_hash,
  1720. .suite = {
  1721. .hash = {
  1722. .vecs = rmd128_tv_template,
  1723. .count = RMD128_TEST_VECTORS
  1724. }
  1725. }
  1726. }, {
  1727. .alg = "rmd160",
  1728. .test = alg_test_hash,
  1729. .suite = {
  1730. .hash = {
  1731. .vecs = rmd160_tv_template,
  1732. .count = RMD160_TEST_VECTORS
  1733. }
  1734. }
  1735. }, {
  1736. .alg = "rmd256",
  1737. .test = alg_test_hash,
  1738. .suite = {
  1739. .hash = {
  1740. .vecs = rmd256_tv_template,
  1741. .count = RMD256_TEST_VECTORS
  1742. }
  1743. }
  1744. }, {
  1745. .alg = "rmd320",
  1746. .test = alg_test_hash,
  1747. .suite = {
  1748. .hash = {
  1749. .vecs = rmd320_tv_template,
  1750. .count = RMD320_TEST_VECTORS
  1751. }
  1752. }
  1753. }, {
  1754. .alg = "salsa20",
  1755. .test = alg_test_cipher,
  1756. .suite = {
  1757. .cipher = {
  1758. .enc = {
  1759. .vecs = salsa20_stream_enc_tv_template,
  1760. .count = SALSA20_STREAM_ENC_TEST_VECTORS
  1761. }
  1762. }
  1763. }
  1764. }, {
  1765. .alg = "sha1",
  1766. .test = alg_test_hash,
  1767. .suite = {
  1768. .hash = {
  1769. .vecs = sha1_tv_template,
  1770. .count = SHA1_TEST_VECTORS
  1771. }
  1772. }
  1773. }, {
  1774. .alg = "sha224",
  1775. .test = alg_test_hash,
  1776. .suite = {
  1777. .hash = {
  1778. .vecs = sha224_tv_template,
  1779. .count = SHA224_TEST_VECTORS
  1780. }
  1781. }
  1782. }, {
  1783. .alg = "sha256",
  1784. .test = alg_test_hash,
  1785. .suite = {
  1786. .hash = {
  1787. .vecs = sha256_tv_template,
  1788. .count = SHA256_TEST_VECTORS
  1789. }
  1790. }
  1791. }, {
  1792. .alg = "sha384",
  1793. .test = alg_test_hash,
  1794. .suite = {
  1795. .hash = {
  1796. .vecs = sha384_tv_template,
  1797. .count = SHA384_TEST_VECTORS
  1798. }
  1799. }
  1800. }, {
  1801. .alg = "sha512",
  1802. .test = alg_test_hash,
  1803. .suite = {
  1804. .hash = {
  1805. .vecs = sha512_tv_template,
  1806. .count = SHA512_TEST_VECTORS
  1807. }
  1808. }
  1809. }, {
  1810. .alg = "tgr128",
  1811. .test = alg_test_hash,
  1812. .suite = {
  1813. .hash = {
  1814. .vecs = tgr128_tv_template,
  1815. .count = TGR128_TEST_VECTORS
  1816. }
  1817. }
  1818. }, {
  1819. .alg = "tgr160",
  1820. .test = alg_test_hash,
  1821. .suite = {
  1822. .hash = {
  1823. .vecs = tgr160_tv_template,
  1824. .count = TGR160_TEST_VECTORS
  1825. }
  1826. }
  1827. }, {
  1828. .alg = "tgr192",
  1829. .test = alg_test_hash,
  1830. .suite = {
  1831. .hash = {
  1832. .vecs = tgr192_tv_template,
  1833. .count = TGR192_TEST_VECTORS
  1834. }
  1835. }
  1836. }, {
  1837. .alg = "wp256",
  1838. .test = alg_test_hash,
  1839. .suite = {
  1840. .hash = {
  1841. .vecs = wp256_tv_template,
  1842. .count = WP256_TEST_VECTORS
  1843. }
  1844. }
  1845. }, {
  1846. .alg = "wp384",
  1847. .test = alg_test_hash,
  1848. .suite = {
  1849. .hash = {
  1850. .vecs = wp384_tv_template,
  1851. .count = WP384_TEST_VECTORS
  1852. }
  1853. }
  1854. }, {
  1855. .alg = "wp512",
  1856. .test = alg_test_hash,
  1857. .suite = {
  1858. .hash = {
  1859. .vecs = wp512_tv_template,
  1860. .count = WP512_TEST_VECTORS
  1861. }
  1862. }
  1863. }, {
  1864. .alg = "xcbc(aes)",
  1865. .test = alg_test_hash,
  1866. .suite = {
  1867. .hash = {
  1868. .vecs = aes_xcbc128_tv_template,
  1869. .count = XCBC_AES_TEST_VECTORS
  1870. }
  1871. }
  1872. }, {
  1873. .alg = "xts(aes)",
  1874. .test = alg_test_cipher,
  1875. .suite = {
  1876. .cipher = {
  1877. .enc = {
  1878. .vecs = aes_xts_enc_tv_template,
  1879. .count = AES_XTS_ENC_TEST_VECTORS
  1880. },
  1881. .dec = {
  1882. .vecs = aes_xts_dec_tv_template,
  1883. .count = AES_XTS_DEC_TEST_VECTORS
  1884. }
  1885. }
  1886. }
  1887. }
  1888. };
  1889. static int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
  1890. {
  1891. int start = 0;
  1892. int end = ARRAY_SIZE(alg_test_descs);
  1893. while (start < end) {
  1894. int i = (start + end) / 2;
  1895. int diff = strcmp(alg_test_descs[i].alg, alg);
  1896. if (diff > 0) {
  1897. end = i;
  1898. continue;
  1899. }
  1900. if (diff < 0) {
  1901. start = i + 1;
  1902. continue;
  1903. }
  1904. return alg_test_descs[i].test(alg_test_descs + i, driver,
  1905. type, mask);
  1906. }
  1907. printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
  1908. return 0;
  1909. }
  1910. static inline int tcrypt_test(const char *alg)
  1911. {
  1912. return alg_test(alg, alg, 0, 0);
  1913. }
  1914. static void do_test(int m)
  1915. {
  1916. int i;
  1917. switch (m) {
  1918. case 0:
  1919. for (i = 1; i < 200; i++)
  1920. do_test(i);
  1921. break;
  1922. case 1:
  1923. tcrypt_test("md5");
  1924. break;
  1925. case 2:
  1926. tcrypt_test("sha1");
  1927. break;
  1928. case 3:
  1929. tcrypt_test("ecb(des)");
  1930. tcrypt_test("cbc(des)");
  1931. break;
  1932. case 4:
  1933. tcrypt_test("ecb(des3_ede)");
  1934. tcrypt_test("cbc(des3_ede)");
  1935. break;
  1936. case 5:
  1937. tcrypt_test("md4");
  1938. break;
  1939. case 6:
  1940. tcrypt_test("sha256");
  1941. break;
  1942. case 7:
  1943. tcrypt_test("ecb(blowfish)");
  1944. tcrypt_test("cbc(blowfish)");
  1945. break;
  1946. case 8:
  1947. tcrypt_test("ecb(twofish)");
  1948. tcrypt_test("cbc(twofish)");
  1949. break;
  1950. case 9:
  1951. tcrypt_test("ecb(serpent)");
  1952. break;
  1953. case 10:
  1954. tcrypt_test("ecb(aes)");
  1955. tcrypt_test("cbc(aes)");
  1956. tcrypt_test("lrw(aes)");
  1957. tcrypt_test("xts(aes)");
  1958. tcrypt_test("rfc3686(ctr(aes))");
  1959. break;
  1960. case 11:
  1961. tcrypt_test("sha384");
  1962. break;
  1963. case 12:
  1964. tcrypt_test("sha512");
  1965. break;
  1966. case 13:
  1967. tcrypt_test("deflate");
  1968. break;
  1969. case 14:
  1970. tcrypt_test("ecb(cast5)");
  1971. break;
  1972. case 15:
  1973. tcrypt_test("ecb(cast6)");
  1974. break;
  1975. case 16:
  1976. tcrypt_test("ecb(arc4)");
  1977. break;
  1978. case 17:
  1979. tcrypt_test("michael_mic");
  1980. break;
  1981. case 18:
  1982. tcrypt_test("crc32c");
  1983. break;
  1984. case 19:
  1985. tcrypt_test("ecb(tea)");
  1986. break;
  1987. case 20:
  1988. tcrypt_test("ecb(xtea)");
  1989. break;
  1990. case 21:
  1991. tcrypt_test("ecb(khazad)");
  1992. break;
  1993. case 22:
  1994. tcrypt_test("wp512");
  1995. break;
  1996. case 23:
  1997. tcrypt_test("wp384");
  1998. break;
  1999. case 24:
  2000. tcrypt_test("wp256");
  2001. break;
  2002. case 25:
  2003. tcrypt_test("ecb(tnepres)");
  2004. break;
  2005. case 26:
  2006. tcrypt_test("ecb(anubis)");
  2007. tcrypt_test("cbc(anubis)");
  2008. break;
  2009. case 27:
  2010. tcrypt_test("tgr192");
  2011. break;
  2012. case 28:
  2013. tcrypt_test("tgr160");
  2014. break;
  2015. case 29:
  2016. tcrypt_test("tgr128");
  2017. break;
  2018. case 30:
  2019. tcrypt_test("ecb(xeta)");
  2020. break;
  2021. case 31:
  2022. tcrypt_test("pcbc(fcrypt)");
  2023. break;
  2024. case 32:
  2025. tcrypt_test("ecb(camellia)");
  2026. tcrypt_test("cbc(camellia)");
  2027. break;
  2028. case 33:
  2029. tcrypt_test("sha224");
  2030. break;
  2031. case 34:
  2032. tcrypt_test("salsa20");
  2033. break;
  2034. case 35:
  2035. tcrypt_test("gcm(aes)");
  2036. break;
  2037. case 36:
  2038. tcrypt_test("lzo");
  2039. break;
  2040. case 37:
  2041. tcrypt_test("ccm(aes)");
  2042. break;
  2043. case 38:
  2044. tcrypt_test("cts(cbc(aes))");
  2045. break;
  2046. case 39:
  2047. tcrypt_test("rmd128");
  2048. break;
  2049. case 40:
  2050. tcrypt_test("rmd160");
  2051. break;
  2052. case 41:
  2053. tcrypt_test("rmd256");
  2054. break;
  2055. case 42:
  2056. tcrypt_test("rmd320");
  2057. break;
  2058. case 43:
  2059. tcrypt_test("ecb(seed)");
  2060. break;
  2061. case 100:
  2062. tcrypt_test("hmac(md5)");
  2063. break;
  2064. case 101:
  2065. tcrypt_test("hmac(sha1)");
  2066. break;
  2067. case 102:
  2068. tcrypt_test("hmac(sha256)");
  2069. break;
  2070. case 103:
  2071. tcrypt_test("hmac(sha384)");
  2072. break;
  2073. case 104:
  2074. tcrypt_test("hmac(sha512)");
  2075. break;
  2076. case 105:
  2077. tcrypt_test("hmac(sha224)");
  2078. break;
  2079. case 106:
  2080. tcrypt_test("xcbc(aes)");
  2081. break;
  2082. case 107:
  2083. tcrypt_test("hmac(rmd128)");
  2084. break;
  2085. case 108:
  2086. tcrypt_test("hmac(rmd160)");
  2087. break;
  2088. case 200:
  2089. test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
  2090. speed_template_16_24_32);
  2091. test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
  2092. speed_template_16_24_32);
  2093. test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
  2094. speed_template_16_24_32);
  2095. test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
  2096. speed_template_16_24_32);
  2097. test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
  2098. speed_template_32_40_48);
  2099. test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
  2100. speed_template_32_40_48);
  2101. test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
  2102. speed_template_32_48_64);
  2103. test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
  2104. speed_template_32_48_64);
  2105. break;
  2106. case 201:
  2107. test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
  2108. des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS,
  2109. speed_template_24);
  2110. test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
  2111. des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS,
  2112. speed_template_24);
  2113. test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
  2114. des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS,
  2115. speed_template_24);
  2116. test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
  2117. des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS,
  2118. speed_template_24);
  2119. break;
  2120. case 202:
  2121. test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
  2122. speed_template_16_24_32);
  2123. test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
  2124. speed_template_16_24_32);
  2125. test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
  2126. speed_template_16_24_32);
  2127. test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
  2128. speed_template_16_24_32);
  2129. break;
  2130. case 203:
  2131. test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
  2132. speed_template_8_32);
  2133. test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
  2134. speed_template_8_32);
  2135. test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
  2136. speed_template_8_32);
  2137. test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
  2138. speed_template_8_32);
  2139. break;
  2140. case 204:
  2141. test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
  2142. speed_template_8);
  2143. test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
  2144. speed_template_8);
  2145. test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
  2146. speed_template_8);
  2147. test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
  2148. speed_template_8);
  2149. break;
  2150. case 205:
  2151. test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
  2152. speed_template_16_24_32);
  2153. test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
  2154. speed_template_16_24_32);
  2155. test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
  2156. speed_template_16_24_32);
  2157. test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
  2158. speed_template_16_24_32);
  2159. break;
  2160. case 206:
  2161. test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
  2162. speed_template_16_32);
  2163. break;
  2164. case 300:
  2165. /* fall through */
  2166. case 301:
  2167. test_hash_speed("md4", sec, generic_hash_speed_template);
  2168. if (mode > 300 && mode < 400) break;
  2169. case 302:
  2170. test_hash_speed("md5", sec, generic_hash_speed_template);
  2171. if (mode > 300 && mode < 400) break;
  2172. case 303:
  2173. test_hash_speed("sha1", sec, generic_hash_speed_template);
  2174. if (mode > 300 && mode < 400) break;
  2175. case 304:
  2176. test_hash_speed("sha256", sec, generic_hash_speed_template);
  2177. if (mode > 300 && mode < 400) break;
  2178. case 305:
  2179. test_hash_speed("sha384", sec, generic_hash_speed_template);
  2180. if (mode > 300 && mode < 400) break;
  2181. case 306:
  2182. test_hash_speed("sha512", sec, generic_hash_speed_template);
  2183. if (mode > 300 && mode < 400) break;
  2184. case 307:
  2185. test_hash_speed("wp256", sec, generic_hash_speed_template);
  2186. if (mode > 300 && mode < 400) break;
  2187. case 308:
  2188. test_hash_speed("wp384", sec, generic_hash_speed_template);
  2189. if (mode > 300 && mode < 400) break;
  2190. case 309:
  2191. test_hash_speed("wp512", sec, generic_hash_speed_template);
  2192. if (mode > 300 && mode < 400) break;
  2193. case 310:
  2194. test_hash_speed("tgr128", sec, generic_hash_speed_template);
  2195. if (mode > 300 && mode < 400) break;
  2196. case 311:
  2197. test_hash_speed("tgr160", sec, generic_hash_speed_template);
  2198. if (mode > 300 && mode < 400) break;
  2199. case 312:
  2200. test_hash_speed("tgr192", sec, generic_hash_speed_template);
  2201. if (mode > 300 && mode < 400) break;
  2202. case 313:
  2203. test_hash_speed("sha224", sec, generic_hash_speed_template);
  2204. if (mode > 300 && mode < 400) break;
  2205. case 314:
  2206. test_hash_speed("rmd128", sec, generic_hash_speed_template);
  2207. if (mode > 300 && mode < 400) break;
  2208. case 315:
  2209. test_hash_speed("rmd160", sec, generic_hash_speed_template);
  2210. if (mode > 300 && mode < 400) break;
  2211. case 316:
  2212. test_hash_speed("rmd256", sec, generic_hash_speed_template);
  2213. if (mode > 300 && mode < 400) break;
  2214. case 317:
  2215. test_hash_speed("rmd320", sec, generic_hash_speed_template);
  2216. if (mode > 300 && mode < 400) break;
  2217. case 399:
  2218. break;
  2219. case 1000:
  2220. test_available();
  2221. break;
  2222. }
  2223. }
  2224. static int __init tcrypt_mod_init(void)
  2225. {
  2226. int err = -ENOMEM;
  2227. int i;
  2228. for (i = 0; i < TVMEMSIZE; i++) {
  2229. tvmem[i] = (void *)__get_free_page(GFP_KERNEL);
  2230. if (!tvmem[i])
  2231. goto err_free_tv;
  2232. }
  2233. for (i = 0; i < XBUFSIZE; i++) {
  2234. xbuf[i] = (void *)__get_free_page(GFP_KERNEL);
  2235. if (!xbuf[i])
  2236. goto err_free_xbuf;
  2237. }
  2238. for (i = 0; i < XBUFSIZE; i++) {
  2239. axbuf[i] = (void *)__get_free_page(GFP_KERNEL);
  2240. if (!axbuf[i])
  2241. goto err_free_axbuf;
  2242. }
  2243. do_test(mode);
  2244. /* We intentionaly return -EAGAIN to prevent keeping
  2245. * the module. It does all its work from init()
  2246. * and doesn't offer any runtime functionality
  2247. * => we don't need it in the memory, do we?
  2248. * -- mludvig
  2249. */
  2250. err = -EAGAIN;
  2251. err_free_axbuf:
  2252. for (i = 0; i < XBUFSIZE && axbuf[i]; i++)
  2253. free_page((unsigned long)axbuf[i]);
  2254. err_free_xbuf:
  2255. for (i = 0; i < XBUFSIZE && xbuf[i]; i++)
  2256. free_page((unsigned long)xbuf[i]);
  2257. err_free_tv:
  2258. for (i = 0; i < TVMEMSIZE && tvmem[i]; i++)
  2259. free_page((unsigned long)tvmem[i]);
  2260. return err;
  2261. }
  2262. /*
  2263. * If an init function is provided, an exit function must also be provided
  2264. * to allow module unload.
  2265. */
  2266. static void __exit tcrypt_mod_fini(void) { }
  2267. module_init(tcrypt_mod_init);
  2268. module_exit(tcrypt_mod_fini);
  2269. module_param(mode, int, 0);
  2270. module_param(sec, uint, 0);
  2271. MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
  2272. "(defaults to zero which uses CPU cycles instead)");
  2273. MODULE_LICENSE("GPL");
  2274. MODULE_DESCRIPTION("Quick & dirty crypto testing module");
  2275. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");