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/err.h>
61 #include <openssl/x509v3.h>
63 /* Remove spaces from beginning and end of a string */
65 static void remove_space(char **pval)
67 unsigned char *p = (unsigned char *)*pval;
74 p = p + strlen(*pval) - 1;
76 /* Remove trailing space */
82 * Given a line of the form:
83 * name = value # comment
84 * extract name and value. NB: modifies passed buffer.
87 static int parse_line(char **pkw, char **pval, char *linebuf)
91 p = linebuf + strlen(linebuf) - 1;
94 fprintf(stderr, "FATAL: missing EOL\n");
100 p = strchr(linebuf, '#');
105 /* Look for = sign */
106 p = strchr(linebuf, '=');
117 /* Remove spaces from keyword and value */
124 /* For a hex string "value" convert to a binary allocated buffer */
125 static int test_bin(const char *value, unsigned char **buf, size_t *buflen)
129 /* Don't return NULL for zero length buffer */
130 *buf = OPENSSL_malloc(1);
137 /* Check for string literal */
138 if (value[0] == '"') {
141 vlen = strlen(value);
142 if (value[vlen - 1] != '"')
145 *buf = BUF_memdup(value, vlen);
149 *buf = string_to_hex(value, &len);
151 fprintf(stderr, "Value=%s\n", value);
152 ERR_print_errors_fp(stderr);
155 /* Size of input buffer means we'll never overflow */
160 /* Structure holding test information */
162 /* method for this test */
163 const struct evp_test_method *meth;
164 /* current line being processed */
166 /* start line of current test */
167 unsigned int start_line;
168 /* Error string for test */
170 /* Expected error value of test */
172 /* Number of tests */
176 /* If output mismatch expected and got value */
177 unsigned char *out_got;
178 unsigned char *out_expected;
180 /* test specific data */
183 /* Test method structure */
184 struct evp_test_method {
185 /* Name of test as it appears in file */
187 /* Initialise test for "alg" */
188 int (*init) (struct evp_test * t, const char *alg);
189 /* Clean up method */
190 void (*cleanup) (struct evp_test * t);
191 /* Test specific name value pair processing */
192 int (*parse) (struct evp_test * t, const char *name, const char *value);
193 /* Run the test itself */
194 int (*run_test) (struct evp_test * t);
197 static const struct evp_test_method digest_test_method, cipher_test_method;
198 static const struct evp_test_method aead_test_method, mac_test_method;
200 static const struct evp_test_method *evp_test_list[] = {
207 static const struct evp_test_method *evp_find_test(const char *name)
209 const struct evp_test_method **tt;
210 for (tt = evp_test_list; *tt; tt++) {
211 if (!strcmp(name, (*tt)->name))
217 static void hex_print(const char *name, const unsigned char *buf, size_t len)
220 fprintf(stderr, "%s ", name);
221 for (i = 0; i < len; i++)
222 fprintf(stderr, "%02X", buf[i]);
226 static void print_expected(struct evp_test *t)
228 if (t->out_expected == NULL)
230 hex_print("Expected:", t->out_expected, t->out_len);
231 hex_print("Got: ", t->out_got, t->out_len);
232 OPENSSL_free(t->out_expected);
233 OPENSSL_free(t->out_got);
234 t->out_expected = NULL;
238 static int check_test_error(struct evp_test *t)
240 if (!t->err && !t->expected_err)
242 if (t->err && !t->expected_err) {
243 fprintf(stderr, "Test line %d: unexpected error %s\n",
244 t->start_line, t->err);
248 if (!t->err && t->expected_err) {
249 fprintf(stderr, "Test line %d: succeeded expecting %s\n",
250 t->start_line, t->expected_err);
253 if (!strcmp(t->err, t->expected_err))
256 fprintf(stderr, "Test line %d: expecting %s got %s\n",
257 t->start_line, t->expected_err, t->err);
261 /* Setup a new test, run any existing test */
263 static int setup_test(struct evp_test *t, const struct evp_test_method *tmeth)
265 /* If we already have a test set up run it */
269 if (t->meth->run_test(t) != 1) {
270 fprintf(stderr, "%s test error line %d\n",
271 t->meth->name, t->start_line);
274 if (!check_test_error(t)) {
276 ERR_print_errors_fp(stderr);
281 /* If new test type free old data */
282 if (tmeth != t->meth && t->data) {
283 OPENSSL_free(t->data);
286 if (t->expected_err) {
287 OPENSSL_free(t->expected_err);
288 t->expected_err = NULL;
295 static int process_test(struct evp_test *t, char *buf, int verbose)
297 char *keyword, *value;
299 const struct evp_test_method *tmeth;
302 if (!parse_line(&keyword, &value, buf))
304 /* See if keyword corresponds to a test start */
305 tmeth = evp_find_test(keyword);
307 if (!setup_test(t, tmeth))
309 t->start_line = t->line;
310 if (!tmeth->init(t, value)) {
311 fprintf(stderr, "Unknown %s: %s\n", keyword, value);
315 } else if (!strcmp(keyword, "Result")) {
316 if (t->expected_err) {
317 fprintf(stderr, "Line %d: multiple result lines\n", t->line);
320 t->expected_err = BUF_strdup(value);
321 if (!t->expected_err)
324 /* Must be test specific line: try to parse it */
326 rv = t->meth->parse(t, keyword, value);
329 fprintf(stderr, "line %d: unexpected keyword %s\n",
333 fprintf(stderr, "line %d: error processing keyword %s\n",
341 static int check_output(struct evp_test *t, const unsigned char *expected,
342 const unsigned char *got, size_t len)
344 if (!memcmp(expected, got, len))
346 t->out_expected = BUF_memdup(expected, len);
347 t->out_got = BUF_memdup(got, len);
349 if (t->out_expected == NULL || t->out_got == NULL) {
350 fprintf(stderr, "Memory allocation error!\n");
356 int main(int argc, char **argv)
363 fprintf(stderr, "usage: evp_test testfile.txt\n");
367 ERR_load_crypto_strings();
368 OpenSSL_add_all_algorithms();
375 t.out_expected = NULL;
378 in = fopen(argv[1], "r");
379 while (fgets(buf, sizeof(buf), in)) {
381 if (!process_test(&t, buf, 0))
384 /* Run any final test we have */
385 if (!setup_test(&t, NULL))
387 fprintf(stderr, "%d tests completed with %d errors\n",
395 static void test_free(void *d)
401 /* Message digest tests */
404 /* Digest this test is for */
405 const EVP_MD *digest;
406 /* Input to digest */
407 unsigned char *input;
409 /* Expected output */
410 unsigned char *output;
414 static int digest_test_init(struct evp_test *t, const char *alg)
416 const EVP_MD *digest;
417 struct digest_data *mdat = t->data;
418 digest = EVP_get_digestbyname(alg);
421 mdat = OPENSSL_malloc(sizeof(struct digest_data));
422 mdat->digest = digest;
429 static void digest_test_cleanup(struct evp_test *t)
431 struct digest_data *mdat = t->data;
432 test_free(mdat->input);
433 test_free(mdat->output);
436 static int digest_test_parse(struct evp_test *t,
437 const char *keyword, const char *value)
439 struct digest_data *mdata = t->data;
440 if (!strcmp(keyword, "Input"))
441 return test_bin(value, &mdata->input, &mdata->input_len);
442 if (!strcmp(keyword, "Output"))
443 return test_bin(value, &mdata->output, &mdata->output_len);
447 static int digest_test_run(struct evp_test *t)
449 struct digest_data *mdata = t->data;
450 const char *err = "INTERNAL_ERROR";
452 unsigned char md[EVP_MAX_MD_SIZE];
454 mctx = EVP_MD_CTX_create();
457 err = "DIGESTINIT_ERROR";
458 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))
460 err = "DIGESTUPDATE_ERROR";
461 if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))
463 err = "DIGESTFINAL_ERROR";
464 if (!EVP_DigestFinal(mctx, md, &md_len))
466 err = "DIGEST_LENGTH_MISMATCH";
467 if (md_len != mdata->output_len)
469 err = "DIGEST_MISMATCH";
470 if (check_output(t, mdata->output, md, md_len))
475 EVP_MD_CTX_destroy(mctx);
480 static const struct evp_test_method digest_test_method = {
490 const EVP_CIPHER *cipher;
492 /* Set to EVP_CIPH_GCM_MODE or EVP_CIPH_CCM_MODE if AEAD */
498 unsigned char *plaintext;
499 size_t plaintext_len;
500 unsigned char *ciphertext;
501 size_t ciphertext_len;
509 static int cipher_test_init(struct evp_test *t, const char *alg)
511 const EVP_CIPHER *cipher;
512 struct cipher_data *cdat = t->data;
513 cipher = EVP_get_cipherbyname(alg);
516 cdat = OPENSSL_malloc(sizeof(struct cipher_data));
517 cdat->cipher = cipher;
521 cdat->ciphertext = NULL;
522 cdat->plaintext = NULL;
526 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
527 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
528 cdat->aead = EVP_CIPHER_mode(cipher);
535 static void cipher_test_cleanup(struct evp_test *t)
537 struct cipher_data *cdat = t->data;
538 test_free(cdat->key);
540 test_free(cdat->ciphertext);
541 test_free(cdat->plaintext);
542 test_free(cdat->aad);
543 test_free(cdat->tag);
546 static int cipher_test_parse(struct evp_test *t, const char *keyword,
549 struct cipher_data *cdat = t->data;
550 if (!strcmp(keyword, "Key"))
551 return test_bin(value, &cdat->key, &cdat->key_len);
552 if (!strcmp(keyword, "IV"))
553 return test_bin(value, &cdat->iv, &cdat->iv_len);
554 if (!strcmp(keyword, "Plaintext"))
555 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
556 if (!strcmp(keyword, "Ciphertext"))
557 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
559 if (!strcmp(keyword, "AAD"))
560 return test_bin(value, &cdat->aad, &cdat->aad_len);
561 if (!strcmp(keyword, "Tag"))
562 return test_bin(value, &cdat->tag, &cdat->tag_len);
565 if (!strcmp(keyword, "Operation")) {
566 if (!strcmp(value, "ENCRYPT"))
568 else if (!strcmp(value, "DECRYPT"))
577 static int cipher_test_enc(struct evp_test *t, int enc)
579 struct cipher_data *cdat = t->data;
580 unsigned char *in, *out, *tmp = NULL;
581 size_t in_len, out_len;
583 EVP_CIPHER_CTX *ctx = NULL;
585 err = "INTERNAL_ERROR";
586 ctx = EVP_CIPHER_CTX_new();
589 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
591 in = cdat->plaintext;
592 in_len = cdat->plaintext_len;
593 out = cdat->ciphertext;
594 out_len = cdat->ciphertext_len;
596 in = cdat->ciphertext;
597 in_len = cdat->ciphertext_len;
598 out = cdat->plaintext;
599 out_len = cdat->plaintext_len;
601 tmp = OPENSSL_malloc(in_len + 2 * EVP_MAX_BLOCK_LENGTH);
604 err = "CIPHERINIT_ERROR";
605 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
607 err = "INVALID_IV_LENGTH";
609 if (cdat->aead == EVP_CIPH_GCM_MODE) {
610 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN,
613 } else if (cdat->aead == EVP_CIPH_CCM_MODE) {
614 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN,
617 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
623 * If encrypting just set tag length. If decrypting set
624 * tag length and value.
627 err = "TAG_LENGTH_SET_ERROR";
630 err = "TAG_SET_ERROR";
633 if (cdat->aead == EVP_CIPH_GCM_MODE && tag) {
634 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG,
637 } else if (cdat->aead == EVP_CIPH_CCM_MODE) {
638 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG,
644 err = "INVALID_KEY_LENGTH";
645 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len))
647 err = "KEY_SET_ERROR";
648 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1))
651 if (cdat->aead == EVP_CIPH_CCM_MODE) {
652 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
653 err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
658 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, cdat->aad, cdat->aad_len)) {
659 err = "AAD_SET_ERROR";
663 EVP_CIPHER_CTX_set_padding(ctx, 0);
664 err = "CIPHERUPDATE_ERROR";
665 if (!EVP_CipherUpdate(ctx, tmp, &tmplen, in, in_len))
667 if (cdat->aead == EVP_CIPH_CCM_MODE)
670 err = "CIPHERFINAL_ERROR";
671 if (!EVP_CipherFinal_ex(ctx, tmp + tmplen, &tmpflen))
674 err = "LENGTH_MISMATCH";
675 if (out_len != (size_t)(tmplen + tmpflen))
677 err = "VALUE_MISMATCH";
678 if (check_output(t, out, tmp, out_len))
680 if (enc && cdat->aead) {
681 unsigned char rtag[16];
682 if (cdat->tag_len > sizeof(rtag)) {
683 err = "TAG_LENGTH_INTERNAL_ERROR";
686 /* EVP_CTRL_CCM_GET_TAG and EVP_CTRL_GCM_GET_TAG are equal. */
687 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG,
688 cdat->tag_len, rtag)) {
689 err = "TAG_RETRIEVE_ERROR";
692 if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {
693 err = "TAG_VALUE_MISMATCH";
701 EVP_CIPHER_CTX_free(ctx);
706 static int cipher_test_run(struct evp_test *t)
708 struct cipher_data *cdat = t->data;
714 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
715 /* IV is optional and usually omitted in wrap mode */
716 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
721 if (cdat->aead && !cdat->tag) {
726 rv = cipher_test_enc(t, 1);
727 /* Not fatal errors: return */
734 if (cdat->enc != 1) {
735 rv = cipher_test_enc(t, 0);
736 /* Not fatal errors: return */
746 static const struct evp_test_method cipher_test_method = {
757 /* Algorithm string for this MAC */
763 unsigned char *input;
765 /* Expected output */
766 unsigned char *output;
770 static int mac_test_init(struct evp_test *t, const char *alg)
773 struct mac_data *mdat;
774 if (!strcmp(alg, "HMAC"))
775 type = EVP_PKEY_HMAC;
776 else if (!strcmp(alg, "CMAC"))
777 type = EVP_PKEY_CMAC;
781 mdat = OPENSSL_malloc(sizeof(struct mac_data));
791 static void mac_test_cleanup(struct evp_test *t)
793 struct mac_data *mdat = t->data;
794 test_free(mdat->alg);
795 test_free(mdat->key);
796 test_free(mdat->input);
797 test_free(mdat->output);
800 static int mac_test_parse(struct evp_test *t,
801 const char *keyword, const char *value)
803 struct mac_data *mdata = t->data;
804 if (!strcmp(keyword, "Key"))
805 return test_bin(value, &mdata->key, &mdata->key_len);
806 if (!strcmp(keyword, "Algorithm")) {
807 mdata->alg = BUF_strdup(value);
812 if (!strcmp(keyword, "Input"))
813 return test_bin(value, &mdata->input, &mdata->input_len);
814 if (!strcmp(keyword, "Output"))
815 return test_bin(value, &mdata->output, &mdata->output_len);
819 static int mac_test_run(struct evp_test *t)
821 struct mac_data *mdata = t->data;
822 const char *err = "INTERNAL_ERROR";
823 EVP_MD_CTX *mctx = NULL;
824 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
825 EVP_PKEY *key = NULL;
826 const EVP_MD *md = NULL;
827 unsigned char *mac = NULL;
830 err = "MAC_PKEY_CTX_ERROR";
831 genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL);
835 err = "MAC_KEYGEN_INIT_ERROR";
836 if (EVP_PKEY_keygen_init(genctx) <= 0)
838 if (mdata->type == EVP_PKEY_CMAC) {
839 err = "MAC_ALGORITHM_SET_ERROR";
840 if (EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0)
844 err = "MAC_KEY_SET_ERROR";
845 if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0)
848 err = "MAC_KEY_GENERATE_ERROR";
849 if (EVP_PKEY_keygen(genctx, &key) <= 0)
851 if (mdata->type == EVP_PKEY_HMAC) {
852 err = "MAC_ALGORITHM_SET_ERROR";
853 md = EVP_get_digestbyname(mdata->alg);
857 mctx = EVP_MD_CTX_create();
860 err = "DIGESTSIGNINIT_ERROR";
861 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key))
864 err = "DIGESTSIGNUPDATE_ERROR";
865 if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len))
867 err = "DIGESTSIGNFINAL_LENGTH_ERROR";
868 if (!EVP_DigestSignFinal(mctx, NULL, &mac_len))
870 mac = OPENSSL_malloc(mac_len);
872 fprintf(stderr, "Error allocating mac buffer!\n");
875 if (!EVP_DigestSignFinal(mctx, mac, &mac_len))
877 err = "MAC_LENGTH_MISMATCH";
878 if (mac_len != mdata->output_len)
880 err = "MAC_MISMATCH";
881 if (check_output(t, mdata->output, mac, mac_len))
886 EVP_MD_CTX_destroy(mctx);
890 EVP_PKEY_CTX_free(genctx);
897 static const struct evp_test_method mac_test_method = {