evp_test.c 77 KB

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