2 * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #define OPENSSL_SUPPRESS_DEPRECATED /* EVP_PKEY_new_CMAC_key */
15 #include "../e_os.h" /* strcasecmp */
16 #include <openssl/evp.h>
17 #include <openssl/pem.h>
18 #include <openssl/err.h>
19 #include <openssl/provider.h>
20 #include <openssl/x509v3.h>
21 #include <openssl/pkcs12.h>
22 #include <openssl/kdf.h>
23 #include <openssl/params.h>
24 #include <openssl/core_names.h>
25 #include <openssl/fips_names.h>
26 #include "internal/numbers.h"
27 #include "internal/nelem.h"
28 #include "crypto/evp.h"
31 typedef struct evp_test_buffer_st EVP_TEST_BUFFER;
32 DEFINE_STACK_OF(EVP_TEST_BUFFER)
36 typedef struct evp_test_method_st EVP_TEST_METHOD;
38 /* Structure holding test information */
39 typedef struct evp_test_st {
40 STANZA s; /* Common test stanza */
42 int skip; /* Current test should be skipped */
43 const EVP_TEST_METHOD *meth; /* method for this test */
44 const char *err, *aux_err; /* Error string for test */
45 char *expected_err; /* Expected error value of test */
46 char *reason; /* Expected error reason string */
47 void *data; /* test specific data */
50 /* Test method structure */
51 struct evp_test_method_st {
52 /* Name of test as it appears in file */
54 /* Initialise test for "alg" */
55 int (*init) (EVP_TEST * t, const char *alg);
57 void (*cleanup) (EVP_TEST * t);
58 /* Test specific name value pair processing */
59 int (*parse) (EVP_TEST * t, const char *name, const char *value);
60 /* Run the test itself */
61 int (*run_test) (EVP_TEST * t);
64 /* Linked list of named keys. */
65 typedef struct key_list_st {
68 struct key_list_st *next;
71 typedef enum OPTION_choice {
78 static OSSL_PROVIDER *prov_null = NULL;
79 static OSSL_LIB_CTX *libctx = NULL;
81 /* List of public and private keys */
82 static KEY_LIST *private_keys;
83 static KEY_LIST *public_keys;
85 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst);
86 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen);
87 static int is_digest_disabled(const char *name);
88 static int is_pkey_disabled(const char *name);
89 static int is_mac_disabled(const char *name);
90 static int is_cipher_disabled(const char *name);
91 static int is_kdf_disabled(const char *name);
94 * Compare two memory regions for equality, returning zero if they differ.
95 * However, if there is expected to be an error and the actual error
96 * matches then the memory is expected to be different so handle this
97 * case without producing unnecessary test framework output.
99 static int memory_err_compare(EVP_TEST *t, const char *err,
100 const void *expected, size_t expected_len,
101 const void *got, size_t got_len)
105 if (t->expected_err != NULL && strcmp(t->expected_err, err) == 0)
106 r = !TEST_mem_ne(expected, expected_len, got, got_len);
108 r = TEST_mem_eq(expected, expected_len, got, got_len);
115 * Structure used to hold a list of blocks of memory to test
116 * calls to "update" like functions.
118 struct evp_test_buffer_st {
125 static void evp_test_buffer_free(EVP_TEST_BUFFER *db)
128 OPENSSL_free(db->buf);
133 /* append buffer to a list */
134 static int evp_test_buffer_append(const char *value,
135 STACK_OF(EVP_TEST_BUFFER) **sk)
137 EVP_TEST_BUFFER *db = NULL;
139 if (!TEST_ptr(db = OPENSSL_malloc(sizeof(*db))))
142 if (!parse_bin(value, &db->buf, &db->buflen))
147 if (*sk == NULL && !TEST_ptr(*sk = sk_EVP_TEST_BUFFER_new_null()))
149 if (!sk_EVP_TEST_BUFFER_push(*sk, db))
155 evp_test_buffer_free(db);
159 /* replace last buffer in list with copies of itself */
160 static int evp_test_buffer_ncopy(const char *value,
161 STACK_OF(EVP_TEST_BUFFER) *sk)
164 unsigned char *tbuf, *p;
166 int ncopy = atoi(value);
171 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
173 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
175 tbuflen = db->buflen * ncopy;
176 if (!TEST_ptr(tbuf = OPENSSL_malloc(tbuflen)))
178 for (i = 0, p = tbuf; i < ncopy; i++, p += db->buflen)
179 memcpy(p, db->buf, db->buflen);
181 OPENSSL_free(db->buf);
183 db->buflen = tbuflen;
187 /* set repeat count for last buffer in list */
188 static int evp_test_buffer_set_count(const char *value,
189 STACK_OF(EVP_TEST_BUFFER) *sk)
192 int count = atoi(value);
197 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
200 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
201 if (db->count_set != 0)
204 db->count = (size_t)count;
209 /* call "fn" with each element of the list in turn */
210 static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER) *sk,
212 const unsigned char *buf,
218 for (i = 0; i < sk_EVP_TEST_BUFFER_num(sk); i++) {
219 EVP_TEST_BUFFER *tb = sk_EVP_TEST_BUFFER_value(sk, i);
222 for (j = 0; j < tb->count; j++) {
223 if (fn(ctx, tb->buf, tb->buflen) <= 0)
231 * Unescape some sequences in string literals (only \n for now).
232 * Return an allocated buffer, set |out_len|. If |input_len|
233 * is zero, get an empty buffer but set length to zero.
235 static unsigned char* unescape(const char *input, size_t input_len,
238 unsigned char *ret, *p;
241 if (input_len == 0) {
243 return OPENSSL_zalloc(1);
246 /* Escaping is non-expanding; over-allocate original size for simplicity. */
247 if (!TEST_ptr(ret = p = OPENSSL_malloc(input_len)))
250 for (i = 0; i < input_len; i++) {
251 if (*input == '\\') {
252 if (i == input_len - 1 || *++input != 'n') {
253 TEST_error("Bad escape sequence in file");
273 * For a hex string "value" convert to a binary allocated buffer.
274 * Return 1 on success or 0 on failure.
276 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen)
280 /* Check for NULL literal */
281 if (strcmp(value, "NULL") == 0) {
287 /* Check for empty value */
288 if (*value == '\0') {
290 * Don't return NULL for zero length buffer. This is needed for
291 * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key
292 * buffer even if the key length is 0, in order to detect key reset.
294 *buf = OPENSSL_malloc(1);
302 /* Check for string literal */
303 if (value[0] == '"') {
304 size_t vlen = strlen(++value);
306 if (vlen == 0 || value[vlen - 1] != '"')
309 *buf = unescape(value, vlen, buflen);
310 return *buf == NULL ? 0 : 1;
313 /* Otherwise assume as hex literal and convert it to binary buffer */
314 if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) {
315 TEST_info("Can't convert %s", value);
316 TEST_openssl_errors();
319 /* Size of input buffer means we'll never overflow */
325 ** MESSAGE DIGEST TESTS
328 typedef struct digest_data_st {
329 /* Digest this test is for */
330 const EVP_MD *digest;
331 EVP_MD *fetched_digest;
332 /* Input to digest */
333 STACK_OF(EVP_TEST_BUFFER) *input;
334 /* Expected output */
335 unsigned char *output;
341 static int digest_test_init(EVP_TEST *t, const char *alg)
344 const EVP_MD *digest;
345 EVP_MD *fetched_digest;
347 if (is_digest_disabled(alg)) {
348 TEST_info("skipping, '%s' is disabled", alg);
353 if ((digest = fetched_digest = EVP_MD_fetch(libctx, alg, NULL)) == NULL
354 && (digest = EVP_get_digestbyname(alg)) == NULL)
356 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
359 mdat->digest = digest;
360 mdat->fetched_digest = fetched_digest;
362 if (fetched_digest != NULL)
363 TEST_info("%s is fetched", alg);
367 static void digest_test_cleanup(EVP_TEST *t)
369 DIGEST_DATA *mdat = t->data;
371 sk_EVP_TEST_BUFFER_pop_free(mdat->input, evp_test_buffer_free);
372 OPENSSL_free(mdat->output);
373 EVP_MD_free(mdat->fetched_digest);
376 static int digest_test_parse(EVP_TEST *t,
377 const char *keyword, const char *value)
379 DIGEST_DATA *mdata = t->data;
381 if (strcmp(keyword, "Input") == 0)
382 return evp_test_buffer_append(value, &mdata->input);
383 if (strcmp(keyword, "Output") == 0)
384 return parse_bin(value, &mdata->output, &mdata->output_len);
385 if (strcmp(keyword, "Count") == 0)
386 return evp_test_buffer_set_count(value, mdata->input);
387 if (strcmp(keyword, "Ncopy") == 0)
388 return evp_test_buffer_ncopy(value, mdata->input);
389 if (strcmp(keyword, "Padding") == 0)
390 return (mdata->pad_type = atoi(value)) > 0;
394 static int digest_update_fn(void *ctx, const unsigned char *buf, size_t buflen)
396 return EVP_DigestUpdate(ctx, buf, buflen);
399 static int digest_test_run(EVP_TEST *t)
401 DIGEST_DATA *expected = t->data;
403 unsigned char *got = NULL;
404 unsigned int got_len;
405 OSSL_PARAM params[2];
407 t->err = "TEST_FAILURE";
408 if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
411 got = OPENSSL_malloc(expected->output_len > EVP_MAX_MD_SIZE ?
412 expected->output_len : EVP_MAX_MD_SIZE);
416 if (!EVP_DigestInit_ex(mctx, expected->digest, NULL)) {
417 t->err = "DIGESTINIT_ERROR";
420 if (expected->pad_type > 0) {
421 params[0] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_PAD_TYPE,
422 &expected->pad_type);
423 params[1] = OSSL_PARAM_construct_end();
424 if (!TEST_int_gt(EVP_MD_CTX_set_params(mctx, params), 0)) {
425 t->err = "PARAMS_ERROR";
429 if (!evp_test_buffer_do(expected->input, digest_update_fn, mctx)) {
430 t->err = "DIGESTUPDATE_ERROR";
434 if (EVP_MD_flags(expected->digest) & EVP_MD_FLAG_XOF) {
435 EVP_MD_CTX *mctx_cpy;
436 char dont[] = "touch";
438 if (!TEST_ptr(mctx_cpy = EVP_MD_CTX_new())) {
441 if (!EVP_MD_CTX_copy(mctx_cpy, mctx)) {
442 EVP_MD_CTX_free(mctx_cpy);
445 if (!EVP_DigestFinalXOF(mctx_cpy, (unsigned char *)dont, 0)) {
446 EVP_MD_CTX_free(mctx_cpy);
447 t->err = "DIGESTFINALXOF_ERROR";
450 if (!TEST_str_eq(dont, "touch")) {
451 EVP_MD_CTX_free(mctx_cpy);
452 t->err = "DIGESTFINALXOF_ERROR";
455 EVP_MD_CTX_free(mctx_cpy);
457 got_len = expected->output_len;
458 if (!EVP_DigestFinalXOF(mctx, got, got_len)) {
459 t->err = "DIGESTFINALXOF_ERROR";
463 if (!EVP_DigestFinal(mctx, got, &got_len)) {
464 t->err = "DIGESTFINAL_ERROR";
468 if (!TEST_int_eq(expected->output_len, got_len)) {
469 t->err = "DIGEST_LENGTH_MISMATCH";
472 if (!memory_err_compare(t, "DIGEST_MISMATCH",
473 expected->output, expected->output_len,
481 EVP_MD_CTX_free(mctx);
485 static const EVP_TEST_METHOD digest_test_method = {
497 typedef struct cipher_data_st {
498 const EVP_CIPHER *cipher;
499 EVP_CIPHER *fetched_cipher;
501 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
505 size_t key_bits; /* Used by RC2 */
509 unsigned char *plaintext;
510 size_t plaintext_len;
511 unsigned char *ciphertext;
512 size_t ciphertext_len;
513 /* GCM, CCM, OCB and SIV only */
514 unsigned char *aad[AAD_NUM];
515 size_t aad_len[AAD_NUM];
517 const char *cts_mode;
522 static int cipher_test_init(EVP_TEST *t, const char *alg)
524 const EVP_CIPHER *cipher;
525 EVP_CIPHER *fetched_cipher;
529 if (is_cipher_disabled(alg)) {
531 TEST_info("skipping, '%s' is disabled", alg);
535 if ((cipher = fetched_cipher = EVP_CIPHER_fetch(libctx, alg, NULL)) == NULL
536 && (cipher = EVP_get_cipherbyname(alg)) == NULL)
539 cdat = OPENSSL_zalloc(sizeof(*cdat));
540 cdat->cipher = cipher;
541 cdat->fetched_cipher = fetched_cipher;
543 m = EVP_CIPHER_mode(cipher);
544 if (m == EVP_CIPH_GCM_MODE
545 || m == EVP_CIPH_OCB_MODE
546 || m == EVP_CIPH_SIV_MODE
547 || m == EVP_CIPH_CCM_MODE)
549 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
555 if (fetched_cipher != NULL)
556 TEST_info("%s is fetched", alg);
560 static void cipher_test_cleanup(EVP_TEST *t)
563 CIPHER_DATA *cdat = t->data;
565 OPENSSL_free(cdat->key);
566 OPENSSL_free(cdat->iv);
567 OPENSSL_free(cdat->ciphertext);
568 OPENSSL_free(cdat->plaintext);
569 for (i = 0; i < AAD_NUM; i++)
570 OPENSSL_free(cdat->aad[i]);
571 OPENSSL_free(cdat->tag);
572 EVP_CIPHER_free(cdat->fetched_cipher);
575 static int cipher_test_parse(EVP_TEST *t, const char *keyword,
578 CIPHER_DATA *cdat = t->data;
581 if (strcmp(keyword, "Key") == 0)
582 return parse_bin(value, &cdat->key, &cdat->key_len);
583 if (strcmp(keyword, "Rounds") == 0) {
587 cdat->rounds = (unsigned int)i;
590 if (strcmp(keyword, "IV") == 0)
591 return parse_bin(value, &cdat->iv, &cdat->iv_len);
592 if (strcmp(keyword, "Plaintext") == 0)
593 return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len);
594 if (strcmp(keyword, "Ciphertext") == 0)
595 return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
596 if (strcmp(keyword, "KeyBits") == 0) {
600 cdat->key_bits = (size_t)i;
604 if (strcmp(keyword, "AAD") == 0) {
605 for (i = 0; i < AAD_NUM; i++) {
606 if (cdat->aad[i] == NULL)
607 return parse_bin(value, &cdat->aad[i], &cdat->aad_len[i]);
611 if (strcmp(keyword, "Tag") == 0)
612 return parse_bin(value, &cdat->tag, &cdat->tag_len);
613 if (strcmp(keyword, "SetTagLate") == 0) {
614 if (strcmp(value, "TRUE") == 0)
616 else if (strcmp(value, "FALSE") == 0)
624 if (strcmp(keyword, "Operation") == 0) {
625 if (strcmp(value, "ENCRYPT") == 0)
627 else if (strcmp(value, "DECRYPT") == 0)
633 if (strcmp(keyword, "CTSMode") == 0) {
634 cdat->cts_mode = value;
640 static int cipher_test_enc(EVP_TEST *t, int enc,
641 size_t out_misalign, size_t inp_misalign, int frag)
643 CIPHER_DATA *expected = t->data;
644 unsigned char *in, *expected_out, *tmp = NULL;
645 size_t in_len, out_len, donelen = 0;
646 int ok = 0, tmplen, chunklen, tmpflen, i;
647 EVP_CIPHER_CTX *ctx_base = NULL;
648 EVP_CIPHER_CTX *ctx = NULL;
650 t->err = "TEST_FAILURE";
651 if (!TEST_ptr(ctx_base = EVP_CIPHER_CTX_new()))
653 if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
655 EVP_CIPHER_CTX_set_flags(ctx_base, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
657 in = expected->plaintext;
658 in_len = expected->plaintext_len;
659 expected_out = expected->ciphertext;
660 out_len = expected->ciphertext_len;
662 in = expected->ciphertext;
663 in_len = expected->ciphertext_len;
664 expected_out = expected->plaintext;
665 out_len = expected->plaintext_len;
667 if (inp_misalign == (size_t)-1) {
668 /* Exercise in-place encryption */
669 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
672 in = memcpy(tmp + out_misalign, in, in_len);
674 inp_misalign += 16 - ((out_misalign + in_len) & 15);
676 * 'tmp' will store both output and copy of input. We make the copy
677 * of input to specifically aligned part of 'tmp'. So we just
678 * figured out how much padding would ensure the required alignment,
679 * now we allocate extended buffer and finally copy the input just
680 * past inp_misalign in expression below. Output will be written
681 * past out_misalign...
683 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
684 inp_misalign + in_len);
687 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
688 inp_misalign, in, in_len);
690 if (!EVP_CipherInit_ex(ctx_base, expected->cipher, NULL, NULL, NULL, enc)) {
691 t->err = "CIPHERINIT_ERROR";
694 if (expected->cts_mode != NULL) {
695 OSSL_PARAM params[2];
697 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE,
698 (char *)expected->cts_mode,
700 params[1] = OSSL_PARAM_construct_end();
701 if (!EVP_CIPHER_CTX_set_params(ctx_base, params)) {
702 t->err = "INVALID_CTS_MODE";
707 if (expected->aead) {
708 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_IVLEN,
709 expected->iv_len, 0)) {
710 t->err = "INVALID_IV_LENGTH";
713 } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx_base)) {
714 t->err = "INVALID_IV_LENGTH";
718 if (expected->aead) {
721 * If encrypting or OCB just set tag length initially, otherwise
722 * set tag length and value.
724 if (enc || expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late) {
725 t->err = "TAG_LENGTH_SET_ERROR";
728 t->err = "TAG_SET_ERROR";
731 if (tag || expected->aead != EVP_CIPH_GCM_MODE) {
732 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_TAG,
733 expected->tag_len, tag))
738 if (expected->rounds > 0) {
739 int rounds = (int)expected->rounds;
741 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC5_ROUNDS, rounds, NULL)) {
742 t->err = "INVALID_ROUNDS";
747 if (!EVP_CIPHER_CTX_set_key_length(ctx_base, expected->key_len)) {
748 t->err = "INVALID_KEY_LENGTH";
751 if (expected->key_bits > 0) {
752 int bits = (int)expected->key_bits;
754 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC2_KEY_BITS, bits, NULL)) {
755 t->err = "INVALID KEY BITS";
759 if (!EVP_CipherInit_ex(ctx_base, NULL, NULL, expected->key, expected->iv, -1)) {
760 t->err = "KEY_SET_ERROR";
764 /* Check that we get the same IV back */
765 if (expected->iv != NULL) {
766 /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
767 unsigned char iv[128];
768 if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx_base, iv, sizeof(iv)))
769 || ((EVP_CIPHER_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
770 && !TEST_mem_eq(expected->iv, expected->iv_len, iv,
771 expected->iv_len))) {
772 t->err = "INVALID_IV";
777 /* Test that the cipher dup functions correctly if it is supported */
778 if (EVP_CIPHER_CTX_copy(ctx, ctx_base)) {
779 EVP_CIPHER_CTX_free(ctx_base);
782 EVP_CIPHER_CTX_free(ctx);
786 if (expected->aead == EVP_CIPH_CCM_MODE) {
787 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
788 t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
792 if (expected->aad[0] != NULL) {
793 t->err = "AAD_SET_ERROR";
795 for (i = 0; expected->aad[i] != NULL; i++) {
796 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i],
797 expected->aad_len[i]))
802 * Supply the AAD in chunks less than the block size where possible
804 for (i = 0; expected->aad[i] != NULL; i++) {
805 if (expected->aad_len[i] > 0) {
806 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], 1))
810 if (expected->aad_len[i] > 2) {
811 if (!EVP_CipherUpdate(ctx, NULL, &chunklen,
812 expected->aad[i] + donelen,
813 expected->aad_len[i] - 2))
815 donelen += expected->aad_len[i] - 2;
817 if (expected->aad_len[i] > 1
818 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
819 expected->aad[i] + donelen, 1))
825 if (!enc && (expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late)) {
826 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
827 expected->tag_len, expected->tag)) {
828 t->err = "TAG_SET_ERROR";
833 EVP_CIPHER_CTX_set_padding(ctx, 0);
834 t->err = "CIPHERUPDATE_ERROR";
837 /* We supply the data all in one go */
838 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
841 /* Supply the data in chunks less than the block size where possible */
843 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
850 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
858 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
864 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
865 t->err = "CIPHERFINAL_ERROR";
868 if (!memory_err_compare(t, "VALUE_MISMATCH", expected_out, out_len,
869 tmp + out_misalign, tmplen + tmpflen))
871 if (enc && expected->aead) {
872 unsigned char rtag[16];
874 if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) {
875 t->err = "TAG_LENGTH_INTERNAL_ERROR";
878 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
879 expected->tag_len, rtag)) {
880 t->err = "TAG_RETRIEVE_ERROR";
883 if (!memory_err_compare(t, "TAG_VALUE_MISMATCH",
884 expected->tag, expected->tag_len,
885 rtag, expected->tag_len))
893 EVP_CIPHER_CTX_free(ctx_base);
894 EVP_CIPHER_CTX_free(ctx);
898 static int cipher_test_run(EVP_TEST *t)
900 CIPHER_DATA *cdat = t->data;
902 size_t out_misalign, inp_misalign;
908 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
909 /* IV is optional and usually omitted in wrap mode */
910 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
915 if (cdat->aead && !cdat->tag) {
919 for (out_misalign = 0; out_misalign <= 1;) {
920 static char aux_err[64];
921 t->aux_err = aux_err;
922 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
923 if (inp_misalign == (size_t)-1) {
924 /* kludge: inp_misalign == -1 means "exercise in-place" */
925 BIO_snprintf(aux_err, sizeof(aux_err),
926 "%s in-place, %sfragmented",
927 out_misalign ? "misaligned" : "aligned",
930 BIO_snprintf(aux_err, sizeof(aux_err),
931 "%s output and %s input, %sfragmented",
932 out_misalign ? "misaligned" : "aligned",
933 inp_misalign ? "misaligned" : "aligned",
937 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
938 /* Not fatal errors: return */
945 if (cdat->enc != 1) {
946 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
947 /* Not fatal errors: return */
956 if (out_misalign == 1 && frag == 0) {
958 * XTS, SIV, CCM and Wrap modes have special requirements about input
959 * lengths so we don't fragment for those
961 if (cdat->aead == EVP_CIPH_CCM_MODE
962 || ((EVP_CIPHER_flags(cdat->cipher) & EVP_CIPH_FLAG_CTS) != 0)
963 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_SIV_MODE
964 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
965 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
978 static const EVP_TEST_METHOD cipher_test_method = {
991 typedef struct mac_data_st {
992 /* MAC type in one form or another */
994 EVP_MAC *mac; /* for mac_test_run_mac */
995 int type; /* for mac_test_run_pkey */
996 /* Algorithm string for this MAC */
1005 unsigned char *input;
1007 /* Expected output */
1008 unsigned char *output;
1010 unsigned char *custom;
1012 /* MAC salt (blake2) */
1013 unsigned char *salt;
1015 /* Collection of controls */
1016 STACK_OF(OPENSSL_STRING) *controls;
1019 static int mac_test_init(EVP_TEST *t, const char *alg)
1021 EVP_MAC *mac = NULL;
1022 int type = NID_undef;
1025 if (is_mac_disabled(alg)) {
1026 TEST_info("skipping, '%s' is disabled", alg);
1030 if ((mac = EVP_MAC_fetch(libctx, alg, NULL)) == NULL) {
1032 * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
1033 * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
1034 * the EVP_PKEY method.
1036 size_t sz = strlen(alg);
1037 static const char epilogue[] = " by EVP_PKEY";
1039 if (sz >= sizeof(epilogue)
1040 && strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0)
1041 sz -= sizeof(epilogue) - 1;
1043 if (strncmp(alg, "HMAC", sz) == 0)
1044 type = EVP_PKEY_HMAC;
1045 else if (strncmp(alg, "CMAC", sz) == 0)
1046 type = EVP_PKEY_CMAC;
1047 else if (strncmp(alg, "Poly1305", sz) == 0)
1048 type = EVP_PKEY_POLY1305;
1049 else if (strncmp(alg, "SipHash", sz) == 0)
1050 type = EVP_PKEY_SIPHASH;
1055 mdat = OPENSSL_zalloc(sizeof(*mdat));
1057 mdat->mac_name = OPENSSL_strdup(alg);
1059 mdat->controls = sk_OPENSSL_STRING_new_null();
1064 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
1065 static void openssl_free(char *m)
1070 static void mac_test_cleanup(EVP_TEST *t)
1072 MAC_DATA *mdat = t->data;
1074 EVP_MAC_free(mdat->mac);
1075 OPENSSL_free(mdat->mac_name);
1076 sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free);
1077 OPENSSL_free(mdat->alg);
1078 OPENSSL_free(mdat->key);
1079 OPENSSL_free(mdat->iv);
1080 OPENSSL_free(mdat->custom);
1081 OPENSSL_free(mdat->salt);
1082 OPENSSL_free(mdat->input);
1083 OPENSSL_free(mdat->output);
1086 static int mac_test_parse(EVP_TEST *t,
1087 const char *keyword, const char *value)
1089 MAC_DATA *mdata = t->data;
1091 if (strcmp(keyword, "Key") == 0)
1092 return parse_bin(value, &mdata->key, &mdata->key_len);
1093 if (strcmp(keyword, "IV") == 0)
1094 return parse_bin(value, &mdata->iv, &mdata->iv_len);
1095 if (strcmp(keyword, "Custom") == 0)
1096 return parse_bin(value, &mdata->custom, &mdata->custom_len);
1097 if (strcmp(keyword, "Salt") == 0)
1098 return parse_bin(value, &mdata->salt, &mdata->salt_len);
1099 if (strcmp(keyword, "Algorithm") == 0) {
1100 mdata->alg = OPENSSL_strdup(value);
1105 if (strcmp(keyword, "Input") == 0)
1106 return parse_bin(value, &mdata->input, &mdata->input_len);
1107 if (strcmp(keyword, "Output") == 0)
1108 return parse_bin(value, &mdata->output, &mdata->output_len);
1109 if (strcmp(keyword, "Ctrl") == 0)
1110 return sk_OPENSSL_STRING_push(mdata->controls,
1111 OPENSSL_strdup(value)) != 0;
1115 static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1121 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1123 p = strchr(tmpval, ':');
1126 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1129 t->err = "PKEY_CTRL_INVALID";
1131 t->err = "PKEY_CTRL_ERROR";
1134 OPENSSL_free(tmpval);
1138 static int mac_test_run_pkey(EVP_TEST *t)
1140 MAC_DATA *expected = t->data;
1141 EVP_MD_CTX *mctx = NULL;
1142 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1143 EVP_PKEY *key = NULL;
1144 const char *mdname = NULL;
1145 EVP_CIPHER *cipher = NULL;
1146 unsigned char *got = NULL;
1150 if (expected->alg == NULL)
1151 TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type));
1153 TEST_info("Trying the EVP_PKEY %s test with %s",
1154 OBJ_nid2sn(expected->type), expected->alg);
1156 if (expected->type == EVP_PKEY_CMAC) {
1157 #ifdef OPENSSL_NO_DEPRECATED_3_0
1158 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
1163 OSSL_LIB_CTX *tmpctx;
1165 if (expected->alg != NULL && is_cipher_disabled(expected->alg)) {
1166 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
1171 if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, expected->alg, NULL))) {
1172 t->err = "MAC_KEY_CREATE_ERROR";
1175 tmpctx = OSSL_LIB_CTX_set0_default(libctx);
1176 key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len,
1178 OSSL_LIB_CTX_set0_default(tmpctx);
1181 key = EVP_PKEY_new_raw_private_key_ex(libctx,
1182 OBJ_nid2sn(expected->type), NULL,
1183 expected->key, expected->key_len);
1186 t->err = "MAC_KEY_CREATE_ERROR";
1190 if (expected->type == EVP_PKEY_HMAC && expected->alg != NULL) {
1191 if (is_digest_disabled(expected->alg)) {
1192 TEST_info("skipping, HMAC '%s' is disabled", expected->alg);
1197 mdname = expected->alg;
1199 if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
1200 t->err = "INTERNAL_ERROR";
1203 if (!EVP_DigestSignInit_ex(mctx, &pctx, mdname, libctx, NULL, key, NULL)) {
1204 t->err = "DIGESTSIGNINIT_ERROR";
1207 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++)
1208 if (!mac_test_ctrl_pkey(t, pctx,
1209 sk_OPENSSL_STRING_value(expected->controls,
1211 t->err = "EVPPKEYCTXCTRL_ERROR";
1214 if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) {
1215 t->err = "DIGESTSIGNUPDATE_ERROR";
1218 if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) {
1219 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1222 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1223 t->err = "TEST_FAILURE";
1226 if (!EVP_DigestSignFinal(mctx, got, &got_len)
1227 || !memory_err_compare(t, "TEST_MAC_ERR",
1228 expected->output, expected->output_len,
1230 t->err = "TEST_MAC_ERR";
1235 EVP_CIPHER_free(cipher);
1236 EVP_MD_CTX_free(mctx);
1238 EVP_PKEY_CTX_free(genctx);
1243 static int mac_test_run_mac(EVP_TEST *t)
1245 MAC_DATA *expected = t->data;
1246 EVP_MAC_CTX *ctx = NULL;
1247 unsigned char *got = NULL;
1250 OSSL_PARAM params[21];
1251 size_t params_n = 0;
1252 size_t params_n_allocstart = 0;
1253 const OSSL_PARAM *defined_params =
1254 EVP_MAC_settable_ctx_params(expected->mac);
1256 if (expected->alg == NULL)
1257 TEST_info("Trying the EVP_MAC %s test", expected->mac_name);
1259 TEST_info("Trying the EVP_MAC %s test with %s",
1260 expected->mac_name, expected->alg);
1262 if (expected->alg != NULL) {
1264 * The underlying algorithm may be a cipher or a digest.
1265 * We don't know which it is, but we can ask the MAC what it
1266 * should be and bet on that.
1268 if (OSSL_PARAM_locate_const(defined_params,
1269 OSSL_MAC_PARAM_CIPHER) != NULL) {
1270 params[params_n++] =
1271 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
1273 } else if (OSSL_PARAM_locate_const(defined_params,
1274 OSSL_MAC_PARAM_DIGEST) != NULL) {
1275 params[params_n++] =
1276 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
1279 t->err = "MAC_BAD_PARAMS";
1283 if (expected->custom != NULL)
1284 params[params_n++] =
1285 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
1287 expected->custom_len);
1288 if (expected->salt != NULL)
1289 params[params_n++] =
1290 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT,
1292 expected->salt_len);
1293 if (expected->iv != NULL)
1294 params[params_n++] =
1295 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
1299 /* Unknown controls. They must match parameters that the MAC recognizes */
1300 if (params_n + sk_OPENSSL_STRING_num(expected->controls)
1301 >= OSSL_NELEM(params)) {
1302 t->err = "MAC_TOO_MANY_PARAMETERS";
1305 params_n_allocstart = params_n;
1306 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) {
1307 char *tmpkey, *tmpval;
1308 char *value = sk_OPENSSL_STRING_value(expected->controls, i);
1310 if (!TEST_ptr(tmpkey = OPENSSL_strdup(value))) {
1311 t->err = "MAC_PARAM_ERROR";
1314 tmpval = strchr(tmpkey, ':');
1319 || !OSSL_PARAM_allocate_from_text(¶ms[params_n],
1322 strlen(tmpval), NULL)) {
1323 OPENSSL_free(tmpkey);
1324 t->err = "MAC_PARAM_ERROR";
1329 OPENSSL_free(tmpkey);
1331 params[params_n] = OSSL_PARAM_construct_end();
1333 if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) {
1334 t->err = "MAC_CREATE_ERROR";
1338 if (!EVP_MAC_init(ctx, expected->key, expected->key_len, params)) {
1339 t->err = "MAC_INIT_ERROR";
1342 if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) {
1343 t->err = "MAC_UPDATE_ERROR";
1346 if (!EVP_MAC_final(ctx, NULL, &got_len, 0)) {
1347 t->err = "MAC_FINAL_LENGTH_ERROR";
1350 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1351 t->err = "TEST_FAILURE";
1354 if (!EVP_MAC_final(ctx, got, &got_len, got_len)
1355 || !memory_err_compare(t, "TEST_MAC_ERR",
1356 expected->output, expected->output_len,
1358 t->err = "TEST_MAC_ERR";
1363 while (params_n-- > params_n_allocstart) {
1364 OPENSSL_free(params[params_n].data);
1366 EVP_MAC_CTX_free(ctx);
1371 static int mac_test_run(EVP_TEST *t)
1373 MAC_DATA *expected = t->data;
1375 if (expected->mac != NULL)
1376 return mac_test_run_mac(t);
1377 return mac_test_run_pkey(t);
1380 static const EVP_TEST_METHOD mac_test_method = {
1391 ** These are all very similar and share much common code.
1394 typedef struct pkey_data_st {
1395 /* Context for this operation */
1397 /* Key operation to perform */
1398 int (*keyop) (EVP_PKEY_CTX *ctx,
1399 unsigned char *sig, size_t *siglen,
1400 const unsigned char *tbs, size_t tbslen);
1402 unsigned char *input;
1404 /* Expected output */
1405 unsigned char *output;
1410 * Perform public key operation setup: lookup key, allocated ctx and call
1411 * the appropriate initialisation function
1413 static int pkey_test_init(EVP_TEST *t, const char *name,
1415 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1416 int (*keyop)(EVP_PKEY_CTX *ctx,
1417 unsigned char *sig, size_t *siglen,
1418 const unsigned char *tbs,
1422 EVP_PKEY *pkey = NULL;
1426 rv = find_key(&pkey, name, public_keys);
1428 rv = find_key(&pkey, name, private_keys);
1429 if (rv == 0 || pkey == NULL) {
1430 TEST_info("skipping, key '%s' is disabled", name);
1435 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) {
1436 EVP_PKEY_free(pkey);
1439 kdata->keyop = keyop;
1440 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL))) {
1441 EVP_PKEY_free(pkey);
1442 OPENSSL_free(kdata);
1445 if (keyopinit(kdata->ctx) <= 0)
1446 t->err = "KEYOP_INIT_ERROR";
1451 static void pkey_test_cleanup(EVP_TEST *t)
1453 PKEY_DATA *kdata = t->data;
1455 OPENSSL_free(kdata->input);
1456 OPENSSL_free(kdata->output);
1457 EVP_PKEY_CTX_free(kdata->ctx);
1460 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1466 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1468 p = strchr(tmpval, ':');
1471 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1474 t->err = "PKEY_CTRL_INVALID";
1476 } else if (p != NULL && rv <= 0) {
1477 if (is_digest_disabled(p) || is_cipher_disabled(p)) {
1478 TEST_info("skipping, '%s' is disabled", p);
1482 t->err = "PKEY_CTRL_ERROR";
1486 OPENSSL_free(tmpval);
1490 static int pkey_test_parse(EVP_TEST *t,
1491 const char *keyword, const char *value)
1493 PKEY_DATA *kdata = t->data;
1494 if (strcmp(keyword, "Input") == 0)
1495 return parse_bin(value, &kdata->input, &kdata->input_len);
1496 if (strcmp(keyword, "Output") == 0)
1497 return parse_bin(value, &kdata->output, &kdata->output_len);
1498 if (strcmp(keyword, "Ctrl") == 0)
1499 return pkey_test_ctrl(t, kdata->ctx, value);
1503 static int pkey_test_run(EVP_TEST *t)
1505 PKEY_DATA *expected = t->data;
1506 unsigned char *got = NULL;
1508 EVP_PKEY_CTX *copy = NULL;
1510 if (expected->keyop(expected->ctx, NULL, &got_len,
1511 expected->input, expected->input_len) <= 0
1512 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1513 t->err = "KEYOP_LENGTH_ERROR";
1516 if (expected->keyop(expected->ctx, got, &got_len,
1517 expected->input, expected->input_len) <= 0) {
1518 t->err = "KEYOP_ERROR";
1521 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1522 expected->output, expected->output_len,
1530 /* Repeat the test on a copy. */
1531 if (!TEST_ptr(copy = EVP_PKEY_CTX_dup(expected->ctx))) {
1532 t->err = "INTERNAL_ERROR";
1535 if (expected->keyop(copy, NULL, &got_len, expected->input,
1536 expected->input_len) <= 0
1537 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1538 t->err = "KEYOP_LENGTH_ERROR";
1541 if (expected->keyop(copy, got, &got_len, expected->input,
1542 expected->input_len) <= 0) {
1543 t->err = "KEYOP_ERROR";
1546 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1547 expected->output, expected->output_len,
1553 EVP_PKEY_CTX_free(copy);
1557 static int sign_test_init(EVP_TEST *t, const char *name)
1559 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1562 static const EVP_TEST_METHOD psign_test_method = {
1570 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1572 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1573 EVP_PKEY_verify_recover);
1576 static const EVP_TEST_METHOD pverify_recover_test_method = {
1578 verify_recover_test_init,
1584 static int decrypt_test_init(EVP_TEST *t, const char *name)
1586 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1590 static const EVP_TEST_METHOD pdecrypt_test_method = {
1598 static int verify_test_init(EVP_TEST *t, const char *name)
1600 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1603 static int verify_test_run(EVP_TEST *t)
1605 PKEY_DATA *kdata = t->data;
1607 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1608 kdata->input, kdata->input_len) <= 0)
1609 t->err = "VERIFY_ERROR";
1613 static const EVP_TEST_METHOD pverify_test_method = {
1621 static int pderive_test_init(EVP_TEST *t, const char *name)
1623 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1626 static int pderive_test_parse(EVP_TEST *t,
1627 const char *keyword, const char *value)
1629 PKEY_DATA *kdata = t->data;
1632 if (strcmp(keyword, "PeerKeyValidate") == 0)
1635 if (validate || strcmp(keyword, "PeerKey") == 0) {
1637 if (find_key(&peer, value, public_keys) == 0)
1639 if (EVP_PKEY_derive_set_peer_ex(kdata->ctx, peer, validate) <= 0) {
1640 t->err = "DERIVE_SET_PEER_ERROR";
1646 if (strcmp(keyword, "SharedSecret") == 0)
1647 return parse_bin(value, &kdata->output, &kdata->output_len);
1648 if (strcmp(keyword, "Ctrl") == 0)
1649 return pkey_test_ctrl(t, kdata->ctx, value);
1653 static int pderive_test_run(EVP_TEST *t)
1655 PKEY_DATA *expected = t->data;
1656 unsigned char *got = NULL;
1659 if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) {
1660 t->err = "DERIVE_ERROR";
1663 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1664 t->err = "DERIVE_ERROR";
1667 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
1668 t->err = "DERIVE_ERROR";
1671 if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH",
1672 expected->output, expected->output_len,
1682 static const EVP_TEST_METHOD pderive_test_method = {
1695 typedef enum pbe_type_enum {
1696 PBE_TYPE_INVALID = 0,
1697 PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
1700 typedef struct pbe_data_st {
1702 /* scrypt parameters */
1703 uint64_t N, r, p, maxmem;
1704 /* PKCS#12 parameters */
1708 unsigned char *pass;
1711 unsigned char *salt;
1713 /* Expected output */
1718 #ifndef OPENSSL_NO_SCRYPT
1719 /* Parse unsigned decimal 64 bit integer value */
1720 static int parse_uint64(const char *value, uint64_t *pr)
1722 const char *p = value;
1724 if (!TEST_true(*p)) {
1725 TEST_info("Invalid empty integer value");
1728 for (*pr = 0; *p; ) {
1729 if (*pr > UINT64_MAX / 10) {
1730 TEST_error("Integer overflow in string %s", value);
1734 if (!TEST_true(isdigit((unsigned char)*p))) {
1735 TEST_error("Invalid character in string %s", value);
1744 static int scrypt_test_parse(EVP_TEST *t,
1745 const char *keyword, const char *value)
1747 PBE_DATA *pdata = t->data;
1749 if (strcmp(keyword, "N") == 0)
1750 return parse_uint64(value, &pdata->N);
1751 if (strcmp(keyword, "p") == 0)
1752 return parse_uint64(value, &pdata->p);
1753 if (strcmp(keyword, "r") == 0)
1754 return parse_uint64(value, &pdata->r);
1755 if (strcmp(keyword, "maxmem") == 0)
1756 return parse_uint64(value, &pdata->maxmem);
1761 static int pbkdf2_test_parse(EVP_TEST *t,
1762 const char *keyword, const char *value)
1764 PBE_DATA *pdata = t->data;
1766 if (strcmp(keyword, "iter") == 0) {
1767 pdata->iter = atoi(value);
1768 if (pdata->iter <= 0)
1772 if (strcmp(keyword, "MD") == 0) {
1773 pdata->md = EVP_get_digestbyname(value);
1774 if (pdata->md == NULL)
1781 static int pkcs12_test_parse(EVP_TEST *t,
1782 const char *keyword, const char *value)
1784 PBE_DATA *pdata = t->data;
1786 if (strcmp(keyword, "id") == 0) {
1787 pdata->id = atoi(value);
1792 return pbkdf2_test_parse(t, keyword, value);
1795 static int pbe_test_init(EVP_TEST *t, const char *alg)
1798 PBE_TYPE pbe_type = PBE_TYPE_INVALID;
1800 if (is_kdf_disabled(alg)) {
1801 TEST_info("skipping, '%s' is disabled", alg);
1805 if (strcmp(alg, "scrypt") == 0) {
1806 pbe_type = PBE_TYPE_SCRYPT;
1807 } else if (strcmp(alg, "pbkdf2") == 0) {
1808 pbe_type = PBE_TYPE_PBKDF2;
1809 } else if (strcmp(alg, "pkcs12") == 0) {
1810 pbe_type = PBE_TYPE_PKCS12;
1812 TEST_error("Unknown pbe algorithm %s", alg);
1814 pdat = OPENSSL_zalloc(sizeof(*pdat));
1815 pdat->pbe_type = pbe_type;
1820 static void pbe_test_cleanup(EVP_TEST *t)
1822 PBE_DATA *pdat = t->data;
1824 OPENSSL_free(pdat->pass);
1825 OPENSSL_free(pdat->salt);
1826 OPENSSL_free(pdat->key);
1829 static int pbe_test_parse(EVP_TEST *t,
1830 const char *keyword, const char *value)
1832 PBE_DATA *pdata = t->data;
1834 if (strcmp(keyword, "Password") == 0)
1835 return parse_bin(value, &pdata->pass, &pdata->pass_len);
1836 if (strcmp(keyword, "Salt") == 0)
1837 return parse_bin(value, &pdata->salt, &pdata->salt_len);
1838 if (strcmp(keyword, "Key") == 0)
1839 return parse_bin(value, &pdata->key, &pdata->key_len);
1840 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1841 return pbkdf2_test_parse(t, keyword, value);
1842 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1843 return pkcs12_test_parse(t, keyword, value);
1844 #ifndef OPENSSL_NO_SCRYPT
1845 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1846 return scrypt_test_parse(t, keyword, value);
1851 static int pbe_test_run(EVP_TEST *t)
1853 PBE_DATA *expected = t->data;
1855 EVP_MD *fetched_digest = NULL;
1856 OSSL_LIB_CTX *save_libctx;
1858 save_libctx = OSSL_LIB_CTX_set0_default(libctx);
1860 if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
1861 t->err = "INTERNAL_ERROR";
1864 if (expected->pbe_type == PBE_TYPE_PBKDF2) {
1865 if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
1866 expected->salt, expected->salt_len,
1867 expected->iter, expected->md,
1868 expected->key_len, key) == 0) {
1869 t->err = "PBKDF2_ERROR";
1872 #ifndef OPENSSL_NO_SCRYPT
1873 } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
1874 if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
1875 expected->salt, expected->salt_len,
1876 expected->N, expected->r, expected->p,
1877 expected->maxmem, key, expected->key_len) == 0) {
1878 t->err = "SCRYPT_ERROR";
1882 } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
1883 fetched_digest = EVP_MD_fetch(libctx, EVP_MD_name(expected->md), NULL);
1884 if (fetched_digest == NULL) {
1885 t->err = "PKCS12_ERROR";
1888 if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
1889 expected->salt, expected->salt_len,
1890 expected->id, expected->iter, expected->key_len,
1891 key, fetched_digest) == 0) {
1892 t->err = "PKCS12_ERROR";
1896 if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len,
1897 key, expected->key_len))
1902 EVP_MD_free(fetched_digest);
1904 OSSL_LIB_CTX_set0_default(save_libctx);
1908 static const EVP_TEST_METHOD pbe_test_method = {
1922 BASE64_CANONICAL_ENCODING = 0,
1923 BASE64_VALID_ENCODING = 1,
1924 BASE64_INVALID_ENCODING = 2
1925 } base64_encoding_type;
1927 typedef struct encode_data_st {
1928 /* Input to encoding */
1929 unsigned char *input;
1931 /* Expected output */
1932 unsigned char *output;
1934 base64_encoding_type encoding;
1937 static int encode_test_init(EVP_TEST *t, const char *encoding)
1941 if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
1943 if (strcmp(encoding, "canonical") == 0) {
1944 edata->encoding = BASE64_CANONICAL_ENCODING;
1945 } else if (strcmp(encoding, "valid") == 0) {
1946 edata->encoding = BASE64_VALID_ENCODING;
1947 } else if (strcmp(encoding, "invalid") == 0) {
1948 edata->encoding = BASE64_INVALID_ENCODING;
1949 if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
1952 TEST_error("Bad encoding: %s."
1953 " Should be one of {canonical, valid, invalid}",
1960 OPENSSL_free(edata);
1964 static void encode_test_cleanup(EVP_TEST *t)
1966 ENCODE_DATA *edata = t->data;
1968 OPENSSL_free(edata->input);
1969 OPENSSL_free(edata->output);
1970 memset(edata, 0, sizeof(*edata));
1973 static int encode_test_parse(EVP_TEST *t,
1974 const char *keyword, const char *value)
1976 ENCODE_DATA *edata = t->data;
1978 if (strcmp(keyword, "Input") == 0)
1979 return parse_bin(value, &edata->input, &edata->input_len);
1980 if (strcmp(keyword, "Output") == 0)
1981 return parse_bin(value, &edata->output, &edata->output_len);
1985 static int encode_test_run(EVP_TEST *t)
1987 ENCODE_DATA *expected = t->data;
1988 unsigned char *encode_out = NULL, *decode_out = NULL;
1989 int output_len, chunk_len;
1990 EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL;
1992 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
1993 t->err = "INTERNAL_ERROR";
1997 if (expected->encoding == BASE64_CANONICAL_ENCODING) {
1999 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
2000 || !TEST_ptr(encode_out =
2001 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
2004 EVP_EncodeInit(encode_ctx);
2005 if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
2006 expected->input, expected->input_len)))
2009 output_len = chunk_len;
2011 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
2012 output_len += chunk_len;
2014 if (!memory_err_compare(t, "BAD_ENCODING",
2015 expected->output, expected->output_len,
2016 encode_out, output_len))
2020 if (!TEST_ptr(decode_out =
2021 OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
2024 EVP_DecodeInit(decode_ctx);
2025 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
2026 expected->output_len) < 0) {
2027 t->err = "DECODE_ERROR";
2030 output_len = chunk_len;
2032 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
2033 t->err = "DECODE_ERROR";
2036 output_len += chunk_len;
2038 if (expected->encoding != BASE64_INVALID_ENCODING
2039 && !memory_err_compare(t, "BAD_DECODING",
2040 expected->input, expected->input_len,
2041 decode_out, output_len)) {
2042 t->err = "BAD_DECODING";
2048 OPENSSL_free(encode_out);
2049 OPENSSL_free(decode_out);
2050 EVP_ENCODE_CTX_free(decode_ctx);
2051 EVP_ENCODE_CTX_free(encode_ctx);
2055 static const EVP_TEST_METHOD encode_test_method = {
2058 encode_test_cleanup,
2067 #define MAX_RAND_REPEATS 15
2069 typedef struct rand_data_pass_st {
2070 unsigned char *entropy;
2071 unsigned char *reseed_entropy;
2072 unsigned char *nonce;
2073 unsigned char *pers;
2074 unsigned char *reseed_addin;
2075 unsigned char *addinA;
2076 unsigned char *addinB;
2077 unsigned char *pr_entropyA;
2078 unsigned char *pr_entropyB;
2079 unsigned char *output;
2080 size_t entropy_len, nonce_len, pers_len, addinA_len, addinB_len,
2081 pr_entropyA_len, pr_entropyB_len, output_len, reseed_entropy_len,
2085 typedef struct rand_data_st {
2086 /* Context for this operation */
2088 EVP_RAND_CTX *parent;
2090 int prediction_resistance;
2092 unsigned int generate_bits;
2096 /* Expected output */
2097 RAND_DATA_PASS data[MAX_RAND_REPEATS];
2100 static int rand_test_init(EVP_TEST *t, const char *name)
2104 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
2105 unsigned int strength = 256;
2107 if (!TEST_ptr(rdata = OPENSSL_zalloc(sizeof(*rdata))))
2110 /* TEST-RAND is available in the FIPS provider but not with "fips=yes" */
2111 rand = EVP_RAND_fetch(libctx, "TEST-RAND", "-fips");
2114 rdata->parent = EVP_RAND_CTX_new(rand, NULL);
2115 EVP_RAND_free(rand);
2116 if (rdata->parent == NULL)
2119 *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength);
2120 if (!EVP_RAND_CTX_set_params(rdata->parent, params))
2123 rand = EVP_RAND_fetch(libctx, name, NULL);
2126 rdata->ctx = EVP_RAND_CTX_new(rand, rdata->parent);
2127 EVP_RAND_free(rand);
2128 if (rdata->ctx == NULL)
2135 EVP_RAND_CTX_free(rdata->parent);
2136 OPENSSL_free(rdata);
2140 static void rand_test_cleanup(EVP_TEST *t)
2142 RAND_DATA *rdata = t->data;
2145 OPENSSL_free(rdata->cipher);
2146 OPENSSL_free(rdata->digest);
2148 for (i = 0; i <= rdata->n; i++) {
2149 OPENSSL_free(rdata->data[i].entropy);
2150 OPENSSL_free(rdata->data[i].reseed_entropy);
2151 OPENSSL_free(rdata->data[i].nonce);
2152 OPENSSL_free(rdata->data[i].pers);
2153 OPENSSL_free(rdata->data[i].reseed_addin);
2154 OPENSSL_free(rdata->data[i].addinA);
2155 OPENSSL_free(rdata->data[i].addinB);
2156 OPENSSL_free(rdata->data[i].pr_entropyA);
2157 OPENSSL_free(rdata->data[i].pr_entropyB);
2158 OPENSSL_free(rdata->data[i].output);
2160 EVP_RAND_CTX_free(rdata->ctx);
2161 EVP_RAND_CTX_free(rdata->parent);
2164 static int rand_test_parse(EVP_TEST *t,
2165 const char *keyword, const char *value)
2167 RAND_DATA *rdata = t->data;
2168 RAND_DATA_PASS *item;
2172 if ((p = strchr(keyword, '.')) != NULL) {
2174 if (n >= MAX_RAND_REPEATS)
2178 item = rdata->data + n;
2179 if (strncmp(keyword, "Entropy.", sizeof("Entropy")) == 0)
2180 return parse_bin(value, &item->entropy, &item->entropy_len);
2181 if (strncmp(keyword, "ReseedEntropy.", sizeof("ReseedEntropy")) == 0)
2182 return parse_bin(value, &item->reseed_entropy,
2183 &item->reseed_entropy_len);
2184 if (strncmp(keyword, "Nonce.", sizeof("Nonce")) == 0)
2185 return parse_bin(value, &item->nonce, &item->nonce_len);
2186 if (strncmp(keyword, "PersonalisationString.",
2187 sizeof("PersonalisationString")) == 0)
2188 return parse_bin(value, &item->pers, &item->pers_len);
2189 if (strncmp(keyword, "ReseedAdditionalInput.",
2190 sizeof("ReseedAdditionalInput")) == 0)
2191 return parse_bin(value, &item->reseed_addin,
2192 &item->reseed_addin_len);
2193 if (strncmp(keyword, "AdditionalInputA.",
2194 sizeof("AdditionalInputA")) == 0)
2195 return parse_bin(value, &item->addinA, &item->addinA_len);
2196 if (strncmp(keyword, "AdditionalInputB.",
2197 sizeof("AdditionalInputB")) == 0)
2198 return parse_bin(value, &item->addinB, &item->addinB_len);
2199 if (strncmp(keyword, "EntropyPredictionResistanceA.",
2200 sizeof("EntropyPredictionResistanceA")) == 0)
2201 return parse_bin(value, &item->pr_entropyA, &item->pr_entropyA_len);
2202 if (strncmp(keyword, "EntropyPredictionResistanceB.",
2203 sizeof("EntropyPredictionResistanceB")) == 0)
2204 return parse_bin(value, &item->pr_entropyB, &item->pr_entropyB_len);
2205 if (strncmp(keyword, "Output.", sizeof("Output")) == 0)
2206 return parse_bin(value, &item->output, &item->output_len);
2208 if (strcmp(keyword, "Cipher") == 0)
2209 return TEST_ptr(rdata->cipher = OPENSSL_strdup(value));
2210 if (strcmp(keyword, "Digest") == 0)
2211 return TEST_ptr(rdata->digest = OPENSSL_strdup(value));
2212 if (strcmp(keyword, "DerivationFunction") == 0) {
2213 rdata->use_df = atoi(value) != 0;
2216 if (strcmp(keyword, "GenerateBits") == 0) {
2217 if ((n = atoi(value)) <= 0 || n % 8 != 0)
2219 rdata->generate_bits = (unsigned int)n;
2222 if (strcmp(keyword, "PredictionResistance") == 0) {
2223 rdata->prediction_resistance = atoi(value) != 0;
2230 static int rand_test_run(EVP_TEST *t)
2232 RAND_DATA *expected = t->data;
2233 RAND_DATA_PASS *item;
2235 size_t got_len = expected->generate_bits / 8;
2236 OSSL_PARAM params[5], *p = params;
2237 int i = -1, ret = 0;
2238 unsigned int strength;
2241 if (!TEST_ptr(got = OPENSSL_malloc(got_len)))
2244 *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &expected->use_df);
2245 if (expected->cipher != NULL)
2246 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
2247 expected->cipher, 0);
2248 if (expected->digest != NULL)
2249 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
2250 expected->digest, 0);
2251 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC, "HMAC", 0);
2252 *p = OSSL_PARAM_construct_end();
2253 if (!TEST_true(EVP_RAND_CTX_set_params(expected->ctx, params)))
2256 strength = EVP_RAND_strength(expected->ctx);
2257 for (i = 0; i <= expected->n; i++) {
2258 item = expected->data + i;
2261 z = item->entropy != NULL ? item->entropy : (unsigned char *)"";
2262 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
2263 z, item->entropy_len);
2264 z = item->nonce != NULL ? item->nonce : (unsigned char *)"";
2265 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
2266 z, item->nonce_len);
2267 *p = OSSL_PARAM_construct_end();
2268 if (!TEST_true(EVP_RAND_instantiate(expected->parent, strength,
2269 0, NULL, 0, params)))
2272 z = item->pers != NULL ? item->pers : (unsigned char *)"";
2273 if (!TEST_true(EVP_RAND_instantiate
2274 (expected->ctx, strength,
2275 expected->prediction_resistance, z,
2276 item->pers_len, NULL)))
2279 if (item->reseed_entropy != NULL) {
2280 params[0] = OSSL_PARAM_construct_octet_string
2281 (OSSL_RAND_PARAM_TEST_ENTROPY, item->reseed_entropy,
2282 item->reseed_entropy_len);
2283 params[1] = OSSL_PARAM_construct_end();
2284 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2287 if (!TEST_true(EVP_RAND_reseed
2288 (expected->ctx, expected->prediction_resistance,
2289 NULL, 0, item->reseed_addin,
2290 item->reseed_addin_len)))
2293 if (item->pr_entropyA != NULL) {
2294 params[0] = OSSL_PARAM_construct_octet_string
2295 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyA,
2296 item->pr_entropyA_len);
2297 params[1] = OSSL_PARAM_construct_end();
2298 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2301 if (!TEST_true(EVP_RAND_generate
2302 (expected->ctx, got, got_len,
2303 strength, expected->prediction_resistance,
2304 item->addinA, item->addinA_len)))
2307 if (item->pr_entropyB != NULL) {
2308 params[0] = OSSL_PARAM_construct_octet_string
2309 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyB,
2310 item->pr_entropyB_len);
2311 params[1] = OSSL_PARAM_construct_end();
2312 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2315 if (!TEST_true(EVP_RAND_generate
2316 (expected->ctx, got, got_len,
2317 strength, expected->prediction_resistance,
2318 item->addinB, item->addinB_len)))
2320 if (!TEST_mem_eq(got, got_len, item->output, item->output_len))
2322 if (!TEST_true(EVP_RAND_uninstantiate(expected->ctx))
2323 || !TEST_true(EVP_RAND_uninstantiate(expected->parent))
2324 || !TEST_true(EVP_RAND_verify_zeroization(expected->ctx))
2325 || !TEST_int_eq(EVP_RAND_state(expected->ctx),
2326 EVP_RAND_STATE_UNINITIALISED))
2333 if (ret == 0 && i >= 0)
2334 TEST_info("Error in test case %d of %d\n", i, expected->n + 1);
2339 static const EVP_TEST_METHOD rand_test_method = {
2351 typedef struct kdf_data_st {
2352 /* Context for this operation */
2354 /* Expected output */
2355 unsigned char *output;
2357 OSSL_PARAM params[20];
2362 * Perform public key operation setup: lookup key, allocated ctx and call
2363 * the appropriate initialisation function
2365 static int kdf_test_init(EVP_TEST *t, const char *name)
2370 if (is_kdf_disabled(name)) {
2371 TEST_info("skipping, '%s' is disabled", name);
2376 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2378 kdata->p = kdata->params;
2379 *kdata->p = OSSL_PARAM_construct_end();
2381 kdf = EVP_KDF_fetch(libctx, name, NULL);
2383 OPENSSL_free(kdata);
2386 kdata->ctx = EVP_KDF_CTX_new(kdf);
2388 if (kdata->ctx == NULL) {
2389 OPENSSL_free(kdata);
2396 static void kdf_test_cleanup(EVP_TEST *t)
2398 KDF_DATA *kdata = t->data;
2401 for (p = kdata->params; p->key != NULL; p++)
2402 OPENSSL_free(p->data);
2403 OPENSSL_free(kdata->output);
2404 EVP_KDF_CTX_free(kdata->ctx);
2407 static int kdf_test_ctrl(EVP_TEST *t, EVP_KDF_CTX *kctx,
2410 KDF_DATA *kdata = t->data;
2413 const OSSL_PARAM *defs = EVP_KDF_settable_ctx_params(EVP_KDF_CTX_kdf(kctx));
2415 if (!TEST_ptr(name = OPENSSL_strdup(value)))
2417 p = strchr(name, ':');
2421 rv = OSSL_PARAM_allocate_from_text(kdata->p, defs, name, p,
2422 p != NULL ? strlen(p) : 0, NULL);
2423 *++kdata->p = OSSL_PARAM_construct_end();
2425 t->err = "KDF_PARAM_ERROR";
2429 if (p != NULL && strcmp(name, "digest") == 0) {
2430 if (is_digest_disabled(p)) {
2431 TEST_info("skipping, '%s' is disabled", p);
2436 && (strcmp(name, "cipher") == 0
2437 || strcmp(name, "cekalg") == 0)
2438 && is_cipher_disabled(p)) {
2439 TEST_info("skipping, '%s' is disabled", p);
2446 static int kdf_test_parse(EVP_TEST *t,
2447 const char *keyword, const char *value)
2449 KDF_DATA *kdata = t->data;
2451 if (strcmp(keyword, "Output") == 0)
2452 return parse_bin(value, &kdata->output, &kdata->output_len);
2453 if (strncmp(keyword, "Ctrl", 4) == 0)
2454 return kdf_test_ctrl(t, kdata->ctx, value);
2458 static int kdf_test_run(EVP_TEST *t)
2460 KDF_DATA *expected = t->data;
2461 unsigned char *got = NULL;
2462 size_t got_len = expected->output_len;
2464 if (!EVP_KDF_CTX_set_params(expected->ctx, expected->params)) {
2465 t->err = "KDF_CTRL_ERROR";
2468 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2469 t->err = "INTERNAL_ERROR";
2472 if (EVP_KDF_derive(expected->ctx, got, got_len, NULL) <= 0) {
2473 t->err = "KDF_DERIVE_ERROR";
2476 if (!memory_err_compare(t, "KDF_MISMATCH",
2477 expected->output, expected->output_len,
2488 static const EVP_TEST_METHOD kdf_test_method = {
2500 typedef struct pkey_kdf_data_st {
2501 /* Context for this operation */
2503 /* Expected output */
2504 unsigned char *output;
2509 * Perform public key operation setup: lookup key, allocated ctx and call
2510 * the appropriate initialisation function
2512 static int pkey_kdf_test_init(EVP_TEST *t, const char *name)
2514 PKEY_KDF_DATA *kdata = NULL;
2516 if (is_kdf_disabled(name)) {
2517 TEST_info("skipping, '%s' is disabled", name);
2522 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2525 kdata->ctx = EVP_PKEY_CTX_new_from_name(libctx, name, NULL);
2526 if (kdata->ctx == NULL
2527 || EVP_PKEY_derive_init(kdata->ctx) <= 0)
2533 EVP_PKEY_CTX_free(kdata->ctx);
2534 OPENSSL_free(kdata);
2538 static void pkey_kdf_test_cleanup(EVP_TEST *t)
2540 PKEY_KDF_DATA *kdata = t->data;
2542 OPENSSL_free(kdata->output);
2543 EVP_PKEY_CTX_free(kdata->ctx);
2546 static int pkey_kdf_test_parse(EVP_TEST *t,
2547 const char *keyword, const char *value)
2549 PKEY_KDF_DATA *kdata = t->data;
2551 if (strcmp(keyword, "Output") == 0)
2552 return parse_bin(value, &kdata->output, &kdata->output_len);
2553 if (strncmp(keyword, "Ctrl", 4) == 0)
2554 return pkey_test_ctrl(t, kdata->ctx, value);
2558 static int pkey_kdf_test_run(EVP_TEST *t)
2560 PKEY_KDF_DATA *expected = t->data;
2561 unsigned char *got = NULL;
2562 size_t got_len = expected->output_len;
2564 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2565 t->err = "INTERNAL_ERROR";
2568 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
2569 t->err = "KDF_DERIVE_ERROR";
2572 if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
2573 t->err = "KDF_MISMATCH";
2583 static const EVP_TEST_METHOD pkey_kdf_test_method = {
2586 pkey_kdf_test_cleanup,
2587 pkey_kdf_test_parse,
2595 typedef struct keypair_test_data_st {
2598 } KEYPAIR_TEST_DATA;
2600 static int keypair_test_init(EVP_TEST *t, const char *pair)
2602 KEYPAIR_TEST_DATA *data;
2604 EVP_PKEY *pk = NULL, *pubk = NULL;
2605 char *pub, *priv = NULL;
2607 /* Split private and public names. */
2608 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
2609 || !TEST_ptr(pub = strchr(priv, ':'))) {
2610 t->err = "PARSING_ERROR";
2615 if (!TEST_true(find_key(&pk, priv, private_keys))) {
2616 TEST_info("Can't find private key: %s", priv);
2617 t->err = "MISSING_PRIVATE_KEY";
2620 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
2621 TEST_info("Can't find public key: %s", pub);
2622 t->err = "MISSING_PUBLIC_KEY";
2626 if (pk == NULL && pubk == NULL) {
2627 /* Both keys are listed but unsupported: skip this test */
2633 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2646 static void keypair_test_cleanup(EVP_TEST *t)
2648 OPENSSL_free(t->data);
2653 * For tests that do not accept any custom keywords.
2655 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
2660 static int keypair_test_run(EVP_TEST *t)
2663 const KEYPAIR_TEST_DATA *pair = t->data;
2665 if (pair->privk == NULL || pair->pubk == NULL) {
2667 * this can only happen if only one of the keys is not set
2668 * which means that one of them was unsupported while the
2669 * other isn't: hence a key type mismatch.
2671 t->err = "KEYPAIR_TYPE_MISMATCH";
2676 if ((rv = EVP_PKEY_eq(pair->privk, pair->pubk)) != 1 ) {
2678 t->err = "KEYPAIR_MISMATCH";
2679 } else if ( -1 == rv ) {
2680 t->err = "KEYPAIR_TYPE_MISMATCH";
2681 } else if ( -2 == rv ) {
2682 t->err = "UNSUPPORTED_KEY_COMPARISON";
2684 TEST_error("Unexpected error in key comparison");
2699 static const EVP_TEST_METHOD keypair_test_method = {
2702 keypair_test_cleanup,
2711 typedef struct keygen_test_data_st {
2712 EVP_PKEY_CTX *genctx; /* Keygen context to use */
2713 char *keyname; /* Key name to store key or NULL */
2716 static int keygen_test_init(EVP_TEST *t, const char *alg)
2718 KEYGEN_TEST_DATA *data;
2719 EVP_PKEY_CTX *genctx;
2720 int nid = OBJ_sn2nid(alg);
2722 if (nid == NID_undef) {
2723 nid = OBJ_ln2nid(alg);
2724 if (nid == NID_undef)
2728 if (is_pkey_disabled(alg)) {
2732 if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_from_name(libctx, alg, NULL)))
2735 if (EVP_PKEY_keygen_init(genctx) <= 0) {
2736 t->err = "KEYGEN_INIT_ERROR";
2740 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2742 data->genctx = genctx;
2743 data->keyname = NULL;
2749 EVP_PKEY_CTX_free(genctx);
2753 static void keygen_test_cleanup(EVP_TEST *t)
2755 KEYGEN_TEST_DATA *keygen = t->data;
2757 EVP_PKEY_CTX_free(keygen->genctx);
2758 OPENSSL_free(keygen->keyname);
2759 OPENSSL_free(t->data);
2763 static int keygen_test_parse(EVP_TEST *t,
2764 const char *keyword, const char *value)
2766 KEYGEN_TEST_DATA *keygen = t->data;
2768 if (strcmp(keyword, "KeyName") == 0)
2769 return TEST_ptr(keygen->keyname = OPENSSL_strdup(value));
2770 if (strcmp(keyword, "Ctrl") == 0)
2771 return pkey_test_ctrl(t, keygen->genctx, value);
2775 static int keygen_test_run(EVP_TEST *t)
2777 KEYGEN_TEST_DATA *keygen = t->data;
2778 EVP_PKEY *pkey = NULL;
2781 if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) {
2782 t->err = "KEYGEN_GENERATE_ERROR";
2786 if (!evp_pkey_is_provided(pkey)) {
2787 TEST_info("Warning: legacy key generated %s", keygen->keyname);
2790 if (keygen->keyname != NULL) {
2794 if (find_key(NULL, keygen->keyname, private_keys)) {
2795 TEST_info("Duplicate key %s", keygen->keyname);
2799 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
2801 key->name = keygen->keyname;
2802 keygen->keyname = NULL;
2804 key->next = private_keys;
2808 EVP_PKEY_free(pkey);
2817 static const EVP_TEST_METHOD keygen_test_method = {
2820 keygen_test_cleanup,
2826 ** DIGEST SIGN+VERIFY TESTS
2830 int is_verify; /* Set to 1 if verifying */
2831 int is_oneshot; /* Set to 1 for one shot operation */
2832 const EVP_MD *md; /* Digest to use */
2833 EVP_MD_CTX *ctx; /* Digest context */
2835 STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
2836 unsigned char *osin; /* Input data if one shot */
2837 size_t osin_len; /* Input length data if one shot */
2838 unsigned char *output; /* Expected output */
2839 size_t output_len; /* Expected output length */
2842 static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
2845 const EVP_MD *md = NULL;
2846 DIGESTSIGN_DATA *mdat;
2848 if (strcmp(alg, "NULL") != 0) {
2849 if (is_digest_disabled(alg)) {
2853 md = EVP_get_digestbyname(alg);
2857 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
2860 if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
2864 mdat->is_verify = is_verify;
2865 mdat->is_oneshot = is_oneshot;
2870 static int digestsign_test_init(EVP_TEST *t, const char *alg)
2872 return digestsigver_test_init(t, alg, 0, 0);
2875 static void digestsigver_test_cleanup(EVP_TEST *t)
2877 DIGESTSIGN_DATA *mdata = t->data;
2879 EVP_MD_CTX_free(mdata->ctx);
2880 sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
2881 OPENSSL_free(mdata->osin);
2882 OPENSSL_free(mdata->output);
2883 OPENSSL_free(mdata);
2887 static int digestsigver_test_parse(EVP_TEST *t,
2888 const char *keyword, const char *value)
2890 DIGESTSIGN_DATA *mdata = t->data;
2892 if (strcmp(keyword, "Key") == 0) {
2893 EVP_PKEY *pkey = NULL;
2895 const char *name = mdata->md == NULL ? NULL : EVP_MD_name(mdata->md);
2897 if (mdata->is_verify)
2898 rv = find_key(&pkey, value, public_keys);
2900 rv = find_key(&pkey, value, private_keys);
2901 if (rv == 0 || pkey == NULL) {
2905 if (mdata->is_verify) {
2906 if (!EVP_DigestVerifyInit_ex(mdata->ctx, &mdata->pctx, name, libctx,
2908 t->err = "DIGESTVERIFYINIT_ERROR";
2911 if (!EVP_DigestSignInit_ex(mdata->ctx, &mdata->pctx, name, libctx, NULL,
2913 t->err = "DIGESTSIGNINIT_ERROR";
2917 if (strcmp(keyword, "Input") == 0) {
2918 if (mdata->is_oneshot)
2919 return parse_bin(value, &mdata->osin, &mdata->osin_len);
2920 return evp_test_buffer_append(value, &mdata->input);
2922 if (strcmp(keyword, "Output") == 0)
2923 return parse_bin(value, &mdata->output, &mdata->output_len);
2925 if (!mdata->is_oneshot) {
2926 if (strcmp(keyword, "Count") == 0)
2927 return evp_test_buffer_set_count(value, mdata->input);
2928 if (strcmp(keyword, "Ncopy") == 0)
2929 return evp_test_buffer_ncopy(value, mdata->input);
2931 if (strcmp(keyword, "Ctrl") == 0) {
2932 if (mdata->pctx == NULL)
2934 return pkey_test_ctrl(t, mdata->pctx, value);
2939 static int digestsign_update_fn(void *ctx, const unsigned char *buf,
2942 return EVP_DigestSignUpdate(ctx, buf, buflen);
2945 static int digestsign_test_run(EVP_TEST *t)
2947 DIGESTSIGN_DATA *expected = t->data;
2948 unsigned char *got = NULL;
2951 if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
2953 t->err = "DIGESTUPDATE_ERROR";
2957 if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
2958 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
2961 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2962 t->err = "MALLOC_FAILURE";
2965 if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
2966 t->err = "DIGESTSIGNFINAL_ERROR";
2969 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
2970 expected->output, expected->output_len,
2980 static const EVP_TEST_METHOD digestsign_test_method = {
2982 digestsign_test_init,
2983 digestsigver_test_cleanup,
2984 digestsigver_test_parse,
2988 static int digestverify_test_init(EVP_TEST *t, const char *alg)
2990 return digestsigver_test_init(t, alg, 1, 0);
2993 static int digestverify_update_fn(void *ctx, const unsigned char *buf,
2996 return EVP_DigestVerifyUpdate(ctx, buf, buflen);
2999 static int digestverify_test_run(EVP_TEST *t)
3001 DIGESTSIGN_DATA *mdata = t->data;
3003 if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
3004 t->err = "DIGESTUPDATE_ERROR";
3008 if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
3009 mdata->output_len) <= 0)
3010 t->err = "VERIFY_ERROR";
3014 static const EVP_TEST_METHOD digestverify_test_method = {
3016 digestverify_test_init,
3017 digestsigver_test_cleanup,
3018 digestsigver_test_parse,
3019 digestverify_test_run
3022 static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
3024 return digestsigver_test_init(t, alg, 0, 1);
3027 static int oneshot_digestsign_test_run(EVP_TEST *t)
3029 DIGESTSIGN_DATA *expected = t->data;
3030 unsigned char *got = NULL;
3033 if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
3034 expected->osin, expected->osin_len)) {
3035 t->err = "DIGESTSIGN_LENGTH_ERROR";
3038 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
3039 t->err = "MALLOC_FAILURE";
3042 if (!EVP_DigestSign(expected->ctx, got, &got_len,
3043 expected->osin, expected->osin_len)) {
3044 t->err = "DIGESTSIGN_ERROR";
3047 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
3048 expected->output, expected->output_len,
3058 static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
3059 "OneShotDigestSign",
3060 oneshot_digestsign_test_init,
3061 digestsigver_test_cleanup,
3062 digestsigver_test_parse,
3063 oneshot_digestsign_test_run
3066 static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
3068 return digestsigver_test_init(t, alg, 1, 1);
3071 static int oneshot_digestverify_test_run(EVP_TEST *t)
3073 DIGESTSIGN_DATA *mdata = t->data;
3075 if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
3076 mdata->osin, mdata->osin_len) <= 0)
3077 t->err = "VERIFY_ERROR";
3081 static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
3082 "OneShotDigestVerify",
3083 oneshot_digestverify_test_init,
3084 digestsigver_test_cleanup,
3085 digestsigver_test_parse,
3086 oneshot_digestverify_test_run
3091 ** PARSING AND DISPATCH
3094 static const EVP_TEST_METHOD *evp_test_list[] = {
3096 &cipher_test_method,
3097 &digest_test_method,
3098 &digestsign_test_method,
3099 &digestverify_test_method,
3100 &encode_test_method,
3102 &pkey_kdf_test_method,
3103 &keypair_test_method,
3104 &keygen_test_method,
3106 &oneshot_digestsign_test_method,
3107 &oneshot_digestverify_test_method,
3109 &pdecrypt_test_method,
3110 &pderive_test_method,
3112 &pverify_recover_test_method,
3113 &pverify_test_method,
3117 static const EVP_TEST_METHOD *find_test(const char *name)
3119 const EVP_TEST_METHOD **tt;
3121 for (tt = evp_test_list; *tt; tt++) {
3122 if (strcmp(name, (*tt)->name) == 0)
3128 static void clear_test(EVP_TEST *t)
3130 test_clearstanza(&t->s);
3132 if (t->data != NULL) {
3133 if (t->meth != NULL)
3134 t->meth->cleanup(t);
3135 OPENSSL_free(t->data);
3138 OPENSSL_free(t->expected_err);
3139 t->expected_err = NULL;
3140 OPENSSL_free(t->reason);
3149 /* Check for errors in the test structure; return 1 if okay, else 0. */
3150 static int check_test_error(EVP_TEST *t)
3155 if (t->err == NULL && t->expected_err == NULL)
3157 if (t->err != NULL && t->expected_err == NULL) {
3158 if (t->aux_err != NULL) {
3159 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
3160 t->s.test_file, t->s.start, t->aux_err, t->err);
3162 TEST_info("%s:%d: Source of above error; unexpected error %s",
3163 t->s.test_file, t->s.start, t->err);
3167 if (t->err == NULL && t->expected_err != NULL) {
3168 TEST_info("%s:%d: Succeeded but was expecting %s",
3169 t->s.test_file, t->s.start, t->expected_err);
3173 if (strcmp(t->err, t->expected_err) != 0) {
3174 TEST_info("%s:%d: Expected %s got %s",
3175 t->s.test_file, t->s.start, t->expected_err, t->err);
3179 if (t->reason == NULL)
3182 if (t->reason == NULL) {
3183 TEST_info("%s:%d: Test is missing function or reason code",
3184 t->s.test_file, t->s.start);
3188 err = ERR_peek_error();
3190 TEST_info("%s:%d: Expected error \"%s\" not set",
3191 t->s.test_file, t->s.start, t->reason);
3195 reason = ERR_reason_error_string(err);
3196 if (reason == NULL) {
3197 TEST_info("%s:%d: Expected error \"%s\", no strings available."
3199 t->s.test_file, t->s.start, t->reason);
3203 if (strcmp(reason, t->reason) == 0)
3206 TEST_info("%s:%d: Expected error \"%s\", got \"%s\"",
3207 t->s.test_file, t->s.start, t->reason, reason);
3212 /* Run a parsed test. Log a message and return 0 on error. */
3213 static int run_test(EVP_TEST *t)
3215 if (t->meth == NULL)
3222 if (t->err == NULL && t->meth->run_test(t) != 1) {
3223 TEST_info("%s:%d %s error",
3224 t->s.test_file, t->s.start, t->meth->name);
3227 if (!check_test_error(t)) {
3228 TEST_openssl_errors();
3237 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
3239 for (; lst != NULL; lst = lst->next) {
3240 if (strcmp(lst->name, name) == 0) {
3249 static void free_key_list(KEY_LIST *lst)
3251 while (lst != NULL) {
3252 KEY_LIST *next = lst->next;
3254 EVP_PKEY_free(lst->key);
3255 OPENSSL_free(lst->name);
3262 * Is the key type an unsupported algorithm?
3264 static int key_unsupported(void)
3266 long err = ERR_peek_last_error();
3268 if (ERR_GET_LIB(err) == ERR_LIB_EVP
3269 && (ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM)) {
3273 #ifndef OPENSSL_NO_EC
3275 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
3276 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
3279 if (ERR_GET_LIB(err) == ERR_LIB_EC
3280 && (ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP
3281 || ERR_GET_REASON(err) == EC_R_INVALID_CURVE)) {
3285 #endif /* OPENSSL_NO_EC */
3289 /* NULL out the value from |pp| but return it. This "steals" a pointer. */
3290 static char *take_value(PAIR *pp)
3292 char *p = pp->value;
3298 #if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
3299 static int securitycheck_enabled(void)
3301 static int enabled = -1;
3303 if (enabled == -1) {
3304 if (OSSL_PROVIDER_available(libctx, "fips")) {
3305 OSSL_PARAM params[2];
3306 OSSL_PROVIDER *prov = NULL;
3309 prov = OSSL_PROVIDER_load(libctx, "fips");
3312 OSSL_PARAM_construct_int(OSSL_PROV_PARAM_SECURITY_CHECKS,
3314 params[1] = OSSL_PARAM_construct_end();
3315 OSSL_PROVIDER_get_params(prov, params);
3316 OSSL_PROVIDER_unload(prov);
3328 * Return 1 if one of the providers named in the string is available.
3329 * The provider names are separated with whitespace.
3330 * NOTE: destructive function, it inserts '\0' after each provider name.
3332 static int prov_available(char *providers)
3338 for (; isspace(*providers); providers++)
3340 if (*providers == '\0')
3341 break; /* End of the road */
3342 for (p = providers; *p != '\0' && !isspace(*p); p++)
3348 if (OSSL_PROVIDER_available(libctx, providers))
3349 return 1; /* Found one */
3354 /* Read and parse one test. Return 0 if failure, 1 if okay. */
3355 static int parse(EVP_TEST *t)
3357 KEY_LIST *key, **klist;
3360 int i, skip_availablein = 0;
3364 if (BIO_eof(t->s.fp))
3367 if (!test_readstanza(&t->s))
3369 } while (t->s.numpairs == 0);
3370 pp = &t->s.pairs[0];
3372 /* Are we adding a key? */
3376 if (strcmp(pp->key, "PrivateKey") == 0) {
3377 pkey = PEM_read_bio_PrivateKey_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3378 if (pkey == NULL && !key_unsupported()) {
3379 EVP_PKEY_free(pkey);
3380 TEST_info("Can't read private key %s", pp->value);
3381 TEST_openssl_errors();
3384 klist = &private_keys;
3385 } else if (strcmp(pp->key, "PublicKey") == 0) {
3386 pkey = PEM_read_bio_PUBKEY_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3387 if (pkey == NULL && !key_unsupported()) {
3388 EVP_PKEY_free(pkey);
3389 TEST_info("Can't read public key %s", pp->value);
3390 TEST_openssl_errors();
3393 klist = &public_keys;
3394 } else if (strcmp(pp->key, "PrivateKeyRaw") == 0
3395 || strcmp(pp->key, "PublicKeyRaw") == 0 ) {
3396 char *strnid = NULL, *keydata = NULL;
3397 unsigned char *keybin;
3401 if (strcmp(pp->key, "PrivateKeyRaw") == 0)
3402 klist = &private_keys;
3404 klist = &public_keys;
3406 strnid = strchr(pp->value, ':');
3407 if (strnid != NULL) {
3409 keydata = strchr(strnid, ':');
3410 if (keydata != NULL)
3413 if (keydata == NULL) {
3414 TEST_info("Failed to parse %s value", pp->key);
3418 nid = OBJ_txt2nid(strnid);
3419 if (nid == NID_undef) {
3420 TEST_info("Unrecognised algorithm NID");
3423 if (!parse_bin(keydata, &keybin, &keylen)) {
3424 TEST_info("Failed to create binary key");
3427 if (klist == &private_keys)
3428 pkey = EVP_PKEY_new_raw_private_key_ex(libctx, strnid, NULL, keybin,
3431 pkey = EVP_PKEY_new_raw_public_key_ex(libctx, strnid, NULL, keybin,
3433 if (pkey == NULL && !key_unsupported()) {
3434 TEST_info("Can't read %s data", pp->key);
3435 OPENSSL_free(keybin);
3436 TEST_openssl_errors();
3439 OPENSSL_free(keybin);
3440 } else if (strcmp(pp->key, "Availablein") == 0) {
3441 if (!prov_available(pp->value)) {
3442 TEST_info("skipping, '%s' provider not available: %s:%d",
3443 pp->value, t->s.test_file, t->s.start);
3452 /* If we have a key add to list */
3453 if (klist != NULL) {
3454 if (find_key(NULL, pp->value, *klist)) {
3455 TEST_info("Duplicate key %s", pp->value);
3458 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
3460 key->name = take_value(pp);
3465 /* Go back and start a new stanza. */
3466 if ((t->s.numpairs - skip_availablein) != 1)
3467 TEST_info("Line %d: missing blank line\n", t->s.curr);
3471 /* Find the test, based on first keyword. */
3472 if (!TEST_ptr(t->meth = find_test(pp->key)))
3474 if (!t->meth->init(t, pp->value)) {
3475 TEST_error("unknown %s: %s\n", pp->key, pp->value);
3479 /* TEST_info("skipping %s %s", pp->key, pp->value); */
3483 for (pp++, i = 1; i < (t->s.numpairs - skip_availablein); pp++, i++) {
3484 if (strcmp(pp->key, "Securitycheck") == 0) {
3485 #if defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
3487 if (!securitycheck_enabled())
3490 TEST_info("skipping, Securitycheck is disabled: %s:%d",
3491 t->s.test_file, t->s.start);
3495 } else if (strcmp(pp->key, "Availablein") == 0) {
3496 TEST_info("Line %d: 'Availablein' should be the first option",
3499 } else if (strcmp(pp->key, "Result") == 0) {
3500 if (t->expected_err != NULL) {
3501 TEST_info("Line %d: multiple result lines", t->s.curr);
3504 t->expected_err = take_value(pp);
3505 } else if (strcmp(pp->key, "Function") == 0) {
3506 /* Ignore old line. */
3507 } else if (strcmp(pp->key, "Reason") == 0) {
3508 if (t->reason != NULL) {
3509 TEST_info("Line %d: multiple reason lines", t->s.curr);
3512 t->reason = take_value(pp);
3514 /* Must be test specific line: try to parse it */
3515 int rv = t->meth->parse(t, pp->key, pp->value);
3518 TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key);
3522 TEST_info("Line %d: error processing keyword %s = %s\n",
3523 t->s.curr, pp->key, pp->value);
3532 static int run_file_tests(int i)
3535 const char *testfile = test_get_argument(i);
3538 if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t))))
3540 if (!test_start_file(&t->s, testfile)) {
3545 while (!BIO_eof(t->s.fp)) {
3551 if (c == 0 || !run_test(t)) {
3556 test_end_file(&t->s);
3559 free_key_list(public_keys);
3560 free_key_list(private_keys);
3567 const OPTIONS *test_get_options(void)
3569 static const OPTIONS test_options[] = {
3570 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
3571 { "config", OPT_CONFIG_FILE, '<',
3572 "The configuration file to use for the libctx" },
3573 { OPT_HELP_STR, 1, '-', "file\tFile to run tests on.\n" },
3576 return test_options;
3579 int setup_tests(void)
3582 char *config_file = NULL;
3586 while ((o = opt_next()) != OPT_EOF) {
3588 case OPT_CONFIG_FILE:
3589 config_file = opt_arg();
3591 case OPT_TEST_CASES:
3600 * Load the provider via configuration into the created library context.
3601 * Load the 'null' provider into the default library context to ensure that
3602 * the tests do not fallback to using the default provider.
3604 if (!test_get_libctx(&libctx, &prov_null, config_file, NULL, NULL))
3607 n = test_get_argument_count();
3611 ADD_ALL_TESTS(run_file_tests, n);
3615 void cleanup_tests(void)
3617 OSSL_PROVIDER_unload(prov_null);
3618 OSSL_LIB_CTX_free(libctx);
3621 #define STR_STARTS_WITH(str, pre) strncasecmp(pre, str, strlen(pre)) == 0
3622 #define STR_ENDS_WITH(str, pre) \
3623 strlen(str) < strlen(pre) ? 0 : (strcasecmp(pre, str + strlen(str) - strlen(pre)) == 0)
3625 static int is_digest_disabled(const char *name)
3627 #ifdef OPENSSL_NO_BLAKE2
3628 if (STR_STARTS_WITH(name, "BLAKE"))
3631 #ifdef OPENSSL_NO_MD2
3632 if (strcasecmp(name, "MD2") == 0)
3635 #ifdef OPENSSL_NO_MDC2
3636 if (strcasecmp(name, "MDC2") == 0)
3639 #ifdef OPENSSL_NO_MD4
3640 if (strcasecmp(name, "MD4") == 0)
3643 #ifdef OPENSSL_NO_MD5
3644 if (strcasecmp(name, "MD5") == 0)
3647 #ifdef OPENSSL_NO_RMD160
3648 if (strcasecmp(name, "RIPEMD160") == 0)
3651 #ifdef OPENSSL_NO_SM3
3652 if (strcasecmp(name, "SM3") == 0)
3655 #ifdef OPENSSL_NO_WHIRLPOOL
3656 if (strcasecmp(name, "WHIRLPOOL") == 0)
3662 static int is_pkey_disabled(const char *name)
3664 #ifdef OPENSSL_NO_EC
3665 if (STR_STARTS_WITH(name, "EC"))
3668 #ifdef OPENSSL_NO_DH
3669 if (STR_STARTS_WITH(name, "DH"))
3672 #ifdef OPENSSL_NO_DSA
3673 if (STR_STARTS_WITH(name, "DSA"))
3679 static int is_mac_disabled(const char *name)
3681 #ifdef OPENSSL_NO_BLAKE2
3682 if (STR_STARTS_WITH(name, "BLAKE2BMAC")
3683 || STR_STARTS_WITH(name, "BLAKE2SMAC"))
3686 #ifdef OPENSSL_NO_CMAC
3687 if (STR_STARTS_WITH(name, "CMAC"))
3690 #ifdef OPENSSL_NO_POLY1305
3691 if (STR_STARTS_WITH(name, "Poly1305"))
3694 #ifdef OPENSSL_NO_SIPHASH
3695 if (STR_STARTS_WITH(name, "SipHash"))
3700 static int is_kdf_disabled(const char *name)
3702 #ifdef OPENSSL_NO_SCRYPT
3703 if (STR_ENDS_WITH(name, "SCRYPT"))
3709 static int is_cipher_disabled(const char *name)
3711 #ifdef OPENSSL_NO_ARIA
3712 if (STR_STARTS_WITH(name, "ARIA"))
3715 #ifdef OPENSSL_NO_BF
3716 if (STR_STARTS_WITH(name, "BF"))
3719 #ifdef OPENSSL_NO_CAMELLIA
3720 if (STR_STARTS_WITH(name, "CAMELLIA"))
3723 #ifdef OPENSSL_NO_CAST
3724 if (STR_STARTS_WITH(name, "CAST"))
3727 #ifdef OPENSSL_NO_CHACHA
3728 if (STR_STARTS_WITH(name, "CHACHA"))
3731 #ifdef OPENSSL_NO_POLY1305
3732 if (STR_ENDS_WITH(name, "Poly1305"))
3735 #ifdef OPENSSL_NO_DES
3736 if (STR_STARTS_WITH(name, "DES"))
3738 if (STR_ENDS_WITH(name, "3DESwrap"))
3741 #ifdef OPENSSL_NO_OCB
3742 if (STR_ENDS_WITH(name, "OCB"))
3745 #ifdef OPENSSL_NO_IDEA
3746 if (STR_STARTS_WITH(name, "IDEA"))
3749 #ifdef OPENSSL_NO_RC2
3750 if (STR_STARTS_WITH(name, "RC2"))
3753 #ifdef OPENSSL_NO_RC4
3754 if (STR_STARTS_WITH(name, "RC4"))
3757 #ifdef OPENSSL_NO_RC5
3758 if (STR_STARTS_WITH(name, "RC5"))
3761 #ifdef OPENSSL_NO_SEED
3762 if (STR_STARTS_WITH(name, "SEED"))
3765 #ifdef OPENSSL_NO_SIV
3766 if (STR_ENDS_WITH(name, "SIV"))
3769 #ifdef OPENSSL_NO_SM4
3770 if (STR_STARTS_WITH(name, "SM4"))