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;
235 for (tt = evp_test_list; *tt; tt++) {
236 if (strcmp(name, (*tt)->name) == 0)
242 static void hex_print(const char *name, const unsigned char *buf, size_t len)
245 fprintf(stderr, "%s ", name);
246 for (i = 0; i < len; i++)
247 fprintf(stderr, "%02X", buf[i]);
251 static void free_expected(struct evp_test *t)
253 OPENSSL_free(t->expected_err);
254 t->expected_err = NULL;
255 OPENSSL_free(t->out_expected);
256 OPENSSL_free(t->out_got);
257 t->out_expected = NULL;
261 static void print_expected(struct evp_test *t)
263 if (t->out_expected == NULL)
265 hex_print("Expected:", t->out_expected, t->out_len);
266 hex_print("Got: ", t->out_got, t->out_len);
270 static int check_test_error(struct evp_test *t)
272 if (!t->err && !t->expected_err)
274 if (t->err && !t->expected_err) {
275 fprintf(stderr, "Test line %d: unexpected error %s\n",
276 t->start_line, t->err);
280 if (!t->err && t->expected_err) {
281 fprintf(stderr, "Test line %d: succeeded expecting %s\n",
282 t->start_line, t->expected_err);
285 if (strcmp(t->err, t->expected_err) == 0)
288 fprintf(stderr, "Test line %d: expecting %s got %s\n",
289 t->start_line, t->expected_err, t->err);
293 /* Setup a new test, run any existing test */
295 static int setup_test(struct evp_test *t, const struct evp_test_method *tmeth)
297 /* If we already have a test set up run it */
306 if (t->meth->run_test(t) != 1) {
307 fprintf(stderr, "%s test error line %d\n",
308 t->meth->name, t->start_line);
311 if (!check_test_error(t)) {
313 ERR_print_errors_fp(stderr);
318 OPENSSL_free(t->data);
320 OPENSSL_free(t->expected_err);
321 t->expected_err = NULL;
328 static int find_key(EVP_PKEY **ppk, const char *name, struct key_list *lst)
330 for (; lst; lst = lst->next) {
331 if (strcmp(lst->name, name) == 0) {
340 static void free_key_list(struct key_list *lst)
342 while (lst != NULL) {
343 struct key_list *ltmp;
344 EVP_PKEY_free(lst->key);
345 OPENSSL_free(lst->name);
352 static int check_unsupported()
354 long err = ERR_peek_error();
355 if (ERR_GET_LIB(err) == ERR_LIB_EVP
356 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
363 static int process_test(struct evp_test *t, char *buf, int verbose)
365 char *keyword = NULL, *value = NULL;
366 int rv = 0, add_key = 0;
368 struct key_list **lst = NULL, *key = NULL;
370 const struct evp_test_method *tmeth = NULL;
373 if (!parse_line(&keyword, &value, buf))
375 if (strcmp(keyword, "PrivateKey") == 0) {
376 save_pos = ftell(t->in);
377 pk = PEM_read_PrivateKey(t->in, NULL, 0, NULL);
378 if (pk == NULL && !check_unsupported()) {
379 fprintf(stderr, "Error reading private key %s\n", value);
380 ERR_print_errors_fp(stderr);
386 if (strcmp(keyword, "PublicKey") == 0) {
387 save_pos = ftell(t->in);
388 pk = PEM_read_PUBKEY(t->in, NULL, 0, NULL);
389 if (pk == NULL && !check_unsupported()) {
390 fprintf(stderr, "Error reading public key %s\n", value);
391 ERR_print_errors_fp(stderr);
397 /* If we have a key add to list */
400 if (find_key(NULL, value, *lst)) {
401 fprintf(stderr, "Duplicate key %s\n", value);
404 key = OPENSSL_malloc(sizeof(*key));
407 key->name = BUF_strdup(value);
411 /* Rewind input, read to end and update line numbers */
412 fseek(t->in, save_pos, SEEK_SET);
413 while (fgets(tmpbuf, sizeof(tmpbuf), t->in)) {
415 if (strncmp(tmpbuf, "-----END", 8) == 0)
418 fprintf(stderr, "Can't find key end\n");
422 /* See if keyword corresponds to a test start */
423 tmeth = evp_find_test(keyword);
425 if (!setup_test(t, tmeth))
427 t->start_line = t->line;
429 if (!tmeth->init(t, value)) {
430 fprintf(stderr, "Unknown %s: %s\n", keyword, value);
434 } else if (t->skip) {
436 } else if (strcmp(keyword, "Result") == 0) {
437 if (t->expected_err) {
438 fprintf(stderr, "Line %d: multiple result lines\n", t->line);
441 t->expected_err = BUF_strdup(value);
442 if (!t->expected_err)
445 /* Must be test specific line: try to parse it */
447 rv = t->meth->parse(t, keyword, value);
450 fprintf(stderr, "line %d: unexpected keyword %s\n",
454 fprintf(stderr, "line %d: error processing keyword %s\n",
462 static int check_output(struct evp_test *t, const unsigned char *expected,
463 const unsigned char *got, size_t len)
465 if (!memcmp(expected, got, len))
467 t->out_expected = BUF_memdup(expected, len);
468 t->out_got = BUF_memdup(got, len);
470 if (t->out_expected == NULL || t->out_got == NULL) {
471 fprintf(stderr, "Memory allocation error!\n");
477 int main(int argc, char **argv)
484 fprintf(stderr, "usage: evp_test testfile.txt\n");
488 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
490 ERR_load_crypto_strings();
491 OpenSSL_add_all_algorithms();
493 memset(&t, 0, sizeof(t));
502 t.out_expected = NULL;
505 in = fopen(argv[1], "r");
507 while (fgets(buf, sizeof(buf), in)) {
509 if (!process_test(&t, buf, 0))
512 /* Run any final test we have */
513 if (!setup_test(&t, NULL))
515 fprintf(stderr, "%d tests completed with %d errors, %d skipped\n",
516 t.ntests, t.errors, t.nskip);
517 free_key_list(t.public);
518 free_key_list(t.private);
521 CRYPTO_cleanup_all_ex_data();
522 ERR_remove_thread_state(NULL);
524 CRYPTO_mem_leaks_fp(stderr);
530 static void test_free(void *d)
535 /* Message digest tests */
538 /* Digest this test is for */
539 const EVP_MD *digest;
540 /* Input to digest */
541 unsigned char *input;
543 /* Repeat count for input */
545 /* Expected output */
546 unsigned char *output;
550 static int digest_test_init(struct evp_test *t, const char *alg)
552 const EVP_MD *digest;
553 struct digest_data *mdat = t->data;
554 digest = EVP_get_digestbyname(alg);
556 /* If alg has an OID assume disabled algorithm */
557 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
563 mdat = OPENSSL_malloc(sizeof(*mdat));
564 mdat->digest = digest;
572 static void digest_test_cleanup(struct evp_test *t)
574 struct digest_data *mdat = t->data;
575 test_free(mdat->input);
576 test_free(mdat->output);
579 static int digest_test_parse(struct evp_test *t,
580 const char *keyword, const char *value)
582 struct digest_data *mdata = t->data;
583 if (strcmp(keyword, "Input") == 0)
584 return test_bin(value, &mdata->input, &mdata->input_len);
585 if (strcmp(keyword, "Output") == 0)
586 return test_bin(value, &mdata->output, &mdata->output_len);
587 if (strcmp(keyword, "Count") == 0) {
588 long nrpt = atoi(value);
591 mdata->nrpt = (size_t)nrpt;
597 static int digest_test_run(struct evp_test *t)
599 struct digest_data *mdata = t->data;
601 const char *err = "INTERNAL_ERROR";
603 unsigned char md[EVP_MAX_MD_SIZE];
605 mctx = EVP_MD_CTX_create();
608 err = "DIGESTINIT_ERROR";
609 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))
611 err = "DIGESTUPDATE_ERROR";
612 for (i = 0; i < mdata->nrpt; i++) {
613 if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))
616 err = "DIGESTFINAL_ERROR";
617 if (!EVP_DigestFinal(mctx, md, &md_len))
619 err = "DIGEST_LENGTH_MISMATCH";
620 if (md_len != mdata->output_len)
622 err = "DIGEST_MISMATCH";
623 if (check_output(t, mdata->output, md, md_len))
628 EVP_MD_CTX_destroy(mctx);
633 static const struct evp_test_method digest_test_method = {
643 const EVP_CIPHER *cipher;
645 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
651 unsigned char *plaintext;
652 size_t plaintext_len;
653 unsigned char *ciphertext;
654 size_t ciphertext_len;
662 static int cipher_test_init(struct evp_test *t, const char *alg)
664 const EVP_CIPHER *cipher;
665 struct cipher_data *cdat = t->data;
666 cipher = EVP_get_cipherbyname(alg);
668 /* If alg has an OID assume disabled algorithm */
669 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
675 cdat = OPENSSL_malloc(sizeof(*cdat));
676 cdat->cipher = cipher;
680 cdat->ciphertext = NULL;
681 cdat->plaintext = NULL;
685 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
686 || EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE
687 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
688 cdat->aead = EVP_CIPHER_mode(cipher);
695 static void cipher_test_cleanup(struct evp_test *t)
697 struct cipher_data *cdat = t->data;
698 test_free(cdat->key);
700 test_free(cdat->ciphertext);
701 test_free(cdat->plaintext);
702 test_free(cdat->aad);
703 test_free(cdat->tag);
706 static int cipher_test_parse(struct evp_test *t, const char *keyword,
709 struct cipher_data *cdat = t->data;
710 if (strcmp(keyword, "Key") == 0)
711 return test_bin(value, &cdat->key, &cdat->key_len);
712 if (strcmp(keyword, "IV") == 0)
713 return test_bin(value, &cdat->iv, &cdat->iv_len);
714 if (strcmp(keyword, "Plaintext") == 0)
715 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
716 if (strcmp(keyword, "Ciphertext") == 0)
717 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
719 if (strcmp(keyword, "AAD") == 0)
720 return test_bin(value, &cdat->aad, &cdat->aad_len);
721 if (strcmp(keyword, "Tag") == 0)
722 return test_bin(value, &cdat->tag, &cdat->tag_len);
725 if (strcmp(keyword, "Operation") == 0) {
726 if (strcmp(value, "ENCRYPT") == 0)
728 else if (strcmp(value, "DECRYPT") == 0)
737 static int cipher_test_enc(struct evp_test *t, int enc)
739 struct cipher_data *cdat = t->data;
740 unsigned char *in, *out, *tmp = NULL;
741 size_t in_len, out_len;
743 EVP_CIPHER_CTX *ctx = NULL;
745 err = "INTERNAL_ERROR";
746 ctx = EVP_CIPHER_CTX_new();
749 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
751 in = cdat->plaintext;
752 in_len = cdat->plaintext_len;
753 out = cdat->ciphertext;
754 out_len = cdat->ciphertext_len;
756 in = cdat->ciphertext;
757 in_len = cdat->ciphertext_len;
758 out = cdat->plaintext;
759 out_len = cdat->plaintext_len;
761 tmp = OPENSSL_malloc(in_len + 2 * EVP_MAX_BLOCK_LENGTH);
764 err = "CIPHERINIT_ERROR";
765 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
767 err = "INVALID_IV_LENGTH";
770 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
773 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
779 * If encrypting or OCB just set tag length initially, otherwise
780 * set tag length and value.
782 if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {
783 err = "TAG_LENGTH_SET_ERROR";
786 err = "TAG_SET_ERROR";
789 if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {
790 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
796 err = "INVALID_KEY_LENGTH";
797 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len))
799 err = "KEY_SET_ERROR";
800 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1))
803 if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {
804 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
805 cdat->tag_len, cdat->tag)) {
806 err = "TAG_SET_ERROR";
811 if (cdat->aead == EVP_CIPH_CCM_MODE) {
812 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
813 err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
818 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, cdat->aad, cdat->aad_len)) {
819 err = "AAD_SET_ERROR";
823 EVP_CIPHER_CTX_set_padding(ctx, 0);
824 err = "CIPHERUPDATE_ERROR";
825 if (!EVP_CipherUpdate(ctx, tmp, &tmplen, in, in_len))
827 if (cdat->aead == EVP_CIPH_CCM_MODE)
830 err = "CIPHERFINAL_ERROR";
831 if (!EVP_CipherFinal_ex(ctx, tmp + tmplen, &tmpflen))
834 err = "LENGTH_MISMATCH";
835 if (out_len != (size_t)(tmplen + tmpflen))
837 err = "VALUE_MISMATCH";
838 if (check_output(t, out, tmp, out_len))
840 if (enc && cdat->aead) {
841 unsigned char rtag[16];
842 if (cdat->tag_len > sizeof(rtag)) {
843 err = "TAG_LENGTH_INTERNAL_ERROR";
846 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
847 cdat->tag_len, rtag)) {
848 err = "TAG_RETRIEVE_ERROR";
851 if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {
852 err = "TAG_VALUE_MISMATCH";
859 EVP_CIPHER_CTX_free(ctx);
864 static int cipher_test_run(struct evp_test *t)
866 struct cipher_data *cdat = t->data;
872 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
873 /* IV is optional and usually omitted in wrap mode */
874 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
879 if (cdat->aead && !cdat->tag) {
884 rv = cipher_test_enc(t, 1);
885 /* Not fatal errors: return */
892 if (cdat->enc != 1) {
893 rv = cipher_test_enc(t, 0);
894 /* Not fatal errors: return */
904 static const struct evp_test_method cipher_test_method = {
915 /* Algorithm string for this MAC */
921 unsigned char *input;
923 /* Expected output */
924 unsigned char *output;
928 static int mac_test_init(struct evp_test *t, const char *alg)
931 struct mac_data *mdat;
932 if (strcmp(alg, "HMAC") == 0)
933 type = EVP_PKEY_HMAC;
934 else if (strcmp(alg, "CMAC") == 0)
935 type = EVP_PKEY_CMAC;
939 mdat = OPENSSL_malloc(sizeof(*mdat));
949 static void mac_test_cleanup(struct evp_test *t)
951 struct mac_data *mdat = t->data;
952 test_free(mdat->alg);
953 test_free(mdat->key);
954 test_free(mdat->input);
955 test_free(mdat->output);
958 static int mac_test_parse(struct evp_test *t,
959 const char *keyword, const char *value)
961 struct mac_data *mdata = t->data;
962 if (strcmp(keyword, "Key") == 0)
963 return test_bin(value, &mdata->key, &mdata->key_len);
964 if (strcmp(keyword, "Algorithm") == 0) {
965 mdata->alg = BUF_strdup(value);
970 if (strcmp(keyword, "Input") == 0)
971 return test_bin(value, &mdata->input, &mdata->input_len);
972 if (strcmp(keyword, "Output") == 0)
973 return test_bin(value, &mdata->output, &mdata->output_len);
977 static int mac_test_run(struct evp_test *t)
979 struct mac_data *mdata = t->data;
980 const char *err = "INTERNAL_ERROR";
981 EVP_MD_CTX *mctx = NULL;
982 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
983 EVP_PKEY *key = NULL;
984 const EVP_MD *md = NULL;
985 unsigned char *mac = NULL;
988 err = "MAC_PKEY_CTX_ERROR";
989 genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL);
993 err = "MAC_KEYGEN_INIT_ERROR";
994 if (EVP_PKEY_keygen_init(genctx) <= 0)
996 if (mdata->type == EVP_PKEY_CMAC) {
997 err = "MAC_ALGORITHM_SET_ERROR";
998 if (EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0)
1002 err = "MAC_KEY_SET_ERROR";
1003 if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0)
1006 err = "MAC_KEY_GENERATE_ERROR";
1007 if (EVP_PKEY_keygen(genctx, &key) <= 0)
1009 if (mdata->type == EVP_PKEY_HMAC) {
1010 err = "MAC_ALGORITHM_SET_ERROR";
1011 md = EVP_get_digestbyname(mdata->alg);
1015 mctx = EVP_MD_CTX_create();
1018 err = "DIGESTSIGNINIT_ERROR";
1019 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key))
1022 err = "DIGESTSIGNUPDATE_ERROR";
1023 if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len))
1025 err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1026 if (!EVP_DigestSignFinal(mctx, NULL, &mac_len))
1028 mac = OPENSSL_malloc(mac_len);
1030 fprintf(stderr, "Error allocating mac buffer!\n");
1033 if (!EVP_DigestSignFinal(mctx, mac, &mac_len))
1035 err = "MAC_LENGTH_MISMATCH";
1036 if (mac_len != mdata->output_len)
1038 err = "MAC_MISMATCH";
1039 if (check_output(t, mdata->output, mac, mac_len))
1044 EVP_MD_CTX_destroy(mctx);
1046 EVP_PKEY_CTX_free(genctx);
1052 static const struct evp_test_method mac_test_method = {
1061 * Public key operations. These are all very similar and can share
1062 * a lot of common code.
1066 /* Context for this operation */
1068 /* Key operation to perform */
1069 int (*keyop) (EVP_PKEY_CTX *ctx,
1070 unsigned char *sig, size_t *siglen,
1071 const unsigned char *tbs, size_t tbslen);
1073 unsigned char *input;
1075 /* Expected output */
1076 unsigned char *output;
1081 * Perform public key operation setup: lookup key, allocated ctx and call
1082 * the appropriate initialisation function
1084 static int pkey_test_init(struct evp_test *t, const char *name,
1086 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1087 int (*keyop) (EVP_PKEY_CTX *ctx,
1088 unsigned char *sig, size_t *siglen,
1089 const unsigned char *tbs,
1093 struct pkey_data *kdata;
1094 EVP_PKEY *pkey = NULL;
1097 rv = find_key(&pkey, name, t->public);
1099 rv = find_key(&pkey, name, t->private);
1107 kdata = OPENSSL_malloc(sizeof(*kdata));
1109 EVP_PKEY_free(pkey);
1113 kdata->input = NULL;
1114 kdata->output = NULL;
1115 kdata->keyop = keyop;
1117 kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL);
1120 if (keyopinit(kdata->ctx) <= 0)
1125 static void pkey_test_cleanup(struct evp_test *t)
1127 struct pkey_data *kdata = t->data;
1129 OPENSSL_free(kdata->input);
1130 OPENSSL_free(kdata->output);
1131 EVP_PKEY_CTX_free(kdata->ctx);
1134 static int pkey_test_parse(struct evp_test *t,
1135 const char *keyword, const char *value)
1137 struct pkey_data *kdata = t->data;
1138 if (strcmp(keyword, "Input") == 0)
1139 return test_bin(value, &kdata->input, &kdata->input_len);
1140 if (strcmp(keyword, "Output") == 0)
1141 return test_bin(value, &kdata->output, &kdata->output_len);
1142 if (strcmp(keyword, "Ctrl") == 0) {
1143 char *p = strchr(value, ':');
1146 if (EVP_PKEY_CTX_ctrl_str(kdata->ctx, value, p) <= 0)
1153 static int pkey_test_run(struct evp_test *t)
1155 struct pkey_data *kdata = t->data;
1156 unsigned char *out = NULL;
1158 const char *err = "KEYOP_LENGTH_ERROR";
1159 if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1160 kdata->input_len) <= 0)
1162 out = OPENSSL_malloc(out_len);
1164 fprintf(stderr, "Error allocating output buffer!\n");
1167 err = "KEYOP_ERROR";
1169 (kdata->ctx, out, &out_len, kdata->input, kdata->input_len) <= 0)
1171 err = "KEYOP_LENGTH_MISMATCH";
1172 if (out_len != kdata->output_len)
1174 err = "KEYOP_MISMATCH";
1175 if (check_output(t, kdata->output, out, out_len))
1184 static int sign_test_init(struct evp_test *t, const char *name)
1186 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1189 static const struct evp_test_method psign_test_method = {
1197 static int verify_recover_test_init(struct evp_test *t, const char *name)
1199 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1200 EVP_PKEY_verify_recover);
1203 static const struct evp_test_method pverify_recover_test_method = {
1205 verify_recover_test_init,
1211 static int decrypt_test_init(struct evp_test *t, const char *name)
1213 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1217 static const struct evp_test_method pdecrypt_test_method = {
1225 static int verify_test_init(struct evp_test *t, const char *name)
1227 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1230 static int verify_test_run(struct evp_test *t)
1232 struct pkey_data *kdata = t->data;
1233 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1234 kdata->input, kdata->input_len) <= 0)
1235 t->err = "VERIFY_ERROR";
1239 static const struct evp_test_method pverify_test_method = {