Address some feedback
[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              */
421             t->nskip++;
422         } else {
423             /* run the test */
424             if (t->err == NULL && t->meth->run_test(t) != 1) {
425                 TEST_info("Line %d error %s", t->start_line, t->meth->name);
426                 return 0;
427             }
428             if (!check_test_error(t)) {
429                 test_openssl_errors();
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         t->err = "TEST_FAILURE";
1270         goto err;
1271     }
1272     if (!EVP_DigestSignFinal(mctx, mac, &mac_len)
1273             || !TEST_mem_eq(mdata->output, mdata->output_len, mac, mac_len)) {
1274         t->err = "TEST_MAC_ERR";
1275         goto err;
1276     }
1277
1278     t->err = NULL;
1279  err:
1280     EVP_MD_CTX_free(mctx);
1281     OPENSSL_free(mac);
1282     EVP_PKEY_CTX_free(genctx);
1283     EVP_PKEY_free(key);
1284     return 1;
1285 }
1286
1287 static const EVP_TEST_METHOD mac_test_method = {
1288     "MAC",
1289     mac_test_init,
1290     mac_test_cleanup,
1291     mac_test_parse,
1292     mac_test_run
1293 };
1294
1295 /*
1296  * Public key operations. These are all very similar and can share
1297  * a lot of common code.
1298  */
1299
1300 typedef struct pkey_data_st {
1301     /* Context for this operation */
1302     EVP_PKEY_CTX *ctx;
1303     /* Key operation to perform */
1304     int (*keyop) (EVP_PKEY_CTX *ctx,
1305                   unsigned char *sig, size_t *siglen,
1306                   const unsigned char *tbs, size_t tbslen);
1307     /* Input to MAC */
1308     unsigned char *input;
1309     size_t input_len;
1310     /* Expected output */
1311     unsigned char *output;
1312     size_t output_len;
1313 } PKEY_DATA;
1314
1315 /*
1316  * Perform public key operation setup: lookup key, allocated ctx and call
1317  * the appropriate initialisation function
1318  */
1319 static int pkey_test_init(EVP_TEST *t, const char *name,
1320                           int use_public,
1321                           int (*keyopinit) (EVP_PKEY_CTX *ctx),
1322                           int (*keyop) (EVP_PKEY_CTX *ctx,
1323                                         unsigned char *sig, size_t *siglen,
1324                                         const unsigned char *tbs,
1325                                         size_t tbslen)
1326     )
1327 {
1328     PKEY_DATA *kdata;
1329     EVP_PKEY *pkey = NULL;
1330     int rv = 0;
1331
1332     if (use_public)
1333         rv = find_key(&pkey, name, public_keys);
1334     if (rv == 0)
1335         rv = find_key(&pkey, name, private_keys);
1336     if (rv == 0 || pkey == NULL) {
1337         t->skip = 1;
1338         return 1;
1339     }
1340
1341     if (!TEST_ptr(kdata = OPENSSL_malloc(sizeof(*kdata)))) {
1342         EVP_PKEY_free(pkey);
1343         return 0;
1344     }
1345     kdata->ctx = NULL;
1346     kdata->input = NULL;
1347     kdata->output = NULL;
1348     kdata->keyop = keyop;
1349     t->data = kdata;
1350     if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL)))
1351         return 0;
1352     if (keyopinit(kdata->ctx) <= 0)
1353         t->err = "KEYOP_INIT_ERROR";
1354     return 1;
1355 }
1356
1357 static void pkey_test_cleanup(EVP_TEST *t)
1358 {
1359     PKEY_DATA *kdata = t->data;
1360
1361     OPENSSL_free(kdata->input);
1362     OPENSSL_free(kdata->output);
1363     EVP_PKEY_CTX_free(kdata->ctx);
1364 }
1365
1366 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1367                           const char *value)
1368 {
1369     int rv;
1370     char *p, *tmpval;
1371
1372     if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1373         return 0;
1374     p = strchr(tmpval, ':');
1375     if (p != NULL)
1376         *p++ = 0;
1377     rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1378     if (rv == -2) {
1379         t->err = "PKEY_CTRL_INVALID";
1380         rv = 1;
1381     } else if (p != NULL && rv <= 0) {
1382         /* If p has an OID and lookup fails assume disabled algorithm */
1383         int nid = OBJ_sn2nid(p);
1384
1385         if (nid == NID_undef)
1386              nid = OBJ_ln2nid(p);
1387         if ((nid != NID_undef) && EVP_get_digestbynid(nid) == NULL &&
1388             EVP_get_cipherbynid(nid) == NULL) {
1389             t->skip = 1;
1390             rv = 1;
1391         } else {
1392             t->err = "PKEY_CTRL_ERROR";
1393             rv = 1;
1394         }
1395     }
1396     OPENSSL_free(tmpval);
1397     return rv > 0;
1398 }
1399
1400 static int pkey_test_parse(EVP_TEST *t,
1401                            const char *keyword, const char *value)
1402 {
1403     PKEY_DATA *kdata = t->data;
1404     if (strcmp(keyword, "Input") == 0)
1405         return test_bin(value, &kdata->input, &kdata->input_len);
1406     if (strcmp(keyword, "Output") == 0)
1407         return test_bin(value, &kdata->output, &kdata->output_len);
1408     if (strcmp(keyword, "Ctrl") == 0)
1409         return pkey_test_ctrl(t, kdata->ctx, value);
1410     return 0;
1411 }
1412
1413 static int pkey_test_run(EVP_TEST *t)
1414 {
1415     PKEY_DATA *kdata = t->data;
1416     unsigned char *out = NULL;
1417     size_t out_len;
1418
1419     if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1420                      kdata->input_len) <= 0
1421             || !TEST_ptr(out = OPENSSL_malloc(out_len))) {
1422         t->err = "KEYOP_LENGTH_ERROR";
1423         goto err;
1424     }
1425     if (kdata->keyop(kdata->ctx, out,
1426                      &out_len, kdata->input, kdata->input_len) <= 0) {
1427         t->err = "KEYOP_ERROR";
1428         goto err;
1429     }
1430     if (!TEST_mem_eq(kdata->output, kdata->output_len, out, out_len)) {
1431         t->err = "KEYOP_MISMATCH";
1432         goto err;
1433     }
1434     t->err = NULL;
1435  err:
1436     OPENSSL_free(out);
1437     return 1;
1438 }
1439
1440 static int sign_test_init(EVP_TEST *t, const char *name)
1441 {
1442     return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1443 }
1444
1445 static const EVP_TEST_METHOD psign_test_method = {
1446     "Sign",
1447     sign_test_init,
1448     pkey_test_cleanup,
1449     pkey_test_parse,
1450     pkey_test_run
1451 };
1452
1453 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1454 {
1455     return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1456                           EVP_PKEY_verify_recover);
1457 }
1458
1459 static const EVP_TEST_METHOD pverify_recover_test_method = {
1460     "VerifyRecover",
1461     verify_recover_test_init,
1462     pkey_test_cleanup,
1463     pkey_test_parse,
1464     pkey_test_run
1465 };
1466
1467 static int decrypt_test_init(EVP_TEST *t, const char *name)
1468 {
1469     return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1470                           EVP_PKEY_decrypt);
1471 }
1472
1473 static const EVP_TEST_METHOD pdecrypt_test_method = {
1474     "Decrypt",
1475     decrypt_test_init,
1476     pkey_test_cleanup,
1477     pkey_test_parse,
1478     pkey_test_run
1479 };
1480
1481 static int verify_test_init(EVP_TEST *t, const char *name)
1482 {
1483     return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1484 }
1485
1486 static int verify_test_run(EVP_TEST *t)
1487 {
1488     PKEY_DATA *kdata = t->data;
1489
1490     if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1491                         kdata->input, kdata->input_len) <= 0)
1492         t->err = "VERIFY_ERROR";
1493     return 1;
1494 }
1495
1496 static const EVP_TEST_METHOD pverify_test_method = {
1497     "Verify",
1498     verify_test_init,
1499     pkey_test_cleanup,
1500     pkey_test_parse,
1501     verify_test_run
1502 };
1503
1504
1505 static int pderive_test_init(EVP_TEST *t, const char *name)
1506 {
1507     return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1508 }
1509
1510 static int pderive_test_parse(EVP_TEST *t,
1511                               const char *keyword, const char *value)
1512 {
1513     PKEY_DATA *kdata = t->data;
1514
1515     if (strcmp(keyword, "PeerKey") == 0) {
1516         EVP_PKEY *peer;
1517         if (find_key(&peer, value, public_keys) == 0)
1518             return 0;
1519         if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1520             return 0;
1521         return 1;
1522     }
1523     if (strcmp(keyword, "SharedSecret") == 0)
1524         return test_bin(value, &kdata->output, &kdata->output_len);
1525     if (strcmp(keyword, "Ctrl") == 0)
1526         return pkey_test_ctrl(t, kdata->ctx, value);
1527     return 0;
1528 }
1529
1530 static int pderive_test_run(EVP_TEST *t)
1531 {
1532     PKEY_DATA *kdata = t->data;
1533     unsigned char *out = NULL;
1534     size_t out_len;
1535
1536     out_len = kdata->output_len;
1537     if (!TEST_ptr(out = OPENSSL_malloc(out_len))) {
1538         t->err = "DERIVE_ERROR";
1539         goto err;
1540     }
1541     if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0) {
1542         t->err = "DERIVE_ERROR";
1543         goto err;
1544     }
1545     if (!TEST_mem_eq(kdata->output, kdata->output_len, out, out_len)) {
1546         t->err = "SHARED_SECRET_MISMATCH";
1547         goto err;
1548     }
1549
1550     t->err = NULL;
1551  err:
1552     OPENSSL_free(out);
1553     return 1;
1554 }
1555
1556 static const EVP_TEST_METHOD pderive_test_method = {
1557     "Derive",
1558     pderive_test_init,
1559     pkey_test_cleanup,
1560     pderive_test_parse,
1561     pderive_test_run
1562 };
1563
1564 /* PBE tests */
1565
1566 #define PBE_TYPE_SCRYPT 1
1567 #define PBE_TYPE_PBKDF2 2
1568 #define PBE_TYPE_PKCS12 3
1569
1570 typedef struct pbe_data_st {
1571     int pbe_type;
1572         /* scrypt parameters */
1573     uint64_t N, r, p, maxmem;
1574         /* PKCS#12 parameters */
1575     int id, iter;
1576     const EVP_MD *md;
1577         /* password */
1578     unsigned char *pass;
1579     size_t pass_len;
1580         /* salt */
1581     unsigned char *salt;
1582     size_t salt_len;
1583         /* Expected output */
1584     unsigned char *key;
1585     size_t key_len;
1586 } PBE_DATA;
1587
1588 #ifndef OPENSSL_NO_SCRYPT
1589 static int scrypt_test_parse(EVP_TEST *t,
1590                              const char *keyword, const char *value)
1591 {
1592     PBE_DATA *pdata = t->data;
1593
1594     if (strcmp(keyword, "N") == 0)
1595         return test_uint64(value, &pdata->N);
1596     if (strcmp(keyword, "p") == 0)
1597         return test_uint64(value, &pdata->p);
1598     if (strcmp(keyword, "r") == 0)
1599         return test_uint64(value, &pdata->r);
1600     if (strcmp(keyword, "maxmem") == 0)
1601         return test_uint64(value, &pdata->maxmem);
1602     return 0;
1603 }
1604 #endif
1605
1606 static int pbkdf2_test_parse(EVP_TEST *t,
1607                              const char *keyword, const char *value)
1608 {
1609     PBE_DATA *pdata = t->data;
1610
1611     if (strcmp(keyword, "iter") == 0) {
1612         pdata->iter = atoi(value);
1613         if (pdata->iter <= 0)
1614             return 0;
1615         return 1;
1616     }
1617     if (strcmp(keyword, "MD") == 0) {
1618         pdata->md = EVP_get_digestbyname(value);
1619         if (pdata->md == NULL)
1620             return 0;
1621         return 1;
1622     }
1623     return 0;
1624 }
1625
1626 static int pkcs12_test_parse(EVP_TEST *t,
1627                              const char *keyword, const char *value)
1628 {
1629     PBE_DATA *pdata = t->data;
1630
1631     if (strcmp(keyword, "id") == 0) {
1632         pdata->id = atoi(value);
1633         if (pdata->id <= 0)
1634             return 0;
1635         return 1;
1636     }
1637     return pbkdf2_test_parse(t, keyword, value);
1638 }
1639
1640 static int pbe_test_init(EVP_TEST *t, const char *alg)
1641 {
1642     PBE_DATA *pdat;
1643     int pbe_type = 0;
1644
1645     if (strcmp(alg, "scrypt") == 0) {
1646 #ifndef OPENSSL_NO_SCRYPT
1647         pbe_type = PBE_TYPE_SCRYPT;
1648 #else
1649         t->skip = 1;
1650         return 1;
1651 #endif
1652     } else if (strcmp(alg, "pbkdf2") == 0) {
1653         pbe_type = PBE_TYPE_PBKDF2;
1654     } else if (strcmp(alg, "pkcs12") == 0) {
1655         pbe_type = PBE_TYPE_PKCS12;
1656     } else {
1657         TEST_error("Unknown pbe algorithm %s", alg);
1658     }
1659     pdat = OPENSSL_malloc(sizeof(*pdat));
1660     pdat->pbe_type = pbe_type;
1661     pdat->pass = NULL;
1662     pdat->salt = NULL;
1663     pdat->N = 0;
1664     pdat->r = 0;
1665     pdat->p = 0;
1666     pdat->maxmem = 0;
1667     pdat->id = 0;
1668     pdat->iter = 0;
1669     pdat->md = NULL;
1670     t->data = pdat;
1671     return 1;
1672 }
1673
1674 static void pbe_test_cleanup(EVP_TEST *t)
1675 {
1676     PBE_DATA *pdat = t->data;
1677
1678     OPENSSL_free(pdat->pass);
1679     OPENSSL_free(pdat->salt);
1680     OPENSSL_free(pdat->key);
1681 }
1682
1683 static int pbe_test_parse(EVP_TEST *t,
1684                           const char *keyword, const char *value)
1685 {
1686     PBE_DATA *pdata = t->data;
1687
1688     if (strcmp(keyword, "Password") == 0)
1689         return test_bin(value, &pdata->pass, &pdata->pass_len);
1690     if (strcmp(keyword, "Salt") == 0)
1691         return test_bin(value, &pdata->salt, &pdata->salt_len);
1692     if (strcmp(keyword, "Key") == 0)
1693         return test_bin(value, &pdata->key, &pdata->key_len);
1694     if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1695         return pbkdf2_test_parse(t, keyword, value);
1696     else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1697         return pkcs12_test_parse(t, keyword, value);
1698 #ifndef OPENSSL_NO_SCRYPT
1699     else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1700         return scrypt_test_parse(t, keyword, value);
1701 #endif
1702     return 0;
1703 }
1704
1705 static int pbe_test_run(EVP_TEST *t)
1706 {
1707     PBE_DATA *pdata = t->data;
1708     unsigned char *key;
1709
1710     if (!TEST_ptr(key = OPENSSL_malloc(pdata->key_len))) {
1711         t->err = "INTERNAL_ERROR";
1712         goto err;
1713     }
1714     if (pdata->pbe_type == PBE_TYPE_PBKDF2) {
1715         if (PKCS5_PBKDF2_HMAC((char *)pdata->pass, pdata->pass_len,
1716                               pdata->salt, pdata->salt_len,
1717                               pdata->iter, pdata->md,
1718                               pdata->key_len, key) == 0) {
1719             t->err = "PBKDF2_ERROR";
1720             goto err;
1721         }
1722 #ifndef OPENSSL_NO_SCRYPT
1723     } else if (pdata->pbe_type == PBE_TYPE_SCRYPT) {
1724         if (EVP_PBE_scrypt((const char *)pdata->pass, pdata->pass_len,
1725                            pdata->salt, pdata->salt_len,
1726                            pdata->N, pdata->r, pdata->p, pdata->maxmem,
1727                            key, pdata->key_len) == 0) {
1728             t->err = "SCRYPT_ERROR";
1729             goto err;
1730         }
1731 #endif
1732     } else if (pdata->pbe_type == PBE_TYPE_PKCS12) {
1733         if (PKCS12_key_gen_uni(pdata->pass, pdata->pass_len,
1734                                pdata->salt, pdata->salt_len,
1735                                pdata->id, pdata->iter, pdata->key_len,
1736                                key, pdata->md) == 0) {
1737             t->err = "PKCS12_ERROR";
1738             goto err;
1739         }
1740     }
1741     if (!TEST_mem_eq(pdata->key, pdata->key_len, key, pdata->key_len)) {
1742         t->err = "KEY_MISMATCH";
1743         goto err;
1744     }
1745     t->err = NULL;
1746 err:
1747     OPENSSL_free(key);
1748     return 1;
1749 }
1750
1751 static const EVP_TEST_METHOD pbe_test_method = {
1752     "PBE",
1753     pbe_test_init,
1754     pbe_test_cleanup,
1755     pbe_test_parse,
1756     pbe_test_run
1757 };
1758
1759 /* Base64 tests */
1760
1761 typedef enum {
1762     BASE64_CANONICAL_ENCODING = 0,
1763     BASE64_VALID_ENCODING = 1,
1764     BASE64_INVALID_ENCODING = 2
1765 } base64_encoding_type;
1766
1767 typedef struct encode_data_st {
1768     /* Input to encoding */
1769     unsigned char *input;
1770     size_t input_len;
1771     /* Expected output */
1772     unsigned char *output;
1773     size_t output_len;
1774     base64_encoding_type encoding;
1775 } ENCODE_DATA;
1776
1777 static int encode_test_init(EVP_TEST *t, const char *encoding)
1778 {
1779     ENCODE_DATA *edata = OPENSSL_zalloc(sizeof(*edata));
1780
1781     if (strcmp(encoding, "canonical") == 0) {
1782         edata->encoding = BASE64_CANONICAL_ENCODING;
1783     } else if (strcmp(encoding, "valid") == 0) {
1784         edata->encoding = BASE64_VALID_ENCODING;
1785     } else if (strcmp(encoding, "invalid") == 0) {
1786         edata->encoding = BASE64_INVALID_ENCODING;
1787         t->expected_err = OPENSSL_strdup("DECODE_ERROR");
1788         if (t->expected_err == NULL)
1789             return 0;
1790     } else {
1791         TEST_info("Bad encoding: %s. Should be one of "
1792                   "{canonical, valid, invalid}", encoding);
1793         return 0;
1794     }
1795     t->data = edata;
1796     return 1;
1797 }
1798
1799 static void encode_test_cleanup(EVP_TEST *t)
1800 {
1801     ENCODE_DATA *edata = t->data;
1802
1803     OPENSSL_free(edata->input);
1804     OPENSSL_free(edata->output);
1805     memset(edata, 0, sizeof(*edata));
1806 }
1807
1808 static int encode_test_parse(EVP_TEST *t,
1809                              const char *keyword, const char *value)
1810 {
1811     ENCODE_DATA *edata = t->data;
1812     if (strcmp(keyword, "Input") == 0)
1813         return test_bin(value, &edata->input, &edata->input_len);
1814     if (strcmp(keyword, "Output") == 0)
1815         return test_bin(value, &edata->output, &edata->output_len);
1816     return 0;
1817 }
1818
1819 static int encode_test_run(EVP_TEST *t)
1820 {
1821     ENCODE_DATA *edata = t->data;
1822     unsigned char *encode_out = NULL, *decode_out = NULL;
1823     int output_len, chunk_len;
1824     EVP_ENCODE_CTX *decode_ctx;
1825
1826     if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
1827         t->err = "INTERNAL_ERROR";
1828         goto err;
1829     }
1830
1831     if (edata->encoding == BASE64_CANONICAL_ENCODING) {
1832         EVP_ENCODE_CTX *encode_ctx;
1833
1834         if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
1835                 || !TEST_ptr(encode_out =
1836                         OPENSSL_malloc(EVP_ENCODE_LENGTH(edata->input_len))))
1837             goto err;
1838
1839         EVP_EncodeInit(encode_ctx);
1840         EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1841                          edata->input, edata->input_len);
1842         output_len = chunk_len;
1843
1844         EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
1845         output_len += chunk_len;
1846
1847         EVP_ENCODE_CTX_free(encode_ctx);
1848
1849         if (!TEST_mem_eq(edata->output, edata->output_len,
1850                          encode_out, output_len)) {
1851             t->err = "BAD_ENCODING";
1852             goto err;
1853         }
1854     }
1855
1856     if (!TEST_ptr(decode_out =
1857                 OPENSSL_malloc(EVP_DECODE_LENGTH(edata->output_len))))
1858         goto err;
1859
1860     EVP_DecodeInit(decode_ctx);
1861     if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, edata->output,
1862                          edata->output_len) < 0) {
1863         t->err = "DECODE_ERROR";
1864         goto err;
1865     }
1866     output_len = chunk_len;
1867
1868     if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
1869         t->err = "DECODE_ERROR";
1870         goto err;
1871     }
1872     output_len += chunk_len;
1873
1874     if (edata->encoding != BASE64_INVALID_ENCODING
1875             && !TEST_mem_eq(edata->input, edata->input_len,
1876                             decode_out, output_len)) {
1877         t->err = "BAD_DECODING";
1878         goto err;
1879     }
1880
1881     t->err = NULL;
1882  err:
1883     OPENSSL_free(encode_out);
1884     OPENSSL_free(decode_out);
1885     EVP_ENCODE_CTX_free(decode_ctx);
1886     return 1;
1887 }
1888
1889 static const EVP_TEST_METHOD encode_test_method = {
1890     "Encoding",
1891     encode_test_init,
1892     encode_test_cleanup,
1893     encode_test_parse,
1894     encode_test_run,
1895 };
1896
1897 /* KDF operations */
1898
1899 typedef struct kdf_data_st {
1900     /* Context for this operation */
1901     EVP_PKEY_CTX *ctx;
1902     /* Expected output */
1903     unsigned char *output;
1904     size_t output_len;
1905 } KDF_DATA;
1906
1907 /*
1908  * Perform public key operation setup: lookup key, allocated ctx and call
1909  * the appropriate initialisation function
1910  */
1911 static int kdf_test_init(EVP_TEST *t, const char *name)
1912 {
1913     KDF_DATA *kdata;
1914
1915     kdata = OPENSSL_malloc(sizeof(*kdata));
1916     if (kdata == NULL)
1917         return 0;
1918     kdata->ctx = NULL;
1919     kdata->output = NULL;
1920     t->data = kdata;
1921     kdata->ctx = EVP_PKEY_CTX_new_id(OBJ_sn2nid(name), NULL);
1922     if (kdata->ctx == NULL)
1923         return 0;
1924     if (EVP_PKEY_derive_init(kdata->ctx) <= 0)
1925         return 0;
1926     return 1;
1927 }
1928
1929 static void kdf_test_cleanup(EVP_TEST *t)
1930 {
1931     KDF_DATA *kdata = t->data;
1932     OPENSSL_free(kdata->output);
1933     EVP_PKEY_CTX_free(kdata->ctx);
1934 }
1935
1936 static int kdf_test_parse(EVP_TEST *t,
1937                           const char *keyword, const char *value)
1938 {
1939     KDF_DATA *kdata = t->data;
1940
1941     if (strcmp(keyword, "Output") == 0)
1942         return test_bin(value, &kdata->output, &kdata->output_len);
1943     if (strncmp(keyword, "Ctrl", 4) == 0)
1944         return pkey_test_ctrl(t, kdata->ctx, value);
1945     return 0;
1946 }
1947
1948 static int kdf_test_run(EVP_TEST *t)
1949 {
1950     KDF_DATA *kdata = t->data;
1951     unsigned char *out = NULL;
1952     size_t out_len = kdata->output_len;
1953
1954     if (!TEST_ptr(out = OPENSSL_malloc(out_len))) {
1955         t->err = "INTERNAL_ERROR";
1956         goto err;
1957     }
1958     if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0) {
1959         t->err = "KDF_DERIVE_ERROR";
1960         goto err;
1961     }
1962     if (!TEST_mem_eq(kdata->output, kdata->output_len, out, out_len)) {
1963         t->err = "KDF_MISMATCH";
1964         goto err;
1965     }
1966     t->err = NULL;
1967
1968  err:
1969     OPENSSL_free(out);
1970     return 1;
1971 }
1972
1973 static const EVP_TEST_METHOD kdf_test_method = {
1974     "KDF",
1975     kdf_test_init,
1976     kdf_test_cleanup,
1977     kdf_test_parse,
1978     kdf_test_run
1979 };
1980
1981 typedef struct keypair_test_data_st {
1982     EVP_PKEY *privk;
1983     EVP_PKEY *pubk;
1984 } KEYPAIR_TEST_DATA;
1985
1986 static int keypair_test_init(EVP_TEST *t, const char *pair)
1987 {
1988     int rv = 0;
1989     EVP_PKEY *pk = NULL, *pubk = NULL;
1990     char *pub, *priv = NULL;
1991     KEYPAIR_TEST_DATA *data;
1992
1993     if (!TEST_ptr(priv = OPENSSL_strdup(pair))
1994             || !TEST_ptr(pub = strchr(priv, ':'))) {
1995         t->err = "PARSING_ERROR";
1996         goto end;
1997     }
1998     *pub++ = 0; /* split priv and pub strings */
1999
2000     if (!TEST_true(find_key(&pk, priv, private_keys))) {
2001         TEST_info("Cannot find private key: %s", priv);
2002         t->err = "MISSING_PRIVATE_KEY";
2003         goto end;
2004     }
2005     if (!TEST_true(find_key(&pubk, pub, public_keys))) {
2006         TEST_info("Cannot find public key: %s", pub);
2007         t->err = "MISSING_PUBLIC_KEY";
2008         goto end;
2009     }
2010
2011     if (pk == NULL && pubk == NULL) {
2012         /* Both keys are listed but unsupported: skip this test */
2013         t->skip = 1;
2014         rv = 1;
2015         goto end;
2016     }
2017
2018     if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2019         goto end;
2020
2021     data->privk = pk;
2022     data->pubk = pubk;
2023     t->data = data;
2024     rv = 1;
2025     t->err = NULL;
2026
2027 end:
2028     OPENSSL_free(priv);
2029     return rv;
2030 }
2031
2032 static void keypair_test_cleanup(EVP_TEST *t)
2033 {
2034     OPENSSL_free(t->data);
2035     t->data = NULL;
2036 }
2037
2038 /* For test that do not accept any custom keyword:
2039  *      return 0 if called
2040  */
2041 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
2042 {
2043     return 0;
2044 }
2045
2046 static int keypair_test_run(EVP_TEST *t)
2047 {
2048     int rv = 0;
2049     const KEYPAIR_TEST_DATA *pair = t->data;
2050
2051     if (pair->privk == NULL || pair->pubk == NULL) {
2052         /*
2053          * this can only happen if only one of the keys is not set
2054          * which means that one of them was unsupported while the
2055          * other isn't: hence a key type mismatch.
2056          */
2057         t->err = "KEYPAIR_TYPE_MISMATCH";
2058         rv = 1;
2059         goto end;
2060     }
2061
2062     if ((rv = EVP_PKEY_cmp(pair->privk, pair->pubk)) != 1 ) {
2063         if ( 0 == rv ) {
2064             t->err = "KEYPAIR_MISMATCH";
2065         } else if ( -1 == rv ) {
2066             t->err = "KEYPAIR_TYPE_MISMATCH";
2067         } else if ( -2 == rv ) {
2068             t->err = "UNSUPPORTED_KEY_COMPARISON";
2069         } else {
2070             TEST_error("Unexpected error in key comparison");
2071             rv = 0;
2072             goto end;
2073         }
2074         rv = 1;
2075         goto end;
2076     }
2077
2078     rv = 1;
2079     t->err = NULL;
2080
2081 end:
2082     return rv;
2083 }
2084
2085 static const EVP_TEST_METHOD keypair_test_method = {
2086     "PrivPubKeyPair",
2087     keypair_test_init,
2088     keypair_test_cleanup,
2089     void_test_parse,
2090     keypair_test_run
2091 };
2092
2093 static int do_test_file(const char *testfile)
2094 {
2095     BIO *in;
2096     char buf[10240];
2097     EVP_TEST t;
2098
2099     if (!TEST_ptr(in = BIO_new_file(testfile, "rb")))
2100         return 0;
2101     memset(&t, 0, sizeof(t));
2102     t.start_line = -1;
2103     t.in = in;
2104     t.err = NULL;
2105     while (BIO_gets(in, buf, sizeof(buf))) {
2106         t.line++;
2107         if (!TEST_true(parse_test_line(&t, buf)))
2108             return 0;
2109     }
2110     /* Run any final test we have */
2111     if (!run_and_get_next(&t, NULL))
2112         return 0;
2113
2114     TEST_info("Completed %d tests with %d errors and %d skipped",
2115               t.ntests, t.errors, t.nskip);
2116     free_key_list(public_keys);
2117     free_key_list(private_keys);
2118     BIO_free(t.key);
2119     BIO_free(in);
2120     return t.errors == 0;
2121 }
2122
2123 static char * const *testfiles;
2124
2125 static int run_file_tests(int i)
2126 {
2127     return do_test_file(testfiles[i]);
2128 }
2129
2130 int test_main(int argc, char *argv[])
2131 {
2132     if (argc < 2) {
2133         TEST_error("Usage: %s file...", argv[0]);
2134         return 0;
2135     }
2136     testfiles = &argv[1];
2137
2138     ADD_ALL_TESTS(run_file_tests, argc - 1);
2139
2140     return run_tests(argv[0]);
2141 }