3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
6 /* ====================================================================
7 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
59 #include <openssl/evp.h>
60 #include <openssl/pem.h>
61 #include <openssl/err.h>
62 #include <openssl/x509v3.h>
64 /* Remove spaces from beginning and end of a string */
66 static void remove_space(char **pval)
68 unsigned char *p = (unsigned char *)*pval;
75 p = p + strlen(*pval) - 1;
77 /* Remove trailing space */
83 * Given a line of the form:
84 * name = value # comment
85 * extract name and value. NB: modifies passed buffer.
88 static int parse_line(char **pkw, char **pval, char *linebuf)
92 p = linebuf + strlen(linebuf) - 1;
95 fprintf(stderr, "FATAL: missing EOL\n");
101 p = strchr(linebuf, '#');
106 /* Look for = sign */
107 p = strchr(linebuf, '=');
118 /* Remove spaces from keyword and value */
125 /* For a hex string "value" convert to a binary allocated buffer */
126 static int test_bin(const char *value, unsigned char **buf, size_t *buflen)
130 /* Don't return NULL for zero length buffer */
131 *buf = OPENSSL_malloc(1);
138 /* Check for string literal */
139 if (value[0] == '"') {
142 vlen = strlen(value);
143 if (value[vlen - 1] != '"')
146 *buf = BUF_memdup(value, vlen);
150 *buf = string_to_hex(value, &len);
152 fprintf(stderr, "Value=%s\n", value);
153 ERR_print_errors_fp(stderr);
156 /* Size of input buffer means we'll never overflow */
161 /* Structure holding test information */
163 /* file being read */
165 /* List of public and private keys */
166 struct key_list *private;
167 struct key_list *public;
168 /* method for this test */
169 const struct evp_test_method *meth;
170 /* current line being processed */
172 /* start line of current test */
173 unsigned int start_line;
174 /* Error string for test */
176 /* Expected error value of test */
178 /* Number of tests */
182 /* Number of tests skipped */
184 /* If output mismatch expected and got value */
185 unsigned char *out_got;
186 unsigned char *out_expected;
188 /* test specific data */
190 /* Current test should be skipped */
197 struct key_list *next;
200 /* Test method structure */
201 struct evp_test_method {
202 /* Name of test as it appears in file */
204 /* Initialise test for "alg" */
205 int (*init) (struct evp_test * t, const char *alg);
206 /* Clean up method */
207 void (*cleanup) (struct evp_test * t);
208 /* Test specific name value pair processing */
209 int (*parse) (struct evp_test * t, const char *name, const char *value);
210 /* Run the test itself */
211 int (*run_test) (struct evp_test * t);
214 static const struct evp_test_method digest_test_method, cipher_test_method;
215 static const struct evp_test_method mac_test_method;
216 static const struct evp_test_method psign_test_method, pverify_test_method;
217 static const struct evp_test_method pdecrypt_test_method;
218 static const struct evp_test_method pverify_recover_test_method;
220 static const struct evp_test_method *evp_test_list[] = {
225 &pverify_test_method,
226 &pdecrypt_test_method,
227 &pverify_recover_test_method,
231 static const struct evp_test_method *evp_find_test(const char *name)
233 const struct evp_test_method **tt;
234 for (tt = evp_test_list; *tt; tt++) {
235 if (!strcmp(name, (*tt)->name))
241 static void hex_print(const char *name, const unsigned char *buf, size_t len)
244 fprintf(stderr, "%s ", name);
245 for (i = 0; i < len; i++)
246 fprintf(stderr, "%02X", buf[i]);
250 static void free_expected(struct evp_test *t)
252 OPENSSL_free(t->expected_err);
253 t->expected_err = NULL;
254 if (t->out_expected) {
255 OPENSSL_free(t->out_expected);
256 OPENSSL_free(t->out_got);
257 t->out_expected = NULL;
262 static void print_expected(struct evp_test *t)
264 if (t->out_expected == NULL)
266 hex_print("Expected:", t->out_expected, t->out_len);
267 hex_print("Got: ", t->out_got, t->out_len);
271 static int check_test_error(struct evp_test *t)
273 if (!t->err && !t->expected_err)
275 if (t->err && !t->expected_err) {
276 fprintf(stderr, "Test line %d: unexpected error %s\n",
277 t->start_line, t->err);
281 if (!t->err && t->expected_err) {
282 fprintf(stderr, "Test line %d: succeeded expecting %s\n",
283 t->start_line, t->expected_err);
286 if (!strcmp(t->err, t->expected_err))
289 fprintf(stderr, "Test line %d: expecting %s got %s\n",
290 t->start_line, t->expected_err, t->err);
294 /* Setup a new test, run any existing test */
296 static int setup_test(struct evp_test *t, const struct evp_test_method *tmeth)
298 /* If we already have a test set up run it */
307 if (t->meth->run_test(t) != 1) {
308 fprintf(stderr, "%s test error line %d\n",
309 t->meth->name, t->start_line);
312 if (!check_test_error(t)) {
314 ERR_print_errors_fp(stderr);
319 OPENSSL_free(t->data);
321 OPENSSL_free(t->expected_err);
322 t->expected_err = NULL;
329 static int find_key(EVP_PKEY **ppk, const char *name, struct key_list *lst)
331 for (; lst; lst = lst->next) {
332 if (!strcmp(lst->name, name)) {
341 static void free_key_list(struct key_list *lst)
343 while (lst != NULL) {
344 struct key_list *ltmp;
345 EVP_PKEY_free(lst->key);
346 OPENSSL_free(lst->name);
353 static int check_unsupported()
355 long err = ERR_peek_error();
356 if (ERR_GET_LIB(err) == ERR_LIB_EVP
357 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
364 static int process_test(struct evp_test *t, char *buf, int verbose)
366 char *keyword, *value;
367 int rv = 0, add_key = 0;
369 struct key_list **lst, *key;
371 const struct evp_test_method *tmeth;
374 if (!parse_line(&keyword, &value, buf))
376 if (!strcmp(keyword, "PrivateKey")) {
377 save_pos = ftell(t->in);
378 pk = PEM_read_PrivateKey(t->in, NULL, 0, NULL);
379 if (pk == NULL && !check_unsupported()) {
380 fprintf(stderr, "Error reading private key %s\n", value);
381 ERR_print_errors_fp(stderr);
387 if (!strcmp(keyword, "PublicKey")) {
388 save_pos = ftell(t->in);
389 pk = PEM_read_PUBKEY(t->in, NULL, 0, NULL);
390 if (pk == NULL && !check_unsupported()) {
391 fprintf(stderr, "Error reading public key %s\n", value);
392 ERR_print_errors_fp(stderr);
398 /* If we have a key add to list */
401 if (find_key(NULL, value, *lst)) {
402 fprintf(stderr, "Duplicate key %s\n", value);
405 key = OPENSSL_malloc(sizeof(struct key_list));
408 key->name = BUF_strdup(value);
412 /* Rewind input, read to end and update line numbers */
413 fseek(t->in, save_pos, SEEK_SET);
414 while (fgets(tmpbuf, sizeof(tmpbuf), t->in)) {
416 if (!strncmp(tmpbuf, "-----END", 8))
419 fprintf(stderr, "Can't find key end\n");
423 /* See if keyword corresponds to a test start */
424 tmeth = evp_find_test(keyword);
426 if (!setup_test(t, tmeth))
428 t->start_line = t->line;
430 if (!tmeth->init(t, value)) {
431 fprintf(stderr, "Unknown %s: %s\n", keyword, value);
435 } else if (t->skip) {
437 } else if (!strcmp(keyword, "Result")) {
438 if (t->expected_err) {
439 fprintf(stderr, "Line %d: multiple result lines\n", t->line);
442 t->expected_err = BUF_strdup(value);
443 if (!t->expected_err)
446 /* Must be test specific line: try to parse it */
448 rv = t->meth->parse(t, keyword, value);
451 fprintf(stderr, "line %d: unexpected keyword %s\n",
455 fprintf(stderr, "line %d: error processing keyword %s\n",
463 static int check_output(struct evp_test *t, const unsigned char *expected,
464 const unsigned char *got, size_t len)
466 if (!memcmp(expected, got, len))
468 t->out_expected = BUF_memdup(expected, len);
469 t->out_got = BUF_memdup(got, len);
471 if (t->out_expected == NULL || t->out_got == NULL) {
472 fprintf(stderr, "Memory allocation error!\n");
478 int main(int argc, char **argv)
485 fprintf(stderr, "usage: evp_test testfile.txt\n");
489 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
491 ERR_load_crypto_strings();
492 OpenSSL_add_all_algorithms();
494 memset(&t, 0, sizeof(t));
503 t.out_expected = NULL;
506 in = fopen(argv[1], "r");
508 while (fgets(buf, sizeof(buf), in)) {
510 if (!process_test(&t, buf, 0))
513 /* Run any final test we have */
514 if (!setup_test(&t, NULL))
516 fprintf(stderr, "%d tests completed with %d errors, %d skipped\n",
517 t.ntests, t.errors, t.nskip);
518 free_key_list(t.public);
519 free_key_list(t.private);
522 CRYPTO_cleanup_all_ex_data();
523 ERR_remove_thread_state(NULL);
525 CRYPTO_mem_leaks_fp(stderr);
531 static void test_free(void *d)
536 /* Message digest tests */
539 /* Digest this test is for */
540 const EVP_MD *digest;
541 /* Input to digest */
542 unsigned char *input;
544 /* Repeat count for input */
546 /* Expected output */
547 unsigned char *output;
551 static int digest_test_init(struct evp_test *t, const char *alg)
553 const EVP_MD *digest;
554 struct digest_data *mdat = t->data;
555 digest = EVP_get_digestbyname(alg);
557 /* If alg has an OID assume disabled algorithm */
558 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
564 mdat = OPENSSL_malloc(sizeof(struct digest_data));
565 mdat->digest = digest;
573 static void digest_test_cleanup(struct evp_test *t)
575 struct digest_data *mdat = t->data;
576 test_free(mdat->input);
577 test_free(mdat->output);
580 static int digest_test_parse(struct evp_test *t,
581 const char *keyword, const char *value)
583 struct digest_data *mdata = t->data;
584 if (!strcmp(keyword, "Input"))
585 return test_bin(value, &mdata->input, &mdata->input_len);
586 if (!strcmp(keyword, "Output"))
587 return test_bin(value, &mdata->output, &mdata->output_len);
588 if (!strcmp(keyword, "Count")) {
589 long nrpt = atoi(value);
592 mdata->nrpt = (size_t)nrpt;
598 static int digest_test_run(struct evp_test *t)
600 struct digest_data *mdata = t->data;
602 const char *err = "INTERNAL_ERROR";
604 unsigned char md[EVP_MAX_MD_SIZE];
606 mctx = EVP_MD_CTX_create();
609 err = "DIGESTINIT_ERROR";
610 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))
612 err = "DIGESTUPDATE_ERROR";
613 for (i = 0; i < mdata->nrpt; i++) {
614 if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))
617 err = "DIGESTFINAL_ERROR";
618 if (!EVP_DigestFinal(mctx, md, &md_len))
620 err = "DIGEST_LENGTH_MISMATCH";
621 if (md_len != mdata->output_len)
623 err = "DIGEST_MISMATCH";
624 if (check_output(t, mdata->output, md, md_len))
629 EVP_MD_CTX_destroy(mctx);
634 static const struct evp_test_method digest_test_method = {
644 const EVP_CIPHER *cipher;
646 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
652 unsigned char *plaintext;
653 size_t plaintext_len;
654 unsigned char *ciphertext;
655 size_t ciphertext_len;
663 static int cipher_test_init(struct evp_test *t, const char *alg)
665 const EVP_CIPHER *cipher;
666 struct cipher_data *cdat = t->data;
667 cipher = EVP_get_cipherbyname(alg);
669 /* If alg has an OID assume disabled algorithm */
670 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
676 cdat = OPENSSL_malloc(sizeof(struct cipher_data));
677 cdat->cipher = cipher;
681 cdat->ciphertext = NULL;
682 cdat->plaintext = NULL;
686 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
687 || EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE
688 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
689 cdat->aead = EVP_CIPHER_mode(cipher);
696 static void cipher_test_cleanup(struct evp_test *t)
698 struct cipher_data *cdat = t->data;
699 test_free(cdat->key);
701 test_free(cdat->ciphertext);
702 test_free(cdat->plaintext);
703 test_free(cdat->aad);
704 test_free(cdat->tag);
707 static int cipher_test_parse(struct evp_test *t, const char *keyword,
710 struct cipher_data *cdat = t->data;
711 if (!strcmp(keyword, "Key"))
712 return test_bin(value, &cdat->key, &cdat->key_len);
713 if (!strcmp(keyword, "IV"))
714 return test_bin(value, &cdat->iv, &cdat->iv_len);
715 if (!strcmp(keyword, "Plaintext"))
716 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
717 if (!strcmp(keyword, "Ciphertext"))
718 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
720 if (!strcmp(keyword, "AAD"))
721 return test_bin(value, &cdat->aad, &cdat->aad_len);
722 if (!strcmp(keyword, "Tag"))
723 return test_bin(value, &cdat->tag, &cdat->tag_len);
726 if (!strcmp(keyword, "Operation")) {
727 if (!strcmp(value, "ENCRYPT"))
729 else if (!strcmp(value, "DECRYPT"))
738 static int cipher_test_enc(struct evp_test *t, int enc)
740 struct cipher_data *cdat = t->data;
741 unsigned char *in, *out, *tmp = NULL;
742 size_t in_len, out_len;
744 EVP_CIPHER_CTX *ctx = NULL;
746 err = "INTERNAL_ERROR";
747 ctx = EVP_CIPHER_CTX_new();
750 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
752 in = cdat->plaintext;
753 in_len = cdat->plaintext_len;
754 out = cdat->ciphertext;
755 out_len = cdat->ciphertext_len;
757 in = cdat->ciphertext;
758 in_len = cdat->ciphertext_len;
759 out = cdat->plaintext;
760 out_len = cdat->plaintext_len;
762 tmp = OPENSSL_malloc(in_len + 2 * EVP_MAX_BLOCK_LENGTH);
765 err = "CIPHERINIT_ERROR";
766 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
768 err = "INVALID_IV_LENGTH";
771 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
774 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
780 * If encrypting or OCB just set tag length initially, otherwise
781 * set tag length and value.
783 if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {
784 err = "TAG_LENGTH_SET_ERROR";
787 err = "TAG_SET_ERROR";
790 if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {
791 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
797 err = "INVALID_KEY_LENGTH";
798 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len))
800 err = "KEY_SET_ERROR";
801 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1))
804 if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {
805 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
806 cdat->tag_len, cdat->tag)) {
807 err = "TAG_SET_ERROR";
812 if (cdat->aead == EVP_CIPH_CCM_MODE) {
813 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
814 err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
819 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, cdat->aad, cdat->aad_len)) {
820 err = "AAD_SET_ERROR";
824 EVP_CIPHER_CTX_set_padding(ctx, 0);
825 err = "CIPHERUPDATE_ERROR";
826 if (!EVP_CipherUpdate(ctx, tmp, &tmplen, in, in_len))
828 if (cdat->aead == EVP_CIPH_CCM_MODE)
831 err = "CIPHERFINAL_ERROR";
832 if (!EVP_CipherFinal_ex(ctx, tmp + tmplen, &tmpflen))
835 err = "LENGTH_MISMATCH";
836 if (out_len != (size_t)(tmplen + tmpflen))
838 err = "VALUE_MISMATCH";
839 if (check_output(t, out, tmp, out_len))
841 if (enc && cdat->aead) {
842 unsigned char rtag[16];
843 if (cdat->tag_len > sizeof(rtag)) {
844 err = "TAG_LENGTH_INTERNAL_ERROR";
847 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
848 cdat->tag_len, rtag)) {
849 err = "TAG_RETRIEVE_ERROR";
852 if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {
853 err = "TAG_VALUE_MISMATCH";
860 EVP_CIPHER_CTX_free(ctx);
865 static int cipher_test_run(struct evp_test *t)
867 struct cipher_data *cdat = t->data;
873 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
874 /* IV is optional and usually omitted in wrap mode */
875 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
880 if (cdat->aead && !cdat->tag) {
885 rv = cipher_test_enc(t, 1);
886 /* Not fatal errors: return */
893 if (cdat->enc != 1) {
894 rv = cipher_test_enc(t, 0);
895 /* Not fatal errors: return */
905 static const struct evp_test_method cipher_test_method = {
916 /* Algorithm string for this MAC */
922 unsigned char *input;
924 /* Expected output */
925 unsigned char *output;
929 static int mac_test_init(struct evp_test *t, const char *alg)
932 struct mac_data *mdat;
933 if (!strcmp(alg, "HMAC"))
934 type = EVP_PKEY_HMAC;
935 else if (!strcmp(alg, "CMAC"))
936 type = EVP_PKEY_CMAC;
940 mdat = OPENSSL_malloc(sizeof(struct mac_data));
950 static void mac_test_cleanup(struct evp_test *t)
952 struct mac_data *mdat = t->data;
953 test_free(mdat->alg);
954 test_free(mdat->key);
955 test_free(mdat->input);
956 test_free(mdat->output);
959 static int mac_test_parse(struct evp_test *t,
960 const char *keyword, const char *value)
962 struct mac_data *mdata = t->data;
963 if (!strcmp(keyword, "Key"))
964 return test_bin(value, &mdata->key, &mdata->key_len);
965 if (!strcmp(keyword, "Algorithm")) {
966 mdata->alg = BUF_strdup(value);
971 if (!strcmp(keyword, "Input"))
972 return test_bin(value, &mdata->input, &mdata->input_len);
973 if (!strcmp(keyword, "Output"))
974 return test_bin(value, &mdata->output, &mdata->output_len);
978 static int mac_test_run(struct evp_test *t)
980 struct mac_data *mdata = t->data;
981 const char *err = "INTERNAL_ERROR";
982 EVP_MD_CTX *mctx = NULL;
983 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
984 EVP_PKEY *key = NULL;
985 const EVP_MD *md = NULL;
986 unsigned char *mac = NULL;
989 err = "MAC_PKEY_CTX_ERROR";
990 genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL);
994 err = "MAC_KEYGEN_INIT_ERROR";
995 if (EVP_PKEY_keygen_init(genctx) <= 0)
997 if (mdata->type == EVP_PKEY_CMAC) {
998 err = "MAC_ALGORITHM_SET_ERROR";
999 if (EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0)
1003 err = "MAC_KEY_SET_ERROR";
1004 if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0)
1007 err = "MAC_KEY_GENERATE_ERROR";
1008 if (EVP_PKEY_keygen(genctx, &key) <= 0)
1010 if (mdata->type == EVP_PKEY_HMAC) {
1011 err = "MAC_ALGORITHM_SET_ERROR";
1012 md = EVP_get_digestbyname(mdata->alg);
1016 mctx = EVP_MD_CTX_create();
1019 err = "DIGESTSIGNINIT_ERROR";
1020 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key))
1023 err = "DIGESTSIGNUPDATE_ERROR";
1024 if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len))
1026 err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1027 if (!EVP_DigestSignFinal(mctx, NULL, &mac_len))
1029 mac = OPENSSL_malloc(mac_len);
1031 fprintf(stderr, "Error allocating mac buffer!\n");
1034 if (!EVP_DigestSignFinal(mctx, mac, &mac_len))
1036 err = "MAC_LENGTH_MISMATCH";
1037 if (mac_len != mdata->output_len)
1039 err = "MAC_MISMATCH";
1040 if (check_output(t, mdata->output, mac, mac_len))
1045 EVP_MD_CTX_destroy(mctx);
1047 EVP_PKEY_CTX_free(genctx);
1053 static const struct evp_test_method mac_test_method = {
1062 * Public key operations. These are all very similar and can share
1063 * a lot of common code.
1067 /* Context for this operation */
1069 /* Key operation to perform */
1070 int (*keyop) (EVP_PKEY_CTX *ctx,
1071 unsigned char *sig, size_t *siglen,
1072 const unsigned char *tbs, size_t tbslen);
1074 unsigned char *input;
1076 /* Expected output */
1077 unsigned char *output;
1082 * Perform public key operation setup: lookup key, allocated ctx and call
1083 * the appropriate initialisation function
1085 static int pkey_test_init(struct evp_test *t, const char *name,
1087 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1088 int (*keyop) (EVP_PKEY_CTX *ctx,
1089 unsigned char *sig, size_t *siglen,
1090 const unsigned char *tbs,
1094 struct pkey_data *kdata;
1095 EVP_PKEY *pkey = NULL;
1098 rv = find_key(&pkey, name, t->public);
1100 rv = find_key(&pkey, name, t->private);
1108 kdata = OPENSSL_malloc(sizeof(struct pkey_data));
1110 EVP_PKEY_free(pkey);
1114 kdata->input = NULL;
1115 kdata->output = NULL;
1116 kdata->keyop = keyop;
1118 kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL);
1121 if (keyopinit(kdata->ctx) <= 0)
1126 static void pkey_test_cleanup(struct evp_test *t)
1128 struct pkey_data *kdata = t->data;
1130 OPENSSL_free(kdata->input);
1131 OPENSSL_free(kdata->output);
1132 EVP_PKEY_CTX_free(kdata->ctx);
1135 static int pkey_test_parse(struct evp_test *t,
1136 const char *keyword, const char *value)
1138 struct pkey_data *kdata = t->data;
1139 if (!strcmp(keyword, "Input"))
1140 return test_bin(value, &kdata->input, &kdata->input_len);
1141 if (!strcmp(keyword, "Output"))
1142 return test_bin(value, &kdata->output, &kdata->output_len);
1143 if (!strcmp(keyword, "Ctrl")) {
1144 char *p = strchr(value, ':');
1147 if (EVP_PKEY_CTX_ctrl_str(kdata->ctx, value, p) <= 0)
1154 static int pkey_test_run(struct evp_test *t)
1156 struct pkey_data *kdata = t->data;
1157 unsigned char *out = NULL;
1159 const char *err = "KEYOP_LENGTH_ERROR";
1160 if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1161 kdata->input_len) <= 0)
1163 out = OPENSSL_malloc(out_len);
1165 fprintf(stderr, "Error allocating output buffer!\n");
1168 err = "KEYOP_ERROR";
1170 (kdata->ctx, out, &out_len, kdata->input, kdata->input_len) <= 0)
1172 err = "KEYOP_LENGTH_MISMATCH";
1173 if (out_len != kdata->output_len)
1175 err = "KEYOP_MISMATCH";
1176 if (check_output(t, kdata->output, out, out_len))
1185 static int sign_test_init(struct evp_test *t, const char *name)
1187 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1190 static const struct evp_test_method psign_test_method = {
1198 static int verify_recover_test_init(struct evp_test *t, const char *name)
1200 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1201 EVP_PKEY_verify_recover);
1204 static const struct evp_test_method pverify_recover_test_method = {
1206 verify_recover_test_init,
1212 static int decrypt_test_init(struct evp_test *t, const char *name)
1214 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1218 static const struct evp_test_method pdecrypt_test_method = {
1226 static int verify_test_init(struct evp_test *t, const char *name)
1228 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1231 static int verify_test_run(struct evp_test *t)
1233 struct pkey_data *kdata = t->data;
1234 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1235 kdata->input, kdata->input_len) <= 0)
1236 t->err = "VERIFY_ERROR";
1240 static const struct evp_test_method pverify_test_method = {