VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.7/test/evp_test.c@ 106165

最後變更 在這個檔案從106165是 105949,由 vboxsync 提交於 2 月 前

openssl-3.1.7: Applied and adjusted our OpenSSL changes to 3.1.7. bugref:10757

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette