]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/evp_test.c
test: don't make it more complicated than necessary.
[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 }
0c44545c
RS
472#ifndef OPENSSL_NO_EC
473 /*
474 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
475 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
476 * disabled).
477 */
478 if (ERR_GET_LIB(err) == ERR_LIB_EC
479 && ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP) {
480 ERR_clear_error();
481 return 1;
482 }
483#endif /* OPENSSL_NO_EC */
7a6c9792
DSH
484 return 0;
485}
486
71f60ef3
DSH
487
488static int read_key(struct evp_test *t)
489{
490 char tmpbuf[80];
491 if (t->key == NULL)
492 t->key = BIO_new(BIO_s_mem());
493 else if (BIO_reset(t->key) <= 0)
494 return 0;
495 if (t->key == NULL) {
496 fprintf(stderr, "Error allocating key memory BIO\n");
497 return 0;
498 }
499 /* Read to PEM end line and place content in memory BIO */
500 while (BIO_gets(t->in, tmpbuf, sizeof(tmpbuf))) {
501 t->line++;
502 if (BIO_puts(t->key, tmpbuf) <= 0) {
503 fprintf(stderr, "Error writing to key memory BIO\n");
504 return 0;
505 }
506 if (strncmp(tmpbuf, "-----END", 8) == 0)
507 return 1;
508 }
509 fprintf(stderr, "Can't find key end\n");
510 return 0;
511}
512
307e3978
DSH
513static int process_test(struct evp_test *t, char *buf, int verbose)
514{
4c9b0a03 515 char *keyword = NULL, *value = NULL;
7a6c9792 516 int rv = 0, add_key = 0;
4c9b0a03 517 struct key_list **lst = NULL, *key = NULL;
5824cc29 518 EVP_PKEY *pk = NULL;
4c9b0a03 519 const struct evp_test_method *tmeth = NULL;
307e3978
DSH
520 if (verbose)
521 fputs(buf, stdout);
522 if (!parse_line(&keyword, &value, buf))
523 return 1;
86885c28 524 if (strcmp(keyword, "PrivateKey") == 0) {
71f60ef3
DSH
525 if (!read_key(t))
526 return 0;
527 pk = PEM_read_bio_PrivateKey(t->key, NULL, 0, NULL);
7a6c9792 528 if (pk == NULL && !check_unsupported()) {
5824cc29
DSH
529 fprintf(stderr, "Error reading private key %s\n", value);
530 ERR_print_errors_fp(stderr);
531 return 0;
532 }
533 lst = &t->private;
7a6c9792 534 add_key = 1;
5824cc29 535 }
86885c28 536 if (strcmp(keyword, "PublicKey") == 0) {
71f60ef3
DSH
537 if (!read_key(t))
538 return 0;
539 pk = PEM_read_bio_PUBKEY(t->key, NULL, 0, NULL);
7a6c9792 540 if (pk == NULL && !check_unsupported()) {
5824cc29
DSH
541 fprintf(stderr, "Error reading public key %s\n", value);
542 ERR_print_errors_fp(stderr);
543 return 0;
544 }
545 lst = &t->public;
7a6c9792 546 add_key = 1;
5824cc29
DSH
547 }
548 /* If we have a key add to list */
7a6c9792 549 if (add_key) {
7a6c9792 550 if (find_key(NULL, value, *lst)) {
5824cc29
DSH
551 fprintf(stderr, "Duplicate key %s\n", value);
552 return 0;
553 }
b4faea50 554 key = OPENSSL_malloc(sizeof(*key));
5824cc29
DSH
555 if (!key)
556 return 0;
7644a9ae 557 key->name = OPENSSL_strdup(value);
5824cc29
DSH
558 key->key = pk;
559 key->next = *lst;
560 *lst = key;
71f60ef3 561 return 1;
5824cc29
DSH
562 }
563
307e3978
DSH
564 /* See if keyword corresponds to a test start */
565 tmeth = evp_find_test(keyword);
566 if (tmeth) {
567 if (!setup_test(t, tmeth))
568 return 0;
569 t->start_line = t->line;
7a6c9792 570 t->skip = 0;
307e3978
DSH
571 if (!tmeth->init(t, value)) {
572 fprintf(stderr, "Unknown %s: %s\n", keyword, value);
573 return 0;
0f113f3e 574 }
307e3978 575 return 1;
7a6c9792
DSH
576 } else if (t->skip) {
577 return 1;
86885c28 578 } else if (strcmp(keyword, "Result") == 0) {
307e3978
DSH
579 if (t->expected_err) {
580 fprintf(stderr, "Line %d: multiple result lines\n", t->line);
581 return 0;
0f113f3e 582 }
7644a9ae 583 t->expected_err = OPENSSL_strdup(value);
99f2f1dc
DSH
584 if (t->expected_err == NULL)
585 return 0;
586 } else if (strcmp(keyword, "Function") == 0) {
587 if (t->func != NULL) {
588 fprintf(stderr, "Line %d: multiple function lines\n", t->line);
589 return 0;
590 }
591 t->func = OPENSSL_strdup(value);
592 if (t->func == NULL)
593 return 0;
594 } else if (strcmp(keyword, "Reason") == 0) {
595 if (t->reason != NULL) {
596 fprintf(stderr, "Line %d: multiple reason lines\n", t->line);
597 return 0;
598 }
599 t->reason = OPENSSL_strdup(value);
600 if (t->reason == NULL)
307e3978
DSH
601 return 0;
602 } else {
603 /* Must be test specific line: try to parse it */
604 if (t->meth)
605 rv = t->meth->parse(t, keyword, value);
606
607 if (rv == 0)
608 fprintf(stderr, "line %d: unexpected keyword %s\n",
609 t->line, keyword);
610
611 if (rv < 0)
612 fprintf(stderr, "line %d: error processing keyword %s\n",
613 t->line, keyword);
614 if (rv <= 0)
615 return 0;
0f113f3e 616 }
307e3978
DSH
617 return 1;
618}
0f113f3e 619
3cdd1e94
EK
620static int check_var_length_output(struct evp_test *t,
621 const unsigned char *expected,
622 size_t expected_len,
623 const unsigned char *received,
624 size_t received_len)
b033e5d5 625{
3cdd1e94
EK
626 if (expected_len == received_len &&
627 memcmp(expected, received, expected_len) == 0) {
b033e5d5 628 return 0;
3cdd1e94
EK
629 }
630
631 /* The result printing code expects a non-NULL buffer. */
7644a9ae 632 t->out_expected = OPENSSL_memdup(expected, expected_len ? expected_len : 1);
3cdd1e94 633 t->out_expected_len = expected_len;
7644a9ae 634 t->out_received = OPENSSL_memdup(received, received_len ? received_len : 1);
3cdd1e94
EK
635 t->out_received_len = received_len;
636 if (t->out_expected == NULL || t->out_received == NULL) {
b033e5d5
DSH
637 fprintf(stderr, "Memory allocation error!\n");
638 exit(1);
639 }
640 return 1;
641}
642
3cdd1e94
EK
643static int check_output(struct evp_test *t,
644 const unsigned char *expected,
645 const unsigned char *received,
646 size_t len)
647{
648 return check_var_length_output(t, expected, len, received, len);
649}
650
307e3978
DSH
651int main(int argc, char **argv)
652{
02b91dcf 653 BIO *in = NULL;
307e3978
DSH
654 char buf[10240];
655 struct evp_test t;
0f113f3e 656
b033e5d5
DSH
657 if (argc != 2) {
658 fprintf(stderr, "usage: evp_test testfile.txt\n");
659 return 1;
660 }
661
d5ec8efc
DSH
662 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
663
366448ec 664 memset(&t, 0, sizeof(t));
307e3978 665 t.start_line = -1;
fa013b65 666 in = BIO_new_file(argv[1], "rb");
ce7a2232
RS
667 if (in == NULL) {
668 fprintf(stderr, "Can't open %s for reading\n", argv[1]);
669 return 1;
670 }
5824cc29 671 t.in = in;
cce65266 672 t.err = NULL;
02b91dcf 673 while (BIO_gets(in, buf, sizeof(buf))) {
307e3978
DSH
674 t.line++;
675 if (!process_test(&t, buf, 0))
676 exit(1);
677 }
678 /* Run any final test we have */
679 if (!setup_test(&t, NULL))
680 exit(1);
7a6c9792
DSH
681 fprintf(stderr, "%d tests completed with %d errors, %d skipped\n",
682 t.ntests, t.errors, t.nskip);
5824cc29
DSH
683 free_key_list(t.public);
684 free_key_list(t.private);
71f60ef3 685 BIO_free(t.key);
02b91dcf 686 BIO_free(in);
8793f012 687
c2e27310 688#ifndef OPENSSL_NO_CRYPTO_MDEBUG
541e9565
DSH
689 if (CRYPTO_mem_leaks_fp(stderr) <= 0)
690 return 1;
7644a9ae 691#endif
6906a7c1
DSH
692 if (t.errors)
693 return 1;
307e3978 694 return 0;
0f113f3e
MC
695}
696
307e3978 697static void test_free(void *d)
0f113f3e 698{
b548a1f1 699 OPENSSL_free(d);
307e3978 700}
4897dc40 701
307e3978 702/* Message digest tests */
4897dc40 703
307e3978
DSH
704struct digest_data {
705 /* Digest this test is for */
706 const EVP_MD *digest;
707 /* Input to digest */
708 unsigned char *input;
709 size_t input_len;
618be04e
DSH
710 /* Repeat count for input */
711 size_t nrpt;
307e3978
DSH
712 /* Expected output */
713 unsigned char *output;
714 size_t output_len;
715};
4897dc40 716
307e3978
DSH
717static int digest_test_init(struct evp_test *t, const char *alg)
718{
719 const EVP_MD *digest;
070c2332 720 struct digest_data *mdat;
307e3978 721 digest = EVP_get_digestbyname(alg);
578ce42d
DSH
722 if (!digest) {
723 /* If alg has an OID assume disabled algorithm */
724 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
725 t->skip = 1;
726 return 1;
727 }
307e3978 728 return 0;
578ce42d 729 }
b4faea50 730 mdat = OPENSSL_malloc(sizeof(*mdat));
307e3978
DSH
731 mdat->digest = digest;
732 mdat->input = NULL;
733 mdat->output = NULL;
618be04e 734 mdat->nrpt = 1;
307e3978 735 t->data = mdat;
4897dc40 736 return 1;
0f113f3e 737}
4897dc40 738
307e3978
DSH
739static void digest_test_cleanup(struct evp_test *t)
740{
741 struct digest_data *mdat = t->data;
742 test_free(mdat->input);
743 test_free(mdat->output);
744}
745
746static int digest_test_parse(struct evp_test *t,
747 const char *keyword, const char *value)
748{
749 struct digest_data *mdata = t->data;
86885c28 750 if (strcmp(keyword, "Input") == 0)
307e3978 751 return test_bin(value, &mdata->input, &mdata->input_len);
86885c28 752 if (strcmp(keyword, "Output") == 0)
307e3978 753 return test_bin(value, &mdata->output, &mdata->output_len);
86885c28 754 if (strcmp(keyword, "Count") == 0) {
618be04e
DSH
755 long nrpt = atoi(value);
756 if (nrpt <= 0)
757 return 0;
758 mdata->nrpt = (size_t)nrpt;
759 return 1;
760 }
307e3978
DSH
761 return 0;
762}
763
764static int digest_test_run(struct evp_test *t)
0f113f3e 765{
307e3978 766 struct digest_data *mdata = t->data;
618be04e 767 size_t i;
307e3978
DSH
768 const char *err = "INTERNAL_ERROR";
769 EVP_MD_CTX *mctx;
4897dc40 770 unsigned char md[EVP_MAX_MD_SIZE];
307e3978 771 unsigned int md_len;
bfb0641f 772 mctx = EVP_MD_CTX_new();
307e3978
DSH
773 if (!mctx)
774 goto err;
775 err = "DIGESTINIT_ERROR";
776 if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))
777 goto err;
778 err = "DIGESTUPDATE_ERROR";
618be04e
DSH
779 for (i = 0; i < mdata->nrpt; i++) {
780 if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))
781 goto err;
782 }
307e3978
DSH
783 err = "DIGESTFINAL_ERROR";
784 if (!EVP_DigestFinal(mctx, md, &md_len))
785 goto err;
786 err = "DIGEST_LENGTH_MISMATCH";
787 if (md_len != mdata->output_len)
788 goto err;
789 err = "DIGEST_MISMATCH";
b033e5d5 790 if (check_output(t, mdata->output, md, md_len))
307e3978
DSH
791 goto err;
792 err = NULL;
793 err:
bfb0641f 794 EVP_MD_CTX_free(mctx);
307e3978 795 t->err = err;
b033e5d5 796 return 1;
307e3978 797}
4897dc40 798
307e3978
DSH
799static const struct evp_test_method digest_test_method = {
800 "Digest",
801 digest_test_init,
802 digest_test_cleanup,
803 digest_test_parse,
804 digest_test_run
805};
806
807/* Cipher tests */
808struct cipher_data {
809 const EVP_CIPHER *cipher;
810 int enc;
2207ba7b 811 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
307e3978
DSH
812 int aead;
813 unsigned char *key;
814 size_t key_len;
815 unsigned char *iv;
816 size_t iv_len;
817 unsigned char *plaintext;
818 size_t plaintext_len;
819 unsigned char *ciphertext;
820 size_t ciphertext_len;
821 /* GCM, CCM only */
822 unsigned char *aad;
823 size_t aad_len;
824 unsigned char *tag;
825 size_t tag_len;
826};
827
828static int cipher_test_init(struct evp_test *t, const char *alg)
829{
830 const EVP_CIPHER *cipher;
831 struct cipher_data *cdat = t->data;
832 cipher = EVP_get_cipherbyname(alg);
33a89fa6
DSH
833 if (!cipher) {
834 /* If alg has an OID assume disabled algorithm */
835 if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
836 t->skip = 1;
837 return 1;
838 }
0f113f3e 839 return 0;
33a89fa6 840 }
b4faea50 841 cdat = OPENSSL_malloc(sizeof(*cdat));
307e3978
DSH
842 cdat->cipher = cipher;
843 cdat->enc = -1;
844 cdat->key = NULL;
845 cdat->iv = NULL;
846 cdat->ciphertext = NULL;
847 cdat->plaintext = NULL;
848 cdat->aad = NULL;
849 cdat->tag = NULL;
850 t->data = cdat;
851 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
2207ba7b 852 || EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE
307e3978
DSH
853 || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
854 cdat->aead = EVP_CIPHER_mode(cipher);
eb85cb86
AP
855 else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
856 cdat->aead = -1;
307e3978
DSH
857 else
858 cdat->aead = 0;
4897dc40 859
307e3978
DSH
860 return 1;
861}
4897dc40 862
307e3978
DSH
863static void cipher_test_cleanup(struct evp_test *t)
864{
865 struct cipher_data *cdat = t->data;
866 test_free(cdat->key);
867 test_free(cdat->iv);
868 test_free(cdat->ciphertext);
869 test_free(cdat->plaintext);
870 test_free(cdat->aad);
871 test_free(cdat->tag);
872}
4897dc40 873
307e3978
DSH
874static int cipher_test_parse(struct evp_test *t, const char *keyword,
875 const char *value)
876{
877 struct cipher_data *cdat = t->data;
86885c28 878 if (strcmp(keyword, "Key") == 0)
307e3978 879 return test_bin(value, &cdat->key, &cdat->key_len);
86885c28 880 if (strcmp(keyword, "IV") == 0)
307e3978 881 return test_bin(value, &cdat->iv, &cdat->iv_len);
86885c28 882 if (strcmp(keyword, "Plaintext") == 0)
307e3978 883 return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
86885c28 884 if (strcmp(keyword, "Ciphertext") == 0)
307e3978
DSH
885 return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
886 if (cdat->aead) {
86885c28 887 if (strcmp(keyword, "AAD") == 0)
307e3978 888 return test_bin(value, &cdat->aad, &cdat->aad_len);
86885c28 889 if (strcmp(keyword, "Tag") == 0)
307e3978 890 return test_bin(value, &cdat->tag, &cdat->tag_len);
0f113f3e 891 }
4897dc40 892
86885c28
RS
893 if (strcmp(keyword, "Operation") == 0) {
894 if (strcmp(value, "ENCRYPT") == 0)
307e3978 895 cdat->enc = 1;
86885c28 896 else if (strcmp(value, "DECRYPT") == 0)
307e3978
DSH
897 cdat->enc = 0;
898 else
899 return 0;
900 return 1;
0f113f3e 901 }
307e3978 902 return 0;
0f113f3e 903}
4897dc40 904
9a2d2fb3 905static int cipher_test_enc(struct evp_test *t, int enc,
0b96d77a 906 size_t out_misalign, size_t inp_misalign, int frag)
0f113f3e 907{
307e3978
DSH
908 struct cipher_data *cdat = t->data;
909 unsigned char *in, *out, *tmp = NULL;
0b96d77a
MC
910 size_t in_len, out_len, donelen = 0;
911 int tmplen, chunklen, tmpflen;
307e3978
DSH
912 EVP_CIPHER_CTX *ctx = NULL;
913 const char *err;
914 err = "INTERNAL_ERROR";
915 ctx = EVP_CIPHER_CTX_new();
916 if (!ctx)
917 goto err;
918 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
919 if (enc) {
920 in = cdat->plaintext;
921 in_len = cdat->plaintext_len;
922 out = cdat->ciphertext;
923 out_len = cdat->ciphertext_len;
924 } else {
925 in = cdat->ciphertext;
926 in_len = cdat->ciphertext_len;
927 out = cdat->plaintext;
928 out_len = cdat->plaintext_len;
0f113f3e 929 }
ff715da4
AP
930 if (inp_misalign == (size_t)-1) {
931 /*
932 * Exercise in-place encryption
933 */
934 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
935 if (!tmp)
936 goto err;
937 in = memcpy(tmp + out_misalign, in, in_len);
938 } else {
939 inp_misalign += 16 - ((out_misalign + in_len) & 15);
940 /*
941 * 'tmp' will store both output and copy of input. We make the copy
942 * of input to specifically aligned part of 'tmp'. So we just
943 * figured out how much padding would ensure the required alignment,
944 * now we allocate extended buffer and finally copy the input just
945 * past inp_misalign in expression below. Output will be written
946 * past out_misalign...
947 */
948 tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
949 inp_misalign + in_len);
950 if (!tmp)
951 goto err;
952 in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
953 inp_misalign, in, in_len);
954 }
307e3978
DSH
955 err = "CIPHERINIT_ERROR";
956 if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
957 goto err;
958 err = "INVALID_IV_LENGTH";
959 if (cdat->iv) {
2207ba7b
DSH
960 if (cdat->aead) {
961 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
307e3978
DSH
962 cdat->iv_len, 0))
963 goto err;
964 } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
965 goto err;
0f113f3e 966 }
307e3978
DSH
967 if (cdat->aead) {
968 unsigned char *tag;
969 /*
2207ba7b
DSH
970 * If encrypting or OCB just set tag length initially, otherwise
971 * set tag length and value.
307e3978 972 */
2207ba7b 973 if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {
307e3978
DSH
974 err = "TAG_LENGTH_SET_ERROR";
975 tag = NULL;
0f113f3e 976 } else {
307e3978
DSH
977 err = "TAG_SET_ERROR";
978 tag = cdat->tag;
0f113f3e 979 }
2207ba7b
DSH
980 if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {
981 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
366448ec 982 cdat->tag_len, tag))
307e3978 983 goto err;
0f113f3e 984 }
307e3978 985 }
0f113f3e 986
307e3978
DSH
987 err = "INVALID_KEY_LENGTH";
988 if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len))
989 goto err;
990 err = "KEY_SET_ERROR";
991 if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1))
992 goto err;
993
2207ba7b
DSH
994 if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {
995 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
996 cdat->tag_len, cdat->tag)) {
366448ec
DSH
997 err = "TAG_SET_ERROR";
998 goto err;
2207ba7b
DSH
999 }
1000 }
1001
307e3978
DSH
1002 if (cdat->aead == EVP_CIPH_CCM_MODE) {
1003 if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
1004 err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
1005 goto err;
0f113f3e
MC
1006 }
1007 }
307e3978 1008 if (cdat->aad) {
0b96d77a
MC
1009 err = "AAD_SET_ERROR";
1010 if (!frag) {
1011 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad,
1012 cdat->aad_len))
1013 goto err;
1014 } else {
1015 /*
1016 * Supply the AAD in chunks less than the block size where possible
1017 */
1018 if (cdat->aad_len > 0) {
1019 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad, 1))
1020 goto err;
1021 donelen++;
1022 }
1023 if (cdat->aad_len > 2) {
1024 if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad + donelen,
1025 cdat->aad_len - 2))
1026 goto err;
1027 donelen += cdat->aad_len - 2;
1028 }
1029 if (cdat->aad_len > 1
1030 && !EVP_CipherUpdate(ctx, NULL, &chunklen,
1031 cdat->aad + donelen, 1))
1032 goto err;
307e3978
DSH
1033 }
1034 }
1035 EVP_CIPHER_CTX_set_padding(ctx, 0);
1036 err = "CIPHERUPDATE_ERROR";
0b96d77a 1037 tmplen = 0;
0b96d77a
MC
1038 if (!frag) {
1039 /* We supply the data all in one go */
1040 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
1041 goto err;
1042 } else {
1043 /* Supply the data in chunks less than the block size where possible */
1044 if (in_len > 0) {
1045 if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
1046 goto err;
1047 tmplen += chunklen;
ef055ec5
MC
1048 in++;
1049 in_len--;
0b96d77a 1050 }
ef055ec5 1051 if (in_len > 1) {
0b96d77a 1052 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
ef055ec5 1053 in, in_len - 1))
0b96d77a
MC
1054 goto err;
1055 tmplen += chunklen;
ef055ec5
MC
1056 in += in_len - 1;
1057 in_len = 1;
0b96d77a 1058 }
ef055ec5 1059 if (in_len > 0 ) {
0b96d77a 1060 if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
ef055ec5 1061 in, 1))
0b96d77a
MC
1062 goto err;
1063 tmplen += chunklen;
1064 }
1065 }
00212c66
DSH
1066 err = "CIPHERFINAL_ERROR";
1067 if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen))
1068 goto err;
307e3978
DSH
1069 err = "LENGTH_MISMATCH";
1070 if (out_len != (size_t)(tmplen + tmpflen))
1071 goto err;
1072 err = "VALUE_MISMATCH";
9a2d2fb3 1073 if (check_output(t, out, tmp + out_misalign, out_len))
307e3978
DSH
1074 goto err;
1075 if (enc && cdat->aead) {
1076 unsigned char rtag[16];
1077 if (cdat->tag_len > sizeof(rtag)) {
1078 err = "TAG_LENGTH_INTERNAL_ERROR";
1079 goto err;
1080 }
2207ba7b 1081 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
307e3978
DSH
1082 cdat->tag_len, rtag)) {
1083 err = "TAG_RETRIEVE_ERROR";
1084 goto err;
1085 }
b033e5d5 1086 if (check_output(t, cdat->tag, rtag, cdat->tag_len)) {
307e3978
DSH
1087 err = "TAG_VALUE_MISMATCH";
1088 goto err;
1089 }
1090 }
1091 err = NULL;
1092 err:
b548a1f1 1093 OPENSSL_free(tmp);
307e3978
DSH
1094 EVP_CIPHER_CTX_free(ctx);
1095 t->err = err;
1096 return err ? 0 : 1;
1097}
0e360199 1098
307e3978
DSH
1099static int cipher_test_run(struct evp_test *t)
1100{
1101 struct cipher_data *cdat = t->data;
0b96d77a 1102 int rv, frag = 0;
9a2d2fb3
AP
1103 size_t out_misalign, inp_misalign;
1104
307e3978
DSH
1105 if (!cdat->key) {
1106 t->err = "NO_KEY";
1107 return 0;
1108 }
1109 if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
1110 /* IV is optional and usually omitted in wrap mode */
1111 if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
1112 t->err = "NO_IV";
1113 return 0;
1114 }
1115 }
1116 if (cdat->aead && !cdat->tag) {
1117 t->err = "NO_TAG";
1118 return 0;
1119 }
0b96d77a 1120 for (out_misalign = 0; out_misalign <= 1;) {
9a2d2fb3
AP
1121 static char aux_err[64];
1122 t->aux_err = aux_err;
ff715da4
AP
1123 for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
1124 if (inp_misalign == (size_t)-1) {
1125 /* kludge: inp_misalign == -1 means "exercise in-place" */
0b96d77a
MC
1126 BIO_snprintf(aux_err, sizeof(aux_err),
1127 "%s in-place, %sfragmented",
1128 out_misalign ? "misaligned" : "aligned",
1129 frag ? "" : "not ");
ff715da4 1130 } else {
0b96d77a
MC
1131 BIO_snprintf(aux_err, sizeof(aux_err),
1132 "%s output and %s input, %sfragmented",
ff715da4 1133 out_misalign ? "misaligned" : "aligned",
0b96d77a
MC
1134 inp_misalign ? "misaligned" : "aligned",
1135 frag ? "" : "not ");
ff715da4 1136 }
9a2d2fb3 1137 if (cdat->enc) {
0b96d77a 1138 rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
9a2d2fb3
AP
1139 /* Not fatal errors: return */
1140 if (rv != 1) {
1141 if (rv < 0)
1142 return 0;
1143 return 1;
1144 }
1145 }
1146 if (cdat->enc != 1) {
0b96d77a 1147 rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
9a2d2fb3
AP
1148 /* Not fatal errors: return */
1149 if (rv != 1) {
1150 if (rv < 0)
1151 return 0;
1152 return 1;
1153 }
1154 }
307e3978 1155 }
0b96d77a
MC
1156
1157 if (out_misalign == 1 && frag == 0) {
1158 /*
1159 * XTS, CCM and Wrap modes have special requirements about input
1160 * lengths so we don't fragment for those
1161 */
1162 if (cdat->aead == EVP_CIPH_CCM_MODE
1163 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
1164 || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
1165 break;
1166 out_misalign = 0;
1167 frag++;
1168 } else {
1169 out_misalign++;
1170 }
307e3978 1171 }
9a2d2fb3
AP
1172 t->aux_err = NULL;
1173
307e3978 1174 return 1;
0f113f3e 1175}
307e3978
DSH
1176
1177static const struct evp_test_method cipher_test_method = {
1178 "Cipher",
1179 cipher_test_init,
1180 cipher_test_cleanup,
1181 cipher_test_parse,
1182 cipher_test_run
1183};
83251f39
DSH
1184
1185struct mac_data {
1186 /* MAC type */
1187 int type;
1188 /* Algorithm string for this MAC */
1189 char *alg;
1190 /* MAC key */
1191 unsigned char *key;
1192 size_t key_len;
1193 /* Input to MAC */
1194 unsigned char *input;
1195 size_t input_len;
1196 /* Expected output */
1197 unsigned char *output;
1198 size_t output_len;
1199};
1200
1201static int mac_test_init(struct evp_test *t, const char *alg)
1202{
1203 int type;
1204 struct mac_data *mdat;
b4a3aeeb 1205 if (strcmp(alg, "HMAC") == 0) {
83251f39 1206 type = EVP_PKEY_HMAC;
b4a3aeeb
MC
1207 } else if (strcmp(alg, "CMAC") == 0) {
1208#ifndef OPENSSL_NO_CMAC
83251f39 1209 type = EVP_PKEY_CMAC;
b4a3aeeb
MC
1210#else
1211 t->skip = 1;
1212 return 1;
52ad5b60
TS
1213#endif
1214 } else if (strcmp(alg, "Poly1305") == 0) {
1215#ifndef OPENSSL_NO_POLY1305
1216 type = EVP_PKEY_POLY1305;
1217#else
1218 t->skip = 1;
1219 return 1;
3f5616d7
TS
1220#endif
1221 } else if (strcmp(alg, "SipHash") == 0) {
1222#ifndef OPENSSL_NO_SIPHASH
1223 type = EVP_PKEY_SIPHASH;
1224#else
1225 t->skip = 1;
1226 return 1;
b4a3aeeb
MC
1227#endif
1228 } else
83251f39
DSH
1229 return 0;
1230
b4faea50 1231 mdat = OPENSSL_malloc(sizeof(*mdat));
83251f39
DSH
1232 mdat->type = type;
1233 mdat->alg = NULL;
1234 mdat->key = NULL;
1235 mdat->input = NULL;
1236 mdat->output = NULL;
1237 t->data = mdat;
1238 return 1;
1239}
1240
1241static void mac_test_cleanup(struct evp_test *t)
1242{
1243 struct mac_data *mdat = t->data;
1244 test_free(mdat->alg);
1245 test_free(mdat->key);
1246 test_free(mdat->input);
1247 test_free(mdat->output);
1248}
1249
1250static int mac_test_parse(struct evp_test *t,
1251 const char *keyword, const char *value)
1252{
1253 struct mac_data *mdata = t->data;
86885c28 1254 if (strcmp(keyword, "Key") == 0)
83251f39 1255 return test_bin(value, &mdata->key, &mdata->key_len);
86885c28 1256 if (strcmp(keyword, "Algorithm") == 0) {
7644a9ae 1257 mdata->alg = OPENSSL_strdup(value);
83251f39
DSH
1258 if (!mdata->alg)
1259 return 0;
1260 return 1;
1261 }
86885c28 1262 if (strcmp(keyword, "Input") == 0)
83251f39 1263 return test_bin(value, &mdata->input, &mdata->input_len);
86885c28 1264 if (strcmp(keyword, "Output") == 0)
83251f39
DSH
1265 return test_bin(value, &mdata->output, &mdata->output_len);
1266 return 0;
1267}
1268
1269static int mac_test_run(struct evp_test *t)
1270{
1271 struct mac_data *mdata = t->data;
1272 const char *err = "INTERNAL_ERROR";
1273 EVP_MD_CTX *mctx = NULL;
1274 EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
1275 EVP_PKEY *key = NULL;
1276 const EVP_MD *md = NULL;
1277 unsigned char *mac = NULL;
1278 size_t mac_len;
1279
96bea000 1280#ifdef OPENSSL_NO_DES
31b69e9a 1281 if (mdata->alg != NULL && strstr(mdata->alg, "DES") != NULL) {
96bea000
MC
1282 /* Skip DES */
1283 err = NULL;
1284 goto err;
1285 }
1286#endif
1287
83251f39
DSH
1288 err = "MAC_PKEY_CTX_ERROR";
1289 genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL);
1290 if (!genctx)
1291 goto err;
1292
1293 err = "MAC_KEYGEN_INIT_ERROR";
1294 if (EVP_PKEY_keygen_init(genctx) <= 0)
1295 goto err;
1296 if (mdata->type == EVP_PKEY_CMAC) {
1297 err = "MAC_ALGORITHM_SET_ERROR";
1298 if (EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0)
1299 goto err;
1300 }
1301
1302 err = "MAC_KEY_SET_ERROR";
1303 if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0)
1304 goto err;
1305
1306 err = "MAC_KEY_GENERATE_ERROR";
1307 if (EVP_PKEY_keygen(genctx, &key) <= 0)
1308 goto err;
1309 if (mdata->type == EVP_PKEY_HMAC) {
1310 err = "MAC_ALGORITHM_SET_ERROR";
1311 md = EVP_get_digestbyname(mdata->alg);
1312 if (!md)
1313 goto err;
1314 }
bfb0641f 1315 mctx = EVP_MD_CTX_new();
83251f39
DSH
1316 if (!mctx)
1317 goto err;
1318 err = "DIGESTSIGNINIT_ERROR";
1319 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key))
1320 goto err;
1321
1322 err = "DIGESTSIGNUPDATE_ERROR";
1323 if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len))
1324 goto err;
1325 err = "DIGESTSIGNFINAL_LENGTH_ERROR";
1326 if (!EVP_DigestSignFinal(mctx, NULL, &mac_len))
1327 goto err;
1328 mac = OPENSSL_malloc(mac_len);
1329 if (!mac) {
1330 fprintf(stderr, "Error allocating mac buffer!\n");
1331 exit(1);
1332 }
1333 if (!EVP_DigestSignFinal(mctx, mac, &mac_len))
1334 goto err;
1335 err = "MAC_LENGTH_MISMATCH";
1336 if (mac_len != mdata->output_len)
1337 goto err;
1338 err = "MAC_MISMATCH";
1339 if (check_output(t, mdata->output, mac, mac_len))
1340 goto err;
1341 err = NULL;
1342 err:
bfb0641f 1343 EVP_MD_CTX_free(mctx);
b548a1f1 1344 OPENSSL_free(mac);
c5ba2d99
RS
1345 EVP_PKEY_CTX_free(genctx);
1346 EVP_PKEY_free(key);
83251f39
DSH
1347 t->err = err;
1348 return 1;
1349}
1350
1351static const struct evp_test_method mac_test_method = {
1352 "MAC",
1353 mac_test_init,
1354 mac_test_cleanup,
1355 mac_test_parse,
1356 mac_test_run
1357};
5824cc29
DSH
1358
1359/*
1360 * Public key operations. These are all very similar and can share
1361 * a lot of common code.
1362 */
1363
1364struct pkey_data {
1365 /* Context for this operation */
1366 EVP_PKEY_CTX *ctx;
1367 /* Key operation to perform */
1368 int (*keyop) (EVP_PKEY_CTX *ctx,
1369 unsigned char *sig, size_t *siglen,
1370 const unsigned char *tbs, size_t tbslen);
1371 /* Input to MAC */
1372 unsigned char *input;
1373 size_t input_len;
1374 /* Expected output */
1375 unsigned char *output;
1376 size_t output_len;
1377};
1378
1379/*
1380 * Perform public key operation setup: lookup key, allocated ctx and call
1381 * the appropriate initialisation function
1382 */
1383static int pkey_test_init(struct evp_test *t, const char *name,
1384 int use_public,
1385 int (*keyopinit) (EVP_PKEY_CTX *ctx),
1386 int (*keyop) (EVP_PKEY_CTX *ctx,
1387 unsigned char *sig, size_t *siglen,
1388 const unsigned char *tbs,
1389 size_t tbslen)
1390 )
1391{
1392 struct pkey_data *kdata;
1393 EVP_PKEY *pkey = NULL;
7a6c9792
DSH
1394 int rv = 0;
1395 if (use_public)
1396 rv = find_key(&pkey, name, t->public);
1397 if (!rv)
1398 rv = find_key(&pkey, name, t->private);
024d681e 1399 if (!rv || pkey == NULL) {
7a6c9792
DSH
1400 t->skip = 1;
1401 return 1;
1402 }
1403
b4faea50 1404 kdata = OPENSSL_malloc(sizeof(*kdata));
7a6c9792
DSH
1405 if (!kdata) {
1406 EVP_PKEY_free(pkey);
5824cc29 1407 return 0;
7a6c9792 1408 }
5824cc29
DSH
1409 kdata->ctx = NULL;
1410 kdata->input = NULL;
1411 kdata->output = NULL;
1412 kdata->keyop = keyop;
1413 t->data = kdata;
5824cc29
DSH
1414 kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL);
1415 if (!kdata->ctx)
1416 return 0;
1417 if (keyopinit(kdata->ctx) <= 0)
cce65266 1418 t->err = "KEYOP_INIT_ERROR";
5824cc29
DSH
1419 return 1;
1420}
1421
1422static void pkey_test_cleanup(struct evp_test *t)
1423{
1424 struct pkey_data *kdata = t->data;
b548a1f1
RS
1425
1426 OPENSSL_free(kdata->input);
1427 OPENSSL_free(kdata->output);
c5ba2d99 1428 EVP_PKEY_CTX_free(kdata->ctx);
5824cc29
DSH
1429}
1430
dfbdf4ab
RL
1431static int pkey_test_ctrl(struct evp_test *t, EVP_PKEY_CTX *pctx,
1432 const char *value)
4ddd5ace
DSH
1433{
1434 int rv;
1435 char *p, *tmpval;
1436
1437 tmpval = OPENSSL_strdup(value);
1438 if (tmpval == NULL)
1439 return 0;
1440 p = strchr(tmpval, ':');
1441 if (p != NULL)
1442 *p++ = 0;
1443 rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
cce65266
DSH
1444 if (rv == -2) {
1445 t->err = "PKEY_CTRL_INVALID";
1446 rv = 1;
1447 } else if (p != NULL && rv <= 0) {
1448 /* If p has an OID and lookup fails assume disabled algorithm */
1449 int nid = OBJ_sn2nid(p);
1450 if (nid == NID_undef)
1451 nid = OBJ_ln2nid(p);
1452 if ((nid != NID_undef) && EVP_get_digestbynid(nid) == NULL &&
1453 EVP_get_cipherbynid(nid) == NULL) {
dfbdf4ab
RL
1454 t->skip = 1;
1455 rv = 1;
cce65266
DSH
1456 } else {
1457 t->err = "PKEY_CTRL_ERROR";
1458 rv = 1;
dfbdf4ab
RL
1459 }
1460 }
4ddd5ace
DSH
1461 OPENSSL_free(tmpval);
1462 return rv > 0;
1463}
1464
5824cc29
DSH
1465static int pkey_test_parse(struct evp_test *t,
1466 const char *keyword, const char *value)
1467{
1468 struct pkey_data *kdata = t->data;
86885c28 1469 if (strcmp(keyword, "Input") == 0)
5824cc29 1470 return test_bin(value, &kdata->input, &kdata->input_len);
86885c28 1471 if (strcmp(keyword, "Output") == 0)
5824cc29 1472 return test_bin(value, &kdata->output, &kdata->output_len);
4ddd5ace 1473 if (strcmp(keyword, "Ctrl") == 0)
dfbdf4ab 1474 return pkey_test_ctrl(t, kdata->ctx, value);
5824cc29
DSH
1475 return 0;
1476}
1477
1478static int pkey_test_run(struct evp_test *t)
1479{
1480 struct pkey_data *kdata = t->data;
1481 unsigned char *out = NULL;
1482 size_t out_len;
1483 const char *err = "KEYOP_LENGTH_ERROR";
1484 if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
1485 kdata->input_len) <= 0)
1486 goto err;
1487 out = OPENSSL_malloc(out_len);
1488 if (!out) {
1489 fprintf(stderr, "Error allocating output buffer!\n");
1490 exit(1);
1491 }
1492 err = "KEYOP_ERROR";
1493 if (kdata->keyop
1494 (kdata->ctx, out, &out_len, kdata->input, kdata->input_len) <= 0)
1495 goto err;
1496 err = "KEYOP_LENGTH_MISMATCH";
1497 if (out_len != kdata->output_len)
1498 goto err;
1499 err = "KEYOP_MISMATCH";
1500 if (check_output(t, kdata->output, out, out_len))
1501 goto err;
1502 err = NULL;
1503 err:
b548a1f1 1504 OPENSSL_free(out);
5824cc29
DSH
1505 t->err = err;
1506 return 1;
1507}
1508
1509static int sign_test_init(struct evp_test *t, const char *name)
1510{
1511 return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
1512}
1513
1514static const struct evp_test_method psign_test_method = {
1515 "Sign",
1516 sign_test_init,
1517 pkey_test_cleanup,
1518 pkey_test_parse,
1519 pkey_test_run
1520};
1521
1522static int verify_recover_test_init(struct evp_test *t, const char *name)
1523{
1524 return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
1525 EVP_PKEY_verify_recover);
1526}
1527
1528static const struct evp_test_method pverify_recover_test_method = {
1529 "VerifyRecover",
1530 verify_recover_test_init,
1531 pkey_test_cleanup,
1532 pkey_test_parse,
1533 pkey_test_run
1534};
1535
1536static int decrypt_test_init(struct evp_test *t, const char *name)
1537{
1538 return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
1539 EVP_PKEY_decrypt);
1540}
1541
1542static const struct evp_test_method pdecrypt_test_method = {
1543 "Decrypt",
1544 decrypt_test_init,
1545 pkey_test_cleanup,
1546 pkey_test_parse,
1547 pkey_test_run
1548};
1549
1550static int verify_test_init(struct evp_test *t, const char *name)
1551{
1552 return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
1553}
1554
1555static int verify_test_run(struct evp_test *t)
1556{
1557 struct pkey_data *kdata = t->data;
1558 if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
1559 kdata->input, kdata->input_len) <= 0)
1560 t->err = "VERIFY_ERROR";
1561 return 1;
1562}
1563
1564static const struct evp_test_method pverify_test_method = {
1565 "Verify",
1566 verify_test_init,
1567 pkey_test_cleanup,
1568 pkey_test_parse,
1569 verify_test_run
1570};
3b53e18a 1571
d4ad48d7
DSH
1572
1573static int pderive_test_init(struct evp_test *t, const char *name)
1574{
1575 return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
1576}
1577
1578static int pderive_test_parse(struct evp_test *t,
1579 const char *keyword, const char *value)
1580{
1581 struct pkey_data *kdata = t->data;
1582
1583 if (strcmp(keyword, "PeerKey") == 0) {
1584 EVP_PKEY *peer;
1585 if (find_key(&peer, value, t->public) == 0)
1586 return 0;
1587 if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
1588 return 0;
1589 return 1;
1590 }
1591 if (strcmp(keyword, "SharedSecret") == 0)
1592 return test_bin(value, &kdata->output, &kdata->output_len);
4ddd5ace 1593 if (strcmp(keyword, "Ctrl") == 0)
dfbdf4ab 1594 return pkey_test_ctrl(t, kdata->ctx, value);
d4ad48d7
DSH
1595 return 0;
1596}
1597
1598static int pderive_test_run(struct evp_test *t)
1599{
1600 struct pkey_data *kdata = t->data;
1601 unsigned char *out = NULL;
1602 size_t out_len;
1603 const char *err = "INTERNAL_ERROR";
1604
1605 out_len = kdata->output_len;
1606 out = OPENSSL_malloc(out_len);
1607 if (!out) {
1608 fprintf(stderr, "Error allocating output buffer!\n");
1609 exit(1);
1610 }
1611 err = "DERIVE_ERROR";
1612 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0)
1613 goto err;
1614 err = "SHARED_SECRET_LENGTH_MISMATCH";
1615 if (out_len != kdata->output_len)
1616 goto err;
1617 err = "SHARED_SECRET_MISMATCH";
1618 if (check_output(t, kdata->output, out, out_len))
1619 goto err;
1620 err = NULL;
1621 err:
1622 OPENSSL_free(out);
1623 t->err = err;
1624 return 1;
1625}
1626
1627static const struct evp_test_method pderive_test_method = {
1628 "Derive",
1629 pderive_test_init,
1630 pkey_test_cleanup,
1631 pderive_test_parse,
1632 pderive_test_run
1633};
1634
3b53e18a
DSH
1635/* PBE tests */
1636
1637#define PBE_TYPE_SCRYPT 1
351fe214
DSH
1638#define PBE_TYPE_PBKDF2 2
1639#define PBE_TYPE_PKCS12 3
3b53e18a
DSH
1640
1641struct pbe_data {
1642
1643 int pbe_type;
1644
1645 /* scrypt parameters */
1646 uint64_t N, r, p, maxmem;
1647
351fe214
DSH
1648 /* PKCS#12 parameters */
1649 int id, iter;
1650 const EVP_MD *md;
1651
3b53e18a
DSH
1652 /* password */
1653 unsigned char *pass;
1654 size_t pass_len;
1655
1656 /* salt */
1657 unsigned char *salt;
1658 size_t salt_len;
1659
1660 /* Expected output */
1661 unsigned char *key;
1662 size_t key_len;
1663};
1664
b0809bc8 1665#ifndef OPENSSL_NO_SCRYPT
3b53e18a
DSH
1666static int scrypt_test_parse(struct evp_test *t,
1667 const char *keyword, const char *value)
1668{
1669 struct pbe_data *pdata = t->data;
351fe214 1670
3b53e18a
DSH
1671 if (strcmp(keyword, "N") == 0)
1672 return test_uint64(value, &pdata->N);
1673 if (strcmp(keyword, "p") == 0)
1674 return test_uint64(value, &pdata->p);
1675 if (strcmp(keyword, "r") == 0)
1676 return test_uint64(value, &pdata->r);
1677 if (strcmp(keyword, "maxmem") == 0)
1678 return test_uint64(value, &pdata->maxmem);
1679 return 0;
1680}
b0809bc8 1681#endif
3b53e18a 1682
351fe214
DSH
1683static int pbkdf2_test_parse(struct evp_test *t,
1684 const char *keyword, const char *value)
3b53e18a
DSH
1685{
1686 struct pbe_data *pdata = t->data;
351fe214
DSH
1687
1688 if (strcmp(keyword, "iter") == 0) {
1689 pdata->iter = atoi(value);
1690 if (pdata->iter <= 0)
1691 return 0;
1692 return 1;
1693 }
1694 if (strcmp(keyword, "MD") == 0) {
1695 pdata->md = EVP_get_digestbyname(value);
1696 if (pdata->md == NULL)
1697 return 0;
1698 return 1;
1699 }
1700 return 0;
1701}
1702
1703static int pkcs12_test_parse(struct evp_test *t,
1704 const char *keyword, const char *value)
1705{
1706 struct pbe_data *pdata = t->data;
1707
1708 if (strcmp(keyword, "id") == 0) {
1709 pdata->id = atoi(value);
1710 if (pdata->id <= 0)
1711 return 0;
1712 return 1;
1713 }
1714 return pbkdf2_test_parse(t, keyword, value);
3b53e18a
DSH
1715}
1716
1717static int pbe_test_init(struct evp_test *t, const char *alg)
1718{
1719 struct pbe_data *pdat;
1720 int pbe_type = 0;
351fe214 1721
83bd31da 1722 if (strcmp(alg, "scrypt") == 0) {
b0809bc8 1723#ifndef OPENSSL_NO_SCRYPT
3b53e18a 1724 pbe_type = PBE_TYPE_SCRYPT;
83bd31da
MC
1725#else
1726 t->skip = 1;
1727 return 1;
b0809bc8 1728#endif
83bd31da 1729 } else if (strcmp(alg, "pbkdf2") == 0) {
351fe214 1730 pbe_type = PBE_TYPE_PBKDF2;
83bd31da 1731 } else if (strcmp(alg, "pkcs12") == 0) {
351fe214 1732 pbe_type = PBE_TYPE_PKCS12;
83bd31da 1733 } else {
3b53e18a 1734 fprintf(stderr, "Unknown pbe algorithm %s\n", alg);
83bd31da 1735 }
3b53e18a
DSH
1736 pdat = OPENSSL_malloc(sizeof(*pdat));
1737 pdat->pbe_type = pbe_type;
1738 pdat->pass = NULL;
1739 pdat->salt = NULL;
1740 pdat->N = 0;
1741 pdat->r = 0;
1742 pdat->p = 0;
1743 pdat->maxmem = 0;
351fe214
DSH
1744 pdat->id = 0;
1745 pdat->iter = 0;
1746 pdat->md = NULL;
3b53e18a
DSH
1747 t->data = pdat;
1748 return 1;
1749}
1750
1751static void pbe_test_cleanup(struct evp_test *t)
1752{
1753 struct pbe_data *pdat = t->data;
1754 test_free(pdat->pass);
1755 test_free(pdat->salt);
1756 test_free(pdat->key);
1757}
1758
1759static int pbe_test_parse(struct evp_test *t,
1760 const char *keyword, const char *value)
1761{
1762 struct pbe_data *pdata = t->data;
351fe214 1763
3b53e18a
DSH
1764 if (strcmp(keyword, "Password") == 0)
1765 return test_bin(value, &pdata->pass, &pdata->pass_len);
1766 if (strcmp(keyword, "Salt") == 0)
1767 return test_bin(value, &pdata->salt, &pdata->salt_len);
1768 if (strcmp(keyword, "Key") == 0)
1769 return test_bin(value, &pdata->key, &pdata->key_len);
b0809bc8 1770 if (pdata->pbe_type == PBE_TYPE_PBKDF2)
351fe214
DSH
1771 return pbkdf2_test_parse(t, keyword, value);
1772 else if (pdata->pbe_type == PBE_TYPE_PKCS12)
1773 return pkcs12_test_parse(t, keyword, value);
b0809bc8
RS
1774#ifndef OPENSSL_NO_SCRYPT
1775 else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
1776 return scrypt_test_parse(t, keyword, value);
1777#endif
3b53e18a
DSH
1778 return 0;
1779}
1780
1781static int pbe_test_run(struct evp_test *t)
1782{
1783 struct pbe_data *pdata = t->data;
351fe214
DSH
1784 const char *err = "INTERNAL_ERROR";
1785 unsigned char *key;
1786
1787 key = OPENSSL_malloc(pdata->key_len);
1788 if (!key)
1789 goto err;
1790 if (pdata->pbe_type == PBE_TYPE_PBKDF2) {
1791 err = "PBKDF2_ERROR";
1792 if (PKCS5_PBKDF2_HMAC((char *)pdata->pass, pdata->pass_len,
1793 pdata->salt, pdata->salt_len,
1794 pdata->iter, pdata->md,
1795 pdata->key_len, key) == 0)
1796 goto err;
b0809bc8 1797#ifndef OPENSSL_NO_SCRYPT
351fe214
DSH
1798 } else if (pdata->pbe_type == PBE_TYPE_SCRYPT) {
1799 err = "SCRYPT_ERROR";
1800 if (EVP_PBE_scrypt((const char *)pdata->pass, pdata->pass_len,
1801 pdata->salt, pdata->salt_len,
1802 pdata->N, pdata->r, pdata->p, pdata->maxmem,
1803 key, pdata->key_len) == 0)
1804 goto err;
b0809bc8 1805#endif
351fe214
DSH
1806 } else if (pdata->pbe_type == PBE_TYPE_PKCS12) {
1807 err = "PKCS12_ERROR";
1808 if (PKCS12_key_gen_uni(pdata->pass, pdata->pass_len,
1809 pdata->salt, pdata->salt_len,
1810 pdata->id, pdata->iter, pdata->key_len,
1811 key, pdata->md) == 0)
1812 goto err;
1813 }
1814 err = "KEY_MISMATCH";
1815 if (check_output(t, pdata->key, key, pdata->key_len))
1816 goto err;
1817 err = NULL;
1818 err:
1819 OPENSSL_free(key);
1820 t->err = err;
1821 return 1;
3b53e18a
DSH
1822}
1823
1824static const struct evp_test_method pbe_test_method = {
1825 "PBE",
1826 pbe_test_init,
1827 pbe_test_cleanup,
1828 pbe_test_parse,
1829 pbe_test_run
1830};
3cdd1e94
EK
1831
1832/* Base64 tests */
1833
1834typedef enum {
1835 BASE64_CANONICAL_ENCODING = 0,
1836 BASE64_VALID_ENCODING = 1,
1837 BASE64_INVALID_ENCODING = 2
1838} base64_encoding_type;
1839
1840struct encode_data {
1841 /* Input to encoding */
1842 unsigned char *input;
1843 size_t input_len;
1844 /* Expected output */
1845 unsigned char *output;
1846 size_t output_len;
1847 base64_encoding_type encoding;
1848};
1849
1850static int encode_test_init(struct evp_test *t, const char *encoding)
1851{
1852 struct encode_data *edata = OPENSSL_zalloc(sizeof(*edata));
1853
1854 if (strcmp(encoding, "canonical") == 0) {
1855 edata->encoding = BASE64_CANONICAL_ENCODING;
1856 } else if (strcmp(encoding, "valid") == 0) {
1857 edata->encoding = BASE64_VALID_ENCODING;
1858 } else if (strcmp(encoding, "invalid") == 0) {
1859 edata->encoding = BASE64_INVALID_ENCODING;
7644a9ae 1860 t->expected_err = OPENSSL_strdup("DECODE_ERROR");
3cdd1e94
EK
1861 if (t->expected_err == NULL)
1862 return 0;
1863 } else {
1864 fprintf(stderr, "Bad encoding: %s. Should be one of "
1865 "{canonical, valid, invalid}\n", encoding);
1866 return 0;
1867 }
1868 t->data = edata;
1869 return 1;
1870}
1871
1872static void encode_test_cleanup(struct evp_test *t)
1873{
1874 struct encode_data *edata = t->data;
1875 test_free(edata->input);
1876 test_free(edata->output);
1877 memset(edata, 0, sizeof(*edata));
1878}
1879
1880static int encode_test_parse(struct evp_test *t,
1881 const char *keyword, const char *value)
1882{
1883 struct encode_data *edata = t->data;
1884 if (strcmp(keyword, "Input") == 0)
1885 return test_bin(value, &edata->input, &edata->input_len);
1886 if (strcmp(keyword, "Output") == 0)
1887 return test_bin(value, &edata->output, &edata->output_len);
1888 return 0;
1889}
1890
1891static int encode_test_run(struct evp_test *t)
1892{
1893 struct encode_data *edata = t->data;
1894 unsigned char *encode_out = NULL, *decode_out = NULL;
1895 int output_len, chunk_len;
1896 const char *err = "INTERNAL_ERROR";
254b26af
RL
1897 EVP_ENCODE_CTX *decode_ctx = EVP_ENCODE_CTX_new();
1898
1899 if (decode_ctx == NULL)
1900 goto err;
3cdd1e94
EK
1901
1902 if (edata->encoding == BASE64_CANONICAL_ENCODING) {
254b26af
RL
1903 EVP_ENCODE_CTX *encode_ctx = EVP_ENCODE_CTX_new();
1904 if (encode_ctx == NULL)
1905 goto err;
3cdd1e94
EK
1906 encode_out = OPENSSL_malloc(EVP_ENCODE_LENGTH(edata->input_len));
1907 if (encode_out == NULL)
1908 goto err;
1909
254b26af
RL
1910 EVP_EncodeInit(encode_ctx);
1911 EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
3cdd1e94
EK
1912 edata->input, edata->input_len);
1913 output_len = chunk_len;
1914
254b26af 1915 EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
3cdd1e94
EK
1916 output_len += chunk_len;
1917
254b26af
RL
1918 EVP_ENCODE_CTX_free(encode_ctx);
1919
3cdd1e94
EK
1920 if (check_var_length_output(t, edata->output, edata->output_len,
1921 encode_out, output_len)) {
1922 err = "BAD_ENCODING";
1923 goto err;
1924 }
1925 }
1926
1927 decode_out = OPENSSL_malloc(EVP_DECODE_LENGTH(edata->output_len));
1928 if (decode_out == NULL)
1929 goto err;
1930
254b26af
RL
1931 EVP_DecodeInit(decode_ctx);
1932 if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, edata->output,
3cdd1e94
EK
1933 edata->output_len) < 0) {
1934 err = "DECODE_ERROR";
1935 goto err;
1936 }
1937 output_len = chunk_len;
1938
254b26af 1939 if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
3cdd1e94
EK
1940 err = "DECODE_ERROR";
1941 goto err;
1942 }
1943 output_len += chunk_len;
1944
1945 if (edata->encoding != BASE64_INVALID_ENCODING &&
1946 check_var_length_output(t, edata->input, edata->input_len,
1947 decode_out, output_len)) {
1948 err = "BAD_DECODING";
1949 goto err;
1950 }
1951
1952 err = NULL;
1953 err:
1954 t->err = err;
1955 OPENSSL_free(encode_out);
1956 OPENSSL_free(decode_out);
254b26af 1957 EVP_ENCODE_CTX_free(decode_ctx);
3cdd1e94
EK
1958 return 1;
1959}
1960
1961static const struct evp_test_method encode_test_method = {
1962 "Encoding",
1963 encode_test_init,
1964 encode_test_cleanup,
1965 encode_test_parse,
1966 encode_test_run,
1967};
44a284d2 1968
7d04be79 1969/* KDF operations */
44a284d2
DSH
1970
1971struct kdf_data {
1972 /* Context for this operation */
1973 EVP_PKEY_CTX *ctx;
1974 /* Expected output */
1975 unsigned char *output;
1976 size_t output_len;
1977};
1978
1979/*
1980 * Perform public key operation setup: lookup key, allocated ctx and call
1981 * the appropriate initialisation function
1982 */
1983static int kdf_test_init(struct evp_test *t, const char *name)
1984{
1985 struct kdf_data *kdata;
1986
1987 kdata = OPENSSL_malloc(sizeof(*kdata));
1988 if (kdata == NULL)
1989 return 0;
1990 kdata->ctx = NULL;
1991 kdata->output = NULL;
1992 t->data = kdata;
1993 kdata->ctx = EVP_PKEY_CTX_new_id(OBJ_sn2nid(name), NULL);
1994 if (kdata->ctx == NULL)
1995 return 0;
1996 if (EVP_PKEY_derive_init(kdata->ctx) <= 0)
1997 return 0;
1998 return 1;
1999}
2000
2001static void kdf_test_cleanup(struct evp_test *t)
2002{
2003 struct kdf_data *kdata = t->data;
2004 OPENSSL_free(kdata->output);
2005 EVP_PKEY_CTX_free(kdata->ctx);
2006}
2007
44a284d2
DSH
2008static int kdf_test_parse(struct evp_test *t,
2009 const char *keyword, const char *value)
2010{
2011 struct kdf_data *kdata = t->data;
2012 if (strcmp(keyword, "Output") == 0)
2013 return test_bin(value, &kdata->output, &kdata->output_len);
7d04be79 2014 if (strncmp(keyword, "Ctrl", 4) == 0)
dfbdf4ab 2015 return pkey_test_ctrl(t, kdata->ctx, value);
44a284d2
DSH
2016 return 0;
2017}
2018
2019static int kdf_test_run(struct evp_test *t)
2020{
2021 struct kdf_data *kdata = t->data;
2022 unsigned char *out = NULL;
2023 size_t out_len = kdata->output_len;
2024 const char *err = "INTERNAL_ERROR";
2025 out = OPENSSL_malloc(out_len);
2026 if (!out) {
2027 fprintf(stderr, "Error allocating output buffer!\n");
2028 exit(1);
2029 }
2030 err = "KDF_DERIVE_ERROR";
2031 if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0)
2032 goto err;
2033 err = "KDF_LENGTH_MISMATCH";
2034 if (out_len != kdata->output_len)
2035 goto err;
2036 err = "KDF_MISMATCH";
2037 if (check_output(t, kdata->output, out, out_len))
2038 goto err;
2039 err = NULL;
2040 err:
2041 OPENSSL_free(out);
2042 t->err = err;
2043 return 1;
2044}
2045
2046static const struct evp_test_method kdf_test_method = {
2047 "KDF",
2048 kdf_test_init,
2049 kdf_test_cleanup,
2050 kdf_test_parse,
2051 kdf_test_run
2052};