2 * Copyright 2015-2021 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 cdat = OPENSSL_zalloc(sizeof(*cdat));
578 cdat->cipher = cipher;
579 cdat->fetched_cipher = fetched_cipher;
581 m = EVP_CIPHER_get_mode(cipher);
582 if (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
583 cdat->aead = m != 0 ? m : -1;
588 if (fetched_cipher != NULL)
589 TEST_info("%s is fetched", alg);
593 static void cipher_test_cleanup(EVP_TEST *t)
596 CIPHER_DATA *cdat = t->data;
598 OPENSSL_free(cdat->key);
599 OPENSSL_free(cdat->iv);
600 OPENSSL_free(cdat->next_iv);
601 OPENSSL_free(cdat->ciphertext);
602 OPENSSL_free(cdat->plaintext);
603 for (i = 0; i < AAD_NUM; i++)
604 OPENSSL_free(cdat->aad[i]);
605 OPENSSL_free(cdat->tag);
606 OPENSSL_free(cdat->mac_key);
607 EVP_CIPHER_free(cdat->fetched_cipher);
610 static int cipher_test_parse(EVP_TEST *t, const char *keyword,
613 CIPHER_DATA *cdat = t->data;
616 if (strcmp(keyword, "Key") == 0)
617 return parse_bin(value, &cdat->key, &cdat->key_len);
618 if (strcmp(keyword, "Rounds") == 0) {
622 cdat->rounds = (unsigned int)i;
625 if (strcmp(keyword, "IV") == 0)
626 return parse_bin(value, &cdat->iv, &cdat->iv_len);
627 if (strcmp(keyword, "NextIV") == 0)
628 return parse_bin(value, &cdat->next_iv, &cdat->iv_len);
629 if (strcmp(keyword, "Plaintext") == 0)
630 return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len);
631 if (strcmp(keyword, "Ciphertext") == 0)
632 return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
633 if (strcmp(keyword, "KeyBits") == 0) {
637 cdat->key_bits = (size_t)i;
643 if (strcmp(keyword, "TLSAAD") == 0)
644 cdat->tls_aad = tls_aad = 1;
645 if (strcmp(keyword, "AAD") == 0 || tls_aad) {
646 for (i = 0; i < AAD_NUM; i++) {
647 if (cdat->aad[i] == NULL)
648 return parse_bin(value, &cdat->aad[i], &cdat->aad_len[i]);
652 if (strcmp(keyword, "Tag") == 0)
653 return parse_bin(value, &cdat->tag, &cdat->tag_len);
654 if (strcmp(keyword, "SetTagLate") == 0) {
655 if (strcmp(value, "TRUE") == 0)
657 else if (strcmp(value, "FALSE") == 0)
663 if (strcmp(keyword, "MACKey") == 0)
664 return parse_bin(value, &cdat->mac_key, &cdat->mac_key_len);
665 if (strcmp(keyword, "TLSVersion") == 0) {
668 cdat->tls_version = (int)strtol(value, &endptr, 0);
669 return value[0] != '\0' && endptr[0] == '\0';
673 if (strcmp(keyword, "Operation") == 0) {
674 if (strcmp(value, "ENCRYPT") == 0)
676 else if (strcmp(value, "DECRYPT") == 0)
682 if (strcmp(keyword, "CTSMode") == 0) {
683 cdat->cts_mode = value;
689 static int cipher_test_enc(EVP_TEST *t, int enc,
690 size_t out_misalign, size_t inp_misalign, int frag)
692 CIPHER_DATA *expected = t->data;
693 unsigned char *in, *expected_out, *tmp = NULL;
694 size_t in_len, out_len, donelen = 0;
695 int ok = 0, tmplen, chunklen, tmpflen, i;
696 EVP_CIPHER_CTX *ctx_base = NULL;
697 EVP_CIPHER_CTX *ctx = NULL;
699 t->err = "TEST_FAILURE";
700 if (!TEST_ptr(ctx_base = EVP_CIPHER_CTX_new()))
702 if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
704 EVP_CIPHER_CTX_set_flags(ctx_base, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
706 in = expected->plaintext;
707 in_len = expected->plaintext_len;
708 expected_out = expected->ciphertext;
709 out_len = expected->ciphertext_len;
711 in = expected->ciphertext;
712 in_len = expected->ciphertext_len;
713 expected_out = expected->plaintext;
714 out_len = expected->plaintext_len;
716 if (inp_misalign == (size_t)-1) {
717 /* Exercise in-place encryption */
718 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
721 in = memcpy(tmp + out_misalign, in, in_len);
723 inp_misalign += 16 - ((out_misalign + in_len) & 15);
725 * 'tmp' will store both output and copy of input. We make the copy
726 * of input to specifically aligned part of 'tmp'. So we just
727 * figured out how much padding would ensure the required alignment,
728 * now we allocate extended buffer and finally copy the input just
729 * past inp_misalign in expression below. Output will be written
730 * past out_misalign...
732 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
733 inp_misalign + in_len);
736 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
737 inp_misalign, in, in_len);
739 if (!EVP_CipherInit_ex(ctx_base, expected->cipher, NULL, NULL, NULL, enc)) {
740 t->err = "CIPHERINIT_ERROR";
743 if (expected->cts_mode != NULL) {
744 OSSL_PARAM params[2];
746 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE,
747 (char *)expected->cts_mode,
749 params[1] = OSSL_PARAM_construct_end();
750 if (!EVP_CIPHER_CTX_set_params(ctx_base, params)) {
751 t->err = "INVALID_CTS_MODE";
756 if (expected->aead) {
757 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_IVLEN,
758 expected->iv_len, 0)) {
759 t->err = "INVALID_IV_LENGTH";
762 } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_get_iv_length(ctx_base)) {
763 t->err = "INVALID_IV_LENGTH";
767 if (expected->aead && !expected->tls_aad) {
770 * If encrypting or OCB just set tag length initially, otherwise
771 * set tag length and value.
773 if (enc || expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late) {
774 t->err = "TAG_LENGTH_SET_ERROR";
777 t->err = "TAG_SET_ERROR";
780 if (tag || expected->aead != EVP_CIPH_GCM_MODE) {
781 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_TAG,
782 expected->tag_len, tag))
787 if (expected->rounds > 0) {
788 int rounds = (int)expected->rounds;
790 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC5_ROUNDS, rounds, NULL)) {
791 t->err = "INVALID_ROUNDS";
796 if (!EVP_CIPHER_CTX_set_key_length(ctx_base, expected->key_len)) {
797 t->err = "INVALID_KEY_LENGTH";
800 if (expected->key_bits > 0) {
801 int bits = (int)expected->key_bits;
803 if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC2_KEY_BITS, bits, NULL)) {
804 t->err = "INVALID KEY BITS";
808 if (!EVP_CipherInit_ex(ctx_base, NULL, NULL, expected->key, expected->iv, -1)) {
809 t->err = "KEY_SET_ERROR";
813 /* Check that we get the same IV back */
814 if (expected->iv != NULL) {
815 /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
816 unsigned char iv[128];
817 if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx_base, iv, sizeof(iv)))
818 || ((EVP_CIPHER_get_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
819 && !TEST_mem_eq(expected->iv, expected->iv_len, iv,
820 expected->iv_len))) {
821 t->err = "INVALID_IV";
826 /* Test that the cipher dup functions correctly if it is supported */
828 if (EVP_CIPHER_CTX_copy(ctx, ctx_base)) {
829 EVP_CIPHER_CTX_free(ctx_base);
832 EVP_CIPHER_CTX_free(ctx);
837 if (expected->mac_key != NULL
838 && !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY,
839 (int)expected->mac_key_len,
840 (void *)expected->mac_key)) {
841 t->err = "SET_MAC_KEY_ERROR";
845 if (expected->tls_version) {
846 OSSL_PARAM params[2];
848 params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS_VERSION,
849 &expected->tls_version);
850 params[1] = OSSL_PARAM_construct_end();
851 if (!EVP_CIPHER_CTX_set_params(ctx, params)) {
852 t->err = "SET_TLS_VERSION_ERROR";
857 if (expected->aead == EVP_CIPH_CCM_MODE) {
858 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
859 t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
863 if (expected->aad[0] != NULL && !expected->tls_aad) {
864 t->err = "AAD_SET_ERROR";
866 for (i = 0; expected->aad[i] != NULL; i++) {
867 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i],
868 expected->aad_len[i]))
873 * Supply the AAD in chunks less than the block size where possible
875 for (i = 0; expected->aad[i] != NULL; i++) {
876 if (expected->aad_len[i] > 0) {
877 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], 1))
881 if (expected->aad_len[i] > 2) {
882 if (!EVP_CipherUpdate(ctx, NULL, &chunklen,
883 expected->aad[i] + donelen,
884 expected->aad_len[i] - 2))
886 donelen += expected->aad_len[i] - 2;
888 if (expected->aad_len[i] > 1
889 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
890 expected->aad[i] + donelen, 1))
896 if (expected->tls_aad) {
897 OSSL_PARAM params[2];
900 /* duplicate the aad as the implementation might modify it */
901 if ((tls_aad = OPENSSL_memdup(expected->aad[0],
902 expected->aad_len[0])) == NULL)
904 params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
906 expected->aad_len[0]);
907 params[1] = OSSL_PARAM_construct_end();
908 if (!EVP_CIPHER_CTX_set_params(ctx, params)) {
909 OPENSSL_free(tls_aad);
910 t->err = "TLS1_AAD_ERROR";
913 OPENSSL_free(tls_aad);
914 } else if (!enc && (expected->aead == EVP_CIPH_OCB_MODE
915 || expected->tag_late)) {
916 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
917 expected->tag_len, expected->tag)) {
918 t->err = "TAG_SET_ERROR";
923 EVP_CIPHER_CTX_set_padding(ctx, 0);
924 t->err = "CIPHERUPDATE_ERROR";
927 /* We supply the data all in one go */
928 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
931 /* Supply the data in chunks less than the block size where possible */
933 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
940 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
948 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
954 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
955 t->err = "CIPHERFINAL_ERROR";
958 if (!enc && expected->tls_aad) {
959 if (expected->tls_version >= TLS1_1_VERSION
960 && (EVP_CIPHER_is_a(expected->cipher, "AES-128-CBC-HMAC-SHA1")
961 || EVP_CIPHER_is_a(expected->cipher, "AES-256-CBC-HMAC-SHA1"))) {
962 tmplen -= expected->iv_len;
963 expected_out += expected->iv_len;
964 out_misalign += expected->iv_len;
966 if ((int)out_len > tmplen + tmpflen)
967 out_len = tmplen + tmpflen;
969 if (!memory_err_compare(t, "VALUE_MISMATCH", expected_out, out_len,
970 tmp + out_misalign, tmplen + tmpflen))
972 if (enc && expected->aead && !expected->tls_aad) {
973 unsigned char rtag[16];
975 if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) {
976 t->err = "TAG_LENGTH_INTERNAL_ERROR";
979 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
980 expected->tag_len, rtag)) {
981 t->err = "TAG_RETRIEVE_ERROR";
984 if (!memory_err_compare(t, "TAG_VALUE_MISMATCH",
985 expected->tag, expected->tag_len,
986 rtag, expected->tag_len))
989 /* Check the updated IV */
990 if (expected->next_iv != NULL) {
991 /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
992 unsigned char iv[128];
993 if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv)))
994 || ((EVP_CIPHER_get_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
995 && !TEST_mem_eq(expected->next_iv, expected->iv_len, iv,
996 expected->iv_len))) {
997 t->err = "INVALID_NEXT_IV";
1006 if (ctx != ctx_base)
1007 EVP_CIPHER_CTX_free(ctx_base);
1008 EVP_CIPHER_CTX_free(ctx);
1012 static int cipher_test_run(EVP_TEST *t)
1014 CIPHER_DATA *cdat = t->data;
1016 size_t out_misalign, inp_misalign;
1022 if (!cdat->iv && EVP_CIPHER_get_iv_length(cdat->cipher)) {
1023 /* IV is optional and usually omitted in wrap mode */
1024 if (EVP_CIPHER_get_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
1029 if (cdat->aead && cdat->tag == NULL && !cdat->tls_aad) {
1033 for (out_misalign = 0; out_misalign <= 1;) {
1034 static char aux_err[64];
1035 t->aux_err = aux_err;
1036 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
1037 if (inp_misalign == (size_t)-1) {
1038 /* kludge: inp_misalign == -1 means "exercise in-place" */
1039 BIO_snprintf(aux_err, sizeof(aux_err),
1040 "%s in-place, %sfragmented",
1041 out_misalign ? "misaligned" : "aligned",
1042 frag ? "" : "not ");
1044 BIO_snprintf(aux_err, sizeof(aux_err),
1045 "%s output and %s input, %sfragmented",
1046 out_misalign ? "misaligned" : "aligned",
1047 inp_misalign ? "misaligned" : "aligned",
1048 frag ? "" : "not ");
1051 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
1052 /* Not fatal errors: return */
1059 if (cdat->enc != 1) {
1060 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
1061 /* Not fatal errors: return */
1070 if (out_misalign == 1 && frag == 0) {
1072 * XTS, SIV, CCM, stitched ciphers and Wrap modes have special
1073 * requirements about input lengths so we don't fragment for those
1075 if (cdat->aead == EVP_CIPH_CCM_MODE
1076 || cdat->aead == EVP_CIPH_CBC_MODE
1077 || (cdat->aead == -1
1078 && EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_STREAM_CIPHER)
1079 || ((EVP_CIPHER_get_flags(cdat->cipher) & EVP_CIPH_FLAG_CTS) != 0)
1080 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_SIV_MODE
1081 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
1082 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
1095 static const EVP_TEST_METHOD cipher_test_method = {
1098 cipher_test_cleanup,
1108 typedef struct mac_data_st {
1109 /* MAC type in one form or another */
1111 EVP_MAC *mac; /* for mac_test_run_mac */
1112 int type; /* for mac_test_run_pkey */
1113 /* Algorithm string for this MAC */
1122 unsigned char *input;
1124 /* Expected output */
1125 unsigned char *output;
1127 unsigned char *custom;
1129 /* MAC salt (blake2) */
1130 unsigned char *salt;
1134 /* Collection of controls */
1135 STACK_OF(OPENSSL_STRING) *controls;
1142 static int mac_test_init(EVP_TEST *t, const char *alg)
1144 EVP_MAC *mac = NULL;
1145 int type = NID_undef;
1148 if (is_mac_disabled(alg)) {
1149 TEST_info("skipping, '%s' is disabled", alg);
1153 if ((mac = EVP_MAC_fetch(libctx, alg, NULL)) == NULL) {
1155 * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
1156 * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
1157 * the EVP_PKEY method.
1159 size_t sz = strlen(alg);
1160 static const char epilogue[] = " by EVP_PKEY";
1162 if (sz >= sizeof(epilogue)
1163 && strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0)
1164 sz -= sizeof(epilogue) - 1;
1166 if (strncmp(alg, "HMAC", sz) == 0)
1167 type = EVP_PKEY_HMAC;
1168 else if (strncmp(alg, "CMAC", sz) == 0)
1169 type = EVP_PKEY_CMAC;
1170 else if (strncmp(alg, "Poly1305", sz) == 0)
1171 type = EVP_PKEY_POLY1305;
1172 else if (strncmp(alg, "SipHash", sz) == 0)
1173 type = EVP_PKEY_SIPHASH;
1178 mdat = OPENSSL_zalloc(sizeof(*mdat));
1180 mdat->mac_name = OPENSSL_strdup(alg);
1182 mdat->controls = sk_OPENSSL_STRING_new_null();
1183 mdat->output_size = mdat->block_size = -1;
1188 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
1189 static void openssl_free(char *m)
1194 static void mac_test_cleanup(EVP_TEST *t)
1196 MAC_DATA *mdat = t->data;
1198 EVP_MAC_free(mdat->mac);
1199 OPENSSL_free(mdat->mac_name);
1200 sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free);
1201 OPENSSL_free(mdat->alg);
1202 OPENSSL_free(mdat->key);
1203 OPENSSL_free(mdat->iv);
1204 OPENSSL_free(mdat->custom);
1205 OPENSSL_free(mdat->salt);
1206 OPENSSL_free(mdat->input);
1207 OPENSSL_free(mdat->output);
1210 static int mac_test_parse(EVP_TEST *t,
1211 const char *keyword, const char *value)
1213 MAC_DATA *mdata = t->data;
1215 if (strcmp(keyword, "Key") == 0)
1216 return parse_bin(value, &mdata->key, &mdata->key_len);
1217 if (strcmp(keyword, "IV") == 0)
1218 return parse_bin(value, &mdata->iv, &mdata->iv_len);
1219 if (strcmp(keyword, "Custom") == 0)
1220 return parse_bin(value, &mdata->custom, &mdata->custom_len);
1221 if (strcmp(keyword, "Salt") == 0)
1222 return parse_bin(value, &mdata->salt, &mdata->salt_len);
1223 if (strcmp(keyword, "Algorithm") == 0) {
1224 mdata->alg = OPENSSL_strdup(value);
1229 if (strcmp(keyword, "Input") == 0)
1230 return parse_bin(value, &mdata->input, &mdata->input_len);
1231 if (strcmp(keyword, "Output") == 0)
1232 return parse_bin(value, &mdata->output, &mdata->output_len);
1233 if (strcmp(keyword, "XOF") == 0)
1234 return mdata->xof = 1;
1235 if (strcmp(keyword, "Ctrl") == 0)
1236 return sk_OPENSSL_STRING_push(mdata->controls,
1237 OPENSSL_strdup(value)) != 0;
1238 if (strcmp(keyword, "OutputSize") == 0) {
1239 mdata->output_size = atoi(value);
1240 if (mdata->output_size < 0)
1244 if (strcmp(keyword, "BlockSize") == 0) {
1245 mdata->block_size = atoi(value);
1246 if (mdata->block_size < 0)
1253 static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1259 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1261 p = strchr(tmpval, ':');
1264 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1267 t->err = "PKEY_CTRL_INVALID";
1269 t->err = "PKEY_CTRL_ERROR";
1272 OPENSSL_free(tmpval);
1276 static int mac_test_run_pkey(EVP_TEST *t)
1278 MAC_DATA *expected = t->data;
1279 EVP_MD_CTX *mctx = NULL;
1280 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1281 EVP_PKEY *key = NULL;
1282 const char *mdname = NULL;
1283 EVP_CIPHER *cipher = NULL;
1284 unsigned char *got = NULL;
1288 /* We don't do XOF mode via PKEY */
1292 if (expected->alg == NULL)
1293 TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type));
1295 TEST_info("Trying the EVP_PKEY %s test with %s",
1296 OBJ_nid2sn(expected->type), expected->alg);
1298 if (expected->type == EVP_PKEY_CMAC) {
1299 #ifdef OPENSSL_NO_DEPRECATED_3_0
1300 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
1305 OSSL_LIB_CTX *tmpctx;
1307 if (expected->alg != NULL && is_cipher_disabled(expected->alg)) {
1308 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
1313 if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, expected->alg, NULL))) {
1314 t->err = "MAC_KEY_CREATE_ERROR";
1317 tmpctx = OSSL_LIB_CTX_set0_default(libctx);
1318 key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len,
1320 OSSL_LIB_CTX_set0_default(tmpctx);
1323 key = EVP_PKEY_new_raw_private_key_ex(libctx,
1324 OBJ_nid2sn(expected->type), NULL,
1325 expected->key, expected->key_len);
1328 t->err = "MAC_KEY_CREATE_ERROR";
1332 if (expected->type == EVP_PKEY_HMAC && expected->alg != NULL) {
1333 if (is_digest_disabled(expected->alg)) {
1334 TEST_info("skipping, HMAC '%s' is disabled", expected->alg);
1339 mdname = expected->alg;
1341 if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
1342 t->err = "INTERNAL_ERROR";
1345 if (!EVP_DigestSignInit_ex(mctx, &pctx, mdname, libctx, NULL, key, NULL)) {
1346 t->err = "DIGESTSIGNINIT_ERROR";
1349 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++)
1350 if (!mac_test_ctrl_pkey(t, pctx,
1351 sk_OPENSSL_STRING_value(expected->controls,
1353 t->err = "EVPPKEYCTXCTRL_ERROR";
1356 if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) {
1357 t->err = "DIGESTSIGNUPDATE_ERROR";
1360 if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) {
1361 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1364 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1365 t->err = "TEST_FAILURE";
1368 if (!EVP_DigestSignFinal(mctx, got, &got_len)
1369 || !memory_err_compare(t, "TEST_MAC_ERR",
1370 expected->output, expected->output_len,
1372 t->err = "TEST_MAC_ERR";
1377 EVP_CIPHER_free(cipher);
1378 EVP_MD_CTX_free(mctx);
1380 EVP_PKEY_CTX_free(genctx);
1385 static int mac_test_run_mac(EVP_TEST *t)
1387 MAC_DATA *expected = t->data;
1388 EVP_MAC_CTX *ctx = NULL;
1389 unsigned char *got = NULL;
1390 size_t got_len = 0, size = 0;
1391 int i, block_size = -1, output_size = -1;
1392 OSSL_PARAM params[21], sizes[3], *psizes = sizes;
1393 size_t params_n = 0;
1394 size_t params_n_allocstart = 0;
1395 const OSSL_PARAM *defined_params =
1396 EVP_MAC_settable_ctx_params(expected->mac);
1399 if (expected->alg == NULL)
1400 TEST_info("Trying the EVP_MAC %s test", expected->mac_name);
1402 TEST_info("Trying the EVP_MAC %s test with %s",
1403 expected->mac_name, expected->alg);
1405 if (expected->alg != NULL) {
1407 * The underlying algorithm may be a cipher or a digest.
1408 * We don't know which it is, but we can ask the MAC what it
1409 * should be and bet on that.
1411 if (OSSL_PARAM_locate_const(defined_params,
1412 OSSL_MAC_PARAM_CIPHER) != NULL) {
1413 params[params_n++] =
1414 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
1416 } else if (OSSL_PARAM_locate_const(defined_params,
1417 OSSL_MAC_PARAM_DIGEST) != NULL) {
1418 params[params_n++] =
1419 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
1422 t->err = "MAC_BAD_PARAMS";
1426 if (expected->custom != NULL)
1427 params[params_n++] =
1428 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
1430 expected->custom_len);
1431 if (expected->salt != NULL)
1432 params[params_n++] =
1433 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT,
1435 expected->salt_len);
1436 if (expected->iv != NULL)
1437 params[params_n++] =
1438 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
1442 /* Unknown controls. They must match parameters that the MAC recognizes */
1443 if (params_n + sk_OPENSSL_STRING_num(expected->controls)
1444 >= OSSL_NELEM(params)) {
1445 t->err = "MAC_TOO_MANY_PARAMETERS";
1448 params_n_allocstart = params_n;
1449 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) {
1450 char *tmpkey, *tmpval;
1451 char *value = sk_OPENSSL_STRING_value(expected->controls, i);
1453 if (!TEST_ptr(tmpkey = OPENSSL_strdup(value))) {
1454 t->err = "MAC_PARAM_ERROR";
1457 tmpval = strchr(tmpkey, ':');
1462 || !OSSL_PARAM_allocate_from_text(¶ms[params_n],
1465 strlen(tmpval), NULL)) {
1466 OPENSSL_free(tmpkey);
1467 t->err = "MAC_PARAM_ERROR";
1472 OPENSSL_free(tmpkey);
1474 params[params_n] = OSSL_PARAM_construct_end();
1476 if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) {
1477 t->err = "MAC_CREATE_ERROR";
1481 if (!EVP_MAC_init(ctx, expected->key, expected->key_len, params)) {
1482 t->err = "MAC_INIT_ERROR";
1485 if (expected->output_size >= 0)
1486 *psizes++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_SIZE,
1488 if (expected->block_size >= 0)
1489 *psizes++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_BLOCK_SIZE,
1491 if (psizes != sizes) {
1492 *psizes = OSSL_PARAM_construct_end();
1493 if (!TEST_true(EVP_MAC_CTX_get_params(ctx, sizes))) {
1494 t->err = "INTERNAL_ERROR";
1497 if (expected->output_size >= 0
1498 && !TEST_int_eq(output_size, expected->output_size)) {
1499 t->err = "TEST_FAILURE";
1502 if (expected->block_size >= 0
1503 && !TEST_int_eq(block_size, expected->block_size)) {
1504 t->err = "TEST_FAILURE";
1508 if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) {
1509 t->err = "MAC_UPDATE_ERROR";
1512 xof = expected->xof;
1514 if (!TEST_ptr(got = OPENSSL_malloc(expected->output_len))) {
1515 t->err = "TEST_FAILURE";
1518 if (!EVP_MAC_finalXOF(ctx, got, expected->output_len)
1519 || !memory_err_compare(t, "TEST_MAC_ERR",
1520 expected->output, expected->output_len,
1521 got, expected->output_len)) {
1522 t->err = "MAC_FINAL_ERROR";
1526 if (!EVP_MAC_final(ctx, NULL, &got_len, 0)) {
1527 t->err = "MAC_FINAL_LENGTH_ERROR";
1530 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1531 t->err = "TEST_FAILURE";
1534 if (!EVP_MAC_final(ctx, got, &got_len, got_len)
1535 || !memory_err_compare(t, "TEST_MAC_ERR",
1536 expected->output, expected->output_len,
1538 t->err = "TEST_MAC_ERR";
1544 /* Test the EVP_Q_mac interface as well */
1546 OPENSSL_cleanse(got, got_len);
1547 if (!TEST_true(EVP_Q_mac(libctx, expected->mac_name, NULL,
1548 expected->alg, params,
1549 expected->key, expected->key_len,
1550 expected->input, expected->input_len,
1551 got, got_len, &size))
1552 || !TEST_mem_eq(got, size,
1553 expected->output, expected->output_len)) {
1554 t->err = "EVP_Q_mac failed";
1559 while (params_n-- > params_n_allocstart) {
1560 OPENSSL_free(params[params_n].data);
1562 EVP_MAC_CTX_free(ctx);
1567 static int mac_test_run(EVP_TEST *t)
1569 MAC_DATA *expected = t->data;
1571 if (expected->mac != NULL)
1572 return mac_test_run_mac(t);
1573 return mac_test_run_pkey(t);
1576 static const EVP_TEST_METHOD mac_test_method = {
1587 ** These are all very similar and share much common code.
1590 typedef struct pkey_data_st {
1591 /* Context for this operation */
1593 /* Key operation to perform */
1594 int (*keyop) (EVP_PKEY_CTX *ctx,
1595 unsigned char *sig, size_t *siglen,
1596 const unsigned char *tbs, size_t tbslen);
1598 unsigned char *input;
1600 /* Expected output */
1601 unsigned char *output;
1606 * Perform public key operation setup: lookup key, allocated ctx and call
1607 * the appropriate initialisation function
1609 static int pkey_test_init(EVP_TEST *t, const char *name,
1611 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1612 int (*keyop)(EVP_PKEY_CTX *ctx,
1613 unsigned char *sig, size_t *siglen,
1614 const unsigned char *tbs,
1618 EVP_PKEY *pkey = NULL;
1622 rv = find_key(&pkey, name, public_keys);
1624 rv = find_key(&pkey, name, private_keys);
1625 if (rv == 0 || pkey == NULL) {
1626 TEST_info("skipping, key '%s' is disabled", name);
1631 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) {
1632 EVP_PKEY_free(pkey);
1635 kdata->keyop = keyop;
1636 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL))) {
1637 EVP_PKEY_free(pkey);
1638 OPENSSL_free(kdata);
1641 if (keyopinit(kdata->ctx) <= 0)
1642 t->err = "KEYOP_INIT_ERROR";
1647 static void pkey_test_cleanup(EVP_TEST *t)
1649 PKEY_DATA *kdata = t->data;
1651 OPENSSL_free(kdata->input);
1652 OPENSSL_free(kdata->output);
1653 EVP_PKEY_CTX_free(kdata->ctx);
1656 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1662 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1664 p = strchr(tmpval, ':');
1667 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1670 t->err = "PKEY_CTRL_INVALID";
1672 } else if (p != NULL && rv <= 0) {
1673 if (is_digest_disabled(p) || is_cipher_disabled(p)) {
1674 TEST_info("skipping, '%s' is disabled", p);
1678 t->err = "PKEY_CTRL_ERROR";
1682 OPENSSL_free(tmpval);
1686 static int pkey_test_parse(EVP_TEST *t,
1687 const char *keyword, const char *value)
1689 PKEY_DATA *kdata = t->data;
1690 if (strcmp(keyword, "Input") == 0)
1691 return parse_bin(value, &kdata->input, &kdata->input_len);
1692 if (strcmp(keyword, "Output") == 0)
1693 return parse_bin(value, &kdata->output, &kdata->output_len);
1694 if (strcmp(keyword, "Ctrl") == 0)
1695 return pkey_test_ctrl(t, kdata->ctx, value);
1699 static int pkey_test_run(EVP_TEST *t)
1701 PKEY_DATA *expected = t->data;
1702 unsigned char *got = NULL;
1704 EVP_PKEY_CTX *copy = NULL;
1706 if (expected->keyop(expected->ctx, NULL, &got_len,
1707 expected->input, expected->input_len) <= 0
1708 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1709 t->err = "KEYOP_LENGTH_ERROR";
1712 if (expected->keyop(expected->ctx, got, &got_len,
1713 expected->input, expected->input_len) <= 0) {
1714 t->err = "KEYOP_ERROR";
1717 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1718 expected->output, expected->output_len,
1726 /* Repeat the test on a copy. */
1727 if (!TEST_ptr(copy = EVP_PKEY_CTX_dup(expected->ctx))) {
1728 t->err = "INTERNAL_ERROR";
1731 if (expected->keyop(copy, NULL, &got_len, expected->input,
1732 expected->input_len) <= 0
1733 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1734 t->err = "KEYOP_LENGTH_ERROR";
1737 if (expected->keyop(copy, got, &got_len, expected->input,
1738 expected->input_len) <= 0) {
1739 t->err = "KEYOP_ERROR";
1742 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1743 expected->output, expected->output_len,
1749 EVP_PKEY_CTX_free(copy);
1753 static int sign_test_init(EVP_TEST *t, const char *name)
1755 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1758 static const EVP_TEST_METHOD psign_test_method = {
1766 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1768 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1769 EVP_PKEY_verify_recover);
1772 static const EVP_TEST_METHOD pverify_recover_test_method = {
1774 verify_recover_test_init,
1780 static int decrypt_test_init(EVP_TEST *t, const char *name)
1782 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1786 static const EVP_TEST_METHOD pdecrypt_test_method = {
1794 static int verify_test_init(EVP_TEST *t, const char *name)
1796 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1799 static int verify_test_run(EVP_TEST *t)
1801 PKEY_DATA *kdata = t->data;
1803 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1804 kdata->input, kdata->input_len) <= 0)
1805 t->err = "VERIFY_ERROR";
1809 static const EVP_TEST_METHOD pverify_test_method = {
1817 static int pderive_test_init(EVP_TEST *t, const char *name)
1819 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1822 static int pderive_test_parse(EVP_TEST *t,
1823 const char *keyword, const char *value)
1825 PKEY_DATA *kdata = t->data;
1828 if (strcmp(keyword, "PeerKeyValidate") == 0)
1831 if (validate || strcmp(keyword, "PeerKey") == 0) {
1833 if (find_key(&peer, value, public_keys) == 0)
1835 if (EVP_PKEY_derive_set_peer_ex(kdata->ctx, peer, validate) <= 0) {
1836 t->err = "DERIVE_SET_PEER_ERROR";
1842 if (strcmp(keyword, "SharedSecret") == 0)
1843 return parse_bin(value, &kdata->output, &kdata->output_len);
1844 if (strcmp(keyword, "Ctrl") == 0)
1845 return pkey_test_ctrl(t, kdata->ctx, value);
1849 static int pderive_test_run(EVP_TEST *t)
1851 PKEY_DATA *expected = t->data;
1852 unsigned char *got = NULL;
1855 if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) {
1856 t->err = "DERIVE_ERROR";
1859 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1860 t->err = "DERIVE_ERROR";
1863 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
1864 t->err = "DERIVE_ERROR";
1867 if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH",
1868 expected->output, expected->output_len,
1878 static const EVP_TEST_METHOD pderive_test_method = {
1891 typedef enum pbe_type_enum {
1892 PBE_TYPE_INVALID = 0,
1893 PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
1896 typedef struct pbe_data_st {
1898 /* scrypt parameters */
1899 uint64_t N, r, p, maxmem;
1900 /* PKCS#12 parameters */
1904 unsigned char *pass;
1907 unsigned char *salt;
1909 /* Expected output */
1914 #ifndef OPENSSL_NO_SCRYPT
1915 /* Parse unsigned decimal 64 bit integer value */
1916 static int parse_uint64(const char *value, uint64_t *pr)
1918 const char *p = value;
1920 if (!TEST_true(*p)) {
1921 TEST_info("Invalid empty integer value");
1924 for (*pr = 0; *p; ) {
1925 if (*pr > UINT64_MAX / 10) {
1926 TEST_error("Integer overflow in string %s", value);
1930 if (!TEST_true(isdigit((unsigned char)*p))) {
1931 TEST_error("Invalid character in string %s", value);
1940 static int scrypt_test_parse(EVP_TEST *t,
1941 const char *keyword, const char *value)
1943 PBE_DATA *pdata = t->data;
1945 if (strcmp(keyword, "N") == 0)
1946 return parse_uint64(value, &pdata->N);
1947 if (strcmp(keyword, "p") == 0)
1948 return parse_uint64(value, &pdata->p);
1949 if (strcmp(keyword, "r") == 0)
1950 return parse_uint64(value, &pdata->r);
1951 if (strcmp(keyword, "maxmem") == 0)
1952 return parse_uint64(value, &pdata->maxmem);
1957 static int pbkdf2_test_parse(EVP_TEST *t,
1958 const char *keyword, const char *value)
1960 PBE_DATA *pdata = t->data;
1962 if (strcmp(keyword, "iter") == 0) {
1963 pdata->iter = atoi(value);
1964 if (pdata->iter <= 0)
1968 if (strcmp(keyword, "MD") == 0) {
1969 pdata->md = EVP_get_digestbyname(value);
1970 if (pdata->md == NULL)
1977 static int pkcs12_test_parse(EVP_TEST *t,
1978 const char *keyword, const char *value)
1980 PBE_DATA *pdata = t->data;
1982 if (strcmp(keyword, "id") == 0) {
1983 pdata->id = atoi(value);
1988 return pbkdf2_test_parse(t, keyword, value);
1991 static int pbe_test_init(EVP_TEST *t, const char *alg)
1994 PBE_TYPE pbe_type = PBE_TYPE_INVALID;
1996 if (is_kdf_disabled(alg)) {
1997 TEST_info("skipping, '%s' is disabled", alg);
2001 if (strcmp(alg, "scrypt") == 0) {
2002 pbe_type = PBE_TYPE_SCRYPT;
2003 } else if (strcmp(alg, "pbkdf2") == 0) {
2004 pbe_type = PBE_TYPE_PBKDF2;
2005 } else if (strcmp(alg, "pkcs12") == 0) {
2006 pbe_type = PBE_TYPE_PKCS12;
2008 TEST_error("Unknown pbe algorithm %s", alg);
2011 if (!TEST_ptr(pdat = OPENSSL_zalloc(sizeof(*pdat))))
2013 pdat->pbe_type = pbe_type;
2018 static void pbe_test_cleanup(EVP_TEST *t)
2020 PBE_DATA *pdat = t->data;
2022 OPENSSL_free(pdat->pass);
2023 OPENSSL_free(pdat->salt);
2024 OPENSSL_free(pdat->key);
2027 static int pbe_test_parse(EVP_TEST *t,
2028 const char *keyword, const char *value)
2030 PBE_DATA *pdata = t->data;
2032 if (strcmp(keyword, "Password") == 0)
2033 return parse_bin(value, &pdata->pass, &pdata->pass_len);
2034 if (strcmp(keyword, "Salt") == 0)
2035 return parse_bin(value, &pdata->salt, &pdata->salt_len);
2036 if (strcmp(keyword, "Key") == 0)
2037 return parse_bin(value, &pdata->key, &pdata->key_len);
2038 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
2039 return pbkdf2_test_parse(t, keyword, value);
2040 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
2041 return pkcs12_test_parse(t, keyword, value);
2042 #ifndef OPENSSL_NO_SCRYPT
2043 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
2044 return scrypt_test_parse(t, keyword, value);
2049 static int pbe_test_run(EVP_TEST *t)
2051 PBE_DATA *expected = t->data;
2053 EVP_MD *fetched_digest = NULL;
2054 OSSL_LIB_CTX *save_libctx;
2056 save_libctx = OSSL_LIB_CTX_set0_default(libctx);
2058 if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
2059 t->err = "INTERNAL_ERROR";
2062 if (expected->pbe_type == PBE_TYPE_PBKDF2) {
2063 if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
2064 expected->salt, expected->salt_len,
2065 expected->iter, expected->md,
2066 expected->key_len, key) == 0) {
2067 t->err = "PBKDF2_ERROR";
2070 #ifndef OPENSSL_NO_SCRYPT
2071 } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
2072 if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
2073 expected->salt, expected->salt_len,
2074 expected->N, expected->r, expected->p,
2075 expected->maxmem, key, expected->key_len) == 0) {
2076 t->err = "SCRYPT_ERROR";
2080 } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
2081 fetched_digest = EVP_MD_fetch(libctx, EVP_MD_get0_name(expected->md),
2083 if (fetched_digest == NULL) {
2084 t->err = "PKCS12_ERROR";
2087 if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
2088 expected->salt, expected->salt_len,
2089 expected->id, expected->iter, expected->key_len,
2090 key, fetched_digest) == 0) {
2091 t->err = "PKCS12_ERROR";
2095 if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len,
2096 key, expected->key_len))
2101 EVP_MD_free(fetched_digest);
2103 OSSL_LIB_CTX_set0_default(save_libctx);
2107 static const EVP_TEST_METHOD pbe_test_method = {
2121 BASE64_CANONICAL_ENCODING = 0,
2122 BASE64_VALID_ENCODING = 1,
2123 BASE64_INVALID_ENCODING = 2
2124 } base64_encoding_type;
2126 typedef struct encode_data_st {
2127 /* Input to encoding */
2128 unsigned char *input;
2130 /* Expected output */
2131 unsigned char *output;
2133 base64_encoding_type encoding;
2136 static int encode_test_init(EVP_TEST *t, const char *encoding)
2140 if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
2142 if (strcmp(encoding, "canonical") == 0) {
2143 edata->encoding = BASE64_CANONICAL_ENCODING;
2144 } else if (strcmp(encoding, "valid") == 0) {
2145 edata->encoding = BASE64_VALID_ENCODING;
2146 } else if (strcmp(encoding, "invalid") == 0) {
2147 edata->encoding = BASE64_INVALID_ENCODING;
2148 if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
2151 TEST_error("Bad encoding: %s."
2152 " Should be one of {canonical, valid, invalid}",
2159 OPENSSL_free(edata);
2163 static void encode_test_cleanup(EVP_TEST *t)
2165 ENCODE_DATA *edata = t->data;
2167 OPENSSL_free(edata->input);
2168 OPENSSL_free(edata->output);
2169 memset(edata, 0, sizeof(*edata));
2172 static int encode_test_parse(EVP_TEST *t,
2173 const char *keyword, const char *value)
2175 ENCODE_DATA *edata = t->data;
2177 if (strcmp(keyword, "Input") == 0)
2178 return parse_bin(value, &edata->input, &edata->input_len);
2179 if (strcmp(keyword, "Output") == 0)
2180 return parse_bin(value, &edata->output, &edata->output_len);
2184 static int encode_test_run(EVP_TEST *t)
2186 ENCODE_DATA *expected = t->data;
2187 unsigned char *encode_out = NULL, *decode_out = NULL;
2188 int output_len, chunk_len;
2189 EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL;
2191 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
2192 t->err = "INTERNAL_ERROR";
2196 if (expected->encoding == BASE64_CANONICAL_ENCODING) {
2198 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
2199 || !TEST_ptr(encode_out =
2200 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
2203 EVP_EncodeInit(encode_ctx);
2204 if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
2205 expected->input, expected->input_len)))
2208 output_len = chunk_len;
2210 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
2211 output_len += chunk_len;
2213 if (!memory_err_compare(t, "BAD_ENCODING",
2214 expected->output, expected->output_len,
2215 encode_out, output_len))
2219 if (!TEST_ptr(decode_out =
2220 OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
2223 EVP_DecodeInit(decode_ctx);
2224 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
2225 expected->output_len) < 0) {
2226 t->err = "DECODE_ERROR";
2229 output_len = chunk_len;
2231 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
2232 t->err = "DECODE_ERROR";
2235 output_len += chunk_len;
2237 if (expected->encoding != BASE64_INVALID_ENCODING
2238 && !memory_err_compare(t, "BAD_DECODING",
2239 expected->input, expected->input_len,
2240 decode_out, output_len)) {
2241 t->err = "BAD_DECODING";
2247 OPENSSL_free(encode_out);
2248 OPENSSL_free(decode_out);
2249 EVP_ENCODE_CTX_free(decode_ctx);
2250 EVP_ENCODE_CTX_free(encode_ctx);
2254 static const EVP_TEST_METHOD encode_test_method = {
2257 encode_test_cleanup,
2266 #define MAX_RAND_REPEATS 15
2268 typedef struct rand_data_pass_st {
2269 unsigned char *entropy;
2270 unsigned char *reseed_entropy;
2271 unsigned char *nonce;
2272 unsigned char *pers;
2273 unsigned char *reseed_addin;
2274 unsigned char *addinA;
2275 unsigned char *addinB;
2276 unsigned char *pr_entropyA;
2277 unsigned char *pr_entropyB;
2278 unsigned char *output;
2279 size_t entropy_len, nonce_len, pers_len, addinA_len, addinB_len,
2280 pr_entropyA_len, pr_entropyB_len, output_len, reseed_entropy_len,
2284 typedef struct rand_data_st {
2285 /* Context for this operation */
2287 EVP_RAND_CTX *parent;
2289 int prediction_resistance;
2291 unsigned int generate_bits;
2295 /* Expected output */
2296 RAND_DATA_PASS data[MAX_RAND_REPEATS];
2299 static int rand_test_init(EVP_TEST *t, const char *name)
2303 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
2304 unsigned int strength = 256;
2306 if (!TEST_ptr(rdata = OPENSSL_zalloc(sizeof(*rdata))))
2309 /* TEST-RAND is available in the FIPS provider but not with "fips=yes" */
2310 rand = EVP_RAND_fetch(libctx, "TEST-RAND", "-fips");
2313 rdata->parent = EVP_RAND_CTX_new(rand, NULL);
2314 EVP_RAND_free(rand);
2315 if (rdata->parent == NULL)
2318 *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength);
2319 if (!EVP_RAND_CTX_set_params(rdata->parent, params))
2322 rand = EVP_RAND_fetch(libctx, name, NULL);
2325 rdata->ctx = EVP_RAND_CTX_new(rand, rdata->parent);
2326 EVP_RAND_free(rand);
2327 if (rdata->ctx == NULL)
2334 EVP_RAND_CTX_free(rdata->parent);
2335 OPENSSL_free(rdata);
2339 static void rand_test_cleanup(EVP_TEST *t)
2341 RAND_DATA *rdata = t->data;
2344 OPENSSL_free(rdata->cipher);
2345 OPENSSL_free(rdata->digest);
2347 for (i = 0; i <= rdata->n; i++) {
2348 OPENSSL_free(rdata->data[i].entropy);
2349 OPENSSL_free(rdata->data[i].reseed_entropy);
2350 OPENSSL_free(rdata->data[i].nonce);
2351 OPENSSL_free(rdata->data[i].pers);
2352 OPENSSL_free(rdata->data[i].reseed_addin);
2353 OPENSSL_free(rdata->data[i].addinA);
2354 OPENSSL_free(rdata->data[i].addinB);
2355 OPENSSL_free(rdata->data[i].pr_entropyA);
2356 OPENSSL_free(rdata->data[i].pr_entropyB);
2357 OPENSSL_free(rdata->data[i].output);
2359 EVP_RAND_CTX_free(rdata->ctx);
2360 EVP_RAND_CTX_free(rdata->parent);
2363 static int rand_test_parse(EVP_TEST *t,
2364 const char *keyword, const char *value)
2366 RAND_DATA *rdata = t->data;
2367 RAND_DATA_PASS *item;
2371 if ((p = strchr(keyword, '.')) != NULL) {
2373 if (n >= MAX_RAND_REPEATS)
2377 item = rdata->data + n;
2378 if (strncmp(keyword, "Entropy.", sizeof("Entropy")) == 0)
2379 return parse_bin(value, &item->entropy, &item->entropy_len);
2380 if (strncmp(keyword, "ReseedEntropy.", sizeof("ReseedEntropy")) == 0)
2381 return parse_bin(value, &item->reseed_entropy,
2382 &item->reseed_entropy_len);
2383 if (strncmp(keyword, "Nonce.", sizeof("Nonce")) == 0)
2384 return parse_bin(value, &item->nonce, &item->nonce_len);
2385 if (strncmp(keyword, "PersonalisationString.",
2386 sizeof("PersonalisationString")) == 0)
2387 return parse_bin(value, &item->pers, &item->pers_len);
2388 if (strncmp(keyword, "ReseedAdditionalInput.",
2389 sizeof("ReseedAdditionalInput")) == 0)
2390 return parse_bin(value, &item->reseed_addin,
2391 &item->reseed_addin_len);
2392 if (strncmp(keyword, "AdditionalInputA.",
2393 sizeof("AdditionalInputA")) == 0)
2394 return parse_bin(value, &item->addinA, &item->addinA_len);
2395 if (strncmp(keyword, "AdditionalInputB.",
2396 sizeof("AdditionalInputB")) == 0)
2397 return parse_bin(value, &item->addinB, &item->addinB_len);
2398 if (strncmp(keyword, "EntropyPredictionResistanceA.",
2399 sizeof("EntropyPredictionResistanceA")) == 0)
2400 return parse_bin(value, &item->pr_entropyA, &item->pr_entropyA_len);
2401 if (strncmp(keyword, "EntropyPredictionResistanceB.",
2402 sizeof("EntropyPredictionResistanceB")) == 0)
2403 return parse_bin(value, &item->pr_entropyB, &item->pr_entropyB_len);
2404 if (strncmp(keyword, "Output.", sizeof("Output")) == 0)
2405 return parse_bin(value, &item->output, &item->output_len);
2407 if (strcmp(keyword, "Cipher") == 0)
2408 return TEST_ptr(rdata->cipher = OPENSSL_strdup(value));
2409 if (strcmp(keyword, "Digest") == 0)
2410 return TEST_ptr(rdata->digest = OPENSSL_strdup(value));
2411 if (strcmp(keyword, "DerivationFunction") == 0) {
2412 rdata->use_df = atoi(value) != 0;
2415 if (strcmp(keyword, "GenerateBits") == 0) {
2416 if ((n = atoi(value)) <= 0 || n % 8 != 0)
2418 rdata->generate_bits = (unsigned int)n;
2421 if (strcmp(keyword, "PredictionResistance") == 0) {
2422 rdata->prediction_resistance = atoi(value) != 0;
2429 static int rand_test_run(EVP_TEST *t)
2431 RAND_DATA *expected = t->data;
2432 RAND_DATA_PASS *item;
2434 size_t got_len = expected->generate_bits / 8;
2435 OSSL_PARAM params[5], *p = params;
2436 int i = -1, ret = 0;
2437 unsigned int strength;
2440 if (!TEST_ptr(got = OPENSSL_malloc(got_len)))
2443 *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &expected->use_df);
2444 if (expected->cipher != NULL)
2445 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
2446 expected->cipher, 0);
2447 if (expected->digest != NULL)
2448 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
2449 expected->digest, 0);
2450 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC, "HMAC", 0);
2451 *p = OSSL_PARAM_construct_end();
2452 if (!TEST_true(EVP_RAND_CTX_set_params(expected->ctx, params)))
2455 strength = EVP_RAND_get_strength(expected->ctx);
2456 for (i = 0; i <= expected->n; i++) {
2457 item = expected->data + i;
2460 z = item->entropy != NULL ? item->entropy : (unsigned char *)"";
2461 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
2462 z, item->entropy_len);
2463 z = item->nonce != NULL ? item->nonce : (unsigned char *)"";
2464 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
2465 z, item->nonce_len);
2466 *p = OSSL_PARAM_construct_end();
2467 if (!TEST_true(EVP_RAND_instantiate(expected->parent, strength,
2468 0, NULL, 0, params)))
2471 z = item->pers != NULL ? item->pers : (unsigned char *)"";
2472 if (!TEST_true(EVP_RAND_instantiate
2473 (expected->ctx, strength,
2474 expected->prediction_resistance, z,
2475 item->pers_len, NULL)))
2478 if (item->reseed_entropy != NULL) {
2479 params[0] = OSSL_PARAM_construct_octet_string
2480 (OSSL_RAND_PARAM_TEST_ENTROPY, item->reseed_entropy,
2481 item->reseed_entropy_len);
2482 params[1] = OSSL_PARAM_construct_end();
2483 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2486 if (!TEST_true(EVP_RAND_reseed
2487 (expected->ctx, expected->prediction_resistance,
2488 NULL, 0, item->reseed_addin,
2489 item->reseed_addin_len)))
2492 if (item->pr_entropyA != NULL) {
2493 params[0] = OSSL_PARAM_construct_octet_string
2494 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyA,
2495 item->pr_entropyA_len);
2496 params[1] = OSSL_PARAM_construct_end();
2497 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2500 if (!TEST_true(EVP_RAND_generate
2501 (expected->ctx, got, got_len,
2502 strength, expected->prediction_resistance,
2503 item->addinA, item->addinA_len)))
2506 if (item->pr_entropyB != NULL) {
2507 params[0] = OSSL_PARAM_construct_octet_string
2508 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyB,
2509 item->pr_entropyB_len);
2510 params[1] = OSSL_PARAM_construct_end();
2511 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2514 if (!TEST_true(EVP_RAND_generate
2515 (expected->ctx, got, got_len,
2516 strength, expected->prediction_resistance,
2517 item->addinB, item->addinB_len)))
2519 if (!TEST_mem_eq(got, got_len, item->output, item->output_len))
2521 if (!TEST_true(EVP_RAND_uninstantiate(expected->ctx))
2522 || !TEST_true(EVP_RAND_uninstantiate(expected->parent))
2523 || !TEST_true(EVP_RAND_verify_zeroization(expected->ctx))
2524 || !TEST_int_eq(EVP_RAND_get_state(expected->ctx),
2525 EVP_RAND_STATE_UNINITIALISED))
2532 if (ret == 0 && i >= 0)
2533 TEST_info("Error in test case %d of %d\n", i, expected->n + 1);
2538 static const EVP_TEST_METHOD rand_test_method = {
2550 typedef struct kdf_data_st {
2551 /* Context for this operation */
2553 /* Expected output */
2554 unsigned char *output;
2556 OSSL_PARAM params[20];
2561 * Perform public key operation setup: lookup key, allocated ctx and call
2562 * the appropriate initialisation function
2564 static int kdf_test_init(EVP_TEST *t, const char *name)
2569 if (is_kdf_disabled(name)) {
2570 TEST_info("skipping, '%s' is disabled", name);
2575 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2577 kdata->p = kdata->params;
2578 *kdata->p = OSSL_PARAM_construct_end();
2580 kdf = EVP_KDF_fetch(libctx, name, NULL);
2582 OPENSSL_free(kdata);
2585 kdata->ctx = EVP_KDF_CTX_new(kdf);
2587 if (kdata->ctx == NULL) {
2588 OPENSSL_free(kdata);
2595 static void kdf_test_cleanup(EVP_TEST *t)
2597 KDF_DATA *kdata = t->data;
2600 for (p = kdata->params; p->key != NULL; p++)
2601 OPENSSL_free(p->data);
2602 OPENSSL_free(kdata->output);
2603 EVP_KDF_CTX_free(kdata->ctx);
2606 static int kdf_test_ctrl(EVP_TEST *t, EVP_KDF_CTX *kctx,
2609 KDF_DATA *kdata = t->data;
2612 const OSSL_PARAM *defs = EVP_KDF_settable_ctx_params(EVP_KDF_CTX_kdf(kctx));
2614 if (!TEST_ptr(name = OPENSSL_strdup(value)))
2616 p = strchr(name, ':');
2620 rv = OSSL_PARAM_allocate_from_text(kdata->p, defs, name, p,
2621 p != NULL ? strlen(p) : 0, NULL);
2622 *++kdata->p = OSSL_PARAM_construct_end();
2624 t->err = "KDF_PARAM_ERROR";
2628 if (p != NULL && strcmp(name, "digest") == 0) {
2629 if (is_digest_disabled(p)) {
2630 TEST_info("skipping, '%s' is disabled", p);
2635 && (strcmp(name, "cipher") == 0
2636 || strcmp(name, "cekalg") == 0)
2637 && is_cipher_disabled(p)) {
2638 TEST_info("skipping, '%s' is disabled", p);
2645 static int kdf_test_parse(EVP_TEST *t,
2646 const char *keyword, const char *value)
2648 KDF_DATA *kdata = t->data;
2650 if (strcmp(keyword, "Output") == 0)
2651 return parse_bin(value, &kdata->output, &kdata->output_len);
2652 if (strncmp(keyword, "Ctrl", 4) == 0)
2653 return kdf_test_ctrl(t, kdata->ctx, value);
2657 static int kdf_test_run(EVP_TEST *t)
2659 KDF_DATA *expected = t->data;
2660 unsigned char *got = NULL;
2661 size_t got_len = expected->output_len;
2663 if (!EVP_KDF_CTX_set_params(expected->ctx, expected->params)) {
2664 t->err = "KDF_CTRL_ERROR";
2667 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2668 t->err = "INTERNAL_ERROR";
2671 if (EVP_KDF_derive(expected->ctx, got, got_len, NULL) <= 0) {
2672 t->err = "KDF_DERIVE_ERROR";
2675 if (!memory_err_compare(t, "KDF_MISMATCH",
2676 expected->output, expected->output_len,
2687 static const EVP_TEST_METHOD kdf_test_method = {
2699 typedef struct pkey_kdf_data_st {
2700 /* Context for this operation */
2702 /* Expected output */
2703 unsigned char *output;
2708 * Perform public key operation setup: lookup key, allocated ctx and call
2709 * the appropriate initialisation function
2711 static int pkey_kdf_test_init(EVP_TEST *t, const char *name)
2713 PKEY_KDF_DATA *kdata = NULL;
2715 if (is_kdf_disabled(name)) {
2716 TEST_info("skipping, '%s' is disabled", name);
2721 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2724 kdata->ctx = EVP_PKEY_CTX_new_from_name(libctx, name, NULL);
2725 if (kdata->ctx == NULL
2726 || EVP_PKEY_derive_init(kdata->ctx) <= 0)
2732 EVP_PKEY_CTX_free(kdata->ctx);
2733 OPENSSL_free(kdata);
2737 static void pkey_kdf_test_cleanup(EVP_TEST *t)
2739 PKEY_KDF_DATA *kdata = t->data;
2741 OPENSSL_free(kdata->output);
2742 EVP_PKEY_CTX_free(kdata->ctx);
2745 static int pkey_kdf_test_parse(EVP_TEST *t,
2746 const char *keyword, const char *value)
2748 PKEY_KDF_DATA *kdata = t->data;
2750 if (strcmp(keyword, "Output") == 0)
2751 return parse_bin(value, &kdata->output, &kdata->output_len);
2752 if (strncmp(keyword, "Ctrl", 4) == 0)
2753 return pkey_test_ctrl(t, kdata->ctx, value);
2757 static int pkey_kdf_test_run(EVP_TEST *t)
2759 PKEY_KDF_DATA *expected = t->data;
2760 unsigned char *got = NULL;
2761 size_t got_len = expected->output_len;
2763 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
2764 t->err = "INTERNAL_ERROR";
2767 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
2768 t->err = "KDF_DERIVE_ERROR";
2771 if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
2772 t->err = "KDF_MISMATCH";
2782 static const EVP_TEST_METHOD pkey_kdf_test_method = {
2785 pkey_kdf_test_cleanup,
2786 pkey_kdf_test_parse,
2794 typedef struct keypair_test_data_st {
2797 } KEYPAIR_TEST_DATA;
2799 static int keypair_test_init(EVP_TEST *t, const char *pair)
2801 KEYPAIR_TEST_DATA *data;
2803 EVP_PKEY *pk = NULL, *pubk = NULL;
2804 char *pub, *priv = NULL;
2806 /* Split private and public names. */
2807 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
2808 || !TEST_ptr(pub = strchr(priv, ':'))) {
2809 t->err = "PARSING_ERROR";
2814 if (!TEST_true(find_key(&pk, priv, private_keys))) {
2815 TEST_info("Can't find private key: %s", priv);
2816 t->err = "MISSING_PRIVATE_KEY";
2819 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
2820 TEST_info("Can't find public key: %s", pub);
2821 t->err = "MISSING_PUBLIC_KEY";
2825 if (pk == NULL && pubk == NULL) {
2826 /* Both keys are listed but unsupported: skip this test */
2832 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2845 static void keypair_test_cleanup(EVP_TEST *t)
2847 OPENSSL_free(t->data);
2852 * For tests that do not accept any custom keywords.
2854 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
2859 static int keypair_test_run(EVP_TEST *t)
2862 const KEYPAIR_TEST_DATA *pair = t->data;
2864 if (pair->privk == NULL || pair->pubk == NULL) {
2866 * this can only happen if only one of the keys is not set
2867 * which means that one of them was unsupported while the
2868 * other isn't: hence a key type mismatch.
2870 t->err = "KEYPAIR_TYPE_MISMATCH";
2875 if ((rv = EVP_PKEY_eq(pair->privk, pair->pubk)) != 1 ) {
2877 t->err = "KEYPAIR_MISMATCH";
2878 } else if ( -1 == rv ) {
2879 t->err = "KEYPAIR_TYPE_MISMATCH";
2880 } else if ( -2 == rv ) {
2881 t->err = "UNSUPPORTED_KEY_COMPARISON";
2883 TEST_error("Unexpected error in key comparison");
2898 static const EVP_TEST_METHOD keypair_test_method = {
2901 keypair_test_cleanup,
2910 typedef struct keygen_test_data_st {
2911 EVP_PKEY_CTX *genctx; /* Keygen context to use */
2912 char *keyname; /* Key name to store key or NULL */
2915 static int keygen_test_init(EVP_TEST *t, const char *alg)
2917 KEYGEN_TEST_DATA *data;
2918 EVP_PKEY_CTX *genctx;
2919 int nid = OBJ_sn2nid(alg);
2921 if (nid == NID_undef) {
2922 nid = OBJ_ln2nid(alg);
2923 if (nid == NID_undef)
2927 if (is_pkey_disabled(alg)) {
2931 if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_from_name(libctx, alg, NULL)))
2934 if (EVP_PKEY_keygen_init(genctx) <= 0) {
2935 t->err = "KEYGEN_INIT_ERROR";
2939 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
2941 data->genctx = genctx;
2942 data->keyname = NULL;
2948 EVP_PKEY_CTX_free(genctx);
2952 static void keygen_test_cleanup(EVP_TEST *t)
2954 KEYGEN_TEST_DATA *keygen = t->data;
2956 EVP_PKEY_CTX_free(keygen->genctx);
2957 OPENSSL_free(keygen->keyname);
2958 OPENSSL_free(t->data);
2962 static int keygen_test_parse(EVP_TEST *t,
2963 const char *keyword, const char *value)
2965 KEYGEN_TEST_DATA *keygen = t->data;
2967 if (strcmp(keyword, "KeyName") == 0)
2968 return TEST_ptr(keygen->keyname = OPENSSL_strdup(value));
2969 if (strcmp(keyword, "Ctrl") == 0)
2970 return pkey_test_ctrl(t, keygen->genctx, value);
2974 static int keygen_test_run(EVP_TEST *t)
2976 KEYGEN_TEST_DATA *keygen = t->data;
2977 EVP_PKEY *pkey = NULL;
2980 if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) {
2981 t->err = "KEYGEN_GENERATE_ERROR";
2985 if (!evp_pkey_is_provided(pkey)) {
2986 TEST_info("Warning: legacy key generated %s", keygen->keyname);
2989 if (keygen->keyname != NULL) {
2993 if (find_key(NULL, keygen->keyname, private_keys)) {
2994 TEST_info("Duplicate key %s", keygen->keyname);
2998 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
3000 key->name = keygen->keyname;
3001 keygen->keyname = NULL;
3003 key->next = private_keys;
3007 EVP_PKEY_free(pkey);
3016 static const EVP_TEST_METHOD keygen_test_method = {
3019 keygen_test_cleanup,
3025 ** DIGEST SIGN+VERIFY TESTS
3029 int is_verify; /* Set to 1 if verifying */
3030 int is_oneshot; /* Set to 1 for one shot operation */
3031 const EVP_MD *md; /* Digest to use */
3032 EVP_MD_CTX *ctx; /* Digest context */
3034 STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
3035 unsigned char *osin; /* Input data if one shot */
3036 size_t osin_len; /* Input length data if one shot */
3037 unsigned char *output; /* Expected output */
3038 size_t output_len; /* Expected output length */
3041 static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
3044 const EVP_MD *md = NULL;
3045 DIGESTSIGN_DATA *mdat;
3047 if (strcmp(alg, "NULL") != 0) {
3048 if (is_digest_disabled(alg)) {
3052 md = EVP_get_digestbyname(alg);
3056 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
3059 if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
3063 mdat->is_verify = is_verify;
3064 mdat->is_oneshot = is_oneshot;
3069 static int digestsign_test_init(EVP_TEST *t, const char *alg)
3071 return digestsigver_test_init(t, alg, 0, 0);
3074 static void digestsigver_test_cleanup(EVP_TEST *t)
3076 DIGESTSIGN_DATA *mdata = t->data;
3078 EVP_MD_CTX_free(mdata->ctx);
3079 sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
3080 OPENSSL_free(mdata->osin);
3081 OPENSSL_free(mdata->output);
3082 OPENSSL_free(mdata);
3086 static int digestsigver_test_parse(EVP_TEST *t,
3087 const char *keyword, const char *value)
3089 DIGESTSIGN_DATA *mdata = t->data;
3091 if (strcmp(keyword, "Key") == 0) {
3092 EVP_PKEY *pkey = NULL;
3094 const char *name = mdata->md == NULL ? NULL : EVP_MD_get0_name(mdata->md);
3096 if (mdata->is_verify)
3097 rv = find_key(&pkey, value, public_keys);
3099 rv = find_key(&pkey, value, private_keys);
3100 if (rv == 0 || pkey == NULL) {
3104 if (mdata->is_verify) {
3105 if (!EVP_DigestVerifyInit_ex(mdata->ctx, &mdata->pctx, name, libctx,
3107 t->err = "DIGESTVERIFYINIT_ERROR";
3110 if (!EVP_DigestSignInit_ex(mdata->ctx, &mdata->pctx, name, libctx, NULL,
3112 t->err = "DIGESTSIGNINIT_ERROR";
3116 if (strcmp(keyword, "Input") == 0) {
3117 if (mdata->is_oneshot)
3118 return parse_bin(value, &mdata->osin, &mdata->osin_len);
3119 return evp_test_buffer_append(value, &mdata->input);
3121 if (strcmp(keyword, "Output") == 0)
3122 return parse_bin(value, &mdata->output, &mdata->output_len);
3124 if (!mdata->is_oneshot) {
3125 if (strcmp(keyword, "Count") == 0)
3126 return evp_test_buffer_set_count(value, mdata->input);
3127 if (strcmp(keyword, "Ncopy") == 0)
3128 return evp_test_buffer_ncopy(value, mdata->input);
3130 if (strcmp(keyword, "Ctrl") == 0) {
3131 if (mdata->pctx == NULL)
3133 return pkey_test_ctrl(t, mdata->pctx, value);
3138 static int digestsign_update_fn(void *ctx, const unsigned char *buf,
3141 return EVP_DigestSignUpdate(ctx, buf, buflen);
3144 static int digestsign_test_run(EVP_TEST *t)
3146 DIGESTSIGN_DATA *expected = t->data;
3147 unsigned char *got = NULL;
3150 if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
3152 t->err = "DIGESTUPDATE_ERROR";
3156 if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
3157 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
3160 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
3161 t->err = "MALLOC_FAILURE";
3164 if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
3165 t->err = "DIGESTSIGNFINAL_ERROR";
3168 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
3169 expected->output, expected->output_len,
3179 static const EVP_TEST_METHOD digestsign_test_method = {
3181 digestsign_test_init,
3182 digestsigver_test_cleanup,
3183 digestsigver_test_parse,
3187 static int digestverify_test_init(EVP_TEST *t, const char *alg)
3189 return digestsigver_test_init(t, alg, 1, 0);
3192 static int digestverify_update_fn(void *ctx, const unsigned char *buf,
3195 return EVP_DigestVerifyUpdate(ctx, buf, buflen);
3198 static int digestverify_test_run(EVP_TEST *t)
3200 DIGESTSIGN_DATA *mdata = t->data;
3202 if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
3203 t->err = "DIGESTUPDATE_ERROR";
3207 if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
3208 mdata->output_len) <= 0)
3209 t->err = "VERIFY_ERROR";
3213 static const EVP_TEST_METHOD digestverify_test_method = {
3215 digestverify_test_init,
3216 digestsigver_test_cleanup,
3217 digestsigver_test_parse,
3218 digestverify_test_run
3221 static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
3223 return digestsigver_test_init(t, alg, 0, 1);
3226 static int oneshot_digestsign_test_run(EVP_TEST *t)
3228 DIGESTSIGN_DATA *expected = t->data;
3229 unsigned char *got = NULL;
3232 if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
3233 expected->osin, expected->osin_len)) {
3234 t->err = "DIGESTSIGN_LENGTH_ERROR";
3237 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
3238 t->err = "MALLOC_FAILURE";
3241 if (!EVP_DigestSign(expected->ctx, got, &got_len,
3242 expected->osin, expected->osin_len)) {
3243 t->err = "DIGESTSIGN_ERROR";
3246 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
3247 expected->output, expected->output_len,
3257 static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
3258 "OneShotDigestSign",
3259 oneshot_digestsign_test_init,
3260 digestsigver_test_cleanup,
3261 digestsigver_test_parse,
3262 oneshot_digestsign_test_run
3265 static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
3267 return digestsigver_test_init(t, alg, 1, 1);
3270 static int oneshot_digestverify_test_run(EVP_TEST *t)
3272 DIGESTSIGN_DATA *mdata = t->data;
3274 if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
3275 mdata->osin, mdata->osin_len) <= 0)
3276 t->err = "VERIFY_ERROR";
3280 static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
3281 "OneShotDigestVerify",
3282 oneshot_digestverify_test_init,
3283 digestsigver_test_cleanup,
3284 digestsigver_test_parse,
3285 oneshot_digestverify_test_run
3290 ** PARSING AND DISPATCH
3293 static const EVP_TEST_METHOD *evp_test_list[] = {
3295 &cipher_test_method,
3296 &digest_test_method,
3297 &digestsign_test_method,
3298 &digestverify_test_method,
3299 &encode_test_method,
3301 &pkey_kdf_test_method,
3302 &keypair_test_method,
3303 &keygen_test_method,
3305 &oneshot_digestsign_test_method,
3306 &oneshot_digestverify_test_method,
3308 &pdecrypt_test_method,
3309 &pderive_test_method,
3311 &pverify_recover_test_method,
3312 &pverify_test_method,
3316 static const EVP_TEST_METHOD *find_test(const char *name)
3318 const EVP_TEST_METHOD **tt;
3320 for (tt = evp_test_list; *tt; tt++) {
3321 if (strcmp(name, (*tt)->name) == 0)
3327 static void clear_test(EVP_TEST *t)
3329 test_clearstanza(&t->s);
3331 if (t->data != NULL) {
3332 if (t->meth != NULL)
3333 t->meth->cleanup(t);
3334 OPENSSL_free(t->data);
3337 OPENSSL_free(t->expected_err);
3338 t->expected_err = NULL;
3339 OPENSSL_free(t->reason);
3348 /* Check for errors in the test structure; return 1 if okay, else 0. */
3349 static int check_test_error(EVP_TEST *t)
3354 if (t->err == NULL && t->expected_err == NULL)
3356 if (t->err != NULL && t->expected_err == NULL) {
3357 if (t->aux_err != NULL) {
3358 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
3359 t->s.test_file, t->s.start, t->aux_err, t->err);
3361 TEST_info("%s:%d: Source of above error; unexpected error %s",
3362 t->s.test_file, t->s.start, t->err);
3366 if (t->err == NULL && t->expected_err != NULL) {
3367 TEST_info("%s:%d: Succeeded but was expecting %s",
3368 t->s.test_file, t->s.start, t->expected_err);
3372 if (strcmp(t->err, t->expected_err) != 0) {
3373 TEST_info("%s:%d: Expected %s got %s",
3374 t->s.test_file, t->s.start, t->expected_err, t->err);
3378 if (t->reason == NULL)
3381 if (t->reason == NULL) {
3382 TEST_info("%s:%d: Test is missing function or reason code",
3383 t->s.test_file, t->s.start);
3387 err = ERR_peek_error();
3389 TEST_info("%s:%d: Expected error \"%s\" not set",
3390 t->s.test_file, t->s.start, t->reason);
3394 reason = ERR_reason_error_string(err);
3395 if (reason == NULL) {
3396 TEST_info("%s:%d: Expected error \"%s\", no strings available."
3398 t->s.test_file, t->s.start, t->reason);
3402 if (strcmp(reason, t->reason) == 0)
3405 TEST_info("%s:%d: Expected error \"%s\", got \"%s\"",
3406 t->s.test_file, t->s.start, t->reason, reason);
3411 /* Run a parsed test. Log a message and return 0 on error. */
3412 static int run_test(EVP_TEST *t)
3414 if (t->meth == NULL)
3421 if (t->err == NULL && t->meth->run_test(t) != 1) {
3422 TEST_info("%s:%d %s error",
3423 t->s.test_file, t->s.start, t->meth->name);
3426 if (!check_test_error(t)) {
3427 TEST_openssl_errors();
3436 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
3438 for (; lst != NULL; lst = lst->next) {
3439 if (strcmp(lst->name, name) == 0) {
3448 static void free_key_list(KEY_LIST *lst)
3450 while (lst != NULL) {
3451 KEY_LIST *next = lst->next;
3453 EVP_PKEY_free(lst->key);
3454 OPENSSL_free(lst->name);
3461 * Is the key type an unsupported algorithm?
3463 static int key_unsupported(void)
3465 long err = ERR_peek_last_error();
3466 int lib = ERR_GET_LIB(err);
3467 long reason = ERR_GET_REASON(err);
3469 if ((lib == ERR_LIB_EVP && reason == EVP_R_UNSUPPORTED_ALGORITHM)
3470 || (lib == ERR_LIB_EVP && reason == EVP_R_DECODE_ERROR)
3471 || reason == ERR_R_UNSUPPORTED) {
3475 #ifndef OPENSSL_NO_EC
3477 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
3478 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
3481 if (lib == ERR_LIB_EC
3482 && (reason == EC_R_UNKNOWN_GROUP
3483 || reason == EC_R_INVALID_CURVE)) {
3487 #endif /* OPENSSL_NO_EC */
3491 /* NULL out the value from |pp| but return it. This "steals" a pointer. */
3492 static char *take_value(PAIR *pp)
3494 char *p = pp->value;
3500 #if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
3501 static int securitycheck_enabled(void)
3503 static int enabled = -1;
3505 if (enabled == -1) {
3506 if (OSSL_PROVIDER_available(libctx, "fips")) {
3507 OSSL_PARAM params[2];
3508 OSSL_PROVIDER *prov = NULL;
3511 prov = OSSL_PROVIDER_load(libctx, "fips");
3514 OSSL_PARAM_construct_int(OSSL_PROV_PARAM_SECURITY_CHECKS,
3516 params[1] = OSSL_PARAM_construct_end();
3517 OSSL_PROVIDER_get_params(prov, params);
3518 OSSL_PROVIDER_unload(prov);
3530 * Return 1 if one of the providers named in the string is available.
3531 * The provider names are separated with whitespace.
3532 * NOTE: destructive function, it inserts '\0' after each provider name.
3534 static int prov_available(char *providers)
3540 for (; isspace(*providers); providers++)
3542 if (*providers == '\0')
3543 break; /* End of the road */
3544 for (p = providers; *p != '\0' && !isspace(*p); p++)
3550 if (OSSL_PROVIDER_available(libctx, providers))
3551 return 1; /* Found one */
3556 /* Read and parse one test. Return 0 if failure, 1 if okay. */
3557 static int parse(EVP_TEST *t)
3559 KEY_LIST *key, **klist;
3562 int i, skip_availablein = 0;
3566 if (BIO_eof(t->s.fp))
3569 if (!test_readstanza(&t->s))
3571 } while (t->s.numpairs == 0);
3572 pp = &t->s.pairs[0];
3574 /* Are we adding a key? */
3578 if (strcmp(pp->key, "PrivateKey") == 0) {
3579 pkey = PEM_read_bio_PrivateKey_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3580 if (pkey == NULL && !key_unsupported()) {
3581 EVP_PKEY_free(pkey);
3582 TEST_info("Can't read private key %s", pp->value);
3583 TEST_openssl_errors();
3586 klist = &private_keys;
3587 } else if (strcmp(pp->key, "PublicKey") == 0) {
3588 pkey = PEM_read_bio_PUBKEY_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3589 if (pkey == NULL && !key_unsupported()) {
3590 EVP_PKEY_free(pkey);
3591 TEST_info("Can't read public key %s", pp->value);
3592 TEST_openssl_errors();
3595 klist = &public_keys;
3596 } else if (strcmp(pp->key, "PrivateKeyRaw") == 0
3597 || strcmp(pp->key, "PublicKeyRaw") == 0 ) {
3598 char *strnid = NULL, *keydata = NULL;
3599 unsigned char *keybin;
3603 if (strcmp(pp->key, "PrivateKeyRaw") == 0)
3604 klist = &private_keys;
3606 klist = &public_keys;
3608 strnid = strchr(pp->value, ':');
3609 if (strnid != NULL) {
3611 keydata = strchr(strnid, ':');
3612 if (keydata != NULL)
3615 if (keydata == NULL) {
3616 TEST_info("Failed to parse %s value", pp->key);
3620 nid = OBJ_txt2nid(strnid);
3621 if (nid == NID_undef) {
3622 TEST_info("Unrecognised algorithm NID");
3625 if (!parse_bin(keydata, &keybin, &keylen)) {
3626 TEST_info("Failed to create binary key");
3629 if (klist == &private_keys)
3630 pkey = EVP_PKEY_new_raw_private_key_ex(libctx, strnid, NULL, keybin,
3633 pkey = EVP_PKEY_new_raw_public_key_ex(libctx, strnid, NULL, keybin,
3635 if (pkey == NULL && !key_unsupported()) {
3636 TEST_info("Can't read %s data", pp->key);
3637 OPENSSL_free(keybin);
3638 TEST_openssl_errors();
3641 OPENSSL_free(keybin);
3642 } else if (strcmp(pp->key, "Availablein") == 0) {
3643 if (!prov_available(pp->value)) {
3644 TEST_info("skipping, '%s' provider not available: %s:%d",
3645 pp->value, t->s.test_file, t->s.start);
3654 /* If we have a key add to list */
3655 if (klist != NULL) {
3656 if (find_key(NULL, pp->value, *klist)) {
3657 TEST_info("Duplicate key %s", pp->value);
3660 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
3662 key->name = take_value(pp);
3667 /* Go back and start a new stanza. */
3668 if ((t->s.numpairs - skip_availablein) != 1)
3669 TEST_info("Line %d: missing blank line\n", t->s.curr);
3673 /* Find the test, based on first keyword. */
3674 if (!TEST_ptr(t->meth = find_test(pp->key)))
3676 if (!t->meth->init(t, pp->value)) {
3677 TEST_error("unknown %s: %s\n", pp->key, pp->value);
3681 /* TEST_info("skipping %s %s", pp->key, pp->value); */
3685 for (pp++, i = 1; i < (t->s.numpairs - skip_availablein); pp++, i++) {
3686 if (strcmp(pp->key, "Securitycheck") == 0) {
3687 #if defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
3689 if (!securitycheck_enabled())
3692 TEST_info("skipping, Securitycheck is disabled: %s:%d",
3693 t->s.test_file, t->s.start);
3697 } else if (strcmp(pp->key, "Availablein") == 0) {
3698 TEST_info("Line %d: 'Availablein' should be the first option",
3701 } else if (strcmp(pp->key, "Result") == 0) {
3702 if (t->expected_err != NULL) {
3703 TEST_info("Line %d: multiple result lines", t->s.curr);
3706 t->expected_err = take_value(pp);
3707 } else if (strcmp(pp->key, "Function") == 0) {
3708 /* Ignore old line. */
3709 } else if (strcmp(pp->key, "Reason") == 0) {
3710 if (t->reason != NULL) {
3711 TEST_info("Line %d: multiple reason lines", t->s.curr);
3714 t->reason = take_value(pp);
3716 /* Must be test specific line: try to parse it */
3717 int rv = t->meth->parse(t, pp->key, pp->value);
3720 TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key);
3724 TEST_info("Line %d: error processing keyword %s = %s\n",
3725 t->s.curr, pp->key, pp->value);
3734 static int run_file_tests(int i)
3737 const char *testfile = test_get_argument(i);
3740 if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t))))
3742 if (!test_start_file(&t->s, testfile)) {
3747 while (!BIO_eof(t->s.fp)) {
3753 if (c == 0 || !run_test(t)) {
3758 test_end_file(&t->s);
3761 free_key_list(public_keys);
3762 free_key_list(private_keys);
3769 const OPTIONS *test_get_options(void)
3771 static const OPTIONS test_options[] = {
3772 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
3773 { "config", OPT_CONFIG_FILE, '<',
3774 "The configuration file to use for the libctx" },
3775 { OPT_HELP_STR, 1, '-', "file\tFile to run tests on.\n" },
3778 return test_options;
3781 int setup_tests(void)
3784 char *config_file = NULL;
3788 while ((o = opt_next()) != OPT_EOF) {
3790 case OPT_CONFIG_FILE:
3791 config_file = opt_arg();
3793 case OPT_TEST_CASES:
3802 * Load the provider via configuration into the created library context.
3803 * Load the 'null' provider into the default library context to ensure that
3804 * the tests do not fallback to using the default provider.
3806 if (!test_get_libctx(&libctx, &prov_null, config_file, NULL, NULL))
3809 n = test_get_argument_count();
3813 ADD_ALL_TESTS(run_file_tests, n);
3817 void cleanup_tests(void)
3819 OSSL_PROVIDER_unload(prov_null);
3820 OSSL_LIB_CTX_free(libctx);
3823 #define STR_STARTS_WITH(str, pre) strncasecmp(pre, str, strlen(pre)) == 0
3824 #define STR_ENDS_WITH(str, pre) \
3825 strlen(str) < strlen(pre) ? 0 : (strcasecmp(pre, str + strlen(str) - strlen(pre)) == 0)
3827 static int is_digest_disabled(const char *name)
3829 #ifdef OPENSSL_NO_BLAKE2
3830 if (STR_STARTS_WITH(name, "BLAKE"))
3833 #ifdef OPENSSL_NO_MD2
3834 if (strcasecmp(name, "MD2") == 0)
3837 #ifdef OPENSSL_NO_MDC2
3838 if (strcasecmp(name, "MDC2") == 0)
3841 #ifdef OPENSSL_NO_MD4
3842 if (strcasecmp(name, "MD4") == 0)
3845 #ifdef OPENSSL_NO_MD5
3846 if (strcasecmp(name, "MD5") == 0)
3849 #ifdef OPENSSL_NO_RMD160
3850 if (strcasecmp(name, "RIPEMD160") == 0)
3853 #ifdef OPENSSL_NO_SM3
3854 if (strcasecmp(name, "SM3") == 0)
3857 #ifdef OPENSSL_NO_WHIRLPOOL
3858 if (strcasecmp(name, "WHIRLPOOL") == 0)
3864 static int is_pkey_disabled(const char *name)
3866 #ifdef OPENSSL_NO_EC
3867 if (STR_STARTS_WITH(name, "EC"))
3870 #ifdef OPENSSL_NO_DH
3871 if (STR_STARTS_WITH(name, "DH"))
3874 #ifdef OPENSSL_NO_DSA
3875 if (STR_STARTS_WITH(name, "DSA"))
3881 static int is_mac_disabled(const char *name)
3883 #ifdef OPENSSL_NO_BLAKE2
3884 if (STR_STARTS_WITH(name, "BLAKE2BMAC")
3885 || STR_STARTS_WITH(name, "BLAKE2SMAC"))
3888 #ifdef OPENSSL_NO_CMAC
3889 if (STR_STARTS_WITH(name, "CMAC"))
3892 #ifdef OPENSSL_NO_POLY1305
3893 if (STR_STARTS_WITH(name, "Poly1305"))
3896 #ifdef OPENSSL_NO_SIPHASH
3897 if (STR_STARTS_WITH(name, "SipHash"))
3902 static int is_kdf_disabled(const char *name)
3904 #ifdef OPENSSL_NO_SCRYPT
3905 if (STR_ENDS_WITH(name, "SCRYPT"))
3911 static int is_cipher_disabled(const char *name)
3913 #ifdef OPENSSL_NO_ARIA
3914 if (STR_STARTS_WITH(name, "ARIA"))
3917 #ifdef OPENSSL_NO_BF
3918 if (STR_STARTS_WITH(name, "BF"))
3921 #ifdef OPENSSL_NO_CAMELLIA
3922 if (STR_STARTS_WITH(name, "CAMELLIA"))
3925 #ifdef OPENSSL_NO_CAST
3926 if (STR_STARTS_WITH(name, "CAST"))
3929 #ifdef OPENSSL_NO_CHACHA
3930 if (STR_STARTS_WITH(name, "CHACHA"))
3933 #ifdef OPENSSL_NO_POLY1305
3934 if (STR_ENDS_WITH(name, "Poly1305"))
3937 #ifdef OPENSSL_NO_DES
3938 if (STR_STARTS_WITH(name, "DES"))
3940 if (STR_ENDS_WITH(name, "3DESwrap"))
3943 #ifdef OPENSSL_NO_OCB
3944 if (STR_ENDS_WITH(name, "OCB"))
3947 #ifdef OPENSSL_NO_IDEA
3948 if (STR_STARTS_WITH(name, "IDEA"))
3951 #ifdef OPENSSL_NO_RC2
3952 if (STR_STARTS_WITH(name, "RC2"))
3955 #ifdef OPENSSL_NO_RC4
3956 if (STR_STARTS_WITH(name, "RC4"))
3959 #ifdef OPENSSL_NO_RC5
3960 if (STR_STARTS_WITH(name, "RC5"))
3963 #ifdef OPENSSL_NO_SEED
3964 if (STR_STARTS_WITH(name, "SEED"))
3967 #ifdef OPENSSL_NO_SIV
3968 if (STR_ENDS_WITH(name, "SIV"))
3971 #ifdef OPENSSL_NO_SM4
3972 if (STR_STARTS_WITH(name, "SM4"))