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