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