3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
6 /* ====================================================================
7 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
59 #include <openssl/evp.h>
60 #include <openssl/pem.h>
61 #include <openssl/err.h>
62 #include <openssl/x509v3.h>
63 #include <openssl/pkcs12.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
;
297 static const struct evp_test_method
*evp_test_list
[] = {
302 &pverify_test_method
,
303 &pdecrypt_test_method
,
304 &pverify_recover_test_method
,
310 static const struct evp_test_method
*evp_find_test(const char *name
)
312 const struct evp_test_method
**tt
;
314 for (tt
= evp_test_list
; *tt
; tt
++) {
315 if (strcmp(name
, (*tt
)->name
) == 0)
321 static void hex_print(const char *name
, const unsigned char *buf
, size_t len
)
324 fprintf(stderr
, "%s ", name
);
325 for (i
= 0; i
< len
; i
++)
326 fprintf(stderr
, "%02X", buf
[i
]);
330 static void free_expected(struct evp_test
*t
)
332 OPENSSL_free(t
->expected_err
);
333 t
->expected_err
= NULL
;
334 OPENSSL_free(t
->out_expected
);
335 OPENSSL_free(t
->out_received
);
336 t
->out_expected
= NULL
;
337 t
->out_received
= NULL
;
338 t
->out_expected_len
= 0;
339 t
->out_received_len
= 0;
344 static void print_expected(struct evp_test
*t
)
346 if (t
->out_expected
== NULL
&& t
->out_received
== NULL
)
348 hex_print("Expected:", t
->out_expected
, t
->out_expected_len
);
349 hex_print("Got: ", t
->out_received
, t
->out_received_len
);
353 static int check_test_error(struct evp_test
*t
)
355 if (!t
->err
&& !t
->expected_err
)
357 if (t
->err
&& !t
->expected_err
) {
358 fprintf(stderr
, "Test line %d: unexpected error %s\n",
359 t
->start_line
, t
->err
);
363 if (!t
->err
&& t
->expected_err
) {
364 fprintf(stderr
, "Test line %d: succeeded expecting %s\n",
365 t
->start_line
, t
->expected_err
);
368 if (strcmp(t
->err
, t
->expected_err
) == 0)
371 fprintf(stderr
, "Test line %d: expecting %s got %s\n",
372 t
->start_line
, t
->expected_err
, t
->err
);
376 /* Setup a new test, run any existing test */
378 static int setup_test(struct evp_test
*t
, const struct evp_test_method
*tmeth
)
380 /* If we already have a test set up run it */
389 if (t
->meth
->run_test(t
) != 1) {
390 fprintf(stderr
, "%s test error line %d\n",
391 t
->meth
->name
, t
->start_line
);
394 if (!check_test_error(t
)) {
396 ERR_print_errors_fp(stderr
);
401 OPENSSL_free(t
->data
);
403 OPENSSL_free(t
->expected_err
);
404 t
->expected_err
= NULL
;
411 static int find_key(EVP_PKEY
**ppk
, const char *name
, struct key_list
*lst
)
413 for (; lst
; lst
= lst
->next
) {
414 if (strcmp(lst
->name
, name
) == 0) {
423 static void free_key_list(struct key_list
*lst
)
425 while (lst
!= NULL
) {
426 struct key_list
*ltmp
;
427 EVP_PKEY_free(lst
->key
);
428 OPENSSL_free(lst
->name
);
435 static int check_unsupported()
437 long err
= ERR_peek_error();
438 if (ERR_GET_LIB(err
) == ERR_LIB_EVP
439 && ERR_GET_REASON(err
) == EVP_R_UNSUPPORTED_ALGORITHM
) {
446 static int process_test(struct evp_test
*t
, char *buf
, int verbose
)
448 char *keyword
= NULL
, *value
= NULL
;
449 int rv
= 0, add_key
= 0;
451 struct key_list
**lst
= NULL
, *key
= NULL
;
453 const struct evp_test_method
*tmeth
= NULL
;
456 if (!parse_line(&keyword
, &value
, buf
))
458 if (strcmp(keyword
, "PrivateKey") == 0) {
459 save_pos
= ftell(t
->in
);
460 pk
= PEM_read_PrivateKey(t
->in
, NULL
, 0, NULL
);
461 if (pk
== NULL
&& !check_unsupported()) {
462 fprintf(stderr
, "Error reading private key %s\n", value
);
463 ERR_print_errors_fp(stderr
);
469 if (strcmp(keyword
, "PublicKey") == 0) {
470 save_pos
= ftell(t
->in
);
471 pk
= PEM_read_PUBKEY(t
->in
, NULL
, 0, NULL
);
472 if (pk
== NULL
&& !check_unsupported()) {
473 fprintf(stderr
, "Error reading public key %s\n", value
);
474 ERR_print_errors_fp(stderr
);
480 /* If we have a key add to list */
483 if (find_key(NULL
, value
, *lst
)) {
484 fprintf(stderr
, "Duplicate key %s\n", value
);
487 key
= OPENSSL_malloc(sizeof(*key
));
490 key
->name
= OPENSSL_strdup(value
);
494 /* Rewind input, read to end and update line numbers */
495 fseek(t
->in
, save_pos
, SEEK_SET
);
496 while (fgets(tmpbuf
, sizeof(tmpbuf
), t
->in
)) {
498 if (strncmp(tmpbuf
, "-----END", 8) == 0)
501 fprintf(stderr
, "Can't find key end\n");
505 /* See if keyword corresponds to a test start */
506 tmeth
= evp_find_test(keyword
);
508 if (!setup_test(t
, tmeth
))
510 t
->start_line
= t
->line
;
512 if (!tmeth
->init(t
, value
)) {
513 fprintf(stderr
, "Unknown %s: %s\n", keyword
, value
);
517 } else if (t
->skip
) {
519 } else if (strcmp(keyword
, "Result") == 0) {
520 if (t
->expected_err
) {
521 fprintf(stderr
, "Line %d: multiple result lines\n", t
->line
);
524 t
->expected_err
= OPENSSL_strdup(value
);
525 if (!t
->expected_err
)
528 /* Must be test specific line: try to parse it */
530 rv
= t
->meth
->parse(t
, keyword
, value
);
533 fprintf(stderr
, "line %d: unexpected keyword %s\n",
537 fprintf(stderr
, "line %d: error processing keyword %s\n",
545 static int check_var_length_output(struct evp_test
*t
,
546 const unsigned char *expected
,
548 const unsigned char *received
,
551 if (expected_len
== received_len
&&
552 memcmp(expected
, received
, expected_len
) == 0) {
556 /* The result printing code expects a non-NULL buffer. */
557 t
->out_expected
= OPENSSL_memdup(expected
, expected_len
? expected_len
: 1);
558 t
->out_expected_len
= expected_len
;
559 t
->out_received
= OPENSSL_memdup(received
, received_len
? received_len
: 1);
560 t
->out_received_len
= received_len
;
561 if (t
->out_expected
== NULL
|| t
->out_received
== NULL
) {
562 fprintf(stderr
, "Memory allocation error!\n");
568 static int check_output(struct evp_test
*t
,
569 const unsigned char *expected
,
570 const unsigned char *received
,
573 return check_var_length_output(t
, expected
, len
, received
, len
);
576 int main(int argc
, char **argv
)
583 fprintf(stderr
, "usage: evp_test testfile.txt\n");
587 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
589 ERR_load_crypto_strings();
590 OpenSSL_add_all_algorithms();
592 memset(&t
, 0, sizeof(t
));
594 in
= fopen(argv
[1], "r");
596 while (fgets(buf
, sizeof(buf
), in
)) {
598 if (!process_test(&t
, buf
, 0))
601 /* Run any final test we have */
602 if (!setup_test(&t
, NULL
))
604 fprintf(stderr
, "%d tests completed with %d errors, %d skipped\n",
605 t
.ntests
, t
.errors
, t
.nskip
);
606 free_key_list(t
.public);
607 free_key_list(t
.private);
610 CRYPTO_cleanup_all_ex_data();
611 ERR_remove_thread_state(NULL
);
613 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
614 CRYPTO_mem_leaks_fp(stderr
);
621 static void test_free(void *d
)
626 /* Message digest tests */
629 /* Digest this test is for */
630 const EVP_MD
*digest
;
631 /* Input to digest */
632 unsigned char *input
;
634 /* Repeat count for input */
636 /* Expected output */
637 unsigned char *output
;
641 static int digest_test_init(struct evp_test
*t
, const char *alg
)
643 const EVP_MD
*digest
;
644 struct digest_data
*mdat
;
645 digest
= EVP_get_digestbyname(alg
);
647 /* If alg has an OID assume disabled algorithm */
648 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
654 mdat
= OPENSSL_malloc(sizeof(*mdat
));
655 mdat
->digest
= digest
;
663 static void digest_test_cleanup(struct evp_test
*t
)
665 struct digest_data
*mdat
= t
->data
;
666 test_free(mdat
->input
);
667 test_free(mdat
->output
);
670 static int digest_test_parse(struct evp_test
*t
,
671 const char *keyword
, const char *value
)
673 struct digest_data
*mdata
= t
->data
;
674 if (strcmp(keyword
, "Input") == 0)
675 return test_bin(value
, &mdata
->input
, &mdata
->input_len
);
676 if (strcmp(keyword
, "Output") == 0)
677 return test_bin(value
, &mdata
->output
, &mdata
->output_len
);
678 if (strcmp(keyword
, "Count") == 0) {
679 long nrpt
= atoi(value
);
682 mdata
->nrpt
= (size_t)nrpt
;
688 static int digest_test_run(struct evp_test
*t
)
690 struct digest_data
*mdata
= t
->data
;
692 const char *err
= "INTERNAL_ERROR";
694 unsigned char md
[EVP_MAX_MD_SIZE
];
696 mctx
= EVP_MD_CTX_new();
699 err
= "DIGESTINIT_ERROR";
700 if (!EVP_DigestInit_ex(mctx
, mdata
->digest
, NULL
))
702 err
= "DIGESTUPDATE_ERROR";
703 for (i
= 0; i
< mdata
->nrpt
; i
++) {
704 if (!EVP_DigestUpdate(mctx
, mdata
->input
, mdata
->input_len
))
707 err
= "DIGESTFINAL_ERROR";
708 if (!EVP_DigestFinal(mctx
, md
, &md_len
))
710 err
= "DIGEST_LENGTH_MISMATCH";
711 if (md_len
!= mdata
->output_len
)
713 err
= "DIGEST_MISMATCH";
714 if (check_output(t
, mdata
->output
, md
, md_len
))
718 EVP_MD_CTX_free(mctx
);
723 static const struct evp_test_method digest_test_method
= {
733 const EVP_CIPHER
*cipher
;
735 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
741 unsigned char *plaintext
;
742 size_t plaintext_len
;
743 unsigned char *ciphertext
;
744 size_t ciphertext_len
;
752 static int cipher_test_init(struct evp_test
*t
, const char *alg
)
754 const EVP_CIPHER
*cipher
;
755 struct cipher_data
*cdat
= t
->data
;
756 cipher
= EVP_get_cipherbyname(alg
);
758 /* If alg has an OID assume disabled algorithm */
759 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
765 cdat
= OPENSSL_malloc(sizeof(*cdat
));
766 cdat
->cipher
= cipher
;
770 cdat
->ciphertext
= NULL
;
771 cdat
->plaintext
= NULL
;
775 if (EVP_CIPHER_mode(cipher
) == EVP_CIPH_GCM_MODE
776 || EVP_CIPHER_mode(cipher
) == EVP_CIPH_OCB_MODE
777 || EVP_CIPHER_mode(cipher
) == EVP_CIPH_CCM_MODE
)
778 cdat
->aead
= EVP_CIPHER_mode(cipher
);
779 else if (EVP_CIPHER_flags(cipher
) & EVP_CIPH_FLAG_AEAD_CIPHER
)
787 static void cipher_test_cleanup(struct evp_test
*t
)
789 struct cipher_data
*cdat
= t
->data
;
790 test_free(cdat
->key
);
792 test_free(cdat
->ciphertext
);
793 test_free(cdat
->plaintext
);
794 test_free(cdat
->aad
);
795 test_free(cdat
->tag
);
798 static int cipher_test_parse(struct evp_test
*t
, const char *keyword
,
801 struct cipher_data
*cdat
= t
->data
;
802 if (strcmp(keyword
, "Key") == 0)
803 return test_bin(value
, &cdat
->key
, &cdat
->key_len
);
804 if (strcmp(keyword
, "IV") == 0)
805 return test_bin(value
, &cdat
->iv
, &cdat
->iv_len
);
806 if (strcmp(keyword
, "Plaintext") == 0)
807 return test_bin(value
, &cdat
->plaintext
, &cdat
->plaintext_len
);
808 if (strcmp(keyword
, "Ciphertext") == 0)
809 return test_bin(value
, &cdat
->ciphertext
, &cdat
->ciphertext_len
);
811 if (strcmp(keyword
, "AAD") == 0)
812 return test_bin(value
, &cdat
->aad
, &cdat
->aad_len
);
813 if (strcmp(keyword
, "Tag") == 0)
814 return test_bin(value
, &cdat
->tag
, &cdat
->tag_len
);
817 if (strcmp(keyword
, "Operation") == 0) {
818 if (strcmp(value
, "ENCRYPT") == 0)
820 else if (strcmp(value
, "DECRYPT") == 0)
829 static int cipher_test_enc(struct evp_test
*t
, int enc
)
831 struct cipher_data
*cdat
= t
->data
;
832 unsigned char *in
, *out
, *tmp
= NULL
;
833 size_t in_len
, out_len
;
835 EVP_CIPHER_CTX
*ctx
= NULL
;
837 err
= "INTERNAL_ERROR";
838 ctx
= EVP_CIPHER_CTX_new();
841 EVP_CIPHER_CTX_set_flags(ctx
, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
);
843 in
= cdat
->plaintext
;
844 in_len
= cdat
->plaintext_len
;
845 out
= cdat
->ciphertext
;
846 out_len
= cdat
->ciphertext_len
;
848 in
= cdat
->ciphertext
;
849 in_len
= cdat
->ciphertext_len
;
850 out
= cdat
->plaintext
;
851 out_len
= cdat
->plaintext_len
;
853 tmp
= OPENSSL_malloc(in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
);
856 err
= "CIPHERINIT_ERROR";
857 if (!EVP_CipherInit_ex(ctx
, cdat
->cipher
, NULL
, NULL
, NULL
, enc
))
859 err
= "INVALID_IV_LENGTH";
862 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_IVLEN
,
865 } else if (cdat
->iv_len
!= (size_t)EVP_CIPHER_CTX_iv_length(ctx
))
871 * If encrypting or OCB just set tag length initially, otherwise
872 * set tag length and value.
874 if (enc
|| cdat
->aead
== EVP_CIPH_OCB_MODE
) {
875 err
= "TAG_LENGTH_SET_ERROR";
878 err
= "TAG_SET_ERROR";
881 if (tag
|| cdat
->aead
!= EVP_CIPH_GCM_MODE
) {
882 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_TAG
,
888 err
= "INVALID_KEY_LENGTH";
889 if (!EVP_CIPHER_CTX_set_key_length(ctx
, cdat
->key_len
))
891 err
= "KEY_SET_ERROR";
892 if (!EVP_CipherInit_ex(ctx
, NULL
, NULL
, cdat
->key
, cdat
->iv
, -1))
895 if (!enc
&& cdat
->aead
== EVP_CIPH_OCB_MODE
) {
896 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_TAG
,
897 cdat
->tag_len
, cdat
->tag
)) {
898 err
= "TAG_SET_ERROR";
903 if (cdat
->aead
== EVP_CIPH_CCM_MODE
) {
904 if (!EVP_CipherUpdate(ctx
, NULL
, &tmplen
, NULL
, out_len
)) {
905 err
= "CCM_PLAINTEXT_LENGTH_SET_ERROR";
910 if (!EVP_CipherUpdate(ctx
, NULL
, &tmplen
, cdat
->aad
, cdat
->aad_len
)) {
911 err
= "AAD_SET_ERROR";
915 EVP_CIPHER_CTX_set_padding(ctx
, 0);
916 err
= "CIPHERUPDATE_ERROR";
917 if (!EVP_CipherUpdate(ctx
, tmp
, &tmplen
, in
, in_len
))
919 if (cdat
->aead
== EVP_CIPH_CCM_MODE
)
922 err
= "CIPHERFINAL_ERROR";
923 if (!EVP_CipherFinal_ex(ctx
, tmp
+ tmplen
, &tmpflen
))
926 err
= "LENGTH_MISMATCH";
927 if (out_len
!= (size_t)(tmplen
+ tmpflen
))
929 err
= "VALUE_MISMATCH";
930 if (check_output(t
, out
, tmp
, out_len
))
932 if (enc
&& cdat
->aead
) {
933 unsigned char rtag
[16];
934 if (cdat
->tag_len
> sizeof(rtag
)) {
935 err
= "TAG_LENGTH_INTERNAL_ERROR";
938 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_GET_TAG
,
939 cdat
->tag_len
, rtag
)) {
940 err
= "TAG_RETRIEVE_ERROR";
943 if (check_output(t
, cdat
->tag
, rtag
, cdat
->tag_len
)) {
944 err
= "TAG_VALUE_MISMATCH";
951 EVP_CIPHER_CTX_free(ctx
);
956 static int cipher_test_run(struct evp_test
*t
)
958 struct cipher_data
*cdat
= t
->data
;
964 if (!cdat
->iv
&& EVP_CIPHER_iv_length(cdat
->cipher
)) {
965 /* IV is optional and usually omitted in wrap mode */
966 if (EVP_CIPHER_mode(cdat
->cipher
) != EVP_CIPH_WRAP_MODE
) {
971 if (cdat
->aead
&& !cdat
->tag
) {
976 rv
= cipher_test_enc(t
, 1);
977 /* Not fatal errors: return */
984 if (cdat
->enc
!= 1) {
985 rv
= cipher_test_enc(t
, 0);
986 /* Not fatal errors: return */
996 static const struct evp_test_method cipher_test_method
= {
1007 /* Algorithm string for this MAC */
1013 unsigned char *input
;
1015 /* Expected output */
1016 unsigned char *output
;
1020 static int mac_test_init(struct evp_test
*t
, const char *alg
)
1023 struct mac_data
*mdat
;
1024 if (strcmp(alg
, "HMAC") == 0)
1025 type
= EVP_PKEY_HMAC
;
1026 else if (strcmp(alg
, "CMAC") == 0)
1027 type
= EVP_PKEY_CMAC
;
1031 mdat
= OPENSSL_malloc(sizeof(*mdat
));
1036 mdat
->output
= NULL
;
1041 static void mac_test_cleanup(struct evp_test
*t
)
1043 struct mac_data
*mdat
= t
->data
;
1044 test_free(mdat
->alg
);
1045 test_free(mdat
->key
);
1046 test_free(mdat
->input
);
1047 test_free(mdat
->output
);
1050 static int mac_test_parse(struct evp_test
*t
,
1051 const char *keyword
, const char *value
)
1053 struct mac_data
*mdata
= t
->data
;
1054 if (strcmp(keyword
, "Key") == 0)
1055 return test_bin(value
, &mdata
->key
, &mdata
->key_len
);
1056 if (strcmp(keyword
, "Algorithm") == 0) {
1057 mdata
->alg
= OPENSSL_strdup(value
);
1062 if (strcmp(keyword
, "Input") == 0)
1063 return test_bin(value
, &mdata
->input
, &mdata
->input_len
);
1064 if (strcmp(keyword
, "Output") == 0)
1065 return test_bin(value
, &mdata
->output
, &mdata
->output_len
);
1069 static int mac_test_run(struct evp_test
*t
)
1071 struct mac_data
*mdata
= t
->data
;
1072 const char *err
= "INTERNAL_ERROR";
1073 EVP_MD_CTX
*mctx
= NULL
;
1074 EVP_PKEY_CTX
*pctx
= NULL
, *genctx
= NULL
;
1075 EVP_PKEY
*key
= NULL
;
1076 const EVP_MD
*md
= NULL
;
1077 unsigned char *mac
= NULL
;
1080 err
= "MAC_PKEY_CTX_ERROR";
1081 genctx
= EVP_PKEY_CTX_new_id(mdata
->type
, NULL
);
1085 err
= "MAC_KEYGEN_INIT_ERROR";
1086 if (EVP_PKEY_keygen_init(genctx
) <= 0)
1088 if (mdata
->type
== EVP_PKEY_CMAC
) {
1089 err
= "MAC_ALGORITHM_SET_ERROR";
1090 if (EVP_PKEY_CTX_ctrl_str(genctx
, "cipher", mdata
->alg
) <= 0)
1094 err
= "MAC_KEY_SET_ERROR";
1095 if (EVP_PKEY_CTX_set_mac_key(genctx
, mdata
->key
, mdata
->key_len
) <= 0)
1098 err
= "MAC_KEY_GENERATE_ERROR";
1099 if (EVP_PKEY_keygen(genctx
, &key
) <= 0)
1101 if (mdata
->type
== EVP_PKEY_HMAC
) {
1102 err
= "MAC_ALGORITHM_SET_ERROR";
1103 md
= EVP_get_digestbyname(mdata
->alg
);
1107 mctx
= EVP_MD_CTX_new();
1110 err
= "DIGESTSIGNINIT_ERROR";
1111 if (!EVP_DigestSignInit(mctx
, &pctx
, md
, NULL
, key
))
1114 err
= "DIGESTSIGNUPDATE_ERROR";
1115 if (!EVP_DigestSignUpdate(mctx
, mdata
->input
, mdata
->input_len
))
1117 err
= "DIGESTSIGNFINAL_LENGTH_ERROR";
1118 if (!EVP_DigestSignFinal(mctx
, NULL
, &mac_len
))
1120 mac
= OPENSSL_malloc(mac_len
);
1122 fprintf(stderr
, "Error allocating mac buffer!\n");
1125 if (!EVP_DigestSignFinal(mctx
, mac
, &mac_len
))
1127 err
= "MAC_LENGTH_MISMATCH";
1128 if (mac_len
!= mdata
->output_len
)
1130 err
= "MAC_MISMATCH";
1131 if (check_output(t
, mdata
->output
, mac
, mac_len
))
1135 EVP_MD_CTX_free(mctx
);
1137 EVP_PKEY_CTX_free(genctx
);
1143 static const struct evp_test_method mac_test_method
= {
1152 * Public key operations. These are all very similar and can share
1153 * a lot of common code.
1157 /* Context for this operation */
1159 /* Key operation to perform */
1160 int (*keyop
) (EVP_PKEY_CTX
*ctx
,
1161 unsigned char *sig
, size_t *siglen
,
1162 const unsigned char *tbs
, size_t tbslen
);
1164 unsigned char *input
;
1166 /* Expected output */
1167 unsigned char *output
;
1172 * Perform public key operation setup: lookup key, allocated ctx and call
1173 * the appropriate initialisation function
1175 static int pkey_test_init(struct evp_test
*t
, const char *name
,
1177 int (*keyopinit
) (EVP_PKEY_CTX
*ctx
),
1178 int (*keyop
) (EVP_PKEY_CTX
*ctx
,
1179 unsigned char *sig
, size_t *siglen
,
1180 const unsigned char *tbs
,
1184 struct pkey_data
*kdata
;
1185 EVP_PKEY
*pkey
= NULL
;
1188 rv
= find_key(&pkey
, name
, t
->public);
1190 rv
= find_key(&pkey
, name
, t
->private);
1198 kdata
= OPENSSL_malloc(sizeof(*kdata
));
1200 EVP_PKEY_free(pkey
);
1204 kdata
->input
= NULL
;
1205 kdata
->output
= NULL
;
1206 kdata
->keyop
= keyop
;
1208 kdata
->ctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
1211 if (keyopinit(kdata
->ctx
) <= 0)
1216 static void pkey_test_cleanup(struct evp_test
*t
)
1218 struct pkey_data
*kdata
= t
->data
;
1220 OPENSSL_free(kdata
->input
);
1221 OPENSSL_free(kdata
->output
);
1222 EVP_PKEY_CTX_free(kdata
->ctx
);
1225 static int pkey_test_parse(struct evp_test
*t
,
1226 const char *keyword
, const char *value
)
1228 struct pkey_data
*kdata
= t
->data
;
1229 if (strcmp(keyword
, "Input") == 0)
1230 return test_bin(value
, &kdata
->input
, &kdata
->input_len
);
1231 if (strcmp(keyword
, "Output") == 0)
1232 return test_bin(value
, &kdata
->output
, &kdata
->output_len
);
1233 if (strcmp(keyword
, "Ctrl") == 0) {
1234 char *p
= strchr(value
, ':');
1237 if (EVP_PKEY_CTX_ctrl_str(kdata
->ctx
, value
, p
) <= 0)
1244 static int pkey_test_run(struct evp_test
*t
)
1246 struct pkey_data
*kdata
= t
->data
;
1247 unsigned char *out
= NULL
;
1249 const char *err
= "KEYOP_LENGTH_ERROR";
1250 if (kdata
->keyop(kdata
->ctx
, NULL
, &out_len
, kdata
->input
,
1251 kdata
->input_len
) <= 0)
1253 out
= OPENSSL_malloc(out_len
);
1255 fprintf(stderr
, "Error allocating output buffer!\n");
1258 err
= "KEYOP_ERROR";
1260 (kdata
->ctx
, out
, &out_len
, kdata
->input
, kdata
->input_len
) <= 0)
1262 err
= "KEYOP_LENGTH_MISMATCH";
1263 if (out_len
!= kdata
->output_len
)
1265 err
= "KEYOP_MISMATCH";
1266 if (check_output(t
, kdata
->output
, out
, out_len
))
1275 static int sign_test_init(struct evp_test
*t
, const char *name
)
1277 return pkey_test_init(t
, name
, 0, EVP_PKEY_sign_init
, EVP_PKEY_sign
);
1280 static const struct evp_test_method psign_test_method
= {
1288 static int verify_recover_test_init(struct evp_test
*t
, const char *name
)
1290 return pkey_test_init(t
, name
, 1, EVP_PKEY_verify_recover_init
,
1291 EVP_PKEY_verify_recover
);
1294 static const struct evp_test_method pverify_recover_test_method
= {
1296 verify_recover_test_init
,
1302 static int decrypt_test_init(struct evp_test
*t
, const char *name
)
1304 return pkey_test_init(t
, name
, 0, EVP_PKEY_decrypt_init
,
1308 static const struct evp_test_method pdecrypt_test_method
= {
1316 static int verify_test_init(struct evp_test
*t
, const char *name
)
1318 return pkey_test_init(t
, name
, 1, EVP_PKEY_verify_init
, 0);
1321 static int verify_test_run(struct evp_test
*t
)
1323 struct pkey_data
*kdata
= t
->data
;
1324 if (EVP_PKEY_verify(kdata
->ctx
, kdata
->output
, kdata
->output_len
,
1325 kdata
->input
, kdata
->input_len
) <= 0)
1326 t
->err
= "VERIFY_ERROR";
1330 static const struct evp_test_method pverify_test_method
= {
1340 #define PBE_TYPE_SCRYPT 1
1341 #define PBE_TYPE_PBKDF2 2
1342 #define PBE_TYPE_PKCS12 3
1348 /* scrypt parameters */
1349 uint64_t N
, r
, p
, maxmem
;
1351 /* PKCS#12 parameters */
1356 unsigned char *pass
;
1360 unsigned char *salt
;
1363 /* Expected output */
1368 #ifndef OPENSSL_NO_SCRYPT
1369 static int scrypt_test_parse(struct evp_test
*t
,
1370 const char *keyword
, const char *value
)
1372 struct pbe_data
*pdata
= t
->data
;
1374 if (strcmp(keyword
, "N") == 0)
1375 return test_uint64(value
, &pdata
->N
);
1376 if (strcmp(keyword
, "p") == 0)
1377 return test_uint64(value
, &pdata
->p
);
1378 if (strcmp(keyword
, "r") == 0)
1379 return test_uint64(value
, &pdata
->r
);
1380 if (strcmp(keyword
, "maxmem") == 0)
1381 return test_uint64(value
, &pdata
->maxmem
);
1386 static int pbkdf2_test_parse(struct evp_test
*t
,
1387 const char *keyword
, const char *value
)
1389 struct pbe_data
*pdata
= t
->data
;
1391 if (strcmp(keyword
, "iter") == 0) {
1392 pdata
->iter
= atoi(value
);
1393 if (pdata
->iter
<= 0)
1397 if (strcmp(keyword
, "MD") == 0) {
1398 pdata
->md
= EVP_get_digestbyname(value
);
1399 if (pdata
->md
== NULL
)
1406 static int pkcs12_test_parse(struct evp_test
*t
,
1407 const char *keyword
, const char *value
)
1409 struct pbe_data
*pdata
= t
->data
;
1411 if (strcmp(keyword
, "id") == 0) {
1412 pdata
->id
= atoi(value
);
1417 return pbkdf2_test_parse(t
, keyword
, value
);
1420 static int pbe_test_init(struct evp_test
*t
, const char *alg
)
1422 struct pbe_data
*pdat
;
1425 #ifndef OPENSSL_NO_SCRYPT
1426 if (strcmp(alg
, "scrypt") == 0)
1427 pbe_type
= PBE_TYPE_SCRYPT
;
1429 else if (strcmp(alg
, "pbkdf2") == 0)
1430 pbe_type
= PBE_TYPE_PBKDF2
;
1431 else if (strcmp(alg
, "pkcs12") == 0)
1432 pbe_type
= PBE_TYPE_PKCS12
;
1434 fprintf(stderr
, "Unknown pbe algorithm %s\n", alg
);
1435 pdat
= OPENSSL_malloc(sizeof(*pdat
));
1436 pdat
->pbe_type
= pbe_type
;
1450 static void pbe_test_cleanup(struct evp_test
*t
)
1452 struct pbe_data
*pdat
= t
->data
;
1453 test_free(pdat
->pass
);
1454 test_free(pdat
->salt
);
1455 test_free(pdat
->key
);
1458 static int pbe_test_parse(struct evp_test
*t
,
1459 const char *keyword
, const char *value
)
1461 struct pbe_data
*pdata
= t
->data
;
1463 if (strcmp(keyword
, "Password") == 0)
1464 return test_bin(value
, &pdata
->pass
, &pdata
->pass_len
);
1465 if (strcmp(keyword
, "Salt") == 0)
1466 return test_bin(value
, &pdata
->salt
, &pdata
->salt_len
);
1467 if (strcmp(keyword
, "Key") == 0)
1468 return test_bin(value
, &pdata
->key
, &pdata
->key_len
);
1469 if (pdata
->pbe_type
== PBE_TYPE_PBKDF2
)
1470 return pbkdf2_test_parse(t
, keyword
, value
);
1471 else if (pdata
->pbe_type
== PBE_TYPE_PKCS12
)
1472 return pkcs12_test_parse(t
, keyword
, value
);
1473 #ifndef OPENSSL_NO_SCRYPT
1474 else if (pdata
->pbe_type
== PBE_TYPE_SCRYPT
)
1475 return scrypt_test_parse(t
, keyword
, value
);
1480 static int pbe_test_run(struct evp_test
*t
)
1482 struct pbe_data
*pdata
= t
->data
;
1483 const char *err
= "INTERNAL_ERROR";
1486 key
= OPENSSL_malloc(pdata
->key_len
);
1489 if (pdata
->pbe_type
== PBE_TYPE_PBKDF2
) {
1490 err
= "PBKDF2_ERROR";
1491 if (PKCS5_PBKDF2_HMAC((char *)pdata
->pass
, pdata
->pass_len
,
1492 pdata
->salt
, pdata
->salt_len
,
1493 pdata
->iter
, pdata
->md
,
1494 pdata
->key_len
, key
) == 0)
1496 #ifndef OPENSSL_NO_SCRYPT
1497 } else if (pdata
->pbe_type
== PBE_TYPE_SCRYPT
) {
1498 err
= "SCRYPT_ERROR";
1499 if (EVP_PBE_scrypt((const char *)pdata
->pass
, pdata
->pass_len
,
1500 pdata
->salt
, pdata
->salt_len
,
1501 pdata
->N
, pdata
->r
, pdata
->p
, pdata
->maxmem
,
1502 key
, pdata
->key_len
) == 0)
1505 } else if (pdata
->pbe_type
== PBE_TYPE_PKCS12
) {
1506 err
= "PKCS12_ERROR";
1507 if (PKCS12_key_gen_uni(pdata
->pass
, pdata
->pass_len
,
1508 pdata
->salt
, pdata
->salt_len
,
1509 pdata
->id
, pdata
->iter
, pdata
->key_len
,
1510 key
, pdata
->md
) == 0)
1513 err
= "KEY_MISMATCH";
1514 if (check_output(t
, pdata
->key
, key
, pdata
->key_len
))
1523 static const struct evp_test_method pbe_test_method
= {
1534 BASE64_CANONICAL_ENCODING
= 0,
1535 BASE64_VALID_ENCODING
= 1,
1536 BASE64_INVALID_ENCODING
= 2
1537 } base64_encoding_type
;
1539 struct encode_data
{
1540 /* Input to encoding */
1541 unsigned char *input
;
1543 /* Expected output */
1544 unsigned char *output
;
1546 base64_encoding_type encoding
;
1549 static int encode_test_init(struct evp_test
*t
, const char *encoding
)
1551 struct encode_data
*edata
= OPENSSL_zalloc(sizeof(*edata
));
1553 if (strcmp(encoding
, "canonical") == 0) {
1554 edata
->encoding
= BASE64_CANONICAL_ENCODING
;
1555 } else if (strcmp(encoding
, "valid") == 0) {
1556 edata
->encoding
= BASE64_VALID_ENCODING
;
1557 } else if (strcmp(encoding
, "invalid") == 0) {
1558 edata
->encoding
= BASE64_INVALID_ENCODING
;
1559 t
->expected_err
= OPENSSL_strdup("DECODE_ERROR");
1560 if (t
->expected_err
== NULL
)
1563 fprintf(stderr
, "Bad encoding: %s. Should be one of "
1564 "{canonical, valid, invalid}\n", encoding
);
1571 static void encode_test_cleanup(struct evp_test
*t
)
1573 struct encode_data
*edata
= t
->data
;
1574 test_free(edata
->input
);
1575 test_free(edata
->output
);
1576 memset(edata
, 0, sizeof(*edata
));
1579 static int encode_test_parse(struct evp_test
*t
,
1580 const char *keyword
, const char *value
)
1582 struct encode_data
*edata
= t
->data
;
1583 if (strcmp(keyword
, "Input") == 0)
1584 return test_bin(value
, &edata
->input
, &edata
->input_len
);
1585 if (strcmp(keyword
, "Output") == 0)
1586 return test_bin(value
, &edata
->output
, &edata
->output_len
);
1590 static int encode_test_run(struct evp_test
*t
)
1592 struct encode_data
*edata
= t
->data
;
1593 unsigned char *encode_out
= NULL
, *decode_out
= NULL
;
1594 int output_len
, chunk_len
;
1595 const char *err
= "INTERNAL_ERROR";
1596 EVP_ENCODE_CTX
*decode_ctx
= EVP_ENCODE_CTX_new();
1598 if (decode_ctx
== NULL
)
1601 if (edata
->encoding
== BASE64_CANONICAL_ENCODING
) {
1602 EVP_ENCODE_CTX
*encode_ctx
= EVP_ENCODE_CTX_new();
1603 if (encode_ctx
== NULL
)
1605 encode_out
= OPENSSL_malloc(EVP_ENCODE_LENGTH(edata
->input_len
));
1606 if (encode_out
== NULL
)
1609 EVP_EncodeInit(encode_ctx
);
1610 EVP_EncodeUpdate(encode_ctx
, encode_out
, &chunk_len
,
1611 edata
->input
, edata
->input_len
);
1612 output_len
= chunk_len
;
1614 EVP_EncodeFinal(encode_ctx
, encode_out
+ chunk_len
, &chunk_len
);
1615 output_len
+= chunk_len
;
1617 EVP_ENCODE_CTX_free(encode_ctx
);
1619 if (check_var_length_output(t
, edata
->output
, edata
->output_len
,
1620 encode_out
, output_len
)) {
1621 err
= "BAD_ENCODING";
1626 decode_out
= OPENSSL_malloc(EVP_DECODE_LENGTH(edata
->output_len
));
1627 if (decode_out
== NULL
)
1630 EVP_DecodeInit(decode_ctx
);
1631 if (EVP_DecodeUpdate(decode_ctx
, decode_out
, &chunk_len
, edata
->output
,
1632 edata
->output_len
) < 0) {
1633 err
= "DECODE_ERROR";
1636 output_len
= chunk_len
;
1638 if (EVP_DecodeFinal(decode_ctx
, decode_out
+ chunk_len
, &chunk_len
) != 1) {
1639 err
= "DECODE_ERROR";
1642 output_len
+= chunk_len
;
1644 if (edata
->encoding
!= BASE64_INVALID_ENCODING
&&
1645 check_var_length_output(t
, edata
->input
, edata
->input_len
,
1646 decode_out
, output_len
)) {
1647 err
= "BAD_DECODING";
1654 OPENSSL_free(encode_out
);
1655 OPENSSL_free(decode_out
);
1656 EVP_ENCODE_CTX_free(decode_ctx
);
1660 static const struct evp_test_method encode_test_method
= {
1663 encode_test_cleanup
,