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 /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
765 unsigned char iv[128];
766 if (!TEST_true(EVP_CIPHER_CTX_get_iv_state(ctx_base, iv, sizeof(iv)))
767 || ((EVP_CIPHER_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
768 && !TEST_mem_eq(expected->iv, expected->iv_len, iv,
769 expected->iv_len))) {
770 t->err = "INVALID_IV";
775 /* Test that the cipher dup functions correctly if it is supported */
776 if (EVP_CIPHER_CTX_copy(ctx, ctx_base)) {
777 EVP_CIPHER_CTX_free(ctx_base);
780 EVP_CIPHER_CTX_free(ctx);
784 if (expected->aead == EVP_CIPH_CCM_MODE) {
785 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
786 t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
790 if (expected->aad[0] != NULL) {
791 t->err = "AAD_SET_ERROR";
793 for (i = 0; expected->aad[i] != NULL; i++) {
794 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i],
795 expected->aad_len[i]))
800 * Supply the AAD in chunks less than the block size where possible
802 for (i = 0; expected->aad[i] != NULL; i++) {
803 if (expected->aad_len[i] > 0) {
804 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], 1))
808 if (expected->aad_len[i] > 2) {
809 if (!EVP_CipherUpdate(ctx, NULL, &chunklen,
810 expected->aad[i] + donelen,
811 expected->aad_len[i] - 2))
813 donelen += expected->aad_len[i] - 2;
815 if (expected->aad_len[i] > 1
816 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
817 expected->aad[i] + donelen, 1))
823 if (!enc && (expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late)) {
824 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
825 expected->tag_len, expected->tag)) {
826 t->err = "TAG_SET_ERROR";
831 EVP_CIPHER_CTX_set_padding(ctx, 0);
832 t->err = "CIPHERUPDATE_ERROR";
835 /* We supply the data all in one go */
836 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
839 /* Supply the data in chunks less than the block size where possible */
841 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
848 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
856 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
862 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
863 t->err = "CIPHERFINAL_ERROR";
866 if (!memory_err_compare(t, "VALUE_MISMATCH", expected_out, out_len,
867 tmp + out_misalign, tmplen + tmpflen))
869 if (enc && expected->aead) {
870 unsigned char rtag[16];
872 if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) {
873 t->err = "TAG_LENGTH_INTERNAL_ERROR";
876 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
877 expected->tag_len, rtag)) {
878 t->err = "TAG_RETRIEVE_ERROR";
881 if (!memory_err_compare(t, "TAG_VALUE_MISMATCH",
882 expected->tag, expected->tag_len,
883 rtag, expected->tag_len))
891 EVP_CIPHER_CTX_free(ctx_base);
892 EVP_CIPHER_CTX_free(ctx);
896 static int cipher_test_run(EVP_TEST *t)
898 CIPHER_DATA *cdat = t->data;
900 size_t out_misalign, inp_misalign;
906 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
907 /* IV is optional and usually omitted in wrap mode */
908 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
913 if (cdat->aead && !cdat->tag) {
917 for (out_misalign = 0; out_misalign <= 1;) {
918 static char aux_err[64];
919 t->aux_err = aux_err;
920 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
921 if (inp_misalign == (size_t)-1) {
922 /* kludge: inp_misalign == -1 means "exercise in-place" */
923 BIO_snprintf(aux_err, sizeof(aux_err),
924 "%s in-place, %sfragmented",
925 out_misalign ? "misaligned" : "aligned",
928 BIO_snprintf(aux_err, sizeof(aux_err),
929 "%s output and %s input, %sfragmented",
930 out_misalign ? "misaligned" : "aligned",
931 inp_misalign ? "misaligned" : "aligned",
935 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
936 /* Not fatal errors: return */
943 if (cdat->enc != 1) {
944 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
945 /* Not fatal errors: return */
954 if (out_misalign == 1 && frag == 0) {
956 * XTS, SIV, CCM and Wrap modes have special requirements about input
957 * lengths so we don't fragment for those
959 if (cdat->aead == EVP_CIPH_CCM_MODE
960 || ((EVP_CIPHER_flags(cdat->cipher) & EVP_CIPH_FLAG_CTS) != 0)
961 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_SIV_MODE
962 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
963 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
976 static const EVP_TEST_METHOD cipher_test_method = {
989 typedef struct mac_data_st {
990 /* MAC type in one form or another */
992 EVP_MAC *mac; /* for mac_test_run_mac */
993 int type; /* for mac_test_run_pkey */
994 /* Algorithm string for this MAC */
1003 unsigned char *input;
1005 /* Expected output */
1006 unsigned char *output;
1008 unsigned char *custom;
1010 /* MAC salt (blake2) */
1011 unsigned char *salt;
1013 /* Collection of controls */
1014 STACK_OF(OPENSSL_STRING) *controls;
1017 static int mac_test_init(EVP_TEST *t, const char *alg)
1019 EVP_MAC *mac = NULL;
1020 int type = NID_undef;
1023 if (is_mac_disabled(alg)) {
1024 TEST_info("skipping, '%s' is disabled", alg);
1028 if ((mac = EVP_MAC_fetch(libctx, alg, NULL)) == NULL) {
1030 * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
1031 * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
1032 * the EVP_PKEY method.
1034 size_t sz = strlen(alg);
1035 static const char epilogue[] = " by EVP_PKEY";
1037 if (sz >= sizeof(epilogue)
1038 && strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0)
1039 sz -= sizeof(epilogue) - 1;
1041 if (strncmp(alg, "HMAC", sz) == 0)
1042 type = EVP_PKEY_HMAC;
1043 else if (strncmp(alg, "CMAC", sz) == 0)
1044 type = EVP_PKEY_CMAC;
1045 else if (strncmp(alg, "Poly1305", sz) == 0)
1046 type = EVP_PKEY_POLY1305;
1047 else if (strncmp(alg, "SipHash", sz) == 0)
1048 type = EVP_PKEY_SIPHASH;
1053 mdat = OPENSSL_zalloc(sizeof(*mdat));
1055 mdat->mac_name = OPENSSL_strdup(alg);
1057 mdat->controls = sk_OPENSSL_STRING_new_null();
1062 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
1063 static void openssl_free(char *m)
1068 static void mac_test_cleanup(EVP_TEST *t)
1070 MAC_DATA *mdat = t->data;
1072 EVP_MAC_free(mdat->mac);
1073 OPENSSL_free(mdat->mac_name);
1074 sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free);
1075 OPENSSL_free(mdat->alg);
1076 OPENSSL_free(mdat->key);
1077 OPENSSL_free(mdat->iv);
1078 OPENSSL_free(mdat->custom);
1079 OPENSSL_free(mdat->salt);
1080 OPENSSL_free(mdat->input);
1081 OPENSSL_free(mdat->output);
1084 static int mac_test_parse(EVP_TEST *t,
1085 const char *keyword, const char *value)
1087 MAC_DATA *mdata = t->data;
1089 if (strcmp(keyword, "Key") == 0)
1090 return parse_bin(value, &mdata->key, &mdata->key_len);
1091 if (strcmp(keyword, "IV") == 0)
1092 return parse_bin(value, &mdata->iv, &mdata->iv_len);
1093 if (strcmp(keyword, "Custom") == 0)
1094 return parse_bin(value, &mdata->custom, &mdata->custom_len);
1095 if (strcmp(keyword, "Salt") == 0)
1096 return parse_bin(value, &mdata->salt, &mdata->salt_len);
1097 if (strcmp(keyword, "Algorithm") == 0) {
1098 mdata->alg = OPENSSL_strdup(value);
1103 if (strcmp(keyword, "Input") == 0)
1104 return parse_bin(value, &mdata->input, &mdata->input_len);
1105 if (strcmp(keyword, "Output") == 0)
1106 return parse_bin(value, &mdata->output, &mdata->output_len);
1107 if (strcmp(keyword, "Ctrl") == 0)
1108 return sk_OPENSSL_STRING_push(mdata->controls,
1109 OPENSSL_strdup(value)) != 0;
1113 static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1119 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1121 p = strchr(tmpval, ':');
1124 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1126 t->err = "PKEY_CTRL_INVALID";
1128 t->err = "PKEY_CTRL_ERROR";
1131 OPENSSL_free(tmpval);
1135 static int mac_test_run_pkey(EVP_TEST *t)
1137 MAC_DATA *expected = t->data;
1138 EVP_MD_CTX *mctx = NULL;
1139 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1140 EVP_PKEY *key = NULL;
1141 const char *mdname = NULL;
1142 EVP_CIPHER *cipher = NULL;
1143 unsigned char *got = NULL;
1147 if (expected->alg == NULL)
1148 TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type));
1150 TEST_info("Trying the EVP_PKEY %s test with %s",
1151 OBJ_nid2sn(expected->type), expected->alg);
1153 if (expected->type == EVP_PKEY_CMAC) {
1154 if (expected->alg != NULL && is_cipher_disabled(expected->alg)) {
1155 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
1160 if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, expected->alg, NULL))) {
1161 t->err = "MAC_KEY_CREATE_ERROR";
1164 key = EVP_PKEY_new_CMAC_key_with_libctx(expected->key,
1166 EVP_CIPHER_name(cipher),
1169 key = EVP_PKEY_new_raw_private_key_with_libctx(libctx,
1170 OBJ_nid2sn(expected->type),
1171 NULL, expected->key,
1175 t->err = "MAC_KEY_CREATE_ERROR";
1179 if (expected->type == EVP_PKEY_HMAC && expected->alg != NULL) {
1180 if (is_digest_disabled(expected->alg)) {
1181 TEST_info("skipping, HMAC '%s' is disabled", expected->alg);
1186 mdname = expected->alg;
1188 if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
1189 t->err = "INTERNAL_ERROR";
1192 if (!EVP_DigestSignInit_with_libctx(mctx, &pctx, mdname, libctx, NULL, key)) {
1193 t->err = "DIGESTSIGNINIT_ERROR";
1196 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++)
1197 if (!mac_test_ctrl_pkey(t, pctx,
1198 sk_OPENSSL_STRING_value(expected->controls,
1200 t->err = "EVPPKEYCTXCTRL_ERROR";
1203 if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) {
1204 t->err = "DIGESTSIGNUPDATE_ERROR";
1207 if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) {
1208 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1211 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1212 t->err = "TEST_FAILURE";
1215 if (!EVP_DigestSignFinal(mctx, got, &got_len)
1216 || !memory_err_compare(t, "TEST_MAC_ERR",
1217 expected->output, expected->output_len,
1219 t->err = "TEST_MAC_ERR";
1224 EVP_CIPHER_free(cipher);
1225 EVP_MD_CTX_free(mctx);
1227 EVP_PKEY_CTX_free(genctx);
1232 static int mac_test_run_mac(EVP_TEST *t)
1234 MAC_DATA *expected = t->data;
1235 EVP_MAC_CTX *ctx = NULL;
1236 unsigned char *got = NULL;
1239 OSSL_PARAM params[21];
1240 size_t params_n = 0;
1241 size_t params_n_allocstart = 0;
1242 const OSSL_PARAM *defined_params =
1243 EVP_MAC_settable_ctx_params(expected->mac);
1245 if (expected->alg == NULL)
1246 TEST_info("Trying the EVP_MAC %s test", expected->mac_name);
1248 TEST_info("Trying the EVP_MAC %s test with %s",
1249 expected->mac_name, expected->alg);
1251 if (expected->alg != NULL) {
1253 * The underlying algorithm may be a cipher or a digest.
1254 * We don't know which it is, but we can ask the MAC what it
1255 * should be and bet on that.
1257 if (OSSL_PARAM_locate_const(defined_params,
1258 OSSL_MAC_PARAM_CIPHER) != NULL) {
1259 params[params_n++] =
1260 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
1262 } else if (OSSL_PARAM_locate_const(defined_params,
1263 OSSL_MAC_PARAM_DIGEST) != NULL) {
1264 params[params_n++] =
1265 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
1268 t->err = "MAC_BAD_PARAMS";
1272 if (expected->key != NULL)
1273 params[params_n++] =
1274 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
1277 if (expected->custom != NULL)
1278 params[params_n++] =
1279 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
1281 expected->custom_len);
1282 if (expected->salt != NULL)
1283 params[params_n++] =
1284 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT,
1286 expected->salt_len);
1287 if (expected->iv != NULL)
1288 params[params_n++] =
1289 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
1293 /* Unknown controls. They must match parameters that the MAC recognizes */
1294 if (params_n + sk_OPENSSL_STRING_num(expected->controls)
1295 >= OSSL_NELEM(params)) {
1296 t->err = "MAC_TOO_MANY_PARAMETERS";
1299 params_n_allocstart = params_n;
1300 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) {
1301 char *tmpkey, *tmpval;
1302 char *value = sk_OPENSSL_STRING_value(expected->controls, i);
1304 if (!TEST_ptr(tmpkey = OPENSSL_strdup(value))) {
1305 t->err = "MAC_PARAM_ERROR";
1308 tmpval = strchr(tmpkey, ':');
1313 || !OSSL_PARAM_allocate_from_text(¶ms[params_n],
1316 strlen(tmpval), NULL)) {
1317 OPENSSL_free(tmpkey);
1318 t->err = "MAC_PARAM_ERROR";
1323 OPENSSL_free(tmpkey);
1325 params[params_n] = OSSL_PARAM_construct_end();
1327 if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) {
1328 t->err = "MAC_CREATE_ERROR";
1332 if (!EVP_MAC_CTX_set_params(ctx, params)) {
1333 t->err = "MAC_BAD_PARAMS";
1336 if (!EVP_MAC_init(ctx)) {
1337 t->err = "MAC_INIT_ERROR";
1340 if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) {
1341 t->err = "MAC_UPDATE_ERROR";
1344 if (!EVP_MAC_final(ctx, NULL, &got_len, 0)) {
1345 t->err = "MAC_FINAL_LENGTH_ERROR";
1348 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1349 t->err = "TEST_FAILURE";
1352 if (!EVP_MAC_final(ctx, got, &got_len, got_len)
1353 || !memory_err_compare(t, "TEST_MAC_ERR",
1354 expected->output, expected->output_len,
1356 t->err = "TEST_MAC_ERR";
1361 while (params_n-- > params_n_allocstart) {
1362 OPENSSL_free(params[params_n].data);
1364 EVP_MAC_CTX_free(ctx);
1369 static int mac_test_run(EVP_TEST *t)
1371 MAC_DATA *expected = t->data;
1373 if (expected->mac != NULL)
1374 return mac_test_run_mac(t);
1375 return mac_test_run_pkey(t);
1378 static const EVP_TEST_METHOD mac_test_method = {
1389 ** These are all very similar and share much common code.
1392 typedef struct pkey_data_st {
1393 /* Context for this operation */
1395 /* Key operation to perform */
1396 int (*keyop) (EVP_PKEY_CTX *ctx,
1397 unsigned char *sig, size_t *siglen,
1398 const unsigned char *tbs, size_t tbslen);
1400 unsigned char *input;
1402 /* Expected output */
1403 unsigned char *output;
1408 * Perform public key operation setup: lookup key, allocated ctx and call
1409 * the appropriate initialisation function
1411 static int pkey_test_init(EVP_TEST *t, const char *name,
1413 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1414 int (*keyop)(EVP_PKEY_CTX *ctx,
1415 unsigned char *sig, size_t *siglen,
1416 const unsigned char *tbs,
1420 EVP_PKEY *pkey = NULL;
1424 rv = find_key(&pkey, name, public_keys);
1426 rv = find_key(&pkey, name, private_keys);
1427 if (rv == 0 || pkey == NULL) {
1428 TEST_info("skipping, key '%s' is disabled", name);
1433 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) {
1434 EVP_PKEY_free(pkey);
1437 kdata->keyop = keyop;
1438 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL))) {
1439 EVP_PKEY_free(pkey);
1440 OPENSSL_free(kdata);
1443 if (keyopinit(kdata->ctx) <= 0)
1444 t->err = "KEYOP_INIT_ERROR";
1449 static void pkey_test_cleanup(EVP_TEST *t)
1451 PKEY_DATA *kdata = t->data;
1453 OPENSSL_free(kdata->input);
1454 OPENSSL_free(kdata->output);
1455 EVP_PKEY_CTX_free(kdata->ctx);
1458 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1464 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1466 p = strchr(tmpval, ':');
1469 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1471 t->err = "PKEY_CTRL_INVALID";
1473 } else if (p != NULL && rv <= 0) {
1474 if (is_digest_disabled(p) || is_cipher_disabled(p)) {
1475 TEST_info("skipping, '%s' is disabled", p);
1479 t->err = "PKEY_CTRL_ERROR";
1483 OPENSSL_free(tmpval);
1487 static int pkey_test_parse(EVP_TEST *t,
1488 const char *keyword, const char *value)
1490 PKEY_DATA *kdata = t->data;
1491 if (strcmp(keyword, "Input") == 0)
1492 return parse_bin(value, &kdata->input, &kdata->input_len);
1493 if (strcmp(keyword, "Output") == 0)
1494 return parse_bin(value, &kdata->output, &kdata->output_len);
1495 if (strcmp(keyword, "Ctrl") == 0)
1496 return pkey_test_ctrl(t, kdata->ctx, value);
1500 static int pkey_test_run(EVP_TEST *t)
1502 PKEY_DATA *expected = t->data;
1503 unsigned char *got = NULL;
1505 EVP_PKEY_CTX *copy = NULL;
1507 if (expected->keyop(expected->ctx, NULL, &got_len,
1508 expected->input, expected->input_len) <= 0
1509 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1510 t->err = "KEYOP_LENGTH_ERROR";
1513 if (expected->keyop(expected->ctx, got, &got_len,
1514 expected->input, expected->input_len) <= 0) {
1515 t->err = "KEYOP_ERROR";
1518 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1519 expected->output, expected->output_len,
1527 /* Repeat the test on a copy. */
1528 if (!TEST_ptr(copy = EVP_PKEY_CTX_dup(expected->ctx))) {
1529 t->err = "INTERNAL_ERROR";
1532 if (expected->keyop(copy, NULL, &got_len, expected->input,
1533 expected->input_len) <= 0
1534 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1535 t->err = "KEYOP_LENGTH_ERROR";
1538 if (expected->keyop(copy, got, &got_len, expected->input,
1539 expected->input_len) <= 0) {
1540 t->err = "KEYOP_ERROR";
1543 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1544 expected->output, expected->output_len,
1550 EVP_PKEY_CTX_free(copy);
1554 static int sign_test_init(EVP_TEST *t, const char *name)
1556 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1559 static const EVP_TEST_METHOD psign_test_method = {
1567 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1569 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1570 EVP_PKEY_verify_recover);
1573 static const EVP_TEST_METHOD pverify_recover_test_method = {
1575 verify_recover_test_init,
1581 static int decrypt_test_init(EVP_TEST *t, const char *name)
1583 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1587 static const EVP_TEST_METHOD pdecrypt_test_method = {
1595 static int verify_test_init(EVP_TEST *t, const char *name)
1597 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1600 static int verify_test_run(EVP_TEST *t)
1602 PKEY_DATA *kdata = t->data;
1604 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1605 kdata->input, kdata->input_len) <= 0)
1606 t->err = "VERIFY_ERROR";
1610 static const EVP_TEST_METHOD pverify_test_method = {
1618 static int pderive_test_init(EVP_TEST *t, const char *name)
1620 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1623 static int pderive_test_parse(EVP_TEST *t,
1624 const char *keyword, const char *value)
1626 PKEY_DATA *kdata = t->data;
1628 if (strcmp(keyword, "PeerKey") == 0) {
1630 if (find_key(&peer, value, public_keys) == 0)
1632 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1636 if (strcmp(keyword, "SharedSecret") == 0)
1637 return parse_bin(value, &kdata->output, &kdata->output_len);
1638 if (strcmp(keyword, "Ctrl") == 0)
1639 return pkey_test_ctrl(t, kdata->ctx, value);
1643 static int pderive_test_run(EVP_TEST *t)
1645 PKEY_DATA *expected = t->data;
1646 unsigned char *got = NULL;
1649 if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) {
1650 t->err = "DERIVE_ERROR";
1653 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1654 t->err = "DERIVE_ERROR";
1657 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
1658 t->err = "DERIVE_ERROR";
1661 if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH",
1662 expected->output, expected->output_len,
1672 static const EVP_TEST_METHOD pderive_test_method = {
1685 typedef enum pbe_type_enum {
1686 PBE_TYPE_INVALID = 0,
1687 PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
1690 typedef struct pbe_data_st {
1692 /* scrypt parameters */
1693 uint64_t N, r, p, maxmem;
1694 /* PKCS#12 parameters */
1698 unsigned char *pass;
1701 unsigned char *salt;
1703 /* Expected output */
1708 #ifndef OPENSSL_NO_SCRYPT
1709 /* Parse unsigned decimal 64 bit integer value */
1710 static int parse_uint64(const char *value, uint64_t *pr)
1712 const char *p = value;
1714 if (!TEST_true(*p)) {
1715 TEST_info("Invalid empty integer value");
1718 for (*pr = 0; *p; ) {
1719 if (*pr > UINT64_MAX / 10) {
1720 TEST_error("Integer overflow in string %s", value);
1724 if (!TEST_true(isdigit((unsigned char)*p))) {
1725 TEST_error("Invalid character in string %s", value);
1734 static int scrypt_test_parse(EVP_TEST *t,
1735 const char *keyword, const char *value)
1737 PBE_DATA *pdata = t->data;
1739 if (strcmp(keyword, "N") == 0)
1740 return parse_uint64(value, &pdata->N);
1741 if (strcmp(keyword, "p") == 0)
1742 return parse_uint64(value, &pdata->p);
1743 if (strcmp(keyword, "r") == 0)
1744 return parse_uint64(value, &pdata->r);
1745 if (strcmp(keyword, "maxmem") == 0)
1746 return parse_uint64(value, &pdata->maxmem);
1751 static int pbkdf2_test_parse(EVP_TEST *t,
1752 const char *keyword, const char *value)
1754 PBE_DATA *pdata = t->data;
1756 if (strcmp(keyword, "iter") == 0) {
1757 pdata->iter = atoi(value);
1758 if (pdata->iter <= 0)
1762 if (strcmp(keyword, "MD") == 0) {
1763 pdata->md = EVP_get_digestbyname(value);
1764 if (pdata->md == NULL)
1771 static int pkcs12_test_parse(EVP_TEST *t,
1772 const char *keyword, const char *value)
1774 PBE_DATA *pdata = t->data;
1776 if (strcmp(keyword, "id") == 0) {
1777 pdata->id = atoi(value);
1782 return pbkdf2_test_parse(t, keyword, value);
1785 static int pbe_test_init(EVP_TEST *t, const char *alg)
1788 PBE_TYPE pbe_type = PBE_TYPE_INVALID;
1790 if (is_kdf_disabled(alg)) {
1791 TEST_info("skipping, '%s' is disabled", alg);
1795 if (strcmp(alg, "scrypt") == 0) {
1796 pbe_type = PBE_TYPE_SCRYPT;
1797 } else if (strcmp(alg, "pbkdf2") == 0) {
1798 pbe_type = PBE_TYPE_PBKDF2;
1799 } else if (strcmp(alg, "pkcs12") == 0) {
1800 pbe_type = PBE_TYPE_PKCS12;
1802 TEST_error("Unknown pbe algorithm %s", alg);
1804 pdat = OPENSSL_zalloc(sizeof(*pdat));
1805 pdat->pbe_type = pbe_type;
1810 static void pbe_test_cleanup(EVP_TEST *t)
1812 PBE_DATA *pdat = t->data;
1814 OPENSSL_free(pdat->pass);
1815 OPENSSL_free(pdat->salt);
1816 OPENSSL_free(pdat->key);
1819 static int pbe_test_parse(EVP_TEST *t,
1820 const char *keyword, const char *value)
1822 PBE_DATA *pdata = t->data;
1824 if (strcmp(keyword, "Password") == 0)
1825 return parse_bin(value, &pdata->pass, &pdata->pass_len);
1826 if (strcmp(keyword, "Salt") == 0)
1827 return parse_bin(value, &pdata->salt, &pdata->salt_len);
1828 if (strcmp(keyword, "Key") == 0)
1829 return parse_bin(value, &pdata->key, &pdata->key_len);
1830 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1831 return pbkdf2_test_parse(t, keyword, value);
1832 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1833 return pkcs12_test_parse(t, keyword, value);
1834 #ifndef OPENSSL_NO_SCRYPT
1835 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1836 return scrypt_test_parse(t, keyword, value);
1841 static int pbe_test_run(EVP_TEST *t)
1843 PBE_DATA *expected = t->data;
1845 EVP_MD *fetched_digest = NULL;
1846 OPENSSL_CTX *save_libctx;
1848 save_libctx = OPENSSL_CTX_set0_default(libctx);
1850 if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
1851 t->err = "INTERNAL_ERROR";
1854 if (expected->pbe_type == PBE_TYPE_PBKDF2) {
1855 if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
1856 expected->salt, expected->salt_len,
1857 expected->iter, expected->md,
1858 expected->key_len, key) == 0) {
1859 t->err = "PBKDF2_ERROR";
1862 #ifndef OPENSSL_NO_SCRYPT
1863 } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
1864 if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
1865 expected->salt, expected->salt_len,
1866 expected->N, expected->r, expected->p,
1867 expected->maxmem, key, expected->key_len) == 0) {
1868 t->err = "SCRYPT_ERROR";
1872 } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
1873 fetched_digest = EVP_MD_fetch(libctx, EVP_MD_name(expected->md), NULL);
1874 if (fetched_digest == NULL) {
1875 t->err = "PKCS12_ERROR";
1878 if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
1879 expected->salt, expected->salt_len,
1880 expected->id, expected->iter, expected->key_len,
1881 key, fetched_digest) == 0) {
1882 t->err = "PKCS12_ERROR";
1886 if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len,
1887 key, expected->key_len))
1892 EVP_MD_free(fetched_digest);
1894 OPENSSL_CTX_set0_default(save_libctx);
1898 static const EVP_TEST_METHOD pbe_test_method = {
1912 BASE64_CANONICAL_ENCODING = 0,
1913 BASE64_VALID_ENCODING = 1,
1914 BASE64_INVALID_ENCODING = 2
1915 } base64_encoding_type;
1917 typedef struct encode_data_st {
1918 /* Input to encoding */
1919 unsigned char *input;
1921 /* Expected output */
1922 unsigned char *output;
1924 base64_encoding_type encoding;
1927 static int encode_test_init(EVP_TEST *t, const char *encoding)
1931 if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
1933 if (strcmp(encoding, "canonical") == 0) {
1934 edata->encoding = BASE64_CANONICAL_ENCODING;
1935 } else if (strcmp(encoding, "valid") == 0) {
1936 edata->encoding = BASE64_VALID_ENCODING;
1937 } else if (strcmp(encoding, "invalid") == 0) {
1938 edata->encoding = BASE64_INVALID_ENCODING;
1939 if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
1942 TEST_error("Bad encoding: %s."
1943 " Should be one of {canonical, valid, invalid}",
1950 OPENSSL_free(edata);
1954 static void encode_test_cleanup(EVP_TEST *t)
1956 ENCODE_DATA *edata = t->data;
1958 OPENSSL_free(edata->input);
1959 OPENSSL_free(edata->output);
1960 memset(edata, 0, sizeof(*edata));
1963 static int encode_test_parse(EVP_TEST *t,
1964 const char *keyword, const char *value)
1966 ENCODE_DATA *edata = t->data;
1968 if (strcmp(keyword, "Input") == 0)
1969 return parse_bin(value, &edata->input, &edata->input_len);
1970 if (strcmp(keyword, "Output") == 0)
1971 return parse_bin(value, &edata->output, &edata->output_len);
1975 static int encode_test_run(EVP_TEST *t)
1977 ENCODE_DATA *expected = t->data;
1978 unsigned char *encode_out = NULL, *decode_out = NULL;
1979 int output_len, chunk_len;
1980 EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL;
1982 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
1983 t->err = "INTERNAL_ERROR";
1987 if (expected->encoding == BASE64_CANONICAL_ENCODING) {
1989 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
1990 || !TEST_ptr(encode_out =
1991 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
1994 EVP_EncodeInit(encode_ctx);
1995 if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1996 expected->input, expected->input_len)))
1999 output_len = chunk_len;
2001 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
2002 output_len += chunk_len;
2004 if (!memory_err_compare(t, "BAD_ENCODING",
2005 expected->output, expected->output_len,
2006 encode_out, output_len))
2010 if (!TEST_ptr(decode_out =
2011 OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
2014 EVP_DecodeInit(decode_ctx);
2015 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
2016 expected->output_len) < 0) {
2017 t->err = "DECODE_ERROR";
2020 output_len = chunk_len;
2022 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
2023 t->err = "DECODE_ERROR";
2026 output_len += chunk_len;
2028 if (expected->encoding != BASE64_INVALID_ENCODING
2029 && !memory_err_compare(t, "BAD_DECODING",
2030 expected->input, expected->input_len,
2031 decode_out, output_len)) {
2032 t->err = "BAD_DECODING";
2038 OPENSSL_free(encode_out);
2039 OPENSSL_free(decode_out);
2040 EVP_ENCODE_CTX_free(decode_ctx);
2041 EVP_ENCODE_CTX_free(encode_ctx);
2045 static const EVP_TEST_METHOD encode_test_method = {
2048 encode_test_cleanup,
2057 #define MAX_RAND_REPEATS 15
2059 typedef struct rand_data_pass_st {
2060 unsigned char *entropy;
2061 unsigned char *reseed_entropy;
2062 unsigned char *nonce;
2063 unsigned char *pers;
2064 unsigned char *reseed_addin;
2065 unsigned char *addinA;
2066 unsigned char *addinB;
2067 unsigned char *pr_entropyA;
2068 unsigned char *pr_entropyB;
2069 unsigned char *output;
2070 size_t entropy_len, nonce_len, pers_len, addinA_len, addinB_len,
2071 pr_entropyA_len, pr_entropyB_len, output_len, reseed_entropy_len,
2075 typedef struct rand_data_st {
2076 /* Context for this operation */
2078 EVP_RAND_CTX *parent;
2080 int prediction_resistance;
2082 unsigned int generate_bits;
2086 /* Expected output */
2087 RAND_DATA_PASS data[MAX_RAND_REPEATS];
2090 static int rand_test_init(EVP_TEST *t, const char *name)
2094 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
2095 unsigned int strength = 256;
2097 if (!TEST_ptr(rdata = OPENSSL_zalloc(sizeof(*rdata))))
2100 /* TEST-RAND is available in the FIPS provider but not with "fips=yes" */
2101 rand = EVP_RAND_fetch(libctx, "TEST-RAND", "-fips");
2104 rdata->parent = EVP_RAND_CTX_new(rand, NULL);
2105 EVP_RAND_free(rand);
2106 if (rdata->parent == NULL)
2109 *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength);
2110 if (!EVP_RAND_set_ctx_params(rdata->parent, params))
2113 rand = EVP_RAND_fetch(libctx, name, NULL);
2116 rdata->ctx = EVP_RAND_CTX_new(rand, rdata->parent);
2117 EVP_RAND_free(rand);
2118 if (rdata->ctx == NULL)
2125 EVP_RAND_CTX_free(rdata->parent);
2126 OPENSSL_free(rdata);
2130 static void rand_test_cleanup(EVP_TEST *t)
2132 RAND_DATA *rdata = t->data;
2135 OPENSSL_free(rdata->cipher);
2136 OPENSSL_free(rdata->digest);
2138 for (i = 0; i <= rdata->n; i++) {
2139 OPENSSL_free(rdata->data[i].entropy);
2140 OPENSSL_free(rdata->data[i].reseed_entropy);
2141 OPENSSL_free(rdata->data[i].nonce);
2142 OPENSSL_free(rdata->data[i].pers);
2143 OPENSSL_free(rdata->data[i].reseed_addin);
2144 OPENSSL_free(rdata->data[i].addinA);
2145 OPENSSL_free(rdata->data[i].addinB);
2146 OPENSSL_free(rdata->data[i].pr_entropyA);
2147 OPENSSL_free(rdata->data[i].pr_entropyB);
2148 OPENSSL_free(rdata->data[i].output);
2150 EVP_RAND_CTX_free(rdata->ctx);
2151 EVP_RAND_CTX_free(rdata->parent);
2154 static int rand_test_parse(EVP_TEST *t,
2155 const char *keyword, const char *value)
2157 RAND_DATA *rdata = t->data;
2158 RAND_DATA_PASS *item;
2162 if ((p = strchr(keyword, '.')) != NULL) {
2164 if (n >= MAX_RAND_REPEATS)
2168 item = rdata->data + n;
2169 if (strncmp(keyword, "Entropy.", sizeof("Entropy")) == 0)
2170 return parse_bin(value, &item->entropy, &item->entropy_len);
2171 if (strncmp(keyword, "ReseedEntropy.", sizeof("ReseedEntropy")) == 0)
2172 return parse_bin(value, &item->reseed_entropy,
2173 &item->reseed_entropy_len);
2174 if (strncmp(keyword, "Nonce.", sizeof("Nonce")) == 0)
2175 return parse_bin(value, &item->nonce, &item->nonce_len);
2176 if (strncmp(keyword, "PersonalisationString.",
2177 sizeof("PersonalisationString")) == 0)
2178 return parse_bin(value, &item->pers, &item->pers_len);
2179 if (strncmp(keyword, "ReseedAdditionalInput.",
2180 sizeof("ReseedAdditionalInput")) == 0)
2181 return parse_bin(value, &item->reseed_addin,
2182 &item->reseed_addin_len);
2183 if (strncmp(keyword, "AdditionalInputA.",
2184 sizeof("AdditionalInputA")) == 0)
2185 return parse_bin(value, &item->addinA, &item->addinA_len);
2186 if (strncmp(keyword, "AdditionalInputB.",
2187 sizeof("AdditionalInputB")) == 0)
2188 return parse_bin(value, &item->addinB, &item->addinB_len);
2189 if (strncmp(keyword, "EntropyPredictionResistanceA.",
2190 sizeof("EntropyPredictionResistanceA")) == 0)
2191 return parse_bin(value, &item->pr_entropyA, &item->pr_entropyA_len);
2192 if (strncmp(keyword, "EntropyPredictionResistanceB.",
2193 sizeof("EntropyPredictionResistanceB")) == 0)
2194 return parse_bin(value, &item->pr_entropyB, &item->pr_entropyB_len);
2195 if (strncmp(keyword, "Output.", sizeof("Output")) == 0)
2196 return parse_bin(value, &item->output, &item->output_len);
2198 if (strcmp(keyword, "Cipher") == 0)
2199 return TEST_ptr(rdata->cipher = OPENSSL_strdup(value));
2200 if (strcmp(keyword, "Digest") == 0)
2201 return TEST_ptr(rdata->digest = OPENSSL_strdup(value));
2202 if (strcmp(keyword, "DerivationFunction") == 0) {
2203 rdata->use_df = atoi(value) != 0;
2206 if (strcmp(keyword, "GenerateBits") == 0) {
2207 if ((n = atoi(value)) <= 0 || n % 8 != 0)
2209 rdata->generate_bits = (unsigned int)n;
2212 if (strcmp(keyword, "PredictionResistance") == 0) {
2213 rdata->prediction_resistance = atoi(value) != 0;
2220 static int rand_test_run(EVP_TEST *t)
2222 RAND_DATA *expected = t->data;
2223 RAND_DATA_PASS *item;
2225 size_t got_len = expected->generate_bits / 8;
2226 OSSL_PARAM params[5], *p = params;
2227 int i = -1, ret = 0;
2228 unsigned int strength;
2231 if (!TEST_ptr(got = OPENSSL_malloc(got_len)))
2234 *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &expected->use_df);
2235 if (expected->cipher != NULL)
2236 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
2237 expected->cipher, 0);
2238 if (expected->digest != NULL)
2239 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
2240 expected->digest, 0);
2241 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC, "HMAC", 0);
2242 *p = OSSL_PARAM_construct_end();
2243 if (!TEST_true(EVP_RAND_set_ctx_params(expected->ctx, params)))
2246 strength = EVP_RAND_strength(expected->ctx);
2247 for (i = 0; i <= expected->n; i++) {
2248 item = expected->data + i;
2251 z = item->entropy != NULL ? item->entropy : (unsigned char *)"";
2252 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
2253 z, item->entropy_len);
2254 z = item->nonce != NULL ? item->nonce : (unsigned char *)"";
2255 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
2256 z, item->nonce_len);
2257 *p = OSSL_PARAM_construct_end();
2258 if (!TEST_true(EVP_RAND_set_ctx_params(expected->parent, params))
2259 || !TEST_true(EVP_RAND_instantiate(expected->parent, strength,
2263 z = item->pers != NULL ? item->pers : (unsigned char *)"";
2264 if (!TEST_true(EVP_RAND_instantiate
2265 (expected->ctx, strength,
2266 expected->prediction_resistance, z,
2270 if (item->reseed_entropy != NULL) {
2271 params[0] = OSSL_PARAM_construct_octet_string
2272 (OSSL_RAND_PARAM_TEST_ENTROPY, item->reseed_entropy,
2273 item->reseed_entropy_len);
2274 params[1] = OSSL_PARAM_construct_end();
2275 if (!TEST_true(EVP_RAND_set_ctx_params(expected->parent, params)))
2278 if (!TEST_true(EVP_RAND_reseed
2279 (expected->ctx, expected->prediction_resistance,
2280 NULL, 0, item->reseed_addin,
2281 item->reseed_addin_len)))
2284 if (item->pr_entropyA != NULL) {
2285 params[0] = OSSL_PARAM_construct_octet_string
2286 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyA,
2287 item->pr_entropyA_len);
2288 params[1] = OSSL_PARAM_construct_end();
2289 if (!TEST_true(EVP_RAND_set_ctx_params(expected->parent, params)))
2292 if (!TEST_true(EVP_RAND_generate
2293 (expected->ctx, got, got_len,
2294 strength, expected->prediction_resistance,
2295 item->addinA, item->addinA_len)))
2298 if (item->pr_entropyB != NULL) {
2299 params[0] = OSSL_PARAM_construct_octet_string
2300 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyB,
2301 item->pr_entropyB_len);
2302 params[1] = OSSL_PARAM_construct_end();
2303 if (!TEST_true(EVP_RAND_set_ctx_params(expected->parent, params)))
2306 if (!TEST_true(EVP_RAND_generate
2307 (expected->ctx, got, got_len,
2308 strength, expected->prediction_resistance,
2309 item->addinB, item->addinB_len)))
2311 if (!TEST_mem_eq(got, got_len, item->output, item->output_len))
2313 if (!TEST_true(EVP_RAND_uninstantiate(expected->ctx))
2314 || !TEST_true(EVP_RAND_uninstantiate(expected->parent))
2315 || !TEST_true(EVP_RAND_verify_zeroization(expected->ctx))
2316 || !TEST_int_eq(EVP_RAND_state(expected->ctx),
2317 EVP_RAND_STATE_UNINITIALISED))
2324 if (ret == 0 && i >= 0)
2325 TEST_info("Error in test case %d of %d\n", i, expected->n + 1);
2330 static const EVP_TEST_METHOD rand_test_method = {
2342 typedef struct kdf_data_st {
2343 /* Context for this operation */
2345 /* Expected output */
2346 unsigned char *output;
2348 OSSL_PARAM params[20];
2353 * Perform public key operation setup: lookup key, allocated ctx and call
2354 * the appropriate initialisation function
2356 static int kdf_test_init(EVP_TEST *t, const char *name)
2361 if (is_kdf_disabled(name)) {
2362 TEST_info("skipping, '%s' is disabled", name);
2367 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2369 kdata->p = kdata->params;
2370 *kdata->p = OSSL_PARAM_construct_end();
2372 kdf = EVP_KDF_fetch(libctx, name, NULL);
2374 OPENSSL_free(kdata);
2377 kdata->ctx = EVP_KDF_CTX_new(kdf);
2379 if (kdata->ctx == NULL) {
2380 OPENSSL_free(kdata);
2387 static void kdf_test_cleanup(EVP_TEST *t)
2389 KDF_DATA *kdata = t->data;
2392 for (p = kdata->params; p->key != NULL; p++)
2393 OPENSSL_free(p->data);
2394 OPENSSL_free(kdata->output);
2395 EVP_KDF_CTX_free(kdata->ctx);
2398 static int kdf_test_ctrl(EVP_TEST *t, EVP_KDF_CTX *kctx,
2401 KDF_DATA *kdata = t->data;
2404 const OSSL_PARAM *defs = EVP_KDF_settable_ctx_params(EVP_KDF_CTX_kdf(kctx));
2406 if (!TEST_ptr(name = OPENSSL_strdup(value)))
2408 p = strchr(name, ':');
2412 rv = OSSL_PARAM_allocate_from_text(kdata->p, defs, name, p,
2413 p != NULL ? strlen(p) : 0, NULL);
2414 *++kdata->p = OSSL_PARAM_construct_end();
2416 t->err = "KDF_PARAM_ERROR";
2420 if (p != NULL && strcmp(name, "digest") == 0) {
2421 if (is_digest_disabled(p)) {
2422 TEST_info("skipping, '%s' is disabled", p);
2426 if (p != NULL && strcmp(name, "cipher") == 0) {
2427 if (is_cipher_disabled(p)) {
2428 TEST_info("skipping, '%s' is disabled", p);
2436 static int kdf_test_parse(EVP_TEST *t,
2437 const char *keyword, const char *value)
2439 KDF_DATA *kdata = t->data;
2441 if (strcmp(keyword, "Output") == 0)
2442 return parse_bin(value, &kdata->output, &kdata->output_len);
2443 if (strncmp(keyword, "Ctrl", 4) == 0)
2444 return kdf_test_ctrl(t, kdata->ctx, value);
2448 static int kdf_test_run(EVP_TEST *t)
2450 KDF_DATA *expected = t->data;
2451 unsigned char *got = NULL;
2452 size_t got_len = expected->output_len;
2454 if (!EVP_KDF_CTX_set_params(expected->ctx, expected->params)) {
2455 t->err = "KDF_CTRL_ERROR";
2458 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2459 t->err = "INTERNAL_ERROR";
2462 if (EVP_KDF_derive(expected->ctx, got, got_len) <= 0) {
2463 t->err = "KDF_DERIVE_ERROR";
2466 if (!memory_err_compare(t, "KDF_MISMATCH",
2467 expected->output, expected->output_len,
2478 static const EVP_TEST_METHOD kdf_test_method = {
2490 typedef struct pkey_kdf_data_st {
2491 /* Context for this operation */
2493 /* Expected output */
2494 unsigned char *output;
2499 * Perform public key operation setup: lookup key, allocated ctx and call
2500 * the appropriate initialisation function
2502 static int pkey_kdf_test_init(EVP_TEST *t, const char *name)
2504 PKEY_KDF_DATA *kdata = NULL;
2506 if (is_kdf_disabled(name)) {
2507 TEST_info("skipping, '%s' is disabled", name);
2512 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2515 kdata->ctx = EVP_PKEY_CTX_new_from_name(libctx, name, NULL);
2516 if (kdata->ctx == NULL
2517 || EVP_PKEY_derive_init(kdata->ctx) <= 0)
2523 EVP_PKEY_CTX_free(kdata->ctx);
2524 OPENSSL_free(kdata);
2528 static void pkey_kdf_test_cleanup(EVP_TEST *t)
2530 PKEY_KDF_DATA *kdata = t->data;
2532 OPENSSL_free(kdata->output);
2533 EVP_PKEY_CTX_free(kdata->ctx);
2536 static int pkey_kdf_test_parse(EVP_TEST *t,
2537 const char *keyword, const char *value)
2539 PKEY_KDF_DATA *kdata = t->data;
2541 if (strcmp(keyword, "Output") == 0)
2542 return parse_bin(value, &kdata->output, &kdata->output_len);
2543 if (strncmp(keyword, "Ctrl", 4) == 0)
2544 return pkey_test_ctrl(t, kdata->ctx, value);
2548 static int pkey_kdf_test_run(EVP_TEST *t)
2550 PKEY_KDF_DATA *expected = t->data;
2551 unsigned char *got = NULL;
2552 size_t got_len = expected->output_len;
2554 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2555 t->err = "INTERNAL_ERROR";
2558 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
2559 t->err = "KDF_DERIVE_ERROR";
2562 if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
2563 t->err = "KDF_MISMATCH";
2573 static const EVP_TEST_METHOD pkey_kdf_test_method = {
2576 pkey_kdf_test_cleanup,
2577 pkey_kdf_test_parse,
2585 typedef struct keypair_test_data_st {
2588 } KEYPAIR_TEST_DATA;
2590 static int keypair_test_init(EVP_TEST *t, const char *pair)
2592 KEYPAIR_TEST_DATA *data;
2594 EVP_PKEY *pk = NULL, *pubk = NULL;
2595 char *pub, *priv = NULL;
2597 /* Split private and public names. */
2598 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
2599 || !TEST_ptr(pub = strchr(priv, ':'))) {
2600 t->err = "PARSING_ERROR";
2605 if (!TEST_true(find_key(&pk, priv, private_keys))) {
2606 TEST_info("Can't find private key: %s", priv);
2607 t->err = "MISSING_PRIVATE_KEY";
2610 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
2611 TEST_info("Can't find public key: %s", pub);
2612 t->err = "MISSING_PUBLIC_KEY";
2616 if (pk == NULL && pubk == NULL) {
2617 /* Both keys are listed but unsupported: skip this test */
2623 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2636 static void keypair_test_cleanup(EVP_TEST *t)
2638 OPENSSL_free(t->data);
2643 * For tests that do not accept any custom keywords.
2645 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
2650 static int keypair_test_run(EVP_TEST *t)
2653 const KEYPAIR_TEST_DATA *pair = t->data;
2655 if (pair->privk == NULL || pair->pubk == NULL) {
2657 * this can only happen if only one of the keys is not set
2658 * which means that one of them was unsupported while the
2659 * other isn't: hence a key type mismatch.
2661 t->err = "KEYPAIR_TYPE_MISMATCH";
2666 if ((rv = EVP_PKEY_eq(pair->privk, pair->pubk)) != 1 ) {
2668 t->err = "KEYPAIR_MISMATCH";
2669 } else if ( -1 == rv ) {
2670 t->err = "KEYPAIR_TYPE_MISMATCH";
2671 } else if ( -2 == rv ) {
2672 t->err = "UNSUPPORTED_KEY_COMPARISON";
2674 TEST_error("Unexpected error in key comparison");
2689 static const EVP_TEST_METHOD keypair_test_method = {
2692 keypair_test_cleanup,
2701 typedef struct keygen_test_data_st {
2702 EVP_PKEY_CTX *genctx; /* Keygen context to use */
2703 char *keyname; /* Key name to store key or NULL */
2706 static int keygen_test_init(EVP_TEST *t, const char *alg)
2708 KEYGEN_TEST_DATA *data;
2709 EVP_PKEY_CTX *genctx;
2710 int nid = OBJ_sn2nid(alg);
2712 if (nid == NID_undef) {
2713 nid = OBJ_ln2nid(alg);
2714 if (nid == NID_undef)
2718 if (is_pkey_disabled(alg)) {
2722 if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_from_name(libctx, alg, NULL)))
2725 if (EVP_PKEY_keygen_init(genctx) <= 0) {
2726 t->err = "KEYGEN_INIT_ERROR";
2730 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2732 data->genctx = genctx;
2733 data->keyname = NULL;
2739 EVP_PKEY_CTX_free(genctx);
2743 static void keygen_test_cleanup(EVP_TEST *t)
2745 KEYGEN_TEST_DATA *keygen = t->data;
2747 EVP_PKEY_CTX_free(keygen->genctx);
2748 OPENSSL_free(keygen->keyname);
2749 OPENSSL_free(t->data);
2753 static int keygen_test_parse(EVP_TEST *t,
2754 const char *keyword, const char *value)
2756 KEYGEN_TEST_DATA *keygen = t->data;
2758 if (strcmp(keyword, "KeyName") == 0)
2759 return TEST_ptr(keygen->keyname = OPENSSL_strdup(value));
2760 if (strcmp(keyword, "Ctrl") == 0)
2761 return pkey_test_ctrl(t, keygen->genctx, value);
2765 static int keygen_test_run(EVP_TEST *t)
2767 KEYGEN_TEST_DATA *keygen = t->data;
2768 EVP_PKEY *pkey = NULL;
2771 if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) {
2772 t->err = "KEYGEN_GENERATE_ERROR";
2776 if (!evp_pkey_is_provided(pkey)) {
2777 TEST_info("Warning: legacy key generated %s", keygen->keyname);
2780 if (keygen->keyname != NULL) {
2784 if (find_key(NULL, keygen->keyname, private_keys)) {
2785 TEST_info("Duplicate key %s", keygen->keyname);
2789 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
2791 key->name = keygen->keyname;
2792 keygen->keyname = NULL;
2794 key->next = private_keys;
2798 EVP_PKEY_free(pkey);
2807 static const EVP_TEST_METHOD keygen_test_method = {
2810 keygen_test_cleanup,
2816 ** DIGEST SIGN+VERIFY TESTS
2820 int is_verify; /* Set to 1 if verifying */
2821 int is_oneshot; /* Set to 1 for one shot operation */
2822 const EVP_MD *md; /* Digest to use */
2823 EVP_MD_CTX *ctx; /* Digest context */
2825 STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
2826 unsigned char *osin; /* Input data if one shot */
2827 size_t osin_len; /* Input length data if one shot */
2828 unsigned char *output; /* Expected output */
2829 size_t output_len; /* Expected output length */
2832 static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
2835 const EVP_MD *md = NULL;
2836 DIGESTSIGN_DATA *mdat;
2838 if (strcmp(alg, "NULL") != 0) {
2839 if (is_digest_disabled(alg)) {
2843 md = EVP_get_digestbyname(alg);
2847 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
2850 if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
2854 mdat->is_verify = is_verify;
2855 mdat->is_oneshot = is_oneshot;
2860 static int digestsign_test_init(EVP_TEST *t, const char *alg)
2862 return digestsigver_test_init(t, alg, 0, 0);
2865 static void digestsigver_test_cleanup(EVP_TEST *t)
2867 DIGESTSIGN_DATA *mdata = t->data;
2869 EVP_MD_CTX_free(mdata->ctx);
2870 sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
2871 OPENSSL_free(mdata->osin);
2872 OPENSSL_free(mdata->output);
2873 OPENSSL_free(mdata);
2877 static int digestsigver_test_parse(EVP_TEST *t,
2878 const char *keyword, const char *value)
2880 DIGESTSIGN_DATA *mdata = t->data;
2882 if (strcmp(keyword, "Key") == 0) {
2883 EVP_PKEY *pkey = NULL;
2885 const char *name = mdata->md == NULL ? NULL : EVP_MD_name(mdata->md);
2887 if (mdata->is_verify)
2888 rv = find_key(&pkey, value, public_keys);
2890 rv = find_key(&pkey, value, private_keys);
2891 if (rv == 0 || pkey == NULL) {
2895 if (mdata->is_verify) {
2896 if (!EVP_DigestVerifyInit_with_libctx(mdata->ctx, &mdata->pctx,
2897 name, libctx, NULL, pkey))
2898 t->err = "DIGESTVERIFYINIT_ERROR";
2901 if (!EVP_DigestSignInit_with_libctx(mdata->ctx, &mdata->pctx,
2902 name, libctx, NULL, pkey))
2903 t->err = "DIGESTSIGNINIT_ERROR";
2907 if (strcmp(keyword, "Input") == 0) {
2908 if (mdata->is_oneshot)
2909 return parse_bin(value, &mdata->osin, &mdata->osin_len);
2910 return evp_test_buffer_append(value, &mdata->input);
2912 if (strcmp(keyword, "Output") == 0)
2913 return parse_bin(value, &mdata->output, &mdata->output_len);
2915 if (!mdata->is_oneshot) {
2916 if (strcmp(keyword, "Count") == 0)
2917 return evp_test_buffer_set_count(value, mdata->input);
2918 if (strcmp(keyword, "Ncopy") == 0)
2919 return evp_test_buffer_ncopy(value, mdata->input);
2921 if (strcmp(keyword, "Ctrl") == 0) {
2922 if (mdata->pctx == NULL)
2924 return pkey_test_ctrl(t, mdata->pctx, value);
2929 static int digestsign_update_fn(void *ctx, const unsigned char *buf,
2932 return EVP_DigestSignUpdate(ctx, buf, buflen);
2935 static int digestsign_test_run(EVP_TEST *t)
2937 DIGESTSIGN_DATA *expected = t->data;
2938 unsigned char *got = NULL;
2941 if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
2943 t->err = "DIGESTUPDATE_ERROR";
2947 if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
2948 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
2951 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2952 t->err = "MALLOC_FAILURE";
2955 if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
2956 t->err = "DIGESTSIGNFINAL_ERROR";
2959 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
2960 expected->output, expected->output_len,
2970 static const EVP_TEST_METHOD digestsign_test_method = {
2972 digestsign_test_init,
2973 digestsigver_test_cleanup,
2974 digestsigver_test_parse,
2978 static int digestverify_test_init(EVP_TEST *t, const char *alg)
2980 return digestsigver_test_init(t, alg, 1, 0);
2983 static int digestverify_update_fn(void *ctx, const unsigned char *buf,
2986 return EVP_DigestVerifyUpdate(ctx, buf, buflen);
2989 static int digestverify_test_run(EVP_TEST *t)
2991 DIGESTSIGN_DATA *mdata = t->data;
2993 if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
2994 t->err = "DIGESTUPDATE_ERROR";
2998 if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
2999 mdata->output_len) <= 0)
3000 t->err = "VERIFY_ERROR";
3004 static const EVP_TEST_METHOD digestverify_test_method = {
3006 digestverify_test_init,
3007 digestsigver_test_cleanup,
3008 digestsigver_test_parse,
3009 digestverify_test_run
3012 static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
3014 return digestsigver_test_init(t, alg, 0, 1);
3017 static int oneshot_digestsign_test_run(EVP_TEST *t)
3019 DIGESTSIGN_DATA *expected = t->data;
3020 unsigned char *got = NULL;
3023 if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
3024 expected->osin, expected->osin_len)) {
3025 t->err = "DIGESTSIGN_LENGTH_ERROR";
3028 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
3029 t->err = "MALLOC_FAILURE";
3032 if (!EVP_DigestSign(expected->ctx, got, &got_len,
3033 expected->osin, expected->osin_len)) {
3034 t->err = "DIGESTSIGN_ERROR";
3037 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
3038 expected->output, expected->output_len,
3048 static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
3049 "OneShotDigestSign",
3050 oneshot_digestsign_test_init,
3051 digestsigver_test_cleanup,
3052 digestsigver_test_parse,
3053 oneshot_digestsign_test_run
3056 static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
3058 return digestsigver_test_init(t, alg, 1, 1);
3061 static int oneshot_digestverify_test_run(EVP_TEST *t)
3063 DIGESTSIGN_DATA *mdata = t->data;
3065 if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
3066 mdata->osin, mdata->osin_len) <= 0)
3067 t->err = "VERIFY_ERROR";
3071 static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
3072 "OneShotDigestVerify",
3073 oneshot_digestverify_test_init,
3074 digestsigver_test_cleanup,
3075 digestsigver_test_parse,
3076 oneshot_digestverify_test_run
3081 ** PARSING AND DISPATCH
3084 static const EVP_TEST_METHOD *evp_test_list[] = {
3086 &cipher_test_method,
3087 &digest_test_method,
3088 &digestsign_test_method,
3089 &digestverify_test_method,
3090 &encode_test_method,
3092 &pkey_kdf_test_method,
3093 &keypair_test_method,
3094 &keygen_test_method,
3096 &oneshot_digestsign_test_method,
3097 &oneshot_digestverify_test_method,
3099 &pdecrypt_test_method,
3100 &pderive_test_method,
3102 &pverify_recover_test_method,
3103 &pverify_test_method,
3107 static const EVP_TEST_METHOD *find_test(const char *name)
3109 const EVP_TEST_METHOD **tt;
3111 for (tt = evp_test_list; *tt; tt++) {
3112 if (strcmp(name, (*tt)->name) == 0)
3118 static void clear_test(EVP_TEST *t)
3120 test_clearstanza(&t->s);
3122 if (t->data != NULL) {
3123 if (t->meth != NULL)
3124 t->meth->cleanup(t);
3125 OPENSSL_free(t->data);
3128 OPENSSL_free(t->expected_err);
3129 t->expected_err = NULL;
3130 OPENSSL_free(t->reason);
3139 /* Check for errors in the test structure; return 1 if okay, else 0. */
3140 static int check_test_error(EVP_TEST *t)
3145 if (t->err == NULL && t->expected_err == NULL)
3147 if (t->err != NULL && t->expected_err == NULL) {
3148 if (t->aux_err != NULL) {
3149 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
3150 t->s.test_file, t->s.start, t->aux_err, t->err);
3152 TEST_info("%s:%d: Source of above error; unexpected error %s",
3153 t->s.test_file, t->s.start, t->err);
3157 if (t->err == NULL && t->expected_err != NULL) {
3158 TEST_info("%s:%d: Succeeded but was expecting %s",
3159 t->s.test_file, t->s.start, t->expected_err);
3163 if (strcmp(t->err, t->expected_err) != 0) {
3164 TEST_info("%s:%d: Expected %s got %s",
3165 t->s.test_file, t->s.start, t->expected_err, t->err);
3169 if (t->reason == NULL)
3172 if (t->reason == NULL) {
3173 TEST_info("%s:%d: Test is missing function or reason code",
3174 t->s.test_file, t->s.start);
3178 err = ERR_peek_error();
3180 TEST_info("%s:%d: Expected error \"%s\" not set",
3181 t->s.test_file, t->s.start, t->reason);
3185 reason = ERR_reason_error_string(err);
3186 if (reason == NULL) {
3187 TEST_info("%s:%d: Expected error \"%s\", no strings available."
3189 t->s.test_file, t->s.start, t->reason);
3193 if (strcmp(reason, t->reason) == 0)
3196 TEST_info("%s:%d: Expected error \"%s\", got \"%s\"",
3197 t->s.test_file, t->s.start, t->reason, reason);
3202 /* Run a parsed test. Log a message and return 0 on error. */
3203 static int run_test(EVP_TEST *t)
3205 if (t->meth == NULL)
3212 if (t->err == NULL && t->meth->run_test(t) != 1) {
3213 TEST_info("%s:%d %s error",
3214 t->s.test_file, t->s.start, t->meth->name);
3217 if (!check_test_error(t)) {
3218 TEST_openssl_errors();
3227 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
3229 for (; lst != NULL; lst = lst->next) {
3230 if (strcmp(lst->name, name) == 0) {
3239 static void free_key_list(KEY_LIST *lst)
3241 while (lst != NULL) {
3242 KEY_LIST *next = lst->next;
3244 EVP_PKEY_free(lst->key);
3245 OPENSSL_free(lst->name);
3252 * Is the key type an unsupported algorithm?
3254 static int key_unsupported(void)
3256 long err = ERR_peek_last_error();
3258 if (ERR_GET_LIB(err) == ERR_LIB_EVP
3259 && (ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM
3260 || ERR_GET_REASON(err) == EVP_R_FETCH_FAILED)) {
3264 #ifndef OPENSSL_NO_EC
3266 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
3267 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
3270 if (ERR_GET_LIB(err) == ERR_LIB_EC
3271 && (ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP
3272 || ERR_GET_REASON(err) == EC_R_INVALID_CURVE)) {
3276 #endif /* OPENSSL_NO_EC */
3280 /* NULL out the value from |pp| but return it. This "steals" a pointer. */
3281 static char *take_value(PAIR *pp)
3283 char *p = pp->value;
3290 * Return 1 if one of the providers named in the string is available.
3291 * The provider names are separated with whitespace.
3292 * NOTE: destructive function, it inserts '\0' after each provider name.
3294 static int prov_available(char *providers)
3300 for (; isspace(*providers); providers++)
3302 if (*providers == '\0')
3303 break; /* End of the road */
3304 for (p = providers; *p != '\0' && !isspace(*p); p++)
3310 if (OSSL_PROVIDER_available(libctx, providers))
3311 return 1; /* Found one */
3316 /* Read and parse one test. Return 0 if failure, 1 if okay. */
3317 static int parse(EVP_TEST *t)
3319 KEY_LIST *key, **klist;
3322 int i, skip_availablein = 0;
3326 if (BIO_eof(t->s.fp))
3329 if (!test_readstanza(&t->s))
3331 } while (t->s.numpairs == 0);
3332 pp = &t->s.pairs[0];
3334 /* Are we adding a key? */
3338 if (strcmp(pp->key, "PrivateKey") == 0) {
3339 pkey = PEM_read_bio_PrivateKey_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3340 if (pkey == NULL && !key_unsupported()) {
3341 EVP_PKEY_free(pkey);
3342 TEST_info("Can't read private key %s", pp->value);
3343 TEST_openssl_errors();
3346 klist = &private_keys;
3347 } else if (strcmp(pp->key, "PublicKey") == 0) {
3348 pkey = PEM_read_bio_PUBKEY_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3349 if (pkey == NULL && !key_unsupported()) {
3350 EVP_PKEY_free(pkey);
3351 TEST_info("Can't read public key %s", pp->value);
3352 TEST_openssl_errors();
3355 klist = &public_keys;
3356 } else if (strcmp(pp->key, "PrivateKeyRaw") == 0
3357 || strcmp(pp->key, "PublicKeyRaw") == 0 ) {
3358 char *strnid = NULL, *keydata = NULL;
3359 unsigned char *keybin;
3363 if (strcmp(pp->key, "PrivateKeyRaw") == 0)
3364 klist = &private_keys;
3366 klist = &public_keys;
3368 strnid = strchr(pp->value, ':');
3369 if (strnid != NULL) {
3371 keydata = strchr(strnid, ':');
3372 if (keydata != NULL)
3375 if (keydata == NULL) {
3376 TEST_info("Failed to parse %s value", pp->key);
3380 nid = OBJ_txt2nid(strnid);
3381 if (nid == NID_undef) {
3382 TEST_info("Unrecognised algorithm NID");
3385 if (!parse_bin(keydata, &keybin, &keylen)) {
3386 TEST_info("Failed to create binary key");
3389 if (klist == &private_keys)
3390 pkey = EVP_PKEY_new_raw_private_key_with_libctx(libctx, strnid, NULL,
3393 pkey = EVP_PKEY_new_raw_public_key_with_libctx(libctx, strnid, NULL,
3395 if (pkey == NULL && !key_unsupported()) {
3396 TEST_info("Can't read %s data", pp->key);
3397 OPENSSL_free(keybin);
3398 TEST_openssl_errors();
3401 OPENSSL_free(keybin);
3402 } else if (strcmp(pp->key, "Availablein") == 0) {
3403 if (!prov_available(pp->value)) {
3404 TEST_info("skipping, '%s' provider not available: %s:%d",
3405 pp->value, t->s.test_file, t->s.start);
3414 /* If we have a key add to list */
3415 if (klist != NULL) {
3416 if (find_key(NULL, pp->value, *klist)) {
3417 TEST_info("Duplicate key %s", pp->value);
3420 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
3422 key->name = take_value(pp);
3427 /* Go back and start a new stanza. */
3428 if ((t->s.numpairs - skip_availablein) != 1)
3429 TEST_info("Line %d: missing blank line\n", t->s.curr);
3433 /* Find the test, based on first keyword. */
3434 if (!TEST_ptr(t->meth = find_test(pp->key)))
3436 if (!t->meth->init(t, pp->value)) {
3437 TEST_error("unknown %s: %s\n", pp->key, pp->value);
3441 /* TEST_info("skipping %s %s", pp->key, pp->value); */
3445 for (pp++, i = 1; i < (t->s.numpairs - skip_availablein); pp++, i++) {
3446 if (strcmp(pp->key, "Availablein") == 0) {
3447 TEST_info("Line %d: 'Availablein' should be the first option",
3450 } else if (strcmp(pp->key, "Result") == 0) {
3451 if (t->expected_err != NULL) {
3452 TEST_info("Line %d: multiple result lines", t->s.curr);
3455 t->expected_err = take_value(pp);
3456 } else if (strcmp(pp->key, "Function") == 0) {
3457 /* Ignore old line. */
3458 } else if (strcmp(pp->key, "Reason") == 0) {
3459 if (t->reason != NULL) {
3460 TEST_info("Line %d: multiple reason lines", t->s.curr);
3463 t->reason = take_value(pp);
3465 /* Must be test specific line: try to parse it */
3466 int rv = t->meth->parse(t, pp->key, pp->value);
3469 TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key);
3473 TEST_info("Line %d: error processing keyword %s = %s\n",
3474 t->s.curr, pp->key, pp->value);
3483 static int run_file_tests(int i)
3486 const char *testfile = test_get_argument(i);
3489 if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t))))
3491 if (!test_start_file(&t->s, testfile)) {
3496 while (!BIO_eof(t->s.fp)) {
3502 if (c == 0 || !run_test(t)) {
3507 test_end_file(&t->s);
3510 free_key_list(public_keys);
3511 free_key_list(private_keys);
3518 const OPTIONS *test_get_options(void)
3520 static const OPTIONS test_options[] = {
3521 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
3522 { "config", OPT_CONFIG_FILE, '<',
3523 "The configuration file to use for the libctx" },
3524 { OPT_HELP_STR, 1, '-',
3525 "file\tFile to run tests on.\n" },
3528 return test_options;
3531 int setup_tests(void)
3534 char *config_file = NULL;
3538 while ((o = opt_next()) != OPT_EOF) {
3540 case OPT_CONFIG_FILE:
3541 config_file = opt_arg();
3543 case OPT_TEST_CASES:
3552 * Load the 'null' provider into the default library context to ensure that
3553 * the the tests do not fallback to using the default provider.
3555 prov_null = OSSL_PROVIDER_load(NULL, "null");
3556 if (prov_null == NULL) {
3557 opt_printf_stderr("Failed to load null provider into default libctx\n");
3561 /* load the provider via configuration into the created library context */
3562 libctx = OPENSSL_CTX_new();
3564 || !OPENSSL_CTX_load_config(libctx, config_file)) {
3565 TEST_error("Failed to load config %s\n", config_file);
3569 n = test_get_argument_count();
3573 ADD_ALL_TESTS(run_file_tests, n);
3577 void cleanup_tests(void)
3579 OSSL_PROVIDER_unload(prov_null);
3580 OPENSSL_CTX_free(libctx);
3583 #define STR_STARTS_WITH(str, pre) strncasecmp(pre, str, strlen(pre)) == 0
3584 #define STR_ENDS_WITH(str, pre) \
3585 strlen(str) < strlen(pre) ? 0 : (strcasecmp(pre, str + strlen(str) - strlen(pre)) == 0)
3587 static int is_digest_disabled(const char *name)
3589 #ifdef OPENSSL_NO_BLAKE2
3590 if (STR_STARTS_WITH(name, "BLAKE"))
3593 #ifdef OPENSSL_NO_MD2
3594 if (strcasecmp(name, "MD2") == 0)
3597 #ifdef OPENSSL_NO_MDC2
3598 if (strcasecmp(name, "MDC2") == 0)
3601 #ifdef OPENSSL_NO_MD4
3602 if (strcasecmp(name, "MD4") == 0)
3605 #ifdef OPENSSL_NO_MD5
3606 if (strcasecmp(name, "MD5") == 0)
3609 #ifdef OPENSSL_NO_RMD160
3610 if (strcasecmp(name, "RIPEMD160") == 0)
3613 #ifdef OPENSSL_NO_SM3
3614 if (strcasecmp(name, "SM3") == 0)
3617 #ifdef OPENSSL_NO_WHIRLPOOL
3618 if (strcasecmp(name, "WHIRLPOOL") == 0)
3624 static int is_pkey_disabled(const char *name)
3626 #ifdef OPENSSL_NO_RSA
3627 if (STR_STARTS_WITH(name, "RSA"))
3630 #ifdef OPENSSL_NO_EC
3631 if (STR_STARTS_WITH(name, "EC"))
3634 #ifdef OPENSSL_NO_DH
3635 if (STR_STARTS_WITH(name, "DH"))
3638 #ifdef OPENSSL_NO_DSA
3639 if (STR_STARTS_WITH(name, "DSA"))
3645 static int is_mac_disabled(const char *name)
3647 #ifdef OPENSSL_NO_BLAKE2
3648 if (STR_STARTS_WITH(name, "BLAKE2BMAC")
3649 || STR_STARTS_WITH(name, "BLAKE2SMAC"))
3652 #ifdef OPENSSL_NO_CMAC
3653 if (STR_STARTS_WITH(name, "CMAC"))
3656 #ifdef OPENSSL_NO_POLY1305
3657 if (STR_STARTS_WITH(name, "Poly1305"))
3660 #ifdef OPENSSL_NO_SIPHASH
3661 if (STR_STARTS_WITH(name, "SipHash"))
3666 static int is_kdf_disabled(const char *name)
3668 #ifdef OPENSSL_NO_SCRYPT
3669 if (STR_ENDS_WITH(name, "SCRYPT"))
3672 #ifdef OPENSSL_NO_CMS
3673 if (strcasecmp(name, "X942KDF") == 0)
3675 #endif /* OPENSSL_NO_CMS */
3679 static int is_cipher_disabled(const char *name)
3681 #ifdef OPENSSL_NO_ARIA
3682 if (STR_STARTS_WITH(name, "ARIA"))
3685 #ifdef OPENSSL_NO_BF
3686 if (STR_STARTS_WITH(name, "BF"))
3689 #ifdef OPENSSL_NO_CAMELLIA
3690 if (STR_STARTS_WITH(name, "CAMELLIA"))
3693 #ifdef OPENSSL_NO_CAST
3694 if (STR_STARTS_WITH(name, "CAST"))
3697 #ifdef OPENSSL_NO_CHACHA
3698 if (STR_STARTS_WITH(name, "CHACHA"))
3701 #ifdef OPENSSL_NO_POLY1305
3702 if (STR_ENDS_WITH(name, "Poly1305"))
3705 #ifdef OPENSSL_NO_DES
3706 if (STR_STARTS_WITH(name, "DES"))
3709 #ifdef OPENSSL_NO_OCB
3710 if (STR_ENDS_WITH(name, "OCB"))
3713 #ifdef OPENSSL_NO_IDEA
3714 if (STR_STARTS_WITH(name, "IDEA"))
3717 #ifdef OPENSSL_NO_RC2
3718 if (STR_STARTS_WITH(name, "RC2"))
3721 #ifdef OPENSSL_NO_RC4
3722 if (STR_STARTS_WITH(name, "RC4"))
3725 #ifdef OPENSSL_NO_RC5
3726 if (STR_STARTS_WITH(name, "RC5"))
3729 #ifdef OPENSSL_NO_SEED
3730 if (STR_STARTS_WITH(name, "SEED"))
3733 #ifdef OPENSSL_NO_SIV
3734 if (STR_ENDS_WITH(name, "SIV"))
3737 #ifdef OPENSSL_NO_SM4
3738 if (STR_STARTS_WITH(name, "SM4"))