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 /* Input to digest */
329 STACK_OF(EVP_TEST_BUFFER
) *input
;
330 /* Expected output */
331 unsigned char *output
;
335 static int digest_test_init(EVP_TEST
*t
, const char *alg
)
338 const EVP_MD
*digest
;
340 if ((digest
= EVP_get_digestbyname(alg
)) == NULL
) {
341 /* If alg has an OID assume disabled algorithm */
342 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
348 if (!TEST_ptr(mdat
= OPENSSL_zalloc(sizeof(*mdat
))))
351 mdat
->digest
= digest
;
355 static void digest_test_cleanup(EVP_TEST
*t
)
357 DIGEST_DATA
*mdat
= t
->data
;
359 sk_EVP_TEST_BUFFER_pop_free(mdat
->input
, evp_test_buffer_free
);
360 OPENSSL_free(mdat
->output
);
363 static int digest_test_parse(EVP_TEST
*t
,
364 const char *keyword
, const char *value
)
366 DIGEST_DATA
*mdata
= t
->data
;
368 if (strcmp(keyword
, "Input") == 0)
369 return evp_test_buffer_append(value
, &mdata
->input
);
370 if (strcmp(keyword
, "Output") == 0)
371 return parse_bin(value
, &mdata
->output
, &mdata
->output_len
);
372 if (strcmp(keyword
, "Count") == 0)
373 return evp_test_buffer_set_count(value
, mdata
->input
);
374 if (strcmp(keyword
, "Ncopy") == 0)
375 return evp_test_buffer_ncopy(value
, mdata
->input
);
379 static int digest_update_fn(void *ctx
, const unsigned char *buf
, size_t buflen
)
381 return EVP_DigestUpdate(ctx
, buf
, buflen
);
384 static int digest_test_run(EVP_TEST
*t
)
386 DIGEST_DATA
*expected
= t
->data
;
388 unsigned char *got
= NULL
;
389 unsigned int got_len
;
391 t
->err
= "TEST_FAILURE";
392 if (!TEST_ptr(mctx
= EVP_MD_CTX_new()))
395 got
= OPENSSL_malloc(expected
->output_len
> EVP_MAX_MD_SIZE
?
396 expected
->output_len
: EVP_MAX_MD_SIZE
);
400 if (!EVP_DigestInit_ex(mctx
, expected
->digest
, NULL
)) {
401 t
->err
= "DIGESTINIT_ERROR";
404 if (!evp_test_buffer_do(expected
->input
, digest_update_fn
, mctx
)) {
405 t
->err
= "DIGESTUPDATE_ERROR";
409 if (EVP_MD_flags(expected
->digest
) & EVP_MD_FLAG_XOF
) {
410 got_len
= expected
->output_len
;
411 if (!EVP_DigestFinalXOF(mctx
, got
, got_len
)) {
412 t
->err
= "DIGESTFINALXOF_ERROR";
416 if (!EVP_DigestFinal(mctx
, got
, &got_len
)) {
417 t
->err
= "DIGESTFINAL_ERROR";
421 if (!TEST_int_eq(expected
->output_len
, got_len
)) {
422 t
->err
= "DIGEST_LENGTH_MISMATCH";
425 if (!memory_err_compare(t
, "DIGEST_MISMATCH",
426 expected
->output
, expected
->output_len
,
434 EVP_MD_CTX_free(mctx
);
438 static const EVP_TEST_METHOD digest_test_method
= {
451 typedef struct cipher_data_st
{
452 const EVP_CIPHER
*cipher
;
454 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
460 unsigned char *plaintext
;
461 size_t plaintext_len
;
462 unsigned char *ciphertext
;
463 size_t ciphertext_len
;
464 /* GCM, CCM, OCB and SIV only */
465 unsigned char *aad
[AAD_NUM
];
466 size_t aad_len
[AAD_NUM
];
472 static int cipher_test_init(EVP_TEST
*t
, const char *alg
)
474 const EVP_CIPHER
*cipher
;
478 if ((cipher
= EVP_get_cipherbyname(alg
)) == NULL
) {
479 /* If alg has an OID assume disabled algorithm */
480 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
486 cdat
= OPENSSL_zalloc(sizeof(*cdat
));
487 cdat
->cipher
= cipher
;
489 m
= EVP_CIPHER_mode(cipher
);
490 if (m
== EVP_CIPH_GCM_MODE
491 || m
== EVP_CIPH_OCB_MODE
492 || m
== EVP_CIPH_SIV_MODE
493 || m
== EVP_CIPH_CCM_MODE
)
495 else if (EVP_CIPHER_flags(cipher
) & EVP_CIPH_FLAG_AEAD_CIPHER
)
504 static void cipher_test_cleanup(EVP_TEST
*t
)
507 CIPHER_DATA
*cdat
= t
->data
;
509 OPENSSL_free(cdat
->key
);
510 OPENSSL_free(cdat
->iv
);
511 OPENSSL_free(cdat
->ciphertext
);
512 OPENSSL_free(cdat
->plaintext
);
513 for (i
= 0; i
< AAD_NUM
; i
++)
514 OPENSSL_free(cdat
->aad
[i
]);
515 OPENSSL_free(cdat
->tag
);
518 static int cipher_test_parse(EVP_TEST
*t
, const char *keyword
,
521 CIPHER_DATA
*cdat
= t
->data
;
524 if (strcmp(keyword
, "Key") == 0)
525 return parse_bin(value
, &cdat
->key
, &cdat
->key_len
);
526 if (strcmp(keyword
, "IV") == 0)
527 return parse_bin(value
, &cdat
->iv
, &cdat
->iv_len
);
528 if (strcmp(keyword
, "Plaintext") == 0)
529 return parse_bin(value
, &cdat
->plaintext
, &cdat
->plaintext_len
);
530 if (strcmp(keyword
, "Ciphertext") == 0)
531 return parse_bin(value
, &cdat
->ciphertext
, &cdat
->ciphertext_len
);
533 if (strcmp(keyword
, "AAD") == 0) {
534 for (i
= 0; i
< AAD_NUM
; i
++) {
535 if (cdat
->aad
[i
] == NULL
)
536 return parse_bin(value
, &cdat
->aad
[i
], &cdat
->aad_len
[i
]);
540 if (strcmp(keyword
, "Tag") == 0)
541 return parse_bin(value
, &cdat
->tag
, &cdat
->tag_len
);
542 if (strcmp(keyword
, "SetTagLate") == 0) {
543 if (strcmp(value
, "TRUE") == 0)
545 else if (strcmp(value
, "FALSE") == 0)
553 if (strcmp(keyword
, "Operation") == 0) {
554 if (strcmp(value
, "ENCRYPT") == 0)
556 else if (strcmp(value
, "DECRYPT") == 0)
565 static int cipher_test_enc(EVP_TEST
*t
, int enc
,
566 size_t out_misalign
, size_t inp_misalign
, int frag
)
568 CIPHER_DATA
*expected
= t
->data
;
569 unsigned char *in
, *expected_out
, *tmp
= NULL
;
570 size_t in_len
, out_len
, donelen
= 0;
571 int ok
= 0, tmplen
, chunklen
, tmpflen
, i
;
572 EVP_CIPHER_CTX
*ctx
= NULL
;
574 t
->err
= "TEST_FAILURE";
575 if (!TEST_ptr(ctx
= EVP_CIPHER_CTX_new()))
577 EVP_CIPHER_CTX_set_flags(ctx
, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
);
579 in
= expected
->plaintext
;
580 in_len
= expected
->plaintext_len
;
581 expected_out
= expected
->ciphertext
;
582 out_len
= expected
->ciphertext_len
;
584 in
= expected
->ciphertext
;
585 in_len
= expected
->ciphertext_len
;
586 expected_out
= expected
->plaintext
;
587 out_len
= expected
->plaintext_len
;
589 if (inp_misalign
== (size_t)-1) {
591 * Exercise in-place encryption
593 tmp
= OPENSSL_malloc(out_misalign
+ in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
);
596 in
= memcpy(tmp
+ out_misalign
, in
, in_len
);
598 inp_misalign
+= 16 - ((out_misalign
+ in_len
) & 15);
600 * 'tmp' will store both output and copy of input. We make the copy
601 * of input to specifically aligned part of 'tmp'. So we just
602 * figured out how much padding would ensure the required alignment,
603 * now we allocate extended buffer and finally copy the input just
604 * past inp_misalign in expression below. Output will be written
605 * past out_misalign...
607 tmp
= OPENSSL_malloc(out_misalign
+ in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
+
608 inp_misalign
+ in_len
);
611 in
= memcpy(tmp
+ out_misalign
+ in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
+
612 inp_misalign
, in
, in_len
);
614 if (!EVP_CipherInit_ex(ctx
, expected
->cipher
, NULL
, NULL
, NULL
, enc
)) {
615 t
->err
= "CIPHERINIT_ERROR";
619 if (expected
->aead
) {
620 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_IVLEN
,
621 expected
->iv_len
, 0)) {
622 t
->err
= "INVALID_IV_LENGTH";
625 } else if (expected
->iv_len
!= (size_t)EVP_CIPHER_CTX_iv_length(ctx
)) {
626 t
->err
= "INVALID_IV_LENGTH";
630 if (expected
->aead
) {
633 * If encrypting or OCB just set tag length initially, otherwise
634 * set tag length and value.
636 if (enc
|| expected
->aead
== EVP_CIPH_OCB_MODE
|| expected
->tag_late
) {
637 t
->err
= "TAG_LENGTH_SET_ERROR";
640 t
->err
= "TAG_SET_ERROR";
643 if (tag
|| expected
->aead
!= EVP_CIPH_GCM_MODE
) {
644 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_TAG
,
645 expected
->tag_len
, tag
))
650 if (!EVP_CIPHER_CTX_set_key_length(ctx
, expected
->key_len
)) {
651 t
->err
= "INVALID_KEY_LENGTH";
654 if (!EVP_CipherInit_ex(ctx
, NULL
, NULL
, expected
->key
, expected
->iv
, -1)) {
655 t
->err
= "KEY_SET_ERROR";
658 /* Check that we get the same IV back */
659 if (expected
->iv
!= NULL
660 && (EVP_CIPHER_flags(expected
->cipher
) & EVP_CIPH_CUSTOM_IV
) == 0
661 && !TEST_mem_eq(expected
->iv
, expected
->iv_len
,
662 EVP_CIPHER_CTX_iv(ctx
), expected
->iv_len
)) {
663 t
->err
= "INVALID_IV";
667 if (expected
->aead
== EVP_CIPH_CCM_MODE
) {
668 if (!EVP_CipherUpdate(ctx
, NULL
, &tmplen
, NULL
, out_len
)) {
669 t
->err
= "CCM_PLAINTEXT_LENGTH_SET_ERROR";
673 if (expected
->aad
[0] != NULL
) {
674 t
->err
= "AAD_SET_ERROR";
676 for (i
= 0; expected
->aad
[i
] != NULL
; i
++) {
677 if (!EVP_CipherUpdate(ctx
, NULL
, &chunklen
, expected
->aad
[i
],
678 expected
->aad_len
[i
]))
683 * Supply the AAD in chunks less than the block size where possible
685 for (i
= 0; expected
->aad
[i
] != NULL
; i
++) {
686 if (expected
->aad_len
[i
] > 0) {
687 if (!EVP_CipherUpdate(ctx
, NULL
, &chunklen
, expected
->aad
[i
], 1))
691 if (expected
->aad_len
[i
] > 2) {
692 if (!EVP_CipherUpdate(ctx
, NULL
, &chunklen
,
693 expected
->aad
[i
] + donelen
,
694 expected
->aad_len
[i
] - 2))
696 donelen
+= expected
->aad_len
[i
] - 2;
698 if (expected
->aad_len
[i
] > 1
699 && !EVP_CipherUpdate(ctx
, NULL
, &chunklen
,
700 expected
->aad
[i
] + donelen
, 1))
706 if (!enc
&& (expected
->aead
== EVP_CIPH_OCB_MODE
|| expected
->tag_late
)) {
707 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_TAG
,
708 expected
->tag_len
, expected
->tag
)) {
709 t
->err
= "TAG_SET_ERROR";
714 EVP_CIPHER_CTX_set_padding(ctx
, 0);
715 t
->err
= "CIPHERUPDATE_ERROR";
718 /* We supply the data all in one go */
719 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
, &tmplen
, in
, in_len
))
722 /* Supply the data in chunks less than the block size where possible */
724 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
, &chunklen
, in
, 1))
731 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
+ tmplen
, &chunklen
,
739 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
+ tmplen
, &chunklen
,
745 if (!EVP_CipherFinal_ex(ctx
, tmp
+ out_misalign
+ tmplen
, &tmpflen
)) {
746 t
->err
= "CIPHERFINAL_ERROR";
749 if (!memory_err_compare(t
, "VALUE_MISMATCH", expected_out
, out_len
,
750 tmp
+ out_misalign
, tmplen
+ tmpflen
))
752 if (enc
&& expected
->aead
) {
753 unsigned char rtag
[16];
755 if (!TEST_size_t_le(expected
->tag_len
, sizeof(rtag
))) {
756 t
->err
= "TAG_LENGTH_INTERNAL_ERROR";
759 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_GET_TAG
,
760 expected
->tag_len
, rtag
)) {
761 t
->err
= "TAG_RETRIEVE_ERROR";
764 if (!memory_err_compare(t
, "TAG_VALUE_MISMATCH",
765 expected
->tag
, expected
->tag_len
,
766 rtag
, expected
->tag_len
))
773 EVP_CIPHER_CTX_free(ctx
);
777 static int cipher_test_run(EVP_TEST
*t
)
779 CIPHER_DATA
*cdat
= t
->data
;
781 size_t out_misalign
, inp_misalign
;
787 if (!cdat
->iv
&& EVP_CIPHER_iv_length(cdat
->cipher
)) {
788 /* IV is optional and usually omitted in wrap mode */
789 if (EVP_CIPHER_mode(cdat
->cipher
) != EVP_CIPH_WRAP_MODE
) {
794 if (cdat
->aead
&& !cdat
->tag
) {
798 for (out_misalign
= 0; out_misalign
<= 1;) {
799 static char aux_err
[64];
800 t
->aux_err
= aux_err
;
801 for (inp_misalign
= (size_t)-1; inp_misalign
!= 2; inp_misalign
++) {
802 if (inp_misalign
== (size_t)-1) {
803 /* kludge: inp_misalign == -1 means "exercise in-place" */
804 BIO_snprintf(aux_err
, sizeof(aux_err
),
805 "%s in-place, %sfragmented",
806 out_misalign
? "misaligned" : "aligned",
809 BIO_snprintf(aux_err
, sizeof(aux_err
),
810 "%s output and %s input, %sfragmented",
811 out_misalign
? "misaligned" : "aligned",
812 inp_misalign
? "misaligned" : "aligned",
816 rv
= cipher_test_enc(t
, 1, out_misalign
, inp_misalign
, frag
);
817 /* Not fatal errors: return */
824 if (cdat
->enc
!= 1) {
825 rv
= cipher_test_enc(t
, 0, out_misalign
, inp_misalign
, frag
);
826 /* Not fatal errors: return */
835 if (out_misalign
== 1 && frag
== 0) {
837 * XTS, SIV, CCM and Wrap modes have special requirements about input
838 * lengths so we don't fragment for those
840 if (cdat
->aead
== EVP_CIPH_CCM_MODE
841 || EVP_CIPHER_mode(cdat
->cipher
) == EVP_CIPH_SIV_MODE
842 || EVP_CIPHER_mode(cdat
->cipher
) == EVP_CIPH_XTS_MODE
843 || EVP_CIPHER_mode(cdat
->cipher
) == EVP_CIPH_WRAP_MODE
)
856 static const EVP_TEST_METHOD cipher_test_method
= {
869 typedef struct mac_data_st
{
870 /* MAC type in one form or another */
871 EVP_MAC
*mac
; /* for mac_test_run_mac */
872 int type
; /* for mac_test_run_pkey */
873 /* Algorithm string for this MAC */
882 unsigned char *input
;
884 /* Expected output */
885 unsigned char *output
;
887 unsigned char *custom
;
889 /* MAC salt (blake2) */
892 /* Collection of controls */
893 STACK_OF(OPENSSL_STRING
) *controls
;
896 static int mac_test_init(EVP_TEST
*t
, const char *alg
)
899 int type
= NID_undef
;
902 if ((mac
= EVP_MAC_fetch(NULL
, alg
, NULL
)) == NULL
) {
904 * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
905 * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
906 * the EVP_PKEY method.
908 size_t sz
= strlen(alg
);
909 static const char epilogue
[] = " by EVP_PKEY";
911 if (sz
>= sizeof(epilogue
)
912 && strcmp(alg
+ sz
- (sizeof(epilogue
) - 1), epilogue
) == 0)
913 sz
-= sizeof(epilogue
) - 1;
915 if (strncmp(alg
, "HMAC", sz
) == 0) {
916 type
= EVP_PKEY_HMAC
;
917 } else if (strncmp(alg
, "CMAC", sz
) == 0) {
918 #ifndef OPENSSL_NO_CMAC
919 type
= EVP_PKEY_CMAC
;
924 } else if (strncmp(alg
, "Poly1305", sz
) == 0) {
925 #ifndef OPENSSL_NO_POLY1305
926 type
= EVP_PKEY_POLY1305
;
931 } else if (strncmp(alg
, "SipHash", sz
) == 0) {
932 #ifndef OPENSSL_NO_SIPHASH
933 type
= EVP_PKEY_SIPHASH
;
940 * Not a known EVP_PKEY method either. If it's a known OID, then
941 * assume it's been disabled.
943 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
952 mdat
= OPENSSL_zalloc(sizeof(*mdat
));
955 mdat
->controls
= sk_OPENSSL_STRING_new_null();
960 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
961 static void openssl_free(char *m
)
966 static void mac_test_cleanup(EVP_TEST
*t
)
968 MAC_DATA
*mdat
= t
->data
;
970 EVP_MAC_free(mdat
->mac
);
971 sk_OPENSSL_STRING_pop_free(mdat
->controls
, openssl_free
);
972 OPENSSL_free(mdat
->alg
);
973 OPENSSL_free(mdat
->key
);
974 OPENSSL_free(mdat
->iv
);
975 OPENSSL_free(mdat
->custom
);
976 OPENSSL_free(mdat
->salt
);
977 OPENSSL_free(mdat
->input
);
978 OPENSSL_free(mdat
->output
);
981 static int mac_test_parse(EVP_TEST
*t
,
982 const char *keyword
, const char *value
)
984 MAC_DATA
*mdata
= t
->data
;
986 if (strcmp(keyword
, "Key") == 0)
987 return parse_bin(value
, &mdata
->key
, &mdata
->key_len
);
988 if (strcmp(keyword
, "IV") == 0)
989 return parse_bin(value
, &mdata
->iv
, &mdata
->iv_len
);
990 if (strcmp(keyword
, "Custom") == 0)
991 return parse_bin(value
, &mdata
->custom
, &mdata
->custom_len
);
992 if (strcmp(keyword
, "Salt") == 0)
993 return parse_bin(value
, &mdata
->salt
, &mdata
->salt_len
);
994 if (strcmp(keyword
, "Algorithm") == 0) {
995 mdata
->alg
= OPENSSL_strdup(value
);
1000 if (strcmp(keyword
, "Input") == 0)
1001 return parse_bin(value
, &mdata
->input
, &mdata
->input_len
);
1002 if (strcmp(keyword
, "Output") == 0)
1003 return parse_bin(value
, &mdata
->output
, &mdata
->output_len
);
1004 if (strcmp(keyword
, "Ctrl") == 0)
1005 return sk_OPENSSL_STRING_push(mdata
->controls
,
1006 OPENSSL_strdup(value
)) != 0;
1010 static int mac_test_ctrl_pkey(EVP_TEST
*t
, EVP_PKEY_CTX
*pctx
,
1016 if (!TEST_ptr(tmpval
= OPENSSL_strdup(value
)))
1018 p
= strchr(tmpval
, ':');
1021 rv
= EVP_PKEY_CTX_ctrl_str(pctx
, tmpval
, p
);
1023 t
->err
= "PKEY_CTRL_INVALID";
1025 t
->err
= "PKEY_CTRL_ERROR";
1028 OPENSSL_free(tmpval
);
1032 static int mac_test_run_pkey(EVP_TEST
*t
)
1034 MAC_DATA
*expected
= t
->data
;
1035 EVP_MD_CTX
*mctx
= NULL
;
1036 EVP_PKEY_CTX
*pctx
= NULL
, *genctx
= NULL
;
1037 EVP_PKEY
*key
= NULL
;
1038 const EVP_MD
*md
= NULL
;
1039 unsigned char *got
= NULL
;
1043 if (expected
->alg
== NULL
)
1044 TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected
->type
));
1046 TEST_info("Trying the EVP_PKEY %s test with %s",
1047 OBJ_nid2sn(expected
->type
), expected
->alg
);
1049 #ifdef OPENSSL_NO_DES
1050 if (expected
->alg
!= NULL
&& strstr(expected
->alg
, "DES") != NULL
) {
1057 if (expected
->type
== EVP_PKEY_CMAC
)
1058 key
= EVP_PKEY_new_CMAC_key(NULL
, expected
->key
, expected
->key_len
,
1059 EVP_get_cipherbyname(expected
->alg
));
1061 key
= EVP_PKEY_new_raw_private_key(expected
->type
, NULL
, expected
->key
,
1064 t
->err
= "MAC_KEY_CREATE_ERROR";
1068 if (expected
->type
== EVP_PKEY_HMAC
) {
1069 if (!TEST_ptr(md
= EVP_get_digestbyname(expected
->alg
))) {
1070 t
->err
= "MAC_ALGORITHM_SET_ERROR";
1074 if (!TEST_ptr(mctx
= EVP_MD_CTX_new())) {
1075 t
->err
= "INTERNAL_ERROR";
1078 if (!EVP_DigestSignInit(mctx
, &pctx
, md
, NULL
, key
)) {
1079 t
->err
= "DIGESTSIGNINIT_ERROR";
1082 for (i
= 0; i
< sk_OPENSSL_STRING_num(expected
->controls
); i
++)
1083 if (!mac_test_ctrl_pkey(t
, pctx
,
1084 sk_OPENSSL_STRING_value(expected
->controls
,
1086 t
->err
= "EVPPKEYCTXCTRL_ERROR";
1089 if (!EVP_DigestSignUpdate(mctx
, expected
->input
, expected
->input_len
)) {
1090 t
->err
= "DIGESTSIGNUPDATE_ERROR";
1093 if (!EVP_DigestSignFinal(mctx
, NULL
, &got_len
)) {
1094 t
->err
= "DIGESTSIGNFINAL_LENGTH_ERROR";
1097 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1098 t
->err
= "TEST_FAILURE";
1101 if (!EVP_DigestSignFinal(mctx
, got
, &got_len
)
1102 || !memory_err_compare(t
, "TEST_MAC_ERR",
1103 expected
->output
, expected
->output_len
,
1105 t
->err
= "TEST_MAC_ERR";
1110 EVP_MD_CTX_free(mctx
);
1112 EVP_PKEY_CTX_free(genctx
);
1117 static int mac_test_run_mac(EVP_TEST
*t
)
1119 MAC_DATA
*expected
= t
->data
;
1120 EVP_MAC_CTX
*ctx
= NULL
;
1121 unsigned char *got
= NULL
;
1124 OSSL_PARAM params
[21];
1125 size_t params_n
= 0;
1126 size_t params_n_allocstart
= 0;
1127 const OSSL_PARAM
*defined_params
=
1128 EVP_MAC_CTX_settable_params(expected
->mac
);
1130 if (expected
->alg
== NULL
)
1131 TEST_info("Trying the EVP_MAC %s test", EVP_MAC_name(expected
->mac
));
1133 TEST_info("Trying the EVP_MAC %s test with %s",
1134 EVP_MAC_name(expected
->mac
), expected
->alg
);
1136 #ifdef OPENSSL_NO_DES
1137 if (expected
->alg
!= NULL
&& strstr(expected
->alg
, "DES") != NULL
) {
1144 if (expected
->alg
!= NULL
)
1145 params
[params_n
++] =
1146 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_ALGORITHM
,
1148 strlen(expected
->alg
) + 1);
1149 if (expected
->key
!= NULL
)
1150 params
[params_n
++] =
1151 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY
,
1154 if (expected
->custom
!= NULL
)
1155 params
[params_n
++] =
1156 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM
,
1158 expected
->custom_len
);
1159 if (expected
->salt
!= NULL
)
1160 params
[params_n
++] =
1161 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT
,
1163 expected
->salt_len
);
1164 if (expected
->iv
!= NULL
)
1165 params
[params_n
++] =
1166 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV
,
1171 * Unknown controls. They must match parameters that the MAC recognises
1173 if (params_n
+ sk_OPENSSL_STRING_num(expected
->controls
)
1174 >= OSSL_NELEM(params
)) {
1175 t
->err
= "MAC_TOO_MANY_PARAMETERS";
1178 params_n_allocstart
= params_n
;
1179 for (i
= 0; i
< sk_OPENSSL_STRING_num(expected
->controls
); i
++) {
1180 char *tmpkey
, *tmpval
;
1181 char *value
= sk_OPENSSL_STRING_value(expected
->controls
, i
);
1183 if (!TEST_ptr(tmpkey
= OPENSSL_strdup(value
))) {
1184 t
->err
= "MAC_PARAM_ERROR";
1187 tmpval
= strchr(tmpkey
, ':');
1191 if (!OSSL_PARAM_allocate_from_text(¶ms
[params_n
], defined_params
,
1194 OPENSSL_free(tmpkey
);
1195 t
->err
= "MAC_PARAM_ERROR";
1200 OPENSSL_free(tmpkey
);
1202 params
[params_n
] = OSSL_PARAM_construct_end();
1204 if ((ctx
= EVP_MAC_CTX_new(expected
->mac
)) == NULL
) {
1205 t
->err
= "MAC_CREATE_ERROR";
1209 if (!EVP_MAC_CTX_set_params(ctx
, params
)) {
1210 t
->err
= "MAC_BAD_PARAMS";
1213 if (!EVP_MAC_init(ctx
)) {
1214 t
->err
= "MAC_INIT_ERROR";
1217 if (!EVP_MAC_update(ctx
, expected
->input
, expected
->input_len
)) {
1218 t
->err
= "MAC_UPDATE_ERROR";
1221 if (!EVP_MAC_final(ctx
, NULL
, &got_len
, 0)) {
1222 t
->err
= "MAC_FINAL_LENGTH_ERROR";
1225 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1226 t
->err
= "TEST_FAILURE";
1229 if (!EVP_MAC_final(ctx
, got
, &got_len
, got_len
)
1230 || !memory_err_compare(t
, "TEST_MAC_ERR",
1231 expected
->output
, expected
->output_len
,
1233 t
->err
= "TEST_MAC_ERR";
1238 while (params_n
-- > params_n_allocstart
) {
1239 OPENSSL_free(params
[params_n
].data
);
1241 EVP_MAC_CTX_free(ctx
);
1246 static int mac_test_run(EVP_TEST
*t
)
1248 MAC_DATA
*expected
= t
->data
;
1250 if (expected
->mac
!= NULL
)
1251 return mac_test_run_mac(t
);
1252 return mac_test_run_pkey(t
);
1255 static const EVP_TEST_METHOD mac_test_method
= {
1265 *** PUBLIC KEY TESTS
1266 *** These are all very similar and share much common code.
1269 typedef struct pkey_data_st
{
1270 /* Context for this operation */
1272 /* Key operation to perform */
1273 int (*keyop
) (EVP_PKEY_CTX
*ctx
,
1274 unsigned char *sig
, size_t *siglen
,
1275 const unsigned char *tbs
, size_t tbslen
);
1277 unsigned char *input
;
1279 /* Expected output */
1280 unsigned char *output
;
1285 * Perform public key operation setup: lookup key, allocated ctx and call
1286 * the appropriate initialisation function
1288 static int pkey_test_init(EVP_TEST
*t
, const char *name
,
1290 int (*keyopinit
) (EVP_PKEY_CTX
*ctx
),
1291 int (*keyop
)(EVP_PKEY_CTX
*ctx
,
1292 unsigned char *sig
, size_t *siglen
,
1293 const unsigned char *tbs
,
1297 EVP_PKEY
*pkey
= NULL
;
1301 rv
= find_key(&pkey
, name
, public_keys
);
1303 rv
= find_key(&pkey
, name
, private_keys
);
1304 if (rv
== 0 || pkey
== NULL
) {
1309 if (!TEST_ptr(kdata
= OPENSSL_zalloc(sizeof(*kdata
)))) {
1310 EVP_PKEY_free(pkey
);
1313 kdata
->keyop
= keyop
;
1314 if (!TEST_ptr(kdata
->ctx
= EVP_PKEY_CTX_new(pkey
, NULL
))) {
1315 EVP_PKEY_free(pkey
);
1316 OPENSSL_free(kdata
);
1319 if (keyopinit(kdata
->ctx
) <= 0)
1320 t
->err
= "KEYOP_INIT_ERROR";
1325 static void pkey_test_cleanup(EVP_TEST
*t
)
1327 PKEY_DATA
*kdata
= t
->data
;
1329 OPENSSL_free(kdata
->input
);
1330 OPENSSL_free(kdata
->output
);
1331 EVP_PKEY_CTX_free(kdata
->ctx
);
1334 static int pkey_test_ctrl(EVP_TEST
*t
, EVP_PKEY_CTX
*pctx
,
1340 if (!TEST_ptr(tmpval
= OPENSSL_strdup(value
)))
1342 p
= strchr(tmpval
, ':');
1345 rv
= EVP_PKEY_CTX_ctrl_str(pctx
, tmpval
, p
);
1347 t
->err
= "PKEY_CTRL_INVALID";
1349 } else if (p
!= NULL
&& rv
<= 0) {
1350 /* If p has an OID and lookup fails assume disabled algorithm */
1351 int nid
= OBJ_sn2nid(p
);
1353 if (nid
== NID_undef
)
1354 nid
= OBJ_ln2nid(p
);
1355 if (nid
!= NID_undef
1356 && EVP_get_digestbynid(nid
) == NULL
1357 && EVP_get_cipherbynid(nid
) == NULL
) {
1361 t
->err
= "PKEY_CTRL_ERROR";
1365 OPENSSL_free(tmpval
);
1369 static int pkey_test_parse(EVP_TEST
*t
,
1370 const char *keyword
, const char *value
)
1372 PKEY_DATA
*kdata
= t
->data
;
1373 if (strcmp(keyword
, "Input") == 0)
1374 return parse_bin(value
, &kdata
->input
, &kdata
->input_len
);
1375 if (strcmp(keyword
, "Output") == 0)
1376 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
1377 if (strcmp(keyword
, "Ctrl") == 0)
1378 return pkey_test_ctrl(t
, kdata
->ctx
, value
);
1382 static int pkey_test_run(EVP_TEST
*t
)
1384 PKEY_DATA
*expected
= t
->data
;
1385 unsigned char *got
= NULL
;
1387 EVP_PKEY_CTX
*copy
= NULL
;
1389 if (expected
->keyop(expected
->ctx
, NULL
, &got_len
,
1390 expected
->input
, expected
->input_len
) <= 0
1391 || !TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1392 t
->err
= "KEYOP_LENGTH_ERROR";
1395 if (expected
->keyop(expected
->ctx
, got
, &got_len
,
1396 expected
->input
, expected
->input_len
) <= 0) {
1397 t
->err
= "KEYOP_ERROR";
1400 if (!memory_err_compare(t
, "KEYOP_MISMATCH",
1401 expected
->output
, expected
->output_len
,
1409 /* Repeat the test on a copy. */
1410 if (!TEST_ptr(copy
= EVP_PKEY_CTX_dup(expected
->ctx
))) {
1411 t
->err
= "INTERNAL_ERROR";
1414 if (expected
->keyop(copy
, NULL
, &got_len
, expected
->input
,
1415 expected
->input_len
) <= 0
1416 || !TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1417 t
->err
= "KEYOP_LENGTH_ERROR";
1420 if (expected
->keyop(copy
, got
, &got_len
, expected
->input
,
1421 expected
->input_len
) <= 0) {
1422 t
->err
= "KEYOP_ERROR";
1425 if (!memory_err_compare(t
, "KEYOP_MISMATCH",
1426 expected
->output
, expected
->output_len
,
1432 EVP_PKEY_CTX_free(copy
);
1436 static int sign_test_init(EVP_TEST
*t
, const char *name
)
1438 return pkey_test_init(t
, name
, 0, EVP_PKEY_sign_init
, EVP_PKEY_sign
);
1441 static const EVP_TEST_METHOD psign_test_method
= {
1449 static int verify_recover_test_init(EVP_TEST
*t
, const char *name
)
1451 return pkey_test_init(t
, name
, 1, EVP_PKEY_verify_recover_init
,
1452 EVP_PKEY_verify_recover
);
1455 static const EVP_TEST_METHOD pverify_recover_test_method
= {
1457 verify_recover_test_init
,
1463 static int decrypt_test_init(EVP_TEST
*t
, const char *name
)
1465 return pkey_test_init(t
, name
, 0, EVP_PKEY_decrypt_init
,
1469 static const EVP_TEST_METHOD pdecrypt_test_method
= {
1477 static int verify_test_init(EVP_TEST
*t
, const char *name
)
1479 return pkey_test_init(t
, name
, 1, EVP_PKEY_verify_init
, 0);
1482 static int verify_test_run(EVP_TEST
*t
)
1484 PKEY_DATA
*kdata
= t
->data
;
1486 if (EVP_PKEY_verify(kdata
->ctx
, kdata
->output
, kdata
->output_len
,
1487 kdata
->input
, kdata
->input_len
) <= 0)
1488 t
->err
= "VERIFY_ERROR";
1492 static const EVP_TEST_METHOD pverify_test_method
= {
1501 static int pderive_test_init(EVP_TEST
*t
, const char *name
)
1503 return pkey_test_init(t
, name
, 0, EVP_PKEY_derive_init
, 0);
1506 static int pderive_test_parse(EVP_TEST
*t
,
1507 const char *keyword
, const char *value
)
1509 PKEY_DATA
*kdata
= t
->data
;
1511 if (strcmp(keyword
, "PeerKey") == 0) {
1513 if (find_key(&peer
, value
, public_keys
) == 0)
1515 if (EVP_PKEY_derive_set_peer(kdata
->ctx
, peer
) <= 0)
1519 if (strcmp(keyword
, "SharedSecret") == 0)
1520 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
1521 if (strcmp(keyword
, "Ctrl") == 0)
1522 return pkey_test_ctrl(t
, kdata
->ctx
, value
);
1526 static int pderive_test_run(EVP_TEST
*t
)
1528 PKEY_DATA
*expected
= t
->data
;
1529 unsigned char *got
= NULL
;
1532 if (EVP_PKEY_derive(expected
->ctx
, NULL
, &got_len
) <= 0) {
1533 t
->err
= "DERIVE_ERROR";
1536 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1537 t
->err
= "DERIVE_ERROR";
1540 if (EVP_PKEY_derive(expected
->ctx
, got
, &got_len
) <= 0) {
1541 t
->err
= "DERIVE_ERROR";
1544 if (!memory_err_compare(t
, "SHARED_SECRET_MISMATCH",
1545 expected
->output
, expected
->output_len
,
1555 static const EVP_TEST_METHOD pderive_test_method
= {
1568 typedef enum pbe_type_enum
{
1569 PBE_TYPE_INVALID
= 0,
1570 PBE_TYPE_SCRYPT
, PBE_TYPE_PBKDF2
, PBE_TYPE_PKCS12
1573 typedef struct pbe_data_st
{
1575 /* scrypt parameters */
1576 uint64_t N
, r
, p
, maxmem
;
1577 /* PKCS#12 parameters */
1581 unsigned char *pass
;
1584 unsigned char *salt
;
1586 /* Expected output */
1591 #ifndef OPENSSL_NO_SCRYPT
1593 * Parse unsigned decimal 64 bit integer value
1595 static int parse_uint64(const char *value
, uint64_t *pr
)
1597 const char *p
= value
;
1599 if (!TEST_true(*p
)) {
1600 TEST_info("Invalid empty integer value");
1603 for (*pr
= 0; *p
; ) {
1604 if (*pr
> UINT64_MAX
/ 10) {
1605 TEST_error("Integer overflow in string %s", value
);
1609 if (!TEST_true(isdigit((unsigned char)*p
))) {
1610 TEST_error("Invalid character in string %s", value
);
1619 static int scrypt_test_parse(EVP_TEST
*t
,
1620 const char *keyword
, const char *value
)
1622 PBE_DATA
*pdata
= t
->data
;
1624 if (strcmp(keyword
, "N") == 0)
1625 return parse_uint64(value
, &pdata
->N
);
1626 if (strcmp(keyword
, "p") == 0)
1627 return parse_uint64(value
, &pdata
->p
);
1628 if (strcmp(keyword
, "r") == 0)
1629 return parse_uint64(value
, &pdata
->r
);
1630 if (strcmp(keyword
, "maxmem") == 0)
1631 return parse_uint64(value
, &pdata
->maxmem
);
1636 static int pbkdf2_test_parse(EVP_TEST
*t
,
1637 const char *keyword
, const char *value
)
1639 PBE_DATA
*pdata
= t
->data
;
1641 if (strcmp(keyword
, "iter") == 0) {
1642 pdata
->iter
= atoi(value
);
1643 if (pdata
->iter
<= 0)
1647 if (strcmp(keyword
, "MD") == 0) {
1648 pdata
->md
= EVP_get_digestbyname(value
);
1649 if (pdata
->md
== NULL
)
1656 static int pkcs12_test_parse(EVP_TEST
*t
,
1657 const char *keyword
, const char *value
)
1659 PBE_DATA
*pdata
= t
->data
;
1661 if (strcmp(keyword
, "id") == 0) {
1662 pdata
->id
= atoi(value
);
1667 return pbkdf2_test_parse(t
, keyword
, value
);
1670 static int pbe_test_init(EVP_TEST
*t
, const char *alg
)
1673 PBE_TYPE pbe_type
= PBE_TYPE_INVALID
;
1675 if (strcmp(alg
, "scrypt") == 0) {
1676 #ifndef OPENSSL_NO_SCRYPT
1677 pbe_type
= PBE_TYPE_SCRYPT
;
1682 } else if (strcmp(alg
, "pbkdf2") == 0) {
1683 pbe_type
= PBE_TYPE_PBKDF2
;
1684 } else if (strcmp(alg
, "pkcs12") == 0) {
1685 pbe_type
= PBE_TYPE_PKCS12
;
1687 TEST_error("Unknown pbe algorithm %s", alg
);
1689 pdat
= OPENSSL_zalloc(sizeof(*pdat
));
1690 pdat
->pbe_type
= pbe_type
;
1695 static void pbe_test_cleanup(EVP_TEST
*t
)
1697 PBE_DATA
*pdat
= t
->data
;
1699 OPENSSL_free(pdat
->pass
);
1700 OPENSSL_free(pdat
->salt
);
1701 OPENSSL_free(pdat
->key
);
1704 static int pbe_test_parse(EVP_TEST
*t
,
1705 const char *keyword
, const char *value
)
1707 PBE_DATA
*pdata
= t
->data
;
1709 if (strcmp(keyword
, "Password") == 0)
1710 return parse_bin(value
, &pdata
->pass
, &pdata
->pass_len
);
1711 if (strcmp(keyword
, "Salt") == 0)
1712 return parse_bin(value
, &pdata
->salt
, &pdata
->salt_len
);
1713 if (strcmp(keyword
, "Key") == 0)
1714 return parse_bin(value
, &pdata
->key
, &pdata
->key_len
);
1715 if (pdata
->pbe_type
== PBE_TYPE_PBKDF2
)
1716 return pbkdf2_test_parse(t
, keyword
, value
);
1717 else if (pdata
->pbe_type
== PBE_TYPE_PKCS12
)
1718 return pkcs12_test_parse(t
, keyword
, value
);
1719 #ifndef OPENSSL_NO_SCRYPT
1720 else if (pdata
->pbe_type
== PBE_TYPE_SCRYPT
)
1721 return scrypt_test_parse(t
, keyword
, value
);
1726 static int pbe_test_run(EVP_TEST
*t
)
1728 PBE_DATA
*expected
= t
->data
;
1731 if (!TEST_ptr(key
= OPENSSL_malloc(expected
->key_len
))) {
1732 t
->err
= "INTERNAL_ERROR";
1735 if (expected
->pbe_type
== PBE_TYPE_PBKDF2
) {
1736 if (PKCS5_PBKDF2_HMAC((char *)expected
->pass
, expected
->pass_len
,
1737 expected
->salt
, expected
->salt_len
,
1738 expected
->iter
, expected
->md
,
1739 expected
->key_len
, key
) == 0) {
1740 t
->err
= "PBKDF2_ERROR";
1743 #ifndef OPENSSL_NO_SCRYPT
1744 } else if (expected
->pbe_type
== PBE_TYPE_SCRYPT
) {
1745 if (EVP_PBE_scrypt((const char *)expected
->pass
, expected
->pass_len
,
1746 expected
->salt
, expected
->salt_len
, expected
->N
,
1747 expected
->r
, expected
->p
, expected
->maxmem
,
1748 key
, expected
->key_len
) == 0) {
1749 t
->err
= "SCRYPT_ERROR";
1753 } else if (expected
->pbe_type
== PBE_TYPE_PKCS12
) {
1754 if (PKCS12_key_gen_uni(expected
->pass
, expected
->pass_len
,
1755 expected
->salt
, expected
->salt_len
,
1756 expected
->id
, expected
->iter
, expected
->key_len
,
1757 key
, expected
->md
) == 0) {
1758 t
->err
= "PKCS12_ERROR";
1762 if (!memory_err_compare(t
, "KEY_MISMATCH", expected
->key
, expected
->key_len
,
1763 key
, expected
->key_len
))
1772 static const EVP_TEST_METHOD pbe_test_method
= {
1786 BASE64_CANONICAL_ENCODING
= 0,
1787 BASE64_VALID_ENCODING
= 1,
1788 BASE64_INVALID_ENCODING
= 2
1789 } base64_encoding_type
;
1791 typedef struct encode_data_st
{
1792 /* Input to encoding */
1793 unsigned char *input
;
1795 /* Expected output */
1796 unsigned char *output
;
1798 base64_encoding_type encoding
;
1801 static int encode_test_init(EVP_TEST
*t
, const char *encoding
)
1805 if (!TEST_ptr(edata
= OPENSSL_zalloc(sizeof(*edata
))))
1807 if (strcmp(encoding
, "canonical") == 0) {
1808 edata
->encoding
= BASE64_CANONICAL_ENCODING
;
1809 } else if (strcmp(encoding
, "valid") == 0) {
1810 edata
->encoding
= BASE64_VALID_ENCODING
;
1811 } else if (strcmp(encoding
, "invalid") == 0) {
1812 edata
->encoding
= BASE64_INVALID_ENCODING
;
1813 if (!TEST_ptr(t
->expected_err
= OPENSSL_strdup("DECODE_ERROR")))
1816 TEST_error("Bad encoding: %s."
1817 " Should be one of {canonical, valid, invalid}",
1824 OPENSSL_free(edata
);
1828 static void encode_test_cleanup(EVP_TEST
*t
)
1830 ENCODE_DATA
*edata
= t
->data
;
1832 OPENSSL_free(edata
->input
);
1833 OPENSSL_free(edata
->output
);
1834 memset(edata
, 0, sizeof(*edata
));
1837 static int encode_test_parse(EVP_TEST
*t
,
1838 const char *keyword
, const char *value
)
1840 ENCODE_DATA
*edata
= t
->data
;
1842 if (strcmp(keyword
, "Input") == 0)
1843 return parse_bin(value
, &edata
->input
, &edata
->input_len
);
1844 if (strcmp(keyword
, "Output") == 0)
1845 return parse_bin(value
, &edata
->output
, &edata
->output_len
);
1849 static int encode_test_run(EVP_TEST
*t
)
1851 ENCODE_DATA
*expected
= t
->data
;
1852 unsigned char *encode_out
= NULL
, *decode_out
= NULL
;
1853 int output_len
, chunk_len
;
1854 EVP_ENCODE_CTX
*decode_ctx
= NULL
, *encode_ctx
= NULL
;
1856 if (!TEST_ptr(decode_ctx
= EVP_ENCODE_CTX_new())) {
1857 t
->err
= "INTERNAL_ERROR";
1861 if (expected
->encoding
== BASE64_CANONICAL_ENCODING
) {
1863 if (!TEST_ptr(encode_ctx
= EVP_ENCODE_CTX_new())
1864 || !TEST_ptr(encode_out
=
1865 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected
->input_len
))))
1868 EVP_EncodeInit(encode_ctx
);
1869 if (!TEST_true(EVP_EncodeUpdate(encode_ctx
, encode_out
, &chunk_len
,
1870 expected
->input
, expected
->input_len
)))
1873 output_len
= chunk_len
;
1875 EVP_EncodeFinal(encode_ctx
, encode_out
+ chunk_len
, &chunk_len
);
1876 output_len
+= chunk_len
;
1878 if (!memory_err_compare(t
, "BAD_ENCODING",
1879 expected
->output
, expected
->output_len
,
1880 encode_out
, output_len
))
1884 if (!TEST_ptr(decode_out
=
1885 OPENSSL_malloc(EVP_DECODE_LENGTH(expected
->output_len
))))
1888 EVP_DecodeInit(decode_ctx
);
1889 if (EVP_DecodeUpdate(decode_ctx
, decode_out
, &chunk_len
, expected
->output
,
1890 expected
->output_len
) < 0) {
1891 t
->err
= "DECODE_ERROR";
1894 output_len
= chunk_len
;
1896 if (EVP_DecodeFinal(decode_ctx
, decode_out
+ chunk_len
, &chunk_len
) != 1) {
1897 t
->err
= "DECODE_ERROR";
1900 output_len
+= chunk_len
;
1902 if (expected
->encoding
!= BASE64_INVALID_ENCODING
1903 && !memory_err_compare(t
, "BAD_DECODING",
1904 expected
->input
, expected
->input_len
,
1905 decode_out
, output_len
)) {
1906 t
->err
= "BAD_DECODING";
1912 OPENSSL_free(encode_out
);
1913 OPENSSL_free(decode_out
);
1914 EVP_ENCODE_CTX_free(decode_ctx
);
1915 EVP_ENCODE_CTX_free(encode_ctx
);
1919 static const EVP_TEST_METHOD encode_test_method
= {
1922 encode_test_cleanup
,
1932 typedef struct kdf_data_st
{
1933 /* Context for this operation */
1935 /* Expected output */
1936 unsigned char *output
;
1941 * Perform public key operation setup: lookup key, allocated ctx and call
1942 * the appropriate initialisation function
1944 static int kdf_test_init(EVP_TEST
*t
, const char *name
)
1949 #ifdef OPENSSL_NO_SCRYPT
1950 if (strcmp(name
, "scrypt") == 0) {
1954 #endif /* OPENSSL_NO_SCRYPT */
1956 #ifdef OPENSSL_NO_CMS
1957 if (strcmp(name
, "X942KDF") == 0) {
1961 #endif /* OPENSSL_NO_CMS */
1963 kdf
= EVP_get_kdfbyname(name
);
1967 if (!TEST_ptr(kdata
= OPENSSL_zalloc(sizeof(*kdata
))))
1969 kdata
->ctx
= EVP_KDF_CTX_new(kdf
);
1970 if (kdata
->ctx
== NULL
) {
1971 OPENSSL_free(kdata
);
1978 static void kdf_test_cleanup(EVP_TEST
*t
)
1980 KDF_DATA
*kdata
= t
->data
;
1981 OPENSSL_free(kdata
->output
);
1982 EVP_KDF_CTX_free(kdata
->ctx
);
1985 static int kdf_test_ctrl(EVP_TEST
*t
, EVP_KDF_CTX
*kctx
,
1991 if (!TEST_ptr(tmpval
= OPENSSL_strdup(value
)))
1993 p
= strchr(tmpval
, ':');
1996 rv
= EVP_KDF_ctrl_str(kctx
, tmpval
, p
);
1998 t
->err
= "KDF_CTRL_INVALID";
2000 } else if (p
!= NULL
&& rv
<= 0) {
2001 /* If p has an OID and lookup fails assume disabled algorithm */
2002 int nid
= OBJ_sn2nid(p
);
2004 if (nid
== NID_undef
)
2005 nid
= OBJ_ln2nid(p
);
2006 if (nid
!= NID_undef
2007 && EVP_get_digestbynid(nid
) == NULL
2008 && EVP_get_cipherbynid(nid
) == NULL
) {
2012 t
->err
= "KDF_CTRL_ERROR";
2016 OPENSSL_free(tmpval
);
2020 static int kdf_test_parse(EVP_TEST
*t
,
2021 const char *keyword
, const char *value
)
2023 KDF_DATA
*kdata
= t
->data
;
2025 if (strcmp(keyword
, "Output") == 0)
2026 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
2027 if (strncmp(keyword
, "Ctrl", 4) == 0)
2028 return kdf_test_ctrl(t
, kdata
->ctx
, value
);
2032 static int kdf_test_run(EVP_TEST
*t
)
2034 KDF_DATA
*expected
= t
->data
;
2035 unsigned char *got
= NULL
;
2036 size_t got_len
= expected
->output_len
;
2038 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
2039 t
->err
= "INTERNAL_ERROR";
2042 if (EVP_KDF_derive(expected
->ctx
, got
, got_len
) <= 0) {
2043 t
->err
= "KDF_DERIVE_ERROR";
2046 if (!memory_err_compare(t
, "KDF_MISMATCH",
2047 expected
->output
, expected
->output_len
,
2058 static const EVP_TEST_METHOD kdf_test_method
= {
2071 typedef struct pkey_kdf_data_st
{
2072 /* Context for this operation */
2074 /* Expected output */
2075 unsigned char *output
;
2080 * Perform public key operation setup: lookup key, allocated ctx and call
2081 * the appropriate initialisation function
2083 static int pkey_kdf_test_init(EVP_TEST
*t
, const char *name
)
2085 PKEY_KDF_DATA
*kdata
;
2086 int kdf_nid
= OBJ_sn2nid(name
);
2088 #ifdef OPENSSL_NO_SCRYPT
2089 if (strcmp(name
, "scrypt") == 0) {
2093 #endif /* OPENSSL_NO_SCRYPT */
2095 #ifdef OPENSSL_NO_CMS
2096 if (strcmp(name
, "X942KDF") == 0) {
2100 #endif /* OPENSSL_NO_CMS */
2102 if (kdf_nid
== NID_undef
)
2103 kdf_nid
= OBJ_ln2nid(name
);
2105 if (!TEST_ptr(kdata
= OPENSSL_zalloc(sizeof(*kdata
))))
2107 kdata
->ctx
= EVP_PKEY_CTX_new_id(kdf_nid
, NULL
);
2108 if (kdata
->ctx
== NULL
) {
2109 OPENSSL_free(kdata
);
2112 if (EVP_PKEY_derive_init(kdata
->ctx
) <= 0) {
2113 EVP_PKEY_CTX_free(kdata
->ctx
);
2114 OPENSSL_free(kdata
);
2121 static void pkey_kdf_test_cleanup(EVP_TEST
*t
)
2123 PKEY_KDF_DATA
*kdata
= t
->data
;
2124 OPENSSL_free(kdata
->output
);
2125 EVP_PKEY_CTX_free(kdata
->ctx
);
2128 static int pkey_kdf_test_parse(EVP_TEST
*t
,
2129 const char *keyword
, const char *value
)
2131 PKEY_KDF_DATA
*kdata
= t
->data
;
2133 if (strcmp(keyword
, "Output") == 0)
2134 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
2135 if (strncmp(keyword
, "Ctrl", 4) == 0)
2136 return pkey_test_ctrl(t
, kdata
->ctx
, value
);
2140 static int pkey_kdf_test_run(EVP_TEST
*t
)
2142 PKEY_KDF_DATA
*expected
= t
->data
;
2143 unsigned char *got
= NULL
;
2144 size_t got_len
= expected
->output_len
;
2146 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
2147 t
->err
= "INTERNAL_ERROR";
2150 if (EVP_PKEY_derive(expected
->ctx
, got
, &got_len
) <= 0) {
2151 t
->err
= "KDF_DERIVE_ERROR";
2154 if (!TEST_mem_eq(expected
->output
, expected
->output_len
, got
, got_len
)) {
2155 t
->err
= "KDF_MISMATCH";
2165 static const EVP_TEST_METHOD pkey_kdf_test_method
= {
2168 pkey_kdf_test_cleanup
,
2169 pkey_kdf_test_parse
,
2178 typedef struct keypair_test_data_st
{
2181 } KEYPAIR_TEST_DATA
;
2183 static int keypair_test_init(EVP_TEST
*t
, const char *pair
)
2185 KEYPAIR_TEST_DATA
*data
;
2187 EVP_PKEY
*pk
= NULL
, *pubk
= NULL
;
2188 char *pub
, *priv
= NULL
;
2190 /* Split private and public names. */
2191 if (!TEST_ptr(priv
= OPENSSL_strdup(pair
))
2192 || !TEST_ptr(pub
= strchr(priv
, ':'))) {
2193 t
->err
= "PARSING_ERROR";
2198 if (!TEST_true(find_key(&pk
, priv
, private_keys
))) {
2199 TEST_info("Can't find private key: %s", priv
);
2200 t
->err
= "MISSING_PRIVATE_KEY";
2203 if (!TEST_true(find_key(&pubk
, pub
, public_keys
))) {
2204 TEST_info("Can't find public key: %s", pub
);
2205 t
->err
= "MISSING_PUBLIC_KEY";
2209 if (pk
== NULL
&& pubk
== NULL
) {
2210 /* Both keys are listed but unsupported: skip this test */
2216 if (!TEST_ptr(data
= OPENSSL_malloc(sizeof(*data
))))
2229 static void keypair_test_cleanup(EVP_TEST
*t
)
2231 OPENSSL_free(t
->data
);
2236 * For tests that do not accept any custom keywords.
2238 static int void_test_parse(EVP_TEST
*t
, const char *keyword
, const char *value
)
2243 static int keypair_test_run(EVP_TEST
*t
)
2246 const KEYPAIR_TEST_DATA
*pair
= t
->data
;
2248 if (pair
->privk
== NULL
|| pair
->pubk
== NULL
) {
2250 * this can only happen if only one of the keys is not set
2251 * which means that one of them was unsupported while the
2252 * other isn't: hence a key type mismatch.
2254 t
->err
= "KEYPAIR_TYPE_MISMATCH";
2259 if ((rv
= EVP_PKEY_cmp(pair
->privk
, pair
->pubk
)) != 1 ) {
2261 t
->err
= "KEYPAIR_MISMATCH";
2262 } else if ( -1 == rv
) {
2263 t
->err
= "KEYPAIR_TYPE_MISMATCH";
2264 } else if ( -2 == rv
) {
2265 t
->err
= "UNSUPPORTED_KEY_COMPARISON";
2267 TEST_error("Unexpected error in key comparison");
2282 static const EVP_TEST_METHOD keypair_test_method
= {
2285 keypair_test_cleanup
,
2294 typedef struct keygen_test_data_st
{
2295 EVP_PKEY_CTX
*genctx
; /* Keygen context to use */
2296 char *keyname
; /* Key name to store key or NULL */
2299 static int keygen_test_init(EVP_TEST
*t
, const char *alg
)
2301 KEYGEN_TEST_DATA
*data
;
2302 EVP_PKEY_CTX
*genctx
;
2303 int nid
= OBJ_sn2nid(alg
);
2305 if (nid
== NID_undef
) {
2306 nid
= OBJ_ln2nid(alg
);
2307 if (nid
== NID_undef
)
2311 if (!TEST_ptr(genctx
= EVP_PKEY_CTX_new_id(nid
, NULL
))) {
2312 /* assume algorithm disabled */
2317 if (EVP_PKEY_keygen_init(genctx
) <= 0) {
2318 t
->err
= "KEYGEN_INIT_ERROR";
2322 if (!TEST_ptr(data
= OPENSSL_malloc(sizeof(*data
))))
2324 data
->genctx
= genctx
;
2325 data
->keyname
= NULL
;
2331 EVP_PKEY_CTX_free(genctx
);
2335 static void keygen_test_cleanup(EVP_TEST
*t
)
2337 KEYGEN_TEST_DATA
*keygen
= t
->data
;
2339 EVP_PKEY_CTX_free(keygen
->genctx
);
2340 OPENSSL_free(keygen
->keyname
);
2341 OPENSSL_free(t
->data
);
2345 static int keygen_test_parse(EVP_TEST
*t
,
2346 const char *keyword
, const char *value
)
2348 KEYGEN_TEST_DATA
*keygen
= t
->data
;
2350 if (strcmp(keyword
, "KeyName") == 0)
2351 return TEST_ptr(keygen
->keyname
= OPENSSL_strdup(value
));
2352 if (strcmp(keyword
, "Ctrl") == 0)
2353 return pkey_test_ctrl(t
, keygen
->genctx
, value
);
2357 static int keygen_test_run(EVP_TEST
*t
)
2359 KEYGEN_TEST_DATA
*keygen
= t
->data
;
2360 EVP_PKEY
*pkey
= NULL
;
2363 if (EVP_PKEY_keygen(keygen
->genctx
, &pkey
) <= 0) {
2364 t
->err
= "KEYGEN_GENERATE_ERROR";
2368 if (keygen
->keyname
!= NULL
) {
2371 if (find_key(NULL
, keygen
->keyname
, private_keys
)) {
2372 TEST_info("Duplicate key %s", keygen
->keyname
);
2376 if (!TEST_ptr(key
= OPENSSL_malloc(sizeof(*key
))))
2378 key
->name
= keygen
->keyname
;
2379 keygen
->keyname
= NULL
;
2381 key
->next
= private_keys
;
2384 EVP_PKEY_free(pkey
);
2390 EVP_PKEY_free(pkey
);
2394 static const EVP_TEST_METHOD keygen_test_method
= {
2397 keygen_test_cleanup
,
2403 *** DIGEST SIGN+VERIFY TESTS
2407 int is_verify
; /* Set to 1 if verifying */
2408 int is_oneshot
; /* Set to 1 for one shot operation */
2409 const EVP_MD
*md
; /* Digest to use */
2410 EVP_MD_CTX
*ctx
; /* Digest context */
2412 STACK_OF(EVP_TEST_BUFFER
) *input
; /* Input data: streaming */
2413 unsigned char *osin
; /* Input data if one shot */
2414 size_t osin_len
; /* Input length data if one shot */
2415 unsigned char *output
; /* Expected output */
2416 size_t output_len
; /* Expected output length */
2419 static int digestsigver_test_init(EVP_TEST
*t
, const char *alg
, int is_verify
,
2422 const EVP_MD
*md
= NULL
;
2423 DIGESTSIGN_DATA
*mdat
;
2425 if (strcmp(alg
, "NULL") != 0) {
2426 if ((md
= EVP_get_digestbyname(alg
)) == NULL
) {
2427 /* If alg has an OID assume disabled algorithm */
2428 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
2435 if (!TEST_ptr(mdat
= OPENSSL_zalloc(sizeof(*mdat
))))
2438 if (!TEST_ptr(mdat
->ctx
= EVP_MD_CTX_new())) {
2442 mdat
->is_verify
= is_verify
;
2443 mdat
->is_oneshot
= is_oneshot
;
2448 static int digestsign_test_init(EVP_TEST
*t
, const char *alg
)
2450 return digestsigver_test_init(t
, alg
, 0, 0);
2453 static void digestsigver_test_cleanup(EVP_TEST
*t
)
2455 DIGESTSIGN_DATA
*mdata
= t
->data
;
2457 EVP_MD_CTX_free(mdata
->ctx
);
2458 sk_EVP_TEST_BUFFER_pop_free(mdata
->input
, evp_test_buffer_free
);
2459 OPENSSL_free(mdata
->osin
);
2460 OPENSSL_free(mdata
->output
);
2461 OPENSSL_free(mdata
);
2465 static int digestsigver_test_parse(EVP_TEST
*t
,
2466 const char *keyword
, const char *value
)
2468 DIGESTSIGN_DATA
*mdata
= t
->data
;
2470 if (strcmp(keyword
, "Key") == 0) {
2471 EVP_PKEY
*pkey
= NULL
;
2474 if (mdata
->is_verify
)
2475 rv
= find_key(&pkey
, value
, public_keys
);
2477 rv
= find_key(&pkey
, value
, private_keys
);
2478 if (rv
== 0 || pkey
== NULL
) {
2482 if (mdata
->is_verify
) {
2483 if (!EVP_DigestVerifyInit(mdata
->ctx
, &mdata
->pctx
, mdata
->md
,
2485 t
->err
= "DIGESTVERIFYINIT_ERROR";
2488 if (!EVP_DigestSignInit(mdata
->ctx
, &mdata
->pctx
, mdata
->md
, NULL
,
2490 t
->err
= "DIGESTSIGNINIT_ERROR";
2494 if (strcmp(keyword
, "Input") == 0) {
2495 if (mdata
->is_oneshot
)
2496 return parse_bin(value
, &mdata
->osin
, &mdata
->osin_len
);
2497 return evp_test_buffer_append(value
, &mdata
->input
);
2499 if (strcmp(keyword
, "Output") == 0)
2500 return parse_bin(value
, &mdata
->output
, &mdata
->output_len
);
2502 if (!mdata
->is_oneshot
) {
2503 if (strcmp(keyword
, "Count") == 0)
2504 return evp_test_buffer_set_count(value
, mdata
->input
);
2505 if (strcmp(keyword
, "Ncopy") == 0)
2506 return evp_test_buffer_ncopy(value
, mdata
->input
);
2508 if (strcmp(keyword
, "Ctrl") == 0) {
2509 if (mdata
->pctx
== NULL
)
2511 return pkey_test_ctrl(t
, mdata
->pctx
, value
);
2516 static int digestsign_update_fn(void *ctx
, const unsigned char *buf
,
2519 return EVP_DigestSignUpdate(ctx
, buf
, buflen
);
2522 static int digestsign_test_run(EVP_TEST
*t
)
2524 DIGESTSIGN_DATA
*expected
= t
->data
;
2525 unsigned char *got
= NULL
;
2528 if (!evp_test_buffer_do(expected
->input
, digestsign_update_fn
,
2530 t
->err
= "DIGESTUPDATE_ERROR";
2534 if (!EVP_DigestSignFinal(expected
->ctx
, NULL
, &got_len
)) {
2535 t
->err
= "DIGESTSIGNFINAL_LENGTH_ERROR";
2538 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
2539 t
->err
= "MALLOC_FAILURE";
2542 if (!EVP_DigestSignFinal(expected
->ctx
, got
, &got_len
)) {
2543 t
->err
= "DIGESTSIGNFINAL_ERROR";
2546 if (!memory_err_compare(t
, "SIGNATURE_MISMATCH",
2547 expected
->output
, expected
->output_len
,
2557 static const EVP_TEST_METHOD digestsign_test_method
= {
2559 digestsign_test_init
,
2560 digestsigver_test_cleanup
,
2561 digestsigver_test_parse
,
2565 static int digestverify_test_init(EVP_TEST
*t
, const char *alg
)
2567 return digestsigver_test_init(t
, alg
, 1, 0);
2570 static int digestverify_update_fn(void *ctx
, const unsigned char *buf
,
2573 return EVP_DigestVerifyUpdate(ctx
, buf
, buflen
);
2576 static int digestverify_test_run(EVP_TEST
*t
)
2578 DIGESTSIGN_DATA
*mdata
= t
->data
;
2580 if (!evp_test_buffer_do(mdata
->input
, digestverify_update_fn
, mdata
->ctx
)) {
2581 t
->err
= "DIGESTUPDATE_ERROR";
2585 if (EVP_DigestVerifyFinal(mdata
->ctx
, mdata
->output
,
2586 mdata
->output_len
) <= 0)
2587 t
->err
= "VERIFY_ERROR";
2591 static const EVP_TEST_METHOD digestverify_test_method
= {
2593 digestverify_test_init
,
2594 digestsigver_test_cleanup
,
2595 digestsigver_test_parse
,
2596 digestverify_test_run
2599 static int oneshot_digestsign_test_init(EVP_TEST
*t
, const char *alg
)
2601 return digestsigver_test_init(t
, alg
, 0, 1);
2604 static int oneshot_digestsign_test_run(EVP_TEST
*t
)
2606 DIGESTSIGN_DATA
*expected
= t
->data
;
2607 unsigned char *got
= NULL
;
2610 if (!EVP_DigestSign(expected
->ctx
, NULL
, &got_len
,
2611 expected
->osin
, expected
->osin_len
)) {
2612 t
->err
= "DIGESTSIGN_LENGTH_ERROR";
2615 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
2616 t
->err
= "MALLOC_FAILURE";
2619 if (!EVP_DigestSign(expected
->ctx
, got
, &got_len
,
2620 expected
->osin
, expected
->osin_len
)) {
2621 t
->err
= "DIGESTSIGN_ERROR";
2624 if (!memory_err_compare(t
, "SIGNATURE_MISMATCH",
2625 expected
->output
, expected
->output_len
,
2635 static const EVP_TEST_METHOD oneshot_digestsign_test_method
= {
2636 "OneShotDigestSign",
2637 oneshot_digestsign_test_init
,
2638 digestsigver_test_cleanup
,
2639 digestsigver_test_parse
,
2640 oneshot_digestsign_test_run
2643 static int oneshot_digestverify_test_init(EVP_TEST
*t
, const char *alg
)
2645 return digestsigver_test_init(t
, alg
, 1, 1);
2648 static int oneshot_digestverify_test_run(EVP_TEST
*t
)
2650 DIGESTSIGN_DATA
*mdata
= t
->data
;
2652 if (EVP_DigestVerify(mdata
->ctx
, mdata
->output
, mdata
->output_len
,
2653 mdata
->osin
, mdata
->osin_len
) <= 0)
2654 t
->err
= "VERIFY_ERROR";
2658 static const EVP_TEST_METHOD oneshot_digestverify_test_method
= {
2659 "OneShotDigestVerify",
2660 oneshot_digestverify_test_init
,
2661 digestsigver_test_cleanup
,
2662 digestsigver_test_parse
,
2663 oneshot_digestverify_test_run
2668 *** PARSING AND DISPATCH
2671 static const EVP_TEST_METHOD
*evp_test_list
[] = {
2672 &cipher_test_method
,
2673 &digest_test_method
,
2674 &digestsign_test_method
,
2675 &digestverify_test_method
,
2676 &encode_test_method
,
2678 &pkey_kdf_test_method
,
2679 &keypair_test_method
,
2680 &keygen_test_method
,
2682 &oneshot_digestsign_test_method
,
2683 &oneshot_digestverify_test_method
,
2685 &pdecrypt_test_method
,
2686 &pderive_test_method
,
2688 &pverify_recover_test_method
,
2689 &pverify_test_method
,
2693 static const EVP_TEST_METHOD
*find_test(const char *name
)
2695 const EVP_TEST_METHOD
**tt
;
2697 for (tt
= evp_test_list
; *tt
; tt
++) {
2698 if (strcmp(name
, (*tt
)->name
) == 0)
2704 static void clear_test(EVP_TEST
*t
)
2706 test_clearstanza(&t
->s
);
2708 if (t
->data
!= NULL
) {
2709 if (t
->meth
!= NULL
)
2710 t
->meth
->cleanup(t
);
2711 OPENSSL_free(t
->data
);
2714 OPENSSL_free(t
->expected_err
);
2715 t
->expected_err
= NULL
;
2716 OPENSSL_free(t
->reason
);
2726 * Check for errors in the test structure; return 1 if okay, else 0.
2728 static int check_test_error(EVP_TEST
*t
)
2734 if (t
->err
== NULL
&& t
->expected_err
== NULL
)
2736 if (t
->err
!= NULL
&& t
->expected_err
== NULL
) {
2737 if (t
->aux_err
!= NULL
) {
2738 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
2739 t
->s
.test_file
, t
->s
.start
, t
->aux_err
, t
->err
);
2741 TEST_info("%s:%d: Source of above error; unexpected error %s",
2742 t
->s
.test_file
, t
->s
.start
, t
->err
);
2746 if (t
->err
== NULL
&& t
->expected_err
!= NULL
) {
2747 TEST_info("%s:%d: Succeeded but was expecting %s",
2748 t
->s
.test_file
, t
->s
.start
, t
->expected_err
);
2752 if (strcmp(t
->err
, t
->expected_err
) != 0) {
2753 TEST_info("%s:%d: Expected %s got %s",
2754 t
->s
.test_file
, t
->s
.start
, t
->expected_err
, t
->err
);
2758 if (t
->reason
== NULL
)
2761 if (t
->reason
== NULL
) {
2762 TEST_info("%s:%d: Test is missing function or reason code",
2763 t
->s
.test_file
, t
->s
.start
);
2767 err
= ERR_peek_error();
2769 TEST_info("%s:%d: Expected error \"%s\" not set",
2770 t
->s
.test_file
, t
->s
.start
, t
->reason
);
2774 func
= ERR_func_error_string(err
);
2775 reason
= ERR_reason_error_string(err
);
2776 if (func
== NULL
&& reason
== NULL
) {
2777 TEST_info("%s:%d: Expected error \"%s\", no strings available."
2779 t
->s
.test_file
, t
->s
.start
, t
->reason
);
2783 if (strcmp(reason
, t
->reason
) == 0)
2786 TEST_info("%s:%d: Expected error \"%s\", got \"%s\"",
2787 t
->s
.test_file
, t
->s
.start
, t
->reason
, reason
);
2793 * Run a parsed test. Log a message and return 0 on error.
2795 static int run_test(EVP_TEST
*t
)
2797 if (t
->meth
== NULL
)
2804 if (t
->err
== NULL
&& t
->meth
->run_test(t
) != 1) {
2805 TEST_info("%s:%d %s error",
2806 t
->s
.test_file
, t
->s
.start
, t
->meth
->name
);
2809 if (!check_test_error(t
)) {
2810 TEST_openssl_errors();
2819 static int find_key(EVP_PKEY
**ppk
, const char *name
, KEY_LIST
*lst
)
2821 for (; lst
!= NULL
; lst
= lst
->next
) {
2822 if (strcmp(lst
->name
, name
) == 0) {
2831 static void free_key_list(KEY_LIST
*lst
)
2833 while (lst
!= NULL
) {
2834 KEY_LIST
*next
= lst
->next
;
2836 EVP_PKEY_free(lst
->key
);
2837 OPENSSL_free(lst
->name
);
2844 * Is the key type an unsupported algorithm?
2846 static int key_unsupported(void)
2848 long err
= ERR_peek_error();
2850 if (ERR_GET_LIB(err
) == ERR_LIB_EVP
2851 && ERR_GET_REASON(err
) == EVP_R_UNSUPPORTED_ALGORITHM
) {
2855 #ifndef OPENSSL_NO_EC
2857 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
2858 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
2861 if (ERR_GET_LIB(err
) == ERR_LIB_EC
2862 && ERR_GET_REASON(err
) == EC_R_UNKNOWN_GROUP
) {
2866 #endif /* OPENSSL_NO_EC */
2871 * NULL out the value from |pp| but return it. This "steals" a pointer.
2873 static char *take_value(PAIR
*pp
)
2875 char *p
= pp
->value
;
2882 * Return 1 if one of the providers named in the string is available.
2883 * The provider names are separated with whitespace.
2884 * NOTE: destructive function, it inserts '\0' after each provider name.
2886 static int prov_available(char *providers
)
2892 for (; isspace(*providers
); providers
++)
2894 if (*providers
== '\0')
2895 break; /* End of the road */
2896 for (p
= providers
; *p
!= '\0' && !isspace(*p
); p
++)
2902 if (OSSL_PROVIDER_available(NULL
, providers
))
2903 return 1; /* Found one */
2909 * Read and parse one test. Return 0 if failure, 1 if okay.
2911 static int parse(EVP_TEST
*t
)
2913 KEY_LIST
*key
, **klist
;
2920 if (BIO_eof(t
->s
.fp
))
2923 if (!test_readstanza(&t
->s
))
2925 } while (t
->s
.numpairs
== 0);
2926 pp
= &t
->s
.pairs
[0];
2928 /* Are we adding a key? */
2931 if (strcmp(pp
->key
, "PrivateKey") == 0) {
2932 pkey
= PEM_read_bio_PrivateKey(t
->s
.key
, NULL
, 0, NULL
);
2933 if (pkey
== NULL
&& !key_unsupported()) {
2934 EVP_PKEY_free(pkey
);
2935 TEST_info("Can't read private key %s", pp
->value
);
2936 TEST_openssl_errors();
2939 klist
= &private_keys
;
2940 } else if (strcmp(pp
->key
, "PublicKey") == 0) {
2941 pkey
= PEM_read_bio_PUBKEY(t
->s
.key
, NULL
, 0, NULL
);
2942 if (pkey
== NULL
&& !key_unsupported()) {
2943 EVP_PKEY_free(pkey
);
2944 TEST_info("Can't read public key %s", pp
->value
);
2945 TEST_openssl_errors();
2948 klist
= &public_keys
;
2949 } else if (strcmp(pp
->key
, "PrivateKeyRaw") == 0
2950 || strcmp(pp
->key
, "PublicKeyRaw") == 0 ) {
2951 char *strnid
= NULL
, *keydata
= NULL
;
2952 unsigned char *keybin
;
2956 if (strcmp(pp
->key
, "PrivateKeyRaw") == 0)
2957 klist
= &private_keys
;
2959 klist
= &public_keys
;
2961 strnid
= strchr(pp
->value
, ':');
2962 if (strnid
!= NULL
) {
2964 keydata
= strchr(strnid
, ':');
2965 if (keydata
!= NULL
)
2968 if (keydata
== NULL
) {
2969 TEST_info("Failed to parse %s value", pp
->key
);
2973 nid
= OBJ_txt2nid(strnid
);
2974 if (nid
== NID_undef
) {
2975 TEST_info("Uncrecognised algorithm NID");
2978 if (!parse_bin(keydata
, &keybin
, &keylen
)) {
2979 TEST_info("Failed to create binary key");
2982 if (klist
== &private_keys
)
2983 pkey
= EVP_PKEY_new_raw_private_key(nid
, NULL
, keybin
, keylen
);
2985 pkey
= EVP_PKEY_new_raw_public_key(nid
, NULL
, keybin
, keylen
);
2986 if (pkey
== NULL
&& !key_unsupported()) {
2987 TEST_info("Can't read %s data", pp
->key
);
2988 OPENSSL_free(keybin
);
2989 TEST_openssl_errors();
2992 OPENSSL_free(keybin
);
2995 /* If we have a key add to list */
2996 if (klist
!= NULL
) {
2997 if (find_key(NULL
, pp
->value
, *klist
)) {
2998 TEST_info("Duplicate key %s", pp
->value
);
3001 if (!TEST_ptr(key
= OPENSSL_malloc(sizeof(*key
))))
3003 key
->name
= take_value(pp
);
3005 /* Hack to detect SM2 keys */
3006 if(pkey
!= NULL
&& strstr(key
->name
, "SM2") != NULL
) {
3007 #ifdef OPENSSL_NO_SM2
3008 EVP_PKEY_free(pkey
);
3011 EVP_PKEY_set_alias_type(pkey
, EVP_PKEY_SM2
);
3019 /* Go back and start a new stanza. */
3020 if (t
->s
.numpairs
!= 1)
3021 TEST_info("Line %d: missing blank line\n", t
->s
.curr
);
3025 /* Find the test, based on first keyword. */
3026 if (!TEST_ptr(t
->meth
= find_test(pp
->key
)))
3028 if (!t
->meth
->init(t
, pp
->value
)) {
3029 TEST_error("unknown %s: %s\n", pp
->key
, pp
->value
);
3033 /* TEST_info("skipping %s %s", pp->key, pp->value); */
3037 for (pp
++, i
= 1; i
< t
->s
.numpairs
; pp
++, i
++) {
3038 if (strcmp(pp
->key
, "Availablein") == 0) {
3039 if (!prov_available(pp
->value
)) {
3040 TEST_info("skipping, providers not available: %s:%d",
3041 t
->s
.test_file
, t
->s
.start
);
3045 } else if (strcmp(pp
->key
, "Result") == 0) {
3046 if (t
->expected_err
!= NULL
) {
3047 TEST_info("Line %d: multiple result lines", t
->s
.curr
);
3050 t
->expected_err
= take_value(pp
);
3051 } else if (strcmp(pp
->key
, "Function") == 0) {
3052 /* Ignore old line. */
3053 } else if (strcmp(pp
->key
, "Reason") == 0) {
3054 if (t
->reason
!= NULL
) {
3055 TEST_info("Line %d: multiple reason lines", t
->s
.curr
);
3058 t
->reason
= take_value(pp
);
3060 /* Must be test specific line: try to parse it */
3061 int rv
= t
->meth
->parse(t
, pp
->key
, pp
->value
);
3064 TEST_info("Line %d: unknown keyword %s", t
->s
.curr
, pp
->key
);
3068 TEST_info("Line %d: error processing keyword %s = %s\n",
3069 t
->s
.curr
, pp
->key
, pp
->value
);
3078 static int run_file_tests(int i
)
3081 const char *testfile
= test_get_argument(i
);
3084 if (!TEST_ptr(t
= OPENSSL_zalloc(sizeof(*t
))))
3086 if (!test_start_file(&t
->s
, testfile
)) {
3091 while (!BIO_eof(t
->s
.fp
)) {
3097 if (c
== 0 || !run_test(t
)) {
3102 test_end_file(&t
->s
);
3105 free_key_list(public_keys
);
3106 free_key_list(private_keys
);
3113 OPT_TEST_DECLARE_USAGE("file...\n")
3115 int setup_tests(void)
3117 size_t n
= test_get_argument_count();
3122 ADD_ALL_TESTS(run_file_tests
, n
);