testmgr.c 50 KB

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