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>
63 #include "internal/numbers.h"
65 /* Remove spaces from beginning and end of a string */
67 static void remove_space(char **pval)
69 unsigned char *p = (unsigned char *)*pval;
76 p = p + strlen(*pval) - 1;
78 /* Remove trailing space */
84 * Given a line of the form:
85 * name = value # comment
86 * extract name and value. NB: modifies passed buffer.
89 static int parse_line(char **pkw, char **pval, char *linebuf)
93 p = linebuf + strlen(linebuf) - 1;
96 fprintf(stderr, "FATAL: missing EOL\n");
102 p = strchr(linebuf, '#');
107 /* Look for = sign */
108 p = strchr(linebuf, '=');
119 /* Remove spaces from keyword and value */
126 /* For a hex string "value" convert to a binary allocated buffer */
127 static int test_bin(const char *value, unsigned char **buf, size_t *buflen)
131 /* Don't return NULL for zero length buffer */
132 *buf = OPENSSL_malloc(1);
139 /* Check for string literal */
140 if (value[0] == '"') {
143 vlen = strlen(value);
144 if (value[vlen - 1] != '"')
148 *buf = OPENSSL_malloc(1);
153 *buf = BUF_memdup(value, vlen);
160 *buf = string_to_hex(value, &len);
162 fprintf(stderr, "Value=%s\n", value);
163 ERR_print_errors_fp(stderr);
166 /* Size of input buffer means we'll never overflow */
170 /* Parse unsigned decimal 64 bit integer value */
171 static int test_uint64(const char *value, uint64_t *pr)
173 const char *p = value;
175 fprintf(stderr, "Invalid empty integer value\n");
180 if (*pr > UINT64_MAX/10) {
181 fprintf(stderr, "Integer string overflow value=%s\n", value);
185 if (*p < '0' || *p > '9') {
186 fprintf(stderr, "Invalid integer string value=%s\n", value);
195 /* Structure holding test information */
197 /* file being read */
199 /* List of public and private keys */
200 struct key_list *private;
201 struct key_list *public;
202 /* method for this test */
203 const struct evp_test_method *meth;
204 /* current line being processed */
206 /* start line of current test */
207 unsigned int start_line;
208 /* Error string for test */
210 /* Expected error value of test */
212 /* Number of tests */
216 /* Number of tests skipped */
218 /* If output mismatch expected and got value */
219 unsigned char *out_got;
220 unsigned char *out_expected;
222 /* test specific data */
224 /* Current test should be skipped */
231 struct key_list *next;
234 /* Test method structure */
235 struct evp_test_method {
236 /* Name of test as it appears in file */
238 /* Initialise test for "alg" */
239 int (*init) (struct evp_test * t, const char *alg);
240 /* Clean up method */
241 void (*cleanup) (struct evp_test * t);
242 /* Test specific name value pair processing */
243 int (*parse) (struct evp_test * t, const char *name, const char *value);
244 /* Run the test itself */
245 int (*run_test) (struct evp_test * t);
248 static const struct evp_test_method digest_test_method, cipher_test_method;
249 static const struct evp_test_method mac_test_method;
250 static const struct evp_test_method psign_test_method, pverify_test_method;
251 static const struct evp_test_method pdecrypt_test_method;
252 static const struct evp_test_method pverify_recover_test_method;
253 static const struct evp_test_method pbe_test_method;
255 static const struct evp_test_method *evp_test_list[] = {
260 &pverify_test_method,
261 &pdecrypt_test_method,
262 &pverify_recover_test_method,
267 static const struct evp_test_method *evp_find_test(const char *name)
269 const struct evp_test_method **tt;
271 for (tt = evp_test_list; *tt; tt++) {
272 if (strcmp(name, (*tt)->name) == 0)
278 static void hex_print(const char *name, const unsigned char *buf, size_t len)
281 fprintf(stderr, "%s ", name);
282 for (i = 0; i < len; i++)
283 fprintf(stderr, "%02X", buf[i]);
287 static void free_expected(struct evp_test *t)
289 OPENSSL_free(t->expected_err);
290 t->expected_err = NULL;
291 OPENSSL_free(t->out_expected);
292 OPENSSL_free(t->out_got);
293 t->out_expected = NULL;
297 static void print_expected(struct evp_test *t)
299 if (t->out_expected == NULL)
301 hex_print("Expected:", t->out_expected, t->out_len);
302 hex_print("Got: ", t->out_got, t->out_len);
306 static int check_test_error(struct evp_test *t)
308 if (!t->err && !t->expected_err)
310 if (t->err && !t->expected_err) {
311 fprintf(stderr, "Test line %d: unexpected error %s\n",
312 t->start_line, t->err);
316 if (!t->err && t->expected_err) {
317 fprintf(stderr, "Test line %d: succeeded expecting %s\n",
318 t->start_line, t->expected_err);
321 if (strcmp(t->err, t->expected_err) == 0)
324 fprintf(stderr, "Test line %d: expecting %s got %s\n",
325 t->start_line, t->expected_err, t->err);
329 /* Setup a new test, run any existing test */
331 static int setup_test(struct evp_test *t, const struct evp_test_method *tmeth)
333 /* If we already have a test set up run it */
342 if (t->meth->run_test(t) != 1) {
343 fprintf(stderr, "%s test error line %d\n",
344 t->meth->name, t->start_line);
347 if (!check_test_error(t)) {
349 ERR_print_errors_fp(stderr);
354 OPENSSL_free(t->data);
356 OPENSSL_free(t->expected_err);
357 t->expected_err = NULL;
364 static int find_key(EVP_PKEY **ppk, const char *name, struct key_list *lst)
366 for (; lst; lst = lst->next) {
367 if (strcmp(lst->name, name) == 0) {
376 static void free_key_list(struct key_list *lst)
378 while (lst != NULL) {
379 struct key_list *ltmp;
380 EVP_PKEY_free(lst->key);
381 OPENSSL_free(lst->name);
388 static int check_unsupported()
390 long err = ERR_peek_error();
391 if (ERR_GET_LIB(err) == ERR_LIB_EVP
392 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
399 static int process_test(struct evp_test *t, char *buf, int verbose)
401 char *keyword = NULL, *value = NULL;
402 int rv = 0, add_key = 0;
404 struct key_list **lst = NULL, *key = NULL;
406 const struct evp_test_method *tmeth = NULL;
409 if (!parse_line(&keyword, &value, buf))
411 if (strcmp(keyword, "PrivateKey") == 0) {
412 save_pos = ftell(t->in);
413 pk = PEM_read_PrivateKey(t->in, NULL, 0, NULL);
414 if (pk == NULL && !check_unsupported()) {
415 fprintf(stderr, "Error reading private key %s\n", value);
416 ERR_print_errors_fp(stderr);
422 if (strcmp(keyword, "PublicKey") == 0) {
423 save_pos = ftell(t->in);
424 pk = PEM_read_PUBKEY(t->in, NULL, 0, NULL);
425 if (pk == NULL && !check_unsupported()) {
426 fprintf(stderr, "Error reading public key %s\n", value);
427 ERR_print_errors_fp(stderr);
433 /* If we have a key add to list */
436 if (find_key(NULL, value, *lst)) {
437 fprintf(stderr, "Duplicate key %s\n", value);
440 key = OPENSSL_malloc(sizeof(*key));
443 key->name = BUF_strdup(value);
447 /* Rewind input, read to end and update line numbers */
448 fseek(t->in, save_pos, SEEK_SET);
449 while (fgets(tmpbuf, sizeof(tmpbuf), t->in)) {
451 if (strncmp(tmpbuf, "-----END", 8) == 0)
454 fprintf(stderr, "Can't find key end\n");
458 /* See if keyword corresponds to a test start */
459 tmeth = evp_find_test(keyword);
461 if (!setup_test(t, tmeth))
463 t->start_line = t->line;
465 if (!tmeth->init(t, value)) {
466 fprintf(stderr, "Unknown %s: %s\n", keyword, value);
470 } else if (t->skip) {
472 } else if (strcmp(keyword, "Result") == 0) {
473 if (t->expected_err) {
474 fprintf(stderr, "Line %d: multiple result lines\n", t->line);
477 t->expected_err = BUF_strdup(value);
478 if (!t->expected_err)
481 /* Must be test specific line: try to parse it */
483 rv = t->meth->parse(t, keyword, value);
486 fprintf(stderr, "line %d: unexpected keyword %s\n",
490 fprintf(stderr, "line %d: error processing keyword %s\n",
498 static int check_output(struct evp_test *t, const unsigned char *expected,
499 const unsigned char *got, size_t len)
501 if (!memcmp(expected, got, len))
503 t->out_expected = BUF_memdup(expected, len);
504 t->out_got = BUF_memdup(got, len);
506 if (t->out_expected == NULL || t->out_got == NULL) {
507 fprintf(stderr, "Memory allocation error!\n");
513 int main(int argc, char **argv)
520 fprintf(stderr, "usage: evp_test testfile.txt\n");
524 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
526 ERR_load_crypto_strings();
527 OpenSSL_add_all_algorithms();
529 memset(&t, 0, sizeof(t));
538 t.out_expected = NULL;
541 in = fopen(argv[1], "r");
543 while (fgets(buf, sizeof(buf), in)) {
545 if (!process_test(&t, buf, 0))
548 /* Run any final test we have */
549 if (!setup_test(&t, NULL))
551 fprintf(stderr, "%d tests completed with %d errors, %d skipped\n",
552 t.ntests, t.errors, t.nskip);
553 free_key_list(t.public);
554 free_key_list(t.private);
557 CRYPTO_cleanup_all_ex_data();
558 ERR_remove_thread_state(NULL);
560 CRYPTO_mem_leaks_fp(stderr);
566 static void test_free(void *d)
571 /* Message digest tests */
574 /* Digest this test is for */
575 const EVP_MD *digest;
576 /* Input to digest */
577 unsigned char *input;
579 /* Repeat count for input */
581 /* Expected output */
582 unsigned char *output;
586 static int digest_test_init(struct evp_test *t, const char *alg)
588 const EVP_MD *digest;
589 struct digest_data *mdat = t->data;
590 digest = EVP_get_digestbyname(alg);
592 /* If alg has an OID assume disabled algorithm */
593 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
599 mdat = OPENSSL_malloc(sizeof(*mdat));
600 mdat->digest = digest;
608 static void digest_test_cleanup(struct evp_test *t)
610 struct digest_data *mdat = t->data;
611 test_free(mdat->input);
612 test_free(mdat->output);
615 static int digest_test_parse(struct evp_test *t,
616 const char *keyword, const char *value)
618 struct digest_data *mdata = t->data;
619 if (strcmp(keyword, "Input") == 0)
620 return test_bin(value, &mdata->input, &mdata->input_len);
621 if (strcmp(keyword, "Output") == 0)
622 return test_bin(value, &mdata->output, &mdata->output_len);
623 if (strcmp(keyword, "Count") == 0) {
624 long nrpt = atoi(value);
627 mdata->nrpt = (size_t)nrpt;
633 static int digest_test_run(struct evp_test *t)
635 struct digest_data *mdata = t->data;
637 const char *err = "INTERNAL_ERROR";
639 unsigned char md[EVP_MAX_MD_SIZE];
641 mctx = EVP_MD_CTX_create();
644 err = "DIGESTINIT_ERROR";
645 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))
647 err = "DIGESTUPDATE_ERROR";
648 for (i = 0; i < mdata->nrpt; i++) {
649 if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))
652 err = "DIGESTFINAL_ERROR";
653 if (!EVP_DigestFinal(mctx, md, &md_len))
655 err = "DIGEST_LENGTH_MISMATCH";
656 if (md_len != mdata->output_len)
658 err = "DIGEST_MISMATCH";
659 if (check_output(t, mdata->output, md, md_len))
664 EVP_MD_CTX_destroy(mctx);
669 static const struct evp_test_method digest_test_method = {
679 const EVP_CIPHER *cipher;
681 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
687 unsigned char *plaintext;
688 size_t plaintext_len;
689 unsigned char *ciphertext;
690 size_t ciphertext_len;
698 static int cipher_test_init(struct evp_test *t, const char *alg)
700 const EVP_CIPHER *cipher;
701 struct cipher_data *cdat = t->data;
702 cipher = EVP_get_cipherbyname(alg);
704 /* If alg has an OID assume disabled algorithm */
705 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
711 cdat = OPENSSL_malloc(sizeof(*cdat));
712 cdat->cipher = cipher;
716 cdat->ciphertext = NULL;
717 cdat->plaintext = NULL;
721 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
722 || EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE
723 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
724 cdat->aead = EVP_CIPHER_mode(cipher);
731 static void cipher_test_cleanup(struct evp_test *t)
733 struct cipher_data *cdat = t->data;
734 test_free(cdat->key);
736 test_free(cdat->ciphertext);
737 test_free(cdat->plaintext);
738 test_free(cdat->aad);
739 test_free(cdat->tag);
742 static int cipher_test_parse(struct evp_test *t, const char *keyword,
745 struct cipher_data *cdat = t->data;
746 if (strcmp(keyword, "Key") == 0)
747 return test_bin(value, &cdat->key, &cdat->key_len);
748 if (strcmp(keyword, "IV") == 0)
749 return test_bin(value, &cdat->iv, &cdat->iv_len);
750 if (strcmp(keyword, "Plaintext") == 0)
751 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
752 if (strcmp(keyword, "Ciphertext") == 0)
753 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
755 if (strcmp(keyword, "AAD") == 0)
756 return test_bin(value, &cdat->aad, &cdat->aad_len);
757 if (strcmp(keyword, "Tag") == 0)
758 return test_bin(value, &cdat->tag, &cdat->tag_len);
761 if (strcmp(keyword, "Operation") == 0) {
762 if (strcmp(value, "ENCRYPT") == 0)
764 else if (strcmp(value, "DECRYPT") == 0)
773 static int cipher_test_enc(struct evp_test *t, int enc)
775 struct cipher_data *cdat = t->data;
776 unsigned char *in, *out, *tmp = NULL;
777 size_t in_len, out_len;
779 EVP_CIPHER_CTX *ctx = NULL;
781 err = "INTERNAL_ERROR";
782 ctx = EVP_CIPHER_CTX_new();
785 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
787 in = cdat->plaintext;
788 in_len = cdat->plaintext_len;
789 out = cdat->ciphertext;
790 out_len = cdat->ciphertext_len;
792 in = cdat->ciphertext;
793 in_len = cdat->ciphertext_len;
794 out = cdat->plaintext;
795 out_len = cdat->plaintext_len;
797 tmp = OPENSSL_malloc(in_len + 2 * EVP_MAX_BLOCK_LENGTH);
800 err = "CIPHERINIT_ERROR";
801 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
803 err = "INVALID_IV_LENGTH";
806 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
809 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
815 * If encrypting or OCB just set tag length initially, otherwise
816 * set tag length and value.
818 if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {
819 err = "TAG_LENGTH_SET_ERROR";
822 err = "TAG_SET_ERROR";
825 if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {
826 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
832 err = "INVALID_KEY_LENGTH";
833 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len))
835 err = "KEY_SET_ERROR";
836 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1))
839 if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {
840 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
841 cdat->tag_len, cdat->tag)) {
842 err = "TAG_SET_ERROR";
847 if (cdat->aead == EVP_CIPH_CCM_MODE) {
848 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
849 err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
854 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, cdat->aad, cdat->aad_len)) {
855 err = "AAD_SET_ERROR";
859 EVP_CIPHER_CTX_set_padding(ctx, 0);
860 err = "CIPHERUPDATE_ERROR";
861 if (!EVP_CipherUpdate(ctx, tmp, &tmplen, in, in_len))
863 if (cdat->aead == EVP_CIPH_CCM_MODE)
866 err = "CIPHERFINAL_ERROR";
867 if (!EVP_CipherFinal_ex(ctx, tmp + tmplen, &tmpflen))
870 err = "LENGTH_MISMATCH";
871 if (out_len != (size_t)(tmplen + tmpflen))
873 err = "VALUE_MISMATCH";
874 if (check_output(t, out, tmp, out_len))
876 if (enc && cdat->aead) {
877 unsigned char rtag[16];
878 if (cdat->tag_len > sizeof(rtag)) {
879 err = "TAG_LENGTH_INTERNAL_ERROR";
882 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
883 cdat->tag_len, rtag)) {
884 err = "TAG_RETRIEVE_ERROR";
887 if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {
888 err = "TAG_VALUE_MISMATCH";
895 EVP_CIPHER_CTX_free(ctx);
900 static int cipher_test_run(struct evp_test *t)
902 struct cipher_data *cdat = t->data;
908 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
909 /* IV is optional and usually omitted in wrap mode */
910 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
915 if (cdat->aead && !cdat->tag) {
920 rv = cipher_test_enc(t, 1);
921 /* Not fatal errors: return */
928 if (cdat->enc != 1) {
929 rv = cipher_test_enc(t, 0);
930 /* Not fatal errors: return */
940 static const struct evp_test_method cipher_test_method = {
951 /* Algorithm string for this MAC */
957 unsigned char *input;
959 /* Expected output */
960 unsigned char *output;
964 static int mac_test_init(struct evp_test *t, const char *alg)
967 struct mac_data *mdat;
968 if (strcmp(alg, "HMAC") == 0)
969 type = EVP_PKEY_HMAC;
970 else if (strcmp(alg, "CMAC") == 0)
971 type = EVP_PKEY_CMAC;
975 mdat = OPENSSL_malloc(sizeof(*mdat));
985 static void mac_test_cleanup(struct evp_test *t)
987 struct mac_data *mdat = t->data;
988 test_free(mdat->alg);
989 test_free(mdat->key);
990 test_free(mdat->input);
991 test_free(mdat->output);
994 static int mac_test_parse(struct evp_test *t,
995 const char *keyword, const char *value)
997 struct mac_data *mdata = t->data;
998 if (strcmp(keyword, "Key") == 0)
999 return test_bin(value, &mdata->key, &mdata->key_len);
1000 if (strcmp(keyword, "Algorithm") == 0) {
1001 mdata->alg = BUF_strdup(value);
1006 if (strcmp(keyword, "Input") == 0)
1007 return test_bin(value, &mdata->input, &mdata->input_len);
1008 if (strcmp(keyword, "Output") == 0)
1009 return test_bin(value, &mdata->output, &mdata->output_len);
1013 static int mac_test_run(struct evp_test *t)
1015 struct mac_data *mdata = t->data;
1016 const char *err = "INTERNAL_ERROR";
1017 EVP_MD_CTX *mctx = NULL;
1018 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1019 EVP_PKEY *key = NULL;
1020 const EVP_MD *md = NULL;
1021 unsigned char *mac = NULL;
1024 err = "MAC_PKEY_CTX_ERROR";
1025 genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL);
1029 err = "MAC_KEYGEN_INIT_ERROR";
1030 if (EVP_PKEY_keygen_init(genctx) <= 0)
1032 if (mdata->type == EVP_PKEY_CMAC) {
1033 err = "MAC_ALGORITHM_SET_ERROR";
1034 if (EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0)
1038 err = "MAC_KEY_SET_ERROR";
1039 if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0)
1042 err = "MAC_KEY_GENERATE_ERROR";
1043 if (EVP_PKEY_keygen(genctx, &key) <= 0)
1045 if (mdata->type == EVP_PKEY_HMAC) {
1046 err = "MAC_ALGORITHM_SET_ERROR";
1047 md = EVP_get_digestbyname(mdata->alg);
1051 mctx = EVP_MD_CTX_create();
1054 err = "DIGESTSIGNINIT_ERROR";
1055 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key))
1058 err = "DIGESTSIGNUPDATE_ERROR";
1059 if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len))
1061 err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1062 if (!EVP_DigestSignFinal(mctx, NULL, &mac_len))
1064 mac = OPENSSL_malloc(mac_len);
1066 fprintf(stderr, "Error allocating mac buffer!\n");
1069 if (!EVP_DigestSignFinal(mctx, mac, &mac_len))
1071 err = "MAC_LENGTH_MISMATCH";
1072 if (mac_len != mdata->output_len)
1074 err = "MAC_MISMATCH";
1075 if (check_output(t, mdata->output, mac, mac_len))
1080 EVP_MD_CTX_destroy(mctx);
1082 EVP_PKEY_CTX_free(genctx);
1088 static const struct evp_test_method mac_test_method = {
1097 * Public key operations. These are all very similar and can share
1098 * a lot of common code.
1102 /* Context for this operation */
1104 /* Key operation to perform */
1105 int (*keyop) (EVP_PKEY_CTX *ctx,
1106 unsigned char *sig, size_t *siglen,
1107 const unsigned char *tbs, size_t tbslen);
1109 unsigned char *input;
1111 /* Expected output */
1112 unsigned char *output;
1117 * Perform public key operation setup: lookup key, allocated ctx and call
1118 * the appropriate initialisation function
1120 static int pkey_test_init(struct evp_test *t, const char *name,
1122 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1123 int (*keyop) (EVP_PKEY_CTX *ctx,
1124 unsigned char *sig, size_t *siglen,
1125 const unsigned char *tbs,
1129 struct pkey_data *kdata;
1130 EVP_PKEY *pkey = NULL;
1133 rv = find_key(&pkey, name, t->public);
1135 rv = find_key(&pkey, name, t->private);
1143 kdata = OPENSSL_malloc(sizeof(*kdata));
1145 EVP_PKEY_free(pkey);
1149 kdata->input = NULL;
1150 kdata->output = NULL;
1151 kdata->keyop = keyop;
1153 kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL);
1156 if (keyopinit(kdata->ctx) <= 0)
1161 static void pkey_test_cleanup(struct evp_test *t)
1163 struct pkey_data *kdata = t->data;
1165 OPENSSL_free(kdata->input);
1166 OPENSSL_free(kdata->output);
1167 EVP_PKEY_CTX_free(kdata->ctx);
1170 static int pkey_test_parse(struct evp_test *t,
1171 const char *keyword, const char *value)
1173 struct pkey_data *kdata = t->data;
1174 if (strcmp(keyword, "Input") == 0)
1175 return test_bin(value, &kdata->input, &kdata->input_len);
1176 if (strcmp(keyword, "Output") == 0)
1177 return test_bin(value, &kdata->output, &kdata->output_len);
1178 if (strcmp(keyword, "Ctrl") == 0) {
1179 char *p = strchr(value, ':');
1182 if (EVP_PKEY_CTX_ctrl_str(kdata->ctx, value, p) <= 0)
1189 static int pkey_test_run(struct evp_test *t)
1191 struct pkey_data *kdata = t->data;
1192 unsigned char *out = NULL;
1194 const char *err = "KEYOP_LENGTH_ERROR";
1195 if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1196 kdata->input_len) <= 0)
1198 out = OPENSSL_malloc(out_len);
1200 fprintf(stderr, "Error allocating output buffer!\n");
1203 err = "KEYOP_ERROR";
1205 (kdata->ctx, out, &out_len, kdata->input, kdata->input_len) <= 0)
1207 err = "KEYOP_LENGTH_MISMATCH";
1208 if (out_len != kdata->output_len)
1210 err = "KEYOP_MISMATCH";
1211 if (check_output(t, kdata->output, out, out_len))
1220 static int sign_test_init(struct evp_test *t, const char *name)
1222 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1225 static const struct evp_test_method psign_test_method = {
1233 static int verify_recover_test_init(struct evp_test *t, const char *name)
1235 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1236 EVP_PKEY_verify_recover);
1239 static const struct evp_test_method pverify_recover_test_method = {
1241 verify_recover_test_init,
1247 static int decrypt_test_init(struct evp_test *t, const char *name)
1249 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1253 static const struct evp_test_method pdecrypt_test_method = {
1261 static int verify_test_init(struct evp_test *t, const char *name)
1263 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1266 static int verify_test_run(struct evp_test *t)
1268 struct pkey_data *kdata = t->data;
1269 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1270 kdata->input, kdata->input_len) <= 0)
1271 t->err = "VERIFY_ERROR";
1275 static const struct evp_test_method pverify_test_method = {
1285 #define PBE_TYPE_SCRYPT 1
1291 /* scrypt parameters */
1292 uint64_t N, r, p, maxmem;
1295 unsigned char *pass;
1299 unsigned char *salt;
1302 /* Expected output */
1307 static int scrypt_test_parse(struct evp_test *t,
1308 const char *keyword, const char *value)
1310 struct pbe_data *pdata = t->data;
1311 if (strcmp(keyword, "N") == 0)
1312 return test_uint64(value, &pdata->N);
1313 if (strcmp(keyword, "p") == 0)
1314 return test_uint64(value, &pdata->p);
1315 if (strcmp(keyword, "r") == 0)
1316 return test_uint64(value, &pdata->r);
1317 if (strcmp(keyword, "maxmem") == 0)
1318 return test_uint64(value, &pdata->maxmem);
1322 static int scrypt_test_run(struct evp_test *t)
1324 struct pbe_data *pdata = t->data;
1325 const char *err = "INTERNAL_ERROR";
1327 key = OPENSSL_malloc(pdata->key_len);
1330 err = "SCRYPT_ERROR";
1331 if (EVP_PBE_scrypt((const char *)pdata->pass, pdata->pass_len,
1332 pdata->salt, pdata->salt_len,
1333 pdata->N, pdata->r, pdata->p, pdata->maxmem,
1334 key, pdata->key_len) == 0)
1336 err = "KEY_MISMATCH";
1337 if (check_output(t, pdata->key, key, pdata->key_len))
1346 static int pbe_test_init(struct evp_test *t, const char *alg)
1348 struct pbe_data *pdat;
1350 if (strcmp(alg, "scrypt") == 0)
1351 pbe_type = PBE_TYPE_SCRYPT;
1353 fprintf(stderr, "Unknown pbe algorithm %s\n", alg);
1354 pdat = OPENSSL_malloc(sizeof(*pdat));
1355 pdat->pbe_type = pbe_type;
1366 static void pbe_test_cleanup(struct evp_test *t)
1368 struct pbe_data *pdat = t->data;
1369 test_free(pdat->pass);
1370 test_free(pdat->salt);
1371 test_free(pdat->key);
1374 static int pbe_test_parse(struct evp_test *t,
1375 const char *keyword, const char *value)
1377 struct pbe_data *pdata = t->data;
1378 if (strcmp(keyword, "Password") == 0)
1379 return test_bin(value, &pdata->pass, &pdata->pass_len);
1380 if (strcmp(keyword, "Salt") == 0)
1381 return test_bin(value, &pdata->salt, &pdata->salt_len);
1382 if (strcmp(keyword, "Key") == 0)
1383 return test_bin(value, &pdata->key, &pdata->key_len);
1384 if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1385 return scrypt_test_parse(t, keyword, value);
1389 static int pbe_test_run(struct evp_test *t)
1391 struct pbe_data *pdata = t->data;
1392 if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1393 return scrypt_test_run(t);
1397 static const struct evp_test_method pbe_test_method = {