Add a test for d2i_AutoPrivateKey_ex with a non-default libctx
[openssl.git] / test / drbg_extra_test.c
1 /*
2  * Copyright 2020 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 #include <string.h>
11 #include "internal/nelem.h"
12 #include <openssl/crypto.h>
13 #include <openssl/err.h>
14 #include <openssl/rand.h>
15 #include <openssl/obj_mac.h>
16 #include <openssl/evp.h>
17 #include <openssl/aes.h>
18 #include "../crypto/rand/rand_local.h"
19
20 #include "testutil.h"
21 #include "drbg_extra_test.h"
22
23 static unsigned char zerobuff[32];
24
25 static size_t kat_entropy(RAND_DRBG *drbg, unsigned char **pout,
26                           int entropy, size_t min_len, size_t max_len,
27                           int prediction_resistance)
28 {
29     *pout = zerobuff;
30     return sizeof(zerobuff);
31 }
32
33 static size_t kat_nonce(RAND_DRBG *drbg, unsigned char **pout,
34                         int entropy, size_t min_len, size_t max_len)
35 {
36     *pout = zerobuff;
37     return sizeof(zerobuff);
38 }
39
40 static int run_extra_kat(const struct drbg_extra_kat *td)
41 {
42     unsigned long long i;
43     RAND_DRBG *drbg = NULL;
44     unsigned char buff[BUFFSIZE];
45     unsigned int flags = 0;
46     int failures = 0;
47
48     if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, flags, NULL)))
49         return 0;
50
51     /* Set deterministic entropy callback. */
52     if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
53                                            kat_nonce, NULL))) {
54         failures++;
55         goto err;
56     }
57
58     /* Set fixed reseed intervall. */
59     if (!TEST_true(RAND_DRBG_set_reseed_interval(drbg, RESEEDINTERVAL))) {
60         failures++;
61         goto err;
62     }
63
64     if (!TEST_true(RAND_DRBG_instantiate(drbg, NULL, 0)))
65         failures++;
66
67     for (i = 0; i < td->ngen; i++) {
68         if(!TEST_true(RAND_DRBG_generate(drbg, buff, sizeof(buff), 0, NULL,
69                                          0)))
70             failures++;
71     }
72
73     if (!TEST_true(RAND_DRBG_uninstantiate(drbg))
74         || !TEST_mem_eq(td->expected, sizeof(buff), buff, sizeof(buff)))
75         failures++;
76
77 err:
78     RAND_DRBG_uninstantiate(drbg);
79     RAND_DRBG_free(drbg);
80     return failures == 0;
81 }
82
83 static int test_extra_kats(int i)
84 {
85     return run_extra_kat(drbg_extra_test[i]);
86 }
87
88 int setup_tests(void)
89 {
90     ADD_ALL_TESTS(test_extra_kats, OSSL_NELEM(drbg_extra_test));
91     return 1;
92 }