evp_test.c 126 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356
  1. /*
  2. * Copyright 2015-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #define OPENSSL_SUPPRESS_DEPRECATED /* EVP_PKEY_new_CMAC_key */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <ctype.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/pem.h>
  16. #include <openssl/err.h>
  17. #include <openssl/provider.h>
  18. #include <openssl/x509v3.h>
  19. #include <openssl/pkcs12.h>
  20. #include <openssl/kdf.h>
  21. #include <openssl/params.h>
  22. #include <openssl/core_names.h>
  23. #include <openssl/fips_names.h>
  24. #include <openssl/thread.h>
  25. #include "internal/numbers.h"
  26. #include "internal/nelem.h"
  27. #include "crypto/evp.h"
  28. #include "testutil.h"
  29. typedef struct evp_test_buffer_st EVP_TEST_BUFFER;
  30. DEFINE_STACK_OF(EVP_TEST_BUFFER)
  31. #define AAD_NUM 4
  32. typedef struct evp_test_method_st EVP_TEST_METHOD;
  33. /* Structure holding test information */
  34. typedef struct evp_test_st {
  35. STANZA s; /* Common test stanza */
  36. char *name;
  37. int skip; /* Current test should be skipped */
  38. const EVP_TEST_METHOD *meth; /* method for this test */
  39. const char *err, *aux_err; /* Error string for test */
  40. char *expected_err; /* Expected error value of test */
  41. char *reason; /* Expected error reason string */
  42. void *data; /* test specific data */
  43. } EVP_TEST;
  44. /* Test method structure */
  45. struct evp_test_method_st {
  46. /* Name of test as it appears in file */
  47. const char *name;
  48. /* Initialise test for "alg" */
  49. int (*init) (EVP_TEST *t, const char *alg);
  50. /* Clean up method */
  51. void (*cleanup) (EVP_TEST *t);
  52. /* Test specific name value pair processing */
  53. int (*parse) (EVP_TEST *t, const char *name, const char *value);
  54. /* Run the test itself */
  55. int (*run_test) (EVP_TEST *t);
  56. };
  57. /* Linked list of named keys. */
  58. typedef struct key_list_st {
  59. char *name;
  60. EVP_PKEY *key;
  61. struct key_list_st *next;
  62. } KEY_LIST;
  63. typedef enum OPTION_choice {
  64. OPT_ERR = -1,
  65. OPT_EOF = 0,
  66. OPT_CONFIG_FILE,
  67. OPT_IN_PLACE,
  68. OPT_PROVIDER_NAME,
  69. OPT_PROV_PROPQUERY,
  70. OPT_TEST_ENUM
  71. } OPTION_CHOICE;
  72. static OSSL_PROVIDER *prov_null = NULL;
  73. static OSSL_PROVIDER *libprov = NULL;
  74. static OSSL_LIB_CTX *libctx = NULL;
  75. /* List of public and private keys */
  76. static KEY_LIST *private_keys;
  77. static KEY_LIST *public_keys;
  78. static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst);
  79. static int parse_bin(const char *value, unsigned char **buf, size_t *buflen);
  80. static int is_digest_disabled(const char *name);
  81. static int is_pkey_disabled(const char *name);
  82. static int is_mac_disabled(const char *name);
  83. static int is_cipher_disabled(const char *name);
  84. static int is_kdf_disabled(const char *name);
  85. /*
  86. * Compare two memory regions for equality, returning zero if they differ.
  87. * However, if there is expected to be an error and the actual error
  88. * matches then the memory is expected to be different so handle this
  89. * case without producing unnecessary test framework output.
  90. */
  91. static int memory_err_compare(EVP_TEST *t, const char *err,
  92. const void *expected, size_t expected_len,
  93. const void *got, size_t got_len)
  94. {
  95. int r;
  96. if (t->expected_err != NULL && strcmp(t->expected_err, err) == 0)
  97. r = !TEST_mem_ne(expected, expected_len, got, got_len);
  98. else
  99. r = TEST_mem_eq(expected, expected_len, got, got_len);
  100. if (!r)
  101. t->err = err;
  102. return r;
  103. }
  104. /* Option specific for evp test */
  105. static int process_mode_in_place;
  106. static const char *propquery = NULL;
  107. static int evp_test_process_mode(char *mode)
  108. {
  109. if (strcmp(mode, "in_place") == 0)
  110. return 1;
  111. else if (strcmp(mode, "both") == 0)
  112. return 0;
  113. return -1;
  114. }
  115. /*
  116. * Structure used to hold a list of blocks of memory to test
  117. * calls to "update" like functions.
  118. */
  119. struct evp_test_buffer_st {
  120. unsigned char *buf;
  121. size_t buflen;
  122. size_t count;
  123. int count_set;
  124. };
  125. static void evp_test_buffer_free(EVP_TEST_BUFFER *db)
  126. {
  127. if (db != NULL) {
  128. OPENSSL_free(db->buf);
  129. OPENSSL_free(db);
  130. }
  131. }
  132. /* append buffer to a list */
  133. static int evp_test_buffer_append(const char *value,
  134. STACK_OF(EVP_TEST_BUFFER) **sk)
  135. {
  136. EVP_TEST_BUFFER *db = NULL;
  137. if (!TEST_ptr(db = OPENSSL_malloc(sizeof(*db))))
  138. goto err;
  139. if (!parse_bin(value, &db->buf, &db->buflen))
  140. goto err;
  141. db->count = 1;
  142. db->count_set = 0;
  143. if (*sk == NULL && !TEST_ptr(*sk = sk_EVP_TEST_BUFFER_new_null()))
  144. goto err;
  145. if (!sk_EVP_TEST_BUFFER_push(*sk, db))
  146. goto err;
  147. return 1;
  148. err:
  149. evp_test_buffer_free(db);
  150. return 0;
  151. }
  152. /* replace last buffer in list with copies of itself */
  153. static int evp_test_buffer_ncopy(const char *value,
  154. STACK_OF(EVP_TEST_BUFFER) *sk)
  155. {
  156. EVP_TEST_BUFFER *db;
  157. unsigned char *tbuf, *p;
  158. size_t tbuflen;
  159. int ncopy = atoi(value);
  160. int i;
  161. if (ncopy <= 0)
  162. return 0;
  163. if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
  164. return 0;
  165. db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
  166. tbuflen = db->buflen * ncopy;
  167. if (!TEST_ptr(tbuf = OPENSSL_malloc(tbuflen)))
  168. return 0;
  169. for (i = 0, p = tbuf; i < ncopy; i++, p += db->buflen)
  170. memcpy(p, db->buf, db->buflen);
  171. OPENSSL_free(db->buf);
  172. db->buf = tbuf;
  173. db->buflen = tbuflen;
  174. return 1;
  175. }
  176. /* set repeat count for last buffer in list */
  177. static int evp_test_buffer_set_count(const char *value,
  178. STACK_OF(EVP_TEST_BUFFER) *sk)
  179. {
  180. EVP_TEST_BUFFER *db;
  181. int count = atoi(value);
  182. if (count <= 0)
  183. return 0;
  184. if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
  185. return 0;
  186. db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
  187. if (db->count_set != 0)
  188. return 0;
  189. db->count = (size_t)count;
  190. db->count_set = 1;
  191. return 1;
  192. }
  193. /* call "fn" with each element of the list in turn */
  194. static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER) *sk,
  195. int (*fn)(void *ctx,
  196. const unsigned char *buf,
  197. size_t buflen),
  198. void *ctx)
  199. {
  200. int i;
  201. for (i = 0; i < sk_EVP_TEST_BUFFER_num(sk); i++) {
  202. EVP_TEST_BUFFER *tb = sk_EVP_TEST_BUFFER_value(sk, i);
  203. size_t j;
  204. for (j = 0; j < tb->count; j++) {
  205. if (fn(ctx, tb->buf, tb->buflen) <= 0)
  206. return 0;
  207. }
  208. }
  209. return 1;
  210. }
  211. /*
  212. * Unescape some sequences in string literals (only \n for now).
  213. * Return an allocated buffer, set |out_len|. If |input_len|
  214. * is zero, get an empty buffer but set length to zero.
  215. */
  216. static unsigned char* unescape(const char *input, size_t input_len,
  217. size_t *out_len)
  218. {
  219. unsigned char *ret, *p;
  220. size_t i;
  221. if (input_len == 0) {
  222. *out_len = 0;
  223. return OPENSSL_zalloc(1);
  224. }
  225. /* Escaping is non-expanding; over-allocate original size for simplicity. */
  226. if (!TEST_ptr(ret = p = OPENSSL_malloc(input_len)))
  227. return NULL;
  228. for (i = 0; i < input_len; i++) {
  229. if (*input == '\\') {
  230. if (i == input_len - 1 || *++input != 'n') {
  231. TEST_error("Bad escape sequence in file");
  232. goto err;
  233. }
  234. *p++ = '\n';
  235. i++;
  236. input++;
  237. } else {
  238. *p++ = *input++;
  239. }
  240. }
  241. *out_len = p - ret;
  242. return ret;
  243. err:
  244. OPENSSL_free(ret);
  245. return NULL;
  246. }
  247. /*
  248. * For a hex string "value" convert to a binary allocated buffer.
  249. * Return 1 on success or 0 on failure.
  250. */
  251. static int parse_bin(const char *value, unsigned char **buf, size_t *buflen)
  252. {
  253. long len;
  254. /* Check for NULL literal */
  255. if (strcmp(value, "NULL") == 0) {
  256. *buf = NULL;
  257. *buflen = 0;
  258. return 1;
  259. }
  260. /* Check for empty value */
  261. if (*value == '\0') {
  262. /*
  263. * Don't return NULL for zero length buffer. This is needed for
  264. * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key
  265. * buffer even if the key length is 0, in order to detect key reset.
  266. */
  267. *buf = OPENSSL_malloc(1);
  268. if (*buf == NULL)
  269. return 0;
  270. **buf = 0;
  271. *buflen = 0;
  272. return 1;
  273. }
  274. /* Check for string literal */
  275. if (value[0] == '"') {
  276. size_t vlen = strlen(++value);
  277. if (vlen == 0 || value[vlen - 1] != '"')
  278. return 0;
  279. vlen--;
  280. *buf = unescape(value, vlen, buflen);
  281. return *buf == NULL ? 0 : 1;
  282. }
  283. /* Otherwise assume as hex literal and convert it to binary buffer */
  284. if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) {
  285. TEST_info("Can't convert %s", value);
  286. TEST_openssl_errors();
  287. return -1;
  288. }
  289. /* Size of input buffer means we'll never overflow */
  290. *buflen = len;
  291. return 1;
  292. }
  293. /**
  294. ** MESSAGE DIGEST TESTS
  295. **/
  296. typedef struct digest_data_st {
  297. /* Digest this test is for */
  298. const EVP_MD *digest;
  299. EVP_MD *fetched_digest;
  300. /* Input to digest */
  301. STACK_OF(EVP_TEST_BUFFER) *input;
  302. /* Expected output */
  303. unsigned char *output;
  304. size_t output_len;
  305. /* Padding type */
  306. int pad_type;
  307. /* XOF mode? */
  308. int xof;
  309. /* Size for variable output length but non-XOF */
  310. size_t digest_size;
  311. } DIGEST_DATA;
  312. static int digest_test_init(EVP_TEST *t, const char *alg)
  313. {
  314. DIGEST_DATA *mdat;
  315. const EVP_MD *digest;
  316. EVP_MD *fetched_digest;
  317. if (is_digest_disabled(alg)) {
  318. TEST_info("skipping, '%s' is disabled", alg);
  319. t->skip = 1;
  320. return 1;
  321. }
  322. if ((digest = fetched_digest = EVP_MD_fetch(libctx, alg, propquery)) == NULL
  323. && (digest = EVP_get_digestbyname(alg)) == NULL)
  324. return 0;
  325. if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
  326. return 0;
  327. t->data = mdat;
  328. mdat->digest = digest;
  329. mdat->fetched_digest = fetched_digest;
  330. mdat->pad_type = 0;
  331. mdat->xof = 0;
  332. if (fetched_digest != NULL)
  333. TEST_info("%s is fetched", alg);
  334. return 1;
  335. }
  336. static void digest_test_cleanup(EVP_TEST *t)
  337. {
  338. DIGEST_DATA *mdat = t->data;
  339. sk_EVP_TEST_BUFFER_pop_free(mdat->input, evp_test_buffer_free);
  340. OPENSSL_free(mdat->output);
  341. EVP_MD_free(mdat->fetched_digest);
  342. }
  343. static int digest_test_parse(EVP_TEST *t,
  344. const char *keyword, const char *value)
  345. {
  346. DIGEST_DATA *mdata = t->data;
  347. if (strcmp(keyword, "Input") == 0)
  348. return evp_test_buffer_append(value, &mdata->input);
  349. if (strcmp(keyword, "Output") == 0)
  350. return parse_bin(value, &mdata->output, &mdata->output_len);
  351. if (strcmp(keyword, "Count") == 0)
  352. return evp_test_buffer_set_count(value, mdata->input);
  353. if (strcmp(keyword, "Ncopy") == 0)
  354. return evp_test_buffer_ncopy(value, mdata->input);
  355. if (strcmp(keyword, "Padding") == 0)
  356. return (mdata->pad_type = atoi(value)) > 0;
  357. if (strcmp(keyword, "XOF") == 0)
  358. return (mdata->xof = atoi(value)) > 0;
  359. if (strcmp(keyword, "OutputSize") == 0) {
  360. int sz;
  361. sz = atoi(value);
  362. if (sz < 0)
  363. return -1;
  364. mdata->digest_size = sz;
  365. return 1;
  366. }
  367. return 0;
  368. }
  369. static int digest_update_fn(void *ctx, const unsigned char *buf, size_t buflen)
  370. {
  371. return EVP_DigestUpdate(ctx, buf, buflen);
  372. }
  373. static int test_duplicate_md_ctx(EVP_TEST *t, EVP_MD_CTX *mctx)
  374. {
  375. char dont[] = "touch";
  376. if (!TEST_ptr(mctx))
  377. return 0;
  378. if (!EVP_DigestFinalXOF(mctx, (unsigned char *)dont, 0)) {
  379. EVP_MD_CTX_free(mctx);
  380. t->err = "DIGESTFINALXOF_ERROR";
  381. return 0;
  382. }
  383. if (!TEST_str_eq(dont, "touch")) {
  384. EVP_MD_CTX_free(mctx);
  385. t->err = "DIGESTFINALXOF_ERROR";
  386. return 0;
  387. }
  388. EVP_MD_CTX_free(mctx);
  389. return 1;
  390. }
  391. static int digest_test_run(EVP_TEST *t)
  392. {
  393. DIGEST_DATA *expected = t->data;
  394. EVP_TEST_BUFFER *inbuf;
  395. EVP_MD_CTX *mctx;
  396. unsigned char *got = NULL;
  397. unsigned int got_len;
  398. size_t size = 0;
  399. int xof = 0;
  400. OSSL_PARAM params[4], *p = &params[0];
  401. t->err = "TEST_FAILURE";
  402. if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
  403. goto err;
  404. got = OPENSSL_malloc(expected->output_len > EVP_MAX_MD_SIZE ?
  405. expected->output_len : EVP_MAX_MD_SIZE);
  406. if (!TEST_ptr(got))
  407. goto err;
  408. if (expected->xof > 0) {
  409. xof |= 1;
  410. *p++ = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN,
  411. &expected->output_len);
  412. }
  413. if (expected->digest_size > 0) {
  414. *p++ = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE,
  415. &expected->digest_size);
  416. }
  417. if (expected->pad_type > 0)
  418. *p++ = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_PAD_TYPE,
  419. &expected->pad_type);
  420. *p++ = OSSL_PARAM_construct_end();
  421. if (!EVP_DigestInit_ex2(mctx, expected->digest, params)) {
  422. t->err = "DIGESTINIT_ERROR";
  423. goto err;
  424. }
  425. if (!evp_test_buffer_do(expected->input, digest_update_fn, mctx)) {
  426. t->err = "DIGESTUPDATE_ERROR";
  427. goto err;
  428. }
  429. xof |= (EVP_MD_get_flags(expected->digest) & EVP_MD_FLAG_XOF) != 0;
  430. if (xof) {
  431. EVP_MD_CTX *mctx_cpy;
  432. if (!TEST_ptr(mctx_cpy = EVP_MD_CTX_new())) {
  433. goto err;
  434. }
  435. if (!TEST_true(EVP_MD_CTX_copy(mctx_cpy, mctx))) {
  436. EVP_MD_CTX_free(mctx_cpy);
  437. goto err;
  438. } else if (!test_duplicate_md_ctx(t, mctx_cpy)) {
  439. goto err;
  440. }
  441. if (!test_duplicate_md_ctx(t, EVP_MD_CTX_dup(mctx)))
  442. goto err;
  443. got_len = expected->output_len;
  444. if (!EVP_DigestFinalXOF(mctx, got, got_len)) {
  445. t->err = "DIGESTFINALXOF_ERROR";
  446. goto err;
  447. }
  448. } else {
  449. if (!EVP_DigestFinal(mctx, got, &got_len)) {
  450. t->err = "DIGESTFINAL_ERROR";
  451. goto err;
  452. }
  453. }
  454. if (!TEST_int_eq(expected->output_len, got_len)) {
  455. t->err = "DIGEST_LENGTH_MISMATCH";
  456. goto err;
  457. }
  458. if (!memory_err_compare(t, "DIGEST_MISMATCH",
  459. expected->output, expected->output_len,
  460. got, got_len))
  461. goto err;
  462. t->err = NULL;
  463. /* Test the EVP_Q_digest interface as well */
  464. if (sk_EVP_TEST_BUFFER_num(expected->input) == 1
  465. && !xof
  466. /* This should never fail but we need the returned pointer now */
  467. && !TEST_ptr(inbuf = sk_EVP_TEST_BUFFER_value(expected->input, 0))
  468. && !inbuf->count_set) {
  469. OPENSSL_cleanse(got, got_len);
  470. if (!TEST_true(EVP_Q_digest(libctx,
  471. EVP_MD_get0_name(expected->fetched_digest),
  472. NULL, inbuf->buf, inbuf->buflen,
  473. got, &size))
  474. || !TEST_mem_eq(got, size,
  475. expected->output, expected->output_len)) {
  476. t->err = "EVP_Q_digest failed";
  477. goto err;
  478. }
  479. }
  480. err:
  481. OPENSSL_free(got);
  482. EVP_MD_CTX_free(mctx);
  483. return 1;
  484. }
  485. static const EVP_TEST_METHOD digest_test_method = {
  486. "Digest",
  487. digest_test_init,
  488. digest_test_cleanup,
  489. digest_test_parse,
  490. digest_test_run
  491. };
  492. /**
  493. *** CIPHER TESTS
  494. **/
  495. typedef struct cipher_data_st {
  496. const EVP_CIPHER *cipher;
  497. EVP_CIPHER *fetched_cipher;
  498. int enc;
  499. /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
  500. int aead;
  501. unsigned char *key;
  502. size_t key_len;
  503. size_t key_bits; /* Used by RC2 */
  504. unsigned char *iv;
  505. unsigned char *next_iv; /* Expected IV state after operation */
  506. unsigned int rounds;
  507. size_t iv_len;
  508. unsigned char *plaintext;
  509. size_t plaintext_len;
  510. unsigned char *ciphertext;
  511. size_t ciphertext_len;
  512. /* AEAD ciphers only */
  513. unsigned char *aad[AAD_NUM];
  514. size_t aad_len[AAD_NUM];
  515. int tls_aad;
  516. int tls_version;
  517. unsigned char *tag;
  518. const char *cts_mode;
  519. size_t tag_len;
  520. int tag_late;
  521. unsigned char *mac_key;
  522. size_t mac_key_len;
  523. const char *xts_standard;
  524. } CIPHER_DATA;
  525. static int cipher_test_init(EVP_TEST *t, const char *alg)
  526. {
  527. const EVP_CIPHER *cipher;
  528. EVP_CIPHER *fetched_cipher;
  529. CIPHER_DATA *cdat;
  530. int m;
  531. if (is_cipher_disabled(alg)) {
  532. t->skip = 1;
  533. TEST_info("skipping, '%s' is disabled", alg);
  534. return 1;
  535. }
  536. ERR_set_mark();
  537. if ((cipher = fetched_cipher = EVP_CIPHER_fetch(libctx, alg, propquery)) == NULL
  538. && (cipher = EVP_get_cipherbyname(alg)) == NULL) {
  539. /* a stitched cipher might not be available */
  540. if (strstr(alg, "HMAC") != NULL) {
  541. ERR_pop_to_mark();
  542. t->skip = 1;
  543. TEST_info("skipping, '%s' is not available", alg);
  544. return 1;
  545. }
  546. ERR_clear_last_mark();
  547. return 0;
  548. }
  549. ERR_clear_last_mark();
  550. if (!TEST_ptr(cdat = OPENSSL_zalloc(sizeof(*cdat))))
  551. return 0;
  552. cdat->cipher = cipher;
  553. cdat->fetched_cipher = fetched_cipher;
  554. cdat->enc = -1;
  555. m = EVP_CIPHER_get_mode(cipher);
  556. if (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
  557. cdat->aead = m != 0 ? m : -1;
  558. else
  559. cdat->aead = 0;
  560. t->data = cdat;
  561. if (fetched_cipher != NULL)
  562. TEST_info("%s is fetched", alg);
  563. return 1;
  564. }
  565. static void cipher_test_cleanup(EVP_TEST *t)
  566. {
  567. int i;
  568. CIPHER_DATA *cdat = t->data;
  569. OPENSSL_free(cdat->key);
  570. OPENSSL_free(cdat->iv);
  571. OPENSSL_free(cdat->next_iv);
  572. OPENSSL_free(cdat->ciphertext);
  573. OPENSSL_free(cdat->plaintext);
  574. for (i = 0; i < AAD_NUM; i++)
  575. OPENSSL_free(cdat->aad[i]);
  576. OPENSSL_free(cdat->tag);
  577. OPENSSL_free(cdat->mac_key);
  578. EVP_CIPHER_free(cdat->fetched_cipher);
  579. }
  580. static int cipher_test_parse(EVP_TEST *t, const char *keyword,
  581. const char *value)
  582. {
  583. CIPHER_DATA *cdat = t->data;
  584. int i;
  585. if (strcmp(keyword, "Key") == 0)
  586. return parse_bin(value, &cdat->key, &cdat->key_len);
  587. if (strcmp(keyword, "Rounds") == 0) {
  588. i = atoi(value);
  589. if (i < 0)
  590. return -1;
  591. cdat->rounds = (unsigned int)i;
  592. return 1;
  593. }
  594. if (strcmp(keyword, "IV") == 0)
  595. return parse_bin(value, &cdat->iv, &cdat->iv_len);
  596. if (strcmp(keyword, "NextIV") == 0)
  597. return parse_bin(value, &cdat->next_iv, &cdat->iv_len);
  598. if (strcmp(keyword, "Plaintext") == 0)
  599. return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len);
  600. if (strcmp(keyword, "Ciphertext") == 0)
  601. return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
  602. if (strcmp(keyword, "KeyBits") == 0) {
  603. i = atoi(value);
  604. if (i < 0)
  605. return -1;
  606. cdat->key_bits = (size_t)i;
  607. return 1;
  608. }
  609. if (cdat->aead) {
  610. int tls_aad = 0;
  611. if (strcmp(keyword, "TLSAAD") == 0)
  612. cdat->tls_aad = tls_aad = 1;
  613. if (strcmp(keyword, "AAD") == 0 || tls_aad) {
  614. for (i = 0; i < AAD_NUM; i++) {
  615. if (cdat->aad[i] == NULL)
  616. return parse_bin(value, &cdat->aad[i], &cdat->aad_len[i]);
  617. }
  618. return -1;
  619. }
  620. if (strcmp(keyword, "Tag") == 0)
  621. return parse_bin(value, &cdat->tag, &cdat->tag_len);
  622. if (strcmp(keyword, "SetTagLate") == 0) {
  623. if (strcmp(value, "TRUE") == 0)
  624. cdat->tag_late = 1;
  625. else if (strcmp(value, "FALSE") == 0)
  626. cdat->tag_late = 0;
  627. else
  628. return -1;
  629. return 1;
  630. }
  631. if (strcmp(keyword, "MACKey") == 0)
  632. return parse_bin(value, &cdat->mac_key, &cdat->mac_key_len);
  633. if (strcmp(keyword, "TLSVersion") == 0) {
  634. char *endptr;
  635. cdat->tls_version = (int)strtol(value, &endptr, 0);
  636. return value[0] != '\0' && endptr[0] == '\0';
  637. }
  638. }
  639. if (strcmp(keyword, "Operation") == 0) {
  640. if (strcmp(value, "ENCRYPT") == 0)
  641. cdat->enc = 1;
  642. else if (strcmp(value, "DECRYPT") == 0)
  643. cdat->enc = 0;
  644. else
  645. return -1;
  646. return 1;
  647. }
  648. if (strcmp(keyword, "CTSMode") == 0) {
  649. cdat->cts_mode = value;
  650. return 1;
  651. }
  652. if (strcmp(keyword, "XTSStandard") == 0) {
  653. cdat->xts_standard = value;
  654. return 1;
  655. }
  656. return 0;
  657. }
  658. static int cipher_test_enc(EVP_TEST *t, int enc, size_t out_misalign,
  659. size_t inp_misalign, int frag, int in_place)
  660. {
  661. CIPHER_DATA *expected = t->data;
  662. unsigned char *in, *expected_out, *tmp = NULL;
  663. size_t in_len, out_len, donelen = 0;
  664. int ok = 0, tmplen, chunklen, tmpflen, i;
  665. EVP_CIPHER_CTX *ctx_base = NULL;
  666. EVP_CIPHER_CTX *ctx = NULL, *duped;
  667. int fips_dupctx_supported = fips_provider_version_ge(libctx, 3, 2, 0);
  668. t->err = "TEST_FAILURE";
  669. if (!TEST_ptr(ctx_base = EVP_CIPHER_CTX_new()))
  670. goto err;
  671. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
  672. goto err;
  673. EVP_CIPHER_CTX_set_flags(ctx_base, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
  674. if (enc) {
  675. in = expected->plaintext;
  676. in_len = expected->plaintext_len;
  677. expected_out = expected->ciphertext;
  678. out_len = expected->ciphertext_len;
  679. } else {
  680. in = expected->ciphertext;
  681. in_len = expected->ciphertext_len;
  682. expected_out = expected->plaintext;
  683. out_len = expected->plaintext_len;
  684. }
  685. if (in_place == 1) {
  686. /* Exercise in-place encryption */
  687. tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
  688. if (!tmp)
  689. goto err;
  690. in = memcpy(tmp + out_misalign, in, in_len);
  691. } else {
  692. inp_misalign += 16 - ((out_misalign + in_len) & 15);
  693. /*
  694. * 'tmp' will store both output and copy of input. We make the copy
  695. * of input to specifically aligned part of 'tmp'. So we just
  696. * figured out how much padding would ensure the required alignment,
  697. * now we allocate extended buffer and finally copy the input just
  698. * past inp_misalign in expression below. Output will be written
  699. * past out_misalign...
  700. */
  701. tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
  702. inp_misalign + in_len);
  703. if (!tmp)
  704. goto err;
  705. in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
  706. inp_misalign, in, in_len);
  707. }
  708. if (!EVP_CipherInit_ex(ctx_base, expected->cipher, NULL, NULL, NULL, enc)) {
  709. t->err = "CIPHERINIT_ERROR";
  710. goto err;
  711. }
  712. if (expected->cts_mode != NULL) {
  713. OSSL_PARAM params[2];
  714. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE,
  715. (char *)expected->cts_mode,
  716. 0);
  717. params[1] = OSSL_PARAM_construct_end();
  718. if (!EVP_CIPHER_CTX_set_params(ctx_base, params)) {
  719. t->err = "INVALID_CTS_MODE";
  720. goto err;
  721. }
  722. }
  723. if (expected->iv) {
  724. if (expected->aead) {
  725. if (EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_IVLEN,
  726. expected->iv_len, 0) <= 0) {
  727. t->err = "INVALID_IV_LENGTH";
  728. goto err;
  729. }
  730. } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_get_iv_length(ctx_base)) {
  731. t->err = "INVALID_IV_LENGTH";
  732. goto err;
  733. }
  734. }
  735. if (expected->aead && !expected->tls_aad) {
  736. unsigned char *tag;
  737. /*
  738. * If encrypting or OCB just set tag length initially, otherwise
  739. * set tag length and value.
  740. */
  741. if (enc || expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late) {
  742. t->err = "TAG_LENGTH_SET_ERROR";
  743. tag = NULL;
  744. } else {
  745. t->err = "TAG_SET_ERROR";
  746. tag = expected->tag;
  747. }
  748. if (tag || expected->aead != EVP_CIPH_GCM_MODE) {
  749. if (EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_TAG,
  750. expected->tag_len, tag) <= 0)
  751. goto err;
  752. }
  753. }
  754. if (expected->rounds > 0) {
  755. int rounds = (int)expected->rounds;
  756. if (EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC5_ROUNDS, rounds, NULL) <= 0) {
  757. t->err = "INVALID_ROUNDS";
  758. goto err;
  759. }
  760. }
  761. if (!EVP_CIPHER_CTX_set_key_length(ctx_base, expected->key_len)) {
  762. t->err = "INVALID_KEY_LENGTH";
  763. goto err;
  764. }
  765. if (expected->key_bits > 0) {
  766. int bits = (int)expected->key_bits;
  767. if (EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC2_KEY_BITS, bits, NULL) <= 0) {
  768. t->err = "INVALID KEY BITS";
  769. goto err;
  770. }
  771. }
  772. if (!EVP_CipherInit_ex(ctx_base, NULL, NULL, expected->key, expected->iv, -1)) {
  773. t->err = "KEY_SET_ERROR";
  774. goto err;
  775. }
  776. /* Check that we get the same IV back */
  777. if (expected->iv != NULL) {
  778. /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
  779. unsigned char iv[128];
  780. if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx_base, iv, sizeof(iv)))
  781. || ((EVP_CIPHER_get_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
  782. && !TEST_mem_eq(expected->iv, expected->iv_len, iv,
  783. expected->iv_len))) {
  784. t->err = "INVALID_IV";
  785. goto err;
  786. }
  787. }
  788. /* Test that the cipher dup functions correctly if it is supported */
  789. ERR_set_mark();
  790. if (!EVP_CIPHER_CTX_copy(ctx, ctx_base)) {
  791. if (fips_dupctx_supported) {
  792. TEST_info("Doing a copy of Cipher %s Fails!\n",
  793. EVP_CIPHER_get0_name(expected->cipher));
  794. ERR_print_errors_fp(stderr);
  795. goto err;
  796. } else {
  797. TEST_info("Allowing copy fail as an old fips provider is in use.");
  798. }
  799. EVP_CIPHER_CTX_free(ctx);
  800. ctx = ctx_base;
  801. } else {
  802. EVP_CIPHER_CTX_free(ctx_base);
  803. ctx_base = NULL;
  804. }
  805. /* Likewise for dup */
  806. duped = EVP_CIPHER_CTX_dup(ctx);
  807. if (duped != NULL) {
  808. EVP_CIPHER_CTX_free(ctx);
  809. ctx = duped;
  810. } else {
  811. if (fips_dupctx_supported) {
  812. TEST_info("Doing a dup of Cipher %s Fails!\n",
  813. EVP_CIPHER_get0_name(expected->cipher));
  814. ERR_print_errors_fp(stderr);
  815. goto err;
  816. } else {
  817. TEST_info("Allowing dup fail as an old fips provider is in use.");
  818. }
  819. }
  820. ERR_pop_to_mark();
  821. if (expected->mac_key != NULL
  822. && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY,
  823. (int)expected->mac_key_len,
  824. (void *)expected->mac_key) <= 0) {
  825. t->err = "SET_MAC_KEY_ERROR";
  826. goto err;
  827. }
  828. if (expected->tls_version) {
  829. OSSL_PARAM params[2];
  830. params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS_VERSION,
  831. &expected->tls_version);
  832. params[1] = OSSL_PARAM_construct_end();
  833. if (!EVP_CIPHER_CTX_set_params(ctx, params)) {
  834. t->err = "SET_TLS_VERSION_ERROR";
  835. goto err;
  836. }
  837. }
  838. if (expected->aead == EVP_CIPH_CCM_MODE) {
  839. if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
  840. t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
  841. goto err;
  842. }
  843. }
  844. if (expected->aad[0] != NULL && !expected->tls_aad) {
  845. t->err = "AAD_SET_ERROR";
  846. if (!frag) {
  847. for (i = 0; expected->aad[i] != NULL; i++) {
  848. if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i],
  849. expected->aad_len[i]))
  850. goto err;
  851. }
  852. } else {
  853. /*
  854. * Supply the AAD in chunks less than the block size where possible
  855. */
  856. for (i = 0; expected->aad[i] != NULL; i++) {
  857. if (expected->aad_len[i] > 0) {
  858. if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], 1))
  859. goto err;
  860. donelen++;
  861. }
  862. if (expected->aad_len[i] > 2) {
  863. if (!EVP_CipherUpdate(ctx, NULL, &chunklen,
  864. expected->aad[i] + donelen,
  865. expected->aad_len[i] - 2))
  866. goto err;
  867. donelen += expected->aad_len[i] - 2;
  868. }
  869. if (expected->aad_len[i] > 1
  870. && !EVP_CipherUpdate(ctx, NULL, &chunklen,
  871. expected->aad[i] + donelen, 1))
  872. goto err;
  873. }
  874. }
  875. }
  876. if (expected->tls_aad) {
  877. OSSL_PARAM params[2];
  878. char *tls_aad;
  879. /* duplicate the aad as the implementation might modify it */
  880. if ((tls_aad = OPENSSL_memdup(expected->aad[0],
  881. expected->aad_len[0])) == NULL)
  882. goto err;
  883. params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
  884. tls_aad,
  885. expected->aad_len[0]);
  886. params[1] = OSSL_PARAM_construct_end();
  887. if (!EVP_CIPHER_CTX_set_params(ctx, params)) {
  888. OPENSSL_free(tls_aad);
  889. t->err = "TLS1_AAD_ERROR";
  890. goto err;
  891. }
  892. OPENSSL_free(tls_aad);
  893. } else if (!enc && (expected->aead == EVP_CIPH_OCB_MODE
  894. || expected->tag_late)) {
  895. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  896. expected->tag_len, expected->tag) <= 0) {
  897. t->err = "TAG_SET_ERROR";
  898. goto err;
  899. }
  900. }
  901. if (expected->xts_standard != NULL) {
  902. OSSL_PARAM params[2];
  903. params[0] =
  904. OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_XTS_STANDARD,
  905. (char *)expected->xts_standard, 0);
  906. params[1] = OSSL_PARAM_construct_end();
  907. if (!EVP_CIPHER_CTX_set_params(ctx, params)) {
  908. t->err = "SET_XTS_STANDARD_ERROR";
  909. goto err;
  910. }
  911. }
  912. EVP_CIPHER_CTX_set_padding(ctx, 0);
  913. t->err = "CIPHERUPDATE_ERROR";
  914. tmplen = 0;
  915. if (!frag) {
  916. /* We supply the data all in one go */
  917. if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
  918. goto err;
  919. } else {
  920. /* Supply the data in chunks less than the block size where possible */
  921. if (in_len > 0) {
  922. if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
  923. goto err;
  924. tmplen += chunklen;
  925. in++;
  926. in_len--;
  927. }
  928. if (in_len > 1) {
  929. if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
  930. in, in_len - 1))
  931. goto err;
  932. tmplen += chunklen;
  933. in += in_len - 1;
  934. in_len = 1;
  935. }
  936. if (in_len > 0) {
  937. if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
  938. in, 1))
  939. goto err;
  940. tmplen += chunklen;
  941. }
  942. }
  943. if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
  944. t->err = "CIPHERFINAL_ERROR";
  945. goto err;
  946. }
  947. if (!enc && expected->tls_aad) {
  948. if (expected->tls_version >= TLS1_1_VERSION
  949. && (EVP_CIPHER_is_a(expected->cipher, "AES-128-CBC-HMAC-SHA1")
  950. || EVP_CIPHER_is_a(expected->cipher, "AES-256-CBC-HMAC-SHA1"))) {
  951. tmplen -= expected->iv_len;
  952. expected_out += expected->iv_len;
  953. out_misalign += expected->iv_len;
  954. }
  955. if ((int)out_len > tmplen + tmpflen)
  956. out_len = tmplen + tmpflen;
  957. }
  958. if (!memory_err_compare(t, "VALUE_MISMATCH", expected_out, out_len,
  959. tmp + out_misalign, tmplen + tmpflen))
  960. goto err;
  961. if (enc && expected->aead && !expected->tls_aad) {
  962. unsigned char rtag[16];
  963. if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) {
  964. t->err = "TAG_LENGTH_INTERNAL_ERROR";
  965. goto err;
  966. }
  967. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  968. expected->tag_len, rtag) <= 0) {
  969. t->err = "TAG_RETRIEVE_ERROR";
  970. goto err;
  971. }
  972. if (!memory_err_compare(t, "TAG_VALUE_MISMATCH",
  973. expected->tag, expected->tag_len,
  974. rtag, expected->tag_len))
  975. goto err;
  976. }
  977. /* Check the updated IV */
  978. if (expected->next_iv != NULL) {
  979. /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
  980. unsigned char iv[128];
  981. if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv)))
  982. || ((EVP_CIPHER_get_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
  983. && !TEST_mem_eq(expected->next_iv, expected->iv_len, iv,
  984. expected->iv_len))) {
  985. t->err = "INVALID_NEXT_IV";
  986. goto err;
  987. }
  988. }
  989. t->err = NULL;
  990. ok = 1;
  991. err:
  992. OPENSSL_free(tmp);
  993. if (ctx != ctx_base)
  994. EVP_CIPHER_CTX_free(ctx_base);
  995. EVP_CIPHER_CTX_free(ctx);
  996. return ok;
  997. }
  998. /*
  999. * XTS, SIV, CCM, stitched ciphers and Wrap modes have special
  1000. * requirements about input lengths so we don't fragment for those
  1001. */
  1002. static int cipher_test_valid_fragmentation(CIPHER_DATA *cdat)
  1003. {
  1004. return (cdat->aead == EVP_CIPH_CCM_MODE
  1005. || cdat->aead == EVP_CIPH_CBC_MODE
  1006. || (cdat->aead == -1
  1007. && EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_STREAM_CIPHER)
  1008. || ((EVP_CIPHER_get_flags(cdat->cipher) & EVP_CIPH_FLAG_CTS) != 0)
  1009. || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_SIV_MODE
  1010. || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_GCM_SIV_MODE
  1011. || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
  1012. || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE) ? 0 : 1;
  1013. }
  1014. static int cipher_test_run(EVP_TEST *t)
  1015. {
  1016. CIPHER_DATA *cdat = t->data;
  1017. int rv, frag, fragmax, in_place;
  1018. size_t out_misalign, inp_misalign;
  1019. TEST_info("RUNNING TEST FOR CIPHER %s\n", EVP_CIPHER_get0_name(cdat->cipher));
  1020. if (!cdat->key) {
  1021. t->err = "NO_KEY";
  1022. return 0;
  1023. }
  1024. if (!cdat->iv && EVP_CIPHER_get_iv_length(cdat->cipher)) {
  1025. /* IV is optional and usually omitted in wrap mode */
  1026. if (EVP_CIPHER_get_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
  1027. t->err = "NO_IV";
  1028. return 0;
  1029. }
  1030. }
  1031. if (cdat->aead && cdat->tag == NULL && !cdat->tls_aad) {
  1032. t->err = "NO_TAG";
  1033. return 0;
  1034. }
  1035. fragmax = (cipher_test_valid_fragmentation(cdat) == 0) ? 0 : 1;
  1036. for (in_place = 1; in_place >= 0; in_place--) {
  1037. static char aux_err[64];
  1038. t->aux_err = aux_err;
  1039. /* Test only in-place data processing */
  1040. if (process_mode_in_place == 1 && in_place == 0)
  1041. break;
  1042. for (frag = 0; frag <= fragmax; frag++) {
  1043. for (out_misalign = 0; out_misalign <= 1; out_misalign++) {
  1044. for (inp_misalign = 0; inp_misalign <= 1; inp_misalign++) {
  1045. /* Skip input misalign tests for in-place processing */
  1046. if (inp_misalign == 1 && in_place == 1)
  1047. break;
  1048. if (in_place == 1) {
  1049. BIO_snprintf(aux_err, sizeof(aux_err),
  1050. "%s in-place, %sfragmented",
  1051. out_misalign ? "misaligned" : "aligned",
  1052. frag ? "" : "not ");
  1053. } else {
  1054. BIO_snprintf(aux_err, sizeof(aux_err),
  1055. "%s output and %s input, %sfragmented",
  1056. out_misalign ? "misaligned" : "aligned",
  1057. inp_misalign ? "misaligned" : "aligned",
  1058. frag ? "" : "not ");
  1059. }
  1060. if (cdat->enc) {
  1061. rv = cipher_test_enc(t, 1, out_misalign, inp_misalign,
  1062. frag, in_place);
  1063. /* Not fatal errors: return */
  1064. if (rv != 1) {
  1065. if (rv < 0)
  1066. return 0;
  1067. return 1;
  1068. }
  1069. }
  1070. if (cdat->enc != 1) {
  1071. rv = cipher_test_enc(t, 0, out_misalign, inp_misalign,
  1072. frag, in_place);
  1073. /* Not fatal errors: return */
  1074. if (rv != 1) {
  1075. if (rv < 0)
  1076. return 0;
  1077. return 1;
  1078. }
  1079. }
  1080. }
  1081. }
  1082. }
  1083. }
  1084. t->aux_err = NULL;
  1085. return 1;
  1086. }
  1087. static const EVP_TEST_METHOD cipher_test_method = {
  1088. "Cipher",
  1089. cipher_test_init,
  1090. cipher_test_cleanup,
  1091. cipher_test_parse,
  1092. cipher_test_run
  1093. };
  1094. /**
  1095. ** MAC TESTS
  1096. **/
  1097. typedef struct mac_data_st {
  1098. /* MAC type in one form or another */
  1099. char *mac_name;
  1100. EVP_MAC *mac; /* for mac_test_run_mac */
  1101. int type; /* for mac_test_run_pkey */
  1102. /* Algorithm string for this MAC */
  1103. char *alg;
  1104. /* MAC key */
  1105. unsigned char *key;
  1106. size_t key_len;
  1107. /* MAC IV (GMAC) */
  1108. unsigned char *iv;
  1109. size_t iv_len;
  1110. /* Input to MAC */
  1111. unsigned char *input;
  1112. size_t input_len;
  1113. /* Expected output */
  1114. unsigned char *output;
  1115. size_t output_len;
  1116. unsigned char *custom;
  1117. size_t custom_len;
  1118. /* MAC salt (blake2) */
  1119. unsigned char *salt;
  1120. size_t salt_len;
  1121. /* XOF mode? */
  1122. int xof;
  1123. /* Reinitialization fails */
  1124. int no_reinit;
  1125. /* Collection of controls */
  1126. STACK_OF(OPENSSL_STRING) *controls;
  1127. /* Output size */
  1128. int output_size;
  1129. /* Block size */
  1130. int block_size;
  1131. } MAC_DATA;
  1132. static int mac_test_init(EVP_TEST *t, const char *alg)
  1133. {
  1134. EVP_MAC *mac = NULL;
  1135. int type = NID_undef;
  1136. MAC_DATA *mdat;
  1137. if (is_mac_disabled(alg)) {
  1138. TEST_info("skipping, '%s' is disabled", alg);
  1139. t->skip = 1;
  1140. return 1;
  1141. }
  1142. if ((mac = EVP_MAC_fetch(libctx, alg, propquery)) == NULL) {
  1143. /*
  1144. * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
  1145. * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
  1146. * the EVP_PKEY method.
  1147. */
  1148. size_t sz = strlen(alg);
  1149. static const char epilogue[] = " by EVP_PKEY";
  1150. if (sz >= sizeof(epilogue)
  1151. && strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0)
  1152. sz -= sizeof(epilogue) - 1;
  1153. if (strncmp(alg, "HMAC", sz) == 0)
  1154. type = EVP_PKEY_HMAC;
  1155. else if (strncmp(alg, "CMAC", sz) == 0)
  1156. type = EVP_PKEY_CMAC;
  1157. else if (strncmp(alg, "Poly1305", sz) == 0)
  1158. type = EVP_PKEY_POLY1305;
  1159. else if (strncmp(alg, "SipHash", sz) == 0)
  1160. type = EVP_PKEY_SIPHASH;
  1161. else
  1162. return 0;
  1163. }
  1164. if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
  1165. return 0;
  1166. mdat->type = type;
  1167. if (!TEST_ptr(mdat->mac_name = OPENSSL_strdup(alg))) {
  1168. OPENSSL_free(mdat);
  1169. return 0;
  1170. }
  1171. mdat->mac = mac;
  1172. if (!TEST_ptr(mdat->controls = sk_OPENSSL_STRING_new_null())) {
  1173. OPENSSL_free(mdat->mac_name);
  1174. OPENSSL_free(mdat);
  1175. return 0;
  1176. }
  1177. mdat->output_size = mdat->block_size = -1;
  1178. t->data = mdat;
  1179. return 1;
  1180. }
  1181. /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
  1182. static void openssl_free(char *m)
  1183. {
  1184. OPENSSL_free(m);
  1185. }
  1186. static void mac_test_cleanup(EVP_TEST *t)
  1187. {
  1188. MAC_DATA *mdat = t->data;
  1189. EVP_MAC_free(mdat->mac);
  1190. OPENSSL_free(mdat->mac_name);
  1191. sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free);
  1192. OPENSSL_free(mdat->alg);
  1193. OPENSSL_free(mdat->key);
  1194. OPENSSL_free(mdat->iv);
  1195. OPENSSL_free(mdat->custom);
  1196. OPENSSL_free(mdat->salt);
  1197. OPENSSL_free(mdat->input);
  1198. OPENSSL_free(mdat->output);
  1199. }
  1200. static int mac_test_parse(EVP_TEST *t,
  1201. const char *keyword, const char *value)
  1202. {
  1203. MAC_DATA *mdata = t->data;
  1204. if (strcmp(keyword, "Key") == 0)
  1205. return parse_bin(value, &mdata->key, &mdata->key_len);
  1206. if (strcmp(keyword, "IV") == 0)
  1207. return parse_bin(value, &mdata->iv, &mdata->iv_len);
  1208. if (strcmp(keyword, "Custom") == 0)
  1209. return parse_bin(value, &mdata->custom, &mdata->custom_len);
  1210. if (strcmp(keyword, "Salt") == 0)
  1211. return parse_bin(value, &mdata->salt, &mdata->salt_len);
  1212. if (strcmp(keyword, "Algorithm") == 0) {
  1213. mdata->alg = OPENSSL_strdup(value);
  1214. if (mdata->alg == NULL)
  1215. return -1;
  1216. return 1;
  1217. }
  1218. if (strcmp(keyword, "Input") == 0)
  1219. return parse_bin(value, &mdata->input, &mdata->input_len);
  1220. if (strcmp(keyword, "Output") == 0)
  1221. return parse_bin(value, &mdata->output, &mdata->output_len);
  1222. if (strcmp(keyword, "XOF") == 0)
  1223. return mdata->xof = 1;
  1224. if (strcmp(keyword, "NoReinit") == 0)
  1225. return mdata->no_reinit = 1;
  1226. if (strcmp(keyword, "Ctrl") == 0) {
  1227. char *data = OPENSSL_strdup(value);
  1228. if (data == NULL)
  1229. return -1;
  1230. return sk_OPENSSL_STRING_push(mdata->controls, data) != 0;
  1231. }
  1232. if (strcmp(keyword, "OutputSize") == 0) {
  1233. mdata->output_size = atoi(value);
  1234. if (mdata->output_size < 0)
  1235. return -1;
  1236. return 1;
  1237. }
  1238. if (strcmp(keyword, "BlockSize") == 0) {
  1239. mdata->block_size = atoi(value);
  1240. if (mdata->block_size < 0)
  1241. return -1;
  1242. return 1;
  1243. }
  1244. return 0;
  1245. }
  1246. static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx,
  1247. const char *value)
  1248. {
  1249. int rv = 0;
  1250. char *p, *tmpval;
  1251. if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
  1252. return 0;
  1253. p = strchr(tmpval, ':');
  1254. if (p != NULL) {
  1255. *p++ = '\0';
  1256. rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
  1257. }
  1258. if (rv == -2)
  1259. t->err = "PKEY_CTRL_INVALID";
  1260. else if (rv <= 0)
  1261. t->err = "PKEY_CTRL_ERROR";
  1262. else
  1263. rv = 1;
  1264. OPENSSL_free(tmpval);
  1265. return rv > 0;
  1266. }
  1267. static int mac_test_run_pkey(EVP_TEST *t)
  1268. {
  1269. MAC_DATA *expected = t->data;
  1270. EVP_MD_CTX *mctx = NULL;
  1271. EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
  1272. EVP_PKEY *key = NULL;
  1273. const char *mdname = NULL;
  1274. EVP_CIPHER *cipher = NULL;
  1275. unsigned char *got = NULL;
  1276. size_t got_len;
  1277. int i;
  1278. /* We don't do XOF mode via PKEY */
  1279. if (expected->xof)
  1280. return 1;
  1281. if (expected->alg == NULL)
  1282. TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type));
  1283. else
  1284. TEST_info("Trying the EVP_PKEY %s test with %s",
  1285. OBJ_nid2sn(expected->type), expected->alg);
  1286. if (expected->type == EVP_PKEY_CMAC) {
  1287. #ifdef OPENSSL_NO_DEPRECATED_3_0
  1288. TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
  1289. t->skip = 1;
  1290. t->err = NULL;
  1291. goto err;
  1292. #else
  1293. OSSL_LIB_CTX *tmpctx;
  1294. if (expected->alg != NULL && is_cipher_disabled(expected->alg)) {
  1295. TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
  1296. t->skip = 1;
  1297. t->err = NULL;
  1298. goto err;
  1299. }
  1300. if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, expected->alg, propquery))) {
  1301. t->err = "MAC_KEY_CREATE_ERROR";
  1302. goto err;
  1303. }
  1304. tmpctx = OSSL_LIB_CTX_set0_default(libctx);
  1305. key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len,
  1306. cipher);
  1307. OSSL_LIB_CTX_set0_default(tmpctx);
  1308. #endif
  1309. } else {
  1310. key = EVP_PKEY_new_raw_private_key_ex(libctx,
  1311. OBJ_nid2sn(expected->type), NULL,
  1312. expected->key, expected->key_len);
  1313. }
  1314. if (key == NULL) {
  1315. t->err = "MAC_KEY_CREATE_ERROR";
  1316. goto err;
  1317. }
  1318. if (expected->type == EVP_PKEY_HMAC && expected->alg != NULL) {
  1319. if (is_digest_disabled(expected->alg)) {
  1320. TEST_info("skipping, HMAC '%s' is disabled", expected->alg);
  1321. t->skip = 1;
  1322. t->err = NULL;
  1323. goto err;
  1324. }
  1325. mdname = expected->alg;
  1326. }
  1327. if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
  1328. t->err = "INTERNAL_ERROR";
  1329. goto err;
  1330. }
  1331. if (!EVP_DigestSignInit_ex(mctx, &pctx, mdname, libctx, NULL, key, NULL)) {
  1332. t->err = "DIGESTSIGNINIT_ERROR";
  1333. goto err;
  1334. }
  1335. for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++)
  1336. if (!mac_test_ctrl_pkey(t, pctx,
  1337. sk_OPENSSL_STRING_value(expected->controls,
  1338. i))) {
  1339. t->err = "EVPPKEYCTXCTRL_ERROR";
  1340. goto err;
  1341. }
  1342. if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) {
  1343. t->err = "DIGESTSIGNUPDATE_ERROR";
  1344. goto err;
  1345. }
  1346. if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) {
  1347. t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
  1348. goto err;
  1349. }
  1350. if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
  1351. t->err = "TEST_FAILURE";
  1352. goto err;
  1353. }
  1354. if (!EVP_DigestSignFinal(mctx, got, &got_len)
  1355. || !memory_err_compare(t, "TEST_MAC_ERR",
  1356. expected->output, expected->output_len,
  1357. got, got_len)) {
  1358. t->err = "TEST_MAC_ERR";
  1359. goto err;
  1360. }
  1361. t->err = NULL;
  1362. err:
  1363. EVP_CIPHER_free(cipher);
  1364. EVP_MD_CTX_free(mctx);
  1365. OPENSSL_free(got);
  1366. EVP_PKEY_CTX_free(genctx);
  1367. EVP_PKEY_free(key);
  1368. return 1;
  1369. }
  1370. static int mac_test_run_mac(EVP_TEST *t)
  1371. {
  1372. MAC_DATA *expected = t->data;
  1373. EVP_MAC_CTX *ctx = NULL;
  1374. unsigned char *got = NULL;
  1375. size_t got_len = 0, size = 0;
  1376. int i, block_size = -1, output_size = -1;
  1377. OSSL_PARAM params[21], sizes[3], *psizes = sizes;
  1378. size_t params_n = 0;
  1379. size_t params_n_allocstart = 0;
  1380. const OSSL_PARAM *defined_params =
  1381. EVP_MAC_settable_ctx_params(expected->mac);
  1382. int xof;
  1383. int reinit = 1;
  1384. if (expected->alg == NULL)
  1385. TEST_info("Trying the EVP_MAC %s test", expected->mac_name);
  1386. else
  1387. TEST_info("Trying the EVP_MAC %s test with %s",
  1388. expected->mac_name, expected->alg);
  1389. if (expected->alg != NULL) {
  1390. int skip = 0;
  1391. /*
  1392. * The underlying algorithm may be a cipher or a digest.
  1393. * We don't know which it is, but we can ask the MAC what it
  1394. * should be and bet on that.
  1395. */
  1396. if (OSSL_PARAM_locate_const(defined_params,
  1397. OSSL_MAC_PARAM_CIPHER) != NULL) {
  1398. if (is_cipher_disabled(expected->alg))
  1399. skip = 1;
  1400. else
  1401. params[params_n++] =
  1402. OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
  1403. expected->alg, 0);
  1404. } else if (OSSL_PARAM_locate_const(defined_params,
  1405. OSSL_MAC_PARAM_DIGEST) != NULL) {
  1406. if (is_digest_disabled(expected->alg))
  1407. skip = 1;
  1408. else
  1409. params[params_n++] =
  1410. OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
  1411. expected->alg, 0);
  1412. } else {
  1413. t->err = "MAC_BAD_PARAMS";
  1414. goto err;
  1415. }
  1416. if (skip) {
  1417. TEST_info("skipping, algorithm '%s' is disabled", expected->alg);
  1418. t->skip = 1;
  1419. t->err = NULL;
  1420. goto err;
  1421. }
  1422. }
  1423. if (expected->custom != NULL)
  1424. params[params_n++] =
  1425. OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
  1426. expected->custom,
  1427. expected->custom_len);
  1428. if (expected->salt != NULL)
  1429. params[params_n++] =
  1430. OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT,
  1431. expected->salt,
  1432. expected->salt_len);
  1433. if (expected->iv != NULL)
  1434. params[params_n++] =
  1435. OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
  1436. expected->iv,
  1437. expected->iv_len);
  1438. /* Unknown controls. They must match parameters that the MAC recognizes */
  1439. if (params_n + sk_OPENSSL_STRING_num(expected->controls)
  1440. >= OSSL_NELEM(params)) {
  1441. t->err = "MAC_TOO_MANY_PARAMETERS";
  1442. goto err;
  1443. }
  1444. params_n_allocstart = params_n;
  1445. for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) {
  1446. char *tmpkey, *tmpval;
  1447. char *value = sk_OPENSSL_STRING_value(expected->controls, i);
  1448. if (!TEST_ptr(tmpkey = OPENSSL_strdup(value))) {
  1449. t->err = "MAC_PARAM_ERROR";
  1450. goto err;
  1451. }
  1452. tmpval = strchr(tmpkey, ':');
  1453. if (tmpval != NULL)
  1454. *tmpval++ = '\0';
  1455. if (tmpval == NULL
  1456. || !OSSL_PARAM_allocate_from_text(&params[params_n],
  1457. defined_params,
  1458. tmpkey, tmpval,
  1459. strlen(tmpval), NULL)) {
  1460. OPENSSL_free(tmpkey);
  1461. t->err = "MAC_PARAM_ERROR";
  1462. goto err;
  1463. }
  1464. params_n++;
  1465. OPENSSL_free(tmpkey);
  1466. }
  1467. params[params_n] = OSSL_PARAM_construct_end();
  1468. if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) {
  1469. t->err = "MAC_CREATE_ERROR";
  1470. goto err;
  1471. }
  1472. if (!EVP_MAC_init(ctx, expected->key, expected->key_len, params)) {
  1473. t->err = "MAC_INIT_ERROR";
  1474. goto err;
  1475. }
  1476. if (expected->output_size >= 0)
  1477. *psizes++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_SIZE,
  1478. &output_size);
  1479. if (expected->block_size >= 0)
  1480. *psizes++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_BLOCK_SIZE,
  1481. &block_size);
  1482. if (psizes != sizes) {
  1483. *psizes = OSSL_PARAM_construct_end();
  1484. if (!TEST_true(EVP_MAC_CTX_get_params(ctx, sizes))) {
  1485. t->err = "INTERNAL_ERROR";
  1486. goto err;
  1487. }
  1488. if (expected->output_size >= 0
  1489. && !TEST_int_eq(output_size, expected->output_size)) {
  1490. t->err = "TEST_FAILURE";
  1491. goto err;
  1492. }
  1493. if (expected->block_size >= 0
  1494. && !TEST_int_eq(block_size, expected->block_size)) {
  1495. t->err = "TEST_FAILURE";
  1496. goto err;
  1497. }
  1498. }
  1499. retry:
  1500. if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) {
  1501. t->err = "MAC_UPDATE_ERROR";
  1502. goto err;
  1503. }
  1504. xof = expected->xof;
  1505. if (xof) {
  1506. if (!TEST_ptr(got = OPENSSL_malloc(expected->output_len))) {
  1507. t->err = "TEST_FAILURE";
  1508. goto err;
  1509. }
  1510. if (!EVP_MAC_finalXOF(ctx, got, expected->output_len)
  1511. || !memory_err_compare(t, "TEST_MAC_ERR",
  1512. expected->output, expected->output_len,
  1513. got, expected->output_len)) {
  1514. t->err = "MAC_FINAL_ERROR";
  1515. goto err;
  1516. }
  1517. } else {
  1518. if (!EVP_MAC_final(ctx, NULL, &got_len, 0)) {
  1519. t->err = "MAC_FINAL_LENGTH_ERROR";
  1520. goto err;
  1521. }
  1522. if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
  1523. t->err = "TEST_FAILURE";
  1524. goto err;
  1525. }
  1526. if (!EVP_MAC_final(ctx, got, &got_len, got_len)
  1527. || !memory_err_compare(t, "TEST_MAC_ERR",
  1528. expected->output, expected->output_len,
  1529. got, got_len)) {
  1530. t->err = "TEST_MAC_ERR";
  1531. goto err;
  1532. }
  1533. }
  1534. /* FIPS(3.0.0): can't reinitialise MAC contexts #18100 */
  1535. if (reinit-- && fips_provider_version_gt(libctx, 3, 0, 0)) {
  1536. OSSL_PARAM ivparams[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  1537. int ret;
  1538. /* If the MAC uses IV, we have to set it again */
  1539. if (expected->iv != NULL) {
  1540. ivparams[0] =
  1541. OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
  1542. expected->iv,
  1543. expected->iv_len);
  1544. ivparams[1] = OSSL_PARAM_construct_end();
  1545. }
  1546. ERR_set_mark();
  1547. ret = EVP_MAC_init(ctx, NULL, 0, ivparams);
  1548. if (expected->no_reinit) {
  1549. if (ret) {
  1550. ERR_clear_last_mark();
  1551. t->err = "MAC_REINIT_SHOULD_FAIL";
  1552. goto err;
  1553. }
  1554. } else if (ret) {
  1555. ERR_clear_last_mark();
  1556. OPENSSL_free(got);
  1557. got = NULL;
  1558. goto retry;
  1559. } else {
  1560. ERR_clear_last_mark();
  1561. t->err = "MAC_REINIT_ERROR";
  1562. goto err;
  1563. }
  1564. /* If reinitialization fails, it is unsupported by the algorithm */
  1565. ERR_pop_to_mark();
  1566. }
  1567. t->err = NULL;
  1568. /* Test the EVP_Q_mac interface as well */
  1569. if (!xof) {
  1570. OPENSSL_cleanse(got, got_len);
  1571. if (!TEST_true(EVP_Q_mac(libctx, expected->mac_name, NULL,
  1572. expected->alg, params,
  1573. expected->key, expected->key_len,
  1574. expected->input, expected->input_len,
  1575. got, got_len, &size))
  1576. || !TEST_mem_eq(got, size,
  1577. expected->output, expected->output_len)) {
  1578. t->err = "EVP_Q_mac failed";
  1579. goto err;
  1580. }
  1581. }
  1582. err:
  1583. while (params_n-- > params_n_allocstart) {
  1584. OPENSSL_free(params[params_n].data);
  1585. }
  1586. EVP_MAC_CTX_free(ctx);
  1587. OPENSSL_free(got);
  1588. return 1;
  1589. }
  1590. static int mac_test_run(EVP_TEST *t)
  1591. {
  1592. MAC_DATA *expected = t->data;
  1593. if (expected->mac != NULL)
  1594. return mac_test_run_mac(t);
  1595. return mac_test_run_pkey(t);
  1596. }
  1597. static const EVP_TEST_METHOD mac_test_method = {
  1598. "MAC",
  1599. mac_test_init,
  1600. mac_test_cleanup,
  1601. mac_test_parse,
  1602. mac_test_run
  1603. };
  1604. /**
  1605. ** PUBLIC KEY TESTS
  1606. ** These are all very similar and share much common code.
  1607. **/
  1608. typedef struct pkey_data_st {
  1609. /* Context for this operation */
  1610. EVP_PKEY_CTX *ctx;
  1611. /* Key operation to perform */
  1612. int (*keyop) (EVP_PKEY_CTX *ctx,
  1613. unsigned char *sig, size_t *siglen,
  1614. const unsigned char *tbs, size_t tbslen);
  1615. /* Input to MAC */
  1616. unsigned char *input;
  1617. size_t input_len;
  1618. /* Expected output */
  1619. unsigned char *output;
  1620. size_t output_len;
  1621. } PKEY_DATA;
  1622. /*
  1623. * Perform public key operation setup: lookup key, allocated ctx and call
  1624. * the appropriate initialisation function
  1625. */
  1626. static int pkey_test_init(EVP_TEST *t, const char *name,
  1627. int use_public,
  1628. int (*keyopinit) (EVP_PKEY_CTX *ctx),
  1629. int (*keyop)(EVP_PKEY_CTX *ctx,
  1630. unsigned char *sig, size_t *siglen,
  1631. const unsigned char *tbs,
  1632. size_t tbslen))
  1633. {
  1634. PKEY_DATA *kdata;
  1635. EVP_PKEY *pkey = NULL;
  1636. int rv = 0;
  1637. if (use_public)
  1638. rv = find_key(&pkey, name, public_keys);
  1639. if (rv == 0)
  1640. rv = find_key(&pkey, name, private_keys);
  1641. if (rv == 0 || pkey == NULL) {
  1642. TEST_info("skipping, key '%s' is disabled", name);
  1643. t->skip = 1;
  1644. return 1;
  1645. }
  1646. if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) {
  1647. EVP_PKEY_free(pkey);
  1648. return 0;
  1649. }
  1650. kdata->keyop = keyop;
  1651. if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propquery))) {
  1652. EVP_PKEY_free(pkey);
  1653. OPENSSL_free(kdata);
  1654. return 0;
  1655. }
  1656. if (keyopinit(kdata->ctx) <= 0)
  1657. t->err = "KEYOP_INIT_ERROR";
  1658. t->data = kdata;
  1659. return 1;
  1660. }
  1661. static void pkey_test_cleanup(EVP_TEST *t)
  1662. {
  1663. PKEY_DATA *kdata = t->data;
  1664. OPENSSL_free(kdata->input);
  1665. OPENSSL_free(kdata->output);
  1666. EVP_PKEY_CTX_free(kdata->ctx);
  1667. }
  1668. static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
  1669. const char *value)
  1670. {
  1671. int rv = 0;
  1672. char *p, *tmpval;
  1673. if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
  1674. return 0;
  1675. p = strchr(tmpval, ':');
  1676. if (p != NULL) {
  1677. *p++ = '\0';
  1678. rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
  1679. }
  1680. if (rv == -2) {
  1681. t->err = "PKEY_CTRL_INVALID";
  1682. rv = 1;
  1683. } else if (p != NULL && rv <= 0) {
  1684. if (is_digest_disabled(p) || is_cipher_disabled(p)) {
  1685. TEST_info("skipping, '%s' is disabled", p);
  1686. t->skip = 1;
  1687. rv = 1;
  1688. } else {
  1689. t->err = "PKEY_CTRL_ERROR";
  1690. rv = 1;
  1691. }
  1692. }
  1693. OPENSSL_free(tmpval);
  1694. return rv > 0;
  1695. }
  1696. static int pkey_test_parse(EVP_TEST *t,
  1697. const char *keyword, const char *value)
  1698. {
  1699. PKEY_DATA *kdata = t->data;
  1700. if (strcmp(keyword, "Input") == 0)
  1701. return parse_bin(value, &kdata->input, &kdata->input_len);
  1702. if (strcmp(keyword, "Output") == 0)
  1703. return parse_bin(value, &kdata->output, &kdata->output_len);
  1704. if (strcmp(keyword, "Ctrl") == 0)
  1705. return pkey_test_ctrl(t, kdata->ctx, value);
  1706. return 0;
  1707. }
  1708. static int pkey_test_run(EVP_TEST *t)
  1709. {
  1710. PKEY_DATA *expected = t->data;
  1711. unsigned char *got = NULL;
  1712. size_t got_len;
  1713. EVP_PKEY_CTX *copy = NULL;
  1714. if (expected->keyop(expected->ctx, NULL, &got_len,
  1715. expected->input, expected->input_len) <= 0
  1716. || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
  1717. t->err = "KEYOP_LENGTH_ERROR";
  1718. goto err;
  1719. }
  1720. if (expected->keyop(expected->ctx, got, &got_len,
  1721. expected->input, expected->input_len) <= 0) {
  1722. t->err = "KEYOP_ERROR";
  1723. goto err;
  1724. }
  1725. if (!memory_err_compare(t, "KEYOP_MISMATCH",
  1726. expected->output, expected->output_len,
  1727. got, got_len))
  1728. goto err;
  1729. t->err = NULL;
  1730. OPENSSL_free(got);
  1731. got = NULL;
  1732. /* Repeat the test on a copy. */
  1733. if (!TEST_ptr(copy = EVP_PKEY_CTX_dup(expected->ctx))) {
  1734. t->err = "INTERNAL_ERROR";
  1735. goto err;
  1736. }
  1737. if (expected->keyop(copy, NULL, &got_len, expected->input,
  1738. expected->input_len) <= 0
  1739. || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
  1740. t->err = "KEYOP_LENGTH_ERROR";
  1741. goto err;
  1742. }
  1743. if (expected->keyop(copy, got, &got_len, expected->input,
  1744. expected->input_len) <= 0) {
  1745. t->err = "KEYOP_ERROR";
  1746. goto err;
  1747. }
  1748. if (!memory_err_compare(t, "KEYOP_MISMATCH",
  1749. expected->output, expected->output_len,
  1750. got, got_len))
  1751. goto err;
  1752. err:
  1753. OPENSSL_free(got);
  1754. EVP_PKEY_CTX_free(copy);
  1755. return 1;
  1756. }
  1757. static int sign_test_init(EVP_TEST *t, const char *name)
  1758. {
  1759. return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
  1760. }
  1761. static const EVP_TEST_METHOD psign_test_method = {
  1762. "Sign",
  1763. sign_test_init,
  1764. pkey_test_cleanup,
  1765. pkey_test_parse,
  1766. pkey_test_run
  1767. };
  1768. static int verify_recover_test_init(EVP_TEST *t, const char *name)
  1769. {
  1770. return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
  1771. EVP_PKEY_verify_recover);
  1772. }
  1773. static const EVP_TEST_METHOD pverify_recover_test_method = {
  1774. "VerifyRecover",
  1775. verify_recover_test_init,
  1776. pkey_test_cleanup,
  1777. pkey_test_parse,
  1778. pkey_test_run
  1779. };
  1780. static int decrypt_test_init(EVP_TEST *t, const char *name)
  1781. {
  1782. return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
  1783. EVP_PKEY_decrypt);
  1784. }
  1785. static const EVP_TEST_METHOD pdecrypt_test_method = {
  1786. "Decrypt",
  1787. decrypt_test_init,
  1788. pkey_test_cleanup,
  1789. pkey_test_parse,
  1790. pkey_test_run
  1791. };
  1792. static int verify_test_init(EVP_TEST *t, const char *name)
  1793. {
  1794. return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
  1795. }
  1796. static int verify_test_run(EVP_TEST *t)
  1797. {
  1798. PKEY_DATA *kdata = t->data;
  1799. if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
  1800. kdata->input, kdata->input_len) <= 0)
  1801. t->err = "VERIFY_ERROR";
  1802. return 1;
  1803. }
  1804. static const EVP_TEST_METHOD pverify_test_method = {
  1805. "Verify",
  1806. verify_test_init,
  1807. pkey_test_cleanup,
  1808. pkey_test_parse,
  1809. verify_test_run
  1810. };
  1811. static int pderive_test_init(EVP_TEST *t, const char *name)
  1812. {
  1813. return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
  1814. }
  1815. static int pderive_test_parse(EVP_TEST *t,
  1816. const char *keyword, const char *value)
  1817. {
  1818. PKEY_DATA *kdata = t->data;
  1819. int validate = 0;
  1820. if (strcmp(keyword, "PeerKeyValidate") == 0)
  1821. validate = 1;
  1822. if (validate || strcmp(keyword, "PeerKey") == 0) {
  1823. EVP_PKEY *peer;
  1824. if (find_key(&peer, value, public_keys) == 0)
  1825. return -1;
  1826. if (EVP_PKEY_derive_set_peer_ex(kdata->ctx, peer, validate) <= 0) {
  1827. t->err = "DERIVE_SET_PEER_ERROR";
  1828. return 1;
  1829. }
  1830. t->err = NULL;
  1831. return 1;
  1832. }
  1833. if (strcmp(keyword, "SharedSecret") == 0)
  1834. return parse_bin(value, &kdata->output, &kdata->output_len);
  1835. if (strcmp(keyword, "Ctrl") == 0)
  1836. return pkey_test_ctrl(t, kdata->ctx, value);
  1837. if (strcmp(keyword, "KDFType") == 0) {
  1838. OSSL_PARAM params[2];
  1839. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_EXCHANGE_PARAM_KDF_TYPE,
  1840. (char *)value, 0);
  1841. params[1] = OSSL_PARAM_construct_end();
  1842. if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0)
  1843. return -1;
  1844. return 1;
  1845. }
  1846. if (strcmp(keyword, "KDFDigest") == 0) {
  1847. OSSL_PARAM params[2];
  1848. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_EXCHANGE_PARAM_KDF_DIGEST,
  1849. (char *)value, 0);
  1850. params[1] = OSSL_PARAM_construct_end();
  1851. if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0)
  1852. return -1;
  1853. return 1;
  1854. }
  1855. if (strcmp(keyword, "CEKAlg") == 0) {
  1856. OSSL_PARAM params[2];
  1857. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CEK_ALG,
  1858. (char *)value, 0);
  1859. params[1] = OSSL_PARAM_construct_end();
  1860. if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0)
  1861. return -1;
  1862. return 1;
  1863. }
  1864. if (strcmp(keyword, "KDFOutlen") == 0) {
  1865. OSSL_PARAM params[2];
  1866. char *endptr;
  1867. size_t outlen = (size_t)strtoul(value, &endptr, 0);
  1868. if (endptr[0] != '\0')
  1869. return -1;
  1870. params[0] = OSSL_PARAM_construct_size_t(OSSL_EXCHANGE_PARAM_KDF_OUTLEN,
  1871. &outlen);
  1872. params[1] = OSSL_PARAM_construct_end();
  1873. if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0)
  1874. return -1;
  1875. return 1;
  1876. }
  1877. return 0;
  1878. }
  1879. static int pderive_test_run(EVP_TEST *t)
  1880. {
  1881. EVP_PKEY_CTX *dctx = NULL;
  1882. PKEY_DATA *expected = t->data;
  1883. unsigned char *got = NULL;
  1884. size_t got_len;
  1885. if (!TEST_ptr(dctx = EVP_PKEY_CTX_dup(expected->ctx))) {
  1886. t->err = "DERIVE_ERROR";
  1887. goto err;
  1888. }
  1889. if (EVP_PKEY_derive(dctx, NULL, &got_len) <= 0
  1890. || !TEST_size_t_ne(got_len, 0)) {
  1891. t->err = "DERIVE_ERROR";
  1892. goto err;
  1893. }
  1894. if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
  1895. t->err = "DERIVE_ERROR";
  1896. goto err;
  1897. }
  1898. if (EVP_PKEY_derive(dctx, got, &got_len) <= 0) {
  1899. t->err = "DERIVE_ERROR";
  1900. goto err;
  1901. }
  1902. if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH",
  1903. expected->output, expected->output_len,
  1904. got, got_len))
  1905. goto err;
  1906. t->err = NULL;
  1907. err:
  1908. OPENSSL_free(got);
  1909. EVP_PKEY_CTX_free(dctx);
  1910. return 1;
  1911. }
  1912. static const EVP_TEST_METHOD pderive_test_method = {
  1913. "Derive",
  1914. pderive_test_init,
  1915. pkey_test_cleanup,
  1916. pderive_test_parse,
  1917. pderive_test_run
  1918. };
  1919. /**
  1920. ** PBE TESTS
  1921. **/
  1922. typedef enum pbe_type_enum {
  1923. PBE_TYPE_INVALID = 0,
  1924. PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
  1925. } PBE_TYPE;
  1926. typedef struct pbe_data_st {
  1927. PBE_TYPE pbe_type;
  1928. /* scrypt parameters */
  1929. uint64_t N, r, p, maxmem;
  1930. /* PKCS#12 parameters */
  1931. int id, iter;
  1932. const EVP_MD *md;
  1933. /* password */
  1934. unsigned char *pass;
  1935. size_t pass_len;
  1936. /* salt */
  1937. unsigned char *salt;
  1938. size_t salt_len;
  1939. /* Expected output */
  1940. unsigned char *key;
  1941. size_t key_len;
  1942. } PBE_DATA;
  1943. #ifndef OPENSSL_NO_SCRYPT
  1944. /* Parse unsigned decimal 64 bit integer value */
  1945. static int parse_uint64(const char *value, uint64_t *pr)
  1946. {
  1947. const char *p = value;
  1948. if (!TEST_true(*p)) {
  1949. TEST_info("Invalid empty integer value");
  1950. return -1;
  1951. }
  1952. for (*pr = 0; *p; ) {
  1953. if (*pr > UINT64_MAX / 10) {
  1954. TEST_error("Integer overflow in string %s", value);
  1955. return -1;
  1956. }
  1957. *pr *= 10;
  1958. if (!TEST_true(isdigit((unsigned char)*p))) {
  1959. TEST_error("Invalid character in string %s", value);
  1960. return -1;
  1961. }
  1962. *pr += *p - '0';
  1963. p++;
  1964. }
  1965. return 1;
  1966. }
  1967. static int scrypt_test_parse(EVP_TEST *t,
  1968. const char *keyword, const char *value)
  1969. {
  1970. PBE_DATA *pdata = t->data;
  1971. if (strcmp(keyword, "N") == 0)
  1972. return parse_uint64(value, &pdata->N);
  1973. if (strcmp(keyword, "p") == 0)
  1974. return parse_uint64(value, &pdata->p);
  1975. if (strcmp(keyword, "r") == 0)
  1976. return parse_uint64(value, &pdata->r);
  1977. if (strcmp(keyword, "maxmem") == 0)
  1978. return parse_uint64(value, &pdata->maxmem);
  1979. return 0;
  1980. }
  1981. #endif
  1982. static int pbkdf2_test_parse(EVP_TEST *t,
  1983. const char *keyword, const char *value)
  1984. {
  1985. PBE_DATA *pdata = t->data;
  1986. if (strcmp(keyword, "iter") == 0) {
  1987. pdata->iter = atoi(value);
  1988. if (pdata->iter <= 0)
  1989. return -1;
  1990. return 1;
  1991. }
  1992. if (strcmp(keyword, "MD") == 0) {
  1993. pdata->md = EVP_get_digestbyname(value);
  1994. if (pdata->md == NULL)
  1995. return -1;
  1996. return 1;
  1997. }
  1998. return 0;
  1999. }
  2000. static int pkcs12_test_parse(EVP_TEST *t,
  2001. const char *keyword, const char *value)
  2002. {
  2003. PBE_DATA *pdata = t->data;
  2004. if (strcmp(keyword, "id") == 0) {
  2005. pdata->id = atoi(value);
  2006. if (pdata->id <= 0)
  2007. return -1;
  2008. return 1;
  2009. }
  2010. return pbkdf2_test_parse(t, keyword, value);
  2011. }
  2012. static int pbe_test_init(EVP_TEST *t, const char *alg)
  2013. {
  2014. PBE_DATA *pdat;
  2015. PBE_TYPE pbe_type = PBE_TYPE_INVALID;
  2016. if (is_kdf_disabled(alg)) {
  2017. TEST_info("skipping, '%s' is disabled", alg);
  2018. t->skip = 1;
  2019. return 1;
  2020. }
  2021. if (strcmp(alg, "scrypt") == 0) {
  2022. pbe_type = PBE_TYPE_SCRYPT;
  2023. } else if (strcmp(alg, "pbkdf2") == 0) {
  2024. pbe_type = PBE_TYPE_PBKDF2;
  2025. } else if (strcmp(alg, "pkcs12") == 0) {
  2026. pbe_type = PBE_TYPE_PKCS12;
  2027. } else {
  2028. TEST_error("Unknown pbe algorithm %s", alg);
  2029. return 0;
  2030. }
  2031. if (!TEST_ptr(pdat = OPENSSL_zalloc(sizeof(*pdat))))
  2032. return 0;
  2033. pdat->pbe_type = pbe_type;
  2034. t->data = pdat;
  2035. return 1;
  2036. }
  2037. static void pbe_test_cleanup(EVP_TEST *t)
  2038. {
  2039. PBE_DATA *pdat = t->data;
  2040. OPENSSL_free(pdat->pass);
  2041. OPENSSL_free(pdat->salt);
  2042. OPENSSL_free(pdat->key);
  2043. }
  2044. static int pbe_test_parse(EVP_TEST *t,
  2045. const char *keyword, const char *value)
  2046. {
  2047. PBE_DATA *pdata = t->data;
  2048. if (strcmp(keyword, "Password") == 0)
  2049. return parse_bin(value, &pdata->pass, &pdata->pass_len);
  2050. if (strcmp(keyword, "Salt") == 0)
  2051. return parse_bin(value, &pdata->salt, &pdata->salt_len);
  2052. if (strcmp(keyword, "Key") == 0)
  2053. return parse_bin(value, &pdata->key, &pdata->key_len);
  2054. if (pdata->pbe_type == PBE_TYPE_PBKDF2)
  2055. return pbkdf2_test_parse(t, keyword, value);
  2056. else if (pdata->pbe_type == PBE_TYPE_PKCS12)
  2057. return pkcs12_test_parse(t, keyword, value);
  2058. #ifndef OPENSSL_NO_SCRYPT
  2059. else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
  2060. return scrypt_test_parse(t, keyword, value);
  2061. #endif
  2062. return 0;
  2063. }
  2064. static int pbe_test_run(EVP_TEST *t)
  2065. {
  2066. PBE_DATA *expected = t->data;
  2067. unsigned char *key;
  2068. EVP_MD *fetched_digest = NULL;
  2069. OSSL_LIB_CTX *save_libctx;
  2070. save_libctx = OSSL_LIB_CTX_set0_default(libctx);
  2071. if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
  2072. t->err = "INTERNAL_ERROR";
  2073. goto err;
  2074. }
  2075. if (expected->pbe_type == PBE_TYPE_PBKDF2) {
  2076. if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
  2077. expected->salt, expected->salt_len,
  2078. expected->iter, expected->md,
  2079. expected->key_len, key) == 0) {
  2080. t->err = "PBKDF2_ERROR";
  2081. goto err;
  2082. }
  2083. #ifndef OPENSSL_NO_SCRYPT
  2084. } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
  2085. if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
  2086. expected->salt, expected->salt_len,
  2087. expected->N, expected->r, expected->p,
  2088. expected->maxmem, key, expected->key_len) == 0) {
  2089. t->err = "SCRYPT_ERROR";
  2090. goto err;
  2091. }
  2092. #endif
  2093. } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
  2094. fetched_digest = EVP_MD_fetch(libctx, EVP_MD_get0_name(expected->md),
  2095. propquery);
  2096. if (fetched_digest == NULL) {
  2097. t->err = "PKCS12_ERROR";
  2098. goto err;
  2099. }
  2100. if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
  2101. expected->salt, expected->salt_len,
  2102. expected->id, expected->iter, expected->key_len,
  2103. key, fetched_digest) == 0) {
  2104. t->err = "PKCS12_ERROR";
  2105. goto err;
  2106. }
  2107. }
  2108. if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len,
  2109. key, expected->key_len))
  2110. goto err;
  2111. t->err = NULL;
  2112. err:
  2113. EVP_MD_free(fetched_digest);
  2114. OPENSSL_free(key);
  2115. OSSL_LIB_CTX_set0_default(save_libctx);
  2116. return 1;
  2117. }
  2118. static const EVP_TEST_METHOD pbe_test_method = {
  2119. "PBE",
  2120. pbe_test_init,
  2121. pbe_test_cleanup,
  2122. pbe_test_parse,
  2123. pbe_test_run
  2124. };
  2125. /**
  2126. ** BASE64 TESTS
  2127. **/
  2128. typedef enum {
  2129. BASE64_CANONICAL_ENCODING = 0,
  2130. BASE64_VALID_ENCODING = 1,
  2131. BASE64_INVALID_ENCODING = 2
  2132. } base64_encoding_type;
  2133. typedef struct encode_data_st {
  2134. /* Input to encoding */
  2135. unsigned char *input;
  2136. size_t input_len;
  2137. /* Expected output */
  2138. unsigned char *output;
  2139. size_t output_len;
  2140. base64_encoding_type encoding;
  2141. } ENCODE_DATA;
  2142. static int encode_test_init(EVP_TEST *t, const char *encoding)
  2143. {
  2144. ENCODE_DATA *edata;
  2145. if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
  2146. return 0;
  2147. if (strcmp(encoding, "canonical") == 0) {
  2148. edata->encoding = BASE64_CANONICAL_ENCODING;
  2149. } else if (strcmp(encoding, "valid") == 0) {
  2150. edata->encoding = BASE64_VALID_ENCODING;
  2151. } else if (strcmp(encoding, "invalid") == 0) {
  2152. edata->encoding = BASE64_INVALID_ENCODING;
  2153. if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
  2154. goto err;
  2155. } else {
  2156. TEST_error("Bad encoding: %s."
  2157. " Should be one of {canonical, valid, invalid}",
  2158. encoding);
  2159. goto err;
  2160. }
  2161. t->data = edata;
  2162. return 1;
  2163. err:
  2164. OPENSSL_free(edata);
  2165. return 0;
  2166. }
  2167. static void encode_test_cleanup(EVP_TEST *t)
  2168. {
  2169. ENCODE_DATA *edata = t->data;
  2170. OPENSSL_free(edata->input);
  2171. OPENSSL_free(edata->output);
  2172. memset(edata, 0, sizeof(*edata));
  2173. }
  2174. static int encode_test_parse(EVP_TEST *t,
  2175. const char *keyword, const char *value)
  2176. {
  2177. ENCODE_DATA *edata = t->data;
  2178. if (strcmp(keyword, "Input") == 0)
  2179. return parse_bin(value, &edata->input, &edata->input_len);
  2180. if (strcmp(keyword, "Output") == 0)
  2181. return parse_bin(value, &edata->output, &edata->output_len);
  2182. return 0;
  2183. }
  2184. static int encode_test_run(EVP_TEST *t)
  2185. {
  2186. ENCODE_DATA *expected = t->data;
  2187. unsigned char *encode_out = NULL, *decode_out = NULL;
  2188. int output_len, chunk_len;
  2189. EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL;
  2190. if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
  2191. t->err = "INTERNAL_ERROR";
  2192. goto err;
  2193. }
  2194. if (expected->encoding == BASE64_CANONICAL_ENCODING) {
  2195. if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
  2196. || !TEST_ptr(encode_out =
  2197. OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
  2198. goto err;
  2199. EVP_EncodeInit(encode_ctx);
  2200. if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
  2201. expected->input, expected->input_len)))
  2202. goto err;
  2203. output_len = chunk_len;
  2204. EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
  2205. output_len += chunk_len;
  2206. if (!memory_err_compare(t, "BAD_ENCODING",
  2207. expected->output, expected->output_len,
  2208. encode_out, output_len))
  2209. goto err;
  2210. }
  2211. if (!TEST_ptr(decode_out =
  2212. OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
  2213. goto err;
  2214. EVP_DecodeInit(decode_ctx);
  2215. if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
  2216. expected->output_len) < 0) {
  2217. t->err = "DECODE_ERROR";
  2218. goto err;
  2219. }
  2220. output_len = chunk_len;
  2221. if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
  2222. t->err = "DECODE_ERROR";
  2223. goto err;
  2224. }
  2225. output_len += chunk_len;
  2226. if (expected->encoding != BASE64_INVALID_ENCODING
  2227. && !memory_err_compare(t, "BAD_DECODING",
  2228. expected->input, expected->input_len,
  2229. decode_out, output_len)) {
  2230. t->err = "BAD_DECODING";
  2231. goto err;
  2232. }
  2233. t->err = NULL;
  2234. err:
  2235. OPENSSL_free(encode_out);
  2236. OPENSSL_free(decode_out);
  2237. EVP_ENCODE_CTX_free(decode_ctx);
  2238. EVP_ENCODE_CTX_free(encode_ctx);
  2239. return 1;
  2240. }
  2241. static const EVP_TEST_METHOD encode_test_method = {
  2242. "Encoding",
  2243. encode_test_init,
  2244. encode_test_cleanup,
  2245. encode_test_parse,
  2246. encode_test_run,
  2247. };
  2248. /**
  2249. ** RAND TESTS
  2250. **/
  2251. #define MAX_RAND_REPEATS 15
  2252. typedef struct rand_data_pass_st {
  2253. unsigned char *entropy;
  2254. unsigned char *reseed_entropy;
  2255. unsigned char *nonce;
  2256. unsigned char *pers;
  2257. unsigned char *reseed_addin;
  2258. unsigned char *addinA;
  2259. unsigned char *addinB;
  2260. unsigned char *pr_entropyA;
  2261. unsigned char *pr_entropyB;
  2262. unsigned char *output;
  2263. size_t entropy_len, nonce_len, pers_len, addinA_len, addinB_len,
  2264. pr_entropyA_len, pr_entropyB_len, output_len, reseed_entropy_len,
  2265. reseed_addin_len;
  2266. } RAND_DATA_PASS;
  2267. typedef struct rand_data_st {
  2268. /* Context for this operation */
  2269. EVP_RAND_CTX *ctx;
  2270. EVP_RAND_CTX *parent;
  2271. int n;
  2272. int prediction_resistance;
  2273. int use_df;
  2274. unsigned int generate_bits;
  2275. char *cipher;
  2276. char *digest;
  2277. /* Expected output */
  2278. RAND_DATA_PASS data[MAX_RAND_REPEATS];
  2279. } RAND_DATA;
  2280. static int rand_test_init(EVP_TEST *t, const char *name)
  2281. {
  2282. RAND_DATA *rdata;
  2283. EVP_RAND *rand;
  2284. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  2285. unsigned int strength = 256;
  2286. if (!TEST_ptr(rdata = OPENSSL_zalloc(sizeof(*rdata))))
  2287. return 0;
  2288. /* TEST-RAND is available in the FIPS provider but not with "fips=yes" */
  2289. rand = EVP_RAND_fetch(libctx, "TEST-RAND", "-fips");
  2290. if (rand == NULL)
  2291. goto err;
  2292. rdata->parent = EVP_RAND_CTX_new(rand, NULL);
  2293. EVP_RAND_free(rand);
  2294. if (rdata->parent == NULL)
  2295. goto err;
  2296. *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength);
  2297. if (!EVP_RAND_CTX_set_params(rdata->parent, params))
  2298. goto err;
  2299. rand = EVP_RAND_fetch(libctx, name, propquery);
  2300. if (rand == NULL)
  2301. goto err;
  2302. rdata->ctx = EVP_RAND_CTX_new(rand, rdata->parent);
  2303. EVP_RAND_free(rand);
  2304. if (rdata->ctx == NULL)
  2305. goto err;
  2306. rdata->n = -1;
  2307. t->data = rdata;
  2308. return 1;
  2309. err:
  2310. EVP_RAND_CTX_free(rdata->parent);
  2311. OPENSSL_free(rdata);
  2312. return 0;
  2313. }
  2314. static void rand_test_cleanup(EVP_TEST *t)
  2315. {
  2316. RAND_DATA *rdata = t->data;
  2317. int i;
  2318. OPENSSL_free(rdata->cipher);
  2319. OPENSSL_free(rdata->digest);
  2320. for (i = 0; i <= rdata->n; i++) {
  2321. OPENSSL_free(rdata->data[i].entropy);
  2322. OPENSSL_free(rdata->data[i].reseed_entropy);
  2323. OPENSSL_free(rdata->data[i].nonce);
  2324. OPENSSL_free(rdata->data[i].pers);
  2325. OPENSSL_free(rdata->data[i].reseed_addin);
  2326. OPENSSL_free(rdata->data[i].addinA);
  2327. OPENSSL_free(rdata->data[i].addinB);
  2328. OPENSSL_free(rdata->data[i].pr_entropyA);
  2329. OPENSSL_free(rdata->data[i].pr_entropyB);
  2330. OPENSSL_free(rdata->data[i].output);
  2331. }
  2332. EVP_RAND_CTX_free(rdata->ctx);
  2333. EVP_RAND_CTX_free(rdata->parent);
  2334. }
  2335. static int rand_test_parse(EVP_TEST *t,
  2336. const char *keyword, const char *value)
  2337. {
  2338. RAND_DATA *rdata = t->data;
  2339. RAND_DATA_PASS *item;
  2340. const char *p;
  2341. int n;
  2342. if ((p = strchr(keyword, '.')) != NULL) {
  2343. n = atoi(++p);
  2344. if (n >= MAX_RAND_REPEATS)
  2345. return 0;
  2346. if (n > rdata->n)
  2347. rdata->n = n;
  2348. item = rdata->data + n;
  2349. if (HAS_PREFIX(keyword, "Entropy."))
  2350. return parse_bin(value, &item->entropy, &item->entropy_len);
  2351. if (HAS_PREFIX(keyword, "ReseedEntropy."))
  2352. return parse_bin(value, &item->reseed_entropy,
  2353. &item->reseed_entropy_len);
  2354. if (HAS_PREFIX(keyword, "Nonce."))
  2355. return parse_bin(value, &item->nonce, &item->nonce_len);
  2356. if (HAS_PREFIX(keyword, "PersonalisationString."))
  2357. return parse_bin(value, &item->pers, &item->pers_len);
  2358. if (HAS_PREFIX(keyword, "ReseedAdditionalInput."))
  2359. return parse_bin(value, &item->reseed_addin,
  2360. &item->reseed_addin_len);
  2361. if (HAS_PREFIX(keyword, "AdditionalInputA."))
  2362. return parse_bin(value, &item->addinA, &item->addinA_len);
  2363. if (HAS_PREFIX(keyword, "AdditionalInputB."))
  2364. return parse_bin(value, &item->addinB, &item->addinB_len);
  2365. if (HAS_PREFIX(keyword, "EntropyPredictionResistanceA."))
  2366. return parse_bin(value, &item->pr_entropyA, &item->pr_entropyA_len);
  2367. if (HAS_PREFIX(keyword, "EntropyPredictionResistanceB."))
  2368. return parse_bin(value, &item->pr_entropyB, &item->pr_entropyB_len);
  2369. if (HAS_PREFIX(keyword, "Output."))
  2370. return parse_bin(value, &item->output, &item->output_len);
  2371. } else {
  2372. if (strcmp(keyword, "Cipher") == 0)
  2373. return TEST_ptr(rdata->cipher = OPENSSL_strdup(value));
  2374. if (strcmp(keyword, "Digest") == 0)
  2375. return TEST_ptr(rdata->digest = OPENSSL_strdup(value));
  2376. if (strcmp(keyword, "DerivationFunction") == 0) {
  2377. rdata->use_df = atoi(value) != 0;
  2378. return 1;
  2379. }
  2380. if (strcmp(keyword, "GenerateBits") == 0) {
  2381. if ((n = atoi(value)) <= 0 || n % 8 != 0)
  2382. return 0;
  2383. rdata->generate_bits = (unsigned int)n;
  2384. return 1;
  2385. }
  2386. if (strcmp(keyword, "PredictionResistance") == 0) {
  2387. rdata->prediction_resistance = atoi(value) != 0;
  2388. return 1;
  2389. }
  2390. }
  2391. return 0;
  2392. }
  2393. static int rand_test_run(EVP_TEST *t)
  2394. {
  2395. RAND_DATA *expected = t->data;
  2396. RAND_DATA_PASS *item;
  2397. unsigned char *got;
  2398. size_t got_len = expected->generate_bits / 8;
  2399. OSSL_PARAM params[5], *p = params;
  2400. int i = -1, ret = 0;
  2401. unsigned int strength;
  2402. unsigned char *z;
  2403. if (!TEST_ptr(got = OPENSSL_malloc(got_len)))
  2404. return 0;
  2405. *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &expected->use_df);
  2406. if (expected->cipher != NULL)
  2407. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
  2408. expected->cipher, 0);
  2409. if (expected->digest != NULL)
  2410. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
  2411. expected->digest, 0);
  2412. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC, "HMAC", 0);
  2413. *p = OSSL_PARAM_construct_end();
  2414. if (!TEST_true(EVP_RAND_CTX_set_params(expected->ctx, params)))
  2415. goto err;
  2416. strength = EVP_RAND_get_strength(expected->ctx);
  2417. for (i = 0; i <= expected->n; i++) {
  2418. item = expected->data + i;
  2419. p = params;
  2420. z = item->entropy != NULL ? item->entropy : (unsigned char *)"";
  2421. *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
  2422. z, item->entropy_len);
  2423. z = item->nonce != NULL ? item->nonce : (unsigned char *)"";
  2424. *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
  2425. z, item->nonce_len);
  2426. *p = OSSL_PARAM_construct_end();
  2427. if (!TEST_true(EVP_RAND_instantiate(expected->parent, strength,
  2428. 0, NULL, 0, params)))
  2429. goto err;
  2430. z = item->pers != NULL ? item->pers : (unsigned char *)"";
  2431. if (!TEST_true(EVP_RAND_instantiate
  2432. (expected->ctx, strength,
  2433. expected->prediction_resistance, z,
  2434. item->pers_len, NULL)))
  2435. goto err;
  2436. if (item->reseed_entropy != NULL) {
  2437. params[0] = OSSL_PARAM_construct_octet_string
  2438. (OSSL_RAND_PARAM_TEST_ENTROPY, item->reseed_entropy,
  2439. item->reseed_entropy_len);
  2440. params[1] = OSSL_PARAM_construct_end();
  2441. if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
  2442. goto err;
  2443. if (!TEST_true(EVP_RAND_reseed
  2444. (expected->ctx, expected->prediction_resistance,
  2445. NULL, 0, item->reseed_addin,
  2446. item->reseed_addin_len)))
  2447. goto err;
  2448. }
  2449. if (item->pr_entropyA != NULL) {
  2450. params[0] = OSSL_PARAM_construct_octet_string
  2451. (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyA,
  2452. item->pr_entropyA_len);
  2453. params[1] = OSSL_PARAM_construct_end();
  2454. if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
  2455. goto err;
  2456. }
  2457. if (!TEST_true(EVP_RAND_generate
  2458. (expected->ctx, got, got_len,
  2459. strength, expected->prediction_resistance,
  2460. item->addinA, item->addinA_len)))
  2461. goto err;
  2462. if (item->pr_entropyB != NULL) {
  2463. params[0] = OSSL_PARAM_construct_octet_string
  2464. (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyB,
  2465. item->pr_entropyB_len);
  2466. params[1] = OSSL_PARAM_construct_end();
  2467. if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
  2468. goto err;
  2469. }
  2470. if (!TEST_true(EVP_RAND_generate
  2471. (expected->ctx, got, got_len,
  2472. strength, expected->prediction_resistance,
  2473. item->addinB, item->addinB_len)))
  2474. goto err;
  2475. if (!TEST_mem_eq(got, got_len, item->output, item->output_len))
  2476. goto err;
  2477. if (!TEST_true(EVP_RAND_uninstantiate(expected->ctx))
  2478. || !TEST_true(EVP_RAND_uninstantiate(expected->parent))
  2479. || !TEST_true(EVP_RAND_verify_zeroization(expected->ctx))
  2480. || !TEST_int_eq(EVP_RAND_get_state(expected->ctx),
  2481. EVP_RAND_STATE_UNINITIALISED))
  2482. goto err;
  2483. }
  2484. t->err = NULL;
  2485. ret = 1;
  2486. err:
  2487. if (ret == 0 && i >= 0)
  2488. TEST_info("Error in test case %d of %d\n", i, expected->n + 1);
  2489. OPENSSL_free(got);
  2490. return ret;
  2491. }
  2492. static const EVP_TEST_METHOD rand_test_method = {
  2493. "RAND",
  2494. rand_test_init,
  2495. rand_test_cleanup,
  2496. rand_test_parse,
  2497. rand_test_run
  2498. };
  2499. /**
  2500. ** KDF TESTS
  2501. **/
  2502. typedef struct kdf_data_st {
  2503. /* Context for this operation */
  2504. EVP_KDF_CTX *ctx;
  2505. /* Expected output */
  2506. unsigned char *output;
  2507. size_t output_len;
  2508. OSSL_PARAM params[20];
  2509. OSSL_PARAM *p;
  2510. } KDF_DATA;
  2511. /*
  2512. * Perform public key operation setup: lookup key, allocated ctx and call
  2513. * the appropriate initialisation function
  2514. */
  2515. static int kdf_test_init(EVP_TEST *t, const char *name)
  2516. {
  2517. KDF_DATA *kdata;
  2518. EVP_KDF *kdf;
  2519. if (is_kdf_disabled(name)) {
  2520. TEST_info("skipping, '%s' is disabled", name);
  2521. t->skip = 1;
  2522. return 1;
  2523. }
  2524. if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
  2525. return 0;
  2526. kdata->p = kdata->params;
  2527. *kdata->p = OSSL_PARAM_construct_end();
  2528. kdf = EVP_KDF_fetch(libctx, name, propquery);
  2529. if (kdf == NULL) {
  2530. OPENSSL_free(kdata);
  2531. return 0;
  2532. }
  2533. kdata->ctx = EVP_KDF_CTX_new(kdf);
  2534. EVP_KDF_free(kdf);
  2535. if (kdata->ctx == NULL) {
  2536. OPENSSL_free(kdata);
  2537. return 0;
  2538. }
  2539. t->data = kdata;
  2540. return 1;
  2541. }
  2542. static void kdf_test_cleanup(EVP_TEST *t)
  2543. {
  2544. KDF_DATA *kdata = t->data;
  2545. OSSL_PARAM *p;
  2546. for (p = kdata->params; p->key != NULL; p++)
  2547. OPENSSL_free(p->data);
  2548. OPENSSL_free(kdata->output);
  2549. EVP_KDF_CTX_free(kdata->ctx);
  2550. }
  2551. static int kdf_test_ctrl(EVP_TEST *t, EVP_KDF_CTX *kctx,
  2552. const char *value)
  2553. {
  2554. KDF_DATA *kdata = t->data;
  2555. int rv;
  2556. char *p, *name;
  2557. const OSSL_PARAM *defs = EVP_KDF_settable_ctx_params(EVP_KDF_CTX_kdf(kctx));
  2558. if (!TEST_ptr(name = OPENSSL_strdup(value)))
  2559. return 0;
  2560. p = strchr(name, ':');
  2561. if (p != NULL)
  2562. *p++ = '\0';
  2563. if (strcmp(name, "r") == 0
  2564. && OSSL_PARAM_locate_const(defs, name) == NULL) {
  2565. TEST_info("skipping, setting 'r' is unsupported");
  2566. t->skip = 1;
  2567. goto end;
  2568. }
  2569. if (strcmp(name, "lanes") == 0
  2570. && OSSL_PARAM_locate_const(defs, name) == NULL) {
  2571. TEST_info("skipping, setting 'lanes' is unsupported");
  2572. t->skip = 1;
  2573. goto end;
  2574. }
  2575. if (strcmp(name, "iter") == 0
  2576. && OSSL_PARAM_locate_const(defs, name) == NULL) {
  2577. TEST_info("skipping, setting 'iter' is unsupported");
  2578. t->skip = 1;
  2579. goto end;
  2580. }
  2581. if (strcmp(name, "memcost") == 0
  2582. && OSSL_PARAM_locate_const(defs, name) == NULL) {
  2583. TEST_info("skipping, setting 'memcost' is unsupported");
  2584. t->skip = 1;
  2585. goto end;
  2586. }
  2587. if (strcmp(name, "secret") == 0
  2588. && OSSL_PARAM_locate_const(defs, name) == NULL) {
  2589. TEST_info("skipping, setting 'secret' is unsupported");
  2590. t->skip = 1;
  2591. goto end;
  2592. }
  2593. if (strcmp(name, "pass") == 0
  2594. && OSSL_PARAM_locate_const(defs, name) == NULL) {
  2595. TEST_info("skipping, setting 'pass' is unsupported");
  2596. t->skip = 1;
  2597. goto end;
  2598. }
  2599. if (strcmp(name, "ad") == 0
  2600. && OSSL_PARAM_locate_const(defs, name) == NULL) {
  2601. TEST_info("skipping, setting 'ad' is unsupported");
  2602. t->skip = 1;
  2603. goto end;
  2604. }
  2605. rv = OSSL_PARAM_allocate_from_text(kdata->p, defs, name, p,
  2606. p != NULL ? strlen(p) : 0, NULL);
  2607. *++kdata->p = OSSL_PARAM_construct_end();
  2608. if (!rv) {
  2609. t->err = "KDF_PARAM_ERROR";
  2610. OPENSSL_free(name);
  2611. return 0;
  2612. }
  2613. if (p != NULL && strcmp(name, "digest") == 0) {
  2614. if (is_digest_disabled(p)) {
  2615. TEST_info("skipping, '%s' is disabled", p);
  2616. t->skip = 1;
  2617. }
  2618. goto end;
  2619. }
  2620. if (p != NULL
  2621. && (strcmp(name, "cipher") == 0
  2622. || strcmp(name, "cekalg") == 0)
  2623. && is_cipher_disabled(p)) {
  2624. TEST_info("skipping, '%s' is disabled", p);
  2625. t->skip = 1;
  2626. goto end;
  2627. }
  2628. if (p != NULL
  2629. && (strcmp(name, "mac") == 0)
  2630. && is_mac_disabled(p)) {
  2631. TEST_info("skipping, '%s' is disabled", p);
  2632. t->skip = 1;
  2633. }
  2634. end:
  2635. OPENSSL_free(name);
  2636. return 1;
  2637. }
  2638. static int kdf_test_parse(EVP_TEST *t,
  2639. const char *keyword, const char *value)
  2640. {
  2641. KDF_DATA *kdata = t->data;
  2642. if (strcmp(keyword, "Output") == 0)
  2643. return parse_bin(value, &kdata->output, &kdata->output_len);
  2644. if (HAS_PREFIX(keyword, "Ctrl"))
  2645. return kdf_test_ctrl(t, kdata->ctx, value);
  2646. return 0;
  2647. }
  2648. static int kdf_test_run(EVP_TEST *t)
  2649. {
  2650. KDF_DATA *expected = t->data;
  2651. unsigned char *got = NULL;
  2652. size_t got_len = expected->output_len;
  2653. EVP_KDF_CTX *ctx;
  2654. if (!EVP_KDF_CTX_set_params(expected->ctx, expected->params)) {
  2655. t->err = "KDF_CTRL_ERROR";
  2656. return 1;
  2657. }
  2658. if (!TEST_ptr(got = OPENSSL_malloc(got_len == 0 ? 1 : got_len))) {
  2659. t->err = "INTERNAL_ERROR";
  2660. goto err;
  2661. }
  2662. /* FIPS(3.0.0): can't dup KDF contexts #17572 */
  2663. if (fips_provider_version_gt(libctx, 3, 0, 0)
  2664. && (ctx = EVP_KDF_CTX_dup(expected->ctx)) != NULL) {
  2665. EVP_KDF_CTX_free(expected->ctx);
  2666. expected->ctx = ctx;
  2667. }
  2668. if (EVP_KDF_derive(expected->ctx, got, got_len, NULL) <= 0) {
  2669. t->err = "KDF_DERIVE_ERROR";
  2670. goto err;
  2671. }
  2672. if (!memory_err_compare(t, "KDF_MISMATCH",
  2673. expected->output, expected->output_len,
  2674. got, got_len))
  2675. goto err;
  2676. t->err = NULL;
  2677. err:
  2678. OPENSSL_free(got);
  2679. return 1;
  2680. }
  2681. static const EVP_TEST_METHOD kdf_test_method = {
  2682. "KDF",
  2683. kdf_test_init,
  2684. kdf_test_cleanup,
  2685. kdf_test_parse,
  2686. kdf_test_run
  2687. };
  2688. /**
  2689. ** PKEY KDF TESTS
  2690. **/
  2691. typedef struct pkey_kdf_data_st {
  2692. /* Context for this operation */
  2693. EVP_PKEY_CTX *ctx;
  2694. /* Expected output */
  2695. unsigned char *output;
  2696. size_t output_len;
  2697. } PKEY_KDF_DATA;
  2698. /*
  2699. * Perform public key operation setup: lookup key, allocated ctx and call
  2700. * the appropriate initialisation function
  2701. */
  2702. static int pkey_kdf_test_init(EVP_TEST *t, const char *name)
  2703. {
  2704. PKEY_KDF_DATA *kdata = NULL;
  2705. if (is_kdf_disabled(name)) {
  2706. TEST_info("skipping, '%s' is disabled", name);
  2707. t->skip = 1;
  2708. return 1;
  2709. }
  2710. if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
  2711. return 0;
  2712. kdata->ctx = EVP_PKEY_CTX_new_from_name(libctx, name, propquery);
  2713. if (kdata->ctx == NULL
  2714. || EVP_PKEY_derive_init(kdata->ctx) <= 0)
  2715. goto err;
  2716. t->data = kdata;
  2717. return 1;
  2718. err:
  2719. EVP_PKEY_CTX_free(kdata->ctx);
  2720. OPENSSL_free(kdata);
  2721. return 0;
  2722. }
  2723. static void pkey_kdf_test_cleanup(EVP_TEST *t)
  2724. {
  2725. PKEY_KDF_DATA *kdata = t->data;
  2726. OPENSSL_free(kdata->output);
  2727. EVP_PKEY_CTX_free(kdata->ctx);
  2728. }
  2729. static int pkey_kdf_test_parse(EVP_TEST *t,
  2730. const char *keyword, const char *value)
  2731. {
  2732. PKEY_KDF_DATA *kdata = t->data;
  2733. if (strcmp(keyword, "Output") == 0)
  2734. return parse_bin(value, &kdata->output, &kdata->output_len);
  2735. if (HAS_PREFIX(keyword, "Ctrl"))
  2736. return pkey_test_ctrl(t, kdata->ctx, value);
  2737. return 0;
  2738. }
  2739. static int pkey_kdf_test_run(EVP_TEST *t)
  2740. {
  2741. PKEY_KDF_DATA *expected = t->data;
  2742. unsigned char *got = NULL;
  2743. size_t got_len = 0;
  2744. if (fips_provider_version_eq(libctx, 3, 0, 0)) {
  2745. /* FIPS(3.0.0): can't deal with oversized output buffers #18533 */
  2746. got_len = expected->output_len;
  2747. } else {
  2748. /* Find out the KDF output size */
  2749. if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) {
  2750. t->err = "INTERNAL_ERROR";
  2751. goto err;
  2752. }
  2753. /*
  2754. * We may get an absurd output size, which signals that anything goes.
  2755. * If not, we specify a too big buffer for the output, to test that
  2756. * EVP_PKEY_derive() can cope with it.
  2757. */
  2758. if (got_len == SIZE_MAX || got_len == 0)
  2759. got_len = expected->output_len;
  2760. else
  2761. got_len = expected->output_len * 2;
  2762. }
  2763. if (!TEST_ptr(got = OPENSSL_malloc(got_len == 0 ? 1 : got_len))) {
  2764. t->err = "INTERNAL_ERROR";
  2765. goto err;
  2766. }
  2767. if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
  2768. t->err = "KDF_DERIVE_ERROR";
  2769. goto err;
  2770. }
  2771. if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
  2772. t->err = "KDF_MISMATCH";
  2773. goto err;
  2774. }
  2775. t->err = NULL;
  2776. err:
  2777. OPENSSL_free(got);
  2778. return 1;
  2779. }
  2780. static const EVP_TEST_METHOD pkey_kdf_test_method = {
  2781. "PKEYKDF",
  2782. pkey_kdf_test_init,
  2783. pkey_kdf_test_cleanup,
  2784. pkey_kdf_test_parse,
  2785. pkey_kdf_test_run
  2786. };
  2787. /**
  2788. ** KEYPAIR TESTS
  2789. **/
  2790. typedef struct keypair_test_data_st {
  2791. EVP_PKEY *privk;
  2792. EVP_PKEY *pubk;
  2793. } KEYPAIR_TEST_DATA;
  2794. static int keypair_test_init(EVP_TEST *t, const char *pair)
  2795. {
  2796. KEYPAIR_TEST_DATA *data;
  2797. int rv = 0;
  2798. EVP_PKEY *pk = NULL, *pubk = NULL;
  2799. char *pub, *priv = NULL;
  2800. /* Split private and public names. */
  2801. if (!TEST_ptr(priv = OPENSSL_strdup(pair))
  2802. || !TEST_ptr(pub = strchr(priv, ':'))) {
  2803. t->err = "PARSING_ERROR";
  2804. goto end;
  2805. }
  2806. *pub++ = '\0';
  2807. if (!TEST_true(find_key(&pk, priv, private_keys))) {
  2808. TEST_info("Can't find private key: %s", priv);
  2809. t->err = "MISSING_PRIVATE_KEY";
  2810. goto end;
  2811. }
  2812. if (!TEST_true(find_key(&pubk, pub, public_keys))) {
  2813. TEST_info("Can't find public key: %s", pub);
  2814. t->err = "MISSING_PUBLIC_KEY";
  2815. goto end;
  2816. }
  2817. if (pk == NULL && pubk == NULL) {
  2818. /* Both keys are listed but unsupported: skip this test */
  2819. t->skip = 1;
  2820. rv = 1;
  2821. goto end;
  2822. }
  2823. if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
  2824. goto end;
  2825. data->privk = pk;
  2826. data->pubk = pubk;
  2827. t->data = data;
  2828. rv = 1;
  2829. t->err = NULL;
  2830. end:
  2831. OPENSSL_free(priv);
  2832. return rv;
  2833. }
  2834. static void keypair_test_cleanup(EVP_TEST *t)
  2835. {
  2836. OPENSSL_free(t->data);
  2837. t->data = NULL;
  2838. }
  2839. /*
  2840. * For tests that do not accept any custom keywords.
  2841. */
  2842. static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
  2843. {
  2844. return 0;
  2845. }
  2846. static int keypair_test_run(EVP_TEST *t)
  2847. {
  2848. int rv = 0;
  2849. const KEYPAIR_TEST_DATA *pair = t->data;
  2850. if (pair->privk == NULL || pair->pubk == NULL) {
  2851. /*
  2852. * this can only happen if only one of the keys is not set
  2853. * which means that one of them was unsupported while the
  2854. * other isn't: hence a key type mismatch.
  2855. */
  2856. t->err = "KEYPAIR_TYPE_MISMATCH";
  2857. rv = 1;
  2858. goto end;
  2859. }
  2860. if ((rv = EVP_PKEY_eq(pair->privk, pair->pubk)) != 1) {
  2861. if (0 == rv) {
  2862. t->err = "KEYPAIR_MISMATCH";
  2863. } else if (-1 == rv) {
  2864. t->err = "KEYPAIR_TYPE_MISMATCH";
  2865. } else if (-2 == rv) {
  2866. t->err = "UNSUPPORTED_KEY_COMPARISON";
  2867. } else {
  2868. TEST_error("Unexpected error in key comparison");
  2869. rv = 0;
  2870. goto end;
  2871. }
  2872. rv = 1;
  2873. goto end;
  2874. }
  2875. rv = 1;
  2876. t->err = NULL;
  2877. end:
  2878. return rv;
  2879. }
  2880. static const EVP_TEST_METHOD keypair_test_method = {
  2881. "PrivPubKeyPair",
  2882. keypair_test_init,
  2883. keypair_test_cleanup,
  2884. void_test_parse,
  2885. keypair_test_run
  2886. };
  2887. /**
  2888. ** KEYGEN TEST
  2889. **/
  2890. typedef struct keygen_test_data_st {
  2891. EVP_PKEY_CTX *genctx; /* Keygen context to use */
  2892. char *keyname; /* Key name to store key or NULL */
  2893. } KEYGEN_TEST_DATA;
  2894. static int keygen_test_init(EVP_TEST *t, const char *alg)
  2895. {
  2896. KEYGEN_TEST_DATA *data;
  2897. EVP_PKEY_CTX *genctx;
  2898. int nid = OBJ_sn2nid(alg);
  2899. if (nid == NID_undef) {
  2900. nid = OBJ_ln2nid(alg);
  2901. if (nid == NID_undef)
  2902. return 0;
  2903. }
  2904. if (is_pkey_disabled(alg)) {
  2905. t->skip = 1;
  2906. return 1;
  2907. }
  2908. if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_from_name(libctx, alg, propquery)))
  2909. goto err;
  2910. if (EVP_PKEY_keygen_init(genctx) <= 0) {
  2911. t->err = "KEYGEN_INIT_ERROR";
  2912. goto err;
  2913. }
  2914. if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
  2915. goto err;
  2916. data->genctx = genctx;
  2917. data->keyname = NULL;
  2918. t->data = data;
  2919. t->err = NULL;
  2920. return 1;
  2921. err:
  2922. EVP_PKEY_CTX_free(genctx);
  2923. return 0;
  2924. }
  2925. static void keygen_test_cleanup(EVP_TEST *t)
  2926. {
  2927. KEYGEN_TEST_DATA *keygen = t->data;
  2928. EVP_PKEY_CTX_free(keygen->genctx);
  2929. OPENSSL_free(keygen->keyname);
  2930. OPENSSL_free(t->data);
  2931. t->data = NULL;
  2932. }
  2933. static int keygen_test_parse(EVP_TEST *t,
  2934. const char *keyword, const char *value)
  2935. {
  2936. KEYGEN_TEST_DATA *keygen = t->data;
  2937. if (strcmp(keyword, "KeyName") == 0)
  2938. return TEST_ptr(keygen->keyname = OPENSSL_strdup(value));
  2939. if (strcmp(keyword, "Ctrl") == 0)
  2940. return pkey_test_ctrl(t, keygen->genctx, value);
  2941. return 0;
  2942. }
  2943. static int keygen_test_run(EVP_TEST *t)
  2944. {
  2945. KEYGEN_TEST_DATA *keygen = t->data;
  2946. EVP_PKEY *pkey = NULL;
  2947. int rv = 1;
  2948. if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) {
  2949. t->err = "KEYGEN_GENERATE_ERROR";
  2950. goto err;
  2951. }
  2952. if (!evp_pkey_is_provided(pkey)) {
  2953. TEST_info("Warning: legacy key generated %s", keygen->keyname);
  2954. goto err;
  2955. }
  2956. if (keygen->keyname != NULL) {
  2957. KEY_LIST *key;
  2958. rv = 0;
  2959. if (find_key(NULL, keygen->keyname, private_keys)) {
  2960. TEST_info("Duplicate key %s", keygen->keyname);
  2961. goto err;
  2962. }
  2963. if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
  2964. goto err;
  2965. key->name = keygen->keyname;
  2966. keygen->keyname = NULL;
  2967. key->key = pkey;
  2968. key->next = private_keys;
  2969. private_keys = key;
  2970. rv = 1;
  2971. } else {
  2972. EVP_PKEY_free(pkey);
  2973. }
  2974. t->err = NULL;
  2975. err:
  2976. return rv;
  2977. }
  2978. static const EVP_TEST_METHOD keygen_test_method = {
  2979. "KeyGen",
  2980. keygen_test_init,
  2981. keygen_test_cleanup,
  2982. keygen_test_parse,
  2983. keygen_test_run,
  2984. };
  2985. /**
  2986. ** DIGEST SIGN+VERIFY TESTS
  2987. **/
  2988. typedef struct {
  2989. int is_verify; /* Set to 1 if verifying */
  2990. int is_oneshot; /* Set to 1 for one shot operation */
  2991. const EVP_MD *md; /* Digest to use */
  2992. EVP_MD_CTX *ctx; /* Digest context */
  2993. EVP_PKEY_CTX *pctx;
  2994. STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
  2995. unsigned char *osin; /* Input data if one shot */
  2996. size_t osin_len; /* Input length data if one shot */
  2997. unsigned char *output; /* Expected output */
  2998. size_t output_len; /* Expected output length */
  2999. const char *nonce_type;
  3000. } DIGESTSIGN_DATA;
  3001. static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
  3002. int is_oneshot)
  3003. {
  3004. const EVP_MD *md = NULL;
  3005. DIGESTSIGN_DATA *mdat;
  3006. if (strcmp(alg, "NULL") != 0) {
  3007. if (is_digest_disabled(alg)) {
  3008. t->skip = 1;
  3009. return 1;
  3010. }
  3011. md = EVP_get_digestbyname(alg);
  3012. if (md == NULL)
  3013. return 0;
  3014. }
  3015. if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
  3016. return 0;
  3017. mdat->md = md;
  3018. if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
  3019. OPENSSL_free(mdat);
  3020. return 0;
  3021. }
  3022. mdat->is_verify = is_verify;
  3023. mdat->is_oneshot = is_oneshot;
  3024. t->data = mdat;
  3025. return 1;
  3026. }
  3027. static int digestsign_test_init(EVP_TEST *t, const char *alg)
  3028. {
  3029. return digestsigver_test_init(t, alg, 0, 0);
  3030. }
  3031. static void digestsigver_test_cleanup(EVP_TEST *t)
  3032. {
  3033. DIGESTSIGN_DATA *mdata = t->data;
  3034. EVP_MD_CTX_free(mdata->ctx);
  3035. sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
  3036. OPENSSL_free(mdata->osin);
  3037. OPENSSL_free(mdata->output);
  3038. OPENSSL_free(mdata);
  3039. t->data = NULL;
  3040. }
  3041. static int digestsigver_test_parse(EVP_TEST *t,
  3042. const char *keyword, const char *value)
  3043. {
  3044. DIGESTSIGN_DATA *mdata = t->data;
  3045. if (strcmp(keyword, "Key") == 0) {
  3046. EVP_PKEY *pkey = NULL;
  3047. int rv = 0;
  3048. const char *name = mdata->md == NULL ? NULL : EVP_MD_get0_name(mdata->md);
  3049. if (mdata->is_verify)
  3050. rv = find_key(&pkey, value, public_keys);
  3051. if (rv == 0)
  3052. rv = find_key(&pkey, value, private_keys);
  3053. if (rv == 0 || pkey == NULL) {
  3054. t->skip = 1;
  3055. return 1;
  3056. }
  3057. if (mdata->is_verify) {
  3058. if (!EVP_DigestVerifyInit_ex(mdata->ctx, &mdata->pctx, name, libctx,
  3059. NULL, pkey, NULL))
  3060. t->err = "DIGESTVERIFYINIT_ERROR";
  3061. return 1;
  3062. }
  3063. if (!EVP_DigestSignInit_ex(mdata->ctx, &mdata->pctx, name, libctx, NULL,
  3064. pkey, NULL))
  3065. t->err = "DIGESTSIGNINIT_ERROR";
  3066. return 1;
  3067. }
  3068. if (strcmp(keyword, "Input") == 0) {
  3069. if (mdata->is_oneshot)
  3070. return parse_bin(value, &mdata->osin, &mdata->osin_len);
  3071. return evp_test_buffer_append(value, &mdata->input);
  3072. }
  3073. if (strcmp(keyword, "Output") == 0)
  3074. return parse_bin(value, &mdata->output, &mdata->output_len);
  3075. if (!mdata->is_oneshot) {
  3076. if (strcmp(keyword, "Count") == 0)
  3077. return evp_test_buffer_set_count(value, mdata->input);
  3078. if (strcmp(keyword, "Ncopy") == 0)
  3079. return evp_test_buffer_ncopy(value, mdata->input);
  3080. }
  3081. if (strcmp(keyword, "Ctrl") == 0) {
  3082. if (mdata->pctx == NULL)
  3083. return -1;
  3084. return pkey_test_ctrl(t, mdata->pctx, value);
  3085. }
  3086. if (strcmp(keyword, "NonceType") == 0) {
  3087. if (strcmp(value, "deterministic") == 0) {
  3088. OSSL_PARAM params[2];
  3089. unsigned int nonce_type = 1;
  3090. params[0] =
  3091. OSSL_PARAM_construct_uint(OSSL_SIGNATURE_PARAM_NONCE_TYPE,
  3092. &nonce_type);
  3093. params[1] = OSSL_PARAM_construct_end();
  3094. if (!EVP_PKEY_CTX_set_params(mdata->pctx, params))
  3095. t->err = "EVP_PKEY_CTX_set_params_ERROR";
  3096. else if (!EVP_PKEY_CTX_get_params(mdata->pctx, params))
  3097. t->err = "EVP_PKEY_CTX_get_params_ERROR";
  3098. else if (!OSSL_PARAM_modified(&params[0]))
  3099. t->err = "nonce_type_not_modified_ERROR";
  3100. else if (nonce_type != 1)
  3101. t->err = "nonce_type_value_ERROR";
  3102. }
  3103. return 1;
  3104. }
  3105. return 0;
  3106. }
  3107. static int digestsign_update_fn(void *ctx, const unsigned char *buf,
  3108. size_t buflen)
  3109. {
  3110. return EVP_DigestSignUpdate(ctx, buf, buflen);
  3111. }
  3112. static int digestsign_test_run(EVP_TEST *t)
  3113. {
  3114. DIGESTSIGN_DATA *expected = t->data;
  3115. unsigned char *got = NULL;
  3116. size_t got_len;
  3117. if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
  3118. expected->ctx)) {
  3119. t->err = "DIGESTUPDATE_ERROR";
  3120. goto err;
  3121. }
  3122. if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
  3123. t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
  3124. goto err;
  3125. }
  3126. if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
  3127. t->err = "MALLOC_FAILURE";
  3128. goto err;
  3129. }
  3130. got_len *= 2;
  3131. if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
  3132. t->err = "DIGESTSIGNFINAL_ERROR";
  3133. goto err;
  3134. }
  3135. if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
  3136. expected->output, expected->output_len,
  3137. got, got_len))
  3138. goto err;
  3139. t->err = NULL;
  3140. err:
  3141. OPENSSL_free(got);
  3142. return 1;
  3143. }
  3144. static const EVP_TEST_METHOD digestsign_test_method = {
  3145. "DigestSign",
  3146. digestsign_test_init,
  3147. digestsigver_test_cleanup,
  3148. digestsigver_test_parse,
  3149. digestsign_test_run
  3150. };
  3151. static int digestverify_test_init(EVP_TEST *t, const char *alg)
  3152. {
  3153. return digestsigver_test_init(t, alg, 1, 0);
  3154. }
  3155. static int digestverify_update_fn(void *ctx, const unsigned char *buf,
  3156. size_t buflen)
  3157. {
  3158. return EVP_DigestVerifyUpdate(ctx, buf, buflen);
  3159. }
  3160. static int digestverify_test_run(EVP_TEST *t)
  3161. {
  3162. DIGESTSIGN_DATA *mdata = t->data;
  3163. if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
  3164. t->err = "DIGESTUPDATE_ERROR";
  3165. return 1;
  3166. }
  3167. if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
  3168. mdata->output_len) <= 0)
  3169. t->err = "VERIFY_ERROR";
  3170. return 1;
  3171. }
  3172. static const EVP_TEST_METHOD digestverify_test_method = {
  3173. "DigestVerify",
  3174. digestverify_test_init,
  3175. digestsigver_test_cleanup,
  3176. digestsigver_test_parse,
  3177. digestverify_test_run
  3178. };
  3179. static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
  3180. {
  3181. return digestsigver_test_init(t, alg, 0, 1);
  3182. }
  3183. static int oneshot_digestsign_test_run(EVP_TEST *t)
  3184. {
  3185. DIGESTSIGN_DATA *expected = t->data;
  3186. unsigned char *got = NULL;
  3187. size_t got_len;
  3188. if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
  3189. expected->osin, expected->osin_len)) {
  3190. t->err = "DIGESTSIGN_LENGTH_ERROR";
  3191. goto err;
  3192. }
  3193. if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
  3194. t->err = "MALLOC_FAILURE";
  3195. goto err;
  3196. }
  3197. got_len *= 2;
  3198. if (!EVP_DigestSign(expected->ctx, got, &got_len,
  3199. expected->osin, expected->osin_len)) {
  3200. t->err = "DIGESTSIGN_ERROR";
  3201. goto err;
  3202. }
  3203. if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
  3204. expected->output, expected->output_len,
  3205. got, got_len))
  3206. goto err;
  3207. t->err = NULL;
  3208. err:
  3209. OPENSSL_free(got);
  3210. return 1;
  3211. }
  3212. static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
  3213. "OneShotDigestSign",
  3214. oneshot_digestsign_test_init,
  3215. digestsigver_test_cleanup,
  3216. digestsigver_test_parse,
  3217. oneshot_digestsign_test_run
  3218. };
  3219. static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
  3220. {
  3221. return digestsigver_test_init(t, alg, 1, 1);
  3222. }
  3223. static int oneshot_digestverify_test_run(EVP_TEST *t)
  3224. {
  3225. DIGESTSIGN_DATA *mdata = t->data;
  3226. if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
  3227. mdata->osin, mdata->osin_len) <= 0)
  3228. t->err = "VERIFY_ERROR";
  3229. return 1;
  3230. }
  3231. static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
  3232. "OneShotDigestVerify",
  3233. oneshot_digestverify_test_init,
  3234. digestsigver_test_cleanup,
  3235. digestsigver_test_parse,
  3236. oneshot_digestverify_test_run
  3237. };
  3238. /**
  3239. ** PARSING AND DISPATCH
  3240. **/
  3241. static const EVP_TEST_METHOD *evp_test_list[] = {
  3242. &rand_test_method,
  3243. &cipher_test_method,
  3244. &digest_test_method,
  3245. &digestsign_test_method,
  3246. &digestverify_test_method,
  3247. &encode_test_method,
  3248. &kdf_test_method,
  3249. &pkey_kdf_test_method,
  3250. &keypair_test_method,
  3251. &keygen_test_method,
  3252. &mac_test_method,
  3253. &oneshot_digestsign_test_method,
  3254. &oneshot_digestverify_test_method,
  3255. &pbe_test_method,
  3256. &pdecrypt_test_method,
  3257. &pderive_test_method,
  3258. &psign_test_method,
  3259. &pverify_recover_test_method,
  3260. &pverify_test_method,
  3261. NULL
  3262. };
  3263. static const EVP_TEST_METHOD *find_test(const char *name)
  3264. {
  3265. const EVP_TEST_METHOD **tt;
  3266. for (tt = evp_test_list; *tt; tt++) {
  3267. if (strcmp(name, (*tt)->name) == 0)
  3268. return *tt;
  3269. }
  3270. return NULL;
  3271. }
  3272. static void clear_test(EVP_TEST *t)
  3273. {
  3274. test_clearstanza(&t->s);
  3275. ERR_clear_error();
  3276. if (t->data != NULL) {
  3277. if (t->meth != NULL)
  3278. t->meth->cleanup(t);
  3279. OPENSSL_free(t->data);
  3280. t->data = NULL;
  3281. }
  3282. OPENSSL_free(t->expected_err);
  3283. t->expected_err = NULL;
  3284. OPENSSL_free(t->reason);
  3285. t->reason = NULL;
  3286. /* Text literal. */
  3287. t->err = NULL;
  3288. t->skip = 0;
  3289. t->meth = NULL;
  3290. #if !defined(OPENSSL_NO_DEFAULT_THREAD_POOL)
  3291. OSSL_set_max_threads(libctx, 0);
  3292. #endif
  3293. }
  3294. /* Check for errors in the test structure; return 1 if okay, else 0. */
  3295. static int check_test_error(EVP_TEST *t)
  3296. {
  3297. unsigned long err;
  3298. const char *reason;
  3299. if (t->err == NULL && t->expected_err == NULL)
  3300. return 1;
  3301. if (t->err != NULL && t->expected_err == NULL) {
  3302. if (t->aux_err != NULL) {
  3303. TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
  3304. t->s.test_file, t->s.start, t->aux_err, t->err);
  3305. } else {
  3306. TEST_info("%s:%d: Source of above error; unexpected error %s",
  3307. t->s.test_file, t->s.start, t->err);
  3308. }
  3309. return 0;
  3310. }
  3311. if (t->err == NULL && t->expected_err != NULL) {
  3312. TEST_info("%s:%d: Succeeded but was expecting %s",
  3313. t->s.test_file, t->s.start, t->expected_err);
  3314. return 0;
  3315. }
  3316. if (strcmp(t->err, t->expected_err) != 0) {
  3317. TEST_info("%s:%d: Expected %s got %s",
  3318. t->s.test_file, t->s.start, t->expected_err, t->err);
  3319. return 0;
  3320. }
  3321. if (t->reason == NULL)
  3322. return 1;
  3323. if (t->reason == NULL) {
  3324. TEST_info("%s:%d: Test is missing function or reason code",
  3325. t->s.test_file, t->s.start);
  3326. return 0;
  3327. }
  3328. err = ERR_peek_error();
  3329. if (err == 0) {
  3330. TEST_info("%s:%d: Expected error \"%s\" not set",
  3331. t->s.test_file, t->s.start, t->reason);
  3332. return 0;
  3333. }
  3334. reason = ERR_reason_error_string(err);
  3335. if (reason == NULL) {
  3336. TEST_info("%s:%d: Expected error \"%s\", no strings available."
  3337. " Assuming ok.",
  3338. t->s.test_file, t->s.start, t->reason);
  3339. return 1;
  3340. }
  3341. if (strcmp(reason, t->reason) == 0)
  3342. return 1;
  3343. TEST_info("%s:%d: Expected error \"%s\", got \"%s\"",
  3344. t->s.test_file, t->s.start, t->reason, reason);
  3345. return 0;
  3346. }
  3347. /* Run a parsed test. Log a message and return 0 on error. */
  3348. static int run_test(EVP_TEST *t)
  3349. {
  3350. if (t->meth == NULL)
  3351. return 1;
  3352. t->s.numtests++;
  3353. if (t->skip) {
  3354. t->s.numskip++;
  3355. } else {
  3356. /* run the test */
  3357. if (t->err == NULL && t->meth->run_test(t) != 1) {
  3358. TEST_info("%s:%d %s error",
  3359. t->s.test_file, t->s.start, t->meth->name);
  3360. return 0;
  3361. }
  3362. if (!check_test_error(t)) {
  3363. TEST_openssl_errors();
  3364. t->s.errors++;
  3365. }
  3366. }
  3367. /* clean it up */
  3368. return 1;
  3369. }
  3370. static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
  3371. {
  3372. for (; lst != NULL; lst = lst->next) {
  3373. if (strcmp(lst->name, name) == 0) {
  3374. if (ppk != NULL)
  3375. *ppk = lst->key;
  3376. return 1;
  3377. }
  3378. }
  3379. return 0;
  3380. }
  3381. static void free_key_list(KEY_LIST *lst)
  3382. {
  3383. while (lst != NULL) {
  3384. KEY_LIST *next = lst->next;
  3385. EVP_PKEY_free(lst->key);
  3386. OPENSSL_free(lst->name);
  3387. OPENSSL_free(lst);
  3388. lst = next;
  3389. }
  3390. }
  3391. /*
  3392. * Is the key type an unsupported algorithm?
  3393. */
  3394. static int key_unsupported(void)
  3395. {
  3396. long err = ERR_peek_last_error();
  3397. int lib = ERR_GET_LIB(err);
  3398. long reason = ERR_GET_REASON(err);
  3399. if ((lib == ERR_LIB_EVP && reason == EVP_R_UNSUPPORTED_ALGORITHM)
  3400. || (lib == ERR_LIB_EVP && reason == EVP_R_DECODE_ERROR)
  3401. || reason == ERR_R_UNSUPPORTED) {
  3402. ERR_clear_error();
  3403. return 1;
  3404. }
  3405. #ifndef OPENSSL_NO_EC
  3406. /*
  3407. * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
  3408. * hint to an unsupported algorithm/curve (e.g. if binary EC support is
  3409. * disabled).
  3410. */
  3411. if (lib == ERR_LIB_EC
  3412. && (reason == EC_R_UNKNOWN_GROUP
  3413. || reason == EC_R_INVALID_CURVE)) {
  3414. ERR_clear_error();
  3415. return 1;
  3416. }
  3417. #endif /* OPENSSL_NO_EC */
  3418. return 0;
  3419. }
  3420. /* NULL out the value from |pp| but return it. This "steals" a pointer. */
  3421. static char *take_value(PAIR *pp)
  3422. {
  3423. char *p = pp->value;
  3424. pp->value = NULL;
  3425. return p;
  3426. }
  3427. #if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
  3428. static int securitycheck_enabled(void)
  3429. {
  3430. static int enabled = -1;
  3431. if (enabled == -1) {
  3432. if (OSSL_PROVIDER_available(libctx, "fips")) {
  3433. OSSL_PARAM params[2];
  3434. OSSL_PROVIDER *prov = NULL;
  3435. int check = 1;
  3436. prov = OSSL_PROVIDER_load(libctx, "fips");
  3437. if (prov != NULL) {
  3438. params[0] =
  3439. OSSL_PARAM_construct_int(OSSL_PROV_PARAM_SECURITY_CHECKS,
  3440. &check);
  3441. params[1] = OSSL_PARAM_construct_end();
  3442. OSSL_PROVIDER_get_params(prov, params);
  3443. OSSL_PROVIDER_unload(prov);
  3444. }
  3445. enabled = check;
  3446. return enabled;
  3447. }
  3448. enabled = 0;
  3449. }
  3450. return enabled;
  3451. }
  3452. #endif
  3453. /*
  3454. * Return 1 if one of the providers named in the string is available.
  3455. * The provider names are separated with whitespace.
  3456. * NOTE: destructive function, it inserts '\0' after each provider name.
  3457. */
  3458. static int prov_available(char *providers)
  3459. {
  3460. char *p;
  3461. int more = 1;
  3462. while (more) {
  3463. for (; isspace((unsigned char)(*providers)); providers++)
  3464. continue;
  3465. if (*providers == '\0')
  3466. break; /* End of the road */
  3467. for (p = providers; *p != '\0' && !isspace((unsigned char)(*p)); p++)
  3468. continue;
  3469. if (*p == '\0')
  3470. more = 0;
  3471. else
  3472. *p = '\0';
  3473. if (OSSL_PROVIDER_available(libctx, providers))
  3474. return 1; /* Found one */
  3475. }
  3476. return 0;
  3477. }
  3478. /* Read and parse one test. Return 0 if failure, 1 if okay. */
  3479. static int parse(EVP_TEST *t)
  3480. {
  3481. KEY_LIST *key, **klist;
  3482. EVP_PKEY *pkey;
  3483. PAIR *pp;
  3484. int i, j, skipped = 0;
  3485. top:
  3486. do {
  3487. if (BIO_eof(t->s.fp))
  3488. return EOF;
  3489. clear_test(t);
  3490. if (!test_readstanza(&t->s))
  3491. return 0;
  3492. } while (t->s.numpairs == 0);
  3493. pp = &t->s.pairs[0];
  3494. /* Are we adding a key? */
  3495. klist = NULL;
  3496. pkey = NULL;
  3497. start:
  3498. if (strcmp(pp->key, "PrivateKey") == 0) {
  3499. pkey = PEM_read_bio_PrivateKey_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
  3500. if (pkey == NULL && !key_unsupported()) {
  3501. EVP_PKEY_free(pkey);
  3502. TEST_info("Can't read private key %s", pp->value);
  3503. TEST_openssl_errors();
  3504. return 0;
  3505. }
  3506. klist = &private_keys;
  3507. } else if (strcmp(pp->key, "PublicKey") == 0) {
  3508. pkey = PEM_read_bio_PUBKEY_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
  3509. if (pkey == NULL && !key_unsupported()) {
  3510. EVP_PKEY_free(pkey);
  3511. TEST_info("Can't read public key %s", pp->value);
  3512. TEST_openssl_errors();
  3513. return 0;
  3514. }
  3515. klist = &public_keys;
  3516. } else if (strcmp(pp->key, "PrivateKeyRaw") == 0
  3517. || strcmp(pp->key, "PublicKeyRaw") == 0) {
  3518. char *strnid = NULL, *keydata = NULL;
  3519. unsigned char *keybin;
  3520. size_t keylen;
  3521. int nid;
  3522. if (strcmp(pp->key, "PrivateKeyRaw") == 0)
  3523. klist = &private_keys;
  3524. else
  3525. klist = &public_keys;
  3526. strnid = strchr(pp->value, ':');
  3527. if (strnid != NULL) {
  3528. *strnid++ = '\0';
  3529. keydata = strchr(strnid, ':');
  3530. if (keydata != NULL)
  3531. *keydata++ = '\0';
  3532. }
  3533. if (keydata == NULL) {
  3534. TEST_info("Failed to parse %s value", pp->key);
  3535. return 0;
  3536. }
  3537. nid = OBJ_txt2nid(strnid);
  3538. if (nid == NID_undef) {
  3539. TEST_info("Unrecognised algorithm NID");
  3540. return 0;
  3541. }
  3542. if (!parse_bin(keydata, &keybin, &keylen)) {
  3543. TEST_info("Failed to create binary key");
  3544. return 0;
  3545. }
  3546. if (klist == &private_keys)
  3547. pkey = EVP_PKEY_new_raw_private_key_ex(libctx, strnid, NULL, keybin,
  3548. keylen);
  3549. else
  3550. pkey = EVP_PKEY_new_raw_public_key_ex(libctx, strnid, NULL, keybin,
  3551. keylen);
  3552. if (pkey == NULL && !key_unsupported()) {
  3553. TEST_info("Can't read %s data", pp->key);
  3554. OPENSSL_free(keybin);
  3555. TEST_openssl_errors();
  3556. return 0;
  3557. }
  3558. OPENSSL_free(keybin);
  3559. } else if (strcmp(pp->key, "Availablein") == 0) {
  3560. if (!prov_available(pp->value)) {
  3561. TEST_info("skipping, '%s' provider not available: %s:%d",
  3562. pp->value, t->s.test_file, t->s.start);
  3563. t->skip = 1;
  3564. return 0;
  3565. }
  3566. skipped++;
  3567. pp++;
  3568. goto start;
  3569. } else if (strcmp(pp->key, "FIPSversion") == 0) {
  3570. if (prov_available("fips")) {
  3571. j = fips_provider_version_match(libctx, pp->value);
  3572. if (j < 0) {
  3573. TEST_info("Line %d: error matching FIPS versions\n", t->s.curr);
  3574. return 0;
  3575. } else if (j == 0) {
  3576. TEST_info("skipping, FIPS provider incompatible version: %s:%d",
  3577. t->s.test_file, t->s.start);
  3578. t->skip = 1;
  3579. return 0;
  3580. }
  3581. }
  3582. skipped++;
  3583. pp++;
  3584. goto start;
  3585. }
  3586. /* If we have a key add to list */
  3587. if (klist != NULL) {
  3588. if (find_key(NULL, pp->value, *klist)) {
  3589. TEST_info("Duplicate key %s", pp->value);
  3590. return 0;
  3591. }
  3592. if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
  3593. return 0;
  3594. key->name = take_value(pp);
  3595. key->key = pkey;
  3596. key->next = *klist;
  3597. *klist = key;
  3598. /* Go back and start a new stanza. */
  3599. if ((t->s.numpairs - skipped) != 1)
  3600. TEST_info("Line %d: missing blank line\n", t->s.curr);
  3601. goto top;
  3602. }
  3603. /* Find the test, based on first keyword. */
  3604. if (!TEST_ptr(t->meth = find_test(pp->key)))
  3605. return 0;
  3606. if (!t->meth->init(t, pp->value)) {
  3607. TEST_error("unknown %s: %s\n", pp->key, pp->value);
  3608. return 0;
  3609. }
  3610. if (t->skip == 1) {
  3611. /* TEST_info("skipping %s %s", pp->key, pp->value); */
  3612. return 0;
  3613. }
  3614. for (pp++, i = 1; i < (t->s.numpairs - skipped); pp++, i++) {
  3615. if (strcmp(pp->key, "Securitycheck") == 0) {
  3616. #if defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
  3617. #else
  3618. if (!securitycheck_enabled())
  3619. #endif
  3620. {
  3621. TEST_info("skipping, Securitycheck is disabled: %s:%d",
  3622. t->s.test_file, t->s.start);
  3623. t->skip = 1;
  3624. return 0;
  3625. }
  3626. } else if (strcmp(pp->key, "Availablein") == 0) {
  3627. TEST_info("Line %d: 'Availablein' should be the first option",
  3628. t->s.curr);
  3629. return 0;
  3630. } else if (strcmp(pp->key, "Result") == 0) {
  3631. if (t->expected_err != NULL) {
  3632. TEST_info("Line %d: multiple result lines", t->s.curr);
  3633. return 0;
  3634. }
  3635. t->expected_err = take_value(pp);
  3636. } else if (strcmp(pp->key, "Function") == 0) {
  3637. /* Ignore old line. */
  3638. } else if (strcmp(pp->key, "Reason") == 0) {
  3639. if (t->reason != NULL) {
  3640. TEST_info("Line %d: multiple reason lines", t->s.curr);
  3641. return 0;
  3642. }
  3643. t->reason = take_value(pp);
  3644. } else if (strcmp(pp->key, "Threads") == 0) {
  3645. if (OSSL_set_max_threads(libctx, atoi(pp->value)) == 0) {
  3646. TEST_info("skipping, '%s' threads not available: %s:%d",
  3647. pp->value, t->s.test_file, t->s.start);
  3648. t->skip = 1;
  3649. }
  3650. } else {
  3651. /* Must be test specific line: try to parse it */
  3652. int rv = t->meth->parse(t, pp->key, pp->value);
  3653. if (rv == 0) {
  3654. TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key);
  3655. return 0;
  3656. }
  3657. if (rv < 0) {
  3658. TEST_info("Line %d: error processing keyword %s = %s\n",
  3659. t->s.curr, pp->key, pp->value);
  3660. return 0;
  3661. }
  3662. if (t->skip)
  3663. return 0;
  3664. }
  3665. }
  3666. return 1;
  3667. }
  3668. static int run_file_tests(int i)
  3669. {
  3670. EVP_TEST *t;
  3671. const char *testfile = test_get_argument(i);
  3672. int c;
  3673. if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t))))
  3674. return 0;
  3675. if (!test_start_file(&t->s, testfile)) {
  3676. OPENSSL_free(t);
  3677. return 0;
  3678. }
  3679. while (!BIO_eof(t->s.fp)) {
  3680. c = parse(t);
  3681. if (t->skip) {
  3682. t->s.numskip++;
  3683. continue;
  3684. }
  3685. if (c == 0 || !run_test(t)) {
  3686. t->s.errors++;
  3687. break;
  3688. }
  3689. }
  3690. test_end_file(&t->s);
  3691. clear_test(t);
  3692. free_key_list(public_keys);
  3693. free_key_list(private_keys);
  3694. BIO_free(t->s.key);
  3695. c = t->s.errors;
  3696. OPENSSL_free(t);
  3697. return c == 0;
  3698. }
  3699. const OPTIONS *test_get_options(void)
  3700. {
  3701. static const OPTIONS test_options[] = {
  3702. OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
  3703. { "config", OPT_CONFIG_FILE, '<',
  3704. "The configuration file to use for the libctx" },
  3705. { "process", OPT_IN_PLACE, 's',
  3706. "Mode for data processing by cipher tests [in_place/both], both by default"},
  3707. { "provider", OPT_PROVIDER_NAME, 's',
  3708. "The provider to load (when no configuration file, the default value is 'default')" },
  3709. { "propquery", OPT_PROV_PROPQUERY, 's',
  3710. "Property query used when fetching algorithms" },
  3711. { OPT_HELP_STR, 1, '-', "file\tFile to run tests on.\n" },
  3712. { NULL }
  3713. };
  3714. return test_options;
  3715. }
  3716. int setup_tests(void)
  3717. {
  3718. size_t n;
  3719. char *config_file = NULL;
  3720. char *provider_name = NULL;
  3721. OPTION_CHOICE o;
  3722. while ((o = opt_next()) != OPT_EOF) {
  3723. switch (o) {
  3724. case OPT_CONFIG_FILE:
  3725. config_file = opt_arg();
  3726. break;
  3727. case OPT_IN_PLACE:
  3728. if ((process_mode_in_place = evp_test_process_mode(opt_arg())) == -1)
  3729. return 0;
  3730. break;
  3731. case OPT_PROVIDER_NAME:
  3732. provider_name = opt_arg();
  3733. break;
  3734. case OPT_PROV_PROPQUERY:
  3735. propquery = opt_arg();
  3736. break;
  3737. case OPT_TEST_CASES:
  3738. break;
  3739. default:
  3740. case OPT_ERR:
  3741. return 0;
  3742. }
  3743. }
  3744. /*
  3745. * Load the provider via configuration into the created library context.
  3746. * Load the 'null' provider into the default library context to ensure that
  3747. * the tests do not fallback to using the default provider.
  3748. */
  3749. if (config_file == NULL && provider_name == NULL)
  3750. provider_name = "default";
  3751. if (!test_get_libctx(&libctx, &prov_null, config_file, &libprov, provider_name))
  3752. return 0;
  3753. n = test_get_argument_count();
  3754. if (n == 0)
  3755. return 0;
  3756. ADD_ALL_TESTS(run_file_tests, n);
  3757. return 1;
  3758. }
  3759. void cleanup_tests(void)
  3760. {
  3761. OSSL_PROVIDER_unload(libprov);
  3762. OSSL_PROVIDER_unload(prov_null);
  3763. OSSL_LIB_CTX_free(libctx);
  3764. }
  3765. static int is_digest_disabled(const char *name)
  3766. {
  3767. #ifdef OPENSSL_NO_BLAKE2
  3768. if (HAS_CASE_PREFIX(name, "BLAKE"))
  3769. return 1;
  3770. #endif
  3771. #ifdef OPENSSL_NO_MD2
  3772. if (OPENSSL_strcasecmp(name, "MD2") == 0)
  3773. return 1;
  3774. #endif
  3775. #ifdef OPENSSL_NO_MDC2
  3776. if (OPENSSL_strcasecmp(name, "MDC2") == 0)
  3777. return 1;
  3778. #endif
  3779. #ifdef OPENSSL_NO_MD4
  3780. if (OPENSSL_strcasecmp(name, "MD4") == 0)
  3781. return 1;
  3782. #endif
  3783. #ifdef OPENSSL_NO_MD5
  3784. if (OPENSSL_strcasecmp(name, "MD5") == 0)
  3785. return 1;
  3786. #endif
  3787. #ifdef OPENSSL_NO_RMD160
  3788. if (OPENSSL_strcasecmp(name, "RIPEMD160") == 0)
  3789. return 1;
  3790. #endif
  3791. #ifdef OPENSSL_NO_SM3
  3792. if (OPENSSL_strcasecmp(name, "SM3") == 0)
  3793. return 1;
  3794. #endif
  3795. #ifdef OPENSSL_NO_WHIRLPOOL
  3796. if (OPENSSL_strcasecmp(name, "WHIRLPOOL") == 0)
  3797. return 1;
  3798. #endif
  3799. return 0;
  3800. }
  3801. static int is_pkey_disabled(const char *name)
  3802. {
  3803. #ifdef OPENSSL_NO_EC
  3804. if (HAS_CASE_PREFIX(name, "EC"))
  3805. return 1;
  3806. #endif
  3807. #ifdef OPENSSL_NO_DH
  3808. if (HAS_CASE_PREFIX(name, "DH"))
  3809. return 1;
  3810. #endif
  3811. #ifdef OPENSSL_NO_DSA
  3812. if (HAS_CASE_PREFIX(name, "DSA"))
  3813. return 1;
  3814. #endif
  3815. return 0;
  3816. }
  3817. static int is_mac_disabled(const char *name)
  3818. {
  3819. #ifdef OPENSSL_NO_BLAKE2
  3820. if (HAS_CASE_PREFIX(name, "BLAKE2BMAC")
  3821. || HAS_CASE_PREFIX(name, "BLAKE2SMAC"))
  3822. return 1;
  3823. #endif
  3824. #ifdef OPENSSL_NO_CMAC
  3825. if (HAS_CASE_PREFIX(name, "CMAC"))
  3826. return 1;
  3827. #endif
  3828. #ifdef OPENSSL_NO_POLY1305
  3829. if (HAS_CASE_PREFIX(name, "Poly1305"))
  3830. return 1;
  3831. #endif
  3832. #ifdef OPENSSL_NO_SIPHASH
  3833. if (HAS_CASE_PREFIX(name, "SipHash"))
  3834. return 1;
  3835. #endif
  3836. return 0;
  3837. }
  3838. static int is_kdf_disabled(const char *name)
  3839. {
  3840. #ifdef OPENSSL_NO_SCRYPT
  3841. if (HAS_CASE_SUFFIX(name, "SCRYPT"))
  3842. return 1;
  3843. #endif
  3844. #ifdef OPENSSL_NO_ARGON2
  3845. if (HAS_CASE_SUFFIX(name, "ARGON2"))
  3846. return 1;
  3847. #endif
  3848. return 0;
  3849. }
  3850. static int is_cipher_disabled(const char *name)
  3851. {
  3852. #ifdef OPENSSL_NO_ARIA
  3853. if (HAS_CASE_PREFIX(name, "ARIA"))
  3854. return 1;
  3855. #endif
  3856. #ifdef OPENSSL_NO_BF
  3857. if (HAS_CASE_PREFIX(name, "BF"))
  3858. return 1;
  3859. #endif
  3860. #ifdef OPENSSL_NO_CAMELLIA
  3861. if (HAS_CASE_PREFIX(name, "CAMELLIA"))
  3862. return 1;
  3863. #endif
  3864. #ifdef OPENSSL_NO_CAST
  3865. if (HAS_CASE_PREFIX(name, "CAST"))
  3866. return 1;
  3867. #endif
  3868. #ifdef OPENSSL_NO_CHACHA
  3869. if (HAS_CASE_PREFIX(name, "CHACHA"))
  3870. return 1;
  3871. #endif
  3872. #ifdef OPENSSL_NO_POLY1305
  3873. if (HAS_CASE_SUFFIX(name, "Poly1305"))
  3874. return 1;
  3875. #endif
  3876. #ifdef OPENSSL_NO_DES
  3877. if (HAS_CASE_PREFIX(name, "DES"))
  3878. return 1;
  3879. if (HAS_CASE_SUFFIX(name, "3DESwrap"))
  3880. return 1;
  3881. #endif
  3882. #ifdef OPENSSL_NO_OCB
  3883. if (HAS_CASE_SUFFIX(name, "OCB"))
  3884. return 1;
  3885. #endif
  3886. #ifdef OPENSSL_NO_IDEA
  3887. if (HAS_CASE_PREFIX(name, "IDEA"))
  3888. return 1;
  3889. #endif
  3890. #ifdef OPENSSL_NO_RC2
  3891. if (HAS_CASE_PREFIX(name, "RC2"))
  3892. return 1;
  3893. #endif
  3894. #ifdef OPENSSL_NO_RC4
  3895. if (HAS_CASE_PREFIX(name, "RC4"))
  3896. return 1;
  3897. #endif
  3898. #ifdef OPENSSL_NO_RC5
  3899. if (HAS_CASE_PREFIX(name, "RC5"))
  3900. return 1;
  3901. #endif
  3902. #ifdef OPENSSL_NO_SEED
  3903. if (HAS_CASE_PREFIX(name, "SEED"))
  3904. return 1;
  3905. #endif
  3906. #ifdef OPENSSL_NO_SIV
  3907. if (HAS_CASE_SUFFIX(name, "SIV"))
  3908. return 1;
  3909. #endif
  3910. #ifdef OPENSSL_NO_SM4
  3911. if (HAS_CASE_PREFIX(name, "SM4"))
  3912. return 1;
  3913. #endif
  3914. return 0;
  3915. }