2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
58 #include <openssl/evp.h>
59 #include <openssl/pem.h>
60 #include <openssl/err.h>
61 #include <openssl/x509v3.h>
62 #include <openssl/pkcs12.h>
63 #include <openssl/kdf.h>
64 #include "internal/numbers.h"
66 /* Remove spaces from beginning and end of a string */
68 static void remove_space(char **pval
)
70 unsigned char *p
= (unsigned char *)*pval
;
77 p
= p
+ strlen(*pval
) - 1;
79 /* Remove trailing space */
85 * Given a line of the form:
86 * name = value # comment
87 * extract name and value. NB: modifies passed buffer.
90 static int parse_line(char **pkw
, char **pval
, char *linebuf
)
94 p
= linebuf
+ strlen(linebuf
) - 1;
97 fprintf(stderr
, "FATAL: missing EOL\n");
103 p
= strchr(linebuf
, '#');
108 /* Look for = sign */
109 p
= strchr(linebuf
, '=');
120 /* Remove spaces from keyword and value */
128 * Unescape some escape sequences in string literals.
129 * Return the result in a newly allocated buffer.
130 * Currently only supports '\n'.
131 * If the input length is 0, returns a valid 1-byte buffer, but sets
134 static unsigned char* unescape(const char *input
, size_t input_len
,
137 unsigned char *ret
, *p
;
139 if (input_len
== 0) {
141 return OPENSSL_zalloc(1);
144 /* Escaping is non-expanding; over-allocate original size for simplicity. */
145 ret
= p
= OPENSSL_malloc(input_len
);
149 for (i
= 0; i
< input_len
; i
++) {
150 if (input
[i
] == '\\') {
151 if (i
== input_len
- 1 || input
[i
+1] != 'n')
168 /* For a hex string "value" convert to a binary allocated buffer */
169 static int test_bin(const char *value
, unsigned char **buf
, size_t *buflen
)
174 * Don't return NULL for zero length buffer.
175 * This is needed for some tests with empty keys: HMAC_Init_ex() expects
176 * a non-NULL key buffer even if the key length is 0, in order to detect
179 *buf
= OPENSSL_malloc(1);
186 /* Check for string literal */
187 if (value
[0] == '"') {
190 vlen
= strlen(value
);
191 if (value
[vlen
- 1] != '"')
194 *buf
= unescape(value
, vlen
, buflen
);
200 *buf
= string_to_hex(value
, &len
);
202 fprintf(stderr
, "Value=%s\n", value
);
203 ERR_print_errors_fp(stderr
);
206 /* Size of input buffer means we'll never overflow */
210 /* Parse unsigned decimal 64 bit integer value */
211 static int test_uint64(const char *value
, uint64_t *pr
)
213 const char *p
= value
;
215 fprintf(stderr
, "Invalid empty integer value\n");
220 if (*pr
> UINT64_MAX
/10) {
221 fprintf(stderr
, "Integer string overflow value=%s\n", value
);
225 if (*p
< '0' || *p
> '9') {
226 fprintf(stderr
, "Invalid integer string value=%s\n", value
);
235 /* Structure holding test information */
237 /* file being read */
239 /* List of public and private keys */
240 struct key_list
*private;
241 struct key_list
*public;
242 /* method for this test */
243 const struct evp_test_method
*meth
;
244 /* current line being processed */
246 /* start line of current test */
247 unsigned int start_line
;
248 /* Error string for test */
250 /* Expected error value of test */
252 /* Number of tests */
256 /* Number of tests skipped */
258 /* If output mismatch expected and got value */
259 unsigned char *out_received
;
260 size_t out_received_len
;
261 unsigned char *out_expected
;
262 size_t out_expected_len
;
263 /* test specific data */
265 /* Current test should be skipped */
272 struct key_list
*next
;
275 /* Test method structure */
276 struct evp_test_method
{
277 /* Name of test as it appears in file */
279 /* Initialise test for "alg" */
280 int (*init
) (struct evp_test
* t
, const char *alg
);
281 /* Clean up method */
282 void (*cleanup
) (struct evp_test
* t
);
283 /* Test specific name value pair processing */
284 int (*parse
) (struct evp_test
* t
, const char *name
, const char *value
);
285 /* Run the test itself */
286 int (*run_test
) (struct evp_test
* t
);
289 static const struct evp_test_method digest_test_method
, cipher_test_method
;
290 static const struct evp_test_method mac_test_method
;
291 static const struct evp_test_method psign_test_method
, pverify_test_method
;
292 static const struct evp_test_method pdecrypt_test_method
;
293 static const struct evp_test_method pverify_recover_test_method
;
294 static const struct evp_test_method pbe_test_method
;
295 static const struct evp_test_method encode_test_method
;
296 static const struct evp_test_method kdf_test_method
;
298 static const struct evp_test_method
*evp_test_list
[] = {
303 &pverify_test_method
,
304 &pdecrypt_test_method
,
305 &pverify_recover_test_method
,
312 static const struct evp_test_method
*evp_find_test(const char *name
)
314 const struct evp_test_method
**tt
;
316 for (tt
= evp_test_list
; *tt
; tt
++) {
317 if (strcmp(name
, (*tt
)->name
) == 0)
323 static void hex_print(const char *name
, const unsigned char *buf
, size_t len
)
326 fprintf(stderr
, "%s ", name
);
327 for (i
= 0; i
< len
; i
++)
328 fprintf(stderr
, "%02X", buf
[i
]);
332 static void free_expected(struct evp_test
*t
)
334 OPENSSL_free(t
->expected_err
);
335 t
->expected_err
= NULL
;
336 OPENSSL_free(t
->out_expected
);
337 OPENSSL_free(t
->out_received
);
338 t
->out_expected
= NULL
;
339 t
->out_received
= NULL
;
340 t
->out_expected_len
= 0;
341 t
->out_received_len
= 0;
346 static void print_expected(struct evp_test
*t
)
348 if (t
->out_expected
== NULL
&& t
->out_received
== NULL
)
350 hex_print("Expected:", t
->out_expected
, t
->out_expected_len
);
351 hex_print("Got: ", t
->out_received
, t
->out_received_len
);
355 static int check_test_error(struct evp_test
*t
)
357 if (!t
->err
&& !t
->expected_err
)
359 if (t
->err
&& !t
->expected_err
) {
360 fprintf(stderr
, "Test line %d: unexpected error %s\n",
361 t
->start_line
, t
->err
);
365 if (!t
->err
&& t
->expected_err
) {
366 fprintf(stderr
, "Test line %d: succeeded expecting %s\n",
367 t
->start_line
, t
->expected_err
);
370 if (strcmp(t
->err
, t
->expected_err
) == 0)
373 fprintf(stderr
, "Test line %d: expecting %s got %s\n",
374 t
->start_line
, t
->expected_err
, t
->err
);
378 /* Setup a new test, run any existing test */
380 static int setup_test(struct evp_test
*t
, const struct evp_test_method
*tmeth
)
382 /* If we already have a test set up run it */
391 if (t
->meth
->run_test(t
) != 1) {
392 fprintf(stderr
, "%s test error line %d\n",
393 t
->meth
->name
, t
->start_line
);
396 if (!check_test_error(t
)) {
398 ERR_print_errors_fp(stderr
);
403 OPENSSL_free(t
->data
);
405 OPENSSL_free(t
->expected_err
);
406 t
->expected_err
= NULL
;
413 static int find_key(EVP_PKEY
**ppk
, const char *name
, struct key_list
*lst
)
415 for (; lst
; lst
= lst
->next
) {
416 if (strcmp(lst
->name
, name
) == 0) {
425 static void free_key_list(struct key_list
*lst
)
427 while (lst
!= NULL
) {
428 struct key_list
*ltmp
;
429 EVP_PKEY_free(lst
->key
);
430 OPENSSL_free(lst
->name
);
437 static int check_unsupported()
439 long err
= ERR_peek_error();
440 if (ERR_GET_LIB(err
) == ERR_LIB_EVP
441 && ERR_GET_REASON(err
) == EVP_R_UNSUPPORTED_ALGORITHM
) {
448 static int process_test(struct evp_test
*t
, char *buf
, int verbose
)
450 char *keyword
= NULL
, *value
= NULL
;
451 int rv
= 0, add_key
= 0;
453 struct key_list
**lst
= NULL
, *key
= NULL
;
455 const struct evp_test_method
*tmeth
= NULL
;
458 if (!parse_line(&keyword
, &value
, buf
))
460 if (strcmp(keyword
, "PrivateKey") == 0) {
461 save_pos
= ftell(t
->in
);
462 pk
= PEM_read_PrivateKey(t
->in
, NULL
, 0, NULL
);
463 if (pk
== NULL
&& !check_unsupported()) {
464 fprintf(stderr
, "Error reading private key %s\n", value
);
465 ERR_print_errors_fp(stderr
);
471 if (strcmp(keyword
, "PublicKey") == 0) {
472 save_pos
= ftell(t
->in
);
473 pk
= PEM_read_PUBKEY(t
->in
, NULL
, 0, NULL
);
474 if (pk
== NULL
&& !check_unsupported()) {
475 fprintf(stderr
, "Error reading public key %s\n", value
);
476 ERR_print_errors_fp(stderr
);
482 /* If we have a key add to list */
485 if (find_key(NULL
, value
, *lst
)) {
486 fprintf(stderr
, "Duplicate key %s\n", value
);
489 key
= OPENSSL_malloc(sizeof(*key
));
492 key
->name
= OPENSSL_strdup(value
);
496 /* Rewind input, read to end and update line numbers */
497 fseek(t
->in
, save_pos
, SEEK_SET
);
498 while (fgets(tmpbuf
, sizeof(tmpbuf
), t
->in
)) {
500 if (strncmp(tmpbuf
, "-----END", 8) == 0)
503 fprintf(stderr
, "Can't find key end\n");
507 /* See if keyword corresponds to a test start */
508 tmeth
= evp_find_test(keyword
);
510 if (!setup_test(t
, tmeth
))
512 t
->start_line
= t
->line
;
514 if (!tmeth
->init(t
, value
)) {
515 fprintf(stderr
, "Unknown %s: %s\n", keyword
, value
);
519 } else if (t
->skip
) {
521 } else if (strcmp(keyword
, "Result") == 0) {
522 if (t
->expected_err
) {
523 fprintf(stderr
, "Line %d: multiple result lines\n", t
->line
);
526 t
->expected_err
= OPENSSL_strdup(value
);
527 if (!t
->expected_err
)
530 /* Must be test specific line: try to parse it */
532 rv
= t
->meth
->parse(t
, keyword
, value
);
535 fprintf(stderr
, "line %d: unexpected keyword %s\n",
539 fprintf(stderr
, "line %d: error processing keyword %s\n",
547 static int check_var_length_output(struct evp_test
*t
,
548 const unsigned char *expected
,
550 const unsigned char *received
,
553 if (expected_len
== received_len
&&
554 memcmp(expected
, received
, expected_len
) == 0) {
558 /* The result printing code expects a non-NULL buffer. */
559 t
->out_expected
= OPENSSL_memdup(expected
, expected_len
? expected_len
: 1);
560 t
->out_expected_len
= expected_len
;
561 t
->out_received
= OPENSSL_memdup(received
, received_len
? received_len
: 1);
562 t
->out_received_len
= received_len
;
563 if (t
->out_expected
== NULL
|| t
->out_received
== NULL
) {
564 fprintf(stderr
, "Memory allocation error!\n");
570 static int check_output(struct evp_test
*t
,
571 const unsigned char *expected
,
572 const unsigned char *received
,
575 return check_var_length_output(t
, expected
, len
, received
, len
);
578 int main(int argc
, char **argv
)
585 fprintf(stderr
, "usage: evp_test testfile.txt\n");
589 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
591 ERR_load_crypto_strings();
592 OpenSSL_add_all_algorithms();
594 memset(&t
, 0, sizeof(t
));
596 in
= fopen(argv
[1], "r");
598 while (fgets(buf
, sizeof(buf
), in
)) {
600 if (!process_test(&t
, buf
, 0))
603 /* Run any final test we have */
604 if (!setup_test(&t
, NULL
))
606 fprintf(stderr
, "%d tests completed with %d errors, %d skipped\n",
607 t
.ntests
, t
.errors
, t
.nskip
);
608 free_key_list(t
.public);
609 free_key_list(t
.private);
612 CRYPTO_cleanup_all_ex_data();
613 ERR_remove_thread_state(NULL
);
615 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
616 CRYPTO_mem_leaks_fp(stderr
);
623 static void test_free(void *d
)
628 /* Message digest tests */
631 /* Digest this test is for */
632 const EVP_MD
*digest
;
633 /* Input to digest */
634 unsigned char *input
;
636 /* Repeat count for input */
638 /* Expected output */
639 unsigned char *output
;
643 static int digest_test_init(struct evp_test
*t
, const char *alg
)
645 const EVP_MD
*digest
;
646 struct digest_data
*mdat
;
647 digest
= EVP_get_digestbyname(alg
);
649 /* If alg has an OID assume disabled algorithm */
650 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
656 mdat
= OPENSSL_malloc(sizeof(*mdat
));
657 mdat
->digest
= digest
;
665 static void digest_test_cleanup(struct evp_test
*t
)
667 struct digest_data
*mdat
= t
->data
;
668 test_free(mdat
->input
);
669 test_free(mdat
->output
);
672 static int digest_test_parse(struct evp_test
*t
,
673 const char *keyword
, const char *value
)
675 struct digest_data
*mdata
= t
->data
;
676 if (strcmp(keyword
, "Input") == 0)
677 return test_bin(value
, &mdata
->input
, &mdata
->input_len
);
678 if (strcmp(keyword
, "Output") == 0)
679 return test_bin(value
, &mdata
->output
, &mdata
->output_len
);
680 if (strcmp(keyword
, "Count") == 0) {
681 long nrpt
= atoi(value
);
684 mdata
->nrpt
= (size_t)nrpt
;
690 static int digest_test_run(struct evp_test
*t
)
692 struct digest_data
*mdata
= t
->data
;
694 const char *err
= "INTERNAL_ERROR";
696 unsigned char md
[EVP_MAX_MD_SIZE
];
698 mctx
= EVP_MD_CTX_new();
701 err
= "DIGESTINIT_ERROR";
702 if (!EVP_DigestInit_ex(mctx
, mdata
->digest
, NULL
))
704 err
= "DIGESTUPDATE_ERROR";
705 for (i
= 0; i
< mdata
->nrpt
; i
++) {
706 if (!EVP_DigestUpdate(mctx
, mdata
->input
, mdata
->input_len
))
709 err
= "DIGESTFINAL_ERROR";
710 if (!EVP_DigestFinal(mctx
, md
, &md_len
))
712 err
= "DIGEST_LENGTH_MISMATCH";
713 if (md_len
!= mdata
->output_len
)
715 err
= "DIGEST_MISMATCH";
716 if (check_output(t
, mdata
->output
, md
, md_len
))
720 EVP_MD_CTX_free(mctx
);
725 static const struct evp_test_method digest_test_method
= {
735 const EVP_CIPHER
*cipher
;
737 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
743 unsigned char *plaintext
;
744 size_t plaintext_len
;
745 unsigned char *ciphertext
;
746 size_t ciphertext_len
;
754 static int cipher_test_init(struct evp_test
*t
, const char *alg
)
756 const EVP_CIPHER
*cipher
;
757 struct cipher_data
*cdat
= t
->data
;
758 cipher
= EVP_get_cipherbyname(alg
);
760 /* If alg has an OID assume disabled algorithm */
761 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
767 cdat
= OPENSSL_malloc(sizeof(*cdat
));
768 cdat
->cipher
= cipher
;
772 cdat
->ciphertext
= NULL
;
773 cdat
->plaintext
= NULL
;
777 if (EVP_CIPHER_mode(cipher
) == EVP_CIPH_GCM_MODE
778 || EVP_CIPHER_mode(cipher
) == EVP_CIPH_OCB_MODE
779 || EVP_CIPHER_mode(cipher
) == EVP_CIPH_CCM_MODE
)
780 cdat
->aead
= EVP_CIPHER_mode(cipher
);
781 else if (EVP_CIPHER_flags(cipher
) & EVP_CIPH_FLAG_AEAD_CIPHER
)
789 static void cipher_test_cleanup(struct evp_test
*t
)
791 struct cipher_data
*cdat
= t
->data
;
792 test_free(cdat
->key
);
794 test_free(cdat
->ciphertext
);
795 test_free(cdat
->plaintext
);
796 test_free(cdat
->aad
);
797 test_free(cdat
->tag
);
800 static int cipher_test_parse(struct evp_test
*t
, const char *keyword
,
803 struct cipher_data
*cdat
= t
->data
;
804 if (strcmp(keyword
, "Key") == 0)
805 return test_bin(value
, &cdat
->key
, &cdat
->key_len
);
806 if (strcmp(keyword
, "IV") == 0)
807 return test_bin(value
, &cdat
->iv
, &cdat
->iv_len
);
808 if (strcmp(keyword
, "Plaintext") == 0)
809 return test_bin(value
, &cdat
->plaintext
, &cdat
->plaintext_len
);
810 if (strcmp(keyword
, "Ciphertext") == 0)
811 return test_bin(value
, &cdat
->ciphertext
, &cdat
->ciphertext_len
);
813 if (strcmp(keyword
, "AAD") == 0)
814 return test_bin(value
, &cdat
->aad
, &cdat
->aad_len
);
815 if (strcmp(keyword
, "Tag") == 0)
816 return test_bin(value
, &cdat
->tag
, &cdat
->tag_len
);
819 if (strcmp(keyword
, "Operation") == 0) {
820 if (strcmp(value
, "ENCRYPT") == 0)
822 else if (strcmp(value
, "DECRYPT") == 0)
831 static int cipher_test_enc(struct evp_test
*t
, int enc
)
833 struct cipher_data
*cdat
= t
->data
;
834 unsigned char *in
, *out
, *tmp
= NULL
;
835 size_t in_len
, out_len
;
837 EVP_CIPHER_CTX
*ctx
= NULL
;
839 err
= "INTERNAL_ERROR";
840 ctx
= EVP_CIPHER_CTX_new();
843 EVP_CIPHER_CTX_set_flags(ctx
, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
);
845 in
= cdat
->plaintext
;
846 in_len
= cdat
->plaintext_len
;
847 out
= cdat
->ciphertext
;
848 out_len
= cdat
->ciphertext_len
;
850 in
= cdat
->ciphertext
;
851 in_len
= cdat
->ciphertext_len
;
852 out
= cdat
->plaintext
;
853 out_len
= cdat
->plaintext_len
;
855 tmp
= OPENSSL_malloc(in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
);
858 err
= "CIPHERINIT_ERROR";
859 if (!EVP_CipherInit_ex(ctx
, cdat
->cipher
, NULL
, NULL
, NULL
, enc
))
861 err
= "INVALID_IV_LENGTH";
864 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_IVLEN
,
867 } else if (cdat
->iv_len
!= (size_t)EVP_CIPHER_CTX_iv_length(ctx
))
873 * If encrypting or OCB just set tag length initially, otherwise
874 * set tag length and value.
876 if (enc
|| cdat
->aead
== EVP_CIPH_OCB_MODE
) {
877 err
= "TAG_LENGTH_SET_ERROR";
880 err
= "TAG_SET_ERROR";
883 if (tag
|| cdat
->aead
!= EVP_CIPH_GCM_MODE
) {
884 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_TAG
,
890 err
= "INVALID_KEY_LENGTH";
891 if (!EVP_CIPHER_CTX_set_key_length(ctx
, cdat
->key_len
))
893 err
= "KEY_SET_ERROR";
894 if (!EVP_CipherInit_ex(ctx
, NULL
, NULL
, cdat
->key
, cdat
->iv
, -1))
897 if (!enc
&& cdat
->aead
== EVP_CIPH_OCB_MODE
) {
898 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_TAG
,
899 cdat
->tag_len
, cdat
->tag
)) {
900 err
= "TAG_SET_ERROR";
905 if (cdat
->aead
== EVP_CIPH_CCM_MODE
) {
906 if (!EVP_CipherUpdate(ctx
, NULL
, &tmplen
, NULL
, out_len
)) {
907 err
= "CCM_PLAINTEXT_LENGTH_SET_ERROR";
912 if (!EVP_CipherUpdate(ctx
, NULL
, &tmplen
, cdat
->aad
, cdat
->aad_len
)) {
913 err
= "AAD_SET_ERROR";
917 EVP_CIPHER_CTX_set_padding(ctx
, 0);
918 err
= "CIPHERUPDATE_ERROR";
919 if (!EVP_CipherUpdate(ctx
, tmp
, &tmplen
, in
, in_len
))
921 if (cdat
->aead
== EVP_CIPH_CCM_MODE
)
924 err
= "CIPHERFINAL_ERROR";
925 if (!EVP_CipherFinal_ex(ctx
, tmp
+ tmplen
, &tmpflen
))
928 err
= "LENGTH_MISMATCH";
929 if (out_len
!= (size_t)(tmplen
+ tmpflen
))
931 err
= "VALUE_MISMATCH";
932 if (check_output(t
, out
, tmp
, out_len
))
934 if (enc
&& cdat
->aead
) {
935 unsigned char rtag
[16];
936 if (cdat
->tag_len
> sizeof(rtag
)) {
937 err
= "TAG_LENGTH_INTERNAL_ERROR";
940 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_GET_TAG
,
941 cdat
->tag_len
, rtag
)) {
942 err
= "TAG_RETRIEVE_ERROR";
945 if (check_output(t
, cdat
->tag
, rtag
, cdat
->tag_len
)) {
946 err
= "TAG_VALUE_MISMATCH";
953 EVP_CIPHER_CTX_free(ctx
);
958 static int cipher_test_run(struct evp_test
*t
)
960 struct cipher_data
*cdat
= t
->data
;
966 if (!cdat
->iv
&& EVP_CIPHER_iv_length(cdat
->cipher
)) {
967 /* IV is optional and usually omitted in wrap mode */
968 if (EVP_CIPHER_mode(cdat
->cipher
) != EVP_CIPH_WRAP_MODE
) {
973 if (cdat
->aead
&& !cdat
->tag
) {
978 rv
= cipher_test_enc(t
, 1);
979 /* Not fatal errors: return */
986 if (cdat
->enc
!= 1) {
987 rv
= cipher_test_enc(t
, 0);
988 /* Not fatal errors: return */
998 static const struct evp_test_method cipher_test_method
= {
1001 cipher_test_cleanup
,
1009 /* Algorithm string for this MAC */
1015 unsigned char *input
;
1017 /* Expected output */
1018 unsigned char *output
;
1022 static int mac_test_init(struct evp_test
*t
, const char *alg
)
1025 struct mac_data
*mdat
;
1026 if (strcmp(alg
, "HMAC") == 0)
1027 type
= EVP_PKEY_HMAC
;
1028 else if (strcmp(alg
, "CMAC") == 0)
1029 type
= EVP_PKEY_CMAC
;
1033 mdat
= OPENSSL_malloc(sizeof(*mdat
));
1038 mdat
->output
= NULL
;
1043 static void mac_test_cleanup(struct evp_test
*t
)
1045 struct mac_data
*mdat
= t
->data
;
1046 test_free(mdat
->alg
);
1047 test_free(mdat
->key
);
1048 test_free(mdat
->input
);
1049 test_free(mdat
->output
);
1052 static int mac_test_parse(struct evp_test
*t
,
1053 const char *keyword
, const char *value
)
1055 struct mac_data
*mdata
= t
->data
;
1056 if (strcmp(keyword
, "Key") == 0)
1057 return test_bin(value
, &mdata
->key
, &mdata
->key_len
);
1058 if (strcmp(keyword
, "Algorithm") == 0) {
1059 mdata
->alg
= OPENSSL_strdup(value
);
1064 if (strcmp(keyword
, "Input") == 0)
1065 return test_bin(value
, &mdata
->input
, &mdata
->input_len
);
1066 if (strcmp(keyword
, "Output") == 0)
1067 return test_bin(value
, &mdata
->output
, &mdata
->output_len
);
1071 static int mac_test_run(struct evp_test
*t
)
1073 struct mac_data
*mdata
= t
->data
;
1074 const char *err
= "INTERNAL_ERROR";
1075 EVP_MD_CTX
*mctx
= NULL
;
1076 EVP_PKEY_CTX
*pctx
= NULL
, *genctx
= NULL
;
1077 EVP_PKEY
*key
= NULL
;
1078 const EVP_MD
*md
= NULL
;
1079 unsigned char *mac
= NULL
;
1082 err
= "MAC_PKEY_CTX_ERROR";
1083 genctx
= EVP_PKEY_CTX_new_id(mdata
->type
, NULL
);
1087 err
= "MAC_KEYGEN_INIT_ERROR";
1088 if (EVP_PKEY_keygen_init(genctx
) <= 0)
1090 if (mdata
->type
== EVP_PKEY_CMAC
) {
1091 err
= "MAC_ALGORITHM_SET_ERROR";
1092 if (EVP_PKEY_CTX_ctrl_str(genctx
, "cipher", mdata
->alg
) <= 0)
1096 err
= "MAC_KEY_SET_ERROR";
1097 if (EVP_PKEY_CTX_set_mac_key(genctx
, mdata
->key
, mdata
->key_len
) <= 0)
1100 err
= "MAC_KEY_GENERATE_ERROR";
1101 if (EVP_PKEY_keygen(genctx
, &key
) <= 0)
1103 if (mdata
->type
== EVP_PKEY_HMAC
) {
1104 err
= "MAC_ALGORITHM_SET_ERROR";
1105 md
= EVP_get_digestbyname(mdata
->alg
);
1109 mctx
= EVP_MD_CTX_new();
1112 err
= "DIGESTSIGNINIT_ERROR";
1113 if (!EVP_DigestSignInit(mctx
, &pctx
, md
, NULL
, key
))
1116 err
= "DIGESTSIGNUPDATE_ERROR";
1117 if (!EVP_DigestSignUpdate(mctx
, mdata
->input
, mdata
->input_len
))
1119 err
= "DIGESTSIGNFINAL_LENGTH_ERROR";
1120 if (!EVP_DigestSignFinal(mctx
, NULL
, &mac_len
))
1122 mac
= OPENSSL_malloc(mac_len
);
1124 fprintf(stderr
, "Error allocating mac buffer!\n");
1127 if (!EVP_DigestSignFinal(mctx
, mac
, &mac_len
))
1129 err
= "MAC_LENGTH_MISMATCH";
1130 if (mac_len
!= mdata
->output_len
)
1132 err
= "MAC_MISMATCH";
1133 if (check_output(t
, mdata
->output
, mac
, mac_len
))
1137 EVP_MD_CTX_free(mctx
);
1139 EVP_PKEY_CTX_free(genctx
);
1145 static const struct evp_test_method mac_test_method
= {
1154 * Public key operations. These are all very similar and can share
1155 * a lot of common code.
1159 /* Context for this operation */
1161 /* Key operation to perform */
1162 int (*keyop
) (EVP_PKEY_CTX
*ctx
,
1163 unsigned char *sig
, size_t *siglen
,
1164 const unsigned char *tbs
, size_t tbslen
);
1166 unsigned char *input
;
1168 /* Expected output */
1169 unsigned char *output
;
1174 * Perform public key operation setup: lookup key, allocated ctx and call
1175 * the appropriate initialisation function
1177 static int pkey_test_init(struct evp_test
*t
, const char *name
,
1179 int (*keyopinit
) (EVP_PKEY_CTX
*ctx
),
1180 int (*keyop
) (EVP_PKEY_CTX
*ctx
,
1181 unsigned char *sig
, size_t *siglen
,
1182 const unsigned char *tbs
,
1186 struct pkey_data
*kdata
;
1187 EVP_PKEY
*pkey
= NULL
;
1190 rv
= find_key(&pkey
, name
, t
->public);
1192 rv
= find_key(&pkey
, name
, t
->private);
1200 kdata
= OPENSSL_malloc(sizeof(*kdata
));
1202 EVP_PKEY_free(pkey
);
1206 kdata
->input
= NULL
;
1207 kdata
->output
= NULL
;
1208 kdata
->keyop
= keyop
;
1210 kdata
->ctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
1213 if (keyopinit(kdata
->ctx
) <= 0)
1218 static void pkey_test_cleanup(struct evp_test
*t
)
1220 struct pkey_data
*kdata
= t
->data
;
1222 OPENSSL_free(kdata
->input
);
1223 OPENSSL_free(kdata
->output
);
1224 EVP_PKEY_CTX_free(kdata
->ctx
);
1227 static int pkey_test_parse(struct evp_test
*t
,
1228 const char *keyword
, const char *value
)
1230 struct pkey_data
*kdata
= t
->data
;
1231 if (strcmp(keyword
, "Input") == 0)
1232 return test_bin(value
, &kdata
->input
, &kdata
->input_len
);
1233 if (strcmp(keyword
, "Output") == 0)
1234 return test_bin(value
, &kdata
->output
, &kdata
->output_len
);
1235 if (strcmp(keyword
, "Ctrl") == 0) {
1236 char *p
= strchr(value
, ':');
1239 if (EVP_PKEY_CTX_ctrl_str(kdata
->ctx
, value
, p
) <= 0)
1246 static int pkey_test_run(struct evp_test
*t
)
1248 struct pkey_data
*kdata
= t
->data
;
1249 unsigned char *out
= NULL
;
1251 const char *err
= "KEYOP_LENGTH_ERROR";
1252 if (kdata
->keyop(kdata
->ctx
, NULL
, &out_len
, kdata
->input
,
1253 kdata
->input_len
) <= 0)
1255 out
= OPENSSL_malloc(out_len
);
1257 fprintf(stderr
, "Error allocating output buffer!\n");
1260 err
= "KEYOP_ERROR";
1262 (kdata
->ctx
, out
, &out_len
, kdata
->input
, kdata
->input_len
) <= 0)
1264 err
= "KEYOP_LENGTH_MISMATCH";
1265 if (out_len
!= kdata
->output_len
)
1267 err
= "KEYOP_MISMATCH";
1268 if (check_output(t
, kdata
->output
, out
, out_len
))
1277 static int sign_test_init(struct evp_test
*t
, const char *name
)
1279 return pkey_test_init(t
, name
, 0, EVP_PKEY_sign_init
, EVP_PKEY_sign
);
1282 static const struct evp_test_method psign_test_method
= {
1290 static int verify_recover_test_init(struct evp_test
*t
, const char *name
)
1292 return pkey_test_init(t
, name
, 1, EVP_PKEY_verify_recover_init
,
1293 EVP_PKEY_verify_recover
);
1296 static const struct evp_test_method pverify_recover_test_method
= {
1298 verify_recover_test_init
,
1304 static int decrypt_test_init(struct evp_test
*t
, const char *name
)
1306 return pkey_test_init(t
, name
, 0, EVP_PKEY_decrypt_init
,
1310 static const struct evp_test_method pdecrypt_test_method
= {
1318 static int verify_test_init(struct evp_test
*t
, const char *name
)
1320 return pkey_test_init(t
, name
, 1, EVP_PKEY_verify_init
, 0);
1323 static int verify_test_run(struct evp_test
*t
)
1325 struct pkey_data
*kdata
= t
->data
;
1326 if (EVP_PKEY_verify(kdata
->ctx
, kdata
->output
, kdata
->output_len
,
1327 kdata
->input
, kdata
->input_len
) <= 0)
1328 t
->err
= "VERIFY_ERROR";
1332 static const struct evp_test_method pverify_test_method
= {
1342 #define PBE_TYPE_SCRYPT 1
1343 #define PBE_TYPE_PBKDF2 2
1344 #define PBE_TYPE_PKCS12 3
1350 /* scrypt parameters */
1351 uint64_t N
, r
, p
, maxmem
;
1353 /* PKCS#12 parameters */
1358 unsigned char *pass
;
1362 unsigned char *salt
;
1365 /* Expected output */
1370 #ifndef OPENSSL_NO_SCRYPT
1371 static int scrypt_test_parse(struct evp_test
*t
,
1372 const char *keyword
, const char *value
)
1374 struct pbe_data
*pdata
= t
->data
;
1376 if (strcmp(keyword
, "N") == 0)
1377 return test_uint64(value
, &pdata
->N
);
1378 if (strcmp(keyword
, "p") == 0)
1379 return test_uint64(value
, &pdata
->p
);
1380 if (strcmp(keyword
, "r") == 0)
1381 return test_uint64(value
, &pdata
->r
);
1382 if (strcmp(keyword
, "maxmem") == 0)
1383 return test_uint64(value
, &pdata
->maxmem
);
1388 static int pbkdf2_test_parse(struct evp_test
*t
,
1389 const char *keyword
, const char *value
)
1391 struct pbe_data
*pdata
= t
->data
;
1393 if (strcmp(keyword
, "iter") == 0) {
1394 pdata
->iter
= atoi(value
);
1395 if (pdata
->iter
<= 0)
1399 if (strcmp(keyword
, "MD") == 0) {
1400 pdata
->md
= EVP_get_digestbyname(value
);
1401 if (pdata
->md
== NULL
)
1408 static int pkcs12_test_parse(struct evp_test
*t
,
1409 const char *keyword
, const char *value
)
1411 struct pbe_data
*pdata
= t
->data
;
1413 if (strcmp(keyword
, "id") == 0) {
1414 pdata
->id
= atoi(value
);
1419 return pbkdf2_test_parse(t
, keyword
, value
);
1422 static int pbe_test_init(struct evp_test
*t
, const char *alg
)
1424 struct pbe_data
*pdat
;
1427 #ifndef OPENSSL_NO_SCRYPT
1428 if (strcmp(alg
, "scrypt") == 0)
1429 pbe_type
= PBE_TYPE_SCRYPT
;
1431 else if (strcmp(alg
, "pbkdf2") == 0)
1432 pbe_type
= PBE_TYPE_PBKDF2
;
1433 else if (strcmp(alg
, "pkcs12") == 0)
1434 pbe_type
= PBE_TYPE_PKCS12
;
1436 fprintf(stderr
, "Unknown pbe algorithm %s\n", alg
);
1437 pdat
= OPENSSL_malloc(sizeof(*pdat
));
1438 pdat
->pbe_type
= pbe_type
;
1452 static void pbe_test_cleanup(struct evp_test
*t
)
1454 struct pbe_data
*pdat
= t
->data
;
1455 test_free(pdat
->pass
);
1456 test_free(pdat
->salt
);
1457 test_free(pdat
->key
);
1460 static int pbe_test_parse(struct evp_test
*t
,
1461 const char *keyword
, const char *value
)
1463 struct pbe_data
*pdata
= t
->data
;
1465 if (strcmp(keyword
, "Password") == 0)
1466 return test_bin(value
, &pdata
->pass
, &pdata
->pass_len
);
1467 if (strcmp(keyword
, "Salt") == 0)
1468 return test_bin(value
, &pdata
->salt
, &pdata
->salt_len
);
1469 if (strcmp(keyword
, "Key") == 0)
1470 return test_bin(value
, &pdata
->key
, &pdata
->key_len
);
1471 if (pdata
->pbe_type
== PBE_TYPE_PBKDF2
)
1472 return pbkdf2_test_parse(t
, keyword
, value
);
1473 else if (pdata
->pbe_type
== PBE_TYPE_PKCS12
)
1474 return pkcs12_test_parse(t
, keyword
, value
);
1475 #ifndef OPENSSL_NO_SCRYPT
1476 else if (pdata
->pbe_type
== PBE_TYPE_SCRYPT
)
1477 return scrypt_test_parse(t
, keyword
, value
);
1482 static int pbe_test_run(struct evp_test
*t
)
1484 struct pbe_data
*pdata
= t
->data
;
1485 const char *err
= "INTERNAL_ERROR";
1488 key
= OPENSSL_malloc(pdata
->key_len
);
1491 if (pdata
->pbe_type
== PBE_TYPE_PBKDF2
) {
1492 err
= "PBKDF2_ERROR";
1493 if (PKCS5_PBKDF2_HMAC((char *)pdata
->pass
, pdata
->pass_len
,
1494 pdata
->salt
, pdata
->salt_len
,
1495 pdata
->iter
, pdata
->md
,
1496 pdata
->key_len
, key
) == 0)
1498 #ifndef OPENSSL_NO_SCRYPT
1499 } else if (pdata
->pbe_type
== PBE_TYPE_SCRYPT
) {
1500 err
= "SCRYPT_ERROR";
1501 if (EVP_PBE_scrypt((const char *)pdata
->pass
, pdata
->pass_len
,
1502 pdata
->salt
, pdata
->salt_len
,
1503 pdata
->N
, pdata
->r
, pdata
->p
, pdata
->maxmem
,
1504 key
, pdata
->key_len
) == 0)
1507 } else if (pdata
->pbe_type
== PBE_TYPE_PKCS12
) {
1508 err
= "PKCS12_ERROR";
1509 if (PKCS12_key_gen_uni(pdata
->pass
, pdata
->pass_len
,
1510 pdata
->salt
, pdata
->salt_len
,
1511 pdata
->id
, pdata
->iter
, pdata
->key_len
,
1512 key
, pdata
->md
) == 0)
1515 err
= "KEY_MISMATCH";
1516 if (check_output(t
, pdata
->key
, key
, pdata
->key_len
))
1525 static const struct evp_test_method pbe_test_method
= {
1536 BASE64_CANONICAL_ENCODING
= 0,
1537 BASE64_VALID_ENCODING
= 1,
1538 BASE64_INVALID_ENCODING
= 2
1539 } base64_encoding_type
;
1541 struct encode_data
{
1542 /* Input to encoding */
1543 unsigned char *input
;
1545 /* Expected output */
1546 unsigned char *output
;
1548 base64_encoding_type encoding
;
1551 static int encode_test_init(struct evp_test
*t
, const char *encoding
)
1553 struct encode_data
*edata
= OPENSSL_zalloc(sizeof(*edata
));
1555 if (strcmp(encoding
, "canonical") == 0) {
1556 edata
->encoding
= BASE64_CANONICAL_ENCODING
;
1557 } else if (strcmp(encoding
, "valid") == 0) {
1558 edata
->encoding
= BASE64_VALID_ENCODING
;
1559 } else if (strcmp(encoding
, "invalid") == 0) {
1560 edata
->encoding
= BASE64_INVALID_ENCODING
;
1561 t
->expected_err
= OPENSSL_strdup("DECODE_ERROR");
1562 if (t
->expected_err
== NULL
)
1565 fprintf(stderr
, "Bad encoding: %s. Should be one of "
1566 "{canonical, valid, invalid}\n", encoding
);
1573 static void encode_test_cleanup(struct evp_test
*t
)
1575 struct encode_data
*edata
= t
->data
;
1576 test_free(edata
->input
);
1577 test_free(edata
->output
);
1578 memset(edata
, 0, sizeof(*edata
));
1581 static int encode_test_parse(struct evp_test
*t
,
1582 const char *keyword
, const char *value
)
1584 struct encode_data
*edata
= t
->data
;
1585 if (strcmp(keyword
, "Input") == 0)
1586 return test_bin(value
, &edata
->input
, &edata
->input_len
);
1587 if (strcmp(keyword
, "Output") == 0)
1588 return test_bin(value
, &edata
->output
, &edata
->output_len
);
1592 static int encode_test_run(struct evp_test
*t
)
1594 struct encode_data
*edata
= t
->data
;
1595 unsigned char *encode_out
= NULL
, *decode_out
= NULL
;
1596 int output_len
, chunk_len
;
1597 const char *err
= "INTERNAL_ERROR";
1598 EVP_ENCODE_CTX
*decode_ctx
= EVP_ENCODE_CTX_new();
1600 if (decode_ctx
== NULL
)
1603 if (edata
->encoding
== BASE64_CANONICAL_ENCODING
) {
1604 EVP_ENCODE_CTX
*encode_ctx
= EVP_ENCODE_CTX_new();
1605 if (encode_ctx
== NULL
)
1607 encode_out
= OPENSSL_malloc(EVP_ENCODE_LENGTH(edata
->input_len
));
1608 if (encode_out
== NULL
)
1611 EVP_EncodeInit(encode_ctx
);
1612 EVP_EncodeUpdate(encode_ctx
, encode_out
, &chunk_len
,
1613 edata
->input
, edata
->input_len
);
1614 output_len
= chunk_len
;
1616 EVP_EncodeFinal(encode_ctx
, encode_out
+ chunk_len
, &chunk_len
);
1617 output_len
+= chunk_len
;
1619 EVP_ENCODE_CTX_free(encode_ctx
);
1621 if (check_var_length_output(t
, edata
->output
, edata
->output_len
,
1622 encode_out
, output_len
)) {
1623 err
= "BAD_ENCODING";
1628 decode_out
= OPENSSL_malloc(EVP_DECODE_LENGTH(edata
->output_len
));
1629 if (decode_out
== NULL
)
1632 EVP_DecodeInit(decode_ctx
);
1633 if (EVP_DecodeUpdate(decode_ctx
, decode_out
, &chunk_len
, edata
->output
,
1634 edata
->output_len
) < 0) {
1635 err
= "DECODE_ERROR";
1638 output_len
= chunk_len
;
1640 if (EVP_DecodeFinal(decode_ctx
, decode_out
+ chunk_len
, &chunk_len
) != 1) {
1641 err
= "DECODE_ERROR";
1644 output_len
+= chunk_len
;
1646 if (edata
->encoding
!= BASE64_INVALID_ENCODING
&&
1647 check_var_length_output(t
, edata
->input
, edata
->input_len
,
1648 decode_out
, output_len
)) {
1649 err
= "BAD_DECODING";
1656 OPENSSL_free(encode_out
);
1657 OPENSSL_free(decode_out
);
1658 EVP_ENCODE_CTX_free(decode_ctx
);
1662 static const struct evp_test_method encode_test_method
= {
1665 encode_test_cleanup
,
1671 * KDF operations: initially just TLS1 PRF but can be adapted.
1675 /* Context for this operation */
1677 /* Expected output */
1678 unsigned char *output
;
1683 * Perform public key operation setup: lookup key, allocated ctx and call
1684 * the appropriate initialisation function
1686 static int kdf_test_init(struct evp_test
*t
, const char *name
)
1688 struct kdf_data
*kdata
;
1690 kdata
= OPENSSL_malloc(sizeof(*kdata
));
1694 kdata
->output
= NULL
;
1696 kdata
->ctx
= EVP_PKEY_CTX_new_id(OBJ_sn2nid(name
), NULL
);
1697 if (kdata
->ctx
== NULL
)
1699 if (EVP_PKEY_derive_init(kdata
->ctx
) <= 0)
1704 static void kdf_test_cleanup(struct evp_test
*t
)
1706 struct kdf_data
*kdata
= t
->data
;
1707 OPENSSL_free(kdata
->output
);
1708 EVP_PKEY_CTX_free(kdata
->ctx
);
1711 static int kdf_ctrl(EVP_PKEY_CTX
*ctx
, int op
, const char *value
)
1713 unsigned char *buf
= NULL
;
1716 if (test_bin(value
, &buf
, &buf_len
) == 0)
1718 if (EVP_PKEY_CTX_ctrl(ctx
, -1, -1, op
, buf_len
, buf
) <= 0)
1726 static int kdf_test_parse(struct evp_test
*t
,
1727 const char *keyword
, const char *value
)
1729 struct kdf_data
*kdata
= t
->data
;
1730 if (strcmp(keyword
, "Output") == 0)
1731 return test_bin(value
, &kdata
->output
, &kdata
->output_len
);
1732 else if (strcmp(keyword
, "MD") == 0) {
1733 const EVP_MD
*md
= EVP_get_digestbyname(value
);
1736 if (EVP_PKEY_CTX_set_tls1_prf_md(kdata
->ctx
, md
) <= 0)
1739 } else if (strcmp(keyword
, "Secret") == 0) {
1740 return kdf_ctrl(kdata
->ctx
, EVP_PKEY_CTRL_TLS_SECRET
, value
);
1741 } else if (strncmp("Seed", keyword
, 4) == 0) {
1742 return kdf_ctrl(kdata
->ctx
, EVP_PKEY_CTRL_TLS_SEED
, value
);
1747 static int kdf_test_run(struct evp_test
*t
)
1749 struct kdf_data
*kdata
= t
->data
;
1750 unsigned char *out
= NULL
;
1751 size_t out_len
= kdata
->output_len
;
1752 const char *err
= "INTERNAL_ERROR";
1753 out
= OPENSSL_malloc(out_len
);
1755 fprintf(stderr
, "Error allocating output buffer!\n");
1758 err
= "KDF_DERIVE_ERROR";
1759 if (EVP_PKEY_derive(kdata
->ctx
, out
, &out_len
) <= 0)
1761 err
= "KDF_LENGTH_MISMATCH";
1762 if (out_len
!= kdata
->output_len
)
1764 err
= "KDF_MISMATCH";
1765 if (check_output(t
, kdata
->output
, out
, out_len
))
1774 static const struct evp_test_method kdf_test_method
= {