testmgr.c 84 KB

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