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