testmgr.c 54 KB

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