]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/evp_test.c
Optionally check for early data
[thirdparty/openssl.git] / test / evp_test.c
CommitLineData
0e360199 1/*
3f5616d7 2 * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved.
0e360199 3 *
440e5d80
RS
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
0e360199
BL
8 */
9
10#include <stdio.h>
11#include <string.h>
307e3978
DSH
12#include <stdlib.h>
13#include <ctype.h>
0e360199 14#include <openssl/evp.h>
5824cc29 15#include <openssl/pem.h>
0b13e9f0 16#include <openssl/err.h>
307e3978 17#include <openssl/x509v3.h>
351fe214 18#include <openssl/pkcs12.h>
44a284d2 19#include <openssl/kdf.h>
3b53e18a 20#include "internal/numbers.h"
0e360199 21
307e3978
DSH
22/* Remove spaces from beginning and end of a string */
23
24static void remove_space(char **pval)
0f113f3e 25{
533b178d 26 unsigned char *p = (unsigned char *)*pval, *beginning;
0f113f3e 27
307e3978
DSH
28 while (isspace(*p))
29 p++;
30
533b178d 31 *pval = (char *)(beginning = p);
307e3978
DSH
32
33 p = p + strlen(*pval) - 1;
34
35 /* Remove trailing space */
533b178d 36 while (p >= beginning && isspace(*p))
307e3978 37 *p-- = 0;
0f113f3e 38}
0e360199 39
307e3978
DSH
40/*
41 * Given a line of the form:
42 * name = value # comment
43 * extract name and value. NB: modifies passed buffer.
44 */
45
46static int parse_line(char **pkw, char **pval, char *linebuf)
0f113f3e 47{
307e3978 48 char *p;
0e360199 49
307e3978 50 p = linebuf + strlen(linebuf) - 1;
0f113f3e 51
307e3978
DSH
52 if (*p != '\n') {
53 fprintf(stderr, "FATAL: missing EOL\n");
54 exit(1);
0e360199
BL
55 }
56
307e3978 57 /* Look for # */
5b46eee0 58
307e3978 59 p = strchr(linebuf, '#');
5b46eee0 60
307e3978
DSH
61 if (p)
62 *p = '\0';
5b46eee0 63
307e3978
DSH
64 /* Look for = sign */
65 p = strchr(linebuf, '=');
5b46eee0 66
307e3978
DSH
67 /* If no '=' exit */
68 if (!p)
69 return 0;
5b46eee0 70
307e3978 71 *p++ = '\0';
5b46eee0 72
307e3978
DSH
73 *pkw = linebuf;
74 *pval = p;
5b46eee0 75
307e3978
DSH
76 /* Remove spaces from keyword and value */
77 remove_space(pkw);
78 remove_space(pval);
79
80 return 1;
0f113f3e 81}
0e360199 82
3cdd1e94
EK
83/*
84 * Unescape some escape sequences in string literals.
85 * Return the result in a newly allocated buffer.
86 * Currently only supports '\n'.
87 * If the input length is 0, returns a valid 1-byte buffer, but sets
88 * the length to 0.
89 */
90static unsigned char* unescape(const char *input, size_t input_len,
91 size_t *out_len)
92{
93 unsigned char *ret, *p;
94 size_t i;
95 if (input_len == 0) {
96 *out_len = 0;
97 return OPENSSL_zalloc(1);
98 }
99
100 /* Escaping is non-expanding; over-allocate original size for simplicity. */
101 ret = p = OPENSSL_malloc(input_len);
102 if (ret == NULL)
103 return NULL;
104
105 for (i = 0; i < input_len; i++) {
106 if (input[i] == '\\') {
107 if (i == input_len - 1 || input[i+1] != 'n')
108 goto err;
109 *p++ = '\n';
110 i++;
111 } else {
112 *p++ = input[i];
113 }
114 }
115
116 *out_len = p - ret;
117 return ret;
118
119 err:
120 OPENSSL_free(ret);
121 return NULL;
122}
123
307e3978
DSH
124/* For a hex string "value" convert to a binary allocated buffer */
125static int test_bin(const char *value, unsigned char **buf, size_t *buflen)
0f113f3e 126{
307e3978 127 long len;
bc9d9ce2
DSH
128
129 *buflen = 0;
fa013b65
PB
130
131 /* Check for empty value */
307e3978 132 if (!*value) {
3cdd1e94
EK
133 /*
134 * Don't return NULL for zero length buffer.
135 * This is needed for some tests with empty keys: HMAC_Init_ex() expects
136 * a non-NULL key buffer even if the key length is 0, in order to detect
137 * key reset.
138 */
307e3978
DSH
139 *buf = OPENSSL_malloc(1);
140 if (!*buf)
141 return 0;
142 **buf = 0;
143 *buflen = 0;
144 return 1;
145 }
fa013b65
PB
146
147 /* Check for NULL literal */
148 if (strcmp(value, "NULL") == 0) {
149 *buf = NULL;
150 *buflen = 0;
151 return 1;
152 }
153
83251f39
DSH
154 /* Check for string literal */
155 if (value[0] == '"') {
156 size_t vlen;
157 value++;
158 vlen = strlen(value);
159 if (value[vlen - 1] != '"')
160 return 0;
161 vlen--;
3cdd1e94
EK
162 *buf = unescape(value, vlen, buflen);
163 if (*buf == NULL)
164 return 0;
83251f39
DSH
165 return 1;
166 }
3cdd1e94 167
fa013b65 168 /* Otherwise assume as hex literal and convert it to binary buffer */
14f051a0 169 *buf = OPENSSL_hexstr2buf(value, &len);
307e3978
DSH
170 if (!*buf) {
171 fprintf(stderr, "Value=%s\n", value);
172 ERR_print_errors_fp(stderr);
173 return -1;
174 }
175 /* Size of input buffer means we'll never overflow */
176 *buflen = len;
177 return 1;
0f113f3e 178}
83bd31da
MC
179#ifndef OPENSSL_NO_SCRYPT
180/* Currently only used by scrypt tests */
3b53e18a
DSH
181/* Parse unsigned decimal 64 bit integer value */
182static int test_uint64(const char *value, uint64_t *pr)
183{
184 const char *p = value;
185 if (!*p) {
186 fprintf(stderr, "Invalid empty integer value\n");
187 return -1;
188 }
189 *pr = 0;
190 while (*p) {
191 if (*pr > UINT64_MAX/10) {
192 fprintf(stderr, "Integer string overflow value=%s\n", value);
193 return -1;
194 }
195 *pr *= 10;
196 if (*p < '0' || *p > '9') {
197 fprintf(stderr, "Invalid integer string value=%s\n", value);
198 return -1;
199 }
200 *pr += *p - '0';
201 p++;
202 }
203 return 1;
204}
83bd31da 205#endif
848f735a 206
307e3978
DSH
207/* Structure holding test information */
208struct evp_test {
5824cc29 209 /* file being read */
02b91dcf 210 BIO *in;
71f60ef3
DSH
211 /* temp memory BIO for reading in keys */
212 BIO *key;
5824cc29
DSH
213 /* List of public and private keys */
214 struct key_list *private;
215 struct key_list *public;
307e3978
DSH
216 /* method for this test */
217 const struct evp_test_method *meth;
218 /* current line being processed */
219 unsigned int line;
220 /* start line of current test */
221 unsigned int start_line;
222 /* Error string for test */
9a2d2fb3 223 const char *err, *aux_err;
307e3978
DSH
224 /* Expected error value of test */
225 char *expected_err;
99f2f1dc
DSH
226 /* Expected error function string */
227 char *func;
228 /* Expected error reason string */
229 char *reason;
307e3978
DSH
230 /* Number of tests */
231 int ntests;
232 /* Error count */
233 int errors;
7a6c9792
DSH
234 /* Number of tests skipped */
235 int nskip;
b033e5d5 236 /* If output mismatch expected and got value */
3cdd1e94
EK
237 unsigned char *out_received;
238 size_t out_received_len;
b033e5d5 239 unsigned char *out_expected;
3cdd1e94 240 size_t out_expected_len;
307e3978
DSH
241 /* test specific data */
242 void *data;
7a6c9792
DSH
243 /* Current test should be skipped */
244 int skip;
307e3978 245};
5824cc29
DSH
246
247struct key_list {
248 char *name;
249 EVP_PKEY *key;
250 struct key_list *next;
251};
252
307e3978
DSH
253/* Test method structure */
254struct evp_test_method {
255 /* Name of test as it appears in file */
256 const char *name;
257 /* Initialise test for "alg" */
258 int (*init) (struct evp_test * t, const char *alg);
259 /* Clean up method */
260 void (*cleanup) (struct evp_test * t);
261 /* Test specific name value pair processing */
262 int (*parse) (struct evp_test * t, const char *name, const char *value);
263 /* Run the test itself */
264 int (*run_test) (struct evp_test * t);
265};
266
267static const struct evp_test_method digest_test_method, cipher_test_method;
f9e31463 268static const struct evp_test_method mac_test_method;
5824cc29
DSH
269static const struct evp_test_method psign_test_method, pverify_test_method;
270static const struct evp_test_method pdecrypt_test_method;
271static const struct evp_test_method pverify_recover_test_method;
d4ad48d7 272static const struct evp_test_method pderive_test_method;
3b53e18a 273static const struct evp_test_method pbe_test_method;
3cdd1e94 274static const struct evp_test_method encode_test_method;
44a284d2 275static const struct evp_test_method kdf_test_method;
307e3978
DSH
276
277static const struct evp_test_method *evp_test_list[] = {
278 &digest_test_method,
279 &cipher_test_method,
83251f39 280 &mac_test_method,
5824cc29
DSH
281 &psign_test_method,
282 &pverify_test_method,
283 &pdecrypt_test_method,
284 &pverify_recover_test_method,
d4ad48d7 285 &pderive_test_method,
3b53e18a 286 &pbe_test_method,
3cdd1e94 287 &encode_test_method,
44a284d2 288 &kdf_test_method,
83251f39 289 NULL
307e3978
DSH
290};
291
292static const struct evp_test_method *evp_find_test(const char *name)
0f113f3e 293{
307e3978 294 const struct evp_test_method **tt;
86885c28 295
307e3978 296 for (tt = evp_test_list; *tt; tt++) {
86885c28 297 if (strcmp(name, (*tt)->name) == 0)
307e3978
DSH
298 return *tt;
299 }
300 return NULL;
0f113f3e
MC
301}
302
b033e5d5
DSH
303static void hex_print(const char *name, const unsigned char *buf, size_t len)
304{
305 size_t i;
306 fprintf(stderr, "%s ", name);
307 for (i = 0; i < len; i++)
308 fprintf(stderr, "%02X", buf[i]);
309 fputs("\n", stderr);
310}
311
5724bd49
DSH
312static void free_expected(struct evp_test *t)
313{
b548a1f1
RS
314 OPENSSL_free(t->expected_err);
315 t->expected_err = NULL;
99f2f1dc
DSH
316 OPENSSL_free(t->func);
317 t->func = NULL;
318 OPENSSL_free(t->reason);
319 t->reason = NULL;
25aaa98a 320 OPENSSL_free(t->out_expected);
3cdd1e94 321 OPENSSL_free(t->out_received);
25aaa98a 322 t->out_expected = NULL;
3cdd1e94
EK
323 t->out_received = NULL;
324 t->out_expected_len = 0;
325 t->out_received_len = 0;
326 /* Literals. */
327 t->err = NULL;
5724bd49
DSH
328}
329
b033e5d5
DSH
330static void print_expected(struct evp_test *t)
331{
3cdd1e94 332 if (t->out_expected == NULL && t->out_received == NULL)
b033e5d5 333 return;
3cdd1e94
EK
334 hex_print("Expected:", t->out_expected, t->out_expected_len);
335 hex_print("Got: ", t->out_received, t->out_received_len);
5724bd49 336 free_expected(t);
b033e5d5
DSH
337}
338
307e3978 339static int check_test_error(struct evp_test *t)
0f113f3e 340{
99f2f1dc
DSH
341 unsigned long err;
342 const char *func;
343 const char *reason;
307e3978
DSH
344 if (!t->err && !t->expected_err)
345 return 1;
346 if (t->err && !t->expected_err) {
9a2d2fb3
AP
347 if (t->aux_err != NULL) {
348 fprintf(stderr, "Test line %d(%s): unexpected error %s\n",
349 t->start_line, t->aux_err, t->err);
350 } else {
351 fprintf(stderr, "Test line %d: unexpected error %s\n",
352 t->start_line, t->err);
353 }
b033e5d5 354 print_expected(t);
307e3978 355 return 0;
0f113f3e 356 }
307e3978
DSH
357 if (!t->err && t->expected_err) {
358 fprintf(stderr, "Test line %d: succeeded expecting %s\n",
359 t->start_line, t->expected_err);
360 return 0;
361 }
99f2f1dc
DSH
362
363 if (strcmp(t->err, t->expected_err) != 0) {
364 fprintf(stderr, "Test line %d: expecting %s got %s\n",
365 t->start_line, t->expected_err, t->err);
366 return 0;
367 }
368
369 if (t->func == NULL && t->reason == NULL)
370 return 1;
371
372 if (t->func == NULL || t->reason == NULL) {
373 fprintf(stderr, "Test line %d: missing function or reason code\n",
374 t->start_line);
375 return 0;
376 }
377
378 err = ERR_peek_error();
379 if (err == 0) {
380 fprintf(stderr, "Test line %d, expected error \"%s:%s\" not set\n",
381 t->start_line, t->func, t->reason);
382 return 0;
383 }
384
385 func = ERR_func_error_string(err);
386 reason = ERR_reason_error_string(err);
387
cd3fe0e0
RL
388 if (func == NULL && reason == NULL) {
389 fprintf(stderr, "Test line %d: expected error \"%s:%s\", no strings available. Skipping...\n",
390 t->start_line, t->func, t->reason);
391 return 1;
392 }
393
99f2f1dc 394 if (strcmp(func, t->func) == 0 && strcmp(reason, t->reason) == 0)
307e3978 395 return 1;
544a2aea 396
99f2f1dc
DSH
397 fprintf(stderr, "Test line %d: expected error \"%s:%s\", got \"%s:%s\"\n",
398 t->start_line, t->func, t->reason, func, reason);
399
307e3978
DSH
400 return 0;
401}
0f113f3e 402
307e3978 403/* Setup a new test, run any existing test */
0f113f3e 404
307e3978
DSH
405static int setup_test(struct evp_test *t, const struct evp_test_method *tmeth)
406{
407 /* If we already have a test set up run it */
408 if (t->meth) {
409 t->ntests++;
7a6c9792
DSH
410 if (t->skip) {
411 t->nskip++;
024d681e
TS
412 } else {
413 /* run the test */
cce65266 414 if (t->err == NULL && t->meth->run_test(t) != 1) {
024d681e
TS
415 fprintf(stderr, "%s test error line %d\n",
416 t->meth->name, t->start_line);
417 return 0;
418 }
419 if (!check_test_error(t)) {
420 if (t->err)
421 ERR_print_errors_fp(stderr);
422 t->errors++;
423 }
0f113f3e 424 }
024d681e 425 /* clean it up */
307e3978 426 ERR_clear_error();
024d681e
TS
427 if (t->data != NULL) {
428 t->meth->cleanup(t);
429 OPENSSL_free(t->data);
430 t->data = NULL;
431 }
b548a1f1
RS
432 OPENSSL_free(t->expected_err);
433 t->expected_err = NULL;
5724bd49 434 free_expected(t);
307e3978
DSH
435 }
436 t->meth = tmeth;
437 return 1;
438}
0f113f3e 439
7a6c9792 440static int find_key(EVP_PKEY **ppk, const char *name, struct key_list *lst)
5824cc29
DSH
441{
442 for (; lst; lst = lst->next) {
86885c28 443 if (strcmp(lst->name, name) == 0) {
7a6c9792
DSH
444 if (ppk)
445 *ppk = lst->key;
446 return 1;
447 }
5824cc29 448 }
7a6c9792 449 return 0;
5824cc29
DSH
450}
451
452static void free_key_list(struct key_list *lst)
453{
d5ec8efc 454 while (lst != NULL) {
366448ec 455 struct key_list *ltmp;
5824cc29
DSH
456 EVP_PKEY_free(lst->key);
457 OPENSSL_free(lst->name);
366448ec
DSH
458 ltmp = lst->next;
459 OPENSSL_free(lst);
460 lst = ltmp;
5824cc29
DSH
461 }
462}
463
7a6c9792
DSH
464static int check_unsupported()
465{
466 long err = ERR_peek_error();
467 if (ERR_GET_LIB(err) == ERR_LIB_EVP
366448ec 468 && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
7a6c9792
DSH
469 ERR_clear_error();
470 return 1;
471 }
472 return 0;
473}
474
71f60ef3
DSH
475
476static int read_key(struct evp_test *t)
477{
478 char tmpbuf[80];
479 if (t->key == NULL)
480 t->key = BIO_new(BIO_s_mem());
481 else if (BIO_reset(t->key) <= 0)
482 return 0;
483 if (t->key == NULL) {
484 fprintf(stderr, "Error allocating key memory BIO\n");
485 return 0;
486 }
487 /* Read to PEM end line and place content in memory BIO */
488 while (BIO_gets(t->in, tmpbuf, sizeof(tmpbuf))) {
489 t->line++;
490 if (BIO_puts(t->key, tmpbuf) <= 0) {
491 fprintf(stderr, "Error writing to key memory BIO\n");
492 return 0;
493 }
494 if (strncmp(tmpbuf, "-----END", 8) == 0)
495 return 1;
496 }
497 fprintf(stderr, "Can't find key end\n");
498 return 0;
499}
500
307e3978
DSH
501static int process_test(struct evp_test *t, char *buf, int verbose)
502{
4c9b0a03 503 char *keyword = NULL, *value = NULL;
7a6c9792 504 int rv = 0, add_key = 0;
4c9b0a03 505 struct key_list **lst = NULL, *key = NULL;
5824cc29 506 EVP_PKEY *pk = NULL;
4c9b0a03 507 const struct evp_test_method *tmeth = NULL;
307e3978
DSH
508 if (verbose)
509 fputs(buf, stdout);
510 if (!parse_line(&keyword, &value, buf))
511 return 1;
86885c28 512 if (strcmp(keyword, "PrivateKey") == 0) {
71f60ef3
DSH
513 if (!read_key(t))
514 return 0;
515 pk = PEM_read_bio_PrivateKey(t->key, NULL, 0, NULL);
7a6c9792 516 if (pk == NULL && !check_unsupported()) {
5824cc29
DSH
517 fprintf(stderr, "Error reading private key %s\n", value);
518 ERR_print_errors_fp(stderr);
519 return 0;
520 }
521 lst = &t->private;
7a6c9792 522 add_key = 1;
5824cc29 523 }
86885c28 524 if (strcmp(keyword, "PublicKey") == 0) {
71f60ef3
DSH
525 if (!read_key(t))
526 return 0;
527 pk = PEM_read_bio_PUBKEY(t->key, NULL, 0, NULL);
7a6c9792 528 if (pk == NULL && !check_unsupported()) {
5824cc29
DSH
529 fprintf(stderr, "Error reading public key %s\n", value);
530 ERR_print_errors_fp(stderr);
531 return 0;
532 }
533 lst = &t->public;
7a6c9792 534 add_key = 1;
5824cc29
DSH
535 }
536 /* If we have a key add to list */
7a6c9792 537 if (add_key) {
7a6c9792 538 if (find_key(NULL, value, *lst)) {
5824cc29
DSH
539 fprintf(stderr, "Duplicate key %s\n", value);
540 return 0;
541 }
b4faea50 542 key = OPENSSL_malloc(sizeof(*key));
5824cc29
DSH
543 if (!key)
544 return 0;
7644a9ae 545 key->name = OPENSSL_strdup(value);
5824cc29
DSH
546 key->key = pk;
547 key->next = *lst;
548 *lst = key;
71f60ef3 549 return 1;
5824cc29
DSH
550 }
551
307e3978
DSH
552 /* See if keyword corresponds to a test start */
553 tmeth = evp_find_test(keyword);
554 if (tmeth) {
555 if (!setup_test(t, tmeth))
556 return 0;
557 t->start_line = t->line;
7a6c9792 558 t->skip = 0;
307e3978
DSH
559 if (!tmeth->init(t, value)) {
560 fprintf(stderr, "Unknown %s: %s\n", keyword, value);
561 return 0;
0f113f3e 562 }
307e3978 563 return 1;
7a6c9792
DSH
564 } else if (t->skip) {
565 return 1;
86885c28 566 } else if (strcmp(keyword, "Result") == 0) {
307e3978
DSH
567 if (t->expected_err) {
568 fprintf(stderr, "Line %d: multiple result lines\n", t->line);
569 return 0;
0f113f3e 570 }
7644a9ae 571 t->expected_err = OPENSSL_strdup(value);
99f2f1dc
DSH
572 if (t->expected_err == NULL)
573 return 0;
574 } else if (strcmp(keyword, "Function") == 0) {
575 if (t->func != NULL) {
576 fprintf(stderr, "Line %d: multiple function lines\n", t->line);
577 return 0;
578 }
579 t->func = OPENSSL_strdup(value);
580 if (t->func == NULL)
581 return 0;
582 } else if (strcmp(keyword, "Reason") == 0) {
583 if (t->reason != NULL) {
584 fprintf(stderr, "Line %d: multiple reason lines\n", t->line);
585 return 0;
586 }
587 t->reason = OPENSSL_strdup(value);
588 if (t->reason == NULL)
307e3978
DSH
589 return 0;
590 } else {
591 /* Must be test specific line: try to parse it */
592 if (t->meth)
593 rv = t->meth->parse(t, keyword, value);
594
595 if (rv == 0)
596 fprintf(stderr, "line %d: unexpected keyword %s\n",
597 t->line, keyword);
598
599 if (rv < 0)
600 fprintf(stderr, "line %d: error processing keyword %s\n",
601 t->line, keyword);
602 if (rv <= 0)
603 return 0;
0f113f3e 604 }
307e3978
DSH
605 return 1;
606}
0f113f3e 607
3cdd1e94
EK
608static int check_var_length_output(struct evp_test *t,
609 const unsigned char *expected,
610 size_t expected_len,
611 const unsigned char *received,
612 size_t received_len)
b033e5d5 613{
3cdd1e94
EK
614 if (expected_len == received_len &&
615 memcmp(expected, received, expected_len) == 0) {
b033e5d5 616 return 0;
3cdd1e94
EK
617 }
618
619 /* The result printing code expects a non-NULL buffer. */
7644a9ae 620 t->out_expected = OPENSSL_memdup(expected, expected_len ? expected_len : 1);
3cdd1e94 621 t->out_expected_len = expected_len;
7644a9ae 622 t->out_received = OPENSSL_memdup(received, received_len ? received_len : 1);
3cdd1e94
EK
623 t->out_received_len = received_len;
624 if (t->out_expected == NULL || t->out_received == NULL) {
b033e5d5
DSH
625 fprintf(stderr, "Memory allocation error!\n");
626 exit(1);
627 }
628 return 1;
629}
630
3cdd1e94
EK
631static int check_output(struct evp_test *t,
632 const unsigned char *expected,
633 const unsigned char *received,
634 size_t len)
635{
636 return check_var_length_output(t, expected, len, received, len);
637}
638
307e3978
DSH
639int main(int argc, char **argv)
640{
02b91dcf 641 BIO *in = NULL;
307e3978
DSH
642 char buf[10240];
643 struct evp_test t;
0f113f3e 644
b033e5d5
DSH
645 if (argc != 2) {
646 fprintf(stderr, "usage: evp_test testfile.txt\n");
647 return 1;
648 }
649
d5ec8efc
DSH
650 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
651
366448ec 652 memset(&t, 0, sizeof(t));
307e3978 653 t.start_line = -1;
fa013b65 654 in = BIO_new_file(argv[1], "rb");
ce7a2232
RS
655 if (in == NULL) {
656 fprintf(stderr, "Can't open %s for reading\n", argv[1]);
657 return 1;
658 }
5824cc29 659 t.in = in;
cce65266 660 t.err = NULL;
02b91dcf 661 while (BIO_gets(in, buf, sizeof(buf))) {
307e3978
DSH
662 t.line++;
663 if (!process_test(&t, buf, 0))
664 exit(1);
665 }
666 /* Run any final test we have */
667 if (!setup_test(&t, NULL))
668 exit(1);
7a6c9792
DSH
669 fprintf(stderr, "%d tests completed with %d errors, %d skipped\n",
670 t.ntests, t.errors, t.nskip);
5824cc29
DSH
671 free_key_list(t.public);
672 free_key_list(t.private);
71f60ef3 673 BIO_free(t.key);
02b91dcf 674 BIO_free(in);
8793f012 675
c2e27310 676#ifndef OPENSSL_NO_CRYPTO_MDEBUG
541e9565
DSH
677 if (CRYPTO_mem_leaks_fp(stderr) <= 0)
678 return 1;
7644a9ae 679#endif
6906a7c1
DSH
680 if (t.errors)
681 return 1;
307e3978 682 return 0;
0f113f3e
MC
683}
684
307e3978 685static void test_free(void *d)
0f113f3e 686{
b548a1f1 687 OPENSSL_free(d);
307e3978 688}
4897dc40 689
307e3978 690/* Message digest tests */
4897dc40 691
307e3978
DSH
692struct digest_data {
693 /* Digest this test is for */
694 const EVP_MD *digest;
695 /* Input to digest */
696 unsigned char *input;
697 size_t input_len;
618be04e
DSH
698 /* Repeat count for input */
699 size_t nrpt;
307e3978
DSH
700 /* Expected output */
701 unsigned char *output;
702 size_t output_len;
703};
4897dc40 704
307e3978
DSH
705static int digest_test_init(struct evp_test *t, const char *alg)
706{
707 const EVP_MD *digest;
070c2332 708 struct digest_data *mdat;
307e3978 709 digest = EVP_get_digestbyname(alg);
578ce42d
DSH
710 if (!digest) {
711 /* If alg has an OID assume disabled algorithm */
712 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
713 t->skip = 1;
714 return 1;
715 }
307e3978 716 return 0;
578ce42d 717 }
b4faea50 718 mdat = OPENSSL_malloc(sizeof(*mdat));
307e3978
DSH
719 mdat->digest = digest;
720 mdat->input = NULL;
721 mdat->output = NULL;
618be04e 722 mdat->nrpt = 1;
307e3978 723 t->data = mdat;
4897dc40 724 return 1;
0f113f3e 725}
4897dc40 726
307e3978
DSH
727static void digest_test_cleanup(struct evp_test *t)
728{
729 struct digest_data *mdat = t->data;
730 test_free(mdat->input);
731 test_free(mdat->output);
732}
733
734static int digest_test_parse(struct evp_test *t,
735 const char *keyword, const char *value)
736{
737 struct digest_data *mdata = t->data;
86885c28 738 if (strcmp(keyword, "Input") == 0)
307e3978 739 return test_bin(value, &mdata->input, &mdata->input_len);
86885c28 740 if (strcmp(keyword, "Output") == 0)
307e3978 741 return test_bin(value, &mdata->output, &mdata->output_len);
86885c28 742 if (strcmp(keyword, "Count") == 0) {
618be04e
DSH
743 long nrpt = atoi(value);
744 if (nrpt <= 0)
745 return 0;
746 mdata->nrpt = (size_t)nrpt;
747 return 1;
748 }
307e3978
DSH
749 return 0;
750}
751
752static int digest_test_run(struct evp_test *t)
0f113f3e 753{
307e3978 754 struct digest_data *mdata = t->data;
618be04e 755 size_t i;
307e3978
DSH
756 const char *err = "INTERNAL_ERROR";
757 EVP_MD_CTX *mctx;
4897dc40 758 unsigned char md[EVP_MAX_MD_SIZE];
307e3978 759 unsigned int md_len;
bfb0641f 760 mctx = EVP_MD_CTX_new();
307e3978
DSH
761 if (!mctx)
762 goto err;
763 err = "DIGESTINIT_ERROR";
764 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))
765 goto err;
766 err = "DIGESTUPDATE_ERROR";
618be04e
DSH
767 for (i = 0; i < mdata->nrpt; i++) {
768 if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))
769 goto err;
770 }
307e3978
DSH
771 err = "DIGESTFINAL_ERROR";
772 if (!EVP_DigestFinal(mctx, md, &md_len))
773 goto err;
774 err = "DIGEST_LENGTH_MISMATCH";
775 if (md_len != mdata->output_len)
776 goto err;
777 err = "DIGEST_MISMATCH";
b033e5d5 778 if (check_output(t, mdata->output, md, md_len))
307e3978
DSH
779 goto err;
780 err = NULL;
781 err:
bfb0641f 782 EVP_MD_CTX_free(mctx);
307e3978 783 t->err = err;
b033e5d5 784 return 1;
307e3978 785}
4897dc40 786
307e3978
DSH
787static const struct evp_test_method digest_test_method = {
788 "Digest",
789 digest_test_init,
790 digest_test_cleanup,
791 digest_test_parse,
792 digest_test_run
793};
794
795/* Cipher tests */
796struct cipher_data {
797 const EVP_CIPHER *cipher;
798 int enc;
2207ba7b 799 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
307e3978
DSH
800 int aead;
801 unsigned char *key;
802 size_t key_len;
803 unsigned char *iv;
804 size_t iv_len;
805 unsigned char *plaintext;
806 size_t plaintext_len;
807 unsigned char *ciphertext;
808 size_t ciphertext_len;
809 /* GCM, CCM only */
810 unsigned char *aad;
811 size_t aad_len;
812 unsigned char *tag;
813 size_t tag_len;
814};
815
816static int cipher_test_init(struct evp_test *t, const char *alg)
817{
818 const EVP_CIPHER *cipher;
819 struct cipher_data *cdat = t->data;
820 cipher = EVP_get_cipherbyname(alg);
33a89fa6
DSH
821 if (!cipher) {
822 /* If alg has an OID assume disabled algorithm */
823 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
824 t->skip = 1;
825 return 1;
826 }
0f113f3e 827 return 0;
33a89fa6 828 }
b4faea50 829 cdat = OPENSSL_malloc(sizeof(*cdat));
307e3978
DSH
830 cdat->cipher = cipher;
831 cdat->enc = -1;
832 cdat->key = NULL;
833 cdat->iv = NULL;
834 cdat->ciphertext = NULL;
835 cdat->plaintext = NULL;
836 cdat->aad = NULL;
837 cdat->tag = NULL;
838 t->data = cdat;
839 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
2207ba7b 840 || EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE
307e3978
DSH
841 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
842 cdat->aead = EVP_CIPHER_mode(cipher);
eb85cb86
AP
843 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
844 cdat->aead = -1;
307e3978
DSH
845 else
846 cdat->aead = 0;
4897dc40 847
307e3978
DSH
848 return 1;
849}
4897dc40 850
307e3978
DSH
851static void cipher_test_cleanup(struct evp_test *t)
852{
853 struct cipher_data *cdat = t->data;
854 test_free(cdat->key);
855 test_free(cdat->iv);
856 test_free(cdat->ciphertext);
857 test_free(cdat->plaintext);
858 test_free(cdat->aad);
859 test_free(cdat->tag);
860}
4897dc40 861
307e3978
DSH
862static int cipher_test_parse(struct evp_test *t, const char *keyword,
863 const char *value)
864{
865 struct cipher_data *cdat = t->data;
86885c28 866 if (strcmp(keyword, "Key") == 0)
307e3978 867 return test_bin(value, &cdat->key, &cdat->key_len);
86885c28 868 if (strcmp(keyword, "IV") == 0)
307e3978 869 return test_bin(value, &cdat->iv, &cdat->iv_len);
86885c28 870 if (strcmp(keyword, "Plaintext") == 0)
307e3978 871 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
86885c28 872 if (strcmp(keyword, "Ciphertext") == 0)
307e3978
DSH
873 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
874 if (cdat->aead) {
86885c28 875 if (strcmp(keyword, "AAD") == 0)
307e3978 876 return test_bin(value, &cdat->aad, &cdat->aad_len);
86885c28 877 if (strcmp(keyword, "Tag") == 0)
307e3978 878 return test_bin(value, &cdat->tag, &cdat->tag_len);
0f113f3e 879 }
4897dc40 880
86885c28
RS
881 if (strcmp(keyword, "Operation") == 0) {
882 if (strcmp(value, "ENCRYPT") == 0)
307e3978 883 cdat->enc = 1;
86885c28 884 else if (strcmp(value, "DECRYPT") == 0)
307e3978
DSH
885 cdat->enc = 0;
886 else
887 return 0;
888 return 1;
0f113f3e 889 }
307e3978 890 return 0;
0f113f3e 891}
4897dc40 892
9a2d2fb3 893static int cipher_test_enc(struct evp_test *t, int enc,
0b96d77a 894 size_t out_misalign, size_t inp_misalign, int frag)
0f113f3e 895{
307e3978
DSH
896 struct cipher_data *cdat = t->data;
897 unsigned char *in, *out, *tmp = NULL;
0b96d77a
MC
898 size_t in_len, out_len, donelen = 0;
899 int tmplen, chunklen, tmpflen;
307e3978
DSH
900 EVP_CIPHER_CTX *ctx = NULL;
901 const char *err;
902 err = "INTERNAL_ERROR";
903 ctx = EVP_CIPHER_CTX_new();
904 if (!ctx)
905 goto err;
906 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
907 if (enc) {
908 in = cdat->plaintext;
909 in_len = cdat->plaintext_len;
910 out = cdat->ciphertext;
911 out_len = cdat->ciphertext_len;
912 } else {
913 in = cdat->ciphertext;
914 in_len = cdat->ciphertext_len;
915 out = cdat->plaintext;
916 out_len = cdat->plaintext_len;
0f113f3e 917 }
ff715da4
AP
918 if (inp_misalign == (size_t)-1) {
919 /*
920 * Exercise in-place encryption
921 */
922 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
923 if (!tmp)
924 goto err;
925 in = memcpy(tmp + out_misalign, in, in_len);
926 } else {
927 inp_misalign += 16 - ((out_misalign + in_len) & 15);
928 /*
929 * 'tmp' will store both output and copy of input. We make the copy
930 * of input to specifically aligned part of 'tmp'. So we just
931 * figured out how much padding would ensure the required alignment,
932 * now we allocate extended buffer and finally copy the input just
933 * past inp_misalign in expression below. Output will be written
934 * past out_misalign...
935 */
936 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
937 inp_misalign + in_len);
938 if (!tmp)
939 goto err;
940 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
941 inp_misalign, in, in_len);
942 }
307e3978
DSH
943 err = "CIPHERINIT_ERROR";
944 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
945 goto err;
946 err = "INVALID_IV_LENGTH";
947 if (cdat->iv) {
2207ba7b
DSH
948 if (cdat->aead) {
949 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
307e3978
DSH
950 cdat->iv_len, 0))
951 goto err;
952 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
953 goto err;
0f113f3e 954 }
307e3978
DSH
955 if (cdat->aead) {
956 unsigned char *tag;
957 /*
2207ba7b
DSH
958 * If encrypting or OCB just set tag length initially, otherwise
959 * set tag length and value.
307e3978 960 */
2207ba7b 961 if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {
307e3978
DSH
962 err = "TAG_LENGTH_SET_ERROR";
963 tag = NULL;
0f113f3e 964 } else {
307e3978
DSH
965 err = "TAG_SET_ERROR";
966 tag = cdat->tag;
0f113f3e 967 }
2207ba7b
DSH
968 if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {
969 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
366448ec 970 cdat->tag_len, tag))
307e3978 971 goto err;
0f113f3e 972 }
307e3978 973 }
0f113f3e 974
307e3978
DSH
975 err = "INVALID_KEY_LENGTH";
976 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len))
977 goto err;
978 err = "KEY_SET_ERROR";
979 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1))
980 goto err;
981
2207ba7b
DSH
982 if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {
983 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
984 cdat->tag_len, cdat->tag)) {
366448ec
DSH
985 err = "TAG_SET_ERROR";
986 goto err;
2207ba7b
DSH
987 }
988 }
989
307e3978
DSH
990 if (cdat->aead == EVP_CIPH_CCM_MODE) {
991 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
992 err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
993 goto err;
0f113f3e
MC
994 }
995 }
307e3978 996 if (cdat->aad) {
0b96d77a
MC
997 err = "AAD_SET_ERROR";
998 if (!frag) {
999 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad,
1000 cdat->aad_len))
1001 goto err;
1002 } else {
1003 /*
1004 * Supply the AAD in chunks less than the block size where possible
1005 */
1006 if (cdat->aad_len > 0) {
1007 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad, 1))
1008 goto err;
1009 donelen++;
1010 }
1011 if (cdat->aad_len > 2) {
1012 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad + donelen,
1013 cdat->aad_len - 2))
1014 goto err;
1015 donelen += cdat->aad_len - 2;
1016 }
1017 if (cdat->aad_len > 1
1018 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
1019 cdat->aad + donelen, 1))
1020 goto err;
307e3978
DSH
1021 }
1022 }
1023 EVP_CIPHER_CTX_set_padding(ctx, 0);
1024 err = "CIPHERUPDATE_ERROR";
0b96d77a 1025 tmplen = 0;
0b96d77a
MC
1026 if (!frag) {
1027 /* We supply the data all in one go */
1028 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
1029 goto err;
1030 } else {
1031 /* Supply the data in chunks less than the block size where possible */
1032 if (in_len > 0) {
1033 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
1034 goto err;
1035 tmplen += chunklen;
ef055ec5
MC
1036 in++;
1037 in_len--;
0b96d77a 1038 }
ef055ec5 1039 if (in_len > 1) {
0b96d77a 1040 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
ef055ec5 1041 in, in_len - 1))
0b96d77a
MC
1042 goto err;
1043 tmplen += chunklen;
ef055ec5
MC
1044 in += in_len - 1;
1045 in_len = 1;
0b96d77a 1046 }
ef055ec5 1047 if (in_len > 0 ) {
0b96d77a 1048 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
ef055ec5 1049 in, 1))
0b96d77a
MC
1050 goto err;
1051 tmplen += chunklen;
1052 }
1053 }
00212c66
DSH
1054 err = "CIPHERFINAL_ERROR";
1055 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen))
1056 goto err;
307e3978
DSH
1057 err = "LENGTH_MISMATCH";
1058 if (out_len != (size_t)(tmplen + tmpflen))
1059 goto err;
1060 err = "VALUE_MISMATCH";
9a2d2fb3 1061 if (check_output(t, out, tmp + out_misalign, out_len))
307e3978
DSH
1062 goto err;
1063 if (enc && cdat->aead) {
1064 unsigned char rtag[16];
1065 if (cdat->tag_len > sizeof(rtag)) {
1066 err = "TAG_LENGTH_INTERNAL_ERROR";
1067 goto err;
1068 }
2207ba7b 1069 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
307e3978
DSH
1070 cdat->tag_len, rtag)) {
1071 err = "TAG_RETRIEVE_ERROR";
1072 goto err;
1073 }
b033e5d5 1074 if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {
307e3978
DSH
1075 err = "TAG_VALUE_MISMATCH";
1076 goto err;
1077 }
1078 }
1079 err = NULL;
1080 err:
b548a1f1 1081 OPENSSL_free(tmp);
307e3978
DSH
1082 EVP_CIPHER_CTX_free(ctx);
1083 t->err = err;
1084 return err ? 0 : 1;
1085}
0e360199 1086
307e3978
DSH
1087static int cipher_test_run(struct evp_test *t)
1088{
1089 struct cipher_data *cdat = t->data;
0b96d77a 1090 int rv, frag = 0;
9a2d2fb3
AP
1091 size_t out_misalign, inp_misalign;
1092
307e3978
DSH
1093 if (!cdat->key) {
1094 t->err = "NO_KEY";
1095 return 0;
1096 }
1097 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
1098 /* IV is optional and usually omitted in wrap mode */
1099 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
1100 t->err = "NO_IV";
1101 return 0;
1102 }
1103 }
1104 if (cdat->aead && !cdat->tag) {
1105 t->err = "NO_TAG";
1106 return 0;
1107 }
0b96d77a 1108 for (out_misalign = 0; out_misalign <= 1;) {
9a2d2fb3
AP
1109 static char aux_err[64];
1110 t->aux_err = aux_err;
ff715da4
AP
1111 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
1112 if (inp_misalign == (size_t)-1) {
1113 /* kludge: inp_misalign == -1 means "exercise in-place" */
0b96d77a
MC
1114 BIO_snprintf(aux_err, sizeof(aux_err),
1115 "%s in-place, %sfragmented",
1116 out_misalign ? "misaligned" : "aligned",
1117 frag ? "" : "not ");
ff715da4 1118 } else {
0b96d77a
MC
1119 BIO_snprintf(aux_err, sizeof(aux_err),
1120 "%s output and %s input, %sfragmented",
ff715da4 1121 out_misalign ? "misaligned" : "aligned",
0b96d77a
MC
1122 inp_misalign ? "misaligned" : "aligned",
1123 frag ? "" : "not ");
ff715da4 1124 }
9a2d2fb3 1125 if (cdat->enc) {
0b96d77a 1126 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
9a2d2fb3
AP
1127 /* Not fatal errors: return */
1128 if (rv != 1) {
1129 if (rv < 0)
1130 return 0;
1131 return 1;
1132 }
1133 }
1134 if (cdat->enc != 1) {
0b96d77a 1135 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
9a2d2fb3
AP
1136 /* Not fatal errors: return */
1137 if (rv != 1) {
1138 if (rv < 0)
1139 return 0;
1140 return 1;
1141 }
1142 }
307e3978 1143 }
0b96d77a
MC
1144
1145 if (out_misalign == 1 && frag == 0) {
1146 /*
1147 * XTS, CCM and Wrap modes have special requirements about input
1148 * lengths so we don't fragment for those
1149 */
1150 if (cdat->aead == EVP_CIPH_CCM_MODE
1151 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
1152 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
1153 break;
1154 out_misalign = 0;
1155 frag++;
1156 } else {
1157 out_misalign++;
1158 }
307e3978 1159 }
9a2d2fb3
AP
1160 t->aux_err = NULL;
1161
307e3978 1162 return 1;
0f113f3e 1163}
307e3978
DSH
1164
1165static const struct evp_test_method cipher_test_method = {
1166 "Cipher",
1167 cipher_test_init,
1168 cipher_test_cleanup,
1169 cipher_test_parse,
1170 cipher_test_run
1171};
83251f39
DSH
1172
1173struct mac_data {
1174 /* MAC type */
1175 int type;
1176 /* Algorithm string for this MAC */
1177 char *alg;
1178 /* MAC key */
1179 unsigned char *key;
1180 size_t key_len;
1181 /* Input to MAC */
1182 unsigned char *input;
1183 size_t input_len;
1184 /* Expected output */
1185 unsigned char *output;
1186 size_t output_len;
1187};
1188
1189static int mac_test_init(struct evp_test *t, const char *alg)
1190{
1191 int type;
1192 struct mac_data *mdat;
b4a3aeeb 1193 if (strcmp(alg, "HMAC") == 0) {
83251f39 1194 type = EVP_PKEY_HMAC;
b4a3aeeb
MC
1195 } else if (strcmp(alg, "CMAC") == 0) {
1196#ifndef OPENSSL_NO_CMAC
83251f39 1197 type = EVP_PKEY_CMAC;
b4a3aeeb
MC
1198#else
1199 t->skip = 1;
1200 return 1;
52ad5b60
TS
1201#endif
1202 } else if (strcmp(alg, "Poly1305") == 0) {
1203#ifndef OPENSSL_NO_POLY1305
1204 type = EVP_PKEY_POLY1305;
1205#else
1206 t->skip = 1;
1207 return 1;
3f5616d7
TS
1208#endif
1209 } else if (strcmp(alg, "SipHash") == 0) {
1210#ifndef OPENSSL_NO_SIPHASH
1211 type = EVP_PKEY_SIPHASH;
1212#else
1213 t->skip = 1;
1214 return 1;
b4a3aeeb
MC
1215#endif
1216 } else
83251f39
DSH
1217 return 0;
1218
b4faea50 1219 mdat = OPENSSL_malloc(sizeof(*mdat));
83251f39
DSH
1220 mdat->type = type;
1221 mdat->alg = NULL;
1222 mdat->key = NULL;
1223 mdat->input = NULL;
1224 mdat->output = NULL;
1225 t->data = mdat;
1226 return 1;
1227}
1228
1229static void mac_test_cleanup(struct evp_test *t)
1230{
1231 struct mac_data *mdat = t->data;
1232 test_free(mdat->alg);
1233 test_free(mdat->key);
1234 test_free(mdat->input);
1235 test_free(mdat->output);
1236}
1237
1238static int mac_test_parse(struct evp_test *t,
1239 const char *keyword, const char *value)
1240{
1241 struct mac_data *mdata = t->data;
86885c28 1242 if (strcmp(keyword, "Key") == 0)
83251f39 1243 return test_bin(value, &mdata->key, &mdata->key_len);
86885c28 1244 if (strcmp(keyword, "Algorithm") == 0) {
7644a9ae 1245 mdata->alg = OPENSSL_strdup(value);
83251f39
DSH
1246 if (!mdata->alg)
1247 return 0;
1248 return 1;
1249 }
86885c28 1250 if (strcmp(keyword, "Input") == 0)
83251f39 1251 return test_bin(value, &mdata->input, &mdata->input_len);
86885c28 1252 if (strcmp(keyword, "Output") == 0)
83251f39
DSH
1253 return test_bin(value, &mdata->output, &mdata->output_len);
1254 return 0;
1255}
1256
1257static int mac_test_run(struct evp_test *t)
1258{
1259 struct mac_data *mdata = t->data;
1260 const char *err = "INTERNAL_ERROR";
1261 EVP_MD_CTX *mctx = NULL;
1262 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1263 EVP_PKEY *key = NULL;
1264 const EVP_MD *md = NULL;
1265 unsigned char *mac = NULL;
1266 size_t mac_len;
1267
96bea000 1268#ifdef OPENSSL_NO_DES
31b69e9a 1269 if (mdata->alg != NULL && strstr(mdata->alg, "DES") != NULL) {
96bea000
MC
1270 /* Skip DES */
1271 err = NULL;
1272 goto err;
1273 }
1274#endif
1275
83251f39
DSH
1276 err = "MAC_PKEY_CTX_ERROR";
1277 genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL);
1278 if (!genctx)
1279 goto err;
1280
1281 err = "MAC_KEYGEN_INIT_ERROR";
1282 if (EVP_PKEY_keygen_init(genctx) <= 0)
1283 goto err;
1284 if (mdata->type == EVP_PKEY_CMAC) {
1285 err = "MAC_ALGORITHM_SET_ERROR";
1286 if (EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0)
1287 goto err;
1288 }
1289
1290 err = "MAC_KEY_SET_ERROR";
1291 if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0)
1292 goto err;
1293
1294 err = "MAC_KEY_GENERATE_ERROR";
1295 if (EVP_PKEY_keygen(genctx, &key) <= 0)
1296 goto err;
1297 if (mdata->type == EVP_PKEY_HMAC) {
1298 err = "MAC_ALGORITHM_SET_ERROR";
1299 md = EVP_get_digestbyname(mdata->alg);
1300 if (!md)
1301 goto err;
1302 }
bfb0641f 1303 mctx = EVP_MD_CTX_new();
83251f39
DSH
1304 if (!mctx)
1305 goto err;
1306 err = "DIGESTSIGNINIT_ERROR";
1307 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key))
1308 goto err;
1309
1310 err = "DIGESTSIGNUPDATE_ERROR";
1311 if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len))
1312 goto err;
1313 err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1314 if (!EVP_DigestSignFinal(mctx, NULL, &mac_len))
1315 goto err;
1316 mac = OPENSSL_malloc(mac_len);
1317 if (!mac) {
1318 fprintf(stderr, "Error allocating mac buffer!\n");
1319 exit(1);
1320 }
1321 if (!EVP_DigestSignFinal(mctx, mac, &mac_len))
1322 goto err;
1323 err = "MAC_LENGTH_MISMATCH";
1324 if (mac_len != mdata->output_len)
1325 goto err;
1326 err = "MAC_MISMATCH";
1327 if (check_output(t, mdata->output, mac, mac_len))
1328 goto err;
1329 err = NULL;
1330 err:
bfb0641f 1331 EVP_MD_CTX_free(mctx);
b548a1f1 1332 OPENSSL_free(mac);
c5ba2d99
RS
1333 EVP_PKEY_CTX_free(genctx);
1334 EVP_PKEY_free(key);
83251f39
DSH
1335 t->err = err;
1336 return 1;
1337}
1338
1339static const struct evp_test_method mac_test_method = {
1340 "MAC",
1341 mac_test_init,
1342 mac_test_cleanup,
1343 mac_test_parse,
1344 mac_test_run
1345};
5824cc29
DSH
1346
1347/*
1348 * Public key operations. These are all very similar and can share
1349 * a lot of common code.
1350 */
1351
1352struct pkey_data {
1353 /* Context for this operation */
1354 EVP_PKEY_CTX *ctx;
1355 /* Key operation to perform */
1356 int (*keyop) (EVP_PKEY_CTX *ctx,
1357 unsigned char *sig, size_t *siglen,
1358 const unsigned char *tbs, size_t tbslen);
1359 /* Input to MAC */
1360 unsigned char *input;
1361 size_t input_len;
1362 /* Expected output */
1363 unsigned char *output;
1364 size_t output_len;
1365};
1366
1367/*
1368 * Perform public key operation setup: lookup key, allocated ctx and call
1369 * the appropriate initialisation function
1370 */
1371static int pkey_test_init(struct evp_test *t, const char *name,
1372 int use_public,
1373 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1374 int (*keyop) (EVP_PKEY_CTX *ctx,
1375 unsigned char *sig, size_t *siglen,
1376 const unsigned char *tbs,
1377 size_t tbslen)
1378 )
1379{
1380 struct pkey_data *kdata;
1381 EVP_PKEY *pkey = NULL;
7a6c9792
DSH
1382 int rv = 0;
1383 if (use_public)
1384 rv = find_key(&pkey, name, t->public);
1385 if (!rv)
1386 rv = find_key(&pkey, name, t->private);
024d681e 1387 if (!rv || pkey == NULL) {
7a6c9792
DSH
1388 t->skip = 1;
1389 return 1;
1390 }
1391
b4faea50 1392 kdata = OPENSSL_malloc(sizeof(*kdata));
7a6c9792
DSH
1393 if (!kdata) {
1394 EVP_PKEY_free(pkey);
5824cc29 1395 return 0;
7a6c9792 1396 }
5824cc29
DSH
1397 kdata->ctx = NULL;
1398 kdata->input = NULL;
1399 kdata->output = NULL;
1400 kdata->keyop = keyop;
1401 t->data = kdata;
5824cc29
DSH
1402 kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL);
1403 if (!kdata->ctx)
1404 return 0;
1405 if (keyopinit(kdata->ctx) <= 0)
cce65266 1406 t->err = "KEYOP_INIT_ERROR";
5824cc29
DSH
1407 return 1;
1408}
1409
1410static void pkey_test_cleanup(struct evp_test *t)
1411{
1412 struct pkey_data *kdata = t->data;
b548a1f1
RS
1413
1414 OPENSSL_free(kdata->input);
1415 OPENSSL_free(kdata->output);
c5ba2d99 1416 EVP_PKEY_CTX_free(kdata->ctx);
5824cc29
DSH
1417}
1418
dfbdf4ab
RL
1419static int pkey_test_ctrl(struct evp_test *t, EVP_PKEY_CTX *pctx,
1420 const char *value)
4ddd5ace
DSH
1421{
1422 int rv;
1423 char *p, *tmpval;
1424
1425 tmpval = OPENSSL_strdup(value);
1426 if (tmpval == NULL)
1427 return 0;
1428 p = strchr(tmpval, ':');
1429 if (p != NULL)
1430 *p++ = 0;
1431 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
cce65266
DSH
1432 if (rv == -2) {
1433 t->err = "PKEY_CTRL_INVALID";
1434 rv = 1;
1435 } else if (p != NULL && rv <= 0) {
1436 /* If p has an OID and lookup fails assume disabled algorithm */
1437 int nid = OBJ_sn2nid(p);
1438 if (nid == NID_undef)
1439 nid = OBJ_ln2nid(p);
1440 if ((nid != NID_undef) && EVP_get_digestbynid(nid) == NULL &&
1441 EVP_get_cipherbynid(nid) == NULL) {
dfbdf4ab
RL
1442 t->skip = 1;
1443 rv = 1;
cce65266
DSH
1444 } else {
1445 t->err = "PKEY_CTRL_ERROR";
1446 rv = 1;
dfbdf4ab
RL
1447 }
1448 }
4ddd5ace
DSH
1449 OPENSSL_free(tmpval);
1450 return rv > 0;
1451}
1452
5824cc29
DSH
1453static int pkey_test_parse(struct evp_test *t,
1454 const char *keyword, const char *value)
1455{
1456 struct pkey_data *kdata = t->data;
86885c28 1457 if (strcmp(keyword, "Input") == 0)
5824cc29 1458 return test_bin(value, &kdata->input, &kdata->input_len);
86885c28 1459 if (strcmp(keyword, "Output") == 0)
5824cc29 1460 return test_bin(value, &kdata->output, &kdata->output_len);
4ddd5ace 1461 if (strcmp(keyword, "Ctrl") == 0)
dfbdf4ab 1462 return pkey_test_ctrl(t, kdata->ctx, value);
5824cc29
DSH
1463 return 0;
1464}
1465
1466static int pkey_test_run(struct evp_test *t)
1467{
1468 struct pkey_data *kdata = t->data;
1469 unsigned char *out = NULL;
1470 size_t out_len;
1471 const char *err = "KEYOP_LENGTH_ERROR";
1472 if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1473 kdata->input_len) <= 0)
1474 goto err;
1475 out = OPENSSL_malloc(out_len);
1476 if (!out) {
1477 fprintf(stderr, "Error allocating output buffer!\n");
1478 exit(1);
1479 }
1480 err = "KEYOP_ERROR";
1481 if (kdata->keyop
1482 (kdata->ctx, out, &out_len, kdata->input, kdata->input_len) <= 0)
1483 goto err;
1484 err = "KEYOP_LENGTH_MISMATCH";
1485 if (out_len != kdata->output_len)
1486 goto err;
1487 err = "KEYOP_MISMATCH";
1488 if (check_output(t, kdata->output, out, out_len))
1489 goto err;
1490 err = NULL;
1491 err:
b548a1f1 1492 OPENSSL_free(out);
5824cc29
DSH
1493 t->err = err;
1494 return 1;
1495}
1496
1497static int sign_test_init(struct evp_test *t, const char *name)
1498{
1499 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1500}
1501
1502static const struct evp_test_method psign_test_method = {
1503 "Sign",
1504 sign_test_init,
1505 pkey_test_cleanup,
1506 pkey_test_parse,
1507 pkey_test_run
1508};
1509
1510static int verify_recover_test_init(struct evp_test *t, const char *name)
1511{
1512 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1513 EVP_PKEY_verify_recover);
1514}
1515
1516static const struct evp_test_method pverify_recover_test_method = {
1517 "VerifyRecover",
1518 verify_recover_test_init,
1519 pkey_test_cleanup,
1520 pkey_test_parse,
1521 pkey_test_run
1522};
1523
1524static int decrypt_test_init(struct evp_test *t, const char *name)
1525{
1526 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1527 EVP_PKEY_decrypt);
1528}
1529
1530static const struct evp_test_method pdecrypt_test_method = {
1531 "Decrypt",
1532 decrypt_test_init,
1533 pkey_test_cleanup,
1534 pkey_test_parse,
1535 pkey_test_run
1536};
1537
1538static int verify_test_init(struct evp_test *t, const char *name)
1539{
1540 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1541}
1542
1543static int verify_test_run(struct evp_test *t)
1544{
1545 struct pkey_data *kdata = t->data;
1546 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1547 kdata->input, kdata->input_len) <= 0)
1548 t->err = "VERIFY_ERROR";
1549 return 1;
1550}
1551
1552static const struct evp_test_method pverify_test_method = {
1553 "Verify",
1554 verify_test_init,
1555 pkey_test_cleanup,
1556 pkey_test_parse,
1557 verify_test_run
1558};
3b53e18a 1559
d4ad48d7
DSH
1560
1561static int pderive_test_init(struct evp_test *t, const char *name)
1562{
1563 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1564}
1565
1566static int pderive_test_parse(struct evp_test *t,
1567 const char *keyword, const char *value)
1568{
1569 struct pkey_data *kdata = t->data;
1570
1571 if (strcmp(keyword, "PeerKey") == 0) {
1572 EVP_PKEY *peer;
1573 if (find_key(&peer, value, t->public) == 0)
1574 return 0;
1575 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1576 return 0;
1577 return 1;
1578 }
1579 if (strcmp(keyword, "SharedSecret") == 0)
1580 return test_bin(value, &kdata->output, &kdata->output_len);
4ddd5ace 1581 if (strcmp(keyword, "Ctrl") == 0)
dfbdf4ab 1582 return pkey_test_ctrl(t, kdata->ctx, value);
d4ad48d7
DSH
1583 return 0;
1584}
1585
1586static int pderive_test_run(struct evp_test *t)
1587{
1588 struct pkey_data *kdata = t->data;
1589 unsigned char *out = NULL;
1590 size_t out_len;
1591 const char *err = "INTERNAL_ERROR";
1592
1593 out_len = kdata->output_len;
1594 out = OPENSSL_malloc(out_len);
1595 if (!out) {
1596 fprintf(stderr, "Error allocating output buffer!\n");
1597 exit(1);
1598 }
1599 err = "DERIVE_ERROR";
1600 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0)
1601 goto err;
1602 err = "SHARED_SECRET_LENGTH_MISMATCH";
1603 if (out_len != kdata->output_len)
1604 goto err;
1605 err = "SHARED_SECRET_MISMATCH";
1606 if (check_output(t, kdata->output, out, out_len))
1607 goto err;
1608 err = NULL;
1609 err:
1610 OPENSSL_free(out);
1611 t->err = err;
1612 return 1;
1613}
1614
1615static const struct evp_test_method pderive_test_method = {
1616 "Derive",
1617 pderive_test_init,
1618 pkey_test_cleanup,
1619 pderive_test_parse,
1620 pderive_test_run
1621};
1622
3b53e18a
DSH
1623/* PBE tests */
1624
1625#define PBE_TYPE_SCRYPT 1
351fe214
DSH
1626#define PBE_TYPE_PBKDF2 2
1627#define PBE_TYPE_PKCS12 3
3b53e18a
DSH
1628
1629struct pbe_data {
1630
1631 int pbe_type;
1632
1633 /* scrypt parameters */
1634 uint64_t N, r, p, maxmem;
1635
351fe214
DSH
1636 /* PKCS#12 parameters */
1637 int id, iter;
1638 const EVP_MD *md;
1639
3b53e18a
DSH
1640 /* password */
1641 unsigned char *pass;
1642 size_t pass_len;
1643
1644 /* salt */
1645 unsigned char *salt;
1646 size_t salt_len;
1647
1648 /* Expected output */
1649 unsigned char *key;
1650 size_t key_len;
1651};
1652
b0809bc8 1653#ifndef OPENSSL_NO_SCRYPT
3b53e18a
DSH
1654static int scrypt_test_parse(struct evp_test *t,
1655 const char *keyword, const char *value)
1656{
1657 struct pbe_data *pdata = t->data;
351fe214 1658
3b53e18a
DSH
1659 if (strcmp(keyword, "N") == 0)
1660 return test_uint64(value, &pdata->N);
1661 if (strcmp(keyword, "p") == 0)
1662 return test_uint64(value, &pdata->p);
1663 if (strcmp(keyword, "r") == 0)
1664 return test_uint64(value, &pdata->r);
1665 if (strcmp(keyword, "maxmem") == 0)
1666 return test_uint64(value, &pdata->maxmem);
1667 return 0;
1668}
b0809bc8 1669#endif
3b53e18a 1670
351fe214
DSH
1671static int pbkdf2_test_parse(struct evp_test *t,
1672 const char *keyword, const char *value)
3b53e18a
DSH
1673{
1674 struct pbe_data *pdata = t->data;
351fe214
DSH
1675
1676 if (strcmp(keyword, "iter") == 0) {
1677 pdata->iter = atoi(value);
1678 if (pdata->iter <= 0)
1679 return 0;
1680 return 1;
1681 }
1682 if (strcmp(keyword, "MD") == 0) {
1683 pdata->md = EVP_get_digestbyname(value);
1684 if (pdata->md == NULL)
1685 return 0;
1686 return 1;
1687 }
1688 return 0;
1689}
1690
1691static int pkcs12_test_parse(struct evp_test *t,
1692 const char *keyword, const char *value)
1693{
1694 struct pbe_data *pdata = t->data;
1695
1696 if (strcmp(keyword, "id") == 0) {
1697 pdata->id = atoi(value);
1698 if (pdata->id <= 0)
1699 return 0;
1700 return 1;
1701 }
1702 return pbkdf2_test_parse(t, keyword, value);
3b53e18a
DSH
1703}
1704
1705static int pbe_test_init(struct evp_test *t, const char *alg)
1706{
1707 struct pbe_data *pdat;
1708 int pbe_type = 0;
351fe214 1709
83bd31da 1710 if (strcmp(alg, "scrypt") == 0) {
b0809bc8 1711#ifndef OPENSSL_NO_SCRYPT
3b53e18a 1712 pbe_type = PBE_TYPE_SCRYPT;
83bd31da
MC
1713#else
1714 t->skip = 1;
1715 return 1;
b0809bc8 1716#endif
83bd31da 1717 } else if (strcmp(alg, "pbkdf2") == 0) {
351fe214 1718 pbe_type = PBE_TYPE_PBKDF2;
83bd31da 1719 } else if (strcmp(alg, "pkcs12") == 0) {
351fe214 1720 pbe_type = PBE_TYPE_PKCS12;
83bd31da 1721 } else {
3b53e18a 1722 fprintf(stderr, "Unknown pbe algorithm %s\n", alg);
83bd31da 1723 }
3b53e18a
DSH
1724 pdat = OPENSSL_malloc(sizeof(*pdat));
1725 pdat->pbe_type = pbe_type;
1726 pdat->pass = NULL;
1727 pdat->salt = NULL;
1728 pdat->N = 0;
1729 pdat->r = 0;
1730 pdat->p = 0;
1731 pdat->maxmem = 0;
351fe214
DSH
1732 pdat->id = 0;
1733 pdat->iter = 0;
1734 pdat->md = NULL;
3b53e18a
DSH
1735 t->data = pdat;
1736 return 1;
1737}
1738
1739static void pbe_test_cleanup(struct evp_test *t)
1740{
1741 struct pbe_data *pdat = t->data;
1742 test_free(pdat->pass);
1743 test_free(pdat->salt);
1744 test_free(pdat->key);
1745}
1746
1747static int pbe_test_parse(struct evp_test *t,
1748 const char *keyword, const char *value)
1749{
1750 struct pbe_data *pdata = t->data;
351fe214 1751
3b53e18a
DSH
1752 if (strcmp(keyword, "Password") == 0)
1753 return test_bin(value, &pdata->pass, &pdata->pass_len);
1754 if (strcmp(keyword, "Salt") == 0)
1755 return test_bin(value, &pdata->salt, &pdata->salt_len);
1756 if (strcmp(keyword, "Key") == 0)
1757 return test_bin(value, &pdata->key, &pdata->key_len);
b0809bc8 1758 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
351fe214
DSH
1759 return pbkdf2_test_parse(t, keyword, value);
1760 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1761 return pkcs12_test_parse(t, keyword, value);
b0809bc8
RS
1762#ifndef OPENSSL_NO_SCRYPT
1763 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1764 return scrypt_test_parse(t, keyword, value);
1765#endif
3b53e18a
DSH
1766 return 0;
1767}
1768
1769static int pbe_test_run(struct evp_test *t)
1770{
1771 struct pbe_data *pdata = t->data;
351fe214
DSH
1772 const char *err = "INTERNAL_ERROR";
1773 unsigned char *key;
1774
1775 key = OPENSSL_malloc(pdata->key_len);
1776 if (!key)
1777 goto err;
1778 if (pdata->pbe_type == PBE_TYPE_PBKDF2) {
1779 err = "PBKDF2_ERROR";
1780 if (PKCS5_PBKDF2_HMAC((char *)pdata->pass, pdata->pass_len,
1781 pdata->salt, pdata->salt_len,
1782 pdata->iter, pdata->md,
1783 pdata->key_len, key) == 0)
1784 goto err;
b0809bc8 1785#ifndef OPENSSL_NO_SCRYPT
351fe214
DSH
1786 } else if (pdata->pbe_type == PBE_TYPE_SCRYPT) {
1787 err = "SCRYPT_ERROR";
1788 if (EVP_PBE_scrypt((const char *)pdata->pass, pdata->pass_len,
1789 pdata->salt, pdata->salt_len,
1790 pdata->N, pdata->r, pdata->p, pdata->maxmem,
1791 key, pdata->key_len) == 0)
1792 goto err;
b0809bc8 1793#endif
351fe214
DSH
1794 } else if (pdata->pbe_type == PBE_TYPE_PKCS12) {
1795 err = "PKCS12_ERROR";
1796 if (PKCS12_key_gen_uni(pdata->pass, pdata->pass_len,
1797 pdata->salt, pdata->salt_len,
1798 pdata->id, pdata->iter, pdata->key_len,
1799 key, pdata->md) == 0)
1800 goto err;
1801 }
1802 err = "KEY_MISMATCH";
1803 if (check_output(t, pdata->key, key, pdata->key_len))
1804 goto err;
1805 err = NULL;
1806 err:
1807 OPENSSL_free(key);
1808 t->err = err;
1809 return 1;
3b53e18a
DSH
1810}
1811
1812static const struct evp_test_method pbe_test_method = {
1813 "PBE",
1814 pbe_test_init,
1815 pbe_test_cleanup,
1816 pbe_test_parse,
1817 pbe_test_run
1818};
3cdd1e94
EK
1819
1820/* Base64 tests */
1821
1822typedef enum {
1823 BASE64_CANONICAL_ENCODING = 0,
1824 BASE64_VALID_ENCODING = 1,
1825 BASE64_INVALID_ENCODING = 2
1826} base64_encoding_type;
1827
1828struct encode_data {
1829 /* Input to encoding */
1830 unsigned char *input;
1831 size_t input_len;
1832 /* Expected output */
1833 unsigned char *output;
1834 size_t output_len;
1835 base64_encoding_type encoding;
1836};
1837
1838static int encode_test_init(struct evp_test *t, const char *encoding)
1839{
1840 struct encode_data *edata = OPENSSL_zalloc(sizeof(*edata));
1841
1842 if (strcmp(encoding, "canonical") == 0) {
1843 edata->encoding = BASE64_CANONICAL_ENCODING;
1844 } else if (strcmp(encoding, "valid") == 0) {
1845 edata->encoding = BASE64_VALID_ENCODING;
1846 } else if (strcmp(encoding, "invalid") == 0) {
1847 edata->encoding = BASE64_INVALID_ENCODING;
7644a9ae 1848 t->expected_err = OPENSSL_strdup("DECODE_ERROR");
3cdd1e94
EK
1849 if (t->expected_err == NULL)
1850 return 0;
1851 } else {
1852 fprintf(stderr, "Bad encoding: %s. Should be one of "
1853 "{canonical, valid, invalid}\n", encoding);
1854 return 0;
1855 }
1856 t->data = edata;
1857 return 1;
1858}
1859
1860static void encode_test_cleanup(struct evp_test *t)
1861{
1862 struct encode_data *edata = t->data;
1863 test_free(edata->input);
1864 test_free(edata->output);
1865 memset(edata, 0, sizeof(*edata));
1866}
1867
1868static int encode_test_parse(struct evp_test *t,
1869 const char *keyword, const char *value)
1870{
1871 struct encode_data *edata = t->data;
1872 if (strcmp(keyword, "Input") == 0)
1873 return test_bin(value, &edata->input, &edata->input_len);
1874 if (strcmp(keyword, "Output") == 0)
1875 return test_bin(value, &edata->output, &edata->output_len);
1876 return 0;
1877}
1878
1879static int encode_test_run(struct evp_test *t)
1880{
1881 struct encode_data *edata = t->data;
1882 unsigned char *encode_out = NULL, *decode_out = NULL;
1883 int output_len, chunk_len;
1884 const char *err = "INTERNAL_ERROR";
254b26af
RL
1885 EVP_ENCODE_CTX *decode_ctx = EVP_ENCODE_CTX_new();
1886
1887 if (decode_ctx == NULL)
1888 goto err;
3cdd1e94
EK
1889
1890 if (edata->encoding == BASE64_CANONICAL_ENCODING) {
254b26af
RL
1891 EVP_ENCODE_CTX *encode_ctx = EVP_ENCODE_CTX_new();
1892 if (encode_ctx == NULL)
1893 goto err;
3cdd1e94
EK
1894 encode_out = OPENSSL_malloc(EVP_ENCODE_LENGTH(edata->input_len));
1895 if (encode_out == NULL)
1896 goto err;
1897
254b26af
RL
1898 EVP_EncodeInit(encode_ctx);
1899 EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
3cdd1e94
EK
1900 edata->input, edata->input_len);
1901 output_len = chunk_len;
1902
254b26af 1903 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
3cdd1e94
EK
1904 output_len += chunk_len;
1905
254b26af
RL
1906 EVP_ENCODE_CTX_free(encode_ctx);
1907
3cdd1e94
EK
1908 if (check_var_length_output(t, edata->output, edata->output_len,
1909 encode_out, output_len)) {
1910 err = "BAD_ENCODING";
1911 goto err;
1912 }
1913 }
1914
1915 decode_out = OPENSSL_malloc(EVP_DECODE_LENGTH(edata->output_len));
1916 if (decode_out == NULL)
1917 goto err;
1918
254b26af
RL
1919 EVP_DecodeInit(decode_ctx);
1920 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, edata->output,
3cdd1e94
EK
1921 edata->output_len) < 0) {
1922 err = "DECODE_ERROR";
1923 goto err;
1924 }
1925 output_len = chunk_len;
1926
254b26af 1927 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
3cdd1e94
EK
1928 err = "DECODE_ERROR";
1929 goto err;
1930 }
1931 output_len += chunk_len;
1932
1933 if (edata->encoding != BASE64_INVALID_ENCODING &&
1934 check_var_length_output(t, edata->input, edata->input_len,
1935 decode_out, output_len)) {
1936 err = "BAD_DECODING";
1937 goto err;
1938 }
1939
1940 err = NULL;
1941 err:
1942 t->err = err;
1943 OPENSSL_free(encode_out);
1944 OPENSSL_free(decode_out);
254b26af 1945 EVP_ENCODE_CTX_free(decode_ctx);
3cdd1e94
EK
1946 return 1;
1947}
1948
1949static const struct evp_test_method encode_test_method = {
1950 "Encoding",
1951 encode_test_init,
1952 encode_test_cleanup,
1953 encode_test_parse,
1954 encode_test_run,
1955};
44a284d2 1956
7d04be79 1957/* KDF operations */
44a284d2
DSH
1958
1959struct kdf_data {
1960 /* Context for this operation */
1961 EVP_PKEY_CTX *ctx;
1962 /* Expected output */
1963 unsigned char *output;
1964 size_t output_len;
1965};
1966
1967/*
1968 * Perform public key operation setup: lookup key, allocated ctx and call
1969 * the appropriate initialisation function
1970 */
1971static int kdf_test_init(struct evp_test *t, const char *name)
1972{
1973 struct kdf_data *kdata;
1974
1975 kdata = OPENSSL_malloc(sizeof(*kdata));
1976 if (kdata == NULL)
1977 return 0;
1978 kdata->ctx = NULL;
1979 kdata->output = NULL;
1980 t->data = kdata;
1981 kdata->ctx = EVP_PKEY_CTX_new_id(OBJ_sn2nid(name), NULL);
1982 if (kdata->ctx == NULL)
1983 return 0;
1984 if (EVP_PKEY_derive_init(kdata->ctx) <= 0)
1985 return 0;
1986 return 1;
1987}
1988
1989static void kdf_test_cleanup(struct evp_test *t)
1990{
1991 struct kdf_data *kdata = t->data;
1992 OPENSSL_free(kdata->output);
1993 EVP_PKEY_CTX_free(kdata->ctx);
1994}
1995
44a284d2
DSH
1996static int kdf_test_parse(struct evp_test *t,
1997 const char *keyword, const char *value)
1998{
1999 struct kdf_data *kdata = t->data;
2000 if (strcmp(keyword, "Output") == 0)
2001 return test_bin(value, &kdata->output, &kdata->output_len);
7d04be79 2002 if (strncmp(keyword, "Ctrl", 4) == 0)
dfbdf4ab 2003 return pkey_test_ctrl(t, kdata->ctx, value);
44a284d2
DSH
2004 return 0;
2005}
2006
2007static int kdf_test_run(struct evp_test *t)
2008{
2009 struct kdf_data *kdata = t->data;
2010 unsigned char *out = NULL;
2011 size_t out_len = kdata->output_len;
2012 const char *err = "INTERNAL_ERROR";
2013 out = OPENSSL_malloc(out_len);
2014 if (!out) {
2015 fprintf(stderr, "Error allocating output buffer!\n");
2016 exit(1);
2017 }
2018 err = "KDF_DERIVE_ERROR";
2019 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0)
2020 goto err;
2021 err = "KDF_LENGTH_MISMATCH";
2022 if (out_len != kdata->output_len)
2023 goto err;
2024 err = "KDF_MISMATCH";
2025 if (check_output(t, kdata->output, out, out_len))
2026 goto err;
2027 err = NULL;
2028 err:
2029 OPENSSL_free(out);
2030 t->err = err;
2031 return 1;
2032}
2033
2034static const struct evp_test_method kdf_test_method = {
2035 "KDF",
2036 kdf_test_init,
2037 kdf_test_cleanup,
2038 kdf_test_parse,
2039 kdf_test_run
2040};