evp_test.c 127 KB

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