testmgr.c 82 KB

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