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 *buf = string_to_hex(value, &len);
139 fprintf(stderr, "Value=%s\n", value);
140 ERR_print_errors_fp(stderr);
143 /* Size of input buffer means we'll never overflow */
148 /* Structure holding test information */
150 /* method for this test */
151 const struct evp_test_method *meth;
152 /* current line being processed */
154 /* start line of current test */
155 unsigned int start_line;
156 /* Error string for test */
158 /* Expected error value of test */
160 /* Number of tests */
164 /* If output mismatch expected and got value */
165 unsigned char *out_got;
166 unsigned char *out_expected;
168 /* test specific data */
171 /* Test method structure */
172 struct evp_test_method {
173 /* Name of test as it appears in file */
175 /* Initialise test for "alg" */
176 int (*init) (struct evp_test * t, const char *alg);
177 /* Clean up method */
178 void (*cleanup) (struct evp_test * t);
179 /* Test specific name value pair processing */
180 int (*parse) (struct evp_test * t, const char *name, const char *value);
181 /* Run the test itself */
182 int (*run_test) (struct evp_test * t);
185 static const struct evp_test_method digest_test_method, cipher_test_method;
186 static const struct evp_test_method aead_test_method;
188 static const struct evp_test_method *evp_test_list[] = {
194 static const struct evp_test_method *evp_find_test(const char *name)
196 const struct evp_test_method **tt;
197 for (tt = evp_test_list; *tt; tt++) {
198 if (!strcmp(name, (*tt)->name))
204 static void hex_print(const char *name, const unsigned char *buf, size_t len)
207 fprintf(stderr, "%s ", name);
208 for (i = 0; i < len; i++)
209 fprintf(stderr, "%02X", buf[i]);
213 static void print_expected(struct evp_test *t)
215 if (t->out_expected == NULL)
217 hex_print("Expected:", t->out_expected, t->out_len);
218 hex_print("Got: ", t->out_got, t->out_len);
219 OPENSSL_free(t->out_expected);
220 OPENSSL_free(t->out_got);
221 t->out_expected = NULL;
225 static int check_test_error(struct evp_test *t)
227 if (!t->err && !t->expected_err)
229 if (t->err && !t->expected_err) {
230 fprintf(stderr, "Test line %d: unexpected error %s\n",
231 t->start_line, t->err);
235 if (!t->err && t->expected_err) {
236 fprintf(stderr, "Test line %d: succeeded expecting %s\n",
237 t->start_line, t->expected_err);
240 if (!strcmp(t->err, t->expected_err))
243 fprintf(stderr, "Test line %d: expecting %s got %s\n",
244 t->start_line, t->expected_err, t->err);
248 /* Setup a new test, run any existing test */
250 static int setup_test(struct evp_test *t, const struct evp_test_method *tmeth)
252 /* If we already have a test set up run it */
256 if (t->meth->run_test(t) != 1) {
257 fprintf(stderr, "%s test error line %d\n",
258 t->meth->name, t->start_line);
261 if (!check_test_error(t)) {
263 ERR_print_errors_fp(stderr);
268 /* If new test type free old data */
269 if (tmeth != t->meth && t->data) {
270 OPENSSL_free(t->data);
273 if (t->expected_err) {
274 OPENSSL_free(t->expected_err);
275 t->expected_err = NULL;
282 static int process_test(struct evp_test *t, char *buf, int verbose)
284 char *keyword, *value;
286 const struct evp_test_method *tmeth;
289 if (!parse_line(&keyword, &value, buf))
291 /* See if keyword corresponds to a test start */
292 tmeth = evp_find_test(keyword);
294 if (!setup_test(t, tmeth))
296 t->start_line = t->line;
297 if (!tmeth->init(t, value)) {
298 fprintf(stderr, "Unknown %s: %s\n", keyword, value);
302 } else if (!strcmp(keyword, "Result")) {
303 if (t->expected_err) {
304 fprintf(stderr, "Line %d: multiple result lines\n", t->line);
307 t->expected_err = BUF_strdup(value);
308 if (!t->expected_err)
311 /* Must be test specific line: try to parse it */
313 rv = t->meth->parse(t, keyword, value);
316 fprintf(stderr, "line %d: unexpected keyword %s\n",
320 fprintf(stderr, "line %d: error processing keyword %s\n",
328 static int check_output(struct evp_test *t, const unsigned char *expected,
329 const unsigned char *got, size_t len)
331 if (!memcmp(expected, got, len))
333 t->out_expected = BUF_memdup(expected, len);
334 t->out_got = BUF_memdup(got, len);
336 if (t->out_expected == NULL || t->out_got == NULL) {
337 fprintf(stderr, "Memory allocation error!\n");
343 int main(int argc, char **argv)
350 fprintf(stderr, "usage: evp_test testfile.txt\n");
354 ERR_load_crypto_strings();
355 OpenSSL_add_all_algorithms();
362 t.out_expected = NULL;
365 in = fopen(argv[1], "r");
366 while (fgets(buf, sizeof(buf), in)) {
368 if (!process_test(&t, buf, 0))
371 /* Run any final test we have */
372 if (!setup_test(&t, NULL))
374 fprintf(stderr, "%d tests completed with %d errors\n",
380 static void test_free(void *d)
386 /* Message digest tests */
389 /* Digest this test is for */
390 const EVP_MD *digest;
391 /* Input to digest */
392 unsigned char *input;
394 /* Expected output */
395 unsigned char *output;
399 static int digest_test_init(struct evp_test *t, const char *alg)
401 const EVP_MD *digest;
402 struct digest_data *mdat = t->data;
403 digest = EVP_get_digestbyname(alg);
406 mdat = OPENSSL_malloc(sizeof(struct digest_data));
407 mdat->digest = digest;
414 static void digest_test_cleanup(struct evp_test *t)
416 struct digest_data *mdat = t->data;
417 test_free(mdat->input);
418 test_free(mdat->output);
421 static int digest_test_parse(struct evp_test *t,
422 const char *keyword, const char *value)
424 struct digest_data *mdata = t->data;
425 if (!strcmp(keyword, "Input"))
426 return test_bin(value, &mdata->input, &mdata->input_len);
427 if (!strcmp(keyword, "Output"))
428 return test_bin(value, &mdata->output, &mdata->output_len);
432 static int digest_test_run(struct evp_test *t)
434 struct digest_data *mdata = t->data;
435 const char *err = "INTERNAL_ERROR";
437 unsigned char md[EVP_MAX_MD_SIZE];
439 mctx = EVP_MD_CTX_create();
442 err = "DIGESTINIT_ERROR";
443 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))
445 err = "DIGESTUPDATE_ERROR";
446 if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))
448 err = "DIGESTFINAL_ERROR";
449 if (!EVP_DigestFinal(mctx, md, &md_len))
451 err = "DIGEST_LENGTH_MISMATCH";
452 if (md_len != mdata->output_len)
454 err = "DIGEST_MISMATCH";
455 if (check_output(t, mdata->output, md, md_len))
460 EVP_MD_CTX_destroy(mctx);
465 static const struct evp_test_method digest_test_method = {
475 const EVP_CIPHER *cipher;
477 /* Set to EVP_CIPH_GCM_MODE or EVP_CIPH_CCM_MODE if AEAD */
483 unsigned char *plaintext;
484 size_t plaintext_len;
485 unsigned char *ciphertext;
486 size_t ciphertext_len;
494 static int cipher_test_init(struct evp_test *t, const char *alg)
496 const EVP_CIPHER *cipher;
497 struct cipher_data *cdat = t->data;
498 cipher = EVP_get_cipherbyname(alg);
501 cdat = OPENSSL_malloc(sizeof(struct cipher_data));
502 cdat->cipher = cipher;
506 cdat->ciphertext = NULL;
507 cdat->plaintext = NULL;
511 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
512 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
513 cdat->aead = EVP_CIPHER_mode(cipher);
520 static void cipher_test_cleanup(struct evp_test *t)
522 struct cipher_data *cdat = t->data;
523 test_free(cdat->key);
525 test_free(cdat->ciphertext);
526 test_free(cdat->plaintext);
527 test_free(cdat->aad);
528 test_free(cdat->tag);
531 static int cipher_test_parse(struct evp_test *t, const char *keyword,
534 struct cipher_data *cdat = t->data;
535 if (!strcmp(keyword, "Key"))
536 return test_bin(value, &cdat->key, &cdat->key_len);
537 if (!strcmp(keyword, "IV"))
538 return test_bin(value, &cdat->iv, &cdat->iv_len);
539 if (!strcmp(keyword, "Plaintext"))
540 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
541 if (!strcmp(keyword, "Ciphertext"))
542 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
544 if (!strcmp(keyword, "AAD"))
545 return test_bin(value, &cdat->aad, &cdat->aad_len);
546 if (!strcmp(keyword, "Tag"))
547 return test_bin(value, &cdat->tag, &cdat->tag_len);
550 if (!strcmp(keyword, "Operation")) {
551 if (!strcmp(value, "ENCRYPT"))
553 else if (!strcmp(value, "DECRYPT"))
562 static int cipher_test_enc(struct evp_test *t, int enc)
564 struct cipher_data *cdat = t->data;
565 unsigned char *in, *out, *tmp = NULL;
566 size_t in_len, out_len;
568 EVP_CIPHER_CTX *ctx = NULL;
570 err = "INTERNAL_ERROR";
571 ctx = EVP_CIPHER_CTX_new();
574 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
576 in = cdat->plaintext;
577 in_len = cdat->plaintext_len;
578 out = cdat->ciphertext;
579 out_len = cdat->ciphertext_len;
581 in = cdat->ciphertext;
582 in_len = cdat->ciphertext_len;
583 out = cdat->plaintext;
584 out_len = cdat->plaintext_len;
586 tmp = OPENSSL_malloc(in_len + 2 * EVP_MAX_BLOCK_LENGTH);
589 err = "CIPHERINIT_ERROR";
590 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
592 err = "INVALID_IV_LENGTH";
594 if (cdat->aead == EVP_CIPH_GCM_MODE) {
595 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN,
598 } else if (cdat->aead == EVP_CIPH_CCM_MODE) {
599 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN,
602 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
608 * If encrypting just set tag length. If decrypting set
609 * tag length and value.
612 err = "TAG_LENGTH_SET_ERROR";
615 err = "TAG_SET_ERROR";
618 if (cdat->aead == EVP_CIPH_GCM_MODE && tag) {
619 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG,
622 } else if (cdat->aead == EVP_CIPH_CCM_MODE) {
623 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG,
629 err = "INVALID_KEY_LENGTH";
630 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len))
632 err = "KEY_SET_ERROR";
633 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1))
636 if (cdat->aead == EVP_CIPH_CCM_MODE) {
637 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
638 err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
643 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, cdat->aad, cdat->aad_len)) {
644 err = "AAD_SET_ERROR";
648 EVP_CIPHER_CTX_set_padding(ctx, 0);
649 err = "CIPHERUPDATE_ERROR";
650 if (!EVP_CipherUpdate(ctx, tmp, &tmplen, in, in_len))
652 if (cdat->aead == EVP_CIPH_CCM_MODE)
655 err = "CIPHERFINAL_ERROR";
656 if (!EVP_CipherFinal_ex(ctx, tmp + tmplen, &tmpflen))
659 err = "LENGTH_MISMATCH";
660 if (out_len != (size_t)(tmplen + tmpflen))
662 err = "VALUE_MISMATCH";
663 if (check_output(t, out, tmp, out_len))
665 if (enc && cdat->aead) {
666 unsigned char rtag[16];
667 if (cdat->tag_len > sizeof(rtag)) {
668 err = "TAG_LENGTH_INTERNAL_ERROR";
671 /* EVP_CTRL_CCM_GET_TAG and EVP_CTRL_GCM_GET_TAG are equal. */
672 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG,
673 cdat->tag_len, rtag)) {
674 err = "TAG_RETRIEVE_ERROR";
677 if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {
678 err = "TAG_VALUE_MISMATCH";
686 EVP_CIPHER_CTX_free(ctx);
691 static int cipher_test_run(struct evp_test *t)
693 struct cipher_data *cdat = t->data;
699 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
700 /* IV is optional and usually omitted in wrap mode */
701 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
706 if (cdat->aead && !cdat->tag) {
711 rv = cipher_test_enc(t, 1);
712 /* Not fatal errors: return */
719 if (cdat->enc != 1) {
720 rv = cipher_test_enc(t, 0);
721 /* Not fatal errors: return */
731 static const struct evp_test_method cipher_test_method = {