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