xref: /freebsd/crypto/openssl/providers/implementations/rands/seed_src.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1*b077aed3SPierre Pronchery /*
2*b077aed3SPierre Pronchery  * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b077aed3SPierre Pronchery  *
4*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*b077aed3SPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6*b077aed3SPierre Pronchery  * in the file LICENSE in the source distribution or at
7*b077aed3SPierre Pronchery  * https://www.openssl.org/source/license.html
8*b077aed3SPierre Pronchery  */
9*b077aed3SPierre Pronchery 
10*b077aed3SPierre Pronchery #include <string.h>
11*b077aed3SPierre Pronchery #include <openssl/rand.h>
12*b077aed3SPierre Pronchery #include <openssl/core_dispatch.h>
13*b077aed3SPierre Pronchery #include <openssl/e_os2.h>
14*b077aed3SPierre Pronchery #include <openssl/params.h>
15*b077aed3SPierre Pronchery #include <openssl/core_names.h>
16*b077aed3SPierre Pronchery #include <openssl/evp.h>
17*b077aed3SPierre Pronchery #include <openssl/err.h>
18*b077aed3SPierre Pronchery #include <openssl/randerr.h>
19*b077aed3SPierre Pronchery #include <openssl/proverr.h>
20*b077aed3SPierre Pronchery #include "prov/implementations.h"
21*b077aed3SPierre Pronchery #include "prov/provider_ctx.h"
22*b077aed3SPierre Pronchery #include "crypto/rand.h"
23*b077aed3SPierre Pronchery #include "crypto/rand_pool.h"
24*b077aed3SPierre Pronchery 
25*b077aed3SPierre Pronchery static OSSL_FUNC_rand_newctx_fn seed_src_new;
26*b077aed3SPierre Pronchery static OSSL_FUNC_rand_freectx_fn seed_src_free;
27*b077aed3SPierre Pronchery static OSSL_FUNC_rand_instantiate_fn seed_src_instantiate;
28*b077aed3SPierre Pronchery static OSSL_FUNC_rand_uninstantiate_fn seed_src_uninstantiate;
29*b077aed3SPierre Pronchery static OSSL_FUNC_rand_generate_fn seed_src_generate;
30*b077aed3SPierre Pronchery static OSSL_FUNC_rand_reseed_fn seed_src_reseed;
31*b077aed3SPierre Pronchery static OSSL_FUNC_rand_gettable_ctx_params_fn seed_src_gettable_ctx_params;
32*b077aed3SPierre Pronchery static OSSL_FUNC_rand_get_ctx_params_fn seed_src_get_ctx_params;
33*b077aed3SPierre Pronchery static OSSL_FUNC_rand_verify_zeroization_fn seed_src_verify_zeroization;
34*b077aed3SPierre Pronchery static OSSL_FUNC_rand_enable_locking_fn seed_src_enable_locking;
35*b077aed3SPierre Pronchery static OSSL_FUNC_rand_lock_fn seed_src_lock;
36*b077aed3SPierre Pronchery static OSSL_FUNC_rand_unlock_fn seed_src_unlock;
37*b077aed3SPierre Pronchery static OSSL_FUNC_rand_get_seed_fn seed_get_seed;
38*b077aed3SPierre Pronchery static OSSL_FUNC_rand_clear_seed_fn seed_clear_seed;
39*b077aed3SPierre Pronchery 
40*b077aed3SPierre Pronchery typedef struct {
41*b077aed3SPierre Pronchery     void *provctx;
42*b077aed3SPierre Pronchery     int state;
43*b077aed3SPierre Pronchery } PROV_SEED_SRC;
44*b077aed3SPierre Pronchery 
seed_src_new(void * provctx,void * parent,const OSSL_DISPATCH * parent_dispatch)45*b077aed3SPierre Pronchery static void *seed_src_new(void *provctx, void *parent,
46*b077aed3SPierre Pronchery                           const OSSL_DISPATCH *parent_dispatch)
47*b077aed3SPierre Pronchery {
48*b077aed3SPierre Pronchery     PROV_SEED_SRC *s;
49*b077aed3SPierre Pronchery 
50*b077aed3SPierre Pronchery     if (parent != NULL) {
51*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_SEED_SOURCES_MUST_NOT_HAVE_A_PARENT);
52*b077aed3SPierre Pronchery         return NULL;
53*b077aed3SPierre Pronchery     }
54*b077aed3SPierre Pronchery 
55*b077aed3SPierre Pronchery     s = OPENSSL_zalloc(sizeof(*s));
56*b077aed3SPierre Pronchery     if (s == NULL) {
57*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
58*b077aed3SPierre Pronchery         return NULL;
59*b077aed3SPierre Pronchery     }
60*b077aed3SPierre Pronchery 
61*b077aed3SPierre Pronchery     s->provctx = provctx;
62*b077aed3SPierre Pronchery     s->state = EVP_RAND_STATE_UNINITIALISED;
63*b077aed3SPierre Pronchery     return s;
64*b077aed3SPierre Pronchery }
65*b077aed3SPierre Pronchery 
seed_src_free(void * vseed)66*b077aed3SPierre Pronchery static void seed_src_free(void *vseed)
67*b077aed3SPierre Pronchery {
68*b077aed3SPierre Pronchery     OPENSSL_free(vseed);
69*b077aed3SPierre Pronchery }
70*b077aed3SPierre Pronchery 
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[])71*b077aed3SPierre Pronchery static int seed_src_instantiate(void *vseed, unsigned int strength,
72*b077aed3SPierre Pronchery                                 int prediction_resistance,
73*b077aed3SPierre Pronchery                                 const unsigned char *pstr, size_t pstr_len,
74*b077aed3SPierre Pronchery                                 ossl_unused const OSSL_PARAM params[])
75*b077aed3SPierre Pronchery {
76*b077aed3SPierre Pronchery     PROV_SEED_SRC *s = (PROV_SEED_SRC *)vseed;
77*b077aed3SPierre Pronchery 
78*b077aed3SPierre Pronchery     s->state = EVP_RAND_STATE_READY;
79*b077aed3SPierre Pronchery     return 1;
80*b077aed3SPierre Pronchery }
81*b077aed3SPierre Pronchery 
seed_src_uninstantiate(void * vseed)82*b077aed3SPierre Pronchery static int seed_src_uninstantiate(void *vseed)
83*b077aed3SPierre Pronchery {
84*b077aed3SPierre Pronchery     PROV_SEED_SRC *s = (PROV_SEED_SRC *)vseed;
85*b077aed3SPierre Pronchery 
86*b077aed3SPierre Pronchery     s->state = EVP_RAND_STATE_UNINITIALISED;
87*b077aed3SPierre Pronchery     return 1;
88*b077aed3SPierre Pronchery }
89*b077aed3SPierre Pronchery 
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)90*b077aed3SPierre Pronchery static int seed_src_generate(void *vseed, unsigned char *out, size_t outlen,
91*b077aed3SPierre Pronchery                              unsigned int strength,
92*b077aed3SPierre Pronchery                              ossl_unused int prediction_resistance,
93*b077aed3SPierre Pronchery                              ossl_unused const unsigned char *adin,
94*b077aed3SPierre Pronchery                              ossl_unused size_t adin_len)
95*b077aed3SPierre Pronchery {
96*b077aed3SPierre Pronchery     PROV_SEED_SRC *s = (PROV_SEED_SRC *)vseed;
97*b077aed3SPierre Pronchery     size_t entropy_available;
98*b077aed3SPierre Pronchery     RAND_POOL *pool;
99*b077aed3SPierre Pronchery 
100*b077aed3SPierre Pronchery     if (s->state != EVP_RAND_STATE_READY) {
101*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV,
102*b077aed3SPierre Pronchery                   s->state == EVP_RAND_STATE_ERROR ? PROV_R_IN_ERROR_STATE
103*b077aed3SPierre Pronchery                                                    : PROV_R_NOT_INSTANTIATED);
104*b077aed3SPierre Pronchery         return 0;
105*b077aed3SPierre Pronchery     }
106*b077aed3SPierre Pronchery 
107*b077aed3SPierre Pronchery     pool = ossl_rand_pool_new(strength, 1, outlen, outlen);
108*b077aed3SPierre Pronchery     if (pool == NULL) {
109*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
110*b077aed3SPierre Pronchery         return 0;
111*b077aed3SPierre Pronchery     }
112*b077aed3SPierre Pronchery 
113*b077aed3SPierre Pronchery     /* Get entropy by polling system entropy sources. */
114*b077aed3SPierre Pronchery     entropy_available = ossl_pool_acquire_entropy(pool);
115*b077aed3SPierre Pronchery 
116*b077aed3SPierre Pronchery     if (entropy_available > 0)
117*b077aed3SPierre Pronchery         memcpy(out, ossl_rand_pool_buffer(pool), ossl_rand_pool_length(pool));
118*b077aed3SPierre Pronchery 
119*b077aed3SPierre Pronchery     ossl_rand_pool_free(pool);
120*b077aed3SPierre Pronchery     return entropy_available > 0;
121*b077aed3SPierre Pronchery }
122*b077aed3SPierre Pronchery 
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)123*b077aed3SPierre Pronchery static int seed_src_reseed(void *vseed,
124*b077aed3SPierre Pronchery                            ossl_unused int prediction_resistance,
125*b077aed3SPierre Pronchery                            ossl_unused const unsigned char *ent,
126*b077aed3SPierre Pronchery                            ossl_unused size_t ent_len,
127*b077aed3SPierre Pronchery                            ossl_unused const unsigned char *adin,
128*b077aed3SPierre Pronchery                            ossl_unused size_t adin_len)
129*b077aed3SPierre Pronchery {
130*b077aed3SPierre Pronchery     PROV_SEED_SRC *s = (PROV_SEED_SRC *)vseed;
131*b077aed3SPierre Pronchery 
132*b077aed3SPierre Pronchery     if (s->state != EVP_RAND_STATE_READY) {
133*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV,
134*b077aed3SPierre Pronchery                   s->state == EVP_RAND_STATE_ERROR ? PROV_R_IN_ERROR_STATE
135*b077aed3SPierre Pronchery                                                    : PROV_R_NOT_INSTANTIATED);
136*b077aed3SPierre Pronchery         return 0;
137*b077aed3SPierre Pronchery     }
138*b077aed3SPierre Pronchery     return 1;
139*b077aed3SPierre Pronchery }
140*b077aed3SPierre Pronchery 
seed_src_get_ctx_params(void * vseed,OSSL_PARAM params[])141*b077aed3SPierre Pronchery static int seed_src_get_ctx_params(void *vseed, OSSL_PARAM params[])
142*b077aed3SPierre Pronchery {
143*b077aed3SPierre Pronchery     PROV_SEED_SRC *s = (PROV_SEED_SRC *)vseed;
144*b077aed3SPierre Pronchery     OSSL_PARAM *p;
145*b077aed3SPierre Pronchery 
146*b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);
147*b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_int(p, s->state))
148*b077aed3SPierre Pronchery         return 0;
149*b077aed3SPierre Pronchery 
150*b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);
151*b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_int(p, 1024))
152*b077aed3SPierre Pronchery         return 0;
153*b077aed3SPierre Pronchery 
154*b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);
155*b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_size_t(p, 128))
156*b077aed3SPierre Pronchery         return 0;
157*b077aed3SPierre Pronchery     return 1;
158*b077aed3SPierre Pronchery }
159*b077aed3SPierre Pronchery 
seed_src_gettable_ctx_params(ossl_unused void * vseed,ossl_unused void * provctx)160*b077aed3SPierre Pronchery static const OSSL_PARAM *seed_src_gettable_ctx_params(ossl_unused void *vseed,
161*b077aed3SPierre Pronchery                                                       ossl_unused void *provctx)
162*b077aed3SPierre Pronchery {
163*b077aed3SPierre Pronchery     static const OSSL_PARAM known_gettable_ctx_params[] = {
164*b077aed3SPierre Pronchery         OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL),
165*b077aed3SPierre Pronchery         OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),
166*b077aed3SPierre Pronchery         OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),
167*b077aed3SPierre Pronchery         OSSL_PARAM_END
168*b077aed3SPierre Pronchery     };
169*b077aed3SPierre Pronchery     return known_gettable_ctx_params;
170*b077aed3SPierre Pronchery }
171*b077aed3SPierre Pronchery 
seed_src_verify_zeroization(ossl_unused void * vseed)172*b077aed3SPierre Pronchery static int seed_src_verify_zeroization(ossl_unused void *vseed)
173*b077aed3SPierre Pronchery {
174*b077aed3SPierre Pronchery     return 1;
175*b077aed3SPierre Pronchery }
176*b077aed3SPierre Pronchery 
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)177*b077aed3SPierre Pronchery static size_t seed_get_seed(void *vseed, unsigned char **pout,
178*b077aed3SPierre Pronchery                             int entropy, size_t min_len, size_t max_len,
179*b077aed3SPierre Pronchery                             int prediction_resistance,
180*b077aed3SPierre Pronchery                             const unsigned char *adin, size_t adin_len)
181*b077aed3SPierre Pronchery {
182*b077aed3SPierre Pronchery     size_t bytes_needed;
183*b077aed3SPierre Pronchery     unsigned char *p;
184*b077aed3SPierre Pronchery 
185*b077aed3SPierre Pronchery     /*
186*b077aed3SPierre Pronchery      * Figure out how many bytes we need.
187*b077aed3SPierre Pronchery      * This assumes that the seed sources provide eight bits of entropy
188*b077aed3SPierre Pronchery      * per byte.  For lower quality sources, the formula will need to be
189*b077aed3SPierre Pronchery      * different.
190*b077aed3SPierre Pronchery      */
191*b077aed3SPierre Pronchery     bytes_needed = entropy >= 0 ? (entropy + 7) / 8 : 0;
192*b077aed3SPierre Pronchery     if (bytes_needed < min_len)
193*b077aed3SPierre Pronchery         bytes_needed = min_len;
194*b077aed3SPierre Pronchery     if (bytes_needed > max_len) {
195*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_ENTROPY_SOURCE_STRENGTH_TOO_WEAK);
196*b077aed3SPierre Pronchery         return 0;
197*b077aed3SPierre Pronchery     }
198*b077aed3SPierre Pronchery 
199*b077aed3SPierre Pronchery     p = OPENSSL_secure_malloc(bytes_needed);
200*b077aed3SPierre Pronchery     if (p == NULL) {
201*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
202*b077aed3SPierre Pronchery         return 0;
203*b077aed3SPierre Pronchery     }
204*b077aed3SPierre Pronchery     if (seed_src_generate(vseed, p, bytes_needed, 0, prediction_resistance,
205*b077aed3SPierre Pronchery                           adin, adin_len) != 0) {
206*b077aed3SPierre Pronchery         *pout = p;
207*b077aed3SPierre Pronchery         return bytes_needed;
208*b077aed3SPierre Pronchery     }
209*b077aed3SPierre Pronchery     OPENSSL_secure_clear_free(p, bytes_needed);
210*b077aed3SPierre Pronchery     return 0;
211*b077aed3SPierre Pronchery }
212*b077aed3SPierre Pronchery 
seed_clear_seed(ossl_unused void * vdrbg,unsigned char * out,size_t outlen)213*b077aed3SPierre Pronchery static void seed_clear_seed(ossl_unused void *vdrbg,
214*b077aed3SPierre Pronchery                             unsigned char *out, size_t outlen)
215*b077aed3SPierre Pronchery {
216*b077aed3SPierre Pronchery     OPENSSL_secure_clear_free(out, outlen);
217*b077aed3SPierre Pronchery }
218*b077aed3SPierre Pronchery 
seed_src_enable_locking(ossl_unused void * vseed)219*b077aed3SPierre Pronchery static int seed_src_enable_locking(ossl_unused void *vseed)
220*b077aed3SPierre Pronchery {
221*b077aed3SPierre Pronchery     return 1;
222*b077aed3SPierre Pronchery }
223*b077aed3SPierre Pronchery 
seed_src_lock(ossl_unused void * vctx)224*b077aed3SPierre Pronchery int seed_src_lock(ossl_unused void *vctx)
225*b077aed3SPierre Pronchery {
226*b077aed3SPierre Pronchery     return 1;
227*b077aed3SPierre Pronchery }
228*b077aed3SPierre Pronchery 
seed_src_unlock(ossl_unused void * vctx)229*b077aed3SPierre Pronchery void seed_src_unlock(ossl_unused void *vctx)
230*b077aed3SPierre Pronchery {
231*b077aed3SPierre Pronchery }
232*b077aed3SPierre Pronchery 
233*b077aed3SPierre Pronchery const OSSL_DISPATCH ossl_seed_src_functions[] = {
234*b077aed3SPierre Pronchery     { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))seed_src_new },
235*b077aed3SPierre Pronchery     { OSSL_FUNC_RAND_FREECTX, (void(*)(void))seed_src_free },
236*b077aed3SPierre Pronchery     { OSSL_FUNC_RAND_INSTANTIATE,
237*b077aed3SPierre Pronchery       (void(*)(void))seed_src_instantiate },
238*b077aed3SPierre Pronchery     { OSSL_FUNC_RAND_UNINSTANTIATE,
239*b077aed3SPierre Pronchery       (void(*)(void))seed_src_uninstantiate },
240*b077aed3SPierre Pronchery     { OSSL_FUNC_RAND_GENERATE, (void(*)(void))seed_src_generate },
241*b077aed3SPierre Pronchery     { OSSL_FUNC_RAND_RESEED, (void(*)(void))seed_src_reseed },
242*b077aed3SPierre Pronchery     { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))seed_src_enable_locking },
243*b077aed3SPierre Pronchery     { OSSL_FUNC_RAND_LOCK, (void(*)(void))seed_src_lock },
244*b077aed3SPierre Pronchery     { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))seed_src_unlock },
245*b077aed3SPierre Pronchery     { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
246*b077aed3SPierre Pronchery       (void(*)(void))seed_src_gettable_ctx_params },
247*b077aed3SPierre Pronchery     { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))seed_src_get_ctx_params },
248*b077aed3SPierre Pronchery     { OSSL_FUNC_RAND_VERIFY_ZEROIZATION,
249*b077aed3SPierre Pronchery       (void(*)(void))seed_src_verify_zeroization },
250*b077aed3SPierre Pronchery     { OSSL_FUNC_RAND_GET_SEED, (void(*)(void))seed_get_seed },
251*b077aed3SPierre Pronchery     { OSSL_FUNC_RAND_CLEAR_SEED, (void(*)(void))seed_clear_seed },
252*b077aed3SPierre Pronchery     { 0, NULL }
253*b077aed3SPierre Pronchery };
254