2 * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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"
25 typedef struct evp_test_method_st EVP_TEST_METHOD
;
28 * Structure holding test information
30 typedef struct evp_test_st
{
31 STANZA s
; /* Common test stanza */
33 int skip
; /* Current test should be skipped */
34 const EVP_TEST_METHOD
*meth
; /* method for this test */
35 const char *err
, *aux_err
; /* Error string for test */
36 char *expected_err
; /* Expected error value of test */
37 char *func
; /* Expected error function string */
38 char *reason
; /* Expected error reason string */
39 void *data
; /* test specific data */
43 * Test method structure
45 struct evp_test_method_st
{
46 /* Name of test as it appears in file */
48 /* Initialise test for "alg" */
49 int (*init
) (EVP_TEST
* t
, const char *alg
);
51 void (*cleanup
) (EVP_TEST
* t
);
52 /* Test specific name value pair processing */
53 int (*parse
) (EVP_TEST
* t
, const char *name
, const char *value
);
54 /* Run the test itself */
55 int (*run_test
) (EVP_TEST
* t
);
60 * Linked list of named keys.
62 typedef struct key_list_st
{
65 struct key_list_st
*next
;
69 * List of public and private keys
71 static KEY_LIST
*private_keys
;
72 static KEY_LIST
*public_keys
;
73 static int find_key(EVP_PKEY
**ppk
, const char *name
, KEY_LIST
*lst
);
75 static int parse_bin(const char *value
, unsigned char **buf
, size_t *buflen
);
78 * Compare two memory regions for equality, returning zero if they differ.
79 * However, if there is expected to be an error and the actual error
80 * matches then the memory is expected to be different so handle this
81 * case without producing unnecessary test framework output.
83 static int memory_err_compare(EVP_TEST
*t
, const char *err
,
84 const void *expected
, size_t expected_len
,
85 const void *got
, size_t got_len
)
89 if (t
->expected_err
!= NULL
&& strcmp(t
->expected_err
, err
) == 0)
90 r
= !TEST_mem_ne(expected
, expected_len
, got
, got_len
);
92 r
= TEST_mem_eq(expected
, expected_len
, got
, got_len
);
99 * Structure used to hold a list of blocks of memory to test
100 * calls to "update" like functions.
102 struct evp_test_buffer_st
{
109 static void evp_test_buffer_free(EVP_TEST_BUFFER
*db
)
112 OPENSSL_free(db
->buf
);
118 * append buffer to a list
120 static int evp_test_buffer_append(const char *value
,
121 STACK_OF(EVP_TEST_BUFFER
) **sk
)
123 EVP_TEST_BUFFER
*db
= NULL
;
125 if (!TEST_ptr(db
= OPENSSL_malloc(sizeof(*db
))))
128 if (!parse_bin(value
, &db
->buf
, &db
->buflen
))
133 if (*sk
== NULL
&& !TEST_ptr(*sk
= sk_EVP_TEST_BUFFER_new_null()))
135 if (!sk_EVP_TEST_BUFFER_push(*sk
, db
))
141 evp_test_buffer_free(db
);
146 * replace last buffer in list with copies of itself
148 static int evp_test_buffer_ncopy(const char *value
,
149 STACK_OF(EVP_TEST_BUFFER
) *sk
)
152 unsigned char *tbuf
, *p
;
154 int ncopy
= atoi(value
);
159 if (sk
== NULL
|| sk_EVP_TEST_BUFFER_num(sk
) == 0)
161 db
= sk_EVP_TEST_BUFFER_value(sk
, sk_EVP_TEST_BUFFER_num(sk
) - 1);
163 tbuflen
= db
->buflen
* ncopy
;
164 if (!TEST_ptr(tbuf
= OPENSSL_malloc(tbuflen
)))
166 for (i
= 0, p
= tbuf
; i
< ncopy
; i
++, p
+= db
->buflen
)
167 memcpy(p
, db
->buf
, db
->buflen
);
169 OPENSSL_free(db
->buf
);
171 db
->buflen
= tbuflen
;
176 * set repeat count for last buffer in list
178 static int evp_test_buffer_set_count(const char *value
,
179 STACK_OF(EVP_TEST_BUFFER
) *sk
)
182 int count
= atoi(value
);
187 if (sk
== NULL
|| sk_EVP_TEST_BUFFER_num(sk
) == 0)
190 db
= sk_EVP_TEST_BUFFER_value(sk
, sk_EVP_TEST_BUFFER_num(sk
) - 1);
191 if (db
->count_set
!= 0)
194 db
->count
= (size_t)count
;
200 * call "fn" with each element of the list in turn
202 static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER
) *sk
,
204 const unsigned char *buf
,
210 for (i
= 0; i
< sk_EVP_TEST_BUFFER_num(sk
); i
++) {
211 EVP_TEST_BUFFER
*tb
= sk_EVP_TEST_BUFFER_value(sk
, i
);
214 for (j
= 0; j
< tb
->count
; j
++) {
215 if (fn(ctx
, tb
->buf
, tb
->buflen
) <= 0)
223 * Unescape some sequences in string literals (only \n for now).
224 * Return an allocated buffer, set |out_len|. If |input_len|
225 * is zero, get an empty buffer but set length to zero.
227 static unsigned char* unescape(const char *input
, size_t input_len
,
230 unsigned char *ret
, *p
;
233 if (input_len
== 0) {
235 return OPENSSL_zalloc(1);
238 /* Escaping is non-expanding; over-allocate original size for simplicity. */
239 if (!TEST_ptr(ret
= p
= OPENSSL_malloc(input_len
)))
242 for (i
= 0; i
< input_len
; i
++) {
243 if (*input
== '\\') {
244 if (i
== input_len
- 1 || *++input
!= 'n') {
245 TEST_error("Bad escape sequence in file");
265 * For a hex string "value" convert to a binary allocated buffer.
266 * Return 1 on success or 0 on failure.
268 static int parse_bin(const char *value
, unsigned char **buf
, size_t *buflen
)
272 /* Check for NULL literal */
273 if (strcmp(value
, "NULL") == 0) {
279 /* Check for empty value */
280 if (*value
== '\0') {
282 * Don't return NULL for zero length buffer. This is needed for
283 * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key
284 * buffer even if the key length is 0, in order to detect key reset.
286 *buf
= OPENSSL_malloc(1);
294 /* Check for string literal */
295 if (value
[0] == '"') {
296 size_t vlen
= strlen(++value
);
298 if (vlen
== 0 || value
[vlen
- 1] != '"')
301 *buf
= unescape(value
, vlen
, buflen
);
302 return *buf
== NULL
? 0 : 1;
305 /* Otherwise assume as hex literal and convert it to binary buffer */
306 if (!TEST_ptr(*buf
= OPENSSL_hexstr2buf(value
, &len
))) {
307 TEST_info("Can't convert %s", value
);
308 TEST_openssl_errors();
311 /* Size of input buffer means we'll never overflow */
318 *** MESSAGE DIGEST TESTS
321 typedef struct digest_data_st
{
322 /* Digest this test is for */
323 const EVP_MD
*digest
;
324 /* Input to digest */
325 STACK_OF(EVP_TEST_BUFFER
) *input
;
326 /* Expected output */
327 unsigned char *output
;
331 static int digest_test_init(EVP_TEST
*t
, const char *alg
)
334 const EVP_MD
*digest
;
336 if ((digest
= EVP_get_digestbyname(alg
)) == NULL
) {
337 /* If alg has an OID assume disabled algorithm */
338 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
344 if (!TEST_ptr(mdat
= OPENSSL_zalloc(sizeof(*mdat
))))
347 mdat
->digest
= digest
;
351 static void digest_test_cleanup(EVP_TEST
*t
)
353 DIGEST_DATA
*mdat
= t
->data
;
355 sk_EVP_TEST_BUFFER_pop_free(mdat
->input
, evp_test_buffer_free
);
356 OPENSSL_free(mdat
->output
);
359 static int digest_test_parse(EVP_TEST
*t
,
360 const char *keyword
, const char *value
)
362 DIGEST_DATA
*mdata
= t
->data
;
364 if (strcmp(keyword
, "Input") == 0)
365 return evp_test_buffer_append(value
, &mdata
->input
);
366 if (strcmp(keyword
, "Output") == 0)
367 return parse_bin(value
, &mdata
->output
, &mdata
->output_len
);
368 if (strcmp(keyword
, "Count") == 0)
369 return evp_test_buffer_set_count(value
, mdata
->input
);
370 if (strcmp(keyword
, "Ncopy") == 0)
371 return evp_test_buffer_ncopy(value
, mdata
->input
);
375 static int digest_update_fn(void *ctx
, const unsigned char *buf
, size_t buflen
)
377 return EVP_DigestUpdate(ctx
, buf
, buflen
);
380 static int digest_test_run(EVP_TEST
*t
)
382 DIGEST_DATA
*expected
= t
->data
;
384 unsigned char *got
= NULL
;
385 unsigned int got_len
;
387 t
->err
= "TEST_FAILURE";
388 if (!TEST_ptr(mctx
= EVP_MD_CTX_new()))
391 got
= OPENSSL_malloc(expected
->output_len
> EVP_MAX_MD_SIZE
?
392 expected
->output_len
: EVP_MAX_MD_SIZE
);
396 if (!EVP_DigestInit_ex(mctx
, expected
->digest
, NULL
)) {
397 t
->err
= "DIGESTINIT_ERROR";
400 if (!evp_test_buffer_do(expected
->input
, digest_update_fn
, mctx
)) {
401 t
->err
= "DIGESTUPDATE_ERROR";
405 if (EVP_MD_flags(expected
->digest
) & EVP_MD_FLAG_XOF
) {
406 got_len
= expected
->output_len
;
407 if (!EVP_DigestFinalXOF(mctx
, got
, got_len
)) {
408 t
->err
= "DIGESTFINALXOF_ERROR";
412 if (!EVP_DigestFinal(mctx
, got
, &got_len
)) {
413 t
->err
= "DIGESTFINAL_ERROR";
417 if (!TEST_int_eq(expected
->output_len
, got_len
)) {
418 t
->err
= "DIGEST_LENGTH_MISMATCH";
421 if (!memory_err_compare(t
, "DIGEST_MISMATCH",
422 expected
->output
, expected
->output_len
,
430 EVP_MD_CTX_free(mctx
);
434 static const EVP_TEST_METHOD digest_test_method
= {
447 typedef struct cipher_data_st
{
448 const EVP_CIPHER
*cipher
;
450 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
456 unsigned char *plaintext
;
457 size_t plaintext_len
;
458 unsigned char *ciphertext
;
459 size_t ciphertext_len
;
460 /* GCM, CCM and OCB only */
467 static int cipher_test_init(EVP_TEST
*t
, const char *alg
)
469 const EVP_CIPHER
*cipher
;
473 if ((cipher
= EVP_get_cipherbyname(alg
)) == NULL
) {
474 /* If alg has an OID assume disabled algorithm */
475 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
481 cdat
= OPENSSL_zalloc(sizeof(*cdat
));
482 cdat
->cipher
= cipher
;
484 m
= EVP_CIPHER_mode(cipher
);
485 if (m
== EVP_CIPH_GCM_MODE
486 || m
== EVP_CIPH_OCB_MODE
487 || m
== EVP_CIPH_CCM_MODE
)
489 else if (EVP_CIPHER_flags(cipher
) & EVP_CIPH_FLAG_AEAD_CIPHER
)
498 static void cipher_test_cleanup(EVP_TEST
*t
)
500 CIPHER_DATA
*cdat
= t
->data
;
502 OPENSSL_free(cdat
->key
);
503 OPENSSL_free(cdat
->iv
);
504 OPENSSL_free(cdat
->ciphertext
);
505 OPENSSL_free(cdat
->plaintext
);
506 OPENSSL_free(cdat
->aad
);
507 OPENSSL_free(cdat
->tag
);
510 static int cipher_test_parse(EVP_TEST
*t
, const char *keyword
,
513 CIPHER_DATA
*cdat
= t
->data
;
515 if (strcmp(keyword
, "Key") == 0)
516 return parse_bin(value
, &cdat
->key
, &cdat
->key_len
);
517 if (strcmp(keyword
, "IV") == 0)
518 return parse_bin(value
, &cdat
->iv
, &cdat
->iv_len
);
519 if (strcmp(keyword
, "Plaintext") == 0)
520 return parse_bin(value
, &cdat
->plaintext
, &cdat
->plaintext_len
);
521 if (strcmp(keyword
, "Ciphertext") == 0)
522 return parse_bin(value
, &cdat
->ciphertext
, &cdat
->ciphertext_len
);
524 if (strcmp(keyword
, "AAD") == 0)
525 return parse_bin(value
, &cdat
->aad
, &cdat
->aad_len
);
526 if (strcmp(keyword
, "Tag") == 0)
527 return parse_bin(value
, &cdat
->tag
, &cdat
->tag_len
);
530 if (strcmp(keyword
, "Operation") == 0) {
531 if (strcmp(value
, "ENCRYPT") == 0)
533 else if (strcmp(value
, "DECRYPT") == 0)
542 static int cipher_test_enc(EVP_TEST
*t
, int enc
,
543 size_t out_misalign
, size_t inp_misalign
, int frag
)
545 CIPHER_DATA
*expected
= t
->data
;
546 unsigned char *in
, *expected_out
, *tmp
= NULL
;
547 size_t in_len
, out_len
, donelen
= 0;
548 int ok
= 0, tmplen
, chunklen
, tmpflen
;
549 EVP_CIPHER_CTX
*ctx
= NULL
;
551 t
->err
= "TEST_FAILURE";
552 if (!TEST_ptr(ctx
= EVP_CIPHER_CTX_new()))
554 EVP_CIPHER_CTX_set_flags(ctx
, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
);
556 in
= expected
->plaintext
;
557 in_len
= expected
->plaintext_len
;
558 expected_out
= expected
->ciphertext
;
559 out_len
= expected
->ciphertext_len
;
561 in
= expected
->ciphertext
;
562 in_len
= expected
->ciphertext_len
;
563 expected_out
= expected
->plaintext
;
564 out_len
= expected
->plaintext_len
;
566 if (inp_misalign
== (size_t)-1) {
568 * Exercise in-place encryption
570 tmp
= OPENSSL_malloc(out_misalign
+ in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
);
573 in
= memcpy(tmp
+ out_misalign
, in
, in_len
);
575 inp_misalign
+= 16 - ((out_misalign
+ in_len
) & 15);
577 * 'tmp' will store both output and copy of input. We make the copy
578 * of input to specifically aligned part of 'tmp'. So we just
579 * figured out how much padding would ensure the required alignment,
580 * now we allocate extended buffer and finally copy the input just
581 * past inp_misalign in expression below. Output will be written
582 * past out_misalign...
584 tmp
= OPENSSL_malloc(out_misalign
+ in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
+
585 inp_misalign
+ in_len
);
588 in
= memcpy(tmp
+ out_misalign
+ in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
+
589 inp_misalign
, in
, in_len
);
591 if (!EVP_CipherInit_ex(ctx
, expected
->cipher
, NULL
, NULL
, NULL
, enc
)) {
592 t
->err
= "CIPHERINIT_ERROR";
596 if (expected
->aead
) {
597 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_IVLEN
,
598 expected
->iv_len
, 0)) {
599 t
->err
= "INVALID_IV_LENGTH";
602 } else if (expected
->iv_len
!= (size_t)EVP_CIPHER_CTX_iv_length(ctx
)) {
603 t
->err
= "INVALID_IV_LENGTH";
607 if (expected
->aead
) {
610 * If encrypting or OCB just set tag length initially, otherwise
611 * set tag length and value.
613 if (enc
|| expected
->aead
== EVP_CIPH_OCB_MODE
) {
614 t
->err
= "TAG_LENGTH_SET_ERROR";
617 t
->err
= "TAG_SET_ERROR";
620 if (tag
|| expected
->aead
!= EVP_CIPH_GCM_MODE
) {
621 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_TAG
,
622 expected
->tag_len
, tag
))
627 if (!EVP_CIPHER_CTX_set_key_length(ctx
, expected
->key_len
)) {
628 t
->err
= "INVALID_KEY_LENGTH";
631 if (!EVP_CipherInit_ex(ctx
, NULL
, NULL
, expected
->key
, expected
->iv
, -1)) {
632 t
->err
= "KEY_SET_ERROR";
636 if (!enc
&& expected
->aead
== EVP_CIPH_OCB_MODE
) {
637 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_TAG
,
638 expected
->tag_len
, expected
->tag
)) {
639 t
->err
= "TAG_SET_ERROR";
644 if (expected
->aead
== EVP_CIPH_CCM_MODE
) {
645 if (!EVP_CipherUpdate(ctx
, NULL
, &tmplen
, NULL
, out_len
)) {
646 t
->err
= "CCM_PLAINTEXT_LENGTH_SET_ERROR";
651 t
->err
= "AAD_SET_ERROR";
653 if (!EVP_CipherUpdate(ctx
, NULL
, &chunklen
, expected
->aad
,
658 * Supply the AAD in chunks less than the block size where possible
660 if (expected
->aad_len
> 0) {
661 if (!EVP_CipherUpdate(ctx
, NULL
, &chunklen
, expected
->aad
, 1))
665 if (expected
->aad_len
> 2) {
666 if (!EVP_CipherUpdate(ctx
, NULL
, &chunklen
,
667 expected
->aad
+ donelen
,
668 expected
->aad_len
- 2))
670 donelen
+= expected
->aad_len
- 2;
672 if (expected
->aad_len
> 1
673 && !EVP_CipherUpdate(ctx
, NULL
, &chunklen
,
674 expected
->aad
+ donelen
, 1))
678 EVP_CIPHER_CTX_set_padding(ctx
, 0);
679 t
->err
= "CIPHERUPDATE_ERROR";
682 /* We supply the data all in one go */
683 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
, &tmplen
, in
, in_len
))
686 /* Supply the data in chunks less than the block size where possible */
688 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
, &chunklen
, in
, 1))
695 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
+ tmplen
, &chunklen
,
703 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
+ tmplen
, &chunklen
,
709 if (!EVP_CipherFinal_ex(ctx
, tmp
+ out_misalign
+ tmplen
, &tmpflen
)) {
710 t
->err
= "CIPHERFINAL_ERROR";
713 if (!memory_err_compare(t
, "VALUE_MISMATCH", expected_out
, out_len
,
714 tmp
+ out_misalign
, tmplen
+ tmpflen
))
716 if (enc
&& expected
->aead
) {
717 unsigned char rtag
[16];
719 if (!TEST_size_t_le(expected
->tag_len
, sizeof(rtag
))) {
720 t
->err
= "TAG_LENGTH_INTERNAL_ERROR";
723 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_GET_TAG
,
724 expected
->tag_len
, rtag
)) {
725 t
->err
= "TAG_RETRIEVE_ERROR";
728 if (!memory_err_compare(t
, "TAG_VALUE_MISMATCH",
729 expected
->tag
, expected
->tag_len
,
730 rtag
, expected
->tag_len
))
737 EVP_CIPHER_CTX_free(ctx
);
741 static int cipher_test_run(EVP_TEST
*t
)
743 CIPHER_DATA
*cdat
= t
->data
;
745 size_t out_misalign
, inp_misalign
;
751 if (!cdat
->iv
&& EVP_CIPHER_iv_length(cdat
->cipher
)) {
752 /* IV is optional and usually omitted in wrap mode */
753 if (EVP_CIPHER_mode(cdat
->cipher
) != EVP_CIPH_WRAP_MODE
) {
758 if (cdat
->aead
&& !cdat
->tag
) {
762 for (out_misalign
= 0; out_misalign
<= 1;) {
763 static char aux_err
[64];
764 t
->aux_err
= aux_err
;
765 for (inp_misalign
= (size_t)-1; inp_misalign
!= 2; inp_misalign
++) {
766 if (inp_misalign
== (size_t)-1) {
767 /* kludge: inp_misalign == -1 means "exercise in-place" */
768 BIO_snprintf(aux_err
, sizeof(aux_err
),
769 "%s in-place, %sfragmented",
770 out_misalign
? "misaligned" : "aligned",
773 BIO_snprintf(aux_err
, sizeof(aux_err
),
774 "%s output and %s input, %sfragmented",
775 out_misalign
? "misaligned" : "aligned",
776 inp_misalign
? "misaligned" : "aligned",
780 rv
= cipher_test_enc(t
, 1, out_misalign
, inp_misalign
, frag
);
781 /* Not fatal errors: return */
788 if (cdat
->enc
!= 1) {
789 rv
= cipher_test_enc(t
, 0, out_misalign
, inp_misalign
, frag
);
790 /* Not fatal errors: return */
799 if (out_misalign
== 1 && frag
== 0) {
801 * XTS, CCM and Wrap modes have special requirements about input
802 * lengths so we don't fragment for those
804 if (cdat
->aead
== EVP_CIPH_CCM_MODE
805 || EVP_CIPHER_mode(cdat
->cipher
) == EVP_CIPH_XTS_MODE
806 || EVP_CIPHER_mode(cdat
->cipher
) == EVP_CIPH_WRAP_MODE
)
819 static const EVP_TEST_METHOD cipher_test_method
= {
832 typedef struct mac_data_st
{
833 /* MAC type in one form or another */
834 const EVP_MAC
*mac
; /* for mac_test_run_mac */
835 int type
; /* for mac_test_run_pkey */
836 /* Algorithm string for this MAC */
845 unsigned char *input
;
847 /* Expected output */
848 unsigned char *output
;
850 /* Collection of controls */
851 STACK_OF(OPENSSL_STRING
) *controls
;
854 static int mac_test_init(EVP_TEST
*t
, const char *alg
)
856 const EVP_MAC
*mac
= NULL
;
857 int type
= NID_undef
;
860 if ((mac
= EVP_get_macbyname(alg
)) == NULL
) {
862 * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods
863 * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running
864 * the EVP_PKEY method.
866 size_t sz
= strlen(alg
);
867 static const char epilogue
[] = " by EVP_PKEY";
869 if (sz
>= sizeof(epilogue
)
870 && strcmp(alg
+ sz
- (sizeof(epilogue
) - 1), epilogue
) == 0)
871 sz
-= sizeof(epilogue
) - 1;
873 if (strncmp(alg
, "HMAC", sz
) == 0) {
874 type
= EVP_PKEY_HMAC
;
875 } else if (strncmp(alg
, "CMAC", sz
) == 0) {
876 #ifndef OPENSSL_NO_CMAC
877 type
= EVP_PKEY_CMAC
;
882 } else if (strncmp(alg
, "Poly1305", sz
) == 0) {
883 #ifndef OPENSSL_NO_POLY1305
884 type
= EVP_PKEY_POLY1305
;
889 } else if (strncmp(alg
, "SipHash", sz
) == 0) {
890 #ifndef OPENSSL_NO_SIPHASH
891 type
= EVP_PKEY_SIPHASH
;
898 * Not a known EVP_PKEY method either. If it's a known OID, then
899 * assume it's been disabled.
901 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
910 mdat
= OPENSSL_zalloc(sizeof(*mdat
));
913 mdat
->controls
= sk_OPENSSL_STRING_new_null();
918 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
919 static void openssl_free(char *m
)
924 static void mac_test_cleanup(EVP_TEST
*t
)
926 MAC_DATA
*mdat
= t
->data
;
928 sk_OPENSSL_STRING_pop_free(mdat
->controls
, openssl_free
);
929 OPENSSL_free(mdat
->alg
);
930 OPENSSL_free(mdat
->key
);
931 OPENSSL_free(mdat
->iv
);
932 OPENSSL_free(mdat
->input
);
933 OPENSSL_free(mdat
->output
);
936 static int mac_test_parse(EVP_TEST
*t
,
937 const char *keyword
, const char *value
)
939 MAC_DATA
*mdata
= t
->data
;
941 if (strcmp(keyword
, "Key") == 0)
942 return parse_bin(value
, &mdata
->key
, &mdata
->key_len
);
943 if (strcmp(keyword
, "IV") == 0)
944 return parse_bin(value
, &mdata
->iv
, &mdata
->iv_len
);
945 if (strcmp(keyword
, "Algorithm") == 0) {
946 mdata
->alg
= OPENSSL_strdup(value
);
951 if (strcmp(keyword
, "Input") == 0)
952 return parse_bin(value
, &mdata
->input
, &mdata
->input_len
);
953 if (strcmp(keyword
, "Output") == 0)
954 return parse_bin(value
, &mdata
->output
, &mdata
->output_len
);
955 if (strcmp(keyword
, "Ctrl") == 0)
956 return sk_OPENSSL_STRING_push(mdata
->controls
,
957 OPENSSL_strdup(value
)) != 0;
961 static int mac_test_ctrl_pkey(EVP_TEST
*t
, EVP_PKEY_CTX
*pctx
,
967 if (!TEST_ptr(tmpval
= OPENSSL_strdup(value
)))
969 p
= strchr(tmpval
, ':');
972 rv
= EVP_PKEY_CTX_ctrl_str(pctx
, tmpval
, p
);
974 t
->err
= "PKEY_CTRL_INVALID";
976 t
->err
= "PKEY_CTRL_ERROR";
979 OPENSSL_free(tmpval
);
983 static int mac_test_run_pkey(EVP_TEST
*t
)
985 MAC_DATA
*expected
= t
->data
;
986 EVP_MD_CTX
*mctx
= NULL
;
987 EVP_PKEY_CTX
*pctx
= NULL
, *genctx
= NULL
;
988 EVP_PKEY
*key
= NULL
;
989 const EVP_MD
*md
= NULL
;
990 unsigned char *got
= NULL
;
994 if (expected
->alg
== NULL
)
995 TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected
->type
));
997 TEST_info("Trying the EVP_PKEY %s test with %s",
998 OBJ_nid2sn(expected
->type
), expected
->alg
);
1000 #ifdef OPENSSL_NO_DES
1001 if (expected
->alg
!= NULL
&& strstr(expected
->alg
, "DES") != NULL
) {
1008 if (expected
->type
== EVP_PKEY_CMAC
)
1009 key
= EVP_PKEY_new_CMAC_key(NULL
, expected
->key
, expected
->key_len
,
1010 EVP_get_cipherbyname(expected
->alg
));
1012 key
= EVP_PKEY_new_raw_private_key(expected
->type
, NULL
, expected
->key
,
1015 t
->err
= "MAC_KEY_CREATE_ERROR";
1019 if (expected
->type
== EVP_PKEY_HMAC
) {
1020 if (!TEST_ptr(md
= EVP_get_digestbyname(expected
->alg
))) {
1021 t
->err
= "MAC_ALGORITHM_SET_ERROR";
1025 if (!TEST_ptr(mctx
= EVP_MD_CTX_new())) {
1026 t
->err
= "INTERNAL_ERROR";
1029 if (!EVP_DigestSignInit(mctx
, &pctx
, md
, NULL
, key
)) {
1030 t
->err
= "DIGESTSIGNINIT_ERROR";
1033 for (i
= 0; i
< sk_OPENSSL_STRING_num(expected
->controls
); i
++)
1034 if (!mac_test_ctrl_pkey(t
, pctx
,
1035 sk_OPENSSL_STRING_value(expected
->controls
,
1037 t
->err
= "EVPPKEYCTXCTRL_ERROR";
1040 if (!EVP_DigestSignUpdate(mctx
, expected
->input
, expected
->input_len
)) {
1041 t
->err
= "DIGESTSIGNUPDATE_ERROR";
1044 if (!EVP_DigestSignFinal(mctx
, NULL
, &got_len
)) {
1045 t
->err
= "DIGESTSIGNFINAL_LENGTH_ERROR";
1048 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1049 t
->err
= "TEST_FAILURE";
1052 if (!EVP_DigestSignFinal(mctx
, got
, &got_len
)
1053 || !memory_err_compare(t
, "TEST_MAC_ERR",
1054 expected
->output
, expected
->output_len
,
1056 t
->err
= "TEST_MAC_ERR";
1061 EVP_MD_CTX_free(mctx
);
1063 EVP_PKEY_CTX_free(genctx
);
1068 static int mac_test_run_mac(EVP_TEST
*t
)
1070 MAC_DATA
*expected
= t
->data
;
1071 EVP_MAC_CTX
*ctx
= NULL
;
1072 const void *algo
= NULL
;
1074 unsigned char *got
= NULL
;
1078 if (expected
->alg
== NULL
)
1079 TEST_info("Trying the EVP_MAC %s test", EVP_MAC_name(expected
->mac
));
1081 TEST_info("Trying the EVP_MAC %s test with %s",
1082 EVP_MAC_name(expected
->mac
), expected
->alg
);
1084 #ifdef OPENSSL_NO_DES
1085 if (expected
->alg
!= NULL
&& strstr(expected
->alg
, "DES") != NULL
) {
1092 if ((ctx
= EVP_MAC_CTX_new(expected
->mac
)) == NULL
) {
1093 t
->err
= "MAC_CREATE_ERROR";
1097 if (expected
->alg
!= NULL
1098 && ((algo_ctrl
= EVP_MAC_CTRL_SET_CIPHER
,
1099 algo
= EVP_get_cipherbyname(expected
->alg
)) == NULL
1100 && (algo_ctrl
= EVP_MAC_CTRL_SET_MD
,
1101 algo
= EVP_get_digestbyname(expected
->alg
)) == NULL
)) {
1102 t
->err
= "MAC_BAD_ALGORITHM";
1107 if (algo_ctrl
!= 0) {
1108 rv
= EVP_MAC_ctrl(ctx
, algo_ctrl
, algo
);
1110 t
->err
= "MAC_CTRL_INVALID";
1112 } else if (rv
<= 0) {
1113 t
->err
= "MAC_CTRL_ERROR";
1118 rv
= EVP_MAC_ctrl(ctx
, EVP_MAC_CTRL_SET_KEY
,
1119 expected
->key
, expected
->key_len
);
1121 t
->err
= "MAC_CTRL_INVALID";
1123 } else if (rv
<= 0) {
1124 t
->err
= "MAC_CTRL_ERROR";
1128 if (expected
->iv
!= NULL
) {
1129 rv
= EVP_MAC_ctrl(ctx
, EVP_MAC_CTRL_SET_IV
,
1130 expected
->iv
, expected
->iv_len
);
1132 t
->err
= "MAC_CTRL_INVALID";
1134 } else if (rv
<= 0) {
1135 t
->err
= "MAC_CTRL_ERROR";
1140 if (!EVP_MAC_init(ctx
)) {
1141 t
->err
= "MAC_INIT_ERROR";
1144 for (i
= 0; i
< sk_OPENSSL_STRING_num(expected
->controls
); i
++) {
1146 char *value
= sk_OPENSSL_STRING_value(expected
->controls
, i
);
1148 if (!TEST_ptr(tmpval
= OPENSSL_strdup(value
))) {
1149 t
->err
= "MAC_CTRL_ERROR";
1152 p
= strchr(tmpval
, ':');
1155 rv
= EVP_MAC_ctrl_str(ctx
, tmpval
, p
);
1156 OPENSSL_free(tmpval
);
1158 t
->err
= "MAC_CTRL_INVALID";
1160 } else if (rv
<= 0) {
1161 t
->err
= "MAC_CTRL_ERROR";
1165 if (!EVP_MAC_update(ctx
, expected
->input
, expected
->input_len
)) {
1166 t
->err
= "MAC_UPDATE_ERROR";
1169 if (!EVP_MAC_final(ctx
, NULL
, &got_len
)) {
1170 t
->err
= "MAC_FINAL_LENGTH_ERROR";
1173 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1174 t
->err
= "TEST_FAILURE";
1177 if (!EVP_MAC_final(ctx
, got
, &got_len
)
1178 || !memory_err_compare(t
, "TEST_MAC_ERR",
1179 expected
->output
, expected
->output_len
,
1181 t
->err
= "TEST_MAC_ERR";
1186 EVP_MAC_CTX_free(ctx
);
1191 static int mac_test_run(EVP_TEST
*t
)
1193 MAC_DATA
*expected
= t
->data
;
1195 if (expected
->mac
!= NULL
)
1196 return mac_test_run_mac(t
);
1197 return mac_test_run_pkey(t
);
1200 static const EVP_TEST_METHOD mac_test_method
= {
1210 *** PUBLIC KEY TESTS
1211 *** These are all very similar and share much common code.
1214 typedef struct pkey_data_st
{
1215 /* Context for this operation */
1217 /* Key operation to perform */
1218 int (*keyop
) (EVP_PKEY_CTX
*ctx
,
1219 unsigned char *sig
, size_t *siglen
,
1220 const unsigned char *tbs
, size_t tbslen
);
1222 unsigned char *input
;
1224 /* Expected output */
1225 unsigned char *output
;
1230 * Perform public key operation setup: lookup key, allocated ctx and call
1231 * the appropriate initialisation function
1233 static int pkey_test_init(EVP_TEST
*t
, const char *name
,
1235 int (*keyopinit
) (EVP_PKEY_CTX
*ctx
),
1236 int (*keyop
)(EVP_PKEY_CTX
*ctx
,
1237 unsigned char *sig
, size_t *siglen
,
1238 const unsigned char *tbs
,
1242 EVP_PKEY
*pkey
= NULL
;
1246 rv
= find_key(&pkey
, name
, public_keys
);
1248 rv
= find_key(&pkey
, name
, private_keys
);
1249 if (rv
== 0 || pkey
== NULL
) {
1254 if (!TEST_ptr(kdata
= OPENSSL_zalloc(sizeof(*kdata
)))) {
1255 EVP_PKEY_free(pkey
);
1258 kdata
->keyop
= keyop
;
1259 if (!TEST_ptr(kdata
->ctx
= EVP_PKEY_CTX_new(pkey
, NULL
))) {
1260 EVP_PKEY_free(pkey
);
1261 OPENSSL_free(kdata
);
1264 if (keyopinit(kdata
->ctx
) <= 0)
1265 t
->err
= "KEYOP_INIT_ERROR";
1270 static void pkey_test_cleanup(EVP_TEST
*t
)
1272 PKEY_DATA
*kdata
= t
->data
;
1274 OPENSSL_free(kdata
->input
);
1275 OPENSSL_free(kdata
->output
);
1276 EVP_PKEY_CTX_free(kdata
->ctx
);
1279 static int pkey_test_ctrl(EVP_TEST
*t
, EVP_PKEY_CTX
*pctx
,
1285 if (!TEST_ptr(tmpval
= OPENSSL_strdup(value
)))
1287 p
= strchr(tmpval
, ':');
1290 rv
= EVP_PKEY_CTX_ctrl_str(pctx
, tmpval
, p
);
1292 t
->err
= "PKEY_CTRL_INVALID";
1294 } else if (p
!= NULL
&& rv
<= 0) {
1295 /* If p has an OID and lookup fails assume disabled algorithm */
1296 int nid
= OBJ_sn2nid(p
);
1298 if (nid
== NID_undef
)
1299 nid
= OBJ_ln2nid(p
);
1300 if (nid
!= NID_undef
1301 && EVP_get_digestbynid(nid
) == NULL
1302 && EVP_get_cipherbynid(nid
) == NULL
) {
1306 t
->err
= "PKEY_CTRL_ERROR";
1310 OPENSSL_free(tmpval
);
1314 static int pkey_test_parse(EVP_TEST
*t
,
1315 const char *keyword
, const char *value
)
1317 PKEY_DATA
*kdata
= t
->data
;
1318 if (strcmp(keyword
, "Input") == 0)
1319 return parse_bin(value
, &kdata
->input
, &kdata
->input_len
);
1320 if (strcmp(keyword
, "Output") == 0)
1321 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
1322 if (strcmp(keyword
, "Ctrl") == 0)
1323 return pkey_test_ctrl(t
, kdata
->ctx
, value
);
1327 static int pkey_test_run(EVP_TEST
*t
)
1329 PKEY_DATA
*expected
= t
->data
;
1330 unsigned char *got
= NULL
;
1333 if (expected
->keyop(expected
->ctx
, NULL
, &got_len
,
1334 expected
->input
, expected
->input_len
) <= 0
1335 || !TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1336 t
->err
= "KEYOP_LENGTH_ERROR";
1339 if (expected
->keyop(expected
->ctx
, got
, &got_len
,
1340 expected
->input
, expected
->input_len
) <= 0) {
1341 t
->err
= "KEYOP_ERROR";
1344 if (!memory_err_compare(t
, "KEYOP_MISMATCH",
1345 expected
->output
, expected
->output_len
,
1355 static int sign_test_init(EVP_TEST
*t
, const char *name
)
1357 return pkey_test_init(t
, name
, 0, EVP_PKEY_sign_init
, EVP_PKEY_sign
);
1360 static const EVP_TEST_METHOD psign_test_method
= {
1368 static int verify_recover_test_init(EVP_TEST
*t
, const char *name
)
1370 return pkey_test_init(t
, name
, 1, EVP_PKEY_verify_recover_init
,
1371 EVP_PKEY_verify_recover
);
1374 static const EVP_TEST_METHOD pverify_recover_test_method
= {
1376 verify_recover_test_init
,
1382 static int decrypt_test_init(EVP_TEST
*t
, const char *name
)
1384 return pkey_test_init(t
, name
, 0, EVP_PKEY_decrypt_init
,
1388 static const EVP_TEST_METHOD pdecrypt_test_method
= {
1396 static int verify_test_init(EVP_TEST
*t
, const char *name
)
1398 return pkey_test_init(t
, name
, 1, EVP_PKEY_verify_init
, 0);
1401 static int verify_test_run(EVP_TEST
*t
)
1403 PKEY_DATA
*kdata
= t
->data
;
1405 if (EVP_PKEY_verify(kdata
->ctx
, kdata
->output
, kdata
->output_len
,
1406 kdata
->input
, kdata
->input_len
) <= 0)
1407 t
->err
= "VERIFY_ERROR";
1411 static const EVP_TEST_METHOD pverify_test_method
= {
1420 static int pderive_test_init(EVP_TEST
*t
, const char *name
)
1422 return pkey_test_init(t
, name
, 0, EVP_PKEY_derive_init
, 0);
1425 static int pderive_test_parse(EVP_TEST
*t
,
1426 const char *keyword
, const char *value
)
1428 PKEY_DATA
*kdata
= t
->data
;
1430 if (strcmp(keyword
, "PeerKey") == 0) {
1432 if (find_key(&peer
, value
, public_keys
) == 0)
1434 if (EVP_PKEY_derive_set_peer(kdata
->ctx
, peer
) <= 0)
1438 if (strcmp(keyword
, "SharedSecret") == 0)
1439 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
1440 if (strcmp(keyword
, "Ctrl") == 0)
1441 return pkey_test_ctrl(t
, kdata
->ctx
, value
);
1445 static int pderive_test_run(EVP_TEST
*t
)
1447 PKEY_DATA
*expected
= t
->data
;
1448 unsigned char *got
= NULL
;
1451 if (EVP_PKEY_derive(expected
->ctx
, NULL
, &got_len
) <= 0) {
1452 t
->err
= "DERIVE_ERROR";
1455 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1456 t
->err
= "DERIVE_ERROR";
1459 if (EVP_PKEY_derive(expected
->ctx
, got
, &got_len
) <= 0) {
1460 t
->err
= "DERIVE_ERROR";
1463 if (!memory_err_compare(t
, "SHARED_SECRET_MISMATCH",
1464 expected
->output
, expected
->output_len
,
1474 static const EVP_TEST_METHOD pderive_test_method
= {
1487 typedef enum pbe_type_enum
{
1488 PBE_TYPE_INVALID
= 0,
1489 PBE_TYPE_SCRYPT
, PBE_TYPE_PBKDF2
, PBE_TYPE_PKCS12
1492 typedef struct pbe_data_st
{
1494 /* scrypt parameters */
1495 uint64_t N
, r
, p
, maxmem
;
1496 /* PKCS#12 parameters */
1500 unsigned char *pass
;
1503 unsigned char *salt
;
1505 /* Expected output */
1510 #ifndef OPENSSL_NO_SCRYPT
1512 * Parse unsigned decimal 64 bit integer value
1514 static int parse_uint64(const char *value
, uint64_t *pr
)
1516 const char *p
= value
;
1518 if (!TEST_true(*p
)) {
1519 TEST_info("Invalid empty integer value");
1522 for (*pr
= 0; *p
; ) {
1523 if (*pr
> UINT64_MAX
/ 10) {
1524 TEST_error("Integer overflow in string %s", value
);
1528 if (!TEST_true(isdigit((unsigned char)*p
))) {
1529 TEST_error("Invalid character in string %s", value
);
1538 static int scrypt_test_parse(EVP_TEST
*t
,
1539 const char *keyword
, const char *value
)
1541 PBE_DATA
*pdata
= t
->data
;
1543 if (strcmp(keyword
, "N") == 0)
1544 return parse_uint64(value
, &pdata
->N
);
1545 if (strcmp(keyword
, "p") == 0)
1546 return parse_uint64(value
, &pdata
->p
);
1547 if (strcmp(keyword
, "r") == 0)
1548 return parse_uint64(value
, &pdata
->r
);
1549 if (strcmp(keyword
, "maxmem") == 0)
1550 return parse_uint64(value
, &pdata
->maxmem
);
1555 static int pbkdf2_test_parse(EVP_TEST
*t
,
1556 const char *keyword
, const char *value
)
1558 PBE_DATA
*pdata
= t
->data
;
1560 if (strcmp(keyword
, "iter") == 0) {
1561 pdata
->iter
= atoi(value
);
1562 if (pdata
->iter
<= 0)
1566 if (strcmp(keyword
, "MD") == 0) {
1567 pdata
->md
= EVP_get_digestbyname(value
);
1568 if (pdata
->md
== NULL
)
1575 static int pkcs12_test_parse(EVP_TEST
*t
,
1576 const char *keyword
, const char *value
)
1578 PBE_DATA
*pdata
= t
->data
;
1580 if (strcmp(keyword
, "id") == 0) {
1581 pdata
->id
= atoi(value
);
1586 return pbkdf2_test_parse(t
, keyword
, value
);
1589 static int pbe_test_init(EVP_TEST
*t
, const char *alg
)
1592 PBE_TYPE pbe_type
= PBE_TYPE_INVALID
;
1594 if (strcmp(alg
, "scrypt") == 0) {
1595 #ifndef OPENSSL_NO_SCRYPT
1596 pbe_type
= PBE_TYPE_SCRYPT
;
1601 } else if (strcmp(alg
, "pbkdf2") == 0) {
1602 pbe_type
= PBE_TYPE_PBKDF2
;
1603 } else if (strcmp(alg
, "pkcs12") == 0) {
1604 pbe_type
= PBE_TYPE_PKCS12
;
1606 TEST_error("Unknown pbe algorithm %s", alg
);
1608 pdat
= OPENSSL_zalloc(sizeof(*pdat
));
1609 pdat
->pbe_type
= pbe_type
;
1614 static void pbe_test_cleanup(EVP_TEST
*t
)
1616 PBE_DATA
*pdat
= t
->data
;
1618 OPENSSL_free(pdat
->pass
);
1619 OPENSSL_free(pdat
->salt
);
1620 OPENSSL_free(pdat
->key
);
1623 static int pbe_test_parse(EVP_TEST
*t
,
1624 const char *keyword
, const char *value
)
1626 PBE_DATA
*pdata
= t
->data
;
1628 if (strcmp(keyword
, "Password") == 0)
1629 return parse_bin(value
, &pdata
->pass
, &pdata
->pass_len
);
1630 if (strcmp(keyword
, "Salt") == 0)
1631 return parse_bin(value
, &pdata
->salt
, &pdata
->salt_len
);
1632 if (strcmp(keyword
, "Key") == 0)
1633 return parse_bin(value
, &pdata
->key
, &pdata
->key_len
);
1634 if (pdata
->pbe_type
== PBE_TYPE_PBKDF2
)
1635 return pbkdf2_test_parse(t
, keyword
, value
);
1636 else if (pdata
->pbe_type
== PBE_TYPE_PKCS12
)
1637 return pkcs12_test_parse(t
, keyword
, value
);
1638 #ifndef OPENSSL_NO_SCRYPT
1639 else if (pdata
->pbe_type
== PBE_TYPE_SCRYPT
)
1640 return scrypt_test_parse(t
, keyword
, value
);
1645 static int pbe_test_run(EVP_TEST
*t
)
1647 PBE_DATA
*expected
= t
->data
;
1650 if (!TEST_ptr(key
= OPENSSL_malloc(expected
->key_len
))) {
1651 t
->err
= "INTERNAL_ERROR";
1654 if (expected
->pbe_type
== PBE_TYPE_PBKDF2
) {
1655 if (PKCS5_PBKDF2_HMAC((char *)expected
->pass
, expected
->pass_len
,
1656 expected
->salt
, expected
->salt_len
,
1657 expected
->iter
, expected
->md
,
1658 expected
->key_len
, key
) == 0) {
1659 t
->err
= "PBKDF2_ERROR";
1662 #ifndef OPENSSL_NO_SCRYPT
1663 } else if (expected
->pbe_type
== PBE_TYPE_SCRYPT
) {
1664 if (EVP_PBE_scrypt((const char *)expected
->pass
, expected
->pass_len
,
1665 expected
->salt
, expected
->salt_len
, expected
->N
,
1666 expected
->r
, expected
->p
, expected
->maxmem
,
1667 key
, expected
->key_len
) == 0) {
1668 t
->err
= "SCRYPT_ERROR";
1672 } else if (expected
->pbe_type
== PBE_TYPE_PKCS12
) {
1673 if (PKCS12_key_gen_uni(expected
->pass
, expected
->pass_len
,
1674 expected
->salt
, expected
->salt_len
,
1675 expected
->id
, expected
->iter
, expected
->key_len
,
1676 key
, expected
->md
) == 0) {
1677 t
->err
= "PKCS12_ERROR";
1681 if (!memory_err_compare(t
, "KEY_MISMATCH", expected
->key
, expected
->key_len
,
1682 key
, expected
->key_len
))
1691 static const EVP_TEST_METHOD pbe_test_method
= {
1705 BASE64_CANONICAL_ENCODING
= 0,
1706 BASE64_VALID_ENCODING
= 1,
1707 BASE64_INVALID_ENCODING
= 2
1708 } base64_encoding_type
;
1710 typedef struct encode_data_st
{
1711 /* Input to encoding */
1712 unsigned char *input
;
1714 /* Expected output */
1715 unsigned char *output
;
1717 base64_encoding_type encoding
;
1720 static int encode_test_init(EVP_TEST
*t
, const char *encoding
)
1724 if (!TEST_ptr(edata
= OPENSSL_zalloc(sizeof(*edata
))))
1726 if (strcmp(encoding
, "canonical") == 0) {
1727 edata
->encoding
= BASE64_CANONICAL_ENCODING
;
1728 } else if (strcmp(encoding
, "valid") == 0) {
1729 edata
->encoding
= BASE64_VALID_ENCODING
;
1730 } else if (strcmp(encoding
, "invalid") == 0) {
1731 edata
->encoding
= BASE64_INVALID_ENCODING
;
1732 if (!TEST_ptr(t
->expected_err
= OPENSSL_strdup("DECODE_ERROR")))
1735 TEST_error("Bad encoding: %s."
1736 " Should be one of {canonical, valid, invalid}",
1744 static void encode_test_cleanup(EVP_TEST
*t
)
1746 ENCODE_DATA
*edata
= t
->data
;
1748 OPENSSL_free(edata
->input
);
1749 OPENSSL_free(edata
->output
);
1750 memset(edata
, 0, sizeof(*edata
));
1753 static int encode_test_parse(EVP_TEST
*t
,
1754 const char *keyword
, const char *value
)
1756 ENCODE_DATA
*edata
= t
->data
;
1758 if (strcmp(keyword
, "Input") == 0)
1759 return parse_bin(value
, &edata
->input
, &edata
->input_len
);
1760 if (strcmp(keyword
, "Output") == 0)
1761 return parse_bin(value
, &edata
->output
, &edata
->output_len
);
1765 static int encode_test_run(EVP_TEST
*t
)
1767 ENCODE_DATA
*expected
= t
->data
;
1768 unsigned char *encode_out
= NULL
, *decode_out
= NULL
;
1769 int output_len
, chunk_len
;
1770 EVP_ENCODE_CTX
*decode_ctx
;
1772 if (!TEST_ptr(decode_ctx
= EVP_ENCODE_CTX_new())) {
1773 t
->err
= "INTERNAL_ERROR";
1777 if (expected
->encoding
== BASE64_CANONICAL_ENCODING
) {
1778 EVP_ENCODE_CTX
*encode_ctx
;
1780 if (!TEST_ptr(encode_ctx
= EVP_ENCODE_CTX_new())
1781 || !TEST_ptr(encode_out
=
1782 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected
->input_len
))))
1785 EVP_EncodeInit(encode_ctx
);
1786 EVP_EncodeUpdate(encode_ctx
, encode_out
, &chunk_len
,
1787 expected
->input
, expected
->input_len
);
1788 output_len
= chunk_len
;
1790 EVP_EncodeFinal(encode_ctx
, encode_out
+ chunk_len
, &chunk_len
);
1791 output_len
+= chunk_len
;
1793 EVP_ENCODE_CTX_free(encode_ctx
);
1795 if (!memory_err_compare(t
, "BAD_ENCODING",
1796 expected
->output
, expected
->output_len
,
1797 encode_out
, output_len
))
1801 if (!TEST_ptr(decode_out
=
1802 OPENSSL_malloc(EVP_DECODE_LENGTH(expected
->output_len
))))
1805 EVP_DecodeInit(decode_ctx
);
1806 if (EVP_DecodeUpdate(decode_ctx
, decode_out
, &chunk_len
, expected
->output
,
1807 expected
->output_len
) < 0) {
1808 t
->err
= "DECODE_ERROR";
1811 output_len
= chunk_len
;
1813 if (EVP_DecodeFinal(decode_ctx
, decode_out
+ chunk_len
, &chunk_len
) != 1) {
1814 t
->err
= "DECODE_ERROR";
1817 output_len
+= chunk_len
;
1819 if (expected
->encoding
!= BASE64_INVALID_ENCODING
1820 && !memory_err_compare(t
, "BAD_DECODING",
1821 expected
->input
, expected
->input_len
,
1822 decode_out
, output_len
)) {
1823 t
->err
= "BAD_DECODING";
1829 OPENSSL_free(encode_out
);
1830 OPENSSL_free(decode_out
);
1831 EVP_ENCODE_CTX_free(decode_ctx
);
1835 static const EVP_TEST_METHOD encode_test_method
= {
1838 encode_test_cleanup
,
1847 typedef struct kdf_data_st
{
1848 /* Context for this operation */
1850 /* Expected output */
1851 unsigned char *output
;
1856 * Perform public key operation setup: lookup key, allocated ctx and call
1857 * the appropriate initialisation function
1859 static int kdf_test_init(EVP_TEST
*t
, const char *name
)
1862 int kdf_nid
= OBJ_sn2nid(name
);
1864 #ifdef OPENSSL_NO_SCRYPT
1865 if (strcmp(name
, "scrypt") == 0) {
1871 if (kdf_nid
== NID_undef
)
1872 kdf_nid
= OBJ_ln2nid(name
);
1874 if (!TEST_ptr(kdata
= OPENSSL_zalloc(sizeof(*kdata
))))
1876 kdata
->ctx
= EVP_PKEY_CTX_new_id(kdf_nid
, NULL
);
1877 if (kdata
->ctx
== NULL
) {
1878 OPENSSL_free(kdata
);
1881 if (EVP_PKEY_derive_init(kdata
->ctx
) <= 0) {
1882 EVP_PKEY_CTX_free(kdata
->ctx
);
1883 OPENSSL_free(kdata
);
1890 static void kdf_test_cleanup(EVP_TEST
*t
)
1892 KDF_DATA
*kdata
= t
->data
;
1893 OPENSSL_free(kdata
->output
);
1894 EVP_PKEY_CTX_free(kdata
->ctx
);
1897 static int kdf_test_parse(EVP_TEST
*t
,
1898 const char *keyword
, const char *value
)
1900 KDF_DATA
*kdata
= t
->data
;
1902 if (strcmp(keyword
, "Output") == 0)
1903 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
1904 if (strncmp(keyword
, "Ctrl", 4) == 0)
1905 return pkey_test_ctrl(t
, kdata
->ctx
, value
);
1909 static int kdf_test_run(EVP_TEST
*t
)
1911 KDF_DATA
*expected
= t
->data
;
1912 unsigned char *got
= NULL
;
1913 size_t got_len
= expected
->output_len
;
1915 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1916 t
->err
= "INTERNAL_ERROR";
1919 if (EVP_PKEY_derive(expected
->ctx
, got
, &got_len
) <= 0) {
1920 t
->err
= "KDF_DERIVE_ERROR";
1923 if (!memory_err_compare(t
, "KDF_MISMATCH",
1924 expected
->output
, expected
->output_len
,
1935 static const EVP_TEST_METHOD kdf_test_method
= {
1948 typedef struct keypair_test_data_st
{
1951 } KEYPAIR_TEST_DATA
;
1953 static int keypair_test_init(EVP_TEST
*t
, const char *pair
)
1955 KEYPAIR_TEST_DATA
*data
;
1957 EVP_PKEY
*pk
= NULL
, *pubk
= NULL
;
1958 char *pub
, *priv
= NULL
;
1960 /* Split private and public names. */
1961 if (!TEST_ptr(priv
= OPENSSL_strdup(pair
))
1962 || !TEST_ptr(pub
= strchr(priv
, ':'))) {
1963 t
->err
= "PARSING_ERROR";
1968 if (!TEST_true(find_key(&pk
, priv
, private_keys
))) {
1969 TEST_info("Can't find private key: %s", priv
);
1970 t
->err
= "MISSING_PRIVATE_KEY";
1973 if (!TEST_true(find_key(&pubk
, pub
, public_keys
))) {
1974 TEST_info("Can't find public key: %s", pub
);
1975 t
->err
= "MISSING_PUBLIC_KEY";
1979 if (pk
== NULL
&& pubk
== NULL
) {
1980 /* Both keys are listed but unsupported: skip this test */
1986 if (!TEST_ptr(data
= OPENSSL_malloc(sizeof(*data
))))
1999 static void keypair_test_cleanup(EVP_TEST
*t
)
2001 OPENSSL_free(t
->data
);
2006 * For tests that do not accept any custom keywords.
2008 static int void_test_parse(EVP_TEST
*t
, const char *keyword
, const char *value
)
2013 static int keypair_test_run(EVP_TEST
*t
)
2016 const KEYPAIR_TEST_DATA
*pair
= t
->data
;
2018 if (pair
->privk
== NULL
|| pair
->pubk
== NULL
) {
2020 * this can only happen if only one of the keys is not set
2021 * which means that one of them was unsupported while the
2022 * other isn't: hence a key type mismatch.
2024 t
->err
= "KEYPAIR_TYPE_MISMATCH";
2029 if ((rv
= EVP_PKEY_cmp(pair
->privk
, pair
->pubk
)) != 1 ) {
2031 t
->err
= "KEYPAIR_MISMATCH";
2032 } else if ( -1 == rv
) {
2033 t
->err
= "KEYPAIR_TYPE_MISMATCH";
2034 } else if ( -2 == rv
) {
2035 t
->err
= "UNSUPPORTED_KEY_COMPARISON";
2037 TEST_error("Unexpected error in key comparison");
2052 static const EVP_TEST_METHOD keypair_test_method
= {
2055 keypair_test_cleanup
,
2064 typedef struct keygen_test_data_st
{
2065 EVP_PKEY_CTX
*genctx
; /* Keygen context to use */
2066 char *keyname
; /* Key name to store key or NULL */
2069 static int keygen_test_init(EVP_TEST
*t
, const char *alg
)
2071 KEYGEN_TEST_DATA
*data
;
2072 EVP_PKEY_CTX
*genctx
;
2073 int nid
= OBJ_sn2nid(alg
);
2075 if (nid
== NID_undef
) {
2076 nid
= OBJ_ln2nid(alg
);
2077 if (nid
== NID_undef
)
2081 if (!TEST_ptr(genctx
= EVP_PKEY_CTX_new_id(nid
, NULL
))) {
2082 /* assume algorithm disabled */
2087 if (EVP_PKEY_keygen_init(genctx
) <= 0) {
2088 t
->err
= "KEYGEN_INIT_ERROR";
2092 if (!TEST_ptr(data
= OPENSSL_malloc(sizeof(*data
))))
2094 data
->genctx
= genctx
;
2095 data
->keyname
= NULL
;
2101 EVP_PKEY_CTX_free(genctx
);
2105 static void keygen_test_cleanup(EVP_TEST
*t
)
2107 KEYGEN_TEST_DATA
*keygen
= t
->data
;
2109 EVP_PKEY_CTX_free(keygen
->genctx
);
2110 OPENSSL_free(keygen
->keyname
);
2111 OPENSSL_free(t
->data
);
2115 static int keygen_test_parse(EVP_TEST
*t
,
2116 const char *keyword
, const char *value
)
2118 KEYGEN_TEST_DATA
*keygen
= t
->data
;
2120 if (strcmp(keyword
, "KeyName") == 0)
2121 return TEST_ptr(keygen
->keyname
= OPENSSL_strdup(value
));
2122 if (strcmp(keyword
, "Ctrl") == 0)
2123 return pkey_test_ctrl(t
, keygen
->genctx
, value
);
2127 static int keygen_test_run(EVP_TEST
*t
)
2129 KEYGEN_TEST_DATA
*keygen
= t
->data
;
2130 EVP_PKEY
*pkey
= NULL
;
2133 if (EVP_PKEY_keygen(keygen
->genctx
, &pkey
) <= 0) {
2134 t
->err
= "KEYGEN_GENERATE_ERROR";
2138 if (keygen
->keyname
!= NULL
) {
2141 if (find_key(NULL
, keygen
->keyname
, private_keys
)) {
2142 TEST_info("Duplicate key %s", keygen
->keyname
);
2146 if (!TEST_ptr(key
= OPENSSL_malloc(sizeof(*key
))))
2148 key
->name
= keygen
->keyname
;
2149 keygen
->keyname
= NULL
;
2151 key
->next
= private_keys
;
2154 EVP_PKEY_free(pkey
);
2160 EVP_PKEY_free(pkey
);
2164 static const EVP_TEST_METHOD keygen_test_method
= {
2167 keygen_test_cleanup
,
2173 *** DIGEST SIGN+VERIFY TESTS
2177 int is_verify
; /* Set to 1 if verifying */
2178 int is_oneshot
; /* Set to 1 for one shot operation */
2179 const EVP_MD
*md
; /* Digest to use */
2180 EVP_MD_CTX
*ctx
; /* Digest context */
2182 STACK_OF(EVP_TEST_BUFFER
) *input
; /* Input data: streaming */
2183 unsigned char *osin
; /* Input data if one shot */
2184 size_t osin_len
; /* Input length data if one shot */
2185 unsigned char *output
; /* Expected output */
2186 size_t output_len
; /* Expected output length */
2189 static int digestsigver_test_init(EVP_TEST
*t
, const char *alg
, int is_verify
,
2192 const EVP_MD
*md
= NULL
;
2193 DIGESTSIGN_DATA
*mdat
;
2195 if (strcmp(alg
, "NULL") != 0) {
2196 if ((md
= EVP_get_digestbyname(alg
)) == NULL
) {
2197 /* If alg has an OID assume disabled algorithm */
2198 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
2205 if (!TEST_ptr(mdat
= OPENSSL_zalloc(sizeof(*mdat
))))
2208 if (!TEST_ptr(mdat
->ctx
= EVP_MD_CTX_new())) {
2212 mdat
->is_verify
= is_verify
;
2213 mdat
->is_oneshot
= is_oneshot
;
2218 static int digestsign_test_init(EVP_TEST
*t
, const char *alg
)
2220 return digestsigver_test_init(t
, alg
, 0, 0);
2223 static void digestsigver_test_cleanup(EVP_TEST
*t
)
2225 DIGESTSIGN_DATA
*mdata
= t
->data
;
2227 EVP_MD_CTX_free(mdata
->ctx
);
2228 sk_EVP_TEST_BUFFER_pop_free(mdata
->input
, evp_test_buffer_free
);
2229 OPENSSL_free(mdata
->osin
);
2230 OPENSSL_free(mdata
->output
);
2231 OPENSSL_free(mdata
);
2235 static int digestsigver_test_parse(EVP_TEST
*t
,
2236 const char *keyword
, const char *value
)
2238 DIGESTSIGN_DATA
*mdata
= t
->data
;
2240 if (strcmp(keyword
, "Key") == 0) {
2241 EVP_PKEY
*pkey
= NULL
;
2244 if (mdata
->is_verify
)
2245 rv
= find_key(&pkey
, value
, public_keys
);
2247 rv
= find_key(&pkey
, value
, private_keys
);
2248 if (rv
== 0 || pkey
== NULL
) {
2252 if (mdata
->is_verify
) {
2253 if (!EVP_DigestVerifyInit(mdata
->ctx
, &mdata
->pctx
, mdata
->md
,
2255 t
->err
= "DIGESTVERIFYINIT_ERROR";
2258 if (!EVP_DigestSignInit(mdata
->ctx
, &mdata
->pctx
, mdata
->md
, NULL
,
2260 t
->err
= "DIGESTSIGNINIT_ERROR";
2264 if (strcmp(keyword
, "Input") == 0) {
2265 if (mdata
->is_oneshot
)
2266 return parse_bin(value
, &mdata
->osin
, &mdata
->osin_len
);
2267 return evp_test_buffer_append(value
, &mdata
->input
);
2269 if (strcmp(keyword
, "Output") == 0)
2270 return parse_bin(value
, &mdata
->output
, &mdata
->output_len
);
2272 if (!mdata
->is_oneshot
) {
2273 if (strcmp(keyword
, "Count") == 0)
2274 return evp_test_buffer_set_count(value
, mdata
->input
);
2275 if (strcmp(keyword
, "Ncopy") == 0)
2276 return evp_test_buffer_ncopy(value
, mdata
->input
);
2278 if (strcmp(keyword
, "Ctrl") == 0) {
2279 if (mdata
->pctx
== NULL
)
2281 return pkey_test_ctrl(t
, mdata
->pctx
, value
);
2286 static int digestsign_update_fn(void *ctx
, const unsigned char *buf
,
2289 return EVP_DigestSignUpdate(ctx
, buf
, buflen
);
2292 static int digestsign_test_run(EVP_TEST
*t
)
2294 DIGESTSIGN_DATA
*expected
= t
->data
;
2295 unsigned char *got
= NULL
;
2298 if (!evp_test_buffer_do(expected
->input
, digestsign_update_fn
,
2300 t
->err
= "DIGESTUPDATE_ERROR";
2304 if (!EVP_DigestSignFinal(expected
->ctx
, NULL
, &got_len
)) {
2305 t
->err
= "DIGESTSIGNFINAL_LENGTH_ERROR";
2308 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
2309 t
->err
= "MALLOC_FAILURE";
2312 if (!EVP_DigestSignFinal(expected
->ctx
, got
, &got_len
)) {
2313 t
->err
= "DIGESTSIGNFINAL_ERROR";
2316 if (!memory_err_compare(t
, "SIGNATURE_MISMATCH",
2317 expected
->output
, expected
->output_len
,
2327 static const EVP_TEST_METHOD digestsign_test_method
= {
2329 digestsign_test_init
,
2330 digestsigver_test_cleanup
,
2331 digestsigver_test_parse
,
2335 static int digestverify_test_init(EVP_TEST
*t
, const char *alg
)
2337 return digestsigver_test_init(t
, alg
, 1, 0);
2340 static int digestverify_update_fn(void *ctx
, const unsigned char *buf
,
2343 return EVP_DigestVerifyUpdate(ctx
, buf
, buflen
);
2346 static int digestverify_test_run(EVP_TEST
*t
)
2348 DIGESTSIGN_DATA
*mdata
= t
->data
;
2350 if (!evp_test_buffer_do(mdata
->input
, digestverify_update_fn
, mdata
->ctx
)) {
2351 t
->err
= "DIGESTUPDATE_ERROR";
2355 if (EVP_DigestVerifyFinal(mdata
->ctx
, mdata
->output
,
2356 mdata
->output_len
) <= 0)
2357 t
->err
= "VERIFY_ERROR";
2361 static const EVP_TEST_METHOD digestverify_test_method
= {
2363 digestverify_test_init
,
2364 digestsigver_test_cleanup
,
2365 digestsigver_test_parse
,
2366 digestverify_test_run
2369 static int oneshot_digestsign_test_init(EVP_TEST
*t
, const char *alg
)
2371 return digestsigver_test_init(t
, alg
, 0, 1);
2374 static int oneshot_digestsign_test_run(EVP_TEST
*t
)
2376 DIGESTSIGN_DATA
*expected
= t
->data
;
2377 unsigned char *got
= NULL
;
2380 if (!EVP_DigestSign(expected
->ctx
, NULL
, &got_len
,
2381 expected
->osin
, expected
->osin_len
)) {
2382 t
->err
= "DIGESTSIGN_LENGTH_ERROR";
2385 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
2386 t
->err
= "MALLOC_FAILURE";
2389 if (!EVP_DigestSign(expected
->ctx
, got
, &got_len
,
2390 expected
->osin
, expected
->osin_len
)) {
2391 t
->err
= "DIGESTSIGN_ERROR";
2394 if (!memory_err_compare(t
, "SIGNATURE_MISMATCH",
2395 expected
->output
, expected
->output_len
,
2405 static const EVP_TEST_METHOD oneshot_digestsign_test_method
= {
2406 "OneShotDigestSign",
2407 oneshot_digestsign_test_init
,
2408 digestsigver_test_cleanup
,
2409 digestsigver_test_parse
,
2410 oneshot_digestsign_test_run
2413 static int oneshot_digestverify_test_init(EVP_TEST
*t
, const char *alg
)
2415 return digestsigver_test_init(t
, alg
, 1, 1);
2418 static int oneshot_digestverify_test_run(EVP_TEST
*t
)
2420 DIGESTSIGN_DATA
*mdata
= t
->data
;
2422 if (EVP_DigestVerify(mdata
->ctx
, mdata
->output
, mdata
->output_len
,
2423 mdata
->osin
, mdata
->osin_len
) <= 0)
2424 t
->err
= "VERIFY_ERROR";
2428 static const EVP_TEST_METHOD oneshot_digestverify_test_method
= {
2429 "OneShotDigestVerify",
2430 oneshot_digestverify_test_init
,
2431 digestsigver_test_cleanup
,
2432 digestsigver_test_parse
,
2433 oneshot_digestverify_test_run
2438 *** PARSING AND DISPATCH
2441 static const EVP_TEST_METHOD
*evp_test_list
[] = {
2442 &cipher_test_method
,
2443 &digest_test_method
,
2444 &digestsign_test_method
,
2445 &digestverify_test_method
,
2446 &encode_test_method
,
2448 &keypair_test_method
,
2449 &keygen_test_method
,
2451 &oneshot_digestsign_test_method
,
2452 &oneshot_digestverify_test_method
,
2454 &pdecrypt_test_method
,
2455 &pderive_test_method
,
2457 &pverify_recover_test_method
,
2458 &pverify_test_method
,
2462 static const EVP_TEST_METHOD
*find_test(const char *name
)
2464 const EVP_TEST_METHOD
**tt
;
2466 for (tt
= evp_test_list
; *tt
; tt
++) {
2467 if (strcmp(name
, (*tt
)->name
) == 0)
2473 static void clear_test(EVP_TEST
*t
)
2475 test_clearstanza(&t
->s
);
2477 if (t
->data
!= NULL
) {
2478 if (t
->meth
!= NULL
)
2479 t
->meth
->cleanup(t
);
2480 OPENSSL_free(t
->data
);
2483 OPENSSL_free(t
->expected_err
);
2484 t
->expected_err
= NULL
;
2485 OPENSSL_free(t
->func
);
2487 OPENSSL_free(t
->reason
);
2497 * Check for errors in the test structure; return 1 if okay, else 0.
2499 static int check_test_error(EVP_TEST
*t
)
2505 if (t
->err
== NULL
&& t
->expected_err
== NULL
)
2507 if (t
->err
!= NULL
&& t
->expected_err
== NULL
) {
2508 if (t
->aux_err
!= NULL
) {
2509 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
2510 t
->s
.test_file
, t
->s
.start
, t
->aux_err
, t
->err
);
2512 TEST_info("%s:%d: Source of above error; unexpected error %s",
2513 t
->s
.test_file
, t
->s
.start
, t
->err
);
2517 if (t
->err
== NULL
&& t
->expected_err
!= NULL
) {
2518 TEST_info("%s:%d: Succeeded but was expecting %s",
2519 t
->s
.test_file
, t
->s
.start
, t
->expected_err
);
2523 if (strcmp(t
->err
, t
->expected_err
) != 0) {
2524 TEST_info("%s:%d: Expected %s got %s",
2525 t
->s
.test_file
, t
->s
.start
, t
->expected_err
, t
->err
);
2529 if (t
->func
== NULL
&& t
->reason
== NULL
)
2532 if (t
->func
== NULL
|| t
->reason
== NULL
) {
2533 TEST_info("%s:%d: Test is missing function or reason code",
2534 t
->s
.test_file
, t
->s
.start
);
2538 err
= ERR_peek_error();
2540 TEST_info("%s:%d: Expected error \"%s:%s\" not set",
2541 t
->s
.test_file
, t
->s
.start
, t
->func
, t
->reason
);
2545 func
= ERR_func_error_string(err
);
2546 reason
= ERR_reason_error_string(err
);
2547 if (func
== NULL
&& reason
== NULL
) {
2548 TEST_info("%s:%d: Expected error \"%s:%s\", no strings available."
2550 t
->s
.test_file
, t
->s
.start
, t
->func
, t
->reason
);
2554 if (strcmp(func
, t
->func
) == 0 && strcmp(reason
, t
->reason
) == 0)
2557 TEST_info("%s:%d: Expected error \"%s:%s\", got \"%s:%s\"",
2558 t
->s
.test_file
, t
->s
.start
, t
->func
, t
->reason
, func
, reason
);
2564 * Run a parsed test. Log a message and return 0 on error.
2566 static int run_test(EVP_TEST
*t
)
2568 if (t
->meth
== NULL
)
2575 if (t
->err
== NULL
&& t
->meth
->run_test(t
) != 1) {
2576 TEST_info("%s:%d %s error",
2577 t
->s
.test_file
, t
->s
.start
, t
->meth
->name
);
2580 if (!check_test_error(t
)) {
2581 TEST_openssl_errors();
2590 static int find_key(EVP_PKEY
**ppk
, const char *name
, KEY_LIST
*lst
)
2592 for (; lst
!= NULL
; lst
= lst
->next
) {
2593 if (strcmp(lst
->name
, name
) == 0) {
2602 static void free_key_list(KEY_LIST
*lst
)
2604 while (lst
!= NULL
) {
2605 KEY_LIST
*next
= lst
->next
;
2607 EVP_PKEY_free(lst
->key
);
2608 OPENSSL_free(lst
->name
);
2615 * Is the key type an unsupported algorithm?
2617 static int key_unsupported(void)
2619 long err
= ERR_peek_error();
2621 if (ERR_GET_LIB(err
) == ERR_LIB_EVP
2622 && ERR_GET_REASON(err
) == EVP_R_UNSUPPORTED_ALGORITHM
) {
2626 #ifndef OPENSSL_NO_EC
2628 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
2629 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
2632 if (ERR_GET_LIB(err
) == ERR_LIB_EC
2633 && ERR_GET_REASON(err
) == EC_R_UNKNOWN_GROUP
) {
2637 #endif /* OPENSSL_NO_EC */
2642 * NULL out the value from |pp| but return it. This "steals" a pointer.
2644 static char *take_value(PAIR
*pp
)
2646 char *p
= pp
->value
;
2653 * Read and parse one test. Return 0 if failure, 1 if okay.
2655 static int parse(EVP_TEST
*t
)
2657 KEY_LIST
*key
, **klist
;
2664 if (BIO_eof(t
->s
.fp
))
2667 if (!test_readstanza(&t
->s
))
2669 } while (t
->s
.numpairs
== 0);
2670 pp
= &t
->s
.pairs
[0];
2672 /* Are we adding a key? */
2675 if (strcmp(pp
->key
, "PrivateKey") == 0) {
2676 pkey
= PEM_read_bio_PrivateKey(t
->s
.key
, NULL
, 0, NULL
);
2677 if (pkey
== NULL
&& !key_unsupported()) {
2678 EVP_PKEY_free(pkey
);
2679 TEST_info("Can't read private key %s", pp
->value
);
2680 TEST_openssl_errors();
2683 klist
= &private_keys
;
2684 } else if (strcmp(pp
->key
, "PublicKey") == 0) {
2685 pkey
= PEM_read_bio_PUBKEY(t
->s
.key
, NULL
, 0, NULL
);
2686 if (pkey
== NULL
&& !key_unsupported()) {
2687 EVP_PKEY_free(pkey
);
2688 TEST_info("Can't read public key %s", pp
->value
);
2689 TEST_openssl_errors();
2692 klist
= &public_keys
;
2693 } else if (strcmp(pp
->key
, "PrivateKeyRaw") == 0
2694 || strcmp(pp
->key
, "PublicKeyRaw") == 0 ) {
2695 char *strnid
= NULL
, *keydata
= NULL
;
2696 unsigned char *keybin
;
2700 if (strcmp(pp
->key
, "PrivateKeyRaw") == 0)
2701 klist
= &private_keys
;
2703 klist
= &public_keys
;
2705 strnid
= strchr(pp
->value
, ':');
2706 if (strnid
!= NULL
) {
2708 keydata
= strchr(strnid
, ':');
2709 if (keydata
!= NULL
)
2712 if (keydata
== NULL
) {
2713 TEST_info("Failed to parse %s value", pp
->key
);
2717 nid
= OBJ_txt2nid(strnid
);
2718 if (nid
== NID_undef
) {
2719 TEST_info("Uncrecognised algorithm NID");
2722 if (!parse_bin(keydata
, &keybin
, &keylen
)) {
2723 TEST_info("Failed to create binary key");
2726 if (klist
== &private_keys
)
2727 pkey
= EVP_PKEY_new_raw_private_key(nid
, NULL
, keybin
, keylen
);
2729 pkey
= EVP_PKEY_new_raw_public_key(nid
, NULL
, keybin
, keylen
);
2730 if (pkey
== NULL
&& !key_unsupported()) {
2731 TEST_info("Can't read %s data", pp
->key
);
2732 OPENSSL_free(keybin
);
2733 TEST_openssl_errors();
2736 OPENSSL_free(keybin
);
2739 /* If we have a key add to list */
2740 if (klist
!= NULL
) {
2741 if (find_key(NULL
, pp
->value
, *klist
)) {
2742 TEST_info("Duplicate key %s", pp
->value
);
2745 if (!TEST_ptr(key
= OPENSSL_malloc(sizeof(*key
))))
2747 key
->name
= take_value(pp
);
2749 /* Hack to detect SM2 keys */
2750 if(pkey
!= NULL
&& strstr(key
->name
, "SM2") != NULL
) {
2751 #ifdef OPENSSL_NO_SM2
2752 EVP_PKEY_free(pkey
);
2755 EVP_PKEY_set_alias_type(pkey
, EVP_PKEY_SM2
);
2763 /* Go back and start a new stanza. */
2764 if (t
->s
.numpairs
!= 1)
2765 TEST_info("Line %d: missing blank line\n", t
->s
.curr
);
2769 /* Find the test, based on first keyword. */
2770 if (!TEST_ptr(t
->meth
= find_test(pp
->key
)))
2772 if (!t
->meth
->init(t
, pp
->value
)) {
2773 TEST_error("unknown %s: %s\n", pp
->key
, pp
->value
);
2777 /* TEST_info("skipping %s %s", pp->key, pp->value); */
2781 for (pp
++, i
= 1; i
< t
->s
.numpairs
; pp
++, i
++) {
2782 if (strcmp(pp
->key
, "Result") == 0) {
2783 if (t
->expected_err
!= NULL
) {
2784 TEST_info("Line %d: multiple result lines", t
->s
.curr
);
2787 t
->expected_err
= take_value(pp
);
2788 } else if (strcmp(pp
->key
, "Function") == 0) {
2789 if (t
->func
!= NULL
) {
2790 TEST_info("Line %d: multiple function lines\n", t
->s
.curr
);
2793 t
->func
= take_value(pp
);
2794 } else if (strcmp(pp
->key
, "Reason") == 0) {
2795 if (t
->reason
!= NULL
) {
2796 TEST_info("Line %d: multiple reason lines", t
->s
.curr
);
2799 t
->reason
= take_value(pp
);
2801 /* Must be test specific line: try to parse it */
2802 int rv
= t
->meth
->parse(t
, pp
->key
, pp
->value
);
2805 TEST_info("Line %d: unknown keyword %s", t
->s
.curr
, pp
->key
);
2809 TEST_info("Line %d: error processing keyword %s = %s\n",
2810 t
->s
.curr
, pp
->key
, pp
->value
);
2819 static int run_file_tests(int i
)
2822 const char *testfile
= test_get_argument(i
);
2825 if (!TEST_ptr(t
= OPENSSL_zalloc(sizeof(*t
))))
2827 if (!test_start_file(&t
->s
, testfile
)) {
2832 while (!BIO_eof(t
->s
.fp
)) {
2836 if (c
== 0 || !run_test(t
)) {
2841 test_end_file(&t
->s
);
2844 free_key_list(public_keys
);
2845 free_key_list(private_keys
);
2852 int setup_tests(void)
2854 size_t n
= test_get_argument_count();
2857 TEST_error("Usage: %s file...", test_get_program_name());
2861 ADD_ALL_TESTS(run_file_tests
, n
);