2 * Copyright 2015-2019 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
14 #include <openssl/evp.h>
15 #include <openssl/pem.h>
16 #include <openssl/err.h>
17 #include <openssl/provider.h>
18 #include <openssl/x509v3.h>
19 #include <openssl/pkcs12.h>
20 #include <openssl/kdf.h>
21 #include <openssl/params.h>
22 #include <openssl/core_names.h>
23 #include "internal/numbers.h"
24 #include "internal/nelem.h"
30 typedef struct evp_test_method_st EVP_TEST_METHOD
;
33 * Structure holding test information
35 typedef struct evp_test_st
{
36 STANZA s
; /* Common test stanza */
38 int skip
; /* Current test should be skipped */
39 const EVP_TEST_METHOD
*meth
; /* method for this test */
40 const char *err
, *aux_err
; /* Error string for test */
41 char *expected_err
; /* Expected error value of test */
42 char *reason
; /* Expected error reason string */
43 void *data
; /* test specific data */
47 * Test method structure
49 struct evp_test_method_st
{
50 /* Name of test as it appears in file */
52 /* Initialise test for "alg" */
53 int (*init
) (EVP_TEST
* t
, const char *alg
);
55 void (*cleanup
) (EVP_TEST
* t
);
56 /* Test specific name value pair processing */
57 int (*parse
) (EVP_TEST
* t
, const char *name
, const char *value
);
58 /* Run the test itself */
59 int (*run_test
) (EVP_TEST
* t
);
64 * Linked list of named keys.
66 typedef struct key_list_st
{
69 struct key_list_st
*next
;
73 * List of public and private keys
75 static KEY_LIST
*private_keys
;
76 static KEY_LIST
*public_keys
;
77 static int find_key(EVP_PKEY
**ppk
, const char *name
, KEY_LIST
*lst
);
79 static int parse_bin(const char *value
, unsigned char **buf
, size_t *buflen
);
82 * Compare two memory regions for equality, returning zero if they differ.
83 * However, if there is expected to be an error and the actual error
84 * matches then the memory is expected to be different so handle this
85 * case without producing unnecessary test framework output.
87 static int memory_err_compare(EVP_TEST
*t
, const char *err
,
88 const void *expected
, size_t expected_len
,
89 const void *got
, size_t got_len
)
93 if (t
->expected_err
!= NULL
&& strcmp(t
->expected_err
, err
) == 0)
94 r
= !TEST_mem_ne(expected
, expected_len
, got
, got_len
);
96 r
= TEST_mem_eq(expected
, expected_len
, got
, got_len
);
103 * Structure used to hold a list of blocks of memory to test
104 * calls to "update" like functions.
106 struct evp_test_buffer_st
{
113 static void evp_test_buffer_free(EVP_TEST_BUFFER
*db
)
116 OPENSSL_free(db
->buf
);
122 * append buffer to a list
124 static int evp_test_buffer_append(const char *value
,
125 STACK_OF(EVP_TEST_BUFFER
) **sk
)
127 EVP_TEST_BUFFER
*db
= NULL
;
129 if (!TEST_ptr(db
= OPENSSL_malloc(sizeof(*db
))))
132 if (!parse_bin(value
, &db
->buf
, &db
->buflen
))
137 if (*sk
== NULL
&& !TEST_ptr(*sk
= sk_EVP_TEST_BUFFER_new_null()))
139 if (!sk_EVP_TEST_BUFFER_push(*sk
, db
))
145 evp_test_buffer_free(db
);
150 * replace last buffer in list with copies of itself
152 static int evp_test_buffer_ncopy(const char *value
,
153 STACK_OF(EVP_TEST_BUFFER
) *sk
)
156 unsigned char *tbuf
, *p
;
158 int ncopy
= atoi(value
);
163 if (sk
== NULL
|| sk_EVP_TEST_BUFFER_num(sk
) == 0)
165 db
= sk_EVP_TEST_BUFFER_value(sk
, sk_EVP_TEST_BUFFER_num(sk
) - 1);
167 tbuflen
= db
->buflen
* ncopy
;
168 if (!TEST_ptr(tbuf
= OPENSSL_malloc(tbuflen
)))
170 for (i
= 0, p
= tbuf
; i
< ncopy
; i
++, p
+= db
->buflen
)
171 memcpy(p
, db
->buf
, db
->buflen
);
173 OPENSSL_free(db
->buf
);
175 db
->buflen
= tbuflen
;
180 * set repeat count for last buffer in list
182 static int evp_test_buffer_set_count(const char *value
,
183 STACK_OF(EVP_TEST_BUFFER
) *sk
)
186 int count
= atoi(value
);
191 if (sk
== NULL
|| sk_EVP_TEST_BUFFER_num(sk
) == 0)
194 db
= sk_EVP_TEST_BUFFER_value(sk
, sk_EVP_TEST_BUFFER_num(sk
) - 1);
195 if (db
->count_set
!= 0)
198 db
->count
= (size_t)count
;
204 * call "fn" with each element of the list in turn
206 static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER
) *sk
,
208 const unsigned char *buf
,
214 for (i
= 0; i
< sk_EVP_TEST_BUFFER_num(sk
); i
++) {
215 EVP_TEST_BUFFER
*tb
= sk_EVP_TEST_BUFFER_value(sk
, i
);
218 for (j
= 0; j
< tb
->count
; j
++) {
219 if (fn(ctx
, tb
->buf
, tb
->buflen
) <= 0)
227 * Unescape some sequences in string literals (only \n for now).
228 * Return an allocated buffer, set |out_len|. If |input_len|
229 * is zero, get an empty buffer but set length to zero.
231 static unsigned char* unescape(const char *input
, size_t input_len
,
234 unsigned char *ret
, *p
;
237 if (input_len
== 0) {
239 return OPENSSL_zalloc(1);
242 /* Escaping is non-expanding; over-allocate original size for simplicity. */
243 if (!TEST_ptr(ret
= p
= OPENSSL_malloc(input_len
)))
246 for (i
= 0; i
< input_len
; i
++) {
247 if (*input
== '\\') {
248 if (i
== input_len
- 1 || *++input
!= 'n') {
249 TEST_error("Bad escape sequence in file");
269 * For a hex string "value" convert to a binary allocated buffer.
270 * Return 1 on success or 0 on failure.
272 static int parse_bin(const char *value
, unsigned char **buf
, size_t *buflen
)
276 /* Check for NULL literal */
277 if (strcmp(value
, "NULL") == 0) {
283 /* Check for empty value */
284 if (*value
== '\0') {
286 * Don't return NULL for zero length buffer. This is needed for
287 * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key
288 * buffer even if the key length is 0, in order to detect key reset.
290 *buf
= OPENSSL_malloc(1);
298 /* Check for string literal */
299 if (value
[0] == '"') {
300 size_t vlen
= strlen(++value
);
302 if (vlen
== 0 || value
[vlen
- 1] != '"')
305 *buf
= unescape(value
, vlen
, buflen
);
306 return *buf
== NULL
? 0 : 1;
309 /* Otherwise assume as hex literal and convert it to binary buffer */
310 if (!TEST_ptr(*buf
= OPENSSL_hexstr2buf(value
, &len
))) {
311 TEST_info("Can't convert %s", value
);
312 TEST_openssl_errors();
315 /* Size of input buffer means we'll never overflow */
322 *** MESSAGE DIGEST TESTS
325 typedef struct digest_data_st
{
326 /* Digest this test is for */
327 const EVP_MD
*digest
;
328 EVP_MD
*fetched_digest
;
329 /* Input to digest */
330 STACK_OF(EVP_TEST_BUFFER
) *input
;
331 /* Expected output */
332 unsigned char *output
;
338 static int digest_test_init(EVP_TEST
*t
, const char *alg
)
341 const EVP_MD
*digest
;
342 EVP_MD
*fetched_digest
;
344 if ((digest
= fetched_digest
= EVP_MD_fetch(NULL
, alg
, NULL
)) == NULL
345 && (digest
= EVP_get_digestbyname(alg
)) == NULL
) {
346 /* If alg has an OID assume disabled algorithm */
347 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
353 if (!TEST_ptr(mdat
= OPENSSL_zalloc(sizeof(*mdat
))))
356 mdat
->digest
= digest
;
357 mdat
->fetched_digest
= fetched_digest
;
359 if (fetched_digest
!= NULL
)
360 TEST_info("%s is fetched", alg
);
364 static void digest_test_cleanup(EVP_TEST
*t
)
366 DIGEST_DATA
*mdat
= t
->data
;
368 sk_EVP_TEST_BUFFER_pop_free(mdat
->input
, evp_test_buffer_free
);
369 OPENSSL_free(mdat
->output
);
370 EVP_MD_meth_free(mdat
->fetched_digest
);
373 static int digest_test_parse(EVP_TEST
*t
,
374 const char *keyword
, const char *value
)
376 DIGEST_DATA
*mdata
= t
->data
;
378 if (strcmp(keyword
, "Input") == 0)
379 return evp_test_buffer_append(value
, &mdata
->input
);
380 if (strcmp(keyword
, "Output") == 0)
381 return parse_bin(value
, &mdata
->output
, &mdata
->output_len
);
382 if (strcmp(keyword
, "Count") == 0)
383 return evp_test_buffer_set_count(value
, mdata
->input
);
384 if (strcmp(keyword
, "Ncopy") == 0)
385 return evp_test_buffer_ncopy(value
, mdata
->input
);
386 if (strcmp(keyword
, "Padding") == 0)
387 return (mdata
->pad_type
= atoi(value
)) > 0;
391 static int digest_update_fn(void *ctx
, const unsigned char *buf
, size_t buflen
)
393 return EVP_DigestUpdate(ctx
, buf
, buflen
);
396 static int digest_test_run(EVP_TEST
*t
)
398 DIGEST_DATA
*expected
= t
->data
;
400 unsigned char *got
= NULL
;
401 unsigned int got_len
;
402 OSSL_PARAM params
[2];
404 t
->err
= "TEST_FAILURE";
405 if (!TEST_ptr(mctx
= EVP_MD_CTX_new()))
408 got
= OPENSSL_malloc(expected
->output_len
> EVP_MAX_MD_SIZE
?
409 expected
->output_len
: EVP_MAX_MD_SIZE
);
413 if (!EVP_DigestInit_ex(mctx
, expected
->digest
, NULL
)) {
414 t
->err
= "DIGESTINIT_ERROR";
417 if (expected
->pad_type
> 0) {
418 params
[0] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_PAD_TYPE
,
419 &expected
->pad_type
);
420 params
[1] = OSSL_PARAM_construct_end();
421 if (!TEST_int_gt(EVP_MD_CTX_set_params(mctx
, params
), 0)) {
422 t
->err
= "PARAMS_ERROR";
426 if (!evp_test_buffer_do(expected
->input
, digest_update_fn
, mctx
)) {
427 t
->err
= "DIGESTUPDATE_ERROR";
431 if (EVP_MD_flags(expected
->digest
) & EVP_MD_FLAG_XOF
) {
432 EVP_MD_CTX
*mctx_cpy
;
433 char dont
[] = "touch";
435 if (!TEST_ptr(mctx_cpy
= EVP_MD_CTX_new())) {
438 if (!EVP_MD_CTX_copy(mctx_cpy
, mctx
)) {
439 EVP_MD_CTX_free(mctx_cpy
);
442 if (!EVP_DigestFinalXOF(mctx_cpy
, (unsigned char *)dont
, 0)) {
443 EVP_MD_CTX_free(mctx_cpy
);
444 t
->err
= "DIGESTFINALXOF_ERROR";
447 if (!TEST_str_eq(dont
, "touch")) {
448 EVP_MD_CTX_free(mctx_cpy
);
449 t
->err
= "DIGESTFINALXOF_ERROR";
452 EVP_MD_CTX_free(mctx_cpy
);
454 got_len
= expected
->output_len
;
455 if (!EVP_DigestFinalXOF(mctx
, got
, got_len
)) {
456 t
->err
= "DIGESTFINALXOF_ERROR";
460 if (!EVP_DigestFinal(mctx
, got
, &got_len
)) {
461 t
->err
= "DIGESTFINAL_ERROR";
465 if (!TEST_int_eq(expected
->output_len
, got_len
)) {
466 t
->err
= "DIGEST_LENGTH_MISMATCH";
469 if (!memory_err_compare(t
, "DIGEST_MISMATCH",
470 expected
->output
, expected
->output_len
,
478 EVP_MD_CTX_free(mctx
);
482 static const EVP_TEST_METHOD digest_test_method
= {
495 typedef struct cipher_data_st
{
496 const EVP_CIPHER
*cipher
;
497 EVP_CIPHER
*fetched_cipher
;
499 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
503 size_t key_bits
; /* Used by RC2 */
507 unsigned char *plaintext
;
508 size_t plaintext_len
;
509 unsigned char *ciphertext
;
510 size_t ciphertext_len
;
511 /* GCM, CCM, OCB and SIV only */
512 unsigned char *aad
[AAD_NUM
];
513 size_t aad_len
[AAD_NUM
];
519 static int cipher_test_init(EVP_TEST
*t
, const char *alg
)
521 const EVP_CIPHER
*cipher
;
522 EVP_CIPHER
*fetched_cipher
;
526 if ((cipher
= fetched_cipher
= EVP_CIPHER_fetch(NULL
, alg
, NULL
)) == NULL
527 && (cipher
= EVP_get_cipherbyname(alg
)) == NULL
) {
528 /* If alg has an OID assume disabled algorithm */
529 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
535 cdat
= OPENSSL_zalloc(sizeof(*cdat
));
536 cdat
->cipher
= cipher
;
537 cdat
->fetched_cipher
= fetched_cipher
;
539 m
= EVP_CIPHER_mode(cipher
);
540 if (m
== EVP_CIPH_GCM_MODE
541 || m
== EVP_CIPH_OCB_MODE
542 || m
== EVP_CIPH_SIV_MODE
543 || m
== EVP_CIPH_CCM_MODE
)
545 else if (EVP_CIPHER_flags(cipher
) & EVP_CIPH_FLAG_AEAD_CIPHER
)
551 if (fetched_cipher
!= NULL
)
552 TEST_info("%s is fetched", alg
);
556 static void cipher_test_cleanup(EVP_TEST
*t
)
559 CIPHER_DATA
*cdat
= t
->data
;
561 OPENSSL_free(cdat
->key
);
562 OPENSSL_free(cdat
->iv
);
563 OPENSSL_free(cdat
->ciphertext
);
564 OPENSSL_free(cdat
->plaintext
);
565 for (i
= 0; i
< AAD_NUM
; i
++)
566 OPENSSL_free(cdat
->aad
[i
]);
567 OPENSSL_free(cdat
->tag
);
568 EVP_CIPHER_meth_free(cdat
->fetched_cipher
);
571 static int cipher_test_parse(EVP_TEST
*t
, const char *keyword
,
574 CIPHER_DATA
*cdat
= t
->data
;
577 if (strcmp(keyword
, "Key") == 0)
578 return parse_bin(value
, &cdat
->key
, &cdat
->key_len
);
579 if (strcmp(keyword
, "Rounds") == 0) {
583 cdat
->rounds
= (unsigned int)i
;
586 if (strcmp(keyword
, "IV") == 0)
587 return parse_bin(value
, &cdat
->iv
, &cdat
->iv_len
);
588 if (strcmp(keyword
, "Plaintext") == 0)
589 return parse_bin(value
, &cdat
->plaintext
, &cdat
->plaintext_len
);
590 if (strcmp(keyword
, "Ciphertext") == 0)
591 return parse_bin(value
, &cdat
->ciphertext
, &cdat
->ciphertext_len
);
592 if (strcmp(keyword
, "KeyBits") == 0) {
596 cdat
->key_bits
= (size_t)i
;
600 if (strcmp(keyword
, "AAD") == 0) {
601 for (i
= 0; i
< AAD_NUM
; i
++) {
602 if (cdat
->aad
[i
] == NULL
)
603 return parse_bin(value
, &cdat
->aad
[i
], &cdat
->aad_len
[i
]);
607 if (strcmp(keyword
, "Tag") == 0)
608 return parse_bin(value
, &cdat
->tag
, &cdat
->tag_len
);
609 if (strcmp(keyword
, "SetTagLate") == 0) {
610 if (strcmp(value
, "TRUE") == 0)
612 else if (strcmp(value
, "FALSE") == 0)
620 if (strcmp(keyword
, "Operation") == 0) {
621 if (strcmp(value
, "ENCRYPT") == 0)
623 else if (strcmp(value
, "DECRYPT") == 0)
632 static int cipher_test_enc(EVP_TEST
*t
, int enc
,
633 size_t out_misalign
, size_t inp_misalign
, int frag
)
635 CIPHER_DATA
*expected
= t
->data
;
636 unsigned char *in
, *expected_out
, *tmp
= NULL
;
637 size_t in_len
, out_len
, donelen
= 0;
638 int ok
= 0, tmplen
, chunklen
, tmpflen
, i
;
639 EVP_CIPHER_CTX
*ctx_base
= NULL
;
640 EVP_CIPHER_CTX
*ctx
= NULL
;
642 t
->err
= "TEST_FAILURE";
643 if (!TEST_ptr(ctx_base
= EVP_CIPHER_CTX_new()))
645 if (!TEST_ptr(ctx
= EVP_CIPHER_CTX_new()))
647 EVP_CIPHER_CTX_set_flags(ctx_base
, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
);
649 in
= expected
->plaintext
;
650 in_len
= expected
->plaintext_len
;
651 expected_out
= expected
->ciphertext
;
652 out_len
= expected
->ciphertext_len
;
654 in
= expected
->ciphertext
;
655 in_len
= expected
->ciphertext_len
;
656 expected_out
= expected
->plaintext
;
657 out_len
= expected
->plaintext_len
;
659 if (inp_misalign
== (size_t)-1) {
661 * Exercise in-place encryption
663 tmp
= OPENSSL_malloc(out_misalign
+ in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
);
666 in
= memcpy(tmp
+ out_misalign
, in
, in_len
);
668 inp_misalign
+= 16 - ((out_misalign
+ in_len
) & 15);
670 * 'tmp' will store both output and copy of input. We make the copy
671 * of input to specifically aligned part of 'tmp'. So we just
672 * figured out how much padding would ensure the required alignment,
673 * now we allocate extended buffer and finally copy the input just
674 * past inp_misalign in expression below. Output will be written
675 * past out_misalign...
677 tmp
= OPENSSL_malloc(out_misalign
+ in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
+
678 inp_misalign
+ in_len
);
681 in
= memcpy(tmp
+ out_misalign
+ in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
+
682 inp_misalign
, in
, in_len
);
684 if (!EVP_CipherInit_ex(ctx_base
, expected
->cipher
, NULL
, NULL
, NULL
, enc
)) {
685 t
->err
= "CIPHERINIT_ERROR";
689 if (expected
->aead
) {
690 if (!EVP_CIPHER_CTX_ctrl(ctx_base
, EVP_CTRL_AEAD_SET_IVLEN
,
691 expected
->iv_len
, 0)) {
692 t
->err
= "INVALID_IV_LENGTH";
695 } else if (expected
->iv_len
!= (size_t)EVP_CIPHER_CTX_iv_length(ctx_base
)) {
696 t
->err
= "INVALID_IV_LENGTH";
700 if (expected
->aead
) {
703 * If encrypting or OCB just set tag length initially, otherwise
704 * set tag length and value.
706 if (enc
|| expected
->aead
== EVP_CIPH_OCB_MODE
|| expected
->tag_late
) {
707 t
->err
= "TAG_LENGTH_SET_ERROR";
710 t
->err
= "TAG_SET_ERROR";
713 if (tag
|| expected
->aead
!= EVP_CIPH_GCM_MODE
) {
714 if (!EVP_CIPHER_CTX_ctrl(ctx_base
, EVP_CTRL_AEAD_SET_TAG
,
715 expected
->tag_len
, tag
))
720 if (expected
->rounds
> 0) {
721 int rounds
= (int)expected
->rounds
;
723 if (!EVP_CIPHER_CTX_ctrl(ctx_base
, EVP_CTRL_SET_RC5_ROUNDS
, rounds
, NULL
)) {
724 t
->err
= "INVALID_ROUNDS";
729 if (!EVP_CIPHER_CTX_set_key_length(ctx_base
, expected
->key_len
)) {
730 t
->err
= "INVALID_KEY_LENGTH";
733 if (expected
->key_bits
> 0) {
734 int bits
= (int)expected
->key_bits
;
736 if (!EVP_CIPHER_CTX_ctrl(ctx_base
, EVP_CTRL_SET_RC2_KEY_BITS
, bits
, NULL
)) {
737 t
->err
= "INVALID KEY BITS";
741 if (!EVP_CipherInit_ex(ctx_base
, NULL
, NULL
, expected
->key
, expected
->iv
, -1)) {
742 t
->err
= "KEY_SET_ERROR";
746 /* Check that we get the same IV back */
747 if (expected
->iv
!= NULL
748 && (EVP_CIPHER_flags(expected
->cipher
) & EVP_CIPH_CUSTOM_IV
) == 0
749 && !TEST_mem_eq(expected
->iv
, expected
->iv_len
,
750 EVP_CIPHER_CTX_iv(ctx_base
), expected
->iv_len
)) {
751 t
->err
= "INVALID_IV";
755 /* Test that the cipher dup functions correctly if it is supported */
756 if (EVP_CIPHER_CTX_copy(ctx
, ctx_base
)) {
757 EVP_CIPHER_CTX_free(ctx_base
);
760 EVP_CIPHER_CTX_free(ctx
);
764 if (expected
->aead
== EVP_CIPH_CCM_MODE
) {
765 if (!EVP_CipherUpdate(ctx
, NULL
, &tmplen
, NULL
, out_len
)) {
766 t
->err
= "CCM_PLAINTEXT_LENGTH_SET_ERROR";
770 if (expected
->aad
[0] != NULL
) {
771 t
->err
= "AAD_SET_ERROR";
773 for (i
= 0; expected
->aad
[i
] != NULL
; i
++) {
774 if (!EVP_CipherUpdate(ctx
, NULL
, &chunklen
, expected
->aad
[i
],
775 expected
->aad_len
[i
]))
780 * Supply the AAD in chunks less than the block size where possible
782 for (i
= 0; expected
->aad
[i
] != NULL
; i
++) {
783 if (expected
->aad_len
[i
] > 0) {
784 if (!EVP_CipherUpdate(ctx
, NULL
, &chunklen
, expected
->aad
[i
], 1))
788 if (expected
->aad_len
[i
] > 2) {
789 if (!EVP_CipherUpdate(ctx
, NULL
, &chunklen
,
790 expected
->aad
[i
] + donelen
,
791 expected
->aad_len
[i
] - 2))
793 donelen
+= expected
->aad_len
[i
] - 2;
795 if (expected
->aad_len
[i
] > 1
796 && !EVP_CipherUpdate(ctx
, NULL
, &chunklen
,
797 expected
->aad
[i
] + donelen
, 1))
803 if (!enc
&& (expected
->aead
== EVP_CIPH_OCB_MODE
|| expected
->tag_late
)) {
804 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_TAG
,
805 expected
->tag_len
, expected
->tag
)) {
806 t
->err
= "TAG_SET_ERROR";
811 EVP_CIPHER_CTX_set_padding(ctx
, 0);
812 t
->err
= "CIPHERUPDATE_ERROR";
815 /* We supply the data all in one go */
816 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
, &tmplen
, in
, in_len
))
819 /* Supply the data in chunks less than the block size where possible */
821 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
, &chunklen
, in
, 1))
828 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
+ tmplen
, &chunklen
,
836 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
+ tmplen
, &chunklen
,
842 if (!EVP_CipherFinal_ex(ctx
, tmp
+ out_misalign
+ tmplen
, &tmpflen
)) {
843 t
->err
= "CIPHERFINAL_ERROR";
846 if (!memory_err_compare(t
, "VALUE_MISMATCH", expected_out
, out_len
,
847 tmp
+ out_misalign
, tmplen
+ tmpflen
))
849 if (enc
&& expected
->aead
) {
850 unsigned char rtag
[16];
852 if (!TEST_size_t_le(expected
->tag_len
, sizeof(rtag
))) {
853 t
->err
= "TAG_LENGTH_INTERNAL_ERROR";
856 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_GET_TAG
,
857 expected
->tag_len
, rtag
)) {
858 t
->err
= "TAG_RETRIEVE_ERROR";
861 if (!memory_err_compare(t
, "TAG_VALUE_MISMATCH",
862 expected
->tag
, expected
->tag_len
,
863 rtag
, expected
->tag_len
))
871 EVP_CIPHER_CTX_free(ctx_base
);
872 EVP_CIPHER_CTX_free(ctx
);
876 static int cipher_test_run(EVP_TEST
*t
)
878 CIPHER_DATA
*cdat
= t
->data
;
880 size_t out_misalign
, inp_misalign
;
886 if (!cdat
->iv
&& EVP_CIPHER_iv_length(cdat
->cipher
)) {
887 /* IV is optional and usually omitted in wrap mode */
888 if (EVP_CIPHER_mode(cdat
->cipher
) != EVP_CIPH_WRAP_MODE
) {
893 if (cdat
->aead
&& !cdat
->tag
) {
897 for (out_misalign
= 0; out_misalign
<= 1;) {
898 static char aux_err
[64];
899 t
->aux_err
= aux_err
;
900 for (inp_misalign
= (size_t)-1; inp_misalign
!= 2; inp_misalign
++) {
901 if (inp_misalign
== (size_t)-1) {
902 /* kludge: inp_misalign == -1 means "exercise in-place" */
903 BIO_snprintf(aux_err
, sizeof(aux_err
),
904 "%s in-place, %sfragmented",
905 out_misalign
? "misaligned" : "aligned",
908 BIO_snprintf(aux_err
, sizeof(aux_err
),
909 "%s output and %s input, %sfragmented",
910 out_misalign
? "misaligned" : "aligned",
911 inp_misalign
? "misaligned" : "aligned",
915 rv
= cipher_test_enc(t
, 1, out_misalign
, inp_misalign
, frag
);
916 /* Not fatal errors: return */
923 if (cdat
->enc
!= 1) {
924 rv
= cipher_test_enc(t
, 0, out_misalign
, inp_misalign
, frag
);
925 /* Not fatal errors: return */
934 if (out_misalign
== 1 && frag
== 0) {
936 * XTS, SIV, CCM and Wrap modes have special requirements about input
937 * lengths so we don't fragment for those
939 if (cdat
->aead
== EVP_CIPH_CCM_MODE
940 || EVP_CIPHER_mode(cdat
->cipher
) == EVP_CIPH_SIV_MODE
941 || EVP_CIPHER_mode(cdat
->cipher
) == EVP_CIPH_XTS_MODE
942 || EVP_CIPHER_mode(cdat
->cipher
) == EVP_CIPH_WRAP_MODE
)
955 static const EVP_TEST_METHOD cipher_test_method
= {
968 typedef struct mac_data_st
{
969 /* MAC type in one form or another */
971 EVP_MAC
*mac
; /* for mac_test_run_mac */
972 int type
; /* for mac_test_run_pkey */
973 /* Algorithm string for this MAC */
982 unsigned char *input
;
984 /* Expected output */
985 unsigned char *output
;
987 unsigned char *custom
;
989 /* MAC salt (blake2) */
992 /* Collection of controls */
993 STACK_OF(OPENSSL_STRING
) *controls
;
996 static int mac_test_init(EVP_TEST
*t
, const char *alg
)
999 int type
= NID_undef
;
1002 if ((mac
= EVP_MAC_fetch(NULL
, alg
, NULL
)) == NULL
) {
1004 * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
1005 * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
1006 * the EVP_PKEY method.
1008 size_t sz
= strlen(alg
);
1009 static const char epilogue
[] = " by EVP_PKEY";
1011 if (sz
>= sizeof(epilogue
)
1012 && strcmp(alg
+ sz
- (sizeof(epilogue
) - 1), epilogue
) == 0)
1013 sz
-= sizeof(epilogue
) - 1;
1015 if (strncmp(alg
, "HMAC", sz
) == 0) {
1016 type
= EVP_PKEY_HMAC
;
1017 } else if (strncmp(alg
, "CMAC", sz
) == 0) {
1018 #ifndef OPENSSL_NO_CMAC
1019 type
= EVP_PKEY_CMAC
;
1024 } else if (strncmp(alg
, "Poly1305", sz
) == 0) {
1025 #ifndef OPENSSL_NO_POLY1305
1026 type
= EVP_PKEY_POLY1305
;
1031 } else if (strncmp(alg
, "SipHash", sz
) == 0) {
1032 #ifndef OPENSSL_NO_SIPHASH
1033 type
= EVP_PKEY_SIPHASH
;
1040 * Not a known EVP_PKEY method either. If it's a known OID, then
1041 * assume it's been disabled.
1043 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
1052 mdat
= OPENSSL_zalloc(sizeof(*mdat
));
1054 mdat
->mac_name
= OPENSSL_strdup(alg
);
1056 mdat
->controls
= sk_OPENSSL_STRING_new_null();
1061 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
1062 static void openssl_free(char *m
)
1067 static void mac_test_cleanup(EVP_TEST
*t
)
1069 MAC_DATA
*mdat
= t
->data
;
1071 EVP_MAC_free(mdat
->mac
);
1072 OPENSSL_free(mdat
->mac_name
);
1073 sk_OPENSSL_STRING_pop_free(mdat
->controls
, openssl_free
);
1074 OPENSSL_free(mdat
->alg
);
1075 OPENSSL_free(mdat
->key
);
1076 OPENSSL_free(mdat
->iv
);
1077 OPENSSL_free(mdat
->custom
);
1078 OPENSSL_free(mdat
->salt
);
1079 OPENSSL_free(mdat
->input
);
1080 OPENSSL_free(mdat
->output
);
1083 static int mac_test_parse(EVP_TEST
*t
,
1084 const char *keyword
, const char *value
)
1086 MAC_DATA
*mdata
= t
->data
;
1088 if (strcmp(keyword
, "Key") == 0)
1089 return parse_bin(value
, &mdata
->key
, &mdata
->key_len
);
1090 if (strcmp(keyword
, "IV") == 0)
1091 return parse_bin(value
, &mdata
->iv
, &mdata
->iv_len
);
1092 if (strcmp(keyword
, "Custom") == 0)
1093 return parse_bin(value
, &mdata
->custom
, &mdata
->custom_len
);
1094 if (strcmp(keyword
, "Salt") == 0)
1095 return parse_bin(value
, &mdata
->salt
, &mdata
->salt_len
);
1096 if (strcmp(keyword
, "Algorithm") == 0) {
1097 mdata
->alg
= OPENSSL_strdup(value
);
1102 if (strcmp(keyword
, "Input") == 0)
1103 return parse_bin(value
, &mdata
->input
, &mdata
->input_len
);
1104 if (strcmp(keyword
, "Output") == 0)
1105 return parse_bin(value
, &mdata
->output
, &mdata
->output_len
);
1106 if (strcmp(keyword
, "Ctrl") == 0)
1107 return sk_OPENSSL_STRING_push(mdata
->controls
,
1108 OPENSSL_strdup(value
)) != 0;
1112 static int mac_test_ctrl_pkey(EVP_TEST
*t
, EVP_PKEY_CTX
*pctx
,
1118 if (!TEST_ptr(tmpval
= OPENSSL_strdup(value
)))
1120 p
= strchr(tmpval
, ':');
1123 rv
= EVP_PKEY_CTX_ctrl_str(pctx
, tmpval
, p
);
1125 t
->err
= "PKEY_CTRL_INVALID";
1127 t
->err
= "PKEY_CTRL_ERROR";
1130 OPENSSL_free(tmpval
);
1134 static int mac_test_run_pkey(EVP_TEST
*t
)
1136 MAC_DATA
*expected
= t
->data
;
1137 EVP_MD_CTX
*mctx
= NULL
;
1138 EVP_PKEY_CTX
*pctx
= NULL
, *genctx
= NULL
;
1139 EVP_PKEY
*key
= NULL
;
1140 const EVP_MD
*md
= NULL
;
1141 unsigned char *got
= NULL
;
1145 if (expected
->alg
== NULL
)
1146 TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected
->type
));
1148 TEST_info("Trying the EVP_PKEY %s test with %s",
1149 OBJ_nid2sn(expected
->type
), expected
->alg
);
1151 #ifdef OPENSSL_NO_DES
1152 if (expected
->alg
!= NULL
&& strstr(expected
->alg
, "DES") != NULL
) {
1159 if (expected
->type
== EVP_PKEY_CMAC
)
1160 key
= EVP_PKEY_new_CMAC_key(NULL
, expected
->key
, expected
->key_len
,
1161 EVP_get_cipherbyname(expected
->alg
));
1163 key
= EVP_PKEY_new_raw_private_key(expected
->type
, NULL
, expected
->key
,
1166 t
->err
= "MAC_KEY_CREATE_ERROR";
1170 if (expected
->type
== EVP_PKEY_HMAC
) {
1171 if (!TEST_ptr(md
= EVP_get_digestbyname(expected
->alg
))) {
1172 t
->err
= "MAC_ALGORITHM_SET_ERROR";
1176 if (!TEST_ptr(mctx
= EVP_MD_CTX_new())) {
1177 t
->err
= "INTERNAL_ERROR";
1180 if (!EVP_DigestSignInit(mctx
, &pctx
, md
, NULL
, key
)) {
1181 t
->err
= "DIGESTSIGNINIT_ERROR";
1184 for (i
= 0; i
< sk_OPENSSL_STRING_num(expected
->controls
); i
++)
1185 if (!mac_test_ctrl_pkey(t
, pctx
,
1186 sk_OPENSSL_STRING_value(expected
->controls
,
1188 t
->err
= "EVPPKEYCTXCTRL_ERROR";
1191 if (!EVP_DigestSignUpdate(mctx
, expected
->input
, expected
->input_len
)) {
1192 t
->err
= "DIGESTSIGNUPDATE_ERROR";
1195 if (!EVP_DigestSignFinal(mctx
, NULL
, &got_len
)) {
1196 t
->err
= "DIGESTSIGNFINAL_LENGTH_ERROR";
1199 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1200 t
->err
= "TEST_FAILURE";
1203 if (!EVP_DigestSignFinal(mctx
, got
, &got_len
)
1204 || !memory_err_compare(t
, "TEST_MAC_ERR",
1205 expected
->output
, expected
->output_len
,
1207 t
->err
= "TEST_MAC_ERR";
1212 EVP_MD_CTX_free(mctx
);
1214 EVP_PKEY_CTX_free(genctx
);
1219 static int mac_test_run_mac(EVP_TEST
*t
)
1221 MAC_DATA
*expected
= t
->data
;
1222 EVP_MAC_CTX
*ctx
= NULL
;
1223 unsigned char *got
= NULL
;
1226 OSSL_PARAM params
[21];
1227 size_t params_n
= 0;
1228 size_t params_n_allocstart
= 0;
1229 const OSSL_PARAM
*defined_params
=
1230 EVP_MAC_settable_ctx_params(expected
->mac
);
1232 if (expected
->alg
== NULL
)
1233 TEST_info("Trying the EVP_MAC %s test", expected
->mac_name
);
1235 TEST_info("Trying the EVP_MAC %s test with %s",
1236 expected
->mac_name
, expected
->alg
);
1238 #ifdef OPENSSL_NO_DES
1239 if (expected
->alg
!= NULL
&& strstr(expected
->alg
, "DES") != NULL
) {
1246 if (expected
->alg
!= NULL
) {
1248 * The underlying algorithm may be a cipher or a digest.
1249 * We don't know which it is, but we can ask the MAC what it
1250 * should be and bet on that.
1252 if (OSSL_PARAM_locate_const(defined_params
,
1253 OSSL_MAC_PARAM_CIPHER
) != NULL
) {
1254 params
[params_n
++] =
1255 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER
,
1257 } else if (OSSL_PARAM_locate_const(defined_params
,
1258 OSSL_MAC_PARAM_DIGEST
) != NULL
) {
1259 params
[params_n
++] =
1260 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST
,
1263 t
->err
= "MAC_BAD_PARAMS";
1267 if (expected
->key
!= NULL
)
1268 params
[params_n
++] =
1269 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY
,
1272 if (expected
->custom
!= NULL
)
1273 params
[params_n
++] =
1274 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM
,
1276 expected
->custom_len
);
1277 if (expected
->salt
!= NULL
)
1278 params
[params_n
++] =
1279 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT
,
1281 expected
->salt_len
);
1282 if (expected
->iv
!= NULL
)
1283 params
[params_n
++] =
1284 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV
,
1289 * Unknown controls. They must match parameters that the MAC recognises
1291 if (params_n
+ sk_OPENSSL_STRING_num(expected
->controls
)
1292 >= OSSL_NELEM(params
)) {
1293 t
->err
= "MAC_TOO_MANY_PARAMETERS";
1296 params_n_allocstart
= params_n
;
1297 for (i
= 0; i
< sk_OPENSSL_STRING_num(expected
->controls
); i
++) {
1298 char *tmpkey
, *tmpval
;
1299 char *value
= sk_OPENSSL_STRING_value(expected
->controls
, i
);
1301 if (!TEST_ptr(tmpkey
= OPENSSL_strdup(value
))) {
1302 t
->err
= "MAC_PARAM_ERROR";
1305 tmpval
= strchr(tmpkey
, ':');
1310 || !OSSL_PARAM_allocate_from_text(¶ms
[params_n
],
1314 OPENSSL_free(tmpkey
);
1315 t
->err
= "MAC_PARAM_ERROR";
1320 OPENSSL_free(tmpkey
);
1322 params
[params_n
] = OSSL_PARAM_construct_end();
1324 if ((ctx
= EVP_MAC_CTX_new(expected
->mac
)) == NULL
) {
1325 t
->err
= "MAC_CREATE_ERROR";
1329 if (!EVP_MAC_CTX_set_params(ctx
, params
)) {
1330 t
->err
= "MAC_BAD_PARAMS";
1333 if (!EVP_MAC_init(ctx
)) {
1334 t
->err
= "MAC_INIT_ERROR";
1337 if (!EVP_MAC_update(ctx
, expected
->input
, expected
->input_len
)) {
1338 t
->err
= "MAC_UPDATE_ERROR";
1341 if (!EVP_MAC_final(ctx
, NULL
, &got_len
, 0)) {
1342 t
->err
= "MAC_FINAL_LENGTH_ERROR";
1345 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1346 t
->err
= "TEST_FAILURE";
1349 if (!EVP_MAC_final(ctx
, got
, &got_len
, got_len
)
1350 || !memory_err_compare(t
, "TEST_MAC_ERR",
1351 expected
->output
, expected
->output_len
,
1353 t
->err
= "TEST_MAC_ERR";
1358 while (params_n
-- > params_n_allocstart
) {
1359 OPENSSL_free(params
[params_n
].data
);
1361 EVP_MAC_CTX_free(ctx
);
1366 static int mac_test_run(EVP_TEST
*t
)
1368 MAC_DATA
*expected
= t
->data
;
1370 if (expected
->mac
!= NULL
)
1371 return mac_test_run_mac(t
);
1372 return mac_test_run_pkey(t
);
1375 static const EVP_TEST_METHOD mac_test_method
= {
1385 *** PUBLIC KEY TESTS
1386 *** These are all very similar and share much common code.
1389 typedef struct pkey_data_st
{
1390 /* Context for this operation */
1392 /* Key operation to perform */
1393 int (*keyop
) (EVP_PKEY_CTX
*ctx
,
1394 unsigned char *sig
, size_t *siglen
,
1395 const unsigned char *tbs
, size_t tbslen
);
1397 unsigned char *input
;
1399 /* Expected output */
1400 unsigned char *output
;
1405 * Perform public key operation setup: lookup key, allocated ctx and call
1406 * the appropriate initialisation function
1408 static int pkey_test_init(EVP_TEST
*t
, const char *name
,
1410 int (*keyopinit
) (EVP_PKEY_CTX
*ctx
),
1411 int (*keyop
)(EVP_PKEY_CTX
*ctx
,
1412 unsigned char *sig
, size_t *siglen
,
1413 const unsigned char *tbs
,
1417 EVP_PKEY
*pkey
= NULL
;
1421 rv
= find_key(&pkey
, name
, public_keys
);
1423 rv
= find_key(&pkey
, name
, private_keys
);
1424 if (rv
== 0 || pkey
== NULL
) {
1429 if (!TEST_ptr(kdata
= OPENSSL_zalloc(sizeof(*kdata
)))) {
1430 EVP_PKEY_free(pkey
);
1433 kdata
->keyop
= keyop
;
1434 if (!TEST_ptr(kdata
->ctx
= EVP_PKEY_CTX_new(pkey
, NULL
))) {
1435 EVP_PKEY_free(pkey
);
1436 OPENSSL_free(kdata
);
1439 if (keyopinit(kdata
->ctx
) <= 0)
1440 t
->err
= "KEYOP_INIT_ERROR";
1445 static void pkey_test_cleanup(EVP_TEST
*t
)
1447 PKEY_DATA
*kdata
= t
->data
;
1449 OPENSSL_free(kdata
->input
);
1450 OPENSSL_free(kdata
->output
);
1451 EVP_PKEY_CTX_free(kdata
->ctx
);
1454 static int pkey_test_ctrl(EVP_TEST
*t
, EVP_PKEY_CTX
*pctx
,
1460 if (!TEST_ptr(tmpval
= OPENSSL_strdup(value
)))
1462 p
= strchr(tmpval
, ':');
1465 rv
= EVP_PKEY_CTX_ctrl_str(pctx
, tmpval
, p
);
1467 t
->err
= "PKEY_CTRL_INVALID";
1469 } else if (p
!= NULL
&& rv
<= 0) {
1470 /* If p has an OID and lookup fails assume disabled algorithm */
1471 int nid
= OBJ_sn2nid(p
);
1473 if (nid
== NID_undef
)
1474 nid
= OBJ_ln2nid(p
);
1475 if (nid
!= NID_undef
1476 && EVP_get_digestbynid(nid
) == NULL
1477 && EVP_get_cipherbynid(nid
) == NULL
) {
1481 t
->err
= "PKEY_CTRL_ERROR";
1485 OPENSSL_free(tmpval
);
1489 static int pkey_test_parse(EVP_TEST
*t
,
1490 const char *keyword
, const char *value
)
1492 PKEY_DATA
*kdata
= t
->data
;
1493 if (strcmp(keyword
, "Input") == 0)
1494 return parse_bin(value
, &kdata
->input
, &kdata
->input_len
);
1495 if (strcmp(keyword
, "Output") == 0)
1496 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
1497 if (strcmp(keyword
, "Ctrl") == 0)
1498 return pkey_test_ctrl(t
, kdata
->ctx
, value
);
1502 static int pkey_test_run(EVP_TEST
*t
)
1504 PKEY_DATA
*expected
= t
->data
;
1505 unsigned char *got
= NULL
;
1507 EVP_PKEY_CTX
*copy
= NULL
;
1509 if (expected
->keyop(expected
->ctx
, NULL
, &got_len
,
1510 expected
->input
, expected
->input_len
) <= 0
1511 || !TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1512 t
->err
= "KEYOP_LENGTH_ERROR";
1515 if (expected
->keyop(expected
->ctx
, got
, &got_len
,
1516 expected
->input
, expected
->input_len
) <= 0) {
1517 t
->err
= "KEYOP_ERROR";
1520 if (!memory_err_compare(t
, "KEYOP_MISMATCH",
1521 expected
->output
, expected
->output_len
,
1529 /* Repeat the test on a copy. */
1530 if (!TEST_ptr(copy
= EVP_PKEY_CTX_dup(expected
->ctx
))) {
1531 t
->err
= "INTERNAL_ERROR";
1534 if (expected
->keyop(copy
, NULL
, &got_len
, expected
->input
,
1535 expected
->input_len
) <= 0
1536 || !TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1537 t
->err
= "KEYOP_LENGTH_ERROR";
1540 if (expected
->keyop(copy
, got
, &got_len
, expected
->input
,
1541 expected
->input_len
) <= 0) {
1542 t
->err
= "KEYOP_ERROR";
1545 if (!memory_err_compare(t
, "KEYOP_MISMATCH",
1546 expected
->output
, expected
->output_len
,
1552 EVP_PKEY_CTX_free(copy
);
1556 static int sign_test_init(EVP_TEST
*t
, const char *name
)
1558 return pkey_test_init(t
, name
, 0, EVP_PKEY_sign_init
, EVP_PKEY_sign
);
1561 static const EVP_TEST_METHOD psign_test_method
= {
1569 static int verify_recover_test_init(EVP_TEST
*t
, const char *name
)
1571 return pkey_test_init(t
, name
, 1, EVP_PKEY_verify_recover_init
,
1572 EVP_PKEY_verify_recover
);
1575 static const EVP_TEST_METHOD pverify_recover_test_method
= {
1577 verify_recover_test_init
,
1583 static int decrypt_test_init(EVP_TEST
*t
, const char *name
)
1585 return pkey_test_init(t
, name
, 0, EVP_PKEY_decrypt_init
,
1589 static const EVP_TEST_METHOD pdecrypt_test_method
= {
1597 static int verify_test_init(EVP_TEST
*t
, const char *name
)
1599 return pkey_test_init(t
, name
, 1, EVP_PKEY_verify_init
, 0);
1602 static int verify_test_run(EVP_TEST
*t
)
1604 PKEY_DATA
*kdata
= t
->data
;
1606 if (EVP_PKEY_verify(kdata
->ctx
, kdata
->output
, kdata
->output_len
,
1607 kdata
->input
, kdata
->input_len
) <= 0)
1608 t
->err
= "VERIFY_ERROR";
1612 static const EVP_TEST_METHOD pverify_test_method
= {
1621 static int pderive_test_init(EVP_TEST
*t
, const char *name
)
1623 return pkey_test_init(t
, name
, 0, EVP_PKEY_derive_init
, 0);
1626 static int pderive_test_parse(EVP_TEST
*t
,
1627 const char *keyword
, const char *value
)
1629 PKEY_DATA
*kdata
= t
->data
;
1631 if (strcmp(keyword
, "PeerKey") == 0) {
1633 if (find_key(&peer
, value
, public_keys
) == 0)
1635 if (EVP_PKEY_derive_set_peer(kdata
->ctx
, peer
) <= 0)
1639 if (strcmp(keyword
, "SharedSecret") == 0)
1640 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
1641 if (strcmp(keyword
, "Ctrl") == 0)
1642 return pkey_test_ctrl(t
, kdata
->ctx
, value
);
1646 static int pderive_test_run(EVP_TEST
*t
)
1648 PKEY_DATA
*expected
= t
->data
;
1649 unsigned char *got
= NULL
;
1652 if (EVP_PKEY_derive(expected
->ctx
, NULL
, &got_len
) <= 0) {
1653 t
->err
= "DERIVE_ERROR";
1656 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1657 t
->err
= "DERIVE_ERROR";
1660 if (EVP_PKEY_derive(expected
->ctx
, got
, &got_len
) <= 0) {
1661 t
->err
= "DERIVE_ERROR";
1664 if (!memory_err_compare(t
, "SHARED_SECRET_MISMATCH",
1665 expected
->output
, expected
->output_len
,
1675 static const EVP_TEST_METHOD pderive_test_method
= {
1688 typedef enum pbe_type_enum
{
1689 PBE_TYPE_INVALID
= 0,
1690 PBE_TYPE_SCRYPT
, PBE_TYPE_PBKDF2
, PBE_TYPE_PKCS12
1693 typedef struct pbe_data_st
{
1695 /* scrypt parameters */
1696 uint64_t N
, r
, p
, maxmem
;
1697 /* PKCS#12 parameters */
1701 unsigned char *pass
;
1704 unsigned char *salt
;
1706 /* Expected output */
1711 #ifndef OPENSSL_NO_SCRYPT
1713 * Parse unsigned decimal 64 bit integer value
1715 static int parse_uint64(const char *value
, uint64_t *pr
)
1717 const char *p
= value
;
1719 if (!TEST_true(*p
)) {
1720 TEST_info("Invalid empty integer value");
1723 for (*pr
= 0; *p
; ) {
1724 if (*pr
> UINT64_MAX
/ 10) {
1725 TEST_error("Integer overflow in string %s", value
);
1729 if (!TEST_true(isdigit((unsigned char)*p
))) {
1730 TEST_error("Invalid character in string %s", value
);
1739 static int scrypt_test_parse(EVP_TEST
*t
,
1740 const char *keyword
, const char *value
)
1742 PBE_DATA
*pdata
= t
->data
;
1744 if (strcmp(keyword
, "N") == 0)
1745 return parse_uint64(value
, &pdata
->N
);
1746 if (strcmp(keyword
, "p") == 0)
1747 return parse_uint64(value
, &pdata
->p
);
1748 if (strcmp(keyword
, "r") == 0)
1749 return parse_uint64(value
, &pdata
->r
);
1750 if (strcmp(keyword
, "maxmem") == 0)
1751 return parse_uint64(value
, &pdata
->maxmem
);
1756 static int pbkdf2_test_parse(EVP_TEST
*t
,
1757 const char *keyword
, const char *value
)
1759 PBE_DATA
*pdata
= t
->data
;
1761 if (strcmp(keyword
, "iter") == 0) {
1762 pdata
->iter
= atoi(value
);
1763 if (pdata
->iter
<= 0)
1767 if (strcmp(keyword
, "MD") == 0) {
1768 pdata
->md
= EVP_get_digestbyname(value
);
1769 if (pdata
->md
== NULL
)
1776 static int pkcs12_test_parse(EVP_TEST
*t
,
1777 const char *keyword
, const char *value
)
1779 PBE_DATA
*pdata
= t
->data
;
1781 if (strcmp(keyword
, "id") == 0) {
1782 pdata
->id
= atoi(value
);
1787 return pbkdf2_test_parse(t
, keyword
, value
);
1790 static int pbe_test_init(EVP_TEST
*t
, const char *alg
)
1793 PBE_TYPE pbe_type
= PBE_TYPE_INVALID
;
1795 if (strcmp(alg
, "scrypt") == 0) {
1796 #ifndef OPENSSL_NO_SCRYPT
1797 pbe_type
= PBE_TYPE_SCRYPT
;
1802 } else if (strcmp(alg
, "pbkdf2") == 0) {
1803 pbe_type
= PBE_TYPE_PBKDF2
;
1804 } else if (strcmp(alg
, "pkcs12") == 0) {
1805 pbe_type
= PBE_TYPE_PKCS12
;
1807 TEST_error("Unknown pbe algorithm %s", alg
);
1809 pdat
= OPENSSL_zalloc(sizeof(*pdat
));
1810 pdat
->pbe_type
= pbe_type
;
1815 static void pbe_test_cleanup(EVP_TEST
*t
)
1817 PBE_DATA
*pdat
= t
->data
;
1819 OPENSSL_free(pdat
->pass
);
1820 OPENSSL_free(pdat
->salt
);
1821 OPENSSL_free(pdat
->key
);
1824 static int pbe_test_parse(EVP_TEST
*t
,
1825 const char *keyword
, const char *value
)
1827 PBE_DATA
*pdata
= t
->data
;
1829 if (strcmp(keyword
, "Password") == 0)
1830 return parse_bin(value
, &pdata
->pass
, &pdata
->pass_len
);
1831 if (strcmp(keyword
, "Salt") == 0)
1832 return parse_bin(value
, &pdata
->salt
, &pdata
->salt_len
);
1833 if (strcmp(keyword
, "Key") == 0)
1834 return parse_bin(value
, &pdata
->key
, &pdata
->key_len
);
1835 if (pdata
->pbe_type
== PBE_TYPE_PBKDF2
)
1836 return pbkdf2_test_parse(t
, keyword
, value
);
1837 else if (pdata
->pbe_type
== PBE_TYPE_PKCS12
)
1838 return pkcs12_test_parse(t
, keyword
, value
);
1839 #ifndef OPENSSL_NO_SCRYPT
1840 else if (pdata
->pbe_type
== PBE_TYPE_SCRYPT
)
1841 return scrypt_test_parse(t
, keyword
, value
);
1846 static int pbe_test_run(EVP_TEST
*t
)
1848 PBE_DATA
*expected
= t
->data
;
1851 if (!TEST_ptr(key
= OPENSSL_malloc(expected
->key_len
))) {
1852 t
->err
= "INTERNAL_ERROR";
1855 if (expected
->pbe_type
== PBE_TYPE_PBKDF2
) {
1856 if (PKCS5_PBKDF2_HMAC((char *)expected
->pass
, expected
->pass_len
,
1857 expected
->salt
, expected
->salt_len
,
1858 expected
->iter
, expected
->md
,
1859 expected
->key_len
, key
) == 0) {
1860 t
->err
= "PBKDF2_ERROR";
1863 #ifndef OPENSSL_NO_SCRYPT
1864 } else if (expected
->pbe_type
== PBE_TYPE_SCRYPT
) {
1865 if (EVP_PBE_scrypt((const char *)expected
->pass
, expected
->pass_len
,
1866 expected
->salt
, expected
->salt_len
, expected
->N
,
1867 expected
->r
, expected
->p
, expected
->maxmem
,
1868 key
, expected
->key_len
) == 0) {
1869 t
->err
= "SCRYPT_ERROR";
1873 } else if (expected
->pbe_type
== PBE_TYPE_PKCS12
) {
1874 if (PKCS12_key_gen_uni(expected
->pass
, expected
->pass_len
,
1875 expected
->salt
, expected
->salt_len
,
1876 expected
->id
, expected
->iter
, expected
->key_len
,
1877 key
, expected
->md
) == 0) {
1878 t
->err
= "PKCS12_ERROR";
1882 if (!memory_err_compare(t
, "KEY_MISMATCH", expected
->key
, expected
->key_len
,
1883 key
, expected
->key_len
))
1892 static const EVP_TEST_METHOD pbe_test_method
= {
1906 BASE64_CANONICAL_ENCODING
= 0,
1907 BASE64_VALID_ENCODING
= 1,
1908 BASE64_INVALID_ENCODING
= 2
1909 } base64_encoding_type
;
1911 typedef struct encode_data_st
{
1912 /* Input to encoding */
1913 unsigned char *input
;
1915 /* Expected output */
1916 unsigned char *output
;
1918 base64_encoding_type encoding
;
1921 static int encode_test_init(EVP_TEST
*t
, const char *encoding
)
1925 if (!TEST_ptr(edata
= OPENSSL_zalloc(sizeof(*edata
))))
1927 if (strcmp(encoding
, "canonical") == 0) {
1928 edata
->encoding
= BASE64_CANONICAL_ENCODING
;
1929 } else if (strcmp(encoding
, "valid") == 0) {
1930 edata
->encoding
= BASE64_VALID_ENCODING
;
1931 } else if (strcmp(encoding
, "invalid") == 0) {
1932 edata
->encoding
= BASE64_INVALID_ENCODING
;
1933 if (!TEST_ptr(t
->expected_err
= OPENSSL_strdup("DECODE_ERROR")))
1936 TEST_error("Bad encoding: %s."
1937 " Should be one of {canonical, valid, invalid}",
1944 OPENSSL_free(edata
);
1948 static void encode_test_cleanup(EVP_TEST
*t
)
1950 ENCODE_DATA
*edata
= t
->data
;
1952 OPENSSL_free(edata
->input
);
1953 OPENSSL_free(edata
->output
);
1954 memset(edata
, 0, sizeof(*edata
));
1957 static int encode_test_parse(EVP_TEST
*t
,
1958 const char *keyword
, const char *value
)
1960 ENCODE_DATA
*edata
= t
->data
;
1962 if (strcmp(keyword
, "Input") == 0)
1963 return parse_bin(value
, &edata
->input
, &edata
->input_len
);
1964 if (strcmp(keyword
, "Output") == 0)
1965 return parse_bin(value
, &edata
->output
, &edata
->output_len
);
1969 static int encode_test_run(EVP_TEST
*t
)
1971 ENCODE_DATA
*expected
= t
->data
;
1972 unsigned char *encode_out
= NULL
, *decode_out
= NULL
;
1973 int output_len
, chunk_len
;
1974 EVP_ENCODE_CTX
*decode_ctx
= NULL
, *encode_ctx
= NULL
;
1976 if (!TEST_ptr(decode_ctx
= EVP_ENCODE_CTX_new())) {
1977 t
->err
= "INTERNAL_ERROR";
1981 if (expected
->encoding
== BASE64_CANONICAL_ENCODING
) {
1983 if (!TEST_ptr(encode_ctx
= EVP_ENCODE_CTX_new())
1984 || !TEST_ptr(encode_out
=
1985 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected
->input_len
))))
1988 EVP_EncodeInit(encode_ctx
);
1989 if (!TEST_true(EVP_EncodeUpdate(encode_ctx
, encode_out
, &chunk_len
,
1990 expected
->input
, expected
->input_len
)))
1993 output_len
= chunk_len
;
1995 EVP_EncodeFinal(encode_ctx
, encode_out
+ chunk_len
, &chunk_len
);
1996 output_len
+= chunk_len
;
1998 if (!memory_err_compare(t
, "BAD_ENCODING",
1999 expected
->output
, expected
->output_len
,
2000 encode_out
, output_len
))
2004 if (!TEST_ptr(decode_out
=
2005 OPENSSL_malloc(EVP_DECODE_LENGTH(expected
->output_len
))))
2008 EVP_DecodeInit(decode_ctx
);
2009 if (EVP_DecodeUpdate(decode_ctx
, decode_out
, &chunk_len
, expected
->output
,
2010 expected
->output_len
) < 0) {
2011 t
->err
= "DECODE_ERROR";
2014 output_len
= chunk_len
;
2016 if (EVP_DecodeFinal(decode_ctx
, decode_out
+ chunk_len
, &chunk_len
) != 1) {
2017 t
->err
= "DECODE_ERROR";
2020 output_len
+= chunk_len
;
2022 if (expected
->encoding
!= BASE64_INVALID_ENCODING
2023 && !memory_err_compare(t
, "BAD_DECODING",
2024 expected
->input
, expected
->input_len
,
2025 decode_out
, output_len
)) {
2026 t
->err
= "BAD_DECODING";
2032 OPENSSL_free(encode_out
);
2033 OPENSSL_free(decode_out
);
2034 EVP_ENCODE_CTX_free(decode_ctx
);
2035 EVP_ENCODE_CTX_free(encode_ctx
);
2039 static const EVP_TEST_METHOD encode_test_method
= {
2042 encode_test_cleanup
,
2052 typedef struct kdf_data_st
{
2053 /* Context for this operation */
2055 /* Expected output */
2056 unsigned char *output
;
2058 OSSL_PARAM params
[20];
2063 * Perform public key operation setup: lookup key, allocated ctx and call
2064 * the appropriate initialisation function
2066 static int kdf_test_init(EVP_TEST
*t
, const char *name
)
2071 #ifdef OPENSSL_NO_SCRYPT
2072 /* TODO(3.0) Replace with "scrypt" once aliases are supported */
2073 if (strcmp(name
, "id-scrypt") == 0) {
2077 #endif /* OPENSSL_NO_SCRYPT */
2079 #ifdef OPENSSL_NO_CMS
2080 if (strcmp(name
, "X942KDF") == 0) {
2084 #endif /* OPENSSL_NO_CMS */
2086 if (!TEST_ptr(kdata
= OPENSSL_zalloc(sizeof(*kdata
))))
2088 kdata
->p
= kdata
->params
;
2089 *kdata
->p
= OSSL_PARAM_construct_end();
2091 kdf
= EVP_KDF_fetch(NULL
, name
, NULL
);
2093 OPENSSL_free(kdata
);
2096 kdata
->ctx
= EVP_KDF_CTX_new(kdf
);
2098 if (kdata
->ctx
== NULL
) {
2099 OPENSSL_free(kdata
);
2106 static void kdf_test_cleanup(EVP_TEST
*t
)
2108 KDF_DATA
*kdata
= t
->data
;
2111 for (p
= kdata
->params
; p
->key
!= NULL
; p
++)
2112 OPENSSL_free(p
->data
);
2113 OPENSSL_free(kdata
->output
);
2114 EVP_KDF_CTX_free(kdata
->ctx
);
2117 static int kdf_test_ctrl(EVP_TEST
*t
, EVP_KDF_CTX
*kctx
,
2120 KDF_DATA
*kdata
= t
->data
;
2123 const OSSL_PARAM
*defs
= EVP_KDF_settable_ctx_params(EVP_KDF_CTX_kdf(kctx
));
2125 if (!TEST_ptr(name
= OPENSSL_strdup(value
)))
2127 p
= strchr(name
, ':');
2131 rv
= OSSL_PARAM_allocate_from_text(kdata
->p
, defs
, name
, p
,
2132 p
!= NULL
? strlen(p
) : 0);
2133 *++kdata
->p
= OSSL_PARAM_construct_end();
2135 t
->err
= "KDF_PARAM_ERROR";
2139 if (p
!= NULL
&& strcmp(name
, "digest") == 0) {
2140 /* If p has an OID and lookup fails assume disabled algorithm */
2141 int nid
= OBJ_sn2nid(p
);
2143 if (nid
== NID_undef
)
2144 nid
= OBJ_ln2nid(p
);
2145 if (nid
!= NID_undef
&& EVP_get_digestbynid(nid
) == NULL
)
2148 if (p
!= NULL
&& strcmp(name
, "cipher") == 0) {
2149 /* If p has an OID and lookup fails assume disabled algorithm */
2150 int nid
= OBJ_sn2nid(p
);
2152 if (nid
== NID_undef
)
2153 nid
= OBJ_ln2nid(p
);
2154 if (nid
!= NID_undef
&& EVP_get_cipherbynid(nid
) == NULL
)
2161 static int kdf_test_parse(EVP_TEST
*t
,
2162 const char *keyword
, const char *value
)
2164 KDF_DATA
*kdata
= t
->data
;
2166 if (strcmp(keyword
, "Output") == 0)
2167 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
2168 if (strncmp(keyword
, "Ctrl", 4) == 0)
2169 return kdf_test_ctrl(t
, kdata
->ctx
, value
);
2173 static int kdf_test_run(EVP_TEST
*t
)
2175 KDF_DATA
*expected
= t
->data
;
2176 unsigned char *got
= NULL
;
2177 size_t got_len
= expected
->output_len
;
2179 if (!EVP_KDF_CTX_set_params(expected
->ctx
, expected
->params
)) {
2180 t
->err
= "KDF_CTRL_ERROR";
2183 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
2184 t
->err
= "INTERNAL_ERROR";
2187 if (EVP_KDF_derive(expected
->ctx
, got
, got_len
) <= 0) {
2188 t
->err
= "KDF_DERIVE_ERROR";
2191 if (!memory_err_compare(t
, "KDF_MISMATCH",
2192 expected
->output
, expected
->output_len
,
2203 static const EVP_TEST_METHOD kdf_test_method
= {
2216 typedef struct pkey_kdf_data_st
{
2217 /* Context for this operation */
2219 /* Expected output */
2220 unsigned char *output
;
2225 * Perform public key operation setup: lookup key, allocated ctx and call
2226 * the appropriate initialisation function
2228 static int pkey_kdf_test_init(EVP_TEST
*t
, const char *name
)
2230 PKEY_KDF_DATA
*kdata
;
2231 int kdf_nid
= OBJ_sn2nid(name
);
2233 #ifdef OPENSSL_NO_SCRYPT
2234 if (strcmp(name
, "scrypt") == 0) {
2238 #endif /* OPENSSL_NO_SCRYPT */
2240 #ifdef OPENSSL_NO_CMS
2241 if (strcmp(name
, "X942KDF") == 0) {
2245 #endif /* OPENSSL_NO_CMS */
2247 if (kdf_nid
== NID_undef
)
2248 kdf_nid
= OBJ_ln2nid(name
);
2250 if (!TEST_ptr(kdata
= OPENSSL_zalloc(sizeof(*kdata
))))
2252 kdata
->ctx
= EVP_PKEY_CTX_new_id(kdf_nid
, NULL
);
2253 if (kdata
->ctx
== NULL
) {
2254 OPENSSL_free(kdata
);
2257 if (EVP_PKEY_derive_init(kdata
->ctx
) <= 0) {
2258 EVP_PKEY_CTX_free(kdata
->ctx
);
2259 OPENSSL_free(kdata
);
2266 static void pkey_kdf_test_cleanup(EVP_TEST
*t
)
2268 PKEY_KDF_DATA
*kdata
= t
->data
;
2270 OPENSSL_free(kdata
->output
);
2271 EVP_PKEY_CTX_free(kdata
->ctx
);
2274 static int pkey_kdf_test_parse(EVP_TEST
*t
,
2275 const char *keyword
, const char *value
)
2277 PKEY_KDF_DATA
*kdata
= t
->data
;
2279 if (strcmp(keyword
, "Output") == 0)
2280 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
2281 if (strncmp(keyword
, "Ctrl", 4) == 0)
2282 return pkey_test_ctrl(t
, kdata
->ctx
, value
);
2286 static int pkey_kdf_test_run(EVP_TEST
*t
)
2288 PKEY_KDF_DATA
*expected
= t
->data
;
2289 unsigned char *got
= NULL
;
2290 size_t got_len
= expected
->output_len
;
2292 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
2293 t
->err
= "INTERNAL_ERROR";
2296 if (EVP_PKEY_derive(expected
->ctx
, got
, &got_len
) <= 0) {
2297 t
->err
= "KDF_DERIVE_ERROR";
2300 if (!TEST_mem_eq(expected
->output
, expected
->output_len
, got
, got_len
)) {
2301 t
->err
= "KDF_MISMATCH";
2311 static const EVP_TEST_METHOD pkey_kdf_test_method
= {
2314 pkey_kdf_test_cleanup
,
2315 pkey_kdf_test_parse
,
2324 typedef struct keypair_test_data_st
{
2327 } KEYPAIR_TEST_DATA
;
2329 static int keypair_test_init(EVP_TEST
*t
, const char *pair
)
2331 KEYPAIR_TEST_DATA
*data
;
2333 EVP_PKEY
*pk
= NULL
, *pubk
= NULL
;
2334 char *pub
, *priv
= NULL
;
2336 /* Split private and public names. */
2337 if (!TEST_ptr(priv
= OPENSSL_strdup(pair
))
2338 || !TEST_ptr(pub
= strchr(priv
, ':'))) {
2339 t
->err
= "PARSING_ERROR";
2344 if (!TEST_true(find_key(&pk
, priv
, private_keys
))) {
2345 TEST_info("Can't find private key: %s", priv
);
2346 t
->err
= "MISSING_PRIVATE_KEY";
2349 if (!TEST_true(find_key(&pubk
, pub
, public_keys
))) {
2350 TEST_info("Can't find public key: %s", pub
);
2351 t
->err
= "MISSING_PUBLIC_KEY";
2355 if (pk
== NULL
&& pubk
== NULL
) {
2356 /* Both keys are listed but unsupported: skip this test */
2362 if (!TEST_ptr(data
= OPENSSL_malloc(sizeof(*data
))))
2375 static void keypair_test_cleanup(EVP_TEST
*t
)
2377 OPENSSL_free(t
->data
);
2382 * For tests that do not accept any custom keywords.
2384 static int void_test_parse(EVP_TEST
*t
, const char *keyword
, const char *value
)
2389 static int keypair_test_run(EVP_TEST
*t
)
2392 const KEYPAIR_TEST_DATA
*pair
= t
->data
;
2394 if (pair
->privk
== NULL
|| pair
->pubk
== NULL
) {
2396 * this can only happen if only one of the keys is not set
2397 * which means that one of them was unsupported while the
2398 * other isn't: hence a key type mismatch.
2400 t
->err
= "KEYPAIR_TYPE_MISMATCH";
2405 if ((rv
= EVP_PKEY_cmp(pair
->privk
, pair
->pubk
)) != 1 ) {
2407 t
->err
= "KEYPAIR_MISMATCH";
2408 } else if ( -1 == rv
) {
2409 t
->err
= "KEYPAIR_TYPE_MISMATCH";
2410 } else if ( -2 == rv
) {
2411 t
->err
= "UNSUPPORTED_KEY_COMPARISON";
2413 TEST_error("Unexpected error in key comparison");
2428 static const EVP_TEST_METHOD keypair_test_method
= {
2431 keypair_test_cleanup
,
2440 typedef struct keygen_test_data_st
{
2441 EVP_PKEY_CTX
*genctx
; /* Keygen context to use */
2442 char *keyname
; /* Key name to store key or NULL */
2445 static int keygen_test_init(EVP_TEST
*t
, const char *alg
)
2447 KEYGEN_TEST_DATA
*data
;
2448 EVP_PKEY_CTX
*genctx
;
2449 int nid
= OBJ_sn2nid(alg
);
2451 if (nid
== NID_undef
) {
2452 nid
= OBJ_ln2nid(alg
);
2453 if (nid
== NID_undef
)
2457 if (!TEST_ptr(genctx
= EVP_PKEY_CTX_new_id(nid
, NULL
))) {
2458 /* assume algorithm disabled */
2463 if (EVP_PKEY_keygen_init(genctx
) <= 0) {
2464 t
->err
= "KEYGEN_INIT_ERROR";
2468 if (!TEST_ptr(data
= OPENSSL_malloc(sizeof(*data
))))
2470 data
->genctx
= genctx
;
2471 data
->keyname
= NULL
;
2477 EVP_PKEY_CTX_free(genctx
);
2481 static void keygen_test_cleanup(EVP_TEST
*t
)
2483 KEYGEN_TEST_DATA
*keygen
= t
->data
;
2485 EVP_PKEY_CTX_free(keygen
->genctx
);
2486 OPENSSL_free(keygen
->keyname
);
2487 OPENSSL_free(t
->data
);
2491 static int keygen_test_parse(EVP_TEST
*t
,
2492 const char *keyword
, const char *value
)
2494 KEYGEN_TEST_DATA
*keygen
= t
->data
;
2496 if (strcmp(keyword
, "KeyName") == 0)
2497 return TEST_ptr(keygen
->keyname
= OPENSSL_strdup(value
));
2498 if (strcmp(keyword
, "Ctrl") == 0)
2499 return pkey_test_ctrl(t
, keygen
->genctx
, value
);
2503 static int keygen_test_run(EVP_TEST
*t
)
2505 KEYGEN_TEST_DATA
*keygen
= t
->data
;
2506 EVP_PKEY
*pkey
= NULL
;
2509 if (EVP_PKEY_keygen(keygen
->genctx
, &pkey
) <= 0) {
2510 t
->err
= "KEYGEN_GENERATE_ERROR";
2514 if (keygen
->keyname
!= NULL
) {
2517 if (find_key(NULL
, keygen
->keyname
, private_keys
)) {
2518 TEST_info("Duplicate key %s", keygen
->keyname
);
2522 if (!TEST_ptr(key
= OPENSSL_malloc(sizeof(*key
))))
2524 key
->name
= keygen
->keyname
;
2525 keygen
->keyname
= NULL
;
2527 key
->next
= private_keys
;
2530 EVP_PKEY_free(pkey
);
2536 EVP_PKEY_free(pkey
);
2540 static const EVP_TEST_METHOD keygen_test_method
= {
2543 keygen_test_cleanup
,
2549 *** DIGEST SIGN+VERIFY TESTS
2553 int is_verify
; /* Set to 1 if verifying */
2554 int is_oneshot
; /* Set to 1 for one shot operation */
2555 const EVP_MD
*md
; /* Digest to use */
2556 EVP_MD_CTX
*ctx
; /* Digest context */
2558 STACK_OF(EVP_TEST_BUFFER
) *input
; /* Input data: streaming */
2559 unsigned char *osin
; /* Input data if one shot */
2560 size_t osin_len
; /* Input length data if one shot */
2561 unsigned char *output
; /* Expected output */
2562 size_t output_len
; /* Expected output length */
2565 static int digestsigver_test_init(EVP_TEST
*t
, const char *alg
, int is_verify
,
2568 const EVP_MD
*md
= NULL
;
2569 DIGESTSIGN_DATA
*mdat
;
2571 if (strcmp(alg
, "NULL") != 0) {
2572 if ((md
= EVP_get_digestbyname(alg
)) == NULL
) {
2573 /* If alg has an OID assume disabled algorithm */
2574 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
2581 if (!TEST_ptr(mdat
= OPENSSL_zalloc(sizeof(*mdat
))))
2584 if (!TEST_ptr(mdat
->ctx
= EVP_MD_CTX_new())) {
2588 mdat
->is_verify
= is_verify
;
2589 mdat
->is_oneshot
= is_oneshot
;
2594 static int digestsign_test_init(EVP_TEST
*t
, const char *alg
)
2596 return digestsigver_test_init(t
, alg
, 0, 0);
2599 static void digestsigver_test_cleanup(EVP_TEST
*t
)
2601 DIGESTSIGN_DATA
*mdata
= t
->data
;
2603 EVP_MD_CTX_free(mdata
->ctx
);
2604 sk_EVP_TEST_BUFFER_pop_free(mdata
->input
, evp_test_buffer_free
);
2605 OPENSSL_free(mdata
->osin
);
2606 OPENSSL_free(mdata
->output
);
2607 OPENSSL_free(mdata
);
2611 static int digestsigver_test_parse(EVP_TEST
*t
,
2612 const char *keyword
, const char *value
)
2614 DIGESTSIGN_DATA
*mdata
= t
->data
;
2616 if (strcmp(keyword
, "Key") == 0) {
2617 EVP_PKEY
*pkey
= NULL
;
2620 if (mdata
->is_verify
)
2621 rv
= find_key(&pkey
, value
, public_keys
);
2623 rv
= find_key(&pkey
, value
, private_keys
);
2624 if (rv
== 0 || pkey
== NULL
) {
2628 if (mdata
->is_verify
) {
2629 if (!EVP_DigestVerifyInit(mdata
->ctx
, &mdata
->pctx
, mdata
->md
,
2631 t
->err
= "DIGESTVERIFYINIT_ERROR";
2634 if (!EVP_DigestSignInit(mdata
->ctx
, &mdata
->pctx
, mdata
->md
, NULL
,
2636 t
->err
= "DIGESTSIGNINIT_ERROR";
2640 if (strcmp(keyword
, "Input") == 0) {
2641 if (mdata
->is_oneshot
)
2642 return parse_bin(value
, &mdata
->osin
, &mdata
->osin_len
);
2643 return evp_test_buffer_append(value
, &mdata
->input
);
2645 if (strcmp(keyword
, "Output") == 0)
2646 return parse_bin(value
, &mdata
->output
, &mdata
->output_len
);
2648 if (!mdata
->is_oneshot
) {
2649 if (strcmp(keyword
, "Count") == 0)
2650 return evp_test_buffer_set_count(value
, mdata
->input
);
2651 if (strcmp(keyword
, "Ncopy") == 0)
2652 return evp_test_buffer_ncopy(value
, mdata
->input
);
2654 if (strcmp(keyword
, "Ctrl") == 0) {
2655 if (mdata
->pctx
== NULL
)
2657 return pkey_test_ctrl(t
, mdata
->pctx
, value
);
2662 static int digestsign_update_fn(void *ctx
, const unsigned char *buf
,
2665 return EVP_DigestSignUpdate(ctx
, buf
, buflen
);
2668 static int digestsign_test_run(EVP_TEST
*t
)
2670 DIGESTSIGN_DATA
*expected
= t
->data
;
2671 unsigned char *got
= NULL
;
2674 if (!evp_test_buffer_do(expected
->input
, digestsign_update_fn
,
2676 t
->err
= "DIGESTUPDATE_ERROR";
2680 if (!EVP_DigestSignFinal(expected
->ctx
, NULL
, &got_len
)) {
2681 t
->err
= "DIGESTSIGNFINAL_LENGTH_ERROR";
2684 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
2685 t
->err
= "MALLOC_FAILURE";
2688 if (!EVP_DigestSignFinal(expected
->ctx
, got
, &got_len
)) {
2689 t
->err
= "DIGESTSIGNFINAL_ERROR";
2692 if (!memory_err_compare(t
, "SIGNATURE_MISMATCH",
2693 expected
->output
, expected
->output_len
,
2703 static const EVP_TEST_METHOD digestsign_test_method
= {
2705 digestsign_test_init
,
2706 digestsigver_test_cleanup
,
2707 digestsigver_test_parse
,
2711 static int digestverify_test_init(EVP_TEST
*t
, const char *alg
)
2713 return digestsigver_test_init(t
, alg
, 1, 0);
2716 static int digestverify_update_fn(void *ctx
, const unsigned char *buf
,
2719 return EVP_DigestVerifyUpdate(ctx
, buf
, buflen
);
2722 static int digestverify_test_run(EVP_TEST
*t
)
2724 DIGESTSIGN_DATA
*mdata
= t
->data
;
2726 if (!evp_test_buffer_do(mdata
->input
, digestverify_update_fn
, mdata
->ctx
)) {
2727 t
->err
= "DIGESTUPDATE_ERROR";
2731 if (EVP_DigestVerifyFinal(mdata
->ctx
, mdata
->output
,
2732 mdata
->output_len
) <= 0)
2733 t
->err
= "VERIFY_ERROR";
2737 static const EVP_TEST_METHOD digestverify_test_method
= {
2739 digestverify_test_init
,
2740 digestsigver_test_cleanup
,
2741 digestsigver_test_parse
,
2742 digestverify_test_run
2745 static int oneshot_digestsign_test_init(EVP_TEST
*t
, const char *alg
)
2747 return digestsigver_test_init(t
, alg
, 0, 1);
2750 static int oneshot_digestsign_test_run(EVP_TEST
*t
)
2752 DIGESTSIGN_DATA
*expected
= t
->data
;
2753 unsigned char *got
= NULL
;
2756 if (!EVP_DigestSign(expected
->ctx
, NULL
, &got_len
,
2757 expected
->osin
, expected
->osin_len
)) {
2758 t
->err
= "DIGESTSIGN_LENGTH_ERROR";
2761 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
2762 t
->err
= "MALLOC_FAILURE";
2765 if (!EVP_DigestSign(expected
->ctx
, got
, &got_len
,
2766 expected
->osin
, expected
->osin_len
)) {
2767 t
->err
= "DIGESTSIGN_ERROR";
2770 if (!memory_err_compare(t
, "SIGNATURE_MISMATCH",
2771 expected
->output
, expected
->output_len
,
2781 static const EVP_TEST_METHOD oneshot_digestsign_test_method
= {
2782 "OneShotDigestSign",
2783 oneshot_digestsign_test_init
,
2784 digestsigver_test_cleanup
,
2785 digestsigver_test_parse
,
2786 oneshot_digestsign_test_run
2789 static int oneshot_digestverify_test_init(EVP_TEST
*t
, const char *alg
)
2791 return digestsigver_test_init(t
, alg
, 1, 1);
2794 static int oneshot_digestverify_test_run(EVP_TEST
*t
)
2796 DIGESTSIGN_DATA
*mdata
= t
->data
;
2798 if (EVP_DigestVerify(mdata
->ctx
, mdata
->output
, mdata
->output_len
,
2799 mdata
->osin
, mdata
->osin_len
) <= 0)
2800 t
->err
= "VERIFY_ERROR";
2804 static const EVP_TEST_METHOD oneshot_digestverify_test_method
= {
2805 "OneShotDigestVerify",
2806 oneshot_digestverify_test_init
,
2807 digestsigver_test_cleanup
,
2808 digestsigver_test_parse
,
2809 oneshot_digestverify_test_run
2814 *** PARSING AND DISPATCH
2817 static const EVP_TEST_METHOD
*evp_test_list
[] = {
2818 &cipher_test_method
,
2819 &digest_test_method
,
2820 &digestsign_test_method
,
2821 &digestverify_test_method
,
2822 &encode_test_method
,
2824 &pkey_kdf_test_method
,
2825 &keypair_test_method
,
2826 &keygen_test_method
,
2828 &oneshot_digestsign_test_method
,
2829 &oneshot_digestverify_test_method
,
2831 &pdecrypt_test_method
,
2832 &pderive_test_method
,
2834 &pverify_recover_test_method
,
2835 &pverify_test_method
,
2839 static const EVP_TEST_METHOD
*find_test(const char *name
)
2841 const EVP_TEST_METHOD
**tt
;
2843 for (tt
= evp_test_list
; *tt
; tt
++) {
2844 if (strcmp(name
, (*tt
)->name
) == 0)
2850 static void clear_test(EVP_TEST
*t
)
2852 test_clearstanza(&t
->s
);
2854 if (t
->data
!= NULL
) {
2855 if (t
->meth
!= NULL
)
2856 t
->meth
->cleanup(t
);
2857 OPENSSL_free(t
->data
);
2860 OPENSSL_free(t
->expected_err
);
2861 t
->expected_err
= NULL
;
2862 OPENSSL_free(t
->reason
);
2872 * Check for errors in the test structure; return 1 if okay, else 0.
2874 static int check_test_error(EVP_TEST
*t
)
2879 if (t
->err
== NULL
&& t
->expected_err
== NULL
)
2881 if (t
->err
!= NULL
&& t
->expected_err
== NULL
) {
2882 if (t
->aux_err
!= NULL
) {
2883 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
2884 t
->s
.test_file
, t
->s
.start
, t
->aux_err
, t
->err
);
2886 TEST_info("%s:%d: Source of above error; unexpected error %s",
2887 t
->s
.test_file
, t
->s
.start
, t
->err
);
2891 if (t
->err
== NULL
&& t
->expected_err
!= NULL
) {
2892 TEST_info("%s:%d: Succeeded but was expecting %s",
2893 t
->s
.test_file
, t
->s
.start
, t
->expected_err
);
2897 if (strcmp(t
->err
, t
->expected_err
) != 0) {
2898 TEST_info("%s:%d: Expected %s got %s",
2899 t
->s
.test_file
, t
->s
.start
, t
->expected_err
, t
->err
);
2903 if (t
->reason
== NULL
)
2906 if (t
->reason
== NULL
) {
2907 TEST_info("%s:%d: Test is missing function or reason code",
2908 t
->s
.test_file
, t
->s
.start
);
2912 err
= ERR_peek_error();
2914 TEST_info("%s:%d: Expected error \"%s\" not set",
2915 t
->s
.test_file
, t
->s
.start
, t
->reason
);
2919 reason
= ERR_reason_error_string(err
);
2920 if (reason
== NULL
) {
2921 TEST_info("%s:%d: Expected error \"%s\", no strings available."
2923 t
->s
.test_file
, t
->s
.start
, t
->reason
);
2927 if (strcmp(reason
, t
->reason
) == 0)
2930 TEST_info("%s:%d: Expected error \"%s\", got \"%s\"",
2931 t
->s
.test_file
, t
->s
.start
, t
->reason
, reason
);
2937 * Run a parsed test. Log a message and return 0 on error.
2939 static int run_test(EVP_TEST
*t
)
2941 if (t
->meth
== NULL
)
2948 if (t
->err
== NULL
&& t
->meth
->run_test(t
) != 1) {
2949 TEST_info("%s:%d %s error",
2950 t
->s
.test_file
, t
->s
.start
, t
->meth
->name
);
2953 if (!check_test_error(t
)) {
2954 TEST_openssl_errors();
2963 static int find_key(EVP_PKEY
**ppk
, const char *name
, KEY_LIST
*lst
)
2965 for (; lst
!= NULL
; lst
= lst
->next
) {
2966 if (strcmp(lst
->name
, name
) == 0) {
2975 static void free_key_list(KEY_LIST
*lst
)
2977 while (lst
!= NULL
) {
2978 KEY_LIST
*next
= lst
->next
;
2980 EVP_PKEY_free(lst
->key
);
2981 OPENSSL_free(lst
->name
);
2988 * Is the key type an unsupported algorithm?
2990 static int key_unsupported(void)
2992 long err
= ERR_peek_error();
2994 if (ERR_GET_LIB(err
) == ERR_LIB_EVP
2995 && ERR_GET_REASON(err
) == EVP_R_UNSUPPORTED_ALGORITHM
) {
2999 #ifndef OPENSSL_NO_EC
3001 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
3002 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
3005 if (ERR_GET_LIB(err
) == ERR_LIB_EC
3006 && ERR_GET_REASON(err
) == EC_R_UNKNOWN_GROUP
) {
3010 #endif /* OPENSSL_NO_EC */
3015 * NULL out the value from |pp| but return it. This "steals" a pointer.
3017 static char *take_value(PAIR
*pp
)
3019 char *p
= pp
->value
;
3026 * Return 1 if one of the providers named in the string is available.
3027 * The provider names are separated with whitespace.
3028 * NOTE: destructive function, it inserts '\0' after each provider name.
3030 static int prov_available(char *providers
)
3036 for (; isspace(*providers
); providers
++)
3038 if (*providers
== '\0')
3039 break; /* End of the road */
3040 for (p
= providers
; *p
!= '\0' && !isspace(*p
); p
++)
3046 if (OSSL_PROVIDER_available(NULL
, providers
))
3047 return 1; /* Found one */
3053 * Read and parse one test. Return 0 if failure, 1 if okay.
3055 static int parse(EVP_TEST
*t
)
3057 KEY_LIST
*key
, **klist
;
3064 if (BIO_eof(t
->s
.fp
))
3067 if (!test_readstanza(&t
->s
))
3069 } while (t
->s
.numpairs
== 0);
3070 pp
= &t
->s
.pairs
[0];
3072 /* Are we adding a key? */
3075 if (strcmp(pp
->key
, "PrivateKey") == 0) {
3076 pkey
= PEM_read_bio_PrivateKey(t
->s
.key
, NULL
, 0, NULL
);
3077 if (pkey
== NULL
&& !key_unsupported()) {
3078 EVP_PKEY_free(pkey
);
3079 TEST_info("Can't read private key %s", pp
->value
);
3080 TEST_openssl_errors();
3083 klist
= &private_keys
;
3084 } else if (strcmp(pp
->key
, "PublicKey") == 0) {
3085 pkey
= PEM_read_bio_PUBKEY(t
->s
.key
, NULL
, 0, NULL
);
3086 if (pkey
== NULL
&& !key_unsupported()) {
3087 EVP_PKEY_free(pkey
);
3088 TEST_info("Can't read public key %s", pp
->value
);
3089 TEST_openssl_errors();
3092 klist
= &public_keys
;
3093 } else if (strcmp(pp
->key
, "PrivateKeyRaw") == 0
3094 || strcmp(pp
->key
, "PublicKeyRaw") == 0 ) {
3095 char *strnid
= NULL
, *keydata
= NULL
;
3096 unsigned char *keybin
;
3100 if (strcmp(pp
->key
, "PrivateKeyRaw") == 0)
3101 klist
= &private_keys
;
3103 klist
= &public_keys
;
3105 strnid
= strchr(pp
->value
, ':');
3106 if (strnid
!= NULL
) {
3108 keydata
= strchr(strnid
, ':');
3109 if (keydata
!= NULL
)
3112 if (keydata
== NULL
) {
3113 TEST_info("Failed to parse %s value", pp
->key
);
3117 nid
= OBJ_txt2nid(strnid
);
3118 if (nid
== NID_undef
) {
3119 TEST_info("Uncrecognised algorithm NID");
3122 if (!parse_bin(keydata
, &keybin
, &keylen
)) {
3123 TEST_info("Failed to create binary key");
3126 if (klist
== &private_keys
)
3127 pkey
= EVP_PKEY_new_raw_private_key(nid
, NULL
, keybin
, keylen
);
3129 pkey
= EVP_PKEY_new_raw_public_key(nid
, NULL
, keybin
, keylen
);
3130 if (pkey
== NULL
&& !key_unsupported()) {
3131 TEST_info("Can't read %s data", pp
->key
);
3132 OPENSSL_free(keybin
);
3133 TEST_openssl_errors();
3136 OPENSSL_free(keybin
);
3139 /* If we have a key add to list */
3140 if (klist
!= NULL
) {
3141 if (find_key(NULL
, pp
->value
, *klist
)) {
3142 TEST_info("Duplicate key %s", pp
->value
);
3145 if (!TEST_ptr(key
= OPENSSL_malloc(sizeof(*key
))))
3147 key
->name
= take_value(pp
);
3152 /* Go back and start a new stanza. */
3153 if (t
->s
.numpairs
!= 1)
3154 TEST_info("Line %d: missing blank line\n", t
->s
.curr
);
3158 /* Find the test, based on first keyword. */
3159 if (!TEST_ptr(t
->meth
= find_test(pp
->key
)))
3161 if (!t
->meth
->init(t
, pp
->value
)) {
3162 TEST_error("unknown %s: %s\n", pp
->key
, pp
->value
);
3166 /* TEST_info("skipping %s %s", pp->key, pp->value); */
3170 for (pp
++, i
= 1; i
< t
->s
.numpairs
; pp
++, i
++) {
3171 if (strcmp(pp
->key
, "Availablein") == 0) {
3172 if (!prov_available(pp
->value
)) {
3173 TEST_info("skipping, providers not available: %s:%d",
3174 t
->s
.test_file
, t
->s
.start
);
3178 } else if (strcmp(pp
->key
, "Result") == 0) {
3179 if (t
->expected_err
!= NULL
) {
3180 TEST_info("Line %d: multiple result lines", t
->s
.curr
);
3183 t
->expected_err
= take_value(pp
);
3184 } else if (strcmp(pp
->key
, "Function") == 0) {
3185 /* Ignore old line. */
3186 } else if (strcmp(pp
->key
, "Reason") == 0) {
3187 if (t
->reason
!= NULL
) {
3188 TEST_info("Line %d: multiple reason lines", t
->s
.curr
);
3191 t
->reason
= take_value(pp
);
3193 /* Must be test specific line: try to parse it */
3194 int rv
= t
->meth
->parse(t
, pp
->key
, pp
->value
);
3197 TEST_info("Line %d: unknown keyword %s", t
->s
.curr
, pp
->key
);
3201 TEST_info("Line %d: error processing keyword %s = %s\n",
3202 t
->s
.curr
, pp
->key
, pp
->value
);
3211 static int run_file_tests(int i
)
3214 const char *testfile
= test_get_argument(i
);
3217 if (!TEST_ptr(t
= OPENSSL_zalloc(sizeof(*t
))))
3219 if (!test_start_file(&t
->s
, testfile
)) {
3224 while (!BIO_eof(t
->s
.fp
)) {
3230 if (c
== 0 || !run_test(t
)) {
3235 test_end_file(&t
->s
);
3238 free_key_list(public_keys
);
3239 free_key_list(private_keys
);
3246 OPT_TEST_DECLARE_USAGE("file...\n")
3248 int setup_tests(void)
3250 size_t n
= test_get_argument_count();
3255 ADD_ALL_TESTS(run_file_tests
, n
);