2 * Copyright 2015-2020 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
14 #include "../e_os.h" /* strcasecmp */
15 #include <openssl/evp.h>
16 #include <openssl/pem.h>
17 #include <openssl/err.h>
18 #include <openssl/provider.h>
19 #include <openssl/x509v3.h>
20 #include <openssl/pkcs12.h>
21 #include <openssl/kdf.h>
22 #include <openssl/params.h>
23 #include <openssl/core_names.h>
24 #include "internal/numbers.h"
25 #include "internal/nelem.h"
26 #include "crypto/evp.h"
30 DEFINE_STACK_OF_STRING()
34 typedef struct evp_test_method_st EVP_TEST_METHOD;
36 /* Structure holding test information */
37 typedef struct evp_test_st {
38 STANZA s; /* Common test stanza */
40 int skip; /* Current test should be skipped */
41 const EVP_TEST_METHOD *meth; /* method for this test */
42 const char *err, *aux_err; /* Error string for test */
43 char *expected_err; /* Expected error value of test */
44 char *reason; /* Expected error reason string */
45 void *data; /* test specific data */
48 /* Test method structure */
49 struct evp_test_method_st {
50 /* Name of test as it appears in file */
52 /* Initialise test for "alg" */
53 int (*init) (EVP_TEST * t, const char *alg);
55 void (*cleanup) (EVP_TEST * t);
56 /* Test specific name value pair processing */
57 int (*parse) (EVP_TEST * t, const char *name, const char *value);
58 /* Run the test itself */
59 int (*run_test) (EVP_TEST * t);
62 /* Linked list of named keys. */
63 typedef struct key_list_st {
66 struct key_list_st *next;
69 typedef enum OPTION_choice {
76 static OSSL_PROVIDER *prov_null = NULL;
77 static OPENSSL_CTX *libctx = NULL;
79 /* List of public and private keys */
80 static KEY_LIST *private_keys;
81 static KEY_LIST *public_keys;
83 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst);
84 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen);
85 static int is_digest_disabled(const char *name);
86 static int is_pkey_disabled(const char *name);
87 static int is_mac_disabled(const char *name);
88 static int is_cipher_disabled(const char *name);
89 static int is_kdf_disabled(const char *name);
92 * Compare two memory regions for equality, returning zero if they differ.
93 * However, if there is expected to be an error and the actual error
94 * matches then the memory is expected to be different so handle this
95 * case without producing unnecessary test framework output.
97 static int memory_err_compare(EVP_TEST *t, const char *err,
98 const void *expected, size_t expected_len,
99 const void *got, size_t got_len)
103 if (t->expected_err != NULL && strcmp(t->expected_err, err) == 0)
104 r = !TEST_mem_ne(expected, expected_len, got, got_len);
106 r = TEST_mem_eq(expected, expected_len, got, got_len);
113 * Structure used to hold a list of blocks of memory to test
114 * calls to "update" like functions.
116 struct evp_test_buffer_st {
123 static void evp_test_buffer_free(EVP_TEST_BUFFER *db)
126 OPENSSL_free(db->buf);
131 /* append buffer to a list */
132 static int evp_test_buffer_append(const char *value,
133 STACK_OF(EVP_TEST_BUFFER) **sk)
135 EVP_TEST_BUFFER *db = NULL;
137 if (!TEST_ptr(db = OPENSSL_malloc(sizeof(*db))))
140 if (!parse_bin(value, &db->buf, &db->buflen))
145 if (*sk == NULL && !TEST_ptr(*sk = sk_EVP_TEST_BUFFER_new_null()))
147 if (!sk_EVP_TEST_BUFFER_push(*sk, db))
153 evp_test_buffer_free(db);
157 /* replace last buffer in list with copies of itself */
158 static int evp_test_buffer_ncopy(const char *value,
159 STACK_OF(EVP_TEST_BUFFER) *sk)
162 unsigned char *tbuf, *p;
164 int ncopy = atoi(value);
169 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
171 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
173 tbuflen = db->buflen * ncopy;
174 if (!TEST_ptr(tbuf = OPENSSL_malloc(tbuflen)))
176 for (i = 0, p = tbuf; i < ncopy; i++, p += db->buflen)
177 memcpy(p, db->buf, db->buflen);
179 OPENSSL_free(db->buf);
181 db->buflen = tbuflen;
185 /* set repeat count for last buffer in list */
186 static int evp_test_buffer_set_count(const char *value,
187 STACK_OF(EVP_TEST_BUFFER) *sk)
190 int count = atoi(value);
195 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
198 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
199 if (db->count_set != 0)
202 db->count = (size_t)count;
207 /* call "fn" with each element of the list in turn */
208 static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER) *sk,
210 const unsigned char *buf,
216 for (i = 0; i < sk_EVP_TEST_BUFFER_num(sk); i++) {
217 EVP_TEST_BUFFER *tb = sk_EVP_TEST_BUFFER_value(sk, i);
220 for (j = 0; j < tb->count; j++) {
221 if (fn(ctx, tb->buf, tb->buflen) <= 0)
229 * Unescape some sequences in string literals (only \n for now).
230 * Return an allocated buffer, set |out_len|. If |input_len|
231 * is zero, get an empty buffer but set length to zero.
233 static unsigned char* unescape(const char *input, size_t input_len,
236 unsigned char *ret, *p;
239 if (input_len == 0) {
241 return OPENSSL_zalloc(1);
244 /* Escaping is non-expanding; over-allocate original size for simplicity. */
245 if (!TEST_ptr(ret = p = OPENSSL_malloc(input_len)))
248 for (i = 0; i < input_len; i++) {
249 if (*input == '\\') {
250 if (i == input_len - 1 || *++input != 'n') {
251 TEST_error("Bad escape sequence in file");
271 * For a hex string "value" convert to a binary allocated buffer.
272 * Return 1 on success or 0 on failure.
274 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen)
278 /* Check for NULL literal */
279 if (strcmp(value, "NULL") == 0) {
285 /* Check for empty value */
286 if (*value == '\0') {
288 * Don't return NULL for zero length buffer. This is needed for
289 * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key
290 * buffer even if the key length is 0, in order to detect key reset.
292 *buf = OPENSSL_malloc(1);
300 /* Check for string literal */
301 if (value[0] == '"') {
302 size_t vlen = strlen(++value);
304 if (vlen == 0 || value[vlen - 1] != '"')
307 *buf = unescape(value, vlen, buflen);
308 return *buf == NULL ? 0 : 1;
311 /* Otherwise assume as hex literal and convert it to binary buffer */
312 if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) {
313 TEST_info("Can't convert %s", value);
314 TEST_openssl_errors();
317 /* Size of input buffer means we'll never overflow */
323 ** MESSAGE DIGEST TESTS
326 typedef struct digest_data_st {
327 /* Digest this test is for */
328 const EVP_MD *digest;
329 EVP_MD *fetched_digest;
330 /* Input to digest */
331 STACK_OF(EVP_TEST_BUFFER) *input;
332 /* Expected output */
333 unsigned char *output;
339 static int digest_test_init(EVP_TEST *t, const char *alg)
342 const EVP_MD *digest;
343 EVP_MD *fetched_digest;
345 if (is_digest_disabled(alg)) {
346 TEST_info("skipping, '%s' is disabled", alg);
351 if ((digest = fetched_digest = EVP_MD_fetch(libctx, alg, NULL)) == NULL
352 && (digest = EVP_get_digestbyname(alg)) == NULL)
354 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
357 mdat->digest = digest;
358 mdat->fetched_digest = fetched_digest;
360 if (fetched_digest != NULL)
361 TEST_info("%s is fetched", alg);
365 static void digest_test_cleanup(EVP_TEST *t)
367 DIGEST_DATA *mdat = t->data;
369 sk_EVP_TEST_BUFFER_pop_free(mdat->input, evp_test_buffer_free);
370 OPENSSL_free(mdat->output);
371 EVP_MD_free(mdat->fetched_digest);
374 static int digest_test_parse(EVP_TEST *t,
375 const char *keyword, const char *value)
377 DIGEST_DATA *mdata = t->data;
379 if (strcmp(keyword, "Input") == 0)
380 return evp_test_buffer_append(value, &mdata->input);
381 if (strcmp(keyword, "Output") == 0)
382 return parse_bin(value, &mdata->output, &mdata->output_len);
383 if (strcmp(keyword, "Count") == 0)
384 return evp_test_buffer_set_count(value, mdata->input);
385 if (strcmp(keyword, "Ncopy") == 0)
386 return evp_test_buffer_ncopy(value, mdata->input);
387 if (strcmp(keyword, "Padding") == 0)
388 return (mdata->pad_type = atoi(value)) > 0;
392 static int digest_update_fn(void *ctx, const unsigned char *buf, size_t buflen)
394 return EVP_DigestUpdate(ctx, buf, buflen);
397 static int digest_test_run(EVP_TEST *t)
399 DIGEST_DATA *expected = t->data;
401 unsigned char *got = NULL;
402 unsigned int got_len;
403 OSSL_PARAM params[2];
405 t->err = "TEST_FAILURE";
406 if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
409 got = OPENSSL_malloc(expected->output_len > EVP_MAX_MD_SIZE ?
410 expected->output_len : EVP_MAX_MD_SIZE);
414 if (!EVP_DigestInit_ex(mctx, expected->digest, NULL)) {
415 t->err = "DIGESTINIT_ERROR";
418 if (expected->pad_type > 0) {
419 params[0] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_PAD_TYPE,
420 &expected->pad_type);
421 params[1] = OSSL_PARAM_construct_end();
422 if (!TEST_int_gt(EVP_MD_CTX_set_params(mctx, params), 0)) {
423 t->err = "PARAMS_ERROR";
427 if (!evp_test_buffer_do(expected->input, digest_update_fn, mctx)) {
428 t->err = "DIGESTUPDATE_ERROR";
432 if (EVP_MD_flags(expected->digest) & EVP_MD_FLAG_XOF) {
433 EVP_MD_CTX *mctx_cpy;
434 char dont[] = "touch";
436 if (!TEST_ptr(mctx_cpy = EVP_MD_CTX_new())) {
439 if (!EVP_MD_CTX_copy(mctx_cpy, mctx)) {
440 EVP_MD_CTX_free(mctx_cpy);
443 if (!EVP_DigestFinalXOF(mctx_cpy, (unsigned char *)dont, 0)) {
444 EVP_MD_CTX_free(mctx_cpy);
445 t->err = "DIGESTFINALXOF_ERROR";
448 if (!TEST_str_eq(dont, "touch")) {
449 EVP_MD_CTX_free(mctx_cpy);
450 t->err = "DIGESTFINALXOF_ERROR";
453 EVP_MD_CTX_free(mctx_cpy);
455 got_len = expected->output_len;
456 if (!EVP_DigestFinalXOF(mctx, got, got_len)) {
457 t->err = "DIGESTFINALXOF_ERROR";
461 if (!EVP_DigestFinal(mctx, got, &got_len)) {
462 t->err = "DIGESTFINAL_ERROR";
466 if (!TEST_int_eq(expected->output_len, got_len)) {
467 t->err = "DIGEST_LENGTH_MISMATCH";
470 if (!memory_err_compare(t, "DIGEST_MISMATCH",
471 expected->output, expected->output_len,
479 EVP_MD_CTX_free(mctx);
483 static const EVP_TEST_METHOD digest_test_method = {
495 typedef struct cipher_data_st {
496 const EVP_CIPHER *cipher;
497 EVP_CIPHER *fetched_cipher;
499 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
503 size_t key_bits; /* Used by RC2 */
507 unsigned char *plaintext;
508 size_t plaintext_len;
509 unsigned char *ciphertext;
510 size_t ciphertext_len;
511 /* GCM, CCM, OCB and SIV only */
512 unsigned char *aad[AAD_NUM];
513 size_t aad_len[AAD_NUM];
515 const char *cts_mode;
520 static int cipher_test_init(EVP_TEST *t, const char *alg)
522 const EVP_CIPHER *cipher;
523 EVP_CIPHER *fetched_cipher;
527 if (is_cipher_disabled(alg)) {
529 TEST_info("skipping, '%s' is disabled", alg);
533 if ((cipher = fetched_cipher = EVP_CIPHER_fetch(libctx, alg, NULL)) == NULL
534 && (cipher = EVP_get_cipherbyname(alg)) == NULL)
537 cdat = OPENSSL_zalloc(sizeof(*cdat));
538 cdat->cipher = cipher;
539 cdat->fetched_cipher = fetched_cipher;
541 m = EVP_CIPHER_mode(cipher);
542 if (m == EVP_CIPH_GCM_MODE
543 || m == EVP_CIPH_OCB_MODE
544 || m == EVP_CIPH_SIV_MODE
545 || m == EVP_CIPH_CCM_MODE)
547 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
553 if (fetched_cipher != NULL)
554 TEST_info("%s is fetched", alg);
558 static void cipher_test_cleanup(EVP_TEST *t)
561 CIPHER_DATA *cdat = t->data;
563 OPENSSL_free(cdat->key);
564 OPENSSL_free(cdat->iv);
565 OPENSSL_free(cdat->ciphertext);
566 OPENSSL_free(cdat->plaintext);
567 for (i = 0; i < AAD_NUM; i++)
568 OPENSSL_free(cdat->aad[i]);
569 OPENSSL_free(cdat->tag);
570 EVP_CIPHER_free(cdat->fetched_cipher);
573 static int cipher_test_parse(EVP_TEST *t, const char *keyword,
576 CIPHER_DATA *cdat = t->data;
579 if (strcmp(keyword, "Key") == 0)
580 return parse_bin(value, &cdat->key, &cdat->key_len);
581 if (strcmp(keyword, "Rounds") == 0) {
585 cdat->rounds = (unsigned int)i;
588 if (strcmp(keyword, "IV") == 0)
589 return parse_bin(value, &cdat->iv, &cdat->iv_len);
590 if (strcmp(keyword, "Plaintext") == 0)
591 return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len);
592 if (strcmp(keyword, "Ciphertext") == 0)
593 return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
594 if (strcmp(keyword, "KeyBits") == 0) {
598 cdat->key_bits = (size_t)i;
602 if (strcmp(keyword, "AAD") == 0) {
603 for (i = 0; i < AAD_NUM; i++) {
604 if (cdat->aad[i] == NULL)
605 return parse_bin(value, &cdat->aad[i], &cdat->aad_len[i]);
609 if (strcmp(keyword, "Tag") == 0)
610 return parse_bin(value, &cdat->tag, &cdat->tag_len);
611 if (strcmp(keyword, "SetTagLate") == 0) {
612 if (strcmp(value, "TRUE") == 0)
614 else if (strcmp(value, "FALSE") == 0)
622 if (strcmp(keyword, "Operation") == 0) {
623 if (strcmp(value, "ENCRYPT") == 0)
625 else if (strcmp(value, "DECRYPT") == 0)
631 if (strcmp(keyword, "CTSMode") == 0) {
632 cdat->cts_mode = value;
638 static int cipher_test_enc(EVP_TEST *t, int enc,
639 size_t out_misalign, size_t inp_misalign, int frag)
641 CIPHER_DATA *expected = t->data;
642 unsigned char *in, *expected_out, *tmp = NULL;
643 size_t in_len, out_len, donelen = 0;
644 int ok = 0, tmplen, chunklen, tmpflen, i;
645 EVP_CIPHER_CTX *ctx_base = NULL;
646 EVP_CIPHER_CTX *ctx = NULL;
648 t->err = "TEST_FAILURE";
649 if (!TEST_ptr(ctx_base = EVP_CIPHER_CTX_new()))
651 if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
653 EVP_CIPHER_CTX_set_flags(ctx_base, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
655 in = expected->plaintext;
656 in_len = expected->plaintext_len;
657 expected_out = expected->ciphertext;
658 out_len = expected->ciphertext_len;
660 in = expected->ciphertext;
661 in_len = expected->ciphertext_len;
662 expected_out = expected->plaintext;
663 out_len = expected->plaintext_len;
665 if (inp_misalign == (size_t)-1) {
666 /* Exercise in-place encryption */
667 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
670 in = memcpy(tmp + out_misalign, in, in_len);
672 inp_misalign += 16 - ((out_misalign + in_len) & 15);
674 * 'tmp' will store both output and copy of input. We make the copy
675 * of input to specifically aligned part of 'tmp'. So we just
676 * figured out how much padding would ensure the required alignment,
677 * now we allocate extended buffer and finally copy the input just
678 * past inp_misalign in expression below. Output will be written
679 * past out_misalign...
681 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
682 inp_misalign + in_len);
685 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
686 inp_misalign, in, in_len);
688 if (!EVP_CipherInit_ex(ctx_base, expected->cipher, NULL, NULL, NULL, enc)) {
689 t->err = "CIPHERINIT_ERROR";
692 if (expected->cts_mode != NULL) {
693 OSSL_PARAM params[2];
695 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE,
696 (char *)expected->cts_mode,
698 params[1] = OSSL_PARAM_construct_end();
699 if (!EVP_CIPHER_CTX_set_params(ctx_base, params)) {
700 t->err = "INVALID_CTS_MODE";
705 if (expected->aead) {
706 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_IVLEN,
707 expected->iv_len, 0)) {
708 t->err = "INVALID_IV_LENGTH";
711 } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx_base)) {
712 t->err = "INVALID_IV_LENGTH";
716 if (expected->aead) {
719 * If encrypting or OCB just set tag length initially, otherwise
720 * set tag length and value.
722 if (enc || expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late) {
723 t->err = "TAG_LENGTH_SET_ERROR";
726 t->err = "TAG_SET_ERROR";
729 if (tag || expected->aead != EVP_CIPH_GCM_MODE) {
730 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_TAG,
731 expected->tag_len, tag))
736 if (expected->rounds > 0) {
737 int rounds = (int)expected->rounds;
739 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC5_ROUNDS, rounds, NULL)) {
740 t->err = "INVALID_ROUNDS";
745 if (!EVP_CIPHER_CTX_set_key_length(ctx_base, expected->key_len)) {
746 t->err = "INVALID_KEY_LENGTH";
749 if (expected->key_bits > 0) {
750 int bits = (int)expected->key_bits;
752 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC2_KEY_BITS, bits, NULL)) {
753 t->err = "INVALID KEY BITS";
757 if (!EVP_CipherInit_ex(ctx_base, NULL, NULL, expected->key, expected->iv, -1)) {
758 t->err = "KEY_SET_ERROR";
762 /* Check that we get the same IV back */
763 if (expected->iv != NULL
764 && (EVP_CIPHER_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
765 && !TEST_mem_eq(expected->iv, expected->iv_len,
766 EVP_CIPHER_CTX_iv(ctx_base), expected->iv_len)) {
767 t->err = "INVALID_IV";
771 /* Test that the cipher dup functions correctly if it is supported */
772 if (EVP_CIPHER_CTX_copy(ctx, ctx_base)) {
773 EVP_CIPHER_CTX_free(ctx_base);
776 EVP_CIPHER_CTX_free(ctx);
780 if (expected->aead == EVP_CIPH_CCM_MODE) {
781 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
782 t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
786 if (expected->aad[0] != NULL) {
787 t->err = "AAD_SET_ERROR";
789 for (i = 0; expected->aad[i] != NULL; i++) {
790 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i],
791 expected->aad_len[i]))
796 * Supply the AAD in chunks less than the block size where possible
798 for (i = 0; expected->aad[i] != NULL; i++) {
799 if (expected->aad_len[i] > 0) {
800 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], 1))
804 if (expected->aad_len[i] > 2) {
805 if (!EVP_CipherUpdate(ctx, NULL, &chunklen,
806 expected->aad[i] + donelen,
807 expected->aad_len[i] - 2))
809 donelen += expected->aad_len[i] - 2;
811 if (expected->aad_len[i] > 1
812 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
813 expected->aad[i] + donelen, 1))
819 if (!enc && (expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late)) {
820 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
821 expected->tag_len, expected->tag)) {
822 t->err = "TAG_SET_ERROR";
827 EVP_CIPHER_CTX_set_padding(ctx, 0);
828 t->err = "CIPHERUPDATE_ERROR";
831 /* We supply the data all in one go */
832 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
835 /* Supply the data in chunks less than the block size where possible */
837 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
844 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
852 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
858 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
859 t->err = "CIPHERFINAL_ERROR";
862 if (!memory_err_compare(t, "VALUE_MISMATCH", expected_out, out_len,
863 tmp + out_misalign, tmplen + tmpflen))
865 if (enc && expected->aead) {
866 unsigned char rtag[16];
868 if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) {
869 t->err = "TAG_LENGTH_INTERNAL_ERROR";
872 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
873 expected->tag_len, rtag)) {
874 t->err = "TAG_RETRIEVE_ERROR";
877 if (!memory_err_compare(t, "TAG_VALUE_MISMATCH",
878 expected->tag, expected->tag_len,
879 rtag, expected->tag_len))
887 EVP_CIPHER_CTX_free(ctx_base);
888 EVP_CIPHER_CTX_free(ctx);
892 static int cipher_test_run(EVP_TEST *t)
894 CIPHER_DATA *cdat = t->data;
896 size_t out_misalign, inp_misalign;
902 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
903 /* IV is optional and usually omitted in wrap mode */
904 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
909 if (cdat->aead && !cdat->tag) {
913 for (out_misalign = 0; out_misalign <= 1;) {
914 static char aux_err[64];
915 t->aux_err = aux_err;
916 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
917 if (inp_misalign == (size_t)-1) {
918 /* kludge: inp_misalign == -1 means "exercise in-place" */
919 BIO_snprintf(aux_err, sizeof(aux_err),
920 "%s in-place, %sfragmented",
921 out_misalign ? "misaligned" : "aligned",
924 BIO_snprintf(aux_err, sizeof(aux_err),
925 "%s output and %s input, %sfragmented",
926 out_misalign ? "misaligned" : "aligned",
927 inp_misalign ? "misaligned" : "aligned",
931 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
932 /* Not fatal errors: return */
939 if (cdat->enc != 1) {
940 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
941 /* Not fatal errors: return */
950 if (out_misalign == 1 && frag == 0) {
952 * XTS, SIV, CCM and Wrap modes have special requirements about input
953 * lengths so we don't fragment for those
955 if (cdat->aead == EVP_CIPH_CCM_MODE
956 || ((EVP_CIPHER_flags(cdat->cipher) & EVP_CIPH_FLAG_CTS) != 0)
957 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_SIV_MODE
958 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
959 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
972 static const EVP_TEST_METHOD cipher_test_method = {
985 typedef struct mac_data_st {
986 /* MAC type in one form or another */
988 EVP_MAC *mac; /* for mac_test_run_mac */
989 int type; /* for mac_test_run_pkey */
990 /* Algorithm string for this MAC */
999 unsigned char *input;
1001 /* Expected output */
1002 unsigned char *output;
1004 unsigned char *custom;
1006 /* MAC salt (blake2) */
1007 unsigned char *salt;
1009 /* Collection of controls */
1010 STACK_OF(OPENSSL_STRING) *controls;
1013 static int mac_test_init(EVP_TEST *t, const char *alg)
1015 EVP_MAC *mac = NULL;
1016 int type = NID_undef;
1019 if (is_mac_disabled(alg)) {
1020 TEST_info("skipping, '%s' is disabled", alg);
1024 if ((mac = EVP_MAC_fetch(libctx, alg, NULL)) == NULL) {
1026 * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
1027 * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
1028 * the EVP_PKEY method.
1030 size_t sz = strlen(alg);
1031 static const char epilogue[] = " by EVP_PKEY";
1033 if (sz >= sizeof(epilogue)
1034 && strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0)
1035 sz -= sizeof(epilogue) - 1;
1037 if (strncmp(alg, "HMAC", sz) == 0)
1038 type = EVP_PKEY_HMAC;
1039 else if (strncmp(alg, "CMAC", sz) == 0)
1040 type = EVP_PKEY_CMAC;
1041 else if (strncmp(alg, "Poly1305", sz) == 0)
1042 type = EVP_PKEY_POLY1305;
1043 else if (strncmp(alg, "SipHash", sz) == 0)
1044 type = EVP_PKEY_SIPHASH;
1049 mdat = OPENSSL_zalloc(sizeof(*mdat));
1051 mdat->mac_name = OPENSSL_strdup(alg);
1053 mdat->controls = sk_OPENSSL_STRING_new_null();
1058 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
1059 static void openssl_free(char *m)
1064 static void mac_test_cleanup(EVP_TEST *t)
1066 MAC_DATA *mdat = t->data;
1068 EVP_MAC_free(mdat->mac);
1069 OPENSSL_free(mdat->mac_name);
1070 sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free);
1071 OPENSSL_free(mdat->alg);
1072 OPENSSL_free(mdat->key);
1073 OPENSSL_free(mdat->iv);
1074 OPENSSL_free(mdat->custom);
1075 OPENSSL_free(mdat->salt);
1076 OPENSSL_free(mdat->input);
1077 OPENSSL_free(mdat->output);
1080 static int mac_test_parse(EVP_TEST *t,
1081 const char *keyword, const char *value)
1083 MAC_DATA *mdata = t->data;
1085 if (strcmp(keyword, "Key") == 0)
1086 return parse_bin(value, &mdata->key, &mdata->key_len);
1087 if (strcmp(keyword, "IV") == 0)
1088 return parse_bin(value, &mdata->iv, &mdata->iv_len);
1089 if (strcmp(keyword, "Custom") == 0)
1090 return parse_bin(value, &mdata->custom, &mdata->custom_len);
1091 if (strcmp(keyword, "Salt") == 0)
1092 return parse_bin(value, &mdata->salt, &mdata->salt_len);
1093 if (strcmp(keyword, "Algorithm") == 0) {
1094 mdata->alg = OPENSSL_strdup(value);
1099 if (strcmp(keyword, "Input") == 0)
1100 return parse_bin(value, &mdata->input, &mdata->input_len);
1101 if (strcmp(keyword, "Output") == 0)
1102 return parse_bin(value, &mdata->output, &mdata->output_len);
1103 if (strcmp(keyword, "Ctrl") == 0)
1104 return sk_OPENSSL_STRING_push(mdata->controls,
1105 OPENSSL_strdup(value)) != 0;
1109 static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1115 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1117 p = strchr(tmpval, ':');
1120 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1122 t->err = "PKEY_CTRL_INVALID";
1124 t->err = "PKEY_CTRL_ERROR";
1127 OPENSSL_free(tmpval);
1131 static int mac_test_run_pkey(EVP_TEST *t)
1133 MAC_DATA *expected = t->data;
1134 EVP_MD_CTX *mctx = NULL;
1135 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1136 EVP_PKEY *key = NULL;
1137 const char *mdname = NULL;
1138 EVP_CIPHER *cipher = NULL;
1139 unsigned char *got = NULL;
1143 if (expected->alg == NULL)
1144 TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type));
1146 TEST_info("Trying the EVP_PKEY %s test with %s",
1147 OBJ_nid2sn(expected->type), expected->alg);
1149 if (expected->type == EVP_PKEY_CMAC) {
1150 if (is_cipher_disabled(expected->alg)) {
1151 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
1156 if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, expected->alg, NULL))) {
1157 t->err = "MAC_KEY_CREATE_ERROR";
1160 key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len,
1163 key = EVP_PKEY_new_raw_private_key_with_libctx(libctx,
1164 OBJ_nid2sn(expected->type),
1165 NULL, expected->key,
1169 t->err = "MAC_KEY_CREATE_ERROR";
1173 if (expected->type == EVP_PKEY_HMAC) {
1174 if (is_digest_disabled(expected->alg)) {
1175 TEST_info("skipping, HMAC '%s' is disabled", expected->alg);
1180 mdname = expected->alg;
1182 if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
1183 t->err = "INTERNAL_ERROR";
1186 if (!EVP_DigestSignInit_ex(mctx, &pctx, mdname, NULL, key, libctx)) {
1187 t->err = "DIGESTSIGNINIT_ERROR";
1190 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++)
1191 if (!mac_test_ctrl_pkey(t, pctx,
1192 sk_OPENSSL_STRING_value(expected->controls,
1194 t->err = "EVPPKEYCTXCTRL_ERROR";
1197 if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) {
1198 t->err = "DIGESTSIGNUPDATE_ERROR";
1201 if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) {
1202 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1205 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1206 t->err = "TEST_FAILURE";
1209 if (!EVP_DigestSignFinal(mctx, got, &got_len)
1210 || !memory_err_compare(t, "TEST_MAC_ERR",
1211 expected->output, expected->output_len,
1213 t->err = "TEST_MAC_ERR";
1218 EVP_CIPHER_free(cipher);
1219 EVP_MD_CTX_free(mctx);
1221 EVP_PKEY_CTX_free(genctx);
1226 static int mac_test_run_mac(EVP_TEST *t)
1228 MAC_DATA *expected = t->data;
1229 EVP_MAC_CTX *ctx = NULL;
1230 unsigned char *got = NULL;
1233 OSSL_PARAM params[21];
1234 size_t params_n = 0;
1235 size_t params_n_allocstart = 0;
1236 const OSSL_PARAM *defined_params =
1237 EVP_MAC_settable_ctx_params(expected->mac);
1239 if (expected->alg == NULL)
1240 TEST_info("Trying the EVP_MAC %s test", expected->mac_name);
1242 TEST_info("Trying the EVP_MAC %s test with %s",
1243 expected->mac_name, expected->alg);
1245 if (expected->alg != NULL) {
1247 * The underlying algorithm may be a cipher or a digest.
1248 * We don't know which it is, but we can ask the MAC what it
1249 * should be and bet on that.
1251 if (OSSL_PARAM_locate_const(defined_params,
1252 OSSL_MAC_PARAM_CIPHER) != NULL) {
1253 params[params_n++] =
1254 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
1256 } else if (OSSL_PARAM_locate_const(defined_params,
1257 OSSL_MAC_PARAM_DIGEST) != NULL) {
1258 params[params_n++] =
1259 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
1262 t->err = "MAC_BAD_PARAMS";
1266 if (expected->key != NULL)
1267 params[params_n++] =
1268 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
1271 if (expected->custom != NULL)
1272 params[params_n++] =
1273 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
1275 expected->custom_len);
1276 if (expected->salt != NULL)
1277 params[params_n++] =
1278 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT,
1280 expected->salt_len);
1281 if (expected->iv != NULL)
1282 params[params_n++] =
1283 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
1287 /* Unknown controls. They must match parameters that the MAC recognizes */
1288 if (params_n + sk_OPENSSL_STRING_num(expected->controls)
1289 >= OSSL_NELEM(params)) {
1290 t->err = "MAC_TOO_MANY_PARAMETERS";
1293 params_n_allocstart = params_n;
1294 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) {
1295 char *tmpkey, *tmpval;
1296 char *value = sk_OPENSSL_STRING_value(expected->controls, i);
1298 if (!TEST_ptr(tmpkey = OPENSSL_strdup(value))) {
1299 t->err = "MAC_PARAM_ERROR";
1302 tmpval = strchr(tmpkey, ':');
1307 || !OSSL_PARAM_allocate_from_text(¶ms[params_n],
1310 strlen(tmpval), NULL)) {
1311 OPENSSL_free(tmpkey);
1312 t->err = "MAC_PARAM_ERROR";
1317 OPENSSL_free(tmpkey);
1319 params[params_n] = OSSL_PARAM_construct_end();
1321 if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) {
1322 t->err = "MAC_CREATE_ERROR";
1326 if (!EVP_MAC_CTX_set_params(ctx, params)) {
1327 t->err = "MAC_BAD_PARAMS";
1330 if (!EVP_MAC_init(ctx)) {
1331 t->err = "MAC_INIT_ERROR";
1334 if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) {
1335 t->err = "MAC_UPDATE_ERROR";
1338 if (!EVP_MAC_final(ctx, NULL, &got_len, 0)) {
1339 t->err = "MAC_FINAL_LENGTH_ERROR";
1342 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1343 t->err = "TEST_FAILURE";
1346 if (!EVP_MAC_final(ctx, got, &got_len, got_len)
1347 || !memory_err_compare(t, "TEST_MAC_ERR",
1348 expected->output, expected->output_len,
1350 t->err = "TEST_MAC_ERR";
1355 while (params_n-- > params_n_allocstart) {
1356 OPENSSL_free(params[params_n].data);
1358 EVP_MAC_CTX_free(ctx);
1363 static int mac_test_run(EVP_TEST *t)
1365 MAC_DATA *expected = t->data;
1367 if (expected->mac != NULL)
1368 return mac_test_run_mac(t);
1369 return mac_test_run_pkey(t);
1372 static const EVP_TEST_METHOD mac_test_method = {
1383 ** These are all very similar and share much common code.
1386 typedef struct pkey_data_st {
1387 /* Context for this operation */
1389 /* Key operation to perform */
1390 int (*keyop) (EVP_PKEY_CTX *ctx,
1391 unsigned char *sig, size_t *siglen,
1392 const unsigned char *tbs, size_t tbslen);
1394 unsigned char *input;
1396 /* Expected output */
1397 unsigned char *output;
1402 * Perform public key operation setup: lookup key, allocated ctx and call
1403 * the appropriate initialisation function
1405 static int pkey_test_init(EVP_TEST *t, const char *name,
1407 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1408 int (*keyop)(EVP_PKEY_CTX *ctx,
1409 unsigned char *sig, size_t *siglen,
1410 const unsigned char *tbs,
1414 EVP_PKEY *pkey = NULL;
1418 rv = find_key(&pkey, name, public_keys);
1420 rv = find_key(&pkey, name, private_keys);
1421 if (rv == 0 || pkey == NULL) {
1422 TEST_info("skipping, key '%s' is disabled", name);
1427 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) {
1428 EVP_PKEY_free(pkey);
1431 kdata->keyop = keyop;
1432 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL))) {
1433 EVP_PKEY_free(pkey);
1434 OPENSSL_free(kdata);
1437 if (keyopinit(kdata->ctx) <= 0)
1438 t->err = "KEYOP_INIT_ERROR";
1443 static void pkey_test_cleanup(EVP_TEST *t)
1445 PKEY_DATA *kdata = t->data;
1447 OPENSSL_free(kdata->input);
1448 OPENSSL_free(kdata->output);
1449 EVP_PKEY_CTX_free(kdata->ctx);
1452 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1458 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1460 p = strchr(tmpval, ':');
1463 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1465 t->err = "PKEY_CTRL_INVALID";
1467 } else if (p != NULL && rv <= 0) {
1468 if (is_digest_disabled(p) || is_cipher_disabled(p)) {
1469 TEST_info("skipping, '%s' is disabled", p);
1473 t->err = "PKEY_CTRL_ERROR";
1477 OPENSSL_free(tmpval);
1481 static int pkey_test_parse(EVP_TEST *t,
1482 const char *keyword, const char *value)
1484 PKEY_DATA *kdata = t->data;
1485 if (strcmp(keyword, "Input") == 0)
1486 return parse_bin(value, &kdata->input, &kdata->input_len);
1487 if (strcmp(keyword, "Output") == 0)
1488 return parse_bin(value, &kdata->output, &kdata->output_len);
1489 if (strcmp(keyword, "Ctrl") == 0)
1490 return pkey_test_ctrl(t, kdata->ctx, value);
1494 static int pkey_test_run(EVP_TEST *t)
1496 PKEY_DATA *expected = t->data;
1497 unsigned char *got = NULL;
1499 EVP_PKEY_CTX *copy = NULL;
1501 if (expected->keyop(expected->ctx, NULL, &got_len,
1502 expected->input, expected->input_len) <= 0
1503 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1504 t->err = "KEYOP_LENGTH_ERROR";
1507 if (expected->keyop(expected->ctx, got, &got_len,
1508 expected->input, expected->input_len) <= 0) {
1509 t->err = "KEYOP_ERROR";
1512 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1513 expected->output, expected->output_len,
1521 /* Repeat the test on a copy. */
1522 if (!TEST_ptr(copy = EVP_PKEY_CTX_dup(expected->ctx))) {
1523 t->err = "INTERNAL_ERROR";
1526 if (expected->keyop(copy, NULL, &got_len, expected->input,
1527 expected->input_len) <= 0
1528 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1529 t->err = "KEYOP_LENGTH_ERROR";
1532 if (expected->keyop(copy, got, &got_len, expected->input,
1533 expected->input_len) <= 0) {
1534 t->err = "KEYOP_ERROR";
1537 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1538 expected->output, expected->output_len,
1544 EVP_PKEY_CTX_free(copy);
1548 static int sign_test_init(EVP_TEST *t, const char *name)
1550 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1553 static const EVP_TEST_METHOD psign_test_method = {
1561 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1563 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1564 EVP_PKEY_verify_recover);
1567 static const EVP_TEST_METHOD pverify_recover_test_method = {
1569 verify_recover_test_init,
1575 static int decrypt_test_init(EVP_TEST *t, const char *name)
1577 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1581 static const EVP_TEST_METHOD pdecrypt_test_method = {
1589 static int verify_test_init(EVP_TEST *t, const char *name)
1591 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1594 static int verify_test_run(EVP_TEST *t)
1596 PKEY_DATA *kdata = t->data;
1598 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1599 kdata->input, kdata->input_len) <= 0)
1600 t->err = "VERIFY_ERROR";
1604 static const EVP_TEST_METHOD pverify_test_method = {
1612 static int pderive_test_init(EVP_TEST *t, const char *name)
1614 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1617 static int pderive_test_parse(EVP_TEST *t,
1618 const char *keyword, const char *value)
1620 PKEY_DATA *kdata = t->data;
1622 if (strcmp(keyword, "PeerKey") == 0) {
1624 if (find_key(&peer, value, public_keys) == 0)
1626 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1630 if (strcmp(keyword, "SharedSecret") == 0)
1631 return parse_bin(value, &kdata->output, &kdata->output_len);
1632 if (strcmp(keyword, "Ctrl") == 0)
1633 return pkey_test_ctrl(t, kdata->ctx, value);
1637 static int pderive_test_run(EVP_TEST *t)
1639 PKEY_DATA *expected = t->data;
1640 unsigned char *got = NULL;
1643 if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) {
1644 t->err = "DERIVE_ERROR";
1647 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1648 t->err = "DERIVE_ERROR";
1651 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
1652 t->err = "DERIVE_ERROR";
1655 if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH",
1656 expected->output, expected->output_len,
1666 static const EVP_TEST_METHOD pderive_test_method = {
1679 typedef enum pbe_type_enum {
1680 PBE_TYPE_INVALID = 0,
1681 PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
1684 typedef struct pbe_data_st {
1686 /* scrypt parameters */
1687 uint64_t N, r, p, maxmem;
1688 /* PKCS#12 parameters */
1692 unsigned char *pass;
1695 unsigned char *salt;
1697 /* Expected output */
1702 #ifndef OPENSSL_NO_SCRYPT
1703 /* Parse unsigned decimal 64 bit integer value */
1704 static int parse_uint64(const char *value, uint64_t *pr)
1706 const char *p = value;
1708 if (!TEST_true(*p)) {
1709 TEST_info("Invalid empty integer value");
1712 for (*pr = 0; *p; ) {
1713 if (*pr > UINT64_MAX / 10) {
1714 TEST_error("Integer overflow in string %s", value);
1718 if (!TEST_true(isdigit((unsigned char)*p))) {
1719 TEST_error("Invalid character in string %s", value);
1728 static int scrypt_test_parse(EVP_TEST *t,
1729 const char *keyword, const char *value)
1731 PBE_DATA *pdata = t->data;
1733 if (strcmp(keyword, "N") == 0)
1734 return parse_uint64(value, &pdata->N);
1735 if (strcmp(keyword, "p") == 0)
1736 return parse_uint64(value, &pdata->p);
1737 if (strcmp(keyword, "r") == 0)
1738 return parse_uint64(value, &pdata->r);
1739 if (strcmp(keyword, "maxmem") == 0)
1740 return parse_uint64(value, &pdata->maxmem);
1745 static int pbkdf2_test_parse(EVP_TEST *t,
1746 const char *keyword, const char *value)
1748 PBE_DATA *pdata = t->data;
1750 if (strcmp(keyword, "iter") == 0) {
1751 pdata->iter = atoi(value);
1752 if (pdata->iter <= 0)
1756 if (strcmp(keyword, "MD") == 0) {
1757 pdata->md = EVP_get_digestbyname(value);
1758 if (pdata->md == NULL)
1765 static int pkcs12_test_parse(EVP_TEST *t,
1766 const char *keyword, const char *value)
1768 PBE_DATA *pdata = t->data;
1770 if (strcmp(keyword, "id") == 0) {
1771 pdata->id = atoi(value);
1776 return pbkdf2_test_parse(t, keyword, value);
1779 static int pbe_test_init(EVP_TEST *t, const char *alg)
1782 PBE_TYPE pbe_type = PBE_TYPE_INVALID;
1784 if (is_kdf_disabled(alg)) {
1785 TEST_info("skipping, '%s' is disabled", alg);
1789 if (strcmp(alg, "scrypt") == 0) {
1790 pbe_type = PBE_TYPE_SCRYPT;
1791 } else if (strcmp(alg, "pbkdf2") == 0) {
1792 pbe_type = PBE_TYPE_PBKDF2;
1793 } else if (strcmp(alg, "pkcs12") == 0) {
1794 pbe_type = PBE_TYPE_PKCS12;
1796 TEST_error("Unknown pbe algorithm %s", alg);
1798 pdat = OPENSSL_zalloc(sizeof(*pdat));
1799 pdat->pbe_type = pbe_type;
1804 static void pbe_test_cleanup(EVP_TEST *t)
1806 PBE_DATA *pdat = t->data;
1808 OPENSSL_free(pdat->pass);
1809 OPENSSL_free(pdat->salt);
1810 OPENSSL_free(pdat->key);
1813 static int pbe_test_parse(EVP_TEST *t,
1814 const char *keyword, const char *value)
1816 PBE_DATA *pdata = t->data;
1818 if (strcmp(keyword, "Password") == 0)
1819 return parse_bin(value, &pdata->pass, &pdata->pass_len);
1820 if (strcmp(keyword, "Salt") == 0)
1821 return parse_bin(value, &pdata->salt, &pdata->salt_len);
1822 if (strcmp(keyword, "Key") == 0)
1823 return parse_bin(value, &pdata->key, &pdata->key_len);
1824 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1825 return pbkdf2_test_parse(t, keyword, value);
1826 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1827 return pkcs12_test_parse(t, keyword, value);
1828 #ifndef OPENSSL_NO_SCRYPT
1829 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1830 return scrypt_test_parse(t, keyword, value);
1835 static int pbe_test_run(EVP_TEST *t)
1837 PBE_DATA *expected = t->data;
1839 EVP_MD *fetched_digest = NULL;
1840 OPENSSL_CTX *save_libctx;
1842 save_libctx = OPENSSL_CTX_set0_default(libctx);
1844 if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
1845 t->err = "INTERNAL_ERROR";
1848 if (expected->pbe_type == PBE_TYPE_PBKDF2) {
1849 if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
1850 expected->salt, expected->salt_len,
1851 expected->iter, expected->md,
1852 expected->key_len, key) == 0) {
1853 t->err = "PBKDF2_ERROR";
1856 #ifndef OPENSSL_NO_SCRYPT
1857 } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
1858 if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
1859 expected->salt, expected->salt_len,
1860 expected->N, expected->r, expected->p,
1861 expected->maxmem, key, expected->key_len) == 0) {
1862 t->err = "SCRYPT_ERROR";
1866 } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
1867 fetched_digest = EVP_MD_fetch(libctx, EVP_MD_name(expected->md), NULL);
1868 if (fetched_digest == NULL) {
1869 t->err = "PKCS12_ERROR";
1872 if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
1873 expected->salt, expected->salt_len,
1874 expected->id, expected->iter, expected->key_len,
1875 key, fetched_digest) == 0) {
1876 t->err = "PKCS12_ERROR";
1880 if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len,
1881 key, expected->key_len))
1886 EVP_MD_free(fetched_digest);
1888 OPENSSL_CTX_set0_default(save_libctx);
1892 static const EVP_TEST_METHOD pbe_test_method = {
1906 BASE64_CANONICAL_ENCODING = 0,
1907 BASE64_VALID_ENCODING = 1,
1908 BASE64_INVALID_ENCODING = 2
1909 } base64_encoding_type;
1911 typedef struct encode_data_st {
1912 /* Input to encoding */
1913 unsigned char *input;
1915 /* Expected output */
1916 unsigned char *output;
1918 base64_encoding_type encoding;
1921 static int encode_test_init(EVP_TEST *t, const char *encoding)
1925 if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
1927 if (strcmp(encoding, "canonical") == 0) {
1928 edata->encoding = BASE64_CANONICAL_ENCODING;
1929 } else if (strcmp(encoding, "valid") == 0) {
1930 edata->encoding = BASE64_VALID_ENCODING;
1931 } else if (strcmp(encoding, "invalid") == 0) {
1932 edata->encoding = BASE64_INVALID_ENCODING;
1933 if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
1936 TEST_error("Bad encoding: %s."
1937 " Should be one of {canonical, valid, invalid}",
1944 OPENSSL_free(edata);
1948 static void encode_test_cleanup(EVP_TEST *t)
1950 ENCODE_DATA *edata = t->data;
1952 OPENSSL_free(edata->input);
1953 OPENSSL_free(edata->output);
1954 memset(edata, 0, sizeof(*edata));
1957 static int encode_test_parse(EVP_TEST *t,
1958 const char *keyword, const char *value)
1960 ENCODE_DATA *edata = t->data;
1962 if (strcmp(keyword, "Input") == 0)
1963 return parse_bin(value, &edata->input, &edata->input_len);
1964 if (strcmp(keyword, "Output") == 0)
1965 return parse_bin(value, &edata->output, &edata->output_len);
1969 static int encode_test_run(EVP_TEST *t)
1971 ENCODE_DATA *expected = t->data;
1972 unsigned char *encode_out = NULL, *decode_out = NULL;
1973 int output_len, chunk_len;
1974 EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL;
1976 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
1977 t->err = "INTERNAL_ERROR";
1981 if (expected->encoding == BASE64_CANONICAL_ENCODING) {
1983 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
1984 || !TEST_ptr(encode_out =
1985 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
1988 EVP_EncodeInit(encode_ctx);
1989 if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1990 expected->input, expected->input_len)))
1993 output_len = chunk_len;
1995 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
1996 output_len += chunk_len;
1998 if (!memory_err_compare(t, "BAD_ENCODING",
1999 expected->output, expected->output_len,
2000 encode_out, output_len))
2004 if (!TEST_ptr(decode_out =
2005 OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
2008 EVP_DecodeInit(decode_ctx);
2009 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
2010 expected->output_len) < 0) {
2011 t->err = "DECODE_ERROR";
2014 output_len = chunk_len;
2016 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
2017 t->err = "DECODE_ERROR";
2020 output_len += chunk_len;
2022 if (expected->encoding != BASE64_INVALID_ENCODING
2023 && !memory_err_compare(t, "BAD_DECODING",
2024 expected->input, expected->input_len,
2025 decode_out, output_len)) {
2026 t->err = "BAD_DECODING";
2032 OPENSSL_free(encode_out);
2033 OPENSSL_free(decode_out);
2034 EVP_ENCODE_CTX_free(decode_ctx);
2035 EVP_ENCODE_CTX_free(encode_ctx);
2039 static const EVP_TEST_METHOD encode_test_method = {
2042 encode_test_cleanup,
2051 #define MAX_RAND_REPEATS 15
2053 typedef struct rand_data_pass_st {
2054 unsigned char *entropy;
2055 unsigned char *reseed_entropy;
2056 unsigned char *nonce;
2057 unsigned char *pers;
2058 unsigned char *reseed_addin;
2059 unsigned char *addinA;
2060 unsigned char *addinB;
2061 unsigned char *pr_entropyA;
2062 unsigned char *pr_entropyB;
2063 unsigned char *output;
2064 size_t entropy_len, nonce_len, pers_len, addinA_len, addinB_len,
2065 pr_entropyA_len, pr_entropyB_len, output_len, reseed_entropy_len,
2069 typedef struct rand_data_st {
2070 /* Context for this operation */
2072 EVP_RAND_CTX *parent;
2074 int prediction_resistance;
2076 unsigned int generate_bits;
2080 /* Expected output */
2081 RAND_DATA_PASS data[MAX_RAND_REPEATS];
2084 static int rand_test_init(EVP_TEST *t, const char *name)
2088 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
2089 unsigned int strength = 256;
2091 if (!TEST_ptr(rdata = OPENSSL_zalloc(sizeof(*rdata))))
2094 rand = EVP_RAND_fetch(libctx, "TEST-RAND", NULL);
2097 rdata->parent = EVP_RAND_CTX_new(rand, NULL);
2098 EVP_RAND_free(rand);
2099 if (rdata->parent == NULL)
2102 *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength);
2103 if (!EVP_RAND_set_ctx_params(rdata->parent, params))
2106 rand = EVP_RAND_fetch(libctx, name, NULL);
2109 rdata->ctx = EVP_RAND_CTX_new(rand, rdata->parent);
2110 EVP_RAND_free(rand);
2111 if (rdata->ctx == NULL)
2118 EVP_RAND_CTX_free(rdata->parent);
2119 OPENSSL_free(rdata);
2123 static void rand_test_cleanup(EVP_TEST *t)
2125 RAND_DATA *rdata = t->data;
2128 OPENSSL_free(rdata->cipher);
2129 OPENSSL_free(rdata->digest);
2131 for (i = 0; i <= rdata->n; i++) {
2132 OPENSSL_free(rdata->data[i].entropy);
2133 OPENSSL_free(rdata->data[i].reseed_entropy);
2134 OPENSSL_free(rdata->data[i].nonce);
2135 OPENSSL_free(rdata->data[i].pers);
2136 OPENSSL_free(rdata->data[i].reseed_addin);
2137 OPENSSL_free(rdata->data[i].addinA);
2138 OPENSSL_free(rdata->data[i].addinB);
2139 OPENSSL_free(rdata->data[i].pr_entropyA);
2140 OPENSSL_free(rdata->data[i].pr_entropyB);
2141 OPENSSL_free(rdata->data[i].output);
2143 EVP_RAND_CTX_free(rdata->ctx);
2144 EVP_RAND_CTX_free(rdata->parent);
2147 static int rand_test_parse(EVP_TEST *t,
2148 const char *keyword, const char *value)
2150 RAND_DATA *rdata = t->data;
2151 RAND_DATA_PASS *item;
2155 if ((p = strchr(keyword, '.')) != NULL) {
2157 if (n >= MAX_RAND_REPEATS)
2161 item = rdata->data + n;
2162 if (strncmp(keyword, "Entropy.", sizeof("Entropy")) == 0)
2163 return parse_bin(value, &item->entropy, &item->entropy_len);
2164 if (strncmp(keyword, "ReseedEntropy.", sizeof("ReseedEntropy")) == 0)
2165 return parse_bin(value, &item->reseed_entropy,
2166 &item->reseed_entropy_len);
2167 if (strncmp(keyword, "Nonce.", sizeof("Nonce")) == 0)
2168 return parse_bin(value, &item->nonce, &item->nonce_len);
2169 if (strncmp(keyword, "PersonalisationString.",
2170 sizeof("PersonalisationString")) == 0)
2171 return parse_bin(value, &item->pers, &item->pers_len);
2172 if (strncmp(keyword, "ReseedAdditionalInput.",
2173 sizeof("ReseedAdditionalInput")) == 0)
2174 return parse_bin(value, &item->reseed_addin,
2175 &item->reseed_addin_len);
2176 if (strncmp(keyword, "AdditionalInputA.",
2177 sizeof("AdditionalInputA")) == 0)
2178 return parse_bin(value, &item->addinA, &item->addinA_len);
2179 if (strncmp(keyword, "AdditionalInputB.",
2180 sizeof("AdditionalInputB")) == 0)
2181 return parse_bin(value, &item->addinB, &item->addinB_len);
2182 if (strncmp(keyword, "EntropyPredictionResistanceA.",
2183 sizeof("EntropyPredictionResistanceA")) == 0)
2184 return parse_bin(value, &item->pr_entropyA, &item->pr_entropyA_len);
2185 if (strncmp(keyword, "EntropyPredictionResistanceB.",
2186 sizeof("EntropyPredictionResistanceB")) == 0)
2187 return parse_bin(value, &item->pr_entropyB, &item->pr_entropyB_len);
2188 if (strncmp(keyword, "Output.", sizeof("Output")) == 0)
2189 return parse_bin(value, &item->output, &item->output_len);
2191 if (strcmp(keyword, "Cipher") == 0)
2192 return TEST_ptr(rdata->cipher = OPENSSL_strdup(value));
2193 if (strcmp(keyword, "Digest") == 0)
2194 return TEST_ptr(rdata->digest = OPENSSL_strdup(value));
2195 if (strcmp(keyword, "DerivationFunction") == 0) {
2196 rdata->use_df = atoi(value) != 0;
2199 if (strcmp(keyword, "GenerateBits") == 0) {
2200 if ((n = atoi(value)) <= 0 || n % 8 != 0)
2202 rdata->generate_bits = (unsigned int)n;
2205 if (strcmp(keyword, "PredictionResistance") == 0) {
2206 rdata->prediction_resistance = atoi(value) != 0;
2213 static int rand_test_run(EVP_TEST *t)
2215 RAND_DATA *expected = t->data;
2216 RAND_DATA_PASS *item;
2218 size_t got_len = expected->generate_bits / 8;
2219 OSSL_PARAM params[5], *p = params;
2220 int i = -1, ret = 0;
2221 unsigned int strength;
2224 if (!TEST_ptr(got = OPENSSL_malloc(got_len)))
2227 *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &expected->use_df);
2228 if (expected->cipher != NULL)
2229 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
2230 expected->cipher, 0);
2231 if (expected->digest != NULL)
2232 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
2233 expected->digest, 0);
2234 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC, "HMAC", 0);
2235 *p = OSSL_PARAM_construct_end();
2236 if (!TEST_true(EVP_RAND_set_ctx_params(expected->ctx, params)))
2239 strength = EVP_RAND_strength(expected->ctx);
2240 for (i = 0; i <= expected->n; i++) {
2241 item = expected->data + i;
2244 z = item->entropy != NULL ? item->entropy : (unsigned char *)"";
2245 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
2246 z, item->entropy_len);
2247 z = item->nonce != NULL ? item->nonce : (unsigned char *)"";
2248 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
2249 z, item->nonce_len);
2250 *p = OSSL_PARAM_construct_end();
2251 if (!TEST_true(EVP_RAND_set_ctx_params(expected->parent, params))
2252 || !TEST_true(EVP_RAND_instantiate(expected->parent, strength,
2256 z = item->pers != NULL ? item->pers : (unsigned char *)"";
2257 if (!TEST_true(EVP_RAND_instantiate
2258 (expected->ctx, strength,
2259 expected->prediction_resistance, z,
2263 if (item->reseed_entropy != NULL) {
2264 params[0] = OSSL_PARAM_construct_octet_string
2265 (OSSL_RAND_PARAM_TEST_ENTROPY, item->reseed_entropy,
2266 item->reseed_entropy_len);
2267 params[1] = OSSL_PARAM_construct_end();
2268 if (!TEST_true(EVP_RAND_set_ctx_params(expected->parent, params)))
2271 if (!TEST_true(EVP_RAND_reseed
2272 (expected->ctx, expected->prediction_resistance,
2273 NULL, 0, item->reseed_addin,
2274 item->reseed_addin_len)))
2277 if (item->pr_entropyA != NULL) {
2278 params[0] = OSSL_PARAM_construct_octet_string
2279 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyA,
2280 item->pr_entropyA_len);
2281 params[1] = OSSL_PARAM_construct_end();
2282 if (!TEST_true(EVP_RAND_set_ctx_params(expected->parent, params)))
2285 if (!TEST_true(EVP_RAND_generate
2286 (expected->ctx, got, got_len,
2287 strength, expected->prediction_resistance,
2288 item->addinA, item->addinA_len)))
2291 if (item->pr_entropyB != NULL) {
2292 params[0] = OSSL_PARAM_construct_octet_string
2293 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyB,
2294 item->pr_entropyB_len);
2295 params[1] = OSSL_PARAM_construct_end();
2296 if (!TEST_true(EVP_RAND_set_ctx_params(expected->parent, params)))
2299 if (!TEST_true(EVP_RAND_generate
2300 (expected->ctx, got, got_len,
2301 strength, expected->prediction_resistance,
2302 item->addinB, item->addinB_len)))
2304 if (!TEST_mem_eq(got, got_len, item->output, item->output_len))
2306 if (!TEST_true(EVP_RAND_uninstantiate(expected->ctx))
2307 || !TEST_true(EVP_RAND_uninstantiate(expected->parent))
2308 || !TEST_true(EVP_RAND_verify_zeroization(expected->ctx))
2309 || !TEST_int_eq(EVP_RAND_state(expected->ctx),
2310 EVP_RAND_STATE_UNINITIALISED))
2317 if (ret == 0 && i >= 0)
2318 TEST_info("Error in test case %d of %d\n", i, expected->n + 1);
2323 static const EVP_TEST_METHOD rand_test_method = {
2335 typedef struct kdf_data_st {
2336 /* Context for this operation */
2338 /* Expected output */
2339 unsigned char *output;
2341 OSSL_PARAM params[20];
2346 * Perform public key operation setup: lookup key, allocated ctx and call
2347 * the appropriate initialisation function
2349 static int kdf_test_init(EVP_TEST *t, const char *name)
2354 if (is_kdf_disabled(name)) {
2355 TEST_info("skipping, '%s' is disabled", name);
2360 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2362 kdata->p = kdata->params;
2363 *kdata->p = OSSL_PARAM_construct_end();
2365 kdf = EVP_KDF_fetch(libctx, name, NULL);
2367 OPENSSL_free(kdata);
2370 kdata->ctx = EVP_KDF_CTX_new(kdf);
2372 if (kdata->ctx == NULL) {
2373 OPENSSL_free(kdata);
2380 static void kdf_test_cleanup(EVP_TEST *t)
2382 KDF_DATA *kdata = t->data;
2385 for (p = kdata->params; p->key != NULL; p++)
2386 OPENSSL_free(p->data);
2387 OPENSSL_free(kdata->output);
2388 EVP_KDF_CTX_free(kdata->ctx);
2391 static int kdf_test_ctrl(EVP_TEST *t, EVP_KDF_CTX *kctx,
2394 KDF_DATA *kdata = t->data;
2397 const OSSL_PARAM *defs = EVP_KDF_settable_ctx_params(EVP_KDF_CTX_kdf(kctx));
2399 if (!TEST_ptr(name = OPENSSL_strdup(value)))
2401 p = strchr(name, ':');
2405 rv = OSSL_PARAM_allocate_from_text(kdata->p, defs, name, p,
2406 p != NULL ? strlen(p) : 0, NULL);
2407 *++kdata->p = OSSL_PARAM_construct_end();
2409 t->err = "KDF_PARAM_ERROR";
2413 if (p != NULL && strcmp(name, "digest") == 0) {
2414 if (is_digest_disabled(p)) {
2415 TEST_info("skipping, '%s' is disabled", p);
2419 if (p != NULL && strcmp(name, "cipher") == 0) {
2420 if (is_cipher_disabled(p)) {
2421 TEST_info("skipping, '%s' is disabled", p);
2429 static int kdf_test_parse(EVP_TEST *t,
2430 const char *keyword, const char *value)
2432 KDF_DATA *kdata = t->data;
2434 if (strcmp(keyword, "Output") == 0)
2435 return parse_bin(value, &kdata->output, &kdata->output_len);
2436 if (strncmp(keyword, "Ctrl", 4) == 0)
2437 return kdf_test_ctrl(t, kdata->ctx, value);
2441 static int kdf_test_run(EVP_TEST *t)
2443 KDF_DATA *expected = t->data;
2444 unsigned char *got = NULL;
2445 size_t got_len = expected->output_len;
2447 if (!EVP_KDF_CTX_set_params(expected->ctx, expected->params)) {
2448 t->err = "KDF_CTRL_ERROR";
2451 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2452 t->err = "INTERNAL_ERROR";
2455 if (EVP_KDF_derive(expected->ctx, got, got_len) <= 0) {
2456 t->err = "KDF_DERIVE_ERROR";
2459 if (!memory_err_compare(t, "KDF_MISMATCH",
2460 expected->output, expected->output_len,
2471 static const EVP_TEST_METHOD kdf_test_method = {
2483 typedef struct pkey_kdf_data_st {
2484 /* Context for this operation */
2486 /* Expected output */
2487 unsigned char *output;
2492 * Perform public key operation setup: lookup key, allocated ctx and call
2493 * the appropriate initialisation function
2495 static int pkey_kdf_test_init(EVP_TEST *t, const char *name)
2497 OPENSSL_CTX *save_libctx = NULL;
2498 PKEY_KDF_DATA *kdata = NULL;
2499 int kdf_nid = OBJ_sn2nid(name);
2501 if (is_kdf_disabled(name)) {
2502 TEST_info("skipping, '%s' is disabled", name);
2507 if (kdf_nid == NID_undef)
2508 kdf_nid = OBJ_ln2nid(name);
2510 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2513 * TODO(3.0): This should be using EVP_PKEY_CTX_new_from_name(),
2514 * but it does not currently since the PKEY_KDF is using legacy paths.
2515 * Internally it still uses fetches with the legacy path,
2516 * So for now we hack in the library context.
2518 save_libctx = OPENSSL_CTX_set0_default(libctx);
2519 kdata->ctx = EVP_PKEY_CTX_new_id(kdf_nid, NULL);
2520 if (kdata->ctx == NULL
2521 || EVP_PKEY_derive_init(kdata->ctx) <= 0)
2524 OPENSSL_CTX_set0_default(save_libctx);
2528 OPENSSL_CTX_set0_default(save_libctx);
2529 EVP_PKEY_CTX_free(kdata->ctx);
2530 OPENSSL_free(kdata);
2534 static void pkey_kdf_test_cleanup(EVP_TEST *t)
2536 PKEY_KDF_DATA *kdata = t->data;
2538 OPENSSL_free(kdata->output);
2539 EVP_PKEY_CTX_free(kdata->ctx);
2542 static int pkey_kdf_test_parse(EVP_TEST *t,
2543 const char *keyword, const char *value)
2545 PKEY_KDF_DATA *kdata = t->data;
2547 if (strcmp(keyword, "Output") == 0)
2548 return parse_bin(value, &kdata->output, &kdata->output_len);
2549 if (strncmp(keyword, "Ctrl", 4) == 0)
2550 return pkey_test_ctrl(t, kdata->ctx, value);
2554 static int pkey_kdf_test_run(EVP_TEST *t)
2556 PKEY_KDF_DATA *expected = t->data;
2557 unsigned char *got = NULL;
2558 size_t got_len = expected->output_len;
2560 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2561 t->err = "INTERNAL_ERROR";
2564 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
2565 t->err = "KDF_DERIVE_ERROR";
2568 if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
2569 t->err = "KDF_MISMATCH";
2579 static const EVP_TEST_METHOD pkey_kdf_test_method = {
2582 pkey_kdf_test_cleanup,
2583 pkey_kdf_test_parse,
2591 typedef struct keypair_test_data_st {
2594 } KEYPAIR_TEST_DATA;
2596 static int keypair_test_init(EVP_TEST *t, const char *pair)
2598 KEYPAIR_TEST_DATA *data;
2600 EVP_PKEY *pk = NULL, *pubk = NULL;
2601 char *pub, *priv = NULL;
2603 /* Split private and public names. */
2604 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
2605 || !TEST_ptr(pub = strchr(priv, ':'))) {
2606 t->err = "PARSING_ERROR";
2611 if (!TEST_true(find_key(&pk, priv, private_keys))) {
2612 TEST_info("Can't find private key: %s", priv);
2613 t->err = "MISSING_PRIVATE_KEY";
2616 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
2617 TEST_info("Can't find public key: %s", pub);
2618 t->err = "MISSING_PUBLIC_KEY";
2622 if (pk == NULL && pubk == NULL) {
2623 /* Both keys are listed but unsupported: skip this test */
2629 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2642 static void keypair_test_cleanup(EVP_TEST *t)
2644 OPENSSL_free(t->data);
2649 * For tests that do not accept any custom keywords.
2651 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
2656 static int keypair_test_run(EVP_TEST *t)
2659 const KEYPAIR_TEST_DATA *pair = t->data;
2661 if (pair->privk == NULL || pair->pubk == NULL) {
2663 * this can only happen if only one of the keys is not set
2664 * which means that one of them was unsupported while the
2665 * other isn't: hence a key type mismatch.
2667 t->err = "KEYPAIR_TYPE_MISMATCH";
2672 if ((rv = EVP_PKEY_eq(pair->privk, pair->pubk)) != 1 ) {
2674 t->err = "KEYPAIR_MISMATCH";
2675 } else if ( -1 == rv ) {
2676 t->err = "KEYPAIR_TYPE_MISMATCH";
2677 } else if ( -2 == rv ) {
2678 t->err = "UNSUPPORTED_KEY_COMPARISON";
2680 TEST_error("Unexpected error in key comparison");
2695 static const EVP_TEST_METHOD keypair_test_method = {
2698 keypair_test_cleanup,
2707 typedef struct keygen_test_data_st {
2708 EVP_PKEY_CTX *genctx; /* Keygen context to use */
2709 char *keyname; /* Key name to store key or NULL */
2712 static int keygen_test_init(EVP_TEST *t, const char *alg)
2714 KEYGEN_TEST_DATA *data;
2715 EVP_PKEY_CTX *genctx;
2716 int nid = OBJ_sn2nid(alg);
2718 if (nid == NID_undef) {
2719 nid = OBJ_ln2nid(alg);
2720 if (nid == NID_undef)
2724 if (is_pkey_disabled(alg)) {
2728 if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_from_name(libctx, alg, NULL)))
2731 if (EVP_PKEY_keygen_init(genctx) <= 0) {
2732 t->err = "KEYGEN_INIT_ERROR";
2736 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2738 data->genctx = genctx;
2739 data->keyname = NULL;
2745 EVP_PKEY_CTX_free(genctx);
2749 static void keygen_test_cleanup(EVP_TEST *t)
2751 KEYGEN_TEST_DATA *keygen = t->data;
2753 EVP_PKEY_CTX_free(keygen->genctx);
2754 OPENSSL_free(keygen->keyname);
2755 OPENSSL_free(t->data);
2759 static int keygen_test_parse(EVP_TEST *t,
2760 const char *keyword, const char *value)
2762 KEYGEN_TEST_DATA *keygen = t->data;
2764 if (strcmp(keyword, "KeyName") == 0)
2765 return TEST_ptr(keygen->keyname = OPENSSL_strdup(value));
2766 if (strcmp(keyword, "Ctrl") == 0)
2767 return pkey_test_ctrl(t, keygen->genctx, value);
2771 static int keygen_test_run(EVP_TEST *t)
2773 KEYGEN_TEST_DATA *keygen = t->data;
2774 EVP_PKEY *pkey = NULL;
2777 if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) {
2778 t->err = "KEYGEN_GENERATE_ERROR";
2782 if (!evp_pkey_is_provided(pkey)) {
2783 TEST_info("Warning: legacy key generated %s", keygen->keyname);
2786 if (keygen->keyname != NULL) {
2790 if (find_key(NULL, keygen->keyname, private_keys)) {
2791 TEST_info("Duplicate key %s", keygen->keyname);
2795 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
2797 key->name = keygen->keyname;
2798 keygen->keyname = NULL;
2800 key->next = private_keys;
2804 EVP_PKEY_free(pkey);
2813 static const EVP_TEST_METHOD keygen_test_method = {
2816 keygen_test_cleanup,
2822 ** DIGEST SIGN+VERIFY TESTS
2826 int is_verify; /* Set to 1 if verifying */
2827 int is_oneshot; /* Set to 1 for one shot operation */
2828 const EVP_MD *md; /* Digest to use */
2829 EVP_MD_CTX *ctx; /* Digest context */
2831 STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
2832 unsigned char *osin; /* Input data if one shot */
2833 size_t osin_len; /* Input length data if one shot */
2834 unsigned char *output; /* Expected output */
2835 size_t output_len; /* Expected output length */
2838 static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
2841 const EVP_MD *md = NULL;
2842 DIGESTSIGN_DATA *mdat;
2844 if (strcmp(alg, "NULL") != 0) {
2845 if (is_digest_disabled(alg)) {
2849 md = EVP_get_digestbyname(alg);
2853 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
2856 if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
2860 mdat->is_verify = is_verify;
2861 mdat->is_oneshot = is_oneshot;
2866 static int digestsign_test_init(EVP_TEST *t, const char *alg)
2868 return digestsigver_test_init(t, alg, 0, 0);
2871 static void digestsigver_test_cleanup(EVP_TEST *t)
2873 DIGESTSIGN_DATA *mdata = t->data;
2875 EVP_MD_CTX_free(mdata->ctx);
2876 sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
2877 OPENSSL_free(mdata->osin);
2878 OPENSSL_free(mdata->output);
2879 OPENSSL_free(mdata);
2883 static int digestsigver_test_parse(EVP_TEST *t,
2884 const char *keyword, const char *value)
2886 DIGESTSIGN_DATA *mdata = t->data;
2888 if (strcmp(keyword, "Key") == 0) {
2889 EVP_PKEY *pkey = NULL;
2891 const char *name = mdata->md == NULL ? NULL : EVP_MD_name(mdata->md);
2893 if (mdata->is_verify)
2894 rv = find_key(&pkey, value, public_keys);
2896 rv = find_key(&pkey, value, private_keys);
2897 if (rv == 0 || pkey == NULL) {
2901 if (mdata->is_verify) {
2902 if (!EVP_DigestVerifyInit_ex(mdata->ctx, &mdata->pctx,
2903 name, NULL, pkey, libctx))
2904 t->err = "DIGESTVERIFYINIT_ERROR";
2907 if (!EVP_DigestSignInit_ex(mdata->ctx, &mdata->pctx,
2908 name, NULL, pkey, libctx))
2909 t->err = "DIGESTSIGNINIT_ERROR";
2913 if (strcmp(keyword, "Input") == 0) {
2914 if (mdata->is_oneshot)
2915 return parse_bin(value, &mdata->osin, &mdata->osin_len);
2916 return evp_test_buffer_append(value, &mdata->input);
2918 if (strcmp(keyword, "Output") == 0)
2919 return parse_bin(value, &mdata->output, &mdata->output_len);
2921 if (!mdata->is_oneshot) {
2922 if (strcmp(keyword, "Count") == 0)
2923 return evp_test_buffer_set_count(value, mdata->input);
2924 if (strcmp(keyword, "Ncopy") == 0)
2925 return evp_test_buffer_ncopy(value, mdata->input);
2927 if (strcmp(keyword, "Ctrl") == 0) {
2928 if (mdata->pctx == NULL)
2930 return pkey_test_ctrl(t, mdata->pctx, value);
2935 static int digestsign_update_fn(void *ctx, const unsigned char *buf,
2938 return EVP_DigestSignUpdate(ctx, buf, buflen);
2941 static int digestsign_test_run(EVP_TEST *t)
2943 DIGESTSIGN_DATA *expected = t->data;
2944 unsigned char *got = NULL;
2947 if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
2949 t->err = "DIGESTUPDATE_ERROR";
2953 if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
2954 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
2957 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2958 t->err = "MALLOC_FAILURE";
2961 if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
2962 t->err = "DIGESTSIGNFINAL_ERROR";
2965 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
2966 expected->output, expected->output_len,
2976 static const EVP_TEST_METHOD digestsign_test_method = {
2978 digestsign_test_init,
2979 digestsigver_test_cleanup,
2980 digestsigver_test_parse,
2984 static int digestverify_test_init(EVP_TEST *t, const char *alg)
2986 return digestsigver_test_init(t, alg, 1, 0);
2989 static int digestverify_update_fn(void *ctx, const unsigned char *buf,
2992 return EVP_DigestVerifyUpdate(ctx, buf, buflen);
2995 static int digestverify_test_run(EVP_TEST *t)
2997 DIGESTSIGN_DATA *mdata = t->data;
2999 if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
3000 t->err = "DIGESTUPDATE_ERROR";
3004 if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
3005 mdata->output_len) <= 0)
3006 t->err = "VERIFY_ERROR";
3010 static const EVP_TEST_METHOD digestverify_test_method = {
3012 digestverify_test_init,
3013 digestsigver_test_cleanup,
3014 digestsigver_test_parse,
3015 digestverify_test_run
3018 static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
3020 return digestsigver_test_init(t, alg, 0, 1);
3023 static int oneshot_digestsign_test_run(EVP_TEST *t)
3025 DIGESTSIGN_DATA *expected = t->data;
3026 unsigned char *got = NULL;
3029 if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
3030 expected->osin, expected->osin_len)) {
3031 t->err = "DIGESTSIGN_LENGTH_ERROR";
3034 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
3035 t->err = "MALLOC_FAILURE";
3038 if (!EVP_DigestSign(expected->ctx, got, &got_len,
3039 expected->osin, expected->osin_len)) {
3040 t->err = "DIGESTSIGN_ERROR";
3043 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
3044 expected->output, expected->output_len,
3054 static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
3055 "OneShotDigestSign",
3056 oneshot_digestsign_test_init,
3057 digestsigver_test_cleanup,
3058 digestsigver_test_parse,
3059 oneshot_digestsign_test_run
3062 static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
3064 return digestsigver_test_init(t, alg, 1, 1);
3067 static int oneshot_digestverify_test_run(EVP_TEST *t)
3069 DIGESTSIGN_DATA *mdata = t->data;
3071 if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
3072 mdata->osin, mdata->osin_len) <= 0)
3073 t->err = "VERIFY_ERROR";
3077 static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
3078 "OneShotDigestVerify",
3079 oneshot_digestverify_test_init,
3080 digestsigver_test_cleanup,
3081 digestsigver_test_parse,
3082 oneshot_digestverify_test_run
3087 ** PARSING AND DISPATCH
3090 static const EVP_TEST_METHOD *evp_test_list[] = {
3092 &cipher_test_method,
3093 &digest_test_method,
3094 &digestsign_test_method,
3095 &digestverify_test_method,
3096 &encode_test_method,
3098 &pkey_kdf_test_method,
3099 &keypair_test_method,
3100 &keygen_test_method,
3102 &oneshot_digestsign_test_method,
3103 &oneshot_digestverify_test_method,
3105 &pdecrypt_test_method,
3106 &pderive_test_method,
3108 &pverify_recover_test_method,
3109 &pverify_test_method,
3113 static const EVP_TEST_METHOD *find_test(const char *name)
3115 const EVP_TEST_METHOD **tt;
3117 for (tt = evp_test_list; *tt; tt++) {
3118 if (strcmp(name, (*tt)->name) == 0)
3124 static void clear_test(EVP_TEST *t)
3126 test_clearstanza(&t->s);
3128 if (t->data != NULL) {
3129 if (t->meth != NULL)
3130 t->meth->cleanup(t);
3131 OPENSSL_free(t->data);
3134 OPENSSL_free(t->expected_err);
3135 t->expected_err = NULL;
3136 OPENSSL_free(t->reason);
3145 /* Check for errors in the test structure; return 1 if okay, else 0. */
3146 static int check_test_error(EVP_TEST *t)
3151 if (t->err == NULL && t->expected_err == NULL)
3153 if (t->err != NULL && t->expected_err == NULL) {
3154 if (t->aux_err != NULL) {
3155 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
3156 t->s.test_file, t->s.start, t->aux_err, t->err);
3158 TEST_info("%s:%d: Source of above error; unexpected error %s",
3159 t->s.test_file, t->s.start, t->err);
3163 if (t->err == NULL && t->expected_err != NULL) {
3164 TEST_info("%s:%d: Succeeded but was expecting %s",
3165 t->s.test_file, t->s.start, t->expected_err);
3169 if (strcmp(t->err, t->expected_err) != 0) {
3170 TEST_info("%s:%d: Expected %s got %s",
3171 t->s.test_file, t->s.start, t->expected_err, t->err);
3175 if (t->reason == NULL)
3178 if (t->reason == NULL) {
3179 TEST_info("%s:%d: Test is missing function or reason code",
3180 t->s.test_file, t->s.start);
3184 err = ERR_peek_error();
3186 TEST_info("%s:%d: Expected error \"%s\" not set",
3187 t->s.test_file, t->s.start, t->reason);
3191 reason = ERR_reason_error_string(err);
3192 if (reason == NULL) {
3193 TEST_info("%s:%d: Expected error \"%s\", no strings available."
3195 t->s.test_file, t->s.start, t->reason);
3199 if (strcmp(reason, t->reason) == 0)
3202 TEST_info("%s:%d: Expected error \"%s\", got \"%s\"",
3203 t->s.test_file, t->s.start, t->reason, reason);
3208 /* Run a parsed test. Log a message and return 0 on error. */
3209 static int run_test(EVP_TEST *t)
3211 if (t->meth == NULL)
3218 if (t->err == NULL && t->meth->run_test(t) != 1) {
3219 TEST_info("%s:%d %s error",
3220 t->s.test_file, t->s.start, t->meth->name);
3223 if (!check_test_error(t)) {
3224 TEST_openssl_errors();
3233 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
3235 for (; lst != NULL; lst = lst->next) {
3236 if (strcmp(lst->name, name) == 0) {
3245 static void free_key_list(KEY_LIST *lst)
3247 while (lst != NULL) {
3248 KEY_LIST *next = lst->next;
3250 EVP_PKEY_free(lst->key);
3251 OPENSSL_free(lst->name);
3258 * Is the key type an unsupported algorithm?
3260 static int key_unsupported(void)
3262 long err = ERR_peek_error();
3264 if (ERR_GET_LIB(err) == ERR_LIB_EVP
3265 && (ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM
3266 || ERR_GET_REASON(err) == EVP_R_FETCH_FAILED)) {
3270 #ifndef OPENSSL_NO_EC
3272 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
3273 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
3276 if (ERR_GET_LIB(err) == ERR_LIB_EC
3277 && ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP) {
3281 #endif /* OPENSSL_NO_EC */
3285 /* NULL out the value from |pp| but return it. This "steals" a pointer. */
3286 static char *take_value(PAIR *pp)
3288 char *p = pp->value;
3295 * Return 1 if one of the providers named in the string is available.
3296 * The provider names are separated with whitespace.
3297 * NOTE: destructive function, it inserts '\0' after each provider name.
3299 static int prov_available(char *providers)
3305 for (; isspace(*providers); providers++)
3307 if (*providers == '\0')
3308 break; /* End of the road */
3309 for (p = providers; *p != '\0' && !isspace(*p); p++)
3315 if (OSSL_PROVIDER_available(libctx, providers))
3316 return 1; /* Found one */
3321 /* Read and parse one test. Return 0 if failure, 1 if okay. */
3322 static int parse(EVP_TEST *t)
3324 KEY_LIST *key, **klist;
3327 int i, skip_availablein = 0;
3331 if (BIO_eof(t->s.fp))
3334 if (!test_readstanza(&t->s))
3336 } while (t->s.numpairs == 0);
3337 pp = &t->s.pairs[0];
3339 /* Are we adding a key? */
3343 if (strcmp(pp->key, "PrivateKey") == 0) {
3344 pkey = PEM_read_bio_PrivateKey_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3345 if (pkey == NULL && !key_unsupported()) {
3346 EVP_PKEY_free(pkey);
3347 TEST_info("Can't read private key %s", pp->value);
3348 TEST_openssl_errors();
3351 klist = &private_keys;
3352 } else if (strcmp(pp->key, "PublicKey") == 0) {
3353 pkey = PEM_read_bio_PUBKEY(t->s.key, NULL, 0, NULL);
3354 if (pkey == NULL && !key_unsupported()) {
3355 EVP_PKEY_free(pkey);
3356 TEST_info("Can't read public key %s", pp->value);
3357 TEST_openssl_errors();
3360 klist = &public_keys;
3361 } else if (strcmp(pp->key, "PrivateKeyRaw") == 0
3362 || strcmp(pp->key, "PublicKeyRaw") == 0 ) {
3363 char *strnid = NULL, *keydata = NULL;
3364 unsigned char *keybin;
3368 if (strcmp(pp->key, "PrivateKeyRaw") == 0)
3369 klist = &private_keys;
3371 klist = &public_keys;
3373 strnid = strchr(pp->value, ':');
3374 if (strnid != NULL) {
3376 keydata = strchr(strnid, ':');
3377 if (keydata != NULL)
3380 if (keydata == NULL) {
3381 TEST_info("Failed to parse %s value", pp->key);
3385 nid = OBJ_txt2nid(strnid);
3386 if (nid == NID_undef) {
3387 TEST_info("Unrecognised algorithm NID");
3390 if (!parse_bin(keydata, &keybin, &keylen)) {
3391 TEST_info("Failed to create binary key");
3394 if (klist == &private_keys)
3395 pkey = EVP_PKEY_new_raw_private_key_with_libctx(libctx, strnid, NULL,
3398 pkey = EVP_PKEY_new_raw_public_key_with_libctx(libctx, strnid, NULL,
3400 if (pkey == NULL && !key_unsupported()) {
3401 TEST_info("Can't read %s data", pp->key);
3402 OPENSSL_free(keybin);
3403 TEST_openssl_errors();
3406 OPENSSL_free(keybin);
3407 } else if (strcmp(pp->key, "Availablein") == 0) {
3408 if (!prov_available(pp->value)) {
3409 TEST_info("skipping, '%s' provider not available: %s:%d",
3410 pp->value, t->s.test_file, t->s.start);
3419 /* If we have a key add to list */
3420 if (klist != NULL) {
3421 if (find_key(NULL, pp->value, *klist)) {
3422 TEST_info("Duplicate key %s", pp->value);
3425 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
3427 key->name = take_value(pp);
3432 /* Go back and start a new stanza. */
3433 if ((t->s.numpairs - skip_availablein) != 1)
3434 TEST_info("Line %d: missing blank line\n", t->s.curr);
3438 /* Find the test, based on first keyword. */
3439 if (!TEST_ptr(t->meth = find_test(pp->key)))
3441 if (!t->meth->init(t, pp->value)) {
3442 TEST_error("unknown %s: %s\n", pp->key, pp->value);
3446 /* TEST_info("skipping %s %s", pp->key, pp->value); */
3450 for (pp++, i = 1; i < (t->s.numpairs - skip_availablein); pp++, i++) {
3451 if (strcmp(pp->key, "Availablein") == 0) {
3452 TEST_info("Line %d: 'Availablein' should be the first option",
3455 } else if (strcmp(pp->key, "Result") == 0) {
3456 if (t->expected_err != NULL) {
3457 TEST_info("Line %d: multiple result lines", t->s.curr);
3460 t->expected_err = take_value(pp);
3461 } else if (strcmp(pp->key, "Function") == 0) {
3462 /* Ignore old line. */
3463 } else if (strcmp(pp->key, "Reason") == 0) {
3464 if (t->reason != NULL) {
3465 TEST_info("Line %d: multiple reason lines", t->s.curr);
3468 t->reason = take_value(pp);
3470 /* Must be test specific line: try to parse it */
3471 int rv = t->meth->parse(t, pp->key, pp->value);
3474 TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key);
3478 TEST_info("Line %d: error processing keyword %s = %s\n",
3479 t->s.curr, pp->key, pp->value);
3488 static int run_file_tests(int i)
3491 const char *testfile = test_get_argument(i);
3494 if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t))))
3496 if (!test_start_file(&t->s, testfile)) {
3501 while (!BIO_eof(t->s.fp)) {
3507 if (c == 0 || !run_test(t)) {
3512 test_end_file(&t->s);
3515 free_key_list(public_keys);
3516 free_key_list(private_keys);
3523 const OPTIONS *test_get_options(void)
3525 static const OPTIONS test_options[] = {
3526 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
3527 { "config", OPT_CONFIG_FILE, '<',
3528 "The configuration file to use for the libctx" },
3529 { OPT_HELP_STR, 1, '-',
3530 "file\tFile to run tests on.\n" },
3533 return test_options;
3536 int setup_tests(void)
3539 char *config_file = NULL;
3543 while ((o = opt_next()) != OPT_EOF) {
3545 case OPT_CONFIG_FILE:
3546 config_file = opt_arg();
3548 case OPT_TEST_CASES:
3557 * Load the 'null' provider into the default library context to ensure that
3558 * the the tests do not fallback to using the default provider.
3560 prov_null = OSSL_PROVIDER_load(NULL, "null");
3561 if (prov_null == NULL) {
3562 opt_printf_stderr("Failed to load null provider into default libctx\n");
3566 /* load the provider via configuration into the created library context */
3567 libctx = OPENSSL_CTX_new();
3569 || !OPENSSL_CTX_load_config(libctx, config_file)) {
3570 TEST_error("Failed to load config %s\n", config_file);
3574 n = test_get_argument_count();
3578 ADD_ALL_TESTS(run_file_tests, n);
3582 void cleanup_tests(void)
3584 OSSL_PROVIDER_unload(prov_null);
3585 OPENSSL_CTX_free(libctx);
3588 #define STR_STARTS_WITH(str, pre) strncasecmp(pre, str, strlen(pre)) == 0
3589 #define STR_ENDS_WITH(str, pre) \
3590 strlen(str) < strlen(pre) ? 0 : (strcasecmp(pre, str + strlen(str) - strlen(pre)) == 0)
3592 static int is_digest_disabled(const char *name)
3594 #ifdef OPENSSL_NO_BLAKE2
3595 if (STR_STARTS_WITH(name, "BLAKE"))
3598 #ifdef OPENSSL_NO_MD2
3599 if (strcasecmp(name, "MD2") == 0)
3602 #ifdef OPENSSL_NO_MDC2
3603 if (strcasecmp(name, "MDC2") == 0)
3606 #ifdef OPENSSL_NO_MD4
3607 if (strcasecmp(name, "MD4") == 0)
3610 #ifdef OPENSSL_NO_MD5
3611 if (strcasecmp(name, "MD5") == 0)
3614 #ifdef OPENSSL_NO_RMD160
3615 if (strcasecmp(name, "RIPEMD160") == 0)
3618 #ifdef OPENSSL_NO_SM3
3619 if (strcasecmp(name, "SM3") == 0)
3622 #ifdef OPENSSL_NO_WHIRLPOOL
3623 if (strcasecmp(name, "WHIRLPOOL") == 0)
3629 static int is_pkey_disabled(const char *name)
3631 #ifdef OPENSSL_NO_RSA
3632 if (STR_STARTS_WITH(name, "RSA"))
3635 #ifdef OPENSSL_NO_EC
3636 if (STR_STARTS_WITH(name, "EC"))
3639 #ifdef OPENSSL_NO_DH
3640 if (STR_STARTS_WITH(name, "DH"))
3643 #ifdef OPENSSL_NO_DSA
3644 if (STR_STARTS_WITH(name, "DSA"))
3650 static int is_mac_disabled(const char *name)
3652 #ifdef OPENSSL_NO_BLAKE2
3653 if (STR_STARTS_WITH(name, "BLAKE2BMAC")
3654 || STR_STARTS_WITH(name, "BLAKE2SMAC"))
3657 #ifdef OPENSSL_NO_CMAC
3658 if (STR_STARTS_WITH(name, "CMAC"))
3661 #ifdef OPENSSL_NO_POLY1305
3662 if (STR_STARTS_WITH(name, "Poly1305"))
3665 #ifdef OPENSSL_NO_SIPHASH
3666 if (STR_STARTS_WITH(name, "SipHash"))
3671 static int is_kdf_disabled(const char *name)
3673 #ifdef OPENSSL_NO_SCRYPT
3674 if (STR_ENDS_WITH(name, "SCRYPT"))
3677 #ifdef OPENSSL_NO_CMS
3678 if (strcasecmp(name, "X942KDF") == 0)
3680 #endif /* OPENSSL_NO_CMS */
3684 static int is_cipher_disabled(const char *name)
3686 #ifdef OPENSSL_NO_ARIA
3687 if (STR_STARTS_WITH(name, "ARIA"))
3690 #ifdef OPENSSL_NO_BF
3691 if (STR_STARTS_WITH(name, "BF"))
3694 #ifdef OPENSSL_NO_CAMELLIA
3695 if (STR_STARTS_WITH(name, "CAMELLIA"))
3698 #ifdef OPENSSL_NO_CAST
3699 if (STR_STARTS_WITH(name, "CAST"))
3702 #ifdef OPENSSL_NO_CHACHA
3703 if (STR_STARTS_WITH(name, "CHACHA"))
3706 #ifdef OPENSSL_NO_POLY1305
3707 if (STR_ENDS_WITH(name, "Poly1305"))
3710 #ifdef OPENSSL_NO_DES
3711 if (STR_STARTS_WITH(name, "DES"))
3714 #ifdef OPENSSL_NO_OCB
3715 if (STR_ENDS_WITH(name, "OCB"))
3718 #ifdef OPENSSL_NO_IDEA
3719 if (STR_STARTS_WITH(name, "IDEA"))
3722 #ifdef OPENSSL_NO_RC2
3723 if (STR_STARTS_WITH(name, "RC2"))
3726 #ifdef OPENSSL_NO_RC4
3727 if (STR_STARTS_WITH(name, "RC4"))
3730 #ifdef OPENSSL_NO_RC5
3731 if (STR_STARTS_WITH(name, "RC5"))
3734 #ifdef OPENSSL_NO_SEED
3735 if (STR_STARTS_WITH(name, "SEED"))
3738 #ifdef OPENSSL_NO_SIV
3739 if (STR_ENDS_WITH(name, "SIV"))
3742 #ifdef OPENSSL_NO_SM4
3743 if (STR_STARTS_WITH(name, "SM4"))