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 /* 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_get_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_get_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_get_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_get_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_get_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_get_iv_length(cdat->cipher)) {
926 /* IV is optional and usually omitted in wrap mode */
927 if (EVP_CIPHER_get_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_get_flags(cdat->cipher) & EVP_CIPH_FLAG_CTS) != 0)
980 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_SIV_MODE
981 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
982 || EVP_CIPHER_get_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;
1034 /* Collection of controls */
1035 STACK_OF(OPENSSL_STRING) *controls;
1042 static int mac_test_init(EVP_TEST *t, const char *alg)
1044 EVP_MAC *mac = NULL;
1045 int type = NID_undef;
1048 if (is_mac_disabled(alg)) {
1049 TEST_info("skipping, '%s' is disabled", alg);
1053 if ((mac = EVP_MAC_fetch(libctx, alg, NULL)) == NULL) {
1055 * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
1056 * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
1057 * the EVP_PKEY method.
1059 size_t sz = strlen(alg);
1060 static const char epilogue[] = " by EVP_PKEY";
1062 if (sz >= sizeof(epilogue)
1063 && strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0)
1064 sz -= sizeof(epilogue) - 1;
1066 if (strncmp(alg, "HMAC", sz) == 0)
1067 type = EVP_PKEY_HMAC;
1068 else if (strncmp(alg, "CMAC", sz) == 0)
1069 type = EVP_PKEY_CMAC;
1070 else if (strncmp(alg, "Poly1305", sz) == 0)
1071 type = EVP_PKEY_POLY1305;
1072 else if (strncmp(alg, "SipHash", sz) == 0)
1073 type = EVP_PKEY_SIPHASH;
1078 mdat = OPENSSL_zalloc(sizeof(*mdat));
1080 mdat->mac_name = OPENSSL_strdup(alg);
1082 mdat->controls = sk_OPENSSL_STRING_new_null();
1083 mdat->output_size = mdat->block_size = -1;
1088 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
1089 static void openssl_free(char *m)
1094 static void mac_test_cleanup(EVP_TEST *t)
1096 MAC_DATA *mdat = t->data;
1098 EVP_MAC_free(mdat->mac);
1099 OPENSSL_free(mdat->mac_name);
1100 sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free);
1101 OPENSSL_free(mdat->alg);
1102 OPENSSL_free(mdat->key);
1103 OPENSSL_free(mdat->iv);
1104 OPENSSL_free(mdat->custom);
1105 OPENSSL_free(mdat->salt);
1106 OPENSSL_free(mdat->input);
1107 OPENSSL_free(mdat->output);
1110 static int mac_test_parse(EVP_TEST *t,
1111 const char *keyword, const char *value)
1113 MAC_DATA *mdata = t->data;
1115 if (strcmp(keyword, "Key") == 0)
1116 return parse_bin(value, &mdata->key, &mdata->key_len);
1117 if (strcmp(keyword, "IV") == 0)
1118 return parse_bin(value, &mdata->iv, &mdata->iv_len);
1119 if (strcmp(keyword, "Custom") == 0)
1120 return parse_bin(value, &mdata->custom, &mdata->custom_len);
1121 if (strcmp(keyword, "Salt") == 0)
1122 return parse_bin(value, &mdata->salt, &mdata->salt_len);
1123 if (strcmp(keyword, "Algorithm") == 0) {
1124 mdata->alg = OPENSSL_strdup(value);
1129 if (strcmp(keyword, "Input") == 0)
1130 return parse_bin(value, &mdata->input, &mdata->input_len);
1131 if (strcmp(keyword, "Output") == 0)
1132 return parse_bin(value, &mdata->output, &mdata->output_len);
1133 if (strcmp(keyword, "XOF") == 0)
1134 return mdata->xof = 1;
1135 if (strcmp(keyword, "Ctrl") == 0)
1136 return sk_OPENSSL_STRING_push(mdata->controls,
1137 OPENSSL_strdup(value)) != 0;
1138 if (strcmp(keyword, "OutputSize") == 0) {
1139 mdata->output_size = atoi(value);
1140 if (mdata->output_size < 0)
1144 if (strcmp(keyword, "BlockSize") == 0) {
1145 mdata->block_size = atoi(value);
1146 if (mdata->block_size < 0)
1153 static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1159 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1161 p = strchr(tmpval, ':');
1164 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1167 t->err = "PKEY_CTRL_INVALID";
1169 t->err = "PKEY_CTRL_ERROR";
1172 OPENSSL_free(tmpval);
1176 static int mac_test_run_pkey(EVP_TEST *t)
1178 MAC_DATA *expected = t->data;
1179 EVP_MD_CTX *mctx = NULL;
1180 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1181 EVP_PKEY *key = NULL;
1182 const char *mdname = NULL;
1183 EVP_CIPHER *cipher = NULL;
1184 unsigned char *got = NULL;
1188 /* We don't do XOF mode via PKEY */
1192 if (expected->alg == NULL)
1193 TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type));
1195 TEST_info("Trying the EVP_PKEY %s test with %s",
1196 OBJ_nid2sn(expected->type), expected->alg);
1198 if (expected->type == EVP_PKEY_CMAC) {
1199 #ifdef OPENSSL_NO_DEPRECATED_3_0
1200 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
1205 OSSL_LIB_CTX *tmpctx;
1207 if (expected->alg != NULL && is_cipher_disabled(expected->alg)) {
1208 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
1213 if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, expected->alg, NULL))) {
1214 t->err = "MAC_KEY_CREATE_ERROR";
1217 tmpctx = OSSL_LIB_CTX_set0_default(libctx);
1218 key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len,
1220 OSSL_LIB_CTX_set0_default(tmpctx);
1223 key = EVP_PKEY_new_raw_private_key_ex(libctx,
1224 OBJ_nid2sn(expected->type), NULL,
1225 expected->key, expected->key_len);
1228 t->err = "MAC_KEY_CREATE_ERROR";
1232 if (expected->type == EVP_PKEY_HMAC && expected->alg != NULL) {
1233 if (is_digest_disabled(expected->alg)) {
1234 TEST_info("skipping, HMAC '%s' is disabled", expected->alg);
1239 mdname = expected->alg;
1241 if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
1242 t->err = "INTERNAL_ERROR";
1245 if (!EVP_DigestSignInit_ex(mctx, &pctx, mdname, libctx, NULL, key, NULL)) {
1246 t->err = "DIGESTSIGNINIT_ERROR";
1249 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++)
1250 if (!mac_test_ctrl_pkey(t, pctx,
1251 sk_OPENSSL_STRING_value(expected->controls,
1253 t->err = "EVPPKEYCTXCTRL_ERROR";
1256 if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) {
1257 t->err = "DIGESTSIGNUPDATE_ERROR";
1260 if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) {
1261 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1264 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1265 t->err = "TEST_FAILURE";
1268 if (!EVP_DigestSignFinal(mctx, got, &got_len)
1269 || !memory_err_compare(t, "TEST_MAC_ERR",
1270 expected->output, expected->output_len,
1272 t->err = "TEST_MAC_ERR";
1277 EVP_CIPHER_free(cipher);
1278 EVP_MD_CTX_free(mctx);
1280 EVP_PKEY_CTX_free(genctx);
1285 static int mac_test_run_mac(EVP_TEST *t)
1287 MAC_DATA *expected = t->data;
1288 EVP_MAC_CTX *ctx = NULL;
1289 unsigned char *got = NULL;
1291 int i, block_size = -1, output_size = -1;
1292 OSSL_PARAM params[21], sizes[3], *psizes = sizes;
1293 size_t params_n = 0;
1294 size_t params_n_allocstart = 0;
1295 const OSSL_PARAM *defined_params =
1296 EVP_MAC_settable_ctx_params(expected->mac);
1298 if (expected->alg == NULL)
1299 TEST_info("Trying the EVP_MAC %s test", expected->mac_name);
1301 TEST_info("Trying the EVP_MAC %s test with %s",
1302 expected->mac_name, expected->alg);
1304 if (expected->alg != NULL) {
1306 * The underlying algorithm may be a cipher or a digest.
1307 * We don't know which it is, but we can ask the MAC what it
1308 * should be and bet on that.
1310 if (OSSL_PARAM_locate_const(defined_params,
1311 OSSL_MAC_PARAM_CIPHER) != NULL) {
1312 params[params_n++] =
1313 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
1315 } else if (OSSL_PARAM_locate_const(defined_params,
1316 OSSL_MAC_PARAM_DIGEST) != NULL) {
1317 params[params_n++] =
1318 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
1321 t->err = "MAC_BAD_PARAMS";
1325 if (expected->custom != NULL)
1326 params[params_n++] =
1327 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
1329 expected->custom_len);
1330 if (expected->salt != NULL)
1331 params[params_n++] =
1332 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT,
1334 expected->salt_len);
1335 if (expected->iv != NULL)
1336 params[params_n++] =
1337 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
1341 /* Unknown controls. They must match parameters that the MAC recognizes */
1342 if (params_n + sk_OPENSSL_STRING_num(expected->controls)
1343 >= OSSL_NELEM(params)) {
1344 t->err = "MAC_TOO_MANY_PARAMETERS";
1347 params_n_allocstart = params_n;
1348 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) {
1349 char *tmpkey, *tmpval;
1350 char *value = sk_OPENSSL_STRING_value(expected->controls, i);
1352 if (!TEST_ptr(tmpkey = OPENSSL_strdup(value))) {
1353 t->err = "MAC_PARAM_ERROR";
1356 tmpval = strchr(tmpkey, ':');
1361 || !OSSL_PARAM_allocate_from_text(¶ms[params_n],
1364 strlen(tmpval), NULL)) {
1365 OPENSSL_free(tmpkey);
1366 t->err = "MAC_PARAM_ERROR";
1371 OPENSSL_free(tmpkey);
1373 params[params_n] = OSSL_PARAM_construct_end();
1375 if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) {
1376 t->err = "MAC_CREATE_ERROR";
1380 if (!EVP_MAC_init(ctx, expected->key, expected->key_len, params)) {
1381 t->err = "MAC_INIT_ERROR";
1384 if (expected->output_size >= 0)
1385 *psizes++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_SIZE,
1387 if (expected->block_size >= 0)
1388 *psizes++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_BLOCK_SIZE,
1390 if (psizes != sizes) {
1391 *psizes = OSSL_PARAM_construct_end();
1392 if (!TEST_true(EVP_MAC_CTX_get_params(ctx, sizes))) {
1393 t->err = "INTERNAL_ERROR";
1396 if (expected->output_size >= 0
1397 && !TEST_int_eq(output_size, expected->output_size)) {
1398 t->err = "TEST_FAILURE";
1401 if (expected->block_size >= 0
1402 && !TEST_int_eq(block_size, expected->block_size)) {
1403 t->err = "TEST_FAILURE";
1407 if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) {
1408 t->err = "MAC_UPDATE_ERROR";
1411 if (expected->xof) {
1412 if (!TEST_ptr(got = OPENSSL_malloc(expected->output_len))) {
1413 t->err = "TEST_FAILURE";
1416 if (!EVP_MAC_finalXOF(ctx, got, expected->output_len)
1417 || !memory_err_compare(t, "TEST_MAC_ERR",
1418 expected->output, expected->output_len,
1419 got, expected->output_len)) {
1420 t->err = "MAC_FINAL_ERROR";
1424 if (!EVP_MAC_final(ctx, NULL, &got_len, 0)) {
1425 t->err = "MAC_FINAL_LENGTH_ERROR";
1428 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1429 t->err = "TEST_FAILURE";
1432 if (!EVP_MAC_final(ctx, got, &got_len, got_len)
1433 || !memory_err_compare(t, "TEST_MAC_ERR",
1434 expected->output, expected->output_len,
1436 t->err = "TEST_MAC_ERR";
1442 while (params_n-- > params_n_allocstart) {
1443 OPENSSL_free(params[params_n].data);
1445 EVP_MAC_CTX_free(ctx);
1450 static int mac_test_run(EVP_TEST *t)
1452 MAC_DATA *expected = t->data;
1454 if (expected->mac != NULL)
1455 return mac_test_run_mac(t);
1456 return mac_test_run_pkey(t);
1459 static const EVP_TEST_METHOD mac_test_method = {
1470 ** These are all very similar and share much common code.
1473 typedef struct pkey_data_st {
1474 /* Context for this operation */
1476 /* Key operation to perform */
1477 int (*keyop) (EVP_PKEY_CTX *ctx,
1478 unsigned char *sig, size_t *siglen,
1479 const unsigned char *tbs, size_t tbslen);
1481 unsigned char *input;
1483 /* Expected output */
1484 unsigned char *output;
1489 * Perform public key operation setup: lookup key, allocated ctx and call
1490 * the appropriate initialisation function
1492 static int pkey_test_init(EVP_TEST *t, const char *name,
1494 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1495 int (*keyop)(EVP_PKEY_CTX *ctx,
1496 unsigned char *sig, size_t *siglen,
1497 const unsigned char *tbs,
1501 EVP_PKEY *pkey = NULL;
1505 rv = find_key(&pkey, name, public_keys);
1507 rv = find_key(&pkey, name, private_keys);
1508 if (rv == 0 || pkey == NULL) {
1509 TEST_info("skipping, key '%s' is disabled", name);
1514 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) {
1515 EVP_PKEY_free(pkey);
1518 kdata->keyop = keyop;
1519 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL))) {
1520 EVP_PKEY_free(pkey);
1521 OPENSSL_free(kdata);
1524 if (keyopinit(kdata->ctx) <= 0)
1525 t->err = "KEYOP_INIT_ERROR";
1530 static void pkey_test_cleanup(EVP_TEST *t)
1532 PKEY_DATA *kdata = t->data;
1534 OPENSSL_free(kdata->input);
1535 OPENSSL_free(kdata->output);
1536 EVP_PKEY_CTX_free(kdata->ctx);
1539 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1545 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1547 p = strchr(tmpval, ':');
1550 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1553 t->err = "PKEY_CTRL_INVALID";
1555 } else if (p != NULL && rv <= 0) {
1556 if (is_digest_disabled(p) || is_cipher_disabled(p)) {
1557 TEST_info("skipping, '%s' is disabled", p);
1561 t->err = "PKEY_CTRL_ERROR";
1565 OPENSSL_free(tmpval);
1569 static int pkey_test_parse(EVP_TEST *t,
1570 const char *keyword, const char *value)
1572 PKEY_DATA *kdata = t->data;
1573 if (strcmp(keyword, "Input") == 0)
1574 return parse_bin(value, &kdata->input, &kdata->input_len);
1575 if (strcmp(keyword, "Output") == 0)
1576 return parse_bin(value, &kdata->output, &kdata->output_len);
1577 if (strcmp(keyword, "Ctrl") == 0)
1578 return pkey_test_ctrl(t, kdata->ctx, value);
1582 static int pkey_test_run(EVP_TEST *t)
1584 PKEY_DATA *expected = t->data;
1585 unsigned char *got = NULL;
1587 EVP_PKEY_CTX *copy = NULL;
1589 if (expected->keyop(expected->ctx, NULL, &got_len,
1590 expected->input, expected->input_len) <= 0
1591 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1592 t->err = "KEYOP_LENGTH_ERROR";
1595 if (expected->keyop(expected->ctx, got, &got_len,
1596 expected->input, expected->input_len) <= 0) {
1597 t->err = "KEYOP_ERROR";
1600 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1601 expected->output, expected->output_len,
1609 /* Repeat the test on a copy. */
1610 if (!TEST_ptr(copy = EVP_PKEY_CTX_dup(expected->ctx))) {
1611 t->err = "INTERNAL_ERROR";
1614 if (expected->keyop(copy, NULL, &got_len, expected->input,
1615 expected->input_len) <= 0
1616 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1617 t->err = "KEYOP_LENGTH_ERROR";
1620 if (expected->keyop(copy, got, &got_len, expected->input,
1621 expected->input_len) <= 0) {
1622 t->err = "KEYOP_ERROR";
1625 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1626 expected->output, expected->output_len,
1632 EVP_PKEY_CTX_free(copy);
1636 static int sign_test_init(EVP_TEST *t, const char *name)
1638 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1641 static const EVP_TEST_METHOD psign_test_method = {
1649 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1651 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1652 EVP_PKEY_verify_recover);
1655 static const EVP_TEST_METHOD pverify_recover_test_method = {
1657 verify_recover_test_init,
1663 static int decrypt_test_init(EVP_TEST *t, const char *name)
1665 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1669 static const EVP_TEST_METHOD pdecrypt_test_method = {
1677 static int verify_test_init(EVP_TEST *t, const char *name)
1679 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1682 static int verify_test_run(EVP_TEST *t)
1684 PKEY_DATA *kdata = t->data;
1686 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1687 kdata->input, kdata->input_len) <= 0)
1688 t->err = "VERIFY_ERROR";
1692 static const EVP_TEST_METHOD pverify_test_method = {
1700 static int pderive_test_init(EVP_TEST *t, const char *name)
1702 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1705 static int pderive_test_parse(EVP_TEST *t,
1706 const char *keyword, const char *value)
1708 PKEY_DATA *kdata = t->data;
1711 if (strcmp(keyword, "PeerKeyValidate") == 0)
1714 if (validate || strcmp(keyword, "PeerKey") == 0) {
1716 if (find_key(&peer, value, public_keys) == 0)
1718 if (EVP_PKEY_derive_set_peer_ex(kdata->ctx, peer, validate) <= 0) {
1719 t->err = "DERIVE_SET_PEER_ERROR";
1725 if (strcmp(keyword, "SharedSecret") == 0)
1726 return parse_bin(value, &kdata->output, &kdata->output_len);
1727 if (strcmp(keyword, "Ctrl") == 0)
1728 return pkey_test_ctrl(t, kdata->ctx, value);
1732 static int pderive_test_run(EVP_TEST *t)
1734 PKEY_DATA *expected = t->data;
1735 unsigned char *got = NULL;
1738 if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) {
1739 t->err = "DERIVE_ERROR";
1742 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1743 t->err = "DERIVE_ERROR";
1746 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
1747 t->err = "DERIVE_ERROR";
1750 if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH",
1751 expected->output, expected->output_len,
1761 static const EVP_TEST_METHOD pderive_test_method = {
1774 typedef enum pbe_type_enum {
1775 PBE_TYPE_INVALID = 0,
1776 PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
1779 typedef struct pbe_data_st {
1781 /* scrypt parameters */
1782 uint64_t N, r, p, maxmem;
1783 /* PKCS#12 parameters */
1787 unsigned char *pass;
1790 unsigned char *salt;
1792 /* Expected output */
1797 #ifndef OPENSSL_NO_SCRYPT
1798 /* Parse unsigned decimal 64 bit integer value */
1799 static int parse_uint64(const char *value, uint64_t *pr)
1801 const char *p = value;
1803 if (!TEST_true(*p)) {
1804 TEST_info("Invalid empty integer value");
1807 for (*pr = 0; *p; ) {
1808 if (*pr > UINT64_MAX / 10) {
1809 TEST_error("Integer overflow in string %s", value);
1813 if (!TEST_true(isdigit((unsigned char)*p))) {
1814 TEST_error("Invalid character in string %s", value);
1823 static int scrypt_test_parse(EVP_TEST *t,
1824 const char *keyword, const char *value)
1826 PBE_DATA *pdata = t->data;
1828 if (strcmp(keyword, "N") == 0)
1829 return parse_uint64(value, &pdata->N);
1830 if (strcmp(keyword, "p") == 0)
1831 return parse_uint64(value, &pdata->p);
1832 if (strcmp(keyword, "r") == 0)
1833 return parse_uint64(value, &pdata->r);
1834 if (strcmp(keyword, "maxmem") == 0)
1835 return parse_uint64(value, &pdata->maxmem);
1840 static int pbkdf2_test_parse(EVP_TEST *t,
1841 const char *keyword, const char *value)
1843 PBE_DATA *pdata = t->data;
1845 if (strcmp(keyword, "iter") == 0) {
1846 pdata->iter = atoi(value);
1847 if (pdata->iter <= 0)
1851 if (strcmp(keyword, "MD") == 0) {
1852 pdata->md = EVP_get_digestbyname(value);
1853 if (pdata->md == NULL)
1860 static int pkcs12_test_parse(EVP_TEST *t,
1861 const char *keyword, const char *value)
1863 PBE_DATA *pdata = t->data;
1865 if (strcmp(keyword, "id") == 0) {
1866 pdata->id = atoi(value);
1871 return pbkdf2_test_parse(t, keyword, value);
1874 static int pbe_test_init(EVP_TEST *t, const char *alg)
1877 PBE_TYPE pbe_type = PBE_TYPE_INVALID;
1879 if (is_kdf_disabled(alg)) {
1880 TEST_info("skipping, '%s' is disabled", alg);
1884 if (strcmp(alg, "scrypt") == 0) {
1885 pbe_type = PBE_TYPE_SCRYPT;
1886 } else if (strcmp(alg, "pbkdf2") == 0) {
1887 pbe_type = PBE_TYPE_PBKDF2;
1888 } else if (strcmp(alg, "pkcs12") == 0) {
1889 pbe_type = PBE_TYPE_PKCS12;
1891 TEST_error("Unknown pbe algorithm %s", alg);
1893 pdat = OPENSSL_zalloc(sizeof(*pdat));
1894 pdat->pbe_type = pbe_type;
1899 static void pbe_test_cleanup(EVP_TEST *t)
1901 PBE_DATA *pdat = t->data;
1903 OPENSSL_free(pdat->pass);
1904 OPENSSL_free(pdat->salt);
1905 OPENSSL_free(pdat->key);
1908 static int pbe_test_parse(EVP_TEST *t,
1909 const char *keyword, const char *value)
1911 PBE_DATA *pdata = t->data;
1913 if (strcmp(keyword, "Password") == 0)
1914 return parse_bin(value, &pdata->pass, &pdata->pass_len);
1915 if (strcmp(keyword, "Salt") == 0)
1916 return parse_bin(value, &pdata->salt, &pdata->salt_len);
1917 if (strcmp(keyword, "Key") == 0)
1918 return parse_bin(value, &pdata->key, &pdata->key_len);
1919 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1920 return pbkdf2_test_parse(t, keyword, value);
1921 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1922 return pkcs12_test_parse(t, keyword, value);
1923 #ifndef OPENSSL_NO_SCRYPT
1924 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1925 return scrypt_test_parse(t, keyword, value);
1930 static int pbe_test_run(EVP_TEST *t)
1932 PBE_DATA *expected = t->data;
1934 EVP_MD *fetched_digest = NULL;
1935 OSSL_LIB_CTX *save_libctx;
1937 save_libctx = OSSL_LIB_CTX_set0_default(libctx);
1939 if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
1940 t->err = "INTERNAL_ERROR";
1943 if (expected->pbe_type == PBE_TYPE_PBKDF2) {
1944 if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
1945 expected->salt, expected->salt_len,
1946 expected->iter, expected->md,
1947 expected->key_len, key) == 0) {
1948 t->err = "PBKDF2_ERROR";
1951 #ifndef OPENSSL_NO_SCRYPT
1952 } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
1953 if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
1954 expected->salt, expected->salt_len,
1955 expected->N, expected->r, expected->p,
1956 expected->maxmem, key, expected->key_len) == 0) {
1957 t->err = "SCRYPT_ERROR";
1961 } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
1962 fetched_digest = EVP_MD_fetch(libctx, EVP_MD_get0_name(expected->md),
1964 if (fetched_digest == NULL) {
1965 t->err = "PKCS12_ERROR";
1968 if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
1969 expected->salt, expected->salt_len,
1970 expected->id, expected->iter, expected->key_len,
1971 key, fetched_digest) == 0) {
1972 t->err = "PKCS12_ERROR";
1976 if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len,
1977 key, expected->key_len))
1982 EVP_MD_free(fetched_digest);
1984 OSSL_LIB_CTX_set0_default(save_libctx);
1988 static const EVP_TEST_METHOD pbe_test_method = {
2002 BASE64_CANONICAL_ENCODING = 0,
2003 BASE64_VALID_ENCODING = 1,
2004 BASE64_INVALID_ENCODING = 2
2005 } base64_encoding_type;
2007 typedef struct encode_data_st {
2008 /* Input to encoding */
2009 unsigned char *input;
2011 /* Expected output */
2012 unsigned char *output;
2014 base64_encoding_type encoding;
2017 static int encode_test_init(EVP_TEST *t, const char *encoding)
2021 if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
2023 if (strcmp(encoding, "canonical") == 0) {
2024 edata->encoding = BASE64_CANONICAL_ENCODING;
2025 } else if (strcmp(encoding, "valid") == 0) {
2026 edata->encoding = BASE64_VALID_ENCODING;
2027 } else if (strcmp(encoding, "invalid") == 0) {
2028 edata->encoding = BASE64_INVALID_ENCODING;
2029 if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
2032 TEST_error("Bad encoding: %s."
2033 " Should be one of {canonical, valid, invalid}",
2040 OPENSSL_free(edata);
2044 static void encode_test_cleanup(EVP_TEST *t)
2046 ENCODE_DATA *edata = t->data;
2048 OPENSSL_free(edata->input);
2049 OPENSSL_free(edata->output);
2050 memset(edata, 0, sizeof(*edata));
2053 static int encode_test_parse(EVP_TEST *t,
2054 const char *keyword, const char *value)
2056 ENCODE_DATA *edata = t->data;
2058 if (strcmp(keyword, "Input") == 0)
2059 return parse_bin(value, &edata->input, &edata->input_len);
2060 if (strcmp(keyword, "Output") == 0)
2061 return parse_bin(value, &edata->output, &edata->output_len);
2065 static int encode_test_run(EVP_TEST *t)
2067 ENCODE_DATA *expected = t->data;
2068 unsigned char *encode_out = NULL, *decode_out = NULL;
2069 int output_len, chunk_len;
2070 EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL;
2072 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
2073 t->err = "INTERNAL_ERROR";
2077 if (expected->encoding == BASE64_CANONICAL_ENCODING) {
2079 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
2080 || !TEST_ptr(encode_out =
2081 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
2084 EVP_EncodeInit(encode_ctx);
2085 if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
2086 expected->input, expected->input_len)))
2089 output_len = chunk_len;
2091 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
2092 output_len += chunk_len;
2094 if (!memory_err_compare(t, "BAD_ENCODING",
2095 expected->output, expected->output_len,
2096 encode_out, output_len))
2100 if (!TEST_ptr(decode_out =
2101 OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
2104 EVP_DecodeInit(decode_ctx);
2105 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
2106 expected->output_len) < 0) {
2107 t->err = "DECODE_ERROR";
2110 output_len = chunk_len;
2112 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
2113 t->err = "DECODE_ERROR";
2116 output_len += chunk_len;
2118 if (expected->encoding != BASE64_INVALID_ENCODING
2119 && !memory_err_compare(t, "BAD_DECODING",
2120 expected->input, expected->input_len,
2121 decode_out, output_len)) {
2122 t->err = "BAD_DECODING";
2128 OPENSSL_free(encode_out);
2129 OPENSSL_free(decode_out);
2130 EVP_ENCODE_CTX_free(decode_ctx);
2131 EVP_ENCODE_CTX_free(encode_ctx);
2135 static const EVP_TEST_METHOD encode_test_method = {
2138 encode_test_cleanup,
2147 #define MAX_RAND_REPEATS 15
2149 typedef struct rand_data_pass_st {
2150 unsigned char *entropy;
2151 unsigned char *reseed_entropy;
2152 unsigned char *nonce;
2153 unsigned char *pers;
2154 unsigned char *reseed_addin;
2155 unsigned char *addinA;
2156 unsigned char *addinB;
2157 unsigned char *pr_entropyA;
2158 unsigned char *pr_entropyB;
2159 unsigned char *output;
2160 size_t entropy_len, nonce_len, pers_len, addinA_len, addinB_len,
2161 pr_entropyA_len, pr_entropyB_len, output_len, reseed_entropy_len,
2165 typedef struct rand_data_st {
2166 /* Context for this operation */
2168 EVP_RAND_CTX *parent;
2170 int prediction_resistance;
2172 unsigned int generate_bits;
2176 /* Expected output */
2177 RAND_DATA_PASS data[MAX_RAND_REPEATS];
2180 static int rand_test_init(EVP_TEST *t, const char *name)
2184 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
2185 unsigned int strength = 256;
2187 if (!TEST_ptr(rdata = OPENSSL_zalloc(sizeof(*rdata))))
2190 /* TEST-RAND is available in the FIPS provider but not with "fips=yes" */
2191 rand = EVP_RAND_fetch(libctx, "TEST-RAND", "-fips");
2194 rdata->parent = EVP_RAND_CTX_new(rand, NULL);
2195 EVP_RAND_free(rand);
2196 if (rdata->parent == NULL)
2199 *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength);
2200 if (!EVP_RAND_CTX_set_params(rdata->parent, params))
2203 rand = EVP_RAND_fetch(libctx, name, NULL);
2206 rdata->ctx = EVP_RAND_CTX_new(rand, rdata->parent);
2207 EVP_RAND_free(rand);
2208 if (rdata->ctx == NULL)
2215 EVP_RAND_CTX_free(rdata->parent);
2216 OPENSSL_free(rdata);
2220 static void rand_test_cleanup(EVP_TEST *t)
2222 RAND_DATA *rdata = t->data;
2225 OPENSSL_free(rdata->cipher);
2226 OPENSSL_free(rdata->digest);
2228 for (i = 0; i <= rdata->n; i++) {
2229 OPENSSL_free(rdata->data[i].entropy);
2230 OPENSSL_free(rdata->data[i].reseed_entropy);
2231 OPENSSL_free(rdata->data[i].nonce);
2232 OPENSSL_free(rdata->data[i].pers);
2233 OPENSSL_free(rdata->data[i].reseed_addin);
2234 OPENSSL_free(rdata->data[i].addinA);
2235 OPENSSL_free(rdata->data[i].addinB);
2236 OPENSSL_free(rdata->data[i].pr_entropyA);
2237 OPENSSL_free(rdata->data[i].pr_entropyB);
2238 OPENSSL_free(rdata->data[i].output);
2240 EVP_RAND_CTX_free(rdata->ctx);
2241 EVP_RAND_CTX_free(rdata->parent);
2244 static int rand_test_parse(EVP_TEST *t,
2245 const char *keyword, const char *value)
2247 RAND_DATA *rdata = t->data;
2248 RAND_DATA_PASS *item;
2252 if ((p = strchr(keyword, '.')) != NULL) {
2254 if (n >= MAX_RAND_REPEATS)
2258 item = rdata->data + n;
2259 if (strncmp(keyword, "Entropy.", sizeof("Entropy")) == 0)
2260 return parse_bin(value, &item->entropy, &item->entropy_len);
2261 if (strncmp(keyword, "ReseedEntropy.", sizeof("ReseedEntropy")) == 0)
2262 return parse_bin(value, &item->reseed_entropy,
2263 &item->reseed_entropy_len);
2264 if (strncmp(keyword, "Nonce.", sizeof("Nonce")) == 0)
2265 return parse_bin(value, &item->nonce, &item->nonce_len);
2266 if (strncmp(keyword, "PersonalisationString.",
2267 sizeof("PersonalisationString")) == 0)
2268 return parse_bin(value, &item->pers, &item->pers_len);
2269 if (strncmp(keyword, "ReseedAdditionalInput.",
2270 sizeof("ReseedAdditionalInput")) == 0)
2271 return parse_bin(value, &item->reseed_addin,
2272 &item->reseed_addin_len);
2273 if (strncmp(keyword, "AdditionalInputA.",
2274 sizeof("AdditionalInputA")) == 0)
2275 return parse_bin(value, &item->addinA, &item->addinA_len);
2276 if (strncmp(keyword, "AdditionalInputB.",
2277 sizeof("AdditionalInputB")) == 0)
2278 return parse_bin(value, &item->addinB, &item->addinB_len);
2279 if (strncmp(keyword, "EntropyPredictionResistanceA.",
2280 sizeof("EntropyPredictionResistanceA")) == 0)
2281 return parse_bin(value, &item->pr_entropyA, &item->pr_entropyA_len);
2282 if (strncmp(keyword, "EntropyPredictionResistanceB.",
2283 sizeof("EntropyPredictionResistanceB")) == 0)
2284 return parse_bin(value, &item->pr_entropyB, &item->pr_entropyB_len);
2285 if (strncmp(keyword, "Output.", sizeof("Output")) == 0)
2286 return parse_bin(value, &item->output, &item->output_len);
2288 if (strcmp(keyword, "Cipher") == 0)
2289 return TEST_ptr(rdata->cipher = OPENSSL_strdup(value));
2290 if (strcmp(keyword, "Digest") == 0)
2291 return TEST_ptr(rdata->digest = OPENSSL_strdup(value));
2292 if (strcmp(keyword, "DerivationFunction") == 0) {
2293 rdata->use_df = atoi(value) != 0;
2296 if (strcmp(keyword, "GenerateBits") == 0) {
2297 if ((n = atoi(value)) <= 0 || n % 8 != 0)
2299 rdata->generate_bits = (unsigned int)n;
2302 if (strcmp(keyword, "PredictionResistance") == 0) {
2303 rdata->prediction_resistance = atoi(value) != 0;
2310 static int rand_test_run(EVP_TEST *t)
2312 RAND_DATA *expected = t->data;
2313 RAND_DATA_PASS *item;
2315 size_t got_len = expected->generate_bits / 8;
2316 OSSL_PARAM params[5], *p = params;
2317 int i = -1, ret = 0;
2318 unsigned int strength;
2321 if (!TEST_ptr(got = OPENSSL_malloc(got_len)))
2324 *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &expected->use_df);
2325 if (expected->cipher != NULL)
2326 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
2327 expected->cipher, 0);
2328 if (expected->digest != NULL)
2329 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
2330 expected->digest, 0);
2331 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC, "HMAC", 0);
2332 *p = OSSL_PARAM_construct_end();
2333 if (!TEST_true(EVP_RAND_CTX_set_params(expected->ctx, params)))
2336 strength = EVP_RAND_get_strength(expected->ctx);
2337 for (i = 0; i <= expected->n; i++) {
2338 item = expected->data + i;
2341 z = item->entropy != NULL ? item->entropy : (unsigned char *)"";
2342 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
2343 z, item->entropy_len);
2344 z = item->nonce != NULL ? item->nonce : (unsigned char *)"";
2345 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
2346 z, item->nonce_len);
2347 *p = OSSL_PARAM_construct_end();
2348 if (!TEST_true(EVP_RAND_instantiate(expected->parent, strength,
2349 0, NULL, 0, params)))
2352 z = item->pers != NULL ? item->pers : (unsigned char *)"";
2353 if (!TEST_true(EVP_RAND_instantiate
2354 (expected->ctx, strength,
2355 expected->prediction_resistance, z,
2356 item->pers_len, NULL)))
2359 if (item->reseed_entropy != NULL) {
2360 params[0] = OSSL_PARAM_construct_octet_string
2361 (OSSL_RAND_PARAM_TEST_ENTROPY, item->reseed_entropy,
2362 item->reseed_entropy_len);
2363 params[1] = OSSL_PARAM_construct_end();
2364 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2367 if (!TEST_true(EVP_RAND_reseed
2368 (expected->ctx, expected->prediction_resistance,
2369 NULL, 0, item->reseed_addin,
2370 item->reseed_addin_len)))
2373 if (item->pr_entropyA != NULL) {
2374 params[0] = OSSL_PARAM_construct_octet_string
2375 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyA,
2376 item->pr_entropyA_len);
2377 params[1] = OSSL_PARAM_construct_end();
2378 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2381 if (!TEST_true(EVP_RAND_generate
2382 (expected->ctx, got, got_len,
2383 strength, expected->prediction_resistance,
2384 item->addinA, item->addinA_len)))
2387 if (item->pr_entropyB != NULL) {
2388 params[0] = OSSL_PARAM_construct_octet_string
2389 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyB,
2390 item->pr_entropyB_len);
2391 params[1] = OSSL_PARAM_construct_end();
2392 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2395 if (!TEST_true(EVP_RAND_generate
2396 (expected->ctx, got, got_len,
2397 strength, expected->prediction_resistance,
2398 item->addinB, item->addinB_len)))
2400 if (!TEST_mem_eq(got, got_len, item->output, item->output_len))
2402 if (!TEST_true(EVP_RAND_uninstantiate(expected->ctx))
2403 || !TEST_true(EVP_RAND_uninstantiate(expected->parent))
2404 || !TEST_true(EVP_RAND_verify_zeroization(expected->ctx))
2405 || !TEST_int_eq(EVP_RAND_get_state(expected->ctx),
2406 EVP_RAND_STATE_UNINITIALISED))
2413 if (ret == 0 && i >= 0)
2414 TEST_info("Error in test case %d of %d\n", i, expected->n + 1);
2419 static const EVP_TEST_METHOD rand_test_method = {
2431 typedef struct kdf_data_st {
2432 /* Context for this operation */
2434 /* Expected output */
2435 unsigned char *output;
2437 OSSL_PARAM params[20];
2442 * Perform public key operation setup: lookup key, allocated ctx and call
2443 * the appropriate initialisation function
2445 static int kdf_test_init(EVP_TEST *t, const char *name)
2450 if (is_kdf_disabled(name)) {
2451 TEST_info("skipping, '%s' is disabled", name);
2456 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2458 kdata->p = kdata->params;
2459 *kdata->p = OSSL_PARAM_construct_end();
2461 kdf = EVP_KDF_fetch(libctx, name, NULL);
2463 OPENSSL_free(kdata);
2466 kdata->ctx = EVP_KDF_CTX_new(kdf);
2468 if (kdata->ctx == NULL) {
2469 OPENSSL_free(kdata);
2476 static void kdf_test_cleanup(EVP_TEST *t)
2478 KDF_DATA *kdata = t->data;
2481 for (p = kdata->params; p->key != NULL; p++)
2482 OPENSSL_free(p->data);
2483 OPENSSL_free(kdata->output);
2484 EVP_KDF_CTX_free(kdata->ctx);
2487 static int kdf_test_ctrl(EVP_TEST *t, EVP_KDF_CTX *kctx,
2490 KDF_DATA *kdata = t->data;
2493 const OSSL_PARAM *defs = EVP_KDF_settable_ctx_params(EVP_KDF_CTX_kdf(kctx));
2495 if (!TEST_ptr(name = OPENSSL_strdup(value)))
2497 p = strchr(name, ':');
2501 rv = OSSL_PARAM_allocate_from_text(kdata->p, defs, name, p,
2502 p != NULL ? strlen(p) : 0, NULL);
2503 *++kdata->p = OSSL_PARAM_construct_end();
2505 t->err = "KDF_PARAM_ERROR";
2509 if (p != NULL && strcmp(name, "digest") == 0) {
2510 if (is_digest_disabled(p)) {
2511 TEST_info("skipping, '%s' is disabled", p);
2516 && (strcmp(name, "cipher") == 0
2517 || strcmp(name, "cekalg") == 0)
2518 && is_cipher_disabled(p)) {
2519 TEST_info("skipping, '%s' is disabled", p);
2526 static int kdf_test_parse(EVP_TEST *t,
2527 const char *keyword, const char *value)
2529 KDF_DATA *kdata = t->data;
2531 if (strcmp(keyword, "Output") == 0)
2532 return parse_bin(value, &kdata->output, &kdata->output_len);
2533 if (strncmp(keyword, "Ctrl", 4) == 0)
2534 return kdf_test_ctrl(t, kdata->ctx, value);
2538 static int kdf_test_run(EVP_TEST *t)
2540 KDF_DATA *expected = t->data;
2541 unsigned char *got = NULL;
2542 size_t got_len = expected->output_len;
2544 if (!EVP_KDF_CTX_set_params(expected->ctx, expected->params)) {
2545 t->err = "KDF_CTRL_ERROR";
2548 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2549 t->err = "INTERNAL_ERROR";
2552 if (EVP_KDF_derive(expected->ctx, got, got_len, NULL) <= 0) {
2553 t->err = "KDF_DERIVE_ERROR";
2556 if (!memory_err_compare(t, "KDF_MISMATCH",
2557 expected->output, expected->output_len,
2568 static const EVP_TEST_METHOD kdf_test_method = {
2580 typedef struct pkey_kdf_data_st {
2581 /* Context for this operation */
2583 /* Expected output */
2584 unsigned char *output;
2589 * Perform public key operation setup: lookup key, allocated ctx and call
2590 * the appropriate initialisation function
2592 static int pkey_kdf_test_init(EVP_TEST *t, const char *name)
2594 PKEY_KDF_DATA *kdata = NULL;
2596 if (is_kdf_disabled(name)) {
2597 TEST_info("skipping, '%s' is disabled", name);
2602 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2605 kdata->ctx = EVP_PKEY_CTX_new_from_name(libctx, name, NULL);
2606 if (kdata->ctx == NULL
2607 || EVP_PKEY_derive_init(kdata->ctx) <= 0)
2613 EVP_PKEY_CTX_free(kdata->ctx);
2614 OPENSSL_free(kdata);
2618 static void pkey_kdf_test_cleanup(EVP_TEST *t)
2620 PKEY_KDF_DATA *kdata = t->data;
2622 OPENSSL_free(kdata->output);
2623 EVP_PKEY_CTX_free(kdata->ctx);
2626 static int pkey_kdf_test_parse(EVP_TEST *t,
2627 const char *keyword, const char *value)
2629 PKEY_KDF_DATA *kdata = t->data;
2631 if (strcmp(keyword, "Output") == 0)
2632 return parse_bin(value, &kdata->output, &kdata->output_len);
2633 if (strncmp(keyword, "Ctrl", 4) == 0)
2634 return pkey_test_ctrl(t, kdata->ctx, value);
2638 static int pkey_kdf_test_run(EVP_TEST *t)
2640 PKEY_KDF_DATA *expected = t->data;
2641 unsigned char *got = NULL;
2642 size_t got_len = expected->output_len;
2644 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2645 t->err = "INTERNAL_ERROR";
2648 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
2649 t->err = "KDF_DERIVE_ERROR";
2652 if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
2653 t->err = "KDF_MISMATCH";
2663 static const EVP_TEST_METHOD pkey_kdf_test_method = {
2666 pkey_kdf_test_cleanup,
2667 pkey_kdf_test_parse,
2675 typedef struct keypair_test_data_st {
2678 } KEYPAIR_TEST_DATA;
2680 static int keypair_test_init(EVP_TEST *t, const char *pair)
2682 KEYPAIR_TEST_DATA *data;
2684 EVP_PKEY *pk = NULL, *pubk = NULL;
2685 char *pub, *priv = NULL;
2687 /* Split private and public names. */
2688 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
2689 || !TEST_ptr(pub = strchr(priv, ':'))) {
2690 t->err = "PARSING_ERROR";
2695 if (!TEST_true(find_key(&pk, priv, private_keys))) {
2696 TEST_info("Can't find private key: %s", priv);
2697 t->err = "MISSING_PRIVATE_KEY";
2700 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
2701 TEST_info("Can't find public key: %s", pub);
2702 t->err = "MISSING_PUBLIC_KEY";
2706 if (pk == NULL && pubk == NULL) {
2707 /* Both keys are listed but unsupported: skip this test */
2713 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2726 static void keypair_test_cleanup(EVP_TEST *t)
2728 OPENSSL_free(t->data);
2733 * For tests that do not accept any custom keywords.
2735 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
2740 static int keypair_test_run(EVP_TEST *t)
2743 const KEYPAIR_TEST_DATA *pair = t->data;
2745 if (pair->privk == NULL || pair->pubk == NULL) {
2747 * this can only happen if only one of the keys is not set
2748 * which means that one of them was unsupported while the
2749 * other isn't: hence a key type mismatch.
2751 t->err = "KEYPAIR_TYPE_MISMATCH";
2756 if ((rv = EVP_PKEY_eq(pair->privk, pair->pubk)) != 1 ) {
2758 t->err = "KEYPAIR_MISMATCH";
2759 } else if ( -1 == rv ) {
2760 t->err = "KEYPAIR_TYPE_MISMATCH";
2761 } else if ( -2 == rv ) {
2762 t->err = "UNSUPPORTED_KEY_COMPARISON";
2764 TEST_error("Unexpected error in key comparison");
2779 static const EVP_TEST_METHOD keypair_test_method = {
2782 keypair_test_cleanup,
2791 typedef struct keygen_test_data_st {
2792 EVP_PKEY_CTX *genctx; /* Keygen context to use */
2793 char *keyname; /* Key name to store key or NULL */
2796 static int keygen_test_init(EVP_TEST *t, const char *alg)
2798 KEYGEN_TEST_DATA *data;
2799 EVP_PKEY_CTX *genctx;
2800 int nid = OBJ_sn2nid(alg);
2802 if (nid == NID_undef) {
2803 nid = OBJ_ln2nid(alg);
2804 if (nid == NID_undef)
2808 if (is_pkey_disabled(alg)) {
2812 if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_from_name(libctx, alg, NULL)))
2815 if (EVP_PKEY_keygen_init(genctx) <= 0) {
2816 t->err = "KEYGEN_INIT_ERROR";
2820 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2822 data->genctx = genctx;
2823 data->keyname = NULL;
2829 EVP_PKEY_CTX_free(genctx);
2833 static void keygen_test_cleanup(EVP_TEST *t)
2835 KEYGEN_TEST_DATA *keygen = t->data;
2837 EVP_PKEY_CTX_free(keygen->genctx);
2838 OPENSSL_free(keygen->keyname);
2839 OPENSSL_free(t->data);
2843 static int keygen_test_parse(EVP_TEST *t,
2844 const char *keyword, const char *value)
2846 KEYGEN_TEST_DATA *keygen = t->data;
2848 if (strcmp(keyword, "KeyName") == 0)
2849 return TEST_ptr(keygen->keyname = OPENSSL_strdup(value));
2850 if (strcmp(keyword, "Ctrl") == 0)
2851 return pkey_test_ctrl(t, keygen->genctx, value);
2855 static int keygen_test_run(EVP_TEST *t)
2857 KEYGEN_TEST_DATA *keygen = t->data;
2858 EVP_PKEY *pkey = NULL;
2861 if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) {
2862 t->err = "KEYGEN_GENERATE_ERROR";
2866 if (!evp_pkey_is_provided(pkey)) {
2867 TEST_info("Warning: legacy key generated %s", keygen->keyname);
2870 if (keygen->keyname != NULL) {
2874 if (find_key(NULL, keygen->keyname, private_keys)) {
2875 TEST_info("Duplicate key %s", keygen->keyname);
2879 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
2881 key->name = keygen->keyname;
2882 keygen->keyname = NULL;
2884 key->next = private_keys;
2888 EVP_PKEY_free(pkey);
2897 static const EVP_TEST_METHOD keygen_test_method = {
2900 keygen_test_cleanup,
2906 ** DIGEST SIGN+VERIFY TESTS
2910 int is_verify; /* Set to 1 if verifying */
2911 int is_oneshot; /* Set to 1 for one shot operation */
2912 const EVP_MD *md; /* Digest to use */
2913 EVP_MD_CTX *ctx; /* Digest context */
2915 STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
2916 unsigned char *osin; /* Input data if one shot */
2917 size_t osin_len; /* Input length data if one shot */
2918 unsigned char *output; /* Expected output */
2919 size_t output_len; /* Expected output length */
2922 static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
2925 const EVP_MD *md = NULL;
2926 DIGESTSIGN_DATA *mdat;
2928 if (strcmp(alg, "NULL") != 0) {
2929 if (is_digest_disabled(alg)) {
2933 md = EVP_get_digestbyname(alg);
2937 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
2940 if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
2944 mdat->is_verify = is_verify;
2945 mdat->is_oneshot = is_oneshot;
2950 static int digestsign_test_init(EVP_TEST *t, const char *alg)
2952 return digestsigver_test_init(t, alg, 0, 0);
2955 static void digestsigver_test_cleanup(EVP_TEST *t)
2957 DIGESTSIGN_DATA *mdata = t->data;
2959 EVP_MD_CTX_free(mdata->ctx);
2960 sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
2961 OPENSSL_free(mdata->osin);
2962 OPENSSL_free(mdata->output);
2963 OPENSSL_free(mdata);
2967 static int digestsigver_test_parse(EVP_TEST *t,
2968 const char *keyword, const char *value)
2970 DIGESTSIGN_DATA *mdata = t->data;
2972 if (strcmp(keyword, "Key") == 0) {
2973 EVP_PKEY *pkey = NULL;
2975 const char *name = mdata->md == NULL ? NULL : EVP_MD_get0_name(mdata->md);
2977 if (mdata->is_verify)
2978 rv = find_key(&pkey, value, public_keys);
2980 rv = find_key(&pkey, value, private_keys);
2981 if (rv == 0 || pkey == NULL) {
2985 if (mdata->is_verify) {
2986 if (!EVP_DigestVerifyInit_ex(mdata->ctx, &mdata->pctx, name, libctx,
2988 t->err = "DIGESTVERIFYINIT_ERROR";
2991 if (!EVP_DigestSignInit_ex(mdata->ctx, &mdata->pctx, name, libctx, NULL,
2993 t->err = "DIGESTSIGNINIT_ERROR";
2997 if (strcmp(keyword, "Input") == 0) {
2998 if (mdata->is_oneshot)
2999 return parse_bin(value, &mdata->osin, &mdata->osin_len);
3000 return evp_test_buffer_append(value, &mdata->input);
3002 if (strcmp(keyword, "Output") == 0)
3003 return parse_bin(value, &mdata->output, &mdata->output_len);
3005 if (!mdata->is_oneshot) {
3006 if (strcmp(keyword, "Count") == 0)
3007 return evp_test_buffer_set_count(value, mdata->input);
3008 if (strcmp(keyword, "Ncopy") == 0)
3009 return evp_test_buffer_ncopy(value, mdata->input);
3011 if (strcmp(keyword, "Ctrl") == 0) {
3012 if (mdata->pctx == NULL)
3014 return pkey_test_ctrl(t, mdata->pctx, value);
3019 static int digestsign_update_fn(void *ctx, const unsigned char *buf,
3022 return EVP_DigestSignUpdate(ctx, buf, buflen);
3025 static int digestsign_test_run(EVP_TEST *t)
3027 DIGESTSIGN_DATA *expected = t->data;
3028 unsigned char *got = NULL;
3031 if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
3033 t->err = "DIGESTUPDATE_ERROR";
3037 if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
3038 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
3041 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
3042 t->err = "MALLOC_FAILURE";
3045 if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
3046 t->err = "DIGESTSIGNFINAL_ERROR";
3049 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
3050 expected->output, expected->output_len,
3060 static const EVP_TEST_METHOD digestsign_test_method = {
3062 digestsign_test_init,
3063 digestsigver_test_cleanup,
3064 digestsigver_test_parse,
3068 static int digestverify_test_init(EVP_TEST *t, const char *alg)
3070 return digestsigver_test_init(t, alg, 1, 0);
3073 static int digestverify_update_fn(void *ctx, const unsigned char *buf,
3076 return EVP_DigestVerifyUpdate(ctx, buf, buflen);
3079 static int digestverify_test_run(EVP_TEST *t)
3081 DIGESTSIGN_DATA *mdata = t->data;
3083 if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
3084 t->err = "DIGESTUPDATE_ERROR";
3088 if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
3089 mdata->output_len) <= 0)
3090 t->err = "VERIFY_ERROR";
3094 static const EVP_TEST_METHOD digestverify_test_method = {
3096 digestverify_test_init,
3097 digestsigver_test_cleanup,
3098 digestsigver_test_parse,
3099 digestverify_test_run
3102 static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
3104 return digestsigver_test_init(t, alg, 0, 1);
3107 static int oneshot_digestsign_test_run(EVP_TEST *t)
3109 DIGESTSIGN_DATA *expected = t->data;
3110 unsigned char *got = NULL;
3113 if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
3114 expected->osin, expected->osin_len)) {
3115 t->err = "DIGESTSIGN_LENGTH_ERROR";
3118 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
3119 t->err = "MALLOC_FAILURE";
3122 if (!EVP_DigestSign(expected->ctx, got, &got_len,
3123 expected->osin, expected->osin_len)) {
3124 t->err = "DIGESTSIGN_ERROR";
3127 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
3128 expected->output, expected->output_len,
3138 static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
3139 "OneShotDigestSign",
3140 oneshot_digestsign_test_init,
3141 digestsigver_test_cleanup,
3142 digestsigver_test_parse,
3143 oneshot_digestsign_test_run
3146 static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
3148 return digestsigver_test_init(t, alg, 1, 1);
3151 static int oneshot_digestverify_test_run(EVP_TEST *t)
3153 DIGESTSIGN_DATA *mdata = t->data;
3155 if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
3156 mdata->osin, mdata->osin_len) <= 0)
3157 t->err = "VERIFY_ERROR";
3161 static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
3162 "OneShotDigestVerify",
3163 oneshot_digestverify_test_init,
3164 digestsigver_test_cleanup,
3165 digestsigver_test_parse,
3166 oneshot_digestverify_test_run
3171 ** PARSING AND DISPATCH
3174 static const EVP_TEST_METHOD *evp_test_list[] = {
3176 &cipher_test_method,
3177 &digest_test_method,
3178 &digestsign_test_method,
3179 &digestverify_test_method,
3180 &encode_test_method,
3182 &pkey_kdf_test_method,
3183 &keypair_test_method,
3184 &keygen_test_method,
3186 &oneshot_digestsign_test_method,
3187 &oneshot_digestverify_test_method,
3189 &pdecrypt_test_method,
3190 &pderive_test_method,
3192 &pverify_recover_test_method,
3193 &pverify_test_method,
3197 static const EVP_TEST_METHOD *find_test(const char *name)
3199 const EVP_TEST_METHOD **tt;
3201 for (tt = evp_test_list; *tt; tt++) {
3202 if (strcmp(name, (*tt)->name) == 0)
3208 static void clear_test(EVP_TEST *t)
3210 test_clearstanza(&t->s);
3212 if (t->data != NULL) {
3213 if (t->meth != NULL)
3214 t->meth->cleanup(t);
3215 OPENSSL_free(t->data);
3218 OPENSSL_free(t->expected_err);
3219 t->expected_err = NULL;
3220 OPENSSL_free(t->reason);
3229 /* Check for errors in the test structure; return 1 if okay, else 0. */
3230 static int check_test_error(EVP_TEST *t)
3235 if (t->err == NULL && t->expected_err == NULL)
3237 if (t->err != NULL && t->expected_err == NULL) {
3238 if (t->aux_err != NULL) {
3239 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
3240 t->s.test_file, t->s.start, t->aux_err, t->err);
3242 TEST_info("%s:%d: Source of above error; unexpected error %s",
3243 t->s.test_file, t->s.start, t->err);
3247 if (t->err == NULL && t->expected_err != NULL) {
3248 TEST_info("%s:%d: Succeeded but was expecting %s",
3249 t->s.test_file, t->s.start, t->expected_err);
3253 if (strcmp(t->err, t->expected_err) != 0) {
3254 TEST_info("%s:%d: Expected %s got %s",
3255 t->s.test_file, t->s.start, t->expected_err, t->err);
3259 if (t->reason == NULL)
3262 if (t->reason == NULL) {
3263 TEST_info("%s:%d: Test is missing function or reason code",
3264 t->s.test_file, t->s.start);
3268 err = ERR_peek_error();
3270 TEST_info("%s:%d: Expected error \"%s\" not set",
3271 t->s.test_file, t->s.start, t->reason);
3275 reason = ERR_reason_error_string(err);
3276 if (reason == NULL) {
3277 TEST_info("%s:%d: Expected error \"%s\", no strings available."
3279 t->s.test_file, t->s.start, t->reason);
3283 if (strcmp(reason, t->reason) == 0)
3286 TEST_info("%s:%d: Expected error \"%s\", got \"%s\"",
3287 t->s.test_file, t->s.start, t->reason, reason);
3292 /* Run a parsed test. Log a message and return 0 on error. */
3293 static int run_test(EVP_TEST *t)
3295 if (t->meth == NULL)
3302 if (t->err == NULL && t->meth->run_test(t) != 1) {
3303 TEST_info("%s:%d %s error",
3304 t->s.test_file, t->s.start, t->meth->name);
3307 if (!check_test_error(t)) {
3308 TEST_openssl_errors();
3317 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
3319 for (; lst != NULL; lst = lst->next) {
3320 if (strcmp(lst->name, name) == 0) {
3329 static void free_key_list(KEY_LIST *lst)
3331 while (lst != NULL) {
3332 KEY_LIST *next = lst->next;
3334 EVP_PKEY_free(lst->key);
3335 OPENSSL_free(lst->name);
3342 * Is the key type an unsupported algorithm?
3344 static int key_unsupported(void)
3346 long err = ERR_peek_last_error();
3347 int lib = ERR_GET_LIB(err);
3348 long reason = ERR_GET_REASON(err);
3350 if ((lib == ERR_LIB_EVP && reason == EVP_R_UNSUPPORTED_ALGORITHM)
3351 || reason == ERR_R_UNSUPPORTED) {
3355 #ifndef OPENSSL_NO_EC
3357 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
3358 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
3361 if (lib == ERR_LIB_EC
3362 && (reason == EC_R_UNKNOWN_GROUP
3363 || reason == EC_R_INVALID_CURVE)) {
3367 #endif /* OPENSSL_NO_EC */
3371 /* NULL out the value from |pp| but return it. This "steals" a pointer. */
3372 static char *take_value(PAIR *pp)
3374 char *p = pp->value;
3380 #if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
3381 static int securitycheck_enabled(void)
3383 static int enabled = -1;
3385 if (enabled == -1) {
3386 if (OSSL_PROVIDER_available(libctx, "fips")) {
3387 OSSL_PARAM params[2];
3388 OSSL_PROVIDER *prov = NULL;
3391 prov = OSSL_PROVIDER_load(libctx, "fips");
3394 OSSL_PARAM_construct_int(OSSL_PROV_PARAM_SECURITY_CHECKS,
3396 params[1] = OSSL_PARAM_construct_end();
3397 OSSL_PROVIDER_get_params(prov, params);
3398 OSSL_PROVIDER_unload(prov);
3410 * Return 1 if one of the providers named in the string is available.
3411 * The provider names are separated with whitespace.
3412 * NOTE: destructive function, it inserts '\0' after each provider name.
3414 static int prov_available(char *providers)
3420 for (; isspace(*providers); providers++)
3422 if (*providers == '\0')
3423 break; /* End of the road */
3424 for (p = providers; *p != '\0' && !isspace(*p); p++)
3430 if (OSSL_PROVIDER_available(libctx, providers))
3431 return 1; /* Found one */
3436 /* Read and parse one test. Return 0 if failure, 1 if okay. */
3437 static int parse(EVP_TEST *t)
3439 KEY_LIST *key, **klist;
3442 int i, skip_availablein = 0;
3446 if (BIO_eof(t->s.fp))
3449 if (!test_readstanza(&t->s))
3451 } while (t->s.numpairs == 0);
3452 pp = &t->s.pairs[0];
3454 /* Are we adding a key? */
3458 if (strcmp(pp->key, "PrivateKey") == 0) {
3459 pkey = PEM_read_bio_PrivateKey_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3460 if (pkey == NULL && !key_unsupported()) {
3461 EVP_PKEY_free(pkey);
3462 TEST_info("Can't read private key %s", pp->value);
3463 TEST_openssl_errors();
3466 klist = &private_keys;
3467 } else if (strcmp(pp->key, "PublicKey") == 0) {
3468 pkey = PEM_read_bio_PUBKEY_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3469 if (pkey == NULL && !key_unsupported()) {
3470 EVP_PKEY_free(pkey);
3471 TEST_info("Can't read public key %s", pp->value);
3472 TEST_openssl_errors();
3475 klist = &public_keys;
3476 } else if (strcmp(pp->key, "PrivateKeyRaw") == 0
3477 || strcmp(pp->key, "PublicKeyRaw") == 0 ) {
3478 char *strnid = NULL, *keydata = NULL;
3479 unsigned char *keybin;
3483 if (strcmp(pp->key, "PrivateKeyRaw") == 0)
3484 klist = &private_keys;
3486 klist = &public_keys;
3488 strnid = strchr(pp->value, ':');
3489 if (strnid != NULL) {
3491 keydata = strchr(strnid, ':');
3492 if (keydata != NULL)
3495 if (keydata == NULL) {
3496 TEST_info("Failed to parse %s value", pp->key);
3500 nid = OBJ_txt2nid(strnid);
3501 if (nid == NID_undef) {
3502 TEST_info("Unrecognised algorithm NID");
3505 if (!parse_bin(keydata, &keybin, &keylen)) {
3506 TEST_info("Failed to create binary key");
3509 if (klist == &private_keys)
3510 pkey = EVP_PKEY_new_raw_private_key_ex(libctx, strnid, NULL, keybin,
3513 pkey = EVP_PKEY_new_raw_public_key_ex(libctx, strnid, NULL, keybin,
3515 if (pkey == NULL && !key_unsupported()) {
3516 TEST_info("Can't read %s data", pp->key);
3517 OPENSSL_free(keybin);
3518 TEST_openssl_errors();
3521 OPENSSL_free(keybin);
3522 } else if (strcmp(pp->key, "Availablein") == 0) {
3523 if (!prov_available(pp->value)) {
3524 TEST_info("skipping, '%s' provider not available: %s:%d",
3525 pp->value, t->s.test_file, t->s.start);
3534 /* If we have a key add to list */
3535 if (klist != NULL) {
3536 if (find_key(NULL, pp->value, *klist)) {
3537 TEST_info("Duplicate key %s", pp->value);
3540 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
3542 key->name = take_value(pp);
3547 /* Go back and start a new stanza. */
3548 if ((t->s.numpairs - skip_availablein) != 1)
3549 TEST_info("Line %d: missing blank line\n", t->s.curr);
3553 /* Find the test, based on first keyword. */
3554 if (!TEST_ptr(t->meth = find_test(pp->key)))
3556 if (!t->meth->init(t, pp->value)) {
3557 TEST_error("unknown %s: %s\n", pp->key, pp->value);
3561 /* TEST_info("skipping %s %s", pp->key, pp->value); */
3565 for (pp++, i = 1; i < (t->s.numpairs - skip_availablein); pp++, i++) {
3566 if (strcmp(pp->key, "Securitycheck") == 0) {
3567 #if defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
3569 if (!securitycheck_enabled())
3572 TEST_info("skipping, Securitycheck is disabled: %s:%d",
3573 t->s.test_file, t->s.start);
3577 } else if (strcmp(pp->key, "Availablein") == 0) {
3578 TEST_info("Line %d: 'Availablein' should be the first option",
3581 } else if (strcmp(pp->key, "Result") == 0) {
3582 if (t->expected_err != NULL) {
3583 TEST_info("Line %d: multiple result lines", t->s.curr);
3586 t->expected_err = take_value(pp);
3587 } else if (strcmp(pp->key, "Function") == 0) {
3588 /* Ignore old line. */
3589 } else if (strcmp(pp->key, "Reason") == 0) {
3590 if (t->reason != NULL) {
3591 TEST_info("Line %d: multiple reason lines", t->s.curr);
3594 t->reason = take_value(pp);
3596 /* Must be test specific line: try to parse it */
3597 int rv = t->meth->parse(t, pp->key, pp->value);
3600 TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key);
3604 TEST_info("Line %d: error processing keyword %s = %s\n",
3605 t->s.curr, pp->key, pp->value);
3614 static int run_file_tests(int i)
3617 const char *testfile = test_get_argument(i);
3620 if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t))))
3622 if (!test_start_file(&t->s, testfile)) {
3627 while (!BIO_eof(t->s.fp)) {
3633 if (c == 0 || !run_test(t)) {
3638 test_end_file(&t->s);
3641 free_key_list(public_keys);
3642 free_key_list(private_keys);
3649 const OPTIONS *test_get_options(void)
3651 static const OPTIONS test_options[] = {
3652 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
3653 { "config", OPT_CONFIG_FILE, '<',
3654 "The configuration file to use for the libctx" },
3655 { OPT_HELP_STR, 1, '-', "file\tFile to run tests on.\n" },
3658 return test_options;
3661 int setup_tests(void)
3664 char *config_file = NULL;
3668 while ((o = opt_next()) != OPT_EOF) {
3670 case OPT_CONFIG_FILE:
3671 config_file = opt_arg();
3673 case OPT_TEST_CASES:
3682 * Load the provider via configuration into the created library context.
3683 * Load the 'null' provider into the default library context to ensure that
3684 * the tests do not fallback to using the default provider.
3686 if (!test_get_libctx(&libctx, &prov_null, config_file, NULL, NULL))
3689 n = test_get_argument_count();
3693 ADD_ALL_TESTS(run_file_tests, n);
3697 void cleanup_tests(void)
3699 OSSL_PROVIDER_unload(prov_null);
3700 OSSL_LIB_CTX_free(libctx);
3703 #define STR_STARTS_WITH(str, pre) strncasecmp(pre, str, strlen(pre)) == 0
3704 #define STR_ENDS_WITH(str, pre) \
3705 strlen(str) < strlen(pre) ? 0 : (strcasecmp(pre, str + strlen(str) - strlen(pre)) == 0)
3707 static int is_digest_disabled(const char *name)
3709 #ifdef OPENSSL_NO_BLAKE2
3710 if (STR_STARTS_WITH(name, "BLAKE"))
3713 #ifdef OPENSSL_NO_MD2
3714 if (strcasecmp(name, "MD2") == 0)
3717 #ifdef OPENSSL_NO_MDC2
3718 if (strcasecmp(name, "MDC2") == 0)
3721 #ifdef OPENSSL_NO_MD4
3722 if (strcasecmp(name, "MD4") == 0)
3725 #ifdef OPENSSL_NO_MD5
3726 if (strcasecmp(name, "MD5") == 0)
3729 #ifdef OPENSSL_NO_RMD160
3730 if (strcasecmp(name, "RIPEMD160") == 0)
3733 #ifdef OPENSSL_NO_SM3
3734 if (strcasecmp(name, "SM3") == 0)
3737 #ifdef OPENSSL_NO_WHIRLPOOL
3738 if (strcasecmp(name, "WHIRLPOOL") == 0)
3744 static int is_pkey_disabled(const char *name)
3746 #ifdef OPENSSL_NO_EC
3747 if (STR_STARTS_WITH(name, "EC"))
3750 #ifdef OPENSSL_NO_DH
3751 if (STR_STARTS_WITH(name, "DH"))
3754 #ifdef OPENSSL_NO_DSA
3755 if (STR_STARTS_WITH(name, "DSA"))
3761 static int is_mac_disabled(const char *name)
3763 #ifdef OPENSSL_NO_BLAKE2
3764 if (STR_STARTS_WITH(name, "BLAKE2BMAC")
3765 || STR_STARTS_WITH(name, "BLAKE2SMAC"))
3768 #ifdef OPENSSL_NO_CMAC
3769 if (STR_STARTS_WITH(name, "CMAC"))
3772 #ifdef OPENSSL_NO_POLY1305
3773 if (STR_STARTS_WITH(name, "Poly1305"))
3776 #ifdef OPENSSL_NO_SIPHASH
3777 if (STR_STARTS_WITH(name, "SipHash"))
3782 static int is_kdf_disabled(const char *name)
3784 #ifdef OPENSSL_NO_SCRYPT
3785 if (STR_ENDS_WITH(name, "SCRYPT"))
3791 static int is_cipher_disabled(const char *name)
3793 #ifdef OPENSSL_NO_ARIA
3794 if (STR_STARTS_WITH(name, "ARIA"))
3797 #ifdef OPENSSL_NO_BF
3798 if (STR_STARTS_WITH(name, "BF"))
3801 #ifdef OPENSSL_NO_CAMELLIA
3802 if (STR_STARTS_WITH(name, "CAMELLIA"))
3805 #ifdef OPENSSL_NO_CAST
3806 if (STR_STARTS_WITH(name, "CAST"))
3809 #ifdef OPENSSL_NO_CHACHA
3810 if (STR_STARTS_WITH(name, "CHACHA"))
3813 #ifdef OPENSSL_NO_POLY1305
3814 if (STR_ENDS_WITH(name, "Poly1305"))
3817 #ifdef OPENSSL_NO_DES
3818 if (STR_STARTS_WITH(name, "DES"))
3820 if (STR_ENDS_WITH(name, "3DESwrap"))
3823 #ifdef OPENSSL_NO_OCB
3824 if (STR_ENDS_WITH(name, "OCB"))
3827 #ifdef OPENSSL_NO_IDEA
3828 if (STR_STARTS_WITH(name, "IDEA"))
3831 #ifdef OPENSSL_NO_RC2
3832 if (STR_STARTS_WITH(name, "RC2"))
3835 #ifdef OPENSSL_NO_RC4
3836 if (STR_STARTS_WITH(name, "RC4"))
3839 #ifdef OPENSSL_NO_RC5
3840 if (STR_STARTS_WITH(name, "RC5"))
3843 #ifdef OPENSSL_NO_SEED
3844 if (STR_STARTS_WITH(name, "SEED"))
3847 #ifdef OPENSSL_NO_SIV
3848 if (STR_ENDS_WITH(name, "SIV"))
3851 #ifdef OPENSSL_NO_SM4
3852 if (STR_STARTS_WITH(name, "SM4"))