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_get_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 /* AEAD ciphers only */
515 unsigned char *aad[AAD_NUM];
516 size_t aad_len[AAD_NUM];
520 const char *cts_mode;
523 unsigned char *mac_key;
527 static int cipher_test_init(EVP_TEST *t, const char *alg)
529 const EVP_CIPHER *cipher;
530 EVP_CIPHER *fetched_cipher;
534 if (is_cipher_disabled(alg)) {
536 TEST_info("skipping, '%s' is disabled", alg);
541 if ((cipher = fetched_cipher = EVP_CIPHER_fetch(libctx, alg, NULL)) == NULL
542 && (cipher = EVP_get_cipherbyname(alg)) == NULL) {
543 /* a stitched cipher might not be available */
544 if (strstr(alg, "HMAC") != NULL) {
547 TEST_info("skipping, '%s' is not available", alg);
550 ERR_clear_last_mark();
553 ERR_clear_last_mark();
555 cdat = OPENSSL_zalloc(sizeof(*cdat));
556 cdat->cipher = cipher;
557 cdat->fetched_cipher = fetched_cipher;
559 m = EVP_CIPHER_get_mode(cipher);
560 if (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
561 cdat->aead = m != 0 ? m : -1;
566 if (fetched_cipher != NULL)
567 TEST_info("%s is fetched", alg);
571 static void cipher_test_cleanup(EVP_TEST *t)
574 CIPHER_DATA *cdat = t->data;
576 OPENSSL_free(cdat->key);
577 OPENSSL_free(cdat->iv);
578 OPENSSL_free(cdat->next_iv);
579 OPENSSL_free(cdat->ciphertext);
580 OPENSSL_free(cdat->plaintext);
581 for (i = 0; i < AAD_NUM; i++)
582 OPENSSL_free(cdat->aad[i]);
583 OPENSSL_free(cdat->tag);
584 OPENSSL_free(cdat->mac_key);
585 EVP_CIPHER_free(cdat->fetched_cipher);
588 static int cipher_test_parse(EVP_TEST *t, const char *keyword,
591 CIPHER_DATA *cdat = t->data;
594 if (strcmp(keyword, "Key") == 0)
595 return parse_bin(value, &cdat->key, &cdat->key_len);
596 if (strcmp(keyword, "Rounds") == 0) {
600 cdat->rounds = (unsigned int)i;
603 if (strcmp(keyword, "IV") == 0)
604 return parse_bin(value, &cdat->iv, &cdat->iv_len);
605 if (strcmp(keyword, "NextIV") == 0)
606 return parse_bin(value, &cdat->next_iv, &cdat->iv_len);
607 if (strcmp(keyword, "Plaintext") == 0)
608 return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len);
609 if (strcmp(keyword, "Ciphertext") == 0)
610 return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
611 if (strcmp(keyword, "KeyBits") == 0) {
615 cdat->key_bits = (size_t)i;
621 if (strcmp(keyword, "TLSAAD") == 0)
622 cdat->tls_aad = tls_aad = 1;
623 if (strcmp(keyword, "AAD") == 0 || tls_aad) {
624 for (i = 0; i < AAD_NUM; i++) {
625 if (cdat->aad[i] == NULL)
626 return parse_bin(value, &cdat->aad[i], &cdat->aad_len[i]);
630 if (strcmp(keyword, "Tag") == 0)
631 return parse_bin(value, &cdat->tag, &cdat->tag_len);
632 if (strcmp(keyword, "SetTagLate") == 0) {
633 if (strcmp(value, "TRUE") == 0)
635 else if (strcmp(value, "FALSE") == 0)
641 if (strcmp(keyword, "MACKey") == 0)
642 return parse_bin(value, &cdat->mac_key, &cdat->mac_key_len);
643 if (strcmp(keyword, "TLSVersion") == 0) {
646 cdat->tls_version = (int)strtol(value, &endptr, 0);
647 return value[0] != '\0' && endptr[0] == '\0';
651 if (strcmp(keyword, "Operation") == 0) {
652 if (strcmp(value, "ENCRYPT") == 0)
654 else if (strcmp(value, "DECRYPT") == 0)
660 if (strcmp(keyword, "CTSMode") == 0) {
661 cdat->cts_mode = value;
667 static int cipher_test_enc(EVP_TEST *t, int enc,
668 size_t out_misalign, size_t inp_misalign, int frag)
670 CIPHER_DATA *expected = t->data;
671 unsigned char *in, *expected_out, *tmp = NULL;
672 size_t in_len, out_len, donelen = 0;
673 int ok = 0, tmplen, chunklen, tmpflen, i;
674 EVP_CIPHER_CTX *ctx_base = NULL;
675 EVP_CIPHER_CTX *ctx = NULL;
677 t->err = "TEST_FAILURE";
678 if (!TEST_ptr(ctx_base = EVP_CIPHER_CTX_new()))
680 if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
682 EVP_CIPHER_CTX_set_flags(ctx_base, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
684 in = expected->plaintext;
685 in_len = expected->plaintext_len;
686 expected_out = expected->ciphertext;
687 out_len = expected->ciphertext_len;
689 in = expected->ciphertext;
690 in_len = expected->ciphertext_len;
691 expected_out = expected->plaintext;
692 out_len = expected->plaintext_len;
694 if (inp_misalign == (size_t)-1) {
695 /* Exercise in-place encryption */
696 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
699 in = memcpy(tmp + out_misalign, in, in_len);
701 inp_misalign += 16 - ((out_misalign + in_len) & 15);
703 * 'tmp' will store both output and copy of input. We make the copy
704 * of input to specifically aligned part of 'tmp'. So we just
705 * figured out how much padding would ensure the required alignment,
706 * now we allocate extended buffer and finally copy the input just
707 * past inp_misalign in expression below. Output will be written
708 * past out_misalign...
710 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
711 inp_misalign + in_len);
714 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
715 inp_misalign, in, in_len);
717 if (!EVP_CipherInit_ex(ctx_base, expected->cipher, NULL, NULL, NULL, enc)) {
718 t->err = "CIPHERINIT_ERROR";
721 if (expected->cts_mode != NULL) {
722 OSSL_PARAM params[2];
724 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE,
725 (char *)expected->cts_mode,
727 params[1] = OSSL_PARAM_construct_end();
728 if (!EVP_CIPHER_CTX_set_params(ctx_base, params)) {
729 t->err = "INVALID_CTS_MODE";
734 if (expected->aead) {
735 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_IVLEN,
736 expected->iv_len, 0)) {
737 t->err = "INVALID_IV_LENGTH";
740 } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_get_iv_length(ctx_base)) {
741 t->err = "INVALID_IV_LENGTH";
745 if (expected->aead && !expected->tls_aad) {
748 * If encrypting or OCB just set tag length initially, otherwise
749 * set tag length and value.
751 if (enc || expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late) {
752 t->err = "TAG_LENGTH_SET_ERROR";
755 t->err = "TAG_SET_ERROR";
758 if (tag || expected->aead != EVP_CIPH_GCM_MODE) {
759 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_TAG,
760 expected->tag_len, tag))
765 if (expected->rounds > 0) {
766 int rounds = (int)expected->rounds;
768 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC5_ROUNDS, rounds, NULL)) {
769 t->err = "INVALID_ROUNDS";
774 if (!EVP_CIPHER_CTX_set_key_length(ctx_base, expected->key_len)) {
775 t->err = "INVALID_KEY_LENGTH";
778 if (expected->key_bits > 0) {
779 int bits = (int)expected->key_bits;
781 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC2_KEY_BITS, bits, NULL)) {
782 t->err = "INVALID KEY BITS";
786 if (!EVP_CipherInit_ex(ctx_base, NULL, NULL, expected->key, expected->iv, -1)) {
787 t->err = "KEY_SET_ERROR";
791 /* Check that we get the same IV back */
792 if (expected->iv != NULL) {
793 /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
794 unsigned char iv[128];
795 if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx_base, iv, sizeof(iv)))
796 || ((EVP_CIPHER_get_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
797 && !TEST_mem_eq(expected->iv, expected->iv_len, iv,
798 expected->iv_len))) {
799 t->err = "INVALID_IV";
804 /* Test that the cipher dup functions correctly if it is supported */
806 if (EVP_CIPHER_CTX_copy(ctx, ctx_base)) {
807 EVP_CIPHER_CTX_free(ctx_base);
810 EVP_CIPHER_CTX_free(ctx);
815 if (expected->mac_key != NULL
816 && !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY,
817 (int)expected->mac_key_len,
818 (void *)expected->mac_key)) {
819 t->err = "SET_MAC_KEY_ERROR";
823 if (expected->tls_version) {
824 OSSL_PARAM params[2];
826 params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS_VERSION,
827 &expected->tls_version);
828 params[1] = OSSL_PARAM_construct_end();
829 if (!EVP_CIPHER_CTX_set_params(ctx, params)) {
830 t->err = "SET_TLS_VERSION_ERROR";
835 if (expected->aead == EVP_CIPH_CCM_MODE) {
836 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
837 t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
841 if (expected->aad[0] != NULL && !expected->tls_aad) {
842 t->err = "AAD_SET_ERROR";
844 for (i = 0; expected->aad[i] != NULL; i++) {
845 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i],
846 expected->aad_len[i]))
851 * Supply the AAD in chunks less than the block size where possible
853 for (i = 0; expected->aad[i] != NULL; i++) {
854 if (expected->aad_len[i] > 0) {
855 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], 1))
859 if (expected->aad_len[i] > 2) {
860 if (!EVP_CipherUpdate(ctx, NULL, &chunklen,
861 expected->aad[i] + donelen,
862 expected->aad_len[i] - 2))
864 donelen += expected->aad_len[i] - 2;
866 if (expected->aad_len[i] > 1
867 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
868 expected->aad[i] + donelen, 1))
874 if (expected->tls_aad) {
875 OSSL_PARAM params[2];
878 /* duplicate the aad as the implementation might modify it */
879 if ((tls_aad = OPENSSL_memdup(expected->aad[0],
880 expected->aad_len[0])) == NULL)
882 params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
884 expected->aad_len[0]);
885 params[1] = OSSL_PARAM_construct_end();
886 if (!EVP_CIPHER_CTX_set_params(ctx, params)) {
887 OPENSSL_free(tls_aad);
888 t->err = "TLS1_AAD_ERROR";
891 OPENSSL_free(tls_aad);
892 } else if (!enc && (expected->aead == EVP_CIPH_OCB_MODE
893 || expected->tag_late)) {
894 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
895 expected->tag_len, expected->tag)) {
896 t->err = "TAG_SET_ERROR";
901 EVP_CIPHER_CTX_set_padding(ctx, 0);
902 t->err = "CIPHERUPDATE_ERROR";
905 /* We supply the data all in one go */
906 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
909 /* Supply the data in chunks less than the block size where possible */
911 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
918 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
926 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
932 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
933 t->err = "CIPHERFINAL_ERROR";
936 if (!enc && expected->tls_aad) {
937 if (expected->tls_version >= TLS1_1_VERSION
938 && (EVP_CIPHER_is_a(expected->cipher, "AES-128-CBC-HMAC-SHA1")
939 || EVP_CIPHER_is_a(expected->cipher, "AES-256-CBC-HMAC-SHA1"))) {
940 tmplen -= expected->iv_len;
941 expected_out += expected->iv_len;
942 out_misalign += expected->iv_len;
944 if ((int)out_len > tmplen + tmpflen)
945 out_len = tmplen + tmpflen;
947 if (!memory_err_compare(t, "VALUE_MISMATCH", expected_out, out_len,
948 tmp + out_misalign, tmplen + tmpflen))
950 if (enc && expected->aead && !expected->tls_aad) {
951 unsigned char rtag[16];
953 if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) {
954 t->err = "TAG_LENGTH_INTERNAL_ERROR";
957 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
958 expected->tag_len, rtag)) {
959 t->err = "TAG_RETRIEVE_ERROR";
962 if (!memory_err_compare(t, "TAG_VALUE_MISMATCH",
963 expected->tag, expected->tag_len,
964 rtag, expected->tag_len))
967 /* Check the updated IV */
968 if (expected->next_iv != NULL) {
969 /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
970 unsigned char iv[128];
971 if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv)))
972 || ((EVP_CIPHER_get_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
973 && !TEST_mem_eq(expected->next_iv, expected->iv_len, iv,
974 expected->iv_len))) {
975 t->err = "INVALID_NEXT_IV";
985 EVP_CIPHER_CTX_free(ctx_base);
986 EVP_CIPHER_CTX_free(ctx);
990 static int cipher_test_run(EVP_TEST *t)
992 CIPHER_DATA *cdat = t->data;
994 size_t out_misalign, inp_misalign;
1000 if (!cdat->iv && EVP_CIPHER_get_iv_length(cdat->cipher)) {
1001 /* IV is optional and usually omitted in wrap mode */
1002 if (EVP_CIPHER_get_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
1007 if (cdat->aead && cdat->tag == NULL && !cdat->tls_aad) {
1011 for (out_misalign = 0; out_misalign <= 1;) {
1012 static char aux_err[64];
1013 t->aux_err = aux_err;
1014 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
1015 if (inp_misalign == (size_t)-1) {
1016 /* kludge: inp_misalign == -1 means "exercise in-place" */
1017 BIO_snprintf(aux_err, sizeof(aux_err),
1018 "%s in-place, %sfragmented",
1019 out_misalign ? "misaligned" : "aligned",
1020 frag ? "" : "not ");
1022 BIO_snprintf(aux_err, sizeof(aux_err),
1023 "%s output and %s input, %sfragmented",
1024 out_misalign ? "misaligned" : "aligned",
1025 inp_misalign ? "misaligned" : "aligned",
1026 frag ? "" : "not ");
1029 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
1030 /* Not fatal errors: return */
1037 if (cdat->enc != 1) {
1038 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
1039 /* Not fatal errors: return */
1048 if (out_misalign == 1 && frag == 0) {
1050 * XTS, SIV, CCM, stitched ciphers and Wrap modes have special
1051 * requirements about input lengths so we don't fragment for those
1053 if (cdat->aead == EVP_CIPH_CCM_MODE
1054 || cdat->aead == EVP_CIPH_CBC_MODE
1055 || (cdat->aead == -1
1056 && EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_STREAM_CIPHER)
1057 || ((EVP_CIPHER_get_flags(cdat->cipher) & EVP_CIPH_FLAG_CTS) != 0)
1058 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_SIV_MODE
1059 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
1060 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
1073 static const EVP_TEST_METHOD cipher_test_method = {
1076 cipher_test_cleanup,
1086 typedef struct mac_data_st {
1087 /* MAC type in one form or another */
1089 EVP_MAC *mac; /* for mac_test_run_mac */
1090 int type; /* for mac_test_run_pkey */
1091 /* Algorithm string for this MAC */
1100 unsigned char *input;
1102 /* Expected output */
1103 unsigned char *output;
1105 unsigned char *custom;
1107 /* MAC salt (blake2) */
1108 unsigned char *salt;
1112 /* Collection of controls */
1113 STACK_OF(OPENSSL_STRING) *controls;
1120 static int mac_test_init(EVP_TEST *t, const char *alg)
1122 EVP_MAC *mac = NULL;
1123 int type = NID_undef;
1126 if (is_mac_disabled(alg)) {
1127 TEST_info("skipping, '%s' is disabled", alg);
1131 if ((mac = EVP_MAC_fetch(libctx, alg, NULL)) == NULL) {
1133 * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
1134 * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
1135 * the EVP_PKEY method.
1137 size_t sz = strlen(alg);
1138 static const char epilogue[] = " by EVP_PKEY";
1140 if (sz >= sizeof(epilogue)
1141 && strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0)
1142 sz -= sizeof(epilogue) - 1;
1144 if (strncmp(alg, "HMAC", sz) == 0)
1145 type = EVP_PKEY_HMAC;
1146 else if (strncmp(alg, "CMAC", sz) == 0)
1147 type = EVP_PKEY_CMAC;
1148 else if (strncmp(alg, "Poly1305", sz) == 0)
1149 type = EVP_PKEY_POLY1305;
1150 else if (strncmp(alg, "SipHash", sz) == 0)
1151 type = EVP_PKEY_SIPHASH;
1156 mdat = OPENSSL_zalloc(sizeof(*mdat));
1158 mdat->mac_name = OPENSSL_strdup(alg);
1160 mdat->controls = sk_OPENSSL_STRING_new_null();
1161 mdat->output_size = mdat->block_size = -1;
1166 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
1167 static void openssl_free(char *m)
1172 static void mac_test_cleanup(EVP_TEST *t)
1174 MAC_DATA *mdat = t->data;
1176 EVP_MAC_free(mdat->mac);
1177 OPENSSL_free(mdat->mac_name);
1178 sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free);
1179 OPENSSL_free(mdat->alg);
1180 OPENSSL_free(mdat->key);
1181 OPENSSL_free(mdat->iv);
1182 OPENSSL_free(mdat->custom);
1183 OPENSSL_free(mdat->salt);
1184 OPENSSL_free(mdat->input);
1185 OPENSSL_free(mdat->output);
1188 static int mac_test_parse(EVP_TEST *t,
1189 const char *keyword, const char *value)
1191 MAC_DATA *mdata = t->data;
1193 if (strcmp(keyword, "Key") == 0)
1194 return parse_bin(value, &mdata->key, &mdata->key_len);
1195 if (strcmp(keyword, "IV") == 0)
1196 return parse_bin(value, &mdata->iv, &mdata->iv_len);
1197 if (strcmp(keyword, "Custom") == 0)
1198 return parse_bin(value, &mdata->custom, &mdata->custom_len);
1199 if (strcmp(keyword, "Salt") == 0)
1200 return parse_bin(value, &mdata->salt, &mdata->salt_len);
1201 if (strcmp(keyword, "Algorithm") == 0) {
1202 mdata->alg = OPENSSL_strdup(value);
1207 if (strcmp(keyword, "Input") == 0)
1208 return parse_bin(value, &mdata->input, &mdata->input_len);
1209 if (strcmp(keyword, "Output") == 0)
1210 return parse_bin(value, &mdata->output, &mdata->output_len);
1211 if (strcmp(keyword, "XOF") == 0)
1212 return mdata->xof = 1;
1213 if (strcmp(keyword, "Ctrl") == 0)
1214 return sk_OPENSSL_STRING_push(mdata->controls,
1215 OPENSSL_strdup(value)) != 0;
1216 if (strcmp(keyword, "OutputSize") == 0) {
1217 mdata->output_size = atoi(value);
1218 if (mdata->output_size < 0)
1222 if (strcmp(keyword, "BlockSize") == 0) {
1223 mdata->block_size = atoi(value);
1224 if (mdata->block_size < 0)
1231 static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1237 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1239 p = strchr(tmpval, ':');
1242 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1245 t->err = "PKEY_CTRL_INVALID";
1247 t->err = "PKEY_CTRL_ERROR";
1250 OPENSSL_free(tmpval);
1254 static int mac_test_run_pkey(EVP_TEST *t)
1256 MAC_DATA *expected = t->data;
1257 EVP_MD_CTX *mctx = NULL;
1258 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1259 EVP_PKEY *key = NULL;
1260 const char *mdname = NULL;
1261 EVP_CIPHER *cipher = NULL;
1262 unsigned char *got = NULL;
1266 /* We don't do XOF mode via PKEY */
1270 if (expected->alg == NULL)
1271 TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type));
1273 TEST_info("Trying the EVP_PKEY %s test with %s",
1274 OBJ_nid2sn(expected->type), expected->alg);
1276 if (expected->type == EVP_PKEY_CMAC) {
1277 #ifdef OPENSSL_NO_DEPRECATED_3_0
1278 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
1283 OSSL_LIB_CTX *tmpctx;
1285 if (expected->alg != NULL && is_cipher_disabled(expected->alg)) {
1286 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
1291 if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, expected->alg, NULL))) {
1292 t->err = "MAC_KEY_CREATE_ERROR";
1295 tmpctx = OSSL_LIB_CTX_set0_default(libctx);
1296 key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len,
1298 OSSL_LIB_CTX_set0_default(tmpctx);
1301 key = EVP_PKEY_new_raw_private_key_ex(libctx,
1302 OBJ_nid2sn(expected->type), NULL,
1303 expected->key, expected->key_len);
1306 t->err = "MAC_KEY_CREATE_ERROR";
1310 if (expected->type == EVP_PKEY_HMAC && expected->alg != NULL) {
1311 if (is_digest_disabled(expected->alg)) {
1312 TEST_info("skipping, HMAC '%s' is disabled", expected->alg);
1317 mdname = expected->alg;
1319 if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
1320 t->err = "INTERNAL_ERROR";
1323 if (!EVP_DigestSignInit_ex(mctx, &pctx, mdname, libctx, NULL, key, NULL)) {
1324 t->err = "DIGESTSIGNINIT_ERROR";
1327 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++)
1328 if (!mac_test_ctrl_pkey(t, pctx,
1329 sk_OPENSSL_STRING_value(expected->controls,
1331 t->err = "EVPPKEYCTXCTRL_ERROR";
1334 if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) {
1335 t->err = "DIGESTSIGNUPDATE_ERROR";
1338 if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) {
1339 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1342 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1343 t->err = "TEST_FAILURE";
1346 if (!EVP_DigestSignFinal(mctx, got, &got_len)
1347 || !memory_err_compare(t, "TEST_MAC_ERR",
1348 expected->output, expected->output_len,
1350 t->err = "TEST_MAC_ERR";
1355 EVP_CIPHER_free(cipher);
1356 EVP_MD_CTX_free(mctx);
1358 EVP_PKEY_CTX_free(genctx);
1363 static int mac_test_run_mac(EVP_TEST *t)
1365 MAC_DATA *expected = t->data;
1366 EVP_MAC_CTX *ctx = NULL;
1367 unsigned char *got = NULL;
1369 int i, block_size = -1, output_size = -1;
1370 OSSL_PARAM params[21], sizes[3], *psizes = sizes;
1371 size_t params_n = 0;
1372 size_t params_n_allocstart = 0;
1373 const OSSL_PARAM *defined_params =
1374 EVP_MAC_settable_ctx_params(expected->mac);
1376 if (expected->alg == NULL)
1377 TEST_info("Trying the EVP_MAC %s test", expected->mac_name);
1379 TEST_info("Trying the EVP_MAC %s test with %s",
1380 expected->mac_name, expected->alg);
1382 if (expected->alg != NULL) {
1384 * The underlying algorithm may be a cipher or a digest.
1385 * We don't know which it is, but we can ask the MAC what it
1386 * should be and bet on that.
1388 if (OSSL_PARAM_locate_const(defined_params,
1389 OSSL_MAC_PARAM_CIPHER) != NULL) {
1390 params[params_n++] =
1391 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
1393 } else if (OSSL_PARAM_locate_const(defined_params,
1394 OSSL_MAC_PARAM_DIGEST) != NULL) {
1395 params[params_n++] =
1396 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
1399 t->err = "MAC_BAD_PARAMS";
1403 if (expected->custom != NULL)
1404 params[params_n++] =
1405 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
1407 expected->custom_len);
1408 if (expected->salt != NULL)
1409 params[params_n++] =
1410 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT,
1412 expected->salt_len);
1413 if (expected->iv != NULL)
1414 params[params_n++] =
1415 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
1419 /* Unknown controls. They must match parameters that the MAC recognizes */
1420 if (params_n + sk_OPENSSL_STRING_num(expected->controls)
1421 >= OSSL_NELEM(params)) {
1422 t->err = "MAC_TOO_MANY_PARAMETERS";
1425 params_n_allocstart = params_n;
1426 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) {
1427 char *tmpkey, *tmpval;
1428 char *value = sk_OPENSSL_STRING_value(expected->controls, i);
1430 if (!TEST_ptr(tmpkey = OPENSSL_strdup(value))) {
1431 t->err = "MAC_PARAM_ERROR";
1434 tmpval = strchr(tmpkey, ':');
1439 || !OSSL_PARAM_allocate_from_text(¶ms[params_n],
1442 strlen(tmpval), NULL)) {
1443 OPENSSL_free(tmpkey);
1444 t->err = "MAC_PARAM_ERROR";
1449 OPENSSL_free(tmpkey);
1451 params[params_n] = OSSL_PARAM_construct_end();
1453 if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) {
1454 t->err = "MAC_CREATE_ERROR";
1458 if (!EVP_MAC_init(ctx, expected->key, expected->key_len, params)) {
1459 t->err = "MAC_INIT_ERROR";
1462 if (expected->output_size >= 0)
1463 *psizes++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_SIZE,
1465 if (expected->block_size >= 0)
1466 *psizes++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_BLOCK_SIZE,
1468 if (psizes != sizes) {
1469 *psizes = OSSL_PARAM_construct_end();
1470 if (!TEST_true(EVP_MAC_CTX_get_params(ctx, sizes))) {
1471 t->err = "INTERNAL_ERROR";
1474 if (expected->output_size >= 0
1475 && !TEST_int_eq(output_size, expected->output_size)) {
1476 t->err = "TEST_FAILURE";
1479 if (expected->block_size >= 0
1480 && !TEST_int_eq(block_size, expected->block_size)) {
1481 t->err = "TEST_FAILURE";
1485 if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) {
1486 t->err = "MAC_UPDATE_ERROR";
1489 if (expected->xof) {
1490 if (!TEST_ptr(got = OPENSSL_malloc(expected->output_len))) {
1491 t->err = "TEST_FAILURE";
1494 if (!EVP_MAC_finalXOF(ctx, got, expected->output_len)
1495 || !memory_err_compare(t, "TEST_MAC_ERR",
1496 expected->output, expected->output_len,
1497 got, expected->output_len)) {
1498 t->err = "MAC_FINAL_ERROR";
1502 if (!EVP_MAC_final(ctx, NULL, &got_len, 0)) {
1503 t->err = "MAC_FINAL_LENGTH_ERROR";
1506 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1507 t->err = "TEST_FAILURE";
1510 if (!EVP_MAC_final(ctx, got, &got_len, got_len)
1511 || !memory_err_compare(t, "TEST_MAC_ERR",
1512 expected->output, expected->output_len,
1514 t->err = "TEST_MAC_ERR";
1520 while (params_n-- > params_n_allocstart) {
1521 OPENSSL_free(params[params_n].data);
1523 EVP_MAC_CTX_free(ctx);
1528 static int mac_test_run(EVP_TEST *t)
1530 MAC_DATA *expected = t->data;
1532 if (expected->mac != NULL)
1533 return mac_test_run_mac(t);
1534 return mac_test_run_pkey(t);
1537 static const EVP_TEST_METHOD mac_test_method = {
1548 ** These are all very similar and share much common code.
1551 typedef struct pkey_data_st {
1552 /* Context for this operation */
1554 /* Key operation to perform */
1555 int (*keyop) (EVP_PKEY_CTX *ctx,
1556 unsigned char *sig, size_t *siglen,
1557 const unsigned char *tbs, size_t tbslen);
1559 unsigned char *input;
1561 /* Expected output */
1562 unsigned char *output;
1567 * Perform public key operation setup: lookup key, allocated ctx and call
1568 * the appropriate initialisation function
1570 static int pkey_test_init(EVP_TEST *t, const char *name,
1572 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1573 int (*keyop)(EVP_PKEY_CTX *ctx,
1574 unsigned char *sig, size_t *siglen,
1575 const unsigned char *tbs,
1579 EVP_PKEY *pkey = NULL;
1583 rv = find_key(&pkey, name, public_keys);
1585 rv = find_key(&pkey, name, private_keys);
1586 if (rv == 0 || pkey == NULL) {
1587 TEST_info("skipping, key '%s' is disabled", name);
1592 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) {
1593 EVP_PKEY_free(pkey);
1596 kdata->keyop = keyop;
1597 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL))) {
1598 EVP_PKEY_free(pkey);
1599 OPENSSL_free(kdata);
1602 if (keyopinit(kdata->ctx) <= 0)
1603 t->err = "KEYOP_INIT_ERROR";
1608 static void pkey_test_cleanup(EVP_TEST *t)
1610 PKEY_DATA *kdata = t->data;
1612 OPENSSL_free(kdata->input);
1613 OPENSSL_free(kdata->output);
1614 EVP_PKEY_CTX_free(kdata->ctx);
1617 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1623 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1625 p = strchr(tmpval, ':');
1628 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1631 t->err = "PKEY_CTRL_INVALID";
1633 } else if (p != NULL && rv <= 0) {
1634 if (is_digest_disabled(p) || is_cipher_disabled(p)) {
1635 TEST_info("skipping, '%s' is disabled", p);
1639 t->err = "PKEY_CTRL_ERROR";
1643 OPENSSL_free(tmpval);
1647 static int pkey_test_parse(EVP_TEST *t,
1648 const char *keyword, const char *value)
1650 PKEY_DATA *kdata = t->data;
1651 if (strcmp(keyword, "Input") == 0)
1652 return parse_bin(value, &kdata->input, &kdata->input_len);
1653 if (strcmp(keyword, "Output") == 0)
1654 return parse_bin(value, &kdata->output, &kdata->output_len);
1655 if (strcmp(keyword, "Ctrl") == 0)
1656 return pkey_test_ctrl(t, kdata->ctx, value);
1660 static int pkey_test_run(EVP_TEST *t)
1662 PKEY_DATA *expected = t->data;
1663 unsigned char *got = NULL;
1665 EVP_PKEY_CTX *copy = NULL;
1667 if (expected->keyop(expected->ctx, NULL, &got_len,
1668 expected->input, expected->input_len) <= 0
1669 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1670 t->err = "KEYOP_LENGTH_ERROR";
1673 if (expected->keyop(expected->ctx, got, &got_len,
1674 expected->input, expected->input_len) <= 0) {
1675 t->err = "KEYOP_ERROR";
1678 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1679 expected->output, expected->output_len,
1687 /* Repeat the test on a copy. */
1688 if (!TEST_ptr(copy = EVP_PKEY_CTX_dup(expected->ctx))) {
1689 t->err = "INTERNAL_ERROR";
1692 if (expected->keyop(copy, NULL, &got_len, expected->input,
1693 expected->input_len) <= 0
1694 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1695 t->err = "KEYOP_LENGTH_ERROR";
1698 if (expected->keyop(copy, got, &got_len, expected->input,
1699 expected->input_len) <= 0) {
1700 t->err = "KEYOP_ERROR";
1703 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1704 expected->output, expected->output_len,
1710 EVP_PKEY_CTX_free(copy);
1714 static int sign_test_init(EVP_TEST *t, const char *name)
1716 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1719 static const EVP_TEST_METHOD psign_test_method = {
1727 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1729 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1730 EVP_PKEY_verify_recover);
1733 static const EVP_TEST_METHOD pverify_recover_test_method = {
1735 verify_recover_test_init,
1741 static int decrypt_test_init(EVP_TEST *t, const char *name)
1743 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1747 static const EVP_TEST_METHOD pdecrypt_test_method = {
1755 static int verify_test_init(EVP_TEST *t, const char *name)
1757 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1760 static int verify_test_run(EVP_TEST *t)
1762 PKEY_DATA *kdata = t->data;
1764 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1765 kdata->input, kdata->input_len) <= 0)
1766 t->err = "VERIFY_ERROR";
1770 static const EVP_TEST_METHOD pverify_test_method = {
1778 static int pderive_test_init(EVP_TEST *t, const char *name)
1780 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1783 static int pderive_test_parse(EVP_TEST *t,
1784 const char *keyword, const char *value)
1786 PKEY_DATA *kdata = t->data;
1789 if (strcmp(keyword, "PeerKeyValidate") == 0)
1792 if (validate || strcmp(keyword, "PeerKey") == 0) {
1794 if (find_key(&peer, value, public_keys) == 0)
1796 if (EVP_PKEY_derive_set_peer_ex(kdata->ctx, peer, validate) <= 0) {
1797 t->err = "DERIVE_SET_PEER_ERROR";
1803 if (strcmp(keyword, "SharedSecret") == 0)
1804 return parse_bin(value, &kdata->output, &kdata->output_len);
1805 if (strcmp(keyword, "Ctrl") == 0)
1806 return pkey_test_ctrl(t, kdata->ctx, value);
1810 static int pderive_test_run(EVP_TEST *t)
1812 PKEY_DATA *expected = t->data;
1813 unsigned char *got = NULL;
1816 if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) {
1817 t->err = "DERIVE_ERROR";
1820 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1821 t->err = "DERIVE_ERROR";
1824 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
1825 t->err = "DERIVE_ERROR";
1828 if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH",
1829 expected->output, expected->output_len,
1839 static const EVP_TEST_METHOD pderive_test_method = {
1852 typedef enum pbe_type_enum {
1853 PBE_TYPE_INVALID = 0,
1854 PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
1857 typedef struct pbe_data_st {
1859 /* scrypt parameters */
1860 uint64_t N, r, p, maxmem;
1861 /* PKCS#12 parameters */
1865 unsigned char *pass;
1868 unsigned char *salt;
1870 /* Expected output */
1875 #ifndef OPENSSL_NO_SCRYPT
1876 /* Parse unsigned decimal 64 bit integer value */
1877 static int parse_uint64(const char *value, uint64_t *pr)
1879 const char *p = value;
1881 if (!TEST_true(*p)) {
1882 TEST_info("Invalid empty integer value");
1885 for (*pr = 0; *p; ) {
1886 if (*pr > UINT64_MAX / 10) {
1887 TEST_error("Integer overflow in string %s", value);
1891 if (!TEST_true(isdigit((unsigned char)*p))) {
1892 TEST_error("Invalid character in string %s", value);
1901 static int scrypt_test_parse(EVP_TEST *t,
1902 const char *keyword, const char *value)
1904 PBE_DATA *pdata = t->data;
1906 if (strcmp(keyword, "N") == 0)
1907 return parse_uint64(value, &pdata->N);
1908 if (strcmp(keyword, "p") == 0)
1909 return parse_uint64(value, &pdata->p);
1910 if (strcmp(keyword, "r") == 0)
1911 return parse_uint64(value, &pdata->r);
1912 if (strcmp(keyword, "maxmem") == 0)
1913 return parse_uint64(value, &pdata->maxmem);
1918 static int pbkdf2_test_parse(EVP_TEST *t,
1919 const char *keyword, const char *value)
1921 PBE_DATA *pdata = t->data;
1923 if (strcmp(keyword, "iter") == 0) {
1924 pdata->iter = atoi(value);
1925 if (pdata->iter <= 0)
1929 if (strcmp(keyword, "MD") == 0) {
1930 pdata->md = EVP_get_digestbyname(value);
1931 if (pdata->md == NULL)
1938 static int pkcs12_test_parse(EVP_TEST *t,
1939 const char *keyword, const char *value)
1941 PBE_DATA *pdata = t->data;
1943 if (strcmp(keyword, "id") == 0) {
1944 pdata->id = atoi(value);
1949 return pbkdf2_test_parse(t, keyword, value);
1952 static int pbe_test_init(EVP_TEST *t, const char *alg)
1955 PBE_TYPE pbe_type = PBE_TYPE_INVALID;
1957 if (is_kdf_disabled(alg)) {
1958 TEST_info("skipping, '%s' is disabled", alg);
1962 if (strcmp(alg, "scrypt") == 0) {
1963 pbe_type = PBE_TYPE_SCRYPT;
1964 } else if (strcmp(alg, "pbkdf2") == 0) {
1965 pbe_type = PBE_TYPE_PBKDF2;
1966 } else if (strcmp(alg, "pkcs12") == 0) {
1967 pbe_type = PBE_TYPE_PKCS12;
1969 TEST_error("Unknown pbe algorithm %s", alg);
1971 pdat = OPENSSL_zalloc(sizeof(*pdat));
1972 pdat->pbe_type = pbe_type;
1977 static void pbe_test_cleanup(EVP_TEST *t)
1979 PBE_DATA *pdat = t->data;
1981 OPENSSL_free(pdat->pass);
1982 OPENSSL_free(pdat->salt);
1983 OPENSSL_free(pdat->key);
1986 static int pbe_test_parse(EVP_TEST *t,
1987 const char *keyword, const char *value)
1989 PBE_DATA *pdata = t->data;
1991 if (strcmp(keyword, "Password") == 0)
1992 return parse_bin(value, &pdata->pass, &pdata->pass_len);
1993 if (strcmp(keyword, "Salt") == 0)
1994 return parse_bin(value, &pdata->salt, &pdata->salt_len);
1995 if (strcmp(keyword, "Key") == 0)
1996 return parse_bin(value, &pdata->key, &pdata->key_len);
1997 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1998 return pbkdf2_test_parse(t, keyword, value);
1999 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
2000 return pkcs12_test_parse(t, keyword, value);
2001 #ifndef OPENSSL_NO_SCRYPT
2002 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
2003 return scrypt_test_parse(t, keyword, value);
2008 static int pbe_test_run(EVP_TEST *t)
2010 PBE_DATA *expected = t->data;
2012 EVP_MD *fetched_digest = NULL;
2013 OSSL_LIB_CTX *save_libctx;
2015 save_libctx = OSSL_LIB_CTX_set0_default(libctx);
2017 if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
2018 t->err = "INTERNAL_ERROR";
2021 if (expected->pbe_type == PBE_TYPE_PBKDF2) {
2022 if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
2023 expected->salt, expected->salt_len,
2024 expected->iter, expected->md,
2025 expected->key_len, key) == 0) {
2026 t->err = "PBKDF2_ERROR";
2029 #ifndef OPENSSL_NO_SCRYPT
2030 } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
2031 if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
2032 expected->salt, expected->salt_len,
2033 expected->N, expected->r, expected->p,
2034 expected->maxmem, key, expected->key_len) == 0) {
2035 t->err = "SCRYPT_ERROR";
2039 } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
2040 fetched_digest = EVP_MD_fetch(libctx, EVP_MD_get0_name(expected->md),
2042 if (fetched_digest == NULL) {
2043 t->err = "PKCS12_ERROR";
2046 if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
2047 expected->salt, expected->salt_len,
2048 expected->id, expected->iter, expected->key_len,
2049 key, fetched_digest) == 0) {
2050 t->err = "PKCS12_ERROR";
2054 if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len,
2055 key, expected->key_len))
2060 EVP_MD_free(fetched_digest);
2062 OSSL_LIB_CTX_set0_default(save_libctx);
2066 static const EVP_TEST_METHOD pbe_test_method = {
2080 BASE64_CANONICAL_ENCODING = 0,
2081 BASE64_VALID_ENCODING = 1,
2082 BASE64_INVALID_ENCODING = 2
2083 } base64_encoding_type;
2085 typedef struct encode_data_st {
2086 /* Input to encoding */
2087 unsigned char *input;
2089 /* Expected output */
2090 unsigned char *output;
2092 base64_encoding_type encoding;
2095 static int encode_test_init(EVP_TEST *t, const char *encoding)
2099 if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
2101 if (strcmp(encoding, "canonical") == 0) {
2102 edata->encoding = BASE64_CANONICAL_ENCODING;
2103 } else if (strcmp(encoding, "valid") == 0) {
2104 edata->encoding = BASE64_VALID_ENCODING;
2105 } else if (strcmp(encoding, "invalid") == 0) {
2106 edata->encoding = BASE64_INVALID_ENCODING;
2107 if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
2110 TEST_error("Bad encoding: %s."
2111 " Should be one of {canonical, valid, invalid}",
2118 OPENSSL_free(edata);
2122 static void encode_test_cleanup(EVP_TEST *t)
2124 ENCODE_DATA *edata = t->data;
2126 OPENSSL_free(edata->input);
2127 OPENSSL_free(edata->output);
2128 memset(edata, 0, sizeof(*edata));
2131 static int encode_test_parse(EVP_TEST *t,
2132 const char *keyword, const char *value)
2134 ENCODE_DATA *edata = t->data;
2136 if (strcmp(keyword, "Input") == 0)
2137 return parse_bin(value, &edata->input, &edata->input_len);
2138 if (strcmp(keyword, "Output") == 0)
2139 return parse_bin(value, &edata->output, &edata->output_len);
2143 static int encode_test_run(EVP_TEST *t)
2145 ENCODE_DATA *expected = t->data;
2146 unsigned char *encode_out = NULL, *decode_out = NULL;
2147 int output_len, chunk_len;
2148 EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL;
2150 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
2151 t->err = "INTERNAL_ERROR";
2155 if (expected->encoding == BASE64_CANONICAL_ENCODING) {
2157 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
2158 || !TEST_ptr(encode_out =
2159 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
2162 EVP_EncodeInit(encode_ctx);
2163 if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
2164 expected->input, expected->input_len)))
2167 output_len = chunk_len;
2169 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
2170 output_len += chunk_len;
2172 if (!memory_err_compare(t, "BAD_ENCODING",
2173 expected->output, expected->output_len,
2174 encode_out, output_len))
2178 if (!TEST_ptr(decode_out =
2179 OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
2182 EVP_DecodeInit(decode_ctx);
2183 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
2184 expected->output_len) < 0) {
2185 t->err = "DECODE_ERROR";
2188 output_len = chunk_len;
2190 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
2191 t->err = "DECODE_ERROR";
2194 output_len += chunk_len;
2196 if (expected->encoding != BASE64_INVALID_ENCODING
2197 && !memory_err_compare(t, "BAD_DECODING",
2198 expected->input, expected->input_len,
2199 decode_out, output_len)) {
2200 t->err = "BAD_DECODING";
2206 OPENSSL_free(encode_out);
2207 OPENSSL_free(decode_out);
2208 EVP_ENCODE_CTX_free(decode_ctx);
2209 EVP_ENCODE_CTX_free(encode_ctx);
2213 static const EVP_TEST_METHOD encode_test_method = {
2216 encode_test_cleanup,
2225 #define MAX_RAND_REPEATS 15
2227 typedef struct rand_data_pass_st {
2228 unsigned char *entropy;
2229 unsigned char *reseed_entropy;
2230 unsigned char *nonce;
2231 unsigned char *pers;
2232 unsigned char *reseed_addin;
2233 unsigned char *addinA;
2234 unsigned char *addinB;
2235 unsigned char *pr_entropyA;
2236 unsigned char *pr_entropyB;
2237 unsigned char *output;
2238 size_t entropy_len, nonce_len, pers_len, addinA_len, addinB_len,
2239 pr_entropyA_len, pr_entropyB_len, output_len, reseed_entropy_len,
2243 typedef struct rand_data_st {
2244 /* Context for this operation */
2246 EVP_RAND_CTX *parent;
2248 int prediction_resistance;
2250 unsigned int generate_bits;
2254 /* Expected output */
2255 RAND_DATA_PASS data[MAX_RAND_REPEATS];
2258 static int rand_test_init(EVP_TEST *t, const char *name)
2262 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
2263 unsigned int strength = 256;
2265 if (!TEST_ptr(rdata = OPENSSL_zalloc(sizeof(*rdata))))
2268 /* TEST-RAND is available in the FIPS provider but not with "fips=yes" */
2269 rand = EVP_RAND_fetch(libctx, "TEST-RAND", "-fips");
2272 rdata->parent = EVP_RAND_CTX_new(rand, NULL);
2273 EVP_RAND_free(rand);
2274 if (rdata->parent == NULL)
2277 *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength);
2278 if (!EVP_RAND_CTX_set_params(rdata->parent, params))
2281 rand = EVP_RAND_fetch(libctx, name, NULL);
2284 rdata->ctx = EVP_RAND_CTX_new(rand, rdata->parent);
2285 EVP_RAND_free(rand);
2286 if (rdata->ctx == NULL)
2293 EVP_RAND_CTX_free(rdata->parent);
2294 OPENSSL_free(rdata);
2298 static void rand_test_cleanup(EVP_TEST *t)
2300 RAND_DATA *rdata = t->data;
2303 OPENSSL_free(rdata->cipher);
2304 OPENSSL_free(rdata->digest);
2306 for (i = 0; i <= rdata->n; i++) {
2307 OPENSSL_free(rdata->data[i].entropy);
2308 OPENSSL_free(rdata->data[i].reseed_entropy);
2309 OPENSSL_free(rdata->data[i].nonce);
2310 OPENSSL_free(rdata->data[i].pers);
2311 OPENSSL_free(rdata->data[i].reseed_addin);
2312 OPENSSL_free(rdata->data[i].addinA);
2313 OPENSSL_free(rdata->data[i].addinB);
2314 OPENSSL_free(rdata->data[i].pr_entropyA);
2315 OPENSSL_free(rdata->data[i].pr_entropyB);
2316 OPENSSL_free(rdata->data[i].output);
2318 EVP_RAND_CTX_free(rdata->ctx);
2319 EVP_RAND_CTX_free(rdata->parent);
2322 static int rand_test_parse(EVP_TEST *t,
2323 const char *keyword, const char *value)
2325 RAND_DATA *rdata = t->data;
2326 RAND_DATA_PASS *item;
2330 if ((p = strchr(keyword, '.')) != NULL) {
2332 if (n >= MAX_RAND_REPEATS)
2336 item = rdata->data + n;
2337 if (strncmp(keyword, "Entropy.", sizeof("Entropy")) == 0)
2338 return parse_bin(value, &item->entropy, &item->entropy_len);
2339 if (strncmp(keyword, "ReseedEntropy.", sizeof("ReseedEntropy")) == 0)
2340 return parse_bin(value, &item->reseed_entropy,
2341 &item->reseed_entropy_len);
2342 if (strncmp(keyword, "Nonce.", sizeof("Nonce")) == 0)
2343 return parse_bin(value, &item->nonce, &item->nonce_len);
2344 if (strncmp(keyword, "PersonalisationString.",
2345 sizeof("PersonalisationString")) == 0)
2346 return parse_bin(value, &item->pers, &item->pers_len);
2347 if (strncmp(keyword, "ReseedAdditionalInput.",
2348 sizeof("ReseedAdditionalInput")) == 0)
2349 return parse_bin(value, &item->reseed_addin,
2350 &item->reseed_addin_len);
2351 if (strncmp(keyword, "AdditionalInputA.",
2352 sizeof("AdditionalInputA")) == 0)
2353 return parse_bin(value, &item->addinA, &item->addinA_len);
2354 if (strncmp(keyword, "AdditionalInputB.",
2355 sizeof("AdditionalInputB")) == 0)
2356 return parse_bin(value, &item->addinB, &item->addinB_len);
2357 if (strncmp(keyword, "EntropyPredictionResistanceA.",
2358 sizeof("EntropyPredictionResistanceA")) == 0)
2359 return parse_bin(value, &item->pr_entropyA, &item->pr_entropyA_len);
2360 if (strncmp(keyword, "EntropyPredictionResistanceB.",
2361 sizeof("EntropyPredictionResistanceB")) == 0)
2362 return parse_bin(value, &item->pr_entropyB, &item->pr_entropyB_len);
2363 if (strncmp(keyword, "Output.", sizeof("Output")) == 0)
2364 return parse_bin(value, &item->output, &item->output_len);
2366 if (strcmp(keyword, "Cipher") == 0)
2367 return TEST_ptr(rdata->cipher = OPENSSL_strdup(value));
2368 if (strcmp(keyword, "Digest") == 0)
2369 return TEST_ptr(rdata->digest = OPENSSL_strdup(value));
2370 if (strcmp(keyword, "DerivationFunction") == 0) {
2371 rdata->use_df = atoi(value) != 0;
2374 if (strcmp(keyword, "GenerateBits") == 0) {
2375 if ((n = atoi(value)) <= 0 || n % 8 != 0)
2377 rdata->generate_bits = (unsigned int)n;
2380 if (strcmp(keyword, "PredictionResistance") == 0) {
2381 rdata->prediction_resistance = atoi(value) != 0;
2388 static int rand_test_run(EVP_TEST *t)
2390 RAND_DATA *expected = t->data;
2391 RAND_DATA_PASS *item;
2393 size_t got_len = expected->generate_bits / 8;
2394 OSSL_PARAM params[5], *p = params;
2395 int i = -1, ret = 0;
2396 unsigned int strength;
2399 if (!TEST_ptr(got = OPENSSL_malloc(got_len)))
2402 *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &expected->use_df);
2403 if (expected->cipher != NULL)
2404 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
2405 expected->cipher, 0);
2406 if (expected->digest != NULL)
2407 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
2408 expected->digest, 0);
2409 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC, "HMAC", 0);
2410 *p = OSSL_PARAM_construct_end();
2411 if (!TEST_true(EVP_RAND_CTX_set_params(expected->ctx, params)))
2414 strength = EVP_RAND_get_strength(expected->ctx);
2415 for (i = 0; i <= expected->n; i++) {
2416 item = expected->data + i;
2419 z = item->entropy != NULL ? item->entropy : (unsigned char *)"";
2420 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
2421 z, item->entropy_len);
2422 z = item->nonce != NULL ? item->nonce : (unsigned char *)"";
2423 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
2424 z, item->nonce_len);
2425 *p = OSSL_PARAM_construct_end();
2426 if (!TEST_true(EVP_RAND_instantiate(expected->parent, strength,
2427 0, NULL, 0, params)))
2430 z = item->pers != NULL ? item->pers : (unsigned char *)"";
2431 if (!TEST_true(EVP_RAND_instantiate
2432 (expected->ctx, strength,
2433 expected->prediction_resistance, z,
2434 item->pers_len, NULL)))
2437 if (item->reseed_entropy != NULL) {
2438 params[0] = OSSL_PARAM_construct_octet_string
2439 (OSSL_RAND_PARAM_TEST_ENTROPY, item->reseed_entropy,
2440 item->reseed_entropy_len);
2441 params[1] = OSSL_PARAM_construct_end();
2442 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2445 if (!TEST_true(EVP_RAND_reseed
2446 (expected->ctx, expected->prediction_resistance,
2447 NULL, 0, item->reseed_addin,
2448 item->reseed_addin_len)))
2451 if (item->pr_entropyA != NULL) {
2452 params[0] = OSSL_PARAM_construct_octet_string
2453 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyA,
2454 item->pr_entropyA_len);
2455 params[1] = OSSL_PARAM_construct_end();
2456 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2459 if (!TEST_true(EVP_RAND_generate
2460 (expected->ctx, got, got_len,
2461 strength, expected->prediction_resistance,
2462 item->addinA, item->addinA_len)))
2465 if (item->pr_entropyB != NULL) {
2466 params[0] = OSSL_PARAM_construct_octet_string
2467 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyB,
2468 item->pr_entropyB_len);
2469 params[1] = OSSL_PARAM_construct_end();
2470 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2473 if (!TEST_true(EVP_RAND_generate
2474 (expected->ctx, got, got_len,
2475 strength, expected->prediction_resistance,
2476 item->addinB, item->addinB_len)))
2478 if (!TEST_mem_eq(got, got_len, item->output, item->output_len))
2480 if (!TEST_true(EVP_RAND_uninstantiate(expected->ctx))
2481 || !TEST_true(EVP_RAND_uninstantiate(expected->parent))
2482 || !TEST_true(EVP_RAND_verify_zeroization(expected->ctx))
2483 || !TEST_int_eq(EVP_RAND_get_state(expected->ctx),
2484 EVP_RAND_STATE_UNINITIALISED))
2491 if (ret == 0 && i >= 0)
2492 TEST_info("Error in test case %d of %d\n", i, expected->n + 1);
2497 static const EVP_TEST_METHOD rand_test_method = {
2509 typedef struct kdf_data_st {
2510 /* Context for this operation */
2512 /* Expected output */
2513 unsigned char *output;
2515 OSSL_PARAM params[20];
2520 * Perform public key operation setup: lookup key, allocated ctx and call
2521 * the appropriate initialisation function
2523 static int kdf_test_init(EVP_TEST *t, const char *name)
2528 if (is_kdf_disabled(name)) {
2529 TEST_info("skipping, '%s' is disabled", name);
2534 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2536 kdata->p = kdata->params;
2537 *kdata->p = OSSL_PARAM_construct_end();
2539 kdf = EVP_KDF_fetch(libctx, name, NULL);
2541 OPENSSL_free(kdata);
2544 kdata->ctx = EVP_KDF_CTX_new(kdf);
2546 if (kdata->ctx == NULL) {
2547 OPENSSL_free(kdata);
2554 static void kdf_test_cleanup(EVP_TEST *t)
2556 KDF_DATA *kdata = t->data;
2559 for (p = kdata->params; p->key != NULL; p++)
2560 OPENSSL_free(p->data);
2561 OPENSSL_free(kdata->output);
2562 EVP_KDF_CTX_free(kdata->ctx);
2565 static int kdf_test_ctrl(EVP_TEST *t, EVP_KDF_CTX *kctx,
2568 KDF_DATA *kdata = t->data;
2571 const OSSL_PARAM *defs = EVP_KDF_settable_ctx_params(EVP_KDF_CTX_kdf(kctx));
2573 if (!TEST_ptr(name = OPENSSL_strdup(value)))
2575 p = strchr(name, ':');
2579 rv = OSSL_PARAM_allocate_from_text(kdata->p, defs, name, p,
2580 p != NULL ? strlen(p) : 0, NULL);
2581 *++kdata->p = OSSL_PARAM_construct_end();
2583 t->err = "KDF_PARAM_ERROR";
2587 if (p != NULL && strcmp(name, "digest") == 0) {
2588 if (is_digest_disabled(p)) {
2589 TEST_info("skipping, '%s' is disabled", p);
2594 && (strcmp(name, "cipher") == 0
2595 || strcmp(name, "cekalg") == 0)
2596 && is_cipher_disabled(p)) {
2597 TEST_info("skipping, '%s' is disabled", p);
2604 static int kdf_test_parse(EVP_TEST *t,
2605 const char *keyword, const char *value)
2607 KDF_DATA *kdata = t->data;
2609 if (strcmp(keyword, "Output") == 0)
2610 return parse_bin(value, &kdata->output, &kdata->output_len);
2611 if (strncmp(keyword, "Ctrl", 4) == 0)
2612 return kdf_test_ctrl(t, kdata->ctx, value);
2616 static int kdf_test_run(EVP_TEST *t)
2618 KDF_DATA *expected = t->data;
2619 unsigned char *got = NULL;
2620 size_t got_len = expected->output_len;
2622 if (!EVP_KDF_CTX_set_params(expected->ctx, expected->params)) {
2623 t->err = "KDF_CTRL_ERROR";
2626 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2627 t->err = "INTERNAL_ERROR";
2630 if (EVP_KDF_derive(expected->ctx, got, got_len, NULL) <= 0) {
2631 t->err = "KDF_DERIVE_ERROR";
2634 if (!memory_err_compare(t, "KDF_MISMATCH",
2635 expected->output, expected->output_len,
2646 static const EVP_TEST_METHOD kdf_test_method = {
2658 typedef struct pkey_kdf_data_st {
2659 /* Context for this operation */
2661 /* Expected output */
2662 unsigned char *output;
2667 * Perform public key operation setup: lookup key, allocated ctx and call
2668 * the appropriate initialisation function
2670 static int pkey_kdf_test_init(EVP_TEST *t, const char *name)
2672 PKEY_KDF_DATA *kdata = NULL;
2674 if (is_kdf_disabled(name)) {
2675 TEST_info("skipping, '%s' is disabled", name);
2680 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2683 kdata->ctx = EVP_PKEY_CTX_new_from_name(libctx, name, NULL);
2684 if (kdata->ctx == NULL
2685 || EVP_PKEY_derive_init(kdata->ctx) <= 0)
2691 EVP_PKEY_CTX_free(kdata->ctx);
2692 OPENSSL_free(kdata);
2696 static void pkey_kdf_test_cleanup(EVP_TEST *t)
2698 PKEY_KDF_DATA *kdata = t->data;
2700 OPENSSL_free(kdata->output);
2701 EVP_PKEY_CTX_free(kdata->ctx);
2704 static int pkey_kdf_test_parse(EVP_TEST *t,
2705 const char *keyword, const char *value)
2707 PKEY_KDF_DATA *kdata = t->data;
2709 if (strcmp(keyword, "Output") == 0)
2710 return parse_bin(value, &kdata->output, &kdata->output_len);
2711 if (strncmp(keyword, "Ctrl", 4) == 0)
2712 return pkey_test_ctrl(t, kdata->ctx, value);
2716 static int pkey_kdf_test_run(EVP_TEST *t)
2718 PKEY_KDF_DATA *expected = t->data;
2719 unsigned char *got = NULL;
2720 size_t got_len = expected->output_len;
2722 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2723 t->err = "INTERNAL_ERROR";
2726 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
2727 t->err = "KDF_DERIVE_ERROR";
2730 if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
2731 t->err = "KDF_MISMATCH";
2741 static const EVP_TEST_METHOD pkey_kdf_test_method = {
2744 pkey_kdf_test_cleanup,
2745 pkey_kdf_test_parse,
2753 typedef struct keypair_test_data_st {
2756 } KEYPAIR_TEST_DATA;
2758 static int keypair_test_init(EVP_TEST *t, const char *pair)
2760 KEYPAIR_TEST_DATA *data;
2762 EVP_PKEY *pk = NULL, *pubk = NULL;
2763 char *pub, *priv = NULL;
2765 /* Split private and public names. */
2766 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
2767 || !TEST_ptr(pub = strchr(priv, ':'))) {
2768 t->err = "PARSING_ERROR";
2773 if (!TEST_true(find_key(&pk, priv, private_keys))) {
2774 TEST_info("Can't find private key: %s", priv);
2775 t->err = "MISSING_PRIVATE_KEY";
2778 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
2779 TEST_info("Can't find public key: %s", pub);
2780 t->err = "MISSING_PUBLIC_KEY";
2784 if (pk == NULL && pubk == NULL) {
2785 /* Both keys are listed but unsupported: skip this test */
2791 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2804 static void keypair_test_cleanup(EVP_TEST *t)
2806 OPENSSL_free(t->data);
2811 * For tests that do not accept any custom keywords.
2813 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
2818 static int keypair_test_run(EVP_TEST *t)
2821 const KEYPAIR_TEST_DATA *pair = t->data;
2823 if (pair->privk == NULL || pair->pubk == NULL) {
2825 * this can only happen if only one of the keys is not set
2826 * which means that one of them was unsupported while the
2827 * other isn't: hence a key type mismatch.
2829 t->err = "KEYPAIR_TYPE_MISMATCH";
2834 if ((rv = EVP_PKEY_eq(pair->privk, pair->pubk)) != 1 ) {
2836 t->err = "KEYPAIR_MISMATCH";
2837 } else if ( -1 == rv ) {
2838 t->err = "KEYPAIR_TYPE_MISMATCH";
2839 } else if ( -2 == rv ) {
2840 t->err = "UNSUPPORTED_KEY_COMPARISON";
2842 TEST_error("Unexpected error in key comparison");
2857 static const EVP_TEST_METHOD keypair_test_method = {
2860 keypair_test_cleanup,
2869 typedef struct keygen_test_data_st {
2870 EVP_PKEY_CTX *genctx; /* Keygen context to use */
2871 char *keyname; /* Key name to store key or NULL */
2874 static int keygen_test_init(EVP_TEST *t, const char *alg)
2876 KEYGEN_TEST_DATA *data;
2877 EVP_PKEY_CTX *genctx;
2878 int nid = OBJ_sn2nid(alg);
2880 if (nid == NID_undef) {
2881 nid = OBJ_ln2nid(alg);
2882 if (nid == NID_undef)
2886 if (is_pkey_disabled(alg)) {
2890 if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_from_name(libctx, alg, NULL)))
2893 if (EVP_PKEY_keygen_init(genctx) <= 0) {
2894 t->err = "KEYGEN_INIT_ERROR";
2898 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2900 data->genctx = genctx;
2901 data->keyname = NULL;
2907 EVP_PKEY_CTX_free(genctx);
2911 static void keygen_test_cleanup(EVP_TEST *t)
2913 KEYGEN_TEST_DATA *keygen = t->data;
2915 EVP_PKEY_CTX_free(keygen->genctx);
2916 OPENSSL_free(keygen->keyname);
2917 OPENSSL_free(t->data);
2921 static int keygen_test_parse(EVP_TEST *t,
2922 const char *keyword, const char *value)
2924 KEYGEN_TEST_DATA *keygen = t->data;
2926 if (strcmp(keyword, "KeyName") == 0)
2927 return TEST_ptr(keygen->keyname = OPENSSL_strdup(value));
2928 if (strcmp(keyword, "Ctrl") == 0)
2929 return pkey_test_ctrl(t, keygen->genctx, value);
2933 static int keygen_test_run(EVP_TEST *t)
2935 KEYGEN_TEST_DATA *keygen = t->data;
2936 EVP_PKEY *pkey = NULL;
2939 if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) {
2940 t->err = "KEYGEN_GENERATE_ERROR";
2944 if (!evp_pkey_is_provided(pkey)) {
2945 TEST_info("Warning: legacy key generated %s", keygen->keyname);
2948 if (keygen->keyname != NULL) {
2952 if (find_key(NULL, keygen->keyname, private_keys)) {
2953 TEST_info("Duplicate key %s", keygen->keyname);
2957 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
2959 key->name = keygen->keyname;
2960 keygen->keyname = NULL;
2962 key->next = private_keys;
2966 EVP_PKEY_free(pkey);
2975 static const EVP_TEST_METHOD keygen_test_method = {
2978 keygen_test_cleanup,
2984 ** DIGEST SIGN+VERIFY TESTS
2988 int is_verify; /* Set to 1 if verifying */
2989 int is_oneshot; /* Set to 1 for one shot operation */
2990 const EVP_MD *md; /* Digest to use */
2991 EVP_MD_CTX *ctx; /* Digest context */
2993 STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
2994 unsigned char *osin; /* Input data if one shot */
2995 size_t osin_len; /* Input length data if one shot */
2996 unsigned char *output; /* Expected output */
2997 size_t output_len; /* Expected output length */
3000 static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
3003 const EVP_MD *md = NULL;
3004 DIGESTSIGN_DATA *mdat;
3006 if (strcmp(alg, "NULL") != 0) {
3007 if (is_digest_disabled(alg)) {
3011 md = EVP_get_digestbyname(alg);
3015 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
3018 if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
3022 mdat->is_verify = is_verify;
3023 mdat->is_oneshot = is_oneshot;
3028 static int digestsign_test_init(EVP_TEST *t, const char *alg)
3030 return digestsigver_test_init(t, alg, 0, 0);
3033 static void digestsigver_test_cleanup(EVP_TEST *t)
3035 DIGESTSIGN_DATA *mdata = t->data;
3037 EVP_MD_CTX_free(mdata->ctx);
3038 sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
3039 OPENSSL_free(mdata->osin);
3040 OPENSSL_free(mdata->output);
3041 OPENSSL_free(mdata);
3045 static int digestsigver_test_parse(EVP_TEST *t,
3046 const char *keyword, const char *value)
3048 DIGESTSIGN_DATA *mdata = t->data;
3050 if (strcmp(keyword, "Key") == 0) {
3051 EVP_PKEY *pkey = NULL;
3053 const char *name = mdata->md == NULL ? NULL : EVP_MD_get0_name(mdata->md);
3055 if (mdata->is_verify)
3056 rv = find_key(&pkey, value, public_keys);
3058 rv = find_key(&pkey, value, private_keys);
3059 if (rv == 0 || pkey == NULL) {
3063 if (mdata->is_verify) {
3064 if (!EVP_DigestVerifyInit_ex(mdata->ctx, &mdata->pctx, name, libctx,
3066 t->err = "DIGESTVERIFYINIT_ERROR";
3069 if (!EVP_DigestSignInit_ex(mdata->ctx, &mdata->pctx, name, libctx, NULL,
3071 t->err = "DIGESTSIGNINIT_ERROR";
3075 if (strcmp(keyword, "Input") == 0) {
3076 if (mdata->is_oneshot)
3077 return parse_bin(value, &mdata->osin, &mdata->osin_len);
3078 return evp_test_buffer_append(value, &mdata->input);
3080 if (strcmp(keyword, "Output") == 0)
3081 return parse_bin(value, &mdata->output, &mdata->output_len);
3083 if (!mdata->is_oneshot) {
3084 if (strcmp(keyword, "Count") == 0)
3085 return evp_test_buffer_set_count(value, mdata->input);
3086 if (strcmp(keyword, "Ncopy") == 0)
3087 return evp_test_buffer_ncopy(value, mdata->input);
3089 if (strcmp(keyword, "Ctrl") == 0) {
3090 if (mdata->pctx == NULL)
3092 return pkey_test_ctrl(t, mdata->pctx, value);
3097 static int digestsign_update_fn(void *ctx, const unsigned char *buf,
3100 return EVP_DigestSignUpdate(ctx, buf, buflen);
3103 static int digestsign_test_run(EVP_TEST *t)
3105 DIGESTSIGN_DATA *expected = t->data;
3106 unsigned char *got = NULL;
3109 if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
3111 t->err = "DIGESTUPDATE_ERROR";
3115 if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
3116 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
3119 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
3120 t->err = "MALLOC_FAILURE";
3123 if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
3124 t->err = "DIGESTSIGNFINAL_ERROR";
3127 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
3128 expected->output, expected->output_len,
3138 static const EVP_TEST_METHOD digestsign_test_method = {
3140 digestsign_test_init,
3141 digestsigver_test_cleanup,
3142 digestsigver_test_parse,
3146 static int digestverify_test_init(EVP_TEST *t, const char *alg)
3148 return digestsigver_test_init(t, alg, 1, 0);
3151 static int digestverify_update_fn(void *ctx, const unsigned char *buf,
3154 return EVP_DigestVerifyUpdate(ctx, buf, buflen);
3157 static int digestverify_test_run(EVP_TEST *t)
3159 DIGESTSIGN_DATA *mdata = t->data;
3161 if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
3162 t->err = "DIGESTUPDATE_ERROR";
3166 if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
3167 mdata->output_len) <= 0)
3168 t->err = "VERIFY_ERROR";
3172 static const EVP_TEST_METHOD digestverify_test_method = {
3174 digestverify_test_init,
3175 digestsigver_test_cleanup,
3176 digestsigver_test_parse,
3177 digestverify_test_run
3180 static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
3182 return digestsigver_test_init(t, alg, 0, 1);
3185 static int oneshot_digestsign_test_run(EVP_TEST *t)
3187 DIGESTSIGN_DATA *expected = t->data;
3188 unsigned char *got = NULL;
3191 if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
3192 expected->osin, expected->osin_len)) {
3193 t->err = "DIGESTSIGN_LENGTH_ERROR";
3196 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
3197 t->err = "MALLOC_FAILURE";
3200 if (!EVP_DigestSign(expected->ctx, got, &got_len,
3201 expected->osin, expected->osin_len)) {
3202 t->err = "DIGESTSIGN_ERROR";
3205 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
3206 expected->output, expected->output_len,
3216 static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
3217 "OneShotDigestSign",
3218 oneshot_digestsign_test_init,
3219 digestsigver_test_cleanup,
3220 digestsigver_test_parse,
3221 oneshot_digestsign_test_run
3224 static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
3226 return digestsigver_test_init(t, alg, 1, 1);
3229 static int oneshot_digestverify_test_run(EVP_TEST *t)
3231 DIGESTSIGN_DATA *mdata = t->data;
3233 if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
3234 mdata->osin, mdata->osin_len) <= 0)
3235 t->err = "VERIFY_ERROR";
3239 static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
3240 "OneShotDigestVerify",
3241 oneshot_digestverify_test_init,
3242 digestsigver_test_cleanup,
3243 digestsigver_test_parse,
3244 oneshot_digestverify_test_run
3249 ** PARSING AND DISPATCH
3252 static const EVP_TEST_METHOD *evp_test_list[] = {
3254 &cipher_test_method,
3255 &digest_test_method,
3256 &digestsign_test_method,
3257 &digestverify_test_method,
3258 &encode_test_method,
3260 &pkey_kdf_test_method,
3261 &keypair_test_method,
3262 &keygen_test_method,
3264 &oneshot_digestsign_test_method,
3265 &oneshot_digestverify_test_method,
3267 &pdecrypt_test_method,
3268 &pderive_test_method,
3270 &pverify_recover_test_method,
3271 &pverify_test_method,
3275 static const EVP_TEST_METHOD *find_test(const char *name)
3277 const EVP_TEST_METHOD **tt;
3279 for (tt = evp_test_list; *tt; tt++) {
3280 if (strcmp(name, (*tt)->name) == 0)
3286 static void clear_test(EVP_TEST *t)
3288 test_clearstanza(&t->s);
3290 if (t->data != NULL) {
3291 if (t->meth != NULL)
3292 t->meth->cleanup(t);
3293 OPENSSL_free(t->data);
3296 OPENSSL_free(t->expected_err);
3297 t->expected_err = NULL;
3298 OPENSSL_free(t->reason);
3307 /* Check for errors in the test structure; return 1 if okay, else 0. */
3308 static int check_test_error(EVP_TEST *t)
3313 if (t->err == NULL && t->expected_err == NULL)
3315 if (t->err != NULL && t->expected_err == NULL) {
3316 if (t->aux_err != NULL) {
3317 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
3318 t->s.test_file, t->s.start, t->aux_err, t->err);
3320 TEST_info("%s:%d: Source of above error; unexpected error %s",
3321 t->s.test_file, t->s.start, t->err);
3325 if (t->err == NULL && t->expected_err != NULL) {
3326 TEST_info("%s:%d: Succeeded but was expecting %s",
3327 t->s.test_file, t->s.start, t->expected_err);
3331 if (strcmp(t->err, t->expected_err) != 0) {
3332 TEST_info("%s:%d: Expected %s got %s",
3333 t->s.test_file, t->s.start, t->expected_err, t->err);
3337 if (t->reason == NULL)
3340 if (t->reason == NULL) {
3341 TEST_info("%s:%d: Test is missing function or reason code",
3342 t->s.test_file, t->s.start);
3346 err = ERR_peek_error();
3348 TEST_info("%s:%d: Expected error \"%s\" not set",
3349 t->s.test_file, t->s.start, t->reason);
3353 reason = ERR_reason_error_string(err);
3354 if (reason == NULL) {
3355 TEST_info("%s:%d: Expected error \"%s\", no strings available."
3357 t->s.test_file, t->s.start, t->reason);
3361 if (strcmp(reason, t->reason) == 0)
3364 TEST_info("%s:%d: Expected error \"%s\", got \"%s\"",
3365 t->s.test_file, t->s.start, t->reason, reason);
3370 /* Run a parsed test. Log a message and return 0 on error. */
3371 static int run_test(EVP_TEST *t)
3373 if (t->meth == NULL)
3380 if (t->err == NULL && t->meth->run_test(t) != 1) {
3381 TEST_info("%s:%d %s error",
3382 t->s.test_file, t->s.start, t->meth->name);
3385 if (!check_test_error(t)) {
3386 TEST_openssl_errors();
3395 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
3397 for (; lst != NULL; lst = lst->next) {
3398 if (strcmp(lst->name, name) == 0) {
3407 static void free_key_list(KEY_LIST *lst)
3409 while (lst != NULL) {
3410 KEY_LIST *next = lst->next;
3412 EVP_PKEY_free(lst->key);
3413 OPENSSL_free(lst->name);
3420 * Is the key type an unsupported algorithm?
3422 static int key_unsupported(void)
3424 long err = ERR_peek_last_error();
3425 int lib = ERR_GET_LIB(err);
3426 long reason = ERR_GET_REASON(err);
3428 if ((lib == ERR_LIB_EVP && reason == EVP_R_UNSUPPORTED_ALGORITHM)
3429 || (lib == ERR_LIB_EVP && reason == EVP_R_DECODE_ERROR)
3430 || reason == ERR_R_UNSUPPORTED) {
3434 #ifndef OPENSSL_NO_EC
3436 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
3437 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
3440 if (lib == ERR_LIB_EC
3441 && (reason == EC_R_UNKNOWN_GROUP
3442 || reason == EC_R_INVALID_CURVE)) {
3446 #endif /* OPENSSL_NO_EC */
3450 /* NULL out the value from |pp| but return it. This "steals" a pointer. */
3451 static char *take_value(PAIR *pp)
3453 char *p = pp->value;
3459 #if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
3460 static int securitycheck_enabled(void)
3462 static int enabled = -1;
3464 if (enabled == -1) {
3465 if (OSSL_PROVIDER_available(libctx, "fips")) {
3466 OSSL_PARAM params[2];
3467 OSSL_PROVIDER *prov = NULL;
3470 prov = OSSL_PROVIDER_load(libctx, "fips");
3473 OSSL_PARAM_construct_int(OSSL_PROV_PARAM_SECURITY_CHECKS,
3475 params[1] = OSSL_PARAM_construct_end();
3476 OSSL_PROVIDER_get_params(prov, params);
3477 OSSL_PROVIDER_unload(prov);
3489 * Return 1 if one of the providers named in the string is available.
3490 * The provider names are separated with whitespace.
3491 * NOTE: destructive function, it inserts '\0' after each provider name.
3493 static int prov_available(char *providers)
3499 for (; isspace(*providers); providers++)
3501 if (*providers == '\0')
3502 break; /* End of the road */
3503 for (p = providers; *p != '\0' && !isspace(*p); p++)
3509 if (OSSL_PROVIDER_available(libctx, providers))
3510 return 1; /* Found one */
3515 /* Read and parse one test. Return 0 if failure, 1 if okay. */
3516 static int parse(EVP_TEST *t)
3518 KEY_LIST *key, **klist;
3521 int i, skip_availablein = 0;
3525 if (BIO_eof(t->s.fp))
3528 if (!test_readstanza(&t->s))
3530 } while (t->s.numpairs == 0);
3531 pp = &t->s.pairs[0];
3533 /* Are we adding a key? */
3537 if (strcmp(pp->key, "PrivateKey") == 0) {
3538 pkey = PEM_read_bio_PrivateKey_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3539 if (pkey == NULL && !key_unsupported()) {
3540 EVP_PKEY_free(pkey);
3541 TEST_info("Can't read private key %s", pp->value);
3542 TEST_openssl_errors();
3545 klist = &private_keys;
3546 } else if (strcmp(pp->key, "PublicKey") == 0) {
3547 pkey = PEM_read_bio_PUBKEY_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3548 if (pkey == NULL && !key_unsupported()) {
3549 EVP_PKEY_free(pkey);
3550 TEST_info("Can't read public key %s", pp->value);
3551 TEST_openssl_errors();
3554 klist = &public_keys;
3555 } else if (strcmp(pp->key, "PrivateKeyRaw") == 0
3556 || strcmp(pp->key, "PublicKeyRaw") == 0 ) {
3557 char *strnid = NULL, *keydata = NULL;
3558 unsigned char *keybin;
3562 if (strcmp(pp->key, "PrivateKeyRaw") == 0)
3563 klist = &private_keys;
3565 klist = &public_keys;
3567 strnid = strchr(pp->value, ':');
3568 if (strnid != NULL) {
3570 keydata = strchr(strnid, ':');
3571 if (keydata != NULL)
3574 if (keydata == NULL) {
3575 TEST_info("Failed to parse %s value", pp->key);
3579 nid = OBJ_txt2nid(strnid);
3580 if (nid == NID_undef) {
3581 TEST_info("Unrecognised algorithm NID");
3584 if (!parse_bin(keydata, &keybin, &keylen)) {
3585 TEST_info("Failed to create binary key");
3588 if (klist == &private_keys)
3589 pkey = EVP_PKEY_new_raw_private_key_ex(libctx, strnid, NULL, keybin,
3592 pkey = EVP_PKEY_new_raw_public_key_ex(libctx, strnid, NULL, keybin,
3594 if (pkey == NULL && !key_unsupported()) {
3595 TEST_info("Can't read %s data", pp->key);
3596 OPENSSL_free(keybin);
3597 TEST_openssl_errors();
3600 OPENSSL_free(keybin);
3601 } else if (strcmp(pp->key, "Availablein") == 0) {
3602 if (!prov_available(pp->value)) {
3603 TEST_info("skipping, '%s' provider not available: %s:%d",
3604 pp->value, t->s.test_file, t->s.start);
3613 /* If we have a key add to list */
3614 if (klist != NULL) {
3615 if (find_key(NULL, pp->value, *klist)) {
3616 TEST_info("Duplicate key %s", pp->value);
3619 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
3621 key->name = take_value(pp);
3626 /* Go back and start a new stanza. */
3627 if ((t->s.numpairs - skip_availablein) != 1)
3628 TEST_info("Line %d: missing blank line\n", t->s.curr);
3632 /* Find the test, based on first keyword. */
3633 if (!TEST_ptr(t->meth = find_test(pp->key)))
3635 if (!t->meth->init(t, pp->value)) {
3636 TEST_error("unknown %s: %s\n", pp->key, pp->value);
3640 /* TEST_info("skipping %s %s", pp->key, pp->value); */
3644 for (pp++, i = 1; i < (t->s.numpairs - skip_availablein); pp++, i++) {
3645 if (strcmp(pp->key, "Securitycheck") == 0) {
3646 #if defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
3648 if (!securitycheck_enabled())
3651 TEST_info("skipping, Securitycheck is disabled: %s:%d",
3652 t->s.test_file, t->s.start);
3656 } else if (strcmp(pp->key, "Availablein") == 0) {
3657 TEST_info("Line %d: 'Availablein' should be the first option",
3660 } else if (strcmp(pp->key, "Result") == 0) {
3661 if (t->expected_err != NULL) {
3662 TEST_info("Line %d: multiple result lines", t->s.curr);
3665 t->expected_err = take_value(pp);
3666 } else if (strcmp(pp->key, "Function") == 0) {
3667 /* Ignore old line. */
3668 } else if (strcmp(pp->key, "Reason") == 0) {
3669 if (t->reason != NULL) {
3670 TEST_info("Line %d: multiple reason lines", t->s.curr);
3673 t->reason = take_value(pp);
3675 /* Must be test specific line: try to parse it */
3676 int rv = t->meth->parse(t, pp->key, pp->value);
3679 TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key);
3683 TEST_info("Line %d: error processing keyword %s = %s\n",
3684 t->s.curr, pp->key, pp->value);
3693 static int run_file_tests(int i)
3696 const char *testfile = test_get_argument(i);
3699 if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t))))
3701 if (!test_start_file(&t->s, testfile)) {
3706 while (!BIO_eof(t->s.fp)) {
3712 if (c == 0 || !run_test(t)) {
3717 test_end_file(&t->s);
3720 free_key_list(public_keys);
3721 free_key_list(private_keys);
3728 const OPTIONS *test_get_options(void)
3730 static const OPTIONS test_options[] = {
3731 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
3732 { "config", OPT_CONFIG_FILE, '<',
3733 "The configuration file to use for the libctx" },
3734 { OPT_HELP_STR, 1, '-', "file\tFile to run tests on.\n" },
3737 return test_options;
3740 int setup_tests(void)
3743 char *config_file = NULL;
3747 while ((o = opt_next()) != OPT_EOF) {
3749 case OPT_CONFIG_FILE:
3750 config_file = opt_arg();
3752 case OPT_TEST_CASES:
3761 * Load the provider via configuration into the created library context.
3762 * Load the 'null' provider into the default library context to ensure that
3763 * the tests do not fallback to using the default provider.
3765 if (!test_get_libctx(&libctx, &prov_null, config_file, NULL, NULL))
3768 n = test_get_argument_count();
3772 ADD_ALL_TESTS(run_file_tests, n);
3776 void cleanup_tests(void)
3778 OSSL_PROVIDER_unload(prov_null);
3779 OSSL_LIB_CTX_free(libctx);
3782 #define STR_STARTS_WITH(str, pre) strncasecmp(pre, str, strlen(pre)) == 0
3783 #define STR_ENDS_WITH(str, pre) \
3784 strlen(str) < strlen(pre) ? 0 : (strcasecmp(pre, str + strlen(str) - strlen(pre)) == 0)
3786 static int is_digest_disabled(const char *name)
3788 #ifdef OPENSSL_NO_BLAKE2
3789 if (STR_STARTS_WITH(name, "BLAKE"))
3792 #ifdef OPENSSL_NO_MD2
3793 if (strcasecmp(name, "MD2") == 0)
3796 #ifdef OPENSSL_NO_MDC2
3797 if (strcasecmp(name, "MDC2") == 0)
3800 #ifdef OPENSSL_NO_MD4
3801 if (strcasecmp(name, "MD4") == 0)
3804 #ifdef OPENSSL_NO_MD5
3805 if (strcasecmp(name, "MD5") == 0)
3808 #ifdef OPENSSL_NO_RMD160
3809 if (strcasecmp(name, "RIPEMD160") == 0)
3812 #ifdef OPENSSL_NO_SM3
3813 if (strcasecmp(name, "SM3") == 0)
3816 #ifdef OPENSSL_NO_WHIRLPOOL
3817 if (strcasecmp(name, "WHIRLPOOL") == 0)
3823 static int is_pkey_disabled(const char *name)
3825 #ifdef OPENSSL_NO_EC
3826 if (STR_STARTS_WITH(name, "EC"))
3829 #ifdef OPENSSL_NO_DH
3830 if (STR_STARTS_WITH(name, "DH"))
3833 #ifdef OPENSSL_NO_DSA
3834 if (STR_STARTS_WITH(name, "DSA"))
3840 static int is_mac_disabled(const char *name)
3842 #ifdef OPENSSL_NO_BLAKE2
3843 if (STR_STARTS_WITH(name, "BLAKE2BMAC")
3844 || STR_STARTS_WITH(name, "BLAKE2SMAC"))
3847 #ifdef OPENSSL_NO_CMAC
3848 if (STR_STARTS_WITH(name, "CMAC"))
3851 #ifdef OPENSSL_NO_POLY1305
3852 if (STR_STARTS_WITH(name, "Poly1305"))
3855 #ifdef OPENSSL_NO_SIPHASH
3856 if (STR_STARTS_WITH(name, "SipHash"))
3861 static int is_kdf_disabled(const char *name)
3863 #ifdef OPENSSL_NO_SCRYPT
3864 if (STR_ENDS_WITH(name, "SCRYPT"))
3870 static int is_cipher_disabled(const char *name)
3872 #ifdef OPENSSL_NO_ARIA
3873 if (STR_STARTS_WITH(name, "ARIA"))
3876 #ifdef OPENSSL_NO_BF
3877 if (STR_STARTS_WITH(name, "BF"))
3880 #ifdef OPENSSL_NO_CAMELLIA
3881 if (STR_STARTS_WITH(name, "CAMELLIA"))
3884 #ifdef OPENSSL_NO_CAST
3885 if (STR_STARTS_WITH(name, "CAST"))
3888 #ifdef OPENSSL_NO_CHACHA
3889 if (STR_STARTS_WITH(name, "CHACHA"))
3892 #ifdef OPENSSL_NO_POLY1305
3893 if (STR_ENDS_WITH(name, "Poly1305"))
3896 #ifdef OPENSSL_NO_DES
3897 if (STR_STARTS_WITH(name, "DES"))
3899 if (STR_ENDS_WITH(name, "3DESwrap"))
3902 #ifdef OPENSSL_NO_OCB
3903 if (STR_ENDS_WITH(name, "OCB"))
3906 #ifdef OPENSSL_NO_IDEA
3907 if (STR_STARTS_WITH(name, "IDEA"))
3910 #ifdef OPENSSL_NO_RC2
3911 if (STR_STARTS_WITH(name, "RC2"))
3914 #ifdef OPENSSL_NO_RC4
3915 if (STR_STARTS_WITH(name, "RC4"))
3918 #ifdef OPENSSL_NO_RC5
3919 if (STR_STARTS_WITH(name, "RC5"))
3922 #ifdef OPENSSL_NO_SEED
3923 if (STR_STARTS_WITH(name, "SEED"))
3926 #ifdef OPENSSL_NO_SIV
3927 if (STR_ENDS_WITH(name, "SIV"))
3930 #ifdef OPENSSL_NO_SM4
3931 if (STR_STARTS_WITH(name, "SM4"))