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