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 print_expected(struct evp_test *t)
252 if (t->out_expected == NULL)
254 hex_print("Expected:", t->out_expected, t->out_len);
255 hex_print("Got: ", t->out_got, t->out_len);
256 OPENSSL_free(t->out_expected);
257 OPENSSL_free(t->out_got);
258 t->out_expected = NULL;
262 static int check_test_error(struct evp_test *t)
264 if (!t->err && !t->expected_err)
266 if (t->err && !t->expected_err) {
267 fprintf(stderr, "Test line %d: unexpected error %s\n",
268 t->start_line, t->err);
272 if (!t->err && t->expected_err) {
273 fprintf(stderr, "Test line %d: succeeded expecting %s\n",
274 t->start_line, t->expected_err);
277 if (!strcmp(t->err, t->expected_err))
280 fprintf(stderr, "Test line %d: expecting %s got %s\n",
281 t->start_line, t->expected_err, t->err);
285 /* Setup a new test, run any existing test */
287 static int setup_test(struct evp_test *t, const struct evp_test_method *tmeth)
289 /* If we already have a test set up run it */
297 if (t->meth->run_test(t) != 1) {
298 fprintf(stderr, "%s test error line %d\n",
299 t->meth->name, t->start_line);
302 if (!check_test_error(t)) {
304 ERR_print_errors_fp(stderr);
309 OPENSSL_free(t->data);
311 if (t->expected_err) {
312 OPENSSL_free(t->expected_err);
313 t->expected_err = NULL;
320 static int find_key(EVP_PKEY **ppk, const char *name, struct key_list *lst)
322 for (; lst; lst = lst->next) {
323 if (!strcmp(lst->name, name)) {
332 static void free_key_list(struct key_list *lst)
334 while (lst != NULL) {
335 struct key_list *ltmp;
336 EVP_PKEY_free(lst->key);
337 OPENSSL_free(lst->name);
344 static int check_unsupported()
346 long err = ERR_peek_error();
347 if (ERR_GET_LIB(err) == ERR_LIB_EVP
348 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
355 static int process_test(struct evp_test *t, char *buf, int verbose)
357 char *keyword, *value;
358 int rv = 0, add_key = 0;
360 struct key_list **lst, *key;
362 const struct evp_test_method *tmeth;
365 if (!parse_line(&keyword, &value, buf))
367 if (!strcmp(keyword, "PrivateKey")) {
368 save_pos = ftell(t->in);
369 pk = PEM_read_PrivateKey(t->in, NULL, 0, NULL);
370 if (pk == NULL && !check_unsupported()) {
371 fprintf(stderr, "Error reading private key %s\n", value);
372 ERR_print_errors_fp(stderr);
378 if (!strcmp(keyword, "PublicKey")) {
379 save_pos = ftell(t->in);
380 pk = PEM_read_PUBKEY(t->in, NULL, 0, NULL);
381 if (pk == NULL && !check_unsupported()) {
382 fprintf(stderr, "Error reading public key %s\n", value);
383 ERR_print_errors_fp(stderr);
389 /* If we have a key add to list */
392 if (find_key(NULL, value, *lst)) {
393 fprintf(stderr, "Duplicate key %s\n", value);
396 key = OPENSSL_malloc(sizeof(struct key_list));
399 key->name = BUF_strdup(value);
403 /* Rewind input, read to end and update line numbers */
404 fseek(t->in, save_pos, SEEK_SET);
405 while (fgets(tmpbuf, sizeof(tmpbuf), t->in)) {
407 if (!strncmp(tmpbuf, "-----END", 8))
410 fprintf(stderr, "Can't find key end\n");
414 /* See if keyword corresponds to a test start */
415 tmeth = evp_find_test(keyword);
417 if (!setup_test(t, tmeth))
419 t->start_line = t->line;
421 if (!tmeth->init(t, value)) {
422 fprintf(stderr, "Unknown %s: %s\n", keyword, value);
426 } else if (t->skip) {
428 } else if (!strcmp(keyword, "Result")) {
429 if (t->expected_err) {
430 fprintf(stderr, "Line %d: multiple result lines\n", t->line);
433 t->expected_err = BUF_strdup(value);
434 if (!t->expected_err)
437 /* Must be test specific line: try to parse it */
439 rv = t->meth->parse(t, keyword, value);
442 fprintf(stderr, "line %d: unexpected keyword %s\n",
446 fprintf(stderr, "line %d: error processing keyword %s\n",
454 static int check_output(struct evp_test *t, const unsigned char *expected,
455 const unsigned char *got, size_t len)
457 if (!memcmp(expected, got, len))
459 t->out_expected = BUF_memdup(expected, len);
460 t->out_got = BUF_memdup(got, len);
462 if (t->out_expected == NULL || t->out_got == NULL) {
463 fprintf(stderr, "Memory allocation error!\n");
469 int main(int argc, char **argv)
476 fprintf(stderr, "usage: evp_test testfile.txt\n");
480 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
482 ERR_load_crypto_strings();
483 OpenSSL_add_all_algorithms();
485 memset(&t,0,sizeof(t));
494 t.out_expected = NULL;
497 in = fopen(argv[1], "r");
499 while (fgets(buf, sizeof(buf), in)) {
501 if (!process_test(&t, buf, 0))
504 /* Run any final test we have */
505 if (!setup_test(&t, NULL))
507 fprintf(stderr, "%d tests completed with %d errors, %d skipped\n",
508 t.ntests, t.errors, t.nskip);
509 free_key_list(t.public);
510 free_key_list(t.private);
513 CRYPTO_cleanup_all_ex_data();
514 ERR_remove_thread_state(NULL);
516 CRYPTO_mem_leaks_fp(stderr);
522 static void test_free(void *d)
528 /* Message digest tests */
531 /* Digest this test is for */
532 const EVP_MD *digest;
533 /* Input to digest */
534 unsigned char *input;
536 /* Expected output */
537 unsigned char *output;
541 static int digest_test_init(struct evp_test *t, const char *alg)
543 const EVP_MD *digest;
544 struct digest_data *mdat = t->data;
545 digest = EVP_get_digestbyname(alg);
548 mdat = OPENSSL_malloc(sizeof(struct digest_data));
549 mdat->digest = digest;
556 static void digest_test_cleanup(struct evp_test *t)
558 struct digest_data *mdat = t->data;
559 test_free(mdat->input);
560 test_free(mdat->output);
563 static int digest_test_parse(struct evp_test *t,
564 const char *keyword, const char *value)
566 struct digest_data *mdata = t->data;
567 if (!strcmp(keyword, "Input"))
568 return test_bin(value, &mdata->input, &mdata->input_len);
569 if (!strcmp(keyword, "Output"))
570 return test_bin(value, &mdata->output, &mdata->output_len);
574 static int digest_test_run(struct evp_test *t)
576 struct digest_data *mdata = t->data;
577 const char *err = "INTERNAL_ERROR";
579 unsigned char md[EVP_MAX_MD_SIZE];
581 mctx = EVP_MD_CTX_create();
584 err = "DIGESTINIT_ERROR";
585 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))
587 err = "DIGESTUPDATE_ERROR";
588 if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))
590 err = "DIGESTFINAL_ERROR";
591 if (!EVP_DigestFinal(mctx, md, &md_len))
593 err = "DIGEST_LENGTH_MISMATCH";
594 if (md_len != mdata->output_len)
596 err = "DIGEST_MISMATCH";
597 if (check_output(t, mdata->output, md, md_len))
602 EVP_MD_CTX_destroy(mctx);
607 static const struct evp_test_method digest_test_method = {
617 const EVP_CIPHER *cipher;
619 /* Set to EVP_CIPH_GCM_MODE or EVP_CIPH_CCM_MODE if AEAD */
625 unsigned char *plaintext;
626 size_t plaintext_len;
627 unsigned char *ciphertext;
628 size_t ciphertext_len;
636 static int cipher_test_init(struct evp_test *t, const char *alg)
638 const EVP_CIPHER *cipher;
639 struct cipher_data *cdat = t->data;
640 cipher = EVP_get_cipherbyname(alg);
643 cdat = OPENSSL_malloc(sizeof(struct cipher_data));
644 cdat->cipher = cipher;
648 cdat->ciphertext = NULL;
649 cdat->plaintext = NULL;
653 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
654 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
655 cdat->aead = EVP_CIPHER_mode(cipher);
662 static void cipher_test_cleanup(struct evp_test *t)
664 struct cipher_data *cdat = t->data;
665 test_free(cdat->key);
667 test_free(cdat->ciphertext);
668 test_free(cdat->plaintext);
669 test_free(cdat->aad);
670 test_free(cdat->tag);
673 static int cipher_test_parse(struct evp_test *t, const char *keyword,
676 struct cipher_data *cdat = t->data;
677 if (!strcmp(keyword, "Key"))
678 return test_bin(value, &cdat->key, &cdat->key_len);
679 if (!strcmp(keyword, "IV"))
680 return test_bin(value, &cdat->iv, &cdat->iv_len);
681 if (!strcmp(keyword, "Plaintext"))
682 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
683 if (!strcmp(keyword, "Ciphertext"))
684 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
686 if (!strcmp(keyword, "AAD"))
687 return test_bin(value, &cdat->aad, &cdat->aad_len);
688 if (!strcmp(keyword, "Tag"))
689 return test_bin(value, &cdat->tag, &cdat->tag_len);
692 if (!strcmp(keyword, "Operation")) {
693 if (!strcmp(value, "ENCRYPT"))
695 else if (!strcmp(value, "DECRYPT"))
704 static int cipher_test_enc(struct evp_test *t, int enc)
706 struct cipher_data *cdat = t->data;
707 unsigned char *in, *out, *tmp = NULL;
708 size_t in_len, out_len;
710 EVP_CIPHER_CTX *ctx = NULL;
712 err = "INTERNAL_ERROR";
713 ctx = EVP_CIPHER_CTX_new();
716 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
718 in = cdat->plaintext;
719 in_len = cdat->plaintext_len;
720 out = cdat->ciphertext;
721 out_len = cdat->ciphertext_len;
723 in = cdat->ciphertext;
724 in_len = cdat->ciphertext_len;
725 out = cdat->plaintext;
726 out_len = cdat->plaintext_len;
728 tmp = OPENSSL_malloc(in_len + 2 * EVP_MAX_BLOCK_LENGTH);
731 err = "CIPHERINIT_ERROR";
732 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
734 err = "INVALID_IV_LENGTH";
736 if (cdat->aead == EVP_CIPH_GCM_MODE) {
737 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN,
740 } else if (cdat->aead == EVP_CIPH_CCM_MODE) {
741 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN,
744 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
750 * If encrypting just set tag length. If decrypting set
751 * tag length and value.
754 err = "TAG_LENGTH_SET_ERROR";
757 err = "TAG_SET_ERROR";
760 if (cdat->aead == EVP_CIPH_GCM_MODE && tag) {
761 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG,
764 } else if (cdat->aead == EVP_CIPH_CCM_MODE) {
765 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG,
771 err = "INVALID_KEY_LENGTH";
772 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len))
774 err = "KEY_SET_ERROR";
775 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1))
778 if (cdat->aead == EVP_CIPH_CCM_MODE) {
779 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
780 err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
785 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, cdat->aad, cdat->aad_len)) {
786 err = "AAD_SET_ERROR";
790 EVP_CIPHER_CTX_set_padding(ctx, 0);
791 err = "CIPHERUPDATE_ERROR";
792 if (!EVP_CipherUpdate(ctx, tmp, &tmplen, in, in_len))
794 if (cdat->aead == EVP_CIPH_CCM_MODE)
797 err = "CIPHERFINAL_ERROR";
798 if (!EVP_CipherFinal_ex(ctx, tmp + tmplen, &tmpflen))
801 err = "LENGTH_MISMATCH";
802 if (out_len != (size_t)(tmplen + tmpflen))
804 err = "VALUE_MISMATCH";
805 if (check_output(t, out, tmp, out_len))
807 if (enc && cdat->aead) {
808 unsigned char rtag[16];
809 if (cdat->tag_len > sizeof(rtag)) {
810 err = "TAG_LENGTH_INTERNAL_ERROR";
813 /* EVP_CTRL_CCM_GET_TAG and EVP_CTRL_GCM_GET_TAG are equal. */
814 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG,
815 cdat->tag_len, rtag)) {
816 err = "TAG_RETRIEVE_ERROR";
819 if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {
820 err = "TAG_VALUE_MISMATCH";
828 EVP_CIPHER_CTX_free(ctx);
833 static int cipher_test_run(struct evp_test *t)
835 struct cipher_data *cdat = t->data;
841 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
842 /* IV is optional and usually omitted in wrap mode */
843 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
848 if (cdat->aead && !cdat->tag) {
853 rv = cipher_test_enc(t, 1);
854 /* Not fatal errors: return */
861 if (cdat->enc != 1) {
862 rv = cipher_test_enc(t, 0);
863 /* Not fatal errors: return */
873 static const struct evp_test_method cipher_test_method = {
884 /* Algorithm string for this MAC */
890 unsigned char *input;
892 /* Expected output */
893 unsigned char *output;
897 static int mac_test_init(struct evp_test *t, const char *alg)
900 struct mac_data *mdat;
901 if (!strcmp(alg, "HMAC"))
902 type = EVP_PKEY_HMAC;
903 else if (!strcmp(alg, "CMAC"))
904 type = EVP_PKEY_CMAC;
908 mdat = OPENSSL_malloc(sizeof(struct mac_data));
918 static void mac_test_cleanup(struct evp_test *t)
920 struct mac_data *mdat = t->data;
921 test_free(mdat->alg);
922 test_free(mdat->key);
923 test_free(mdat->input);
924 test_free(mdat->output);
927 static int mac_test_parse(struct evp_test *t,
928 const char *keyword, const char *value)
930 struct mac_data *mdata = t->data;
931 if (!strcmp(keyword, "Key"))
932 return test_bin(value, &mdata->key, &mdata->key_len);
933 if (!strcmp(keyword, "Algorithm")) {
934 mdata->alg = BUF_strdup(value);
939 if (!strcmp(keyword, "Input"))
940 return test_bin(value, &mdata->input, &mdata->input_len);
941 if (!strcmp(keyword, "Output"))
942 return test_bin(value, &mdata->output, &mdata->output_len);
946 static int mac_test_run(struct evp_test *t)
948 struct mac_data *mdata = t->data;
949 const char *err = "INTERNAL_ERROR";
950 EVP_MD_CTX *mctx = NULL;
951 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
952 EVP_PKEY *key = NULL;
953 const EVP_MD *md = NULL;
954 unsigned char *mac = NULL;
957 err = "MAC_PKEY_CTX_ERROR";
958 genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL);
962 err = "MAC_KEYGEN_INIT_ERROR";
963 if (EVP_PKEY_keygen_init(genctx) <= 0)
965 if (mdata->type == EVP_PKEY_CMAC) {
966 err = "MAC_ALGORITHM_SET_ERROR";
967 if (EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0)
971 err = "MAC_KEY_SET_ERROR";
972 if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0)
975 err = "MAC_KEY_GENERATE_ERROR";
976 if (EVP_PKEY_keygen(genctx, &key) <= 0)
978 if (mdata->type == EVP_PKEY_HMAC) {
979 err = "MAC_ALGORITHM_SET_ERROR";
980 md = EVP_get_digestbyname(mdata->alg);
984 mctx = EVP_MD_CTX_create();
987 err = "DIGESTSIGNINIT_ERROR";
988 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key))
991 err = "DIGESTSIGNUPDATE_ERROR";
992 if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len))
994 err = "DIGESTSIGNFINAL_LENGTH_ERROR";
995 if (!EVP_DigestSignFinal(mctx, NULL, &mac_len))
997 mac = OPENSSL_malloc(mac_len);
999 fprintf(stderr, "Error allocating mac buffer!\n");
1002 if (!EVP_DigestSignFinal(mctx, mac, &mac_len))
1004 err = "MAC_LENGTH_MISMATCH";
1005 if (mac_len != mdata->output_len)
1007 err = "MAC_MISMATCH";
1008 if (check_output(t, mdata->output, mac, mac_len))
1013 EVP_MD_CTX_destroy(mctx);
1017 EVP_PKEY_CTX_free(genctx);
1024 static const struct evp_test_method mac_test_method = {
1033 * Public key operations. These are all very similar and can share
1034 * a lot of common code.
1038 /* Context for this operation */
1040 /* Key operation to perform */
1041 int (*keyop) (EVP_PKEY_CTX *ctx,
1042 unsigned char *sig, size_t *siglen,
1043 const unsigned char *tbs, size_t tbslen);
1045 unsigned char *input;
1047 /* Expected output */
1048 unsigned char *output;
1053 * Perform public key operation setup: lookup key, allocated ctx and call
1054 * the appropriate initialisation function
1056 static int pkey_test_init(struct evp_test *t, const char *name,
1058 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1059 int (*keyop) (EVP_PKEY_CTX *ctx,
1060 unsigned char *sig, size_t *siglen,
1061 const unsigned char *tbs,
1065 struct pkey_data *kdata;
1066 EVP_PKEY *pkey = NULL;
1069 rv = find_key(&pkey, name, t->public);
1071 rv = find_key(&pkey, name, t->private);
1079 kdata = OPENSSL_malloc(sizeof(struct pkey_data));
1081 EVP_PKEY_free(pkey);
1085 kdata->input = NULL;
1086 kdata->output = NULL;
1087 kdata->keyop = keyop;
1089 kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL);
1092 if (keyopinit(kdata->ctx) <= 0)
1097 static void pkey_test_cleanup(struct evp_test *t)
1099 struct pkey_data *kdata = t->data;
1101 OPENSSL_free(kdata->input);
1103 OPENSSL_free(kdata->output);
1105 EVP_PKEY_CTX_free(kdata->ctx);
1108 static int pkey_test_parse(struct evp_test *t,
1109 const char *keyword, const char *value)
1111 struct pkey_data *kdata = t->data;
1112 if (!strcmp(keyword, "Input"))
1113 return test_bin(value, &kdata->input, &kdata->input_len);
1114 if (!strcmp(keyword, "Output"))
1115 return test_bin(value, &kdata->output, &kdata->output_len);
1116 if (!strcmp(keyword, "Ctrl")) {
1117 char *p = strchr(value, ':');
1120 if (EVP_PKEY_CTX_ctrl_str(kdata->ctx, value, p) <= 0)
1127 static int pkey_test_run(struct evp_test *t)
1129 struct pkey_data *kdata = t->data;
1130 unsigned char *out = NULL;
1132 const char *err = "KEYOP_LENGTH_ERROR";
1133 if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1134 kdata->input_len) <= 0)
1136 out = OPENSSL_malloc(out_len);
1138 fprintf(stderr, "Error allocating output buffer!\n");
1141 err = "KEYOP_ERROR";
1143 (kdata->ctx, out, &out_len, kdata->input, kdata->input_len) <= 0)
1145 err = "KEYOP_LENGTH_MISMATCH";
1146 if (out_len != kdata->output_len)
1148 err = "KEYOP_MISMATCH";
1149 if (check_output(t, kdata->output, out, out_len))
1159 static int sign_test_init(struct evp_test *t, const char *name)
1161 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1164 static const struct evp_test_method psign_test_method = {
1172 static int verify_recover_test_init(struct evp_test *t, const char *name)
1174 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1175 EVP_PKEY_verify_recover);
1178 static const struct evp_test_method pverify_recover_test_method = {
1180 verify_recover_test_init,
1186 static int decrypt_test_init(struct evp_test *t, const char *name)
1188 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1192 static const struct evp_test_method pdecrypt_test_method = {
1200 static int verify_test_init(struct evp_test *t, const char *name)
1202 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1205 static int verify_test_run(struct evp_test *t)
1207 struct pkey_data *kdata = t->data;
1208 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1209 kdata->input, kdata->input_len) <= 0)
1210 t->err = "VERIFY_ERROR";
1214 static const struct evp_test_method pverify_test_method = {