evp_test.c 100 KB

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