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