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 <openssl/fips_names.h>
25 #include "internal/numbers.h"
26 #include "internal/nelem.h"
27 #include "crypto/evp.h"
30 typedef struct evp_test_buffer_st EVP_TEST_BUFFER;
31 DEFINE_STACK_OF(EVP_TEST_BUFFER)
35 typedef struct evp_test_method_st EVP_TEST_METHOD;
37 /* Structure holding test information */
38 typedef struct evp_test_st {
39 STANZA s; /* Common test stanza */
41 int skip; /* Current test should be skipped */
42 const EVP_TEST_METHOD *meth; /* method for this test */
43 const char *err, *aux_err; /* Error string for test */
44 char *expected_err; /* Expected error value of test */
45 char *reason; /* Expected error reason string */
46 void *data; /* test specific data */
49 /* Test method structure */
50 struct evp_test_method_st {
51 /* Name of test as it appears in file */
53 /* Initialise test for "alg" */
54 int (*init) (EVP_TEST * t, const char *alg);
56 void (*cleanup) (EVP_TEST * t);
57 /* Test specific name value pair processing */
58 int (*parse) (EVP_TEST * t, const char *name, const char *value);
59 /* Run the test itself */
60 int (*run_test) (EVP_TEST * t);
63 /* Linked list of named keys. */
64 typedef struct key_list_st {
67 struct key_list_st *next;
70 typedef enum OPTION_choice {
77 static OSSL_PROVIDER *prov_null = NULL;
78 static OSSL_LIB_CTX *libctx = NULL;
80 /* List of public and private keys */
81 static KEY_LIST *private_keys;
82 static KEY_LIST *public_keys;
84 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst);
85 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen);
86 static int is_digest_disabled(const char *name);
87 static int is_pkey_disabled(const char *name);
88 static int is_mac_disabled(const char *name);
89 static int is_cipher_disabled(const char *name);
90 static int is_kdf_disabled(const char *name);
93 * Compare two memory regions for equality, returning zero if they differ.
94 * However, if there is expected to be an error and the actual error
95 * matches then the memory is expected to be different so handle this
96 * case without producing unnecessary test framework output.
98 static int memory_err_compare(EVP_TEST *t, const char *err,
99 const void *expected, size_t expected_len,
100 const void *got, size_t got_len)
104 if (t->expected_err != NULL && strcmp(t->expected_err, err) == 0)
105 r = !TEST_mem_ne(expected, expected_len, got, got_len);
107 r = TEST_mem_eq(expected, expected_len, got, got_len);
114 * Structure used to hold a list of blocks of memory to test
115 * calls to "update" like functions.
117 struct evp_test_buffer_st {
124 static void evp_test_buffer_free(EVP_TEST_BUFFER *db)
127 OPENSSL_free(db->buf);
132 /* append buffer to a list */
133 static int evp_test_buffer_append(const char *value,
134 STACK_OF(EVP_TEST_BUFFER) **sk)
136 EVP_TEST_BUFFER *db = NULL;
138 if (!TEST_ptr(db = OPENSSL_malloc(sizeof(*db))))
141 if (!parse_bin(value, &db->buf, &db->buflen))
146 if (*sk == NULL && !TEST_ptr(*sk = sk_EVP_TEST_BUFFER_new_null()))
148 if (!sk_EVP_TEST_BUFFER_push(*sk, db))
154 evp_test_buffer_free(db);
158 /* replace last buffer in list with copies of itself */
159 static int evp_test_buffer_ncopy(const char *value,
160 STACK_OF(EVP_TEST_BUFFER) *sk)
163 unsigned char *tbuf, *p;
165 int ncopy = atoi(value);
170 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
172 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
174 tbuflen = db->buflen * ncopy;
175 if (!TEST_ptr(tbuf = OPENSSL_malloc(tbuflen)))
177 for (i = 0, p = tbuf; i < ncopy; i++, p += db->buflen)
178 memcpy(p, db->buf, db->buflen);
180 OPENSSL_free(db->buf);
182 db->buflen = tbuflen;
186 /* set repeat count for last buffer in list */
187 static int evp_test_buffer_set_count(const char *value,
188 STACK_OF(EVP_TEST_BUFFER) *sk)
191 int count = atoi(value);
196 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
199 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
200 if (db->count_set != 0)
203 db->count = (size_t)count;
208 /* call "fn" with each element of the list in turn */
209 static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER) *sk,
211 const unsigned char *buf,
217 for (i = 0; i < sk_EVP_TEST_BUFFER_num(sk); i++) {
218 EVP_TEST_BUFFER *tb = sk_EVP_TEST_BUFFER_value(sk, i);
221 for (j = 0; j < tb->count; j++) {
222 if (fn(ctx, tb->buf, tb->buflen) <= 0)
230 * Unescape some sequences in string literals (only \n for now).
231 * Return an allocated buffer, set |out_len|. If |input_len|
232 * is zero, get an empty buffer but set length to zero.
234 static unsigned char* unescape(const char *input, size_t input_len,
237 unsigned char *ret, *p;
240 if (input_len == 0) {
242 return OPENSSL_zalloc(1);
245 /* Escaping is non-expanding; over-allocate original size for simplicity. */
246 if (!TEST_ptr(ret = p = OPENSSL_malloc(input_len)))
249 for (i = 0; i < input_len; i++) {
250 if (*input == '\\') {
251 if (i == input_len - 1 || *++input != 'n') {
252 TEST_error("Bad escape sequence in file");
272 * For a hex string "value" convert to a binary allocated buffer.
273 * Return 1 on success or 0 on failure.
275 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen)
279 /* Check for NULL literal */
280 if (strcmp(value, "NULL") == 0) {
286 /* Check for empty value */
287 if (*value == '\0') {
289 * Don't return NULL for zero length buffer. This is needed for
290 * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key
291 * buffer even if the key length is 0, in order to detect key reset.
293 *buf = OPENSSL_malloc(1);
301 /* Check for string literal */
302 if (value[0] == '"') {
303 size_t vlen = strlen(++value);
305 if (vlen == 0 || value[vlen - 1] != '"')
308 *buf = unescape(value, vlen, buflen);
309 return *buf == NULL ? 0 : 1;
312 /* Otherwise assume as hex literal and convert it to binary buffer */
313 if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) {
314 TEST_info("Can't convert %s", value);
315 TEST_openssl_errors();
318 /* Size of input buffer means we'll never overflow */
324 ** MESSAGE DIGEST TESTS
327 typedef struct digest_data_st {
328 /* Digest this test is for */
329 const EVP_MD *digest;
330 EVP_MD *fetched_digest;
331 /* Input to digest */
332 STACK_OF(EVP_TEST_BUFFER) *input;
333 /* Expected output */
334 unsigned char *output;
340 static int digest_test_init(EVP_TEST *t, const char *alg)
343 const EVP_MD *digest;
344 EVP_MD *fetched_digest;
346 if (is_digest_disabled(alg)) {
347 TEST_info("skipping, '%s' is disabled", alg);
352 if ((digest = fetched_digest = EVP_MD_fetch(libctx, alg, NULL)) == NULL
353 && (digest = EVP_get_digestbyname(alg)) == NULL)
355 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
358 mdat->digest = digest;
359 mdat->fetched_digest = fetched_digest;
361 if (fetched_digest != NULL)
362 TEST_info("%s is fetched", alg);
366 static void digest_test_cleanup(EVP_TEST *t)
368 DIGEST_DATA *mdat = t->data;
370 sk_EVP_TEST_BUFFER_pop_free(mdat->input, evp_test_buffer_free);
371 OPENSSL_free(mdat->output);
372 EVP_MD_free(mdat->fetched_digest);
375 static int digest_test_parse(EVP_TEST *t,
376 const char *keyword, const char *value)
378 DIGEST_DATA *mdata = t->data;
380 if (strcmp(keyword, "Input") == 0)
381 return evp_test_buffer_append(value, &mdata->input);
382 if (strcmp(keyword, "Output") == 0)
383 return parse_bin(value, &mdata->output, &mdata->output_len);
384 if (strcmp(keyword, "Count") == 0)
385 return evp_test_buffer_set_count(value, mdata->input);
386 if (strcmp(keyword, "Ncopy") == 0)
387 return evp_test_buffer_ncopy(value, mdata->input);
388 if (strcmp(keyword, "Padding") == 0)
389 return (mdata->pad_type = atoi(value)) > 0;
393 static int digest_update_fn(void *ctx, const unsigned char *buf, size_t buflen)
395 return EVP_DigestUpdate(ctx, buf, buflen);
398 static int digest_test_run(EVP_TEST *t)
400 DIGEST_DATA *expected = t->data;
402 unsigned char *got = NULL;
403 unsigned int got_len;
404 OSSL_PARAM params[2];
406 t->err = "TEST_FAILURE";
407 if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
410 got = OPENSSL_malloc(expected->output_len > EVP_MAX_MD_SIZE ?
411 expected->output_len : EVP_MAX_MD_SIZE);
415 if (!EVP_DigestInit_ex(mctx, expected->digest, NULL)) {
416 t->err = "DIGESTINIT_ERROR";
419 if (expected->pad_type > 0) {
420 params[0] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_PAD_TYPE,
421 &expected->pad_type);
422 params[1] = OSSL_PARAM_construct_end();
423 if (!TEST_int_gt(EVP_MD_CTX_set_params(mctx, params), 0)) {
424 t->err = "PARAMS_ERROR";
428 if (!evp_test_buffer_do(expected->input, digest_update_fn, mctx)) {
429 t->err = "DIGESTUPDATE_ERROR";
433 if (EVP_MD_flags(expected->digest) & EVP_MD_FLAG_XOF) {
434 EVP_MD_CTX *mctx_cpy;
435 char dont[] = "touch";
437 if (!TEST_ptr(mctx_cpy = EVP_MD_CTX_new())) {
440 if (!EVP_MD_CTX_copy(mctx_cpy, mctx)) {
441 EVP_MD_CTX_free(mctx_cpy);
444 if (!EVP_DigestFinalXOF(mctx_cpy, (unsigned char *)dont, 0)) {
445 EVP_MD_CTX_free(mctx_cpy);
446 t->err = "DIGESTFINALXOF_ERROR";
449 if (!TEST_str_eq(dont, "touch")) {
450 EVP_MD_CTX_free(mctx_cpy);
451 t->err = "DIGESTFINALXOF_ERROR";
454 EVP_MD_CTX_free(mctx_cpy);
456 got_len = expected->output_len;
457 if (!EVP_DigestFinalXOF(mctx, got, got_len)) {
458 t->err = "DIGESTFINALXOF_ERROR";
462 if (!EVP_DigestFinal(mctx, got, &got_len)) {
463 t->err = "DIGESTFINAL_ERROR";
467 if (!TEST_int_eq(expected->output_len, got_len)) {
468 t->err = "DIGEST_LENGTH_MISMATCH";
471 if (!memory_err_compare(t, "DIGEST_MISMATCH",
472 expected->output, expected->output_len,
480 EVP_MD_CTX_free(mctx);
484 static const EVP_TEST_METHOD digest_test_method = {
496 typedef struct cipher_data_st {
497 const EVP_CIPHER *cipher;
498 EVP_CIPHER *fetched_cipher;
500 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
504 size_t key_bits; /* Used by RC2 */
508 unsigned char *plaintext;
509 size_t plaintext_len;
510 unsigned char *ciphertext;
511 size_t ciphertext_len;
512 /* GCM, CCM, OCB and SIV only */
513 unsigned char *aad[AAD_NUM];
514 size_t aad_len[AAD_NUM];
516 const char *cts_mode;
521 static int cipher_test_init(EVP_TEST *t, const char *alg)
523 const EVP_CIPHER *cipher;
524 EVP_CIPHER *fetched_cipher;
528 if (is_cipher_disabled(alg)) {
530 TEST_info("skipping, '%s' is disabled", alg);
534 if ((cipher = fetched_cipher = EVP_CIPHER_fetch(libctx, alg, NULL)) == NULL
535 && (cipher = EVP_get_cipherbyname(alg)) == NULL)
538 cdat = OPENSSL_zalloc(sizeof(*cdat));
539 cdat->cipher = cipher;
540 cdat->fetched_cipher = fetched_cipher;
542 m = EVP_CIPHER_mode(cipher);
543 if (m == EVP_CIPH_GCM_MODE
544 || m == EVP_CIPH_OCB_MODE
545 || m == EVP_CIPH_SIV_MODE
546 || m == EVP_CIPH_CCM_MODE)
548 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
554 if (fetched_cipher != NULL)
555 TEST_info("%s is fetched", alg);
559 static void cipher_test_cleanup(EVP_TEST *t)
562 CIPHER_DATA *cdat = t->data;
564 OPENSSL_free(cdat->key);
565 OPENSSL_free(cdat->iv);
566 OPENSSL_free(cdat->ciphertext);
567 OPENSSL_free(cdat->plaintext);
568 for (i = 0; i < AAD_NUM; i++)
569 OPENSSL_free(cdat->aad[i]);
570 OPENSSL_free(cdat->tag);
571 EVP_CIPHER_free(cdat->fetched_cipher);
574 static int cipher_test_parse(EVP_TEST *t, const char *keyword,
577 CIPHER_DATA *cdat = t->data;
580 if (strcmp(keyword, "Key") == 0)
581 return parse_bin(value, &cdat->key, &cdat->key_len);
582 if (strcmp(keyword, "Rounds") == 0) {
586 cdat->rounds = (unsigned int)i;
589 if (strcmp(keyword, "IV") == 0)
590 return parse_bin(value, &cdat->iv, &cdat->iv_len);
591 if (strcmp(keyword, "Plaintext") == 0)
592 return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len);
593 if (strcmp(keyword, "Ciphertext") == 0)
594 return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
595 if (strcmp(keyword, "KeyBits") == 0) {
599 cdat->key_bits = (size_t)i;
603 if (strcmp(keyword, "AAD") == 0) {
604 for (i = 0; i < AAD_NUM; i++) {
605 if (cdat->aad[i] == NULL)
606 return parse_bin(value, &cdat->aad[i], &cdat->aad_len[i]);
610 if (strcmp(keyword, "Tag") == 0)
611 return parse_bin(value, &cdat->tag, &cdat->tag_len);
612 if (strcmp(keyword, "SetTagLate") == 0) {
613 if (strcmp(value, "TRUE") == 0)
615 else if (strcmp(value, "FALSE") == 0)
623 if (strcmp(keyword, "Operation") == 0) {
624 if (strcmp(value, "ENCRYPT") == 0)
626 else if (strcmp(value, "DECRYPT") == 0)
632 if (strcmp(keyword, "CTSMode") == 0) {
633 cdat->cts_mode = value;
639 static int cipher_test_enc(EVP_TEST *t, int enc,
640 size_t out_misalign, size_t inp_misalign, int frag)
642 CIPHER_DATA *expected = t->data;
643 unsigned char *in, *expected_out, *tmp = NULL;
644 size_t in_len, out_len, donelen = 0;
645 int ok = 0, tmplen, chunklen, tmpflen, i;
646 EVP_CIPHER_CTX *ctx_base = NULL;
647 EVP_CIPHER_CTX *ctx = NULL;
649 t->err = "TEST_FAILURE";
650 if (!TEST_ptr(ctx_base = EVP_CIPHER_CTX_new()))
652 if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
654 EVP_CIPHER_CTX_set_flags(ctx_base, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
656 in = expected->plaintext;
657 in_len = expected->plaintext_len;
658 expected_out = expected->ciphertext;
659 out_len = expected->ciphertext_len;
661 in = expected->ciphertext;
662 in_len = expected->ciphertext_len;
663 expected_out = expected->plaintext;
664 out_len = expected->plaintext_len;
666 if (inp_misalign == (size_t)-1) {
667 /* Exercise in-place encryption */
668 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
671 in = memcpy(tmp + out_misalign, in, in_len);
673 inp_misalign += 16 - ((out_misalign + in_len) & 15);
675 * 'tmp' will store both output and copy of input. We make the copy
676 * of input to specifically aligned part of 'tmp'. So we just
677 * figured out how much padding would ensure the required alignment,
678 * now we allocate extended buffer and finally copy the input just
679 * past inp_misalign in expression below. Output will be written
680 * past out_misalign...
682 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
683 inp_misalign + in_len);
686 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
687 inp_misalign, in, in_len);
689 if (!EVP_CipherInit_ex(ctx_base, expected->cipher, NULL, NULL, NULL, enc)) {
690 t->err = "CIPHERINIT_ERROR";
693 if (expected->cts_mode != NULL) {
694 OSSL_PARAM params[2];
696 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE,
697 (char *)expected->cts_mode,
699 params[1] = OSSL_PARAM_construct_end();
700 if (!EVP_CIPHER_CTX_set_params(ctx_base, params)) {
701 t->err = "INVALID_CTS_MODE";
706 if (expected->aead) {
707 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_IVLEN,
708 expected->iv_len, 0)) {
709 t->err = "INVALID_IV_LENGTH";
712 } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx_base)) {
713 t->err = "INVALID_IV_LENGTH";
717 if (expected->aead) {
720 * If encrypting or OCB just set tag length initially, otherwise
721 * set tag length and value.
723 if (enc || expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late) {
724 t->err = "TAG_LENGTH_SET_ERROR";
727 t->err = "TAG_SET_ERROR";
730 if (tag || expected->aead != EVP_CIPH_GCM_MODE) {
731 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_TAG,
732 expected->tag_len, tag))
737 if (expected->rounds > 0) {
738 int rounds = (int)expected->rounds;
740 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC5_ROUNDS, rounds, NULL)) {
741 t->err = "INVALID_ROUNDS";
746 if (!EVP_CIPHER_CTX_set_key_length(ctx_base, expected->key_len)) {
747 t->err = "INVALID_KEY_LENGTH";
750 if (expected->key_bits > 0) {
751 int bits = (int)expected->key_bits;
753 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC2_KEY_BITS, bits, NULL)) {
754 t->err = "INVALID KEY BITS";
758 if (!EVP_CipherInit_ex(ctx_base, NULL, NULL, expected->key, expected->iv, -1)) {
759 t->err = "KEY_SET_ERROR";
763 /* Check that we get the same IV back */
764 if (expected->iv != NULL) {
765 /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
766 unsigned char iv[128];
767 if (!TEST_true(EVP_CIPHER_CTX_get_iv_state(ctx_base, iv, sizeof(iv)))
768 || ((EVP_CIPHER_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
769 && !TEST_mem_eq(expected->iv, expected->iv_len, iv,
770 expected->iv_len))) {
771 t->err = "INVALID_IV";
776 /* Test that the cipher dup functions correctly if it is supported */
777 if (EVP_CIPHER_CTX_copy(ctx, ctx_base)) {
778 EVP_CIPHER_CTX_free(ctx_base);
781 EVP_CIPHER_CTX_free(ctx);
785 if (expected->aead == EVP_CIPH_CCM_MODE) {
786 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
787 t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
791 if (expected->aad[0] != NULL) {
792 t->err = "AAD_SET_ERROR";
794 for (i = 0; expected->aad[i] != NULL; i++) {
795 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i],
796 expected->aad_len[i]))
801 * Supply the AAD in chunks less than the block size where possible
803 for (i = 0; expected->aad[i] != NULL; i++) {
804 if (expected->aad_len[i] > 0) {
805 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], 1))
809 if (expected->aad_len[i] > 2) {
810 if (!EVP_CipherUpdate(ctx, NULL, &chunklen,
811 expected->aad[i] + donelen,
812 expected->aad_len[i] - 2))
814 donelen += expected->aad_len[i] - 2;
816 if (expected->aad_len[i] > 1
817 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
818 expected->aad[i] + donelen, 1))
824 if (!enc && (expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late)) {
825 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
826 expected->tag_len, expected->tag)) {
827 t->err = "TAG_SET_ERROR";
832 EVP_CIPHER_CTX_set_padding(ctx, 0);
833 t->err = "CIPHERUPDATE_ERROR";
836 /* We supply the data all in one go */
837 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
840 /* Supply the data in chunks less than the block size where possible */
842 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
849 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
857 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
863 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
864 t->err = "CIPHERFINAL_ERROR";
867 if (!memory_err_compare(t, "VALUE_MISMATCH", expected_out, out_len,
868 tmp + out_misalign, tmplen + tmpflen))
870 if (enc && expected->aead) {
871 unsigned char rtag[16];
873 if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) {
874 t->err = "TAG_LENGTH_INTERNAL_ERROR";
877 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
878 expected->tag_len, rtag)) {
879 t->err = "TAG_RETRIEVE_ERROR";
882 if (!memory_err_compare(t, "TAG_VALUE_MISMATCH",
883 expected->tag, expected->tag_len,
884 rtag, expected->tag_len))
892 EVP_CIPHER_CTX_free(ctx_base);
893 EVP_CIPHER_CTX_free(ctx);
897 static int cipher_test_run(EVP_TEST *t)
899 CIPHER_DATA *cdat = t->data;
901 size_t out_misalign, inp_misalign;
907 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
908 /* IV is optional and usually omitted in wrap mode */
909 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
914 if (cdat->aead && !cdat->tag) {
918 for (out_misalign = 0; out_misalign <= 1;) {
919 static char aux_err[64];
920 t->aux_err = aux_err;
921 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
922 if (inp_misalign == (size_t)-1) {
923 /* kludge: inp_misalign == -1 means "exercise in-place" */
924 BIO_snprintf(aux_err, sizeof(aux_err),
925 "%s in-place, %sfragmented",
926 out_misalign ? "misaligned" : "aligned",
929 BIO_snprintf(aux_err, sizeof(aux_err),
930 "%s output and %s input, %sfragmented",
931 out_misalign ? "misaligned" : "aligned",
932 inp_misalign ? "misaligned" : "aligned",
936 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
937 /* Not fatal errors: return */
944 if (cdat->enc != 1) {
945 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
946 /* Not fatal errors: return */
955 if (out_misalign == 1 && frag == 0) {
957 * XTS, SIV, CCM and Wrap modes have special requirements about input
958 * lengths so we don't fragment for those
960 if (cdat->aead == EVP_CIPH_CCM_MODE
961 || ((EVP_CIPHER_flags(cdat->cipher) & EVP_CIPH_FLAG_CTS) != 0)
962 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_SIV_MODE
963 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
964 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
977 static const EVP_TEST_METHOD cipher_test_method = {
990 typedef struct mac_data_st {
991 /* MAC type in one form or another */
993 EVP_MAC *mac; /* for mac_test_run_mac */
994 int type; /* for mac_test_run_pkey */
995 /* Algorithm string for this MAC */
1004 unsigned char *input;
1006 /* Expected output */
1007 unsigned char *output;
1009 unsigned char *custom;
1011 /* MAC salt (blake2) */
1012 unsigned char *salt;
1014 /* Collection of controls */
1015 STACK_OF(OPENSSL_STRING) *controls;
1018 static int mac_test_init(EVP_TEST *t, const char *alg)
1020 EVP_MAC *mac = NULL;
1021 int type = NID_undef;
1024 if (is_mac_disabled(alg)) {
1025 TEST_info("skipping, '%s' is disabled", alg);
1029 if ((mac = EVP_MAC_fetch(libctx, alg, NULL)) == NULL) {
1031 * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
1032 * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
1033 * the EVP_PKEY method.
1035 size_t sz = strlen(alg);
1036 static const char epilogue[] = " by EVP_PKEY";
1038 if (sz >= sizeof(epilogue)
1039 && strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0)
1040 sz -= sizeof(epilogue) - 1;
1042 if (strncmp(alg, "HMAC", sz) == 0)
1043 type = EVP_PKEY_HMAC;
1044 else if (strncmp(alg, "CMAC", sz) == 0)
1045 type = EVP_PKEY_CMAC;
1046 else if (strncmp(alg, "Poly1305", sz) == 0)
1047 type = EVP_PKEY_POLY1305;
1048 else if (strncmp(alg, "SipHash", sz) == 0)
1049 type = EVP_PKEY_SIPHASH;
1054 mdat = OPENSSL_zalloc(sizeof(*mdat));
1056 mdat->mac_name = OPENSSL_strdup(alg);
1058 mdat->controls = sk_OPENSSL_STRING_new_null();
1063 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
1064 static void openssl_free(char *m)
1069 static void mac_test_cleanup(EVP_TEST *t)
1071 MAC_DATA *mdat = t->data;
1073 EVP_MAC_free(mdat->mac);
1074 OPENSSL_free(mdat->mac_name);
1075 sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free);
1076 OPENSSL_free(mdat->alg);
1077 OPENSSL_free(mdat->key);
1078 OPENSSL_free(mdat->iv);
1079 OPENSSL_free(mdat->custom);
1080 OPENSSL_free(mdat->salt);
1081 OPENSSL_free(mdat->input);
1082 OPENSSL_free(mdat->output);
1085 static int mac_test_parse(EVP_TEST *t,
1086 const char *keyword, const char *value)
1088 MAC_DATA *mdata = t->data;
1090 if (strcmp(keyword, "Key") == 0)
1091 return parse_bin(value, &mdata->key, &mdata->key_len);
1092 if (strcmp(keyword, "IV") == 0)
1093 return parse_bin(value, &mdata->iv, &mdata->iv_len);
1094 if (strcmp(keyword, "Custom") == 0)
1095 return parse_bin(value, &mdata->custom, &mdata->custom_len);
1096 if (strcmp(keyword, "Salt") == 0)
1097 return parse_bin(value, &mdata->salt, &mdata->salt_len);
1098 if (strcmp(keyword, "Algorithm") == 0) {
1099 mdata->alg = OPENSSL_strdup(value);
1104 if (strcmp(keyword, "Input") == 0)
1105 return parse_bin(value, &mdata->input, &mdata->input_len);
1106 if (strcmp(keyword, "Output") == 0)
1107 return parse_bin(value, &mdata->output, &mdata->output_len);
1108 if (strcmp(keyword, "Ctrl") == 0)
1109 return sk_OPENSSL_STRING_push(mdata->controls,
1110 OPENSSL_strdup(value)) != 0;
1114 static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1120 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1122 p = strchr(tmpval, ':');
1125 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1127 t->err = "PKEY_CTRL_INVALID";
1129 t->err = "PKEY_CTRL_ERROR";
1132 OPENSSL_free(tmpval);
1136 static int mac_test_run_pkey(EVP_TEST *t)
1138 MAC_DATA *expected = t->data;
1139 EVP_MD_CTX *mctx = NULL;
1140 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1141 EVP_PKEY *key = NULL;
1142 const char *mdname = NULL;
1143 EVP_CIPHER *cipher = NULL;
1144 unsigned char *got = NULL;
1148 if (expected->alg == NULL)
1149 TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type));
1151 TEST_info("Trying the EVP_PKEY %s test with %s",
1152 OBJ_nid2sn(expected->type), expected->alg);
1154 if (expected->type == EVP_PKEY_CMAC) {
1155 if (expected->alg != NULL && is_cipher_disabled(expected->alg)) {
1156 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
1161 if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, expected->alg, NULL))) {
1162 t->err = "MAC_KEY_CREATE_ERROR";
1165 key = EVP_PKEY_new_CMAC_key_ex(expected->key, expected->key_len,
1166 EVP_CIPHER_name(cipher), libctx, NULL);
1168 key = EVP_PKEY_new_raw_private_key_ex(libctx,
1169 OBJ_nid2sn(expected->type), NULL,
1170 expected->key, expected->key_len);
1173 t->err = "MAC_KEY_CREATE_ERROR";
1177 if (expected->type == EVP_PKEY_HMAC && expected->alg != NULL) {
1178 if (is_digest_disabled(expected->alg)) {
1179 TEST_info("skipping, HMAC '%s' is disabled", expected->alg);
1184 mdname = expected->alg;
1186 if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
1187 t->err = "INTERNAL_ERROR";
1190 if (!EVP_DigestSignInit_ex(mctx, &pctx, mdname, libctx, NULL, key)) {
1191 t->err = "DIGESTSIGNINIT_ERROR";
1194 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++)
1195 if (!mac_test_ctrl_pkey(t, pctx,
1196 sk_OPENSSL_STRING_value(expected->controls,
1198 t->err = "EVPPKEYCTXCTRL_ERROR";
1201 if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) {
1202 t->err = "DIGESTSIGNUPDATE_ERROR";
1205 if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) {
1206 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1209 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1210 t->err = "TEST_FAILURE";
1213 if (!EVP_DigestSignFinal(mctx, got, &got_len)
1214 || !memory_err_compare(t, "TEST_MAC_ERR",
1215 expected->output, expected->output_len,
1217 t->err = "TEST_MAC_ERR";
1222 EVP_CIPHER_free(cipher);
1223 EVP_MD_CTX_free(mctx);
1225 EVP_PKEY_CTX_free(genctx);
1230 static int mac_test_run_mac(EVP_TEST *t)
1232 MAC_DATA *expected = t->data;
1233 EVP_MAC_CTX *ctx = NULL;
1234 unsigned char *got = NULL;
1237 OSSL_PARAM params[21];
1238 size_t params_n = 0;
1239 size_t params_n_allocstart = 0;
1240 const OSSL_PARAM *defined_params =
1241 EVP_MAC_settable_ctx_params(expected->mac);
1243 if (expected->alg == NULL)
1244 TEST_info("Trying the EVP_MAC %s test", expected->mac_name);
1246 TEST_info("Trying the EVP_MAC %s test with %s",
1247 expected->mac_name, expected->alg);
1249 if (expected->alg != NULL) {
1251 * The underlying algorithm may be a cipher or a digest.
1252 * We don't know which it is, but we can ask the MAC what it
1253 * should be and bet on that.
1255 if (OSSL_PARAM_locate_const(defined_params,
1256 OSSL_MAC_PARAM_CIPHER) != NULL) {
1257 params[params_n++] =
1258 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
1260 } else if (OSSL_PARAM_locate_const(defined_params,
1261 OSSL_MAC_PARAM_DIGEST) != NULL) {
1262 params[params_n++] =
1263 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
1266 t->err = "MAC_BAD_PARAMS";
1270 if (expected->key != NULL)
1271 params[params_n++] =
1272 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
1275 if (expected->custom != NULL)
1276 params[params_n++] =
1277 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
1279 expected->custom_len);
1280 if (expected->salt != NULL)
1281 params[params_n++] =
1282 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT,
1284 expected->salt_len);
1285 if (expected->iv != NULL)
1286 params[params_n++] =
1287 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
1291 /* Unknown controls. They must match parameters that the MAC recognizes */
1292 if (params_n + sk_OPENSSL_STRING_num(expected->controls)
1293 >= OSSL_NELEM(params)) {
1294 t->err = "MAC_TOO_MANY_PARAMETERS";
1297 params_n_allocstart = params_n;
1298 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) {
1299 char *tmpkey, *tmpval;
1300 char *value = sk_OPENSSL_STRING_value(expected->controls, i);
1302 if (!TEST_ptr(tmpkey = OPENSSL_strdup(value))) {
1303 t->err = "MAC_PARAM_ERROR";
1306 tmpval = strchr(tmpkey, ':');
1311 || !OSSL_PARAM_allocate_from_text(¶ms[params_n],
1314 strlen(tmpval), NULL)) {
1315 OPENSSL_free(tmpkey);
1316 t->err = "MAC_PARAM_ERROR";
1321 OPENSSL_free(tmpkey);
1323 params[params_n] = OSSL_PARAM_construct_end();
1325 if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) {
1326 t->err = "MAC_CREATE_ERROR";
1330 if (!EVP_MAC_CTX_set_params(ctx, params)) {
1331 t->err = "MAC_BAD_PARAMS";
1334 if (!EVP_MAC_init(ctx)) {
1335 t->err = "MAC_INIT_ERROR";
1338 if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) {
1339 t->err = "MAC_UPDATE_ERROR";
1342 if (!EVP_MAC_final(ctx, NULL, &got_len, 0)) {
1343 t->err = "MAC_FINAL_LENGTH_ERROR";
1346 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1347 t->err = "TEST_FAILURE";
1350 if (!EVP_MAC_final(ctx, got, &got_len, got_len)
1351 || !memory_err_compare(t, "TEST_MAC_ERR",
1352 expected->output, expected->output_len,
1354 t->err = "TEST_MAC_ERR";
1359 while (params_n-- > params_n_allocstart) {
1360 OPENSSL_free(params[params_n].data);
1362 EVP_MAC_CTX_free(ctx);
1367 static int mac_test_run(EVP_TEST *t)
1369 MAC_DATA *expected = t->data;
1371 if (expected->mac != NULL)
1372 return mac_test_run_mac(t);
1373 return mac_test_run_pkey(t);
1376 static const EVP_TEST_METHOD mac_test_method = {
1387 ** These are all very similar and share much common code.
1390 typedef struct pkey_data_st {
1391 /* Context for this operation */
1393 /* Key operation to perform */
1394 int (*keyop) (EVP_PKEY_CTX *ctx,
1395 unsigned char *sig, size_t *siglen,
1396 const unsigned char *tbs, size_t tbslen);
1398 unsigned char *input;
1400 /* Expected output */
1401 unsigned char *output;
1406 * Perform public key operation setup: lookup key, allocated ctx and call
1407 * the appropriate initialisation function
1409 static int pkey_test_init(EVP_TEST *t, const char *name,
1411 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1412 int (*keyop)(EVP_PKEY_CTX *ctx,
1413 unsigned char *sig, size_t *siglen,
1414 const unsigned char *tbs,
1418 EVP_PKEY *pkey = NULL;
1422 rv = find_key(&pkey, name, public_keys);
1424 rv = find_key(&pkey, name, private_keys);
1425 if (rv == 0 || pkey == NULL) {
1426 TEST_info("skipping, key '%s' is disabled", name);
1431 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) {
1432 EVP_PKEY_free(pkey);
1435 kdata->keyop = keyop;
1436 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL))) {
1437 EVP_PKEY_free(pkey);
1438 OPENSSL_free(kdata);
1441 if (keyopinit(kdata->ctx) <= 0)
1442 t->err = "KEYOP_INIT_ERROR";
1447 static void pkey_test_cleanup(EVP_TEST *t)
1449 PKEY_DATA *kdata = t->data;
1451 OPENSSL_free(kdata->input);
1452 OPENSSL_free(kdata->output);
1453 EVP_PKEY_CTX_free(kdata->ctx);
1456 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1462 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1464 p = strchr(tmpval, ':');
1467 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1469 t->err = "PKEY_CTRL_INVALID";
1471 } else if (p != NULL && rv <= 0) {
1472 if (is_digest_disabled(p) || is_cipher_disabled(p)) {
1473 TEST_info("skipping, '%s' is disabled", p);
1477 t->err = "PKEY_CTRL_ERROR";
1481 OPENSSL_free(tmpval);
1485 static int pkey_test_parse(EVP_TEST *t,
1486 const char *keyword, const char *value)
1488 PKEY_DATA *kdata = t->data;
1489 if (strcmp(keyword, "Input") == 0)
1490 return parse_bin(value, &kdata->input, &kdata->input_len);
1491 if (strcmp(keyword, "Output") == 0)
1492 return parse_bin(value, &kdata->output, &kdata->output_len);
1493 if (strcmp(keyword, "Ctrl") == 0)
1494 return pkey_test_ctrl(t, kdata->ctx, value);
1498 static int pkey_test_run(EVP_TEST *t)
1500 PKEY_DATA *expected = t->data;
1501 unsigned char *got = NULL;
1503 EVP_PKEY_CTX *copy = NULL;
1505 if (expected->keyop(expected->ctx, NULL, &got_len,
1506 expected->input, expected->input_len) <= 0
1507 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1508 t->err = "KEYOP_LENGTH_ERROR";
1511 if (expected->keyop(expected->ctx, got, &got_len,
1512 expected->input, expected->input_len) <= 0) {
1513 t->err = "KEYOP_ERROR";
1516 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1517 expected->output, expected->output_len,
1525 /* Repeat the test on a copy. */
1526 if (!TEST_ptr(copy = EVP_PKEY_CTX_dup(expected->ctx))) {
1527 t->err = "INTERNAL_ERROR";
1530 if (expected->keyop(copy, NULL, &got_len, expected->input,
1531 expected->input_len) <= 0
1532 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1533 t->err = "KEYOP_LENGTH_ERROR";
1536 if (expected->keyop(copy, got, &got_len, expected->input,
1537 expected->input_len) <= 0) {
1538 t->err = "KEYOP_ERROR";
1541 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1542 expected->output, expected->output_len,
1548 EVP_PKEY_CTX_free(copy);
1552 static int sign_test_init(EVP_TEST *t, const char *name)
1554 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1557 static const EVP_TEST_METHOD psign_test_method = {
1565 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1567 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1568 EVP_PKEY_verify_recover);
1571 static const EVP_TEST_METHOD pverify_recover_test_method = {
1573 verify_recover_test_init,
1579 static int decrypt_test_init(EVP_TEST *t, const char *name)
1581 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1585 static const EVP_TEST_METHOD pdecrypt_test_method = {
1593 static int verify_test_init(EVP_TEST *t, const char *name)
1595 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1598 static int verify_test_run(EVP_TEST *t)
1600 PKEY_DATA *kdata = t->data;
1602 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1603 kdata->input, kdata->input_len) <= 0)
1604 t->err = "VERIFY_ERROR";
1608 static const EVP_TEST_METHOD pverify_test_method = {
1616 static int pderive_test_init(EVP_TEST *t, const char *name)
1618 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1621 static int pderive_test_parse(EVP_TEST *t,
1622 const char *keyword, const char *value)
1624 PKEY_DATA *kdata = t->data;
1626 if (strcmp(keyword, "PeerKey") == 0) {
1628 if (find_key(&peer, value, public_keys) == 0)
1630 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0) {
1631 t->err = "DERIVE_SET_PEER_ERROR";
1637 if (strcmp(keyword, "SharedSecret") == 0)
1638 return parse_bin(value, &kdata->output, &kdata->output_len);
1639 if (strcmp(keyword, "Ctrl") == 0)
1640 return pkey_test_ctrl(t, kdata->ctx, value);
1644 static int pderive_test_run(EVP_TEST *t)
1646 PKEY_DATA *expected = t->data;
1647 unsigned char *got = NULL;
1650 if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) {
1651 t->err = "DERIVE_ERROR";
1654 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1655 t->err = "DERIVE_ERROR";
1658 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
1659 t->err = "DERIVE_ERROR";
1662 if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH",
1663 expected->output, expected->output_len,
1673 static const EVP_TEST_METHOD pderive_test_method = {
1686 typedef enum pbe_type_enum {
1687 PBE_TYPE_INVALID = 0,
1688 PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
1691 typedef struct pbe_data_st {
1693 /* scrypt parameters */
1694 uint64_t N, r, p, maxmem;
1695 /* PKCS#12 parameters */
1699 unsigned char *pass;
1702 unsigned char *salt;
1704 /* Expected output */
1709 #ifndef OPENSSL_NO_SCRYPT
1710 /* Parse unsigned decimal 64 bit integer value */
1711 static int parse_uint64(const char *value, uint64_t *pr)
1713 const char *p = value;
1715 if (!TEST_true(*p)) {
1716 TEST_info("Invalid empty integer value");
1719 for (*pr = 0; *p; ) {
1720 if (*pr > UINT64_MAX / 10) {
1721 TEST_error("Integer overflow in string %s", value);
1725 if (!TEST_true(isdigit((unsigned char)*p))) {
1726 TEST_error("Invalid character in string %s", value);
1735 static int scrypt_test_parse(EVP_TEST *t,
1736 const char *keyword, const char *value)
1738 PBE_DATA *pdata = t->data;
1740 if (strcmp(keyword, "N") == 0)
1741 return parse_uint64(value, &pdata->N);
1742 if (strcmp(keyword, "p") == 0)
1743 return parse_uint64(value, &pdata->p);
1744 if (strcmp(keyword, "r") == 0)
1745 return parse_uint64(value, &pdata->r);
1746 if (strcmp(keyword, "maxmem") == 0)
1747 return parse_uint64(value, &pdata->maxmem);
1752 static int pbkdf2_test_parse(EVP_TEST *t,
1753 const char *keyword, const char *value)
1755 PBE_DATA *pdata = t->data;
1757 if (strcmp(keyword, "iter") == 0) {
1758 pdata->iter = atoi(value);
1759 if (pdata->iter <= 0)
1763 if (strcmp(keyword, "MD") == 0) {
1764 pdata->md = EVP_get_digestbyname(value);
1765 if (pdata->md == NULL)
1772 static int pkcs12_test_parse(EVP_TEST *t,
1773 const char *keyword, const char *value)
1775 PBE_DATA *pdata = t->data;
1777 if (strcmp(keyword, "id") == 0) {
1778 pdata->id = atoi(value);
1783 return pbkdf2_test_parse(t, keyword, value);
1786 static int pbe_test_init(EVP_TEST *t, const char *alg)
1789 PBE_TYPE pbe_type = PBE_TYPE_INVALID;
1791 if (is_kdf_disabled(alg)) {
1792 TEST_info("skipping, '%s' is disabled", alg);
1796 if (strcmp(alg, "scrypt") == 0) {
1797 pbe_type = PBE_TYPE_SCRYPT;
1798 } else if (strcmp(alg, "pbkdf2") == 0) {
1799 pbe_type = PBE_TYPE_PBKDF2;
1800 } else if (strcmp(alg, "pkcs12") == 0) {
1801 pbe_type = PBE_TYPE_PKCS12;
1803 TEST_error("Unknown pbe algorithm %s", alg);
1805 pdat = OPENSSL_zalloc(sizeof(*pdat));
1806 pdat->pbe_type = pbe_type;
1811 static void pbe_test_cleanup(EVP_TEST *t)
1813 PBE_DATA *pdat = t->data;
1815 OPENSSL_free(pdat->pass);
1816 OPENSSL_free(pdat->salt);
1817 OPENSSL_free(pdat->key);
1820 static int pbe_test_parse(EVP_TEST *t,
1821 const char *keyword, const char *value)
1823 PBE_DATA *pdata = t->data;
1825 if (strcmp(keyword, "Password") == 0)
1826 return parse_bin(value, &pdata->pass, &pdata->pass_len);
1827 if (strcmp(keyword, "Salt") == 0)
1828 return parse_bin(value, &pdata->salt, &pdata->salt_len);
1829 if (strcmp(keyword, "Key") == 0)
1830 return parse_bin(value, &pdata->key, &pdata->key_len);
1831 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
1832 return pbkdf2_test_parse(t, keyword, value);
1833 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1834 return pkcs12_test_parse(t, keyword, value);
1835 #ifndef OPENSSL_NO_SCRYPT
1836 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1837 return scrypt_test_parse(t, keyword, value);
1842 static int pbe_test_run(EVP_TEST *t)
1844 PBE_DATA *expected = t->data;
1846 EVP_MD *fetched_digest = NULL;
1847 OSSL_LIB_CTX *save_libctx;
1849 save_libctx = OSSL_LIB_CTX_set0_default(libctx);
1851 if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
1852 t->err = "INTERNAL_ERROR";
1855 if (expected->pbe_type == PBE_TYPE_PBKDF2) {
1856 if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
1857 expected->salt, expected->salt_len,
1858 expected->iter, expected->md,
1859 expected->key_len, key) == 0) {
1860 t->err = "PBKDF2_ERROR";
1863 #ifndef OPENSSL_NO_SCRYPT
1864 } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
1865 if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
1866 expected->salt, expected->salt_len,
1867 expected->N, expected->r, expected->p,
1868 expected->maxmem, key, expected->key_len) == 0) {
1869 t->err = "SCRYPT_ERROR";
1873 } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
1874 fetched_digest = EVP_MD_fetch(libctx, EVP_MD_name(expected->md), NULL);
1875 if (fetched_digest == NULL) {
1876 t->err = "PKCS12_ERROR";
1879 if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
1880 expected->salt, expected->salt_len,
1881 expected->id, expected->iter, expected->key_len,
1882 key, fetched_digest) == 0) {
1883 t->err = "PKCS12_ERROR";
1887 if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len,
1888 key, expected->key_len))
1893 EVP_MD_free(fetched_digest);
1895 OSSL_LIB_CTX_set0_default(save_libctx);
1899 static const EVP_TEST_METHOD pbe_test_method = {
1913 BASE64_CANONICAL_ENCODING = 0,
1914 BASE64_VALID_ENCODING = 1,
1915 BASE64_INVALID_ENCODING = 2
1916 } base64_encoding_type;
1918 typedef struct encode_data_st {
1919 /* Input to encoding */
1920 unsigned char *input;
1922 /* Expected output */
1923 unsigned char *output;
1925 base64_encoding_type encoding;
1928 static int encode_test_init(EVP_TEST *t, const char *encoding)
1932 if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
1934 if (strcmp(encoding, "canonical") == 0) {
1935 edata->encoding = BASE64_CANONICAL_ENCODING;
1936 } else if (strcmp(encoding, "valid") == 0) {
1937 edata->encoding = BASE64_VALID_ENCODING;
1938 } else if (strcmp(encoding, "invalid") == 0) {
1939 edata->encoding = BASE64_INVALID_ENCODING;
1940 if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
1943 TEST_error("Bad encoding: %s."
1944 " Should be one of {canonical, valid, invalid}",
1951 OPENSSL_free(edata);
1955 static void encode_test_cleanup(EVP_TEST *t)
1957 ENCODE_DATA *edata = t->data;
1959 OPENSSL_free(edata->input);
1960 OPENSSL_free(edata->output);
1961 memset(edata, 0, sizeof(*edata));
1964 static int encode_test_parse(EVP_TEST *t,
1965 const char *keyword, const char *value)
1967 ENCODE_DATA *edata = t->data;
1969 if (strcmp(keyword, "Input") == 0)
1970 return parse_bin(value, &edata->input, &edata->input_len);
1971 if (strcmp(keyword, "Output") == 0)
1972 return parse_bin(value, &edata->output, &edata->output_len);
1976 static int encode_test_run(EVP_TEST *t)
1978 ENCODE_DATA *expected = t->data;
1979 unsigned char *encode_out = NULL, *decode_out = NULL;
1980 int output_len, chunk_len;
1981 EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL;
1983 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
1984 t->err = "INTERNAL_ERROR";
1988 if (expected->encoding == BASE64_CANONICAL_ENCODING) {
1990 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
1991 || !TEST_ptr(encode_out =
1992 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
1995 EVP_EncodeInit(encode_ctx);
1996 if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
1997 expected->input, expected->input_len)))
2000 output_len = chunk_len;
2002 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
2003 output_len += chunk_len;
2005 if (!memory_err_compare(t, "BAD_ENCODING",
2006 expected->output, expected->output_len,
2007 encode_out, output_len))
2011 if (!TEST_ptr(decode_out =
2012 OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
2015 EVP_DecodeInit(decode_ctx);
2016 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
2017 expected->output_len) < 0) {
2018 t->err = "DECODE_ERROR";
2021 output_len = chunk_len;
2023 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
2024 t->err = "DECODE_ERROR";
2027 output_len += chunk_len;
2029 if (expected->encoding != BASE64_INVALID_ENCODING
2030 && !memory_err_compare(t, "BAD_DECODING",
2031 expected->input, expected->input_len,
2032 decode_out, output_len)) {
2033 t->err = "BAD_DECODING";
2039 OPENSSL_free(encode_out);
2040 OPENSSL_free(decode_out);
2041 EVP_ENCODE_CTX_free(decode_ctx);
2042 EVP_ENCODE_CTX_free(encode_ctx);
2046 static const EVP_TEST_METHOD encode_test_method = {
2049 encode_test_cleanup,
2058 #define MAX_RAND_REPEATS 15
2060 typedef struct rand_data_pass_st {
2061 unsigned char *entropy;
2062 unsigned char *reseed_entropy;
2063 unsigned char *nonce;
2064 unsigned char *pers;
2065 unsigned char *reseed_addin;
2066 unsigned char *addinA;
2067 unsigned char *addinB;
2068 unsigned char *pr_entropyA;
2069 unsigned char *pr_entropyB;
2070 unsigned char *output;
2071 size_t entropy_len, nonce_len, pers_len, addinA_len, addinB_len,
2072 pr_entropyA_len, pr_entropyB_len, output_len, reseed_entropy_len,
2076 typedef struct rand_data_st {
2077 /* Context for this operation */
2079 EVP_RAND_CTX *parent;
2081 int prediction_resistance;
2083 unsigned int generate_bits;
2087 /* Expected output */
2088 RAND_DATA_PASS data[MAX_RAND_REPEATS];
2091 static int rand_test_init(EVP_TEST *t, const char *name)
2095 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
2096 unsigned int strength = 256;
2098 if (!TEST_ptr(rdata = OPENSSL_zalloc(sizeof(*rdata))))
2101 /* TEST-RAND is available in the FIPS provider but not with "fips=yes" */
2102 rand = EVP_RAND_fetch(libctx, "TEST-RAND", "-fips");
2105 rdata->parent = EVP_RAND_CTX_new(rand, NULL);
2106 EVP_RAND_free(rand);
2107 if (rdata->parent == NULL)
2110 *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength);
2111 if (!EVP_RAND_set_ctx_params(rdata->parent, params))
2114 rand = EVP_RAND_fetch(libctx, name, NULL);
2117 rdata->ctx = EVP_RAND_CTX_new(rand, rdata->parent);
2118 EVP_RAND_free(rand);
2119 if (rdata->ctx == NULL)
2126 EVP_RAND_CTX_free(rdata->parent);
2127 OPENSSL_free(rdata);
2131 static void rand_test_cleanup(EVP_TEST *t)
2133 RAND_DATA *rdata = t->data;
2136 OPENSSL_free(rdata->cipher);
2137 OPENSSL_free(rdata->digest);
2139 for (i = 0; i <= rdata->n; i++) {
2140 OPENSSL_free(rdata->data[i].entropy);
2141 OPENSSL_free(rdata->data[i].reseed_entropy);
2142 OPENSSL_free(rdata->data[i].nonce);
2143 OPENSSL_free(rdata->data[i].pers);
2144 OPENSSL_free(rdata->data[i].reseed_addin);
2145 OPENSSL_free(rdata->data[i].addinA);
2146 OPENSSL_free(rdata->data[i].addinB);
2147 OPENSSL_free(rdata->data[i].pr_entropyA);
2148 OPENSSL_free(rdata->data[i].pr_entropyB);
2149 OPENSSL_free(rdata->data[i].output);
2151 EVP_RAND_CTX_free(rdata->ctx);
2152 EVP_RAND_CTX_free(rdata->parent);
2155 static int rand_test_parse(EVP_TEST *t,
2156 const char *keyword, const char *value)
2158 RAND_DATA *rdata = t->data;
2159 RAND_DATA_PASS *item;
2163 if ((p = strchr(keyword, '.')) != NULL) {
2165 if (n >= MAX_RAND_REPEATS)
2169 item = rdata->data + n;
2170 if (strncmp(keyword, "Entropy.", sizeof("Entropy")) == 0)
2171 return parse_bin(value, &item->entropy, &item->entropy_len);
2172 if (strncmp(keyword, "ReseedEntropy.", sizeof("ReseedEntropy")) == 0)
2173 return parse_bin(value, &item->reseed_entropy,
2174 &item->reseed_entropy_len);
2175 if (strncmp(keyword, "Nonce.", sizeof("Nonce")) == 0)
2176 return parse_bin(value, &item->nonce, &item->nonce_len);
2177 if (strncmp(keyword, "PersonalisationString.",
2178 sizeof("PersonalisationString")) == 0)
2179 return parse_bin(value, &item->pers, &item->pers_len);
2180 if (strncmp(keyword, "ReseedAdditionalInput.",
2181 sizeof("ReseedAdditionalInput")) == 0)
2182 return parse_bin(value, &item->reseed_addin,
2183 &item->reseed_addin_len);
2184 if (strncmp(keyword, "AdditionalInputA.",
2185 sizeof("AdditionalInputA")) == 0)
2186 return parse_bin(value, &item->addinA, &item->addinA_len);
2187 if (strncmp(keyword, "AdditionalInputB.",
2188 sizeof("AdditionalInputB")) == 0)
2189 return parse_bin(value, &item->addinB, &item->addinB_len);
2190 if (strncmp(keyword, "EntropyPredictionResistanceA.",
2191 sizeof("EntropyPredictionResistanceA")) == 0)
2192 return parse_bin(value, &item->pr_entropyA, &item->pr_entropyA_len);
2193 if (strncmp(keyword, "EntropyPredictionResistanceB.",
2194 sizeof("EntropyPredictionResistanceB")) == 0)
2195 return parse_bin(value, &item->pr_entropyB, &item->pr_entropyB_len);
2196 if (strncmp(keyword, "Output.", sizeof("Output")) == 0)
2197 return parse_bin(value, &item->output, &item->output_len);
2199 if (strcmp(keyword, "Cipher") == 0)
2200 return TEST_ptr(rdata->cipher = OPENSSL_strdup(value));
2201 if (strcmp(keyword, "Digest") == 0)
2202 return TEST_ptr(rdata->digest = OPENSSL_strdup(value));
2203 if (strcmp(keyword, "DerivationFunction") == 0) {
2204 rdata->use_df = atoi(value) != 0;
2207 if (strcmp(keyword, "GenerateBits") == 0) {
2208 if ((n = atoi(value)) <= 0 || n % 8 != 0)
2210 rdata->generate_bits = (unsigned int)n;
2213 if (strcmp(keyword, "PredictionResistance") == 0) {
2214 rdata->prediction_resistance = atoi(value) != 0;
2221 static int rand_test_run(EVP_TEST *t)
2223 RAND_DATA *expected = t->data;
2224 RAND_DATA_PASS *item;
2226 size_t got_len = expected->generate_bits / 8;
2227 OSSL_PARAM params[5], *p = params;
2228 int i = -1, ret = 0;
2229 unsigned int strength;
2232 if (!TEST_ptr(got = OPENSSL_malloc(got_len)))
2235 *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &expected->use_df);
2236 if (expected->cipher != NULL)
2237 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
2238 expected->cipher, 0);
2239 if (expected->digest != NULL)
2240 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
2241 expected->digest, 0);
2242 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC, "HMAC", 0);
2243 *p = OSSL_PARAM_construct_end();
2244 if (!TEST_true(EVP_RAND_set_ctx_params(expected->ctx, params)))
2247 strength = EVP_RAND_strength(expected->ctx);
2248 for (i = 0; i <= expected->n; i++) {
2249 item = expected->data + i;
2252 z = item->entropy != NULL ? item->entropy : (unsigned char *)"";
2253 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
2254 z, item->entropy_len);
2255 z = item->nonce != NULL ? item->nonce : (unsigned char *)"";
2256 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
2257 z, item->nonce_len);
2258 *p = OSSL_PARAM_construct_end();
2259 if (!TEST_true(EVP_RAND_set_ctx_params(expected->parent, params))
2260 || !TEST_true(EVP_RAND_instantiate(expected->parent, strength,
2264 z = item->pers != NULL ? item->pers : (unsigned char *)"";
2265 if (!TEST_true(EVP_RAND_instantiate
2266 (expected->ctx, strength,
2267 expected->prediction_resistance, z,
2271 if (item->reseed_entropy != NULL) {
2272 params[0] = OSSL_PARAM_construct_octet_string
2273 (OSSL_RAND_PARAM_TEST_ENTROPY, item->reseed_entropy,
2274 item->reseed_entropy_len);
2275 params[1] = OSSL_PARAM_construct_end();
2276 if (!TEST_true(EVP_RAND_set_ctx_params(expected->parent, params)))
2279 if (!TEST_true(EVP_RAND_reseed
2280 (expected->ctx, expected->prediction_resistance,
2281 NULL, 0, item->reseed_addin,
2282 item->reseed_addin_len)))
2285 if (item->pr_entropyA != NULL) {
2286 params[0] = OSSL_PARAM_construct_octet_string
2287 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyA,
2288 item->pr_entropyA_len);
2289 params[1] = OSSL_PARAM_construct_end();
2290 if (!TEST_true(EVP_RAND_set_ctx_params(expected->parent, params)))
2293 if (!TEST_true(EVP_RAND_generate
2294 (expected->ctx, got, got_len,
2295 strength, expected->prediction_resistance,
2296 item->addinA, item->addinA_len)))
2299 if (item->pr_entropyB != NULL) {
2300 params[0] = OSSL_PARAM_construct_octet_string
2301 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyB,
2302 item->pr_entropyB_len);
2303 params[1] = OSSL_PARAM_construct_end();
2304 if (!TEST_true(EVP_RAND_set_ctx_params(expected->parent, params)))
2307 if (!TEST_true(EVP_RAND_generate
2308 (expected->ctx, got, got_len,
2309 strength, expected->prediction_resistance,
2310 item->addinB, item->addinB_len)))
2312 if (!TEST_mem_eq(got, got_len, item->output, item->output_len))
2314 if (!TEST_true(EVP_RAND_uninstantiate(expected->ctx))
2315 || !TEST_true(EVP_RAND_uninstantiate(expected->parent))
2316 || !TEST_true(EVP_RAND_verify_zeroization(expected->ctx))
2317 || !TEST_int_eq(EVP_RAND_state(expected->ctx),
2318 EVP_RAND_STATE_UNINITIALISED))
2325 if (ret == 0 && i >= 0)
2326 TEST_info("Error in test case %d of %d\n", i, expected->n + 1);
2331 static const EVP_TEST_METHOD rand_test_method = {
2343 typedef struct kdf_data_st {
2344 /* Context for this operation */
2346 /* Expected output */
2347 unsigned char *output;
2349 OSSL_PARAM params[20];
2354 * Perform public key operation setup: lookup key, allocated ctx and call
2355 * the appropriate initialisation function
2357 static int kdf_test_init(EVP_TEST *t, const char *name)
2362 if (is_kdf_disabled(name)) {
2363 TEST_info("skipping, '%s' is disabled", name);
2368 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2370 kdata->p = kdata->params;
2371 *kdata->p = OSSL_PARAM_construct_end();
2373 kdf = EVP_KDF_fetch(libctx, name, NULL);
2375 OPENSSL_free(kdata);
2378 kdata->ctx = EVP_KDF_CTX_new(kdf);
2380 if (kdata->ctx == NULL) {
2381 OPENSSL_free(kdata);
2388 static void kdf_test_cleanup(EVP_TEST *t)
2390 KDF_DATA *kdata = t->data;
2393 for (p = kdata->params; p->key != NULL; p++)
2394 OPENSSL_free(p->data);
2395 OPENSSL_free(kdata->output);
2396 EVP_KDF_CTX_free(kdata->ctx);
2399 static int kdf_test_ctrl(EVP_TEST *t, EVP_KDF_CTX *kctx,
2402 KDF_DATA *kdata = t->data;
2405 const OSSL_PARAM *defs = EVP_KDF_settable_ctx_params(EVP_KDF_CTX_kdf(kctx));
2407 if (!TEST_ptr(name = OPENSSL_strdup(value)))
2409 p = strchr(name, ':');
2413 rv = OSSL_PARAM_allocate_from_text(kdata->p, defs, name, p,
2414 p != NULL ? strlen(p) : 0, NULL);
2415 *++kdata->p = OSSL_PARAM_construct_end();
2417 t->err = "KDF_PARAM_ERROR";
2421 if (p != NULL && strcmp(name, "digest") == 0) {
2422 if (is_digest_disabled(p)) {
2423 TEST_info("skipping, '%s' is disabled", p);
2428 && (strcmp(name, "cipher") == 0
2429 || strcmp(name, "cekalg") == 0)
2430 && is_cipher_disabled(p)) {
2431 TEST_info("skipping, '%s' is disabled", p);
2438 static int kdf_test_parse(EVP_TEST *t,
2439 const char *keyword, const char *value)
2441 KDF_DATA *kdata = t->data;
2443 if (strcmp(keyword, "Output") == 0)
2444 return parse_bin(value, &kdata->output, &kdata->output_len);
2445 if (strncmp(keyword, "Ctrl", 4) == 0)
2446 return kdf_test_ctrl(t, kdata->ctx, value);
2450 static int kdf_test_run(EVP_TEST *t)
2452 KDF_DATA *expected = t->data;
2453 unsigned char *got = NULL;
2454 size_t got_len = expected->output_len;
2456 if (!EVP_KDF_CTX_set_params(expected->ctx, expected->params)) {
2457 t->err = "KDF_CTRL_ERROR";
2460 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2461 t->err = "INTERNAL_ERROR";
2464 if (EVP_KDF_derive(expected->ctx, got, got_len) <= 0) {
2465 t->err = "KDF_DERIVE_ERROR";
2468 if (!memory_err_compare(t, "KDF_MISMATCH",
2469 expected->output, expected->output_len,
2480 static const EVP_TEST_METHOD kdf_test_method = {
2492 typedef struct pkey_kdf_data_st {
2493 /* Context for this operation */
2495 /* Expected output */
2496 unsigned char *output;
2501 * Perform public key operation setup: lookup key, allocated ctx and call
2502 * the appropriate initialisation function
2504 static int pkey_kdf_test_init(EVP_TEST *t, const char *name)
2506 PKEY_KDF_DATA *kdata = NULL;
2508 if (is_kdf_disabled(name)) {
2509 TEST_info("skipping, '%s' is disabled", name);
2514 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2517 kdata->ctx = EVP_PKEY_CTX_new_from_name(libctx, name, NULL);
2518 if (kdata->ctx == NULL
2519 || EVP_PKEY_derive_init(kdata->ctx) <= 0)
2525 EVP_PKEY_CTX_free(kdata->ctx);
2526 OPENSSL_free(kdata);
2530 static void pkey_kdf_test_cleanup(EVP_TEST *t)
2532 PKEY_KDF_DATA *kdata = t->data;
2534 OPENSSL_free(kdata->output);
2535 EVP_PKEY_CTX_free(kdata->ctx);
2538 static int pkey_kdf_test_parse(EVP_TEST *t,
2539 const char *keyword, const char *value)
2541 PKEY_KDF_DATA *kdata = t->data;
2543 if (strcmp(keyword, "Output") == 0)
2544 return parse_bin(value, &kdata->output, &kdata->output_len);
2545 if (strncmp(keyword, "Ctrl", 4) == 0)
2546 return pkey_test_ctrl(t, kdata->ctx, value);
2550 static int pkey_kdf_test_run(EVP_TEST *t)
2552 PKEY_KDF_DATA *expected = t->data;
2553 unsigned char *got = NULL;
2554 size_t got_len = expected->output_len;
2556 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2557 t->err = "INTERNAL_ERROR";
2560 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
2561 t->err = "KDF_DERIVE_ERROR";
2564 if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
2565 t->err = "KDF_MISMATCH";
2575 static const EVP_TEST_METHOD pkey_kdf_test_method = {
2578 pkey_kdf_test_cleanup,
2579 pkey_kdf_test_parse,
2587 typedef struct keypair_test_data_st {
2590 } KEYPAIR_TEST_DATA;
2592 static int keypair_test_init(EVP_TEST *t, const char *pair)
2594 KEYPAIR_TEST_DATA *data;
2596 EVP_PKEY *pk = NULL, *pubk = NULL;
2597 char *pub, *priv = NULL;
2599 /* Split private and public names. */
2600 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
2601 || !TEST_ptr(pub = strchr(priv, ':'))) {
2602 t->err = "PARSING_ERROR";
2607 if (!TEST_true(find_key(&pk, priv, private_keys))) {
2608 TEST_info("Can't find private key: %s", priv);
2609 t->err = "MISSING_PRIVATE_KEY";
2612 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
2613 TEST_info("Can't find public key: %s", pub);
2614 t->err = "MISSING_PUBLIC_KEY";
2618 if (pk == NULL && pubk == NULL) {
2619 /* Both keys are listed but unsupported: skip this test */
2625 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2638 static void keypair_test_cleanup(EVP_TEST *t)
2640 OPENSSL_free(t->data);
2645 * For tests that do not accept any custom keywords.
2647 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
2652 static int keypair_test_run(EVP_TEST *t)
2655 const KEYPAIR_TEST_DATA *pair = t->data;
2657 if (pair->privk == NULL || pair->pubk == NULL) {
2659 * this can only happen if only one of the keys is not set
2660 * which means that one of them was unsupported while the
2661 * other isn't: hence a key type mismatch.
2663 t->err = "KEYPAIR_TYPE_MISMATCH";
2668 if ((rv = EVP_PKEY_eq(pair->privk, pair->pubk)) != 1 ) {
2670 t->err = "KEYPAIR_MISMATCH";
2671 } else if ( -1 == rv ) {
2672 t->err = "KEYPAIR_TYPE_MISMATCH";
2673 } else if ( -2 == rv ) {
2674 t->err = "UNSUPPORTED_KEY_COMPARISON";
2676 TEST_error("Unexpected error in key comparison");
2691 static const EVP_TEST_METHOD keypair_test_method = {
2694 keypair_test_cleanup,
2703 typedef struct keygen_test_data_st {
2704 EVP_PKEY_CTX *genctx; /* Keygen context to use */
2705 char *keyname; /* Key name to store key or NULL */
2708 static int keygen_test_init(EVP_TEST *t, const char *alg)
2710 KEYGEN_TEST_DATA *data;
2711 EVP_PKEY_CTX *genctx;
2712 int nid = OBJ_sn2nid(alg);
2714 if (nid == NID_undef) {
2715 nid = OBJ_ln2nid(alg);
2716 if (nid == NID_undef)
2720 if (is_pkey_disabled(alg)) {
2724 if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_from_name(libctx, alg, NULL)))
2727 if (EVP_PKEY_keygen_init(genctx) <= 0) {
2728 t->err = "KEYGEN_INIT_ERROR";
2732 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2734 data->genctx = genctx;
2735 data->keyname = NULL;
2741 EVP_PKEY_CTX_free(genctx);
2745 static void keygen_test_cleanup(EVP_TEST *t)
2747 KEYGEN_TEST_DATA *keygen = t->data;
2749 EVP_PKEY_CTX_free(keygen->genctx);
2750 OPENSSL_free(keygen->keyname);
2751 OPENSSL_free(t->data);
2755 static int keygen_test_parse(EVP_TEST *t,
2756 const char *keyword, const char *value)
2758 KEYGEN_TEST_DATA *keygen = t->data;
2760 if (strcmp(keyword, "KeyName") == 0)
2761 return TEST_ptr(keygen->keyname = OPENSSL_strdup(value));
2762 if (strcmp(keyword, "Ctrl") == 0)
2763 return pkey_test_ctrl(t, keygen->genctx, value);
2767 static int keygen_test_run(EVP_TEST *t)
2769 KEYGEN_TEST_DATA *keygen = t->data;
2770 EVP_PKEY *pkey = NULL;
2773 if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) {
2774 t->err = "KEYGEN_GENERATE_ERROR";
2778 if (!evp_pkey_is_provided(pkey)) {
2779 TEST_info("Warning: legacy key generated %s", keygen->keyname);
2782 if (keygen->keyname != NULL) {
2786 if (find_key(NULL, keygen->keyname, private_keys)) {
2787 TEST_info("Duplicate key %s", keygen->keyname);
2791 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
2793 key->name = keygen->keyname;
2794 keygen->keyname = NULL;
2796 key->next = private_keys;
2800 EVP_PKEY_free(pkey);
2809 static const EVP_TEST_METHOD keygen_test_method = {
2812 keygen_test_cleanup,
2818 ** DIGEST SIGN+VERIFY TESTS
2822 int is_verify; /* Set to 1 if verifying */
2823 int is_oneshot; /* Set to 1 for one shot operation */
2824 const EVP_MD *md; /* Digest to use */
2825 EVP_MD_CTX *ctx; /* Digest context */
2827 STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
2828 unsigned char *osin; /* Input data if one shot */
2829 size_t osin_len; /* Input length data if one shot */
2830 unsigned char *output; /* Expected output */
2831 size_t output_len; /* Expected output length */
2834 static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
2837 const EVP_MD *md = NULL;
2838 DIGESTSIGN_DATA *mdat;
2840 if (strcmp(alg, "NULL") != 0) {
2841 if (is_digest_disabled(alg)) {
2845 md = EVP_get_digestbyname(alg);
2849 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
2852 if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
2856 mdat->is_verify = is_verify;
2857 mdat->is_oneshot = is_oneshot;
2862 static int digestsign_test_init(EVP_TEST *t, const char *alg)
2864 return digestsigver_test_init(t, alg, 0, 0);
2867 static void digestsigver_test_cleanup(EVP_TEST *t)
2869 DIGESTSIGN_DATA *mdata = t->data;
2871 EVP_MD_CTX_free(mdata->ctx);
2872 sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
2873 OPENSSL_free(mdata->osin);
2874 OPENSSL_free(mdata->output);
2875 OPENSSL_free(mdata);
2879 static int digestsigver_test_parse(EVP_TEST *t,
2880 const char *keyword, const char *value)
2882 DIGESTSIGN_DATA *mdata = t->data;
2884 if (strcmp(keyword, "Key") == 0) {
2885 EVP_PKEY *pkey = NULL;
2887 const char *name = mdata->md == NULL ? NULL : EVP_MD_name(mdata->md);
2889 if (mdata->is_verify)
2890 rv = find_key(&pkey, value, public_keys);
2892 rv = find_key(&pkey, value, private_keys);
2893 if (rv == 0 || pkey == NULL) {
2897 if (mdata->is_verify) {
2898 if (!EVP_DigestVerifyInit_ex(mdata->ctx, &mdata->pctx, name, libctx,
2900 t->err = "DIGESTVERIFYINIT_ERROR";
2903 if (!EVP_DigestSignInit_ex(mdata->ctx, &mdata->pctx, name, libctx, NULL,
2905 t->err = "DIGESTSIGNINIT_ERROR";
2909 if (strcmp(keyword, "Input") == 0) {
2910 if (mdata->is_oneshot)
2911 return parse_bin(value, &mdata->osin, &mdata->osin_len);
2912 return evp_test_buffer_append(value, &mdata->input);
2914 if (strcmp(keyword, "Output") == 0)
2915 return parse_bin(value, &mdata->output, &mdata->output_len);
2917 if (!mdata->is_oneshot) {
2918 if (strcmp(keyword, "Count") == 0)
2919 return evp_test_buffer_set_count(value, mdata->input);
2920 if (strcmp(keyword, "Ncopy") == 0)
2921 return evp_test_buffer_ncopy(value, mdata->input);
2923 if (strcmp(keyword, "Ctrl") == 0) {
2924 if (mdata->pctx == NULL)
2926 return pkey_test_ctrl(t, mdata->pctx, value);
2931 static int digestsign_update_fn(void *ctx, const unsigned char *buf,
2934 return EVP_DigestSignUpdate(ctx, buf, buflen);
2937 static int digestsign_test_run(EVP_TEST *t)
2939 DIGESTSIGN_DATA *expected = t->data;
2940 unsigned char *got = NULL;
2943 if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
2945 t->err = "DIGESTUPDATE_ERROR";
2949 if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
2950 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
2953 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2954 t->err = "MALLOC_FAILURE";
2957 if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
2958 t->err = "DIGESTSIGNFINAL_ERROR";
2961 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
2962 expected->output, expected->output_len,
2972 static const EVP_TEST_METHOD digestsign_test_method = {
2974 digestsign_test_init,
2975 digestsigver_test_cleanup,
2976 digestsigver_test_parse,
2980 static int digestverify_test_init(EVP_TEST *t, const char *alg)
2982 return digestsigver_test_init(t, alg, 1, 0);
2985 static int digestverify_update_fn(void *ctx, const unsigned char *buf,
2988 return EVP_DigestVerifyUpdate(ctx, buf, buflen);
2991 static int digestverify_test_run(EVP_TEST *t)
2993 DIGESTSIGN_DATA *mdata = t->data;
2995 if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
2996 t->err = "DIGESTUPDATE_ERROR";
3000 if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
3001 mdata->output_len) <= 0)
3002 t->err = "VERIFY_ERROR";
3006 static const EVP_TEST_METHOD digestverify_test_method = {
3008 digestverify_test_init,
3009 digestsigver_test_cleanup,
3010 digestsigver_test_parse,
3011 digestverify_test_run
3014 static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
3016 return digestsigver_test_init(t, alg, 0, 1);
3019 static int oneshot_digestsign_test_run(EVP_TEST *t)
3021 DIGESTSIGN_DATA *expected = t->data;
3022 unsigned char *got = NULL;
3025 if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
3026 expected->osin, expected->osin_len)) {
3027 t->err = "DIGESTSIGN_LENGTH_ERROR";
3030 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
3031 t->err = "MALLOC_FAILURE";
3034 if (!EVP_DigestSign(expected->ctx, got, &got_len,
3035 expected->osin, expected->osin_len)) {
3036 t->err = "DIGESTSIGN_ERROR";
3039 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
3040 expected->output, expected->output_len,
3050 static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
3051 "OneShotDigestSign",
3052 oneshot_digestsign_test_init,
3053 digestsigver_test_cleanup,
3054 digestsigver_test_parse,
3055 oneshot_digestsign_test_run
3058 static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
3060 return digestsigver_test_init(t, alg, 1, 1);
3063 static int oneshot_digestverify_test_run(EVP_TEST *t)
3065 DIGESTSIGN_DATA *mdata = t->data;
3067 if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
3068 mdata->osin, mdata->osin_len) <= 0)
3069 t->err = "VERIFY_ERROR";
3073 static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
3074 "OneShotDigestVerify",
3075 oneshot_digestverify_test_init,
3076 digestsigver_test_cleanup,
3077 digestsigver_test_parse,
3078 oneshot_digestverify_test_run
3083 ** PARSING AND DISPATCH
3086 static const EVP_TEST_METHOD *evp_test_list[] = {
3088 &cipher_test_method,
3089 &digest_test_method,
3090 &digestsign_test_method,
3091 &digestverify_test_method,
3092 &encode_test_method,
3094 &pkey_kdf_test_method,
3095 &keypair_test_method,
3096 &keygen_test_method,
3098 &oneshot_digestsign_test_method,
3099 &oneshot_digestverify_test_method,
3101 &pdecrypt_test_method,
3102 &pderive_test_method,
3104 &pverify_recover_test_method,
3105 &pverify_test_method,
3109 static const EVP_TEST_METHOD *find_test(const char *name)
3111 const EVP_TEST_METHOD **tt;
3113 for (tt = evp_test_list; *tt; tt++) {
3114 if (strcmp(name, (*tt)->name) == 0)
3120 static void clear_test(EVP_TEST *t)
3122 test_clearstanza(&t->s);
3124 if (t->data != NULL) {
3125 if (t->meth != NULL)
3126 t->meth->cleanup(t);
3127 OPENSSL_free(t->data);
3130 OPENSSL_free(t->expected_err);
3131 t->expected_err = NULL;
3132 OPENSSL_free(t->reason);
3141 /* Check for errors in the test structure; return 1 if okay, else 0. */
3142 static int check_test_error(EVP_TEST *t)
3147 if (t->err == NULL && t->expected_err == NULL)
3149 if (t->err != NULL && t->expected_err == NULL) {
3150 if (t->aux_err != NULL) {
3151 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
3152 t->s.test_file, t->s.start, t->aux_err, t->err);
3154 TEST_info("%s:%d: Source of above error; unexpected error %s",
3155 t->s.test_file, t->s.start, t->err);
3159 if (t->err == NULL && t->expected_err != NULL) {
3160 TEST_info("%s:%d: Succeeded but was expecting %s",
3161 t->s.test_file, t->s.start, t->expected_err);
3165 if (strcmp(t->err, t->expected_err) != 0) {
3166 TEST_info("%s:%d: Expected %s got %s",
3167 t->s.test_file, t->s.start, t->expected_err, t->err);
3171 if (t->reason == NULL)
3174 if (t->reason == NULL) {
3175 TEST_info("%s:%d: Test is missing function or reason code",
3176 t->s.test_file, t->s.start);
3180 err = ERR_peek_error();
3182 TEST_info("%s:%d: Expected error \"%s\" not set",
3183 t->s.test_file, t->s.start, t->reason);
3187 reason = ERR_reason_error_string(err);
3188 if (reason == NULL) {
3189 TEST_info("%s:%d: Expected error \"%s\", no strings available."
3191 t->s.test_file, t->s.start, t->reason);
3195 if (strcmp(reason, t->reason) == 0)
3198 TEST_info("%s:%d: Expected error \"%s\", got \"%s\"",
3199 t->s.test_file, t->s.start, t->reason, reason);
3204 /* Run a parsed test. Log a message and return 0 on error. */
3205 static int run_test(EVP_TEST *t)
3207 if (t->meth == NULL)
3214 if (t->err == NULL && t->meth->run_test(t) != 1) {
3215 TEST_info("%s:%d %s error",
3216 t->s.test_file, t->s.start, t->meth->name);
3219 if (!check_test_error(t)) {
3220 TEST_openssl_errors();
3229 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
3231 for (; lst != NULL; lst = lst->next) {
3232 if (strcmp(lst->name, name) == 0) {
3241 static void free_key_list(KEY_LIST *lst)
3243 while (lst != NULL) {
3244 KEY_LIST *next = lst->next;
3246 EVP_PKEY_free(lst->key);
3247 OPENSSL_free(lst->name);
3254 * Is the key type an unsupported algorithm?
3256 static int key_unsupported(void)
3258 long err = ERR_peek_last_error();
3260 if (ERR_GET_LIB(err) == ERR_LIB_EVP
3261 && (ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM)) {
3265 #ifndef OPENSSL_NO_EC
3267 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
3268 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
3271 if (ERR_GET_LIB(err) == ERR_LIB_EC
3272 && (ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP
3273 || ERR_GET_REASON(err) == EC_R_INVALID_CURVE)) {
3277 #endif /* OPENSSL_NO_EC */
3281 /* NULL out the value from |pp| but return it. This "steals" a pointer. */
3282 static char *take_value(PAIR *pp)
3284 char *p = pp->value;
3290 #if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
3291 static int securitycheck_enabled(void)
3293 static int enabled = -1;
3295 if (enabled == -1) {
3296 if (OSSL_PROVIDER_available(libctx, "fips")) {
3297 OSSL_PARAM params[2];
3298 OSSL_PROVIDER *prov = NULL;
3301 prov = OSSL_PROVIDER_load(libctx, "fips");
3304 OSSL_PARAM_construct_int(OSSL_PROV_PARAM_SECURITY_CHECKS,
3306 params[1] = OSSL_PARAM_construct_end();
3307 OSSL_PROVIDER_get_params(prov, params);
3308 OSSL_PROVIDER_unload(prov);
3320 * Return 1 if one of the providers named in the string is available.
3321 * The provider names are separated with whitespace.
3322 * NOTE: destructive function, it inserts '\0' after each provider name.
3324 static int prov_available(char *providers)
3330 for (; isspace(*providers); providers++)
3332 if (*providers == '\0')
3333 break; /* End of the road */
3334 for (p = providers; *p != '\0' && !isspace(*p); p++)
3340 if (OSSL_PROVIDER_available(libctx, providers))
3341 return 1; /* Found one */
3346 /* Read and parse one test. Return 0 if failure, 1 if okay. */
3347 static int parse(EVP_TEST *t)
3349 KEY_LIST *key, **klist;
3352 int i, skip_availablein = 0;
3356 if (BIO_eof(t->s.fp))
3359 if (!test_readstanza(&t->s))
3361 } while (t->s.numpairs == 0);
3362 pp = &t->s.pairs[0];
3364 /* Are we adding a key? */
3368 if (strcmp(pp->key, "PrivateKey") == 0) {
3369 pkey = PEM_read_bio_PrivateKey_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3370 if (pkey == NULL && !key_unsupported()) {
3371 EVP_PKEY_free(pkey);
3372 TEST_info("Can't read private key %s", pp->value);
3373 TEST_openssl_errors();
3376 klist = &private_keys;
3377 } else if (strcmp(pp->key, "PublicKey") == 0) {
3378 pkey = PEM_read_bio_PUBKEY_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3379 if (pkey == NULL && !key_unsupported()) {
3380 EVP_PKEY_free(pkey);
3381 TEST_info("Can't read public key %s", pp->value);
3382 TEST_openssl_errors();
3385 klist = &public_keys;
3386 } else if (strcmp(pp->key, "PrivateKeyRaw") == 0
3387 || strcmp(pp->key, "PublicKeyRaw") == 0 ) {
3388 char *strnid = NULL, *keydata = NULL;
3389 unsigned char *keybin;
3393 if (strcmp(pp->key, "PrivateKeyRaw") == 0)
3394 klist = &private_keys;
3396 klist = &public_keys;
3398 strnid = strchr(pp->value, ':');
3399 if (strnid != NULL) {
3401 keydata = strchr(strnid, ':');
3402 if (keydata != NULL)
3405 if (keydata == NULL) {
3406 TEST_info("Failed to parse %s value", pp->key);
3410 nid = OBJ_txt2nid(strnid);
3411 if (nid == NID_undef) {
3412 TEST_info("Unrecognised algorithm NID");
3415 if (!parse_bin(keydata, &keybin, &keylen)) {
3416 TEST_info("Failed to create binary key");
3419 if (klist == &private_keys)
3420 pkey = EVP_PKEY_new_raw_private_key_ex(libctx, strnid, NULL, keybin,
3423 pkey = EVP_PKEY_new_raw_public_key_ex(libctx, strnid, NULL, keybin,
3425 if (pkey == NULL && !key_unsupported()) {
3426 TEST_info("Can't read %s data", pp->key);
3427 OPENSSL_free(keybin);
3428 TEST_openssl_errors();
3431 OPENSSL_free(keybin);
3432 } else if (strcmp(pp->key, "Availablein") == 0) {
3433 if (!prov_available(pp->value)) {
3434 TEST_info("skipping, '%s' provider not available: %s:%d",
3435 pp->value, t->s.test_file, t->s.start);
3444 /* If we have a key add to list */
3445 if (klist != NULL) {
3446 if (find_key(NULL, pp->value, *klist)) {
3447 TEST_info("Duplicate key %s", pp->value);
3450 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
3452 key->name = take_value(pp);
3457 /* Go back and start a new stanza. */
3458 if ((t->s.numpairs - skip_availablein) != 1)
3459 TEST_info("Line %d: missing blank line\n", t->s.curr);
3463 /* Find the test, based on first keyword. */
3464 if (!TEST_ptr(t->meth = find_test(pp->key)))
3466 if (!t->meth->init(t, pp->value)) {
3467 TEST_error("unknown %s: %s\n", pp->key, pp->value);
3471 /* TEST_info("skipping %s %s", pp->key, pp->value); */
3475 for (pp++, i = 1; i < (t->s.numpairs - skip_availablein); pp++, i++) {
3476 if (strcmp(pp->key, "Securitycheck") == 0) {
3477 #if defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
3479 if (!securitycheck_enabled())
3482 TEST_info("skipping, Securitycheck is disabled: %s:%d",
3483 t->s.test_file, t->s.start);
3487 } else if (strcmp(pp->key, "Availablein") == 0) {
3488 TEST_info("Line %d: 'Availablein' should be the first option",
3491 } else if (strcmp(pp->key, "Result") == 0) {
3492 if (t->expected_err != NULL) {
3493 TEST_info("Line %d: multiple result lines", t->s.curr);
3496 t->expected_err = take_value(pp);
3497 } else if (strcmp(pp->key, "Function") == 0) {
3498 /* Ignore old line. */
3499 } else if (strcmp(pp->key, "Reason") == 0) {
3500 if (t->reason != NULL) {
3501 TEST_info("Line %d: multiple reason lines", t->s.curr);
3504 t->reason = take_value(pp);
3506 /* Must be test specific line: try to parse it */
3507 int rv = t->meth->parse(t, pp->key, pp->value);
3510 TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key);
3514 TEST_info("Line %d: error processing keyword %s = %s\n",
3515 t->s.curr, pp->key, pp->value);
3524 static int run_file_tests(int i)
3527 const char *testfile = test_get_argument(i);
3530 if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t))))
3532 if (!test_start_file(&t->s, testfile)) {
3537 while (!BIO_eof(t->s.fp)) {
3543 if (c == 0 || !run_test(t)) {
3548 test_end_file(&t->s);
3551 free_key_list(public_keys);
3552 free_key_list(private_keys);
3559 const OPTIONS *test_get_options(void)
3561 static const OPTIONS test_options[] = {
3562 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
3563 { "config", OPT_CONFIG_FILE, '<',
3564 "The configuration file to use for the libctx" },
3565 { OPT_HELP_STR, 1, '-',
3566 "file\tFile to run tests on.\n" },
3569 return test_options;
3572 int setup_tests(void)
3575 char *config_file = NULL;
3579 while ((o = opt_next()) != OPT_EOF) {
3581 case OPT_CONFIG_FILE:
3582 config_file = opt_arg();
3584 case OPT_TEST_CASES:
3593 * Load the provider via configuration into the created library context.
3594 * Load the 'null' provider into the default library context to ensure that
3595 * the the tests do not fallback to using the default provider.
3597 if (!test_get_libctx(&libctx, &prov_null, config_file, NULL, NULL))
3600 n = test_get_argument_count();
3604 ADD_ALL_TESTS(run_file_tests, n);
3608 void cleanup_tests(void)
3610 OSSL_PROVIDER_unload(prov_null);
3611 OSSL_LIB_CTX_free(libctx);
3614 #define STR_STARTS_WITH(str, pre) strncasecmp(pre, str, strlen(pre)) == 0
3615 #define STR_ENDS_WITH(str, pre) \
3616 strlen(str) < strlen(pre) ? 0 : (strcasecmp(pre, str + strlen(str) - strlen(pre)) == 0)
3618 static int is_digest_disabled(const char *name)
3620 #ifdef OPENSSL_NO_BLAKE2
3621 if (STR_STARTS_WITH(name, "BLAKE"))
3624 #ifdef OPENSSL_NO_MD2
3625 if (strcasecmp(name, "MD2") == 0)
3628 #ifdef OPENSSL_NO_MDC2
3629 if (strcasecmp(name, "MDC2") == 0)
3632 #ifdef OPENSSL_NO_MD4
3633 if (strcasecmp(name, "MD4") == 0)
3636 #ifdef OPENSSL_NO_MD5
3637 if (strcasecmp(name, "MD5") == 0)
3640 #ifdef OPENSSL_NO_RMD160
3641 if (strcasecmp(name, "RIPEMD160") == 0)
3644 #ifdef OPENSSL_NO_SM3
3645 if (strcasecmp(name, "SM3") == 0)
3648 #ifdef OPENSSL_NO_WHIRLPOOL
3649 if (strcasecmp(name, "WHIRLPOOL") == 0)
3655 static int is_pkey_disabled(const char *name)
3657 #ifdef OPENSSL_NO_RSA
3658 if (STR_STARTS_WITH(name, "RSA"))
3661 #ifdef OPENSSL_NO_EC
3662 if (STR_STARTS_WITH(name, "EC"))
3665 #ifdef OPENSSL_NO_DH
3666 if (STR_STARTS_WITH(name, "DH"))
3669 #ifdef OPENSSL_NO_DSA
3670 if (STR_STARTS_WITH(name, "DSA"))
3676 static int is_mac_disabled(const char *name)
3678 #ifdef OPENSSL_NO_BLAKE2
3679 if (STR_STARTS_WITH(name, "BLAKE2BMAC")
3680 || STR_STARTS_WITH(name, "BLAKE2SMAC"))
3683 #ifdef OPENSSL_NO_CMAC
3684 if (STR_STARTS_WITH(name, "CMAC"))
3687 #ifdef OPENSSL_NO_POLY1305
3688 if (STR_STARTS_WITH(name, "Poly1305"))
3691 #ifdef OPENSSL_NO_SIPHASH
3692 if (STR_STARTS_WITH(name, "SipHash"))
3697 static int is_kdf_disabled(const char *name)
3699 #ifdef OPENSSL_NO_SCRYPT
3700 if (STR_ENDS_WITH(name, "SCRYPT"))
3706 static int is_cipher_disabled(const char *name)
3708 #ifdef OPENSSL_NO_ARIA
3709 if (STR_STARTS_WITH(name, "ARIA"))
3712 #ifdef OPENSSL_NO_BF
3713 if (STR_STARTS_WITH(name, "BF"))
3716 #ifdef OPENSSL_NO_CAMELLIA
3717 if (STR_STARTS_WITH(name, "CAMELLIA"))
3720 #ifdef OPENSSL_NO_CAST
3721 if (STR_STARTS_WITH(name, "CAST"))
3724 #ifdef OPENSSL_NO_CHACHA
3725 if (STR_STARTS_WITH(name, "CHACHA"))
3728 #ifdef OPENSSL_NO_POLY1305
3729 if (STR_ENDS_WITH(name, "Poly1305"))
3732 #ifdef OPENSSL_NO_DES
3733 if (STR_STARTS_WITH(name, "DES"))
3735 if (STR_ENDS_WITH(name, "3DESwrap"))
3738 #ifdef OPENSSL_NO_OCB
3739 if (STR_ENDS_WITH(name, "OCB"))
3742 #ifdef OPENSSL_NO_IDEA
3743 if (STR_STARTS_WITH(name, "IDEA"))
3746 #ifdef OPENSSL_NO_RC2
3747 if (STR_STARTS_WITH(name, "RC2"))
3750 #ifdef OPENSSL_NO_RC4
3751 if (STR_STARTS_WITH(name, "RC4"))
3754 #ifdef OPENSSL_NO_RC5
3755 if (STR_STARTS_WITH(name, "RC5"))
3758 #ifdef OPENSSL_NO_SEED
3759 if (STR_STARTS_WITH(name, "SEED"))
3762 #ifdef OPENSSL_NO_SIV
3763 if (STR_ENDS_WITH(name, "SIV"))
3766 #ifdef OPENSSL_NO_SM4
3767 if (STR_STARTS_WITH(name, "SM4"))