2 * Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
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
14 #include "../e_os.h" /* strcasecmp */
15 #include <openssl/evp.h>
16 #include <openssl/pem.h>
17 #include <openssl/err.h>
18 #include <openssl/provider.h>
19 #include <openssl/x509v3.h>
20 #include <openssl/pkcs12.h>
21 #include <openssl/kdf.h>
22 #include <openssl/params.h>
23 #include <openssl/core_names.h>
24 #include <openssl/fips_names.h>
25 #include "internal/numbers.h"
26 #include "internal/nelem.h"
27 #include "crypto/evp.h"
33 typedef struct evp_test_method_st EVP_TEST_METHOD;
35 /* Structure holding test information */
36 typedef struct evp_test_st {
37 STANZA s; /* Common test stanza */
39 int skip; /* Current test should be skipped */
40 const EVP_TEST_METHOD *meth; /* method for this test */
41 const char *err, *aux_err; /* Error string for test */
42 char *expected_err; /* Expected error value of test */
43 char *reason; /* Expected error reason string */
44 void *data; /* test specific data */
47 /* Test method structure */
48 struct evp_test_method_st {
49 /* Name of test as it appears in file */
51 /* Initialise test for "alg" */
52 int (*init) (EVP_TEST * t, const char *alg);
54 void (*cleanup) (EVP_TEST * t);
55 /* Test specific name value pair processing */
56 int (*parse) (EVP_TEST * t, const char *name, const char *value);
57 /* Run the test itself */
58 int (*run_test) (EVP_TEST * t);
61 /* Linked list of named keys. */
62 typedef struct key_list_st {
65 struct key_list_st *next;
68 typedef enum OPTION_choice {
75 static OSSL_PROVIDER *prov_null = NULL;
76 static OSSL_LIB_CTX *libctx = NULL;
78 /* List of public and private keys */
79 static KEY_LIST *private_keys;
80 static KEY_LIST *public_keys;
82 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst);
83 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen);
84 static int is_digest_disabled(const char *name);
85 static int is_pkey_disabled(const char *name);
86 static int is_mac_disabled(const char *name);
87 static int is_cipher_disabled(const char *name);
88 static int is_kdf_disabled(const char *name);
91 * Compare two memory regions for equality, returning zero if they differ.
92 * However, if there is expected to be an error and the actual error
93 * matches then the memory is expected to be different so handle this
94 * case without producing unnecessary test framework output.
96 static int memory_err_compare(EVP_TEST *t, const char *err,
97 const void *expected, size_t expected_len,
98 const void *got, size_t got_len)
102 if (t->expected_err != NULL && strcmp(t->expected_err, err) == 0)
103 r = !TEST_mem_ne(expected, expected_len, got, got_len);
105 r = TEST_mem_eq(expected, expected_len, got, got_len);
112 * Structure used to hold a list of blocks of memory to test
113 * calls to "update" like functions.
115 struct evp_test_buffer_st {
122 static void evp_test_buffer_free(EVP_TEST_BUFFER *db)
125 OPENSSL_free(db->buf);
130 /* append buffer to a list */
131 static int evp_test_buffer_append(const char *value,
132 STACK_OF(EVP_TEST_BUFFER) **sk)
134 EVP_TEST_BUFFER *db = NULL;
136 if (!TEST_ptr(db = OPENSSL_malloc(sizeof(*db))))
139 if (!parse_bin(value, &db->buf, &db->buflen))
144 if (*sk == NULL && !TEST_ptr(*sk = sk_EVP_TEST_BUFFER_new_null()))
146 if (!sk_EVP_TEST_BUFFER_push(*sk, db))
152 evp_test_buffer_free(db);
156 /* replace last buffer in list with copies of itself */
157 static int evp_test_buffer_ncopy(const char *value,
158 STACK_OF(EVP_TEST_BUFFER) *sk)
161 unsigned char *tbuf, *p;
163 int ncopy = atoi(value);
168 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
170 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
172 tbuflen = db->buflen * ncopy;
173 if (!TEST_ptr(tbuf = OPENSSL_malloc(tbuflen)))
175 for (i = 0, p = tbuf; i < ncopy; i++, p += db->buflen)
176 memcpy(p, db->buf, db->buflen);
178 OPENSSL_free(db->buf);
180 db->buflen = tbuflen;
184 /* set repeat count for last buffer in list */
185 static int evp_test_buffer_set_count(const char *value,
186 STACK_OF(EVP_TEST_BUFFER) *sk)
189 int count = atoi(value);
194 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
197 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
198 if (db->count_set != 0)
201 db->count = (size_t)count;
206 /* call "fn" with each element of the list in turn */
207 static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER) *sk,
209 const unsigned char *buf,
215 for (i = 0; i < sk_EVP_TEST_BUFFER_num(sk); i++) {
216 EVP_TEST_BUFFER *tb = sk_EVP_TEST_BUFFER_value(sk, i);
219 for (j = 0; j < tb->count; j++) {
220 if (fn(ctx, tb->buf, tb->buflen) <= 0)
228 * Unescape some sequences in string literals (only \n for now).
229 * Return an allocated buffer, set |out_len|. If |input_len|
230 * is zero, get an empty buffer but set length to zero.
232 static unsigned char* unescape(const char *input, size_t input_len,
235 unsigned char *ret, *p;
238 if (input_len == 0) {
240 return OPENSSL_zalloc(1);
243 /* Escaping is non-expanding; over-allocate original size for simplicity. */
244 if (!TEST_ptr(ret = p = OPENSSL_malloc(input_len)))
247 for (i = 0; i < input_len; i++) {
248 if (*input == '\\') {
249 if (i == input_len - 1 || *++input != 'n') {
250 TEST_error("Bad escape sequence in file");
270 * For a hex string "value" convert to a binary allocated buffer.
271 * Return 1 on success or 0 on failure.
273 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen)
277 /* Check for NULL literal */
278 if (strcmp(value, "NULL") == 0) {
284 /* Check for empty value */
285 if (*value == '\0') {
287 * Don't return NULL for zero length buffer. This is needed for
288 * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key
289 * buffer even if the key length is 0, in order to detect key reset.
291 *buf = OPENSSL_malloc(1);
299 /* Check for string literal */
300 if (value[0] == '"') {
301 size_t vlen = strlen(++value);
303 if (vlen == 0 || value[vlen - 1] != '"')
306 *buf = unescape(value, vlen, buflen);
307 return *buf == NULL ? 0 : 1;
310 /* Otherwise assume as hex literal and convert it to binary buffer */
311 if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) {
312 TEST_info("Can't convert %s", value);
313 TEST_openssl_errors();
316 /* Size of input buffer means we'll never overflow */
322 ** MESSAGE DIGEST TESTS
325 typedef struct digest_data_st {
326 /* Digest this test is for */
327 const EVP_MD *digest;
328 EVP_MD *fetched_digest;
329 /* Input to digest */
330 STACK_OF(EVP_TEST_BUFFER) *input;
331 /* Expected output */
332 unsigned char *output;
338 static int digest_test_init(EVP_TEST *t, const char *alg)
341 const EVP_MD *digest;
342 EVP_MD *fetched_digest;
344 if (is_digest_disabled(alg)) {
345 TEST_info("skipping, '%s' is disabled", alg);
350 if ((digest = fetched_digest = EVP_MD_fetch(libctx, alg, NULL)) == NULL
351 && (digest = EVP_get_digestbyname(alg)) == NULL)
353 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
356 mdat->digest = digest;
357 mdat->fetched_digest = fetched_digest;
359 if (fetched_digest != NULL)
360 TEST_info("%s is fetched", alg);
364 static void digest_test_cleanup(EVP_TEST *t)
366 DIGEST_DATA *mdat = t->data;
368 sk_EVP_TEST_BUFFER_pop_free(mdat->input, evp_test_buffer_free);
369 OPENSSL_free(mdat->output);
370 EVP_MD_free(mdat->fetched_digest);
373 static int digest_test_parse(EVP_TEST *t,
374 const char *keyword, const char *value)
376 DIGEST_DATA *mdata = t->data;
378 if (strcmp(keyword, "Input") == 0)
379 return evp_test_buffer_append(value, &mdata->input);
380 if (strcmp(keyword, "Output") == 0)
381 return parse_bin(value, &mdata->output, &mdata->output_len);
382 if (strcmp(keyword, "Count") == 0)
383 return evp_test_buffer_set_count(value, mdata->input);
384 if (strcmp(keyword, "Ncopy") == 0)
385 return evp_test_buffer_ncopy(value, mdata->input);
386 if (strcmp(keyword, "Padding") == 0)
387 return (mdata->pad_type = atoi(value)) > 0;
391 static int digest_update_fn(void *ctx, const unsigned char *buf, size_t buflen)
393 return EVP_DigestUpdate(ctx, buf, buflen);
396 static int digest_test_run(EVP_TEST *t)
398 DIGEST_DATA *expected = t->data;
400 unsigned char *got = NULL;
401 unsigned int got_len;
402 OSSL_PARAM params[2];
404 t->err = "TEST_FAILURE";
405 if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
408 got = OPENSSL_malloc(expected->output_len > EVP_MAX_MD_SIZE ?
409 expected->output_len : EVP_MAX_MD_SIZE);
413 if (!EVP_DigestInit_ex(mctx, expected->digest, NULL)) {
414 t->err = "DIGESTINIT_ERROR";
417 if (expected->pad_type > 0) {
418 params[0] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_PAD_TYPE,
419 &expected->pad_type);
420 params[1] = OSSL_PARAM_construct_end();
421 if (!TEST_int_gt(EVP_MD_CTX_set_params(mctx, params), 0)) {
422 t->err = "PARAMS_ERROR";
426 if (!evp_test_buffer_do(expected->input, digest_update_fn, mctx)) {
427 t->err = "DIGESTUPDATE_ERROR";
431 if (EVP_MD_flags(expected->digest) & EVP_MD_FLAG_XOF) {
432 EVP_MD_CTX *mctx_cpy;
433 char dont[] = "touch";
435 if (!TEST_ptr(mctx_cpy = EVP_MD_CTX_new())) {
438 if (!EVP_MD_CTX_copy(mctx_cpy, mctx)) {
439 EVP_MD_CTX_free(mctx_cpy);
442 if (!EVP_DigestFinalXOF(mctx_cpy, (unsigned char *)dont, 0)) {
443 EVP_MD_CTX_free(mctx_cpy);
444 t->err = "DIGESTFINALXOF_ERROR";
447 if (!TEST_str_eq(dont, "touch")) {
448 EVP_MD_CTX_free(mctx_cpy);
449 t->err = "DIGESTFINALXOF_ERROR";
452 EVP_MD_CTX_free(mctx_cpy);
454 got_len = expected->output_len;
455 if (!EVP_DigestFinalXOF(mctx, got, got_len)) {
456 t->err = "DIGESTFINALXOF_ERROR";
460 if (!EVP_DigestFinal(mctx, got, &got_len)) {
461 t->err = "DIGESTFINAL_ERROR";
465 if (!TEST_int_eq(expected->output_len, got_len)) {
466 t->err = "DIGEST_LENGTH_MISMATCH";
469 if (!memory_err_compare(t, "DIGEST_MISMATCH",
470 expected->output, expected->output_len,
478 EVP_MD_CTX_free(mctx);
482 static const EVP_TEST_METHOD digest_test_method = {
494 typedef struct cipher_data_st {
495 const EVP_CIPHER *cipher;
496 EVP_CIPHER *fetched_cipher;
498 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
502 size_t key_bits; /* Used by RC2 */
506 unsigned char *plaintext;
507 size_t plaintext_len;
508 unsigned char *ciphertext;
509 size_t ciphertext_len;
510 /* GCM, CCM, OCB and SIV only */
511 unsigned char *aad[AAD_NUM];
512 size_t aad_len[AAD_NUM];
514 const char *cts_mode;
519 static int cipher_test_init(EVP_TEST *t, const char *alg)
521 const EVP_CIPHER *cipher;
522 EVP_CIPHER *fetched_cipher;
526 if (is_cipher_disabled(alg)) {
528 TEST_info("skipping, '%s' is disabled", alg);
532 if ((cipher = fetched_cipher = EVP_CIPHER_fetch(libctx, alg, NULL)) == NULL
533 && (cipher = EVP_get_cipherbyname(alg)) == NULL)
536 cdat = OPENSSL_zalloc(sizeof(*cdat));
537 cdat->cipher = cipher;
538 cdat->fetched_cipher = fetched_cipher;
540 m = EVP_CIPHER_mode(cipher);
541 if (m == EVP_CIPH_GCM_MODE
542 || m == EVP_CIPH_OCB_MODE
543 || m == EVP_CIPH_SIV_MODE
544 || m == EVP_CIPH_CCM_MODE)
546 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
552 if (fetched_cipher != NULL)
553 TEST_info("%s is fetched", alg);
557 static void cipher_test_cleanup(EVP_TEST *t)
560 CIPHER_DATA *cdat = t->data;
562 OPENSSL_free(cdat->key);
563 OPENSSL_free(cdat->iv);
564 OPENSSL_free(cdat->ciphertext);
565 OPENSSL_free(cdat->plaintext);
566 for (i = 0; i < AAD_NUM; i++)
567 OPENSSL_free(cdat->aad[i]);
568 OPENSSL_free(cdat->tag);
569 EVP_CIPHER_free(cdat->fetched_cipher);
572 static int cipher_test_parse(EVP_TEST *t, const char *keyword,
575 CIPHER_DATA *cdat = t->data;
578 if (strcmp(keyword, "Key") == 0)
579 return parse_bin(value, &cdat->key, &cdat->key_len);
580 if (strcmp(keyword, "Rounds") == 0) {
584 cdat->rounds = (unsigned int)i;
587 if (strcmp(keyword, "IV") == 0)
588 return parse_bin(value, &cdat->iv, &cdat->iv_len);
589 if (strcmp(keyword, "Plaintext") == 0)
590 return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len);
591 if (strcmp(keyword, "Ciphertext") == 0)
592 return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
593 if (strcmp(keyword, "KeyBits") == 0) {
597 cdat->key_bits = (size_t)i;
601 if (strcmp(keyword, "AAD") == 0) {
602 for (i = 0; i < AAD_NUM; i++) {
603 if (cdat->aad[i] == NULL)
604 return parse_bin(value, &cdat->aad[i], &cdat->aad_len[i]);
608 if (strcmp(keyword, "Tag") == 0)
609 return parse_bin(value, &cdat->tag, &cdat->tag_len);
610 if (strcmp(keyword, "SetTagLate") == 0) {
611 if (strcmp(value, "TRUE") == 0)
613 else if (strcmp(value, "FALSE") == 0)
621 if (strcmp(keyword, "Operation") == 0) {
622 if (strcmp(value, "ENCRYPT") == 0)
624 else if (strcmp(value, "DECRYPT") == 0)
630 if (strcmp(keyword, "CTSMode") == 0) {
631 cdat->cts_mode = value;
637 static int cipher_test_enc(EVP_TEST *t, int enc,
638 size_t out_misalign, size_t inp_misalign, int frag)
640 CIPHER_DATA *expected = t->data;
641 unsigned char *in, *expected_out, *tmp = NULL;
642 size_t in_len, out_len, donelen = 0;
643 int ok = 0, tmplen, chunklen, tmpflen, i;
644 EVP_CIPHER_CTX *ctx_base = NULL;
645 EVP_CIPHER_CTX *ctx = NULL;
647 t->err = "TEST_FAILURE";
648 if (!TEST_ptr(ctx_base = EVP_CIPHER_CTX_new()))
650 if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
652 EVP_CIPHER_CTX_set_flags(ctx_base, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
654 in = expected->plaintext;
655 in_len = expected->plaintext_len;
656 expected_out = expected->ciphertext;
657 out_len = expected->ciphertext_len;
659 in = expected->ciphertext;
660 in_len = expected->ciphertext_len;
661 expected_out = expected->plaintext;
662 out_len = expected->plaintext_len;
664 if (inp_misalign == (size_t)-1) {
665 /* Exercise in-place encryption */
666 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
669 in = memcpy(tmp + out_misalign, in, in_len);
671 inp_misalign += 16 - ((out_misalign + in_len) & 15);
673 * 'tmp' will store both output and copy of input. We make the copy
674 * of input to specifically aligned part of 'tmp'. So we just
675 * figured out how much padding would ensure the required alignment,
676 * now we allocate extended buffer and finally copy the input just
677 * past inp_misalign in expression below. Output will be written
678 * past out_misalign...
680 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
681 inp_misalign + in_len);
684 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
685 inp_misalign, in, in_len);
687 if (!EVP_CipherInit_ex(ctx_base, expected->cipher, NULL, NULL, NULL, enc)) {
688 t->err = "CIPHERINIT_ERROR";
691 if (expected->cts_mode != NULL) {
692 OSSL_PARAM params[2];
694 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE,
695 (char *)expected->cts_mode,
697 params[1] = OSSL_PARAM_construct_end();
698 if (!EVP_CIPHER_CTX_set_params(ctx_base, params)) {
699 t->err = "INVALID_CTS_MODE";
704 if (expected->aead) {
705 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_IVLEN,
706 expected->iv_len, 0)) {
707 t->err = "INVALID_IV_LENGTH";
710 } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx_base)) {
711 t->err = "INVALID_IV_LENGTH";
715 if (expected->aead) {
718 * If encrypting or OCB just set tag length initially, otherwise
719 * set tag length and value.
721 if (enc || expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late) {
722 t->err = "TAG_LENGTH_SET_ERROR";
725 t->err = "TAG_SET_ERROR";
728 if (tag || expected->aead != EVP_CIPH_GCM_MODE) {
729 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_TAG,
730 expected->tag_len, tag))
735 if (expected->rounds > 0) {
736 int rounds = (int)expected->rounds;
738 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC5_ROUNDS, rounds, NULL)) {
739 t->err = "INVALID_ROUNDS";
744 if (!EVP_CIPHER_CTX_set_key_length(ctx_base, expected->key_len)) {
745 t->err = "INVALID_KEY_LENGTH";
748 if (expected->key_bits > 0) {
749 int bits = (int)expected->key_bits;
751 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC2_KEY_BITS, bits, NULL)) {
752 t->err = "INVALID KEY BITS";
756 if (!EVP_CipherInit_ex(ctx_base, NULL, NULL, expected->key, expected->iv, -1)) {
757 t->err = "KEY_SET_ERROR";
761 /* Check that we get the same IV back */
762 if (expected->iv != NULL) {
763 /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
764 unsigned char iv[128];
765 if (!TEST_true(EVP_CIPHER_CTX_get_iv_state(ctx_base, iv, sizeof(iv)))
766 || ((EVP_CIPHER_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
767 && !TEST_mem_eq(expected->iv, expected->iv_len, iv,
768 expected->iv_len))) {
769 t->err = "INVALID_IV";
774 /* Test that the cipher dup functions correctly if it is supported */
775 if (EVP_CIPHER_CTX_copy(ctx, ctx_base)) {
776 EVP_CIPHER_CTX_free(ctx_base);
779 EVP_CIPHER_CTX_free(ctx);
783 if (expected->aead == EVP_CIPH_CCM_MODE) {
784 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
785 t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
789 if (expected->aad[0] != NULL) {
790 t->err = "AAD_SET_ERROR";
792 for (i = 0; expected->aad[i] != NULL; i++) {
793 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i],
794 expected->aad_len[i]))
799 * Supply the AAD in chunks less than the block size where possible
801 for (i = 0; expected->aad[i] != NULL; i++) {
802 if (expected->aad_len[i] > 0) {
803 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], 1))
807 if (expected->aad_len[i] > 2) {
808 if (!EVP_CipherUpdate(ctx, NULL, &chunklen,
809 expected->aad[i] + donelen,
810 expected->aad_len[i] - 2))
812 donelen += expected->aad_len[i] - 2;
814 if (expected->aad_len[i] > 1
815 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
816 expected->aad[i] + donelen, 1))
822 if (!enc && (expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late)) {
823 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
824 expected->tag_len, expected->tag)) {
825 t->err = "TAG_SET_ERROR";
830 EVP_CIPHER_CTX_set_padding(ctx, 0);
831 t->err = "CIPHERUPDATE_ERROR";
834 /* We supply the data all in one go */
835 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
838 /* Supply the data in chunks less than the block size where possible */
840 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
847 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
855 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
861 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
862 t->err = "CIPHERFINAL_ERROR";
865 if (!memory_err_compare(t, "VALUE_MISMATCH", expected_out, out_len,
866 tmp + out_misalign, tmplen + tmpflen))
868 if (enc && expected->aead) {
869 unsigned char rtag[16];
871 if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) {
872 t->err = "TAG_LENGTH_INTERNAL_ERROR";
875 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
876 expected->tag_len, rtag)) {
877 t->err = "TAG_RETRIEVE_ERROR";
880 if (!memory_err_compare(t, "TAG_VALUE_MISMATCH",
881 expected->tag, expected->tag_len,
882 rtag, expected->tag_len))
890 EVP_CIPHER_CTX_free(ctx_base);
891 EVP_CIPHER_CTX_free(ctx);
895 static int cipher_test_run(EVP_TEST *t)
897 CIPHER_DATA *cdat = t->data;
899 size_t out_misalign, inp_misalign;
905 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
906 /* IV is optional and usually omitted in wrap mode */
907 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
912 if (cdat->aead && !cdat->tag) {
916 for (out_misalign = 0; out_misalign <= 1;) {
917 static char aux_err[64];
918 t->aux_err = aux_err;
919 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
920 if (inp_misalign == (size_t)-1) {
921 /* kludge: inp_misalign == -1 means "exercise in-place" */
922 BIO_snprintf(aux_err, sizeof(aux_err),
923 "%s in-place, %sfragmented",
924 out_misalign ? "misaligned" : "aligned",
927 BIO_snprintf(aux_err, sizeof(aux_err),
928 "%s output and %s input, %sfragmented",
929 out_misalign ? "misaligned" : "aligned",
930 inp_misalign ? "misaligned" : "aligned",
934 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
935 /* Not fatal errors: return */
942 if (cdat->enc != 1) {
943 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
944 /* Not fatal errors: return */
953 if (out_misalign == 1 && frag == 0) {
955 * XTS, SIV, CCM and Wrap modes have special requirements about input
956 * lengths so we don't fragment for those
958 if (cdat->aead == EVP_CIPH_CCM_MODE
959 || ((EVP_CIPHER_flags(cdat->cipher) & EVP_CIPH_FLAG_CTS) != 0)
960 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_SIV_MODE
961 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
962 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
975 static const EVP_TEST_METHOD cipher_test_method = {
988 typedef struct mac_data_st {
989 /* MAC type in one form or another */
991 EVP_MAC *mac; /* for mac_test_run_mac */
992 int type; /* for mac_test_run_pkey */
993 /* Algorithm string for this MAC */
1002 unsigned char *input;
1004 /* Expected output */
1005 unsigned char *output;
1007 unsigned char *custom;
1009 /* MAC salt (blake2) */
1010 unsigned char *salt;
1012 /* Collection of controls */
1013 STACK_OF(OPENSSL_STRING) *controls;
1016 static int mac_test_init(EVP_TEST *t, const char *alg)
1018 EVP_MAC *mac = NULL;
1019 int type = NID_undef;
1022 if (is_mac_disabled(alg)) {
1023 TEST_info("skipping, '%s' is disabled", alg);
1027 if ((mac = EVP_MAC_fetch(libctx, alg, NULL)) == NULL) {
1029 * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
1030 * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
1031 * the EVP_PKEY method.
1033 size_t sz = strlen(alg);
1034 static const char epilogue[] = " by EVP_PKEY";
1036 if (sz >= sizeof(epilogue)
1037 && strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0)
1038 sz -= sizeof(epilogue) - 1;
1040 if (strncmp(alg, "HMAC", sz) == 0)
1041 type = EVP_PKEY_HMAC;
1042 else if (strncmp(alg, "CMAC", sz) == 0)
1043 type = EVP_PKEY_CMAC;
1044 else if (strncmp(alg, "Poly1305", sz) == 0)
1045 type = EVP_PKEY_POLY1305;
1046 else if (strncmp(alg, "SipHash", sz) == 0)
1047 type = EVP_PKEY_SIPHASH;
1052 mdat = OPENSSL_zalloc(sizeof(*mdat));
1054 mdat->mac_name = OPENSSL_strdup(alg);
1056 mdat->controls = sk_OPENSSL_STRING_new_null();
1061 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
1062 static void openssl_free(char *m)
1067 static void mac_test_cleanup(EVP_TEST *t)
1069 MAC_DATA *mdat = t->data;
1071 EVP_MAC_free(mdat->mac);
1072 OPENSSL_free(mdat->mac_name);
1073 sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free);
1074 OPENSSL_free(mdat->alg);
1075 OPENSSL_free(mdat->key);
1076 OPENSSL_free(mdat->iv);
1077 OPENSSL_free(mdat->custom);
1078 OPENSSL_free(mdat->salt);
1079 OPENSSL_free(mdat->input);
1080 OPENSSL_free(mdat->output);
1083 static int mac_test_parse(EVP_TEST *t,
1084 const char *keyword, const char *value)
1086 MAC_DATA *mdata = t->data;
1088 if (strcmp(keyword, "Key") == 0)
1089 return parse_bin(value, &mdata->key, &mdata->key_len);
1090 if (strcmp(keyword, "IV") == 0)
1091 return parse_bin(value, &mdata->iv, &mdata->iv_len);
1092 if (strcmp(keyword, "Custom") == 0)
1093 return parse_bin(value, &mdata->custom, &mdata->custom_len);
1094 if (strcmp(keyword, "Salt") == 0)
1095 return parse_bin(value, &mdata->salt, &mdata->salt_len);
1096 if (strcmp(keyword, "Algorithm") == 0) {
1097 mdata->alg = OPENSSL_strdup(value);
1102 if (strcmp(keyword, "Input") == 0)
1103 return parse_bin(value, &mdata->input, &mdata->input_len);
1104 if (strcmp(keyword, "Output") == 0)
1105 return parse_bin(value, &mdata->output, &mdata->output_len);
1106 if (strcmp(keyword, "Ctrl") == 0)
1107 return sk_OPENSSL_STRING_push(mdata->controls,
1108 OPENSSL_strdup(value)) != 0;
1112 static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1118 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1120 p = strchr(tmpval, ':');
1123 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1125 t->err = "PKEY_CTRL_INVALID";
1127 t->err = "PKEY_CTRL_ERROR";
1130 OPENSSL_free(tmpval);
1134 static int mac_test_run_pkey(EVP_TEST *t)
1136 MAC_DATA *expected = t->data;
1137 EVP_MD_CTX *mctx = NULL;
1138 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1139 EVP_PKEY *key = NULL;
1140 const char *mdname = NULL;
1141 EVP_CIPHER *cipher = NULL;
1142 unsigned char *got = NULL;
1146 if (expected->alg == NULL)
1147 TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type));
1149 TEST_info("Trying the EVP_PKEY %s test with %s",
1150 OBJ_nid2sn(expected->type), expected->alg);
1152 if (expected->type == EVP_PKEY_CMAC) {
1153 if (expected->alg != NULL && is_cipher_disabled(expected->alg)) {
1154 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
1159 if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, expected->alg, NULL))) {
1160 t->err = "MAC_KEY_CREATE_ERROR";
1163 key = EVP_PKEY_new_CMAC_key_ex(expected->key, expected->key_len,
1164 EVP_CIPHER_name(cipher), libctx, NULL);
1166 key = EVP_PKEY_new_raw_private_key_ex(libctx,
1167 OBJ_nid2sn(expected->type), NULL,
1168 expected->key, expected->key_len);
1171 t->err = "MAC_KEY_CREATE_ERROR";
1175 if (expected->type == EVP_PKEY_HMAC && expected->alg != NULL) {
1176 if (is_digest_disabled(expected->alg)) {
1177 TEST_info("skipping, HMAC '%s' is disabled", expected->alg);
1182 mdname = expected->alg;
1184 if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
1185 t->err = "INTERNAL_ERROR";
1188 if (!EVP_DigestSignInit_ex(mctx, &pctx, mdname, libctx, NULL, key)) {
1189 t->err = "DIGESTSIGNINIT_ERROR";
1192 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++)
1193 if (!mac_test_ctrl_pkey(t, pctx,
1194 sk_OPENSSL_STRING_value(expected->controls,
1196 t->err = "EVPPKEYCTXCTRL_ERROR";
1199 if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) {
1200 t->err = "DIGESTSIGNUPDATE_ERROR";
1203 if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) {
1204 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1207 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1208 t->err = "TEST_FAILURE";
1211 if (!EVP_DigestSignFinal(mctx, got, &got_len)
1212 || !memory_err_compare(t, "TEST_MAC_ERR",
1213 expected->output, expected->output_len,
1215 t->err = "TEST_MAC_ERR";
1220 EVP_CIPHER_free(cipher);
1221 EVP_MD_CTX_free(mctx);
1223 EVP_PKEY_CTX_free(genctx);
1228 static int mac_test_run_mac(EVP_TEST *t)
1230 MAC_DATA *expected = t->data;
1231 EVP_MAC_CTX *ctx = NULL;
1232 unsigned char *got = NULL;
1235 OSSL_PARAM params[21];
1236 size_t params_n = 0;
1237 size_t params_n_allocstart = 0;
1238 const OSSL_PARAM *defined_params =
1239 EVP_MAC_settable_ctx_params(expected->mac);
1241 if (expected->alg == NULL)
1242 TEST_info("Trying the EVP_MAC %s test", expected->mac_name);
1244 TEST_info("Trying the EVP_MAC %s test with %s",
1245 expected->mac_name, expected->alg);
1247 if (expected->alg != NULL) {
1249 * The underlying algorithm may be a cipher or a digest.
1250 * We don't know which it is, but we can ask the MAC what it
1251 * should be and bet on that.
1253 if (OSSL_PARAM_locate_const(defined_params,
1254 OSSL_MAC_PARAM_CIPHER) != NULL) {
1255 params[params_n++] =
1256 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
1258 } else if (OSSL_PARAM_locate_const(defined_params,
1259 OSSL_MAC_PARAM_DIGEST) != NULL) {
1260 params[params_n++] =
1261 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
1264 t->err = "MAC_BAD_PARAMS";
1268 if (expected->key != NULL)
1269 params[params_n++] =
1270 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
1273 if (expected->custom != NULL)
1274 params[params_n++] =
1275 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
1277 expected->custom_len);
1278 if (expected->salt != NULL)
1279 params[params_n++] =
1280 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT,
1282 expected->salt_len);
1283 if (expected->iv != NULL)
1284 params[params_n++] =
1285 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
1289 /* Unknown controls. They must match parameters that the MAC recognizes */
1290 if (params_n + sk_OPENSSL_STRING_num(expected->controls)
1291 >= OSSL_NELEM(params)) {
1292 t->err = "MAC_TOO_MANY_PARAMETERS";
1295 params_n_allocstart = params_n;
1296 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) {
1297 char *tmpkey, *tmpval;
1298 char *value = sk_OPENSSL_STRING_value(expected->controls, i);
1300 if (!TEST_ptr(tmpkey = OPENSSL_strdup(value))) {
1301 t->err = "MAC_PARAM_ERROR";
1304 tmpval = strchr(tmpkey, ':');
1309 || !OSSL_PARAM_allocate_from_text(¶ms[params_n],
1312 strlen(tmpval), NULL)) {
1313 OPENSSL_free(tmpkey);
1314 t->err = "MAC_PARAM_ERROR";
1319 OPENSSL_free(tmpkey);
1321 params[params_n] = OSSL_PARAM_construct_end();
1323 if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) {
1324 t->err = "MAC_CREATE_ERROR";
1328 if (!EVP_MAC_CTX_set_params(ctx, params)) {
1329 t->err = "MAC_BAD_PARAMS";
1332 if (!EVP_MAC_init(ctx)) {
1333 t->err = "MAC_INIT_ERROR";
1336 if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) {
1337 t->err = "MAC_UPDATE_ERROR";
1340 if (!EVP_MAC_final(ctx, NULL, &got_len, 0)) {
1341 t->err = "MAC_FINAL_LENGTH_ERROR";
1344 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1345 t->err = "TEST_FAILURE";
1348 if (!EVP_MAC_final(ctx, got, &got_len, got_len)
1349 || !memory_err_compare(t, "TEST_MAC_ERR",
1350 expected->output, expected->output_len,
1352 t->err = "TEST_MAC_ERR";
1357 while (params_n-- > params_n_allocstart) {
1358 OPENSSL_free(params[params_n].data);
1360 EVP_MAC_CTX_free(ctx);
1365 static int mac_test_run(EVP_TEST *t)
1367 MAC_DATA *expected = t->data;
1369 if (expected->mac != NULL)
1370 return mac_test_run_mac(t);
1371 return mac_test_run_pkey(t);
1374 static const EVP_TEST_METHOD mac_test_method = {
1385 ** These are all very similar and share much common code.
1388 typedef struct pkey_data_st {
1389 /* Context for this operation */
1391 /* Key operation to perform */
1392 int (*keyop) (EVP_PKEY_CTX *ctx,
1393 unsigned char *sig, size_t *siglen,
1394 const unsigned char *tbs, size_t tbslen);
1396 unsigned char *input;
1398 /* Expected output */
1399 unsigned char *output;
1404 * Perform public key operation setup: lookup key, allocated ctx and call
1405 * the appropriate initialisation function
1407 static int pkey_test_init(EVP_TEST *t, const char *name,
1409 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1410 int (*keyop)(EVP_PKEY_CTX *ctx,
1411 unsigned char *sig, size_t *siglen,
1412 const unsigned char *tbs,
1416 EVP_PKEY *pkey = NULL;
1420 rv = find_key(&pkey, name, public_keys);
1422 rv = find_key(&pkey, name, private_keys);
1423 if (rv == 0 || pkey == NULL) {
1424 TEST_info("skipping, key '%s' is disabled", name);
1429 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) {
1430 EVP_PKEY_free(pkey);
1433 kdata->keyop = keyop;
1434 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL))) {
1435 EVP_PKEY_free(pkey);
1436 OPENSSL_free(kdata);
1439 if (keyopinit(kdata->ctx) <= 0)
1440 t->err = "KEYOP_INIT_ERROR";
1445 static void pkey_test_cleanup(EVP_TEST *t)
1447 PKEY_DATA *kdata = t->data;
1449 OPENSSL_free(kdata->input);
1450 OPENSSL_free(kdata->output);
1451 EVP_PKEY_CTX_free(kdata->ctx);
1454 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1460 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1462 p = strchr(tmpval, ':');
1465 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1467 t->err = "PKEY_CTRL_INVALID";
1469 } else if (p != NULL && rv <= 0) {
1470 if (is_digest_disabled(p) || is_cipher_disabled(p)) {
1471 TEST_info("skipping, '%s' is disabled", p);
1475 t->err = "PKEY_CTRL_ERROR";
1479 OPENSSL_free(tmpval);
1483 static int pkey_test_parse(EVP_TEST *t,
1484 const char *keyword, const char *value)
1486 PKEY_DATA *kdata = t->data;
1487 if (strcmp(keyword, "Input") == 0)
1488 return parse_bin(value, &kdata->input, &kdata->input_len);
1489 if (strcmp(keyword, "Output") == 0)
1490 return parse_bin(value, &kdata->output, &kdata->output_len);
1491 if (strcmp(keyword, "Ctrl") == 0)
1492 return pkey_test_ctrl(t, kdata->ctx, value);
1496 static int pkey_test_run(EVP_TEST *t)
1498 PKEY_DATA *expected = t->data;
1499 unsigned char *got = NULL;
1501 EVP_PKEY_CTX *copy = NULL;
1503 if (expected->keyop(expected->ctx, NULL, &got_len,
1504 expected->input, expected->input_len) <= 0
1505 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1506 t->err = "KEYOP_LENGTH_ERROR";
1509 if (expected->keyop(expected->ctx, got, &got_len,
1510 expected->input, expected->input_len) <= 0) {
1511 t->err = "KEYOP_ERROR";
1514 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1515 expected->output, expected->output_len,
1523 /* Repeat the test on a copy. */
1524 if (!TEST_ptr(copy = EVP_PKEY_CTX_dup(expected->ctx))) {
1525 t->err = "INTERNAL_ERROR";
1528 if (expected->keyop(copy, NULL, &got_len, expected->input,
1529 expected->input_len) <= 0
1530 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1531 t->err = "KEYOP_LENGTH_ERROR";
1534 if (expected->keyop(copy, got, &got_len, expected->input,
1535 expected->input_len) <= 0) {
1536 t->err = "KEYOP_ERROR";
1539 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1540 expected->output, expected->output_len,
1546 EVP_PKEY_CTX_free(copy);
1550 static int sign_test_init(EVP_TEST *t, const char *name)
1552 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1555 static const EVP_TEST_METHOD psign_test_method = {
1563 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1565 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1566 EVP_PKEY_verify_recover);
1569 static const EVP_TEST_METHOD pverify_recover_test_method = {
1571 verify_recover_test_init,
1577 static int decrypt_test_init(EVP_TEST *t, const char *name)
1579 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1583 static const EVP_TEST_METHOD pdecrypt_test_method = {
1591 static int verify_test_init(EVP_TEST *t, const char *name)
1593 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1596 static int verify_test_run(EVP_TEST *t)
1598 PKEY_DATA *kdata = t->data;
1600 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1601 kdata->input, kdata->input_len) <= 0)
1602 t->err = "VERIFY_ERROR";
1606 static const EVP_TEST_METHOD pverify_test_method = {
1614 static int pderive_test_init(EVP_TEST *t, const char *name)
1616 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1619 static int pderive_test_parse(EVP_TEST *t,
1620 const char *keyword, const char *value)
1622 PKEY_DATA *kdata = t->data;
1624 if (strcmp(keyword, "PeerKey") == 0) {
1626 if (find_key(&peer, value, public_keys) == 0)
1628 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0) {
1629 t->err = "DERIVE_SET_PEER_ERROR";
1635 if (strcmp(keyword, "SharedSecret") == 0)
1636 return parse_bin(value, &kdata->output, &kdata->output_len);
1637 if (strcmp(keyword, "Ctrl") == 0)
1638 return pkey_test_ctrl(t, kdata->ctx, value);
1642 static int pderive_test_run(EVP_TEST *t)
1644 PKEY_DATA *expected = t->data;
1645 unsigned char *got = NULL;
1648 if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) {
1649 t->err = "DERIVE_ERROR";
1652 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1653 t->err = "DERIVE_ERROR";
1656 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
1657 t->err = "DERIVE_ERROR";
1660 if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH",
1661 expected->output, expected->output_len,
1671 static const EVP_TEST_METHOD pderive_test_method = {
1684 typedef enum pbe_type_enum {
1685 PBE_TYPE_INVALID = 0,
1686 PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
1689 typedef struct pbe_data_st {
1691 /* scrypt parameters */
1692 uint64_t N, r, p, maxmem;
1693 /* PKCS#12 parameters */
1697 unsigned char *pass;
1700 unsigned char *salt;
1702 /* Expected output */
1707 #ifndef OPENSSL_NO_SCRYPT
1708 /* Parse unsigned decimal 64 bit integer value */
1709 static int parse_uint64(const char *value, uint64_t *pr)
1711 const char *p = value;
1713 if (!TEST_true(*p)) {
1714 TEST_info("Invalid empty integer value");
1717 for (*pr = 0; *p; ) {
1718 if (*pr > UINT64_MAX / 10) {
1719 TEST_error("Integer overflow in string %s", value);
1723 if (!TEST_true(isdigit((unsigned char)*p))) {
1724 TEST_error("Invalid character in string %s", value);
1733 static int scrypt_test_parse(EVP_TEST *t,
1734 const char *keyword, const char *value)
1736 PBE_DATA *pdata = t->data;
1738 if (strcmp(keyword, "N") == 0)
1739 return parse_uint64(value, &pdata->N);
1740 if (strcmp(keyword, "p") == 0)
1741 return parse_uint64(value, &pdata->p);
1742 if (strcmp(keyword, "r") == 0)
1743 return parse_uint64(value, &pdata->r);
1744 if (strcmp(keyword, "maxmem") == 0)
1745 return parse_uint64(value, &pdata->maxmem);
1750 static int pbkdf2_test_parse(EVP_TEST *t,
1751 const char *keyword, const char *value)
1753 PBE_DATA *pdata = t->data;
1755 if (strcmp(keyword, "iter") == 0) {
1756 pdata->iter = atoi(value);
1757 if (pdata->iter <= 0)
1761 if (strcmp(keyword, "MD") == 0) {
1762 pdata->md = EVP_get_digestbyname(value);
1763 if (pdata->md == NULL)
1770 static int pkcs12_test_parse(EVP_TEST *t,
1771 const char *keyword, const char *value)
1773 PBE_DATA *pdata = t->data;
1775 if (strcmp(keyword, "id") == 0) {
1776 pdata->id = atoi(value);
1781 return pbkdf2_test_parse(t, keyword, value);
1784 static int pbe_test_init(EVP_TEST *t, const char *alg)
1787 PBE_TYPE pbe_type = PBE_TYPE_INVALID;
1789 if (is_kdf_disabled(alg)) {
1790 TEST_info("skipping, '%s' is disabled", alg);
1794 if (strcmp(alg, "scrypt") == 0) {
1795 pbe_type = PBE_TYPE_SCRYPT;
1796 } else if (strcmp(alg, "pbkdf2") == 0) {
1797 pbe_type = PBE_TYPE_PBKDF2;
1798 } else if (strcmp(alg, "pkcs12") == 0) {
1799 pbe_type = PBE_TYPE_PKCS12;
1801 TEST_error("Unknown pbe algorithm %s", alg);
1803 pdat = OPENSSL_zalloc(sizeof(*pdat));
1804 pdat->pbe_type = pbe_type;
1809 static void pbe_test_cleanup(EVP_TEST *t)
1811 PBE_DATA *pdat = t->data;
1813 OPENSSL_free(pdat->pass);
1814 OPENSSL_free(pdat->salt);
1815 OPENSSL_free(pdat->key);
1818 static int pbe_test_parse(EVP_TEST *t,
1819 const char *keyword, const char *value)
1821 PBE_DATA *pdata = t->data;
1823 if (strcmp(keyword, "Password") == 0)
1824 return parse_bin(value, &pdata->pass, &pdata->pass_len);
1825 if (strcmp(keyword, "Salt") == 0)
1826 return parse_bin(value, &pdata->salt, &pdata->salt_len);
1827 if (strcmp(keyword, "Key") == 0)
1828 return parse_bin(value, &pdata->key, &pdata->key_len);
1829 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1830 return pbkdf2_test_parse(t, keyword, value);
1831 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1832 return pkcs12_test_parse(t, keyword, value);
1833 #ifndef OPENSSL_NO_SCRYPT
1834 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1835 return scrypt_test_parse(t, keyword, value);
1840 static int pbe_test_run(EVP_TEST *t)
1842 PBE_DATA *expected = t->data;
1844 EVP_MD *fetched_digest = NULL;
1845 OSSL_LIB_CTX *save_libctx;
1847 save_libctx = OSSL_LIB_CTX_set0_default(libctx);
1849 if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
1850 t->err = "INTERNAL_ERROR";
1853 if (expected->pbe_type == PBE_TYPE_PBKDF2) {
1854 if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
1855 expected->salt, expected->salt_len,
1856 expected->iter, expected->md,
1857 expected->key_len, key) == 0) {
1858 t->err = "PBKDF2_ERROR";
1861 #ifndef OPENSSL_NO_SCRYPT
1862 } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
1863 if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
1864 expected->salt, expected->salt_len,
1865 expected->N, expected->r, expected->p,
1866 expected->maxmem, key, expected->key_len) == 0) {
1867 t->err = "SCRYPT_ERROR";
1871 } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
1872 fetched_digest = EVP_MD_fetch(libctx, EVP_MD_name(expected->md), NULL);
1873 if (fetched_digest == NULL) {
1874 t->err = "PKCS12_ERROR";
1877 if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
1878 expected->salt, expected->salt_len,
1879 expected->id, expected->iter, expected->key_len,
1880 key, fetched_digest) == 0) {
1881 t->err = "PKCS12_ERROR";
1885 if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len,
1886 key, expected->key_len))
1891 EVP_MD_free(fetched_digest);
1893 OSSL_LIB_CTX_set0_default(save_libctx);
1897 static const EVP_TEST_METHOD pbe_test_method = {
1911 BASE64_CANONICAL_ENCODING = 0,
1912 BASE64_VALID_ENCODING = 1,
1913 BASE64_INVALID_ENCODING = 2
1914 } base64_encoding_type;
1916 typedef struct encode_data_st {
1917 /* Input to encoding */
1918 unsigned char *input;
1920 /* Expected output */
1921 unsigned char *output;
1923 base64_encoding_type encoding;
1926 static int encode_test_init(EVP_TEST *t, const char *encoding)
1930 if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
1932 if (strcmp(encoding, "canonical") == 0) {
1933 edata->encoding = BASE64_CANONICAL_ENCODING;
1934 } else if (strcmp(encoding, "valid") == 0) {
1935 edata->encoding = BASE64_VALID_ENCODING;
1936 } else if (strcmp(encoding, "invalid") == 0) {
1937 edata->encoding = BASE64_INVALID_ENCODING;
1938 if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
1941 TEST_error("Bad encoding: %s."
1942 " Should be one of {canonical, valid, invalid}",
1949 OPENSSL_free(edata);
1953 static void encode_test_cleanup(EVP_TEST *t)
1955 ENCODE_DATA *edata = t->data;
1957 OPENSSL_free(edata->input);
1958 OPENSSL_free(edata->output);
1959 memset(edata, 0, sizeof(*edata));
1962 static int encode_test_parse(EVP_TEST *t,
1963 const char *keyword, const char *value)
1965 ENCODE_DATA *edata = t->data;
1967 if (strcmp(keyword, "Input") == 0)
1968 return parse_bin(value, &edata->input, &edata->input_len);
1969 if (strcmp(keyword, "Output") == 0)
1970 return parse_bin(value, &edata->output, &edata->output_len);
1974 static int encode_test_run(EVP_TEST *t)
1976 ENCODE_DATA *expected = t->data;
1977 unsigned char *encode_out = NULL, *decode_out = NULL;
1978 int output_len, chunk_len;
1979 EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL;
1981 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
1982 t->err = "INTERNAL_ERROR";
1986 if (expected->encoding == BASE64_CANONICAL_ENCODING) {
1988 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
1989 || !TEST_ptr(encode_out =
1990 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
1993 EVP_EncodeInit(encode_ctx);
1994 if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1995 expected->input, expected->input_len)))
1998 output_len = chunk_len;
2000 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
2001 output_len += chunk_len;
2003 if (!memory_err_compare(t, "BAD_ENCODING",
2004 expected->output, expected->output_len,
2005 encode_out, output_len))
2009 if (!TEST_ptr(decode_out =
2010 OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
2013 EVP_DecodeInit(decode_ctx);
2014 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
2015 expected->output_len) < 0) {
2016 t->err = "DECODE_ERROR";
2019 output_len = chunk_len;
2021 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
2022 t->err = "DECODE_ERROR";
2025 output_len += chunk_len;
2027 if (expected->encoding != BASE64_INVALID_ENCODING
2028 && !memory_err_compare(t, "BAD_DECODING",
2029 expected->input, expected->input_len,
2030 decode_out, output_len)) {
2031 t->err = "BAD_DECODING";
2037 OPENSSL_free(encode_out);
2038 OPENSSL_free(decode_out);
2039 EVP_ENCODE_CTX_free(decode_ctx);
2040 EVP_ENCODE_CTX_free(encode_ctx);
2044 static const EVP_TEST_METHOD encode_test_method = {
2047 encode_test_cleanup,
2056 #define MAX_RAND_REPEATS 15
2058 typedef struct rand_data_pass_st {
2059 unsigned char *entropy;
2060 unsigned char *reseed_entropy;
2061 unsigned char *nonce;
2062 unsigned char *pers;
2063 unsigned char *reseed_addin;
2064 unsigned char *addinA;
2065 unsigned char *addinB;
2066 unsigned char *pr_entropyA;
2067 unsigned char *pr_entropyB;
2068 unsigned char *output;
2069 size_t entropy_len, nonce_len, pers_len, addinA_len, addinB_len,
2070 pr_entropyA_len, pr_entropyB_len, output_len, reseed_entropy_len,
2074 typedef struct rand_data_st {
2075 /* Context for this operation */
2077 EVP_RAND_CTX *parent;
2079 int prediction_resistance;
2081 unsigned int generate_bits;
2085 /* Expected output */
2086 RAND_DATA_PASS data[MAX_RAND_REPEATS];
2089 static int rand_test_init(EVP_TEST *t, const char *name)
2093 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
2094 unsigned int strength = 256;
2096 if (!TEST_ptr(rdata = OPENSSL_zalloc(sizeof(*rdata))))
2099 /* TEST-RAND is available in the FIPS provider but not with "fips=yes" */
2100 rand = EVP_RAND_fetch(libctx, "TEST-RAND", "-fips");
2103 rdata->parent = EVP_RAND_CTX_new(rand, NULL);
2104 EVP_RAND_free(rand);
2105 if (rdata->parent == NULL)
2108 *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength);
2109 if (!EVP_RAND_set_ctx_params(rdata->parent, params))
2112 rand = EVP_RAND_fetch(libctx, name, NULL);
2115 rdata->ctx = EVP_RAND_CTX_new(rand, rdata->parent);
2116 EVP_RAND_free(rand);
2117 if (rdata->ctx == NULL)
2124 EVP_RAND_CTX_free(rdata->parent);
2125 OPENSSL_free(rdata);
2129 static void rand_test_cleanup(EVP_TEST *t)
2131 RAND_DATA *rdata = t->data;
2134 OPENSSL_free(rdata->cipher);
2135 OPENSSL_free(rdata->digest);
2137 for (i = 0; i <= rdata->n; i++) {
2138 OPENSSL_free(rdata->data[i].entropy);
2139 OPENSSL_free(rdata->data[i].reseed_entropy);
2140 OPENSSL_free(rdata->data[i].nonce);
2141 OPENSSL_free(rdata->data[i].pers);
2142 OPENSSL_free(rdata->data[i].reseed_addin);
2143 OPENSSL_free(rdata->data[i].addinA);
2144 OPENSSL_free(rdata->data[i].addinB);
2145 OPENSSL_free(rdata->data[i].pr_entropyA);
2146 OPENSSL_free(rdata->data[i].pr_entropyB);
2147 OPENSSL_free(rdata->data[i].output);
2149 EVP_RAND_CTX_free(rdata->ctx);
2150 EVP_RAND_CTX_free(rdata->parent);
2153 static int rand_test_parse(EVP_TEST *t,
2154 const char *keyword, const char *value)
2156 RAND_DATA *rdata = t->data;
2157 RAND_DATA_PASS *item;
2161 if ((p = strchr(keyword, '.')) != NULL) {
2163 if (n >= MAX_RAND_REPEATS)
2167 item = rdata->data + n;
2168 if (strncmp(keyword, "Entropy.", sizeof("Entropy")) == 0)
2169 return parse_bin(value, &item->entropy, &item->entropy_len);
2170 if (strncmp(keyword, "ReseedEntropy.", sizeof("ReseedEntropy")) == 0)
2171 return parse_bin(value, &item->reseed_entropy,
2172 &item->reseed_entropy_len);
2173 if (strncmp(keyword, "Nonce.", sizeof("Nonce")) == 0)
2174 return parse_bin(value, &item->nonce, &item->nonce_len);
2175 if (strncmp(keyword, "PersonalisationString.",
2176 sizeof("PersonalisationString")) == 0)
2177 return parse_bin(value, &item->pers, &item->pers_len);
2178 if (strncmp(keyword, "ReseedAdditionalInput.",
2179 sizeof("ReseedAdditionalInput")) == 0)
2180 return parse_bin(value, &item->reseed_addin,
2181 &item->reseed_addin_len);
2182 if (strncmp(keyword, "AdditionalInputA.",
2183 sizeof("AdditionalInputA")) == 0)
2184 return parse_bin(value, &item->addinA, &item->addinA_len);
2185 if (strncmp(keyword, "AdditionalInputB.",
2186 sizeof("AdditionalInputB")) == 0)
2187 return parse_bin(value, &item->addinB, &item->addinB_len);
2188 if (strncmp(keyword, "EntropyPredictionResistanceA.",
2189 sizeof("EntropyPredictionResistanceA")) == 0)
2190 return parse_bin(value, &item->pr_entropyA, &item->pr_entropyA_len);
2191 if (strncmp(keyword, "EntropyPredictionResistanceB.",
2192 sizeof("EntropyPredictionResistanceB")) == 0)
2193 return parse_bin(value, &item->pr_entropyB, &item->pr_entropyB_len);
2194 if (strncmp(keyword, "Output.", sizeof("Output")) == 0)
2195 return parse_bin(value, &item->output, &item->output_len);
2197 if (strcmp(keyword, "Cipher") == 0)
2198 return TEST_ptr(rdata->cipher = OPENSSL_strdup(value));
2199 if (strcmp(keyword, "Digest") == 0)
2200 return TEST_ptr(rdata->digest = OPENSSL_strdup(value));
2201 if (strcmp(keyword, "DerivationFunction") == 0) {
2202 rdata->use_df = atoi(value) != 0;
2205 if (strcmp(keyword, "GenerateBits") == 0) {
2206 if ((n = atoi(value)) <= 0 || n % 8 != 0)
2208 rdata->generate_bits = (unsigned int)n;
2211 if (strcmp(keyword, "PredictionResistance") == 0) {
2212 rdata->prediction_resistance = atoi(value) != 0;
2219 static int rand_test_run(EVP_TEST *t)
2221 RAND_DATA *expected = t->data;
2222 RAND_DATA_PASS *item;
2224 size_t got_len = expected->generate_bits / 8;
2225 OSSL_PARAM params[5], *p = params;
2226 int i = -1, ret = 0;
2227 unsigned int strength;
2230 if (!TEST_ptr(got = OPENSSL_malloc(got_len)))
2233 *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &expected->use_df);
2234 if (expected->cipher != NULL)
2235 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
2236 expected->cipher, 0);
2237 if (expected->digest != NULL)
2238 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
2239 expected->digest, 0);
2240 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC, "HMAC", 0);
2241 *p = OSSL_PARAM_construct_end();
2242 if (!TEST_true(EVP_RAND_set_ctx_params(expected->ctx, params)))
2245 strength = EVP_RAND_strength(expected->ctx);
2246 for (i = 0; i <= expected->n; i++) {
2247 item = expected->data + i;
2250 z = item->entropy != NULL ? item->entropy : (unsigned char *)"";
2251 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
2252 z, item->entropy_len);
2253 z = item->nonce != NULL ? item->nonce : (unsigned char *)"";
2254 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
2255 z, item->nonce_len);
2256 *p = OSSL_PARAM_construct_end();
2257 if (!TEST_true(EVP_RAND_set_ctx_params(expected->parent, params))
2258 || !TEST_true(EVP_RAND_instantiate(expected->parent, strength,
2262 z = item->pers != NULL ? item->pers : (unsigned char *)"";
2263 if (!TEST_true(EVP_RAND_instantiate
2264 (expected->ctx, strength,
2265 expected->prediction_resistance, z,
2269 if (item->reseed_entropy != NULL) {
2270 params[0] = OSSL_PARAM_construct_octet_string
2271 (OSSL_RAND_PARAM_TEST_ENTROPY, item->reseed_entropy,
2272 item->reseed_entropy_len);
2273 params[1] = OSSL_PARAM_construct_end();
2274 if (!TEST_true(EVP_RAND_set_ctx_params(expected->parent, params)))
2277 if (!TEST_true(EVP_RAND_reseed
2278 (expected->ctx, expected->prediction_resistance,
2279 NULL, 0, item->reseed_addin,
2280 item->reseed_addin_len)))
2283 if (item->pr_entropyA != NULL) {
2284 params[0] = OSSL_PARAM_construct_octet_string
2285 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyA,
2286 item->pr_entropyA_len);
2287 params[1] = OSSL_PARAM_construct_end();
2288 if (!TEST_true(EVP_RAND_set_ctx_params(expected->parent, params)))
2291 if (!TEST_true(EVP_RAND_generate
2292 (expected->ctx, got, got_len,
2293 strength, expected->prediction_resistance,
2294 item->addinA, item->addinA_len)))
2297 if (item->pr_entropyB != NULL) {
2298 params[0] = OSSL_PARAM_construct_octet_string
2299 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyB,
2300 item->pr_entropyB_len);
2301 params[1] = OSSL_PARAM_construct_end();
2302 if (!TEST_true(EVP_RAND_set_ctx_params(expected->parent, params)))
2305 if (!TEST_true(EVP_RAND_generate
2306 (expected->ctx, got, got_len,
2307 strength, expected->prediction_resistance,
2308 item->addinB, item->addinB_len)))
2310 if (!TEST_mem_eq(got, got_len, item->output, item->output_len))
2312 if (!TEST_true(EVP_RAND_uninstantiate(expected->ctx))
2313 || !TEST_true(EVP_RAND_uninstantiate(expected->parent))
2314 || !TEST_true(EVP_RAND_verify_zeroization(expected->ctx))
2315 || !TEST_int_eq(EVP_RAND_state(expected->ctx),
2316 EVP_RAND_STATE_UNINITIALISED))
2323 if (ret == 0 && i >= 0)
2324 TEST_info("Error in test case %d of %d\n", i, expected->n + 1);
2329 static const EVP_TEST_METHOD rand_test_method = {
2341 typedef struct kdf_data_st {
2342 /* Context for this operation */
2344 /* Expected output */
2345 unsigned char *output;
2347 OSSL_PARAM params[20];
2352 * Perform public key operation setup: lookup key, allocated ctx and call
2353 * the appropriate initialisation function
2355 static int kdf_test_init(EVP_TEST *t, const char *name)
2360 if (is_kdf_disabled(name)) {
2361 TEST_info("skipping, '%s' is disabled", name);
2366 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2368 kdata->p = kdata->params;
2369 *kdata->p = OSSL_PARAM_construct_end();
2371 kdf = EVP_KDF_fetch(libctx, name, NULL);
2373 OPENSSL_free(kdata);
2376 kdata->ctx = EVP_KDF_CTX_new(kdf);
2378 if (kdata->ctx == NULL) {
2379 OPENSSL_free(kdata);
2386 static void kdf_test_cleanup(EVP_TEST *t)
2388 KDF_DATA *kdata = t->data;
2391 for (p = kdata->params; p->key != NULL; p++)
2392 OPENSSL_free(p->data);
2393 OPENSSL_free(kdata->output);
2394 EVP_KDF_CTX_free(kdata->ctx);
2397 static int kdf_test_ctrl(EVP_TEST *t, EVP_KDF_CTX *kctx,
2400 KDF_DATA *kdata = t->data;
2403 const OSSL_PARAM *defs = EVP_KDF_settable_ctx_params(EVP_KDF_CTX_kdf(kctx));
2405 if (!TEST_ptr(name = OPENSSL_strdup(value)))
2407 p = strchr(name, ':');
2411 rv = OSSL_PARAM_allocate_from_text(kdata->p, defs, name, p,
2412 p != NULL ? strlen(p) : 0, NULL);
2413 *++kdata->p = OSSL_PARAM_construct_end();
2415 t->err = "KDF_PARAM_ERROR";
2419 if (p != NULL && strcmp(name, "digest") == 0) {
2420 if (is_digest_disabled(p)) {
2421 TEST_info("skipping, '%s' is disabled", p);
2425 if (p != NULL && strcmp(name, "cipher") == 0) {
2426 if (is_cipher_disabled(p)) {
2427 TEST_info("skipping, '%s' is disabled", p);
2435 static int kdf_test_parse(EVP_TEST *t,
2436 const char *keyword, const char *value)
2438 KDF_DATA *kdata = t->data;
2440 if (strcmp(keyword, "Output") == 0)
2441 return parse_bin(value, &kdata->output, &kdata->output_len);
2442 if (strncmp(keyword, "Ctrl", 4) == 0)
2443 return kdf_test_ctrl(t, kdata->ctx, value);
2447 static int kdf_test_run(EVP_TEST *t)
2449 KDF_DATA *expected = t->data;
2450 unsigned char *got = NULL;
2451 size_t got_len = expected->output_len;
2453 if (!EVP_KDF_CTX_set_params(expected->ctx, expected->params)) {
2454 t->err = "KDF_CTRL_ERROR";
2457 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2458 t->err = "INTERNAL_ERROR";
2461 if (EVP_KDF_derive(expected->ctx, got, got_len) <= 0) {
2462 t->err = "KDF_DERIVE_ERROR";
2465 if (!memory_err_compare(t, "KDF_MISMATCH",
2466 expected->output, expected->output_len,
2477 static const EVP_TEST_METHOD kdf_test_method = {
2489 typedef struct pkey_kdf_data_st {
2490 /* Context for this operation */
2492 /* Expected output */
2493 unsigned char *output;
2498 * Perform public key operation setup: lookup key, allocated ctx and call
2499 * the appropriate initialisation function
2501 static int pkey_kdf_test_init(EVP_TEST *t, const char *name)
2503 PKEY_KDF_DATA *kdata = NULL;
2505 if (is_kdf_disabled(name)) {
2506 TEST_info("skipping, '%s' is disabled", name);
2511 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2514 kdata->ctx = EVP_PKEY_CTX_new_from_name(libctx, name, NULL);
2515 if (kdata->ctx == NULL
2516 || EVP_PKEY_derive_init(kdata->ctx) <= 0)
2522 EVP_PKEY_CTX_free(kdata->ctx);
2523 OPENSSL_free(kdata);
2527 static void pkey_kdf_test_cleanup(EVP_TEST *t)
2529 PKEY_KDF_DATA *kdata = t->data;
2531 OPENSSL_free(kdata->output);
2532 EVP_PKEY_CTX_free(kdata->ctx);
2535 static int pkey_kdf_test_parse(EVP_TEST *t,
2536 const char *keyword, const char *value)
2538 PKEY_KDF_DATA *kdata = t->data;
2540 if (strcmp(keyword, "Output") == 0)
2541 return parse_bin(value, &kdata->output, &kdata->output_len);
2542 if (strncmp(keyword, "Ctrl", 4) == 0)
2543 return pkey_test_ctrl(t, kdata->ctx, value);
2547 static int pkey_kdf_test_run(EVP_TEST *t)
2549 PKEY_KDF_DATA *expected = t->data;
2550 unsigned char *got = NULL;
2551 size_t got_len = expected->output_len;
2553 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2554 t->err = "INTERNAL_ERROR";
2557 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
2558 t->err = "KDF_DERIVE_ERROR";
2561 if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
2562 t->err = "KDF_MISMATCH";
2572 static const EVP_TEST_METHOD pkey_kdf_test_method = {
2575 pkey_kdf_test_cleanup,
2576 pkey_kdf_test_parse,
2584 typedef struct keypair_test_data_st {
2587 } KEYPAIR_TEST_DATA;
2589 static int keypair_test_init(EVP_TEST *t, const char *pair)
2591 KEYPAIR_TEST_DATA *data;
2593 EVP_PKEY *pk = NULL, *pubk = NULL;
2594 char *pub, *priv = NULL;
2596 /* Split private and public names. */
2597 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
2598 || !TEST_ptr(pub = strchr(priv, ':'))) {
2599 t->err = "PARSING_ERROR";
2604 if (!TEST_true(find_key(&pk, priv, private_keys))) {
2605 TEST_info("Can't find private key: %s", priv);
2606 t->err = "MISSING_PRIVATE_KEY";
2609 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
2610 TEST_info("Can't find public key: %s", pub);
2611 t->err = "MISSING_PUBLIC_KEY";
2615 if (pk == NULL && pubk == NULL) {
2616 /* Both keys are listed but unsupported: skip this test */
2622 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2635 static void keypair_test_cleanup(EVP_TEST *t)
2637 OPENSSL_free(t->data);
2642 * For tests that do not accept any custom keywords.
2644 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
2649 static int keypair_test_run(EVP_TEST *t)
2652 const KEYPAIR_TEST_DATA *pair = t->data;
2654 if (pair->privk == NULL || pair->pubk == NULL) {
2656 * this can only happen if only one of the keys is not set
2657 * which means that one of them was unsupported while the
2658 * other isn't: hence a key type mismatch.
2660 t->err = "KEYPAIR_TYPE_MISMATCH";
2665 if ((rv = EVP_PKEY_eq(pair->privk, pair->pubk)) != 1 ) {
2667 t->err = "KEYPAIR_MISMATCH";
2668 } else if ( -1 == rv ) {
2669 t->err = "KEYPAIR_TYPE_MISMATCH";
2670 } else if ( -2 == rv ) {
2671 t->err = "UNSUPPORTED_KEY_COMPARISON";
2673 TEST_error("Unexpected error in key comparison");
2688 static const EVP_TEST_METHOD keypair_test_method = {
2691 keypair_test_cleanup,
2700 typedef struct keygen_test_data_st {
2701 EVP_PKEY_CTX *genctx; /* Keygen context to use */
2702 char *keyname; /* Key name to store key or NULL */
2705 static int keygen_test_init(EVP_TEST *t, const char *alg)
2707 KEYGEN_TEST_DATA *data;
2708 EVP_PKEY_CTX *genctx;
2709 int nid = OBJ_sn2nid(alg);
2711 if (nid == NID_undef) {
2712 nid = OBJ_ln2nid(alg);
2713 if (nid == NID_undef)
2717 if (is_pkey_disabled(alg)) {
2721 if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_from_name(libctx, alg, NULL)))
2724 if (EVP_PKEY_keygen_init(genctx) <= 0) {
2725 t->err = "KEYGEN_INIT_ERROR";
2729 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2731 data->genctx = genctx;
2732 data->keyname = NULL;
2738 EVP_PKEY_CTX_free(genctx);
2742 static void keygen_test_cleanup(EVP_TEST *t)
2744 KEYGEN_TEST_DATA *keygen = t->data;
2746 EVP_PKEY_CTX_free(keygen->genctx);
2747 OPENSSL_free(keygen->keyname);
2748 OPENSSL_free(t->data);
2752 static int keygen_test_parse(EVP_TEST *t,
2753 const char *keyword, const char *value)
2755 KEYGEN_TEST_DATA *keygen = t->data;
2757 if (strcmp(keyword, "KeyName") == 0)
2758 return TEST_ptr(keygen->keyname = OPENSSL_strdup(value));
2759 if (strcmp(keyword, "Ctrl") == 0)
2760 return pkey_test_ctrl(t, keygen->genctx, value);
2764 static int keygen_test_run(EVP_TEST *t)
2766 KEYGEN_TEST_DATA *keygen = t->data;
2767 EVP_PKEY *pkey = NULL;
2770 if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) {
2771 t->err = "KEYGEN_GENERATE_ERROR";
2775 if (!evp_pkey_is_provided(pkey)) {
2776 TEST_info("Warning: legacy key generated %s", keygen->keyname);
2779 if (keygen->keyname != NULL) {
2783 if (find_key(NULL, keygen->keyname, private_keys)) {
2784 TEST_info("Duplicate key %s", keygen->keyname);
2788 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
2790 key->name = keygen->keyname;
2791 keygen->keyname = NULL;
2793 key->next = private_keys;
2797 EVP_PKEY_free(pkey);
2806 static const EVP_TEST_METHOD keygen_test_method = {
2809 keygen_test_cleanup,
2815 ** DIGEST SIGN+VERIFY TESTS
2819 int is_verify; /* Set to 1 if verifying */
2820 int is_oneshot; /* Set to 1 for one shot operation */
2821 const EVP_MD *md; /* Digest to use */
2822 EVP_MD_CTX *ctx; /* Digest context */
2824 STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
2825 unsigned char *osin; /* Input data if one shot */
2826 size_t osin_len; /* Input length data if one shot */
2827 unsigned char *output; /* Expected output */
2828 size_t output_len; /* Expected output length */
2831 static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
2834 const EVP_MD *md = NULL;
2835 DIGESTSIGN_DATA *mdat;
2837 if (strcmp(alg, "NULL") != 0) {
2838 if (is_digest_disabled(alg)) {
2842 md = EVP_get_digestbyname(alg);
2846 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
2849 if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
2853 mdat->is_verify = is_verify;
2854 mdat->is_oneshot = is_oneshot;
2859 static int digestsign_test_init(EVP_TEST *t, const char *alg)
2861 return digestsigver_test_init(t, alg, 0, 0);
2864 static void digestsigver_test_cleanup(EVP_TEST *t)
2866 DIGESTSIGN_DATA *mdata = t->data;
2868 EVP_MD_CTX_free(mdata->ctx);
2869 sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
2870 OPENSSL_free(mdata->osin);
2871 OPENSSL_free(mdata->output);
2872 OPENSSL_free(mdata);
2876 static int digestsigver_test_parse(EVP_TEST *t,
2877 const char *keyword, const char *value)
2879 DIGESTSIGN_DATA *mdata = t->data;
2881 if (strcmp(keyword, "Key") == 0) {
2882 EVP_PKEY *pkey = NULL;
2884 const char *name = mdata->md == NULL ? NULL : EVP_MD_name(mdata->md);
2886 if (mdata->is_verify)
2887 rv = find_key(&pkey, value, public_keys);
2889 rv = find_key(&pkey, value, private_keys);
2890 if (rv == 0 || pkey == NULL) {
2894 if (mdata->is_verify) {
2895 if (!EVP_DigestVerifyInit_ex(mdata->ctx, &mdata->pctx, name, libctx,
2897 t->err = "DIGESTVERIFYINIT_ERROR";
2900 if (!EVP_DigestSignInit_ex(mdata->ctx, &mdata->pctx, name, libctx, NULL,
2902 t->err = "DIGESTSIGNINIT_ERROR";
2906 if (strcmp(keyword, "Input") == 0) {
2907 if (mdata->is_oneshot)
2908 return parse_bin(value, &mdata->osin, &mdata->osin_len);
2909 return evp_test_buffer_append(value, &mdata->input);
2911 if (strcmp(keyword, "Output") == 0)
2912 return parse_bin(value, &mdata->output, &mdata->output_len);
2914 if (!mdata->is_oneshot) {
2915 if (strcmp(keyword, "Count") == 0)
2916 return evp_test_buffer_set_count(value, mdata->input);
2917 if (strcmp(keyword, "Ncopy") == 0)
2918 return evp_test_buffer_ncopy(value, mdata->input);
2920 if (strcmp(keyword, "Ctrl") == 0) {
2921 if (mdata->pctx == NULL)
2923 return pkey_test_ctrl(t, mdata->pctx, value);
2928 static int digestsign_update_fn(void *ctx, const unsigned char *buf,
2931 return EVP_DigestSignUpdate(ctx, buf, buflen);
2934 static int digestsign_test_run(EVP_TEST *t)
2936 DIGESTSIGN_DATA *expected = t->data;
2937 unsigned char *got = NULL;
2940 if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
2942 t->err = "DIGESTUPDATE_ERROR";
2946 if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
2947 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
2950 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2951 t->err = "MALLOC_FAILURE";
2954 if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
2955 t->err = "DIGESTSIGNFINAL_ERROR";
2958 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
2959 expected->output, expected->output_len,
2969 static const EVP_TEST_METHOD digestsign_test_method = {
2971 digestsign_test_init,
2972 digestsigver_test_cleanup,
2973 digestsigver_test_parse,
2977 static int digestverify_test_init(EVP_TEST *t, const char *alg)
2979 return digestsigver_test_init(t, alg, 1, 0);
2982 static int digestverify_update_fn(void *ctx, const unsigned char *buf,
2985 return EVP_DigestVerifyUpdate(ctx, buf, buflen);
2988 static int digestverify_test_run(EVP_TEST *t)
2990 DIGESTSIGN_DATA *mdata = t->data;
2992 if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
2993 t->err = "DIGESTUPDATE_ERROR";
2997 if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
2998 mdata->output_len) <= 0)
2999 t->err = "VERIFY_ERROR";
3003 static const EVP_TEST_METHOD digestverify_test_method = {
3005 digestverify_test_init,
3006 digestsigver_test_cleanup,
3007 digestsigver_test_parse,
3008 digestverify_test_run
3011 static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
3013 return digestsigver_test_init(t, alg, 0, 1);
3016 static int oneshot_digestsign_test_run(EVP_TEST *t)
3018 DIGESTSIGN_DATA *expected = t->data;
3019 unsigned char *got = NULL;
3022 if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
3023 expected->osin, expected->osin_len)) {
3024 t->err = "DIGESTSIGN_LENGTH_ERROR";
3027 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
3028 t->err = "MALLOC_FAILURE";
3031 if (!EVP_DigestSign(expected->ctx, got, &got_len,
3032 expected->osin, expected->osin_len)) {
3033 t->err = "DIGESTSIGN_ERROR";
3036 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
3037 expected->output, expected->output_len,
3047 static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
3048 "OneShotDigestSign",
3049 oneshot_digestsign_test_init,
3050 digestsigver_test_cleanup,
3051 digestsigver_test_parse,
3052 oneshot_digestsign_test_run
3055 static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
3057 return digestsigver_test_init(t, alg, 1, 1);
3060 static int oneshot_digestverify_test_run(EVP_TEST *t)
3062 DIGESTSIGN_DATA *mdata = t->data;
3064 if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
3065 mdata->osin, mdata->osin_len) <= 0)
3066 t->err = "VERIFY_ERROR";
3070 static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
3071 "OneShotDigestVerify",
3072 oneshot_digestverify_test_init,
3073 digestsigver_test_cleanup,
3074 digestsigver_test_parse,
3075 oneshot_digestverify_test_run
3080 ** PARSING AND DISPATCH
3083 static const EVP_TEST_METHOD *evp_test_list[] = {
3085 &cipher_test_method,
3086 &digest_test_method,
3087 &digestsign_test_method,
3088 &digestverify_test_method,
3089 &encode_test_method,
3091 &pkey_kdf_test_method,
3092 &keypair_test_method,
3093 &keygen_test_method,
3095 &oneshot_digestsign_test_method,
3096 &oneshot_digestverify_test_method,
3098 &pdecrypt_test_method,
3099 &pderive_test_method,
3101 &pverify_recover_test_method,
3102 &pverify_test_method,
3106 static const EVP_TEST_METHOD *find_test(const char *name)
3108 const EVP_TEST_METHOD **tt;
3110 for (tt = evp_test_list; *tt; tt++) {
3111 if (strcmp(name, (*tt)->name) == 0)
3117 static void clear_test(EVP_TEST *t)
3119 test_clearstanza(&t->s);
3121 if (t->data != NULL) {
3122 if (t->meth != NULL)
3123 t->meth->cleanup(t);
3124 OPENSSL_free(t->data);
3127 OPENSSL_free(t->expected_err);
3128 t->expected_err = NULL;
3129 OPENSSL_free(t->reason);
3138 /* Check for errors in the test structure; return 1 if okay, else 0. */
3139 static int check_test_error(EVP_TEST *t)
3144 if (t->err == NULL && t->expected_err == NULL)
3146 if (t->err != NULL && t->expected_err == NULL) {
3147 if (t->aux_err != NULL) {
3148 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
3149 t->s.test_file, t->s.start, t->aux_err, t->err);
3151 TEST_info("%s:%d: Source of above error; unexpected error %s",
3152 t->s.test_file, t->s.start, t->err);
3156 if (t->err == NULL && t->expected_err != NULL) {
3157 TEST_info("%s:%d: Succeeded but was expecting %s",
3158 t->s.test_file, t->s.start, t->expected_err);
3162 if (strcmp(t->err, t->expected_err) != 0) {
3163 TEST_info("%s:%d: Expected %s got %s",
3164 t->s.test_file, t->s.start, t->expected_err, t->err);
3168 if (t->reason == NULL)
3171 if (t->reason == NULL) {
3172 TEST_info("%s:%d: Test is missing function or reason code",
3173 t->s.test_file, t->s.start);
3177 err = ERR_peek_error();
3179 TEST_info("%s:%d: Expected error \"%s\" not set",
3180 t->s.test_file, t->s.start, t->reason);
3184 reason = ERR_reason_error_string(err);
3185 if (reason == NULL) {
3186 TEST_info("%s:%d: Expected error \"%s\", no strings available."
3188 t->s.test_file, t->s.start, t->reason);
3192 if (strcmp(reason, t->reason) == 0)
3195 TEST_info("%s:%d: Expected error \"%s\", got \"%s\"",
3196 t->s.test_file, t->s.start, t->reason, reason);
3201 /* Run a parsed test. Log a message and return 0 on error. */
3202 static int run_test(EVP_TEST *t)
3204 if (t->meth == NULL)
3211 if (t->err == NULL && t->meth->run_test(t) != 1) {
3212 TEST_info("%s:%d %s error",
3213 t->s.test_file, t->s.start, t->meth->name);
3216 if (!check_test_error(t)) {
3217 TEST_openssl_errors();
3226 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
3228 for (; lst != NULL; lst = lst->next) {
3229 if (strcmp(lst->name, name) == 0) {
3238 static void free_key_list(KEY_LIST *lst)
3240 while (lst != NULL) {
3241 KEY_LIST *next = lst->next;
3243 EVP_PKEY_free(lst->key);
3244 OPENSSL_free(lst->name);
3251 * Is the key type an unsupported algorithm?
3253 static int key_unsupported(void)
3255 long err = ERR_peek_last_error();
3257 if (ERR_GET_LIB(err) == ERR_LIB_EVP
3258 && (ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM)) {
3262 #ifndef OPENSSL_NO_EC
3264 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
3265 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
3268 if (ERR_GET_LIB(err) == ERR_LIB_EC
3269 && (ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP
3270 || ERR_GET_REASON(err) == EC_R_INVALID_CURVE)) {
3274 #endif /* OPENSSL_NO_EC */
3278 /* NULL out the value from |pp| but return it. This "steals" a pointer. */
3279 static char *take_value(PAIR *pp)
3281 char *p = pp->value;
3287 #if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
3288 static int securitycheck_enabled(void)
3290 static int enabled = -1;
3292 if (enabled == -1) {
3293 if (OSSL_PROVIDER_available(libctx, "fips")) {
3294 OSSL_PARAM params[2];
3295 OSSL_PROVIDER *prov = NULL;
3298 prov = OSSL_PROVIDER_load(libctx, "fips");
3301 OSSL_PARAM_construct_int(OSSL_PROV_PARAM_SECURITY_CHECKS,
3303 params[1] = OSSL_PARAM_construct_end();
3304 OSSL_PROVIDER_get_params(prov, params);
3305 OSSL_PROVIDER_unload(prov);
3317 * Return 1 if one of the providers named in the string is available.
3318 * The provider names are separated with whitespace.
3319 * NOTE: destructive function, it inserts '\0' after each provider name.
3321 static int prov_available(char *providers)
3327 for (; isspace(*providers); providers++)
3329 if (*providers == '\0')
3330 break; /* End of the road */
3331 for (p = providers; *p != '\0' && !isspace(*p); p++)
3337 if (OSSL_PROVIDER_available(libctx, providers))
3338 return 1; /* Found one */
3343 /* Read and parse one test. Return 0 if failure, 1 if okay. */
3344 static int parse(EVP_TEST *t)
3346 KEY_LIST *key, **klist;
3349 int i, skip_availablein = 0;
3353 if (BIO_eof(t->s.fp))
3356 if (!test_readstanza(&t->s))
3358 } while (t->s.numpairs == 0);
3359 pp = &t->s.pairs[0];
3361 /* Are we adding a key? */
3365 if (strcmp(pp->key, "PrivateKey") == 0) {
3366 pkey = PEM_read_bio_PrivateKey_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3367 if (pkey == NULL && !key_unsupported()) {
3368 EVP_PKEY_free(pkey);
3369 TEST_info("Can't read private key %s", pp->value);
3370 TEST_openssl_errors();
3373 klist = &private_keys;
3374 } else if (strcmp(pp->key, "PublicKey") == 0) {
3375 pkey = PEM_read_bio_PUBKEY_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3376 if (pkey == NULL && !key_unsupported()) {
3377 EVP_PKEY_free(pkey);
3378 TEST_info("Can't read public key %s", pp->value);
3379 TEST_openssl_errors();
3382 klist = &public_keys;
3383 } else if (strcmp(pp->key, "PrivateKeyRaw") == 0
3384 || strcmp(pp->key, "PublicKeyRaw") == 0 ) {
3385 char *strnid = NULL, *keydata = NULL;
3386 unsigned char *keybin;
3390 if (strcmp(pp->key, "PrivateKeyRaw") == 0)
3391 klist = &private_keys;
3393 klist = &public_keys;
3395 strnid = strchr(pp->value, ':');
3396 if (strnid != NULL) {
3398 keydata = strchr(strnid, ':');
3399 if (keydata != NULL)
3402 if (keydata == NULL) {
3403 TEST_info("Failed to parse %s value", pp->key);
3407 nid = OBJ_txt2nid(strnid);
3408 if (nid == NID_undef) {
3409 TEST_info("Unrecognised algorithm NID");
3412 if (!parse_bin(keydata, &keybin, &keylen)) {
3413 TEST_info("Failed to create binary key");
3416 if (klist == &private_keys)
3417 pkey = EVP_PKEY_new_raw_private_key_ex(libctx, strnid, NULL, keybin,
3420 pkey = EVP_PKEY_new_raw_public_key_ex(libctx, strnid, NULL, keybin,
3422 if (pkey == NULL && !key_unsupported()) {
3423 TEST_info("Can't read %s data", pp->key);
3424 OPENSSL_free(keybin);
3425 TEST_openssl_errors();
3428 OPENSSL_free(keybin);
3429 } else if (strcmp(pp->key, "Availablein") == 0) {
3430 if (!prov_available(pp->value)) {
3431 TEST_info("skipping, '%s' provider not available: %s:%d",
3432 pp->value, t->s.test_file, t->s.start);
3441 /* If we have a key add to list */
3442 if (klist != NULL) {
3443 if (find_key(NULL, pp->value, *klist)) {
3444 TEST_info("Duplicate key %s", pp->value);
3447 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
3449 key->name = take_value(pp);
3454 /* Go back and start a new stanza. */
3455 if ((t->s.numpairs - skip_availablein) != 1)
3456 TEST_info("Line %d: missing blank line\n", t->s.curr);
3460 /* Find the test, based on first keyword. */
3461 if (!TEST_ptr(t->meth = find_test(pp->key)))
3463 if (!t->meth->init(t, pp->value)) {
3464 TEST_error("unknown %s: %s\n", pp->key, pp->value);
3468 /* TEST_info("skipping %s %s", pp->key, pp->value); */
3472 for (pp++, i = 1; i < (t->s.numpairs - skip_availablein); pp++, i++) {
3473 if (strcmp(pp->key, "Securitycheck") == 0) {
3474 #if defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
3476 if (!securitycheck_enabled())
3479 TEST_info("skipping, Securitycheck is disabled: %s:%d",
3480 t->s.test_file, t->s.start);
3484 } else if (strcmp(pp->key, "Availablein") == 0) {
3485 TEST_info("Line %d: 'Availablein' should be the first option",
3488 } else if (strcmp(pp->key, "Result") == 0) {
3489 if (t->expected_err != NULL) {
3490 TEST_info("Line %d: multiple result lines", t->s.curr);
3493 t->expected_err = take_value(pp);
3494 } else if (strcmp(pp->key, "Function") == 0) {
3495 /* Ignore old line. */
3496 } else if (strcmp(pp->key, "Reason") == 0) {
3497 if (t->reason != NULL) {
3498 TEST_info("Line %d: multiple reason lines", t->s.curr);
3501 t->reason = take_value(pp);
3503 /* Must be test specific line: try to parse it */
3504 int rv = t->meth->parse(t, pp->key, pp->value);
3507 TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key);
3511 TEST_info("Line %d: error processing keyword %s = %s\n",
3512 t->s.curr, pp->key, pp->value);
3521 static int run_file_tests(int i)
3524 const char *testfile = test_get_argument(i);
3527 if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t))))
3529 if (!test_start_file(&t->s, testfile)) {
3534 while (!BIO_eof(t->s.fp)) {
3540 if (c == 0 || !run_test(t)) {
3545 test_end_file(&t->s);
3548 free_key_list(public_keys);
3549 free_key_list(private_keys);
3556 const OPTIONS *test_get_options(void)
3558 static const OPTIONS test_options[] = {
3559 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
3560 { "config", OPT_CONFIG_FILE, '<',
3561 "The configuration file to use for the libctx" },
3562 { OPT_HELP_STR, 1, '-',
3563 "file\tFile to run tests on.\n" },
3566 return test_options;
3569 int setup_tests(void)
3572 char *config_file = NULL;
3576 while ((o = opt_next()) != OPT_EOF) {
3578 case OPT_CONFIG_FILE:
3579 config_file = opt_arg();
3581 case OPT_TEST_CASES:
3590 * Load the 'null' provider into the default library context to ensure that
3591 * the the tests do not fallback to using the default provider.
3593 prov_null = OSSL_PROVIDER_load(NULL, "null");
3594 if (prov_null == NULL) {
3595 opt_printf_stderr("Failed to load null provider into default libctx\n");
3599 /* load the provider via configuration into the created library context */
3600 libctx = OSSL_LIB_CTX_new();
3602 || !OSSL_LIB_CTX_load_config(libctx, config_file)) {
3603 TEST_error("Failed to load config %s\n", config_file);
3607 n = test_get_argument_count();
3611 ADD_ALL_TESTS(run_file_tests, n);
3615 void cleanup_tests(void)
3617 OSSL_PROVIDER_unload(prov_null);
3618 OSSL_LIB_CTX_free(libctx);
3621 #define STR_STARTS_WITH(str, pre) strncasecmp(pre, str, strlen(pre)) == 0
3622 #define STR_ENDS_WITH(str, pre) \
3623 strlen(str) < strlen(pre) ? 0 : (strcasecmp(pre, str + strlen(str) - strlen(pre)) == 0)
3625 static int is_digest_disabled(const char *name)
3627 #ifdef OPENSSL_NO_BLAKE2
3628 if (STR_STARTS_WITH(name, "BLAKE"))
3631 #ifdef OPENSSL_NO_MD2
3632 if (strcasecmp(name, "MD2") == 0)
3635 #ifdef OPENSSL_NO_MDC2
3636 if (strcasecmp(name, "MDC2") == 0)
3639 #ifdef OPENSSL_NO_MD4
3640 if (strcasecmp(name, "MD4") == 0)
3643 #ifdef OPENSSL_NO_MD5
3644 if (strcasecmp(name, "MD5") == 0)
3647 #ifdef OPENSSL_NO_RMD160
3648 if (strcasecmp(name, "RIPEMD160") == 0)
3651 #ifdef OPENSSL_NO_SM3
3652 if (strcasecmp(name, "SM3") == 0)
3655 #ifdef OPENSSL_NO_WHIRLPOOL
3656 if (strcasecmp(name, "WHIRLPOOL") == 0)
3662 static int is_pkey_disabled(const char *name)
3664 #ifdef OPENSSL_NO_RSA
3665 if (STR_STARTS_WITH(name, "RSA"))
3668 #ifdef OPENSSL_NO_EC
3669 if (STR_STARTS_WITH(name, "EC"))
3672 #ifdef OPENSSL_NO_DH
3673 if (STR_STARTS_WITH(name, "DH"))
3676 #ifdef OPENSSL_NO_DSA
3677 if (STR_STARTS_WITH(name, "DSA"))
3683 static int is_mac_disabled(const char *name)
3685 #ifdef OPENSSL_NO_BLAKE2
3686 if (STR_STARTS_WITH(name, "BLAKE2BMAC")
3687 || STR_STARTS_WITH(name, "BLAKE2SMAC"))
3690 #ifdef OPENSSL_NO_CMAC
3691 if (STR_STARTS_WITH(name, "CMAC"))
3694 #ifdef OPENSSL_NO_POLY1305
3695 if (STR_STARTS_WITH(name, "Poly1305"))
3698 #ifdef OPENSSL_NO_SIPHASH
3699 if (STR_STARTS_WITH(name, "SipHash"))
3704 static int is_kdf_disabled(const char *name)
3706 #ifdef OPENSSL_NO_SCRYPT
3707 if (STR_ENDS_WITH(name, "SCRYPT"))
3710 #ifdef OPENSSL_NO_CMS
3711 if (strcasecmp(name, "X942KDF") == 0)
3713 #endif /* OPENSSL_NO_CMS */
3717 static int is_cipher_disabled(const char *name)
3719 #ifdef OPENSSL_NO_ARIA
3720 if (STR_STARTS_WITH(name, "ARIA"))
3723 #ifdef OPENSSL_NO_BF
3724 if (STR_STARTS_WITH(name, "BF"))
3727 #ifdef OPENSSL_NO_CAMELLIA
3728 if (STR_STARTS_WITH(name, "CAMELLIA"))
3731 #ifdef OPENSSL_NO_CAST
3732 if (STR_STARTS_WITH(name, "CAST"))
3735 #ifdef OPENSSL_NO_CHACHA
3736 if (STR_STARTS_WITH(name, "CHACHA"))
3739 #ifdef OPENSSL_NO_POLY1305
3740 if (STR_ENDS_WITH(name, "Poly1305"))
3743 #ifdef OPENSSL_NO_DES
3744 if (STR_STARTS_WITH(name, "DES"))
3747 #ifdef OPENSSL_NO_OCB
3748 if (STR_ENDS_WITH(name, "OCB"))
3751 #ifdef OPENSSL_NO_IDEA
3752 if (STR_STARTS_WITH(name, "IDEA"))
3755 #ifdef OPENSSL_NO_RC2
3756 if (STR_STARTS_WITH(name, "RC2"))
3759 #ifdef OPENSSL_NO_RC4
3760 if (STR_STARTS_WITH(name, "RC4"))
3763 #ifdef OPENSSL_NO_RC5
3764 if (STR_STARTS_WITH(name, "RC5"))
3767 #ifdef OPENSSL_NO_SEED
3768 if (STR_STARTS_WITH(name, "SEED"))
3771 #ifdef OPENSSL_NO_SIV
3772 if (STR_ENDS_WITH(name, "SIV"))
3775 #ifdef OPENSSL_NO_SM4
3776 if (STR_STARTS_WITH(name, "SM4"))