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 "internal/numbers.h"
25 #include "internal/nelem.h"
26 #include "crypto/evp.h"
32 typedef struct evp_test_method_st EVP_TEST_METHOD;
34 /* Structure holding test information */
35 typedef struct evp_test_st {
36 STANZA s; /* Common test stanza */
38 int skip; /* Current test should be skipped */
39 const EVP_TEST_METHOD *meth; /* method for this test */
40 const char *err, *aux_err; /* Error string for test */
41 char *expected_err; /* Expected error value of test */
42 char *reason; /* Expected error reason string */
43 void *data; /* test specific data */
46 /* Test method structure */
47 struct evp_test_method_st {
48 /* Name of test as it appears in file */
50 /* Initialise test for "alg" */
51 int (*init) (EVP_TEST * t, const char *alg);
53 void (*cleanup) (EVP_TEST * t);
54 /* Test specific name value pair processing */
55 int (*parse) (EVP_TEST * t, const char *name, const char *value);
56 /* Run the test itself */
57 int (*run_test) (EVP_TEST * t);
60 /* Linked list of named keys. */
61 typedef struct key_list_st {
64 struct key_list_st *next;
67 typedef enum OPTION_choice {
74 static OSSL_PROVIDER *prov_null = NULL;
75 static OPENSSL_CTX *libctx = NULL;
77 /* List of public and private keys */
78 static KEY_LIST *private_keys;
79 static KEY_LIST *public_keys;
81 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst);
82 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen);
83 static int is_digest_disabled(const char *name);
84 static int is_pkey_disabled(const char *name);
85 static int is_mac_disabled(const char *name);
86 static int is_cipher_disabled(const char *name);
87 static int is_kdf_disabled(const char *name);
90 * Compare two memory regions for equality, returning zero if they differ.
91 * However, if there is expected to be an error and the actual error
92 * matches then the memory is expected to be different so handle this
93 * case without producing unnecessary test framework output.
95 static int memory_err_compare(EVP_TEST *t, const char *err,
96 const void *expected, size_t expected_len,
97 const void *got, size_t got_len)
101 if (t->expected_err != NULL && strcmp(t->expected_err, err) == 0)
102 r = !TEST_mem_ne(expected, expected_len, got, got_len);
104 r = TEST_mem_eq(expected, expected_len, got, got_len);
111 * Structure used to hold a list of blocks of memory to test
112 * calls to "update" like functions.
114 struct evp_test_buffer_st {
121 static void evp_test_buffer_free(EVP_TEST_BUFFER *db)
124 OPENSSL_free(db->buf);
129 /* append buffer to a list */
130 static int evp_test_buffer_append(const char *value,
131 STACK_OF(EVP_TEST_BUFFER) **sk)
133 EVP_TEST_BUFFER *db = NULL;
135 if (!TEST_ptr(db = OPENSSL_malloc(sizeof(*db))))
138 if (!parse_bin(value, &db->buf, &db->buflen))
143 if (*sk == NULL && !TEST_ptr(*sk = sk_EVP_TEST_BUFFER_new_null()))
145 if (!sk_EVP_TEST_BUFFER_push(*sk, db))
151 evp_test_buffer_free(db);
155 /* replace last buffer in list with copies of itself */
156 static int evp_test_buffer_ncopy(const char *value,
157 STACK_OF(EVP_TEST_BUFFER) *sk)
160 unsigned char *tbuf, *p;
162 int ncopy = atoi(value);
167 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
169 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
171 tbuflen = db->buflen * ncopy;
172 if (!TEST_ptr(tbuf = OPENSSL_malloc(tbuflen)))
174 for (i = 0, p = tbuf; i < ncopy; i++, p += db->buflen)
175 memcpy(p, db->buf, db->buflen);
177 OPENSSL_free(db->buf);
179 db->buflen = tbuflen;
183 /* set repeat count for last buffer in list */
184 static int evp_test_buffer_set_count(const char *value,
185 STACK_OF(EVP_TEST_BUFFER) *sk)
188 int count = atoi(value);
193 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
196 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
197 if (db->count_set != 0)
200 db->count = (size_t)count;
205 /* call "fn" with each element of the list in turn */
206 static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER) *sk,
208 const unsigned char *buf,
214 for (i = 0; i < sk_EVP_TEST_BUFFER_num(sk); i++) {
215 EVP_TEST_BUFFER *tb = sk_EVP_TEST_BUFFER_value(sk, i);
218 for (j = 0; j < tb->count; j++) {
219 if (fn(ctx, tb->buf, tb->buflen) <= 0)
227 * Unescape some sequences in string literals (only \n for now).
228 * Return an allocated buffer, set |out_len|. If |input_len|
229 * is zero, get an empty buffer but set length to zero.
231 static unsigned char* unescape(const char *input, size_t input_len,
234 unsigned char *ret, *p;
237 if (input_len == 0) {
239 return OPENSSL_zalloc(1);
242 /* Escaping is non-expanding; over-allocate original size for simplicity. */
243 if (!TEST_ptr(ret = p = OPENSSL_malloc(input_len)))
246 for (i = 0; i < input_len; i++) {
247 if (*input == '\\') {
248 if (i == input_len - 1 || *++input != 'n') {
249 TEST_error("Bad escape sequence in file");
269 * For a hex string "value" convert to a binary allocated buffer.
270 * Return 1 on success or 0 on failure.
272 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen)
276 /* Check for NULL literal */
277 if (strcmp(value, "NULL") == 0) {
283 /* Check for empty value */
284 if (*value == '\0') {
286 * Don't return NULL for zero length buffer. This is needed for
287 * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key
288 * buffer even if the key length is 0, in order to detect key reset.
290 *buf = OPENSSL_malloc(1);
298 /* Check for string literal */
299 if (value[0] == '"') {
300 size_t vlen = strlen(++value);
302 if (vlen == 0 || value[vlen - 1] != '"')
305 *buf = unescape(value, vlen, buflen);
306 return *buf == NULL ? 0 : 1;
309 /* Otherwise assume as hex literal and convert it to binary buffer */
310 if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) {
311 TEST_info("Can't convert %s", value);
312 TEST_openssl_errors();
315 /* Size of input buffer means we'll never overflow */
321 ** MESSAGE DIGEST TESTS
324 typedef struct digest_data_st {
325 /* Digest this test is for */
326 const EVP_MD *digest;
327 EVP_MD *fetched_digest;
328 /* Input to digest */
329 STACK_OF(EVP_TEST_BUFFER) *input;
330 /* Expected output */
331 unsigned char *output;
337 static int digest_test_init(EVP_TEST *t, const char *alg)
340 const EVP_MD *digest;
341 EVP_MD *fetched_digest;
343 if (is_digest_disabled(alg)) {
344 TEST_info("skipping, '%s' is disabled", alg);
349 if ((digest = fetched_digest = EVP_MD_fetch(libctx, alg, NULL)) == NULL
350 && (digest = EVP_get_digestbyname(alg)) == NULL)
352 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
355 mdat->digest = digest;
356 mdat->fetched_digest = fetched_digest;
358 if (fetched_digest != NULL)
359 TEST_info("%s is fetched", alg);
363 static void digest_test_cleanup(EVP_TEST *t)
365 DIGEST_DATA *mdat = t->data;
367 sk_EVP_TEST_BUFFER_pop_free(mdat->input, evp_test_buffer_free);
368 OPENSSL_free(mdat->output);
369 EVP_MD_free(mdat->fetched_digest);
372 static int digest_test_parse(EVP_TEST *t,
373 const char *keyword, const char *value)
375 DIGEST_DATA *mdata = t->data;
377 if (strcmp(keyword, "Input") == 0)
378 return evp_test_buffer_append(value, &mdata->input);
379 if (strcmp(keyword, "Output") == 0)
380 return parse_bin(value, &mdata->output, &mdata->output_len);
381 if (strcmp(keyword, "Count") == 0)
382 return evp_test_buffer_set_count(value, mdata->input);
383 if (strcmp(keyword, "Ncopy") == 0)
384 return evp_test_buffer_ncopy(value, mdata->input);
385 if (strcmp(keyword, "Padding") == 0)
386 return (mdata->pad_type = atoi(value)) > 0;
390 static int digest_update_fn(void *ctx, const unsigned char *buf, size_t buflen)
392 return EVP_DigestUpdate(ctx, buf, buflen);
395 static int digest_test_run(EVP_TEST *t)
397 DIGEST_DATA *expected = t->data;
399 unsigned char *got = NULL;
400 unsigned int got_len;
401 OSSL_PARAM params[2];
403 t->err = "TEST_FAILURE";
404 if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
407 got = OPENSSL_malloc(expected->output_len > EVP_MAX_MD_SIZE ?
408 expected->output_len : EVP_MAX_MD_SIZE);
412 if (!EVP_DigestInit_ex(mctx, expected->digest, NULL)) {
413 t->err = "DIGESTINIT_ERROR";
416 if (expected->pad_type > 0) {
417 params[0] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_PAD_TYPE,
418 &expected->pad_type);
419 params[1] = OSSL_PARAM_construct_end();
420 if (!TEST_int_gt(EVP_MD_CTX_set_params(mctx, params), 0)) {
421 t->err = "PARAMS_ERROR";
425 if (!evp_test_buffer_do(expected->input, digest_update_fn, mctx)) {
426 t->err = "DIGESTUPDATE_ERROR";
430 if (EVP_MD_flags(expected->digest) & EVP_MD_FLAG_XOF) {
431 EVP_MD_CTX *mctx_cpy;
432 char dont[] = "touch";
434 if (!TEST_ptr(mctx_cpy = EVP_MD_CTX_new())) {
437 if (!EVP_MD_CTX_copy(mctx_cpy, mctx)) {
438 EVP_MD_CTX_free(mctx_cpy);
441 if (!EVP_DigestFinalXOF(mctx_cpy, (unsigned char *)dont, 0)) {
442 EVP_MD_CTX_free(mctx_cpy);
443 t->err = "DIGESTFINALXOF_ERROR";
446 if (!TEST_str_eq(dont, "touch")) {
447 EVP_MD_CTX_free(mctx_cpy);
448 t->err = "DIGESTFINALXOF_ERROR";
451 EVP_MD_CTX_free(mctx_cpy);
453 got_len = expected->output_len;
454 if (!EVP_DigestFinalXOF(mctx, got, got_len)) {
455 t->err = "DIGESTFINALXOF_ERROR";
459 if (!EVP_DigestFinal(mctx, got, &got_len)) {
460 t->err = "DIGESTFINAL_ERROR";
464 if (!TEST_int_eq(expected->output_len, got_len)) {
465 t->err = "DIGEST_LENGTH_MISMATCH";
468 if (!memory_err_compare(t, "DIGEST_MISMATCH",
469 expected->output, expected->output_len,
477 EVP_MD_CTX_free(mctx);
481 static const EVP_TEST_METHOD digest_test_method = {
493 typedef struct cipher_data_st {
494 const EVP_CIPHER *cipher;
495 EVP_CIPHER *fetched_cipher;
497 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
501 size_t key_bits; /* Used by RC2 */
505 unsigned char *plaintext;
506 size_t plaintext_len;
507 unsigned char *ciphertext;
508 size_t ciphertext_len;
509 /* GCM, CCM, OCB and SIV only */
510 unsigned char *aad[AAD_NUM];
511 size_t aad_len[AAD_NUM];
513 const char *cts_mode;
518 static int cipher_test_init(EVP_TEST *t, const char *alg)
520 const EVP_CIPHER *cipher;
521 EVP_CIPHER *fetched_cipher;
525 if (is_cipher_disabled(alg)) {
527 TEST_info("skipping, '%s' is disabled", alg);
531 if ((cipher = fetched_cipher = EVP_CIPHER_fetch(libctx, alg, NULL)) == NULL
532 && (cipher = EVP_get_cipherbyname(alg)) == NULL)
535 cdat = OPENSSL_zalloc(sizeof(*cdat));
536 cdat->cipher = cipher;
537 cdat->fetched_cipher = fetched_cipher;
539 m = EVP_CIPHER_mode(cipher);
540 if (m == EVP_CIPH_GCM_MODE
541 || m == EVP_CIPH_OCB_MODE
542 || m == EVP_CIPH_SIV_MODE
543 || m == EVP_CIPH_CCM_MODE)
545 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
551 if (fetched_cipher != NULL)
552 TEST_info("%s is fetched", alg);
556 static void cipher_test_cleanup(EVP_TEST *t)
559 CIPHER_DATA *cdat = t->data;
561 OPENSSL_free(cdat->key);
562 OPENSSL_free(cdat->iv);
563 OPENSSL_free(cdat->ciphertext);
564 OPENSSL_free(cdat->plaintext);
565 for (i = 0; i < AAD_NUM; i++)
566 OPENSSL_free(cdat->aad[i]);
567 OPENSSL_free(cdat->tag);
568 EVP_CIPHER_free(cdat->fetched_cipher);
571 static int cipher_test_parse(EVP_TEST *t, const char *keyword,
574 CIPHER_DATA *cdat = t->data;
577 if (strcmp(keyword, "Key") == 0)
578 return parse_bin(value, &cdat->key, &cdat->key_len);
579 if (strcmp(keyword, "Rounds") == 0) {
583 cdat->rounds = (unsigned int)i;
586 if (strcmp(keyword, "IV") == 0)
587 return parse_bin(value, &cdat->iv, &cdat->iv_len);
588 if (strcmp(keyword, "Plaintext") == 0)
589 return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len);
590 if (strcmp(keyword, "Ciphertext") == 0)
591 return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
592 if (strcmp(keyword, "KeyBits") == 0) {
596 cdat->key_bits = (size_t)i;
600 if (strcmp(keyword, "AAD") == 0) {
601 for (i = 0; i < AAD_NUM; i++) {
602 if (cdat->aad[i] == NULL)
603 return parse_bin(value, &cdat->aad[i], &cdat->aad_len[i]);
607 if (strcmp(keyword, "Tag") == 0)
608 return parse_bin(value, &cdat->tag, &cdat->tag_len);
609 if (strcmp(keyword, "SetTagLate") == 0) {
610 if (strcmp(value, "TRUE") == 0)
612 else if (strcmp(value, "FALSE") == 0)
620 if (strcmp(keyword, "Operation") == 0) {
621 if (strcmp(value, "ENCRYPT") == 0)
623 else if (strcmp(value, "DECRYPT") == 0)
629 if (strcmp(keyword, "CTSMode") == 0) {
630 cdat->cts_mode = value;
636 static int cipher_test_enc(EVP_TEST *t, int enc,
637 size_t out_misalign, size_t inp_misalign, int frag)
639 CIPHER_DATA *expected = t->data;
640 unsigned char *in, *expected_out, *tmp = NULL;
641 size_t in_len, out_len, donelen = 0;
642 int ok = 0, tmplen, chunklen, tmpflen, i;
643 EVP_CIPHER_CTX *ctx_base = NULL;
644 EVP_CIPHER_CTX *ctx = NULL;
646 t->err = "TEST_FAILURE";
647 if (!TEST_ptr(ctx_base = EVP_CIPHER_CTX_new()))
649 if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
651 EVP_CIPHER_CTX_set_flags(ctx_base, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
653 in = expected->plaintext;
654 in_len = expected->plaintext_len;
655 expected_out = expected->ciphertext;
656 out_len = expected->ciphertext_len;
658 in = expected->ciphertext;
659 in_len = expected->ciphertext_len;
660 expected_out = expected->plaintext;
661 out_len = expected->plaintext_len;
663 if (inp_misalign == (size_t)-1) {
664 /* Exercise in-place encryption */
665 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
668 in = memcpy(tmp + out_misalign, in, in_len);
670 inp_misalign += 16 - ((out_misalign + in_len) & 15);
672 * 'tmp' will store both output and copy of input. We make the copy
673 * of input to specifically aligned part of 'tmp'. So we just
674 * figured out how much padding would ensure the required alignment,
675 * now we allocate extended buffer and finally copy the input just
676 * past inp_misalign in expression below. Output will be written
677 * past out_misalign...
679 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
680 inp_misalign + in_len);
683 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
684 inp_misalign, in, in_len);
686 if (!EVP_CipherInit_ex(ctx_base, expected->cipher, NULL, NULL, NULL, enc)) {
687 t->err = "CIPHERINIT_ERROR";
690 if (expected->cts_mode != NULL) {
691 OSSL_PARAM params[2];
693 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE,
694 (char *)expected->cts_mode,
696 params[1] = OSSL_PARAM_construct_end();
697 if (!EVP_CIPHER_CTX_set_params(ctx_base, params)) {
698 t->err = "INVALID_CTS_MODE";
703 if (expected->aead) {
704 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_IVLEN,
705 expected->iv_len, 0)) {
706 t->err = "INVALID_IV_LENGTH";
709 } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx_base)) {
710 t->err = "INVALID_IV_LENGTH";
714 if (expected->aead) {
717 * If encrypting or OCB just set tag length initially, otherwise
718 * set tag length and value.
720 if (enc || expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late) {
721 t->err = "TAG_LENGTH_SET_ERROR";
724 t->err = "TAG_SET_ERROR";
727 if (tag || expected->aead != EVP_CIPH_GCM_MODE) {
728 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_TAG,
729 expected->tag_len, tag))
734 if (expected->rounds > 0) {
735 int rounds = (int)expected->rounds;
737 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC5_ROUNDS, rounds, NULL)) {
738 t->err = "INVALID_ROUNDS";
743 if (!EVP_CIPHER_CTX_set_key_length(ctx_base, expected->key_len)) {
744 t->err = "INVALID_KEY_LENGTH";
747 if (expected->key_bits > 0) {
748 int bits = (int)expected->key_bits;
750 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC2_KEY_BITS, bits, NULL)) {
751 t->err = "INVALID KEY BITS";
755 if (!EVP_CipherInit_ex(ctx_base, NULL, NULL, expected->key, expected->iv, -1)) {
756 t->err = "KEY_SET_ERROR";
760 /* Check that we get the same IV back */
761 if (expected->iv != NULL) {
762 /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
763 unsigned char iv[128];
764 if (!TEST_true(EVP_CIPHER_CTX_get_iv_state(ctx_base, iv, sizeof(iv)))
765 || ((EVP_CIPHER_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
766 && !TEST_mem_eq(expected->iv, expected->iv_len, iv,
767 expected->iv_len))) {
768 t->err = "INVALID_IV";
773 /* Test that the cipher dup functions correctly if it is supported */
774 if (EVP_CIPHER_CTX_copy(ctx, ctx_base)) {
775 EVP_CIPHER_CTX_free(ctx_base);
778 EVP_CIPHER_CTX_free(ctx);
782 if (expected->aead == EVP_CIPH_CCM_MODE) {
783 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
784 t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
788 if (expected->aad[0] != NULL) {
789 t->err = "AAD_SET_ERROR";
791 for (i = 0; expected->aad[i] != NULL; i++) {
792 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i],
793 expected->aad_len[i]))
798 * Supply the AAD in chunks less than the block size where possible
800 for (i = 0; expected->aad[i] != NULL; i++) {
801 if (expected->aad_len[i] > 0) {
802 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], 1))
806 if (expected->aad_len[i] > 2) {
807 if (!EVP_CipherUpdate(ctx, NULL, &chunklen,
808 expected->aad[i] + donelen,
809 expected->aad_len[i] - 2))
811 donelen += expected->aad_len[i] - 2;
813 if (expected->aad_len[i] > 1
814 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
815 expected->aad[i] + donelen, 1))
821 if (!enc && (expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late)) {
822 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
823 expected->tag_len, expected->tag)) {
824 t->err = "TAG_SET_ERROR";
829 EVP_CIPHER_CTX_set_padding(ctx, 0);
830 t->err = "CIPHERUPDATE_ERROR";
833 /* We supply the data all in one go */
834 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
837 /* Supply the data in chunks less than the block size where possible */
839 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
846 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
854 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
860 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
861 t->err = "CIPHERFINAL_ERROR";
864 if (!memory_err_compare(t, "VALUE_MISMATCH", expected_out, out_len,
865 tmp + out_misalign, tmplen + tmpflen))
867 if (enc && expected->aead) {
868 unsigned char rtag[16];
870 if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) {
871 t->err = "TAG_LENGTH_INTERNAL_ERROR";
874 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
875 expected->tag_len, rtag)) {
876 t->err = "TAG_RETRIEVE_ERROR";
879 if (!memory_err_compare(t, "TAG_VALUE_MISMATCH",
880 expected->tag, expected->tag_len,
881 rtag, expected->tag_len))
889 EVP_CIPHER_CTX_free(ctx_base);
890 EVP_CIPHER_CTX_free(ctx);
894 static int cipher_test_run(EVP_TEST *t)
896 CIPHER_DATA *cdat = t->data;
898 size_t out_misalign, inp_misalign;
904 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
905 /* IV is optional and usually omitted in wrap mode */
906 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
911 if (cdat->aead && !cdat->tag) {
915 for (out_misalign = 0; out_misalign <= 1;) {
916 static char aux_err[64];
917 t->aux_err = aux_err;
918 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
919 if (inp_misalign == (size_t)-1) {
920 /* kludge: inp_misalign == -1 means "exercise in-place" */
921 BIO_snprintf(aux_err, sizeof(aux_err),
922 "%s in-place, %sfragmented",
923 out_misalign ? "misaligned" : "aligned",
926 BIO_snprintf(aux_err, sizeof(aux_err),
927 "%s output and %s input, %sfragmented",
928 out_misalign ? "misaligned" : "aligned",
929 inp_misalign ? "misaligned" : "aligned",
933 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
934 /* Not fatal errors: return */
941 if (cdat->enc != 1) {
942 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
943 /* Not fatal errors: return */
952 if (out_misalign == 1 && frag == 0) {
954 * XTS, SIV, CCM and Wrap modes have special requirements about input
955 * lengths so we don't fragment for those
957 if (cdat->aead == EVP_CIPH_CCM_MODE
958 || ((EVP_CIPHER_flags(cdat->cipher) & EVP_CIPH_FLAG_CTS) != 0)
959 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_SIV_MODE
960 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
961 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
974 static const EVP_TEST_METHOD cipher_test_method = {
987 typedef struct mac_data_st {
988 /* MAC type in one form or another */
990 EVP_MAC *mac; /* for mac_test_run_mac */
991 int type; /* for mac_test_run_pkey */
992 /* Algorithm string for this MAC */
1001 unsigned char *input;
1003 /* Expected output */
1004 unsigned char *output;
1006 unsigned char *custom;
1008 /* MAC salt (blake2) */
1009 unsigned char *salt;
1011 /* Collection of controls */
1012 STACK_OF(OPENSSL_STRING) *controls;
1015 static int mac_test_init(EVP_TEST *t, const char *alg)
1017 EVP_MAC *mac = NULL;
1018 int type = NID_undef;
1021 if (is_mac_disabled(alg)) {
1022 TEST_info("skipping, '%s' is disabled", alg);
1026 if ((mac = EVP_MAC_fetch(libctx, alg, NULL)) == NULL) {
1028 * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
1029 * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
1030 * the EVP_PKEY method.
1032 size_t sz = strlen(alg);
1033 static const char epilogue[] = " by EVP_PKEY";
1035 if (sz >= sizeof(epilogue)
1036 && strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0)
1037 sz -= sizeof(epilogue) - 1;
1039 if (strncmp(alg, "HMAC", sz) == 0)
1040 type = EVP_PKEY_HMAC;
1041 else if (strncmp(alg, "CMAC", sz) == 0)
1042 type = EVP_PKEY_CMAC;
1043 else if (strncmp(alg, "Poly1305", sz) == 0)
1044 type = EVP_PKEY_POLY1305;
1045 else if (strncmp(alg, "SipHash", sz) == 0)
1046 type = EVP_PKEY_SIPHASH;
1051 mdat = OPENSSL_zalloc(sizeof(*mdat));
1053 mdat->mac_name = OPENSSL_strdup(alg);
1055 mdat->controls = sk_OPENSSL_STRING_new_null();
1060 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
1061 static void openssl_free(char *m)
1066 static void mac_test_cleanup(EVP_TEST *t)
1068 MAC_DATA *mdat = t->data;
1070 EVP_MAC_free(mdat->mac);
1071 OPENSSL_free(mdat->mac_name);
1072 sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free);
1073 OPENSSL_free(mdat->alg);
1074 OPENSSL_free(mdat->key);
1075 OPENSSL_free(mdat->iv);
1076 OPENSSL_free(mdat->custom);
1077 OPENSSL_free(mdat->salt);
1078 OPENSSL_free(mdat->input);
1079 OPENSSL_free(mdat->output);
1082 static int mac_test_parse(EVP_TEST *t,
1083 const char *keyword, const char *value)
1085 MAC_DATA *mdata = t->data;
1087 if (strcmp(keyword, "Key") == 0)
1088 return parse_bin(value, &mdata->key, &mdata->key_len);
1089 if (strcmp(keyword, "IV") == 0)
1090 return parse_bin(value, &mdata->iv, &mdata->iv_len);
1091 if (strcmp(keyword, "Custom") == 0)
1092 return parse_bin(value, &mdata->custom, &mdata->custom_len);
1093 if (strcmp(keyword, "Salt") == 0)
1094 return parse_bin(value, &mdata->salt, &mdata->salt_len);
1095 if (strcmp(keyword, "Algorithm") == 0) {
1096 mdata->alg = OPENSSL_strdup(value);
1101 if (strcmp(keyword, "Input") == 0)
1102 return parse_bin(value, &mdata->input, &mdata->input_len);
1103 if (strcmp(keyword, "Output") == 0)
1104 return parse_bin(value, &mdata->output, &mdata->output_len);
1105 if (strcmp(keyword, "Ctrl") == 0)
1106 return sk_OPENSSL_STRING_push(mdata->controls,
1107 OPENSSL_strdup(value)) != 0;
1111 static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1117 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1119 p = strchr(tmpval, ':');
1122 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1124 t->err = "PKEY_CTRL_INVALID";
1126 t->err = "PKEY_CTRL_ERROR";
1129 OPENSSL_free(tmpval);
1133 static int mac_test_run_pkey(EVP_TEST *t)
1135 MAC_DATA *expected = t->data;
1136 EVP_MD_CTX *mctx = NULL;
1137 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1138 EVP_PKEY *key = NULL;
1139 const char *mdname = NULL;
1140 EVP_CIPHER *cipher = NULL;
1141 unsigned char *got = NULL;
1145 if (expected->alg == NULL)
1146 TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type));
1148 TEST_info("Trying the EVP_PKEY %s test with %s",
1149 OBJ_nid2sn(expected->type), expected->alg);
1151 if (expected->type == EVP_PKEY_CMAC) {
1152 if (expected->alg != NULL && is_cipher_disabled(expected->alg)) {
1153 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
1158 if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, expected->alg, NULL))) {
1159 t->err = "MAC_KEY_CREATE_ERROR";
1162 key = EVP_PKEY_new_CMAC_key_with_libctx(expected->key,
1164 EVP_CIPHER_name(cipher),
1167 key = EVP_PKEY_new_raw_private_key_with_libctx(libctx,
1168 OBJ_nid2sn(expected->type),
1169 NULL, expected->key,
1173 t->err = "MAC_KEY_CREATE_ERROR";
1177 if (expected->type == EVP_PKEY_HMAC && expected->alg != NULL) {
1178 if (is_digest_disabled(expected->alg)) {
1179 TEST_info("skipping, HMAC '%s' is disabled", expected->alg);
1184 mdname = expected->alg;
1186 if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
1187 t->err = "INTERNAL_ERROR";
1190 if (!EVP_DigestSignInit_with_libctx(mctx, &pctx, mdname, libctx, NULL, key)) {
1191 t->err = "DIGESTSIGNINIT_ERROR";
1194 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++)
1195 if (!mac_test_ctrl_pkey(t, pctx,
1196 sk_OPENSSL_STRING_value(expected->controls,
1198 t->err = "EVPPKEYCTXCTRL_ERROR";
1201 if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) {
1202 t->err = "DIGESTSIGNUPDATE_ERROR";
1205 if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) {
1206 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1209 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1210 t->err = "TEST_FAILURE";
1213 if (!EVP_DigestSignFinal(mctx, got, &got_len)
1214 || !memory_err_compare(t, "TEST_MAC_ERR",
1215 expected->output, expected->output_len,
1217 t->err = "TEST_MAC_ERR";
1222 EVP_CIPHER_free(cipher);
1223 EVP_MD_CTX_free(mctx);
1225 EVP_PKEY_CTX_free(genctx);
1230 static int mac_test_run_mac(EVP_TEST *t)
1232 MAC_DATA *expected = t->data;
1233 EVP_MAC_CTX *ctx = NULL;
1234 unsigned char *got = NULL;
1237 OSSL_PARAM params[21];
1238 size_t params_n = 0;
1239 size_t params_n_allocstart = 0;
1240 const OSSL_PARAM *defined_params =
1241 EVP_MAC_settable_ctx_params(expected->mac);
1243 if (expected->alg == NULL)
1244 TEST_info("Trying the EVP_MAC %s test", expected->mac_name);
1246 TEST_info("Trying the EVP_MAC %s test with %s",
1247 expected->mac_name, expected->alg);
1249 if (expected->alg != NULL) {
1251 * The underlying algorithm may be a cipher or a digest.
1252 * We don't know which it is, but we can ask the MAC what it
1253 * should be and bet on that.
1255 if (OSSL_PARAM_locate_const(defined_params,
1256 OSSL_MAC_PARAM_CIPHER) != NULL) {
1257 params[params_n++] =
1258 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
1260 } else if (OSSL_PARAM_locate_const(defined_params,
1261 OSSL_MAC_PARAM_DIGEST) != NULL) {
1262 params[params_n++] =
1263 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
1266 t->err = "MAC_BAD_PARAMS";
1270 if (expected->key != NULL)
1271 params[params_n++] =
1272 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
1275 if (expected->custom != NULL)
1276 params[params_n++] =
1277 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
1279 expected->custom_len);
1280 if (expected->salt != NULL)
1281 params[params_n++] =
1282 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT,
1284 expected->salt_len);
1285 if (expected->iv != NULL)
1286 params[params_n++] =
1287 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
1291 /* Unknown controls. They must match parameters that the MAC recognizes */
1292 if (params_n + sk_OPENSSL_STRING_num(expected->controls)
1293 >= OSSL_NELEM(params)) {
1294 t->err = "MAC_TOO_MANY_PARAMETERS";
1297 params_n_allocstart = params_n;
1298 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) {
1299 char *tmpkey, *tmpval;
1300 char *value = sk_OPENSSL_STRING_value(expected->controls, i);
1302 if (!TEST_ptr(tmpkey = OPENSSL_strdup(value))) {
1303 t->err = "MAC_PARAM_ERROR";
1306 tmpval = strchr(tmpkey, ':');
1311 || !OSSL_PARAM_allocate_from_text(¶ms[params_n],
1314 strlen(tmpval), NULL)) {
1315 OPENSSL_free(tmpkey);
1316 t->err = "MAC_PARAM_ERROR";
1321 OPENSSL_free(tmpkey);
1323 params[params_n] = OSSL_PARAM_construct_end();
1325 if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) {
1326 t->err = "MAC_CREATE_ERROR";
1330 if (!EVP_MAC_CTX_set_params(ctx, params)) {
1331 t->err = "MAC_BAD_PARAMS";
1334 if (!EVP_MAC_init(ctx)) {
1335 t->err = "MAC_INIT_ERROR";
1338 if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) {
1339 t->err = "MAC_UPDATE_ERROR";
1342 if (!EVP_MAC_final(ctx, NULL, &got_len, 0)) {
1343 t->err = "MAC_FINAL_LENGTH_ERROR";
1346 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1347 t->err = "TEST_FAILURE";
1350 if (!EVP_MAC_final(ctx, got, &got_len, got_len)
1351 || !memory_err_compare(t, "TEST_MAC_ERR",
1352 expected->output, expected->output_len,
1354 t->err = "TEST_MAC_ERR";
1359 while (params_n-- > params_n_allocstart) {
1360 OPENSSL_free(params[params_n].data);
1362 EVP_MAC_CTX_free(ctx);
1367 static int mac_test_run(EVP_TEST *t)
1369 MAC_DATA *expected = t->data;
1371 if (expected->mac != NULL)
1372 return mac_test_run_mac(t);
1373 return mac_test_run_pkey(t);
1376 static const EVP_TEST_METHOD mac_test_method = {
1387 ** These are all very similar and share much common code.
1390 typedef struct pkey_data_st {
1391 /* Context for this operation */
1393 /* Key operation to perform */
1394 int (*keyop) (EVP_PKEY_CTX *ctx,
1395 unsigned char *sig, size_t *siglen,
1396 const unsigned char *tbs, size_t tbslen);
1398 unsigned char *input;
1400 /* Expected output */
1401 unsigned char *output;
1406 * Perform public key operation setup: lookup key, allocated ctx and call
1407 * the appropriate initialisation function
1409 static int pkey_test_init(EVP_TEST *t, const char *name,
1411 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1412 int (*keyop)(EVP_PKEY_CTX *ctx,
1413 unsigned char *sig, size_t *siglen,
1414 const unsigned char *tbs,
1418 EVP_PKEY *pkey = NULL;
1422 rv = find_key(&pkey, name, public_keys);
1424 rv = find_key(&pkey, name, private_keys);
1425 if (rv == 0 || pkey == NULL) {
1426 TEST_info("skipping, key '%s' is disabled", name);
1431 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) {
1432 EVP_PKEY_free(pkey);
1435 kdata->keyop = keyop;
1436 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL))) {
1437 EVP_PKEY_free(pkey);
1438 OPENSSL_free(kdata);
1441 if (keyopinit(kdata->ctx) <= 0)
1442 t->err = "KEYOP_INIT_ERROR";
1447 static void pkey_test_cleanup(EVP_TEST *t)
1449 PKEY_DATA *kdata = t->data;
1451 OPENSSL_free(kdata->input);
1452 OPENSSL_free(kdata->output);
1453 EVP_PKEY_CTX_free(kdata->ctx);
1456 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1462 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1464 p = strchr(tmpval, ':');
1467 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1469 t->err = "PKEY_CTRL_INVALID";
1471 } else if (p != NULL && rv <= 0) {
1472 if (is_digest_disabled(p) || is_cipher_disabled(p)) {
1473 TEST_info("skipping, '%s' is disabled", p);
1477 t->err = "PKEY_CTRL_ERROR";
1481 OPENSSL_free(tmpval);
1485 static int pkey_test_parse(EVP_TEST *t,
1486 const char *keyword, const char *value)
1488 PKEY_DATA *kdata = t->data;
1489 if (strcmp(keyword, "Input") == 0)
1490 return parse_bin(value, &kdata->input, &kdata->input_len);
1491 if (strcmp(keyword, "Output") == 0)
1492 return parse_bin(value, &kdata->output, &kdata->output_len);
1493 if (strcmp(keyword, "Ctrl") == 0)
1494 return pkey_test_ctrl(t, kdata->ctx, value);
1498 static int pkey_test_run(EVP_TEST *t)
1500 PKEY_DATA *expected = t->data;
1501 unsigned char *got = NULL;
1503 EVP_PKEY_CTX *copy = NULL;
1505 if (expected->keyop(expected->ctx, NULL, &got_len,
1506 expected->input, expected->input_len) <= 0
1507 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1508 t->err = "KEYOP_LENGTH_ERROR";
1511 if (expected->keyop(expected->ctx, got, &got_len,
1512 expected->input, expected->input_len) <= 0) {
1513 t->err = "KEYOP_ERROR";
1516 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1517 expected->output, expected->output_len,
1525 /* Repeat the test on a copy. */
1526 if (!TEST_ptr(copy = EVP_PKEY_CTX_dup(expected->ctx))) {
1527 t->err = "INTERNAL_ERROR";
1530 if (expected->keyop(copy, NULL, &got_len, expected->input,
1531 expected->input_len) <= 0
1532 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1533 t->err = "KEYOP_LENGTH_ERROR";
1536 if (expected->keyop(copy, got, &got_len, expected->input,
1537 expected->input_len) <= 0) {
1538 t->err = "KEYOP_ERROR";
1541 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1542 expected->output, expected->output_len,
1548 EVP_PKEY_CTX_free(copy);
1552 static int sign_test_init(EVP_TEST *t, const char *name)
1554 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1557 static const EVP_TEST_METHOD psign_test_method = {
1565 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1567 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1568 EVP_PKEY_verify_recover);
1571 static const EVP_TEST_METHOD pverify_recover_test_method = {
1573 verify_recover_test_init,
1579 static int decrypt_test_init(EVP_TEST *t, const char *name)
1581 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1585 static const EVP_TEST_METHOD pdecrypt_test_method = {
1593 static int verify_test_init(EVP_TEST *t, const char *name)
1595 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1598 static int verify_test_run(EVP_TEST *t)
1600 PKEY_DATA *kdata = t->data;
1602 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1603 kdata->input, kdata->input_len) <= 0)
1604 t->err = "VERIFY_ERROR";
1608 static const EVP_TEST_METHOD pverify_test_method = {
1616 static int pderive_test_init(EVP_TEST *t, const char *name)
1618 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1621 static int pderive_test_parse(EVP_TEST *t,
1622 const char *keyword, const char *value)
1624 PKEY_DATA *kdata = t->data;
1626 if (strcmp(keyword, "PeerKey") == 0) {
1628 if (find_key(&peer, value, public_keys) == 0)
1630 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1634 if (strcmp(keyword, "SharedSecret") == 0)
1635 return parse_bin(value, &kdata->output, &kdata->output_len);
1636 if (strcmp(keyword, "Ctrl") == 0)
1637 return pkey_test_ctrl(t, kdata->ctx, value);
1641 static int pderive_test_run(EVP_TEST *t)
1643 PKEY_DATA *expected = t->data;
1644 unsigned char *got = NULL;
1647 if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) {
1648 t->err = "DERIVE_ERROR";
1651 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1652 t->err = "DERIVE_ERROR";
1655 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
1656 t->err = "DERIVE_ERROR";
1659 if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH",
1660 expected->output, expected->output_len,
1670 static const EVP_TEST_METHOD pderive_test_method = {
1683 typedef enum pbe_type_enum {
1684 PBE_TYPE_INVALID = 0,
1685 PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
1688 typedef struct pbe_data_st {
1690 /* scrypt parameters */
1691 uint64_t N, r, p, maxmem;
1692 /* PKCS#12 parameters */
1696 unsigned char *pass;
1699 unsigned char *salt;
1701 /* Expected output */
1706 #ifndef OPENSSL_NO_SCRYPT
1707 /* Parse unsigned decimal 64 bit integer value */
1708 static int parse_uint64(const char *value, uint64_t *pr)
1710 const char *p = value;
1712 if (!TEST_true(*p)) {
1713 TEST_info("Invalid empty integer value");
1716 for (*pr = 0; *p; ) {
1717 if (*pr > UINT64_MAX / 10) {
1718 TEST_error("Integer overflow in string %s", value);
1722 if (!TEST_true(isdigit((unsigned char)*p))) {
1723 TEST_error("Invalid character in string %s", value);
1732 static int scrypt_test_parse(EVP_TEST *t,
1733 const char *keyword, const char *value)
1735 PBE_DATA *pdata = t->data;
1737 if (strcmp(keyword, "N") == 0)
1738 return parse_uint64(value, &pdata->N);
1739 if (strcmp(keyword, "p") == 0)
1740 return parse_uint64(value, &pdata->p);
1741 if (strcmp(keyword, "r") == 0)
1742 return parse_uint64(value, &pdata->r);
1743 if (strcmp(keyword, "maxmem") == 0)
1744 return parse_uint64(value, &pdata->maxmem);
1749 static int pbkdf2_test_parse(EVP_TEST *t,
1750 const char *keyword, const char *value)
1752 PBE_DATA *pdata = t->data;
1754 if (strcmp(keyword, "iter") == 0) {
1755 pdata->iter = atoi(value);
1756 if (pdata->iter <= 0)
1760 if (strcmp(keyword, "MD") == 0) {
1761 pdata->md = EVP_get_digestbyname(value);
1762 if (pdata->md == NULL)
1769 static int pkcs12_test_parse(EVP_TEST *t,
1770 const char *keyword, const char *value)
1772 PBE_DATA *pdata = t->data;
1774 if (strcmp(keyword, "id") == 0) {
1775 pdata->id = atoi(value);
1780 return pbkdf2_test_parse(t, keyword, value);
1783 static int pbe_test_init(EVP_TEST *t, const char *alg)
1786 PBE_TYPE pbe_type = PBE_TYPE_INVALID;
1788 if (is_kdf_disabled(alg)) {
1789 TEST_info("skipping, '%s' is disabled", alg);
1793 if (strcmp(alg, "scrypt") == 0) {
1794 pbe_type = PBE_TYPE_SCRYPT;
1795 } else if (strcmp(alg, "pbkdf2") == 0) {
1796 pbe_type = PBE_TYPE_PBKDF2;
1797 } else if (strcmp(alg, "pkcs12") == 0) {
1798 pbe_type = PBE_TYPE_PKCS12;
1800 TEST_error("Unknown pbe algorithm %s", alg);
1802 pdat = OPENSSL_zalloc(sizeof(*pdat));
1803 pdat->pbe_type = pbe_type;
1808 static void pbe_test_cleanup(EVP_TEST *t)
1810 PBE_DATA *pdat = t->data;
1812 OPENSSL_free(pdat->pass);
1813 OPENSSL_free(pdat->salt);
1814 OPENSSL_free(pdat->key);
1817 static int pbe_test_parse(EVP_TEST *t,
1818 const char *keyword, const char *value)
1820 PBE_DATA *pdata = t->data;
1822 if (strcmp(keyword, "Password") == 0)
1823 return parse_bin(value, &pdata->pass, &pdata->pass_len);
1824 if (strcmp(keyword, "Salt") == 0)
1825 return parse_bin(value, &pdata->salt, &pdata->salt_len);
1826 if (strcmp(keyword, "Key") == 0)
1827 return parse_bin(value, &pdata->key, &pdata->key_len);
1828 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1829 return pbkdf2_test_parse(t, keyword, value);
1830 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1831 return pkcs12_test_parse(t, keyword, value);
1832 #ifndef OPENSSL_NO_SCRYPT
1833 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1834 return scrypt_test_parse(t, keyword, value);
1839 static int pbe_test_run(EVP_TEST *t)
1841 PBE_DATA *expected = t->data;
1843 EVP_MD *fetched_digest = NULL;
1844 OPENSSL_CTX *save_libctx;
1846 save_libctx = OPENSSL_CTX_set0_default(libctx);
1848 if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
1849 t->err = "INTERNAL_ERROR";
1852 if (expected->pbe_type == PBE_TYPE_PBKDF2) {
1853 if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
1854 expected->salt, expected->salt_len,
1855 expected->iter, expected->md,
1856 expected->key_len, key) == 0) {
1857 t->err = "PBKDF2_ERROR";
1860 #ifndef OPENSSL_NO_SCRYPT
1861 } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
1862 if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
1863 expected->salt, expected->salt_len,
1864 expected->N, expected->r, expected->p,
1865 expected->maxmem, key, expected->key_len) == 0) {
1866 t->err = "SCRYPT_ERROR";
1870 } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
1871 fetched_digest = EVP_MD_fetch(libctx, EVP_MD_name(expected->md), NULL);
1872 if (fetched_digest == NULL) {
1873 t->err = "PKCS12_ERROR";
1876 if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
1877 expected->salt, expected->salt_len,
1878 expected->id, expected->iter, expected->key_len,
1879 key, fetched_digest) == 0) {
1880 t->err = "PKCS12_ERROR";
1884 if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len,
1885 key, expected->key_len))
1890 EVP_MD_free(fetched_digest);
1892 OPENSSL_CTX_set0_default(save_libctx);
1896 static const EVP_TEST_METHOD pbe_test_method = {
1910 BASE64_CANONICAL_ENCODING = 0,
1911 BASE64_VALID_ENCODING = 1,
1912 BASE64_INVALID_ENCODING = 2
1913 } base64_encoding_type;
1915 typedef struct encode_data_st {
1916 /* Input to encoding */
1917 unsigned char *input;
1919 /* Expected output */
1920 unsigned char *output;
1922 base64_encoding_type encoding;
1925 static int encode_test_init(EVP_TEST *t, const char *encoding)
1929 if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
1931 if (strcmp(encoding, "canonical") == 0) {
1932 edata->encoding = BASE64_CANONICAL_ENCODING;
1933 } else if (strcmp(encoding, "valid") == 0) {
1934 edata->encoding = BASE64_VALID_ENCODING;
1935 } else if (strcmp(encoding, "invalid") == 0) {
1936 edata->encoding = BASE64_INVALID_ENCODING;
1937 if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
1940 TEST_error("Bad encoding: %s."
1941 " Should be one of {canonical, valid, invalid}",
1948 OPENSSL_free(edata);
1952 static void encode_test_cleanup(EVP_TEST *t)
1954 ENCODE_DATA *edata = t->data;
1956 OPENSSL_free(edata->input);
1957 OPENSSL_free(edata->output);
1958 memset(edata, 0, sizeof(*edata));
1961 static int encode_test_parse(EVP_TEST *t,
1962 const char *keyword, const char *value)
1964 ENCODE_DATA *edata = t->data;
1966 if (strcmp(keyword, "Input") == 0)
1967 return parse_bin(value, &edata->input, &edata->input_len);
1968 if (strcmp(keyword, "Output") == 0)
1969 return parse_bin(value, &edata->output, &edata->output_len);
1973 static int encode_test_run(EVP_TEST *t)
1975 ENCODE_DATA *expected = t->data;
1976 unsigned char *encode_out = NULL, *decode_out = NULL;
1977 int output_len, chunk_len;
1978 EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL;
1980 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
1981 t->err = "INTERNAL_ERROR";
1985 if (expected->encoding == BASE64_CANONICAL_ENCODING) {
1987 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
1988 || !TEST_ptr(encode_out =
1989 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
1992 EVP_EncodeInit(encode_ctx);
1993 if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1994 expected->input, expected->input_len)))
1997 output_len = chunk_len;
1999 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
2000 output_len += chunk_len;
2002 if (!memory_err_compare(t, "BAD_ENCODING",
2003 expected->output, expected->output_len,
2004 encode_out, output_len))
2008 if (!TEST_ptr(decode_out =
2009 OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
2012 EVP_DecodeInit(decode_ctx);
2013 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
2014 expected->output_len) < 0) {
2015 t->err = "DECODE_ERROR";
2018 output_len = chunk_len;
2020 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
2021 t->err = "DECODE_ERROR";
2024 output_len += chunk_len;
2026 if (expected->encoding != BASE64_INVALID_ENCODING
2027 && !memory_err_compare(t, "BAD_DECODING",
2028 expected->input, expected->input_len,
2029 decode_out, output_len)) {
2030 t->err = "BAD_DECODING";
2036 OPENSSL_free(encode_out);
2037 OPENSSL_free(decode_out);
2038 EVP_ENCODE_CTX_free(decode_ctx);
2039 EVP_ENCODE_CTX_free(encode_ctx);
2043 static const EVP_TEST_METHOD encode_test_method = {
2046 encode_test_cleanup,
2055 #define MAX_RAND_REPEATS 15
2057 typedef struct rand_data_pass_st {
2058 unsigned char *entropy;
2059 unsigned char *reseed_entropy;
2060 unsigned char *nonce;
2061 unsigned char *pers;
2062 unsigned char *reseed_addin;
2063 unsigned char *addinA;
2064 unsigned char *addinB;
2065 unsigned char *pr_entropyA;
2066 unsigned char *pr_entropyB;
2067 unsigned char *output;
2068 size_t entropy_len, nonce_len, pers_len, addinA_len, addinB_len,
2069 pr_entropyA_len, pr_entropyB_len, output_len, reseed_entropy_len,
2073 typedef struct rand_data_st {
2074 /* Context for this operation */
2076 EVP_RAND_CTX *parent;
2078 int prediction_resistance;
2080 unsigned int generate_bits;
2084 /* Expected output */
2085 RAND_DATA_PASS data[MAX_RAND_REPEATS];
2088 static int rand_test_init(EVP_TEST *t, const char *name)
2092 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
2093 unsigned int strength = 256;
2095 if (!TEST_ptr(rdata = OPENSSL_zalloc(sizeof(*rdata))))
2098 /* TEST-RAND is available in the FIPS provider but not with "fips=yes" */
2099 rand = EVP_RAND_fetch(libctx, "TEST-RAND", "-fips");
2102 rdata->parent = EVP_RAND_CTX_new(rand, NULL);
2103 EVP_RAND_free(rand);
2104 if (rdata->parent == NULL)
2107 *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength);
2108 if (!EVP_RAND_set_ctx_params(rdata->parent, params))
2111 rand = EVP_RAND_fetch(libctx, name, NULL);
2114 rdata->ctx = EVP_RAND_CTX_new(rand, rdata->parent);
2115 EVP_RAND_free(rand);
2116 if (rdata->ctx == NULL)
2123 EVP_RAND_CTX_free(rdata->parent);
2124 OPENSSL_free(rdata);
2128 static void rand_test_cleanup(EVP_TEST *t)
2130 RAND_DATA *rdata = t->data;
2133 OPENSSL_free(rdata->cipher);
2134 OPENSSL_free(rdata->digest);
2136 for (i = 0; i <= rdata->n; i++) {
2137 OPENSSL_free(rdata->data[i].entropy);
2138 OPENSSL_free(rdata->data[i].reseed_entropy);
2139 OPENSSL_free(rdata->data[i].nonce);
2140 OPENSSL_free(rdata->data[i].pers);
2141 OPENSSL_free(rdata->data[i].reseed_addin);
2142 OPENSSL_free(rdata->data[i].addinA);
2143 OPENSSL_free(rdata->data[i].addinB);
2144 OPENSSL_free(rdata->data[i].pr_entropyA);
2145 OPENSSL_free(rdata->data[i].pr_entropyB);
2146 OPENSSL_free(rdata->data[i].output);
2148 EVP_RAND_CTX_free(rdata->ctx);
2149 EVP_RAND_CTX_free(rdata->parent);
2152 static int rand_test_parse(EVP_TEST *t,
2153 const char *keyword, const char *value)
2155 RAND_DATA *rdata = t->data;
2156 RAND_DATA_PASS *item;
2160 if ((p = strchr(keyword, '.')) != NULL) {
2162 if (n >= MAX_RAND_REPEATS)
2166 item = rdata->data + n;
2167 if (strncmp(keyword, "Entropy.", sizeof("Entropy")) == 0)
2168 return parse_bin(value, &item->entropy, &item->entropy_len);
2169 if (strncmp(keyword, "ReseedEntropy.", sizeof("ReseedEntropy")) == 0)
2170 return parse_bin(value, &item->reseed_entropy,
2171 &item->reseed_entropy_len);
2172 if (strncmp(keyword, "Nonce.", sizeof("Nonce")) == 0)
2173 return parse_bin(value, &item->nonce, &item->nonce_len);
2174 if (strncmp(keyword, "PersonalisationString.",
2175 sizeof("PersonalisationString")) == 0)
2176 return parse_bin(value, &item->pers, &item->pers_len);
2177 if (strncmp(keyword, "ReseedAdditionalInput.",
2178 sizeof("ReseedAdditionalInput")) == 0)
2179 return parse_bin(value, &item->reseed_addin,
2180 &item->reseed_addin_len);
2181 if (strncmp(keyword, "AdditionalInputA.",
2182 sizeof("AdditionalInputA")) == 0)
2183 return parse_bin(value, &item->addinA, &item->addinA_len);
2184 if (strncmp(keyword, "AdditionalInputB.",
2185 sizeof("AdditionalInputB")) == 0)
2186 return parse_bin(value, &item->addinB, &item->addinB_len);
2187 if (strncmp(keyword, "EntropyPredictionResistanceA.",
2188 sizeof("EntropyPredictionResistanceA")) == 0)
2189 return parse_bin(value, &item->pr_entropyA, &item->pr_entropyA_len);
2190 if (strncmp(keyword, "EntropyPredictionResistanceB.",
2191 sizeof("EntropyPredictionResistanceB")) == 0)
2192 return parse_bin(value, &item->pr_entropyB, &item->pr_entropyB_len);
2193 if (strncmp(keyword, "Output.", sizeof("Output")) == 0)
2194 return parse_bin(value, &item->output, &item->output_len);
2196 if (strcmp(keyword, "Cipher") == 0)
2197 return TEST_ptr(rdata->cipher = OPENSSL_strdup(value));
2198 if (strcmp(keyword, "Digest") == 0)
2199 return TEST_ptr(rdata->digest = OPENSSL_strdup(value));
2200 if (strcmp(keyword, "DerivationFunction") == 0) {
2201 rdata->use_df = atoi(value) != 0;
2204 if (strcmp(keyword, "GenerateBits") == 0) {
2205 if ((n = atoi(value)) <= 0 || n % 8 != 0)
2207 rdata->generate_bits = (unsigned int)n;
2210 if (strcmp(keyword, "PredictionResistance") == 0) {
2211 rdata->prediction_resistance = atoi(value) != 0;
2218 static int rand_test_run(EVP_TEST *t)
2220 RAND_DATA *expected = t->data;
2221 RAND_DATA_PASS *item;
2223 size_t got_len = expected->generate_bits / 8;
2224 OSSL_PARAM params[5], *p = params;
2225 int i = -1, ret = 0;
2226 unsigned int strength;
2229 if (!TEST_ptr(got = OPENSSL_malloc(got_len)))
2232 *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &expected->use_df);
2233 if (expected->cipher != NULL)
2234 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
2235 expected->cipher, 0);
2236 if (expected->digest != NULL)
2237 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
2238 expected->digest, 0);
2239 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC, "HMAC", 0);
2240 *p = OSSL_PARAM_construct_end();
2241 if (!TEST_true(EVP_RAND_set_ctx_params(expected->ctx, params)))
2244 strength = EVP_RAND_strength(expected->ctx);
2245 for (i = 0; i <= expected->n; i++) {
2246 item = expected->data + i;
2249 z = item->entropy != NULL ? item->entropy : (unsigned char *)"";
2250 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
2251 z, item->entropy_len);
2252 z = item->nonce != NULL ? item->nonce : (unsigned char *)"";
2253 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
2254 z, item->nonce_len);
2255 *p = OSSL_PARAM_construct_end();
2256 if (!TEST_true(EVP_RAND_set_ctx_params(expected->parent, params))
2257 || !TEST_true(EVP_RAND_instantiate(expected->parent, strength,
2261 z = item->pers != NULL ? item->pers : (unsigned char *)"";
2262 if (!TEST_true(EVP_RAND_instantiate
2263 (expected->ctx, strength,
2264 expected->prediction_resistance, z,
2268 if (item->reseed_entropy != NULL) {
2269 params[0] = OSSL_PARAM_construct_octet_string
2270 (OSSL_RAND_PARAM_TEST_ENTROPY, item->reseed_entropy,
2271 item->reseed_entropy_len);
2272 params[1] = OSSL_PARAM_construct_end();
2273 if (!TEST_true(EVP_RAND_set_ctx_params(expected->parent, params)))
2276 if (!TEST_true(EVP_RAND_reseed
2277 (expected->ctx, expected->prediction_resistance,
2278 NULL, 0, item->reseed_addin,
2279 item->reseed_addin_len)))
2282 if (item->pr_entropyA != NULL) {
2283 params[0] = OSSL_PARAM_construct_octet_string
2284 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyA,
2285 item->pr_entropyA_len);
2286 params[1] = OSSL_PARAM_construct_end();
2287 if (!TEST_true(EVP_RAND_set_ctx_params(expected->parent, params)))
2290 if (!TEST_true(EVP_RAND_generate
2291 (expected->ctx, got, got_len,
2292 strength, expected->prediction_resistance,
2293 item->addinA, item->addinA_len)))
2296 if (item->pr_entropyB != NULL) {
2297 params[0] = OSSL_PARAM_construct_octet_string
2298 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyB,
2299 item->pr_entropyB_len);
2300 params[1] = OSSL_PARAM_construct_end();
2301 if (!TEST_true(EVP_RAND_set_ctx_params(expected->parent, params)))
2304 if (!TEST_true(EVP_RAND_generate
2305 (expected->ctx, got, got_len,
2306 strength, expected->prediction_resistance,
2307 item->addinB, item->addinB_len)))
2309 if (!TEST_mem_eq(got, got_len, item->output, item->output_len))
2311 if (!TEST_true(EVP_RAND_uninstantiate(expected->ctx))
2312 || !TEST_true(EVP_RAND_uninstantiate(expected->parent))
2313 || !TEST_true(EVP_RAND_verify_zeroization(expected->ctx))
2314 || !TEST_int_eq(EVP_RAND_state(expected->ctx),
2315 EVP_RAND_STATE_UNINITIALISED))
2322 if (ret == 0 && i >= 0)
2323 TEST_info("Error in test case %d of %d\n", i, expected->n + 1);
2328 static const EVP_TEST_METHOD rand_test_method = {
2340 typedef struct kdf_data_st {
2341 /* Context for this operation */
2343 /* Expected output */
2344 unsigned char *output;
2346 OSSL_PARAM params[20];
2351 * Perform public key operation setup: lookup key, allocated ctx and call
2352 * the appropriate initialisation function
2354 static int kdf_test_init(EVP_TEST *t, const char *name)
2359 if (is_kdf_disabled(name)) {
2360 TEST_info("skipping, '%s' is disabled", name);
2365 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2367 kdata->p = kdata->params;
2368 *kdata->p = OSSL_PARAM_construct_end();
2370 kdf = EVP_KDF_fetch(libctx, name, NULL);
2372 OPENSSL_free(kdata);
2375 kdata->ctx = EVP_KDF_CTX_new(kdf);
2377 if (kdata->ctx == NULL) {
2378 OPENSSL_free(kdata);
2385 static void kdf_test_cleanup(EVP_TEST *t)
2387 KDF_DATA *kdata = t->data;
2390 for (p = kdata->params; p->key != NULL; p++)
2391 OPENSSL_free(p->data);
2392 OPENSSL_free(kdata->output);
2393 EVP_KDF_CTX_free(kdata->ctx);
2396 static int kdf_test_ctrl(EVP_TEST *t, EVP_KDF_CTX *kctx,
2399 KDF_DATA *kdata = t->data;
2402 const OSSL_PARAM *defs = EVP_KDF_settable_ctx_params(EVP_KDF_CTX_kdf(kctx));
2404 if (!TEST_ptr(name = OPENSSL_strdup(value)))
2406 p = strchr(name, ':');
2410 rv = OSSL_PARAM_allocate_from_text(kdata->p, defs, name, p,
2411 p != NULL ? strlen(p) : 0, NULL);
2412 *++kdata->p = OSSL_PARAM_construct_end();
2414 t->err = "KDF_PARAM_ERROR";
2418 if (p != NULL && strcmp(name, "digest") == 0) {
2419 if (is_digest_disabled(p)) {
2420 TEST_info("skipping, '%s' is disabled", p);
2424 if (p != NULL && strcmp(name, "cipher") == 0) {
2425 if (is_cipher_disabled(p)) {
2426 TEST_info("skipping, '%s' is disabled", p);
2434 static int kdf_test_parse(EVP_TEST *t,
2435 const char *keyword, const char *value)
2437 KDF_DATA *kdata = t->data;
2439 if (strcmp(keyword, "Output") == 0)
2440 return parse_bin(value, &kdata->output, &kdata->output_len);
2441 if (strncmp(keyword, "Ctrl", 4) == 0)
2442 return kdf_test_ctrl(t, kdata->ctx, value);
2446 static int kdf_test_run(EVP_TEST *t)
2448 KDF_DATA *expected = t->data;
2449 unsigned char *got = NULL;
2450 size_t got_len = expected->output_len;
2452 if (!EVP_KDF_CTX_set_params(expected->ctx, expected->params)) {
2453 t->err = "KDF_CTRL_ERROR";
2456 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2457 t->err = "INTERNAL_ERROR";
2460 if (EVP_KDF_derive(expected->ctx, got, got_len) <= 0) {
2461 t->err = "KDF_DERIVE_ERROR";
2464 if (!memory_err_compare(t, "KDF_MISMATCH",
2465 expected->output, expected->output_len,
2476 static const EVP_TEST_METHOD kdf_test_method = {
2488 typedef struct pkey_kdf_data_st {
2489 /* Context for this operation */
2491 /* Expected output */
2492 unsigned char *output;
2497 * Perform public key operation setup: lookup key, allocated ctx and call
2498 * the appropriate initialisation function
2500 static int pkey_kdf_test_init(EVP_TEST *t, const char *name)
2502 PKEY_KDF_DATA *kdata = NULL;
2504 if (is_kdf_disabled(name)) {
2505 TEST_info("skipping, '%s' is disabled", name);
2510 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2513 kdata->ctx = EVP_PKEY_CTX_new_from_name(libctx, name, NULL);
2514 if (kdata->ctx == NULL
2515 || EVP_PKEY_derive_init(kdata->ctx) <= 0)
2521 EVP_PKEY_CTX_free(kdata->ctx);
2522 OPENSSL_free(kdata);
2526 static void pkey_kdf_test_cleanup(EVP_TEST *t)
2528 PKEY_KDF_DATA *kdata = t->data;
2530 OPENSSL_free(kdata->output);
2531 EVP_PKEY_CTX_free(kdata->ctx);
2534 static int pkey_kdf_test_parse(EVP_TEST *t,
2535 const char *keyword, const char *value)
2537 PKEY_KDF_DATA *kdata = t->data;
2539 if (strcmp(keyword, "Output") == 0)
2540 return parse_bin(value, &kdata->output, &kdata->output_len);
2541 if (strncmp(keyword, "Ctrl", 4) == 0)
2542 return pkey_test_ctrl(t, kdata->ctx, value);
2546 static int pkey_kdf_test_run(EVP_TEST *t)
2548 PKEY_KDF_DATA *expected = t->data;
2549 unsigned char *got = NULL;
2550 size_t got_len = expected->output_len;
2552 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2553 t->err = "INTERNAL_ERROR";
2556 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
2557 t->err = "KDF_DERIVE_ERROR";
2560 if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
2561 t->err = "KDF_MISMATCH";
2571 static const EVP_TEST_METHOD pkey_kdf_test_method = {
2574 pkey_kdf_test_cleanup,
2575 pkey_kdf_test_parse,
2583 typedef struct keypair_test_data_st {
2586 } KEYPAIR_TEST_DATA;
2588 static int keypair_test_init(EVP_TEST *t, const char *pair)
2590 KEYPAIR_TEST_DATA *data;
2592 EVP_PKEY *pk = NULL, *pubk = NULL;
2593 char *pub, *priv = NULL;
2595 /* Split private and public names. */
2596 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
2597 || !TEST_ptr(pub = strchr(priv, ':'))) {
2598 t->err = "PARSING_ERROR";
2603 if (!TEST_true(find_key(&pk, priv, private_keys))) {
2604 TEST_info("Can't find private key: %s", priv);
2605 t->err = "MISSING_PRIVATE_KEY";
2608 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
2609 TEST_info("Can't find public key: %s", pub);
2610 t->err = "MISSING_PUBLIC_KEY";
2614 if (pk == NULL && pubk == NULL) {
2615 /* Both keys are listed but unsupported: skip this test */
2621 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2634 static void keypair_test_cleanup(EVP_TEST *t)
2636 OPENSSL_free(t->data);
2641 * For tests that do not accept any custom keywords.
2643 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
2648 static int keypair_test_run(EVP_TEST *t)
2651 const KEYPAIR_TEST_DATA *pair = t->data;
2653 if (pair->privk == NULL || pair->pubk == NULL) {
2655 * this can only happen if only one of the keys is not set
2656 * which means that one of them was unsupported while the
2657 * other isn't: hence a key type mismatch.
2659 t->err = "KEYPAIR_TYPE_MISMATCH";
2664 if ((rv = EVP_PKEY_eq(pair->privk, pair->pubk)) != 1 ) {
2666 t->err = "KEYPAIR_MISMATCH";
2667 } else if ( -1 == rv ) {
2668 t->err = "KEYPAIR_TYPE_MISMATCH";
2669 } else if ( -2 == rv ) {
2670 t->err = "UNSUPPORTED_KEY_COMPARISON";
2672 TEST_error("Unexpected error in key comparison");
2687 static const EVP_TEST_METHOD keypair_test_method = {
2690 keypair_test_cleanup,
2699 typedef struct keygen_test_data_st {
2700 EVP_PKEY_CTX *genctx; /* Keygen context to use */
2701 char *keyname; /* Key name to store key or NULL */
2704 static int keygen_test_init(EVP_TEST *t, const char *alg)
2706 KEYGEN_TEST_DATA *data;
2707 EVP_PKEY_CTX *genctx;
2708 int nid = OBJ_sn2nid(alg);
2710 if (nid == NID_undef) {
2711 nid = OBJ_ln2nid(alg);
2712 if (nid == NID_undef)
2716 if (is_pkey_disabled(alg)) {
2720 if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_from_name(libctx, alg, NULL)))
2723 if (EVP_PKEY_keygen_init(genctx) <= 0) {
2724 t->err = "KEYGEN_INIT_ERROR";
2728 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2730 data->genctx = genctx;
2731 data->keyname = NULL;
2737 EVP_PKEY_CTX_free(genctx);
2741 static void keygen_test_cleanup(EVP_TEST *t)
2743 KEYGEN_TEST_DATA *keygen = t->data;
2745 EVP_PKEY_CTX_free(keygen->genctx);
2746 OPENSSL_free(keygen->keyname);
2747 OPENSSL_free(t->data);
2751 static int keygen_test_parse(EVP_TEST *t,
2752 const char *keyword, const char *value)
2754 KEYGEN_TEST_DATA *keygen = t->data;
2756 if (strcmp(keyword, "KeyName") == 0)
2757 return TEST_ptr(keygen->keyname = OPENSSL_strdup(value));
2758 if (strcmp(keyword, "Ctrl") == 0)
2759 return pkey_test_ctrl(t, keygen->genctx, value);
2763 static int keygen_test_run(EVP_TEST *t)
2765 KEYGEN_TEST_DATA *keygen = t->data;
2766 EVP_PKEY *pkey = NULL;
2769 if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) {
2770 t->err = "KEYGEN_GENERATE_ERROR";
2774 if (!evp_pkey_is_provided(pkey)) {
2775 TEST_info("Warning: legacy key generated %s", keygen->keyname);
2778 if (keygen->keyname != NULL) {
2782 if (find_key(NULL, keygen->keyname, private_keys)) {
2783 TEST_info("Duplicate key %s", keygen->keyname);
2787 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
2789 key->name = keygen->keyname;
2790 keygen->keyname = NULL;
2792 key->next = private_keys;
2796 EVP_PKEY_free(pkey);
2805 static const EVP_TEST_METHOD keygen_test_method = {
2808 keygen_test_cleanup,
2814 ** DIGEST SIGN+VERIFY TESTS
2818 int is_verify; /* Set to 1 if verifying */
2819 int is_oneshot; /* Set to 1 for one shot operation */
2820 const EVP_MD *md; /* Digest to use */
2821 EVP_MD_CTX *ctx; /* Digest context */
2823 STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
2824 unsigned char *osin; /* Input data if one shot */
2825 size_t osin_len; /* Input length data if one shot */
2826 unsigned char *output; /* Expected output */
2827 size_t output_len; /* Expected output length */
2830 static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
2833 const EVP_MD *md = NULL;
2834 DIGESTSIGN_DATA *mdat;
2836 if (strcmp(alg, "NULL") != 0) {
2837 if (is_digest_disabled(alg)) {
2841 md = EVP_get_digestbyname(alg);
2845 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
2848 if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
2852 mdat->is_verify = is_verify;
2853 mdat->is_oneshot = is_oneshot;
2858 static int digestsign_test_init(EVP_TEST *t, const char *alg)
2860 return digestsigver_test_init(t, alg, 0, 0);
2863 static void digestsigver_test_cleanup(EVP_TEST *t)
2865 DIGESTSIGN_DATA *mdata = t->data;
2867 EVP_MD_CTX_free(mdata->ctx);
2868 sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
2869 OPENSSL_free(mdata->osin);
2870 OPENSSL_free(mdata->output);
2871 OPENSSL_free(mdata);
2875 static int digestsigver_test_parse(EVP_TEST *t,
2876 const char *keyword, const char *value)
2878 DIGESTSIGN_DATA *mdata = t->data;
2880 if (strcmp(keyword, "Key") == 0) {
2881 EVP_PKEY *pkey = NULL;
2883 const char *name = mdata->md == NULL ? NULL : EVP_MD_name(mdata->md);
2885 if (mdata->is_verify)
2886 rv = find_key(&pkey, value, public_keys);
2888 rv = find_key(&pkey, value, private_keys);
2889 if (rv == 0 || pkey == NULL) {
2893 if (mdata->is_verify) {
2894 if (!EVP_DigestVerifyInit_with_libctx(mdata->ctx, &mdata->pctx,
2895 name, libctx, NULL, pkey))
2896 t->err = "DIGESTVERIFYINIT_ERROR";
2899 if (!EVP_DigestSignInit_with_libctx(mdata->ctx, &mdata->pctx,
2900 name, libctx, NULL, pkey))
2901 t->err = "DIGESTSIGNINIT_ERROR";
2905 if (strcmp(keyword, "Input") == 0) {
2906 if (mdata->is_oneshot)
2907 return parse_bin(value, &mdata->osin, &mdata->osin_len);
2908 return evp_test_buffer_append(value, &mdata->input);
2910 if (strcmp(keyword, "Output") == 0)
2911 return parse_bin(value, &mdata->output, &mdata->output_len);
2913 if (!mdata->is_oneshot) {
2914 if (strcmp(keyword, "Count") == 0)
2915 return evp_test_buffer_set_count(value, mdata->input);
2916 if (strcmp(keyword, "Ncopy") == 0)
2917 return evp_test_buffer_ncopy(value, mdata->input);
2919 if (strcmp(keyword, "Ctrl") == 0) {
2920 if (mdata->pctx == NULL)
2922 return pkey_test_ctrl(t, mdata->pctx, value);
2927 static int digestsign_update_fn(void *ctx, const unsigned char *buf,
2930 return EVP_DigestSignUpdate(ctx, buf, buflen);
2933 static int digestsign_test_run(EVP_TEST *t)
2935 DIGESTSIGN_DATA *expected = t->data;
2936 unsigned char *got = NULL;
2939 if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
2941 t->err = "DIGESTUPDATE_ERROR";
2945 if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
2946 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
2949 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2950 t->err = "MALLOC_FAILURE";
2953 if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
2954 t->err = "DIGESTSIGNFINAL_ERROR";
2957 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
2958 expected->output, expected->output_len,
2968 static const EVP_TEST_METHOD digestsign_test_method = {
2970 digestsign_test_init,
2971 digestsigver_test_cleanup,
2972 digestsigver_test_parse,
2976 static int digestverify_test_init(EVP_TEST *t, const char *alg)
2978 return digestsigver_test_init(t, alg, 1, 0);
2981 static int digestverify_update_fn(void *ctx, const unsigned char *buf,
2984 return EVP_DigestVerifyUpdate(ctx, buf, buflen);
2987 static int digestverify_test_run(EVP_TEST *t)
2989 DIGESTSIGN_DATA *mdata = t->data;
2991 if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
2992 t->err = "DIGESTUPDATE_ERROR";
2996 if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
2997 mdata->output_len) <= 0)
2998 t->err = "VERIFY_ERROR";
3002 static const EVP_TEST_METHOD digestverify_test_method = {
3004 digestverify_test_init,
3005 digestsigver_test_cleanup,
3006 digestsigver_test_parse,
3007 digestverify_test_run
3010 static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
3012 return digestsigver_test_init(t, alg, 0, 1);
3015 static int oneshot_digestsign_test_run(EVP_TEST *t)
3017 DIGESTSIGN_DATA *expected = t->data;
3018 unsigned char *got = NULL;
3021 if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
3022 expected->osin, expected->osin_len)) {
3023 t->err = "DIGESTSIGN_LENGTH_ERROR";
3026 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
3027 t->err = "MALLOC_FAILURE";
3030 if (!EVP_DigestSign(expected->ctx, got, &got_len,
3031 expected->osin, expected->osin_len)) {
3032 t->err = "DIGESTSIGN_ERROR";
3035 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
3036 expected->output, expected->output_len,
3046 static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
3047 "OneShotDigestSign",
3048 oneshot_digestsign_test_init,
3049 digestsigver_test_cleanup,
3050 digestsigver_test_parse,
3051 oneshot_digestsign_test_run
3054 static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
3056 return digestsigver_test_init(t, alg, 1, 1);
3059 static int oneshot_digestverify_test_run(EVP_TEST *t)
3061 DIGESTSIGN_DATA *mdata = t->data;
3063 if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
3064 mdata->osin, mdata->osin_len) <= 0)
3065 t->err = "VERIFY_ERROR";
3069 static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
3070 "OneShotDigestVerify",
3071 oneshot_digestverify_test_init,
3072 digestsigver_test_cleanup,
3073 digestsigver_test_parse,
3074 oneshot_digestverify_test_run
3079 ** PARSING AND DISPATCH
3082 static const EVP_TEST_METHOD *evp_test_list[] = {
3084 &cipher_test_method,
3085 &digest_test_method,
3086 &digestsign_test_method,
3087 &digestverify_test_method,
3088 &encode_test_method,
3090 &pkey_kdf_test_method,
3091 &keypair_test_method,
3092 &keygen_test_method,
3094 &oneshot_digestsign_test_method,
3095 &oneshot_digestverify_test_method,
3097 &pdecrypt_test_method,
3098 &pderive_test_method,
3100 &pverify_recover_test_method,
3101 &pverify_test_method,
3105 static const EVP_TEST_METHOD *find_test(const char *name)
3107 const EVP_TEST_METHOD **tt;
3109 for (tt = evp_test_list; *tt; tt++) {
3110 if (strcmp(name, (*tt)->name) == 0)
3116 static void clear_test(EVP_TEST *t)
3118 test_clearstanza(&t->s);
3120 if (t->data != NULL) {
3121 if (t->meth != NULL)
3122 t->meth->cleanup(t);
3123 OPENSSL_free(t->data);
3126 OPENSSL_free(t->expected_err);
3127 t->expected_err = NULL;
3128 OPENSSL_free(t->reason);
3137 /* Check for errors in the test structure; return 1 if okay, else 0. */
3138 static int check_test_error(EVP_TEST *t)
3143 if (t->err == NULL && t->expected_err == NULL)
3145 if (t->err != NULL && t->expected_err == NULL) {
3146 if (t->aux_err != NULL) {
3147 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
3148 t->s.test_file, t->s.start, t->aux_err, t->err);
3150 TEST_info("%s:%d: Source of above error; unexpected error %s",
3151 t->s.test_file, t->s.start, t->err);
3155 if (t->err == NULL && t->expected_err != NULL) {
3156 TEST_info("%s:%d: Succeeded but was expecting %s",
3157 t->s.test_file, t->s.start, t->expected_err);
3161 if (strcmp(t->err, t->expected_err) != 0) {
3162 TEST_info("%s:%d: Expected %s got %s",
3163 t->s.test_file, t->s.start, t->expected_err, t->err);
3167 if (t->reason == NULL)
3170 if (t->reason == NULL) {
3171 TEST_info("%s:%d: Test is missing function or reason code",
3172 t->s.test_file, t->s.start);
3176 err = ERR_peek_error();
3178 TEST_info("%s:%d: Expected error \"%s\" not set",
3179 t->s.test_file, t->s.start, t->reason);
3183 reason = ERR_reason_error_string(err);
3184 if (reason == NULL) {
3185 TEST_info("%s:%d: Expected error \"%s\", no strings available."
3187 t->s.test_file, t->s.start, t->reason);
3191 if (strcmp(reason, t->reason) == 0)
3194 TEST_info("%s:%d: Expected error \"%s\", got \"%s\"",
3195 t->s.test_file, t->s.start, t->reason, reason);
3200 /* Run a parsed test. Log a message and return 0 on error. */
3201 static int run_test(EVP_TEST *t)
3203 if (t->meth == NULL)
3210 if (t->err == NULL && t->meth->run_test(t) != 1) {
3211 TEST_info("%s:%d %s error",
3212 t->s.test_file, t->s.start, t->meth->name);
3215 if (!check_test_error(t)) {
3216 TEST_openssl_errors();
3225 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
3227 for (; lst != NULL; lst = lst->next) {
3228 if (strcmp(lst->name, name) == 0) {
3237 static void free_key_list(KEY_LIST *lst)
3239 while (lst != NULL) {
3240 KEY_LIST *next = lst->next;
3242 EVP_PKEY_free(lst->key);
3243 OPENSSL_free(lst->name);
3250 * Is the key type an unsupported algorithm?
3252 static int key_unsupported(void)
3254 long err = ERR_peek_last_error();
3256 if (ERR_GET_LIB(err) == ERR_LIB_EVP
3257 && (ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM)) {
3261 #ifndef OPENSSL_NO_EC
3263 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
3264 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
3267 if (ERR_GET_LIB(err) == ERR_LIB_EC
3268 && (ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP
3269 || ERR_GET_REASON(err) == EC_R_INVALID_CURVE)) {
3273 #endif /* OPENSSL_NO_EC */
3277 /* NULL out the value from |pp| but return it. This "steals" a pointer. */
3278 static char *take_value(PAIR *pp)
3280 char *p = pp->value;
3287 * Return 1 if one of the providers named in the string is available.
3288 * The provider names are separated with whitespace.
3289 * NOTE: destructive function, it inserts '\0' after each provider name.
3291 static int prov_available(char *providers)
3297 for (; isspace(*providers); providers++)
3299 if (*providers == '\0')
3300 break; /* End of the road */
3301 for (p = providers; *p != '\0' && !isspace(*p); p++)
3307 if (OSSL_PROVIDER_available(libctx, providers))
3308 return 1; /* Found one */
3313 /* Read and parse one test. Return 0 if failure, 1 if okay. */
3314 static int parse(EVP_TEST *t)
3316 KEY_LIST *key, **klist;
3319 int i, skip_availablein = 0;
3323 if (BIO_eof(t->s.fp))
3326 if (!test_readstanza(&t->s))
3328 } while (t->s.numpairs == 0);
3329 pp = &t->s.pairs[0];
3331 /* Are we adding a key? */
3335 if (strcmp(pp->key, "PrivateKey") == 0) {
3336 pkey = PEM_read_bio_PrivateKey_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3337 if (pkey == NULL && !key_unsupported()) {
3338 EVP_PKEY_free(pkey);
3339 TEST_info("Can't read private key %s", pp->value);
3340 TEST_openssl_errors();
3343 klist = &private_keys;
3344 } else if (strcmp(pp->key, "PublicKey") == 0) {
3345 pkey = PEM_read_bio_PUBKEY_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3346 if (pkey == NULL && !key_unsupported()) {
3347 EVP_PKEY_free(pkey);
3348 TEST_info("Can't read public key %s", pp->value);
3349 TEST_openssl_errors();
3352 klist = &public_keys;
3353 } else if (strcmp(pp->key, "PrivateKeyRaw") == 0
3354 || strcmp(pp->key, "PublicKeyRaw") == 0 ) {
3355 char *strnid = NULL, *keydata = NULL;
3356 unsigned char *keybin;
3360 if (strcmp(pp->key, "PrivateKeyRaw") == 0)
3361 klist = &private_keys;
3363 klist = &public_keys;
3365 strnid = strchr(pp->value, ':');
3366 if (strnid != NULL) {
3368 keydata = strchr(strnid, ':');
3369 if (keydata != NULL)
3372 if (keydata == NULL) {
3373 TEST_info("Failed to parse %s value", pp->key);
3377 nid = OBJ_txt2nid(strnid);
3378 if (nid == NID_undef) {
3379 TEST_info("Unrecognised algorithm NID");
3382 if (!parse_bin(keydata, &keybin, &keylen)) {
3383 TEST_info("Failed to create binary key");
3386 if (klist == &private_keys)
3387 pkey = EVP_PKEY_new_raw_private_key_with_libctx(libctx, strnid, NULL,
3390 pkey = EVP_PKEY_new_raw_public_key_with_libctx(libctx, strnid, NULL,
3392 if (pkey == NULL && !key_unsupported()) {
3393 TEST_info("Can't read %s data", pp->key);
3394 OPENSSL_free(keybin);
3395 TEST_openssl_errors();
3398 OPENSSL_free(keybin);
3399 } else if (strcmp(pp->key, "Availablein") == 0) {
3400 if (!prov_available(pp->value)) {
3401 TEST_info("skipping, '%s' provider not available: %s:%d",
3402 pp->value, t->s.test_file, t->s.start);
3411 /* If we have a key add to list */
3412 if (klist != NULL) {
3413 if (find_key(NULL, pp->value, *klist)) {
3414 TEST_info("Duplicate key %s", pp->value);
3417 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
3419 key->name = take_value(pp);
3424 /* Go back and start a new stanza. */
3425 if ((t->s.numpairs - skip_availablein) != 1)
3426 TEST_info("Line %d: missing blank line\n", t->s.curr);
3430 /* Find the test, based on first keyword. */
3431 if (!TEST_ptr(t->meth = find_test(pp->key)))
3433 if (!t->meth->init(t, pp->value)) {
3434 TEST_error("unknown %s: %s\n", pp->key, pp->value);
3438 /* TEST_info("skipping %s %s", pp->key, pp->value); */
3442 for (pp++, i = 1; i < (t->s.numpairs - skip_availablein); pp++, i++) {
3443 if (strcmp(pp->key, "Availablein") == 0) {
3444 TEST_info("Line %d: 'Availablein' should be the first option",
3447 } else if (strcmp(pp->key, "Result") == 0) {
3448 if (t->expected_err != NULL) {
3449 TEST_info("Line %d: multiple result lines", t->s.curr);
3452 t->expected_err = take_value(pp);
3453 } else if (strcmp(pp->key, "Function") == 0) {
3454 /* Ignore old line. */
3455 } else if (strcmp(pp->key, "Reason") == 0) {
3456 if (t->reason != NULL) {
3457 TEST_info("Line %d: multiple reason lines", t->s.curr);
3460 t->reason = take_value(pp);
3462 /* Must be test specific line: try to parse it */
3463 int rv = t->meth->parse(t, pp->key, pp->value);
3466 TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key);
3470 TEST_info("Line %d: error processing keyword %s = %s\n",
3471 t->s.curr, pp->key, pp->value);
3480 static int run_file_tests(int i)
3483 const char *testfile = test_get_argument(i);
3486 if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t))))
3488 if (!test_start_file(&t->s, testfile)) {
3493 while (!BIO_eof(t->s.fp)) {
3499 if (c == 0 || !run_test(t)) {
3504 test_end_file(&t->s);
3507 free_key_list(public_keys);
3508 free_key_list(private_keys);
3515 const OPTIONS *test_get_options(void)
3517 static const OPTIONS test_options[] = {
3518 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
3519 { "config", OPT_CONFIG_FILE, '<',
3520 "The configuration file to use for the libctx" },
3521 { OPT_HELP_STR, 1, '-',
3522 "file\tFile to run tests on.\n" },
3525 return test_options;
3528 int setup_tests(void)
3531 char *config_file = NULL;
3535 while ((o = opt_next()) != OPT_EOF) {
3537 case OPT_CONFIG_FILE:
3538 config_file = opt_arg();
3540 case OPT_TEST_CASES:
3549 * Load the 'null' provider into the default library context to ensure that
3550 * the the tests do not fallback to using the default provider.
3552 prov_null = OSSL_PROVIDER_load(NULL, "null");
3553 if (prov_null == NULL) {
3554 opt_printf_stderr("Failed to load null provider into default libctx\n");
3558 /* load the provider via configuration into the created library context */
3559 libctx = OPENSSL_CTX_new();
3561 || !OPENSSL_CTX_load_config(libctx, config_file)) {
3562 TEST_error("Failed to load config %s\n", config_file);
3566 n = test_get_argument_count();
3570 ADD_ALL_TESTS(run_file_tests, n);
3574 void cleanup_tests(void)
3576 OSSL_PROVIDER_unload(prov_null);
3577 OPENSSL_CTX_free(libctx);
3580 #define STR_STARTS_WITH(str, pre) strncasecmp(pre, str, strlen(pre)) == 0
3581 #define STR_ENDS_WITH(str, pre) \
3582 strlen(str) < strlen(pre) ? 0 : (strcasecmp(pre, str + strlen(str) - strlen(pre)) == 0)
3584 static int is_digest_disabled(const char *name)
3586 #ifdef OPENSSL_NO_BLAKE2
3587 if (STR_STARTS_WITH(name, "BLAKE"))
3590 #ifdef OPENSSL_NO_MD2
3591 if (strcasecmp(name, "MD2") == 0)
3594 #ifdef OPENSSL_NO_MDC2
3595 if (strcasecmp(name, "MDC2") == 0)
3598 #ifdef OPENSSL_NO_MD4
3599 if (strcasecmp(name, "MD4") == 0)
3602 #ifdef OPENSSL_NO_MD5
3603 if (strcasecmp(name, "MD5") == 0)
3606 #ifdef OPENSSL_NO_RMD160
3607 if (strcasecmp(name, "RIPEMD160") == 0)
3610 #ifdef OPENSSL_NO_SM3
3611 if (strcasecmp(name, "SM3") == 0)
3614 #ifdef OPENSSL_NO_WHIRLPOOL
3615 if (strcasecmp(name, "WHIRLPOOL") == 0)
3621 static int is_pkey_disabled(const char *name)
3623 #ifdef OPENSSL_NO_RSA
3624 if (STR_STARTS_WITH(name, "RSA"))
3627 #ifdef OPENSSL_NO_EC
3628 if (STR_STARTS_WITH(name, "EC"))
3631 #ifdef OPENSSL_NO_DH
3632 if (STR_STARTS_WITH(name, "DH"))
3635 #ifdef OPENSSL_NO_DSA
3636 if (STR_STARTS_WITH(name, "DSA"))
3642 static int is_mac_disabled(const char *name)
3644 #ifdef OPENSSL_NO_BLAKE2
3645 if (STR_STARTS_WITH(name, "BLAKE2BMAC")
3646 || STR_STARTS_WITH(name, "BLAKE2SMAC"))
3649 #ifdef OPENSSL_NO_CMAC
3650 if (STR_STARTS_WITH(name, "CMAC"))
3653 #ifdef OPENSSL_NO_POLY1305
3654 if (STR_STARTS_WITH(name, "Poly1305"))
3657 #ifdef OPENSSL_NO_SIPHASH
3658 if (STR_STARTS_WITH(name, "SipHash"))
3663 static int is_kdf_disabled(const char *name)
3665 #ifdef OPENSSL_NO_SCRYPT
3666 if (STR_ENDS_WITH(name, "SCRYPT"))
3669 #ifdef OPENSSL_NO_CMS
3670 if (strcasecmp(name, "X942KDF") == 0)
3672 #endif /* OPENSSL_NO_CMS */
3676 static int is_cipher_disabled(const char *name)
3678 #ifdef OPENSSL_NO_ARIA
3679 if (STR_STARTS_WITH(name, "ARIA"))
3682 #ifdef OPENSSL_NO_BF
3683 if (STR_STARTS_WITH(name, "BF"))
3686 #ifdef OPENSSL_NO_CAMELLIA
3687 if (STR_STARTS_WITH(name, "CAMELLIA"))
3690 #ifdef OPENSSL_NO_CAST
3691 if (STR_STARTS_WITH(name, "CAST"))
3694 #ifdef OPENSSL_NO_CHACHA
3695 if (STR_STARTS_WITH(name, "CHACHA"))
3698 #ifdef OPENSSL_NO_POLY1305
3699 if (STR_ENDS_WITH(name, "Poly1305"))
3702 #ifdef OPENSSL_NO_DES
3703 if (STR_STARTS_WITH(name, "DES"))
3706 #ifdef OPENSSL_NO_OCB
3707 if (STR_ENDS_WITH(name, "OCB"))
3710 #ifdef OPENSSL_NO_IDEA
3711 if (STR_STARTS_WITH(name, "IDEA"))
3714 #ifdef OPENSSL_NO_RC2
3715 if (STR_STARTS_WITH(name, "RC2"))
3718 #ifdef OPENSSL_NO_RC4
3719 if (STR_STARTS_WITH(name, "RC4"))
3722 #ifdef OPENSSL_NO_RC5
3723 if (STR_STARTS_WITH(name, "RC5"))
3726 #ifdef OPENSSL_NO_SEED
3727 if (STR_STARTS_WITH(name, "SEED"))
3730 #ifdef OPENSSL_NO_SIV
3731 if (STR_ENDS_WITH(name, "SIV"))
3734 #ifdef OPENSSL_NO_SM4
3735 if (STR_STARTS_WITH(name, "SM4"))