2 * Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #define OPENSSL_SUPPRESS_DEPRECATED /* EVP_PKEY_new_CMAC_key */
15 #include <openssl/evp.h>
16 #include <openssl/pem.h>
17 #include <openssl/err.h>
18 #include <openssl/provider.h>
19 #include <openssl/x509v3.h>
20 #include <openssl/pkcs12.h>
21 #include <openssl/kdf.h>
22 #include <openssl/params.h>
23 #include <openssl/core_names.h>
24 #include <openssl/fips_names.h>
25 #include "internal/numbers.h"
26 #include "internal/nelem.h"
27 #include "crypto/evp.h"
30 typedef struct evp_test_buffer_st EVP_TEST_BUFFER;
31 DEFINE_STACK_OF(EVP_TEST_BUFFER)
35 typedef struct evp_test_method_st EVP_TEST_METHOD;
37 /* Structure holding test information */
38 typedef struct evp_test_st {
39 STANZA s; /* Common test stanza */
41 int skip; /* Current test should be skipped */
42 const EVP_TEST_METHOD *meth; /* method for this test */
43 const char *err, *aux_err; /* Error string for test */
44 char *expected_err; /* Expected error value of test */
45 char *reason; /* Expected error reason string */
46 void *data; /* test specific data */
49 /* Test method structure */
50 struct evp_test_method_st {
51 /* Name of test as it appears in file */
53 /* Initialise test for "alg" */
54 int (*init) (EVP_TEST * t, const char *alg);
56 void (*cleanup) (EVP_TEST * t);
57 /* Test specific name value pair processing */
58 int (*parse) (EVP_TEST * t, const char *name, const char *value);
59 /* Run the test itself */
60 int (*run_test) (EVP_TEST * t);
63 /* Linked list of named keys. */
64 typedef struct key_list_st {
67 struct key_list_st *next;
70 typedef enum OPTION_choice {
77 static OSSL_PROVIDER *prov_null = NULL;
78 static OSSL_LIB_CTX *libctx = NULL;
80 /* List of public and private keys */
81 static KEY_LIST *private_keys;
82 static KEY_LIST *public_keys;
84 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst);
85 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen);
86 static int is_digest_disabled(const char *name);
87 static int is_pkey_disabled(const char *name);
88 static int is_mac_disabled(const char *name);
89 static int is_cipher_disabled(const char *name);
90 static int is_kdf_disabled(const char *name);
93 * Compare two memory regions for equality, returning zero if they differ.
94 * However, if there is expected to be an error and the actual error
95 * matches then the memory is expected to be different so handle this
96 * case without producing unnecessary test framework output.
98 static int memory_err_compare(EVP_TEST *t, const char *err,
99 const void *expected, size_t expected_len,
100 const void *got, size_t got_len)
104 if (t->expected_err != NULL && strcmp(t->expected_err, err) == 0)
105 r = !TEST_mem_ne(expected, expected_len, got, got_len);
107 r = TEST_mem_eq(expected, expected_len, got, got_len);
114 * Structure used to hold a list of blocks of memory to test
115 * calls to "update" like functions.
117 struct evp_test_buffer_st {
124 static void evp_test_buffer_free(EVP_TEST_BUFFER *db)
127 OPENSSL_free(db->buf);
132 /* append buffer to a list */
133 static int evp_test_buffer_append(const char *value,
134 STACK_OF(EVP_TEST_BUFFER) **sk)
136 EVP_TEST_BUFFER *db = NULL;
138 if (!TEST_ptr(db = OPENSSL_malloc(sizeof(*db))))
141 if (!parse_bin(value, &db->buf, &db->buflen))
146 if (*sk == NULL && !TEST_ptr(*sk = sk_EVP_TEST_BUFFER_new_null()))
148 if (!sk_EVP_TEST_BUFFER_push(*sk, db))
154 evp_test_buffer_free(db);
158 /* replace last buffer in list with copies of itself */
159 static int evp_test_buffer_ncopy(const char *value,
160 STACK_OF(EVP_TEST_BUFFER) *sk)
163 unsigned char *tbuf, *p;
165 int ncopy = atoi(value);
170 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
172 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
174 tbuflen = db->buflen * ncopy;
175 if (!TEST_ptr(tbuf = OPENSSL_malloc(tbuflen)))
177 for (i = 0, p = tbuf; i < ncopy; i++, p += db->buflen)
178 memcpy(p, db->buf, db->buflen);
180 OPENSSL_free(db->buf);
182 db->buflen = tbuflen;
186 /* set repeat count for last buffer in list */
187 static int evp_test_buffer_set_count(const char *value,
188 STACK_OF(EVP_TEST_BUFFER) *sk)
191 int count = atoi(value);
196 if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
199 db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
200 if (db->count_set != 0)
203 db->count = (size_t)count;
208 /* call "fn" with each element of the list in turn */
209 static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER) *sk,
211 const unsigned char *buf,
217 for (i = 0; i < sk_EVP_TEST_BUFFER_num(sk); i++) {
218 EVP_TEST_BUFFER *tb = sk_EVP_TEST_BUFFER_value(sk, i);
221 for (j = 0; j < tb->count; j++) {
222 if (fn(ctx, tb->buf, tb->buflen) <= 0)
230 * Unescape some sequences in string literals (only \n for now).
231 * Return an allocated buffer, set |out_len|. If |input_len|
232 * is zero, get an empty buffer but set length to zero.
234 static unsigned char* unescape(const char *input, size_t input_len,
237 unsigned char *ret, *p;
240 if (input_len == 0) {
242 return OPENSSL_zalloc(1);
245 /* Escaping is non-expanding; over-allocate original size for simplicity. */
246 if (!TEST_ptr(ret = p = OPENSSL_malloc(input_len)))
249 for (i = 0; i < input_len; i++) {
250 if (*input == '\\') {
251 if (i == input_len - 1 || *++input != 'n') {
252 TEST_error("Bad escape sequence in file");
272 * For a hex string "value" convert to a binary allocated buffer.
273 * Return 1 on success or 0 on failure.
275 static int parse_bin(const char *value, unsigned char **buf, size_t *buflen)
279 /* Check for NULL literal */
280 if (strcmp(value, "NULL") == 0) {
286 /* Check for empty value */
287 if (*value == '\0') {
289 * Don't return NULL for zero length buffer. This is needed for
290 * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key
291 * buffer even if the key length is 0, in order to detect key reset.
293 *buf = OPENSSL_malloc(1);
301 /* Check for string literal */
302 if (value[0] == '"') {
303 size_t vlen = strlen(++value);
305 if (vlen == 0 || value[vlen - 1] != '"')
308 *buf = unescape(value, vlen, buflen);
309 return *buf == NULL ? 0 : 1;
312 /* Otherwise assume as hex literal and convert it to binary buffer */
313 if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) {
314 TEST_info("Can't convert %s", value);
315 TEST_openssl_errors();
318 /* Size of input buffer means we'll never overflow */
324 ** MESSAGE DIGEST TESTS
327 typedef struct digest_data_st {
328 /* Digest this test is for */
329 const EVP_MD *digest;
330 EVP_MD *fetched_digest;
331 /* Input to digest */
332 STACK_OF(EVP_TEST_BUFFER) *input;
333 /* Expected output */
334 unsigned char *output;
340 static int digest_test_init(EVP_TEST *t, const char *alg)
343 const EVP_MD *digest;
344 EVP_MD *fetched_digest;
346 if (is_digest_disabled(alg)) {
347 TEST_info("skipping, '%s' is disabled", alg);
352 if ((digest = fetched_digest = EVP_MD_fetch(libctx, alg, NULL)) == NULL
353 && (digest = EVP_get_digestbyname(alg)) == NULL)
355 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
358 mdat->digest = digest;
359 mdat->fetched_digest = fetched_digest;
361 if (fetched_digest != NULL)
362 TEST_info("%s is fetched", alg);
366 static void digest_test_cleanup(EVP_TEST *t)
368 DIGEST_DATA *mdat = t->data;
370 sk_EVP_TEST_BUFFER_pop_free(mdat->input, evp_test_buffer_free);
371 OPENSSL_free(mdat->output);
372 EVP_MD_free(mdat->fetched_digest);
375 static int digest_test_parse(EVP_TEST *t,
376 const char *keyword, const char *value)
378 DIGEST_DATA *mdata = t->data;
380 if (strcmp(keyword, "Input") == 0)
381 return evp_test_buffer_append(value, &mdata->input);
382 if (strcmp(keyword, "Output") == 0)
383 return parse_bin(value, &mdata->output, &mdata->output_len);
384 if (strcmp(keyword, "Count") == 0)
385 return evp_test_buffer_set_count(value, mdata->input);
386 if (strcmp(keyword, "Ncopy") == 0)
387 return evp_test_buffer_ncopy(value, mdata->input);
388 if (strcmp(keyword, "Padding") == 0)
389 return (mdata->pad_type = atoi(value)) > 0;
393 static int digest_update_fn(void *ctx, const unsigned char *buf, size_t buflen)
395 return EVP_DigestUpdate(ctx, buf, buflen);
398 static int test_duplicate_md_ctx(EVP_TEST *t, EVP_MD_CTX *mctx)
400 char dont[] = "touch";
404 if (!EVP_DigestFinalXOF(mctx, (unsigned char *)dont, 0)) {
405 EVP_MD_CTX_free(mctx);
406 t->err = "DIGESTFINALXOF_ERROR";
409 if (!TEST_str_eq(dont, "touch")) {
410 EVP_MD_CTX_free(mctx);
411 t->err = "DIGESTFINALXOF_ERROR";
414 EVP_MD_CTX_free(mctx);
418 static int digest_test_run(EVP_TEST *t)
420 DIGEST_DATA *expected = t->data;
421 EVP_TEST_BUFFER *inbuf;
423 unsigned char *got = NULL;
424 unsigned int got_len;
427 OSSL_PARAM params[2];
429 printf("test %s (%d %d)\n", t->name, t->s.start, t->s.curr);
430 t->err = "TEST_FAILURE";
431 if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
434 got = OPENSSL_malloc(expected->output_len > EVP_MAX_MD_SIZE ?
435 expected->output_len : EVP_MAX_MD_SIZE);
439 if (!EVP_DigestInit_ex(mctx, expected->digest, NULL)) {
440 t->err = "DIGESTINIT_ERROR";
443 if (expected->pad_type > 0) {
444 params[0] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_PAD_TYPE,
445 &expected->pad_type);
446 params[1] = OSSL_PARAM_construct_end();
447 if (!TEST_int_gt(EVP_MD_CTX_set_params(mctx, params), 0)) {
448 t->err = "PARAMS_ERROR";
452 if (!evp_test_buffer_do(expected->input, digest_update_fn, mctx)) {
453 t->err = "DIGESTUPDATE_ERROR";
457 xof = (EVP_MD_get_flags(expected->digest) & EVP_MD_FLAG_XOF) != 0;
459 EVP_MD_CTX *mctx_cpy;
461 if (!TEST_ptr(mctx_cpy = EVP_MD_CTX_new())) {
464 if (!TEST_true(EVP_MD_CTX_copy(mctx_cpy, mctx))) {
465 EVP_MD_CTX_free(mctx_cpy);
467 } else if (!test_duplicate_md_ctx(t, mctx_cpy)) {
471 if (!test_duplicate_md_ctx(t, EVP_MD_CTX_dup(mctx)))
474 got_len = expected->output_len;
475 if (!EVP_DigestFinalXOF(mctx, got, got_len)) {
476 t->err = "DIGESTFINALXOF_ERROR";
480 if (!EVP_DigestFinal(mctx, got, &got_len)) {
481 t->err = "DIGESTFINAL_ERROR";
485 if (!TEST_int_eq(expected->output_len, got_len)) {
486 t->err = "DIGEST_LENGTH_MISMATCH";
489 if (!memory_err_compare(t, "DIGEST_MISMATCH",
490 expected->output, expected->output_len,
496 /* Test the EVP_Q_digest interface as well */
497 if (sk_EVP_TEST_BUFFER_num(expected->input) == 1
499 /* This should never fail but we need the returned pointer now */
500 && !TEST_ptr(inbuf = sk_EVP_TEST_BUFFER_value(expected->input, 0))
501 && !inbuf->count_set) {
502 OPENSSL_cleanse(got, got_len);
503 if (!TEST_true(EVP_Q_digest(libctx,
504 EVP_MD_get0_name(expected->fetched_digest),
505 NULL, inbuf->buf, inbuf->buflen,
507 || !TEST_mem_eq(got, size,
508 expected->output, expected->output_len)) {
509 t->err = "EVP_Q_digest failed";
516 EVP_MD_CTX_free(mctx);
520 static const EVP_TEST_METHOD digest_test_method = {
532 typedef struct cipher_data_st {
533 const EVP_CIPHER *cipher;
534 EVP_CIPHER *fetched_cipher;
536 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
540 size_t key_bits; /* Used by RC2 */
542 unsigned char *next_iv; /* Expected IV state after operation */
545 unsigned char *plaintext;
546 size_t plaintext_len;
547 unsigned char *ciphertext;
548 size_t ciphertext_len;
549 /* AEAD ciphers only */
550 unsigned char *aad[AAD_NUM];
551 size_t aad_len[AAD_NUM];
555 const char *cts_mode;
558 unsigned char *mac_key;
562 static int cipher_test_init(EVP_TEST *t, const char *alg)
564 const EVP_CIPHER *cipher;
565 EVP_CIPHER *fetched_cipher;
569 if (is_cipher_disabled(alg)) {
571 TEST_info("skipping, '%s' is disabled", alg);
576 if ((cipher = fetched_cipher = EVP_CIPHER_fetch(libctx, alg, NULL)) == NULL
577 && (cipher = EVP_get_cipherbyname(alg)) == NULL) {
578 /* a stitched cipher might not be available */
579 if (strstr(alg, "HMAC") != NULL) {
582 TEST_info("skipping, '%s' is not available", alg);
585 ERR_clear_last_mark();
588 ERR_clear_last_mark();
590 if (!TEST_ptr(cdat = OPENSSL_zalloc(sizeof(*cdat))))
593 cdat->cipher = cipher;
594 cdat->fetched_cipher = fetched_cipher;
596 m = EVP_CIPHER_get_mode(cipher);
597 if (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
598 cdat->aead = m != 0 ? m : -1;
603 if (fetched_cipher != NULL)
604 TEST_info("%s is fetched", alg);
608 static void cipher_test_cleanup(EVP_TEST *t)
611 CIPHER_DATA *cdat = t->data;
613 OPENSSL_free(cdat->key);
614 OPENSSL_free(cdat->iv);
615 OPENSSL_free(cdat->next_iv);
616 OPENSSL_free(cdat->ciphertext);
617 OPENSSL_free(cdat->plaintext);
618 for (i = 0; i < AAD_NUM; i++)
619 OPENSSL_free(cdat->aad[i]);
620 OPENSSL_free(cdat->tag);
621 OPENSSL_free(cdat->mac_key);
622 EVP_CIPHER_free(cdat->fetched_cipher);
625 static int cipher_test_parse(EVP_TEST *t, const char *keyword,
628 CIPHER_DATA *cdat = t->data;
631 if (strcmp(keyword, "Key") == 0)
632 return parse_bin(value, &cdat->key, &cdat->key_len);
633 if (strcmp(keyword, "Rounds") == 0) {
637 cdat->rounds = (unsigned int)i;
640 if (strcmp(keyword, "IV") == 0)
641 return parse_bin(value, &cdat->iv, &cdat->iv_len);
642 if (strcmp(keyword, "NextIV") == 0)
643 return parse_bin(value, &cdat->next_iv, &cdat->iv_len);
644 if (strcmp(keyword, "Plaintext") == 0)
645 return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len);
646 if (strcmp(keyword, "Ciphertext") == 0)
647 return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
648 if (strcmp(keyword, "KeyBits") == 0) {
652 cdat->key_bits = (size_t)i;
658 if (strcmp(keyword, "TLSAAD") == 0)
659 cdat->tls_aad = tls_aad = 1;
660 if (strcmp(keyword, "AAD") == 0 || tls_aad) {
661 for (i = 0; i < AAD_NUM; i++) {
662 if (cdat->aad[i] == NULL)
663 return parse_bin(value, &cdat->aad[i], &cdat->aad_len[i]);
667 if (strcmp(keyword, "Tag") == 0)
668 return parse_bin(value, &cdat->tag, &cdat->tag_len);
669 if (strcmp(keyword, "SetTagLate") == 0) {
670 if (strcmp(value, "TRUE") == 0)
672 else if (strcmp(value, "FALSE") == 0)
678 if (strcmp(keyword, "MACKey") == 0)
679 return parse_bin(value, &cdat->mac_key, &cdat->mac_key_len);
680 if (strcmp(keyword, "TLSVersion") == 0) {
683 cdat->tls_version = (int)strtol(value, &endptr, 0);
684 return value[0] != '\0' && endptr[0] == '\0';
688 if (strcmp(keyword, "Operation") == 0) {
689 if (strcmp(value, "ENCRYPT") == 0)
691 else if (strcmp(value, "DECRYPT") == 0)
697 if (strcmp(keyword, "CTSMode") == 0) {
698 cdat->cts_mode = value;
704 static int cipher_test_enc(EVP_TEST *t, int enc,
705 size_t out_misalign, size_t inp_misalign, int frag)
707 CIPHER_DATA *expected = t->data;
708 unsigned char *in, *expected_out, *tmp = NULL;
709 size_t in_len, out_len, donelen = 0;
710 int ok = 0, tmplen, chunklen, tmpflen, i;
711 EVP_CIPHER_CTX *ctx_base = NULL;
712 EVP_CIPHER_CTX *ctx = NULL, *duped;
714 t->err = "TEST_FAILURE";
715 if (!TEST_ptr(ctx_base = EVP_CIPHER_CTX_new()))
717 if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
719 EVP_CIPHER_CTX_set_flags(ctx_base, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
721 in = expected->plaintext;
722 in_len = expected->plaintext_len;
723 expected_out = expected->ciphertext;
724 out_len = expected->ciphertext_len;
726 in = expected->ciphertext;
727 in_len = expected->ciphertext_len;
728 expected_out = expected->plaintext;
729 out_len = expected->plaintext_len;
731 if (inp_misalign == (size_t)-1) {
732 /* Exercise in-place encryption */
733 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
736 in = memcpy(tmp + out_misalign, in, in_len);
738 inp_misalign += 16 - ((out_misalign + in_len) & 15);
740 * 'tmp' will store both output and copy of input. We make the copy
741 * of input to specifically aligned part of 'tmp'. So we just
742 * figured out how much padding would ensure the required alignment,
743 * now we allocate extended buffer and finally copy the input just
744 * past inp_misalign in expression below. Output will be written
745 * past out_misalign...
747 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
748 inp_misalign + in_len);
751 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
752 inp_misalign, in, in_len);
754 if (!EVP_CipherInit_ex(ctx_base, expected->cipher, NULL, NULL, NULL, enc)) {
755 t->err = "CIPHERINIT_ERROR";
758 if (expected->cts_mode != NULL) {
759 OSSL_PARAM params[2];
761 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE,
762 (char *)expected->cts_mode,
764 params[1] = OSSL_PARAM_construct_end();
765 if (!EVP_CIPHER_CTX_set_params(ctx_base, params)) {
766 t->err = "INVALID_CTS_MODE";
771 if (expected->aead) {
772 if (EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_IVLEN,
773 expected->iv_len, 0) <= 0) {
774 t->err = "INVALID_IV_LENGTH";
777 } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_get_iv_length(ctx_base)) {
778 t->err = "INVALID_IV_LENGTH";
782 if (expected->aead && !expected->tls_aad) {
785 * If encrypting or OCB just set tag length initially, otherwise
786 * set tag length and value.
788 if (enc || expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late) {
789 t->err = "TAG_LENGTH_SET_ERROR";
792 t->err = "TAG_SET_ERROR";
795 if (tag || expected->aead != EVP_CIPH_GCM_MODE) {
796 if (EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_TAG,
797 expected->tag_len, tag) <= 0)
802 if (expected->rounds > 0) {
803 int rounds = (int)expected->rounds;
805 if (EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC5_ROUNDS, rounds, NULL) <= 0) {
806 t->err = "INVALID_ROUNDS";
811 if (!EVP_CIPHER_CTX_set_key_length(ctx_base, expected->key_len)) {
812 t->err = "INVALID_KEY_LENGTH";
815 if (expected->key_bits > 0) {
816 int bits = (int)expected->key_bits;
818 if (EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC2_KEY_BITS, bits, NULL) <= 0) {
819 t->err = "INVALID KEY BITS";
823 if (!EVP_CipherInit_ex(ctx_base, NULL, NULL, expected->key, expected->iv, -1)) {
824 t->err = "KEY_SET_ERROR";
828 /* Check that we get the same IV back */
829 if (expected->iv != NULL) {
830 /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
831 unsigned char iv[128];
832 if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx_base, iv, sizeof(iv)))
833 || ((EVP_CIPHER_get_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
834 && !TEST_mem_eq(expected->iv, expected->iv_len, iv,
835 expected->iv_len))) {
836 t->err = "INVALID_IV";
841 /* Test that the cipher dup functions correctly if it is supported */
843 if (EVP_CIPHER_CTX_copy(ctx, ctx_base)) {
844 EVP_CIPHER_CTX_free(ctx_base);
847 EVP_CIPHER_CTX_free(ctx);
850 /* Likewise for dup */
851 duped = EVP_CIPHER_CTX_dup(ctx);
853 EVP_CIPHER_CTX_free(ctx);
858 if (expected->mac_key != NULL
859 && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY,
860 (int)expected->mac_key_len,
861 (void *)expected->mac_key) <= 0) {
862 t->err = "SET_MAC_KEY_ERROR";
866 if (expected->tls_version) {
867 OSSL_PARAM params[2];
869 params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS_VERSION,
870 &expected->tls_version);
871 params[1] = OSSL_PARAM_construct_end();
872 if (!EVP_CIPHER_CTX_set_params(ctx, params)) {
873 t->err = "SET_TLS_VERSION_ERROR";
878 if (expected->aead == EVP_CIPH_CCM_MODE) {
879 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
880 t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
884 if (expected->aad[0] != NULL && !expected->tls_aad) {
885 t->err = "AAD_SET_ERROR";
887 for (i = 0; expected->aad[i] != NULL; i++) {
888 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i],
889 expected->aad_len[i]))
894 * Supply the AAD in chunks less than the block size where possible
896 for (i = 0; expected->aad[i] != NULL; i++) {
897 if (expected->aad_len[i] > 0) {
898 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], 1))
902 if (expected->aad_len[i] > 2) {
903 if (!EVP_CipherUpdate(ctx, NULL, &chunklen,
904 expected->aad[i] + donelen,
905 expected->aad_len[i] - 2))
907 donelen += expected->aad_len[i] - 2;
909 if (expected->aad_len[i] > 1
910 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
911 expected->aad[i] + donelen, 1))
917 if (expected->tls_aad) {
918 OSSL_PARAM params[2];
921 /* duplicate the aad as the implementation might modify it */
922 if ((tls_aad = OPENSSL_memdup(expected->aad[0],
923 expected->aad_len[0])) == NULL)
925 params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
927 expected->aad_len[0]);
928 params[1] = OSSL_PARAM_construct_end();
929 if (!EVP_CIPHER_CTX_set_params(ctx, params)) {
930 OPENSSL_free(tls_aad);
931 t->err = "TLS1_AAD_ERROR";
934 OPENSSL_free(tls_aad);
935 } else if (!enc && (expected->aead == EVP_CIPH_OCB_MODE
936 || expected->tag_late)) {
937 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
938 expected->tag_len, expected->tag) <= 0) {
939 t->err = "TAG_SET_ERROR";
944 EVP_CIPHER_CTX_set_padding(ctx, 0);
945 t->err = "CIPHERUPDATE_ERROR";
948 /* We supply the data all in one go */
949 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
952 /* Supply the data in chunks less than the block size where possible */
954 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
961 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
969 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
975 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
976 t->err = "CIPHERFINAL_ERROR";
979 if (!enc && expected->tls_aad) {
980 if (expected->tls_version >= TLS1_1_VERSION
981 && (EVP_CIPHER_is_a(expected->cipher, "AES-128-CBC-HMAC-SHA1")
982 || EVP_CIPHER_is_a(expected->cipher, "AES-256-CBC-HMAC-SHA1"))) {
983 tmplen -= expected->iv_len;
984 expected_out += expected->iv_len;
985 out_misalign += expected->iv_len;
987 if ((int)out_len > tmplen + tmpflen)
988 out_len = tmplen + tmpflen;
990 if (!memory_err_compare(t, "VALUE_MISMATCH", expected_out, out_len,
991 tmp + out_misalign, tmplen + tmpflen))
993 if (enc && expected->aead && !expected->tls_aad) {
994 unsigned char rtag[16];
996 if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) {
997 t->err = "TAG_LENGTH_INTERNAL_ERROR";
1000 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
1001 expected->tag_len, rtag) <= 0) {
1002 t->err = "TAG_RETRIEVE_ERROR";
1005 if (!memory_err_compare(t, "TAG_VALUE_MISMATCH",
1006 expected->tag, expected->tag_len,
1007 rtag, expected->tag_len))
1010 /* Check the updated IV */
1011 if (expected->next_iv != NULL) {
1012 /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
1013 unsigned char iv[128];
1014 if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv)))
1015 || ((EVP_CIPHER_get_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0
1016 && !TEST_mem_eq(expected->next_iv, expected->iv_len, iv,
1017 expected->iv_len))) {
1018 t->err = "INVALID_NEXT_IV";
1027 if (ctx != ctx_base)
1028 EVP_CIPHER_CTX_free(ctx_base);
1029 EVP_CIPHER_CTX_free(ctx);
1033 static int cipher_test_run(EVP_TEST *t)
1035 CIPHER_DATA *cdat = t->data;
1037 size_t out_misalign, inp_misalign;
1043 if (!cdat->iv && EVP_CIPHER_get_iv_length(cdat->cipher)) {
1044 /* IV is optional and usually omitted in wrap mode */
1045 if (EVP_CIPHER_get_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
1050 if (cdat->aead && cdat->tag == NULL && !cdat->tls_aad) {
1054 for (out_misalign = 0; out_misalign <= 1;) {
1055 static char aux_err[64];
1056 t->aux_err = aux_err;
1057 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
1058 if (inp_misalign == (size_t)-1) {
1059 /* kludge: inp_misalign == -1 means "exercise in-place" */
1060 BIO_snprintf(aux_err, sizeof(aux_err),
1061 "%s in-place, %sfragmented",
1062 out_misalign ? "misaligned" : "aligned",
1063 frag ? "" : "not ");
1065 BIO_snprintf(aux_err, sizeof(aux_err),
1066 "%s output and %s input, %sfragmented",
1067 out_misalign ? "misaligned" : "aligned",
1068 inp_misalign ? "misaligned" : "aligned",
1069 frag ? "" : "not ");
1072 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
1073 /* Not fatal errors: return */
1080 if (cdat->enc != 1) {
1081 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
1082 /* Not fatal errors: return */
1091 if (out_misalign == 1 && frag == 0) {
1093 * XTS, SIV, CCM, stitched ciphers and Wrap modes have special
1094 * requirements about input lengths so we don't fragment for those
1096 if (cdat->aead == EVP_CIPH_CCM_MODE
1097 || cdat->aead == EVP_CIPH_CBC_MODE
1098 || (cdat->aead == -1
1099 && EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_STREAM_CIPHER)
1100 || ((EVP_CIPHER_get_flags(cdat->cipher) & EVP_CIPH_FLAG_CTS) != 0)
1101 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_SIV_MODE
1102 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
1103 || EVP_CIPHER_get_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
1116 static const EVP_TEST_METHOD cipher_test_method = {
1119 cipher_test_cleanup,
1129 typedef struct mac_data_st {
1130 /* MAC type in one form or another */
1132 EVP_MAC *mac; /* for mac_test_run_mac */
1133 int type; /* for mac_test_run_pkey */
1134 /* Algorithm string for this MAC */
1143 unsigned char *input;
1145 /* Expected output */
1146 unsigned char *output;
1148 unsigned char *custom;
1150 /* MAC salt (blake2) */
1151 unsigned char *salt;
1155 /* Reinitialization fails */
1157 /* Collection of controls */
1158 STACK_OF(OPENSSL_STRING) *controls;
1165 static int mac_test_init(EVP_TEST *t, const char *alg)
1167 EVP_MAC *mac = NULL;
1168 int type = NID_undef;
1171 if (is_mac_disabled(alg)) {
1172 TEST_info("skipping, '%s' is disabled", alg);
1176 if ((mac = EVP_MAC_fetch(libctx, alg, NULL)) == NULL) {
1178 * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
1179 * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
1180 * the EVP_PKEY method.
1182 size_t sz = strlen(alg);
1183 static const char epilogue[] = " by EVP_PKEY";
1185 if (sz >= sizeof(epilogue)
1186 && strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0)
1187 sz -= sizeof(epilogue) - 1;
1189 if (strncmp(alg, "HMAC", sz) == 0)
1190 type = EVP_PKEY_HMAC;
1191 else if (strncmp(alg, "CMAC", sz) == 0)
1192 type = EVP_PKEY_CMAC;
1193 else if (strncmp(alg, "Poly1305", sz) == 0)
1194 type = EVP_PKEY_POLY1305;
1195 else if (strncmp(alg, "SipHash", sz) == 0)
1196 type = EVP_PKEY_SIPHASH;
1201 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
1205 if (!TEST_ptr(mdat->mac_name = OPENSSL_strdup(alg))) {
1211 if (!TEST_ptr(mdat->controls = sk_OPENSSL_STRING_new_null())) {
1212 OPENSSL_free(mdat->mac_name);
1217 mdat->output_size = mdat->block_size = -1;
1222 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
1223 static void openssl_free(char *m)
1228 static void mac_test_cleanup(EVP_TEST *t)
1230 MAC_DATA *mdat = t->data;
1232 EVP_MAC_free(mdat->mac);
1233 OPENSSL_free(mdat->mac_name);
1234 sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free);
1235 OPENSSL_free(mdat->alg);
1236 OPENSSL_free(mdat->key);
1237 OPENSSL_free(mdat->iv);
1238 OPENSSL_free(mdat->custom);
1239 OPENSSL_free(mdat->salt);
1240 OPENSSL_free(mdat->input);
1241 OPENSSL_free(mdat->output);
1244 static int mac_test_parse(EVP_TEST *t,
1245 const char *keyword, const char *value)
1247 MAC_DATA *mdata = t->data;
1249 if (strcmp(keyword, "Key") == 0)
1250 return parse_bin(value, &mdata->key, &mdata->key_len);
1251 if (strcmp(keyword, "IV") == 0)
1252 return parse_bin(value, &mdata->iv, &mdata->iv_len);
1253 if (strcmp(keyword, "Custom") == 0)
1254 return parse_bin(value, &mdata->custom, &mdata->custom_len);
1255 if (strcmp(keyword, "Salt") == 0)
1256 return parse_bin(value, &mdata->salt, &mdata->salt_len);
1257 if (strcmp(keyword, "Algorithm") == 0) {
1258 mdata->alg = OPENSSL_strdup(value);
1259 if (mdata->alg == NULL)
1263 if (strcmp(keyword, "Input") == 0)
1264 return parse_bin(value, &mdata->input, &mdata->input_len);
1265 if (strcmp(keyword, "Output") == 0)
1266 return parse_bin(value, &mdata->output, &mdata->output_len);
1267 if (strcmp(keyword, "XOF") == 0)
1268 return mdata->xof = 1;
1269 if (strcmp(keyword, "NoReinit") == 0)
1270 return mdata->no_reinit = 1;
1271 if (strcmp(keyword, "Ctrl") == 0) {
1272 char *data = OPENSSL_strdup(value);
1276 return sk_OPENSSL_STRING_push(mdata->controls, data) != 0;
1278 if (strcmp(keyword, "OutputSize") == 0) {
1279 mdata->output_size = atoi(value);
1280 if (mdata->output_size < 0)
1284 if (strcmp(keyword, "BlockSize") == 0) {
1285 mdata->block_size = atoi(value);
1286 if (mdata->block_size < 0)
1293 static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1299 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1301 p = strchr(tmpval, ':');
1304 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1307 t->err = "PKEY_CTRL_INVALID";
1309 t->err = "PKEY_CTRL_ERROR";
1312 OPENSSL_free(tmpval);
1316 static int mac_test_run_pkey(EVP_TEST *t)
1318 MAC_DATA *expected = t->data;
1319 EVP_MD_CTX *mctx = NULL;
1320 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1321 EVP_PKEY *key = NULL;
1322 const char *mdname = NULL;
1323 EVP_CIPHER *cipher = NULL;
1324 unsigned char *got = NULL;
1328 /* We don't do XOF mode via PKEY */
1332 if (expected->alg == NULL)
1333 TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type));
1335 TEST_info("Trying the EVP_PKEY %s test with %s",
1336 OBJ_nid2sn(expected->type), expected->alg);
1338 if (expected->type == EVP_PKEY_CMAC) {
1339 #ifdef OPENSSL_NO_DEPRECATED_3_0
1340 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
1345 OSSL_LIB_CTX *tmpctx;
1347 if (expected->alg != NULL && is_cipher_disabled(expected->alg)) {
1348 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg);
1353 if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, expected->alg, NULL))) {
1354 t->err = "MAC_KEY_CREATE_ERROR";
1357 tmpctx = OSSL_LIB_CTX_set0_default(libctx);
1358 key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len,
1360 OSSL_LIB_CTX_set0_default(tmpctx);
1363 key = EVP_PKEY_new_raw_private_key_ex(libctx,
1364 OBJ_nid2sn(expected->type), NULL,
1365 expected->key, expected->key_len);
1368 t->err = "MAC_KEY_CREATE_ERROR";
1372 if (expected->type == EVP_PKEY_HMAC && expected->alg != NULL) {
1373 if (is_digest_disabled(expected->alg)) {
1374 TEST_info("skipping, HMAC '%s' is disabled", expected->alg);
1379 mdname = expected->alg;
1381 if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
1382 t->err = "INTERNAL_ERROR";
1385 if (!EVP_DigestSignInit_ex(mctx, &pctx, mdname, libctx, NULL, key, NULL)) {
1386 t->err = "DIGESTSIGNINIT_ERROR";
1389 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++)
1390 if (!mac_test_ctrl_pkey(t, pctx,
1391 sk_OPENSSL_STRING_value(expected->controls,
1393 t->err = "EVPPKEYCTXCTRL_ERROR";
1396 if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) {
1397 t->err = "DIGESTSIGNUPDATE_ERROR";
1400 if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) {
1401 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1404 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1405 t->err = "TEST_FAILURE";
1408 if (!EVP_DigestSignFinal(mctx, got, &got_len)
1409 || !memory_err_compare(t, "TEST_MAC_ERR",
1410 expected->output, expected->output_len,
1412 t->err = "TEST_MAC_ERR";
1417 EVP_CIPHER_free(cipher);
1418 EVP_MD_CTX_free(mctx);
1420 EVP_PKEY_CTX_free(genctx);
1425 static int mac_test_run_mac(EVP_TEST *t)
1427 MAC_DATA *expected = t->data;
1428 EVP_MAC_CTX *ctx = NULL;
1429 unsigned char *got = NULL;
1430 size_t got_len = 0, size = 0;
1431 int i, block_size = -1, output_size = -1;
1432 OSSL_PARAM params[21], sizes[3], *psizes = sizes;
1433 size_t params_n = 0;
1434 size_t params_n_allocstart = 0;
1435 const OSSL_PARAM *defined_params =
1436 EVP_MAC_settable_ctx_params(expected->mac);
1440 if (expected->alg == NULL)
1441 TEST_info("Trying the EVP_MAC %s test", expected->mac_name);
1443 TEST_info("Trying the EVP_MAC %s test with %s",
1444 expected->mac_name, expected->alg);
1446 if (expected->alg != NULL) {
1448 * The underlying algorithm may be a cipher or a digest.
1449 * We don't know which it is, but we can ask the MAC what it
1450 * should be and bet on that.
1452 if (OSSL_PARAM_locate_const(defined_params,
1453 OSSL_MAC_PARAM_CIPHER) != NULL) {
1454 params[params_n++] =
1455 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
1457 } else if (OSSL_PARAM_locate_const(defined_params,
1458 OSSL_MAC_PARAM_DIGEST) != NULL) {
1459 params[params_n++] =
1460 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
1463 t->err = "MAC_BAD_PARAMS";
1467 if (expected->custom != NULL)
1468 params[params_n++] =
1469 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
1471 expected->custom_len);
1472 if (expected->salt != NULL)
1473 params[params_n++] =
1474 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT,
1476 expected->salt_len);
1477 if (expected->iv != NULL)
1478 params[params_n++] =
1479 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
1483 /* Unknown controls. They must match parameters that the MAC recognizes */
1484 if (params_n + sk_OPENSSL_STRING_num(expected->controls)
1485 >= OSSL_NELEM(params)) {
1486 t->err = "MAC_TOO_MANY_PARAMETERS";
1489 params_n_allocstart = params_n;
1490 for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) {
1491 char *tmpkey, *tmpval;
1492 char *value = sk_OPENSSL_STRING_value(expected->controls, i);
1494 if (!TEST_ptr(tmpkey = OPENSSL_strdup(value))) {
1495 t->err = "MAC_PARAM_ERROR";
1498 tmpval = strchr(tmpkey, ':');
1503 || !OSSL_PARAM_allocate_from_text(¶ms[params_n],
1506 strlen(tmpval), NULL)) {
1507 OPENSSL_free(tmpkey);
1508 t->err = "MAC_PARAM_ERROR";
1513 OPENSSL_free(tmpkey);
1515 params[params_n] = OSSL_PARAM_construct_end();
1517 if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) {
1518 t->err = "MAC_CREATE_ERROR";
1522 if (!EVP_MAC_init(ctx, expected->key, expected->key_len, params)) {
1523 t->err = "MAC_INIT_ERROR";
1526 if (expected->output_size >= 0)
1527 *psizes++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_SIZE,
1529 if (expected->block_size >= 0)
1530 *psizes++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_BLOCK_SIZE,
1532 if (psizes != sizes) {
1533 *psizes = OSSL_PARAM_construct_end();
1534 if (!TEST_true(EVP_MAC_CTX_get_params(ctx, sizes))) {
1535 t->err = "INTERNAL_ERROR";
1538 if (expected->output_size >= 0
1539 && !TEST_int_eq(output_size, expected->output_size)) {
1540 t->err = "TEST_FAILURE";
1543 if (expected->block_size >= 0
1544 && !TEST_int_eq(block_size, expected->block_size)) {
1545 t->err = "TEST_FAILURE";
1550 if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) {
1551 t->err = "MAC_UPDATE_ERROR";
1554 xof = expected->xof;
1556 if (!TEST_ptr(got = OPENSSL_malloc(expected->output_len))) {
1557 t->err = "TEST_FAILURE";
1560 if (!EVP_MAC_finalXOF(ctx, got, expected->output_len)
1561 || !memory_err_compare(t, "TEST_MAC_ERR",
1562 expected->output, expected->output_len,
1563 got, expected->output_len)) {
1564 t->err = "MAC_FINAL_ERROR";
1568 if (!EVP_MAC_final(ctx, NULL, &got_len, 0)) {
1569 t->err = "MAC_FINAL_LENGTH_ERROR";
1572 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1573 t->err = "TEST_FAILURE";
1576 if (!EVP_MAC_final(ctx, got, &got_len, got_len)
1577 || !memory_err_compare(t, "TEST_MAC_ERR",
1578 expected->output, expected->output_len,
1580 t->err = "TEST_MAC_ERR";
1585 OSSL_PARAM ivparams[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1588 /* If the MAC uses IV, we have to set it again */
1589 if (expected->iv != NULL) {
1591 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
1594 ivparams[1] = OSSL_PARAM_construct_end();
1597 ret = EVP_MAC_init(ctx, NULL, 0, ivparams);
1598 if (expected->no_reinit) {
1600 ERR_clear_last_mark();
1601 t->err = "MAC_REINIT_SHOULD_FAIL";
1605 ERR_clear_last_mark();
1610 ERR_clear_last_mark();
1611 t->err = "MAC_REINIT_ERROR";
1614 /* If reinitialization fails, it is unsupported by the algorithm */
1619 /* Test the EVP_Q_mac interface as well */
1621 OPENSSL_cleanse(got, got_len);
1622 if (!TEST_true(EVP_Q_mac(libctx, expected->mac_name, NULL,
1623 expected->alg, params,
1624 expected->key, expected->key_len,
1625 expected->input, expected->input_len,
1626 got, got_len, &size))
1627 || !TEST_mem_eq(got, size,
1628 expected->output, expected->output_len)) {
1629 t->err = "EVP_Q_mac failed";
1634 while (params_n-- > params_n_allocstart) {
1635 OPENSSL_free(params[params_n].data);
1637 EVP_MAC_CTX_free(ctx);
1642 static int mac_test_run(EVP_TEST *t)
1644 MAC_DATA *expected = t->data;
1646 if (expected->mac != NULL)
1647 return mac_test_run_mac(t);
1648 return mac_test_run_pkey(t);
1651 static const EVP_TEST_METHOD mac_test_method = {
1662 ** These are all very similar and share much common code.
1665 typedef struct pkey_data_st {
1666 /* Context for this operation */
1668 /* Key operation to perform */
1669 int (*keyop) (EVP_PKEY_CTX *ctx,
1670 unsigned char *sig, size_t *siglen,
1671 const unsigned char *tbs, size_t tbslen);
1673 unsigned char *input;
1675 /* Expected output */
1676 unsigned char *output;
1681 * Perform public key operation setup: lookup key, allocated ctx and call
1682 * the appropriate initialisation function
1684 static int pkey_test_init(EVP_TEST *t, const char *name,
1686 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1687 int (*keyop)(EVP_PKEY_CTX *ctx,
1688 unsigned char *sig, size_t *siglen,
1689 const unsigned char *tbs,
1693 EVP_PKEY *pkey = NULL;
1697 rv = find_key(&pkey, name, public_keys);
1699 rv = find_key(&pkey, name, private_keys);
1700 if (rv == 0 || pkey == NULL) {
1701 TEST_info("skipping, key '%s' is disabled", name);
1706 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) {
1707 EVP_PKEY_free(pkey);
1710 kdata->keyop = keyop;
1711 if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL))) {
1712 EVP_PKEY_free(pkey);
1713 OPENSSL_free(kdata);
1716 if (keyopinit(kdata->ctx) <= 0)
1717 t->err = "KEYOP_INIT_ERROR";
1722 static void pkey_test_cleanup(EVP_TEST *t)
1724 PKEY_DATA *kdata = t->data;
1726 OPENSSL_free(kdata->input);
1727 OPENSSL_free(kdata->output);
1728 EVP_PKEY_CTX_free(kdata->ctx);
1731 static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1737 if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1739 p = strchr(tmpval, ':');
1742 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1745 t->err = "PKEY_CTRL_INVALID";
1747 } else if (p != NULL && rv <= 0) {
1748 if (is_digest_disabled(p) || is_cipher_disabled(p)) {
1749 TEST_info("skipping, '%s' is disabled", p);
1753 t->err = "PKEY_CTRL_ERROR";
1757 OPENSSL_free(tmpval);
1761 static int pkey_test_parse(EVP_TEST *t,
1762 const char *keyword, const char *value)
1764 PKEY_DATA *kdata = t->data;
1765 if (strcmp(keyword, "Input") == 0)
1766 return parse_bin(value, &kdata->input, &kdata->input_len);
1767 if (strcmp(keyword, "Output") == 0)
1768 return parse_bin(value, &kdata->output, &kdata->output_len);
1769 if (strcmp(keyword, "Ctrl") == 0)
1770 return pkey_test_ctrl(t, kdata->ctx, value);
1774 static int pkey_test_run(EVP_TEST *t)
1776 PKEY_DATA *expected = t->data;
1777 unsigned char *got = NULL;
1779 EVP_PKEY_CTX *copy = NULL;
1781 if (expected->keyop(expected->ctx, NULL, &got_len,
1782 expected->input, expected->input_len) <= 0
1783 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1784 t->err = "KEYOP_LENGTH_ERROR";
1787 if (expected->keyop(expected->ctx, got, &got_len,
1788 expected->input, expected->input_len) <= 0) {
1789 t->err = "KEYOP_ERROR";
1792 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1793 expected->output, expected->output_len,
1801 /* Repeat the test on a copy. */
1802 if (!TEST_ptr(copy = EVP_PKEY_CTX_dup(expected->ctx))) {
1803 t->err = "INTERNAL_ERROR";
1806 if (expected->keyop(copy, NULL, &got_len, expected->input,
1807 expected->input_len) <= 0
1808 || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
1809 t->err = "KEYOP_LENGTH_ERROR";
1812 if (expected->keyop(copy, got, &got_len, expected->input,
1813 expected->input_len) <= 0) {
1814 t->err = "KEYOP_ERROR";
1817 if (!memory_err_compare(t, "KEYOP_MISMATCH",
1818 expected->output, expected->output_len,
1824 EVP_PKEY_CTX_free(copy);
1828 static int sign_test_init(EVP_TEST *t, const char *name)
1830 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1833 static const EVP_TEST_METHOD psign_test_method = {
1841 static int verify_recover_test_init(EVP_TEST *t, const char *name)
1843 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1844 EVP_PKEY_verify_recover);
1847 static const EVP_TEST_METHOD pverify_recover_test_method = {
1849 verify_recover_test_init,
1855 static int decrypt_test_init(EVP_TEST *t, const char *name)
1857 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1861 static const EVP_TEST_METHOD pdecrypt_test_method = {
1869 static int verify_test_init(EVP_TEST *t, const char *name)
1871 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1874 static int verify_test_run(EVP_TEST *t)
1876 PKEY_DATA *kdata = t->data;
1878 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1879 kdata->input, kdata->input_len) <= 0)
1880 t->err = "VERIFY_ERROR";
1884 static const EVP_TEST_METHOD pverify_test_method = {
1892 static int pderive_test_init(EVP_TEST *t, const char *name)
1894 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1897 static int pderive_test_parse(EVP_TEST *t,
1898 const char *keyword, const char *value)
1900 PKEY_DATA *kdata = t->data;
1903 if (strcmp(keyword, "PeerKeyValidate") == 0)
1906 if (validate || strcmp(keyword, "PeerKey") == 0) {
1908 if (find_key(&peer, value, public_keys) == 0)
1910 if (EVP_PKEY_derive_set_peer_ex(kdata->ctx, peer, validate) <= 0) {
1911 t->err = "DERIVE_SET_PEER_ERROR";
1917 if (strcmp(keyword, "SharedSecret") == 0)
1918 return parse_bin(value, &kdata->output, &kdata->output_len);
1919 if (strcmp(keyword, "Ctrl") == 0)
1920 return pkey_test_ctrl(t, kdata->ctx, value);
1921 if (strcmp(keyword, "KDFType") == 0) {
1922 OSSL_PARAM params[2];
1924 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_EXCHANGE_PARAM_KDF_TYPE,
1926 params[1] = OSSL_PARAM_construct_end();
1927 if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0)
1931 if (strcmp(keyword, "KDFDigest") == 0) {
1932 OSSL_PARAM params[2];
1934 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_EXCHANGE_PARAM_KDF_DIGEST,
1936 params[1] = OSSL_PARAM_construct_end();
1937 if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0)
1941 if (strcmp(keyword, "CEKAlg") == 0) {
1942 OSSL_PARAM params[2];
1944 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CEK_ALG,
1946 params[1] = OSSL_PARAM_construct_end();
1947 if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0)
1951 if (strcmp(keyword, "KDFOutlen") == 0) {
1952 OSSL_PARAM params[2];
1954 size_t outlen = (size_t)strtoul(value, &endptr, 0);
1956 if (endptr[0] != '\0')
1959 params[0] = OSSL_PARAM_construct_size_t(OSSL_EXCHANGE_PARAM_KDF_OUTLEN,
1961 params[1] = OSSL_PARAM_construct_end();
1962 if (EVP_PKEY_CTX_set_params(kdata->ctx, params) == 0)
1969 static int pderive_test_run(EVP_TEST *t)
1971 EVP_PKEY_CTX *dctx = NULL;
1972 PKEY_DATA *expected = t->data;
1973 unsigned char *got = NULL;
1976 if (!TEST_ptr(dctx = EVP_PKEY_CTX_dup(expected->ctx))) {
1977 t->err = "DERIVE_ERROR";
1981 if (EVP_PKEY_derive(dctx, NULL, &got_len) <= 0
1982 || !TEST_size_t_ne(got_len, 0)) {
1983 t->err = "DERIVE_ERROR";
1986 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
1987 t->err = "DERIVE_ERROR";
1990 if (EVP_PKEY_derive(dctx, got, &got_len) <= 0) {
1991 t->err = "DERIVE_ERROR";
1994 if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH",
1995 expected->output, expected->output_len,
2002 EVP_PKEY_CTX_free(dctx);
2006 static const EVP_TEST_METHOD pderive_test_method = {
2019 typedef enum pbe_type_enum {
2020 PBE_TYPE_INVALID = 0,
2021 PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
2024 typedef struct pbe_data_st {
2026 /* scrypt parameters */
2027 uint64_t N, r, p, maxmem;
2028 /* PKCS#12 parameters */
2032 unsigned char *pass;
2035 unsigned char *salt;
2037 /* Expected output */
2042 #ifndef OPENSSL_NO_SCRYPT
2043 /* Parse unsigned decimal 64 bit integer value */
2044 static int parse_uint64(const char *value, uint64_t *pr)
2046 const char *p = value;
2048 if (!TEST_true(*p)) {
2049 TEST_info("Invalid empty integer value");
2052 for (*pr = 0; *p; ) {
2053 if (*pr > UINT64_MAX / 10) {
2054 TEST_error("Integer overflow in string %s", value);
2058 if (!TEST_true(isdigit((unsigned char)*p))) {
2059 TEST_error("Invalid character in string %s", value);
2068 static int scrypt_test_parse(EVP_TEST *t,
2069 const char *keyword, const char *value)
2071 PBE_DATA *pdata = t->data;
2073 if (strcmp(keyword, "N") == 0)
2074 return parse_uint64(value, &pdata->N);
2075 if (strcmp(keyword, "p") == 0)
2076 return parse_uint64(value, &pdata->p);
2077 if (strcmp(keyword, "r") == 0)
2078 return parse_uint64(value, &pdata->r);
2079 if (strcmp(keyword, "maxmem") == 0)
2080 return parse_uint64(value, &pdata->maxmem);
2085 static int pbkdf2_test_parse(EVP_TEST *t,
2086 const char *keyword, const char *value)
2088 PBE_DATA *pdata = t->data;
2090 if (strcmp(keyword, "iter") == 0) {
2091 pdata->iter = atoi(value);
2092 if (pdata->iter <= 0)
2096 if (strcmp(keyword, "MD") == 0) {
2097 pdata->md = EVP_get_digestbyname(value);
2098 if (pdata->md == NULL)
2105 static int pkcs12_test_parse(EVP_TEST *t,
2106 const char *keyword, const char *value)
2108 PBE_DATA *pdata = t->data;
2110 if (strcmp(keyword, "id") == 0) {
2111 pdata->id = atoi(value);
2116 return pbkdf2_test_parse(t, keyword, value);
2119 static int pbe_test_init(EVP_TEST *t, const char *alg)
2122 PBE_TYPE pbe_type = PBE_TYPE_INVALID;
2124 if (is_kdf_disabled(alg)) {
2125 TEST_info("skipping, '%s' is disabled", alg);
2129 if (strcmp(alg, "scrypt") == 0) {
2130 pbe_type = PBE_TYPE_SCRYPT;
2131 } else if (strcmp(alg, "pbkdf2") == 0) {
2132 pbe_type = PBE_TYPE_PBKDF2;
2133 } else if (strcmp(alg, "pkcs12") == 0) {
2134 pbe_type = PBE_TYPE_PKCS12;
2136 TEST_error("Unknown pbe algorithm %s", alg);
2139 if (!TEST_ptr(pdat = OPENSSL_zalloc(sizeof(*pdat))))
2141 pdat->pbe_type = pbe_type;
2146 static void pbe_test_cleanup(EVP_TEST *t)
2148 PBE_DATA *pdat = t->data;
2150 OPENSSL_free(pdat->pass);
2151 OPENSSL_free(pdat->salt);
2152 OPENSSL_free(pdat->key);
2155 static int pbe_test_parse(EVP_TEST *t,
2156 const char *keyword, const char *value)
2158 PBE_DATA *pdata = t->data;
2160 if (strcmp(keyword, "Password") == 0)
2161 return parse_bin(value, &pdata->pass, &pdata->pass_len);
2162 if (strcmp(keyword, "Salt") == 0)
2163 return parse_bin(value, &pdata->salt, &pdata->salt_len);
2164 if (strcmp(keyword, "Key") == 0)
2165 return parse_bin(value, &pdata->key, &pdata->key_len);
2166 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
2167 return pbkdf2_test_parse(t, keyword, value);
2168 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
2169 return pkcs12_test_parse(t, keyword, value);
2170 #ifndef OPENSSL_NO_SCRYPT
2171 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
2172 return scrypt_test_parse(t, keyword, value);
2177 static int pbe_test_run(EVP_TEST *t)
2179 PBE_DATA *expected = t->data;
2181 EVP_MD *fetched_digest = NULL;
2182 OSSL_LIB_CTX *save_libctx;
2184 save_libctx = OSSL_LIB_CTX_set0_default(libctx);
2186 if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
2187 t->err = "INTERNAL_ERROR";
2190 if (expected->pbe_type == PBE_TYPE_PBKDF2) {
2191 if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
2192 expected->salt, expected->salt_len,
2193 expected->iter, expected->md,
2194 expected->key_len, key) == 0) {
2195 t->err = "PBKDF2_ERROR";
2198 #ifndef OPENSSL_NO_SCRYPT
2199 } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
2200 if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
2201 expected->salt, expected->salt_len,
2202 expected->N, expected->r, expected->p,
2203 expected->maxmem, key, expected->key_len) == 0) {
2204 t->err = "SCRYPT_ERROR";
2208 } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
2209 fetched_digest = EVP_MD_fetch(libctx, EVP_MD_get0_name(expected->md),
2211 if (fetched_digest == NULL) {
2212 t->err = "PKCS12_ERROR";
2215 if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
2216 expected->salt, expected->salt_len,
2217 expected->id, expected->iter, expected->key_len,
2218 key, fetched_digest) == 0) {
2219 t->err = "PKCS12_ERROR";
2223 if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len,
2224 key, expected->key_len))
2229 EVP_MD_free(fetched_digest);
2231 OSSL_LIB_CTX_set0_default(save_libctx);
2235 static const EVP_TEST_METHOD pbe_test_method = {
2249 BASE64_CANONICAL_ENCODING = 0,
2250 BASE64_VALID_ENCODING = 1,
2251 BASE64_INVALID_ENCODING = 2
2252 } base64_encoding_type;
2254 typedef struct encode_data_st {
2255 /* Input to encoding */
2256 unsigned char *input;
2258 /* Expected output */
2259 unsigned char *output;
2261 base64_encoding_type encoding;
2264 static int encode_test_init(EVP_TEST *t, const char *encoding)
2268 if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
2270 if (strcmp(encoding, "canonical") == 0) {
2271 edata->encoding = BASE64_CANONICAL_ENCODING;
2272 } else if (strcmp(encoding, "valid") == 0) {
2273 edata->encoding = BASE64_VALID_ENCODING;
2274 } else if (strcmp(encoding, "invalid") == 0) {
2275 edata->encoding = BASE64_INVALID_ENCODING;
2276 if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
2279 TEST_error("Bad encoding: %s."
2280 " Should be one of {canonical, valid, invalid}",
2287 OPENSSL_free(edata);
2291 static void encode_test_cleanup(EVP_TEST *t)
2293 ENCODE_DATA *edata = t->data;
2295 OPENSSL_free(edata->input);
2296 OPENSSL_free(edata->output);
2297 memset(edata, 0, sizeof(*edata));
2300 static int encode_test_parse(EVP_TEST *t,
2301 const char *keyword, const char *value)
2303 ENCODE_DATA *edata = t->data;
2305 if (strcmp(keyword, "Input") == 0)
2306 return parse_bin(value, &edata->input, &edata->input_len);
2307 if (strcmp(keyword, "Output") == 0)
2308 return parse_bin(value, &edata->output, &edata->output_len);
2312 static int encode_test_run(EVP_TEST *t)
2314 ENCODE_DATA *expected = t->data;
2315 unsigned char *encode_out = NULL, *decode_out = NULL;
2316 int output_len, chunk_len;
2317 EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL;
2319 if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
2320 t->err = "INTERNAL_ERROR";
2324 if (expected->encoding == BASE64_CANONICAL_ENCODING) {
2326 if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
2327 || !TEST_ptr(encode_out =
2328 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
2331 EVP_EncodeInit(encode_ctx);
2332 if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
2333 expected->input, expected->input_len)))
2336 output_len = chunk_len;
2338 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
2339 output_len += chunk_len;
2341 if (!memory_err_compare(t, "BAD_ENCODING",
2342 expected->output, expected->output_len,
2343 encode_out, output_len))
2347 if (!TEST_ptr(decode_out =
2348 OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
2351 EVP_DecodeInit(decode_ctx);
2352 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
2353 expected->output_len) < 0) {
2354 t->err = "DECODE_ERROR";
2357 output_len = chunk_len;
2359 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
2360 t->err = "DECODE_ERROR";
2363 output_len += chunk_len;
2365 if (expected->encoding != BASE64_INVALID_ENCODING
2366 && !memory_err_compare(t, "BAD_DECODING",
2367 expected->input, expected->input_len,
2368 decode_out, output_len)) {
2369 t->err = "BAD_DECODING";
2375 OPENSSL_free(encode_out);
2376 OPENSSL_free(decode_out);
2377 EVP_ENCODE_CTX_free(decode_ctx);
2378 EVP_ENCODE_CTX_free(encode_ctx);
2382 static const EVP_TEST_METHOD encode_test_method = {
2385 encode_test_cleanup,
2394 #define MAX_RAND_REPEATS 15
2396 typedef struct rand_data_pass_st {
2397 unsigned char *entropy;
2398 unsigned char *reseed_entropy;
2399 unsigned char *nonce;
2400 unsigned char *pers;
2401 unsigned char *reseed_addin;
2402 unsigned char *addinA;
2403 unsigned char *addinB;
2404 unsigned char *pr_entropyA;
2405 unsigned char *pr_entropyB;
2406 unsigned char *output;
2407 size_t entropy_len, nonce_len, pers_len, addinA_len, addinB_len,
2408 pr_entropyA_len, pr_entropyB_len, output_len, reseed_entropy_len,
2412 typedef struct rand_data_st {
2413 /* Context for this operation */
2415 EVP_RAND_CTX *parent;
2417 int prediction_resistance;
2419 unsigned int generate_bits;
2423 /* Expected output */
2424 RAND_DATA_PASS data[MAX_RAND_REPEATS];
2427 static int rand_test_init(EVP_TEST *t, const char *name)
2431 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
2432 unsigned int strength = 256;
2434 if (!TEST_ptr(rdata = OPENSSL_zalloc(sizeof(*rdata))))
2437 /* TEST-RAND is available in the FIPS provider but not with "fips=yes" */
2438 rand = EVP_RAND_fetch(libctx, "TEST-RAND", "-fips");
2441 rdata->parent = EVP_RAND_CTX_new(rand, NULL);
2442 EVP_RAND_free(rand);
2443 if (rdata->parent == NULL)
2446 *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength);
2447 if (!EVP_RAND_CTX_set_params(rdata->parent, params))
2450 rand = EVP_RAND_fetch(libctx, name, NULL);
2453 rdata->ctx = EVP_RAND_CTX_new(rand, rdata->parent);
2454 EVP_RAND_free(rand);
2455 if (rdata->ctx == NULL)
2462 EVP_RAND_CTX_free(rdata->parent);
2463 OPENSSL_free(rdata);
2467 static void rand_test_cleanup(EVP_TEST *t)
2469 RAND_DATA *rdata = t->data;
2472 OPENSSL_free(rdata->cipher);
2473 OPENSSL_free(rdata->digest);
2475 for (i = 0; i <= rdata->n; i++) {
2476 OPENSSL_free(rdata->data[i].entropy);
2477 OPENSSL_free(rdata->data[i].reseed_entropy);
2478 OPENSSL_free(rdata->data[i].nonce);
2479 OPENSSL_free(rdata->data[i].pers);
2480 OPENSSL_free(rdata->data[i].reseed_addin);
2481 OPENSSL_free(rdata->data[i].addinA);
2482 OPENSSL_free(rdata->data[i].addinB);
2483 OPENSSL_free(rdata->data[i].pr_entropyA);
2484 OPENSSL_free(rdata->data[i].pr_entropyB);
2485 OPENSSL_free(rdata->data[i].output);
2487 EVP_RAND_CTX_free(rdata->ctx);
2488 EVP_RAND_CTX_free(rdata->parent);
2491 static int rand_test_parse(EVP_TEST *t,
2492 const char *keyword, const char *value)
2494 RAND_DATA *rdata = t->data;
2495 RAND_DATA_PASS *item;
2499 if ((p = strchr(keyword, '.')) != NULL) {
2501 if (n >= MAX_RAND_REPEATS)
2505 item = rdata->data + n;
2506 if (HAS_PREFIX(keyword, "Entropy."))
2507 return parse_bin(value, &item->entropy, &item->entropy_len);
2508 if (HAS_PREFIX(keyword, "ReseedEntropy."))
2509 return parse_bin(value, &item->reseed_entropy,
2510 &item->reseed_entropy_len);
2511 if (HAS_PREFIX(keyword, "Nonce."))
2512 return parse_bin(value, &item->nonce, &item->nonce_len);
2513 if (HAS_PREFIX(keyword, "PersonalisationString."))
2514 return parse_bin(value, &item->pers, &item->pers_len);
2515 if (HAS_PREFIX(keyword, "ReseedAdditionalInput."))
2516 return parse_bin(value, &item->reseed_addin,
2517 &item->reseed_addin_len);
2518 if (HAS_PREFIX(keyword, "AdditionalInputA."))
2519 return parse_bin(value, &item->addinA, &item->addinA_len);
2520 if (HAS_PREFIX(keyword, "AdditionalInputB."))
2521 return parse_bin(value, &item->addinB, &item->addinB_len);
2522 if (HAS_PREFIX(keyword, "EntropyPredictionResistanceA."))
2523 return parse_bin(value, &item->pr_entropyA, &item->pr_entropyA_len);
2524 if (HAS_PREFIX(keyword, "EntropyPredictionResistanceB."))
2525 return parse_bin(value, &item->pr_entropyB, &item->pr_entropyB_len);
2526 if (HAS_PREFIX(keyword, "Output."))
2527 return parse_bin(value, &item->output, &item->output_len);
2529 if (strcmp(keyword, "Cipher") == 0)
2530 return TEST_ptr(rdata->cipher = OPENSSL_strdup(value));
2531 if (strcmp(keyword, "Digest") == 0)
2532 return TEST_ptr(rdata->digest = OPENSSL_strdup(value));
2533 if (strcmp(keyword, "DerivationFunction") == 0) {
2534 rdata->use_df = atoi(value) != 0;
2537 if (strcmp(keyword, "GenerateBits") == 0) {
2538 if ((n = atoi(value)) <= 0 || n % 8 != 0)
2540 rdata->generate_bits = (unsigned int)n;
2543 if (strcmp(keyword, "PredictionResistance") == 0) {
2544 rdata->prediction_resistance = atoi(value) != 0;
2551 static int rand_test_run(EVP_TEST *t)
2553 RAND_DATA *expected = t->data;
2554 RAND_DATA_PASS *item;
2556 size_t got_len = expected->generate_bits / 8;
2557 OSSL_PARAM params[5], *p = params;
2558 int i = -1, ret = 0;
2559 unsigned int strength;
2562 if (!TEST_ptr(got = OPENSSL_malloc(got_len)))
2565 *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &expected->use_df);
2566 if (expected->cipher != NULL)
2567 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
2568 expected->cipher, 0);
2569 if (expected->digest != NULL)
2570 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
2571 expected->digest, 0);
2572 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC, "HMAC", 0);
2573 *p = OSSL_PARAM_construct_end();
2574 if (!TEST_true(EVP_RAND_CTX_set_params(expected->ctx, params)))
2577 strength = EVP_RAND_get_strength(expected->ctx);
2578 for (i = 0; i <= expected->n; i++) {
2579 item = expected->data + i;
2582 z = item->entropy != NULL ? item->entropy : (unsigned char *)"";
2583 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
2584 z, item->entropy_len);
2585 z = item->nonce != NULL ? item->nonce : (unsigned char *)"";
2586 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
2587 z, item->nonce_len);
2588 *p = OSSL_PARAM_construct_end();
2589 if (!TEST_true(EVP_RAND_instantiate(expected->parent, strength,
2590 0, NULL, 0, params)))
2593 z = item->pers != NULL ? item->pers : (unsigned char *)"";
2594 if (!TEST_true(EVP_RAND_instantiate
2595 (expected->ctx, strength,
2596 expected->prediction_resistance, z,
2597 item->pers_len, NULL)))
2600 if (item->reseed_entropy != NULL) {
2601 params[0] = OSSL_PARAM_construct_octet_string
2602 (OSSL_RAND_PARAM_TEST_ENTROPY, item->reseed_entropy,
2603 item->reseed_entropy_len);
2604 params[1] = OSSL_PARAM_construct_end();
2605 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2608 if (!TEST_true(EVP_RAND_reseed
2609 (expected->ctx, expected->prediction_resistance,
2610 NULL, 0, item->reseed_addin,
2611 item->reseed_addin_len)))
2614 if (item->pr_entropyA != NULL) {
2615 params[0] = OSSL_PARAM_construct_octet_string
2616 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyA,
2617 item->pr_entropyA_len);
2618 params[1] = OSSL_PARAM_construct_end();
2619 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2622 if (!TEST_true(EVP_RAND_generate
2623 (expected->ctx, got, got_len,
2624 strength, expected->prediction_resistance,
2625 item->addinA, item->addinA_len)))
2628 if (item->pr_entropyB != NULL) {
2629 params[0] = OSSL_PARAM_construct_octet_string
2630 (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyB,
2631 item->pr_entropyB_len);
2632 params[1] = OSSL_PARAM_construct_end();
2633 if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params)))
2636 if (!TEST_true(EVP_RAND_generate
2637 (expected->ctx, got, got_len,
2638 strength, expected->prediction_resistance,
2639 item->addinB, item->addinB_len)))
2641 if (!TEST_mem_eq(got, got_len, item->output, item->output_len))
2643 if (!TEST_true(EVP_RAND_uninstantiate(expected->ctx))
2644 || !TEST_true(EVP_RAND_uninstantiate(expected->parent))
2645 || !TEST_true(EVP_RAND_verify_zeroization(expected->ctx))
2646 || !TEST_int_eq(EVP_RAND_get_state(expected->ctx),
2647 EVP_RAND_STATE_UNINITIALISED))
2654 if (ret == 0 && i >= 0)
2655 TEST_info("Error in test case %d of %d\n", i, expected->n + 1);
2660 static const EVP_TEST_METHOD rand_test_method = {
2672 typedef struct kdf_data_st {
2673 /* Context for this operation */
2675 /* Expected output */
2676 unsigned char *output;
2678 OSSL_PARAM params[20];
2683 * Perform public key operation setup: lookup key, allocated ctx and call
2684 * the appropriate initialisation function
2686 static int kdf_test_init(EVP_TEST *t, const char *name)
2691 if (is_kdf_disabled(name)) {
2692 TEST_info("skipping, '%s' is disabled", name);
2697 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2699 kdata->p = kdata->params;
2700 *kdata->p = OSSL_PARAM_construct_end();
2702 kdf = EVP_KDF_fetch(libctx, name, NULL);
2704 OPENSSL_free(kdata);
2707 kdata->ctx = EVP_KDF_CTX_new(kdf);
2709 if (kdata->ctx == NULL) {
2710 OPENSSL_free(kdata);
2717 static void kdf_test_cleanup(EVP_TEST *t)
2719 KDF_DATA *kdata = t->data;
2722 for (p = kdata->params; p->key != NULL; p++)
2723 OPENSSL_free(p->data);
2724 OPENSSL_free(kdata->output);
2725 EVP_KDF_CTX_free(kdata->ctx);
2728 static int kdf_test_ctrl(EVP_TEST *t, EVP_KDF_CTX *kctx,
2731 KDF_DATA *kdata = t->data;
2734 const OSSL_PARAM *defs = EVP_KDF_settable_ctx_params(EVP_KDF_CTX_kdf(kctx));
2736 if (!TEST_ptr(name = OPENSSL_strdup(value)))
2738 p = strchr(name, ':');
2742 if (strcmp(name, "r") == 0
2743 && OSSL_PARAM_locate_const(defs, name) == NULL) {
2744 TEST_info("skipping, setting 'r' is unsupported");
2749 rv = OSSL_PARAM_allocate_from_text(kdata->p, defs, name, p,
2750 p != NULL ? strlen(p) : 0, NULL);
2751 *++kdata->p = OSSL_PARAM_construct_end();
2753 t->err = "KDF_PARAM_ERROR";
2757 if (p != NULL && strcmp(name, "digest") == 0) {
2758 if (is_digest_disabled(p)) {
2759 TEST_info("skipping, '%s' is disabled", p);
2765 && (strcmp(name, "cipher") == 0
2766 || strcmp(name, "cekalg") == 0)
2767 && is_cipher_disabled(p)) {
2768 TEST_info("skipping, '%s' is disabled", p);
2773 && (strcmp(name, "mac") == 0)
2774 && is_mac_disabled(p)) {
2775 TEST_info("skipping, '%s' is disabled", p);
2783 static int kdf_test_parse(EVP_TEST *t,
2784 const char *keyword, const char *value)
2786 KDF_DATA *kdata = t->data;
2788 if (strcmp(keyword, "Output") == 0)
2789 return parse_bin(value, &kdata->output, &kdata->output_len);
2790 if (HAS_PREFIX(keyword, "Ctrl"))
2791 return kdf_test_ctrl(t, kdata->ctx, value);
2795 static int kdf_test_run(EVP_TEST *t)
2797 KDF_DATA *expected = t->data;
2798 unsigned char *got = NULL;
2799 size_t got_len = expected->output_len;
2802 if (!EVP_KDF_CTX_set_params(expected->ctx, expected->params)) {
2803 t->err = "KDF_CTRL_ERROR";
2806 if (!TEST_ptr(got = OPENSSL_malloc(got_len == 0 ? 1 : got_len))) {
2807 t->err = "INTERNAL_ERROR";
2810 if ((ctx = EVP_KDF_CTX_dup(expected->ctx)) != NULL) {
2811 EVP_KDF_CTX_free(expected->ctx);
2812 expected->ctx = ctx;
2814 if (EVP_KDF_derive(expected->ctx, got, got_len, NULL) <= 0) {
2815 t->err = "KDF_DERIVE_ERROR";
2818 if (!memory_err_compare(t, "KDF_MISMATCH",
2819 expected->output, expected->output_len,
2830 static const EVP_TEST_METHOD kdf_test_method = {
2842 typedef struct pkey_kdf_data_st {
2843 /* Context for this operation */
2845 /* Expected output */
2846 unsigned char *output;
2851 * Perform public key operation setup: lookup key, allocated ctx and call
2852 * the appropriate initialisation function
2854 static int pkey_kdf_test_init(EVP_TEST *t, const char *name)
2856 PKEY_KDF_DATA *kdata = NULL;
2858 if (is_kdf_disabled(name)) {
2859 TEST_info("skipping, '%s' is disabled", name);
2864 if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
2867 kdata->ctx = EVP_PKEY_CTX_new_from_name(libctx, name, NULL);
2868 if (kdata->ctx == NULL
2869 || EVP_PKEY_derive_init(kdata->ctx) <= 0)
2875 EVP_PKEY_CTX_free(kdata->ctx);
2876 OPENSSL_free(kdata);
2880 static void pkey_kdf_test_cleanup(EVP_TEST *t)
2882 PKEY_KDF_DATA *kdata = t->data;
2884 OPENSSL_free(kdata->output);
2885 EVP_PKEY_CTX_free(kdata->ctx);
2888 static int pkey_kdf_test_parse(EVP_TEST *t,
2889 const char *keyword, const char *value)
2891 PKEY_KDF_DATA *kdata = t->data;
2893 if (strcmp(keyword, "Output") == 0)
2894 return parse_bin(value, &kdata->output, &kdata->output_len);
2895 if (HAS_PREFIX(keyword, "Ctrl"))
2896 return pkey_test_ctrl(t, kdata->ctx, value);
2900 static int pkey_kdf_test_run(EVP_TEST *t)
2902 PKEY_KDF_DATA *expected = t->data;
2903 unsigned char *got = NULL;
2906 /* Find out the KDF output size */
2907 if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) {
2908 t->err = "INTERNAL_ERROR";
2913 * We may get an absurd output size, which signals that anything goes.
2914 * If not, we specify a too big buffer for the output, to test that
2915 * EVP_PKEY_derive() can cope with it.
2917 if (got_len == SIZE_MAX || got_len == 0)
2918 got_len = expected->output_len;
2920 got_len = expected->output_len * 2;
2922 if (!TEST_ptr(got = OPENSSL_malloc(got_len == 0 ? 1 : got_len))) {
2923 t->err = "INTERNAL_ERROR";
2926 if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
2927 t->err = "KDF_DERIVE_ERROR";
2930 if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) {
2931 t->err = "KDF_MISMATCH";
2941 static const EVP_TEST_METHOD pkey_kdf_test_method = {
2944 pkey_kdf_test_cleanup,
2945 pkey_kdf_test_parse,
2953 typedef struct keypair_test_data_st {
2956 } KEYPAIR_TEST_DATA;
2958 static int keypair_test_init(EVP_TEST *t, const char *pair)
2960 KEYPAIR_TEST_DATA *data;
2962 EVP_PKEY *pk = NULL, *pubk = NULL;
2963 char *pub, *priv = NULL;
2965 /* Split private and public names. */
2966 if (!TEST_ptr(priv = OPENSSL_strdup(pair))
2967 || !TEST_ptr(pub = strchr(priv, ':'))) {
2968 t->err = "PARSING_ERROR";
2973 if (!TEST_true(find_key(&pk, priv, private_keys))) {
2974 TEST_info("Can't find private key: %s", priv);
2975 t->err = "MISSING_PRIVATE_KEY";
2978 if (!TEST_true(find_key(&pubk, pub, public_keys))) {
2979 TEST_info("Can't find public key: %s", pub);
2980 t->err = "MISSING_PUBLIC_KEY";
2984 if (pk == NULL && pubk == NULL) {
2985 /* Both keys are listed but unsupported: skip this test */
2991 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
3004 static void keypair_test_cleanup(EVP_TEST *t)
3006 OPENSSL_free(t->data);
3011 * For tests that do not accept any custom keywords.
3013 static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
3018 static int keypair_test_run(EVP_TEST *t)
3021 const KEYPAIR_TEST_DATA *pair = t->data;
3023 if (pair->privk == NULL || pair->pubk == NULL) {
3025 * this can only happen if only one of the keys is not set
3026 * which means that one of them was unsupported while the
3027 * other isn't: hence a key type mismatch.
3029 t->err = "KEYPAIR_TYPE_MISMATCH";
3034 if ((rv = EVP_PKEY_eq(pair->privk, pair->pubk)) != 1) {
3036 t->err = "KEYPAIR_MISMATCH";
3037 } else if (-1 == rv) {
3038 t->err = "KEYPAIR_TYPE_MISMATCH";
3039 } else if (-2 == rv) {
3040 t->err = "UNSUPPORTED_KEY_COMPARISON";
3042 TEST_error("Unexpected error in key comparison");
3057 static const EVP_TEST_METHOD keypair_test_method = {
3060 keypair_test_cleanup,
3069 typedef struct keygen_test_data_st {
3070 EVP_PKEY_CTX *genctx; /* Keygen context to use */
3071 char *keyname; /* Key name to store key or NULL */
3074 static int keygen_test_init(EVP_TEST *t, const char *alg)
3076 KEYGEN_TEST_DATA *data;
3077 EVP_PKEY_CTX *genctx;
3078 int nid = OBJ_sn2nid(alg);
3080 if (nid == NID_undef) {
3081 nid = OBJ_ln2nid(alg);
3082 if (nid == NID_undef)
3086 if (is_pkey_disabled(alg)) {
3090 if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_from_name(libctx, alg, NULL)))
3093 if (EVP_PKEY_keygen_init(genctx) <= 0) {
3094 t->err = "KEYGEN_INIT_ERROR";
3098 if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
3100 data->genctx = genctx;
3101 data->keyname = NULL;
3107 EVP_PKEY_CTX_free(genctx);
3111 static void keygen_test_cleanup(EVP_TEST *t)
3113 KEYGEN_TEST_DATA *keygen = t->data;
3115 EVP_PKEY_CTX_free(keygen->genctx);
3116 OPENSSL_free(keygen->keyname);
3117 OPENSSL_free(t->data);
3121 static int keygen_test_parse(EVP_TEST *t,
3122 const char *keyword, const char *value)
3124 KEYGEN_TEST_DATA *keygen = t->data;
3126 if (strcmp(keyword, "KeyName") == 0)
3127 return TEST_ptr(keygen->keyname = OPENSSL_strdup(value));
3128 if (strcmp(keyword, "Ctrl") == 0)
3129 return pkey_test_ctrl(t, keygen->genctx, value);
3133 static int keygen_test_run(EVP_TEST *t)
3135 KEYGEN_TEST_DATA *keygen = t->data;
3136 EVP_PKEY *pkey = NULL;
3139 if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) {
3140 t->err = "KEYGEN_GENERATE_ERROR";
3144 if (!evp_pkey_is_provided(pkey)) {
3145 TEST_info("Warning: legacy key generated %s", keygen->keyname);
3148 if (keygen->keyname != NULL) {
3152 if (find_key(NULL, keygen->keyname, private_keys)) {
3153 TEST_info("Duplicate key %s", keygen->keyname);
3157 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
3159 key->name = keygen->keyname;
3160 keygen->keyname = NULL;
3162 key->next = private_keys;
3166 EVP_PKEY_free(pkey);
3175 static const EVP_TEST_METHOD keygen_test_method = {
3178 keygen_test_cleanup,
3184 ** DIGEST SIGN+VERIFY TESTS
3188 int is_verify; /* Set to 1 if verifying */
3189 int is_oneshot; /* Set to 1 for one shot operation */
3190 const EVP_MD *md; /* Digest to use */
3191 EVP_MD_CTX *ctx; /* Digest context */
3193 STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
3194 unsigned char *osin; /* Input data if one shot */
3195 size_t osin_len; /* Input length data if one shot */
3196 unsigned char *output; /* Expected output */
3197 size_t output_len; /* Expected output length */
3200 static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
3203 const EVP_MD *md = NULL;
3204 DIGESTSIGN_DATA *mdat;
3206 if (strcmp(alg, "NULL") != 0) {
3207 if (is_digest_disabled(alg)) {
3211 md = EVP_get_digestbyname(alg);
3215 if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
3218 if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
3222 mdat->is_verify = is_verify;
3223 mdat->is_oneshot = is_oneshot;
3228 static int digestsign_test_init(EVP_TEST *t, const char *alg)
3230 return digestsigver_test_init(t, alg, 0, 0);
3233 static void digestsigver_test_cleanup(EVP_TEST *t)
3235 DIGESTSIGN_DATA *mdata = t->data;
3237 EVP_MD_CTX_free(mdata->ctx);
3238 sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
3239 OPENSSL_free(mdata->osin);
3240 OPENSSL_free(mdata->output);
3241 OPENSSL_free(mdata);
3245 static int digestsigver_test_parse(EVP_TEST *t,
3246 const char *keyword, const char *value)
3248 DIGESTSIGN_DATA *mdata = t->data;
3250 if (strcmp(keyword, "Key") == 0) {
3251 EVP_PKEY *pkey = NULL;
3253 const char *name = mdata->md == NULL ? NULL : EVP_MD_get0_name(mdata->md);
3255 if (mdata->is_verify)
3256 rv = find_key(&pkey, value, public_keys);
3258 rv = find_key(&pkey, value, private_keys);
3259 if (rv == 0 || pkey == NULL) {
3263 if (mdata->is_verify) {
3264 if (!EVP_DigestVerifyInit_ex(mdata->ctx, &mdata->pctx, name, libctx,
3266 t->err = "DIGESTVERIFYINIT_ERROR";
3269 if (!EVP_DigestSignInit_ex(mdata->ctx, &mdata->pctx, name, libctx, NULL,
3271 t->err = "DIGESTSIGNINIT_ERROR";
3275 if (strcmp(keyword, "Input") == 0) {
3276 if (mdata->is_oneshot)
3277 return parse_bin(value, &mdata->osin, &mdata->osin_len);
3278 return evp_test_buffer_append(value, &mdata->input);
3280 if (strcmp(keyword, "Output") == 0)
3281 return parse_bin(value, &mdata->output, &mdata->output_len);
3283 if (!mdata->is_oneshot) {
3284 if (strcmp(keyword, "Count") == 0)
3285 return evp_test_buffer_set_count(value, mdata->input);
3286 if (strcmp(keyword, "Ncopy") == 0)
3287 return evp_test_buffer_ncopy(value, mdata->input);
3289 if (strcmp(keyword, "Ctrl") == 0) {
3290 if (mdata->pctx == NULL)
3292 return pkey_test_ctrl(t, mdata->pctx, value);
3297 static int digestsign_update_fn(void *ctx, const unsigned char *buf,
3300 return EVP_DigestSignUpdate(ctx, buf, buflen);
3303 static int digestsign_test_run(EVP_TEST *t)
3305 DIGESTSIGN_DATA *expected = t->data;
3306 unsigned char *got = NULL;
3309 if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
3311 t->err = "DIGESTUPDATE_ERROR";
3315 if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
3316 t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
3319 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
3320 t->err = "MALLOC_FAILURE";
3323 if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
3324 t->err = "DIGESTSIGNFINAL_ERROR";
3327 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
3328 expected->output, expected->output_len,
3338 static const EVP_TEST_METHOD digestsign_test_method = {
3340 digestsign_test_init,
3341 digestsigver_test_cleanup,
3342 digestsigver_test_parse,
3346 static int digestverify_test_init(EVP_TEST *t, const char *alg)
3348 return digestsigver_test_init(t, alg, 1, 0);
3351 static int digestverify_update_fn(void *ctx, const unsigned char *buf,
3354 return EVP_DigestVerifyUpdate(ctx, buf, buflen);
3357 static int digestverify_test_run(EVP_TEST *t)
3359 DIGESTSIGN_DATA *mdata = t->data;
3361 if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
3362 t->err = "DIGESTUPDATE_ERROR";
3366 if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
3367 mdata->output_len) <= 0)
3368 t->err = "VERIFY_ERROR";
3372 static const EVP_TEST_METHOD digestverify_test_method = {
3374 digestverify_test_init,
3375 digestsigver_test_cleanup,
3376 digestsigver_test_parse,
3377 digestverify_test_run
3380 static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
3382 return digestsigver_test_init(t, alg, 0, 1);
3385 static int oneshot_digestsign_test_run(EVP_TEST *t)
3387 DIGESTSIGN_DATA *expected = t->data;
3388 unsigned char *got = NULL;
3391 if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
3392 expected->osin, expected->osin_len)) {
3393 t->err = "DIGESTSIGN_LENGTH_ERROR";
3396 if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
3397 t->err = "MALLOC_FAILURE";
3400 if (!EVP_DigestSign(expected->ctx, got, &got_len,
3401 expected->osin, expected->osin_len)) {
3402 t->err = "DIGESTSIGN_ERROR";
3405 if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
3406 expected->output, expected->output_len,
3416 static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
3417 "OneShotDigestSign",
3418 oneshot_digestsign_test_init,
3419 digestsigver_test_cleanup,
3420 digestsigver_test_parse,
3421 oneshot_digestsign_test_run
3424 static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
3426 return digestsigver_test_init(t, alg, 1, 1);
3429 static int oneshot_digestverify_test_run(EVP_TEST *t)
3431 DIGESTSIGN_DATA *mdata = t->data;
3433 if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
3434 mdata->osin, mdata->osin_len) <= 0)
3435 t->err = "VERIFY_ERROR";
3439 static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
3440 "OneShotDigestVerify",
3441 oneshot_digestverify_test_init,
3442 digestsigver_test_cleanup,
3443 digestsigver_test_parse,
3444 oneshot_digestverify_test_run
3449 ** PARSING AND DISPATCH
3452 static const EVP_TEST_METHOD *evp_test_list[] = {
3454 &cipher_test_method,
3455 &digest_test_method,
3456 &digestsign_test_method,
3457 &digestverify_test_method,
3458 &encode_test_method,
3460 &pkey_kdf_test_method,
3461 &keypair_test_method,
3462 &keygen_test_method,
3464 &oneshot_digestsign_test_method,
3465 &oneshot_digestverify_test_method,
3467 &pdecrypt_test_method,
3468 &pderive_test_method,
3470 &pverify_recover_test_method,
3471 &pverify_test_method,
3475 static const EVP_TEST_METHOD *find_test(const char *name)
3477 const EVP_TEST_METHOD **tt;
3479 for (tt = evp_test_list; *tt; tt++) {
3480 if (strcmp(name, (*tt)->name) == 0)
3486 static void clear_test(EVP_TEST *t)
3488 test_clearstanza(&t->s);
3490 if (t->data != NULL) {
3491 if (t->meth != NULL)
3492 t->meth->cleanup(t);
3493 OPENSSL_free(t->data);
3496 OPENSSL_free(t->expected_err);
3497 t->expected_err = NULL;
3498 OPENSSL_free(t->reason);
3507 /* Check for errors in the test structure; return 1 if okay, else 0. */
3508 static int check_test_error(EVP_TEST *t)
3513 if (t->err == NULL && t->expected_err == NULL)
3515 if (t->err != NULL && t->expected_err == NULL) {
3516 if (t->aux_err != NULL) {
3517 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
3518 t->s.test_file, t->s.start, t->aux_err, t->err);
3520 TEST_info("%s:%d: Source of above error; unexpected error %s",
3521 t->s.test_file, t->s.start, t->err);
3525 if (t->err == NULL && t->expected_err != NULL) {
3526 TEST_info("%s:%d: Succeeded but was expecting %s",
3527 t->s.test_file, t->s.start, t->expected_err);
3531 if (strcmp(t->err, t->expected_err) != 0) {
3532 TEST_info("%s:%d: Expected %s got %s",
3533 t->s.test_file, t->s.start, t->expected_err, t->err);
3537 if (t->reason == NULL)
3540 if (t->reason == NULL) {
3541 TEST_info("%s:%d: Test is missing function or reason code",
3542 t->s.test_file, t->s.start);
3546 err = ERR_peek_error();
3548 TEST_info("%s:%d: Expected error \"%s\" not set",
3549 t->s.test_file, t->s.start, t->reason);
3553 reason = ERR_reason_error_string(err);
3554 if (reason == NULL) {
3555 TEST_info("%s:%d: Expected error \"%s\", no strings available."
3557 t->s.test_file, t->s.start, t->reason);
3561 if (strcmp(reason, t->reason) == 0)
3564 TEST_info("%s:%d: Expected error \"%s\", got \"%s\"",
3565 t->s.test_file, t->s.start, t->reason, reason);
3570 /* Run a parsed test. Log a message and return 0 on error. */
3571 static int run_test(EVP_TEST *t)
3573 if (t->meth == NULL)
3580 if (t->err == NULL && t->meth->run_test(t) != 1) {
3581 TEST_info("%s:%d %s error",
3582 t->s.test_file, t->s.start, t->meth->name);
3585 if (!check_test_error(t)) {
3586 TEST_openssl_errors();
3595 static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
3597 for (; lst != NULL; lst = lst->next) {
3598 if (strcmp(lst->name, name) == 0) {
3607 static void free_key_list(KEY_LIST *lst)
3609 while (lst != NULL) {
3610 KEY_LIST *next = lst->next;
3612 EVP_PKEY_free(lst->key);
3613 OPENSSL_free(lst->name);
3620 * Is the key type an unsupported algorithm?
3622 static int key_unsupported(void)
3624 long err = ERR_peek_last_error();
3625 int lib = ERR_GET_LIB(err);
3626 long reason = ERR_GET_REASON(err);
3628 if ((lib == ERR_LIB_EVP && reason == EVP_R_UNSUPPORTED_ALGORITHM)
3629 || (lib == ERR_LIB_EVP && reason == EVP_R_DECODE_ERROR)
3630 || reason == ERR_R_UNSUPPORTED) {
3634 #ifndef OPENSSL_NO_EC
3636 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
3637 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
3640 if (lib == ERR_LIB_EC
3641 && (reason == EC_R_UNKNOWN_GROUP
3642 || reason == EC_R_INVALID_CURVE)) {
3646 #endif /* OPENSSL_NO_EC */
3650 /* NULL out the value from |pp| but return it. This "steals" a pointer. */
3651 static char *take_value(PAIR *pp)
3653 char *p = pp->value;
3659 #if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
3660 static int securitycheck_enabled(void)
3662 static int enabled = -1;
3664 if (enabled == -1) {
3665 if (OSSL_PROVIDER_available(libctx, "fips")) {
3666 OSSL_PARAM params[2];
3667 OSSL_PROVIDER *prov = NULL;
3670 prov = OSSL_PROVIDER_load(libctx, "fips");
3673 OSSL_PARAM_construct_int(OSSL_PROV_PARAM_SECURITY_CHECKS,
3675 params[1] = OSSL_PARAM_construct_end();
3676 OSSL_PROVIDER_get_params(prov, params);
3677 OSSL_PROVIDER_unload(prov);
3689 * Return 1 if one of the providers named in the string is available.
3690 * The provider names are separated with whitespace.
3691 * NOTE: destructive function, it inserts '\0' after each provider name.
3693 static int prov_available(char *providers)
3699 for (; isspace(*providers); providers++)
3701 if (*providers == '\0')
3702 break; /* End of the road */
3703 for (p = providers; *p != '\0' && !isspace(*p); p++)
3709 if (OSSL_PROVIDER_available(libctx, providers))
3710 return 1; /* Found one */
3715 /* Read and parse one test. Return 0 if failure, 1 if okay. */
3716 static int parse(EVP_TEST *t)
3718 KEY_LIST *key, **klist;
3721 int i, skip_availablein = 0;
3725 if (BIO_eof(t->s.fp))
3728 if (!test_readstanza(&t->s))
3730 } while (t->s.numpairs == 0);
3731 pp = &t->s.pairs[0];
3733 /* Are we adding a key? */
3737 if (strcmp(pp->key, "PrivateKey") == 0) {
3738 pkey = PEM_read_bio_PrivateKey_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3739 if (pkey == NULL && !key_unsupported()) {
3740 EVP_PKEY_free(pkey);
3741 TEST_info("Can't read private key %s", pp->value);
3742 TEST_openssl_errors();
3745 klist = &private_keys;
3746 } else if (strcmp(pp->key, "PublicKey") == 0) {
3747 pkey = PEM_read_bio_PUBKEY_ex(t->s.key, NULL, 0, NULL, libctx, NULL);
3748 if (pkey == NULL && !key_unsupported()) {
3749 EVP_PKEY_free(pkey);
3750 TEST_info("Can't read public key %s", pp->value);
3751 TEST_openssl_errors();
3754 klist = &public_keys;
3755 } else if (strcmp(pp->key, "PrivateKeyRaw") == 0
3756 || strcmp(pp->key, "PublicKeyRaw") == 0) {
3757 char *strnid = NULL, *keydata = NULL;
3758 unsigned char *keybin;
3762 if (strcmp(pp->key, "PrivateKeyRaw") == 0)
3763 klist = &private_keys;
3765 klist = &public_keys;
3767 strnid = strchr(pp->value, ':');
3768 if (strnid != NULL) {
3770 keydata = strchr(strnid, ':');
3771 if (keydata != NULL)
3774 if (keydata == NULL) {
3775 TEST_info("Failed to parse %s value", pp->key);
3779 nid = OBJ_txt2nid(strnid);
3780 if (nid == NID_undef) {
3781 TEST_info("Unrecognised algorithm NID");
3784 if (!parse_bin(keydata, &keybin, &keylen)) {
3785 TEST_info("Failed to create binary key");
3788 if (klist == &private_keys)
3789 pkey = EVP_PKEY_new_raw_private_key_ex(libctx, strnid, NULL, keybin,
3792 pkey = EVP_PKEY_new_raw_public_key_ex(libctx, strnid, NULL, keybin,
3794 if (pkey == NULL && !key_unsupported()) {
3795 TEST_info("Can't read %s data", pp->key);
3796 OPENSSL_free(keybin);
3797 TEST_openssl_errors();
3800 OPENSSL_free(keybin);
3801 } else if (strcmp(pp->key, "Availablein") == 0) {
3802 if (!prov_available(pp->value)) {
3803 TEST_info("skipping, '%s' provider not available: %s:%d",
3804 pp->value, t->s.test_file, t->s.start);
3813 /* If we have a key add to list */
3814 if (klist != NULL) {
3815 if (find_key(NULL, pp->value, *klist)) {
3816 TEST_info("Duplicate key %s", pp->value);
3819 if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
3821 key->name = take_value(pp);
3826 /* Go back and start a new stanza. */
3827 if ((t->s.numpairs - skip_availablein) != 1)
3828 TEST_info("Line %d: missing blank line\n", t->s.curr);
3832 /* Find the test, based on first keyword. */
3833 if (!TEST_ptr(t->meth = find_test(pp->key)))
3835 if (!t->meth->init(t, pp->value)) {
3836 TEST_error("unknown %s: %s\n", pp->key, pp->value);
3840 /* TEST_info("skipping %s %s", pp->key, pp->value); */
3844 for (pp++, i = 1; i < (t->s.numpairs - skip_availablein); pp++, i++) {
3845 if (strcmp(pp->key, "Securitycheck") == 0) {
3846 #if defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
3848 if (!securitycheck_enabled())
3851 TEST_info("skipping, Securitycheck is disabled: %s:%d",
3852 t->s.test_file, t->s.start);
3856 } else if (strcmp(pp->key, "Availablein") == 0) {
3857 TEST_info("Line %d: 'Availablein' should be the first option",
3860 } else if (strcmp(pp->key, "Result") == 0) {
3861 if (t->expected_err != NULL) {
3862 TEST_info("Line %d: multiple result lines", t->s.curr);
3865 t->expected_err = take_value(pp);
3866 } else if (strcmp(pp->key, "Function") == 0) {
3867 /* Ignore old line. */
3868 } else if (strcmp(pp->key, "Reason") == 0) {
3869 if (t->reason != NULL) {
3870 TEST_info("Line %d: multiple reason lines", t->s.curr);
3873 t->reason = take_value(pp);
3875 /* Must be test specific line: try to parse it */
3876 int rv = t->meth->parse(t, pp->key, pp->value);
3879 TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key);
3883 TEST_info("Line %d: error processing keyword %s = %s\n",
3884 t->s.curr, pp->key, pp->value);
3893 static int run_file_tests(int i)
3896 const char *testfile = test_get_argument(i);
3899 if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t))))
3901 if (!test_start_file(&t->s, testfile)) {
3906 while (!BIO_eof(t->s.fp)) {
3912 if (c == 0 || !run_test(t)) {
3917 test_end_file(&t->s);
3920 free_key_list(public_keys);
3921 free_key_list(private_keys);
3928 const OPTIONS *test_get_options(void)
3930 static const OPTIONS test_options[] = {
3931 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
3932 { "config", OPT_CONFIG_FILE, '<',
3933 "The configuration file to use for the libctx" },
3934 { OPT_HELP_STR, 1, '-', "file\tFile to run tests on.\n" },
3937 return test_options;
3940 int setup_tests(void)
3943 char *config_file = NULL;
3947 while ((o = opt_next()) != OPT_EOF) {
3949 case OPT_CONFIG_FILE:
3950 config_file = opt_arg();
3952 case OPT_TEST_CASES:
3961 * Load the provider via configuration into the created library context.
3962 * Load the 'null' provider into the default library context to ensure that
3963 * the tests do not fallback to using the default provider.
3965 if (!test_get_libctx(&libctx, &prov_null, config_file, NULL, NULL))
3968 n = test_get_argument_count();
3972 ADD_ALL_TESTS(run_file_tests, n);
3976 void cleanup_tests(void)
3978 OSSL_PROVIDER_unload(prov_null);
3979 OSSL_LIB_CTX_free(libctx);
3982 static int is_digest_disabled(const char *name)
3984 #ifdef OPENSSL_NO_BLAKE2
3985 if (HAS_CASE_PREFIX(name, "BLAKE"))
3988 #ifdef OPENSSL_NO_MD2
3989 if (OPENSSL_strcasecmp(name, "MD2") == 0)
3992 #ifdef OPENSSL_NO_MDC2
3993 if (OPENSSL_strcasecmp(name, "MDC2") == 0)
3996 #ifdef OPENSSL_NO_MD4
3997 if (OPENSSL_strcasecmp(name, "MD4") == 0)
4000 #ifdef OPENSSL_NO_MD5
4001 if (OPENSSL_strcasecmp(name, "MD5") == 0)
4004 #ifdef OPENSSL_NO_RMD160
4005 if (OPENSSL_strcasecmp(name, "RIPEMD160") == 0)
4008 #ifdef OPENSSL_NO_SM3
4009 if (OPENSSL_strcasecmp(name, "SM3") == 0)
4012 #ifdef OPENSSL_NO_WHIRLPOOL
4013 if (OPENSSL_strcasecmp(name, "WHIRLPOOL") == 0)
4019 static int is_pkey_disabled(const char *name)
4021 #ifdef OPENSSL_NO_EC
4022 if (HAS_CASE_PREFIX(name, "EC"))
4025 #ifdef OPENSSL_NO_DH
4026 if (HAS_CASE_PREFIX(name, "DH"))
4029 #ifdef OPENSSL_NO_DSA
4030 if (HAS_CASE_PREFIX(name, "DSA"))
4036 static int is_mac_disabled(const char *name)
4038 #ifdef OPENSSL_NO_BLAKE2
4039 if (HAS_CASE_PREFIX(name, "BLAKE2BMAC")
4040 || HAS_CASE_PREFIX(name, "BLAKE2SMAC"))
4043 #ifdef OPENSSL_NO_CMAC
4044 if (HAS_CASE_PREFIX(name, "CMAC"))
4047 #ifdef OPENSSL_NO_POLY1305
4048 if (HAS_CASE_PREFIX(name, "Poly1305"))
4051 #ifdef OPENSSL_NO_SIPHASH
4052 if (HAS_CASE_PREFIX(name, "SipHash"))
4057 static int is_kdf_disabled(const char *name)
4059 #ifdef OPENSSL_NO_SCRYPT
4060 if (HAS_CASE_SUFFIX(name, "SCRYPT"))
4066 static int is_cipher_disabled(const char *name)
4068 #ifdef OPENSSL_NO_ARIA
4069 if (HAS_CASE_PREFIX(name, "ARIA"))
4072 #ifdef OPENSSL_NO_BF
4073 if (HAS_CASE_PREFIX(name, "BF"))
4076 #ifdef OPENSSL_NO_CAMELLIA
4077 if (HAS_CASE_PREFIX(name, "CAMELLIA"))
4080 #ifdef OPENSSL_NO_CAST
4081 if (HAS_CASE_PREFIX(name, "CAST"))
4084 #ifdef OPENSSL_NO_CHACHA
4085 if (HAS_CASE_PREFIX(name, "CHACHA"))
4088 #ifdef OPENSSL_NO_POLY1305
4089 if (HAS_CASE_SUFFIX(name, "Poly1305"))
4092 #ifdef OPENSSL_NO_DES
4093 if (HAS_CASE_PREFIX(name, "DES"))
4095 if (HAS_CASE_SUFFIX(name, "3DESwrap"))
4098 #ifdef OPENSSL_NO_OCB
4099 if (HAS_CASE_SUFFIX(name, "OCB"))
4102 #ifdef OPENSSL_NO_IDEA
4103 if (HAS_CASE_PREFIX(name, "IDEA"))
4106 #ifdef OPENSSL_NO_RC2
4107 if (HAS_CASE_PREFIX(name, "RC2"))
4110 #ifdef OPENSSL_NO_RC4
4111 if (HAS_CASE_PREFIX(name, "RC4"))
4114 #ifdef OPENSSL_NO_RC5
4115 if (HAS_CASE_PREFIX(name, "RC5"))
4118 #ifdef OPENSSL_NO_SEED
4119 if (HAS_CASE_PREFIX(name, "SEED"))
4122 #ifdef OPENSSL_NO_SIV
4123 if (HAS_CASE_SUFFIX(name, "SIV"))
4126 #ifdef OPENSSL_NO_SM4
4127 if (HAS_CASE_PREFIX(name, "SM4"))