2 * Copyright 2015-2021 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 "../e_os.h" /* strcasecmp */
16 #include <openssl/evp.h>
17 #include <openssl/pem.h>
18 #include <openssl/err.h>
19 #include <openssl/provider.h>
20 #include <openssl/x509v3.h>
21 #include <openssl/pkcs12.h>
22 #include <openssl/kdf.h>
23 #include <openssl/params.h>
24 #include <openssl/core_names.h>
25 #include <openssl/fips_names.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 {
78 static OSSL_PROVIDER *prov_null = NULL;
79 static OSSL_LIB_CTX *libctx = NULL;
81 /* List of public and private keys */
82 static KEY_LIST *private_keys;
83 static KEY_LIST *public_keys;
85 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst);
86 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen);
87 static int is_digest_disabled(const char *name);
88 static int is_pkey_disabled(const char *name);
89 static int is_mac_disabled(const char *name);
90 static int is_cipher_disabled(const char *name);
91 static int is_kdf_disabled(const char *name);
94 * Compare two memory regions for equality, returning zero if they differ.
95 * However, if there is expected to be an error and the actual error
96 * matches then the memory is expected to be different so handle this
97 * case without producing unnecessary test framework output.
99 static int memory_err_compare(EVP_TEST *t, const char *err,
100 const void *expected, size_t expected_len,
101 const void *got, size_t got_len)
105 if (t->expected_err != NULL && strcmp(t->expected_err, err) == 0)
106 r = !TEST_mem_ne(expected, expected_len, got, got_len);
108 r = TEST_mem_eq(expected, expected_len, got, got_len);
115 * Structure used to hold a list of blocks of memory to test
116 * calls to "update" like functions.
118 struct evp_test_buffer_st {
125 static void evp_test_buffer_free(EVP_TEST_BUFFER *db)
128 OPENSSL_free(db->buf);
133 /* append buffer to a list */
134 static int evp_test_buffer_append(const char *value,
135 STACK_OF(EVP_TEST_BUFFER) **sk)
137 EVP_TEST_BUFFER *db = NULL;
139 if (!TEST_ptr(db = OPENSSL_malloc(sizeof(*db))))
142 if (!parse_bin(value, &db->buf, &db->buflen))
147 if (*sk == NULL && !TEST_ptr(*sk = sk_EVP_TEST_BUFFER_new_null()))
149 if (!sk_EVP_TEST_BUFFER_push(*sk, db))
155 evp_test_buffer_free(db);
159 /* replace last buffer in list with copies of itself */
160 static int evp_test_buffer_ncopy(const char *value,
161 STACK_OF(EVP_TEST_BUFFER) *sk)
164 unsigned char *tbuf, *p;
166 int ncopy = atoi(value);
171 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
173 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
175 tbuflen = db->buflen * ncopy;
176 if (!TEST_ptr(tbuf = OPENSSL_malloc(tbuflen)))
178 for (i = 0, p = tbuf; i < ncopy; i++, p += db->buflen)
179 memcpy(p, db->buf, db->buflen);
181 OPENSSL_free(db->buf);
183 db->buflen = tbuflen;
187 /* set repeat count for last buffer in list */
188 static int evp_test_buffer_set_count(const char *value,
189 STACK_OF(EVP_TEST_BUFFER) *sk)
192 int count = atoi(value);
197 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
200 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
201 if (db->count_set != 0)
204 db->count = (size_t)count;
209 /* call "fn" with each element of the list in turn */
210 static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER) *sk,
212 const unsigned char *buf,
218 for (i = 0; i < sk_EVP_TEST_BUFFER_num(sk); i++) {
219 EVP_TEST_BUFFER *tb = sk_EVP_TEST_BUFFER_value(sk, i);
222 for (j = 0; j < tb->count; j++) {
223 if (fn(ctx, tb->buf, tb->buflen) <= 0)
231 * Unescape some sequences in string literals (only \n for now).
232 * Return an allocated buffer, set |out_len|. If |input_len|
233 * is zero, get an empty buffer but set length to zero.
235 static unsigned char* unescape(const char *input, size_t input_len,
238 unsigned char *ret, *p;
241 if (input_len == 0) {
243 return OPENSSL_zalloc(1);
246 /* Escaping is non-expanding; over-allocate original size for simplicity. */
247 if (!TEST_ptr(ret = p = OPENSSL_malloc(input_len)))
250 for (i = 0; i < input_len; i++) {
251 if (*input == '\\') {
252 if (i == input_len - 1 || *++input != 'n') {
253 TEST_error("Bad escape sequence in file");
273 * For a hex string "value" convert to a binary allocated buffer.
274 * Return 1 on success or 0 on failure.
276 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen)
280 /* Check for NULL literal */
281 if (strcmp(value, "NULL") == 0) {
287 /* Check for empty value */
288 if (*value == '\0') {
290 * Don't return NULL for zero length buffer. This is needed for
291 * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key
292 * buffer even if the key length is 0, in order to detect key reset.
294 *buf = OPENSSL_malloc(1);
302 /* Check for string literal */
303 if (value[0] == '"') {
304 size_t vlen = strlen(++value);
306 if (vlen == 0 || value[vlen - 1] != '"')
309 *buf = unescape(value, vlen, buflen);
310 return *buf == NULL ? 0 : 1;
313 /* Otherwise assume as hex literal and convert it to binary buffer */
314 if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) {
315 TEST_info("Can't convert %s", value);
316 TEST_openssl_errors();
319 /* Size of input buffer means we'll never overflow */
325 ** MESSAGE DIGEST TESTS
328 typedef struct digest_data_st {
329 /* Digest this test is for */
330 const EVP_MD *digest;
331 EVP_MD *fetched_digest;
332 /* Input to digest */
333 STACK_OF(EVP_TEST_BUFFER) *input;
334 /* Expected output */
335 unsigned char *output;
341 static int digest_test_init(EVP_TEST *t, const char *alg)
344 const EVP_MD *digest;
345 EVP_MD *fetched_digest;
347 if (is_digest_disabled(alg)) {
348 TEST_info("skipping, '%s' is disabled", alg);
353 if ((digest = fetched_digest = EVP_MD_fetch(libctx, alg, NULL)) == NULL
354 && (digest = EVP_get_digestbyname(alg)) == NULL)
356 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
359 mdat->digest = digest;
360 mdat->fetched_digest = fetched_digest;
362 if (fetched_digest != NULL)
363 TEST_info("%s is fetched", alg);
367 static void digest_test_cleanup(EVP_TEST *t)
369 DIGEST_DATA *mdat = t->data;
371 sk_EVP_TEST_BUFFER_pop_free(mdat->input, evp_test_buffer_free);
372 OPENSSL_free(mdat->output);
373 EVP_MD_free(mdat->fetched_digest);
376 static int digest_test_parse(EVP_TEST *t,
377 const char *keyword, const char *value)
379 DIGEST_DATA *mdata = t->data;
381 if (strcmp(keyword, "Input") == 0)
382 return evp_test_buffer_append(value, &mdata->input);
383 if (strcmp(keyword, "Output") == 0)
384 return parse_bin(value, &mdata->output, &mdata->output_len);
385 if (strcmp(keyword, "Count") == 0)
386 return evp_test_buffer_set_count(value, mdata->input);
387 if (strcmp(keyword, "Ncopy") == 0)
388 return evp_test_buffer_ncopy(value, mdata->input);
389 if (strcmp(keyword, "Padding") == 0)
390 return (mdata->pad_type = atoi(value)) > 0;
394 static int digest_update_fn(void *ctx, const unsigned char *buf, size_t buflen)
396 return EVP_DigestUpdate(ctx, buf, buflen);
399 static int digest_test_run(EVP_TEST *t)
401 DIGEST_DATA *expected = t->data;
403 unsigned char *got = NULL;
404 unsigned int got_len;
405 OSSL_PARAM params[2];
407 t->err = "TEST_FAILURE";
408 if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
411 got = OPENSSL_malloc(expected->output_len > EVP_MAX_MD_SIZE ?
412 expected->output_len : EVP_MAX_MD_SIZE);
416 if (!EVP_DigestInit_ex(mctx, expected->digest, NULL)) {
417 t->err = "DIGESTINIT_ERROR";
420 if (expected->pad_type > 0) {
421 params[0] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_PAD_TYPE,
422 &expected->pad_type);
423 params[1] = OSSL_PARAM_construct_end();
424 if (!TEST_int_gt(EVP_MD_CTX_set_params(mctx, params), 0)) {
425 t->err = "PARAMS_ERROR";
429 if (!evp_test_buffer_do(expected->input, digest_update_fn, mctx)) {
430 t->err = "DIGESTUPDATE_ERROR";
434 if (EVP_MD_flags(expected->digest) & EVP_MD_FLAG_XOF) {
435 EVP_MD_CTX *mctx_cpy;
436 char dont[] = "touch";
438 if (!TEST_ptr(mctx_cpy = EVP_MD_CTX_new())) {
441 if (!EVP_MD_CTX_copy(mctx_cpy, mctx)) {
442 EVP_MD_CTX_free(mctx_cpy);
445 if (!EVP_DigestFinalXOF(mctx_cpy, (unsigned char *)dont, 0)) {
446 EVP_MD_CTX_free(mctx_cpy);
447 t->err = "DIGESTFINALXOF_ERROR";
450 if (!TEST_str_eq(dont, "touch")) {
451 EVP_MD_CTX_free(mctx_cpy);
452 t->err = "DIGESTFINALXOF_ERROR";
455 EVP_MD_CTX_free(mctx_cpy);
457 got_len = expected->output_len;
458 if (!EVP_DigestFinalXOF(mctx, got, got_len)) {
459 t->err = "DIGESTFINALXOF_ERROR";
463 if (!EVP_DigestFinal(mctx, got, &got_len)) {
464 t->err = "DIGESTFINAL_ERROR";
468 if (!TEST_int_eq(expected->output_len, got_len)) {
469 t->err = "DIGEST_LENGTH_MISMATCH";
472 if (!memory_err_compare(t, "DIGEST_MISMATCH",
473 expected->output, expected->output_len,
481 EVP_MD_CTX_free(mctx);
485 static const EVP_TEST_METHOD digest_test_method = {
497 typedef struct cipher_data_st {
498 const EVP_CIPHER *cipher;
499 EVP_CIPHER *fetched_cipher;
501 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
505 size_t key_bits; /* Used by RC2 */
507 unsigned char *next_iv; /* Expected IV state after operation */
510 unsigned char *plaintext;
511 size_t plaintext_len;
512 unsigned char *ciphertext;
513 size_t ciphertext_len;
514 /* GCM, CCM, OCB and SIV only */
515 unsigned char *aad[AAD_NUM];
516 size_t aad_len[AAD_NUM];
518 const char *cts_mode;
523 static int cipher_test_init(EVP_TEST *t, const char *alg)
525 const EVP_CIPHER *cipher;
526 EVP_CIPHER *fetched_cipher;
530 if (is_cipher_disabled(alg)) {
532 TEST_info("skipping, '%s' is disabled", alg);
536 if ((cipher = fetched_cipher = EVP_CIPHER_fetch(libctx, alg, NULL)) == NULL
537 && (cipher = EVP_get_cipherbyname(alg)) == NULL)
540 cdat = OPENSSL_zalloc(sizeof(*cdat));
541 cdat->cipher = cipher;
542 cdat->fetched_cipher = fetched_cipher;
544 m = EVP_CIPHER_mode(cipher);
545 if (m == EVP_CIPH_GCM_MODE
546 || m == EVP_CIPH_OCB_MODE
547 || m == EVP_CIPH_SIV_MODE
548 || m == EVP_CIPH_CCM_MODE)
550 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
556 if (fetched_cipher != NULL)
557 TEST_info("%s is fetched", alg);
561 static void cipher_test_cleanup(EVP_TEST *t)
564 CIPHER_DATA *cdat = t->data;
566 OPENSSL_free(cdat->key);
567 OPENSSL_free(cdat->iv);
568 OPENSSL_free(cdat->next_iv);
569 OPENSSL_free(cdat->ciphertext);
570 OPENSSL_free(cdat->plaintext);
571 for (i = 0; i < AAD_NUM; i++)
572 OPENSSL_free(cdat->aad[i]);
573 OPENSSL_free(cdat->tag);
574 EVP_CIPHER_free(cdat->fetched_cipher);
577 static int cipher_test_parse(EVP_TEST *t, const char *keyword,
580 CIPHER_DATA *cdat = t->data;
583 if (strcmp(keyword, "Key") == 0)
584 return parse_bin(value, &cdat->key, &cdat->key_len);
585 if (strcmp(keyword, "Rounds") == 0) {
589 cdat->rounds = (unsigned int)i;
592 if (strcmp(keyword, "IV") == 0)
593 return parse_bin(value, &cdat->iv, &cdat->iv_len);
594 if (strcmp(keyword, "NextIV") == 0)
595 return parse_bin(value, &cdat->next_iv, &cdat->iv_len);
596 if (strcmp(keyword, "Plaintext") == 0)
597 return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len);
598 if (strcmp(keyword, "Ciphertext") == 0)
599 return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
600 if (strcmp(keyword, "KeyBits") == 0) {
604 cdat->key_bits = (size_t)i;
608 if (strcmp(keyword, "AAD") == 0) {
609 for (i = 0; i < AAD_NUM; i++) {
610 if (cdat->aad[i] == NULL)
611 return parse_bin(value, &cdat->aad[i], &cdat->aad_len[i]);
615 if (strcmp(keyword, "Tag") == 0)
616 return parse_bin(value, &cdat->tag, &cdat->tag_len);
617 if (strcmp(keyword, "SetTagLate") == 0) {
618 if (strcmp(value, "TRUE") == 0)
620 else if (strcmp(value, "FALSE") == 0)
628 if (strcmp(keyword, "Operation") == 0) {
629 if (strcmp(value, "ENCRYPT") == 0)
631 else if (strcmp(value, "DECRYPT") == 0)
637 if (strcmp(keyword, "CTSMode") == 0) {
638 cdat->cts_mode = value;
644 static int cipher_test_enc(EVP_TEST *t, int enc,
645 size_t out_misalign, size_t inp_misalign, int frag)
647 CIPHER_DATA *expected = t->data;
648 unsigned char *in, *expected_out, *tmp = NULL;
649 size_t in_len, out_len, donelen = 0;
650 int ok = 0, tmplen, chunklen, tmpflen, i;
651 EVP_CIPHER_CTX *ctx_base = NULL;
652 EVP_CIPHER_CTX *ctx = NULL;
654 t->err = "TEST_FAILURE";
655 if (!TEST_ptr(ctx_base = EVP_CIPHER_CTX_new()))
657 if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
659 EVP_CIPHER_CTX_set_flags(ctx_base, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
661 in = expected->plaintext;
662 in_len = expected->plaintext_len;
663 expected_out = expected->ciphertext;
664 out_len = expected->ciphertext_len;
666 in = expected->ciphertext;
667 in_len = expected->ciphertext_len;
668 expected_out = expected->plaintext;
669 out_len = expected->plaintext_len;
671 if (inp_misalign == (size_t)-1) {
672 /* Exercise in-place encryption */
673 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
676 in = memcpy(tmp + out_misalign, in, in_len);
678 inp_misalign += 16 - ((out_misalign + in_len) & 15);
680 * 'tmp' will store both output and copy of input. We make the copy
681 * of input to specifically aligned part of 'tmp'. So we just
682 * figured out how much padding would ensure the required alignment,
683 * now we allocate extended buffer and finally copy the input just
684 * past inp_misalign in expression below. Output will be written
685 * past out_misalign...
687 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
688 inp_misalign + in_len);
691 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
692 inp_misalign, in, in_len);
694 if (!EVP_CipherInit_ex(ctx_base, expected->cipher, NULL, NULL, NULL, enc)) {
695 t->err = "CIPHERINIT_ERROR";
698 if (expected->cts_mode != NULL) {
699 OSSL_PARAM params[2];
701 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE,
702 (char *)expected->cts_mode,
704 params[1] = OSSL_PARAM_construct_end();
705 if (!EVP_CIPHER_CTX_set_params(ctx_base, params)) {
706 t->err = "INVALID_CTS_MODE";
711 if (expected->aead) {
712 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_IVLEN,
713 expected->iv_len, 0)) {
714 t->err = "INVALID_IV_LENGTH";
717 } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx_base)) {
718 t->err = "INVALID_IV_LENGTH";
722 if (expected->aead) {
725 * If encrypting or OCB just set tag length initially, otherwise
726 * set tag length and value.
728 if (enc || expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late) {
729 t->err = "TAG_LENGTH_SET_ERROR";
732 t->err = "TAG_SET_ERROR";
735 if (tag || expected->aead != EVP_CIPH_GCM_MODE) {
736 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_TAG,
737 expected->tag_len, tag))
742 if (expected->rounds > 0) {
743 int rounds = (int)expected->rounds;
745 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC5_ROUNDS, rounds, NULL)) {
746 t->err = "INVALID_ROUNDS";
751 if (!EVP_CIPHER_CTX_set_key_length(ctx_base, expected->key_len)) {
752 t->err = "INVALID_KEY_LENGTH";
755 if (expected->key_bits > 0) {
756 int bits = (int)expected->key_bits;
758 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC2_KEY_BITS, bits, NULL)) {
759 t->err = "INVALID KEY BITS";
763 if (!EVP_CipherInit_ex(ctx_base, NULL, NULL, expected->key, expected->iv, -1)) {
764 t->err = "KEY_SET_ERROR";
768 /* Check that we get the same IV back */
769 if (expected->iv != NULL) {
770 /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
771 unsigned char iv[128];
772 if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx_base, iv, sizeof(iv)))
773 || ((EVP_CIPHER_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
774 && !TEST_mem_eq(expected->iv, expected->iv_len, iv,
775 expected->iv_len))) {
776 t->err = "INVALID_IV";
781 /* Test that the cipher dup functions correctly if it is supported */
782 if (EVP_CIPHER_CTX_copy(ctx, ctx_base)) {
783 EVP_CIPHER_CTX_free(ctx_base);
786 EVP_CIPHER_CTX_free(ctx);
790 if (expected->aead == EVP_CIPH_CCM_MODE) {
791 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
792 t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
796 if (expected->aad[0] != NULL) {
797 t->err = "AAD_SET_ERROR";
799 for (i = 0; expected->aad[i] != NULL; i++) {
800 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i],
801 expected->aad_len[i]))
806 * Supply the AAD in chunks less than the block size where possible
808 for (i = 0; expected->aad[i] != NULL; i++) {
809 if (expected->aad_len[i] > 0) {
810 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], 1))
814 if (expected->aad_len[i] > 2) {
815 if (!EVP_CipherUpdate(ctx, NULL, &chunklen,
816 expected->aad[i] + donelen,
817 expected->aad_len[i] - 2))
819 donelen += expected->aad_len[i] - 2;
821 if (expected->aad_len[i] > 1
822 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
823 expected->aad[i] + donelen, 1))
829 if (!enc && (expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late)) {
830 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
831 expected->tag_len, expected->tag)) {
832 t->err = "TAG_SET_ERROR";
837 EVP_CIPHER_CTX_set_padding(ctx, 0);
838 t->err = "CIPHERUPDATE_ERROR";
841 /* We supply the data all in one go */
842 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
845 /* Supply the data in chunks less than the block size where possible */
847 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
854 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
862 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
868 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
869 t->err = "CIPHERFINAL_ERROR";
872 if (!memory_err_compare(t, "VALUE_MISMATCH", expected_out, out_len,
873 tmp + out_misalign, tmplen + tmpflen))
875 if (enc && expected->aead) {
876 unsigned char rtag[16];
878 if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) {
879 t->err = "TAG_LENGTH_INTERNAL_ERROR";
882 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
883 expected->tag_len, rtag)) {
884 t->err = "TAG_RETRIEVE_ERROR";
887 if (!memory_err_compare(t, "TAG_VALUE_MISMATCH",
888 expected->tag, expected->tag_len,
889 rtag, expected->tag_len))
892 /* Check the updated IV */
893 if (expected->next_iv != NULL) {
894 /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
895 unsigned char iv[128];
896 if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv)))
897 || ((EVP_CIPHER_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
898 && !TEST_mem_eq(expected->next_iv, expected->iv_len, iv,
899 expected->iv_len))) {
900 t->err = "INVALID_NEXT_IV";
910 EVP_CIPHER_CTX_free(ctx_base);
911 EVP_CIPHER_CTX_free(ctx);
915 static int cipher_test_run(EVP_TEST *t)
917 CIPHER_DATA *cdat = t->data;
919 size_t out_misalign, inp_misalign;
925 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
926 /* IV is optional and usually omitted in wrap mode */
927 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
932 if (cdat->aead && !cdat->tag) {
936 for (out_misalign = 0; out_misalign <= 1;) {
937 static char aux_err[64];
938 t->aux_err = aux_err;
939 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
940 if (inp_misalign == (size_t)-1) {
941 /* kludge: inp_misalign == -1 means "exercise in-place" */
942 BIO_snprintf(aux_err, sizeof(aux_err),
943 "%s in-place, %sfragmented",
944 out_misalign ? "misaligned" : "aligned",
947 BIO_snprintf(aux_err, sizeof(aux_err),
948 "%s output and %s input, %sfragmented",
949 out_misalign ? "misaligned" : "aligned",
950 inp_misalign ? "misaligned" : "aligned",
954 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
955 /* Not fatal errors: return */
962 if (cdat->enc != 1) {
963 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
964 /* Not fatal errors: return */
973 if (out_misalign == 1 && frag == 0) {
975 * XTS, SIV, CCM and Wrap modes have special requirements about input
976 * lengths so we don't fragment for those
978 if (cdat->aead == EVP_CIPH_CCM_MODE
979 || ((EVP_CIPHER_flags(cdat->cipher) & EVP_CIPH_FLAG_CTS) != 0)
980 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_SIV_MODE
981 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
982 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
995 static const EVP_TEST_METHOD cipher_test_method = {
1008 typedef struct mac_data_st {
1009 /* MAC type in one form or another */
1011 EVP_MAC *mac; /* for mac_test_run_mac */
1012 int type; /* for mac_test_run_pkey */
1013 /* Algorithm string for this MAC */
1022 unsigned char *input;
1024 /* Expected output */
1025 unsigned char *output;
1027 unsigned char *custom;
1029 /* MAC salt (blake2) */
1030 unsigned char *salt;
1032 /* Collection of controls */
1033 STACK_OF(OPENSSL_STRING) *controls;
1036 static int mac_test_init(EVP_TEST *t, const char *alg)
1038 EVP_MAC *mac = NULL;
1039 int type = NID_undef;
1042 if (is_mac_disabled(alg)) {
1043 TEST_info("skipping, '%s' is disabled", alg);
1047 if ((mac = EVP_MAC_fetch(libctx, alg, NULL)) == NULL) {
1049 * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
1050 * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
1051 * the EVP_PKEY method.
1053 size_t sz = strlen(alg);
1054 static const char epilogue[] = " by EVP_PKEY";
1056 if (sz >= sizeof(epilogue)
1057 && strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0)
1058 sz -= sizeof(epilogue) - 1;
1060 if (strncmp(alg, "HMAC", sz) == 0)
1061 type = EVP_PKEY_HMAC;
1062 else if (strncmp(alg, "CMAC", sz) == 0)
1063 type = EVP_PKEY_CMAC;
1064 else if (strncmp(alg, "Poly1305", sz) == 0)
1065 type = EVP_PKEY_POLY1305;
1066 else if (strncmp(alg, "SipHash", sz) == 0)
1067 type = EVP_PKEY_SIPHASH;
1072 mdat = OPENSSL_zalloc(sizeof(*mdat));
1074 mdat->mac_name = OPENSSL_strdup(alg);
1076 mdat->controls = sk_OPENSSL_STRING_new_null();
1081 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
1082 static void openssl_free(char *m)
1087 static void mac_test_cleanup(EVP_TEST *t)
1089 MAC_DATA *mdat = t->data;
1091 EVP_MAC_free(mdat->mac);
1092 OPENSSL_free(mdat->mac_name);
1093 sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free);
1094 OPENSSL_free(mdat->alg);
1095 OPENSSL_free(mdat->key);
1096 OPENSSL_free(mdat->iv);
1097 OPENSSL_free(mdat->custom);
1098 OPENSSL_free(mdat->salt);
1099 OPENSSL_free(mdat->input);
1100 OPENSSL_free(mdat->output);
1103 static int mac_test_parse(EVP_TEST *t,
1104 const char *keyword, const char *value)
1106 MAC_DATA *mdata = t->data;
1108 if (strcmp(keyword, "Key") == 0)
1109 return parse_bin(value, &mdata->key, &mdata->key_len);
1110 if (strcmp(keyword, "IV") == 0)
1111 return parse_bin(value, &mdata->iv, &mdata->iv_len);
1112 if (strcmp(keyword, "Custom") == 0)
1113 return parse_bin(value, &mdata->custom, &mdata->custom_len);
1114 if (strcmp(keyword, "Salt") == 0)
1115 return parse_bin(value, &mdata->salt, &mdata->salt_len);
1116 if (strcmp(keyword, "Algorithm") == 0) {
1117 mdata->alg = OPENSSL_strdup(value);
1122 if (strcmp(keyword, "Input") == 0)
1123 return parse_bin(value, &mdata->input, &mdata->input_len);
1124 if (strcmp(keyword, "Output") == 0)
1125 return parse_bin(value, &mdata->output, &mdata->output_len);
1126 if (strcmp(keyword, "Ctrl") == 0)
1127 return sk_OPENSSL_STRING_push(mdata->controls,
1128 OPENSSL_strdup(value)) != 0;
1132 static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1138 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1140 p = strchr(tmpval, ':');
1143 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1146 t->err = "PKEY_CTRL_INVALID";
1148 t->err = "PKEY_CTRL_ERROR";
1151 OPENSSL_free(tmpval);
1155 static int mac_test_run_pkey(EVP_TEST *t)
1157 MAC_DATA *expected = t->data;
1158 EVP_MD_CTX *mctx = NULL;
1159 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1160 EVP_PKEY *key = NULL;
1161 const char *mdname = NULL;
1162 EVP_CIPHER *cipher = NULL;
1163 unsigned char *got = NULL;
1167 if (expected->alg == NULL)
1168 TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type));
1170 TEST_info("Trying the EVP_PKEY %s test with %s",
1171 OBJ_nid2sn(expected->type), expected->alg);
1173 if (expected->type == EVP_PKEY_CMAC) {
1174 #ifdef OPENSSL_NO_DEPRECATED_3_0
1175 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
1180 OSSL_LIB_CTX *tmpctx;
1182 if (expected->alg != NULL && is_cipher_disabled(expected->alg)) {
1183 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
1188 if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, expected->alg, NULL))) {
1189 t->err = "MAC_KEY_CREATE_ERROR";
1192 tmpctx = OSSL_LIB_CTX_set0_default(libctx);
1193 key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len,
1195 OSSL_LIB_CTX_set0_default(tmpctx);
1198 key = EVP_PKEY_new_raw_private_key_ex(libctx,
1199 OBJ_nid2sn(expected->type), NULL,
1200 expected->key, expected->key_len);
1203 t->err = "MAC_KEY_CREATE_ERROR";
1207 if (expected->type == EVP_PKEY_HMAC && expected->alg != NULL) {
1208 if (is_digest_disabled(expected->alg)) {
1209 TEST_info("skipping, HMAC '%s' is disabled", expected->alg);
1214 mdname = expected->alg;
1216 if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
1217 t->err = "INTERNAL_ERROR";
1220 if (!EVP_DigestSignInit_ex(mctx, &pctx, mdname, libctx, NULL, key, NULL)) {
1221 t->err = "DIGESTSIGNINIT_ERROR";
1224 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++)
1225 if (!mac_test_ctrl_pkey(t, pctx,
1226 sk_OPENSSL_STRING_value(expected->controls,
1228 t->err = "EVPPKEYCTXCTRL_ERROR";
1231 if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) {
1232 t->err = "DIGESTSIGNUPDATE_ERROR";
1235 if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) {
1236 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1239 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1240 t->err = "TEST_FAILURE";
1243 if (!EVP_DigestSignFinal(mctx, got, &got_len)
1244 || !memory_err_compare(t, "TEST_MAC_ERR",
1245 expected->output, expected->output_len,
1247 t->err = "TEST_MAC_ERR";
1252 EVP_CIPHER_free(cipher);
1253 EVP_MD_CTX_free(mctx);
1255 EVP_PKEY_CTX_free(genctx);
1260 static int mac_test_run_mac(EVP_TEST *t)
1262 MAC_DATA *expected = t->data;
1263 EVP_MAC_CTX *ctx = NULL;
1264 unsigned char *got = NULL;
1267 OSSL_PARAM params[21];
1268 size_t params_n = 0;
1269 size_t params_n_allocstart = 0;
1270 const OSSL_PARAM *defined_params =
1271 EVP_MAC_settable_ctx_params(expected->mac);
1273 if (expected->alg == NULL)
1274 TEST_info("Trying the EVP_MAC %s test", expected->mac_name);
1276 TEST_info("Trying the EVP_MAC %s test with %s",
1277 expected->mac_name, expected->alg);
1279 if (expected->alg != NULL) {
1281 * The underlying algorithm may be a cipher or a digest.
1282 * We don't know which it is, but we can ask the MAC what it
1283 * should be and bet on that.
1285 if (OSSL_PARAM_locate_const(defined_params,
1286 OSSL_MAC_PARAM_CIPHER) != NULL) {
1287 params[params_n++] =
1288 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
1290 } else if (OSSL_PARAM_locate_const(defined_params,
1291 OSSL_MAC_PARAM_DIGEST) != NULL) {
1292 params[params_n++] =
1293 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
1296 t->err = "MAC_BAD_PARAMS";
1300 if (expected->custom != NULL)
1301 params[params_n++] =
1302 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
1304 expected->custom_len);
1305 if (expected->salt != NULL)
1306 params[params_n++] =
1307 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT,
1309 expected->salt_len);
1310 if (expected->iv != NULL)
1311 params[params_n++] =
1312 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
1316 /* Unknown controls. They must match parameters that the MAC recognizes */
1317 if (params_n + sk_OPENSSL_STRING_num(expected->controls)
1318 >= OSSL_NELEM(params)) {
1319 t->err = "MAC_TOO_MANY_PARAMETERS";
1322 params_n_allocstart = params_n;
1323 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) {
1324 char *tmpkey, *tmpval;
1325 char *value = sk_OPENSSL_STRING_value(expected->controls, i);
1327 if (!TEST_ptr(tmpkey = OPENSSL_strdup(value))) {
1328 t->err = "MAC_PARAM_ERROR";
1331 tmpval = strchr(tmpkey, ':');
1336 || !OSSL_PARAM_allocate_from_text(¶ms[params_n],
1339 strlen(tmpval), NULL)) {
1340 OPENSSL_free(tmpkey);
1341 t->err = "MAC_PARAM_ERROR";
1346 OPENSSL_free(tmpkey);
1348 params[params_n] = OSSL_PARAM_construct_end();
1350 if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) {
1351 t->err = "MAC_CREATE_ERROR";
1355 if (!EVP_MAC_init(ctx, expected->key, expected->key_len, params)) {
1356 t->err = "MAC_INIT_ERROR";
1359 if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) {
1360 t->err = "MAC_UPDATE_ERROR";
1363 if (!EVP_MAC_final(ctx, NULL, &got_len, 0)) {
1364 t->err = "MAC_FINAL_LENGTH_ERROR";
1367 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1368 t->err = "TEST_FAILURE";
1371 if (!EVP_MAC_final(ctx, got, &got_len, got_len)
1372 || !memory_err_compare(t, "TEST_MAC_ERR",
1373 expected->output, expected->output_len,
1375 t->err = "TEST_MAC_ERR";
1380 while (params_n-- > params_n_allocstart) {
1381 OPENSSL_free(params[params_n].data);
1383 EVP_MAC_CTX_free(ctx);
1388 static int mac_test_run(EVP_TEST *t)
1390 MAC_DATA *expected = t->data;
1392 if (expected->mac != NULL)
1393 return mac_test_run_mac(t);
1394 return mac_test_run_pkey(t);
1397 static const EVP_TEST_METHOD mac_test_method = {
1408 ** These are all very similar and share much common code.
1411 typedef struct pkey_data_st {
1412 /* Context for this operation */
1414 /* Key operation to perform */
1415 int (*keyop) (EVP_PKEY_CTX *ctx,
1416 unsigned char *sig, size_t *siglen,
1417 const unsigned char *tbs, size_t tbslen);
1419 unsigned char *input;
1421 /* Expected output */
1422 unsigned char *output;
1427 * Perform public key operation setup: lookup key, allocated ctx and call
1428 * the appropriate initialisation function
1430 static int pkey_test_init(EVP_TEST *t, const char *name,
1432 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1433 int (*keyop)(EVP_PKEY_CTX *ctx,
1434 unsigned char *sig, size_t *siglen,
1435 const unsigned char *tbs,
1439 EVP_PKEY *pkey = NULL;
1443 rv = find_key(&pkey, name, public_keys);
1445 rv = find_key(&pkey, name, private_keys);
1446 if (rv == 0 || pkey == NULL) {
1447 TEST_info("skipping, key '%s' is disabled", name);
1452 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) {
1453 EVP_PKEY_free(pkey);
1456 kdata->keyop = keyop;
1457 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL))) {
1458 EVP_PKEY_free(pkey);
1459 OPENSSL_free(kdata);
1462 if (keyopinit(kdata->ctx) <= 0)
1463 t->err = "KEYOP_INIT_ERROR";
1468 static void pkey_test_cleanup(EVP_TEST *t)
1470 PKEY_DATA *kdata = t->data;
1472 OPENSSL_free(kdata->input);
1473 OPENSSL_free(kdata->output);
1474 EVP_PKEY_CTX_free(kdata->ctx);
1477 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1483 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1485 p = strchr(tmpval, ':');
1488 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1491 t->err = "PKEY_CTRL_INVALID";
1493 } else if (p != NULL && rv <= 0) {
1494 if (is_digest_disabled(p) || is_cipher_disabled(p)) {
1495 TEST_info("skipping, '%s' is disabled", p);
1499 t->err = "PKEY_CTRL_ERROR";
1503 OPENSSL_free(tmpval);
1507 static int pkey_test_parse(EVP_TEST *t,
1508 const char *keyword, const char *value)
1510 PKEY_DATA *kdata = t->data;
1511 if (strcmp(keyword, "Input") == 0)
1512 return parse_bin(value, &kdata->input, &kdata->input_len);
1513 if (strcmp(keyword, "Output") == 0)
1514 return parse_bin(value, &kdata->output, &kdata->output_len);
1515 if (strcmp(keyword, "Ctrl") == 0)
1516 return pkey_test_ctrl(t, kdata->ctx, value);
1520 static int pkey_test_run(EVP_TEST *t)
1522 PKEY_DATA *expected = t->data;
1523 unsigned char *got = NULL;
1525 EVP_PKEY_CTX *copy = NULL;
1527 if (expected->keyop(expected->ctx, NULL, &got_len,
1528 expected->input, expected->input_len) <= 0
1529 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1530 t->err = "KEYOP_LENGTH_ERROR";
1533 if (expected->keyop(expected->ctx, got, &got_len,
1534 expected->input, expected->input_len) <= 0) {
1535 t->err = "KEYOP_ERROR";
1538 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1539 expected->output, expected->output_len,
1547 /* Repeat the test on a copy. */
1548 if (!TEST_ptr(copy = EVP_PKEY_CTX_dup(expected->ctx))) {
1549 t->err = "INTERNAL_ERROR";
1552 if (expected->keyop(copy, NULL, &got_len, expected->input,
1553 expected->input_len) <= 0
1554 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1555 t->err = "KEYOP_LENGTH_ERROR";
1558 if (expected->keyop(copy, got, &got_len, expected->input,
1559 expected->input_len) <= 0) {
1560 t->err = "KEYOP_ERROR";
1563 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1564 expected->output, expected->output_len,
1570 EVP_PKEY_CTX_free(copy);
1574 static int sign_test_init(EVP_TEST *t, const char *name)
1576 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1579 static const EVP_TEST_METHOD psign_test_method = {
1587 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1589 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1590 EVP_PKEY_verify_recover);
1593 static const EVP_TEST_METHOD pverify_recover_test_method = {
1595 verify_recover_test_init,
1601 static int decrypt_test_init(EVP_TEST *t, const char *name)
1603 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1607 static const EVP_TEST_METHOD pdecrypt_test_method = {
1615 static int verify_test_init(EVP_TEST *t, const char *name)
1617 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1620 static int verify_test_run(EVP_TEST *t)
1622 PKEY_DATA *kdata = t->data;
1624 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1625 kdata->input, kdata->input_len) <= 0)
1626 t->err = "VERIFY_ERROR";
1630 static const EVP_TEST_METHOD pverify_test_method = {
1638 static int pderive_test_init(EVP_TEST *t, const char *name)
1640 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1643 static int pderive_test_parse(EVP_TEST *t,
1644 const char *keyword, const char *value)
1646 PKEY_DATA *kdata = t->data;
1649 if (strcmp(keyword, "PeerKeyValidate") == 0)
1652 if (validate || strcmp(keyword, "PeerKey") == 0) {
1654 if (find_key(&peer, value, public_keys) == 0)
1656 if (EVP_PKEY_derive_set_peer_ex(kdata->ctx, peer, validate) <= 0) {
1657 t->err = "DERIVE_SET_PEER_ERROR";
1663 if (strcmp(keyword, "SharedSecret") == 0)
1664 return parse_bin(value, &kdata->output, &kdata->output_len);
1665 if (strcmp(keyword, "Ctrl") == 0)
1666 return pkey_test_ctrl(t, kdata->ctx, value);
1670 static int pderive_test_run(EVP_TEST *t)
1672 PKEY_DATA *expected = t->data;
1673 unsigned char *got = NULL;
1676 if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) {
1677 t->err = "DERIVE_ERROR";
1680 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1681 t->err = "DERIVE_ERROR";
1684 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
1685 t->err = "DERIVE_ERROR";
1688 if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH",
1689 expected->output, expected->output_len,
1699 static const EVP_TEST_METHOD pderive_test_method = {
1712 typedef enum pbe_type_enum {
1713 PBE_TYPE_INVALID = 0,
1714 PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
1717 typedef struct pbe_data_st {
1719 /* scrypt parameters */
1720 uint64_t N, r, p, maxmem;
1721 /* PKCS#12 parameters */
1725 unsigned char *pass;
1728 unsigned char *salt;
1730 /* Expected output */
1735 #ifndef OPENSSL_NO_SCRYPT
1736 /* Parse unsigned decimal 64 bit integer value */
1737 static int parse_uint64(const char *value, uint64_t *pr)
1739 const char *p = value;
1741 if (!TEST_true(*p)) {
1742 TEST_info("Invalid empty integer value");
1745 for (*pr = 0; *p; ) {
1746 if (*pr > UINT64_MAX / 10) {
1747 TEST_error("Integer overflow in string %s", value);
1751 if (!TEST_true(isdigit((unsigned char)*p))) {
1752 TEST_error("Invalid character in string %s", value);
1761 static int scrypt_test_parse(EVP_TEST *t,
1762 const char *keyword, const char *value)
1764 PBE_DATA *pdata = t->data;
1766 if (strcmp(keyword, "N") == 0)
1767 return parse_uint64(value, &pdata->N);
1768 if (strcmp(keyword, "p") == 0)
1769 return parse_uint64(value, &pdata->p);
1770 if (strcmp(keyword, "r") == 0)
1771 return parse_uint64(value, &pdata->r);
1772 if (strcmp(keyword, "maxmem") == 0)
1773 return parse_uint64(value, &pdata->maxmem);
1778 static int pbkdf2_test_parse(EVP_TEST *t,
1779 const char *keyword, const char *value)
1781 PBE_DATA *pdata = t->data;
1783 if (strcmp(keyword, "iter") == 0) {
1784 pdata->iter = atoi(value);
1785 if (pdata->iter <= 0)
1789 if (strcmp(keyword, "MD") == 0) {
1790 pdata->md = EVP_get_digestbyname(value);
1791 if (pdata->md == NULL)
1798 static int pkcs12_test_parse(EVP_TEST *t,
1799 const char *keyword, const char *value)
1801 PBE_DATA *pdata = t->data;
1803 if (strcmp(keyword, "id") == 0) {
1804 pdata->id = atoi(value);
1809 return pbkdf2_test_parse(t, keyword, value);
1812 static int pbe_test_init(EVP_TEST *t, const char *alg)
1815 PBE_TYPE pbe_type = PBE_TYPE_INVALID;
1817 if (is_kdf_disabled(alg)) {
1818 TEST_info("skipping, '%s' is disabled", alg);
1822 if (strcmp(alg, "scrypt") == 0) {
1823 pbe_type = PBE_TYPE_SCRYPT;
1824 } else if (strcmp(alg, "pbkdf2") == 0) {
1825 pbe_type = PBE_TYPE_PBKDF2;
1826 } else if (strcmp(alg, "pkcs12") == 0) {
1827 pbe_type = PBE_TYPE_PKCS12;
1829 TEST_error("Unknown pbe algorithm %s", alg);
1831 pdat = OPENSSL_zalloc(sizeof(*pdat));
1832 pdat->pbe_type = pbe_type;
1837 static void pbe_test_cleanup(EVP_TEST *t)
1839 PBE_DATA *pdat = t->data;
1841 OPENSSL_free(pdat->pass);
1842 OPENSSL_free(pdat->salt);
1843 OPENSSL_free(pdat->key);
1846 static int pbe_test_parse(EVP_TEST *t,
1847 const char *keyword, const char *value)
1849 PBE_DATA *pdata = t->data;
1851 if (strcmp(keyword, "Password") == 0)
1852 return parse_bin(value, &pdata->pass, &pdata->pass_len);
1853 if (strcmp(keyword, "Salt") == 0)
1854 return parse_bin(value, &pdata->salt, &pdata->salt_len);
1855 if (strcmp(keyword, "Key") == 0)
1856 return parse_bin(value, &pdata->key, &pdata->key_len);
1857 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1858 return pbkdf2_test_parse(t, keyword, value);
1859 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1860 return pkcs12_test_parse(t, keyword, value);
1861 #ifndef OPENSSL_NO_SCRYPT
1862 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1863 return scrypt_test_parse(t, keyword, value);
1868 static int pbe_test_run(EVP_TEST *t)
1870 PBE_DATA *expected = t->data;
1872 EVP_MD *fetched_digest = NULL;
1873 OSSL_LIB_CTX *save_libctx;
1875 save_libctx = OSSL_LIB_CTX_set0_default(libctx);
1877 if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
1878 t->err = "INTERNAL_ERROR";
1881 if (expected->pbe_type == PBE_TYPE_PBKDF2) {
1882 if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
1883 expected->salt, expected->salt_len,
1884 expected->iter, expected->md,
1885 expected->key_len, key) == 0) {
1886 t->err = "PBKDF2_ERROR";
1889 #ifndef OPENSSL_NO_SCRYPT
1890 } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
1891 if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
1892 expected->salt, expected->salt_len,
1893 expected->N, expected->r, expected->p,
1894 expected->maxmem, key, expected->key_len) == 0) {
1895 t->err = "SCRYPT_ERROR";
1899 } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
1900 fetched_digest = EVP_MD_fetch(libctx, EVP_MD_name(expected->md), NULL);
1901 if (fetched_digest == NULL) {
1902 t->err = "PKCS12_ERROR";
1905 if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
1906 expected->salt, expected->salt_len,
1907 expected->id, expected->iter, expected->key_len,
1908 key, fetched_digest) == 0) {
1909 t->err = "PKCS12_ERROR";
1913 if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len,
1914 key, expected->key_len))
1919 EVP_MD_free(fetched_digest);
1921 OSSL_LIB_CTX_set0_default(save_libctx);
1925 static const EVP_TEST_METHOD pbe_test_method = {
1939 BASE64_CANONICAL_ENCODING = 0,
1940 BASE64_VALID_ENCODING = 1,
1941 BASE64_INVALID_ENCODING = 2
1942 } base64_encoding_type;
1944 typedef struct encode_data_st {
1945 /* Input to encoding */
1946 unsigned char *input;
1948 /* Expected output */
1949 unsigned char *output;
1951 base64_encoding_type encoding;
1954 static int encode_test_init(EVP_TEST *t, const char *encoding)
1958 if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
1960 if (strcmp(encoding, "canonical") == 0) {
1961 edata->encoding = BASE64_CANONICAL_ENCODING;
1962 } else if (strcmp(encoding, "valid") == 0) {
1963 edata->encoding = BASE64_VALID_ENCODING;
1964 } else if (strcmp(encoding, "invalid") == 0) {
1965 edata->encoding = BASE64_INVALID_ENCODING;
1966 if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
1969 TEST_error("Bad encoding: %s."
1970 " Should be one of {canonical, valid, invalid}",
1977 OPENSSL_free(edata);
1981 static void encode_test_cleanup(EVP_TEST *t)
1983 ENCODE_DATA *edata = t->data;
1985 OPENSSL_free(edata->input);
1986 OPENSSL_free(edata->output);
1987 memset(edata, 0, sizeof(*edata));
1990 static int encode_test_parse(EVP_TEST *t,
1991 const char *keyword, const char *value)
1993 ENCODE_DATA *edata = t->data;
1995 if (strcmp(keyword, "Input") == 0)
1996 return parse_bin(value, &edata->input, &edata->input_len);
1997 if (strcmp(keyword, "Output") == 0)
1998 return parse_bin(value, &edata->output, &edata->output_len);
2002 static int encode_test_run(EVP_TEST *t)
2004 ENCODE_DATA *expected = t->data;
2005 unsigned char *encode_out = NULL, *decode_out = NULL;
2006 int output_len, chunk_len;
2007 EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL;
2009 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
2010 t->err = "INTERNAL_ERROR";
2014 if (expected->encoding == BASE64_CANONICAL_ENCODING) {
2016 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
2017 || !TEST_ptr(encode_out =
2018 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
2021 EVP_EncodeInit(encode_ctx);
2022 if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
2023 expected->input, expected->input_len)))
2026 output_len = chunk_len;
2028 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
2029 output_len += chunk_len;
2031 if (!memory_err_compare(t, "BAD_ENCODING",
2032 expected->output, expected->output_len,
2033 encode_out, output_len))
2037 if (!TEST_ptr(decode_out =
2038 OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
2041 EVP_DecodeInit(decode_ctx);
2042 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
2043 expected->output_len) < 0) {
2044 t->err = "DECODE_ERROR";
2047 output_len = chunk_len;
2049 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
2050 t->err = "DECODE_ERROR";
2053 output_len += chunk_len;
2055 if (expected->encoding != BASE64_INVALID_ENCODING
2056 && !memory_err_compare(t, "BAD_DECODING",
2057 expected->input, expected->input_len,
2058 decode_out, output_len)) {
2059 t->err = "BAD_DECODING";
2065 OPENSSL_free(encode_out);
2066 OPENSSL_free(decode_out);
2067 EVP_ENCODE_CTX_free(decode_ctx);
2068 EVP_ENCODE_CTX_free(encode_ctx);
2072 static const EVP_TEST_METHOD encode_test_method = {
2075 encode_test_cleanup,
2084 #define MAX_RAND_REPEATS 15
2086 typedef struct rand_data_pass_st {
2087 unsigned char *entropy;
2088 unsigned char *reseed_entropy;
2089 unsigned char *nonce;
2090 unsigned char *pers;
2091 unsigned char *reseed_addin;
2092 unsigned char *addinA;
2093 unsigned char *addinB;
2094 unsigned char *pr_entropyA;
2095 unsigned char *pr_entropyB;
2096 unsigned char *output;
2097 size_t entropy_len, nonce_len, pers_len, addinA_len, addinB_len,
2098 pr_entropyA_len, pr_entropyB_len, output_len, reseed_entropy_len,
2102 typedef struct rand_data_st {
2103 /* Context for this operation */
2105 EVP_RAND_CTX *parent;
2107 int prediction_resistance;
2109 unsigned int generate_bits;
2113 /* Expected output */
2114 RAND_DATA_PASS data[MAX_RAND_REPEATS];
2117 static int rand_test_init(EVP_TEST *t, const char *name)
2121 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
2122 unsigned int strength = 256;
2124 if (!TEST_ptr(rdata = OPENSSL_zalloc(sizeof(*rdata))))
2127 /* TEST-RAND is available in the FIPS provider but not with "fips=yes" */
2128 rand = EVP_RAND_fetch(libctx, "TEST-RAND", "-fips");
2131 rdata->parent = EVP_RAND_CTX_new(rand, NULL);
2132 EVP_RAND_free(rand);
2133 if (rdata->parent == NULL)
2136 *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength);
2137 if (!EVP_RAND_CTX_set_params(rdata->parent, params))
2140 rand = EVP_RAND_fetch(libctx, name, NULL);
2143 rdata->ctx = EVP_RAND_CTX_new(rand, rdata->parent);
2144 EVP_RAND_free(rand);
2145 if (rdata->ctx == NULL)
2152 EVP_RAND_CTX_free(rdata->parent);
2153 OPENSSL_free(rdata);
2157 static void rand_test_cleanup(EVP_TEST *t)
2159 RAND_DATA *rdata = t->data;
2162 OPENSSL_free(rdata->cipher);
2163 OPENSSL_free(rdata->digest);
2165 for (i = 0; i <= rdata->n; i++) {
2166 OPENSSL_free(rdata->data[i].entropy);
2167 OPENSSL_free(rdata->data[i].reseed_entropy);
2168 OPENSSL_free(rdata->data[i].nonce);
2169 OPENSSL_free(rdata->data[i].pers);
2170 OPENSSL_free(rdata->data[i].reseed_addin);
2171 OPENSSL_free(rdata->data[i].addinA);
2172 OPENSSL_free(rdata->data[i].addinB);
2173 OPENSSL_free(rdata->data[i].pr_entropyA);
2174 OPENSSL_free(rdata->data[i].pr_entropyB);
2175 OPENSSL_free(rdata->data[i].output);
2177 EVP_RAND_CTX_free(rdata->ctx);
2178 EVP_RAND_CTX_free(rdata->parent);
2181 static int rand_test_parse(EVP_TEST *t,
2182 const char *keyword, const char *value)
2184 RAND_DATA *rdata = t->data;
2185 RAND_DATA_PASS *item;
2189 if ((p = strchr(keyword, '.')) != NULL) {
2191 if (n >= MAX_RAND_REPEATS)
2195 item = rdata->data + n;
2196 if (strncmp(keyword, "Entropy.", sizeof("Entropy")) == 0)
2197 return parse_bin(value, &item->entropy, &item->entropy_len);
2198 if (strncmp(keyword, "ReseedEntropy.", sizeof("ReseedEntropy")) == 0)
2199 return parse_bin(value, &item->reseed_entropy,
2200 &item->reseed_entropy_len);
2201 if (strncmp(keyword, "Nonce.", sizeof("Nonce")) == 0)
2202 return parse_bin(value, &item->nonce, &item->nonce_len);
2203 if (strncmp(keyword, "PersonalisationString.",
2204 sizeof("PersonalisationString")) == 0)
2205 return parse_bin(value, &item->pers, &item->pers_len);
2206 if (strncmp(keyword, "ReseedAdditionalInput.",
2207 sizeof("ReseedAdditionalInput")) == 0)
2208 return parse_bin(value, &item->reseed_addin,
2209 &item->reseed_addin_len);
2210 if (strncmp(keyword, "AdditionalInputA.",
2211 sizeof("AdditionalInputA")) == 0)
2212 return parse_bin(value, &item->addinA, &item->addinA_len);
2213 if (strncmp(keyword, "AdditionalInputB.",
2214 sizeof("AdditionalInputB")) == 0)
2215 return parse_bin(value, &item->addinB, &item->addinB_len);
2216 if (strncmp(keyword, "EntropyPredictionResistanceA.",
2217 sizeof("EntropyPredictionResistanceA")) == 0)
2218 return parse_bin(value, &item->pr_entropyA, &item->pr_entropyA_len);
2219 if (strncmp(keyword, "EntropyPredictionResistanceB.",
2220 sizeof("EntropyPredictionResistanceB")) == 0)
2221 return parse_bin(value, &item->pr_entropyB, &item->pr_entropyB_len);
2222 if (strncmp(keyword, "Output.", sizeof("Output")) == 0)
2223 return parse_bin(value, &item->output, &item->output_len);
2225 if (strcmp(keyword, "Cipher") == 0)
2226 return TEST_ptr(rdata->cipher = OPENSSL_strdup(value));
2227 if (strcmp(keyword, "Digest") == 0)
2228 return TEST_ptr(rdata->digest = OPENSSL_strdup(value));
2229 if (strcmp(keyword, "DerivationFunction") == 0) {
2230 rdata->use_df = atoi(value) != 0;
2233 if (strcmp(keyword, "GenerateBits") == 0) {
2234 if ((n = atoi(value)) <= 0 || n % 8 != 0)
2236 rdata->generate_bits = (unsigned int)n;
2239 if (strcmp(keyword, "PredictionResistance") == 0) {
2240 rdata->prediction_resistance = atoi(value) != 0;
2247 static int rand_test_run(EVP_TEST *t)
2249 RAND_DATA *expected = t->data;
2250 RAND_DATA_PASS *item;
2252 size_t got_len = expected->generate_bits / 8;
2253 OSSL_PARAM params[5], *p = params;
2254 int i = -1, ret = 0;
2255 unsigned int strength;
2258 if (!TEST_ptr(got = OPENSSL_malloc(got_len)))
2261 *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &expected->use_df);
2262 if (expected->cipher != NULL)
2263 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
2264 expected->cipher, 0);
2265 if (expected->digest != NULL)
2266 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
2267 expected->digest, 0);
2268 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC, "HMAC", 0);
2269 *p = OSSL_PARAM_construct_end();
2270 if (!TEST_true(EVP_RAND_CTX_set_params(expected->ctx, params)))
2273 strength = EVP_RAND_strength(expected->ctx);
2274 for (i = 0; i <= expected->n; i++) {
2275 item = expected->data + i;
2278 z = item->entropy != NULL ? item->entropy : (unsigned char *)"";
2279 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
2280 z, item->entropy_len);
2281 z = item->nonce != NULL ? item->nonce : (unsigned char *)"";
2282 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
2283 z, item->nonce_len);
2284 *p = OSSL_PARAM_construct_end();
2285 if (!TEST_true(EVP_RAND_instantiate(expected->parent, strength,
2286 0, NULL, 0, params)))
2289 z = item->pers != NULL ? item->pers : (unsigned char *)"";
2290 if (!TEST_true(EVP_RAND_instantiate
2291 (expected->ctx, strength,
2292 expected->prediction_resistance, z,
2293 item->pers_len, NULL)))
2296 if (item->reseed_entropy != NULL) {
2297 params[0] = OSSL_PARAM_construct_octet_string
2298 (OSSL_RAND_PARAM_TEST_ENTROPY, item->reseed_entropy,
2299 item->reseed_entropy_len);
2300 params[1] = OSSL_PARAM_construct_end();
2301 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2304 if (!TEST_true(EVP_RAND_reseed
2305 (expected->ctx, expected->prediction_resistance,
2306 NULL, 0, item->reseed_addin,
2307 item->reseed_addin_len)))
2310 if (item->pr_entropyA != NULL) {
2311 params[0] = OSSL_PARAM_construct_octet_string
2312 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyA,
2313 item->pr_entropyA_len);
2314 params[1] = OSSL_PARAM_construct_end();
2315 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2318 if (!TEST_true(EVP_RAND_generate
2319 (expected->ctx, got, got_len,
2320 strength, expected->prediction_resistance,
2321 item->addinA, item->addinA_len)))
2324 if (item->pr_entropyB != NULL) {
2325 params[0] = OSSL_PARAM_construct_octet_string
2326 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyB,
2327 item->pr_entropyB_len);
2328 params[1] = OSSL_PARAM_construct_end();
2329 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2332 if (!TEST_true(EVP_RAND_generate
2333 (expected->ctx, got, got_len,
2334 strength, expected->prediction_resistance,
2335 item->addinB, item->addinB_len)))
2337 if (!TEST_mem_eq(got, got_len, item->output, item->output_len))
2339 if (!TEST_true(EVP_RAND_uninstantiate(expected->ctx))
2340 || !TEST_true(EVP_RAND_uninstantiate(expected->parent))
2341 || !TEST_true(EVP_RAND_verify_zeroization(expected->ctx))
2342 || !TEST_int_eq(EVP_RAND_state(expected->ctx),
2343 EVP_RAND_STATE_UNINITIALISED))
2350 if (ret == 0 && i >= 0)
2351 TEST_info("Error in test case %d of %d\n", i, expected->n + 1);
2356 static const EVP_TEST_METHOD rand_test_method = {
2368 typedef struct kdf_data_st {
2369 /* Context for this operation */
2371 /* Expected output */
2372 unsigned char *output;
2374 OSSL_PARAM params[20];
2379 * Perform public key operation setup: lookup key, allocated ctx and call
2380 * the appropriate initialisation function
2382 static int kdf_test_init(EVP_TEST *t, const char *name)
2387 if (is_kdf_disabled(name)) {
2388 TEST_info("skipping, '%s' is disabled", name);
2393 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2395 kdata->p = kdata->params;
2396 *kdata->p = OSSL_PARAM_construct_end();
2398 kdf = EVP_KDF_fetch(libctx, name, NULL);
2400 OPENSSL_free(kdata);
2403 kdata->ctx = EVP_KDF_CTX_new(kdf);
2405 if (kdata->ctx == NULL) {
2406 OPENSSL_free(kdata);
2413 static void kdf_test_cleanup(EVP_TEST *t)
2415 KDF_DATA *kdata = t->data;
2418 for (p = kdata->params; p->key != NULL; p++)
2419 OPENSSL_free(p->data);
2420 OPENSSL_free(kdata->output);
2421 EVP_KDF_CTX_free(kdata->ctx);
2424 static int kdf_test_ctrl(EVP_TEST *t, EVP_KDF_CTX *kctx,
2427 KDF_DATA *kdata = t->data;
2430 const OSSL_PARAM *defs = EVP_KDF_settable_ctx_params(EVP_KDF_CTX_kdf(kctx));
2432 if (!TEST_ptr(name = OPENSSL_strdup(value)))
2434 p = strchr(name, ':');
2438 rv = OSSL_PARAM_allocate_from_text(kdata->p, defs, name, p,
2439 p != NULL ? strlen(p) : 0, NULL);
2440 *++kdata->p = OSSL_PARAM_construct_end();
2442 t->err = "KDF_PARAM_ERROR";
2446 if (p != NULL && strcmp(name, "digest") == 0) {
2447 if (is_digest_disabled(p)) {
2448 TEST_info("skipping, '%s' is disabled", p);
2453 && (strcmp(name, "cipher") == 0
2454 || strcmp(name, "cekalg") == 0)
2455 && is_cipher_disabled(p)) {
2456 TEST_info("skipping, '%s' is disabled", p);
2463 static int kdf_test_parse(EVP_TEST *t,
2464 const char *keyword, const char *value)
2466 KDF_DATA *kdata = t->data;
2468 if (strcmp(keyword, "Output") == 0)
2469 return parse_bin(value, &kdata->output, &kdata->output_len);
2470 if (strncmp(keyword, "Ctrl", 4) == 0)
2471 return kdf_test_ctrl(t, kdata->ctx, value);
2475 static int kdf_test_run(EVP_TEST *t)
2477 KDF_DATA *expected = t->data;
2478 unsigned char *got = NULL;
2479 size_t got_len = expected->output_len;
2481 if (!EVP_KDF_CTX_set_params(expected->ctx, expected->params)) {
2482 t->err = "KDF_CTRL_ERROR";
2485 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2486 t->err = "INTERNAL_ERROR";
2489 if (EVP_KDF_derive(expected->ctx, got, got_len, NULL) <= 0) {
2490 t->err = "KDF_DERIVE_ERROR";
2493 if (!memory_err_compare(t, "KDF_MISMATCH",
2494 expected->output, expected->output_len,
2505 static const EVP_TEST_METHOD kdf_test_method = {
2517 typedef struct pkey_kdf_data_st {
2518 /* Context for this operation */
2520 /* Expected output */
2521 unsigned char *output;
2526 * Perform public key operation setup: lookup key, allocated ctx and call
2527 * the appropriate initialisation function
2529 static int pkey_kdf_test_init(EVP_TEST *t, const char *name)
2531 PKEY_KDF_DATA *kdata = NULL;
2533 if (is_kdf_disabled(name)) {
2534 TEST_info("skipping, '%s' is disabled", name);
2539 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2542 kdata->ctx = EVP_PKEY_CTX_new_from_name(libctx, name, NULL);
2543 if (kdata->ctx == NULL
2544 || EVP_PKEY_derive_init(kdata->ctx) <= 0)
2550 EVP_PKEY_CTX_free(kdata->ctx);
2551 OPENSSL_free(kdata);
2555 static void pkey_kdf_test_cleanup(EVP_TEST *t)
2557 PKEY_KDF_DATA *kdata = t->data;
2559 OPENSSL_free(kdata->output);
2560 EVP_PKEY_CTX_free(kdata->ctx);
2563 static int pkey_kdf_test_parse(EVP_TEST *t,
2564 const char *keyword, const char *value)
2566 PKEY_KDF_DATA *kdata = t->data;
2568 if (strcmp(keyword, "Output") == 0)
2569 return parse_bin(value, &kdata->output, &kdata->output_len);
2570 if (strncmp(keyword, "Ctrl", 4) == 0)
2571 return pkey_test_ctrl(t, kdata->ctx, value);
2575 static int pkey_kdf_test_run(EVP_TEST *t)
2577 PKEY_KDF_DATA *expected = t->data;
2578 unsigned char *got = NULL;
2579 size_t got_len = expected->output_len;
2581 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2582 t->err = "INTERNAL_ERROR";
2585 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
2586 t->err = "KDF_DERIVE_ERROR";
2589 if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
2590 t->err = "KDF_MISMATCH";
2600 static const EVP_TEST_METHOD pkey_kdf_test_method = {
2603 pkey_kdf_test_cleanup,
2604 pkey_kdf_test_parse,
2612 typedef struct keypair_test_data_st {
2615 } KEYPAIR_TEST_DATA;
2617 static int keypair_test_init(EVP_TEST *t, const char *pair)
2619 KEYPAIR_TEST_DATA *data;
2621 EVP_PKEY *pk = NULL, *pubk = NULL;
2622 char *pub, *priv = NULL;
2624 /* Split private and public names. */
2625 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
2626 || !TEST_ptr(pub = strchr(priv, ':'))) {
2627 t->err = "PARSING_ERROR";
2632 if (!TEST_true(find_key(&pk, priv, private_keys))) {
2633 TEST_info("Can't find private key: %s", priv);
2634 t->err = "MISSING_PRIVATE_KEY";
2637 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
2638 TEST_info("Can't find public key: %s", pub);
2639 t->err = "MISSING_PUBLIC_KEY";
2643 if (pk == NULL && pubk == NULL) {
2644 /* Both keys are listed but unsupported: skip this test */
2650 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2663 static void keypair_test_cleanup(EVP_TEST *t)
2665 OPENSSL_free(t->data);
2670 * For tests that do not accept any custom keywords.
2672 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
2677 static int keypair_test_run(EVP_TEST *t)
2680 const KEYPAIR_TEST_DATA *pair = t->data;
2682 if (pair->privk == NULL || pair->pubk == NULL) {
2684 * this can only happen if only one of the keys is not set
2685 * which means that one of them was unsupported while the
2686 * other isn't: hence a key type mismatch.
2688 t->err = "KEYPAIR_TYPE_MISMATCH";
2693 if ((rv = EVP_PKEY_eq(pair->privk, pair->pubk)) != 1 ) {
2695 t->err = "KEYPAIR_MISMATCH";
2696 } else if ( -1 == rv ) {
2697 t->err = "KEYPAIR_TYPE_MISMATCH";
2698 } else if ( -2 == rv ) {
2699 t->err = "UNSUPPORTED_KEY_COMPARISON";
2701 TEST_error("Unexpected error in key comparison");
2716 static const EVP_TEST_METHOD keypair_test_method = {
2719 keypair_test_cleanup,
2728 typedef struct keygen_test_data_st {
2729 EVP_PKEY_CTX *genctx; /* Keygen context to use */
2730 char *keyname; /* Key name to store key or NULL */
2733 static int keygen_test_init(EVP_TEST *t, const char *alg)
2735 KEYGEN_TEST_DATA *data;
2736 EVP_PKEY_CTX *genctx;
2737 int nid = OBJ_sn2nid(alg);
2739 if (nid == NID_undef) {
2740 nid = OBJ_ln2nid(alg);
2741 if (nid == NID_undef)
2745 if (is_pkey_disabled(alg)) {
2749 if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_from_name(libctx, alg, NULL)))
2752 if (EVP_PKEY_keygen_init(genctx) <= 0) {
2753 t->err = "KEYGEN_INIT_ERROR";
2757 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2759 data->genctx = genctx;
2760 data->keyname = NULL;
2766 EVP_PKEY_CTX_free(genctx);
2770 static void keygen_test_cleanup(EVP_TEST *t)
2772 KEYGEN_TEST_DATA *keygen = t->data;
2774 EVP_PKEY_CTX_free(keygen->genctx);
2775 OPENSSL_free(keygen->keyname);
2776 OPENSSL_free(t->data);
2780 static int keygen_test_parse(EVP_TEST *t,
2781 const char *keyword, const char *value)
2783 KEYGEN_TEST_DATA *keygen = t->data;
2785 if (strcmp(keyword, "KeyName") == 0)
2786 return TEST_ptr(keygen->keyname = OPENSSL_strdup(value));
2787 if (strcmp(keyword, "Ctrl") == 0)
2788 return pkey_test_ctrl(t, keygen->genctx, value);
2792 static int keygen_test_run(EVP_TEST *t)
2794 KEYGEN_TEST_DATA *keygen = t->data;
2795 EVP_PKEY *pkey = NULL;
2798 if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) {
2799 t->err = "KEYGEN_GENERATE_ERROR";
2803 if (!evp_pkey_is_provided(pkey)) {
2804 TEST_info("Warning: legacy key generated %s", keygen->keyname);
2807 if (keygen->keyname != NULL) {
2811 if (find_key(NULL, keygen->keyname, private_keys)) {
2812 TEST_info("Duplicate key %s", keygen->keyname);
2816 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
2818 key->name = keygen->keyname;
2819 keygen->keyname = NULL;
2821 key->next = private_keys;
2825 EVP_PKEY_free(pkey);
2834 static const EVP_TEST_METHOD keygen_test_method = {
2837 keygen_test_cleanup,
2843 ** DIGEST SIGN+VERIFY TESTS
2847 int is_verify; /* Set to 1 if verifying */
2848 int is_oneshot; /* Set to 1 for one shot operation */
2849 const EVP_MD *md; /* Digest to use */
2850 EVP_MD_CTX *ctx; /* Digest context */
2852 STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
2853 unsigned char *osin; /* Input data if one shot */
2854 size_t osin_len; /* Input length data if one shot */
2855 unsigned char *output; /* Expected output */
2856 size_t output_len; /* Expected output length */
2859 static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
2862 const EVP_MD *md = NULL;
2863 DIGESTSIGN_DATA *mdat;
2865 if (strcmp(alg, "NULL") != 0) {
2866 if (is_digest_disabled(alg)) {
2870 md = EVP_get_digestbyname(alg);
2874 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
2877 if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
2881 mdat->is_verify = is_verify;
2882 mdat->is_oneshot = is_oneshot;
2887 static int digestsign_test_init(EVP_TEST *t, const char *alg)
2889 return digestsigver_test_init(t, alg, 0, 0);
2892 static void digestsigver_test_cleanup(EVP_TEST *t)
2894 DIGESTSIGN_DATA *mdata = t->data;
2896 EVP_MD_CTX_free(mdata->ctx);
2897 sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
2898 OPENSSL_free(mdata->osin);
2899 OPENSSL_free(mdata->output);
2900 OPENSSL_free(mdata);
2904 static int digestsigver_test_parse(EVP_TEST *t,
2905 const char *keyword, const char *value)
2907 DIGESTSIGN_DATA *mdata = t->data;
2909 if (strcmp(keyword, "Key") == 0) {
2910 EVP_PKEY *pkey = NULL;
2912 const char *name = mdata->md == NULL ? NULL : EVP_MD_name(mdata->md);
2914 if (mdata->is_verify)
2915 rv = find_key(&pkey, value, public_keys);
2917 rv = find_key(&pkey, value, private_keys);
2918 if (rv == 0 || pkey == NULL) {
2922 if (mdata->is_verify) {
2923 if (!EVP_DigestVerifyInit_ex(mdata->ctx, &mdata->pctx, name, libctx,
2925 t->err = "DIGESTVERIFYINIT_ERROR";
2928 if (!EVP_DigestSignInit_ex(mdata->ctx, &mdata->pctx, name, libctx, NULL,
2930 t->err = "DIGESTSIGNINIT_ERROR";
2934 if (strcmp(keyword, "Input") == 0) {
2935 if (mdata->is_oneshot)
2936 return parse_bin(value, &mdata->osin, &mdata->osin_len);
2937 return evp_test_buffer_append(value, &mdata->input);
2939 if (strcmp(keyword, "Output") == 0)
2940 return parse_bin(value, &mdata->output, &mdata->output_len);
2942 if (!mdata->is_oneshot) {
2943 if (strcmp(keyword, "Count") == 0)
2944 return evp_test_buffer_set_count(value, mdata->input);
2945 if (strcmp(keyword, "Ncopy") == 0)
2946 return evp_test_buffer_ncopy(value, mdata->input);
2948 if (strcmp(keyword, "Ctrl") == 0) {
2949 if (mdata->pctx == NULL)
2951 return pkey_test_ctrl(t, mdata->pctx, value);
2956 static int digestsign_update_fn(void *ctx, const unsigned char *buf,
2959 return EVP_DigestSignUpdate(ctx, buf, buflen);
2962 static int digestsign_test_run(EVP_TEST *t)
2964 DIGESTSIGN_DATA *expected = t->data;
2965 unsigned char *got = NULL;
2968 if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
2970 t->err = "DIGESTUPDATE_ERROR";
2974 if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
2975 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
2978 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2979 t->err = "MALLOC_FAILURE";
2982 if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
2983 t->err = "DIGESTSIGNFINAL_ERROR";
2986 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
2987 expected->output, expected->output_len,
2997 static const EVP_TEST_METHOD digestsign_test_method = {
2999 digestsign_test_init,
3000 digestsigver_test_cleanup,
3001 digestsigver_test_parse,
3005 static int digestverify_test_init(EVP_TEST *t, const char *alg)
3007 return digestsigver_test_init(t, alg, 1, 0);
3010 static int digestverify_update_fn(void *ctx, const unsigned char *buf,
3013 return EVP_DigestVerifyUpdate(ctx, buf, buflen);
3016 static int digestverify_test_run(EVP_TEST *t)
3018 DIGESTSIGN_DATA *mdata = t->data;
3020 if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
3021 t->err = "DIGESTUPDATE_ERROR";
3025 if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
3026 mdata->output_len) <= 0)
3027 t->err = "VERIFY_ERROR";
3031 static const EVP_TEST_METHOD digestverify_test_method = {
3033 digestverify_test_init,
3034 digestsigver_test_cleanup,
3035 digestsigver_test_parse,
3036 digestverify_test_run
3039 static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
3041 return digestsigver_test_init(t, alg, 0, 1);
3044 static int oneshot_digestsign_test_run(EVP_TEST *t)
3046 DIGESTSIGN_DATA *expected = t->data;
3047 unsigned char *got = NULL;
3050 if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
3051 expected->osin, expected->osin_len)) {
3052 t->err = "DIGESTSIGN_LENGTH_ERROR";
3055 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
3056 t->err = "MALLOC_FAILURE";
3059 if (!EVP_DigestSign(expected->ctx, got, &got_len,
3060 expected->osin, expected->osin_len)) {
3061 t->err = "DIGESTSIGN_ERROR";
3064 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
3065 expected->output, expected->output_len,
3075 static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
3076 "OneShotDigestSign",
3077 oneshot_digestsign_test_init,
3078 digestsigver_test_cleanup,
3079 digestsigver_test_parse,
3080 oneshot_digestsign_test_run
3083 static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
3085 return digestsigver_test_init(t, alg, 1, 1);
3088 static int oneshot_digestverify_test_run(EVP_TEST *t)
3090 DIGESTSIGN_DATA *mdata = t->data;
3092 if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
3093 mdata->osin, mdata->osin_len) <= 0)
3094 t->err = "VERIFY_ERROR";
3098 static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
3099 "OneShotDigestVerify",
3100 oneshot_digestverify_test_init,
3101 digestsigver_test_cleanup,
3102 digestsigver_test_parse,
3103 oneshot_digestverify_test_run
3108 ** PARSING AND DISPATCH
3111 static const EVP_TEST_METHOD *evp_test_list[] = {
3113 &cipher_test_method,
3114 &digest_test_method,
3115 &digestsign_test_method,
3116 &digestverify_test_method,
3117 &encode_test_method,
3119 &pkey_kdf_test_method,
3120 &keypair_test_method,
3121 &keygen_test_method,
3123 &oneshot_digestsign_test_method,
3124 &oneshot_digestverify_test_method,
3126 &pdecrypt_test_method,
3127 &pderive_test_method,
3129 &pverify_recover_test_method,
3130 &pverify_test_method,
3134 static const EVP_TEST_METHOD *find_test(const char *name)
3136 const EVP_TEST_METHOD **tt;
3138 for (tt = evp_test_list; *tt; tt++) {
3139 if (strcmp(name, (*tt)->name) == 0)
3145 static void clear_test(EVP_TEST *t)
3147 test_clearstanza(&t->s);
3149 if (t->data != NULL) {
3150 if (t->meth != NULL)
3151 t->meth->cleanup(t);
3152 OPENSSL_free(t->data);
3155 OPENSSL_free(t->expected_err);
3156 t->expected_err = NULL;
3157 OPENSSL_free(t->reason);
3166 /* Check for errors in the test structure; return 1 if okay, else 0. */
3167 static int check_test_error(EVP_TEST *t)
3172 if (t->err == NULL && t->expected_err == NULL)
3174 if (t->err != NULL && t->expected_err == NULL) {
3175 if (t->aux_err != NULL) {
3176 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
3177 t->s.test_file, t->s.start, t->aux_err, t->err);
3179 TEST_info("%s:%d: Source of above error; unexpected error %s",
3180 t->s.test_file, t->s.start, t->err);
3184 if (t->err == NULL && t->expected_err != NULL) {
3185 TEST_info("%s:%d: Succeeded but was expecting %s",
3186 t->s.test_file, t->s.start, t->expected_err);
3190 if (strcmp(t->err, t->expected_err) != 0) {
3191 TEST_info("%s:%d: Expected %s got %s",
3192 t->s.test_file, t->s.start, t->expected_err, t->err);
3196 if (t->reason == NULL)
3199 if (t->reason == NULL) {
3200 TEST_info("%s:%d: Test is missing function or reason code",
3201 t->s.test_file, t->s.start);
3205 err = ERR_peek_error();
3207 TEST_info("%s:%d: Expected error \"%s\" not set",
3208 t->s.test_file, t->s.start, t->reason);
3212 reason = ERR_reason_error_string(err);
3213 if (reason == NULL) {
3214 TEST_info("%s:%d: Expected error \"%s\", no strings available."
3216 t->s.test_file, t->s.start, t->reason);
3220 if (strcmp(reason, t->reason) == 0)
3223 TEST_info("%s:%d: Expected error \"%s\", got \"%s\"",
3224 t->s.test_file, t->s.start, t->reason, reason);
3229 /* Run a parsed test. Log a message and return 0 on error. */
3230 static int run_test(EVP_TEST *t)
3232 if (t->meth == NULL)
3239 if (t->err == NULL && t->meth->run_test(t) != 1) {
3240 TEST_info("%s:%d %s error",
3241 t->s.test_file, t->s.start, t->meth->name);
3244 if (!check_test_error(t)) {
3245 TEST_openssl_errors();
3254 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
3256 for (; lst != NULL; lst = lst->next) {
3257 if (strcmp(lst->name, name) == 0) {
3266 static void free_key_list(KEY_LIST *lst)
3268 while (lst != NULL) {
3269 KEY_LIST *next = lst->next;
3271 EVP_PKEY_free(lst->key);
3272 OPENSSL_free(lst->name);
3279 * Is the key type an unsupported algorithm?
3281 static int key_unsupported(void)
3283 long err = ERR_peek_last_error();
3284 int lib = ERR_GET_LIB(err);
3285 long reason = ERR_GET_REASON(err);
3287 if ((lib == ERR_LIB_EVP && reason == EVP_R_UNSUPPORTED_ALGORITHM)
3288 || reason == ERR_R_UNSUPPORTED) {
3292 #ifndef OPENSSL_NO_EC
3294 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
3295 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
3298 if (lib == ERR_LIB_EC
3299 && (reason == EC_R_UNKNOWN_GROUP
3300 || reason == EC_R_INVALID_CURVE)) {
3304 #endif /* OPENSSL_NO_EC */
3308 /* NULL out the value from |pp| but return it. This "steals" a pointer. */
3309 static char *take_value(PAIR *pp)
3311 char *p = pp->value;
3317 #if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
3318 static int securitycheck_enabled(void)
3320 static int enabled = -1;
3322 if (enabled == -1) {
3323 if (OSSL_PROVIDER_available(libctx, "fips")) {
3324 OSSL_PARAM params[2];
3325 OSSL_PROVIDER *prov = NULL;
3328 prov = OSSL_PROVIDER_load(libctx, "fips");
3331 OSSL_PARAM_construct_int(OSSL_PROV_PARAM_SECURITY_CHECKS,
3333 params[1] = OSSL_PARAM_construct_end();
3334 OSSL_PROVIDER_get_params(prov, params);
3335 OSSL_PROVIDER_unload(prov);
3347 * Return 1 if one of the providers named in the string is available.
3348 * The provider names are separated with whitespace.
3349 * NOTE: destructive function, it inserts '\0' after each provider name.
3351 static int prov_available(char *providers)
3357 for (; isspace(*providers); providers++)
3359 if (*providers == '\0')
3360 break; /* End of the road */
3361 for (p = providers; *p != '\0' && !isspace(*p); p++)
3367 if (OSSL_PROVIDER_available(libctx, providers))
3368 return 1; /* Found one */
3373 /* Read and parse one test. Return 0 if failure, 1 if okay. */
3374 static int parse(EVP_TEST *t)
3376 KEY_LIST *key, **klist;
3379 int i, skip_availablein = 0;
3383 if (BIO_eof(t->s.fp))
3386 if (!test_readstanza(&t->s))
3388 } while (t->s.numpairs == 0);
3389 pp = &t->s.pairs[0];
3391 /* Are we adding a key? */
3395 if (strcmp(pp->key, "PrivateKey") == 0) {
3396 pkey = PEM_read_bio_PrivateKey_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3397 if (pkey == NULL && !key_unsupported()) {
3398 EVP_PKEY_free(pkey);
3399 TEST_info("Can't read private key %s", pp->value);
3400 TEST_openssl_errors();
3403 klist = &private_keys;
3404 } else if (strcmp(pp->key, "PublicKey") == 0) {
3405 pkey = PEM_read_bio_PUBKEY_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3406 if (pkey == NULL && !key_unsupported()) {
3407 EVP_PKEY_free(pkey);
3408 TEST_info("Can't read public key %s", pp->value);
3409 TEST_openssl_errors();
3412 klist = &public_keys;
3413 } else if (strcmp(pp->key, "PrivateKeyRaw") == 0
3414 || strcmp(pp->key, "PublicKeyRaw") == 0 ) {
3415 char *strnid = NULL, *keydata = NULL;
3416 unsigned char *keybin;
3420 if (strcmp(pp->key, "PrivateKeyRaw") == 0)
3421 klist = &private_keys;
3423 klist = &public_keys;
3425 strnid = strchr(pp->value, ':');
3426 if (strnid != NULL) {
3428 keydata = strchr(strnid, ':');
3429 if (keydata != NULL)
3432 if (keydata == NULL) {
3433 TEST_info("Failed to parse %s value", pp->key);
3437 nid = OBJ_txt2nid(strnid);
3438 if (nid == NID_undef) {
3439 TEST_info("Unrecognised algorithm NID");
3442 if (!parse_bin(keydata, &keybin, &keylen)) {
3443 TEST_info("Failed to create binary key");
3446 if (klist == &private_keys)
3447 pkey = EVP_PKEY_new_raw_private_key_ex(libctx, strnid, NULL, keybin,
3450 pkey = EVP_PKEY_new_raw_public_key_ex(libctx, strnid, NULL, keybin,
3452 if (pkey == NULL && !key_unsupported()) {
3453 TEST_info("Can't read %s data", pp->key);
3454 OPENSSL_free(keybin);
3455 TEST_openssl_errors();
3458 OPENSSL_free(keybin);
3459 } else if (strcmp(pp->key, "Availablein") == 0) {
3460 if (!prov_available(pp->value)) {
3461 TEST_info("skipping, '%s' provider not available: %s:%d",
3462 pp->value, t->s.test_file, t->s.start);
3471 /* If we have a key add to list */
3472 if (klist != NULL) {
3473 if (find_key(NULL, pp->value, *klist)) {
3474 TEST_info("Duplicate key %s", pp->value);
3477 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
3479 key->name = take_value(pp);
3484 /* Go back and start a new stanza. */
3485 if ((t->s.numpairs - skip_availablein) != 1)
3486 TEST_info("Line %d: missing blank line\n", t->s.curr);
3490 /* Find the test, based on first keyword. */
3491 if (!TEST_ptr(t->meth = find_test(pp->key)))
3493 if (!t->meth->init(t, pp->value)) {
3494 TEST_error("unknown %s: %s\n", pp->key, pp->value);
3498 /* TEST_info("skipping %s %s", pp->key, pp->value); */
3502 for (pp++, i = 1; i < (t->s.numpairs - skip_availablein); pp++, i++) {
3503 if (strcmp(pp->key, "Securitycheck") == 0) {
3504 #if defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
3506 if (!securitycheck_enabled())
3509 TEST_info("skipping, Securitycheck is disabled: %s:%d",
3510 t->s.test_file, t->s.start);
3514 } else if (strcmp(pp->key, "Availablein") == 0) {
3515 TEST_info("Line %d: 'Availablein' should be the first option",
3518 } else if (strcmp(pp->key, "Result") == 0) {
3519 if (t->expected_err != NULL) {
3520 TEST_info("Line %d: multiple result lines", t->s.curr);
3523 t->expected_err = take_value(pp);
3524 } else if (strcmp(pp->key, "Function") == 0) {
3525 /* Ignore old line. */
3526 } else if (strcmp(pp->key, "Reason") == 0) {
3527 if (t->reason != NULL) {
3528 TEST_info("Line %d: multiple reason lines", t->s.curr);
3531 t->reason = take_value(pp);
3533 /* Must be test specific line: try to parse it */
3534 int rv = t->meth->parse(t, pp->key, pp->value);
3537 TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key);
3541 TEST_info("Line %d: error processing keyword %s = %s\n",
3542 t->s.curr, pp->key, pp->value);
3551 static int run_file_tests(int i)
3554 const char *testfile = test_get_argument(i);
3557 if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t))))
3559 if (!test_start_file(&t->s, testfile)) {
3564 while (!BIO_eof(t->s.fp)) {
3570 if (c == 0 || !run_test(t)) {
3575 test_end_file(&t->s);
3578 free_key_list(public_keys);
3579 free_key_list(private_keys);
3586 const OPTIONS *test_get_options(void)
3588 static const OPTIONS test_options[] = {
3589 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
3590 { "config", OPT_CONFIG_FILE, '<',
3591 "The configuration file to use for the libctx" },
3592 { OPT_HELP_STR, 1, '-', "file\tFile to run tests on.\n" },
3595 return test_options;
3598 int setup_tests(void)
3601 char *config_file = NULL;
3605 while ((o = opt_next()) != OPT_EOF) {
3607 case OPT_CONFIG_FILE:
3608 config_file = opt_arg();
3610 case OPT_TEST_CASES:
3619 * Load the provider via configuration into the created library context.
3620 * Load the 'null' provider into the default library context to ensure that
3621 * the tests do not fallback to using the default provider.
3623 if (!test_get_libctx(&libctx, &prov_null, config_file, NULL, NULL))
3626 n = test_get_argument_count();
3630 ADD_ALL_TESTS(run_file_tests, n);
3634 void cleanup_tests(void)
3636 OSSL_PROVIDER_unload(prov_null);
3637 OSSL_LIB_CTX_free(libctx);
3640 #define STR_STARTS_WITH(str, pre) strncasecmp(pre, str, strlen(pre)) == 0
3641 #define STR_ENDS_WITH(str, pre) \
3642 strlen(str) < strlen(pre) ? 0 : (strcasecmp(pre, str + strlen(str) - strlen(pre)) == 0)
3644 static int is_digest_disabled(const char *name)
3646 #ifdef OPENSSL_NO_BLAKE2
3647 if (STR_STARTS_WITH(name, "BLAKE"))
3650 #ifdef OPENSSL_NO_MD2
3651 if (strcasecmp(name, "MD2") == 0)
3654 #ifdef OPENSSL_NO_MDC2
3655 if (strcasecmp(name, "MDC2") == 0)
3658 #ifdef OPENSSL_NO_MD4
3659 if (strcasecmp(name, "MD4") == 0)
3662 #ifdef OPENSSL_NO_MD5
3663 if (strcasecmp(name, "MD5") == 0)
3666 #ifdef OPENSSL_NO_RMD160
3667 if (strcasecmp(name, "RIPEMD160") == 0)
3670 #ifdef OPENSSL_NO_SM3
3671 if (strcasecmp(name, "SM3") == 0)
3674 #ifdef OPENSSL_NO_WHIRLPOOL
3675 if (strcasecmp(name, "WHIRLPOOL") == 0)
3681 static int is_pkey_disabled(const char *name)
3683 #ifdef OPENSSL_NO_EC
3684 if (STR_STARTS_WITH(name, "EC"))
3687 #ifdef OPENSSL_NO_DH
3688 if (STR_STARTS_WITH(name, "DH"))
3691 #ifdef OPENSSL_NO_DSA
3692 if (STR_STARTS_WITH(name, "DSA"))
3698 static int is_mac_disabled(const char *name)
3700 #ifdef OPENSSL_NO_BLAKE2
3701 if (STR_STARTS_WITH(name, "BLAKE2BMAC")
3702 || STR_STARTS_WITH(name, "BLAKE2SMAC"))
3705 #ifdef OPENSSL_NO_CMAC
3706 if (STR_STARTS_WITH(name, "CMAC"))
3709 #ifdef OPENSSL_NO_POLY1305
3710 if (STR_STARTS_WITH(name, "Poly1305"))
3713 #ifdef OPENSSL_NO_SIPHASH
3714 if (STR_STARTS_WITH(name, "SipHash"))
3719 static int is_kdf_disabled(const char *name)
3721 #ifdef OPENSSL_NO_SCRYPT
3722 if (STR_ENDS_WITH(name, "SCRYPT"))
3728 static int is_cipher_disabled(const char *name)
3730 #ifdef OPENSSL_NO_ARIA
3731 if (STR_STARTS_WITH(name, "ARIA"))
3734 #ifdef OPENSSL_NO_BF
3735 if (STR_STARTS_WITH(name, "BF"))
3738 #ifdef OPENSSL_NO_CAMELLIA
3739 if (STR_STARTS_WITH(name, "CAMELLIA"))
3742 #ifdef OPENSSL_NO_CAST
3743 if (STR_STARTS_WITH(name, "CAST"))
3746 #ifdef OPENSSL_NO_CHACHA
3747 if (STR_STARTS_WITH(name, "CHACHA"))
3750 #ifdef OPENSSL_NO_POLY1305
3751 if (STR_ENDS_WITH(name, "Poly1305"))
3754 #ifdef OPENSSL_NO_DES
3755 if (STR_STARTS_WITH(name, "DES"))
3757 if (STR_ENDS_WITH(name, "3DESwrap"))
3760 #ifdef OPENSSL_NO_OCB
3761 if (STR_ENDS_WITH(name, "OCB"))
3764 #ifdef OPENSSL_NO_IDEA
3765 if (STR_STARTS_WITH(name, "IDEA"))
3768 #ifdef OPENSSL_NO_RC2
3769 if (STR_STARTS_WITH(name, "RC2"))
3772 #ifdef OPENSSL_NO_RC4
3773 if (STR_STARTS_WITH(name, "RC4"))
3776 #ifdef OPENSSL_NO_RC5
3777 if (STR_STARTS_WITH(name, "RC5"))
3780 #ifdef OPENSSL_NO_SEED
3781 if (STR_STARTS_WITH(name, "SEED"))
3784 #ifdef OPENSSL_NO_SIV
3785 if (STR_ENDS_WITH(name, "SIV"))
3788 #ifdef OPENSSL_NO_SM4
3789 if (STR_STARTS_WITH(name, "SM4"))