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