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 */
298 if (t->meth->run_test(t) != 1) {
299 fprintf(stderr, "%s test error line %d\n",
300 t->meth->name, t->start_line);
303 if (!check_test_error(t)) {
305 ERR_print_errors_fp(stderr);
310 OPENSSL_free(t->data);
312 if (t->expected_err) {
313 OPENSSL_free(t->expected_err);
314 t->expected_err = NULL;
321 static int find_key(EVP_PKEY **ppk, const char *name, struct key_list *lst)
323 for (; lst; lst = lst->next) {
324 if (!strcmp(lst->name, name)) {
333 static void free_key_list(struct key_list *lst)
335 while (lst != NULL) {
336 struct key_list *ltmp;
337 EVP_PKEY_free(lst->key);
338 OPENSSL_free(lst->name);
345 static int check_unsupported()
347 long err = ERR_peek_error();
348 if (ERR_GET_LIB(err) == ERR_LIB_EVP
349 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
356 static int process_test(struct evp_test *t, char *buf, int verbose)
358 char *keyword, *value;
359 int rv = 0, add_key = 0;
361 struct key_list **lst, *key;
363 const struct evp_test_method *tmeth;
366 if (!parse_line(&keyword, &value, buf))
368 if (!strcmp(keyword, "PrivateKey")) {
369 save_pos = ftell(t->in);
370 pk = PEM_read_PrivateKey(t->in, NULL, 0, NULL);
371 if (pk == NULL && !check_unsupported()) {
372 fprintf(stderr, "Error reading private key %s\n", value);
373 ERR_print_errors_fp(stderr);
379 if (!strcmp(keyword, "PublicKey")) {
380 save_pos = ftell(t->in);
381 pk = PEM_read_PUBKEY(t->in, NULL, 0, NULL);
382 if (pk == NULL && !check_unsupported()) {
383 fprintf(stderr, "Error reading public key %s\n", value);
384 ERR_print_errors_fp(stderr);
390 /* If we have a key add to list */
393 if (find_key(NULL, value, *lst)) {
394 fprintf(stderr, "Duplicate key %s\n", value);
397 key = OPENSSL_malloc(sizeof(struct key_list));
400 key->name = BUF_strdup(value);
404 /* Rewind input, read to end and update line numbers */
405 fseek(t->in, save_pos, SEEK_SET);
406 while (fgets(tmpbuf, sizeof(tmpbuf), t->in)) {
408 if (!strncmp(tmpbuf, "-----END", 8))
411 fprintf(stderr, "Can't find key end\n");
415 /* See if keyword corresponds to a test start */
416 tmeth = evp_find_test(keyword);
418 if (!setup_test(t, tmeth))
420 t->start_line = t->line;
422 if (!tmeth->init(t, value)) {
423 fprintf(stderr, "Unknown %s: %s\n", keyword, value);
427 } else if (t->skip) {
429 } else if (!strcmp(keyword, "Result")) {
430 if (t->expected_err) {
431 fprintf(stderr, "Line %d: multiple result lines\n", t->line);
434 t->expected_err = BUF_strdup(value);
435 if (!t->expected_err)
438 /* Must be test specific line: try to parse it */
440 rv = t->meth->parse(t, keyword, value);
443 fprintf(stderr, "line %d: unexpected keyword %s\n",
447 fprintf(stderr, "line %d: error processing keyword %s\n",
455 static int check_output(struct evp_test *t, const unsigned char *expected,
456 const unsigned char *got, size_t len)
458 if (!memcmp(expected, got, len))
460 t->out_expected = BUF_memdup(expected, len);
461 t->out_got = BUF_memdup(got, len);
463 if (t->out_expected == NULL || t->out_got == NULL) {
464 fprintf(stderr, "Memory allocation error!\n");
470 int main(int argc, char **argv)
477 fprintf(stderr, "usage: evp_test testfile.txt\n");
481 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
483 ERR_load_crypto_strings();
484 OpenSSL_add_all_algorithms();
486 memset(&t, 0, sizeof(t));
495 t.out_expected = NULL;
498 in = fopen(argv[1], "r");
500 while (fgets(buf, sizeof(buf), in)) {
502 if (!process_test(&t, buf, 0))
505 /* Run any final test we have */
506 if (!setup_test(&t, NULL))
508 fprintf(stderr, "%d tests completed with %d errors, %d skipped\n",
509 t.ntests, t.errors, t.nskip);
510 free_key_list(t.public);
511 free_key_list(t.private);
514 CRYPTO_cleanup_all_ex_data();
515 ERR_remove_thread_state(NULL);
517 CRYPTO_mem_leaks_fp(stderr);
523 static void test_free(void *d)
529 /* Message digest tests */
532 /* Digest this test is for */
533 const EVP_MD *digest;
534 /* Input to digest */
535 unsigned char *input;
537 /* Expected output */
538 unsigned char *output;
542 static int digest_test_init(struct evp_test *t, const char *alg)
544 const EVP_MD *digest;
545 struct digest_data *mdat = t->data;
546 digest = EVP_get_digestbyname(alg);
548 /* If alg has an OID assume disabled algorithm */
549 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
555 mdat = OPENSSL_malloc(sizeof(struct digest_data));
556 mdat->digest = digest;
563 static void digest_test_cleanup(struct evp_test *t)
565 struct digest_data *mdat = t->data;
566 test_free(mdat->input);
567 test_free(mdat->output);
570 static int digest_test_parse(struct evp_test *t,
571 const char *keyword, const char *value)
573 struct digest_data *mdata = t->data;
574 if (!strcmp(keyword, "Input"))
575 return test_bin(value, &mdata->input, &mdata->input_len);
576 if (!strcmp(keyword, "Output"))
577 return test_bin(value, &mdata->output, &mdata->output_len);
581 static int digest_test_run(struct evp_test *t)
583 struct digest_data *mdata = t->data;
584 const char *err = "INTERNAL_ERROR";
586 unsigned char md[EVP_MAX_MD_SIZE];
588 mctx = EVP_MD_CTX_create();
591 err = "DIGESTINIT_ERROR";
592 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))
594 err = "DIGESTUPDATE_ERROR";
595 if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))
597 err = "DIGESTFINAL_ERROR";
598 if (!EVP_DigestFinal(mctx, md, &md_len))
600 err = "DIGEST_LENGTH_MISMATCH";
601 if (md_len != mdata->output_len)
603 err = "DIGEST_MISMATCH";
604 if (check_output(t, mdata->output, md, md_len))
609 EVP_MD_CTX_destroy(mctx);
614 static const struct evp_test_method digest_test_method = {
624 const EVP_CIPHER *cipher;
626 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
632 unsigned char *plaintext;
633 size_t plaintext_len;
634 unsigned char *ciphertext;
635 size_t ciphertext_len;
643 static int cipher_test_init(struct evp_test *t, const char *alg)
645 const EVP_CIPHER *cipher;
646 struct cipher_data *cdat = t->data;
647 cipher = EVP_get_cipherbyname(alg);
649 /* If alg has an OID assume disabled algorithm */
650 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
656 cdat = OPENSSL_malloc(sizeof(struct cipher_data));
657 cdat->cipher = cipher;
661 cdat->ciphertext = NULL;
662 cdat->plaintext = NULL;
666 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
667 || EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE
668 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
669 cdat->aead = EVP_CIPHER_mode(cipher);
676 static void cipher_test_cleanup(struct evp_test *t)
678 struct cipher_data *cdat = t->data;
679 test_free(cdat->key);
681 test_free(cdat->ciphertext);
682 test_free(cdat->plaintext);
683 test_free(cdat->aad);
684 test_free(cdat->tag);
687 static int cipher_test_parse(struct evp_test *t, const char *keyword,
690 struct cipher_data *cdat = t->data;
691 if (!strcmp(keyword, "Key"))
692 return test_bin(value, &cdat->key, &cdat->key_len);
693 if (!strcmp(keyword, "IV"))
694 return test_bin(value, &cdat->iv, &cdat->iv_len);
695 if (!strcmp(keyword, "Plaintext"))
696 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
697 if (!strcmp(keyword, "Ciphertext"))
698 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
700 if (!strcmp(keyword, "AAD"))
701 return test_bin(value, &cdat->aad, &cdat->aad_len);
702 if (!strcmp(keyword, "Tag"))
703 return test_bin(value, &cdat->tag, &cdat->tag_len);
706 if (!strcmp(keyword, "Operation")) {
707 if (!strcmp(value, "ENCRYPT"))
709 else if (!strcmp(value, "DECRYPT"))
718 static int cipher_test_enc(struct evp_test *t, int enc)
720 struct cipher_data *cdat = t->data;
721 unsigned char *in, *out, *tmp = NULL;
722 size_t in_len, out_len;
724 EVP_CIPHER_CTX *ctx = NULL;
726 err = "INTERNAL_ERROR";
727 ctx = EVP_CIPHER_CTX_new();
730 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
732 in = cdat->plaintext;
733 in_len = cdat->plaintext_len;
734 out = cdat->ciphertext;
735 out_len = cdat->ciphertext_len;
737 in = cdat->ciphertext;
738 in_len = cdat->ciphertext_len;
739 out = cdat->plaintext;
740 out_len = cdat->plaintext_len;
742 tmp = OPENSSL_malloc(in_len + 2 * EVP_MAX_BLOCK_LENGTH);
745 err = "CIPHERINIT_ERROR";
746 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
748 err = "INVALID_IV_LENGTH";
751 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
754 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
760 * If encrypting or OCB just set tag length initially, otherwise
761 * set tag length and value.
763 if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {
764 err = "TAG_LENGTH_SET_ERROR";
767 err = "TAG_SET_ERROR";
770 if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {
771 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
777 err = "INVALID_KEY_LENGTH";
778 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len))
780 err = "KEY_SET_ERROR";
781 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1))
784 if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {
785 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
786 cdat->tag_len, cdat->tag)) {
787 err = "TAG_SET_ERROR";
792 if (cdat->aead == EVP_CIPH_CCM_MODE) {
793 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
794 err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
799 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, cdat->aad, cdat->aad_len)) {
800 err = "AAD_SET_ERROR";
804 EVP_CIPHER_CTX_set_padding(ctx, 0);
805 err = "CIPHERUPDATE_ERROR";
806 if (!EVP_CipherUpdate(ctx, tmp, &tmplen, in, in_len))
808 if (cdat->aead == EVP_CIPH_CCM_MODE)
811 err = "CIPHERFINAL_ERROR";
812 if (!EVP_CipherFinal_ex(ctx, tmp + tmplen, &tmpflen))
815 err = "LENGTH_MISMATCH";
816 if (out_len != (size_t)(tmplen + tmpflen))
818 err = "VALUE_MISMATCH";
819 if (check_output(t, out, tmp, out_len))
821 if (enc && cdat->aead) {
822 unsigned char rtag[16];
823 if (cdat->tag_len > sizeof(rtag)) {
824 err = "TAG_LENGTH_INTERNAL_ERROR";
827 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
828 cdat->tag_len, rtag)) {
829 err = "TAG_RETRIEVE_ERROR";
832 if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {
833 err = "TAG_VALUE_MISMATCH";
841 EVP_CIPHER_CTX_free(ctx);
846 static int cipher_test_run(struct evp_test *t)
848 struct cipher_data *cdat = t->data;
854 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
855 /* IV is optional and usually omitted in wrap mode */
856 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
861 if (cdat->aead && !cdat->tag) {
866 rv = cipher_test_enc(t, 1);
867 /* Not fatal errors: return */
874 if (cdat->enc != 1) {
875 rv = cipher_test_enc(t, 0);
876 /* Not fatal errors: return */
886 static const struct evp_test_method cipher_test_method = {
897 /* Algorithm string for this MAC */
903 unsigned char *input;
905 /* Expected output */
906 unsigned char *output;
910 static int mac_test_init(struct evp_test *t, const char *alg)
913 struct mac_data *mdat;
914 if (!strcmp(alg, "HMAC"))
915 type = EVP_PKEY_HMAC;
916 else if (!strcmp(alg, "CMAC"))
917 type = EVP_PKEY_CMAC;
921 mdat = OPENSSL_malloc(sizeof(struct mac_data));
931 static void mac_test_cleanup(struct evp_test *t)
933 struct mac_data *mdat = t->data;
934 test_free(mdat->alg);
935 test_free(mdat->key);
936 test_free(mdat->input);
937 test_free(mdat->output);
940 static int mac_test_parse(struct evp_test *t,
941 const char *keyword, const char *value)
943 struct mac_data *mdata = t->data;
944 if (!strcmp(keyword, "Key"))
945 return test_bin(value, &mdata->key, &mdata->key_len);
946 if (!strcmp(keyword, "Algorithm")) {
947 mdata->alg = BUF_strdup(value);
952 if (!strcmp(keyword, "Input"))
953 return test_bin(value, &mdata->input, &mdata->input_len);
954 if (!strcmp(keyword, "Output"))
955 return test_bin(value, &mdata->output, &mdata->output_len);
959 static int mac_test_run(struct evp_test *t)
961 struct mac_data *mdata = t->data;
962 const char *err = "INTERNAL_ERROR";
963 EVP_MD_CTX *mctx = NULL;
964 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
965 EVP_PKEY *key = NULL;
966 const EVP_MD *md = NULL;
967 unsigned char *mac = NULL;
970 err = "MAC_PKEY_CTX_ERROR";
971 genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL);
975 err = "MAC_KEYGEN_INIT_ERROR";
976 if (EVP_PKEY_keygen_init(genctx) <= 0)
978 if (mdata->type == EVP_PKEY_CMAC) {
979 err = "MAC_ALGORITHM_SET_ERROR";
980 if (EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0)
984 err = "MAC_KEY_SET_ERROR";
985 if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0)
988 err = "MAC_KEY_GENERATE_ERROR";
989 if (EVP_PKEY_keygen(genctx, &key) <= 0)
991 if (mdata->type == EVP_PKEY_HMAC) {
992 err = "MAC_ALGORITHM_SET_ERROR";
993 md = EVP_get_digestbyname(mdata->alg);
997 mctx = EVP_MD_CTX_create();
1000 err = "DIGESTSIGNINIT_ERROR";
1001 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key))
1004 err = "DIGESTSIGNUPDATE_ERROR";
1005 if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len))
1007 err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1008 if (!EVP_DigestSignFinal(mctx, NULL, &mac_len))
1010 mac = OPENSSL_malloc(mac_len);
1012 fprintf(stderr, "Error allocating mac buffer!\n");
1015 if (!EVP_DigestSignFinal(mctx, mac, &mac_len))
1017 err = "MAC_LENGTH_MISMATCH";
1018 if (mac_len != mdata->output_len)
1020 err = "MAC_MISMATCH";
1021 if (check_output(t, mdata->output, mac, mac_len))
1026 EVP_MD_CTX_destroy(mctx);
1030 EVP_PKEY_CTX_free(genctx);
1037 static const struct evp_test_method mac_test_method = {
1046 * Public key operations. These are all very similar and can share
1047 * a lot of common code.
1051 /* Context for this operation */
1053 /* Key operation to perform */
1054 int (*keyop) (EVP_PKEY_CTX *ctx,
1055 unsigned char *sig, size_t *siglen,
1056 const unsigned char *tbs, size_t tbslen);
1058 unsigned char *input;
1060 /* Expected output */
1061 unsigned char *output;
1066 * Perform public key operation setup: lookup key, allocated ctx and call
1067 * the appropriate initialisation function
1069 static int pkey_test_init(struct evp_test *t, const char *name,
1071 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1072 int (*keyop) (EVP_PKEY_CTX *ctx,
1073 unsigned char *sig, size_t *siglen,
1074 const unsigned char *tbs,
1078 struct pkey_data *kdata;
1079 EVP_PKEY *pkey = NULL;
1082 rv = find_key(&pkey, name, t->public);
1084 rv = find_key(&pkey, name, t->private);
1092 kdata = OPENSSL_malloc(sizeof(struct pkey_data));
1094 EVP_PKEY_free(pkey);
1098 kdata->input = NULL;
1099 kdata->output = NULL;
1100 kdata->keyop = keyop;
1102 kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL);
1105 if (keyopinit(kdata->ctx) <= 0)
1110 static void pkey_test_cleanup(struct evp_test *t)
1112 struct pkey_data *kdata = t->data;
1114 OPENSSL_free(kdata->input);
1116 OPENSSL_free(kdata->output);
1118 EVP_PKEY_CTX_free(kdata->ctx);
1121 static int pkey_test_parse(struct evp_test *t,
1122 const char *keyword, const char *value)
1124 struct pkey_data *kdata = t->data;
1125 if (!strcmp(keyword, "Input"))
1126 return test_bin(value, &kdata->input, &kdata->input_len);
1127 if (!strcmp(keyword, "Output"))
1128 return test_bin(value, &kdata->output, &kdata->output_len);
1129 if (!strcmp(keyword, "Ctrl")) {
1130 char *p = strchr(value, ':');
1133 if (EVP_PKEY_CTX_ctrl_str(kdata->ctx, value, p) <= 0)
1140 static int pkey_test_run(struct evp_test *t)
1142 struct pkey_data *kdata = t->data;
1143 unsigned char *out = NULL;
1145 const char *err = "KEYOP_LENGTH_ERROR";
1146 if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1147 kdata->input_len) <= 0)
1149 out = OPENSSL_malloc(out_len);
1151 fprintf(stderr, "Error allocating output buffer!\n");
1154 err = "KEYOP_ERROR";
1156 (kdata->ctx, out, &out_len, kdata->input, kdata->input_len) <= 0)
1158 err = "KEYOP_LENGTH_MISMATCH";
1159 if (out_len != kdata->output_len)
1161 err = "KEYOP_MISMATCH";
1162 if (check_output(t, kdata->output, out, out_len))
1172 static int sign_test_init(struct evp_test *t, const char *name)
1174 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1177 static const struct evp_test_method psign_test_method = {
1185 static int verify_recover_test_init(struct evp_test *t, const char *name)
1187 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1188 EVP_PKEY_verify_recover);
1191 static const struct evp_test_method pverify_recover_test_method = {
1193 verify_recover_test_init,
1199 static int decrypt_test_init(struct evp_test *t, const char *name)
1201 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1205 static const struct evp_test_method pdecrypt_test_method = {
1213 static int verify_test_init(struct evp_test *t, const char *name)
1215 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1218 static int verify_test_run(struct evp_test *t)
1220 struct pkey_data *kdata = t->data;
1221 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1222 kdata->input, kdata->input_len) <= 0)
1223 t->err = "VERIFY_ERROR";
1227 static const struct evp_test_method pverify_test_method = {