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