]>
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; |
b2f90e93 JJ |
1204 | if (!TEST_ptr(mdat->mac_name = OPENSSL_strdup(alg))) { |
1205 | OPENSSL_free(mdat); | |
1206 | return 0; | |
1207 | } | |
1208 | ||
2bdb4af5 | 1209 | mdat->mac = mac; |
b2f90e93 JJ |
1210 | if (!TEST_ptr(mdat->controls = sk_OPENSSL_STRING_new_null())) { |
1211 | OPENSSL_free(mdat->mac_name); | |
1212 | OPENSSL_free(mdat); | |
1213 | return 0; | |
1214 | } | |
1215 | ||
36b6db08 | 1216 | mdat->output_size = mdat->block_size = -1; |
83251f39 DSH |
1217 | t->data = mdat; |
1218 | return 1; | |
1219 | } | |
1220 | ||
7e6a3025 RL |
1221 | /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */ |
1222 | static void openssl_free(char *m) | |
1223 | { | |
1224 | OPENSSL_free(m); | |
1225 | } | |
1226 | ||
6c5943c9 | 1227 | static void mac_test_cleanup(EVP_TEST *t) |
83251f39 | 1228 | { |
6c5943c9 RS |
1229 | MAC_DATA *mdat = t->data; |
1230 | ||
25446a66 | 1231 | EVP_MAC_free(mdat->mac); |
f651c727 | 1232 | OPENSSL_free(mdat->mac_name); |
7e6a3025 | 1233 | sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free); |
6c5943c9 RS |
1234 | OPENSSL_free(mdat->alg); |
1235 | OPENSSL_free(mdat->key); | |
afc580b9 | 1236 | OPENSSL_free(mdat->iv); |
6e624a64 | 1237 | OPENSSL_free(mdat->custom); |
b215db23 | 1238 | OPENSSL_free(mdat->salt); |
6c5943c9 RS |
1239 | OPENSSL_free(mdat->input); |
1240 | OPENSSL_free(mdat->output); | |
83251f39 DSH |
1241 | } |
1242 | ||
6c5943c9 | 1243 | static int mac_test_parse(EVP_TEST *t, |
83251f39 DSH |
1244 | const char *keyword, const char *value) |
1245 | { | |
6c5943c9 RS |
1246 | MAC_DATA *mdata = t->data; |
1247 | ||
86885c28 | 1248 | if (strcmp(keyword, "Key") == 0) |
c49e0b04 | 1249 | return parse_bin(value, &mdata->key, &mdata->key_len); |
afc580b9 P |
1250 | if (strcmp(keyword, "IV") == 0) |
1251 | return parse_bin(value, &mdata->iv, &mdata->iv_len); | |
6e624a64 SL |
1252 | if (strcmp(keyword, "Custom") == 0) |
1253 | return parse_bin(value, &mdata->custom, &mdata->custom_len); | |
b215db23 AS |
1254 | if (strcmp(keyword, "Salt") == 0) |
1255 | return parse_bin(value, &mdata->salt, &mdata->salt_len); | |
86885c28 | 1256 | if (strcmp(keyword, "Algorithm") == 0) { |
7644a9ae | 1257 | mdata->alg = OPENSSL_strdup(value); |
83251f39 | 1258 | if (!mdata->alg) |
f42c225d | 1259 | return -1; |
83251f39 DSH |
1260 | return 1; |
1261 | } | |
86885c28 | 1262 | if (strcmp(keyword, "Input") == 0) |
c49e0b04 | 1263 | return parse_bin(value, &mdata->input, &mdata->input_len); |
86885c28 | 1264 | if (strcmp(keyword, "Output") == 0) |
c49e0b04 | 1265 | return parse_bin(value, &mdata->output, &mdata->output_len); |
6a38b09a P |
1266 | if (strcmp(keyword, "XOF") == 0) |
1267 | return mdata->xof = 1; | |
7e6a3025 RL |
1268 | if (strcmp(keyword, "Ctrl") == 0) |
1269 | return sk_OPENSSL_STRING_push(mdata->controls, | |
1270 | OPENSSL_strdup(value)) != 0; | |
36b6db08 P |
1271 | if (strcmp(keyword, "OutputSize") == 0) { |
1272 | mdata->output_size = atoi(value); | |
1273 | if (mdata->output_size < 0) | |
1274 | return -1; | |
1275 | return 1; | |
1276 | } | |
1277 | if (strcmp(keyword, "BlockSize") == 0) { | |
1278 | mdata->block_size = atoi(value); | |
1279 | if (mdata->block_size < 0) | |
1280 | return -1; | |
1281 | return 1; | |
1282 | } | |
83251f39 DSH |
1283 | return 0; |
1284 | } | |
1285 | ||
ce5d64c7 RL |
1286 | static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx, |
1287 | const char *value) | |
1288 | { | |
337ace06 | 1289 | int rv = 0; |
ce5d64c7 RL |
1290 | char *p, *tmpval; |
1291 | ||
1292 | if (!TEST_ptr(tmpval = OPENSSL_strdup(value))) | |
1293 | return 0; | |
1294 | p = strchr(tmpval, ':'); | |
337ace06 | 1295 | if (p != NULL) { |
ce5d64c7 | 1296 | *p++ = '\0'; |
337ace06 P |
1297 | rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p); |
1298 | } | |
ce5d64c7 RL |
1299 | if (rv == -2) |
1300 | t->err = "PKEY_CTRL_INVALID"; | |
1301 | else if (rv <= 0) | |
1302 | t->err = "PKEY_CTRL_ERROR"; | |
1303 | else | |
1304 | rv = 1; | |
1305 | OPENSSL_free(tmpval); | |
1306 | return rv > 0; | |
1307 | } | |
1308 | ||
2bdb4af5 | 1309 | static int mac_test_run_pkey(EVP_TEST *t) |
83251f39 | 1310 | { |
e3d378bc | 1311 | MAC_DATA *expected = t->data; |
83251f39 DSH |
1312 | EVP_MD_CTX *mctx = NULL; |
1313 | EVP_PKEY_CTX *pctx = NULL, *genctx = NULL; | |
1314 | EVP_PKEY *key = NULL; | |
5ccada09 SL |
1315 | const char *mdname = NULL; |
1316 | EVP_CIPHER *cipher = NULL; | |
e3d378bc AP |
1317 | unsigned char *got = NULL; |
1318 | size_t got_len; | |
7e6a3025 | 1319 | int i; |
83251f39 | 1320 | |
6a38b09a P |
1321 | /* We don't do XOF mode via PKEY */ |
1322 | if (expected->xof) | |
1323 | return 1; | |
1324 | ||
2bdb4af5 RL |
1325 | if (expected->alg == NULL) |
1326 | TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type)); | |
1327 | else | |
1328 | TEST_info("Trying the EVP_PKEY %s test with %s", | |
1329 | OBJ_nid2sn(expected->type), expected->alg); | |
1330 | ||
5ccada09 | 1331 | if (expected->type == EVP_PKEY_CMAC) { |
a3d267f1 RS |
1332 | #ifdef OPENSSL_NO_DEPRECATED_3_0 |
1333 | TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg); | |
1334 | t->skip = 1; | |
1335 | t->err = NULL; | |
1336 | goto err; | |
1337 | #else | |
1338 | OSSL_LIB_CTX *tmpctx; | |
1339 | ||
ab7f4a3d | 1340 | if (expected->alg != NULL && is_cipher_disabled(expected->alg)) { |
5ccada09 SL |
1341 | TEST_info("skipping, PKEY CMAC '%s' is disabled", expected->alg); |
1342 | t->skip = 1; | |
1343 | t->err = NULL; | |
1344 | goto err; | |
1345 | } | |
1346 | if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, expected->alg, NULL))) { | |
1347 | t->err = "MAC_KEY_CREATE_ERROR"; | |
1348 | goto err; | |
1349 | } | |
a3d267f1 RS |
1350 | tmpctx = OSSL_LIB_CTX_set0_default(libctx); |
1351 | key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len, | |
1352 | cipher); | |
1353 | OSSL_LIB_CTX_set0_default(tmpctx); | |
1354 | #endif | |
5ccada09 | 1355 | } else { |
d8652be0 MC |
1356 | key = EVP_PKEY_new_raw_private_key_ex(libctx, |
1357 | OBJ_nid2sn(expected->type), NULL, | |
1358 | expected->key, expected->key_len); | |
5ccada09 | 1359 | } |
9442c8d7 MC |
1360 | if (key == NULL) { |
1361 | t->err = "MAC_KEY_CREATE_ERROR"; | |
83251f39 | 1362 | goto err; |
6c5943c9 | 1363 | } |
83251f39 | 1364 | |
ab7f4a3d | 1365 | if (expected->type == EVP_PKEY_HMAC && expected->alg != NULL) { |
5ccada09 SL |
1366 | if (is_digest_disabled(expected->alg)) { |
1367 | TEST_info("skipping, HMAC '%s' is disabled", expected->alg); | |
1368 | t->skip = 1; | |
1369 | t->err = NULL; | |
83251f39 | 1370 | goto err; |
6c5943c9 | 1371 | } |
5ccada09 | 1372 | mdname = expected->alg; |
83251f39 | 1373 | } |
6c5943c9 RS |
1374 | if (!TEST_ptr(mctx = EVP_MD_CTX_new())) { |
1375 | t->err = "INTERNAL_ERROR"; | |
83251f39 | 1376 | goto err; |
6c5943c9 | 1377 | } |
af6171b3 | 1378 | if (!EVP_DigestSignInit_ex(mctx, &pctx, mdname, libctx, NULL, key, NULL)) { |
6c5943c9 | 1379 | t->err = "DIGESTSIGNINIT_ERROR"; |
83251f39 | 1380 | goto err; |
6c5943c9 | 1381 | } |
7e6a3025 | 1382 | for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) |
ce5d64c7 RL |
1383 | if (!mac_test_ctrl_pkey(t, pctx, |
1384 | sk_OPENSSL_STRING_value(expected->controls, | |
1385 | i))) { | |
7e6a3025 RL |
1386 | t->err = "EVPPKEYCTXCTRL_ERROR"; |
1387 | goto err; | |
1388 | } | |
e3d378bc | 1389 | if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) { |
6c5943c9 | 1390 | t->err = "DIGESTSIGNUPDATE_ERROR"; |
83251f39 | 1391 | goto err; |
83251f39 | 1392 | } |
e3d378bc | 1393 | if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) { |
6c5943c9 | 1394 | t->err = "DIGESTSIGNFINAL_LENGTH_ERROR"; |
83251f39 | 1395 | goto err; |
6c5943c9 | 1396 | } |
e3d378bc | 1397 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { |
6c5943c9 | 1398 | t->err = "TEST_FAILURE"; |
83251f39 | 1399 | goto err; |
6c5943c9 | 1400 | } |
e3d378bc | 1401 | if (!EVP_DigestSignFinal(mctx, got, &got_len) |
4cceb185 P |
1402 | || !memory_err_compare(t, "TEST_MAC_ERR", |
1403 | expected->output, expected->output_len, | |
1404 | got, got_len)) { | |
41248607 RS |
1405 | t->err = "TEST_MAC_ERR"; |
1406 | goto err; | |
1407 | } | |
6c5943c9 | 1408 | t->err = NULL; |
83251f39 | 1409 | err: |
5ccada09 | 1410 | EVP_CIPHER_free(cipher); |
bfb0641f | 1411 | EVP_MD_CTX_free(mctx); |
e3d378bc | 1412 | OPENSSL_free(got); |
c5ba2d99 RS |
1413 | EVP_PKEY_CTX_free(genctx); |
1414 | EVP_PKEY_free(key); | |
83251f39 DSH |
1415 | return 1; |
1416 | } | |
1417 | ||
2bdb4af5 RL |
1418 | static int mac_test_run_mac(EVP_TEST *t) |
1419 | { | |
1420 | MAC_DATA *expected = t->data; | |
1421 | EVP_MAC_CTX *ctx = NULL; | |
2bdb4af5 | 1422 | unsigned char *got = NULL; |
0f7a4ca5 | 1423 | size_t got_len = 0, size = 0; |
36b6db08 P |
1424 | int i, block_size = -1, output_size = -1; |
1425 | OSSL_PARAM params[21], sizes[3], *psizes = sizes; | |
25446a66 RL |
1426 | size_t params_n = 0; |
1427 | size_t params_n_allocstart = 0; | |
1428 | const OSSL_PARAM *defined_params = | |
41f7ecf3 | 1429 | EVP_MAC_settable_ctx_params(expected->mac); |
0f7a4ca5 | 1430 | int xof; |
2bdb4af5 RL |
1431 | |
1432 | if (expected->alg == NULL) | |
f651c727 | 1433 | TEST_info("Trying the EVP_MAC %s test", expected->mac_name); |
2bdb4af5 RL |
1434 | else |
1435 | TEST_info("Trying the EVP_MAC %s test with %s", | |
f651c727 | 1436 | expected->mac_name, expected->alg); |
2bdb4af5 | 1437 | |
703170d4 RL |
1438 | if (expected->alg != NULL) { |
1439 | /* | |
1440 | * The underlying algorithm may be a cipher or a digest. | |
1441 | * We don't know which it is, but we can ask the MAC what it | |
1442 | * should be and bet on that. | |
1443 | */ | |
1444 | if (OSSL_PARAM_locate_const(defined_params, | |
1445 | OSSL_MAC_PARAM_CIPHER) != NULL) { | |
1446 | params[params_n++] = | |
1447 | OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER, | |
7f588d20 | 1448 | expected->alg, 0); |
703170d4 RL |
1449 | } else if (OSSL_PARAM_locate_const(defined_params, |
1450 | OSSL_MAC_PARAM_DIGEST) != NULL) { | |
1451 | params[params_n++] = | |
1452 | OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, | |
7f588d20 | 1453 | expected->alg, 0); |
703170d4 RL |
1454 | } else { |
1455 | t->err = "MAC_BAD_PARAMS"; | |
1456 | goto err; | |
1457 | } | |
1458 | } | |
25446a66 RL |
1459 | if (expected->custom != NULL) |
1460 | params[params_n++] = | |
1461 | OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM, | |
1462 | expected->custom, | |
1463 | expected->custom_len); | |
1464 | if (expected->salt != NULL) | |
1465 | params[params_n++] = | |
1466 | OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT, | |
1467 | expected->salt, | |
1468 | expected->salt_len); | |
1469 | if (expected->iv != NULL) | |
1470 | params[params_n++] = | |
1471 | OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV, | |
1472 | expected->iv, | |
1473 | expected->iv_len); | |
1474 | ||
5ccada09 | 1475 | /* Unknown controls. They must match parameters that the MAC recognizes */ |
25446a66 RL |
1476 | if (params_n + sk_OPENSSL_STRING_num(expected->controls) |
1477 | >= OSSL_NELEM(params)) { | |
1478 | t->err = "MAC_TOO_MANY_PARAMETERS"; | |
2bdb4af5 RL |
1479 | goto err; |
1480 | } | |
25446a66 RL |
1481 | params_n_allocstart = params_n; |
1482 | for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) { | |
1483 | char *tmpkey, *tmpval; | |
1484 | char *value = sk_OPENSSL_STRING_value(expected->controls, i); | |
2bdb4af5 | 1485 | |
25446a66 RL |
1486 | if (!TEST_ptr(tmpkey = OPENSSL_strdup(value))) { |
1487 | t->err = "MAC_PARAM_ERROR"; | |
2bdb4af5 RL |
1488 | goto err; |
1489 | } | |
25446a66 RL |
1490 | tmpval = strchr(tmpkey, ':'); |
1491 | if (tmpval != NULL) | |
1492 | *tmpval++ = '\0'; | |
1493 | ||
d5f85429 RL |
1494 | if (tmpval == NULL |
1495 | || !OSSL_PARAM_allocate_from_text(¶ms[params_n], | |
1496 | defined_params, | |
1497 | tmpkey, tmpval, | |
2ee0dfa6 | 1498 | strlen(tmpval), NULL)) { |
25446a66 RL |
1499 | OPENSSL_free(tmpkey); |
1500 | t->err = "MAC_PARAM_ERROR"; | |
6e624a64 SL |
1501 | goto err; |
1502 | } | |
25446a66 | 1503 | params_n++; |
2bdb4af5 | 1504 | |
25446a66 | 1505 | OPENSSL_free(tmpkey); |
b215db23 | 1506 | } |
25446a66 | 1507 | params[params_n] = OSSL_PARAM_construct_end(); |
b215db23 | 1508 | |
865adf97 | 1509 | if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) { |
25446a66 RL |
1510 | t->err = "MAC_CREATE_ERROR"; |
1511 | goto err; | |
afc580b9 P |
1512 | } |
1513 | ||
41df96ef | 1514 | if (!EVP_MAC_init(ctx, expected->key, expected->key_len, params)) { |
b215db23 AS |
1515 | t->err = "MAC_INIT_ERROR"; |
1516 | goto err; | |
1517 | } | |
36b6db08 P |
1518 | if (expected->output_size >= 0) |
1519 | *psizes++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_SIZE, | |
1520 | &output_size); | |
1521 | if (expected->block_size >= 0) | |
1522 | *psizes++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_BLOCK_SIZE, | |
1523 | &block_size); | |
1524 | if (psizes != sizes) { | |
1525 | *psizes = OSSL_PARAM_construct_end(); | |
1526 | if (!TEST_true(EVP_MAC_CTX_get_params(ctx, sizes))) { | |
1527 | t->err = "INTERNAL_ERROR"; | |
1528 | goto err; | |
1529 | } | |
1530 | if (expected->output_size >= 0 | |
1531 | && !TEST_int_eq(output_size, expected->output_size)) { | |
1532 | t->err = "TEST_FAILURE"; | |
1533 | goto err; | |
1534 | } | |
1535 | if (expected->block_size >= 0 | |
1536 | && !TEST_int_eq(block_size, expected->block_size)) { | |
1537 | t->err = "TEST_FAILURE"; | |
1538 | goto err; | |
1539 | } | |
1540 | } | |
2bdb4af5 RL |
1541 | if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) { |
1542 | t->err = "MAC_UPDATE_ERROR"; | |
1543 | goto err; | |
1544 | } | |
0f7a4ca5 P |
1545 | xof = expected->xof; |
1546 | if (xof) { | |
6a38b09a P |
1547 | if (!TEST_ptr(got = OPENSSL_malloc(expected->output_len))) { |
1548 | t->err = "TEST_FAILURE"; | |
1549 | goto err; | |
1550 | } | |
1551 | if (!EVP_MAC_finalXOF(ctx, got, expected->output_len) | |
1552 | || !memory_err_compare(t, "TEST_MAC_ERR", | |
1553 | expected->output, expected->output_len, | |
1554 | got, expected->output_len)) { | |
1555 | t->err = "MAC_FINAL_ERROR"; | |
1556 | goto err; | |
1557 | } | |
1558 | } else { | |
1559 | if (!EVP_MAC_final(ctx, NULL, &got_len, 0)) { | |
1560 | t->err = "MAC_FINAL_LENGTH_ERROR"; | |
1561 | goto err; | |
1562 | } | |
1563 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { | |
1564 | t->err = "TEST_FAILURE"; | |
1565 | goto err; | |
1566 | } | |
1567 | if (!EVP_MAC_final(ctx, got, &got_len, got_len) | |
1568 | || !memory_err_compare(t, "TEST_MAC_ERR", | |
1569 | expected->output, expected->output_len, | |
1570 | got, got_len)) { | |
1571 | t->err = "TEST_MAC_ERR"; | |
1572 | goto err; | |
1573 | } | |
2bdb4af5 RL |
1574 | } |
1575 | t->err = NULL; | |
0f7a4ca5 P |
1576 | |
1577 | /* Test the EVP_Q_mac interface as well */ | |
1578 | if (!xof) { | |
9f54970c | 1579 | OPENSSL_cleanse(got, got_len); |
0f7a4ca5 P |
1580 | if (!TEST_true(EVP_Q_mac(libctx, expected->mac_name, NULL, |
1581 | expected->alg, params, | |
1582 | expected->key, expected->key_len, | |
1583 | expected->input, expected->input_len, | |
1584 | got, got_len, &size)) | |
1585 | || !TEST_mem_eq(got, size, | |
1586 | expected->output, expected->output_len)) { | |
1587 | t->err = "EVP_Q_mac failed"; | |
1588 | goto err; | |
1589 | } | |
1590 | } | |
2bdb4af5 | 1591 | err: |
25446a66 RL |
1592 | while (params_n-- > params_n_allocstart) { |
1593 | OPENSSL_free(params[params_n].data); | |
1594 | } | |
865adf97 | 1595 | EVP_MAC_CTX_free(ctx); |
2bdb4af5 RL |
1596 | OPENSSL_free(got); |
1597 | return 1; | |
1598 | } | |
1599 | ||
1600 | static int mac_test_run(EVP_TEST *t) | |
1601 | { | |
1602 | MAC_DATA *expected = t->data; | |
1603 | ||
1604 | if (expected->mac != NULL) | |
1605 | return mac_test_run_mac(t); | |
1606 | return mac_test_run_pkey(t); | |
1607 | } | |
1608 | ||
6c5943c9 | 1609 | static const EVP_TEST_METHOD mac_test_method = { |
83251f39 DSH |
1610 | "MAC", |
1611 | mac_test_init, | |
1612 | mac_test_cleanup, | |
1613 | mac_test_parse, | |
1614 | mac_test_run | |
1615 | }; | |
5824cc29 | 1616 | |
c49e0b04 RS |
1617 | |
1618 | /** | |
5ccada09 SL |
1619 | ** PUBLIC KEY TESTS |
1620 | ** These are all very similar and share much common code. | |
1621 | **/ | |
5824cc29 | 1622 | |
6c5943c9 | 1623 | typedef struct pkey_data_st { |
5824cc29 DSH |
1624 | /* Context for this operation */ |
1625 | EVP_PKEY_CTX *ctx; | |
1626 | /* Key operation to perform */ | |
1627 | int (*keyop) (EVP_PKEY_CTX *ctx, | |
1628 | unsigned char *sig, size_t *siglen, | |
1629 | const unsigned char *tbs, size_t tbslen); | |
1630 | /* Input to MAC */ | |
1631 | unsigned char *input; | |
1632 | size_t input_len; | |
1633 | /* Expected output */ | |
1634 | unsigned char *output; | |
1635 | size_t output_len; | |
6c5943c9 | 1636 | } PKEY_DATA; |
5824cc29 DSH |
1637 | |
1638 | /* | |
1639 | * Perform public key operation setup: lookup key, allocated ctx and call | |
1640 | * the appropriate initialisation function | |
1641 | */ | |
6c5943c9 | 1642 | static int pkey_test_init(EVP_TEST *t, const char *name, |
5824cc29 DSH |
1643 | int use_public, |
1644 | int (*keyopinit) (EVP_PKEY_CTX *ctx), | |
c49e0b04 RS |
1645 | int (*keyop)(EVP_PKEY_CTX *ctx, |
1646 | unsigned char *sig, size_t *siglen, | |
1647 | const unsigned char *tbs, | |
1648 | size_t tbslen)) | |
5824cc29 | 1649 | { |
6c5943c9 | 1650 | PKEY_DATA *kdata; |
5824cc29 | 1651 | EVP_PKEY *pkey = NULL; |
7a6c9792 | 1652 | int rv = 0; |
6c5943c9 | 1653 | |
7a6c9792 | 1654 | if (use_public) |
6c5943c9 RS |
1655 | rv = find_key(&pkey, name, public_keys); |
1656 | if (rv == 0) | |
1657 | rv = find_key(&pkey, name, private_keys); | |
1658 | if (rv == 0 || pkey == NULL) { | |
5ccada09 | 1659 | TEST_info("skipping, key '%s' is disabled", name); |
7a6c9792 DSH |
1660 | t->skip = 1; |
1661 | return 1; | |
1662 | } | |
1663 | ||
c49e0b04 | 1664 | if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) { |
7a6c9792 | 1665 | EVP_PKEY_free(pkey); |
5824cc29 | 1666 | return 0; |
7a6c9792 | 1667 | } |
5824cc29 | 1668 | kdata->keyop = keyop; |
5ccada09 | 1669 | if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL))) { |
9e206ce5 P |
1670 | EVP_PKEY_free(pkey); |
1671 | OPENSSL_free(kdata); | |
5824cc29 | 1672 | return 0; |
9e206ce5 | 1673 | } |
5824cc29 | 1674 | if (keyopinit(kdata->ctx) <= 0) |
cce65266 | 1675 | t->err = "KEYOP_INIT_ERROR"; |
c49e0b04 | 1676 | t->data = kdata; |
5824cc29 DSH |
1677 | return 1; |
1678 | } | |
1679 | ||
6c5943c9 | 1680 | static void pkey_test_cleanup(EVP_TEST *t) |
5824cc29 | 1681 | { |
6c5943c9 | 1682 | PKEY_DATA *kdata = t->data; |
b548a1f1 RS |
1683 | |
1684 | OPENSSL_free(kdata->input); | |
1685 | OPENSSL_free(kdata->output); | |
c5ba2d99 | 1686 | EVP_PKEY_CTX_free(kdata->ctx); |
5824cc29 DSH |
1687 | } |
1688 | ||
6c5943c9 | 1689 | static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx, |
dfbdf4ab | 1690 | const char *value) |
4ddd5ace | 1691 | { |
8f586f5b | 1692 | int rv = 0; |
4ddd5ace DSH |
1693 | char *p, *tmpval; |
1694 | ||
6c5943c9 | 1695 | if (!TEST_ptr(tmpval = OPENSSL_strdup(value))) |
4ddd5ace DSH |
1696 | return 0; |
1697 | p = strchr(tmpval, ':'); | |
8f586f5b | 1698 | if (p != NULL) { |
c49e0b04 | 1699 | *p++ = '\0'; |
8f586f5b P |
1700 | rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p); |
1701 | } | |
cce65266 DSH |
1702 | if (rv == -2) { |
1703 | t->err = "PKEY_CTRL_INVALID"; | |
1704 | rv = 1; | |
1705 | } else if (p != NULL && rv <= 0) { | |
5ccada09 SL |
1706 | if (is_digest_disabled(p) || is_cipher_disabled(p)) { |
1707 | TEST_info("skipping, '%s' is disabled", p); | |
dfbdf4ab RL |
1708 | t->skip = 1; |
1709 | rv = 1; | |
cce65266 DSH |
1710 | } else { |
1711 | t->err = "PKEY_CTRL_ERROR"; | |
1712 | rv = 1; | |
dfbdf4ab RL |
1713 | } |
1714 | } | |
4ddd5ace DSH |
1715 | OPENSSL_free(tmpval); |
1716 | return rv > 0; | |
1717 | } | |
1718 | ||
6c5943c9 | 1719 | static int pkey_test_parse(EVP_TEST *t, |
5824cc29 DSH |
1720 | const char *keyword, const char *value) |
1721 | { | |
6c5943c9 | 1722 | PKEY_DATA *kdata = t->data; |
86885c28 | 1723 | if (strcmp(keyword, "Input") == 0) |
c49e0b04 | 1724 | return parse_bin(value, &kdata->input, &kdata->input_len); |
86885c28 | 1725 | if (strcmp(keyword, "Output") == 0) |
c49e0b04 | 1726 | return parse_bin(value, &kdata->output, &kdata->output_len); |
4ddd5ace | 1727 | if (strcmp(keyword, "Ctrl") == 0) |
dfbdf4ab | 1728 | return pkey_test_ctrl(t, kdata->ctx, value); |
5824cc29 DSH |
1729 | return 0; |
1730 | } | |
1731 | ||
6c5943c9 | 1732 | static int pkey_test_run(EVP_TEST *t) |
5824cc29 | 1733 | { |
e3d378bc AP |
1734 | PKEY_DATA *expected = t->data; |
1735 | unsigned char *got = NULL; | |
1736 | size_t got_len; | |
d7fcf1fe | 1737 | EVP_PKEY_CTX *copy = NULL; |
6c5943c9 | 1738 | |
e3d378bc AP |
1739 | if (expected->keyop(expected->ctx, NULL, &got_len, |
1740 | expected->input, expected->input_len) <= 0 | |
1741 | || !TEST_ptr(got = OPENSSL_malloc(got_len))) { | |
6c5943c9 | 1742 | t->err = "KEYOP_LENGTH_ERROR"; |
5824cc29 | 1743 | goto err; |
6c5943c9 | 1744 | } |
e3d378bc AP |
1745 | if (expected->keyop(expected->ctx, got, &got_len, |
1746 | expected->input, expected->input_len) <= 0) { | |
6c5943c9 | 1747 | t->err = "KEYOP_ERROR"; |
5824cc29 | 1748 | goto err; |
6c5943c9 | 1749 | } |
4cceb185 P |
1750 | if (!memory_err_compare(t, "KEYOP_MISMATCH", |
1751 | expected->output, expected->output_len, | |
1752 | got, got_len)) | |
5824cc29 | 1753 | goto err; |
4cceb185 | 1754 | |
6c5943c9 | 1755 | t->err = NULL; |
d7fcf1fe DB |
1756 | OPENSSL_free(got); |
1757 | got = NULL; | |
1758 | ||
1759 | /* Repeat the test on a copy. */ | |
1760 | if (!TEST_ptr(copy = EVP_PKEY_CTX_dup(expected->ctx))) { | |
1761 | t->err = "INTERNAL_ERROR"; | |
1762 | goto err; | |
1763 | } | |
1764 | if (expected->keyop(copy, NULL, &got_len, expected->input, | |
1765 | expected->input_len) <= 0 | |
1766 | || !TEST_ptr(got = OPENSSL_malloc(got_len))) { | |
1767 | t->err = "KEYOP_LENGTH_ERROR"; | |
1768 | goto err; | |
1769 | } | |
1770 | if (expected->keyop(copy, got, &got_len, expected->input, | |
1771 | expected->input_len) <= 0) { | |
1772 | t->err = "KEYOP_ERROR"; | |
1773 | goto err; | |
1774 | } | |
1775 | if (!memory_err_compare(t, "KEYOP_MISMATCH", | |
1776 | expected->output, expected->output_len, | |
1777 | got, got_len)) | |
1778 | goto err; | |
1779 | ||
5824cc29 | 1780 | err: |
e3d378bc | 1781 | OPENSSL_free(got); |
d7fcf1fe | 1782 | EVP_PKEY_CTX_free(copy); |
5824cc29 DSH |
1783 | return 1; |
1784 | } | |
1785 | ||
6c5943c9 | 1786 | static int sign_test_init(EVP_TEST *t, const char *name) |
5824cc29 DSH |
1787 | { |
1788 | return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign); | |
1789 | } | |
1790 | ||
6c5943c9 | 1791 | static const EVP_TEST_METHOD psign_test_method = { |
5824cc29 DSH |
1792 | "Sign", |
1793 | sign_test_init, | |
1794 | pkey_test_cleanup, | |
1795 | pkey_test_parse, | |
1796 | pkey_test_run | |
1797 | }; | |
1798 | ||
6c5943c9 | 1799 | static int verify_recover_test_init(EVP_TEST *t, const char *name) |
5824cc29 DSH |
1800 | { |
1801 | return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init, | |
1802 | EVP_PKEY_verify_recover); | |
1803 | } | |
1804 | ||
6c5943c9 | 1805 | static const EVP_TEST_METHOD pverify_recover_test_method = { |
5824cc29 DSH |
1806 | "VerifyRecover", |
1807 | verify_recover_test_init, | |
1808 | pkey_test_cleanup, | |
1809 | pkey_test_parse, | |
1810 | pkey_test_run | |
1811 | }; | |
1812 | ||
6c5943c9 | 1813 | static int decrypt_test_init(EVP_TEST *t, const char *name) |
5824cc29 DSH |
1814 | { |
1815 | return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init, | |
1816 | EVP_PKEY_decrypt); | |
1817 | } | |
1818 | ||
6c5943c9 | 1819 | static const EVP_TEST_METHOD pdecrypt_test_method = { |
5824cc29 DSH |
1820 | "Decrypt", |
1821 | decrypt_test_init, | |
1822 | pkey_test_cleanup, | |
1823 | pkey_test_parse, | |
1824 | pkey_test_run | |
1825 | }; | |
1826 | ||
6c5943c9 | 1827 | static int verify_test_init(EVP_TEST *t, const char *name) |
5824cc29 DSH |
1828 | { |
1829 | return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0); | |
1830 | } | |
1831 | ||
6c5943c9 | 1832 | static int verify_test_run(EVP_TEST *t) |
5824cc29 | 1833 | { |
6c5943c9 RS |
1834 | PKEY_DATA *kdata = t->data; |
1835 | ||
5824cc29 DSH |
1836 | if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len, |
1837 | kdata->input, kdata->input_len) <= 0) | |
1838 | t->err = "VERIFY_ERROR"; | |
1839 | return 1; | |
1840 | } | |
1841 | ||
6c5943c9 | 1842 | static const EVP_TEST_METHOD pverify_test_method = { |
5824cc29 DSH |
1843 | "Verify", |
1844 | verify_test_init, | |
1845 | pkey_test_cleanup, | |
1846 | pkey_test_parse, | |
1847 | verify_test_run | |
1848 | }; | |
3b53e18a | 1849 | |
6c5943c9 | 1850 | static int pderive_test_init(EVP_TEST *t, const char *name) |
d4ad48d7 DSH |
1851 | { |
1852 | return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0); | |
1853 | } | |
1854 | ||
6c5943c9 | 1855 | static int pderive_test_parse(EVP_TEST *t, |
d4ad48d7 DSH |
1856 | const char *keyword, const char *value) |
1857 | { | |
6c5943c9 | 1858 | PKEY_DATA *kdata = t->data; |
46eee710 | 1859 | int validate = 0; |
d4ad48d7 | 1860 | |
46eee710 SL |
1861 | if (strcmp(keyword, "PeerKeyValidate") == 0) |
1862 | validate = 1; | |
1863 | ||
1864 | if (validate || strcmp(keyword, "PeerKey") == 0) { | |
d4ad48d7 | 1865 | EVP_PKEY *peer; |
6c5943c9 | 1866 | if (find_key(&peer, value, public_keys) == 0) |
f42c225d | 1867 | return -1; |
46eee710 | 1868 | if (EVP_PKEY_derive_set_peer_ex(kdata->ctx, peer, validate) <= 0) { |
0645110e SL |
1869 | t->err = "DERIVE_SET_PEER_ERROR"; |
1870 | return 1; | |
1871 | } | |
1872 | t->err = NULL; | |
d4ad48d7 DSH |
1873 | return 1; |
1874 | } | |
1875 | if (strcmp(keyword, "SharedSecret") == 0) | |
c49e0b04 | 1876 | return parse_bin(value, &kdata->output, &kdata->output_len); |
4ddd5ace | 1877 | if (strcmp(keyword, "Ctrl") == 0) |
dfbdf4ab | 1878 | return pkey_test_ctrl(t, kdata->ctx, value); |
d4ad48d7 DSH |
1879 | return 0; |
1880 | } | |
1881 | ||
6c5943c9 | 1882 | static int pderive_test_run(EVP_TEST *t) |
d4ad48d7 | 1883 | { |
21a0d9f3 | 1884 | EVP_PKEY_CTX *dctx = NULL; |
e3d378bc AP |
1885 | PKEY_DATA *expected = t->data; |
1886 | unsigned char *got = NULL; | |
1887 | size_t got_len; | |
d4ad48d7 | 1888 | |
21a0d9f3 | 1889 | if (!TEST_ptr(dctx = EVP_PKEY_CTX_dup(expected->ctx))) { |
1890 | t->err = "DERIVE_ERROR"; | |
1891 | goto err; | |
1892 | } | |
1893 | ||
1894 | if (EVP_PKEY_derive(dctx, NULL, &got_len) <= 0) { | |
9b82c8b1 DSH |
1895 | t->err = "DERIVE_ERROR"; |
1896 | goto err; | |
1897 | } | |
e3d378bc | 1898 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { |
6c5943c9 | 1899 | t->err = "DERIVE_ERROR"; |
d4ad48d7 | 1900 | goto err; |
6c5943c9 | 1901 | } |
21a0d9f3 | 1902 | if (EVP_PKEY_derive(dctx, got, &got_len) <= 0) { |
6c5943c9 | 1903 | t->err = "DERIVE_ERROR"; |
d4ad48d7 | 1904 | goto err; |
6c5943c9 | 1905 | } |
4cceb185 P |
1906 | if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH", |
1907 | expected->output, expected->output_len, | |
1908 | got, got_len)) | |
d4ad48d7 | 1909 | goto err; |
6c5943c9 RS |
1910 | |
1911 | t->err = NULL; | |
d4ad48d7 | 1912 | err: |
e3d378bc | 1913 | OPENSSL_free(got); |
21a0d9f3 | 1914 | EVP_PKEY_CTX_free(dctx); |
d4ad48d7 DSH |
1915 | return 1; |
1916 | } | |
1917 | ||
6c5943c9 | 1918 | static const EVP_TEST_METHOD pderive_test_method = { |
d4ad48d7 DSH |
1919 | "Derive", |
1920 | pderive_test_init, | |
1921 | pkey_test_cleanup, | |
1922 | pderive_test_parse, | |
1923 | pderive_test_run | |
1924 | }; | |
1925 | ||
3b53e18a | 1926 | |
c49e0b04 | 1927 | /** |
5ccada09 SL |
1928 | ** PBE TESTS |
1929 | **/ | |
c49e0b04 RS |
1930 | |
1931 | typedef enum pbe_type_enum { | |
1932 | PBE_TYPE_INVALID = 0, | |
1933 | PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12 | |
1934 | } PBE_TYPE; | |
3b53e18a | 1935 | |
6c5943c9 | 1936 | typedef struct pbe_data_st { |
c49e0b04 | 1937 | PBE_TYPE pbe_type; |
6c5943c9 | 1938 | /* scrypt parameters */ |
3b53e18a | 1939 | uint64_t N, r, p, maxmem; |
6c5943c9 | 1940 | /* PKCS#12 parameters */ |
351fe214 DSH |
1941 | int id, iter; |
1942 | const EVP_MD *md; | |
6c5943c9 | 1943 | /* password */ |
3b53e18a DSH |
1944 | unsigned char *pass; |
1945 | size_t pass_len; | |
6c5943c9 | 1946 | /* salt */ |
3b53e18a DSH |
1947 | unsigned char *salt; |
1948 | size_t salt_len; | |
6c5943c9 | 1949 | /* Expected output */ |
3b53e18a DSH |
1950 | unsigned char *key; |
1951 | size_t key_len; | |
6c5943c9 | 1952 | } PBE_DATA; |
3b53e18a | 1953 | |
b0809bc8 | 1954 | #ifndef OPENSSL_NO_SCRYPT |
5ccada09 | 1955 | /* Parse unsigned decimal 64 bit integer value */ |
c49e0b04 RS |
1956 | static int parse_uint64(const char *value, uint64_t *pr) |
1957 | { | |
1958 | const char *p = value; | |
1959 | ||
1960 | if (!TEST_true(*p)) { | |
1961 | TEST_info("Invalid empty integer value"); | |
1962 | return -1; | |
1963 | } | |
1964 | for (*pr = 0; *p; ) { | |
1965 | if (*pr > UINT64_MAX / 10) { | |
1966 | TEST_error("Integer overflow in string %s", value); | |
1967 | return -1; | |
1968 | } | |
1969 | *pr *= 10; | |
00dfbaad | 1970 | if (!TEST_true(isdigit((unsigned char)*p))) { |
c49e0b04 RS |
1971 | TEST_error("Invalid character in string %s", value); |
1972 | return -1; | |
1973 | } | |
1974 | *pr += *p - '0'; | |
1975 | p++; | |
1976 | } | |
1977 | return 1; | |
1978 | } | |
1979 | ||
6c5943c9 | 1980 | static int scrypt_test_parse(EVP_TEST *t, |
3b53e18a DSH |
1981 | const char *keyword, const char *value) |
1982 | { | |
6c5943c9 | 1983 | PBE_DATA *pdata = t->data; |
351fe214 | 1984 | |
3b53e18a | 1985 | if (strcmp(keyword, "N") == 0) |
c49e0b04 | 1986 | return parse_uint64(value, &pdata->N); |
3b53e18a | 1987 | if (strcmp(keyword, "p") == 0) |
c49e0b04 | 1988 | return parse_uint64(value, &pdata->p); |
3b53e18a | 1989 | if (strcmp(keyword, "r") == 0) |
c49e0b04 | 1990 | return parse_uint64(value, &pdata->r); |
3b53e18a | 1991 | if (strcmp(keyword, "maxmem") == 0) |
c49e0b04 | 1992 | return parse_uint64(value, &pdata->maxmem); |
3b53e18a DSH |
1993 | return 0; |
1994 | } | |
b0809bc8 | 1995 | #endif |
3b53e18a | 1996 | |
6c5943c9 | 1997 | static int pbkdf2_test_parse(EVP_TEST *t, |
351fe214 | 1998 | const char *keyword, const char *value) |
3b53e18a | 1999 | { |
6c5943c9 | 2000 | PBE_DATA *pdata = t->data; |
351fe214 DSH |
2001 | |
2002 | if (strcmp(keyword, "iter") == 0) { | |
2003 | pdata->iter = atoi(value); | |
2004 | if (pdata->iter <= 0) | |
c49e0b04 | 2005 | return -1; |
351fe214 DSH |
2006 | return 1; |
2007 | } | |
2008 | if (strcmp(keyword, "MD") == 0) { | |
2009 | pdata->md = EVP_get_digestbyname(value); | |
2010 | if (pdata->md == NULL) | |
c49e0b04 | 2011 | return -1; |
351fe214 DSH |
2012 | return 1; |
2013 | } | |
2014 | return 0; | |
2015 | } | |
2016 | ||
6c5943c9 | 2017 | static int pkcs12_test_parse(EVP_TEST *t, |
351fe214 DSH |
2018 | const char *keyword, const char *value) |
2019 | { | |
6c5943c9 | 2020 | PBE_DATA *pdata = t->data; |
351fe214 DSH |
2021 | |
2022 | if (strcmp(keyword, "id") == 0) { | |
2023 | pdata->id = atoi(value); | |
2024 | if (pdata->id <= 0) | |
c49e0b04 | 2025 | return -1; |
351fe214 DSH |
2026 | return 1; |
2027 | } | |
2028 | return pbkdf2_test_parse(t, keyword, value); | |
3b53e18a DSH |
2029 | } |
2030 | ||
6c5943c9 | 2031 | static int pbe_test_init(EVP_TEST *t, const char *alg) |
3b53e18a | 2032 | { |
6c5943c9 | 2033 | PBE_DATA *pdat; |
c49e0b04 | 2034 | PBE_TYPE pbe_type = PBE_TYPE_INVALID; |
351fe214 | 2035 | |
5ccada09 SL |
2036 | if (is_kdf_disabled(alg)) { |
2037 | TEST_info("skipping, '%s' is disabled", alg); | |
83bd31da MC |
2038 | t->skip = 1; |
2039 | return 1; | |
5ccada09 SL |
2040 | } |
2041 | if (strcmp(alg, "scrypt") == 0) { | |
2042 | pbe_type = PBE_TYPE_SCRYPT; | |
83bd31da | 2043 | } else if (strcmp(alg, "pbkdf2") == 0) { |
351fe214 | 2044 | pbe_type = PBE_TYPE_PBKDF2; |
83bd31da | 2045 | } else if (strcmp(alg, "pkcs12") == 0) { |
351fe214 | 2046 | pbe_type = PBE_TYPE_PKCS12; |
83bd31da | 2047 | } else { |
6c5943c9 | 2048 | TEST_error("Unknown pbe algorithm %s", alg); |
1f25fd16 | 2049 | return 0; |
83bd31da | 2050 | } |
1f25fd16 P |
2051 | if (!TEST_ptr(pdat = OPENSSL_zalloc(sizeof(*pdat)))) |
2052 | return 0; | |
3b53e18a | 2053 | pdat->pbe_type = pbe_type; |
3b53e18a DSH |
2054 | t->data = pdat; |
2055 | return 1; | |
2056 | } | |
2057 | ||
6c5943c9 | 2058 | static void pbe_test_cleanup(EVP_TEST *t) |
3b53e18a | 2059 | { |
6c5943c9 RS |
2060 | PBE_DATA *pdat = t->data; |
2061 | ||
2062 | OPENSSL_free(pdat->pass); | |
2063 | OPENSSL_free(pdat->salt); | |
2064 | OPENSSL_free(pdat->key); | |
3b53e18a DSH |
2065 | } |
2066 | ||
6c5943c9 RS |
2067 | static int pbe_test_parse(EVP_TEST *t, |
2068 | const char *keyword, const char *value) | |
3b53e18a | 2069 | { |
6c5943c9 | 2070 | PBE_DATA *pdata = t->data; |
351fe214 | 2071 | |
3b53e18a | 2072 | if (strcmp(keyword, "Password") == 0) |
c49e0b04 | 2073 | return parse_bin(value, &pdata->pass, &pdata->pass_len); |
3b53e18a | 2074 | if (strcmp(keyword, "Salt") == 0) |
c49e0b04 | 2075 | return parse_bin(value, &pdata->salt, &pdata->salt_len); |
3b53e18a | 2076 | if (strcmp(keyword, "Key") == 0) |
c49e0b04 | 2077 | return parse_bin(value, &pdata->key, &pdata->key_len); |
b0809bc8 | 2078 | if (pdata->pbe_type == PBE_TYPE_PBKDF2) |
351fe214 DSH |
2079 | return pbkdf2_test_parse(t, keyword, value); |
2080 | else if (pdata->pbe_type == PBE_TYPE_PKCS12) | |
2081 | return pkcs12_test_parse(t, keyword, value); | |
b0809bc8 RS |
2082 | #ifndef OPENSSL_NO_SCRYPT |
2083 | else if (pdata->pbe_type == PBE_TYPE_SCRYPT) | |
2084 | return scrypt_test_parse(t, keyword, value); | |
2085 | #endif | |
3b53e18a DSH |
2086 | return 0; |
2087 | } | |
2088 | ||
6c5943c9 | 2089 | static int pbe_test_run(EVP_TEST *t) |
3b53e18a | 2090 | { |
e3d378bc | 2091 | PBE_DATA *expected = t->data; |
351fe214 | 2092 | unsigned char *key; |
5ccada09 | 2093 | EVP_MD *fetched_digest = NULL; |
b4250010 | 2094 | OSSL_LIB_CTX *save_libctx; |
5ccada09 | 2095 | |
b4250010 | 2096 | save_libctx = OSSL_LIB_CTX_set0_default(libctx); |
351fe214 | 2097 | |
e3d378bc | 2098 | if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) { |
6c5943c9 | 2099 | t->err = "INTERNAL_ERROR"; |
351fe214 | 2100 | goto err; |
6c5943c9 | 2101 | } |
e3d378bc AP |
2102 | if (expected->pbe_type == PBE_TYPE_PBKDF2) { |
2103 | if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len, | |
2104 | expected->salt, expected->salt_len, | |
2105 | expected->iter, expected->md, | |
2106 | expected->key_len, key) == 0) { | |
6c5943c9 | 2107 | t->err = "PBKDF2_ERROR"; |
351fe214 | 2108 | goto err; |
6c5943c9 | 2109 | } |
b0809bc8 | 2110 | #ifndef OPENSSL_NO_SCRYPT |
e3d378bc AP |
2111 | } else if (expected->pbe_type == PBE_TYPE_SCRYPT) { |
2112 | if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len, | |
5ccada09 SL |
2113 | expected->salt, expected->salt_len, |
2114 | expected->N, expected->r, expected->p, | |
2115 | expected->maxmem, key, expected->key_len) == 0) { | |
6c5943c9 | 2116 | t->err = "SCRYPT_ERROR"; |
351fe214 | 2117 | goto err; |
6c5943c9 | 2118 | } |
b0809bc8 | 2119 | #endif |
e3d378bc | 2120 | } else if (expected->pbe_type == PBE_TYPE_PKCS12) { |
ed576acd TM |
2121 | fetched_digest = EVP_MD_fetch(libctx, EVP_MD_get0_name(expected->md), |
2122 | NULL); | |
5ccada09 SL |
2123 | if (fetched_digest == NULL) { |
2124 | t->err = "PKCS12_ERROR"; | |
2125 | goto err; | |
2126 | } | |
e3d378bc AP |
2127 | if (PKCS12_key_gen_uni(expected->pass, expected->pass_len, |
2128 | expected->salt, expected->salt_len, | |
2129 | expected->id, expected->iter, expected->key_len, | |
5ccada09 | 2130 | key, fetched_digest) == 0) { |
6c5943c9 | 2131 | t->err = "PKCS12_ERROR"; |
351fe214 | 2132 | goto err; |
6c5943c9 | 2133 | } |
351fe214 | 2134 | } |
4cceb185 P |
2135 | if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len, |
2136 | key, expected->key_len)) | |
351fe214 | 2137 | goto err; |
4cceb185 | 2138 | |
6c5943c9 RS |
2139 | t->err = NULL; |
2140 | err: | |
5ccada09 | 2141 | EVP_MD_free(fetched_digest); |
351fe214 | 2142 | OPENSSL_free(key); |
b4250010 | 2143 | OSSL_LIB_CTX_set0_default(save_libctx); |
351fe214 | 2144 | return 1; |
3b53e18a DSH |
2145 | } |
2146 | ||
6c5943c9 | 2147 | static const EVP_TEST_METHOD pbe_test_method = { |
3b53e18a DSH |
2148 | "PBE", |
2149 | pbe_test_init, | |
2150 | pbe_test_cleanup, | |
2151 | pbe_test_parse, | |
2152 | pbe_test_run | |
2153 | }; | |
3cdd1e94 | 2154 | |
c49e0b04 RS |
2155 | |
2156 | /** | |
5ccada09 SL |
2157 | ** BASE64 TESTS |
2158 | **/ | |
3cdd1e94 EK |
2159 | |
2160 | typedef enum { | |
2161 | BASE64_CANONICAL_ENCODING = 0, | |
2162 | BASE64_VALID_ENCODING = 1, | |
2163 | BASE64_INVALID_ENCODING = 2 | |
2164 | } base64_encoding_type; | |
2165 | ||
6c5943c9 | 2166 | typedef struct encode_data_st { |
3cdd1e94 EK |
2167 | /* Input to encoding */ |
2168 | unsigned char *input; | |
2169 | size_t input_len; | |
2170 | /* Expected output */ | |
2171 | unsigned char *output; | |
2172 | size_t output_len; | |
2173 | base64_encoding_type encoding; | |
6c5943c9 | 2174 | } ENCODE_DATA; |
3cdd1e94 | 2175 | |
6c5943c9 | 2176 | static int encode_test_init(EVP_TEST *t, const char *encoding) |
3cdd1e94 | 2177 | { |
c49e0b04 | 2178 | ENCODE_DATA *edata; |
3cdd1e94 | 2179 | |
c49e0b04 RS |
2180 | if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata)))) |
2181 | return 0; | |
3cdd1e94 EK |
2182 | if (strcmp(encoding, "canonical") == 0) { |
2183 | edata->encoding = BASE64_CANONICAL_ENCODING; | |
2184 | } else if (strcmp(encoding, "valid") == 0) { | |
2185 | edata->encoding = BASE64_VALID_ENCODING; | |
2186 | } else if (strcmp(encoding, "invalid") == 0) { | |
2187 | edata->encoding = BASE64_INVALID_ENCODING; | |
c49e0b04 | 2188 | if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR"))) |
760e2d60 | 2189 | goto err; |
3cdd1e94 | 2190 | } else { |
c49e0b04 RS |
2191 | TEST_error("Bad encoding: %s." |
2192 | " Should be one of {canonical, valid, invalid}", | |
2193 | encoding); | |
760e2d60 | 2194 | goto err; |
3cdd1e94 EK |
2195 | } |
2196 | t->data = edata; | |
2197 | return 1; | |
760e2d60 F |
2198 | err: |
2199 | OPENSSL_free(edata); | |
2200 | return 0; | |
3cdd1e94 EK |
2201 | } |
2202 | ||
6c5943c9 | 2203 | static void encode_test_cleanup(EVP_TEST *t) |
3cdd1e94 | 2204 | { |
6c5943c9 RS |
2205 | ENCODE_DATA *edata = t->data; |
2206 | ||
2207 | OPENSSL_free(edata->input); | |
2208 | OPENSSL_free(edata->output); | |
3cdd1e94 EK |
2209 | memset(edata, 0, sizeof(*edata)); |
2210 | } | |
2211 | ||
6c5943c9 | 2212 | static int encode_test_parse(EVP_TEST *t, |
3cdd1e94 EK |
2213 | const char *keyword, const char *value) |
2214 | { | |
6c5943c9 | 2215 | ENCODE_DATA *edata = t->data; |
c49e0b04 | 2216 | |
3cdd1e94 | 2217 | if (strcmp(keyword, "Input") == 0) |
c49e0b04 | 2218 | return parse_bin(value, &edata->input, &edata->input_len); |
3cdd1e94 | 2219 | if (strcmp(keyword, "Output") == 0) |
c49e0b04 | 2220 | return parse_bin(value, &edata->output, &edata->output_len); |
3cdd1e94 EK |
2221 | return 0; |
2222 | } | |
2223 | ||
6c5943c9 | 2224 | static int encode_test_run(EVP_TEST *t) |
3cdd1e94 | 2225 | { |
e3d378bc | 2226 | ENCODE_DATA *expected = t->data; |
3cdd1e94 EK |
2227 | unsigned char *encode_out = NULL, *decode_out = NULL; |
2228 | int output_len, chunk_len; | |
760e2d60 | 2229 | EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL; |
254b26af | 2230 | |
6c5943c9 RS |
2231 | if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) { |
2232 | t->err = "INTERNAL_ERROR"; | |
254b26af | 2233 | goto err; |
6c5943c9 | 2234 | } |
3cdd1e94 | 2235 | |
e3d378bc | 2236 | if (expected->encoding == BASE64_CANONICAL_ENCODING) { |
6c5943c9 RS |
2237 | |
2238 | if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new()) | |
2239 | || !TEST_ptr(encode_out = | |
e3d378bc | 2240 | OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len)))) |
3cdd1e94 EK |
2241 | goto err; |
2242 | ||
254b26af | 2243 | EVP_EncodeInit(encode_ctx); |
760e2d60 F |
2244 | if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len, |
2245 | expected->input, expected->input_len))) | |
2246 | goto err; | |
2247 | ||
3cdd1e94 EK |
2248 | output_len = chunk_len; |
2249 | ||
254b26af | 2250 | EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len); |
3cdd1e94 EK |
2251 | output_len += chunk_len; |
2252 | ||
4cceb185 P |
2253 | if (!memory_err_compare(t, "BAD_ENCODING", |
2254 | expected->output, expected->output_len, | |
2255 | encode_out, output_len)) | |
3cdd1e94 | 2256 | goto err; |
3cdd1e94 EK |
2257 | } |
2258 | ||
6c5943c9 | 2259 | if (!TEST_ptr(decode_out = |
e3d378bc | 2260 | OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len)))) |
3cdd1e94 EK |
2261 | goto err; |
2262 | ||
254b26af | 2263 | EVP_DecodeInit(decode_ctx); |
e3d378bc AP |
2264 | if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output, |
2265 | expected->output_len) < 0) { | |
6c5943c9 | 2266 | t->err = "DECODE_ERROR"; |
3cdd1e94 EK |
2267 | goto err; |
2268 | } | |
2269 | output_len = chunk_len; | |
2270 | ||
254b26af | 2271 | if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) { |
6c5943c9 | 2272 | t->err = "DECODE_ERROR"; |
3cdd1e94 EK |
2273 | goto err; |
2274 | } | |
2275 | output_len += chunk_len; | |
2276 | ||
e3d378bc | 2277 | if (expected->encoding != BASE64_INVALID_ENCODING |
4cceb185 P |
2278 | && !memory_err_compare(t, "BAD_DECODING", |
2279 | expected->input, expected->input_len, | |
2280 | decode_out, output_len)) { | |
6c5943c9 | 2281 | t->err = "BAD_DECODING"; |
3cdd1e94 EK |
2282 | goto err; |
2283 | } | |
2284 | ||
6c5943c9 | 2285 | t->err = NULL; |
3cdd1e94 | 2286 | err: |
3cdd1e94 EK |
2287 | OPENSSL_free(encode_out); |
2288 | OPENSSL_free(decode_out); | |
254b26af | 2289 | EVP_ENCODE_CTX_free(decode_ctx); |
760e2d60 | 2290 | EVP_ENCODE_CTX_free(encode_ctx); |
3cdd1e94 EK |
2291 | return 1; |
2292 | } | |
2293 | ||
6c5943c9 | 2294 | static const EVP_TEST_METHOD encode_test_method = { |
3cdd1e94 EK |
2295 | "Encoding", |
2296 | encode_test_init, | |
2297 | encode_test_cleanup, | |
2298 | encode_test_parse, | |
2299 | encode_test_run, | |
2300 | }; | |
44a284d2 | 2301 | |
5a285add | 2302 | |
c9ed9307 | 2303 | /** |
5ccada09 SL |
2304 | ** RAND TESTS |
2305 | **/ | |
c9ed9307 P |
2306 | #define MAX_RAND_REPEATS 15 |
2307 | ||
2308 | typedef struct rand_data_pass_st { | |
2309 | unsigned char *entropy; | |
2310 | unsigned char *reseed_entropy; | |
2311 | unsigned char *nonce; | |
2312 | unsigned char *pers; | |
2313 | unsigned char *reseed_addin; | |
2314 | unsigned char *addinA; | |
2315 | unsigned char *addinB; | |
2316 | unsigned char *pr_entropyA; | |
2317 | unsigned char *pr_entropyB; | |
2318 | unsigned char *output; | |
2319 | size_t entropy_len, nonce_len, pers_len, addinA_len, addinB_len, | |
2320 | pr_entropyA_len, pr_entropyB_len, output_len, reseed_entropy_len, | |
2321 | reseed_addin_len; | |
2322 | } RAND_DATA_PASS; | |
2323 | ||
2324 | typedef struct rand_data_st { | |
2325 | /* Context for this operation */ | |
2326 | EVP_RAND_CTX *ctx; | |
2327 | EVP_RAND_CTX *parent; | |
2328 | int n; | |
2329 | int prediction_resistance; | |
2330 | int use_df; | |
2331 | unsigned int generate_bits; | |
2332 | char *cipher; | |
2333 | char *digest; | |
2334 | ||
2335 | /* Expected output */ | |
2336 | RAND_DATA_PASS data[MAX_RAND_REPEATS]; | |
2337 | } RAND_DATA; | |
2338 | ||
2339 | static int rand_test_init(EVP_TEST *t, const char *name) | |
2340 | { | |
2341 | RAND_DATA *rdata; | |
2342 | EVP_RAND *rand; | |
2343 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
2344 | unsigned int strength = 256; | |
2345 | ||
2346 | if (!TEST_ptr(rdata = OPENSSL_zalloc(sizeof(*rdata)))) | |
2347 | return 0; | |
2348 | ||
bfa6aaab MC |
2349 | /* TEST-RAND is available in the FIPS provider but not with "fips=yes" */ |
2350 | rand = EVP_RAND_fetch(libctx, "TEST-RAND", "-fips"); | |
c9ed9307 P |
2351 | if (rand == NULL) |
2352 | goto err; | |
2353 | rdata->parent = EVP_RAND_CTX_new(rand, NULL); | |
2354 | EVP_RAND_free(rand); | |
2355 | if (rdata->parent == NULL) | |
2356 | goto err; | |
2357 | ||
2358 | *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength); | |
e494fac7 | 2359 | if (!EVP_RAND_CTX_set_params(rdata->parent, params)) |
c9ed9307 P |
2360 | goto err; |
2361 | ||
5ccada09 | 2362 | rand = EVP_RAND_fetch(libctx, name, NULL); |
c9ed9307 P |
2363 | if (rand == NULL) |
2364 | goto err; | |
2365 | rdata->ctx = EVP_RAND_CTX_new(rand, rdata->parent); | |
2366 | EVP_RAND_free(rand); | |
2367 | if (rdata->ctx == NULL) | |
2368 | goto err; | |
2369 | ||
2370 | rdata->n = -1; | |
2371 | t->data = rdata; | |
2372 | return 1; | |
2373 | err: | |
2374 | EVP_RAND_CTX_free(rdata->parent); | |
2375 | OPENSSL_free(rdata); | |
2376 | return 0; | |
2377 | } | |
2378 | ||
2379 | static void rand_test_cleanup(EVP_TEST *t) | |
2380 | { | |
2381 | RAND_DATA *rdata = t->data; | |
2382 | int i; | |
2383 | ||
2384 | OPENSSL_free(rdata->cipher); | |
2385 | OPENSSL_free(rdata->digest); | |
2386 | ||
2387 | for (i = 0; i <= rdata->n; i++) { | |
2388 | OPENSSL_free(rdata->data[i].entropy); | |
2389 | OPENSSL_free(rdata->data[i].reseed_entropy); | |
2390 | OPENSSL_free(rdata->data[i].nonce); | |
2391 | OPENSSL_free(rdata->data[i].pers); | |
2392 | OPENSSL_free(rdata->data[i].reseed_addin); | |
2393 | OPENSSL_free(rdata->data[i].addinA); | |
2394 | OPENSSL_free(rdata->data[i].addinB); | |
2395 | OPENSSL_free(rdata->data[i].pr_entropyA); | |
2396 | OPENSSL_free(rdata->data[i].pr_entropyB); | |
2397 | OPENSSL_free(rdata->data[i].output); | |
2398 | } | |
2399 | EVP_RAND_CTX_free(rdata->ctx); | |
2400 | EVP_RAND_CTX_free(rdata->parent); | |
2401 | } | |
2402 | ||
2403 | static int rand_test_parse(EVP_TEST *t, | |
2404 | const char *keyword, const char *value) | |
2405 | { | |
2406 | RAND_DATA *rdata = t->data; | |
2407 | RAND_DATA_PASS *item; | |
2408 | const char *p; | |
2409 | int n; | |
2410 | ||
2411 | if ((p = strchr(keyword, '.')) != NULL) { | |
2412 | n = atoi(++p); | |
2413 | if (n >= MAX_RAND_REPEATS) | |
2414 | return 0; | |
2415 | if (n > rdata->n) | |
2416 | rdata->n = n; | |
2417 | item = rdata->data + n; | |
2ff286c2 | 2418 | if (HAS_PREFIX(keyword, "Entropy.")) |
c9ed9307 | 2419 | return parse_bin(value, &item->entropy, &item->entropy_len); |
2ff286c2 | 2420 | if (HAS_PREFIX(keyword, "ReseedEntropy.")) |
c9ed9307 P |
2421 | return parse_bin(value, &item->reseed_entropy, |
2422 | &item->reseed_entropy_len); | |
2ff286c2 | 2423 | if (HAS_PREFIX(keyword, "Nonce.")) |
c9ed9307 | 2424 | return parse_bin(value, &item->nonce, &item->nonce_len); |
2ff286c2 | 2425 | if (HAS_PREFIX(keyword, "PersonalisationString.")) |
c9ed9307 | 2426 | return parse_bin(value, &item->pers, &item->pers_len); |
2ff286c2 | 2427 | if (HAS_PREFIX(keyword, "ReseedAdditionalInput.")) |
c9ed9307 P |
2428 | return parse_bin(value, &item->reseed_addin, |
2429 | &item->reseed_addin_len); | |
2ff286c2 | 2430 | if (HAS_PREFIX(keyword, "AdditionalInputA.")) |
c9ed9307 | 2431 | return parse_bin(value, &item->addinA, &item->addinA_len); |
2ff286c2 | 2432 | if (HAS_PREFIX(keyword, "AdditionalInputB.")) |
c9ed9307 | 2433 | return parse_bin(value, &item->addinB, &item->addinB_len); |
2ff286c2 | 2434 | if (HAS_PREFIX(keyword, "EntropyPredictionResistanceA.")) |
c9ed9307 | 2435 | return parse_bin(value, &item->pr_entropyA, &item->pr_entropyA_len); |
2ff286c2 | 2436 | if (HAS_PREFIX(keyword, "EntropyPredictionResistanceB.")) |
c9ed9307 | 2437 | return parse_bin(value, &item->pr_entropyB, &item->pr_entropyB_len); |
2ff286c2 | 2438 | if (HAS_PREFIX(keyword, "Output.")) |
c9ed9307 P |
2439 | return parse_bin(value, &item->output, &item->output_len); |
2440 | } else { | |
2441 | if (strcmp(keyword, "Cipher") == 0) | |
2442 | return TEST_ptr(rdata->cipher = OPENSSL_strdup(value)); | |
2443 | if (strcmp(keyword, "Digest") == 0) | |
2444 | return TEST_ptr(rdata->digest = OPENSSL_strdup(value)); | |
2445 | if (strcmp(keyword, "DerivationFunction") == 0) { | |
2446 | rdata->use_df = atoi(value) != 0; | |
2447 | return 1; | |
2448 | } | |
2449 | if (strcmp(keyword, "GenerateBits") == 0) { | |
2450 | if ((n = atoi(value)) <= 0 || n % 8 != 0) | |
2451 | return 0; | |
2452 | rdata->generate_bits = (unsigned int)n; | |
2453 | return 1; | |
2454 | } | |
2455 | if (strcmp(keyword, "PredictionResistance") == 0) { | |
2456 | rdata->prediction_resistance = atoi(value) != 0; | |
2457 | return 1; | |
2458 | } | |
2459 | } | |
2460 | return 0; | |
2461 | } | |
2462 | ||
2463 | static int rand_test_run(EVP_TEST *t) | |
2464 | { | |
2465 | RAND_DATA *expected = t->data; | |
2466 | RAND_DATA_PASS *item; | |
2467 | unsigned char *got; | |
2468 | size_t got_len = expected->generate_bits / 8; | |
2469 | OSSL_PARAM params[5], *p = params; | |
2470 | int i = -1, ret = 0; | |
2471 | unsigned int strength; | |
2472 | unsigned char *z; | |
2473 | ||
2474 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) | |
2475 | return 0; | |
2476 | ||
2477 | *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &expected->use_df); | |
2478 | if (expected->cipher != NULL) | |
2479 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER, | |
2480 | expected->cipher, 0); | |
2481 | if (expected->digest != NULL) | |
2482 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST, | |
2483 | expected->digest, 0); | |
2484 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC, "HMAC", 0); | |
2485 | *p = OSSL_PARAM_construct_end(); | |
e494fac7 | 2486 | if (!TEST_true(EVP_RAND_CTX_set_params(expected->ctx, params))) |
c9ed9307 P |
2487 | goto err; |
2488 | ||
ed576acd | 2489 | strength = EVP_RAND_get_strength(expected->ctx); |
c9ed9307 P |
2490 | for (i = 0; i <= expected->n; i++) { |
2491 | item = expected->data + i; | |
2492 | ||
2493 | p = params; | |
2494 | z = item->entropy != NULL ? item->entropy : (unsigned char *)""; | |
2495 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY, | |
2496 | z, item->entropy_len); | |
2497 | z = item->nonce != NULL ? item->nonce : (unsigned char *)""; | |
2498 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE, | |
2499 | z, item->nonce_len); | |
2500 | *p = OSSL_PARAM_construct_end(); | |
7198bd1a P |
2501 | if (!TEST_true(EVP_RAND_instantiate(expected->parent, strength, |
2502 | 0, NULL, 0, params))) | |
c9ed9307 P |
2503 | goto err; |
2504 | ||
2505 | z = item->pers != NULL ? item->pers : (unsigned char *)""; | |
2506 | if (!TEST_true(EVP_RAND_instantiate | |
2507 | (expected->ctx, strength, | |
2508 | expected->prediction_resistance, z, | |
7198bd1a | 2509 | item->pers_len, NULL))) |
c9ed9307 P |
2510 | goto err; |
2511 | ||
2512 | if (item->reseed_entropy != NULL) { | |
2513 | params[0] = OSSL_PARAM_construct_octet_string | |
2514 | (OSSL_RAND_PARAM_TEST_ENTROPY, item->reseed_entropy, | |
2515 | item->reseed_entropy_len); | |
2516 | params[1] = OSSL_PARAM_construct_end(); | |
e494fac7 | 2517 | if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params))) |
c9ed9307 P |
2518 | goto err; |
2519 | ||
2520 | if (!TEST_true(EVP_RAND_reseed | |
2521 | (expected->ctx, expected->prediction_resistance, | |
2522 | NULL, 0, item->reseed_addin, | |
2523 | item->reseed_addin_len))) | |
2524 | goto err; | |
2525 | } | |
2526 | if (item->pr_entropyA != NULL) { | |
2527 | params[0] = OSSL_PARAM_construct_octet_string | |
2528 | (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyA, | |
2529 | item->pr_entropyA_len); | |
2530 | params[1] = OSSL_PARAM_construct_end(); | |
e494fac7 | 2531 | if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params))) |
c9ed9307 P |
2532 | goto err; |
2533 | } | |
2534 | if (!TEST_true(EVP_RAND_generate | |
2535 | (expected->ctx, got, got_len, | |
2536 | strength, expected->prediction_resistance, | |
2537 | item->addinA, item->addinA_len))) | |
2538 | goto err; | |
2539 | ||
2540 | if (item->pr_entropyB != NULL) { | |
2541 | params[0] = OSSL_PARAM_construct_octet_string | |
2542 | (OSSL_RAND_PARAM_TEST_ENTROPY, item->pr_entropyB, | |
2543 | item->pr_entropyB_len); | |
2544 | params[1] = OSSL_PARAM_construct_end(); | |
e494fac7 | 2545 | if (!TEST_true(EVP_RAND_CTX_set_params(expected->parent, params))) |
10481d33 | 2546 | goto err; |
c9ed9307 P |
2547 | } |
2548 | if (!TEST_true(EVP_RAND_generate | |
2549 | (expected->ctx, got, got_len, | |
2550 | strength, expected->prediction_resistance, | |
2551 | item->addinB, item->addinB_len))) | |
2552 | goto err; | |
2553 | if (!TEST_mem_eq(got, got_len, item->output, item->output_len)) | |
2554 | goto err; | |
2555 | if (!TEST_true(EVP_RAND_uninstantiate(expected->ctx)) | |
2556 | || !TEST_true(EVP_RAND_uninstantiate(expected->parent)) | |
2557 | || !TEST_true(EVP_RAND_verify_zeroization(expected->ctx)) | |
ed576acd | 2558 | || !TEST_int_eq(EVP_RAND_get_state(expected->ctx), |
c9ed9307 P |
2559 | EVP_RAND_STATE_UNINITIALISED)) |
2560 | goto err; | |
2561 | } | |
2562 | t->err = NULL; | |
2563 | ret = 1; | |
2564 | ||
2565 | err: | |
2566 | if (ret == 0 && i >= 0) | |
2567 | TEST_info("Error in test case %d of %d\n", i, expected->n + 1); | |
2568 | OPENSSL_free(got); | |
2569 | return ret; | |
2570 | } | |
2571 | ||
2572 | static const EVP_TEST_METHOD rand_test_method = { | |
2573 | "RAND", | |
2574 | rand_test_init, | |
2575 | rand_test_cleanup, | |
2576 | rand_test_parse, | |
2577 | rand_test_run | |
2578 | }; | |
2579 | ||
2580 | ||
c49e0b04 | 2581 | /** |
5ccada09 SL |
2582 | ** KDF TESTS |
2583 | **/ | |
6c5943c9 | 2584 | typedef struct kdf_data_st { |
44a284d2 | 2585 | /* Context for this operation */ |
5a285add | 2586 | EVP_KDF_CTX *ctx; |
44a284d2 DSH |
2587 | /* Expected output */ |
2588 | unsigned char *output; | |
2589 | size_t output_len; | |
bf5739a0 P |
2590 | OSSL_PARAM params[20]; |
2591 | OSSL_PARAM *p; | |
6c5943c9 | 2592 | } KDF_DATA; |
44a284d2 DSH |
2593 | |
2594 | /* | |
2595 | * Perform public key operation setup: lookup key, allocated ctx and call | |
2596 | * the appropriate initialisation function | |
2597 | */ | |
6c5943c9 | 2598 | static int kdf_test_init(EVP_TEST *t, const char *name) |
44a284d2 | 2599 | { |
6c5943c9 | 2600 | KDF_DATA *kdata; |
bf5739a0 | 2601 | EVP_KDF *kdf; |
b15d5ab6 | 2602 | |
5ccada09 SL |
2603 | if (is_kdf_disabled(name)) { |
2604 | TEST_info("skipping, '%s' is disabled", name); | |
1aec7716 SL |
2605 | t->skip = 1; |
2606 | return 1; | |
2607 | } | |
ab78f89b | 2608 | |
bf5739a0 | 2609 | if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) |
d2ba8123 | 2610 | return 0; |
bf5739a0 P |
2611 | kdata->p = kdata->params; |
2612 | *kdata->p = OSSL_PARAM_construct_end(); | |
44a284d2 | 2613 | |
5ccada09 | 2614 | kdf = EVP_KDF_fetch(libctx, name, NULL); |
92475712 P |
2615 | if (kdf == NULL) { |
2616 | OPENSSL_free(kdata); | |
44a284d2 | 2617 | return 0; |
92475712 | 2618 | } |
660c5344 | 2619 | kdata->ctx = EVP_KDF_CTX_new(kdf); |
bf5739a0 | 2620 | EVP_KDF_free(kdf); |
9e206ce5 P |
2621 | if (kdata->ctx == NULL) { |
2622 | OPENSSL_free(kdata); | |
44a284d2 | 2623 | return 0; |
9e206ce5 | 2624 | } |
c49e0b04 | 2625 | t->data = kdata; |
44a284d2 DSH |
2626 | return 1; |
2627 | } | |
2628 | ||
6c5943c9 | 2629 | static void kdf_test_cleanup(EVP_TEST *t) |
44a284d2 | 2630 | { |
6c5943c9 | 2631 | KDF_DATA *kdata = t->data; |
bf5739a0 P |
2632 | OSSL_PARAM *p; |
2633 | ||
2634 | for (p = kdata->params; p->key != NULL; p++) | |
2635 | OPENSSL_free(p->data); | |
44a284d2 | 2636 | OPENSSL_free(kdata->output); |
660c5344 | 2637 | EVP_KDF_CTX_free(kdata->ctx); |
5a285add DM |
2638 | } |
2639 | ||
2640 | static int kdf_test_ctrl(EVP_TEST *t, EVP_KDF_CTX *kctx, | |
2641 | const char *value) | |
2642 | { | |
bf5739a0 | 2643 | KDF_DATA *kdata = t->data; |
5a285add | 2644 | int rv; |
bf5739a0 | 2645 | char *p, *name; |
660c5344 | 2646 | const OSSL_PARAM *defs = EVP_KDF_settable_ctx_params(EVP_KDF_CTX_kdf(kctx)); |
5a285add | 2647 | |
bf5739a0 | 2648 | if (!TEST_ptr(name = OPENSSL_strdup(value))) |
5a285add | 2649 | return 0; |
bf5739a0 | 2650 | p = strchr(name, ':'); |
5a285add DM |
2651 | if (p != NULL) |
2652 | *p++ = '\0'; | |
bf5739a0 | 2653 | |
bbbd1210 TM |
2654 | if (strcmp(name, "r") == 0 |
2655 | && OSSL_PARAM_locate_const(defs, name) == NULL) { | |
2656 | TEST_info("skipping, setting 'r' is unsupported"); | |
2657 | t->skip = 1; | |
2658 | goto end; | |
2659 | } | |
2660 | ||
64da55a6 | 2661 | rv = OSSL_PARAM_allocate_from_text(kdata->p, defs, name, p, |
2ee0dfa6 | 2662 | p != NULL ? strlen(p) : 0, NULL); |
bf5739a0 P |
2663 | *++kdata->p = OSSL_PARAM_construct_end(); |
2664 | if (!rv) { | |
2665 | t->err = "KDF_PARAM_ERROR"; | |
2666 | OPENSSL_free(name); | |
2667 | return 0; | |
2668 | } | |
64da55a6 | 2669 | if (p != NULL && strcmp(name, "digest") == 0) { |
5ccada09 SL |
2670 | if (is_digest_disabled(p)) { |
2671 | TEST_info("skipping, '%s' is disabled", p); | |
5a285add | 2672 | t->skip = 1; |
5ccada09 | 2673 | } |
bbbd1210 | 2674 | goto end; |
5a285add | 2675 | } |
89cccbea SL |
2676 | if (p != NULL |
2677 | && (strcmp(name, "cipher") == 0 | |
2678 | || strcmp(name, "cekalg") == 0) | |
2679 | && is_cipher_disabled(p)) { | |
2680 | TEST_info("skipping, '%s' is disabled", p); | |
2681 | t->skip = 1; | |
bbbd1210 | 2682 | goto end; |
33f54da3 | 2683 | } |
0e9a265e PU |
2684 | if (p != NULL |
2685 | && (strcmp(name, "mac") == 0) | |
2686 | && is_mac_disabled(p)) { | |
2687 | TEST_info("skipping, '%s' is disabled", p); | |
2688 | t->skip = 1; | |
2689 | } | |
bbbd1210 | 2690 | end: |
bf5739a0 P |
2691 | OPENSSL_free(name); |
2692 | return 1; | |
44a284d2 DSH |
2693 | } |
2694 | ||
6c5943c9 | 2695 | static int kdf_test_parse(EVP_TEST *t, |
44a284d2 DSH |
2696 | const char *keyword, const char *value) |
2697 | { | |
6c5943c9 RS |
2698 | KDF_DATA *kdata = t->data; |
2699 | ||
44a284d2 | 2700 | if (strcmp(keyword, "Output") == 0) |
c49e0b04 | 2701 | return parse_bin(value, &kdata->output, &kdata->output_len); |
2ff286c2 | 2702 | if (HAS_PREFIX(keyword, "Ctrl")) |
5a285add | 2703 | return kdf_test_ctrl(t, kdata->ctx, value); |
44a284d2 DSH |
2704 | return 0; |
2705 | } | |
2706 | ||
6c5943c9 | 2707 | static int kdf_test_run(EVP_TEST *t) |
44a284d2 | 2708 | { |
e3d378bc AP |
2709 | KDF_DATA *expected = t->data; |
2710 | unsigned char *got = NULL; | |
2711 | size_t got_len = expected->output_len; | |
c8adf19d | 2712 | EVP_KDF_CTX *ctx; |
6c5943c9 | 2713 | |
660c5344 | 2714 | if (!EVP_KDF_CTX_set_params(expected->ctx, expected->params)) { |
bf5739a0 P |
2715 | t->err = "KDF_CTRL_ERROR"; |
2716 | return 1; | |
2717 | } | |
cce935b2 | 2718 | if (!TEST_ptr(got = OPENSSL_malloc(got_len == 0 ? 1 : got_len))) { |
6c5943c9 | 2719 | t->err = "INTERNAL_ERROR"; |
44a284d2 | 2720 | goto err; |
6c5943c9 | 2721 | } |
c8adf19d P |
2722 | if ((ctx = EVP_KDF_CTX_dup(expected->ctx)) != NULL) { |
2723 | EVP_KDF_CTX_free(expected->ctx); | |
2724 | expected->ctx = ctx; | |
2725 | } | |
05cdec39 | 2726 | if (EVP_KDF_derive(expected->ctx, got, got_len, NULL) <= 0) { |
6c5943c9 | 2727 | t->err = "KDF_DERIVE_ERROR"; |
44a284d2 | 2728 | goto err; |
6c5943c9 | 2729 | } |
4cceb185 P |
2730 | if (!memory_err_compare(t, "KDF_MISMATCH", |
2731 | expected->output, expected->output_len, | |
2732 | got, got_len)) | |
44a284d2 | 2733 | goto err; |
4cceb185 | 2734 | |
6c5943c9 RS |
2735 | t->err = NULL; |
2736 | ||
44a284d2 | 2737 | err: |
e3d378bc | 2738 | OPENSSL_free(got); |
44a284d2 DSH |
2739 | return 1; |
2740 | } | |
2741 | ||
6c5943c9 | 2742 | static const EVP_TEST_METHOD kdf_test_method = { |
44a284d2 DSH |
2743 | "KDF", |
2744 | kdf_test_init, | |
2745 | kdf_test_cleanup, | |
2746 | kdf_test_parse, | |
2747 | kdf_test_run | |
2748 | }; | |
d91b7423 | 2749 | |
5a285add | 2750 | /** |
5ccada09 SL |
2751 | ** PKEY KDF TESTS |
2752 | **/ | |
5a285add DM |
2753 | |
2754 | typedef struct pkey_kdf_data_st { | |
2755 | /* Context for this operation */ | |
2756 | EVP_PKEY_CTX *ctx; | |
2757 | /* Expected output */ | |
2758 | unsigned char *output; | |
2759 | size_t output_len; | |
2760 | } PKEY_KDF_DATA; | |
2761 | ||
2762 | /* | |
2763 | * Perform public key operation setup: lookup key, allocated ctx and call | |
2764 | * the appropriate initialisation function | |
2765 | */ | |
2766 | static int pkey_kdf_test_init(EVP_TEST *t, const char *name) | |
2767 | { | |
5ccada09 | 2768 | PKEY_KDF_DATA *kdata = NULL; |
5a285add | 2769 | |
5ccada09 SL |
2770 | if (is_kdf_disabled(name)) { |
2771 | TEST_info("skipping, '%s' is disabled", name); | |
5a285add DM |
2772 | t->skip = 1; |
2773 | return 1; | |
2774 | } | |
5a285add | 2775 | |
5a285add DM |
2776 | if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) |
2777 | return 0; | |
711ae5d3 MC |
2778 | |
2779 | kdata->ctx = EVP_PKEY_CTX_new_from_name(libctx, name, NULL); | |
5ccada09 SL |
2780 | if (kdata->ctx == NULL |
2781 | || EVP_PKEY_derive_init(kdata->ctx) <= 0) | |
2782 | goto err; | |
2783 | ||
5a285add DM |
2784 | t->data = kdata; |
2785 | return 1; | |
5ccada09 | 2786 | err: |
5ccada09 SL |
2787 | EVP_PKEY_CTX_free(kdata->ctx); |
2788 | OPENSSL_free(kdata); | |
2789 | return 0; | |
5a285add DM |
2790 | } |
2791 | ||
2792 | static void pkey_kdf_test_cleanup(EVP_TEST *t) | |
2793 | { | |
2794 | PKEY_KDF_DATA *kdata = t->data; | |
bf5739a0 | 2795 | |
5a285add DM |
2796 | OPENSSL_free(kdata->output); |
2797 | EVP_PKEY_CTX_free(kdata->ctx); | |
2798 | } | |
2799 | ||
2800 | static int pkey_kdf_test_parse(EVP_TEST *t, | |
2801 | const char *keyword, const char *value) | |
2802 | { | |
2803 | PKEY_KDF_DATA *kdata = t->data; | |
2804 | ||
2805 | if (strcmp(keyword, "Output") == 0) | |
2806 | return parse_bin(value, &kdata->output, &kdata->output_len); | |
2ff286c2 | 2807 | if (HAS_PREFIX(keyword, "Ctrl")) |
5a285add DM |
2808 | return pkey_test_ctrl(t, kdata->ctx, value); |
2809 | return 0; | |
2810 | } | |
2811 | ||
2812 | static int pkey_kdf_test_run(EVP_TEST *t) | |
2813 | { | |
2814 | PKEY_KDF_DATA *expected = t->data; | |
2815 | unsigned char *got = NULL; | |
2816 | size_t got_len = expected->output_len; | |
2817 | ||
cce935b2 | 2818 | if (!TEST_ptr(got = OPENSSL_malloc(got_len == 0 ? 1 : got_len))) { |
5a285add DM |
2819 | t->err = "INTERNAL_ERROR"; |
2820 | goto err; | |
2821 | } | |
2822 | if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) { | |
2823 | t->err = "KDF_DERIVE_ERROR"; | |
2824 | goto err; | |
2825 | } | |
2826 | if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) { | |
2827 | t->err = "KDF_MISMATCH"; | |
2828 | goto err; | |
2829 | } | |
2830 | t->err = NULL; | |
2831 | ||
2832 | err: | |
2833 | OPENSSL_free(got); | |
2834 | return 1; | |
2835 | } | |
2836 | ||
2837 | static const EVP_TEST_METHOD pkey_kdf_test_method = { | |
2838 | "PKEYKDF", | |
2839 | pkey_kdf_test_init, | |
2840 | pkey_kdf_test_cleanup, | |
2841 | pkey_kdf_test_parse, | |
2842 | pkey_kdf_test_run | |
2843 | }; | |
2844 | ||
c49e0b04 | 2845 | /** |
5ccada09 SL |
2846 | ** KEYPAIR TESTS |
2847 | **/ | |
c49e0b04 RS |
2848 | |
2849 | typedef struct keypair_test_data_st { | |
d91b7423 RS |
2850 | EVP_PKEY *privk; |
2851 | EVP_PKEY *pubk; | |
6c5943c9 | 2852 | } KEYPAIR_TEST_DATA; |
d91b7423 | 2853 | |
6c5943c9 | 2854 | static int keypair_test_init(EVP_TEST *t, const char *pair) |
d91b7423 | 2855 | { |
c49e0b04 | 2856 | KEYPAIR_TEST_DATA *data; |
d91b7423 RS |
2857 | int rv = 0; |
2858 | EVP_PKEY *pk = NULL, *pubk = NULL; | |
2859 | char *pub, *priv = NULL; | |
d91b7423 | 2860 | |
c49e0b04 | 2861 | /* Split private and public names. */ |
6c5943c9 RS |
2862 | if (!TEST_ptr(priv = OPENSSL_strdup(pair)) |
2863 | || !TEST_ptr(pub = strchr(priv, ':'))) { | |
2864 | t->err = "PARSING_ERROR"; | |
d91b7423 RS |
2865 | goto end; |
2866 | } | |
c49e0b04 | 2867 | *pub++ = '\0'; |
d91b7423 | 2868 | |
6c5943c9 | 2869 | if (!TEST_true(find_key(&pk, priv, private_keys))) { |
c49e0b04 | 2870 | TEST_info("Can't find private key: %s", priv); |
6c5943c9 | 2871 | t->err = "MISSING_PRIVATE_KEY"; |
d91b7423 RS |
2872 | goto end; |
2873 | } | |
6c5943c9 | 2874 | if (!TEST_true(find_key(&pubk, pub, public_keys))) { |
c49e0b04 | 2875 | TEST_info("Can't find public key: %s", pub); |
6c5943c9 | 2876 | t->err = "MISSING_PUBLIC_KEY"; |
d91b7423 RS |
2877 | goto end; |
2878 | } | |
2879 | ||
2880 | if (pk == NULL && pubk == NULL) { | |
2881 | /* Both keys are listed but unsupported: skip this test */ | |
2882 | t->skip = 1; | |
2883 | rv = 1; | |
2884 | goto end; | |
2885 | } | |
2886 | ||
6c5943c9 | 2887 | if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data)))) |
d91b7423 | 2888 | goto end; |
d91b7423 RS |
2889 | data->privk = pk; |
2890 | data->pubk = pubk; | |
2891 | t->data = data; | |
d91b7423 | 2892 | rv = 1; |
6c5943c9 | 2893 | t->err = NULL; |
d91b7423 RS |
2894 | |
2895 | end: | |
6c5943c9 | 2896 | OPENSSL_free(priv); |
d91b7423 RS |
2897 | return rv; |
2898 | } | |
2899 | ||
6c5943c9 | 2900 | static void keypair_test_cleanup(EVP_TEST *t) |
d91b7423 | 2901 | { |
6c5943c9 | 2902 | OPENSSL_free(t->data); |
d91b7423 | 2903 | t->data = NULL; |
d91b7423 RS |
2904 | } |
2905 | ||
c49e0b04 RS |
2906 | /* |
2907 | * For tests that do not accept any custom keywords. | |
d91b7423 | 2908 | */ |
6c5943c9 | 2909 | static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value) |
d91b7423 RS |
2910 | { |
2911 | return 0; | |
2912 | } | |
2913 | ||
6c5943c9 | 2914 | static int keypair_test_run(EVP_TEST *t) |
d91b7423 RS |
2915 | { |
2916 | int rv = 0; | |
6c5943c9 | 2917 | const KEYPAIR_TEST_DATA *pair = t->data; |
d91b7423 RS |
2918 | |
2919 | if (pair->privk == NULL || pair->pubk == NULL) { | |
6c5943c9 RS |
2920 | /* |
2921 | * this can only happen if only one of the keys is not set | |
d91b7423 RS |
2922 | * which means that one of them was unsupported while the |
2923 | * other isn't: hence a key type mismatch. | |
2924 | */ | |
6c5943c9 | 2925 | t->err = "KEYPAIR_TYPE_MISMATCH"; |
d91b7423 RS |
2926 | rv = 1; |
2927 | goto end; | |
2928 | } | |
2929 | ||
1287dabd | 2930 | if ((rv = EVP_PKEY_eq(pair->privk, pair->pubk)) != 1) { |
2931 | if (0 == rv) { | |
6c5943c9 | 2932 | t->err = "KEYPAIR_MISMATCH"; |
1287dabd | 2933 | } else if (-1 == rv) { |
6c5943c9 | 2934 | t->err = "KEYPAIR_TYPE_MISMATCH"; |
1287dabd | 2935 | } else if (-2 == rv) { |
6c5943c9 | 2936 | t->err = "UNSUPPORTED_KEY_COMPARISON"; |
d91b7423 | 2937 | } else { |
6c5943c9 | 2938 | TEST_error("Unexpected error in key comparison"); |
d91b7423 RS |
2939 | rv = 0; |
2940 | goto end; | |
2941 | } | |
2942 | rv = 1; | |
2943 | goto end; | |
2944 | } | |
2945 | ||
2946 | rv = 1; | |
6c5943c9 | 2947 | t->err = NULL; |
d91b7423 RS |
2948 | |
2949 | end: | |
d91b7423 RS |
2950 | return rv; |
2951 | } | |
2952 | ||
6c5943c9 | 2953 | static const EVP_TEST_METHOD keypair_test_method = { |
d91b7423 RS |
2954 | "PrivPubKeyPair", |
2955 | keypair_test_init, | |
2956 | keypair_test_cleanup, | |
2957 | void_test_parse, | |
2958 | keypair_test_run | |
2959 | }; | |
2960 | ||
1f0fc03b | 2961 | /** |
5ccada09 SL |
2962 | ** KEYGEN TEST |
2963 | **/ | |
1f0fc03b DSH |
2964 | |
2965 | typedef struct keygen_test_data_st { | |
2966 | EVP_PKEY_CTX *genctx; /* Keygen context to use */ | |
2967 | char *keyname; /* Key name to store key or NULL */ | |
2968 | } KEYGEN_TEST_DATA; | |
2969 | ||
2970 | static int keygen_test_init(EVP_TEST *t, const char *alg) | |
2971 | { | |
2972 | KEYGEN_TEST_DATA *data; | |
2973 | EVP_PKEY_CTX *genctx; | |
2974 | int nid = OBJ_sn2nid(alg); | |
2975 | ||
2976 | if (nid == NID_undef) { | |
2977 | nid = OBJ_ln2nid(alg); | |
2978 | if (nid == NID_undef) | |
2979 | return 0; | |
2980 | } | |
2981 | ||
5ccada09 | 2982 | if (is_pkey_disabled(alg)) { |
1f0fc03b DSH |
2983 | t->skip = 1; |
2984 | return 1; | |
2985 | } | |
5ccada09 SL |
2986 | if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_from_name(libctx, alg, NULL))) |
2987 | goto err; | |
1f0fc03b DSH |
2988 | |
2989 | if (EVP_PKEY_keygen_init(genctx) <= 0) { | |
2990 | t->err = "KEYGEN_INIT_ERROR"; | |
2991 | goto err; | |
2992 | } | |
2993 | ||
2994 | if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data)))) | |
2995 | goto err; | |
2996 | data->genctx = genctx; | |
2997 | data->keyname = NULL; | |
2998 | t->data = data; | |
2999 | t->err = NULL; | |
3000 | return 1; | |
3001 | ||
3002 | err: | |
3003 | EVP_PKEY_CTX_free(genctx); | |
3004 | return 0; | |
3005 | } | |
3006 | ||
3007 | static void keygen_test_cleanup(EVP_TEST *t) | |
3008 | { | |
3009 | KEYGEN_TEST_DATA *keygen = t->data; | |
3010 | ||
3011 | EVP_PKEY_CTX_free(keygen->genctx); | |
3012 | OPENSSL_free(keygen->keyname); | |
3013 | OPENSSL_free(t->data); | |
3014 | t->data = NULL; | |
3015 | } | |
3016 | ||
3017 | static int keygen_test_parse(EVP_TEST *t, | |
3018 | const char *keyword, const char *value) | |
3019 | { | |
3020 | KEYGEN_TEST_DATA *keygen = t->data; | |
3021 | ||
3022 | if (strcmp(keyword, "KeyName") == 0) | |
3023 | return TEST_ptr(keygen->keyname = OPENSSL_strdup(value)); | |
3024 | if (strcmp(keyword, "Ctrl") == 0) | |
3025 | return pkey_test_ctrl(t, keygen->genctx, value); | |
3026 | return 0; | |
3027 | } | |
3028 | ||
3029 | static int keygen_test_run(EVP_TEST *t) | |
3030 | { | |
3031 | KEYGEN_TEST_DATA *keygen = t->data; | |
3032 | EVP_PKEY *pkey = NULL; | |
88af1ebb | 3033 | int rv = 1; |
1f0fc03b | 3034 | |
1f0fc03b DSH |
3035 | if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) { |
3036 | t->err = "KEYGEN_GENERATE_ERROR"; | |
3037 | goto err; | |
3038 | } | |
3039 | ||
5ccada09 SL |
3040 | if (!evp_pkey_is_provided(pkey)) { |
3041 | TEST_info("Warning: legacy key generated %s", keygen->keyname); | |
3042 | goto err; | |
3043 | } | |
1f0fc03b DSH |
3044 | if (keygen->keyname != NULL) { |
3045 | KEY_LIST *key; | |
3046 | ||
88af1ebb | 3047 | rv = 0; |
1f0fc03b DSH |
3048 | if (find_key(NULL, keygen->keyname, private_keys)) { |
3049 | TEST_info("Duplicate key %s", keygen->keyname); | |
3050 | goto err; | |
3051 | } | |
3052 | ||
3053 | if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key)))) | |
3054 | goto err; | |
3055 | key->name = keygen->keyname; | |
3056 | keygen->keyname = NULL; | |
3057 | key->key = pkey; | |
3058 | key->next = private_keys; | |
3059 | private_keys = key; | |
88af1ebb | 3060 | rv = 1; |
1f0fc03b DSH |
3061 | } else { |
3062 | EVP_PKEY_free(pkey); | |
3063 | } | |
3064 | ||
88af1ebb | 3065 | t->err = NULL; |
1f0fc03b DSH |
3066 | |
3067 | err: | |
88af1ebb | 3068 | return rv; |
1f0fc03b DSH |
3069 | } |
3070 | ||
3071 | static const EVP_TEST_METHOD keygen_test_method = { | |
3072 | "KeyGen", | |
3073 | keygen_test_init, | |
3074 | keygen_test_cleanup, | |
3075 | keygen_test_parse, | |
3076 | keygen_test_run, | |
3077 | }; | |
c49e0b04 RS |
3078 | |
3079 | /** | |
5ccada09 SL |
3080 | ** DIGEST SIGN+VERIFY TESTS |
3081 | **/ | |
c49e0b04 | 3082 | |
75726fe8 | 3083 | typedef struct { |
2117a737 DSH |
3084 | int is_verify; /* Set to 1 if verifying */ |
3085 | int is_oneshot; /* Set to 1 for one shot operation */ | |
3086 | const EVP_MD *md; /* Digest to use */ | |
3087 | EVP_MD_CTX *ctx; /* Digest context */ | |
75726fe8 | 3088 | EVP_PKEY_CTX *pctx; |
2117a737 DSH |
3089 | STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */ |
3090 | unsigned char *osin; /* Input data if one shot */ | |
3091 | size_t osin_len; /* Input length data if one shot */ | |
3092 | unsigned char *output; /* Expected output */ | |
3093 | size_t output_len; /* Expected output length */ | |
75726fe8 DSH |
3094 | } DIGESTSIGN_DATA; |
3095 | ||
7b22334f DSH |
3096 | static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify, |
3097 | int is_oneshot) | |
75726fe8 DSH |
3098 | { |
3099 | const EVP_MD *md = NULL; | |
3100 | DIGESTSIGN_DATA *mdat; | |
3101 | ||
3102 | if (strcmp(alg, "NULL") != 0) { | |
5ccada09 SL |
3103 | if (is_digest_disabled(alg)) { |
3104 | t->skip = 1; | |
3105 | return 1; | |
75726fe8 | 3106 | } |
5ccada09 SL |
3107 | md = EVP_get_digestbyname(alg); |
3108 | if (md == NULL) | |
3109 | return 0; | |
75726fe8 DSH |
3110 | } |
3111 | if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat)))) | |
3112 | return 0; | |
3113 | mdat->md = md; | |
3114 | if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) { | |
3115 | OPENSSL_free(mdat); | |
3116 | return 0; | |
3117 | } | |
3118 | mdat->is_verify = is_verify; | |
7b22334f | 3119 | mdat->is_oneshot = is_oneshot; |
75726fe8 DSH |
3120 | t->data = mdat; |
3121 | return 1; | |
3122 | } | |
3123 | ||
3124 | static int digestsign_test_init(EVP_TEST *t, const char *alg) | |
3125 | { | |
7b22334f | 3126 | return digestsigver_test_init(t, alg, 0, 0); |
75726fe8 DSH |
3127 | } |
3128 | ||
3129 | static void digestsigver_test_cleanup(EVP_TEST *t) | |
3130 | { | |
3131 | DIGESTSIGN_DATA *mdata = t->data; | |
3132 | ||
3133 | EVP_MD_CTX_free(mdata->ctx); | |
3134 | sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free); | |
7b22334f | 3135 | OPENSSL_free(mdata->osin); |
75726fe8 DSH |
3136 | OPENSSL_free(mdata->output); |
3137 | OPENSSL_free(mdata); | |
3138 | t->data = NULL; | |
3139 | } | |
3140 | ||
3141 | static int digestsigver_test_parse(EVP_TEST *t, | |
3142 | const char *keyword, const char *value) | |
3143 | { | |
3144 | DIGESTSIGN_DATA *mdata = t->data; | |
3145 | ||
3146 | if (strcmp(keyword, "Key") == 0) { | |
3147 | EVP_PKEY *pkey = NULL; | |
3148 | int rv = 0; | |
ed576acd | 3149 | const char *name = mdata->md == NULL ? NULL : EVP_MD_get0_name(mdata->md); |
75726fe8 DSH |
3150 | |
3151 | if (mdata->is_verify) | |
3152 | rv = find_key(&pkey, value, public_keys); | |
3153 | if (rv == 0) | |
3154 | rv = find_key(&pkey, value, private_keys); | |
3155 | if (rv == 0 || pkey == NULL) { | |
3156 | t->skip = 1; | |
3157 | return 1; | |
3158 | } | |
3159 | if (mdata->is_verify) { | |
d8652be0 | 3160 | if (!EVP_DigestVerifyInit_ex(mdata->ctx, &mdata->pctx, name, libctx, |
af6171b3 | 3161 | NULL, pkey, NULL)) |
75726fe8 DSH |
3162 | t->err = "DIGESTVERIFYINIT_ERROR"; |
3163 | return 1; | |
3164 | } | |
d8652be0 | 3165 | if (!EVP_DigestSignInit_ex(mdata->ctx, &mdata->pctx, name, libctx, NULL, |
af6171b3 | 3166 | pkey, NULL)) |
75726fe8 DSH |
3167 | t->err = "DIGESTSIGNINIT_ERROR"; |
3168 | return 1; | |
3169 | } | |
3170 | ||
7b22334f DSH |
3171 | if (strcmp(keyword, "Input") == 0) { |
3172 | if (mdata->is_oneshot) | |
c49e0b04 | 3173 | return parse_bin(value, &mdata->osin, &mdata->osin_len); |
75726fe8 | 3174 | return evp_test_buffer_append(value, &mdata->input); |
7b22334f | 3175 | } |
75726fe8 | 3176 | if (strcmp(keyword, "Output") == 0) |
c49e0b04 | 3177 | return parse_bin(value, &mdata->output, &mdata->output_len); |
7b22334f DSH |
3178 | |
3179 | if (!mdata->is_oneshot) { | |
3180 | if (strcmp(keyword, "Count") == 0) | |
3181 | return evp_test_buffer_set_count(value, mdata->input); | |
3182 | if (strcmp(keyword, "Ncopy") == 0) | |
3183 | return evp_test_buffer_ncopy(value, mdata->input); | |
3184 | } | |
75726fe8 DSH |
3185 | if (strcmp(keyword, "Ctrl") == 0) { |
3186 | if (mdata->pctx == NULL) | |
f42c225d | 3187 | return -1; |
75726fe8 DSH |
3188 | return pkey_test_ctrl(t, mdata->pctx, value); |
3189 | } | |
3190 | return 0; | |
3191 | } | |
3192 | ||
3193 | static int digestsign_update_fn(void *ctx, const unsigned char *buf, | |
3194 | size_t buflen) | |
3195 | { | |
3196 | return EVP_DigestSignUpdate(ctx, buf, buflen); | |
3197 | } | |
3198 | ||
3199 | static int digestsign_test_run(EVP_TEST *t) | |
3200 | { | |
e3d378bc AP |
3201 | DIGESTSIGN_DATA *expected = t->data; |
3202 | unsigned char *got = NULL; | |
3203 | size_t got_len; | |
75726fe8 | 3204 | |
e3d378bc AP |
3205 | if (!evp_test_buffer_do(expected->input, digestsign_update_fn, |
3206 | expected->ctx)) { | |
75726fe8 DSH |
3207 | t->err = "DIGESTUPDATE_ERROR"; |
3208 | goto err; | |
3209 | } | |
3210 | ||
e3d378bc | 3211 | if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) { |
75726fe8 DSH |
3212 | t->err = "DIGESTSIGNFINAL_LENGTH_ERROR"; |
3213 | goto err; | |
3214 | } | |
e3d378bc | 3215 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { |
75726fe8 DSH |
3216 | t->err = "MALLOC_FAILURE"; |
3217 | goto err; | |
3218 | } | |
e3d378bc | 3219 | if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) { |
75726fe8 DSH |
3220 | t->err = "DIGESTSIGNFINAL_ERROR"; |
3221 | goto err; | |
3222 | } | |
4cceb185 P |
3223 | if (!memory_err_compare(t, "SIGNATURE_MISMATCH", |
3224 | expected->output, expected->output_len, | |
3225 | got, got_len)) | |
75726fe8 | 3226 | goto err; |
75726fe8 | 3227 | |
4cceb185 | 3228 | t->err = NULL; |
75726fe8 | 3229 | err: |
e3d378bc | 3230 | OPENSSL_free(got); |
75726fe8 DSH |
3231 | return 1; |
3232 | } | |
3233 | ||
3234 | static const EVP_TEST_METHOD digestsign_test_method = { | |
3235 | "DigestSign", | |
3236 | digestsign_test_init, | |
3237 | digestsigver_test_cleanup, | |
3238 | digestsigver_test_parse, | |
3239 | digestsign_test_run | |
3240 | }; | |
3241 | ||
3242 | static int digestverify_test_init(EVP_TEST *t, const char *alg) | |
3243 | { | |
7b22334f | 3244 | return digestsigver_test_init(t, alg, 1, 0); |
75726fe8 DSH |
3245 | } |
3246 | ||
3247 | static int digestverify_update_fn(void *ctx, const unsigned char *buf, | |
3248 | size_t buflen) | |
3249 | { | |
3250 | return EVP_DigestVerifyUpdate(ctx, buf, buflen); | |
3251 | } | |
3252 | ||
3253 | static int digestverify_test_run(EVP_TEST *t) | |
3254 | { | |
3255 | DIGESTSIGN_DATA *mdata = t->data; | |
3256 | ||
3257 | if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) { | |
3258 | t->err = "DIGESTUPDATE_ERROR"; | |
3259 | return 1; | |
3260 | } | |
3261 | ||
3262 | if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output, | |
3263 | mdata->output_len) <= 0) | |
3264 | t->err = "VERIFY_ERROR"; | |
3265 | return 1; | |
3266 | } | |
3267 | ||
3268 | static const EVP_TEST_METHOD digestverify_test_method = { | |
3269 | "DigestVerify", | |
3270 | digestverify_test_init, | |
3271 | digestsigver_test_cleanup, | |
3272 | digestsigver_test_parse, | |
3273 | digestverify_test_run | |
3274 | }; | |
3275 | ||
7b22334f DSH |
3276 | static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg) |
3277 | { | |
3278 | return digestsigver_test_init(t, alg, 0, 1); | |
3279 | } | |
3280 | ||
3281 | static int oneshot_digestsign_test_run(EVP_TEST *t) | |
3282 | { | |
e3d378bc AP |
3283 | DIGESTSIGN_DATA *expected = t->data; |
3284 | unsigned char *got = NULL; | |
3285 | size_t got_len; | |
7b22334f | 3286 | |
e3d378bc AP |
3287 | if (!EVP_DigestSign(expected->ctx, NULL, &got_len, |
3288 | expected->osin, expected->osin_len)) { | |
7b22334f DSH |
3289 | t->err = "DIGESTSIGN_LENGTH_ERROR"; |
3290 | goto err; | |
3291 | } | |
e3d378bc | 3292 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { |
7b22334f DSH |
3293 | t->err = "MALLOC_FAILURE"; |
3294 | goto err; | |
3295 | } | |
e3d378bc AP |
3296 | if (!EVP_DigestSign(expected->ctx, got, &got_len, |
3297 | expected->osin, expected->osin_len)) { | |
7b22334f DSH |
3298 | t->err = "DIGESTSIGN_ERROR"; |
3299 | goto err; | |
3300 | } | |
4cceb185 P |
3301 | if (!memory_err_compare(t, "SIGNATURE_MISMATCH", |
3302 | expected->output, expected->output_len, | |
3303 | got, got_len)) | |
7b22334f | 3304 | goto err; |
7b22334f | 3305 | |
4cceb185 | 3306 | t->err = NULL; |
7b22334f | 3307 | err: |
e3d378bc | 3308 | OPENSSL_free(got); |
7b22334f DSH |
3309 | return 1; |
3310 | } | |
3311 | ||
3312 | static const EVP_TEST_METHOD oneshot_digestsign_test_method = { | |
3313 | "OneShotDigestSign", | |
3314 | oneshot_digestsign_test_init, | |
3315 | digestsigver_test_cleanup, | |
3316 | digestsigver_test_parse, | |
3317 | oneshot_digestsign_test_run | |
3318 | }; | |
3319 | ||
3320 | static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg) | |
3321 | { | |
3322 | return digestsigver_test_init(t, alg, 1, 1); | |
3323 | } | |
3324 | ||
3325 | static int oneshot_digestverify_test_run(EVP_TEST *t) | |
3326 | { | |
3327 | DIGESTSIGN_DATA *mdata = t->data; | |
3328 | ||
3329 | if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len, | |
3330 | mdata->osin, mdata->osin_len) <= 0) | |
3331 | t->err = "VERIFY_ERROR"; | |
3332 | return 1; | |
3333 | } | |
3334 | ||
3335 | static const EVP_TEST_METHOD oneshot_digestverify_test_method = { | |
3336 | "OneShotDigestVerify", | |
3337 | oneshot_digestverify_test_init, | |
3338 | digestsigver_test_cleanup, | |
3339 | digestsigver_test_parse, | |
3340 | oneshot_digestverify_test_run | |
3341 | }; | |
3342 | ||
c49e0b04 RS |
3343 | |
3344 | /** | |
5ccada09 SL |
3345 | ** PARSING AND DISPATCH |
3346 | **/ | |
c49e0b04 RS |
3347 | |
3348 | static const EVP_TEST_METHOD *evp_test_list[] = { | |
c9ed9307 | 3349 | &rand_test_method, |
c49e0b04 RS |
3350 | &cipher_test_method, |
3351 | &digest_test_method, | |
3352 | &digestsign_test_method, | |
3353 | &digestverify_test_method, | |
3354 | &encode_test_method, | |
3355 | &kdf_test_method, | |
5a285add | 3356 | &pkey_kdf_test_method, |
c49e0b04 | 3357 | &keypair_test_method, |
1f0fc03b | 3358 | &keygen_test_method, |
c49e0b04 RS |
3359 | &mac_test_method, |
3360 | &oneshot_digestsign_test_method, | |
3361 | &oneshot_digestverify_test_method, | |
3362 | &pbe_test_method, | |
3363 | &pdecrypt_test_method, | |
3364 | &pderive_test_method, | |
3365 | &psign_test_method, | |
3366 | &pverify_recover_test_method, | |
3367 | &pverify_test_method, | |
3368 | NULL | |
3369 | }; | |
3370 | ||
3371 | static const EVP_TEST_METHOD *find_test(const char *name) | |
3372 | { | |
3373 | const EVP_TEST_METHOD **tt; | |
3374 | ||
3375 | for (tt = evp_test_list; *tt; tt++) { | |
3376 | if (strcmp(name, (*tt)->name) == 0) | |
3377 | return *tt; | |
3378 | } | |
3379 | return NULL; | |
3380 | } | |
3381 | ||
3382 | static void clear_test(EVP_TEST *t) | |
3383 | { | |
ae269dd8 | 3384 | test_clearstanza(&t->s); |
c49e0b04 RS |
3385 | ERR_clear_error(); |
3386 | if (t->data != NULL) { | |
3387 | if (t->meth != NULL) | |
3388 | t->meth->cleanup(t); | |
3389 | OPENSSL_free(t->data); | |
3390 | t->data = NULL; | |
3391 | } | |
3392 | OPENSSL_free(t->expected_err); | |
3393 | t->expected_err = NULL; | |
c49e0b04 RS |
3394 | OPENSSL_free(t->reason); |
3395 | t->reason = NULL; | |
ae269dd8 | 3396 | |
c49e0b04 RS |
3397 | /* Text literal. */ |
3398 | t->err = NULL; | |
3399 | t->skip = 0; | |
3400 | t->meth = NULL; | |
3401 | } | |
3402 | ||
5ccada09 | 3403 | /* Check for errors in the test structure; return 1 if okay, else 0. */ |
c49e0b04 RS |
3404 | static int check_test_error(EVP_TEST *t) |
3405 | { | |
3406 | unsigned long err; | |
c49e0b04 RS |
3407 | const char *reason; |
3408 | ||
3409 | if (t->err == NULL && t->expected_err == NULL) | |
3410 | return 1; | |
3411 | if (t->err != NULL && t->expected_err == NULL) { | |
3412 | if (t->aux_err != NULL) { | |
ae269dd8 RS |
3413 | TEST_info("%s:%d: Source of above error (%s); unexpected error %s", |
3414 | t->s.test_file, t->s.start, t->aux_err, t->err); | |
c49e0b04 | 3415 | } else { |
ae269dd8 RS |
3416 | TEST_info("%s:%d: Source of above error; unexpected error %s", |
3417 | t->s.test_file, t->s.start, t->err); | |
c49e0b04 RS |
3418 | } |
3419 | return 0; | |
3420 | } | |
3421 | if (t->err == NULL && t->expected_err != NULL) { | |
ae269dd8 RS |
3422 | TEST_info("%s:%d: Succeeded but was expecting %s", |
3423 | t->s.test_file, t->s.start, t->expected_err); | |
c49e0b04 RS |
3424 | return 0; |
3425 | } | |
3426 | ||
3427 | if (strcmp(t->err, t->expected_err) != 0) { | |
ae269dd8 RS |
3428 | TEST_info("%s:%d: Expected %s got %s", |
3429 | t->s.test_file, t->s.start, t->expected_err, t->err); | |
c49e0b04 RS |
3430 | return 0; |
3431 | } | |
3432 | ||
aac96e27 | 3433 | if (t->reason == NULL) |
c49e0b04 RS |
3434 | return 1; |
3435 | ||
aac96e27 | 3436 | if (t->reason == NULL) { |
ae269dd8 RS |
3437 | TEST_info("%s:%d: Test is missing function or reason code", |
3438 | t->s.test_file, t->s.start); | |
c49e0b04 RS |
3439 | return 0; |
3440 | } | |
3441 | ||
3442 | err = ERR_peek_error(); | |
3443 | if (err == 0) { | |
aac96e27 RS |
3444 | TEST_info("%s:%d: Expected error \"%s\" not set", |
3445 | t->s.test_file, t->s.start, t->reason); | |
c49e0b04 RS |
3446 | return 0; |
3447 | } | |
3448 | ||
c49e0b04 | 3449 | reason = ERR_reason_error_string(err); |
b13342e9 | 3450 | if (reason == NULL) { |
aac96e27 | 3451 | TEST_info("%s:%d: Expected error \"%s\", no strings available." |
ae269dd8 | 3452 | " Assuming ok.", |
aac96e27 | 3453 | t->s.test_file, t->s.start, t->reason); |
c49e0b04 RS |
3454 | return 1; |
3455 | } | |
3456 | ||
aac96e27 | 3457 | if (strcmp(reason, t->reason) == 0) |
c49e0b04 RS |
3458 | return 1; |
3459 | ||
aac96e27 RS |
3460 | TEST_info("%s:%d: Expected error \"%s\", got \"%s\"", |
3461 | t->s.test_file, t->s.start, t->reason, reason); | |
c49e0b04 RS |
3462 | |
3463 | return 0; | |
3464 | } | |
3465 | ||
5ccada09 | 3466 | /* Run a parsed test. Log a message and return 0 on error. */ |
c49e0b04 RS |
3467 | static int run_test(EVP_TEST *t) |
3468 | { | |
3469 | if (t->meth == NULL) | |
3470 | return 1; | |
ae269dd8 | 3471 | t->s.numtests++; |
c49e0b04 | 3472 | if (t->skip) { |
ae269dd8 | 3473 | t->s.numskip++; |
c49e0b04 RS |
3474 | } else { |
3475 | /* run the test */ | |
3476 | if (t->err == NULL && t->meth->run_test(t) != 1) { | |
ae269dd8 RS |
3477 | TEST_info("%s:%d %s error", |
3478 | t->s.test_file, t->s.start, t->meth->name); | |
c49e0b04 RS |
3479 | return 0; |
3480 | } | |
3481 | if (!check_test_error(t)) { | |
8fe3127c | 3482 | TEST_openssl_errors(); |
ae269dd8 | 3483 | t->s.errors++; |
c49e0b04 RS |
3484 | } |
3485 | } | |
3486 | ||
3487 | /* clean it up */ | |
3488 | return 1; | |
3489 | } | |
3490 | ||
3491 | static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst) | |
3492 | { | |
3493 | for (; lst != NULL; lst = lst->next) { | |
3494 | if (strcmp(lst->name, name) == 0) { | |
3495 | if (ppk != NULL) | |
3496 | *ppk = lst->key; | |
3497 | return 1; | |
3498 | } | |
3499 | } | |
3500 | return 0; | |
3501 | } | |
3502 | ||
3503 | static void free_key_list(KEY_LIST *lst) | |
3504 | { | |
3505 | while (lst != NULL) { | |
3506 | KEY_LIST *next = lst->next; | |
3507 | ||
3508 | EVP_PKEY_free(lst->key); | |
3509 | OPENSSL_free(lst->name); | |
3510 | OPENSSL_free(lst); | |
3511 | lst = next; | |
3512 | } | |
3513 | } | |
3514 | ||
c49e0b04 RS |
3515 | /* |
3516 | * Is the key type an unsupported algorithm? | |
3517 | */ | |
3cb7c5cf | 3518 | static int key_unsupported(void) |
c49e0b04 | 3519 | { |
88c1d0c1 | 3520 | long err = ERR_peek_last_error(); |
7aef2000 RL |
3521 | int lib = ERR_GET_LIB(err); |
3522 | long reason = ERR_GET_REASON(err); | |
c49e0b04 | 3523 | |
7aef2000 | 3524 | if ((lib == ERR_LIB_EVP && reason == EVP_R_UNSUPPORTED_ALGORITHM) |
4bf696c1 | 3525 | || (lib == ERR_LIB_EVP && reason == EVP_R_DECODE_ERROR) |
7aef2000 | 3526 | || reason == ERR_R_UNSUPPORTED) { |
c49e0b04 RS |
3527 | ERR_clear_error(); |
3528 | return 1; | |
3529 | } | |
3530 | #ifndef OPENSSL_NO_EC | |
3531 | /* | |
3532 | * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an | |
3533 | * hint to an unsupported algorithm/curve (e.g. if binary EC support is | |
3534 | * disabled). | |
3535 | */ | |
7aef2000 RL |
3536 | if (lib == ERR_LIB_EC |
3537 | && (reason == EC_R_UNKNOWN_GROUP | |
3538 | || reason == EC_R_INVALID_CURVE)) { | |
c49e0b04 RS |
3539 | ERR_clear_error(); |
3540 | return 1; | |
3541 | } | |
3542 | #endif /* OPENSSL_NO_EC */ | |
3543 | return 0; | |
3544 | } | |
3545 | ||
5ccada09 | 3546 | /* NULL out the value from |pp| but return it. This "steals" a pointer. */ |
ae269dd8 | 3547 | static char *take_value(PAIR *pp) |
c49e0b04 | 3548 | { |
ae269dd8 RS |
3549 | char *p = pp->value; |
3550 | ||
3551 | pp->value = NULL; | |
3552 | return p; | |
3553 | } | |
3554 | ||
4605c5ab | 3555 | #if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS) |
991a6bb5 SL |
3556 | static int securitycheck_enabled(void) |
3557 | { | |
3558 | static int enabled = -1; | |
3559 | ||
3560 | if (enabled == -1) { | |
3561 | if (OSSL_PROVIDER_available(libctx, "fips")) { | |
3562 | OSSL_PARAM params[2]; | |
3563 | OSSL_PROVIDER *prov = NULL; | |
3564 | int check = 1; | |
3565 | ||
3566 | prov = OSSL_PROVIDER_load(libctx, "fips"); | |
3567 | if (prov != NULL) { | |
3568 | params[0] = | |
3569 | OSSL_PARAM_construct_int(OSSL_PROV_PARAM_SECURITY_CHECKS, | |
3570 | &check); | |
3571 | params[1] = OSSL_PARAM_construct_end(); | |
3572 | OSSL_PROVIDER_get_params(prov, params); | |
3573 | OSSL_PROVIDER_unload(prov); | |
3574 | } | |
3575 | enabled = check; | |
3576 | return enabled; | |
3577 | } | |
3578 | enabled = 0; | |
3579 | } | |
3580 | return enabled; | |
3581 | } | |
4605c5ab | 3582 | #endif |
991a6bb5 | 3583 | |
3b5d61f4 RL |
3584 | /* |
3585 | * Return 1 if one of the providers named in the string is available. | |
3586 | * The provider names are separated with whitespace. | |
3587 | * NOTE: destructive function, it inserts '\0' after each provider name. | |
3588 | */ | |
3589 | static int prov_available(char *providers) | |
3590 | { | |
3591 | char *p; | |
3592 | int more = 1; | |
3593 | ||
3594 | while (more) { | |
3595 | for (; isspace(*providers); providers++) | |
3596 | continue; | |
3597 | if (*providers == '\0') | |
3598 | break; /* End of the road */ | |
3599 | for (p = providers; *p != '\0' && !isspace(*p); p++) | |
3600 | continue; | |
3601 | if (*p == '\0') | |
3602 | more = 0; | |
3603 | else | |
3604 | *p = '\0'; | |
5ccada09 | 3605 | if (OSSL_PROVIDER_available(libctx, providers)) |
3b5d61f4 RL |
3606 | return 1; /* Found one */ |
3607 | } | |
3608 | return 0; | |
3609 | } | |
3610 | ||
5ccada09 | 3611 | /* Read and parse one test. Return 0 if failure, 1 if okay. */ |
ae269dd8 RS |
3612 | static int parse(EVP_TEST *t) |
3613 | { | |
3614 | KEY_LIST *key, **klist; | |
c49e0b04 | 3615 | EVP_PKEY *pkey; |
ae269dd8 | 3616 | PAIR *pp; |
5ccada09 | 3617 | int i, skip_availablein = 0; |
c49e0b04 | 3618 | |
c49e0b04 | 3619 | top: |
ae269dd8 RS |
3620 | do { |
3621 | if (BIO_eof(t->s.fp)) | |
c49e0b04 | 3622 | return EOF; |
ae269dd8 RS |
3623 | clear_test(t); |
3624 | if (!test_readstanza(&t->s)) | |
3625 | return 0; | |
3626 | } while (t->s.numpairs == 0); | |
3627 | pp = &t->s.pairs[0]; | |
c49e0b04 | 3628 | |
ae269dd8 | 3629 | /* Are we adding a key? */ |
c49e0b04 RS |
3630 | klist = NULL; |
3631 | pkey = NULL; | |
5ccada09 | 3632 | start: |
ae269dd8 | 3633 | if (strcmp(pp->key, "PrivateKey") == 0) { |
5ccada09 | 3634 | pkey = PEM_read_bio_PrivateKey_ex(t->s.key, NULL, 0, NULL, libctx, NULL); |
c49e0b04 | 3635 | if (pkey == NULL && !key_unsupported()) { |
1bf2cc23 | 3636 | EVP_PKEY_free(pkey); |
ae269dd8 | 3637 | TEST_info("Can't read private key %s", pp->value); |
8fe3127c | 3638 | TEST_openssl_errors(); |
c49e0b04 RS |
3639 | return 0; |
3640 | } | |
3641 | klist = &private_keys; | |
4665244c | 3642 | } else if (strcmp(pp->key, "PublicKey") == 0) { |
5f2b7db0 | 3643 | pkey = PEM_read_bio_PUBKEY_ex(t->s.key, NULL, 0, NULL, libctx, NULL); |
c49e0b04 | 3644 | if (pkey == NULL && !key_unsupported()) { |
1bf2cc23 | 3645 | EVP_PKEY_free(pkey); |
ae269dd8 | 3646 | TEST_info("Can't read public key %s", pp->value); |
8fe3127c | 3647 | TEST_openssl_errors(); |
c49e0b04 RS |
3648 | return 0; |
3649 | } | |
3650 | klist = &public_keys; | |
4665244c | 3651 | } else if (strcmp(pp->key, "PrivateKeyRaw") == 0 |
1287dabd | 3652 | || strcmp(pp->key, "PublicKeyRaw") == 0) { |
4665244c MC |
3653 | char *strnid = NULL, *keydata = NULL; |
3654 | unsigned char *keybin; | |
3655 | size_t keylen; | |
3656 | int nid; | |
3657 | ||
3658 | if (strcmp(pp->key, "PrivateKeyRaw") == 0) | |
3659 | klist = &private_keys; | |
3660 | else | |
3661 | klist = &public_keys; | |
3662 | ||
3663 | strnid = strchr(pp->value, ':'); | |
3664 | if (strnid != NULL) { | |
3665 | *strnid++ = '\0'; | |
3666 | keydata = strchr(strnid, ':'); | |
3667 | if (keydata != NULL) | |
3668 | *keydata++ = '\0'; | |
3669 | } | |
3670 | if (keydata == NULL) { | |
3671 | TEST_info("Failed to parse %s value", pp->key); | |
3672 | return 0; | |
3673 | } | |
3674 | ||
3675 | nid = OBJ_txt2nid(strnid); | |
3676 | if (nid == NID_undef) { | |
5ccada09 | 3677 | TEST_info("Unrecognised algorithm NID"); |
4665244c MC |
3678 | return 0; |
3679 | } | |
3680 | if (!parse_bin(keydata, &keybin, &keylen)) { | |
3681 | TEST_info("Failed to create binary key"); | |
3682 | return 0; | |
3683 | } | |
3684 | if (klist == &private_keys) | |
d8652be0 MC |
3685 | pkey = EVP_PKEY_new_raw_private_key_ex(libctx, strnid, NULL, keybin, |
3686 | keylen); | |
4665244c | 3687 | else |
d8652be0 MC |
3688 | pkey = EVP_PKEY_new_raw_public_key_ex(libctx, strnid, NULL, keybin, |
3689 | keylen); | |
66a925ea | 3690 | if (pkey == NULL && !key_unsupported()) { |
4665244c MC |
3691 | TEST_info("Can't read %s data", pp->key); |
3692 | OPENSSL_free(keybin); | |
3693 | TEST_openssl_errors(); | |
3694 | return 0; | |
3695 | } | |
3696 | OPENSSL_free(keybin); | |
5ccada09 SL |
3697 | } else if (strcmp(pp->key, "Availablein") == 0) { |
3698 | if (!prov_available(pp->value)) { | |
3699 | TEST_info("skipping, '%s' provider not available: %s:%d", | |
3700 | pp->value, t->s.test_file, t->s.start); | |
3701 | t->skip = 1; | |
3702 | return 0; | |
3703 | } | |
3704 | skip_availablein++; | |
3705 | pp++; | |
3706 | goto start; | |
c49e0b04 RS |
3707 | } |
3708 | ||
3709 | /* If we have a key add to list */ | |
3710 | if (klist != NULL) { | |
ae269dd8 RS |
3711 | if (find_key(NULL, pp->value, *klist)) { |
3712 | TEST_info("Duplicate key %s", pp->value); | |
c49e0b04 RS |
3713 | return 0; |
3714 | } | |
ae269dd8 | 3715 | if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key)))) |
c49e0b04 | 3716 | return 0; |
ae269dd8 | 3717 | key->name = take_value(pp); |
c49e0b04 RS |
3718 | key->key = pkey; |
3719 | key->next = *klist; | |
3720 | *klist = key; | |
3721 | ||
3722 | /* Go back and start a new stanza. */ | |
5ccada09 | 3723 | if ((t->s.numpairs - skip_availablein) != 1) |
ae269dd8 | 3724 | TEST_info("Line %d: missing blank line\n", t->s.curr); |
c49e0b04 RS |
3725 | goto top; |
3726 | } | |
3727 | ||
ae269dd8 RS |
3728 | /* Find the test, based on first keyword. */ |
3729 | if (!TEST_ptr(t->meth = find_test(pp->key))) | |
3730 | return 0; | |
3731 | if (!t->meth->init(t, pp->value)) { | |
3732 | TEST_error("unknown %s: %s\n", pp->key, pp->value); | |
3733 | return 0; | |
c49e0b04 RS |
3734 | } |
3735 | if (t->skip == 1) { | |
ae269dd8 RS |
3736 | /* TEST_info("skipping %s %s", pp->key, pp->value); */ |
3737 | return 0; | |
c49e0b04 RS |
3738 | } |
3739 | ||
5ccada09 | 3740 | for (pp++, i = 1; i < (t->s.numpairs - skip_availablein); pp++, i++) { |
7a810fac SL |
3741 | if (strcmp(pp->key, "Securitycheck") == 0) { |
3742 | #if defined(OPENSSL_NO_FIPS_SECURITYCHECKS) | |
991a6bb5 SL |
3743 | #else |
3744 | if (!securitycheck_enabled()) | |
7a810fac | 3745 | #endif |
991a6bb5 SL |
3746 | { |
3747 | TEST_info("skipping, Securitycheck is disabled: %s:%d", | |
3748 | t->s.test_file, t->s.start); | |
3749 | t->skip = 1; | |
3750 | return 0; | |
3751 | } | |
7a810fac | 3752 | } else if (strcmp(pp->key, "Availablein") == 0) { |
5ccada09 SL |
3753 | TEST_info("Line %d: 'Availablein' should be the first option", |
3754 | t->s.curr); | |
3755 | return 0; | |
8453096e | 3756 | } else if (strcmp(pp->key, "Result") == 0) { |
c49e0b04 | 3757 | if (t->expected_err != NULL) { |
ae269dd8 RS |
3758 | TEST_info("Line %d: multiple result lines", t->s.curr); |
3759 | return 0; | |
c49e0b04 | 3760 | } |
ae269dd8 RS |
3761 | t->expected_err = take_value(pp); |
3762 | } else if (strcmp(pp->key, "Function") == 0) { | |
aac96e27 | 3763 | /* Ignore old line. */ |
ae269dd8 | 3764 | } else if (strcmp(pp->key, "Reason") == 0) { |
c49e0b04 | 3765 | if (t->reason != NULL) { |
ae269dd8 RS |
3766 | TEST_info("Line %d: multiple reason lines", t->s.curr); |
3767 | return 0; | |
c49e0b04 | 3768 | } |
ae269dd8 | 3769 | t->reason = take_value(pp); |
c49e0b04 RS |
3770 | } else { |
3771 | /* Must be test specific line: try to parse it */ | |
ae269dd8 | 3772 | int rv = t->meth->parse(t, pp->key, pp->value); |
c49e0b04 RS |
3773 | |
3774 | if (rv == 0) { | |
ae269dd8 RS |
3775 | TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key); |
3776 | return 0; | |
c49e0b04 RS |
3777 | } |
3778 | if (rv < 0) { | |
ce5d64c7 RL |
3779 | TEST_info("Line %d: error processing keyword %s = %s\n", |
3780 | t->s.curr, pp->key, pp->value); | |
ae269dd8 | 3781 | return 0; |
c49e0b04 RS |
3782 | } |
3783 | } | |
3784 | } | |
3785 | ||
3786 | return 1; | |
c49e0b04 RS |
3787 | } |
3788 | ||
ae269dd8 | 3789 | static int run_file_tests(int i) |
6c5943c9 | 3790 | { |
ae269dd8 | 3791 | EVP_TEST *t; |
ad887416 | 3792 | const char *testfile = test_get_argument(i); |
c49e0b04 | 3793 | int c; |
6c5943c9 | 3794 | |
ae269dd8 | 3795 | if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t)))) |
6c5943c9 | 3796 | return 0; |
ad887416 | 3797 | if (!test_start_file(&t->s, testfile)) { |
ae269dd8 RS |
3798 | OPENSSL_free(t); |
3799 | return 0; | |
3800 | } | |
c49e0b04 | 3801 | |
ae269dd8 RS |
3802 | while (!BIO_eof(t->s.fp)) { |
3803 | c = parse(t); | |
d5e5e2ff SL |
3804 | if (t->skip) { |
3805 | t->s.numskip++; | |
c49e0b04 | 3806 | continue; |
d5e5e2ff | 3807 | } |
ae269dd8 RS |
3808 | if (c == 0 || !run_test(t)) { |
3809 | t->s.errors++; | |
c49e0b04 RS |
3810 | break; |
3811 | } | |
6c5943c9 | 3812 | } |
ae269dd8 RS |
3813 | test_end_file(&t->s); |
3814 | clear_test(t); | |
6c5943c9 | 3815 | |
6c5943c9 RS |
3816 | free_key_list(public_keys); |
3817 | free_key_list(private_keys); | |
ae269dd8 RS |
3818 | BIO_free(t->s.key); |
3819 | c = t->s.errors; | |
3820 | OPENSSL_free(t); | |
3821 | return c == 0; | |
6c5943c9 RS |
3822 | } |
3823 | ||
5ccada09 SL |
3824 | const OPTIONS *test_get_options(void) |
3825 | { | |
3826 | static const OPTIONS test_options[] = { | |
3827 | OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"), | |
3828 | { "config", OPT_CONFIG_FILE, '<', | |
3829 | "The configuration file to use for the libctx" }, | |
f56c9c7c | 3830 | { OPT_HELP_STR, 1, '-', "file\tFile to run tests on.\n" }, |
5ccada09 SL |
3831 | { NULL } |
3832 | }; | |
3833 | return test_options; | |
3834 | } | |
a43ce58f | 3835 | |
ad887416 | 3836 | int setup_tests(void) |
6c5943c9 | 3837 | { |
8d242823 | 3838 | size_t n; |
5ccada09 SL |
3839 | char *config_file = NULL; |
3840 | ||
3841 | OPTION_CHOICE o; | |
3842 | ||
3843 | while ((o = opt_next()) != OPT_EOF) { | |
3844 | switch (o) { | |
3845 | case OPT_CONFIG_FILE: | |
3846 | config_file = opt_arg(); | |
3847 | break; | |
3848 | case OPT_TEST_CASES: | |
3849 | break; | |
3850 | default: | |
3851 | case OPT_ERR: | |
3852 | return 0; | |
3853 | } | |
3854 | } | |
3855 | ||
3856 | /* | |
bca7ad6e | 3857 | * Load the provider via configuration into the created library context. |
5ccada09 | 3858 | * Load the 'null' provider into the default library context to ensure that |
3e6a0d57 | 3859 | * the tests do not fallback to using the default provider. |
5ccada09 | 3860 | */ |
bca7ad6e | 3861 | if (!test_get_libctx(&libctx, &prov_null, config_file, NULL, NULL)) |
5ccada09 | 3862 | return 0; |
8d242823 MC |
3863 | |
3864 | n = test_get_argument_count(); | |
a43ce58f | 3865 | if (n == 0) |
6c5943c9 | 3866 | return 0; |
6c5943c9 | 3867 | |
ad887416 P |
3868 | ADD_ALL_TESTS(run_file_tests, n); |
3869 | return 1; | |
6c5943c9 | 3870 | } |
5ccada09 SL |
3871 | |
3872 | void cleanup_tests(void) | |
3873 | { | |
3874 | OSSL_PROVIDER_unload(prov_null); | |
b4250010 | 3875 | OSSL_LIB_CTX_free(libctx); |
5ccada09 SL |
3876 | } |
3877 | ||
5ccada09 SL |
3878 | static int is_digest_disabled(const char *name) |
3879 | { | |
3880 | #ifdef OPENSSL_NO_BLAKE2 | |
747adb6a | 3881 | if (HAS_CASE_PREFIX(name, "BLAKE")) |
5ccada09 SL |
3882 | return 1; |
3883 | #endif | |
3884 | #ifdef OPENSSL_NO_MD2 | |
3885 | if (strcasecmp(name, "MD2") == 0) | |
3886 | return 1; | |
3887 | #endif | |
3888 | #ifdef OPENSSL_NO_MDC2 | |
3889 | if (strcasecmp(name, "MDC2") == 0) | |
3890 | return 1; | |
3891 | #endif | |
3892 | #ifdef OPENSSL_NO_MD4 | |
3893 | if (strcasecmp(name, "MD4") == 0) | |
3894 | return 1; | |
3895 | #endif | |
3896 | #ifdef OPENSSL_NO_MD5 | |
3897 | if (strcasecmp(name, "MD5") == 0) | |
3898 | return 1; | |
3899 | #endif | |
3900 | #ifdef OPENSSL_NO_RMD160 | |
3901 | if (strcasecmp(name, "RIPEMD160") == 0) | |
3902 | return 1; | |
3903 | #endif | |
3904 | #ifdef OPENSSL_NO_SM3 | |
3905 | if (strcasecmp(name, "SM3") == 0) | |
3906 | return 1; | |
3907 | #endif | |
3908 | #ifdef OPENSSL_NO_WHIRLPOOL | |
3909 | if (strcasecmp(name, "WHIRLPOOL") == 0) | |
3910 | return 1; | |
3911 | #endif | |
3912 | return 0; | |
3913 | } | |
3914 | ||
3915 | static int is_pkey_disabled(const char *name) | |
3916 | { | |
5ccada09 | 3917 | #ifdef OPENSSL_NO_EC |
747adb6a | 3918 | if (HAS_CASE_PREFIX(name, "EC")) |
5ccada09 SL |
3919 | return 1; |
3920 | #endif | |
3921 | #ifdef OPENSSL_NO_DH | |
747adb6a | 3922 | if (HAS_CASE_PREFIX(name, "DH")) |
5ccada09 SL |
3923 | return 1; |
3924 | #endif | |
3925 | #ifdef OPENSSL_NO_DSA | |
747adb6a | 3926 | if (HAS_CASE_PREFIX(name, "DSA")) |
5ccada09 SL |
3927 | return 1; |
3928 | #endif | |
3929 | return 0; | |
3930 | } | |
3931 | ||
3932 | static int is_mac_disabled(const char *name) | |
3933 | { | |
3934 | #ifdef OPENSSL_NO_BLAKE2 | |
747adb6a DDO |
3935 | if (HAS_CASE_PREFIX(name, "BLAKE2BMAC") |
3936 | || HAS_CASE_PREFIX(name, "BLAKE2SMAC")) | |
5ccada09 SL |
3937 | return 1; |
3938 | #endif | |
3939 | #ifdef OPENSSL_NO_CMAC | |
747adb6a | 3940 | if (HAS_CASE_PREFIX(name, "CMAC")) |
5ccada09 SL |
3941 | return 1; |
3942 | #endif | |
3943 | #ifdef OPENSSL_NO_POLY1305 | |
747adb6a | 3944 | if (HAS_CASE_PREFIX(name, "Poly1305")) |
5ccada09 SL |
3945 | return 1; |
3946 | #endif | |
3947 | #ifdef OPENSSL_NO_SIPHASH | |
747adb6a | 3948 | if (HAS_CASE_PREFIX(name, "SipHash")) |
5ccada09 SL |
3949 | return 1; |
3950 | #endif | |
3951 | return 0; | |
3952 | } | |
3953 | static int is_kdf_disabled(const char *name) | |
3954 | { | |
3955 | #ifdef OPENSSL_NO_SCRYPT | |
747adb6a | 3956 | if (HAS_CASE_SUFFIX(name, "SCRYPT")) |
5ccada09 SL |
3957 | return 1; |
3958 | #endif | |
5ccada09 SL |
3959 | return 0; |
3960 | } | |
3961 | ||
3962 | static int is_cipher_disabled(const char *name) | |
3963 | { | |
3964 | #ifdef OPENSSL_NO_ARIA | |
747adb6a | 3965 | if (HAS_CASE_PREFIX(name, "ARIA")) |
5ccada09 SL |
3966 | return 1; |
3967 | #endif | |
3968 | #ifdef OPENSSL_NO_BF | |
747adb6a | 3969 | if (HAS_CASE_PREFIX(name, "BF")) |
5ccada09 SL |
3970 | return 1; |
3971 | #endif | |
3972 | #ifdef OPENSSL_NO_CAMELLIA | |
747adb6a | 3973 | if (HAS_CASE_PREFIX(name, "CAMELLIA")) |
5ccada09 SL |
3974 | return 1; |
3975 | #endif | |
3976 | #ifdef OPENSSL_NO_CAST | |
747adb6a | 3977 | if (HAS_CASE_PREFIX(name, "CAST")) |
5ccada09 SL |
3978 | return 1; |
3979 | #endif | |
3980 | #ifdef OPENSSL_NO_CHACHA | |
747adb6a | 3981 | if (HAS_CASE_PREFIX(name, "CHACHA")) |
5ccada09 SL |
3982 | return 1; |
3983 | #endif | |
3984 | #ifdef OPENSSL_NO_POLY1305 | |
747adb6a | 3985 | if (HAS_CASE_SUFFIX(name, "Poly1305")) |
5ccada09 SL |
3986 | return 1; |
3987 | #endif | |
3988 | #ifdef OPENSSL_NO_DES | |
747adb6a | 3989 | if (HAS_CASE_PREFIX(name, "DES")) |
5ccada09 | 3990 | return 1; |
747adb6a | 3991 | if (HAS_CASE_SUFFIX(name, "3DESwrap")) |
89cccbea | 3992 | return 1; |
5ccada09 SL |
3993 | #endif |
3994 | #ifdef OPENSSL_NO_OCB | |
747adb6a | 3995 | if (HAS_CASE_SUFFIX(name, "OCB")) |
5ccada09 SL |
3996 | return 1; |
3997 | #endif | |
3998 | #ifdef OPENSSL_NO_IDEA | |
747adb6a | 3999 | if (HAS_CASE_PREFIX(name, "IDEA")) |
5ccada09 SL |
4000 | return 1; |
4001 | #endif | |
4002 | #ifdef OPENSSL_NO_RC2 | |
747adb6a | 4003 | if (HAS_CASE_PREFIX(name, "RC2")) |
5ccada09 SL |
4004 | return 1; |
4005 | #endif | |
4006 | #ifdef OPENSSL_NO_RC4 | |
747adb6a | 4007 | if (HAS_CASE_PREFIX(name, "RC4")) |
5ccada09 SL |
4008 | return 1; |
4009 | #endif | |
4010 | #ifdef OPENSSL_NO_RC5 | |
747adb6a | 4011 | if (HAS_CASE_PREFIX(name, "RC5")) |
5ccada09 SL |
4012 | return 1; |
4013 | #endif | |
4014 | #ifdef OPENSSL_NO_SEED | |
747adb6a | 4015 | if (HAS_CASE_PREFIX(name, "SEED")) |
5ccada09 SL |
4016 | return 1; |
4017 | #endif | |
4018 | #ifdef OPENSSL_NO_SIV | |
747adb6a | 4019 | if (HAS_CASE_SUFFIX(name, "SIV")) |
5ccada09 SL |
4020 | return 1; |
4021 | #endif | |
4022 | #ifdef OPENSSL_NO_SM4 | |
747adb6a | 4023 | if (HAS_CASE_PREFIX(name, "SM4")) |
5ccada09 SL |
4024 | return 1; |
4025 | #endif | |
4026 | return 0; | |
4027 | } |