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