]>
Commit | Line | Data |
---|---|---|
0e360199 | 1 | /* |
33388b44 | 2 | * Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved. |
0e360199 | 3 | * |
909f1a2e | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
440e5d80 RS |
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> |
d5e5e2ff | 17 | #include <openssl/provider.h> |
307e3978 | 18 | #include <openssl/x509v3.h> |
351fe214 | 19 | #include <openssl/pkcs12.h> |
44a284d2 | 20 | #include <openssl/kdf.h> |
25446a66 RL |
21 | #include <openssl/params.h> |
22 | #include <openssl/core_names.h> | |
3b53e18a | 23 | #include "internal/numbers.h" |
25446a66 | 24 | #include "internal/nelem.h" |
6c5943c9 | 25 | #include "testutil.h" |
c3fc7d9a | 26 | #include "evp_test.h" |
0e360199 | 27 | |
852c2ed2 RS |
28 | DEFINE_STACK_OF_STRING() |
29 | ||
b1ceb439 | 30 | #define AAD_NUM 4 |
c49e0b04 RS |
31 | |
32 | typedef struct evp_test_method_st EVP_TEST_METHOD; | |
7193f872 | 33 | |
6c5943c9 | 34 | /* |
c49e0b04 | 35 | * Structure holding test information |
6c5943c9 | 36 | */ |
c49e0b04 | 37 | typedef struct evp_test_st { |
ae269dd8 RS |
38 | STANZA s; /* Common test stanza */ |
39 | char *name; | |
c49e0b04 | 40 | int skip; /* Current test should be skipped */ |
c49e0b04 RS |
41 | const EVP_TEST_METHOD *meth; /* method for this test */ |
42 | const char *err, *aux_err; /* Error string for test */ | |
43 | char *expected_err; /* Expected error value of test */ | |
c49e0b04 RS |
44 | char *reason; /* Expected error reason string */ |
45 | void *data; /* test specific data */ | |
46 | } EVP_TEST; | |
0e360199 | 47 | |
307e3978 | 48 | /* |
c49e0b04 | 49 | * Test method structure |
307e3978 | 50 | */ |
c49e0b04 RS |
51 | struct evp_test_method_st { |
52 | /* Name of test as it appears in file */ | |
53 | const char *name; | |
54 | /* Initialise test for "alg" */ | |
55 | int (*init) (EVP_TEST * t, const char *alg); | |
56 | /* Clean up method */ | |
57 | void (*cleanup) (EVP_TEST * t); | |
58 | /* Test specific name value pair processing */ | |
59 | int (*parse) (EVP_TEST * t, const char *name, const char *value); | |
60 | /* Run the test itself */ | |
61 | int (*run_test) (EVP_TEST * t); | |
62 | }; | |
5b46eee0 | 63 | |
0e360199 | 64 | |
3cdd1e94 | 65 | /* |
c49e0b04 | 66 | * Linked list of named keys. |
3cdd1e94 | 67 | */ |
c49e0b04 RS |
68 | typedef struct key_list_st { |
69 | char *name; | |
70 | EVP_PKEY *key; | |
71 | struct key_list_st *next; | |
72 | } KEY_LIST; | |
fa013b65 | 73 | |
c49e0b04 RS |
74 | /* |
75 | * List of public and private keys | |
76 | */ | |
77 | static KEY_LIST *private_keys; | |
78 | static KEY_LIST *public_keys; | |
79 | static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst); | |
fa013b65 | 80 | |
c49e0b04 | 81 | static int parse_bin(const char *value, unsigned char **buf, size_t *buflen); |
3cdd1e94 | 82 | |
4cceb185 P |
83 | /* |
84 | * Compare two memory regions for equality, returning zero if they differ. | |
85 | * However, if there is expected to be an error and the actual error | |
86 | * matches then the memory is expected to be different so handle this | |
87 | * case without producing unnecessary test framework output. | |
88 | */ | |
89 | static int memory_err_compare(EVP_TEST *t, const char *err, | |
90 | const void *expected, size_t expected_len, | |
91 | const void *got, size_t got_len) | |
92 | { | |
93 | int r; | |
94 | ||
95 | if (t->expected_err != NULL && strcmp(t->expected_err, err) == 0) | |
96 | r = !TEST_mem_ne(expected, expected_len, got, got_len); | |
97 | else | |
98 | r = TEST_mem_eq(expected, expected_len, got, got_len); | |
99 | if (!r) | |
100 | t->err = err; | |
101 | return r; | |
102 | } | |
103 | ||
c3fc7d9a DSH |
104 | /* |
105 | * Structure used to hold a list of blocks of memory to test | |
106 | * calls to "update" like functions. | |
107 | */ | |
c3fc7d9a DSH |
108 | struct evp_test_buffer_st { |
109 | unsigned char *buf; | |
110 | size_t buflen; | |
111 | size_t count; | |
112 | int count_set; | |
113 | }; | |
114 | ||
115 | static void evp_test_buffer_free(EVP_TEST_BUFFER *db) | |
116 | { | |
117 | if (db != NULL) { | |
118 | OPENSSL_free(db->buf); | |
119 | OPENSSL_free(db); | |
120 | } | |
121 | } | |
122 | ||
c49e0b04 RS |
123 | /* |
124 | * append buffer to a list | |
125 | */ | |
c3fc7d9a DSH |
126 | static int evp_test_buffer_append(const char *value, |
127 | STACK_OF(EVP_TEST_BUFFER) **sk) | |
128 | { | |
129 | EVP_TEST_BUFFER *db = NULL; | |
130 | ||
131 | if (!TEST_ptr(db = OPENSSL_malloc(sizeof(*db)))) | |
132 | goto err; | |
133 | ||
c49e0b04 | 134 | if (!parse_bin(value, &db->buf, &db->buflen)) |
c3fc7d9a DSH |
135 | goto err; |
136 | db->count = 1; | |
137 | db->count_set = 0; | |
138 | ||
139 | if (*sk == NULL && !TEST_ptr(*sk = sk_EVP_TEST_BUFFER_new_null())) | |
c49e0b04 | 140 | goto err; |
c3fc7d9a DSH |
141 | if (!sk_EVP_TEST_BUFFER_push(*sk, db)) |
142 | goto err; | |
143 | ||
144 | return 1; | |
145 | ||
c49e0b04 | 146 | err: |
c3fc7d9a | 147 | evp_test_buffer_free(db); |
c3fc7d9a DSH |
148 | return 0; |
149 | } | |
150 | ||
151 | /* | |
152 | * replace last buffer in list with copies of itself | |
153 | */ | |
154 | static int evp_test_buffer_ncopy(const char *value, | |
155 | STACK_OF(EVP_TEST_BUFFER) *sk) | |
156 | { | |
157 | EVP_TEST_BUFFER *db; | |
158 | unsigned char *tbuf, *p; | |
159 | size_t tbuflen; | |
160 | int ncopy = atoi(value); | |
161 | int i; | |
162 | ||
163 | if (ncopy <= 0) | |
164 | return 0; | |
165 | if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0) | |
166 | return 0; | |
167 | db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1); | |
168 | ||
169 | tbuflen = db->buflen * ncopy; | |
170 | if (!TEST_ptr(tbuf = OPENSSL_malloc(tbuflen))) | |
171 | return 0; | |
172 | for (i = 0, p = tbuf; i < ncopy; i++, p += db->buflen) | |
173 | memcpy(p, db->buf, db->buflen); | |
174 | ||
175 | OPENSSL_free(db->buf); | |
176 | db->buf = tbuf; | |
177 | db->buflen = tbuflen; | |
178 | return 1; | |
179 | } | |
180 | ||
c49e0b04 RS |
181 | /* |
182 | * set repeat count for last buffer in list | |
183 | */ | |
c3fc7d9a DSH |
184 | static int evp_test_buffer_set_count(const char *value, |
185 | STACK_OF(EVP_TEST_BUFFER) *sk) | |
186 | { | |
187 | EVP_TEST_BUFFER *db; | |
188 | int count = atoi(value); | |
189 | ||
190 | if (count <= 0) | |
191 | return 0; | |
192 | ||
193 | if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0) | |
194 | return 0; | |
195 | ||
196 | db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1); | |
c49e0b04 | 197 | if (db->count_set != 0) |
c3fc7d9a DSH |
198 | return 0; |
199 | ||
200 | db->count = (size_t)count; | |
201 | db->count_set = 1; | |
202 | return 1; | |
203 | } | |
204 | ||
205 | /* | |
206 | * call "fn" with each element of the list in turn | |
207 | */ | |
208 | static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER) *sk, | |
209 | int (*fn)(void *ctx, | |
210 | const unsigned char *buf, | |
211 | size_t buflen), | |
212 | void *ctx) | |
213 | { | |
214 | int i; | |
215 | ||
216 | for (i = 0; i < sk_EVP_TEST_BUFFER_num(sk); i++) { | |
217 | EVP_TEST_BUFFER *tb = sk_EVP_TEST_BUFFER_value(sk, i); | |
218 | size_t j; | |
219 | ||
220 | for (j = 0; j < tb->count; j++) { | |
221 | if (fn(ctx, tb->buf, tb->buflen) <= 0) | |
222 | return 0; | |
223 | } | |
224 | } | |
225 | return 1; | |
226 | } | |
227 | ||
6c5943c9 | 228 | /* |
c49e0b04 RS |
229 | * Unescape some sequences in string literals (only \n for now). |
230 | * Return an allocated buffer, set |out_len|. If |input_len| | |
231 | * is zero, get an empty buffer but set length to zero. | |
6c5943c9 | 232 | */ |
c49e0b04 RS |
233 | static unsigned char* unescape(const char *input, size_t input_len, |
234 | size_t *out_len) | |
235 | { | |
236 | unsigned char *ret, *p; | |
237 | size_t i; | |
5824cc29 | 238 | |
c49e0b04 RS |
239 | if (input_len == 0) { |
240 | *out_len = 0; | |
241 | return OPENSSL_zalloc(1); | |
242 | } | |
307e3978 | 243 | |
c49e0b04 RS |
244 | /* Escaping is non-expanding; over-allocate original size for simplicity. */ |
245 | if (!TEST_ptr(ret = p = OPENSSL_malloc(input_len))) | |
246 | return NULL; | |
6c5943c9 | 247 | |
c49e0b04 RS |
248 | for (i = 0; i < input_len; i++) { |
249 | if (*input == '\\') { | |
250 | if (i == input_len - 1 || *++input != 'n') { | |
251 | TEST_error("Bad escape sequence in file"); | |
252 | goto err; | |
253 | } | |
254 | *p++ = '\n'; | |
255 | i++; | |
256 | input++; | |
257 | } else { | |
258 | *p++ = *input++; | |
259 | } | |
260 | } | |
307e3978 | 261 | |
c49e0b04 RS |
262 | *out_len = p - ret; |
263 | return ret; | |
86885c28 | 264 | |
c49e0b04 RS |
265 | err: |
266 | OPENSSL_free(ret); | |
307e3978 | 267 | return NULL; |
0f113f3e MC |
268 | } |
269 | ||
6c5943c9 | 270 | /* |
c49e0b04 RS |
271 | * For a hex string "value" convert to a binary allocated buffer. |
272 | * Return 1 on success or 0 on failure. | |
6c5943c9 | 273 | */ |
c49e0b04 | 274 | static int parse_bin(const char *value, unsigned char **buf, size_t *buflen) |
0f113f3e | 275 | { |
c49e0b04 | 276 | long len; |
6c5943c9 | 277 | |
c49e0b04 RS |
278 | /* Check for NULL literal */ |
279 | if (strcmp(value, "NULL") == 0) { | |
280 | *buf = NULL; | |
281 | *buflen = 0; | |
307e3978 | 282 | return 1; |
71f60ef3 | 283 | } |
6c5943c9 | 284 | |
c49e0b04 RS |
285 | /* Check for empty value */ |
286 | if (*value == '\0') { | |
287 | /* | |
288 | * Don't return NULL for zero length buffer. This is needed for | |
289 | * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key | |
290 | * buffer even if the key length is 0, in order to detect key reset. | |
291 | */ | |
292 | *buf = OPENSSL_malloc(1); | |
293 | if (*buf == NULL) | |
5824cc29 | 294 | return 0; |
c49e0b04 RS |
295 | **buf = 0; |
296 | *buflen = 0; | |
71f60ef3 | 297 | return 1; |
5824cc29 DSH |
298 | } |
299 | ||
c49e0b04 RS |
300 | /* Check for string literal */ |
301 | if (value[0] == '"') { | |
302 | size_t vlen = strlen(++value); | |
303 | ||
304 | if (vlen == 0 || value[vlen - 1] != '"') | |
307e3978 | 305 | return 0; |
c49e0b04 RS |
306 | vlen--; |
307 | *buf = unescape(value, vlen, buflen); | |
308 | return *buf == NULL ? 0 : 1; | |
6c5943c9 | 309 | } |
307e3978 | 310 | |
c49e0b04 RS |
311 | /* Otherwise assume as hex literal and convert it to binary buffer */ |
312 | if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) { | |
313 | TEST_info("Can't convert %s", value); | |
8fe3127c | 314 | TEST_openssl_errors(); |
c49e0b04 | 315 | return -1; |
0f113f3e | 316 | } |
c49e0b04 RS |
317 | /* Size of input buffer means we'll never overflow */ |
318 | *buflen = len; | |
307e3978 DSH |
319 | return 1; |
320 | } | |
0f113f3e | 321 | |
c49e0b04 RS |
322 | |
323 | /** | |
324 | *** MESSAGE DIGEST TESTS | |
325 | **/ | |
4897dc40 | 326 | |
6c5943c9 | 327 | typedef struct digest_data_st { |
307e3978 DSH |
328 | /* Digest this test is for */ |
329 | const EVP_MD *digest; | |
022351fd | 330 | EVP_MD *fetched_digest; |
307e3978 | 331 | /* Input to digest */ |
c3fc7d9a | 332 | STACK_OF(EVP_TEST_BUFFER) *input; |
307e3978 DSH |
333 | /* Expected output */ |
334 | unsigned char *output; | |
335 | size_t output_len; | |
ed5cb177 P |
336 | /* Padding type */ |
337 | int pad_type; | |
6c5943c9 | 338 | } DIGEST_DATA; |
4897dc40 | 339 | |
6c5943c9 | 340 | static int digest_test_init(EVP_TEST *t, const char *alg) |
307e3978 | 341 | { |
6c5943c9 | 342 | DIGEST_DATA *mdat; |
c49e0b04 | 343 | const EVP_MD *digest; |
022351fd | 344 | EVP_MD *fetched_digest; |
6c5943c9 | 345 | |
022351fd RL |
346 | if ((digest = fetched_digest = EVP_MD_fetch(NULL, alg, NULL)) == NULL |
347 | && (digest = EVP_get_digestbyname(alg)) == NULL) { | |
578ce42d DSH |
348 | /* If alg has an OID assume disabled algorithm */ |
349 | if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) { | |
350 | t->skip = 1; | |
351 | return 1; | |
352 | } | |
307e3978 | 353 | return 0; |
578ce42d | 354 | } |
c49e0b04 RS |
355 | if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat)))) |
356 | return 0; | |
307e3978 | 357 | t->data = mdat; |
c49e0b04 | 358 | mdat->digest = digest; |
022351fd | 359 | mdat->fetched_digest = fetched_digest; |
ed5cb177 | 360 | mdat->pad_type = 0; |
022351fd RL |
361 | if (fetched_digest != NULL) |
362 | TEST_info("%s is fetched", alg); | |
4897dc40 | 363 | return 1; |
0f113f3e | 364 | } |
4897dc40 | 365 | |
6c5943c9 | 366 | static void digest_test_cleanup(EVP_TEST *t) |
307e3978 | 367 | { |
6c5943c9 RS |
368 | DIGEST_DATA *mdat = t->data; |
369 | ||
c3fc7d9a | 370 | sk_EVP_TEST_BUFFER_pop_free(mdat->input, evp_test_buffer_free); |
6c5943c9 | 371 | OPENSSL_free(mdat->output); |
022351fd | 372 | EVP_MD_meth_free(mdat->fetched_digest); |
307e3978 DSH |
373 | } |
374 | ||
6c5943c9 | 375 | static int digest_test_parse(EVP_TEST *t, |
307e3978 DSH |
376 | const char *keyword, const char *value) |
377 | { | |
6c5943c9 RS |
378 | DIGEST_DATA *mdata = t->data; |
379 | ||
86885c28 | 380 | if (strcmp(keyword, "Input") == 0) |
c3fc7d9a | 381 | return evp_test_buffer_append(value, &mdata->input); |
86885c28 | 382 | if (strcmp(keyword, "Output") == 0) |
c49e0b04 | 383 | return parse_bin(value, &mdata->output, &mdata->output_len); |
c3fc7d9a DSH |
384 | if (strcmp(keyword, "Count") == 0) |
385 | return evp_test_buffer_set_count(value, mdata->input); | |
386 | if (strcmp(keyword, "Ncopy") == 0) | |
387 | return evp_test_buffer_ncopy(value, mdata->input); | |
ed5cb177 P |
388 | if (strcmp(keyword, "Padding") == 0) |
389 | return (mdata->pad_type = atoi(value)) > 0; | |
307e3978 DSH |
390 | return 0; |
391 | } | |
392 | ||
c3fc7d9a DSH |
393 | static int digest_update_fn(void *ctx, const unsigned char *buf, size_t buflen) |
394 | { | |
395 | return EVP_DigestUpdate(ctx, buf, buflen); | |
396 | } | |
397 | ||
6c5943c9 | 398 | static int digest_test_run(EVP_TEST *t) |
0f113f3e | 399 | { |
e3d378bc | 400 | DIGEST_DATA *expected = t->data; |
307e3978 | 401 | EVP_MD_CTX *mctx; |
cd8d1456 | 402 | unsigned char *got = NULL; |
e3d378bc | 403 | unsigned int got_len; |
ed5cb177 | 404 | OSSL_PARAM params[2]; |
6c5943c9 RS |
405 | |
406 | t->err = "TEST_FAILURE"; | |
407 | if (!TEST_ptr(mctx = EVP_MD_CTX_new())) | |
307e3978 | 408 | goto err; |
6c5943c9 | 409 | |
cd8d1456 AP |
410 | got = OPENSSL_malloc(expected->output_len > EVP_MAX_MD_SIZE ? |
411 | expected->output_len : EVP_MAX_MD_SIZE); | |
412 | if (!TEST_ptr(got)) | |
413 | goto err; | |
414 | ||
e3d378bc | 415 | if (!EVP_DigestInit_ex(mctx, expected->digest, NULL)) { |
6c5943c9 | 416 | t->err = "DIGESTINIT_ERROR"; |
307e3978 | 417 | goto err; |
618be04e | 418 | } |
ed5cb177 P |
419 | if (expected->pad_type > 0) { |
420 | params[0] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_PAD_TYPE, | |
421 | &expected->pad_type); | |
422 | params[1] = OSSL_PARAM_construct_end(); | |
423 | if (!TEST_int_gt(EVP_MD_CTX_set_params(mctx, params), 0)) { | |
424 | t->err = "PARAMS_ERROR"; | |
425 | goto err; | |
426 | } | |
427 | } | |
e3d378bc | 428 | if (!evp_test_buffer_do(expected->input, digest_update_fn, mctx)) { |
c3fc7d9a DSH |
429 | t->err = "DIGESTUPDATE_ERROR"; |
430 | goto err; | |
431 | } | |
432 | ||
cd8d1456 | 433 | if (EVP_MD_flags(expected->digest) & EVP_MD_FLAG_XOF) { |
3ce46435 PS |
434 | EVP_MD_CTX *mctx_cpy; |
435 | char dont[] = "touch"; | |
436 | ||
437 | if (!TEST_ptr(mctx_cpy = EVP_MD_CTX_new())) { | |
438 | goto err; | |
439 | } | |
440 | if (!EVP_MD_CTX_copy(mctx_cpy, mctx)) { | |
441 | EVP_MD_CTX_free(mctx_cpy); | |
442 | goto err; | |
443 | } | |
444 | if (!EVP_DigestFinalXOF(mctx_cpy, (unsigned char *)dont, 0)) { | |
445 | EVP_MD_CTX_free(mctx_cpy); | |
446 | t->err = "DIGESTFINALXOF_ERROR"; | |
447 | goto err; | |
448 | } | |
449 | if (!TEST_str_eq(dont, "touch")) { | |
450 | EVP_MD_CTX_free(mctx_cpy); | |
451 | t->err = "DIGESTFINALXOF_ERROR"; | |
452 | goto err; | |
453 | } | |
454 | EVP_MD_CTX_free(mctx_cpy); | |
455 | ||
cd8d1456 AP |
456 | got_len = expected->output_len; |
457 | if (!EVP_DigestFinalXOF(mctx, got, got_len)) { | |
458 | t->err = "DIGESTFINALXOF_ERROR"; | |
459 | goto err; | |
460 | } | |
461 | } else { | |
462 | if (!EVP_DigestFinal(mctx, got, &got_len)) { | |
463 | t->err = "DIGESTFINAL_ERROR"; | |
464 | goto err; | |
465 | } | |
6c5943c9 | 466 | } |
e3d378bc | 467 | if (!TEST_int_eq(expected->output_len, got_len)) { |
6c5943c9 | 468 | t->err = "DIGEST_LENGTH_MISMATCH"; |
307e3978 | 469 | goto err; |
6c5943c9 | 470 | } |
4cceb185 P |
471 | if (!memory_err_compare(t, "DIGEST_MISMATCH", |
472 | expected->output, expected->output_len, | |
473 | got, got_len)) | |
307e3978 | 474 | goto err; |
4cceb185 | 475 | |
6c5943c9 RS |
476 | t->err = NULL; |
477 | ||
307e3978 | 478 | err: |
cd8d1456 | 479 | OPENSSL_free(got); |
bfb0641f | 480 | EVP_MD_CTX_free(mctx); |
b033e5d5 | 481 | return 1; |
307e3978 | 482 | } |
4897dc40 | 483 | |
6c5943c9 | 484 | static const EVP_TEST_METHOD digest_test_method = { |
307e3978 DSH |
485 | "Digest", |
486 | digest_test_init, | |
487 | digest_test_cleanup, | |
488 | digest_test_parse, | |
489 | digest_test_run | |
490 | }; | |
491 | ||
c49e0b04 RS |
492 | |
493 | /** | |
494 | *** CIPHER TESTS | |
495 | **/ | |
496 | ||
6c5943c9 | 497 | typedef struct cipher_data_st { |
307e3978 | 498 | const EVP_CIPHER *cipher; |
022351fd | 499 | EVP_CIPHER *fetched_cipher; |
307e3978 | 500 | int enc; |
2207ba7b | 501 | /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */ |
307e3978 DSH |
502 | int aead; |
503 | unsigned char *key; | |
504 | size_t key_len; | |
f816aa47 | 505 | size_t key_bits; /* Used by RC2 */ |
307e3978 | 506 | unsigned char *iv; |
6a41156c | 507 | unsigned int rounds; |
307e3978 DSH |
508 | size_t iv_len; |
509 | unsigned char *plaintext; | |
510 | size_t plaintext_len; | |
511 | unsigned char *ciphertext; | |
512 | size_t ciphertext_len; | |
b1ceb439 TS |
513 | /* GCM, CCM, OCB and SIV only */ |
514 | unsigned char *aad[AAD_NUM]; | |
515 | size_t aad_len[AAD_NUM]; | |
307e3978 DSH |
516 | unsigned char *tag; |
517 | size_t tag_len; | |
67c81ec3 | 518 | int tag_late; |
6c5943c9 | 519 | } CIPHER_DATA; |
307e3978 | 520 | |
6c5943c9 | 521 | static int cipher_test_init(EVP_TEST *t, const char *alg) |
307e3978 DSH |
522 | { |
523 | const EVP_CIPHER *cipher; | |
022351fd | 524 | EVP_CIPHER *fetched_cipher; |
c49e0b04 RS |
525 | CIPHER_DATA *cdat; |
526 | int m; | |
6c5943c9 | 527 | |
022351fd RL |
528 | if ((cipher = fetched_cipher = EVP_CIPHER_fetch(NULL, alg, NULL)) == NULL |
529 | && (cipher = EVP_get_cipherbyname(alg)) == NULL) { | |
33a89fa6 DSH |
530 | /* If alg has an OID assume disabled algorithm */ |
531 | if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) { | |
532 | t->skip = 1; | |
533 | return 1; | |
534 | } | |
0f113f3e | 535 | return 0; |
33a89fa6 | 536 | } |
c49e0b04 | 537 | cdat = OPENSSL_zalloc(sizeof(*cdat)); |
307e3978 | 538 | cdat->cipher = cipher; |
022351fd | 539 | cdat->fetched_cipher = fetched_cipher; |
307e3978 | 540 | cdat->enc = -1; |
c49e0b04 RS |
541 | m = EVP_CIPHER_mode(cipher); |
542 | if (m == EVP_CIPH_GCM_MODE | |
543 | || m == EVP_CIPH_OCB_MODE | |
b1ceb439 | 544 | || m == EVP_CIPH_SIV_MODE |
c49e0b04 | 545 | || m == EVP_CIPH_CCM_MODE) |
523fcfb4 | 546 | cdat->aead = m; |
eb85cb86 AP |
547 | else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) |
548 | cdat->aead = -1; | |
307e3978 DSH |
549 | else |
550 | cdat->aead = 0; | |
4897dc40 | 551 | |
c49e0b04 | 552 | t->data = cdat; |
022351fd RL |
553 | if (fetched_cipher != NULL) |
554 | TEST_info("%s is fetched", alg); | |
307e3978 DSH |
555 | return 1; |
556 | } | |
4897dc40 | 557 | |
6c5943c9 | 558 | static void cipher_test_cleanup(EVP_TEST *t) |
307e3978 | 559 | { |
b1ceb439 | 560 | int i; |
6c5943c9 RS |
561 | CIPHER_DATA *cdat = t->data; |
562 | ||
563 | OPENSSL_free(cdat->key); | |
564 | OPENSSL_free(cdat->iv); | |
565 | OPENSSL_free(cdat->ciphertext); | |
566 | OPENSSL_free(cdat->plaintext); | |
b1ceb439 TS |
567 | for (i = 0; i < AAD_NUM; i++) |
568 | OPENSSL_free(cdat->aad[i]); | |
6c5943c9 | 569 | OPENSSL_free(cdat->tag); |
022351fd | 570 | EVP_CIPHER_meth_free(cdat->fetched_cipher); |
307e3978 | 571 | } |
4897dc40 | 572 | |
6c5943c9 | 573 | static int cipher_test_parse(EVP_TEST *t, const char *keyword, |
307e3978 DSH |
574 | const char *value) |
575 | { | |
6c5943c9 | 576 | CIPHER_DATA *cdat = t->data; |
b1ceb439 | 577 | int i; |
6c5943c9 | 578 | |
86885c28 | 579 | if (strcmp(keyword, "Key") == 0) |
c49e0b04 | 580 | return parse_bin(value, &cdat->key, &cdat->key_len); |
6a41156c SL |
581 | if (strcmp(keyword, "Rounds") == 0) { |
582 | i = atoi(value); | |
583 | if (i < 0) | |
584 | return -1; | |
585 | cdat->rounds = (unsigned int)i; | |
586 | return 1; | |
587 | } | |
86885c28 | 588 | if (strcmp(keyword, "IV") == 0) |
c49e0b04 | 589 | return parse_bin(value, &cdat->iv, &cdat->iv_len); |
86885c28 | 590 | if (strcmp(keyword, "Plaintext") == 0) |
c49e0b04 | 591 | return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len); |
86885c28 | 592 | if (strcmp(keyword, "Ciphertext") == 0) |
c49e0b04 | 593 | return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len); |
f816aa47 SL |
594 | if (strcmp(keyword, "KeyBits") == 0) { |
595 | i = atoi(value); | |
596 | if (i < 0) | |
597 | return -1; | |
598 | cdat->key_bits = (size_t)i; | |
599 | return 1; | |
600 | } | |
307e3978 | 601 | if (cdat->aead) { |
b1ceb439 TS |
602 | if (strcmp(keyword, "AAD") == 0) { |
603 | for (i = 0; i < AAD_NUM; i++) { | |
604 | if (cdat->aad[i] == NULL) | |
605 | return parse_bin(value, &cdat->aad[i], &cdat->aad_len[i]); | |
606 | } | |
f42c225d | 607 | return -1; |
b1ceb439 | 608 | } |
86885c28 | 609 | if (strcmp(keyword, "Tag") == 0) |
c49e0b04 | 610 | return parse_bin(value, &cdat->tag, &cdat->tag_len); |
67c81ec3 TN |
611 | if (strcmp(keyword, "SetTagLate") == 0) { |
612 | if (strcmp(value, "TRUE") == 0) | |
613 | cdat->tag_late = 1; | |
614 | else if (strcmp(value, "FALSE") == 0) | |
615 | cdat->tag_late = 0; | |
616 | else | |
f42c225d | 617 | return -1; |
67c81ec3 TN |
618 | return 1; |
619 | } | |
0f113f3e | 620 | } |
4897dc40 | 621 | |
86885c28 RS |
622 | if (strcmp(keyword, "Operation") == 0) { |
623 | if (strcmp(value, "ENCRYPT") == 0) | |
307e3978 | 624 | cdat->enc = 1; |
86885c28 | 625 | else if (strcmp(value, "DECRYPT") == 0) |
307e3978 DSH |
626 | cdat->enc = 0; |
627 | else | |
f42c225d | 628 | return -1; |
307e3978 | 629 | return 1; |
0f113f3e | 630 | } |
307e3978 | 631 | return 0; |
0f113f3e | 632 | } |
4897dc40 | 633 | |
6c5943c9 | 634 | static int cipher_test_enc(EVP_TEST *t, int enc, |
0b96d77a | 635 | size_t out_misalign, size_t inp_misalign, int frag) |
0f113f3e | 636 | { |
e3d378bc AP |
637 | CIPHER_DATA *expected = t->data; |
638 | unsigned char *in, *expected_out, *tmp = NULL; | |
0b96d77a | 639 | size_t in_len, out_len, donelen = 0; |
b1ceb439 | 640 | int ok = 0, tmplen, chunklen, tmpflen, i; |
f75abcc0 | 641 | EVP_CIPHER_CTX *ctx_base = NULL; |
307e3978 | 642 | EVP_CIPHER_CTX *ctx = NULL; |
6c5943c9 RS |
643 | |
644 | t->err = "TEST_FAILURE"; | |
f75abcc0 SL |
645 | if (!TEST_ptr(ctx_base = EVP_CIPHER_CTX_new())) |
646 | goto err; | |
6c5943c9 | 647 | if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) |
307e3978 | 648 | goto err; |
f75abcc0 | 649 | EVP_CIPHER_CTX_set_flags(ctx_base, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW); |
307e3978 | 650 | if (enc) { |
e3d378bc AP |
651 | in = expected->plaintext; |
652 | in_len = expected->plaintext_len; | |
653 | expected_out = expected->ciphertext; | |
654 | out_len = expected->ciphertext_len; | |
307e3978 | 655 | } else { |
e3d378bc AP |
656 | in = expected->ciphertext; |
657 | in_len = expected->ciphertext_len; | |
658 | expected_out = expected->plaintext; | |
659 | out_len = expected->plaintext_len; | |
0f113f3e | 660 | } |
ff715da4 AP |
661 | if (inp_misalign == (size_t)-1) { |
662 | /* | |
663 | * Exercise in-place encryption | |
664 | */ | |
665 | tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH); | |
666 | if (!tmp) | |
667 | goto err; | |
668 | in = memcpy(tmp + out_misalign, in, in_len); | |
669 | } else { | |
670 | inp_misalign += 16 - ((out_misalign + in_len) & 15); | |
671 | /* | |
672 | * 'tmp' will store both output and copy of input. We make the copy | |
673 | * of input to specifically aligned part of 'tmp'. So we just | |
674 | * figured out how much padding would ensure the required alignment, | |
675 | * now we allocate extended buffer and finally copy the input just | |
676 | * past inp_misalign in expression below. Output will be written | |
677 | * past out_misalign... | |
678 | */ | |
679 | tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH + | |
680 | inp_misalign + in_len); | |
681 | if (!tmp) | |
682 | goto err; | |
683 | in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH + | |
684 | inp_misalign, in, in_len); | |
685 | } | |
f75abcc0 | 686 | if (!EVP_CipherInit_ex(ctx_base, expected->cipher, NULL, NULL, NULL, enc)) { |
6c5943c9 | 687 | t->err = "CIPHERINIT_ERROR"; |
307e3978 | 688 | goto err; |
6c5943c9 | 689 | } |
e3d378bc AP |
690 | if (expected->iv) { |
691 | if (expected->aead) { | |
f75abcc0 | 692 | if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_IVLEN, |
e3d378bc | 693 | expected->iv_len, 0)) { |
6c5943c9 | 694 | t->err = "INVALID_IV_LENGTH"; |
307e3978 | 695 | goto err; |
6c5943c9 | 696 | } |
f75abcc0 | 697 | } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx_base)) { |
6c5943c9 | 698 | t->err = "INVALID_IV_LENGTH"; |
307e3978 | 699 | goto err; |
6c5943c9 | 700 | } |
0f113f3e | 701 | } |
e3d378bc | 702 | if (expected->aead) { |
307e3978 DSH |
703 | unsigned char *tag; |
704 | /* | |
2207ba7b DSH |
705 | * If encrypting or OCB just set tag length initially, otherwise |
706 | * set tag length and value. | |
307e3978 | 707 | */ |
67c81ec3 | 708 | if (enc || expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late) { |
6c5943c9 | 709 | t->err = "TAG_LENGTH_SET_ERROR"; |
307e3978 | 710 | tag = NULL; |
0f113f3e | 711 | } else { |
6c5943c9 | 712 | t->err = "TAG_SET_ERROR"; |
e3d378bc | 713 | tag = expected->tag; |
0f113f3e | 714 | } |
e3d378bc | 715 | if (tag || expected->aead != EVP_CIPH_GCM_MODE) { |
f75abcc0 | 716 | if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_AEAD_SET_TAG, |
e3d378bc | 717 | expected->tag_len, tag)) |
307e3978 | 718 | goto err; |
0f113f3e | 719 | } |
307e3978 | 720 | } |
0f113f3e | 721 | |
6a41156c SL |
722 | if (expected->rounds > 0) { |
723 | int rounds = (int)expected->rounds; | |
724 | ||
f75abcc0 | 725 | if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC5_ROUNDS, rounds, NULL)) { |
6a41156c SL |
726 | t->err = "INVALID_ROUNDS"; |
727 | goto err; | |
728 | } | |
729 | } | |
730 | ||
f75abcc0 | 731 | if (!EVP_CIPHER_CTX_set_key_length(ctx_base, expected->key_len)) { |
6c5943c9 | 732 | t->err = "INVALID_KEY_LENGTH"; |
307e3978 | 733 | goto err; |
6c5943c9 | 734 | } |
f816aa47 SL |
735 | if (expected->key_bits > 0) { |
736 | int bits = (int)expected->key_bits; | |
737 | ||
f75abcc0 | 738 | if (!EVP_CIPHER_CTX_ctrl(ctx_base, EVP_CTRL_SET_RC2_KEY_BITS, bits, NULL)) { |
f816aa47 SL |
739 | t->err = "INVALID KEY BITS"; |
740 | goto err; | |
741 | } | |
742 | } | |
f75abcc0 | 743 | if (!EVP_CipherInit_ex(ctx_base, NULL, NULL, expected->key, expected->iv, -1)) { |
6c5943c9 | 744 | t->err = "KEY_SET_ERROR"; |
307e3978 | 745 | goto err; |
6c5943c9 | 746 | } |
f816aa47 | 747 | |
48ebde22 RL |
748 | /* Check that we get the same IV back */ |
749 | if (expected->iv != NULL | |
750 | && (EVP_CIPHER_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0 | |
751 | && !TEST_mem_eq(expected->iv, expected->iv_len, | |
f75abcc0 | 752 | EVP_CIPHER_CTX_iv(ctx_base), expected->iv_len)) { |
48ebde22 RL |
753 | t->err = "INVALID_IV"; |
754 | goto err; | |
755 | } | |
307e3978 | 756 | |
f75abcc0 SL |
757 | /* Test that the cipher dup functions correctly if it is supported */ |
758 | if (EVP_CIPHER_CTX_copy(ctx, ctx_base)) { | |
759 | EVP_CIPHER_CTX_free(ctx_base); | |
760 | ctx_base = NULL; | |
761 | } else { | |
762 | EVP_CIPHER_CTX_free(ctx); | |
763 | ctx = ctx_base; | |
764 | } | |
765 | ||
e3d378bc | 766 | if (expected->aead == EVP_CIPH_CCM_MODE) { |
307e3978 | 767 | if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) { |
6c5943c9 | 768 | t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR"; |
307e3978 | 769 | goto err; |
0f113f3e MC |
770 | } |
771 | } | |
b1ceb439 | 772 | if (expected->aad[0] != NULL) { |
6c5943c9 | 773 | t->err = "AAD_SET_ERROR"; |
0b96d77a | 774 | if (!frag) { |
b1ceb439 TS |
775 | for (i = 0; expected->aad[i] != NULL; i++) { |
776 | if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], | |
777 | expected->aad_len[i])) | |
778 | goto err; | |
779 | } | |
0b96d77a MC |
780 | } else { |
781 | /* | |
782 | * Supply the AAD in chunks less than the block size where possible | |
783 | */ | |
b1ceb439 TS |
784 | for (i = 0; expected->aad[i] != NULL; i++) { |
785 | if (expected->aad_len[i] > 0) { | |
786 | if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], 1)) | |
787 | goto err; | |
788 | donelen++; | |
789 | } | |
790 | if (expected->aad_len[i] > 2) { | |
791 | if (!EVP_CipherUpdate(ctx, NULL, &chunklen, | |
792 | expected->aad[i] + donelen, | |
793 | expected->aad_len[i] - 2)) | |
794 | goto err; | |
795 | donelen += expected->aad_len[i] - 2; | |
796 | } | |
797 | if (expected->aad_len[i] > 1 | |
798 | && !EVP_CipherUpdate(ctx, NULL, &chunklen, | |
799 | expected->aad[i] + donelen, 1)) | |
0b96d77a | 800 | goto err; |
0b96d77a | 801 | } |
307e3978 DSH |
802 | } |
803 | } | |
67c81ec3 TN |
804 | |
805 | if (!enc && (expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late)) { | |
806 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, | |
807 | expected->tag_len, expected->tag)) { | |
808 | t->err = "TAG_SET_ERROR"; | |
809 | goto err; | |
810 | } | |
811 | } | |
812 | ||
307e3978 | 813 | EVP_CIPHER_CTX_set_padding(ctx, 0); |
6c5943c9 | 814 | t->err = "CIPHERUPDATE_ERROR"; |
0b96d77a | 815 | tmplen = 0; |
0b96d77a MC |
816 | if (!frag) { |
817 | /* We supply the data all in one go */ | |
818 | if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len)) | |
819 | goto err; | |
820 | } else { | |
821 | /* Supply the data in chunks less than the block size where possible */ | |
822 | if (in_len > 0) { | |
823 | if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1)) | |
824 | goto err; | |
825 | tmplen += chunklen; | |
ef055ec5 MC |
826 | in++; |
827 | in_len--; | |
0b96d77a | 828 | } |
ef055ec5 | 829 | if (in_len > 1) { |
0b96d77a | 830 | if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen, |
ef055ec5 | 831 | in, in_len - 1)) |
0b96d77a MC |
832 | goto err; |
833 | tmplen += chunklen; | |
ef055ec5 MC |
834 | in += in_len - 1; |
835 | in_len = 1; | |
0b96d77a | 836 | } |
ef055ec5 | 837 | if (in_len > 0 ) { |
0b96d77a | 838 | if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen, |
ef055ec5 | 839 | in, 1)) |
0b96d77a MC |
840 | goto err; |
841 | tmplen += chunklen; | |
842 | } | |
843 | } | |
6c5943c9 RS |
844 | if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) { |
845 | t->err = "CIPHERFINAL_ERROR"; | |
00212c66 | 846 | goto err; |
6c5943c9 | 847 | } |
4cceb185 P |
848 | if (!memory_err_compare(t, "VALUE_MISMATCH", expected_out, out_len, |
849 | tmp + out_misalign, tmplen + tmpflen)) | |
307e3978 | 850 | goto err; |
e3d378bc | 851 | if (enc && expected->aead) { |
307e3978 | 852 | unsigned char rtag[16]; |
6c5943c9 | 853 | |
e3d378bc | 854 | if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) { |
6c5943c9 | 855 | t->err = "TAG_LENGTH_INTERNAL_ERROR"; |
307e3978 DSH |
856 | goto err; |
857 | } | |
2207ba7b | 858 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, |
e3d378bc | 859 | expected->tag_len, rtag)) { |
6c5943c9 | 860 | t->err = "TAG_RETRIEVE_ERROR"; |
307e3978 DSH |
861 | goto err; |
862 | } | |
4cceb185 P |
863 | if (!memory_err_compare(t, "TAG_VALUE_MISMATCH", |
864 | expected->tag, expected->tag_len, | |
865 | rtag, expected->tag_len)) | |
307e3978 | 866 | goto err; |
307e3978 | 867 | } |
6c5943c9 RS |
868 | t->err = NULL; |
869 | ok = 1; | |
307e3978 | 870 | err: |
b548a1f1 | 871 | OPENSSL_free(tmp); |
f75abcc0 SL |
872 | if (ctx != ctx_base) |
873 | EVP_CIPHER_CTX_free(ctx_base); | |
307e3978 | 874 | EVP_CIPHER_CTX_free(ctx); |
6c5943c9 | 875 | return ok; |
307e3978 | 876 | } |
0e360199 | 877 | |
6c5943c9 | 878 | static int cipher_test_run(EVP_TEST *t) |
307e3978 | 879 | { |
6c5943c9 | 880 | CIPHER_DATA *cdat = t->data; |
0b96d77a | 881 | int rv, frag = 0; |
9a2d2fb3 AP |
882 | size_t out_misalign, inp_misalign; |
883 | ||
307e3978 DSH |
884 | if (!cdat->key) { |
885 | t->err = "NO_KEY"; | |
886 | return 0; | |
887 | } | |
888 | if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) { | |
889 | /* IV is optional and usually omitted in wrap mode */ | |
890 | if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) { | |
891 | t->err = "NO_IV"; | |
892 | return 0; | |
893 | } | |
894 | } | |
895 | if (cdat->aead && !cdat->tag) { | |
896 | t->err = "NO_TAG"; | |
897 | return 0; | |
898 | } | |
0b96d77a | 899 | for (out_misalign = 0; out_misalign <= 1;) { |
9a2d2fb3 AP |
900 | static char aux_err[64]; |
901 | t->aux_err = aux_err; | |
ff715da4 AP |
902 | for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) { |
903 | if (inp_misalign == (size_t)-1) { | |
904 | /* kludge: inp_misalign == -1 means "exercise in-place" */ | |
0b96d77a MC |
905 | BIO_snprintf(aux_err, sizeof(aux_err), |
906 | "%s in-place, %sfragmented", | |
907 | out_misalign ? "misaligned" : "aligned", | |
908 | frag ? "" : "not "); | |
ff715da4 | 909 | } else { |
0b96d77a MC |
910 | BIO_snprintf(aux_err, sizeof(aux_err), |
911 | "%s output and %s input, %sfragmented", | |
ff715da4 | 912 | out_misalign ? "misaligned" : "aligned", |
0b96d77a MC |
913 | inp_misalign ? "misaligned" : "aligned", |
914 | frag ? "" : "not "); | |
ff715da4 | 915 | } |
9a2d2fb3 | 916 | if (cdat->enc) { |
0b96d77a | 917 | rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag); |
9a2d2fb3 AP |
918 | /* Not fatal errors: return */ |
919 | if (rv != 1) { | |
920 | if (rv < 0) | |
921 | return 0; | |
922 | return 1; | |
923 | } | |
924 | } | |
925 | if (cdat->enc != 1) { | |
0b96d77a | 926 | rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag); |
9a2d2fb3 AP |
927 | /* Not fatal errors: return */ |
928 | if (rv != 1) { | |
929 | if (rv < 0) | |
930 | return 0; | |
931 | return 1; | |
932 | } | |
933 | } | |
307e3978 | 934 | } |
0b96d77a MC |
935 | |
936 | if (out_misalign == 1 && frag == 0) { | |
937 | /* | |
b1ceb439 | 938 | * XTS, SIV, CCM and Wrap modes have special requirements about input |
0b96d77a MC |
939 | * lengths so we don't fragment for those |
940 | */ | |
941 | if (cdat->aead == EVP_CIPH_CCM_MODE | |
b1ceb439 | 942 | || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_SIV_MODE |
0b96d77a | 943 | || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE |
e3d378bc | 944 | || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE) |
0b96d77a MC |
945 | break; |
946 | out_misalign = 0; | |
947 | frag++; | |
948 | } else { | |
949 | out_misalign++; | |
950 | } | |
307e3978 | 951 | } |
9a2d2fb3 AP |
952 | t->aux_err = NULL; |
953 | ||
307e3978 | 954 | return 1; |
0f113f3e | 955 | } |
307e3978 | 956 | |
6c5943c9 | 957 | static const EVP_TEST_METHOD cipher_test_method = { |
307e3978 DSH |
958 | "Cipher", |
959 | cipher_test_init, | |
960 | cipher_test_cleanup, | |
961 | cipher_test_parse, | |
962 | cipher_test_run | |
963 | }; | |
83251f39 | 964 | |
c49e0b04 RS |
965 | |
966 | /** | |
967 | *** MAC TESTS | |
968 | **/ | |
969 | ||
6c5943c9 | 970 | typedef struct mac_data_st { |
2bdb4af5 | 971 | /* MAC type in one form or another */ |
f651c727 | 972 | char *mac_name; |
25446a66 | 973 | EVP_MAC *mac; /* for mac_test_run_mac */ |
2bdb4af5 | 974 | int type; /* for mac_test_run_pkey */ |
83251f39 DSH |
975 | /* Algorithm string for this MAC */ |
976 | char *alg; | |
977 | /* MAC key */ | |
978 | unsigned char *key; | |
979 | size_t key_len; | |
afc580b9 P |
980 | /* MAC IV (GMAC) */ |
981 | unsigned char *iv; | |
982 | size_t iv_len; | |
83251f39 DSH |
983 | /* Input to MAC */ |
984 | unsigned char *input; | |
985 | size_t input_len; | |
986 | /* Expected output */ | |
987 | unsigned char *output; | |
988 | size_t output_len; | |
6e624a64 SL |
989 | unsigned char *custom; |
990 | size_t custom_len; | |
b215db23 AS |
991 | /* MAC salt (blake2) */ |
992 | unsigned char *salt; | |
993 | size_t salt_len; | |
7e6a3025 RL |
994 | /* Collection of controls */ |
995 | STACK_OF(OPENSSL_STRING) *controls; | |
6c5943c9 | 996 | } MAC_DATA; |
83251f39 | 997 | |
6c5943c9 | 998 | static int mac_test_init(EVP_TEST *t, const char *alg) |
83251f39 | 999 | { |
25446a66 | 1000 | EVP_MAC *mac = NULL; |
2bdb4af5 | 1001 | int type = NID_undef; |
6c5943c9 RS |
1002 | MAC_DATA *mdat; |
1003 | ||
25446a66 | 1004 | if ((mac = EVP_MAC_fetch(NULL, alg, NULL)) == NULL) { |
2bdb4af5 RL |
1005 | /* |
1006 | * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods | |
1007 | * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running | |
1008 | * the EVP_PKEY method. | |
1009 | */ | |
1010 | size_t sz = strlen(alg); | |
1011 | static const char epilogue[] = " by EVP_PKEY"; | |
1012 | ||
88e3cf0a RL |
1013 | if (sz >= sizeof(epilogue) |
1014 | && strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0) | |
2bdb4af5 RL |
1015 | sz -= sizeof(epilogue) - 1; |
1016 | ||
1017 | if (strncmp(alg, "HMAC", sz) == 0) { | |
1018 | type = EVP_PKEY_HMAC; | |
1019 | } else if (strncmp(alg, "CMAC", sz) == 0) { | |
b4a3aeeb | 1020 | #ifndef OPENSSL_NO_CMAC |
2bdb4af5 | 1021 | type = EVP_PKEY_CMAC; |
b4a3aeeb | 1022 | #else |
2bdb4af5 RL |
1023 | t->skip = 1; |
1024 | return 1; | |
52ad5b60 | 1025 | #endif |
2bdb4af5 | 1026 | } else if (strncmp(alg, "Poly1305", sz) == 0) { |
52ad5b60 | 1027 | #ifndef OPENSSL_NO_POLY1305 |
2bdb4af5 | 1028 | type = EVP_PKEY_POLY1305; |
52ad5b60 | 1029 | #else |
2bdb4af5 RL |
1030 | t->skip = 1; |
1031 | return 1; | |
3f5616d7 | 1032 | #endif |
2bdb4af5 | 1033 | } else if (strncmp(alg, "SipHash", sz) == 0) { |
3f5616d7 | 1034 | #ifndef OPENSSL_NO_SIPHASH |
2bdb4af5 | 1035 | type = EVP_PKEY_SIPHASH; |
3f5616d7 | 1036 | #else |
2bdb4af5 RL |
1037 | t->skip = 1; |
1038 | return 1; | |
b4a3aeeb | 1039 | #endif |
2bdb4af5 RL |
1040 | } else { |
1041 | /* | |
1042 | * Not a known EVP_PKEY method either. If it's a known OID, then | |
1043 | * assume it's been disabled. | |
1044 | */ | |
1045 | if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) { | |
1046 | t->skip = 1; | |
1047 | return 1; | |
1048 | } | |
1049 | ||
1050 | return 0; | |
1051 | } | |
1052 | } | |
83251f39 | 1053 | |
6c5943c9 | 1054 | mdat = OPENSSL_zalloc(sizeof(*mdat)); |
83251f39 | 1055 | mdat->type = type; |
f651c727 | 1056 | mdat->mac_name = OPENSSL_strdup(alg); |
2bdb4af5 | 1057 | mdat->mac = mac; |
7e6a3025 | 1058 | mdat->controls = sk_OPENSSL_STRING_new_null(); |
83251f39 DSH |
1059 | t->data = mdat; |
1060 | return 1; | |
1061 | } | |
1062 | ||
7e6a3025 RL |
1063 | /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */ |
1064 | static void openssl_free(char *m) | |
1065 | { | |
1066 | OPENSSL_free(m); | |
1067 | } | |
1068 | ||
6c5943c9 | 1069 | static void mac_test_cleanup(EVP_TEST *t) |
83251f39 | 1070 | { |
6c5943c9 RS |
1071 | MAC_DATA *mdat = t->data; |
1072 | ||
25446a66 | 1073 | EVP_MAC_free(mdat->mac); |
f651c727 | 1074 | OPENSSL_free(mdat->mac_name); |
7e6a3025 | 1075 | sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free); |
6c5943c9 RS |
1076 | OPENSSL_free(mdat->alg); |
1077 | OPENSSL_free(mdat->key); | |
afc580b9 | 1078 | OPENSSL_free(mdat->iv); |
6e624a64 | 1079 | OPENSSL_free(mdat->custom); |
b215db23 | 1080 | OPENSSL_free(mdat->salt); |
6c5943c9 RS |
1081 | OPENSSL_free(mdat->input); |
1082 | OPENSSL_free(mdat->output); | |
83251f39 DSH |
1083 | } |
1084 | ||
6c5943c9 | 1085 | static int mac_test_parse(EVP_TEST *t, |
83251f39 DSH |
1086 | const char *keyword, const char *value) |
1087 | { | |
6c5943c9 RS |
1088 | MAC_DATA *mdata = t->data; |
1089 | ||
86885c28 | 1090 | if (strcmp(keyword, "Key") == 0) |
c49e0b04 | 1091 | return parse_bin(value, &mdata->key, &mdata->key_len); |
afc580b9 P |
1092 | if (strcmp(keyword, "IV") == 0) |
1093 | return parse_bin(value, &mdata->iv, &mdata->iv_len); | |
6e624a64 SL |
1094 | if (strcmp(keyword, "Custom") == 0) |
1095 | return parse_bin(value, &mdata->custom, &mdata->custom_len); | |
b215db23 AS |
1096 | if (strcmp(keyword, "Salt") == 0) |
1097 | return parse_bin(value, &mdata->salt, &mdata->salt_len); | |
86885c28 | 1098 | if (strcmp(keyword, "Algorithm") == 0) { |
7644a9ae | 1099 | mdata->alg = OPENSSL_strdup(value); |
83251f39 | 1100 | if (!mdata->alg) |
f42c225d | 1101 | return -1; |
83251f39 DSH |
1102 | return 1; |
1103 | } | |
86885c28 | 1104 | if (strcmp(keyword, "Input") == 0) |
c49e0b04 | 1105 | return parse_bin(value, &mdata->input, &mdata->input_len); |
86885c28 | 1106 | if (strcmp(keyword, "Output") == 0) |
c49e0b04 | 1107 | return parse_bin(value, &mdata->output, &mdata->output_len); |
7e6a3025 RL |
1108 | if (strcmp(keyword, "Ctrl") == 0) |
1109 | return sk_OPENSSL_STRING_push(mdata->controls, | |
1110 | OPENSSL_strdup(value)) != 0; | |
83251f39 DSH |
1111 | return 0; |
1112 | } | |
1113 | ||
ce5d64c7 RL |
1114 | static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx, |
1115 | const char *value) | |
1116 | { | |
1117 | int rv; | |
1118 | char *p, *tmpval; | |
1119 | ||
1120 | if (!TEST_ptr(tmpval = OPENSSL_strdup(value))) | |
1121 | return 0; | |
1122 | p = strchr(tmpval, ':'); | |
1123 | if (p != NULL) | |
1124 | *p++ = '\0'; | |
1125 | rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p); | |
1126 | if (rv == -2) | |
1127 | t->err = "PKEY_CTRL_INVALID"; | |
1128 | else if (rv <= 0) | |
1129 | t->err = "PKEY_CTRL_ERROR"; | |
1130 | else | |
1131 | rv = 1; | |
1132 | OPENSSL_free(tmpval); | |
1133 | return rv > 0; | |
1134 | } | |
1135 | ||
2bdb4af5 | 1136 | static int mac_test_run_pkey(EVP_TEST *t) |
83251f39 | 1137 | { |
e3d378bc | 1138 | MAC_DATA *expected = t->data; |
83251f39 DSH |
1139 | EVP_MD_CTX *mctx = NULL; |
1140 | EVP_PKEY_CTX *pctx = NULL, *genctx = NULL; | |
1141 | EVP_PKEY *key = NULL; | |
1142 | const EVP_MD *md = NULL; | |
e3d378bc AP |
1143 | unsigned char *got = NULL; |
1144 | size_t got_len; | |
7e6a3025 | 1145 | int i; |
83251f39 | 1146 | |
2bdb4af5 RL |
1147 | if (expected->alg == NULL) |
1148 | TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type)); | |
1149 | else | |
1150 | TEST_info("Trying the EVP_PKEY %s test with %s", | |
1151 | OBJ_nid2sn(expected->type), expected->alg); | |
1152 | ||
96bea000 | 1153 | #ifdef OPENSSL_NO_DES |
e3d378bc | 1154 | if (expected->alg != NULL && strstr(expected->alg, "DES") != NULL) { |
96bea000 | 1155 | /* Skip DES */ |
6c5943c9 | 1156 | t->err = NULL; |
96bea000 MC |
1157 | goto err; |
1158 | } | |
1159 | #endif | |
1160 | ||
9442c8d7 MC |
1161 | if (expected->type == EVP_PKEY_CMAC) |
1162 | key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len, | |
1163 | EVP_get_cipherbyname(expected->alg)); | |
1164 | else | |
f929439f MC |
1165 | key = EVP_PKEY_new_raw_private_key(expected->type, NULL, expected->key, |
1166 | expected->key_len); | |
9442c8d7 MC |
1167 | if (key == NULL) { |
1168 | t->err = "MAC_KEY_CREATE_ERROR"; | |
83251f39 | 1169 | goto err; |
6c5943c9 | 1170 | } |
83251f39 | 1171 | |
e3d378bc AP |
1172 | if (expected->type == EVP_PKEY_HMAC) { |
1173 | if (!TEST_ptr(md = EVP_get_digestbyname(expected->alg))) { | |
6c5943c9 | 1174 | t->err = "MAC_ALGORITHM_SET_ERROR"; |
83251f39 | 1175 | goto err; |
6c5943c9 | 1176 | } |
83251f39 | 1177 | } |
6c5943c9 RS |
1178 | if (!TEST_ptr(mctx = EVP_MD_CTX_new())) { |
1179 | t->err = "INTERNAL_ERROR"; | |
83251f39 | 1180 | goto err; |
6c5943c9 RS |
1181 | } |
1182 | if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key)) { | |
1183 | t->err = "DIGESTSIGNINIT_ERROR"; | |
83251f39 | 1184 | goto err; |
6c5943c9 | 1185 | } |
7e6a3025 | 1186 | for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) |
ce5d64c7 RL |
1187 | if (!mac_test_ctrl_pkey(t, pctx, |
1188 | sk_OPENSSL_STRING_value(expected->controls, | |
1189 | i))) { | |
7e6a3025 RL |
1190 | t->err = "EVPPKEYCTXCTRL_ERROR"; |
1191 | goto err; | |
1192 | } | |
e3d378bc | 1193 | if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) { |
6c5943c9 | 1194 | t->err = "DIGESTSIGNUPDATE_ERROR"; |
83251f39 | 1195 | goto err; |
83251f39 | 1196 | } |
e3d378bc | 1197 | if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) { |
6c5943c9 | 1198 | t->err = "DIGESTSIGNFINAL_LENGTH_ERROR"; |
83251f39 | 1199 | goto err; |
6c5943c9 | 1200 | } |
e3d378bc | 1201 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { |
6c5943c9 | 1202 | t->err = "TEST_FAILURE"; |
83251f39 | 1203 | goto err; |
6c5943c9 | 1204 | } |
e3d378bc | 1205 | if (!EVP_DigestSignFinal(mctx, got, &got_len) |
4cceb185 P |
1206 | || !memory_err_compare(t, "TEST_MAC_ERR", |
1207 | expected->output, expected->output_len, | |
1208 | got, got_len)) { | |
41248607 RS |
1209 | t->err = "TEST_MAC_ERR"; |
1210 | goto err; | |
1211 | } | |
6c5943c9 | 1212 | t->err = NULL; |
83251f39 | 1213 | err: |
bfb0641f | 1214 | EVP_MD_CTX_free(mctx); |
e3d378bc | 1215 | OPENSSL_free(got); |
c5ba2d99 RS |
1216 | EVP_PKEY_CTX_free(genctx); |
1217 | EVP_PKEY_free(key); | |
83251f39 DSH |
1218 | return 1; |
1219 | } | |
1220 | ||
2bdb4af5 RL |
1221 | static int mac_test_run_mac(EVP_TEST *t) |
1222 | { | |
1223 | MAC_DATA *expected = t->data; | |
1224 | EVP_MAC_CTX *ctx = NULL; | |
2bdb4af5 RL |
1225 | unsigned char *got = NULL; |
1226 | size_t got_len; | |
25446a66 RL |
1227 | int i; |
1228 | OSSL_PARAM params[21]; | |
1229 | size_t params_n = 0; | |
1230 | size_t params_n_allocstart = 0; | |
1231 | const OSSL_PARAM *defined_params = | |
41f7ecf3 | 1232 | EVP_MAC_settable_ctx_params(expected->mac); |
2bdb4af5 RL |
1233 | |
1234 | if (expected->alg == NULL) | |
f651c727 | 1235 | TEST_info("Trying the EVP_MAC %s test", expected->mac_name); |
2bdb4af5 RL |
1236 | else |
1237 | TEST_info("Trying the EVP_MAC %s test with %s", | |
f651c727 | 1238 | expected->mac_name, expected->alg); |
2bdb4af5 RL |
1239 | |
1240 | #ifdef OPENSSL_NO_DES | |
1241 | if (expected->alg != NULL && strstr(expected->alg, "DES") != NULL) { | |
1242 | /* Skip DES */ | |
1243 | t->err = NULL; | |
1244 | goto err; | |
1245 | } | |
1246 | #endif | |
1247 | ||
703170d4 RL |
1248 | if (expected->alg != NULL) { |
1249 | /* | |
1250 | * The underlying algorithm may be a cipher or a digest. | |
1251 | * We don't know which it is, but we can ask the MAC what it | |
1252 | * should be and bet on that. | |
1253 | */ | |
1254 | if (OSSL_PARAM_locate_const(defined_params, | |
1255 | OSSL_MAC_PARAM_CIPHER) != NULL) { | |
1256 | params[params_n++] = | |
1257 | OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER, | |
7f588d20 | 1258 | expected->alg, 0); |
703170d4 RL |
1259 | } else if (OSSL_PARAM_locate_const(defined_params, |
1260 | OSSL_MAC_PARAM_DIGEST) != NULL) { | |
1261 | params[params_n++] = | |
1262 | OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, | |
7f588d20 | 1263 | expected->alg, 0); |
703170d4 RL |
1264 | } else { |
1265 | t->err = "MAC_BAD_PARAMS"; | |
1266 | goto err; | |
1267 | } | |
1268 | } | |
25446a66 RL |
1269 | if (expected->key != NULL) |
1270 | params[params_n++] = | |
1271 | OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY, | |
1272 | expected->key, | |
1273 | expected->key_len); | |
1274 | if (expected->custom != NULL) | |
1275 | params[params_n++] = | |
1276 | OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM, | |
1277 | expected->custom, | |
1278 | expected->custom_len); | |
1279 | if (expected->salt != NULL) | |
1280 | params[params_n++] = | |
1281 | OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT, | |
1282 | expected->salt, | |
1283 | expected->salt_len); | |
1284 | if (expected->iv != NULL) | |
1285 | params[params_n++] = | |
1286 | OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV, | |
1287 | expected->iv, | |
1288 | expected->iv_len); | |
1289 | ||
1290 | /* | |
1291 | * Unknown controls. They must match parameters that the MAC recognises | |
1292 | */ | |
1293 | if (params_n + sk_OPENSSL_STRING_num(expected->controls) | |
1294 | >= OSSL_NELEM(params)) { | |
1295 | t->err = "MAC_TOO_MANY_PARAMETERS"; | |
2bdb4af5 RL |
1296 | goto err; |
1297 | } | |
25446a66 RL |
1298 | params_n_allocstart = params_n; |
1299 | for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) { | |
1300 | char *tmpkey, *tmpval; | |
1301 | char *value = sk_OPENSSL_STRING_value(expected->controls, i); | |
2bdb4af5 | 1302 | |
25446a66 RL |
1303 | if (!TEST_ptr(tmpkey = OPENSSL_strdup(value))) { |
1304 | t->err = "MAC_PARAM_ERROR"; | |
2bdb4af5 RL |
1305 | goto err; |
1306 | } | |
25446a66 RL |
1307 | tmpval = strchr(tmpkey, ':'); |
1308 | if (tmpval != NULL) | |
1309 | *tmpval++ = '\0'; | |
1310 | ||
d5f85429 RL |
1311 | if (tmpval == NULL |
1312 | || !OSSL_PARAM_allocate_from_text(¶ms[params_n], | |
1313 | defined_params, | |
1314 | tmpkey, tmpval, | |
2ee0dfa6 | 1315 | strlen(tmpval), NULL)) { |
25446a66 RL |
1316 | OPENSSL_free(tmpkey); |
1317 | t->err = "MAC_PARAM_ERROR"; | |
6e624a64 SL |
1318 | goto err; |
1319 | } | |
25446a66 | 1320 | params_n++; |
2bdb4af5 | 1321 | |
25446a66 | 1322 | OPENSSL_free(tmpkey); |
b215db23 | 1323 | } |
25446a66 | 1324 | params[params_n] = OSSL_PARAM_construct_end(); |
b215db23 | 1325 | |
d9c2fd51 | 1326 | if ((ctx = EVP_MAC_new_ctx(expected->mac)) == NULL) { |
25446a66 RL |
1327 | t->err = "MAC_CREATE_ERROR"; |
1328 | goto err; | |
afc580b9 P |
1329 | } |
1330 | ||
d9c2fd51 | 1331 | if (!EVP_MAC_set_ctx_params(ctx, params)) { |
25446a66 RL |
1332 | t->err = "MAC_BAD_PARAMS"; |
1333 | goto err; | |
2bdb4af5 | 1334 | } |
b215db23 AS |
1335 | if (!EVP_MAC_init(ctx)) { |
1336 | t->err = "MAC_INIT_ERROR"; | |
1337 | goto err; | |
1338 | } | |
2bdb4af5 RL |
1339 | if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) { |
1340 | t->err = "MAC_UPDATE_ERROR"; | |
1341 | goto err; | |
1342 | } | |
25446a66 | 1343 | if (!EVP_MAC_final(ctx, NULL, &got_len, 0)) { |
2bdb4af5 RL |
1344 | t->err = "MAC_FINAL_LENGTH_ERROR"; |
1345 | goto err; | |
1346 | } | |
1347 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { | |
1348 | t->err = "TEST_FAILURE"; | |
1349 | goto err; | |
1350 | } | |
25446a66 | 1351 | if (!EVP_MAC_final(ctx, got, &got_len, got_len) |
2bdb4af5 RL |
1352 | || !memory_err_compare(t, "TEST_MAC_ERR", |
1353 | expected->output, expected->output_len, | |
1354 | got, got_len)) { | |
1355 | t->err = "TEST_MAC_ERR"; | |
1356 | goto err; | |
1357 | } | |
1358 | t->err = NULL; | |
1359 | err: | |
25446a66 RL |
1360 | while (params_n-- > params_n_allocstart) { |
1361 | OPENSSL_free(params[params_n].data); | |
1362 | } | |
d9c2fd51 | 1363 | EVP_MAC_free_ctx(ctx); |
2bdb4af5 RL |
1364 | OPENSSL_free(got); |
1365 | return 1; | |
1366 | } | |
1367 | ||
1368 | static int mac_test_run(EVP_TEST *t) | |
1369 | { | |
1370 | MAC_DATA *expected = t->data; | |
1371 | ||
1372 | if (expected->mac != NULL) | |
1373 | return mac_test_run_mac(t); | |
1374 | return mac_test_run_pkey(t); | |
1375 | } | |
1376 | ||
6c5943c9 | 1377 | static const EVP_TEST_METHOD mac_test_method = { |
83251f39 DSH |
1378 | "MAC", |
1379 | mac_test_init, | |
1380 | mac_test_cleanup, | |
1381 | mac_test_parse, | |
1382 | mac_test_run | |
1383 | }; | |
5824cc29 | 1384 | |
c49e0b04 RS |
1385 | |
1386 | /** | |
1387 | *** PUBLIC KEY TESTS | |
1388 | *** These are all very similar and share much common code. | |
1389 | **/ | |
5824cc29 | 1390 | |
6c5943c9 | 1391 | typedef struct pkey_data_st { |
5824cc29 DSH |
1392 | /* Context for this operation */ |
1393 | EVP_PKEY_CTX *ctx; | |
1394 | /* Key operation to perform */ | |
1395 | int (*keyop) (EVP_PKEY_CTX *ctx, | |
1396 | unsigned char *sig, size_t *siglen, | |
1397 | const unsigned char *tbs, size_t tbslen); | |
1398 | /* Input to MAC */ | |
1399 | unsigned char *input; | |
1400 | size_t input_len; | |
1401 | /* Expected output */ | |
1402 | unsigned char *output; | |
1403 | size_t output_len; | |
6c5943c9 | 1404 | } PKEY_DATA; |
5824cc29 DSH |
1405 | |
1406 | /* | |
1407 | * Perform public key operation setup: lookup key, allocated ctx and call | |
1408 | * the appropriate initialisation function | |
1409 | */ | |
6c5943c9 | 1410 | static int pkey_test_init(EVP_TEST *t, const char *name, |
5824cc29 DSH |
1411 | int use_public, |
1412 | int (*keyopinit) (EVP_PKEY_CTX *ctx), | |
c49e0b04 RS |
1413 | int (*keyop)(EVP_PKEY_CTX *ctx, |
1414 | unsigned char *sig, size_t *siglen, | |
1415 | const unsigned char *tbs, | |
1416 | size_t tbslen)) | |
5824cc29 | 1417 | { |
6c5943c9 | 1418 | PKEY_DATA *kdata; |
5824cc29 | 1419 | EVP_PKEY *pkey = NULL; |
7a6c9792 | 1420 | int rv = 0; |
6c5943c9 | 1421 | |
7a6c9792 | 1422 | if (use_public) |
6c5943c9 RS |
1423 | rv = find_key(&pkey, name, public_keys); |
1424 | if (rv == 0) | |
1425 | rv = find_key(&pkey, name, private_keys); | |
1426 | if (rv == 0 || pkey == NULL) { | |
7a6c9792 DSH |
1427 | t->skip = 1; |
1428 | return 1; | |
1429 | } | |
1430 | ||
c49e0b04 | 1431 | if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) { |
7a6c9792 | 1432 | EVP_PKEY_free(pkey); |
5824cc29 | 1433 | return 0; |
7a6c9792 | 1434 | } |
5824cc29 | 1435 | kdata->keyop = keyop; |
9e206ce5 P |
1436 | if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL))) { |
1437 | EVP_PKEY_free(pkey); | |
1438 | OPENSSL_free(kdata); | |
5824cc29 | 1439 | return 0; |
9e206ce5 | 1440 | } |
5824cc29 | 1441 | if (keyopinit(kdata->ctx) <= 0) |
cce65266 | 1442 | t->err = "KEYOP_INIT_ERROR"; |
c49e0b04 | 1443 | t->data = kdata; |
5824cc29 DSH |
1444 | return 1; |
1445 | } | |
1446 | ||
6c5943c9 | 1447 | static void pkey_test_cleanup(EVP_TEST *t) |
5824cc29 | 1448 | { |
6c5943c9 | 1449 | PKEY_DATA *kdata = t->data; |
b548a1f1 RS |
1450 | |
1451 | OPENSSL_free(kdata->input); | |
1452 | OPENSSL_free(kdata->output); | |
c5ba2d99 | 1453 | EVP_PKEY_CTX_free(kdata->ctx); |
5824cc29 DSH |
1454 | } |
1455 | ||
6c5943c9 | 1456 | static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx, |
dfbdf4ab | 1457 | const char *value) |
4ddd5ace DSH |
1458 | { |
1459 | int rv; | |
1460 | char *p, *tmpval; | |
1461 | ||
6c5943c9 | 1462 | if (!TEST_ptr(tmpval = OPENSSL_strdup(value))) |
4ddd5ace DSH |
1463 | return 0; |
1464 | p = strchr(tmpval, ':'); | |
1465 | if (p != NULL) | |
c49e0b04 | 1466 | *p++ = '\0'; |
4ddd5ace | 1467 | rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p); |
cce65266 DSH |
1468 | if (rv == -2) { |
1469 | t->err = "PKEY_CTRL_INVALID"; | |
1470 | rv = 1; | |
1471 | } else if (p != NULL && rv <= 0) { | |
1472 | /* If p has an OID and lookup fails assume disabled algorithm */ | |
1473 | int nid = OBJ_sn2nid(p); | |
6c5943c9 | 1474 | |
cce65266 DSH |
1475 | if (nid == NID_undef) |
1476 | nid = OBJ_ln2nid(p); | |
c49e0b04 RS |
1477 | if (nid != NID_undef |
1478 | && EVP_get_digestbynid(nid) == NULL | |
1479 | && EVP_get_cipherbynid(nid) == NULL) { | |
dfbdf4ab RL |
1480 | t->skip = 1; |
1481 | rv = 1; | |
cce65266 DSH |
1482 | } else { |
1483 | t->err = "PKEY_CTRL_ERROR"; | |
1484 | rv = 1; | |
dfbdf4ab RL |
1485 | } |
1486 | } | |
4ddd5ace DSH |
1487 | OPENSSL_free(tmpval); |
1488 | return rv > 0; | |
1489 | } | |
1490 | ||
6c5943c9 | 1491 | static int pkey_test_parse(EVP_TEST *t, |
5824cc29 DSH |
1492 | const char *keyword, const char *value) |
1493 | { | |
6c5943c9 | 1494 | PKEY_DATA *kdata = t->data; |
86885c28 | 1495 | if (strcmp(keyword, "Input") == 0) |
c49e0b04 | 1496 | return parse_bin(value, &kdata->input, &kdata->input_len); |
86885c28 | 1497 | if (strcmp(keyword, "Output") == 0) |
c49e0b04 | 1498 | return parse_bin(value, &kdata->output, &kdata->output_len); |
4ddd5ace | 1499 | if (strcmp(keyword, "Ctrl") == 0) |
dfbdf4ab | 1500 | return pkey_test_ctrl(t, kdata->ctx, value); |
5824cc29 DSH |
1501 | return 0; |
1502 | } | |
1503 | ||
6c5943c9 | 1504 | static int pkey_test_run(EVP_TEST *t) |
5824cc29 | 1505 | { |
e3d378bc AP |
1506 | PKEY_DATA *expected = t->data; |
1507 | unsigned char *got = NULL; | |
1508 | size_t got_len; | |
d7fcf1fe | 1509 | EVP_PKEY_CTX *copy = NULL; |
6c5943c9 | 1510 | |
e3d378bc AP |
1511 | if (expected->keyop(expected->ctx, NULL, &got_len, |
1512 | expected->input, expected->input_len) <= 0 | |
1513 | || !TEST_ptr(got = OPENSSL_malloc(got_len))) { | |
6c5943c9 | 1514 | t->err = "KEYOP_LENGTH_ERROR"; |
5824cc29 | 1515 | goto err; |
6c5943c9 | 1516 | } |
e3d378bc AP |
1517 | if (expected->keyop(expected->ctx, got, &got_len, |
1518 | expected->input, expected->input_len) <= 0) { | |
6c5943c9 | 1519 | t->err = "KEYOP_ERROR"; |
5824cc29 | 1520 | goto err; |
6c5943c9 | 1521 | } |
4cceb185 P |
1522 | if (!memory_err_compare(t, "KEYOP_MISMATCH", |
1523 | expected->output, expected->output_len, | |
1524 | got, got_len)) | |
5824cc29 | 1525 | goto err; |
4cceb185 | 1526 | |
6c5943c9 | 1527 | t->err = NULL; |
d7fcf1fe DB |
1528 | OPENSSL_free(got); |
1529 | got = NULL; | |
1530 | ||
1531 | /* Repeat the test on a copy. */ | |
1532 | if (!TEST_ptr(copy = EVP_PKEY_CTX_dup(expected->ctx))) { | |
1533 | t->err = "INTERNAL_ERROR"; | |
1534 | goto err; | |
1535 | } | |
1536 | if (expected->keyop(copy, NULL, &got_len, expected->input, | |
1537 | expected->input_len) <= 0 | |
1538 | || !TEST_ptr(got = OPENSSL_malloc(got_len))) { | |
1539 | t->err = "KEYOP_LENGTH_ERROR"; | |
1540 | goto err; | |
1541 | } | |
1542 | if (expected->keyop(copy, got, &got_len, expected->input, | |
1543 | expected->input_len) <= 0) { | |
1544 | t->err = "KEYOP_ERROR"; | |
1545 | goto err; | |
1546 | } | |
1547 | if (!memory_err_compare(t, "KEYOP_MISMATCH", | |
1548 | expected->output, expected->output_len, | |
1549 | got, got_len)) | |
1550 | goto err; | |
1551 | ||
5824cc29 | 1552 | err: |
e3d378bc | 1553 | OPENSSL_free(got); |
d7fcf1fe | 1554 | EVP_PKEY_CTX_free(copy); |
5824cc29 DSH |
1555 | return 1; |
1556 | } | |
1557 | ||
6c5943c9 | 1558 | static int sign_test_init(EVP_TEST *t, const char *name) |
5824cc29 DSH |
1559 | { |
1560 | return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign); | |
1561 | } | |
1562 | ||
6c5943c9 | 1563 | static const EVP_TEST_METHOD psign_test_method = { |
5824cc29 DSH |
1564 | "Sign", |
1565 | sign_test_init, | |
1566 | pkey_test_cleanup, | |
1567 | pkey_test_parse, | |
1568 | pkey_test_run | |
1569 | }; | |
1570 | ||
6c5943c9 | 1571 | static int verify_recover_test_init(EVP_TEST *t, const char *name) |
5824cc29 DSH |
1572 | { |
1573 | return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init, | |
1574 | EVP_PKEY_verify_recover); | |
1575 | } | |
1576 | ||
6c5943c9 | 1577 | static const EVP_TEST_METHOD pverify_recover_test_method = { |
5824cc29 DSH |
1578 | "VerifyRecover", |
1579 | verify_recover_test_init, | |
1580 | pkey_test_cleanup, | |
1581 | pkey_test_parse, | |
1582 | pkey_test_run | |
1583 | }; | |
1584 | ||
6c5943c9 | 1585 | static int decrypt_test_init(EVP_TEST *t, const char *name) |
5824cc29 DSH |
1586 | { |
1587 | return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init, | |
1588 | EVP_PKEY_decrypt); | |
1589 | } | |
1590 | ||
6c5943c9 | 1591 | static const EVP_TEST_METHOD pdecrypt_test_method = { |
5824cc29 DSH |
1592 | "Decrypt", |
1593 | decrypt_test_init, | |
1594 | pkey_test_cleanup, | |
1595 | pkey_test_parse, | |
1596 | pkey_test_run | |
1597 | }; | |
1598 | ||
6c5943c9 | 1599 | static int verify_test_init(EVP_TEST *t, const char *name) |
5824cc29 DSH |
1600 | { |
1601 | return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0); | |
1602 | } | |
1603 | ||
6c5943c9 | 1604 | static int verify_test_run(EVP_TEST *t) |
5824cc29 | 1605 | { |
6c5943c9 RS |
1606 | PKEY_DATA *kdata = t->data; |
1607 | ||
5824cc29 DSH |
1608 | if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len, |
1609 | kdata->input, kdata->input_len) <= 0) | |
1610 | t->err = "VERIFY_ERROR"; | |
1611 | return 1; | |
1612 | } | |
1613 | ||
6c5943c9 | 1614 | static const EVP_TEST_METHOD pverify_test_method = { |
5824cc29 DSH |
1615 | "Verify", |
1616 | verify_test_init, | |
1617 | pkey_test_cleanup, | |
1618 | pkey_test_parse, | |
1619 | verify_test_run | |
1620 | }; | |
3b53e18a | 1621 | |
d4ad48d7 | 1622 | |
6c5943c9 | 1623 | static int pderive_test_init(EVP_TEST *t, const char *name) |
d4ad48d7 DSH |
1624 | { |
1625 | return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0); | |
1626 | } | |
1627 | ||
6c5943c9 | 1628 | static int pderive_test_parse(EVP_TEST *t, |
d4ad48d7 DSH |
1629 | const char *keyword, const char *value) |
1630 | { | |
6c5943c9 | 1631 | PKEY_DATA *kdata = t->data; |
d4ad48d7 DSH |
1632 | |
1633 | if (strcmp(keyword, "PeerKey") == 0) { | |
1634 | EVP_PKEY *peer; | |
6c5943c9 | 1635 | if (find_key(&peer, value, public_keys) == 0) |
f42c225d | 1636 | return -1; |
d4ad48d7 | 1637 | if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0) |
f42c225d | 1638 | return -1; |
d4ad48d7 DSH |
1639 | return 1; |
1640 | } | |
1641 | if (strcmp(keyword, "SharedSecret") == 0) | |
c49e0b04 | 1642 | return parse_bin(value, &kdata->output, &kdata->output_len); |
4ddd5ace | 1643 | if (strcmp(keyword, "Ctrl") == 0) |
dfbdf4ab | 1644 | return pkey_test_ctrl(t, kdata->ctx, value); |
d4ad48d7 DSH |
1645 | return 0; |
1646 | } | |
1647 | ||
6c5943c9 | 1648 | static int pderive_test_run(EVP_TEST *t) |
d4ad48d7 | 1649 | { |
e3d378bc AP |
1650 | PKEY_DATA *expected = t->data; |
1651 | unsigned char *got = NULL; | |
1652 | size_t got_len; | |
d4ad48d7 | 1653 | |
9b82c8b1 DSH |
1654 | if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) { |
1655 | t->err = "DERIVE_ERROR"; | |
1656 | goto err; | |
1657 | } | |
e3d378bc | 1658 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { |
6c5943c9 | 1659 | t->err = "DERIVE_ERROR"; |
d4ad48d7 | 1660 | goto err; |
6c5943c9 | 1661 | } |
e3d378bc | 1662 | if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) { |
6c5943c9 | 1663 | t->err = "DERIVE_ERROR"; |
d4ad48d7 | 1664 | goto err; |
6c5943c9 | 1665 | } |
4cceb185 P |
1666 | if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH", |
1667 | expected->output, expected->output_len, | |
1668 | got, got_len)) | |
d4ad48d7 | 1669 | goto err; |
6c5943c9 RS |
1670 | |
1671 | t->err = NULL; | |
d4ad48d7 | 1672 | err: |
e3d378bc | 1673 | OPENSSL_free(got); |
d4ad48d7 DSH |
1674 | return 1; |
1675 | } | |
1676 | ||
6c5943c9 | 1677 | static const EVP_TEST_METHOD pderive_test_method = { |
d4ad48d7 DSH |
1678 | "Derive", |
1679 | pderive_test_init, | |
1680 | pkey_test_cleanup, | |
1681 | pderive_test_parse, | |
1682 | pderive_test_run | |
1683 | }; | |
1684 | ||
3b53e18a | 1685 | |
c49e0b04 RS |
1686 | /** |
1687 | *** PBE TESTS | |
1688 | **/ | |
1689 | ||
1690 | typedef enum pbe_type_enum { | |
1691 | PBE_TYPE_INVALID = 0, | |
1692 | PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12 | |
1693 | } PBE_TYPE; | |
3b53e18a | 1694 | |
6c5943c9 | 1695 | typedef struct pbe_data_st { |
c49e0b04 | 1696 | PBE_TYPE pbe_type; |
6c5943c9 | 1697 | /* scrypt parameters */ |
3b53e18a | 1698 | uint64_t N, r, p, maxmem; |
6c5943c9 | 1699 | /* PKCS#12 parameters */ |
351fe214 DSH |
1700 | int id, iter; |
1701 | const EVP_MD *md; | |
6c5943c9 | 1702 | /* password */ |
3b53e18a DSH |
1703 | unsigned char *pass; |
1704 | size_t pass_len; | |
6c5943c9 | 1705 | /* salt */ |
3b53e18a DSH |
1706 | unsigned char *salt; |
1707 | size_t salt_len; | |
6c5943c9 | 1708 | /* Expected output */ |
3b53e18a DSH |
1709 | unsigned char *key; |
1710 | size_t key_len; | |
6c5943c9 | 1711 | } PBE_DATA; |
3b53e18a | 1712 | |
b0809bc8 | 1713 | #ifndef OPENSSL_NO_SCRYPT |
c49e0b04 RS |
1714 | /* |
1715 | * Parse unsigned decimal 64 bit integer value | |
1716 | */ | |
1717 | static int parse_uint64(const char *value, uint64_t *pr) | |
1718 | { | |
1719 | const char *p = value; | |
1720 | ||
1721 | if (!TEST_true(*p)) { | |
1722 | TEST_info("Invalid empty integer value"); | |
1723 | return -1; | |
1724 | } | |
1725 | for (*pr = 0; *p; ) { | |
1726 | if (*pr > UINT64_MAX / 10) { | |
1727 | TEST_error("Integer overflow in string %s", value); | |
1728 | return -1; | |
1729 | } | |
1730 | *pr *= 10; | |
00dfbaad | 1731 | if (!TEST_true(isdigit((unsigned char)*p))) { |
c49e0b04 RS |
1732 | TEST_error("Invalid character in string %s", value); |
1733 | return -1; | |
1734 | } | |
1735 | *pr += *p - '0'; | |
1736 | p++; | |
1737 | } | |
1738 | return 1; | |
1739 | } | |
1740 | ||
6c5943c9 | 1741 | static int scrypt_test_parse(EVP_TEST *t, |
3b53e18a DSH |
1742 | const char *keyword, const char *value) |
1743 | { | |
6c5943c9 | 1744 | PBE_DATA *pdata = t->data; |
351fe214 | 1745 | |
3b53e18a | 1746 | if (strcmp(keyword, "N") == 0) |
c49e0b04 | 1747 | return parse_uint64(value, &pdata->N); |
3b53e18a | 1748 | if (strcmp(keyword, "p") == 0) |
c49e0b04 | 1749 | return parse_uint64(value, &pdata->p); |
3b53e18a | 1750 | if (strcmp(keyword, "r") == 0) |
c49e0b04 | 1751 | return parse_uint64(value, &pdata->r); |
3b53e18a | 1752 | if (strcmp(keyword, "maxmem") == 0) |
c49e0b04 | 1753 | return parse_uint64(value, &pdata->maxmem); |
3b53e18a DSH |
1754 | return 0; |
1755 | } | |
b0809bc8 | 1756 | #endif |
3b53e18a | 1757 | |
6c5943c9 | 1758 | static int pbkdf2_test_parse(EVP_TEST *t, |
351fe214 | 1759 | const char *keyword, const char *value) |
3b53e18a | 1760 | { |
6c5943c9 | 1761 | PBE_DATA *pdata = t->data; |
351fe214 DSH |
1762 | |
1763 | if (strcmp(keyword, "iter") == 0) { | |
1764 | pdata->iter = atoi(value); | |
1765 | if (pdata->iter <= 0) | |
c49e0b04 | 1766 | return -1; |
351fe214 DSH |
1767 | return 1; |
1768 | } | |
1769 | if (strcmp(keyword, "MD") == 0) { | |
1770 | pdata->md = EVP_get_digestbyname(value); | |
1771 | if (pdata->md == NULL) | |
c49e0b04 | 1772 | return -1; |
351fe214 DSH |
1773 | return 1; |
1774 | } | |
1775 | return 0; | |
1776 | } | |
1777 | ||
6c5943c9 | 1778 | static int pkcs12_test_parse(EVP_TEST *t, |
351fe214 DSH |
1779 | const char *keyword, const char *value) |
1780 | { | |
6c5943c9 | 1781 | PBE_DATA *pdata = t->data; |
351fe214 DSH |
1782 | |
1783 | if (strcmp(keyword, "id") == 0) { | |
1784 | pdata->id = atoi(value); | |
1785 | if (pdata->id <= 0) | |
c49e0b04 | 1786 | return -1; |
351fe214 DSH |
1787 | return 1; |
1788 | } | |
1789 | return pbkdf2_test_parse(t, keyword, value); | |
3b53e18a DSH |
1790 | } |
1791 | ||
6c5943c9 | 1792 | static int pbe_test_init(EVP_TEST *t, const char *alg) |
3b53e18a | 1793 | { |
6c5943c9 | 1794 | PBE_DATA *pdat; |
c49e0b04 | 1795 | PBE_TYPE pbe_type = PBE_TYPE_INVALID; |
351fe214 | 1796 | |
83bd31da | 1797 | if (strcmp(alg, "scrypt") == 0) { |
b0809bc8 | 1798 | #ifndef OPENSSL_NO_SCRYPT |
3b53e18a | 1799 | pbe_type = PBE_TYPE_SCRYPT; |
83bd31da MC |
1800 | #else |
1801 | t->skip = 1; | |
1802 | return 1; | |
b0809bc8 | 1803 | #endif |
83bd31da | 1804 | } else if (strcmp(alg, "pbkdf2") == 0) { |
351fe214 | 1805 | pbe_type = PBE_TYPE_PBKDF2; |
83bd31da | 1806 | } else if (strcmp(alg, "pkcs12") == 0) { |
351fe214 | 1807 | pbe_type = PBE_TYPE_PKCS12; |
83bd31da | 1808 | } else { |
6c5943c9 | 1809 | TEST_error("Unknown pbe algorithm %s", alg); |
83bd31da | 1810 | } |
c49e0b04 | 1811 | pdat = OPENSSL_zalloc(sizeof(*pdat)); |
3b53e18a | 1812 | pdat->pbe_type = pbe_type; |
3b53e18a DSH |
1813 | t->data = pdat; |
1814 | return 1; | |
1815 | } | |
1816 | ||
6c5943c9 | 1817 | static void pbe_test_cleanup(EVP_TEST *t) |
3b53e18a | 1818 | { |
6c5943c9 RS |
1819 | PBE_DATA *pdat = t->data; |
1820 | ||
1821 | OPENSSL_free(pdat->pass); | |
1822 | OPENSSL_free(pdat->salt); | |
1823 | OPENSSL_free(pdat->key); | |
3b53e18a DSH |
1824 | } |
1825 | ||
6c5943c9 RS |
1826 | static int pbe_test_parse(EVP_TEST *t, |
1827 | const char *keyword, const char *value) | |
3b53e18a | 1828 | { |
6c5943c9 | 1829 | PBE_DATA *pdata = t->data; |
351fe214 | 1830 | |
3b53e18a | 1831 | if (strcmp(keyword, "Password") == 0) |
c49e0b04 | 1832 | return parse_bin(value, &pdata->pass, &pdata->pass_len); |
3b53e18a | 1833 | if (strcmp(keyword, "Salt") == 0) |
c49e0b04 | 1834 | return parse_bin(value, &pdata->salt, &pdata->salt_len); |
3b53e18a | 1835 | if (strcmp(keyword, "Key") == 0) |
c49e0b04 | 1836 | return parse_bin(value, &pdata->key, &pdata->key_len); |
b0809bc8 | 1837 | if (pdata->pbe_type == PBE_TYPE_PBKDF2) |
351fe214 DSH |
1838 | return pbkdf2_test_parse(t, keyword, value); |
1839 | else if (pdata->pbe_type == PBE_TYPE_PKCS12) | |
1840 | return pkcs12_test_parse(t, keyword, value); | |
b0809bc8 RS |
1841 | #ifndef OPENSSL_NO_SCRYPT |
1842 | else if (pdata->pbe_type == PBE_TYPE_SCRYPT) | |
1843 | return scrypt_test_parse(t, keyword, value); | |
1844 | #endif | |
3b53e18a DSH |
1845 | return 0; |
1846 | } | |
1847 | ||
6c5943c9 | 1848 | static int pbe_test_run(EVP_TEST *t) |
3b53e18a | 1849 | { |
e3d378bc | 1850 | PBE_DATA *expected = t->data; |
351fe214 DSH |
1851 | unsigned char *key; |
1852 | ||
e3d378bc | 1853 | if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) { |
6c5943c9 | 1854 | t->err = "INTERNAL_ERROR"; |
351fe214 | 1855 | goto err; |
6c5943c9 | 1856 | } |
e3d378bc AP |
1857 | if (expected->pbe_type == PBE_TYPE_PBKDF2) { |
1858 | if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len, | |
1859 | expected->salt, expected->salt_len, | |
1860 | expected->iter, expected->md, | |
1861 | expected->key_len, key) == 0) { | |
6c5943c9 | 1862 | t->err = "PBKDF2_ERROR"; |
351fe214 | 1863 | goto err; |
6c5943c9 | 1864 | } |
b0809bc8 | 1865 | #ifndef OPENSSL_NO_SCRYPT |
e3d378bc AP |
1866 | } else if (expected->pbe_type == PBE_TYPE_SCRYPT) { |
1867 | if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len, | |
1868 | expected->salt, expected->salt_len, expected->N, | |
1869 | expected->r, expected->p, expected->maxmem, | |
1870 | key, expected->key_len) == 0) { | |
6c5943c9 | 1871 | t->err = "SCRYPT_ERROR"; |
351fe214 | 1872 | goto err; |
6c5943c9 | 1873 | } |
b0809bc8 | 1874 | #endif |
e3d378bc AP |
1875 | } else if (expected->pbe_type == PBE_TYPE_PKCS12) { |
1876 | if (PKCS12_key_gen_uni(expected->pass, expected->pass_len, | |
1877 | expected->salt, expected->salt_len, | |
1878 | expected->id, expected->iter, expected->key_len, | |
1879 | key, expected->md) == 0) { | |
6c5943c9 | 1880 | t->err = "PKCS12_ERROR"; |
351fe214 | 1881 | goto err; |
6c5943c9 | 1882 | } |
351fe214 | 1883 | } |
4cceb185 P |
1884 | if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len, |
1885 | key, expected->key_len)) | |
351fe214 | 1886 | goto err; |
4cceb185 | 1887 | |
6c5943c9 RS |
1888 | t->err = NULL; |
1889 | err: | |
351fe214 | 1890 | OPENSSL_free(key); |
351fe214 | 1891 | return 1; |
3b53e18a DSH |
1892 | } |
1893 | ||
6c5943c9 | 1894 | static const EVP_TEST_METHOD pbe_test_method = { |
3b53e18a DSH |
1895 | "PBE", |
1896 | pbe_test_init, | |
1897 | pbe_test_cleanup, | |
1898 | pbe_test_parse, | |
1899 | pbe_test_run | |
1900 | }; | |
3cdd1e94 | 1901 | |
c49e0b04 RS |
1902 | |
1903 | /** | |
1904 | *** BASE64 TESTS | |
1905 | **/ | |
3cdd1e94 EK |
1906 | |
1907 | typedef enum { | |
1908 | BASE64_CANONICAL_ENCODING = 0, | |
1909 | BASE64_VALID_ENCODING = 1, | |
1910 | BASE64_INVALID_ENCODING = 2 | |
1911 | } base64_encoding_type; | |
1912 | ||
6c5943c9 | 1913 | typedef struct encode_data_st { |
3cdd1e94 EK |
1914 | /* Input to encoding */ |
1915 | unsigned char *input; | |
1916 | size_t input_len; | |
1917 | /* Expected output */ | |
1918 | unsigned char *output; | |
1919 | size_t output_len; | |
1920 | base64_encoding_type encoding; | |
6c5943c9 | 1921 | } ENCODE_DATA; |
3cdd1e94 | 1922 | |
6c5943c9 | 1923 | static int encode_test_init(EVP_TEST *t, const char *encoding) |
3cdd1e94 | 1924 | { |
c49e0b04 | 1925 | ENCODE_DATA *edata; |
3cdd1e94 | 1926 | |
c49e0b04 RS |
1927 | if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata)))) |
1928 | return 0; | |
3cdd1e94 EK |
1929 | if (strcmp(encoding, "canonical") == 0) { |
1930 | edata->encoding = BASE64_CANONICAL_ENCODING; | |
1931 | } else if (strcmp(encoding, "valid") == 0) { | |
1932 | edata->encoding = BASE64_VALID_ENCODING; | |
1933 | } else if (strcmp(encoding, "invalid") == 0) { | |
1934 | edata->encoding = BASE64_INVALID_ENCODING; | |
c49e0b04 | 1935 | if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR"))) |
760e2d60 | 1936 | goto err; |
3cdd1e94 | 1937 | } else { |
c49e0b04 RS |
1938 | TEST_error("Bad encoding: %s." |
1939 | " Should be one of {canonical, valid, invalid}", | |
1940 | encoding); | |
760e2d60 | 1941 | goto err; |
3cdd1e94 EK |
1942 | } |
1943 | t->data = edata; | |
1944 | return 1; | |
760e2d60 F |
1945 | err: |
1946 | OPENSSL_free(edata); | |
1947 | return 0; | |
3cdd1e94 EK |
1948 | } |
1949 | ||
6c5943c9 | 1950 | static void encode_test_cleanup(EVP_TEST *t) |
3cdd1e94 | 1951 | { |
6c5943c9 RS |
1952 | ENCODE_DATA *edata = t->data; |
1953 | ||
1954 | OPENSSL_free(edata->input); | |
1955 | OPENSSL_free(edata->output); | |
3cdd1e94 EK |
1956 | memset(edata, 0, sizeof(*edata)); |
1957 | } | |
1958 | ||
6c5943c9 | 1959 | static int encode_test_parse(EVP_TEST *t, |
3cdd1e94 EK |
1960 | const char *keyword, const char *value) |
1961 | { | |
6c5943c9 | 1962 | ENCODE_DATA *edata = t->data; |
c49e0b04 | 1963 | |
3cdd1e94 | 1964 | if (strcmp(keyword, "Input") == 0) |
c49e0b04 | 1965 | return parse_bin(value, &edata->input, &edata->input_len); |
3cdd1e94 | 1966 | if (strcmp(keyword, "Output") == 0) |
c49e0b04 | 1967 | return parse_bin(value, &edata->output, &edata->output_len); |
3cdd1e94 EK |
1968 | return 0; |
1969 | } | |
1970 | ||
6c5943c9 | 1971 | static int encode_test_run(EVP_TEST *t) |
3cdd1e94 | 1972 | { |
e3d378bc | 1973 | ENCODE_DATA *expected = t->data; |
3cdd1e94 EK |
1974 | unsigned char *encode_out = NULL, *decode_out = NULL; |
1975 | int output_len, chunk_len; | |
760e2d60 | 1976 | EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL; |
254b26af | 1977 | |
6c5943c9 RS |
1978 | if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) { |
1979 | t->err = "INTERNAL_ERROR"; | |
254b26af | 1980 | goto err; |
6c5943c9 | 1981 | } |
3cdd1e94 | 1982 | |
e3d378bc | 1983 | if (expected->encoding == BASE64_CANONICAL_ENCODING) { |
6c5943c9 RS |
1984 | |
1985 | if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new()) | |
1986 | || !TEST_ptr(encode_out = | |
e3d378bc | 1987 | OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len)))) |
3cdd1e94 EK |
1988 | goto err; |
1989 | ||
254b26af | 1990 | EVP_EncodeInit(encode_ctx); |
760e2d60 F |
1991 | if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len, |
1992 | expected->input, expected->input_len))) | |
1993 | goto err; | |
1994 | ||
3cdd1e94 EK |
1995 | output_len = chunk_len; |
1996 | ||
254b26af | 1997 | EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len); |
3cdd1e94 EK |
1998 | output_len += chunk_len; |
1999 | ||
4cceb185 P |
2000 | if (!memory_err_compare(t, "BAD_ENCODING", |
2001 | expected->output, expected->output_len, | |
2002 | encode_out, output_len)) | |
3cdd1e94 | 2003 | goto err; |
3cdd1e94 EK |
2004 | } |
2005 | ||
6c5943c9 | 2006 | if (!TEST_ptr(decode_out = |
e3d378bc | 2007 | OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len)))) |
3cdd1e94 EK |
2008 | goto err; |
2009 | ||
254b26af | 2010 | EVP_DecodeInit(decode_ctx); |
e3d378bc AP |
2011 | if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output, |
2012 | expected->output_len) < 0) { | |
6c5943c9 | 2013 | t->err = "DECODE_ERROR"; |
3cdd1e94 EK |
2014 | goto err; |
2015 | } | |
2016 | output_len = chunk_len; | |
2017 | ||
254b26af | 2018 | if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) { |
6c5943c9 | 2019 | t->err = "DECODE_ERROR"; |
3cdd1e94 EK |
2020 | goto err; |
2021 | } | |
2022 | output_len += chunk_len; | |
2023 | ||
e3d378bc | 2024 | if (expected->encoding != BASE64_INVALID_ENCODING |
4cceb185 P |
2025 | && !memory_err_compare(t, "BAD_DECODING", |
2026 | expected->input, expected->input_len, | |
2027 | decode_out, output_len)) { | |
6c5943c9 | 2028 | t->err = "BAD_DECODING"; |
3cdd1e94 EK |
2029 | goto err; |
2030 | } | |
2031 | ||
6c5943c9 | 2032 | t->err = NULL; |
3cdd1e94 | 2033 | err: |
3cdd1e94 EK |
2034 | OPENSSL_free(encode_out); |
2035 | OPENSSL_free(decode_out); | |
254b26af | 2036 | EVP_ENCODE_CTX_free(decode_ctx); |
760e2d60 | 2037 | EVP_ENCODE_CTX_free(encode_ctx); |
3cdd1e94 EK |
2038 | return 1; |
2039 | } | |
2040 | ||
6c5943c9 | 2041 | static const EVP_TEST_METHOD encode_test_method = { |
3cdd1e94 EK |
2042 | "Encoding", |
2043 | encode_test_init, | |
2044 | encode_test_cleanup, | |
2045 | encode_test_parse, | |
2046 | encode_test_run, | |
2047 | }; | |
44a284d2 | 2048 | |
5a285add | 2049 | |
c49e0b04 RS |
2050 | /** |
2051 | *** KDF TESTS | |
2052 | **/ | |
44a284d2 | 2053 | |
6c5943c9 | 2054 | typedef struct kdf_data_st { |
44a284d2 | 2055 | /* Context for this operation */ |
5a285add | 2056 | EVP_KDF_CTX *ctx; |
44a284d2 DSH |
2057 | /* Expected output */ |
2058 | unsigned char *output; | |
2059 | size_t output_len; | |
bf5739a0 P |
2060 | OSSL_PARAM params[20]; |
2061 | OSSL_PARAM *p; | |
6c5943c9 | 2062 | } KDF_DATA; |
44a284d2 DSH |
2063 | |
2064 | /* | |
2065 | * Perform public key operation setup: lookup key, allocated ctx and call | |
2066 | * the appropriate initialisation function | |
2067 | */ | |
6c5943c9 | 2068 | static int kdf_test_init(EVP_TEST *t, const char *name) |
44a284d2 | 2069 | { |
6c5943c9 | 2070 | KDF_DATA *kdata; |
bf5739a0 | 2071 | EVP_KDF *kdf; |
b15d5ab6 | 2072 | |
ab78f89b | 2073 | #ifdef OPENSSL_NO_SCRYPT |
98b687f8 PY |
2074 | /* TODO(3.0) Replace with "scrypt" once aliases are supported */ |
2075 | if (strcmp(name, "id-scrypt") == 0) { | |
ab78f89b MC |
2076 | t->skip = 1; |
2077 | return 1; | |
2078 | } | |
1aec7716 SL |
2079 | #endif /* OPENSSL_NO_SCRYPT */ |
2080 | ||
2081 | #ifdef OPENSSL_NO_CMS | |
2082 | if (strcmp(name, "X942KDF") == 0) { | |
2083 | t->skip = 1; | |
2084 | return 1; | |
2085 | } | |
2086 | #endif /* OPENSSL_NO_CMS */ | |
ab78f89b | 2087 | |
bf5739a0 | 2088 | if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) |
d2ba8123 | 2089 | return 0; |
bf5739a0 P |
2090 | kdata->p = kdata->params; |
2091 | *kdata->p = OSSL_PARAM_construct_end(); | |
44a284d2 | 2092 | |
bf5739a0 | 2093 | kdf = EVP_KDF_fetch(NULL, name, NULL); |
92475712 P |
2094 | if (kdf == NULL) { |
2095 | OPENSSL_free(kdata); | |
44a284d2 | 2096 | return 0; |
92475712 | 2097 | } |
765d04c9 | 2098 | kdata->ctx = EVP_KDF_new_ctx(kdf); |
bf5739a0 | 2099 | EVP_KDF_free(kdf); |
9e206ce5 P |
2100 | if (kdata->ctx == NULL) { |
2101 | OPENSSL_free(kdata); | |
44a284d2 | 2102 | return 0; |
9e206ce5 | 2103 | } |
c49e0b04 | 2104 | t->data = kdata; |
44a284d2 DSH |
2105 | return 1; |
2106 | } | |
2107 | ||
6c5943c9 | 2108 | static void kdf_test_cleanup(EVP_TEST *t) |
44a284d2 | 2109 | { |
6c5943c9 | 2110 | KDF_DATA *kdata = t->data; |
bf5739a0 P |
2111 | OSSL_PARAM *p; |
2112 | ||
2113 | for (p = kdata->params; p->key != NULL; p++) | |
2114 | OPENSSL_free(p->data); | |
44a284d2 | 2115 | OPENSSL_free(kdata->output); |
765d04c9 | 2116 | EVP_KDF_free_ctx(kdata->ctx); |
5a285add DM |
2117 | } |
2118 | ||
2119 | static int kdf_test_ctrl(EVP_TEST *t, EVP_KDF_CTX *kctx, | |
2120 | const char *value) | |
2121 | { | |
bf5739a0 | 2122 | KDF_DATA *kdata = t->data; |
5a285add | 2123 | int rv; |
bf5739a0 | 2124 | char *p, *name; |
765d04c9 P |
2125 | const OSSL_PARAM *defs = |
2126 | EVP_KDF_settable_ctx_params(EVP_KDF_get_ctx_kdf(kctx)); | |
5a285add | 2127 | |
bf5739a0 | 2128 | if (!TEST_ptr(name = OPENSSL_strdup(value))) |
5a285add | 2129 | return 0; |
bf5739a0 | 2130 | p = strchr(name, ':'); |
5a285add DM |
2131 | if (p != NULL) |
2132 | *p++ = '\0'; | |
bf5739a0 | 2133 | |
64da55a6 | 2134 | rv = OSSL_PARAM_allocate_from_text(kdata->p, defs, name, p, |
2ee0dfa6 | 2135 | p != NULL ? strlen(p) : 0, NULL); |
bf5739a0 P |
2136 | *++kdata->p = OSSL_PARAM_construct_end(); |
2137 | if (!rv) { | |
2138 | t->err = "KDF_PARAM_ERROR"; | |
2139 | OPENSSL_free(name); | |
2140 | return 0; | |
2141 | } | |
64da55a6 | 2142 | if (p != NULL && strcmp(name, "digest") == 0) { |
5a285add DM |
2143 | /* If p has an OID and lookup fails assume disabled algorithm */ |
2144 | int nid = OBJ_sn2nid(p); | |
2145 | ||
2146 | if (nid == NID_undef) | |
2147 | nid = OBJ_ln2nid(p); | |
bf5739a0 | 2148 | if (nid != NID_undef && EVP_get_digestbynid(nid) == NULL) |
5a285add | 2149 | t->skip = 1; |
5a285add | 2150 | } |
33f54da3 SS |
2151 | if (p != NULL && strcmp(name, "cipher") == 0) { |
2152 | /* If p has an OID and lookup fails assume disabled algorithm */ | |
2153 | int nid = OBJ_sn2nid(p); | |
2154 | ||
2155 | if (nid == NID_undef) | |
2156 | nid = OBJ_ln2nid(p); | |
2157 | if (nid != NID_undef && EVP_get_cipherbynid(nid) == NULL) | |
2158 | t->skip = 1; | |
2159 | } | |
bf5739a0 P |
2160 | OPENSSL_free(name); |
2161 | return 1; | |
44a284d2 DSH |
2162 | } |
2163 | ||
6c5943c9 | 2164 | static int kdf_test_parse(EVP_TEST *t, |
44a284d2 DSH |
2165 | const char *keyword, const char *value) |
2166 | { | |
6c5943c9 RS |
2167 | KDF_DATA *kdata = t->data; |
2168 | ||
44a284d2 | 2169 | if (strcmp(keyword, "Output") == 0) |
c49e0b04 | 2170 | return parse_bin(value, &kdata->output, &kdata->output_len); |
7d04be79 | 2171 | if (strncmp(keyword, "Ctrl", 4) == 0) |
5a285add | 2172 | return kdf_test_ctrl(t, kdata->ctx, value); |
44a284d2 DSH |
2173 | return 0; |
2174 | } | |
2175 | ||
6c5943c9 | 2176 | static int kdf_test_run(EVP_TEST *t) |
44a284d2 | 2177 | { |
e3d378bc AP |
2178 | KDF_DATA *expected = t->data; |
2179 | unsigned char *got = NULL; | |
2180 | size_t got_len = expected->output_len; | |
6c5943c9 | 2181 | |
765d04c9 | 2182 | if (!EVP_KDF_set_ctx_params(expected->ctx, expected->params)) { |
bf5739a0 P |
2183 | t->err = "KDF_CTRL_ERROR"; |
2184 | return 1; | |
2185 | } | |
e3d378bc | 2186 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { |
6c5943c9 | 2187 | t->err = "INTERNAL_ERROR"; |
44a284d2 | 2188 | goto err; |
6c5943c9 | 2189 | } |
5a285add | 2190 | if (EVP_KDF_derive(expected->ctx, got, got_len) <= 0) { |
6c5943c9 | 2191 | t->err = "KDF_DERIVE_ERROR"; |
44a284d2 | 2192 | goto err; |
6c5943c9 | 2193 | } |
4cceb185 P |
2194 | if (!memory_err_compare(t, "KDF_MISMATCH", |
2195 | expected->output, expected->output_len, | |
2196 | got, got_len)) | |
44a284d2 | 2197 | goto err; |
4cceb185 | 2198 | |
6c5943c9 RS |
2199 | t->err = NULL; |
2200 | ||
44a284d2 | 2201 | err: |
e3d378bc | 2202 | OPENSSL_free(got); |
44a284d2 DSH |
2203 | return 1; |
2204 | } | |
2205 | ||
6c5943c9 | 2206 | static const EVP_TEST_METHOD kdf_test_method = { |
44a284d2 DSH |
2207 | "KDF", |
2208 | kdf_test_init, | |
2209 | kdf_test_cleanup, | |
2210 | kdf_test_parse, | |
2211 | kdf_test_run | |
2212 | }; | |
d91b7423 | 2213 | |
c49e0b04 | 2214 | |
5a285add DM |
2215 | /** |
2216 | *** PKEY KDF TESTS | |
2217 | **/ | |
2218 | ||
2219 | typedef struct pkey_kdf_data_st { | |
2220 | /* Context for this operation */ | |
2221 | EVP_PKEY_CTX *ctx; | |
2222 | /* Expected output */ | |
2223 | unsigned char *output; | |
2224 | size_t output_len; | |
2225 | } PKEY_KDF_DATA; | |
2226 | ||
2227 | /* | |
2228 | * Perform public key operation setup: lookup key, allocated ctx and call | |
2229 | * the appropriate initialisation function | |
2230 | */ | |
2231 | static int pkey_kdf_test_init(EVP_TEST *t, const char *name) | |
2232 | { | |
2233 | PKEY_KDF_DATA *kdata; | |
2234 | int kdf_nid = OBJ_sn2nid(name); | |
2235 | ||
2236 | #ifdef OPENSSL_NO_SCRYPT | |
2237 | if (strcmp(name, "scrypt") == 0) { | |
2238 | t->skip = 1; | |
2239 | return 1; | |
2240 | } | |
1aec7716 SL |
2241 | #endif /* OPENSSL_NO_SCRYPT */ |
2242 | ||
2243 | #ifdef OPENSSL_NO_CMS | |
2244 | if (strcmp(name, "X942KDF") == 0) { | |
2245 | t->skip = 1; | |
2246 | return 1; | |
2247 | } | |
2248 | #endif /* OPENSSL_NO_CMS */ | |
5a285add DM |
2249 | |
2250 | if (kdf_nid == NID_undef) | |
2251 | kdf_nid = OBJ_ln2nid(name); | |
2252 | ||
2253 | if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) | |
2254 | return 0; | |
2255 | kdata->ctx = EVP_PKEY_CTX_new_id(kdf_nid, NULL); | |
2256 | if (kdata->ctx == NULL) { | |
2257 | OPENSSL_free(kdata); | |
2258 | return 0; | |
2259 | } | |
2260 | if (EVP_PKEY_derive_init(kdata->ctx) <= 0) { | |
2261 | EVP_PKEY_CTX_free(kdata->ctx); | |
2262 | OPENSSL_free(kdata); | |
2263 | return 0; | |
2264 | } | |
2265 | t->data = kdata; | |
2266 | return 1; | |
2267 | } | |
2268 | ||
2269 | static void pkey_kdf_test_cleanup(EVP_TEST *t) | |
2270 | { | |
2271 | PKEY_KDF_DATA *kdata = t->data; | |
bf5739a0 | 2272 | |
5a285add DM |
2273 | OPENSSL_free(kdata->output); |
2274 | EVP_PKEY_CTX_free(kdata->ctx); | |
2275 | } | |
2276 | ||
2277 | static int pkey_kdf_test_parse(EVP_TEST *t, | |
2278 | const char *keyword, const char *value) | |
2279 | { | |
2280 | PKEY_KDF_DATA *kdata = t->data; | |
2281 | ||
2282 | if (strcmp(keyword, "Output") == 0) | |
2283 | return parse_bin(value, &kdata->output, &kdata->output_len); | |
2284 | if (strncmp(keyword, "Ctrl", 4) == 0) | |
2285 | return pkey_test_ctrl(t, kdata->ctx, value); | |
2286 | return 0; | |
2287 | } | |
2288 | ||
2289 | static int pkey_kdf_test_run(EVP_TEST *t) | |
2290 | { | |
2291 | PKEY_KDF_DATA *expected = t->data; | |
2292 | unsigned char *got = NULL; | |
2293 | size_t got_len = expected->output_len; | |
2294 | ||
2295 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { | |
2296 | t->err = "INTERNAL_ERROR"; | |
2297 | goto err; | |
2298 | } | |
2299 | if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) { | |
2300 | t->err = "KDF_DERIVE_ERROR"; | |
2301 | goto err; | |
2302 | } | |
2303 | if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) { | |
2304 | t->err = "KDF_MISMATCH"; | |
2305 | goto err; | |
2306 | } | |
2307 | t->err = NULL; | |
2308 | ||
2309 | err: | |
2310 | OPENSSL_free(got); | |
2311 | return 1; | |
2312 | } | |
2313 | ||
2314 | static const EVP_TEST_METHOD pkey_kdf_test_method = { | |
2315 | "PKEYKDF", | |
2316 | pkey_kdf_test_init, | |
2317 | pkey_kdf_test_cleanup, | |
2318 | pkey_kdf_test_parse, | |
2319 | pkey_kdf_test_run | |
2320 | }; | |
2321 | ||
2322 | ||
c49e0b04 RS |
2323 | /** |
2324 | *** KEYPAIR TESTS | |
2325 | **/ | |
2326 | ||
2327 | typedef struct keypair_test_data_st { | |
d91b7423 RS |
2328 | EVP_PKEY *privk; |
2329 | EVP_PKEY *pubk; | |
6c5943c9 | 2330 | } KEYPAIR_TEST_DATA; |
d91b7423 | 2331 | |
6c5943c9 | 2332 | static int keypair_test_init(EVP_TEST *t, const char *pair) |
d91b7423 | 2333 | { |
c49e0b04 | 2334 | KEYPAIR_TEST_DATA *data; |
d91b7423 RS |
2335 | int rv = 0; |
2336 | EVP_PKEY *pk = NULL, *pubk = NULL; | |
2337 | char *pub, *priv = NULL; | |
d91b7423 | 2338 | |
c49e0b04 | 2339 | /* Split private and public names. */ |
6c5943c9 RS |
2340 | if (!TEST_ptr(priv = OPENSSL_strdup(pair)) |
2341 | || !TEST_ptr(pub = strchr(priv, ':'))) { | |
2342 | t->err = "PARSING_ERROR"; | |
d91b7423 RS |
2343 | goto end; |
2344 | } | |
c49e0b04 | 2345 | *pub++ = '\0'; |
d91b7423 | 2346 | |
6c5943c9 | 2347 | if (!TEST_true(find_key(&pk, priv, private_keys))) { |
c49e0b04 | 2348 | TEST_info("Can't find private key: %s", priv); |
6c5943c9 | 2349 | t->err = "MISSING_PRIVATE_KEY"; |
d91b7423 RS |
2350 | goto end; |
2351 | } | |
6c5943c9 | 2352 | if (!TEST_true(find_key(&pubk, pub, public_keys))) { |
c49e0b04 | 2353 | TEST_info("Can't find public key: %s", pub); |
6c5943c9 | 2354 | t->err = "MISSING_PUBLIC_KEY"; |
d91b7423 RS |
2355 | goto end; |
2356 | } | |
2357 | ||
2358 | if (pk == NULL && pubk == NULL) { | |
2359 | /* Both keys are listed but unsupported: skip this test */ | |
2360 | t->skip = 1; | |
2361 | rv = 1; | |
2362 | goto end; | |
2363 | } | |
2364 | ||
6c5943c9 | 2365 | if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data)))) |
d91b7423 | 2366 | goto end; |
d91b7423 RS |
2367 | data->privk = pk; |
2368 | data->pubk = pubk; | |
2369 | t->data = data; | |
d91b7423 | 2370 | rv = 1; |
6c5943c9 | 2371 | t->err = NULL; |
d91b7423 RS |
2372 | |
2373 | end: | |
6c5943c9 | 2374 | OPENSSL_free(priv); |
d91b7423 RS |
2375 | return rv; |
2376 | } | |
2377 | ||
6c5943c9 | 2378 | static void keypair_test_cleanup(EVP_TEST *t) |
d91b7423 | 2379 | { |
6c5943c9 | 2380 | OPENSSL_free(t->data); |
d91b7423 | 2381 | t->data = NULL; |
d91b7423 RS |
2382 | } |
2383 | ||
c49e0b04 RS |
2384 | /* |
2385 | * For tests that do not accept any custom keywords. | |
d91b7423 | 2386 | */ |
6c5943c9 | 2387 | static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value) |
d91b7423 RS |
2388 | { |
2389 | return 0; | |
2390 | } | |
2391 | ||
6c5943c9 | 2392 | static int keypair_test_run(EVP_TEST *t) |
d91b7423 RS |
2393 | { |
2394 | int rv = 0; | |
6c5943c9 | 2395 | const KEYPAIR_TEST_DATA *pair = t->data; |
d91b7423 RS |
2396 | |
2397 | if (pair->privk == NULL || pair->pubk == NULL) { | |
6c5943c9 RS |
2398 | /* |
2399 | * this can only happen if only one of the keys is not set | |
d91b7423 RS |
2400 | * which means that one of them was unsupported while the |
2401 | * other isn't: hence a key type mismatch. | |
2402 | */ | |
6c5943c9 | 2403 | t->err = "KEYPAIR_TYPE_MISMATCH"; |
d91b7423 RS |
2404 | rv = 1; |
2405 | goto end; | |
2406 | } | |
2407 | ||
c74aaa39 | 2408 | if ((rv = EVP_PKEY_eq(pair->privk, pair->pubk)) != 1 ) { |
d91b7423 | 2409 | if ( 0 == rv ) { |
6c5943c9 | 2410 | t->err = "KEYPAIR_MISMATCH"; |
d91b7423 | 2411 | } else if ( -1 == rv ) { |
6c5943c9 | 2412 | t->err = "KEYPAIR_TYPE_MISMATCH"; |
d91b7423 | 2413 | } else if ( -2 == rv ) { |
6c5943c9 | 2414 | t->err = "UNSUPPORTED_KEY_COMPARISON"; |
d91b7423 | 2415 | } else { |
6c5943c9 | 2416 | TEST_error("Unexpected error in key comparison"); |
d91b7423 RS |
2417 | rv = 0; |
2418 | goto end; | |
2419 | } | |
2420 | rv = 1; | |
2421 | goto end; | |
2422 | } | |
2423 | ||
2424 | rv = 1; | |
6c5943c9 | 2425 | t->err = NULL; |
d91b7423 RS |
2426 | |
2427 | end: | |
d91b7423 RS |
2428 | return rv; |
2429 | } | |
2430 | ||
6c5943c9 | 2431 | static const EVP_TEST_METHOD keypair_test_method = { |
d91b7423 RS |
2432 | "PrivPubKeyPair", |
2433 | keypair_test_init, | |
2434 | keypair_test_cleanup, | |
2435 | void_test_parse, | |
2436 | keypair_test_run | |
2437 | }; | |
2438 | ||
1f0fc03b DSH |
2439 | /** |
2440 | *** KEYGEN TEST | |
2441 | **/ | |
2442 | ||
2443 | typedef struct keygen_test_data_st { | |
2444 | EVP_PKEY_CTX *genctx; /* Keygen context to use */ | |
2445 | char *keyname; /* Key name to store key or NULL */ | |
2446 | } KEYGEN_TEST_DATA; | |
2447 | ||
2448 | static int keygen_test_init(EVP_TEST *t, const char *alg) | |
2449 | { | |
2450 | KEYGEN_TEST_DATA *data; | |
2451 | EVP_PKEY_CTX *genctx; | |
2452 | int nid = OBJ_sn2nid(alg); | |
2453 | ||
2454 | if (nid == NID_undef) { | |
2455 | nid = OBJ_ln2nid(alg); | |
2456 | if (nid == NID_undef) | |
2457 | return 0; | |
2458 | } | |
2459 | ||
2460 | if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_id(nid, NULL))) { | |
2461 | /* assume algorithm disabled */ | |
2462 | t->skip = 1; | |
2463 | return 1; | |
2464 | } | |
2465 | ||
2466 | if (EVP_PKEY_keygen_init(genctx) <= 0) { | |
2467 | t->err = "KEYGEN_INIT_ERROR"; | |
2468 | goto err; | |
2469 | } | |
2470 | ||
2471 | if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data)))) | |
2472 | goto err; | |
2473 | data->genctx = genctx; | |
2474 | data->keyname = NULL; | |
2475 | t->data = data; | |
2476 | t->err = NULL; | |
2477 | return 1; | |
2478 | ||
2479 | err: | |
2480 | EVP_PKEY_CTX_free(genctx); | |
2481 | return 0; | |
2482 | } | |
2483 | ||
2484 | static void keygen_test_cleanup(EVP_TEST *t) | |
2485 | { | |
2486 | KEYGEN_TEST_DATA *keygen = t->data; | |
2487 | ||
2488 | EVP_PKEY_CTX_free(keygen->genctx); | |
2489 | OPENSSL_free(keygen->keyname); | |
2490 | OPENSSL_free(t->data); | |
2491 | t->data = NULL; | |
2492 | } | |
2493 | ||
2494 | static int keygen_test_parse(EVP_TEST *t, | |
2495 | const char *keyword, const char *value) | |
2496 | { | |
2497 | KEYGEN_TEST_DATA *keygen = t->data; | |
2498 | ||
2499 | if (strcmp(keyword, "KeyName") == 0) | |
2500 | return TEST_ptr(keygen->keyname = OPENSSL_strdup(value)); | |
2501 | if (strcmp(keyword, "Ctrl") == 0) | |
2502 | return pkey_test_ctrl(t, keygen->genctx, value); | |
2503 | return 0; | |
2504 | } | |
2505 | ||
2506 | static int keygen_test_run(EVP_TEST *t) | |
2507 | { | |
2508 | KEYGEN_TEST_DATA *keygen = t->data; | |
2509 | EVP_PKEY *pkey = NULL; | |
88af1ebb | 2510 | int rv = 1; |
1f0fc03b | 2511 | |
1f0fc03b DSH |
2512 | if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) { |
2513 | t->err = "KEYGEN_GENERATE_ERROR"; | |
2514 | goto err; | |
2515 | } | |
2516 | ||
2517 | if (keygen->keyname != NULL) { | |
2518 | KEY_LIST *key; | |
2519 | ||
88af1ebb | 2520 | rv = 0; |
1f0fc03b DSH |
2521 | if (find_key(NULL, keygen->keyname, private_keys)) { |
2522 | TEST_info("Duplicate key %s", keygen->keyname); | |
2523 | goto err; | |
2524 | } | |
2525 | ||
2526 | if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key)))) | |
2527 | goto err; | |
2528 | key->name = keygen->keyname; | |
2529 | keygen->keyname = NULL; | |
2530 | key->key = pkey; | |
2531 | key->next = private_keys; | |
2532 | private_keys = key; | |
88af1ebb | 2533 | rv = 1; |
1f0fc03b DSH |
2534 | } else { |
2535 | EVP_PKEY_free(pkey); | |
2536 | } | |
2537 | ||
88af1ebb | 2538 | t->err = NULL; |
1f0fc03b DSH |
2539 | |
2540 | err: | |
88af1ebb | 2541 | return rv; |
1f0fc03b DSH |
2542 | } |
2543 | ||
2544 | static const EVP_TEST_METHOD keygen_test_method = { | |
2545 | "KeyGen", | |
2546 | keygen_test_init, | |
2547 | keygen_test_cleanup, | |
2548 | keygen_test_parse, | |
2549 | keygen_test_run, | |
2550 | }; | |
c49e0b04 RS |
2551 | |
2552 | /** | |
2553 | *** DIGEST SIGN+VERIFY TESTS | |
2554 | **/ | |
2555 | ||
75726fe8 | 2556 | typedef struct { |
2117a737 DSH |
2557 | int is_verify; /* Set to 1 if verifying */ |
2558 | int is_oneshot; /* Set to 1 for one shot operation */ | |
2559 | const EVP_MD *md; /* Digest to use */ | |
2560 | EVP_MD_CTX *ctx; /* Digest context */ | |
75726fe8 | 2561 | EVP_PKEY_CTX *pctx; |
2117a737 DSH |
2562 | STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */ |
2563 | unsigned char *osin; /* Input data if one shot */ | |
2564 | size_t osin_len; /* Input length data if one shot */ | |
2565 | unsigned char *output; /* Expected output */ | |
2566 | size_t output_len; /* Expected output length */ | |
75726fe8 DSH |
2567 | } DIGESTSIGN_DATA; |
2568 | ||
7b22334f DSH |
2569 | static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify, |
2570 | int is_oneshot) | |
75726fe8 DSH |
2571 | { |
2572 | const EVP_MD *md = NULL; | |
2573 | DIGESTSIGN_DATA *mdat; | |
2574 | ||
2575 | if (strcmp(alg, "NULL") != 0) { | |
2576 | if ((md = EVP_get_digestbyname(alg)) == NULL) { | |
2577 | /* If alg has an OID assume disabled algorithm */ | |
2578 | if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) { | |
2579 | t->skip = 1; | |
2580 | return 1; | |
2581 | } | |
2582 | return 0; | |
2583 | } | |
2584 | } | |
2585 | if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat)))) | |
2586 | return 0; | |
2587 | mdat->md = md; | |
2588 | if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) { | |
2589 | OPENSSL_free(mdat); | |
2590 | return 0; | |
2591 | } | |
2592 | mdat->is_verify = is_verify; | |
7b22334f | 2593 | mdat->is_oneshot = is_oneshot; |
75726fe8 DSH |
2594 | t->data = mdat; |
2595 | return 1; | |
2596 | } | |
2597 | ||
2598 | static int digestsign_test_init(EVP_TEST *t, const char *alg) | |
2599 | { | |
7b22334f | 2600 | return digestsigver_test_init(t, alg, 0, 0); |
75726fe8 DSH |
2601 | } |
2602 | ||
2603 | static void digestsigver_test_cleanup(EVP_TEST *t) | |
2604 | { | |
2605 | DIGESTSIGN_DATA *mdata = t->data; | |
2606 | ||
2607 | EVP_MD_CTX_free(mdata->ctx); | |
2608 | sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free); | |
7b22334f | 2609 | OPENSSL_free(mdata->osin); |
75726fe8 DSH |
2610 | OPENSSL_free(mdata->output); |
2611 | OPENSSL_free(mdata); | |
2612 | t->data = NULL; | |
2613 | } | |
2614 | ||
2615 | static int digestsigver_test_parse(EVP_TEST *t, | |
2616 | const char *keyword, const char *value) | |
2617 | { | |
2618 | DIGESTSIGN_DATA *mdata = t->data; | |
2619 | ||
2620 | if (strcmp(keyword, "Key") == 0) { | |
2621 | EVP_PKEY *pkey = NULL; | |
2622 | int rv = 0; | |
2623 | ||
2624 | if (mdata->is_verify) | |
2625 | rv = find_key(&pkey, value, public_keys); | |
2626 | if (rv == 0) | |
2627 | rv = find_key(&pkey, value, private_keys); | |
2628 | if (rv == 0 || pkey == NULL) { | |
2629 | t->skip = 1; | |
2630 | return 1; | |
2631 | } | |
2632 | if (mdata->is_verify) { | |
2633 | if (!EVP_DigestVerifyInit(mdata->ctx, &mdata->pctx, mdata->md, | |
2634 | NULL, pkey)) | |
2635 | t->err = "DIGESTVERIFYINIT_ERROR"; | |
2636 | return 1; | |
2637 | } | |
2638 | if (!EVP_DigestSignInit(mdata->ctx, &mdata->pctx, mdata->md, NULL, | |
2639 | pkey)) | |
2640 | t->err = "DIGESTSIGNINIT_ERROR"; | |
2641 | return 1; | |
2642 | } | |
2643 | ||
7b22334f DSH |
2644 | if (strcmp(keyword, "Input") == 0) { |
2645 | if (mdata->is_oneshot) | |
c49e0b04 | 2646 | return parse_bin(value, &mdata->osin, &mdata->osin_len); |
75726fe8 | 2647 | return evp_test_buffer_append(value, &mdata->input); |
7b22334f | 2648 | } |
75726fe8 | 2649 | if (strcmp(keyword, "Output") == 0) |
c49e0b04 | 2650 | return parse_bin(value, &mdata->output, &mdata->output_len); |
7b22334f DSH |
2651 | |
2652 | if (!mdata->is_oneshot) { | |
2653 | if (strcmp(keyword, "Count") == 0) | |
2654 | return evp_test_buffer_set_count(value, mdata->input); | |
2655 | if (strcmp(keyword, "Ncopy") == 0) | |
2656 | return evp_test_buffer_ncopy(value, mdata->input); | |
2657 | } | |
75726fe8 DSH |
2658 | if (strcmp(keyword, "Ctrl") == 0) { |
2659 | if (mdata->pctx == NULL) | |
f42c225d | 2660 | return -1; |
75726fe8 DSH |
2661 | return pkey_test_ctrl(t, mdata->pctx, value); |
2662 | } | |
2663 | return 0; | |
2664 | } | |
2665 | ||
2666 | static int digestsign_update_fn(void *ctx, const unsigned char *buf, | |
2667 | size_t buflen) | |
2668 | { | |
2669 | return EVP_DigestSignUpdate(ctx, buf, buflen); | |
2670 | } | |
2671 | ||
2672 | static int digestsign_test_run(EVP_TEST *t) | |
2673 | { | |
e3d378bc AP |
2674 | DIGESTSIGN_DATA *expected = t->data; |
2675 | unsigned char *got = NULL; | |
2676 | size_t got_len; | |
75726fe8 | 2677 | |
e3d378bc AP |
2678 | if (!evp_test_buffer_do(expected->input, digestsign_update_fn, |
2679 | expected->ctx)) { | |
75726fe8 DSH |
2680 | t->err = "DIGESTUPDATE_ERROR"; |
2681 | goto err; | |
2682 | } | |
2683 | ||
e3d378bc | 2684 | if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) { |
75726fe8 DSH |
2685 | t->err = "DIGESTSIGNFINAL_LENGTH_ERROR"; |
2686 | goto err; | |
2687 | } | |
e3d378bc | 2688 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { |
75726fe8 DSH |
2689 | t->err = "MALLOC_FAILURE"; |
2690 | goto err; | |
2691 | } | |
e3d378bc | 2692 | if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) { |
75726fe8 DSH |
2693 | t->err = "DIGESTSIGNFINAL_ERROR"; |
2694 | goto err; | |
2695 | } | |
4cceb185 P |
2696 | if (!memory_err_compare(t, "SIGNATURE_MISMATCH", |
2697 | expected->output, expected->output_len, | |
2698 | got, got_len)) | |
75726fe8 | 2699 | goto err; |
75726fe8 | 2700 | |
4cceb185 | 2701 | t->err = NULL; |
75726fe8 | 2702 | err: |
e3d378bc | 2703 | OPENSSL_free(got); |
75726fe8 DSH |
2704 | return 1; |
2705 | } | |
2706 | ||
2707 | static const EVP_TEST_METHOD digestsign_test_method = { | |
2708 | "DigestSign", | |
2709 | digestsign_test_init, | |
2710 | digestsigver_test_cleanup, | |
2711 | digestsigver_test_parse, | |
2712 | digestsign_test_run | |
2713 | }; | |
2714 | ||
2715 | static int digestverify_test_init(EVP_TEST *t, const char *alg) | |
2716 | { | |
7b22334f | 2717 | return digestsigver_test_init(t, alg, 1, 0); |
75726fe8 DSH |
2718 | } |
2719 | ||
2720 | static int digestverify_update_fn(void *ctx, const unsigned char *buf, | |
2721 | size_t buflen) | |
2722 | { | |
2723 | return EVP_DigestVerifyUpdate(ctx, buf, buflen); | |
2724 | } | |
2725 | ||
2726 | static int digestverify_test_run(EVP_TEST *t) | |
2727 | { | |
2728 | DIGESTSIGN_DATA *mdata = t->data; | |
2729 | ||
2730 | if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) { | |
2731 | t->err = "DIGESTUPDATE_ERROR"; | |
2732 | return 1; | |
2733 | } | |
2734 | ||
2735 | if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output, | |
2736 | mdata->output_len) <= 0) | |
2737 | t->err = "VERIFY_ERROR"; | |
2738 | return 1; | |
2739 | } | |
2740 | ||
2741 | static const EVP_TEST_METHOD digestverify_test_method = { | |
2742 | "DigestVerify", | |
2743 | digestverify_test_init, | |
2744 | digestsigver_test_cleanup, | |
2745 | digestsigver_test_parse, | |
2746 | digestverify_test_run | |
2747 | }; | |
2748 | ||
7b22334f DSH |
2749 | static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg) |
2750 | { | |
2751 | return digestsigver_test_init(t, alg, 0, 1); | |
2752 | } | |
2753 | ||
2754 | static int oneshot_digestsign_test_run(EVP_TEST *t) | |
2755 | { | |
e3d378bc AP |
2756 | DIGESTSIGN_DATA *expected = t->data; |
2757 | unsigned char *got = NULL; | |
2758 | size_t got_len; | |
7b22334f | 2759 | |
e3d378bc AP |
2760 | if (!EVP_DigestSign(expected->ctx, NULL, &got_len, |
2761 | expected->osin, expected->osin_len)) { | |
7b22334f DSH |
2762 | t->err = "DIGESTSIGN_LENGTH_ERROR"; |
2763 | goto err; | |
2764 | } | |
e3d378bc | 2765 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { |
7b22334f DSH |
2766 | t->err = "MALLOC_FAILURE"; |
2767 | goto err; | |
2768 | } | |
e3d378bc AP |
2769 | if (!EVP_DigestSign(expected->ctx, got, &got_len, |
2770 | expected->osin, expected->osin_len)) { | |
7b22334f DSH |
2771 | t->err = "DIGESTSIGN_ERROR"; |
2772 | goto err; | |
2773 | } | |
4cceb185 P |
2774 | if (!memory_err_compare(t, "SIGNATURE_MISMATCH", |
2775 | expected->output, expected->output_len, | |
2776 | got, got_len)) | |
7b22334f | 2777 | goto err; |
7b22334f | 2778 | |
4cceb185 | 2779 | t->err = NULL; |
7b22334f | 2780 | err: |
e3d378bc | 2781 | OPENSSL_free(got); |
7b22334f DSH |
2782 | return 1; |
2783 | } | |
2784 | ||
2785 | static const EVP_TEST_METHOD oneshot_digestsign_test_method = { | |
2786 | "OneShotDigestSign", | |
2787 | oneshot_digestsign_test_init, | |
2788 | digestsigver_test_cleanup, | |
2789 | digestsigver_test_parse, | |
2790 | oneshot_digestsign_test_run | |
2791 | }; | |
2792 | ||
2793 | static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg) | |
2794 | { | |
2795 | return digestsigver_test_init(t, alg, 1, 1); | |
2796 | } | |
2797 | ||
2798 | static int oneshot_digestverify_test_run(EVP_TEST *t) | |
2799 | { | |
2800 | DIGESTSIGN_DATA *mdata = t->data; | |
2801 | ||
2802 | if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len, | |
2803 | mdata->osin, mdata->osin_len) <= 0) | |
2804 | t->err = "VERIFY_ERROR"; | |
2805 | return 1; | |
2806 | } | |
2807 | ||
2808 | static const EVP_TEST_METHOD oneshot_digestverify_test_method = { | |
2809 | "OneShotDigestVerify", | |
2810 | oneshot_digestverify_test_init, | |
2811 | digestsigver_test_cleanup, | |
2812 | digestsigver_test_parse, | |
2813 | oneshot_digestverify_test_run | |
2814 | }; | |
2815 | ||
c49e0b04 RS |
2816 | |
2817 | /** | |
2818 | *** PARSING AND DISPATCH | |
2819 | **/ | |
2820 | ||
2821 | static const EVP_TEST_METHOD *evp_test_list[] = { | |
2822 | &cipher_test_method, | |
2823 | &digest_test_method, | |
2824 | &digestsign_test_method, | |
2825 | &digestverify_test_method, | |
2826 | &encode_test_method, | |
2827 | &kdf_test_method, | |
5a285add | 2828 | &pkey_kdf_test_method, |
c49e0b04 | 2829 | &keypair_test_method, |
1f0fc03b | 2830 | &keygen_test_method, |
c49e0b04 RS |
2831 | &mac_test_method, |
2832 | &oneshot_digestsign_test_method, | |
2833 | &oneshot_digestverify_test_method, | |
2834 | &pbe_test_method, | |
2835 | &pdecrypt_test_method, | |
2836 | &pderive_test_method, | |
2837 | &psign_test_method, | |
2838 | &pverify_recover_test_method, | |
2839 | &pverify_test_method, | |
2840 | NULL | |
2841 | }; | |
2842 | ||
2843 | static const EVP_TEST_METHOD *find_test(const char *name) | |
2844 | { | |
2845 | const EVP_TEST_METHOD **tt; | |
2846 | ||
2847 | for (tt = evp_test_list; *tt; tt++) { | |
2848 | if (strcmp(name, (*tt)->name) == 0) | |
2849 | return *tt; | |
2850 | } | |
2851 | return NULL; | |
2852 | } | |
2853 | ||
2854 | static void clear_test(EVP_TEST *t) | |
2855 | { | |
ae269dd8 | 2856 | test_clearstanza(&t->s); |
c49e0b04 RS |
2857 | ERR_clear_error(); |
2858 | if (t->data != NULL) { | |
2859 | if (t->meth != NULL) | |
2860 | t->meth->cleanup(t); | |
2861 | OPENSSL_free(t->data); | |
2862 | t->data = NULL; | |
2863 | } | |
2864 | OPENSSL_free(t->expected_err); | |
2865 | t->expected_err = NULL; | |
c49e0b04 RS |
2866 | OPENSSL_free(t->reason); |
2867 | t->reason = NULL; | |
ae269dd8 | 2868 | |
c49e0b04 RS |
2869 | /* Text literal. */ |
2870 | t->err = NULL; | |
2871 | t->skip = 0; | |
2872 | t->meth = NULL; | |
2873 | } | |
2874 | ||
2875 | /* | |
2876 | * Check for errors in the test structure; return 1 if okay, else 0. | |
2877 | */ | |
2878 | static int check_test_error(EVP_TEST *t) | |
2879 | { | |
2880 | unsigned long err; | |
c49e0b04 RS |
2881 | const char *reason; |
2882 | ||
2883 | if (t->err == NULL && t->expected_err == NULL) | |
2884 | return 1; | |
2885 | if (t->err != NULL && t->expected_err == NULL) { | |
2886 | if (t->aux_err != NULL) { | |
ae269dd8 RS |
2887 | TEST_info("%s:%d: Source of above error (%s); unexpected error %s", |
2888 | t->s.test_file, t->s.start, t->aux_err, t->err); | |
c49e0b04 | 2889 | } else { |
ae269dd8 RS |
2890 | TEST_info("%s:%d: Source of above error; unexpected error %s", |
2891 | t->s.test_file, t->s.start, t->err); | |
c49e0b04 RS |
2892 | } |
2893 | return 0; | |
2894 | } | |
2895 | if (t->err == NULL && t->expected_err != NULL) { | |
ae269dd8 RS |
2896 | TEST_info("%s:%d: Succeeded but was expecting %s", |
2897 | t->s.test_file, t->s.start, t->expected_err); | |
c49e0b04 RS |
2898 | return 0; |
2899 | } | |
2900 | ||
2901 | if (strcmp(t->err, t->expected_err) != 0) { | |
ae269dd8 RS |
2902 | TEST_info("%s:%d: Expected %s got %s", |
2903 | t->s.test_file, t->s.start, t->expected_err, t->err); | |
c49e0b04 RS |
2904 | return 0; |
2905 | } | |
2906 | ||
aac96e27 | 2907 | if (t->reason == NULL) |
c49e0b04 RS |
2908 | return 1; |
2909 | ||
aac96e27 | 2910 | if (t->reason == NULL) { |
ae269dd8 RS |
2911 | TEST_info("%s:%d: Test is missing function or reason code", |
2912 | t->s.test_file, t->s.start); | |
c49e0b04 RS |
2913 | return 0; |
2914 | } | |
2915 | ||
2916 | err = ERR_peek_error(); | |
2917 | if (err == 0) { | |
aac96e27 RS |
2918 | TEST_info("%s:%d: Expected error \"%s\" not set", |
2919 | t->s.test_file, t->s.start, t->reason); | |
c49e0b04 RS |
2920 | return 0; |
2921 | } | |
2922 | ||
c49e0b04 | 2923 | reason = ERR_reason_error_string(err); |
b13342e9 | 2924 | if (reason == NULL) { |
aac96e27 | 2925 | TEST_info("%s:%d: Expected error \"%s\", no strings available." |
ae269dd8 | 2926 | " Assuming ok.", |
aac96e27 | 2927 | t->s.test_file, t->s.start, t->reason); |
c49e0b04 RS |
2928 | return 1; |
2929 | } | |
2930 | ||
aac96e27 | 2931 | if (strcmp(reason, t->reason) == 0) |
c49e0b04 RS |
2932 | return 1; |
2933 | ||
aac96e27 RS |
2934 | TEST_info("%s:%d: Expected error \"%s\", got \"%s\"", |
2935 | t->s.test_file, t->s.start, t->reason, reason); | |
c49e0b04 RS |
2936 | |
2937 | return 0; | |
2938 | } | |
2939 | ||
2940 | /* | |
2941 | * Run a parsed test. Log a message and return 0 on error. | |
2942 | */ | |
2943 | static int run_test(EVP_TEST *t) | |
2944 | { | |
2945 | if (t->meth == NULL) | |
2946 | return 1; | |
ae269dd8 | 2947 | t->s.numtests++; |
c49e0b04 | 2948 | if (t->skip) { |
ae269dd8 | 2949 | t->s.numskip++; |
c49e0b04 RS |
2950 | } else { |
2951 | /* run the test */ | |
2952 | if (t->err == NULL && t->meth->run_test(t) != 1) { | |
ae269dd8 RS |
2953 | TEST_info("%s:%d %s error", |
2954 | t->s.test_file, t->s.start, t->meth->name); | |
c49e0b04 RS |
2955 | return 0; |
2956 | } | |
2957 | if (!check_test_error(t)) { | |
8fe3127c | 2958 | TEST_openssl_errors(); |
ae269dd8 | 2959 | t->s.errors++; |
c49e0b04 RS |
2960 | } |
2961 | } | |
2962 | ||
2963 | /* clean it up */ | |
2964 | return 1; | |
2965 | } | |
2966 | ||
2967 | static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst) | |
2968 | { | |
2969 | for (; lst != NULL; lst = lst->next) { | |
2970 | if (strcmp(lst->name, name) == 0) { | |
2971 | if (ppk != NULL) | |
2972 | *ppk = lst->key; | |
2973 | return 1; | |
2974 | } | |
2975 | } | |
2976 | return 0; | |
2977 | } | |
2978 | ||
2979 | static void free_key_list(KEY_LIST *lst) | |
2980 | { | |
2981 | while (lst != NULL) { | |
2982 | KEY_LIST *next = lst->next; | |
2983 | ||
2984 | EVP_PKEY_free(lst->key); | |
2985 | OPENSSL_free(lst->name); | |
2986 | OPENSSL_free(lst); | |
2987 | lst = next; | |
2988 | } | |
2989 | } | |
2990 | ||
c49e0b04 RS |
2991 | /* |
2992 | * Is the key type an unsupported algorithm? | |
2993 | */ | |
3cb7c5cf | 2994 | static int key_unsupported(void) |
c49e0b04 RS |
2995 | { |
2996 | long err = ERR_peek_error(); | |
2997 | ||
2998 | if (ERR_GET_LIB(err) == ERR_LIB_EVP | |
b533510f MC |
2999 | && (ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM |
3000 | || ERR_GET_REASON(err) == EVP_R_FETCH_FAILED)) { | |
c49e0b04 RS |
3001 | ERR_clear_error(); |
3002 | return 1; | |
3003 | } | |
3004 | #ifndef OPENSSL_NO_EC | |
3005 | /* | |
3006 | * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an | |
3007 | * hint to an unsupported algorithm/curve (e.g. if binary EC support is | |
3008 | * disabled). | |
3009 | */ | |
3010 | if (ERR_GET_LIB(err) == ERR_LIB_EC | |
3011 | && ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP) { | |
3012 | ERR_clear_error(); | |
3013 | return 1; | |
3014 | } | |
3015 | #endif /* OPENSSL_NO_EC */ | |
3016 | return 0; | |
3017 | } | |
3018 | ||
3019 | /* | |
ae269dd8 | 3020 | * NULL out the value from |pp| but return it. This "steals" a pointer. |
c49e0b04 | 3021 | */ |
ae269dd8 | 3022 | static char *take_value(PAIR *pp) |
c49e0b04 | 3023 | { |
ae269dd8 RS |
3024 | char *p = pp->value; |
3025 | ||
3026 | pp->value = NULL; | |
3027 | return p; | |
3028 | } | |
3029 | ||
3b5d61f4 RL |
3030 | /* |
3031 | * Return 1 if one of the providers named in the string is available. | |
3032 | * The provider names are separated with whitespace. | |
3033 | * NOTE: destructive function, it inserts '\0' after each provider name. | |
3034 | */ | |
3035 | static int prov_available(char *providers) | |
3036 | { | |
3037 | char *p; | |
3038 | int more = 1; | |
3039 | ||
3040 | while (more) { | |
3041 | for (; isspace(*providers); providers++) | |
3042 | continue; | |
3043 | if (*providers == '\0') | |
3044 | break; /* End of the road */ | |
3045 | for (p = providers; *p != '\0' && !isspace(*p); p++) | |
3046 | continue; | |
3047 | if (*p == '\0') | |
3048 | more = 0; | |
3049 | else | |
3050 | *p = '\0'; | |
3051 | if (OSSL_PROVIDER_available(NULL, providers)) | |
3052 | return 1; /* Found one */ | |
3053 | } | |
3054 | return 0; | |
3055 | } | |
3056 | ||
ae269dd8 RS |
3057 | /* |
3058 | * Read and parse one test. Return 0 if failure, 1 if okay. | |
3059 | */ | |
3060 | static int parse(EVP_TEST *t) | |
3061 | { | |
3062 | KEY_LIST *key, **klist; | |
c49e0b04 | 3063 | EVP_PKEY *pkey; |
ae269dd8 RS |
3064 | PAIR *pp; |
3065 | int i; | |
c49e0b04 | 3066 | |
c49e0b04 | 3067 | top: |
ae269dd8 RS |
3068 | do { |
3069 | if (BIO_eof(t->s.fp)) | |
c49e0b04 | 3070 | return EOF; |
ae269dd8 RS |
3071 | clear_test(t); |
3072 | if (!test_readstanza(&t->s)) | |
3073 | return 0; | |
3074 | } while (t->s.numpairs == 0); | |
3075 | pp = &t->s.pairs[0]; | |
c49e0b04 | 3076 | |
ae269dd8 | 3077 | /* Are we adding a key? */ |
c49e0b04 RS |
3078 | klist = NULL; |
3079 | pkey = NULL; | |
ae269dd8 RS |
3080 | if (strcmp(pp->key, "PrivateKey") == 0) { |
3081 | pkey = PEM_read_bio_PrivateKey(t->s.key, NULL, 0, NULL); | |
c49e0b04 | 3082 | if (pkey == NULL && !key_unsupported()) { |
1bf2cc23 | 3083 | EVP_PKEY_free(pkey); |
ae269dd8 | 3084 | TEST_info("Can't read private key %s", pp->value); |
8fe3127c | 3085 | TEST_openssl_errors(); |
c49e0b04 RS |
3086 | return 0; |
3087 | } | |
3088 | klist = &private_keys; | |
4665244c | 3089 | } else if (strcmp(pp->key, "PublicKey") == 0) { |
ae269dd8 | 3090 | pkey = PEM_read_bio_PUBKEY(t->s.key, NULL, 0, NULL); |
c49e0b04 | 3091 | if (pkey == NULL && !key_unsupported()) { |
1bf2cc23 | 3092 | EVP_PKEY_free(pkey); |
ae269dd8 | 3093 | TEST_info("Can't read public key %s", pp->value); |
8fe3127c | 3094 | TEST_openssl_errors(); |
c49e0b04 RS |
3095 | return 0; |
3096 | } | |
3097 | klist = &public_keys; | |
4665244c MC |
3098 | } else if (strcmp(pp->key, "PrivateKeyRaw") == 0 |
3099 | || strcmp(pp->key, "PublicKeyRaw") == 0 ) { | |
3100 | char *strnid = NULL, *keydata = NULL; | |
3101 | unsigned char *keybin; | |
3102 | size_t keylen; | |
3103 | int nid; | |
3104 | ||
3105 | if (strcmp(pp->key, "PrivateKeyRaw") == 0) | |
3106 | klist = &private_keys; | |
3107 | else | |
3108 | klist = &public_keys; | |
3109 | ||
3110 | strnid = strchr(pp->value, ':'); | |
3111 | if (strnid != NULL) { | |
3112 | *strnid++ = '\0'; | |
3113 | keydata = strchr(strnid, ':'); | |
3114 | if (keydata != NULL) | |
3115 | *keydata++ = '\0'; | |
3116 | } | |
3117 | if (keydata == NULL) { | |
3118 | TEST_info("Failed to parse %s value", pp->key); | |
3119 | return 0; | |
3120 | } | |
3121 | ||
3122 | nid = OBJ_txt2nid(strnid); | |
3123 | if (nid == NID_undef) { | |
3124 | TEST_info("Uncrecognised algorithm NID"); | |
3125 | return 0; | |
3126 | } | |
3127 | if (!parse_bin(keydata, &keybin, &keylen)) { | |
3128 | TEST_info("Failed to create binary key"); | |
3129 | return 0; | |
3130 | } | |
3131 | if (klist == &private_keys) | |
f929439f | 3132 | pkey = EVP_PKEY_new_raw_private_key(nid, NULL, keybin, keylen); |
4665244c | 3133 | else |
f929439f | 3134 | pkey = EVP_PKEY_new_raw_public_key(nid, NULL, keybin, keylen); |
66a925ea | 3135 | if (pkey == NULL && !key_unsupported()) { |
4665244c MC |
3136 | TEST_info("Can't read %s data", pp->key); |
3137 | OPENSSL_free(keybin); | |
3138 | TEST_openssl_errors(); | |
3139 | return 0; | |
3140 | } | |
3141 | OPENSSL_free(keybin); | |
c49e0b04 RS |
3142 | } |
3143 | ||
3144 | /* If we have a key add to list */ | |
3145 | if (klist != NULL) { | |
ae269dd8 RS |
3146 | if (find_key(NULL, pp->value, *klist)) { |
3147 | TEST_info("Duplicate key %s", pp->value); | |
c49e0b04 RS |
3148 | return 0; |
3149 | } | |
ae269dd8 | 3150 | if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key)))) |
c49e0b04 | 3151 | return 0; |
ae269dd8 | 3152 | key->name = take_value(pp); |
c49e0b04 RS |
3153 | key->key = pkey; |
3154 | key->next = *klist; | |
3155 | *klist = key; | |
3156 | ||
3157 | /* Go back and start a new stanza. */ | |
ae269dd8 RS |
3158 | if (t->s.numpairs != 1) |
3159 | TEST_info("Line %d: missing blank line\n", t->s.curr); | |
c49e0b04 RS |
3160 | goto top; |
3161 | } | |
3162 | ||
ae269dd8 RS |
3163 | /* Find the test, based on first keyword. */ |
3164 | if (!TEST_ptr(t->meth = find_test(pp->key))) | |
3165 | return 0; | |
3166 | if (!t->meth->init(t, pp->value)) { | |
3167 | TEST_error("unknown %s: %s\n", pp->key, pp->value); | |
3168 | return 0; | |
c49e0b04 RS |
3169 | } |
3170 | if (t->skip == 1) { | |
ae269dd8 RS |
3171 | /* TEST_info("skipping %s %s", pp->key, pp->value); */ |
3172 | return 0; | |
c49e0b04 RS |
3173 | } |
3174 | ||
ae269dd8 | 3175 | for (pp++, i = 1; i < t->s.numpairs; pp++, i++) { |
3b5d61f4 RL |
3176 | if (strcmp(pp->key, "Availablein") == 0) { |
3177 | if (!prov_available(pp->value)) { | |
3178 | TEST_info("skipping, providers not available: %s:%d", | |
3179 | t->s.test_file, t->s.start); | |
3180 | t->skip = 1; | |
3181 | return 0; | |
3182 | } | |
8453096e | 3183 | } else if (strcmp(pp->key, "Result") == 0) { |
c49e0b04 | 3184 | if (t->expected_err != NULL) { |
ae269dd8 RS |
3185 | TEST_info("Line %d: multiple result lines", t->s.curr); |
3186 | return 0; | |
c49e0b04 | 3187 | } |
ae269dd8 RS |
3188 | t->expected_err = take_value(pp); |
3189 | } else if (strcmp(pp->key, "Function") == 0) { | |
aac96e27 | 3190 | /* Ignore old line. */ |
ae269dd8 | 3191 | } else if (strcmp(pp->key, "Reason") == 0) { |
c49e0b04 | 3192 | if (t->reason != NULL) { |
ae269dd8 RS |
3193 | TEST_info("Line %d: multiple reason lines", t->s.curr); |
3194 | return 0; | |
c49e0b04 | 3195 | } |
ae269dd8 | 3196 | t->reason = take_value(pp); |
c49e0b04 RS |
3197 | } else { |
3198 | /* Must be test specific line: try to parse it */ | |
ae269dd8 | 3199 | int rv = t->meth->parse(t, pp->key, pp->value); |
c49e0b04 RS |
3200 | |
3201 | if (rv == 0) { | |
ae269dd8 RS |
3202 | TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key); |
3203 | return 0; | |
c49e0b04 RS |
3204 | } |
3205 | if (rv < 0) { | |
ce5d64c7 RL |
3206 | TEST_info("Line %d: error processing keyword %s = %s\n", |
3207 | t->s.curr, pp->key, pp->value); | |
ae269dd8 | 3208 | return 0; |
c49e0b04 RS |
3209 | } |
3210 | } | |
3211 | } | |
3212 | ||
3213 | return 1; | |
c49e0b04 RS |
3214 | } |
3215 | ||
ae269dd8 | 3216 | static int run_file_tests(int i) |
6c5943c9 | 3217 | { |
ae269dd8 | 3218 | EVP_TEST *t; |
ad887416 | 3219 | const char *testfile = test_get_argument(i); |
c49e0b04 | 3220 | int c; |
6c5943c9 | 3221 | |
ae269dd8 | 3222 | if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t)))) |
6c5943c9 | 3223 | return 0; |
ad887416 | 3224 | if (!test_start_file(&t->s, testfile)) { |
ae269dd8 RS |
3225 | OPENSSL_free(t); |
3226 | return 0; | |
3227 | } | |
c49e0b04 | 3228 | |
ae269dd8 RS |
3229 | while (!BIO_eof(t->s.fp)) { |
3230 | c = parse(t); | |
d5e5e2ff SL |
3231 | if (t->skip) { |
3232 | t->s.numskip++; | |
c49e0b04 | 3233 | continue; |
d5e5e2ff | 3234 | } |
ae269dd8 RS |
3235 | if (c == 0 || !run_test(t)) { |
3236 | t->s.errors++; | |
c49e0b04 RS |
3237 | break; |
3238 | } | |
6c5943c9 | 3239 | } |
ae269dd8 RS |
3240 | test_end_file(&t->s); |
3241 | clear_test(t); | |
6c5943c9 | 3242 | |
6c5943c9 RS |
3243 | free_key_list(public_keys); |
3244 | free_key_list(private_keys); | |
ae269dd8 RS |
3245 | BIO_free(t->s.key); |
3246 | c = t->s.errors; | |
3247 | OPENSSL_free(t); | |
3248 | return c == 0; | |
6c5943c9 RS |
3249 | } |
3250 | ||
a43ce58f SL |
3251 | OPT_TEST_DECLARE_USAGE("file...\n") |
3252 | ||
ad887416 | 3253 | int setup_tests(void) |
6c5943c9 | 3254 | { |
8d242823 | 3255 | size_t n; |
ad887416 | 3256 | |
8d242823 MC |
3257 | if (!test_skip_common_options()) { |
3258 | TEST_error("Error parsing test options\n"); | |
3259 | return 0; | |
3260 | } | |
3261 | ||
3262 | n = test_get_argument_count(); | |
a43ce58f | 3263 | if (n == 0) |
6c5943c9 | 3264 | return 0; |
6c5943c9 | 3265 | |
ad887416 P |
3266 | ADD_ALL_TESTS(run_file_tests, n); |
3267 | return 1; | |
6c5943c9 | 3268 | } |