evp_test.c 107 KB

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