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