evp_test.c 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894
  1. /*
  2. * Copyright 2015-2018 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. /* Collection of controls */
  782. STACK_OF(OPENSSL_STRING) *controls;
  783. } MAC_DATA;
  784. static int mac_test_init(EVP_TEST *t, const char *alg)
  785. {
  786. const EVP_MAC *mac = NULL;
  787. int type = NID_undef;
  788. MAC_DATA *mdat;
  789. if ((mac = EVP_get_macbyname(alg)) == NULL) {
  790. /*
  791. * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
  792. * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
  793. * the EVP_PKEY method.
  794. */
  795. size_t sz = strlen(alg);
  796. static const char epilogue[] = " by EVP_PKEY";
  797. if (sz >= sizeof(epilogue)
  798. && strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0)
  799. sz -= sizeof(epilogue) - 1;
  800. if (strncmp(alg, "HMAC", sz) == 0) {
  801. type = EVP_PKEY_HMAC;
  802. } else if (strncmp(alg, "CMAC", sz) == 0) {
  803. #ifndef OPENSSL_NO_CMAC
  804. type = EVP_PKEY_CMAC;
  805. #else
  806. t->skip = 1;
  807. return 1;
  808. #endif
  809. } else if (strncmp(alg, "Poly1305", sz) == 0) {
  810. #ifndef OPENSSL_NO_POLY1305
  811. type = EVP_PKEY_POLY1305;
  812. #else
  813. t->skip = 1;
  814. return 1;
  815. #endif
  816. } else if (strncmp(alg, "SipHash", sz) == 0) {
  817. #ifndef OPENSSL_NO_SIPHASH
  818. type = EVP_PKEY_SIPHASH;
  819. #else
  820. t->skip = 1;
  821. return 1;
  822. #endif
  823. } else {
  824. /*
  825. * Not a known EVP_PKEY method either. If it's a known OID, then
  826. * assume it's been disabled.
  827. */
  828. if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
  829. t->skip = 1;
  830. return 1;
  831. }
  832. return 0;
  833. }
  834. }
  835. mdat = OPENSSL_zalloc(sizeof(*mdat));
  836. mdat->type = type;
  837. mdat->mac = mac;
  838. mdat->controls = sk_OPENSSL_STRING_new_null();
  839. t->data = mdat;
  840. return 1;
  841. }
  842. /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
  843. static void openssl_free(char *m)
  844. {
  845. OPENSSL_free(m);
  846. }
  847. static void mac_test_cleanup(EVP_TEST *t)
  848. {
  849. MAC_DATA *mdat = t->data;
  850. sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free);
  851. OPENSSL_free(mdat->alg);
  852. OPENSSL_free(mdat->key);
  853. OPENSSL_free(mdat->iv);
  854. OPENSSL_free(mdat->custom);
  855. OPENSSL_free(mdat->input);
  856. OPENSSL_free(mdat->output);
  857. }
  858. static int mac_test_parse(EVP_TEST *t,
  859. const char *keyword, const char *value)
  860. {
  861. MAC_DATA *mdata = t->data;
  862. if (strcmp(keyword, "Key") == 0)
  863. return parse_bin(value, &mdata->key, &mdata->key_len);
  864. if (strcmp(keyword, "IV") == 0)
  865. return parse_bin(value, &mdata->iv, &mdata->iv_len);
  866. if (strcmp(keyword, "Custom") == 0)
  867. return parse_bin(value, &mdata->custom, &mdata->custom_len);
  868. if (strcmp(keyword, "Algorithm") == 0) {
  869. mdata->alg = OPENSSL_strdup(value);
  870. if (!mdata->alg)
  871. return 0;
  872. return 1;
  873. }
  874. if (strcmp(keyword, "Input") == 0)
  875. return parse_bin(value, &mdata->input, &mdata->input_len);
  876. if (strcmp(keyword, "Output") == 0)
  877. return parse_bin(value, &mdata->output, &mdata->output_len);
  878. if (strcmp(keyword, "Ctrl") == 0)
  879. return sk_OPENSSL_STRING_push(mdata->controls,
  880. OPENSSL_strdup(value)) != 0;
  881. return 0;
  882. }
  883. static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx,
  884. const char *value)
  885. {
  886. int rv;
  887. char *p, *tmpval;
  888. if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
  889. return 0;
  890. p = strchr(tmpval, ':');
  891. if (p != NULL)
  892. *p++ = '\0';
  893. rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
  894. if (rv == -2)
  895. t->err = "PKEY_CTRL_INVALID";
  896. else if (rv <= 0)
  897. t->err = "PKEY_CTRL_ERROR";
  898. else
  899. rv = 1;
  900. OPENSSL_free(tmpval);
  901. return rv > 0;
  902. }
  903. static int mac_test_run_pkey(EVP_TEST *t)
  904. {
  905. MAC_DATA *expected = t->data;
  906. EVP_MD_CTX *mctx = NULL;
  907. EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
  908. EVP_PKEY *key = NULL;
  909. const EVP_MD *md = NULL;
  910. unsigned char *got = NULL;
  911. size_t got_len;
  912. int i;
  913. if (expected->alg == NULL)
  914. TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type));
  915. else
  916. TEST_info("Trying the EVP_PKEY %s test with %s",
  917. OBJ_nid2sn(expected->type), expected->alg);
  918. #ifdef OPENSSL_NO_DES
  919. if (expected->alg != NULL && strstr(expected->alg, "DES") != NULL) {
  920. /* Skip DES */
  921. t->err = NULL;
  922. goto err;
  923. }
  924. #endif
  925. if (expected->type == EVP_PKEY_CMAC)
  926. key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len,
  927. EVP_get_cipherbyname(expected->alg));
  928. else
  929. key = EVP_PKEY_new_raw_private_key(expected->type, NULL, expected->key,
  930. expected->key_len);
  931. if (key == NULL) {
  932. t->err = "MAC_KEY_CREATE_ERROR";
  933. goto err;
  934. }
  935. if (expected->type == EVP_PKEY_HMAC) {
  936. if (!TEST_ptr(md = EVP_get_digestbyname(expected->alg))) {
  937. t->err = "MAC_ALGORITHM_SET_ERROR";
  938. goto err;
  939. }
  940. }
  941. if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
  942. t->err = "INTERNAL_ERROR";
  943. goto err;
  944. }
  945. if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key)) {
  946. t->err = "DIGESTSIGNINIT_ERROR";
  947. goto err;
  948. }
  949. for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++)
  950. if (!mac_test_ctrl_pkey(t, pctx,
  951. sk_OPENSSL_STRING_value(expected->controls,
  952. i))) {
  953. t->err = "EVPPKEYCTXCTRL_ERROR";
  954. goto err;
  955. }
  956. if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) {
  957. t->err = "DIGESTSIGNUPDATE_ERROR";
  958. goto err;
  959. }
  960. if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) {
  961. t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
  962. goto err;
  963. }
  964. if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
  965. t->err = "TEST_FAILURE";
  966. goto err;
  967. }
  968. if (!EVP_DigestSignFinal(mctx, got, &got_len)
  969. || !memory_err_compare(t, "TEST_MAC_ERR",
  970. expected->output, expected->output_len,
  971. got, got_len)) {
  972. t->err = "TEST_MAC_ERR";
  973. goto err;
  974. }
  975. t->err = NULL;
  976. err:
  977. EVP_MD_CTX_free(mctx);
  978. OPENSSL_free(got);
  979. EVP_PKEY_CTX_free(genctx);
  980. EVP_PKEY_free(key);
  981. return 1;
  982. }
  983. static int mac_test_run_mac(EVP_TEST *t)
  984. {
  985. MAC_DATA *expected = t->data;
  986. EVP_MAC_CTX *ctx = NULL;
  987. const void *algo = NULL;
  988. int algo_ctrl = 0;
  989. unsigned char *got = NULL;
  990. size_t got_len;
  991. int rv, i;
  992. if (expected->alg == NULL)
  993. TEST_info("Trying the EVP_MAC %s test", EVP_MAC_name(expected->mac));
  994. else
  995. TEST_info("Trying the EVP_MAC %s test with %s",
  996. EVP_MAC_name(expected->mac), expected->alg);
  997. #ifdef OPENSSL_NO_DES
  998. if (expected->alg != NULL && strstr(expected->alg, "DES") != NULL) {
  999. /* Skip DES */
  1000. t->err = NULL;
  1001. goto err;
  1002. }
  1003. #endif
  1004. if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) {
  1005. t->err = "MAC_CREATE_ERROR";
  1006. goto err;
  1007. }
  1008. if (expected->alg != NULL
  1009. && ((algo_ctrl = EVP_MAC_CTRL_SET_CIPHER,
  1010. algo = EVP_get_cipherbyname(expected->alg)) == NULL
  1011. && (algo_ctrl = EVP_MAC_CTRL_SET_MD,
  1012. algo = EVP_get_digestbyname(expected->alg)) == NULL)) {
  1013. t->err = "MAC_BAD_ALGORITHM";
  1014. goto err;
  1015. }
  1016. if (algo_ctrl != 0) {
  1017. rv = EVP_MAC_ctrl(ctx, algo_ctrl, algo);
  1018. if (rv == -2) {
  1019. t->err = "MAC_CTRL_INVALID";
  1020. goto err;
  1021. } else if (rv <= 0) {
  1022. t->err = "MAC_CTRL_ERROR";
  1023. goto err;
  1024. }
  1025. }
  1026. rv = EVP_MAC_ctrl(ctx, EVP_MAC_CTRL_SET_KEY,
  1027. expected->key, expected->key_len);
  1028. if (rv == -2) {
  1029. t->err = "MAC_CTRL_INVALID";
  1030. goto err;
  1031. } else if (rv <= 0) {
  1032. t->err = "MAC_CTRL_ERROR";
  1033. goto err;
  1034. }
  1035. if (expected->custom != NULL) {
  1036. rv = EVP_MAC_ctrl(ctx, EVP_MAC_CTRL_SET_CUSTOM,
  1037. expected->custom, expected->custom_len);
  1038. if (rv == -2) {
  1039. t->err = "MAC_CTRL_INVALID";
  1040. goto err;
  1041. } else if (rv <= 0) {
  1042. t->err = "MAC_CTRL_ERROR";
  1043. goto err;
  1044. }
  1045. }
  1046. if (expected->iv != NULL) {
  1047. rv = EVP_MAC_ctrl(ctx, EVP_MAC_CTRL_SET_IV,
  1048. expected->iv, expected->iv_len);
  1049. if (rv == -2) {
  1050. t->err = "MAC_CTRL_INVALID";
  1051. goto err;
  1052. } else if (rv <= 0) {
  1053. t->err = "MAC_CTRL_ERROR";
  1054. goto err;
  1055. }
  1056. }
  1057. if (!EVP_MAC_init(ctx)) {
  1058. t->err = "MAC_INIT_ERROR";
  1059. goto err;
  1060. }
  1061. for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) {
  1062. char *p, *tmpval;
  1063. char *value = sk_OPENSSL_STRING_value(expected->controls, i);
  1064. if (!TEST_ptr(tmpval = OPENSSL_strdup(value))) {
  1065. t->err = "MAC_CTRL_ERROR";
  1066. goto err;
  1067. }
  1068. p = strchr(tmpval, ':');
  1069. if (p != NULL)
  1070. *p++ = '\0';
  1071. rv = EVP_MAC_ctrl_str(ctx, tmpval, p);
  1072. OPENSSL_free(tmpval);
  1073. if (rv == -2) {
  1074. t->err = "MAC_CTRL_INVALID";
  1075. goto err;
  1076. } else if (rv <= 0) {
  1077. t->err = "MAC_CTRL_ERROR";
  1078. goto err;
  1079. }
  1080. }
  1081. if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) {
  1082. t->err = "MAC_UPDATE_ERROR";
  1083. goto err;
  1084. }
  1085. if (!EVP_MAC_final(ctx, NULL, &got_len)) {
  1086. t->err = "MAC_FINAL_LENGTH_ERROR";
  1087. goto err;
  1088. }
  1089. if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
  1090. t->err = "TEST_FAILURE";
  1091. goto err;
  1092. }
  1093. if (!EVP_MAC_final(ctx, got, &got_len)
  1094. || !memory_err_compare(t, "TEST_MAC_ERR",
  1095. expected->output, expected->output_len,
  1096. got, got_len)) {
  1097. t->err = "TEST_MAC_ERR";
  1098. goto err;
  1099. }
  1100. t->err = NULL;
  1101. err:
  1102. EVP_MAC_CTX_free(ctx);
  1103. OPENSSL_free(got);
  1104. return 1;
  1105. }
  1106. static int mac_test_run(EVP_TEST *t)
  1107. {
  1108. MAC_DATA *expected = t->data;
  1109. if (expected->mac != NULL)
  1110. return mac_test_run_mac(t);
  1111. return mac_test_run_pkey(t);
  1112. }
  1113. static const EVP_TEST_METHOD mac_test_method = {
  1114. "MAC",
  1115. mac_test_init,
  1116. mac_test_cleanup,
  1117. mac_test_parse,
  1118. mac_test_run
  1119. };
  1120. /**
  1121. *** PUBLIC KEY TESTS
  1122. *** These are all very similar and share much common code.
  1123. **/
  1124. typedef struct pkey_data_st {
  1125. /* Context for this operation */
  1126. EVP_PKEY_CTX *ctx;
  1127. /* Key operation to perform */
  1128. int (*keyop) (EVP_PKEY_CTX *ctx,
  1129. unsigned char *sig, size_t *siglen,
  1130. const unsigned char *tbs, size_t tbslen);
  1131. /* Input to MAC */
  1132. unsigned char *input;
  1133. size_t input_len;
  1134. /* Expected output */
  1135. unsigned char *output;
  1136. size_t output_len;
  1137. } PKEY_DATA;
  1138. /*
  1139. * Perform public key operation setup: lookup key, allocated ctx and call
  1140. * the appropriate initialisation function
  1141. */
  1142. static int pkey_test_init(EVP_TEST *t, const char *name,
  1143. int use_public,
  1144. int (*keyopinit) (EVP_PKEY_CTX *ctx),
  1145. int (*keyop)(EVP_PKEY_CTX *ctx,
  1146. unsigned char *sig, size_t *siglen,
  1147. const unsigned char *tbs,
  1148. size_t tbslen))
  1149. {
  1150. PKEY_DATA *kdata;
  1151. EVP_PKEY *pkey = NULL;
  1152. int rv = 0;
  1153. if (use_public)
  1154. rv = find_key(&pkey, name, public_keys);
  1155. if (rv == 0)
  1156. rv = find_key(&pkey, name, private_keys);
  1157. if (rv == 0 || pkey == NULL) {
  1158. t->skip = 1;
  1159. return 1;
  1160. }
  1161. if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) {
  1162. EVP_PKEY_free(pkey);
  1163. return 0;
  1164. }
  1165. kdata->keyop = keyop;
  1166. if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL))) {
  1167. EVP_PKEY_free(pkey);
  1168. OPENSSL_free(kdata);
  1169. return 0;
  1170. }
  1171. if (keyopinit(kdata->ctx) <= 0)
  1172. t->err = "KEYOP_INIT_ERROR";
  1173. t->data = kdata;
  1174. return 1;
  1175. }
  1176. static void pkey_test_cleanup(EVP_TEST *t)
  1177. {
  1178. PKEY_DATA *kdata = t->data;
  1179. OPENSSL_free(kdata->input);
  1180. OPENSSL_free(kdata->output);
  1181. EVP_PKEY_CTX_free(kdata->ctx);
  1182. }
  1183. static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
  1184. const char *value)
  1185. {
  1186. int rv;
  1187. char *p, *tmpval;
  1188. if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
  1189. return 0;
  1190. p = strchr(tmpval, ':');
  1191. if (p != NULL)
  1192. *p++ = '\0';
  1193. rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
  1194. if (rv == -2) {
  1195. t->err = "PKEY_CTRL_INVALID";
  1196. rv = 1;
  1197. } else if (p != NULL && rv <= 0) {
  1198. /* If p has an OID and lookup fails assume disabled algorithm */
  1199. int nid = OBJ_sn2nid(p);
  1200. if (nid == NID_undef)
  1201. nid = OBJ_ln2nid(p);
  1202. if (nid != NID_undef
  1203. && EVP_get_digestbynid(nid) == NULL
  1204. && EVP_get_cipherbynid(nid) == NULL) {
  1205. t->skip = 1;
  1206. rv = 1;
  1207. } else {
  1208. t->err = "PKEY_CTRL_ERROR";
  1209. rv = 1;
  1210. }
  1211. }
  1212. OPENSSL_free(tmpval);
  1213. return rv > 0;
  1214. }
  1215. static int pkey_test_parse(EVP_TEST *t,
  1216. const char *keyword, const char *value)
  1217. {
  1218. PKEY_DATA *kdata = t->data;
  1219. if (strcmp(keyword, "Input") == 0)
  1220. return parse_bin(value, &kdata->input, &kdata->input_len);
  1221. if (strcmp(keyword, "Output") == 0)
  1222. return parse_bin(value, &kdata->output, &kdata->output_len);
  1223. if (strcmp(keyword, "Ctrl") == 0)
  1224. return pkey_test_ctrl(t, kdata->ctx, value);
  1225. return 0;
  1226. }
  1227. static int pkey_test_run(EVP_TEST *t)
  1228. {
  1229. PKEY_DATA *expected = t->data;
  1230. unsigned char *got = NULL;
  1231. size_t got_len;
  1232. if (expected->keyop(expected->ctx, NULL, &got_len,
  1233. expected->input, expected->input_len) <= 0
  1234. || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
  1235. t->err = "KEYOP_LENGTH_ERROR";
  1236. goto err;
  1237. }
  1238. if (expected->keyop(expected->ctx, got, &got_len,
  1239. expected->input, expected->input_len) <= 0) {
  1240. t->err = "KEYOP_ERROR";
  1241. goto err;
  1242. }
  1243. if (!memory_err_compare(t, "KEYOP_MISMATCH",
  1244. expected->output, expected->output_len,
  1245. got, got_len))
  1246. goto err;
  1247. t->err = NULL;
  1248. err:
  1249. OPENSSL_free(got);
  1250. return 1;
  1251. }
  1252. static int sign_test_init(EVP_TEST *t, const char *name)
  1253. {
  1254. return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
  1255. }
  1256. static const EVP_TEST_METHOD psign_test_method = {
  1257. "Sign",
  1258. sign_test_init,
  1259. pkey_test_cleanup,
  1260. pkey_test_parse,
  1261. pkey_test_run
  1262. };
  1263. static int verify_recover_test_init(EVP_TEST *t, const char *name)
  1264. {
  1265. return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
  1266. EVP_PKEY_verify_recover);
  1267. }
  1268. static const EVP_TEST_METHOD pverify_recover_test_method = {
  1269. "VerifyRecover",
  1270. verify_recover_test_init,
  1271. pkey_test_cleanup,
  1272. pkey_test_parse,
  1273. pkey_test_run
  1274. };
  1275. static int decrypt_test_init(EVP_TEST *t, const char *name)
  1276. {
  1277. return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
  1278. EVP_PKEY_decrypt);
  1279. }
  1280. static const EVP_TEST_METHOD pdecrypt_test_method = {
  1281. "Decrypt",
  1282. decrypt_test_init,
  1283. pkey_test_cleanup,
  1284. pkey_test_parse,
  1285. pkey_test_run
  1286. };
  1287. static int verify_test_init(EVP_TEST *t, const char *name)
  1288. {
  1289. return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
  1290. }
  1291. static int verify_test_run(EVP_TEST *t)
  1292. {
  1293. PKEY_DATA *kdata = t->data;
  1294. if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
  1295. kdata->input, kdata->input_len) <= 0)
  1296. t->err = "VERIFY_ERROR";
  1297. return 1;
  1298. }
  1299. static const EVP_TEST_METHOD pverify_test_method = {
  1300. "Verify",
  1301. verify_test_init,
  1302. pkey_test_cleanup,
  1303. pkey_test_parse,
  1304. verify_test_run
  1305. };
  1306. static int pderive_test_init(EVP_TEST *t, const char *name)
  1307. {
  1308. return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
  1309. }
  1310. static int pderive_test_parse(EVP_TEST *t,
  1311. const char *keyword, const char *value)
  1312. {
  1313. PKEY_DATA *kdata = t->data;
  1314. if (strcmp(keyword, "PeerKey") == 0) {
  1315. EVP_PKEY *peer;
  1316. if (find_key(&peer, value, public_keys) == 0)
  1317. return 0;
  1318. if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
  1319. return 0;
  1320. return 1;
  1321. }
  1322. if (strcmp(keyword, "SharedSecret") == 0)
  1323. return parse_bin(value, &kdata->output, &kdata->output_len);
  1324. if (strcmp(keyword, "Ctrl") == 0)
  1325. return pkey_test_ctrl(t, kdata->ctx, value);
  1326. return 0;
  1327. }
  1328. static int pderive_test_run(EVP_TEST *t)
  1329. {
  1330. PKEY_DATA *expected = t->data;
  1331. unsigned char *got = NULL;
  1332. size_t got_len;
  1333. if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) {
  1334. t->err = "DERIVE_ERROR";
  1335. goto err;
  1336. }
  1337. if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
  1338. t->err = "DERIVE_ERROR";
  1339. goto err;
  1340. }
  1341. if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
  1342. t->err = "DERIVE_ERROR";
  1343. goto err;
  1344. }
  1345. if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH",
  1346. expected->output, expected->output_len,
  1347. got, got_len))
  1348. goto err;
  1349. t->err = NULL;
  1350. err:
  1351. OPENSSL_free(got);
  1352. return 1;
  1353. }
  1354. static const EVP_TEST_METHOD pderive_test_method = {
  1355. "Derive",
  1356. pderive_test_init,
  1357. pkey_test_cleanup,
  1358. pderive_test_parse,
  1359. pderive_test_run
  1360. };
  1361. /**
  1362. *** PBE TESTS
  1363. **/
  1364. typedef enum pbe_type_enum {
  1365. PBE_TYPE_INVALID = 0,
  1366. PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
  1367. } PBE_TYPE;
  1368. typedef struct pbe_data_st {
  1369. PBE_TYPE pbe_type;
  1370. /* scrypt parameters */
  1371. uint64_t N, r, p, maxmem;
  1372. /* PKCS#12 parameters */
  1373. int id, iter;
  1374. const EVP_MD *md;
  1375. /* password */
  1376. unsigned char *pass;
  1377. size_t pass_len;
  1378. /* salt */
  1379. unsigned char *salt;
  1380. size_t salt_len;
  1381. /* Expected output */
  1382. unsigned char *key;
  1383. size_t key_len;
  1384. } PBE_DATA;
  1385. #ifndef OPENSSL_NO_SCRYPT
  1386. /*
  1387. * Parse unsigned decimal 64 bit integer value
  1388. */
  1389. static int parse_uint64(const char *value, uint64_t *pr)
  1390. {
  1391. const char *p = value;
  1392. if (!TEST_true(*p)) {
  1393. TEST_info("Invalid empty integer value");
  1394. return -1;
  1395. }
  1396. for (*pr = 0; *p; ) {
  1397. if (*pr > UINT64_MAX / 10) {
  1398. TEST_error("Integer overflow in string %s", value);
  1399. return -1;
  1400. }
  1401. *pr *= 10;
  1402. if (!TEST_true(isdigit((unsigned char)*p))) {
  1403. TEST_error("Invalid character in string %s", value);
  1404. return -1;
  1405. }
  1406. *pr += *p - '0';
  1407. p++;
  1408. }
  1409. return 1;
  1410. }
  1411. static int scrypt_test_parse(EVP_TEST *t,
  1412. const char *keyword, const char *value)
  1413. {
  1414. PBE_DATA *pdata = t->data;
  1415. if (strcmp(keyword, "N") == 0)
  1416. return parse_uint64(value, &pdata->N);
  1417. if (strcmp(keyword, "p") == 0)
  1418. return parse_uint64(value, &pdata->p);
  1419. if (strcmp(keyword, "r") == 0)
  1420. return parse_uint64(value, &pdata->r);
  1421. if (strcmp(keyword, "maxmem") == 0)
  1422. return parse_uint64(value, &pdata->maxmem);
  1423. return 0;
  1424. }
  1425. #endif
  1426. static int pbkdf2_test_parse(EVP_TEST *t,
  1427. const char *keyword, const char *value)
  1428. {
  1429. PBE_DATA *pdata = t->data;
  1430. if (strcmp(keyword, "iter") == 0) {
  1431. pdata->iter = atoi(value);
  1432. if (pdata->iter <= 0)
  1433. return -1;
  1434. return 1;
  1435. }
  1436. if (strcmp(keyword, "MD") == 0) {
  1437. pdata->md = EVP_get_digestbyname(value);
  1438. if (pdata->md == NULL)
  1439. return -1;
  1440. return 1;
  1441. }
  1442. return 0;
  1443. }
  1444. static int pkcs12_test_parse(EVP_TEST *t,
  1445. const char *keyword, const char *value)
  1446. {
  1447. PBE_DATA *pdata = t->data;
  1448. if (strcmp(keyword, "id") == 0) {
  1449. pdata->id = atoi(value);
  1450. if (pdata->id <= 0)
  1451. return -1;
  1452. return 1;
  1453. }
  1454. return pbkdf2_test_parse(t, keyword, value);
  1455. }
  1456. static int pbe_test_init(EVP_TEST *t, const char *alg)
  1457. {
  1458. PBE_DATA *pdat;
  1459. PBE_TYPE pbe_type = PBE_TYPE_INVALID;
  1460. if (strcmp(alg, "scrypt") == 0) {
  1461. #ifndef OPENSSL_NO_SCRYPT
  1462. pbe_type = PBE_TYPE_SCRYPT;
  1463. #else
  1464. t->skip = 1;
  1465. return 1;
  1466. #endif
  1467. } else if (strcmp(alg, "pbkdf2") == 0) {
  1468. pbe_type = PBE_TYPE_PBKDF2;
  1469. } else if (strcmp(alg, "pkcs12") == 0) {
  1470. pbe_type = PBE_TYPE_PKCS12;
  1471. } else {
  1472. TEST_error("Unknown pbe algorithm %s", alg);
  1473. }
  1474. pdat = OPENSSL_zalloc(sizeof(*pdat));
  1475. pdat->pbe_type = pbe_type;
  1476. t->data = pdat;
  1477. return 1;
  1478. }
  1479. static void pbe_test_cleanup(EVP_TEST *t)
  1480. {
  1481. PBE_DATA *pdat = t->data;
  1482. OPENSSL_free(pdat->pass);
  1483. OPENSSL_free(pdat->salt);
  1484. OPENSSL_free(pdat->key);
  1485. }
  1486. static int pbe_test_parse(EVP_TEST *t,
  1487. const char *keyword, const char *value)
  1488. {
  1489. PBE_DATA *pdata = t->data;
  1490. if (strcmp(keyword, "Password") == 0)
  1491. return parse_bin(value, &pdata->pass, &pdata->pass_len);
  1492. if (strcmp(keyword, "Salt") == 0)
  1493. return parse_bin(value, &pdata->salt, &pdata->salt_len);
  1494. if (strcmp(keyword, "Key") == 0)
  1495. return parse_bin(value, &pdata->key, &pdata->key_len);
  1496. if (pdata->pbe_type == PBE_TYPE_PBKDF2)
  1497. return pbkdf2_test_parse(t, keyword, value);
  1498. else if (pdata->pbe_type == PBE_TYPE_PKCS12)
  1499. return pkcs12_test_parse(t, keyword, value);
  1500. #ifndef OPENSSL_NO_SCRYPT
  1501. else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
  1502. return scrypt_test_parse(t, keyword, value);
  1503. #endif
  1504. return 0;
  1505. }
  1506. static int pbe_test_run(EVP_TEST *t)
  1507. {
  1508. PBE_DATA *expected = t->data;
  1509. unsigned char *key;
  1510. if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
  1511. t->err = "INTERNAL_ERROR";
  1512. goto err;
  1513. }
  1514. if (expected->pbe_type == PBE_TYPE_PBKDF2) {
  1515. if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
  1516. expected->salt, expected->salt_len,
  1517. expected->iter, expected->md,
  1518. expected->key_len, key) == 0) {
  1519. t->err = "PBKDF2_ERROR";
  1520. goto err;
  1521. }
  1522. #ifndef OPENSSL_NO_SCRYPT
  1523. } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
  1524. if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
  1525. expected->salt, expected->salt_len, expected->N,
  1526. expected->r, expected->p, expected->maxmem,
  1527. key, expected->key_len) == 0) {
  1528. t->err = "SCRYPT_ERROR";
  1529. goto err;
  1530. }
  1531. #endif
  1532. } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
  1533. if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
  1534. expected->salt, expected->salt_len,
  1535. expected->id, expected->iter, expected->key_len,
  1536. key, expected->md) == 0) {
  1537. t->err = "PKCS12_ERROR";
  1538. goto err;
  1539. }
  1540. }
  1541. if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len,
  1542. key, expected->key_len))
  1543. goto err;
  1544. t->err = NULL;
  1545. err:
  1546. OPENSSL_free(key);
  1547. return 1;
  1548. }
  1549. static const EVP_TEST_METHOD pbe_test_method = {
  1550. "PBE",
  1551. pbe_test_init,
  1552. pbe_test_cleanup,
  1553. pbe_test_parse,
  1554. pbe_test_run
  1555. };
  1556. /**
  1557. *** BASE64 TESTS
  1558. **/
  1559. typedef enum {
  1560. BASE64_CANONICAL_ENCODING = 0,
  1561. BASE64_VALID_ENCODING = 1,
  1562. BASE64_INVALID_ENCODING = 2
  1563. } base64_encoding_type;
  1564. typedef struct encode_data_st {
  1565. /* Input to encoding */
  1566. unsigned char *input;
  1567. size_t input_len;
  1568. /* Expected output */
  1569. unsigned char *output;
  1570. size_t output_len;
  1571. base64_encoding_type encoding;
  1572. } ENCODE_DATA;
  1573. static int encode_test_init(EVP_TEST *t, const char *encoding)
  1574. {
  1575. ENCODE_DATA *edata;
  1576. if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
  1577. return 0;
  1578. if (strcmp(encoding, "canonical") == 0) {
  1579. edata->encoding = BASE64_CANONICAL_ENCODING;
  1580. } else if (strcmp(encoding, "valid") == 0) {
  1581. edata->encoding = BASE64_VALID_ENCODING;
  1582. } else if (strcmp(encoding, "invalid") == 0) {
  1583. edata->encoding = BASE64_INVALID_ENCODING;
  1584. if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
  1585. return 0;
  1586. } else {
  1587. TEST_error("Bad encoding: %s."
  1588. " Should be one of {canonical, valid, invalid}",
  1589. encoding);
  1590. return 0;
  1591. }
  1592. t->data = edata;
  1593. return 1;
  1594. }
  1595. static void encode_test_cleanup(EVP_TEST *t)
  1596. {
  1597. ENCODE_DATA *edata = t->data;
  1598. OPENSSL_free(edata->input);
  1599. OPENSSL_free(edata->output);
  1600. memset(edata, 0, sizeof(*edata));
  1601. }
  1602. static int encode_test_parse(EVP_TEST *t,
  1603. const char *keyword, const char *value)
  1604. {
  1605. ENCODE_DATA *edata = t->data;
  1606. if (strcmp(keyword, "Input") == 0)
  1607. return parse_bin(value, &edata->input, &edata->input_len);
  1608. if (strcmp(keyword, "Output") == 0)
  1609. return parse_bin(value, &edata->output, &edata->output_len);
  1610. return 0;
  1611. }
  1612. static int encode_test_run(EVP_TEST *t)
  1613. {
  1614. ENCODE_DATA *expected = t->data;
  1615. unsigned char *encode_out = NULL, *decode_out = NULL;
  1616. int output_len, chunk_len;
  1617. EVP_ENCODE_CTX *decode_ctx;
  1618. if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
  1619. t->err = "INTERNAL_ERROR";
  1620. goto err;
  1621. }
  1622. if (expected->encoding == BASE64_CANONICAL_ENCODING) {
  1623. EVP_ENCODE_CTX *encode_ctx;
  1624. if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
  1625. || !TEST_ptr(encode_out =
  1626. OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
  1627. goto err;
  1628. EVP_EncodeInit(encode_ctx);
  1629. EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
  1630. expected->input, expected->input_len);
  1631. output_len = chunk_len;
  1632. EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
  1633. output_len += chunk_len;
  1634. EVP_ENCODE_CTX_free(encode_ctx);
  1635. if (!memory_err_compare(t, "BAD_ENCODING",
  1636. expected->output, expected->output_len,
  1637. encode_out, output_len))
  1638. goto err;
  1639. }
  1640. if (!TEST_ptr(decode_out =
  1641. OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
  1642. goto err;
  1643. EVP_DecodeInit(decode_ctx);
  1644. if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
  1645. expected->output_len) < 0) {
  1646. t->err = "DECODE_ERROR";
  1647. goto err;
  1648. }
  1649. output_len = chunk_len;
  1650. if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
  1651. t->err = "DECODE_ERROR";
  1652. goto err;
  1653. }
  1654. output_len += chunk_len;
  1655. if (expected->encoding != BASE64_INVALID_ENCODING
  1656. && !memory_err_compare(t, "BAD_DECODING",
  1657. expected->input, expected->input_len,
  1658. decode_out, output_len)) {
  1659. t->err = "BAD_DECODING";
  1660. goto err;
  1661. }
  1662. t->err = NULL;
  1663. err:
  1664. OPENSSL_free(encode_out);
  1665. OPENSSL_free(decode_out);
  1666. EVP_ENCODE_CTX_free(decode_ctx);
  1667. return 1;
  1668. }
  1669. static const EVP_TEST_METHOD encode_test_method = {
  1670. "Encoding",
  1671. encode_test_init,
  1672. encode_test_cleanup,
  1673. encode_test_parse,
  1674. encode_test_run,
  1675. };
  1676. /**
  1677. *** KDF TESTS
  1678. **/
  1679. typedef struct kdf_data_st {
  1680. /* Context for this operation */
  1681. EVP_PKEY_CTX *ctx;
  1682. /* Expected output */
  1683. unsigned char *output;
  1684. size_t output_len;
  1685. } KDF_DATA;
  1686. /*
  1687. * Perform public key operation setup: lookup key, allocated ctx and call
  1688. * the appropriate initialisation function
  1689. */
  1690. static int kdf_test_init(EVP_TEST *t, const char *name)
  1691. {
  1692. KDF_DATA *kdata;
  1693. int kdf_nid = OBJ_sn2nid(name);
  1694. #ifdef OPENSSL_NO_SCRYPT
  1695. if (strcmp(name, "scrypt") == 0) {
  1696. t->skip = 1;
  1697. return 1;
  1698. }
  1699. #endif
  1700. if (kdf_nid == NID_undef)
  1701. kdf_nid = OBJ_ln2nid(name);
  1702. if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
  1703. return 0;
  1704. kdata->ctx = EVP_PKEY_CTX_new_id(kdf_nid, NULL);
  1705. if (kdata->ctx == NULL) {
  1706. OPENSSL_free(kdata);
  1707. return 0;
  1708. }
  1709. if (EVP_PKEY_derive_init(kdata->ctx) <= 0) {
  1710. EVP_PKEY_CTX_free(kdata->ctx);
  1711. OPENSSL_free(kdata);
  1712. return 0;
  1713. }
  1714. t->data = kdata;
  1715. return 1;
  1716. }
  1717. static void kdf_test_cleanup(EVP_TEST *t)
  1718. {
  1719. KDF_DATA *kdata = t->data;
  1720. OPENSSL_free(kdata->output);
  1721. EVP_PKEY_CTX_free(kdata->ctx);
  1722. }
  1723. static int kdf_test_parse(EVP_TEST *t,
  1724. const char *keyword, const char *value)
  1725. {
  1726. KDF_DATA *kdata = t->data;
  1727. if (strcmp(keyword, "Output") == 0)
  1728. return parse_bin(value, &kdata->output, &kdata->output_len);
  1729. if (strncmp(keyword, "Ctrl", 4) == 0)
  1730. return pkey_test_ctrl(t, kdata->ctx, value);
  1731. return 0;
  1732. }
  1733. static int kdf_test_run(EVP_TEST *t)
  1734. {
  1735. KDF_DATA *expected = t->data;
  1736. unsigned char *got = NULL;
  1737. size_t got_len = expected->output_len;
  1738. if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
  1739. t->err = "INTERNAL_ERROR";
  1740. goto err;
  1741. }
  1742. if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
  1743. t->err = "KDF_DERIVE_ERROR";
  1744. goto err;
  1745. }
  1746. if (!memory_err_compare(t, "KDF_MISMATCH",
  1747. expected->output, expected->output_len,
  1748. got, got_len))
  1749. goto err;
  1750. t->err = NULL;
  1751. err:
  1752. OPENSSL_free(got);
  1753. return 1;
  1754. }
  1755. static const EVP_TEST_METHOD kdf_test_method = {
  1756. "KDF",
  1757. kdf_test_init,
  1758. kdf_test_cleanup,
  1759. kdf_test_parse,
  1760. kdf_test_run
  1761. };
  1762. /**
  1763. *** KEYPAIR TESTS
  1764. **/
  1765. typedef struct keypair_test_data_st {
  1766. EVP_PKEY *privk;
  1767. EVP_PKEY *pubk;
  1768. } KEYPAIR_TEST_DATA;
  1769. static int keypair_test_init(EVP_TEST *t, const char *pair)
  1770. {
  1771. KEYPAIR_TEST_DATA *data;
  1772. int rv = 0;
  1773. EVP_PKEY *pk = NULL, *pubk = NULL;
  1774. char *pub, *priv = NULL;
  1775. /* Split private and public names. */
  1776. if (!TEST_ptr(priv = OPENSSL_strdup(pair))
  1777. || !TEST_ptr(pub = strchr(priv, ':'))) {
  1778. t->err = "PARSING_ERROR";
  1779. goto end;
  1780. }
  1781. *pub++ = '\0';
  1782. if (!TEST_true(find_key(&pk, priv, private_keys))) {
  1783. TEST_info("Can't find private key: %s", priv);
  1784. t->err = "MISSING_PRIVATE_KEY";
  1785. goto end;
  1786. }
  1787. if (!TEST_true(find_key(&pubk, pub, public_keys))) {
  1788. TEST_info("Can't find public key: %s", pub);
  1789. t->err = "MISSING_PUBLIC_KEY";
  1790. goto end;
  1791. }
  1792. if (pk == NULL && pubk == NULL) {
  1793. /* Both keys are listed but unsupported: skip this test */
  1794. t->skip = 1;
  1795. rv = 1;
  1796. goto end;
  1797. }
  1798. if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
  1799. goto end;
  1800. data->privk = pk;
  1801. data->pubk = pubk;
  1802. t->data = data;
  1803. rv = 1;
  1804. t->err = NULL;
  1805. end:
  1806. OPENSSL_free(priv);
  1807. return rv;
  1808. }
  1809. static void keypair_test_cleanup(EVP_TEST *t)
  1810. {
  1811. OPENSSL_free(t->data);
  1812. t->data = NULL;
  1813. }
  1814. /*
  1815. * For tests that do not accept any custom keywords.
  1816. */
  1817. static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
  1818. {
  1819. return 0;
  1820. }
  1821. static int keypair_test_run(EVP_TEST *t)
  1822. {
  1823. int rv = 0;
  1824. const KEYPAIR_TEST_DATA *pair = t->data;
  1825. if (pair->privk == NULL || pair->pubk == NULL) {
  1826. /*
  1827. * this can only happen if only one of the keys is not set
  1828. * which means that one of them was unsupported while the
  1829. * other isn't: hence a key type mismatch.
  1830. */
  1831. t->err = "KEYPAIR_TYPE_MISMATCH";
  1832. rv = 1;
  1833. goto end;
  1834. }
  1835. if ((rv = EVP_PKEY_cmp(pair->privk, pair->pubk)) != 1 ) {
  1836. if ( 0 == rv ) {
  1837. t->err = "KEYPAIR_MISMATCH";
  1838. } else if ( -1 == rv ) {
  1839. t->err = "KEYPAIR_TYPE_MISMATCH";
  1840. } else if ( -2 == rv ) {
  1841. t->err = "UNSUPPORTED_KEY_COMPARISON";
  1842. } else {
  1843. TEST_error("Unexpected error in key comparison");
  1844. rv = 0;
  1845. goto end;
  1846. }
  1847. rv = 1;
  1848. goto end;
  1849. }
  1850. rv = 1;
  1851. t->err = NULL;
  1852. end:
  1853. return rv;
  1854. }
  1855. static const EVP_TEST_METHOD keypair_test_method = {
  1856. "PrivPubKeyPair",
  1857. keypair_test_init,
  1858. keypair_test_cleanup,
  1859. void_test_parse,
  1860. keypair_test_run
  1861. };
  1862. /**
  1863. *** KEYGEN TEST
  1864. **/
  1865. typedef struct keygen_test_data_st {
  1866. EVP_PKEY_CTX *genctx; /* Keygen context to use */
  1867. char *keyname; /* Key name to store key or NULL */
  1868. } KEYGEN_TEST_DATA;
  1869. static int keygen_test_init(EVP_TEST *t, const char *alg)
  1870. {
  1871. KEYGEN_TEST_DATA *data;
  1872. EVP_PKEY_CTX *genctx;
  1873. int nid = OBJ_sn2nid(alg);
  1874. if (nid == NID_undef) {
  1875. nid = OBJ_ln2nid(alg);
  1876. if (nid == NID_undef)
  1877. return 0;
  1878. }
  1879. if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_id(nid, NULL))) {
  1880. /* assume algorithm disabled */
  1881. t->skip = 1;
  1882. return 1;
  1883. }
  1884. if (EVP_PKEY_keygen_init(genctx) <= 0) {
  1885. t->err = "KEYGEN_INIT_ERROR";
  1886. goto err;
  1887. }
  1888. if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
  1889. goto err;
  1890. data->genctx = genctx;
  1891. data->keyname = NULL;
  1892. t->data = data;
  1893. t->err = NULL;
  1894. return 1;
  1895. err:
  1896. EVP_PKEY_CTX_free(genctx);
  1897. return 0;
  1898. }
  1899. static void keygen_test_cleanup(EVP_TEST *t)
  1900. {
  1901. KEYGEN_TEST_DATA *keygen = t->data;
  1902. EVP_PKEY_CTX_free(keygen->genctx);
  1903. OPENSSL_free(keygen->keyname);
  1904. OPENSSL_free(t->data);
  1905. t->data = NULL;
  1906. }
  1907. static int keygen_test_parse(EVP_TEST *t,
  1908. const char *keyword, const char *value)
  1909. {
  1910. KEYGEN_TEST_DATA *keygen = t->data;
  1911. if (strcmp(keyword, "KeyName") == 0)
  1912. return TEST_ptr(keygen->keyname = OPENSSL_strdup(value));
  1913. if (strcmp(keyword, "Ctrl") == 0)
  1914. return pkey_test_ctrl(t, keygen->genctx, value);
  1915. return 0;
  1916. }
  1917. static int keygen_test_run(EVP_TEST *t)
  1918. {
  1919. KEYGEN_TEST_DATA *keygen = t->data;
  1920. EVP_PKEY *pkey = NULL;
  1921. t->err = NULL;
  1922. if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) {
  1923. t->err = "KEYGEN_GENERATE_ERROR";
  1924. goto err;
  1925. }
  1926. if (keygen->keyname != NULL) {
  1927. KEY_LIST *key;
  1928. if (find_key(NULL, keygen->keyname, private_keys)) {
  1929. TEST_info("Duplicate key %s", keygen->keyname);
  1930. goto err;
  1931. }
  1932. if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
  1933. goto err;
  1934. key->name = keygen->keyname;
  1935. keygen->keyname = NULL;
  1936. key->key = pkey;
  1937. key->next = private_keys;
  1938. private_keys = key;
  1939. } else {
  1940. EVP_PKEY_free(pkey);
  1941. }
  1942. return 1;
  1943. err:
  1944. EVP_PKEY_free(pkey);
  1945. return 0;
  1946. }
  1947. static const EVP_TEST_METHOD keygen_test_method = {
  1948. "KeyGen",
  1949. keygen_test_init,
  1950. keygen_test_cleanup,
  1951. keygen_test_parse,
  1952. keygen_test_run,
  1953. };
  1954. /**
  1955. *** DIGEST SIGN+VERIFY TESTS
  1956. **/
  1957. typedef struct {
  1958. int is_verify; /* Set to 1 if verifying */
  1959. int is_oneshot; /* Set to 1 for one shot operation */
  1960. const EVP_MD *md; /* Digest to use */
  1961. EVP_MD_CTX *ctx; /* Digest context */
  1962. EVP_PKEY_CTX *pctx;
  1963. STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
  1964. unsigned char *osin; /* Input data if one shot */
  1965. size_t osin_len; /* Input length data if one shot */
  1966. unsigned char *output; /* Expected output */
  1967. size_t output_len; /* Expected output length */
  1968. } DIGESTSIGN_DATA;
  1969. static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
  1970. int is_oneshot)
  1971. {
  1972. const EVP_MD *md = NULL;
  1973. DIGESTSIGN_DATA *mdat;
  1974. if (strcmp(alg, "NULL") != 0) {
  1975. if ((md = EVP_get_digestbyname(alg)) == NULL) {
  1976. /* If alg has an OID assume disabled algorithm */
  1977. if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
  1978. t->skip = 1;
  1979. return 1;
  1980. }
  1981. return 0;
  1982. }
  1983. }
  1984. if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
  1985. return 0;
  1986. mdat->md = md;
  1987. if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
  1988. OPENSSL_free(mdat);
  1989. return 0;
  1990. }
  1991. mdat->is_verify = is_verify;
  1992. mdat->is_oneshot = is_oneshot;
  1993. t->data = mdat;
  1994. return 1;
  1995. }
  1996. static int digestsign_test_init(EVP_TEST *t, const char *alg)
  1997. {
  1998. return digestsigver_test_init(t, alg, 0, 0);
  1999. }
  2000. static void digestsigver_test_cleanup(EVP_TEST *t)
  2001. {
  2002. DIGESTSIGN_DATA *mdata = t->data;
  2003. EVP_MD_CTX_free(mdata->ctx);
  2004. sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
  2005. OPENSSL_free(mdata->osin);
  2006. OPENSSL_free(mdata->output);
  2007. OPENSSL_free(mdata);
  2008. t->data = NULL;
  2009. }
  2010. static int digestsigver_test_parse(EVP_TEST *t,
  2011. const char *keyword, const char *value)
  2012. {
  2013. DIGESTSIGN_DATA *mdata = t->data;
  2014. if (strcmp(keyword, "Key") == 0) {
  2015. EVP_PKEY *pkey = NULL;
  2016. int rv = 0;
  2017. if (mdata->is_verify)
  2018. rv = find_key(&pkey, value, public_keys);
  2019. if (rv == 0)
  2020. rv = find_key(&pkey, value, private_keys);
  2021. if (rv == 0 || pkey == NULL) {
  2022. t->skip = 1;
  2023. return 1;
  2024. }
  2025. if (mdata->is_verify) {
  2026. if (!EVP_DigestVerifyInit(mdata->ctx, &mdata->pctx, mdata->md,
  2027. NULL, pkey))
  2028. t->err = "DIGESTVERIFYINIT_ERROR";
  2029. return 1;
  2030. }
  2031. if (!EVP_DigestSignInit(mdata->ctx, &mdata->pctx, mdata->md, NULL,
  2032. pkey))
  2033. t->err = "DIGESTSIGNINIT_ERROR";
  2034. return 1;
  2035. }
  2036. if (strcmp(keyword, "Input") == 0) {
  2037. if (mdata->is_oneshot)
  2038. return parse_bin(value, &mdata->osin, &mdata->osin_len);
  2039. return evp_test_buffer_append(value, &mdata->input);
  2040. }
  2041. if (strcmp(keyword, "Output") == 0)
  2042. return parse_bin(value, &mdata->output, &mdata->output_len);
  2043. if (!mdata->is_oneshot) {
  2044. if (strcmp(keyword, "Count") == 0)
  2045. return evp_test_buffer_set_count(value, mdata->input);
  2046. if (strcmp(keyword, "Ncopy") == 0)
  2047. return evp_test_buffer_ncopy(value, mdata->input);
  2048. }
  2049. if (strcmp(keyword, "Ctrl") == 0) {
  2050. if (mdata->pctx == NULL)
  2051. return 0;
  2052. return pkey_test_ctrl(t, mdata->pctx, value);
  2053. }
  2054. return 0;
  2055. }
  2056. static int digestsign_update_fn(void *ctx, const unsigned char *buf,
  2057. size_t buflen)
  2058. {
  2059. return EVP_DigestSignUpdate(ctx, buf, buflen);
  2060. }
  2061. static int digestsign_test_run(EVP_TEST *t)
  2062. {
  2063. DIGESTSIGN_DATA *expected = t->data;
  2064. unsigned char *got = NULL;
  2065. size_t got_len;
  2066. if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
  2067. expected->ctx)) {
  2068. t->err = "DIGESTUPDATE_ERROR";
  2069. goto err;
  2070. }
  2071. if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
  2072. t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
  2073. goto err;
  2074. }
  2075. if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
  2076. t->err = "MALLOC_FAILURE";
  2077. goto err;
  2078. }
  2079. if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
  2080. t->err = "DIGESTSIGNFINAL_ERROR";
  2081. goto err;
  2082. }
  2083. if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
  2084. expected->output, expected->output_len,
  2085. got, got_len))
  2086. goto err;
  2087. t->err = NULL;
  2088. err:
  2089. OPENSSL_free(got);
  2090. return 1;
  2091. }
  2092. static const EVP_TEST_METHOD digestsign_test_method = {
  2093. "DigestSign",
  2094. digestsign_test_init,
  2095. digestsigver_test_cleanup,
  2096. digestsigver_test_parse,
  2097. digestsign_test_run
  2098. };
  2099. static int digestverify_test_init(EVP_TEST *t, const char *alg)
  2100. {
  2101. return digestsigver_test_init(t, alg, 1, 0);
  2102. }
  2103. static int digestverify_update_fn(void *ctx, const unsigned char *buf,
  2104. size_t buflen)
  2105. {
  2106. return EVP_DigestVerifyUpdate(ctx, buf, buflen);
  2107. }
  2108. static int digestverify_test_run(EVP_TEST *t)
  2109. {
  2110. DIGESTSIGN_DATA *mdata = t->data;
  2111. if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
  2112. t->err = "DIGESTUPDATE_ERROR";
  2113. return 1;
  2114. }
  2115. if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
  2116. mdata->output_len) <= 0)
  2117. t->err = "VERIFY_ERROR";
  2118. return 1;
  2119. }
  2120. static const EVP_TEST_METHOD digestverify_test_method = {
  2121. "DigestVerify",
  2122. digestverify_test_init,
  2123. digestsigver_test_cleanup,
  2124. digestsigver_test_parse,
  2125. digestverify_test_run
  2126. };
  2127. static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
  2128. {
  2129. return digestsigver_test_init(t, alg, 0, 1);
  2130. }
  2131. static int oneshot_digestsign_test_run(EVP_TEST *t)
  2132. {
  2133. DIGESTSIGN_DATA *expected = t->data;
  2134. unsigned char *got = NULL;
  2135. size_t got_len;
  2136. if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
  2137. expected->osin, expected->osin_len)) {
  2138. t->err = "DIGESTSIGN_LENGTH_ERROR";
  2139. goto err;
  2140. }
  2141. if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
  2142. t->err = "MALLOC_FAILURE";
  2143. goto err;
  2144. }
  2145. if (!EVP_DigestSign(expected->ctx, got, &got_len,
  2146. expected->osin, expected->osin_len)) {
  2147. t->err = "DIGESTSIGN_ERROR";
  2148. goto err;
  2149. }
  2150. if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
  2151. expected->output, expected->output_len,
  2152. got, got_len))
  2153. goto err;
  2154. t->err = NULL;
  2155. err:
  2156. OPENSSL_free(got);
  2157. return 1;
  2158. }
  2159. static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
  2160. "OneShotDigestSign",
  2161. oneshot_digestsign_test_init,
  2162. digestsigver_test_cleanup,
  2163. digestsigver_test_parse,
  2164. oneshot_digestsign_test_run
  2165. };
  2166. static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
  2167. {
  2168. return digestsigver_test_init(t, alg, 1, 1);
  2169. }
  2170. static int oneshot_digestverify_test_run(EVP_TEST *t)
  2171. {
  2172. DIGESTSIGN_DATA *mdata = t->data;
  2173. if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
  2174. mdata->osin, mdata->osin_len) <= 0)
  2175. t->err = "VERIFY_ERROR";
  2176. return 1;
  2177. }
  2178. static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
  2179. "OneShotDigestVerify",
  2180. oneshot_digestverify_test_init,
  2181. digestsigver_test_cleanup,
  2182. digestsigver_test_parse,
  2183. oneshot_digestverify_test_run
  2184. };
  2185. /**
  2186. *** PARSING AND DISPATCH
  2187. **/
  2188. static const EVP_TEST_METHOD *evp_test_list[] = {
  2189. &cipher_test_method,
  2190. &digest_test_method,
  2191. &digestsign_test_method,
  2192. &digestverify_test_method,
  2193. &encode_test_method,
  2194. &kdf_test_method,
  2195. &keypair_test_method,
  2196. &keygen_test_method,
  2197. &mac_test_method,
  2198. &oneshot_digestsign_test_method,
  2199. &oneshot_digestverify_test_method,
  2200. &pbe_test_method,
  2201. &pdecrypt_test_method,
  2202. &pderive_test_method,
  2203. &psign_test_method,
  2204. &pverify_recover_test_method,
  2205. &pverify_test_method,
  2206. NULL
  2207. };
  2208. static const EVP_TEST_METHOD *find_test(const char *name)
  2209. {
  2210. const EVP_TEST_METHOD **tt;
  2211. for (tt = evp_test_list; *tt; tt++) {
  2212. if (strcmp(name, (*tt)->name) == 0)
  2213. return *tt;
  2214. }
  2215. return NULL;
  2216. }
  2217. static void clear_test(EVP_TEST *t)
  2218. {
  2219. test_clearstanza(&t->s);
  2220. ERR_clear_error();
  2221. if (t->data != NULL) {
  2222. if (t->meth != NULL)
  2223. t->meth->cleanup(t);
  2224. OPENSSL_free(t->data);
  2225. t->data = NULL;
  2226. }
  2227. OPENSSL_free(t->expected_err);
  2228. t->expected_err = NULL;
  2229. OPENSSL_free(t->func);
  2230. t->func = NULL;
  2231. OPENSSL_free(t->reason);
  2232. t->reason = NULL;
  2233. /* Text literal. */
  2234. t->err = NULL;
  2235. t->skip = 0;
  2236. t->meth = NULL;
  2237. }
  2238. /*
  2239. * Check for errors in the test structure; return 1 if okay, else 0.
  2240. */
  2241. static int check_test_error(EVP_TEST *t)
  2242. {
  2243. unsigned long err;
  2244. const char *func;
  2245. const char *reason;
  2246. if (t->err == NULL && t->expected_err == NULL)
  2247. return 1;
  2248. if (t->err != NULL && t->expected_err == NULL) {
  2249. if (t->aux_err != NULL) {
  2250. TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
  2251. t->s.test_file, t->s.start, t->aux_err, t->err);
  2252. } else {
  2253. TEST_info("%s:%d: Source of above error; unexpected error %s",
  2254. t->s.test_file, t->s.start, t->err);
  2255. }
  2256. return 0;
  2257. }
  2258. if (t->err == NULL && t->expected_err != NULL) {
  2259. TEST_info("%s:%d: Succeeded but was expecting %s",
  2260. t->s.test_file, t->s.start, t->expected_err);
  2261. return 0;
  2262. }
  2263. if (strcmp(t->err, t->expected_err) != 0) {
  2264. TEST_info("%s:%d: Expected %s got %s",
  2265. t->s.test_file, t->s.start, t->expected_err, t->err);
  2266. return 0;
  2267. }
  2268. if (t->func == NULL && t->reason == NULL)
  2269. return 1;
  2270. if (t->func == NULL || t->reason == NULL) {
  2271. TEST_info("%s:%d: Test is missing function or reason code",
  2272. t->s.test_file, t->s.start);
  2273. return 0;
  2274. }
  2275. err = ERR_peek_error();
  2276. if (err == 0) {
  2277. TEST_info("%s:%d: Expected error \"%s:%s\" not set",
  2278. t->s.test_file, t->s.start, t->func, t->reason);
  2279. return 0;
  2280. }
  2281. func = ERR_func_error_string(err);
  2282. reason = ERR_reason_error_string(err);
  2283. if (func == NULL && reason == NULL) {
  2284. TEST_info("%s:%d: Expected error \"%s:%s\", no strings available."
  2285. " Assuming ok.",
  2286. t->s.test_file, t->s.start, t->func, t->reason);
  2287. return 1;
  2288. }
  2289. if (strcmp(func, t->func) == 0 && strcmp(reason, t->reason) == 0)
  2290. return 1;
  2291. TEST_info("%s:%d: Expected error \"%s:%s\", got \"%s:%s\"",
  2292. t->s.test_file, t->s.start, t->func, t->reason, func, reason);
  2293. return 0;
  2294. }
  2295. /*
  2296. * Run a parsed test. Log a message and return 0 on error.
  2297. */
  2298. static int run_test(EVP_TEST *t)
  2299. {
  2300. if (t->meth == NULL)
  2301. return 1;
  2302. t->s.numtests++;
  2303. if (t->skip) {
  2304. t->s.numskip++;
  2305. } else {
  2306. /* run the test */
  2307. if (t->err == NULL && t->meth->run_test(t) != 1) {
  2308. TEST_info("%s:%d %s error",
  2309. t->s.test_file, t->s.start, t->meth->name);
  2310. return 0;
  2311. }
  2312. if (!check_test_error(t)) {
  2313. TEST_openssl_errors();
  2314. t->s.errors++;
  2315. }
  2316. }
  2317. /* clean it up */
  2318. return 1;
  2319. }
  2320. static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
  2321. {
  2322. for (; lst != NULL; lst = lst->next) {
  2323. if (strcmp(lst->name, name) == 0) {
  2324. if (ppk != NULL)
  2325. *ppk = lst->key;
  2326. return 1;
  2327. }
  2328. }
  2329. return 0;
  2330. }
  2331. static void free_key_list(KEY_LIST *lst)
  2332. {
  2333. while (lst != NULL) {
  2334. KEY_LIST *next = lst->next;
  2335. EVP_PKEY_free(lst->key);
  2336. OPENSSL_free(lst->name);
  2337. OPENSSL_free(lst);
  2338. lst = next;
  2339. }
  2340. }
  2341. /*
  2342. * Is the key type an unsupported algorithm?
  2343. */
  2344. static int key_unsupported(void)
  2345. {
  2346. long err = ERR_peek_error();
  2347. if (ERR_GET_LIB(err) == ERR_LIB_EVP
  2348. && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
  2349. ERR_clear_error();
  2350. return 1;
  2351. }
  2352. #ifndef OPENSSL_NO_EC
  2353. /*
  2354. * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
  2355. * hint to an unsupported algorithm/curve (e.g. if binary EC support is
  2356. * disabled).
  2357. */
  2358. if (ERR_GET_LIB(err) == ERR_LIB_EC
  2359. && ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP) {
  2360. ERR_clear_error();
  2361. return 1;
  2362. }
  2363. #endif /* OPENSSL_NO_EC */
  2364. return 0;
  2365. }
  2366. /*
  2367. * NULL out the value from |pp| but return it. This "steals" a pointer.
  2368. */
  2369. static char *take_value(PAIR *pp)
  2370. {
  2371. char *p = pp->value;
  2372. pp->value = NULL;
  2373. return p;
  2374. }
  2375. /*
  2376. * Read and parse one test. Return 0 if failure, 1 if okay.
  2377. */
  2378. static int parse(EVP_TEST *t)
  2379. {
  2380. KEY_LIST *key, **klist;
  2381. EVP_PKEY *pkey;
  2382. PAIR *pp;
  2383. int i;
  2384. top:
  2385. do {
  2386. if (BIO_eof(t->s.fp))
  2387. return EOF;
  2388. clear_test(t);
  2389. if (!test_readstanza(&t->s))
  2390. return 0;
  2391. } while (t->s.numpairs == 0);
  2392. pp = &t->s.pairs[0];
  2393. /* Are we adding a key? */
  2394. klist = NULL;
  2395. pkey = NULL;
  2396. if (strcmp(pp->key, "PrivateKey") == 0) {
  2397. pkey = PEM_read_bio_PrivateKey(t->s.key, NULL, 0, NULL);
  2398. if (pkey == NULL && !key_unsupported()) {
  2399. EVP_PKEY_free(pkey);
  2400. TEST_info("Can't read private key %s", pp->value);
  2401. TEST_openssl_errors();
  2402. return 0;
  2403. }
  2404. klist = &private_keys;
  2405. } else if (strcmp(pp->key, "PublicKey") == 0) {
  2406. pkey = PEM_read_bio_PUBKEY(t->s.key, NULL, 0, NULL);
  2407. if (pkey == NULL && !key_unsupported()) {
  2408. EVP_PKEY_free(pkey);
  2409. TEST_info("Can't read public key %s", pp->value);
  2410. TEST_openssl_errors();
  2411. return 0;
  2412. }
  2413. klist = &public_keys;
  2414. } else if (strcmp(pp->key, "PrivateKeyRaw") == 0
  2415. || strcmp(pp->key, "PublicKeyRaw") == 0 ) {
  2416. char *strnid = NULL, *keydata = NULL;
  2417. unsigned char *keybin;
  2418. size_t keylen;
  2419. int nid;
  2420. if (strcmp(pp->key, "PrivateKeyRaw") == 0)
  2421. klist = &private_keys;
  2422. else
  2423. klist = &public_keys;
  2424. strnid = strchr(pp->value, ':');
  2425. if (strnid != NULL) {
  2426. *strnid++ = '\0';
  2427. keydata = strchr(strnid, ':');
  2428. if (keydata != NULL)
  2429. *keydata++ = '\0';
  2430. }
  2431. if (keydata == NULL) {
  2432. TEST_info("Failed to parse %s value", pp->key);
  2433. return 0;
  2434. }
  2435. nid = OBJ_txt2nid(strnid);
  2436. if (nid == NID_undef) {
  2437. TEST_info("Uncrecognised algorithm NID");
  2438. return 0;
  2439. }
  2440. if (!parse_bin(keydata, &keybin, &keylen)) {
  2441. TEST_info("Failed to create binary key");
  2442. return 0;
  2443. }
  2444. if (klist == &private_keys)
  2445. pkey = EVP_PKEY_new_raw_private_key(nid, NULL, keybin, keylen);
  2446. else
  2447. pkey = EVP_PKEY_new_raw_public_key(nid, NULL, keybin, keylen);
  2448. if (pkey == NULL && !key_unsupported()) {
  2449. TEST_info("Can't read %s data", pp->key);
  2450. OPENSSL_free(keybin);
  2451. TEST_openssl_errors();
  2452. return 0;
  2453. }
  2454. OPENSSL_free(keybin);
  2455. }
  2456. /* If we have a key add to list */
  2457. if (klist != NULL) {
  2458. if (find_key(NULL, pp->value, *klist)) {
  2459. TEST_info("Duplicate key %s", pp->value);
  2460. return 0;
  2461. }
  2462. if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
  2463. return 0;
  2464. key->name = take_value(pp);
  2465. /* Hack to detect SM2 keys */
  2466. if(pkey != NULL && strstr(key->name, "SM2") != NULL) {
  2467. #ifdef OPENSSL_NO_SM2
  2468. EVP_PKEY_free(pkey);
  2469. pkey = NULL;
  2470. #else
  2471. EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2);
  2472. #endif
  2473. }
  2474. key->key = pkey;
  2475. key->next = *klist;
  2476. *klist = key;
  2477. /* Go back and start a new stanza. */
  2478. if (t->s.numpairs != 1)
  2479. TEST_info("Line %d: missing blank line\n", t->s.curr);
  2480. goto top;
  2481. }
  2482. /* Find the test, based on first keyword. */
  2483. if (!TEST_ptr(t->meth = find_test(pp->key)))
  2484. return 0;
  2485. if (!t->meth->init(t, pp->value)) {
  2486. TEST_error("unknown %s: %s\n", pp->key, pp->value);
  2487. return 0;
  2488. }
  2489. if (t->skip == 1) {
  2490. /* TEST_info("skipping %s %s", pp->key, pp->value); */
  2491. return 0;
  2492. }
  2493. for (pp++, i = 1; i < t->s.numpairs; pp++, i++) {
  2494. if (strcmp(pp->key, "Result") == 0) {
  2495. if (t->expected_err != NULL) {
  2496. TEST_info("Line %d: multiple result lines", t->s.curr);
  2497. return 0;
  2498. }
  2499. t->expected_err = take_value(pp);
  2500. } else if (strcmp(pp->key, "Function") == 0) {
  2501. if (t->func != NULL) {
  2502. TEST_info("Line %d: multiple function lines\n", t->s.curr);
  2503. return 0;
  2504. }
  2505. t->func = take_value(pp);
  2506. } else if (strcmp(pp->key, "Reason") == 0) {
  2507. if (t->reason != NULL) {
  2508. TEST_info("Line %d: multiple reason lines", t->s.curr);
  2509. return 0;
  2510. }
  2511. t->reason = take_value(pp);
  2512. } else {
  2513. /* Must be test specific line: try to parse it */
  2514. int rv = t->meth->parse(t, pp->key, pp->value);
  2515. if (rv == 0) {
  2516. TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key);
  2517. return 0;
  2518. }
  2519. if (rv < 0) {
  2520. TEST_info("Line %d: error processing keyword %s = %s\n",
  2521. t->s.curr, pp->key, pp->value);
  2522. return 0;
  2523. }
  2524. }
  2525. }
  2526. return 1;
  2527. }
  2528. static int run_file_tests(int i)
  2529. {
  2530. EVP_TEST *t;
  2531. const char *testfile = test_get_argument(i);
  2532. int c;
  2533. if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t))))
  2534. return 0;
  2535. if (!test_start_file(&t->s, testfile)) {
  2536. OPENSSL_free(t);
  2537. return 0;
  2538. }
  2539. while (!BIO_eof(t->s.fp)) {
  2540. c = parse(t);
  2541. if (t->skip)
  2542. continue;
  2543. if (c == 0 || !run_test(t)) {
  2544. t->s.errors++;
  2545. break;
  2546. }
  2547. }
  2548. test_end_file(&t->s);
  2549. clear_test(t);
  2550. free_key_list(public_keys);
  2551. free_key_list(private_keys);
  2552. BIO_free(t->s.key);
  2553. c = t->s.errors;
  2554. OPENSSL_free(t);
  2555. return c == 0;
  2556. }
  2557. int setup_tests(void)
  2558. {
  2559. size_t n = test_get_argument_count();
  2560. if (n == 0) {
  2561. TEST_error("Usage: %s file...", test_get_program_name());
  2562. return 0;
  2563. }
  2564. ADD_ALL_TESTS(run_file_tests, n);
  2565. return 1;
  2566. }