2 * Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #define OPENSSL_SUPPRESS_DEPRECATED /* EVP_PKEY_new_CMAC_key */
15 #include <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 test_duplicate_md_ctx(EVP_TEST *t, EVP_MD_CTX *mctx)
400 char dont[] = "touch";
404 if (!EVP_DigestFinalXOF(mctx, (unsigned char *)dont, 0)) {
405 EVP_MD_CTX_free(mctx);
406 t->err = "DIGESTFINALXOF_ERROR";
409 if (!TEST_str_eq(dont, "touch")) {
410 EVP_MD_CTX_free(mctx);
411 t->err = "DIGESTFINALXOF_ERROR";
414 EVP_MD_CTX_free(mctx);
418 static int digest_test_run(EVP_TEST *t)
420 DIGEST_DATA *expected = t->data;
421 EVP_TEST_BUFFER *inbuf;
423 unsigned char *got = NULL;
424 unsigned int got_len;
427 OSSL_PARAM params[2];
429 t->err = "TEST_FAILURE";
430 if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
433 got = OPENSSL_malloc(expected->output_len > EVP_MAX_MD_SIZE ?
434 expected->output_len : EVP_MAX_MD_SIZE);
438 if (!EVP_DigestInit_ex(mctx, expected->digest, NULL)) {
439 t->err = "DIGESTINIT_ERROR";
442 if (expected->pad_type > 0) {
443 params[0] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_PAD_TYPE,
444 &expected->pad_type);
445 params[1] = OSSL_PARAM_construct_end();
446 if (!TEST_int_gt(EVP_MD_CTX_set_params(mctx, params), 0)) {
447 t->err = "PARAMS_ERROR";
451 if (!evp_test_buffer_do(expected->input, digest_update_fn, mctx)) {
452 t->err = "DIGESTUPDATE_ERROR";
456 xof = (EVP_MD_get_flags(expected->digest) & EVP_MD_FLAG_XOF) != 0;
458 EVP_MD_CTX *mctx_cpy;
460 if (!TEST_ptr(mctx_cpy = EVP_MD_CTX_new())) {
463 if (!TEST_true(EVP_MD_CTX_copy(mctx_cpy, mctx))) {
464 EVP_MD_CTX_free(mctx_cpy);
466 } else if (!test_duplicate_md_ctx(t, mctx_cpy)) {
470 if (!test_duplicate_md_ctx(t, EVP_MD_CTX_dup(mctx)))
473 got_len = expected->output_len;
474 if (!EVP_DigestFinalXOF(mctx, got, got_len)) {
475 t->err = "DIGESTFINALXOF_ERROR";
479 if (!EVP_DigestFinal(mctx, got, &got_len)) {
480 t->err = "DIGESTFINAL_ERROR";
484 if (!TEST_int_eq(expected->output_len, got_len)) {
485 t->err = "DIGEST_LENGTH_MISMATCH";
488 if (!memory_err_compare(t, "DIGEST_MISMATCH",
489 expected->output, expected->output_len,
495 /* Test the EVP_Q_digest interface as well */
496 if (sk_EVP_TEST_BUFFER_num(expected->input) == 1
498 /* This should never fail but we need the returned pointer now */
499 && !TEST_ptr(inbuf = sk_EVP_TEST_BUFFER_value(expected->input, 0))
500 && !inbuf->count_set) {
501 OPENSSL_cleanse(got, got_len);
502 if (!TEST_true(EVP_Q_digest(libctx,
503 EVP_MD_get0_name(expected->fetched_digest),
504 NULL, inbuf->buf, inbuf->buflen,
506 || !TEST_mem_eq(got, size,
507 expected->output, expected->output_len)) {
508 t->err = "EVP_Q_digest failed";
515 EVP_MD_CTX_free(mctx);
519 static const EVP_TEST_METHOD digest_test_method = {
531 typedef struct cipher_data_st {
532 const EVP_CIPHER *cipher;
533 EVP_CIPHER *fetched_cipher;
535 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
539 size_t key_bits; /* Used by RC2 */
541 unsigned char *next_iv; /* Expected IV state after operation */
544 unsigned char *plaintext;
545 size_t plaintext_len;
546 unsigned char *ciphertext;
547 size_t ciphertext_len;
548 /* AEAD ciphers only */
549 unsigned char *aad[AAD_NUM];
550 size_t aad_len[AAD_NUM];
554 const char *cts_mode;
557 unsigned char *mac_key;
559 const char *xts_standard;
562 static int cipher_test_init(EVP_TEST *t, const char *alg)
564 const EVP_CIPHER *cipher;
565 EVP_CIPHER *fetched_cipher;
569 if (is_cipher_disabled(alg)) {
571 TEST_info("skipping, '%s' is disabled", alg);
576 if ((cipher = fetched_cipher = EVP_CIPHER_fetch(libctx, alg, NULL)) == NULL
577 && (cipher = EVP_get_cipherbyname(alg)) == NULL) {
578 /* a stitched cipher might not be available */
579 if (strstr(alg, "HMAC") != NULL) {
582 TEST_info("skipping, '%s' is not available", alg);
585 ERR_clear_last_mark();
588 ERR_clear_last_mark();
590 if (!TEST_ptr(cdat = OPENSSL_zalloc(sizeof(*cdat))))
593 cdat->cipher = cipher;
594 cdat->fetched_cipher = fetched_cipher;
596 m = EVP_CIPHER_get_mode(cipher);
597 if (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
598 cdat->aead = m != 0 ? m : -1;
603 if (fetched_cipher != NULL)
604 TEST_info("%s is fetched", alg);
608 static void cipher_test_cleanup(EVP_TEST *t)
611 CIPHER_DATA *cdat = t->data;
613 OPENSSL_free(cdat->key);
614 OPENSSL_free(cdat->iv);
615 OPENSSL_free(cdat->next_iv);
616 OPENSSL_free(cdat->ciphertext);
617 OPENSSL_free(cdat->plaintext);
618 for (i = 0; i < AAD_NUM; i++)
619 OPENSSL_free(cdat->aad[i]);
620 OPENSSL_free(cdat->tag);
621 OPENSSL_free(cdat->mac_key);
622 EVP_CIPHER_free(cdat->fetched_cipher);
625 static int cipher_test_parse(EVP_TEST *t, const char *keyword,
628 CIPHER_DATA *cdat = t->data;
631 if (strcmp(keyword, "Key") == 0)
632 return parse_bin(value, &cdat->key, &cdat->key_len);
633 if (strcmp(keyword, "Rounds") == 0) {
637 cdat->rounds = (unsigned int)i;
640 if (strcmp(keyword, "IV") == 0)
641 return parse_bin(value, &cdat->iv, &cdat->iv_len);
642 if (strcmp(keyword, "NextIV") == 0)
643 return parse_bin(value, &cdat->next_iv, &cdat->iv_len);
644 if (strcmp(keyword, "Plaintext") == 0)
645 return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len);
646 if (strcmp(keyword, "Ciphertext") == 0)
647 return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
648 if (strcmp(keyword, "KeyBits") == 0) {
652 cdat->key_bits = (size_t)i;
658 if (strcmp(keyword, "TLSAAD") == 0)
659 cdat->tls_aad = tls_aad = 1;
660 if (strcmp(keyword, "AAD") == 0 || tls_aad) {
661 for (i = 0; i < AAD_NUM; i++) {
662 if (cdat->aad[i] == NULL)
663 return parse_bin(value, &cdat->aad[i], &cdat->aad_len[i]);
667 if (strcmp(keyword, "Tag") == 0)
668 return parse_bin(value, &cdat->tag, &cdat->tag_len);
669 if (strcmp(keyword, "SetTagLate") == 0) {
670 if (strcmp(value, "TRUE") == 0)
672 else if (strcmp(value, "FALSE") == 0)
678 if (strcmp(keyword, "MACKey") == 0)
679 return parse_bin(value, &cdat->mac_key, &cdat->mac_key_len);
680 if (strcmp(keyword, "TLSVersion") == 0) {
683 cdat->tls_version = (int)strtol(value, &endptr, 0);
684 return value[0] != '\0' && endptr[0] == '\0';
688 if (strcmp(keyword, "Operation") == 0) {
689 if (strcmp(value, "ENCRYPT") == 0)
691 else if (strcmp(value, "DECRYPT") == 0)
697 if (strcmp(keyword, "CTSMode") == 0) {
698 cdat->cts_mode = value;
701 if (strcmp(keyword, "XTSStandard") == 0) {
702 cdat->xts_standard = value;
708 static int cipher_test_enc(EVP_TEST *t, int enc,
709 size_t out_misalign, size_t inp_misalign, int frag)
711 CIPHER_DATA *expected = t->data;
712 unsigned char *in, *expected_out, *tmp = NULL;
713 size_t in_len, out_len, donelen = 0;
714 int ok = 0, tmplen, chunklen, tmpflen, i;
715 EVP_CIPHER_CTX *ctx_base = NULL;
716 EVP_CIPHER_CTX *ctx = NULL, *duped;
718 t->err = "TEST_FAILURE";
719 if (!TEST_ptr(ctx_base = EVP_CIPHER_CTX_new()))
721 if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
723 EVP_CIPHER_CTX_set_flags(ctx_base, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
725 in = expected->plaintext;
726 in_len = expected->plaintext_len;
727 expected_out = expected->ciphertext;
728 out_len = expected->ciphertext_len;
730 in = expected->ciphertext;
731 in_len = expected->ciphertext_len;
732 expected_out = expected->plaintext;
733 out_len = expected->plaintext_len;
735 if (inp_misalign == (size_t)-1) {
736 /* Exercise in-place encryption */
737 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
740 in = memcpy(tmp + out_misalign, in, in_len);
742 inp_misalign += 16 - ((out_misalign + in_len) & 15);
744 * 'tmp' will store both output and copy of input. We make the copy
745 * of input to specifically aligned part of 'tmp'. So we just
746 * figured out how much padding would ensure the required alignment,
747 * now we allocate extended buffer and finally copy the input just
748 * past inp_misalign in expression below. Output will be written
749 * past out_misalign...
751 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
752 inp_misalign + in_len);
755 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
756 inp_misalign, in, in_len);
758 if (!EVP_CipherInit_ex(ctx_base, expected->cipher, NULL, NULL, NULL, enc)) {
759 t->err = "CIPHERINIT_ERROR";
762 if (expected->cts_mode != NULL) {
763 OSSL_PARAM params[2];
765 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE,
766 (char *)expected->cts_mode,
768 params[1] = OSSL_PARAM_construct_end();
769 if (!EVP_CIPHER_CTX_set_params(ctx_base, params)) {
770 t->err = "INVALID_CTS_MODE";
775 if (expected->aead) {
776 if (EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_IVLEN,
777 expected->iv_len, 0) <= 0) {
778 t->err = "INVALID_IV_LENGTH";
781 } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_get_iv_length(ctx_base)) {
782 t->err = "INVALID_IV_LENGTH";
786 if (expected->aead && !expected->tls_aad) {
789 * If encrypting or OCB just set tag length initially, otherwise
790 * set tag length and value.
792 if (enc || expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late) {
793 t->err = "TAG_LENGTH_SET_ERROR";
796 t->err = "TAG_SET_ERROR";
799 if (tag || expected->aead != EVP_CIPH_GCM_MODE) {
800 if (EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_TAG,
801 expected->tag_len, tag) <= 0)
806 if (expected->rounds > 0) {
807 int rounds = (int)expected->rounds;
809 if (EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC5_ROUNDS, rounds, NULL) <= 0) {
810 t->err = "INVALID_ROUNDS";
815 if (!EVP_CIPHER_CTX_set_key_length(ctx_base, expected->key_len)) {
816 t->err = "INVALID_KEY_LENGTH";
819 if (expected->key_bits > 0) {
820 int bits = (int)expected->key_bits;
822 if (EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC2_KEY_BITS, bits, NULL) <= 0) {
823 t->err = "INVALID KEY BITS";
827 if (!EVP_CipherInit_ex(ctx_base, NULL, NULL, expected->key, expected->iv, -1)) {
828 t->err = "KEY_SET_ERROR";
832 /* Check that we get the same IV back */
833 if (expected->iv != NULL) {
834 /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
835 unsigned char iv[128];
836 if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx_base, iv, sizeof(iv)))
837 || ((EVP_CIPHER_get_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
838 && !TEST_mem_eq(expected->iv, expected->iv_len, iv,
839 expected->iv_len))) {
840 t->err = "INVALID_IV";
845 /* Test that the cipher dup functions correctly if it is supported */
847 if (EVP_CIPHER_CTX_copy(ctx, ctx_base)) {
848 EVP_CIPHER_CTX_free(ctx_base);
851 EVP_CIPHER_CTX_free(ctx);
854 /* Likewise for dup */
855 duped = EVP_CIPHER_CTX_dup(ctx);
857 EVP_CIPHER_CTX_free(ctx);
862 if (expected->mac_key != NULL
863 && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY,
864 (int)expected->mac_key_len,
865 (void *)expected->mac_key) <= 0) {
866 t->err = "SET_MAC_KEY_ERROR";
870 if (expected->tls_version) {
871 OSSL_PARAM params[2];
873 params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS_VERSION,
874 &expected->tls_version);
875 params[1] = OSSL_PARAM_construct_end();
876 if (!EVP_CIPHER_CTX_set_params(ctx, params)) {
877 t->err = "SET_TLS_VERSION_ERROR";
882 if (expected->aead == EVP_CIPH_CCM_MODE) {
883 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
884 t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
888 if (expected->aad[0] != NULL && !expected->tls_aad) {
889 t->err = "AAD_SET_ERROR";
891 for (i = 0; expected->aad[i] != NULL; i++) {
892 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i],
893 expected->aad_len[i]))
898 * Supply the AAD in chunks less than the block size where possible
900 for (i = 0; expected->aad[i] != NULL; i++) {
901 if (expected->aad_len[i] > 0) {
902 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], 1))
906 if (expected->aad_len[i] > 2) {
907 if (!EVP_CipherUpdate(ctx, NULL, &chunklen,
908 expected->aad[i] + donelen,
909 expected->aad_len[i] - 2))
911 donelen += expected->aad_len[i] - 2;
913 if (expected->aad_len[i] > 1
914 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
915 expected->aad[i] + donelen, 1))
921 if (expected->tls_aad) {
922 OSSL_PARAM params[2];
925 /* duplicate the aad as the implementation might modify it */
926 if ((tls_aad = OPENSSL_memdup(expected->aad[0],
927 expected->aad_len[0])) == NULL)
929 params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
931 expected->aad_len[0]);
932 params[1] = OSSL_PARAM_construct_end();
933 if (!EVP_CIPHER_CTX_set_params(ctx, params)) {
934 OPENSSL_free(tls_aad);
935 t->err = "TLS1_AAD_ERROR";
938 OPENSSL_free(tls_aad);
939 } else if (!enc && (expected->aead == EVP_CIPH_OCB_MODE
940 || expected->tag_late)) {
941 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
942 expected->tag_len, expected->tag) <= 0) {
943 t->err = "TAG_SET_ERROR";
947 if (expected->xts_standard != NULL) {
948 OSSL_PARAM params[2];
951 OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_XTS_STANDARD,
952 (char *)expected->xts_standard, 0);
953 params[1] = OSSL_PARAM_construct_end();
954 if (!EVP_CIPHER_CTX_set_params(ctx, params)) {
955 t->err = "SET_XTS_STANDARD_ERROR";
959 EVP_CIPHER_CTX_set_padding(ctx, 0);
960 t->err = "CIPHERUPDATE_ERROR";
963 /* We supply the data all in one go */
964 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
967 /* Supply the data in chunks less than the block size where possible */
969 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
976 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
984 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
990 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
991 t->err = "CIPHERFINAL_ERROR";
994 if (!enc && expected->tls_aad) {
995 if (expected->tls_version >= TLS1_1_VERSION
996 && (EVP_CIPHER_is_a(expected->cipher, "AES-128-CBC-HMAC-SHA1")
997 || EVP_CIPHER_is_a(expected->cipher, "AES-256-CBC-HMAC-SHA1"))) {
998 tmplen -= expected->iv_len;
999 expected_out += expected->iv_len;
1000 out_misalign += expected->iv_len;
1002 if ((int)out_len > tmplen + tmpflen)
1003 out_len = tmplen + tmpflen;
1005 if (!memory_err_compare(t, "VALUE_MISMATCH", expected_out, out_len,
1006 tmp + out_misalign, tmplen + tmpflen))
1008 if (enc && expected->aead && !expected->tls_aad) {
1009 unsigned char rtag[16];
1011 if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) {
1012 t->err = "TAG_LENGTH_INTERNAL_ERROR";
1015 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
1016 expected->tag_len, rtag) <= 0) {
1017 t->err = "TAG_RETRIEVE_ERROR";
1020 if (!memory_err_compare(t, "TAG_VALUE_MISMATCH",
1021 expected->tag, expected->tag_len,
1022 rtag, expected->tag_len))
1025 /* Check the updated IV */
1026 if (expected->next_iv != NULL) {
1027 /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
1028 unsigned char iv[128];
1029 if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv)))
1030 || ((EVP_CIPHER_get_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
1031 && !TEST_mem_eq(expected->next_iv, expected->iv_len, iv,
1032 expected->iv_len))) {
1033 t->err = "INVALID_NEXT_IV";
1042 if (ctx != ctx_base)
1043 EVP_CIPHER_CTX_free(ctx_base);
1044 EVP_CIPHER_CTX_free(ctx);
1048 static int cipher_test_run(EVP_TEST *t)
1050 CIPHER_DATA *cdat = t->data;
1052 size_t out_misalign, inp_misalign;
1058 if (!cdat->iv && EVP_CIPHER_get_iv_length(cdat->cipher)) {
1059 /* IV is optional and usually omitted in wrap mode */
1060 if (EVP_CIPHER_get_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
1065 if (cdat->aead && cdat->tag == NULL && !cdat->tls_aad) {
1069 for (out_misalign = 0; out_misalign <= 1;) {
1070 static char aux_err[64];
1071 t->aux_err = aux_err;
1072 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
1073 if (inp_misalign == (size_t)-1) {
1074 /* kludge: inp_misalign == -1 means "exercise in-place" */
1075 BIO_snprintf(aux_err, sizeof(aux_err),
1076 "%s in-place, %sfragmented",
1077 out_misalign ? "misaligned" : "aligned",
1078 frag ? "" : "not ");
1080 BIO_snprintf(aux_err, sizeof(aux_err),
1081 "%s output and %s input, %sfragmented",
1082 out_misalign ? "misaligned" : "aligned",
1083 inp_misalign ? "misaligned" : "aligned",
1084 frag ? "" : "not ");
1087 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
1088 /* Not fatal errors: return */
1095 if (cdat->enc != 1) {
1096 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
1097 /* Not fatal errors: return */
1106 if (out_misalign == 1 && frag == 0) {
1108 * XTS, SIV, CCM, stitched ciphers and Wrap modes have special
1109 * requirements about input lengths so we don't fragment for those
1111 if (cdat->aead == EVP_CIPH_CCM_MODE
1112 || cdat->aead == EVP_CIPH_CBC_MODE
1113 || (cdat->aead == -1
1114 && EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_STREAM_CIPHER)
1115 || ((EVP_CIPHER_get_flags(cdat->cipher) & EVP_CIPH_FLAG_CTS) != 0)
1116 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_SIV_MODE
1117 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_GCM_SIV_MODE
1118 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
1119 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
1132 static const EVP_TEST_METHOD cipher_test_method = {
1135 cipher_test_cleanup,
1145 typedef struct mac_data_st {
1146 /* MAC type in one form or another */
1148 EVP_MAC *mac; /* for mac_test_run_mac */
1149 int type; /* for mac_test_run_pkey */
1150 /* Algorithm string for this MAC */
1159 unsigned char *input;
1161 /* Expected output */
1162 unsigned char *output;
1164 unsigned char *custom;
1166 /* MAC salt (blake2) */
1167 unsigned char *salt;
1171 /* Reinitialization fails */
1173 /* Collection of controls */
1174 STACK_OF(OPENSSL_STRING) *controls;
1181 static int mac_test_init(EVP_TEST *t, const char *alg)
1183 EVP_MAC *mac = NULL;
1184 int type = NID_undef;
1187 if (is_mac_disabled(alg)) {
1188 TEST_info("skipping, '%s' is disabled", alg);
1192 if ((mac = EVP_MAC_fetch(libctx, alg, NULL)) == NULL) {
1194 * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
1195 * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
1196 * the EVP_PKEY method.
1198 size_t sz = strlen(alg);
1199 static const char epilogue[] = " by EVP_PKEY";
1201 if (sz >= sizeof(epilogue)
1202 && strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0)
1203 sz -= sizeof(epilogue) - 1;
1205 if (strncmp(alg, "HMAC", sz) == 0)
1206 type = EVP_PKEY_HMAC;
1207 else if (strncmp(alg, "CMAC", sz) == 0)
1208 type = EVP_PKEY_CMAC;
1209 else if (strncmp(alg, "Poly1305", sz) == 0)
1210 type = EVP_PKEY_POLY1305;
1211 else if (strncmp(alg, "SipHash", sz) == 0)
1212 type = EVP_PKEY_SIPHASH;
1217 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
1221 if (!TEST_ptr(mdat->mac_name = OPENSSL_strdup(alg))) {
1227 if (!TEST_ptr(mdat->controls = sk_OPENSSL_STRING_new_null())) {
1228 OPENSSL_free(mdat->mac_name);
1233 mdat->output_size = mdat->block_size = -1;
1238 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
1239 static void openssl_free(char *m)
1244 static void mac_test_cleanup(EVP_TEST *t)
1246 MAC_DATA *mdat = t->data;
1248 EVP_MAC_free(mdat->mac);
1249 OPENSSL_free(mdat->mac_name);
1250 sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free);
1251 OPENSSL_free(mdat->alg);
1252 OPENSSL_free(mdat->key);
1253 OPENSSL_free(mdat->iv);
1254 OPENSSL_free(mdat->custom);
1255 OPENSSL_free(mdat->salt);
1256 OPENSSL_free(mdat->input);
1257 OPENSSL_free(mdat->output);
1260 static int mac_test_parse(EVP_TEST *t,
1261 const char *keyword, const char *value)
1263 MAC_DATA *mdata = t->data;
1265 if (strcmp(keyword, "Key") == 0)
1266 return parse_bin(value, &mdata->key, &mdata->key_len);
1267 if (strcmp(keyword, "IV") == 0)
1268 return parse_bin(value, &mdata->iv, &mdata->iv_len);
1269 if (strcmp(keyword, "Custom") == 0)
1270 return parse_bin(value, &mdata->custom, &mdata->custom_len);
1271 if (strcmp(keyword, "Salt") == 0)
1272 return parse_bin(value, &mdata->salt, &mdata->salt_len);
1273 if (strcmp(keyword, "Algorithm") == 0) {
1274 mdata->alg = OPENSSL_strdup(value);
1275 if (mdata->alg == NULL)
1279 if (strcmp(keyword, "Input") == 0)
1280 return parse_bin(value, &mdata->input, &mdata->input_len);
1281 if (strcmp(keyword, "Output") == 0)
1282 return parse_bin(value, &mdata->output, &mdata->output_len);
1283 if (strcmp(keyword, "XOF") == 0)
1284 return mdata->xof = 1;
1285 if (strcmp(keyword, "NoReinit") == 0)
1286 return mdata->no_reinit = 1;
1287 if (strcmp(keyword, "Ctrl") == 0) {
1288 char *data = OPENSSL_strdup(value);
1292 return sk_OPENSSL_STRING_push(mdata->controls, data) != 0;
1294 if (strcmp(keyword, "OutputSize") == 0) {
1295 mdata->output_size = atoi(value);
1296 if (mdata->output_size < 0)
1300 if (strcmp(keyword, "BlockSize") == 0) {
1301 mdata->block_size = atoi(value);
1302 if (mdata->block_size < 0)
1309 static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1315 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1317 p = strchr(tmpval, ':');
1320 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1323 t->err = "PKEY_CTRL_INVALID";
1325 t->err = "PKEY_CTRL_ERROR";
1328 OPENSSL_free(tmpval);
1332 static int mac_test_run_pkey(EVP_TEST *t)
1334 MAC_DATA *expected = t->data;
1335 EVP_MD_CTX *mctx = NULL;
1336 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1337 EVP_PKEY *key = NULL;
1338 const char *mdname = NULL;
1339 EVP_CIPHER *cipher = NULL;
1340 unsigned char *got = NULL;
1344 /* We don't do XOF mode via PKEY */
1348 if (expected->alg == NULL)
1349 TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type));
1351 TEST_info("Trying the EVP_PKEY %s test with %s",
1352 OBJ_nid2sn(expected->type), expected->alg);
1354 if (expected->type == EVP_PKEY_CMAC) {
1355 #ifdef OPENSSL_NO_DEPRECATED_3_0
1356 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
1361 OSSL_LIB_CTX *tmpctx;
1363 if (expected->alg != NULL && is_cipher_disabled(expected->alg)) {
1364 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
1369 if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, expected->alg, NULL))) {
1370 t->err = "MAC_KEY_CREATE_ERROR";
1373 tmpctx = OSSL_LIB_CTX_set0_default(libctx);
1374 key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len,
1376 OSSL_LIB_CTX_set0_default(tmpctx);
1379 key = EVP_PKEY_new_raw_private_key_ex(libctx,
1380 OBJ_nid2sn(expected->type), NULL,
1381 expected->key, expected->key_len);
1384 t->err = "MAC_KEY_CREATE_ERROR";
1388 if (expected->type == EVP_PKEY_HMAC && expected->alg != NULL) {
1389 if (is_digest_disabled(expected->alg)) {
1390 TEST_info("skipping, HMAC '%s' is disabled", expected->alg);
1395 mdname = expected->alg;
1397 if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
1398 t->err = "INTERNAL_ERROR";
1401 if (!EVP_DigestSignInit_ex(mctx, &pctx, mdname, libctx, NULL, key, NULL)) {
1402 t->err = "DIGESTSIGNINIT_ERROR";
1405 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++)
1406 if (!mac_test_ctrl_pkey(t, pctx,
1407 sk_OPENSSL_STRING_value(expected->controls,
1409 t->err = "EVPPKEYCTXCTRL_ERROR";
1412 if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) {
1413 t->err = "DIGESTSIGNUPDATE_ERROR";
1416 if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) {
1417 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1420 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1421 t->err = "TEST_FAILURE";
1424 if (!EVP_DigestSignFinal(mctx, got, &got_len)
1425 || !memory_err_compare(t, "TEST_MAC_ERR",
1426 expected->output, expected->output_len,
1428 t->err = "TEST_MAC_ERR";
1433 EVP_CIPHER_free(cipher);
1434 EVP_MD_CTX_free(mctx);
1436 EVP_PKEY_CTX_free(genctx);
1441 static int mac_test_run_mac(EVP_TEST *t)
1443 MAC_DATA *expected = t->data;
1444 EVP_MAC_CTX *ctx = NULL;
1445 unsigned char *got = NULL;
1446 size_t got_len = 0, size = 0;
1447 int i, block_size = -1, output_size = -1;
1448 OSSL_PARAM params[21], sizes[3], *psizes = sizes;
1449 size_t params_n = 0;
1450 size_t params_n_allocstart = 0;
1451 const OSSL_PARAM *defined_params =
1452 EVP_MAC_settable_ctx_params(expected->mac);
1456 if (expected->alg == NULL)
1457 TEST_info("Trying the EVP_MAC %s test", expected->mac_name);
1459 TEST_info("Trying the EVP_MAC %s test with %s",
1460 expected->mac_name, expected->alg);
1462 if (expected->alg != NULL) {
1466 * The underlying algorithm may be a cipher or a digest.
1467 * We don't know which it is, but we can ask the MAC what it
1468 * should be and bet on that.
1470 if (OSSL_PARAM_locate_const(defined_params,
1471 OSSL_MAC_PARAM_CIPHER) != NULL) {
1472 if (is_cipher_disabled(expected->alg))
1475 params[params_n++] =
1476 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
1478 } else if (OSSL_PARAM_locate_const(defined_params,
1479 OSSL_MAC_PARAM_DIGEST) != NULL) {
1480 if (is_digest_disabled(expected->alg))
1483 params[params_n++] =
1484 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
1487 t->err = "MAC_BAD_PARAMS";
1491 TEST_info("skipping, algorithm '%s' is disabled", expected->alg);
1497 if (expected->custom != NULL)
1498 params[params_n++] =
1499 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
1501 expected->custom_len);
1502 if (expected->salt != NULL)
1503 params[params_n++] =
1504 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT,
1506 expected->salt_len);
1507 if (expected->iv != NULL)
1508 params[params_n++] =
1509 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
1513 /* Unknown controls. They must match parameters that the MAC recognizes */
1514 if (params_n + sk_OPENSSL_STRING_num(expected->controls)
1515 >= OSSL_NELEM(params)) {
1516 t->err = "MAC_TOO_MANY_PARAMETERS";
1519 params_n_allocstart = params_n;
1520 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) {
1521 char *tmpkey, *tmpval;
1522 char *value = sk_OPENSSL_STRING_value(expected->controls, i);
1524 if (!TEST_ptr(tmpkey = OPENSSL_strdup(value))) {
1525 t->err = "MAC_PARAM_ERROR";
1528 tmpval = strchr(tmpkey, ':');
1533 || !OSSL_PARAM_allocate_from_text(¶ms[params_n],
1536 strlen(tmpval), NULL)) {
1537 OPENSSL_free(tmpkey);
1538 t->err = "MAC_PARAM_ERROR";
1543 OPENSSL_free(tmpkey);
1545 params[params_n] = OSSL_PARAM_construct_end();
1547 if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) {
1548 t->err = "MAC_CREATE_ERROR";
1552 if (!EVP_MAC_init(ctx, expected->key, expected->key_len, params)) {
1553 t->err = "MAC_INIT_ERROR";
1556 if (expected->output_size >= 0)
1557 *psizes++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_SIZE,
1559 if (expected->block_size >= 0)
1560 *psizes++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_BLOCK_SIZE,
1562 if (psizes != sizes) {
1563 *psizes = OSSL_PARAM_construct_end();
1564 if (!TEST_true(EVP_MAC_CTX_get_params(ctx, sizes))) {
1565 t->err = "INTERNAL_ERROR";
1568 if (expected->output_size >= 0
1569 && !TEST_int_eq(output_size, expected->output_size)) {
1570 t->err = "TEST_FAILURE";
1573 if (expected->block_size >= 0
1574 && !TEST_int_eq(block_size, expected->block_size)) {
1575 t->err = "TEST_FAILURE";
1580 if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) {
1581 t->err = "MAC_UPDATE_ERROR";
1584 xof = expected->xof;
1586 if (!TEST_ptr(got = OPENSSL_malloc(expected->output_len))) {
1587 t->err = "TEST_FAILURE";
1590 if (!EVP_MAC_finalXOF(ctx, got, expected->output_len)
1591 || !memory_err_compare(t, "TEST_MAC_ERR",
1592 expected->output, expected->output_len,
1593 got, expected->output_len)) {
1594 t->err = "MAC_FINAL_ERROR";
1598 if (!EVP_MAC_final(ctx, NULL, &got_len, 0)) {
1599 t->err = "MAC_FINAL_LENGTH_ERROR";
1602 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1603 t->err = "TEST_FAILURE";
1606 if (!EVP_MAC_final(ctx, got, &got_len, got_len)
1607 || !memory_err_compare(t, "TEST_MAC_ERR",
1608 expected->output, expected->output_len,
1610 t->err = "TEST_MAC_ERR";
1614 /* FIPS(3.0.0): can't reinitialise MAC contexts #18100 */
1615 if (reinit-- && fips_provider_version_gt(libctx, 3, 0, 0)) {
1616 OSSL_PARAM ivparams[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1619 /* If the MAC uses IV, we have to set it again */
1620 if (expected->iv != NULL) {
1622 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
1625 ivparams[1] = OSSL_PARAM_construct_end();
1628 ret = EVP_MAC_init(ctx, NULL, 0, ivparams);
1629 if (expected->no_reinit) {
1631 ERR_clear_last_mark();
1632 t->err = "MAC_REINIT_SHOULD_FAIL";
1636 ERR_clear_last_mark();
1641 ERR_clear_last_mark();
1642 t->err = "MAC_REINIT_ERROR";
1645 /* If reinitialization fails, it is unsupported by the algorithm */
1650 /* Test the EVP_Q_mac interface as well */
1652 OPENSSL_cleanse(got, got_len);
1653 if (!TEST_true(EVP_Q_mac(libctx, expected->mac_name, NULL,
1654 expected->alg, params,
1655 expected->key, expected->key_len,
1656 expected->input, expected->input_len,
1657 got, got_len, &size))
1658 || !TEST_mem_eq(got, size,
1659 expected->output, expected->output_len)) {
1660 t->err = "EVP_Q_mac failed";
1665 while (params_n-- > params_n_allocstart) {
1666 OPENSSL_free(params[params_n].data);
1668 EVP_MAC_CTX_free(ctx);
1673 static int mac_test_run(EVP_TEST *t)
1675 MAC_DATA *expected = t->data;
1677 if (expected->mac != NULL)
1678 return mac_test_run_mac(t);
1679 return mac_test_run_pkey(t);
1682 static const EVP_TEST_METHOD mac_test_method = {
1693 ** These are all very similar and share much common code.
1696 typedef struct pkey_data_st {
1697 /* Context for this operation */
1699 /* Key operation to perform */
1700 int (*keyop) (EVP_PKEY_CTX *ctx,
1701 unsigned char *sig, size_t *siglen,
1702 const unsigned char *tbs, size_t tbslen);
1704 unsigned char *input;
1706 /* Expected output */
1707 unsigned char *output;
1712 * Perform public key operation setup: lookup key, allocated ctx and call
1713 * the appropriate initialisation function
1715 static int pkey_test_init(EVP_TEST *t, const char *name,
1717 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1718 int (*keyop)(EVP_PKEY_CTX *ctx,
1719 unsigned char *sig, size_t *siglen,
1720 const unsigned char *tbs,
1724 EVP_PKEY *pkey = NULL;
1728 rv = find_key(&pkey, name, public_keys);
1730 rv = find_key(&pkey, name, private_keys);
1731 if (rv == 0 || pkey == NULL) {
1732 TEST_info("skipping, key '%s' is disabled", name);
1737 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) {
1738 EVP_PKEY_free(pkey);
1741 kdata->keyop = keyop;
1742 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL))) {
1743 EVP_PKEY_free(pkey);
1744 OPENSSL_free(kdata);
1747 if (keyopinit(kdata->ctx) <= 0)
1748 t->err = "KEYOP_INIT_ERROR";
1753 static void pkey_test_cleanup(EVP_TEST *t)
1755 PKEY_DATA *kdata = t->data;
1757 OPENSSL_free(kdata->input);
1758 OPENSSL_free(kdata->output);
1759 EVP_PKEY_CTX_free(kdata->ctx);
1762 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1768 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1770 p = strchr(tmpval, ':');
1773 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1776 t->err = "PKEY_CTRL_INVALID";
1778 } else if (p != NULL && rv <= 0) {
1779 if (is_digest_disabled(p) || is_cipher_disabled(p)) {
1780 TEST_info("skipping, '%s' is disabled", p);
1784 t->err = "PKEY_CTRL_ERROR";
1788 OPENSSL_free(tmpval);
1792 static int pkey_test_parse(EVP_TEST *t,
1793 const char *keyword, const char *value)
1795 PKEY_DATA *kdata = t->data;
1796 if (strcmp(keyword, "Input") == 0)
1797 return parse_bin(value, &kdata->input, &kdata->input_len);
1798 if (strcmp(keyword, "Output") == 0)
1799 return parse_bin(value, &kdata->output, &kdata->output_len);
1800 if (strcmp(keyword, "Ctrl") == 0)
1801 return pkey_test_ctrl(t, kdata->ctx, value);
1805 static int pkey_test_run(EVP_TEST *t)
1807 PKEY_DATA *expected = t->data;
1808 unsigned char *got = NULL;
1810 EVP_PKEY_CTX *copy = NULL;
1812 if (expected->keyop(expected->ctx, NULL, &got_len,
1813 expected->input, expected->input_len) <= 0
1814 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1815 t->err = "KEYOP_LENGTH_ERROR";
1818 if (expected->keyop(expected->ctx, got, &got_len,
1819 expected->input, expected->input_len) <= 0) {
1820 t->err = "KEYOP_ERROR";
1823 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1824 expected->output, expected->output_len,
1832 /* Repeat the test on a copy. */
1833 if (!TEST_ptr(copy = EVP_PKEY_CTX_dup(expected->ctx))) {
1834 t->err = "INTERNAL_ERROR";
1837 if (expected->keyop(copy, NULL, &got_len, expected->input,
1838 expected->input_len) <= 0
1839 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1840 t->err = "KEYOP_LENGTH_ERROR";
1843 if (expected->keyop(copy, got, &got_len, expected->input,
1844 expected->input_len) <= 0) {
1845 t->err = "KEYOP_ERROR";
1848 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1849 expected->output, expected->output_len,
1855 EVP_PKEY_CTX_free(copy);
1859 static int sign_test_init(EVP_TEST *t, const char *name)
1861 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1864 static const EVP_TEST_METHOD psign_test_method = {
1872 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1874 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1875 EVP_PKEY_verify_recover);
1878 static const EVP_TEST_METHOD pverify_recover_test_method = {
1880 verify_recover_test_init,
1886 static int decrypt_test_init(EVP_TEST *t, const char *name)
1888 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1892 static const EVP_TEST_METHOD pdecrypt_test_method = {
1900 static int verify_test_init(EVP_TEST *t, const char *name)
1902 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1905 static int verify_test_run(EVP_TEST *t)
1907 PKEY_DATA *kdata = t->data;
1909 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1910 kdata->input, kdata->input_len) <= 0)
1911 t->err = "VERIFY_ERROR";
1915 static const EVP_TEST_METHOD pverify_test_method = {
1923 static int pderive_test_init(EVP_TEST *t, const char *name)
1925 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1928 static int pderive_test_parse(EVP_TEST *t,
1929 const char *keyword, const char *value)
1931 PKEY_DATA *kdata = t->data;
1934 if (strcmp(keyword, "PeerKeyValidate") == 0)
1937 if (validate || strcmp(keyword, "PeerKey") == 0) {
1939 if (find_key(&peer, value, public_keys) == 0)
1941 if (EVP_PKEY_derive_set_peer_ex(kdata->ctx, peer, validate) <= 0) {
1942 t->err = "DERIVE_SET_PEER_ERROR";
1948 if (strcmp(keyword, "SharedSecret") == 0)
1949 return parse_bin(value, &kdata->output, &kdata->output_len);
1950 if (strcmp(keyword, "Ctrl") == 0)
1951 return pkey_test_ctrl(t, kdata->ctx, value);
1952 if (strcmp(keyword, "KDFType") == 0) {
1953 OSSL_PARAM params[2];
1955 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_EXCHANGE_PARAM_KDF_TYPE,
1957 params[1] = OSSL_PARAM_construct_end();
1958 if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0)
1962 if (strcmp(keyword, "KDFDigest") == 0) {
1963 OSSL_PARAM params[2];
1965 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_EXCHANGE_PARAM_KDF_DIGEST,
1967 params[1] = OSSL_PARAM_construct_end();
1968 if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0)
1972 if (strcmp(keyword, "CEKAlg") == 0) {
1973 OSSL_PARAM params[2];
1975 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CEK_ALG,
1977 params[1] = OSSL_PARAM_construct_end();
1978 if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0)
1982 if (strcmp(keyword, "KDFOutlen") == 0) {
1983 OSSL_PARAM params[2];
1985 size_t outlen = (size_t)strtoul(value, &endptr, 0);
1987 if (endptr[0] != '\0')
1990 params[0] = OSSL_PARAM_construct_size_t(OSSL_EXCHANGE_PARAM_KDF_OUTLEN,
1992 params[1] = OSSL_PARAM_construct_end();
1993 if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0)
2000 static int pderive_test_run(EVP_TEST *t)
2002 EVP_PKEY_CTX *dctx = NULL;
2003 PKEY_DATA *expected = t->data;
2004 unsigned char *got = NULL;
2007 if (!TEST_ptr(dctx = EVP_PKEY_CTX_dup(expected->ctx))) {
2008 t->err = "DERIVE_ERROR";
2012 if (EVP_PKEY_derive(dctx, NULL, &got_len) <= 0
2013 || !TEST_size_t_ne(got_len, 0)) {
2014 t->err = "DERIVE_ERROR";
2017 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2018 t->err = "DERIVE_ERROR";
2021 if (EVP_PKEY_derive(dctx, got, &got_len) <= 0) {
2022 t->err = "DERIVE_ERROR";
2025 if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH",
2026 expected->output, expected->output_len,
2033 EVP_PKEY_CTX_free(dctx);
2037 static const EVP_TEST_METHOD pderive_test_method = {
2050 typedef enum pbe_type_enum {
2051 PBE_TYPE_INVALID = 0,
2052 PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
2055 typedef struct pbe_data_st {
2057 /* scrypt parameters */
2058 uint64_t N, r, p, maxmem;
2059 /* PKCS#12 parameters */
2063 unsigned char *pass;
2066 unsigned char *salt;
2068 /* Expected output */
2073 #ifndef OPENSSL_NO_SCRYPT
2074 /* Parse unsigned decimal 64 bit integer value */
2075 static int parse_uint64(const char *value, uint64_t *pr)
2077 const char *p = value;
2079 if (!TEST_true(*p)) {
2080 TEST_info("Invalid empty integer value");
2083 for (*pr = 0; *p; ) {
2084 if (*pr > UINT64_MAX / 10) {
2085 TEST_error("Integer overflow in string %s", value);
2089 if (!TEST_true(isdigit((unsigned char)*p))) {
2090 TEST_error("Invalid character in string %s", value);
2099 static int scrypt_test_parse(EVP_TEST *t,
2100 const char *keyword, const char *value)
2102 PBE_DATA *pdata = t->data;
2104 if (strcmp(keyword, "N") == 0)
2105 return parse_uint64(value, &pdata->N);
2106 if (strcmp(keyword, "p") == 0)
2107 return parse_uint64(value, &pdata->p);
2108 if (strcmp(keyword, "r") == 0)
2109 return parse_uint64(value, &pdata->r);
2110 if (strcmp(keyword, "maxmem") == 0)
2111 return parse_uint64(value, &pdata->maxmem);
2116 static int pbkdf2_test_parse(EVP_TEST *t,
2117 const char *keyword, const char *value)
2119 PBE_DATA *pdata = t->data;
2121 if (strcmp(keyword, "iter") == 0) {
2122 pdata->iter = atoi(value);
2123 if (pdata->iter <= 0)
2127 if (strcmp(keyword, "MD") == 0) {
2128 pdata->md = EVP_get_digestbyname(value);
2129 if (pdata->md == NULL)
2136 static int pkcs12_test_parse(EVP_TEST *t,
2137 const char *keyword, const char *value)
2139 PBE_DATA *pdata = t->data;
2141 if (strcmp(keyword, "id") == 0) {
2142 pdata->id = atoi(value);
2147 return pbkdf2_test_parse(t, keyword, value);
2150 static int pbe_test_init(EVP_TEST *t, const char *alg)
2153 PBE_TYPE pbe_type = PBE_TYPE_INVALID;
2155 if (is_kdf_disabled(alg)) {
2156 TEST_info("skipping, '%s' is disabled", alg);
2160 if (strcmp(alg, "scrypt") == 0) {
2161 pbe_type = PBE_TYPE_SCRYPT;
2162 } else if (strcmp(alg, "pbkdf2") == 0) {
2163 pbe_type = PBE_TYPE_PBKDF2;
2164 } else if (strcmp(alg, "pkcs12") == 0) {
2165 pbe_type = PBE_TYPE_PKCS12;
2167 TEST_error("Unknown pbe algorithm %s", alg);
2170 if (!TEST_ptr(pdat = OPENSSL_zalloc(sizeof(*pdat))))
2172 pdat->pbe_type = pbe_type;
2177 static void pbe_test_cleanup(EVP_TEST *t)
2179 PBE_DATA *pdat = t->data;
2181 OPENSSL_free(pdat->pass);
2182 OPENSSL_free(pdat->salt);
2183 OPENSSL_free(pdat->key);
2186 static int pbe_test_parse(EVP_TEST *t,
2187 const char *keyword, const char *value)
2189 PBE_DATA *pdata = t->data;
2191 if (strcmp(keyword, "Password") == 0)
2192 return parse_bin(value, &pdata->pass, &pdata->pass_len);
2193 if (strcmp(keyword, "Salt") == 0)
2194 return parse_bin(value, &pdata->salt, &pdata->salt_len);
2195 if (strcmp(keyword, "Key") == 0)
2196 return parse_bin(value, &pdata->key, &pdata->key_len);
2197 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
2198 return pbkdf2_test_parse(t, keyword, value);
2199 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
2200 return pkcs12_test_parse(t, keyword, value);
2201 #ifndef OPENSSL_NO_SCRYPT
2202 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
2203 return scrypt_test_parse(t, keyword, value);
2208 static int pbe_test_run(EVP_TEST *t)
2210 PBE_DATA *expected = t->data;
2212 EVP_MD *fetched_digest = NULL;
2213 OSSL_LIB_CTX *save_libctx;
2215 save_libctx = OSSL_LIB_CTX_set0_default(libctx);
2217 if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
2218 t->err = "INTERNAL_ERROR";
2221 if (expected->pbe_type == PBE_TYPE_PBKDF2) {
2222 if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
2223 expected->salt, expected->salt_len,
2224 expected->iter, expected->md,
2225 expected->key_len, key) == 0) {
2226 t->err = "PBKDF2_ERROR";
2229 #ifndef OPENSSL_NO_SCRYPT
2230 } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
2231 if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
2232 expected->salt, expected->salt_len,
2233 expected->N, expected->r, expected->p,
2234 expected->maxmem, key, expected->key_len) == 0) {
2235 t->err = "SCRYPT_ERROR";
2239 } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
2240 fetched_digest = EVP_MD_fetch(libctx, EVP_MD_get0_name(expected->md),
2242 if (fetched_digest == NULL) {
2243 t->err = "PKCS12_ERROR";
2246 if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
2247 expected->salt, expected->salt_len,
2248 expected->id, expected->iter, expected->key_len,
2249 key, fetched_digest) == 0) {
2250 t->err = "PKCS12_ERROR";
2254 if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len,
2255 key, expected->key_len))
2260 EVP_MD_free(fetched_digest);
2262 OSSL_LIB_CTX_set0_default(save_libctx);
2266 static const EVP_TEST_METHOD pbe_test_method = {
2280 BASE64_CANONICAL_ENCODING = 0,
2281 BASE64_VALID_ENCODING = 1,
2282 BASE64_INVALID_ENCODING = 2
2283 } base64_encoding_type;
2285 typedef struct encode_data_st {
2286 /* Input to encoding */
2287 unsigned char *input;
2289 /* Expected output */
2290 unsigned char *output;
2292 base64_encoding_type encoding;
2295 static int encode_test_init(EVP_TEST *t, const char *encoding)
2299 if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
2301 if (strcmp(encoding, "canonical") == 0) {
2302 edata->encoding = BASE64_CANONICAL_ENCODING;
2303 } else if (strcmp(encoding, "valid") == 0) {
2304 edata->encoding = BASE64_VALID_ENCODING;
2305 } else if (strcmp(encoding, "invalid") == 0) {
2306 edata->encoding = BASE64_INVALID_ENCODING;
2307 if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
2310 TEST_error("Bad encoding: %s."
2311 " Should be one of {canonical, valid, invalid}",
2318 OPENSSL_free(edata);
2322 static void encode_test_cleanup(EVP_TEST *t)
2324 ENCODE_DATA *edata = t->data;
2326 OPENSSL_free(edata->input);
2327 OPENSSL_free(edata->output);
2328 memset(edata, 0, sizeof(*edata));
2331 static int encode_test_parse(EVP_TEST *t,
2332 const char *keyword, const char *value)
2334 ENCODE_DATA *edata = t->data;
2336 if (strcmp(keyword, "Input") == 0)
2337 return parse_bin(value, &edata->input, &edata->input_len);
2338 if (strcmp(keyword, "Output") == 0)
2339 return parse_bin(value, &edata->output, &edata->output_len);
2343 static int encode_test_run(EVP_TEST *t)
2345 ENCODE_DATA *expected = t->data;
2346 unsigned char *encode_out = NULL, *decode_out = NULL;
2347 int output_len, chunk_len;
2348 EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL;
2350 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
2351 t->err = "INTERNAL_ERROR";
2355 if (expected->encoding == BASE64_CANONICAL_ENCODING) {
2357 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
2358 || !TEST_ptr(encode_out =
2359 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
2362 EVP_EncodeInit(encode_ctx);
2363 if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
2364 expected->input, expected->input_len)))
2367 output_len = chunk_len;
2369 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
2370 output_len += chunk_len;
2372 if (!memory_err_compare(t, "BAD_ENCODING",
2373 expected->output, expected->output_len,
2374 encode_out, output_len))
2378 if (!TEST_ptr(decode_out =
2379 OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
2382 EVP_DecodeInit(decode_ctx);
2383 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
2384 expected->output_len) < 0) {
2385 t->err = "DECODE_ERROR";
2388 output_len = chunk_len;
2390 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
2391 t->err = "DECODE_ERROR";
2394 output_len += chunk_len;
2396 if (expected->encoding != BASE64_INVALID_ENCODING
2397 && !memory_err_compare(t, "BAD_DECODING",
2398 expected->input, expected->input_len,
2399 decode_out, output_len)) {
2400 t->err = "BAD_DECODING";
2406 OPENSSL_free(encode_out);
2407 OPENSSL_free(decode_out);
2408 EVP_ENCODE_CTX_free(decode_ctx);
2409 EVP_ENCODE_CTX_free(encode_ctx);
2413 static const EVP_TEST_METHOD encode_test_method = {
2416 encode_test_cleanup,
2425 #define MAX_RAND_REPEATS 15
2427 typedef struct rand_data_pass_st {
2428 unsigned char *entropy;
2429 unsigned char *reseed_entropy;
2430 unsigned char *nonce;
2431 unsigned char *pers;
2432 unsigned char *reseed_addin;
2433 unsigned char *addinA;
2434 unsigned char *addinB;
2435 unsigned char *pr_entropyA;
2436 unsigned char *pr_entropyB;
2437 unsigned char *output;
2438 size_t entropy_len, nonce_len, pers_len, addinA_len, addinB_len,
2439 pr_entropyA_len, pr_entropyB_len, output_len, reseed_entropy_len,
2443 typedef struct rand_data_st {
2444 /* Context for this operation */
2446 EVP_RAND_CTX *parent;
2448 int prediction_resistance;
2450 unsigned int generate_bits;
2454 /* Expected output */
2455 RAND_DATA_PASS data[MAX_RAND_REPEATS];
2458 static int rand_test_init(EVP_TEST *t, const char *name)
2462 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
2463 unsigned int strength = 256;
2465 if (!TEST_ptr(rdata = OPENSSL_zalloc(sizeof(*rdata))))
2468 /* TEST-RAND is available in the FIPS provider but not with "fips=yes" */
2469 rand = EVP_RAND_fetch(libctx, "TEST-RAND", "-fips");
2472 rdata->parent = EVP_RAND_CTX_new(rand, NULL);
2473 EVP_RAND_free(rand);
2474 if (rdata->parent == NULL)
2477 *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength);
2478 if (!EVP_RAND_CTX_set_params(rdata->parent, params))
2481 rand = EVP_RAND_fetch(libctx, name, NULL);
2484 rdata->ctx = EVP_RAND_CTX_new(rand, rdata->parent);
2485 EVP_RAND_free(rand);
2486 if (rdata->ctx == NULL)
2493 EVP_RAND_CTX_free(rdata->parent);
2494 OPENSSL_free(rdata);
2498 static void rand_test_cleanup(EVP_TEST *t)
2500 RAND_DATA *rdata = t->data;
2503 OPENSSL_free(rdata->cipher);
2504 OPENSSL_free(rdata->digest);
2506 for (i = 0; i <= rdata->n; i++) {
2507 OPENSSL_free(rdata->data[i].entropy);
2508 OPENSSL_free(rdata->data[i].reseed_entropy);
2509 OPENSSL_free(rdata->data[i].nonce);
2510 OPENSSL_free(rdata->data[i].pers);
2511 OPENSSL_free(rdata->data[i].reseed_addin);
2512 OPENSSL_free(rdata->data[i].addinA);
2513 OPENSSL_free(rdata->data[i].addinB);
2514 OPENSSL_free(rdata->data[i].pr_entropyA);
2515 OPENSSL_free(rdata->data[i].pr_entropyB);
2516 OPENSSL_free(rdata->data[i].output);
2518 EVP_RAND_CTX_free(rdata->ctx);
2519 EVP_RAND_CTX_free(rdata->parent);
2522 static int rand_test_parse(EVP_TEST *t,
2523 const char *keyword, const char *value)
2525 RAND_DATA *rdata = t->data;
2526 RAND_DATA_PASS *item;
2530 if ((p = strchr(keyword, '.')) != NULL) {
2532 if (n >= MAX_RAND_REPEATS)
2536 item = rdata->data + n;
2537 if (HAS_PREFIX(keyword, "Entropy."))
2538 return parse_bin(value, &item->entropy, &item->entropy_len);
2539 if (HAS_PREFIX(keyword, "ReseedEntropy."))
2540 return parse_bin(value, &item->reseed_entropy,
2541 &item->reseed_entropy_len);
2542 if (HAS_PREFIX(keyword, "Nonce."))
2543 return parse_bin(value, &item->nonce, &item->nonce_len);
2544 if (HAS_PREFIX(keyword, "PersonalisationString."))
2545 return parse_bin(value, &item->pers, &item->pers_len);
2546 if (HAS_PREFIX(keyword, "ReseedAdditionalInput."))
2547 return parse_bin(value, &item->reseed_addin,
2548 &item->reseed_addin_len);
2549 if (HAS_PREFIX(keyword, "AdditionalInputA."))
2550 return parse_bin(value, &item->addinA, &item->addinA_len);
2551 if (HAS_PREFIX(keyword, "AdditionalInputB."))
2552 return parse_bin(value, &item->addinB, &item->addinB_len);
2553 if (HAS_PREFIX(keyword, "EntropyPredictionResistanceA."))
2554 return parse_bin(value, &item->pr_entropyA, &item->pr_entropyA_len);
2555 if (HAS_PREFIX(keyword, "EntropyPredictionResistanceB."))
2556 return parse_bin(value, &item->pr_entropyB, &item->pr_entropyB_len);
2557 if (HAS_PREFIX(keyword, "Output."))
2558 return parse_bin(value, &item->output, &item->output_len);
2560 if (strcmp(keyword, "Cipher") == 0)
2561 return TEST_ptr(rdata->cipher = OPENSSL_strdup(value));
2562 if (strcmp(keyword, "Digest") == 0)
2563 return TEST_ptr(rdata->digest = OPENSSL_strdup(value));
2564 if (strcmp(keyword, "DerivationFunction") == 0) {
2565 rdata->use_df = atoi(value) != 0;
2568 if (strcmp(keyword, "GenerateBits") == 0) {
2569 if ((n = atoi(value)) <= 0 || n % 8 != 0)
2571 rdata->generate_bits = (unsigned int)n;
2574 if (strcmp(keyword, "PredictionResistance") == 0) {
2575 rdata->prediction_resistance = atoi(value) != 0;
2582 static int rand_test_run(EVP_TEST *t)
2584 RAND_DATA *expected = t->data;
2585 RAND_DATA_PASS *item;
2587 size_t got_len = expected->generate_bits / 8;
2588 OSSL_PARAM params[5], *p = params;
2589 int i = -1, ret = 0;
2590 unsigned int strength;
2593 if (!TEST_ptr(got = OPENSSL_malloc(got_len)))
2596 *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &expected->use_df);
2597 if (expected->cipher != NULL)
2598 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
2599 expected->cipher, 0);
2600 if (expected->digest != NULL)
2601 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
2602 expected->digest, 0);
2603 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC, "HMAC", 0);
2604 *p = OSSL_PARAM_construct_end();
2605 if (!TEST_true(EVP_RAND_CTX_set_params(expected->ctx, params)))
2608 strength = EVP_RAND_get_strength(expected->ctx);
2609 for (i = 0; i <= expected->n; i++) {
2610 item = expected->data + i;
2613 z = item->entropy != NULL ? item->entropy : (unsigned char *)"";
2614 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
2615 z, item->entropy_len);
2616 z = item->nonce != NULL ? item->nonce : (unsigned char *)"";
2617 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
2618 z, item->nonce_len);
2619 *p = OSSL_PARAM_construct_end();
2620 if (!TEST_true(EVP_RAND_instantiate(expected->parent, strength,
2621 0, NULL, 0, params)))
2624 z = item->pers != NULL ? item->pers : (unsigned char *)"";
2625 if (!TEST_true(EVP_RAND_instantiate
2626 (expected->ctx, strength,
2627 expected->prediction_resistance, z,
2628 item->pers_len, NULL)))
2631 if (item->reseed_entropy != NULL) {
2632 params[0] = OSSL_PARAM_construct_octet_string
2633 (OSSL_RAND_PARAM_TEST_ENTROPY, item->reseed_entropy,
2634 item->reseed_entropy_len);
2635 params[1] = OSSL_PARAM_construct_end();
2636 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2639 if (!TEST_true(EVP_RAND_reseed
2640 (expected->ctx, expected->prediction_resistance,
2641 NULL, 0, item->reseed_addin,
2642 item->reseed_addin_len)))
2645 if (item->pr_entropyA != NULL) {
2646 params[0] = OSSL_PARAM_construct_octet_string
2647 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyA,
2648 item->pr_entropyA_len);
2649 params[1] = OSSL_PARAM_construct_end();
2650 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2653 if (!TEST_true(EVP_RAND_generate
2654 (expected->ctx, got, got_len,
2655 strength, expected->prediction_resistance,
2656 item->addinA, item->addinA_len)))
2659 if (item->pr_entropyB != NULL) {
2660 params[0] = OSSL_PARAM_construct_octet_string
2661 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyB,
2662 item->pr_entropyB_len);
2663 params[1] = OSSL_PARAM_construct_end();
2664 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2667 if (!TEST_true(EVP_RAND_generate
2668 (expected->ctx, got, got_len,
2669 strength, expected->prediction_resistance,
2670 item->addinB, item->addinB_len)))
2672 if (!TEST_mem_eq(got, got_len, item->output, item->output_len))
2674 if (!TEST_true(EVP_RAND_uninstantiate(expected->ctx))
2675 || !TEST_true(EVP_RAND_uninstantiate(expected->parent))
2676 || !TEST_true(EVP_RAND_verify_zeroization(expected->ctx))
2677 || !TEST_int_eq(EVP_RAND_get_state(expected->ctx),
2678 EVP_RAND_STATE_UNINITIALISED))
2685 if (ret == 0 && i >= 0)
2686 TEST_info("Error in test case %d of %d\n", i, expected->n + 1);
2691 static const EVP_TEST_METHOD rand_test_method = {
2703 typedef struct kdf_data_st {
2704 /* Context for this operation */
2706 /* Expected output */
2707 unsigned char *output;
2709 OSSL_PARAM params[20];
2714 * Perform public key operation setup: lookup key, allocated ctx and call
2715 * the appropriate initialisation function
2717 static int kdf_test_init(EVP_TEST *t, const char *name)
2722 if (is_kdf_disabled(name)) {
2723 TEST_info("skipping, '%s' is disabled", name);
2728 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2730 kdata->p = kdata->params;
2731 *kdata->p = OSSL_PARAM_construct_end();
2733 kdf = EVP_KDF_fetch(libctx, name, NULL);
2735 OPENSSL_free(kdata);
2738 kdata->ctx = EVP_KDF_CTX_new(kdf);
2740 if (kdata->ctx == NULL) {
2741 OPENSSL_free(kdata);
2748 static void kdf_test_cleanup(EVP_TEST *t)
2750 KDF_DATA *kdata = t->data;
2753 for (p = kdata->params; p->key != NULL; p++)
2754 OPENSSL_free(p->data);
2755 OPENSSL_free(kdata->output);
2756 EVP_KDF_CTX_free(kdata->ctx);
2759 static int kdf_test_ctrl(EVP_TEST *t, EVP_KDF_CTX *kctx,
2762 KDF_DATA *kdata = t->data;
2765 const OSSL_PARAM *defs = EVP_KDF_settable_ctx_params(EVP_KDF_CTX_kdf(kctx));
2767 if (!TEST_ptr(name = OPENSSL_strdup(value)))
2769 p = strchr(name, ':');
2773 if (strcmp(name, "r") == 0
2774 && OSSL_PARAM_locate_const(defs, name) == NULL) {
2775 TEST_info("skipping, setting 'r' is unsupported");
2780 rv = OSSL_PARAM_allocate_from_text(kdata->p, defs, name, p,
2781 p != NULL ? strlen(p) : 0, NULL);
2782 *++kdata->p = OSSL_PARAM_construct_end();
2784 t->err = "KDF_PARAM_ERROR";
2788 if (p != NULL && strcmp(name, "digest") == 0) {
2789 if (is_digest_disabled(p)) {
2790 TEST_info("skipping, '%s' is disabled", p);
2796 && (strcmp(name, "cipher") == 0
2797 || strcmp(name, "cekalg") == 0)
2798 && is_cipher_disabled(p)) {
2799 TEST_info("skipping, '%s' is disabled", p);
2804 && (strcmp(name, "mac") == 0)
2805 && is_mac_disabled(p)) {
2806 TEST_info("skipping, '%s' is disabled", p);
2814 static int kdf_test_parse(EVP_TEST *t,
2815 const char *keyword, const char *value)
2817 KDF_DATA *kdata = t->data;
2819 if (strcmp(keyword, "Output") == 0)
2820 return parse_bin(value, &kdata->output, &kdata->output_len);
2821 if (HAS_PREFIX(keyword, "Ctrl"))
2822 return kdf_test_ctrl(t, kdata->ctx, value);
2826 static int kdf_test_run(EVP_TEST *t)
2828 KDF_DATA *expected = t->data;
2829 unsigned char *got = NULL;
2830 size_t got_len = expected->output_len;
2833 if (!EVP_KDF_CTX_set_params(expected->ctx, expected->params)) {
2834 t->err = "KDF_CTRL_ERROR";
2837 if (!TEST_ptr(got = OPENSSL_malloc(got_len == 0 ? 1 : got_len))) {
2838 t->err = "INTERNAL_ERROR";
2841 /* FIPS(3.0.0): can't dup KDF contexts #17572 */
2842 if (fips_provider_version_gt(libctx, 3, 0, 0)
2843 && (ctx = EVP_KDF_CTX_dup(expected->ctx)) != NULL) {
2844 EVP_KDF_CTX_free(expected->ctx);
2845 expected->ctx = ctx;
2847 if (EVP_KDF_derive(expected->ctx, got, got_len, NULL) <= 0) {
2848 t->err = "KDF_DERIVE_ERROR";
2851 if (!memory_err_compare(t, "KDF_MISMATCH",
2852 expected->output, expected->output_len,
2863 static const EVP_TEST_METHOD kdf_test_method = {
2875 typedef struct pkey_kdf_data_st {
2876 /* Context for this operation */
2878 /* Expected output */
2879 unsigned char *output;
2884 * Perform public key operation setup: lookup key, allocated ctx and call
2885 * the appropriate initialisation function
2887 static int pkey_kdf_test_init(EVP_TEST *t, const char *name)
2889 PKEY_KDF_DATA *kdata = NULL;
2891 if (is_kdf_disabled(name)) {
2892 TEST_info("skipping, '%s' is disabled", name);
2897 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2900 kdata->ctx = EVP_PKEY_CTX_new_from_name(libctx, name, NULL);
2901 if (kdata->ctx == NULL
2902 || EVP_PKEY_derive_init(kdata->ctx) <= 0)
2908 EVP_PKEY_CTX_free(kdata->ctx);
2909 OPENSSL_free(kdata);
2913 static void pkey_kdf_test_cleanup(EVP_TEST *t)
2915 PKEY_KDF_DATA *kdata = t->data;
2917 OPENSSL_free(kdata->output);
2918 EVP_PKEY_CTX_free(kdata->ctx);
2921 static int pkey_kdf_test_parse(EVP_TEST *t,
2922 const char *keyword, const char *value)
2924 PKEY_KDF_DATA *kdata = t->data;
2926 if (strcmp(keyword, "Output") == 0)
2927 return parse_bin(value, &kdata->output, &kdata->output_len);
2928 if (HAS_PREFIX(keyword, "Ctrl"))
2929 return pkey_test_ctrl(t, kdata->ctx, value);
2933 static int pkey_kdf_test_run(EVP_TEST *t)
2935 PKEY_KDF_DATA *expected = t->data;
2936 unsigned char *got = NULL;
2939 if (fips_provider_version_eq(libctx, 3, 0, 0)) {
2940 /* FIPS(3.0.0): can't deal with oversized output buffers #18533 */
2941 got_len = expected->output_len;
2943 /* Find out the KDF output size */
2944 if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) {
2945 t->err = "INTERNAL_ERROR";
2950 * We may get an absurd output size, which signals that anything goes.
2951 * If not, we specify a too big buffer for the output, to test that
2952 * EVP_PKEY_derive() can cope with it.
2954 if (got_len == SIZE_MAX || got_len == 0)
2955 got_len = expected->output_len;
2957 got_len = expected->output_len * 2;
2960 if (!TEST_ptr(got = OPENSSL_malloc(got_len == 0 ? 1 : got_len))) {
2961 t->err = "INTERNAL_ERROR";
2964 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
2965 t->err = "KDF_DERIVE_ERROR";
2968 if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
2969 t->err = "KDF_MISMATCH";
2979 static const EVP_TEST_METHOD pkey_kdf_test_method = {
2982 pkey_kdf_test_cleanup,
2983 pkey_kdf_test_parse,
2991 typedef struct keypair_test_data_st {
2994 } KEYPAIR_TEST_DATA;
2996 static int keypair_test_init(EVP_TEST *t, const char *pair)
2998 KEYPAIR_TEST_DATA *data;
3000 EVP_PKEY *pk = NULL, *pubk = NULL;
3001 char *pub, *priv = NULL;
3003 /* Split private and public names. */
3004 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
3005 || !TEST_ptr(pub = strchr(priv, ':'))) {
3006 t->err = "PARSING_ERROR";
3011 if (!TEST_true(find_key(&pk, priv, private_keys))) {
3012 TEST_info("Can't find private key: %s", priv);
3013 t->err = "MISSING_PRIVATE_KEY";
3016 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
3017 TEST_info("Can't find public key: %s", pub);
3018 t->err = "MISSING_PUBLIC_KEY";
3022 if (pk == NULL && pubk == NULL) {
3023 /* Both keys are listed but unsupported: skip this test */
3029 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
3042 static void keypair_test_cleanup(EVP_TEST *t)
3044 OPENSSL_free(t->data);
3049 * For tests that do not accept any custom keywords.
3051 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
3056 static int keypair_test_run(EVP_TEST *t)
3059 const KEYPAIR_TEST_DATA *pair = t->data;
3061 if (pair->privk == NULL || pair->pubk == NULL) {
3063 * this can only happen if only one of the keys is not set
3064 * which means that one of them was unsupported while the
3065 * other isn't: hence a key type mismatch.
3067 t->err = "KEYPAIR_TYPE_MISMATCH";
3072 if ((rv = EVP_PKEY_eq(pair->privk, pair->pubk)) != 1) {
3074 t->err = "KEYPAIR_MISMATCH";
3075 } else if (-1 == rv) {
3076 t->err = "KEYPAIR_TYPE_MISMATCH";
3077 } else if (-2 == rv) {
3078 t->err = "UNSUPPORTED_KEY_COMPARISON";
3080 TEST_error("Unexpected error in key comparison");
3095 static const EVP_TEST_METHOD keypair_test_method = {
3098 keypair_test_cleanup,
3107 typedef struct keygen_test_data_st {
3108 EVP_PKEY_CTX *genctx; /* Keygen context to use */
3109 char *keyname; /* Key name to store key or NULL */
3112 static int keygen_test_init(EVP_TEST *t, const char *alg)
3114 KEYGEN_TEST_DATA *data;
3115 EVP_PKEY_CTX *genctx;
3116 int nid = OBJ_sn2nid(alg);
3118 if (nid == NID_undef) {
3119 nid = OBJ_ln2nid(alg);
3120 if (nid == NID_undef)
3124 if (is_pkey_disabled(alg)) {
3128 if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_from_name(libctx, alg, NULL)))
3131 if (EVP_PKEY_keygen_init(genctx) <= 0) {
3132 t->err = "KEYGEN_INIT_ERROR";
3136 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
3138 data->genctx = genctx;
3139 data->keyname = NULL;
3145 EVP_PKEY_CTX_free(genctx);
3149 static void keygen_test_cleanup(EVP_TEST *t)
3151 KEYGEN_TEST_DATA *keygen = t->data;
3153 EVP_PKEY_CTX_free(keygen->genctx);
3154 OPENSSL_free(keygen->keyname);
3155 OPENSSL_free(t->data);
3159 static int keygen_test_parse(EVP_TEST *t,
3160 const char *keyword, const char *value)
3162 KEYGEN_TEST_DATA *keygen = t->data;
3164 if (strcmp(keyword, "KeyName") == 0)
3165 return TEST_ptr(keygen->keyname = OPENSSL_strdup(value));
3166 if (strcmp(keyword, "Ctrl") == 0)
3167 return pkey_test_ctrl(t, keygen->genctx, value);
3171 static int keygen_test_run(EVP_TEST *t)
3173 KEYGEN_TEST_DATA *keygen = t->data;
3174 EVP_PKEY *pkey = NULL;
3177 if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) {
3178 t->err = "KEYGEN_GENERATE_ERROR";
3182 if (!evp_pkey_is_provided(pkey)) {
3183 TEST_info("Warning: legacy key generated %s", keygen->keyname);
3186 if (keygen->keyname != NULL) {
3190 if (find_key(NULL, keygen->keyname, private_keys)) {
3191 TEST_info("Duplicate key %s", keygen->keyname);
3195 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
3197 key->name = keygen->keyname;
3198 keygen->keyname = NULL;
3200 key->next = private_keys;
3204 EVP_PKEY_free(pkey);
3213 static const EVP_TEST_METHOD keygen_test_method = {
3216 keygen_test_cleanup,
3222 ** DIGEST SIGN+VERIFY TESTS
3226 int is_verify; /* Set to 1 if verifying */
3227 int is_oneshot; /* Set to 1 for one shot operation */
3228 const EVP_MD *md; /* Digest to use */
3229 EVP_MD_CTX *ctx; /* Digest context */
3231 STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
3232 unsigned char *osin; /* Input data if one shot */
3233 size_t osin_len; /* Input length data if one shot */
3234 unsigned char *output; /* Expected output */
3235 size_t output_len; /* Expected output length */
3236 const char *nonce_type;
3239 static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
3242 const EVP_MD *md = NULL;
3243 DIGESTSIGN_DATA *mdat;
3245 if (strcmp(alg, "NULL") != 0) {
3246 if (is_digest_disabled(alg)) {
3250 md = EVP_get_digestbyname(alg);
3254 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
3257 if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
3261 mdat->is_verify = is_verify;
3262 mdat->is_oneshot = is_oneshot;
3267 static int digestsign_test_init(EVP_TEST *t, const char *alg)
3269 return digestsigver_test_init(t, alg, 0, 0);
3272 static void digestsigver_test_cleanup(EVP_TEST *t)
3274 DIGESTSIGN_DATA *mdata = t->data;
3276 EVP_MD_CTX_free(mdata->ctx);
3277 sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
3278 OPENSSL_free(mdata->osin);
3279 OPENSSL_free(mdata->output);
3280 OPENSSL_free(mdata);
3284 static int digestsigver_test_parse(EVP_TEST *t,
3285 const char *keyword, const char *value)
3287 DIGESTSIGN_DATA *mdata = t->data;
3289 if (strcmp(keyword, "Key") == 0) {
3290 EVP_PKEY *pkey = NULL;
3292 const char *name = mdata->md == NULL ? NULL : EVP_MD_get0_name(mdata->md);
3294 if (mdata->is_verify)
3295 rv = find_key(&pkey, value, public_keys);
3297 rv = find_key(&pkey, value, private_keys);
3298 if (rv == 0 || pkey == NULL) {
3302 if (mdata->is_verify) {
3303 if (!EVP_DigestVerifyInit_ex(mdata->ctx, &mdata->pctx, name, libctx,
3305 t->err = "DIGESTVERIFYINIT_ERROR";
3308 if (!EVP_DigestSignInit_ex(mdata->ctx, &mdata->pctx, name, libctx, NULL,
3310 t->err = "DIGESTSIGNINIT_ERROR";
3314 if (strcmp(keyword, "Input") == 0) {
3315 if (mdata->is_oneshot)
3316 return parse_bin(value, &mdata->osin, &mdata->osin_len);
3317 return evp_test_buffer_append(value, &mdata->input);
3319 if (strcmp(keyword, "Output") == 0)
3320 return parse_bin(value, &mdata->output, &mdata->output_len);
3322 if (!mdata->is_oneshot) {
3323 if (strcmp(keyword, "Count") == 0)
3324 return evp_test_buffer_set_count(value, mdata->input);
3325 if (strcmp(keyword, "Ncopy") == 0)
3326 return evp_test_buffer_ncopy(value, mdata->input);
3328 if (strcmp(keyword, "Ctrl") == 0) {
3329 if (mdata->pctx == NULL)
3331 return pkey_test_ctrl(t, mdata->pctx, value);
3333 if (strcmp(keyword, "NonceType") == 0) {
3334 if (strcmp(value, "deterministic") == 0) {
3335 OSSL_PARAM params[2];
3336 unsigned int nonce_type = 1;
3339 OSSL_PARAM_construct_uint(OSSL_SIGNATURE_PARAM_NONCE_TYPE,
3341 params[1] = OSSL_PARAM_construct_end();
3342 if (!EVP_PKEY_CTX_set_params(mdata->pctx, params))
3343 t->err = "EVP_PKEY_CTX_set_params_ERROR";
3350 static int digestsign_update_fn(void *ctx, const unsigned char *buf,
3353 return EVP_DigestSignUpdate(ctx, buf, buflen);
3356 static int digestsign_test_run(EVP_TEST *t)
3358 DIGESTSIGN_DATA *expected = t->data;
3359 unsigned char *got = NULL;
3362 if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
3364 t->err = "DIGESTUPDATE_ERROR";
3368 if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
3369 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
3372 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
3373 t->err = "MALLOC_FAILURE";
3377 if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
3378 t->err = "DIGESTSIGNFINAL_ERROR";
3381 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
3382 expected->output, expected->output_len,
3392 static const EVP_TEST_METHOD digestsign_test_method = {
3394 digestsign_test_init,
3395 digestsigver_test_cleanup,
3396 digestsigver_test_parse,
3400 static int digestverify_test_init(EVP_TEST *t, const char *alg)
3402 return digestsigver_test_init(t, alg, 1, 0);
3405 static int digestverify_update_fn(void *ctx, const unsigned char *buf,
3408 return EVP_DigestVerifyUpdate(ctx, buf, buflen);
3411 static int digestverify_test_run(EVP_TEST *t)
3413 DIGESTSIGN_DATA *mdata = t->data;
3415 if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
3416 t->err = "DIGESTUPDATE_ERROR";
3420 if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
3421 mdata->output_len) <= 0)
3422 t->err = "VERIFY_ERROR";
3426 static const EVP_TEST_METHOD digestverify_test_method = {
3428 digestverify_test_init,
3429 digestsigver_test_cleanup,
3430 digestsigver_test_parse,
3431 digestverify_test_run
3434 static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
3436 return digestsigver_test_init(t, alg, 0, 1);
3439 static int oneshot_digestsign_test_run(EVP_TEST *t)
3441 DIGESTSIGN_DATA *expected = t->data;
3442 unsigned char *got = NULL;
3445 if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
3446 expected->osin, expected->osin_len)) {
3447 t->err = "DIGESTSIGN_LENGTH_ERROR";
3450 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
3451 t->err = "MALLOC_FAILURE";
3455 if (!EVP_DigestSign(expected->ctx, got, &got_len,
3456 expected->osin, expected->osin_len)) {
3457 t->err = "DIGESTSIGN_ERROR";
3460 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
3461 expected->output, expected->output_len,
3471 static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
3472 "OneShotDigestSign",
3473 oneshot_digestsign_test_init,
3474 digestsigver_test_cleanup,
3475 digestsigver_test_parse,
3476 oneshot_digestsign_test_run
3479 static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
3481 return digestsigver_test_init(t, alg, 1, 1);
3484 static int oneshot_digestverify_test_run(EVP_TEST *t)
3486 DIGESTSIGN_DATA *mdata = t->data;
3488 if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
3489 mdata->osin, mdata->osin_len) <= 0)
3490 t->err = "VERIFY_ERROR";
3494 static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
3495 "OneShotDigestVerify",
3496 oneshot_digestverify_test_init,
3497 digestsigver_test_cleanup,
3498 digestsigver_test_parse,
3499 oneshot_digestverify_test_run
3504 ** PARSING AND DISPATCH
3507 static const EVP_TEST_METHOD *evp_test_list[] = {
3509 &cipher_test_method,
3510 &digest_test_method,
3511 &digestsign_test_method,
3512 &digestverify_test_method,
3513 &encode_test_method,
3515 &pkey_kdf_test_method,
3516 &keypair_test_method,
3517 &keygen_test_method,
3519 &oneshot_digestsign_test_method,
3520 &oneshot_digestverify_test_method,
3522 &pdecrypt_test_method,
3523 &pderive_test_method,
3525 &pverify_recover_test_method,
3526 &pverify_test_method,
3530 static const EVP_TEST_METHOD *find_test(const char *name)
3532 const EVP_TEST_METHOD **tt;
3534 for (tt = evp_test_list; *tt; tt++) {
3535 if (strcmp(name, (*tt)->name) == 0)
3541 static void clear_test(EVP_TEST *t)
3543 test_clearstanza(&t->s);
3545 if (t->data != NULL) {
3546 if (t->meth != NULL)
3547 t->meth->cleanup(t);
3548 OPENSSL_free(t->data);
3551 OPENSSL_free(t->expected_err);
3552 t->expected_err = NULL;
3553 OPENSSL_free(t->reason);
3562 /* Check for errors in the test structure; return 1 if okay, else 0. */
3563 static int check_test_error(EVP_TEST *t)
3568 if (t->err == NULL && t->expected_err == NULL)
3570 if (t->err != NULL && t->expected_err == NULL) {
3571 if (t->aux_err != NULL) {
3572 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
3573 t->s.test_file, t->s.start, t->aux_err, t->err);
3575 TEST_info("%s:%d: Source of above error; unexpected error %s",
3576 t->s.test_file, t->s.start, t->err);
3580 if (t->err == NULL && t->expected_err != NULL) {
3581 TEST_info("%s:%d: Succeeded but was expecting %s",
3582 t->s.test_file, t->s.start, t->expected_err);
3586 if (strcmp(t->err, t->expected_err) != 0) {
3587 TEST_info("%s:%d: Expected %s got %s",
3588 t->s.test_file, t->s.start, t->expected_err, t->err);
3592 if (t->reason == NULL)
3595 if (t->reason == NULL) {
3596 TEST_info("%s:%d: Test is missing function or reason code",
3597 t->s.test_file, t->s.start);
3601 err = ERR_peek_error();
3603 TEST_info("%s:%d: Expected error \"%s\" not set",
3604 t->s.test_file, t->s.start, t->reason);
3608 reason = ERR_reason_error_string(err);
3609 if (reason == NULL) {
3610 TEST_info("%s:%d: Expected error \"%s\", no strings available."
3612 t->s.test_file, t->s.start, t->reason);
3616 if (strcmp(reason, t->reason) == 0)
3619 TEST_info("%s:%d: Expected error \"%s\", got \"%s\"",
3620 t->s.test_file, t->s.start, t->reason, reason);
3625 /* Run a parsed test. Log a message and return 0 on error. */
3626 static int run_test(EVP_TEST *t)
3628 if (t->meth == NULL)
3635 if (t->err == NULL && t->meth->run_test(t) != 1) {
3636 TEST_info("%s:%d %s error",
3637 t->s.test_file, t->s.start, t->meth->name);
3640 if (!check_test_error(t)) {
3641 TEST_openssl_errors();
3650 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
3652 for (; lst != NULL; lst = lst->next) {
3653 if (strcmp(lst->name, name) == 0) {
3662 static void free_key_list(KEY_LIST *lst)
3664 while (lst != NULL) {
3665 KEY_LIST *next = lst->next;
3667 EVP_PKEY_free(lst->key);
3668 OPENSSL_free(lst->name);
3675 * Is the key type an unsupported algorithm?
3677 static int key_unsupported(void)
3679 long err = ERR_peek_last_error();
3680 int lib = ERR_GET_LIB(err);
3681 long reason = ERR_GET_REASON(err);
3683 if ((lib == ERR_LIB_EVP && reason == EVP_R_UNSUPPORTED_ALGORITHM)
3684 || (lib == ERR_LIB_EVP && reason == EVP_R_DECODE_ERROR)
3685 || reason == ERR_R_UNSUPPORTED) {
3689 #ifndef OPENSSL_NO_EC
3691 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
3692 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
3695 if (lib == ERR_LIB_EC
3696 && (reason == EC_R_UNKNOWN_GROUP
3697 || reason == EC_R_INVALID_CURVE)) {
3701 #endif /* OPENSSL_NO_EC */
3705 /* NULL out the value from |pp| but return it. This "steals" a pointer. */
3706 static char *take_value(PAIR *pp)
3708 char *p = pp->value;
3714 #if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
3715 static int securitycheck_enabled(void)
3717 static int enabled = -1;
3719 if (enabled == -1) {
3720 if (OSSL_PROVIDER_available(libctx, "fips")) {
3721 OSSL_PARAM params[2];
3722 OSSL_PROVIDER *prov = NULL;
3725 prov = OSSL_PROVIDER_load(libctx, "fips");
3728 OSSL_PARAM_construct_int(OSSL_PROV_PARAM_SECURITY_CHECKS,
3730 params[1] = OSSL_PARAM_construct_end();
3731 OSSL_PROVIDER_get_params(prov, params);
3732 OSSL_PROVIDER_unload(prov);
3744 * Return 1 if one of the providers named in the string is available.
3745 * The provider names are separated with whitespace.
3746 * NOTE: destructive function, it inserts '\0' after each provider name.
3748 static int prov_available(char *providers)
3754 for (; isspace(*providers); providers++)
3756 if (*providers == '\0')
3757 break; /* End of the road */
3758 for (p = providers; *p != '\0' && !isspace(*p); p++)
3764 if (OSSL_PROVIDER_available(libctx, providers))
3765 return 1; /* Found one */
3770 /* Read and parse one test. Return 0 if failure, 1 if okay. */
3771 static int parse(EVP_TEST *t)
3773 KEY_LIST *key, **klist;
3776 int i, j, skipped = 0;
3780 if (BIO_eof(t->s.fp))
3783 if (!test_readstanza(&t->s))
3785 } while (t->s.numpairs == 0);
3786 pp = &t->s.pairs[0];
3788 /* Are we adding a key? */
3792 if (strcmp(pp->key, "PrivateKey") == 0) {
3793 pkey = PEM_read_bio_PrivateKey_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3794 if (pkey == NULL && !key_unsupported()) {
3795 EVP_PKEY_free(pkey);
3796 TEST_info("Can't read private key %s", pp->value);
3797 TEST_openssl_errors();
3800 klist = &private_keys;
3801 } else if (strcmp(pp->key, "PublicKey") == 0) {
3802 pkey = PEM_read_bio_PUBKEY_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3803 if (pkey == NULL && !key_unsupported()) {
3804 EVP_PKEY_free(pkey);
3805 TEST_info("Can't read public key %s", pp->value);
3806 TEST_openssl_errors();
3809 klist = &public_keys;
3810 } else if (strcmp(pp->key, "PrivateKeyRaw") == 0
3811 || strcmp(pp->key, "PublicKeyRaw") == 0) {
3812 char *strnid = NULL, *keydata = NULL;
3813 unsigned char *keybin;
3817 if (strcmp(pp->key, "PrivateKeyRaw") == 0)
3818 klist = &private_keys;
3820 klist = &public_keys;
3822 strnid = strchr(pp->value, ':');
3823 if (strnid != NULL) {
3825 keydata = strchr(strnid, ':');
3826 if (keydata != NULL)
3829 if (keydata == NULL) {
3830 TEST_info("Failed to parse %s value", pp->key);
3834 nid = OBJ_txt2nid(strnid);
3835 if (nid == NID_undef) {
3836 TEST_info("Unrecognised algorithm NID");
3839 if (!parse_bin(keydata, &keybin, &keylen)) {
3840 TEST_info("Failed to create binary key");
3843 if (klist == &private_keys)
3844 pkey = EVP_PKEY_new_raw_private_key_ex(libctx, strnid, NULL, keybin,
3847 pkey = EVP_PKEY_new_raw_public_key_ex(libctx, strnid, NULL, keybin,
3849 if (pkey == NULL && !key_unsupported()) {
3850 TEST_info("Can't read %s data", pp->key);
3851 OPENSSL_free(keybin);
3852 TEST_openssl_errors();
3855 OPENSSL_free(keybin);
3856 } else if (strcmp(pp->key, "Availablein") == 0) {
3857 if (!prov_available(pp->value)) {
3858 TEST_info("skipping, '%s' provider not available: %s:%d",
3859 pp->value, t->s.test_file, t->s.start);
3866 } else if (strcmp(pp->key, "FIPSversion") == 0) {
3867 if (prov_available("fips")) {
3868 j = fips_provider_version_match(libctx, pp->value);
3870 TEST_info("Line %d: error matching FIPS versions\n", t->s.curr);
3872 } else if (j == 0) {
3873 TEST_info("skipping, FIPS provider incompatible version: %s:%d",
3874 t->s.test_file, t->s.start);
3884 /* If we have a key add to list */
3885 if (klist != NULL) {
3886 if (find_key(NULL, pp->value, *klist)) {
3887 TEST_info("Duplicate key %s", pp->value);
3890 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
3892 key->name = take_value(pp);
3897 /* Go back and start a new stanza. */
3898 if ((t->s.numpairs - skipped) != 1)
3899 TEST_info("Line %d: missing blank line\n", t->s.curr);
3903 /* Find the test, based on first keyword. */
3904 if (!TEST_ptr(t->meth = find_test(pp->key)))
3906 if (!t->meth->init(t, pp->value)) {
3907 TEST_error("unknown %s: %s\n", pp->key, pp->value);
3911 /* TEST_info("skipping %s %s", pp->key, pp->value); */
3915 for (pp++, i = 1; i < (t->s.numpairs - skipped); pp++, i++) {
3916 if (strcmp(pp->key, "Securitycheck") == 0) {
3917 #if defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
3919 if (!securitycheck_enabled())
3922 TEST_info("skipping, Securitycheck is disabled: %s:%d",
3923 t->s.test_file, t->s.start);
3927 } else if (strcmp(pp->key, "Availablein") == 0) {
3928 TEST_info("Line %d: 'Availablein' should be the first option",
3931 } else if (strcmp(pp->key, "Result") == 0) {
3932 if (t->expected_err != NULL) {
3933 TEST_info("Line %d: multiple result lines", t->s.curr);
3936 t->expected_err = take_value(pp);
3937 } else if (strcmp(pp->key, "Function") == 0) {
3938 /* Ignore old line. */
3939 } else if (strcmp(pp->key, "Reason") == 0) {
3940 if (t->reason != NULL) {
3941 TEST_info("Line %d: multiple reason lines", t->s.curr);
3944 t->reason = take_value(pp);
3946 /* Must be test specific line: try to parse it */
3947 int rv = t->meth->parse(t, pp->key, pp->value);
3950 TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key);
3954 TEST_info("Line %d: error processing keyword %s = %s\n",
3955 t->s.curr, pp->key, pp->value);
3966 static int run_file_tests(int i)
3969 const char *testfile = test_get_argument(i);
3972 if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t))))
3974 if (!test_start_file(&t->s, testfile)) {
3979 while (!BIO_eof(t->s.fp)) {
3985 if (c == 0 || !run_test(t)) {
3990 test_end_file(&t->s);
3993 free_key_list(public_keys);
3994 free_key_list(private_keys);
4001 const OPTIONS *test_get_options(void)
4003 static const OPTIONS test_options[] = {
4004 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
4005 { "config", OPT_CONFIG_FILE, '<',
4006 "The configuration file to use for the libctx" },
4007 { OPT_HELP_STR, 1, '-', "file\tFile to run tests on.\n" },
4010 return test_options;
4013 int setup_tests(void)
4016 char *config_file = NULL;
4020 while ((o = opt_next()) != OPT_EOF) {
4022 case OPT_CONFIG_FILE:
4023 config_file = opt_arg();
4025 case OPT_TEST_CASES:
4034 * Load the provider via configuration into the created library context.
4035 * Load the 'null' provider into the default library context to ensure that
4036 * the tests do not fallback to using the default provider.
4038 if (!test_get_libctx(&libctx, &prov_null, config_file, NULL, NULL))
4041 n = test_get_argument_count();
4045 ADD_ALL_TESTS(run_file_tests, n);
4049 void cleanup_tests(void)
4051 OSSL_PROVIDER_unload(prov_null);
4052 OSSL_LIB_CTX_free(libctx);
4055 static int is_digest_disabled(const char *name)
4057 #ifdef OPENSSL_NO_BLAKE2
4058 if (HAS_CASE_PREFIX(name, "BLAKE"))
4061 #ifdef OPENSSL_NO_MD2
4062 if (OPENSSL_strcasecmp(name, "MD2") == 0)
4065 #ifdef OPENSSL_NO_MDC2
4066 if (OPENSSL_strcasecmp(name, "MDC2") == 0)
4069 #ifdef OPENSSL_NO_MD4
4070 if (OPENSSL_strcasecmp(name, "MD4") == 0)
4073 #ifdef OPENSSL_NO_MD5
4074 if (OPENSSL_strcasecmp(name, "MD5") == 0)
4077 #ifdef OPENSSL_NO_RMD160
4078 if (OPENSSL_strcasecmp(name, "RIPEMD160") == 0)
4081 #ifdef OPENSSL_NO_SM3
4082 if (OPENSSL_strcasecmp(name, "SM3") == 0)
4085 #ifdef OPENSSL_NO_WHIRLPOOL
4086 if (OPENSSL_strcasecmp(name, "WHIRLPOOL") == 0)
4092 static int is_pkey_disabled(const char *name)
4094 #ifdef OPENSSL_NO_EC
4095 if (HAS_CASE_PREFIX(name, "EC"))
4098 #ifdef OPENSSL_NO_DH
4099 if (HAS_CASE_PREFIX(name, "DH"))
4102 #ifdef OPENSSL_NO_DSA
4103 if (HAS_CASE_PREFIX(name, "DSA"))
4109 static int is_mac_disabled(const char *name)
4111 #ifdef OPENSSL_NO_BLAKE2
4112 if (HAS_CASE_PREFIX(name, "BLAKE2BMAC")
4113 || HAS_CASE_PREFIX(name, "BLAKE2SMAC"))
4116 #ifdef OPENSSL_NO_CMAC
4117 if (HAS_CASE_PREFIX(name, "CMAC"))
4120 #ifdef OPENSSL_NO_POLY1305
4121 if (HAS_CASE_PREFIX(name, "Poly1305"))
4124 #ifdef OPENSSL_NO_SIPHASH
4125 if (HAS_CASE_PREFIX(name, "SipHash"))
4130 static int is_kdf_disabled(const char *name)
4132 #ifdef OPENSSL_NO_SCRYPT
4133 if (HAS_CASE_SUFFIX(name, "SCRYPT"))
4139 static int is_cipher_disabled(const char *name)
4141 #ifdef OPENSSL_NO_ARIA
4142 if (HAS_CASE_PREFIX(name, "ARIA"))
4145 #ifdef OPENSSL_NO_BF
4146 if (HAS_CASE_PREFIX(name, "BF"))
4149 #ifdef OPENSSL_NO_CAMELLIA
4150 if (HAS_CASE_PREFIX(name, "CAMELLIA"))
4153 #ifdef OPENSSL_NO_CAST
4154 if (HAS_CASE_PREFIX(name, "CAST"))
4157 #ifdef OPENSSL_NO_CHACHA
4158 if (HAS_CASE_PREFIX(name, "CHACHA"))
4161 #ifdef OPENSSL_NO_POLY1305
4162 if (HAS_CASE_SUFFIX(name, "Poly1305"))
4165 #ifdef OPENSSL_NO_DES
4166 if (HAS_CASE_PREFIX(name, "DES"))
4168 if (HAS_CASE_SUFFIX(name, "3DESwrap"))
4171 #ifdef OPENSSL_NO_OCB
4172 if (HAS_CASE_SUFFIX(name, "OCB"))
4175 #ifdef OPENSSL_NO_IDEA
4176 if (HAS_CASE_PREFIX(name, "IDEA"))
4179 #ifdef OPENSSL_NO_RC2
4180 if (HAS_CASE_PREFIX(name, "RC2"))
4183 #ifdef OPENSSL_NO_RC4
4184 if (HAS_CASE_PREFIX(name, "RC4"))
4187 #ifdef OPENSSL_NO_RC5
4188 if (HAS_CASE_PREFIX(name, "RC5"))
4191 #ifdef OPENSSL_NO_SEED
4192 if (HAS_CASE_PREFIX(name, "SEED"))
4195 #ifdef OPENSSL_NO_SIV
4196 if (HAS_CASE_SUFFIX(name, "SIV"))
4199 #ifdef OPENSSL_NO_SM4
4200 if (HAS_CASE_PREFIX(name, "SM4"))