]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/evp_test.c
Add PBE tests.
[thirdparty/openssl.git] / test / evp_test.c
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
55 #include <stdio.h>
56 #include <string.h>
57 #include <stdlib.h>
58 #include <ctype.h>
59 #include <openssl/evp.h>
60 #include <openssl/pem.h>
61 #include <openssl/err.h>
62 #include <openssl/x509v3.h>
63 #include <openssl/pkcs12.h>
64 #include "internal/numbers.h"
65
66 /* Remove spaces from beginning and end of a string */
67
68 static void remove_space(char **pval)
69 {
70 unsigned char *p = (unsigned char *)*pval;
71
72 while (isspace(*p))
73 p++;
74
75 *pval = (char *)p;
76
77 p = p + strlen(*pval) - 1;
78
79 /* Remove trailing space */
80 while (isspace(*p))
81 *p-- = 0;
82 }
83
84 /*
85 * Given a line of the form:
86 * name = value # comment
87 * extract name and value. NB: modifies passed buffer.
88 */
89
90 static int parse_line(char **pkw, char **pval, char *linebuf)
91 {
92 char *p;
93
94 p = linebuf + strlen(linebuf) - 1;
95
96 if (*p != '\n') {
97 fprintf(stderr, "FATAL: missing EOL\n");
98 exit(1);
99 }
100
101 /* Look for # */
102
103 p = strchr(linebuf, '#');
104
105 if (p)
106 *p = '\0';
107
108 /* Look for = sign */
109 p = strchr(linebuf, '=');
110
111 /* If no '=' exit */
112 if (!p)
113 return 0;
114
115 *p++ = '\0';
116
117 *pkw = linebuf;
118 *pval = p;
119
120 /* Remove spaces from keyword and value */
121 remove_space(pkw);
122 remove_space(pval);
123
124 return 1;
125 }
126
127 /* For a hex string "value" convert to a binary allocated buffer */
128 static int test_bin(const char *value, unsigned char **buf, size_t *buflen)
129 {
130 long len;
131 if (!*value) {
132 /* Don't return NULL for zero length buffer */
133 *buf = OPENSSL_malloc(1);
134 if (!*buf)
135 return 0;
136 **buf = 0;
137 *buflen = 0;
138 return 1;
139 }
140 /* Check for string literal */
141 if (value[0] == '"') {
142 size_t vlen;
143 value++;
144 vlen = strlen(value);
145 if (value[vlen - 1] != '"')
146 return 0;
147 vlen--;
148 if (vlen == 0) {
149 *buf = OPENSSL_malloc(1);
150 if (*buf == NULL)
151 return 0;
152 **buf = 0;
153 } else {
154 *buf = BUF_memdup(value, vlen);
155 if (*buf == NULL)
156 return 0;
157 }
158 *buflen = vlen;
159 return 1;
160 }
161 *buf = string_to_hex(value, &len);
162 if (!*buf) {
163 fprintf(stderr, "Value=%s\n", value);
164 ERR_print_errors_fp(stderr);
165 return -1;
166 }
167 /* Size of input buffer means we'll never overflow */
168 *buflen = len;
169 return 1;
170 }
171 /* Parse unsigned decimal 64 bit integer value */
172 static int test_uint64(const char *value, uint64_t *pr)
173 {
174 const char *p = value;
175 if (!*p) {
176 fprintf(stderr, "Invalid empty integer value\n");
177 return -1;
178 }
179 *pr = 0;
180 while (*p) {
181 if (*pr > UINT64_MAX/10) {
182 fprintf(stderr, "Integer string overflow value=%s\n", value);
183 return -1;
184 }
185 *pr *= 10;
186 if (*p < '0' || *p > '9') {
187 fprintf(stderr, "Invalid integer string value=%s\n", value);
188 return -1;
189 }
190 *pr += *p - '0';
191 p++;
192 }
193 return 1;
194 }
195
196 /* Structure holding test information */
197 struct evp_test {
198 /* file being read */
199 FILE *in;
200 /* List of public and private keys */
201 struct key_list *private;
202 struct key_list *public;
203 /* method for this test */
204 const struct evp_test_method *meth;
205 /* current line being processed */
206 unsigned int line;
207 /* start line of current test */
208 unsigned int start_line;
209 /* Error string for test */
210 const char *err;
211 /* Expected error value of test */
212 char *expected_err;
213 /* Number of tests */
214 int ntests;
215 /* Error count */
216 int errors;
217 /* Number of tests skipped */
218 int nskip;
219 /* If output mismatch expected and got value */
220 unsigned char *out_got;
221 unsigned char *out_expected;
222 size_t out_len;
223 /* test specific data */
224 void *data;
225 /* Current test should be skipped */
226 int skip;
227 };
228
229 struct key_list {
230 char *name;
231 EVP_PKEY *key;
232 struct key_list *next;
233 };
234
235 /* Test method structure */
236 struct evp_test_method {
237 /* Name of test as it appears in file */
238 const char *name;
239 /* Initialise test for "alg" */
240 int (*init) (struct evp_test * t, const char *alg);
241 /* Clean up method */
242 void (*cleanup) (struct evp_test * t);
243 /* Test specific name value pair processing */
244 int (*parse) (struct evp_test * t, const char *name, const char *value);
245 /* Run the test itself */
246 int (*run_test) (struct evp_test * t);
247 };
248
249 static const struct evp_test_method digest_test_method, cipher_test_method;
250 static const struct evp_test_method mac_test_method;
251 static const struct evp_test_method psign_test_method, pverify_test_method;
252 static const struct evp_test_method pdecrypt_test_method;
253 static const struct evp_test_method pverify_recover_test_method;
254 static const struct evp_test_method pbe_test_method;
255
256 static const struct evp_test_method *evp_test_list[] = {
257 &digest_test_method,
258 &cipher_test_method,
259 &mac_test_method,
260 &psign_test_method,
261 &pverify_test_method,
262 &pdecrypt_test_method,
263 &pverify_recover_test_method,
264 &pbe_test_method,
265 NULL
266 };
267
268 static const struct evp_test_method *evp_find_test(const char *name)
269 {
270 const struct evp_test_method **tt;
271
272 for (tt = evp_test_list; *tt; tt++) {
273 if (strcmp(name, (*tt)->name) == 0)
274 return *tt;
275 }
276 return NULL;
277 }
278
279 static void hex_print(const char *name, const unsigned char *buf, size_t len)
280 {
281 size_t i;
282 fprintf(stderr, "%s ", name);
283 for (i = 0; i < len; i++)
284 fprintf(stderr, "%02X", buf[i]);
285 fputs("\n", stderr);
286 }
287
288 static void free_expected(struct evp_test *t)
289 {
290 OPENSSL_free(t->expected_err);
291 t->expected_err = NULL;
292 OPENSSL_free(t->out_expected);
293 OPENSSL_free(t->out_got);
294 t->out_expected = NULL;
295 t->out_got = NULL;
296 }
297
298 static void print_expected(struct evp_test *t)
299 {
300 if (t->out_expected == NULL)
301 return;
302 hex_print("Expected:", t->out_expected, t->out_len);
303 hex_print("Got: ", t->out_got, t->out_len);
304 free_expected(t);
305 }
306
307 static int check_test_error(struct evp_test *t)
308 {
309 if (!t->err && !t->expected_err)
310 return 1;
311 if (t->err && !t->expected_err) {
312 fprintf(stderr, "Test line %d: unexpected error %s\n",
313 t->start_line, t->err);
314 print_expected(t);
315 return 0;
316 }
317 if (!t->err && t->expected_err) {
318 fprintf(stderr, "Test line %d: succeeded expecting %s\n",
319 t->start_line, t->expected_err);
320 return 0;
321 }
322 if (strcmp(t->err, t->expected_err) == 0)
323 return 1;
324
325 fprintf(stderr, "Test line %d: expecting %s got %s\n",
326 t->start_line, t->expected_err, t->err);
327 return 0;
328 }
329
330 /* Setup a new test, run any existing test */
331
332 static int setup_test(struct evp_test *t, const struct evp_test_method *tmeth)
333 {
334 /* If we already have a test set up run it */
335 if (t->meth) {
336 t->ntests++;
337 if (t->skip) {
338 t->meth = tmeth;
339 t->nskip++;
340 return 1;
341 }
342 t->err = NULL;
343 if (t->meth->run_test(t) != 1) {
344 fprintf(stderr, "%s test error line %d\n",
345 t->meth->name, t->start_line);
346 return 0;
347 }
348 if (!check_test_error(t)) {
349 if (t->err)
350 ERR_print_errors_fp(stderr);
351 t->errors++;
352 }
353 ERR_clear_error();
354 t->meth->cleanup(t);
355 OPENSSL_free(t->data);
356 t->data = NULL;
357 OPENSSL_free(t->expected_err);
358 t->expected_err = NULL;
359 free_expected(t);
360 }
361 t->meth = tmeth;
362 return 1;
363 }
364
365 static int find_key(EVP_PKEY **ppk, const char *name, struct key_list *lst)
366 {
367 for (; lst; lst = lst->next) {
368 if (strcmp(lst->name, name) == 0) {
369 if (ppk)
370 *ppk = lst->key;
371 return 1;
372 }
373 }
374 return 0;
375 }
376
377 static void free_key_list(struct key_list *lst)
378 {
379 while (lst != NULL) {
380 struct key_list *ltmp;
381 EVP_PKEY_free(lst->key);
382 OPENSSL_free(lst->name);
383 ltmp = lst->next;
384 OPENSSL_free(lst);
385 lst = ltmp;
386 }
387 }
388
389 static int check_unsupported()
390 {
391 long err = ERR_peek_error();
392 if (ERR_GET_LIB(err) == ERR_LIB_EVP
393 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
394 ERR_clear_error();
395 return 1;
396 }
397 return 0;
398 }
399
400 static int process_test(struct evp_test *t, char *buf, int verbose)
401 {
402 char *keyword = NULL, *value = NULL;
403 int rv = 0, add_key = 0;
404 long save_pos = 0;
405 struct key_list **lst = NULL, *key = NULL;
406 EVP_PKEY *pk = NULL;
407 const struct evp_test_method *tmeth = NULL;
408 if (verbose)
409 fputs(buf, stdout);
410 if (!parse_line(&keyword, &value, buf))
411 return 1;
412 if (strcmp(keyword, "PrivateKey") == 0) {
413 save_pos = ftell(t->in);
414 pk = PEM_read_PrivateKey(t->in, NULL, 0, NULL);
415 if (pk == NULL && !check_unsupported()) {
416 fprintf(stderr, "Error reading private key %s\n", value);
417 ERR_print_errors_fp(stderr);
418 return 0;
419 }
420 lst = &t->private;
421 add_key = 1;
422 }
423 if (strcmp(keyword, "PublicKey") == 0) {
424 save_pos = ftell(t->in);
425 pk = PEM_read_PUBKEY(t->in, NULL, 0, NULL);
426 if (pk == NULL && !check_unsupported()) {
427 fprintf(stderr, "Error reading public key %s\n", value);
428 ERR_print_errors_fp(stderr);
429 return 0;
430 }
431 lst = &t->public;
432 add_key = 1;
433 }
434 /* If we have a key add to list */
435 if (add_key) {
436 char tmpbuf[80];
437 if (find_key(NULL, value, *lst)) {
438 fprintf(stderr, "Duplicate key %s\n", value);
439 return 0;
440 }
441 key = OPENSSL_malloc(sizeof(*key));
442 if (!key)
443 return 0;
444 key->name = BUF_strdup(value);
445 key->key = pk;
446 key->next = *lst;
447 *lst = key;
448 /* Rewind input, read to end and update line numbers */
449 fseek(t->in, save_pos, SEEK_SET);
450 while (fgets(tmpbuf, sizeof(tmpbuf), t->in)) {
451 t->line++;
452 if (strncmp(tmpbuf, "-----END", 8) == 0)
453 return 1;
454 }
455 fprintf(stderr, "Can't find key end\n");
456 return 0;
457 }
458
459 /* See if keyword corresponds to a test start */
460 tmeth = evp_find_test(keyword);
461 if (tmeth) {
462 if (!setup_test(t, tmeth))
463 return 0;
464 t->start_line = t->line;
465 t->skip = 0;
466 if (!tmeth->init(t, value)) {
467 fprintf(stderr, "Unknown %s: %s\n", keyword, value);
468 return 0;
469 }
470 return 1;
471 } else if (t->skip) {
472 return 1;
473 } else if (strcmp(keyword, "Result") == 0) {
474 if (t->expected_err) {
475 fprintf(stderr, "Line %d: multiple result lines\n", t->line);
476 return 0;
477 }
478 t->expected_err = BUF_strdup(value);
479 if (!t->expected_err)
480 return 0;
481 } else {
482 /* Must be test specific line: try to parse it */
483 if (t->meth)
484 rv = t->meth->parse(t, keyword, value);
485
486 if (rv == 0)
487 fprintf(stderr, "line %d: unexpected keyword %s\n",
488 t->line, keyword);
489
490 if (rv < 0)
491 fprintf(stderr, "line %d: error processing keyword %s\n",
492 t->line, keyword);
493 if (rv <= 0)
494 return 0;
495 }
496 return 1;
497 }
498
499 static int check_output(struct evp_test *t, const unsigned char *expected,
500 const unsigned char *got, size_t len)
501 {
502 if (!memcmp(expected, got, len))
503 return 0;
504 t->out_expected = BUF_memdup(expected, len);
505 t->out_got = BUF_memdup(got, len);
506 t->out_len = len;
507 if (t->out_expected == NULL || t->out_got == NULL) {
508 fprintf(stderr, "Memory allocation error!\n");
509 exit(1);
510 }
511 return 1;
512 }
513
514 int main(int argc, char **argv)
515 {
516 FILE *in = NULL;
517 char buf[10240];
518 struct evp_test t;
519
520 if (argc != 2) {
521 fprintf(stderr, "usage: evp_test testfile.txt\n");
522 return 1;
523 }
524
525 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
526
527 ERR_load_crypto_strings();
528 OpenSSL_add_all_algorithms();
529
530 memset(&t, 0, sizeof(t));
531 t.meth = NULL;
532 t.public = NULL;
533 t.private = NULL;
534 t.err = NULL;
535 t.line = 0;
536 t.start_line = -1;
537 t.errors = 0;
538 t.ntests = 0;
539 t.out_expected = NULL;
540 t.out_got = NULL;
541 t.out_len = 0;
542 in = fopen(argv[1], "r");
543 t.in = in;
544 while (fgets(buf, sizeof(buf), in)) {
545 t.line++;
546 if (!process_test(&t, buf, 0))
547 exit(1);
548 }
549 /* Run any final test we have */
550 if (!setup_test(&t, NULL))
551 exit(1);
552 fprintf(stderr, "%d tests completed with %d errors, %d skipped\n",
553 t.ntests, t.errors, t.nskip);
554 free_key_list(t.public);
555 free_key_list(t.private);
556 fclose(in);
557 EVP_cleanup();
558 CRYPTO_cleanup_all_ex_data();
559 ERR_remove_thread_state(NULL);
560 ERR_free_strings();
561 CRYPTO_mem_leaks_fp(stderr);
562 if (t.errors)
563 return 1;
564 return 0;
565 }
566
567 static void test_free(void *d)
568 {
569 OPENSSL_free(d);
570 }
571
572 /* Message digest tests */
573
574 struct digest_data {
575 /* Digest this test is for */
576 const EVP_MD *digest;
577 /* Input to digest */
578 unsigned char *input;
579 size_t input_len;
580 /* Repeat count for input */
581 size_t nrpt;
582 /* Expected output */
583 unsigned char *output;
584 size_t output_len;
585 };
586
587 static int digest_test_init(struct evp_test *t, const char *alg)
588 {
589 const EVP_MD *digest;
590 struct digest_data *mdat = t->data;
591 digest = EVP_get_digestbyname(alg);
592 if (!digest) {
593 /* If alg has an OID assume disabled algorithm */
594 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
595 t->skip = 1;
596 return 1;
597 }
598 return 0;
599 }
600 mdat = OPENSSL_malloc(sizeof(*mdat));
601 mdat->digest = digest;
602 mdat->input = NULL;
603 mdat->output = NULL;
604 mdat->nrpt = 1;
605 t->data = mdat;
606 return 1;
607 }
608
609 static void digest_test_cleanup(struct evp_test *t)
610 {
611 struct digest_data *mdat = t->data;
612 test_free(mdat->input);
613 test_free(mdat->output);
614 }
615
616 static int digest_test_parse(struct evp_test *t,
617 const char *keyword, const char *value)
618 {
619 struct digest_data *mdata = t->data;
620 if (strcmp(keyword, "Input") == 0)
621 return test_bin(value, &mdata->input, &mdata->input_len);
622 if (strcmp(keyword, "Output") == 0)
623 return test_bin(value, &mdata->output, &mdata->output_len);
624 if (strcmp(keyword, "Count") == 0) {
625 long nrpt = atoi(value);
626 if (nrpt <= 0)
627 return 0;
628 mdata->nrpt = (size_t)nrpt;
629 return 1;
630 }
631 return 0;
632 }
633
634 static int digest_test_run(struct evp_test *t)
635 {
636 struct digest_data *mdata = t->data;
637 size_t i;
638 const char *err = "INTERNAL_ERROR";
639 EVP_MD_CTX *mctx;
640 unsigned char md[EVP_MAX_MD_SIZE];
641 unsigned int md_len;
642 mctx = EVP_MD_CTX_create();
643 if (!mctx)
644 goto err;
645 err = "DIGESTINIT_ERROR";
646 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))
647 goto err;
648 err = "DIGESTUPDATE_ERROR";
649 for (i = 0; i < mdata->nrpt; i++) {
650 if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))
651 goto err;
652 }
653 err = "DIGESTFINAL_ERROR";
654 if (!EVP_DigestFinal(mctx, md, &md_len))
655 goto err;
656 err = "DIGEST_LENGTH_MISMATCH";
657 if (md_len != mdata->output_len)
658 goto err;
659 err = "DIGEST_MISMATCH";
660 if (check_output(t, mdata->output, md, md_len))
661 goto err;
662 err = NULL;
663 err:
664 if (mctx)
665 EVP_MD_CTX_destroy(mctx);
666 t->err = err;
667 return 1;
668 }
669
670 static const struct evp_test_method digest_test_method = {
671 "Digest",
672 digest_test_init,
673 digest_test_cleanup,
674 digest_test_parse,
675 digest_test_run
676 };
677
678 /* Cipher tests */
679 struct cipher_data {
680 const EVP_CIPHER *cipher;
681 int enc;
682 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
683 int aead;
684 unsigned char *key;
685 size_t key_len;
686 unsigned char *iv;
687 size_t iv_len;
688 unsigned char *plaintext;
689 size_t plaintext_len;
690 unsigned char *ciphertext;
691 size_t ciphertext_len;
692 /* GCM, CCM only */
693 unsigned char *aad;
694 size_t aad_len;
695 unsigned char *tag;
696 size_t tag_len;
697 };
698
699 static int cipher_test_init(struct evp_test *t, const char *alg)
700 {
701 const EVP_CIPHER *cipher;
702 struct cipher_data *cdat = t->data;
703 cipher = EVP_get_cipherbyname(alg);
704 if (!cipher) {
705 /* If alg has an OID assume disabled algorithm */
706 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
707 t->skip = 1;
708 return 1;
709 }
710 return 0;
711 }
712 cdat = OPENSSL_malloc(sizeof(*cdat));
713 cdat->cipher = cipher;
714 cdat->enc = -1;
715 cdat->key = NULL;
716 cdat->iv = NULL;
717 cdat->ciphertext = NULL;
718 cdat->plaintext = NULL;
719 cdat->aad = NULL;
720 cdat->tag = NULL;
721 t->data = cdat;
722 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
723 || EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE
724 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
725 cdat->aead = EVP_CIPHER_mode(cipher);
726 else
727 cdat->aead = 0;
728
729 return 1;
730 }
731
732 static void cipher_test_cleanup(struct evp_test *t)
733 {
734 struct cipher_data *cdat = t->data;
735 test_free(cdat->key);
736 test_free(cdat->iv);
737 test_free(cdat->ciphertext);
738 test_free(cdat->plaintext);
739 test_free(cdat->aad);
740 test_free(cdat->tag);
741 }
742
743 static int cipher_test_parse(struct evp_test *t, const char *keyword,
744 const char *value)
745 {
746 struct cipher_data *cdat = t->data;
747 if (strcmp(keyword, "Key") == 0)
748 return test_bin(value, &cdat->key, &cdat->key_len);
749 if (strcmp(keyword, "IV") == 0)
750 return test_bin(value, &cdat->iv, &cdat->iv_len);
751 if (strcmp(keyword, "Plaintext") == 0)
752 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
753 if (strcmp(keyword, "Ciphertext") == 0)
754 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
755 if (cdat->aead) {
756 if (strcmp(keyword, "AAD") == 0)
757 return test_bin(value, &cdat->aad, &cdat->aad_len);
758 if (strcmp(keyword, "Tag") == 0)
759 return test_bin(value, &cdat->tag, &cdat->tag_len);
760 }
761
762 if (strcmp(keyword, "Operation") == 0) {
763 if (strcmp(value, "ENCRYPT") == 0)
764 cdat->enc = 1;
765 else if (strcmp(value, "DECRYPT") == 0)
766 cdat->enc = 0;
767 else
768 return 0;
769 return 1;
770 }
771 return 0;
772 }
773
774 static int cipher_test_enc(struct evp_test *t, int enc)
775 {
776 struct cipher_data *cdat = t->data;
777 unsigned char *in, *out, *tmp = NULL;
778 size_t in_len, out_len;
779 int tmplen, tmpflen;
780 EVP_CIPHER_CTX *ctx = NULL;
781 const char *err;
782 err = "INTERNAL_ERROR";
783 ctx = EVP_CIPHER_CTX_new();
784 if (!ctx)
785 goto err;
786 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
787 if (enc) {
788 in = cdat->plaintext;
789 in_len = cdat->plaintext_len;
790 out = cdat->ciphertext;
791 out_len = cdat->ciphertext_len;
792 } else {
793 in = cdat->ciphertext;
794 in_len = cdat->ciphertext_len;
795 out = cdat->plaintext;
796 out_len = cdat->plaintext_len;
797 }
798 tmp = OPENSSL_malloc(in_len + 2 * EVP_MAX_BLOCK_LENGTH);
799 if (!tmp)
800 goto err;
801 err = "CIPHERINIT_ERROR";
802 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
803 goto err;
804 err = "INVALID_IV_LENGTH";
805 if (cdat->iv) {
806 if (cdat->aead) {
807 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
808 cdat->iv_len, 0))
809 goto err;
810 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
811 goto err;
812 }
813 if (cdat->aead) {
814 unsigned char *tag;
815 /*
816 * If encrypting or OCB just set tag length initially, otherwise
817 * set tag length and value.
818 */
819 if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {
820 err = "TAG_LENGTH_SET_ERROR";
821 tag = NULL;
822 } else {
823 err = "TAG_SET_ERROR";
824 tag = cdat->tag;
825 }
826 if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {
827 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
828 cdat->tag_len, tag))
829 goto err;
830 }
831 }
832
833 err = "INVALID_KEY_LENGTH";
834 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len))
835 goto err;
836 err = "KEY_SET_ERROR";
837 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1))
838 goto err;
839
840 if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {
841 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
842 cdat->tag_len, cdat->tag)) {
843 err = "TAG_SET_ERROR";
844 goto err;
845 }
846 }
847
848 if (cdat->aead == EVP_CIPH_CCM_MODE) {
849 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
850 err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
851 goto err;
852 }
853 }
854 if (cdat->aad) {
855 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, cdat->aad, cdat->aad_len)) {
856 err = "AAD_SET_ERROR";
857 goto err;
858 }
859 }
860 EVP_CIPHER_CTX_set_padding(ctx, 0);
861 err = "CIPHERUPDATE_ERROR";
862 if (!EVP_CipherUpdate(ctx, tmp, &tmplen, in, in_len))
863 goto err;
864 if (cdat->aead == EVP_CIPH_CCM_MODE)
865 tmpflen = 0;
866 else {
867 err = "CIPHERFINAL_ERROR";
868 if (!EVP_CipherFinal_ex(ctx, tmp + tmplen, &tmpflen))
869 goto err;
870 }
871 err = "LENGTH_MISMATCH";
872 if (out_len != (size_t)(tmplen + tmpflen))
873 goto err;
874 err = "VALUE_MISMATCH";
875 if (check_output(t, out, tmp, out_len))
876 goto err;
877 if (enc && cdat->aead) {
878 unsigned char rtag[16];
879 if (cdat->tag_len > sizeof(rtag)) {
880 err = "TAG_LENGTH_INTERNAL_ERROR";
881 goto err;
882 }
883 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
884 cdat->tag_len, rtag)) {
885 err = "TAG_RETRIEVE_ERROR";
886 goto err;
887 }
888 if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {
889 err = "TAG_VALUE_MISMATCH";
890 goto err;
891 }
892 }
893 err = NULL;
894 err:
895 OPENSSL_free(tmp);
896 EVP_CIPHER_CTX_free(ctx);
897 t->err = err;
898 return err ? 0 : 1;
899 }
900
901 static int cipher_test_run(struct evp_test *t)
902 {
903 struct cipher_data *cdat = t->data;
904 int rv;
905 if (!cdat->key) {
906 t->err = "NO_KEY";
907 return 0;
908 }
909 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
910 /* IV is optional and usually omitted in wrap mode */
911 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
912 t->err = "NO_IV";
913 return 0;
914 }
915 }
916 if (cdat->aead && !cdat->tag) {
917 t->err = "NO_TAG";
918 return 0;
919 }
920 if (cdat->enc) {
921 rv = cipher_test_enc(t, 1);
922 /* Not fatal errors: return */
923 if (rv != 1) {
924 if (rv < 0)
925 return 0;
926 return 1;
927 }
928 }
929 if (cdat->enc != 1) {
930 rv = cipher_test_enc(t, 0);
931 /* Not fatal errors: return */
932 if (rv != 1) {
933 if (rv < 0)
934 return 0;
935 return 1;
936 }
937 }
938 return 1;
939 }
940
941 static const struct evp_test_method cipher_test_method = {
942 "Cipher",
943 cipher_test_init,
944 cipher_test_cleanup,
945 cipher_test_parse,
946 cipher_test_run
947 };
948
949 struct mac_data {
950 /* MAC type */
951 int type;
952 /* Algorithm string for this MAC */
953 char *alg;
954 /* MAC key */
955 unsigned char *key;
956 size_t key_len;
957 /* Input to MAC */
958 unsigned char *input;
959 size_t input_len;
960 /* Expected output */
961 unsigned char *output;
962 size_t output_len;
963 };
964
965 static int mac_test_init(struct evp_test *t, const char *alg)
966 {
967 int type;
968 struct mac_data *mdat;
969 if (strcmp(alg, "HMAC") == 0)
970 type = EVP_PKEY_HMAC;
971 else if (strcmp(alg, "CMAC") == 0)
972 type = EVP_PKEY_CMAC;
973 else
974 return 0;
975
976 mdat = OPENSSL_malloc(sizeof(*mdat));
977 mdat->type = type;
978 mdat->alg = NULL;
979 mdat->key = NULL;
980 mdat->input = NULL;
981 mdat->output = NULL;
982 t->data = mdat;
983 return 1;
984 }
985
986 static void mac_test_cleanup(struct evp_test *t)
987 {
988 struct mac_data *mdat = t->data;
989 test_free(mdat->alg);
990 test_free(mdat->key);
991 test_free(mdat->input);
992 test_free(mdat->output);
993 }
994
995 static int mac_test_parse(struct evp_test *t,
996 const char *keyword, const char *value)
997 {
998 struct mac_data *mdata = t->data;
999 if (strcmp(keyword, "Key") == 0)
1000 return test_bin(value, &mdata->key, &mdata->key_len);
1001 if (strcmp(keyword, "Algorithm") == 0) {
1002 mdata->alg = BUF_strdup(value);
1003 if (!mdata->alg)
1004 return 0;
1005 return 1;
1006 }
1007 if (strcmp(keyword, "Input") == 0)
1008 return test_bin(value, &mdata->input, &mdata->input_len);
1009 if (strcmp(keyword, "Output") == 0)
1010 return test_bin(value, &mdata->output, &mdata->output_len);
1011 return 0;
1012 }
1013
1014 static int mac_test_run(struct evp_test *t)
1015 {
1016 struct mac_data *mdata = t->data;
1017 const char *err = "INTERNAL_ERROR";
1018 EVP_MD_CTX *mctx = NULL;
1019 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1020 EVP_PKEY *key = NULL;
1021 const EVP_MD *md = NULL;
1022 unsigned char *mac = NULL;
1023 size_t mac_len;
1024
1025 err = "MAC_PKEY_CTX_ERROR";
1026 genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL);
1027 if (!genctx)
1028 goto err;
1029
1030 err = "MAC_KEYGEN_INIT_ERROR";
1031 if (EVP_PKEY_keygen_init(genctx) <= 0)
1032 goto err;
1033 if (mdata->type == EVP_PKEY_CMAC) {
1034 err = "MAC_ALGORITHM_SET_ERROR";
1035 if (EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0)
1036 goto err;
1037 }
1038
1039 err = "MAC_KEY_SET_ERROR";
1040 if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0)
1041 goto err;
1042
1043 err = "MAC_KEY_GENERATE_ERROR";
1044 if (EVP_PKEY_keygen(genctx, &key) <= 0)
1045 goto err;
1046 if (mdata->type == EVP_PKEY_HMAC) {
1047 err = "MAC_ALGORITHM_SET_ERROR";
1048 md = EVP_get_digestbyname(mdata->alg);
1049 if (!md)
1050 goto err;
1051 }
1052 mctx = EVP_MD_CTX_create();
1053 if (!mctx)
1054 goto err;
1055 err = "DIGESTSIGNINIT_ERROR";
1056 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key))
1057 goto err;
1058
1059 err = "DIGESTSIGNUPDATE_ERROR";
1060 if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len))
1061 goto err;
1062 err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1063 if (!EVP_DigestSignFinal(mctx, NULL, &mac_len))
1064 goto err;
1065 mac = OPENSSL_malloc(mac_len);
1066 if (!mac) {
1067 fprintf(stderr, "Error allocating mac buffer!\n");
1068 exit(1);
1069 }
1070 if (!EVP_DigestSignFinal(mctx, mac, &mac_len))
1071 goto err;
1072 err = "MAC_LENGTH_MISMATCH";
1073 if (mac_len != mdata->output_len)
1074 goto err;
1075 err = "MAC_MISMATCH";
1076 if (check_output(t, mdata->output, mac, mac_len))
1077 goto err;
1078 err = NULL;
1079 err:
1080 if (mctx)
1081 EVP_MD_CTX_destroy(mctx);
1082 OPENSSL_free(mac);
1083 EVP_PKEY_CTX_free(genctx);
1084 EVP_PKEY_free(key);
1085 t->err = err;
1086 return 1;
1087 }
1088
1089 static const struct evp_test_method mac_test_method = {
1090 "MAC",
1091 mac_test_init,
1092 mac_test_cleanup,
1093 mac_test_parse,
1094 mac_test_run
1095 };
1096
1097 /*
1098 * Public key operations. These are all very similar and can share
1099 * a lot of common code.
1100 */
1101
1102 struct pkey_data {
1103 /* Context for this operation */
1104 EVP_PKEY_CTX *ctx;
1105 /* Key operation to perform */
1106 int (*keyop) (EVP_PKEY_CTX *ctx,
1107 unsigned char *sig, size_t *siglen,
1108 const unsigned char *tbs, size_t tbslen);
1109 /* Input to MAC */
1110 unsigned char *input;
1111 size_t input_len;
1112 /* Expected output */
1113 unsigned char *output;
1114 size_t output_len;
1115 };
1116
1117 /*
1118 * Perform public key operation setup: lookup key, allocated ctx and call
1119 * the appropriate initialisation function
1120 */
1121 static int pkey_test_init(struct evp_test *t, const char *name,
1122 int use_public,
1123 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1124 int (*keyop) (EVP_PKEY_CTX *ctx,
1125 unsigned char *sig, size_t *siglen,
1126 const unsigned char *tbs,
1127 size_t tbslen)
1128 )
1129 {
1130 struct pkey_data *kdata;
1131 EVP_PKEY *pkey = NULL;
1132 int rv = 0;
1133 if (use_public)
1134 rv = find_key(&pkey, name, t->public);
1135 if (!rv)
1136 rv = find_key(&pkey, name, t->private);
1137 if (!rv)
1138 return 0;
1139 if (!pkey) {
1140 t->skip = 1;
1141 return 1;
1142 }
1143
1144 kdata = OPENSSL_malloc(sizeof(*kdata));
1145 if (!kdata) {
1146 EVP_PKEY_free(pkey);
1147 return 0;
1148 }
1149 kdata->ctx = NULL;
1150 kdata->input = NULL;
1151 kdata->output = NULL;
1152 kdata->keyop = keyop;
1153 t->data = kdata;
1154 kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL);
1155 if (!kdata->ctx)
1156 return 0;
1157 if (keyopinit(kdata->ctx) <= 0)
1158 return 0;
1159 return 1;
1160 }
1161
1162 static void pkey_test_cleanup(struct evp_test *t)
1163 {
1164 struct pkey_data *kdata = t->data;
1165
1166 OPENSSL_free(kdata->input);
1167 OPENSSL_free(kdata->output);
1168 EVP_PKEY_CTX_free(kdata->ctx);
1169 }
1170
1171 static int pkey_test_parse(struct evp_test *t,
1172 const char *keyword, const char *value)
1173 {
1174 struct pkey_data *kdata = t->data;
1175 if (strcmp(keyword, "Input") == 0)
1176 return test_bin(value, &kdata->input, &kdata->input_len);
1177 if (strcmp(keyword, "Output") == 0)
1178 return test_bin(value, &kdata->output, &kdata->output_len);
1179 if (strcmp(keyword, "Ctrl") == 0) {
1180 char *p = strchr(value, ':');
1181 if (p)
1182 *p++ = 0;
1183 if (EVP_PKEY_CTX_ctrl_str(kdata->ctx, value, p) <= 0)
1184 return 0;
1185 return 1;
1186 }
1187 return 0;
1188 }
1189
1190 static int pkey_test_run(struct evp_test *t)
1191 {
1192 struct pkey_data *kdata = t->data;
1193 unsigned char *out = NULL;
1194 size_t out_len;
1195 const char *err = "KEYOP_LENGTH_ERROR";
1196 if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1197 kdata->input_len) <= 0)
1198 goto err;
1199 out = OPENSSL_malloc(out_len);
1200 if (!out) {
1201 fprintf(stderr, "Error allocating output buffer!\n");
1202 exit(1);
1203 }
1204 err = "KEYOP_ERROR";
1205 if (kdata->keyop
1206 (kdata->ctx, out, &out_len, kdata->input, kdata->input_len) <= 0)
1207 goto err;
1208 err = "KEYOP_LENGTH_MISMATCH";
1209 if (out_len != kdata->output_len)
1210 goto err;
1211 err = "KEYOP_MISMATCH";
1212 if (check_output(t, kdata->output, out, out_len))
1213 goto err;
1214 err = NULL;
1215 err:
1216 OPENSSL_free(out);
1217 t->err = err;
1218 return 1;
1219 }
1220
1221 static int sign_test_init(struct evp_test *t, const char *name)
1222 {
1223 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1224 }
1225
1226 static const struct evp_test_method psign_test_method = {
1227 "Sign",
1228 sign_test_init,
1229 pkey_test_cleanup,
1230 pkey_test_parse,
1231 pkey_test_run
1232 };
1233
1234 static int verify_recover_test_init(struct evp_test *t, const char *name)
1235 {
1236 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1237 EVP_PKEY_verify_recover);
1238 }
1239
1240 static const struct evp_test_method pverify_recover_test_method = {
1241 "VerifyRecover",
1242 verify_recover_test_init,
1243 pkey_test_cleanup,
1244 pkey_test_parse,
1245 pkey_test_run
1246 };
1247
1248 static int decrypt_test_init(struct evp_test *t, const char *name)
1249 {
1250 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1251 EVP_PKEY_decrypt);
1252 }
1253
1254 static const struct evp_test_method pdecrypt_test_method = {
1255 "Decrypt",
1256 decrypt_test_init,
1257 pkey_test_cleanup,
1258 pkey_test_parse,
1259 pkey_test_run
1260 };
1261
1262 static int verify_test_init(struct evp_test *t, const char *name)
1263 {
1264 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1265 }
1266
1267 static int verify_test_run(struct evp_test *t)
1268 {
1269 struct pkey_data *kdata = t->data;
1270 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1271 kdata->input, kdata->input_len) <= 0)
1272 t->err = "VERIFY_ERROR";
1273 return 1;
1274 }
1275
1276 static const struct evp_test_method pverify_test_method = {
1277 "Verify",
1278 verify_test_init,
1279 pkey_test_cleanup,
1280 pkey_test_parse,
1281 verify_test_run
1282 };
1283
1284 /* PBE tests */
1285
1286 #define PBE_TYPE_SCRYPT 1
1287 #define PBE_TYPE_PBKDF2 2
1288 #define PBE_TYPE_PKCS12 3
1289
1290 struct pbe_data {
1291
1292 int pbe_type;
1293
1294 /* scrypt parameters */
1295 uint64_t N, r, p, maxmem;
1296
1297 /* PKCS#12 parameters */
1298 int id, iter;
1299 const EVP_MD *md;
1300
1301 /* password */
1302 unsigned char *pass;
1303 size_t pass_len;
1304
1305 /* salt */
1306 unsigned char *salt;
1307 size_t salt_len;
1308
1309 /* Expected output */
1310 unsigned char *key;
1311 size_t key_len;
1312 };
1313
1314 static int scrypt_test_parse(struct evp_test *t,
1315 const char *keyword, const char *value)
1316 {
1317 struct pbe_data *pdata = t->data;
1318
1319 if (strcmp(keyword, "N") == 0)
1320 return test_uint64(value, &pdata->N);
1321 if (strcmp(keyword, "p") == 0)
1322 return test_uint64(value, &pdata->p);
1323 if (strcmp(keyword, "r") == 0)
1324 return test_uint64(value, &pdata->r);
1325 if (strcmp(keyword, "maxmem") == 0)
1326 return test_uint64(value, &pdata->maxmem);
1327 return 0;
1328 }
1329
1330 static int pbkdf2_test_parse(struct evp_test *t,
1331 const char *keyword, const char *value)
1332 {
1333 struct pbe_data *pdata = t->data;
1334
1335 if (strcmp(keyword, "iter") == 0) {
1336 pdata->iter = atoi(value);
1337 if (pdata->iter <= 0)
1338 return 0;
1339 return 1;
1340 }
1341 if (strcmp(keyword, "MD") == 0) {
1342 pdata->md = EVP_get_digestbyname(value);
1343 if (pdata->md == NULL)
1344 return 0;
1345 return 1;
1346 }
1347 return 0;
1348 }
1349
1350 static int pkcs12_test_parse(struct evp_test *t,
1351 const char *keyword, const char *value)
1352 {
1353 struct pbe_data *pdata = t->data;
1354
1355 if (strcmp(keyword, "id") == 0) {
1356 pdata->id = atoi(value);
1357 if (pdata->id <= 0)
1358 return 0;
1359 return 1;
1360 }
1361 return pbkdf2_test_parse(t, keyword, value);
1362 }
1363
1364 static int pbe_test_init(struct evp_test *t, const char *alg)
1365 {
1366 struct pbe_data *pdat;
1367 int pbe_type = 0;
1368
1369 if (strcmp(alg, "scrypt") == 0)
1370 pbe_type = PBE_TYPE_SCRYPT;
1371 else if (strcmp(alg, "pbkdf2") == 0)
1372 pbe_type = PBE_TYPE_PBKDF2;
1373 else if (strcmp(alg, "pkcs12") == 0)
1374 pbe_type = PBE_TYPE_PKCS12;
1375 else
1376 fprintf(stderr, "Unknown pbe algorithm %s\n", alg);
1377 pdat = OPENSSL_malloc(sizeof(*pdat));
1378 pdat->pbe_type = pbe_type;
1379 pdat->pass = NULL;
1380 pdat->salt = NULL;
1381 pdat->N = 0;
1382 pdat->r = 0;
1383 pdat->p = 0;
1384 pdat->maxmem = 0;
1385 pdat->id = 0;
1386 pdat->iter = 0;
1387 pdat->md = NULL;
1388 t->data = pdat;
1389 return 1;
1390 }
1391
1392 static void pbe_test_cleanup(struct evp_test *t)
1393 {
1394 struct pbe_data *pdat = t->data;
1395 test_free(pdat->pass);
1396 test_free(pdat->salt);
1397 test_free(pdat->key);
1398 }
1399
1400 static int pbe_test_parse(struct evp_test *t,
1401 const char *keyword, const char *value)
1402 {
1403 struct pbe_data *pdata = t->data;
1404
1405 if (strcmp(keyword, "Password") == 0)
1406 return test_bin(value, &pdata->pass, &pdata->pass_len);
1407 if (strcmp(keyword, "Salt") == 0)
1408 return test_bin(value, &pdata->salt, &pdata->salt_len);
1409 if (strcmp(keyword, "Key") == 0)
1410 return test_bin(value, &pdata->key, &pdata->key_len);
1411 if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1412 return scrypt_test_parse(t, keyword, value);
1413 else if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1414 return pbkdf2_test_parse(t, keyword, value);
1415 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1416 return pkcs12_test_parse(t, keyword, value);
1417 return 0;
1418 }
1419
1420 static int pbe_test_run(struct evp_test *t)
1421 {
1422 struct pbe_data *pdata = t->data;
1423 const char *err = "INTERNAL_ERROR";
1424 unsigned char *key;
1425
1426 key = OPENSSL_malloc(pdata->key_len);
1427 if (!key)
1428 goto err;
1429 if (pdata->pbe_type == PBE_TYPE_PBKDF2) {
1430 err = "PBKDF2_ERROR";
1431 if (PKCS5_PBKDF2_HMAC((char *)pdata->pass, pdata->pass_len,
1432 pdata->salt, pdata->salt_len,
1433 pdata->iter, pdata->md,
1434 pdata->key_len, key) == 0)
1435 goto err;
1436 } else if (pdata->pbe_type == PBE_TYPE_SCRYPT) {
1437 err = "SCRYPT_ERROR";
1438 if (EVP_PBE_scrypt((const char *)pdata->pass, pdata->pass_len,
1439 pdata->salt, pdata->salt_len,
1440 pdata->N, pdata->r, pdata->p, pdata->maxmem,
1441 key, pdata->key_len) == 0)
1442 goto err;
1443 } else if (pdata->pbe_type == PBE_TYPE_PKCS12) {
1444 err = "PKCS12_ERROR";
1445 if (PKCS12_key_gen_uni(pdata->pass, pdata->pass_len,
1446 pdata->salt, pdata->salt_len,
1447 pdata->id, pdata->iter, pdata->key_len,
1448 key, pdata->md) == 0)
1449 goto err;
1450 }
1451 err = "KEY_MISMATCH";
1452 if (check_output(t, pdata->key, key, pdata->key_len))
1453 goto err;
1454 err = NULL;
1455 err:
1456 OPENSSL_free(key);
1457 t->err = err;
1458 return 1;
1459 }
1460
1461 static const struct evp_test_method pbe_test_method = {
1462 "PBE",
1463 pbe_test_init,
1464 pbe_test_cleanup,
1465 pbe_test_parse,
1466 pbe_test_run
1467 };