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