testmgr.c 84 KB

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