testmgr.c 51 KB

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