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