1 /*
2  * Copyright 2020-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 #include <string.h>
11 #include <stdlib.h>
12 #include <openssl/core_dispatch.h>
13 #include <openssl/e_os2.h>
14 #include <openssl/params.h>
15 #include <openssl/core_names.h>
16 #include <openssl/evp.h>
17 #include <openssl/err.h>
18 #include <openssl/randerr.h>
19 #include "prov/securitycheck.h"
20 #include "prov/providercommon.h"
21 #include "prov/provider_ctx.h"
22 #include "prov/provider_util.h"
23 #include "prov/implementations.h"
24 
25 static OSSL_FUNC_rand_newctx_fn test_rng_new;
26 static OSSL_FUNC_rand_freectx_fn test_rng_free;
27 static OSSL_FUNC_rand_instantiate_fn test_rng_instantiate;
28 static OSSL_FUNC_rand_uninstantiate_fn test_rng_uninstantiate;
29 static OSSL_FUNC_rand_generate_fn test_rng_generate;
30 static OSSL_FUNC_rand_reseed_fn test_rng_reseed;
31 static OSSL_FUNC_rand_nonce_fn test_rng_nonce;
32 static OSSL_FUNC_rand_settable_ctx_params_fn test_rng_settable_ctx_params;
33 static OSSL_FUNC_rand_set_ctx_params_fn test_rng_set_ctx_params;
34 static OSSL_FUNC_rand_gettable_ctx_params_fn test_rng_gettable_ctx_params;
35 static OSSL_FUNC_rand_get_ctx_params_fn test_rng_get_ctx_params;
36 static OSSL_FUNC_rand_verify_zeroization_fn test_rng_verify_zeroization;
37 static OSSL_FUNC_rand_enable_locking_fn test_rng_enable_locking;
38 static OSSL_FUNC_rand_lock_fn test_rng_lock;
39 static OSSL_FUNC_rand_unlock_fn test_rng_unlock;
40 static OSSL_FUNC_rand_get_seed_fn test_rng_get_seed;
41 
42 typedef struct {
43     void *provctx;
44     unsigned int generate;
45     int state;
46     unsigned int strength;
47     size_t max_request;
48     unsigned char *entropy, *nonce;
49     size_t entropy_len, entropy_pos, nonce_len;
50     CRYPTO_RWLOCK *lock;
51     uint32_t seed;
52 } PROV_TEST_RNG;
53 
test_rng_new(void * provctx,void * parent,const OSSL_DISPATCH * parent_dispatch)54 static void *test_rng_new(void *provctx, void *parent,
55                           const OSSL_DISPATCH *parent_dispatch)
56 {
57     PROV_TEST_RNG *t;
58 
59     t = OPENSSL_zalloc(sizeof(*t));
60     if (t == NULL)
61         return NULL;
62 
63     t->max_request = INT_MAX;
64     t->provctx = provctx;
65     t->state = EVP_RAND_STATE_UNINITIALISED;
66     return t;
67 }
68 
test_rng_free(void * vtest)69 static void test_rng_free(void *vtest)
70 {
71     PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
72 
73     if (t == NULL)
74         return;
75     OPENSSL_free(t->entropy);
76     OPENSSL_free(t->nonce);
77     CRYPTO_THREAD_lock_free(t->lock);
78     OPENSSL_free(t);
79 }
80 
test_rng_instantiate(void * vtest,unsigned int strength,int prediction_resistance,const unsigned char * pstr,size_t pstr_len,const OSSL_PARAM params[])81 static int test_rng_instantiate(void *vtest, unsigned int strength,
82                                 int prediction_resistance,
83                                 const unsigned char *pstr, size_t pstr_len,
84                                 const OSSL_PARAM params[])
85 {
86     PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
87 
88     if (!test_rng_set_ctx_params(t, params) || strength > t->strength)
89         return 0;
90 
91     t->state = EVP_RAND_STATE_READY;
92     t->entropy_pos = 0;
93     t->seed = 221953166;    /* Value doesn't matter, so long as it isn't zero */
94 
95     return 1;
96 }
97 
test_rng_uninstantiate(void * vtest)98 static int test_rng_uninstantiate(void *vtest)
99 {
100     PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
101 
102     t->entropy_pos = 0;
103     t->state = EVP_RAND_STATE_UNINITIALISED;
104     return 1;
105 }
106 
gen_byte(PROV_TEST_RNG * t)107 static unsigned char gen_byte(PROV_TEST_RNG *t)
108 {
109     uint32_t n;
110 
111     /*
112      * Implement the 32 bit xorshift as suggested by George Marsaglia in:
113      *      https://doi.org/10.18637/jss.v008.i14
114      *
115      * This is a very fast PRNG so there is no need to extract bytes one at a
116      * time and use the entire value each time.
117      */
118     n = t->seed;
119     n ^= n << 13;
120     n ^= n >> 17;
121     n ^= n << 5;
122     t->seed = n;
123 
124     return n & 0xff;
125 }
126 
test_rng_generate(void * vtest,unsigned char * out,size_t outlen,unsigned int strength,int prediction_resistance,const unsigned char * adin,size_t adin_len)127 static int test_rng_generate(void *vtest, unsigned char *out, size_t outlen,
128                              unsigned int strength, int prediction_resistance,
129                              const unsigned char *adin, size_t adin_len)
130 {
131     PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
132     size_t i;
133 
134     if (strength > t->strength)
135         return 0;
136     if (t->generate) {
137         for (i = 0; i < outlen; i++)
138             out[i] = gen_byte(t);
139     } else {
140         if (t->entropy_len - t->entropy_pos < outlen)
141             return 0;
142 
143         memcpy(out, t->entropy + t->entropy_pos, outlen);
144         t->entropy_pos += outlen;
145     }
146     return 1;
147 }
148 
test_rng_reseed(ossl_unused void * vtest,ossl_unused int prediction_resistance,ossl_unused const unsigned char * ent,ossl_unused size_t ent_len,ossl_unused const unsigned char * adin,ossl_unused size_t adin_len)149 static int test_rng_reseed(ossl_unused void *vtest,
150                            ossl_unused int prediction_resistance,
151                            ossl_unused const unsigned char *ent,
152                            ossl_unused size_t ent_len,
153                            ossl_unused const unsigned char *adin,
154                            ossl_unused size_t adin_len)
155 {
156     return 1;
157 }
158 
test_rng_nonce(void * vtest,unsigned char * out,unsigned int strength,size_t min_noncelen,ossl_unused size_t max_noncelen)159 static size_t test_rng_nonce(void *vtest, unsigned char *out,
160                              unsigned int strength, size_t min_noncelen,
161                              ossl_unused size_t max_noncelen)
162 {
163     PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
164     size_t i;
165 
166     if (strength > t->strength)
167         return 0;
168 
169     if (t->generate) {
170         for (i = 0; i < min_noncelen; i++)
171             out[i] = gen_byte(t);
172         return min_noncelen;
173     }
174 
175     if (t->nonce == NULL)
176         return 0;
177     if (out != NULL)
178         memcpy(out, t->nonce, t->nonce_len);
179     return t->nonce_len;
180 }
181 
test_rng_get_ctx_params(void * vtest,OSSL_PARAM params[])182 static int test_rng_get_ctx_params(void *vtest, OSSL_PARAM params[])
183 {
184     PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
185     OSSL_PARAM *p;
186 
187     p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);
188     if (p != NULL && !OSSL_PARAM_set_int(p, t->state))
189         return 0;
190 
191     p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);
192     if (p != NULL && !OSSL_PARAM_set_int(p, t->strength))
193         return 0;
194 
195     p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);
196     if (p != NULL && !OSSL_PARAM_set_size_t(p, t->max_request))
197         return 0;
198 
199     p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_GENERATE);
200     if (p != NULL && !OSSL_PARAM_set_uint(p, t->generate))
201         return 0;
202 
203 #ifdef FIPS_MODULE
204     p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_FIPS_APPROVED_INDICATOR);
205     if (p != NULL && !OSSL_PARAM_set_int(p, 0))
206         return 0;
207 #endif  /* FIPS_MODULE */
208     return 1;
209 }
210 
test_rng_gettable_ctx_params(ossl_unused void * vtest,ossl_unused void * provctx)211 static const OSSL_PARAM *test_rng_gettable_ctx_params(ossl_unused void *vtest,
212                                                       ossl_unused void *provctx)
213 {
214     static const OSSL_PARAM known_gettable_ctx_params[] = {
215         OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL),
216         OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),
217         OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),
218         OSSL_PARAM_uint(OSSL_RAND_PARAM_GENERATE, NULL),
219         OSSL_FIPS_IND_GETTABLE_CTX_PARAM()
220         OSSL_PARAM_END
221     };
222     return known_gettable_ctx_params;
223 }
224 
test_rng_set_ctx_params(void * vtest,const OSSL_PARAM params[])225 static int test_rng_set_ctx_params(void *vtest, const OSSL_PARAM params[])
226 {
227     PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
228     const OSSL_PARAM *p;
229     void *ptr = NULL;
230     size_t size = 0;
231 
232     if (ossl_param_is_empty(params))
233         return 1;
234 
235     p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_STRENGTH);
236     if (p != NULL && !OSSL_PARAM_get_uint(p, &t->strength))
237         return 0;
238 
239     p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_TEST_ENTROPY);
240     if (p != NULL) {
241         if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))
242             return 0;
243         OPENSSL_free(t->entropy);
244         t->entropy = ptr;
245         t->entropy_len = size;
246         t->entropy_pos = 0;
247         ptr = NULL;
248     }
249 
250     p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_TEST_NONCE);
251     if (p != NULL) {
252         if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))
253             return 0;
254         OPENSSL_free(t->nonce);
255         t->nonce = ptr;
256         t->nonce_len = size;
257     }
258 
259     p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_MAX_REQUEST);
260     if (p != NULL && !OSSL_PARAM_get_size_t(p, &t->max_request))
261         return 0;
262 
263     p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_GENERATE);
264     if (p != NULL && !OSSL_PARAM_get_uint(p, &t->generate))
265         return 0;
266     return 1;
267 }
268 
test_rng_settable_ctx_params(ossl_unused void * vtest,ossl_unused void * provctx)269 static const OSSL_PARAM *test_rng_settable_ctx_params(ossl_unused void *vtest,
270                                                       ossl_unused void *provctx)
271 {
272     static const OSSL_PARAM known_settable_ctx_params[] = {
273         OSSL_PARAM_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY, NULL, 0),
274         OSSL_PARAM_octet_string(OSSL_RAND_PARAM_TEST_NONCE, NULL, 0),
275         OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),
276         OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),
277         OSSL_PARAM_uint(OSSL_RAND_PARAM_GENERATE, NULL),
278         OSSL_PARAM_END
279     };
280     return known_settable_ctx_params;
281 }
282 
test_rng_verify_zeroization(ossl_unused void * vtest)283 static int test_rng_verify_zeroization(ossl_unused void *vtest)
284 {
285     return 1;
286 }
287 
test_rng_get_seed(void * vtest,unsigned char ** pout,int entropy,size_t min_len,size_t max_len,ossl_unused int prediction_resistance,ossl_unused const unsigned char * adin,ossl_unused size_t adin_len)288 static size_t test_rng_get_seed(void *vtest, unsigned char **pout,
289                                 int entropy, size_t min_len, size_t max_len,
290                                 ossl_unused int prediction_resistance,
291                                 ossl_unused const unsigned char *adin,
292                                 ossl_unused size_t adin_len)
293 {
294     PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
295 
296     *pout = t->entropy;
297     return  t->entropy_len > max_len ? max_len : t->entropy_len;
298 }
299 
test_rng_enable_locking(void * vtest)300 static int test_rng_enable_locking(void *vtest)
301 {
302     PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
303 
304     if (t != NULL && t->lock == NULL) {
305         t->lock = CRYPTO_THREAD_lock_new();
306         if (t->lock == NULL) {
307             ERR_raise(ERR_LIB_PROV, RAND_R_FAILED_TO_CREATE_LOCK);
308             return 0;
309         }
310     }
311     return 1;
312 }
313 
test_rng_lock(void * vtest)314 static int test_rng_lock(void *vtest)
315 {
316     PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
317 
318     if (t == NULL || t->lock == NULL)
319         return 1;
320     return CRYPTO_THREAD_write_lock(t->lock);
321 }
322 
test_rng_unlock(void * vtest)323 static void test_rng_unlock(void *vtest)
324 {
325     PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
326 
327     if (t != NULL && t->lock != NULL)
328         CRYPTO_THREAD_unlock(t->lock);
329 }
330 
331 const OSSL_DISPATCH ossl_test_rng_functions[] = {
332     { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))test_rng_new },
333     { OSSL_FUNC_RAND_FREECTX, (void(*)(void))test_rng_free },
334     { OSSL_FUNC_RAND_INSTANTIATE,
335       (void(*)(void))test_rng_instantiate },
336     { OSSL_FUNC_RAND_UNINSTANTIATE,
337       (void(*)(void))test_rng_uninstantiate },
338     { OSSL_FUNC_RAND_GENERATE, (void(*)(void))test_rng_generate },
339     { OSSL_FUNC_RAND_RESEED, (void(*)(void))test_rng_reseed },
340     { OSSL_FUNC_RAND_NONCE, (void(*)(void))test_rng_nonce },
341     { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))test_rng_enable_locking },
342     { OSSL_FUNC_RAND_LOCK, (void(*)(void))test_rng_lock },
343     { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))test_rng_unlock },
344     { OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS,
345       (void(*)(void))test_rng_settable_ctx_params },
346     { OSSL_FUNC_RAND_SET_CTX_PARAMS, (void(*)(void))test_rng_set_ctx_params },
347     { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
348       (void(*)(void))test_rng_gettable_ctx_params },
349     { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))test_rng_get_ctx_params },
350     { OSSL_FUNC_RAND_VERIFY_ZEROIZATION,
351       (void(*)(void))test_rng_verify_zeroization },
352     { OSSL_FUNC_RAND_GET_SEED, (void(*)(void))test_rng_get_seed },
353     OSSL_DISPATCH_END
354 };
355