evp_test.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047
  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. if (expected->keyop(expected->ctx, NULL, &got_len,
  1250. expected->input, expected->input_len) <= 0
  1251. || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
  1252. t->err = "KEYOP_LENGTH_ERROR";
  1253. goto err;
  1254. }
  1255. if (expected->keyop(expected->ctx, got, &got_len,
  1256. expected->input, expected->input_len) <= 0) {
  1257. t->err = "KEYOP_ERROR";
  1258. goto err;
  1259. }
  1260. if (!memory_err_compare(t, "KEYOP_MISMATCH",
  1261. expected->output, expected->output_len,
  1262. got, got_len))
  1263. goto err;
  1264. t->err = NULL;
  1265. err:
  1266. OPENSSL_free(got);
  1267. return 1;
  1268. }
  1269. static int sign_test_init(EVP_TEST *t, const char *name)
  1270. {
  1271. return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
  1272. }
  1273. static const EVP_TEST_METHOD psign_test_method = {
  1274. "Sign",
  1275. sign_test_init,
  1276. pkey_test_cleanup,
  1277. pkey_test_parse,
  1278. pkey_test_run
  1279. };
  1280. static int verify_recover_test_init(EVP_TEST *t, const char *name)
  1281. {
  1282. return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
  1283. EVP_PKEY_verify_recover);
  1284. }
  1285. static const EVP_TEST_METHOD pverify_recover_test_method = {
  1286. "VerifyRecover",
  1287. verify_recover_test_init,
  1288. pkey_test_cleanup,
  1289. pkey_test_parse,
  1290. pkey_test_run
  1291. };
  1292. static int decrypt_test_init(EVP_TEST *t, const char *name)
  1293. {
  1294. return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
  1295. EVP_PKEY_decrypt);
  1296. }
  1297. static const EVP_TEST_METHOD pdecrypt_test_method = {
  1298. "Decrypt",
  1299. decrypt_test_init,
  1300. pkey_test_cleanup,
  1301. pkey_test_parse,
  1302. pkey_test_run
  1303. };
  1304. static int verify_test_init(EVP_TEST *t, const char *name)
  1305. {
  1306. return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
  1307. }
  1308. static int verify_test_run(EVP_TEST *t)
  1309. {
  1310. PKEY_DATA *kdata = t->data;
  1311. if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
  1312. kdata->input, kdata->input_len) <= 0)
  1313. t->err = "VERIFY_ERROR";
  1314. return 1;
  1315. }
  1316. static const EVP_TEST_METHOD pverify_test_method = {
  1317. "Verify",
  1318. verify_test_init,
  1319. pkey_test_cleanup,
  1320. pkey_test_parse,
  1321. verify_test_run
  1322. };
  1323. static int pderive_test_init(EVP_TEST *t, const char *name)
  1324. {
  1325. return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
  1326. }
  1327. static int pderive_test_parse(EVP_TEST *t,
  1328. const char *keyword, const char *value)
  1329. {
  1330. PKEY_DATA *kdata = t->data;
  1331. if (strcmp(keyword, "PeerKey") == 0) {
  1332. EVP_PKEY *peer;
  1333. if (find_key(&peer, value, public_keys) == 0)
  1334. return 0;
  1335. if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
  1336. return 0;
  1337. return 1;
  1338. }
  1339. if (strcmp(keyword, "SharedSecret") == 0)
  1340. return parse_bin(value, &kdata->output, &kdata->output_len);
  1341. if (strcmp(keyword, "Ctrl") == 0)
  1342. return pkey_test_ctrl(t, kdata->ctx, value);
  1343. return 0;
  1344. }
  1345. static int pderive_test_run(EVP_TEST *t)
  1346. {
  1347. PKEY_DATA *expected = t->data;
  1348. unsigned char *got = NULL;
  1349. size_t got_len;
  1350. if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) {
  1351. t->err = "DERIVE_ERROR";
  1352. goto err;
  1353. }
  1354. if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
  1355. t->err = "DERIVE_ERROR";
  1356. goto err;
  1357. }
  1358. if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
  1359. t->err = "DERIVE_ERROR";
  1360. goto err;
  1361. }
  1362. if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH",
  1363. expected->output, expected->output_len,
  1364. got, got_len))
  1365. goto err;
  1366. t->err = NULL;
  1367. err:
  1368. OPENSSL_free(got);
  1369. return 1;
  1370. }
  1371. static const EVP_TEST_METHOD pderive_test_method = {
  1372. "Derive",
  1373. pderive_test_init,
  1374. pkey_test_cleanup,
  1375. pderive_test_parse,
  1376. pderive_test_run
  1377. };
  1378. /**
  1379. *** PBE TESTS
  1380. **/
  1381. typedef enum pbe_type_enum {
  1382. PBE_TYPE_INVALID = 0,
  1383. PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
  1384. } PBE_TYPE;
  1385. typedef struct pbe_data_st {
  1386. PBE_TYPE pbe_type;
  1387. /* scrypt parameters */
  1388. uint64_t N, r, p, maxmem;
  1389. /* PKCS#12 parameters */
  1390. int id, iter;
  1391. const EVP_MD *md;
  1392. /* password */
  1393. unsigned char *pass;
  1394. size_t pass_len;
  1395. /* salt */
  1396. unsigned char *salt;
  1397. size_t salt_len;
  1398. /* Expected output */
  1399. unsigned char *key;
  1400. size_t key_len;
  1401. } PBE_DATA;
  1402. #ifndef OPENSSL_NO_SCRYPT
  1403. /*
  1404. * Parse unsigned decimal 64 bit integer value
  1405. */
  1406. static int parse_uint64(const char *value, uint64_t *pr)
  1407. {
  1408. const char *p = value;
  1409. if (!TEST_true(*p)) {
  1410. TEST_info("Invalid empty integer value");
  1411. return -1;
  1412. }
  1413. for (*pr = 0; *p; ) {
  1414. if (*pr > UINT64_MAX / 10) {
  1415. TEST_error("Integer overflow in string %s", value);
  1416. return -1;
  1417. }
  1418. *pr *= 10;
  1419. if (!TEST_true(isdigit((unsigned char)*p))) {
  1420. TEST_error("Invalid character in string %s", value);
  1421. return -1;
  1422. }
  1423. *pr += *p - '0';
  1424. p++;
  1425. }
  1426. return 1;
  1427. }
  1428. static int scrypt_test_parse(EVP_TEST *t,
  1429. const char *keyword, const char *value)
  1430. {
  1431. PBE_DATA *pdata = t->data;
  1432. if (strcmp(keyword, "N") == 0)
  1433. return parse_uint64(value, &pdata->N);
  1434. if (strcmp(keyword, "p") == 0)
  1435. return parse_uint64(value, &pdata->p);
  1436. if (strcmp(keyword, "r") == 0)
  1437. return parse_uint64(value, &pdata->r);
  1438. if (strcmp(keyword, "maxmem") == 0)
  1439. return parse_uint64(value, &pdata->maxmem);
  1440. return 0;
  1441. }
  1442. #endif
  1443. static int pbkdf2_test_parse(EVP_TEST *t,
  1444. const char *keyword, const char *value)
  1445. {
  1446. PBE_DATA *pdata = t->data;
  1447. if (strcmp(keyword, "iter") == 0) {
  1448. pdata->iter = atoi(value);
  1449. if (pdata->iter <= 0)
  1450. return -1;
  1451. return 1;
  1452. }
  1453. if (strcmp(keyword, "MD") == 0) {
  1454. pdata->md = EVP_get_digestbyname(value);
  1455. if (pdata->md == NULL)
  1456. return -1;
  1457. return 1;
  1458. }
  1459. return 0;
  1460. }
  1461. static int pkcs12_test_parse(EVP_TEST *t,
  1462. const char *keyword, const char *value)
  1463. {
  1464. PBE_DATA *pdata = t->data;
  1465. if (strcmp(keyword, "id") == 0) {
  1466. pdata->id = atoi(value);
  1467. if (pdata->id <= 0)
  1468. return -1;
  1469. return 1;
  1470. }
  1471. return pbkdf2_test_parse(t, keyword, value);
  1472. }
  1473. static int pbe_test_init(EVP_TEST *t, const char *alg)
  1474. {
  1475. PBE_DATA *pdat;
  1476. PBE_TYPE pbe_type = PBE_TYPE_INVALID;
  1477. if (strcmp(alg, "scrypt") == 0) {
  1478. #ifndef OPENSSL_NO_SCRYPT
  1479. pbe_type = PBE_TYPE_SCRYPT;
  1480. #else
  1481. t->skip = 1;
  1482. return 1;
  1483. #endif
  1484. } else if (strcmp(alg, "pbkdf2") == 0) {
  1485. pbe_type = PBE_TYPE_PBKDF2;
  1486. } else if (strcmp(alg, "pkcs12") == 0) {
  1487. pbe_type = PBE_TYPE_PKCS12;
  1488. } else {
  1489. TEST_error("Unknown pbe algorithm %s", alg);
  1490. }
  1491. pdat = OPENSSL_zalloc(sizeof(*pdat));
  1492. pdat->pbe_type = pbe_type;
  1493. t->data = pdat;
  1494. return 1;
  1495. }
  1496. static void pbe_test_cleanup(EVP_TEST *t)
  1497. {
  1498. PBE_DATA *pdat = t->data;
  1499. OPENSSL_free(pdat->pass);
  1500. OPENSSL_free(pdat->salt);
  1501. OPENSSL_free(pdat->key);
  1502. }
  1503. static int pbe_test_parse(EVP_TEST *t,
  1504. const char *keyword, const char *value)
  1505. {
  1506. PBE_DATA *pdata = t->data;
  1507. if (strcmp(keyword, "Password") == 0)
  1508. return parse_bin(value, &pdata->pass, &pdata->pass_len);
  1509. if (strcmp(keyword, "Salt") == 0)
  1510. return parse_bin(value, &pdata->salt, &pdata->salt_len);
  1511. if (strcmp(keyword, "Key") == 0)
  1512. return parse_bin(value, &pdata->key, &pdata->key_len);
  1513. if (pdata->pbe_type == PBE_TYPE_PBKDF2)
  1514. return pbkdf2_test_parse(t, keyword, value);
  1515. else if (pdata->pbe_type == PBE_TYPE_PKCS12)
  1516. return pkcs12_test_parse(t, keyword, value);
  1517. #ifndef OPENSSL_NO_SCRYPT
  1518. else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
  1519. return scrypt_test_parse(t, keyword, value);
  1520. #endif
  1521. return 0;
  1522. }
  1523. static int pbe_test_run(EVP_TEST *t)
  1524. {
  1525. PBE_DATA *expected = t->data;
  1526. unsigned char *key;
  1527. if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
  1528. t->err = "INTERNAL_ERROR";
  1529. goto err;
  1530. }
  1531. if (expected->pbe_type == PBE_TYPE_PBKDF2) {
  1532. if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
  1533. expected->salt, expected->salt_len,
  1534. expected->iter, expected->md,
  1535. expected->key_len, key) == 0) {
  1536. t->err = "PBKDF2_ERROR";
  1537. goto err;
  1538. }
  1539. #ifndef OPENSSL_NO_SCRYPT
  1540. } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
  1541. if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
  1542. expected->salt, expected->salt_len, expected->N,
  1543. expected->r, expected->p, expected->maxmem,
  1544. key, expected->key_len) == 0) {
  1545. t->err = "SCRYPT_ERROR";
  1546. goto err;
  1547. }
  1548. #endif
  1549. } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
  1550. if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
  1551. expected->salt, expected->salt_len,
  1552. expected->id, expected->iter, expected->key_len,
  1553. key, expected->md) == 0) {
  1554. t->err = "PKCS12_ERROR";
  1555. goto err;
  1556. }
  1557. }
  1558. if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len,
  1559. key, expected->key_len))
  1560. goto err;
  1561. t->err = NULL;
  1562. err:
  1563. OPENSSL_free(key);
  1564. return 1;
  1565. }
  1566. static const EVP_TEST_METHOD pbe_test_method = {
  1567. "PBE",
  1568. pbe_test_init,
  1569. pbe_test_cleanup,
  1570. pbe_test_parse,
  1571. pbe_test_run
  1572. };
  1573. /**
  1574. *** BASE64 TESTS
  1575. **/
  1576. typedef enum {
  1577. BASE64_CANONICAL_ENCODING = 0,
  1578. BASE64_VALID_ENCODING = 1,
  1579. BASE64_INVALID_ENCODING = 2
  1580. } base64_encoding_type;
  1581. typedef struct encode_data_st {
  1582. /* Input to encoding */
  1583. unsigned char *input;
  1584. size_t input_len;
  1585. /* Expected output */
  1586. unsigned char *output;
  1587. size_t output_len;
  1588. base64_encoding_type encoding;
  1589. } ENCODE_DATA;
  1590. static int encode_test_init(EVP_TEST *t, const char *encoding)
  1591. {
  1592. ENCODE_DATA *edata;
  1593. if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
  1594. return 0;
  1595. if (strcmp(encoding, "canonical") == 0) {
  1596. edata->encoding = BASE64_CANONICAL_ENCODING;
  1597. } else if (strcmp(encoding, "valid") == 0) {
  1598. edata->encoding = BASE64_VALID_ENCODING;
  1599. } else if (strcmp(encoding, "invalid") == 0) {
  1600. edata->encoding = BASE64_INVALID_ENCODING;
  1601. if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
  1602. goto err;
  1603. } else {
  1604. TEST_error("Bad encoding: %s."
  1605. " Should be one of {canonical, valid, invalid}",
  1606. encoding);
  1607. goto err;
  1608. }
  1609. t->data = edata;
  1610. return 1;
  1611. err:
  1612. OPENSSL_free(edata);
  1613. return 0;
  1614. }
  1615. static void encode_test_cleanup(EVP_TEST *t)
  1616. {
  1617. ENCODE_DATA *edata = t->data;
  1618. OPENSSL_free(edata->input);
  1619. OPENSSL_free(edata->output);
  1620. memset(edata, 0, sizeof(*edata));
  1621. }
  1622. static int encode_test_parse(EVP_TEST *t,
  1623. const char *keyword, const char *value)
  1624. {
  1625. ENCODE_DATA *edata = t->data;
  1626. if (strcmp(keyword, "Input") == 0)
  1627. return parse_bin(value, &edata->input, &edata->input_len);
  1628. if (strcmp(keyword, "Output") == 0)
  1629. return parse_bin(value, &edata->output, &edata->output_len);
  1630. return 0;
  1631. }
  1632. static int encode_test_run(EVP_TEST *t)
  1633. {
  1634. ENCODE_DATA *expected = t->data;
  1635. unsigned char *encode_out = NULL, *decode_out = NULL;
  1636. int output_len, chunk_len;
  1637. EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL;
  1638. if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
  1639. t->err = "INTERNAL_ERROR";
  1640. goto err;
  1641. }
  1642. if (expected->encoding == BASE64_CANONICAL_ENCODING) {
  1643. if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
  1644. || !TEST_ptr(encode_out =
  1645. OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
  1646. goto err;
  1647. EVP_EncodeInit(encode_ctx);
  1648. if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
  1649. expected->input, expected->input_len)))
  1650. goto err;
  1651. output_len = chunk_len;
  1652. EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
  1653. output_len += chunk_len;
  1654. if (!memory_err_compare(t, "BAD_ENCODING",
  1655. expected->output, expected->output_len,
  1656. encode_out, output_len))
  1657. goto err;
  1658. }
  1659. if (!TEST_ptr(decode_out =
  1660. OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
  1661. goto err;
  1662. EVP_DecodeInit(decode_ctx);
  1663. if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
  1664. expected->output_len) < 0) {
  1665. t->err = "DECODE_ERROR";
  1666. goto err;
  1667. }
  1668. output_len = chunk_len;
  1669. if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
  1670. t->err = "DECODE_ERROR";
  1671. goto err;
  1672. }
  1673. output_len += chunk_len;
  1674. if (expected->encoding != BASE64_INVALID_ENCODING
  1675. && !memory_err_compare(t, "BAD_DECODING",
  1676. expected->input, expected->input_len,
  1677. decode_out, output_len)) {
  1678. t->err = "BAD_DECODING";
  1679. goto err;
  1680. }
  1681. t->err = NULL;
  1682. err:
  1683. OPENSSL_free(encode_out);
  1684. OPENSSL_free(decode_out);
  1685. EVP_ENCODE_CTX_free(decode_ctx);
  1686. EVP_ENCODE_CTX_free(encode_ctx);
  1687. return 1;
  1688. }
  1689. static const EVP_TEST_METHOD encode_test_method = {
  1690. "Encoding",
  1691. encode_test_init,
  1692. encode_test_cleanup,
  1693. encode_test_parse,
  1694. encode_test_run,
  1695. };
  1696. /**
  1697. *** KDF TESTS
  1698. **/
  1699. typedef struct kdf_data_st {
  1700. /* Context for this operation */
  1701. EVP_KDF_CTX *ctx;
  1702. /* Expected output */
  1703. unsigned char *output;
  1704. size_t output_len;
  1705. } KDF_DATA;
  1706. /*
  1707. * Perform public key operation setup: lookup key, allocated ctx and call
  1708. * the appropriate initialisation function
  1709. */
  1710. static int kdf_test_init(EVP_TEST *t, const char *name)
  1711. {
  1712. KDF_DATA *kdata;
  1713. int kdf_nid = OBJ_sn2nid(name);
  1714. #ifdef OPENSSL_NO_SCRYPT
  1715. if (strcmp(name, "scrypt") == 0) {
  1716. t->skip = 1;
  1717. return 1;
  1718. }
  1719. #endif
  1720. if (kdf_nid == NID_undef)
  1721. kdf_nid = OBJ_ln2nid(name);
  1722. if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
  1723. return 0;
  1724. kdata->ctx = EVP_KDF_CTX_new_id(kdf_nid);
  1725. if (kdata->ctx == NULL) {
  1726. OPENSSL_free(kdata);
  1727. return 0;
  1728. }
  1729. t->data = kdata;
  1730. return 1;
  1731. }
  1732. static void kdf_test_cleanup(EVP_TEST *t)
  1733. {
  1734. KDF_DATA *kdata = t->data;
  1735. OPENSSL_free(kdata->output);
  1736. EVP_KDF_CTX_free(kdata->ctx);
  1737. }
  1738. static int kdf_test_ctrl(EVP_TEST *t, EVP_KDF_CTX *kctx,
  1739. const char *value)
  1740. {
  1741. int rv;
  1742. char *p, *tmpval;
  1743. if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
  1744. return 0;
  1745. p = strchr(tmpval, ':');
  1746. if (p != NULL)
  1747. *p++ = '\0';
  1748. rv = EVP_KDF_ctrl_str(kctx, tmpval, p);
  1749. if (rv == -2) {
  1750. t->err = "KDF_CTRL_INVALID";
  1751. rv = 1;
  1752. } else if (p != NULL && rv <= 0) {
  1753. /* If p has an OID and lookup fails assume disabled algorithm */
  1754. int nid = OBJ_sn2nid(p);
  1755. if (nid == NID_undef)
  1756. nid = OBJ_ln2nid(p);
  1757. if (nid != NID_undef
  1758. && EVP_get_digestbynid(nid) == NULL
  1759. && EVP_get_cipherbynid(nid) == NULL) {
  1760. t->skip = 1;
  1761. rv = 1;
  1762. } else {
  1763. t->err = "KDF_CTRL_ERROR";
  1764. rv = 1;
  1765. }
  1766. }
  1767. OPENSSL_free(tmpval);
  1768. return rv > 0;
  1769. }
  1770. static int kdf_test_parse(EVP_TEST *t,
  1771. const char *keyword, const char *value)
  1772. {
  1773. KDF_DATA *kdata = t->data;
  1774. if (strcmp(keyword, "Output") == 0)
  1775. return parse_bin(value, &kdata->output, &kdata->output_len);
  1776. if (strncmp(keyword, "Ctrl", 4) == 0)
  1777. return kdf_test_ctrl(t, kdata->ctx, value);
  1778. return 0;
  1779. }
  1780. static int kdf_test_run(EVP_TEST *t)
  1781. {
  1782. KDF_DATA *expected = t->data;
  1783. unsigned char *got = NULL;
  1784. size_t got_len = expected->output_len;
  1785. if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
  1786. t->err = "INTERNAL_ERROR";
  1787. goto err;
  1788. }
  1789. if (EVP_KDF_derive(expected->ctx, got, got_len) <= 0) {
  1790. t->err = "KDF_DERIVE_ERROR";
  1791. goto err;
  1792. }
  1793. if (!memory_err_compare(t, "KDF_MISMATCH",
  1794. expected->output, expected->output_len,
  1795. got, got_len))
  1796. goto err;
  1797. t->err = NULL;
  1798. err:
  1799. OPENSSL_free(got);
  1800. return 1;
  1801. }
  1802. static const EVP_TEST_METHOD kdf_test_method = {
  1803. "KDF",
  1804. kdf_test_init,
  1805. kdf_test_cleanup,
  1806. kdf_test_parse,
  1807. kdf_test_run
  1808. };
  1809. /**
  1810. *** PKEY KDF TESTS
  1811. **/
  1812. typedef struct pkey_kdf_data_st {
  1813. /* Context for this operation */
  1814. EVP_PKEY_CTX *ctx;
  1815. /* Expected output */
  1816. unsigned char *output;
  1817. size_t output_len;
  1818. } PKEY_KDF_DATA;
  1819. /*
  1820. * Perform public key operation setup: lookup key, allocated ctx and call
  1821. * the appropriate initialisation function
  1822. */
  1823. static int pkey_kdf_test_init(EVP_TEST *t, const char *name)
  1824. {
  1825. PKEY_KDF_DATA *kdata;
  1826. int kdf_nid = OBJ_sn2nid(name);
  1827. #ifdef OPENSSL_NO_SCRYPT
  1828. if (strcmp(name, "scrypt") == 0) {
  1829. t->skip = 1;
  1830. return 1;
  1831. }
  1832. #endif
  1833. if (kdf_nid == NID_undef)
  1834. kdf_nid = OBJ_ln2nid(name);
  1835. if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
  1836. return 0;
  1837. kdata->ctx = EVP_PKEY_CTX_new_id(kdf_nid, NULL);
  1838. if (kdata->ctx == NULL) {
  1839. OPENSSL_free(kdata);
  1840. return 0;
  1841. }
  1842. if (EVP_PKEY_derive_init(kdata->ctx) <= 0) {
  1843. EVP_PKEY_CTX_free(kdata->ctx);
  1844. OPENSSL_free(kdata);
  1845. return 0;
  1846. }
  1847. t->data = kdata;
  1848. return 1;
  1849. }
  1850. static void pkey_kdf_test_cleanup(EVP_TEST *t)
  1851. {
  1852. PKEY_KDF_DATA *kdata = t->data;
  1853. OPENSSL_free(kdata->output);
  1854. EVP_PKEY_CTX_free(kdata->ctx);
  1855. }
  1856. static int pkey_kdf_test_parse(EVP_TEST *t,
  1857. const char *keyword, const char *value)
  1858. {
  1859. PKEY_KDF_DATA *kdata = t->data;
  1860. if (strcmp(keyword, "Output") == 0)
  1861. return parse_bin(value, &kdata->output, &kdata->output_len);
  1862. if (strncmp(keyword, "Ctrl", 4) == 0)
  1863. return pkey_test_ctrl(t, kdata->ctx, value);
  1864. return 0;
  1865. }
  1866. static int pkey_kdf_test_run(EVP_TEST *t)
  1867. {
  1868. PKEY_KDF_DATA *expected = t->data;
  1869. unsigned char *got = NULL;
  1870. size_t got_len = expected->output_len;
  1871. if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
  1872. t->err = "INTERNAL_ERROR";
  1873. goto err;
  1874. }
  1875. if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
  1876. t->err = "KDF_DERIVE_ERROR";
  1877. goto err;
  1878. }
  1879. if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
  1880. t->err = "KDF_MISMATCH";
  1881. goto err;
  1882. }
  1883. t->err = NULL;
  1884. err:
  1885. OPENSSL_free(got);
  1886. return 1;
  1887. }
  1888. static const EVP_TEST_METHOD pkey_kdf_test_method = {
  1889. "PKEYKDF",
  1890. pkey_kdf_test_init,
  1891. pkey_kdf_test_cleanup,
  1892. pkey_kdf_test_parse,
  1893. pkey_kdf_test_run
  1894. };
  1895. /**
  1896. *** KEYPAIR TESTS
  1897. **/
  1898. typedef struct keypair_test_data_st {
  1899. EVP_PKEY *privk;
  1900. EVP_PKEY *pubk;
  1901. } KEYPAIR_TEST_DATA;
  1902. static int keypair_test_init(EVP_TEST *t, const char *pair)
  1903. {
  1904. KEYPAIR_TEST_DATA *data;
  1905. int rv = 0;
  1906. EVP_PKEY *pk = NULL, *pubk = NULL;
  1907. char *pub, *priv = NULL;
  1908. /* Split private and public names. */
  1909. if (!TEST_ptr(priv = OPENSSL_strdup(pair))
  1910. || !TEST_ptr(pub = strchr(priv, ':'))) {
  1911. t->err = "PARSING_ERROR";
  1912. goto end;
  1913. }
  1914. *pub++ = '\0';
  1915. if (!TEST_true(find_key(&pk, priv, private_keys))) {
  1916. TEST_info("Can't find private key: %s", priv);
  1917. t->err = "MISSING_PRIVATE_KEY";
  1918. goto end;
  1919. }
  1920. if (!TEST_true(find_key(&pubk, pub, public_keys))) {
  1921. TEST_info("Can't find public key: %s", pub);
  1922. t->err = "MISSING_PUBLIC_KEY";
  1923. goto end;
  1924. }
  1925. if (pk == NULL && pubk == NULL) {
  1926. /* Both keys are listed but unsupported: skip this test */
  1927. t->skip = 1;
  1928. rv = 1;
  1929. goto end;
  1930. }
  1931. if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
  1932. goto end;
  1933. data->privk = pk;
  1934. data->pubk = pubk;
  1935. t->data = data;
  1936. rv = 1;
  1937. t->err = NULL;
  1938. end:
  1939. OPENSSL_free(priv);
  1940. return rv;
  1941. }
  1942. static void keypair_test_cleanup(EVP_TEST *t)
  1943. {
  1944. OPENSSL_free(t->data);
  1945. t->data = NULL;
  1946. }
  1947. /*
  1948. * For tests that do not accept any custom keywords.
  1949. */
  1950. static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
  1951. {
  1952. return 0;
  1953. }
  1954. static int keypair_test_run(EVP_TEST *t)
  1955. {
  1956. int rv = 0;
  1957. const KEYPAIR_TEST_DATA *pair = t->data;
  1958. if (pair->privk == NULL || pair->pubk == NULL) {
  1959. /*
  1960. * this can only happen if only one of the keys is not set
  1961. * which means that one of them was unsupported while the
  1962. * other isn't: hence a key type mismatch.
  1963. */
  1964. t->err = "KEYPAIR_TYPE_MISMATCH";
  1965. rv = 1;
  1966. goto end;
  1967. }
  1968. if ((rv = EVP_PKEY_cmp(pair->privk, pair->pubk)) != 1 ) {
  1969. if ( 0 == rv ) {
  1970. t->err = "KEYPAIR_MISMATCH";
  1971. } else if ( -1 == rv ) {
  1972. t->err = "KEYPAIR_TYPE_MISMATCH";
  1973. } else if ( -2 == rv ) {
  1974. t->err = "UNSUPPORTED_KEY_COMPARISON";
  1975. } else {
  1976. TEST_error("Unexpected error in key comparison");
  1977. rv = 0;
  1978. goto end;
  1979. }
  1980. rv = 1;
  1981. goto end;
  1982. }
  1983. rv = 1;
  1984. t->err = NULL;
  1985. end:
  1986. return rv;
  1987. }
  1988. static const EVP_TEST_METHOD keypair_test_method = {
  1989. "PrivPubKeyPair",
  1990. keypair_test_init,
  1991. keypair_test_cleanup,
  1992. void_test_parse,
  1993. keypair_test_run
  1994. };
  1995. /**
  1996. *** KEYGEN TEST
  1997. **/
  1998. typedef struct keygen_test_data_st {
  1999. EVP_PKEY_CTX *genctx; /* Keygen context to use */
  2000. char *keyname; /* Key name to store key or NULL */
  2001. } KEYGEN_TEST_DATA;
  2002. static int keygen_test_init(EVP_TEST *t, const char *alg)
  2003. {
  2004. KEYGEN_TEST_DATA *data;
  2005. EVP_PKEY_CTX *genctx;
  2006. int nid = OBJ_sn2nid(alg);
  2007. if (nid == NID_undef) {
  2008. nid = OBJ_ln2nid(alg);
  2009. if (nid == NID_undef)
  2010. return 0;
  2011. }
  2012. if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_id(nid, NULL))) {
  2013. /* assume algorithm disabled */
  2014. t->skip = 1;
  2015. return 1;
  2016. }
  2017. if (EVP_PKEY_keygen_init(genctx) <= 0) {
  2018. t->err = "KEYGEN_INIT_ERROR";
  2019. goto err;
  2020. }
  2021. if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
  2022. goto err;
  2023. data->genctx = genctx;
  2024. data->keyname = NULL;
  2025. t->data = data;
  2026. t->err = NULL;
  2027. return 1;
  2028. err:
  2029. EVP_PKEY_CTX_free(genctx);
  2030. return 0;
  2031. }
  2032. static void keygen_test_cleanup(EVP_TEST *t)
  2033. {
  2034. KEYGEN_TEST_DATA *keygen = t->data;
  2035. EVP_PKEY_CTX_free(keygen->genctx);
  2036. OPENSSL_free(keygen->keyname);
  2037. OPENSSL_free(t->data);
  2038. t->data = NULL;
  2039. }
  2040. static int keygen_test_parse(EVP_TEST *t,
  2041. const char *keyword, const char *value)
  2042. {
  2043. KEYGEN_TEST_DATA *keygen = t->data;
  2044. if (strcmp(keyword, "KeyName") == 0)
  2045. return TEST_ptr(keygen->keyname = OPENSSL_strdup(value));
  2046. if (strcmp(keyword, "Ctrl") == 0)
  2047. return pkey_test_ctrl(t, keygen->genctx, value);
  2048. return 0;
  2049. }
  2050. static int keygen_test_run(EVP_TEST *t)
  2051. {
  2052. KEYGEN_TEST_DATA *keygen = t->data;
  2053. EVP_PKEY *pkey = NULL;
  2054. t->err = NULL;
  2055. if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) {
  2056. t->err = "KEYGEN_GENERATE_ERROR";
  2057. goto err;
  2058. }
  2059. if (keygen->keyname != NULL) {
  2060. KEY_LIST *key;
  2061. if (find_key(NULL, keygen->keyname, private_keys)) {
  2062. TEST_info("Duplicate key %s", keygen->keyname);
  2063. goto err;
  2064. }
  2065. if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
  2066. goto err;
  2067. key->name = keygen->keyname;
  2068. keygen->keyname = NULL;
  2069. key->key = pkey;
  2070. key->next = private_keys;
  2071. private_keys = key;
  2072. } else {
  2073. EVP_PKEY_free(pkey);
  2074. }
  2075. return 1;
  2076. err:
  2077. EVP_PKEY_free(pkey);
  2078. return 0;
  2079. }
  2080. static const EVP_TEST_METHOD keygen_test_method = {
  2081. "KeyGen",
  2082. keygen_test_init,
  2083. keygen_test_cleanup,
  2084. keygen_test_parse,
  2085. keygen_test_run,
  2086. };
  2087. /**
  2088. *** DIGEST SIGN+VERIFY TESTS
  2089. **/
  2090. typedef struct {
  2091. int is_verify; /* Set to 1 if verifying */
  2092. int is_oneshot; /* Set to 1 for one shot operation */
  2093. const EVP_MD *md; /* Digest to use */
  2094. EVP_MD_CTX *ctx; /* Digest context */
  2095. EVP_PKEY_CTX *pctx;
  2096. STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
  2097. unsigned char *osin; /* Input data if one shot */
  2098. size_t osin_len; /* Input length data if one shot */
  2099. unsigned char *output; /* Expected output */
  2100. size_t output_len; /* Expected output length */
  2101. } DIGESTSIGN_DATA;
  2102. static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
  2103. int is_oneshot)
  2104. {
  2105. const EVP_MD *md = NULL;
  2106. DIGESTSIGN_DATA *mdat;
  2107. if (strcmp(alg, "NULL") != 0) {
  2108. if ((md = EVP_get_digestbyname(alg)) == NULL) {
  2109. /* If alg has an OID assume disabled algorithm */
  2110. if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
  2111. t->skip = 1;
  2112. return 1;
  2113. }
  2114. return 0;
  2115. }
  2116. }
  2117. if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
  2118. return 0;
  2119. mdat->md = md;
  2120. if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
  2121. OPENSSL_free(mdat);
  2122. return 0;
  2123. }
  2124. mdat->is_verify = is_verify;
  2125. mdat->is_oneshot = is_oneshot;
  2126. t->data = mdat;
  2127. return 1;
  2128. }
  2129. static int digestsign_test_init(EVP_TEST *t, const char *alg)
  2130. {
  2131. return digestsigver_test_init(t, alg, 0, 0);
  2132. }
  2133. static void digestsigver_test_cleanup(EVP_TEST *t)
  2134. {
  2135. DIGESTSIGN_DATA *mdata = t->data;
  2136. EVP_MD_CTX_free(mdata->ctx);
  2137. sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
  2138. OPENSSL_free(mdata->osin);
  2139. OPENSSL_free(mdata->output);
  2140. OPENSSL_free(mdata);
  2141. t->data = NULL;
  2142. }
  2143. static int digestsigver_test_parse(EVP_TEST *t,
  2144. const char *keyword, const char *value)
  2145. {
  2146. DIGESTSIGN_DATA *mdata = t->data;
  2147. if (strcmp(keyword, "Key") == 0) {
  2148. EVP_PKEY *pkey = NULL;
  2149. int rv = 0;
  2150. if (mdata->is_verify)
  2151. rv = find_key(&pkey, value, public_keys);
  2152. if (rv == 0)
  2153. rv = find_key(&pkey, value, private_keys);
  2154. if (rv == 0 || pkey == NULL) {
  2155. t->skip = 1;
  2156. return 1;
  2157. }
  2158. if (mdata->is_verify) {
  2159. if (!EVP_DigestVerifyInit(mdata->ctx, &mdata->pctx, mdata->md,
  2160. NULL, pkey))
  2161. t->err = "DIGESTVERIFYINIT_ERROR";
  2162. return 1;
  2163. }
  2164. if (!EVP_DigestSignInit(mdata->ctx, &mdata->pctx, mdata->md, NULL,
  2165. pkey))
  2166. t->err = "DIGESTSIGNINIT_ERROR";
  2167. return 1;
  2168. }
  2169. if (strcmp(keyword, "Input") == 0) {
  2170. if (mdata->is_oneshot)
  2171. return parse_bin(value, &mdata->osin, &mdata->osin_len);
  2172. return evp_test_buffer_append(value, &mdata->input);
  2173. }
  2174. if (strcmp(keyword, "Output") == 0)
  2175. return parse_bin(value, &mdata->output, &mdata->output_len);
  2176. if (!mdata->is_oneshot) {
  2177. if (strcmp(keyword, "Count") == 0)
  2178. return evp_test_buffer_set_count(value, mdata->input);
  2179. if (strcmp(keyword, "Ncopy") == 0)
  2180. return evp_test_buffer_ncopy(value, mdata->input);
  2181. }
  2182. if (strcmp(keyword, "Ctrl") == 0) {
  2183. if (mdata->pctx == NULL)
  2184. return 0;
  2185. return pkey_test_ctrl(t, mdata->pctx, value);
  2186. }
  2187. return 0;
  2188. }
  2189. static int digestsign_update_fn(void *ctx, const unsigned char *buf,
  2190. size_t buflen)
  2191. {
  2192. return EVP_DigestSignUpdate(ctx, buf, buflen);
  2193. }
  2194. static int digestsign_test_run(EVP_TEST *t)
  2195. {
  2196. DIGESTSIGN_DATA *expected = t->data;
  2197. unsigned char *got = NULL;
  2198. size_t got_len;
  2199. if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
  2200. expected->ctx)) {
  2201. t->err = "DIGESTUPDATE_ERROR";
  2202. goto err;
  2203. }
  2204. if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
  2205. t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
  2206. goto err;
  2207. }
  2208. if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
  2209. t->err = "MALLOC_FAILURE";
  2210. goto err;
  2211. }
  2212. if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
  2213. t->err = "DIGESTSIGNFINAL_ERROR";
  2214. goto err;
  2215. }
  2216. if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
  2217. expected->output, expected->output_len,
  2218. got, got_len))
  2219. goto err;
  2220. t->err = NULL;
  2221. err:
  2222. OPENSSL_free(got);
  2223. return 1;
  2224. }
  2225. static const EVP_TEST_METHOD digestsign_test_method = {
  2226. "DigestSign",
  2227. digestsign_test_init,
  2228. digestsigver_test_cleanup,
  2229. digestsigver_test_parse,
  2230. digestsign_test_run
  2231. };
  2232. static int digestverify_test_init(EVP_TEST *t, const char *alg)
  2233. {
  2234. return digestsigver_test_init(t, alg, 1, 0);
  2235. }
  2236. static int digestverify_update_fn(void *ctx, const unsigned char *buf,
  2237. size_t buflen)
  2238. {
  2239. return EVP_DigestVerifyUpdate(ctx, buf, buflen);
  2240. }
  2241. static int digestverify_test_run(EVP_TEST *t)
  2242. {
  2243. DIGESTSIGN_DATA *mdata = t->data;
  2244. if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
  2245. t->err = "DIGESTUPDATE_ERROR";
  2246. return 1;
  2247. }
  2248. if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
  2249. mdata->output_len) <= 0)
  2250. t->err = "VERIFY_ERROR";
  2251. return 1;
  2252. }
  2253. static const EVP_TEST_METHOD digestverify_test_method = {
  2254. "DigestVerify",
  2255. digestverify_test_init,
  2256. digestsigver_test_cleanup,
  2257. digestsigver_test_parse,
  2258. digestverify_test_run
  2259. };
  2260. static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
  2261. {
  2262. return digestsigver_test_init(t, alg, 0, 1);
  2263. }
  2264. static int oneshot_digestsign_test_run(EVP_TEST *t)
  2265. {
  2266. DIGESTSIGN_DATA *expected = t->data;
  2267. unsigned char *got = NULL;
  2268. size_t got_len;
  2269. if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
  2270. expected->osin, expected->osin_len)) {
  2271. t->err = "DIGESTSIGN_LENGTH_ERROR";
  2272. goto err;
  2273. }
  2274. if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
  2275. t->err = "MALLOC_FAILURE";
  2276. goto err;
  2277. }
  2278. if (!EVP_DigestSign(expected->ctx, got, &got_len,
  2279. expected->osin, expected->osin_len)) {
  2280. t->err = "DIGESTSIGN_ERROR";
  2281. goto err;
  2282. }
  2283. if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
  2284. expected->output, expected->output_len,
  2285. got, got_len))
  2286. goto err;
  2287. t->err = NULL;
  2288. err:
  2289. OPENSSL_free(got);
  2290. return 1;
  2291. }
  2292. static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
  2293. "OneShotDigestSign",
  2294. oneshot_digestsign_test_init,
  2295. digestsigver_test_cleanup,
  2296. digestsigver_test_parse,
  2297. oneshot_digestsign_test_run
  2298. };
  2299. static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
  2300. {
  2301. return digestsigver_test_init(t, alg, 1, 1);
  2302. }
  2303. static int oneshot_digestverify_test_run(EVP_TEST *t)
  2304. {
  2305. DIGESTSIGN_DATA *mdata = t->data;
  2306. if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
  2307. mdata->osin, mdata->osin_len) <= 0)
  2308. t->err = "VERIFY_ERROR";
  2309. return 1;
  2310. }
  2311. static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
  2312. "OneShotDigestVerify",
  2313. oneshot_digestverify_test_init,
  2314. digestsigver_test_cleanup,
  2315. digestsigver_test_parse,
  2316. oneshot_digestverify_test_run
  2317. };
  2318. /**
  2319. *** PARSING AND DISPATCH
  2320. **/
  2321. static const EVP_TEST_METHOD *evp_test_list[] = {
  2322. &cipher_test_method,
  2323. &digest_test_method,
  2324. &digestsign_test_method,
  2325. &digestverify_test_method,
  2326. &encode_test_method,
  2327. &kdf_test_method,
  2328. &pkey_kdf_test_method,
  2329. &keypair_test_method,
  2330. &keygen_test_method,
  2331. &mac_test_method,
  2332. &oneshot_digestsign_test_method,
  2333. &oneshot_digestverify_test_method,
  2334. &pbe_test_method,
  2335. &pdecrypt_test_method,
  2336. &pderive_test_method,
  2337. &psign_test_method,
  2338. &pverify_recover_test_method,
  2339. &pverify_test_method,
  2340. NULL
  2341. };
  2342. static const EVP_TEST_METHOD *find_test(const char *name)
  2343. {
  2344. const EVP_TEST_METHOD **tt;
  2345. for (tt = evp_test_list; *tt; tt++) {
  2346. if (strcmp(name, (*tt)->name) == 0)
  2347. return *tt;
  2348. }
  2349. return NULL;
  2350. }
  2351. static void clear_test(EVP_TEST *t)
  2352. {
  2353. test_clearstanza(&t->s);
  2354. ERR_clear_error();
  2355. if (t->data != NULL) {
  2356. if (t->meth != NULL)
  2357. t->meth->cleanup(t);
  2358. OPENSSL_free(t->data);
  2359. t->data = NULL;
  2360. }
  2361. OPENSSL_free(t->expected_err);
  2362. t->expected_err = NULL;
  2363. OPENSSL_free(t->func);
  2364. t->func = NULL;
  2365. OPENSSL_free(t->reason);
  2366. t->reason = NULL;
  2367. /* Text literal. */
  2368. t->err = NULL;
  2369. t->skip = 0;
  2370. t->meth = NULL;
  2371. }
  2372. /*
  2373. * Check for errors in the test structure; return 1 if okay, else 0.
  2374. */
  2375. static int check_test_error(EVP_TEST *t)
  2376. {
  2377. unsigned long err;
  2378. const char *func;
  2379. const char *reason;
  2380. if (t->err == NULL && t->expected_err == NULL)
  2381. return 1;
  2382. if (t->err != NULL && t->expected_err == NULL) {
  2383. if (t->aux_err != NULL) {
  2384. TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
  2385. t->s.test_file, t->s.start, t->aux_err, t->err);
  2386. } else {
  2387. TEST_info("%s:%d: Source of above error; unexpected error %s",
  2388. t->s.test_file, t->s.start, t->err);
  2389. }
  2390. return 0;
  2391. }
  2392. if (t->err == NULL && t->expected_err != NULL) {
  2393. TEST_info("%s:%d: Succeeded but was expecting %s",
  2394. t->s.test_file, t->s.start, t->expected_err);
  2395. return 0;
  2396. }
  2397. if (strcmp(t->err, t->expected_err) != 0) {
  2398. TEST_info("%s:%d: Expected %s got %s",
  2399. t->s.test_file, t->s.start, t->expected_err, t->err);
  2400. return 0;
  2401. }
  2402. if (t->func == NULL && t->reason == NULL)
  2403. return 1;
  2404. if (t->func == NULL || t->reason == NULL) {
  2405. TEST_info("%s:%d: Test is missing function or reason code",
  2406. t->s.test_file, t->s.start);
  2407. return 0;
  2408. }
  2409. err = ERR_peek_error();
  2410. if (err == 0) {
  2411. TEST_info("%s:%d: Expected error \"%s:%s\" not set",
  2412. t->s.test_file, t->s.start, t->func, t->reason);
  2413. return 0;
  2414. }
  2415. func = ERR_func_error_string(err);
  2416. reason = ERR_reason_error_string(err);
  2417. if (func == NULL && reason == NULL) {
  2418. TEST_info("%s:%d: Expected error \"%s:%s\", no strings available."
  2419. " Assuming ok.",
  2420. t->s.test_file, t->s.start, t->func, t->reason);
  2421. return 1;
  2422. }
  2423. if (strcmp(func, t->func) == 0 && strcmp(reason, t->reason) == 0)
  2424. return 1;
  2425. TEST_info("%s:%d: Expected error \"%s:%s\", got \"%s:%s\"",
  2426. t->s.test_file, t->s.start, t->func, t->reason, func, reason);
  2427. return 0;
  2428. }
  2429. /*
  2430. * Run a parsed test. Log a message and return 0 on error.
  2431. */
  2432. static int run_test(EVP_TEST *t)
  2433. {
  2434. if (t->meth == NULL)
  2435. return 1;
  2436. t->s.numtests++;
  2437. if (t->skip) {
  2438. t->s.numskip++;
  2439. } else {
  2440. /* run the test */
  2441. if (t->err == NULL && t->meth->run_test(t) != 1) {
  2442. TEST_info("%s:%d %s error",
  2443. t->s.test_file, t->s.start, t->meth->name);
  2444. return 0;
  2445. }
  2446. if (!check_test_error(t)) {
  2447. TEST_openssl_errors();
  2448. t->s.errors++;
  2449. }
  2450. }
  2451. /* clean it up */
  2452. return 1;
  2453. }
  2454. static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
  2455. {
  2456. for (; lst != NULL; lst = lst->next) {
  2457. if (strcmp(lst->name, name) == 0) {
  2458. if (ppk != NULL)
  2459. *ppk = lst->key;
  2460. return 1;
  2461. }
  2462. }
  2463. return 0;
  2464. }
  2465. static void free_key_list(KEY_LIST *lst)
  2466. {
  2467. while (lst != NULL) {
  2468. KEY_LIST *next = lst->next;
  2469. EVP_PKEY_free(lst->key);
  2470. OPENSSL_free(lst->name);
  2471. OPENSSL_free(lst);
  2472. lst = next;
  2473. }
  2474. }
  2475. /*
  2476. * Is the key type an unsupported algorithm?
  2477. */
  2478. static int key_unsupported(void)
  2479. {
  2480. long err = ERR_peek_error();
  2481. if (ERR_GET_LIB(err) == ERR_LIB_EVP
  2482. && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
  2483. ERR_clear_error();
  2484. return 1;
  2485. }
  2486. #ifndef OPENSSL_NO_EC
  2487. /*
  2488. * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
  2489. * hint to an unsupported algorithm/curve (e.g. if binary EC support is
  2490. * disabled).
  2491. */
  2492. if (ERR_GET_LIB(err) == ERR_LIB_EC
  2493. && ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP) {
  2494. ERR_clear_error();
  2495. return 1;
  2496. }
  2497. #endif /* OPENSSL_NO_EC */
  2498. return 0;
  2499. }
  2500. /*
  2501. * NULL out the value from |pp| but return it. This "steals" a pointer.
  2502. */
  2503. static char *take_value(PAIR *pp)
  2504. {
  2505. char *p = pp->value;
  2506. pp->value = NULL;
  2507. return p;
  2508. }
  2509. /*
  2510. * Read and parse one test. Return 0 if failure, 1 if okay.
  2511. */
  2512. static int parse(EVP_TEST *t)
  2513. {
  2514. KEY_LIST *key, **klist;
  2515. EVP_PKEY *pkey;
  2516. PAIR *pp;
  2517. int i;
  2518. top:
  2519. do {
  2520. if (BIO_eof(t->s.fp))
  2521. return EOF;
  2522. clear_test(t);
  2523. if (!test_readstanza(&t->s))
  2524. return 0;
  2525. } while (t->s.numpairs == 0);
  2526. pp = &t->s.pairs[0];
  2527. /* Are we adding a key? */
  2528. klist = NULL;
  2529. pkey = NULL;
  2530. if (strcmp(pp->key, "PrivateKey") == 0) {
  2531. pkey = PEM_read_bio_PrivateKey(t->s.key, NULL, 0, NULL);
  2532. if (pkey == NULL && !key_unsupported()) {
  2533. EVP_PKEY_free(pkey);
  2534. TEST_info("Can't read private key %s", pp->value);
  2535. TEST_openssl_errors();
  2536. return 0;
  2537. }
  2538. klist = &private_keys;
  2539. } else if (strcmp(pp->key, "PublicKey") == 0) {
  2540. pkey = PEM_read_bio_PUBKEY(t->s.key, NULL, 0, NULL);
  2541. if (pkey == NULL && !key_unsupported()) {
  2542. EVP_PKEY_free(pkey);
  2543. TEST_info("Can't read public key %s", pp->value);
  2544. TEST_openssl_errors();
  2545. return 0;
  2546. }
  2547. klist = &public_keys;
  2548. } else if (strcmp(pp->key, "PrivateKeyRaw") == 0
  2549. || strcmp(pp->key, "PublicKeyRaw") == 0 ) {
  2550. char *strnid = NULL, *keydata = NULL;
  2551. unsigned char *keybin;
  2552. size_t keylen;
  2553. int nid;
  2554. if (strcmp(pp->key, "PrivateKeyRaw") == 0)
  2555. klist = &private_keys;
  2556. else
  2557. klist = &public_keys;
  2558. strnid = strchr(pp->value, ':');
  2559. if (strnid != NULL) {
  2560. *strnid++ = '\0';
  2561. keydata = strchr(strnid, ':');
  2562. if (keydata != NULL)
  2563. *keydata++ = '\0';
  2564. }
  2565. if (keydata == NULL) {
  2566. TEST_info("Failed to parse %s value", pp->key);
  2567. return 0;
  2568. }
  2569. nid = OBJ_txt2nid(strnid);
  2570. if (nid == NID_undef) {
  2571. TEST_info("Uncrecognised algorithm NID");
  2572. return 0;
  2573. }
  2574. if (!parse_bin(keydata, &keybin, &keylen)) {
  2575. TEST_info("Failed to create binary key");
  2576. return 0;
  2577. }
  2578. if (klist == &private_keys)
  2579. pkey = EVP_PKEY_new_raw_private_key(nid, NULL, keybin, keylen);
  2580. else
  2581. pkey = EVP_PKEY_new_raw_public_key(nid, NULL, keybin, keylen);
  2582. if (pkey == NULL && !key_unsupported()) {
  2583. TEST_info("Can't read %s data", pp->key);
  2584. OPENSSL_free(keybin);
  2585. TEST_openssl_errors();
  2586. return 0;
  2587. }
  2588. OPENSSL_free(keybin);
  2589. }
  2590. /* If we have a key add to list */
  2591. if (klist != NULL) {
  2592. if (find_key(NULL, pp->value, *klist)) {
  2593. TEST_info("Duplicate key %s", pp->value);
  2594. return 0;
  2595. }
  2596. if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
  2597. return 0;
  2598. key->name = take_value(pp);
  2599. /* Hack to detect SM2 keys */
  2600. if(pkey != NULL && strstr(key->name, "SM2") != NULL) {
  2601. #ifdef OPENSSL_NO_SM2
  2602. EVP_PKEY_free(pkey);
  2603. pkey = NULL;
  2604. #else
  2605. EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2);
  2606. #endif
  2607. }
  2608. key->key = pkey;
  2609. key->next = *klist;
  2610. *klist = key;
  2611. /* Go back and start a new stanza. */
  2612. if (t->s.numpairs != 1)
  2613. TEST_info("Line %d: missing blank line\n", t->s.curr);
  2614. goto top;
  2615. }
  2616. /* Find the test, based on first keyword. */
  2617. if (!TEST_ptr(t->meth = find_test(pp->key)))
  2618. return 0;
  2619. if (!t->meth->init(t, pp->value)) {
  2620. TEST_error("unknown %s: %s\n", pp->key, pp->value);
  2621. return 0;
  2622. }
  2623. if (t->skip == 1) {
  2624. /* TEST_info("skipping %s %s", pp->key, pp->value); */
  2625. return 0;
  2626. }
  2627. for (pp++, i = 1; i < t->s.numpairs; pp++, i++) {
  2628. if (strcmp(pp->key, "Result") == 0) {
  2629. if (t->expected_err != NULL) {
  2630. TEST_info("Line %d: multiple result lines", t->s.curr);
  2631. return 0;
  2632. }
  2633. t->expected_err = take_value(pp);
  2634. } else if (strcmp(pp->key, "Function") == 0) {
  2635. if (t->func != NULL) {
  2636. TEST_info("Line %d: multiple function lines\n", t->s.curr);
  2637. return 0;
  2638. }
  2639. t->func = take_value(pp);
  2640. } else if (strcmp(pp->key, "Reason") == 0) {
  2641. if (t->reason != NULL) {
  2642. TEST_info("Line %d: multiple reason lines", t->s.curr);
  2643. return 0;
  2644. }
  2645. t->reason = take_value(pp);
  2646. } else {
  2647. /* Must be test specific line: try to parse it */
  2648. int rv = t->meth->parse(t, pp->key, pp->value);
  2649. if (rv == 0) {
  2650. TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key);
  2651. return 0;
  2652. }
  2653. if (rv < 0) {
  2654. TEST_info("Line %d: error processing keyword %s = %s\n",
  2655. t->s.curr, pp->key, pp->value);
  2656. return 0;
  2657. }
  2658. }
  2659. }
  2660. return 1;
  2661. }
  2662. static int run_file_tests(int i)
  2663. {
  2664. EVP_TEST *t;
  2665. const char *testfile = test_get_argument(i);
  2666. int c;
  2667. if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t))))
  2668. return 0;
  2669. if (!test_start_file(&t->s, testfile)) {
  2670. OPENSSL_free(t);
  2671. return 0;
  2672. }
  2673. while (!BIO_eof(t->s.fp)) {
  2674. c = parse(t);
  2675. if (t->skip)
  2676. continue;
  2677. if (c == 0 || !run_test(t)) {
  2678. t->s.errors++;
  2679. break;
  2680. }
  2681. }
  2682. test_end_file(&t->s);
  2683. clear_test(t);
  2684. free_key_list(public_keys);
  2685. free_key_list(private_keys);
  2686. BIO_free(t->s.key);
  2687. c = t->s.errors;
  2688. OPENSSL_free(t);
  2689. return c == 0;
  2690. }
  2691. OPT_TEST_DECLARE_USAGE("file...\n")
  2692. int setup_tests(void)
  2693. {
  2694. size_t n = test_get_argument_count();
  2695. if (n == 0)
  2696. return 0;
  2697. ADD_ALL_TESTS(run_file_tests, n);
  2698. return 1;
  2699. }