testmgr.c 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649
  1. /*
  2. * Algorithm testing framework and tests.
  3. *
  4. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  5. * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
  6. * Copyright (c) 2007 Nokia Siemens Networks
  7. * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
  8. *
  9. * Updated RFC4106 AES-GCM testing.
  10. * Authors: Aidan O'Mahony (aidan.o.mahony@intel.com)
  11. * Adrian Hoban <adrian.hoban@intel.com>
  12. * Gabriele Paoloni <gabriele.paoloni@intel.com>
  13. * Tadeusz Struk (tadeusz.struk@intel.com)
  14. * Copyright (c) 2010, Intel Corporation.
  15. *
  16. * This program is free software; you can redistribute it and/or modify it
  17. * under the terms of the GNU General Public License as published by the Free
  18. * Software Foundation; either version 2 of the License, or (at your option)
  19. * any later version.
  20. *
  21. */
  22. #include <crypto/aead.h>
  23. #include <crypto/hash.h>
  24. #include <crypto/skcipher.h>
  25. #include <linux/err.h>
  26. #include <linux/fips.h>
  27. #include <linux/module.h>
  28. #include <linux/scatterlist.h>
  29. #include <linux/slab.h>
  30. #include <linux/string.h>
  31. #include <crypto/rng.h>
  32. #include <crypto/drbg.h>
  33. #include <crypto/akcipher.h>
  34. #include <crypto/kpp.h>
  35. #include <crypto/acompress.h>
  36. #include "internal.h"
  37. static bool notests;
  38. module_param(notests, bool, 0644);
  39. MODULE_PARM_DESC(notests, "disable crypto self-tests");
  40. #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
  41. /* a perfect nop */
  42. int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
  43. {
  44. return 0;
  45. }
  46. #else
  47. #include "testmgr.h"
  48. /*
  49. * Need slab memory for testing (size in number of pages).
  50. */
  51. #define XBUFSIZE 8
  52. /*
  53. * Indexes into the xbuf to simulate cross-page access.
  54. */
  55. #define IDX1 32
  56. #define IDX2 32400
  57. #define IDX3 1511
  58. #define IDX4 8193
  59. #define IDX5 22222
  60. #define IDX6 17101
  61. #define IDX7 27333
  62. #define IDX8 3000
  63. /*
  64. * Used by test_cipher()
  65. */
  66. #define ENCRYPT 1
  67. #define DECRYPT 0
  68. struct tcrypt_result {
  69. struct completion completion;
  70. int err;
  71. };
  72. struct aead_test_suite {
  73. struct {
  74. const struct aead_testvec *vecs;
  75. unsigned int count;
  76. } enc, dec;
  77. };
  78. struct cipher_test_suite {
  79. struct {
  80. const struct cipher_testvec *vecs;
  81. unsigned int count;
  82. } enc, dec;
  83. };
  84. struct comp_test_suite {
  85. struct {
  86. const struct comp_testvec *vecs;
  87. unsigned int count;
  88. } comp, decomp;
  89. };
  90. struct hash_test_suite {
  91. const struct hash_testvec *vecs;
  92. unsigned int count;
  93. };
  94. struct cprng_test_suite {
  95. const struct cprng_testvec *vecs;
  96. unsigned int count;
  97. };
  98. struct drbg_test_suite {
  99. const struct drbg_testvec *vecs;
  100. unsigned int count;
  101. };
  102. struct akcipher_test_suite {
  103. const struct akcipher_testvec *vecs;
  104. unsigned int count;
  105. };
  106. struct kpp_test_suite {
  107. const struct kpp_testvec *vecs;
  108. unsigned int count;
  109. };
  110. struct alg_test_desc {
  111. const char *alg;
  112. int (*test)(const struct alg_test_desc *desc, const char *driver,
  113. u32 type, u32 mask);
  114. int fips_allowed; /* set if alg is allowed in fips mode */
  115. union {
  116. struct aead_test_suite aead;
  117. struct cipher_test_suite cipher;
  118. struct comp_test_suite comp;
  119. struct hash_test_suite hash;
  120. struct cprng_test_suite cprng;
  121. struct drbg_test_suite drbg;
  122. struct akcipher_test_suite akcipher;
  123. struct kpp_test_suite kpp;
  124. } suite;
  125. };
  126. static const unsigned int IDX[8] = {
  127. IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
  128. static void hexdump(unsigned char *buf, unsigned int len)
  129. {
  130. print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
  131. 16, 1,
  132. buf, len, false);
  133. }
  134. static void tcrypt_complete(struct crypto_async_request *req, int err)
  135. {
  136. struct tcrypt_result *res = req->data;
  137. if (err == -EINPROGRESS)
  138. return;
  139. res->err = err;
  140. complete(&res->completion);
  141. }
  142. static int testmgr_alloc_buf(char *buf[XBUFSIZE])
  143. {
  144. int i;
  145. for (i = 0; i < XBUFSIZE; i++) {
  146. buf[i] = (void *)__get_free_page(GFP_KERNEL);
  147. if (!buf[i])
  148. goto err_free_buf;
  149. }
  150. return 0;
  151. err_free_buf:
  152. while (i-- > 0)
  153. free_page((unsigned long)buf[i]);
  154. return -ENOMEM;
  155. }
  156. static void testmgr_free_buf(char *buf[XBUFSIZE])
  157. {
  158. int i;
  159. for (i = 0; i < XBUFSIZE; i++)
  160. free_page((unsigned long)buf[i]);
  161. }
  162. static int wait_async_op(struct tcrypt_result *tr, int ret)
  163. {
  164. if (ret == -EINPROGRESS || ret == -EBUSY) {
  165. wait_for_completion(&tr->completion);
  166. reinit_completion(&tr->completion);
  167. ret = tr->err;
  168. }
  169. return ret;
  170. }
  171. static int ahash_partial_update(struct ahash_request **preq,
  172. struct crypto_ahash *tfm, const struct hash_testvec *template,
  173. void *hash_buff, int k, int temp, struct scatterlist *sg,
  174. const char *algo, char *result, struct tcrypt_result *tresult)
  175. {
  176. char *state;
  177. struct ahash_request *req;
  178. int statesize, ret = -EINVAL;
  179. const char guard[] = { 0x00, 0xba, 0xad, 0x00 };
  180. req = *preq;
  181. statesize = crypto_ahash_statesize(
  182. crypto_ahash_reqtfm(req));
  183. state = kmalloc(statesize + sizeof(guard), GFP_KERNEL);
  184. if (!state) {
  185. pr_err("alt: hash: Failed to alloc state for %s\n", algo);
  186. goto out_nostate;
  187. }
  188. memcpy(state + statesize, guard, sizeof(guard));
  189. ret = crypto_ahash_export(req, state);
  190. WARN_ON(memcmp(state + statesize, guard, sizeof(guard)));
  191. if (ret) {
  192. pr_err("alt: hash: Failed to export() for %s\n", algo);
  193. goto out;
  194. }
  195. ahash_request_free(req);
  196. req = ahash_request_alloc(tfm, GFP_KERNEL);
  197. if (!req) {
  198. pr_err("alg: hash: Failed to alloc request for %s\n", algo);
  199. goto out_noreq;
  200. }
  201. ahash_request_set_callback(req,
  202. CRYPTO_TFM_REQ_MAY_BACKLOG,
  203. tcrypt_complete, tresult);
  204. memcpy(hash_buff, template->plaintext + temp,
  205. template->tap[k]);
  206. sg_init_one(&sg[0], hash_buff, template->tap[k]);
  207. ahash_request_set_crypt(req, sg, result, template->tap[k]);
  208. ret = crypto_ahash_import(req, state);
  209. if (ret) {
  210. pr_err("alg: hash: Failed to import() for %s\n", algo);
  211. goto out;
  212. }
  213. ret = wait_async_op(tresult, crypto_ahash_update(req));
  214. if (ret)
  215. goto out;
  216. *preq = req;
  217. ret = 0;
  218. goto out_noreq;
  219. out:
  220. ahash_request_free(req);
  221. out_noreq:
  222. kfree(state);
  223. out_nostate:
  224. return ret;
  225. }
  226. static int __test_hash(struct crypto_ahash *tfm,
  227. const struct hash_testvec *template, unsigned int tcount,
  228. bool use_digest, const int align_offset)
  229. {
  230. const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
  231. size_t digest_size = crypto_ahash_digestsize(tfm);
  232. unsigned int i, j, k, temp;
  233. struct scatterlist sg[8];
  234. char *result;
  235. char *key;
  236. struct ahash_request *req;
  237. struct tcrypt_result tresult;
  238. void *hash_buff;
  239. char *xbuf[XBUFSIZE];
  240. int ret = -ENOMEM;
  241. result = kmalloc(digest_size, GFP_KERNEL);
  242. if (!result)
  243. return ret;
  244. key = kmalloc(MAX_KEYLEN, GFP_KERNEL);
  245. if (!key)
  246. goto out_nobuf;
  247. if (testmgr_alloc_buf(xbuf))
  248. goto out_nobuf;
  249. init_completion(&tresult.completion);
  250. req = ahash_request_alloc(tfm, GFP_KERNEL);
  251. if (!req) {
  252. printk(KERN_ERR "alg: hash: Failed to allocate request for "
  253. "%s\n", algo);
  254. goto out_noreq;
  255. }
  256. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  257. tcrypt_complete, &tresult);
  258. j = 0;
  259. for (i = 0; i < tcount; i++) {
  260. if (template[i].np)
  261. continue;
  262. ret = -EINVAL;
  263. if (WARN_ON(align_offset + template[i].psize > PAGE_SIZE))
  264. goto out;
  265. j++;
  266. memset(result, 0, digest_size);
  267. hash_buff = xbuf[0];
  268. hash_buff += align_offset;
  269. memcpy(hash_buff, template[i].plaintext, template[i].psize);
  270. sg_init_one(&sg[0], hash_buff, template[i].psize);
  271. if (template[i].ksize) {
  272. crypto_ahash_clear_flags(tfm, ~0);
  273. if (template[i].ksize > MAX_KEYLEN) {
  274. pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
  275. j, algo, template[i].ksize, MAX_KEYLEN);
  276. ret = -EINVAL;
  277. goto out;
  278. }
  279. memcpy(key, template[i].key, template[i].ksize);
  280. ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
  281. if (ret) {
  282. printk(KERN_ERR "alg: hash: setkey failed on "
  283. "test %d for %s: ret=%d\n", j, algo,
  284. -ret);
  285. goto out;
  286. }
  287. }
  288. ahash_request_set_crypt(req, sg, result, template[i].psize);
  289. if (use_digest) {
  290. ret = wait_async_op(&tresult, crypto_ahash_digest(req));
  291. if (ret) {
  292. pr_err("alg: hash: digest failed on test %d "
  293. "for %s: ret=%d\n", j, algo, -ret);
  294. goto out;
  295. }
  296. } else {
  297. ret = wait_async_op(&tresult, crypto_ahash_init(req));
  298. if (ret) {
  299. pr_err("alt: hash: init failed on test %d "
  300. "for %s: ret=%d\n", j, algo, -ret);
  301. goto out;
  302. }
  303. ret = wait_async_op(&tresult, crypto_ahash_update(req));
  304. if (ret) {
  305. pr_err("alt: hash: update failed on test %d "
  306. "for %s: ret=%d\n", j, algo, -ret);
  307. goto out;
  308. }
  309. ret = wait_async_op(&tresult, crypto_ahash_final(req));
  310. if (ret) {
  311. pr_err("alt: hash: final failed on test %d "
  312. "for %s: ret=%d\n", j, algo, -ret);
  313. goto out;
  314. }
  315. }
  316. if (memcmp(result, template[i].digest,
  317. crypto_ahash_digestsize(tfm))) {
  318. printk(KERN_ERR "alg: hash: Test %d failed for %s\n",
  319. j, algo);
  320. hexdump(result, crypto_ahash_digestsize(tfm));
  321. ret = -EINVAL;
  322. goto out;
  323. }
  324. }
  325. j = 0;
  326. for (i = 0; i < tcount; i++) {
  327. /* alignment tests are only done with continuous buffers */
  328. if (align_offset != 0)
  329. break;
  330. if (!template[i].np)
  331. continue;
  332. j++;
  333. memset(result, 0, digest_size);
  334. temp = 0;
  335. sg_init_table(sg, template[i].np);
  336. ret = -EINVAL;
  337. for (k = 0; k < template[i].np; k++) {
  338. if (WARN_ON(offset_in_page(IDX[k]) +
  339. template[i].tap[k] > PAGE_SIZE))
  340. goto out;
  341. sg_set_buf(&sg[k],
  342. memcpy(xbuf[IDX[k] >> PAGE_SHIFT] +
  343. offset_in_page(IDX[k]),
  344. template[i].plaintext + temp,
  345. template[i].tap[k]),
  346. template[i].tap[k]);
  347. temp += template[i].tap[k];
  348. }
  349. if (template[i].ksize) {
  350. if (template[i].ksize > MAX_KEYLEN) {
  351. pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
  352. j, algo, template[i].ksize, MAX_KEYLEN);
  353. ret = -EINVAL;
  354. goto out;
  355. }
  356. crypto_ahash_clear_flags(tfm, ~0);
  357. memcpy(key, template[i].key, template[i].ksize);
  358. ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
  359. if (ret) {
  360. printk(KERN_ERR "alg: hash: setkey "
  361. "failed on chunking test %d "
  362. "for %s: ret=%d\n", j, algo, -ret);
  363. goto out;
  364. }
  365. }
  366. ahash_request_set_crypt(req, sg, result, template[i].psize);
  367. ret = crypto_ahash_digest(req);
  368. switch (ret) {
  369. case 0:
  370. break;
  371. case -EINPROGRESS:
  372. case -EBUSY:
  373. wait_for_completion(&tresult.completion);
  374. reinit_completion(&tresult.completion);
  375. ret = tresult.err;
  376. if (!ret)
  377. break;
  378. /* fall through */
  379. default:
  380. printk(KERN_ERR "alg: hash: digest failed "
  381. "on chunking test %d for %s: "
  382. "ret=%d\n", j, algo, -ret);
  383. goto out;
  384. }
  385. if (memcmp(result, template[i].digest,
  386. crypto_ahash_digestsize(tfm))) {
  387. printk(KERN_ERR "alg: hash: Chunking test %d "
  388. "failed for %s\n", j, algo);
  389. hexdump(result, crypto_ahash_digestsize(tfm));
  390. ret = -EINVAL;
  391. goto out;
  392. }
  393. }
  394. /* partial update exercise */
  395. j = 0;
  396. for (i = 0; i < tcount; i++) {
  397. /* alignment tests are only done with continuous buffers */
  398. if (align_offset != 0)
  399. break;
  400. if (template[i].np < 2)
  401. continue;
  402. j++;
  403. memset(result, 0, digest_size);
  404. ret = -EINVAL;
  405. hash_buff = xbuf[0];
  406. memcpy(hash_buff, template[i].plaintext,
  407. template[i].tap[0]);
  408. sg_init_one(&sg[0], hash_buff, template[i].tap[0]);
  409. if (template[i].ksize) {
  410. crypto_ahash_clear_flags(tfm, ~0);
  411. if (template[i].ksize > MAX_KEYLEN) {
  412. pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
  413. j, algo, template[i].ksize, MAX_KEYLEN);
  414. ret = -EINVAL;
  415. goto out;
  416. }
  417. memcpy(key, template[i].key, template[i].ksize);
  418. ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
  419. if (ret) {
  420. pr_err("alg: hash: setkey failed on test %d for %s: ret=%d\n",
  421. j, algo, -ret);
  422. goto out;
  423. }
  424. }
  425. ahash_request_set_crypt(req, sg, result, template[i].tap[0]);
  426. ret = wait_async_op(&tresult, crypto_ahash_init(req));
  427. if (ret) {
  428. pr_err("alt: hash: init failed on test %d for %s: ret=%d\n",
  429. j, algo, -ret);
  430. goto out;
  431. }
  432. ret = wait_async_op(&tresult, crypto_ahash_update(req));
  433. if (ret) {
  434. pr_err("alt: hash: update failed on test %d for %s: ret=%d\n",
  435. j, algo, -ret);
  436. goto out;
  437. }
  438. temp = template[i].tap[0];
  439. for (k = 1; k < template[i].np; k++) {
  440. ret = ahash_partial_update(&req, tfm, &template[i],
  441. hash_buff, k, temp, &sg[0], algo, result,
  442. &tresult);
  443. if (ret) {
  444. pr_err("hash: partial update failed on test %d for %s: ret=%d\n",
  445. j, algo, -ret);
  446. goto out_noreq;
  447. }
  448. temp += template[i].tap[k];
  449. }
  450. ret = wait_async_op(&tresult, crypto_ahash_final(req));
  451. if (ret) {
  452. pr_err("alt: hash: final failed on test %d for %s: ret=%d\n",
  453. j, algo, -ret);
  454. goto out;
  455. }
  456. if (memcmp(result, template[i].digest,
  457. crypto_ahash_digestsize(tfm))) {
  458. pr_err("alg: hash: Partial Test %d failed for %s\n",
  459. j, algo);
  460. hexdump(result, crypto_ahash_digestsize(tfm));
  461. ret = -EINVAL;
  462. goto out;
  463. }
  464. }
  465. ret = 0;
  466. out:
  467. ahash_request_free(req);
  468. out_noreq:
  469. testmgr_free_buf(xbuf);
  470. out_nobuf:
  471. kfree(key);
  472. kfree(result);
  473. return ret;
  474. }
  475. static int test_hash(struct crypto_ahash *tfm,
  476. const struct hash_testvec *template,
  477. unsigned int tcount, bool use_digest)
  478. {
  479. unsigned int alignmask;
  480. int ret;
  481. ret = __test_hash(tfm, template, tcount, use_digest, 0);
  482. if (ret)
  483. return ret;
  484. /* test unaligned buffers, check with one byte offset */
  485. ret = __test_hash(tfm, template, tcount, use_digest, 1);
  486. if (ret)
  487. return ret;
  488. alignmask = crypto_tfm_alg_alignmask(&tfm->base);
  489. if (alignmask) {
  490. /* Check if alignment mask for tfm is correctly set. */
  491. ret = __test_hash(tfm, template, tcount, use_digest,
  492. alignmask + 1);
  493. if (ret)
  494. return ret;
  495. }
  496. return 0;
  497. }
  498. static int __test_aead(struct crypto_aead *tfm, int enc,
  499. const struct aead_testvec *template, unsigned int tcount,
  500. const bool diff_dst, const int align_offset)
  501. {
  502. const char *algo = crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));
  503. unsigned int i, j, k, n, temp;
  504. int ret = -ENOMEM;
  505. char *q;
  506. char *key;
  507. struct aead_request *req;
  508. struct scatterlist *sg;
  509. struct scatterlist *sgout;
  510. const char *e, *d;
  511. struct tcrypt_result result;
  512. unsigned int authsize, iv_len;
  513. void *input;
  514. void *output;
  515. void *assoc;
  516. char *iv;
  517. char *xbuf[XBUFSIZE];
  518. char *xoutbuf[XBUFSIZE];
  519. char *axbuf[XBUFSIZE];
  520. iv = kzalloc(MAX_IVLEN, GFP_KERNEL);
  521. if (!iv)
  522. return ret;
  523. key = kmalloc(MAX_KEYLEN, GFP_KERNEL);
  524. if (!key)
  525. goto out_noxbuf;
  526. if (testmgr_alloc_buf(xbuf))
  527. goto out_noxbuf;
  528. if (testmgr_alloc_buf(axbuf))
  529. goto out_noaxbuf;
  530. if (diff_dst && testmgr_alloc_buf(xoutbuf))
  531. goto out_nooutbuf;
  532. /* avoid "the frame size is larger than 1024 bytes" compiler warning */
  533. sg = kmalloc(sizeof(*sg) * 8 * (diff_dst ? 4 : 2), GFP_KERNEL);
  534. if (!sg)
  535. goto out_nosg;
  536. sgout = &sg[16];
  537. if (diff_dst)
  538. d = "-ddst";
  539. else
  540. d = "";
  541. if (enc == ENCRYPT)
  542. e = "encryption";
  543. else
  544. e = "decryption";
  545. init_completion(&result.completion);
  546. req = aead_request_alloc(tfm, GFP_KERNEL);
  547. if (!req) {
  548. pr_err("alg: aead%s: Failed to allocate request for %s\n",
  549. d, algo);
  550. goto out;
  551. }
  552. aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  553. tcrypt_complete, &result);
  554. iv_len = crypto_aead_ivsize(tfm);
  555. for (i = 0, j = 0; i < tcount; i++) {
  556. if (template[i].np)
  557. continue;
  558. j++;
  559. /* some templates have no input data but they will
  560. * touch input
  561. */
  562. input = xbuf[0];
  563. input += align_offset;
  564. assoc = axbuf[0];
  565. ret = -EINVAL;
  566. if (WARN_ON(align_offset + template[i].ilen >
  567. PAGE_SIZE || template[i].alen > PAGE_SIZE))
  568. goto out;
  569. memcpy(input, template[i].input, template[i].ilen);
  570. memcpy(assoc, template[i].assoc, template[i].alen);
  571. if (template[i].iv)
  572. memcpy(iv, template[i].iv, iv_len);
  573. else
  574. memset(iv, 0, iv_len);
  575. crypto_aead_clear_flags(tfm, ~0);
  576. if (template[i].wk)
  577. crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  578. if (template[i].klen > MAX_KEYLEN) {
  579. pr_err("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n",
  580. d, j, algo, template[i].klen,
  581. MAX_KEYLEN);
  582. ret = -EINVAL;
  583. goto out;
  584. }
  585. memcpy(key, template[i].key, template[i].klen);
  586. ret = crypto_aead_setkey(tfm, key, template[i].klen);
  587. if (template[i].fail == !ret) {
  588. pr_err("alg: aead%s: setkey failed on test %d for %s: flags=%x\n",
  589. d, j, algo, crypto_aead_get_flags(tfm));
  590. goto out;
  591. } else if (ret)
  592. continue;
  593. authsize = abs(template[i].rlen - template[i].ilen);
  594. ret = crypto_aead_setauthsize(tfm, authsize);
  595. if (ret) {
  596. pr_err("alg: aead%s: Failed to set authsize to %u on test %d for %s\n",
  597. d, authsize, j, algo);
  598. goto out;
  599. }
  600. k = !!template[i].alen;
  601. sg_init_table(sg, k + 1);
  602. sg_set_buf(&sg[0], assoc, template[i].alen);
  603. sg_set_buf(&sg[k], input,
  604. template[i].ilen + (enc ? authsize : 0));
  605. output = input;
  606. if (diff_dst) {
  607. sg_init_table(sgout, k + 1);
  608. sg_set_buf(&sgout[0], assoc, template[i].alen);
  609. output = xoutbuf[0];
  610. output += align_offset;
  611. sg_set_buf(&sgout[k], output,
  612. template[i].rlen + (enc ? 0 : authsize));
  613. }
  614. aead_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
  615. template[i].ilen, iv);
  616. aead_request_set_ad(req, template[i].alen);
  617. ret = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
  618. switch (ret) {
  619. case 0:
  620. if (template[i].novrfy) {
  621. /* verification was supposed to fail */
  622. pr_err("alg: aead%s: %s failed on test %d for %s: ret was 0, expected -EBADMSG\n",
  623. d, e, j, algo);
  624. /* so really, we got a bad message */
  625. ret = -EBADMSG;
  626. goto out;
  627. }
  628. break;
  629. case -EINPROGRESS:
  630. case -EBUSY:
  631. wait_for_completion(&result.completion);
  632. reinit_completion(&result.completion);
  633. ret = result.err;
  634. if (!ret)
  635. break;
  636. case -EBADMSG:
  637. if (template[i].novrfy)
  638. /* verification failure was expected */
  639. continue;
  640. /* fall through */
  641. default:
  642. pr_err("alg: aead%s: %s failed on test %d for %s: ret=%d\n",
  643. d, e, j, algo, -ret);
  644. goto out;
  645. }
  646. q = output;
  647. if (memcmp(q, template[i].result, template[i].rlen)) {
  648. pr_err("alg: aead%s: Test %d failed on %s for %s\n",
  649. d, j, e, algo);
  650. hexdump(q, template[i].rlen);
  651. ret = -EINVAL;
  652. goto out;
  653. }
  654. }
  655. for (i = 0, j = 0; i < tcount; i++) {
  656. /* alignment tests are only done with continuous buffers */
  657. if (align_offset != 0)
  658. break;
  659. if (!template[i].np)
  660. continue;
  661. j++;
  662. if (template[i].iv)
  663. memcpy(iv, template[i].iv, iv_len);
  664. else
  665. memset(iv, 0, MAX_IVLEN);
  666. crypto_aead_clear_flags(tfm, ~0);
  667. if (template[i].wk)
  668. crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  669. if (template[i].klen > MAX_KEYLEN) {
  670. pr_err("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n",
  671. d, j, algo, template[i].klen, MAX_KEYLEN);
  672. ret = -EINVAL;
  673. goto out;
  674. }
  675. memcpy(key, template[i].key, template[i].klen);
  676. ret = crypto_aead_setkey(tfm, key, template[i].klen);
  677. if (template[i].fail == !ret) {
  678. pr_err("alg: aead%s: setkey failed on chunk test %d for %s: flags=%x\n",
  679. d, j, algo, crypto_aead_get_flags(tfm));
  680. goto out;
  681. } else if (ret)
  682. continue;
  683. authsize = abs(template[i].rlen - template[i].ilen);
  684. ret = -EINVAL;
  685. sg_init_table(sg, template[i].anp + template[i].np);
  686. if (diff_dst)
  687. sg_init_table(sgout, template[i].anp + template[i].np);
  688. ret = -EINVAL;
  689. for (k = 0, temp = 0; k < template[i].anp; k++) {
  690. if (WARN_ON(offset_in_page(IDX[k]) +
  691. template[i].atap[k] > PAGE_SIZE))
  692. goto out;
  693. sg_set_buf(&sg[k],
  694. memcpy(axbuf[IDX[k] >> PAGE_SHIFT] +
  695. offset_in_page(IDX[k]),
  696. template[i].assoc + temp,
  697. template[i].atap[k]),
  698. template[i].atap[k]);
  699. if (diff_dst)
  700. sg_set_buf(&sgout[k],
  701. axbuf[IDX[k] >> PAGE_SHIFT] +
  702. offset_in_page(IDX[k]),
  703. template[i].atap[k]);
  704. temp += template[i].atap[k];
  705. }
  706. for (k = 0, temp = 0; k < template[i].np; k++) {
  707. if (WARN_ON(offset_in_page(IDX[k]) +
  708. template[i].tap[k] > PAGE_SIZE))
  709. goto out;
  710. q = xbuf[IDX[k] >> PAGE_SHIFT] + offset_in_page(IDX[k]);
  711. memcpy(q, template[i].input + temp, template[i].tap[k]);
  712. sg_set_buf(&sg[template[i].anp + k],
  713. q, template[i].tap[k]);
  714. if (diff_dst) {
  715. q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
  716. offset_in_page(IDX[k]);
  717. memset(q, 0, template[i].tap[k]);
  718. sg_set_buf(&sgout[template[i].anp + k],
  719. q, template[i].tap[k]);
  720. }
  721. n = template[i].tap[k];
  722. if (k == template[i].np - 1 && enc)
  723. n += authsize;
  724. if (offset_in_page(q) + n < PAGE_SIZE)
  725. q[n] = 0;
  726. temp += template[i].tap[k];
  727. }
  728. ret = crypto_aead_setauthsize(tfm, authsize);
  729. if (ret) {
  730. pr_err("alg: aead%s: Failed to set authsize to %u on chunk test %d for %s\n",
  731. d, authsize, j, algo);
  732. goto out;
  733. }
  734. if (enc) {
  735. if (WARN_ON(sg[template[i].anp + k - 1].offset +
  736. sg[template[i].anp + k - 1].length +
  737. authsize > PAGE_SIZE)) {
  738. ret = -EINVAL;
  739. goto out;
  740. }
  741. if (diff_dst)
  742. sgout[template[i].anp + k - 1].length +=
  743. authsize;
  744. sg[template[i].anp + k - 1].length += authsize;
  745. }
  746. aead_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
  747. template[i].ilen,
  748. iv);
  749. aead_request_set_ad(req, template[i].alen);
  750. ret = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
  751. switch (ret) {
  752. case 0:
  753. if (template[i].novrfy) {
  754. /* verification was supposed to fail */
  755. pr_err("alg: aead%s: %s failed on chunk test %d for %s: ret was 0, expected -EBADMSG\n",
  756. d, e, j, algo);
  757. /* so really, we got a bad message */
  758. ret = -EBADMSG;
  759. goto out;
  760. }
  761. break;
  762. case -EINPROGRESS:
  763. case -EBUSY:
  764. wait_for_completion(&result.completion);
  765. reinit_completion(&result.completion);
  766. ret = result.err;
  767. if (!ret)
  768. break;
  769. case -EBADMSG:
  770. if (template[i].novrfy)
  771. /* verification failure was expected */
  772. continue;
  773. /* fall through */
  774. default:
  775. pr_err("alg: aead%s: %s failed on chunk test %d for %s: ret=%d\n",
  776. d, e, j, algo, -ret);
  777. goto out;
  778. }
  779. ret = -EINVAL;
  780. for (k = 0, temp = 0; k < template[i].np; k++) {
  781. if (diff_dst)
  782. q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
  783. offset_in_page(IDX[k]);
  784. else
  785. q = xbuf[IDX[k] >> PAGE_SHIFT] +
  786. offset_in_page(IDX[k]);
  787. n = template[i].tap[k];
  788. if (k == template[i].np - 1)
  789. n += enc ? authsize : -authsize;
  790. if (memcmp(q, template[i].result + temp, n)) {
  791. pr_err("alg: aead%s: Chunk test %d failed on %s at page %u for %s\n",
  792. d, j, e, k, algo);
  793. hexdump(q, n);
  794. goto out;
  795. }
  796. q += n;
  797. if (k == template[i].np - 1 && !enc) {
  798. if (!diff_dst &&
  799. memcmp(q, template[i].input +
  800. temp + n, authsize))
  801. n = authsize;
  802. else
  803. n = 0;
  804. } else {
  805. for (n = 0; offset_in_page(q + n) && q[n]; n++)
  806. ;
  807. }
  808. if (n) {
  809. pr_err("alg: aead%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n",
  810. d, j, e, k, algo, n);
  811. hexdump(q, n);
  812. goto out;
  813. }
  814. temp += template[i].tap[k];
  815. }
  816. }
  817. ret = 0;
  818. out:
  819. aead_request_free(req);
  820. kfree(sg);
  821. out_nosg:
  822. if (diff_dst)
  823. testmgr_free_buf(xoutbuf);
  824. out_nooutbuf:
  825. testmgr_free_buf(axbuf);
  826. out_noaxbuf:
  827. testmgr_free_buf(xbuf);
  828. out_noxbuf:
  829. kfree(key);
  830. kfree(iv);
  831. return ret;
  832. }
  833. static int test_aead(struct crypto_aead *tfm, int enc,
  834. const struct aead_testvec *template, unsigned int tcount)
  835. {
  836. unsigned int alignmask;
  837. int ret;
  838. /* test 'dst == src' case */
  839. ret = __test_aead(tfm, enc, template, tcount, false, 0);
  840. if (ret)
  841. return ret;
  842. /* test 'dst != src' case */
  843. ret = __test_aead(tfm, enc, template, tcount, true, 0);
  844. if (ret)
  845. return ret;
  846. /* test unaligned buffers, check with one byte offset */
  847. ret = __test_aead(tfm, enc, template, tcount, true, 1);
  848. if (ret)
  849. return ret;
  850. alignmask = crypto_tfm_alg_alignmask(&tfm->base);
  851. if (alignmask) {
  852. /* Check if alignment mask for tfm is correctly set. */
  853. ret = __test_aead(tfm, enc, template, tcount, true,
  854. alignmask + 1);
  855. if (ret)
  856. return ret;
  857. }
  858. return 0;
  859. }
  860. static int test_cipher(struct crypto_cipher *tfm, int enc,
  861. const struct cipher_testvec *template,
  862. unsigned int tcount)
  863. {
  864. const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
  865. unsigned int i, j, k;
  866. char *q;
  867. const char *e;
  868. void *data;
  869. char *xbuf[XBUFSIZE];
  870. int ret = -ENOMEM;
  871. if (testmgr_alloc_buf(xbuf))
  872. goto out_nobuf;
  873. if (enc == ENCRYPT)
  874. e = "encryption";
  875. else
  876. e = "decryption";
  877. j = 0;
  878. for (i = 0; i < tcount; i++) {
  879. if (template[i].np)
  880. continue;
  881. if (fips_enabled && template[i].fips_skip)
  882. continue;
  883. j++;
  884. ret = -EINVAL;
  885. if (WARN_ON(template[i].ilen > PAGE_SIZE))
  886. goto out;
  887. data = xbuf[0];
  888. memcpy(data, template[i].input, template[i].ilen);
  889. crypto_cipher_clear_flags(tfm, ~0);
  890. if (template[i].wk)
  891. crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  892. ret = crypto_cipher_setkey(tfm, template[i].key,
  893. template[i].klen);
  894. if (template[i].fail == !ret) {
  895. printk(KERN_ERR "alg: cipher: setkey failed "
  896. "on test %d for %s: flags=%x\n", j,
  897. algo, crypto_cipher_get_flags(tfm));
  898. goto out;
  899. } else if (ret)
  900. continue;
  901. for (k = 0; k < template[i].ilen;
  902. k += crypto_cipher_blocksize(tfm)) {
  903. if (enc)
  904. crypto_cipher_encrypt_one(tfm, data + k,
  905. data + k);
  906. else
  907. crypto_cipher_decrypt_one(tfm, data + k,
  908. data + k);
  909. }
  910. q = data;
  911. if (memcmp(q, template[i].result, template[i].rlen)) {
  912. printk(KERN_ERR "alg: cipher: Test %d failed "
  913. "on %s for %s\n", j, e, algo);
  914. hexdump(q, template[i].rlen);
  915. ret = -EINVAL;
  916. goto out;
  917. }
  918. }
  919. ret = 0;
  920. out:
  921. testmgr_free_buf(xbuf);
  922. out_nobuf:
  923. return ret;
  924. }
  925. static int __test_skcipher(struct crypto_skcipher *tfm, int enc,
  926. const struct cipher_testvec *template,
  927. unsigned int tcount,
  928. const bool diff_dst, const int align_offset)
  929. {
  930. const char *algo =
  931. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm));
  932. unsigned int i, j, k, n, temp;
  933. char *q;
  934. struct skcipher_request *req;
  935. struct scatterlist sg[8];
  936. struct scatterlist sgout[8];
  937. const char *e, *d;
  938. struct tcrypt_result result;
  939. void *data;
  940. char iv[MAX_IVLEN];
  941. char *xbuf[XBUFSIZE];
  942. char *xoutbuf[XBUFSIZE];
  943. int ret = -ENOMEM;
  944. unsigned int ivsize = crypto_skcipher_ivsize(tfm);
  945. if (testmgr_alloc_buf(xbuf))
  946. goto out_nobuf;
  947. if (diff_dst && testmgr_alloc_buf(xoutbuf))
  948. goto out_nooutbuf;
  949. if (diff_dst)
  950. d = "-ddst";
  951. else
  952. d = "";
  953. if (enc == ENCRYPT)
  954. e = "encryption";
  955. else
  956. e = "decryption";
  957. init_completion(&result.completion);
  958. req = skcipher_request_alloc(tfm, GFP_KERNEL);
  959. if (!req) {
  960. pr_err("alg: skcipher%s: Failed to allocate request for %s\n",
  961. d, algo);
  962. goto out;
  963. }
  964. skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  965. tcrypt_complete, &result);
  966. j = 0;
  967. for (i = 0; i < tcount; i++) {
  968. if (template[i].np && !template[i].also_non_np)
  969. continue;
  970. if (fips_enabled && template[i].fips_skip)
  971. continue;
  972. if (template[i].iv)
  973. memcpy(iv, template[i].iv, ivsize);
  974. else
  975. memset(iv, 0, MAX_IVLEN);
  976. j++;
  977. ret = -EINVAL;
  978. if (WARN_ON(align_offset + template[i].ilen > PAGE_SIZE))
  979. goto out;
  980. data = xbuf[0];
  981. data += align_offset;
  982. memcpy(data, template[i].input, template[i].ilen);
  983. crypto_skcipher_clear_flags(tfm, ~0);
  984. if (template[i].wk)
  985. crypto_skcipher_set_flags(tfm,
  986. CRYPTO_TFM_REQ_WEAK_KEY);
  987. ret = crypto_skcipher_setkey(tfm, template[i].key,
  988. template[i].klen);
  989. if (template[i].fail == !ret) {
  990. pr_err("alg: skcipher%s: setkey failed on test %d for %s: flags=%x\n",
  991. d, j, algo, crypto_skcipher_get_flags(tfm));
  992. goto out;
  993. } else if (ret)
  994. continue;
  995. sg_init_one(&sg[0], data, template[i].ilen);
  996. if (diff_dst) {
  997. data = xoutbuf[0];
  998. data += align_offset;
  999. sg_init_one(&sgout[0], data, template[i].ilen);
  1000. }
  1001. skcipher_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
  1002. template[i].ilen, iv);
  1003. ret = enc ? crypto_skcipher_encrypt(req) :
  1004. crypto_skcipher_decrypt(req);
  1005. switch (ret) {
  1006. case 0:
  1007. break;
  1008. case -EINPROGRESS:
  1009. case -EBUSY:
  1010. wait_for_completion(&result.completion);
  1011. reinit_completion(&result.completion);
  1012. ret = result.err;
  1013. if (!ret)
  1014. break;
  1015. /* fall through */
  1016. default:
  1017. pr_err("alg: skcipher%s: %s failed on test %d for %s: ret=%d\n",
  1018. d, e, j, algo, -ret);
  1019. goto out;
  1020. }
  1021. q = data;
  1022. if (memcmp(q, template[i].result, template[i].rlen)) {
  1023. pr_err("alg: skcipher%s: Test %d failed (invalid result) on %s for %s\n",
  1024. d, j, e, algo);
  1025. hexdump(q, template[i].rlen);
  1026. ret = -EINVAL;
  1027. goto out;
  1028. }
  1029. if (template[i].iv_out &&
  1030. memcmp(iv, template[i].iv_out,
  1031. crypto_skcipher_ivsize(tfm))) {
  1032. pr_err("alg: skcipher%s: Test %d failed (invalid output IV) on %s for %s\n",
  1033. d, j, e, algo);
  1034. hexdump(iv, crypto_skcipher_ivsize(tfm));
  1035. ret = -EINVAL;
  1036. goto out;
  1037. }
  1038. }
  1039. j = 0;
  1040. for (i = 0; i < tcount; i++) {
  1041. /* alignment tests are only done with continuous buffers */
  1042. if (align_offset != 0)
  1043. break;
  1044. if (!template[i].np)
  1045. continue;
  1046. if (fips_enabled && template[i].fips_skip)
  1047. continue;
  1048. if (template[i].iv)
  1049. memcpy(iv, template[i].iv, ivsize);
  1050. else
  1051. memset(iv, 0, MAX_IVLEN);
  1052. j++;
  1053. crypto_skcipher_clear_flags(tfm, ~0);
  1054. if (template[i].wk)
  1055. crypto_skcipher_set_flags(tfm,
  1056. CRYPTO_TFM_REQ_WEAK_KEY);
  1057. ret = crypto_skcipher_setkey(tfm, template[i].key,
  1058. template[i].klen);
  1059. if (template[i].fail == !ret) {
  1060. pr_err("alg: skcipher%s: setkey failed on chunk test %d for %s: flags=%x\n",
  1061. d, j, algo, crypto_skcipher_get_flags(tfm));
  1062. goto out;
  1063. } else if (ret)
  1064. continue;
  1065. temp = 0;
  1066. ret = -EINVAL;
  1067. sg_init_table(sg, template[i].np);
  1068. if (diff_dst)
  1069. sg_init_table(sgout, template[i].np);
  1070. for (k = 0; k < template[i].np; k++) {
  1071. if (WARN_ON(offset_in_page(IDX[k]) +
  1072. template[i].tap[k] > PAGE_SIZE))
  1073. goto out;
  1074. q = xbuf[IDX[k] >> PAGE_SHIFT] + offset_in_page(IDX[k]);
  1075. memcpy(q, template[i].input + temp, template[i].tap[k]);
  1076. if (offset_in_page(q) + template[i].tap[k] < PAGE_SIZE)
  1077. q[template[i].tap[k]] = 0;
  1078. sg_set_buf(&sg[k], q, template[i].tap[k]);
  1079. if (diff_dst) {
  1080. q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
  1081. offset_in_page(IDX[k]);
  1082. sg_set_buf(&sgout[k], q, template[i].tap[k]);
  1083. memset(q, 0, template[i].tap[k]);
  1084. if (offset_in_page(q) +
  1085. template[i].tap[k] < PAGE_SIZE)
  1086. q[template[i].tap[k]] = 0;
  1087. }
  1088. temp += template[i].tap[k];
  1089. }
  1090. skcipher_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
  1091. template[i].ilen, iv);
  1092. ret = enc ? crypto_skcipher_encrypt(req) :
  1093. crypto_skcipher_decrypt(req);
  1094. switch (ret) {
  1095. case 0:
  1096. break;
  1097. case -EINPROGRESS:
  1098. case -EBUSY:
  1099. wait_for_completion(&result.completion);
  1100. reinit_completion(&result.completion);
  1101. ret = result.err;
  1102. if (!ret)
  1103. break;
  1104. /* fall through */
  1105. default:
  1106. pr_err("alg: skcipher%s: %s failed on chunk test %d for %s: ret=%d\n",
  1107. d, e, j, algo, -ret);
  1108. goto out;
  1109. }
  1110. temp = 0;
  1111. ret = -EINVAL;
  1112. for (k = 0; k < template[i].np; k++) {
  1113. if (diff_dst)
  1114. q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
  1115. offset_in_page(IDX[k]);
  1116. else
  1117. q = xbuf[IDX[k] >> PAGE_SHIFT] +
  1118. offset_in_page(IDX[k]);
  1119. if (memcmp(q, template[i].result + temp,
  1120. template[i].tap[k])) {
  1121. pr_err("alg: skcipher%s: Chunk test %d failed on %s at page %u for %s\n",
  1122. d, j, e, k, algo);
  1123. hexdump(q, template[i].tap[k]);
  1124. goto out;
  1125. }
  1126. q += template[i].tap[k];
  1127. for (n = 0; offset_in_page(q + n) && q[n]; n++)
  1128. ;
  1129. if (n) {
  1130. pr_err("alg: skcipher%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n",
  1131. d, j, e, k, algo, n);
  1132. hexdump(q, n);
  1133. goto out;
  1134. }
  1135. temp += template[i].tap[k];
  1136. }
  1137. }
  1138. ret = 0;
  1139. out:
  1140. skcipher_request_free(req);
  1141. if (diff_dst)
  1142. testmgr_free_buf(xoutbuf);
  1143. out_nooutbuf:
  1144. testmgr_free_buf(xbuf);
  1145. out_nobuf:
  1146. return ret;
  1147. }
  1148. static int test_skcipher(struct crypto_skcipher *tfm, int enc,
  1149. const struct cipher_testvec *template,
  1150. unsigned int tcount)
  1151. {
  1152. unsigned int alignmask;
  1153. int ret;
  1154. /* test 'dst == src' case */
  1155. ret = __test_skcipher(tfm, enc, template, tcount, false, 0);
  1156. if (ret)
  1157. return ret;
  1158. /* test 'dst != src' case */
  1159. ret = __test_skcipher(tfm, enc, template, tcount, true, 0);
  1160. if (ret)
  1161. return ret;
  1162. /* test unaligned buffers, check with one byte offset */
  1163. ret = __test_skcipher(tfm, enc, template, tcount, true, 1);
  1164. if (ret)
  1165. return ret;
  1166. alignmask = crypto_tfm_alg_alignmask(&tfm->base);
  1167. if (alignmask) {
  1168. /* Check if alignment mask for tfm is correctly set. */
  1169. ret = __test_skcipher(tfm, enc, template, tcount, true,
  1170. alignmask + 1);
  1171. if (ret)
  1172. return ret;
  1173. }
  1174. return 0;
  1175. }
  1176. static int test_comp(struct crypto_comp *tfm,
  1177. const struct comp_testvec *ctemplate,
  1178. const struct comp_testvec *dtemplate,
  1179. int ctcount, int dtcount)
  1180. {
  1181. const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
  1182. unsigned int i;
  1183. char result[COMP_BUF_SIZE];
  1184. int ret;
  1185. for (i = 0; i < ctcount; i++) {
  1186. int ilen;
  1187. unsigned int dlen = COMP_BUF_SIZE;
  1188. memset(result, 0, sizeof (result));
  1189. ilen = ctemplate[i].inlen;
  1190. ret = crypto_comp_compress(tfm, ctemplate[i].input,
  1191. ilen, result, &dlen);
  1192. if (ret) {
  1193. printk(KERN_ERR "alg: comp: compression failed "
  1194. "on test %d for %s: ret=%d\n", i + 1, algo,
  1195. -ret);
  1196. goto out;
  1197. }
  1198. if (dlen != ctemplate[i].outlen) {
  1199. printk(KERN_ERR "alg: comp: Compression test %d "
  1200. "failed for %s: output len = %d\n", i + 1, algo,
  1201. dlen);
  1202. ret = -EINVAL;
  1203. goto out;
  1204. }
  1205. if (memcmp(result, ctemplate[i].output, dlen)) {
  1206. printk(KERN_ERR "alg: comp: Compression test %d "
  1207. "failed for %s\n", i + 1, algo);
  1208. hexdump(result, dlen);
  1209. ret = -EINVAL;
  1210. goto out;
  1211. }
  1212. }
  1213. for (i = 0; i < dtcount; i++) {
  1214. int ilen;
  1215. unsigned int dlen = COMP_BUF_SIZE;
  1216. memset(result, 0, sizeof (result));
  1217. ilen = dtemplate[i].inlen;
  1218. ret = crypto_comp_decompress(tfm, dtemplate[i].input,
  1219. ilen, result, &dlen);
  1220. if (ret) {
  1221. printk(KERN_ERR "alg: comp: decompression failed "
  1222. "on test %d for %s: ret=%d\n", i + 1, algo,
  1223. -ret);
  1224. goto out;
  1225. }
  1226. if (dlen != dtemplate[i].outlen) {
  1227. printk(KERN_ERR "alg: comp: Decompression test %d "
  1228. "failed for %s: output len = %d\n", i + 1, algo,
  1229. dlen);
  1230. ret = -EINVAL;
  1231. goto out;
  1232. }
  1233. if (memcmp(result, dtemplate[i].output, dlen)) {
  1234. printk(KERN_ERR "alg: comp: Decompression test %d "
  1235. "failed for %s\n", i + 1, algo);
  1236. hexdump(result, dlen);
  1237. ret = -EINVAL;
  1238. goto out;
  1239. }
  1240. }
  1241. ret = 0;
  1242. out:
  1243. return ret;
  1244. }
  1245. static int test_acomp(struct crypto_acomp *tfm,
  1246. const struct comp_testvec *ctemplate,
  1247. const struct comp_testvec *dtemplate,
  1248. int ctcount, int dtcount)
  1249. {
  1250. const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
  1251. unsigned int i;
  1252. char *output, *decomp_out;
  1253. int ret;
  1254. struct scatterlist src, dst;
  1255. struct acomp_req *req;
  1256. struct tcrypt_result result;
  1257. output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
  1258. if (!output)
  1259. return -ENOMEM;
  1260. decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
  1261. if (!decomp_out) {
  1262. kfree(output);
  1263. return -ENOMEM;
  1264. }
  1265. for (i = 0; i < ctcount; i++) {
  1266. unsigned int dlen = COMP_BUF_SIZE;
  1267. int ilen = ctemplate[i].inlen;
  1268. void *input_vec;
  1269. input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
  1270. if (!input_vec) {
  1271. ret = -ENOMEM;
  1272. goto out;
  1273. }
  1274. memset(output, 0, dlen);
  1275. init_completion(&result.completion);
  1276. sg_init_one(&src, input_vec, ilen);
  1277. sg_init_one(&dst, output, dlen);
  1278. req = acomp_request_alloc(tfm);
  1279. if (!req) {
  1280. pr_err("alg: acomp: request alloc failed for %s\n",
  1281. algo);
  1282. kfree(input_vec);
  1283. ret = -ENOMEM;
  1284. goto out;
  1285. }
  1286. acomp_request_set_params(req, &src, &dst, ilen, dlen);
  1287. acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  1288. tcrypt_complete, &result);
  1289. ret = wait_async_op(&result, crypto_acomp_compress(req));
  1290. if (ret) {
  1291. pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
  1292. i + 1, algo, -ret);
  1293. kfree(input_vec);
  1294. acomp_request_free(req);
  1295. goto out;
  1296. }
  1297. ilen = req->dlen;
  1298. dlen = COMP_BUF_SIZE;
  1299. sg_init_one(&src, output, ilen);
  1300. sg_init_one(&dst, decomp_out, dlen);
  1301. init_completion(&result.completion);
  1302. acomp_request_set_params(req, &src, &dst, ilen, dlen);
  1303. ret = wait_async_op(&result, crypto_acomp_decompress(req));
  1304. if (ret) {
  1305. pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
  1306. i + 1, algo, -ret);
  1307. kfree(input_vec);
  1308. acomp_request_free(req);
  1309. goto out;
  1310. }
  1311. if (req->dlen != ctemplate[i].inlen) {
  1312. pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
  1313. i + 1, algo, req->dlen);
  1314. ret = -EINVAL;
  1315. kfree(input_vec);
  1316. acomp_request_free(req);
  1317. goto out;
  1318. }
  1319. if (memcmp(input_vec, decomp_out, req->dlen)) {
  1320. pr_err("alg: acomp: Compression test %d failed for %s\n",
  1321. i + 1, algo);
  1322. hexdump(output, req->dlen);
  1323. ret = -EINVAL;
  1324. kfree(input_vec);
  1325. acomp_request_free(req);
  1326. goto out;
  1327. }
  1328. kfree(input_vec);
  1329. acomp_request_free(req);
  1330. }
  1331. for (i = 0; i < dtcount; i++) {
  1332. unsigned int dlen = COMP_BUF_SIZE;
  1333. int ilen = dtemplate[i].inlen;
  1334. void *input_vec;
  1335. input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
  1336. if (!input_vec) {
  1337. ret = -ENOMEM;
  1338. goto out;
  1339. }
  1340. memset(output, 0, dlen);
  1341. init_completion(&result.completion);
  1342. sg_init_one(&src, input_vec, ilen);
  1343. sg_init_one(&dst, output, dlen);
  1344. req = acomp_request_alloc(tfm);
  1345. if (!req) {
  1346. pr_err("alg: acomp: request alloc failed for %s\n",
  1347. algo);
  1348. kfree(input_vec);
  1349. ret = -ENOMEM;
  1350. goto out;
  1351. }
  1352. acomp_request_set_params(req, &src, &dst, ilen, dlen);
  1353. acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  1354. tcrypt_complete, &result);
  1355. ret = wait_async_op(&result, crypto_acomp_decompress(req));
  1356. if (ret) {
  1357. pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
  1358. i + 1, algo, -ret);
  1359. kfree(input_vec);
  1360. acomp_request_free(req);
  1361. goto out;
  1362. }
  1363. if (req->dlen != dtemplate[i].outlen) {
  1364. pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
  1365. i + 1, algo, req->dlen);
  1366. ret = -EINVAL;
  1367. kfree(input_vec);
  1368. acomp_request_free(req);
  1369. goto out;
  1370. }
  1371. if (memcmp(output, dtemplate[i].output, req->dlen)) {
  1372. pr_err("alg: acomp: Decompression test %d failed for %s\n",
  1373. i + 1, algo);
  1374. hexdump(output, req->dlen);
  1375. ret = -EINVAL;
  1376. kfree(input_vec);
  1377. acomp_request_free(req);
  1378. goto out;
  1379. }
  1380. kfree(input_vec);
  1381. acomp_request_free(req);
  1382. }
  1383. ret = 0;
  1384. out:
  1385. kfree(decomp_out);
  1386. kfree(output);
  1387. return ret;
  1388. }
  1389. static int test_cprng(struct crypto_rng *tfm,
  1390. const struct cprng_testvec *template,
  1391. unsigned int tcount)
  1392. {
  1393. const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
  1394. int err = 0, i, j, seedsize;
  1395. u8 *seed;
  1396. char result[32];
  1397. seedsize = crypto_rng_seedsize(tfm);
  1398. seed = kmalloc(seedsize, GFP_KERNEL);
  1399. if (!seed) {
  1400. printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
  1401. "for %s\n", algo);
  1402. return -ENOMEM;
  1403. }
  1404. for (i = 0; i < tcount; i++) {
  1405. memset(result, 0, 32);
  1406. memcpy(seed, template[i].v, template[i].vlen);
  1407. memcpy(seed + template[i].vlen, template[i].key,
  1408. template[i].klen);
  1409. memcpy(seed + template[i].vlen + template[i].klen,
  1410. template[i].dt, template[i].dtlen);
  1411. err = crypto_rng_reset(tfm, seed, seedsize);
  1412. if (err) {
  1413. printk(KERN_ERR "alg: cprng: Failed to reset rng "
  1414. "for %s\n", algo);
  1415. goto out;
  1416. }
  1417. for (j = 0; j < template[i].loops; j++) {
  1418. err = crypto_rng_get_bytes(tfm, result,
  1419. template[i].rlen);
  1420. if (err < 0) {
  1421. printk(KERN_ERR "alg: cprng: Failed to obtain "
  1422. "the correct amount of random data for "
  1423. "%s (requested %d)\n", algo,
  1424. template[i].rlen);
  1425. goto out;
  1426. }
  1427. }
  1428. err = memcmp(result, template[i].result,
  1429. template[i].rlen);
  1430. if (err) {
  1431. printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
  1432. i, algo);
  1433. hexdump(result, template[i].rlen);
  1434. err = -EINVAL;
  1435. goto out;
  1436. }
  1437. }
  1438. out:
  1439. kfree(seed);
  1440. return err;
  1441. }
  1442. static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
  1443. u32 type, u32 mask)
  1444. {
  1445. struct crypto_aead *tfm;
  1446. int err = 0;
  1447. tfm = crypto_alloc_aead(driver, type, mask);
  1448. if (IS_ERR(tfm)) {
  1449. printk(KERN_ERR "alg: aead: Failed to load transform for %s: "
  1450. "%ld\n", driver, PTR_ERR(tfm));
  1451. return PTR_ERR(tfm);
  1452. }
  1453. if (desc->suite.aead.enc.vecs) {
  1454. err = test_aead(tfm, ENCRYPT, desc->suite.aead.enc.vecs,
  1455. desc->suite.aead.enc.count);
  1456. if (err)
  1457. goto out;
  1458. }
  1459. if (!err && desc->suite.aead.dec.vecs)
  1460. err = test_aead(tfm, DECRYPT, desc->suite.aead.dec.vecs,
  1461. desc->suite.aead.dec.count);
  1462. out:
  1463. crypto_free_aead(tfm);
  1464. return err;
  1465. }
  1466. static int alg_test_cipher(const struct alg_test_desc *desc,
  1467. const char *driver, u32 type, u32 mask)
  1468. {
  1469. struct crypto_cipher *tfm;
  1470. int err = 0;
  1471. tfm = crypto_alloc_cipher(driver, type, mask);
  1472. if (IS_ERR(tfm)) {
  1473. printk(KERN_ERR "alg: cipher: Failed to load transform for "
  1474. "%s: %ld\n", driver, PTR_ERR(tfm));
  1475. return PTR_ERR(tfm);
  1476. }
  1477. if (desc->suite.cipher.enc.vecs) {
  1478. err = test_cipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
  1479. desc->suite.cipher.enc.count);
  1480. if (err)
  1481. goto out;
  1482. }
  1483. if (desc->suite.cipher.dec.vecs)
  1484. err = test_cipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
  1485. desc->suite.cipher.dec.count);
  1486. out:
  1487. crypto_free_cipher(tfm);
  1488. return err;
  1489. }
  1490. static int alg_test_skcipher(const struct alg_test_desc *desc,
  1491. const char *driver, u32 type, u32 mask)
  1492. {
  1493. struct crypto_skcipher *tfm;
  1494. int err = 0;
  1495. tfm = crypto_alloc_skcipher(driver, type, mask);
  1496. if (IS_ERR(tfm)) {
  1497. printk(KERN_ERR "alg: skcipher: Failed to load transform for "
  1498. "%s: %ld\n", driver, PTR_ERR(tfm));
  1499. return PTR_ERR(tfm);
  1500. }
  1501. if (desc->suite.cipher.enc.vecs) {
  1502. err = test_skcipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
  1503. desc->suite.cipher.enc.count);
  1504. if (err)
  1505. goto out;
  1506. }
  1507. if (desc->suite.cipher.dec.vecs)
  1508. err = test_skcipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
  1509. desc->suite.cipher.dec.count);
  1510. out:
  1511. crypto_free_skcipher(tfm);
  1512. return err;
  1513. }
  1514. static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
  1515. u32 type, u32 mask)
  1516. {
  1517. struct crypto_comp *comp;
  1518. struct crypto_acomp *acomp;
  1519. int err;
  1520. u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
  1521. if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
  1522. acomp = crypto_alloc_acomp(driver, type, mask);
  1523. if (IS_ERR(acomp)) {
  1524. pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
  1525. driver, PTR_ERR(acomp));
  1526. return PTR_ERR(acomp);
  1527. }
  1528. err = test_acomp(acomp, desc->suite.comp.comp.vecs,
  1529. desc->suite.comp.decomp.vecs,
  1530. desc->suite.comp.comp.count,
  1531. desc->suite.comp.decomp.count);
  1532. crypto_free_acomp(acomp);
  1533. } else {
  1534. comp = crypto_alloc_comp(driver, type, mask);
  1535. if (IS_ERR(comp)) {
  1536. pr_err("alg: comp: Failed to load transform for %s: %ld\n",
  1537. driver, PTR_ERR(comp));
  1538. return PTR_ERR(comp);
  1539. }
  1540. err = test_comp(comp, desc->suite.comp.comp.vecs,
  1541. desc->suite.comp.decomp.vecs,
  1542. desc->suite.comp.comp.count,
  1543. desc->suite.comp.decomp.count);
  1544. crypto_free_comp(comp);
  1545. }
  1546. return err;
  1547. }
  1548. static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
  1549. u32 type, u32 mask)
  1550. {
  1551. struct crypto_ahash *tfm;
  1552. int err;
  1553. tfm = crypto_alloc_ahash(driver, type, mask);
  1554. if (IS_ERR(tfm)) {
  1555. printk(KERN_ERR "alg: hash: Failed to load transform for %s: "
  1556. "%ld\n", driver, PTR_ERR(tfm));
  1557. return PTR_ERR(tfm);
  1558. }
  1559. err = test_hash(tfm, desc->suite.hash.vecs,
  1560. desc->suite.hash.count, true);
  1561. if (!err)
  1562. err = test_hash(tfm, desc->suite.hash.vecs,
  1563. desc->suite.hash.count, false);
  1564. crypto_free_ahash(tfm);
  1565. return err;
  1566. }
  1567. static int alg_test_crc32c(const struct alg_test_desc *desc,
  1568. const char *driver, u32 type, u32 mask)
  1569. {
  1570. struct crypto_shash *tfm;
  1571. u32 val;
  1572. int err;
  1573. err = alg_test_hash(desc, driver, type, mask);
  1574. if (err)
  1575. goto out;
  1576. tfm = crypto_alloc_shash(driver, type, mask);
  1577. if (IS_ERR(tfm)) {
  1578. printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
  1579. "%ld\n", driver, PTR_ERR(tfm));
  1580. err = PTR_ERR(tfm);
  1581. goto out;
  1582. }
  1583. do {
  1584. SHASH_DESC_ON_STACK(shash, tfm);
  1585. u32 *ctx = (u32 *)shash_desc_ctx(shash);
  1586. shash->tfm = tfm;
  1587. shash->flags = 0;
  1588. *ctx = le32_to_cpu(420553207);
  1589. err = crypto_shash_final(shash, (u8 *)&val);
  1590. if (err) {
  1591. printk(KERN_ERR "alg: crc32c: Operation failed for "
  1592. "%s: %d\n", driver, err);
  1593. break;
  1594. }
  1595. if (val != ~420553207) {
  1596. printk(KERN_ERR "alg: crc32c: Test failed for %s: "
  1597. "%d\n", driver, val);
  1598. err = -EINVAL;
  1599. }
  1600. } while (0);
  1601. crypto_free_shash(tfm);
  1602. out:
  1603. return err;
  1604. }
  1605. static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
  1606. u32 type, u32 mask)
  1607. {
  1608. struct crypto_rng *rng;
  1609. int err;
  1610. rng = crypto_alloc_rng(driver, type, mask);
  1611. if (IS_ERR(rng)) {
  1612. printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
  1613. "%ld\n", driver, PTR_ERR(rng));
  1614. return PTR_ERR(rng);
  1615. }
  1616. err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
  1617. crypto_free_rng(rng);
  1618. return err;
  1619. }
  1620. static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
  1621. const char *driver, u32 type, u32 mask)
  1622. {
  1623. int ret = -EAGAIN;
  1624. struct crypto_rng *drng;
  1625. struct drbg_test_data test_data;
  1626. struct drbg_string addtl, pers, testentropy;
  1627. unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
  1628. if (!buf)
  1629. return -ENOMEM;
  1630. drng = crypto_alloc_rng(driver, type, mask);
  1631. if (IS_ERR(drng)) {
  1632. printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
  1633. "%s\n", driver);
  1634. kzfree(buf);
  1635. return -ENOMEM;
  1636. }
  1637. test_data.testentropy = &testentropy;
  1638. drbg_string_fill(&testentropy, test->entropy, test->entropylen);
  1639. drbg_string_fill(&pers, test->pers, test->perslen);
  1640. ret = crypto_drbg_reset_test(drng, &pers, &test_data);
  1641. if (ret) {
  1642. printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
  1643. goto outbuf;
  1644. }
  1645. drbg_string_fill(&addtl, test->addtla, test->addtllen);
  1646. if (pr) {
  1647. drbg_string_fill(&testentropy, test->entpra, test->entprlen);
  1648. ret = crypto_drbg_get_bytes_addtl_test(drng,
  1649. buf, test->expectedlen, &addtl, &test_data);
  1650. } else {
  1651. ret = crypto_drbg_get_bytes_addtl(drng,
  1652. buf, test->expectedlen, &addtl);
  1653. }
  1654. if (ret < 0) {
  1655. printk(KERN_ERR "alg: drbg: could not obtain random data for "
  1656. "driver %s\n", driver);
  1657. goto outbuf;
  1658. }
  1659. drbg_string_fill(&addtl, test->addtlb, test->addtllen);
  1660. if (pr) {
  1661. drbg_string_fill(&testentropy, test->entprb, test->entprlen);
  1662. ret = crypto_drbg_get_bytes_addtl_test(drng,
  1663. buf, test->expectedlen, &addtl, &test_data);
  1664. } else {
  1665. ret = crypto_drbg_get_bytes_addtl(drng,
  1666. buf, test->expectedlen, &addtl);
  1667. }
  1668. if (ret < 0) {
  1669. printk(KERN_ERR "alg: drbg: could not obtain random data for "
  1670. "driver %s\n", driver);
  1671. goto outbuf;
  1672. }
  1673. ret = memcmp(test->expected, buf, test->expectedlen);
  1674. outbuf:
  1675. crypto_free_rng(drng);
  1676. kzfree(buf);
  1677. return ret;
  1678. }
  1679. static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
  1680. u32 type, u32 mask)
  1681. {
  1682. int err = 0;
  1683. int pr = 0;
  1684. int i = 0;
  1685. const struct drbg_testvec *template = desc->suite.drbg.vecs;
  1686. unsigned int tcount = desc->suite.drbg.count;
  1687. if (0 == memcmp(driver, "drbg_pr_", 8))
  1688. pr = 1;
  1689. for (i = 0; i < tcount; i++) {
  1690. err = drbg_cavs_test(&template[i], pr, driver, type, mask);
  1691. if (err) {
  1692. printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
  1693. i, driver);
  1694. err = -EINVAL;
  1695. break;
  1696. }
  1697. }
  1698. return err;
  1699. }
  1700. static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
  1701. const char *alg)
  1702. {
  1703. struct kpp_request *req;
  1704. void *input_buf = NULL;
  1705. void *output_buf = NULL;
  1706. struct tcrypt_result result;
  1707. unsigned int out_len_max;
  1708. int err = -ENOMEM;
  1709. struct scatterlist src, dst;
  1710. req = kpp_request_alloc(tfm, GFP_KERNEL);
  1711. if (!req)
  1712. return err;
  1713. init_completion(&result.completion);
  1714. err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
  1715. if (err < 0)
  1716. goto free_req;
  1717. out_len_max = crypto_kpp_maxsize(tfm);
  1718. output_buf = kzalloc(out_len_max, GFP_KERNEL);
  1719. if (!output_buf) {
  1720. err = -ENOMEM;
  1721. goto free_req;
  1722. }
  1723. /* Use appropriate parameter as base */
  1724. kpp_request_set_input(req, NULL, 0);
  1725. sg_init_one(&dst, output_buf, out_len_max);
  1726. kpp_request_set_output(req, &dst, out_len_max);
  1727. kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  1728. tcrypt_complete, &result);
  1729. /* Compute public key */
  1730. err = wait_async_op(&result, crypto_kpp_generate_public_key(req));
  1731. if (err) {
  1732. pr_err("alg: %s: generate public key test failed. err %d\n",
  1733. alg, err);
  1734. goto free_output;
  1735. }
  1736. /* Verify calculated public key */
  1737. if (memcmp(vec->expected_a_public, sg_virt(req->dst),
  1738. vec->expected_a_public_size)) {
  1739. pr_err("alg: %s: generate public key test failed. Invalid output\n",
  1740. alg);
  1741. err = -EINVAL;
  1742. goto free_output;
  1743. }
  1744. /* Calculate shared secret key by using counter part (b) public key. */
  1745. input_buf = kzalloc(vec->b_public_size, GFP_KERNEL);
  1746. if (!input_buf) {
  1747. err = -ENOMEM;
  1748. goto free_output;
  1749. }
  1750. memcpy(input_buf, vec->b_public, vec->b_public_size);
  1751. sg_init_one(&src, input_buf, vec->b_public_size);
  1752. sg_init_one(&dst, output_buf, out_len_max);
  1753. kpp_request_set_input(req, &src, vec->b_public_size);
  1754. kpp_request_set_output(req, &dst, out_len_max);
  1755. kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  1756. tcrypt_complete, &result);
  1757. err = wait_async_op(&result, crypto_kpp_compute_shared_secret(req));
  1758. if (err) {
  1759. pr_err("alg: %s: compute shard secret test failed. err %d\n",
  1760. alg, err);
  1761. goto free_all;
  1762. }
  1763. /*
  1764. * verify shared secret from which the user will derive
  1765. * secret key by executing whatever hash it has chosen
  1766. */
  1767. if (memcmp(vec->expected_ss, sg_virt(req->dst),
  1768. vec->expected_ss_size)) {
  1769. pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
  1770. alg);
  1771. err = -EINVAL;
  1772. }
  1773. free_all:
  1774. kfree(input_buf);
  1775. free_output:
  1776. kfree(output_buf);
  1777. free_req:
  1778. kpp_request_free(req);
  1779. return err;
  1780. }
  1781. static int test_kpp(struct crypto_kpp *tfm, const char *alg,
  1782. const struct kpp_testvec *vecs, unsigned int tcount)
  1783. {
  1784. int ret, i;
  1785. for (i = 0; i < tcount; i++) {
  1786. ret = do_test_kpp(tfm, vecs++, alg);
  1787. if (ret) {
  1788. pr_err("alg: %s: test failed on vector %d, err=%d\n",
  1789. alg, i + 1, ret);
  1790. return ret;
  1791. }
  1792. }
  1793. return 0;
  1794. }
  1795. static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
  1796. u32 type, u32 mask)
  1797. {
  1798. struct crypto_kpp *tfm;
  1799. int err = 0;
  1800. tfm = crypto_alloc_kpp(driver, type, mask);
  1801. if (IS_ERR(tfm)) {
  1802. pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
  1803. driver, PTR_ERR(tfm));
  1804. return PTR_ERR(tfm);
  1805. }
  1806. if (desc->suite.kpp.vecs)
  1807. err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
  1808. desc->suite.kpp.count);
  1809. crypto_free_kpp(tfm);
  1810. return err;
  1811. }
  1812. static int test_akcipher_one(struct crypto_akcipher *tfm,
  1813. const struct akcipher_testvec *vecs)
  1814. {
  1815. char *xbuf[XBUFSIZE];
  1816. struct akcipher_request *req;
  1817. void *outbuf_enc = NULL;
  1818. void *outbuf_dec = NULL;
  1819. struct tcrypt_result result;
  1820. unsigned int out_len_max, out_len = 0;
  1821. int err = -ENOMEM;
  1822. struct scatterlist src, dst, src_tab[2];
  1823. if (testmgr_alloc_buf(xbuf))
  1824. return err;
  1825. req = akcipher_request_alloc(tfm, GFP_KERNEL);
  1826. if (!req)
  1827. goto free_xbuf;
  1828. init_completion(&result.completion);
  1829. if (vecs->public_key_vec)
  1830. err = crypto_akcipher_set_pub_key(tfm, vecs->key,
  1831. vecs->key_len);
  1832. else
  1833. err = crypto_akcipher_set_priv_key(tfm, vecs->key,
  1834. vecs->key_len);
  1835. if (err)
  1836. goto free_req;
  1837. err = -ENOMEM;
  1838. out_len_max = crypto_akcipher_maxsize(tfm);
  1839. outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
  1840. if (!outbuf_enc)
  1841. goto free_req;
  1842. if (WARN_ON(vecs->m_size > PAGE_SIZE))
  1843. goto free_all;
  1844. memcpy(xbuf[0], vecs->m, vecs->m_size);
  1845. sg_init_table(src_tab, 2);
  1846. sg_set_buf(&src_tab[0], xbuf[0], 8);
  1847. sg_set_buf(&src_tab[1], xbuf[0] + 8, vecs->m_size - 8);
  1848. sg_init_one(&dst, outbuf_enc, out_len_max);
  1849. akcipher_request_set_crypt(req, src_tab, &dst, vecs->m_size,
  1850. out_len_max);
  1851. akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  1852. tcrypt_complete, &result);
  1853. /* Run RSA encrypt - c = m^e mod n;*/
  1854. err = wait_async_op(&result, crypto_akcipher_encrypt(req));
  1855. if (err) {
  1856. pr_err("alg: akcipher: encrypt test failed. err %d\n", err);
  1857. goto free_all;
  1858. }
  1859. if (req->dst_len != vecs->c_size) {
  1860. pr_err("alg: akcipher: encrypt test failed. Invalid output len\n");
  1861. err = -EINVAL;
  1862. goto free_all;
  1863. }
  1864. /* verify that encrypted message is equal to expected */
  1865. if (memcmp(vecs->c, outbuf_enc, vecs->c_size)) {
  1866. pr_err("alg: akcipher: encrypt test failed. Invalid output\n");
  1867. hexdump(outbuf_enc, vecs->c_size);
  1868. err = -EINVAL;
  1869. goto free_all;
  1870. }
  1871. /* Don't invoke decrypt for vectors with public key */
  1872. if (vecs->public_key_vec) {
  1873. err = 0;
  1874. goto free_all;
  1875. }
  1876. outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
  1877. if (!outbuf_dec) {
  1878. err = -ENOMEM;
  1879. goto free_all;
  1880. }
  1881. if (WARN_ON(vecs->c_size > PAGE_SIZE))
  1882. goto free_all;
  1883. memcpy(xbuf[0], vecs->c, vecs->c_size);
  1884. sg_init_one(&src, xbuf[0], vecs->c_size);
  1885. sg_init_one(&dst, outbuf_dec, out_len_max);
  1886. init_completion(&result.completion);
  1887. akcipher_request_set_crypt(req, &src, &dst, vecs->c_size, out_len_max);
  1888. /* Run RSA decrypt - m = c^d mod n;*/
  1889. err = wait_async_op(&result, crypto_akcipher_decrypt(req));
  1890. if (err) {
  1891. pr_err("alg: akcipher: decrypt test failed. err %d\n", err);
  1892. goto free_all;
  1893. }
  1894. out_len = req->dst_len;
  1895. if (out_len < vecs->m_size) {
  1896. pr_err("alg: akcipher: decrypt test failed. "
  1897. "Invalid output len %u\n", out_len);
  1898. err = -EINVAL;
  1899. goto free_all;
  1900. }
  1901. /* verify that decrypted message is equal to the original msg */
  1902. if (memchr_inv(outbuf_dec, 0, out_len - vecs->m_size) ||
  1903. memcmp(vecs->m, outbuf_dec + out_len - vecs->m_size,
  1904. vecs->m_size)) {
  1905. pr_err("alg: akcipher: decrypt test failed. Invalid output\n");
  1906. hexdump(outbuf_dec, out_len);
  1907. err = -EINVAL;
  1908. }
  1909. free_all:
  1910. kfree(outbuf_dec);
  1911. kfree(outbuf_enc);
  1912. free_req:
  1913. akcipher_request_free(req);
  1914. free_xbuf:
  1915. testmgr_free_buf(xbuf);
  1916. return err;
  1917. }
  1918. static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
  1919. const struct akcipher_testvec *vecs,
  1920. unsigned int tcount)
  1921. {
  1922. const char *algo =
  1923. crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
  1924. int ret, i;
  1925. for (i = 0; i < tcount; i++) {
  1926. ret = test_akcipher_one(tfm, vecs++);
  1927. if (!ret)
  1928. continue;
  1929. pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
  1930. i + 1, algo, ret);
  1931. return ret;
  1932. }
  1933. return 0;
  1934. }
  1935. static int alg_test_akcipher(const struct alg_test_desc *desc,
  1936. const char *driver, u32 type, u32 mask)
  1937. {
  1938. struct crypto_akcipher *tfm;
  1939. int err = 0;
  1940. tfm = crypto_alloc_akcipher(driver, type, mask);
  1941. if (IS_ERR(tfm)) {
  1942. pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
  1943. driver, PTR_ERR(tfm));
  1944. return PTR_ERR(tfm);
  1945. }
  1946. if (desc->suite.akcipher.vecs)
  1947. err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
  1948. desc->suite.akcipher.count);
  1949. crypto_free_akcipher(tfm);
  1950. return err;
  1951. }
  1952. static int alg_test_null(const struct alg_test_desc *desc,
  1953. const char *driver, u32 type, u32 mask)
  1954. {
  1955. return 0;
  1956. }
  1957. #define __VECS(tv) { .vecs = tv, .count = ARRAY_SIZE(tv) }
  1958. /* Please keep this list sorted by algorithm name. */
  1959. static const struct alg_test_desc alg_test_descs[] = {
  1960. {
  1961. .alg = "ansi_cprng",
  1962. .test = alg_test_cprng,
  1963. .suite = {
  1964. .cprng = __VECS(ansi_cprng_aes_tv_template)
  1965. }
  1966. }, {
  1967. .alg = "authenc(hmac(md5),ecb(cipher_null))",
  1968. .test = alg_test_aead,
  1969. .suite = {
  1970. .aead = {
  1971. .enc = __VECS(hmac_md5_ecb_cipher_null_enc_tv_template),
  1972. .dec = __VECS(hmac_md5_ecb_cipher_null_dec_tv_template)
  1973. }
  1974. }
  1975. }, {
  1976. .alg = "authenc(hmac(sha1),cbc(aes))",
  1977. .test = alg_test_aead,
  1978. .suite = {
  1979. .aead = {
  1980. .enc = __VECS(hmac_sha1_aes_cbc_enc_tv_temp)
  1981. }
  1982. }
  1983. }, {
  1984. .alg = "authenc(hmac(sha1),cbc(des))",
  1985. .test = alg_test_aead,
  1986. .suite = {
  1987. .aead = {
  1988. .enc = __VECS(hmac_sha1_des_cbc_enc_tv_temp)
  1989. }
  1990. }
  1991. }, {
  1992. .alg = "authenc(hmac(sha1),cbc(des3_ede))",
  1993. .test = alg_test_aead,
  1994. .fips_allowed = 1,
  1995. .suite = {
  1996. .aead = {
  1997. .enc = __VECS(hmac_sha1_des3_ede_cbc_enc_tv_temp)
  1998. }
  1999. }
  2000. }, {
  2001. .alg = "authenc(hmac(sha1),ctr(aes))",
  2002. .test = alg_test_null,
  2003. .fips_allowed = 1,
  2004. }, {
  2005. .alg = "authenc(hmac(sha1),ecb(cipher_null))",
  2006. .test = alg_test_aead,
  2007. .suite = {
  2008. .aead = {
  2009. .enc = __VECS(hmac_sha1_ecb_cipher_null_enc_tv_temp),
  2010. .dec = __VECS(hmac_sha1_ecb_cipher_null_dec_tv_temp)
  2011. }
  2012. }
  2013. }, {
  2014. .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
  2015. .test = alg_test_null,
  2016. .fips_allowed = 1,
  2017. }, {
  2018. .alg = "authenc(hmac(sha224),cbc(des))",
  2019. .test = alg_test_aead,
  2020. .suite = {
  2021. .aead = {
  2022. .enc = __VECS(hmac_sha224_des_cbc_enc_tv_temp)
  2023. }
  2024. }
  2025. }, {
  2026. .alg = "authenc(hmac(sha224),cbc(des3_ede))",
  2027. .test = alg_test_aead,
  2028. .fips_allowed = 1,
  2029. .suite = {
  2030. .aead = {
  2031. .enc = __VECS(hmac_sha224_des3_ede_cbc_enc_tv_temp)
  2032. }
  2033. }
  2034. }, {
  2035. .alg = "authenc(hmac(sha256),cbc(aes))",
  2036. .test = alg_test_aead,
  2037. .fips_allowed = 1,
  2038. .suite = {
  2039. .aead = {
  2040. .enc = __VECS(hmac_sha256_aes_cbc_enc_tv_temp)
  2041. }
  2042. }
  2043. }, {
  2044. .alg = "authenc(hmac(sha256),cbc(des))",
  2045. .test = alg_test_aead,
  2046. .suite = {
  2047. .aead = {
  2048. .enc = __VECS(hmac_sha256_des_cbc_enc_tv_temp)
  2049. }
  2050. }
  2051. }, {
  2052. .alg = "authenc(hmac(sha256),cbc(des3_ede))",
  2053. .test = alg_test_aead,
  2054. .fips_allowed = 1,
  2055. .suite = {
  2056. .aead = {
  2057. .enc = __VECS(hmac_sha256_des3_ede_cbc_enc_tv_temp)
  2058. }
  2059. }
  2060. }, {
  2061. .alg = "authenc(hmac(sha256),ctr(aes))",
  2062. .test = alg_test_null,
  2063. .fips_allowed = 1,
  2064. }, {
  2065. .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
  2066. .test = alg_test_null,
  2067. .fips_allowed = 1,
  2068. }, {
  2069. .alg = "authenc(hmac(sha384),cbc(des))",
  2070. .test = alg_test_aead,
  2071. .suite = {
  2072. .aead = {
  2073. .enc = __VECS(hmac_sha384_des_cbc_enc_tv_temp)
  2074. }
  2075. }
  2076. }, {
  2077. .alg = "authenc(hmac(sha384),cbc(des3_ede))",
  2078. .test = alg_test_aead,
  2079. .fips_allowed = 1,
  2080. .suite = {
  2081. .aead = {
  2082. .enc = __VECS(hmac_sha384_des3_ede_cbc_enc_tv_temp)
  2083. }
  2084. }
  2085. }, {
  2086. .alg = "authenc(hmac(sha384),ctr(aes))",
  2087. .test = alg_test_null,
  2088. .fips_allowed = 1,
  2089. }, {
  2090. .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
  2091. .test = alg_test_null,
  2092. .fips_allowed = 1,
  2093. }, {
  2094. .alg = "authenc(hmac(sha512),cbc(aes))",
  2095. .fips_allowed = 1,
  2096. .test = alg_test_aead,
  2097. .suite = {
  2098. .aead = {
  2099. .enc = __VECS(hmac_sha512_aes_cbc_enc_tv_temp)
  2100. }
  2101. }
  2102. }, {
  2103. .alg = "authenc(hmac(sha512),cbc(des))",
  2104. .test = alg_test_aead,
  2105. .suite = {
  2106. .aead = {
  2107. .enc = __VECS(hmac_sha512_des_cbc_enc_tv_temp)
  2108. }
  2109. }
  2110. }, {
  2111. .alg = "authenc(hmac(sha512),cbc(des3_ede))",
  2112. .test = alg_test_aead,
  2113. .fips_allowed = 1,
  2114. .suite = {
  2115. .aead = {
  2116. .enc = __VECS(hmac_sha512_des3_ede_cbc_enc_tv_temp)
  2117. }
  2118. }
  2119. }, {
  2120. .alg = "authenc(hmac(sha512),ctr(aes))",
  2121. .test = alg_test_null,
  2122. .fips_allowed = 1,
  2123. }, {
  2124. .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
  2125. .test = alg_test_null,
  2126. .fips_allowed = 1,
  2127. }, {
  2128. .alg = "cbc(aes)",
  2129. .test = alg_test_skcipher,
  2130. .fips_allowed = 1,
  2131. .suite = {
  2132. .cipher = {
  2133. .enc = __VECS(aes_cbc_enc_tv_template),
  2134. .dec = __VECS(aes_cbc_dec_tv_template)
  2135. }
  2136. }
  2137. }, {
  2138. .alg = "cbc(anubis)",
  2139. .test = alg_test_skcipher,
  2140. .suite = {
  2141. .cipher = {
  2142. .enc = __VECS(anubis_cbc_enc_tv_template),
  2143. .dec = __VECS(anubis_cbc_dec_tv_template)
  2144. }
  2145. }
  2146. }, {
  2147. .alg = "cbc(blowfish)",
  2148. .test = alg_test_skcipher,
  2149. .suite = {
  2150. .cipher = {
  2151. .enc = __VECS(bf_cbc_enc_tv_template),
  2152. .dec = __VECS(bf_cbc_dec_tv_template)
  2153. }
  2154. }
  2155. }, {
  2156. .alg = "cbc(camellia)",
  2157. .test = alg_test_skcipher,
  2158. .suite = {
  2159. .cipher = {
  2160. .enc = __VECS(camellia_cbc_enc_tv_template),
  2161. .dec = __VECS(camellia_cbc_dec_tv_template)
  2162. }
  2163. }
  2164. }, {
  2165. .alg = "cbc(cast5)",
  2166. .test = alg_test_skcipher,
  2167. .suite = {
  2168. .cipher = {
  2169. .enc = __VECS(cast5_cbc_enc_tv_template),
  2170. .dec = __VECS(cast5_cbc_dec_tv_template)
  2171. }
  2172. }
  2173. }, {
  2174. .alg = "cbc(cast6)",
  2175. .test = alg_test_skcipher,
  2176. .suite = {
  2177. .cipher = {
  2178. .enc = __VECS(cast6_cbc_enc_tv_template),
  2179. .dec = __VECS(cast6_cbc_dec_tv_template)
  2180. }
  2181. }
  2182. }, {
  2183. .alg = "cbc(des)",
  2184. .test = alg_test_skcipher,
  2185. .suite = {
  2186. .cipher = {
  2187. .enc = __VECS(des_cbc_enc_tv_template),
  2188. .dec = __VECS(des_cbc_dec_tv_template)
  2189. }
  2190. }
  2191. }, {
  2192. .alg = "cbc(des3_ede)",
  2193. .test = alg_test_skcipher,
  2194. .fips_allowed = 1,
  2195. .suite = {
  2196. .cipher = {
  2197. .enc = __VECS(des3_ede_cbc_enc_tv_template),
  2198. .dec = __VECS(des3_ede_cbc_dec_tv_template)
  2199. }
  2200. }
  2201. }, {
  2202. .alg = "cbc(serpent)",
  2203. .test = alg_test_skcipher,
  2204. .suite = {
  2205. .cipher = {
  2206. .enc = __VECS(serpent_cbc_enc_tv_template),
  2207. .dec = __VECS(serpent_cbc_dec_tv_template)
  2208. }
  2209. }
  2210. }, {
  2211. .alg = "cbc(twofish)",
  2212. .test = alg_test_skcipher,
  2213. .suite = {
  2214. .cipher = {
  2215. .enc = __VECS(tf_cbc_enc_tv_template),
  2216. .dec = __VECS(tf_cbc_dec_tv_template)
  2217. }
  2218. }
  2219. }, {
  2220. .alg = "cbcmac(aes)",
  2221. .fips_allowed = 1,
  2222. .test = alg_test_hash,
  2223. .suite = {
  2224. .hash = __VECS(aes_cbcmac_tv_template)
  2225. }
  2226. }, {
  2227. .alg = "ccm(aes)",
  2228. .test = alg_test_aead,
  2229. .fips_allowed = 1,
  2230. .suite = {
  2231. .aead = {
  2232. .enc = __VECS(aes_ccm_enc_tv_template),
  2233. .dec = __VECS(aes_ccm_dec_tv_template)
  2234. }
  2235. }
  2236. }, {
  2237. .alg = "chacha20",
  2238. .test = alg_test_skcipher,
  2239. .suite = {
  2240. .cipher = {
  2241. .enc = __VECS(chacha20_enc_tv_template),
  2242. .dec = __VECS(chacha20_enc_tv_template),
  2243. }
  2244. }
  2245. }, {
  2246. .alg = "cmac(aes)",
  2247. .fips_allowed = 1,
  2248. .test = alg_test_hash,
  2249. .suite = {
  2250. .hash = __VECS(aes_cmac128_tv_template)
  2251. }
  2252. }, {
  2253. .alg = "cmac(des3_ede)",
  2254. .fips_allowed = 1,
  2255. .test = alg_test_hash,
  2256. .suite = {
  2257. .hash = __VECS(des3_ede_cmac64_tv_template)
  2258. }
  2259. }, {
  2260. .alg = "compress_null",
  2261. .test = alg_test_null,
  2262. }, {
  2263. .alg = "crc32",
  2264. .test = alg_test_hash,
  2265. .suite = {
  2266. .hash = __VECS(crc32_tv_template)
  2267. }
  2268. }, {
  2269. .alg = "crc32c",
  2270. .test = alg_test_crc32c,
  2271. .fips_allowed = 1,
  2272. .suite = {
  2273. .hash = __VECS(crc32c_tv_template)
  2274. }
  2275. }, {
  2276. .alg = "crct10dif",
  2277. .test = alg_test_hash,
  2278. .fips_allowed = 1,
  2279. .suite = {
  2280. .hash = __VECS(crct10dif_tv_template)
  2281. }
  2282. }, {
  2283. .alg = "ctr(aes)",
  2284. .test = alg_test_skcipher,
  2285. .fips_allowed = 1,
  2286. .suite = {
  2287. .cipher = {
  2288. .enc = __VECS(aes_ctr_enc_tv_template),
  2289. .dec = __VECS(aes_ctr_dec_tv_template)
  2290. }
  2291. }
  2292. }, {
  2293. .alg = "ctr(blowfish)",
  2294. .test = alg_test_skcipher,
  2295. .suite = {
  2296. .cipher = {
  2297. .enc = __VECS(bf_ctr_enc_tv_template),
  2298. .dec = __VECS(bf_ctr_dec_tv_template)
  2299. }
  2300. }
  2301. }, {
  2302. .alg = "ctr(camellia)",
  2303. .test = alg_test_skcipher,
  2304. .suite = {
  2305. .cipher = {
  2306. .enc = __VECS(camellia_ctr_enc_tv_template),
  2307. .dec = __VECS(camellia_ctr_dec_tv_template)
  2308. }
  2309. }
  2310. }, {
  2311. .alg = "ctr(cast5)",
  2312. .test = alg_test_skcipher,
  2313. .suite = {
  2314. .cipher = {
  2315. .enc = __VECS(cast5_ctr_enc_tv_template),
  2316. .dec = __VECS(cast5_ctr_dec_tv_template)
  2317. }
  2318. }
  2319. }, {
  2320. .alg = "ctr(cast6)",
  2321. .test = alg_test_skcipher,
  2322. .suite = {
  2323. .cipher = {
  2324. .enc = __VECS(cast6_ctr_enc_tv_template),
  2325. .dec = __VECS(cast6_ctr_dec_tv_template)
  2326. }
  2327. }
  2328. }, {
  2329. .alg = "ctr(des)",
  2330. .test = alg_test_skcipher,
  2331. .suite = {
  2332. .cipher = {
  2333. .enc = __VECS(des_ctr_enc_tv_template),
  2334. .dec = __VECS(des_ctr_dec_tv_template)
  2335. }
  2336. }
  2337. }, {
  2338. .alg = "ctr(des3_ede)",
  2339. .test = alg_test_skcipher,
  2340. .fips_allowed = 1,
  2341. .suite = {
  2342. .cipher = {
  2343. .enc = __VECS(des3_ede_ctr_enc_tv_template),
  2344. .dec = __VECS(des3_ede_ctr_dec_tv_template)
  2345. }
  2346. }
  2347. }, {
  2348. .alg = "ctr(serpent)",
  2349. .test = alg_test_skcipher,
  2350. .suite = {
  2351. .cipher = {
  2352. .enc = __VECS(serpent_ctr_enc_tv_template),
  2353. .dec = __VECS(serpent_ctr_dec_tv_template)
  2354. }
  2355. }
  2356. }, {
  2357. .alg = "ctr(twofish)",
  2358. .test = alg_test_skcipher,
  2359. .suite = {
  2360. .cipher = {
  2361. .enc = __VECS(tf_ctr_enc_tv_template),
  2362. .dec = __VECS(tf_ctr_dec_tv_template)
  2363. }
  2364. }
  2365. }, {
  2366. .alg = "cts(cbc(aes))",
  2367. .test = alg_test_skcipher,
  2368. .suite = {
  2369. .cipher = {
  2370. .enc = __VECS(cts_mode_enc_tv_template),
  2371. .dec = __VECS(cts_mode_dec_tv_template)
  2372. }
  2373. }
  2374. }, {
  2375. .alg = "deflate",
  2376. .test = alg_test_comp,
  2377. .fips_allowed = 1,
  2378. .suite = {
  2379. .comp = {
  2380. .comp = __VECS(deflate_comp_tv_template),
  2381. .decomp = __VECS(deflate_decomp_tv_template)
  2382. }
  2383. }
  2384. }, {
  2385. .alg = "dh",
  2386. .test = alg_test_kpp,
  2387. .fips_allowed = 1,
  2388. .suite = {
  2389. .kpp = __VECS(dh_tv_template)
  2390. }
  2391. }, {
  2392. .alg = "digest_null",
  2393. .test = alg_test_null,
  2394. }, {
  2395. .alg = "drbg_nopr_ctr_aes128",
  2396. .test = alg_test_drbg,
  2397. .fips_allowed = 1,
  2398. .suite = {
  2399. .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
  2400. }
  2401. }, {
  2402. .alg = "drbg_nopr_ctr_aes192",
  2403. .test = alg_test_drbg,
  2404. .fips_allowed = 1,
  2405. .suite = {
  2406. .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
  2407. }
  2408. }, {
  2409. .alg = "drbg_nopr_ctr_aes256",
  2410. .test = alg_test_drbg,
  2411. .fips_allowed = 1,
  2412. .suite = {
  2413. .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
  2414. }
  2415. }, {
  2416. /*
  2417. * There is no need to specifically test the DRBG with every
  2418. * backend cipher -- covered by drbg_nopr_hmac_sha256 test
  2419. */
  2420. .alg = "drbg_nopr_hmac_sha1",
  2421. .fips_allowed = 1,
  2422. .test = alg_test_null,
  2423. }, {
  2424. .alg = "drbg_nopr_hmac_sha256",
  2425. .test = alg_test_drbg,
  2426. .fips_allowed = 1,
  2427. .suite = {
  2428. .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
  2429. }
  2430. }, {
  2431. /* covered by drbg_nopr_hmac_sha256 test */
  2432. .alg = "drbg_nopr_hmac_sha384",
  2433. .fips_allowed = 1,
  2434. .test = alg_test_null,
  2435. }, {
  2436. .alg = "drbg_nopr_hmac_sha512",
  2437. .test = alg_test_null,
  2438. .fips_allowed = 1,
  2439. }, {
  2440. .alg = "drbg_nopr_sha1",
  2441. .fips_allowed = 1,
  2442. .test = alg_test_null,
  2443. }, {
  2444. .alg = "drbg_nopr_sha256",
  2445. .test = alg_test_drbg,
  2446. .fips_allowed = 1,
  2447. .suite = {
  2448. .drbg = __VECS(drbg_nopr_sha256_tv_template)
  2449. }
  2450. }, {
  2451. /* covered by drbg_nopr_sha256 test */
  2452. .alg = "drbg_nopr_sha384",
  2453. .fips_allowed = 1,
  2454. .test = alg_test_null,
  2455. }, {
  2456. .alg = "drbg_nopr_sha512",
  2457. .fips_allowed = 1,
  2458. .test = alg_test_null,
  2459. }, {
  2460. .alg = "drbg_pr_ctr_aes128",
  2461. .test = alg_test_drbg,
  2462. .fips_allowed = 1,
  2463. .suite = {
  2464. .drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
  2465. }
  2466. }, {
  2467. /* covered by drbg_pr_ctr_aes128 test */
  2468. .alg = "drbg_pr_ctr_aes192",
  2469. .fips_allowed = 1,
  2470. .test = alg_test_null,
  2471. }, {
  2472. .alg = "drbg_pr_ctr_aes256",
  2473. .fips_allowed = 1,
  2474. .test = alg_test_null,
  2475. }, {
  2476. .alg = "drbg_pr_hmac_sha1",
  2477. .fips_allowed = 1,
  2478. .test = alg_test_null,
  2479. }, {
  2480. .alg = "drbg_pr_hmac_sha256",
  2481. .test = alg_test_drbg,
  2482. .fips_allowed = 1,
  2483. .suite = {
  2484. .drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
  2485. }
  2486. }, {
  2487. /* covered by drbg_pr_hmac_sha256 test */
  2488. .alg = "drbg_pr_hmac_sha384",
  2489. .fips_allowed = 1,
  2490. .test = alg_test_null,
  2491. }, {
  2492. .alg = "drbg_pr_hmac_sha512",
  2493. .test = alg_test_null,
  2494. .fips_allowed = 1,
  2495. }, {
  2496. .alg = "drbg_pr_sha1",
  2497. .fips_allowed = 1,
  2498. .test = alg_test_null,
  2499. }, {
  2500. .alg = "drbg_pr_sha256",
  2501. .test = alg_test_drbg,
  2502. .fips_allowed = 1,
  2503. .suite = {
  2504. .drbg = __VECS(drbg_pr_sha256_tv_template)
  2505. }
  2506. }, {
  2507. /* covered by drbg_pr_sha256 test */
  2508. .alg = "drbg_pr_sha384",
  2509. .fips_allowed = 1,
  2510. .test = alg_test_null,
  2511. }, {
  2512. .alg = "drbg_pr_sha512",
  2513. .fips_allowed = 1,
  2514. .test = alg_test_null,
  2515. }, {
  2516. .alg = "ecb(aes)",
  2517. .test = alg_test_skcipher,
  2518. .fips_allowed = 1,
  2519. .suite = {
  2520. .cipher = {
  2521. .enc = __VECS(aes_enc_tv_template),
  2522. .dec = __VECS(aes_dec_tv_template)
  2523. }
  2524. }
  2525. }, {
  2526. .alg = "ecb(anubis)",
  2527. .test = alg_test_skcipher,
  2528. .suite = {
  2529. .cipher = {
  2530. .enc = __VECS(anubis_enc_tv_template),
  2531. .dec = __VECS(anubis_dec_tv_template)
  2532. }
  2533. }
  2534. }, {
  2535. .alg = "ecb(arc4)",
  2536. .test = alg_test_skcipher,
  2537. .suite = {
  2538. .cipher = {
  2539. .enc = __VECS(arc4_enc_tv_template),
  2540. .dec = __VECS(arc4_dec_tv_template)
  2541. }
  2542. }
  2543. }, {
  2544. .alg = "ecb(blowfish)",
  2545. .test = alg_test_skcipher,
  2546. .suite = {
  2547. .cipher = {
  2548. .enc = __VECS(bf_enc_tv_template),
  2549. .dec = __VECS(bf_dec_tv_template)
  2550. }
  2551. }
  2552. }, {
  2553. .alg = "ecb(camellia)",
  2554. .test = alg_test_skcipher,
  2555. .suite = {
  2556. .cipher = {
  2557. .enc = __VECS(camellia_enc_tv_template),
  2558. .dec = __VECS(camellia_dec_tv_template)
  2559. }
  2560. }
  2561. }, {
  2562. .alg = "ecb(cast5)",
  2563. .test = alg_test_skcipher,
  2564. .suite = {
  2565. .cipher = {
  2566. .enc = __VECS(cast5_enc_tv_template),
  2567. .dec = __VECS(cast5_dec_tv_template)
  2568. }
  2569. }
  2570. }, {
  2571. .alg = "ecb(cast6)",
  2572. .test = alg_test_skcipher,
  2573. .suite = {
  2574. .cipher = {
  2575. .enc = __VECS(cast6_enc_tv_template),
  2576. .dec = __VECS(cast6_dec_tv_template)
  2577. }
  2578. }
  2579. }, {
  2580. .alg = "ecb(cipher_null)",
  2581. .test = alg_test_null,
  2582. .fips_allowed = 1,
  2583. }, {
  2584. .alg = "ecb(des)",
  2585. .test = alg_test_skcipher,
  2586. .suite = {
  2587. .cipher = {
  2588. .enc = __VECS(des_enc_tv_template),
  2589. .dec = __VECS(des_dec_tv_template)
  2590. }
  2591. }
  2592. }, {
  2593. .alg = "ecb(des3_ede)",
  2594. .test = alg_test_skcipher,
  2595. .fips_allowed = 1,
  2596. .suite = {
  2597. .cipher = {
  2598. .enc = __VECS(des3_ede_enc_tv_template),
  2599. .dec = __VECS(des3_ede_dec_tv_template)
  2600. }
  2601. }
  2602. }, {
  2603. .alg = "ecb(fcrypt)",
  2604. .test = alg_test_skcipher,
  2605. .suite = {
  2606. .cipher = {
  2607. .enc = {
  2608. .vecs = fcrypt_pcbc_enc_tv_template,
  2609. .count = 1
  2610. },
  2611. .dec = {
  2612. .vecs = fcrypt_pcbc_dec_tv_template,
  2613. .count = 1
  2614. }
  2615. }
  2616. }
  2617. }, {
  2618. .alg = "ecb(khazad)",
  2619. .test = alg_test_skcipher,
  2620. .suite = {
  2621. .cipher = {
  2622. .enc = __VECS(khazad_enc_tv_template),
  2623. .dec = __VECS(khazad_dec_tv_template)
  2624. }
  2625. }
  2626. }, {
  2627. .alg = "ecb(seed)",
  2628. .test = alg_test_skcipher,
  2629. .suite = {
  2630. .cipher = {
  2631. .enc = __VECS(seed_enc_tv_template),
  2632. .dec = __VECS(seed_dec_tv_template)
  2633. }
  2634. }
  2635. }, {
  2636. .alg = "ecb(serpent)",
  2637. .test = alg_test_skcipher,
  2638. .suite = {
  2639. .cipher = {
  2640. .enc = __VECS(serpent_enc_tv_template),
  2641. .dec = __VECS(serpent_dec_tv_template)
  2642. }
  2643. }
  2644. }, {
  2645. .alg = "ecb(tea)",
  2646. .test = alg_test_skcipher,
  2647. .suite = {
  2648. .cipher = {
  2649. .enc = __VECS(tea_enc_tv_template),
  2650. .dec = __VECS(tea_dec_tv_template)
  2651. }
  2652. }
  2653. }, {
  2654. .alg = "ecb(tnepres)",
  2655. .test = alg_test_skcipher,
  2656. .suite = {
  2657. .cipher = {
  2658. .enc = __VECS(tnepres_enc_tv_template),
  2659. .dec = __VECS(tnepres_dec_tv_template)
  2660. }
  2661. }
  2662. }, {
  2663. .alg = "ecb(twofish)",
  2664. .test = alg_test_skcipher,
  2665. .suite = {
  2666. .cipher = {
  2667. .enc = __VECS(tf_enc_tv_template),
  2668. .dec = __VECS(tf_dec_tv_template)
  2669. }
  2670. }
  2671. }, {
  2672. .alg = "ecb(xeta)",
  2673. .test = alg_test_skcipher,
  2674. .suite = {
  2675. .cipher = {
  2676. .enc = __VECS(xeta_enc_tv_template),
  2677. .dec = __VECS(xeta_dec_tv_template)
  2678. }
  2679. }
  2680. }, {
  2681. .alg = "ecb(xtea)",
  2682. .test = alg_test_skcipher,
  2683. .suite = {
  2684. .cipher = {
  2685. .enc = __VECS(xtea_enc_tv_template),
  2686. .dec = __VECS(xtea_dec_tv_template)
  2687. }
  2688. }
  2689. }, {
  2690. .alg = "ecdh",
  2691. .test = alg_test_kpp,
  2692. .fips_allowed = 1,
  2693. .suite = {
  2694. .kpp = __VECS(ecdh_tv_template)
  2695. }
  2696. }, {
  2697. .alg = "gcm(aes)",
  2698. .test = alg_test_aead,
  2699. .fips_allowed = 1,
  2700. .suite = {
  2701. .aead = {
  2702. .enc = __VECS(aes_gcm_enc_tv_template),
  2703. .dec = __VECS(aes_gcm_dec_tv_template)
  2704. }
  2705. }
  2706. }, {
  2707. .alg = "ghash",
  2708. .test = alg_test_hash,
  2709. .fips_allowed = 1,
  2710. .suite = {
  2711. .hash = __VECS(ghash_tv_template)
  2712. }
  2713. }, {
  2714. .alg = "hmac(crc32)",
  2715. .test = alg_test_hash,
  2716. .suite = {
  2717. .hash = __VECS(bfin_crc_tv_template)
  2718. }
  2719. }, {
  2720. .alg = "hmac(md5)",
  2721. .test = alg_test_hash,
  2722. .suite = {
  2723. .hash = __VECS(hmac_md5_tv_template)
  2724. }
  2725. }, {
  2726. .alg = "hmac(rmd128)",
  2727. .test = alg_test_hash,
  2728. .suite = {
  2729. .hash = __VECS(hmac_rmd128_tv_template)
  2730. }
  2731. }, {
  2732. .alg = "hmac(rmd160)",
  2733. .test = alg_test_hash,
  2734. .suite = {
  2735. .hash = __VECS(hmac_rmd160_tv_template)
  2736. }
  2737. }, {
  2738. .alg = "hmac(sha1)",
  2739. .test = alg_test_hash,
  2740. .fips_allowed = 1,
  2741. .suite = {
  2742. .hash = __VECS(hmac_sha1_tv_template)
  2743. }
  2744. }, {
  2745. .alg = "hmac(sha224)",
  2746. .test = alg_test_hash,
  2747. .fips_allowed = 1,
  2748. .suite = {
  2749. .hash = __VECS(hmac_sha224_tv_template)
  2750. }
  2751. }, {
  2752. .alg = "hmac(sha256)",
  2753. .test = alg_test_hash,
  2754. .fips_allowed = 1,
  2755. .suite = {
  2756. .hash = __VECS(hmac_sha256_tv_template)
  2757. }
  2758. }, {
  2759. .alg = "hmac(sha3-224)",
  2760. .test = alg_test_hash,
  2761. .fips_allowed = 1,
  2762. .suite = {
  2763. .hash = __VECS(hmac_sha3_224_tv_template)
  2764. }
  2765. }, {
  2766. .alg = "hmac(sha3-256)",
  2767. .test = alg_test_hash,
  2768. .fips_allowed = 1,
  2769. .suite = {
  2770. .hash = __VECS(hmac_sha3_256_tv_template)
  2771. }
  2772. }, {
  2773. .alg = "hmac(sha3-384)",
  2774. .test = alg_test_hash,
  2775. .fips_allowed = 1,
  2776. .suite = {
  2777. .hash = __VECS(hmac_sha3_384_tv_template)
  2778. }
  2779. }, {
  2780. .alg = "hmac(sha3-512)",
  2781. .test = alg_test_hash,
  2782. .fips_allowed = 1,
  2783. .suite = {
  2784. .hash = __VECS(hmac_sha3_512_tv_template)
  2785. }
  2786. }, {
  2787. .alg = "hmac(sha384)",
  2788. .test = alg_test_hash,
  2789. .fips_allowed = 1,
  2790. .suite = {
  2791. .hash = __VECS(hmac_sha384_tv_template)
  2792. }
  2793. }, {
  2794. .alg = "hmac(sha512)",
  2795. .test = alg_test_hash,
  2796. .fips_allowed = 1,
  2797. .suite = {
  2798. .hash = __VECS(hmac_sha512_tv_template)
  2799. }
  2800. }, {
  2801. .alg = "jitterentropy_rng",
  2802. .fips_allowed = 1,
  2803. .test = alg_test_null,
  2804. }, {
  2805. .alg = "kw(aes)",
  2806. .test = alg_test_skcipher,
  2807. .fips_allowed = 1,
  2808. .suite = {
  2809. .cipher = {
  2810. .enc = __VECS(aes_kw_enc_tv_template),
  2811. .dec = __VECS(aes_kw_dec_tv_template)
  2812. }
  2813. }
  2814. }, {
  2815. .alg = "lrw(aes)",
  2816. .test = alg_test_skcipher,
  2817. .suite = {
  2818. .cipher = {
  2819. .enc = __VECS(aes_lrw_enc_tv_template),
  2820. .dec = __VECS(aes_lrw_dec_tv_template)
  2821. }
  2822. }
  2823. }, {
  2824. .alg = "lrw(camellia)",
  2825. .test = alg_test_skcipher,
  2826. .suite = {
  2827. .cipher = {
  2828. .enc = __VECS(camellia_lrw_enc_tv_template),
  2829. .dec = __VECS(camellia_lrw_dec_tv_template)
  2830. }
  2831. }
  2832. }, {
  2833. .alg = "lrw(cast6)",
  2834. .test = alg_test_skcipher,
  2835. .suite = {
  2836. .cipher = {
  2837. .enc = __VECS(cast6_lrw_enc_tv_template),
  2838. .dec = __VECS(cast6_lrw_dec_tv_template)
  2839. }
  2840. }
  2841. }, {
  2842. .alg = "lrw(serpent)",
  2843. .test = alg_test_skcipher,
  2844. .suite = {
  2845. .cipher = {
  2846. .enc = __VECS(serpent_lrw_enc_tv_template),
  2847. .dec = __VECS(serpent_lrw_dec_tv_template)
  2848. }
  2849. }
  2850. }, {
  2851. .alg = "lrw(twofish)",
  2852. .test = alg_test_skcipher,
  2853. .suite = {
  2854. .cipher = {
  2855. .enc = __VECS(tf_lrw_enc_tv_template),
  2856. .dec = __VECS(tf_lrw_dec_tv_template)
  2857. }
  2858. }
  2859. }, {
  2860. .alg = "lz4",
  2861. .test = alg_test_comp,
  2862. .fips_allowed = 1,
  2863. .suite = {
  2864. .comp = {
  2865. .comp = __VECS(lz4_comp_tv_template),
  2866. .decomp = __VECS(lz4_decomp_tv_template)
  2867. }
  2868. }
  2869. }, {
  2870. .alg = "lz4hc",
  2871. .test = alg_test_comp,
  2872. .fips_allowed = 1,
  2873. .suite = {
  2874. .comp = {
  2875. .comp = __VECS(lz4hc_comp_tv_template),
  2876. .decomp = __VECS(lz4hc_decomp_tv_template)
  2877. }
  2878. }
  2879. }, {
  2880. .alg = "lzo",
  2881. .test = alg_test_comp,
  2882. .fips_allowed = 1,
  2883. .suite = {
  2884. .comp = {
  2885. .comp = __VECS(lzo_comp_tv_template),
  2886. .decomp = __VECS(lzo_decomp_tv_template)
  2887. }
  2888. }
  2889. }, {
  2890. .alg = "md4",
  2891. .test = alg_test_hash,
  2892. .suite = {
  2893. .hash = __VECS(md4_tv_template)
  2894. }
  2895. }, {
  2896. .alg = "md5",
  2897. .test = alg_test_hash,
  2898. .suite = {
  2899. .hash = __VECS(md5_tv_template)
  2900. }
  2901. }, {
  2902. .alg = "michael_mic",
  2903. .test = alg_test_hash,
  2904. .suite = {
  2905. .hash = __VECS(michael_mic_tv_template)
  2906. }
  2907. }, {
  2908. .alg = "ofb(aes)",
  2909. .test = alg_test_skcipher,
  2910. .fips_allowed = 1,
  2911. .suite = {
  2912. .cipher = {
  2913. .enc = __VECS(aes_ofb_enc_tv_template),
  2914. .dec = __VECS(aes_ofb_dec_tv_template)
  2915. }
  2916. }
  2917. }, {
  2918. .alg = "pcbc(fcrypt)",
  2919. .test = alg_test_skcipher,
  2920. .suite = {
  2921. .cipher = {
  2922. .enc = __VECS(fcrypt_pcbc_enc_tv_template),
  2923. .dec = __VECS(fcrypt_pcbc_dec_tv_template)
  2924. }
  2925. }
  2926. }, {
  2927. .alg = "poly1305",
  2928. .test = alg_test_hash,
  2929. .suite = {
  2930. .hash = __VECS(poly1305_tv_template)
  2931. }
  2932. }, {
  2933. .alg = "rfc3686(ctr(aes))",
  2934. .test = alg_test_skcipher,
  2935. .fips_allowed = 1,
  2936. .suite = {
  2937. .cipher = {
  2938. .enc = __VECS(aes_ctr_rfc3686_enc_tv_template),
  2939. .dec = __VECS(aes_ctr_rfc3686_dec_tv_template)
  2940. }
  2941. }
  2942. }, {
  2943. .alg = "rfc4106(gcm(aes))",
  2944. .test = alg_test_aead,
  2945. .fips_allowed = 1,
  2946. .suite = {
  2947. .aead = {
  2948. .enc = __VECS(aes_gcm_rfc4106_enc_tv_template),
  2949. .dec = __VECS(aes_gcm_rfc4106_dec_tv_template)
  2950. }
  2951. }
  2952. }, {
  2953. .alg = "rfc4309(ccm(aes))",
  2954. .test = alg_test_aead,
  2955. .fips_allowed = 1,
  2956. .suite = {
  2957. .aead = {
  2958. .enc = __VECS(aes_ccm_rfc4309_enc_tv_template),
  2959. .dec = __VECS(aes_ccm_rfc4309_dec_tv_template)
  2960. }
  2961. }
  2962. }, {
  2963. .alg = "rfc4543(gcm(aes))",
  2964. .test = alg_test_aead,
  2965. .suite = {
  2966. .aead = {
  2967. .enc = __VECS(aes_gcm_rfc4543_enc_tv_template),
  2968. .dec = __VECS(aes_gcm_rfc4543_dec_tv_template),
  2969. }
  2970. }
  2971. }, {
  2972. .alg = "rfc7539(chacha20,poly1305)",
  2973. .test = alg_test_aead,
  2974. .suite = {
  2975. .aead = {
  2976. .enc = __VECS(rfc7539_enc_tv_template),
  2977. .dec = __VECS(rfc7539_dec_tv_template),
  2978. }
  2979. }
  2980. }, {
  2981. .alg = "rfc7539esp(chacha20,poly1305)",
  2982. .test = alg_test_aead,
  2983. .suite = {
  2984. .aead = {
  2985. .enc = __VECS(rfc7539esp_enc_tv_template),
  2986. .dec = __VECS(rfc7539esp_dec_tv_template),
  2987. }
  2988. }
  2989. }, {
  2990. .alg = "rmd128",
  2991. .test = alg_test_hash,
  2992. .suite = {
  2993. .hash = __VECS(rmd128_tv_template)
  2994. }
  2995. }, {
  2996. .alg = "rmd160",
  2997. .test = alg_test_hash,
  2998. .suite = {
  2999. .hash = __VECS(rmd160_tv_template)
  3000. }
  3001. }, {
  3002. .alg = "rmd256",
  3003. .test = alg_test_hash,
  3004. .suite = {
  3005. .hash = __VECS(rmd256_tv_template)
  3006. }
  3007. }, {
  3008. .alg = "rmd320",
  3009. .test = alg_test_hash,
  3010. .suite = {
  3011. .hash = __VECS(rmd320_tv_template)
  3012. }
  3013. }, {
  3014. .alg = "rsa",
  3015. .test = alg_test_akcipher,
  3016. .fips_allowed = 1,
  3017. .suite = {
  3018. .akcipher = __VECS(rsa_tv_template)
  3019. }
  3020. }, {
  3021. .alg = "salsa20",
  3022. .test = alg_test_skcipher,
  3023. .suite = {
  3024. .cipher = {
  3025. .enc = __VECS(salsa20_stream_enc_tv_template)
  3026. }
  3027. }
  3028. }, {
  3029. .alg = "sha1",
  3030. .test = alg_test_hash,
  3031. .fips_allowed = 1,
  3032. .suite = {
  3033. .hash = __VECS(sha1_tv_template)
  3034. }
  3035. }, {
  3036. .alg = "sha224",
  3037. .test = alg_test_hash,
  3038. .fips_allowed = 1,
  3039. .suite = {
  3040. .hash = __VECS(sha224_tv_template)
  3041. }
  3042. }, {
  3043. .alg = "sha256",
  3044. .test = alg_test_hash,
  3045. .fips_allowed = 1,
  3046. .suite = {
  3047. .hash = __VECS(sha256_tv_template)
  3048. }
  3049. }, {
  3050. .alg = "sha3-224",
  3051. .test = alg_test_hash,
  3052. .fips_allowed = 1,
  3053. .suite = {
  3054. .hash = __VECS(sha3_224_tv_template)
  3055. }
  3056. }, {
  3057. .alg = "sha3-256",
  3058. .test = alg_test_hash,
  3059. .fips_allowed = 1,
  3060. .suite = {
  3061. .hash = __VECS(sha3_256_tv_template)
  3062. }
  3063. }, {
  3064. .alg = "sha3-384",
  3065. .test = alg_test_hash,
  3066. .fips_allowed = 1,
  3067. .suite = {
  3068. .hash = __VECS(sha3_384_tv_template)
  3069. }
  3070. }, {
  3071. .alg = "sha3-512",
  3072. .test = alg_test_hash,
  3073. .fips_allowed = 1,
  3074. .suite = {
  3075. .hash = __VECS(sha3_512_tv_template)
  3076. }
  3077. }, {
  3078. .alg = "sha384",
  3079. .test = alg_test_hash,
  3080. .fips_allowed = 1,
  3081. .suite = {
  3082. .hash = __VECS(sha384_tv_template)
  3083. }
  3084. }, {
  3085. .alg = "sha512",
  3086. .test = alg_test_hash,
  3087. .fips_allowed = 1,
  3088. .suite = {
  3089. .hash = __VECS(sha512_tv_template)
  3090. }
  3091. }, {
  3092. .alg = "tgr128",
  3093. .test = alg_test_hash,
  3094. .suite = {
  3095. .hash = __VECS(tgr128_tv_template)
  3096. }
  3097. }, {
  3098. .alg = "tgr160",
  3099. .test = alg_test_hash,
  3100. .suite = {
  3101. .hash = __VECS(tgr160_tv_template)
  3102. }
  3103. }, {
  3104. .alg = "tgr192",
  3105. .test = alg_test_hash,
  3106. .suite = {
  3107. .hash = __VECS(tgr192_tv_template)
  3108. }
  3109. }, {
  3110. .alg = "vmac(aes)",
  3111. .test = alg_test_hash,
  3112. .suite = {
  3113. .hash = __VECS(aes_vmac128_tv_template)
  3114. }
  3115. }, {
  3116. .alg = "wp256",
  3117. .test = alg_test_hash,
  3118. .suite = {
  3119. .hash = __VECS(wp256_tv_template)
  3120. }
  3121. }, {
  3122. .alg = "wp384",
  3123. .test = alg_test_hash,
  3124. .suite = {
  3125. .hash = __VECS(wp384_tv_template)
  3126. }
  3127. }, {
  3128. .alg = "wp512",
  3129. .test = alg_test_hash,
  3130. .suite = {
  3131. .hash = __VECS(wp512_tv_template)
  3132. }
  3133. }, {
  3134. .alg = "xcbc(aes)",
  3135. .test = alg_test_hash,
  3136. .suite = {
  3137. .hash = __VECS(aes_xcbc128_tv_template)
  3138. }
  3139. }, {
  3140. .alg = "xts(aes)",
  3141. .test = alg_test_skcipher,
  3142. .fips_allowed = 1,
  3143. .suite = {
  3144. .cipher = {
  3145. .enc = __VECS(aes_xts_enc_tv_template),
  3146. .dec = __VECS(aes_xts_dec_tv_template)
  3147. }
  3148. }
  3149. }, {
  3150. .alg = "xts(camellia)",
  3151. .test = alg_test_skcipher,
  3152. .suite = {
  3153. .cipher = {
  3154. .enc = __VECS(camellia_xts_enc_tv_template),
  3155. .dec = __VECS(camellia_xts_dec_tv_template)
  3156. }
  3157. }
  3158. }, {
  3159. .alg = "xts(cast6)",
  3160. .test = alg_test_skcipher,
  3161. .suite = {
  3162. .cipher = {
  3163. .enc = __VECS(cast6_xts_enc_tv_template),
  3164. .dec = __VECS(cast6_xts_dec_tv_template)
  3165. }
  3166. }
  3167. }, {
  3168. .alg = "xts(serpent)",
  3169. .test = alg_test_skcipher,
  3170. .suite = {
  3171. .cipher = {
  3172. .enc = __VECS(serpent_xts_enc_tv_template),
  3173. .dec = __VECS(serpent_xts_dec_tv_template)
  3174. }
  3175. }
  3176. }, {
  3177. .alg = "xts(twofish)",
  3178. .test = alg_test_skcipher,
  3179. .suite = {
  3180. .cipher = {
  3181. .enc = __VECS(tf_xts_enc_tv_template),
  3182. .dec = __VECS(tf_xts_dec_tv_template)
  3183. }
  3184. }
  3185. }, {
  3186. .alg = "zlib-deflate",
  3187. .test = alg_test_comp,
  3188. .fips_allowed = 1,
  3189. .suite = {
  3190. .comp = {
  3191. .comp = __VECS(zlib_deflate_comp_tv_template),
  3192. .decomp = __VECS(zlib_deflate_decomp_tv_template)
  3193. }
  3194. }
  3195. }
  3196. };
  3197. static bool alg_test_descs_checked;
  3198. static void alg_test_descs_check_order(void)
  3199. {
  3200. int i;
  3201. /* only check once */
  3202. if (alg_test_descs_checked)
  3203. return;
  3204. alg_test_descs_checked = true;
  3205. for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
  3206. int diff = strcmp(alg_test_descs[i - 1].alg,
  3207. alg_test_descs[i].alg);
  3208. if (WARN_ON(diff > 0)) {
  3209. pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
  3210. alg_test_descs[i - 1].alg,
  3211. alg_test_descs[i].alg);
  3212. }
  3213. if (WARN_ON(diff == 0)) {
  3214. pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
  3215. alg_test_descs[i].alg);
  3216. }
  3217. }
  3218. }
  3219. static int alg_find_test(const char *alg)
  3220. {
  3221. int start = 0;
  3222. int end = ARRAY_SIZE(alg_test_descs);
  3223. while (start < end) {
  3224. int i = (start + end) / 2;
  3225. int diff = strcmp(alg_test_descs[i].alg, alg);
  3226. if (diff > 0) {
  3227. end = i;
  3228. continue;
  3229. }
  3230. if (diff < 0) {
  3231. start = i + 1;
  3232. continue;
  3233. }
  3234. return i;
  3235. }
  3236. return -1;
  3237. }
  3238. int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
  3239. {
  3240. int i;
  3241. int j;
  3242. int rc;
  3243. if (!fips_enabled && notests) {
  3244. printk_once(KERN_INFO "alg: self-tests disabled\n");
  3245. return 0;
  3246. }
  3247. alg_test_descs_check_order();
  3248. if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
  3249. char nalg[CRYPTO_MAX_ALG_NAME];
  3250. if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
  3251. sizeof(nalg))
  3252. return -ENAMETOOLONG;
  3253. i = alg_find_test(nalg);
  3254. if (i < 0)
  3255. goto notest;
  3256. if (fips_enabled && !alg_test_descs[i].fips_allowed)
  3257. goto non_fips_alg;
  3258. rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
  3259. goto test_done;
  3260. }
  3261. i = alg_find_test(alg);
  3262. j = alg_find_test(driver);
  3263. if (i < 0 && j < 0)
  3264. goto notest;
  3265. if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
  3266. (j >= 0 && !alg_test_descs[j].fips_allowed)))
  3267. goto non_fips_alg;
  3268. rc = 0;
  3269. if (i >= 0)
  3270. rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
  3271. type, mask);
  3272. if (j >= 0 && j != i)
  3273. rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
  3274. type, mask);
  3275. test_done:
  3276. if (fips_enabled && rc)
  3277. panic("%s: %s alg self test failed in fips mode!\n", driver, alg);
  3278. if (fips_enabled && !rc)
  3279. pr_info("alg: self-tests for %s (%s) passed\n", driver, alg);
  3280. return rc;
  3281. notest:
  3282. printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
  3283. return 0;
  3284. non_fips_alg:
  3285. return -EINVAL;
  3286. }
  3287. #endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
  3288. EXPORT_SYMBOL_GPL(alg_test);