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 and strncasecmp */
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 test_duplicate_md_ctx(EVP_TEST
*t
, EVP_MD_CTX
*mctx
)
401 char dont
[] = "touch";
405 if (!EVP_DigestFinalXOF(mctx
, (unsigned char *)dont
, 0)) {
406 EVP_MD_CTX_free(mctx
);
407 t
->err
= "DIGESTFINALXOF_ERROR";
410 if (!TEST_str_eq(dont
, "touch")) {
411 EVP_MD_CTX_free(mctx
);
412 t
->err
= "DIGESTFINALXOF_ERROR";
415 EVP_MD_CTX_free(mctx
);
419 static int digest_test_run(EVP_TEST
*t
)
421 DIGEST_DATA
*expected
= t
->data
;
422 EVP_TEST_BUFFER
*inbuf
;
424 unsigned char *got
= NULL
;
425 unsigned int got_len
;
428 OSSL_PARAM params
[2];
430 printf("test %s (%d %d)\n", t
->name
, t
->s
.start
, t
->s
.curr
);
431 t
->err
= "TEST_FAILURE";
432 if (!TEST_ptr(mctx
= EVP_MD_CTX_new()))
435 got
= OPENSSL_malloc(expected
->output_len
> EVP_MAX_MD_SIZE
?
436 expected
->output_len
: EVP_MAX_MD_SIZE
);
440 if (!EVP_DigestInit_ex(mctx
, expected
->digest
, NULL
)) {
441 t
->err
= "DIGESTINIT_ERROR";
444 if (expected
->pad_type
> 0) {
445 params
[0] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_PAD_TYPE
,
446 &expected
->pad_type
);
447 params
[1] = OSSL_PARAM_construct_end();
448 if (!TEST_int_gt(EVP_MD_CTX_set_params(mctx
, params
), 0)) {
449 t
->err
= "PARAMS_ERROR";
453 if (!evp_test_buffer_do(expected
->input
, digest_update_fn
, mctx
)) {
454 t
->err
= "DIGESTUPDATE_ERROR";
458 xof
= (EVP_MD_get_flags(expected
->digest
) & EVP_MD_FLAG_XOF
) != 0;
460 EVP_MD_CTX
*mctx_cpy
;
462 if (!TEST_ptr(mctx_cpy
= EVP_MD_CTX_new())) {
465 if (!TEST_true(EVP_MD_CTX_copy(mctx_cpy
, mctx
))) {
466 EVP_MD_CTX_free(mctx_cpy
);
468 } else if (!test_duplicate_md_ctx(t
, mctx_cpy
)) {
472 if (!test_duplicate_md_ctx(t
, EVP_MD_CTX_dup(mctx
)))
475 got_len
= expected
->output_len
;
476 if (!EVP_DigestFinalXOF(mctx
, got
, got_len
)) {
477 t
->err
= "DIGESTFINALXOF_ERROR";
481 if (!EVP_DigestFinal(mctx
, got
, &got_len
)) {
482 t
->err
= "DIGESTFINAL_ERROR";
486 if (!TEST_int_eq(expected
->output_len
, got_len
)) {
487 t
->err
= "DIGEST_LENGTH_MISMATCH";
490 if (!memory_err_compare(t
, "DIGEST_MISMATCH",
491 expected
->output
, expected
->output_len
,
497 /* Test the EVP_Q_digest interface as well */
498 if (sk_EVP_TEST_BUFFER_num(expected
->input
) == 1
500 /* This should never fail but we need the returned pointer now */
501 && !TEST_ptr(inbuf
= sk_EVP_TEST_BUFFER_value(expected
->input
, 0))
502 && !inbuf
->count_set
) {
503 OPENSSL_cleanse(got
, got_len
);
504 if (!TEST_true(EVP_Q_digest(libctx
,
505 EVP_MD_get0_name(expected
->fetched_digest
),
506 NULL
, inbuf
->buf
, inbuf
->buflen
,
508 || !TEST_mem_eq(got
, size
,
509 expected
->output
, expected
->output_len
)) {
510 t
->err
= "EVP_Q_digest failed";
517 EVP_MD_CTX_free(mctx
);
521 static const EVP_TEST_METHOD digest_test_method
= {
533 typedef struct cipher_data_st
{
534 const EVP_CIPHER
*cipher
;
535 EVP_CIPHER
*fetched_cipher
;
537 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
541 size_t key_bits
; /* Used by RC2 */
543 unsigned char *next_iv
; /* Expected IV state after operation */
546 unsigned char *plaintext
;
547 size_t plaintext_len
;
548 unsigned char *ciphertext
;
549 size_t ciphertext_len
;
550 /* AEAD ciphers only */
551 unsigned char *aad
[AAD_NUM
];
552 size_t aad_len
[AAD_NUM
];
556 const char *cts_mode
;
559 unsigned char *mac_key
;
563 static int cipher_test_init(EVP_TEST
*t
, const char *alg
)
565 const EVP_CIPHER
*cipher
;
566 EVP_CIPHER
*fetched_cipher
;
570 if (is_cipher_disabled(alg
)) {
572 TEST_info("skipping, '%s' is disabled", alg
);
577 if ((cipher
= fetched_cipher
= EVP_CIPHER_fetch(libctx
, alg
, NULL
)) == NULL
578 && (cipher
= EVP_get_cipherbyname(alg
)) == NULL
) {
579 /* a stitched cipher might not be available */
580 if (strstr(alg
, "HMAC") != NULL
) {
583 TEST_info("skipping, '%s' is not available", alg
);
586 ERR_clear_last_mark();
589 ERR_clear_last_mark();
591 cdat
= OPENSSL_zalloc(sizeof(*cdat
));
592 cdat
->cipher
= cipher
;
593 cdat
->fetched_cipher
= fetched_cipher
;
595 m
= EVP_CIPHER_get_mode(cipher
);
596 if (EVP_CIPHER_get_flags(cipher
) & EVP_CIPH_FLAG_AEAD_CIPHER
)
597 cdat
->aead
= m
!= 0 ? m
: -1;
602 if (fetched_cipher
!= NULL
)
603 TEST_info("%s is fetched", alg
);
607 static void cipher_test_cleanup(EVP_TEST
*t
)
610 CIPHER_DATA
*cdat
= t
->data
;
612 OPENSSL_free(cdat
->key
);
613 OPENSSL_free(cdat
->iv
);
614 OPENSSL_free(cdat
->next_iv
);
615 OPENSSL_free(cdat
->ciphertext
);
616 OPENSSL_free(cdat
->plaintext
);
617 for (i
= 0; i
< AAD_NUM
; i
++)
618 OPENSSL_free(cdat
->aad
[i
]);
619 OPENSSL_free(cdat
->tag
);
620 OPENSSL_free(cdat
->mac_key
);
621 EVP_CIPHER_free(cdat
->fetched_cipher
);
624 static int cipher_test_parse(EVP_TEST
*t
, const char *keyword
,
627 CIPHER_DATA
*cdat
= t
->data
;
630 if (strcmp(keyword
, "Key") == 0)
631 return parse_bin(value
, &cdat
->key
, &cdat
->key_len
);
632 if (strcmp(keyword
, "Rounds") == 0) {
636 cdat
->rounds
= (unsigned int)i
;
639 if (strcmp(keyword
, "IV") == 0)
640 return parse_bin(value
, &cdat
->iv
, &cdat
->iv_len
);
641 if (strcmp(keyword
, "NextIV") == 0)
642 return parse_bin(value
, &cdat
->next_iv
, &cdat
->iv_len
);
643 if (strcmp(keyword
, "Plaintext") == 0)
644 return parse_bin(value
, &cdat
->plaintext
, &cdat
->plaintext_len
);
645 if (strcmp(keyword
, "Ciphertext") == 0)
646 return parse_bin(value
, &cdat
->ciphertext
, &cdat
->ciphertext_len
);
647 if (strcmp(keyword
, "KeyBits") == 0) {
651 cdat
->key_bits
= (size_t)i
;
657 if (strcmp(keyword
, "TLSAAD") == 0)
658 cdat
->tls_aad
= tls_aad
= 1;
659 if (strcmp(keyword
, "AAD") == 0 || tls_aad
) {
660 for (i
= 0; i
< AAD_NUM
; i
++) {
661 if (cdat
->aad
[i
] == NULL
)
662 return parse_bin(value
, &cdat
->aad
[i
], &cdat
->aad_len
[i
]);
666 if (strcmp(keyword
, "Tag") == 0)
667 return parse_bin(value
, &cdat
->tag
, &cdat
->tag_len
);
668 if (strcmp(keyword
, "SetTagLate") == 0) {
669 if (strcmp(value
, "TRUE") == 0)
671 else if (strcmp(value
, "FALSE") == 0)
677 if (strcmp(keyword
, "MACKey") == 0)
678 return parse_bin(value
, &cdat
->mac_key
, &cdat
->mac_key_len
);
679 if (strcmp(keyword
, "TLSVersion") == 0) {
682 cdat
->tls_version
= (int)strtol(value
, &endptr
, 0);
683 return value
[0] != '\0' && endptr
[0] == '\0';
687 if (strcmp(keyword
, "Operation") == 0) {
688 if (strcmp(value
, "ENCRYPT") == 0)
690 else if (strcmp(value
, "DECRYPT") == 0)
696 if (strcmp(keyword
, "CTSMode") == 0) {
697 cdat
->cts_mode
= value
;
703 static int cipher_test_enc(EVP_TEST
*t
, int enc
,
704 size_t out_misalign
, size_t inp_misalign
, int frag
)
706 CIPHER_DATA
*expected
= t
->data
;
707 unsigned char *in
, *expected_out
, *tmp
= NULL
;
708 size_t in_len
, out_len
, donelen
= 0;
709 int ok
= 0, tmplen
, chunklen
, tmpflen
, i
;
710 EVP_CIPHER_CTX
*ctx_base
= NULL
;
711 EVP_CIPHER_CTX
*ctx
= NULL
, *duped
;
713 t
->err
= "TEST_FAILURE";
714 if (!TEST_ptr(ctx_base
= EVP_CIPHER_CTX_new()))
716 if (!TEST_ptr(ctx
= EVP_CIPHER_CTX_new()))
718 EVP_CIPHER_CTX_set_flags(ctx_base
, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
);
720 in
= expected
->plaintext
;
721 in_len
= expected
->plaintext_len
;
722 expected_out
= expected
->ciphertext
;
723 out_len
= expected
->ciphertext_len
;
725 in
= expected
->ciphertext
;
726 in_len
= expected
->ciphertext_len
;
727 expected_out
= expected
->plaintext
;
728 out_len
= expected
->plaintext_len
;
730 if (inp_misalign
== (size_t)-1) {
731 /* Exercise in-place encryption */
732 tmp
= OPENSSL_malloc(out_misalign
+ in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
);
735 in
= memcpy(tmp
+ out_misalign
, in
, in_len
);
737 inp_misalign
+= 16 - ((out_misalign
+ in_len
) & 15);
739 * 'tmp' will store both output and copy of input. We make the copy
740 * of input to specifically aligned part of 'tmp'. So we just
741 * figured out how much padding would ensure the required alignment,
742 * now we allocate extended buffer and finally copy the input just
743 * past inp_misalign in expression below. Output will be written
744 * past out_misalign...
746 tmp
= OPENSSL_malloc(out_misalign
+ in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
+
747 inp_misalign
+ in_len
);
750 in
= memcpy(tmp
+ out_misalign
+ in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
+
751 inp_misalign
, in
, in_len
);
753 if (!EVP_CipherInit_ex(ctx_base
, expected
->cipher
, NULL
, NULL
, NULL
, enc
)) {
754 t
->err
= "CIPHERINIT_ERROR";
757 if (expected
->cts_mode
!= NULL
) {
758 OSSL_PARAM params
[2];
760 params
[0] = OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE
,
761 (char *)expected
->cts_mode
,
763 params
[1] = OSSL_PARAM_construct_end();
764 if (!EVP_CIPHER_CTX_set_params(ctx_base
, params
)) {
765 t
->err
= "INVALID_CTS_MODE";
770 if (expected
->aead
) {
771 if (!EVP_CIPHER_CTX_ctrl(ctx_base
, EVP_CTRL_AEAD_SET_IVLEN
,
772 expected
->iv_len
, 0)) {
773 t
->err
= "INVALID_IV_LENGTH";
776 } else if (expected
->iv_len
!= (size_t)EVP_CIPHER_CTX_get_iv_length(ctx_base
)) {
777 t
->err
= "INVALID_IV_LENGTH";
781 if (expected
->aead
&& !expected
->tls_aad
) {
784 * If encrypting or OCB just set tag length initially, otherwise
785 * set tag length and value.
787 if (enc
|| expected
->aead
== EVP_CIPH_OCB_MODE
|| expected
->tag_late
) {
788 t
->err
= "TAG_LENGTH_SET_ERROR";
791 t
->err
= "TAG_SET_ERROR";
794 if (tag
|| expected
->aead
!= EVP_CIPH_GCM_MODE
) {
795 if (!EVP_CIPHER_CTX_ctrl(ctx_base
, EVP_CTRL_AEAD_SET_TAG
,
796 expected
->tag_len
, tag
))
801 if (expected
->rounds
> 0) {
802 int rounds
= (int)expected
->rounds
;
804 if (!EVP_CIPHER_CTX_ctrl(ctx_base
, EVP_CTRL_SET_RC5_ROUNDS
, rounds
, NULL
)) {
805 t
->err
= "INVALID_ROUNDS";
810 if (!EVP_CIPHER_CTX_set_key_length(ctx_base
, expected
->key_len
)) {
811 t
->err
= "INVALID_KEY_LENGTH";
814 if (expected
->key_bits
> 0) {
815 int bits
= (int)expected
->key_bits
;
817 if (!EVP_CIPHER_CTX_ctrl(ctx_base
, EVP_CTRL_SET_RC2_KEY_BITS
, bits
, NULL
)) {
818 t
->err
= "INVALID KEY BITS";
822 if (!EVP_CipherInit_ex(ctx_base
, NULL
, NULL
, expected
->key
, expected
->iv
, -1)) {
823 t
->err
= "KEY_SET_ERROR";
827 /* Check that we get the same IV back */
828 if (expected
->iv
!= NULL
) {
829 /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
830 unsigned char iv
[128];
831 if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx_base
, iv
, sizeof(iv
)))
832 || ((EVP_CIPHER_get_flags(expected
->cipher
) & EVP_CIPH_CUSTOM_IV
) == 0
833 && !TEST_mem_eq(expected
->iv
, expected
->iv_len
, iv
,
834 expected
->iv_len
))) {
835 t
->err
= "INVALID_IV";
840 /* Test that the cipher dup functions correctly if it is supported */
842 if (EVP_CIPHER_CTX_copy(ctx
, ctx_base
)) {
843 EVP_CIPHER_CTX_free(ctx_base
);
846 EVP_CIPHER_CTX_free(ctx
);
849 /* Likewise for dup */
850 duped
= EVP_CIPHER_CTX_dup(ctx
);
852 EVP_CIPHER_CTX_free(ctx
);
857 if (expected
->mac_key
!= NULL
858 && !EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_MAC_KEY
,
859 (int)expected
->mac_key_len
,
860 (void *)expected
->mac_key
)) {
861 t
->err
= "SET_MAC_KEY_ERROR";
865 if (expected
->tls_version
) {
866 OSSL_PARAM params
[2];
868 params
[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS_VERSION
,
869 &expected
->tls_version
);
870 params
[1] = OSSL_PARAM_construct_end();
871 if (!EVP_CIPHER_CTX_set_params(ctx
, params
)) {
872 t
->err
= "SET_TLS_VERSION_ERROR";
877 if (expected
->aead
== EVP_CIPH_CCM_MODE
) {
878 if (!EVP_CipherUpdate(ctx
, NULL
, &tmplen
, NULL
, out_len
)) {
879 t
->err
= "CCM_PLAINTEXT_LENGTH_SET_ERROR";
883 if (expected
->aad
[0] != NULL
&& !expected
->tls_aad
) {
884 t
->err
= "AAD_SET_ERROR";
886 for (i
= 0; expected
->aad
[i
] != NULL
; i
++) {
887 if (!EVP_CipherUpdate(ctx
, NULL
, &chunklen
, expected
->aad
[i
],
888 expected
->aad_len
[i
]))
893 * Supply the AAD in chunks less than the block size where possible
895 for (i
= 0; expected
->aad
[i
] != NULL
; i
++) {
896 if (expected
->aad_len
[i
] > 0) {
897 if (!EVP_CipherUpdate(ctx
, NULL
, &chunklen
, expected
->aad
[i
], 1))
901 if (expected
->aad_len
[i
] > 2) {
902 if (!EVP_CipherUpdate(ctx
, NULL
, &chunklen
,
903 expected
->aad
[i
] + donelen
,
904 expected
->aad_len
[i
] - 2))
906 donelen
+= expected
->aad_len
[i
] - 2;
908 if (expected
->aad_len
[i
] > 1
909 && !EVP_CipherUpdate(ctx
, NULL
, &chunklen
,
910 expected
->aad
[i
] + donelen
, 1))
916 if (expected
->tls_aad
) {
917 OSSL_PARAM params
[2];
920 /* duplicate the aad as the implementation might modify it */
921 if ((tls_aad
= OPENSSL_memdup(expected
->aad
[0],
922 expected
->aad_len
[0])) == NULL
)
924 params
[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD
,
926 expected
->aad_len
[0]);
927 params
[1] = OSSL_PARAM_construct_end();
928 if (!EVP_CIPHER_CTX_set_params(ctx
, params
)) {
929 OPENSSL_free(tls_aad
);
930 t
->err
= "TLS1_AAD_ERROR";
933 OPENSSL_free(tls_aad
);
934 } else if (!enc
&& (expected
->aead
== EVP_CIPH_OCB_MODE
935 || expected
->tag_late
)) {
936 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_TAG
,
937 expected
->tag_len
, expected
->tag
)) {
938 t
->err
= "TAG_SET_ERROR";
943 EVP_CIPHER_CTX_set_padding(ctx
, 0);
944 t
->err
= "CIPHERUPDATE_ERROR";
947 /* We supply the data all in one go */
948 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
, &tmplen
, in
, in_len
))
951 /* Supply the data in chunks less than the block size where possible */
953 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
, &chunklen
, in
, 1))
960 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
+ tmplen
, &chunklen
,
968 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
+ tmplen
, &chunklen
,
974 if (!EVP_CipherFinal_ex(ctx
, tmp
+ out_misalign
+ tmplen
, &tmpflen
)) {
975 t
->err
= "CIPHERFINAL_ERROR";
978 if (!enc
&& expected
->tls_aad
) {
979 if (expected
->tls_version
>= TLS1_1_VERSION
980 && (EVP_CIPHER_is_a(expected
->cipher
, "AES-128-CBC-HMAC-SHA1")
981 || EVP_CIPHER_is_a(expected
->cipher
, "AES-256-CBC-HMAC-SHA1"))) {
982 tmplen
-= expected
->iv_len
;
983 expected_out
+= expected
->iv_len
;
984 out_misalign
+= expected
->iv_len
;
986 if ((int)out_len
> tmplen
+ tmpflen
)
987 out_len
= tmplen
+ tmpflen
;
989 if (!memory_err_compare(t
, "VALUE_MISMATCH", expected_out
, out_len
,
990 tmp
+ out_misalign
, tmplen
+ tmpflen
))
992 if (enc
&& expected
->aead
&& !expected
->tls_aad
) {
993 unsigned char rtag
[16];
995 if (!TEST_size_t_le(expected
->tag_len
, sizeof(rtag
))) {
996 t
->err
= "TAG_LENGTH_INTERNAL_ERROR";
999 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_GET_TAG
,
1000 expected
->tag_len
, rtag
)) {
1001 t
->err
= "TAG_RETRIEVE_ERROR";
1004 if (!memory_err_compare(t
, "TAG_VALUE_MISMATCH",
1005 expected
->tag
, expected
->tag_len
,
1006 rtag
, expected
->tag_len
))
1009 /* Check the updated IV */
1010 if (expected
->next_iv
!= NULL
) {
1011 /* Some (e.g., GCM) tests use IVs longer than EVP_MAX_IV_LENGTH. */
1012 unsigned char iv
[128];
1013 if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx
, iv
, sizeof(iv
)))
1014 || ((EVP_CIPHER_get_flags(expected
->cipher
) & EVP_CIPH_CUSTOM_IV
) == 0
1015 && !TEST_mem_eq(expected
->next_iv
, expected
->iv_len
, iv
,
1016 expected
->iv_len
))) {
1017 t
->err
= "INVALID_NEXT_IV";
1026 if (ctx
!= ctx_base
)
1027 EVP_CIPHER_CTX_free(ctx_base
);
1028 EVP_CIPHER_CTX_free(ctx
);
1032 static int cipher_test_run(EVP_TEST
*t
)
1034 CIPHER_DATA
*cdat
= t
->data
;
1036 size_t out_misalign
, inp_misalign
;
1042 if (!cdat
->iv
&& EVP_CIPHER_get_iv_length(cdat
->cipher
)) {
1043 /* IV is optional and usually omitted in wrap mode */
1044 if (EVP_CIPHER_get_mode(cdat
->cipher
) != EVP_CIPH_WRAP_MODE
) {
1049 if (cdat
->aead
&& cdat
->tag
== NULL
&& !cdat
->tls_aad
) {
1053 for (out_misalign
= 0; out_misalign
<= 1;) {
1054 static char aux_err
[64];
1055 t
->aux_err
= aux_err
;
1056 for (inp_misalign
= (size_t)-1; inp_misalign
!= 2; inp_misalign
++) {
1057 if (inp_misalign
== (size_t)-1) {
1058 /* kludge: inp_misalign == -1 means "exercise in-place" */
1059 BIO_snprintf(aux_err
, sizeof(aux_err
),
1060 "%s in-place, %sfragmented",
1061 out_misalign
? "misaligned" : "aligned",
1062 frag
? "" : "not ");
1064 BIO_snprintf(aux_err
, sizeof(aux_err
),
1065 "%s output and %s input, %sfragmented",
1066 out_misalign
? "misaligned" : "aligned",
1067 inp_misalign
? "misaligned" : "aligned",
1068 frag
? "" : "not ");
1071 rv
= cipher_test_enc(t
, 1, out_misalign
, inp_misalign
, frag
);
1072 /* Not fatal errors: return */
1079 if (cdat
->enc
!= 1) {
1080 rv
= cipher_test_enc(t
, 0, out_misalign
, inp_misalign
, frag
);
1081 /* Not fatal errors: return */
1090 if (out_misalign
== 1 && frag
== 0) {
1092 * XTS, SIV, CCM, stitched ciphers and Wrap modes have special
1093 * requirements about input lengths so we don't fragment for those
1095 if (cdat
->aead
== EVP_CIPH_CCM_MODE
1096 || cdat
->aead
== EVP_CIPH_CBC_MODE
1097 || (cdat
->aead
== -1
1098 && EVP_CIPHER_get_mode(cdat
->cipher
) == EVP_CIPH_STREAM_CIPHER
)
1099 || ((EVP_CIPHER_get_flags(cdat
->cipher
) & EVP_CIPH_FLAG_CTS
) != 0)
1100 || EVP_CIPHER_get_mode(cdat
->cipher
) == EVP_CIPH_SIV_MODE
1101 || EVP_CIPHER_get_mode(cdat
->cipher
) == EVP_CIPH_XTS_MODE
1102 || EVP_CIPHER_get_mode(cdat
->cipher
) == EVP_CIPH_WRAP_MODE
)
1115 static const EVP_TEST_METHOD cipher_test_method
= {
1118 cipher_test_cleanup
,
1128 typedef struct mac_data_st
{
1129 /* MAC type in one form or another */
1131 EVP_MAC
*mac
; /* for mac_test_run_mac */
1132 int type
; /* for mac_test_run_pkey */
1133 /* Algorithm string for this MAC */
1142 unsigned char *input
;
1144 /* Expected output */
1145 unsigned char *output
;
1147 unsigned char *custom
;
1149 /* MAC salt (blake2) */
1150 unsigned char *salt
;
1154 /* Collection of controls */
1155 STACK_OF(OPENSSL_STRING
) *controls
;
1162 static int mac_test_init(EVP_TEST
*t
, const char *alg
)
1164 EVP_MAC
*mac
= NULL
;
1165 int type
= NID_undef
;
1168 if (is_mac_disabled(alg
)) {
1169 TEST_info("skipping, '%s' is disabled", alg
);
1173 if ((mac
= EVP_MAC_fetch(libctx
, alg
, NULL
)) == NULL
) {
1175 * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
1176 * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
1177 * the EVP_PKEY method.
1179 size_t sz
= strlen(alg
);
1180 static const char epilogue
[] = " by EVP_PKEY";
1182 if (sz
>= sizeof(epilogue
)
1183 && strcmp(alg
+ sz
- (sizeof(epilogue
) - 1), epilogue
) == 0)
1184 sz
-= sizeof(epilogue
) - 1;
1186 if (strncmp(alg
, "HMAC", sz
) == 0)
1187 type
= EVP_PKEY_HMAC
;
1188 else if (strncmp(alg
, "CMAC", sz
) == 0)
1189 type
= EVP_PKEY_CMAC
;
1190 else if (strncmp(alg
, "Poly1305", sz
) == 0)
1191 type
= EVP_PKEY_POLY1305
;
1192 else if (strncmp(alg
, "SipHash", sz
) == 0)
1193 type
= EVP_PKEY_SIPHASH
;
1198 mdat
= OPENSSL_zalloc(sizeof(*mdat
));
1200 mdat
->mac_name
= OPENSSL_strdup(alg
);
1202 mdat
->controls
= sk_OPENSSL_STRING_new_null();
1203 mdat
->output_size
= mdat
->block_size
= -1;
1208 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
1209 static void openssl_free(char *m
)
1214 static void mac_test_cleanup(EVP_TEST
*t
)
1216 MAC_DATA
*mdat
= t
->data
;
1218 EVP_MAC_free(mdat
->mac
);
1219 OPENSSL_free(mdat
->mac_name
);
1220 sk_OPENSSL_STRING_pop_free(mdat
->controls
, openssl_free
);
1221 OPENSSL_free(mdat
->alg
);
1222 OPENSSL_free(mdat
->key
);
1223 OPENSSL_free(mdat
->iv
);
1224 OPENSSL_free(mdat
->custom
);
1225 OPENSSL_free(mdat
->salt
);
1226 OPENSSL_free(mdat
->input
);
1227 OPENSSL_free(mdat
->output
);
1230 static int mac_test_parse(EVP_TEST
*t
,
1231 const char *keyword
, const char *value
)
1233 MAC_DATA
*mdata
= t
->data
;
1235 if (strcmp(keyword
, "Key") == 0)
1236 return parse_bin(value
, &mdata
->key
, &mdata
->key_len
);
1237 if (strcmp(keyword
, "IV") == 0)
1238 return parse_bin(value
, &mdata
->iv
, &mdata
->iv_len
);
1239 if (strcmp(keyword
, "Custom") == 0)
1240 return parse_bin(value
, &mdata
->custom
, &mdata
->custom_len
);
1241 if (strcmp(keyword
, "Salt") == 0)
1242 return parse_bin(value
, &mdata
->salt
, &mdata
->salt_len
);
1243 if (strcmp(keyword
, "Algorithm") == 0) {
1244 mdata
->alg
= OPENSSL_strdup(value
);
1249 if (strcmp(keyword
, "Input") == 0)
1250 return parse_bin(value
, &mdata
->input
, &mdata
->input_len
);
1251 if (strcmp(keyword
, "Output") == 0)
1252 return parse_bin(value
, &mdata
->output
, &mdata
->output_len
);
1253 if (strcmp(keyword
, "XOF") == 0)
1254 return mdata
->xof
= 1;
1255 if (strcmp(keyword
, "Ctrl") == 0)
1256 return sk_OPENSSL_STRING_push(mdata
->controls
,
1257 OPENSSL_strdup(value
)) != 0;
1258 if (strcmp(keyword
, "OutputSize") == 0) {
1259 mdata
->output_size
= atoi(value
);
1260 if (mdata
->output_size
< 0)
1264 if (strcmp(keyword
, "BlockSize") == 0) {
1265 mdata
->block_size
= atoi(value
);
1266 if (mdata
->block_size
< 0)
1273 static int mac_test_ctrl_pkey(EVP_TEST
*t
, EVP_PKEY_CTX
*pctx
,
1279 if (!TEST_ptr(tmpval
= OPENSSL_strdup(value
)))
1281 p
= strchr(tmpval
, ':');
1284 rv
= EVP_PKEY_CTX_ctrl_str(pctx
, tmpval
, p
);
1287 t
->err
= "PKEY_CTRL_INVALID";
1289 t
->err
= "PKEY_CTRL_ERROR";
1292 OPENSSL_free(tmpval
);
1296 static int mac_test_run_pkey(EVP_TEST
*t
)
1298 MAC_DATA
*expected
= t
->data
;
1299 EVP_MD_CTX
*mctx
= NULL
;
1300 EVP_PKEY_CTX
*pctx
= NULL
, *genctx
= NULL
;
1301 EVP_PKEY
*key
= NULL
;
1302 const char *mdname
= NULL
;
1303 EVP_CIPHER
*cipher
= NULL
;
1304 unsigned char *got
= NULL
;
1308 /* We don't do XOF mode via PKEY */
1312 if (expected
->alg
== NULL
)
1313 TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected
->type
));
1315 TEST_info("Trying the EVP_PKEY %s test with %s",
1316 OBJ_nid2sn(expected
->type
), expected
->alg
);
1318 if (expected
->type
== EVP_PKEY_CMAC
) {
1319 #ifdef OPENSSL_NO_DEPRECATED_3_0
1320 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected
->alg
);
1325 OSSL_LIB_CTX
*tmpctx
;
1327 if (expected
->alg
!= NULL
&& is_cipher_disabled(expected
->alg
)) {
1328 TEST_info("skipping, PKEY CMAC '%s' is disabled", expected
->alg
);
1333 if (!TEST_ptr(cipher
= EVP_CIPHER_fetch(libctx
, expected
->alg
, NULL
))) {
1334 t
->err
= "MAC_KEY_CREATE_ERROR";
1337 tmpctx
= OSSL_LIB_CTX_set0_default(libctx
);
1338 key
= EVP_PKEY_new_CMAC_key(NULL
, expected
->key
, expected
->key_len
,
1340 OSSL_LIB_CTX_set0_default(tmpctx
);
1343 key
= EVP_PKEY_new_raw_private_key_ex(libctx
,
1344 OBJ_nid2sn(expected
->type
), NULL
,
1345 expected
->key
, expected
->key_len
);
1348 t
->err
= "MAC_KEY_CREATE_ERROR";
1352 if (expected
->type
== EVP_PKEY_HMAC
&& expected
->alg
!= NULL
) {
1353 if (is_digest_disabled(expected
->alg
)) {
1354 TEST_info("skipping, HMAC '%s' is disabled", expected
->alg
);
1359 mdname
= expected
->alg
;
1361 if (!TEST_ptr(mctx
= EVP_MD_CTX_new())) {
1362 t
->err
= "INTERNAL_ERROR";
1365 if (!EVP_DigestSignInit_ex(mctx
, &pctx
, mdname
, libctx
, NULL
, key
, NULL
)) {
1366 t
->err
= "DIGESTSIGNINIT_ERROR";
1369 for (i
= 0; i
< sk_OPENSSL_STRING_num(expected
->controls
); i
++)
1370 if (!mac_test_ctrl_pkey(t
, pctx
,
1371 sk_OPENSSL_STRING_value(expected
->controls
,
1373 t
->err
= "EVPPKEYCTXCTRL_ERROR";
1376 if (!EVP_DigestSignUpdate(mctx
, expected
->input
, expected
->input_len
)) {
1377 t
->err
= "DIGESTSIGNUPDATE_ERROR";
1380 if (!EVP_DigestSignFinal(mctx
, NULL
, &got_len
)) {
1381 t
->err
= "DIGESTSIGNFINAL_LENGTH_ERROR";
1384 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1385 t
->err
= "TEST_FAILURE";
1388 if (!EVP_DigestSignFinal(mctx
, got
, &got_len
)
1389 || !memory_err_compare(t
, "TEST_MAC_ERR",
1390 expected
->output
, expected
->output_len
,
1392 t
->err
= "TEST_MAC_ERR";
1397 EVP_CIPHER_free(cipher
);
1398 EVP_MD_CTX_free(mctx
);
1400 EVP_PKEY_CTX_free(genctx
);
1405 static int mac_test_run_mac(EVP_TEST
*t
)
1407 MAC_DATA
*expected
= t
->data
;
1408 EVP_MAC_CTX
*ctx
= NULL
;
1409 unsigned char *got
= NULL
;
1410 size_t got_len
= 0, size
= 0;
1411 int i
, block_size
= -1, output_size
= -1;
1412 OSSL_PARAM params
[21], sizes
[3], *psizes
= sizes
;
1413 size_t params_n
= 0;
1414 size_t params_n_allocstart
= 0;
1415 const OSSL_PARAM
*defined_params
=
1416 EVP_MAC_settable_ctx_params(expected
->mac
);
1419 if (expected
->alg
== NULL
)
1420 TEST_info("Trying the EVP_MAC %s test", expected
->mac_name
);
1422 TEST_info("Trying the EVP_MAC %s test with %s",
1423 expected
->mac_name
, expected
->alg
);
1425 if (expected
->alg
!= NULL
) {
1427 * The underlying algorithm may be a cipher or a digest.
1428 * We don't know which it is, but we can ask the MAC what it
1429 * should be and bet on that.
1431 if (OSSL_PARAM_locate_const(defined_params
,
1432 OSSL_MAC_PARAM_CIPHER
) != NULL
) {
1433 params
[params_n
++] =
1434 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER
,
1436 } else if (OSSL_PARAM_locate_const(defined_params
,
1437 OSSL_MAC_PARAM_DIGEST
) != NULL
) {
1438 params
[params_n
++] =
1439 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST
,
1442 t
->err
= "MAC_BAD_PARAMS";
1446 if (expected
->custom
!= NULL
)
1447 params
[params_n
++] =
1448 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM
,
1450 expected
->custom_len
);
1451 if (expected
->salt
!= NULL
)
1452 params
[params_n
++] =
1453 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT
,
1455 expected
->salt_len
);
1456 if (expected
->iv
!= NULL
)
1457 params
[params_n
++] =
1458 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV
,
1462 /* Unknown controls. They must match parameters that the MAC recognizes */
1463 if (params_n
+ sk_OPENSSL_STRING_num(expected
->controls
)
1464 >= OSSL_NELEM(params
)) {
1465 t
->err
= "MAC_TOO_MANY_PARAMETERS";
1468 params_n_allocstart
= params_n
;
1469 for (i
= 0; i
< sk_OPENSSL_STRING_num(expected
->controls
); i
++) {
1470 char *tmpkey
, *tmpval
;
1471 char *value
= sk_OPENSSL_STRING_value(expected
->controls
, i
);
1473 if (!TEST_ptr(tmpkey
= OPENSSL_strdup(value
))) {
1474 t
->err
= "MAC_PARAM_ERROR";
1477 tmpval
= strchr(tmpkey
, ':');
1482 || !OSSL_PARAM_allocate_from_text(¶ms
[params_n
],
1485 strlen(tmpval
), NULL
)) {
1486 OPENSSL_free(tmpkey
);
1487 t
->err
= "MAC_PARAM_ERROR";
1492 OPENSSL_free(tmpkey
);
1494 params
[params_n
] = OSSL_PARAM_construct_end();
1496 if ((ctx
= EVP_MAC_CTX_new(expected
->mac
)) == NULL
) {
1497 t
->err
= "MAC_CREATE_ERROR";
1501 if (!EVP_MAC_init(ctx
, expected
->key
, expected
->key_len
, params
)) {
1502 t
->err
= "MAC_INIT_ERROR";
1505 if (expected
->output_size
>= 0)
1506 *psizes
++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_SIZE
,
1508 if (expected
->block_size
>= 0)
1509 *psizes
++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_BLOCK_SIZE
,
1511 if (psizes
!= sizes
) {
1512 *psizes
= OSSL_PARAM_construct_end();
1513 if (!TEST_true(EVP_MAC_CTX_get_params(ctx
, sizes
))) {
1514 t
->err
= "INTERNAL_ERROR";
1517 if (expected
->output_size
>= 0
1518 && !TEST_int_eq(output_size
, expected
->output_size
)) {
1519 t
->err
= "TEST_FAILURE";
1522 if (expected
->block_size
>= 0
1523 && !TEST_int_eq(block_size
, expected
->block_size
)) {
1524 t
->err
= "TEST_FAILURE";
1528 if (!EVP_MAC_update(ctx
, expected
->input
, expected
->input_len
)) {
1529 t
->err
= "MAC_UPDATE_ERROR";
1532 xof
= expected
->xof
;
1534 if (!TEST_ptr(got
= OPENSSL_malloc(expected
->output_len
))) {
1535 t
->err
= "TEST_FAILURE";
1538 if (!EVP_MAC_finalXOF(ctx
, got
, expected
->output_len
)
1539 || !memory_err_compare(t
, "TEST_MAC_ERR",
1540 expected
->output
, expected
->output_len
,
1541 got
, expected
->output_len
)) {
1542 t
->err
= "MAC_FINAL_ERROR";
1546 if (!EVP_MAC_final(ctx
, NULL
, &got_len
, 0)) {
1547 t
->err
= "MAC_FINAL_LENGTH_ERROR";
1550 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1551 t
->err
= "TEST_FAILURE";
1554 if (!EVP_MAC_final(ctx
, got
, &got_len
, got_len
)
1555 || !memory_err_compare(t
, "TEST_MAC_ERR",
1556 expected
->output
, expected
->output_len
,
1558 t
->err
= "TEST_MAC_ERR";
1564 /* Test the EVP_Q_mac interface as well */
1566 OPENSSL_cleanse(got
, got_len
);
1567 if (!TEST_true(EVP_Q_mac(libctx
, expected
->mac_name
, NULL
,
1568 expected
->alg
, params
,
1569 expected
->key
, expected
->key_len
,
1570 expected
->input
, expected
->input_len
,
1571 got
, got_len
, &size
))
1572 || !TEST_mem_eq(got
, size
,
1573 expected
->output
, expected
->output_len
)) {
1574 t
->err
= "EVP_Q_mac failed";
1579 while (params_n
-- > params_n_allocstart
) {
1580 OPENSSL_free(params
[params_n
].data
);
1582 EVP_MAC_CTX_free(ctx
);
1587 static int mac_test_run(EVP_TEST
*t
)
1589 MAC_DATA
*expected
= t
->data
;
1591 if (expected
->mac
!= NULL
)
1592 return mac_test_run_mac(t
);
1593 return mac_test_run_pkey(t
);
1596 static const EVP_TEST_METHOD mac_test_method
= {
1607 ** These are all very similar and share much common code.
1610 typedef struct pkey_data_st
{
1611 /* Context for this operation */
1613 /* Key operation to perform */
1614 int (*keyop
) (EVP_PKEY_CTX
*ctx
,
1615 unsigned char *sig
, size_t *siglen
,
1616 const unsigned char *tbs
, size_t tbslen
);
1618 unsigned char *input
;
1620 /* Expected output */
1621 unsigned char *output
;
1626 * Perform public key operation setup: lookup key, allocated ctx and call
1627 * the appropriate initialisation function
1629 static int pkey_test_init(EVP_TEST
*t
, const char *name
,
1631 int (*keyopinit
) (EVP_PKEY_CTX
*ctx
),
1632 int (*keyop
)(EVP_PKEY_CTX
*ctx
,
1633 unsigned char *sig
, size_t *siglen
,
1634 const unsigned char *tbs
,
1638 EVP_PKEY
*pkey
= NULL
;
1642 rv
= find_key(&pkey
, name
, public_keys
);
1644 rv
= find_key(&pkey
, name
, private_keys
);
1645 if (rv
== 0 || pkey
== NULL
) {
1646 TEST_info("skipping, key '%s' is disabled", name
);
1651 if (!TEST_ptr(kdata
= OPENSSL_zalloc(sizeof(*kdata
)))) {
1652 EVP_PKEY_free(pkey
);
1655 kdata
->keyop
= keyop
;
1656 if (!TEST_ptr(kdata
->ctx
= EVP_PKEY_CTX_new_from_pkey(libctx
, pkey
, NULL
))) {
1657 EVP_PKEY_free(pkey
);
1658 OPENSSL_free(kdata
);
1661 if (keyopinit(kdata
->ctx
) <= 0)
1662 t
->err
= "KEYOP_INIT_ERROR";
1667 static void pkey_test_cleanup(EVP_TEST
*t
)
1669 PKEY_DATA
*kdata
= t
->data
;
1671 OPENSSL_free(kdata
->input
);
1672 OPENSSL_free(kdata
->output
);
1673 EVP_PKEY_CTX_free(kdata
->ctx
);
1676 static int pkey_test_ctrl(EVP_TEST
*t
, EVP_PKEY_CTX
*pctx
,
1682 if (!TEST_ptr(tmpval
= OPENSSL_strdup(value
)))
1684 p
= strchr(tmpval
, ':');
1687 rv
= EVP_PKEY_CTX_ctrl_str(pctx
, tmpval
, p
);
1690 t
->err
= "PKEY_CTRL_INVALID";
1692 } else if (p
!= NULL
&& rv
<= 0) {
1693 if (is_digest_disabled(p
) || is_cipher_disabled(p
)) {
1694 TEST_info("skipping, '%s' is disabled", p
);
1698 t
->err
= "PKEY_CTRL_ERROR";
1702 OPENSSL_free(tmpval
);
1706 static int pkey_test_parse(EVP_TEST
*t
,
1707 const char *keyword
, const char *value
)
1709 PKEY_DATA
*kdata
= t
->data
;
1710 if (strcmp(keyword
, "Input") == 0)
1711 return parse_bin(value
, &kdata
->input
, &kdata
->input_len
);
1712 if (strcmp(keyword
, "Output") == 0)
1713 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
1714 if (strcmp(keyword
, "Ctrl") == 0)
1715 return pkey_test_ctrl(t
, kdata
->ctx
, value
);
1719 static int pkey_test_run(EVP_TEST
*t
)
1721 PKEY_DATA
*expected
= t
->data
;
1722 unsigned char *got
= NULL
;
1724 EVP_PKEY_CTX
*copy
= NULL
;
1726 if (expected
->keyop(expected
->ctx
, NULL
, &got_len
,
1727 expected
->input
, expected
->input_len
) <= 0
1728 || !TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1729 t
->err
= "KEYOP_LENGTH_ERROR";
1732 if (expected
->keyop(expected
->ctx
, got
, &got_len
,
1733 expected
->input
, expected
->input_len
) <= 0) {
1734 t
->err
= "KEYOP_ERROR";
1737 if (!memory_err_compare(t
, "KEYOP_MISMATCH",
1738 expected
->output
, expected
->output_len
,
1746 /* Repeat the test on a copy. */
1747 if (!TEST_ptr(copy
= EVP_PKEY_CTX_dup(expected
->ctx
))) {
1748 t
->err
= "INTERNAL_ERROR";
1751 if (expected
->keyop(copy
, NULL
, &got_len
, expected
->input
,
1752 expected
->input_len
) <= 0
1753 || !TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1754 t
->err
= "KEYOP_LENGTH_ERROR";
1757 if (expected
->keyop(copy
, got
, &got_len
, expected
->input
,
1758 expected
->input_len
) <= 0) {
1759 t
->err
= "KEYOP_ERROR";
1762 if (!memory_err_compare(t
, "KEYOP_MISMATCH",
1763 expected
->output
, expected
->output_len
,
1769 EVP_PKEY_CTX_free(copy
);
1773 static int sign_test_init(EVP_TEST
*t
, const char *name
)
1775 return pkey_test_init(t
, name
, 0, EVP_PKEY_sign_init
, EVP_PKEY_sign
);
1778 static const EVP_TEST_METHOD psign_test_method
= {
1786 static int verify_recover_test_init(EVP_TEST
*t
, const char *name
)
1788 return pkey_test_init(t
, name
, 1, EVP_PKEY_verify_recover_init
,
1789 EVP_PKEY_verify_recover
);
1792 static const EVP_TEST_METHOD pverify_recover_test_method
= {
1794 verify_recover_test_init
,
1800 static int decrypt_test_init(EVP_TEST
*t
, const char *name
)
1802 return pkey_test_init(t
, name
, 0, EVP_PKEY_decrypt_init
,
1806 static const EVP_TEST_METHOD pdecrypt_test_method
= {
1814 static int verify_test_init(EVP_TEST
*t
, const char *name
)
1816 return pkey_test_init(t
, name
, 1, EVP_PKEY_verify_init
, 0);
1819 static int verify_test_run(EVP_TEST
*t
)
1821 PKEY_DATA
*kdata
= t
->data
;
1823 if (EVP_PKEY_verify(kdata
->ctx
, kdata
->output
, kdata
->output_len
,
1824 kdata
->input
, kdata
->input_len
) <= 0)
1825 t
->err
= "VERIFY_ERROR";
1829 static const EVP_TEST_METHOD pverify_test_method
= {
1837 static int pderive_test_init(EVP_TEST
*t
, const char *name
)
1839 return pkey_test_init(t
, name
, 0, EVP_PKEY_derive_init
, 0);
1842 static int pderive_test_parse(EVP_TEST
*t
,
1843 const char *keyword
, const char *value
)
1845 PKEY_DATA
*kdata
= t
->data
;
1848 if (strcmp(keyword
, "PeerKeyValidate") == 0)
1851 if (validate
|| strcmp(keyword
, "PeerKey") == 0) {
1853 if (find_key(&peer
, value
, public_keys
) == 0)
1855 if (EVP_PKEY_derive_set_peer_ex(kdata
->ctx
, peer
, validate
) <= 0) {
1856 t
->err
= "DERIVE_SET_PEER_ERROR";
1862 if (strcmp(keyword
, "SharedSecret") == 0)
1863 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
1864 if (strcmp(keyword
, "Ctrl") == 0)
1865 return pkey_test_ctrl(t
, kdata
->ctx
, value
);
1869 static int pderive_test_run(EVP_TEST
*t
)
1871 EVP_PKEY_CTX
*dctx
= NULL
;
1872 PKEY_DATA
*expected
= t
->data
;
1873 unsigned char *got
= NULL
;
1876 if (!TEST_ptr(dctx
= EVP_PKEY_CTX_dup(expected
->ctx
))) {
1877 t
->err
= "DERIVE_ERROR";
1881 if (EVP_PKEY_derive(dctx
, NULL
, &got_len
) <= 0) {
1882 t
->err
= "DERIVE_ERROR";
1885 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1886 t
->err
= "DERIVE_ERROR";
1889 if (EVP_PKEY_derive(dctx
, got
, &got_len
) <= 0) {
1890 t
->err
= "DERIVE_ERROR";
1893 if (!memory_err_compare(t
, "SHARED_SECRET_MISMATCH",
1894 expected
->output
, expected
->output_len
,
1901 EVP_PKEY_CTX_free(dctx
);
1905 static const EVP_TEST_METHOD pderive_test_method
= {
1918 typedef enum pbe_type_enum
{
1919 PBE_TYPE_INVALID
= 0,
1920 PBE_TYPE_SCRYPT
, PBE_TYPE_PBKDF2
, PBE_TYPE_PKCS12
1923 typedef struct pbe_data_st
{
1925 /* scrypt parameters */
1926 uint64_t N
, r
, p
, maxmem
;
1927 /* PKCS#12 parameters */
1931 unsigned char *pass
;
1934 unsigned char *salt
;
1936 /* Expected output */
1941 #ifndef OPENSSL_NO_SCRYPT
1942 /* Parse unsigned decimal 64 bit integer value */
1943 static int parse_uint64(const char *value
, uint64_t *pr
)
1945 const char *p
= value
;
1947 if (!TEST_true(*p
)) {
1948 TEST_info("Invalid empty integer value");
1951 for (*pr
= 0; *p
; ) {
1952 if (*pr
> UINT64_MAX
/ 10) {
1953 TEST_error("Integer overflow in string %s", value
);
1957 if (!TEST_true(isdigit((unsigned char)*p
))) {
1958 TEST_error("Invalid character in string %s", value
);
1967 static int scrypt_test_parse(EVP_TEST
*t
,
1968 const char *keyword
, const char *value
)
1970 PBE_DATA
*pdata
= t
->data
;
1972 if (strcmp(keyword
, "N") == 0)
1973 return parse_uint64(value
, &pdata
->N
);
1974 if (strcmp(keyword
, "p") == 0)
1975 return parse_uint64(value
, &pdata
->p
);
1976 if (strcmp(keyword
, "r") == 0)
1977 return parse_uint64(value
, &pdata
->r
);
1978 if (strcmp(keyword
, "maxmem") == 0)
1979 return parse_uint64(value
, &pdata
->maxmem
);
1984 static int pbkdf2_test_parse(EVP_TEST
*t
,
1985 const char *keyword
, const char *value
)
1987 PBE_DATA
*pdata
= t
->data
;
1989 if (strcmp(keyword
, "iter") == 0) {
1990 pdata
->iter
= atoi(value
);
1991 if (pdata
->iter
<= 0)
1995 if (strcmp(keyword
, "MD") == 0) {
1996 pdata
->md
= EVP_get_digestbyname(value
);
1997 if (pdata
->md
== NULL
)
2004 static int pkcs12_test_parse(EVP_TEST
*t
,
2005 const char *keyword
, const char *value
)
2007 PBE_DATA
*pdata
= t
->data
;
2009 if (strcmp(keyword
, "id") == 0) {
2010 pdata
->id
= atoi(value
);
2015 return pbkdf2_test_parse(t
, keyword
, value
);
2018 static int pbe_test_init(EVP_TEST
*t
, const char *alg
)
2021 PBE_TYPE pbe_type
= PBE_TYPE_INVALID
;
2023 if (is_kdf_disabled(alg
)) {
2024 TEST_info("skipping, '%s' is disabled", alg
);
2028 if (strcmp(alg
, "scrypt") == 0) {
2029 pbe_type
= PBE_TYPE_SCRYPT
;
2030 } else if (strcmp(alg
, "pbkdf2") == 0) {
2031 pbe_type
= PBE_TYPE_PBKDF2
;
2032 } else if (strcmp(alg
, "pkcs12") == 0) {
2033 pbe_type
= PBE_TYPE_PKCS12
;
2035 TEST_error("Unknown pbe algorithm %s", alg
);
2038 if (!TEST_ptr(pdat
= OPENSSL_zalloc(sizeof(*pdat
))))
2040 pdat
->pbe_type
= pbe_type
;
2045 static void pbe_test_cleanup(EVP_TEST
*t
)
2047 PBE_DATA
*pdat
= t
->data
;
2049 OPENSSL_free(pdat
->pass
);
2050 OPENSSL_free(pdat
->salt
);
2051 OPENSSL_free(pdat
->key
);
2054 static int pbe_test_parse(EVP_TEST
*t
,
2055 const char *keyword
, const char *value
)
2057 PBE_DATA
*pdata
= t
->data
;
2059 if (strcmp(keyword
, "Password") == 0)
2060 return parse_bin(value
, &pdata
->pass
, &pdata
->pass_len
);
2061 if (strcmp(keyword
, "Salt") == 0)
2062 return parse_bin(value
, &pdata
->salt
, &pdata
->salt_len
);
2063 if (strcmp(keyword
, "Key") == 0)
2064 return parse_bin(value
, &pdata
->key
, &pdata
->key_len
);
2065 if (pdata
->pbe_type
== PBE_TYPE_PBKDF2
)
2066 return pbkdf2_test_parse(t
, keyword
, value
);
2067 else if (pdata
->pbe_type
== PBE_TYPE_PKCS12
)
2068 return pkcs12_test_parse(t
, keyword
, value
);
2069 #ifndef OPENSSL_NO_SCRYPT
2070 else if (pdata
->pbe_type
== PBE_TYPE_SCRYPT
)
2071 return scrypt_test_parse(t
, keyword
, value
);
2076 static int pbe_test_run(EVP_TEST
*t
)
2078 PBE_DATA
*expected
= t
->data
;
2080 EVP_MD
*fetched_digest
= NULL
;
2081 OSSL_LIB_CTX
*save_libctx
;
2083 save_libctx
= OSSL_LIB_CTX_set0_default(libctx
);
2085 if (!TEST_ptr(key
= OPENSSL_malloc(expected
->key_len
))) {
2086 t
->err
= "INTERNAL_ERROR";
2089 if (expected
->pbe_type
== PBE_TYPE_PBKDF2
) {
2090 if (PKCS5_PBKDF2_HMAC((char *)expected
->pass
, expected
->pass_len
,
2091 expected
->salt
, expected
->salt_len
,
2092 expected
->iter
, expected
->md
,
2093 expected
->key_len
, key
) == 0) {
2094 t
->err
= "PBKDF2_ERROR";
2097 #ifndef OPENSSL_NO_SCRYPT
2098 } else if (expected
->pbe_type
== PBE_TYPE_SCRYPT
) {
2099 if (EVP_PBE_scrypt((const char *)expected
->pass
, expected
->pass_len
,
2100 expected
->salt
, expected
->salt_len
,
2101 expected
->N
, expected
->r
, expected
->p
,
2102 expected
->maxmem
, key
, expected
->key_len
) == 0) {
2103 t
->err
= "SCRYPT_ERROR";
2107 } else if (expected
->pbe_type
== PBE_TYPE_PKCS12
) {
2108 fetched_digest
= EVP_MD_fetch(libctx
, EVP_MD_get0_name(expected
->md
),
2110 if (fetched_digest
== NULL
) {
2111 t
->err
= "PKCS12_ERROR";
2114 if (PKCS12_key_gen_uni(expected
->pass
, expected
->pass_len
,
2115 expected
->salt
, expected
->salt_len
,
2116 expected
->id
, expected
->iter
, expected
->key_len
,
2117 key
, fetched_digest
) == 0) {
2118 t
->err
= "PKCS12_ERROR";
2122 if (!memory_err_compare(t
, "KEY_MISMATCH", expected
->key
, expected
->key_len
,
2123 key
, expected
->key_len
))
2128 EVP_MD_free(fetched_digest
);
2130 OSSL_LIB_CTX_set0_default(save_libctx
);
2134 static const EVP_TEST_METHOD pbe_test_method
= {
2148 BASE64_CANONICAL_ENCODING
= 0,
2149 BASE64_VALID_ENCODING
= 1,
2150 BASE64_INVALID_ENCODING
= 2
2151 } base64_encoding_type
;
2153 typedef struct encode_data_st
{
2154 /* Input to encoding */
2155 unsigned char *input
;
2157 /* Expected output */
2158 unsigned char *output
;
2160 base64_encoding_type encoding
;
2163 static int encode_test_init(EVP_TEST
*t
, const char *encoding
)
2167 if (!TEST_ptr(edata
= OPENSSL_zalloc(sizeof(*edata
))))
2169 if (strcmp(encoding
, "canonical") == 0) {
2170 edata
->encoding
= BASE64_CANONICAL_ENCODING
;
2171 } else if (strcmp(encoding
, "valid") == 0) {
2172 edata
->encoding
= BASE64_VALID_ENCODING
;
2173 } else if (strcmp(encoding
, "invalid") == 0) {
2174 edata
->encoding
= BASE64_INVALID_ENCODING
;
2175 if (!TEST_ptr(t
->expected_err
= OPENSSL_strdup("DECODE_ERROR")))
2178 TEST_error("Bad encoding: %s."
2179 " Should be one of {canonical, valid, invalid}",
2186 OPENSSL_free(edata
);
2190 static void encode_test_cleanup(EVP_TEST
*t
)
2192 ENCODE_DATA
*edata
= t
->data
;
2194 OPENSSL_free(edata
->input
);
2195 OPENSSL_free(edata
->output
);
2196 memset(edata
, 0, sizeof(*edata
));
2199 static int encode_test_parse(EVP_TEST
*t
,
2200 const char *keyword
, const char *value
)
2202 ENCODE_DATA
*edata
= t
->data
;
2204 if (strcmp(keyword
, "Input") == 0)
2205 return parse_bin(value
, &edata
->input
, &edata
->input_len
);
2206 if (strcmp(keyword
, "Output") == 0)
2207 return parse_bin(value
, &edata
->output
, &edata
->output_len
);
2211 static int encode_test_run(EVP_TEST
*t
)
2213 ENCODE_DATA
*expected
= t
->data
;
2214 unsigned char *encode_out
= NULL
, *decode_out
= NULL
;
2215 int output_len
, chunk_len
;
2216 EVP_ENCODE_CTX
*decode_ctx
= NULL
, *encode_ctx
= NULL
;
2218 if (!TEST_ptr(decode_ctx
= EVP_ENCODE_CTX_new())) {
2219 t
->err
= "INTERNAL_ERROR";
2223 if (expected
->encoding
== BASE64_CANONICAL_ENCODING
) {
2225 if (!TEST_ptr(encode_ctx
= EVP_ENCODE_CTX_new())
2226 || !TEST_ptr(encode_out
=
2227 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected
->input_len
))))
2230 EVP_EncodeInit(encode_ctx
);
2231 if (!TEST_true(EVP_EncodeUpdate(encode_ctx
, encode_out
, &chunk_len
,
2232 expected
->input
, expected
->input_len
)))
2235 output_len
= chunk_len
;
2237 EVP_EncodeFinal(encode_ctx
, encode_out
+ chunk_len
, &chunk_len
);
2238 output_len
+= chunk_len
;
2240 if (!memory_err_compare(t
, "BAD_ENCODING",
2241 expected
->output
, expected
->output_len
,
2242 encode_out
, output_len
))
2246 if (!TEST_ptr(decode_out
=
2247 OPENSSL_malloc(EVP_DECODE_LENGTH(expected
->output_len
))))
2250 EVP_DecodeInit(decode_ctx
);
2251 if (EVP_DecodeUpdate(decode_ctx
, decode_out
, &chunk_len
, expected
->output
,
2252 expected
->output_len
) < 0) {
2253 t
->err
= "DECODE_ERROR";
2256 output_len
= chunk_len
;
2258 if (EVP_DecodeFinal(decode_ctx
, decode_out
+ chunk_len
, &chunk_len
) != 1) {
2259 t
->err
= "DECODE_ERROR";
2262 output_len
+= chunk_len
;
2264 if (expected
->encoding
!= BASE64_INVALID_ENCODING
2265 && !memory_err_compare(t
, "BAD_DECODING",
2266 expected
->input
, expected
->input_len
,
2267 decode_out
, output_len
)) {
2268 t
->err
= "BAD_DECODING";
2274 OPENSSL_free(encode_out
);
2275 OPENSSL_free(decode_out
);
2276 EVP_ENCODE_CTX_free(decode_ctx
);
2277 EVP_ENCODE_CTX_free(encode_ctx
);
2281 static const EVP_TEST_METHOD encode_test_method
= {
2284 encode_test_cleanup
,
2293 #define MAX_RAND_REPEATS 15
2295 typedef struct rand_data_pass_st
{
2296 unsigned char *entropy
;
2297 unsigned char *reseed_entropy
;
2298 unsigned char *nonce
;
2299 unsigned char *pers
;
2300 unsigned char *reseed_addin
;
2301 unsigned char *addinA
;
2302 unsigned char *addinB
;
2303 unsigned char *pr_entropyA
;
2304 unsigned char *pr_entropyB
;
2305 unsigned char *output
;
2306 size_t entropy_len
, nonce_len
, pers_len
, addinA_len
, addinB_len
,
2307 pr_entropyA_len
, pr_entropyB_len
, output_len
, reseed_entropy_len
,
2311 typedef struct rand_data_st
{
2312 /* Context for this operation */
2314 EVP_RAND_CTX
*parent
;
2316 int prediction_resistance
;
2318 unsigned int generate_bits
;
2322 /* Expected output */
2323 RAND_DATA_PASS data
[MAX_RAND_REPEATS
];
2326 static int rand_test_init(EVP_TEST
*t
, const char *name
)
2330 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
2331 unsigned int strength
= 256;
2333 if (!TEST_ptr(rdata
= OPENSSL_zalloc(sizeof(*rdata
))))
2336 /* TEST-RAND is available in the FIPS provider but not with "fips=yes" */
2337 rand
= EVP_RAND_fetch(libctx
, "TEST-RAND", "-fips");
2340 rdata
->parent
= EVP_RAND_CTX_new(rand
, NULL
);
2341 EVP_RAND_free(rand
);
2342 if (rdata
->parent
== NULL
)
2345 *params
= OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH
, &strength
);
2346 if (!EVP_RAND_CTX_set_params(rdata
->parent
, params
))
2349 rand
= EVP_RAND_fetch(libctx
, name
, NULL
);
2352 rdata
->ctx
= EVP_RAND_CTX_new(rand
, rdata
->parent
);
2353 EVP_RAND_free(rand
);
2354 if (rdata
->ctx
== NULL
)
2361 EVP_RAND_CTX_free(rdata
->parent
);
2362 OPENSSL_free(rdata
);
2366 static void rand_test_cleanup(EVP_TEST
*t
)
2368 RAND_DATA
*rdata
= t
->data
;
2371 OPENSSL_free(rdata
->cipher
);
2372 OPENSSL_free(rdata
->digest
);
2374 for (i
= 0; i
<= rdata
->n
; i
++) {
2375 OPENSSL_free(rdata
->data
[i
].entropy
);
2376 OPENSSL_free(rdata
->data
[i
].reseed_entropy
);
2377 OPENSSL_free(rdata
->data
[i
].nonce
);
2378 OPENSSL_free(rdata
->data
[i
].pers
);
2379 OPENSSL_free(rdata
->data
[i
].reseed_addin
);
2380 OPENSSL_free(rdata
->data
[i
].addinA
);
2381 OPENSSL_free(rdata
->data
[i
].addinB
);
2382 OPENSSL_free(rdata
->data
[i
].pr_entropyA
);
2383 OPENSSL_free(rdata
->data
[i
].pr_entropyB
);
2384 OPENSSL_free(rdata
->data
[i
].output
);
2386 EVP_RAND_CTX_free(rdata
->ctx
);
2387 EVP_RAND_CTX_free(rdata
->parent
);
2390 static int rand_test_parse(EVP_TEST
*t
,
2391 const char *keyword
, const char *value
)
2393 RAND_DATA
*rdata
= t
->data
;
2394 RAND_DATA_PASS
*item
;
2398 if ((p
= strchr(keyword
, '.')) != NULL
) {
2400 if (n
>= MAX_RAND_REPEATS
)
2404 item
= rdata
->data
+ n
;
2405 if (HAS_PREFIX(keyword
, "Entropy."))
2406 return parse_bin(value
, &item
->entropy
, &item
->entropy_len
);
2407 if (HAS_PREFIX(keyword
, "ReseedEntropy."))
2408 return parse_bin(value
, &item
->reseed_entropy
,
2409 &item
->reseed_entropy_len
);
2410 if (HAS_PREFIX(keyword
, "Nonce."))
2411 return parse_bin(value
, &item
->nonce
, &item
->nonce_len
);
2412 if (HAS_PREFIX(keyword
, "PersonalisationString."))
2413 return parse_bin(value
, &item
->pers
, &item
->pers_len
);
2414 if (HAS_PREFIX(keyword
, "ReseedAdditionalInput."))
2415 return parse_bin(value
, &item
->reseed_addin
,
2416 &item
->reseed_addin_len
);
2417 if (HAS_PREFIX(keyword
, "AdditionalInputA."))
2418 return parse_bin(value
, &item
->addinA
, &item
->addinA_len
);
2419 if (HAS_PREFIX(keyword
, "AdditionalInputB."))
2420 return parse_bin(value
, &item
->addinB
, &item
->addinB_len
);
2421 if (HAS_PREFIX(keyword
, "EntropyPredictionResistanceA."))
2422 return parse_bin(value
, &item
->pr_entropyA
, &item
->pr_entropyA_len
);
2423 if (HAS_PREFIX(keyword
, "EntropyPredictionResistanceB."))
2424 return parse_bin(value
, &item
->pr_entropyB
, &item
->pr_entropyB_len
);
2425 if (HAS_PREFIX(keyword
, "Output."))
2426 return parse_bin(value
, &item
->output
, &item
->output_len
);
2428 if (strcmp(keyword
, "Cipher") == 0)
2429 return TEST_ptr(rdata
->cipher
= OPENSSL_strdup(value
));
2430 if (strcmp(keyword
, "Digest") == 0)
2431 return TEST_ptr(rdata
->digest
= OPENSSL_strdup(value
));
2432 if (strcmp(keyword
, "DerivationFunction") == 0) {
2433 rdata
->use_df
= atoi(value
) != 0;
2436 if (strcmp(keyword
, "GenerateBits") == 0) {
2437 if ((n
= atoi(value
)) <= 0 || n
% 8 != 0)
2439 rdata
->generate_bits
= (unsigned int)n
;
2442 if (strcmp(keyword
, "PredictionResistance") == 0) {
2443 rdata
->prediction_resistance
= atoi(value
) != 0;
2450 static int rand_test_run(EVP_TEST
*t
)
2452 RAND_DATA
*expected
= t
->data
;
2453 RAND_DATA_PASS
*item
;
2455 size_t got_len
= expected
->generate_bits
/ 8;
2456 OSSL_PARAM params
[5], *p
= params
;
2457 int i
= -1, ret
= 0;
2458 unsigned int strength
;
2461 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
)))
2464 *p
++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF
, &expected
->use_df
);
2465 if (expected
->cipher
!= NULL
)
2466 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER
,
2467 expected
->cipher
, 0);
2468 if (expected
->digest
!= NULL
)
2469 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST
,
2470 expected
->digest
, 0);
2471 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC
, "HMAC", 0);
2472 *p
= OSSL_PARAM_construct_end();
2473 if (!TEST_true(EVP_RAND_CTX_set_params(expected
->ctx
, params
)))
2476 strength
= EVP_RAND_get_strength(expected
->ctx
);
2477 for (i
= 0; i
<= expected
->n
; i
++) {
2478 item
= expected
->data
+ i
;
2481 z
= item
->entropy
!= NULL
? item
->entropy
: (unsigned char *)"";
2482 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY
,
2483 z
, item
->entropy_len
);
2484 z
= item
->nonce
!= NULL
? item
->nonce
: (unsigned char *)"";
2485 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE
,
2486 z
, item
->nonce_len
);
2487 *p
= OSSL_PARAM_construct_end();
2488 if (!TEST_true(EVP_RAND_instantiate(expected
->parent
, strength
,
2489 0, NULL
, 0, params
)))
2492 z
= item
->pers
!= NULL
? item
->pers
: (unsigned char *)"";
2493 if (!TEST_true(EVP_RAND_instantiate
2494 (expected
->ctx
, strength
,
2495 expected
->prediction_resistance
, z
,
2496 item
->pers_len
, NULL
)))
2499 if (item
->reseed_entropy
!= NULL
) {
2500 params
[0] = OSSL_PARAM_construct_octet_string
2501 (OSSL_RAND_PARAM_TEST_ENTROPY
, item
->reseed_entropy
,
2502 item
->reseed_entropy_len
);
2503 params
[1] = OSSL_PARAM_construct_end();
2504 if (!TEST_true(EVP_RAND_CTX_set_params(expected
->parent
, params
)))
2507 if (!TEST_true(EVP_RAND_reseed
2508 (expected
->ctx
, expected
->prediction_resistance
,
2509 NULL
, 0, item
->reseed_addin
,
2510 item
->reseed_addin_len
)))
2513 if (item
->pr_entropyA
!= NULL
) {
2514 params
[0] = OSSL_PARAM_construct_octet_string
2515 (OSSL_RAND_PARAM_TEST_ENTROPY
, item
->pr_entropyA
,
2516 item
->pr_entropyA_len
);
2517 params
[1] = OSSL_PARAM_construct_end();
2518 if (!TEST_true(EVP_RAND_CTX_set_params(expected
->parent
, params
)))
2521 if (!TEST_true(EVP_RAND_generate
2522 (expected
->ctx
, got
, got_len
,
2523 strength
, expected
->prediction_resistance
,
2524 item
->addinA
, item
->addinA_len
)))
2527 if (item
->pr_entropyB
!= NULL
) {
2528 params
[0] = OSSL_PARAM_construct_octet_string
2529 (OSSL_RAND_PARAM_TEST_ENTROPY
, item
->pr_entropyB
,
2530 item
->pr_entropyB_len
);
2531 params
[1] = OSSL_PARAM_construct_end();
2532 if (!TEST_true(EVP_RAND_CTX_set_params(expected
->parent
, params
)))
2535 if (!TEST_true(EVP_RAND_generate
2536 (expected
->ctx
, got
, got_len
,
2537 strength
, expected
->prediction_resistance
,
2538 item
->addinB
, item
->addinB_len
)))
2540 if (!TEST_mem_eq(got
, got_len
, item
->output
, item
->output_len
))
2542 if (!TEST_true(EVP_RAND_uninstantiate(expected
->ctx
))
2543 || !TEST_true(EVP_RAND_uninstantiate(expected
->parent
))
2544 || !TEST_true(EVP_RAND_verify_zeroization(expected
->ctx
))
2545 || !TEST_int_eq(EVP_RAND_get_state(expected
->ctx
),
2546 EVP_RAND_STATE_UNINITIALISED
))
2553 if (ret
== 0 && i
>= 0)
2554 TEST_info("Error in test case %d of %d\n", i
, expected
->n
+ 1);
2559 static const EVP_TEST_METHOD rand_test_method
= {
2571 typedef struct kdf_data_st
{
2572 /* Context for this operation */
2574 /* Expected output */
2575 unsigned char *output
;
2577 OSSL_PARAM params
[20];
2582 * Perform public key operation setup: lookup key, allocated ctx and call
2583 * the appropriate initialisation function
2585 static int kdf_test_init(EVP_TEST
*t
, const char *name
)
2590 if (is_kdf_disabled(name
)) {
2591 TEST_info("skipping, '%s' is disabled", name
);
2596 if (!TEST_ptr(kdata
= OPENSSL_zalloc(sizeof(*kdata
))))
2598 kdata
->p
= kdata
->params
;
2599 *kdata
->p
= OSSL_PARAM_construct_end();
2601 kdf
= EVP_KDF_fetch(libctx
, name
, NULL
);
2603 OPENSSL_free(kdata
);
2606 kdata
->ctx
= EVP_KDF_CTX_new(kdf
);
2608 if (kdata
->ctx
== NULL
) {
2609 OPENSSL_free(kdata
);
2616 static void kdf_test_cleanup(EVP_TEST
*t
)
2618 KDF_DATA
*kdata
= t
->data
;
2621 for (p
= kdata
->params
; p
->key
!= NULL
; p
++)
2622 OPENSSL_free(p
->data
);
2623 OPENSSL_free(kdata
->output
);
2624 EVP_KDF_CTX_free(kdata
->ctx
);
2627 static int kdf_test_ctrl(EVP_TEST
*t
, EVP_KDF_CTX
*kctx
,
2630 KDF_DATA
*kdata
= t
->data
;
2633 const OSSL_PARAM
*defs
= EVP_KDF_settable_ctx_params(EVP_KDF_CTX_kdf(kctx
));
2635 if (!TEST_ptr(name
= OPENSSL_strdup(value
)))
2637 p
= strchr(name
, ':');
2641 rv
= OSSL_PARAM_allocate_from_text(kdata
->p
, defs
, name
, p
,
2642 p
!= NULL
? strlen(p
) : 0, NULL
);
2643 *++kdata
->p
= OSSL_PARAM_construct_end();
2645 t
->err
= "KDF_PARAM_ERROR";
2649 if (p
!= NULL
&& strcmp(name
, "digest") == 0) {
2650 if (is_digest_disabled(p
)) {
2651 TEST_info("skipping, '%s' is disabled", p
);
2656 && (strcmp(name
, "cipher") == 0
2657 || strcmp(name
, "cekalg") == 0)
2658 && is_cipher_disabled(p
)) {
2659 TEST_info("skipping, '%s' is disabled", p
);
2663 && (strcmp(name
, "mac") == 0)
2664 && is_mac_disabled(p
)) {
2665 TEST_info("skipping, '%s' is disabled", p
);
2672 static int kdf_test_parse(EVP_TEST
*t
,
2673 const char *keyword
, const char *value
)
2675 KDF_DATA
*kdata
= t
->data
;
2677 if (strcmp(keyword
, "Output") == 0)
2678 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
2679 if (HAS_PREFIX(keyword
, "Ctrl"))
2680 return kdf_test_ctrl(t
, kdata
->ctx
, value
);
2684 static int kdf_test_run(EVP_TEST
*t
)
2686 KDF_DATA
*expected
= t
->data
;
2687 unsigned char *got
= NULL
;
2688 size_t got_len
= expected
->output_len
;
2690 if (!EVP_KDF_CTX_set_params(expected
->ctx
, expected
->params
)) {
2691 t
->err
= "KDF_CTRL_ERROR";
2694 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
== 0 ? 1 : got_len
))) {
2695 t
->err
= "INTERNAL_ERROR";
2698 if (EVP_KDF_derive(expected
->ctx
, got
, got_len
, NULL
) <= 0) {
2699 t
->err
= "KDF_DERIVE_ERROR";
2702 if (!memory_err_compare(t
, "KDF_MISMATCH",
2703 expected
->output
, expected
->output_len
,
2714 static const EVP_TEST_METHOD kdf_test_method
= {
2726 typedef struct pkey_kdf_data_st
{
2727 /* Context for this operation */
2729 /* Expected output */
2730 unsigned char *output
;
2735 * Perform public key operation setup: lookup key, allocated ctx and call
2736 * the appropriate initialisation function
2738 static int pkey_kdf_test_init(EVP_TEST
*t
, const char *name
)
2740 PKEY_KDF_DATA
*kdata
= NULL
;
2742 if (is_kdf_disabled(name
)) {
2743 TEST_info("skipping, '%s' is disabled", name
);
2748 if (!TEST_ptr(kdata
= OPENSSL_zalloc(sizeof(*kdata
))))
2751 kdata
->ctx
= EVP_PKEY_CTX_new_from_name(libctx
, name
, NULL
);
2752 if (kdata
->ctx
== NULL
2753 || EVP_PKEY_derive_init(kdata
->ctx
) <= 0)
2759 EVP_PKEY_CTX_free(kdata
->ctx
);
2760 OPENSSL_free(kdata
);
2764 static void pkey_kdf_test_cleanup(EVP_TEST
*t
)
2766 PKEY_KDF_DATA
*kdata
= t
->data
;
2768 OPENSSL_free(kdata
->output
);
2769 EVP_PKEY_CTX_free(kdata
->ctx
);
2772 static int pkey_kdf_test_parse(EVP_TEST
*t
,
2773 const char *keyword
, const char *value
)
2775 PKEY_KDF_DATA
*kdata
= t
->data
;
2777 if (strcmp(keyword
, "Output") == 0)
2778 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
2779 if (HAS_PREFIX(keyword
, "Ctrl"))
2780 return pkey_test_ctrl(t
, kdata
->ctx
, value
);
2784 static int pkey_kdf_test_run(EVP_TEST
*t
)
2786 PKEY_KDF_DATA
*expected
= t
->data
;
2787 unsigned char *got
= NULL
;
2788 size_t got_len
= expected
->output_len
;
2790 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
== 0 ? 1 : got_len
))) {
2791 t
->err
= "INTERNAL_ERROR";
2794 if (EVP_PKEY_derive(expected
->ctx
, got
, &got_len
) <= 0) {
2795 t
->err
= "KDF_DERIVE_ERROR";
2798 if (!TEST_mem_eq(expected
->output
, expected
->output_len
, got
, got_len
)) {
2799 t
->err
= "KDF_MISMATCH";
2809 static const EVP_TEST_METHOD pkey_kdf_test_method
= {
2812 pkey_kdf_test_cleanup
,
2813 pkey_kdf_test_parse
,
2821 typedef struct keypair_test_data_st
{
2824 } KEYPAIR_TEST_DATA
;
2826 static int keypair_test_init(EVP_TEST
*t
, const char *pair
)
2828 KEYPAIR_TEST_DATA
*data
;
2830 EVP_PKEY
*pk
= NULL
, *pubk
= NULL
;
2831 char *pub
, *priv
= NULL
;
2833 /* Split private and public names. */
2834 if (!TEST_ptr(priv
= OPENSSL_strdup(pair
))
2835 || !TEST_ptr(pub
= strchr(priv
, ':'))) {
2836 t
->err
= "PARSING_ERROR";
2841 if (!TEST_true(find_key(&pk
, priv
, private_keys
))) {
2842 TEST_info("Can't find private key: %s", priv
);
2843 t
->err
= "MISSING_PRIVATE_KEY";
2846 if (!TEST_true(find_key(&pubk
, pub
, public_keys
))) {
2847 TEST_info("Can't find public key: %s", pub
);
2848 t
->err
= "MISSING_PUBLIC_KEY";
2852 if (pk
== NULL
&& pubk
== NULL
) {
2853 /* Both keys are listed but unsupported: skip this test */
2859 if (!TEST_ptr(data
= OPENSSL_malloc(sizeof(*data
))))
2872 static void keypair_test_cleanup(EVP_TEST
*t
)
2874 OPENSSL_free(t
->data
);
2879 * For tests that do not accept any custom keywords.
2881 static int void_test_parse(EVP_TEST
*t
, const char *keyword
, const char *value
)
2886 static int keypair_test_run(EVP_TEST
*t
)
2889 const KEYPAIR_TEST_DATA
*pair
= t
->data
;
2891 if (pair
->privk
== NULL
|| pair
->pubk
== NULL
) {
2893 * this can only happen if only one of the keys is not set
2894 * which means that one of them was unsupported while the
2895 * other isn't: hence a key type mismatch.
2897 t
->err
= "KEYPAIR_TYPE_MISMATCH";
2902 if ((rv
= EVP_PKEY_eq(pair
->privk
, pair
->pubk
)) != 1) {
2904 t
->err
= "KEYPAIR_MISMATCH";
2905 } else if (-1 == rv
) {
2906 t
->err
= "KEYPAIR_TYPE_MISMATCH";
2907 } else if (-2 == rv
) {
2908 t
->err
= "UNSUPPORTED_KEY_COMPARISON";
2910 TEST_error("Unexpected error in key comparison");
2925 static const EVP_TEST_METHOD keypair_test_method
= {
2928 keypair_test_cleanup
,
2937 typedef struct keygen_test_data_st
{
2938 EVP_PKEY_CTX
*genctx
; /* Keygen context to use */
2939 char *keyname
; /* Key name to store key or NULL */
2942 static int keygen_test_init(EVP_TEST
*t
, const char *alg
)
2944 KEYGEN_TEST_DATA
*data
;
2945 EVP_PKEY_CTX
*genctx
;
2946 int nid
= OBJ_sn2nid(alg
);
2948 if (nid
== NID_undef
) {
2949 nid
= OBJ_ln2nid(alg
);
2950 if (nid
== NID_undef
)
2954 if (is_pkey_disabled(alg
)) {
2958 if (!TEST_ptr(genctx
= EVP_PKEY_CTX_new_from_name(libctx
, alg
, NULL
)))
2961 if (EVP_PKEY_keygen_init(genctx
) <= 0) {
2962 t
->err
= "KEYGEN_INIT_ERROR";
2966 if (!TEST_ptr(data
= OPENSSL_malloc(sizeof(*data
))))
2968 data
->genctx
= genctx
;
2969 data
->keyname
= NULL
;
2975 EVP_PKEY_CTX_free(genctx
);
2979 static void keygen_test_cleanup(EVP_TEST
*t
)
2981 KEYGEN_TEST_DATA
*keygen
= t
->data
;
2983 EVP_PKEY_CTX_free(keygen
->genctx
);
2984 OPENSSL_free(keygen
->keyname
);
2985 OPENSSL_free(t
->data
);
2989 static int keygen_test_parse(EVP_TEST
*t
,
2990 const char *keyword
, const char *value
)
2992 KEYGEN_TEST_DATA
*keygen
= t
->data
;
2994 if (strcmp(keyword
, "KeyName") == 0)
2995 return TEST_ptr(keygen
->keyname
= OPENSSL_strdup(value
));
2996 if (strcmp(keyword
, "Ctrl") == 0)
2997 return pkey_test_ctrl(t
, keygen
->genctx
, value
);
3001 static int keygen_test_run(EVP_TEST
*t
)
3003 KEYGEN_TEST_DATA
*keygen
= t
->data
;
3004 EVP_PKEY
*pkey
= NULL
;
3007 if (EVP_PKEY_keygen(keygen
->genctx
, &pkey
) <= 0) {
3008 t
->err
= "KEYGEN_GENERATE_ERROR";
3012 if (!evp_pkey_is_provided(pkey
)) {
3013 TEST_info("Warning: legacy key generated %s", keygen
->keyname
);
3016 if (keygen
->keyname
!= NULL
) {
3020 if (find_key(NULL
, keygen
->keyname
, private_keys
)) {
3021 TEST_info("Duplicate key %s", keygen
->keyname
);
3025 if (!TEST_ptr(key
= OPENSSL_malloc(sizeof(*key
))))
3027 key
->name
= keygen
->keyname
;
3028 keygen
->keyname
= NULL
;
3030 key
->next
= private_keys
;
3034 EVP_PKEY_free(pkey
);
3043 static const EVP_TEST_METHOD keygen_test_method
= {
3046 keygen_test_cleanup
,
3052 ** DIGEST SIGN+VERIFY TESTS
3056 int is_verify
; /* Set to 1 if verifying */
3057 int is_oneshot
; /* Set to 1 for one shot operation */
3058 const EVP_MD
*md
; /* Digest to use */
3059 EVP_MD_CTX
*ctx
; /* Digest context */
3061 STACK_OF(EVP_TEST_BUFFER
) *input
; /* Input data: streaming */
3062 unsigned char *osin
; /* Input data if one shot */
3063 size_t osin_len
; /* Input length data if one shot */
3064 unsigned char *output
; /* Expected output */
3065 size_t output_len
; /* Expected output length */
3068 static int digestsigver_test_init(EVP_TEST
*t
, const char *alg
, int is_verify
,
3071 const EVP_MD
*md
= NULL
;
3072 DIGESTSIGN_DATA
*mdat
;
3074 if (strcmp(alg
, "NULL") != 0) {
3075 if (is_digest_disabled(alg
)) {
3079 md
= EVP_get_digestbyname(alg
);
3083 if (!TEST_ptr(mdat
= OPENSSL_zalloc(sizeof(*mdat
))))
3086 if (!TEST_ptr(mdat
->ctx
= EVP_MD_CTX_new())) {
3090 mdat
->is_verify
= is_verify
;
3091 mdat
->is_oneshot
= is_oneshot
;
3096 static int digestsign_test_init(EVP_TEST
*t
, const char *alg
)
3098 return digestsigver_test_init(t
, alg
, 0, 0);
3101 static void digestsigver_test_cleanup(EVP_TEST
*t
)
3103 DIGESTSIGN_DATA
*mdata
= t
->data
;
3105 EVP_MD_CTX_free(mdata
->ctx
);
3106 sk_EVP_TEST_BUFFER_pop_free(mdata
->input
, evp_test_buffer_free
);
3107 OPENSSL_free(mdata
->osin
);
3108 OPENSSL_free(mdata
->output
);
3109 OPENSSL_free(mdata
);
3113 static int digestsigver_test_parse(EVP_TEST
*t
,
3114 const char *keyword
, const char *value
)
3116 DIGESTSIGN_DATA
*mdata
= t
->data
;
3118 if (strcmp(keyword
, "Key") == 0) {
3119 EVP_PKEY
*pkey
= NULL
;
3121 const char *name
= mdata
->md
== NULL
? NULL
: EVP_MD_get0_name(mdata
->md
);
3123 if (mdata
->is_verify
)
3124 rv
= find_key(&pkey
, value
, public_keys
);
3126 rv
= find_key(&pkey
, value
, private_keys
);
3127 if (rv
== 0 || pkey
== NULL
) {
3131 if (mdata
->is_verify
) {
3132 if (!EVP_DigestVerifyInit_ex(mdata
->ctx
, &mdata
->pctx
, name
, libctx
,
3134 t
->err
= "DIGESTVERIFYINIT_ERROR";
3137 if (!EVP_DigestSignInit_ex(mdata
->ctx
, &mdata
->pctx
, name
, libctx
, NULL
,
3139 t
->err
= "DIGESTSIGNINIT_ERROR";
3143 if (strcmp(keyword
, "Input") == 0) {
3144 if (mdata
->is_oneshot
)
3145 return parse_bin(value
, &mdata
->osin
, &mdata
->osin_len
);
3146 return evp_test_buffer_append(value
, &mdata
->input
);
3148 if (strcmp(keyword
, "Output") == 0)
3149 return parse_bin(value
, &mdata
->output
, &mdata
->output_len
);
3151 if (!mdata
->is_oneshot
) {
3152 if (strcmp(keyword
, "Count") == 0)
3153 return evp_test_buffer_set_count(value
, mdata
->input
);
3154 if (strcmp(keyword
, "Ncopy") == 0)
3155 return evp_test_buffer_ncopy(value
, mdata
->input
);
3157 if (strcmp(keyword
, "Ctrl") == 0) {
3158 if (mdata
->pctx
== NULL
)
3160 return pkey_test_ctrl(t
, mdata
->pctx
, value
);
3165 static int digestsign_update_fn(void *ctx
, const unsigned char *buf
,
3168 return EVP_DigestSignUpdate(ctx
, buf
, buflen
);
3171 static int digestsign_test_run(EVP_TEST
*t
)
3173 DIGESTSIGN_DATA
*expected
= t
->data
;
3174 unsigned char *got
= NULL
;
3177 if (!evp_test_buffer_do(expected
->input
, digestsign_update_fn
,
3179 t
->err
= "DIGESTUPDATE_ERROR";
3183 if (!EVP_DigestSignFinal(expected
->ctx
, NULL
, &got_len
)) {
3184 t
->err
= "DIGESTSIGNFINAL_LENGTH_ERROR";
3187 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
3188 t
->err
= "MALLOC_FAILURE";
3191 if (!EVP_DigestSignFinal(expected
->ctx
, got
, &got_len
)) {
3192 t
->err
= "DIGESTSIGNFINAL_ERROR";
3195 if (!memory_err_compare(t
, "SIGNATURE_MISMATCH",
3196 expected
->output
, expected
->output_len
,
3206 static const EVP_TEST_METHOD digestsign_test_method
= {
3208 digestsign_test_init
,
3209 digestsigver_test_cleanup
,
3210 digestsigver_test_parse
,
3214 static int digestverify_test_init(EVP_TEST
*t
, const char *alg
)
3216 return digestsigver_test_init(t
, alg
, 1, 0);
3219 static int digestverify_update_fn(void *ctx
, const unsigned char *buf
,
3222 return EVP_DigestVerifyUpdate(ctx
, buf
, buflen
);
3225 static int digestverify_test_run(EVP_TEST
*t
)
3227 DIGESTSIGN_DATA
*mdata
= t
->data
;
3229 if (!evp_test_buffer_do(mdata
->input
, digestverify_update_fn
, mdata
->ctx
)) {
3230 t
->err
= "DIGESTUPDATE_ERROR";
3234 if (EVP_DigestVerifyFinal(mdata
->ctx
, mdata
->output
,
3235 mdata
->output_len
) <= 0)
3236 t
->err
= "VERIFY_ERROR";
3240 static const EVP_TEST_METHOD digestverify_test_method
= {
3242 digestverify_test_init
,
3243 digestsigver_test_cleanup
,
3244 digestsigver_test_parse
,
3245 digestverify_test_run
3248 static int oneshot_digestsign_test_init(EVP_TEST
*t
, const char *alg
)
3250 return digestsigver_test_init(t
, alg
, 0, 1);
3253 static int oneshot_digestsign_test_run(EVP_TEST
*t
)
3255 DIGESTSIGN_DATA
*expected
= t
->data
;
3256 unsigned char *got
= NULL
;
3259 if (!EVP_DigestSign(expected
->ctx
, NULL
, &got_len
,
3260 expected
->osin
, expected
->osin_len
)) {
3261 t
->err
= "DIGESTSIGN_LENGTH_ERROR";
3264 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
3265 t
->err
= "MALLOC_FAILURE";
3268 if (!EVP_DigestSign(expected
->ctx
, got
, &got_len
,
3269 expected
->osin
, expected
->osin_len
)) {
3270 t
->err
= "DIGESTSIGN_ERROR";
3273 if (!memory_err_compare(t
, "SIGNATURE_MISMATCH",
3274 expected
->output
, expected
->output_len
,
3284 static const EVP_TEST_METHOD oneshot_digestsign_test_method
= {
3285 "OneShotDigestSign",
3286 oneshot_digestsign_test_init
,
3287 digestsigver_test_cleanup
,
3288 digestsigver_test_parse
,
3289 oneshot_digestsign_test_run
3292 static int oneshot_digestverify_test_init(EVP_TEST
*t
, const char *alg
)
3294 return digestsigver_test_init(t
, alg
, 1, 1);
3297 static int oneshot_digestverify_test_run(EVP_TEST
*t
)
3299 DIGESTSIGN_DATA
*mdata
= t
->data
;
3301 if (EVP_DigestVerify(mdata
->ctx
, mdata
->output
, mdata
->output_len
,
3302 mdata
->osin
, mdata
->osin_len
) <= 0)
3303 t
->err
= "VERIFY_ERROR";
3307 static const EVP_TEST_METHOD oneshot_digestverify_test_method
= {
3308 "OneShotDigestVerify",
3309 oneshot_digestverify_test_init
,
3310 digestsigver_test_cleanup
,
3311 digestsigver_test_parse
,
3312 oneshot_digestverify_test_run
3317 ** PARSING AND DISPATCH
3320 static const EVP_TEST_METHOD
*evp_test_list
[] = {
3322 &cipher_test_method
,
3323 &digest_test_method
,
3324 &digestsign_test_method
,
3325 &digestverify_test_method
,
3326 &encode_test_method
,
3328 &pkey_kdf_test_method
,
3329 &keypair_test_method
,
3330 &keygen_test_method
,
3332 &oneshot_digestsign_test_method
,
3333 &oneshot_digestverify_test_method
,
3335 &pdecrypt_test_method
,
3336 &pderive_test_method
,
3338 &pverify_recover_test_method
,
3339 &pverify_test_method
,
3343 static const EVP_TEST_METHOD
*find_test(const char *name
)
3345 const EVP_TEST_METHOD
**tt
;
3347 for (tt
= evp_test_list
; *tt
; tt
++) {
3348 if (strcmp(name
, (*tt
)->name
) == 0)
3354 static void clear_test(EVP_TEST
*t
)
3356 test_clearstanza(&t
->s
);
3358 if (t
->data
!= NULL
) {
3359 if (t
->meth
!= NULL
)
3360 t
->meth
->cleanup(t
);
3361 OPENSSL_free(t
->data
);
3364 OPENSSL_free(t
->expected_err
);
3365 t
->expected_err
= NULL
;
3366 OPENSSL_free(t
->reason
);
3375 /* Check for errors in the test structure; return 1 if okay, else 0. */
3376 static int check_test_error(EVP_TEST
*t
)
3381 if (t
->err
== NULL
&& t
->expected_err
== NULL
)
3383 if (t
->err
!= NULL
&& t
->expected_err
== NULL
) {
3384 if (t
->aux_err
!= NULL
) {
3385 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
3386 t
->s
.test_file
, t
->s
.start
, t
->aux_err
, t
->err
);
3388 TEST_info("%s:%d: Source of above error; unexpected error %s",
3389 t
->s
.test_file
, t
->s
.start
, t
->err
);
3393 if (t
->err
== NULL
&& t
->expected_err
!= NULL
) {
3394 TEST_info("%s:%d: Succeeded but was expecting %s",
3395 t
->s
.test_file
, t
->s
.start
, t
->expected_err
);
3399 if (strcmp(t
->err
, t
->expected_err
) != 0) {
3400 TEST_info("%s:%d: Expected %s got %s",
3401 t
->s
.test_file
, t
->s
.start
, t
->expected_err
, t
->err
);
3405 if (t
->reason
== NULL
)
3408 if (t
->reason
== NULL
) {
3409 TEST_info("%s:%d: Test is missing function or reason code",
3410 t
->s
.test_file
, t
->s
.start
);
3414 err
= ERR_peek_error();
3416 TEST_info("%s:%d: Expected error \"%s\" not set",
3417 t
->s
.test_file
, t
->s
.start
, t
->reason
);
3421 reason
= ERR_reason_error_string(err
);
3422 if (reason
== NULL
) {
3423 TEST_info("%s:%d: Expected error \"%s\", no strings available."
3425 t
->s
.test_file
, t
->s
.start
, t
->reason
);
3429 if (strcmp(reason
, t
->reason
) == 0)
3432 TEST_info("%s:%d: Expected error \"%s\", got \"%s\"",
3433 t
->s
.test_file
, t
->s
.start
, t
->reason
, reason
);
3438 /* Run a parsed test. Log a message and return 0 on error. */
3439 static int run_test(EVP_TEST
*t
)
3441 if (t
->meth
== NULL
)
3448 if (t
->err
== NULL
&& t
->meth
->run_test(t
) != 1) {
3449 TEST_info("%s:%d %s error",
3450 t
->s
.test_file
, t
->s
.start
, t
->meth
->name
);
3453 if (!check_test_error(t
)) {
3454 TEST_openssl_errors();
3463 static int find_key(EVP_PKEY
**ppk
, const char *name
, KEY_LIST
*lst
)
3465 for (; lst
!= NULL
; lst
= lst
->next
) {
3466 if (strcmp(lst
->name
, name
) == 0) {
3475 static void free_key_list(KEY_LIST
*lst
)
3477 while (lst
!= NULL
) {
3478 KEY_LIST
*next
= lst
->next
;
3480 EVP_PKEY_free(lst
->key
);
3481 OPENSSL_free(lst
->name
);
3488 * Is the key type an unsupported algorithm?
3490 static int key_unsupported(void)
3492 long err
= ERR_peek_last_error();
3493 int lib
= ERR_GET_LIB(err
);
3494 long reason
= ERR_GET_REASON(err
);
3496 if ((lib
== ERR_LIB_EVP
&& reason
== EVP_R_UNSUPPORTED_ALGORITHM
)
3497 || (lib
== ERR_LIB_EVP
&& reason
== EVP_R_DECODE_ERROR
)
3498 || reason
== ERR_R_UNSUPPORTED
) {
3502 #ifndef OPENSSL_NO_EC
3504 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
3505 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
3508 if (lib
== ERR_LIB_EC
3509 && (reason
== EC_R_UNKNOWN_GROUP
3510 || reason
== EC_R_INVALID_CURVE
)) {
3514 #endif /* OPENSSL_NO_EC */
3518 /* NULL out the value from |pp| but return it. This "steals" a pointer. */
3519 static char *take_value(PAIR
*pp
)
3521 char *p
= pp
->value
;
3527 #if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
3528 static int securitycheck_enabled(void)
3530 static int enabled
= -1;
3532 if (enabled
== -1) {
3533 if (OSSL_PROVIDER_available(libctx
, "fips")) {
3534 OSSL_PARAM params
[2];
3535 OSSL_PROVIDER
*prov
= NULL
;
3538 prov
= OSSL_PROVIDER_load(libctx
, "fips");
3541 OSSL_PARAM_construct_int(OSSL_PROV_PARAM_SECURITY_CHECKS
,
3543 params
[1] = OSSL_PARAM_construct_end();
3544 OSSL_PROVIDER_get_params(prov
, params
);
3545 OSSL_PROVIDER_unload(prov
);
3557 * Return 1 if one of the providers named in the string is available.
3558 * The provider names are separated with whitespace.
3559 * NOTE: destructive function, it inserts '\0' after each provider name.
3561 static int prov_available(char *providers
)
3567 for (; isspace(*providers
); providers
++)
3569 if (*providers
== '\0')
3570 break; /* End of the road */
3571 for (p
= providers
; *p
!= '\0' && !isspace(*p
); p
++)
3577 if (OSSL_PROVIDER_available(libctx
, providers
))
3578 return 1; /* Found one */
3583 /* Read and parse one test. Return 0 if failure, 1 if okay. */
3584 static int parse(EVP_TEST
*t
)
3586 KEY_LIST
*key
, **klist
;
3589 int i
, skip_availablein
= 0;
3593 if (BIO_eof(t
->s
.fp
))
3596 if (!test_readstanza(&t
->s
))
3598 } while (t
->s
.numpairs
== 0);
3599 pp
= &t
->s
.pairs
[0];
3601 /* Are we adding a key? */
3605 if (strcmp(pp
->key
, "PrivateKey") == 0) {
3606 pkey
= PEM_read_bio_PrivateKey_ex(t
->s
.key
, NULL
, 0, NULL
, libctx
, NULL
);
3607 if (pkey
== NULL
&& !key_unsupported()) {
3608 EVP_PKEY_free(pkey
);
3609 TEST_info("Can't read private key %s", pp
->value
);
3610 TEST_openssl_errors();
3613 klist
= &private_keys
;
3614 } else if (strcmp(pp
->key
, "PublicKey") == 0) {
3615 pkey
= PEM_read_bio_PUBKEY_ex(t
->s
.key
, NULL
, 0, NULL
, libctx
, NULL
);
3616 if (pkey
== NULL
&& !key_unsupported()) {
3617 EVP_PKEY_free(pkey
);
3618 TEST_info("Can't read public key %s", pp
->value
);
3619 TEST_openssl_errors();
3622 klist
= &public_keys
;
3623 } else if (strcmp(pp
->key
, "PrivateKeyRaw") == 0
3624 || strcmp(pp
->key
, "PublicKeyRaw") == 0) {
3625 char *strnid
= NULL
, *keydata
= NULL
;
3626 unsigned char *keybin
;
3630 if (strcmp(pp
->key
, "PrivateKeyRaw") == 0)
3631 klist
= &private_keys
;
3633 klist
= &public_keys
;
3635 strnid
= strchr(pp
->value
, ':');
3636 if (strnid
!= NULL
) {
3638 keydata
= strchr(strnid
, ':');
3639 if (keydata
!= NULL
)
3642 if (keydata
== NULL
) {
3643 TEST_info("Failed to parse %s value", pp
->key
);
3647 nid
= OBJ_txt2nid(strnid
);
3648 if (nid
== NID_undef
) {
3649 TEST_info("Unrecognised algorithm NID");
3652 if (!parse_bin(keydata
, &keybin
, &keylen
)) {
3653 TEST_info("Failed to create binary key");
3656 if (klist
== &private_keys
)
3657 pkey
= EVP_PKEY_new_raw_private_key_ex(libctx
, strnid
, NULL
, keybin
,
3660 pkey
= EVP_PKEY_new_raw_public_key_ex(libctx
, strnid
, NULL
, keybin
,
3662 if (pkey
== NULL
&& !key_unsupported()) {
3663 TEST_info("Can't read %s data", pp
->key
);
3664 OPENSSL_free(keybin
);
3665 TEST_openssl_errors();
3668 OPENSSL_free(keybin
);
3669 } else if (strcmp(pp
->key
, "Availablein") == 0) {
3670 if (!prov_available(pp
->value
)) {
3671 TEST_info("skipping, '%s' provider not available: %s:%d",
3672 pp
->value
, t
->s
.test_file
, t
->s
.start
);
3681 /* If we have a key add to list */
3682 if (klist
!= NULL
) {
3683 if (find_key(NULL
, pp
->value
, *klist
)) {
3684 TEST_info("Duplicate key %s", pp
->value
);
3687 if (!TEST_ptr(key
= OPENSSL_malloc(sizeof(*key
))))
3689 key
->name
= take_value(pp
);
3694 /* Go back and start a new stanza. */
3695 if ((t
->s
.numpairs
- skip_availablein
) != 1)
3696 TEST_info("Line %d: missing blank line\n", t
->s
.curr
);
3700 /* Find the test, based on first keyword. */
3701 if (!TEST_ptr(t
->meth
= find_test(pp
->key
)))
3703 if (!t
->meth
->init(t
, pp
->value
)) {
3704 TEST_error("unknown %s: %s\n", pp
->key
, pp
->value
);
3708 /* TEST_info("skipping %s %s", pp->key, pp->value); */
3712 for (pp
++, i
= 1; i
< (t
->s
.numpairs
- skip_availablein
); pp
++, i
++) {
3713 if (strcmp(pp
->key
, "Securitycheck") == 0) {
3714 #if defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
3716 if (!securitycheck_enabled())
3719 TEST_info("skipping, Securitycheck is disabled: %s:%d",
3720 t
->s
.test_file
, t
->s
.start
);
3724 } else if (strcmp(pp
->key
, "Availablein") == 0) {
3725 TEST_info("Line %d: 'Availablein' should be the first option",
3728 } else if (strcmp(pp
->key
, "Result") == 0) {
3729 if (t
->expected_err
!= NULL
) {
3730 TEST_info("Line %d: multiple result lines", t
->s
.curr
);
3733 t
->expected_err
= take_value(pp
);
3734 } else if (strcmp(pp
->key
, "Function") == 0) {
3735 /* Ignore old line. */
3736 } else if (strcmp(pp
->key
, "Reason") == 0) {
3737 if (t
->reason
!= NULL
) {
3738 TEST_info("Line %d: multiple reason lines", t
->s
.curr
);
3741 t
->reason
= take_value(pp
);
3743 /* Must be test specific line: try to parse it */
3744 int rv
= t
->meth
->parse(t
, pp
->key
, pp
->value
);
3747 TEST_info("Line %d: unknown keyword %s", t
->s
.curr
, pp
->key
);
3751 TEST_info("Line %d: error processing keyword %s = %s\n",
3752 t
->s
.curr
, pp
->key
, pp
->value
);
3761 static int run_file_tests(int i
)
3764 const char *testfile
= test_get_argument(i
);
3767 if (!TEST_ptr(t
= OPENSSL_zalloc(sizeof(*t
))))
3769 if (!test_start_file(&t
->s
, testfile
)) {
3774 while (!BIO_eof(t
->s
.fp
)) {
3780 if (c
== 0 || !run_test(t
)) {
3785 test_end_file(&t
->s
);
3788 free_key_list(public_keys
);
3789 free_key_list(private_keys
);
3796 const OPTIONS
*test_get_options(void)
3798 static const OPTIONS test_options
[] = {
3799 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
3800 { "config", OPT_CONFIG_FILE
, '<',
3801 "The configuration file to use for the libctx" },
3802 { OPT_HELP_STR
, 1, '-', "file\tFile to run tests on.\n" },
3805 return test_options
;
3808 int setup_tests(void)
3811 char *config_file
= NULL
;
3815 while ((o
= opt_next()) != OPT_EOF
) {
3817 case OPT_CONFIG_FILE
:
3818 config_file
= opt_arg();
3820 case OPT_TEST_CASES
:
3829 * Load the provider via configuration into the created library context.
3830 * Load the 'null' provider into the default library context to ensure that
3831 * the tests do not fallback to using the default provider.
3833 if (!test_get_libctx(&libctx
, &prov_null
, config_file
, NULL
, NULL
))
3836 n
= test_get_argument_count();
3840 ADD_ALL_TESTS(run_file_tests
, n
);
3844 void cleanup_tests(void)
3846 OSSL_PROVIDER_unload(prov_null
);
3847 OSSL_LIB_CTX_free(libctx
);
3850 static int is_digest_disabled(const char *name
)
3852 #ifdef OPENSSL_NO_BLAKE2
3853 if (HAS_CASE_PREFIX(name
, "BLAKE"))
3856 #ifdef OPENSSL_NO_MD2
3857 if (strcasecmp(name
, "MD2") == 0)
3860 #ifdef OPENSSL_NO_MDC2
3861 if (strcasecmp(name
, "MDC2") == 0)
3864 #ifdef OPENSSL_NO_MD4
3865 if (strcasecmp(name
, "MD4") == 0)
3868 #ifdef OPENSSL_NO_MD5
3869 if (strcasecmp(name
, "MD5") == 0)
3872 #ifdef OPENSSL_NO_RMD160
3873 if (strcasecmp(name
, "RIPEMD160") == 0)
3876 #ifdef OPENSSL_NO_SM3
3877 if (strcasecmp(name
, "SM3") == 0)
3880 #ifdef OPENSSL_NO_WHIRLPOOL
3881 if (strcasecmp(name
, "WHIRLPOOL") == 0)
3887 static int is_pkey_disabled(const char *name
)
3889 #ifdef OPENSSL_NO_EC
3890 if (HAS_CASE_PREFIX(name
, "EC"))
3893 #ifdef OPENSSL_NO_DH
3894 if (HAS_CASE_PREFIX(name
, "DH"))
3897 #ifdef OPENSSL_NO_DSA
3898 if (HAS_CASE_PREFIX(name
, "DSA"))
3904 static int is_mac_disabled(const char *name
)
3906 #ifdef OPENSSL_NO_BLAKE2
3907 if (HAS_CASE_PREFIX(name
, "BLAKE2BMAC")
3908 || HAS_CASE_PREFIX(name
, "BLAKE2SMAC"))
3911 #ifdef OPENSSL_NO_CMAC
3912 if (HAS_CASE_PREFIX(name
, "CMAC"))
3915 #ifdef OPENSSL_NO_POLY1305
3916 if (HAS_CASE_PREFIX(name
, "Poly1305"))
3919 #ifdef OPENSSL_NO_SIPHASH
3920 if (HAS_CASE_PREFIX(name
, "SipHash"))
3925 static int is_kdf_disabled(const char *name
)
3927 #ifdef OPENSSL_NO_SCRYPT
3928 if (HAS_CASE_SUFFIX(name
, "SCRYPT"))
3934 static int is_cipher_disabled(const char *name
)
3936 #ifdef OPENSSL_NO_ARIA
3937 if (HAS_CASE_PREFIX(name
, "ARIA"))
3940 #ifdef OPENSSL_NO_BF
3941 if (HAS_CASE_PREFIX(name
, "BF"))
3944 #ifdef OPENSSL_NO_CAMELLIA
3945 if (HAS_CASE_PREFIX(name
, "CAMELLIA"))
3948 #ifdef OPENSSL_NO_CAST
3949 if (HAS_CASE_PREFIX(name
, "CAST"))
3952 #ifdef OPENSSL_NO_CHACHA
3953 if (HAS_CASE_PREFIX(name
, "CHACHA"))
3956 #ifdef OPENSSL_NO_POLY1305
3957 if (HAS_CASE_SUFFIX(name
, "Poly1305"))
3960 #ifdef OPENSSL_NO_DES
3961 if (HAS_CASE_PREFIX(name
, "DES"))
3963 if (HAS_CASE_SUFFIX(name
, "3DESwrap"))
3966 #ifdef OPENSSL_NO_OCB
3967 if (HAS_CASE_SUFFIX(name
, "OCB"))
3970 #ifdef OPENSSL_NO_IDEA
3971 if (HAS_CASE_PREFIX(name
, "IDEA"))
3974 #ifdef OPENSSL_NO_RC2
3975 if (HAS_CASE_PREFIX(name
, "RC2"))
3978 #ifdef OPENSSL_NO_RC4
3979 if (HAS_CASE_PREFIX(name
, "RC4"))
3982 #ifdef OPENSSL_NO_RC5
3983 if (HAS_CASE_PREFIX(name
, "RC5"))
3986 #ifdef OPENSSL_NO_SEED
3987 if (HAS_CASE_PREFIX(name
, "SEED"))
3990 #ifdef OPENSSL_NO_SIV
3991 if (HAS_CASE_SUFFIX(name
, "SIV"))
3994 #ifdef OPENSSL_NO_SM4
3995 if (HAS_CASE_PREFIX(name
, "SM4"))