2 * Copyright 2015-2024 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
10 #define OPENSSL_SUPPRESS_DEPRECATED /* EVP_PKEY_new_CMAC_key */
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 <openssl/thread.h>
26 #include "internal/numbers.h"
27 #include "internal/nelem.h"
28 #include "crypto/evp.h"
31 typedef struct evp_test_buffer_st EVP_TEST_BUFFER;
32 DEFINE_STACK_OF(EVP_TEST_BUFFER)
36 typedef struct evp_test_method_st EVP_TEST_METHOD;
38 /* Structure holding test information */
39 typedef struct evp_test_st {
40 STANZA s; /* Common test stanza */
42 int skip; /* Current test should be skipped */
43 const EVP_TEST_METHOD *meth; /* method for this test */
44 const char *err, *aux_err; /* Error string for test */
45 char *expected_err; /* Expected error value of test */
46 char *reason; /* Expected error reason string */
47 void *data; /* test specific data */
50 /* Test method structure */
51 struct evp_test_method_st {
52 /* Name of test as it appears in file */
54 /* Initialise test for "alg" */
55 int (*init) (EVP_TEST *t, const char *alg);
57 void (*cleanup) (EVP_TEST *t);
58 /* Test specific name value pair processing */
59 int (*parse) (EVP_TEST *t, const char *name, const char *value);
60 /* Run the test itself */
61 int (*run_test) (EVP_TEST *t);
64 /* Linked list of named keys. */
65 typedef struct key_list_st {
68 struct key_list_st *next;
71 typedef enum OPTION_choice {
82 static OSSL_PROVIDER *prov_null = NULL;
83 static OSSL_PROVIDER *libprov = NULL;
84 static OSSL_LIB_CTX *libctx = NULL;
86 /* List of public and private keys */
87 static KEY_LIST *private_keys;
88 static KEY_LIST *public_keys;
90 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst);
91 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen);
92 static int parse_bin_chunk(const char *value, size_t offset, size_t max,
93 unsigned char **buf, size_t *buflen, size_t *out_offset);
94 static int is_digest_disabled(const char *name);
95 static int is_pkey_disabled(const char *name);
96 static int is_mac_disabled(const char *name);
97 static int is_cipher_disabled(const char *name);
98 static int is_kdf_disabled(const char *name);
101 * Compare two memory regions for equality, returning zero if they differ.
102 * However, if there is expected to be an error and the actual error
103 * matches then the memory is expected to be different so handle this
104 * case without producing unnecessary test framework output.
106 static int memory_err_compare(EVP_TEST *t, const char *err,
107 const void *expected, size_t expected_len,
108 const void *got, size_t got_len)
112 if (t->expected_err != NULL && strcmp(t->expected_err, err) == 0)
113 r = !TEST_mem_ne(expected, expected_len, got, got_len);
115 r = TEST_mem_eq(expected, expected_len, got, got_len);
121 /* Option specific for evp test */
122 static int process_mode_in_place;
123 static const char *propquery = NULL;
124 static int data_chunk_size;
126 static int evp_test_process_mode(char *mode)
128 if (strcmp(mode, "in_place") == 0)
130 else if (strcmp(mode, "both") == 0)
136 * Structure used to hold a list of blocks of memory to test
137 * calls to "update" like functions.
139 struct evp_test_buffer_st {
146 static void evp_test_buffer_free(EVP_TEST_BUFFER *db)
149 OPENSSL_free(db->buf);
154 /* append buffer to a list */
155 static int evp_test_buffer_append(const char *value, size_t max_len,
156 STACK_OF(EVP_TEST_BUFFER) **sk)
158 EVP_TEST_BUFFER *db = NULL;
162 if (*sk == NULL && !TEST_ptr(*sk = sk_EVP_TEST_BUFFER_new_null()))
166 if (!TEST_ptr(db = OPENSSL_zalloc(sizeof(*db))))
169 /* parse all in one shot */
170 if ((rv = parse_bin(value, &db->buf, &db->buflen)) != 1)
173 /* parse in chunks */
174 size_t new_offset = 0;
176 if ((rv = parse_bin_chunk(value, offset, max_len, &db->buf,
177 &db->buflen, &new_offset)) == -1)
186 evp_test_buffer_free(db);
187 else if (db->buf != NULL && !sk_EVP_TEST_BUFFER_push(*sk, db))
189 /* if processing by chunks, continue until the whole value is parsed */
190 } while (rv == 1 && max_len != 0);
195 evp_test_buffer_free(db);
199 /* replace last buffer in list with copies of itself */
200 static int evp_test_buffer_ncopy(const char *value,
201 STACK_OF(EVP_TEST_BUFFER) *sk)
204 unsigned char *tbuf, *p;
206 int ncopy = atoi(value);
211 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
213 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
215 tbuflen = db->buflen * ncopy;
216 if (!TEST_ptr(tbuf = OPENSSL_malloc(tbuflen)))
218 for (i = 0, p = tbuf; i < ncopy; i++, p += db->buflen)
219 memcpy(p, db->buf, db->buflen);
221 OPENSSL_free(db->buf);
223 db->buflen = tbuflen;
227 /* set repeat count for last buffer in list */
228 static int evp_test_buffer_set_count(const char *value,
229 STACK_OF(EVP_TEST_BUFFER) *sk)
232 int count = atoi(value);
237 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
240 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
241 if (db->count_set != 0)
244 db->count = (size_t)count;
249 /* call "fn" with each element of the list in turn */
250 static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER) *sk,
252 const unsigned char *buf,
258 for (i = 0; i < sk_EVP_TEST_BUFFER_num(sk); i++) {
259 EVP_TEST_BUFFER *tb = sk_EVP_TEST_BUFFER_value(sk, i);
262 for (j = 0; j < tb->count; j++) {
263 if (fn(ctx, tb->buf, tb->buflen) <= 0)
271 * Unescape some sequences in string literals (only \n for now).
272 * Return an allocated buffer, set |out_len|. If |input_len|
273 * is zero, get an empty buffer but set length to zero.
275 static unsigned char* unescape(const char *input, size_t input_len,
278 unsigned char *ret, *p;
281 if (input_len == 0) {
283 return OPENSSL_zalloc(1);
286 /* Escaping is non-expanding; over-allocate original size for simplicity. */
287 if (!TEST_ptr(ret = p = OPENSSL_malloc(input_len)))
290 for (i = 0; i < input_len; i++) {
291 if (*input == '\\') {
292 if (i == input_len - 1 || *++input != 'n') {
293 TEST_error("Bad escape sequence in file");
313 * For a hex string "value" convert to a binary allocated buffer.
314 * Return 1 on success or 0 on failure.
316 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen)
320 /* Check for NULL literal */
321 if (strcmp(value, "NULL") == 0) {
327 /* Check for empty value */
328 if (*value == '\0') {
330 * Don't return NULL for zero length buffer. This is needed for
331 * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key
332 * buffer even if the key length is 0, in order to detect key reset.
334 *buf = OPENSSL_malloc(1);
342 /* Check for string literal */
343 if (value[0] == '"') {
344 size_t vlen = strlen(++value);
346 if (vlen == 0 || value[vlen - 1] != '"')
349 *buf = unescape(value, vlen, buflen);
350 return *buf == NULL ? 0 : 1;
353 /* Otherwise assume as hex literal and convert it to binary buffer */
354 if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) {
355 TEST_info("Can't convert %s", value);
356 TEST_openssl_errors();
359 /* Size of input buffer means we'll never overflow */
365 * Convert at maximum "max" bytes to a binary allocated buffer.
366 * Return 1 on success, -1 on failure or 0 for end of value string.
368 static int parse_bin_chunk(const char *value, size_t offset, size_t max,
369 unsigned char **buf, size_t *buflen, size_t *out_offset)
373 const char *value_str = value[0] == '"' ? value + offset + 1 : value + offset;
378 if (*value == '\0' || strcmp(value, "\"\"") == 0) {
379 *buf = OPENSSL_malloc(1);
387 if (*value_str == '\0')
390 vlen = strlen(value_str);
391 if (value[0] == '"') {
392 /* Parse string literal */
393 if (vlen == 1 && value_str[0] != '"')
394 /* Missing ending quotation mark */
396 if (vlen == 1 && value_str[0] == '"')
400 chunk_len = max > vlen ? vlen : max;
401 if ((*buf = unescape(value_str, chunk_len, buflen)) == NULL)
404 /* Parse hex string chunk */
408 chunk_len = 2 * max > vlen ? vlen : 2 * max;
409 chunk = OPENSSL_strndup(value_str, chunk_len);
412 if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(chunk, &len))) {
414 TEST_openssl_errors();
420 *out_offset = value[0] == '"' ? offset + (*buflen) : offset + 2 * (*buflen);
425 ** MESSAGE DIGEST TESTS
428 typedef struct digest_data_st {
429 /* Digest this test is for */
430 const EVP_MD *digest;
431 EVP_MD *fetched_digest;
432 /* Input to digest */
433 STACK_OF(EVP_TEST_BUFFER) *input;
434 /* Expected output */
435 unsigned char *output;
441 /* Size for variable output length but non-XOF */
445 static int digest_test_init(EVP_TEST *t, const char *alg)
448 const EVP_MD *digest;
449 EVP_MD *fetched_digest;
451 if (is_digest_disabled(alg)) {
452 TEST_info("skipping, '%s' is disabled", alg);
457 if ((digest = fetched_digest = EVP_MD_fetch(libctx, alg, propquery)) == NULL
458 && (digest = EVP_get_digestbyname(alg)) == NULL)
460 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
463 mdat->digest = digest;
464 mdat->fetched_digest = fetched_digest;
467 if (fetched_digest != NULL)
468 TEST_info("%s is fetched", alg);
472 static void digest_test_cleanup(EVP_TEST *t)
474 DIGEST_DATA *mdat = t->data;
476 sk_EVP_TEST_BUFFER_pop_free(mdat->input, evp_test_buffer_free);
477 OPENSSL_free(mdat->output);
478 EVP_MD_free(mdat->fetched_digest);
481 static int digest_test_parse(EVP_TEST *t,
482 const char *keyword, const char *value)
484 DIGEST_DATA *mdata = t->data;
486 if (strcmp(keyword, "Input") == 0)
487 return evp_test_buffer_append(value, data_chunk_size, &mdata->input);
488 if (strcmp(keyword, "Output") == 0)
489 return parse_bin(value, &mdata->output, &mdata->output_len);
490 if (strcmp(keyword, "Count") == 0)
491 return evp_test_buffer_set_count(value, mdata->input);
492 if (strcmp(keyword, "Ncopy") == 0)
493 return evp_test_buffer_ncopy(value, mdata->input);
494 if (strcmp(keyword, "Padding") == 0)
495 return (mdata->pad_type = atoi(value)) > 0;
496 if (strcmp(keyword, "XOF") == 0)
497 return (mdata->xof = atoi(value)) > 0;
498 if (strcmp(keyword, "OutputSize") == 0) {
504 mdata->digest_size = sz;
510 static int digest_update_fn(void *ctx, const unsigned char *buf, size_t buflen)
512 return EVP_DigestUpdate(ctx, buf, buflen);
515 static int test_duplicate_md_ctx(EVP_TEST *t, EVP_MD_CTX *mctx)
517 char dont[] = "touch";
521 if (!EVP_DigestFinalXOF(mctx, (unsigned char *)dont, 0)) {
522 EVP_MD_CTX_free(mctx);
523 t->err = "DIGESTFINALXOF_ERROR";
526 if (!TEST_str_eq(dont, "touch")) {
527 EVP_MD_CTX_free(mctx);
528 t->err = "DIGESTFINALXOF_ERROR";
531 EVP_MD_CTX_free(mctx);
535 static int digest_test_run(EVP_TEST *t)
537 DIGEST_DATA *expected = t->data;
538 EVP_TEST_BUFFER *inbuf;
540 unsigned char *got = NULL;
541 unsigned int got_len;
544 OSSL_PARAM params[4], *p = ¶ms[0];
546 t->err = "TEST_FAILURE";
547 if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
550 got = OPENSSL_malloc(expected->output_len > EVP_MAX_MD_SIZE ?
551 expected->output_len : EVP_MAX_MD_SIZE);
555 if (expected->xof > 0) {
557 *p++ = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN,
558 &expected->output_len);
560 if (expected->digest_size > 0) {
561 *p++ = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE,
562 &expected->digest_size);
564 if (expected->pad_type > 0)
565 *p++ = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_PAD_TYPE,
566 &expected->pad_type);
567 *p++ = OSSL_PARAM_construct_end();
569 if (!EVP_DigestInit_ex2(mctx, expected->digest, params)) {
570 t->err = "DIGESTINIT_ERROR";
574 if (!evp_test_buffer_do(expected->input, digest_update_fn, mctx)) {
575 t->err = "DIGESTUPDATE_ERROR";
579 xof |= (EVP_MD_get_flags(expected->digest) & EVP_MD_FLAG_XOF) != 0;
581 EVP_MD_CTX *mctx_cpy;
583 if (!TEST_ptr(mctx_cpy = EVP_MD_CTX_new())) {
586 if (!TEST_true(EVP_MD_CTX_copy(mctx_cpy, mctx))) {
587 EVP_MD_CTX_free(mctx_cpy);
589 } else if (!test_duplicate_md_ctx(t, mctx_cpy)) {
593 if (!test_duplicate_md_ctx(t, EVP_MD_CTX_dup(mctx)))
596 got_len = expected->output_len;
597 if (!EVP_DigestFinalXOF(mctx, got, got_len)) {
598 t->err = "DIGESTFINALXOF_ERROR";
602 if (!EVP_DigestFinal(mctx, got, &got_len)) {
603 t->err = "DIGESTFINAL_ERROR";
607 if (!TEST_int_eq(expected->output_len, got_len)) {
608 t->err = "DIGEST_LENGTH_MISMATCH";
611 if (!memory_err_compare(t, "DIGEST_MISMATCH",
612 expected->output, expected->output_len,
618 /* Test the EVP_Q_digest interface as well */
619 if (sk_EVP_TEST_BUFFER_num(expected->input) == 1
621 /* This should never fail but we need the returned pointer now */
622 && !TEST_ptr(inbuf = sk_EVP_TEST_BUFFER_value(expected->input, 0))
623 && !inbuf->count_set) {
624 OPENSSL_cleanse(got, got_len);
625 if (!TEST_true(EVP_Q_digest(libctx,
626 EVP_MD_get0_name(expected->fetched_digest),
627 NULL, inbuf->buf, inbuf->buflen,
629 || !TEST_mem_eq(got, size,
630 expected->output, expected->output_len)) {
631 t->err = "EVP_Q_digest failed";
638 EVP_MD_CTX_free(mctx);
642 static const EVP_TEST_METHOD digest_test_method = {
654 typedef struct cipher_data_st {
655 const EVP_CIPHER *cipher;
656 EVP_CIPHER *fetched_cipher;
658 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
662 size_t key_bits; /* Used by RC2 */
664 unsigned char *next_iv; /* Expected IV state after operation */
667 unsigned char *plaintext;
668 size_t plaintext_len;
669 unsigned char *ciphertext;
670 size_t ciphertext_len;
671 /* AEAD ciphers only */
672 unsigned char *aad[AAD_NUM];
673 size_t aad_len[AAD_NUM];
677 const char *cts_mode;
680 unsigned char *mac_key;
682 const char *xts_standard;
687 * XTS, SIV, CCM, stitched ciphers and Wrap modes have special
688 * requirements about input lengths so we don't fragment for those
690 static int cipher_test_valid_fragmentation(CIPHER_DATA *cdat)
692 return (cdat->aead == EVP_CIPH_CCM_MODE
693 || cdat->aead == EVP_CIPH_CBC_MODE
695 && EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_STREAM_CIPHER)
696 || ((EVP_CIPHER_get_flags(cdat->cipher) & EVP_CIPH_FLAG_CTS) != 0)
697 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_SIV_MODE
698 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_GCM_SIV_MODE
699 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
700 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE) ? 0 : 1;
703 static int cipher_test_init(EVP_TEST *t, const char *alg)
705 const EVP_CIPHER *cipher;
706 EVP_CIPHER *fetched_cipher;
710 if (is_cipher_disabled(alg)) {
712 TEST_info("skipping, '%s' is disabled", alg);
717 if ((cipher = fetched_cipher = EVP_CIPHER_fetch(libctx, alg, propquery)) == NULL
718 && (cipher = EVP_get_cipherbyname(alg)) == NULL) {
719 /* a stitched cipher might not be available */
720 if (strstr(alg, "HMAC") != NULL) {
723 TEST_info("skipping, '%s' is not available", alg);
726 ERR_clear_last_mark();
729 ERR_clear_last_mark();
731 if (!TEST_ptr(cdat = OPENSSL_zalloc(sizeof(*cdat))))
734 cdat->cipher = cipher;
735 cdat->fetched_cipher = fetched_cipher;
737 m = EVP_CIPHER_get_mode(cipher);
738 if (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
739 cdat->aead = m != 0 ? m : -1;
743 if (data_chunk_size != 0 && !cipher_test_valid_fragmentation(cdat)) {
745 EVP_CIPHER_free(fetched_cipher);
748 TEST_info("skipping, '%s' does not support fragmentation", alg);
753 if (fetched_cipher != NULL)
754 TEST_info("%s is fetched", alg);
758 static void cipher_test_cleanup(EVP_TEST *t)
761 CIPHER_DATA *cdat = t->data;
763 OPENSSL_free(cdat->key);
764 OPENSSL_free(cdat->iv);
765 OPENSSL_free(cdat->next_iv);
766 OPENSSL_free(cdat->ciphertext);
767 OPENSSL_free(cdat->plaintext);
768 for (i = 0; i < AAD_NUM; i++)
769 OPENSSL_free(cdat->aad[i]);
770 OPENSSL_free(cdat->tag);
771 OPENSSL_free(cdat->mac_key);
772 EVP_CIPHER_free(cdat->fetched_cipher);
775 static int cipher_test_parse(EVP_TEST *t, const char *keyword,
778 CIPHER_DATA *cdat = t->data;
781 if (strcmp(keyword, "Key") == 0)
782 return parse_bin(value, &cdat->key, &cdat->key_len);
783 if (strcmp(keyword, "Rounds") == 0) {
787 cdat->rounds = (unsigned int)i;
790 if (strcmp(keyword, "IV") == 0)
791 return parse_bin(value, &cdat->iv, &cdat->iv_len);
792 if (strcmp(keyword, "NextIV") == 0)
793 return parse_bin(value, &cdat->next_iv, &cdat->iv_len);
794 if (strcmp(keyword, "Plaintext") == 0)
795 return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len);
796 if (strcmp(keyword, "Ciphertext") == 0)
797 return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
798 if (strcmp(keyword, "KeyBits") == 0) {
802 cdat->key_bits = (size_t)i;
808 if (strcmp(keyword, "TLSAAD") == 0)
809 cdat->tls_aad = tls_aad = 1;
810 if (strcmp(keyword, "AAD") == 0 || tls_aad) {
811 for (i = 0; i < AAD_NUM; i++) {
812 if (cdat->aad[i] == NULL)
813 return parse_bin(value, &cdat->aad[i], &cdat->aad_len[i]);
817 if (strcmp(keyword, "Tag") == 0)
818 return parse_bin(value, &cdat->tag, &cdat->tag_len);
819 if (strcmp(keyword, "SetTagLate") == 0) {
820 if (strcmp(value, "TRUE") == 0)
822 else if (strcmp(value, "FALSE") == 0)
828 if (strcmp(keyword, "MACKey") == 0)
829 return parse_bin(value, &cdat->mac_key, &cdat->mac_key_len);
830 if (strcmp(keyword, "TLSVersion") == 0) {
833 cdat->tls_version = (int)strtol(value, &endptr, 0);
834 return value[0] != '\0' && endptr[0] == '\0';
838 if (strcmp(keyword, "Operation") == 0) {
839 if (strcmp(value, "ENCRYPT") == 0)
841 else if (strcmp(value, "DECRYPT") == 0)
847 if (strcmp(keyword, "CTSMode") == 0) {
848 cdat->cts_mode = value;
851 if (strcmp(keyword, "XTSStandard") == 0) {
852 cdat->xts_standard = value;
858 static int cipher_test_enc(EVP_TEST *t, int enc, size_t out_misalign,
859 size_t inp_misalign, int frag, int in_place)
861 CIPHER_DATA *expected = t->data;
862 unsigned char *in, *expected_out, *tmp = NULL;
863 size_t in_len, out_len, donelen = 0;
864 int ok = 0, tmplen, chunklen, tmpflen, i;
865 EVP_CIPHER_CTX *ctx_base = NULL;
866 EVP_CIPHER_CTX *ctx = NULL, *duped;
867 int fips_dupctx_supported = fips_provider_version_ge(libctx, 3, 2, 0);
869 t->err = "TEST_FAILURE";
870 if (!TEST_ptr(ctx_base = EVP_CIPHER_CTX_new()))
872 if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
874 EVP_CIPHER_CTX_set_flags(ctx_base, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
876 in = expected->plaintext;
877 in_len = expected->plaintext_len;
878 expected_out = expected->ciphertext;
879 out_len = expected->ciphertext_len;
881 in = expected->ciphertext;
882 in_len = expected->ciphertext_len;
883 expected_out = expected->plaintext;
884 out_len = expected->plaintext_len;
887 /* Exercise in-place encryption */
888 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
891 in = memcpy(tmp + out_misalign, in, in_len);
893 inp_misalign += 16 - ((out_misalign + in_len) & 15);
895 * 'tmp' will store both output and copy of input. We make the copy
896 * of input to specifically aligned part of 'tmp'. So we just
897 * figured out how much padding would ensure the required alignment,
898 * now we allocate extended buffer and finally copy the input just
899 * past inp_misalign in expression below. Output will be written
900 * past out_misalign...
902 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
903 inp_misalign + in_len);
906 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
907 inp_misalign, in, in_len);
909 if (!EVP_CipherInit_ex(ctx_base, expected->cipher, NULL, NULL, NULL, enc)) {
910 t->err = "CIPHERINIT_ERROR";
913 if (expected->cts_mode != NULL) {
914 OSSL_PARAM params[2];
916 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE,
917 (char *)expected->cts_mode,
919 params[1] = OSSL_PARAM_construct_end();
920 if (!EVP_CIPHER_CTX_set_params(ctx_base, params)) {
921 t->err = "INVALID_CTS_MODE";
926 if (expected->aead) {
927 if (EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_IVLEN,
928 expected->iv_len, 0) <= 0) {
929 t->err = "INVALID_IV_LENGTH";
932 } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_get_iv_length(ctx_base)) {
933 t->err = "INVALID_IV_LENGTH";
937 if (expected->aead && !expected->tls_aad) {
940 * If encrypting or OCB just set tag length initially, otherwise
941 * set tag length and value.
943 if (enc || expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late) {
944 t->err = "TAG_LENGTH_SET_ERROR";
947 t->err = "TAG_SET_ERROR";
950 if (tag || expected->aead != EVP_CIPH_GCM_MODE) {
951 if (EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_TAG,
952 expected->tag_len, tag) <= 0)
957 if (expected->rounds > 0) {
958 int rounds = (int)expected->rounds;
960 if (EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC5_ROUNDS, rounds, NULL) <= 0) {
961 t->err = "INVALID_ROUNDS";
966 if (!EVP_CIPHER_CTX_set_key_length(ctx_base, expected->key_len)) {
967 t->err = "INVALID_KEY_LENGTH";
970 if (expected->key_bits > 0) {
971 int bits = (int)expected->key_bits;
973 if (EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC2_KEY_BITS, bits, NULL) <= 0) {
974 t->err = "INVALID KEY BITS";
978 if (!EVP_CipherInit_ex(ctx_base, NULL, NULL, expected->key, expected->iv, -1)) {
979 t->err = "KEY_SET_ERROR";
983 /* Check that we get the same IV back */
984 if (expected->iv != NULL) {
985 /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
986 unsigned char iv[128];
987 if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx_base, iv, sizeof(iv)))
988 || ((EVP_CIPHER_get_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
989 && !TEST_mem_eq(expected->iv, expected->iv_len, iv,
990 expected->iv_len))) {
991 t->err = "INVALID_IV";
996 /* Test that the cipher dup functions correctly if it is supported */
998 if (!EVP_CIPHER_CTX_copy(ctx, ctx_base)) {
999 if (fips_dupctx_supported) {
1000 TEST_info("Doing a copy of Cipher %s Fails!\n",
1001 EVP_CIPHER_get0_name(expected->cipher));
1002 ERR_print_errors_fp(stderr);
1005 TEST_info("Allowing copy fail as an old fips provider is in use.");
1007 EVP_CIPHER_CTX_free(ctx);
1010 EVP_CIPHER_CTX_free(ctx_base);
1013 /* Likewise for dup */
1014 duped = EVP_CIPHER_CTX_dup(ctx);
1015 if (duped != NULL) {
1016 EVP_CIPHER_CTX_free(ctx);
1019 if (fips_dupctx_supported) {
1020 TEST_info("Doing a dup of Cipher %s Fails!\n",
1021 EVP_CIPHER_get0_name(expected->cipher));
1022 ERR_print_errors_fp(stderr);
1025 TEST_info("Allowing dup fail as an old fips provider is in use.");
1030 if (expected->mac_key != NULL
1031 && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY,
1032 (int)expected->mac_key_len,
1033 (void *)expected->mac_key) <= 0) {
1034 t->err = "SET_MAC_KEY_ERROR";
1038 if (expected->tls_version) {
1039 OSSL_PARAM params[2];
1041 params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS_VERSION,
1042 &expected->tls_version);
1043 params[1] = OSSL_PARAM_construct_end();
1044 if (!EVP_CIPHER_CTX_set_params(ctx, params)) {
1045 t->err = "SET_TLS_VERSION_ERROR";
1050 if (expected->aead == EVP_CIPH_CCM_MODE) {
1051 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
1052 t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
1056 if (expected->aad[0] != NULL && !expected->tls_aad) {
1057 t->err = "AAD_SET_ERROR";
1059 /* Supply the data all in one go or according to data_chunk_size */
1060 for (i = 0; expected->aad[i] != NULL; i++) {
1061 size_t aad_len = expected->aad_len[i];
1065 size_t current_aad_len = (size_t) data_chunk_size;
1067 if (data_chunk_size == 0 || (size_t) data_chunk_size > aad_len)
1068 current_aad_len = aad_len;
1069 if (!EVP_CipherUpdate(ctx, NULL, &chunklen,
1070 expected->aad[i] + donelen,
1073 donelen += current_aad_len;
1074 aad_len -= current_aad_len;
1075 } while (aad_len > 0);
1078 /* Supply the AAD in chunks less than the block size where possible */
1079 for (i = 0; expected->aad[i] != NULL; i++) {
1080 if (expected->aad_len[i] > 0) {
1081 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], 1))
1085 if (expected->aad_len[i] > 2) {
1086 if (!EVP_CipherUpdate(ctx, NULL, &chunklen,
1087 expected->aad[i] + donelen,
1088 expected->aad_len[i] - 2))
1090 donelen += expected->aad_len[i] - 2;
1092 if (expected->aad_len[i] > 1
1093 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
1094 expected->aad[i] + donelen, 1))
1100 if (expected->tls_aad) {
1101 OSSL_PARAM params[2];
1104 /* duplicate the aad as the implementation might modify it */
1105 if ((tls_aad = OPENSSL_memdup(expected->aad[0],
1106 expected->aad_len[0])) == NULL)
1108 params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
1110 expected->aad_len[0]);
1111 params[1] = OSSL_PARAM_construct_end();
1112 if (!EVP_CIPHER_CTX_set_params(ctx, params)) {
1113 OPENSSL_free(tls_aad);
1114 t->err = "TLS1_AAD_ERROR";
1117 OPENSSL_free(tls_aad);
1118 } else if (!enc && (expected->aead == EVP_CIPH_OCB_MODE
1119 || expected->tag_late)) {
1120 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
1121 expected->tag_len, expected->tag) <= 0) {
1122 t->err = "TAG_SET_ERROR";
1126 if (expected->xts_standard != NULL) {
1127 OSSL_PARAM params[2];
1130 OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_XTS_STANDARD,
1131 (char *)expected->xts_standard, 0);
1132 params[1] = OSSL_PARAM_construct_end();
1133 if (!EVP_CIPHER_CTX_set_params(ctx, params)) {
1134 t->err = "SET_XTS_STANDARD_ERROR";
1138 EVP_CIPHER_CTX_set_padding(ctx, 0);
1139 t->err = "CIPHERUPDATE_ERROR";
1143 /* Supply the data all in one go or according to data_chunk_size */
1144 size_t current_in_len = (size_t) data_chunk_size;
1146 if (data_chunk_size == 0 || (size_t) data_chunk_size > in_len)
1147 current_in_len = in_len;
1148 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
1149 in, current_in_len))
1152 in += current_in_len;
1153 in_len -= current_in_len;
1154 } while (in_len > 0);
1156 /* Supply the data in chunks less than the block size where possible */
1158 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
1165 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
1173 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
1179 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
1180 t->err = "CIPHERFINAL_ERROR";
1183 if (!enc && expected->tls_aad) {
1184 if (expected->tls_version >= TLS1_1_VERSION
1185 && (EVP_CIPHER_is_a(expected->cipher, "AES-128-CBC-HMAC-SHA1")
1186 || EVP_CIPHER_is_a(expected->cipher, "AES-256-CBC-HMAC-SHA1"))) {
1187 tmplen -= expected->iv_len;
1188 expected_out += expected->iv_len;
1189 out_misalign += expected->iv_len;
1191 if ((int)out_len > tmplen + tmpflen)
1192 out_len = tmplen + tmpflen;
1194 if (!memory_err_compare(t, "VALUE_MISMATCH", expected_out, out_len,
1195 tmp + out_misalign, tmplen + tmpflen))
1197 if (enc && expected->aead && !expected->tls_aad) {
1198 unsigned char rtag[48]; /* longest known for TLS_SHA384_SHA384 */
1200 if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) {
1201 t->err = "TAG_LENGTH_INTERNAL_ERROR";
1204 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
1205 expected->tag_len, rtag) <= 0) {
1206 t->err = "TAG_RETRIEVE_ERROR";
1209 if (!memory_err_compare(t, "TAG_VALUE_MISMATCH",
1210 expected->tag, expected->tag_len,
1211 rtag, expected->tag_len))
1214 /* Check the updated IV */
1215 if (expected->next_iv != NULL) {
1216 /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
1217 unsigned char iv[128];
1218 if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv)))
1219 || ((EVP_CIPHER_get_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
1220 && !TEST_mem_eq(expected->next_iv, expected->iv_len, iv,
1221 expected->iv_len))) {
1222 t->err = "INVALID_NEXT_IV";
1231 if (ctx != ctx_base)
1232 EVP_CIPHER_CTX_free(ctx_base);
1233 EVP_CIPHER_CTX_free(ctx);
1237 static int cipher_test_run(EVP_TEST *t)
1239 CIPHER_DATA *cdat = t->data;
1240 int rv, frag, fragmax, in_place;
1241 size_t out_misalign, inp_misalign;
1243 TEST_info("RUNNING TEST FOR CIPHER %s\n", EVP_CIPHER_get0_name(cdat->cipher));
1248 if (!cdat->iv && EVP_CIPHER_get_iv_length(cdat->cipher) > 0) {
1249 /* IV is optional and usually omitted in wrap mode */
1250 if (EVP_CIPHER_get_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
1255 if (cdat->aead && cdat->tag == NULL && !cdat->tls_aad) {
1260 fragmax = (cipher_test_valid_fragmentation(cdat) == 0) ? 0 : 1;
1261 for (in_place = 1; in_place >= 0; in_place--) {
1262 static char aux_err[64];
1264 t->aux_err = aux_err;
1265 /* Test only in-place data processing */
1266 if (process_mode_in_place == 1 && in_place == 0)
1269 for (frag = 0; frag <= fragmax; frag++) {
1270 if (frag == 1 && data_chunk_size != 0)
1272 for (out_misalign = 0; out_misalign <= 1; out_misalign++) {
1273 for (inp_misalign = 0; inp_misalign <= 1; inp_misalign++) {
1274 /* Skip input misalign tests for in-place processing */
1275 if (inp_misalign == 1 && in_place == 1)
1277 if (in_place == 1) {
1278 BIO_snprintf(aux_err, sizeof(aux_err),
1279 "%s in-place, %sfragmented",
1280 out_misalign ? "misaligned" : "aligned",
1281 frag ? "" : "not ");
1283 BIO_snprintf(aux_err, sizeof(aux_err),
1284 "%s output and %s input, %sfragmented",
1285 out_misalign ? "misaligned" : "aligned",
1286 inp_misalign ? "misaligned" : "aligned",
1287 frag ? "" : "not ");
1290 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign,
1292 /* Not fatal errors: return */
1299 if (cdat->enc != 1) {
1300 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign,
1302 /* Not fatal errors: return */
1318 static const EVP_TEST_METHOD cipher_test_method = {
1321 cipher_test_cleanup,
1331 typedef struct mac_data_st {
1332 /* MAC type in one form or another */
1334 EVP_MAC *mac; /* for mac_test_run_mac */
1335 int type; /* for mac_test_run_pkey */
1336 /* Algorithm string for this MAC */
1345 unsigned char *input;
1347 /* Expected output */
1348 unsigned char *output;
1350 unsigned char *custom;
1352 /* MAC salt (blake2) */
1353 unsigned char *salt;
1357 /* Reinitialization fails */
1359 /* Collection of controls */
1360 STACK_OF(OPENSSL_STRING) *controls;
1367 static int mac_test_init(EVP_TEST *t, const char *alg)
1369 EVP_MAC *mac = NULL;
1370 int type = NID_undef;
1373 if (is_mac_disabled(alg)) {
1374 TEST_info("skipping, '%s' is disabled", alg);
1378 if ((mac = EVP_MAC_fetch(libctx, alg, propquery)) == NULL) {
1380 * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
1381 * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
1382 * the EVP_PKEY method.
1384 size_t sz = strlen(alg);
1385 static const char epilogue[] = " by EVP_PKEY";
1387 if (sz >= sizeof(epilogue)
1388 && strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0)
1389 sz -= sizeof(epilogue) - 1;
1391 if (strncmp(alg, "HMAC", sz) == 0)
1392 type = EVP_PKEY_HMAC;
1393 else if (strncmp(alg, "CMAC", sz) == 0)
1394 type = EVP_PKEY_CMAC;
1395 else if (strncmp(alg, "Poly1305", sz) == 0)
1396 type = EVP_PKEY_POLY1305;
1397 else if (strncmp(alg, "SipHash", sz) == 0)
1398 type = EVP_PKEY_SIPHASH;
1403 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
1407 if (!TEST_ptr(mdat->mac_name = OPENSSL_strdup(alg))) {
1413 if (!TEST_ptr(mdat->controls = sk_OPENSSL_STRING_new_null())) {
1414 OPENSSL_free(mdat->mac_name);
1419 mdat->output_size = mdat->block_size = -1;
1424 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
1425 static void openssl_free(char *m)
1430 static void mac_test_cleanup(EVP_TEST *t)
1432 MAC_DATA *mdat = t->data;
1434 EVP_MAC_free(mdat->mac);
1435 OPENSSL_free(mdat->mac_name);
1436 sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free);
1437 OPENSSL_free(mdat->alg);
1438 OPENSSL_free(mdat->key);
1439 OPENSSL_free(mdat->iv);
1440 OPENSSL_free(mdat->custom);
1441 OPENSSL_free(mdat->salt);
1442 OPENSSL_free(mdat->input);
1443 OPENSSL_free(mdat->output);
1446 static int mac_test_parse(EVP_TEST *t,
1447 const char *keyword, const char *value)
1449 MAC_DATA *mdata = t->data;
1451 if (strcmp(keyword, "Key") == 0)
1452 return parse_bin(value, &mdata->key, &mdata->key_len);
1453 if (strcmp(keyword, "IV") == 0)
1454 return parse_bin(value, &mdata->iv, &mdata->iv_len);
1455 if (strcmp(keyword, "Custom") == 0)
1456 return parse_bin(value, &mdata->custom, &mdata->custom_len);
1457 if (strcmp(keyword, "Salt") == 0)
1458 return parse_bin(value, &mdata->salt, &mdata->salt_len);
1459 if (strcmp(keyword, "Algorithm") == 0) {
1460 mdata->alg = OPENSSL_strdup(value);
1461 if (mdata->alg == NULL)
1465 if (strcmp(keyword, "Input") == 0)
1466 return parse_bin(value, &mdata->input, &mdata->input_len);
1467 if (strcmp(keyword, "Output") == 0)
1468 return parse_bin(value, &mdata->output, &mdata->output_len);
1469 if (strcmp(keyword, "XOF") == 0)
1470 return mdata->xof = 1;
1471 if (strcmp(keyword, "NoReinit") == 0)
1472 return mdata->no_reinit = 1;
1473 if (strcmp(keyword, "Ctrl") == 0) {
1474 char *data = OPENSSL_strdup(value);
1478 return sk_OPENSSL_STRING_push(mdata->controls, data) != 0;
1480 if (strcmp(keyword, "OutputSize") == 0) {
1481 mdata->output_size = atoi(value);
1482 if (mdata->output_size < 0)
1486 if (strcmp(keyword, "BlockSize") == 0) {
1487 mdata->block_size = atoi(value);
1488 if (mdata->block_size < 0)
1495 static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1501 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1503 p = strchr(tmpval, ':');
1506 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1509 t->err = "PKEY_CTRL_INVALID";
1511 t->err = "PKEY_CTRL_ERROR";
1514 OPENSSL_free(tmpval);
1518 static int mac_test_run_pkey(EVP_TEST *t)
1520 MAC_DATA *expected = t->data;
1521 EVP_MD_CTX *mctx = NULL;
1522 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1523 EVP_PKEY *key = NULL;
1524 const char *mdname = NULL;
1525 EVP_CIPHER *cipher = NULL;
1526 unsigned char *got = NULL;
1529 size_t input_len, donelen;
1531 /* We don't do XOF mode via PKEY */
1535 if (expected->alg == NULL)
1536 TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type));
1538 TEST_info("Trying the EVP_PKEY %s test with %s",
1539 OBJ_nid2sn(expected->type), expected->alg);
1541 if (expected->type == EVP_PKEY_CMAC) {
1542 #ifdef OPENSSL_NO_DEPRECATED_3_0
1543 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
1548 OSSL_LIB_CTX *tmpctx;
1550 if (expected->alg != NULL && is_cipher_disabled(expected->alg)) {
1551 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
1556 if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, expected->alg, propquery))) {
1557 t->err = "MAC_KEY_CREATE_ERROR";
1560 tmpctx = OSSL_LIB_CTX_set0_default(libctx);
1561 key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len,
1563 OSSL_LIB_CTX_set0_default(tmpctx);
1566 key = EVP_PKEY_new_raw_private_key_ex(libctx,
1567 OBJ_nid2sn(expected->type), NULL,
1568 expected->key, expected->key_len);
1571 t->err = "MAC_KEY_CREATE_ERROR";
1575 if (expected->type == EVP_PKEY_HMAC && expected->alg != NULL) {
1576 if (is_digest_disabled(expected->alg)) {
1577 TEST_info("skipping, HMAC '%s' is disabled", expected->alg);
1582 mdname = expected->alg;
1584 if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
1585 t->err = "INTERNAL_ERROR";
1588 if (!EVP_DigestSignInit_ex(mctx, &pctx, mdname, libctx, NULL, key, NULL)) {
1589 t->err = "DIGESTSIGNINIT_ERROR";
1592 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++)
1593 if (!mac_test_ctrl_pkey(t, pctx,
1594 sk_OPENSSL_STRING_value(expected->controls,
1596 t->err = "EVPPKEYCTXCTRL_ERROR";
1599 input_len = expected->input_len;
1602 size_t current_len = (size_t) data_chunk_size;
1604 if (data_chunk_size == 0 || (size_t) data_chunk_size > input_len)
1605 current_len = input_len;
1606 if (!EVP_DigestSignUpdate(mctx, expected->input + donelen, current_len)) {
1607 t->err = "DIGESTSIGNUPDATE_ERROR";
1610 donelen += current_len;
1611 input_len -= current_len;
1612 } while (input_len > 0);
1614 if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) {
1615 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1618 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1619 t->err = "TEST_FAILURE";
1622 if (!EVP_DigestSignFinal(mctx, got, &got_len)
1623 || !memory_err_compare(t, "TEST_MAC_ERR",
1624 expected->output, expected->output_len,
1626 t->err = "TEST_MAC_ERR";
1631 EVP_CIPHER_free(cipher);
1632 EVP_MD_CTX_free(mctx);
1634 EVP_PKEY_CTX_free(genctx);
1639 static int mac_test_run_mac(EVP_TEST *t)
1641 MAC_DATA *expected = t->data;
1642 EVP_MAC_CTX *ctx = NULL;
1643 unsigned char *got = NULL;
1644 size_t got_len = 0, size = 0;
1645 size_t size_before_init = 0, size_after_init, size_val = 0;
1646 int i, block_size = -1, output_size = -1;
1647 OSSL_PARAM params[21], sizes[3], *psizes = sizes;
1648 size_t params_n = 0;
1649 size_t params_n_allocstart = 0;
1650 const OSSL_PARAM *defined_params =
1651 EVP_MAC_settable_ctx_params(expected->mac);
1654 size_t input_len, donelen ;
1656 if (expected->alg == NULL)
1657 TEST_info("Trying the EVP_MAC %s test", expected->mac_name);
1659 TEST_info("Trying the EVP_MAC %s test with %s",
1660 expected->mac_name, expected->alg);
1662 if (expected->alg != NULL) {
1666 * The underlying algorithm may be a cipher or a digest.
1667 * We don't know which it is, but we can ask the MAC what it
1668 * should be and bet on that.
1670 if (OSSL_PARAM_locate_const(defined_params,
1671 OSSL_MAC_PARAM_CIPHER) != NULL) {
1672 if (is_cipher_disabled(expected->alg))
1675 params[params_n++] =
1676 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
1678 } else if (OSSL_PARAM_locate_const(defined_params,
1679 OSSL_MAC_PARAM_DIGEST) != NULL) {
1680 if (is_digest_disabled(expected->alg))
1683 params[params_n++] =
1684 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
1687 t->err = "MAC_BAD_PARAMS";
1691 TEST_info("skipping, algorithm '%s' is disabled", expected->alg);
1697 if (expected->custom != NULL)
1698 params[params_n++] =
1699 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
1701 expected->custom_len);
1702 if (expected->salt != NULL)
1703 params[params_n++] =
1704 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT,
1706 expected->salt_len);
1707 if (expected->iv != NULL)
1708 params[params_n++] =
1709 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
1713 /* Unknown controls. They must match parameters that the MAC recognizes */
1714 if (params_n + sk_OPENSSL_STRING_num(expected->controls)
1715 >= OSSL_NELEM(params)) {
1716 t->err = "MAC_TOO_MANY_PARAMETERS";
1719 params_n_allocstart = params_n;
1720 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) {
1721 char *tmpkey, *tmpval;
1722 char *value = sk_OPENSSL_STRING_value(expected->controls, i);
1724 if (!TEST_ptr(tmpkey = OPENSSL_strdup(value))) {
1725 t->err = "MAC_PARAM_ERROR";
1728 tmpval = strchr(tmpkey, ':');
1733 || !OSSL_PARAM_allocate_from_text(¶ms[params_n],
1736 strlen(tmpval), NULL)) {
1737 OPENSSL_free(tmpkey);
1738 t->err = "MAC_PARAM_ERROR";
1743 if (strcmp(tmpkey, "size") == 0)
1744 size_val = (size_t)strtoul(tmpval, NULL, 0);
1746 OPENSSL_free(tmpkey);
1748 params[params_n] = OSSL_PARAM_construct_end();
1750 if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) {
1751 t->err = "MAC_CREATE_ERROR";
1754 if (fips_provider_version_gt(libctx, 3, 2, 0))
1755 size_before_init = EVP_MAC_CTX_get_mac_size(ctx);
1756 if (!EVP_MAC_init(ctx, expected->key, expected->key_len, params)) {
1757 t->err = "MAC_INIT_ERROR";
1760 size_after_init = EVP_MAC_CTX_get_mac_size(ctx);
1761 if (!TEST_false(size_before_init == 0 && size_after_init == 0)) {
1762 t->err = "MAC SIZE not set";
1765 if (size_before_init != 0) {
1766 /* mac-size not modified by init params */
1767 if (size_val == 0 && !TEST_size_t_eq(size_before_init, size_after_init)) {
1768 t->err = "MAC SIZE check failed";
1771 /* mac-size modified by init params */
1772 if (size_val != 0 && !TEST_size_t_eq(size_val, size_after_init)) {
1773 t->err = "MAC SIZE check failed";
1777 if (expected->output_size >= 0)
1778 *psizes++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_SIZE,
1780 if (expected->block_size >= 0)
1781 *psizes++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_BLOCK_SIZE,
1783 if (psizes != sizes) {
1784 *psizes = OSSL_PARAM_construct_end();
1785 if (!TEST_true(EVP_MAC_CTX_get_params(ctx, sizes))) {
1786 t->err = "INTERNAL_ERROR";
1789 if (expected->output_size >= 0
1790 && !TEST_int_eq(output_size, expected->output_size)) {
1791 t->err = "TEST_FAILURE";
1794 if (expected->block_size >= 0
1795 && !TEST_int_eq(block_size, expected->block_size)) {
1796 t->err = "TEST_FAILURE";
1801 input_len = expected->input_len;
1804 size_t current_len = (size_t) data_chunk_size;
1806 if (data_chunk_size == 0 || (size_t) data_chunk_size > input_len)
1807 current_len = input_len;
1808 if (!EVP_MAC_update(ctx, expected->input + donelen, current_len)) {
1809 t->err = "MAC_UPDATE_ERROR";
1812 donelen += current_len;
1813 input_len -= current_len;
1814 } while (input_len > 0);
1816 xof = expected->xof;
1818 if (!TEST_ptr(got = OPENSSL_malloc(expected->output_len))) {
1819 t->err = "TEST_FAILURE";
1822 if (!EVP_MAC_finalXOF(ctx, got, expected->output_len)
1823 || !memory_err_compare(t, "TEST_MAC_ERR",
1824 expected->output, expected->output_len,
1825 got, expected->output_len)) {
1826 t->err = "MAC_FINAL_ERROR";
1830 if (!EVP_MAC_final(ctx, NULL, &got_len, 0)) {
1831 t->err = "MAC_FINAL_LENGTH_ERROR";
1834 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1835 t->err = "TEST_FAILURE";
1838 if (!EVP_MAC_final(ctx, got, &got_len, got_len)
1839 || !memory_err_compare(t, "TEST_MAC_ERR",
1840 expected->output, expected->output_len,
1842 t->err = "TEST_MAC_ERR";
1846 /* FIPS(3.0.0): can't reinitialise MAC contexts #18100 */
1847 if (reinit-- && fips_provider_version_gt(libctx, 3, 0, 0)) {
1848 OSSL_PARAM ivparams[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1851 /* If the MAC uses IV, we have to set it again */
1852 if (expected->iv != NULL) {
1854 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
1857 ivparams[1] = OSSL_PARAM_construct_end();
1860 ret = EVP_MAC_init(ctx, NULL, 0, ivparams);
1861 if (expected->no_reinit) {
1863 ERR_clear_last_mark();
1864 t->err = "MAC_REINIT_SHOULD_FAIL";
1868 ERR_clear_last_mark();
1873 ERR_clear_last_mark();
1874 t->err = "MAC_REINIT_ERROR";
1877 /* If reinitialization fails, it is unsupported by the algorithm */
1882 /* Test the EVP_Q_mac interface as well */
1884 OPENSSL_cleanse(got, got_len);
1885 if (!TEST_true(EVP_Q_mac(libctx, expected->mac_name, NULL,
1886 expected->alg, params,
1887 expected->key, expected->key_len,
1888 expected->input, expected->input_len,
1889 got, got_len, &size))
1890 || !TEST_mem_eq(got, size,
1891 expected->output, expected->output_len)) {
1892 t->err = "EVP_Q_mac failed";
1897 while (params_n-- > params_n_allocstart) {
1898 OPENSSL_free(params[params_n].data);
1900 EVP_MAC_CTX_free(ctx);
1905 static int mac_test_run(EVP_TEST *t)
1907 MAC_DATA *expected = t->data;
1909 if (expected->mac != NULL)
1910 return mac_test_run_mac(t);
1911 return mac_test_run_pkey(t);
1914 static const EVP_TEST_METHOD mac_test_method = {
1925 ** These are all very similar and share much common code.
1928 typedef struct pkey_data_st {
1929 /* Context for this operation */
1931 /* Key operation to perform */
1932 int (*keyop) (EVP_PKEY_CTX *ctx,
1933 unsigned char *sig, size_t *siglen,
1934 const unsigned char *tbs, size_t tbslen);
1936 unsigned char *input;
1938 /* Expected output */
1939 unsigned char *output;
1944 * Perform public key operation setup: lookup key, allocated ctx and call
1945 * the appropriate initialisation function
1947 static int pkey_test_init(EVP_TEST *t, const char *name,
1949 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1950 int (*keyop)(EVP_PKEY_CTX *ctx,
1951 unsigned char *sig, size_t *siglen,
1952 const unsigned char *tbs,
1956 EVP_PKEY *pkey = NULL;
1960 rv = find_key(&pkey, name, public_keys);
1962 rv = find_key(&pkey, name, private_keys);
1963 if (rv == 0 || pkey == NULL) {
1964 TEST_info("skipping, key '%s' is disabled", name);
1969 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) {
1970 EVP_PKEY_free(pkey);
1973 kdata->keyop = keyop;
1974 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propquery))) {
1975 EVP_PKEY_free(pkey);
1976 OPENSSL_free(kdata);
1979 if (keyopinit(kdata->ctx) <= 0)
1980 t->err = "KEYOP_INIT_ERROR";
1985 static void pkey_test_cleanup(EVP_TEST *t)
1987 PKEY_DATA *kdata = t->data;
1989 OPENSSL_free(kdata->input);
1990 OPENSSL_free(kdata->output);
1991 EVP_PKEY_CTX_free(kdata->ctx);
1994 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
2000 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
2002 p = strchr(tmpval, ':');
2005 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
2008 t->err = "PKEY_CTRL_INVALID";
2010 } else if (p != NULL && rv <= 0) {
2011 if (is_digest_disabled(p) || is_cipher_disabled(p)) {
2012 TEST_info("skipping, '%s' is disabled", p);
2016 t->err = "PKEY_CTRL_ERROR";
2020 OPENSSL_free(tmpval);
2024 static int pkey_test_parse(EVP_TEST *t,
2025 const char *keyword, const char *value)
2027 PKEY_DATA *kdata = t->data;
2028 if (strcmp(keyword, "Input") == 0)
2029 return parse_bin(value, &kdata->input, &kdata->input_len);
2030 if (strcmp(keyword, "Output") == 0)
2031 return parse_bin(value, &kdata->output, &kdata->output_len);
2032 if (strcmp(keyword, "Ctrl") == 0)
2033 return pkey_test_ctrl(t, kdata->ctx, value);
2037 static int pkey_test_run(EVP_TEST *t)
2039 PKEY_DATA *expected = t->data;
2040 unsigned char *got = NULL;
2042 EVP_PKEY_CTX *copy = NULL;
2044 if (expected->keyop(expected->ctx, NULL, &got_len,
2045 expected->input, expected->input_len) <= 0
2046 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
2047 t->err = "KEYOP_LENGTH_ERROR";
2050 if (expected->keyop(expected->ctx, got, &got_len,
2051 expected->input, expected->input_len) <= 0) {
2052 t->err = "KEYOP_ERROR";
2055 if (!memory_err_compare(t, "KEYOP_MISMATCH",
2056 expected->output, expected->output_len,
2064 /* Repeat the test on a copy. */
2065 if (!TEST_ptr(copy = EVP_PKEY_CTX_dup(expected->ctx))) {
2066 t->err = "INTERNAL_ERROR";
2069 if (expected->keyop(copy, NULL, &got_len, expected->input,
2070 expected->input_len) <= 0
2071 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
2072 t->err = "KEYOP_LENGTH_ERROR";
2075 if (expected->keyop(copy, got, &got_len, expected->input,
2076 expected->input_len) <= 0) {
2077 t->err = "KEYOP_ERROR";
2080 if (!memory_err_compare(t, "KEYOP_MISMATCH",
2081 expected->output, expected->output_len,
2087 EVP_PKEY_CTX_free(copy);
2091 static int sign_test_init(EVP_TEST *t, const char *name)
2093 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
2096 static const EVP_TEST_METHOD psign_test_method = {
2104 static int verify_recover_test_init(EVP_TEST *t, const char *name)
2106 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
2107 EVP_PKEY_verify_recover);
2110 static const EVP_TEST_METHOD pverify_recover_test_method = {
2112 verify_recover_test_init,
2118 static int decrypt_test_init(EVP_TEST *t, const char *name)
2120 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
2124 static const EVP_TEST_METHOD pdecrypt_test_method = {
2132 static int verify_test_init(EVP_TEST *t, const char *name)
2134 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
2137 static int verify_test_run(EVP_TEST *t)
2139 PKEY_DATA *kdata = t->data;
2141 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
2142 kdata->input, kdata->input_len) <= 0)
2143 t->err = "VERIFY_ERROR";
2147 static const EVP_TEST_METHOD pverify_test_method = {
2155 static int pderive_test_init(EVP_TEST *t, const char *name)
2157 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
2160 static int pderive_test_parse(EVP_TEST *t,
2161 const char *keyword, const char *value)
2163 PKEY_DATA *kdata = t->data;
2166 if (strcmp(keyword, "PeerKeyValidate") == 0)
2169 if (validate || strcmp(keyword, "PeerKey") == 0) {
2171 if (find_key(&peer, value, public_keys) == 0)
2173 if (EVP_PKEY_derive_set_peer_ex(kdata->ctx, peer, validate) <= 0) {
2174 t->err = "DERIVE_SET_PEER_ERROR";
2180 if (strcmp(keyword, "SharedSecret") == 0)
2181 return parse_bin(value, &kdata->output, &kdata->output_len);
2182 if (strcmp(keyword, "Ctrl") == 0)
2183 return pkey_test_ctrl(t, kdata->ctx, value);
2184 if (strcmp(keyword, "KDFType") == 0) {
2185 OSSL_PARAM params[2];
2187 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_EXCHANGE_PARAM_KDF_TYPE,
2189 params[1] = OSSL_PARAM_construct_end();
2190 if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0)
2194 if (strcmp(keyword, "KDFDigest") == 0) {
2195 OSSL_PARAM params[2];
2197 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_EXCHANGE_PARAM_KDF_DIGEST,
2199 params[1] = OSSL_PARAM_construct_end();
2200 if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0)
2204 if (strcmp(keyword, "CEKAlg") == 0) {
2205 OSSL_PARAM params[2];
2207 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CEK_ALG,
2209 params[1] = OSSL_PARAM_construct_end();
2210 if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0)
2214 if (strcmp(keyword, "KDFOutlen") == 0) {
2215 OSSL_PARAM params[2];
2217 size_t outlen = (size_t)strtoul(value, &endptr, 0);
2219 if (endptr[0] != '\0')
2222 params[0] = OSSL_PARAM_construct_size_t(OSSL_EXCHANGE_PARAM_KDF_OUTLEN,
2224 params[1] = OSSL_PARAM_construct_end();
2225 if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0)
2232 static int pderive_test_run(EVP_TEST *t)
2234 EVP_PKEY_CTX *dctx = NULL;
2235 PKEY_DATA *expected = t->data;
2236 unsigned char *got = NULL;
2239 if (!TEST_ptr(dctx = EVP_PKEY_CTX_dup(expected->ctx))) {
2240 t->err = "DERIVE_ERROR";
2244 if (EVP_PKEY_derive(dctx, NULL, &got_len) <= 0
2245 || !TEST_size_t_ne(got_len, 0)) {
2246 t->err = "DERIVE_ERROR";
2249 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2250 t->err = "DERIVE_ERROR";
2253 if (EVP_PKEY_derive(dctx, got, &got_len) <= 0) {
2254 t->err = "DERIVE_ERROR";
2257 if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH",
2258 expected->output, expected->output_len,
2265 EVP_PKEY_CTX_free(dctx);
2269 static const EVP_TEST_METHOD pderive_test_method = {
2282 typedef enum pbe_type_enum {
2283 PBE_TYPE_INVALID = 0,
2284 PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
2287 typedef struct pbe_data_st {
2289 /* scrypt parameters */
2290 uint64_t N, r, p, maxmem;
2291 /* PKCS#12 parameters */
2295 unsigned char *pass;
2298 unsigned char *salt;
2300 /* Expected output */
2305 #ifndef OPENSSL_NO_SCRYPT
2306 /* Parse unsigned decimal 64 bit integer value */
2307 static int parse_uint64(const char *value, uint64_t *pr)
2309 const char *p = value;
2311 if (!TEST_true(*p)) {
2312 TEST_info("Invalid empty integer value");
2315 for (*pr = 0; *p; ) {
2316 if (*pr > UINT64_MAX / 10) {
2317 TEST_error("Integer overflow in string %s", value);
2321 if (!TEST_true(isdigit((unsigned char)*p))) {
2322 TEST_error("Invalid character in string %s", value);
2331 static int scrypt_test_parse(EVP_TEST *t,
2332 const char *keyword, const char *value)
2334 PBE_DATA *pdata = t->data;
2336 if (strcmp(keyword, "N") == 0)
2337 return parse_uint64(value, &pdata->N);
2338 if (strcmp(keyword, "p") == 0)
2339 return parse_uint64(value, &pdata->p);
2340 if (strcmp(keyword, "r") == 0)
2341 return parse_uint64(value, &pdata->r);
2342 if (strcmp(keyword, "maxmem") == 0)
2343 return parse_uint64(value, &pdata->maxmem);
2348 static int pbkdf2_test_parse(EVP_TEST *t,
2349 const char *keyword, const char *value)
2351 PBE_DATA *pdata = t->data;
2353 if (strcmp(keyword, "iter") == 0) {
2354 pdata->iter = atoi(value);
2355 if (pdata->iter <= 0)
2359 if (strcmp(keyword, "MD") == 0) {
2360 pdata->md = EVP_get_digestbyname(value);
2361 if (pdata->md == NULL)
2368 static int pkcs12_test_parse(EVP_TEST *t,
2369 const char *keyword, const char *value)
2371 PBE_DATA *pdata = t->data;
2373 if (strcmp(keyword, "id") == 0) {
2374 pdata->id = atoi(value);
2379 return pbkdf2_test_parse(t, keyword, value);
2382 static int pbe_test_init(EVP_TEST *t, const char *alg)
2385 PBE_TYPE pbe_type = PBE_TYPE_INVALID;
2387 if (is_kdf_disabled(alg)) {
2388 TEST_info("skipping, '%s' is disabled", alg);
2392 if (strcmp(alg, "scrypt") == 0) {
2393 pbe_type = PBE_TYPE_SCRYPT;
2394 } else if (strcmp(alg, "pbkdf2") == 0) {
2395 pbe_type = PBE_TYPE_PBKDF2;
2396 } else if (strcmp(alg, "pkcs12") == 0) {
2397 pbe_type = PBE_TYPE_PKCS12;
2399 TEST_error("Unknown pbe algorithm %s", alg);
2402 if (!TEST_ptr(pdat = OPENSSL_zalloc(sizeof(*pdat))))
2404 pdat->pbe_type = pbe_type;
2409 static void pbe_test_cleanup(EVP_TEST *t)
2411 PBE_DATA *pdat = t->data;
2413 OPENSSL_free(pdat->pass);
2414 OPENSSL_free(pdat->salt);
2415 OPENSSL_free(pdat->key);
2418 static int pbe_test_parse(EVP_TEST *t,
2419 const char *keyword, const char *value)
2421 PBE_DATA *pdata = t->data;
2423 if (strcmp(keyword, "Password") == 0)
2424 return parse_bin(value, &pdata->pass, &pdata->pass_len);
2425 if (strcmp(keyword, "Salt") == 0)
2426 return parse_bin(value, &pdata->salt, &pdata->salt_len);
2427 if (strcmp(keyword, "Key") == 0)
2428 return parse_bin(value, &pdata->key, &pdata->key_len);
2429 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
2430 return pbkdf2_test_parse(t, keyword, value);
2431 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
2432 return pkcs12_test_parse(t, keyword, value);
2433 #ifndef OPENSSL_NO_SCRYPT
2434 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
2435 return scrypt_test_parse(t, keyword, value);
2440 static int pbe_test_run(EVP_TEST *t)
2442 PBE_DATA *expected = t->data;
2444 EVP_MD *fetched_digest = NULL;
2445 OSSL_LIB_CTX *save_libctx;
2447 save_libctx = OSSL_LIB_CTX_set0_default(libctx);
2449 if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
2450 t->err = "INTERNAL_ERROR";
2453 if (expected->pbe_type == PBE_TYPE_PBKDF2) {
2454 if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
2455 expected->salt, expected->salt_len,
2456 expected->iter, expected->md,
2457 expected->key_len, key) == 0) {
2458 t->err = "PBKDF2_ERROR";
2461 #ifndef OPENSSL_NO_SCRYPT
2462 } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
2463 if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
2464 expected->salt, expected->salt_len,
2465 expected->N, expected->r, expected->p,
2466 expected->maxmem, key, expected->key_len) == 0) {
2467 t->err = "SCRYPT_ERROR";
2471 } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
2472 fetched_digest = EVP_MD_fetch(libctx, EVP_MD_get0_name(expected->md),
2474 if (fetched_digest == NULL) {
2475 t->err = "PKCS12_ERROR";
2478 if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
2479 expected->salt, expected->salt_len,
2480 expected->id, expected->iter, expected->key_len,
2481 key, fetched_digest) == 0) {
2482 t->err = "PKCS12_ERROR";
2486 if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len,
2487 key, expected->key_len))
2492 EVP_MD_free(fetched_digest);
2494 OSSL_LIB_CTX_set0_default(save_libctx);
2498 static const EVP_TEST_METHOD pbe_test_method = {
2512 BASE64_CANONICAL_ENCODING = 0,
2513 BASE64_VALID_ENCODING = 1,
2514 BASE64_INVALID_ENCODING = 2
2515 } base64_encoding_type;
2517 typedef struct encode_data_st {
2518 /* Input to encoding */
2519 unsigned char *input;
2521 /* Expected output */
2522 unsigned char *output;
2524 base64_encoding_type encoding;
2527 static int encode_test_init(EVP_TEST *t, const char *encoding)
2531 if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
2533 if (strcmp(encoding, "canonical") == 0) {
2534 edata->encoding = BASE64_CANONICAL_ENCODING;
2535 } else if (strcmp(encoding, "valid") == 0) {
2536 edata->encoding = BASE64_VALID_ENCODING;
2537 } else if (strcmp(encoding, "invalid") == 0) {
2538 edata->encoding = BASE64_INVALID_ENCODING;
2539 if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
2542 TEST_error("Bad encoding: %s."
2543 " Should be one of {canonical, valid, invalid}",
2550 OPENSSL_free(edata);
2554 static void encode_test_cleanup(EVP_TEST *t)
2556 ENCODE_DATA *edata = t->data;
2558 OPENSSL_free(edata->input);
2559 OPENSSL_free(edata->output);
2560 memset(edata, 0, sizeof(*edata));
2563 static int encode_test_parse(EVP_TEST *t,
2564 const char *keyword, const char *value)
2566 ENCODE_DATA *edata = t->data;
2568 if (strcmp(keyword, "Input") == 0)
2569 return parse_bin(value, &edata->input, &edata->input_len);
2570 if (strcmp(keyword, "Output") == 0)
2571 return parse_bin(value, &edata->output, &edata->output_len);
2575 static int encode_test_run(EVP_TEST *t)
2577 ENCODE_DATA *expected = t->data;
2578 unsigned char *encode_out = NULL, *decode_out = NULL;
2579 int output_len, chunk_len;
2580 EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL;
2581 size_t input_len, donelen;
2583 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
2584 t->err = "INTERNAL_ERROR";
2588 if (expected->encoding == BASE64_CANONICAL_ENCODING) {
2590 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
2591 || !TEST_ptr(encode_out =
2592 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
2595 EVP_EncodeInit(encode_ctx);
2597 input_len = expected->input_len;
2601 size_t current_len = (size_t) data_chunk_size;
2603 if (data_chunk_size == 0 || (size_t) data_chunk_size > input_len)
2604 current_len = input_len;
2605 if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
2606 expected->input + donelen,
2609 donelen += current_len;
2610 input_len -= current_len;
2611 output_len += chunk_len;
2612 } while (input_len > 0);
2614 EVP_EncodeFinal(encode_ctx, encode_out + output_len, &chunk_len);
2615 output_len += chunk_len;
2617 if (!memory_err_compare(t, "BAD_ENCODING",
2618 expected->output, expected->output_len,
2619 encode_out, output_len))
2623 if (!TEST_ptr(decode_out =
2624 OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
2628 EVP_DecodeInit(decode_ctx);
2630 input_len = expected->output_len;
2633 size_t current_len = (size_t) data_chunk_size;
2635 if (data_chunk_size == 0 || (size_t) data_chunk_size > input_len)
2636 current_len = input_len;
2637 if (EVP_DecodeUpdate(decode_ctx, decode_out + output_len, &chunk_len,
2638 expected->output + donelen, current_len) < 0) {
2639 t->err = "DECODE_ERROR";
2642 donelen += current_len;
2643 input_len -= current_len;
2644 output_len += chunk_len;
2645 } while (input_len > 0);
2647 if (EVP_DecodeFinal(decode_ctx, decode_out + output_len, &chunk_len) != 1) {
2648 t->err = "DECODE_ERROR";
2651 output_len += chunk_len;
2653 if (expected->encoding != BASE64_INVALID_ENCODING
2654 && !memory_err_compare(t, "BAD_DECODING",
2655 expected->input, expected->input_len,
2656 decode_out, output_len)) {
2657 t->err = "BAD_DECODING";
2663 OPENSSL_free(encode_out);
2664 OPENSSL_free(decode_out);
2665 EVP_ENCODE_CTX_free(decode_ctx);
2666 EVP_ENCODE_CTX_free(encode_ctx);
2670 static const EVP_TEST_METHOD encode_test_method = {
2673 encode_test_cleanup,
2682 #define MAX_RAND_REPEATS 15
2684 typedef struct rand_data_pass_st {
2685 unsigned char *entropy;
2686 unsigned char *reseed_entropy;
2687 unsigned char *nonce;
2688 unsigned char *pers;
2689 unsigned char *reseed_addin;
2690 unsigned char *addinA;
2691 unsigned char *addinB;
2692 unsigned char *pr_entropyA;
2693 unsigned char *pr_entropyB;
2694 unsigned char *output;
2695 size_t entropy_len, nonce_len, pers_len, addinA_len, addinB_len,
2696 pr_entropyA_len, pr_entropyB_len, output_len, reseed_entropy_len,
2700 typedef struct rand_data_st {
2701 /* Context for this operation */
2703 EVP_RAND_CTX *parent;
2705 int prediction_resistance;
2707 unsigned int generate_bits;
2711 /* Expected output */
2712 RAND_DATA_PASS data[MAX_RAND_REPEATS];
2715 static int rand_test_init(EVP_TEST *t, const char *name)
2719 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
2720 unsigned int strength = 256;
2722 if (!TEST_ptr(rdata = OPENSSL_zalloc(sizeof(*rdata))))
2725 /* TEST-RAND is available in the FIPS provider but not with "fips=yes" */
2726 rand = EVP_RAND_fetch(libctx, "TEST-RAND", "-fips");
2729 rdata->parent = EVP_RAND_CTX_new(rand, NULL);
2730 EVP_RAND_free(rand);
2731 if (rdata->parent == NULL)
2734 *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength);
2735 if (!EVP_RAND_CTX_set_params(rdata->parent, params))
2738 rand = EVP_RAND_fetch(libctx, name, propquery);
2741 rdata->ctx = EVP_RAND_CTX_new(rand, rdata->parent);
2742 EVP_RAND_free(rand);
2743 if (rdata->ctx == NULL)
2750 EVP_RAND_CTX_free(rdata->parent);
2751 OPENSSL_free(rdata);
2755 static void rand_test_cleanup(EVP_TEST *t)
2757 RAND_DATA *rdata = t->data;
2760 OPENSSL_free(rdata->cipher);
2761 OPENSSL_free(rdata->digest);
2763 for (i = 0; i <= rdata->n; i++) {
2764 OPENSSL_free(rdata->data[i].entropy);
2765 OPENSSL_free(rdata->data[i].reseed_entropy);
2766 OPENSSL_free(rdata->data[i].nonce);
2767 OPENSSL_free(rdata->data[i].pers);
2768 OPENSSL_free(rdata->data[i].reseed_addin);
2769 OPENSSL_free(rdata->data[i].addinA);
2770 OPENSSL_free(rdata->data[i].addinB);
2771 OPENSSL_free(rdata->data[i].pr_entropyA);
2772 OPENSSL_free(rdata->data[i].pr_entropyB);
2773 OPENSSL_free(rdata->data[i].output);
2775 EVP_RAND_CTX_free(rdata->ctx);
2776 EVP_RAND_CTX_free(rdata->parent);
2779 static int rand_test_parse(EVP_TEST *t,
2780 const char *keyword, const char *value)
2782 RAND_DATA *rdata = t->data;
2783 RAND_DATA_PASS *item;
2787 if ((p = strchr(keyword, '.')) != NULL) {
2789 if (n >= MAX_RAND_REPEATS)
2793 item = rdata->data + n;
2794 if (HAS_PREFIX(keyword, "Entropy."))
2795 return parse_bin(value, &item->entropy, &item->entropy_len);
2796 if (HAS_PREFIX(keyword, "ReseedEntropy."))
2797 return parse_bin(value, &item->reseed_entropy,
2798 &item->reseed_entropy_len);
2799 if (HAS_PREFIX(keyword, "Nonce."))
2800 return parse_bin(value, &item->nonce, &item->nonce_len);
2801 if (HAS_PREFIX(keyword, "PersonalisationString."))
2802 return parse_bin(value, &item->pers, &item->pers_len);
2803 if (HAS_PREFIX(keyword, "ReseedAdditionalInput."))
2804 return parse_bin(value, &item->reseed_addin,
2805 &item->reseed_addin_len);
2806 if (HAS_PREFIX(keyword, "AdditionalInputA."))
2807 return parse_bin(value, &item->addinA, &item->addinA_len);
2808 if (HAS_PREFIX(keyword, "AdditionalInputB."))
2809 return parse_bin(value, &item->addinB, &item->addinB_len);
2810 if (HAS_PREFIX(keyword, "EntropyPredictionResistanceA."))
2811 return parse_bin(value, &item->pr_entropyA, &item->pr_entropyA_len);
2812 if (HAS_PREFIX(keyword, "EntropyPredictionResistanceB."))
2813 return parse_bin(value, &item->pr_entropyB, &item->pr_entropyB_len);
2814 if (HAS_PREFIX(keyword, "Output."))
2815 return parse_bin(value, &item->output, &item->output_len);
2817 if (strcmp(keyword, "Cipher") == 0)
2818 return TEST_ptr(rdata->cipher = OPENSSL_strdup(value));
2819 if (strcmp(keyword, "Digest") == 0)
2820 return TEST_ptr(rdata->digest = OPENSSL_strdup(value));
2821 if (strcmp(keyword, "DerivationFunction") == 0) {
2822 rdata->use_df = atoi(value) != 0;
2825 if (strcmp(keyword, "GenerateBits") == 0) {
2826 if ((n = atoi(value)) <= 0 || n % 8 != 0)
2828 rdata->generate_bits = (unsigned int)n;
2831 if (strcmp(keyword, "PredictionResistance") == 0) {
2832 rdata->prediction_resistance = atoi(value) != 0;
2839 static int rand_test_run(EVP_TEST *t)
2841 RAND_DATA *expected = t->data;
2842 RAND_DATA_PASS *item;
2844 size_t got_len = expected->generate_bits / 8;
2845 OSSL_PARAM params[5], *p = params;
2846 int i = -1, ret = 0;
2847 unsigned int strength;
2850 if (!TEST_ptr(got = OPENSSL_malloc(got_len)))
2853 *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &expected->use_df);
2854 if (expected->cipher != NULL)
2855 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
2856 expected->cipher, 0);
2857 if (expected->digest != NULL)
2858 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
2859 expected->digest, 0);
2860 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC, "HMAC", 0);
2861 *p = OSSL_PARAM_construct_end();
2862 if (!TEST_true(EVP_RAND_CTX_set_params(expected->ctx, params)))
2865 strength = EVP_RAND_get_strength(expected->ctx);
2866 for (i = 0; i <= expected->n; i++) {
2867 item = expected->data + i;
2870 z = item->entropy != NULL ? item->entropy : (unsigned char *)"";
2871 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
2872 z, item->entropy_len);
2873 z = item->nonce != NULL ? item->nonce : (unsigned char *)"";
2874 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
2875 z, item->nonce_len);
2876 *p = OSSL_PARAM_construct_end();
2877 if (!TEST_true(EVP_RAND_instantiate(expected->parent, strength,
2878 0, NULL, 0, params)))
2881 z = item->pers != NULL ? item->pers : (unsigned char *)"";
2882 if (!TEST_true(EVP_RAND_instantiate
2883 (expected->ctx, strength,
2884 expected->prediction_resistance, z,
2885 item->pers_len, NULL)))
2888 if (item->reseed_entropy != NULL) {
2889 params[0] = OSSL_PARAM_construct_octet_string
2890 (OSSL_RAND_PARAM_TEST_ENTROPY, item->reseed_entropy,
2891 item->reseed_entropy_len);
2892 params[1] = OSSL_PARAM_construct_end();
2893 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2896 if (!TEST_true(EVP_RAND_reseed
2897 (expected->ctx, expected->prediction_resistance,
2898 NULL, 0, item->reseed_addin,
2899 item->reseed_addin_len)))
2902 if (item->pr_entropyA != NULL) {
2903 params[0] = OSSL_PARAM_construct_octet_string
2904 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyA,
2905 item->pr_entropyA_len);
2906 params[1] = OSSL_PARAM_construct_end();
2907 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2910 if (!TEST_true(EVP_RAND_generate
2911 (expected->ctx, got, got_len,
2912 strength, expected->prediction_resistance,
2913 item->addinA, item->addinA_len)))
2916 if (item->pr_entropyB != NULL) {
2917 params[0] = OSSL_PARAM_construct_octet_string
2918 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyB,
2919 item->pr_entropyB_len);
2920 params[1] = OSSL_PARAM_construct_end();
2921 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2924 if (!TEST_true(EVP_RAND_generate
2925 (expected->ctx, got, got_len,
2926 strength, expected->prediction_resistance,
2927 item->addinB, item->addinB_len)))
2929 if (!TEST_mem_eq(got, got_len, item->output, item->output_len))
2931 if (!TEST_true(EVP_RAND_uninstantiate(expected->ctx))
2932 || !TEST_true(EVP_RAND_uninstantiate(expected->parent))
2933 || !TEST_true(EVP_RAND_verify_zeroization(expected->ctx))
2934 || !TEST_int_eq(EVP_RAND_get_state(expected->ctx),
2935 EVP_RAND_STATE_UNINITIALISED))
2942 if (ret == 0 && i >= 0)
2943 TEST_info("Error in test case %d of %d\n", i, expected->n + 1);
2948 static const EVP_TEST_METHOD rand_test_method = {
2960 typedef struct kdf_data_st {
2961 /* Context for this operation */
2963 /* Expected output */
2964 unsigned char *output;
2966 OSSL_PARAM params[20];
2971 * Perform public key operation setup: lookup key, allocated ctx and call
2972 * the appropriate initialisation function
2974 static int kdf_test_init(EVP_TEST *t, const char *name)
2979 if (is_kdf_disabled(name)) {
2980 TEST_info("skipping, '%s' is disabled", name);
2985 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2987 kdata->p = kdata->params;
2988 *kdata->p = OSSL_PARAM_construct_end();
2990 kdf = EVP_KDF_fetch(libctx, name, propquery);
2992 OPENSSL_free(kdata);
2995 kdata->ctx = EVP_KDF_CTX_new(kdf);
2997 if (kdata->ctx == NULL) {
2998 OPENSSL_free(kdata);
3005 static void kdf_test_cleanup(EVP_TEST *t)
3007 KDF_DATA *kdata = t->data;
3010 for (p = kdata->params; p->key != NULL; p++)
3011 OPENSSL_free(p->data);
3012 OPENSSL_free(kdata->output);
3013 EVP_KDF_CTX_free(kdata->ctx);
3016 static int kdf_test_ctrl(EVP_TEST *t, EVP_KDF_CTX *kctx,
3019 KDF_DATA *kdata = t->data;
3022 const OSSL_PARAM *defs = EVP_KDF_settable_ctx_params(EVP_KDF_CTX_kdf(kctx));
3024 if (!TEST_ptr(name = OPENSSL_strdup(value)))
3026 p = strchr(name, ':');
3032 if (strcmp(name, "r") == 0
3033 && OSSL_PARAM_locate_const(defs, name) == NULL) {
3034 TEST_info("skipping, setting 'r' is unsupported");
3039 if (strcmp(name, "lanes") == 0
3040 && OSSL_PARAM_locate_const(defs, name) == NULL) {
3041 TEST_info("skipping, setting 'lanes' is unsupported");
3046 if (strcmp(name, "iter") == 0
3047 && OSSL_PARAM_locate_const(defs, name) == NULL) {
3048 TEST_info("skipping, setting 'iter' is unsupported");
3053 if (strcmp(name, "memcost") == 0
3054 && OSSL_PARAM_locate_const(defs, name) == NULL) {
3055 TEST_info("skipping, setting 'memcost' is unsupported");
3060 if (strcmp(name, "secret") == 0
3061 && OSSL_PARAM_locate_const(defs, name) == NULL) {
3062 TEST_info("skipping, setting 'secret' is unsupported");
3067 if (strcmp(name, "pass") == 0
3068 && OSSL_PARAM_locate_const(defs, name) == NULL) {
3069 TEST_info("skipping, setting 'pass' is unsupported");
3074 if (strcmp(name, "ad") == 0
3075 && OSSL_PARAM_locate_const(defs, name) == NULL) {
3076 TEST_info("skipping, setting 'ad' is unsupported");
3081 rv = OSSL_PARAM_allocate_from_text(kdata->p, defs, name, p,
3083 *++kdata->p = OSSL_PARAM_construct_end();
3085 t->err = "KDF_PARAM_ERROR";
3089 if (strcmp(name, "digest") == 0) {
3090 if (is_digest_disabled(p)) {
3091 TEST_info("skipping, '%s' is disabled", p);
3097 if ((strcmp(name, "cipher") == 0
3098 || strcmp(name, "cekalg") == 0)
3099 && is_cipher_disabled(p)) {
3100 TEST_info("skipping, '%s' is disabled", p);
3104 if ((strcmp(name, "mac") == 0)
3105 && is_mac_disabled(p)) {
3106 TEST_info("skipping, '%s' is disabled", p);
3114 static int kdf_test_parse(EVP_TEST *t,
3115 const char *keyword, const char *value)
3117 KDF_DATA *kdata = t->data;
3119 if (strcmp(keyword, "Output") == 0)
3120 return parse_bin(value, &kdata->output, &kdata->output_len);
3121 if (HAS_PREFIX(keyword, "Ctrl"))
3122 return kdf_test_ctrl(t, kdata->ctx, value);
3126 static int kdf_test_run(EVP_TEST *t)
3128 KDF_DATA *expected = t->data;
3129 unsigned char *got = NULL;
3130 size_t got_len = expected->output_len;
3133 if (!EVP_KDF_CTX_set_params(expected->ctx, expected->params)) {
3134 t->err = "KDF_CTRL_ERROR";
3137 if (!TEST_ptr(got = OPENSSL_malloc(got_len == 0 ? 1 : got_len))) {
3138 t->err = "INTERNAL_ERROR";
3141 /* FIPS(3.0.0): can't dup KDF contexts #17572 */
3142 if (fips_provider_version_gt(libctx, 3, 0, 0)
3143 && (ctx = EVP_KDF_CTX_dup(expected->ctx)) != NULL) {
3144 EVP_KDF_CTX_free(expected->ctx);
3145 expected->ctx = ctx;
3147 if (EVP_KDF_derive(expected->ctx, got, got_len, NULL) <= 0) {
3148 t->err = "KDF_DERIVE_ERROR";
3151 if (!memory_err_compare(t, "KDF_MISMATCH",
3152 expected->output, expected->output_len,
3163 static const EVP_TEST_METHOD kdf_test_method = {
3175 typedef struct pkey_kdf_data_st {
3176 /* Context for this operation */
3178 /* Expected output */
3179 unsigned char *output;
3184 * Perform public key operation setup: lookup key, allocated ctx and call
3185 * the appropriate initialisation function
3187 static int pkey_kdf_test_init(EVP_TEST *t, const char *name)
3189 PKEY_KDF_DATA *kdata = NULL;
3191 if (is_kdf_disabled(name)) {
3192 TEST_info("skipping, '%s' is disabled", name);
3197 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
3200 kdata->ctx = EVP_PKEY_CTX_new_from_name(libctx, name, propquery);
3201 if (kdata->ctx == NULL
3202 || EVP_PKEY_derive_init(kdata->ctx) <= 0)
3208 EVP_PKEY_CTX_free(kdata->ctx);
3209 OPENSSL_free(kdata);
3213 static void pkey_kdf_test_cleanup(EVP_TEST *t)
3215 PKEY_KDF_DATA *kdata = t->data;
3217 OPENSSL_free(kdata->output);
3218 EVP_PKEY_CTX_free(kdata->ctx);
3221 static int pkey_kdf_test_parse(EVP_TEST *t,
3222 const char *keyword, const char *value)
3224 PKEY_KDF_DATA *kdata = t->data;
3226 if (strcmp(keyword, "Output") == 0)
3227 return parse_bin(value, &kdata->output, &kdata->output_len);
3228 if (HAS_PREFIX(keyword, "Ctrl"))
3229 return pkey_test_ctrl(t, kdata->ctx, value);
3233 static int pkey_kdf_test_run(EVP_TEST *t)
3235 PKEY_KDF_DATA *expected = t->data;
3236 unsigned char *got = NULL;
3239 if (fips_provider_version_eq(libctx, 3, 0, 0)) {
3240 /* FIPS(3.0.0): can't deal with oversized output buffers #18533 */
3241 got_len = expected->output_len;
3243 /* Find out the KDF output size */
3244 if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) {
3245 t->err = "INTERNAL_ERROR";
3250 * We may get an absurd output size, which signals that anything goes.
3251 * If not, we specify a too big buffer for the output, to test that
3252 * EVP_PKEY_derive() can cope with it.
3254 if (got_len == SIZE_MAX || got_len == 0)
3255 got_len = expected->output_len;
3257 got_len = expected->output_len * 2;
3260 if (!TEST_ptr(got = OPENSSL_malloc(got_len == 0 ? 1 : got_len))) {
3261 t->err = "INTERNAL_ERROR";
3264 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
3265 t->err = "KDF_DERIVE_ERROR";
3268 if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
3269 t->err = "KDF_MISMATCH";
3279 static const EVP_TEST_METHOD pkey_kdf_test_method = {
3282 pkey_kdf_test_cleanup,
3283 pkey_kdf_test_parse,
3291 typedef struct keypair_test_data_st {
3294 } KEYPAIR_TEST_DATA;
3296 static int keypair_test_init(EVP_TEST *t, const char *pair)
3298 KEYPAIR_TEST_DATA *data;
3300 EVP_PKEY *pk = NULL, *pubk = NULL;
3301 char *pub, *priv = NULL;
3303 /* Split private and public names. */
3304 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
3305 || !TEST_ptr(pub = strchr(priv, ':'))) {
3306 t->err = "PARSING_ERROR";
3311 if (!TEST_true(find_key(&pk, priv, private_keys))) {
3312 TEST_info("Can't find private key: %s", priv);
3313 t->err = "MISSING_PRIVATE_KEY";
3316 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
3317 TEST_info("Can't find public key: %s", pub);
3318 t->err = "MISSING_PUBLIC_KEY";
3322 if (pk == NULL && pubk == NULL) {
3323 /* Both keys are listed but unsupported: skip this test */
3329 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
3342 static void keypair_test_cleanup(EVP_TEST *t)
3344 OPENSSL_free(t->data);
3349 * For tests that do not accept any custom keywords.
3351 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
3356 static int keypair_test_run(EVP_TEST *t)
3359 const KEYPAIR_TEST_DATA *pair = t->data;
3361 if (pair->privk == NULL || pair->pubk == NULL) {
3363 * this can only happen if only one of the keys is not set
3364 * which means that one of them was unsupported while the
3365 * other isn't: hence a key type mismatch.
3367 t->err = "KEYPAIR_TYPE_MISMATCH";
3372 if ((rv = EVP_PKEY_eq(pair->privk, pair->pubk)) != 1) {
3374 t->err = "KEYPAIR_MISMATCH";
3375 } else if (-1 == rv) {
3376 t->err = "KEYPAIR_TYPE_MISMATCH";
3377 } else if (-2 == rv) {
3378 t->err = "UNSUPPORTED_KEY_COMPARISON";
3380 TEST_error("Unexpected error in key comparison");
3395 static const EVP_TEST_METHOD keypair_test_method = {
3398 keypair_test_cleanup,
3407 typedef struct keygen_test_data_st {
3408 EVP_PKEY_CTX *genctx; /* Keygen context to use */
3409 char *keyname; /* Key name to store key or NULL */
3412 static int keygen_test_init(EVP_TEST *t, const char *alg)
3414 KEYGEN_TEST_DATA *data;
3415 EVP_PKEY_CTX *genctx;
3416 int nid = OBJ_sn2nid(alg);
3418 if (nid == NID_undef) {
3419 nid = OBJ_ln2nid(alg);
3420 if (nid == NID_undef)
3424 if (is_pkey_disabled(alg)) {
3428 if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_from_name(libctx, alg, propquery)))
3431 if (EVP_PKEY_keygen_init(genctx) <= 0) {
3432 t->err = "KEYGEN_INIT_ERROR";
3436 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
3438 data->genctx = genctx;
3439 data->keyname = NULL;
3445 EVP_PKEY_CTX_free(genctx);
3449 static void keygen_test_cleanup(EVP_TEST *t)
3451 KEYGEN_TEST_DATA *keygen = t->data;
3453 EVP_PKEY_CTX_free(keygen->genctx);
3454 OPENSSL_free(keygen->keyname);
3455 OPENSSL_free(t->data);
3459 static int keygen_test_parse(EVP_TEST *t,
3460 const char *keyword, const char *value)
3462 KEYGEN_TEST_DATA *keygen = t->data;
3464 if (strcmp(keyword, "KeyName") == 0)
3465 return TEST_ptr(keygen->keyname = OPENSSL_strdup(value));
3466 if (strcmp(keyword, "Ctrl") == 0)
3467 return pkey_test_ctrl(t, keygen->genctx, value);
3471 static int keygen_test_run(EVP_TEST *t)
3473 KEYGEN_TEST_DATA *keygen = t->data;
3474 EVP_PKEY *pkey = NULL;
3477 if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) {
3478 t->err = "KEYGEN_GENERATE_ERROR";
3482 if (!evp_pkey_is_provided(pkey)) {
3483 TEST_info("Warning: legacy key generated %s", keygen->keyname);
3486 if (keygen->keyname != NULL) {
3490 if (find_key(NULL, keygen->keyname, private_keys)) {
3491 TEST_info("Duplicate key %s", keygen->keyname);
3495 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
3497 key->name = keygen->keyname;
3498 keygen->keyname = NULL;
3500 key->next = private_keys;
3504 EVP_PKEY_free(pkey);
3513 static const EVP_TEST_METHOD keygen_test_method = {
3516 keygen_test_cleanup,
3522 ** DIGEST SIGN+VERIFY TESTS
3526 int is_verify; /* Set to 1 if verifying */
3527 int is_oneshot; /* Set to 1 for one shot operation */
3528 const EVP_MD *md; /* Digest to use */
3529 EVP_MD_CTX *ctx; /* Digest context */
3531 STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
3532 unsigned char *osin; /* Input data if one shot */
3533 size_t osin_len; /* Input length data if one shot */
3534 unsigned char *output; /* Expected output */
3535 size_t output_len; /* Expected output length */
3536 const char *nonce_type;
3539 static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
3542 const EVP_MD *md = NULL;
3543 DIGESTSIGN_DATA *mdat;
3545 if (strcmp(alg, "NULL") != 0) {
3546 if (is_digest_disabled(alg)) {
3550 md = EVP_get_digestbyname(alg);
3554 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
3557 if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
3561 mdat->is_verify = is_verify;
3562 mdat->is_oneshot = is_oneshot;
3567 static int digestsign_test_init(EVP_TEST *t, const char *alg)
3569 return digestsigver_test_init(t, alg, 0, 0);
3572 static void digestsigver_test_cleanup(EVP_TEST *t)
3574 DIGESTSIGN_DATA *mdata = t->data;
3576 EVP_MD_CTX_free(mdata->ctx);
3577 sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
3578 OPENSSL_free(mdata->osin);
3579 OPENSSL_free(mdata->output);
3580 OPENSSL_free(mdata);
3584 static int digestsigver_test_parse(EVP_TEST *t,
3585 const char *keyword, const char *value)
3587 DIGESTSIGN_DATA *mdata = t->data;
3589 if (strcmp(keyword, "Key") == 0) {
3590 EVP_PKEY *pkey = NULL;
3592 const char *name = mdata->md == NULL ? NULL : EVP_MD_get0_name(mdata->md);
3594 if (mdata->is_verify)
3595 rv = find_key(&pkey, value, public_keys);
3597 rv = find_key(&pkey, value, private_keys);
3598 if (rv == 0 || pkey == NULL) {
3602 if (mdata->is_verify) {
3603 if (!EVP_DigestVerifyInit_ex(mdata->ctx, &mdata->pctx, name, libctx,
3605 t->err = "DIGESTVERIFYINIT_ERROR";
3608 if (!EVP_DigestSignInit_ex(mdata->ctx, &mdata->pctx, name, libctx, NULL,
3610 t->err = "DIGESTSIGNINIT_ERROR";
3614 if (strcmp(keyword, "Input") == 0) {
3615 if (mdata->is_oneshot)
3616 return parse_bin(value, &mdata->osin, &mdata->osin_len);
3617 return evp_test_buffer_append(value, data_chunk_size, &mdata->input);
3619 if (strcmp(keyword, "Output") == 0)
3620 return parse_bin(value, &mdata->output, &mdata->output_len);
3622 if (!mdata->is_oneshot && data_chunk_size == 0) {
3623 if (strcmp(keyword, "Count") == 0)
3624 return evp_test_buffer_set_count(value, mdata->input);
3625 if (strcmp(keyword, "Ncopy") == 0)
3626 return evp_test_buffer_ncopy(value, mdata->input);
3628 if (strcmp(keyword, "Ctrl") == 0) {
3629 if (mdata->pctx == NULL)
3631 return pkey_test_ctrl(t, mdata->pctx, value);
3633 if (strcmp(keyword, "NonceType") == 0) {
3634 if (strcmp(value, "deterministic") == 0) {
3635 OSSL_PARAM params[2];
3636 unsigned int nonce_type = 1;
3639 OSSL_PARAM_construct_uint(OSSL_SIGNATURE_PARAM_NONCE_TYPE,
3641 params[1] = OSSL_PARAM_construct_end();
3642 if (!EVP_PKEY_CTX_set_params(mdata->pctx, params))
3643 t->err = "EVP_PKEY_CTX_set_params_ERROR";
3644 else if (!EVP_PKEY_CTX_get_params(mdata->pctx, params))
3645 t->err = "EVP_PKEY_CTX_get_params_ERROR";
3646 else if (!OSSL_PARAM_modified(¶ms[0]))
3647 t->err = "nonce_type_not_modified_ERROR";
3648 else if (nonce_type != 1)
3649 t->err = "nonce_type_value_ERROR";
3656 static int digestsign_update_fn(void *ctx, const unsigned char *buf,
3659 return EVP_DigestSignUpdate(ctx, buf, buflen);
3662 static int digestsign_test_run(EVP_TEST *t)
3664 DIGESTSIGN_DATA *expected = t->data;
3665 unsigned char *got = NULL;
3668 if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
3670 t->err = "DIGESTUPDATE_ERROR";
3674 if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
3675 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
3678 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
3679 t->err = "MALLOC_FAILURE";
3683 if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
3684 t->err = "DIGESTSIGNFINAL_ERROR";
3687 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
3688 expected->output, expected->output_len,
3698 static const EVP_TEST_METHOD digestsign_test_method = {
3700 digestsign_test_init,
3701 digestsigver_test_cleanup,
3702 digestsigver_test_parse,
3706 static int digestverify_test_init(EVP_TEST *t, const char *alg)
3708 return digestsigver_test_init(t, alg, 1, 0);
3711 static int digestverify_update_fn(void *ctx, const unsigned char *buf,
3714 return EVP_DigestVerifyUpdate(ctx, buf, buflen);
3717 static int digestverify_test_run(EVP_TEST *t)
3719 DIGESTSIGN_DATA *mdata = t->data;
3721 if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
3722 t->err = "DIGESTUPDATE_ERROR";
3726 if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
3727 mdata->output_len) <= 0)
3728 t->err = "VERIFY_ERROR";
3732 static const EVP_TEST_METHOD digestverify_test_method = {
3734 digestverify_test_init,
3735 digestsigver_test_cleanup,
3736 digestsigver_test_parse,
3737 digestverify_test_run
3740 static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
3742 return digestsigver_test_init(t, alg, 0, 1);
3745 static int oneshot_digestsign_test_run(EVP_TEST *t)
3747 DIGESTSIGN_DATA *expected = t->data;
3748 unsigned char *got = NULL;
3751 if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
3752 expected->osin, expected->osin_len)) {
3753 t->err = "DIGESTSIGN_LENGTH_ERROR";
3756 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
3757 t->err = "MALLOC_FAILURE";
3761 if (!EVP_DigestSign(expected->ctx, got, &got_len,
3762 expected->osin, expected->osin_len)) {
3763 t->err = "DIGESTSIGN_ERROR";
3766 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
3767 expected->output, expected->output_len,
3777 static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
3778 "OneShotDigestSign",
3779 oneshot_digestsign_test_init,
3780 digestsigver_test_cleanup,
3781 digestsigver_test_parse,
3782 oneshot_digestsign_test_run
3785 static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
3787 return digestsigver_test_init(t, alg, 1, 1);
3790 static int oneshot_digestverify_test_run(EVP_TEST *t)
3792 DIGESTSIGN_DATA *mdata = t->data;
3794 if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
3795 mdata->osin, mdata->osin_len) <= 0)
3796 t->err = "VERIFY_ERROR";
3800 static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
3801 "OneShotDigestVerify",
3802 oneshot_digestverify_test_init,
3803 digestsigver_test_cleanup,
3804 digestsigver_test_parse,
3805 oneshot_digestverify_test_run
3810 ** PARSING AND DISPATCH
3813 static const EVP_TEST_METHOD *evp_test_list[] = {
3815 &cipher_test_method,
3816 &digest_test_method,
3817 &digestsign_test_method,
3818 &digestverify_test_method,
3819 &encode_test_method,
3821 &pkey_kdf_test_method,
3822 &keypair_test_method,
3823 &keygen_test_method,
3825 &oneshot_digestsign_test_method,
3826 &oneshot_digestverify_test_method,
3828 &pdecrypt_test_method,
3829 &pderive_test_method,
3831 &pverify_recover_test_method,
3832 &pverify_test_method,
3836 static const EVP_TEST_METHOD *find_test(const char *name)
3838 const EVP_TEST_METHOD **tt;
3840 for (tt = evp_test_list; *tt; tt++) {
3841 if (strcmp(name, (*tt)->name) == 0)
3847 static void clear_test(EVP_TEST *t)
3849 test_clearstanza(&t->s);
3851 if (t->data != NULL) {
3852 if (t->meth != NULL)
3853 t->meth->cleanup(t);
3854 OPENSSL_free(t->data);
3857 OPENSSL_free(t->expected_err);
3858 t->expected_err = NULL;
3859 OPENSSL_free(t->reason);
3867 #if !defined(OPENSSL_NO_DEFAULT_THREAD_POOL)
3868 OSSL_set_max_threads(libctx, 0);
3872 /* Check for errors in the test structure; return 1 if okay, else 0. */
3873 static int check_test_error(EVP_TEST *t)
3878 if (t->err == NULL && t->expected_err == NULL)
3880 if (t->err != NULL && t->expected_err == NULL) {
3881 if (t->aux_err != NULL) {
3882 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
3883 t->s.test_file, t->s.start, t->aux_err, t->err);
3885 TEST_info("%s:%d: Source of above error; unexpected error %s",
3886 t->s.test_file, t->s.start, t->err);
3890 if (t->err == NULL && t->expected_err != NULL) {
3891 TEST_info("%s:%d: Succeeded but was expecting %s",
3892 t->s.test_file, t->s.start, t->expected_err);
3896 if (strcmp(t->err, t->expected_err) != 0) {
3897 TEST_info("%s:%d: Expected %s got %s",
3898 t->s.test_file, t->s.start, t->expected_err, t->err);
3902 if (t->reason == NULL)
3905 if (t->reason == NULL) {
3906 TEST_info("%s:%d: Test is missing function or reason code",
3907 t->s.test_file, t->s.start);
3911 err = ERR_peek_error();
3913 TEST_info("%s:%d: Expected error \"%s\" not set",
3914 t->s.test_file, t->s.start, t->reason);
3918 reason = ERR_reason_error_string(err);
3919 if (reason == NULL) {
3920 TEST_info("%s:%d: Expected error \"%s\", no strings available."
3922 t->s.test_file, t->s.start, t->reason);
3926 if (strcmp(reason, t->reason) == 0)
3929 TEST_info("%s:%d: Expected error \"%s\", got \"%s\"",
3930 t->s.test_file, t->s.start, t->reason, reason);
3935 /* Run a parsed test. Log a message and return 0 on error. */
3936 static int run_test(EVP_TEST *t)
3938 if (t->meth == NULL)
3945 if (t->err == NULL && t->meth->run_test(t) != 1) {
3946 TEST_info("%s:%d %s error",
3947 t->s.test_file, t->s.start, t->meth->name);
3950 if (!check_test_error(t)) {
3951 TEST_openssl_errors();
3960 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
3962 for (; lst != NULL; lst = lst->next) {
3963 if (strcmp(lst->name, name) == 0) {
3972 static void free_key_list(KEY_LIST *lst)
3974 while (lst != NULL) {
3975 KEY_LIST *next = lst->next;
3977 EVP_PKEY_free(lst->key);
3978 OPENSSL_free(lst->name);
3985 * Is the key type an unsupported algorithm?
3987 static int key_unsupported(void)
3989 long err = ERR_peek_last_error();
3990 int lib = ERR_GET_LIB(err);
3991 long reason = ERR_GET_REASON(err);
3993 if ((lib == ERR_LIB_EVP && reason == EVP_R_UNSUPPORTED_ALGORITHM)
3994 || (lib == ERR_LIB_EVP && reason == EVP_R_DECODE_ERROR)
3995 || reason == ERR_R_UNSUPPORTED) {
3999 #ifndef OPENSSL_NO_EC
4001 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
4002 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
4005 if (lib == ERR_LIB_EC
4006 && (reason == EC_R_UNKNOWN_GROUP
4007 || reason == EC_R_INVALID_CURVE)) {
4011 #endif /* OPENSSL_NO_EC */
4015 /* NULL out the value from |pp| but return it. This "steals" a pointer. */
4016 static char *take_value(PAIR *pp)
4018 char *p = pp->value;
4024 #if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
4025 static int securitycheck_enabled(void)
4027 static int enabled = -1;
4029 if (enabled == -1) {
4030 if (OSSL_PROVIDER_available(libctx, "fips")) {
4031 OSSL_PARAM params[2];
4032 OSSL_PROVIDER *prov = NULL;
4035 prov = OSSL_PROVIDER_load(libctx, "fips");
4038 OSSL_PARAM_construct_int(OSSL_PROV_PARAM_SECURITY_CHECKS,
4040 params[1] = OSSL_PARAM_construct_end();
4041 OSSL_PROVIDER_get_params(prov, params);
4042 OSSL_PROVIDER_unload(prov);
4054 * Return 1 if one of the providers named in the string is available.
4055 * The provider names are separated with whitespace.
4056 * NOTE: destructive function, it inserts '\0' after each provider name.
4058 static int prov_available(char *providers)
4064 for (; isspace((unsigned char)(*providers)); providers++)
4066 if (*providers == '\0')
4067 break; /* End of the road */
4068 for (p = providers; *p != '\0' && !isspace((unsigned char)(*p)); p++)
4074 if (OSSL_PROVIDER_available(libctx, providers))
4075 return 1; /* Found one */
4080 /* Read and parse one test. Return 0 if failure, 1 if okay. */
4081 static int parse(EVP_TEST *t)
4083 KEY_LIST *key, **klist;
4086 int i, j, skipped = 0;
4090 if (BIO_eof(t->s.fp))
4093 if (!test_readstanza(&t->s))
4095 } while (t->s.numpairs == 0);
4096 pp = &t->s.pairs[0];
4098 /* Are we adding a key? */
4102 if (strcmp(pp->key, "PrivateKey") == 0) {
4103 pkey = PEM_read_bio_PrivateKey_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
4104 if (pkey == NULL && !key_unsupported()) {
4105 EVP_PKEY_free(pkey);
4106 TEST_info("Can't read private key %s", pp->value);
4107 TEST_openssl_errors();
4110 klist = &private_keys;
4111 } else if (strcmp(pp->key, "PublicKey") == 0) {
4112 pkey = PEM_read_bio_PUBKEY_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
4113 if (pkey == NULL && !key_unsupported()) {
4114 EVP_PKEY_free(pkey);
4115 TEST_info("Can't read public key %s", pp->value);
4116 TEST_openssl_errors();
4119 klist = &public_keys;
4120 } else if (strcmp(pp->key, "PrivateKeyRaw") == 0
4121 || strcmp(pp->key, "PublicKeyRaw") == 0) {
4122 char *strnid = NULL, *keydata = NULL;
4123 unsigned char *keybin;
4127 if (strcmp(pp->key, "PrivateKeyRaw") == 0)
4128 klist = &private_keys;
4130 klist = &public_keys;
4132 strnid = strchr(pp->value, ':');
4133 if (strnid != NULL) {
4135 keydata = strchr(strnid, ':');
4136 if (keydata != NULL)
4139 if (keydata == NULL) {
4140 TEST_info("Failed to parse %s value", pp->key);
4144 nid = OBJ_txt2nid(strnid);
4145 if (nid == NID_undef) {
4146 TEST_info("Unrecognised algorithm NID");
4149 if (!parse_bin(keydata, &keybin, &keylen)) {
4150 TEST_info("Failed to create binary key");
4153 if (klist == &private_keys)
4154 pkey = EVP_PKEY_new_raw_private_key_ex(libctx, strnid, NULL, keybin,
4157 pkey = EVP_PKEY_new_raw_public_key_ex(libctx, strnid, NULL, keybin,
4159 if (pkey == NULL && !key_unsupported()) {
4160 TEST_info("Can't read %s data", pp->key);
4161 OPENSSL_free(keybin);
4162 TEST_openssl_errors();
4165 OPENSSL_free(keybin);
4166 } else if (strcmp(pp->key, "Availablein") == 0) {
4167 if (!prov_available(pp->value)) {
4168 TEST_info("skipping, '%s' provider not available: %s:%d",
4169 pp->value, t->s.test_file, t->s.start);
4176 } else if (strcmp(pp->key, "FIPSversion") == 0) {
4177 if (prov_available("fips")) {
4178 j = fips_provider_version_match(libctx, pp->value);
4180 TEST_info("Line %d: error matching FIPS versions\n", t->s.curr);
4182 } else if (j == 0) {
4183 TEST_info("skipping, FIPS provider incompatible version: %s:%d",
4184 t->s.test_file, t->s.start);
4194 /* If we have a key add to list */
4195 if (klist != NULL) {
4196 if (find_key(NULL, pp->value, *klist)) {
4197 TEST_info("Duplicate key %s", pp->value);
4200 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
4202 key->name = take_value(pp);
4207 /* Go back and start a new stanza. */
4208 if ((t->s.numpairs - skipped) != 1)
4209 TEST_info("Line %d: missing blank line\n", t->s.curr);
4213 /* Find the test, based on first keyword. */
4214 if (!TEST_ptr(t->meth = find_test(pp->key)))
4216 if (!t->meth->init(t, pp->value)) {
4217 TEST_error("unknown %s: %s\n", pp->key, pp->value);
4221 /* TEST_info("skipping %s %s", pp->key, pp->value); */
4225 for (pp++, i = 1; i < (t->s.numpairs - skipped); pp++, i++) {
4226 if (strcmp(pp->key, "Securitycheck") == 0) {
4227 #if defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
4229 if (!securitycheck_enabled())
4232 TEST_info("skipping, Securitycheck is disabled: %s:%d",
4233 t->s.test_file, t->s.start);
4237 } else if (strcmp(pp->key, "Availablein") == 0) {
4238 TEST_info("Line %d: 'Availablein' should be the first option",
4241 } else if (strcmp(pp->key, "Result") == 0) {
4242 if (t->expected_err != NULL) {
4243 TEST_info("Line %d: multiple result lines", t->s.curr);
4246 t->expected_err = take_value(pp);
4247 } else if (strcmp(pp->key, "Function") == 0) {
4248 /* Ignore old line. */
4249 } else if (strcmp(pp->key, "Reason") == 0) {
4250 if (t->reason != NULL) {
4251 TEST_info("Line %d: multiple reason lines", t->s.curr);
4254 t->reason = take_value(pp);
4255 } else if (strcmp(pp->key, "Threads") == 0) {
4256 if (OSSL_set_max_threads(libctx, atoi(pp->value)) == 0) {
4257 TEST_info("skipping, '%s' threads not available: %s:%d",
4258 pp->value, t->s.test_file, t->s.start);
4262 /* Must be test specific line: try to parse it */
4263 int rv = t->meth->parse(t, pp->key, pp->value);
4266 TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key);
4270 TEST_info("Line %d: error processing keyword %s = %s\n",
4271 t->s.curr, pp->key, pp->value);
4282 static int run_file_tests(int i)
4285 const char *testfile = test_get_argument(i);
4288 if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t))))
4290 if (!test_start_file(&t->s, testfile)) {
4295 while (!BIO_eof(t->s.fp)) {
4301 if (c == 0 || !run_test(t)) {
4306 test_end_file(&t->s);
4309 free_key_list(public_keys);
4310 free_key_list(private_keys);
4317 const OPTIONS *test_get_options(void)
4319 static const OPTIONS test_options[] = {
4320 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
4321 { "config", OPT_CONFIG_FILE, '<',
4322 "The configuration file to use for the libctx" },
4323 { "process", OPT_IN_PLACE, 's',
4324 "Mode for data processing by cipher tests [in_place/both], both by default"},
4325 { "provider", OPT_PROVIDER_NAME, 's',
4326 "The provider to load (when no configuration file, the default value is 'default')" },
4327 { "propquery", OPT_PROV_PROPQUERY, 's',
4328 "Property query used when fetching algorithms" },
4329 { "chunk", OPT_DATA_CHUNK, 'N', "Size of data chunks to be processed, 0 for default size"},
4330 { OPT_HELP_STR, 1, '-', "file\tFile to run tests on.\n" },
4333 return test_options;
4336 int setup_tests(void)
4339 char *config_file = NULL;
4340 char *provider_name = NULL;
4344 while ((o = opt_next()) != OPT_EOF) {
4346 case OPT_CONFIG_FILE:
4347 config_file = opt_arg();
4350 if ((process_mode_in_place = evp_test_process_mode(opt_arg())) == -1)
4351 case OPT_DATA_CHUNK:
4352 if (!opt_int(opt_arg(), &data_chunk_size))
4355 case OPT_PROVIDER_NAME:
4356 provider_name = opt_arg();
4358 case OPT_PROV_PROPQUERY:
4359 propquery = opt_arg();
4361 case OPT_TEST_CASES:
4370 * Load the provider via configuration into the created library context.
4371 * Load the 'null' provider into the default library context to ensure that
4372 * the tests do not fallback to using the default provider.
4374 if (config_file == NULL && provider_name == NULL)
4375 provider_name = "default";
4376 if (!test_get_libctx(&libctx, &prov_null, config_file, &libprov, provider_name))
4379 n = test_get_argument_count();
4383 ADD_ALL_TESTS(run_file_tests, n);
4387 void cleanup_tests(void)
4389 OSSL_PROVIDER_unload(libprov);
4390 OSSL_PROVIDER_unload(prov_null);
4391 OSSL_LIB_CTX_free(libctx);
4394 static int is_digest_disabled(const char *name)
4396 #ifdef OPENSSL_NO_BLAKE2
4397 if (HAS_CASE_PREFIX(name, "BLAKE"))
4400 #ifdef OPENSSL_NO_MD2
4401 if (OPENSSL_strcasecmp(name, "MD2") == 0)
4404 #ifdef OPENSSL_NO_MDC2
4405 if (OPENSSL_strcasecmp(name, "MDC2") == 0)
4408 #ifdef OPENSSL_NO_MD4
4409 if (OPENSSL_strcasecmp(name, "MD4") == 0)
4412 #ifdef OPENSSL_NO_MD5
4413 if (OPENSSL_strcasecmp(name, "MD5") == 0)
4416 #ifdef OPENSSL_NO_RMD160
4417 if (OPENSSL_strcasecmp(name, "RIPEMD160") == 0)
4420 #ifdef OPENSSL_NO_SM3
4421 if (OPENSSL_strcasecmp(name, "SM3") == 0)
4424 #ifdef OPENSSL_NO_WHIRLPOOL
4425 if (OPENSSL_strcasecmp(name, "WHIRLPOOL") == 0)
4431 static int is_pkey_disabled(const char *name)
4433 #ifdef OPENSSL_NO_EC
4434 if (HAS_CASE_PREFIX(name, "EC"))
4437 #ifdef OPENSSL_NO_DH
4438 if (HAS_CASE_PREFIX(name, "DH"))
4441 #ifdef OPENSSL_NO_DSA
4442 if (HAS_CASE_PREFIX(name, "DSA"))
4448 static int is_mac_disabled(const char *name)
4450 #ifdef OPENSSL_NO_BLAKE2
4451 if (HAS_CASE_PREFIX(name, "BLAKE2BMAC")
4452 || HAS_CASE_PREFIX(name, "BLAKE2SMAC"))
4455 #ifdef OPENSSL_NO_CMAC
4456 if (HAS_CASE_PREFIX(name, "CMAC"))
4459 #ifdef OPENSSL_NO_POLY1305
4460 if (HAS_CASE_PREFIX(name, "Poly1305"))
4463 #ifdef OPENSSL_NO_SIPHASH
4464 if (HAS_CASE_PREFIX(name, "SipHash"))
4469 static int is_kdf_disabled(const char *name)
4471 #ifdef OPENSSL_NO_SCRYPT
4472 if (HAS_CASE_SUFFIX(name, "SCRYPT"))
4475 #ifdef OPENSSL_NO_ARGON2
4476 if (HAS_CASE_SUFFIX(name, "ARGON2"))
4482 static int is_cipher_disabled(const char *name)
4484 #ifdef OPENSSL_NO_ARIA
4485 if (HAS_CASE_PREFIX(name, "ARIA"))
4488 #ifdef OPENSSL_NO_BF
4489 if (HAS_CASE_PREFIX(name, "BF"))
4492 #ifdef OPENSSL_NO_CAMELLIA
4493 if (HAS_CASE_PREFIX(name, "CAMELLIA"))
4496 #ifdef OPENSSL_NO_CAST
4497 if (HAS_CASE_PREFIX(name, "CAST"))
4500 #ifdef OPENSSL_NO_CHACHA
4501 if (HAS_CASE_PREFIX(name, "CHACHA"))
4504 #ifdef OPENSSL_NO_POLY1305
4505 if (HAS_CASE_SUFFIX(name, "Poly1305"))
4508 #ifdef OPENSSL_NO_DES
4509 if (HAS_CASE_PREFIX(name, "DES"))
4511 if (HAS_CASE_SUFFIX(name, "3DESwrap"))
4514 #ifdef OPENSSL_NO_OCB
4515 if (HAS_CASE_SUFFIX(name, "OCB"))
4518 #ifdef OPENSSL_NO_IDEA
4519 if (HAS_CASE_PREFIX(name, "IDEA"))
4522 #ifdef OPENSSL_NO_RC2
4523 if (HAS_CASE_PREFIX(name, "RC2"))
4526 #ifdef OPENSSL_NO_RC4
4527 if (HAS_CASE_PREFIX(name, "RC4"))
4530 #ifdef OPENSSL_NO_RC5
4531 if (HAS_CASE_PREFIX(name, "RC5"))
4534 #ifdef OPENSSL_NO_SEED
4535 if (HAS_CASE_PREFIX(name, "SEED"))
4538 #ifdef OPENSSL_NO_SIV
4539 if (HAS_CASE_SUFFIX(name, "SIV"))
4542 #ifdef OPENSSL_NO_SM4
4543 if (HAS_CASE_PREFIX(name, "SM4"))