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