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