41e83ab4851249c2006a77fe3cb1d2ba98c3f43b
[openssl.git] / providers / implementations / rands / test_rng.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 <openssl/core_numbers.h>
12 #include <openssl/e_os2.h>
13 #include <openssl/params.h>
14 #include "prov/providercommon.h"
15 #include "prov/provider_ctx.h"
16 #include "prov/provider_util.h"
17 #include "prov/implementations.h"
18 #include "drbg_local.h"
19
20 static OSSL_OP_rand_newctx_fn test_rng_new_wrapper;
21 static OSSL_OP_rand_freectx_fn test_rng_free;
22 static OSSL_OP_rand_instantiate_fn test_rng_instantiate_wrapper;
23 static OSSL_OP_rand_uninstantiate_fn test_rng_uninstantiate_wrapper;
24 static OSSL_OP_rand_generate_fn test_rng_generate_wrapper;
25 static OSSL_OP_rand_reseed_fn test_rng_reseed_wrapper;
26 static OSSL_OP_rand_nonce_fn test_rng_nonce;
27 static OSSL_OP_rand_settable_ctx_params_fn test_rng_settable_ctx_params;
28 static OSSL_OP_rand_set_ctx_params_fn test_rng_set_ctx_params;
29 static OSSL_OP_rand_gettable_ctx_params_fn test_rng_gettable_ctx_params;
30 static OSSL_OP_rand_get_ctx_params_fn test_rng_get_ctx_params;
31 static OSSL_OP_rand_verify_zeroization_fn test_rng_verify_zeroization;
32
33 typedef struct {
34     unsigned char *entropy, *nonce;
35     size_t entropy_len, entropy_pos, nonce_len;
36     unsigned int strength;
37 } PROV_TEST_RNG;
38
39 static int test_rng_new(PROV_DRBG *ctx, int secure)
40 {
41     PROV_TEST_RNG *t;
42
43     t = OPENSSL_zalloc(sizeof(*t));
44     if (t == NULL)
45         return 0;
46     ctx->data = t;
47     ctx->seedlen = INT_MAX;
48     ctx->max_entropylen = INT_MAX;
49     ctx->max_noncelen = INT_MAX;
50     ctx->max_perslen = INT_MAX;
51     ctx->max_adinlen = INT_MAX;
52     ctx->max_request = INT_MAX;
53     ctx->strength = 1024;
54     return 1;
55 }
56
57 static void *test_rng_new_wrapper(void *provctx, int secure, void *parent,
58                                    const OSSL_DISPATCH *parent_dispatch)
59 {
60     return prov_rand_drbg_new(provctx, secure, parent, parent_dispatch,
61                               &test_rng_new);
62 }
63
64 static void test_rng_free(void *vdrbg)
65 {
66     PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
67     PROV_TEST_RNG *t = (PROV_TEST_RNG *)drbg->data;
68
69     OPENSSL_free(t->entropy);
70     OPENSSL_free(t->nonce);
71     OPENSSL_free(drbg->data);
72     prov_rand_drbg_free(drbg);
73 }
74
75 static int test_rng_instantiate(PROV_DRBG *drbg,
76                                 const unsigned char *ent, size_t ent_len,
77                                 const unsigned char *nonce, size_t nonce_len,
78                                 const unsigned char *pstr, size_t pstr_len)
79 {
80     PROV_TEST_RNG *t = (PROV_TEST_RNG *)drbg->data;
81
82     if (ent != NULL && (ent_len < drbg->min_entropylen
83                         || ent_len >= drbg->max_entropylen))
84         return 0;
85     if (nonce != NULL && (nonce_len < drbg->min_noncelen
86                         || nonce_len >= drbg->max_noncelen))
87         return 0;
88     if (pstr != NULL && pstr_len >= drbg->max_perslen)
89         return 0;
90
91     t->entropy_pos = 0;
92     return 1;
93 }
94
95 static int test_rng_instantiate_wrapper(void *vdrbg, unsigned int strength,
96                                         int prediction_resistance,
97                                         const unsigned char *pstr,
98                                         size_t pstr_len)
99 {
100     PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
101
102     if (pstr != NULL && pstr_len >= drbg->max_perslen)
103         return 0;
104
105     return PROV_DRBG_instantiate(drbg, strength, prediction_resistance,
106                                  pstr, pstr_len);
107 }
108
109 static int test_rng_uninstantiate(PROV_DRBG *drbg)
110 {
111     PROV_TEST_RNG *t = (PROV_TEST_RNG *)drbg->data;
112
113     t->entropy_pos = 0;
114     return 1;
115 }
116
117 static int test_rng_uninstantiate_wrapper(void *vdrbg)
118 {
119     return test_rng_uninstantiate((PROV_DRBG *)vdrbg);
120 }
121
122 static int test_rng_generate(PROV_DRBG *drbg,
123                              unsigned char *out, size_t outlen,
124                              const unsigned char *adin, size_t adin_len)
125 {
126     PROV_TEST_RNG *t = (PROV_TEST_RNG *)drbg->data;
127     size_t i;
128
129     if (t->entropy == NULL || (adin != NULL && adin_len >= drbg->max_adinlen))
130         return 0;
131
132     for (i = 0; i < outlen; i++) {
133         out[i] = t->entropy[t->entropy_pos++];
134         if (t->entropy_pos >= t->entropy_len)
135             break;
136     }
137     return 1;
138 }
139
140 static int test_rng_generate_wrapper
141     (void *vdrbg, unsigned char *out, size_t outlen,
142       unsigned int strength, int prediction_resistance,
143       const unsigned char *adin, size_t adin_len)
144 {
145     PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
146
147     if (strength > drbg->strength)
148         return 0;
149     return test_rng_generate(drbg, out, outlen, adin, adin_len);
150 }
151
152 static int test_rng_reseed(PROV_DRBG *drbg,
153                             const unsigned char *ent, size_t ent_len,
154                             const unsigned char *adin, size_t adin_len)
155 {
156     if (ent != NULL && (ent_len < drbg->min_entropylen
157                         || ent_len >= drbg->max_entropylen))
158         return 0;
159     if (adin != NULL && adin_len >= drbg->max_adinlen)
160         return 0;
161
162     return 1;
163 }
164
165 static int test_rng_reseed_wrapper(void *vdrbg, int prediction_resistance,
166                                    const unsigned char *ent, size_t ent_len,
167                                    const unsigned char *adin, size_t adin_len)
168 {
169     return test_rng_reseed((PROV_DRBG *)vdrbg, ent, ent_len, adin, adin_len);
170 }
171
172 static void *test_rng_new_wrapper(void *provctx, void *parent,
173                                    const OSSL_DISPATCH *parent_dispatch)
174 {
175     return prov_rand_drbg_new(provctx, parent, parent_dispatch,
176                               &test_rng_new, &test_rng_instantiate,
177                               &test_rng_uninstantiate, &test_rng_reseed,
178                               &test_rng_generate);
179 }
180
181 static size_t test_rng_nonce(void *vdrbg, unsigned char *out,
182                              unsigned int strength, size_t min_noncelen,
183                              size_t max_noncelen)
184 {
185     PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
186     PROV_TEST_RNG *t = (PROV_TEST_RNG *)drbg->data;
187
188     if (t->nonce == NULL
189             || strength > drbg->strength
190             || min_noncelen > t->nonce_len
191             || max_noncelen < t->nonce_len)
192         return 0;
193
194     if (out != NULL)
195         memcpy(out, t->nonce, t->nonce_len);
196     return t->nonce_len;
197 }
198
199 static int test_rng_get_ctx_params(void *vdrbg, OSSL_PARAM params[])
200 {
201     PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
202
203     return drbg_get_ctx_params(drbg, params);
204 }
205
206 static const OSSL_PARAM *test_rng_gettable_ctx_params(void)
207 {
208     static const OSSL_PARAM known_gettable_ctx_params[] = {
209         OSSL_PARAM_DRBG_GETABLE_CTX_COMMON,
210         OSSL_PARAM_END
211     };
212     return known_gettable_ctx_params;
213 }
214
215 static int set_size_t(const OSSL_PARAM *params, const char *name,
216                             size_t *val)
217 {
218     const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, name);
219
220     return p == NULL || OSSL_PARAM_get_size_t(p, val);
221 }
222
223 static int test_rng_set_ctx_params(void *vdrbg, const OSSL_PARAM params[])
224 {
225     PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
226     PROV_TEST_RNG *t = (PROV_TEST_RNG *)drbg->data;
227     const OSSL_PARAM *p;
228     void *ptr = NULL;
229     size_t size = 0;
230     unsigned int uint;
231
232     p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_STRENGTH);
233     if (p != NULL && !OSSL_PARAM_get_uint(p, &drbg->strength))
234         return 0;
235
236     p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_TEST_ENTROPY);
237     if (p != NULL) {
238         if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))
239             return 0;
240         OPENSSL_free(t->entropy);
241         t->entropy = ptr;
242         t->entropy_len = size;
243         t->entropy_pos = 0;
244         ptr = NULL;
245     }
246
247     p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_TEST_NONCE);
248     if (p != NULL) {
249         if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))
250             return 0;
251         OPENSSL_free(t->nonce);
252         t->nonce = ptr;
253         t->nonce_len = size;
254     }
255
256     p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_RESEED_CTR);
257     if (p != NULL) {
258         if (!OSSL_PARAM_get_uint(p, &uint))
259             return 0;
260         tsan_store(&drbg->reseed_counter, uint);
261     }
262
263     p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_RESEED_TIME);
264     if (p != NULL && !OSSL_PARAM_get_time_t(p, &drbg->reseed_time))
265         return 0;
266
267     if (!set_size_t(params, OSSL_DRBG_PARAM_MAX_REQUEST, &drbg->max_request)
268             || !set_size_t(params, OSSL_DRBG_PARAM_MIN_ENTROPYLEN,
269                            &drbg->min_entropylen)
270             || !set_size_t(params, OSSL_DRBG_PARAM_MAX_ENTROPYLEN,
271                            &drbg->max_entropylen)
272             || !set_size_t(params, OSSL_DRBG_PARAM_MIN_NONCELEN,
273                            &drbg->min_noncelen)
274             || !set_size_t(params, OSSL_DRBG_PARAM_MAX_NONCELEN,
275                            &drbg->max_noncelen)
276             || !set_size_t(params, OSSL_DRBG_PARAM_MAX_PERSLEN,
277                            &drbg->max_perslen)
278             || !set_size_t(params, OSSL_DRBG_PARAM_MAX_ADINLEN,
279                            &drbg->max_adinlen))
280         return 0;
281     return drbg_set_ctx_params(drbg, params);
282 }
283
284 static const OSSL_PARAM *test_rng_settable_ctx_params(void)
285 {
286     static const OSSL_PARAM known_settable_ctx_params[] = {
287         OSSL_PARAM_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY, NULL, 0),
288         OSSL_PARAM_octet_string(OSSL_RAND_PARAM_TEST_NONCE, NULL, 0),
289         OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),
290         OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_REQUEST, NULL),
291         OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MIN_ENTROPYLEN, NULL),
292         OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_ENTROPYLEN, NULL),
293         OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MIN_NONCELEN, NULL),
294         OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_NONCELEN, NULL),
295         OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_PERSLEN, NULL),
296         OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_ADINLEN, NULL),
297         OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_CTR, NULL),
298         OSSL_PARAM_time_t(OSSL_DRBG_PARAM_RESEED_TIME, NULL),
299         OSSL_PARAM_DRBG_SETABLE_CTX_COMMON,
300         OSSL_PARAM_END
301     };
302     return known_settable_ctx_params;
303 }
304
305 static int test_rng_verify_zeroization(void *vdrbg)
306 {
307     return 1;
308 }
309
310 const OSSL_DISPATCH test_rng_functions[] = {
311     { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))test_rng_new_wrapper },
312     { OSSL_FUNC_RAND_FREECTX, (void(*)(void))test_rng_free },
313     { OSSL_FUNC_RAND_INSTANTIATE,
314       (void(*)(void))test_rng_instantiate_wrapper },
315     { OSSL_FUNC_RAND_UNINSTANTIATE,
316       (void(*)(void))test_rng_uninstantiate_wrapper },
317     { OSSL_FUNC_RAND_GENERATE, (void(*)(void))test_rng_generate_wrapper },
318     { OSSL_FUNC_RAND_RESEED, (void(*)(void))test_rng_reseed_wrapper },
319     { OSSL_FUNC_RAND_NONCE, (void(*)(void))test_rng_nonce },
320     { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))drbg_enable_locking },
321     { OSSL_FUNC_RAND_LOCK, (void(*)(void))drbg_lock },
322     { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))drbg_unlock },
323     { OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS,
324       (void(*)(void))test_rng_settable_ctx_params },
325     { OSSL_FUNC_RAND_SET_CTX_PARAMS, (void(*)(void))test_rng_set_ctx_params },
326     { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
327       (void(*)(void))test_rng_gettable_ctx_params },
328     { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))test_rng_get_ctx_params },
329     { OSSL_FUNC_RAND_VERIFY_ZEROIZATION,
330       (void(*)(void))test_rng_verify_zeroization },
331     { 0, NULL }
332 };