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 OPENSSL_free(t
->expected_err
);
253 t
->expected_err
= NULL
;
254 OPENSSL_free(t
->out_expected
);
255 OPENSSL_free(t
->out_got
);
256 t
->out_expected
= NULL
;
260 static void print_expected(struct evp_test
*t
)
262 if (t
->out_expected
== NULL
)
264 hex_print("Expected:", t
->out_expected
, t
->out_len
);
265 hex_print("Got: ", t
->out_got
, t
->out_len
);
269 static int check_test_error(struct evp_test
*t
)
271 if (!t
->err
&& !t
->expected_err
)
273 if (t
->err
&& !t
->expected_err
) {
274 fprintf(stderr
, "Test line %d: unexpected error %s\n",
275 t
->start_line
, t
->err
);
279 if (!t
->err
&& t
->expected_err
) {
280 fprintf(stderr
, "Test line %d: succeeded expecting %s\n",
281 t
->start_line
, t
->expected_err
);
284 if (!strcmp(t
->err
, t
->expected_err
))
287 fprintf(stderr
, "Test line %d: expecting %s got %s\n",
288 t
->start_line
, t
->expected_err
, t
->err
);
292 /* Setup a new test, run any existing test */
294 static int setup_test(struct evp_test
*t
, const struct evp_test_method
*tmeth
)
296 /* If we already have a test set up run it */
305 if (t
->meth
->run_test(t
) != 1) {
306 fprintf(stderr
, "%s test error line %d\n",
307 t
->meth
->name
, t
->start_line
);
310 if (!check_test_error(t
)) {
312 ERR_print_errors_fp(stderr
);
317 OPENSSL_free(t
->data
);
319 OPENSSL_free(t
->expected_err
);
320 t
->expected_err
= NULL
;
327 static int find_key(EVP_PKEY
**ppk
, const char *name
, struct key_list
*lst
)
329 for (; lst
; lst
= lst
->next
) {
330 if (!strcmp(lst
->name
, name
)) {
339 static void free_key_list(struct key_list
*lst
)
341 while (lst
!= NULL
) {
342 struct key_list
*ltmp
;
343 EVP_PKEY_free(lst
->key
);
344 OPENSSL_free(lst
->name
);
351 static int check_unsupported()
353 long err
= ERR_peek_error();
354 if (ERR_GET_LIB(err
) == ERR_LIB_EVP
355 && ERR_GET_REASON(err
) == EVP_R_UNSUPPORTED_ALGORITHM
) {
362 static int process_test(struct evp_test
*t
, char *buf
, int verbose
)
364 char *keyword
= NULL
, *value
= NULL
;
365 int rv
= 0, add_key
= 0;
367 struct key_list
**lst
= NULL
, *key
= NULL
;
369 const struct evp_test_method
*tmeth
= NULL
;
372 if (!parse_line(&keyword
, &value
, buf
))
374 if (!strcmp(keyword
, "PrivateKey")) {
375 save_pos
= ftell(t
->in
);
376 pk
= PEM_read_PrivateKey(t
->in
, NULL
, 0, NULL
);
377 if (pk
== NULL
&& !check_unsupported()) {
378 fprintf(stderr
, "Error reading private key %s\n", value
);
379 ERR_print_errors_fp(stderr
);
385 if (!strcmp(keyword
, "PublicKey")) {
386 save_pos
= ftell(t
->in
);
387 pk
= PEM_read_PUBKEY(t
->in
, NULL
, 0, NULL
);
388 if (pk
== NULL
&& !check_unsupported()) {
389 fprintf(stderr
, "Error reading public key %s\n", value
);
390 ERR_print_errors_fp(stderr
);
396 /* If we have a key add to list */
399 if (find_key(NULL
, value
, *lst
)) {
400 fprintf(stderr
, "Duplicate key %s\n", value
);
403 key
= OPENSSL_malloc(sizeof(*key
));
406 key
->name
= BUF_strdup(value
);
410 /* Rewind input, read to end and update line numbers */
411 fseek(t
->in
, save_pos
, SEEK_SET
);
412 while (fgets(tmpbuf
, sizeof(tmpbuf
), t
->in
)) {
414 if (!strncmp(tmpbuf
, "-----END", 8))
417 fprintf(stderr
, "Can't find key end\n");
421 /* See if keyword corresponds to a test start */
422 tmeth
= evp_find_test(keyword
);
424 if (!setup_test(t
, tmeth
))
426 t
->start_line
= t
->line
;
428 if (!tmeth
->init(t
, value
)) {
429 fprintf(stderr
, "Unknown %s: %s\n", keyword
, value
);
433 } else if (t
->skip
) {
435 } else if (!strcmp(keyword
, "Result")) {
436 if (t
->expected_err
) {
437 fprintf(stderr
, "Line %d: multiple result lines\n", t
->line
);
440 t
->expected_err
= BUF_strdup(value
);
441 if (!t
->expected_err
)
444 /* Must be test specific line: try to parse it */
446 rv
= t
->meth
->parse(t
, keyword
, value
);
449 fprintf(stderr
, "line %d: unexpected keyword %s\n",
453 fprintf(stderr
, "line %d: error processing keyword %s\n",
461 static int check_output(struct evp_test
*t
, const unsigned char *expected
,
462 const unsigned char *got
, size_t len
)
464 if (!memcmp(expected
, got
, len
))
466 t
->out_expected
= BUF_memdup(expected
, len
);
467 t
->out_got
= BUF_memdup(got
, len
);
469 if (t
->out_expected
== NULL
|| t
->out_got
== NULL
) {
470 fprintf(stderr
, "Memory allocation error!\n");
476 int main(int argc
, char **argv
)
483 fprintf(stderr
, "usage: evp_test testfile.txt\n");
487 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
489 ERR_load_crypto_strings();
490 OpenSSL_add_all_algorithms();
492 memset(&t
, 0, sizeof(t
));
501 t
.out_expected
= NULL
;
504 in
= fopen(argv
[1], "r");
506 while (fgets(buf
, sizeof(buf
), in
)) {
508 if (!process_test(&t
, buf
, 0))
511 /* Run any final test we have */
512 if (!setup_test(&t
, NULL
))
514 fprintf(stderr
, "%d tests completed with %d errors, %d skipped\n",
515 t
.ntests
, t
.errors
, t
.nskip
);
516 free_key_list(t
.public);
517 free_key_list(t
.private);
520 CRYPTO_cleanup_all_ex_data();
521 ERR_remove_thread_state(NULL
);
523 CRYPTO_mem_leaks_fp(stderr
);
529 static void test_free(void *d
)
534 /* Message digest tests */
537 /* Digest this test is for */
538 const EVP_MD
*digest
;
539 /* Input to digest */
540 unsigned char *input
;
542 /* Repeat count for input */
544 /* Expected output */
545 unsigned char *output
;
549 static int digest_test_init(struct evp_test
*t
, const char *alg
)
551 const EVP_MD
*digest
;
552 struct digest_data
*mdat
= t
->data
;
553 digest
= EVP_get_digestbyname(alg
);
555 /* If alg has an OID assume disabled algorithm */
556 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
562 mdat
= OPENSSL_malloc(sizeof(*mdat
));
563 mdat
->digest
= digest
;
571 static void digest_test_cleanup(struct evp_test
*t
)
573 struct digest_data
*mdat
= t
->data
;
574 test_free(mdat
->input
);
575 test_free(mdat
->output
);
578 static int digest_test_parse(struct evp_test
*t
,
579 const char *keyword
, const char *value
)
581 struct digest_data
*mdata
= t
->data
;
582 if (!strcmp(keyword
, "Input"))
583 return test_bin(value
, &mdata
->input
, &mdata
->input_len
);
584 if (!strcmp(keyword
, "Output"))
585 return test_bin(value
, &mdata
->output
, &mdata
->output_len
);
586 if (!strcmp(keyword
, "Count")) {
587 long nrpt
= atoi(value
);
590 mdata
->nrpt
= (size_t)nrpt
;
596 static int digest_test_run(struct evp_test
*t
)
598 struct digest_data
*mdata
= t
->data
;
600 const char *err
= "INTERNAL_ERROR";
602 unsigned char md
[EVP_MAX_MD_SIZE
];
604 mctx
= EVP_MD_CTX_create();
607 err
= "DIGESTINIT_ERROR";
608 if (!EVP_DigestInit_ex(mctx
, mdata
->digest
, NULL
))
610 err
= "DIGESTUPDATE_ERROR";
611 for (i
= 0; i
< mdata
->nrpt
; i
++) {
612 if (!EVP_DigestUpdate(mctx
, mdata
->input
, mdata
->input_len
))
615 err
= "DIGESTFINAL_ERROR";
616 if (!EVP_DigestFinal(mctx
, md
, &md_len
))
618 err
= "DIGEST_LENGTH_MISMATCH";
619 if (md_len
!= mdata
->output_len
)
621 err
= "DIGEST_MISMATCH";
622 if (check_output(t
, mdata
->output
, md
, md_len
))
627 EVP_MD_CTX_destroy(mctx
);
632 static const struct evp_test_method digest_test_method
= {
642 const EVP_CIPHER
*cipher
;
644 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
650 unsigned char *plaintext
;
651 size_t plaintext_len
;
652 unsigned char *ciphertext
;
653 size_t ciphertext_len
;
661 static int cipher_test_init(struct evp_test
*t
, const char *alg
)
663 const EVP_CIPHER
*cipher
;
664 struct cipher_data
*cdat
= t
->data
;
665 cipher
= EVP_get_cipherbyname(alg
);
667 /* If alg has an OID assume disabled algorithm */
668 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
674 cdat
= OPENSSL_malloc(sizeof(*cdat
));
675 cdat
->cipher
= cipher
;
679 cdat
->ciphertext
= NULL
;
680 cdat
->plaintext
= NULL
;
684 if (EVP_CIPHER_mode(cipher
) == EVP_CIPH_GCM_MODE
685 || EVP_CIPHER_mode(cipher
) == EVP_CIPH_OCB_MODE
686 || EVP_CIPHER_mode(cipher
) == EVP_CIPH_CCM_MODE
)
687 cdat
->aead
= EVP_CIPHER_mode(cipher
);
694 static void cipher_test_cleanup(struct evp_test
*t
)
696 struct cipher_data
*cdat
= t
->data
;
697 test_free(cdat
->key
);
699 test_free(cdat
->ciphertext
);
700 test_free(cdat
->plaintext
);
701 test_free(cdat
->aad
);
702 test_free(cdat
->tag
);
705 static int cipher_test_parse(struct evp_test
*t
, const char *keyword
,
708 struct cipher_data
*cdat
= t
->data
;
709 if (!strcmp(keyword
, "Key"))
710 return test_bin(value
, &cdat
->key
, &cdat
->key_len
);
711 if (!strcmp(keyword
, "IV"))
712 return test_bin(value
, &cdat
->iv
, &cdat
->iv_len
);
713 if (!strcmp(keyword
, "Plaintext"))
714 return test_bin(value
, &cdat
->plaintext
, &cdat
->plaintext_len
);
715 if (!strcmp(keyword
, "Ciphertext"))
716 return test_bin(value
, &cdat
->ciphertext
, &cdat
->ciphertext_len
);
718 if (!strcmp(keyword
, "AAD"))
719 return test_bin(value
, &cdat
->aad
, &cdat
->aad_len
);
720 if (!strcmp(keyword
, "Tag"))
721 return test_bin(value
, &cdat
->tag
, &cdat
->tag_len
);
724 if (!strcmp(keyword
, "Operation")) {
725 if (!strcmp(value
, "ENCRYPT"))
727 else if (!strcmp(value
, "DECRYPT"))
736 static int cipher_test_enc(struct evp_test
*t
, int enc
)
738 struct cipher_data
*cdat
= t
->data
;
739 unsigned char *in
, *out
, *tmp
= NULL
;
740 size_t in_len
, out_len
;
742 EVP_CIPHER_CTX
*ctx
= NULL
;
744 err
= "INTERNAL_ERROR";
745 ctx
= EVP_CIPHER_CTX_new();
748 EVP_CIPHER_CTX_set_flags(ctx
, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
);
750 in
= cdat
->plaintext
;
751 in_len
= cdat
->plaintext_len
;
752 out
= cdat
->ciphertext
;
753 out_len
= cdat
->ciphertext_len
;
755 in
= cdat
->ciphertext
;
756 in_len
= cdat
->ciphertext_len
;
757 out
= cdat
->plaintext
;
758 out_len
= cdat
->plaintext_len
;
760 tmp
= OPENSSL_malloc(in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
);
763 err
= "CIPHERINIT_ERROR";
764 if (!EVP_CipherInit_ex(ctx
, cdat
->cipher
, NULL
, NULL
, NULL
, enc
))
766 err
= "INVALID_IV_LENGTH";
769 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_IVLEN
,
772 } else if (cdat
->iv_len
!= (size_t)EVP_CIPHER_CTX_iv_length(ctx
))
778 * If encrypting or OCB just set tag length initially, otherwise
779 * set tag length and value.
781 if (enc
|| cdat
->aead
== EVP_CIPH_OCB_MODE
) {
782 err
= "TAG_LENGTH_SET_ERROR";
785 err
= "TAG_SET_ERROR";
788 if (tag
|| cdat
->aead
!= EVP_CIPH_GCM_MODE
) {
789 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_TAG
,
795 err
= "INVALID_KEY_LENGTH";
796 if (!EVP_CIPHER_CTX_set_key_length(ctx
, cdat
->key_len
))
798 err
= "KEY_SET_ERROR";
799 if (!EVP_CipherInit_ex(ctx
, NULL
, NULL
, cdat
->key
, cdat
->iv
, -1))
802 if (!enc
&& cdat
->aead
== EVP_CIPH_OCB_MODE
) {
803 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_TAG
,
804 cdat
->tag_len
, cdat
->tag
)) {
805 err
= "TAG_SET_ERROR";
810 if (cdat
->aead
== EVP_CIPH_CCM_MODE
) {
811 if (!EVP_CipherUpdate(ctx
, NULL
, &tmplen
, NULL
, out_len
)) {
812 err
= "CCM_PLAINTEXT_LENGTH_SET_ERROR";
817 if (!EVP_CipherUpdate(ctx
, NULL
, &tmplen
, cdat
->aad
, cdat
->aad_len
)) {
818 err
= "AAD_SET_ERROR";
822 EVP_CIPHER_CTX_set_padding(ctx
, 0);
823 err
= "CIPHERUPDATE_ERROR";
824 if (!EVP_CipherUpdate(ctx
, tmp
, &tmplen
, in
, in_len
))
826 if (cdat
->aead
== EVP_CIPH_CCM_MODE
)
829 err
= "CIPHERFINAL_ERROR";
830 if (!EVP_CipherFinal_ex(ctx
, tmp
+ tmplen
, &tmpflen
))
833 err
= "LENGTH_MISMATCH";
834 if (out_len
!= (size_t)(tmplen
+ tmpflen
))
836 err
= "VALUE_MISMATCH";
837 if (check_output(t
, out
, tmp
, out_len
))
839 if (enc
&& cdat
->aead
) {
840 unsigned char rtag
[16];
841 if (cdat
->tag_len
> sizeof(rtag
)) {
842 err
= "TAG_LENGTH_INTERNAL_ERROR";
845 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_GET_TAG
,
846 cdat
->tag_len
, rtag
)) {
847 err
= "TAG_RETRIEVE_ERROR";
850 if (check_output(t
, cdat
->tag
, rtag
, cdat
->tag_len
)) {
851 err
= "TAG_VALUE_MISMATCH";
858 EVP_CIPHER_CTX_free(ctx
);
863 static int cipher_test_run(struct evp_test
*t
)
865 struct cipher_data
*cdat
= t
->data
;
871 if (!cdat
->iv
&& EVP_CIPHER_iv_length(cdat
->cipher
)) {
872 /* IV is optional and usually omitted in wrap mode */
873 if (EVP_CIPHER_mode(cdat
->cipher
) != EVP_CIPH_WRAP_MODE
) {
878 if (cdat
->aead
&& !cdat
->tag
) {
883 rv
= cipher_test_enc(t
, 1);
884 /* Not fatal errors: return */
891 if (cdat
->enc
!= 1) {
892 rv
= cipher_test_enc(t
, 0);
893 /* Not fatal errors: return */
903 static const struct evp_test_method cipher_test_method
= {
914 /* Algorithm string for this MAC */
920 unsigned char *input
;
922 /* Expected output */
923 unsigned char *output
;
927 static int mac_test_init(struct evp_test
*t
, const char *alg
)
930 struct mac_data
*mdat
;
931 if (!strcmp(alg
, "HMAC"))
932 type
= EVP_PKEY_HMAC
;
933 else if (!strcmp(alg
, "CMAC"))
934 type
= EVP_PKEY_CMAC
;
938 mdat
= OPENSSL_malloc(sizeof(*mdat
));
948 static void mac_test_cleanup(struct evp_test
*t
)
950 struct mac_data
*mdat
= t
->data
;
951 test_free(mdat
->alg
);
952 test_free(mdat
->key
);
953 test_free(mdat
->input
);
954 test_free(mdat
->output
);
957 static int mac_test_parse(struct evp_test
*t
,
958 const char *keyword
, const char *value
)
960 struct mac_data
*mdata
= t
->data
;
961 if (!strcmp(keyword
, "Key"))
962 return test_bin(value
, &mdata
->key
, &mdata
->key_len
);
963 if (!strcmp(keyword
, "Algorithm")) {
964 mdata
->alg
= BUF_strdup(value
);
969 if (!strcmp(keyword
, "Input"))
970 return test_bin(value
, &mdata
->input
, &mdata
->input_len
);
971 if (!strcmp(keyword
, "Output"))
972 return test_bin(value
, &mdata
->output
, &mdata
->output_len
);
976 static int mac_test_run(struct evp_test
*t
)
978 struct mac_data
*mdata
= t
->data
;
979 const char *err
= "INTERNAL_ERROR";
980 EVP_MD_CTX
*mctx
= NULL
;
981 EVP_PKEY_CTX
*pctx
= NULL
, *genctx
= NULL
;
982 EVP_PKEY
*key
= NULL
;
983 const EVP_MD
*md
= NULL
;
984 unsigned char *mac
= NULL
;
987 err
= "MAC_PKEY_CTX_ERROR";
988 genctx
= EVP_PKEY_CTX_new_id(mdata
->type
, NULL
);
992 err
= "MAC_KEYGEN_INIT_ERROR";
993 if (EVP_PKEY_keygen_init(genctx
) <= 0)
995 if (mdata
->type
== EVP_PKEY_CMAC
) {
996 err
= "MAC_ALGORITHM_SET_ERROR";
997 if (EVP_PKEY_CTX_ctrl_str(genctx
, "cipher", mdata
->alg
) <= 0)
1001 err
= "MAC_KEY_SET_ERROR";
1002 if (EVP_PKEY_CTX_set_mac_key(genctx
, mdata
->key
, mdata
->key_len
) <= 0)
1005 err
= "MAC_KEY_GENERATE_ERROR";
1006 if (EVP_PKEY_keygen(genctx
, &key
) <= 0)
1008 if (mdata
->type
== EVP_PKEY_HMAC
) {
1009 err
= "MAC_ALGORITHM_SET_ERROR";
1010 md
= EVP_get_digestbyname(mdata
->alg
);
1014 mctx
= EVP_MD_CTX_create();
1017 err
= "DIGESTSIGNINIT_ERROR";
1018 if (!EVP_DigestSignInit(mctx
, &pctx
, md
, NULL
, key
))
1021 err
= "DIGESTSIGNUPDATE_ERROR";
1022 if (!EVP_DigestSignUpdate(mctx
, mdata
->input
, mdata
->input_len
))
1024 err
= "DIGESTSIGNFINAL_LENGTH_ERROR";
1025 if (!EVP_DigestSignFinal(mctx
, NULL
, &mac_len
))
1027 mac
= OPENSSL_malloc(mac_len
);
1029 fprintf(stderr
, "Error allocating mac buffer!\n");
1032 if (!EVP_DigestSignFinal(mctx
, mac
, &mac_len
))
1034 err
= "MAC_LENGTH_MISMATCH";
1035 if (mac_len
!= mdata
->output_len
)
1037 err
= "MAC_MISMATCH";
1038 if (check_output(t
, mdata
->output
, mac
, mac_len
))
1043 EVP_MD_CTX_destroy(mctx
);
1045 EVP_PKEY_CTX_free(genctx
);
1051 static const struct evp_test_method mac_test_method
= {
1060 * Public key operations. These are all very similar and can share
1061 * a lot of common code.
1065 /* Context for this operation */
1067 /* Key operation to perform */
1068 int (*keyop
) (EVP_PKEY_CTX
*ctx
,
1069 unsigned char *sig
, size_t *siglen
,
1070 const unsigned char *tbs
, size_t tbslen
);
1072 unsigned char *input
;
1074 /* Expected output */
1075 unsigned char *output
;
1080 * Perform public key operation setup: lookup key, allocated ctx and call
1081 * the appropriate initialisation function
1083 static int pkey_test_init(struct evp_test
*t
, const char *name
,
1085 int (*keyopinit
) (EVP_PKEY_CTX
*ctx
),
1086 int (*keyop
) (EVP_PKEY_CTX
*ctx
,
1087 unsigned char *sig
, size_t *siglen
,
1088 const unsigned char *tbs
,
1092 struct pkey_data
*kdata
;
1093 EVP_PKEY
*pkey
= NULL
;
1096 rv
= find_key(&pkey
, name
, t
->public);
1098 rv
= find_key(&pkey
, name
, t
->private);
1106 kdata
= OPENSSL_malloc(sizeof(*kdata
));
1108 EVP_PKEY_free(pkey
);
1112 kdata
->input
= NULL
;
1113 kdata
->output
= NULL
;
1114 kdata
->keyop
= keyop
;
1116 kdata
->ctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
1119 if (keyopinit(kdata
->ctx
) <= 0)
1124 static void pkey_test_cleanup(struct evp_test
*t
)
1126 struct pkey_data
*kdata
= t
->data
;
1128 OPENSSL_free(kdata
->input
);
1129 OPENSSL_free(kdata
->output
);
1130 EVP_PKEY_CTX_free(kdata
->ctx
);
1133 static int pkey_test_parse(struct evp_test
*t
,
1134 const char *keyword
, const char *value
)
1136 struct pkey_data
*kdata
= t
->data
;
1137 if (!strcmp(keyword
, "Input"))
1138 return test_bin(value
, &kdata
->input
, &kdata
->input_len
);
1139 if (!strcmp(keyword
, "Output"))
1140 return test_bin(value
, &kdata
->output
, &kdata
->output_len
);
1141 if (!strcmp(keyword
, "Ctrl")) {
1142 char *p
= strchr(value
, ':');
1145 if (EVP_PKEY_CTX_ctrl_str(kdata
->ctx
, value
, p
) <= 0)
1152 static int pkey_test_run(struct evp_test
*t
)
1154 struct pkey_data
*kdata
= t
->data
;
1155 unsigned char *out
= NULL
;
1157 const char *err
= "KEYOP_LENGTH_ERROR";
1158 if (kdata
->keyop(kdata
->ctx
, NULL
, &out_len
, kdata
->input
,
1159 kdata
->input_len
) <= 0)
1161 out
= OPENSSL_malloc(out_len
);
1163 fprintf(stderr
, "Error allocating output buffer!\n");
1166 err
= "KEYOP_ERROR";
1168 (kdata
->ctx
, out
, &out_len
, kdata
->input
, kdata
->input_len
) <= 0)
1170 err
= "KEYOP_LENGTH_MISMATCH";
1171 if (out_len
!= kdata
->output_len
)
1173 err
= "KEYOP_MISMATCH";
1174 if (check_output(t
, kdata
->output
, out
, out_len
))
1183 static int sign_test_init(struct evp_test
*t
, const char *name
)
1185 return pkey_test_init(t
, name
, 0, EVP_PKEY_sign_init
, EVP_PKEY_sign
);
1188 static const struct evp_test_method psign_test_method
= {
1196 static int verify_recover_test_init(struct evp_test
*t
, const char *name
)
1198 return pkey_test_init(t
, name
, 1, EVP_PKEY_verify_recover_init
,
1199 EVP_PKEY_verify_recover
);
1202 static const struct evp_test_method pverify_recover_test_method
= {
1204 verify_recover_test_init
,
1210 static int decrypt_test_init(struct evp_test
*t
, const char *name
)
1212 return pkey_test_init(t
, name
, 0, EVP_PKEY_decrypt_init
,
1216 static const struct evp_test_method pdecrypt_test_method
= {
1224 static int verify_test_init(struct evp_test
*t
, const char *name
)
1226 return pkey_test_init(t
, name
, 1, EVP_PKEY_verify_init
, 0);
1229 static int verify_test_run(struct evp_test
*t
)
1231 struct pkey_data
*kdata
= t
->data
;
1232 if (EVP_PKEY_verify(kdata
->ctx
, kdata
->output
, kdata
->output_len
,
1233 kdata
->input
, kdata
->input_len
) <= 0)
1234 t
->err
= "VERIFY_ERROR";
1238 static const struct evp_test_method pverify_test_method
= {