]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/evp_test.c
Rename some BUF_xxx to OPENSSL_xxx
[thirdparty/openssl.git] / test / evp_test.c
CommitLineData
307e3978 1/* evp_test.c */
0e360199 2/*
307e3978
DSH
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 * project.
5 */
6/* ====================================================================
7 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
0e360199
BL
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
0f113f3e 14 * notice, this list of conditions and the following disclaimer.
0e360199
BL
15 *
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
19 * distribution.
20 *
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
307e3978 24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
0e360199
BL
25 *
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
307e3978 29 * licensing@OpenSSL.org.
0e360199
BL
30 *
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.
34 *
35 * 6. Redistributions of any form whatsoever must retain the following
36 * acknowledgment:
37 * "This product includes software developed by the OpenSSL Project
307e3978 38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
0e360199
BL
39 *
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.
307e3978 52 * ====================================================================
0e360199
BL
53 */
54
55#include <stdio.h>
56#include <string.h>
307e3978
DSH
57#include <stdlib.h>
58#include <ctype.h>
0e360199 59#include <openssl/evp.h>
5824cc29 60#include <openssl/pem.h>
0b13e9f0 61#include <openssl/err.h>
307e3978 62#include <openssl/x509v3.h>
351fe214 63#include <openssl/pkcs12.h>
3b53e18a 64#include "internal/numbers.h"
0e360199 65
307e3978
DSH
66/* Remove spaces from beginning and end of a string */
67
68static void remove_space(char **pval)
0f113f3e 69{
307e3978 70 unsigned char *p = (unsigned char *)*pval;
0f113f3e 71
307e3978
DSH
72 while (isspace(*p))
73 p++;
74
75 *pval = (char *)p;
76
77 p = p + strlen(*pval) - 1;
78
79 /* Remove trailing space */
80 while (isspace(*p))
81 *p-- = 0;
0f113f3e 82}
0e360199 83
307e3978
DSH
84/*
85 * Given a line of the form:
86 * name = value # comment
87 * extract name and value. NB: modifies passed buffer.
88 */
89
90static int parse_line(char **pkw, char **pval, char *linebuf)
0f113f3e 91{
307e3978 92 char *p;
0e360199 93
307e3978 94 p = linebuf + strlen(linebuf) - 1;
0f113f3e 95
307e3978
DSH
96 if (*p != '\n') {
97 fprintf(stderr, "FATAL: missing EOL\n");
98 exit(1);
0e360199
BL
99 }
100
307e3978 101 /* Look for # */
5b46eee0 102
307e3978 103 p = strchr(linebuf, '#');
5b46eee0 104
307e3978
DSH
105 if (p)
106 *p = '\0';
5b46eee0 107
307e3978
DSH
108 /* Look for = sign */
109 p = strchr(linebuf, '=');
5b46eee0 110
307e3978
DSH
111 /* If no '=' exit */
112 if (!p)
113 return 0;
5b46eee0 114
307e3978 115 *p++ = '\0';
5b46eee0 116
307e3978
DSH
117 *pkw = linebuf;
118 *pval = p;
5b46eee0 119
307e3978
DSH
120 /* Remove spaces from keyword and value */
121 remove_space(pkw);
122 remove_space(pval);
123
124 return 1;
0f113f3e 125}
0e360199 126
3cdd1e94
EK
127/*
128 * Unescape some escape sequences in string literals.
129 * Return the result in a newly allocated buffer.
130 * Currently only supports '\n'.
131 * If the input length is 0, returns a valid 1-byte buffer, but sets
132 * the length to 0.
133 */
134static unsigned char* unescape(const char *input, size_t input_len,
135 size_t *out_len)
136{
137 unsigned char *ret, *p;
138 size_t i;
139 if (input_len == 0) {
140 *out_len = 0;
141 return OPENSSL_zalloc(1);
142 }
143
144 /* Escaping is non-expanding; over-allocate original size for simplicity. */
145 ret = p = OPENSSL_malloc(input_len);
146 if (ret == NULL)
147 return NULL;
148
149 for (i = 0; i < input_len; i++) {
150 if (input[i] == '\\') {
151 if (i == input_len - 1 || input[i+1] != 'n')
152 goto err;
153 *p++ = '\n';
154 i++;
155 } else {
156 *p++ = input[i];
157 }
158 }
159
160 *out_len = p - ret;
161 return ret;
162
163 err:
164 OPENSSL_free(ret);
165 return NULL;
166}
167
307e3978
DSH
168/* For a hex string "value" convert to a binary allocated buffer */
169static int test_bin(const char *value, unsigned char **buf, size_t *buflen)
0f113f3e 170{
307e3978
DSH
171 long len;
172 if (!*value) {
3cdd1e94
EK
173 /*
174 * Don't return NULL for zero length buffer.
175 * This is needed for some tests with empty keys: HMAC_Init_ex() expects
176 * a non-NULL key buffer even if the key length is 0, in order to detect
177 * key reset.
178 */
307e3978
DSH
179 *buf = OPENSSL_malloc(1);
180 if (!*buf)
181 return 0;
182 **buf = 0;
183 *buflen = 0;
184 return 1;
185 }
83251f39
DSH
186 /* Check for string literal */
187 if (value[0] == '"') {
188 size_t vlen;
189 value++;
190 vlen = strlen(value);
191 if (value[vlen - 1] != '"')
192 return 0;
193 vlen--;
3cdd1e94
EK
194 *buf = unescape(value, vlen, buflen);
195 if (*buf == NULL)
196 return 0;
83251f39
DSH
197 return 1;
198 }
3cdd1e94 199
307e3978
DSH
200 *buf = string_to_hex(value, &len);
201 if (!*buf) {
202 fprintf(stderr, "Value=%s\n", value);
203 ERR_print_errors_fp(stderr);
204 return -1;
205 }
206 /* Size of input buffer means we'll never overflow */
207 *buflen = len;
208 return 1;
0f113f3e 209}
3b53e18a
DSH
210/* Parse unsigned decimal 64 bit integer value */
211static int test_uint64(const char *value, uint64_t *pr)
212{
213 const char *p = value;
214 if (!*p) {
215 fprintf(stderr, "Invalid empty integer value\n");
216 return -1;
217 }
218 *pr = 0;
219 while (*p) {
220 if (*pr > UINT64_MAX/10) {
221 fprintf(stderr, "Integer string overflow value=%s\n", value);
222 return -1;
223 }
224 *pr *= 10;
225 if (*p < '0' || *p > '9') {
226 fprintf(stderr, "Invalid integer string value=%s\n", value);
227 return -1;
228 }
229 *pr += *p - '0';
230 p++;
231 }
232 return 1;
233}
848f735a 234
307e3978
DSH
235/* Structure holding test information */
236struct evp_test {
5824cc29
DSH
237 /* file being read */
238 FILE *in;
239 /* List of public and private keys */
240 struct key_list *private;
241 struct key_list *public;
307e3978
DSH
242 /* method for this test */
243 const struct evp_test_method *meth;
244 /* current line being processed */
245 unsigned int line;
246 /* start line of current test */
247 unsigned int start_line;
248 /* Error string for test */
249 const char *err;
250 /* Expected error value of test */
251 char *expected_err;
252 /* Number of tests */
253 int ntests;
254 /* Error count */
255 int errors;
7a6c9792
DSH
256 /* Number of tests skipped */
257 int nskip;
b033e5d5 258 /* If output mismatch expected and got value */
3cdd1e94
EK
259 unsigned char *out_received;
260 size_t out_received_len;
b033e5d5 261 unsigned char *out_expected;
3cdd1e94 262 size_t out_expected_len;
307e3978
DSH
263 /* test specific data */
264 void *data;
7a6c9792
DSH
265 /* Current test should be skipped */
266 int skip;
307e3978 267};
5824cc29
DSH
268
269struct key_list {
270 char *name;
271 EVP_PKEY *key;
272 struct key_list *next;
273};
274
307e3978
DSH
275/* Test method structure */
276struct evp_test_method {
277 /* Name of test as it appears in file */
278 const char *name;
279 /* Initialise test for "alg" */
280 int (*init) (struct evp_test * t, const char *alg);
281 /* Clean up method */
282 void (*cleanup) (struct evp_test * t);
283 /* Test specific name value pair processing */
284 int (*parse) (struct evp_test * t, const char *name, const char *value);
285 /* Run the test itself */
286 int (*run_test) (struct evp_test * t);
287};
288
289static const struct evp_test_method digest_test_method, cipher_test_method;
f9e31463 290static const struct evp_test_method mac_test_method;
5824cc29
DSH
291static const struct evp_test_method psign_test_method, pverify_test_method;
292static const struct evp_test_method pdecrypt_test_method;
293static const struct evp_test_method pverify_recover_test_method;
3b53e18a 294static const struct evp_test_method pbe_test_method;
3cdd1e94 295static const struct evp_test_method encode_test_method;
307e3978
DSH
296
297static const struct evp_test_method *evp_test_list[] = {
298 &digest_test_method,
299 &cipher_test_method,
83251f39 300 &mac_test_method,
5824cc29
DSH
301 &psign_test_method,
302 &pverify_test_method,
303 &pdecrypt_test_method,
304 &pverify_recover_test_method,
3b53e18a 305 &pbe_test_method,
3cdd1e94 306 &encode_test_method,
83251f39 307 NULL
307e3978
DSH
308};
309
310static const struct evp_test_method *evp_find_test(const char *name)
0f113f3e 311{
307e3978 312 const struct evp_test_method **tt;
86885c28 313
307e3978 314 for (tt = evp_test_list; *tt; tt++) {
86885c28 315 if (strcmp(name, (*tt)->name) == 0)
307e3978
DSH
316 return *tt;
317 }
318 return NULL;
0f113f3e
MC
319}
320
b033e5d5
DSH
321static void hex_print(const char *name, const unsigned char *buf, size_t len)
322{
323 size_t i;
324 fprintf(stderr, "%s ", name);
325 for (i = 0; i < len; i++)
326 fprintf(stderr, "%02X", buf[i]);
327 fputs("\n", stderr);
328}
329
5724bd49
DSH
330static void free_expected(struct evp_test *t)
331{
b548a1f1
RS
332 OPENSSL_free(t->expected_err);
333 t->expected_err = NULL;
25aaa98a 334 OPENSSL_free(t->out_expected);
3cdd1e94 335 OPENSSL_free(t->out_received);
25aaa98a 336 t->out_expected = NULL;
3cdd1e94
EK
337 t->out_received = NULL;
338 t->out_expected_len = 0;
339 t->out_received_len = 0;
340 /* Literals. */
341 t->err = NULL;
5724bd49
DSH
342}
343
b033e5d5
DSH
344static void print_expected(struct evp_test *t)
345{
3cdd1e94 346 if (t->out_expected == NULL && t->out_received == NULL)
b033e5d5 347 return;
3cdd1e94
EK
348 hex_print("Expected:", t->out_expected, t->out_expected_len);
349 hex_print("Got: ", t->out_received, t->out_received_len);
5724bd49 350 free_expected(t);
b033e5d5
DSH
351}
352
307e3978 353static int check_test_error(struct evp_test *t)
0f113f3e 354{
307e3978
DSH
355 if (!t->err && !t->expected_err)
356 return 1;
357 if (t->err && !t->expected_err) {
358 fprintf(stderr, "Test line %d: unexpected error %s\n",
359 t->start_line, t->err);
b033e5d5 360 print_expected(t);
307e3978 361 return 0;
0f113f3e 362 }
307e3978
DSH
363 if (!t->err && t->expected_err) {
364 fprintf(stderr, "Test line %d: succeeded expecting %s\n",
365 t->start_line, t->expected_err);
366 return 0;
367 }
86885c28 368 if (strcmp(t->err, t->expected_err) == 0)
307e3978 369 return 1;
544a2aea 370
307e3978
DSH
371 fprintf(stderr, "Test line %d: expecting %s got %s\n",
372 t->start_line, t->expected_err, t->err);
373 return 0;
374}
0f113f3e 375
307e3978 376/* Setup a new test, run any existing test */
0f113f3e 377
307e3978
DSH
378static int setup_test(struct evp_test *t, const struct evp_test_method *tmeth)
379{
380 /* If we already have a test set up run it */
381 if (t->meth) {
382 t->ntests++;
7a6c9792 383 if (t->skip) {
578ce42d 384 t->meth = tmeth;
7a6c9792
DSH
385 t->nskip++;
386 return 1;
387 }
307e3978
DSH
388 t->err = NULL;
389 if (t->meth->run_test(t) != 1) {
390 fprintf(stderr, "%s test error line %d\n",
391 t->meth->name, t->start_line);
392 return 0;
0f113f3e 393 }
307e3978
DSH
394 if (!check_test_error(t)) {
395 if (t->err)
0f113f3e 396 ERR_print_errors_fp(stderr);
307e3978 397 t->errors++;
0f113f3e 398 }
307e3978
DSH
399 ERR_clear_error();
400 t->meth->cleanup(t);
d5ec8efc
DSH
401 OPENSSL_free(t->data);
402 t->data = NULL;
b548a1f1
RS
403 OPENSSL_free(t->expected_err);
404 t->expected_err = NULL;
5724bd49 405 free_expected(t);
307e3978
DSH
406 }
407 t->meth = tmeth;
408 return 1;
409}
0f113f3e 410
7a6c9792 411static int find_key(EVP_PKEY **ppk, const char *name, struct key_list *lst)
5824cc29
DSH
412{
413 for (; lst; lst = lst->next) {
86885c28 414 if (strcmp(lst->name, name) == 0) {
7a6c9792
DSH
415 if (ppk)
416 *ppk = lst->key;
417 return 1;
418 }
5824cc29 419 }
7a6c9792 420 return 0;
5824cc29
DSH
421}
422
423static void free_key_list(struct key_list *lst)
424{
d5ec8efc 425 while (lst != NULL) {
366448ec 426 struct key_list *ltmp;
5824cc29
DSH
427 EVP_PKEY_free(lst->key);
428 OPENSSL_free(lst->name);
366448ec
DSH
429 ltmp = lst->next;
430 OPENSSL_free(lst);
431 lst = ltmp;
5824cc29
DSH
432 }
433}
434
7a6c9792
DSH
435static int check_unsupported()
436{
437 long err = ERR_peek_error();
438 if (ERR_GET_LIB(err) == ERR_LIB_EVP
366448ec 439 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
7a6c9792
DSH
440 ERR_clear_error();
441 return 1;
442 }
443 return 0;
444}
445
307e3978
DSH
446static int process_test(struct evp_test *t, char *buf, int verbose)
447{
4c9b0a03 448 char *keyword = NULL, *value = NULL;
7a6c9792 449 int rv = 0, add_key = 0;
4c9b0a03
GK
450 long save_pos = 0;
451 struct key_list **lst = NULL, *key = NULL;
5824cc29 452 EVP_PKEY *pk = NULL;
4c9b0a03 453 const struct evp_test_method *tmeth = NULL;
307e3978
DSH
454 if (verbose)
455 fputs(buf, stdout);
456 if (!parse_line(&keyword, &value, buf))
457 return 1;
86885c28 458 if (strcmp(keyword, "PrivateKey") == 0) {
5824cc29
DSH
459 save_pos = ftell(t->in);
460 pk = PEM_read_PrivateKey(t->in, NULL, 0, NULL);
7a6c9792 461 if (pk == NULL && !check_unsupported()) {
5824cc29
DSH
462 fprintf(stderr, "Error reading private key %s\n", value);
463 ERR_print_errors_fp(stderr);
464 return 0;
465 }
466 lst = &t->private;
7a6c9792 467 add_key = 1;
5824cc29 468 }
86885c28 469 if (strcmp(keyword, "PublicKey") == 0) {
5824cc29
DSH
470 save_pos = ftell(t->in);
471 pk = PEM_read_PUBKEY(t->in, NULL, 0, NULL);
7a6c9792 472 if (pk == NULL && !check_unsupported()) {
5824cc29
DSH
473 fprintf(stderr, "Error reading public key %s\n", value);
474 ERR_print_errors_fp(stderr);
475 return 0;
476 }
477 lst = &t->public;
7a6c9792 478 add_key = 1;
5824cc29
DSH
479 }
480 /* If we have a key add to list */
7a6c9792 481 if (add_key) {
5824cc29 482 char tmpbuf[80];
7a6c9792 483 if (find_key(NULL, value, *lst)) {
5824cc29
DSH
484 fprintf(stderr, "Duplicate key %s\n", value);
485 return 0;
486 }
b4faea50 487 key = OPENSSL_malloc(sizeof(*key));
5824cc29
DSH
488 if (!key)
489 return 0;
7644a9ae 490 key->name = OPENSSL_strdup(value);
5824cc29
DSH
491 key->key = pk;
492 key->next = *lst;
493 *lst = key;
494 /* Rewind input, read to end and update line numbers */
495 fseek(t->in, save_pos, SEEK_SET);
496 while (fgets(tmpbuf, sizeof(tmpbuf), t->in)) {
497 t->line++;
86885c28 498 if (strncmp(tmpbuf, "-----END", 8) == 0)
5824cc29
DSH
499 return 1;
500 }
501 fprintf(stderr, "Can't find key end\n");
502 return 0;
503 }
504
307e3978
DSH
505 /* See if keyword corresponds to a test start */
506 tmeth = evp_find_test(keyword);
507 if (tmeth) {
508 if (!setup_test(t, tmeth))
509 return 0;
510 t->start_line = t->line;
7a6c9792 511 t->skip = 0;
307e3978
DSH
512 if (!tmeth->init(t, value)) {
513 fprintf(stderr, "Unknown %s: %s\n", keyword, value);
514 return 0;
0f113f3e 515 }
307e3978 516 return 1;
7a6c9792
DSH
517 } else if (t->skip) {
518 return 1;
86885c28 519 } else if (strcmp(keyword, "Result") == 0) {
307e3978
DSH
520 if (t->expected_err) {
521 fprintf(stderr, "Line %d: multiple result lines\n", t->line);
522 return 0;
0f113f3e 523 }
7644a9ae 524 t->expected_err = OPENSSL_strdup(value);
307e3978
DSH
525 if (!t->expected_err)
526 return 0;
527 } else {
528 /* Must be test specific line: try to parse it */
529 if (t->meth)
530 rv = t->meth->parse(t, keyword, value);
531
532 if (rv == 0)
533 fprintf(stderr, "line %d: unexpected keyword %s\n",
534 t->line, keyword);
535
536 if (rv < 0)
537 fprintf(stderr, "line %d: error processing keyword %s\n",
538 t->line, keyword);
539 if (rv <= 0)
540 return 0;
0f113f3e 541 }
307e3978
DSH
542 return 1;
543}
0f113f3e 544
3cdd1e94
EK
545static int check_var_length_output(struct evp_test *t,
546 const unsigned char *expected,
547 size_t expected_len,
548 const unsigned char *received,
549 size_t received_len)
b033e5d5 550{
3cdd1e94
EK
551 if (expected_len == received_len &&
552 memcmp(expected, received, expected_len) == 0) {
b033e5d5 553 return 0;
3cdd1e94
EK
554 }
555
556 /* The result printing code expects a non-NULL buffer. */
7644a9ae 557 t->out_expected = OPENSSL_memdup(expected, expected_len ? expected_len : 1);
3cdd1e94 558 t->out_expected_len = expected_len;
7644a9ae 559 t->out_received = OPENSSL_memdup(received, received_len ? received_len : 1);
3cdd1e94
EK
560 t->out_received_len = received_len;
561 if (t->out_expected == NULL || t->out_received == NULL) {
b033e5d5
DSH
562 fprintf(stderr, "Memory allocation error!\n");
563 exit(1);
564 }
565 return 1;
566}
567
3cdd1e94
EK
568static int check_output(struct evp_test *t,
569 const unsigned char *expected,
570 const unsigned char *received,
571 size_t len)
572{
573 return check_var_length_output(t, expected, len, received, len);
574}
575
307e3978
DSH
576int main(int argc, char **argv)
577{
578 FILE *in = NULL;
579 char buf[10240];
580 struct evp_test t;
0f113f3e 581
b033e5d5
DSH
582 if (argc != 2) {
583 fprintf(stderr, "usage: evp_test testfile.txt\n");
584 return 1;
585 }
586
d5ec8efc
DSH
587 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
588
307e3978
DSH
589 ERR_load_crypto_strings();
590 OpenSSL_add_all_algorithms();
1526fea5 591
366448ec 592 memset(&t, 0, sizeof(t));
307e3978 593 t.start_line = -1;
307e3978 594 in = fopen(argv[1], "r");
5824cc29 595 t.in = in;
307e3978
DSH
596 while (fgets(buf, sizeof(buf), in)) {
597 t.line++;
598 if (!process_test(&t, buf, 0))
599 exit(1);
600 }
601 /* Run any final test we have */
602 if (!setup_test(&t, NULL))
603 exit(1);
7a6c9792
DSH
604 fprintf(stderr, "%d tests completed with %d errors, %d skipped\n",
605 t.ntests, t.errors, t.nskip);
5824cc29
DSH
606 free_key_list(t.public);
607 free_key_list(t.private);
307e3978 608 fclose(in);
d5ec8efc
DSH
609 EVP_cleanup();
610 CRYPTO_cleanup_all_ex_data();
611 ERR_remove_thread_state(NULL);
612 ERR_free_strings();
7644a9ae 613#ifdef CRYPTO_MDEBUG
d5ec8efc 614 CRYPTO_mem_leaks_fp(stderr);
7644a9ae 615#endif
6906a7c1
DSH
616 if (t.errors)
617 return 1;
307e3978 618 return 0;
0f113f3e
MC
619}
620
307e3978 621static void test_free(void *d)
0f113f3e 622{
b548a1f1 623 OPENSSL_free(d);
307e3978 624}
4897dc40 625
307e3978 626/* Message digest tests */
4897dc40 627
307e3978
DSH
628struct digest_data {
629 /* Digest this test is for */
630 const EVP_MD *digest;
631 /* Input to digest */
632 unsigned char *input;
633 size_t input_len;
618be04e
DSH
634 /* Repeat count for input */
635 size_t nrpt;
307e3978
DSH
636 /* Expected output */
637 unsigned char *output;
638 size_t output_len;
639};
4897dc40 640
307e3978
DSH
641static int digest_test_init(struct evp_test *t, const char *alg)
642{
643 const EVP_MD *digest;
070c2332 644 struct digest_data *mdat;
307e3978 645 digest = EVP_get_digestbyname(alg);
578ce42d
DSH
646 if (!digest) {
647 /* If alg has an OID assume disabled algorithm */
648 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
649 t->skip = 1;
650 return 1;
651 }
307e3978 652 return 0;
578ce42d 653 }
b4faea50 654 mdat = OPENSSL_malloc(sizeof(*mdat));
307e3978
DSH
655 mdat->digest = digest;
656 mdat->input = NULL;
657 mdat->output = NULL;
618be04e 658 mdat->nrpt = 1;
307e3978 659 t->data = mdat;
4897dc40 660 return 1;
0f113f3e 661}
4897dc40 662
307e3978
DSH
663static void digest_test_cleanup(struct evp_test *t)
664{
665 struct digest_data *mdat = t->data;
666 test_free(mdat->input);
667 test_free(mdat->output);
668}
669
670static int digest_test_parse(struct evp_test *t,
671 const char *keyword, const char *value)
672{
673 struct digest_data *mdata = t->data;
86885c28 674 if (strcmp(keyword, "Input") == 0)
307e3978 675 return test_bin(value, &mdata->input, &mdata->input_len);
86885c28 676 if (strcmp(keyword, "Output") == 0)
307e3978 677 return test_bin(value, &mdata->output, &mdata->output_len);
86885c28 678 if (strcmp(keyword, "Count") == 0) {
618be04e
DSH
679 long nrpt = atoi(value);
680 if (nrpt <= 0)
681 return 0;
682 mdata->nrpt = (size_t)nrpt;
683 return 1;
684 }
307e3978
DSH
685 return 0;
686}
687
688static int digest_test_run(struct evp_test *t)
0f113f3e 689{
307e3978 690 struct digest_data *mdata = t->data;
618be04e 691 size_t i;
307e3978
DSH
692 const char *err = "INTERNAL_ERROR";
693 EVP_MD_CTX *mctx;
4897dc40 694 unsigned char md[EVP_MAX_MD_SIZE];
307e3978 695 unsigned int md_len;
bfb0641f 696 mctx = EVP_MD_CTX_new();
307e3978
DSH
697 if (!mctx)
698 goto err;
699 err = "DIGESTINIT_ERROR";
700 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))
701 goto err;
702 err = "DIGESTUPDATE_ERROR";
618be04e
DSH
703 for (i = 0; i < mdata->nrpt; i++) {
704 if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))
705 goto err;
706 }
307e3978
DSH
707 err = "DIGESTFINAL_ERROR";
708 if (!EVP_DigestFinal(mctx, md, &md_len))
709 goto err;
710 err = "DIGEST_LENGTH_MISMATCH";
711 if (md_len != mdata->output_len)
712 goto err;
713 err = "DIGEST_MISMATCH";
b033e5d5 714 if (check_output(t, mdata->output, md, md_len))
307e3978
DSH
715 goto err;
716 err = NULL;
717 err:
bfb0641f 718 EVP_MD_CTX_free(mctx);
307e3978 719 t->err = err;
b033e5d5 720 return 1;
307e3978 721}
4897dc40 722
307e3978
DSH
723static const struct evp_test_method digest_test_method = {
724 "Digest",
725 digest_test_init,
726 digest_test_cleanup,
727 digest_test_parse,
728 digest_test_run
729};
730
731/* Cipher tests */
732struct cipher_data {
733 const EVP_CIPHER *cipher;
734 int enc;
2207ba7b 735 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
307e3978
DSH
736 int aead;
737 unsigned char *key;
738 size_t key_len;
739 unsigned char *iv;
740 size_t iv_len;
741 unsigned char *plaintext;
742 size_t plaintext_len;
743 unsigned char *ciphertext;
744 size_t ciphertext_len;
745 /* GCM, CCM only */
746 unsigned char *aad;
747 size_t aad_len;
748 unsigned char *tag;
749 size_t tag_len;
750};
751
752static int cipher_test_init(struct evp_test *t, const char *alg)
753{
754 const EVP_CIPHER *cipher;
755 struct cipher_data *cdat = t->data;
756 cipher = EVP_get_cipherbyname(alg);
33a89fa6
DSH
757 if (!cipher) {
758 /* If alg has an OID assume disabled algorithm */
759 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
760 t->skip = 1;
761 return 1;
762 }
0f113f3e 763 return 0;
33a89fa6 764 }
b4faea50 765 cdat = OPENSSL_malloc(sizeof(*cdat));
307e3978
DSH
766 cdat->cipher = cipher;
767 cdat->enc = -1;
768 cdat->key = NULL;
769 cdat->iv = NULL;
770 cdat->ciphertext = NULL;
771 cdat->plaintext = NULL;
772 cdat->aad = NULL;
773 cdat->tag = NULL;
774 t->data = cdat;
775 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
2207ba7b 776 || EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE
307e3978
DSH
777 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
778 cdat->aead = EVP_CIPHER_mode(cipher);
eb85cb86
AP
779 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
780 cdat->aead = -1;
307e3978
DSH
781 else
782 cdat->aead = 0;
4897dc40 783
307e3978
DSH
784 return 1;
785}
4897dc40 786
307e3978
DSH
787static void cipher_test_cleanup(struct evp_test *t)
788{
789 struct cipher_data *cdat = t->data;
790 test_free(cdat->key);
791 test_free(cdat->iv);
792 test_free(cdat->ciphertext);
793 test_free(cdat->plaintext);
794 test_free(cdat->aad);
795 test_free(cdat->tag);
796}
4897dc40 797
307e3978
DSH
798static int cipher_test_parse(struct evp_test *t, const char *keyword,
799 const char *value)
800{
801 struct cipher_data *cdat = t->data;
86885c28 802 if (strcmp(keyword, "Key") == 0)
307e3978 803 return test_bin(value, &cdat->key, &cdat->key_len);
86885c28 804 if (strcmp(keyword, "IV") == 0)
307e3978 805 return test_bin(value, &cdat->iv, &cdat->iv_len);
86885c28 806 if (strcmp(keyword, "Plaintext") == 0)
307e3978 807 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
86885c28 808 if (strcmp(keyword, "Ciphertext") == 0)
307e3978
DSH
809 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
810 if (cdat->aead) {
86885c28 811 if (strcmp(keyword, "AAD") == 0)
307e3978 812 return test_bin(value, &cdat->aad, &cdat->aad_len);
86885c28 813 if (strcmp(keyword, "Tag") == 0)
307e3978 814 return test_bin(value, &cdat->tag, &cdat->tag_len);
0f113f3e 815 }
4897dc40 816
86885c28
RS
817 if (strcmp(keyword, "Operation") == 0) {
818 if (strcmp(value, "ENCRYPT") == 0)
307e3978 819 cdat->enc = 1;
86885c28 820 else if (strcmp(value, "DECRYPT") == 0)
307e3978
DSH
821 cdat->enc = 0;
822 else
823 return 0;
824 return 1;
0f113f3e 825 }
307e3978 826 return 0;
0f113f3e 827}
4897dc40 828
307e3978 829static int cipher_test_enc(struct evp_test *t, int enc)
0f113f3e 830{
307e3978
DSH
831 struct cipher_data *cdat = t->data;
832 unsigned char *in, *out, *tmp = NULL;
833 size_t in_len, out_len;
834 int tmplen, tmpflen;
835 EVP_CIPHER_CTX *ctx = NULL;
836 const char *err;
837 err = "INTERNAL_ERROR";
838 ctx = EVP_CIPHER_CTX_new();
839 if (!ctx)
840 goto err;
841 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
842 if (enc) {
843 in = cdat->plaintext;
844 in_len = cdat->plaintext_len;
845 out = cdat->ciphertext;
846 out_len = cdat->ciphertext_len;
847 } else {
848 in = cdat->ciphertext;
849 in_len = cdat->ciphertext_len;
850 out = cdat->plaintext;
851 out_len = cdat->plaintext_len;
0f113f3e 852 }
307e3978
DSH
853 tmp = OPENSSL_malloc(in_len + 2 * EVP_MAX_BLOCK_LENGTH);
854 if (!tmp)
855 goto err;
856 err = "CIPHERINIT_ERROR";
857 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
858 goto err;
859 err = "INVALID_IV_LENGTH";
860 if (cdat->iv) {
2207ba7b
DSH
861 if (cdat->aead) {
862 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
307e3978
DSH
863 cdat->iv_len, 0))
864 goto err;
865 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
866 goto err;
0f113f3e 867 }
307e3978
DSH
868 if (cdat->aead) {
869 unsigned char *tag;
870 /*
2207ba7b
DSH
871 * If encrypting or OCB just set tag length initially, otherwise
872 * set tag length and value.
307e3978 873 */
2207ba7b 874 if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {
307e3978
DSH
875 err = "TAG_LENGTH_SET_ERROR";
876 tag = NULL;
0f113f3e 877 } else {
307e3978
DSH
878 err = "TAG_SET_ERROR";
879 tag = cdat->tag;
0f113f3e 880 }
2207ba7b
DSH
881 if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {
882 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
366448ec 883 cdat->tag_len, tag))
307e3978 884 goto err;
0f113f3e 885 }
307e3978 886 }
0f113f3e 887
307e3978
DSH
888 err = "INVALID_KEY_LENGTH";
889 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len))
890 goto err;
891 err = "KEY_SET_ERROR";
892 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1))
893 goto err;
894
2207ba7b
DSH
895 if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {
896 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
897 cdat->tag_len, cdat->tag)) {
366448ec
DSH
898 err = "TAG_SET_ERROR";
899 goto err;
2207ba7b
DSH
900 }
901 }
902
307e3978
DSH
903 if (cdat->aead == EVP_CIPH_CCM_MODE) {
904 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
905 err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
906 goto err;
0f113f3e
MC
907 }
908 }
307e3978
DSH
909 if (cdat->aad) {
910 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, cdat->aad, cdat->aad_len)) {
911 err = "AAD_SET_ERROR";
912 goto err;
913 }
914 }
915 EVP_CIPHER_CTX_set_padding(ctx, 0);
916 err = "CIPHERUPDATE_ERROR";
917 if (!EVP_CipherUpdate(ctx, tmp, &tmplen, in, in_len))
918 goto err;
919 if (cdat->aead == EVP_CIPH_CCM_MODE)
920 tmpflen = 0;
921 else {
922 err = "CIPHERFINAL_ERROR";
923 if (!EVP_CipherFinal_ex(ctx, tmp + tmplen, &tmpflen))
924 goto err;
925 }
926 err = "LENGTH_MISMATCH";
927 if (out_len != (size_t)(tmplen + tmpflen))
928 goto err;
929 err = "VALUE_MISMATCH";
b033e5d5 930 if (check_output(t, out, tmp, out_len))
307e3978
DSH
931 goto err;
932 if (enc && cdat->aead) {
933 unsigned char rtag[16];
934 if (cdat->tag_len > sizeof(rtag)) {
935 err = "TAG_LENGTH_INTERNAL_ERROR";
936 goto err;
937 }
2207ba7b 938 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
307e3978
DSH
939 cdat->tag_len, rtag)) {
940 err = "TAG_RETRIEVE_ERROR";
941 goto err;
942 }
b033e5d5 943 if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {
307e3978
DSH
944 err = "TAG_VALUE_MISMATCH";
945 goto err;
946 }
947 }
948 err = NULL;
949 err:
b548a1f1 950 OPENSSL_free(tmp);
307e3978
DSH
951 EVP_CIPHER_CTX_free(ctx);
952 t->err = err;
953 return err ? 0 : 1;
954}
0e360199 955
307e3978
DSH
956static int cipher_test_run(struct evp_test *t)
957{
958 struct cipher_data *cdat = t->data;
959 int rv;
960 if (!cdat->key) {
961 t->err = "NO_KEY";
962 return 0;
963 }
964 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
965 /* IV is optional and usually omitted in wrap mode */
966 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
967 t->err = "NO_IV";
968 return 0;
969 }
970 }
971 if (cdat->aead && !cdat->tag) {
972 t->err = "NO_TAG";
973 return 0;
974 }
975 if (cdat->enc) {
976 rv = cipher_test_enc(t, 1);
977 /* Not fatal errors: return */
978 if (rv != 1) {
979 if (rv < 0)
980 return 0;
981 return 1;
982 }
983 }
984 if (cdat->enc != 1) {
985 rv = cipher_test_enc(t, 0);
986 /* Not fatal errors: return */
987 if (rv != 1) {
988 if (rv < 0)
989 return 0;
990 return 1;
991 }
992 }
993 return 1;
0f113f3e 994}
307e3978
DSH
995
996static const struct evp_test_method cipher_test_method = {
997 "Cipher",
998 cipher_test_init,
999 cipher_test_cleanup,
1000 cipher_test_parse,
1001 cipher_test_run
1002};
83251f39
DSH
1003
1004struct mac_data {
1005 /* MAC type */
1006 int type;
1007 /* Algorithm string for this MAC */
1008 char *alg;
1009 /* MAC key */
1010 unsigned char *key;
1011 size_t key_len;
1012 /* Input to MAC */
1013 unsigned char *input;
1014 size_t input_len;
1015 /* Expected output */
1016 unsigned char *output;
1017 size_t output_len;
1018};
1019
1020static int mac_test_init(struct evp_test *t, const char *alg)
1021{
1022 int type;
1023 struct mac_data *mdat;
86885c28 1024 if (strcmp(alg, "HMAC") == 0)
83251f39 1025 type = EVP_PKEY_HMAC;
86885c28 1026 else if (strcmp(alg, "CMAC") == 0)
83251f39
DSH
1027 type = EVP_PKEY_CMAC;
1028 else
1029 return 0;
1030
b4faea50 1031 mdat = OPENSSL_malloc(sizeof(*mdat));
83251f39
DSH
1032 mdat->type = type;
1033 mdat->alg = NULL;
1034 mdat->key = NULL;
1035 mdat->input = NULL;
1036 mdat->output = NULL;
1037 t->data = mdat;
1038 return 1;
1039}
1040
1041static void mac_test_cleanup(struct evp_test *t)
1042{
1043 struct mac_data *mdat = t->data;
1044 test_free(mdat->alg);
1045 test_free(mdat->key);
1046 test_free(mdat->input);
1047 test_free(mdat->output);
1048}
1049
1050static int mac_test_parse(struct evp_test *t,
1051 const char *keyword, const char *value)
1052{
1053 struct mac_data *mdata = t->data;
86885c28 1054 if (strcmp(keyword, "Key") == 0)
83251f39 1055 return test_bin(value, &mdata->key, &mdata->key_len);
86885c28 1056 if (strcmp(keyword, "Algorithm") == 0) {
7644a9ae 1057 mdata->alg = OPENSSL_strdup(value);
83251f39
DSH
1058 if (!mdata->alg)
1059 return 0;
1060 return 1;
1061 }
86885c28 1062 if (strcmp(keyword, "Input") == 0)
83251f39 1063 return test_bin(value, &mdata->input, &mdata->input_len);
86885c28 1064 if (strcmp(keyword, "Output") == 0)
83251f39
DSH
1065 return test_bin(value, &mdata->output, &mdata->output_len);
1066 return 0;
1067}
1068
1069static int mac_test_run(struct evp_test *t)
1070{
1071 struct mac_data *mdata = t->data;
1072 const char *err = "INTERNAL_ERROR";
1073 EVP_MD_CTX *mctx = NULL;
1074 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1075 EVP_PKEY *key = NULL;
1076 const EVP_MD *md = NULL;
1077 unsigned char *mac = NULL;
1078 size_t mac_len;
1079
1080 err = "MAC_PKEY_CTX_ERROR";
1081 genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL);
1082 if (!genctx)
1083 goto err;
1084
1085 err = "MAC_KEYGEN_INIT_ERROR";
1086 if (EVP_PKEY_keygen_init(genctx) <= 0)
1087 goto err;
1088 if (mdata->type == EVP_PKEY_CMAC) {
1089 err = "MAC_ALGORITHM_SET_ERROR";
1090 if (EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0)
1091 goto err;
1092 }
1093
1094 err = "MAC_KEY_SET_ERROR";
1095 if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0)
1096 goto err;
1097
1098 err = "MAC_KEY_GENERATE_ERROR";
1099 if (EVP_PKEY_keygen(genctx, &key) <= 0)
1100 goto err;
1101 if (mdata->type == EVP_PKEY_HMAC) {
1102 err = "MAC_ALGORITHM_SET_ERROR";
1103 md = EVP_get_digestbyname(mdata->alg);
1104 if (!md)
1105 goto err;
1106 }
bfb0641f 1107 mctx = EVP_MD_CTX_new();
83251f39
DSH
1108 if (!mctx)
1109 goto err;
1110 err = "DIGESTSIGNINIT_ERROR";
1111 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key))
1112 goto err;
1113
1114 err = "DIGESTSIGNUPDATE_ERROR";
1115 if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len))
1116 goto err;
1117 err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1118 if (!EVP_DigestSignFinal(mctx, NULL, &mac_len))
1119 goto err;
1120 mac = OPENSSL_malloc(mac_len);
1121 if (!mac) {
1122 fprintf(stderr, "Error allocating mac buffer!\n");
1123 exit(1);
1124 }
1125 if (!EVP_DigestSignFinal(mctx, mac, &mac_len))
1126 goto err;
1127 err = "MAC_LENGTH_MISMATCH";
1128 if (mac_len != mdata->output_len)
1129 goto err;
1130 err = "MAC_MISMATCH";
1131 if (check_output(t, mdata->output, mac, mac_len))
1132 goto err;
1133 err = NULL;
1134 err:
bfb0641f 1135 EVP_MD_CTX_free(mctx);
b548a1f1 1136 OPENSSL_free(mac);
c5ba2d99
RS
1137 EVP_PKEY_CTX_free(genctx);
1138 EVP_PKEY_free(key);
83251f39
DSH
1139 t->err = err;
1140 return 1;
1141}
1142
1143static const struct evp_test_method mac_test_method = {
1144 "MAC",
1145 mac_test_init,
1146 mac_test_cleanup,
1147 mac_test_parse,
1148 mac_test_run
1149};
5824cc29
DSH
1150
1151/*
1152 * Public key operations. These are all very similar and can share
1153 * a lot of common code.
1154 */
1155
1156struct pkey_data {
1157 /* Context for this operation */
1158 EVP_PKEY_CTX *ctx;
1159 /* Key operation to perform */
1160 int (*keyop) (EVP_PKEY_CTX *ctx,
1161 unsigned char *sig, size_t *siglen,
1162 const unsigned char *tbs, size_t tbslen);
1163 /* Input to MAC */
1164 unsigned char *input;
1165 size_t input_len;
1166 /* Expected output */
1167 unsigned char *output;
1168 size_t output_len;
1169};
1170
1171/*
1172 * Perform public key operation setup: lookup key, allocated ctx and call
1173 * the appropriate initialisation function
1174 */
1175static int pkey_test_init(struct evp_test *t, const char *name,
1176 int use_public,
1177 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1178 int (*keyop) (EVP_PKEY_CTX *ctx,
1179 unsigned char *sig, size_t *siglen,
1180 const unsigned char *tbs,
1181 size_t tbslen)
1182 )
1183{
1184 struct pkey_data *kdata;
1185 EVP_PKEY *pkey = NULL;
7a6c9792
DSH
1186 int rv = 0;
1187 if (use_public)
1188 rv = find_key(&pkey, name, t->public);
1189 if (!rv)
1190 rv = find_key(&pkey, name, t->private);
1191 if (!rv)
1192 return 0;
1193 if (!pkey) {
1194 t->skip = 1;
1195 return 1;
1196 }
1197
b4faea50 1198 kdata = OPENSSL_malloc(sizeof(*kdata));
7a6c9792
DSH
1199 if (!kdata) {
1200 EVP_PKEY_free(pkey);
5824cc29 1201 return 0;
7a6c9792 1202 }
5824cc29
DSH
1203 kdata->ctx = NULL;
1204 kdata->input = NULL;
1205 kdata->output = NULL;
1206 kdata->keyop = keyop;
1207 t->data = kdata;
5824cc29
DSH
1208 kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL);
1209 if (!kdata->ctx)
1210 return 0;
1211 if (keyopinit(kdata->ctx) <= 0)
1212 return 0;
1213 return 1;
1214}
1215
1216static void pkey_test_cleanup(struct evp_test *t)
1217{
1218 struct pkey_data *kdata = t->data;
b548a1f1
RS
1219
1220 OPENSSL_free(kdata->input);
1221 OPENSSL_free(kdata->output);
c5ba2d99 1222 EVP_PKEY_CTX_free(kdata->ctx);
5824cc29
DSH
1223}
1224
1225static int pkey_test_parse(struct evp_test *t,
1226 const char *keyword, const char *value)
1227{
1228 struct pkey_data *kdata = t->data;
86885c28 1229 if (strcmp(keyword, "Input") == 0)
5824cc29 1230 return test_bin(value, &kdata->input, &kdata->input_len);
86885c28 1231 if (strcmp(keyword, "Output") == 0)
5824cc29 1232 return test_bin(value, &kdata->output, &kdata->output_len);
86885c28 1233 if (strcmp(keyword, "Ctrl") == 0) {
5824cc29
DSH
1234 char *p = strchr(value, ':');
1235 if (p)
1236 *p++ = 0;
1237 if (EVP_PKEY_CTX_ctrl_str(kdata->ctx, value, p) <= 0)
1238 return 0;
1239 return 1;
1240 }
1241 return 0;
1242}
1243
1244static int pkey_test_run(struct evp_test *t)
1245{
1246 struct pkey_data *kdata = t->data;
1247 unsigned char *out = NULL;
1248 size_t out_len;
1249 const char *err = "KEYOP_LENGTH_ERROR";
1250 if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1251 kdata->input_len) <= 0)
1252 goto err;
1253 out = OPENSSL_malloc(out_len);
1254 if (!out) {
1255 fprintf(stderr, "Error allocating output buffer!\n");
1256 exit(1);
1257 }
1258 err = "KEYOP_ERROR";
1259 if (kdata->keyop
1260 (kdata->ctx, out, &out_len, kdata->input, kdata->input_len) <= 0)
1261 goto err;
1262 err = "KEYOP_LENGTH_MISMATCH";
1263 if (out_len != kdata->output_len)
1264 goto err;
1265 err = "KEYOP_MISMATCH";
1266 if (check_output(t, kdata->output, out, out_len))
1267 goto err;
1268 err = NULL;
1269 err:
b548a1f1 1270 OPENSSL_free(out);
5824cc29
DSH
1271 t->err = err;
1272 return 1;
1273}
1274
1275static int sign_test_init(struct evp_test *t, const char *name)
1276{
1277 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1278}
1279
1280static const struct evp_test_method psign_test_method = {
1281 "Sign",
1282 sign_test_init,
1283 pkey_test_cleanup,
1284 pkey_test_parse,
1285 pkey_test_run
1286};
1287
1288static int verify_recover_test_init(struct evp_test *t, const char *name)
1289{
1290 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1291 EVP_PKEY_verify_recover);
1292}
1293
1294static const struct evp_test_method pverify_recover_test_method = {
1295 "VerifyRecover",
1296 verify_recover_test_init,
1297 pkey_test_cleanup,
1298 pkey_test_parse,
1299 pkey_test_run
1300};
1301
1302static int decrypt_test_init(struct evp_test *t, const char *name)
1303{
1304 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1305 EVP_PKEY_decrypt);
1306}
1307
1308static const struct evp_test_method pdecrypt_test_method = {
1309 "Decrypt",
1310 decrypt_test_init,
1311 pkey_test_cleanup,
1312 pkey_test_parse,
1313 pkey_test_run
1314};
1315
1316static int verify_test_init(struct evp_test *t, const char *name)
1317{
1318 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1319}
1320
1321static int verify_test_run(struct evp_test *t)
1322{
1323 struct pkey_data *kdata = t->data;
1324 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1325 kdata->input, kdata->input_len) <= 0)
1326 t->err = "VERIFY_ERROR";
1327 return 1;
1328}
1329
1330static const struct evp_test_method pverify_test_method = {
1331 "Verify",
1332 verify_test_init,
1333 pkey_test_cleanup,
1334 pkey_test_parse,
1335 verify_test_run
1336};
3b53e18a
DSH
1337
1338/* PBE tests */
1339
1340#define PBE_TYPE_SCRYPT 1
351fe214
DSH
1341#define PBE_TYPE_PBKDF2 2
1342#define PBE_TYPE_PKCS12 3
3b53e18a
DSH
1343
1344struct pbe_data {
1345
1346 int pbe_type;
1347
1348 /* scrypt parameters */
1349 uint64_t N, r, p, maxmem;
1350
351fe214
DSH
1351 /* PKCS#12 parameters */
1352 int id, iter;
1353 const EVP_MD *md;
1354
3b53e18a
DSH
1355 /* password */
1356 unsigned char *pass;
1357 size_t pass_len;
1358
1359 /* salt */
1360 unsigned char *salt;
1361 size_t salt_len;
1362
1363 /* Expected output */
1364 unsigned char *key;
1365 size_t key_len;
1366};
1367
b0809bc8 1368#ifndef OPENSSL_NO_SCRYPT
3b53e18a
DSH
1369static int scrypt_test_parse(struct evp_test *t,
1370 const char *keyword, const char *value)
1371{
1372 struct pbe_data *pdata = t->data;
351fe214 1373
3b53e18a
DSH
1374 if (strcmp(keyword, "N") == 0)
1375 return test_uint64(value, &pdata->N);
1376 if (strcmp(keyword, "p") == 0)
1377 return test_uint64(value, &pdata->p);
1378 if (strcmp(keyword, "r") == 0)
1379 return test_uint64(value, &pdata->r);
1380 if (strcmp(keyword, "maxmem") == 0)
1381 return test_uint64(value, &pdata->maxmem);
1382 return 0;
1383}
b0809bc8 1384#endif
3b53e18a 1385
351fe214
DSH
1386static int pbkdf2_test_parse(struct evp_test *t,
1387 const char *keyword, const char *value)
3b53e18a
DSH
1388{
1389 struct pbe_data *pdata = t->data;
351fe214
DSH
1390
1391 if (strcmp(keyword, "iter") == 0) {
1392 pdata->iter = atoi(value);
1393 if (pdata->iter <= 0)
1394 return 0;
1395 return 1;
1396 }
1397 if (strcmp(keyword, "MD") == 0) {
1398 pdata->md = EVP_get_digestbyname(value);
1399 if (pdata->md == NULL)
1400 return 0;
1401 return 1;
1402 }
1403 return 0;
1404}
1405
1406static int pkcs12_test_parse(struct evp_test *t,
1407 const char *keyword, const char *value)
1408{
1409 struct pbe_data *pdata = t->data;
1410
1411 if (strcmp(keyword, "id") == 0) {
1412 pdata->id = atoi(value);
1413 if (pdata->id <= 0)
1414 return 0;
1415 return 1;
1416 }
1417 return pbkdf2_test_parse(t, keyword, value);
3b53e18a
DSH
1418}
1419
1420static int pbe_test_init(struct evp_test *t, const char *alg)
1421{
1422 struct pbe_data *pdat;
1423 int pbe_type = 0;
351fe214 1424
b0809bc8 1425#ifndef OPENSSL_NO_SCRYPT
3b53e18a
DSH
1426 if (strcmp(alg, "scrypt") == 0)
1427 pbe_type = PBE_TYPE_SCRYPT;
b0809bc8 1428#endif
351fe214
DSH
1429 else if (strcmp(alg, "pbkdf2") == 0)
1430 pbe_type = PBE_TYPE_PBKDF2;
1431 else if (strcmp(alg, "pkcs12") == 0)
1432 pbe_type = PBE_TYPE_PKCS12;
3b53e18a
DSH
1433 else
1434 fprintf(stderr, "Unknown pbe algorithm %s\n", alg);
1435 pdat = OPENSSL_malloc(sizeof(*pdat));
1436 pdat->pbe_type = pbe_type;
1437 pdat->pass = NULL;
1438 pdat->salt = NULL;
1439 pdat->N = 0;
1440 pdat->r = 0;
1441 pdat->p = 0;
1442 pdat->maxmem = 0;
351fe214
DSH
1443 pdat->id = 0;
1444 pdat->iter = 0;
1445 pdat->md = NULL;
3b53e18a
DSH
1446 t->data = pdat;
1447 return 1;
1448}
1449
1450static void pbe_test_cleanup(struct evp_test *t)
1451{
1452 struct pbe_data *pdat = t->data;
1453 test_free(pdat->pass);
1454 test_free(pdat->salt);
1455 test_free(pdat->key);
1456}
1457
1458static int pbe_test_parse(struct evp_test *t,
1459 const char *keyword, const char *value)
1460{
1461 struct pbe_data *pdata = t->data;
351fe214 1462
3b53e18a
DSH
1463 if (strcmp(keyword, "Password") == 0)
1464 return test_bin(value, &pdata->pass, &pdata->pass_len);
1465 if (strcmp(keyword, "Salt") == 0)
1466 return test_bin(value, &pdata->salt, &pdata->salt_len);
1467 if (strcmp(keyword, "Key") == 0)
1468 return test_bin(value, &pdata->key, &pdata->key_len);
b0809bc8 1469 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
351fe214
DSH
1470 return pbkdf2_test_parse(t, keyword, value);
1471 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1472 return pkcs12_test_parse(t, keyword, value);
b0809bc8
RS
1473#ifndef OPENSSL_NO_SCRYPT
1474 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1475 return scrypt_test_parse(t, keyword, value);
1476#endif
3b53e18a
DSH
1477 return 0;
1478}
1479
1480static int pbe_test_run(struct evp_test *t)
1481{
1482 struct pbe_data *pdata = t->data;
351fe214
DSH
1483 const char *err = "INTERNAL_ERROR";
1484 unsigned char *key;
1485
1486 key = OPENSSL_malloc(pdata->key_len);
1487 if (!key)
1488 goto err;
1489 if (pdata->pbe_type == PBE_TYPE_PBKDF2) {
1490 err = "PBKDF2_ERROR";
1491 if (PKCS5_PBKDF2_HMAC((char *)pdata->pass, pdata->pass_len,
1492 pdata->salt, pdata->salt_len,
1493 pdata->iter, pdata->md,
1494 pdata->key_len, key) == 0)
1495 goto err;
b0809bc8 1496#ifndef OPENSSL_NO_SCRYPT
351fe214
DSH
1497 } else if (pdata->pbe_type == PBE_TYPE_SCRYPT) {
1498 err = "SCRYPT_ERROR";
1499 if (EVP_PBE_scrypt((const char *)pdata->pass, pdata->pass_len,
1500 pdata->salt, pdata->salt_len,
1501 pdata->N, pdata->r, pdata->p, pdata->maxmem,
1502 key, pdata->key_len) == 0)
1503 goto err;
b0809bc8 1504#endif
351fe214
DSH
1505 } else if (pdata->pbe_type == PBE_TYPE_PKCS12) {
1506 err = "PKCS12_ERROR";
1507 if (PKCS12_key_gen_uni(pdata->pass, pdata->pass_len,
1508 pdata->salt, pdata->salt_len,
1509 pdata->id, pdata->iter, pdata->key_len,
1510 key, pdata->md) == 0)
1511 goto err;
1512 }
1513 err = "KEY_MISMATCH";
1514 if (check_output(t, pdata->key, key, pdata->key_len))
1515 goto err;
1516 err = NULL;
1517 err:
1518 OPENSSL_free(key);
1519 t->err = err;
1520 return 1;
3b53e18a
DSH
1521}
1522
1523static const struct evp_test_method pbe_test_method = {
1524 "PBE",
1525 pbe_test_init,
1526 pbe_test_cleanup,
1527 pbe_test_parse,
1528 pbe_test_run
1529};
3cdd1e94
EK
1530
1531/* Base64 tests */
1532
1533typedef enum {
1534 BASE64_CANONICAL_ENCODING = 0,
1535 BASE64_VALID_ENCODING = 1,
1536 BASE64_INVALID_ENCODING = 2
1537} base64_encoding_type;
1538
1539struct encode_data {
1540 /* Input to encoding */
1541 unsigned char *input;
1542 size_t input_len;
1543 /* Expected output */
1544 unsigned char *output;
1545 size_t output_len;
1546 base64_encoding_type encoding;
1547};
1548
1549static int encode_test_init(struct evp_test *t, const char *encoding)
1550{
1551 struct encode_data *edata = OPENSSL_zalloc(sizeof(*edata));
1552
1553 if (strcmp(encoding, "canonical") == 0) {
1554 edata->encoding = BASE64_CANONICAL_ENCODING;
1555 } else if (strcmp(encoding, "valid") == 0) {
1556 edata->encoding = BASE64_VALID_ENCODING;
1557 } else if (strcmp(encoding, "invalid") == 0) {
1558 edata->encoding = BASE64_INVALID_ENCODING;
7644a9ae 1559 t->expected_err = OPENSSL_strdup("DECODE_ERROR");
3cdd1e94
EK
1560 if (t->expected_err == NULL)
1561 return 0;
1562 } else {
1563 fprintf(stderr, "Bad encoding: %s. Should be one of "
1564 "{canonical, valid, invalid}\n", encoding);
1565 return 0;
1566 }
1567 t->data = edata;
1568 return 1;
1569}
1570
1571static void encode_test_cleanup(struct evp_test *t)
1572{
1573 struct encode_data *edata = t->data;
1574 test_free(edata->input);
1575 test_free(edata->output);
1576 memset(edata, 0, sizeof(*edata));
1577}
1578
1579static int encode_test_parse(struct evp_test *t,
1580 const char *keyword, const char *value)
1581{
1582 struct encode_data *edata = t->data;
1583 if (strcmp(keyword, "Input") == 0)
1584 return test_bin(value, &edata->input, &edata->input_len);
1585 if (strcmp(keyword, "Output") == 0)
1586 return test_bin(value, &edata->output, &edata->output_len);
1587 return 0;
1588}
1589
1590static int encode_test_run(struct evp_test *t)
1591{
1592 struct encode_data *edata = t->data;
1593 unsigned char *encode_out = NULL, *decode_out = NULL;
1594 int output_len, chunk_len;
1595 const char *err = "INTERNAL_ERROR";
254b26af
RL
1596 EVP_ENCODE_CTX *decode_ctx = EVP_ENCODE_CTX_new();
1597
1598 if (decode_ctx == NULL)
1599 goto err;
3cdd1e94
EK
1600
1601 if (edata->encoding == BASE64_CANONICAL_ENCODING) {
254b26af
RL
1602 EVP_ENCODE_CTX *encode_ctx = EVP_ENCODE_CTX_new();
1603 if (encode_ctx == NULL)
1604 goto err;
3cdd1e94
EK
1605 encode_out = OPENSSL_malloc(EVP_ENCODE_LENGTH(edata->input_len));
1606 if (encode_out == NULL)
1607 goto err;
1608
254b26af
RL
1609 EVP_EncodeInit(encode_ctx);
1610 EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
3cdd1e94
EK
1611 edata->input, edata->input_len);
1612 output_len = chunk_len;
1613
254b26af 1614 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
3cdd1e94
EK
1615 output_len += chunk_len;
1616
254b26af
RL
1617 EVP_ENCODE_CTX_free(encode_ctx);
1618
3cdd1e94
EK
1619 if (check_var_length_output(t, edata->output, edata->output_len,
1620 encode_out, output_len)) {
1621 err = "BAD_ENCODING";
1622 goto err;
1623 }
1624 }
1625
1626 decode_out = OPENSSL_malloc(EVP_DECODE_LENGTH(edata->output_len));
1627 if (decode_out == NULL)
1628 goto err;
1629
254b26af
RL
1630 EVP_DecodeInit(decode_ctx);
1631 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, edata->output,
3cdd1e94
EK
1632 edata->output_len) < 0) {
1633 err = "DECODE_ERROR";
1634 goto err;
1635 }
1636 output_len = chunk_len;
1637
254b26af 1638 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
3cdd1e94
EK
1639 err = "DECODE_ERROR";
1640 goto err;
1641 }
1642 output_len += chunk_len;
1643
1644 if (edata->encoding != BASE64_INVALID_ENCODING &&
1645 check_var_length_output(t, edata->input, edata->input_len,
1646 decode_out, output_len)) {
1647 err = "BAD_DECODING";
1648 goto err;
1649 }
1650
1651 err = NULL;
1652 err:
1653 t->err = err;
1654 OPENSSL_free(encode_out);
1655 OPENSSL_free(decode_out);
254b26af 1656 EVP_ENCODE_CTX_free(decode_ctx);
3cdd1e94
EK
1657 return 1;
1658}
1659
1660static const struct evp_test_method encode_test_method = {
1661 "Encoding",
1662 encode_test_init,
1663 encode_test_cleanup,
1664 encode_test_parse,
1665 encode_test_run,
1666};