2 * Copyright 2015-2017 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 * Structure used to hold a list of blocks of memory to test
79 * calls to "update" like functions.
81 struct evp_test_buffer_st
{
88 static void evp_test_buffer_free(EVP_TEST_BUFFER
*db
)
91 OPENSSL_free(db
->buf
);
97 * append buffer to a list
99 static int evp_test_buffer_append(const char *value
,
100 STACK_OF(EVP_TEST_BUFFER
) **sk
)
102 EVP_TEST_BUFFER
*db
= NULL
;
104 if (!TEST_ptr(db
= OPENSSL_malloc(sizeof(*db
))))
107 if (!parse_bin(value
, &db
->buf
, &db
->buflen
))
112 if (*sk
== NULL
&& !TEST_ptr(*sk
= sk_EVP_TEST_BUFFER_new_null()))
114 if (!sk_EVP_TEST_BUFFER_push(*sk
, db
))
120 evp_test_buffer_free(db
);
125 * replace last buffer in list with copies of itself
127 static int evp_test_buffer_ncopy(const char *value
,
128 STACK_OF(EVP_TEST_BUFFER
) *sk
)
131 unsigned char *tbuf
, *p
;
133 int ncopy
= atoi(value
);
138 if (sk
== NULL
|| sk_EVP_TEST_BUFFER_num(sk
) == 0)
140 db
= sk_EVP_TEST_BUFFER_value(sk
, sk_EVP_TEST_BUFFER_num(sk
) - 1);
142 tbuflen
= db
->buflen
* ncopy
;
143 if (!TEST_ptr(tbuf
= OPENSSL_malloc(tbuflen
)))
145 for (i
= 0, p
= tbuf
; i
< ncopy
; i
++, p
+= db
->buflen
)
146 memcpy(p
, db
->buf
, db
->buflen
);
148 OPENSSL_free(db
->buf
);
150 db
->buflen
= tbuflen
;
155 * set repeat count for last buffer in list
157 static int evp_test_buffer_set_count(const char *value
,
158 STACK_OF(EVP_TEST_BUFFER
) *sk
)
161 int count
= atoi(value
);
166 if (sk
== NULL
|| sk_EVP_TEST_BUFFER_num(sk
) == 0)
169 db
= sk_EVP_TEST_BUFFER_value(sk
, sk_EVP_TEST_BUFFER_num(sk
) - 1);
170 if (db
->count_set
!= 0)
173 db
->count
= (size_t)count
;
179 * call "fn" with each element of the list in turn
181 static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER
) *sk
,
183 const unsigned char *buf
,
189 for (i
= 0; i
< sk_EVP_TEST_BUFFER_num(sk
); i
++) {
190 EVP_TEST_BUFFER
*tb
= sk_EVP_TEST_BUFFER_value(sk
, i
);
193 for (j
= 0; j
< tb
->count
; j
++) {
194 if (fn(ctx
, tb
->buf
, tb
->buflen
) <= 0)
202 * Unescape some sequences in string literals (only \n for now).
203 * Return an allocated buffer, set |out_len|. If |input_len|
204 * is zero, get an empty buffer but set length to zero.
206 static unsigned char* unescape(const char *input
, size_t input_len
,
209 unsigned char *ret
, *p
;
212 if (input_len
== 0) {
214 return OPENSSL_zalloc(1);
217 /* Escaping is non-expanding; over-allocate original size for simplicity. */
218 if (!TEST_ptr(ret
= p
= OPENSSL_malloc(input_len
)))
221 for (i
= 0; i
< input_len
; i
++) {
222 if (*input
== '\\') {
223 if (i
== input_len
- 1 || *++input
!= 'n') {
224 TEST_error("Bad escape sequence in file");
244 * For a hex string "value" convert to a binary allocated buffer.
245 * Return 1 on success or 0 on failure.
247 static int parse_bin(const char *value
, unsigned char **buf
, size_t *buflen
)
251 /* Check for NULL literal */
252 if (strcmp(value
, "NULL") == 0) {
258 /* Check for empty value */
259 if (*value
== '\0') {
261 * Don't return NULL for zero length buffer. This is needed for
262 * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key
263 * buffer even if the key length is 0, in order to detect key reset.
265 *buf
= OPENSSL_malloc(1);
273 /* Check for string literal */
274 if (value
[0] == '"') {
275 size_t vlen
= strlen(++value
);
277 if (vlen
== 0 || value
[vlen
- 1] != '"')
280 *buf
= unescape(value
, vlen
, buflen
);
281 return *buf
== NULL
? 0 : 1;
284 /* Otherwise assume as hex literal and convert it to binary buffer */
285 if (!TEST_ptr(*buf
= OPENSSL_hexstr2buf(value
, &len
))) {
286 TEST_info("Can't convert %s", value
);
287 ERR_print_errors(bio_err
);
290 /* Size of input buffer means we'll never overflow */
297 *** MESSAGE DIGEST TESTS
300 typedef struct digest_data_st
{
301 /* Digest this test is for */
302 const EVP_MD
*digest
;
303 /* Input to digest */
304 STACK_OF(EVP_TEST_BUFFER
) *input
;
305 /* Expected output */
306 unsigned char *output
;
310 static int digest_test_init(EVP_TEST
*t
, const char *alg
)
313 const EVP_MD
*digest
;
315 if ((digest
= EVP_get_digestbyname(alg
)) == NULL
) {
316 /* If alg has an OID assume disabled algorithm */
317 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
323 if (!TEST_ptr(mdat
= OPENSSL_zalloc(sizeof(*mdat
))))
326 mdat
->digest
= digest
;
330 static void digest_test_cleanup(EVP_TEST
*t
)
332 DIGEST_DATA
*mdat
= t
->data
;
334 sk_EVP_TEST_BUFFER_pop_free(mdat
->input
, evp_test_buffer_free
);
335 OPENSSL_free(mdat
->output
);
338 static int digest_test_parse(EVP_TEST
*t
,
339 const char *keyword
, const char *value
)
341 DIGEST_DATA
*mdata
= t
->data
;
343 if (strcmp(keyword
, "Input") == 0)
344 return evp_test_buffer_append(value
, &mdata
->input
);
345 if (strcmp(keyword
, "Output") == 0)
346 return parse_bin(value
, &mdata
->output
, &mdata
->output_len
);
347 if (strcmp(keyword
, "Count") == 0)
348 return evp_test_buffer_set_count(value
, mdata
->input
);
349 if (strcmp(keyword
, "Ncopy") == 0)
350 return evp_test_buffer_ncopy(value
, mdata
->input
);
354 static int digest_update_fn(void *ctx
, const unsigned char *buf
, size_t buflen
)
356 return EVP_DigestUpdate(ctx
, buf
, buflen
);
359 static int digest_test_run(EVP_TEST
*t
)
361 DIGEST_DATA
*expected
= t
->data
;
363 unsigned char got
[EVP_MAX_MD_SIZE
];
364 unsigned int got_len
;
366 t
->err
= "TEST_FAILURE";
367 if (!TEST_ptr(mctx
= EVP_MD_CTX_new()))
370 if (!EVP_DigestInit_ex(mctx
, expected
->digest
, NULL
)) {
371 t
->err
= "DIGESTINIT_ERROR";
374 if (!evp_test_buffer_do(expected
->input
, digest_update_fn
, mctx
)) {
375 t
->err
= "DIGESTUPDATE_ERROR";
379 if (!EVP_DigestFinal(mctx
, got
, &got_len
)) {
380 t
->err
= "DIGESTFINAL_ERROR";
383 if (!TEST_int_eq(expected
->output_len
, got_len
)) {
384 t
->err
= "DIGEST_LENGTH_MISMATCH";
387 if (!TEST_mem_eq(expected
->output
, expected
->output_len
, got
, got_len
)) {
388 t
->err
= "DIGEST_MISMATCH";
394 EVP_MD_CTX_free(mctx
);
398 static const EVP_TEST_METHOD digest_test_method
= {
411 typedef struct cipher_data_st
{
412 const EVP_CIPHER
*cipher
;
414 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
420 unsigned char *plaintext
;
421 size_t plaintext_len
;
422 unsigned char *ciphertext
;
423 size_t ciphertext_len
;
431 static int cipher_test_init(EVP_TEST
*t
, const char *alg
)
433 const EVP_CIPHER
*cipher
;
437 if ((cipher
= EVP_get_cipherbyname(alg
)) == NULL
) {
438 /* If alg has an OID assume disabled algorithm */
439 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
445 cdat
= OPENSSL_zalloc(sizeof(*cdat
));
446 cdat
->cipher
= cipher
;
448 m
= EVP_CIPHER_mode(cipher
);
449 if (m
== EVP_CIPH_GCM_MODE
450 || m
== EVP_CIPH_OCB_MODE
451 || m
== EVP_CIPH_CCM_MODE
)
452 cdat
->aead
= EVP_CIPHER_mode(cipher
);
453 else if (EVP_CIPHER_flags(cipher
) & EVP_CIPH_FLAG_AEAD_CIPHER
)
462 static void cipher_test_cleanup(EVP_TEST
*t
)
464 CIPHER_DATA
*cdat
= t
->data
;
466 OPENSSL_free(cdat
->key
);
467 OPENSSL_free(cdat
->iv
);
468 OPENSSL_free(cdat
->ciphertext
);
469 OPENSSL_free(cdat
->plaintext
);
470 OPENSSL_free(cdat
->aad
);
471 OPENSSL_free(cdat
->tag
);
474 static int cipher_test_parse(EVP_TEST
*t
, const char *keyword
,
477 CIPHER_DATA
*cdat
= t
->data
;
479 if (strcmp(keyword
, "Key") == 0)
480 return parse_bin(value
, &cdat
->key
, &cdat
->key_len
);
481 if (strcmp(keyword
, "IV") == 0)
482 return parse_bin(value
, &cdat
->iv
, &cdat
->iv_len
);
483 if (strcmp(keyword
, "Plaintext") == 0)
484 return parse_bin(value
, &cdat
->plaintext
, &cdat
->plaintext_len
);
485 if (strcmp(keyword
, "Ciphertext") == 0)
486 return parse_bin(value
, &cdat
->ciphertext
, &cdat
->ciphertext_len
);
488 if (strcmp(keyword
, "AAD") == 0)
489 return parse_bin(value
, &cdat
->aad
, &cdat
->aad_len
);
490 if (strcmp(keyword
, "Tag") == 0)
491 return parse_bin(value
, &cdat
->tag
, &cdat
->tag_len
);
494 if (strcmp(keyword
, "Operation") == 0) {
495 if (strcmp(value
, "ENCRYPT") == 0)
497 else if (strcmp(value
, "DECRYPT") == 0)
506 static int cipher_test_enc(EVP_TEST
*t
, int enc
,
507 size_t out_misalign
, size_t inp_misalign
, int frag
)
509 CIPHER_DATA
*expected
= t
->data
;
510 unsigned char *in
, *expected_out
, *tmp
= NULL
;
511 size_t in_len
, out_len
, donelen
= 0;
512 int ok
= 0, tmplen
, chunklen
, tmpflen
;
513 EVP_CIPHER_CTX
*ctx
= NULL
;
515 t
->err
= "TEST_FAILURE";
516 if (!TEST_ptr(ctx
= EVP_CIPHER_CTX_new()))
518 EVP_CIPHER_CTX_set_flags(ctx
, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
);
520 in
= expected
->plaintext
;
521 in_len
= expected
->plaintext_len
;
522 expected_out
= expected
->ciphertext
;
523 out_len
= expected
->ciphertext_len
;
525 in
= expected
->ciphertext
;
526 in_len
= expected
->ciphertext_len
;
527 expected_out
= expected
->plaintext
;
528 out_len
= expected
->plaintext_len
;
530 if (inp_misalign
== (size_t)-1) {
532 * Exercise in-place encryption
534 tmp
= OPENSSL_malloc(out_misalign
+ in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
);
537 in
= memcpy(tmp
+ out_misalign
, in
, in_len
);
539 inp_misalign
+= 16 - ((out_misalign
+ in_len
) & 15);
541 * 'tmp' will store both output and copy of input. We make the copy
542 * of input to specifically aligned part of 'tmp'. So we just
543 * figured out how much padding would ensure the required alignment,
544 * now we allocate extended buffer and finally copy the input just
545 * past inp_misalign in expression below. Output will be written
546 * past out_misalign...
548 tmp
= OPENSSL_malloc(out_misalign
+ in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
+
549 inp_misalign
+ in_len
);
552 in
= memcpy(tmp
+ out_misalign
+ in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
+
553 inp_misalign
, in
, in_len
);
555 if (!EVP_CipherInit_ex(ctx
, expected
->cipher
, NULL
, NULL
, NULL
, enc
)) {
556 t
->err
= "CIPHERINIT_ERROR";
560 if (expected
->aead
) {
561 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_IVLEN
,
562 expected
->iv_len
, 0)) {
563 t
->err
= "INVALID_IV_LENGTH";
566 } else if (expected
->iv_len
!= (size_t)EVP_CIPHER_CTX_iv_length(ctx
)) {
567 t
->err
= "INVALID_IV_LENGTH";
571 if (expected
->aead
) {
574 * If encrypting or OCB just set tag length initially, otherwise
575 * set tag length and value.
577 if (enc
|| expected
->aead
== EVP_CIPH_OCB_MODE
) {
578 t
->err
= "TAG_LENGTH_SET_ERROR";
581 t
->err
= "TAG_SET_ERROR";
584 if (tag
|| expected
->aead
!= EVP_CIPH_GCM_MODE
) {
585 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_TAG
,
586 expected
->tag_len
, tag
))
591 if (!EVP_CIPHER_CTX_set_key_length(ctx
, expected
->key_len
)) {
592 t
->err
= "INVALID_KEY_LENGTH";
595 if (!EVP_CipherInit_ex(ctx
, NULL
, NULL
, expected
->key
, expected
->iv
, -1)) {
596 t
->err
= "KEY_SET_ERROR";
600 if (!enc
&& expected
->aead
== EVP_CIPH_OCB_MODE
) {
601 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_TAG
,
602 expected
->tag_len
, expected
->tag
)) {
603 t
->err
= "TAG_SET_ERROR";
608 if (expected
->aead
== EVP_CIPH_CCM_MODE
) {
609 if (!EVP_CipherUpdate(ctx
, NULL
, &tmplen
, NULL
, out_len
)) {
610 t
->err
= "CCM_PLAINTEXT_LENGTH_SET_ERROR";
615 t
->err
= "AAD_SET_ERROR";
617 if (!EVP_CipherUpdate(ctx
, NULL
, &chunklen
, expected
->aad
,
622 * Supply the AAD in chunks less than the block size where possible
624 if (expected
->aad_len
> 0) {
625 if (!EVP_CipherUpdate(ctx
, NULL
, &chunklen
, expected
->aad
, 1))
629 if (expected
->aad_len
> 2) {
630 if (!EVP_CipherUpdate(ctx
, NULL
, &chunklen
,
631 expected
->aad
+ donelen
,
632 expected
->aad_len
- 2))
634 donelen
+= expected
->aad_len
- 2;
636 if (expected
->aad_len
> 1
637 && !EVP_CipherUpdate(ctx
, NULL
, &chunklen
,
638 expected
->aad
+ donelen
, 1))
642 EVP_CIPHER_CTX_set_padding(ctx
, 0);
643 t
->err
= "CIPHERUPDATE_ERROR";
646 /* We supply the data all in one go */
647 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
, &tmplen
, in
, in_len
))
650 /* Supply the data in chunks less than the block size where possible */
652 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
, &chunklen
, in
, 1))
659 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
+ tmplen
, &chunklen
,
667 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
+ tmplen
, &chunklen
,
673 if (!EVP_CipherFinal_ex(ctx
, tmp
+ out_misalign
+ tmplen
, &tmpflen
)) {
674 t
->err
= "CIPHERFINAL_ERROR";
677 if (!TEST_mem_eq(expected_out
, out_len
,
678 tmp
+ out_misalign
, tmplen
+ tmpflen
)) {
679 t
->err
= "VALUE_MISMATCH";
682 if (enc
&& expected
->aead
) {
683 unsigned char rtag
[16];
685 if (!TEST_size_t_le(expected
->tag_len
, sizeof(rtag
))) {
686 t
->err
= "TAG_LENGTH_INTERNAL_ERROR";
689 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_GET_TAG
,
690 expected
->tag_len
, rtag
)) {
691 t
->err
= "TAG_RETRIEVE_ERROR";
694 if (!TEST_mem_eq(expected
->tag
, expected
->tag_len
,
695 rtag
, expected
->tag_len
)) {
696 t
->err
= "TAG_VALUE_MISMATCH";
704 EVP_CIPHER_CTX_free(ctx
);
708 static int cipher_test_run(EVP_TEST
*t
)
710 CIPHER_DATA
*cdat
= t
->data
;
712 size_t out_misalign
, inp_misalign
;
718 if (!cdat
->iv
&& EVP_CIPHER_iv_length(cdat
->cipher
)) {
719 /* IV is optional and usually omitted in wrap mode */
720 if (EVP_CIPHER_mode(cdat
->cipher
) != EVP_CIPH_WRAP_MODE
) {
725 if (cdat
->aead
&& !cdat
->tag
) {
729 for (out_misalign
= 0; out_misalign
<= 1;) {
730 static char aux_err
[64];
731 t
->aux_err
= aux_err
;
732 for (inp_misalign
= (size_t)-1; inp_misalign
!= 2; inp_misalign
++) {
733 if (inp_misalign
== (size_t)-1) {
734 /* kludge: inp_misalign == -1 means "exercise in-place" */
735 BIO_snprintf(aux_err
, sizeof(aux_err
),
736 "%s in-place, %sfragmented",
737 out_misalign
? "misaligned" : "aligned",
740 BIO_snprintf(aux_err
, sizeof(aux_err
),
741 "%s output and %s input, %sfragmented",
742 out_misalign
? "misaligned" : "aligned",
743 inp_misalign
? "misaligned" : "aligned",
747 rv
= cipher_test_enc(t
, 1, out_misalign
, inp_misalign
, frag
);
748 /* Not fatal errors: return */
755 if (cdat
->enc
!= 1) {
756 rv
= cipher_test_enc(t
, 0, out_misalign
, inp_misalign
, frag
);
757 /* Not fatal errors: return */
766 if (out_misalign
== 1 && frag
== 0) {
768 * XTS, CCM and Wrap modes have special requirements about input
769 * lengths so we don't fragment for those
771 if (cdat
->aead
== EVP_CIPH_CCM_MODE
772 || EVP_CIPHER_mode(cdat
->cipher
) == EVP_CIPH_XTS_MODE
773 || EVP_CIPHER_mode(cdat
->cipher
) == EVP_CIPH_WRAP_MODE
)
786 static const EVP_TEST_METHOD cipher_test_method
= {
799 typedef struct mac_data_st
{
802 /* Algorithm string for this MAC */
808 unsigned char *input
;
810 /* Expected output */
811 unsigned char *output
;
815 static int mac_test_init(EVP_TEST
*t
, const char *alg
)
820 if (strcmp(alg
, "HMAC") == 0) {
821 type
= EVP_PKEY_HMAC
;
822 } else if (strcmp(alg
, "CMAC") == 0) {
823 #ifndef OPENSSL_NO_CMAC
824 type
= EVP_PKEY_CMAC
;
829 } else if (strcmp(alg
, "Poly1305") == 0) {
830 #ifndef OPENSSL_NO_POLY1305
831 type
= EVP_PKEY_POLY1305
;
836 } else if (strcmp(alg
, "SipHash") == 0) {
837 #ifndef OPENSSL_NO_SIPHASH
838 type
= EVP_PKEY_SIPHASH
;
846 mdat
= OPENSSL_zalloc(sizeof(*mdat
));
852 static void mac_test_cleanup(EVP_TEST
*t
)
854 MAC_DATA
*mdat
= t
->data
;
856 OPENSSL_free(mdat
->alg
);
857 OPENSSL_free(mdat
->key
);
858 OPENSSL_free(mdat
->input
);
859 OPENSSL_free(mdat
->output
);
862 static int mac_test_parse(EVP_TEST
*t
,
863 const char *keyword
, const char *value
)
865 MAC_DATA
*mdata
= t
->data
;
867 if (strcmp(keyword
, "Key") == 0)
868 return parse_bin(value
, &mdata
->key
, &mdata
->key_len
);
869 if (strcmp(keyword
, "Algorithm") == 0) {
870 mdata
->alg
= OPENSSL_strdup(value
);
875 if (strcmp(keyword
, "Input") == 0)
876 return parse_bin(value
, &mdata
->input
, &mdata
->input_len
);
877 if (strcmp(keyword
, "Output") == 0)
878 return parse_bin(value
, &mdata
->output
, &mdata
->output_len
);
882 static int mac_test_run(EVP_TEST
*t
)
884 MAC_DATA
*expected
= t
->data
;
885 EVP_MD_CTX
*mctx
= NULL
;
886 EVP_PKEY_CTX
*pctx
= NULL
, *genctx
= NULL
;
887 EVP_PKEY
*key
= NULL
;
888 const EVP_MD
*md
= NULL
;
889 unsigned char *got
= NULL
;
892 #ifdef OPENSSL_NO_DES
893 if (expected
->alg
!= NULL
&& strstr(expected
->alg
, "DES") != NULL
) {
900 if (!TEST_ptr(genctx
= EVP_PKEY_CTX_new_id(expected
->type
, NULL
))) {
901 t
->err
= "MAC_PKEY_CTX_ERROR";
905 if (EVP_PKEY_keygen_init(genctx
) <= 0) {
906 t
->err
= "MAC_KEYGEN_INIT_ERROR";
909 if (expected
->type
== EVP_PKEY_CMAC
910 && EVP_PKEY_CTX_ctrl_str(genctx
, "cipher", expected
->alg
) <= 0) {
911 t
->err
= "MAC_ALGORITHM_SET_ERROR";
915 if (EVP_PKEY_CTX_set_mac_key(genctx
, expected
->key
,
916 expected
->key_len
) <= 0) {
917 t
->err
= "MAC_KEY_SET_ERROR";
921 if (EVP_PKEY_keygen(genctx
, &key
) <= 0) {
922 t
->err
= "MAC_KEY_GENERATE_ERROR";
925 if (expected
->type
== EVP_PKEY_HMAC
) {
926 if (!TEST_ptr(md
= EVP_get_digestbyname(expected
->alg
))) {
927 t
->err
= "MAC_ALGORITHM_SET_ERROR";
931 if (!TEST_ptr(mctx
= EVP_MD_CTX_new())) {
932 t
->err
= "INTERNAL_ERROR";
935 if (!EVP_DigestSignInit(mctx
, &pctx
, md
, NULL
, key
)) {
936 t
->err
= "DIGESTSIGNINIT_ERROR";
940 if (!EVP_DigestSignUpdate(mctx
, expected
->input
, expected
->input_len
)) {
941 t
->err
= "DIGESTSIGNUPDATE_ERROR";
944 if (!EVP_DigestSignFinal(mctx
, NULL
, &got_len
)) {
945 t
->err
= "DIGESTSIGNFINAL_LENGTH_ERROR";
948 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
949 t
->err
= "TEST_FAILURE";
952 if (!EVP_DigestSignFinal(mctx
, got
, &got_len
)
953 || !TEST_mem_eq(expected
->output
, expected
->output_len
,
955 t
->err
= "TEST_MAC_ERR";
960 EVP_MD_CTX_free(mctx
);
962 EVP_PKEY_CTX_free(genctx
);
967 static const EVP_TEST_METHOD mac_test_method
= {
978 *** These are all very similar and share much common code.
981 typedef struct pkey_data_st
{
982 /* Context for this operation */
984 /* Key operation to perform */
985 int (*keyop
) (EVP_PKEY_CTX
*ctx
,
986 unsigned char *sig
, size_t *siglen
,
987 const unsigned char *tbs
, size_t tbslen
);
989 unsigned char *input
;
991 /* Expected output */
992 unsigned char *output
;
997 * Perform public key operation setup: lookup key, allocated ctx and call
998 * the appropriate initialisation function
1000 static int pkey_test_init(EVP_TEST
*t
, const char *name
,
1002 int (*keyopinit
) (EVP_PKEY_CTX
*ctx
),
1003 int (*keyop
)(EVP_PKEY_CTX
*ctx
,
1004 unsigned char *sig
, size_t *siglen
,
1005 const unsigned char *tbs
,
1009 EVP_PKEY
*pkey
= NULL
;
1013 rv
= find_key(&pkey
, name
, public_keys
);
1015 rv
= find_key(&pkey
, name
, private_keys
);
1016 if (rv
== 0 || pkey
== NULL
) {
1021 if (!TEST_ptr(kdata
= OPENSSL_zalloc(sizeof(*kdata
)))) {
1022 EVP_PKEY_free(pkey
);
1025 kdata
->keyop
= keyop
;
1026 if (!TEST_ptr(kdata
->ctx
= EVP_PKEY_CTX_new(pkey
, NULL
)))
1028 if (keyopinit(kdata
->ctx
) <= 0)
1029 t
->err
= "KEYOP_INIT_ERROR";
1034 static void pkey_test_cleanup(EVP_TEST
*t
)
1036 PKEY_DATA
*kdata
= t
->data
;
1038 OPENSSL_free(kdata
->input
);
1039 OPENSSL_free(kdata
->output
);
1040 EVP_PKEY_CTX_free(kdata
->ctx
);
1043 static int pkey_test_ctrl(EVP_TEST
*t
, EVP_PKEY_CTX
*pctx
,
1049 if (!TEST_ptr(tmpval
= OPENSSL_strdup(value
)))
1051 p
= strchr(tmpval
, ':');
1054 rv
= EVP_PKEY_CTX_ctrl_str(pctx
, tmpval
, p
);
1056 t
->err
= "PKEY_CTRL_INVALID";
1058 } else if (p
!= NULL
&& rv
<= 0) {
1059 /* If p has an OID and lookup fails assume disabled algorithm */
1060 int nid
= OBJ_sn2nid(p
);
1062 if (nid
== NID_undef
)
1063 nid
= OBJ_ln2nid(p
);
1064 if (nid
!= NID_undef
1065 && EVP_get_digestbynid(nid
) == NULL
1066 && EVP_get_cipherbynid(nid
) == NULL
) {
1070 t
->err
= "PKEY_CTRL_ERROR";
1074 OPENSSL_free(tmpval
);
1078 static int pkey_test_parse(EVP_TEST
*t
,
1079 const char *keyword
, const char *value
)
1081 PKEY_DATA
*kdata
= t
->data
;
1082 if (strcmp(keyword
, "Input") == 0)
1083 return parse_bin(value
, &kdata
->input
, &kdata
->input_len
);
1084 if (strcmp(keyword
, "Output") == 0)
1085 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
1086 if (strcmp(keyword
, "Ctrl") == 0)
1087 return pkey_test_ctrl(t
, kdata
->ctx
, value
);
1091 static int pkey_test_run(EVP_TEST
*t
)
1093 PKEY_DATA
*expected
= t
->data
;
1094 unsigned char *got
= NULL
;
1097 if (expected
->keyop(expected
->ctx
, NULL
, &got_len
,
1098 expected
->input
, expected
->input_len
) <= 0
1099 || !TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1100 t
->err
= "KEYOP_LENGTH_ERROR";
1103 if (expected
->keyop(expected
->ctx
, got
, &got_len
,
1104 expected
->input
, expected
->input_len
) <= 0) {
1105 t
->err
= "KEYOP_ERROR";
1108 if (!TEST_mem_eq(expected
->output
, expected
->output_len
, got
, got_len
)) {
1109 t
->err
= "KEYOP_MISMATCH";
1118 static int sign_test_init(EVP_TEST
*t
, const char *name
)
1120 return pkey_test_init(t
, name
, 0, EVP_PKEY_sign_init
, EVP_PKEY_sign
);
1123 static const EVP_TEST_METHOD psign_test_method
= {
1131 static int verify_recover_test_init(EVP_TEST
*t
, const char *name
)
1133 return pkey_test_init(t
, name
, 1, EVP_PKEY_verify_recover_init
,
1134 EVP_PKEY_verify_recover
);
1137 static const EVP_TEST_METHOD pverify_recover_test_method
= {
1139 verify_recover_test_init
,
1145 static int decrypt_test_init(EVP_TEST
*t
, const char *name
)
1147 return pkey_test_init(t
, name
, 0, EVP_PKEY_decrypt_init
,
1151 static const EVP_TEST_METHOD pdecrypt_test_method
= {
1159 static int verify_test_init(EVP_TEST
*t
, const char *name
)
1161 return pkey_test_init(t
, name
, 1, EVP_PKEY_verify_init
, 0);
1164 static int verify_test_run(EVP_TEST
*t
)
1166 PKEY_DATA
*kdata
= t
->data
;
1168 if (EVP_PKEY_verify(kdata
->ctx
, kdata
->output
, kdata
->output_len
,
1169 kdata
->input
, kdata
->input_len
) <= 0)
1170 t
->err
= "VERIFY_ERROR";
1174 static const EVP_TEST_METHOD pverify_test_method
= {
1183 static int pderive_test_init(EVP_TEST
*t
, const char *name
)
1185 return pkey_test_init(t
, name
, 0, EVP_PKEY_derive_init
, 0);
1188 static int pderive_test_parse(EVP_TEST
*t
,
1189 const char *keyword
, const char *value
)
1191 PKEY_DATA
*kdata
= t
->data
;
1193 if (strcmp(keyword
, "PeerKey") == 0) {
1195 if (find_key(&peer
, value
, public_keys
) == 0)
1197 if (EVP_PKEY_derive_set_peer(kdata
->ctx
, peer
) <= 0)
1201 if (strcmp(keyword
, "SharedSecret") == 0)
1202 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
1203 if (strcmp(keyword
, "Ctrl") == 0)
1204 return pkey_test_ctrl(t
, kdata
->ctx
, value
);
1208 static int pderive_test_run(EVP_TEST
*t
)
1210 PKEY_DATA
*expected
= t
->data
;
1211 unsigned char *got
= NULL
;
1214 got_len
= expected
->output_len
;
1215 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1216 t
->err
= "DERIVE_ERROR";
1219 if (EVP_PKEY_derive(expected
->ctx
, got
, &got_len
) <= 0) {
1220 t
->err
= "DERIVE_ERROR";
1223 if (!TEST_mem_eq(expected
->output
, expected
->output_len
, got
, got_len
)) {
1224 t
->err
= "SHARED_SECRET_MISMATCH";
1234 static const EVP_TEST_METHOD pderive_test_method
= {
1247 typedef enum pbe_type_enum
{
1248 PBE_TYPE_INVALID
= 0,
1249 PBE_TYPE_SCRYPT
, PBE_TYPE_PBKDF2
, PBE_TYPE_PKCS12
1252 typedef struct pbe_data_st
{
1254 /* scrypt parameters */
1255 uint64_t N
, r
, p
, maxmem
;
1256 /* PKCS#12 parameters */
1260 unsigned char *pass
;
1263 unsigned char *salt
;
1265 /* Expected output */
1270 #ifndef OPENSSL_NO_SCRYPT
1272 * Parse unsigned decimal 64 bit integer value
1274 static int parse_uint64(const char *value
, uint64_t *pr
)
1276 const char *p
= value
;
1278 if (!TEST_true(*p
)) {
1279 TEST_info("Invalid empty integer value");
1282 for (*pr
= 0; *p
; ) {
1283 if (*pr
> UINT64_MAX
/ 10) {
1284 TEST_error("Integer overflow in string %s", value
);
1288 if (!TEST_true(isdigit(*p
))) {
1289 TEST_error("Invalid character in string %s", value
);
1298 static int scrypt_test_parse(EVP_TEST
*t
,
1299 const char *keyword
, const char *value
)
1301 PBE_DATA
*pdata
= t
->data
;
1303 if (strcmp(keyword
, "N") == 0)
1304 return parse_uint64(value
, &pdata
->N
);
1305 if (strcmp(keyword
, "p") == 0)
1306 return parse_uint64(value
, &pdata
->p
);
1307 if (strcmp(keyword
, "r") == 0)
1308 return parse_uint64(value
, &pdata
->r
);
1309 if (strcmp(keyword
, "maxmem") == 0)
1310 return parse_uint64(value
, &pdata
->maxmem
);
1315 static int pbkdf2_test_parse(EVP_TEST
*t
,
1316 const char *keyword
, const char *value
)
1318 PBE_DATA
*pdata
= t
->data
;
1320 if (strcmp(keyword
, "iter") == 0) {
1321 pdata
->iter
= atoi(value
);
1322 if (pdata
->iter
<= 0)
1326 if (strcmp(keyword
, "MD") == 0) {
1327 pdata
->md
= EVP_get_digestbyname(value
);
1328 if (pdata
->md
== NULL
)
1335 static int pkcs12_test_parse(EVP_TEST
*t
,
1336 const char *keyword
, const char *value
)
1338 PBE_DATA
*pdata
= t
->data
;
1340 if (strcmp(keyword
, "id") == 0) {
1341 pdata
->id
= atoi(value
);
1346 return pbkdf2_test_parse(t
, keyword
, value
);
1349 static int pbe_test_init(EVP_TEST
*t
, const char *alg
)
1352 PBE_TYPE pbe_type
= PBE_TYPE_INVALID
;
1354 if (strcmp(alg
, "scrypt") == 0) {
1355 #ifndef OPENSSL_NO_SCRYPT
1356 pbe_type
= PBE_TYPE_SCRYPT
;
1361 } else if (strcmp(alg
, "pbkdf2") == 0) {
1362 pbe_type
= PBE_TYPE_PBKDF2
;
1363 } else if (strcmp(alg
, "pkcs12") == 0) {
1364 pbe_type
= PBE_TYPE_PKCS12
;
1366 TEST_error("Unknown pbe algorithm %s", alg
);
1368 pdat
= OPENSSL_zalloc(sizeof(*pdat
));
1369 pdat
->pbe_type
= pbe_type
;
1374 static void pbe_test_cleanup(EVP_TEST
*t
)
1376 PBE_DATA
*pdat
= t
->data
;
1378 OPENSSL_free(pdat
->pass
);
1379 OPENSSL_free(pdat
->salt
);
1380 OPENSSL_free(pdat
->key
);
1383 static int pbe_test_parse(EVP_TEST
*t
,
1384 const char *keyword
, const char *value
)
1386 PBE_DATA
*pdata
= t
->data
;
1388 if (strcmp(keyword
, "Password") == 0)
1389 return parse_bin(value
, &pdata
->pass
, &pdata
->pass_len
);
1390 if (strcmp(keyword
, "Salt") == 0)
1391 return parse_bin(value
, &pdata
->salt
, &pdata
->salt_len
);
1392 if (strcmp(keyword
, "Key") == 0)
1393 return parse_bin(value
, &pdata
->key
, &pdata
->key_len
);
1394 if (pdata
->pbe_type
== PBE_TYPE_PBKDF2
)
1395 return pbkdf2_test_parse(t
, keyword
, value
);
1396 else if (pdata
->pbe_type
== PBE_TYPE_PKCS12
)
1397 return pkcs12_test_parse(t
, keyword
, value
);
1398 #ifndef OPENSSL_NO_SCRYPT
1399 else if (pdata
->pbe_type
== PBE_TYPE_SCRYPT
)
1400 return scrypt_test_parse(t
, keyword
, value
);
1405 static int pbe_test_run(EVP_TEST
*t
)
1407 PBE_DATA
*expected
= t
->data
;
1410 if (!TEST_ptr(key
= OPENSSL_malloc(expected
->key_len
))) {
1411 t
->err
= "INTERNAL_ERROR";
1414 if (expected
->pbe_type
== PBE_TYPE_PBKDF2
) {
1415 if (PKCS5_PBKDF2_HMAC((char *)expected
->pass
, expected
->pass_len
,
1416 expected
->salt
, expected
->salt_len
,
1417 expected
->iter
, expected
->md
,
1418 expected
->key_len
, key
) == 0) {
1419 t
->err
= "PBKDF2_ERROR";
1422 #ifndef OPENSSL_NO_SCRYPT
1423 } else if (expected
->pbe_type
== PBE_TYPE_SCRYPT
) {
1424 if (EVP_PBE_scrypt((const char *)expected
->pass
, expected
->pass_len
,
1425 expected
->salt
, expected
->salt_len
, expected
->N
,
1426 expected
->r
, expected
->p
, expected
->maxmem
,
1427 key
, expected
->key_len
) == 0) {
1428 t
->err
= "SCRYPT_ERROR";
1432 } else if (expected
->pbe_type
== PBE_TYPE_PKCS12
) {
1433 if (PKCS12_key_gen_uni(expected
->pass
, expected
->pass_len
,
1434 expected
->salt
, expected
->salt_len
,
1435 expected
->id
, expected
->iter
, expected
->key_len
,
1436 key
, expected
->md
) == 0) {
1437 t
->err
= "PKCS12_ERROR";
1441 if (!TEST_mem_eq(expected
->key
, expected
->key_len
,
1442 key
, expected
->key_len
)) {
1443 t
->err
= "KEY_MISMATCH";
1452 static const EVP_TEST_METHOD pbe_test_method
= {
1466 BASE64_CANONICAL_ENCODING
= 0,
1467 BASE64_VALID_ENCODING
= 1,
1468 BASE64_INVALID_ENCODING
= 2
1469 } base64_encoding_type
;
1471 typedef struct encode_data_st
{
1472 /* Input to encoding */
1473 unsigned char *input
;
1475 /* Expected output */
1476 unsigned char *output
;
1478 base64_encoding_type encoding
;
1481 static int encode_test_init(EVP_TEST
*t
, const char *encoding
)
1485 if (!TEST_ptr(edata
= OPENSSL_zalloc(sizeof(*edata
))))
1487 if (strcmp(encoding
, "canonical") == 0) {
1488 edata
->encoding
= BASE64_CANONICAL_ENCODING
;
1489 } else if (strcmp(encoding
, "valid") == 0) {
1490 edata
->encoding
= BASE64_VALID_ENCODING
;
1491 } else if (strcmp(encoding
, "invalid") == 0) {
1492 edata
->encoding
= BASE64_INVALID_ENCODING
;
1493 if (!TEST_ptr(t
->expected_err
= OPENSSL_strdup("DECODE_ERROR")))
1496 TEST_error("Bad encoding: %s."
1497 " Should be one of {canonical, valid, invalid}",
1505 static void encode_test_cleanup(EVP_TEST
*t
)
1507 ENCODE_DATA
*edata
= t
->data
;
1509 OPENSSL_free(edata
->input
);
1510 OPENSSL_free(edata
->output
);
1511 memset(edata
, 0, sizeof(*edata
));
1514 static int encode_test_parse(EVP_TEST
*t
,
1515 const char *keyword
, const char *value
)
1517 ENCODE_DATA
*edata
= t
->data
;
1519 if (strcmp(keyword
, "Input") == 0)
1520 return parse_bin(value
, &edata
->input
, &edata
->input_len
);
1521 if (strcmp(keyword
, "Output") == 0)
1522 return parse_bin(value
, &edata
->output
, &edata
->output_len
);
1526 static int encode_test_run(EVP_TEST
*t
)
1528 ENCODE_DATA
*expected
= t
->data
;
1529 unsigned char *encode_out
= NULL
, *decode_out
= NULL
;
1530 int output_len
, chunk_len
;
1531 EVP_ENCODE_CTX
*decode_ctx
;
1533 if (!TEST_ptr(decode_ctx
= EVP_ENCODE_CTX_new())) {
1534 t
->err
= "INTERNAL_ERROR";
1538 if (expected
->encoding
== BASE64_CANONICAL_ENCODING
) {
1539 EVP_ENCODE_CTX
*encode_ctx
;
1541 if (!TEST_ptr(encode_ctx
= EVP_ENCODE_CTX_new())
1542 || !TEST_ptr(encode_out
=
1543 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected
->input_len
))))
1546 EVP_EncodeInit(encode_ctx
);
1547 EVP_EncodeUpdate(encode_ctx
, encode_out
, &chunk_len
,
1548 expected
->input
, expected
->input_len
);
1549 output_len
= chunk_len
;
1551 EVP_EncodeFinal(encode_ctx
, encode_out
+ chunk_len
, &chunk_len
);
1552 output_len
+= chunk_len
;
1554 EVP_ENCODE_CTX_free(encode_ctx
);
1556 if (!TEST_mem_eq(expected
->output
, expected
->output_len
,
1557 encode_out
, output_len
)) {
1558 t
->err
= "BAD_ENCODING";
1563 if (!TEST_ptr(decode_out
=
1564 OPENSSL_malloc(EVP_DECODE_LENGTH(expected
->output_len
))))
1567 EVP_DecodeInit(decode_ctx
);
1568 if (EVP_DecodeUpdate(decode_ctx
, decode_out
, &chunk_len
, expected
->output
,
1569 expected
->output_len
) < 0) {
1570 t
->err
= "DECODE_ERROR";
1573 output_len
= chunk_len
;
1575 if (EVP_DecodeFinal(decode_ctx
, decode_out
+ chunk_len
, &chunk_len
) != 1) {
1576 t
->err
= "DECODE_ERROR";
1579 output_len
+= chunk_len
;
1581 if (expected
->encoding
!= BASE64_INVALID_ENCODING
1582 && !TEST_mem_eq(expected
->input
, expected
->input_len
,
1583 decode_out
, output_len
)) {
1584 t
->err
= "BAD_DECODING";
1590 OPENSSL_free(encode_out
);
1591 OPENSSL_free(decode_out
);
1592 EVP_ENCODE_CTX_free(decode_ctx
);
1596 static const EVP_TEST_METHOD encode_test_method
= {
1599 encode_test_cleanup
,
1608 typedef struct kdf_data_st
{
1609 /* Context for this operation */
1611 /* Expected output */
1612 unsigned char *output
;
1617 * Perform public key operation setup: lookup key, allocated ctx and call
1618 * the appropriate initialisation function
1620 static int kdf_test_init(EVP_TEST
*t
, const char *name
)
1624 if (!TEST_ptr(kdata
= OPENSSL_zalloc(sizeof(*kdata
))))
1626 kdata
->ctx
= EVP_PKEY_CTX_new_id(OBJ_sn2nid(name
), NULL
);
1627 if (kdata
->ctx
== NULL
)
1629 if (EVP_PKEY_derive_init(kdata
->ctx
) <= 0)
1635 static void kdf_test_cleanup(EVP_TEST
*t
)
1637 KDF_DATA
*kdata
= t
->data
;
1638 OPENSSL_free(kdata
->output
);
1639 EVP_PKEY_CTX_free(kdata
->ctx
);
1642 static int kdf_test_parse(EVP_TEST
*t
,
1643 const char *keyword
, const char *value
)
1645 KDF_DATA
*kdata
= t
->data
;
1647 if (strcmp(keyword
, "Output") == 0)
1648 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
1649 if (strncmp(keyword
, "Ctrl", 4) == 0)
1650 return pkey_test_ctrl(t
, kdata
->ctx
, value
);
1654 static int kdf_test_run(EVP_TEST
*t
)
1656 KDF_DATA
*expected
= t
->data
;
1657 unsigned char *got
= NULL
;
1658 size_t got_len
= expected
->output_len
;
1660 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1661 t
->err
= "INTERNAL_ERROR";
1664 if (EVP_PKEY_derive(expected
->ctx
, got
, &got_len
) <= 0) {
1665 t
->err
= "KDF_DERIVE_ERROR";
1668 if (!TEST_mem_eq(expected
->output
, expected
->output_len
, got
, got_len
)) {
1669 t
->err
= "KDF_MISMATCH";
1679 static const EVP_TEST_METHOD kdf_test_method
= {
1692 typedef struct keypair_test_data_st
{
1695 } KEYPAIR_TEST_DATA
;
1697 static int keypair_test_init(EVP_TEST
*t
, const char *pair
)
1699 KEYPAIR_TEST_DATA
*data
;
1701 EVP_PKEY
*pk
= NULL
, *pubk
= NULL
;
1702 char *pub
, *priv
= NULL
;
1704 /* Split private and public names. */
1705 if (!TEST_ptr(priv
= OPENSSL_strdup(pair
))
1706 || !TEST_ptr(pub
= strchr(priv
, ':'))) {
1707 t
->err
= "PARSING_ERROR";
1712 if (!TEST_true(find_key(&pk
, priv
, private_keys
))) {
1713 TEST_info("Can't find private key: %s", priv
);
1714 t
->err
= "MISSING_PRIVATE_KEY";
1717 if (!TEST_true(find_key(&pubk
, pub
, public_keys
))) {
1718 TEST_info("Can't find public key: %s", pub
);
1719 t
->err
= "MISSING_PUBLIC_KEY";
1723 if (pk
== NULL
&& pubk
== NULL
) {
1724 /* Both keys are listed but unsupported: skip this test */
1730 if (!TEST_ptr(data
= OPENSSL_malloc(sizeof(*data
))))
1743 static void keypair_test_cleanup(EVP_TEST
*t
)
1745 OPENSSL_free(t
->data
);
1750 * For tests that do not accept any custom keywords.
1752 static int void_test_parse(EVP_TEST
*t
, const char *keyword
, const char *value
)
1757 static int keypair_test_run(EVP_TEST
*t
)
1760 const KEYPAIR_TEST_DATA
*pair
= t
->data
;
1762 if (pair
->privk
== NULL
|| pair
->pubk
== NULL
) {
1764 * this can only happen if only one of the keys is not set
1765 * which means that one of them was unsupported while the
1766 * other isn't: hence a key type mismatch.
1768 t
->err
= "KEYPAIR_TYPE_MISMATCH";
1773 if ((rv
= EVP_PKEY_cmp(pair
->privk
, pair
->pubk
)) != 1 ) {
1775 t
->err
= "KEYPAIR_MISMATCH";
1776 } else if ( -1 == rv
) {
1777 t
->err
= "KEYPAIR_TYPE_MISMATCH";
1778 } else if ( -2 == rv
) {
1779 t
->err
= "UNSUPPORTED_KEY_COMPARISON";
1781 TEST_error("Unexpected error in key comparison");
1796 static const EVP_TEST_METHOD keypair_test_method
= {
1799 keypair_test_cleanup
,
1806 *** DIGEST SIGN+VERIFY TESTS
1810 int is_verify
; /* Set to 1 if verifying */
1811 int is_oneshot
; /* Set to 1 for one shot operation */
1812 const EVP_MD
*md
; /* Digest to use */
1813 EVP_MD_CTX
*ctx
; /* Digest context */
1815 STACK_OF(EVP_TEST_BUFFER
) *input
; /* Input data: streaming */
1816 unsigned char *osin
; /* Input data if one shot */
1817 size_t osin_len
; /* Input length data if one shot */
1818 unsigned char *output
; /* Expected output */
1819 size_t output_len
; /* Expected output length */
1822 static int digestsigver_test_init(EVP_TEST
*t
, const char *alg
, int is_verify
,
1825 const EVP_MD
*md
= NULL
;
1826 DIGESTSIGN_DATA
*mdat
;
1828 if (strcmp(alg
, "NULL") != 0) {
1829 if ((md
= EVP_get_digestbyname(alg
)) == NULL
) {
1830 /* If alg has an OID assume disabled algorithm */
1831 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
1838 if (!TEST_ptr(mdat
= OPENSSL_zalloc(sizeof(*mdat
))))
1841 if (!TEST_ptr(mdat
->ctx
= EVP_MD_CTX_new())) {
1845 mdat
->is_verify
= is_verify
;
1846 mdat
->is_oneshot
= is_oneshot
;
1851 static int digestsign_test_init(EVP_TEST
*t
, const char *alg
)
1853 return digestsigver_test_init(t
, alg
, 0, 0);
1856 static void digestsigver_test_cleanup(EVP_TEST
*t
)
1858 DIGESTSIGN_DATA
*mdata
= t
->data
;
1860 EVP_MD_CTX_free(mdata
->ctx
);
1861 sk_EVP_TEST_BUFFER_pop_free(mdata
->input
, evp_test_buffer_free
);
1862 OPENSSL_free(mdata
->osin
);
1863 OPENSSL_free(mdata
->output
);
1864 OPENSSL_free(mdata
);
1868 static int digestsigver_test_parse(EVP_TEST
*t
,
1869 const char *keyword
, const char *value
)
1871 DIGESTSIGN_DATA
*mdata
= t
->data
;
1873 if (strcmp(keyword
, "Key") == 0) {
1874 EVP_PKEY
*pkey
= NULL
;
1877 if (mdata
->is_verify
)
1878 rv
= find_key(&pkey
, value
, public_keys
);
1880 rv
= find_key(&pkey
, value
, private_keys
);
1881 if (rv
== 0 || pkey
== NULL
) {
1885 if (mdata
->is_verify
) {
1886 if (!EVP_DigestVerifyInit(mdata
->ctx
, &mdata
->pctx
, mdata
->md
,
1888 t
->err
= "DIGESTVERIFYINIT_ERROR";
1891 if (!EVP_DigestSignInit(mdata
->ctx
, &mdata
->pctx
, mdata
->md
, NULL
,
1893 t
->err
= "DIGESTSIGNINIT_ERROR";
1897 if (strcmp(keyword
, "Input") == 0) {
1898 if (mdata
->is_oneshot
)
1899 return parse_bin(value
, &mdata
->osin
, &mdata
->osin_len
);
1900 return evp_test_buffer_append(value
, &mdata
->input
);
1902 if (strcmp(keyword
, "Output") == 0)
1903 return parse_bin(value
, &mdata
->output
, &mdata
->output_len
);
1905 if (!mdata
->is_oneshot
) {
1906 if (strcmp(keyword
, "Count") == 0)
1907 return evp_test_buffer_set_count(value
, mdata
->input
);
1908 if (strcmp(keyword
, "Ncopy") == 0)
1909 return evp_test_buffer_ncopy(value
, mdata
->input
);
1911 if (strcmp(keyword
, "Ctrl") == 0) {
1912 if (mdata
->pctx
== NULL
)
1914 return pkey_test_ctrl(t
, mdata
->pctx
, value
);
1919 static int digestsign_update_fn(void *ctx
, const unsigned char *buf
,
1922 return EVP_DigestSignUpdate(ctx
, buf
, buflen
);
1925 static int digestsign_test_run(EVP_TEST
*t
)
1927 DIGESTSIGN_DATA
*expected
= t
->data
;
1928 unsigned char *got
= NULL
;
1931 if (!evp_test_buffer_do(expected
->input
, digestsign_update_fn
,
1933 t
->err
= "DIGESTUPDATE_ERROR";
1937 if (!EVP_DigestSignFinal(expected
->ctx
, NULL
, &got_len
)) {
1938 t
->err
= "DIGESTSIGNFINAL_LENGTH_ERROR";
1941 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1942 t
->err
= "MALLOC_FAILURE";
1945 if (!EVP_DigestSignFinal(expected
->ctx
, got
, &got_len
)) {
1946 t
->err
= "DIGESTSIGNFINAL_ERROR";
1949 if (!TEST_mem_eq(expected
->output
, expected
->output_len
, got
, got_len
)) {
1950 t
->err
= "SIGNATURE_MISMATCH";
1959 static const EVP_TEST_METHOD digestsign_test_method
= {
1961 digestsign_test_init
,
1962 digestsigver_test_cleanup
,
1963 digestsigver_test_parse
,
1967 static int digestverify_test_init(EVP_TEST
*t
, const char *alg
)
1969 return digestsigver_test_init(t
, alg
, 1, 0);
1972 static int digestverify_update_fn(void *ctx
, const unsigned char *buf
,
1975 return EVP_DigestVerifyUpdate(ctx
, buf
, buflen
);
1978 static int digestverify_test_run(EVP_TEST
*t
)
1980 DIGESTSIGN_DATA
*mdata
= t
->data
;
1982 if (!evp_test_buffer_do(mdata
->input
, digestverify_update_fn
, mdata
->ctx
)) {
1983 t
->err
= "DIGESTUPDATE_ERROR";
1987 if (EVP_DigestVerifyFinal(mdata
->ctx
, mdata
->output
,
1988 mdata
->output_len
) <= 0)
1989 t
->err
= "VERIFY_ERROR";
1993 static const EVP_TEST_METHOD digestverify_test_method
= {
1995 digestverify_test_init
,
1996 digestsigver_test_cleanup
,
1997 digestsigver_test_parse
,
1998 digestverify_test_run
2001 static int oneshot_digestsign_test_init(EVP_TEST
*t
, const char *alg
)
2003 return digestsigver_test_init(t
, alg
, 0, 1);
2006 static int oneshot_digestsign_test_run(EVP_TEST
*t
)
2008 DIGESTSIGN_DATA
*expected
= t
->data
;
2009 unsigned char *got
= NULL
;
2012 if (!EVP_DigestSign(expected
->ctx
, NULL
, &got_len
,
2013 expected
->osin
, expected
->osin_len
)) {
2014 t
->err
= "DIGESTSIGN_LENGTH_ERROR";
2017 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
2018 t
->err
= "MALLOC_FAILURE";
2021 if (!EVP_DigestSign(expected
->ctx
, got
, &got_len
,
2022 expected
->osin
, expected
->osin_len
)) {
2023 t
->err
= "DIGESTSIGN_ERROR";
2026 if (!TEST_mem_eq(expected
->output
, expected
->output_len
, got
, got_len
)) {
2027 t
->err
= "SIGNATURE_MISMATCH";
2036 static const EVP_TEST_METHOD oneshot_digestsign_test_method
= {
2037 "OneShotDigestSign",
2038 oneshot_digestsign_test_init
,
2039 digestsigver_test_cleanup
,
2040 digestsigver_test_parse
,
2041 oneshot_digestsign_test_run
2044 static int oneshot_digestverify_test_init(EVP_TEST
*t
, const char *alg
)
2046 return digestsigver_test_init(t
, alg
, 1, 1);
2049 static int oneshot_digestverify_test_run(EVP_TEST
*t
)
2051 DIGESTSIGN_DATA
*mdata
= t
->data
;
2053 if (EVP_DigestVerify(mdata
->ctx
, mdata
->output
, mdata
->output_len
,
2054 mdata
->osin
, mdata
->osin_len
) <= 0)
2055 t
->err
= "VERIFY_ERROR";
2059 static const EVP_TEST_METHOD oneshot_digestverify_test_method
= {
2060 "OneShotDigestVerify",
2061 oneshot_digestverify_test_init
,
2062 digestsigver_test_cleanup
,
2063 digestsigver_test_parse
,
2064 oneshot_digestverify_test_run
2069 *** PARSING AND DISPATCH
2072 static const EVP_TEST_METHOD
*evp_test_list
[] = {
2073 &cipher_test_method
,
2074 &digest_test_method
,
2075 &digestsign_test_method
,
2076 &digestverify_test_method
,
2077 &encode_test_method
,
2079 &keypair_test_method
,
2081 &oneshot_digestsign_test_method
,
2082 &oneshot_digestverify_test_method
,
2084 &pdecrypt_test_method
,
2085 &pderive_test_method
,
2087 &pverify_recover_test_method
,
2088 &pverify_test_method
,
2092 static const EVP_TEST_METHOD
*find_test(const char *name
)
2094 const EVP_TEST_METHOD
**tt
;
2096 for (tt
= evp_test_list
; *tt
; tt
++) {
2097 if (strcmp(name
, (*tt
)->name
) == 0)
2103 static void clear_test(EVP_TEST
*t
)
2105 test_clearstanza(&t
->s
);
2107 if (t
->data
!= NULL
) {
2108 if (t
->meth
!= NULL
)
2109 t
->meth
->cleanup(t
);
2110 OPENSSL_free(t
->data
);
2113 OPENSSL_free(t
->expected_err
);
2114 t
->expected_err
= NULL
;
2115 OPENSSL_free(t
->func
);
2117 OPENSSL_free(t
->reason
);
2127 * Check for errors in the test structure; return 1 if okay, else 0.
2129 static int check_test_error(EVP_TEST
*t
)
2135 if (t
->err
== NULL
&& t
->expected_err
== NULL
)
2137 if (t
->err
!= NULL
&& t
->expected_err
== NULL
) {
2138 if (t
->aux_err
!= NULL
) {
2139 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
2140 t
->s
.test_file
, t
->s
.start
, t
->aux_err
, t
->err
);
2142 TEST_info("%s:%d: Source of above error; unexpected error %s",
2143 t
->s
.test_file
, t
->s
.start
, t
->err
);
2147 if (t
->err
== NULL
&& t
->expected_err
!= NULL
) {
2148 TEST_info("%s:%d: Succeeded but was expecting %s",
2149 t
->s
.test_file
, t
->s
.start
, t
->expected_err
);
2153 if (strcmp(t
->err
, t
->expected_err
) != 0) {
2154 TEST_info("%s:%d: Expected %s got %s",
2155 t
->s
.test_file
, t
->s
.start
, t
->expected_err
, t
->err
);
2159 if (t
->func
== NULL
&& t
->reason
== NULL
)
2162 if (t
->func
== NULL
|| t
->reason
== NULL
) {
2163 TEST_info("%s:%d: Test is missing function or reason code",
2164 t
->s
.test_file
, t
->s
.start
);
2168 err
= ERR_peek_error();
2170 TEST_info("%s:%d: Expected error \"%s:%s\" not set",
2171 t
->s
.test_file
, t
->s
.start
, t
->func
, t
->reason
);
2175 func
= ERR_func_error_string(err
);
2176 reason
= ERR_reason_error_string(err
);
2177 if (func
== NULL
&& reason
== NULL
) {
2178 TEST_info("%s:%d: Expected error \"%s:%s\", no strings available."
2180 t
->s
.test_file
, t
->s
.start
, t
->func
, t
->reason
);
2184 if (strcmp(func
, t
->func
) == 0 && strcmp(reason
, t
->reason
) == 0)
2187 TEST_info("%s:%d: Expected error \"%s:%s\", got \"%s:%s\"",
2188 t
->s
.test_file
, t
->s
.start
, t
->func
, t
->reason
, func
, reason
);
2194 * Run a parsed test. Log a message and return 0 on error.
2196 static int run_test(EVP_TEST
*t
)
2198 if (t
->meth
== NULL
)
2205 if (t
->err
== NULL
&& t
->meth
->run_test(t
) != 1) {
2206 TEST_info("%s:%d %s error",
2207 t
->s
.test_file
, t
->s
.start
, t
->meth
->name
);
2210 if (!check_test_error(t
)) {
2211 test_openssl_errors();
2220 static int find_key(EVP_PKEY
**ppk
, const char *name
, KEY_LIST
*lst
)
2222 for (; lst
!= NULL
; lst
= lst
->next
) {
2223 if (strcmp(lst
->name
, name
) == 0) {
2232 static void free_key_list(KEY_LIST
*lst
)
2234 while (lst
!= NULL
) {
2235 KEY_LIST
*next
= lst
->next
;
2237 EVP_PKEY_free(lst
->key
);
2238 OPENSSL_free(lst
->name
);
2245 * Is the key type an unsupported algorithm?
2247 static int key_unsupported()
2249 long err
= ERR_peek_error();
2251 if (ERR_GET_LIB(err
) == ERR_LIB_EVP
2252 && ERR_GET_REASON(err
) == EVP_R_UNSUPPORTED_ALGORITHM
) {
2256 #ifndef OPENSSL_NO_EC
2258 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
2259 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
2262 if (ERR_GET_LIB(err
) == ERR_LIB_EC
2263 && ERR_GET_REASON(err
) == EC_R_UNKNOWN_GROUP
) {
2267 #endif /* OPENSSL_NO_EC */
2272 * NULL out the value from |pp| but return it. This "steals" a pointer.
2274 static char *take_value(PAIR
*pp
)
2276 char *p
= pp
->value
;
2283 * Read and parse one test. Return 0 if failure, 1 if okay.
2285 static int parse(EVP_TEST
*t
)
2287 KEY_LIST
*key
, **klist
;
2294 if (BIO_eof(t
->s
.fp
))
2297 if (!test_readstanza(&t
->s
))
2299 } while (t
->s
.numpairs
== 0);
2300 pp
= &t
->s
.pairs
[0];
2302 /* Are we adding a key? */
2305 if (strcmp(pp
->key
, "PrivateKey") == 0) {
2306 pkey
= PEM_read_bio_PrivateKey(t
->s
.key
, NULL
, 0, NULL
);
2307 if (pkey
== NULL
&& !key_unsupported()) {
2308 TEST_info("Can't read private key %s", pp
->value
);
2309 ERR_print_errors_fp(stderr
);
2312 klist
= &private_keys
;
2314 else if (strcmp(pp
->key
, "PublicKey") == 0) {
2315 pkey
= PEM_read_bio_PUBKEY(t
->s
.key
, NULL
, 0, NULL
);
2316 if (pkey
== NULL
&& !key_unsupported()) {
2317 TEST_info("Can't read public key %s", pp
->value
);
2318 ERR_print_errors_fp(stderr
);
2321 klist
= &public_keys
;
2324 /* If we have a key add to list */
2325 if (klist
!= NULL
) {
2326 if (find_key(NULL
, pp
->value
, *klist
)) {
2327 TEST_info("Duplicate key %s", pp
->value
);
2330 if (!TEST_ptr(key
= OPENSSL_malloc(sizeof(*key
))))
2332 key
->name
= take_value(pp
);
2337 /* Go back and start a new stanza. */
2338 if (t
->s
.numpairs
!= 1)
2339 TEST_info("Line %d: missing blank line\n", t
->s
.curr
);
2343 /* Find the test, based on first keyword. */
2344 if (!TEST_ptr(t
->meth
= find_test(pp
->key
)))
2346 if (!t
->meth
->init(t
, pp
->value
)) {
2347 TEST_error("unknown %s: %s\n", pp
->key
, pp
->value
);
2351 /* TEST_info("skipping %s %s", pp->key, pp->value); */
2355 for (pp
++, i
= 1; i
< t
->s
.numpairs
; pp
++, i
++) {
2356 if (strcmp(pp
->key
, "Result") == 0) {
2357 if (t
->expected_err
!= NULL
) {
2358 TEST_info("Line %d: multiple result lines", t
->s
.curr
);
2361 t
->expected_err
= take_value(pp
);
2362 } else if (strcmp(pp
->key
, "Function") == 0) {
2363 if (t
->func
!= NULL
) {
2364 TEST_info("Line %d: multiple function lines\n", t
->s
.curr
);
2367 t
->func
= take_value(pp
);
2368 } else if (strcmp(pp
->key
, "Reason") == 0) {
2369 if (t
->reason
!= NULL
) {
2370 TEST_info("Line %d: multiple reason lines", t
->s
.curr
);
2373 t
->reason
= take_value(pp
);
2375 /* Must be test specific line: try to parse it */
2376 int rv
= t
->meth
->parse(t
, pp
->key
, pp
->value
);
2379 TEST_info("Line %d: unknown keyword %s", t
->s
.curr
, pp
->key
);
2383 TEST_info("Line %d: error processing keyword %s\n",
2384 t
->s
.curr
, pp
->key
);
2393 static char * const *testfiles
;
2395 static int run_file_tests(int i
)
2400 if (!TEST_ptr(t
= OPENSSL_zalloc(sizeof(*t
))))
2402 if (!test_start_file(&t
->s
, testfiles
[i
])) {
2407 while (!BIO_eof(t
->s
.fp
)) {
2411 if (c
== 0 || !run_test(t
)) {
2416 test_end_file(&t
->s
);
2419 free_key_list(public_keys
);
2420 free_key_list(private_keys
);
2427 int test_main(int argc
, char *argv
[])
2430 TEST_error("Usage: %s file...", argv
[0]);
2433 testfiles
= &argv
[1];
2435 ADD_ALL_TESTS(run_file_tests
, argc
- 1);
2437 return run_tests(argv
[0]);