testmgr.c 85 KB

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