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