]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/evp_test.c
Fix CID 1434549: Unchecked return value in test/evp_test.c
[thirdparty/openssl.git] / test / evp_test.c
1 /*
2 * Copyright 2015-2019 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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 #include "evp_test.h"
23
24 #define AAD_NUM 4
25
26 typedef struct evp_test_method_st EVP_TEST_METHOD;
27
28 /*
29 * Structure holding test information
30 */
31 typedef struct evp_test_st {
32 STANZA s; /* Common test stanza */
33 char *name;
34 int skip; /* Current test should be skipped */
35 const EVP_TEST_METHOD *meth; /* method for this test */
36 const char *err, *aux_err; /* Error string for test */
37 char *expected_err; /* Expected error value of test */
38 char *func; /* Expected error function string */
39 char *reason; /* Expected error reason string */
40 void *data; /* test specific data */
41 } EVP_TEST;
42
43 /*
44 * Test method structure
45 */
46 struct evp_test_method_st {
47 /* Name of test as it appears in file */
48 const char *name;
49 /* Initialise test for "alg" */
50 int (*init) (EVP_TEST * t, const char *alg);
51 /* Clean up method */
52 void (*cleanup) (EVP_TEST * t);
53 /* Test specific name value pair processing */
54 int (*parse) (EVP_TEST * t, const char *name, const char *value);
55 /* Run the test itself */
56 int (*run_test) (EVP_TEST * t);
57 };
58
59
60 /*
61 * Linked list of named keys.
62 */
63 typedef struct key_list_st {
64 char *name;
65 EVP_PKEY *key;
66 struct key_list_st *next;
67 } KEY_LIST;
68
69 /*
70 * List of public and private keys
71 */
72 static KEY_LIST *private_keys;
73 static KEY_LIST *public_keys;
74 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst);
75
76 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen);
77
78 /*
79 * Compare two memory regions for equality, returning zero if they differ.
80 * However, if there is expected to be an error and the actual error
81 * matches then the memory is expected to be different so handle this
82 * case without producing unnecessary test framework output.
83 */
84 static int memory_err_compare(EVP_TEST *t, const char *err,
85 const void *expected, size_t expected_len,
86 const void *got, size_t got_len)
87 {
88 int r;
89
90 if (t->expected_err != NULL && strcmp(t->expected_err, err) == 0)
91 r = !TEST_mem_ne(expected, expected_len, got, got_len);
92 else
93 r = TEST_mem_eq(expected, expected_len, got, got_len);
94 if (!r)
95 t->err = err;
96 return r;
97 }
98
99 /*
100 * Structure used to hold a list of blocks of memory to test
101 * calls to "update" like functions.
102 */
103 struct evp_test_buffer_st {
104 unsigned char *buf;
105 size_t buflen;
106 size_t count;
107 int count_set;
108 };
109
110 static void evp_test_buffer_free(EVP_TEST_BUFFER *db)
111 {
112 if (db != NULL) {
113 OPENSSL_free(db->buf);
114 OPENSSL_free(db);
115 }
116 }
117
118 /*
119 * append buffer to a list
120 */
121 static int evp_test_buffer_append(const char *value,
122 STACK_OF(EVP_TEST_BUFFER) **sk)
123 {
124 EVP_TEST_BUFFER *db = NULL;
125
126 if (!TEST_ptr(db = OPENSSL_malloc(sizeof(*db))))
127 goto err;
128
129 if (!parse_bin(value, &db->buf, &db->buflen))
130 goto err;
131 db->count = 1;
132 db->count_set = 0;
133
134 if (*sk == NULL && !TEST_ptr(*sk = sk_EVP_TEST_BUFFER_new_null()))
135 goto err;
136 if (!sk_EVP_TEST_BUFFER_push(*sk, db))
137 goto err;
138
139 return 1;
140
141 err:
142 evp_test_buffer_free(db);
143 return 0;
144 }
145
146 /*
147 * replace last buffer in list with copies of itself
148 */
149 static int evp_test_buffer_ncopy(const char *value,
150 STACK_OF(EVP_TEST_BUFFER) *sk)
151 {
152 EVP_TEST_BUFFER *db;
153 unsigned char *tbuf, *p;
154 size_t tbuflen;
155 int ncopy = atoi(value);
156 int i;
157
158 if (ncopy <= 0)
159 return 0;
160 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
161 return 0;
162 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
163
164 tbuflen = db->buflen * ncopy;
165 if (!TEST_ptr(tbuf = OPENSSL_malloc(tbuflen)))
166 return 0;
167 for (i = 0, p = tbuf; i < ncopy; i++, p += db->buflen)
168 memcpy(p, db->buf, db->buflen);
169
170 OPENSSL_free(db->buf);
171 db->buf = tbuf;
172 db->buflen = tbuflen;
173 return 1;
174 }
175
176 /*
177 * set repeat count for last buffer in list
178 */
179 static int evp_test_buffer_set_count(const char *value,
180 STACK_OF(EVP_TEST_BUFFER) *sk)
181 {
182 EVP_TEST_BUFFER *db;
183 int count = atoi(value);
184
185 if (count <= 0)
186 return 0;
187
188 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
189 return 0;
190
191 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
192 if (db->count_set != 0)
193 return 0;
194
195 db->count = (size_t)count;
196 db->count_set = 1;
197 return 1;
198 }
199
200 /*
201 * call "fn" with each element of the list in turn
202 */
203 static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER) *sk,
204 int (*fn)(void *ctx,
205 const unsigned char *buf,
206 size_t buflen),
207 void *ctx)
208 {
209 int i;
210
211 for (i = 0; i < sk_EVP_TEST_BUFFER_num(sk); i++) {
212 EVP_TEST_BUFFER *tb = sk_EVP_TEST_BUFFER_value(sk, i);
213 size_t j;
214
215 for (j = 0; j < tb->count; j++) {
216 if (fn(ctx, tb->buf, tb->buflen) <= 0)
217 return 0;
218 }
219 }
220 return 1;
221 }
222
223 /*
224 * Unescape some sequences in string literals (only \n for now).
225 * Return an allocated buffer, set |out_len|. If |input_len|
226 * is zero, get an empty buffer but set length to zero.
227 */
228 static unsigned char* unescape(const char *input, size_t input_len,
229 size_t *out_len)
230 {
231 unsigned char *ret, *p;
232 size_t i;
233
234 if (input_len == 0) {
235 *out_len = 0;
236 return OPENSSL_zalloc(1);
237 }
238
239 /* Escaping is non-expanding; over-allocate original size for simplicity. */
240 if (!TEST_ptr(ret = p = OPENSSL_malloc(input_len)))
241 return NULL;
242
243 for (i = 0; i < input_len; i++) {
244 if (*input == '\\') {
245 if (i == input_len - 1 || *++input != 'n') {
246 TEST_error("Bad escape sequence in file");
247 goto err;
248 }
249 *p++ = '\n';
250 i++;
251 input++;
252 } else {
253 *p++ = *input++;
254 }
255 }
256
257 *out_len = p - ret;
258 return ret;
259
260 err:
261 OPENSSL_free(ret);
262 return NULL;
263 }
264
265 /*
266 * For a hex string "value" convert to a binary allocated buffer.
267 * Return 1 on success or 0 on failure.
268 */
269 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen)
270 {
271 long len;
272
273 /* Check for NULL literal */
274 if (strcmp(value, "NULL") == 0) {
275 *buf = NULL;
276 *buflen = 0;
277 return 1;
278 }
279
280 /* Check for empty value */
281 if (*value == '\0') {
282 /*
283 * Don't return NULL for zero length buffer. This is needed for
284 * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key
285 * buffer even if the key length is 0, in order to detect key reset.
286 */
287 *buf = OPENSSL_malloc(1);
288 if (*buf == NULL)
289 return 0;
290 **buf = 0;
291 *buflen = 0;
292 return 1;
293 }
294
295 /* Check for string literal */
296 if (value[0] == '"') {
297 size_t vlen = strlen(++value);
298
299 if (vlen == 0 || value[vlen - 1] != '"')
300 return 0;
301 vlen--;
302 *buf = unescape(value, vlen, buflen);
303 return *buf == NULL ? 0 : 1;
304 }
305
306 /* Otherwise assume as hex literal and convert it to binary buffer */
307 if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) {
308 TEST_info("Can't convert %s", value);
309 TEST_openssl_errors();
310 return -1;
311 }
312 /* Size of input buffer means we'll never overflow */
313 *buflen = len;
314 return 1;
315 }
316
317
318 /**
319 *** MESSAGE DIGEST TESTS
320 **/
321
322 typedef struct digest_data_st {
323 /* Digest this test is for */
324 const EVP_MD *digest;
325 /* Input to digest */
326 STACK_OF(EVP_TEST_BUFFER) *input;
327 /* Expected output */
328 unsigned char *output;
329 size_t output_len;
330 } DIGEST_DATA;
331
332 static int digest_test_init(EVP_TEST *t, const char *alg)
333 {
334 DIGEST_DATA *mdat;
335 const EVP_MD *digest;
336
337 if ((digest = EVP_get_digestbyname(alg)) == NULL) {
338 /* If alg has an OID assume disabled algorithm */
339 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
340 t->skip = 1;
341 return 1;
342 }
343 return 0;
344 }
345 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
346 return 0;
347 t->data = mdat;
348 mdat->digest = digest;
349 return 1;
350 }
351
352 static void digest_test_cleanup(EVP_TEST *t)
353 {
354 DIGEST_DATA *mdat = t->data;
355
356 sk_EVP_TEST_BUFFER_pop_free(mdat->input, evp_test_buffer_free);
357 OPENSSL_free(mdat->output);
358 }
359
360 static int digest_test_parse(EVP_TEST *t,
361 const char *keyword, const char *value)
362 {
363 DIGEST_DATA *mdata = t->data;
364
365 if (strcmp(keyword, "Input") == 0)
366 return evp_test_buffer_append(value, &mdata->input);
367 if (strcmp(keyword, "Output") == 0)
368 return parse_bin(value, &mdata->output, &mdata->output_len);
369 if (strcmp(keyword, "Count") == 0)
370 return evp_test_buffer_set_count(value, mdata->input);
371 if (strcmp(keyword, "Ncopy") == 0)
372 return evp_test_buffer_ncopy(value, mdata->input);
373 return 0;
374 }
375
376 static int digest_update_fn(void *ctx, const unsigned char *buf, size_t buflen)
377 {
378 return EVP_DigestUpdate(ctx, buf, buflen);
379 }
380
381 static int digest_test_run(EVP_TEST *t)
382 {
383 DIGEST_DATA *expected = t->data;
384 EVP_MD_CTX *mctx;
385 unsigned char *got = NULL;
386 unsigned int got_len;
387
388 t->err = "TEST_FAILURE";
389 if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
390 goto err;
391
392 got = OPENSSL_malloc(expected->output_len > EVP_MAX_MD_SIZE ?
393 expected->output_len : EVP_MAX_MD_SIZE);
394 if (!TEST_ptr(got))
395 goto err;
396
397 if (!EVP_DigestInit_ex(mctx, expected->digest, NULL)) {
398 t->err = "DIGESTINIT_ERROR";
399 goto err;
400 }
401 if (!evp_test_buffer_do(expected->input, digest_update_fn, mctx)) {
402 t->err = "DIGESTUPDATE_ERROR";
403 goto err;
404 }
405
406 if (EVP_MD_flags(expected->digest) & EVP_MD_FLAG_XOF) {
407 got_len = expected->output_len;
408 if (!EVP_DigestFinalXOF(mctx, got, got_len)) {
409 t->err = "DIGESTFINALXOF_ERROR";
410 goto err;
411 }
412 } else {
413 if (!EVP_DigestFinal(mctx, got, &got_len)) {
414 t->err = "DIGESTFINAL_ERROR";
415 goto err;
416 }
417 }
418 if (!TEST_int_eq(expected->output_len, got_len)) {
419 t->err = "DIGEST_LENGTH_MISMATCH";
420 goto err;
421 }
422 if (!memory_err_compare(t, "DIGEST_MISMATCH",
423 expected->output, expected->output_len,
424 got, got_len))
425 goto err;
426
427 t->err = NULL;
428
429 err:
430 OPENSSL_free(got);
431 EVP_MD_CTX_free(mctx);
432 return 1;
433 }
434
435 static const EVP_TEST_METHOD digest_test_method = {
436 "Digest",
437 digest_test_init,
438 digest_test_cleanup,
439 digest_test_parse,
440 digest_test_run
441 };
442
443
444 /**
445 *** CIPHER TESTS
446 **/
447
448 typedef struct cipher_data_st {
449 const EVP_CIPHER *cipher;
450 int enc;
451 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
452 int aead;
453 unsigned char *key;
454 size_t key_len;
455 unsigned char *iv;
456 size_t iv_len;
457 unsigned char *plaintext;
458 size_t plaintext_len;
459 unsigned char *ciphertext;
460 size_t ciphertext_len;
461 /* GCM, CCM, OCB and SIV only */
462 unsigned char *aad[AAD_NUM];
463 size_t aad_len[AAD_NUM];
464 unsigned char *tag;
465 size_t tag_len;
466 } CIPHER_DATA;
467
468 static int cipher_test_init(EVP_TEST *t, const char *alg)
469 {
470 const EVP_CIPHER *cipher;
471 CIPHER_DATA *cdat;
472 int m;
473
474 if ((cipher = EVP_get_cipherbyname(alg)) == NULL) {
475 /* If alg has an OID assume disabled algorithm */
476 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
477 t->skip = 1;
478 return 1;
479 }
480 return 0;
481 }
482 cdat = OPENSSL_zalloc(sizeof(*cdat));
483 cdat->cipher = cipher;
484 cdat->enc = -1;
485 m = EVP_CIPHER_mode(cipher);
486 if (m == EVP_CIPH_GCM_MODE
487 || m == EVP_CIPH_OCB_MODE
488 || m == EVP_CIPH_SIV_MODE
489 || m == EVP_CIPH_CCM_MODE)
490 cdat->aead = m;
491 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
492 cdat->aead = -1;
493 else
494 cdat->aead = 0;
495
496 t->data = cdat;
497 return 1;
498 }
499
500 static void cipher_test_cleanup(EVP_TEST *t)
501 {
502 int i;
503 CIPHER_DATA *cdat = t->data;
504
505 OPENSSL_free(cdat->key);
506 OPENSSL_free(cdat->iv);
507 OPENSSL_free(cdat->ciphertext);
508 OPENSSL_free(cdat->plaintext);
509 for (i = 0; i < AAD_NUM; i++)
510 OPENSSL_free(cdat->aad[i]);
511 OPENSSL_free(cdat->tag);
512 }
513
514 static int cipher_test_parse(EVP_TEST *t, const char *keyword,
515 const char *value)
516 {
517 CIPHER_DATA *cdat = t->data;
518 int i;
519
520 if (strcmp(keyword, "Key") == 0)
521 return parse_bin(value, &cdat->key, &cdat->key_len);
522 if (strcmp(keyword, "IV") == 0)
523 return parse_bin(value, &cdat->iv, &cdat->iv_len);
524 if (strcmp(keyword, "Plaintext") == 0)
525 return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len);
526 if (strcmp(keyword, "Ciphertext") == 0)
527 return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
528 if (cdat->aead) {
529 if (strcmp(keyword, "AAD") == 0) {
530 for (i = 0; i < AAD_NUM; i++) {
531 if (cdat->aad[i] == NULL)
532 return parse_bin(value, &cdat->aad[i], &cdat->aad_len[i]);
533 }
534 return 0;
535 }
536 if (strcmp(keyword, "Tag") == 0)
537 return parse_bin(value, &cdat->tag, &cdat->tag_len);
538 }
539
540 if (strcmp(keyword, "Operation") == 0) {
541 if (strcmp(value, "ENCRYPT") == 0)
542 cdat->enc = 1;
543 else if (strcmp(value, "DECRYPT") == 0)
544 cdat->enc = 0;
545 else
546 return 0;
547 return 1;
548 }
549 return 0;
550 }
551
552 static int cipher_test_enc(EVP_TEST *t, int enc,
553 size_t out_misalign, size_t inp_misalign, int frag)
554 {
555 CIPHER_DATA *expected = t->data;
556 unsigned char *in, *expected_out, *tmp = NULL;
557 size_t in_len, out_len, donelen = 0;
558 int ok = 0, tmplen, chunklen, tmpflen, i;
559 EVP_CIPHER_CTX *ctx = NULL;
560
561 t->err = "TEST_FAILURE";
562 if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
563 goto err;
564 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
565 if (enc) {
566 in = expected->plaintext;
567 in_len = expected->plaintext_len;
568 expected_out = expected->ciphertext;
569 out_len = expected->ciphertext_len;
570 } else {
571 in = expected->ciphertext;
572 in_len = expected->ciphertext_len;
573 expected_out = expected->plaintext;
574 out_len = expected->plaintext_len;
575 }
576 if (inp_misalign == (size_t)-1) {
577 /*
578 * Exercise in-place encryption
579 */
580 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
581 if (!tmp)
582 goto err;
583 in = memcpy(tmp + out_misalign, in, in_len);
584 } else {
585 inp_misalign += 16 - ((out_misalign + in_len) & 15);
586 /*
587 * 'tmp' will store both output and copy of input. We make the copy
588 * of input to specifically aligned part of 'tmp'. So we just
589 * figured out how much padding would ensure the required alignment,
590 * now we allocate extended buffer and finally copy the input just
591 * past inp_misalign in expression below. Output will be written
592 * past out_misalign...
593 */
594 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
595 inp_misalign + in_len);
596 if (!tmp)
597 goto err;
598 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
599 inp_misalign, in, in_len);
600 }
601 if (!EVP_CipherInit_ex(ctx, expected->cipher, NULL, NULL, NULL, enc)) {
602 t->err = "CIPHERINIT_ERROR";
603 goto err;
604 }
605 if (expected->iv) {
606 if (expected->aead) {
607 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
608 expected->iv_len, 0)) {
609 t->err = "INVALID_IV_LENGTH";
610 goto err;
611 }
612 } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx)) {
613 t->err = "INVALID_IV_LENGTH";
614 goto err;
615 }
616 }
617 if (expected->aead) {
618 unsigned char *tag;
619 /*
620 * If encrypting or OCB just set tag length initially, otherwise
621 * set tag length and value.
622 */
623 if (enc || expected->aead == EVP_CIPH_OCB_MODE) {
624 t->err = "TAG_LENGTH_SET_ERROR";
625 tag = NULL;
626 } else {
627 t->err = "TAG_SET_ERROR";
628 tag = expected->tag;
629 }
630 if (tag || expected->aead != EVP_CIPH_GCM_MODE) {
631 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
632 expected->tag_len, tag))
633 goto err;
634 }
635 }
636
637 if (!EVP_CIPHER_CTX_set_key_length(ctx, expected->key_len)) {
638 t->err = "INVALID_KEY_LENGTH";
639 goto err;
640 }
641 if (!EVP_CipherInit_ex(ctx, NULL, NULL, expected->key, expected->iv, -1)) {
642 t->err = "KEY_SET_ERROR";
643 goto err;
644 }
645
646 if (!enc && expected->aead == EVP_CIPH_OCB_MODE) {
647 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
648 expected->tag_len, expected->tag)) {
649 t->err = "TAG_SET_ERROR";
650 goto err;
651 }
652 }
653
654 if (expected->aead == EVP_CIPH_CCM_MODE) {
655 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
656 t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
657 goto err;
658 }
659 }
660 if (expected->aad[0] != NULL) {
661 t->err = "AAD_SET_ERROR";
662 if (!frag) {
663 for (i = 0; expected->aad[i] != NULL; i++) {
664 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i],
665 expected->aad_len[i]))
666 goto err;
667 }
668 } else {
669 /*
670 * Supply the AAD in chunks less than the block size where possible
671 */
672 for (i = 0; expected->aad[i] != NULL; i++) {
673 if (expected->aad_len[i] > 0) {
674 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], 1))
675 goto err;
676 donelen++;
677 }
678 if (expected->aad_len[i] > 2) {
679 if (!EVP_CipherUpdate(ctx, NULL, &chunklen,
680 expected->aad[i] + donelen,
681 expected->aad_len[i] - 2))
682 goto err;
683 donelen += expected->aad_len[i] - 2;
684 }
685 if (expected->aad_len[i] > 1
686 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
687 expected->aad[i] + donelen, 1))
688 goto err;
689 }
690 }
691 }
692 EVP_CIPHER_CTX_set_padding(ctx, 0);
693 t->err = "CIPHERUPDATE_ERROR";
694 tmplen = 0;
695 if (!frag) {
696 /* We supply the data all in one go */
697 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
698 goto err;
699 } else {
700 /* Supply the data in chunks less than the block size where possible */
701 if (in_len > 0) {
702 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
703 goto err;
704 tmplen += chunklen;
705 in++;
706 in_len--;
707 }
708 if (in_len > 1) {
709 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
710 in, in_len - 1))
711 goto err;
712 tmplen += chunklen;
713 in += in_len - 1;
714 in_len = 1;
715 }
716 if (in_len > 0 ) {
717 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
718 in, 1))
719 goto err;
720 tmplen += chunklen;
721 }
722 }
723 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
724 t->err = "CIPHERFINAL_ERROR";
725 goto err;
726 }
727 if (!memory_err_compare(t, "VALUE_MISMATCH", expected_out, out_len,
728 tmp + out_misalign, tmplen + tmpflen))
729 goto err;
730 if (enc && expected->aead) {
731 unsigned char rtag[16];
732
733 if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) {
734 t->err = "TAG_LENGTH_INTERNAL_ERROR";
735 goto err;
736 }
737 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
738 expected->tag_len, rtag)) {
739 t->err = "TAG_RETRIEVE_ERROR";
740 goto err;
741 }
742 if (!memory_err_compare(t, "TAG_VALUE_MISMATCH",
743 expected->tag, expected->tag_len,
744 rtag, expected->tag_len))
745 goto err;
746 }
747 t->err = NULL;
748 ok = 1;
749 err:
750 OPENSSL_free(tmp);
751 EVP_CIPHER_CTX_free(ctx);
752 return ok;
753 }
754
755 static int cipher_test_run(EVP_TEST *t)
756 {
757 CIPHER_DATA *cdat = t->data;
758 int rv, frag = 0;
759 size_t out_misalign, inp_misalign;
760
761 if (!cdat->key) {
762 t->err = "NO_KEY";
763 return 0;
764 }
765 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
766 /* IV is optional and usually omitted in wrap mode */
767 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
768 t->err = "NO_IV";
769 return 0;
770 }
771 }
772 if (cdat->aead && !cdat->tag) {
773 t->err = "NO_TAG";
774 return 0;
775 }
776 for (out_misalign = 0; out_misalign <= 1;) {
777 static char aux_err[64];
778 t->aux_err = aux_err;
779 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
780 if (inp_misalign == (size_t)-1) {
781 /* kludge: inp_misalign == -1 means "exercise in-place" */
782 BIO_snprintf(aux_err, sizeof(aux_err),
783 "%s in-place, %sfragmented",
784 out_misalign ? "misaligned" : "aligned",
785 frag ? "" : "not ");
786 } else {
787 BIO_snprintf(aux_err, sizeof(aux_err),
788 "%s output and %s input, %sfragmented",
789 out_misalign ? "misaligned" : "aligned",
790 inp_misalign ? "misaligned" : "aligned",
791 frag ? "" : "not ");
792 }
793 if (cdat->enc) {
794 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
795 /* Not fatal errors: return */
796 if (rv != 1) {
797 if (rv < 0)
798 return 0;
799 return 1;
800 }
801 }
802 if (cdat->enc != 1) {
803 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
804 /* Not fatal errors: return */
805 if (rv != 1) {
806 if (rv < 0)
807 return 0;
808 return 1;
809 }
810 }
811 }
812
813 if (out_misalign == 1 && frag == 0) {
814 /*
815 * XTS, SIV, CCM and Wrap modes have special requirements about input
816 * lengths so we don't fragment for those
817 */
818 if (cdat->aead == EVP_CIPH_CCM_MODE
819 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_SIV_MODE
820 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
821 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
822 break;
823 out_misalign = 0;
824 frag++;
825 } else {
826 out_misalign++;
827 }
828 }
829 t->aux_err = NULL;
830
831 return 1;
832 }
833
834 static const EVP_TEST_METHOD cipher_test_method = {
835 "Cipher",
836 cipher_test_init,
837 cipher_test_cleanup,
838 cipher_test_parse,
839 cipher_test_run
840 };
841
842
843 /**
844 *** MAC TESTS
845 **/
846
847 typedef struct mac_data_st {
848 /* MAC type in one form or another */
849 const EVP_MAC *mac; /* for mac_test_run_mac */
850 int type; /* for mac_test_run_pkey */
851 /* Algorithm string for this MAC */
852 char *alg;
853 /* MAC key */
854 unsigned char *key;
855 size_t key_len;
856 /* MAC IV (GMAC) */
857 unsigned char *iv;
858 size_t iv_len;
859 /* Input to MAC */
860 unsigned char *input;
861 size_t input_len;
862 /* Expected output */
863 unsigned char *output;
864 size_t output_len;
865 unsigned char *custom;
866 size_t custom_len;
867 /* Collection of controls */
868 STACK_OF(OPENSSL_STRING) *controls;
869 } MAC_DATA;
870
871 static int mac_test_init(EVP_TEST *t, const char *alg)
872 {
873 const EVP_MAC *mac = NULL;
874 int type = NID_undef;
875 MAC_DATA *mdat;
876
877 if ((mac = EVP_get_macbyname(alg)) == NULL) {
878 /*
879 * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
880 * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
881 * the EVP_PKEY method.
882 */
883 size_t sz = strlen(alg);
884 static const char epilogue[] = " by EVP_PKEY";
885
886 if (sz >= sizeof(epilogue)
887 && strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0)
888 sz -= sizeof(epilogue) - 1;
889
890 if (strncmp(alg, "HMAC", sz) == 0) {
891 type = EVP_PKEY_HMAC;
892 } else if (strncmp(alg, "CMAC", sz) == 0) {
893 #ifndef OPENSSL_NO_CMAC
894 type = EVP_PKEY_CMAC;
895 #else
896 t->skip = 1;
897 return 1;
898 #endif
899 } else if (strncmp(alg, "Poly1305", sz) == 0) {
900 #ifndef OPENSSL_NO_POLY1305
901 type = EVP_PKEY_POLY1305;
902 #else
903 t->skip = 1;
904 return 1;
905 #endif
906 } else if (strncmp(alg, "SipHash", sz) == 0) {
907 #ifndef OPENSSL_NO_SIPHASH
908 type = EVP_PKEY_SIPHASH;
909 #else
910 t->skip = 1;
911 return 1;
912 #endif
913 } else {
914 /*
915 * Not a known EVP_PKEY method either. If it's a known OID, then
916 * assume it's been disabled.
917 */
918 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
919 t->skip = 1;
920 return 1;
921 }
922
923 return 0;
924 }
925 }
926
927 mdat = OPENSSL_zalloc(sizeof(*mdat));
928 mdat->type = type;
929 mdat->mac = mac;
930 mdat->controls = sk_OPENSSL_STRING_new_null();
931 t->data = mdat;
932 return 1;
933 }
934
935 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
936 static void openssl_free(char *m)
937 {
938 OPENSSL_free(m);
939 }
940
941 static void mac_test_cleanup(EVP_TEST *t)
942 {
943 MAC_DATA *mdat = t->data;
944
945 sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free);
946 OPENSSL_free(mdat->alg);
947 OPENSSL_free(mdat->key);
948 OPENSSL_free(mdat->iv);
949 OPENSSL_free(mdat->custom);
950 OPENSSL_free(mdat->input);
951 OPENSSL_free(mdat->output);
952 }
953
954 static int mac_test_parse(EVP_TEST *t,
955 const char *keyword, const char *value)
956 {
957 MAC_DATA *mdata = t->data;
958
959 if (strcmp(keyword, "Key") == 0)
960 return parse_bin(value, &mdata->key, &mdata->key_len);
961 if (strcmp(keyword, "IV") == 0)
962 return parse_bin(value, &mdata->iv, &mdata->iv_len);
963 if (strcmp(keyword, "Custom") == 0)
964 return parse_bin(value, &mdata->custom, &mdata->custom_len);
965 if (strcmp(keyword, "Algorithm") == 0) {
966 mdata->alg = OPENSSL_strdup(value);
967 if (!mdata->alg)
968 return 0;
969 return 1;
970 }
971 if (strcmp(keyword, "Input") == 0)
972 return parse_bin(value, &mdata->input, &mdata->input_len);
973 if (strcmp(keyword, "Output") == 0)
974 return parse_bin(value, &mdata->output, &mdata->output_len);
975 if (strcmp(keyword, "Ctrl") == 0)
976 return sk_OPENSSL_STRING_push(mdata->controls,
977 OPENSSL_strdup(value)) != 0;
978 return 0;
979 }
980
981 static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx,
982 const char *value)
983 {
984 int rv;
985 char *p, *tmpval;
986
987 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
988 return 0;
989 p = strchr(tmpval, ':');
990 if (p != NULL)
991 *p++ = '\0';
992 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
993 if (rv == -2)
994 t->err = "PKEY_CTRL_INVALID";
995 else if (rv <= 0)
996 t->err = "PKEY_CTRL_ERROR";
997 else
998 rv = 1;
999 OPENSSL_free(tmpval);
1000 return rv > 0;
1001 }
1002
1003 static int mac_test_run_pkey(EVP_TEST *t)
1004 {
1005 MAC_DATA *expected = t->data;
1006 EVP_MD_CTX *mctx = NULL;
1007 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1008 EVP_PKEY *key = NULL;
1009 const EVP_MD *md = NULL;
1010 unsigned char *got = NULL;
1011 size_t got_len;
1012 int i;
1013
1014 if (expected->alg == NULL)
1015 TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type));
1016 else
1017 TEST_info("Trying the EVP_PKEY %s test with %s",
1018 OBJ_nid2sn(expected->type), expected->alg);
1019
1020 #ifdef OPENSSL_NO_DES
1021 if (expected->alg != NULL && strstr(expected->alg, "DES") != NULL) {
1022 /* Skip DES */
1023 t->err = NULL;
1024 goto err;
1025 }
1026 #endif
1027
1028 if (expected->type == EVP_PKEY_CMAC)
1029 key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len,
1030 EVP_get_cipherbyname(expected->alg));
1031 else
1032 key = EVP_PKEY_new_raw_private_key(expected->type, NULL, expected->key,
1033 expected->key_len);
1034 if (key == NULL) {
1035 t->err = "MAC_KEY_CREATE_ERROR";
1036 goto err;
1037 }
1038
1039 if (expected->type == EVP_PKEY_HMAC) {
1040 if (!TEST_ptr(md = EVP_get_digestbyname(expected->alg))) {
1041 t->err = "MAC_ALGORITHM_SET_ERROR";
1042 goto err;
1043 }
1044 }
1045 if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
1046 t->err = "INTERNAL_ERROR";
1047 goto err;
1048 }
1049 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key)) {
1050 t->err = "DIGESTSIGNINIT_ERROR";
1051 goto err;
1052 }
1053 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++)
1054 if (!mac_test_ctrl_pkey(t, pctx,
1055 sk_OPENSSL_STRING_value(expected->controls,
1056 i))) {
1057 t->err = "EVPPKEYCTXCTRL_ERROR";
1058 goto err;
1059 }
1060 if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) {
1061 t->err = "DIGESTSIGNUPDATE_ERROR";
1062 goto err;
1063 }
1064 if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) {
1065 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1066 goto err;
1067 }
1068 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1069 t->err = "TEST_FAILURE";
1070 goto err;
1071 }
1072 if (!EVP_DigestSignFinal(mctx, got, &got_len)
1073 || !memory_err_compare(t, "TEST_MAC_ERR",
1074 expected->output, expected->output_len,
1075 got, got_len)) {
1076 t->err = "TEST_MAC_ERR";
1077 goto err;
1078 }
1079 t->err = NULL;
1080 err:
1081 EVP_MD_CTX_free(mctx);
1082 OPENSSL_free(got);
1083 EVP_PKEY_CTX_free(genctx);
1084 EVP_PKEY_free(key);
1085 return 1;
1086 }
1087
1088 static int mac_test_run_mac(EVP_TEST *t)
1089 {
1090 MAC_DATA *expected = t->data;
1091 EVP_MAC_CTX *ctx = NULL;
1092 const void *algo = NULL;
1093 int algo_ctrl = 0;
1094 unsigned char *got = NULL;
1095 size_t got_len;
1096 int rv, i;
1097
1098 if (expected->alg == NULL)
1099 TEST_info("Trying the EVP_MAC %s test", EVP_MAC_name(expected->mac));
1100 else
1101 TEST_info("Trying the EVP_MAC %s test with %s",
1102 EVP_MAC_name(expected->mac), expected->alg);
1103
1104 #ifdef OPENSSL_NO_DES
1105 if (expected->alg != NULL && strstr(expected->alg, "DES") != NULL) {
1106 /* Skip DES */
1107 t->err = NULL;
1108 goto err;
1109 }
1110 #endif
1111
1112 if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) {
1113 t->err = "MAC_CREATE_ERROR";
1114 goto err;
1115 }
1116
1117 if (expected->alg != NULL
1118 && ((algo_ctrl = EVP_MAC_CTRL_SET_CIPHER,
1119 algo = EVP_get_cipherbyname(expected->alg)) == NULL
1120 && (algo_ctrl = EVP_MAC_CTRL_SET_MD,
1121 algo = EVP_get_digestbyname(expected->alg)) == NULL)) {
1122 t->err = "MAC_BAD_ALGORITHM";
1123 goto err;
1124 }
1125
1126
1127 if (algo_ctrl != 0) {
1128 rv = EVP_MAC_ctrl(ctx, algo_ctrl, algo);
1129 if (rv == -2) {
1130 t->err = "MAC_CTRL_INVALID";
1131 goto err;
1132 } else if (rv <= 0) {
1133 t->err = "MAC_CTRL_ERROR";
1134 goto err;
1135 }
1136 }
1137
1138 rv = EVP_MAC_ctrl(ctx, EVP_MAC_CTRL_SET_KEY,
1139 expected->key, expected->key_len);
1140 if (rv == -2) {
1141 t->err = "MAC_CTRL_INVALID";
1142 goto err;
1143 } else if (rv <= 0) {
1144 t->err = "MAC_CTRL_ERROR";
1145 goto err;
1146 }
1147 if (expected->custom != NULL) {
1148 rv = EVP_MAC_ctrl(ctx, EVP_MAC_CTRL_SET_CUSTOM,
1149 expected->custom, expected->custom_len);
1150 if (rv == -2) {
1151 t->err = "MAC_CTRL_INVALID";
1152 goto err;
1153 } else if (rv <= 0) {
1154 t->err = "MAC_CTRL_ERROR";
1155 goto err;
1156 }
1157 }
1158
1159 if (expected->iv != NULL) {
1160 rv = EVP_MAC_ctrl(ctx, EVP_MAC_CTRL_SET_IV,
1161 expected->iv, expected->iv_len);
1162 if (rv == -2) {
1163 t->err = "MAC_CTRL_INVALID";
1164 goto err;
1165 } else if (rv <= 0) {
1166 t->err = "MAC_CTRL_ERROR";
1167 goto err;
1168 }
1169 }
1170
1171 if (!EVP_MAC_init(ctx)) {
1172 t->err = "MAC_INIT_ERROR";
1173 goto err;
1174 }
1175 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) {
1176 char *p, *tmpval;
1177 char *value = sk_OPENSSL_STRING_value(expected->controls, i);
1178
1179 if (!TEST_ptr(tmpval = OPENSSL_strdup(value))) {
1180 t->err = "MAC_CTRL_ERROR";
1181 goto err;
1182 }
1183 p = strchr(tmpval, ':');
1184 if (p != NULL)
1185 *p++ = '\0';
1186 rv = EVP_MAC_ctrl_str(ctx, tmpval, p);
1187 OPENSSL_free(tmpval);
1188 if (rv == -2) {
1189 t->err = "MAC_CTRL_INVALID";
1190 goto err;
1191 } else if (rv <= 0) {
1192 t->err = "MAC_CTRL_ERROR";
1193 goto err;
1194 }
1195 }
1196 if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) {
1197 t->err = "MAC_UPDATE_ERROR";
1198 goto err;
1199 }
1200 if (!EVP_MAC_final(ctx, NULL, &got_len)) {
1201 t->err = "MAC_FINAL_LENGTH_ERROR";
1202 goto err;
1203 }
1204 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1205 t->err = "TEST_FAILURE";
1206 goto err;
1207 }
1208 if (!EVP_MAC_final(ctx, got, &got_len)
1209 || !memory_err_compare(t, "TEST_MAC_ERR",
1210 expected->output, expected->output_len,
1211 got, got_len)) {
1212 t->err = "TEST_MAC_ERR";
1213 goto err;
1214 }
1215 t->err = NULL;
1216 err:
1217 EVP_MAC_CTX_free(ctx);
1218 OPENSSL_free(got);
1219 return 1;
1220 }
1221
1222 static int mac_test_run(EVP_TEST *t)
1223 {
1224 MAC_DATA *expected = t->data;
1225
1226 if (expected->mac != NULL)
1227 return mac_test_run_mac(t);
1228 return mac_test_run_pkey(t);
1229 }
1230
1231 static const EVP_TEST_METHOD mac_test_method = {
1232 "MAC",
1233 mac_test_init,
1234 mac_test_cleanup,
1235 mac_test_parse,
1236 mac_test_run
1237 };
1238
1239
1240 /**
1241 *** PUBLIC KEY TESTS
1242 *** These are all very similar and share much common code.
1243 **/
1244
1245 typedef struct pkey_data_st {
1246 /* Context for this operation */
1247 EVP_PKEY_CTX *ctx;
1248 /* Key operation to perform */
1249 int (*keyop) (EVP_PKEY_CTX *ctx,
1250 unsigned char *sig, size_t *siglen,
1251 const unsigned char *tbs, size_t tbslen);
1252 /* Input to MAC */
1253 unsigned char *input;
1254 size_t input_len;
1255 /* Expected output */
1256 unsigned char *output;
1257 size_t output_len;
1258 } PKEY_DATA;
1259
1260 /*
1261 * Perform public key operation setup: lookup key, allocated ctx and call
1262 * the appropriate initialisation function
1263 */
1264 static int pkey_test_init(EVP_TEST *t, const char *name,
1265 int use_public,
1266 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1267 int (*keyop)(EVP_PKEY_CTX *ctx,
1268 unsigned char *sig, size_t *siglen,
1269 const unsigned char *tbs,
1270 size_t tbslen))
1271 {
1272 PKEY_DATA *kdata;
1273 EVP_PKEY *pkey = NULL;
1274 int rv = 0;
1275
1276 if (use_public)
1277 rv = find_key(&pkey, name, public_keys);
1278 if (rv == 0)
1279 rv = find_key(&pkey, name, private_keys);
1280 if (rv == 0 || pkey == NULL) {
1281 t->skip = 1;
1282 return 1;
1283 }
1284
1285 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) {
1286 EVP_PKEY_free(pkey);
1287 return 0;
1288 }
1289 kdata->keyop = keyop;
1290 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL))) {
1291 EVP_PKEY_free(pkey);
1292 OPENSSL_free(kdata);
1293 return 0;
1294 }
1295 if (keyopinit(kdata->ctx) <= 0)
1296 t->err = "KEYOP_INIT_ERROR";
1297 t->data = kdata;
1298 return 1;
1299 }
1300
1301 static void pkey_test_cleanup(EVP_TEST *t)
1302 {
1303 PKEY_DATA *kdata = t->data;
1304
1305 OPENSSL_free(kdata->input);
1306 OPENSSL_free(kdata->output);
1307 EVP_PKEY_CTX_free(kdata->ctx);
1308 }
1309
1310 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1311 const char *value)
1312 {
1313 int rv;
1314 char *p, *tmpval;
1315
1316 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1317 return 0;
1318 p = strchr(tmpval, ':');
1319 if (p != NULL)
1320 *p++ = '\0';
1321 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1322 if (rv == -2) {
1323 t->err = "PKEY_CTRL_INVALID";
1324 rv = 1;
1325 } else if (p != NULL && rv <= 0) {
1326 /* If p has an OID and lookup fails assume disabled algorithm */
1327 int nid = OBJ_sn2nid(p);
1328
1329 if (nid == NID_undef)
1330 nid = OBJ_ln2nid(p);
1331 if (nid != NID_undef
1332 && EVP_get_digestbynid(nid) == NULL
1333 && EVP_get_cipherbynid(nid) == NULL) {
1334 t->skip = 1;
1335 rv = 1;
1336 } else {
1337 t->err = "PKEY_CTRL_ERROR";
1338 rv = 1;
1339 }
1340 }
1341 OPENSSL_free(tmpval);
1342 return rv > 0;
1343 }
1344
1345 static int pkey_test_parse(EVP_TEST *t,
1346 const char *keyword, const char *value)
1347 {
1348 PKEY_DATA *kdata = t->data;
1349 if (strcmp(keyword, "Input") == 0)
1350 return parse_bin(value, &kdata->input, &kdata->input_len);
1351 if (strcmp(keyword, "Output") == 0)
1352 return parse_bin(value, &kdata->output, &kdata->output_len);
1353 if (strcmp(keyword, "Ctrl") == 0)
1354 return pkey_test_ctrl(t, kdata->ctx, value);
1355 return 0;
1356 }
1357
1358 static int pkey_test_run(EVP_TEST *t)
1359 {
1360 PKEY_DATA *expected = t->data;
1361 unsigned char *got = NULL;
1362 size_t got_len;
1363
1364 if (expected->keyop(expected->ctx, NULL, &got_len,
1365 expected->input, expected->input_len) <= 0
1366 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1367 t->err = "KEYOP_LENGTH_ERROR";
1368 goto err;
1369 }
1370 if (expected->keyop(expected->ctx, got, &got_len,
1371 expected->input, expected->input_len) <= 0) {
1372 t->err = "KEYOP_ERROR";
1373 goto err;
1374 }
1375 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1376 expected->output, expected->output_len,
1377 got, got_len))
1378 goto err;
1379
1380 t->err = NULL;
1381 err:
1382 OPENSSL_free(got);
1383 return 1;
1384 }
1385
1386 static int sign_test_init(EVP_TEST *t, const char *name)
1387 {
1388 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1389 }
1390
1391 static const EVP_TEST_METHOD psign_test_method = {
1392 "Sign",
1393 sign_test_init,
1394 pkey_test_cleanup,
1395 pkey_test_parse,
1396 pkey_test_run
1397 };
1398
1399 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1400 {
1401 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1402 EVP_PKEY_verify_recover);
1403 }
1404
1405 static const EVP_TEST_METHOD pverify_recover_test_method = {
1406 "VerifyRecover",
1407 verify_recover_test_init,
1408 pkey_test_cleanup,
1409 pkey_test_parse,
1410 pkey_test_run
1411 };
1412
1413 static int decrypt_test_init(EVP_TEST *t, const char *name)
1414 {
1415 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1416 EVP_PKEY_decrypt);
1417 }
1418
1419 static const EVP_TEST_METHOD pdecrypt_test_method = {
1420 "Decrypt",
1421 decrypt_test_init,
1422 pkey_test_cleanup,
1423 pkey_test_parse,
1424 pkey_test_run
1425 };
1426
1427 static int verify_test_init(EVP_TEST *t, const char *name)
1428 {
1429 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1430 }
1431
1432 static int verify_test_run(EVP_TEST *t)
1433 {
1434 PKEY_DATA *kdata = t->data;
1435
1436 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1437 kdata->input, kdata->input_len) <= 0)
1438 t->err = "VERIFY_ERROR";
1439 return 1;
1440 }
1441
1442 static const EVP_TEST_METHOD pverify_test_method = {
1443 "Verify",
1444 verify_test_init,
1445 pkey_test_cleanup,
1446 pkey_test_parse,
1447 verify_test_run
1448 };
1449
1450
1451 static int pderive_test_init(EVP_TEST *t, const char *name)
1452 {
1453 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1454 }
1455
1456 static int pderive_test_parse(EVP_TEST *t,
1457 const char *keyword, const char *value)
1458 {
1459 PKEY_DATA *kdata = t->data;
1460
1461 if (strcmp(keyword, "PeerKey") == 0) {
1462 EVP_PKEY *peer;
1463 if (find_key(&peer, value, public_keys) == 0)
1464 return 0;
1465 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1466 return 0;
1467 return 1;
1468 }
1469 if (strcmp(keyword, "SharedSecret") == 0)
1470 return parse_bin(value, &kdata->output, &kdata->output_len);
1471 if (strcmp(keyword, "Ctrl") == 0)
1472 return pkey_test_ctrl(t, kdata->ctx, value);
1473 return 0;
1474 }
1475
1476 static int pderive_test_run(EVP_TEST *t)
1477 {
1478 PKEY_DATA *expected = t->data;
1479 unsigned char *got = NULL;
1480 size_t got_len;
1481
1482 if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) {
1483 t->err = "DERIVE_ERROR";
1484 goto err;
1485 }
1486 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1487 t->err = "DERIVE_ERROR";
1488 goto err;
1489 }
1490 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
1491 t->err = "DERIVE_ERROR";
1492 goto err;
1493 }
1494 if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH",
1495 expected->output, expected->output_len,
1496 got, got_len))
1497 goto err;
1498
1499 t->err = NULL;
1500 err:
1501 OPENSSL_free(got);
1502 return 1;
1503 }
1504
1505 static const EVP_TEST_METHOD pderive_test_method = {
1506 "Derive",
1507 pderive_test_init,
1508 pkey_test_cleanup,
1509 pderive_test_parse,
1510 pderive_test_run
1511 };
1512
1513
1514 /**
1515 *** PBE TESTS
1516 **/
1517
1518 typedef enum pbe_type_enum {
1519 PBE_TYPE_INVALID = 0,
1520 PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
1521 } PBE_TYPE;
1522
1523 typedef struct pbe_data_st {
1524 PBE_TYPE pbe_type;
1525 /* scrypt parameters */
1526 uint64_t N, r, p, maxmem;
1527 /* PKCS#12 parameters */
1528 int id, iter;
1529 const EVP_MD *md;
1530 /* password */
1531 unsigned char *pass;
1532 size_t pass_len;
1533 /* salt */
1534 unsigned char *salt;
1535 size_t salt_len;
1536 /* Expected output */
1537 unsigned char *key;
1538 size_t key_len;
1539 } PBE_DATA;
1540
1541 #ifndef OPENSSL_NO_SCRYPT
1542 /*
1543 * Parse unsigned decimal 64 bit integer value
1544 */
1545 static int parse_uint64(const char *value, uint64_t *pr)
1546 {
1547 const char *p = value;
1548
1549 if (!TEST_true(*p)) {
1550 TEST_info("Invalid empty integer value");
1551 return -1;
1552 }
1553 for (*pr = 0; *p; ) {
1554 if (*pr > UINT64_MAX / 10) {
1555 TEST_error("Integer overflow in string %s", value);
1556 return -1;
1557 }
1558 *pr *= 10;
1559 if (!TEST_true(isdigit((unsigned char)*p))) {
1560 TEST_error("Invalid character in string %s", value);
1561 return -1;
1562 }
1563 *pr += *p - '0';
1564 p++;
1565 }
1566 return 1;
1567 }
1568
1569 static int scrypt_test_parse(EVP_TEST *t,
1570 const char *keyword, const char *value)
1571 {
1572 PBE_DATA *pdata = t->data;
1573
1574 if (strcmp(keyword, "N") == 0)
1575 return parse_uint64(value, &pdata->N);
1576 if (strcmp(keyword, "p") == 0)
1577 return parse_uint64(value, &pdata->p);
1578 if (strcmp(keyword, "r") == 0)
1579 return parse_uint64(value, &pdata->r);
1580 if (strcmp(keyword, "maxmem") == 0)
1581 return parse_uint64(value, &pdata->maxmem);
1582 return 0;
1583 }
1584 #endif
1585
1586 static int pbkdf2_test_parse(EVP_TEST *t,
1587 const char *keyword, const char *value)
1588 {
1589 PBE_DATA *pdata = t->data;
1590
1591 if (strcmp(keyword, "iter") == 0) {
1592 pdata->iter = atoi(value);
1593 if (pdata->iter <= 0)
1594 return -1;
1595 return 1;
1596 }
1597 if (strcmp(keyword, "MD") == 0) {
1598 pdata->md = EVP_get_digestbyname(value);
1599 if (pdata->md == NULL)
1600 return -1;
1601 return 1;
1602 }
1603 return 0;
1604 }
1605
1606 static int pkcs12_test_parse(EVP_TEST *t,
1607 const char *keyword, const char *value)
1608 {
1609 PBE_DATA *pdata = t->data;
1610
1611 if (strcmp(keyword, "id") == 0) {
1612 pdata->id = atoi(value);
1613 if (pdata->id <= 0)
1614 return -1;
1615 return 1;
1616 }
1617 return pbkdf2_test_parse(t, keyword, value);
1618 }
1619
1620 static int pbe_test_init(EVP_TEST *t, const char *alg)
1621 {
1622 PBE_DATA *pdat;
1623 PBE_TYPE pbe_type = PBE_TYPE_INVALID;
1624
1625 if (strcmp(alg, "scrypt") == 0) {
1626 #ifndef OPENSSL_NO_SCRYPT
1627 pbe_type = PBE_TYPE_SCRYPT;
1628 #else
1629 t->skip = 1;
1630 return 1;
1631 #endif
1632 } else if (strcmp(alg, "pbkdf2") == 0) {
1633 pbe_type = PBE_TYPE_PBKDF2;
1634 } else if (strcmp(alg, "pkcs12") == 0) {
1635 pbe_type = PBE_TYPE_PKCS12;
1636 } else {
1637 TEST_error("Unknown pbe algorithm %s", alg);
1638 }
1639 pdat = OPENSSL_zalloc(sizeof(*pdat));
1640 pdat->pbe_type = pbe_type;
1641 t->data = pdat;
1642 return 1;
1643 }
1644
1645 static void pbe_test_cleanup(EVP_TEST *t)
1646 {
1647 PBE_DATA *pdat = t->data;
1648
1649 OPENSSL_free(pdat->pass);
1650 OPENSSL_free(pdat->salt);
1651 OPENSSL_free(pdat->key);
1652 }
1653
1654 static int pbe_test_parse(EVP_TEST *t,
1655 const char *keyword, const char *value)
1656 {
1657 PBE_DATA *pdata = t->data;
1658
1659 if (strcmp(keyword, "Password") == 0)
1660 return parse_bin(value, &pdata->pass, &pdata->pass_len);
1661 if (strcmp(keyword, "Salt") == 0)
1662 return parse_bin(value, &pdata->salt, &pdata->salt_len);
1663 if (strcmp(keyword, "Key") == 0)
1664 return parse_bin(value, &pdata->key, &pdata->key_len);
1665 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1666 return pbkdf2_test_parse(t, keyword, value);
1667 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1668 return pkcs12_test_parse(t, keyword, value);
1669 #ifndef OPENSSL_NO_SCRYPT
1670 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1671 return scrypt_test_parse(t, keyword, value);
1672 #endif
1673 return 0;
1674 }
1675
1676 static int pbe_test_run(EVP_TEST *t)
1677 {
1678 PBE_DATA *expected = t->data;
1679 unsigned char *key;
1680
1681 if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
1682 t->err = "INTERNAL_ERROR";
1683 goto err;
1684 }
1685 if (expected->pbe_type == PBE_TYPE_PBKDF2) {
1686 if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
1687 expected->salt, expected->salt_len,
1688 expected->iter, expected->md,
1689 expected->key_len, key) == 0) {
1690 t->err = "PBKDF2_ERROR";
1691 goto err;
1692 }
1693 #ifndef OPENSSL_NO_SCRYPT
1694 } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
1695 if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
1696 expected->salt, expected->salt_len, expected->N,
1697 expected->r, expected->p, expected->maxmem,
1698 key, expected->key_len) == 0) {
1699 t->err = "SCRYPT_ERROR";
1700 goto err;
1701 }
1702 #endif
1703 } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
1704 if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
1705 expected->salt, expected->salt_len,
1706 expected->id, expected->iter, expected->key_len,
1707 key, expected->md) == 0) {
1708 t->err = "PKCS12_ERROR";
1709 goto err;
1710 }
1711 }
1712 if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len,
1713 key, expected->key_len))
1714 goto err;
1715
1716 t->err = NULL;
1717 err:
1718 OPENSSL_free(key);
1719 return 1;
1720 }
1721
1722 static const EVP_TEST_METHOD pbe_test_method = {
1723 "PBE",
1724 pbe_test_init,
1725 pbe_test_cleanup,
1726 pbe_test_parse,
1727 pbe_test_run
1728 };
1729
1730
1731 /**
1732 *** BASE64 TESTS
1733 **/
1734
1735 typedef enum {
1736 BASE64_CANONICAL_ENCODING = 0,
1737 BASE64_VALID_ENCODING = 1,
1738 BASE64_INVALID_ENCODING = 2
1739 } base64_encoding_type;
1740
1741 typedef struct encode_data_st {
1742 /* Input to encoding */
1743 unsigned char *input;
1744 size_t input_len;
1745 /* Expected output */
1746 unsigned char *output;
1747 size_t output_len;
1748 base64_encoding_type encoding;
1749 } ENCODE_DATA;
1750
1751 static int encode_test_init(EVP_TEST *t, const char *encoding)
1752 {
1753 ENCODE_DATA *edata;
1754
1755 if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
1756 return 0;
1757 if (strcmp(encoding, "canonical") == 0) {
1758 edata->encoding = BASE64_CANONICAL_ENCODING;
1759 } else if (strcmp(encoding, "valid") == 0) {
1760 edata->encoding = BASE64_VALID_ENCODING;
1761 } else if (strcmp(encoding, "invalid") == 0) {
1762 edata->encoding = BASE64_INVALID_ENCODING;
1763 if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
1764 goto err;
1765 } else {
1766 TEST_error("Bad encoding: %s."
1767 " Should be one of {canonical, valid, invalid}",
1768 encoding);
1769 goto err;
1770 }
1771 t->data = edata;
1772 return 1;
1773 err:
1774 OPENSSL_free(edata);
1775 return 0;
1776 }
1777
1778 static void encode_test_cleanup(EVP_TEST *t)
1779 {
1780 ENCODE_DATA *edata = t->data;
1781
1782 OPENSSL_free(edata->input);
1783 OPENSSL_free(edata->output);
1784 memset(edata, 0, sizeof(*edata));
1785 }
1786
1787 static int encode_test_parse(EVP_TEST *t,
1788 const char *keyword, const char *value)
1789 {
1790 ENCODE_DATA *edata = t->data;
1791
1792 if (strcmp(keyword, "Input") == 0)
1793 return parse_bin(value, &edata->input, &edata->input_len);
1794 if (strcmp(keyword, "Output") == 0)
1795 return parse_bin(value, &edata->output, &edata->output_len);
1796 return 0;
1797 }
1798
1799 static int encode_test_run(EVP_TEST *t)
1800 {
1801 ENCODE_DATA *expected = t->data;
1802 unsigned char *encode_out = NULL, *decode_out = NULL;
1803 int output_len, chunk_len;
1804 EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL;
1805
1806 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
1807 t->err = "INTERNAL_ERROR";
1808 goto err;
1809 }
1810
1811 if (expected->encoding == BASE64_CANONICAL_ENCODING) {
1812
1813 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
1814 || !TEST_ptr(encode_out =
1815 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
1816 goto err;
1817
1818 EVP_EncodeInit(encode_ctx);
1819 if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1820 expected->input, expected->input_len)))
1821 goto err;
1822
1823 output_len = chunk_len;
1824
1825 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
1826 output_len += chunk_len;
1827
1828 if (!memory_err_compare(t, "BAD_ENCODING",
1829 expected->output, expected->output_len,
1830 encode_out, output_len))
1831 goto err;
1832 }
1833
1834 if (!TEST_ptr(decode_out =
1835 OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
1836 goto err;
1837
1838 EVP_DecodeInit(decode_ctx);
1839 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
1840 expected->output_len) < 0) {
1841 t->err = "DECODE_ERROR";
1842 goto err;
1843 }
1844 output_len = chunk_len;
1845
1846 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
1847 t->err = "DECODE_ERROR";
1848 goto err;
1849 }
1850 output_len += chunk_len;
1851
1852 if (expected->encoding != BASE64_INVALID_ENCODING
1853 && !memory_err_compare(t, "BAD_DECODING",
1854 expected->input, expected->input_len,
1855 decode_out, output_len)) {
1856 t->err = "BAD_DECODING";
1857 goto err;
1858 }
1859
1860 t->err = NULL;
1861 err:
1862 OPENSSL_free(encode_out);
1863 OPENSSL_free(decode_out);
1864 EVP_ENCODE_CTX_free(decode_ctx);
1865 EVP_ENCODE_CTX_free(encode_ctx);
1866 return 1;
1867 }
1868
1869 static const EVP_TEST_METHOD encode_test_method = {
1870 "Encoding",
1871 encode_test_init,
1872 encode_test_cleanup,
1873 encode_test_parse,
1874 encode_test_run,
1875 };
1876
1877 /**
1878 *** KDF TESTS
1879 **/
1880
1881 typedef struct kdf_data_st {
1882 /* Context for this operation */
1883 EVP_PKEY_CTX *ctx;
1884 /* Expected output */
1885 unsigned char *output;
1886 size_t output_len;
1887 } KDF_DATA;
1888
1889 /*
1890 * Perform public key operation setup: lookup key, allocated ctx and call
1891 * the appropriate initialisation function
1892 */
1893 static int kdf_test_init(EVP_TEST *t, const char *name)
1894 {
1895 KDF_DATA *kdata;
1896 int kdf_nid = OBJ_sn2nid(name);
1897
1898 #ifdef OPENSSL_NO_SCRYPT
1899 if (strcmp(name, "scrypt") == 0) {
1900 t->skip = 1;
1901 return 1;
1902 }
1903 #endif
1904
1905 if (kdf_nid == NID_undef)
1906 kdf_nid = OBJ_ln2nid(name);
1907
1908 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
1909 return 0;
1910 kdata->ctx = EVP_PKEY_CTX_new_id(kdf_nid, NULL);
1911 if (kdata->ctx == NULL) {
1912 OPENSSL_free(kdata);
1913 return 0;
1914 }
1915 if (EVP_PKEY_derive_init(kdata->ctx) <= 0) {
1916 EVP_PKEY_CTX_free(kdata->ctx);
1917 OPENSSL_free(kdata);
1918 return 0;
1919 }
1920 t->data = kdata;
1921 return 1;
1922 }
1923
1924 static void kdf_test_cleanup(EVP_TEST *t)
1925 {
1926 KDF_DATA *kdata = t->data;
1927 OPENSSL_free(kdata->output);
1928 EVP_PKEY_CTX_free(kdata->ctx);
1929 }
1930
1931 static int kdf_test_parse(EVP_TEST *t,
1932 const char *keyword, const char *value)
1933 {
1934 KDF_DATA *kdata = t->data;
1935
1936 if (strcmp(keyword, "Output") == 0)
1937 return parse_bin(value, &kdata->output, &kdata->output_len);
1938 if (strncmp(keyword, "Ctrl", 4) == 0)
1939 return pkey_test_ctrl(t, kdata->ctx, value);
1940 return 0;
1941 }
1942
1943 static int kdf_test_run(EVP_TEST *t)
1944 {
1945 KDF_DATA *expected = t->data;
1946 unsigned char *got = NULL;
1947 size_t got_len = expected->output_len;
1948
1949 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1950 t->err = "INTERNAL_ERROR";
1951 goto err;
1952 }
1953 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
1954 t->err = "KDF_DERIVE_ERROR";
1955 goto err;
1956 }
1957 if (!memory_err_compare(t, "KDF_MISMATCH",
1958 expected->output, expected->output_len,
1959 got, got_len))
1960 goto err;
1961
1962 t->err = NULL;
1963
1964 err:
1965 OPENSSL_free(got);
1966 return 1;
1967 }
1968
1969 static const EVP_TEST_METHOD kdf_test_method = {
1970 "KDF",
1971 kdf_test_init,
1972 kdf_test_cleanup,
1973 kdf_test_parse,
1974 kdf_test_run
1975 };
1976
1977
1978 /**
1979 *** KEYPAIR TESTS
1980 **/
1981
1982 typedef struct keypair_test_data_st {
1983 EVP_PKEY *privk;
1984 EVP_PKEY *pubk;
1985 } KEYPAIR_TEST_DATA;
1986
1987 static int keypair_test_init(EVP_TEST *t, const char *pair)
1988 {
1989 KEYPAIR_TEST_DATA *data;
1990 int rv = 0;
1991 EVP_PKEY *pk = NULL, *pubk = NULL;
1992 char *pub, *priv = NULL;
1993
1994 /* Split private and public names. */
1995 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
1996 || !TEST_ptr(pub = strchr(priv, ':'))) {
1997 t->err = "PARSING_ERROR";
1998 goto end;
1999 }
2000 *pub++ = '\0';
2001
2002 if (!TEST_true(find_key(&pk, priv, private_keys))) {
2003 TEST_info("Can't find private key: %s", priv);
2004 t->err = "MISSING_PRIVATE_KEY";
2005 goto end;
2006 }
2007 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
2008 TEST_info("Can't find public key: %s", pub);
2009 t->err = "MISSING_PUBLIC_KEY";
2010 goto end;
2011 }
2012
2013 if (pk == NULL && pubk == NULL) {
2014 /* Both keys are listed but unsupported: skip this test */
2015 t->skip = 1;
2016 rv = 1;
2017 goto end;
2018 }
2019
2020 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2021 goto end;
2022 data->privk = pk;
2023 data->pubk = pubk;
2024 t->data = data;
2025 rv = 1;
2026 t->err = NULL;
2027
2028 end:
2029 OPENSSL_free(priv);
2030 return rv;
2031 }
2032
2033 static void keypair_test_cleanup(EVP_TEST *t)
2034 {
2035 OPENSSL_free(t->data);
2036 t->data = NULL;
2037 }
2038
2039 /*
2040 * For tests that do not accept any custom keywords.
2041 */
2042 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
2043 {
2044 return 0;
2045 }
2046
2047 static int keypair_test_run(EVP_TEST *t)
2048 {
2049 int rv = 0;
2050 const KEYPAIR_TEST_DATA *pair = t->data;
2051
2052 if (pair->privk == NULL || pair->pubk == NULL) {
2053 /*
2054 * this can only happen if only one of the keys is not set
2055 * which means that one of them was unsupported while the
2056 * other isn't: hence a key type mismatch.
2057 */
2058 t->err = "KEYPAIR_TYPE_MISMATCH";
2059 rv = 1;
2060 goto end;
2061 }
2062
2063 if ((rv = EVP_PKEY_cmp(pair->privk, pair->pubk)) != 1 ) {
2064 if ( 0 == rv ) {
2065 t->err = "KEYPAIR_MISMATCH";
2066 } else if ( -1 == rv ) {
2067 t->err = "KEYPAIR_TYPE_MISMATCH";
2068 } else if ( -2 == rv ) {
2069 t->err = "UNSUPPORTED_KEY_COMPARISON";
2070 } else {
2071 TEST_error("Unexpected error in key comparison");
2072 rv = 0;
2073 goto end;
2074 }
2075 rv = 1;
2076 goto end;
2077 }
2078
2079 rv = 1;
2080 t->err = NULL;
2081
2082 end:
2083 return rv;
2084 }
2085
2086 static const EVP_TEST_METHOD keypair_test_method = {
2087 "PrivPubKeyPair",
2088 keypair_test_init,
2089 keypair_test_cleanup,
2090 void_test_parse,
2091 keypair_test_run
2092 };
2093
2094 /**
2095 *** KEYGEN TEST
2096 **/
2097
2098 typedef struct keygen_test_data_st {
2099 EVP_PKEY_CTX *genctx; /* Keygen context to use */
2100 char *keyname; /* Key name to store key or NULL */
2101 } KEYGEN_TEST_DATA;
2102
2103 static int keygen_test_init(EVP_TEST *t, const char *alg)
2104 {
2105 KEYGEN_TEST_DATA *data;
2106 EVP_PKEY_CTX *genctx;
2107 int nid = OBJ_sn2nid(alg);
2108
2109 if (nid == NID_undef) {
2110 nid = OBJ_ln2nid(alg);
2111 if (nid == NID_undef)
2112 return 0;
2113 }
2114
2115 if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_id(nid, NULL))) {
2116 /* assume algorithm disabled */
2117 t->skip = 1;
2118 return 1;
2119 }
2120
2121 if (EVP_PKEY_keygen_init(genctx) <= 0) {
2122 t->err = "KEYGEN_INIT_ERROR";
2123 goto err;
2124 }
2125
2126 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2127 goto err;
2128 data->genctx = genctx;
2129 data->keyname = NULL;
2130 t->data = data;
2131 t->err = NULL;
2132 return 1;
2133
2134 err:
2135 EVP_PKEY_CTX_free(genctx);
2136 return 0;
2137 }
2138
2139 static void keygen_test_cleanup(EVP_TEST *t)
2140 {
2141 KEYGEN_TEST_DATA *keygen = t->data;
2142
2143 EVP_PKEY_CTX_free(keygen->genctx);
2144 OPENSSL_free(keygen->keyname);
2145 OPENSSL_free(t->data);
2146 t->data = NULL;
2147 }
2148
2149 static int keygen_test_parse(EVP_TEST *t,
2150 const char *keyword, const char *value)
2151 {
2152 KEYGEN_TEST_DATA *keygen = t->data;
2153
2154 if (strcmp(keyword, "KeyName") == 0)
2155 return TEST_ptr(keygen->keyname = OPENSSL_strdup(value));
2156 if (strcmp(keyword, "Ctrl") == 0)
2157 return pkey_test_ctrl(t, keygen->genctx, value);
2158 return 0;
2159 }
2160
2161 static int keygen_test_run(EVP_TEST *t)
2162 {
2163 KEYGEN_TEST_DATA *keygen = t->data;
2164 EVP_PKEY *pkey = NULL;
2165
2166 t->err = NULL;
2167 if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) {
2168 t->err = "KEYGEN_GENERATE_ERROR";
2169 goto err;
2170 }
2171
2172 if (keygen->keyname != NULL) {
2173 KEY_LIST *key;
2174
2175 if (find_key(NULL, keygen->keyname, private_keys)) {
2176 TEST_info("Duplicate key %s", keygen->keyname);
2177 goto err;
2178 }
2179
2180 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
2181 goto err;
2182 key->name = keygen->keyname;
2183 keygen->keyname = NULL;
2184 key->key = pkey;
2185 key->next = private_keys;
2186 private_keys = key;
2187 } else {
2188 EVP_PKEY_free(pkey);
2189 }
2190
2191 return 1;
2192
2193 err:
2194 EVP_PKEY_free(pkey);
2195 return 0;
2196 }
2197
2198 static const EVP_TEST_METHOD keygen_test_method = {
2199 "KeyGen",
2200 keygen_test_init,
2201 keygen_test_cleanup,
2202 keygen_test_parse,
2203 keygen_test_run,
2204 };
2205
2206 /**
2207 *** DIGEST SIGN+VERIFY TESTS
2208 **/
2209
2210 typedef struct {
2211 int is_verify; /* Set to 1 if verifying */
2212 int is_oneshot; /* Set to 1 for one shot operation */
2213 const EVP_MD *md; /* Digest to use */
2214 EVP_MD_CTX *ctx; /* Digest context */
2215 EVP_PKEY_CTX *pctx;
2216 STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
2217 unsigned char *osin; /* Input data if one shot */
2218 size_t osin_len; /* Input length data if one shot */
2219 unsigned char *output; /* Expected output */
2220 size_t output_len; /* Expected output length */
2221 } DIGESTSIGN_DATA;
2222
2223 static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
2224 int is_oneshot)
2225 {
2226 const EVP_MD *md = NULL;
2227 DIGESTSIGN_DATA *mdat;
2228
2229 if (strcmp(alg, "NULL") != 0) {
2230 if ((md = EVP_get_digestbyname(alg)) == NULL) {
2231 /* If alg has an OID assume disabled algorithm */
2232 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
2233 t->skip = 1;
2234 return 1;
2235 }
2236 return 0;
2237 }
2238 }
2239 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
2240 return 0;
2241 mdat->md = md;
2242 if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
2243 OPENSSL_free(mdat);
2244 return 0;
2245 }
2246 mdat->is_verify = is_verify;
2247 mdat->is_oneshot = is_oneshot;
2248 t->data = mdat;
2249 return 1;
2250 }
2251
2252 static int digestsign_test_init(EVP_TEST *t, const char *alg)
2253 {
2254 return digestsigver_test_init(t, alg, 0, 0);
2255 }
2256
2257 static void digestsigver_test_cleanup(EVP_TEST *t)
2258 {
2259 DIGESTSIGN_DATA *mdata = t->data;
2260
2261 EVP_MD_CTX_free(mdata->ctx);
2262 sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
2263 OPENSSL_free(mdata->osin);
2264 OPENSSL_free(mdata->output);
2265 OPENSSL_free(mdata);
2266 t->data = NULL;
2267 }
2268
2269 static int digestsigver_test_parse(EVP_TEST *t,
2270 const char *keyword, const char *value)
2271 {
2272 DIGESTSIGN_DATA *mdata = t->data;
2273
2274 if (strcmp(keyword, "Key") == 0) {
2275 EVP_PKEY *pkey = NULL;
2276 int rv = 0;
2277
2278 if (mdata->is_verify)
2279 rv = find_key(&pkey, value, public_keys);
2280 if (rv == 0)
2281 rv = find_key(&pkey, value, private_keys);
2282 if (rv == 0 || pkey == NULL) {
2283 t->skip = 1;
2284 return 1;
2285 }
2286 if (mdata->is_verify) {
2287 if (!EVP_DigestVerifyInit(mdata->ctx, &mdata->pctx, mdata->md,
2288 NULL, pkey))
2289 t->err = "DIGESTVERIFYINIT_ERROR";
2290 return 1;
2291 }
2292 if (!EVP_DigestSignInit(mdata->ctx, &mdata->pctx, mdata->md, NULL,
2293 pkey))
2294 t->err = "DIGESTSIGNINIT_ERROR";
2295 return 1;
2296 }
2297
2298 if (strcmp(keyword, "Input") == 0) {
2299 if (mdata->is_oneshot)
2300 return parse_bin(value, &mdata->osin, &mdata->osin_len);
2301 return evp_test_buffer_append(value, &mdata->input);
2302 }
2303 if (strcmp(keyword, "Output") == 0)
2304 return parse_bin(value, &mdata->output, &mdata->output_len);
2305
2306 if (!mdata->is_oneshot) {
2307 if (strcmp(keyword, "Count") == 0)
2308 return evp_test_buffer_set_count(value, mdata->input);
2309 if (strcmp(keyword, "Ncopy") == 0)
2310 return evp_test_buffer_ncopy(value, mdata->input);
2311 }
2312 if (strcmp(keyword, "Ctrl") == 0) {
2313 if (mdata->pctx == NULL)
2314 return 0;
2315 return pkey_test_ctrl(t, mdata->pctx, value);
2316 }
2317 return 0;
2318 }
2319
2320 static int digestsign_update_fn(void *ctx, const unsigned char *buf,
2321 size_t buflen)
2322 {
2323 return EVP_DigestSignUpdate(ctx, buf, buflen);
2324 }
2325
2326 static int digestsign_test_run(EVP_TEST *t)
2327 {
2328 DIGESTSIGN_DATA *expected = t->data;
2329 unsigned char *got = NULL;
2330 size_t got_len;
2331
2332 if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
2333 expected->ctx)) {
2334 t->err = "DIGESTUPDATE_ERROR";
2335 goto err;
2336 }
2337
2338 if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
2339 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
2340 goto err;
2341 }
2342 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2343 t->err = "MALLOC_FAILURE";
2344 goto err;
2345 }
2346 if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
2347 t->err = "DIGESTSIGNFINAL_ERROR";
2348 goto err;
2349 }
2350 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
2351 expected->output, expected->output_len,
2352 got, got_len))
2353 goto err;
2354
2355 t->err = NULL;
2356 err:
2357 OPENSSL_free(got);
2358 return 1;
2359 }
2360
2361 static const EVP_TEST_METHOD digestsign_test_method = {
2362 "DigestSign",
2363 digestsign_test_init,
2364 digestsigver_test_cleanup,
2365 digestsigver_test_parse,
2366 digestsign_test_run
2367 };
2368
2369 static int digestverify_test_init(EVP_TEST *t, const char *alg)
2370 {
2371 return digestsigver_test_init(t, alg, 1, 0);
2372 }
2373
2374 static int digestverify_update_fn(void *ctx, const unsigned char *buf,
2375 size_t buflen)
2376 {
2377 return EVP_DigestVerifyUpdate(ctx, buf, buflen);
2378 }
2379
2380 static int digestverify_test_run(EVP_TEST *t)
2381 {
2382 DIGESTSIGN_DATA *mdata = t->data;
2383
2384 if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
2385 t->err = "DIGESTUPDATE_ERROR";
2386 return 1;
2387 }
2388
2389 if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
2390 mdata->output_len) <= 0)
2391 t->err = "VERIFY_ERROR";
2392 return 1;
2393 }
2394
2395 static const EVP_TEST_METHOD digestverify_test_method = {
2396 "DigestVerify",
2397 digestverify_test_init,
2398 digestsigver_test_cleanup,
2399 digestsigver_test_parse,
2400 digestverify_test_run
2401 };
2402
2403 static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
2404 {
2405 return digestsigver_test_init(t, alg, 0, 1);
2406 }
2407
2408 static int oneshot_digestsign_test_run(EVP_TEST *t)
2409 {
2410 DIGESTSIGN_DATA *expected = t->data;
2411 unsigned char *got = NULL;
2412 size_t got_len;
2413
2414 if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
2415 expected->osin, expected->osin_len)) {
2416 t->err = "DIGESTSIGN_LENGTH_ERROR";
2417 goto err;
2418 }
2419 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2420 t->err = "MALLOC_FAILURE";
2421 goto err;
2422 }
2423 if (!EVP_DigestSign(expected->ctx, got, &got_len,
2424 expected->osin, expected->osin_len)) {
2425 t->err = "DIGESTSIGN_ERROR";
2426 goto err;
2427 }
2428 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
2429 expected->output, expected->output_len,
2430 got, got_len))
2431 goto err;
2432
2433 t->err = NULL;
2434 err:
2435 OPENSSL_free(got);
2436 return 1;
2437 }
2438
2439 static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
2440 "OneShotDigestSign",
2441 oneshot_digestsign_test_init,
2442 digestsigver_test_cleanup,
2443 digestsigver_test_parse,
2444 oneshot_digestsign_test_run
2445 };
2446
2447 static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
2448 {
2449 return digestsigver_test_init(t, alg, 1, 1);
2450 }
2451
2452 static int oneshot_digestverify_test_run(EVP_TEST *t)
2453 {
2454 DIGESTSIGN_DATA *mdata = t->data;
2455
2456 if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
2457 mdata->osin, mdata->osin_len) <= 0)
2458 t->err = "VERIFY_ERROR";
2459 return 1;
2460 }
2461
2462 static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
2463 "OneShotDigestVerify",
2464 oneshot_digestverify_test_init,
2465 digestsigver_test_cleanup,
2466 digestsigver_test_parse,
2467 oneshot_digestverify_test_run
2468 };
2469
2470
2471 /**
2472 *** PARSING AND DISPATCH
2473 **/
2474
2475 static const EVP_TEST_METHOD *evp_test_list[] = {
2476 &cipher_test_method,
2477 &digest_test_method,
2478 &digestsign_test_method,
2479 &digestverify_test_method,
2480 &encode_test_method,
2481 &kdf_test_method,
2482 &keypair_test_method,
2483 &keygen_test_method,
2484 &mac_test_method,
2485 &oneshot_digestsign_test_method,
2486 &oneshot_digestverify_test_method,
2487 &pbe_test_method,
2488 &pdecrypt_test_method,
2489 &pderive_test_method,
2490 &psign_test_method,
2491 &pverify_recover_test_method,
2492 &pverify_test_method,
2493 NULL
2494 };
2495
2496 static const EVP_TEST_METHOD *find_test(const char *name)
2497 {
2498 const EVP_TEST_METHOD **tt;
2499
2500 for (tt = evp_test_list; *tt; tt++) {
2501 if (strcmp(name, (*tt)->name) == 0)
2502 return *tt;
2503 }
2504 return NULL;
2505 }
2506
2507 static void clear_test(EVP_TEST *t)
2508 {
2509 test_clearstanza(&t->s);
2510 ERR_clear_error();
2511 if (t->data != NULL) {
2512 if (t->meth != NULL)
2513 t->meth->cleanup(t);
2514 OPENSSL_free(t->data);
2515 t->data = NULL;
2516 }
2517 OPENSSL_free(t->expected_err);
2518 t->expected_err = NULL;
2519 OPENSSL_free(t->func);
2520 t->func = NULL;
2521 OPENSSL_free(t->reason);
2522 t->reason = NULL;
2523
2524 /* Text literal. */
2525 t->err = NULL;
2526 t->skip = 0;
2527 t->meth = NULL;
2528 }
2529
2530 /*
2531 * Check for errors in the test structure; return 1 if okay, else 0.
2532 */
2533 static int check_test_error(EVP_TEST *t)
2534 {
2535 unsigned long err;
2536 const char *func;
2537 const char *reason;
2538
2539 if (t->err == NULL && t->expected_err == NULL)
2540 return 1;
2541 if (t->err != NULL && t->expected_err == NULL) {
2542 if (t->aux_err != NULL) {
2543 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
2544 t->s.test_file, t->s.start, t->aux_err, t->err);
2545 } else {
2546 TEST_info("%s:%d: Source of above error; unexpected error %s",
2547 t->s.test_file, t->s.start, t->err);
2548 }
2549 return 0;
2550 }
2551 if (t->err == NULL && t->expected_err != NULL) {
2552 TEST_info("%s:%d: Succeeded but was expecting %s",
2553 t->s.test_file, t->s.start, t->expected_err);
2554 return 0;
2555 }
2556
2557 if (strcmp(t->err, t->expected_err) != 0) {
2558 TEST_info("%s:%d: Expected %s got %s",
2559 t->s.test_file, t->s.start, t->expected_err, t->err);
2560 return 0;
2561 }
2562
2563 if (t->func == NULL && t->reason == NULL)
2564 return 1;
2565
2566 if (t->func == NULL || t->reason == NULL) {
2567 TEST_info("%s:%d: Test is missing function or reason code",
2568 t->s.test_file, t->s.start);
2569 return 0;
2570 }
2571
2572 err = ERR_peek_error();
2573 if (err == 0) {
2574 TEST_info("%s:%d: Expected error \"%s:%s\" not set",
2575 t->s.test_file, t->s.start, t->func, t->reason);
2576 return 0;
2577 }
2578
2579 func = ERR_func_error_string(err);
2580 reason = ERR_reason_error_string(err);
2581 if (func == NULL && reason == NULL) {
2582 TEST_info("%s:%d: Expected error \"%s:%s\", no strings available."
2583 " Assuming ok.",
2584 t->s.test_file, t->s.start, t->func, t->reason);
2585 return 1;
2586 }
2587
2588 if (strcmp(func, t->func) == 0 && strcmp(reason, t->reason) == 0)
2589 return 1;
2590
2591 TEST_info("%s:%d: Expected error \"%s:%s\", got \"%s:%s\"",
2592 t->s.test_file, t->s.start, t->func, t->reason, func, reason);
2593
2594 return 0;
2595 }
2596
2597 /*
2598 * Run a parsed test. Log a message and return 0 on error.
2599 */
2600 static int run_test(EVP_TEST *t)
2601 {
2602 if (t->meth == NULL)
2603 return 1;
2604 t->s.numtests++;
2605 if (t->skip) {
2606 t->s.numskip++;
2607 } else {
2608 /* run the test */
2609 if (t->err == NULL && t->meth->run_test(t) != 1) {
2610 TEST_info("%s:%d %s error",
2611 t->s.test_file, t->s.start, t->meth->name);
2612 return 0;
2613 }
2614 if (!check_test_error(t)) {
2615 TEST_openssl_errors();
2616 t->s.errors++;
2617 }
2618 }
2619
2620 /* clean it up */
2621 return 1;
2622 }
2623
2624 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
2625 {
2626 for (; lst != NULL; lst = lst->next) {
2627 if (strcmp(lst->name, name) == 0) {
2628 if (ppk != NULL)
2629 *ppk = lst->key;
2630 return 1;
2631 }
2632 }
2633 return 0;
2634 }
2635
2636 static void free_key_list(KEY_LIST *lst)
2637 {
2638 while (lst != NULL) {
2639 KEY_LIST *next = lst->next;
2640
2641 EVP_PKEY_free(lst->key);
2642 OPENSSL_free(lst->name);
2643 OPENSSL_free(lst);
2644 lst = next;
2645 }
2646 }
2647
2648 /*
2649 * Is the key type an unsupported algorithm?
2650 */
2651 static int key_unsupported(void)
2652 {
2653 long err = ERR_peek_error();
2654
2655 if (ERR_GET_LIB(err) == ERR_LIB_EVP
2656 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
2657 ERR_clear_error();
2658 return 1;
2659 }
2660 #ifndef OPENSSL_NO_EC
2661 /*
2662 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
2663 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
2664 * disabled).
2665 */
2666 if (ERR_GET_LIB(err) == ERR_LIB_EC
2667 && ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP) {
2668 ERR_clear_error();
2669 return 1;
2670 }
2671 #endif /* OPENSSL_NO_EC */
2672 return 0;
2673 }
2674
2675 /*
2676 * NULL out the value from |pp| but return it. This "steals" a pointer.
2677 */
2678 static char *take_value(PAIR *pp)
2679 {
2680 char *p = pp->value;
2681
2682 pp->value = NULL;
2683 return p;
2684 }
2685
2686 /*
2687 * Read and parse one test. Return 0 if failure, 1 if okay.
2688 */
2689 static int parse(EVP_TEST *t)
2690 {
2691 KEY_LIST *key, **klist;
2692 EVP_PKEY *pkey;
2693 PAIR *pp;
2694 int i;
2695
2696 top:
2697 do {
2698 if (BIO_eof(t->s.fp))
2699 return EOF;
2700 clear_test(t);
2701 if (!test_readstanza(&t->s))
2702 return 0;
2703 } while (t->s.numpairs == 0);
2704 pp = &t->s.pairs[0];
2705
2706 /* Are we adding a key? */
2707 klist = NULL;
2708 pkey = NULL;
2709 if (strcmp(pp->key, "PrivateKey") == 0) {
2710 pkey = PEM_read_bio_PrivateKey(t->s.key, NULL, 0, NULL);
2711 if (pkey == NULL && !key_unsupported()) {
2712 EVP_PKEY_free(pkey);
2713 TEST_info("Can't read private key %s", pp->value);
2714 TEST_openssl_errors();
2715 return 0;
2716 }
2717 klist = &private_keys;
2718 } else if (strcmp(pp->key, "PublicKey") == 0) {
2719 pkey = PEM_read_bio_PUBKEY(t->s.key, NULL, 0, NULL);
2720 if (pkey == NULL && !key_unsupported()) {
2721 EVP_PKEY_free(pkey);
2722 TEST_info("Can't read public key %s", pp->value);
2723 TEST_openssl_errors();
2724 return 0;
2725 }
2726 klist = &public_keys;
2727 } else if (strcmp(pp->key, "PrivateKeyRaw") == 0
2728 || strcmp(pp->key, "PublicKeyRaw") == 0 ) {
2729 char *strnid = NULL, *keydata = NULL;
2730 unsigned char *keybin;
2731 size_t keylen;
2732 int nid;
2733
2734 if (strcmp(pp->key, "PrivateKeyRaw") == 0)
2735 klist = &private_keys;
2736 else
2737 klist = &public_keys;
2738
2739 strnid = strchr(pp->value, ':');
2740 if (strnid != NULL) {
2741 *strnid++ = '\0';
2742 keydata = strchr(strnid, ':');
2743 if (keydata != NULL)
2744 *keydata++ = '\0';
2745 }
2746 if (keydata == NULL) {
2747 TEST_info("Failed to parse %s value", pp->key);
2748 return 0;
2749 }
2750
2751 nid = OBJ_txt2nid(strnid);
2752 if (nid == NID_undef) {
2753 TEST_info("Uncrecognised algorithm NID");
2754 return 0;
2755 }
2756 if (!parse_bin(keydata, &keybin, &keylen)) {
2757 TEST_info("Failed to create binary key");
2758 return 0;
2759 }
2760 if (klist == &private_keys)
2761 pkey = EVP_PKEY_new_raw_private_key(nid, NULL, keybin, keylen);
2762 else
2763 pkey = EVP_PKEY_new_raw_public_key(nid, NULL, keybin, keylen);
2764 if (pkey == NULL && !key_unsupported()) {
2765 TEST_info("Can't read %s data", pp->key);
2766 OPENSSL_free(keybin);
2767 TEST_openssl_errors();
2768 return 0;
2769 }
2770 OPENSSL_free(keybin);
2771 }
2772
2773 /* If we have a key add to list */
2774 if (klist != NULL) {
2775 if (find_key(NULL, pp->value, *klist)) {
2776 TEST_info("Duplicate key %s", pp->value);
2777 return 0;
2778 }
2779 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
2780 return 0;
2781 key->name = take_value(pp);
2782
2783 /* Hack to detect SM2 keys */
2784 if(pkey != NULL && strstr(key->name, "SM2") != NULL) {
2785 #ifdef OPENSSL_NO_SM2
2786 EVP_PKEY_free(pkey);
2787 pkey = NULL;
2788 #else
2789 EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2);
2790 #endif
2791 }
2792
2793 key->key = pkey;
2794 key->next = *klist;
2795 *klist = key;
2796
2797 /* Go back and start a new stanza. */
2798 if (t->s.numpairs != 1)
2799 TEST_info("Line %d: missing blank line\n", t->s.curr);
2800 goto top;
2801 }
2802
2803 /* Find the test, based on first keyword. */
2804 if (!TEST_ptr(t->meth = find_test(pp->key)))
2805 return 0;
2806 if (!t->meth->init(t, pp->value)) {
2807 TEST_error("unknown %s: %s\n", pp->key, pp->value);
2808 return 0;
2809 }
2810 if (t->skip == 1) {
2811 /* TEST_info("skipping %s %s", pp->key, pp->value); */
2812 return 0;
2813 }
2814
2815 for (pp++, i = 1; i < t->s.numpairs; pp++, i++) {
2816 if (strcmp(pp->key, "Result") == 0) {
2817 if (t->expected_err != NULL) {
2818 TEST_info("Line %d: multiple result lines", t->s.curr);
2819 return 0;
2820 }
2821 t->expected_err = take_value(pp);
2822 } else if (strcmp(pp->key, "Function") == 0) {
2823 if (t->func != NULL) {
2824 TEST_info("Line %d: multiple function lines\n", t->s.curr);
2825 return 0;
2826 }
2827 t->func = take_value(pp);
2828 } else if (strcmp(pp->key, "Reason") == 0) {
2829 if (t->reason != NULL) {
2830 TEST_info("Line %d: multiple reason lines", t->s.curr);
2831 return 0;
2832 }
2833 t->reason = take_value(pp);
2834 } else {
2835 /* Must be test specific line: try to parse it */
2836 int rv = t->meth->parse(t, pp->key, pp->value);
2837
2838 if (rv == 0) {
2839 TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key);
2840 return 0;
2841 }
2842 if (rv < 0) {
2843 TEST_info("Line %d: error processing keyword %s = %s\n",
2844 t->s.curr, pp->key, pp->value);
2845 return 0;
2846 }
2847 }
2848 }
2849
2850 return 1;
2851 }
2852
2853 static int run_file_tests(int i)
2854 {
2855 EVP_TEST *t;
2856 const char *testfile = test_get_argument(i);
2857 int c;
2858
2859 if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t))))
2860 return 0;
2861 if (!test_start_file(&t->s, testfile)) {
2862 OPENSSL_free(t);
2863 return 0;
2864 }
2865
2866 while (!BIO_eof(t->s.fp)) {
2867 c = parse(t);
2868 if (t->skip)
2869 continue;
2870 if (c == 0 || !run_test(t)) {
2871 t->s.errors++;
2872 break;
2873 }
2874 }
2875 test_end_file(&t->s);
2876 clear_test(t);
2877
2878 free_key_list(public_keys);
2879 free_key_list(private_keys);
2880 BIO_free(t->s.key);
2881 c = t->s.errors;
2882 OPENSSL_free(t);
2883 return c == 0;
2884 }
2885
2886 int setup_tests(void)
2887 {
2888 size_t n = test_get_argument_count();
2889
2890 if (n == 0) {
2891 TEST_error("Usage: %s file...", test_get_program_name());
2892 return 0;
2893 }
2894
2895 ADD_ALL_TESTS(run_file_tests, n);
2896 return 1;
2897 }