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