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