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 /* If output mismatch expected and got value */
183 unsigned char *out_got;
184 unsigned char *out_expected;
186 /* test specific data */
193 struct key_list *next;
196 /* Test method structure */
197 struct evp_test_method {
198 /* Name of test as it appears in file */
200 /* Initialise test for "alg" */
201 int (*init) (struct evp_test * t, const char *alg);
202 /* Clean up method */
203 void (*cleanup) (struct evp_test * t);
204 /* Test specific name value pair processing */
205 int (*parse) (struct evp_test * t, const char *name, const char *value);
206 /* Run the test itself */
207 int (*run_test) (struct evp_test * t);
210 static const struct evp_test_method digest_test_method, cipher_test_method;
211 static const struct evp_test_method mac_test_method;
212 static const struct evp_test_method psign_test_method, pverify_test_method;
213 static const struct evp_test_method pdecrypt_test_method;
214 static const struct evp_test_method pverify_recover_test_method;
216 static const struct evp_test_method *evp_test_list[] = {
221 &pverify_test_method,
222 &pdecrypt_test_method,
223 &pverify_recover_test_method,
227 static const struct evp_test_method *evp_find_test(const char *name)
229 const struct evp_test_method **tt;
230 for (tt = evp_test_list; *tt; tt++) {
231 if (!strcmp(name, (*tt)->name))
237 static void hex_print(const char *name, const unsigned char *buf, size_t len)
240 fprintf(stderr, "%s ", name);
241 for (i = 0; i < len; i++)
242 fprintf(stderr, "%02X", buf[i]);
246 static void print_expected(struct evp_test *t)
248 if (t->out_expected == NULL)
250 hex_print("Expected:", t->out_expected, t->out_len);
251 hex_print("Got: ", t->out_got, t->out_len);
252 OPENSSL_free(t->out_expected);
253 OPENSSL_free(t->out_got);
254 t->out_expected = NULL;
258 static int check_test_error(struct evp_test *t)
260 if (!t->err && !t->expected_err)
262 if (t->err && !t->expected_err) {
263 fprintf(stderr, "Test line %d: unexpected error %s\n",
264 t->start_line, t->err);
268 if (!t->err && t->expected_err) {
269 fprintf(stderr, "Test line %d: succeeded expecting %s\n",
270 t->start_line, t->expected_err);
273 if (!strcmp(t->err, t->expected_err))
276 fprintf(stderr, "Test line %d: expecting %s got %s\n",
277 t->start_line, t->expected_err, t->err);
281 /* Setup a new test, run any existing test */
283 static int setup_test(struct evp_test *t, const struct evp_test_method *tmeth)
285 /* If we already have a test set up run it */
289 if (t->meth->run_test(t) != 1) {
290 fprintf(stderr, "%s test error line %d\n",
291 t->meth->name, t->start_line);
294 if (!check_test_error(t)) {
296 ERR_print_errors_fp(stderr);
301 OPENSSL_free(t->data);
303 if (t->expected_err) {
304 OPENSSL_free(t->expected_err);
305 t->expected_err = NULL;
312 static EVP_PKEY *find_key(const char *name, struct key_list *lst)
314 for (; lst; lst = lst->next) {
315 if (!strcmp(lst->name, name))
321 static void free_key_list(struct key_list *lst)
323 while (lst != NULL) {
324 struct key_list *ltmp;
325 EVP_PKEY_free(lst->key);
326 OPENSSL_free(lst->name);
333 static int process_test(struct evp_test *t, char *buf, int verbose)
335 char *keyword, *value;
338 struct key_list **lst, *key;
340 const struct evp_test_method *tmeth;
343 if (!parse_line(&keyword, &value, buf))
345 if (!strcmp(keyword, "PrivateKey")) {
346 save_pos = ftell(t->in);
347 pk = PEM_read_PrivateKey(t->in, NULL, 0, NULL);
349 fprintf(stderr, "Error reading private key %s\n", value);
350 ERR_print_errors_fp(stderr);
355 if (!strcmp(keyword, "PublicKey")) {
356 save_pos = ftell(t->in);
357 pk = PEM_read_PUBKEY(t->in, NULL, 0, NULL);
359 fprintf(stderr, "Error reading public key %s\n", value);
360 ERR_print_errors_fp(stderr);
365 /* If we have a key add to list */
368 if (find_key(value, *lst)) {
369 fprintf(stderr, "Duplicate key %s\n", value);
372 key = OPENSSL_malloc(sizeof(struct key_list));
375 key->name = BUF_strdup(value);
379 /* Rewind input, read to end and update line numbers */
380 fseek(t->in, save_pos, SEEK_SET);
381 while (fgets(tmpbuf, sizeof(tmpbuf), t->in)) {
383 if (!strncmp(tmpbuf, "-----END", 8))
386 fprintf(stderr, "Can't find key end\n");
390 /* See if keyword corresponds to a test start */
391 tmeth = evp_find_test(keyword);
393 if (!setup_test(t, tmeth))
395 t->start_line = t->line;
396 if (!tmeth->init(t, value)) {
397 fprintf(stderr, "Unknown %s: %s\n", keyword, value);
401 } else if (!strcmp(keyword, "Result")) {
402 if (t->expected_err) {
403 fprintf(stderr, "Line %d: multiple result lines\n", t->line);
406 t->expected_err = BUF_strdup(value);
407 if (!t->expected_err)
410 /* Must be test specific line: try to parse it */
412 rv = t->meth->parse(t, keyword, value);
415 fprintf(stderr, "line %d: unexpected keyword %s\n",
419 fprintf(stderr, "line %d: error processing keyword %s\n",
427 static int check_output(struct evp_test *t, const unsigned char *expected,
428 const unsigned char *got, size_t len)
430 if (!memcmp(expected, got, len))
432 t->out_expected = BUF_memdup(expected, len);
433 t->out_got = BUF_memdup(got, len);
435 if (t->out_expected == NULL || t->out_got == NULL) {
436 fprintf(stderr, "Memory allocation error!\n");
442 int main(int argc, char **argv)
449 fprintf(stderr, "usage: evp_test testfile.txt\n");
453 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
455 ERR_load_crypto_strings();
456 OpenSSL_add_all_algorithms();
465 t.out_expected = NULL;
468 in = fopen(argv[1], "r");
470 while (fgets(buf, sizeof(buf), in)) {
472 if (!process_test(&t, buf, 0))
475 /* Run any final test we have */
476 if (!setup_test(&t, NULL))
478 fprintf(stderr, "%d tests completed with %d errors\n",
480 free_key_list(t.public);
481 free_key_list(t.private);
484 CRYPTO_cleanup_all_ex_data();
485 ERR_remove_thread_state(NULL);
487 CRYPTO_mem_leaks_fp(stderr);
493 static void test_free(void *d)
499 /* Message digest tests */
502 /* Digest this test is for */
503 const EVP_MD *digest;
504 /* Input to digest */
505 unsigned char *input;
507 /* Expected output */
508 unsigned char *output;
512 static int digest_test_init(struct evp_test *t, const char *alg)
514 const EVP_MD *digest;
515 struct digest_data *mdat = t->data;
516 digest = EVP_get_digestbyname(alg);
519 mdat = OPENSSL_malloc(sizeof(struct digest_data));
520 mdat->digest = digest;
527 static void digest_test_cleanup(struct evp_test *t)
529 struct digest_data *mdat = t->data;
530 test_free(mdat->input);
531 test_free(mdat->output);
534 static int digest_test_parse(struct evp_test *t,
535 const char *keyword, const char *value)
537 struct digest_data *mdata = t->data;
538 if (!strcmp(keyword, "Input"))
539 return test_bin(value, &mdata->input, &mdata->input_len);
540 if (!strcmp(keyword, "Output"))
541 return test_bin(value, &mdata->output, &mdata->output_len);
545 static int digest_test_run(struct evp_test *t)
547 struct digest_data *mdata = t->data;
548 const char *err = "INTERNAL_ERROR";
550 unsigned char md[EVP_MAX_MD_SIZE];
552 mctx = EVP_MD_CTX_create();
555 err = "DIGESTINIT_ERROR";
556 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))
558 err = "DIGESTUPDATE_ERROR";
559 if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))
561 err = "DIGESTFINAL_ERROR";
562 if (!EVP_DigestFinal(mctx, md, &md_len))
564 err = "DIGEST_LENGTH_MISMATCH";
565 if (md_len != mdata->output_len)
567 err = "DIGEST_MISMATCH";
568 if (check_output(t, mdata->output, md, md_len))
573 EVP_MD_CTX_destroy(mctx);
578 static const struct evp_test_method digest_test_method = {
588 const EVP_CIPHER *cipher;
590 /* Set to EVP_CIPH_GCM_MODE or EVP_CIPH_CCM_MODE if AEAD */
596 unsigned char *plaintext;
597 size_t plaintext_len;
598 unsigned char *ciphertext;
599 size_t ciphertext_len;
607 static int cipher_test_init(struct evp_test *t, const char *alg)
609 const EVP_CIPHER *cipher;
610 struct cipher_data *cdat = t->data;
611 cipher = EVP_get_cipherbyname(alg);
614 cdat = OPENSSL_malloc(sizeof(struct cipher_data));
615 cdat->cipher = cipher;
619 cdat->ciphertext = NULL;
620 cdat->plaintext = NULL;
624 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
625 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
626 cdat->aead = EVP_CIPHER_mode(cipher);
633 static void cipher_test_cleanup(struct evp_test *t)
635 struct cipher_data *cdat = t->data;
636 test_free(cdat->key);
638 test_free(cdat->ciphertext);
639 test_free(cdat->plaintext);
640 test_free(cdat->aad);
641 test_free(cdat->tag);
644 static int cipher_test_parse(struct evp_test *t, const char *keyword,
647 struct cipher_data *cdat = t->data;
648 if (!strcmp(keyword, "Key"))
649 return test_bin(value, &cdat->key, &cdat->key_len);
650 if (!strcmp(keyword, "IV"))
651 return test_bin(value, &cdat->iv, &cdat->iv_len);
652 if (!strcmp(keyword, "Plaintext"))
653 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
654 if (!strcmp(keyword, "Ciphertext"))
655 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
657 if (!strcmp(keyword, "AAD"))
658 return test_bin(value, &cdat->aad, &cdat->aad_len);
659 if (!strcmp(keyword, "Tag"))
660 return test_bin(value, &cdat->tag, &cdat->tag_len);
663 if (!strcmp(keyword, "Operation")) {
664 if (!strcmp(value, "ENCRYPT"))
666 else if (!strcmp(value, "DECRYPT"))
675 static int cipher_test_enc(struct evp_test *t, int enc)
677 struct cipher_data *cdat = t->data;
678 unsigned char *in, *out, *tmp = NULL;
679 size_t in_len, out_len;
681 EVP_CIPHER_CTX *ctx = NULL;
683 err = "INTERNAL_ERROR";
684 ctx = EVP_CIPHER_CTX_new();
687 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
689 in = cdat->plaintext;
690 in_len = cdat->plaintext_len;
691 out = cdat->ciphertext;
692 out_len = cdat->ciphertext_len;
694 in = cdat->ciphertext;
695 in_len = cdat->ciphertext_len;
696 out = cdat->plaintext;
697 out_len = cdat->plaintext_len;
699 tmp = OPENSSL_malloc(in_len + 2 * EVP_MAX_BLOCK_LENGTH);
702 err = "CIPHERINIT_ERROR";
703 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
705 err = "INVALID_IV_LENGTH";
707 if (cdat->aead == EVP_CIPH_GCM_MODE) {
708 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN,
711 } else if (cdat->aead == EVP_CIPH_CCM_MODE) {
712 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN,
715 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
721 * If encrypting just set tag length. If decrypting set
722 * tag length and value.
725 err = "TAG_LENGTH_SET_ERROR";
728 err = "TAG_SET_ERROR";
731 if (cdat->aead == EVP_CIPH_GCM_MODE && tag) {
732 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG,
735 } else if (cdat->aead == EVP_CIPH_CCM_MODE) {
736 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG,
742 err = "INVALID_KEY_LENGTH";
743 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len))
745 err = "KEY_SET_ERROR";
746 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1))
749 if (cdat->aead == EVP_CIPH_CCM_MODE) {
750 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
751 err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
756 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, cdat->aad, cdat->aad_len)) {
757 err = "AAD_SET_ERROR";
761 EVP_CIPHER_CTX_set_padding(ctx, 0);
762 err = "CIPHERUPDATE_ERROR";
763 if (!EVP_CipherUpdate(ctx, tmp, &tmplen, in, in_len))
765 if (cdat->aead == EVP_CIPH_CCM_MODE)
768 err = "CIPHERFINAL_ERROR";
769 if (!EVP_CipherFinal_ex(ctx, tmp + tmplen, &tmpflen))
772 err = "LENGTH_MISMATCH";
773 if (out_len != (size_t)(tmplen + tmpflen))
775 err = "VALUE_MISMATCH";
776 if (check_output(t, out, tmp, out_len))
778 if (enc && cdat->aead) {
779 unsigned char rtag[16];
780 if (cdat->tag_len > sizeof(rtag)) {
781 err = "TAG_LENGTH_INTERNAL_ERROR";
784 /* EVP_CTRL_CCM_GET_TAG and EVP_CTRL_GCM_GET_TAG are equal. */
785 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG,
786 cdat->tag_len, rtag)) {
787 err = "TAG_RETRIEVE_ERROR";
790 if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {
791 err = "TAG_VALUE_MISMATCH";
799 EVP_CIPHER_CTX_free(ctx);
804 static int cipher_test_run(struct evp_test *t)
806 struct cipher_data *cdat = t->data;
812 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
813 /* IV is optional and usually omitted in wrap mode */
814 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
819 if (cdat->aead && !cdat->tag) {
824 rv = cipher_test_enc(t, 1);
825 /* Not fatal errors: return */
832 if (cdat->enc != 1) {
833 rv = cipher_test_enc(t, 0);
834 /* Not fatal errors: return */
844 static const struct evp_test_method cipher_test_method = {
855 /* Algorithm string for this MAC */
861 unsigned char *input;
863 /* Expected output */
864 unsigned char *output;
868 static int mac_test_init(struct evp_test *t, const char *alg)
871 struct mac_data *mdat;
872 if (!strcmp(alg, "HMAC"))
873 type = EVP_PKEY_HMAC;
874 else if (!strcmp(alg, "CMAC"))
875 type = EVP_PKEY_CMAC;
879 mdat = OPENSSL_malloc(sizeof(struct mac_data));
889 static void mac_test_cleanup(struct evp_test *t)
891 struct mac_data *mdat = t->data;
892 test_free(mdat->alg);
893 test_free(mdat->key);
894 test_free(mdat->input);
895 test_free(mdat->output);
898 static int mac_test_parse(struct evp_test *t,
899 const char *keyword, const char *value)
901 struct mac_data *mdata = t->data;
902 if (!strcmp(keyword, "Key"))
903 return test_bin(value, &mdata->key, &mdata->key_len);
904 if (!strcmp(keyword, "Algorithm")) {
905 mdata->alg = BUF_strdup(value);
910 if (!strcmp(keyword, "Input"))
911 return test_bin(value, &mdata->input, &mdata->input_len);
912 if (!strcmp(keyword, "Output"))
913 return test_bin(value, &mdata->output, &mdata->output_len);
917 static int mac_test_run(struct evp_test *t)
919 struct mac_data *mdata = t->data;
920 const char *err = "INTERNAL_ERROR";
921 EVP_MD_CTX *mctx = NULL;
922 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
923 EVP_PKEY *key = NULL;
924 const EVP_MD *md = NULL;
925 unsigned char *mac = NULL;
928 err = "MAC_PKEY_CTX_ERROR";
929 genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL);
933 err = "MAC_KEYGEN_INIT_ERROR";
934 if (EVP_PKEY_keygen_init(genctx) <= 0)
936 if (mdata->type == EVP_PKEY_CMAC) {
937 err = "MAC_ALGORITHM_SET_ERROR";
938 if (EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0)
942 err = "MAC_KEY_SET_ERROR";
943 if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0)
946 err = "MAC_KEY_GENERATE_ERROR";
947 if (EVP_PKEY_keygen(genctx, &key) <= 0)
949 if (mdata->type == EVP_PKEY_HMAC) {
950 err = "MAC_ALGORITHM_SET_ERROR";
951 md = EVP_get_digestbyname(mdata->alg);
955 mctx = EVP_MD_CTX_create();
958 err = "DIGESTSIGNINIT_ERROR";
959 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key))
962 err = "DIGESTSIGNUPDATE_ERROR";
963 if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len))
965 err = "DIGESTSIGNFINAL_LENGTH_ERROR";
966 if (!EVP_DigestSignFinal(mctx, NULL, &mac_len))
968 mac = OPENSSL_malloc(mac_len);
970 fprintf(stderr, "Error allocating mac buffer!\n");
973 if (!EVP_DigestSignFinal(mctx, mac, &mac_len))
975 err = "MAC_LENGTH_MISMATCH";
976 if (mac_len != mdata->output_len)
978 err = "MAC_MISMATCH";
979 if (check_output(t, mdata->output, mac, mac_len))
984 EVP_MD_CTX_destroy(mctx);
988 EVP_PKEY_CTX_free(genctx);
995 static const struct evp_test_method mac_test_method = {
1004 * Public key operations. These are all very similar and can share
1005 * a lot of common code.
1009 /* Context for this operation */
1011 /* Key operation to perform */
1012 int (*keyop) (EVP_PKEY_CTX *ctx,
1013 unsigned char *sig, size_t *siglen,
1014 const unsigned char *tbs, size_t tbslen);
1016 unsigned char *input;
1018 /* Expected output */
1019 unsigned char *output;
1024 * Perform public key operation setup: lookup key, allocated ctx and call
1025 * the appropriate initialisation function
1027 static int pkey_test_init(struct evp_test *t, const char *name,
1029 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1030 int (*keyop) (EVP_PKEY_CTX *ctx,
1031 unsigned char *sig, size_t *siglen,
1032 const unsigned char *tbs,
1036 struct pkey_data *kdata;
1037 EVP_PKEY *pkey = NULL;
1038 kdata = OPENSSL_malloc(sizeof(struct pkey_data));
1042 kdata->input = NULL;
1043 kdata->output = NULL;
1044 kdata->keyop = keyop;
1047 pkey = find_key(name, t->public);
1049 pkey = find_key(name, t->private);
1052 kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL);
1055 if (keyopinit(kdata->ctx) <= 0)
1060 static void pkey_test_cleanup(struct evp_test *t)
1062 struct pkey_data *kdata = t->data;
1064 OPENSSL_free(kdata->input);
1066 OPENSSL_free(kdata->output);
1068 EVP_PKEY_CTX_free(kdata->ctx);
1071 static int pkey_test_parse(struct evp_test *t,
1072 const char *keyword, const char *value)
1074 struct pkey_data *kdata = t->data;
1075 if (!strcmp(keyword, "Input"))
1076 return test_bin(value, &kdata->input, &kdata->input_len);
1077 if (!strcmp(keyword, "Output"))
1078 return test_bin(value, &kdata->output, &kdata->output_len);
1079 if (!strcmp(keyword, "Ctrl")) {
1080 char *p = strchr(value, ':');
1083 if (EVP_PKEY_CTX_ctrl_str(kdata->ctx, value, p) <= 0)
1090 static int pkey_test_run(struct evp_test *t)
1092 struct pkey_data *kdata = t->data;
1093 unsigned char *out = NULL;
1095 const char *err = "KEYOP_LENGTH_ERROR";
1096 if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1097 kdata->input_len) <= 0)
1099 out = OPENSSL_malloc(out_len);
1101 fprintf(stderr, "Error allocating output buffer!\n");
1104 err = "KEYOP_ERROR";
1106 (kdata->ctx, out, &out_len, kdata->input, kdata->input_len) <= 0)
1108 err = "KEYOP_LENGTH_MISMATCH";
1109 if (out_len != kdata->output_len)
1111 err = "KEYOP_MISMATCH";
1112 if (check_output(t, kdata->output, out, out_len))
1122 static int sign_test_init(struct evp_test *t, const char *name)
1124 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1127 static const struct evp_test_method psign_test_method = {
1135 static int verify_recover_test_init(struct evp_test *t, const char *name)
1137 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1138 EVP_PKEY_verify_recover);
1141 static const struct evp_test_method pverify_recover_test_method = {
1143 verify_recover_test_init,
1149 static int decrypt_test_init(struct evp_test *t, const char *name)
1151 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1155 static const struct evp_test_method pdecrypt_test_method = {
1163 static int verify_test_init(struct evp_test *t, const char *name)
1165 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1168 static int verify_test_run(struct evp_test *t)
1170 struct pkey_data *kdata = t->data;
1171 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1172 kdata->input, kdata->input_len) <= 0)
1173 t->err = "VERIFY_ERROR";
1177 static const struct evp_test_method pverify_test_method = {