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