testmgr.c 84 KB

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