2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
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
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 "prov/implementations.h"
20 #include "prov/provider_ctx.h"
21 #include "prov/providercommonerr.h"
22 #include "crypto/rand.h"
23 #include "crypto/rand_pool.h"
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;
43 static void *seed_src_new(void *provctx, void *parent,
44 const OSSL_DISPATCH *parent_dispatch)
49 ERR_raise(ERR_LIB_PROV, PROV_R_SEED_SOURCES_MUST_NOT_HAVE_A_PARENT);
53 s = OPENSSL_zalloc(sizeof(*s));
55 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
60 s->state = EVP_RAND_STATE_UNINITIALISED;
64 static void seed_src_free(void *vseed)
69 static int seed_src_instantiate(void *vseed, unsigned int strength,
70 int prediction_resistance,
71 const unsigned char *pstr, size_t pstr_len)
73 PROV_SEED_SRC *s = (PROV_SEED_SRC *)vseed;
75 s->state = EVP_RAND_STATE_READY;
79 static int seed_src_uninstantiate(void *vseed)
81 PROV_SEED_SRC *s = (PROV_SEED_SRC *)vseed;
83 s->state = EVP_RAND_STATE_UNINITIALISED;
87 static int seed_src_generate(void *vseed, unsigned char *out, size_t outlen,
88 unsigned int strength,
89 ossl_unused int prediction_resistance,
90 ossl_unused const unsigned char *adin,
91 ossl_unused size_t adin_len)
93 PROV_SEED_SRC *s = (PROV_SEED_SRC *)vseed;
94 size_t entropy_available;
97 if (s->state != EVP_RAND_STATE_READY) {
98 ERR_raise(ERR_LIB_PROV,
99 s->state == EVP_RAND_STATE_ERROR ? PROV_R_IN_ERROR_STATE
100 : PROV_R_NOT_INSTANTIATED);
104 pool = rand_pool_new(strength, 1, outlen, outlen);
106 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
110 /* Get entropy by polling system entropy sources. */
111 entropy_available = ossl_pool_acquire_entropy(pool);
113 if (entropy_available > 0)
114 memcpy(out, rand_pool_detach(pool), rand_pool_length(pool));
116 rand_pool_free(pool);
117 return entropy_available > 0;
120 static int seed_src_reseed(void *vseed,
121 ossl_unused int prediction_resistance,
122 ossl_unused const unsigned char *ent,
123 ossl_unused size_t ent_len,
124 ossl_unused const unsigned char *adin,
125 ossl_unused size_t adin_len)
127 PROV_SEED_SRC *s = (PROV_SEED_SRC *)vseed;
129 if (s->state != EVP_RAND_STATE_READY) {
130 ERR_raise(ERR_LIB_PROV,
131 s->state == EVP_RAND_STATE_ERROR ? PROV_R_IN_ERROR_STATE
132 : PROV_R_NOT_INSTANTIATED);
138 static int seed_src_get_ctx_params(void *vseed, OSSL_PARAM params[])
140 PROV_SEED_SRC *s = (PROV_SEED_SRC *)vseed;
143 p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);
144 if (p != NULL && !OSSL_PARAM_set_int(p, s->state))
147 p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);
148 if (p != NULL && !OSSL_PARAM_set_int(p, 1024))
151 p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);
152 if (p != NULL && !OSSL_PARAM_set_size_t(p, 128))
157 static const OSSL_PARAM *seed_src_gettable_ctx_params(ossl_unused void *provctx)
159 static const OSSL_PARAM known_gettable_ctx_params[] = {
160 OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL),
161 OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),
162 OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),
165 return known_gettable_ctx_params;
168 static int seed_src_verify_zeroization(ossl_unused void *vseed)
173 static int seed_src_enable_locking(ossl_unused void *vseed)
178 int seed_src_lock(ossl_unused void *vctx)
183 void seed_src_unlock(ossl_unused void *vctx)
187 const OSSL_DISPATCH ossl_seed_src_functions[] = {
188 { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))seed_src_new },
189 { OSSL_FUNC_RAND_FREECTX, (void(*)(void))seed_src_free },
190 { OSSL_FUNC_RAND_INSTANTIATE,
191 (void(*)(void))seed_src_instantiate },
192 { OSSL_FUNC_RAND_UNINSTANTIATE,
193 (void(*)(void))seed_src_uninstantiate },
194 { OSSL_FUNC_RAND_GENERATE, (void(*)(void))seed_src_generate },
195 { OSSL_FUNC_RAND_RESEED, (void(*)(void))seed_src_reseed },
196 { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))seed_src_enable_locking },
197 { OSSL_FUNC_RAND_LOCK, (void(*)(void))seed_src_lock },
198 { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))seed_src_unlock },
199 { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
200 (void(*)(void))seed_src_gettable_ctx_params },
201 { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))seed_src_get_ctx_params },
202 { OSSL_FUNC_RAND_VERIFY_ZEROIZATION,
203 (void(*)(void))seed_src_verify_zeroization },