evp_test.c 105 KB

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