testmgr.c 80 KB

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