testmgr.c 55 KB

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