evp_test.c 82 KB

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