2 * Copyright 2015-2020 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"
28 DEFINE_STACK_OF_STRING()
32 typedef struct evp_test_method_st EVP_TEST_METHOD
;
35 * Structure holding test information
37 typedef struct evp_test_st
{
38 STANZA s
; /* Common test stanza */
40 int skip
; /* Current test should be skipped */
41 const EVP_TEST_METHOD
*meth
; /* method for this test */
42 const char *err
, *aux_err
; /* Error string for test */
43 char *expected_err
; /* Expected error value of test */
44 char *reason
; /* Expected error reason string */
45 void *data
; /* test specific data */
49 * 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
);
66 * Linked list of named keys.
68 typedef struct key_list_st
{
71 struct key_list_st
*next
;
75 * List of public and private keys
77 static KEY_LIST
*private_keys
;
78 static KEY_LIST
*public_keys
;
79 static int find_key(EVP_PKEY
**ppk
, const char *name
, KEY_LIST
*lst
);
81 static int parse_bin(const char *value
, unsigned char **buf
, size_t *buflen
);
84 * Compare two memory regions for equality, returning zero if they differ.
85 * However, if there is expected to be an error and the actual error
86 * matches then the memory is expected to be different so handle this
87 * case without producing unnecessary test framework output.
89 static int memory_err_compare(EVP_TEST
*t
, const char *err
,
90 const void *expected
, size_t expected_len
,
91 const void *got
, size_t got_len
)
95 if (t
->expected_err
!= NULL
&& strcmp(t
->expected_err
, err
) == 0)
96 r
= !TEST_mem_ne(expected
, expected_len
, got
, got_len
);
98 r
= TEST_mem_eq(expected
, expected_len
, got
, got_len
);
105 * Structure used to hold a list of blocks of memory to test
106 * calls to "update" like functions.
108 struct evp_test_buffer_st
{
115 static void evp_test_buffer_free(EVP_TEST_BUFFER
*db
)
118 OPENSSL_free(db
->buf
);
124 * append buffer to a list
126 static int evp_test_buffer_append(const char *value
,
127 STACK_OF(EVP_TEST_BUFFER
) **sk
)
129 EVP_TEST_BUFFER
*db
= NULL
;
131 if (!TEST_ptr(db
= OPENSSL_malloc(sizeof(*db
))))
134 if (!parse_bin(value
, &db
->buf
, &db
->buflen
))
139 if (*sk
== NULL
&& !TEST_ptr(*sk
= sk_EVP_TEST_BUFFER_new_null()))
141 if (!sk_EVP_TEST_BUFFER_push(*sk
, db
))
147 evp_test_buffer_free(db
);
152 * replace last buffer in list with copies of itself
154 static int evp_test_buffer_ncopy(const char *value
,
155 STACK_OF(EVP_TEST_BUFFER
) *sk
)
158 unsigned char *tbuf
, *p
;
160 int ncopy
= atoi(value
);
165 if (sk
== NULL
|| sk_EVP_TEST_BUFFER_num(sk
) == 0)
167 db
= sk_EVP_TEST_BUFFER_value(sk
, sk_EVP_TEST_BUFFER_num(sk
) - 1);
169 tbuflen
= db
->buflen
* ncopy
;
170 if (!TEST_ptr(tbuf
= OPENSSL_malloc(tbuflen
)))
172 for (i
= 0, p
= tbuf
; i
< ncopy
; i
++, p
+= db
->buflen
)
173 memcpy(p
, db
->buf
, db
->buflen
);
175 OPENSSL_free(db
->buf
);
177 db
->buflen
= tbuflen
;
182 * set repeat count for last buffer in list
184 static int evp_test_buffer_set_count(const char *value
,
185 STACK_OF(EVP_TEST_BUFFER
) *sk
)
188 int count
= atoi(value
);
193 if (sk
== NULL
|| sk_EVP_TEST_BUFFER_num(sk
) == 0)
196 db
= sk_EVP_TEST_BUFFER_value(sk
, sk_EVP_TEST_BUFFER_num(sk
) - 1);
197 if (db
->count_set
!= 0)
200 db
->count
= (size_t)count
;
206 * call "fn" with each element of the list in turn
208 static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER
) *sk
,
210 const unsigned char *buf
,
216 for (i
= 0; i
< sk_EVP_TEST_BUFFER_num(sk
); i
++) {
217 EVP_TEST_BUFFER
*tb
= sk_EVP_TEST_BUFFER_value(sk
, i
);
220 for (j
= 0; j
< tb
->count
; j
++) {
221 if (fn(ctx
, tb
->buf
, tb
->buflen
) <= 0)
229 * Unescape some sequences in string literals (only \n for now).
230 * Return an allocated buffer, set |out_len|. If |input_len|
231 * is zero, get an empty buffer but set length to zero.
233 static unsigned char* unescape(const char *input
, size_t input_len
,
236 unsigned char *ret
, *p
;
239 if (input_len
== 0) {
241 return OPENSSL_zalloc(1);
244 /* Escaping is non-expanding; over-allocate original size for simplicity. */
245 if (!TEST_ptr(ret
= p
= OPENSSL_malloc(input_len
)))
248 for (i
= 0; i
< input_len
; i
++) {
249 if (*input
== '\\') {
250 if (i
== input_len
- 1 || *++input
!= 'n') {
251 TEST_error("Bad escape sequence in file");
271 * For a hex string "value" convert to a binary allocated buffer.
272 * Return 1 on success or 0 on failure.
274 static int parse_bin(const char *value
, unsigned char **buf
, size_t *buflen
)
278 /* Check for NULL literal */
279 if (strcmp(value
, "NULL") == 0) {
285 /* Check for empty value */
286 if (*value
== '\0') {
288 * Don't return NULL for zero length buffer. This is needed for
289 * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key
290 * buffer even if the key length is 0, in order to detect key reset.
292 *buf
= OPENSSL_malloc(1);
300 /* Check for string literal */
301 if (value
[0] == '"') {
302 size_t vlen
= strlen(++value
);
304 if (vlen
== 0 || value
[vlen
- 1] != '"')
307 *buf
= unescape(value
, vlen
, buflen
);
308 return *buf
== NULL
? 0 : 1;
311 /* Otherwise assume as hex literal and convert it to binary buffer */
312 if (!TEST_ptr(*buf
= OPENSSL_hexstr2buf(value
, &len
))) {
313 TEST_info("Can't convert %s", value
);
314 TEST_openssl_errors();
317 /* Size of input buffer means we'll never overflow */
324 *** MESSAGE DIGEST TESTS
327 typedef struct digest_data_st
{
328 /* Digest this test is for */
329 const EVP_MD
*digest
;
330 EVP_MD
*fetched_digest
;
331 /* Input to digest */
332 STACK_OF(EVP_TEST_BUFFER
) *input
;
333 /* Expected output */
334 unsigned char *output
;
340 static int digest_test_init(EVP_TEST
*t
, const char *alg
)
343 const EVP_MD
*digest
;
344 EVP_MD
*fetched_digest
;
346 if ((digest
= fetched_digest
= EVP_MD_fetch(NULL
, alg
, NULL
)) == NULL
347 && (digest
= EVP_get_digestbyname(alg
)) == NULL
) {
348 /* If alg has an OID assume disabled algorithm */
349 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
355 if (!TEST_ptr(mdat
= OPENSSL_zalloc(sizeof(*mdat
))))
358 mdat
->digest
= digest
;
359 mdat
->fetched_digest
= fetched_digest
;
361 if (fetched_digest
!= NULL
)
362 TEST_info("%s is fetched", alg
);
366 static void digest_test_cleanup(EVP_TEST
*t
)
368 DIGEST_DATA
*mdat
= t
->data
;
370 sk_EVP_TEST_BUFFER_pop_free(mdat
->input
, evp_test_buffer_free
);
371 OPENSSL_free(mdat
->output
);
372 EVP_MD_meth_free(mdat
->fetched_digest
);
375 static int digest_test_parse(EVP_TEST
*t
,
376 const char *keyword
, const char *value
)
378 DIGEST_DATA
*mdata
= t
->data
;
380 if (strcmp(keyword
, "Input") == 0)
381 return evp_test_buffer_append(value
, &mdata
->input
);
382 if (strcmp(keyword
, "Output") == 0)
383 return parse_bin(value
, &mdata
->output
, &mdata
->output_len
);
384 if (strcmp(keyword
, "Count") == 0)
385 return evp_test_buffer_set_count(value
, mdata
->input
);
386 if (strcmp(keyword
, "Ncopy") == 0)
387 return evp_test_buffer_ncopy(value
, mdata
->input
);
388 if (strcmp(keyword
, "Padding") == 0)
389 return (mdata
->pad_type
= atoi(value
)) > 0;
393 static int digest_update_fn(void *ctx
, const unsigned char *buf
, size_t buflen
)
395 return EVP_DigestUpdate(ctx
, buf
, buflen
);
398 static int digest_test_run(EVP_TEST
*t
)
400 DIGEST_DATA
*expected
= t
->data
;
402 unsigned char *got
= NULL
;
403 unsigned int got_len
;
404 OSSL_PARAM params
[2];
406 t
->err
= "TEST_FAILURE";
407 if (!TEST_ptr(mctx
= EVP_MD_CTX_new()))
410 got
= OPENSSL_malloc(expected
->output_len
> EVP_MAX_MD_SIZE
?
411 expected
->output_len
: EVP_MAX_MD_SIZE
);
415 if (!EVP_DigestInit_ex(mctx
, expected
->digest
, NULL
)) {
416 t
->err
= "DIGESTINIT_ERROR";
419 if (expected
->pad_type
> 0) {
420 params
[0] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_PAD_TYPE
,
421 &expected
->pad_type
);
422 params
[1] = OSSL_PARAM_construct_end();
423 if (!TEST_int_gt(EVP_MD_CTX_set_params(mctx
, params
), 0)) {
424 t
->err
= "PARAMS_ERROR";
428 if (!evp_test_buffer_do(expected
->input
, digest_update_fn
, mctx
)) {
429 t
->err
= "DIGESTUPDATE_ERROR";
433 if (EVP_MD_flags(expected
->digest
) & EVP_MD_FLAG_XOF
) {
434 EVP_MD_CTX
*mctx_cpy
;
435 char dont
[] = "touch";
437 if (!TEST_ptr(mctx_cpy
= EVP_MD_CTX_new())) {
440 if (!EVP_MD_CTX_copy(mctx_cpy
, mctx
)) {
441 EVP_MD_CTX_free(mctx_cpy
);
444 if (!EVP_DigestFinalXOF(mctx_cpy
, (unsigned char *)dont
, 0)) {
445 EVP_MD_CTX_free(mctx_cpy
);
446 t
->err
= "DIGESTFINALXOF_ERROR";
449 if (!TEST_str_eq(dont
, "touch")) {
450 EVP_MD_CTX_free(mctx_cpy
);
451 t
->err
= "DIGESTFINALXOF_ERROR";
454 EVP_MD_CTX_free(mctx_cpy
);
456 got_len
= expected
->output_len
;
457 if (!EVP_DigestFinalXOF(mctx
, got
, got_len
)) {
458 t
->err
= "DIGESTFINALXOF_ERROR";
462 if (!EVP_DigestFinal(mctx
, got
, &got_len
)) {
463 t
->err
= "DIGESTFINAL_ERROR";
467 if (!TEST_int_eq(expected
->output_len
, got_len
)) {
468 t
->err
= "DIGEST_LENGTH_MISMATCH";
471 if (!memory_err_compare(t
, "DIGEST_MISMATCH",
472 expected
->output
, expected
->output_len
,
480 EVP_MD_CTX_free(mctx
);
484 static const EVP_TEST_METHOD digest_test_method
= {
497 typedef struct cipher_data_st
{
498 const EVP_CIPHER
*cipher
;
499 EVP_CIPHER
*fetched_cipher
;
501 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
505 size_t key_bits
; /* Used by RC2 */
509 unsigned char *plaintext
;
510 size_t plaintext_len
;
511 unsigned char *ciphertext
;
512 size_t ciphertext_len
;
513 /* GCM, CCM, OCB and SIV only */
514 unsigned char *aad
[AAD_NUM
];
515 size_t aad_len
[AAD_NUM
];
521 static int cipher_test_init(EVP_TEST
*t
, const char *alg
)
523 const EVP_CIPHER
*cipher
;
524 EVP_CIPHER
*fetched_cipher
;
528 if ((cipher
= fetched_cipher
= EVP_CIPHER_fetch(NULL
, alg
, NULL
)) == NULL
529 && (cipher
= EVP_get_cipherbyname(alg
)) == NULL
) {
530 /* If alg has an OID assume disabled algorithm */
531 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
537 cdat
= OPENSSL_zalloc(sizeof(*cdat
));
538 cdat
->cipher
= cipher
;
539 cdat
->fetched_cipher
= fetched_cipher
;
541 m
= EVP_CIPHER_mode(cipher
);
542 if (m
== EVP_CIPH_GCM_MODE
543 || m
== EVP_CIPH_OCB_MODE
544 || m
== EVP_CIPH_SIV_MODE
545 || m
== EVP_CIPH_CCM_MODE
)
547 else if (EVP_CIPHER_flags(cipher
) & EVP_CIPH_FLAG_AEAD_CIPHER
)
553 if (fetched_cipher
!= NULL
)
554 TEST_info("%s is fetched", alg
);
558 static void cipher_test_cleanup(EVP_TEST
*t
)
561 CIPHER_DATA
*cdat
= t
->data
;
563 OPENSSL_free(cdat
->key
);
564 OPENSSL_free(cdat
->iv
);
565 OPENSSL_free(cdat
->ciphertext
);
566 OPENSSL_free(cdat
->plaintext
);
567 for (i
= 0; i
< AAD_NUM
; i
++)
568 OPENSSL_free(cdat
->aad
[i
]);
569 OPENSSL_free(cdat
->tag
);
570 EVP_CIPHER_meth_free(cdat
->fetched_cipher
);
573 static int cipher_test_parse(EVP_TEST
*t
, const char *keyword
,
576 CIPHER_DATA
*cdat
= t
->data
;
579 if (strcmp(keyword
, "Key") == 0)
580 return parse_bin(value
, &cdat
->key
, &cdat
->key_len
);
581 if (strcmp(keyword
, "Rounds") == 0) {
585 cdat
->rounds
= (unsigned int)i
;
588 if (strcmp(keyword
, "IV") == 0)
589 return parse_bin(value
, &cdat
->iv
, &cdat
->iv_len
);
590 if (strcmp(keyword
, "Plaintext") == 0)
591 return parse_bin(value
, &cdat
->plaintext
, &cdat
->plaintext_len
);
592 if (strcmp(keyword
, "Ciphertext") == 0)
593 return parse_bin(value
, &cdat
->ciphertext
, &cdat
->ciphertext_len
);
594 if (strcmp(keyword
, "KeyBits") == 0) {
598 cdat
->key_bits
= (size_t)i
;
602 if (strcmp(keyword
, "AAD") == 0) {
603 for (i
= 0; i
< AAD_NUM
; i
++) {
604 if (cdat
->aad
[i
] == NULL
)
605 return parse_bin(value
, &cdat
->aad
[i
], &cdat
->aad_len
[i
]);
609 if (strcmp(keyword
, "Tag") == 0)
610 return parse_bin(value
, &cdat
->tag
, &cdat
->tag_len
);
611 if (strcmp(keyword
, "SetTagLate") == 0) {
612 if (strcmp(value
, "TRUE") == 0)
614 else if (strcmp(value
, "FALSE") == 0)
622 if (strcmp(keyword
, "Operation") == 0) {
623 if (strcmp(value
, "ENCRYPT") == 0)
625 else if (strcmp(value
, "DECRYPT") == 0)
634 static int cipher_test_enc(EVP_TEST
*t
, int enc
,
635 size_t out_misalign
, size_t inp_misalign
, int frag
)
637 CIPHER_DATA
*expected
= t
->data
;
638 unsigned char *in
, *expected_out
, *tmp
= NULL
;
639 size_t in_len
, out_len
, donelen
= 0;
640 int ok
= 0, tmplen
, chunklen
, tmpflen
, i
;
641 EVP_CIPHER_CTX
*ctx_base
= NULL
;
642 EVP_CIPHER_CTX
*ctx
= NULL
;
644 t
->err
= "TEST_FAILURE";
645 if (!TEST_ptr(ctx_base
= EVP_CIPHER_CTX_new()))
647 if (!TEST_ptr(ctx
= EVP_CIPHER_CTX_new()))
649 EVP_CIPHER_CTX_set_flags(ctx_base
, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
);
651 in
= expected
->plaintext
;
652 in_len
= expected
->plaintext_len
;
653 expected_out
= expected
->ciphertext
;
654 out_len
= expected
->ciphertext_len
;
656 in
= expected
->ciphertext
;
657 in_len
= expected
->ciphertext_len
;
658 expected_out
= expected
->plaintext
;
659 out_len
= expected
->plaintext_len
;
661 if (inp_misalign
== (size_t)-1) {
663 * Exercise in-place encryption
665 tmp
= OPENSSL_malloc(out_misalign
+ in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
);
668 in
= memcpy(tmp
+ out_misalign
, in
, in_len
);
670 inp_misalign
+= 16 - ((out_misalign
+ in_len
) & 15);
672 * 'tmp' will store both output and copy of input. We make the copy
673 * of input to specifically aligned part of 'tmp'. So we just
674 * figured out how much padding would ensure the required alignment,
675 * now we allocate extended buffer and finally copy the input just
676 * past inp_misalign in expression below. Output will be written
677 * past out_misalign...
679 tmp
= OPENSSL_malloc(out_misalign
+ in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
+
680 inp_misalign
+ in_len
);
683 in
= memcpy(tmp
+ out_misalign
+ in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
+
684 inp_misalign
, in
, in_len
);
686 if (!EVP_CipherInit_ex(ctx_base
, expected
->cipher
, NULL
, NULL
, NULL
, enc
)) {
687 t
->err
= "CIPHERINIT_ERROR";
691 if (expected
->aead
) {
692 if (!EVP_CIPHER_CTX_ctrl(ctx_base
, EVP_CTRL_AEAD_SET_IVLEN
,
693 expected
->iv_len
, 0)) {
694 t
->err
= "INVALID_IV_LENGTH";
697 } else if (expected
->iv_len
!= (size_t)EVP_CIPHER_CTX_iv_length(ctx_base
)) {
698 t
->err
= "INVALID_IV_LENGTH";
702 if (expected
->aead
) {
705 * If encrypting or OCB just set tag length initially, otherwise
706 * set tag length and value.
708 if (enc
|| expected
->aead
== EVP_CIPH_OCB_MODE
|| expected
->tag_late
) {
709 t
->err
= "TAG_LENGTH_SET_ERROR";
712 t
->err
= "TAG_SET_ERROR";
715 if (tag
|| expected
->aead
!= EVP_CIPH_GCM_MODE
) {
716 if (!EVP_CIPHER_CTX_ctrl(ctx_base
, EVP_CTRL_AEAD_SET_TAG
,
717 expected
->tag_len
, tag
))
722 if (expected
->rounds
> 0) {
723 int rounds
= (int)expected
->rounds
;
725 if (!EVP_CIPHER_CTX_ctrl(ctx_base
, EVP_CTRL_SET_RC5_ROUNDS
, rounds
, NULL
)) {
726 t
->err
= "INVALID_ROUNDS";
731 if (!EVP_CIPHER_CTX_set_key_length(ctx_base
, expected
->key_len
)) {
732 t
->err
= "INVALID_KEY_LENGTH";
735 if (expected
->key_bits
> 0) {
736 int bits
= (int)expected
->key_bits
;
738 if (!EVP_CIPHER_CTX_ctrl(ctx_base
, EVP_CTRL_SET_RC2_KEY_BITS
, bits
, NULL
)) {
739 t
->err
= "INVALID KEY BITS";
743 if (!EVP_CipherInit_ex(ctx_base
, NULL
, NULL
, expected
->key
, expected
->iv
, -1)) {
744 t
->err
= "KEY_SET_ERROR";
748 /* Check that we get the same IV back */
749 if (expected
->iv
!= NULL
750 && (EVP_CIPHER_flags(expected
->cipher
) & EVP_CIPH_CUSTOM_IV
) == 0
751 && !TEST_mem_eq(expected
->iv
, expected
->iv_len
,
752 EVP_CIPHER_CTX_iv(ctx_base
), expected
->iv_len
)) {
753 t
->err
= "INVALID_IV";
757 /* Test that the cipher dup functions correctly if it is supported */
758 if (EVP_CIPHER_CTX_copy(ctx
, ctx_base
)) {
759 EVP_CIPHER_CTX_free(ctx_base
);
762 EVP_CIPHER_CTX_free(ctx
);
766 if (expected
->aead
== EVP_CIPH_CCM_MODE
) {
767 if (!EVP_CipherUpdate(ctx
, NULL
, &tmplen
, NULL
, out_len
)) {
768 t
->err
= "CCM_PLAINTEXT_LENGTH_SET_ERROR";
772 if (expected
->aad
[0] != NULL
) {
773 t
->err
= "AAD_SET_ERROR";
775 for (i
= 0; expected
->aad
[i
] != NULL
; i
++) {
776 if (!EVP_CipherUpdate(ctx
, NULL
, &chunklen
, expected
->aad
[i
],
777 expected
->aad_len
[i
]))
782 * Supply the AAD in chunks less than the block size where possible
784 for (i
= 0; expected
->aad
[i
] != NULL
; i
++) {
785 if (expected
->aad_len
[i
] > 0) {
786 if (!EVP_CipherUpdate(ctx
, NULL
, &chunklen
, expected
->aad
[i
], 1))
790 if (expected
->aad_len
[i
] > 2) {
791 if (!EVP_CipherUpdate(ctx
, NULL
, &chunklen
,
792 expected
->aad
[i
] + donelen
,
793 expected
->aad_len
[i
] - 2))
795 donelen
+= expected
->aad_len
[i
] - 2;
797 if (expected
->aad_len
[i
] > 1
798 && !EVP_CipherUpdate(ctx
, NULL
, &chunklen
,
799 expected
->aad
[i
] + donelen
, 1))
805 if (!enc
&& (expected
->aead
== EVP_CIPH_OCB_MODE
|| expected
->tag_late
)) {
806 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_TAG
,
807 expected
->tag_len
, expected
->tag
)) {
808 t
->err
= "TAG_SET_ERROR";
813 EVP_CIPHER_CTX_set_padding(ctx
, 0);
814 t
->err
= "CIPHERUPDATE_ERROR";
817 /* We supply the data all in one go */
818 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
, &tmplen
, in
, in_len
))
821 /* Supply the data in chunks less than the block size where possible */
823 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
, &chunklen
, in
, 1))
830 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
+ tmplen
, &chunklen
,
838 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
+ tmplen
, &chunklen
,
844 if (!EVP_CipherFinal_ex(ctx
, tmp
+ out_misalign
+ tmplen
, &tmpflen
)) {
845 t
->err
= "CIPHERFINAL_ERROR";
848 if (!memory_err_compare(t
, "VALUE_MISMATCH", expected_out
, out_len
,
849 tmp
+ out_misalign
, tmplen
+ tmpflen
))
851 if (enc
&& expected
->aead
) {
852 unsigned char rtag
[16];
854 if (!TEST_size_t_le(expected
->tag_len
, sizeof(rtag
))) {
855 t
->err
= "TAG_LENGTH_INTERNAL_ERROR";
858 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_GET_TAG
,
859 expected
->tag_len
, rtag
)) {
860 t
->err
= "TAG_RETRIEVE_ERROR";
863 if (!memory_err_compare(t
, "TAG_VALUE_MISMATCH",
864 expected
->tag
, expected
->tag_len
,
865 rtag
, expected
->tag_len
))
873 EVP_CIPHER_CTX_free(ctx_base
);
874 EVP_CIPHER_CTX_free(ctx
);
878 static int cipher_test_run(EVP_TEST
*t
)
880 CIPHER_DATA
*cdat
= t
->data
;
882 size_t out_misalign
, inp_misalign
;
888 if (!cdat
->iv
&& EVP_CIPHER_iv_length(cdat
->cipher
)) {
889 /* IV is optional and usually omitted in wrap mode */
890 if (EVP_CIPHER_mode(cdat
->cipher
) != EVP_CIPH_WRAP_MODE
) {
895 if (cdat
->aead
&& !cdat
->tag
) {
899 for (out_misalign
= 0; out_misalign
<= 1;) {
900 static char aux_err
[64];
901 t
->aux_err
= aux_err
;
902 for (inp_misalign
= (size_t)-1; inp_misalign
!= 2; inp_misalign
++) {
903 if (inp_misalign
== (size_t)-1) {
904 /* kludge: inp_misalign == -1 means "exercise in-place" */
905 BIO_snprintf(aux_err
, sizeof(aux_err
),
906 "%s in-place, %sfragmented",
907 out_misalign
? "misaligned" : "aligned",
910 BIO_snprintf(aux_err
, sizeof(aux_err
),
911 "%s output and %s input, %sfragmented",
912 out_misalign
? "misaligned" : "aligned",
913 inp_misalign
? "misaligned" : "aligned",
917 rv
= cipher_test_enc(t
, 1, out_misalign
, inp_misalign
, frag
);
918 /* Not fatal errors: return */
925 if (cdat
->enc
!= 1) {
926 rv
= cipher_test_enc(t
, 0, out_misalign
, inp_misalign
, frag
);
927 /* Not fatal errors: return */
936 if (out_misalign
== 1 && frag
== 0) {
938 * XTS, SIV, CCM and Wrap modes have special requirements about input
939 * lengths so we don't fragment for those
941 if (cdat
->aead
== EVP_CIPH_CCM_MODE
942 || EVP_CIPHER_mode(cdat
->cipher
) == EVP_CIPH_SIV_MODE
943 || EVP_CIPHER_mode(cdat
->cipher
) == EVP_CIPH_XTS_MODE
944 || EVP_CIPHER_mode(cdat
->cipher
) == EVP_CIPH_WRAP_MODE
)
957 static const EVP_TEST_METHOD cipher_test_method
= {
970 typedef struct mac_data_st
{
971 /* MAC type in one form or another */
973 EVP_MAC
*mac
; /* for mac_test_run_mac */
974 int type
; /* for mac_test_run_pkey */
975 /* Algorithm string for this MAC */
984 unsigned char *input
;
986 /* Expected output */
987 unsigned char *output
;
989 unsigned char *custom
;
991 /* MAC salt (blake2) */
994 /* Collection of controls */
995 STACK_OF(OPENSSL_STRING
) *controls
;
998 static int mac_test_init(EVP_TEST
*t
, const char *alg
)
1000 EVP_MAC
*mac
= NULL
;
1001 int type
= NID_undef
;
1004 if ((mac
= EVP_MAC_fetch(NULL
, alg
, NULL
)) == NULL
) {
1006 * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
1007 * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
1008 * the EVP_PKEY method.
1010 size_t sz
= strlen(alg
);
1011 static const char epilogue
[] = " by EVP_PKEY";
1013 if (sz
>= sizeof(epilogue
)
1014 && strcmp(alg
+ sz
- (sizeof(epilogue
) - 1), epilogue
) == 0)
1015 sz
-= sizeof(epilogue
) - 1;
1017 if (strncmp(alg
, "HMAC", sz
) == 0) {
1018 type
= EVP_PKEY_HMAC
;
1019 } else if (strncmp(alg
, "CMAC", sz
) == 0) {
1020 #ifndef OPENSSL_NO_CMAC
1021 type
= EVP_PKEY_CMAC
;
1026 } else if (strncmp(alg
, "Poly1305", sz
) == 0) {
1027 #ifndef OPENSSL_NO_POLY1305
1028 type
= EVP_PKEY_POLY1305
;
1033 } else if (strncmp(alg
, "SipHash", sz
) == 0) {
1034 #ifndef OPENSSL_NO_SIPHASH
1035 type
= EVP_PKEY_SIPHASH
;
1042 * Not a known EVP_PKEY method either. If it's a known OID, then
1043 * assume it's been disabled.
1045 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
1054 mdat
= OPENSSL_zalloc(sizeof(*mdat
));
1056 mdat
->mac_name
= OPENSSL_strdup(alg
);
1058 mdat
->controls
= sk_OPENSSL_STRING_new_null();
1063 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
1064 static void openssl_free(char *m
)
1069 static void mac_test_cleanup(EVP_TEST
*t
)
1071 MAC_DATA
*mdat
= t
->data
;
1073 EVP_MAC_free(mdat
->mac
);
1074 OPENSSL_free(mdat
->mac_name
);
1075 sk_OPENSSL_STRING_pop_free(mdat
->controls
, openssl_free
);
1076 OPENSSL_free(mdat
->alg
);
1077 OPENSSL_free(mdat
->key
);
1078 OPENSSL_free(mdat
->iv
);
1079 OPENSSL_free(mdat
->custom
);
1080 OPENSSL_free(mdat
->salt
);
1081 OPENSSL_free(mdat
->input
);
1082 OPENSSL_free(mdat
->output
);
1085 static int mac_test_parse(EVP_TEST
*t
,
1086 const char *keyword
, const char *value
)
1088 MAC_DATA
*mdata
= t
->data
;
1090 if (strcmp(keyword
, "Key") == 0)
1091 return parse_bin(value
, &mdata
->key
, &mdata
->key_len
);
1092 if (strcmp(keyword
, "IV") == 0)
1093 return parse_bin(value
, &mdata
->iv
, &mdata
->iv_len
);
1094 if (strcmp(keyword
, "Custom") == 0)
1095 return parse_bin(value
, &mdata
->custom
, &mdata
->custom_len
);
1096 if (strcmp(keyword
, "Salt") == 0)
1097 return parse_bin(value
, &mdata
->salt
, &mdata
->salt_len
);
1098 if (strcmp(keyword
, "Algorithm") == 0) {
1099 mdata
->alg
= OPENSSL_strdup(value
);
1104 if (strcmp(keyword
, "Input") == 0)
1105 return parse_bin(value
, &mdata
->input
, &mdata
->input_len
);
1106 if (strcmp(keyword
, "Output") == 0)
1107 return parse_bin(value
, &mdata
->output
, &mdata
->output_len
);
1108 if (strcmp(keyword
, "Ctrl") == 0)
1109 return sk_OPENSSL_STRING_push(mdata
->controls
,
1110 OPENSSL_strdup(value
)) != 0;
1114 static int mac_test_ctrl_pkey(EVP_TEST
*t
, EVP_PKEY_CTX
*pctx
,
1120 if (!TEST_ptr(tmpval
= OPENSSL_strdup(value
)))
1122 p
= strchr(tmpval
, ':');
1125 rv
= EVP_PKEY_CTX_ctrl_str(pctx
, tmpval
, p
);
1127 t
->err
= "PKEY_CTRL_INVALID";
1129 t
->err
= "PKEY_CTRL_ERROR";
1132 OPENSSL_free(tmpval
);
1136 static int mac_test_run_pkey(EVP_TEST
*t
)
1138 MAC_DATA
*expected
= t
->data
;
1139 EVP_MD_CTX
*mctx
= NULL
;
1140 EVP_PKEY_CTX
*pctx
= NULL
, *genctx
= NULL
;
1141 EVP_PKEY
*key
= NULL
;
1142 const EVP_MD
*md
= NULL
;
1143 unsigned char *got
= NULL
;
1147 if (expected
->alg
== NULL
)
1148 TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected
->type
));
1150 TEST_info("Trying the EVP_PKEY %s test with %s",
1151 OBJ_nid2sn(expected
->type
), expected
->alg
);
1153 #ifdef OPENSSL_NO_DES
1154 if (expected
->alg
!= NULL
&& strstr(expected
->alg
, "DES") != NULL
) {
1161 if (expected
->type
== EVP_PKEY_CMAC
)
1162 key
= EVP_PKEY_new_CMAC_key(NULL
, expected
->key
, expected
->key_len
,
1163 EVP_get_cipherbyname(expected
->alg
));
1165 key
= EVP_PKEY_new_raw_private_key(expected
->type
, NULL
, expected
->key
,
1168 t
->err
= "MAC_KEY_CREATE_ERROR";
1172 if (expected
->type
== EVP_PKEY_HMAC
) {
1173 if (!TEST_ptr(md
= EVP_get_digestbyname(expected
->alg
))) {
1174 t
->err
= "MAC_ALGORITHM_SET_ERROR";
1178 if (!TEST_ptr(mctx
= EVP_MD_CTX_new())) {
1179 t
->err
= "INTERNAL_ERROR";
1182 if (!EVP_DigestSignInit(mctx
, &pctx
, md
, NULL
, key
)) {
1183 t
->err
= "DIGESTSIGNINIT_ERROR";
1186 for (i
= 0; i
< sk_OPENSSL_STRING_num(expected
->controls
); i
++)
1187 if (!mac_test_ctrl_pkey(t
, pctx
,
1188 sk_OPENSSL_STRING_value(expected
->controls
,
1190 t
->err
= "EVPPKEYCTXCTRL_ERROR";
1193 if (!EVP_DigestSignUpdate(mctx
, expected
->input
, expected
->input_len
)) {
1194 t
->err
= "DIGESTSIGNUPDATE_ERROR";
1197 if (!EVP_DigestSignFinal(mctx
, NULL
, &got_len
)) {
1198 t
->err
= "DIGESTSIGNFINAL_LENGTH_ERROR";
1201 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1202 t
->err
= "TEST_FAILURE";
1205 if (!EVP_DigestSignFinal(mctx
, got
, &got_len
)
1206 || !memory_err_compare(t
, "TEST_MAC_ERR",
1207 expected
->output
, expected
->output_len
,
1209 t
->err
= "TEST_MAC_ERR";
1214 EVP_MD_CTX_free(mctx
);
1216 EVP_PKEY_CTX_free(genctx
);
1221 static int mac_test_run_mac(EVP_TEST
*t
)
1223 MAC_DATA
*expected
= t
->data
;
1224 EVP_MAC_CTX
*ctx
= NULL
;
1225 unsigned char *got
= NULL
;
1228 OSSL_PARAM params
[21];
1229 size_t params_n
= 0;
1230 size_t params_n_allocstart
= 0;
1231 const OSSL_PARAM
*defined_params
=
1232 EVP_MAC_settable_ctx_params(expected
->mac
);
1234 if (expected
->alg
== NULL
)
1235 TEST_info("Trying the EVP_MAC %s test", expected
->mac_name
);
1237 TEST_info("Trying the EVP_MAC %s test with %s",
1238 expected
->mac_name
, expected
->alg
);
1240 #ifdef OPENSSL_NO_DES
1241 if (expected
->alg
!= NULL
&& strstr(expected
->alg
, "DES") != NULL
) {
1248 if (expected
->alg
!= NULL
) {
1250 * The underlying algorithm may be a cipher or a digest.
1251 * We don't know which it is, but we can ask the MAC what it
1252 * should be and bet on that.
1254 if (OSSL_PARAM_locate_const(defined_params
,
1255 OSSL_MAC_PARAM_CIPHER
) != NULL
) {
1256 params
[params_n
++] =
1257 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER
,
1259 } else if (OSSL_PARAM_locate_const(defined_params
,
1260 OSSL_MAC_PARAM_DIGEST
) != NULL
) {
1261 params
[params_n
++] =
1262 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST
,
1265 t
->err
= "MAC_BAD_PARAMS";
1269 if (expected
->key
!= NULL
)
1270 params
[params_n
++] =
1271 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY
,
1274 if (expected
->custom
!= NULL
)
1275 params
[params_n
++] =
1276 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM
,
1278 expected
->custom_len
);
1279 if (expected
->salt
!= NULL
)
1280 params
[params_n
++] =
1281 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT
,
1283 expected
->salt_len
);
1284 if (expected
->iv
!= NULL
)
1285 params
[params_n
++] =
1286 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV
,
1291 * Unknown controls. They must match parameters that the MAC recognises
1293 if (params_n
+ sk_OPENSSL_STRING_num(expected
->controls
)
1294 >= OSSL_NELEM(params
)) {
1295 t
->err
= "MAC_TOO_MANY_PARAMETERS";
1298 params_n_allocstart
= params_n
;
1299 for (i
= 0; i
< sk_OPENSSL_STRING_num(expected
->controls
); i
++) {
1300 char *tmpkey
, *tmpval
;
1301 char *value
= sk_OPENSSL_STRING_value(expected
->controls
, i
);
1303 if (!TEST_ptr(tmpkey
= OPENSSL_strdup(value
))) {
1304 t
->err
= "MAC_PARAM_ERROR";
1307 tmpval
= strchr(tmpkey
, ':');
1312 || !OSSL_PARAM_allocate_from_text(¶ms
[params_n
],
1315 strlen(tmpval
), NULL
)) {
1316 OPENSSL_free(tmpkey
);
1317 t
->err
= "MAC_PARAM_ERROR";
1322 OPENSSL_free(tmpkey
);
1324 params
[params_n
] = OSSL_PARAM_construct_end();
1326 if ((ctx
= EVP_MAC_CTX_new(expected
->mac
)) == NULL
) {
1327 t
->err
= "MAC_CREATE_ERROR";
1331 if (!EVP_MAC_CTX_set_params(ctx
, params
)) {
1332 t
->err
= "MAC_BAD_PARAMS";
1335 if (!EVP_MAC_init(ctx
)) {
1336 t
->err
= "MAC_INIT_ERROR";
1339 if (!EVP_MAC_update(ctx
, expected
->input
, expected
->input_len
)) {
1340 t
->err
= "MAC_UPDATE_ERROR";
1343 if (!EVP_MAC_final(ctx
, NULL
, &got_len
, 0)) {
1344 t
->err
= "MAC_FINAL_LENGTH_ERROR";
1347 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1348 t
->err
= "TEST_FAILURE";
1351 if (!EVP_MAC_final(ctx
, got
, &got_len
, got_len
)
1352 || !memory_err_compare(t
, "TEST_MAC_ERR",
1353 expected
->output
, expected
->output_len
,
1355 t
->err
= "TEST_MAC_ERR";
1360 while (params_n
-- > params_n_allocstart
) {
1361 OPENSSL_free(params
[params_n
].data
);
1363 EVP_MAC_CTX_free(ctx
);
1368 static int mac_test_run(EVP_TEST
*t
)
1370 MAC_DATA
*expected
= t
->data
;
1372 if (expected
->mac
!= NULL
)
1373 return mac_test_run_mac(t
);
1374 return mac_test_run_pkey(t
);
1377 static const EVP_TEST_METHOD mac_test_method
= {
1387 *** PUBLIC KEY TESTS
1388 *** These are all very similar and share much common code.
1391 typedef struct pkey_data_st
{
1392 /* Context for this operation */
1394 /* Key operation to perform */
1395 int (*keyop
) (EVP_PKEY_CTX
*ctx
,
1396 unsigned char *sig
, size_t *siglen
,
1397 const unsigned char *tbs
, size_t tbslen
);
1399 unsigned char *input
;
1401 /* Expected output */
1402 unsigned char *output
;
1407 * Perform public key operation setup: lookup key, allocated ctx and call
1408 * the appropriate initialisation function
1410 static int pkey_test_init(EVP_TEST
*t
, const char *name
,
1412 int (*keyopinit
) (EVP_PKEY_CTX
*ctx
),
1413 int (*keyop
)(EVP_PKEY_CTX
*ctx
,
1414 unsigned char *sig
, size_t *siglen
,
1415 const unsigned char *tbs
,
1419 EVP_PKEY
*pkey
= NULL
;
1423 rv
= find_key(&pkey
, name
, public_keys
);
1425 rv
= find_key(&pkey
, name
, private_keys
);
1426 if (rv
== 0 || pkey
== NULL
) {
1431 if (!TEST_ptr(kdata
= OPENSSL_zalloc(sizeof(*kdata
)))) {
1432 EVP_PKEY_free(pkey
);
1435 kdata
->keyop
= keyop
;
1436 if (!TEST_ptr(kdata
->ctx
= EVP_PKEY_CTX_new(pkey
, NULL
))) {
1437 EVP_PKEY_free(pkey
);
1438 OPENSSL_free(kdata
);
1441 if (keyopinit(kdata
->ctx
) <= 0)
1442 t
->err
= "KEYOP_INIT_ERROR";
1447 static void pkey_test_cleanup(EVP_TEST
*t
)
1449 PKEY_DATA
*kdata
= t
->data
;
1451 OPENSSL_free(kdata
->input
);
1452 OPENSSL_free(kdata
->output
);
1453 EVP_PKEY_CTX_free(kdata
->ctx
);
1456 static int pkey_test_ctrl(EVP_TEST
*t
, EVP_PKEY_CTX
*pctx
,
1462 if (!TEST_ptr(tmpval
= OPENSSL_strdup(value
)))
1464 p
= strchr(tmpval
, ':');
1467 rv
= EVP_PKEY_CTX_ctrl_str(pctx
, tmpval
, p
);
1469 t
->err
= "PKEY_CTRL_INVALID";
1471 } else if (p
!= NULL
&& rv
<= 0) {
1472 /* If p has an OID and lookup fails assume disabled algorithm */
1473 int nid
= OBJ_sn2nid(p
);
1475 if (nid
== NID_undef
)
1476 nid
= OBJ_ln2nid(p
);
1477 if (nid
!= NID_undef
1478 && EVP_get_digestbynid(nid
) == NULL
1479 && EVP_get_cipherbynid(nid
) == NULL
) {
1483 t
->err
= "PKEY_CTRL_ERROR";
1487 OPENSSL_free(tmpval
);
1491 static int pkey_test_parse(EVP_TEST
*t
,
1492 const char *keyword
, const char *value
)
1494 PKEY_DATA
*kdata
= t
->data
;
1495 if (strcmp(keyword
, "Input") == 0)
1496 return parse_bin(value
, &kdata
->input
, &kdata
->input_len
);
1497 if (strcmp(keyword
, "Output") == 0)
1498 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
1499 if (strcmp(keyword
, "Ctrl") == 0)
1500 return pkey_test_ctrl(t
, kdata
->ctx
, value
);
1504 static int pkey_test_run(EVP_TEST
*t
)
1506 PKEY_DATA
*expected
= t
->data
;
1507 unsigned char *got
= NULL
;
1509 EVP_PKEY_CTX
*copy
= NULL
;
1511 if (expected
->keyop(expected
->ctx
, NULL
, &got_len
,
1512 expected
->input
, expected
->input_len
) <= 0
1513 || !TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1514 t
->err
= "KEYOP_LENGTH_ERROR";
1517 if (expected
->keyop(expected
->ctx
, got
, &got_len
,
1518 expected
->input
, expected
->input_len
) <= 0) {
1519 t
->err
= "KEYOP_ERROR";
1522 if (!memory_err_compare(t
, "KEYOP_MISMATCH",
1523 expected
->output
, expected
->output_len
,
1531 /* Repeat the test on a copy. */
1532 if (!TEST_ptr(copy
= EVP_PKEY_CTX_dup(expected
->ctx
))) {
1533 t
->err
= "INTERNAL_ERROR";
1536 if (expected
->keyop(copy
, NULL
, &got_len
, expected
->input
,
1537 expected
->input_len
) <= 0
1538 || !TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1539 t
->err
= "KEYOP_LENGTH_ERROR";
1542 if (expected
->keyop(copy
, got
, &got_len
, expected
->input
,
1543 expected
->input_len
) <= 0) {
1544 t
->err
= "KEYOP_ERROR";
1547 if (!memory_err_compare(t
, "KEYOP_MISMATCH",
1548 expected
->output
, expected
->output_len
,
1554 EVP_PKEY_CTX_free(copy
);
1558 static int sign_test_init(EVP_TEST
*t
, const char *name
)
1560 return pkey_test_init(t
, name
, 0, EVP_PKEY_sign_init
, EVP_PKEY_sign
);
1563 static const EVP_TEST_METHOD psign_test_method
= {
1571 static int verify_recover_test_init(EVP_TEST
*t
, const char *name
)
1573 return pkey_test_init(t
, name
, 1, EVP_PKEY_verify_recover_init
,
1574 EVP_PKEY_verify_recover
);
1577 static const EVP_TEST_METHOD pverify_recover_test_method
= {
1579 verify_recover_test_init
,
1585 static int decrypt_test_init(EVP_TEST
*t
, const char *name
)
1587 return pkey_test_init(t
, name
, 0, EVP_PKEY_decrypt_init
,
1591 static const EVP_TEST_METHOD pdecrypt_test_method
= {
1599 static int verify_test_init(EVP_TEST
*t
, const char *name
)
1601 return pkey_test_init(t
, name
, 1, EVP_PKEY_verify_init
, 0);
1604 static int verify_test_run(EVP_TEST
*t
)
1606 PKEY_DATA
*kdata
= t
->data
;
1608 if (EVP_PKEY_verify(kdata
->ctx
, kdata
->output
, kdata
->output_len
,
1609 kdata
->input
, kdata
->input_len
) <= 0)
1610 t
->err
= "VERIFY_ERROR";
1614 static const EVP_TEST_METHOD pverify_test_method
= {
1623 static int pderive_test_init(EVP_TEST
*t
, const char *name
)
1625 return pkey_test_init(t
, name
, 0, EVP_PKEY_derive_init
, 0);
1628 static int pderive_test_parse(EVP_TEST
*t
,
1629 const char *keyword
, const char *value
)
1631 PKEY_DATA
*kdata
= t
->data
;
1633 if (strcmp(keyword
, "PeerKey") == 0) {
1635 if (find_key(&peer
, value
, public_keys
) == 0)
1637 if (EVP_PKEY_derive_set_peer(kdata
->ctx
, peer
) <= 0)
1641 if (strcmp(keyword
, "SharedSecret") == 0)
1642 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
1643 if (strcmp(keyword
, "Ctrl") == 0)
1644 return pkey_test_ctrl(t
, kdata
->ctx
, value
);
1648 static int pderive_test_run(EVP_TEST
*t
)
1650 PKEY_DATA
*expected
= t
->data
;
1651 unsigned char *got
= NULL
;
1654 if (EVP_PKEY_derive(expected
->ctx
, NULL
, &got_len
) <= 0) {
1655 t
->err
= "DERIVE_ERROR";
1658 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1659 t
->err
= "DERIVE_ERROR";
1662 if (EVP_PKEY_derive(expected
->ctx
, got
, &got_len
) <= 0) {
1663 t
->err
= "DERIVE_ERROR";
1666 if (!memory_err_compare(t
, "SHARED_SECRET_MISMATCH",
1667 expected
->output
, expected
->output_len
,
1677 static const EVP_TEST_METHOD pderive_test_method
= {
1690 typedef enum pbe_type_enum
{
1691 PBE_TYPE_INVALID
= 0,
1692 PBE_TYPE_SCRYPT
, PBE_TYPE_PBKDF2
, PBE_TYPE_PKCS12
1695 typedef struct pbe_data_st
{
1697 /* scrypt parameters */
1698 uint64_t N
, r
, p
, maxmem
;
1699 /* PKCS#12 parameters */
1703 unsigned char *pass
;
1706 unsigned char *salt
;
1708 /* Expected output */
1713 #ifndef OPENSSL_NO_SCRYPT
1715 * Parse unsigned decimal 64 bit integer value
1717 static int parse_uint64(const char *value
, uint64_t *pr
)
1719 const char *p
= value
;
1721 if (!TEST_true(*p
)) {
1722 TEST_info("Invalid empty integer value");
1725 for (*pr
= 0; *p
; ) {
1726 if (*pr
> UINT64_MAX
/ 10) {
1727 TEST_error("Integer overflow in string %s", value
);
1731 if (!TEST_true(isdigit((unsigned char)*p
))) {
1732 TEST_error("Invalid character in string %s", value
);
1741 static int scrypt_test_parse(EVP_TEST
*t
,
1742 const char *keyword
, const char *value
)
1744 PBE_DATA
*pdata
= t
->data
;
1746 if (strcmp(keyword
, "N") == 0)
1747 return parse_uint64(value
, &pdata
->N
);
1748 if (strcmp(keyword
, "p") == 0)
1749 return parse_uint64(value
, &pdata
->p
);
1750 if (strcmp(keyword
, "r") == 0)
1751 return parse_uint64(value
, &pdata
->r
);
1752 if (strcmp(keyword
, "maxmem") == 0)
1753 return parse_uint64(value
, &pdata
->maxmem
);
1758 static int pbkdf2_test_parse(EVP_TEST
*t
,
1759 const char *keyword
, const char *value
)
1761 PBE_DATA
*pdata
= t
->data
;
1763 if (strcmp(keyword
, "iter") == 0) {
1764 pdata
->iter
= atoi(value
);
1765 if (pdata
->iter
<= 0)
1769 if (strcmp(keyword
, "MD") == 0) {
1770 pdata
->md
= EVP_get_digestbyname(value
);
1771 if (pdata
->md
== NULL
)
1778 static int pkcs12_test_parse(EVP_TEST
*t
,
1779 const char *keyword
, const char *value
)
1781 PBE_DATA
*pdata
= t
->data
;
1783 if (strcmp(keyword
, "id") == 0) {
1784 pdata
->id
= atoi(value
);
1789 return pbkdf2_test_parse(t
, keyword
, value
);
1792 static int pbe_test_init(EVP_TEST
*t
, const char *alg
)
1795 PBE_TYPE pbe_type
= PBE_TYPE_INVALID
;
1797 if (strcmp(alg
, "scrypt") == 0) {
1798 #ifndef OPENSSL_NO_SCRYPT
1799 pbe_type
= PBE_TYPE_SCRYPT
;
1804 } else if (strcmp(alg
, "pbkdf2") == 0) {
1805 pbe_type
= PBE_TYPE_PBKDF2
;
1806 } else if (strcmp(alg
, "pkcs12") == 0) {
1807 pbe_type
= PBE_TYPE_PKCS12
;
1809 TEST_error("Unknown pbe algorithm %s", alg
);
1811 pdat
= OPENSSL_zalloc(sizeof(*pdat
));
1812 pdat
->pbe_type
= pbe_type
;
1817 static void pbe_test_cleanup(EVP_TEST
*t
)
1819 PBE_DATA
*pdat
= t
->data
;
1821 OPENSSL_free(pdat
->pass
);
1822 OPENSSL_free(pdat
->salt
);
1823 OPENSSL_free(pdat
->key
);
1826 static int pbe_test_parse(EVP_TEST
*t
,
1827 const char *keyword
, const char *value
)
1829 PBE_DATA
*pdata
= t
->data
;
1831 if (strcmp(keyword
, "Password") == 0)
1832 return parse_bin(value
, &pdata
->pass
, &pdata
->pass_len
);
1833 if (strcmp(keyword
, "Salt") == 0)
1834 return parse_bin(value
, &pdata
->salt
, &pdata
->salt_len
);
1835 if (strcmp(keyword
, "Key") == 0)
1836 return parse_bin(value
, &pdata
->key
, &pdata
->key_len
);
1837 if (pdata
->pbe_type
== PBE_TYPE_PBKDF2
)
1838 return pbkdf2_test_parse(t
, keyword
, value
);
1839 else if (pdata
->pbe_type
== PBE_TYPE_PKCS12
)
1840 return pkcs12_test_parse(t
, keyword
, value
);
1841 #ifndef OPENSSL_NO_SCRYPT
1842 else if (pdata
->pbe_type
== PBE_TYPE_SCRYPT
)
1843 return scrypt_test_parse(t
, keyword
, value
);
1848 static int pbe_test_run(EVP_TEST
*t
)
1850 PBE_DATA
*expected
= t
->data
;
1853 if (!TEST_ptr(key
= OPENSSL_malloc(expected
->key_len
))) {
1854 t
->err
= "INTERNAL_ERROR";
1857 if (expected
->pbe_type
== PBE_TYPE_PBKDF2
) {
1858 if (PKCS5_PBKDF2_HMAC((char *)expected
->pass
, expected
->pass_len
,
1859 expected
->salt
, expected
->salt_len
,
1860 expected
->iter
, expected
->md
,
1861 expected
->key_len
, key
) == 0) {
1862 t
->err
= "PBKDF2_ERROR";
1865 #ifndef OPENSSL_NO_SCRYPT
1866 } else if (expected
->pbe_type
== PBE_TYPE_SCRYPT
) {
1867 if (EVP_PBE_scrypt((const char *)expected
->pass
, expected
->pass_len
,
1868 expected
->salt
, expected
->salt_len
, expected
->N
,
1869 expected
->r
, expected
->p
, expected
->maxmem
,
1870 key
, expected
->key_len
) == 0) {
1871 t
->err
= "SCRYPT_ERROR";
1875 } else if (expected
->pbe_type
== PBE_TYPE_PKCS12
) {
1876 if (PKCS12_key_gen_uni(expected
->pass
, expected
->pass_len
,
1877 expected
->salt
, expected
->salt_len
,
1878 expected
->id
, expected
->iter
, expected
->key_len
,
1879 key
, expected
->md
) == 0) {
1880 t
->err
= "PKCS12_ERROR";
1884 if (!memory_err_compare(t
, "KEY_MISMATCH", expected
->key
, expected
->key_len
,
1885 key
, expected
->key_len
))
1894 static const EVP_TEST_METHOD pbe_test_method
= {
1908 BASE64_CANONICAL_ENCODING
= 0,
1909 BASE64_VALID_ENCODING
= 1,
1910 BASE64_INVALID_ENCODING
= 2
1911 } base64_encoding_type
;
1913 typedef struct encode_data_st
{
1914 /* Input to encoding */
1915 unsigned char *input
;
1917 /* Expected output */
1918 unsigned char *output
;
1920 base64_encoding_type encoding
;
1923 static int encode_test_init(EVP_TEST
*t
, const char *encoding
)
1927 if (!TEST_ptr(edata
= OPENSSL_zalloc(sizeof(*edata
))))
1929 if (strcmp(encoding
, "canonical") == 0) {
1930 edata
->encoding
= BASE64_CANONICAL_ENCODING
;
1931 } else if (strcmp(encoding
, "valid") == 0) {
1932 edata
->encoding
= BASE64_VALID_ENCODING
;
1933 } else if (strcmp(encoding
, "invalid") == 0) {
1934 edata
->encoding
= BASE64_INVALID_ENCODING
;
1935 if (!TEST_ptr(t
->expected_err
= OPENSSL_strdup("DECODE_ERROR")))
1938 TEST_error("Bad encoding: %s."
1939 " Should be one of {canonical, valid, invalid}",
1946 OPENSSL_free(edata
);
1950 static void encode_test_cleanup(EVP_TEST
*t
)
1952 ENCODE_DATA
*edata
= t
->data
;
1954 OPENSSL_free(edata
->input
);
1955 OPENSSL_free(edata
->output
);
1956 memset(edata
, 0, sizeof(*edata
));
1959 static int encode_test_parse(EVP_TEST
*t
,
1960 const char *keyword
, const char *value
)
1962 ENCODE_DATA
*edata
= t
->data
;
1964 if (strcmp(keyword
, "Input") == 0)
1965 return parse_bin(value
, &edata
->input
, &edata
->input_len
);
1966 if (strcmp(keyword
, "Output") == 0)
1967 return parse_bin(value
, &edata
->output
, &edata
->output_len
);
1971 static int encode_test_run(EVP_TEST
*t
)
1973 ENCODE_DATA
*expected
= t
->data
;
1974 unsigned char *encode_out
= NULL
, *decode_out
= NULL
;
1975 int output_len
, chunk_len
;
1976 EVP_ENCODE_CTX
*decode_ctx
= NULL
, *encode_ctx
= NULL
;
1978 if (!TEST_ptr(decode_ctx
= EVP_ENCODE_CTX_new())) {
1979 t
->err
= "INTERNAL_ERROR";
1983 if (expected
->encoding
== BASE64_CANONICAL_ENCODING
) {
1985 if (!TEST_ptr(encode_ctx
= EVP_ENCODE_CTX_new())
1986 || !TEST_ptr(encode_out
=
1987 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected
->input_len
))))
1990 EVP_EncodeInit(encode_ctx
);
1991 if (!TEST_true(EVP_EncodeUpdate(encode_ctx
, encode_out
, &chunk_len
,
1992 expected
->input
, expected
->input_len
)))
1995 output_len
= chunk_len
;
1997 EVP_EncodeFinal(encode_ctx
, encode_out
+ chunk_len
, &chunk_len
);
1998 output_len
+= chunk_len
;
2000 if (!memory_err_compare(t
, "BAD_ENCODING",
2001 expected
->output
, expected
->output_len
,
2002 encode_out
, output_len
))
2006 if (!TEST_ptr(decode_out
=
2007 OPENSSL_malloc(EVP_DECODE_LENGTH(expected
->output_len
))))
2010 EVP_DecodeInit(decode_ctx
);
2011 if (EVP_DecodeUpdate(decode_ctx
, decode_out
, &chunk_len
, expected
->output
,
2012 expected
->output_len
) < 0) {
2013 t
->err
= "DECODE_ERROR";
2016 output_len
= chunk_len
;
2018 if (EVP_DecodeFinal(decode_ctx
, decode_out
+ chunk_len
, &chunk_len
) != 1) {
2019 t
->err
= "DECODE_ERROR";
2022 output_len
+= chunk_len
;
2024 if (expected
->encoding
!= BASE64_INVALID_ENCODING
2025 && !memory_err_compare(t
, "BAD_DECODING",
2026 expected
->input
, expected
->input_len
,
2027 decode_out
, output_len
)) {
2028 t
->err
= "BAD_DECODING";
2034 OPENSSL_free(encode_out
);
2035 OPENSSL_free(decode_out
);
2036 EVP_ENCODE_CTX_free(decode_ctx
);
2037 EVP_ENCODE_CTX_free(encode_ctx
);
2041 static const EVP_TEST_METHOD encode_test_method
= {
2044 encode_test_cleanup
,
2054 typedef struct kdf_data_st
{
2055 /* Context for this operation */
2057 /* Expected output */
2058 unsigned char *output
;
2060 OSSL_PARAM params
[20];
2065 * Perform public key operation setup: lookup key, allocated ctx and call
2066 * the appropriate initialisation function
2068 static int kdf_test_init(EVP_TEST
*t
, const char *name
)
2073 #ifdef OPENSSL_NO_SCRYPT
2074 /* TODO(3.0) Replace with "scrypt" once aliases are supported */
2075 if (strcmp(name
, "id-scrypt") == 0) {
2079 #endif /* OPENSSL_NO_SCRYPT */
2081 #ifdef OPENSSL_NO_CMS
2082 if (strcmp(name
, "X942KDF") == 0) {
2086 #endif /* OPENSSL_NO_CMS */
2088 if (!TEST_ptr(kdata
= OPENSSL_zalloc(sizeof(*kdata
))))
2090 kdata
->p
= kdata
->params
;
2091 *kdata
->p
= OSSL_PARAM_construct_end();
2093 kdf
= EVP_KDF_fetch(NULL
, name
, NULL
);
2095 OPENSSL_free(kdata
);
2098 kdata
->ctx
= EVP_KDF_CTX_new(kdf
);
2100 if (kdata
->ctx
== NULL
) {
2101 OPENSSL_free(kdata
);
2108 static void kdf_test_cleanup(EVP_TEST
*t
)
2110 KDF_DATA
*kdata
= t
->data
;
2113 for (p
= kdata
->params
; p
->key
!= NULL
; p
++)
2114 OPENSSL_free(p
->data
);
2115 OPENSSL_free(kdata
->output
);
2116 EVP_KDF_CTX_free(kdata
->ctx
);
2119 static int kdf_test_ctrl(EVP_TEST
*t
, EVP_KDF_CTX
*kctx
,
2122 KDF_DATA
*kdata
= t
->data
;
2125 const OSSL_PARAM
*defs
= EVP_KDF_settable_ctx_params(EVP_KDF_CTX_kdf(kctx
));
2127 if (!TEST_ptr(name
= OPENSSL_strdup(value
)))
2129 p
= strchr(name
, ':');
2133 rv
= OSSL_PARAM_allocate_from_text(kdata
->p
, defs
, name
, p
,
2134 p
!= NULL
? strlen(p
) : 0, NULL
);
2135 *++kdata
->p
= OSSL_PARAM_construct_end();
2137 t
->err
= "KDF_PARAM_ERROR";
2141 if (p
!= NULL
&& strcmp(name
, "digest") == 0) {
2142 /* If p has an OID and lookup fails assume disabled algorithm */
2143 int nid
= OBJ_sn2nid(p
);
2145 if (nid
== NID_undef
)
2146 nid
= OBJ_ln2nid(p
);
2147 if (nid
!= NID_undef
&& EVP_get_digestbynid(nid
) == NULL
)
2150 if (p
!= NULL
&& strcmp(name
, "cipher") == 0) {
2151 /* If p has an OID and lookup fails assume disabled algorithm */
2152 int nid
= OBJ_sn2nid(p
);
2154 if (nid
== NID_undef
)
2155 nid
= OBJ_ln2nid(p
);
2156 if (nid
!= NID_undef
&& EVP_get_cipherbynid(nid
) == NULL
)
2163 static int kdf_test_parse(EVP_TEST
*t
,
2164 const char *keyword
, const char *value
)
2166 KDF_DATA
*kdata
= t
->data
;
2168 if (strcmp(keyword
, "Output") == 0)
2169 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
2170 if (strncmp(keyword
, "Ctrl", 4) == 0)
2171 return kdf_test_ctrl(t
, kdata
->ctx
, value
);
2175 static int kdf_test_run(EVP_TEST
*t
)
2177 KDF_DATA
*expected
= t
->data
;
2178 unsigned char *got
= NULL
;
2179 size_t got_len
= expected
->output_len
;
2181 if (!EVP_KDF_CTX_set_params(expected
->ctx
, expected
->params
)) {
2182 t
->err
= "KDF_CTRL_ERROR";
2185 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
2186 t
->err
= "INTERNAL_ERROR";
2189 if (EVP_KDF_derive(expected
->ctx
, got
, got_len
) <= 0) {
2190 t
->err
= "KDF_DERIVE_ERROR";
2193 if (!memory_err_compare(t
, "KDF_MISMATCH",
2194 expected
->output
, expected
->output_len
,
2205 static const EVP_TEST_METHOD kdf_test_method
= {
2218 typedef struct pkey_kdf_data_st
{
2219 /* Context for this operation */
2221 /* Expected output */
2222 unsigned char *output
;
2227 * Perform public key operation setup: lookup key, allocated ctx and call
2228 * the appropriate initialisation function
2230 static int pkey_kdf_test_init(EVP_TEST
*t
, const char *name
)
2232 PKEY_KDF_DATA
*kdata
;
2233 int kdf_nid
= OBJ_sn2nid(name
);
2235 #ifdef OPENSSL_NO_SCRYPT
2236 if (strcmp(name
, "scrypt") == 0) {
2240 #endif /* OPENSSL_NO_SCRYPT */
2242 #ifdef OPENSSL_NO_CMS
2243 if (strcmp(name
, "X942KDF") == 0) {
2247 #endif /* OPENSSL_NO_CMS */
2249 if (kdf_nid
== NID_undef
)
2250 kdf_nid
= OBJ_ln2nid(name
);
2252 if (!TEST_ptr(kdata
= OPENSSL_zalloc(sizeof(*kdata
))))
2254 kdata
->ctx
= EVP_PKEY_CTX_new_id(kdf_nid
, NULL
);
2255 if (kdata
->ctx
== NULL
) {
2256 OPENSSL_free(kdata
);
2259 if (EVP_PKEY_derive_init(kdata
->ctx
) <= 0) {
2260 EVP_PKEY_CTX_free(kdata
->ctx
);
2261 OPENSSL_free(kdata
);
2268 static void pkey_kdf_test_cleanup(EVP_TEST
*t
)
2270 PKEY_KDF_DATA
*kdata
= t
->data
;
2272 OPENSSL_free(kdata
->output
);
2273 EVP_PKEY_CTX_free(kdata
->ctx
);
2276 static int pkey_kdf_test_parse(EVP_TEST
*t
,
2277 const char *keyword
, const char *value
)
2279 PKEY_KDF_DATA
*kdata
= t
->data
;
2281 if (strcmp(keyword
, "Output") == 0)
2282 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
2283 if (strncmp(keyword
, "Ctrl", 4) == 0)
2284 return pkey_test_ctrl(t
, kdata
->ctx
, value
);
2288 static int pkey_kdf_test_run(EVP_TEST
*t
)
2290 PKEY_KDF_DATA
*expected
= t
->data
;
2291 unsigned char *got
= NULL
;
2292 size_t got_len
= expected
->output_len
;
2294 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
2295 t
->err
= "INTERNAL_ERROR";
2298 if (EVP_PKEY_derive(expected
->ctx
, got
, &got_len
) <= 0) {
2299 t
->err
= "KDF_DERIVE_ERROR";
2302 if (!TEST_mem_eq(expected
->output
, expected
->output_len
, got
, got_len
)) {
2303 t
->err
= "KDF_MISMATCH";
2313 static const EVP_TEST_METHOD pkey_kdf_test_method
= {
2316 pkey_kdf_test_cleanup
,
2317 pkey_kdf_test_parse
,
2326 typedef struct keypair_test_data_st
{
2329 } KEYPAIR_TEST_DATA
;
2331 static int keypair_test_init(EVP_TEST
*t
, const char *pair
)
2333 KEYPAIR_TEST_DATA
*data
;
2335 EVP_PKEY
*pk
= NULL
, *pubk
= NULL
;
2336 char *pub
, *priv
= NULL
;
2338 /* Split private and public names. */
2339 if (!TEST_ptr(priv
= OPENSSL_strdup(pair
))
2340 || !TEST_ptr(pub
= strchr(priv
, ':'))) {
2341 t
->err
= "PARSING_ERROR";
2346 if (!TEST_true(find_key(&pk
, priv
, private_keys
))) {
2347 TEST_info("Can't find private key: %s", priv
);
2348 t
->err
= "MISSING_PRIVATE_KEY";
2351 if (!TEST_true(find_key(&pubk
, pub
, public_keys
))) {
2352 TEST_info("Can't find public key: %s", pub
);
2353 t
->err
= "MISSING_PUBLIC_KEY";
2357 if (pk
== NULL
&& pubk
== NULL
) {
2358 /* Both keys are listed but unsupported: skip this test */
2364 if (!TEST_ptr(data
= OPENSSL_malloc(sizeof(*data
))))
2377 static void keypair_test_cleanup(EVP_TEST
*t
)
2379 OPENSSL_free(t
->data
);
2384 * For tests that do not accept any custom keywords.
2386 static int void_test_parse(EVP_TEST
*t
, const char *keyword
, const char *value
)
2391 static int keypair_test_run(EVP_TEST
*t
)
2394 const KEYPAIR_TEST_DATA
*pair
= t
->data
;
2396 if (pair
->privk
== NULL
|| pair
->pubk
== NULL
) {
2398 * this can only happen if only one of the keys is not set
2399 * which means that one of them was unsupported while the
2400 * other isn't: hence a key type mismatch.
2402 t
->err
= "KEYPAIR_TYPE_MISMATCH";
2407 if ((rv
= EVP_PKEY_cmp(pair
->privk
, pair
->pubk
)) != 1 ) {
2409 t
->err
= "KEYPAIR_MISMATCH";
2410 } else if ( -1 == rv
) {
2411 t
->err
= "KEYPAIR_TYPE_MISMATCH";
2412 } else if ( -2 == rv
) {
2413 t
->err
= "UNSUPPORTED_KEY_COMPARISON";
2415 TEST_error("Unexpected error in key comparison");
2430 static const EVP_TEST_METHOD keypair_test_method
= {
2433 keypair_test_cleanup
,
2442 typedef struct keygen_test_data_st
{
2443 EVP_PKEY_CTX
*genctx
; /* Keygen context to use */
2444 char *keyname
; /* Key name to store key or NULL */
2447 static int keygen_test_init(EVP_TEST
*t
, const char *alg
)
2449 KEYGEN_TEST_DATA
*data
;
2450 EVP_PKEY_CTX
*genctx
;
2451 int nid
= OBJ_sn2nid(alg
);
2453 if (nid
== NID_undef
) {
2454 nid
= OBJ_ln2nid(alg
);
2455 if (nid
== NID_undef
)
2459 if (!TEST_ptr(genctx
= EVP_PKEY_CTX_new_id(nid
, NULL
))) {
2460 /* assume algorithm disabled */
2465 if (EVP_PKEY_keygen_init(genctx
) <= 0) {
2466 t
->err
= "KEYGEN_INIT_ERROR";
2470 if (!TEST_ptr(data
= OPENSSL_malloc(sizeof(*data
))))
2472 data
->genctx
= genctx
;
2473 data
->keyname
= NULL
;
2479 EVP_PKEY_CTX_free(genctx
);
2483 static void keygen_test_cleanup(EVP_TEST
*t
)
2485 KEYGEN_TEST_DATA
*keygen
= t
->data
;
2487 EVP_PKEY_CTX_free(keygen
->genctx
);
2488 OPENSSL_free(keygen
->keyname
);
2489 OPENSSL_free(t
->data
);
2493 static int keygen_test_parse(EVP_TEST
*t
,
2494 const char *keyword
, const char *value
)
2496 KEYGEN_TEST_DATA
*keygen
= t
->data
;
2498 if (strcmp(keyword
, "KeyName") == 0)
2499 return TEST_ptr(keygen
->keyname
= OPENSSL_strdup(value
));
2500 if (strcmp(keyword
, "Ctrl") == 0)
2501 return pkey_test_ctrl(t
, keygen
->genctx
, value
);
2505 static int keygen_test_run(EVP_TEST
*t
)
2507 KEYGEN_TEST_DATA
*keygen
= t
->data
;
2508 EVP_PKEY
*pkey
= NULL
;
2511 if (EVP_PKEY_keygen(keygen
->genctx
, &pkey
) <= 0) {
2512 t
->err
= "KEYGEN_GENERATE_ERROR";
2516 if (keygen
->keyname
!= NULL
) {
2520 if (find_key(NULL
, keygen
->keyname
, private_keys
)) {
2521 TEST_info("Duplicate key %s", keygen
->keyname
);
2525 if (!TEST_ptr(key
= OPENSSL_malloc(sizeof(*key
))))
2527 key
->name
= keygen
->keyname
;
2528 keygen
->keyname
= NULL
;
2530 key
->next
= private_keys
;
2534 EVP_PKEY_free(pkey
);
2543 static const EVP_TEST_METHOD keygen_test_method
= {
2546 keygen_test_cleanup
,
2552 *** DIGEST SIGN+VERIFY TESTS
2556 int is_verify
; /* Set to 1 if verifying */
2557 int is_oneshot
; /* Set to 1 for one shot operation */
2558 const EVP_MD
*md
; /* Digest to use */
2559 EVP_MD_CTX
*ctx
; /* Digest context */
2561 STACK_OF(EVP_TEST_BUFFER
) *input
; /* Input data: streaming */
2562 unsigned char *osin
; /* Input data if one shot */
2563 size_t osin_len
; /* Input length data if one shot */
2564 unsigned char *output
; /* Expected output */
2565 size_t output_len
; /* Expected output length */
2568 static int digestsigver_test_init(EVP_TEST
*t
, const char *alg
, int is_verify
,
2571 const EVP_MD
*md
= NULL
;
2572 DIGESTSIGN_DATA
*mdat
;
2574 if (strcmp(alg
, "NULL") != 0) {
2575 if ((md
= EVP_get_digestbyname(alg
)) == NULL
) {
2576 /* If alg has an OID assume disabled algorithm */
2577 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
2584 if (!TEST_ptr(mdat
= OPENSSL_zalloc(sizeof(*mdat
))))
2587 if (!TEST_ptr(mdat
->ctx
= EVP_MD_CTX_new())) {
2591 mdat
->is_verify
= is_verify
;
2592 mdat
->is_oneshot
= is_oneshot
;
2597 static int digestsign_test_init(EVP_TEST
*t
, const char *alg
)
2599 return digestsigver_test_init(t
, alg
, 0, 0);
2602 static void digestsigver_test_cleanup(EVP_TEST
*t
)
2604 DIGESTSIGN_DATA
*mdata
= t
->data
;
2606 EVP_MD_CTX_free(mdata
->ctx
);
2607 sk_EVP_TEST_BUFFER_pop_free(mdata
->input
, evp_test_buffer_free
);
2608 OPENSSL_free(mdata
->osin
);
2609 OPENSSL_free(mdata
->output
);
2610 OPENSSL_free(mdata
);
2614 static int digestsigver_test_parse(EVP_TEST
*t
,
2615 const char *keyword
, const char *value
)
2617 DIGESTSIGN_DATA
*mdata
= t
->data
;
2619 if (strcmp(keyword
, "Key") == 0) {
2620 EVP_PKEY
*pkey
= NULL
;
2623 if (mdata
->is_verify
)
2624 rv
= find_key(&pkey
, value
, public_keys
);
2626 rv
= find_key(&pkey
, value
, private_keys
);
2627 if (rv
== 0 || pkey
== NULL
) {
2631 if (mdata
->is_verify
) {
2632 if (!EVP_DigestVerifyInit(mdata
->ctx
, &mdata
->pctx
, mdata
->md
,
2634 t
->err
= "DIGESTVERIFYINIT_ERROR";
2637 if (!EVP_DigestSignInit(mdata
->ctx
, &mdata
->pctx
, mdata
->md
, NULL
,
2639 t
->err
= "DIGESTSIGNINIT_ERROR";
2643 if (strcmp(keyword
, "Input") == 0) {
2644 if (mdata
->is_oneshot
)
2645 return parse_bin(value
, &mdata
->osin
, &mdata
->osin_len
);
2646 return evp_test_buffer_append(value
, &mdata
->input
);
2648 if (strcmp(keyword
, "Output") == 0)
2649 return parse_bin(value
, &mdata
->output
, &mdata
->output_len
);
2651 if (!mdata
->is_oneshot
) {
2652 if (strcmp(keyword
, "Count") == 0)
2653 return evp_test_buffer_set_count(value
, mdata
->input
);
2654 if (strcmp(keyword
, "Ncopy") == 0)
2655 return evp_test_buffer_ncopy(value
, mdata
->input
);
2657 if (strcmp(keyword
, "Ctrl") == 0) {
2658 if (mdata
->pctx
== NULL
)
2660 return pkey_test_ctrl(t
, mdata
->pctx
, value
);
2665 static int digestsign_update_fn(void *ctx
, const unsigned char *buf
,
2668 return EVP_DigestSignUpdate(ctx
, buf
, buflen
);
2671 static int digestsign_test_run(EVP_TEST
*t
)
2673 DIGESTSIGN_DATA
*expected
= t
->data
;
2674 unsigned char *got
= NULL
;
2677 if (!evp_test_buffer_do(expected
->input
, digestsign_update_fn
,
2679 t
->err
= "DIGESTUPDATE_ERROR";
2683 if (!EVP_DigestSignFinal(expected
->ctx
, NULL
, &got_len
)) {
2684 t
->err
= "DIGESTSIGNFINAL_LENGTH_ERROR";
2687 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
2688 t
->err
= "MALLOC_FAILURE";
2691 if (!EVP_DigestSignFinal(expected
->ctx
, got
, &got_len
)) {
2692 t
->err
= "DIGESTSIGNFINAL_ERROR";
2695 if (!memory_err_compare(t
, "SIGNATURE_MISMATCH",
2696 expected
->output
, expected
->output_len
,
2706 static const EVP_TEST_METHOD digestsign_test_method
= {
2708 digestsign_test_init
,
2709 digestsigver_test_cleanup
,
2710 digestsigver_test_parse
,
2714 static int digestverify_test_init(EVP_TEST
*t
, const char *alg
)
2716 return digestsigver_test_init(t
, alg
, 1, 0);
2719 static int digestverify_update_fn(void *ctx
, const unsigned char *buf
,
2722 return EVP_DigestVerifyUpdate(ctx
, buf
, buflen
);
2725 static int digestverify_test_run(EVP_TEST
*t
)
2727 DIGESTSIGN_DATA
*mdata
= t
->data
;
2729 if (!evp_test_buffer_do(mdata
->input
, digestverify_update_fn
, mdata
->ctx
)) {
2730 t
->err
= "DIGESTUPDATE_ERROR";
2734 if (EVP_DigestVerifyFinal(mdata
->ctx
, mdata
->output
,
2735 mdata
->output_len
) <= 0)
2736 t
->err
= "VERIFY_ERROR";
2740 static const EVP_TEST_METHOD digestverify_test_method
= {
2742 digestverify_test_init
,
2743 digestsigver_test_cleanup
,
2744 digestsigver_test_parse
,
2745 digestverify_test_run
2748 static int oneshot_digestsign_test_init(EVP_TEST
*t
, const char *alg
)
2750 return digestsigver_test_init(t
, alg
, 0, 1);
2753 static int oneshot_digestsign_test_run(EVP_TEST
*t
)
2755 DIGESTSIGN_DATA
*expected
= t
->data
;
2756 unsigned char *got
= NULL
;
2759 if (!EVP_DigestSign(expected
->ctx
, NULL
, &got_len
,
2760 expected
->osin
, expected
->osin_len
)) {
2761 t
->err
= "DIGESTSIGN_LENGTH_ERROR";
2764 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
2765 t
->err
= "MALLOC_FAILURE";
2768 if (!EVP_DigestSign(expected
->ctx
, got
, &got_len
,
2769 expected
->osin
, expected
->osin_len
)) {
2770 t
->err
= "DIGESTSIGN_ERROR";
2773 if (!memory_err_compare(t
, "SIGNATURE_MISMATCH",
2774 expected
->output
, expected
->output_len
,
2784 static const EVP_TEST_METHOD oneshot_digestsign_test_method
= {
2785 "OneShotDigestSign",
2786 oneshot_digestsign_test_init
,
2787 digestsigver_test_cleanup
,
2788 digestsigver_test_parse
,
2789 oneshot_digestsign_test_run
2792 static int oneshot_digestverify_test_init(EVP_TEST
*t
, const char *alg
)
2794 return digestsigver_test_init(t
, alg
, 1, 1);
2797 static int oneshot_digestverify_test_run(EVP_TEST
*t
)
2799 DIGESTSIGN_DATA
*mdata
= t
->data
;
2801 if (EVP_DigestVerify(mdata
->ctx
, mdata
->output
, mdata
->output_len
,
2802 mdata
->osin
, mdata
->osin_len
) <= 0)
2803 t
->err
= "VERIFY_ERROR";
2807 static const EVP_TEST_METHOD oneshot_digestverify_test_method
= {
2808 "OneShotDigestVerify",
2809 oneshot_digestverify_test_init
,
2810 digestsigver_test_cleanup
,
2811 digestsigver_test_parse
,
2812 oneshot_digestverify_test_run
2817 *** PARSING AND DISPATCH
2820 static const EVP_TEST_METHOD
*evp_test_list
[] = {
2821 &cipher_test_method
,
2822 &digest_test_method
,
2823 &digestsign_test_method
,
2824 &digestverify_test_method
,
2825 &encode_test_method
,
2827 &pkey_kdf_test_method
,
2828 &keypair_test_method
,
2829 &keygen_test_method
,
2831 &oneshot_digestsign_test_method
,
2832 &oneshot_digestverify_test_method
,
2834 &pdecrypt_test_method
,
2835 &pderive_test_method
,
2837 &pverify_recover_test_method
,
2838 &pverify_test_method
,
2842 static const EVP_TEST_METHOD
*find_test(const char *name
)
2844 const EVP_TEST_METHOD
**tt
;
2846 for (tt
= evp_test_list
; *tt
; tt
++) {
2847 if (strcmp(name
, (*tt
)->name
) == 0)
2853 static void clear_test(EVP_TEST
*t
)
2855 test_clearstanza(&t
->s
);
2857 if (t
->data
!= NULL
) {
2858 if (t
->meth
!= NULL
)
2859 t
->meth
->cleanup(t
);
2860 OPENSSL_free(t
->data
);
2863 OPENSSL_free(t
->expected_err
);
2864 t
->expected_err
= NULL
;
2865 OPENSSL_free(t
->reason
);
2875 * Check for errors in the test structure; return 1 if okay, else 0.
2877 static int check_test_error(EVP_TEST
*t
)
2882 if (t
->err
== NULL
&& t
->expected_err
== NULL
)
2884 if (t
->err
!= NULL
&& t
->expected_err
== NULL
) {
2885 if (t
->aux_err
!= NULL
) {
2886 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
2887 t
->s
.test_file
, t
->s
.start
, t
->aux_err
, t
->err
);
2889 TEST_info("%s:%d: Source of above error; unexpected error %s",
2890 t
->s
.test_file
, t
->s
.start
, t
->err
);
2894 if (t
->err
== NULL
&& t
->expected_err
!= NULL
) {
2895 TEST_info("%s:%d: Succeeded but was expecting %s",
2896 t
->s
.test_file
, t
->s
.start
, t
->expected_err
);
2900 if (strcmp(t
->err
, t
->expected_err
) != 0) {
2901 TEST_info("%s:%d: Expected %s got %s",
2902 t
->s
.test_file
, t
->s
.start
, t
->expected_err
, t
->err
);
2906 if (t
->reason
== NULL
)
2909 if (t
->reason
== NULL
) {
2910 TEST_info("%s:%d: Test is missing function or reason code",
2911 t
->s
.test_file
, t
->s
.start
);
2915 err
= ERR_peek_error();
2917 TEST_info("%s:%d: Expected error \"%s\" not set",
2918 t
->s
.test_file
, t
->s
.start
, t
->reason
);
2922 reason
= ERR_reason_error_string(err
);
2923 if (reason
== NULL
) {
2924 TEST_info("%s:%d: Expected error \"%s\", no strings available."
2926 t
->s
.test_file
, t
->s
.start
, t
->reason
);
2930 if (strcmp(reason
, t
->reason
) == 0)
2933 TEST_info("%s:%d: Expected error \"%s\", got \"%s\"",
2934 t
->s
.test_file
, t
->s
.start
, t
->reason
, reason
);
2940 * Run a parsed test. Log a message and return 0 on error.
2942 static int run_test(EVP_TEST
*t
)
2944 if (t
->meth
== NULL
)
2951 if (t
->err
== NULL
&& t
->meth
->run_test(t
) != 1) {
2952 TEST_info("%s:%d %s error",
2953 t
->s
.test_file
, t
->s
.start
, t
->meth
->name
);
2956 if (!check_test_error(t
)) {
2957 TEST_openssl_errors();
2966 static int find_key(EVP_PKEY
**ppk
, const char *name
, KEY_LIST
*lst
)
2968 for (; lst
!= NULL
; lst
= lst
->next
) {
2969 if (strcmp(lst
->name
, name
) == 0) {
2978 static void free_key_list(KEY_LIST
*lst
)
2980 while (lst
!= NULL
) {
2981 KEY_LIST
*next
= lst
->next
;
2983 EVP_PKEY_free(lst
->key
);
2984 OPENSSL_free(lst
->name
);
2991 * Is the key type an unsupported algorithm?
2993 static int key_unsupported(void)
2995 long err
= ERR_peek_error();
2997 if (ERR_GET_LIB(err
) == ERR_LIB_EVP
2998 && ERR_GET_REASON(err
) == EVP_R_UNSUPPORTED_ALGORITHM
) {
3002 #ifndef OPENSSL_NO_EC
3004 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
3005 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
3008 if (ERR_GET_LIB(err
) == ERR_LIB_EC
3009 && ERR_GET_REASON(err
) == EC_R_UNKNOWN_GROUP
) {
3013 #endif /* OPENSSL_NO_EC */
3018 * NULL out the value from |pp| but return it. This "steals" a pointer.
3020 static char *take_value(PAIR
*pp
)
3022 char *p
= pp
->value
;
3029 * Return 1 if one of the providers named in the string is available.
3030 * The provider names are separated with whitespace.
3031 * NOTE: destructive function, it inserts '\0' after each provider name.
3033 static int prov_available(char *providers
)
3039 for (; isspace(*providers
); providers
++)
3041 if (*providers
== '\0')
3042 break; /* End of the road */
3043 for (p
= providers
; *p
!= '\0' && !isspace(*p
); p
++)
3049 if (OSSL_PROVIDER_available(NULL
, providers
))
3050 return 1; /* Found one */
3056 * Read and parse one test. Return 0 if failure, 1 if okay.
3058 static int parse(EVP_TEST
*t
)
3060 KEY_LIST
*key
, **klist
;
3067 if (BIO_eof(t
->s
.fp
))
3070 if (!test_readstanza(&t
->s
))
3072 } while (t
->s
.numpairs
== 0);
3073 pp
= &t
->s
.pairs
[0];
3075 /* Are we adding a key? */
3078 if (strcmp(pp
->key
, "PrivateKey") == 0) {
3079 pkey
= PEM_read_bio_PrivateKey(t
->s
.key
, NULL
, 0, NULL
);
3080 if (pkey
== NULL
&& !key_unsupported()) {
3081 EVP_PKEY_free(pkey
);
3082 TEST_info("Can't read private key %s", pp
->value
);
3083 TEST_openssl_errors();
3086 klist
= &private_keys
;
3087 } else if (strcmp(pp
->key
, "PublicKey") == 0) {
3088 pkey
= PEM_read_bio_PUBKEY(t
->s
.key
, NULL
, 0, NULL
);
3089 if (pkey
== NULL
&& !key_unsupported()) {
3090 EVP_PKEY_free(pkey
);
3091 TEST_info("Can't read public key %s", pp
->value
);
3092 TEST_openssl_errors();
3095 klist
= &public_keys
;
3096 } else if (strcmp(pp
->key
, "PrivateKeyRaw") == 0
3097 || strcmp(pp
->key
, "PublicKeyRaw") == 0 ) {
3098 char *strnid
= NULL
, *keydata
= NULL
;
3099 unsigned char *keybin
;
3103 if (strcmp(pp
->key
, "PrivateKeyRaw") == 0)
3104 klist
= &private_keys
;
3106 klist
= &public_keys
;
3108 strnid
= strchr(pp
->value
, ':');
3109 if (strnid
!= NULL
) {
3111 keydata
= strchr(strnid
, ':');
3112 if (keydata
!= NULL
)
3115 if (keydata
== NULL
) {
3116 TEST_info("Failed to parse %s value", pp
->key
);
3120 nid
= OBJ_txt2nid(strnid
);
3121 if (nid
== NID_undef
) {
3122 TEST_info("Uncrecognised algorithm NID");
3125 if (!parse_bin(keydata
, &keybin
, &keylen
)) {
3126 TEST_info("Failed to create binary key");
3129 if (klist
== &private_keys
)
3130 pkey
= EVP_PKEY_new_raw_private_key(nid
, NULL
, keybin
, keylen
);
3132 pkey
= EVP_PKEY_new_raw_public_key(nid
, NULL
, keybin
, keylen
);
3133 if (pkey
== NULL
&& !key_unsupported()) {
3134 TEST_info("Can't read %s data", pp
->key
);
3135 OPENSSL_free(keybin
);
3136 TEST_openssl_errors();
3139 OPENSSL_free(keybin
);
3142 /* If we have a key add to list */
3143 if (klist
!= NULL
) {
3144 if (find_key(NULL
, pp
->value
, *klist
)) {
3145 TEST_info("Duplicate key %s", pp
->value
);
3148 if (!TEST_ptr(key
= OPENSSL_malloc(sizeof(*key
))))
3150 key
->name
= take_value(pp
);
3155 /* Go back and start a new stanza. */
3156 if (t
->s
.numpairs
!= 1)
3157 TEST_info("Line %d: missing blank line\n", t
->s
.curr
);
3161 /* Find the test, based on first keyword. */
3162 if (!TEST_ptr(t
->meth
= find_test(pp
->key
)))
3164 if (!t
->meth
->init(t
, pp
->value
)) {
3165 TEST_error("unknown %s: %s\n", pp
->key
, pp
->value
);
3169 /* TEST_info("skipping %s %s", pp->key, pp->value); */
3173 for (pp
++, i
= 1; i
< t
->s
.numpairs
; pp
++, i
++) {
3174 if (strcmp(pp
->key
, "Availablein") == 0) {
3175 if (!prov_available(pp
->value
)) {
3176 TEST_info("skipping, providers not available: %s:%d",
3177 t
->s
.test_file
, t
->s
.start
);
3181 } else if (strcmp(pp
->key
, "Result") == 0) {
3182 if (t
->expected_err
!= NULL
) {
3183 TEST_info("Line %d: multiple result lines", t
->s
.curr
);
3186 t
->expected_err
= take_value(pp
);
3187 } else if (strcmp(pp
->key
, "Function") == 0) {
3188 /* Ignore old line. */
3189 } else if (strcmp(pp
->key
, "Reason") == 0) {
3190 if (t
->reason
!= NULL
) {
3191 TEST_info("Line %d: multiple reason lines", t
->s
.curr
);
3194 t
->reason
= take_value(pp
);
3196 /* Must be test specific line: try to parse it */
3197 int rv
= t
->meth
->parse(t
, pp
->key
, pp
->value
);
3200 TEST_info("Line %d: unknown keyword %s", t
->s
.curr
, pp
->key
);
3204 TEST_info("Line %d: error processing keyword %s = %s\n",
3205 t
->s
.curr
, pp
->key
, pp
->value
);
3214 static int run_file_tests(int i
)
3217 const char *testfile
= test_get_argument(i
);
3220 if (!TEST_ptr(t
= OPENSSL_zalloc(sizeof(*t
))))
3222 if (!test_start_file(&t
->s
, testfile
)) {
3227 while (!BIO_eof(t
->s
.fp
)) {
3233 if (c
== 0 || !run_test(t
)) {
3238 test_end_file(&t
->s
);
3241 free_key_list(public_keys
);
3242 free_key_list(private_keys
);
3249 OPT_TEST_DECLARE_USAGE("file...\n")
3251 int setup_tests(void)
3255 if (!test_skip_common_options()) {
3256 TEST_error("Error parsing test options\n");
3260 n
= test_get_argument_count();
3264 ADD_ALL_TESTS(run_file_tests
, n
);