evp_test.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. /* evp_test.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  4. * project.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. */
  54. #include <stdio.h>
  55. #include <string.h>
  56. #include <stdlib.h>
  57. #include <ctype.h>
  58. #include <openssl/evp.h>
  59. #include <openssl/pem.h>
  60. #include <openssl/err.h>
  61. #include <openssl/x509v3.h>
  62. /* Remove spaces from beginning and end of a string */
  63. static void remove_space(char **pval)
  64. {
  65. unsigned char *p = (unsigned char *)*pval;
  66. while (isspace(*p))
  67. p++;
  68. *pval = (char *)p;
  69. p = p + strlen(*pval) - 1;
  70. /* Remove trailing space */
  71. while (isspace(*p))
  72. *p-- = 0;
  73. }
  74. /*
  75. * Given a line of the form:
  76. * name = value # comment
  77. * extract name and value. NB: modifies passed buffer.
  78. */
  79. static int parse_line(char **pkw, char **pval, char *linebuf)
  80. {
  81. char *p;
  82. p = linebuf + strlen(linebuf) - 1;
  83. if (*p != '\n') {
  84. fprintf(stderr, "FATAL: missing EOL\n");
  85. exit(1);
  86. }
  87. /* Look for # */
  88. p = strchr(linebuf, '#');
  89. if (p)
  90. *p = '\0';
  91. /* Look for = sign */
  92. p = strchr(linebuf, '=');
  93. /* If no '=' exit */
  94. if (!p)
  95. return 0;
  96. *p++ = '\0';
  97. *pkw = linebuf;
  98. *pval = p;
  99. /* Remove spaces from keyword and value */
  100. remove_space(pkw);
  101. remove_space(pval);
  102. return 1;
  103. }
  104. /* For a hex string "value" convert to a binary allocated buffer */
  105. static int test_bin(const char *value, unsigned char **buf, size_t *buflen)
  106. {
  107. long len;
  108. if (!*value) {
  109. /* Don't return NULL for zero length buffer */
  110. *buf = OPENSSL_malloc(1);
  111. if (!*buf)
  112. return 0;
  113. **buf = 0;
  114. *buflen = 0;
  115. return 1;
  116. }
  117. /* Check for string literal */
  118. if (value[0] == '"') {
  119. size_t vlen;
  120. value++;
  121. vlen = strlen(value);
  122. if (value[vlen - 1] != '"')
  123. return 0;
  124. vlen--;
  125. *buf = BUF_memdup(value, vlen);
  126. *buflen = vlen;
  127. return 1;
  128. }
  129. *buf = string_to_hex(value, &len);
  130. if (!*buf) {
  131. fprintf(stderr, "Value=%s\n", value);
  132. ERR_print_errors_fp(stderr);
  133. return -1;
  134. }
  135. /* Size of input buffer means we'll never overflow */
  136. *buflen = len;
  137. return 1;
  138. }
  139. /* Structure holding test information */
  140. struct evp_test {
  141. /* file being read */
  142. FILE *in;
  143. /* List of public and private keys */
  144. struct key_list *private;
  145. struct key_list *public;
  146. /* method for this test */
  147. const struct evp_test_method *meth;
  148. /* current line being processed */
  149. unsigned int line;
  150. /* start line of current test */
  151. unsigned int start_line;
  152. /* Error string for test */
  153. const char *err;
  154. /* Expected error value of test */
  155. char *expected_err;
  156. /* Number of tests */
  157. int ntests;
  158. /* Error count */
  159. int errors;
  160. /* Number of tests skipped */
  161. int nskip;
  162. /* If output mismatch expected and got value */
  163. unsigned char *out_got;
  164. unsigned char *out_expected;
  165. size_t out_len;
  166. /* test specific data */
  167. void *data;
  168. /* Current test should be skipped */
  169. int skip;
  170. };
  171. struct key_list {
  172. char *name;
  173. EVP_PKEY *key;
  174. struct key_list *next;
  175. };
  176. /* Test method structure */
  177. struct evp_test_method {
  178. /* Name of test as it appears in file */
  179. const char *name;
  180. /* Initialise test for "alg" */
  181. int (*init) (struct evp_test * t, const char *alg);
  182. /* Clean up method */
  183. void (*cleanup) (struct evp_test * t);
  184. /* Test specific name value pair processing */
  185. int (*parse) (struct evp_test * t, const char *name, const char *value);
  186. /* Run the test itself */
  187. int (*run_test) (struct evp_test * t);
  188. };
  189. static const struct evp_test_method digest_test_method, cipher_test_method;
  190. static const struct evp_test_method mac_test_method;
  191. static const struct evp_test_method psign_test_method, pverify_test_method;
  192. static const struct evp_test_method pdecrypt_test_method;
  193. static const struct evp_test_method pverify_recover_test_method;
  194. static const struct evp_test_method *evp_test_list[] = {
  195. &digest_test_method,
  196. &cipher_test_method,
  197. &mac_test_method,
  198. &psign_test_method,
  199. &pverify_test_method,
  200. &pdecrypt_test_method,
  201. &pverify_recover_test_method,
  202. NULL
  203. };
  204. static const struct evp_test_method *evp_find_test(const char *name)
  205. {
  206. const struct evp_test_method **tt;
  207. for (tt = evp_test_list; *tt; tt++) {
  208. if (strcmp(name, (*tt)->name) == 0)
  209. return *tt;
  210. }
  211. return NULL;
  212. }
  213. static void hex_print(const char *name, const unsigned char *buf, size_t len)
  214. {
  215. size_t i;
  216. fprintf(stderr, "%s ", name);
  217. for (i = 0; i < len; i++)
  218. fprintf(stderr, "%02X", buf[i]);
  219. fputs("\n", stderr);
  220. }
  221. static void free_expected(struct evp_test *t)
  222. {
  223. OPENSSL_free(t->expected_err);
  224. t->expected_err = NULL;
  225. OPENSSL_free(t->out_expected);
  226. OPENSSL_free(t->out_got);
  227. t->out_expected = NULL;
  228. t->out_got = NULL;
  229. }
  230. static void print_expected(struct evp_test *t)
  231. {
  232. if (t->out_expected == NULL)
  233. return;
  234. hex_print("Expected:", t->out_expected, t->out_len);
  235. hex_print("Got: ", t->out_got, t->out_len);
  236. free_expected(t);
  237. }
  238. static int check_test_error(struct evp_test *t)
  239. {
  240. if (!t->err && !t->expected_err)
  241. return 1;
  242. if (t->err && !t->expected_err) {
  243. fprintf(stderr, "Test line %d: unexpected error %s\n",
  244. t->start_line, t->err);
  245. print_expected(t);
  246. return 0;
  247. }
  248. if (!t->err && t->expected_err) {
  249. fprintf(stderr, "Test line %d: succeeded expecting %s\n",
  250. t->start_line, t->expected_err);
  251. return 0;
  252. }
  253. if (strcmp(t->err, t->expected_err) == 0)
  254. return 1;
  255. fprintf(stderr, "Test line %d: expecting %s got %s\n",
  256. t->start_line, t->expected_err, t->err);
  257. return 0;
  258. }
  259. /* Setup a new test, run any existing test */
  260. static int setup_test(struct evp_test *t, const struct evp_test_method *tmeth)
  261. {
  262. /* If we already have a test set up run it */
  263. if (t->meth) {
  264. t->ntests++;
  265. if (t->skip) {
  266. t->meth = tmeth;
  267. t->nskip++;
  268. return 1;
  269. }
  270. t->err = NULL;
  271. if (t->meth->run_test(t) != 1) {
  272. fprintf(stderr, "%s test error line %d\n",
  273. t->meth->name, t->start_line);
  274. return 0;
  275. }
  276. if (!check_test_error(t)) {
  277. if (t->err)
  278. ERR_print_errors_fp(stderr);
  279. t->errors++;
  280. }
  281. ERR_clear_error();
  282. t->meth->cleanup(t);
  283. OPENSSL_free(t->data);
  284. t->data = NULL;
  285. OPENSSL_free(t->expected_err);
  286. t->expected_err = NULL;
  287. free_expected(t);
  288. }
  289. t->meth = tmeth;
  290. return 1;
  291. }
  292. static int find_key(EVP_PKEY **ppk, const char *name, struct key_list *lst)
  293. {
  294. for (; lst; lst = lst->next) {
  295. if (strcmp(lst->name, name) == 0) {
  296. if (ppk)
  297. *ppk = lst->key;
  298. return 1;
  299. }
  300. }
  301. return 0;
  302. }
  303. static void free_key_list(struct key_list *lst)
  304. {
  305. while (lst != NULL) {
  306. struct key_list *ltmp;
  307. EVP_PKEY_free(lst->key);
  308. OPENSSL_free(lst->name);
  309. ltmp = lst->next;
  310. OPENSSL_free(lst);
  311. lst = ltmp;
  312. }
  313. }
  314. static int check_unsupported()
  315. {
  316. long err = ERR_peek_error();
  317. if (ERR_GET_LIB(err) == ERR_LIB_EVP
  318. && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
  319. ERR_clear_error();
  320. return 1;
  321. }
  322. return 0;
  323. }
  324. static int process_test(struct evp_test *t, char *buf, int verbose)
  325. {
  326. char *keyword = NULL, *value = NULL;
  327. int rv = 0, add_key = 0;
  328. long save_pos = 0;
  329. struct key_list **lst = NULL, *key = NULL;
  330. EVP_PKEY *pk = NULL;
  331. const struct evp_test_method *tmeth = NULL;
  332. if (verbose)
  333. fputs(buf, stdout);
  334. if (!parse_line(&keyword, &value, buf))
  335. return 1;
  336. if (strcmp(keyword, "PrivateKey") == 0) {
  337. save_pos = ftell(t->in);
  338. pk = PEM_read_PrivateKey(t->in, NULL, 0, NULL);
  339. if (pk == NULL && !check_unsupported()) {
  340. fprintf(stderr, "Error reading private key %s\n", value);
  341. ERR_print_errors_fp(stderr);
  342. return 0;
  343. }
  344. lst = &t->private;
  345. add_key = 1;
  346. }
  347. if (strcmp(keyword, "PublicKey") == 0) {
  348. save_pos = ftell(t->in);
  349. pk = PEM_read_PUBKEY(t->in, NULL, 0, NULL);
  350. if (pk == NULL && !check_unsupported()) {
  351. fprintf(stderr, "Error reading public key %s\n", value);
  352. ERR_print_errors_fp(stderr);
  353. return 0;
  354. }
  355. lst = &t->public;
  356. add_key = 1;
  357. }
  358. /* If we have a key add to list */
  359. if (add_key) {
  360. char tmpbuf[80];
  361. if (find_key(NULL, value, *lst)) {
  362. fprintf(stderr, "Duplicate key %s\n", value);
  363. return 0;
  364. }
  365. key = OPENSSL_malloc(sizeof(*key));
  366. if (!key)
  367. return 0;
  368. key->name = BUF_strdup(value);
  369. key->key = pk;
  370. key->next = *lst;
  371. *lst = key;
  372. /* Rewind input, read to end and update line numbers */
  373. fseek(t->in, save_pos, SEEK_SET);
  374. while (fgets(tmpbuf, sizeof(tmpbuf), t->in)) {
  375. t->line++;
  376. if (strncmp(tmpbuf, "-----END", 8) == 0)
  377. return 1;
  378. }
  379. fprintf(stderr, "Can't find key end\n");
  380. return 0;
  381. }
  382. /* See if keyword corresponds to a test start */
  383. tmeth = evp_find_test(keyword);
  384. if (tmeth) {
  385. if (!setup_test(t, tmeth))
  386. return 0;
  387. t->start_line = t->line;
  388. t->skip = 0;
  389. if (!tmeth->init(t, value)) {
  390. fprintf(stderr, "Unknown %s: %s\n", keyword, value);
  391. return 0;
  392. }
  393. return 1;
  394. } else if (t->skip) {
  395. return 1;
  396. } else if (strcmp(keyword, "Result") == 0) {
  397. if (t->expected_err) {
  398. fprintf(stderr, "Line %d: multiple result lines\n", t->line);
  399. return 0;
  400. }
  401. t->expected_err = BUF_strdup(value);
  402. if (!t->expected_err)
  403. return 0;
  404. } else {
  405. /* Must be test specific line: try to parse it */
  406. if (t->meth)
  407. rv = t->meth->parse(t, keyword, value);
  408. if (rv == 0)
  409. fprintf(stderr, "line %d: unexpected keyword %s\n",
  410. t->line, keyword);
  411. if (rv < 0)
  412. fprintf(stderr, "line %d: error processing keyword %s\n",
  413. t->line, keyword);
  414. if (rv <= 0)
  415. return 0;
  416. }
  417. return 1;
  418. }
  419. static int check_output(struct evp_test *t, const unsigned char *expected,
  420. const unsigned char *got, size_t len)
  421. {
  422. if (!memcmp(expected, got, len))
  423. return 0;
  424. t->out_expected = BUF_memdup(expected, len);
  425. t->out_got = BUF_memdup(got, len);
  426. t->out_len = len;
  427. if (t->out_expected == NULL || t->out_got == NULL) {
  428. fprintf(stderr, "Memory allocation error!\n");
  429. exit(1);
  430. }
  431. return 1;
  432. }
  433. int main(int argc, char **argv)
  434. {
  435. FILE *in = NULL;
  436. char buf[10240];
  437. struct evp_test t;
  438. if (argc != 2) {
  439. fprintf(stderr, "usage: evp_test testfile.txt\n");
  440. return 1;
  441. }
  442. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  443. ERR_load_crypto_strings();
  444. OpenSSL_add_all_algorithms();
  445. memset(&t, 0, sizeof(t));
  446. t.meth = NULL;
  447. t.public = NULL;
  448. t.private = NULL;
  449. t.err = NULL;
  450. t.line = 0;
  451. t.start_line = -1;
  452. t.errors = 0;
  453. t.ntests = 0;
  454. t.out_expected = NULL;
  455. t.out_got = NULL;
  456. t.out_len = 0;
  457. in = fopen(argv[1], "r");
  458. t.in = in;
  459. while (fgets(buf, sizeof(buf), in)) {
  460. t.line++;
  461. if (!process_test(&t, buf, 0))
  462. exit(1);
  463. }
  464. /* Run any final test we have */
  465. if (!setup_test(&t, NULL))
  466. exit(1);
  467. fprintf(stderr, "%d tests completed with %d errors, %d skipped\n",
  468. t.ntests, t.errors, t.nskip);
  469. free_key_list(t.public);
  470. free_key_list(t.private);
  471. fclose(in);
  472. EVP_cleanup();
  473. CRYPTO_cleanup_all_ex_data();
  474. ERR_remove_thread_state(NULL);
  475. ERR_free_strings();
  476. CRYPTO_mem_leaks_fp(stderr);
  477. if (t.errors)
  478. return 1;
  479. return 0;
  480. }
  481. static void test_free(void *d)
  482. {
  483. OPENSSL_free(d);
  484. }
  485. /* Message digest tests */
  486. struct digest_data {
  487. /* Digest this test is for */
  488. const EVP_MD *digest;
  489. /* Input to digest */
  490. unsigned char *input;
  491. size_t input_len;
  492. /* Repeat count for input */
  493. size_t nrpt;
  494. /* Expected output */
  495. unsigned char *output;
  496. size_t output_len;
  497. };
  498. static int digest_test_init(struct evp_test *t, const char *alg)
  499. {
  500. const EVP_MD *digest;
  501. struct digest_data *mdat = t->data;
  502. digest = EVP_get_digestbyname(alg);
  503. if (!digest) {
  504. /* If alg has an OID assume disabled algorithm */
  505. if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
  506. t->skip = 1;
  507. return 1;
  508. }
  509. return 0;
  510. }
  511. mdat = OPENSSL_malloc(sizeof(*mdat));
  512. mdat->digest = digest;
  513. mdat->input = NULL;
  514. mdat->output = NULL;
  515. mdat->nrpt = 1;
  516. t->data = mdat;
  517. return 1;
  518. }
  519. static void digest_test_cleanup(struct evp_test *t)
  520. {
  521. struct digest_data *mdat = t->data;
  522. test_free(mdat->input);
  523. test_free(mdat->output);
  524. }
  525. static int digest_test_parse(struct evp_test *t,
  526. const char *keyword, const char *value)
  527. {
  528. struct digest_data *mdata = t->data;
  529. if (strcmp(keyword, "Input") == 0)
  530. return test_bin(value, &mdata->input, &mdata->input_len);
  531. if (strcmp(keyword, "Output") == 0)
  532. return test_bin(value, &mdata->output, &mdata->output_len);
  533. if (strcmp(keyword, "Count") == 0) {
  534. long nrpt = atoi(value);
  535. if (nrpt <= 0)
  536. return 0;
  537. mdata->nrpt = (size_t)nrpt;
  538. return 1;
  539. }
  540. return 0;
  541. }
  542. static int digest_test_run(struct evp_test *t)
  543. {
  544. struct digest_data *mdata = t->data;
  545. size_t i;
  546. const char *err = "INTERNAL_ERROR";
  547. EVP_MD_CTX *mctx;
  548. unsigned char md[EVP_MAX_MD_SIZE];
  549. unsigned int md_len;
  550. mctx = EVP_MD_CTX_create();
  551. if (!mctx)
  552. goto err;
  553. err = "DIGESTINIT_ERROR";
  554. if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))
  555. goto err;
  556. err = "DIGESTUPDATE_ERROR";
  557. for (i = 0; i < mdata->nrpt; i++) {
  558. if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))
  559. goto err;
  560. }
  561. err = "DIGESTFINAL_ERROR";
  562. if (!EVP_DigestFinal(mctx, md, &md_len))
  563. goto err;
  564. err = "DIGEST_LENGTH_MISMATCH";
  565. if (md_len != mdata->output_len)
  566. goto err;
  567. err = "DIGEST_MISMATCH";
  568. if (check_output(t, mdata->output, md, md_len))
  569. goto err;
  570. err = NULL;
  571. err:
  572. if (mctx)
  573. EVP_MD_CTX_destroy(mctx);
  574. t->err = err;
  575. return 1;
  576. }
  577. static const struct evp_test_method digest_test_method = {
  578. "Digest",
  579. digest_test_init,
  580. digest_test_cleanup,
  581. digest_test_parse,
  582. digest_test_run
  583. };
  584. /* Cipher tests */
  585. struct cipher_data {
  586. const EVP_CIPHER *cipher;
  587. int enc;
  588. /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
  589. int aead;
  590. unsigned char *key;
  591. size_t key_len;
  592. unsigned char *iv;
  593. size_t iv_len;
  594. unsigned char *plaintext;
  595. size_t plaintext_len;
  596. unsigned char *ciphertext;
  597. size_t ciphertext_len;
  598. /* GCM, CCM only */
  599. unsigned char *aad;
  600. size_t aad_len;
  601. unsigned char *tag;
  602. size_t tag_len;
  603. };
  604. static int cipher_test_init(struct evp_test *t, const char *alg)
  605. {
  606. const EVP_CIPHER *cipher;
  607. struct cipher_data *cdat = t->data;
  608. cipher = EVP_get_cipherbyname(alg);
  609. if (!cipher) {
  610. /* If alg has an OID assume disabled algorithm */
  611. if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
  612. t->skip = 1;
  613. return 1;
  614. }
  615. return 0;
  616. }
  617. cdat = OPENSSL_malloc(sizeof(*cdat));
  618. cdat->cipher = cipher;
  619. cdat->enc = -1;
  620. cdat->key = NULL;
  621. cdat->iv = NULL;
  622. cdat->ciphertext = NULL;
  623. cdat->plaintext = NULL;
  624. cdat->aad = NULL;
  625. cdat->tag = NULL;
  626. t->data = cdat;
  627. if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
  628. || EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE
  629. || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
  630. cdat->aead = EVP_CIPHER_mode(cipher);
  631. else
  632. cdat->aead = 0;
  633. return 1;
  634. }
  635. static void cipher_test_cleanup(struct evp_test *t)
  636. {
  637. struct cipher_data *cdat = t->data;
  638. test_free(cdat->key);
  639. test_free(cdat->iv);
  640. test_free(cdat->ciphertext);
  641. test_free(cdat->plaintext);
  642. test_free(cdat->aad);
  643. test_free(cdat->tag);
  644. }
  645. static int cipher_test_parse(struct evp_test *t, const char *keyword,
  646. const char *value)
  647. {
  648. struct cipher_data *cdat = t->data;
  649. if (strcmp(keyword, "Key") == 0)
  650. return test_bin(value, &cdat->key, &cdat->key_len);
  651. if (strcmp(keyword, "IV") == 0)
  652. return test_bin(value, &cdat->iv, &cdat->iv_len);
  653. if (strcmp(keyword, "Plaintext") == 0)
  654. return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
  655. if (strcmp(keyword, "Ciphertext") == 0)
  656. return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
  657. if (cdat->aead) {
  658. if (strcmp(keyword, "AAD") == 0)
  659. return test_bin(value, &cdat->aad, &cdat->aad_len);
  660. if (strcmp(keyword, "Tag") == 0)
  661. return test_bin(value, &cdat->tag, &cdat->tag_len);
  662. }
  663. if (strcmp(keyword, "Operation") == 0) {
  664. if (strcmp(value, "ENCRYPT") == 0)
  665. cdat->enc = 1;
  666. else if (strcmp(value, "DECRYPT") == 0)
  667. cdat->enc = 0;
  668. else
  669. return 0;
  670. return 1;
  671. }
  672. return 0;
  673. }
  674. static int cipher_test_enc(struct evp_test *t, int enc)
  675. {
  676. struct cipher_data *cdat = t->data;
  677. unsigned char *in, *out, *tmp = NULL;
  678. size_t in_len, out_len;
  679. int tmplen, tmpflen;
  680. EVP_CIPHER_CTX *ctx = NULL;
  681. const char *err;
  682. err = "INTERNAL_ERROR";
  683. ctx = EVP_CIPHER_CTX_new();
  684. if (!ctx)
  685. goto err;
  686. EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
  687. if (enc) {
  688. in = cdat->plaintext;
  689. in_len = cdat->plaintext_len;
  690. out = cdat->ciphertext;
  691. out_len = cdat->ciphertext_len;
  692. } else {
  693. in = cdat->ciphertext;
  694. in_len = cdat->ciphertext_len;
  695. out = cdat->plaintext;
  696. out_len = cdat->plaintext_len;
  697. }
  698. tmp = OPENSSL_malloc(in_len + 2 * EVP_MAX_BLOCK_LENGTH);
  699. if (!tmp)
  700. goto err;
  701. err = "CIPHERINIT_ERROR";
  702. if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
  703. goto err;
  704. err = "INVALID_IV_LENGTH";
  705. if (cdat->iv) {
  706. if (cdat->aead) {
  707. if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  708. cdat->iv_len, 0))
  709. goto err;
  710. } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
  711. goto err;
  712. }
  713. if (cdat->aead) {
  714. unsigned char *tag;
  715. /*
  716. * If encrypting or OCB just set tag length initially, otherwise
  717. * set tag length and value.
  718. */
  719. if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {
  720. err = "TAG_LENGTH_SET_ERROR";
  721. tag = NULL;
  722. } else {
  723. err = "TAG_SET_ERROR";
  724. tag = cdat->tag;
  725. }
  726. if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {
  727. if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  728. cdat->tag_len, tag))
  729. goto err;
  730. }
  731. }
  732. err = "INVALID_KEY_LENGTH";
  733. if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len))
  734. goto err;
  735. err = "KEY_SET_ERROR";
  736. if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1))
  737. goto err;
  738. if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {
  739. if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  740. cdat->tag_len, cdat->tag)) {
  741. err = "TAG_SET_ERROR";
  742. goto err;
  743. }
  744. }
  745. if (cdat->aead == EVP_CIPH_CCM_MODE) {
  746. if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
  747. err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
  748. goto err;
  749. }
  750. }
  751. if (cdat->aad) {
  752. if (!EVP_CipherUpdate(ctx, NULL, &tmplen, cdat->aad, cdat->aad_len)) {
  753. err = "AAD_SET_ERROR";
  754. goto err;
  755. }
  756. }
  757. EVP_CIPHER_CTX_set_padding(ctx, 0);
  758. err = "CIPHERUPDATE_ERROR";
  759. if (!EVP_CipherUpdate(ctx, tmp, &tmplen, in, in_len))
  760. goto err;
  761. if (cdat->aead == EVP_CIPH_CCM_MODE)
  762. tmpflen = 0;
  763. else {
  764. err = "CIPHERFINAL_ERROR";
  765. if (!EVP_CipherFinal_ex(ctx, tmp + tmplen, &tmpflen))
  766. goto err;
  767. }
  768. err = "LENGTH_MISMATCH";
  769. if (out_len != (size_t)(tmplen + tmpflen))
  770. goto err;
  771. err = "VALUE_MISMATCH";
  772. if (check_output(t, out, tmp, out_len))
  773. goto err;
  774. if (enc && cdat->aead) {
  775. unsigned char rtag[16];
  776. if (cdat->tag_len > sizeof(rtag)) {
  777. err = "TAG_LENGTH_INTERNAL_ERROR";
  778. goto err;
  779. }
  780. if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  781. cdat->tag_len, rtag)) {
  782. err = "TAG_RETRIEVE_ERROR";
  783. goto err;
  784. }
  785. if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {
  786. err = "TAG_VALUE_MISMATCH";
  787. goto err;
  788. }
  789. }
  790. err = NULL;
  791. err:
  792. OPENSSL_free(tmp);
  793. EVP_CIPHER_CTX_free(ctx);
  794. t->err = err;
  795. return err ? 0 : 1;
  796. }
  797. static int cipher_test_run(struct evp_test *t)
  798. {
  799. struct cipher_data *cdat = t->data;
  800. int rv;
  801. if (!cdat->key) {
  802. t->err = "NO_KEY";
  803. return 0;
  804. }
  805. if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
  806. /* IV is optional and usually omitted in wrap mode */
  807. if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
  808. t->err = "NO_IV";
  809. return 0;
  810. }
  811. }
  812. if (cdat->aead && !cdat->tag) {
  813. t->err = "NO_TAG";
  814. return 0;
  815. }
  816. if (cdat->enc) {
  817. rv = cipher_test_enc(t, 1);
  818. /* Not fatal errors: return */
  819. if (rv != 1) {
  820. if (rv < 0)
  821. return 0;
  822. return 1;
  823. }
  824. }
  825. if (cdat->enc != 1) {
  826. rv = cipher_test_enc(t, 0);
  827. /* Not fatal errors: return */
  828. if (rv != 1) {
  829. if (rv < 0)
  830. return 0;
  831. return 1;
  832. }
  833. }
  834. return 1;
  835. }
  836. static const struct evp_test_method cipher_test_method = {
  837. "Cipher",
  838. cipher_test_init,
  839. cipher_test_cleanup,
  840. cipher_test_parse,
  841. cipher_test_run
  842. };
  843. struct mac_data {
  844. /* MAC type */
  845. int type;
  846. /* Algorithm string for this MAC */
  847. char *alg;
  848. /* MAC key */
  849. unsigned char *key;
  850. size_t key_len;
  851. /* Input to MAC */
  852. unsigned char *input;
  853. size_t input_len;
  854. /* Expected output */
  855. unsigned char *output;
  856. size_t output_len;
  857. };
  858. static int mac_test_init(struct evp_test *t, const char *alg)
  859. {
  860. int type;
  861. struct mac_data *mdat;
  862. if (strcmp(alg, "HMAC") == 0)
  863. type = EVP_PKEY_HMAC;
  864. else if (strcmp(alg, "CMAC") == 0)
  865. type = EVP_PKEY_CMAC;
  866. else
  867. return 0;
  868. mdat = OPENSSL_malloc(sizeof(*mdat));
  869. mdat->type = type;
  870. mdat->alg = NULL;
  871. mdat->key = NULL;
  872. mdat->input = NULL;
  873. mdat->output = NULL;
  874. t->data = mdat;
  875. return 1;
  876. }
  877. static void mac_test_cleanup(struct evp_test *t)
  878. {
  879. struct mac_data *mdat = t->data;
  880. test_free(mdat->alg);
  881. test_free(mdat->key);
  882. test_free(mdat->input);
  883. test_free(mdat->output);
  884. }
  885. static int mac_test_parse(struct evp_test *t,
  886. const char *keyword, const char *value)
  887. {
  888. struct mac_data *mdata = t->data;
  889. if (strcmp(keyword, "Key") == 0)
  890. return test_bin(value, &mdata->key, &mdata->key_len);
  891. if (strcmp(keyword, "Algorithm") == 0) {
  892. mdata->alg = BUF_strdup(value);
  893. if (!mdata->alg)
  894. return 0;
  895. return 1;
  896. }
  897. if (strcmp(keyword, "Input") == 0)
  898. return test_bin(value, &mdata->input, &mdata->input_len);
  899. if (strcmp(keyword, "Output") == 0)
  900. return test_bin(value, &mdata->output, &mdata->output_len);
  901. return 0;
  902. }
  903. static int mac_test_run(struct evp_test *t)
  904. {
  905. struct mac_data *mdata = t->data;
  906. const char *err = "INTERNAL_ERROR";
  907. EVP_MD_CTX *mctx = NULL;
  908. EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
  909. EVP_PKEY *key = NULL;
  910. const EVP_MD *md = NULL;
  911. unsigned char *mac = NULL;
  912. size_t mac_len;
  913. err = "MAC_PKEY_CTX_ERROR";
  914. genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL);
  915. if (!genctx)
  916. goto err;
  917. err = "MAC_KEYGEN_INIT_ERROR";
  918. if (EVP_PKEY_keygen_init(genctx) <= 0)
  919. goto err;
  920. if (mdata->type == EVP_PKEY_CMAC) {
  921. err = "MAC_ALGORITHM_SET_ERROR";
  922. if (EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0)
  923. goto err;
  924. }
  925. err = "MAC_KEY_SET_ERROR";
  926. if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0)
  927. goto err;
  928. err = "MAC_KEY_GENERATE_ERROR";
  929. if (EVP_PKEY_keygen(genctx, &key) <= 0)
  930. goto err;
  931. if (mdata->type == EVP_PKEY_HMAC) {
  932. err = "MAC_ALGORITHM_SET_ERROR";
  933. md = EVP_get_digestbyname(mdata->alg);
  934. if (!md)
  935. goto err;
  936. }
  937. mctx = EVP_MD_CTX_create();
  938. if (!mctx)
  939. goto err;
  940. err = "DIGESTSIGNINIT_ERROR";
  941. if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key))
  942. goto err;
  943. err = "DIGESTSIGNUPDATE_ERROR";
  944. if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len))
  945. goto err;
  946. err = "DIGESTSIGNFINAL_LENGTH_ERROR";
  947. if (!EVP_DigestSignFinal(mctx, NULL, &mac_len))
  948. goto err;
  949. mac = OPENSSL_malloc(mac_len);
  950. if (!mac) {
  951. fprintf(stderr, "Error allocating mac buffer!\n");
  952. exit(1);
  953. }
  954. if (!EVP_DigestSignFinal(mctx, mac, &mac_len))
  955. goto err;
  956. err = "MAC_LENGTH_MISMATCH";
  957. if (mac_len != mdata->output_len)
  958. goto err;
  959. err = "MAC_MISMATCH";
  960. if (check_output(t, mdata->output, mac, mac_len))
  961. goto err;
  962. err = NULL;
  963. err:
  964. if (mctx)
  965. EVP_MD_CTX_destroy(mctx);
  966. OPENSSL_free(mac);
  967. EVP_PKEY_CTX_free(genctx);
  968. EVP_PKEY_free(key);
  969. t->err = err;
  970. return 1;
  971. }
  972. static const struct evp_test_method mac_test_method = {
  973. "MAC",
  974. mac_test_init,
  975. mac_test_cleanup,
  976. mac_test_parse,
  977. mac_test_run
  978. };
  979. /*
  980. * Public key operations. These are all very similar and can share
  981. * a lot of common code.
  982. */
  983. struct pkey_data {
  984. /* Context for this operation */
  985. EVP_PKEY_CTX *ctx;
  986. /* Key operation to perform */
  987. int (*keyop) (EVP_PKEY_CTX *ctx,
  988. unsigned char *sig, size_t *siglen,
  989. const unsigned char *tbs, size_t tbslen);
  990. /* Input to MAC */
  991. unsigned char *input;
  992. size_t input_len;
  993. /* Expected output */
  994. unsigned char *output;
  995. size_t output_len;
  996. };
  997. /*
  998. * Perform public key operation setup: lookup key, allocated ctx and call
  999. * the appropriate initialisation function
  1000. */
  1001. static int pkey_test_init(struct evp_test *t, const char *name,
  1002. int use_public,
  1003. int (*keyopinit) (EVP_PKEY_CTX *ctx),
  1004. int (*keyop) (EVP_PKEY_CTX *ctx,
  1005. unsigned char *sig, size_t *siglen,
  1006. const unsigned char *tbs,
  1007. size_t tbslen)
  1008. )
  1009. {
  1010. struct pkey_data *kdata;
  1011. EVP_PKEY *pkey = NULL;
  1012. int rv = 0;
  1013. if (use_public)
  1014. rv = find_key(&pkey, name, t->public);
  1015. if (!rv)
  1016. rv = find_key(&pkey, name, t->private);
  1017. if (!rv)
  1018. return 0;
  1019. if (!pkey) {
  1020. t->skip = 1;
  1021. return 1;
  1022. }
  1023. kdata = OPENSSL_malloc(sizeof(*kdata));
  1024. if (!kdata) {
  1025. EVP_PKEY_free(pkey);
  1026. return 0;
  1027. }
  1028. kdata->ctx = NULL;
  1029. kdata->input = NULL;
  1030. kdata->output = NULL;
  1031. kdata->keyop = keyop;
  1032. t->data = kdata;
  1033. kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL);
  1034. if (!kdata->ctx)
  1035. return 0;
  1036. if (keyopinit(kdata->ctx) <= 0)
  1037. return 0;
  1038. return 1;
  1039. }
  1040. static void pkey_test_cleanup(struct evp_test *t)
  1041. {
  1042. struct pkey_data *kdata = t->data;
  1043. OPENSSL_free(kdata->input);
  1044. OPENSSL_free(kdata->output);
  1045. EVP_PKEY_CTX_free(kdata->ctx);
  1046. }
  1047. static int pkey_test_parse(struct evp_test *t,
  1048. const char *keyword, const char *value)
  1049. {
  1050. struct pkey_data *kdata = t->data;
  1051. if (strcmp(keyword, "Input") == 0)
  1052. return test_bin(value, &kdata->input, &kdata->input_len);
  1053. if (strcmp(keyword, "Output") == 0)
  1054. return test_bin(value, &kdata->output, &kdata->output_len);
  1055. if (strcmp(keyword, "Ctrl") == 0) {
  1056. char *p = strchr(value, ':');
  1057. if (p)
  1058. *p++ = 0;
  1059. if (EVP_PKEY_CTX_ctrl_str(kdata->ctx, value, p) <= 0)
  1060. return 0;
  1061. return 1;
  1062. }
  1063. return 0;
  1064. }
  1065. static int pkey_test_run(struct evp_test *t)
  1066. {
  1067. struct pkey_data *kdata = t->data;
  1068. unsigned char *out = NULL;
  1069. size_t out_len;
  1070. const char *err = "KEYOP_LENGTH_ERROR";
  1071. if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
  1072. kdata->input_len) <= 0)
  1073. goto err;
  1074. out = OPENSSL_malloc(out_len);
  1075. if (!out) {
  1076. fprintf(stderr, "Error allocating output buffer!\n");
  1077. exit(1);
  1078. }
  1079. err = "KEYOP_ERROR";
  1080. if (kdata->keyop
  1081. (kdata->ctx, out, &out_len, kdata->input, kdata->input_len) <= 0)
  1082. goto err;
  1083. err = "KEYOP_LENGTH_MISMATCH";
  1084. if (out_len != kdata->output_len)
  1085. goto err;
  1086. err = "KEYOP_MISMATCH";
  1087. if (check_output(t, kdata->output, out, out_len))
  1088. goto err;
  1089. err = NULL;
  1090. err:
  1091. OPENSSL_free(out);
  1092. t->err = err;
  1093. return 1;
  1094. }
  1095. static int sign_test_init(struct evp_test *t, const char *name)
  1096. {
  1097. return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
  1098. }
  1099. static const struct evp_test_method psign_test_method = {
  1100. "Sign",
  1101. sign_test_init,
  1102. pkey_test_cleanup,
  1103. pkey_test_parse,
  1104. pkey_test_run
  1105. };
  1106. static int verify_recover_test_init(struct evp_test *t, const char *name)
  1107. {
  1108. return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
  1109. EVP_PKEY_verify_recover);
  1110. }
  1111. static const struct evp_test_method pverify_recover_test_method = {
  1112. "VerifyRecover",
  1113. verify_recover_test_init,
  1114. pkey_test_cleanup,
  1115. pkey_test_parse,
  1116. pkey_test_run
  1117. };
  1118. static int decrypt_test_init(struct evp_test *t, const char *name)
  1119. {
  1120. return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
  1121. EVP_PKEY_decrypt);
  1122. }
  1123. static const struct evp_test_method pdecrypt_test_method = {
  1124. "Decrypt",
  1125. decrypt_test_init,
  1126. pkey_test_cleanup,
  1127. pkey_test_parse,
  1128. pkey_test_run
  1129. };
  1130. static int verify_test_init(struct evp_test *t, const char *name)
  1131. {
  1132. return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
  1133. }
  1134. static int verify_test_run(struct evp_test *t)
  1135. {
  1136. struct pkey_data *kdata = t->data;
  1137. if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
  1138. kdata->input, kdata->input_len) <= 0)
  1139. t->err = "VERIFY_ERROR";
  1140. return 1;
  1141. }
  1142. static const struct evp_test_method pverify_test_method = {
  1143. "Verify",
  1144. verify_test_init,
  1145. pkey_test_cleanup,
  1146. pkey_test_parse,
  1147. verify_test_run
  1148. };