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/x509v3.h>
18 #include <openssl/pkcs12.h>
19 #include <openssl/kdf.h>
20 #include "internal/numbers.h"
26 typedef struct evp_test_method_st EVP_TEST_METHOD
;
29 * Structure holding test information
31 typedef struct evp_test_st
{
32 STANZA s
; /* Common test stanza */
34 int skip
; /* Current test should be skipped */
35 const EVP_TEST_METHOD
*meth
; /* method for this test */
36 const char *err
, *aux_err
; /* Error string for test */
37 char *expected_err
; /* Expected error value of test */
38 char *func
; /* Expected error function string */
39 char *reason
; /* Expected error reason string */
40 void *data
; /* test specific data */
44 * Test method structure
46 struct evp_test_method_st
{
47 /* Name of test as it appears in file */
49 /* Initialise test for "alg" */
50 int (*init
) (EVP_TEST
* t
, const char *alg
);
52 void (*cleanup
) (EVP_TEST
* t
);
53 /* Test specific name value pair processing */
54 int (*parse
) (EVP_TEST
* t
, const char *name
, const char *value
);
55 /* Run the test itself */
56 int (*run_test
) (EVP_TEST
* t
);
61 * Linked list of named keys.
63 typedef struct key_list_st
{
66 struct key_list_st
*next
;
70 * List of public and private keys
72 static KEY_LIST
*private_keys
;
73 static KEY_LIST
*public_keys
;
74 static int find_key(EVP_PKEY
**ppk
, const char *name
, KEY_LIST
*lst
);
76 static int parse_bin(const char *value
, unsigned char **buf
, size_t *buflen
);
79 * Compare two memory regions for equality, returning zero if they differ.
80 * However, if there is expected to be an error and the actual error
81 * matches then the memory is expected to be different so handle this
82 * case without producing unnecessary test framework output.
84 static int memory_err_compare(EVP_TEST
*t
, const char *err
,
85 const void *expected
, size_t expected_len
,
86 const void *got
, size_t got_len
)
90 if (t
->expected_err
!= NULL
&& strcmp(t
->expected_err
, err
) == 0)
91 r
= !TEST_mem_ne(expected
, expected_len
, got
, got_len
);
93 r
= TEST_mem_eq(expected
, expected_len
, got
, got_len
);
100 * Structure used to hold a list of blocks of memory to test
101 * calls to "update" like functions.
103 struct evp_test_buffer_st
{
110 static void evp_test_buffer_free(EVP_TEST_BUFFER
*db
)
113 OPENSSL_free(db
->buf
);
119 * append buffer to a list
121 static int evp_test_buffer_append(const char *value
,
122 STACK_OF(EVP_TEST_BUFFER
) **sk
)
124 EVP_TEST_BUFFER
*db
= NULL
;
126 if (!TEST_ptr(db
= OPENSSL_malloc(sizeof(*db
))))
129 if (!parse_bin(value
, &db
->buf
, &db
->buflen
))
134 if (*sk
== NULL
&& !TEST_ptr(*sk
= sk_EVP_TEST_BUFFER_new_null()))
136 if (!sk_EVP_TEST_BUFFER_push(*sk
, db
))
142 evp_test_buffer_free(db
);
147 * replace last buffer in list with copies of itself
149 static int evp_test_buffer_ncopy(const char *value
,
150 STACK_OF(EVP_TEST_BUFFER
) *sk
)
153 unsigned char *tbuf
, *p
;
155 int ncopy
= atoi(value
);
160 if (sk
== NULL
|| sk_EVP_TEST_BUFFER_num(sk
) == 0)
162 db
= sk_EVP_TEST_BUFFER_value(sk
, sk_EVP_TEST_BUFFER_num(sk
) - 1);
164 tbuflen
= db
->buflen
* ncopy
;
165 if (!TEST_ptr(tbuf
= OPENSSL_malloc(tbuflen
)))
167 for (i
= 0, p
= tbuf
; i
< ncopy
; i
++, p
+= db
->buflen
)
168 memcpy(p
, db
->buf
, db
->buflen
);
170 OPENSSL_free(db
->buf
);
172 db
->buflen
= tbuflen
;
177 * set repeat count for last buffer in list
179 static int evp_test_buffer_set_count(const char *value
,
180 STACK_OF(EVP_TEST_BUFFER
) *sk
)
183 int count
= atoi(value
);
188 if (sk
== NULL
|| sk_EVP_TEST_BUFFER_num(sk
) == 0)
191 db
= sk_EVP_TEST_BUFFER_value(sk
, sk_EVP_TEST_BUFFER_num(sk
) - 1);
192 if (db
->count_set
!= 0)
195 db
->count
= (size_t)count
;
201 * call "fn" with each element of the list in turn
203 static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER
) *sk
,
205 const unsigned char *buf
,
211 for (i
= 0; i
< sk_EVP_TEST_BUFFER_num(sk
); i
++) {
212 EVP_TEST_BUFFER
*tb
= sk_EVP_TEST_BUFFER_value(sk
, i
);
215 for (j
= 0; j
< tb
->count
; j
++) {
216 if (fn(ctx
, tb
->buf
, tb
->buflen
) <= 0)
224 * Unescape some sequences in string literals (only \n for now).
225 * Return an allocated buffer, set |out_len|. If |input_len|
226 * is zero, get an empty buffer but set length to zero.
228 static unsigned char* unescape(const char *input
, size_t input_len
,
231 unsigned char *ret
, *p
;
234 if (input_len
== 0) {
236 return OPENSSL_zalloc(1);
239 /* Escaping is non-expanding; over-allocate original size for simplicity. */
240 if (!TEST_ptr(ret
= p
= OPENSSL_malloc(input_len
)))
243 for (i
= 0; i
< input_len
; i
++) {
244 if (*input
== '\\') {
245 if (i
== input_len
- 1 || *++input
!= 'n') {
246 TEST_error("Bad escape sequence in file");
266 * For a hex string "value" convert to a binary allocated buffer.
267 * Return 1 on success or 0 on failure.
269 static int parse_bin(const char *value
, unsigned char **buf
, size_t *buflen
)
273 /* Check for NULL literal */
274 if (strcmp(value
, "NULL") == 0) {
280 /* Check for empty value */
281 if (*value
== '\0') {
283 * Don't return NULL for zero length buffer. This is needed for
284 * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key
285 * buffer even if the key length is 0, in order to detect key reset.
287 *buf
= OPENSSL_malloc(1);
295 /* Check for string literal */
296 if (value
[0] == '"') {
297 size_t vlen
= strlen(++value
);
299 if (vlen
== 0 || value
[vlen
- 1] != '"')
302 *buf
= unescape(value
, vlen
, buflen
);
303 return *buf
== NULL
? 0 : 1;
306 /* Otherwise assume as hex literal and convert it to binary buffer */
307 if (!TEST_ptr(*buf
= OPENSSL_hexstr2buf(value
, &len
))) {
308 TEST_info("Can't convert %s", value
);
309 TEST_openssl_errors();
312 /* Size of input buffer means we'll never overflow */
319 *** MESSAGE DIGEST TESTS
322 typedef struct digest_data_st
{
323 /* Digest this test is for */
324 const EVP_MD
*digest
;
325 /* Input to digest */
326 STACK_OF(EVP_TEST_BUFFER
) *input
;
327 /* Expected output */
328 unsigned char *output
;
332 static int digest_test_init(EVP_TEST
*t
, const char *alg
)
335 const EVP_MD
*digest
;
337 if ((digest
= EVP_get_digestbyname(alg
)) == NULL
) {
338 /* If alg has an OID assume disabled algorithm */
339 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
345 if (!TEST_ptr(mdat
= OPENSSL_zalloc(sizeof(*mdat
))))
348 mdat
->digest
= digest
;
352 static void digest_test_cleanup(EVP_TEST
*t
)
354 DIGEST_DATA
*mdat
= t
->data
;
356 sk_EVP_TEST_BUFFER_pop_free(mdat
->input
, evp_test_buffer_free
);
357 OPENSSL_free(mdat
->output
);
360 static int digest_test_parse(EVP_TEST
*t
,
361 const char *keyword
, const char *value
)
363 DIGEST_DATA
*mdata
= t
->data
;
365 if (strcmp(keyword
, "Input") == 0)
366 return evp_test_buffer_append(value
, &mdata
->input
);
367 if (strcmp(keyword
, "Output") == 0)
368 return parse_bin(value
, &mdata
->output
, &mdata
->output_len
);
369 if (strcmp(keyword
, "Count") == 0)
370 return evp_test_buffer_set_count(value
, mdata
->input
);
371 if (strcmp(keyword
, "Ncopy") == 0)
372 return evp_test_buffer_ncopy(value
, mdata
->input
);
376 static int digest_update_fn(void *ctx
, const unsigned char *buf
, size_t buflen
)
378 return EVP_DigestUpdate(ctx
, buf
, buflen
);
381 static int digest_test_run(EVP_TEST
*t
)
383 DIGEST_DATA
*expected
= t
->data
;
385 unsigned char *got
= NULL
;
386 unsigned int got_len
;
388 t
->err
= "TEST_FAILURE";
389 if (!TEST_ptr(mctx
= EVP_MD_CTX_new()))
392 got
= OPENSSL_malloc(expected
->output_len
> EVP_MAX_MD_SIZE
?
393 expected
->output_len
: EVP_MAX_MD_SIZE
);
397 if (!EVP_DigestInit_ex(mctx
, expected
->digest
, NULL
)) {
398 t
->err
= "DIGESTINIT_ERROR";
401 if (!evp_test_buffer_do(expected
->input
, digest_update_fn
, mctx
)) {
402 t
->err
= "DIGESTUPDATE_ERROR";
406 if (EVP_MD_flags(expected
->digest
) & EVP_MD_FLAG_XOF
) {
407 got_len
= expected
->output_len
;
408 if (!EVP_DigestFinalXOF(mctx
, got
, got_len
)) {
409 t
->err
= "DIGESTFINALXOF_ERROR";
413 if (!EVP_DigestFinal(mctx
, got
, &got_len
)) {
414 t
->err
= "DIGESTFINAL_ERROR";
418 if (!TEST_int_eq(expected
->output_len
, got_len
)) {
419 t
->err
= "DIGEST_LENGTH_MISMATCH";
422 if (!memory_err_compare(t
, "DIGEST_MISMATCH",
423 expected
->output
, expected
->output_len
,
431 EVP_MD_CTX_free(mctx
);
435 static const EVP_TEST_METHOD digest_test_method
= {
448 typedef struct cipher_data_st
{
449 const EVP_CIPHER
*cipher
;
451 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
457 unsigned char *plaintext
;
458 size_t plaintext_len
;
459 unsigned char *ciphertext
;
460 size_t ciphertext_len
;
461 /* GCM, CCM, OCB and SIV only */
462 unsigned char *aad
[AAD_NUM
];
463 size_t aad_len
[AAD_NUM
];
468 static int cipher_test_init(EVP_TEST
*t
, const char *alg
)
470 const EVP_CIPHER
*cipher
;
474 if ((cipher
= EVP_get_cipherbyname(alg
)) == NULL
) {
475 /* If alg has an OID assume disabled algorithm */
476 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
482 cdat
= OPENSSL_zalloc(sizeof(*cdat
));
483 cdat
->cipher
= cipher
;
485 m
= EVP_CIPHER_mode(cipher
);
486 if (m
== EVP_CIPH_GCM_MODE
487 || m
== EVP_CIPH_OCB_MODE
488 || m
== EVP_CIPH_SIV_MODE
489 || m
== EVP_CIPH_CCM_MODE
)
491 else if (EVP_CIPHER_flags(cipher
) & EVP_CIPH_FLAG_AEAD_CIPHER
)
500 static void cipher_test_cleanup(EVP_TEST
*t
)
503 CIPHER_DATA
*cdat
= t
->data
;
505 OPENSSL_free(cdat
->key
);
506 OPENSSL_free(cdat
->iv
);
507 OPENSSL_free(cdat
->ciphertext
);
508 OPENSSL_free(cdat
->plaintext
);
509 for (i
= 0; i
< AAD_NUM
; i
++)
510 OPENSSL_free(cdat
->aad
[i
]);
511 OPENSSL_free(cdat
->tag
);
514 static int cipher_test_parse(EVP_TEST
*t
, const char *keyword
,
517 CIPHER_DATA
*cdat
= t
->data
;
520 if (strcmp(keyword
, "Key") == 0)
521 return parse_bin(value
, &cdat
->key
, &cdat
->key_len
);
522 if (strcmp(keyword
, "IV") == 0)
523 return parse_bin(value
, &cdat
->iv
, &cdat
->iv_len
);
524 if (strcmp(keyword
, "Plaintext") == 0)
525 return parse_bin(value
, &cdat
->plaintext
, &cdat
->plaintext_len
);
526 if (strcmp(keyword
, "Ciphertext") == 0)
527 return parse_bin(value
, &cdat
->ciphertext
, &cdat
->ciphertext_len
);
529 if (strcmp(keyword
, "AAD") == 0) {
530 for (i
= 0; i
< AAD_NUM
; i
++) {
531 if (cdat
->aad
[i
] == NULL
)
532 return parse_bin(value
, &cdat
->aad
[i
], &cdat
->aad_len
[i
]);
536 if (strcmp(keyword
, "Tag") == 0)
537 return parse_bin(value
, &cdat
->tag
, &cdat
->tag_len
);
540 if (strcmp(keyword
, "Operation") == 0) {
541 if (strcmp(value
, "ENCRYPT") == 0)
543 else if (strcmp(value
, "DECRYPT") == 0)
552 static int cipher_test_enc(EVP_TEST
*t
, int enc
,
553 size_t out_misalign
, size_t inp_misalign
, int frag
)
555 CIPHER_DATA
*expected
= t
->data
;
556 unsigned char *in
, *expected_out
, *tmp
= NULL
;
557 size_t in_len
, out_len
, donelen
= 0;
558 int ok
= 0, tmplen
, chunklen
, tmpflen
, i
;
559 EVP_CIPHER_CTX
*ctx
= NULL
;
561 t
->err
= "TEST_FAILURE";
562 if (!TEST_ptr(ctx
= EVP_CIPHER_CTX_new()))
564 EVP_CIPHER_CTX_set_flags(ctx
, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
);
566 in
= expected
->plaintext
;
567 in_len
= expected
->plaintext_len
;
568 expected_out
= expected
->ciphertext
;
569 out_len
= expected
->ciphertext_len
;
571 in
= expected
->ciphertext
;
572 in_len
= expected
->ciphertext_len
;
573 expected_out
= expected
->plaintext
;
574 out_len
= expected
->plaintext_len
;
576 if (inp_misalign
== (size_t)-1) {
578 * Exercise in-place encryption
580 tmp
= OPENSSL_malloc(out_misalign
+ in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
);
583 in
= memcpy(tmp
+ out_misalign
, in
, in_len
);
585 inp_misalign
+= 16 - ((out_misalign
+ in_len
) & 15);
587 * 'tmp' will store both output and copy of input. We make the copy
588 * of input to specifically aligned part of 'tmp'. So we just
589 * figured out how much padding would ensure the required alignment,
590 * now we allocate extended buffer and finally copy the input just
591 * past inp_misalign in expression below. Output will be written
592 * past out_misalign...
594 tmp
= OPENSSL_malloc(out_misalign
+ in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
+
595 inp_misalign
+ in_len
);
598 in
= memcpy(tmp
+ out_misalign
+ in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
+
599 inp_misalign
, in
, in_len
);
601 if (!EVP_CipherInit_ex(ctx
, expected
->cipher
, NULL
, NULL
, NULL
, enc
)) {
602 t
->err
= "CIPHERINIT_ERROR";
606 if (expected
->aead
) {
607 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_IVLEN
,
608 expected
->iv_len
, 0)) {
609 t
->err
= "INVALID_IV_LENGTH";
612 } else if (expected
->iv_len
!= (size_t)EVP_CIPHER_CTX_iv_length(ctx
)) {
613 t
->err
= "INVALID_IV_LENGTH";
617 if (expected
->aead
) {
620 * If encrypting or OCB just set tag length initially, otherwise
621 * set tag length and value.
623 if (enc
|| expected
->aead
== EVP_CIPH_OCB_MODE
) {
624 t
->err
= "TAG_LENGTH_SET_ERROR";
627 t
->err
= "TAG_SET_ERROR";
630 if (tag
|| expected
->aead
!= EVP_CIPH_GCM_MODE
) {
631 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_TAG
,
632 expected
->tag_len
, tag
))
637 if (!EVP_CIPHER_CTX_set_key_length(ctx
, expected
->key_len
)) {
638 t
->err
= "INVALID_KEY_LENGTH";
641 if (!EVP_CipherInit_ex(ctx
, NULL
, NULL
, expected
->key
, expected
->iv
, -1)) {
642 t
->err
= "KEY_SET_ERROR";
646 if (!enc
&& expected
->aead
== EVP_CIPH_OCB_MODE
) {
647 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_TAG
,
648 expected
->tag_len
, expected
->tag
)) {
649 t
->err
= "TAG_SET_ERROR";
654 if (expected
->aead
== EVP_CIPH_CCM_MODE
) {
655 if (!EVP_CipherUpdate(ctx
, NULL
, &tmplen
, NULL
, out_len
)) {
656 t
->err
= "CCM_PLAINTEXT_LENGTH_SET_ERROR";
660 if (expected
->aad
[0] != NULL
) {
661 t
->err
= "AAD_SET_ERROR";
663 for (i
= 0; expected
->aad
[i
] != NULL
; i
++) {
664 if (!EVP_CipherUpdate(ctx
, NULL
, &chunklen
, expected
->aad
[i
],
665 expected
->aad_len
[i
]))
670 * Supply the AAD in chunks less than the block size where possible
672 for (i
= 0; expected
->aad
[i
] != NULL
; i
++) {
673 if (expected
->aad_len
[i
] > 0) {
674 if (!EVP_CipherUpdate(ctx
, NULL
, &chunklen
, expected
->aad
[i
], 1))
678 if (expected
->aad_len
[i
] > 2) {
679 if (!EVP_CipherUpdate(ctx
, NULL
, &chunklen
,
680 expected
->aad
[i
] + donelen
,
681 expected
->aad_len
[i
] - 2))
683 donelen
+= expected
->aad_len
[i
] - 2;
685 if (expected
->aad_len
[i
] > 1
686 && !EVP_CipherUpdate(ctx
, NULL
, &chunklen
,
687 expected
->aad
[i
] + donelen
, 1))
692 EVP_CIPHER_CTX_set_padding(ctx
, 0);
693 t
->err
= "CIPHERUPDATE_ERROR";
696 /* We supply the data all in one go */
697 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
, &tmplen
, in
, in_len
))
700 /* Supply the data in chunks less than the block size where possible */
702 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
, &chunklen
, in
, 1))
709 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
+ tmplen
, &chunklen
,
717 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
+ tmplen
, &chunklen
,
723 if (!EVP_CipherFinal_ex(ctx
, tmp
+ out_misalign
+ tmplen
, &tmpflen
)) {
724 t
->err
= "CIPHERFINAL_ERROR";
727 if (!memory_err_compare(t
, "VALUE_MISMATCH", expected_out
, out_len
,
728 tmp
+ out_misalign
, tmplen
+ tmpflen
))
730 if (enc
&& expected
->aead
) {
731 unsigned char rtag
[16];
733 if (!TEST_size_t_le(expected
->tag_len
, sizeof(rtag
))) {
734 t
->err
= "TAG_LENGTH_INTERNAL_ERROR";
737 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_GET_TAG
,
738 expected
->tag_len
, rtag
)) {
739 t
->err
= "TAG_RETRIEVE_ERROR";
742 if (!memory_err_compare(t
, "TAG_VALUE_MISMATCH",
743 expected
->tag
, expected
->tag_len
,
744 rtag
, expected
->tag_len
))
751 EVP_CIPHER_CTX_free(ctx
);
755 static int cipher_test_run(EVP_TEST
*t
)
757 CIPHER_DATA
*cdat
= t
->data
;
759 size_t out_misalign
, inp_misalign
;
765 if (!cdat
->iv
&& EVP_CIPHER_iv_length(cdat
->cipher
)) {
766 /* IV is optional and usually omitted in wrap mode */
767 if (EVP_CIPHER_mode(cdat
->cipher
) != EVP_CIPH_WRAP_MODE
) {
772 if (cdat
->aead
&& !cdat
->tag
) {
776 for (out_misalign
= 0; out_misalign
<= 1;) {
777 static char aux_err
[64];
778 t
->aux_err
= aux_err
;
779 for (inp_misalign
= (size_t)-1; inp_misalign
!= 2; inp_misalign
++) {
780 if (inp_misalign
== (size_t)-1) {
781 /* kludge: inp_misalign == -1 means "exercise in-place" */
782 BIO_snprintf(aux_err
, sizeof(aux_err
),
783 "%s in-place, %sfragmented",
784 out_misalign
? "misaligned" : "aligned",
787 BIO_snprintf(aux_err
, sizeof(aux_err
),
788 "%s output and %s input, %sfragmented",
789 out_misalign
? "misaligned" : "aligned",
790 inp_misalign
? "misaligned" : "aligned",
794 rv
= cipher_test_enc(t
, 1, out_misalign
, inp_misalign
, frag
);
795 /* Not fatal errors: return */
802 if (cdat
->enc
!= 1) {
803 rv
= cipher_test_enc(t
, 0, out_misalign
, inp_misalign
, frag
);
804 /* Not fatal errors: return */
813 if (out_misalign
== 1 && frag
== 0) {
815 * XTS, SIV, CCM and Wrap modes have special requirements about input
816 * lengths so we don't fragment for those
818 if (cdat
->aead
== EVP_CIPH_CCM_MODE
819 || EVP_CIPHER_mode(cdat
->cipher
) == EVP_CIPH_SIV_MODE
820 || EVP_CIPHER_mode(cdat
->cipher
) == EVP_CIPH_XTS_MODE
821 || EVP_CIPHER_mode(cdat
->cipher
) == EVP_CIPH_WRAP_MODE
)
834 static const EVP_TEST_METHOD cipher_test_method
= {
847 typedef struct mac_data_st
{
848 /* MAC type in one form or another */
849 const EVP_MAC
*mac
; /* for mac_test_run_mac */
850 int type
; /* for mac_test_run_pkey */
851 /* Algorithm string for this MAC */
860 unsigned char *input
;
862 /* Expected output */
863 unsigned char *output
;
865 unsigned char *custom
;
867 /* Collection of controls */
868 STACK_OF(OPENSSL_STRING
) *controls
;
871 static int mac_test_init(EVP_TEST
*t
, const char *alg
)
873 const EVP_MAC
*mac
= NULL
;
874 int type
= NID_undef
;
877 if ((mac
= EVP_get_macbyname(alg
)) == NULL
) {
879 * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
880 * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
881 * the EVP_PKEY method.
883 size_t sz
= strlen(alg
);
884 static const char epilogue
[] = " by EVP_PKEY";
886 if (sz
>= sizeof(epilogue
)
887 && strcmp(alg
+ sz
- (sizeof(epilogue
) - 1), epilogue
) == 0)
888 sz
-= sizeof(epilogue
) - 1;
890 if (strncmp(alg
, "HMAC", sz
) == 0) {
891 type
= EVP_PKEY_HMAC
;
892 } else if (strncmp(alg
, "CMAC", sz
) == 0) {
893 #ifndef OPENSSL_NO_CMAC
894 type
= EVP_PKEY_CMAC
;
899 } else if (strncmp(alg
, "Poly1305", sz
) == 0) {
900 #ifndef OPENSSL_NO_POLY1305
901 type
= EVP_PKEY_POLY1305
;
906 } else if (strncmp(alg
, "SipHash", sz
) == 0) {
907 #ifndef OPENSSL_NO_SIPHASH
908 type
= EVP_PKEY_SIPHASH
;
915 * Not a known EVP_PKEY method either. If it's a known OID, then
916 * assume it's been disabled.
918 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
927 mdat
= OPENSSL_zalloc(sizeof(*mdat
));
930 mdat
->controls
= sk_OPENSSL_STRING_new_null();
935 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
936 static void openssl_free(char *m
)
941 static void mac_test_cleanup(EVP_TEST
*t
)
943 MAC_DATA
*mdat
= t
->data
;
945 sk_OPENSSL_STRING_pop_free(mdat
->controls
, openssl_free
);
946 OPENSSL_free(mdat
->alg
);
947 OPENSSL_free(mdat
->key
);
948 OPENSSL_free(mdat
->iv
);
949 OPENSSL_free(mdat
->custom
);
950 OPENSSL_free(mdat
->input
);
951 OPENSSL_free(mdat
->output
);
954 static int mac_test_parse(EVP_TEST
*t
,
955 const char *keyword
, const char *value
)
957 MAC_DATA
*mdata
= t
->data
;
959 if (strcmp(keyword
, "Key") == 0)
960 return parse_bin(value
, &mdata
->key
, &mdata
->key_len
);
961 if (strcmp(keyword
, "IV") == 0)
962 return parse_bin(value
, &mdata
->iv
, &mdata
->iv_len
);
963 if (strcmp(keyword
, "Custom") == 0)
964 return parse_bin(value
, &mdata
->custom
, &mdata
->custom_len
);
965 if (strcmp(keyword
, "Algorithm") == 0) {
966 mdata
->alg
= OPENSSL_strdup(value
);
971 if (strcmp(keyword
, "Input") == 0)
972 return parse_bin(value
, &mdata
->input
, &mdata
->input_len
);
973 if (strcmp(keyword
, "Output") == 0)
974 return parse_bin(value
, &mdata
->output
, &mdata
->output_len
);
975 if (strcmp(keyword
, "Ctrl") == 0)
976 return sk_OPENSSL_STRING_push(mdata
->controls
,
977 OPENSSL_strdup(value
)) != 0;
981 static int mac_test_ctrl_pkey(EVP_TEST
*t
, EVP_PKEY_CTX
*pctx
,
987 if (!TEST_ptr(tmpval
= OPENSSL_strdup(value
)))
989 p
= strchr(tmpval
, ':');
992 rv
= EVP_PKEY_CTX_ctrl_str(pctx
, tmpval
, p
);
994 t
->err
= "PKEY_CTRL_INVALID";
996 t
->err
= "PKEY_CTRL_ERROR";
999 OPENSSL_free(tmpval
);
1003 static int mac_test_run_pkey(EVP_TEST
*t
)
1005 MAC_DATA
*expected
= t
->data
;
1006 EVP_MD_CTX
*mctx
= NULL
;
1007 EVP_PKEY_CTX
*pctx
= NULL
, *genctx
= NULL
;
1008 EVP_PKEY
*key
= NULL
;
1009 const EVP_MD
*md
= NULL
;
1010 unsigned char *got
= NULL
;
1014 if (expected
->alg
== NULL
)
1015 TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected
->type
));
1017 TEST_info("Trying the EVP_PKEY %s test with %s",
1018 OBJ_nid2sn(expected
->type
), expected
->alg
);
1020 #ifdef OPENSSL_NO_DES
1021 if (expected
->alg
!= NULL
&& strstr(expected
->alg
, "DES") != NULL
) {
1028 if (expected
->type
== EVP_PKEY_CMAC
)
1029 key
= EVP_PKEY_new_CMAC_key(NULL
, expected
->key
, expected
->key_len
,
1030 EVP_get_cipherbyname(expected
->alg
));
1032 key
= EVP_PKEY_new_raw_private_key(expected
->type
, NULL
, expected
->key
,
1035 t
->err
= "MAC_KEY_CREATE_ERROR";
1039 if (expected
->type
== EVP_PKEY_HMAC
) {
1040 if (!TEST_ptr(md
= EVP_get_digestbyname(expected
->alg
))) {
1041 t
->err
= "MAC_ALGORITHM_SET_ERROR";
1045 if (!TEST_ptr(mctx
= EVP_MD_CTX_new())) {
1046 t
->err
= "INTERNAL_ERROR";
1049 if (!EVP_DigestSignInit(mctx
, &pctx
, md
, NULL
, key
)) {
1050 t
->err
= "DIGESTSIGNINIT_ERROR";
1053 for (i
= 0; i
< sk_OPENSSL_STRING_num(expected
->controls
); i
++)
1054 if (!mac_test_ctrl_pkey(t
, pctx
,
1055 sk_OPENSSL_STRING_value(expected
->controls
,
1057 t
->err
= "EVPPKEYCTXCTRL_ERROR";
1060 if (!EVP_DigestSignUpdate(mctx
, expected
->input
, expected
->input_len
)) {
1061 t
->err
= "DIGESTSIGNUPDATE_ERROR";
1064 if (!EVP_DigestSignFinal(mctx
, NULL
, &got_len
)) {
1065 t
->err
= "DIGESTSIGNFINAL_LENGTH_ERROR";
1068 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1069 t
->err
= "TEST_FAILURE";
1072 if (!EVP_DigestSignFinal(mctx
, got
, &got_len
)
1073 || !memory_err_compare(t
, "TEST_MAC_ERR",
1074 expected
->output
, expected
->output_len
,
1076 t
->err
= "TEST_MAC_ERR";
1081 EVP_MD_CTX_free(mctx
);
1083 EVP_PKEY_CTX_free(genctx
);
1088 static int mac_test_run_mac(EVP_TEST
*t
)
1090 MAC_DATA
*expected
= t
->data
;
1091 EVP_MAC_CTX
*ctx
= NULL
;
1092 const void *algo
= NULL
;
1094 unsigned char *got
= NULL
;
1098 if (expected
->alg
== NULL
)
1099 TEST_info("Trying the EVP_MAC %s test", EVP_MAC_name(expected
->mac
));
1101 TEST_info("Trying the EVP_MAC %s test with %s",
1102 EVP_MAC_name(expected
->mac
), expected
->alg
);
1104 #ifdef OPENSSL_NO_DES
1105 if (expected
->alg
!= NULL
&& strstr(expected
->alg
, "DES") != NULL
) {
1112 if ((ctx
= EVP_MAC_CTX_new(expected
->mac
)) == NULL
) {
1113 t
->err
= "MAC_CREATE_ERROR";
1117 if (expected
->alg
!= NULL
1118 && ((algo_ctrl
= EVP_MAC_CTRL_SET_CIPHER
,
1119 algo
= EVP_get_cipherbyname(expected
->alg
)) == NULL
1120 && (algo_ctrl
= EVP_MAC_CTRL_SET_MD
,
1121 algo
= EVP_get_digestbyname(expected
->alg
)) == NULL
)) {
1122 t
->err
= "MAC_BAD_ALGORITHM";
1127 if (algo_ctrl
!= 0) {
1128 rv
= EVP_MAC_ctrl(ctx
, algo_ctrl
, algo
);
1130 t
->err
= "MAC_CTRL_INVALID";
1132 } else if (rv
<= 0) {
1133 t
->err
= "MAC_CTRL_ERROR";
1138 rv
= EVP_MAC_ctrl(ctx
, EVP_MAC_CTRL_SET_KEY
,
1139 expected
->key
, expected
->key_len
);
1141 t
->err
= "MAC_CTRL_INVALID";
1143 } else if (rv
<= 0) {
1144 t
->err
= "MAC_CTRL_ERROR";
1147 if (expected
->custom
!= NULL
) {
1148 rv
= EVP_MAC_ctrl(ctx
, EVP_MAC_CTRL_SET_CUSTOM
,
1149 expected
->custom
, expected
->custom_len
);
1151 t
->err
= "MAC_CTRL_INVALID";
1153 } else if (rv
<= 0) {
1154 t
->err
= "MAC_CTRL_ERROR";
1159 if (expected
->iv
!= NULL
) {
1160 rv
= EVP_MAC_ctrl(ctx
, EVP_MAC_CTRL_SET_IV
,
1161 expected
->iv
, expected
->iv_len
);
1163 t
->err
= "MAC_CTRL_INVALID";
1165 } else if (rv
<= 0) {
1166 t
->err
= "MAC_CTRL_ERROR";
1171 if (!EVP_MAC_init(ctx
)) {
1172 t
->err
= "MAC_INIT_ERROR";
1175 for (i
= 0; i
< sk_OPENSSL_STRING_num(expected
->controls
); i
++) {
1177 char *value
= sk_OPENSSL_STRING_value(expected
->controls
, i
);
1179 if (!TEST_ptr(tmpval
= OPENSSL_strdup(value
))) {
1180 t
->err
= "MAC_CTRL_ERROR";
1183 p
= strchr(tmpval
, ':');
1186 rv
= EVP_MAC_ctrl_str(ctx
, tmpval
, p
);
1187 OPENSSL_free(tmpval
);
1189 t
->err
= "MAC_CTRL_INVALID";
1191 } else if (rv
<= 0) {
1192 t
->err
= "MAC_CTRL_ERROR";
1196 if (!EVP_MAC_update(ctx
, expected
->input
, expected
->input_len
)) {
1197 t
->err
= "MAC_UPDATE_ERROR";
1200 if (!EVP_MAC_final(ctx
, NULL
, &got_len
)) {
1201 t
->err
= "MAC_FINAL_LENGTH_ERROR";
1204 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1205 t
->err
= "TEST_FAILURE";
1208 if (!EVP_MAC_final(ctx
, got
, &got_len
)
1209 || !memory_err_compare(t
, "TEST_MAC_ERR",
1210 expected
->output
, expected
->output_len
,
1212 t
->err
= "TEST_MAC_ERR";
1217 EVP_MAC_CTX_free(ctx
);
1222 static int mac_test_run(EVP_TEST
*t
)
1224 MAC_DATA
*expected
= t
->data
;
1226 if (expected
->mac
!= NULL
)
1227 return mac_test_run_mac(t
);
1228 return mac_test_run_pkey(t
);
1231 static const EVP_TEST_METHOD mac_test_method
= {
1241 *** PUBLIC KEY TESTS
1242 *** These are all very similar and share much common code.
1245 typedef struct pkey_data_st
{
1246 /* Context for this operation */
1248 /* Key operation to perform */
1249 int (*keyop
) (EVP_PKEY_CTX
*ctx
,
1250 unsigned char *sig
, size_t *siglen
,
1251 const unsigned char *tbs
, size_t tbslen
);
1253 unsigned char *input
;
1255 /* Expected output */
1256 unsigned char *output
;
1261 * Perform public key operation setup: lookup key, allocated ctx and call
1262 * the appropriate initialisation function
1264 static int pkey_test_init(EVP_TEST
*t
, const char *name
,
1266 int (*keyopinit
) (EVP_PKEY_CTX
*ctx
),
1267 int (*keyop
)(EVP_PKEY_CTX
*ctx
,
1268 unsigned char *sig
, size_t *siglen
,
1269 const unsigned char *tbs
,
1273 EVP_PKEY
*pkey
= NULL
;
1277 rv
= find_key(&pkey
, name
, public_keys
);
1279 rv
= find_key(&pkey
, name
, private_keys
);
1280 if (rv
== 0 || pkey
== NULL
) {
1285 if (!TEST_ptr(kdata
= OPENSSL_zalloc(sizeof(*kdata
)))) {
1286 EVP_PKEY_free(pkey
);
1289 kdata
->keyop
= keyop
;
1290 if (!TEST_ptr(kdata
->ctx
= EVP_PKEY_CTX_new(pkey
, NULL
))) {
1291 EVP_PKEY_free(pkey
);
1292 OPENSSL_free(kdata
);
1295 if (keyopinit(kdata
->ctx
) <= 0)
1296 t
->err
= "KEYOP_INIT_ERROR";
1301 static void pkey_test_cleanup(EVP_TEST
*t
)
1303 PKEY_DATA
*kdata
= t
->data
;
1305 OPENSSL_free(kdata
->input
);
1306 OPENSSL_free(kdata
->output
);
1307 EVP_PKEY_CTX_free(kdata
->ctx
);
1310 static int pkey_test_ctrl(EVP_TEST
*t
, EVP_PKEY_CTX
*pctx
,
1316 if (!TEST_ptr(tmpval
= OPENSSL_strdup(value
)))
1318 p
= strchr(tmpval
, ':');
1321 rv
= EVP_PKEY_CTX_ctrl_str(pctx
, tmpval
, p
);
1323 t
->err
= "PKEY_CTRL_INVALID";
1325 } else if (p
!= NULL
&& rv
<= 0) {
1326 /* If p has an OID and lookup fails assume disabled algorithm */
1327 int nid
= OBJ_sn2nid(p
);
1329 if (nid
== NID_undef
)
1330 nid
= OBJ_ln2nid(p
);
1331 if (nid
!= NID_undef
1332 && EVP_get_digestbynid(nid
) == NULL
1333 && EVP_get_cipherbynid(nid
) == NULL
) {
1337 t
->err
= "PKEY_CTRL_ERROR";
1341 OPENSSL_free(tmpval
);
1345 static int pkey_test_parse(EVP_TEST
*t
,
1346 const char *keyword
, const char *value
)
1348 PKEY_DATA
*kdata
= t
->data
;
1349 if (strcmp(keyword
, "Input") == 0)
1350 return parse_bin(value
, &kdata
->input
, &kdata
->input_len
);
1351 if (strcmp(keyword
, "Output") == 0)
1352 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
1353 if (strcmp(keyword
, "Ctrl") == 0)
1354 return pkey_test_ctrl(t
, kdata
->ctx
, value
);
1358 static int pkey_test_run(EVP_TEST
*t
)
1360 PKEY_DATA
*expected
= t
->data
;
1361 unsigned char *got
= NULL
;
1364 if (expected
->keyop(expected
->ctx
, NULL
, &got_len
,
1365 expected
->input
, expected
->input_len
) <= 0
1366 || !TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1367 t
->err
= "KEYOP_LENGTH_ERROR";
1370 if (expected
->keyop(expected
->ctx
, got
, &got_len
,
1371 expected
->input
, expected
->input_len
) <= 0) {
1372 t
->err
= "KEYOP_ERROR";
1375 if (!memory_err_compare(t
, "KEYOP_MISMATCH",
1376 expected
->output
, expected
->output_len
,
1386 static int sign_test_init(EVP_TEST
*t
, const char *name
)
1388 return pkey_test_init(t
, name
, 0, EVP_PKEY_sign_init
, EVP_PKEY_sign
);
1391 static const EVP_TEST_METHOD psign_test_method
= {
1399 static int verify_recover_test_init(EVP_TEST
*t
, const char *name
)
1401 return pkey_test_init(t
, name
, 1, EVP_PKEY_verify_recover_init
,
1402 EVP_PKEY_verify_recover
);
1405 static const EVP_TEST_METHOD pverify_recover_test_method
= {
1407 verify_recover_test_init
,
1413 static int decrypt_test_init(EVP_TEST
*t
, const char *name
)
1415 return pkey_test_init(t
, name
, 0, EVP_PKEY_decrypt_init
,
1419 static const EVP_TEST_METHOD pdecrypt_test_method
= {
1427 static int verify_test_init(EVP_TEST
*t
, const char *name
)
1429 return pkey_test_init(t
, name
, 1, EVP_PKEY_verify_init
, 0);
1432 static int verify_test_run(EVP_TEST
*t
)
1434 PKEY_DATA
*kdata
= t
->data
;
1436 if (EVP_PKEY_verify(kdata
->ctx
, kdata
->output
, kdata
->output_len
,
1437 kdata
->input
, kdata
->input_len
) <= 0)
1438 t
->err
= "VERIFY_ERROR";
1442 static const EVP_TEST_METHOD pverify_test_method
= {
1451 static int pderive_test_init(EVP_TEST
*t
, const char *name
)
1453 return pkey_test_init(t
, name
, 0, EVP_PKEY_derive_init
, 0);
1456 static int pderive_test_parse(EVP_TEST
*t
,
1457 const char *keyword
, const char *value
)
1459 PKEY_DATA
*kdata
= t
->data
;
1461 if (strcmp(keyword
, "PeerKey") == 0) {
1463 if (find_key(&peer
, value
, public_keys
) == 0)
1465 if (EVP_PKEY_derive_set_peer(kdata
->ctx
, peer
) <= 0)
1469 if (strcmp(keyword
, "SharedSecret") == 0)
1470 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
1471 if (strcmp(keyword
, "Ctrl") == 0)
1472 return pkey_test_ctrl(t
, kdata
->ctx
, value
);
1476 static int pderive_test_run(EVP_TEST
*t
)
1478 PKEY_DATA
*expected
= t
->data
;
1479 unsigned char *got
= NULL
;
1482 if (EVP_PKEY_derive(expected
->ctx
, NULL
, &got_len
) <= 0) {
1483 t
->err
= "DERIVE_ERROR";
1486 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1487 t
->err
= "DERIVE_ERROR";
1490 if (EVP_PKEY_derive(expected
->ctx
, got
, &got_len
) <= 0) {
1491 t
->err
= "DERIVE_ERROR";
1494 if (!memory_err_compare(t
, "SHARED_SECRET_MISMATCH",
1495 expected
->output
, expected
->output_len
,
1505 static const EVP_TEST_METHOD pderive_test_method
= {
1518 typedef enum pbe_type_enum
{
1519 PBE_TYPE_INVALID
= 0,
1520 PBE_TYPE_SCRYPT
, PBE_TYPE_PBKDF2
, PBE_TYPE_PKCS12
1523 typedef struct pbe_data_st
{
1525 /* scrypt parameters */
1526 uint64_t N
, r
, p
, maxmem
;
1527 /* PKCS#12 parameters */
1531 unsigned char *pass
;
1534 unsigned char *salt
;
1536 /* Expected output */
1541 #ifndef OPENSSL_NO_SCRYPT
1543 * Parse unsigned decimal 64 bit integer value
1545 static int parse_uint64(const char *value
, uint64_t *pr
)
1547 const char *p
= value
;
1549 if (!TEST_true(*p
)) {
1550 TEST_info("Invalid empty integer value");
1553 for (*pr
= 0; *p
; ) {
1554 if (*pr
> UINT64_MAX
/ 10) {
1555 TEST_error("Integer overflow in string %s", value
);
1559 if (!TEST_true(isdigit((unsigned char)*p
))) {
1560 TEST_error("Invalid character in string %s", value
);
1569 static int scrypt_test_parse(EVP_TEST
*t
,
1570 const char *keyword
, const char *value
)
1572 PBE_DATA
*pdata
= t
->data
;
1574 if (strcmp(keyword
, "N") == 0)
1575 return parse_uint64(value
, &pdata
->N
);
1576 if (strcmp(keyword
, "p") == 0)
1577 return parse_uint64(value
, &pdata
->p
);
1578 if (strcmp(keyword
, "r") == 0)
1579 return parse_uint64(value
, &pdata
->r
);
1580 if (strcmp(keyword
, "maxmem") == 0)
1581 return parse_uint64(value
, &pdata
->maxmem
);
1586 static int pbkdf2_test_parse(EVP_TEST
*t
,
1587 const char *keyword
, const char *value
)
1589 PBE_DATA
*pdata
= t
->data
;
1591 if (strcmp(keyword
, "iter") == 0) {
1592 pdata
->iter
= atoi(value
);
1593 if (pdata
->iter
<= 0)
1597 if (strcmp(keyword
, "MD") == 0) {
1598 pdata
->md
= EVP_get_digestbyname(value
);
1599 if (pdata
->md
== NULL
)
1606 static int pkcs12_test_parse(EVP_TEST
*t
,
1607 const char *keyword
, const char *value
)
1609 PBE_DATA
*pdata
= t
->data
;
1611 if (strcmp(keyword
, "id") == 0) {
1612 pdata
->id
= atoi(value
);
1617 return pbkdf2_test_parse(t
, keyword
, value
);
1620 static int pbe_test_init(EVP_TEST
*t
, const char *alg
)
1623 PBE_TYPE pbe_type
= PBE_TYPE_INVALID
;
1625 if (strcmp(alg
, "scrypt") == 0) {
1626 #ifndef OPENSSL_NO_SCRYPT
1627 pbe_type
= PBE_TYPE_SCRYPT
;
1632 } else if (strcmp(alg
, "pbkdf2") == 0) {
1633 pbe_type
= PBE_TYPE_PBKDF2
;
1634 } else if (strcmp(alg
, "pkcs12") == 0) {
1635 pbe_type
= PBE_TYPE_PKCS12
;
1637 TEST_error("Unknown pbe algorithm %s", alg
);
1639 pdat
= OPENSSL_zalloc(sizeof(*pdat
));
1640 pdat
->pbe_type
= pbe_type
;
1645 static void pbe_test_cleanup(EVP_TEST
*t
)
1647 PBE_DATA
*pdat
= t
->data
;
1649 OPENSSL_free(pdat
->pass
);
1650 OPENSSL_free(pdat
->salt
);
1651 OPENSSL_free(pdat
->key
);
1654 static int pbe_test_parse(EVP_TEST
*t
,
1655 const char *keyword
, const char *value
)
1657 PBE_DATA
*pdata
= t
->data
;
1659 if (strcmp(keyword
, "Password") == 0)
1660 return parse_bin(value
, &pdata
->pass
, &pdata
->pass_len
);
1661 if (strcmp(keyword
, "Salt") == 0)
1662 return parse_bin(value
, &pdata
->salt
, &pdata
->salt_len
);
1663 if (strcmp(keyword
, "Key") == 0)
1664 return parse_bin(value
, &pdata
->key
, &pdata
->key_len
);
1665 if (pdata
->pbe_type
== PBE_TYPE_PBKDF2
)
1666 return pbkdf2_test_parse(t
, keyword
, value
);
1667 else if (pdata
->pbe_type
== PBE_TYPE_PKCS12
)
1668 return pkcs12_test_parse(t
, keyword
, value
);
1669 #ifndef OPENSSL_NO_SCRYPT
1670 else if (pdata
->pbe_type
== PBE_TYPE_SCRYPT
)
1671 return scrypt_test_parse(t
, keyword
, value
);
1676 static int pbe_test_run(EVP_TEST
*t
)
1678 PBE_DATA
*expected
= t
->data
;
1681 if (!TEST_ptr(key
= OPENSSL_malloc(expected
->key_len
))) {
1682 t
->err
= "INTERNAL_ERROR";
1685 if (expected
->pbe_type
== PBE_TYPE_PBKDF2
) {
1686 if (PKCS5_PBKDF2_HMAC((char *)expected
->pass
, expected
->pass_len
,
1687 expected
->salt
, expected
->salt_len
,
1688 expected
->iter
, expected
->md
,
1689 expected
->key_len
, key
) == 0) {
1690 t
->err
= "PBKDF2_ERROR";
1693 #ifndef OPENSSL_NO_SCRYPT
1694 } else if (expected
->pbe_type
== PBE_TYPE_SCRYPT
) {
1695 if (EVP_PBE_scrypt((const char *)expected
->pass
, expected
->pass_len
,
1696 expected
->salt
, expected
->salt_len
, expected
->N
,
1697 expected
->r
, expected
->p
, expected
->maxmem
,
1698 key
, expected
->key_len
) == 0) {
1699 t
->err
= "SCRYPT_ERROR";
1703 } else if (expected
->pbe_type
== PBE_TYPE_PKCS12
) {
1704 if (PKCS12_key_gen_uni(expected
->pass
, expected
->pass_len
,
1705 expected
->salt
, expected
->salt_len
,
1706 expected
->id
, expected
->iter
, expected
->key_len
,
1707 key
, expected
->md
) == 0) {
1708 t
->err
= "PKCS12_ERROR";
1712 if (!memory_err_compare(t
, "KEY_MISMATCH", expected
->key
, expected
->key_len
,
1713 key
, expected
->key_len
))
1722 static const EVP_TEST_METHOD pbe_test_method
= {
1736 BASE64_CANONICAL_ENCODING
= 0,
1737 BASE64_VALID_ENCODING
= 1,
1738 BASE64_INVALID_ENCODING
= 2
1739 } base64_encoding_type
;
1741 typedef struct encode_data_st
{
1742 /* Input to encoding */
1743 unsigned char *input
;
1745 /* Expected output */
1746 unsigned char *output
;
1748 base64_encoding_type encoding
;
1751 static int encode_test_init(EVP_TEST
*t
, const char *encoding
)
1755 if (!TEST_ptr(edata
= OPENSSL_zalloc(sizeof(*edata
))))
1757 if (strcmp(encoding
, "canonical") == 0) {
1758 edata
->encoding
= BASE64_CANONICAL_ENCODING
;
1759 } else if (strcmp(encoding
, "valid") == 0) {
1760 edata
->encoding
= BASE64_VALID_ENCODING
;
1761 } else if (strcmp(encoding
, "invalid") == 0) {
1762 edata
->encoding
= BASE64_INVALID_ENCODING
;
1763 if (!TEST_ptr(t
->expected_err
= OPENSSL_strdup("DECODE_ERROR")))
1766 TEST_error("Bad encoding: %s."
1767 " Should be one of {canonical, valid, invalid}",
1774 OPENSSL_free(edata
);
1778 static void encode_test_cleanup(EVP_TEST
*t
)
1780 ENCODE_DATA
*edata
= t
->data
;
1782 OPENSSL_free(edata
->input
);
1783 OPENSSL_free(edata
->output
);
1784 memset(edata
, 0, sizeof(*edata
));
1787 static int encode_test_parse(EVP_TEST
*t
,
1788 const char *keyword
, const char *value
)
1790 ENCODE_DATA
*edata
= t
->data
;
1792 if (strcmp(keyword
, "Input") == 0)
1793 return parse_bin(value
, &edata
->input
, &edata
->input_len
);
1794 if (strcmp(keyword
, "Output") == 0)
1795 return parse_bin(value
, &edata
->output
, &edata
->output_len
);
1799 static int encode_test_run(EVP_TEST
*t
)
1801 ENCODE_DATA
*expected
= t
->data
;
1802 unsigned char *encode_out
= NULL
, *decode_out
= NULL
;
1803 int output_len
, chunk_len
;
1804 EVP_ENCODE_CTX
*decode_ctx
= NULL
, *encode_ctx
= NULL
;
1806 if (!TEST_ptr(decode_ctx
= EVP_ENCODE_CTX_new())) {
1807 t
->err
= "INTERNAL_ERROR";
1811 if (expected
->encoding
== BASE64_CANONICAL_ENCODING
) {
1813 if (!TEST_ptr(encode_ctx
= EVP_ENCODE_CTX_new())
1814 || !TEST_ptr(encode_out
=
1815 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected
->input_len
))))
1818 EVP_EncodeInit(encode_ctx
);
1819 if (!TEST_true(EVP_EncodeUpdate(encode_ctx
, encode_out
, &chunk_len
,
1820 expected
->input
, expected
->input_len
)))
1823 output_len
= chunk_len
;
1825 EVP_EncodeFinal(encode_ctx
, encode_out
+ chunk_len
, &chunk_len
);
1826 output_len
+= chunk_len
;
1828 if (!memory_err_compare(t
, "BAD_ENCODING",
1829 expected
->output
, expected
->output_len
,
1830 encode_out
, output_len
))
1834 if (!TEST_ptr(decode_out
=
1835 OPENSSL_malloc(EVP_DECODE_LENGTH(expected
->output_len
))))
1838 EVP_DecodeInit(decode_ctx
);
1839 if (EVP_DecodeUpdate(decode_ctx
, decode_out
, &chunk_len
, expected
->output
,
1840 expected
->output_len
) < 0) {
1841 t
->err
= "DECODE_ERROR";
1844 output_len
= chunk_len
;
1846 if (EVP_DecodeFinal(decode_ctx
, decode_out
+ chunk_len
, &chunk_len
) != 1) {
1847 t
->err
= "DECODE_ERROR";
1850 output_len
+= chunk_len
;
1852 if (expected
->encoding
!= BASE64_INVALID_ENCODING
1853 && !memory_err_compare(t
, "BAD_DECODING",
1854 expected
->input
, expected
->input_len
,
1855 decode_out
, output_len
)) {
1856 t
->err
= "BAD_DECODING";
1862 OPENSSL_free(encode_out
);
1863 OPENSSL_free(decode_out
);
1864 EVP_ENCODE_CTX_free(decode_ctx
);
1865 EVP_ENCODE_CTX_free(encode_ctx
);
1869 static const EVP_TEST_METHOD encode_test_method
= {
1872 encode_test_cleanup
,
1881 typedef struct kdf_data_st
{
1882 /* Context for this operation */
1884 /* Expected output */
1885 unsigned char *output
;
1890 * Perform public key operation setup: lookup key, allocated ctx and call
1891 * the appropriate initialisation function
1893 static int kdf_test_init(EVP_TEST
*t
, const char *name
)
1896 int kdf_nid
= OBJ_sn2nid(name
);
1898 #ifdef OPENSSL_NO_SCRYPT
1899 if (strcmp(name
, "scrypt") == 0) {
1905 if (kdf_nid
== NID_undef
)
1906 kdf_nid
= OBJ_ln2nid(name
);
1908 if (!TEST_ptr(kdata
= OPENSSL_zalloc(sizeof(*kdata
))))
1910 kdata
->ctx
= EVP_PKEY_CTX_new_id(kdf_nid
, NULL
);
1911 if (kdata
->ctx
== NULL
) {
1912 OPENSSL_free(kdata
);
1915 if (EVP_PKEY_derive_init(kdata
->ctx
) <= 0) {
1916 EVP_PKEY_CTX_free(kdata
->ctx
);
1917 OPENSSL_free(kdata
);
1924 static void kdf_test_cleanup(EVP_TEST
*t
)
1926 KDF_DATA
*kdata
= t
->data
;
1927 OPENSSL_free(kdata
->output
);
1928 EVP_PKEY_CTX_free(kdata
->ctx
);
1931 static int kdf_test_parse(EVP_TEST
*t
,
1932 const char *keyword
, const char *value
)
1934 KDF_DATA
*kdata
= t
->data
;
1936 if (strcmp(keyword
, "Output") == 0)
1937 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
1938 if (strncmp(keyword
, "Ctrl", 4) == 0)
1939 return pkey_test_ctrl(t
, kdata
->ctx
, value
);
1943 static int kdf_test_run(EVP_TEST
*t
)
1945 KDF_DATA
*expected
= t
->data
;
1946 unsigned char *got
= NULL
;
1947 size_t got_len
= expected
->output_len
;
1949 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1950 t
->err
= "INTERNAL_ERROR";
1953 if (EVP_PKEY_derive(expected
->ctx
, got
, &got_len
) <= 0) {
1954 t
->err
= "KDF_DERIVE_ERROR";
1957 if (!memory_err_compare(t
, "KDF_MISMATCH",
1958 expected
->output
, expected
->output_len
,
1969 static const EVP_TEST_METHOD kdf_test_method
= {
1982 typedef struct keypair_test_data_st
{
1985 } KEYPAIR_TEST_DATA
;
1987 static int keypair_test_init(EVP_TEST
*t
, const char *pair
)
1989 KEYPAIR_TEST_DATA
*data
;
1991 EVP_PKEY
*pk
= NULL
, *pubk
= NULL
;
1992 char *pub
, *priv
= NULL
;
1994 /* Split private and public names. */
1995 if (!TEST_ptr(priv
= OPENSSL_strdup(pair
))
1996 || !TEST_ptr(pub
= strchr(priv
, ':'))) {
1997 t
->err
= "PARSING_ERROR";
2002 if (!TEST_true(find_key(&pk
, priv
, private_keys
))) {
2003 TEST_info("Can't find private key: %s", priv
);
2004 t
->err
= "MISSING_PRIVATE_KEY";
2007 if (!TEST_true(find_key(&pubk
, pub
, public_keys
))) {
2008 TEST_info("Can't find public key: %s", pub
);
2009 t
->err
= "MISSING_PUBLIC_KEY";
2013 if (pk
== NULL
&& pubk
== NULL
) {
2014 /* Both keys are listed but unsupported: skip this test */
2020 if (!TEST_ptr(data
= OPENSSL_malloc(sizeof(*data
))))
2033 static void keypair_test_cleanup(EVP_TEST
*t
)
2035 OPENSSL_free(t
->data
);
2040 * For tests that do not accept any custom keywords.
2042 static int void_test_parse(EVP_TEST
*t
, const char *keyword
, const char *value
)
2047 static int keypair_test_run(EVP_TEST
*t
)
2050 const KEYPAIR_TEST_DATA
*pair
= t
->data
;
2052 if (pair
->privk
== NULL
|| pair
->pubk
== NULL
) {
2054 * this can only happen if only one of the keys is not set
2055 * which means that one of them was unsupported while the
2056 * other isn't: hence a key type mismatch.
2058 t
->err
= "KEYPAIR_TYPE_MISMATCH";
2063 if ((rv
= EVP_PKEY_cmp(pair
->privk
, pair
->pubk
)) != 1 ) {
2065 t
->err
= "KEYPAIR_MISMATCH";
2066 } else if ( -1 == rv
) {
2067 t
->err
= "KEYPAIR_TYPE_MISMATCH";
2068 } else if ( -2 == rv
) {
2069 t
->err
= "UNSUPPORTED_KEY_COMPARISON";
2071 TEST_error("Unexpected error in key comparison");
2086 static const EVP_TEST_METHOD keypair_test_method
= {
2089 keypair_test_cleanup
,
2098 typedef struct keygen_test_data_st
{
2099 EVP_PKEY_CTX
*genctx
; /* Keygen context to use */
2100 char *keyname
; /* Key name to store key or NULL */
2103 static int keygen_test_init(EVP_TEST
*t
, const char *alg
)
2105 KEYGEN_TEST_DATA
*data
;
2106 EVP_PKEY_CTX
*genctx
;
2107 int nid
= OBJ_sn2nid(alg
);
2109 if (nid
== NID_undef
) {
2110 nid
= OBJ_ln2nid(alg
);
2111 if (nid
== NID_undef
)
2115 if (!TEST_ptr(genctx
= EVP_PKEY_CTX_new_id(nid
, NULL
))) {
2116 /* assume algorithm disabled */
2121 if (EVP_PKEY_keygen_init(genctx
) <= 0) {
2122 t
->err
= "KEYGEN_INIT_ERROR";
2126 if (!TEST_ptr(data
= OPENSSL_malloc(sizeof(*data
))))
2128 data
->genctx
= genctx
;
2129 data
->keyname
= NULL
;
2135 EVP_PKEY_CTX_free(genctx
);
2139 static void keygen_test_cleanup(EVP_TEST
*t
)
2141 KEYGEN_TEST_DATA
*keygen
= t
->data
;
2143 EVP_PKEY_CTX_free(keygen
->genctx
);
2144 OPENSSL_free(keygen
->keyname
);
2145 OPENSSL_free(t
->data
);
2149 static int keygen_test_parse(EVP_TEST
*t
,
2150 const char *keyword
, const char *value
)
2152 KEYGEN_TEST_DATA
*keygen
= t
->data
;
2154 if (strcmp(keyword
, "KeyName") == 0)
2155 return TEST_ptr(keygen
->keyname
= OPENSSL_strdup(value
));
2156 if (strcmp(keyword
, "Ctrl") == 0)
2157 return pkey_test_ctrl(t
, keygen
->genctx
, value
);
2161 static int keygen_test_run(EVP_TEST
*t
)
2163 KEYGEN_TEST_DATA
*keygen
= t
->data
;
2164 EVP_PKEY
*pkey
= NULL
;
2167 if (EVP_PKEY_keygen(keygen
->genctx
, &pkey
) <= 0) {
2168 t
->err
= "KEYGEN_GENERATE_ERROR";
2172 if (keygen
->keyname
!= NULL
) {
2175 if (find_key(NULL
, keygen
->keyname
, private_keys
)) {
2176 TEST_info("Duplicate key %s", keygen
->keyname
);
2180 if (!TEST_ptr(key
= OPENSSL_malloc(sizeof(*key
))))
2182 key
->name
= keygen
->keyname
;
2183 keygen
->keyname
= NULL
;
2185 key
->next
= private_keys
;
2188 EVP_PKEY_free(pkey
);
2194 EVP_PKEY_free(pkey
);
2198 static const EVP_TEST_METHOD keygen_test_method
= {
2201 keygen_test_cleanup
,
2207 *** DIGEST SIGN+VERIFY TESTS
2211 int is_verify
; /* Set to 1 if verifying */
2212 int is_oneshot
; /* Set to 1 for one shot operation */
2213 const EVP_MD
*md
; /* Digest to use */
2214 EVP_MD_CTX
*ctx
; /* Digest context */
2216 STACK_OF(EVP_TEST_BUFFER
) *input
; /* Input data: streaming */
2217 unsigned char *osin
; /* Input data if one shot */
2218 size_t osin_len
; /* Input length data if one shot */
2219 unsigned char *output
; /* Expected output */
2220 size_t output_len
; /* Expected output length */
2223 static int digestsigver_test_init(EVP_TEST
*t
, const char *alg
, int is_verify
,
2226 const EVP_MD
*md
= NULL
;
2227 DIGESTSIGN_DATA
*mdat
;
2229 if (strcmp(alg
, "NULL") != 0) {
2230 if ((md
= EVP_get_digestbyname(alg
)) == NULL
) {
2231 /* If alg has an OID assume disabled algorithm */
2232 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
2239 if (!TEST_ptr(mdat
= OPENSSL_zalloc(sizeof(*mdat
))))
2242 if (!TEST_ptr(mdat
->ctx
= EVP_MD_CTX_new())) {
2246 mdat
->is_verify
= is_verify
;
2247 mdat
->is_oneshot
= is_oneshot
;
2252 static int digestsign_test_init(EVP_TEST
*t
, const char *alg
)
2254 return digestsigver_test_init(t
, alg
, 0, 0);
2257 static void digestsigver_test_cleanup(EVP_TEST
*t
)
2259 DIGESTSIGN_DATA
*mdata
= t
->data
;
2261 EVP_MD_CTX_free(mdata
->ctx
);
2262 sk_EVP_TEST_BUFFER_pop_free(mdata
->input
, evp_test_buffer_free
);
2263 OPENSSL_free(mdata
->osin
);
2264 OPENSSL_free(mdata
->output
);
2265 OPENSSL_free(mdata
);
2269 static int digestsigver_test_parse(EVP_TEST
*t
,
2270 const char *keyword
, const char *value
)
2272 DIGESTSIGN_DATA
*mdata
= t
->data
;
2274 if (strcmp(keyword
, "Key") == 0) {
2275 EVP_PKEY
*pkey
= NULL
;
2278 if (mdata
->is_verify
)
2279 rv
= find_key(&pkey
, value
, public_keys
);
2281 rv
= find_key(&pkey
, value
, private_keys
);
2282 if (rv
== 0 || pkey
== NULL
) {
2286 if (mdata
->is_verify
) {
2287 if (!EVP_DigestVerifyInit(mdata
->ctx
, &mdata
->pctx
, mdata
->md
,
2289 t
->err
= "DIGESTVERIFYINIT_ERROR";
2292 if (!EVP_DigestSignInit(mdata
->ctx
, &mdata
->pctx
, mdata
->md
, NULL
,
2294 t
->err
= "DIGESTSIGNINIT_ERROR";
2298 if (strcmp(keyword
, "Input") == 0) {
2299 if (mdata
->is_oneshot
)
2300 return parse_bin(value
, &mdata
->osin
, &mdata
->osin_len
);
2301 return evp_test_buffer_append(value
, &mdata
->input
);
2303 if (strcmp(keyword
, "Output") == 0)
2304 return parse_bin(value
, &mdata
->output
, &mdata
->output_len
);
2306 if (!mdata
->is_oneshot
) {
2307 if (strcmp(keyword
, "Count") == 0)
2308 return evp_test_buffer_set_count(value
, mdata
->input
);
2309 if (strcmp(keyword
, "Ncopy") == 0)
2310 return evp_test_buffer_ncopy(value
, mdata
->input
);
2312 if (strcmp(keyword
, "Ctrl") == 0) {
2313 if (mdata
->pctx
== NULL
)
2315 return pkey_test_ctrl(t
, mdata
->pctx
, value
);
2320 static int digestsign_update_fn(void *ctx
, const unsigned char *buf
,
2323 return EVP_DigestSignUpdate(ctx
, buf
, buflen
);
2326 static int digestsign_test_run(EVP_TEST
*t
)
2328 DIGESTSIGN_DATA
*expected
= t
->data
;
2329 unsigned char *got
= NULL
;
2332 if (!evp_test_buffer_do(expected
->input
, digestsign_update_fn
,
2334 t
->err
= "DIGESTUPDATE_ERROR";
2338 if (!EVP_DigestSignFinal(expected
->ctx
, NULL
, &got_len
)) {
2339 t
->err
= "DIGESTSIGNFINAL_LENGTH_ERROR";
2342 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
2343 t
->err
= "MALLOC_FAILURE";
2346 if (!EVP_DigestSignFinal(expected
->ctx
, got
, &got_len
)) {
2347 t
->err
= "DIGESTSIGNFINAL_ERROR";
2350 if (!memory_err_compare(t
, "SIGNATURE_MISMATCH",
2351 expected
->output
, expected
->output_len
,
2361 static const EVP_TEST_METHOD digestsign_test_method
= {
2363 digestsign_test_init
,
2364 digestsigver_test_cleanup
,
2365 digestsigver_test_parse
,
2369 static int digestverify_test_init(EVP_TEST
*t
, const char *alg
)
2371 return digestsigver_test_init(t
, alg
, 1, 0);
2374 static int digestverify_update_fn(void *ctx
, const unsigned char *buf
,
2377 return EVP_DigestVerifyUpdate(ctx
, buf
, buflen
);
2380 static int digestverify_test_run(EVP_TEST
*t
)
2382 DIGESTSIGN_DATA
*mdata
= t
->data
;
2384 if (!evp_test_buffer_do(mdata
->input
, digestverify_update_fn
, mdata
->ctx
)) {
2385 t
->err
= "DIGESTUPDATE_ERROR";
2389 if (EVP_DigestVerifyFinal(mdata
->ctx
, mdata
->output
,
2390 mdata
->output_len
) <= 0)
2391 t
->err
= "VERIFY_ERROR";
2395 static const EVP_TEST_METHOD digestverify_test_method
= {
2397 digestverify_test_init
,
2398 digestsigver_test_cleanup
,
2399 digestsigver_test_parse
,
2400 digestverify_test_run
2403 static int oneshot_digestsign_test_init(EVP_TEST
*t
, const char *alg
)
2405 return digestsigver_test_init(t
, alg
, 0, 1);
2408 static int oneshot_digestsign_test_run(EVP_TEST
*t
)
2410 DIGESTSIGN_DATA
*expected
= t
->data
;
2411 unsigned char *got
= NULL
;
2414 if (!EVP_DigestSign(expected
->ctx
, NULL
, &got_len
,
2415 expected
->osin
, expected
->osin_len
)) {
2416 t
->err
= "DIGESTSIGN_LENGTH_ERROR";
2419 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
2420 t
->err
= "MALLOC_FAILURE";
2423 if (!EVP_DigestSign(expected
->ctx
, got
, &got_len
,
2424 expected
->osin
, expected
->osin_len
)) {
2425 t
->err
= "DIGESTSIGN_ERROR";
2428 if (!memory_err_compare(t
, "SIGNATURE_MISMATCH",
2429 expected
->output
, expected
->output_len
,
2439 static const EVP_TEST_METHOD oneshot_digestsign_test_method
= {
2440 "OneShotDigestSign",
2441 oneshot_digestsign_test_init
,
2442 digestsigver_test_cleanup
,
2443 digestsigver_test_parse
,
2444 oneshot_digestsign_test_run
2447 static int oneshot_digestverify_test_init(EVP_TEST
*t
, const char *alg
)
2449 return digestsigver_test_init(t
, alg
, 1, 1);
2452 static int oneshot_digestverify_test_run(EVP_TEST
*t
)
2454 DIGESTSIGN_DATA
*mdata
= t
->data
;
2456 if (EVP_DigestVerify(mdata
->ctx
, mdata
->output
, mdata
->output_len
,
2457 mdata
->osin
, mdata
->osin_len
) <= 0)
2458 t
->err
= "VERIFY_ERROR";
2462 static const EVP_TEST_METHOD oneshot_digestverify_test_method
= {
2463 "OneShotDigestVerify",
2464 oneshot_digestverify_test_init
,
2465 digestsigver_test_cleanup
,
2466 digestsigver_test_parse
,
2467 oneshot_digestverify_test_run
2472 *** PARSING AND DISPATCH
2475 static const EVP_TEST_METHOD
*evp_test_list
[] = {
2476 &cipher_test_method
,
2477 &digest_test_method
,
2478 &digestsign_test_method
,
2479 &digestverify_test_method
,
2480 &encode_test_method
,
2482 &keypair_test_method
,
2483 &keygen_test_method
,
2485 &oneshot_digestsign_test_method
,
2486 &oneshot_digestverify_test_method
,
2488 &pdecrypt_test_method
,
2489 &pderive_test_method
,
2491 &pverify_recover_test_method
,
2492 &pverify_test_method
,
2496 static const EVP_TEST_METHOD
*find_test(const char *name
)
2498 const EVP_TEST_METHOD
**tt
;
2500 for (tt
= evp_test_list
; *tt
; tt
++) {
2501 if (strcmp(name
, (*tt
)->name
) == 0)
2507 static void clear_test(EVP_TEST
*t
)
2509 test_clearstanza(&t
->s
);
2511 if (t
->data
!= NULL
) {
2512 if (t
->meth
!= NULL
)
2513 t
->meth
->cleanup(t
);
2514 OPENSSL_free(t
->data
);
2517 OPENSSL_free(t
->expected_err
);
2518 t
->expected_err
= NULL
;
2519 OPENSSL_free(t
->func
);
2521 OPENSSL_free(t
->reason
);
2531 * Check for errors in the test structure; return 1 if okay, else 0.
2533 static int check_test_error(EVP_TEST
*t
)
2539 if (t
->err
== NULL
&& t
->expected_err
== NULL
)
2541 if (t
->err
!= NULL
&& t
->expected_err
== NULL
) {
2542 if (t
->aux_err
!= NULL
) {
2543 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
2544 t
->s
.test_file
, t
->s
.start
, t
->aux_err
, t
->err
);
2546 TEST_info("%s:%d: Source of above error; unexpected error %s",
2547 t
->s
.test_file
, t
->s
.start
, t
->err
);
2551 if (t
->err
== NULL
&& t
->expected_err
!= NULL
) {
2552 TEST_info("%s:%d: Succeeded but was expecting %s",
2553 t
->s
.test_file
, t
->s
.start
, t
->expected_err
);
2557 if (strcmp(t
->err
, t
->expected_err
) != 0) {
2558 TEST_info("%s:%d: Expected %s got %s",
2559 t
->s
.test_file
, t
->s
.start
, t
->expected_err
, t
->err
);
2563 if (t
->func
== NULL
&& t
->reason
== NULL
)
2566 if (t
->func
== NULL
|| t
->reason
== NULL
) {
2567 TEST_info("%s:%d: Test is missing function or reason code",
2568 t
->s
.test_file
, t
->s
.start
);
2572 err
= ERR_peek_error();
2574 TEST_info("%s:%d: Expected error \"%s:%s\" not set",
2575 t
->s
.test_file
, t
->s
.start
, t
->func
, t
->reason
);
2579 func
= ERR_func_error_string(err
);
2580 reason
= ERR_reason_error_string(err
);
2581 if (func
== NULL
&& reason
== NULL
) {
2582 TEST_info("%s:%d: Expected error \"%s:%s\", no strings available."
2584 t
->s
.test_file
, t
->s
.start
, t
->func
, t
->reason
);
2588 if (strcmp(func
, t
->func
) == 0 && strcmp(reason
, t
->reason
) == 0)
2591 TEST_info("%s:%d: Expected error \"%s:%s\", got \"%s:%s\"",
2592 t
->s
.test_file
, t
->s
.start
, t
->func
, t
->reason
, func
, reason
);
2598 * Run a parsed test. Log a message and return 0 on error.
2600 static int run_test(EVP_TEST
*t
)
2602 if (t
->meth
== NULL
)
2609 if (t
->err
== NULL
&& t
->meth
->run_test(t
) != 1) {
2610 TEST_info("%s:%d %s error",
2611 t
->s
.test_file
, t
->s
.start
, t
->meth
->name
);
2614 if (!check_test_error(t
)) {
2615 TEST_openssl_errors();
2624 static int find_key(EVP_PKEY
**ppk
, const char *name
, KEY_LIST
*lst
)
2626 for (; lst
!= NULL
; lst
= lst
->next
) {
2627 if (strcmp(lst
->name
, name
) == 0) {
2636 static void free_key_list(KEY_LIST
*lst
)
2638 while (lst
!= NULL
) {
2639 KEY_LIST
*next
= lst
->next
;
2641 EVP_PKEY_free(lst
->key
);
2642 OPENSSL_free(lst
->name
);
2649 * Is the key type an unsupported algorithm?
2651 static int key_unsupported(void)
2653 long err
= ERR_peek_error();
2655 if (ERR_GET_LIB(err
) == ERR_LIB_EVP
2656 && ERR_GET_REASON(err
) == EVP_R_UNSUPPORTED_ALGORITHM
) {
2660 #ifndef OPENSSL_NO_EC
2662 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
2663 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
2666 if (ERR_GET_LIB(err
) == ERR_LIB_EC
2667 && ERR_GET_REASON(err
) == EC_R_UNKNOWN_GROUP
) {
2671 #endif /* OPENSSL_NO_EC */
2676 * NULL out the value from |pp| but return it. This "steals" a pointer.
2678 static char *take_value(PAIR
*pp
)
2680 char *p
= pp
->value
;
2687 * Read and parse one test. Return 0 if failure, 1 if okay.
2689 static int parse(EVP_TEST
*t
)
2691 KEY_LIST
*key
, **klist
;
2698 if (BIO_eof(t
->s
.fp
))
2701 if (!test_readstanza(&t
->s
))
2703 } while (t
->s
.numpairs
== 0);
2704 pp
= &t
->s
.pairs
[0];
2706 /* Are we adding a key? */
2709 if (strcmp(pp
->key
, "PrivateKey") == 0) {
2710 pkey
= PEM_read_bio_PrivateKey(t
->s
.key
, NULL
, 0, NULL
);
2711 if (pkey
== NULL
&& !key_unsupported()) {
2712 EVP_PKEY_free(pkey
);
2713 TEST_info("Can't read private key %s", pp
->value
);
2714 TEST_openssl_errors();
2717 klist
= &private_keys
;
2718 } else if (strcmp(pp
->key
, "PublicKey") == 0) {
2719 pkey
= PEM_read_bio_PUBKEY(t
->s
.key
, NULL
, 0, NULL
);
2720 if (pkey
== NULL
&& !key_unsupported()) {
2721 EVP_PKEY_free(pkey
);
2722 TEST_info("Can't read public key %s", pp
->value
);
2723 TEST_openssl_errors();
2726 klist
= &public_keys
;
2727 } else if (strcmp(pp
->key
, "PrivateKeyRaw") == 0
2728 || strcmp(pp
->key
, "PublicKeyRaw") == 0 ) {
2729 char *strnid
= NULL
, *keydata
= NULL
;
2730 unsigned char *keybin
;
2734 if (strcmp(pp
->key
, "PrivateKeyRaw") == 0)
2735 klist
= &private_keys
;
2737 klist
= &public_keys
;
2739 strnid
= strchr(pp
->value
, ':');
2740 if (strnid
!= NULL
) {
2742 keydata
= strchr(strnid
, ':');
2743 if (keydata
!= NULL
)
2746 if (keydata
== NULL
) {
2747 TEST_info("Failed to parse %s value", pp
->key
);
2751 nid
= OBJ_txt2nid(strnid
);
2752 if (nid
== NID_undef
) {
2753 TEST_info("Uncrecognised algorithm NID");
2756 if (!parse_bin(keydata
, &keybin
, &keylen
)) {
2757 TEST_info("Failed to create binary key");
2760 if (klist
== &private_keys
)
2761 pkey
= EVP_PKEY_new_raw_private_key(nid
, NULL
, keybin
, keylen
);
2763 pkey
= EVP_PKEY_new_raw_public_key(nid
, NULL
, keybin
, keylen
);
2764 if (pkey
== NULL
&& !key_unsupported()) {
2765 TEST_info("Can't read %s data", pp
->key
);
2766 OPENSSL_free(keybin
);
2767 TEST_openssl_errors();
2770 OPENSSL_free(keybin
);
2773 /* If we have a key add to list */
2774 if (klist
!= NULL
) {
2775 if (find_key(NULL
, pp
->value
, *klist
)) {
2776 TEST_info("Duplicate key %s", pp
->value
);
2779 if (!TEST_ptr(key
= OPENSSL_malloc(sizeof(*key
))))
2781 key
->name
= take_value(pp
);
2783 /* Hack to detect SM2 keys */
2784 if(pkey
!= NULL
&& strstr(key
->name
, "SM2") != NULL
) {
2785 #ifdef OPENSSL_NO_SM2
2786 EVP_PKEY_free(pkey
);
2789 EVP_PKEY_set_alias_type(pkey
, EVP_PKEY_SM2
);
2797 /* Go back and start a new stanza. */
2798 if (t
->s
.numpairs
!= 1)
2799 TEST_info("Line %d: missing blank line\n", t
->s
.curr
);
2803 /* Find the test, based on first keyword. */
2804 if (!TEST_ptr(t
->meth
= find_test(pp
->key
)))
2806 if (!t
->meth
->init(t
, pp
->value
)) {
2807 TEST_error("unknown %s: %s\n", pp
->key
, pp
->value
);
2811 /* TEST_info("skipping %s %s", pp->key, pp->value); */
2815 for (pp
++, i
= 1; i
< t
->s
.numpairs
; pp
++, i
++) {
2816 if (strcmp(pp
->key
, "Result") == 0) {
2817 if (t
->expected_err
!= NULL
) {
2818 TEST_info("Line %d: multiple result lines", t
->s
.curr
);
2821 t
->expected_err
= take_value(pp
);
2822 } else if (strcmp(pp
->key
, "Function") == 0) {
2823 if (t
->func
!= NULL
) {
2824 TEST_info("Line %d: multiple function lines\n", t
->s
.curr
);
2827 t
->func
= take_value(pp
);
2828 } else if (strcmp(pp
->key
, "Reason") == 0) {
2829 if (t
->reason
!= NULL
) {
2830 TEST_info("Line %d: multiple reason lines", t
->s
.curr
);
2833 t
->reason
= take_value(pp
);
2835 /* Must be test specific line: try to parse it */
2836 int rv
= t
->meth
->parse(t
, pp
->key
, pp
->value
);
2839 TEST_info("Line %d: unknown keyword %s", t
->s
.curr
, pp
->key
);
2843 TEST_info("Line %d: error processing keyword %s = %s\n",
2844 t
->s
.curr
, pp
->key
, pp
->value
);
2853 static int run_file_tests(int i
)
2856 const char *testfile
= test_get_argument(i
);
2859 if (!TEST_ptr(t
= OPENSSL_zalloc(sizeof(*t
))))
2861 if (!test_start_file(&t
->s
, testfile
)) {
2866 while (!BIO_eof(t
->s
.fp
)) {
2870 if (c
== 0 || !run_test(t
)) {
2875 test_end_file(&t
->s
);
2878 free_key_list(public_keys
);
2879 free_key_list(private_keys
);
2886 int setup_tests(void)
2888 size_t n
= test_get_argument_count();
2891 TEST_error("Usage: %s file...", test_get_program_name());
2895 ADD_ALL_TESTS(run_file_tests
, n
);