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 "../e_os.h" /* strcasecmp */
16 #include <openssl/evp.h>
17 #include <openssl/pem.h>
18 #include <openssl/err.h>
19 #include <openssl/provider.h>
20 #include <openssl/x509v3.h>
21 #include <openssl/pkcs12.h>
22 #include <openssl/kdf.h>
23 #include <openssl/params.h>
24 #include <openssl/core_names.h>
25 #include <openssl/fips_names.h>
26 #include "internal/numbers.h"
27 #include "internal/nelem.h"
28 #include "crypto/evp.h"
31 typedef struct evp_test_buffer_st EVP_TEST_BUFFER;
32 DEFINE_STACK_OF(EVP_TEST_BUFFER)
36 typedef struct evp_test_method_st EVP_TEST_METHOD;
38 /* Structure holding test information */
39 typedef struct evp_test_st {
40 STANZA s; /* Common test stanza */
42 int skip; /* Current test should be skipped */
43 const EVP_TEST_METHOD *meth; /* method for this test */
44 const char *err, *aux_err; /* Error string for test */
45 char *expected_err; /* Expected error value of test */
46 char *reason; /* Expected error reason string */
47 void *data; /* test specific data */
50 /* Test method structure */
51 struct evp_test_method_st {
52 /* Name of test as it appears in file */
54 /* Initialise test for "alg" */
55 int (*init) (EVP_TEST * t, const char *alg);
57 void (*cleanup) (EVP_TEST * t);
58 /* Test specific name value pair processing */
59 int (*parse) (EVP_TEST * t, const char *name, const char *value);
60 /* Run the test itself */
61 int (*run_test) (EVP_TEST * t);
64 /* Linked list of named keys. */
65 typedef struct key_list_st {
68 struct key_list_st *next;
71 typedef enum OPTION_choice {
78 static OSSL_PROVIDER *prov_null = NULL;
79 static OSSL_LIB_CTX *libctx = NULL;
81 /* List of public and private keys */
82 static KEY_LIST *private_keys;
83 static KEY_LIST *public_keys;
85 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst);
86 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen);
87 static int is_digest_disabled(const char *name);
88 static int is_pkey_disabled(const char *name);
89 static int is_mac_disabled(const char *name);
90 static int is_cipher_disabled(const char *name);
91 static int is_kdf_disabled(const char *name);
94 * Compare two memory regions for equality, returning zero if they differ.
95 * However, if there is expected to be an error and the actual error
96 * matches then the memory is expected to be different so handle this
97 * case without producing unnecessary test framework output.
99 static int memory_err_compare(EVP_TEST *t, const char *err,
100 const void *expected, size_t expected_len,
101 const void *got, size_t got_len)
105 if (t->expected_err != NULL && strcmp(t->expected_err, err) == 0)
106 r = !TEST_mem_ne(expected, expected_len, got, got_len);
108 r = TEST_mem_eq(expected, expected_len, got, got_len);
115 * Structure used to hold a list of blocks of memory to test
116 * calls to "update" like functions.
118 struct evp_test_buffer_st {
125 static void evp_test_buffer_free(EVP_TEST_BUFFER *db)
128 OPENSSL_free(db->buf);
133 /* append buffer to a list */
134 static int evp_test_buffer_append(const char *value,
135 STACK_OF(EVP_TEST_BUFFER) **sk)
137 EVP_TEST_BUFFER *db = NULL;
139 if (!TEST_ptr(db = OPENSSL_malloc(sizeof(*db))))
142 if (!parse_bin(value, &db->buf, &db->buflen))
147 if (*sk == NULL && !TEST_ptr(*sk = sk_EVP_TEST_BUFFER_new_null()))
149 if (!sk_EVP_TEST_BUFFER_push(*sk, db))
155 evp_test_buffer_free(db);
159 /* replace last buffer in list with copies of itself */
160 static int evp_test_buffer_ncopy(const char *value,
161 STACK_OF(EVP_TEST_BUFFER) *sk)
164 unsigned char *tbuf, *p;
166 int ncopy = atoi(value);
171 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
173 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
175 tbuflen = db->buflen * ncopy;
176 if (!TEST_ptr(tbuf = OPENSSL_malloc(tbuflen)))
178 for (i = 0, p = tbuf; i < ncopy; i++, p += db->buflen)
179 memcpy(p, db->buf, db->buflen);
181 OPENSSL_free(db->buf);
183 db->buflen = tbuflen;
187 /* set repeat count for last buffer in list */
188 static int evp_test_buffer_set_count(const char *value,
189 STACK_OF(EVP_TEST_BUFFER) *sk)
192 int count = atoi(value);
197 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
200 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
201 if (db->count_set != 0)
204 db->count = (size_t)count;
209 /* call "fn" with each element of the list in turn */
210 static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER) *sk,
212 const unsigned char *buf,
218 for (i = 0; i < sk_EVP_TEST_BUFFER_num(sk); i++) {
219 EVP_TEST_BUFFER *tb = sk_EVP_TEST_BUFFER_value(sk, i);
222 for (j = 0; j < tb->count; j++) {
223 if (fn(ctx, tb->buf, tb->buflen) <= 0)
231 * Unescape some sequences in string literals (only \n for now).
232 * Return an allocated buffer, set |out_len|. If |input_len|
233 * is zero, get an empty buffer but set length to zero.
235 static unsigned char* unescape(const char *input, size_t input_len,
238 unsigned char *ret, *p;
241 if (input_len == 0) {
243 return OPENSSL_zalloc(1);
246 /* Escaping is non-expanding; over-allocate original size for simplicity. */
247 if (!TEST_ptr(ret = p = OPENSSL_malloc(input_len)))
250 for (i = 0; i < input_len; i++) {
251 if (*input == '\\') {
252 if (i == input_len - 1 || *++input != 'n') {
253 TEST_error("Bad escape sequence in file");
273 * For a hex string "value" convert to a binary allocated buffer.
274 * Return 1 on success or 0 on failure.
276 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen)
280 /* Check for NULL literal */
281 if (strcmp(value, "NULL") == 0) {
287 /* Check for empty value */
288 if (*value == '\0') {
290 * Don't return NULL for zero length buffer. This is needed for
291 * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key
292 * buffer even if the key length is 0, in order to detect key reset.
294 *buf = OPENSSL_malloc(1);
302 /* Check for string literal */
303 if (value[0] == '"') {
304 size_t vlen = strlen(++value);
306 if (vlen == 0 || value[vlen - 1] != '"')
309 *buf = unescape(value, vlen, buflen);
310 return *buf == NULL ? 0 : 1;
313 /* Otherwise assume as hex literal and convert it to binary buffer */
314 if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) {
315 TEST_info("Can't convert %s", value);
316 TEST_openssl_errors();
319 /* Size of input buffer means we'll never overflow */
325 ** MESSAGE DIGEST TESTS
328 typedef struct digest_data_st {
329 /* Digest this test is for */
330 const EVP_MD *digest;
331 EVP_MD *fetched_digest;
332 /* Input to digest */
333 STACK_OF(EVP_TEST_BUFFER) *input;
334 /* Expected output */
335 unsigned char *output;
341 static int digest_test_init(EVP_TEST *t, const char *alg)
344 const EVP_MD *digest;
345 EVP_MD *fetched_digest;
347 if (is_digest_disabled(alg)) {
348 TEST_info("skipping, '%s' is disabled", alg);
353 if ((digest = fetched_digest = EVP_MD_fetch(libctx, alg, NULL)) == NULL
354 && (digest = EVP_get_digestbyname(alg)) == NULL)
356 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
359 mdat->digest = digest;
360 mdat->fetched_digest = fetched_digest;
362 if (fetched_digest != NULL)
363 TEST_info("%s is fetched", alg);
367 static void digest_test_cleanup(EVP_TEST *t)
369 DIGEST_DATA *mdat = t->data;
371 sk_EVP_TEST_BUFFER_pop_free(mdat->input, evp_test_buffer_free);
372 OPENSSL_free(mdat->output);
373 EVP_MD_free(mdat->fetched_digest);
376 static int digest_test_parse(EVP_TEST *t,
377 const char *keyword, const char *value)
379 DIGEST_DATA *mdata = t->data;
381 if (strcmp(keyword, "Input") == 0)
382 return evp_test_buffer_append(value, &mdata->input);
383 if (strcmp(keyword, "Output") == 0)
384 return parse_bin(value, &mdata->output, &mdata->output_len);
385 if (strcmp(keyword, "Count") == 0)
386 return evp_test_buffer_set_count(value, mdata->input);
387 if (strcmp(keyword, "Ncopy") == 0)
388 return evp_test_buffer_ncopy(value, mdata->input);
389 if (strcmp(keyword, "Padding") == 0)
390 return (mdata->pad_type = atoi(value)) > 0;
394 static int digest_update_fn(void *ctx, const unsigned char *buf, size_t buflen)
396 return EVP_DigestUpdate(ctx, buf, buflen);
399 static int digest_test_run(EVP_TEST *t)
401 DIGEST_DATA *expected = t->data;
402 EVP_TEST_BUFFER *inbuf;
404 unsigned char *got = NULL;
405 unsigned int got_len;
408 OSSL_PARAM params[2];
410 t->err = "TEST_FAILURE";
411 if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
414 got = OPENSSL_malloc(expected->output_len > EVP_MAX_MD_SIZE ?
415 expected->output_len : EVP_MAX_MD_SIZE);
419 if (!EVP_DigestInit_ex(mctx, expected->digest, NULL)) {
420 t->err = "DIGESTINIT_ERROR";
423 if (expected->pad_type > 0) {
424 params[0] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_PAD_TYPE,
425 &expected->pad_type);
426 params[1] = OSSL_PARAM_construct_end();
427 if (!TEST_int_gt(EVP_MD_CTX_set_params(mctx, params), 0)) {
428 t->err = "PARAMS_ERROR";
432 if (!evp_test_buffer_do(expected->input, digest_update_fn, mctx)) {
433 t->err = "DIGESTUPDATE_ERROR";
437 xof = (EVP_MD_get_flags(expected->digest) & EVP_MD_FLAG_XOF) != 0;
439 EVP_MD_CTX *mctx_cpy;
440 char dont[] = "touch";
442 if (!TEST_ptr(mctx_cpy = EVP_MD_CTX_new())) {
445 if (!EVP_MD_CTX_copy(mctx_cpy, mctx)) {
446 EVP_MD_CTX_free(mctx_cpy);
449 if (!EVP_DigestFinalXOF(mctx_cpy, (unsigned char *)dont, 0)) {
450 EVP_MD_CTX_free(mctx_cpy);
451 t->err = "DIGESTFINALXOF_ERROR";
454 if (!TEST_str_eq(dont, "touch")) {
455 EVP_MD_CTX_free(mctx_cpy);
456 t->err = "DIGESTFINALXOF_ERROR";
459 EVP_MD_CTX_free(mctx_cpy);
461 got_len = expected->output_len;
462 if (!EVP_DigestFinalXOF(mctx, got, got_len)) {
463 t->err = "DIGESTFINALXOF_ERROR";
467 if (!EVP_DigestFinal(mctx, got, &got_len)) {
468 t->err = "DIGESTFINAL_ERROR";
472 if (!TEST_int_eq(expected->output_len, got_len)) {
473 t->err = "DIGEST_LENGTH_MISMATCH";
476 if (!memory_err_compare(t, "DIGEST_MISMATCH",
477 expected->output, expected->output_len,
483 /* Test the EVP_Q_digest interface as well */
484 if (sk_EVP_TEST_BUFFER_num(expected->input) == 1
486 /* This should never fail but we need the returned pointer now */
487 && !TEST_ptr(inbuf = sk_EVP_TEST_BUFFER_value(expected->input, 0))
488 && !inbuf->count_set) {
489 OPENSSL_cleanse(got, got_len);
490 if (!TEST_true(EVP_Q_digest(libctx,
491 EVP_MD_get0_name(expected->fetched_digest),
492 NULL, inbuf->buf, inbuf->buflen,
494 || !TEST_mem_eq(got, size,
495 expected->output, expected->output_len)) {
496 t->err = "EVP_Q_digest failed";
503 EVP_MD_CTX_free(mctx);
507 static const EVP_TEST_METHOD digest_test_method = {
519 typedef struct cipher_data_st {
520 const EVP_CIPHER *cipher;
521 EVP_CIPHER *fetched_cipher;
523 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
527 size_t key_bits; /* Used by RC2 */
529 unsigned char *next_iv; /* Expected IV state after operation */
532 unsigned char *plaintext;
533 size_t plaintext_len;
534 unsigned char *ciphertext;
535 size_t ciphertext_len;
536 /* AEAD ciphers only */
537 unsigned char *aad[AAD_NUM];
538 size_t aad_len[AAD_NUM];
542 const char *cts_mode;
545 unsigned char *mac_key;
549 static int cipher_test_init(EVP_TEST *t, const char *alg)
551 const EVP_CIPHER *cipher;
552 EVP_CIPHER *fetched_cipher;
556 if (is_cipher_disabled(alg)) {
558 TEST_info("skipping, '%s' is disabled", alg);
563 if ((cipher = fetched_cipher = EVP_CIPHER_fetch(libctx, alg, NULL)) == NULL
564 && (cipher = EVP_get_cipherbyname(alg)) == NULL) {
565 /* a stitched cipher might not be available */
566 if (strstr(alg, "HMAC") != NULL) {
569 TEST_info("skipping, '%s' is not available", alg);
572 ERR_clear_last_mark();
575 ERR_clear_last_mark();
577 if (!TEST_ptr(cdat = OPENSSL_zalloc(sizeof(*cdat))))
580 cdat->cipher = cipher;
581 cdat->fetched_cipher = fetched_cipher;
583 m = EVP_CIPHER_get_mode(cipher);
584 if (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
585 cdat->aead = m != 0 ? m : -1;
590 if (fetched_cipher != NULL)
591 TEST_info("%s is fetched", alg);
595 static void cipher_test_cleanup(EVP_TEST *t)
598 CIPHER_DATA *cdat = t->data;
600 OPENSSL_free(cdat->key);
601 OPENSSL_free(cdat->iv);
602 OPENSSL_free(cdat->next_iv);
603 OPENSSL_free(cdat->ciphertext);
604 OPENSSL_free(cdat->plaintext);
605 for (i = 0; i < AAD_NUM; i++)
606 OPENSSL_free(cdat->aad[i]);
607 OPENSSL_free(cdat->tag);
608 OPENSSL_free(cdat->mac_key);
609 EVP_CIPHER_free(cdat->fetched_cipher);
612 static int cipher_test_parse(EVP_TEST *t, const char *keyword,
615 CIPHER_DATA *cdat = t->data;
618 if (strcmp(keyword, "Key") == 0)
619 return parse_bin(value, &cdat->key, &cdat->key_len);
620 if (strcmp(keyword, "Rounds") == 0) {
624 cdat->rounds = (unsigned int)i;
627 if (strcmp(keyword, "IV") == 0)
628 return parse_bin(value, &cdat->iv, &cdat->iv_len);
629 if (strcmp(keyword, "NextIV") == 0)
630 return parse_bin(value, &cdat->next_iv, &cdat->iv_len);
631 if (strcmp(keyword, "Plaintext") == 0)
632 return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len);
633 if (strcmp(keyword, "Ciphertext") == 0)
634 return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
635 if (strcmp(keyword, "KeyBits") == 0) {
639 cdat->key_bits = (size_t)i;
645 if (strcmp(keyword, "TLSAAD") == 0)
646 cdat->tls_aad = tls_aad = 1;
647 if (strcmp(keyword, "AAD") == 0 || tls_aad) {
648 for (i = 0; i < AAD_NUM; i++) {
649 if (cdat->aad[i] == NULL)
650 return parse_bin(value, &cdat->aad[i], &cdat->aad_len[i]);
654 if (strcmp(keyword, "Tag") == 0)
655 return parse_bin(value, &cdat->tag, &cdat->tag_len);
656 if (strcmp(keyword, "SetTagLate") == 0) {
657 if (strcmp(value, "TRUE") == 0)
659 else if (strcmp(value, "FALSE") == 0)
665 if (strcmp(keyword, "MACKey") == 0)
666 return parse_bin(value, &cdat->mac_key, &cdat->mac_key_len);
667 if (strcmp(keyword, "TLSVersion") == 0) {
670 cdat->tls_version = (int)strtol(value, &endptr, 0);
671 return value[0] != '\0' && endptr[0] == '\0';
675 if (strcmp(keyword, "Operation") == 0) {
676 if (strcmp(value, "ENCRYPT") == 0)
678 else if (strcmp(value, "DECRYPT") == 0)
684 if (strcmp(keyword, "CTSMode") == 0) {
685 cdat->cts_mode = value;
691 static int cipher_test_enc(EVP_TEST *t, int enc,
692 size_t out_misalign, size_t inp_misalign, int frag)
694 CIPHER_DATA *expected = t->data;
695 unsigned char *in, *expected_out, *tmp = NULL;
696 size_t in_len, out_len, donelen = 0;
697 int ok = 0, tmplen, chunklen, tmpflen, i;
698 EVP_CIPHER_CTX *ctx_base = NULL;
699 EVP_CIPHER_CTX *ctx = NULL;
701 t->err = "TEST_FAILURE";
702 if (!TEST_ptr(ctx_base = EVP_CIPHER_CTX_new()))
704 if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
706 EVP_CIPHER_CTX_set_flags(ctx_base, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
708 in = expected->plaintext;
709 in_len = expected->plaintext_len;
710 expected_out = expected->ciphertext;
711 out_len = expected->ciphertext_len;
713 in = expected->ciphertext;
714 in_len = expected->ciphertext_len;
715 expected_out = expected->plaintext;
716 out_len = expected->plaintext_len;
718 if (inp_misalign == (size_t)-1) {
719 /* Exercise in-place encryption */
720 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
723 in = memcpy(tmp + out_misalign, in, in_len);
725 inp_misalign += 16 - ((out_misalign + in_len) & 15);
727 * 'tmp' will store both output and copy of input. We make the copy
728 * of input to specifically aligned part of 'tmp'. So we just
729 * figured out how much padding would ensure the required alignment,
730 * now we allocate extended buffer and finally copy the input just
731 * past inp_misalign in expression below. Output will be written
732 * past out_misalign...
734 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
735 inp_misalign + in_len);
738 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
739 inp_misalign, in, in_len);
741 if (!EVP_CipherInit_ex(ctx_base, expected->cipher, NULL, NULL, NULL, enc)) {
742 t->err = "CIPHERINIT_ERROR";
745 if (expected->cts_mode != NULL) {
746 OSSL_PARAM params[2];
748 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE,
749 (char *)expected->cts_mode,
751 params[1] = OSSL_PARAM_construct_end();
752 if (!EVP_CIPHER_CTX_set_params(ctx_base, params)) {
753 t->err = "INVALID_CTS_MODE";
758 if (expected->aead) {
759 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_IVLEN,
760 expected->iv_len, 0)) {
761 t->err = "INVALID_IV_LENGTH";
764 } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_get_iv_length(ctx_base)) {
765 t->err = "INVALID_IV_LENGTH";
769 if (expected->aead && !expected->tls_aad) {
772 * If encrypting or OCB just set tag length initially, otherwise
773 * set tag length and value.
775 if (enc || expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late) {
776 t->err = "TAG_LENGTH_SET_ERROR";
779 t->err = "TAG_SET_ERROR";
782 if (tag || expected->aead != EVP_CIPH_GCM_MODE) {
783 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_TAG,
784 expected->tag_len, tag))
789 if (expected->rounds > 0) {
790 int rounds = (int)expected->rounds;
792 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC5_ROUNDS, rounds, NULL)) {
793 t->err = "INVALID_ROUNDS";
798 if (!EVP_CIPHER_CTX_set_key_length(ctx_base, expected->key_len)) {
799 t->err = "INVALID_KEY_LENGTH";
802 if (expected->key_bits > 0) {
803 int bits = (int)expected->key_bits;
805 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC2_KEY_BITS, bits, NULL)) {
806 t->err = "INVALID KEY BITS";
810 if (!EVP_CipherInit_ex(ctx_base, NULL, NULL, expected->key, expected->iv, -1)) {
811 t->err = "KEY_SET_ERROR";
815 /* Check that we get the same IV back */
816 if (expected->iv != NULL) {
817 /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
818 unsigned char iv[128];
819 if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx_base, iv, sizeof(iv)))
820 || ((EVP_CIPHER_get_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
821 && !TEST_mem_eq(expected->iv, expected->iv_len, iv,
822 expected->iv_len))) {
823 t->err = "INVALID_IV";
828 /* Test that the cipher dup functions correctly if it is supported */
830 if (EVP_CIPHER_CTX_copy(ctx, ctx_base)) {
831 EVP_CIPHER_CTX_free(ctx_base);
834 EVP_CIPHER_CTX_free(ctx);
839 if (expected->mac_key != NULL
840 && !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY,
841 (int)expected->mac_key_len,
842 (void *)expected->mac_key)) {
843 t->err = "SET_MAC_KEY_ERROR";
847 if (expected->tls_version) {
848 OSSL_PARAM params[2];
850 params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS_VERSION,
851 &expected->tls_version);
852 params[1] = OSSL_PARAM_construct_end();
853 if (!EVP_CIPHER_CTX_set_params(ctx, params)) {
854 t->err = "SET_TLS_VERSION_ERROR";
859 if (expected->aead == EVP_CIPH_CCM_MODE) {
860 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
861 t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
865 if (expected->aad[0] != NULL && !expected->tls_aad) {
866 t->err = "AAD_SET_ERROR";
868 for (i = 0; expected->aad[i] != NULL; i++) {
869 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i],
870 expected->aad_len[i]))
875 * Supply the AAD in chunks less than the block size where possible
877 for (i = 0; expected->aad[i] != NULL; i++) {
878 if (expected->aad_len[i] > 0) {
879 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], 1))
883 if (expected->aad_len[i] > 2) {
884 if (!EVP_CipherUpdate(ctx, NULL, &chunklen,
885 expected->aad[i] + donelen,
886 expected->aad_len[i] - 2))
888 donelen += expected->aad_len[i] - 2;
890 if (expected->aad_len[i] > 1
891 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
892 expected->aad[i] + donelen, 1))
898 if (expected->tls_aad) {
899 OSSL_PARAM params[2];
902 /* duplicate the aad as the implementation might modify it */
903 if ((tls_aad = OPENSSL_memdup(expected->aad[0],
904 expected->aad_len[0])) == NULL)
906 params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
908 expected->aad_len[0]);
909 params[1] = OSSL_PARAM_construct_end();
910 if (!EVP_CIPHER_CTX_set_params(ctx, params)) {
911 OPENSSL_free(tls_aad);
912 t->err = "TLS1_AAD_ERROR";
915 OPENSSL_free(tls_aad);
916 } else if (!enc && (expected->aead == EVP_CIPH_OCB_MODE
917 || expected->tag_late)) {
918 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
919 expected->tag_len, expected->tag)) {
920 t->err = "TAG_SET_ERROR";
925 EVP_CIPHER_CTX_set_padding(ctx, 0);
926 t->err = "CIPHERUPDATE_ERROR";
929 /* We supply the data all in one go */
930 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
933 /* Supply the data in chunks less than the block size where possible */
935 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
942 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
950 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
956 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
957 t->err = "CIPHERFINAL_ERROR";
960 if (!enc && expected->tls_aad) {
961 if (expected->tls_version >= TLS1_1_VERSION
962 && (EVP_CIPHER_is_a(expected->cipher, "AES-128-CBC-HMAC-SHA1")
963 || EVP_CIPHER_is_a(expected->cipher, "AES-256-CBC-HMAC-SHA1"))) {
964 tmplen -= expected->iv_len;
965 expected_out += expected->iv_len;
966 out_misalign += expected->iv_len;
968 if ((int)out_len > tmplen + tmpflen)
969 out_len = tmplen + tmpflen;
971 if (!memory_err_compare(t, "VALUE_MISMATCH", expected_out, out_len,
972 tmp + out_misalign, tmplen + tmpflen))
974 if (enc && expected->aead && !expected->tls_aad) {
975 unsigned char rtag[16];
977 if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) {
978 t->err = "TAG_LENGTH_INTERNAL_ERROR";
981 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
982 expected->tag_len, rtag)) {
983 t->err = "TAG_RETRIEVE_ERROR";
986 if (!memory_err_compare(t, "TAG_VALUE_MISMATCH",
987 expected->tag, expected->tag_len,
988 rtag, expected->tag_len))
991 /* Check the updated IV */
992 if (expected->next_iv != NULL) {
993 /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
994 unsigned char iv[128];
995 if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv)))
996 || ((EVP_CIPHER_get_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
997 && !TEST_mem_eq(expected->next_iv, expected->iv_len, iv,
998 expected->iv_len))) {
999 t->err = "INVALID_NEXT_IV";
1008 if (ctx != ctx_base)
1009 EVP_CIPHER_CTX_free(ctx_base);
1010 EVP_CIPHER_CTX_free(ctx);
1014 static int cipher_test_run(EVP_TEST *t)
1016 CIPHER_DATA *cdat = t->data;
1018 size_t out_misalign, inp_misalign;
1024 if (!cdat->iv && EVP_CIPHER_get_iv_length(cdat->cipher)) {
1025 /* IV is optional and usually omitted in wrap mode */
1026 if (EVP_CIPHER_get_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
1031 if (cdat->aead && cdat->tag == NULL && !cdat->tls_aad) {
1035 for (out_misalign = 0; out_misalign <= 1;) {
1036 static char aux_err[64];
1037 t->aux_err = aux_err;
1038 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
1039 if (inp_misalign == (size_t)-1) {
1040 /* kludge: inp_misalign == -1 means "exercise in-place" */
1041 BIO_snprintf(aux_err, sizeof(aux_err),
1042 "%s in-place, %sfragmented",
1043 out_misalign ? "misaligned" : "aligned",
1044 frag ? "" : "not ");
1046 BIO_snprintf(aux_err, sizeof(aux_err),
1047 "%s output and %s input, %sfragmented",
1048 out_misalign ? "misaligned" : "aligned",
1049 inp_misalign ? "misaligned" : "aligned",
1050 frag ? "" : "not ");
1053 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
1054 /* Not fatal errors: return */
1061 if (cdat->enc != 1) {
1062 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
1063 /* Not fatal errors: return */
1072 if (out_misalign == 1 && frag == 0) {
1074 * XTS, SIV, CCM, stitched ciphers and Wrap modes have special
1075 * requirements about input lengths so we don't fragment for those
1077 if (cdat->aead == EVP_CIPH_CCM_MODE
1078 || cdat->aead == EVP_CIPH_CBC_MODE
1079 || (cdat->aead == -1
1080 && EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_STREAM_CIPHER)
1081 || ((EVP_CIPHER_get_flags(cdat->cipher) & EVP_CIPH_FLAG_CTS) != 0)
1082 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_SIV_MODE
1083 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
1084 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
1097 static const EVP_TEST_METHOD cipher_test_method = {
1100 cipher_test_cleanup,
1110 typedef struct mac_data_st {
1111 /* MAC type in one form or another */
1113 EVP_MAC *mac; /* for mac_test_run_mac */
1114 int type; /* for mac_test_run_pkey */
1115 /* Algorithm string for this MAC */
1124 unsigned char *input;
1126 /* Expected output */
1127 unsigned char *output;
1129 unsigned char *custom;
1131 /* MAC salt (blake2) */
1132 unsigned char *salt;
1136 /* Reinitialization fails */
1138 /* Collection of controls */
1139 STACK_OF(OPENSSL_STRING) *controls;
1146 static int mac_test_init(EVP_TEST *t, const char *alg)
1148 EVP_MAC *mac = NULL;
1149 int type = NID_undef;
1152 if (is_mac_disabled(alg)) {
1153 TEST_info("skipping, '%s' is disabled", alg);
1157 if ((mac = EVP_MAC_fetch(libctx, alg, NULL)) == NULL) {
1159 * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
1160 * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
1161 * the EVP_PKEY method.
1163 size_t sz = strlen(alg);
1164 static const char epilogue[] = " by EVP_PKEY";
1166 if (sz >= sizeof(epilogue)
1167 && strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0)
1168 sz -= sizeof(epilogue) - 1;
1170 if (strncmp(alg, "HMAC", sz) == 0)
1171 type = EVP_PKEY_HMAC;
1172 else if (strncmp(alg, "CMAC", sz) == 0)
1173 type = EVP_PKEY_CMAC;
1174 else if (strncmp(alg, "Poly1305", sz) == 0)
1175 type = EVP_PKEY_POLY1305;
1176 else if (strncmp(alg, "SipHash", sz) == 0)
1177 type = EVP_PKEY_SIPHASH;
1182 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
1186 if (!TEST_ptr(mdat->mac_name = OPENSSL_strdup(alg))) {
1192 if (!TEST_ptr(mdat->controls = sk_OPENSSL_STRING_new_null())) {
1193 OPENSSL_free(mdat->mac_name);
1198 mdat->output_size = mdat->block_size = -1;
1203 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
1204 static void openssl_free(char *m)
1209 static void mac_test_cleanup(EVP_TEST *t)
1211 MAC_DATA *mdat = t->data;
1213 EVP_MAC_free(mdat->mac);
1214 OPENSSL_free(mdat->mac_name);
1215 sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free);
1216 OPENSSL_free(mdat->alg);
1217 OPENSSL_free(mdat->key);
1218 OPENSSL_free(mdat->iv);
1219 OPENSSL_free(mdat->custom);
1220 OPENSSL_free(mdat->salt);
1221 OPENSSL_free(mdat->input);
1222 OPENSSL_free(mdat->output);
1225 static int mac_test_parse(EVP_TEST *t,
1226 const char *keyword, const char *value)
1228 MAC_DATA *mdata = t->data;
1230 if (strcmp(keyword, "Key") == 0)
1231 return parse_bin(value, &mdata->key, &mdata->key_len);
1232 if (strcmp(keyword, "IV") == 0)
1233 return parse_bin(value, &mdata->iv, &mdata->iv_len);
1234 if (strcmp(keyword, "Custom") == 0)
1235 return parse_bin(value, &mdata->custom, &mdata->custom_len);
1236 if (strcmp(keyword, "Salt") == 0)
1237 return parse_bin(value, &mdata->salt, &mdata->salt_len);
1238 if (strcmp(keyword, "Algorithm") == 0) {
1239 mdata->alg = OPENSSL_strdup(value);
1244 if (strcmp(keyword, "Input") == 0)
1245 return parse_bin(value, &mdata->input, &mdata->input_len);
1246 if (strcmp(keyword, "Output") == 0)
1247 return parse_bin(value, &mdata->output, &mdata->output_len);
1248 if (strcmp(keyword, "XOF") == 0)
1249 return mdata->xof = 1;
1250 if (strcmp(keyword, "NoReinit") == 0)
1251 return mdata->no_reinit = 1;
1252 if (strcmp(keyword, "Ctrl") == 0)
1253 return sk_OPENSSL_STRING_push(mdata->controls,
1254 OPENSSL_strdup(value)) != 0;
1255 if (strcmp(keyword, "OutputSize") == 0) {
1256 mdata->output_size = atoi(value);
1257 if (mdata->output_size < 0)
1261 if (strcmp(keyword, "BlockSize") == 0) {
1262 mdata->block_size = atoi(value);
1263 if (mdata->block_size < 0)
1270 static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1276 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1278 p = strchr(tmpval, ':');
1281 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1284 t->err = "PKEY_CTRL_INVALID";
1286 t->err = "PKEY_CTRL_ERROR";
1289 OPENSSL_free(tmpval);
1293 static int mac_test_run_pkey(EVP_TEST *t)
1295 MAC_DATA *expected = t->data;
1296 EVP_MD_CTX *mctx = NULL;
1297 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1298 EVP_PKEY *key = NULL;
1299 const char *mdname = NULL;
1300 EVP_CIPHER *cipher = NULL;
1301 unsigned char *got = NULL;
1305 /* We don't do XOF mode via PKEY */
1309 if (expected->alg == NULL)
1310 TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type));
1312 TEST_info("Trying the EVP_PKEY %s test with %s",
1313 OBJ_nid2sn(expected->type), expected->alg);
1315 if (expected->type == EVP_PKEY_CMAC) {
1316 #ifdef OPENSSL_NO_DEPRECATED_3_0
1317 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
1322 OSSL_LIB_CTX *tmpctx;
1324 if (expected->alg != NULL && is_cipher_disabled(expected->alg)) {
1325 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
1330 if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, expected->alg, NULL))) {
1331 t->err = "MAC_KEY_CREATE_ERROR";
1334 tmpctx = OSSL_LIB_CTX_set0_default(libctx);
1335 key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len,
1337 OSSL_LIB_CTX_set0_default(tmpctx);
1340 key = EVP_PKEY_new_raw_private_key_ex(libctx,
1341 OBJ_nid2sn(expected->type), NULL,
1342 expected->key, expected->key_len);
1345 t->err = "MAC_KEY_CREATE_ERROR";
1349 if (expected->type == EVP_PKEY_HMAC && expected->alg != NULL) {
1350 if (is_digest_disabled(expected->alg)) {
1351 TEST_info("skipping, HMAC '%s' is disabled", expected->alg);
1356 mdname = expected->alg;
1358 if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
1359 t->err = "INTERNAL_ERROR";
1362 if (!EVP_DigestSignInit_ex(mctx, &pctx, mdname, libctx, NULL, key, NULL)) {
1363 t->err = "DIGESTSIGNINIT_ERROR";
1366 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++)
1367 if (!mac_test_ctrl_pkey(t, pctx,
1368 sk_OPENSSL_STRING_value(expected->controls,
1370 t->err = "EVPPKEYCTXCTRL_ERROR";
1373 if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) {
1374 t->err = "DIGESTSIGNUPDATE_ERROR";
1377 if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) {
1378 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1381 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1382 t->err = "TEST_FAILURE";
1385 if (!EVP_DigestSignFinal(mctx, got, &got_len)
1386 || !memory_err_compare(t, "TEST_MAC_ERR",
1387 expected->output, expected->output_len,
1389 t->err = "TEST_MAC_ERR";
1394 EVP_CIPHER_free(cipher);
1395 EVP_MD_CTX_free(mctx);
1397 EVP_PKEY_CTX_free(genctx);
1402 static int mac_test_run_mac(EVP_TEST *t)
1404 MAC_DATA *expected = t->data;
1405 EVP_MAC_CTX *ctx = NULL;
1406 unsigned char *got = NULL;
1407 size_t got_len = 0, size = 0;
1408 int i, block_size = -1, output_size = -1;
1409 OSSL_PARAM params[21], sizes[3], *psizes = sizes;
1410 size_t params_n = 0;
1411 size_t params_n_allocstart = 0;
1412 const OSSL_PARAM *defined_params =
1413 EVP_MAC_settable_ctx_params(expected->mac);
1417 if (expected->alg == NULL)
1418 TEST_info("Trying the EVP_MAC %s test", expected->mac_name);
1420 TEST_info("Trying the EVP_MAC %s test with %s",
1421 expected->mac_name, expected->alg);
1423 if (expected->alg != NULL) {
1425 * The underlying algorithm may be a cipher or a digest.
1426 * We don't know which it is, but we can ask the MAC what it
1427 * should be and bet on that.
1429 if (OSSL_PARAM_locate_const(defined_params,
1430 OSSL_MAC_PARAM_CIPHER) != NULL) {
1431 params[params_n++] =
1432 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
1434 } else if (OSSL_PARAM_locate_const(defined_params,
1435 OSSL_MAC_PARAM_DIGEST) != NULL) {
1436 params[params_n++] =
1437 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
1440 t->err = "MAC_BAD_PARAMS";
1444 if (expected->custom != NULL)
1445 params[params_n++] =
1446 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
1448 expected->custom_len);
1449 if (expected->salt != NULL)
1450 params[params_n++] =
1451 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT,
1453 expected->salt_len);
1454 if (expected->iv != NULL)
1455 params[params_n++] =
1456 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
1460 /* Unknown controls. They must match parameters that the MAC recognizes */
1461 if (params_n + sk_OPENSSL_STRING_num(expected->controls)
1462 >= OSSL_NELEM(params)) {
1463 t->err = "MAC_TOO_MANY_PARAMETERS";
1466 params_n_allocstart = params_n;
1467 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) {
1468 char *tmpkey, *tmpval;
1469 char *value = sk_OPENSSL_STRING_value(expected->controls, i);
1471 if (!TEST_ptr(tmpkey = OPENSSL_strdup(value))) {
1472 t->err = "MAC_PARAM_ERROR";
1475 tmpval = strchr(tmpkey, ':');
1480 || !OSSL_PARAM_allocate_from_text(¶ms[params_n],
1483 strlen(tmpval), NULL)) {
1484 OPENSSL_free(tmpkey);
1485 t->err = "MAC_PARAM_ERROR";
1490 OPENSSL_free(tmpkey);
1492 params[params_n] = OSSL_PARAM_construct_end();
1494 if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) {
1495 t->err = "MAC_CREATE_ERROR";
1499 if (!EVP_MAC_init(ctx, expected->key, expected->key_len, params)) {
1500 t->err = "MAC_INIT_ERROR";
1503 if (expected->output_size >= 0)
1504 *psizes++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_SIZE,
1506 if (expected->block_size >= 0)
1507 *psizes++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_BLOCK_SIZE,
1509 if (psizes != sizes) {
1510 *psizes = OSSL_PARAM_construct_end();
1511 if (!TEST_true(EVP_MAC_CTX_get_params(ctx, sizes))) {
1512 t->err = "INTERNAL_ERROR";
1515 if (expected->output_size >= 0
1516 && !TEST_int_eq(output_size, expected->output_size)) {
1517 t->err = "TEST_FAILURE";
1520 if (expected->block_size >= 0
1521 && !TEST_int_eq(block_size, expected->block_size)) {
1522 t->err = "TEST_FAILURE";
1527 if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) {
1528 t->err = "MAC_UPDATE_ERROR";
1531 xof = expected->xof;
1533 if (!TEST_ptr(got = OPENSSL_malloc(expected->output_len))) {
1534 t->err = "TEST_FAILURE";
1537 if (!EVP_MAC_finalXOF(ctx, got, expected->output_len)
1538 || !memory_err_compare(t, "TEST_MAC_ERR",
1539 expected->output, expected->output_len,
1540 got, expected->output_len)) {
1541 t->err = "MAC_FINAL_ERROR";
1545 if (!EVP_MAC_final(ctx, NULL, &got_len, 0)) {
1546 t->err = "MAC_FINAL_LENGTH_ERROR";
1549 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1550 t->err = "TEST_FAILURE";
1553 if (!EVP_MAC_final(ctx, got, &got_len, got_len)
1554 || !memory_err_compare(t, "TEST_MAC_ERR",
1555 expected->output, expected->output_len,
1557 t->err = "TEST_MAC_ERR";
1562 OSSL_PARAM ivparams[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1565 /* If the MAC uses IV, we have to set it again */
1566 if (expected->iv != NULL) {
1568 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
1571 ivparams[1] = OSSL_PARAM_construct_end();
1574 ret = EVP_MAC_init(ctx, NULL, 0, ivparams);
1575 if (expected->no_reinit) {
1577 ERR_clear_last_mark();
1578 t->err = "MAC_REINIT_SHOULD_FAIL";
1582 ERR_clear_last_mark();
1587 ERR_clear_last_mark();
1588 t->err = "MAC_REINIT_ERROR";
1591 /* If reinitialization fails, it is unsupported by the algorithm */
1596 /* Test the EVP_Q_mac interface as well */
1598 OPENSSL_cleanse(got, got_len);
1599 if (!TEST_true(EVP_Q_mac(libctx, expected->mac_name, NULL,
1600 expected->alg, params,
1601 expected->key, expected->key_len,
1602 expected->input, expected->input_len,
1603 got, got_len, &size))
1604 || !TEST_mem_eq(got, size,
1605 expected->output, expected->output_len)) {
1606 t->err = "EVP_Q_mac failed";
1611 while (params_n-- > params_n_allocstart) {
1612 OPENSSL_free(params[params_n].data);
1614 EVP_MAC_CTX_free(ctx);
1619 static int mac_test_run(EVP_TEST *t)
1621 MAC_DATA *expected = t->data;
1623 if (expected->mac != NULL)
1624 return mac_test_run_mac(t);
1625 return mac_test_run_pkey(t);
1628 static const EVP_TEST_METHOD mac_test_method = {
1639 ** These are all very similar and share much common code.
1642 typedef struct pkey_data_st {
1643 /* Context for this operation */
1645 /* Key operation to perform */
1646 int (*keyop) (EVP_PKEY_CTX *ctx,
1647 unsigned char *sig, size_t *siglen,
1648 const unsigned char *tbs, size_t tbslen);
1650 unsigned char *input;
1652 /* Expected output */
1653 unsigned char *output;
1658 * Perform public key operation setup: lookup key, allocated ctx and call
1659 * the appropriate initialisation function
1661 static int pkey_test_init(EVP_TEST *t, const char *name,
1663 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1664 int (*keyop)(EVP_PKEY_CTX *ctx,
1665 unsigned char *sig, size_t *siglen,
1666 const unsigned char *tbs,
1670 EVP_PKEY *pkey = NULL;
1674 rv = find_key(&pkey, name, public_keys);
1676 rv = find_key(&pkey, name, private_keys);
1677 if (rv == 0 || pkey == NULL) {
1678 TEST_info("skipping, key '%s' is disabled", name);
1683 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) {
1684 EVP_PKEY_free(pkey);
1687 kdata->keyop = keyop;
1688 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL))) {
1689 EVP_PKEY_free(pkey);
1690 OPENSSL_free(kdata);
1693 if (keyopinit(kdata->ctx) <= 0)
1694 t->err = "KEYOP_INIT_ERROR";
1699 static void pkey_test_cleanup(EVP_TEST *t)
1701 PKEY_DATA *kdata = t->data;
1703 OPENSSL_free(kdata->input);
1704 OPENSSL_free(kdata->output);
1705 EVP_PKEY_CTX_free(kdata->ctx);
1708 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1714 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1716 p = strchr(tmpval, ':');
1719 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1722 t->err = "PKEY_CTRL_INVALID";
1724 } else if (p != NULL && rv <= 0) {
1725 if (is_digest_disabled(p) || is_cipher_disabled(p)) {
1726 TEST_info("skipping, '%s' is disabled", p);
1730 t->err = "PKEY_CTRL_ERROR";
1734 OPENSSL_free(tmpval);
1738 static int pkey_test_parse(EVP_TEST *t,
1739 const char *keyword, const char *value)
1741 PKEY_DATA *kdata = t->data;
1742 if (strcmp(keyword, "Input") == 0)
1743 return parse_bin(value, &kdata->input, &kdata->input_len);
1744 if (strcmp(keyword, "Output") == 0)
1745 return parse_bin(value, &kdata->output, &kdata->output_len);
1746 if (strcmp(keyword, "Ctrl") == 0)
1747 return pkey_test_ctrl(t, kdata->ctx, value);
1751 static int pkey_test_run(EVP_TEST *t)
1753 PKEY_DATA *expected = t->data;
1754 unsigned char *got = NULL;
1756 EVP_PKEY_CTX *copy = NULL;
1758 if (expected->keyop(expected->ctx, NULL, &got_len,
1759 expected->input, expected->input_len) <= 0
1760 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1761 t->err = "KEYOP_LENGTH_ERROR";
1764 if (expected->keyop(expected->ctx, got, &got_len,
1765 expected->input, expected->input_len) <= 0) {
1766 t->err = "KEYOP_ERROR";
1769 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1770 expected->output, expected->output_len,
1778 /* Repeat the test on a copy. */
1779 if (!TEST_ptr(copy = EVP_PKEY_CTX_dup(expected->ctx))) {
1780 t->err = "INTERNAL_ERROR";
1783 if (expected->keyop(copy, NULL, &got_len, expected->input,
1784 expected->input_len) <= 0
1785 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1786 t->err = "KEYOP_LENGTH_ERROR";
1789 if (expected->keyop(copy, got, &got_len, expected->input,
1790 expected->input_len) <= 0) {
1791 t->err = "KEYOP_ERROR";
1794 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1795 expected->output, expected->output_len,
1801 EVP_PKEY_CTX_free(copy);
1805 static int sign_test_init(EVP_TEST *t, const char *name)
1807 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1810 static const EVP_TEST_METHOD psign_test_method = {
1818 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1820 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1821 EVP_PKEY_verify_recover);
1824 static const EVP_TEST_METHOD pverify_recover_test_method = {
1826 verify_recover_test_init,
1832 static int decrypt_test_init(EVP_TEST *t, const char *name)
1834 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1838 static const EVP_TEST_METHOD pdecrypt_test_method = {
1846 static int verify_test_init(EVP_TEST *t, const char *name)
1848 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1851 static int verify_test_run(EVP_TEST *t)
1853 PKEY_DATA *kdata = t->data;
1855 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1856 kdata->input, kdata->input_len) <= 0)
1857 t->err = "VERIFY_ERROR";
1861 static const EVP_TEST_METHOD pverify_test_method = {
1869 static int pderive_test_init(EVP_TEST *t, const char *name)
1871 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1874 static int pderive_test_parse(EVP_TEST *t,
1875 const char *keyword, const char *value)
1877 PKEY_DATA *kdata = t->data;
1880 if (strcmp(keyword, "PeerKeyValidate") == 0)
1883 if (validate || strcmp(keyword, "PeerKey") == 0) {
1885 if (find_key(&peer, value, public_keys) == 0)
1887 if (EVP_PKEY_derive_set_peer_ex(kdata->ctx, peer, validate) <= 0) {
1888 t->err = "DERIVE_SET_PEER_ERROR";
1894 if (strcmp(keyword, "SharedSecret") == 0)
1895 return parse_bin(value, &kdata->output, &kdata->output_len);
1896 if (strcmp(keyword, "Ctrl") == 0)
1897 return pkey_test_ctrl(t, kdata->ctx, value);
1898 if (strcmp(keyword, "KDFType") == 0) {
1899 OSSL_PARAM params[2];
1901 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_EXCHANGE_PARAM_KDF_TYPE,
1903 params[1] = OSSL_PARAM_construct_end();
1904 if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0)
1908 if (strcmp(keyword, "KDFDigest") == 0) {
1909 OSSL_PARAM params[2];
1911 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_EXCHANGE_PARAM_KDF_DIGEST,
1913 params[1] = OSSL_PARAM_construct_end();
1914 if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0)
1918 if (strcmp(keyword, "CEKAlg") == 0) {
1919 OSSL_PARAM params[2];
1921 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CEK_ALG,
1923 params[1] = OSSL_PARAM_construct_end();
1924 if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0)
1928 if (strcmp(keyword, "KDFOutlen") == 0) {
1929 OSSL_PARAM params[2];
1931 size_t outlen = (size_t)strtoul(value, &endptr, 0);
1933 if (endptr[0] != '\0')
1936 params[0] = OSSL_PARAM_construct_size_t(OSSL_EXCHANGE_PARAM_KDF_OUTLEN,
1938 params[1] = OSSL_PARAM_construct_end();
1939 if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0)
1946 static int pderive_test_run(EVP_TEST *t)
1948 EVP_PKEY_CTX *dctx = NULL;
1949 PKEY_DATA *expected = t->data;
1950 unsigned char *got = NULL;
1953 if (!TEST_ptr(dctx = EVP_PKEY_CTX_dup(expected->ctx))) {
1954 t->err = "DERIVE_ERROR";
1958 if (EVP_PKEY_derive(dctx, NULL, &got_len) <= 0
1959 || !TEST_size_t_ne(got_len, 0)) {
1960 t->err = "DERIVE_ERROR";
1963 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1964 t->err = "DERIVE_ERROR";
1967 if (EVP_PKEY_derive(dctx, got, &got_len) <= 0) {
1968 t->err = "DERIVE_ERROR";
1971 if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH",
1972 expected->output, expected->output_len,
1979 EVP_PKEY_CTX_free(dctx);
1983 static const EVP_TEST_METHOD pderive_test_method = {
1996 typedef enum pbe_type_enum {
1997 PBE_TYPE_INVALID = 0,
1998 PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
2001 typedef struct pbe_data_st {
2003 /* scrypt parameters */
2004 uint64_t N, r, p, maxmem;
2005 /* PKCS#12 parameters */
2009 unsigned char *pass;
2012 unsigned char *salt;
2014 /* Expected output */
2019 #ifndef OPENSSL_NO_SCRYPT
2020 /* Parse unsigned decimal 64 bit integer value */
2021 static int parse_uint64(const char *value, uint64_t *pr)
2023 const char *p = value;
2025 if (!TEST_true(*p)) {
2026 TEST_info("Invalid empty integer value");
2029 for (*pr = 0; *p; ) {
2030 if (*pr > UINT64_MAX / 10) {
2031 TEST_error("Integer overflow in string %s", value);
2035 if (!TEST_true(isdigit((unsigned char)*p))) {
2036 TEST_error("Invalid character in string %s", value);
2045 static int scrypt_test_parse(EVP_TEST *t,
2046 const char *keyword, const char *value)
2048 PBE_DATA *pdata = t->data;
2050 if (strcmp(keyword, "N") == 0)
2051 return parse_uint64(value, &pdata->N);
2052 if (strcmp(keyword, "p") == 0)
2053 return parse_uint64(value, &pdata->p);
2054 if (strcmp(keyword, "r") == 0)
2055 return parse_uint64(value, &pdata->r);
2056 if (strcmp(keyword, "maxmem") == 0)
2057 return parse_uint64(value, &pdata->maxmem);
2062 static int pbkdf2_test_parse(EVP_TEST *t,
2063 const char *keyword, const char *value)
2065 PBE_DATA *pdata = t->data;
2067 if (strcmp(keyword, "iter") == 0) {
2068 pdata->iter = atoi(value);
2069 if (pdata->iter <= 0)
2073 if (strcmp(keyword, "MD") == 0) {
2074 pdata->md = EVP_get_digestbyname(value);
2075 if (pdata->md == NULL)
2082 static int pkcs12_test_parse(EVP_TEST *t,
2083 const char *keyword, const char *value)
2085 PBE_DATA *pdata = t->data;
2087 if (strcmp(keyword, "id") == 0) {
2088 pdata->id = atoi(value);
2093 return pbkdf2_test_parse(t, keyword, value);
2096 static int pbe_test_init(EVP_TEST *t, const char *alg)
2099 PBE_TYPE pbe_type = PBE_TYPE_INVALID;
2101 if (is_kdf_disabled(alg)) {
2102 TEST_info("skipping, '%s' is disabled", alg);
2106 if (strcmp(alg, "scrypt") == 0) {
2107 pbe_type = PBE_TYPE_SCRYPT;
2108 } else if (strcmp(alg, "pbkdf2") == 0) {
2109 pbe_type = PBE_TYPE_PBKDF2;
2110 } else if (strcmp(alg, "pkcs12") == 0) {
2111 pbe_type = PBE_TYPE_PKCS12;
2113 TEST_error("Unknown pbe algorithm %s", alg);
2116 if (!TEST_ptr(pdat = OPENSSL_zalloc(sizeof(*pdat))))
2118 pdat->pbe_type = pbe_type;
2123 static void pbe_test_cleanup(EVP_TEST *t)
2125 PBE_DATA *pdat = t->data;
2127 OPENSSL_free(pdat->pass);
2128 OPENSSL_free(pdat->salt);
2129 OPENSSL_free(pdat->key);
2132 static int pbe_test_parse(EVP_TEST *t,
2133 const char *keyword, const char *value)
2135 PBE_DATA *pdata = t->data;
2137 if (strcmp(keyword, "Password") == 0)
2138 return parse_bin(value, &pdata->pass, &pdata->pass_len);
2139 if (strcmp(keyword, "Salt") == 0)
2140 return parse_bin(value, &pdata->salt, &pdata->salt_len);
2141 if (strcmp(keyword, "Key") == 0)
2142 return parse_bin(value, &pdata->key, &pdata->key_len);
2143 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
2144 return pbkdf2_test_parse(t, keyword, value);
2145 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
2146 return pkcs12_test_parse(t, keyword, value);
2147 #ifndef OPENSSL_NO_SCRYPT
2148 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
2149 return scrypt_test_parse(t, keyword, value);
2154 static int pbe_test_run(EVP_TEST *t)
2156 PBE_DATA *expected = t->data;
2158 EVP_MD *fetched_digest = NULL;
2159 OSSL_LIB_CTX *save_libctx;
2161 save_libctx = OSSL_LIB_CTX_set0_default(libctx);
2163 if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
2164 t->err = "INTERNAL_ERROR";
2167 if (expected->pbe_type == PBE_TYPE_PBKDF2) {
2168 if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
2169 expected->salt, expected->salt_len,
2170 expected->iter, expected->md,
2171 expected->key_len, key) == 0) {
2172 t->err = "PBKDF2_ERROR";
2175 #ifndef OPENSSL_NO_SCRYPT
2176 } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
2177 if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
2178 expected->salt, expected->salt_len,
2179 expected->N, expected->r, expected->p,
2180 expected->maxmem, key, expected->key_len) == 0) {
2181 t->err = "SCRYPT_ERROR";
2185 } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
2186 fetched_digest = EVP_MD_fetch(libctx, EVP_MD_get0_name(expected->md),
2188 if (fetched_digest == NULL) {
2189 t->err = "PKCS12_ERROR";
2192 if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
2193 expected->salt, expected->salt_len,
2194 expected->id, expected->iter, expected->key_len,
2195 key, fetched_digest) == 0) {
2196 t->err = "PKCS12_ERROR";
2200 if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len,
2201 key, expected->key_len))
2206 EVP_MD_free(fetched_digest);
2208 OSSL_LIB_CTX_set0_default(save_libctx);
2212 static const EVP_TEST_METHOD pbe_test_method = {
2226 BASE64_CANONICAL_ENCODING = 0,
2227 BASE64_VALID_ENCODING = 1,
2228 BASE64_INVALID_ENCODING = 2
2229 } base64_encoding_type;
2231 typedef struct encode_data_st {
2232 /* Input to encoding */
2233 unsigned char *input;
2235 /* Expected output */
2236 unsigned char *output;
2238 base64_encoding_type encoding;
2241 static int encode_test_init(EVP_TEST *t, const char *encoding)
2245 if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
2247 if (strcmp(encoding, "canonical") == 0) {
2248 edata->encoding = BASE64_CANONICAL_ENCODING;
2249 } else if (strcmp(encoding, "valid") == 0) {
2250 edata->encoding = BASE64_VALID_ENCODING;
2251 } else if (strcmp(encoding, "invalid") == 0) {
2252 edata->encoding = BASE64_INVALID_ENCODING;
2253 if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
2256 TEST_error("Bad encoding: %s."
2257 " Should be one of {canonical, valid, invalid}",
2264 OPENSSL_free(edata);
2268 static void encode_test_cleanup(EVP_TEST *t)
2270 ENCODE_DATA *edata = t->data;
2272 OPENSSL_free(edata->input);
2273 OPENSSL_free(edata->output);
2274 memset(edata, 0, sizeof(*edata));
2277 static int encode_test_parse(EVP_TEST *t,
2278 const char *keyword, const char *value)
2280 ENCODE_DATA *edata = t->data;
2282 if (strcmp(keyword, "Input") == 0)
2283 return parse_bin(value, &edata->input, &edata->input_len);
2284 if (strcmp(keyword, "Output") == 0)
2285 return parse_bin(value, &edata->output, &edata->output_len);
2289 static int encode_test_run(EVP_TEST *t)
2291 ENCODE_DATA *expected = t->data;
2292 unsigned char *encode_out = NULL, *decode_out = NULL;
2293 int output_len, chunk_len;
2294 EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL;
2296 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
2297 t->err = "INTERNAL_ERROR";
2301 if (expected->encoding == BASE64_CANONICAL_ENCODING) {
2303 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
2304 || !TEST_ptr(encode_out =
2305 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
2308 EVP_EncodeInit(encode_ctx);
2309 if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
2310 expected->input, expected->input_len)))
2313 output_len = chunk_len;
2315 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
2316 output_len += chunk_len;
2318 if (!memory_err_compare(t, "BAD_ENCODING",
2319 expected->output, expected->output_len,
2320 encode_out, output_len))
2324 if (!TEST_ptr(decode_out =
2325 OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
2328 EVP_DecodeInit(decode_ctx);
2329 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
2330 expected->output_len) < 0) {
2331 t->err = "DECODE_ERROR";
2334 output_len = chunk_len;
2336 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
2337 t->err = "DECODE_ERROR";
2340 output_len += chunk_len;
2342 if (expected->encoding != BASE64_INVALID_ENCODING
2343 && !memory_err_compare(t, "BAD_DECODING",
2344 expected->input, expected->input_len,
2345 decode_out, output_len)) {
2346 t->err = "BAD_DECODING";
2352 OPENSSL_free(encode_out);
2353 OPENSSL_free(decode_out);
2354 EVP_ENCODE_CTX_free(decode_ctx);
2355 EVP_ENCODE_CTX_free(encode_ctx);
2359 static const EVP_TEST_METHOD encode_test_method = {
2362 encode_test_cleanup,
2371 #define MAX_RAND_REPEATS 15
2373 typedef struct rand_data_pass_st {
2374 unsigned char *entropy;
2375 unsigned char *reseed_entropy;
2376 unsigned char *nonce;
2377 unsigned char *pers;
2378 unsigned char *reseed_addin;
2379 unsigned char *addinA;
2380 unsigned char *addinB;
2381 unsigned char *pr_entropyA;
2382 unsigned char *pr_entropyB;
2383 unsigned char *output;
2384 size_t entropy_len, nonce_len, pers_len, addinA_len, addinB_len,
2385 pr_entropyA_len, pr_entropyB_len, output_len, reseed_entropy_len,
2389 typedef struct rand_data_st {
2390 /* Context for this operation */
2392 EVP_RAND_CTX *parent;
2394 int prediction_resistance;
2396 unsigned int generate_bits;
2400 /* Expected output */
2401 RAND_DATA_PASS data[MAX_RAND_REPEATS];
2404 static int rand_test_init(EVP_TEST *t, const char *name)
2408 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
2409 unsigned int strength = 256;
2411 if (!TEST_ptr(rdata = OPENSSL_zalloc(sizeof(*rdata))))
2414 /* TEST-RAND is available in the FIPS provider but not with "fips=yes" */
2415 rand = EVP_RAND_fetch(libctx, "TEST-RAND", "-fips");
2418 rdata->parent = EVP_RAND_CTX_new(rand, NULL);
2419 EVP_RAND_free(rand);
2420 if (rdata->parent == NULL)
2423 *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength);
2424 if (!EVP_RAND_CTX_set_params(rdata->parent, params))
2427 rand = EVP_RAND_fetch(libctx, name, NULL);
2430 rdata->ctx = EVP_RAND_CTX_new(rand, rdata->parent);
2431 EVP_RAND_free(rand);
2432 if (rdata->ctx == NULL)
2439 EVP_RAND_CTX_free(rdata->parent);
2440 OPENSSL_free(rdata);
2444 static void rand_test_cleanup(EVP_TEST *t)
2446 RAND_DATA *rdata = t->data;
2449 OPENSSL_free(rdata->cipher);
2450 OPENSSL_free(rdata->digest);
2452 for (i = 0; i <= rdata->n; i++) {
2453 OPENSSL_free(rdata->data[i].entropy);
2454 OPENSSL_free(rdata->data[i].reseed_entropy);
2455 OPENSSL_free(rdata->data[i].nonce);
2456 OPENSSL_free(rdata->data[i].pers);
2457 OPENSSL_free(rdata->data[i].reseed_addin);
2458 OPENSSL_free(rdata->data[i].addinA);
2459 OPENSSL_free(rdata->data[i].addinB);
2460 OPENSSL_free(rdata->data[i].pr_entropyA);
2461 OPENSSL_free(rdata->data[i].pr_entropyB);
2462 OPENSSL_free(rdata->data[i].output);
2464 EVP_RAND_CTX_free(rdata->ctx);
2465 EVP_RAND_CTX_free(rdata->parent);
2468 static int rand_test_parse(EVP_TEST *t,
2469 const char *keyword, const char *value)
2471 RAND_DATA *rdata = t->data;
2472 RAND_DATA_PASS *item;
2476 if ((p = strchr(keyword, '.')) != NULL) {
2478 if (n >= MAX_RAND_REPEATS)
2482 item = rdata->data + n;
2483 if (strncmp(keyword, "Entropy.", sizeof("Entropy")) == 0)
2484 return parse_bin(value, &item->entropy, &item->entropy_len);
2485 if (strncmp(keyword, "ReseedEntropy.", sizeof("ReseedEntropy")) == 0)
2486 return parse_bin(value, &item->reseed_entropy,
2487 &item->reseed_entropy_len);
2488 if (strncmp(keyword, "Nonce.", sizeof("Nonce")) == 0)
2489 return parse_bin(value, &item->nonce, &item->nonce_len);
2490 if (strncmp(keyword, "PersonalisationString.",
2491 sizeof("PersonalisationString")) == 0)
2492 return parse_bin(value, &item->pers, &item->pers_len);
2493 if (strncmp(keyword, "ReseedAdditionalInput.",
2494 sizeof("ReseedAdditionalInput")) == 0)
2495 return parse_bin(value, &item->reseed_addin,
2496 &item->reseed_addin_len);
2497 if (strncmp(keyword, "AdditionalInputA.",
2498 sizeof("AdditionalInputA")) == 0)
2499 return parse_bin(value, &item->addinA, &item->addinA_len);
2500 if (strncmp(keyword, "AdditionalInputB.",
2501 sizeof("AdditionalInputB")) == 0)
2502 return parse_bin(value, &item->addinB, &item->addinB_len);
2503 if (strncmp(keyword, "EntropyPredictionResistanceA.",
2504 sizeof("EntropyPredictionResistanceA")) == 0)
2505 return parse_bin(value, &item->pr_entropyA, &item->pr_entropyA_len);
2506 if (strncmp(keyword, "EntropyPredictionResistanceB.",
2507 sizeof("EntropyPredictionResistanceB")) == 0)
2508 return parse_bin(value, &item->pr_entropyB, &item->pr_entropyB_len);
2509 if (strncmp(keyword, "Output.", sizeof("Output")) == 0)
2510 return parse_bin(value, &item->output, &item->output_len);
2512 if (strcmp(keyword, "Cipher") == 0)
2513 return TEST_ptr(rdata->cipher = OPENSSL_strdup(value));
2514 if (strcmp(keyword, "Digest") == 0)
2515 return TEST_ptr(rdata->digest = OPENSSL_strdup(value));
2516 if (strcmp(keyword, "DerivationFunction") == 0) {
2517 rdata->use_df = atoi(value) != 0;
2520 if (strcmp(keyword, "GenerateBits") == 0) {
2521 if ((n = atoi(value)) <= 0 || n % 8 != 0)
2523 rdata->generate_bits = (unsigned int)n;
2526 if (strcmp(keyword, "PredictionResistance") == 0) {
2527 rdata->prediction_resistance = atoi(value) != 0;
2534 static int rand_test_run(EVP_TEST *t)
2536 RAND_DATA *expected = t->data;
2537 RAND_DATA_PASS *item;
2539 size_t got_len = expected->generate_bits / 8;
2540 OSSL_PARAM params[5], *p = params;
2541 int i = -1, ret = 0;
2542 unsigned int strength;
2545 if (!TEST_ptr(got = OPENSSL_malloc(got_len)))
2548 *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &expected->use_df);
2549 if (expected->cipher != NULL)
2550 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
2551 expected->cipher, 0);
2552 if (expected->digest != NULL)
2553 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
2554 expected->digest, 0);
2555 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC, "HMAC", 0);
2556 *p = OSSL_PARAM_construct_end();
2557 if (!TEST_true(EVP_RAND_CTX_set_params(expected->ctx, params)))
2560 strength = EVP_RAND_get_strength(expected->ctx);
2561 for (i = 0; i <= expected->n; i++) {
2562 item = expected->data + i;
2565 z = item->entropy != NULL ? item->entropy : (unsigned char *)"";
2566 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
2567 z, item->entropy_len);
2568 z = item->nonce != NULL ? item->nonce : (unsigned char *)"";
2569 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
2570 z, item->nonce_len);
2571 *p = OSSL_PARAM_construct_end();
2572 if (!TEST_true(EVP_RAND_instantiate(expected->parent, strength,
2573 0, NULL, 0, params)))
2576 z = item->pers != NULL ? item->pers : (unsigned char *)"";
2577 if (!TEST_true(EVP_RAND_instantiate
2578 (expected->ctx, strength,
2579 expected->prediction_resistance, z,
2580 item->pers_len, NULL)))
2583 if (item->reseed_entropy != NULL) {
2584 params[0] = OSSL_PARAM_construct_octet_string
2585 (OSSL_RAND_PARAM_TEST_ENTROPY, item->reseed_entropy,
2586 item->reseed_entropy_len);
2587 params[1] = OSSL_PARAM_construct_end();
2588 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2591 if (!TEST_true(EVP_RAND_reseed
2592 (expected->ctx, expected->prediction_resistance,
2593 NULL, 0, item->reseed_addin,
2594 item->reseed_addin_len)))
2597 if (item->pr_entropyA != NULL) {
2598 params[0] = OSSL_PARAM_construct_octet_string
2599 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyA,
2600 item->pr_entropyA_len);
2601 params[1] = OSSL_PARAM_construct_end();
2602 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2605 if (!TEST_true(EVP_RAND_generate
2606 (expected->ctx, got, got_len,
2607 strength, expected->prediction_resistance,
2608 item->addinA, item->addinA_len)))
2611 if (item->pr_entropyB != NULL) {
2612 params[0] = OSSL_PARAM_construct_octet_string
2613 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyB,
2614 item->pr_entropyB_len);
2615 params[1] = OSSL_PARAM_construct_end();
2616 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2619 if (!TEST_true(EVP_RAND_generate
2620 (expected->ctx, got, got_len,
2621 strength, expected->prediction_resistance,
2622 item->addinB, item->addinB_len)))
2624 if (!TEST_mem_eq(got, got_len, item->output, item->output_len))
2626 if (!TEST_true(EVP_RAND_uninstantiate(expected->ctx))
2627 || !TEST_true(EVP_RAND_uninstantiate(expected->parent))
2628 || !TEST_true(EVP_RAND_verify_zeroization(expected->ctx))
2629 || !TEST_int_eq(EVP_RAND_get_state(expected->ctx),
2630 EVP_RAND_STATE_UNINITIALISED))
2637 if (ret == 0 && i >= 0)
2638 TEST_info("Error in test case %d of %d\n", i, expected->n + 1);
2643 static const EVP_TEST_METHOD rand_test_method = {
2655 typedef struct kdf_data_st {
2656 /* Context for this operation */
2658 /* Expected output */
2659 unsigned char *output;
2661 OSSL_PARAM params[20];
2666 * Perform public key operation setup: lookup key, allocated ctx and call
2667 * the appropriate initialisation function
2669 static int kdf_test_init(EVP_TEST *t, const char *name)
2674 if (is_kdf_disabled(name)) {
2675 TEST_info("skipping, '%s' is disabled", name);
2680 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2682 kdata->p = kdata->params;
2683 *kdata->p = OSSL_PARAM_construct_end();
2685 kdf = EVP_KDF_fetch(libctx, name, NULL);
2687 OPENSSL_free(kdata);
2690 kdata->ctx = EVP_KDF_CTX_new(kdf);
2692 if (kdata->ctx == NULL) {
2693 OPENSSL_free(kdata);
2700 static void kdf_test_cleanup(EVP_TEST *t)
2702 KDF_DATA *kdata = t->data;
2705 for (p = kdata->params; p->key != NULL; p++)
2706 OPENSSL_free(p->data);
2707 OPENSSL_free(kdata->output);
2708 EVP_KDF_CTX_free(kdata->ctx);
2711 static int kdf_test_ctrl(EVP_TEST *t, EVP_KDF_CTX *kctx,
2714 KDF_DATA *kdata = t->data;
2717 const OSSL_PARAM *defs = EVP_KDF_settable_ctx_params(EVP_KDF_CTX_kdf(kctx));
2719 if (!TEST_ptr(name = OPENSSL_strdup(value)))
2721 p = strchr(name, ':');
2725 rv = OSSL_PARAM_allocate_from_text(kdata->p, defs, name, p,
2726 p != NULL ? strlen(p) : 0, NULL);
2727 *++kdata->p = OSSL_PARAM_construct_end();
2729 t->err = "KDF_PARAM_ERROR";
2733 if (p != NULL && strcmp(name, "digest") == 0) {
2734 if (is_digest_disabled(p)) {
2735 TEST_info("skipping, '%s' is disabled", p);
2740 && (strcmp(name, "cipher") == 0
2741 || strcmp(name, "cekalg") == 0)
2742 && is_cipher_disabled(p)) {
2743 TEST_info("skipping, '%s' is disabled", p);
2750 static int kdf_test_parse(EVP_TEST *t,
2751 const char *keyword, const char *value)
2753 KDF_DATA *kdata = t->data;
2755 if (strcmp(keyword, "Output") == 0)
2756 return parse_bin(value, &kdata->output, &kdata->output_len);
2757 if (strncmp(keyword, "Ctrl", 4) == 0)
2758 return kdf_test_ctrl(t, kdata->ctx, value);
2762 static int kdf_test_run(EVP_TEST *t)
2764 KDF_DATA *expected = t->data;
2765 unsigned char *got = NULL;
2766 size_t got_len = expected->output_len;
2768 if (!EVP_KDF_CTX_set_params(expected->ctx, expected->params)) {
2769 t->err = "KDF_CTRL_ERROR";
2772 if (!TEST_ptr(got = OPENSSL_malloc(got_len == 0 ? 1 : got_len))) {
2773 t->err = "INTERNAL_ERROR";
2776 if (EVP_KDF_derive(expected->ctx, got, got_len, NULL) <= 0) {
2777 t->err = "KDF_DERIVE_ERROR";
2780 if (!memory_err_compare(t, "KDF_MISMATCH",
2781 expected->output, expected->output_len,
2792 static const EVP_TEST_METHOD kdf_test_method = {
2804 typedef struct pkey_kdf_data_st {
2805 /* Context for this operation */
2807 /* Expected output */
2808 unsigned char *output;
2813 * Perform public key operation setup: lookup key, allocated ctx and call
2814 * the appropriate initialisation function
2816 static int pkey_kdf_test_init(EVP_TEST *t, const char *name)
2818 PKEY_KDF_DATA *kdata = NULL;
2820 if (is_kdf_disabled(name)) {
2821 TEST_info("skipping, '%s' is disabled", name);
2826 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2829 kdata->ctx = EVP_PKEY_CTX_new_from_name(libctx, name, NULL);
2830 if (kdata->ctx == NULL
2831 || EVP_PKEY_derive_init(kdata->ctx) <= 0)
2837 EVP_PKEY_CTX_free(kdata->ctx);
2838 OPENSSL_free(kdata);
2842 static void pkey_kdf_test_cleanup(EVP_TEST *t)
2844 PKEY_KDF_DATA *kdata = t->data;
2846 OPENSSL_free(kdata->output);
2847 EVP_PKEY_CTX_free(kdata->ctx);
2850 static int pkey_kdf_test_parse(EVP_TEST *t,
2851 const char *keyword, const char *value)
2853 PKEY_KDF_DATA *kdata = t->data;
2855 if (strcmp(keyword, "Output") == 0)
2856 return parse_bin(value, &kdata->output, &kdata->output_len);
2857 if (strncmp(keyword, "Ctrl", 4) == 0)
2858 return pkey_test_ctrl(t, kdata->ctx, value);
2862 static int pkey_kdf_test_run(EVP_TEST *t)
2864 PKEY_KDF_DATA *expected = t->data;
2865 unsigned char *got = NULL;
2866 size_t got_len = expected->output_len;
2868 if (!TEST_ptr(got = OPENSSL_malloc(got_len == 0 ? 1 : got_len))) {
2869 t->err = "INTERNAL_ERROR";
2872 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
2873 t->err = "KDF_DERIVE_ERROR";
2876 if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
2877 t->err = "KDF_MISMATCH";
2887 static const EVP_TEST_METHOD pkey_kdf_test_method = {
2890 pkey_kdf_test_cleanup,
2891 pkey_kdf_test_parse,
2899 typedef struct keypair_test_data_st {
2902 } KEYPAIR_TEST_DATA;
2904 static int keypair_test_init(EVP_TEST *t, const char *pair)
2906 KEYPAIR_TEST_DATA *data;
2908 EVP_PKEY *pk = NULL, *pubk = NULL;
2909 char *pub, *priv = NULL;
2911 /* Split private and public names. */
2912 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
2913 || !TEST_ptr(pub = strchr(priv, ':'))) {
2914 t->err = "PARSING_ERROR";
2919 if (!TEST_true(find_key(&pk, priv, private_keys))) {
2920 TEST_info("Can't find private key: %s", priv);
2921 t->err = "MISSING_PRIVATE_KEY";
2924 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
2925 TEST_info("Can't find public key: %s", pub);
2926 t->err = "MISSING_PUBLIC_KEY";
2930 if (pk == NULL && pubk == NULL) {
2931 /* Both keys are listed but unsupported: skip this test */
2937 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2950 static void keypair_test_cleanup(EVP_TEST *t)
2952 OPENSSL_free(t->data);
2957 * For tests that do not accept any custom keywords.
2959 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
2964 static int keypair_test_run(EVP_TEST *t)
2967 const KEYPAIR_TEST_DATA *pair = t->data;
2969 if (pair->privk == NULL || pair->pubk == NULL) {
2971 * this can only happen if only one of the keys is not set
2972 * which means that one of them was unsupported while the
2973 * other isn't: hence a key type mismatch.
2975 t->err = "KEYPAIR_TYPE_MISMATCH";
2980 if ((rv = EVP_PKEY_eq(pair->privk, pair->pubk)) != 1 ) {
2982 t->err = "KEYPAIR_MISMATCH";
2983 } else if ( -1 == rv ) {
2984 t->err = "KEYPAIR_TYPE_MISMATCH";
2985 } else if ( -2 == rv ) {
2986 t->err = "UNSUPPORTED_KEY_COMPARISON";
2988 TEST_error("Unexpected error in key comparison");
3003 static const EVP_TEST_METHOD keypair_test_method = {
3006 keypair_test_cleanup,
3015 typedef struct keygen_test_data_st {
3016 EVP_PKEY_CTX *genctx; /* Keygen context to use */
3017 char *keyname; /* Key name to store key or NULL */
3020 static int keygen_test_init(EVP_TEST *t, const char *alg)
3022 KEYGEN_TEST_DATA *data;
3023 EVP_PKEY_CTX *genctx;
3024 int nid = OBJ_sn2nid(alg);
3026 if (nid == NID_undef) {
3027 nid = OBJ_ln2nid(alg);
3028 if (nid == NID_undef)
3032 if (is_pkey_disabled(alg)) {
3036 if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_from_name(libctx, alg, NULL)))
3039 if (EVP_PKEY_keygen_init(genctx) <= 0) {
3040 t->err = "KEYGEN_INIT_ERROR";
3044 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
3046 data->genctx = genctx;
3047 data->keyname = NULL;
3053 EVP_PKEY_CTX_free(genctx);
3057 static void keygen_test_cleanup(EVP_TEST *t)
3059 KEYGEN_TEST_DATA *keygen = t->data;
3061 EVP_PKEY_CTX_free(keygen->genctx);
3062 OPENSSL_free(keygen->keyname);
3063 OPENSSL_free(t->data);
3067 static int keygen_test_parse(EVP_TEST *t,
3068 const char *keyword, const char *value)
3070 KEYGEN_TEST_DATA *keygen = t->data;
3072 if (strcmp(keyword, "KeyName") == 0)
3073 return TEST_ptr(keygen->keyname = OPENSSL_strdup(value));
3074 if (strcmp(keyword, "Ctrl") == 0)
3075 return pkey_test_ctrl(t, keygen->genctx, value);
3079 static int keygen_test_run(EVP_TEST *t)
3081 KEYGEN_TEST_DATA *keygen = t->data;
3082 EVP_PKEY *pkey = NULL;
3085 if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) {
3086 t->err = "KEYGEN_GENERATE_ERROR";
3090 if (!evp_pkey_is_provided(pkey)) {
3091 TEST_info("Warning: legacy key generated %s", keygen->keyname);
3094 if (keygen->keyname != NULL) {
3098 if (find_key(NULL, keygen->keyname, private_keys)) {
3099 TEST_info("Duplicate key %s", keygen->keyname);
3103 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
3105 key->name = keygen->keyname;
3106 keygen->keyname = NULL;
3108 key->next = private_keys;
3112 EVP_PKEY_free(pkey);
3121 static const EVP_TEST_METHOD keygen_test_method = {
3124 keygen_test_cleanup,
3130 ** DIGEST SIGN+VERIFY TESTS
3134 int is_verify; /* Set to 1 if verifying */
3135 int is_oneshot; /* Set to 1 for one shot operation */
3136 const EVP_MD *md; /* Digest to use */
3137 EVP_MD_CTX *ctx; /* Digest context */
3139 STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
3140 unsigned char *osin; /* Input data if one shot */
3141 size_t osin_len; /* Input length data if one shot */
3142 unsigned char *output; /* Expected output */
3143 size_t output_len; /* Expected output length */
3146 static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
3149 const EVP_MD *md = NULL;
3150 DIGESTSIGN_DATA *mdat;
3152 if (strcmp(alg, "NULL") != 0) {
3153 if (is_digest_disabled(alg)) {
3157 md = EVP_get_digestbyname(alg);
3161 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
3164 if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
3168 mdat->is_verify = is_verify;
3169 mdat->is_oneshot = is_oneshot;
3174 static int digestsign_test_init(EVP_TEST *t, const char *alg)
3176 return digestsigver_test_init(t, alg, 0, 0);
3179 static void digestsigver_test_cleanup(EVP_TEST *t)
3181 DIGESTSIGN_DATA *mdata = t->data;
3183 EVP_MD_CTX_free(mdata->ctx);
3184 sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
3185 OPENSSL_free(mdata->osin);
3186 OPENSSL_free(mdata->output);
3187 OPENSSL_free(mdata);
3191 static int digestsigver_test_parse(EVP_TEST *t,
3192 const char *keyword, const char *value)
3194 DIGESTSIGN_DATA *mdata = t->data;
3196 if (strcmp(keyword, "Key") == 0) {
3197 EVP_PKEY *pkey = NULL;
3199 const char *name = mdata->md == NULL ? NULL : EVP_MD_get0_name(mdata->md);
3201 if (mdata->is_verify)
3202 rv = find_key(&pkey, value, public_keys);
3204 rv = find_key(&pkey, value, private_keys);
3205 if (rv == 0 || pkey == NULL) {
3209 if (mdata->is_verify) {
3210 if (!EVP_DigestVerifyInit_ex(mdata->ctx, &mdata->pctx, name, libctx,
3212 t->err = "DIGESTVERIFYINIT_ERROR";
3215 if (!EVP_DigestSignInit_ex(mdata->ctx, &mdata->pctx, name, libctx, NULL,
3217 t->err = "DIGESTSIGNINIT_ERROR";
3221 if (strcmp(keyword, "Input") == 0) {
3222 if (mdata->is_oneshot)
3223 return parse_bin(value, &mdata->osin, &mdata->osin_len);
3224 return evp_test_buffer_append(value, &mdata->input);
3226 if (strcmp(keyword, "Output") == 0)
3227 return parse_bin(value, &mdata->output, &mdata->output_len);
3229 if (!mdata->is_oneshot) {
3230 if (strcmp(keyword, "Count") == 0)
3231 return evp_test_buffer_set_count(value, mdata->input);
3232 if (strcmp(keyword, "Ncopy") == 0)
3233 return evp_test_buffer_ncopy(value, mdata->input);
3235 if (strcmp(keyword, "Ctrl") == 0) {
3236 if (mdata->pctx == NULL)
3238 return pkey_test_ctrl(t, mdata->pctx, value);
3243 static int digestsign_update_fn(void *ctx, const unsigned char *buf,
3246 return EVP_DigestSignUpdate(ctx, buf, buflen);
3249 static int digestsign_test_run(EVP_TEST *t)
3251 DIGESTSIGN_DATA *expected = t->data;
3252 unsigned char *got = NULL;
3255 if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
3257 t->err = "DIGESTUPDATE_ERROR";
3261 if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
3262 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
3265 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
3266 t->err = "MALLOC_FAILURE";
3269 if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
3270 t->err = "DIGESTSIGNFINAL_ERROR";
3273 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
3274 expected->output, expected->output_len,
3284 static const EVP_TEST_METHOD digestsign_test_method = {
3286 digestsign_test_init,
3287 digestsigver_test_cleanup,
3288 digestsigver_test_parse,
3292 static int digestverify_test_init(EVP_TEST *t, const char *alg)
3294 return digestsigver_test_init(t, alg, 1, 0);
3297 static int digestverify_update_fn(void *ctx, const unsigned char *buf,
3300 return EVP_DigestVerifyUpdate(ctx, buf, buflen);
3303 static int digestverify_test_run(EVP_TEST *t)
3305 DIGESTSIGN_DATA *mdata = t->data;
3307 if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
3308 t->err = "DIGESTUPDATE_ERROR";
3312 if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
3313 mdata->output_len) <= 0)
3314 t->err = "VERIFY_ERROR";
3318 static const EVP_TEST_METHOD digestverify_test_method = {
3320 digestverify_test_init,
3321 digestsigver_test_cleanup,
3322 digestsigver_test_parse,
3323 digestverify_test_run
3326 static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
3328 return digestsigver_test_init(t, alg, 0, 1);
3331 static int oneshot_digestsign_test_run(EVP_TEST *t)
3333 DIGESTSIGN_DATA *expected = t->data;
3334 unsigned char *got = NULL;
3337 if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
3338 expected->osin, expected->osin_len)) {
3339 t->err = "DIGESTSIGN_LENGTH_ERROR";
3342 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
3343 t->err = "MALLOC_FAILURE";
3346 if (!EVP_DigestSign(expected->ctx, got, &got_len,
3347 expected->osin, expected->osin_len)) {
3348 t->err = "DIGESTSIGN_ERROR";
3351 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
3352 expected->output, expected->output_len,
3362 static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
3363 "OneShotDigestSign",
3364 oneshot_digestsign_test_init,
3365 digestsigver_test_cleanup,
3366 digestsigver_test_parse,
3367 oneshot_digestsign_test_run
3370 static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
3372 return digestsigver_test_init(t, alg, 1, 1);
3375 static int oneshot_digestverify_test_run(EVP_TEST *t)
3377 DIGESTSIGN_DATA *mdata = t->data;
3379 if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
3380 mdata->osin, mdata->osin_len) <= 0)
3381 t->err = "VERIFY_ERROR";
3385 static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
3386 "OneShotDigestVerify",
3387 oneshot_digestverify_test_init,
3388 digestsigver_test_cleanup,
3389 digestsigver_test_parse,
3390 oneshot_digestverify_test_run
3395 ** PARSING AND DISPATCH
3398 static const EVP_TEST_METHOD *evp_test_list[] = {
3400 &cipher_test_method,
3401 &digest_test_method,
3402 &digestsign_test_method,
3403 &digestverify_test_method,
3404 &encode_test_method,
3406 &pkey_kdf_test_method,
3407 &keypair_test_method,
3408 &keygen_test_method,
3410 &oneshot_digestsign_test_method,
3411 &oneshot_digestverify_test_method,
3413 &pdecrypt_test_method,
3414 &pderive_test_method,
3416 &pverify_recover_test_method,
3417 &pverify_test_method,
3421 static const EVP_TEST_METHOD *find_test(const char *name)
3423 const EVP_TEST_METHOD **tt;
3425 for (tt = evp_test_list; *tt; tt++) {
3426 if (strcmp(name, (*tt)->name) == 0)
3432 static void clear_test(EVP_TEST *t)
3434 test_clearstanza(&t->s);
3436 if (t->data != NULL) {
3437 if (t->meth != NULL)
3438 t->meth->cleanup(t);
3439 OPENSSL_free(t->data);
3442 OPENSSL_free(t->expected_err);
3443 t->expected_err = NULL;
3444 OPENSSL_free(t->reason);
3453 /* Check for errors in the test structure; return 1 if okay, else 0. */
3454 static int check_test_error(EVP_TEST *t)
3459 if (t->err == NULL && t->expected_err == NULL)
3461 if (t->err != NULL && t->expected_err == NULL) {
3462 if (t->aux_err != NULL) {
3463 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
3464 t->s.test_file, t->s.start, t->aux_err, t->err);
3466 TEST_info("%s:%d: Source of above error; unexpected error %s",
3467 t->s.test_file, t->s.start, t->err);
3471 if (t->err == NULL && t->expected_err != NULL) {
3472 TEST_info("%s:%d: Succeeded but was expecting %s",
3473 t->s.test_file, t->s.start, t->expected_err);
3477 if (strcmp(t->err, t->expected_err) != 0) {
3478 TEST_info("%s:%d: Expected %s got %s",
3479 t->s.test_file, t->s.start, t->expected_err, t->err);
3483 if (t->reason == NULL)
3486 if (t->reason == NULL) {
3487 TEST_info("%s:%d: Test is missing function or reason code",
3488 t->s.test_file, t->s.start);
3492 err = ERR_peek_error();
3494 TEST_info("%s:%d: Expected error \"%s\" not set",
3495 t->s.test_file, t->s.start, t->reason);
3499 reason = ERR_reason_error_string(err);
3500 if (reason == NULL) {
3501 TEST_info("%s:%d: Expected error \"%s\", no strings available."
3503 t->s.test_file, t->s.start, t->reason);
3507 if (strcmp(reason, t->reason) == 0)
3510 TEST_info("%s:%d: Expected error \"%s\", got \"%s\"",
3511 t->s.test_file, t->s.start, t->reason, reason);
3516 /* Run a parsed test. Log a message and return 0 on error. */
3517 static int run_test(EVP_TEST *t)
3519 if (t->meth == NULL)
3526 if (t->err == NULL && t->meth->run_test(t) != 1) {
3527 TEST_info("%s:%d %s error",
3528 t->s.test_file, t->s.start, t->meth->name);
3531 if (!check_test_error(t)) {
3532 TEST_openssl_errors();
3541 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
3543 for (; lst != NULL; lst = lst->next) {
3544 if (strcmp(lst->name, name) == 0) {
3553 static void free_key_list(KEY_LIST *lst)
3555 while (lst != NULL) {
3556 KEY_LIST *next = lst->next;
3558 EVP_PKEY_free(lst->key);
3559 OPENSSL_free(lst->name);
3566 * Is the key type an unsupported algorithm?
3568 static int key_unsupported(void)
3570 long err = ERR_peek_last_error();
3571 int lib = ERR_GET_LIB(err);
3572 long reason = ERR_GET_REASON(err);
3574 if ((lib == ERR_LIB_EVP && reason == EVP_R_UNSUPPORTED_ALGORITHM)
3575 || (lib == ERR_LIB_EVP && reason == EVP_R_DECODE_ERROR)
3576 || reason == ERR_R_UNSUPPORTED) {
3580 #ifndef OPENSSL_NO_EC
3582 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
3583 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
3586 if (lib == ERR_LIB_EC
3587 && (reason == EC_R_UNKNOWN_GROUP
3588 || reason == EC_R_INVALID_CURVE)) {
3592 #endif /* OPENSSL_NO_EC */
3596 /* NULL out the value from |pp| but return it. This "steals" a pointer. */
3597 static char *take_value(PAIR *pp)
3599 char *p = pp->value;
3605 #if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
3606 static int securitycheck_enabled(void)
3608 static int enabled = -1;
3610 if (enabled == -1) {
3611 if (OSSL_PROVIDER_available(libctx, "fips")) {
3612 OSSL_PARAM params[2];
3613 OSSL_PROVIDER *prov = NULL;
3616 prov = OSSL_PROVIDER_load(libctx, "fips");
3619 OSSL_PARAM_construct_int(OSSL_PROV_PARAM_SECURITY_CHECKS,
3621 params[1] = OSSL_PARAM_construct_end();
3622 OSSL_PROVIDER_get_params(prov, params);
3623 OSSL_PROVIDER_unload(prov);
3635 * Return 1 if one of the providers named in the string is available.
3636 * The provider names are separated with whitespace.
3637 * NOTE: destructive function, it inserts '\0' after each provider name.
3639 static int prov_available(char *providers)
3645 for (; isspace(*providers); providers++)
3647 if (*providers == '\0')
3648 break; /* End of the road */
3649 for (p = providers; *p != '\0' && !isspace(*p); p++)
3655 if (OSSL_PROVIDER_available(libctx, providers))
3656 return 1; /* Found one */
3661 /* Read and parse one test. Return 0 if failure, 1 if okay. */
3662 static int parse(EVP_TEST *t)
3664 KEY_LIST *key, **klist;
3667 int i, skip_availablein = 0;
3671 if (BIO_eof(t->s.fp))
3674 if (!test_readstanza(&t->s))
3676 } while (t->s.numpairs == 0);
3677 pp = &t->s.pairs[0];
3679 /* Are we adding a key? */
3683 if (strcmp(pp->key, "PrivateKey") == 0) {
3684 pkey = PEM_read_bio_PrivateKey_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3685 if (pkey == NULL && !key_unsupported()) {
3686 EVP_PKEY_free(pkey);
3687 TEST_info("Can't read private key %s", pp->value);
3688 TEST_openssl_errors();
3691 klist = &private_keys;
3692 } else if (strcmp(pp->key, "PublicKey") == 0) {
3693 pkey = PEM_read_bio_PUBKEY_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3694 if (pkey == NULL && !key_unsupported()) {
3695 EVP_PKEY_free(pkey);
3696 TEST_info("Can't read public key %s", pp->value);
3697 TEST_openssl_errors();
3700 klist = &public_keys;
3701 } else if (strcmp(pp->key, "PrivateKeyRaw") == 0
3702 || strcmp(pp->key, "PublicKeyRaw") == 0 ) {
3703 char *strnid = NULL, *keydata = NULL;
3704 unsigned char *keybin;
3708 if (strcmp(pp->key, "PrivateKeyRaw") == 0)
3709 klist = &private_keys;
3711 klist = &public_keys;
3713 strnid = strchr(pp->value, ':');
3714 if (strnid != NULL) {
3716 keydata = strchr(strnid, ':');
3717 if (keydata != NULL)
3720 if (keydata == NULL) {
3721 TEST_info("Failed to parse %s value", pp->key);
3725 nid = OBJ_txt2nid(strnid);
3726 if (nid == NID_undef) {
3727 TEST_info("Unrecognised algorithm NID");
3730 if (!parse_bin(keydata, &keybin, &keylen)) {
3731 TEST_info("Failed to create binary key");
3734 if (klist == &private_keys)
3735 pkey = EVP_PKEY_new_raw_private_key_ex(libctx, strnid, NULL, keybin,
3738 pkey = EVP_PKEY_new_raw_public_key_ex(libctx, strnid, NULL, keybin,
3740 if (pkey == NULL && !key_unsupported()) {
3741 TEST_info("Can't read %s data", pp->key);
3742 OPENSSL_free(keybin);
3743 TEST_openssl_errors();
3746 OPENSSL_free(keybin);
3747 } else if (strcmp(pp->key, "Availablein") == 0) {
3748 if (!prov_available(pp->value)) {
3749 TEST_info("skipping, '%s' provider not available: %s:%d",
3750 pp->value, t->s.test_file, t->s.start);
3759 /* If we have a key add to list */
3760 if (klist != NULL) {
3761 if (find_key(NULL, pp->value, *klist)) {
3762 TEST_info("Duplicate key %s", pp->value);
3765 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
3767 key->name = take_value(pp);
3772 /* Go back and start a new stanza. */
3773 if ((t->s.numpairs - skip_availablein) != 1)
3774 TEST_info("Line %d: missing blank line\n", t->s.curr);
3778 /* Find the test, based on first keyword. */
3779 if (!TEST_ptr(t->meth = find_test(pp->key)))
3781 if (!t->meth->init(t, pp->value)) {
3782 TEST_error("unknown %s: %s\n", pp->key, pp->value);
3786 /* TEST_info("skipping %s %s", pp->key, pp->value); */
3790 for (pp++, i = 1; i < (t->s.numpairs - skip_availablein); pp++, i++) {
3791 if (strcmp(pp->key, "Securitycheck") == 0) {
3792 #if defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
3794 if (!securitycheck_enabled())
3797 TEST_info("skipping, Securitycheck is disabled: %s:%d",
3798 t->s.test_file, t->s.start);
3802 } else if (strcmp(pp->key, "Availablein") == 0) {
3803 TEST_info("Line %d: 'Availablein' should be the first option",
3806 } else if (strcmp(pp->key, "Result") == 0) {
3807 if (t->expected_err != NULL) {
3808 TEST_info("Line %d: multiple result lines", t->s.curr);
3811 t->expected_err = take_value(pp);
3812 } else if (strcmp(pp->key, "Function") == 0) {
3813 /* Ignore old line. */
3814 } else if (strcmp(pp->key, "Reason") == 0) {
3815 if (t->reason != NULL) {
3816 TEST_info("Line %d: multiple reason lines", t->s.curr);
3819 t->reason = take_value(pp);
3821 /* Must be test specific line: try to parse it */
3822 int rv = t->meth->parse(t, pp->key, pp->value);
3825 TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key);
3829 TEST_info("Line %d: error processing keyword %s = %s\n",
3830 t->s.curr, pp->key, pp->value);
3839 static int run_file_tests(int i)
3842 const char *testfile = test_get_argument(i);
3845 if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t))))
3847 if (!test_start_file(&t->s, testfile)) {
3852 while (!BIO_eof(t->s.fp)) {
3858 if (c == 0 || !run_test(t)) {
3863 test_end_file(&t->s);
3866 free_key_list(public_keys);
3867 free_key_list(private_keys);
3874 const OPTIONS *test_get_options(void)
3876 static const OPTIONS test_options[] = {
3877 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
3878 { "config", OPT_CONFIG_FILE, '<',
3879 "The configuration file to use for the libctx" },
3880 { OPT_HELP_STR, 1, '-', "file\tFile to run tests on.\n" },
3883 return test_options;
3886 int setup_tests(void)
3889 char *config_file = NULL;
3893 while ((o = opt_next()) != OPT_EOF) {
3895 case OPT_CONFIG_FILE:
3896 config_file = opt_arg();
3898 case OPT_TEST_CASES:
3907 * Load the provider via configuration into the created library context.
3908 * Load the 'null' provider into the default library context to ensure that
3909 * the tests do not fallback to using the default provider.
3911 if (!test_get_libctx(&libctx, &prov_null, config_file, NULL, NULL))
3914 n = test_get_argument_count();
3918 ADD_ALL_TESTS(run_file_tests, n);
3922 void cleanup_tests(void)
3924 OSSL_PROVIDER_unload(prov_null);
3925 OSSL_LIB_CTX_free(libctx);
3928 #define STR_STARTS_WITH(str, pre) strncasecmp(pre, str, strlen(pre)) == 0
3929 #define STR_ENDS_WITH(str, pre) \
3930 strlen(str) < strlen(pre) ? 0 : (strcasecmp(pre, str + strlen(str) - strlen(pre)) == 0)
3932 static int is_digest_disabled(const char *name)
3934 #ifdef OPENSSL_NO_BLAKE2
3935 if (STR_STARTS_WITH(name, "BLAKE"))
3938 #ifdef OPENSSL_NO_MD2
3939 if (strcasecmp(name, "MD2") == 0)
3942 #ifdef OPENSSL_NO_MDC2
3943 if (strcasecmp(name, "MDC2") == 0)
3946 #ifdef OPENSSL_NO_MD4
3947 if (strcasecmp(name, "MD4") == 0)
3950 #ifdef OPENSSL_NO_MD5
3951 if (strcasecmp(name, "MD5") == 0)
3954 #ifdef OPENSSL_NO_RMD160
3955 if (strcasecmp(name, "RIPEMD160") == 0)
3958 #ifdef OPENSSL_NO_SM3
3959 if (strcasecmp(name, "SM3") == 0)
3962 #ifdef OPENSSL_NO_WHIRLPOOL
3963 if (strcasecmp(name, "WHIRLPOOL") == 0)
3969 static int is_pkey_disabled(const char *name)
3971 #ifdef OPENSSL_NO_EC
3972 if (STR_STARTS_WITH(name, "EC"))
3975 #ifdef OPENSSL_NO_DH
3976 if (STR_STARTS_WITH(name, "DH"))
3979 #ifdef OPENSSL_NO_DSA
3980 if (STR_STARTS_WITH(name, "DSA"))
3986 static int is_mac_disabled(const char *name)
3988 #ifdef OPENSSL_NO_BLAKE2
3989 if (STR_STARTS_WITH(name, "BLAKE2BMAC")
3990 || STR_STARTS_WITH(name, "BLAKE2SMAC"))
3993 #ifdef OPENSSL_NO_CMAC
3994 if (STR_STARTS_WITH(name, "CMAC"))
3997 #ifdef OPENSSL_NO_POLY1305
3998 if (STR_STARTS_WITH(name, "Poly1305"))
4001 #ifdef OPENSSL_NO_SIPHASH
4002 if (STR_STARTS_WITH(name, "SipHash"))
4007 static int is_kdf_disabled(const char *name)
4009 #ifdef OPENSSL_NO_SCRYPT
4010 if (STR_ENDS_WITH(name, "SCRYPT"))
4016 static int is_cipher_disabled(const char *name)
4018 #ifdef OPENSSL_NO_ARIA
4019 if (STR_STARTS_WITH(name, "ARIA"))
4022 #ifdef OPENSSL_NO_BF
4023 if (STR_STARTS_WITH(name, "BF"))
4026 #ifdef OPENSSL_NO_CAMELLIA
4027 if (STR_STARTS_WITH(name, "CAMELLIA"))
4030 #ifdef OPENSSL_NO_CAST
4031 if (STR_STARTS_WITH(name, "CAST"))
4034 #ifdef OPENSSL_NO_CHACHA
4035 if (STR_STARTS_WITH(name, "CHACHA"))
4038 #ifdef OPENSSL_NO_POLY1305
4039 if (STR_ENDS_WITH(name, "Poly1305"))
4042 #ifdef OPENSSL_NO_DES
4043 if (STR_STARTS_WITH(name, "DES"))
4045 if (STR_ENDS_WITH(name, "3DESwrap"))
4048 #ifdef OPENSSL_NO_OCB
4049 if (STR_ENDS_WITH(name, "OCB"))
4052 #ifdef OPENSSL_NO_IDEA
4053 if (STR_STARTS_WITH(name, "IDEA"))
4056 #ifdef OPENSSL_NO_RC2
4057 if (STR_STARTS_WITH(name, "RC2"))
4060 #ifdef OPENSSL_NO_RC4
4061 if (STR_STARTS_WITH(name, "RC4"))
4064 #ifdef OPENSSL_NO_RC5
4065 if (STR_STARTS_WITH(name, "RC5"))
4068 #ifdef OPENSSL_NO_SEED
4069 if (STR_STARTS_WITH(name, "SEED"))
4072 #ifdef OPENSSL_NO_SIV
4073 if (STR_ENDS_WITH(name, "SIV"))
4076 #ifdef OPENSSL_NO_SM4
4077 if (STR_STARTS_WITH(name, "SM4"))