testmgr.c 85 KB

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