e7396f7b2aab2d96dd3440c57b5e508bddf6fadf
[openssl.git] / test / evp_test.c
1 /*
2  * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <ctype.h>
14 #include <openssl/evp.h>
15 #include <openssl/pem.h>
16 #include <openssl/err.h>
17 #include <openssl/x509v3.h>
18 #include <openssl/pkcs12.h>
19 #include <openssl/kdf.h>
20 #include "internal/numbers.h"
21
22 /* Remove spaces from beginning and end of a string */
23
24 static void remove_space(char **pval)
25 {
26     unsigned char *p = (unsigned char *)*pval, *beginning;
27
28     while (isspace(*p))
29         p++;
30
31     *pval = (char *)(beginning = p);
32
33     p = p + strlen(*pval) - 1;
34
35     /* Remove trailing space */
36     while (p >= beginning && isspace(*p))
37         *p-- = 0;
38 }
39
40 /*
41  * Given a line of the form:
42  *      name = value # comment
43  * extract name and value. NB: modifies passed buffer.
44  */
45
46 static int parse_line(char **pkw, char **pval, char *linebuf)
47 {
48     char *p;
49
50     p = linebuf + strlen(linebuf) - 1;
51
52     if (*p != '\n') {
53         fprintf(stderr, "FATAL: missing EOL\n");
54         exit(1);
55     }
56
57     /* Look for # */
58
59     p = strchr(linebuf, '#');
60
61     if (p)
62         *p = '\0';
63
64     /* Look for = sign */
65     p = strchr(linebuf, '=');
66
67     /* If no '=' exit */
68     if (!p)
69         return 0;
70
71     *p++ = '\0';
72
73     *pkw = linebuf;
74     *pval = p;
75
76     /* Remove spaces from keyword and value */
77     remove_space(pkw);
78     remove_space(pval);
79
80     return 1;
81 }
82
83 /*
84  * Unescape some escape sequences in string literals.
85  * Return the result in a newly allocated buffer.
86  * Currently only supports '\n'.
87  * If the input length is 0, returns a valid 1-byte buffer, but sets
88  * the length to 0.
89  */
90 static unsigned char* unescape(const char *input, size_t input_len,
91                                size_t *out_len)
92 {
93     unsigned char *ret, *p;
94     size_t i;
95     if (input_len == 0) {
96         *out_len = 0;
97         return OPENSSL_zalloc(1);
98     }
99
100     /* Escaping is non-expanding; over-allocate original size for simplicity. */
101     ret = p = OPENSSL_malloc(input_len);
102     if (ret == NULL)
103         return NULL;
104
105     for (i = 0; i < input_len; i++) {
106         if (input[i] == '\\') {
107             if (i == input_len - 1 || input[i+1] != 'n')
108                 goto err;
109             *p++ = '\n';
110             i++;
111         } else {
112             *p++ = input[i];
113         }
114     }
115
116     *out_len = p - ret;
117     return ret;
118
119  err:
120     OPENSSL_free(ret);
121     return NULL;
122 }
123
124 /* For a hex string "value" convert to a binary allocated buffer */
125 static int test_bin(const char *value, unsigned char **buf, size_t *buflen)
126 {
127     long len;
128
129     *buflen = 0;
130
131     /* Check for empty value */
132     if (!*value) {
133         /*
134          * Don't return NULL for zero length buffer.
135          * This is needed for some tests with empty keys: HMAC_Init_ex() expects
136          * a non-NULL key buffer even if the key length is 0, in order to detect
137          * key reset.
138          */
139         *buf = OPENSSL_malloc(1);
140         if (!*buf)
141             return 0;
142         **buf = 0;
143         *buflen = 0;
144         return 1;
145     }
146
147     /* Check for NULL literal */
148     if (strcmp(value, "NULL") == 0) {
149         *buf = NULL;
150         *buflen = 0;
151         return 1;
152     }
153
154     /* Check for string literal */
155     if (value[0] == '"') {
156         size_t vlen;
157         value++;
158         vlen = strlen(value);
159         if (value[vlen - 1] != '"')
160             return 0;
161         vlen--;
162         *buf = unescape(value, vlen, buflen);
163         if (*buf == NULL)
164             return 0;
165         return 1;
166     }
167
168     /* Otherwise assume as hex literal and convert it to binary buffer */
169     *buf = OPENSSL_hexstr2buf(value, &len);
170     if (!*buf) {
171         fprintf(stderr, "Value=%s\n", value);
172         ERR_print_errors_fp(stderr);
173         return -1;
174     }
175     /* Size of input buffer means we'll never overflow */
176     *buflen = len;
177     return 1;
178 }
179 #ifndef OPENSSL_NO_SCRYPT
180 /* Currently only used by scrypt tests */
181 /* Parse unsigned decimal 64 bit integer value */
182 static int test_uint64(const char *value, uint64_t *pr)
183 {
184     const char *p = value;
185     if (!*p) {
186         fprintf(stderr, "Invalid empty integer value\n");
187         return -1;
188     }
189     *pr = 0;
190     while (*p) {
191         if (*pr > UINT64_MAX/10) {
192             fprintf(stderr, "Integer string overflow value=%s\n", value);
193             return -1;
194         }
195         *pr *= 10;
196         if (*p < '0' || *p > '9') {
197             fprintf(stderr, "Invalid integer string value=%s\n", value);
198             return -1;
199         }
200         *pr += *p - '0';
201         p++;
202     }
203     return 1;
204 }
205 #endif
206
207 /* Structure holding test information */
208 struct evp_test {
209     /* file being read */
210     BIO *in;
211     /* temp memory BIO for reading in keys */
212     BIO *key;
213     /* List of public and private keys */
214     struct key_list *private;
215     struct key_list *public;
216     /* method for this test */
217     const struct evp_test_method *meth;
218     /* current line being processed */
219     unsigned int line;
220     /* start line of current test */
221     unsigned int start_line;
222     /* Error string for test */
223     const char *err, *aux_err;
224     /* Expected error value of test */
225     char *expected_err;
226     /* Expected error function string */
227     char *func;
228     /* Expected error reason string */
229     char *reason;
230     /* Number of tests */
231     int ntests;
232     /* Error count */
233     int errors;
234     /* Number of tests skipped */
235     int nskip;
236     /* If output mismatch expected and got value */
237     unsigned char *out_received;
238     size_t out_received_len;
239     unsigned char *out_expected;
240     size_t out_expected_len;
241     /* test specific data */
242     void *data;
243     /* Current test should be skipped */
244     int skip;
245 };
246
247 struct key_list {
248     char *name;
249     EVP_PKEY *key;
250     struct key_list *next;
251 };
252
253 /* Test method structure */
254 struct evp_test_method {
255     /* Name of test as it appears in file */
256     const char *name;
257     /* Initialise test for "alg" */
258     int (*init) (struct evp_test * t, const char *alg);
259     /* Clean up method */
260     void (*cleanup) (struct evp_test * t);
261     /* Test specific name value pair processing */
262     int (*parse) (struct evp_test * t, const char *name, const char *value);
263     /* Run the test itself */
264     int (*run_test) (struct evp_test * t);
265 };
266
267 static const struct evp_test_method digest_test_method, cipher_test_method;
268 static const struct evp_test_method mac_test_method;
269 static const struct evp_test_method psign_test_method, pverify_test_method;
270 static const struct evp_test_method pdecrypt_test_method;
271 static const struct evp_test_method pverify_recover_test_method;
272 static const struct evp_test_method pderive_test_method;
273 static const struct evp_test_method pbe_test_method;
274 static const struct evp_test_method encode_test_method;
275 static const struct evp_test_method kdf_test_method;
276 static const struct evp_test_method keypair_test_method;
277
278 static const struct evp_test_method *evp_test_list[] = {
279     &digest_test_method,
280     &cipher_test_method,
281     &mac_test_method,
282     &psign_test_method,
283     &pverify_test_method,
284     &pdecrypt_test_method,
285     &pverify_recover_test_method,
286     &pderive_test_method,
287     &pbe_test_method,
288     &encode_test_method,
289     &kdf_test_method,
290     &keypair_test_method,
291     NULL
292 };
293
294 static const struct evp_test_method *evp_find_test(const char *name)
295 {
296     const struct evp_test_method **tt;
297
298     for (tt = evp_test_list; *tt; tt++) {
299         if (strcmp(name, (*tt)->name) == 0)
300             return *tt;
301     }
302     return NULL;
303 }
304
305 static void hex_print(const char *name, const unsigned char *buf, size_t len)
306 {
307     size_t i;
308     fprintf(stderr, "%s ", name);
309     for (i = 0; i < len; i++)
310         fprintf(stderr, "%02X", buf[i]);
311     fputs("\n", stderr);
312 }
313
314 static void free_expected(struct evp_test *t)
315 {
316     OPENSSL_free(t->expected_err);
317     t->expected_err = NULL;
318     OPENSSL_free(t->func);
319     t->func = NULL;
320     OPENSSL_free(t->reason);
321     t->reason = NULL;
322     OPENSSL_free(t->out_expected);
323     OPENSSL_free(t->out_received);
324     t->out_expected = NULL;
325     t->out_received = NULL;
326     t->out_expected_len = 0;
327     t->out_received_len = 0;
328     /* Literals. */
329     t->err = NULL;
330 }
331
332 static void print_expected(struct evp_test *t)
333 {
334     if (t->out_expected == NULL && t->out_received == NULL)
335         return;
336     hex_print("Expected:", t->out_expected, t->out_expected_len);
337     hex_print("Got:     ", t->out_received, t->out_received_len);
338     free_expected(t);
339 }
340
341 static int check_test_error(struct evp_test *t)
342 {
343     unsigned long err;
344     const char *func;
345     const char *reason;
346     if (!t->err && !t->expected_err)
347         return 1;
348     if (t->err && !t->expected_err) {
349         if (t->aux_err != NULL) {
350             fprintf(stderr, "Test line %d(%s): unexpected error %s\n",
351                     t->start_line, t->aux_err, t->err);
352         } else {
353             fprintf(stderr, "Test line %d: unexpected error %s\n",
354                     t->start_line, t->err);
355         }
356         print_expected(t);
357         return 0;
358     }
359     if (!t->err && t->expected_err) {
360         fprintf(stderr, "Test line %d: succeeded expecting %s\n",
361                 t->start_line, t->expected_err);
362         return 0;
363     }
364
365     if (strcmp(t->err, t->expected_err) != 0) {
366         fprintf(stderr, "Test line %d: expecting %s got %s\n",
367                 t->start_line, t->expected_err, t->err);
368         return 0;
369     }
370
371     if (t->func == NULL && t->reason == NULL)
372         return 1;
373
374     if (t->func == NULL || t->reason == NULL) {
375         fprintf(stderr, "Test line %d: missing function or reason code\n",
376                 t->start_line);
377         return 0;
378     }
379
380     err = ERR_peek_error();
381     if (err == 0) {
382         fprintf(stderr, "Test line %d, expected error \"%s:%s\" not set\n",
383                 t->start_line, t->func, t->reason);
384         return 0;
385     }
386
387     func = ERR_func_error_string(err);
388     reason = ERR_reason_error_string(err);
389
390     if (func == NULL && reason == NULL) {
391         fprintf(stderr, "Test line %d: expected error \"%s:%s\", no strings available.  Skipping...\n",
392                 t->start_line, t->func, t->reason);
393         return 1;
394     }
395
396     if (strcmp(func, t->func) == 0 && strcmp(reason, t->reason) == 0)
397         return 1;
398
399     fprintf(stderr, "Test line %d: expected error \"%s:%s\", got \"%s:%s\"\n",
400             t->start_line, t->func, t->reason, func, reason);
401
402     return 0;
403 }
404
405 /* Setup a new test, run any existing test */
406
407 static int setup_test(struct evp_test *t, const struct evp_test_method *tmeth)
408 {
409     /* If we already have a test set up run it */
410     if (t->meth) {
411         t->ntests++;
412         if (t->skip) {
413             t->nskip++;
414         } else {
415             /* run the test */
416             if (t->err == NULL && t->meth->run_test(t) != 1) {
417                 fprintf(stderr, "%s test error line %d\n",
418                         t->meth->name, t->start_line);
419                 return 0;
420             }
421             if (!check_test_error(t)) {
422                 if (t->err)
423                     ERR_print_errors_fp(stderr);
424                 t->errors++;
425             }
426         }
427         /* clean it up */
428         ERR_clear_error();
429         if (t->data != NULL) {
430             t->meth->cleanup(t);
431             OPENSSL_free(t->data);
432             t->data = NULL;
433         }
434         OPENSSL_free(t->expected_err);
435         t->expected_err = NULL;
436         free_expected(t);
437     }
438     t->meth = tmeth;
439     return 1;
440 }
441
442 static int find_key(EVP_PKEY **ppk, const char *name, struct key_list *lst)
443 {
444     for (; lst; lst = lst->next) {
445         if (strcmp(lst->name, name) == 0) {
446             if (ppk)
447                 *ppk = lst->key;
448             return 1;
449         }
450     }
451     return 0;
452 }
453
454 static void free_key_list(struct key_list *lst)
455 {
456     while (lst != NULL) {
457         struct key_list *ltmp;
458         EVP_PKEY_free(lst->key);
459         OPENSSL_free(lst->name);
460         ltmp = lst->next;
461         OPENSSL_free(lst);
462         lst = ltmp;
463     }
464 }
465
466 static int check_unsupported()
467 {
468     long err = ERR_peek_error();
469     if (ERR_GET_LIB(err) == ERR_LIB_EVP
470         && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
471         ERR_clear_error();
472         return 1;
473     }
474 #ifndef OPENSSL_NO_EC
475     /*
476      * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
477      * hint to an unsupported algorithm/curve (e.g. if binary EC support is
478      * disabled).
479      */
480     if (ERR_GET_LIB(err) == ERR_LIB_EC
481         && ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP) {
482         ERR_clear_error();
483         return 1;
484     }
485 #endif /* OPENSSL_NO_EC */
486     return 0;
487 }
488
489
490 static int read_key(struct evp_test *t)
491 {
492     char tmpbuf[80];
493     if (t->key == NULL)
494         t->key = BIO_new(BIO_s_mem());
495     else if (BIO_reset(t->key) <= 0)
496         return 0;
497     if (t->key == NULL) {
498         fprintf(stderr, "Error allocating key memory BIO\n");
499         return 0;
500     }
501     /* Read to PEM end line and place content in memory BIO */
502     while (BIO_gets(t->in, tmpbuf, sizeof(tmpbuf))) {
503         t->line++;
504         if (BIO_puts(t->key, tmpbuf) <= 0) {
505             fprintf(stderr, "Error writing to key memory BIO\n");
506             return 0;
507         }
508         if (strncmp(tmpbuf, "-----END", 8) == 0)
509             return 1;
510     }
511     fprintf(stderr, "Can't find key end\n");
512     return 0;
513 }
514
515 static int process_test(struct evp_test *t, char *buf, int verbose)
516 {
517     char *keyword = NULL, *value = NULL;
518     int rv = 0, add_key = 0;
519     struct key_list **lst = NULL, *key = NULL;
520     EVP_PKEY *pk = NULL;
521     const struct evp_test_method *tmeth = NULL;
522     if (verbose)
523         fputs(buf, stdout);
524     if (!parse_line(&keyword, &value, buf))
525         return 1;
526     if (strcmp(keyword, "PrivateKey") == 0) {
527         if (!read_key(t))
528             return 0;
529         pk = PEM_read_bio_PrivateKey(t->key, NULL, 0, NULL);
530         if (pk == NULL && !check_unsupported()) {
531             fprintf(stderr, "Error reading private key %s\n", value);
532             ERR_print_errors_fp(stderr);
533             return 0;
534         }
535         lst = &t->private;
536         add_key = 1;
537     }
538     if (strcmp(keyword, "PublicKey") == 0) {
539         if (!read_key(t))
540             return 0;
541         pk = PEM_read_bio_PUBKEY(t->key, NULL, 0, NULL);
542         if (pk == NULL && !check_unsupported()) {
543             fprintf(stderr, "Error reading public key %s\n", value);
544             ERR_print_errors_fp(stderr);
545             return 0;
546         }
547         lst = &t->public;
548         add_key = 1;
549     }
550     /* If we have a key add to list */
551     if (add_key) {
552         if (find_key(NULL, value, *lst)) {
553             fprintf(stderr, "Duplicate key %s\n", value);
554             return 0;
555         }
556         key = OPENSSL_malloc(sizeof(*key));
557         if (!key)
558             return 0;
559         key->name = OPENSSL_strdup(value);
560         key->key = pk;
561         key->next = *lst;
562         *lst = key;
563         return 1;
564     }
565
566     /* See if keyword corresponds to a test start */
567     tmeth = evp_find_test(keyword);
568     if (tmeth) {
569         if (!setup_test(t, tmeth))
570             return 0;
571         t->start_line = t->line;
572         t->skip = 0;
573         if (!tmeth->init(t, value)) {
574             fprintf(stderr, "Unknown %s: %s\n", keyword, value);
575             return 0;
576         }
577         return 1;
578     } else if (t->skip) {
579         return 1;
580     } else if (strcmp(keyword, "Result") == 0) {
581         if (t->expected_err) {
582             fprintf(stderr, "Line %d: multiple result lines\n", t->line);
583             return 0;
584         }
585         t->expected_err = OPENSSL_strdup(value);
586         if (t->expected_err == NULL)
587             return 0;
588     } else if (strcmp(keyword, "Function") == 0) {
589         if (t->func != NULL) {
590             fprintf(stderr, "Line %d: multiple function lines\n", t->line);
591             return 0;
592         }
593         t->func = OPENSSL_strdup(value);
594         if (t->func == NULL)
595             return 0;
596     } else if (strcmp(keyword, "Reason") == 0) {
597         if (t->reason != NULL) {
598             fprintf(stderr, "Line %d: multiple reason lines\n", t->line);
599             return 0;
600         }
601         t->reason = OPENSSL_strdup(value);
602         if (t->reason == NULL)
603             return 0;
604     } else {
605         /* Must be test specific line: try to parse it */
606         if (t->meth)
607             rv = t->meth->parse(t, keyword, value);
608
609         if (rv == 0)
610             fprintf(stderr, "line %d: unexpected keyword %s\n",
611                     t->line, keyword);
612
613         if (rv < 0)
614             fprintf(stderr, "line %d: error processing keyword %s\n",
615                     t->line, keyword);
616         if (rv <= 0)
617             return 0;
618     }
619     return 1;
620 }
621
622 static int check_var_length_output(struct evp_test *t,
623                                    const unsigned char *expected,
624                                    size_t expected_len,
625                                    const unsigned char *received,
626                                    size_t received_len)
627 {
628     if (expected_len == received_len &&
629         memcmp(expected, received, expected_len) == 0) {
630         return 0;
631     }
632
633     /* The result printing code expects a non-NULL buffer. */
634     t->out_expected = OPENSSL_memdup(expected, expected_len ? expected_len : 1);
635     t->out_expected_len = expected_len;
636     t->out_received = OPENSSL_memdup(received, received_len ? received_len : 1);
637     t->out_received_len = received_len;
638     if (t->out_expected == NULL || t->out_received == NULL) {
639         fprintf(stderr, "Memory allocation error!\n");
640         exit(1);
641     }
642     return 1;
643 }
644
645 static int check_output(struct evp_test *t,
646                         const unsigned char *expected,
647                         const unsigned char *received,
648                         size_t len)
649 {
650     return check_var_length_output(t, expected, len, received, len);
651 }
652
653 int main(int argc, char **argv)
654 {
655     BIO *in = NULL;
656     char buf[10240];
657     struct evp_test t;
658
659     if (argc != 2) {
660         fprintf(stderr, "usage: evp_test testfile.txt\n");
661         return 1;
662     }
663
664     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
665
666     memset(&t, 0, sizeof(t));
667     t.start_line = -1;
668     in = BIO_new_file(argv[1], "rb");
669     if (in == NULL) {
670         fprintf(stderr, "Can't open %s for reading\n", argv[1]);
671         return 1;
672     }
673     t.in = in;
674     t.err = NULL;
675     while (BIO_gets(in, buf, sizeof(buf))) {
676         t.line++;
677         if (!process_test(&t, buf, 0))
678             exit(1);
679     }
680     /* Run any final test we have */
681     if (!setup_test(&t, NULL))
682         exit(1);
683     fprintf(stderr, "%d tests completed with %d errors, %d skipped\n",
684             t.ntests, t.errors, t.nskip);
685     free_key_list(t.public);
686     free_key_list(t.private);
687     BIO_free(t.key);
688     BIO_free(in);
689
690 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
691     if (CRYPTO_mem_leaks_fp(stderr) <= 0)
692         return 1;
693 #endif
694     if (t.errors)
695         return 1;
696     return 0;
697 }
698
699 static void test_free(void *d)
700 {
701     OPENSSL_free(d);
702 }
703
704 /* Message digest tests */
705
706 struct digest_data {
707     /* Digest this test is for */
708     const EVP_MD *digest;
709     /* Input to digest */
710     unsigned char *input;
711     size_t input_len;
712     /* Repeat count for input */
713     size_t nrpt;
714     /* Expected output */
715     unsigned char *output;
716     size_t output_len;
717 };
718
719 static int digest_test_init(struct evp_test *t, const char *alg)
720 {
721     const EVP_MD *digest;
722     struct digest_data *mdat;
723     digest = EVP_get_digestbyname(alg);
724     if (!digest) {
725         /* If alg has an OID assume disabled algorithm */
726         if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
727             t->skip = 1;
728             return 1;
729         }
730         return 0;
731     }
732     mdat = OPENSSL_malloc(sizeof(*mdat));
733     mdat->digest = digest;
734     mdat->input = NULL;
735     mdat->output = NULL;
736     mdat->nrpt = 1;
737     t->data = mdat;
738     return 1;
739 }
740
741 static void digest_test_cleanup(struct evp_test *t)
742 {
743     struct digest_data *mdat = t->data;
744     test_free(mdat->input);
745     test_free(mdat->output);
746 }
747
748 static int digest_test_parse(struct evp_test *t,
749                              const char *keyword, const char *value)
750 {
751     struct digest_data *mdata = t->data;
752     if (strcmp(keyword, "Input") == 0)
753         return test_bin(value, &mdata->input, &mdata->input_len);
754     if (strcmp(keyword, "Output") == 0)
755         return test_bin(value, &mdata->output, &mdata->output_len);
756     if (strcmp(keyword, "Count") == 0) {
757         long nrpt = atoi(value);
758         if (nrpt <= 0)
759             return 0;
760         mdata->nrpt = (size_t)nrpt;
761         return 1;
762     }
763     return 0;
764 }
765
766 static int digest_test_run(struct evp_test *t)
767 {
768     struct digest_data *mdata = t->data;
769     size_t i;
770     const char *err = "INTERNAL_ERROR";
771     EVP_MD_CTX *mctx;
772     unsigned char md[EVP_MAX_MD_SIZE];
773     unsigned int md_len;
774     mctx = EVP_MD_CTX_new();
775     if (!mctx)
776         goto err;
777     err = "DIGESTINIT_ERROR";
778     if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))
779         goto err;
780     err = "DIGESTUPDATE_ERROR";
781     for (i = 0; i < mdata->nrpt; i++) {
782         if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))
783             goto err;
784     }
785     err = "DIGESTFINAL_ERROR";
786     if (!EVP_DigestFinal(mctx, md, &md_len))
787         goto err;
788     err = "DIGEST_LENGTH_MISMATCH";
789     if (md_len != mdata->output_len)
790         goto err;
791     err = "DIGEST_MISMATCH";
792     if (check_output(t, mdata->output, md, md_len))
793         goto err;
794     err = NULL;
795  err:
796     EVP_MD_CTX_free(mctx);
797     t->err = err;
798     return 1;
799 }
800
801 static const struct evp_test_method digest_test_method = {
802     "Digest",
803     digest_test_init,
804     digest_test_cleanup,
805     digest_test_parse,
806     digest_test_run
807 };
808
809 /* Cipher tests */
810 struct cipher_data {
811     const EVP_CIPHER *cipher;
812     int enc;
813     /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
814     int aead;
815     unsigned char *key;
816     size_t key_len;
817     unsigned char *iv;
818     size_t iv_len;
819     unsigned char *plaintext;
820     size_t plaintext_len;
821     unsigned char *ciphertext;
822     size_t ciphertext_len;
823     /* GCM, CCM only */
824     unsigned char *aad;
825     size_t aad_len;
826     unsigned char *tag;
827     size_t tag_len;
828 };
829
830 static int cipher_test_init(struct evp_test *t, const char *alg)
831 {
832     const EVP_CIPHER *cipher;
833     struct cipher_data *cdat = t->data;
834     cipher = EVP_get_cipherbyname(alg);
835     if (!cipher) {
836         /* If alg has an OID assume disabled algorithm */
837         if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
838             t->skip = 1;
839             return 1;
840         }
841         return 0;
842     }
843     cdat = OPENSSL_malloc(sizeof(*cdat));
844     cdat->cipher = cipher;
845     cdat->enc = -1;
846     cdat->key = NULL;
847     cdat->iv = NULL;
848     cdat->ciphertext = NULL;
849     cdat->plaintext = NULL;
850     cdat->aad = NULL;
851     cdat->tag = NULL;
852     t->data = cdat;
853     if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
854         || EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE
855         || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
856         cdat->aead = EVP_CIPHER_mode(cipher);
857     else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
858         cdat->aead = -1;
859     else
860         cdat->aead = 0;
861
862     return 1;
863 }
864
865 static void cipher_test_cleanup(struct evp_test *t)
866 {
867     struct cipher_data *cdat = t->data;
868     test_free(cdat->key);
869     test_free(cdat->iv);
870     test_free(cdat->ciphertext);
871     test_free(cdat->plaintext);
872     test_free(cdat->aad);
873     test_free(cdat->tag);
874 }
875
876 static int cipher_test_parse(struct evp_test *t, const char *keyword,
877                              const char *value)
878 {
879     struct cipher_data *cdat = t->data;
880     if (strcmp(keyword, "Key") == 0)
881         return test_bin(value, &cdat->key, &cdat->key_len);
882     if (strcmp(keyword, "IV") == 0)
883         return test_bin(value, &cdat->iv, &cdat->iv_len);
884     if (strcmp(keyword, "Plaintext") == 0)
885         return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
886     if (strcmp(keyword, "Ciphertext") == 0)
887         return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
888     if (cdat->aead) {
889         if (strcmp(keyword, "AAD") == 0)
890             return test_bin(value, &cdat->aad, &cdat->aad_len);
891         if (strcmp(keyword, "Tag") == 0)
892             return test_bin(value, &cdat->tag, &cdat->tag_len);
893     }
894
895     if (strcmp(keyword, "Operation") == 0) {
896         if (strcmp(value, "ENCRYPT") == 0)
897             cdat->enc = 1;
898         else if (strcmp(value, "DECRYPT") == 0)
899             cdat->enc = 0;
900         else
901             return 0;
902         return 1;
903     }
904     return 0;
905 }
906
907 static int cipher_test_enc(struct evp_test *t, int enc,
908                            size_t out_misalign, size_t inp_misalign, int frag)
909 {
910     struct cipher_data *cdat = t->data;
911     unsigned char *in, *out, *tmp = NULL;
912     size_t in_len, out_len, donelen = 0;
913     int tmplen, chunklen, tmpflen;
914     EVP_CIPHER_CTX *ctx = NULL;
915     const char *err;
916     err = "INTERNAL_ERROR";
917     ctx = EVP_CIPHER_CTX_new();
918     if (!ctx)
919         goto err;
920     EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
921     if (enc) {
922         in = cdat->plaintext;
923         in_len = cdat->plaintext_len;
924         out = cdat->ciphertext;
925         out_len = cdat->ciphertext_len;
926     } else {
927         in = cdat->ciphertext;
928         in_len = cdat->ciphertext_len;
929         out = cdat->plaintext;
930         out_len = cdat->plaintext_len;
931     }
932     if (inp_misalign == (size_t)-1) {
933         /*
934          * Exercise in-place encryption
935          */
936         tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
937         if (!tmp)
938             goto err;
939         in = memcpy(tmp + out_misalign, in, in_len);
940     } else {
941         inp_misalign += 16 - ((out_misalign + in_len) & 15);
942         /*
943          * 'tmp' will store both output and copy of input. We make the copy
944          * of input to specifically aligned part of 'tmp'. So we just
945          * figured out how much padding would ensure the required alignment,
946          * now we allocate extended buffer and finally copy the input just
947          * past inp_misalign in expression below. Output will be written
948          * past out_misalign...
949          */
950         tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
951                              inp_misalign + in_len);
952         if (!tmp)
953             goto err;
954         in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
955                     inp_misalign, in, in_len);
956     }
957     err = "CIPHERINIT_ERROR";
958     if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
959         goto err;
960     err = "INVALID_IV_LENGTH";
961     if (cdat->iv) {
962         if (cdat->aead) {
963             if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
964                                      cdat->iv_len, 0))
965                 goto err;
966         } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
967             goto err;
968     }
969     if (cdat->aead) {
970         unsigned char *tag;
971         /*
972          * If encrypting or OCB just set tag length initially, otherwise
973          * set tag length and value.
974          */
975         if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {
976             err = "TAG_LENGTH_SET_ERROR";
977             tag = NULL;
978         } else {
979             err = "TAG_SET_ERROR";
980             tag = cdat->tag;
981         }
982         if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {
983             if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
984                                      cdat->tag_len, tag))
985                 goto err;
986         }
987     }
988
989     err = "INVALID_KEY_LENGTH";
990     if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len))
991         goto err;
992     err = "KEY_SET_ERROR";
993     if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1))
994         goto err;
995
996     if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {
997         if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
998                                  cdat->tag_len, cdat->tag)) {
999             err = "TAG_SET_ERROR";
1000             goto err;
1001         }
1002     }
1003
1004     if (cdat->aead == EVP_CIPH_CCM_MODE) {
1005         if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
1006             err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
1007             goto err;
1008         }
1009     }
1010     if (cdat->aad) {
1011         err = "AAD_SET_ERROR";
1012         if (!frag) {
1013             if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad,
1014                                   cdat->aad_len))
1015                 goto err;
1016         } else {
1017             /*
1018              * Supply the AAD in chunks less than the block size where possible
1019              */
1020             if (cdat->aad_len > 0) {
1021                 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad, 1))
1022                     goto err;
1023                 donelen++;
1024             }
1025             if (cdat->aad_len > 2) {
1026                 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad + donelen,
1027                                       cdat->aad_len - 2))
1028                     goto err;
1029                 donelen += cdat->aad_len - 2;
1030             }
1031             if (cdat->aad_len > 1
1032                     && !EVP_CipherUpdate(ctx, NULL, &chunklen,
1033                                          cdat->aad + donelen, 1))
1034                 goto err;
1035         }
1036     }
1037     EVP_CIPHER_CTX_set_padding(ctx, 0);
1038     err = "CIPHERUPDATE_ERROR";
1039     tmplen = 0;
1040     if (!frag) {
1041         /* We supply the data all in one go */
1042         if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
1043             goto err;
1044     } else {
1045         /* Supply the data in chunks less than the block size where possible */
1046         if (in_len > 0) {
1047             if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
1048                 goto err;
1049             tmplen += chunklen;
1050             in++;
1051             in_len--;
1052         }
1053         if (in_len > 1) {
1054             if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
1055                                   in, in_len - 1))
1056                 goto err;
1057             tmplen += chunklen;
1058             in += in_len - 1;
1059             in_len = 1;
1060         }
1061         if (in_len > 0 ) {
1062             if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
1063                                   in, 1))
1064                 goto err;
1065             tmplen += chunklen;
1066         }
1067     }
1068     err = "CIPHERFINAL_ERROR";
1069     if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen))
1070         goto err;
1071     err = "LENGTH_MISMATCH";
1072     if (out_len != (size_t)(tmplen + tmpflen))
1073         goto err;
1074     err = "VALUE_MISMATCH";
1075     if (check_output(t, out, tmp + out_misalign, out_len))
1076         goto err;
1077     if (enc && cdat->aead) {
1078         unsigned char rtag[16];
1079         if (cdat->tag_len > sizeof(rtag)) {
1080             err = "TAG_LENGTH_INTERNAL_ERROR";
1081             goto err;
1082         }
1083         if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
1084                                  cdat->tag_len, rtag)) {
1085             err = "TAG_RETRIEVE_ERROR";
1086             goto err;
1087         }
1088         if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {
1089             err = "TAG_VALUE_MISMATCH";
1090             goto err;
1091         }
1092     }
1093     err = NULL;
1094  err:
1095     OPENSSL_free(tmp);
1096     EVP_CIPHER_CTX_free(ctx);
1097     t->err = err;
1098     return err ? 0 : 1;
1099 }
1100
1101 static int cipher_test_run(struct evp_test *t)
1102 {
1103     struct cipher_data *cdat = t->data;
1104     int rv, frag = 0;
1105     size_t out_misalign, inp_misalign;
1106
1107     if (!cdat->key) {
1108         t->err = "NO_KEY";
1109         return 0;
1110     }
1111     if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
1112         /* IV is optional and usually omitted in wrap mode */
1113         if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
1114             t->err = "NO_IV";
1115             return 0;
1116         }
1117     }
1118     if (cdat->aead && !cdat->tag) {
1119         t->err = "NO_TAG";
1120         return 0;
1121     }
1122     for (out_misalign = 0; out_misalign <= 1;) {
1123         static char aux_err[64];
1124         t->aux_err = aux_err;
1125         for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
1126             if (inp_misalign == (size_t)-1) {
1127                 /* kludge: inp_misalign == -1 means "exercise in-place" */
1128                 BIO_snprintf(aux_err, sizeof(aux_err),
1129                              "%s in-place, %sfragmented",
1130                              out_misalign ? "misaligned" : "aligned",
1131                              frag ? "" : "not ");
1132             } else {
1133                 BIO_snprintf(aux_err, sizeof(aux_err),
1134                              "%s output and %s input, %sfragmented",
1135                              out_misalign ? "misaligned" : "aligned",
1136                              inp_misalign ? "misaligned" : "aligned",
1137                              frag ? "" : "not ");
1138             }
1139             if (cdat->enc) {
1140                 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
1141                 /* Not fatal errors: return */
1142                 if (rv != 1) {
1143                     if (rv < 0)
1144                         return 0;
1145                     return 1;
1146                 }
1147             }
1148             if (cdat->enc != 1) {
1149                 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
1150                 /* Not fatal errors: return */
1151                 if (rv != 1) {
1152                     if (rv < 0)
1153                         return 0;
1154                     return 1;
1155                 }
1156             }
1157         }
1158
1159         if (out_misalign == 1 && frag == 0) {
1160             /*
1161              * XTS, CCM and Wrap modes have special requirements about input
1162              * lengths so we don't fragment for those
1163              */
1164             if (cdat->aead == EVP_CIPH_CCM_MODE
1165                     || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
1166                      || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
1167                 break;
1168             out_misalign = 0;
1169             frag++;
1170         } else {
1171             out_misalign++;
1172         }
1173     }
1174     t->aux_err = NULL;
1175
1176     return 1;
1177 }
1178
1179 static const struct evp_test_method cipher_test_method = {
1180     "Cipher",
1181     cipher_test_init,
1182     cipher_test_cleanup,
1183     cipher_test_parse,
1184     cipher_test_run
1185 };
1186
1187 struct mac_data {
1188     /* MAC type */
1189     int type;
1190     /* Algorithm string for this MAC */
1191     char *alg;
1192     /* MAC key */
1193     unsigned char *key;
1194     size_t key_len;
1195     /* Input to MAC */
1196     unsigned char *input;
1197     size_t input_len;
1198     /* Expected output */
1199     unsigned char *output;
1200     size_t output_len;
1201 };
1202
1203 static int mac_test_init(struct evp_test *t, const char *alg)
1204 {
1205     int type;
1206     struct mac_data *mdat;
1207     if (strcmp(alg, "HMAC") == 0) {
1208         type = EVP_PKEY_HMAC;
1209     } else if (strcmp(alg, "CMAC") == 0) {
1210 #ifndef OPENSSL_NO_CMAC
1211         type = EVP_PKEY_CMAC;
1212 #else
1213         t->skip = 1;
1214         return 1;
1215 #endif
1216     } else if (strcmp(alg, "Poly1305") == 0) {
1217 #ifndef OPENSSL_NO_POLY1305
1218         type = EVP_PKEY_POLY1305;
1219 #else
1220         t->skip = 1;
1221         return 1;
1222 #endif
1223     } else if (strcmp(alg, "SipHash") == 0) {
1224 #ifndef OPENSSL_NO_SIPHASH
1225         type = EVP_PKEY_SIPHASH;
1226 #else
1227         t->skip = 1;
1228         return 1;
1229 #endif
1230     } else
1231         return 0;
1232
1233     mdat = OPENSSL_malloc(sizeof(*mdat));
1234     mdat->type = type;
1235     mdat->alg = NULL;
1236     mdat->key = NULL;
1237     mdat->input = NULL;
1238     mdat->output = NULL;
1239     t->data = mdat;
1240     return 1;
1241 }
1242
1243 static void mac_test_cleanup(struct evp_test *t)
1244 {
1245     struct mac_data *mdat = t->data;
1246     test_free(mdat->alg);
1247     test_free(mdat->key);
1248     test_free(mdat->input);
1249     test_free(mdat->output);
1250 }
1251
1252 static int mac_test_parse(struct evp_test *t,
1253                           const char *keyword, const char *value)
1254 {
1255     struct mac_data *mdata = t->data;
1256     if (strcmp(keyword, "Key") == 0)
1257         return test_bin(value, &mdata->key, &mdata->key_len);
1258     if (strcmp(keyword, "Algorithm") == 0) {
1259         mdata->alg = OPENSSL_strdup(value);
1260         if (!mdata->alg)
1261             return 0;
1262         return 1;
1263     }
1264     if (strcmp(keyword, "Input") == 0)
1265         return test_bin(value, &mdata->input, &mdata->input_len);
1266     if (strcmp(keyword, "Output") == 0)
1267         return test_bin(value, &mdata->output, &mdata->output_len);
1268     return 0;
1269 }
1270
1271 static int mac_test_run(struct evp_test *t)
1272 {
1273     struct mac_data *mdata = t->data;
1274     const char *err = "INTERNAL_ERROR";
1275     EVP_MD_CTX *mctx = NULL;
1276     EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1277     EVP_PKEY *key = NULL;
1278     const EVP_MD *md = NULL;
1279     unsigned char *mac = NULL;
1280     size_t mac_len;
1281
1282 #ifdef OPENSSL_NO_DES
1283     if (mdata->alg != NULL && strstr(mdata->alg, "DES") != NULL) {
1284         /* Skip DES */
1285         err = NULL;
1286         goto err;
1287     }
1288 #endif
1289
1290     err = "MAC_PKEY_CTX_ERROR";
1291     genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL);
1292     if (!genctx)
1293         goto err;
1294
1295     err = "MAC_KEYGEN_INIT_ERROR";
1296     if (EVP_PKEY_keygen_init(genctx) <= 0)
1297         goto err;
1298     if (mdata->type == EVP_PKEY_CMAC) {
1299         err = "MAC_ALGORITHM_SET_ERROR";
1300         if (EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0)
1301             goto err;
1302     }
1303
1304     err = "MAC_KEY_SET_ERROR";
1305     if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0)
1306         goto err;
1307
1308     err = "MAC_KEY_GENERATE_ERROR";
1309     if (EVP_PKEY_keygen(genctx, &key) <= 0)
1310         goto err;
1311     if (mdata->type == EVP_PKEY_HMAC) {
1312         err = "MAC_ALGORITHM_SET_ERROR";
1313         md = EVP_get_digestbyname(mdata->alg);
1314         if (!md)
1315             goto err;
1316     }
1317     mctx = EVP_MD_CTX_new();
1318     if (!mctx)
1319         goto err;
1320     err = "DIGESTSIGNINIT_ERROR";
1321     if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key))
1322         goto err;
1323
1324     err = "DIGESTSIGNUPDATE_ERROR";
1325     if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len))
1326         goto err;
1327     err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1328     if (!EVP_DigestSignFinal(mctx, NULL, &mac_len))
1329         goto err;
1330     mac = OPENSSL_malloc(mac_len);
1331     if (!mac) {
1332         fprintf(stderr, "Error allocating mac buffer!\n");
1333         exit(1);
1334     }
1335     if (!EVP_DigestSignFinal(mctx, mac, &mac_len))
1336         goto err;
1337     err = "MAC_LENGTH_MISMATCH";
1338     if (mac_len != mdata->output_len)
1339         goto err;
1340     err = "MAC_MISMATCH";
1341     if (check_output(t, mdata->output, mac, mac_len))
1342         goto err;
1343     err = NULL;
1344  err:
1345     EVP_MD_CTX_free(mctx);
1346     OPENSSL_free(mac);
1347     EVP_PKEY_CTX_free(genctx);
1348     EVP_PKEY_free(key);
1349     t->err = err;
1350     return 1;
1351 }
1352
1353 static const struct evp_test_method mac_test_method = {
1354     "MAC",
1355     mac_test_init,
1356     mac_test_cleanup,
1357     mac_test_parse,
1358     mac_test_run
1359 };
1360
1361 /*
1362  * Public key operations. These are all very similar and can share
1363  * a lot of common code.
1364  */
1365
1366 struct pkey_data {
1367     /* Context for this operation */
1368     EVP_PKEY_CTX *ctx;
1369     /* Key operation to perform */
1370     int (*keyop) (EVP_PKEY_CTX *ctx,
1371                   unsigned char *sig, size_t *siglen,
1372                   const unsigned char *tbs, size_t tbslen);
1373     /* Input to MAC */
1374     unsigned char *input;
1375     size_t input_len;
1376     /* Expected output */
1377     unsigned char *output;
1378     size_t output_len;
1379 };
1380
1381 /*
1382  * Perform public key operation setup: lookup key, allocated ctx and call
1383  * the appropriate initialisation function
1384  */
1385 static int pkey_test_init(struct evp_test *t, const char *name,
1386                           int use_public,
1387                           int (*keyopinit) (EVP_PKEY_CTX *ctx),
1388                           int (*keyop) (EVP_PKEY_CTX *ctx,
1389                                         unsigned char *sig, size_t *siglen,
1390                                         const unsigned char *tbs,
1391                                         size_t tbslen)
1392     )
1393 {
1394     struct pkey_data *kdata;
1395     EVP_PKEY *pkey = NULL;
1396     int rv = 0;
1397     if (use_public)
1398         rv = find_key(&pkey, name, t->public);
1399     if (!rv)
1400         rv = find_key(&pkey, name, t->private);
1401     if (!rv || pkey == NULL) {
1402         t->skip = 1;
1403         return 1;
1404     }
1405
1406     kdata = OPENSSL_malloc(sizeof(*kdata));
1407     if (!kdata) {
1408         EVP_PKEY_free(pkey);
1409         return 0;
1410     }
1411     kdata->ctx = NULL;
1412     kdata->input = NULL;
1413     kdata->output = NULL;
1414     kdata->keyop = keyop;
1415     t->data = kdata;
1416     kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL);
1417     if (!kdata->ctx)
1418         return 0;
1419     if (keyopinit(kdata->ctx) <= 0)
1420         t->err = "KEYOP_INIT_ERROR";
1421     return 1;
1422 }
1423
1424 static void pkey_test_cleanup(struct evp_test *t)
1425 {
1426     struct pkey_data *kdata = t->data;
1427
1428     OPENSSL_free(kdata->input);
1429     OPENSSL_free(kdata->output);
1430     EVP_PKEY_CTX_free(kdata->ctx);
1431 }
1432
1433 static int pkey_test_ctrl(struct evp_test *t, EVP_PKEY_CTX *pctx,
1434                           const char *value)
1435 {
1436     int rv;
1437     char *p, *tmpval;
1438
1439     tmpval = OPENSSL_strdup(value);
1440     if (tmpval == NULL)
1441         return 0;
1442     p = strchr(tmpval, ':');
1443     if (p != NULL)
1444         *p++ = 0;
1445     rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1446     if (rv == -2) {
1447         t->err = "PKEY_CTRL_INVALID";
1448         rv = 1;
1449     } else if (p != NULL && rv <= 0) {
1450         /* If p has an OID and lookup fails assume disabled algorithm */
1451         int nid = OBJ_sn2nid(p);
1452         if (nid == NID_undef)
1453              nid = OBJ_ln2nid(p);
1454         if ((nid != NID_undef) && EVP_get_digestbynid(nid) == NULL &&
1455             EVP_get_cipherbynid(nid) == NULL) {
1456             t->skip = 1;
1457             rv = 1;
1458         } else {
1459             t->err = "PKEY_CTRL_ERROR";
1460             rv = 1;
1461         }
1462     }
1463     OPENSSL_free(tmpval);
1464     return rv > 0;
1465 }
1466
1467 static int pkey_test_parse(struct evp_test *t,
1468                            const char *keyword, const char *value)
1469 {
1470     struct pkey_data *kdata = t->data;
1471     if (strcmp(keyword, "Input") == 0)
1472         return test_bin(value, &kdata->input, &kdata->input_len);
1473     if (strcmp(keyword, "Output") == 0)
1474         return test_bin(value, &kdata->output, &kdata->output_len);
1475     if (strcmp(keyword, "Ctrl") == 0)
1476         return pkey_test_ctrl(t, kdata->ctx, value);
1477     return 0;
1478 }
1479
1480 static int pkey_test_run(struct evp_test *t)
1481 {
1482     struct pkey_data *kdata = t->data;
1483     unsigned char *out = NULL;
1484     size_t out_len;
1485     const char *err = "KEYOP_LENGTH_ERROR";
1486     if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1487                      kdata->input_len) <= 0)
1488         goto err;
1489     out = OPENSSL_malloc(out_len);
1490     if (!out) {
1491         fprintf(stderr, "Error allocating output buffer!\n");
1492         exit(1);
1493     }
1494     err = "KEYOP_ERROR";
1495     if (kdata->keyop
1496         (kdata->ctx, out, &out_len, kdata->input, kdata->input_len) <= 0)
1497         goto err;
1498     err = "KEYOP_LENGTH_MISMATCH";
1499     if (out_len != kdata->output_len)
1500         goto err;
1501     err = "KEYOP_MISMATCH";
1502     if (check_output(t, kdata->output, out, out_len))
1503         goto err;
1504     err = NULL;
1505  err:
1506     OPENSSL_free(out);
1507     t->err = err;
1508     return 1;
1509 }
1510
1511 static int sign_test_init(struct evp_test *t, const char *name)
1512 {
1513     return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1514 }
1515
1516 static const struct evp_test_method psign_test_method = {
1517     "Sign",
1518     sign_test_init,
1519     pkey_test_cleanup,
1520     pkey_test_parse,
1521     pkey_test_run
1522 };
1523
1524 static int verify_recover_test_init(struct evp_test *t, const char *name)
1525 {
1526     return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1527                           EVP_PKEY_verify_recover);
1528 }
1529
1530 static const struct evp_test_method pverify_recover_test_method = {
1531     "VerifyRecover",
1532     verify_recover_test_init,
1533     pkey_test_cleanup,
1534     pkey_test_parse,
1535     pkey_test_run
1536 };
1537
1538 static int decrypt_test_init(struct evp_test *t, const char *name)
1539 {
1540     return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1541                           EVP_PKEY_decrypt);
1542 }
1543
1544 static const struct evp_test_method pdecrypt_test_method = {
1545     "Decrypt",
1546     decrypt_test_init,
1547     pkey_test_cleanup,
1548     pkey_test_parse,
1549     pkey_test_run
1550 };
1551
1552 static int verify_test_init(struct evp_test *t, const char *name)
1553 {
1554     return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1555 }
1556
1557 static int verify_test_run(struct evp_test *t)
1558 {
1559     struct pkey_data *kdata = t->data;
1560     if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1561                         kdata->input, kdata->input_len) <= 0)
1562         t->err = "VERIFY_ERROR";
1563     return 1;
1564 }
1565
1566 static const struct evp_test_method pverify_test_method = {
1567     "Verify",
1568     verify_test_init,
1569     pkey_test_cleanup,
1570     pkey_test_parse,
1571     verify_test_run
1572 };
1573
1574
1575 static int pderive_test_init(struct evp_test *t, const char *name)
1576 {
1577     return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1578 }
1579
1580 static int pderive_test_parse(struct evp_test *t,
1581                               const char *keyword, const char *value)
1582 {
1583     struct pkey_data *kdata = t->data;
1584
1585     if (strcmp(keyword, "PeerKey") == 0) {
1586         EVP_PKEY *peer;
1587         if (find_key(&peer, value, t->public) == 0)
1588             return 0;
1589         if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1590             return 0;
1591         return 1;
1592     }
1593     if (strcmp(keyword, "SharedSecret") == 0)
1594         return test_bin(value, &kdata->output, &kdata->output_len);
1595     if (strcmp(keyword, "Ctrl") == 0)
1596         return pkey_test_ctrl(t, kdata->ctx, value);
1597     return 0;
1598 }
1599
1600 static int pderive_test_run(struct evp_test *t)
1601 {
1602     struct pkey_data *kdata = t->data;
1603     unsigned char *out = NULL;
1604     size_t out_len;
1605     const char *err = "INTERNAL_ERROR";
1606
1607     out_len = kdata->output_len;
1608     out = OPENSSL_malloc(out_len);
1609     if (!out) {
1610         fprintf(stderr, "Error allocating output buffer!\n");
1611         exit(1);
1612     }
1613     err = "DERIVE_ERROR";
1614     if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0)
1615         goto err;
1616     err = "SHARED_SECRET_LENGTH_MISMATCH";
1617     if (out_len != kdata->output_len)
1618         goto err;
1619     err = "SHARED_SECRET_MISMATCH";
1620     if (check_output(t, kdata->output, out, out_len))
1621         goto err;
1622     err = NULL;
1623  err:
1624     OPENSSL_free(out);
1625     t->err = err;
1626     return 1;
1627 }
1628
1629 static const struct evp_test_method pderive_test_method = {
1630     "Derive",
1631     pderive_test_init,
1632     pkey_test_cleanup,
1633     pderive_test_parse,
1634     pderive_test_run
1635 };
1636
1637 /* PBE tests */
1638
1639 #define PBE_TYPE_SCRYPT 1
1640 #define PBE_TYPE_PBKDF2 2
1641 #define PBE_TYPE_PKCS12 3
1642
1643 struct pbe_data {
1644
1645     int pbe_type;
1646
1647     /* scrypt parameters */
1648     uint64_t N, r, p, maxmem;
1649
1650     /* PKCS#12 parameters */
1651     int id, iter;
1652     const EVP_MD *md;
1653
1654     /* password */
1655     unsigned char *pass;
1656     size_t pass_len;
1657
1658     /* salt */
1659     unsigned char *salt;
1660     size_t salt_len;
1661
1662     /* Expected output */
1663     unsigned char *key;
1664     size_t key_len;
1665 };
1666
1667 #ifndef OPENSSL_NO_SCRYPT
1668 static int scrypt_test_parse(struct evp_test *t,
1669                              const char *keyword, const char *value)
1670 {
1671     struct pbe_data *pdata = t->data;
1672
1673     if (strcmp(keyword, "N") == 0)
1674         return test_uint64(value, &pdata->N);
1675     if (strcmp(keyword, "p") == 0)
1676         return test_uint64(value, &pdata->p);
1677     if (strcmp(keyword, "r") == 0)
1678         return test_uint64(value, &pdata->r);
1679     if (strcmp(keyword, "maxmem") == 0)
1680         return test_uint64(value, &pdata->maxmem);
1681     return 0;
1682 }
1683 #endif
1684
1685 static int pbkdf2_test_parse(struct evp_test *t,
1686                              const char *keyword, const char *value)
1687 {
1688     struct pbe_data *pdata = t->data;
1689
1690     if (strcmp(keyword, "iter") == 0) {
1691         pdata->iter = atoi(value);
1692         if (pdata->iter <= 0)
1693             return 0;
1694         return 1;
1695     }
1696     if (strcmp(keyword, "MD") == 0) {
1697         pdata->md = EVP_get_digestbyname(value);
1698         if (pdata->md == NULL)
1699             return 0;
1700         return 1;
1701     }
1702     return 0;
1703 }
1704
1705 static int pkcs12_test_parse(struct evp_test *t,
1706                              const char *keyword, const char *value)
1707 {
1708     struct pbe_data *pdata = t->data;
1709
1710     if (strcmp(keyword, "id") == 0) {
1711         pdata->id = atoi(value);
1712         if (pdata->id <= 0)
1713             return 0;
1714         return 1;
1715     }
1716     return pbkdf2_test_parse(t, keyword, value);
1717 }
1718
1719 static int pbe_test_init(struct evp_test *t, const char *alg)
1720 {
1721     struct pbe_data *pdat;
1722     int pbe_type = 0;
1723
1724     if (strcmp(alg, "scrypt") == 0) {
1725 #ifndef OPENSSL_NO_SCRYPT
1726         pbe_type = PBE_TYPE_SCRYPT;
1727 #else
1728         t->skip = 1;
1729         return 1;
1730 #endif
1731     } else if (strcmp(alg, "pbkdf2") == 0) {
1732         pbe_type = PBE_TYPE_PBKDF2;
1733     } else if (strcmp(alg, "pkcs12") == 0) {
1734         pbe_type = PBE_TYPE_PKCS12;
1735     } else {
1736         fprintf(stderr, "Unknown pbe algorithm %s\n", alg);
1737     }
1738     pdat = OPENSSL_malloc(sizeof(*pdat));
1739     pdat->pbe_type = pbe_type;
1740     pdat->pass = NULL;
1741     pdat->salt = NULL;
1742     pdat->N = 0;
1743     pdat->r = 0;
1744     pdat->p = 0;
1745     pdat->maxmem = 0;
1746     pdat->id = 0;
1747     pdat->iter = 0;
1748     pdat->md = NULL;
1749     t->data = pdat;
1750     return 1;
1751 }
1752
1753 static void pbe_test_cleanup(struct evp_test *t)
1754 {
1755     struct pbe_data *pdat = t->data;
1756     test_free(pdat->pass);
1757     test_free(pdat->salt);
1758     test_free(pdat->key);
1759 }
1760
1761 static int pbe_test_parse(struct evp_test *t,
1762                              const char *keyword, const char *value)
1763 {
1764     struct pbe_data *pdata = t->data;
1765
1766     if (strcmp(keyword, "Password") == 0)
1767         return test_bin(value, &pdata->pass, &pdata->pass_len);
1768     if (strcmp(keyword, "Salt") == 0)
1769         return test_bin(value, &pdata->salt, &pdata->salt_len);
1770     if (strcmp(keyword, "Key") == 0)
1771         return test_bin(value, &pdata->key, &pdata->key_len);
1772     if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1773         return pbkdf2_test_parse(t, keyword, value);
1774     else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1775         return pkcs12_test_parse(t, keyword, value);
1776 #ifndef OPENSSL_NO_SCRYPT
1777     else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1778         return scrypt_test_parse(t, keyword, value);
1779 #endif
1780     return 0;
1781 }
1782
1783 static int pbe_test_run(struct evp_test *t)
1784 {
1785     struct pbe_data *pdata = t->data;
1786     const char *err = "INTERNAL_ERROR";
1787     unsigned char *key;
1788
1789     key = OPENSSL_malloc(pdata->key_len);
1790     if (!key)
1791         goto err;
1792     if (pdata->pbe_type == PBE_TYPE_PBKDF2) {
1793         err = "PBKDF2_ERROR";
1794         if (PKCS5_PBKDF2_HMAC((char *)pdata->pass, pdata->pass_len,
1795                               pdata->salt, pdata->salt_len,
1796                               pdata->iter, pdata->md,
1797                               pdata->key_len, key) == 0)
1798             goto err;
1799 #ifndef OPENSSL_NO_SCRYPT
1800     } else if (pdata->pbe_type == PBE_TYPE_SCRYPT) {
1801         err = "SCRYPT_ERROR";
1802         if (EVP_PBE_scrypt((const char *)pdata->pass, pdata->pass_len,
1803                            pdata->salt, pdata->salt_len,
1804                            pdata->N, pdata->r, pdata->p, pdata->maxmem,
1805                            key, pdata->key_len) == 0)
1806             goto err;
1807 #endif
1808     } else if (pdata->pbe_type == PBE_TYPE_PKCS12) {
1809         err = "PKCS12_ERROR";
1810         if (PKCS12_key_gen_uni(pdata->pass, pdata->pass_len,
1811                                pdata->salt, pdata->salt_len,
1812                                pdata->id, pdata->iter, pdata->key_len,
1813                                key, pdata->md) == 0)
1814             goto err;
1815     }
1816     err = "KEY_MISMATCH";
1817     if (check_output(t, pdata->key, key, pdata->key_len))
1818         goto err;
1819     err = NULL;
1820     err:
1821     OPENSSL_free(key);
1822     t->err = err;
1823     return 1;
1824 }
1825
1826 static const struct evp_test_method pbe_test_method = {
1827     "PBE",
1828     pbe_test_init,
1829     pbe_test_cleanup,
1830     pbe_test_parse,
1831     pbe_test_run
1832 };
1833
1834 /* Base64 tests */
1835
1836 typedef enum {
1837     BASE64_CANONICAL_ENCODING = 0,
1838     BASE64_VALID_ENCODING = 1,
1839     BASE64_INVALID_ENCODING = 2
1840 } base64_encoding_type;
1841
1842 struct encode_data {
1843     /* Input to encoding */
1844     unsigned char *input;
1845     size_t input_len;
1846     /* Expected output */
1847     unsigned char *output;
1848     size_t output_len;
1849     base64_encoding_type encoding;
1850 };
1851
1852 static int encode_test_init(struct evp_test *t, const char *encoding)
1853 {
1854     struct encode_data *edata = OPENSSL_zalloc(sizeof(*edata));
1855
1856     if (strcmp(encoding, "canonical") == 0) {
1857         edata->encoding = BASE64_CANONICAL_ENCODING;
1858     } else if (strcmp(encoding, "valid") == 0) {
1859         edata->encoding = BASE64_VALID_ENCODING;
1860     } else if (strcmp(encoding, "invalid") == 0) {
1861         edata->encoding = BASE64_INVALID_ENCODING;
1862         t->expected_err = OPENSSL_strdup("DECODE_ERROR");
1863         if (t->expected_err == NULL)
1864             return 0;
1865     } else {
1866         fprintf(stderr, "Bad encoding: %s. Should be one of "
1867                 "{canonical, valid, invalid}\n", encoding);
1868         return 0;
1869     }
1870     t->data = edata;
1871     return 1;
1872 }
1873
1874 static void encode_test_cleanup(struct evp_test *t)
1875 {
1876     struct encode_data *edata = t->data;
1877     test_free(edata->input);
1878     test_free(edata->output);
1879     memset(edata, 0, sizeof(*edata));
1880 }
1881
1882 static int encode_test_parse(struct evp_test *t,
1883                              const char *keyword, const char *value)
1884 {
1885     struct encode_data *edata = t->data;
1886     if (strcmp(keyword, "Input") == 0)
1887         return test_bin(value, &edata->input, &edata->input_len);
1888     if (strcmp(keyword, "Output") == 0)
1889         return test_bin(value, &edata->output, &edata->output_len);
1890     return 0;
1891 }
1892
1893 static int encode_test_run(struct evp_test *t)
1894 {
1895     struct encode_data *edata = t->data;
1896     unsigned char *encode_out = NULL, *decode_out = NULL;
1897     int output_len, chunk_len;
1898     const char *err = "INTERNAL_ERROR";
1899     EVP_ENCODE_CTX *decode_ctx = EVP_ENCODE_CTX_new();
1900
1901     if (decode_ctx == NULL)
1902         goto err;
1903
1904     if (edata->encoding == BASE64_CANONICAL_ENCODING) {
1905         EVP_ENCODE_CTX *encode_ctx = EVP_ENCODE_CTX_new();
1906         if (encode_ctx == NULL)
1907             goto err;
1908         encode_out = OPENSSL_malloc(EVP_ENCODE_LENGTH(edata->input_len));
1909         if (encode_out == NULL)
1910             goto err;
1911
1912         EVP_EncodeInit(encode_ctx);
1913         EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1914                          edata->input, edata->input_len);
1915         output_len = chunk_len;
1916
1917         EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
1918         output_len += chunk_len;
1919
1920         EVP_ENCODE_CTX_free(encode_ctx);
1921
1922         if (check_var_length_output(t, edata->output, edata->output_len,
1923                                     encode_out, output_len)) {
1924             err = "BAD_ENCODING";
1925             goto err;
1926         }
1927     }
1928
1929     decode_out = OPENSSL_malloc(EVP_DECODE_LENGTH(edata->output_len));
1930     if (decode_out == NULL)
1931         goto err;
1932
1933     EVP_DecodeInit(decode_ctx);
1934     if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, edata->output,
1935                          edata->output_len) < 0) {
1936         err = "DECODE_ERROR";
1937         goto err;
1938     }
1939     output_len = chunk_len;
1940
1941     if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
1942         err = "DECODE_ERROR";
1943         goto err;
1944     }
1945     output_len += chunk_len;
1946
1947     if (edata->encoding != BASE64_INVALID_ENCODING &&
1948         check_var_length_output(t, edata->input, edata->input_len,
1949                                 decode_out, output_len)) {
1950         err = "BAD_DECODING";
1951         goto err;
1952     }
1953
1954     err = NULL;
1955  err:
1956     t->err = err;
1957     OPENSSL_free(encode_out);
1958     OPENSSL_free(decode_out);
1959     EVP_ENCODE_CTX_free(decode_ctx);
1960     return 1;
1961 }
1962
1963 static const struct evp_test_method encode_test_method = {
1964     "Encoding",
1965     encode_test_init,
1966     encode_test_cleanup,
1967     encode_test_parse,
1968     encode_test_run,
1969 };
1970
1971 /* KDF operations */
1972
1973 struct kdf_data {
1974     /* Context for this operation */
1975     EVP_PKEY_CTX *ctx;
1976     /* Expected output */
1977     unsigned char *output;
1978     size_t output_len;
1979 };
1980
1981 /*
1982  * Perform public key operation setup: lookup key, allocated ctx and call
1983  * the appropriate initialisation function
1984  */
1985 static int kdf_test_init(struct evp_test *t, const char *name)
1986 {
1987     struct kdf_data *kdata;
1988
1989     kdata = OPENSSL_malloc(sizeof(*kdata));
1990     if (kdata == NULL)
1991         return 0;
1992     kdata->ctx = NULL;
1993     kdata->output = NULL;
1994     t->data = kdata;
1995     kdata->ctx = EVP_PKEY_CTX_new_id(OBJ_sn2nid(name), NULL);
1996     if (kdata->ctx == NULL)
1997         return 0;
1998     if (EVP_PKEY_derive_init(kdata->ctx) <= 0)
1999         return 0;
2000     return 1;
2001 }
2002
2003 static void kdf_test_cleanup(struct evp_test *t)
2004 {
2005     struct kdf_data *kdata = t->data;
2006     OPENSSL_free(kdata->output);
2007     EVP_PKEY_CTX_free(kdata->ctx);
2008 }
2009
2010 static int kdf_test_parse(struct evp_test *t,
2011                           const char *keyword, const char *value)
2012 {
2013     struct kdf_data *kdata = t->data;
2014     if (strcmp(keyword, "Output") == 0)
2015         return test_bin(value, &kdata->output, &kdata->output_len);
2016     if (strncmp(keyword, "Ctrl", 4) == 0)
2017         return pkey_test_ctrl(t, kdata->ctx, value);
2018     return 0;
2019 }
2020
2021 static int kdf_test_run(struct evp_test *t)
2022 {
2023     struct kdf_data *kdata = t->data;
2024     unsigned char *out = NULL;
2025     size_t out_len = kdata->output_len;
2026     const char *err = "INTERNAL_ERROR";
2027     out = OPENSSL_malloc(out_len);
2028     if (!out) {
2029         fprintf(stderr, "Error allocating output buffer!\n");
2030         exit(1);
2031     }
2032     err = "KDF_DERIVE_ERROR";
2033     if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0)
2034         goto err;
2035     err = "KDF_LENGTH_MISMATCH";
2036     if (out_len != kdata->output_len)
2037         goto err;
2038     err = "KDF_MISMATCH";
2039     if (check_output(t, kdata->output, out, out_len))
2040         goto err;
2041     err = NULL;
2042  err:
2043     OPENSSL_free(out);
2044     t->err = err;
2045     return 1;
2046 }
2047
2048 static const struct evp_test_method kdf_test_method = {
2049     "KDF",
2050     kdf_test_init,
2051     kdf_test_cleanup,
2052     kdf_test_parse,
2053     kdf_test_run
2054 };
2055
2056 struct keypair_test_data {
2057     EVP_PKEY *privk;
2058     EVP_PKEY *pubk;
2059 };
2060
2061 static int keypair_test_init(struct evp_test *t, const char *pair)
2062 {
2063     int rv = 0;
2064     EVP_PKEY *pk = NULL, *pubk = NULL;
2065     char *pub, *priv = NULL;
2066     const char *err = "INTERNAL_ERROR";
2067     struct keypair_test_data *data;
2068
2069     priv = OPENSSL_strdup(pair);
2070     if (priv == NULL)
2071         return 0;
2072     pub = strchr(priv, ':');
2073     if ( pub == NULL ) {
2074         fprintf(stderr, "Wrong syntax \"%s\"\n", pair);
2075         goto end;
2076     }
2077     *pub++ = 0; /* split priv and pub strings */
2078
2079     if (find_key(&pk, priv, t->private) == 0) {
2080         fprintf(stderr, "Cannot find private key: %s\n", priv);
2081         err = "MISSING_PRIVATE_KEY";
2082         goto end;
2083     }
2084     if (find_key(&pubk, pub, t->public) == 0) {
2085         fprintf(stderr, "Cannot find public key: %s\n", pub);
2086         err = "MISSING_PUBLIC_KEY";
2087         goto end;
2088     }
2089
2090     if (pk == NULL && pubk == NULL) {
2091         /* Both keys are listed but unsupported: skip this test */
2092         t->skip = 1;
2093         rv = 1;
2094         goto end;
2095     }
2096
2097     data = OPENSSL_malloc(sizeof(*data));
2098     if (data == NULL )
2099         goto end;
2100
2101     data->privk = pk;
2102     data->pubk = pubk;
2103     t->data = data;
2104
2105     rv = 1;
2106     err = NULL;
2107
2108 end:
2109     if (priv)
2110         OPENSSL_free(priv);
2111     t->err = err;
2112     return rv;
2113 }
2114
2115 static void keypair_test_cleanup(struct evp_test *t)
2116 {
2117     struct keypair_test_data *data = t->data;
2118     t->data = NULL;
2119     if (data)
2120         test_free(data);
2121     return;
2122 }
2123
2124 /* For test that do not accept any custom keyword:
2125  *      return 0 if called
2126  */
2127 static int void_test_parse(struct evp_test *t, const char *keyword, const char *value)
2128 {
2129     return 0;
2130 }
2131
2132 static int keypair_test_run(struct evp_test *t)
2133 {
2134     int rv = 0;
2135     const struct keypair_test_data *pair = t->data;
2136     const char *err = "INTERNAL_ERROR";
2137
2138     if (pair == NULL)
2139         goto end;
2140
2141     if (pair->privk == NULL || pair->pubk == NULL) {
2142         /* this can only happen if only one of the keys is not set
2143          * which means that one of them was unsupported while the
2144          * other isn't: hence a key type mismatch.
2145          */
2146         err = "KEYPAIR_TYPE_MISMATCH";
2147         rv = 1;
2148         goto end;
2149     }
2150
2151     if ((rv = EVP_PKEY_cmp(pair->privk, pair->pubk)) != 1 ) {
2152         if ( 0 == rv ) {
2153             err = "KEYPAIR_MISMATCH";
2154         } else if ( -1 == rv ) {
2155             err = "KEYPAIR_TYPE_MISMATCH";
2156         } else if ( -2 == rv ) {
2157             err = "UNSUPPORTED_KEY_COMPARISON";
2158         } else {
2159             fprintf(stderr, "Unexpected error in key comparison\n");
2160             rv = 0;
2161             goto end;
2162         }
2163         rv = 1;
2164         goto end;
2165     }
2166
2167     rv = 1;
2168     err = NULL;
2169
2170 end:
2171     t->err = err;
2172     return rv;
2173 }
2174
2175 static const struct evp_test_method keypair_test_method = {
2176     "PrivPubKeyPair",
2177     keypair_test_init,
2178     keypair_test_cleanup,
2179     void_test_parse,
2180     keypair_test_run
2181 };
2182