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>
64 /* Remove spaces from beginning and end of a string */
66 static void remove_space(char **pval
)
68 unsigned char *p
= (unsigned char *)*pval
;
75 p
= p
+ strlen(*pval
) - 1;
77 /* Remove trailing space */
83 * Given a line of the form:
84 * name = value # comment
85 * extract name and value. NB: modifies passed buffer.
88 static int parse_line(char **pkw
, char **pval
, char *linebuf
)
92 p
= linebuf
+ strlen(linebuf
) - 1;
95 fprintf(stderr
, "FATAL: missing EOL\n");
101 p
= strchr(linebuf
, '#');
106 /* Look for = sign */
107 p
= strchr(linebuf
, '=');
118 /* Remove spaces from keyword and value */
125 /* For a hex string "value" convert to a binary allocated buffer */
126 static int test_bin(const char *value
, unsigned char **buf
, size_t *buflen
)
130 /* Don't return NULL for zero length buffer */
131 *buf
= OPENSSL_malloc(1);
138 /* Check for string literal */
139 if (value
[0] == '"') {
142 vlen
= strlen(value
);
143 if (value
[vlen
- 1] != '"')
146 *buf
= BUF_memdup(value
, vlen
);
150 *buf
= string_to_hex(value
, &len
);
152 fprintf(stderr
, "Value=%s\n", value
);
153 ERR_print_errors_fp(stderr
);
156 /* Size of input buffer means we'll never overflow */
161 /* Structure holding test information */
163 /* file being read */
165 /* List of public and private keys */
166 struct key_list
*private;
167 struct key_list
*public;
168 /* method for this test */
169 const struct evp_test_method
*meth
;
170 /* current line being processed */
172 /* start line of current test */
173 unsigned int start_line
;
174 /* Error string for test */
176 /* Expected error value of test */
178 /* Number of tests */
182 /* Number of tests skipped */
184 /* If output mismatch expected and got value */
185 unsigned char *out_got
;
186 unsigned char *out_expected
;
188 /* test specific data */
190 /* Current test should be skipped */
197 struct key_list
*next
;
200 /* Test method structure */
201 struct evp_test_method
{
202 /* Name of test as it appears in file */
204 /* Initialise test for "alg" */
205 int (*init
) (struct evp_test
* t
, const char *alg
);
206 /* Clean up method */
207 void (*cleanup
) (struct evp_test
* t
);
208 /* Test specific name value pair processing */
209 int (*parse
) (struct evp_test
* t
, const char *name
, const char *value
);
210 /* Run the test itself */
211 int (*run_test
) (struct evp_test
* t
);
214 static const struct evp_test_method digest_test_method
, cipher_test_method
;
215 static const struct evp_test_method mac_test_method
;
216 static const struct evp_test_method psign_test_method
, pverify_test_method
;
217 static const struct evp_test_method pdecrypt_test_method
;
218 static const struct evp_test_method pverify_recover_test_method
;
220 static const struct evp_test_method
*evp_test_list
[] = {
225 &pverify_test_method
,
226 &pdecrypt_test_method
,
227 &pverify_recover_test_method
,
231 static const struct evp_test_method
*evp_find_test(const char *name
)
233 const struct evp_test_method
**tt
;
234 for (tt
= evp_test_list
; *tt
; tt
++) {
235 if (!strcmp(name
, (*tt
)->name
))
241 static void hex_print(const char *name
, const unsigned char *buf
, size_t len
)
244 fprintf(stderr
, "%s ", name
);
245 for (i
= 0; i
< len
; i
++)
246 fprintf(stderr
, "%02X", buf
[i
]);
250 static void free_expected(struct evp_test
*t
)
252 if (t
->expected_err
) {
253 OPENSSL_free(t
->expected_err
);
254 t
->expected_err
= NULL
;
256 if (t
->out_expected
) {
257 OPENSSL_free(t
->out_expected
);
258 OPENSSL_free(t
->out_got
);
259 t
->out_expected
= NULL
;
264 static void print_expected(struct evp_test
*t
)
266 if (t
->out_expected
== NULL
)
268 hex_print("Expected:", t
->out_expected
, t
->out_len
);
269 hex_print("Got: ", t
->out_got
, t
->out_len
);
273 static int check_test_error(struct evp_test
*t
)
275 if (!t
->err
&& !t
->expected_err
)
277 if (t
->err
&& !t
->expected_err
) {
278 fprintf(stderr
, "Test line %d: unexpected error %s\n",
279 t
->start_line
, t
->err
);
283 if (!t
->err
&& t
->expected_err
) {
284 fprintf(stderr
, "Test line %d: succeeded expecting %s\n",
285 t
->start_line
, t
->expected_err
);
288 if (!strcmp(t
->err
, t
->expected_err
))
291 fprintf(stderr
, "Test line %d: expecting %s got %s\n",
292 t
->start_line
, t
->expected_err
, t
->err
);
296 /* Setup a new test, run any existing test */
298 static int setup_test(struct evp_test
*t
, const struct evp_test_method
*tmeth
)
300 /* If we already have a test set up run it */
309 if (t
->meth
->run_test(t
) != 1) {
310 fprintf(stderr
, "%s test error line %d\n",
311 t
->meth
->name
, t
->start_line
);
314 if (!check_test_error(t
)) {
316 ERR_print_errors_fp(stderr
);
321 OPENSSL_free(t
->data
);
323 if (t
->expected_err
) {
324 OPENSSL_free(t
->expected_err
);
325 t
->expected_err
= NULL
;
333 static int find_key(EVP_PKEY
**ppk
, const char *name
, struct key_list
*lst
)
335 for (; lst
; lst
= lst
->next
) {
336 if (!strcmp(lst
->name
, name
)) {
345 static void free_key_list(struct key_list
*lst
)
347 while (lst
!= NULL
) {
348 struct key_list
*ltmp
;
349 EVP_PKEY_free(lst
->key
);
350 OPENSSL_free(lst
->name
);
357 static int check_unsupported()
359 long err
= ERR_peek_error();
360 if (ERR_GET_LIB(err
) == ERR_LIB_EVP
361 && ERR_GET_REASON(err
) == EVP_R_UNSUPPORTED_ALGORITHM
) {
368 static int process_test(struct evp_test
*t
, char *buf
, int verbose
)
370 char *keyword
, *value
;
371 int rv
= 0, add_key
= 0;
373 struct key_list
**lst
, *key
;
375 const struct evp_test_method
*tmeth
;
378 if (!parse_line(&keyword
, &value
, buf
))
380 if (!strcmp(keyword
, "PrivateKey")) {
381 save_pos
= ftell(t
->in
);
382 pk
= PEM_read_PrivateKey(t
->in
, NULL
, 0, NULL
);
383 if (pk
== NULL
&& !check_unsupported()) {
384 fprintf(stderr
, "Error reading private key %s\n", value
);
385 ERR_print_errors_fp(stderr
);
391 if (!strcmp(keyword
, "PublicKey")) {
392 save_pos
= ftell(t
->in
);
393 pk
= PEM_read_PUBKEY(t
->in
, NULL
, 0, NULL
);
394 if (pk
== NULL
&& !check_unsupported()) {
395 fprintf(stderr
, "Error reading public key %s\n", value
);
396 ERR_print_errors_fp(stderr
);
402 /* If we have a key add to list */
405 if (find_key(NULL
, value
, *lst
)) {
406 fprintf(stderr
, "Duplicate key %s\n", value
);
409 key
= OPENSSL_malloc(sizeof(struct key_list
));
412 key
->name
= BUF_strdup(value
);
416 /* Rewind input, read to end and update line numbers */
417 fseek(t
->in
, save_pos
, SEEK_SET
);
418 while (fgets(tmpbuf
, sizeof(tmpbuf
), t
->in
)) {
420 if (!strncmp(tmpbuf
, "-----END", 8))
423 fprintf(stderr
, "Can't find key end\n");
427 /* See if keyword corresponds to a test start */
428 tmeth
= evp_find_test(keyword
);
430 if (!setup_test(t
, tmeth
))
432 t
->start_line
= t
->line
;
434 if (!tmeth
->init(t
, value
)) {
435 fprintf(stderr
, "Unknown %s: %s\n", keyword
, value
);
439 } else if (t
->skip
) {
441 } else if (!strcmp(keyword
, "Result")) {
442 if (t
->expected_err
) {
443 fprintf(stderr
, "Line %d: multiple result lines\n", t
->line
);
446 t
->expected_err
= BUF_strdup(value
);
447 if (!t
->expected_err
)
450 /* Must be test specific line: try to parse it */
452 rv
= t
->meth
->parse(t
, keyword
, value
);
455 fprintf(stderr
, "line %d: unexpected keyword %s\n",
459 fprintf(stderr
, "line %d: error processing keyword %s\n",
467 static int check_output(struct evp_test
*t
, const unsigned char *expected
,
468 const unsigned char *got
, size_t len
)
470 if (!memcmp(expected
, got
, len
))
472 t
->out_expected
= BUF_memdup(expected
, len
);
473 t
->out_got
= BUF_memdup(got
, len
);
475 if (t
->out_expected
== NULL
|| t
->out_got
== NULL
) {
476 fprintf(stderr
, "Memory allocation error!\n");
482 int main(int argc
, char **argv
)
489 fprintf(stderr
, "usage: evp_test testfile.txt\n");
493 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
495 ERR_load_crypto_strings();
496 OpenSSL_add_all_algorithms();
498 memset(&t
, 0, sizeof(t
));
507 t
.out_expected
= NULL
;
510 in
= fopen(argv
[1], "r");
512 while (fgets(buf
, sizeof(buf
), in
)) {
514 if (!process_test(&t
, buf
, 0))
517 /* Run any final test we have */
518 if (!setup_test(&t
, NULL
))
520 fprintf(stderr
, "%d tests completed with %d errors, %d skipped\n",
521 t
.ntests
, t
.errors
, t
.nskip
);
522 free_key_list(t
.public);
523 free_key_list(t
.private);
526 CRYPTO_cleanup_all_ex_data();
527 ERR_remove_thread_state(NULL
);
529 CRYPTO_mem_leaks_fp(stderr
);
535 static void test_free(void *d
)
541 /* Message digest tests */
544 /* Digest this test is for */
545 const EVP_MD
*digest
;
546 /* Input to digest */
547 unsigned char *input
;
549 /* Repeat count for input */
551 /* Expected output */
552 unsigned char *output
;
556 static int digest_test_init(struct evp_test
*t
, const char *alg
)
558 const EVP_MD
*digest
;
559 struct digest_data
*mdat
= t
->data
;
560 digest
= EVP_get_digestbyname(alg
);
562 /* If alg has an OID assume disabled algorithm */
563 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
569 mdat
= OPENSSL_malloc(sizeof(struct digest_data
));
570 mdat
->digest
= digest
;
578 static void digest_test_cleanup(struct evp_test
*t
)
580 struct digest_data
*mdat
= t
->data
;
581 test_free(mdat
->input
);
582 test_free(mdat
->output
);
585 static int digest_test_parse(struct evp_test
*t
,
586 const char *keyword
, const char *value
)
588 struct digest_data
*mdata
= t
->data
;
589 if (!strcmp(keyword
, "Input"))
590 return test_bin(value
, &mdata
->input
, &mdata
->input_len
);
591 if (!strcmp(keyword
, "Output"))
592 return test_bin(value
, &mdata
->output
, &mdata
->output_len
);
593 if (!strcmp(keyword
, "Count")) {
594 long nrpt
= atoi(value
);
597 mdata
->nrpt
= (size_t)nrpt
;
603 static int digest_test_run(struct evp_test
*t
)
605 struct digest_data
*mdata
= t
->data
;
607 const char *err
= "INTERNAL_ERROR";
609 unsigned char md
[EVP_MAX_MD_SIZE
];
611 mctx
= EVP_MD_CTX_create();
614 err
= "DIGESTINIT_ERROR";
615 if (!EVP_DigestInit_ex(mctx
, mdata
->digest
, NULL
))
617 err
= "DIGESTUPDATE_ERROR";
618 for (i
= 0; i
< mdata
->nrpt
; i
++) {
619 if (!EVP_DigestUpdate(mctx
, mdata
->input
, mdata
->input_len
))
622 err
= "DIGESTFINAL_ERROR";
623 if (!EVP_DigestFinal(mctx
, md
, &md_len
))
625 err
= "DIGEST_LENGTH_MISMATCH";
626 if (md_len
!= mdata
->output_len
)
628 err
= "DIGEST_MISMATCH";
629 if (check_output(t
, mdata
->output
, md
, md_len
))
634 EVP_MD_CTX_destroy(mctx
);
639 static const struct evp_test_method digest_test_method
= {
649 const EVP_CIPHER
*cipher
;
651 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
657 unsigned char *plaintext
;
658 size_t plaintext_len
;
659 unsigned char *ciphertext
;
660 size_t ciphertext_len
;
668 static int cipher_test_init(struct evp_test
*t
, const char *alg
)
670 const EVP_CIPHER
*cipher
;
671 struct cipher_data
*cdat
= t
->data
;
672 cipher
= EVP_get_cipherbyname(alg
);
674 /* If alg has an OID assume disabled algorithm */
675 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
681 cdat
= OPENSSL_malloc(sizeof(struct cipher_data
));
682 cdat
->cipher
= cipher
;
686 cdat
->ciphertext
= NULL
;
687 cdat
->plaintext
= NULL
;
691 if (EVP_CIPHER_mode(cipher
) == EVP_CIPH_GCM_MODE
692 || EVP_CIPHER_mode(cipher
) == EVP_CIPH_OCB_MODE
693 || EVP_CIPHER_mode(cipher
) == EVP_CIPH_CCM_MODE
)
694 cdat
->aead
= EVP_CIPHER_mode(cipher
);
701 static void cipher_test_cleanup(struct evp_test
*t
)
703 struct cipher_data
*cdat
= t
->data
;
704 test_free(cdat
->key
);
706 test_free(cdat
->ciphertext
);
707 test_free(cdat
->plaintext
);
708 test_free(cdat
->aad
);
709 test_free(cdat
->tag
);
712 static int cipher_test_parse(struct evp_test
*t
, const char *keyword
,
715 struct cipher_data
*cdat
= t
->data
;
716 if (!strcmp(keyword
, "Key"))
717 return test_bin(value
, &cdat
->key
, &cdat
->key_len
);
718 if (!strcmp(keyword
, "IV"))
719 return test_bin(value
, &cdat
->iv
, &cdat
->iv_len
);
720 if (!strcmp(keyword
, "Plaintext"))
721 return test_bin(value
, &cdat
->plaintext
, &cdat
->plaintext_len
);
722 if (!strcmp(keyword
, "Ciphertext"))
723 return test_bin(value
, &cdat
->ciphertext
, &cdat
->ciphertext_len
);
725 if (!strcmp(keyword
, "AAD"))
726 return test_bin(value
, &cdat
->aad
, &cdat
->aad_len
);
727 if (!strcmp(keyword
, "Tag"))
728 return test_bin(value
, &cdat
->tag
, &cdat
->tag_len
);
731 if (!strcmp(keyword
, "Operation")) {
732 if (!strcmp(value
, "ENCRYPT"))
734 else if (!strcmp(value
, "DECRYPT"))
743 static int cipher_test_enc(struct evp_test
*t
, int enc
)
745 struct cipher_data
*cdat
= t
->data
;
746 unsigned char *in
, *out
, *tmp
= NULL
;
747 size_t in_len
, out_len
;
749 EVP_CIPHER_CTX
*ctx
= NULL
;
751 err
= "INTERNAL_ERROR";
752 ctx
= EVP_CIPHER_CTX_new();
755 EVP_CIPHER_CTX_set_flags(ctx
, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
);
757 in
= cdat
->plaintext
;
758 in_len
= cdat
->plaintext_len
;
759 out
= cdat
->ciphertext
;
760 out_len
= cdat
->ciphertext_len
;
762 in
= cdat
->ciphertext
;
763 in_len
= cdat
->ciphertext_len
;
764 out
= cdat
->plaintext
;
765 out_len
= cdat
->plaintext_len
;
767 tmp
= OPENSSL_malloc(in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
);
770 err
= "CIPHERINIT_ERROR";
771 if (!EVP_CipherInit_ex(ctx
, cdat
->cipher
, NULL
, NULL
, NULL
, enc
))
773 err
= "INVALID_IV_LENGTH";
776 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_IVLEN
,
779 } else if (cdat
->iv_len
!= (size_t)EVP_CIPHER_CTX_iv_length(ctx
))
785 * If encrypting or OCB just set tag length initially, otherwise
786 * set tag length and value.
788 if (enc
|| cdat
->aead
== EVP_CIPH_OCB_MODE
) {
789 err
= "TAG_LENGTH_SET_ERROR";
792 err
= "TAG_SET_ERROR";
795 if (tag
|| cdat
->aead
!= EVP_CIPH_GCM_MODE
) {
796 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_TAG
,
802 err
= "INVALID_KEY_LENGTH";
803 if (!EVP_CIPHER_CTX_set_key_length(ctx
, cdat
->key_len
))
805 err
= "KEY_SET_ERROR";
806 if (!EVP_CipherInit_ex(ctx
, NULL
, NULL
, cdat
->key
, cdat
->iv
, -1))
809 if (!enc
&& cdat
->aead
== EVP_CIPH_OCB_MODE
) {
810 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_TAG
,
811 cdat
->tag_len
, cdat
->tag
)) {
812 err
= "TAG_SET_ERROR";
817 if (cdat
->aead
== EVP_CIPH_CCM_MODE
) {
818 if (!EVP_CipherUpdate(ctx
, NULL
, &tmplen
, NULL
, out_len
)) {
819 err
= "CCM_PLAINTEXT_LENGTH_SET_ERROR";
824 if (!EVP_CipherUpdate(ctx
, NULL
, &tmplen
, cdat
->aad
, cdat
->aad_len
)) {
825 err
= "AAD_SET_ERROR";
829 EVP_CIPHER_CTX_set_padding(ctx
, 0);
830 err
= "CIPHERUPDATE_ERROR";
831 if (!EVP_CipherUpdate(ctx
, tmp
, &tmplen
, in
, in_len
))
833 if (cdat
->aead
== EVP_CIPH_CCM_MODE
)
836 err
= "CIPHERFINAL_ERROR";
837 if (!EVP_CipherFinal_ex(ctx
, tmp
+ tmplen
, &tmpflen
))
840 err
= "LENGTH_MISMATCH";
841 if (out_len
!= (size_t)(tmplen
+ tmpflen
))
843 err
= "VALUE_MISMATCH";
844 if (check_output(t
, out
, tmp
, out_len
))
846 if (enc
&& cdat
->aead
) {
847 unsigned char rtag
[16];
848 if (cdat
->tag_len
> sizeof(rtag
)) {
849 err
= "TAG_LENGTH_INTERNAL_ERROR";
852 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_GET_TAG
,
853 cdat
->tag_len
, rtag
)) {
854 err
= "TAG_RETRIEVE_ERROR";
857 if (check_output(t
, cdat
->tag
, rtag
, cdat
->tag_len
)) {
858 err
= "TAG_VALUE_MISMATCH";
866 EVP_CIPHER_CTX_free(ctx
);
871 static int cipher_test_run(struct evp_test
*t
)
873 struct cipher_data
*cdat
= t
->data
;
879 if (!cdat
->iv
&& EVP_CIPHER_iv_length(cdat
->cipher
)) {
880 /* IV is optional and usually omitted in wrap mode */
881 if (EVP_CIPHER_mode(cdat
->cipher
) != EVP_CIPH_WRAP_MODE
) {
886 if (cdat
->aead
&& !cdat
->tag
) {
891 rv
= cipher_test_enc(t
, 1);
892 /* Not fatal errors: return */
899 if (cdat
->enc
!= 1) {
900 rv
= cipher_test_enc(t
, 0);
901 /* Not fatal errors: return */
911 static const struct evp_test_method cipher_test_method
= {
922 /* Algorithm string for this MAC */
928 unsigned char *input
;
930 /* Expected output */
931 unsigned char *output
;
935 static int mac_test_init(struct evp_test
*t
, const char *alg
)
938 struct mac_data
*mdat
;
939 if (!strcmp(alg
, "HMAC"))
940 type
= EVP_PKEY_HMAC
;
941 else if (!strcmp(alg
, "CMAC"))
942 type
= EVP_PKEY_CMAC
;
946 mdat
= OPENSSL_malloc(sizeof(struct mac_data
));
956 static void mac_test_cleanup(struct evp_test
*t
)
958 struct mac_data
*mdat
= t
->data
;
959 test_free(mdat
->alg
);
960 test_free(mdat
->key
);
961 test_free(mdat
->input
);
962 test_free(mdat
->output
);
965 static int mac_test_parse(struct evp_test
*t
,
966 const char *keyword
, const char *value
)
968 struct mac_data
*mdata
= t
->data
;
969 if (!strcmp(keyword
, "Key"))
970 return test_bin(value
, &mdata
->key
, &mdata
->key_len
);
971 if (!strcmp(keyword
, "Algorithm")) {
972 mdata
->alg
= BUF_strdup(value
);
977 if (!strcmp(keyword
, "Input"))
978 return test_bin(value
, &mdata
->input
, &mdata
->input_len
);
979 if (!strcmp(keyword
, "Output"))
980 return test_bin(value
, &mdata
->output
, &mdata
->output_len
);
984 static int mac_test_run(struct evp_test
*t
)
986 struct mac_data
*mdata
= t
->data
;
987 const char *err
= "INTERNAL_ERROR";
988 EVP_MD_CTX
*mctx
= NULL
;
989 EVP_PKEY_CTX
*pctx
= NULL
, *genctx
= NULL
;
990 EVP_PKEY
*key
= NULL
;
991 const EVP_MD
*md
= NULL
;
992 unsigned char *mac
= NULL
;
995 err
= "MAC_PKEY_CTX_ERROR";
996 genctx
= EVP_PKEY_CTX_new_id(mdata
->type
, NULL
);
1000 err
= "MAC_KEYGEN_INIT_ERROR";
1001 if (EVP_PKEY_keygen_init(genctx
) <= 0)
1003 if (mdata
->type
== EVP_PKEY_CMAC
) {
1004 err
= "MAC_ALGORITHM_SET_ERROR";
1005 if (EVP_PKEY_CTX_ctrl_str(genctx
, "cipher", mdata
->alg
) <= 0)
1009 err
= "MAC_KEY_SET_ERROR";
1010 if (EVP_PKEY_CTX_set_mac_key(genctx
, mdata
->key
, mdata
->key_len
) <= 0)
1013 err
= "MAC_KEY_GENERATE_ERROR";
1014 if (EVP_PKEY_keygen(genctx
, &key
) <= 0)
1016 if (mdata
->type
== EVP_PKEY_HMAC
) {
1017 err
= "MAC_ALGORITHM_SET_ERROR";
1018 md
= EVP_get_digestbyname(mdata
->alg
);
1022 mctx
= EVP_MD_CTX_create();
1025 err
= "DIGESTSIGNINIT_ERROR";
1026 if (!EVP_DigestSignInit(mctx
, &pctx
, md
, NULL
, key
))
1029 err
= "DIGESTSIGNUPDATE_ERROR";
1030 if (!EVP_DigestSignUpdate(mctx
, mdata
->input
, mdata
->input_len
))
1032 err
= "DIGESTSIGNFINAL_LENGTH_ERROR";
1033 if (!EVP_DigestSignFinal(mctx
, NULL
, &mac_len
))
1035 mac
= OPENSSL_malloc(mac_len
);
1037 fprintf(stderr
, "Error allocating mac buffer!\n");
1040 if (!EVP_DigestSignFinal(mctx
, mac
, &mac_len
))
1042 err
= "MAC_LENGTH_MISMATCH";
1043 if (mac_len
!= mdata
->output_len
)
1045 err
= "MAC_MISMATCH";
1046 if (check_output(t
, mdata
->output
, mac
, mac_len
))
1051 EVP_MD_CTX_destroy(mctx
);
1055 EVP_PKEY_CTX_free(genctx
);
1062 static const struct evp_test_method mac_test_method
= {
1071 * Public key operations. These are all very similar and can share
1072 * a lot of common code.
1076 /* Context for this operation */
1078 /* Key operation to perform */
1079 int (*keyop
) (EVP_PKEY_CTX
*ctx
,
1080 unsigned char *sig
, size_t *siglen
,
1081 const unsigned char *tbs
, size_t tbslen
);
1083 unsigned char *input
;
1085 /* Expected output */
1086 unsigned char *output
;
1091 * Perform public key operation setup: lookup key, allocated ctx and call
1092 * the appropriate initialisation function
1094 static int pkey_test_init(struct evp_test
*t
, const char *name
,
1096 int (*keyopinit
) (EVP_PKEY_CTX
*ctx
),
1097 int (*keyop
) (EVP_PKEY_CTX
*ctx
,
1098 unsigned char *sig
, size_t *siglen
,
1099 const unsigned char *tbs
,
1103 struct pkey_data
*kdata
;
1104 EVP_PKEY
*pkey
= NULL
;
1107 rv
= find_key(&pkey
, name
, t
->public);
1109 rv
= find_key(&pkey
, name
, t
->private);
1117 kdata
= OPENSSL_malloc(sizeof(struct pkey_data
));
1119 EVP_PKEY_free(pkey
);
1123 kdata
->input
= NULL
;
1124 kdata
->output
= NULL
;
1125 kdata
->keyop
= keyop
;
1127 kdata
->ctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
1130 if (keyopinit(kdata
->ctx
) <= 0)
1135 static void pkey_test_cleanup(struct evp_test
*t
)
1137 struct pkey_data
*kdata
= t
->data
;
1139 OPENSSL_free(kdata
->input
);
1141 OPENSSL_free(kdata
->output
);
1143 EVP_PKEY_CTX_free(kdata
->ctx
);
1146 static int pkey_test_parse(struct evp_test
*t
,
1147 const char *keyword
, const char *value
)
1149 struct pkey_data
*kdata
= t
->data
;
1150 if (!strcmp(keyword
, "Input"))
1151 return test_bin(value
, &kdata
->input
, &kdata
->input_len
);
1152 if (!strcmp(keyword
, "Output"))
1153 return test_bin(value
, &kdata
->output
, &kdata
->output_len
);
1154 if (!strcmp(keyword
, "Ctrl")) {
1155 char *p
= strchr(value
, ':');
1158 if (EVP_PKEY_CTX_ctrl_str(kdata
->ctx
, value
, p
) <= 0)
1165 static int pkey_test_run(struct evp_test
*t
)
1167 struct pkey_data
*kdata
= t
->data
;
1168 unsigned char *out
= NULL
;
1170 const char *err
= "KEYOP_LENGTH_ERROR";
1171 if (kdata
->keyop(kdata
->ctx
, NULL
, &out_len
, kdata
->input
,
1172 kdata
->input_len
) <= 0)
1174 out
= OPENSSL_malloc(out_len
);
1176 fprintf(stderr
, "Error allocating output buffer!\n");
1179 err
= "KEYOP_ERROR";
1181 (kdata
->ctx
, out
, &out_len
, kdata
->input
, kdata
->input_len
) <= 0)
1183 err
= "KEYOP_LENGTH_MISMATCH";
1184 if (out_len
!= kdata
->output_len
)
1186 err
= "KEYOP_MISMATCH";
1187 if (check_output(t
, kdata
->output
, out
, out_len
))
1197 static int sign_test_init(struct evp_test
*t
, const char *name
)
1199 return pkey_test_init(t
, name
, 0, EVP_PKEY_sign_init
, EVP_PKEY_sign
);
1202 static const struct evp_test_method psign_test_method
= {
1210 static int verify_recover_test_init(struct evp_test
*t
, const char *name
)
1212 return pkey_test_init(t
, name
, 1, EVP_PKEY_verify_recover_init
,
1213 EVP_PKEY_verify_recover
);
1216 static const struct evp_test_method pverify_recover_test_method
= {
1218 verify_recover_test_init
,
1224 static int decrypt_test_init(struct evp_test
*t
, const char *name
)
1226 return pkey_test_init(t
, name
, 0, EVP_PKEY_decrypt_init
,
1230 static const struct evp_test_method pdecrypt_test_method
= {
1238 static int verify_test_init(struct evp_test
*t
, const char *name
)
1240 return pkey_test_init(t
, name
, 1, EVP_PKEY_verify_init
, 0);
1243 static int verify_test_run(struct evp_test
*t
)
1245 struct pkey_data
*kdata
= t
->data
;
1246 if (EVP_PKEY_verify(kdata
->ctx
, kdata
->output
, kdata
->output_len
,
1247 kdata
->input
, kdata
->input_len
) <= 0)
1248 t
->err
= "VERIFY_ERROR";
1252 static const struct evp_test_method pverify_test_method
= {