1 /*
2  * Copyright 2020-2023 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/rand.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 <openssl/proverr.h>
20 #include "prov/implementations.h"
21 #include "prov/provider_ctx.h"
22 #include "crypto/rand.h"
23 #include "crypto/rand_pool.h"
24 
25 static OSSL_FUNC_rand_newctx_fn seed_src_new;
26 static OSSL_FUNC_rand_freectx_fn seed_src_free;
27 static OSSL_FUNC_rand_instantiate_fn seed_src_instantiate;
28 static OSSL_FUNC_rand_uninstantiate_fn seed_src_uninstantiate;
29 static OSSL_FUNC_rand_generate_fn seed_src_generate;
30 static OSSL_FUNC_rand_reseed_fn seed_src_reseed;
31 static OSSL_FUNC_rand_gettable_ctx_params_fn seed_src_gettable_ctx_params;
32 static OSSL_FUNC_rand_get_ctx_params_fn seed_src_get_ctx_params;
33 static OSSL_FUNC_rand_verify_zeroization_fn seed_src_verify_zeroization;
34 static OSSL_FUNC_rand_enable_locking_fn seed_src_enable_locking;
35 static OSSL_FUNC_rand_lock_fn seed_src_lock;
36 static OSSL_FUNC_rand_unlock_fn seed_src_unlock;
37 static OSSL_FUNC_rand_get_seed_fn seed_get_seed;
38 static OSSL_FUNC_rand_clear_seed_fn seed_clear_seed;
39 
40 typedef struct {
41     void *provctx;
42     int state;
43 } PROV_SEED_SRC;
44 
seed_src_new(void * provctx,void * parent,const OSSL_DISPATCH * parent_dispatch)45 static void *seed_src_new(void *provctx, void *parent,
46                           const OSSL_DISPATCH *parent_dispatch)
47 {
48     PROV_SEED_SRC *s;
49 
50     if (parent != NULL) {
51         ERR_raise(ERR_LIB_PROV, PROV_R_SEED_SOURCES_MUST_NOT_HAVE_A_PARENT);
52         return NULL;
53     }
54 
55     s = OPENSSL_zalloc(sizeof(*s));
56     if (s == NULL)
57         return NULL;
58 
59     s->provctx = provctx;
60     s->state = EVP_RAND_STATE_UNINITIALISED;
61     return s;
62 }
63 
seed_src_free(void * vseed)64 static void seed_src_free(void *vseed)
65 {
66     OPENSSL_free(vseed);
67 }
68 
seed_src_instantiate(void * vseed,unsigned int strength,int prediction_resistance,const unsigned char * pstr,size_t pstr_len,ossl_unused const OSSL_PARAM params[])69 static int seed_src_instantiate(void *vseed, unsigned int strength,
70                                 int prediction_resistance,
71                                 const unsigned char *pstr, size_t pstr_len,
72                                 ossl_unused const OSSL_PARAM params[])
73 {
74     PROV_SEED_SRC *s = (PROV_SEED_SRC *)vseed;
75 
76     s->state = EVP_RAND_STATE_READY;
77     return 1;
78 }
79 
seed_src_uninstantiate(void * vseed)80 static int seed_src_uninstantiate(void *vseed)
81 {
82     PROV_SEED_SRC *s = (PROV_SEED_SRC *)vseed;
83 
84     s->state = EVP_RAND_STATE_UNINITIALISED;
85     return 1;
86 }
87 
seed_src_generate(void * vseed,unsigned char * out,size_t outlen,unsigned int strength,ossl_unused int prediction_resistance,ossl_unused const unsigned char * adin,ossl_unused size_t adin_len)88 static int seed_src_generate(void *vseed, unsigned char *out, size_t outlen,
89                              unsigned int strength,
90                              ossl_unused int prediction_resistance,
91                              ossl_unused const unsigned char *adin,
92                              ossl_unused size_t adin_len)
93 {
94     PROV_SEED_SRC *s = (PROV_SEED_SRC *)vseed;
95     size_t entropy_available;
96     RAND_POOL *pool;
97 
98     if (s->state != EVP_RAND_STATE_READY) {
99         ERR_raise(ERR_LIB_PROV,
100                   s->state == EVP_RAND_STATE_ERROR ? PROV_R_IN_ERROR_STATE
101                                                    : PROV_R_NOT_INSTANTIATED);
102         return 0;
103     }
104 
105     pool = ossl_rand_pool_new(strength, 1, outlen, outlen);
106     if (pool == NULL) {
107         ERR_raise(ERR_LIB_PROV, ERR_R_RAND_LIB);
108         return 0;
109     }
110 
111     /* Get entropy by polling system entropy sources. */
112     entropy_available = ossl_pool_acquire_entropy(pool);
113 
114     if (entropy_available > 0)
115         memcpy(out, ossl_rand_pool_buffer(pool), ossl_rand_pool_length(pool));
116 
117     ossl_rand_pool_free(pool);
118     return entropy_available > 0;
119 }
120 
seed_src_reseed(void * vseed,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)121 static int seed_src_reseed(void *vseed,
122                            ossl_unused int prediction_resistance,
123                            ossl_unused const unsigned char *ent,
124                            ossl_unused size_t ent_len,
125                            ossl_unused const unsigned char *adin,
126                            ossl_unused size_t adin_len)
127 {
128     PROV_SEED_SRC *s = (PROV_SEED_SRC *)vseed;
129 
130     if (s->state != EVP_RAND_STATE_READY) {
131         ERR_raise(ERR_LIB_PROV,
132                   s->state == EVP_RAND_STATE_ERROR ? PROV_R_IN_ERROR_STATE
133                                                    : PROV_R_NOT_INSTANTIATED);
134         return 0;
135     }
136     return 1;
137 }
138 
seed_src_get_ctx_params(void * vseed,OSSL_PARAM params[])139 static int seed_src_get_ctx_params(void *vseed, OSSL_PARAM params[])
140 {
141     PROV_SEED_SRC *s = (PROV_SEED_SRC *)vseed;
142     OSSL_PARAM *p;
143 
144     p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);
145     if (p != NULL && !OSSL_PARAM_set_int(p, s->state))
146         return 0;
147 
148     p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);
149     if (p != NULL && !OSSL_PARAM_set_int(p, 1024))
150         return 0;
151 
152     p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);
153     if (p != NULL && !OSSL_PARAM_set_size_t(p, 128))
154         return 0;
155     return 1;
156 }
157 
seed_src_gettable_ctx_params(ossl_unused void * vseed,ossl_unused void * provctx)158 static const OSSL_PARAM *seed_src_gettable_ctx_params(ossl_unused void *vseed,
159                                                       ossl_unused void *provctx)
160 {
161     static const OSSL_PARAM known_gettable_ctx_params[] = {
162         OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL),
163         OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),
164         OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),
165         OSSL_PARAM_END
166     };
167     return known_gettable_ctx_params;
168 }
169 
seed_src_verify_zeroization(ossl_unused void * vseed)170 static int seed_src_verify_zeroization(ossl_unused void *vseed)
171 {
172     return 1;
173 }
174 
seed_get_seed(void * vseed,unsigned char ** pout,int entropy,size_t min_len,size_t max_len,int prediction_resistance,const unsigned char * adin,size_t adin_len)175 static size_t seed_get_seed(void *vseed, unsigned char **pout,
176                             int entropy, size_t min_len, size_t max_len,
177                             int prediction_resistance,
178                             const unsigned char *adin, size_t adin_len)
179 {
180     size_t ret = 0;
181     size_t entropy_available = 0;
182     size_t i;
183     RAND_POOL *pool;
184 
185     pool = ossl_rand_pool_new(entropy, 1, min_len, max_len);
186     if (pool == NULL) {
187         ERR_raise(ERR_LIB_PROV, ERR_R_RAND_LIB);
188         return 0;
189     }
190 
191     /* Get entropy by polling system entropy sources. */
192     entropy_available = ossl_pool_acquire_entropy(pool);
193 
194     if (entropy_available > 0) {
195         ret = ossl_rand_pool_length(pool);
196         *pout = ossl_rand_pool_detach(pool);
197 
198         /* xor the additional data into the output */
199         for (i = 0 ; i < adin_len ; ++i)
200             (*pout)[i % ret] ^= adin[i];
201     } else {
202         ERR_raise(ERR_LIB_PROV, PROV_R_ENTROPY_SOURCE_STRENGTH_TOO_WEAK);
203     }
204     ossl_rand_pool_free(pool);
205     return ret;
206 }
207 
seed_clear_seed(ossl_unused void * vdrbg,unsigned char * out,size_t outlen)208 static void seed_clear_seed(ossl_unused void *vdrbg,
209                             unsigned char *out, size_t outlen)
210 {
211     OPENSSL_secure_clear_free(out, outlen);
212 }
213 
seed_src_enable_locking(ossl_unused void * vseed)214 static int seed_src_enable_locking(ossl_unused void *vseed)
215 {
216     return 1;
217 }
218 
seed_src_lock(ossl_unused void * vctx)219 int seed_src_lock(ossl_unused void *vctx)
220 {
221     return 1;
222 }
223 
seed_src_unlock(ossl_unused void * vctx)224 void seed_src_unlock(ossl_unused void *vctx)
225 {
226 }
227 
228 const OSSL_DISPATCH ossl_seed_src_functions[] = {
229     { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))seed_src_new },
230     { OSSL_FUNC_RAND_FREECTX, (void(*)(void))seed_src_free },
231     { OSSL_FUNC_RAND_INSTANTIATE,
232       (void(*)(void))seed_src_instantiate },
233     { OSSL_FUNC_RAND_UNINSTANTIATE,
234       (void(*)(void))seed_src_uninstantiate },
235     { OSSL_FUNC_RAND_GENERATE, (void(*)(void))seed_src_generate },
236     { OSSL_FUNC_RAND_RESEED, (void(*)(void))seed_src_reseed },
237     { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))seed_src_enable_locking },
238     { OSSL_FUNC_RAND_LOCK, (void(*)(void))seed_src_lock },
239     { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))seed_src_unlock },
240     { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
241       (void(*)(void))seed_src_gettable_ctx_params },
242     { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))seed_src_get_ctx_params },
243     { OSSL_FUNC_RAND_VERIFY_ZEROIZATION,
244       (void(*)(void))seed_src_verify_zeroization },
245     { OSSL_FUNC_RAND_GET_SEED, (void(*)(void))seed_get_seed },
246     { OSSL_FUNC_RAND_CLEAR_SEED, (void(*)(void))seed_clear_seed },
247     OSSL_DISPATCH_END
248 };
249