prov: add extra params argument to KDF implementations
[openssl.git] / providers / implementations / rands / test_rng.c
1 /*
2  * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <string.h>
11 #include <openssl/core_dispatch.h>
12 #include <openssl/e_os2.h>
13 #include <openssl/params.h>
14 #include <openssl/core_names.h>
15 #include <openssl/evp.h>
16 #include <openssl/err.h>
17 #include <openssl/randerr.h>
18 #include "prov/providercommon.h"
19 #include "prov/provider_ctx.h"
20 #include "prov/provider_util.h"
21 #include "prov/implementations.h"
22
23 static OSSL_FUNC_rand_newctx_fn test_rng_new;
24 static OSSL_FUNC_rand_freectx_fn test_rng_free;
25 static OSSL_FUNC_rand_instantiate_fn test_rng_instantiate;
26 static OSSL_FUNC_rand_uninstantiate_fn test_rng_uninstantiate;
27 static OSSL_FUNC_rand_generate_fn test_rng_generate;
28 static OSSL_FUNC_rand_reseed_fn test_rng_reseed;
29 static OSSL_FUNC_rand_nonce_fn test_rng_nonce;
30 static OSSL_FUNC_rand_settable_ctx_params_fn test_rng_settable_ctx_params;
31 static OSSL_FUNC_rand_set_ctx_params_fn test_rng_set_ctx_params;
32 static OSSL_FUNC_rand_gettable_ctx_params_fn test_rng_gettable_ctx_params;
33 static OSSL_FUNC_rand_get_ctx_params_fn test_rng_get_ctx_params;
34 static OSSL_FUNC_rand_verify_zeroization_fn test_rng_verify_zeroization;
35 static OSSL_FUNC_rand_enable_locking_fn test_rng_enable_locking;
36 static OSSL_FUNC_rand_lock_fn test_rng_lock;
37 static OSSL_FUNC_rand_unlock_fn test_rng_unlock;
38 static OSSL_FUNC_rand_get_seed_fn test_rng_get_seed;
39
40 typedef struct {
41     void *provctx;
42     int state;
43     unsigned int strength;
44     size_t max_request;
45     unsigned char *entropy, *nonce;
46     size_t entropy_len, entropy_pos, nonce_len;
47     CRYPTO_RWLOCK *lock;
48 } PROV_TEST_RNG;
49
50 static void *test_rng_new(void *provctx, void *parent,
51                           const OSSL_DISPATCH *parent_dispatch)
52 {
53     PROV_TEST_RNG *t;
54
55     if (parent != NULL)
56         return NULL;
57
58     t = OPENSSL_zalloc(sizeof(*t));
59     if (t == NULL)
60         return NULL;
61
62     t->max_request = INT_MAX;
63     t->provctx = provctx;
64     t->state = EVP_RAND_STATE_UNINITIALISED;
65     return t;
66 }
67
68 static void test_rng_free(void *vtest)
69 {
70     PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
71
72     if (t == NULL)
73         return;
74     OPENSSL_free(t->entropy);
75     OPENSSL_free(t->nonce);
76     CRYPTO_THREAD_lock_free(t->lock);
77     OPENSSL_free(t);
78 }
79
80 static int test_rng_instantiate(void *vtest, unsigned int strength,
81                                 int prediction_resistance,
82                                 const unsigned char *pstr, size_t pstr_len)
83 {
84     PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
85
86     if (strength > t->strength)
87         return 0;
88
89     t->state = EVP_RAND_STATE_READY;
90     t->entropy_pos = 0;
91
92     return 1;
93 }
94
95 static int test_rng_uninstantiate(void *vtest)
96 {
97     PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
98
99     t->entropy_pos = 0;
100     t->state = EVP_RAND_STATE_UNINITIALISED;
101     return 1;
102 }
103
104 static int test_rng_generate(void *vtest, unsigned char *out, size_t outlen,
105                              unsigned int strength, int prediction_resistance,
106                              const unsigned char *adin, size_t adin_len)
107 {
108     PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
109     size_t i;
110
111     if (strength > t->strength)
112         return 0;
113
114     for (i = 0; i < outlen; i++) {
115         out[i] = t->entropy[t->entropy_pos++];
116         if (t->entropy_pos >= t->entropy_len)
117             break;
118     }
119     return 1;
120 }
121
122 static int test_rng_reseed(ossl_unused void *vtest,
123                            ossl_unused int prediction_resistance,
124                            ossl_unused const unsigned char *ent,
125                            ossl_unused size_t ent_len,
126                            ossl_unused const unsigned char *adin,
127                            ossl_unused size_t adin_len)
128 {
129     return 1;
130 }
131
132 static size_t test_rng_nonce(void *vtest, unsigned char *out,
133                              unsigned int strength,
134                              ossl_unused size_t min_noncelen,
135                              ossl_unused size_t max_noncelen)
136 {
137     PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
138
139     if (t->nonce == NULL || strength > t->strength)
140         return 0;
141
142     if (out != NULL)
143         memcpy(out, t->nonce, t->nonce_len);
144     return t->nonce_len;
145 }
146
147 static int test_rng_get_ctx_params(void *vtest, OSSL_PARAM params[])
148 {
149     PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
150     OSSL_PARAM *p;
151
152     p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);
153     if (p != NULL && !OSSL_PARAM_set_int(p, t->state))
154         return 0;
155
156     p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);
157     if (p != NULL && !OSSL_PARAM_set_int(p, t->strength))
158         return 0;
159
160     p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);
161     if (p != NULL && !OSSL_PARAM_set_size_t(p, t->max_request))
162         return 0;
163     return 1;
164 }
165
166 static const OSSL_PARAM *test_rng_gettable_ctx_params(ossl_unused void *vtest,
167                                                       ossl_unused void *provctx)
168 {
169     static const OSSL_PARAM known_gettable_ctx_params[] = {
170         OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL),
171         OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),
172         OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),
173         OSSL_PARAM_END
174     };
175     return known_gettable_ctx_params;
176 }
177
178 static int test_rng_set_ctx_params(void *vtest, const OSSL_PARAM params[])
179 {
180     PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
181     const OSSL_PARAM *p;
182     void *ptr = NULL;
183     size_t size = 0;
184
185     p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_STRENGTH);
186     if (p != NULL && !OSSL_PARAM_get_uint(p, &t->strength))
187         return 0;
188
189     p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_TEST_ENTROPY);
190     if (p != NULL) {
191         if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))
192             return 0;
193         OPENSSL_free(t->entropy);
194         t->entropy = ptr;
195         t->entropy_len = size;
196         t->entropy_pos = 0;
197         ptr = NULL;
198     }
199
200     p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_TEST_NONCE);
201     if (p != NULL) {
202         if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))
203             return 0;
204         OPENSSL_free(t->nonce);
205         t->nonce = ptr;
206         t->nonce_len = size;
207     }
208
209     p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_MAX_REQUEST);
210     if (p != NULL  && !OSSL_PARAM_get_size_t(p, &t->max_request))
211         return 0;
212
213     return 1;
214 }
215
216 static const OSSL_PARAM *test_rng_settable_ctx_params(ossl_unused void *vtest,
217                                                       ossl_unused void *provctx)
218 {
219     static const OSSL_PARAM known_settable_ctx_params[] = {
220         OSSL_PARAM_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY, NULL, 0),
221         OSSL_PARAM_octet_string(OSSL_RAND_PARAM_TEST_NONCE, NULL, 0),
222         OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),
223         OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),
224         OSSL_PARAM_END
225     };
226     return known_settable_ctx_params;
227 }
228
229 static int test_rng_verify_zeroization(ossl_unused void *vtest)
230 {
231     return 1;
232 }
233
234 static size_t test_rng_get_seed(void *vtest, unsigned char **pout,
235                                 int entropy, size_t min_len, size_t max_len,
236                                 ossl_unused int prediction_resistance,
237                                 ossl_unused const unsigned char *adin,
238                                 ossl_unused size_t adin_len)
239 {
240     PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
241
242     *pout = t->entropy;
243     return  t->entropy_len > max_len ? max_len : t->entropy_len;
244 }
245
246 static int test_rng_enable_locking(void *vtest)
247 {
248     PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
249
250     if (t != NULL && t->lock == NULL) {
251         t->lock = CRYPTO_THREAD_lock_new();
252         if (t->lock == NULL) {
253             ERR_raise(ERR_LIB_PROV, RAND_R_FAILED_TO_CREATE_LOCK);
254             return 0;
255         }
256     }
257     return 1;
258 }
259
260 static int test_rng_lock(void *vtest)
261 {
262     PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
263
264     if (t == NULL || t->lock == NULL)
265         return 1;
266     return CRYPTO_THREAD_write_lock(t->lock);
267 }
268
269 static void test_rng_unlock(void *vtest)
270 {
271     PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
272
273     if (t != NULL && t->lock != NULL)
274         CRYPTO_THREAD_unlock(t->lock);
275 }
276
277 const OSSL_DISPATCH ossl_test_rng_functions[] = {
278     { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))test_rng_new },
279     { OSSL_FUNC_RAND_FREECTX, (void(*)(void))test_rng_free },
280     { OSSL_FUNC_RAND_INSTANTIATE,
281       (void(*)(void))test_rng_instantiate },
282     { OSSL_FUNC_RAND_UNINSTANTIATE,
283       (void(*)(void))test_rng_uninstantiate },
284     { OSSL_FUNC_RAND_GENERATE, (void(*)(void))test_rng_generate },
285     { OSSL_FUNC_RAND_RESEED, (void(*)(void))test_rng_reseed },
286     { OSSL_FUNC_RAND_NONCE, (void(*)(void))test_rng_nonce },
287     { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))test_rng_enable_locking },
288     { OSSL_FUNC_RAND_LOCK, (void(*)(void))test_rng_lock },
289     { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))test_rng_unlock },
290     { OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS,
291       (void(*)(void))test_rng_settable_ctx_params },
292     { OSSL_FUNC_RAND_SET_CTX_PARAMS, (void(*)(void))test_rng_set_ctx_params },
293     { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
294       (void(*)(void))test_rng_gettable_ctx_params },
295     { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))test_rng_get_ctx_params },
296     { OSSL_FUNC_RAND_VERIFY_ZEROIZATION,
297       (void(*)(void))test_rng_verify_zeroization },
298     { OSSL_FUNC_RAND_GET_SEED, (void(*)(void))test_rng_get_seed },
299     { 0, NULL }
300 };