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