]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/rands/test_rng.c
Update copyright year
[thirdparty/openssl.git] / providers / implementations / rands / test_rng.c
CommitLineData
f3a25707 1/*
a28d06f3 2 * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
f3a25707
P
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>
363b1e5d 11#include <openssl/core_dispatch.h>
f3a25707
P
12#include <openssl/e_os2.h>
13#include <openssl/params.h>
1f50630a
P
14#include <openssl/core_names.h>
15#include <openssl/evp.h>
16#include <openssl/err.h>
17#include <openssl/randerr.h>
f3a25707
P
18#include "prov/providercommon.h"
19#include "prov/provider_ctx.h"
20#include "prov/provider_util.h"
21#include "prov/implementations.h"
f3a25707 22
1f50630a 23static OSSL_FUNC_rand_newctx_fn test_rng_new;
363b1e5d 24static OSSL_FUNC_rand_freectx_fn test_rng_free;
1f50630a
P
25static OSSL_FUNC_rand_instantiate_fn test_rng_instantiate;
26static OSSL_FUNC_rand_uninstantiate_fn test_rng_uninstantiate;
27static OSSL_FUNC_rand_generate_fn test_rng_generate;
28static OSSL_FUNC_rand_reseed_fn test_rng_reseed;
363b1e5d
DMSP
29static OSSL_FUNC_rand_nonce_fn test_rng_nonce;
30static OSSL_FUNC_rand_settable_ctx_params_fn test_rng_settable_ctx_params;
31static OSSL_FUNC_rand_set_ctx_params_fn test_rng_set_ctx_params;
32static OSSL_FUNC_rand_gettable_ctx_params_fn test_rng_gettable_ctx_params;
33static OSSL_FUNC_rand_get_ctx_params_fn test_rng_get_ctx_params;
34static OSSL_FUNC_rand_verify_zeroization_fn test_rng_verify_zeroization;
1f50630a
P
35static OSSL_FUNC_rand_enable_locking_fn test_rng_enable_locking;
36static OSSL_FUNC_rand_lock_fn test_rng_lock;
37static OSSL_FUNC_rand_unlock_fn test_rng_unlock;
e2730b84 38static OSSL_FUNC_rand_get_seed_fn test_rng_get_seed;
f3a25707
P
39
40typedef struct {
1f50630a
P
41 void *provctx;
42 int state;
43 unsigned int strength;
44 size_t max_request;
f3a25707
P
45 unsigned char *entropy, *nonce;
46 size_t entropy_len, entropy_pos, nonce_len;
1f50630a 47 CRYPTO_RWLOCK *lock;
f3a25707
P
48} PROV_TEST_RNG;
49
1f50630a
P
50static void *test_rng_new(void *provctx, void *parent,
51 const OSSL_DISPATCH *parent_dispatch)
f3a25707
P
52{
53 PROV_TEST_RNG *t;
54
1f50630a
P
55 if (parent != NULL)
56 return NULL;
57
f3a25707
P
58 t = OPENSSL_zalloc(sizeof(*t));
59 if (t == NULL)
1f50630a
P
60 return NULL;
61
62 t->max_request = INT_MAX;
63 t->provctx = provctx;
64 t->state = EVP_RAND_STATE_UNINITIALISED;
65 return t;
f3a25707
P
66}
67
1f50630a 68static void test_rng_free(void *vtest)
f3a25707 69{
1f50630a 70 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
f3a25707 71
1f50630a
P
72 if (t == NULL)
73 return;
f3a25707
P
74 OPENSSL_free(t->entropy);
75 OPENSSL_free(t->nonce);
1f50630a
P
76 CRYPTO_THREAD_lock_free(t->lock);
77 OPENSSL_free(t);
f3a25707
P
78}
79
1f50630a
P
80static int test_rng_instantiate(void *vtest, unsigned int strength,
81 int prediction_resistance,
f3a25707
P
82 const unsigned char *pstr, size_t pstr_len)
83{
1f50630a 84 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
f3a25707 85
1f50630a 86 if (strength > t->strength)
f3a25707
P
87 return 0;
88
1f50630a 89 t->state = EVP_RAND_STATE_READY;
f3a25707 90 t->entropy_pos = 0;
f3a25707 91
1f50630a 92 return 1;
f3a25707
P
93}
94
1f50630a 95static int test_rng_uninstantiate(void *vtest)
f3a25707 96{
1f50630a 97 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
f3a25707
P
98
99 t->entropy_pos = 0;
15f54941 100 t->state = EVP_RAND_STATE_UNINITIALISED;
1f50630a 101 return 1;
f3a25707
P
102}
103
1f50630a
P
104static int test_rng_generate(void *vtest, unsigned char *out, size_t outlen,
105 unsigned int strength, int prediction_resistance,
f3a25707
P
106 const unsigned char *adin, size_t adin_len)
107{
1f50630a 108 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
f3a25707
P
109 size_t i;
110
1f50630a 111 if (strength > t->strength)
f3a25707
P
112 return 0;
113
114 for (i = 0; i < outlen; i++) {
115 out[i] = t->entropy[t->entropy_pos++];
116 if (t->entropy_pos >= t->entropy_len)
117 break;
118 }
119 return 1;
120}
121
1f50630a
P
122static int test_rng_reseed(ossl_unused void *vtest,
123 ossl_unused int prediction_resistance,
124 ossl_unused const unsigned char *ent,
125 ossl_unused size_t ent_len,
126 ossl_unused const unsigned char *adin,
127 ossl_unused size_t adin_len)
f3a25707 128{
f3a25707
P
129 return 1;
130}
131
1f50630a
P
132static size_t test_rng_nonce(void *vtest, unsigned char *out,
133 unsigned int strength,
134 ossl_unused size_t min_noncelen,
135 ossl_unused size_t max_noncelen)
f3a25707 136{
1f50630a 137 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
f3a25707 138
1f50630a 139 if (t->nonce == NULL || strength > t->strength)
f3a25707
P
140 return 0;
141
142 if (out != NULL)
143 memcpy(out, t->nonce, t->nonce_len);
144 return t->nonce_len;
145}
146
1f50630a 147static int test_rng_get_ctx_params(void *vtest, OSSL_PARAM params[])
f3a25707 148{
1f50630a
P
149 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
150 OSSL_PARAM *p;
f3a25707 151
1f50630a
P
152 p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);
153 if (p != NULL && !OSSL_PARAM_set_int(p, t->state))
154 return 0;
155
156 p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);
157 if (p != NULL && !OSSL_PARAM_set_int(p, t->strength))
158 return 0;
159
160 p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);
161 if (p != NULL && !OSSL_PARAM_set_size_t(p, t->max_request))
162 return 0;
163 return 1;
f3a25707
P
164}
165
1017ab21 166static const OSSL_PARAM *test_rng_gettable_ctx_params(ossl_unused void *provctx)
f3a25707
P
167{
168 static const OSSL_PARAM known_gettable_ctx_params[] = {
1f50630a
P
169 OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL),
170 OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),
171 OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),
f3a25707
P
172 OSSL_PARAM_END
173 };
174 return known_gettable_ctx_params;
175}
176
1f50630a 177static int test_rng_set_ctx_params(void *vtest, const OSSL_PARAM params[])
f3a25707 178{
1f50630a 179 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
f3a25707
P
180 const OSSL_PARAM *p;
181 void *ptr = NULL;
182 size_t size = 0;
f3a25707
P
183
184 p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_STRENGTH);
1f50630a 185 if (p != NULL && !OSSL_PARAM_get_uint(p, &t->strength))
f3a25707
P
186 return 0;
187
188 p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_TEST_ENTROPY);
189 if (p != NULL) {
190 if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))
191 return 0;
192 OPENSSL_free(t->entropy);
193 t->entropy = ptr;
194 t->entropy_len = size;
195 t->entropy_pos = 0;
196 ptr = NULL;
197 }
198
199 p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_TEST_NONCE);
200 if (p != NULL) {
201 if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))
202 return 0;
203 OPENSSL_free(t->nonce);
204 t->nonce = ptr;
205 t->nonce_len = size;
206 }
207
1f50630a
P
208 p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_MAX_REQUEST);
209 if (p != NULL && !OSSL_PARAM_get_size_t(p, &t->max_request))
f3a25707
P
210 return 0;
211
1f50630a 212 return 1;
f3a25707
P
213}
214
1017ab21 215static const OSSL_PARAM *test_rng_settable_ctx_params(ossl_unused void *provctx)
f3a25707
P
216{
217 static const OSSL_PARAM known_settable_ctx_params[] = {
218 OSSL_PARAM_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY, NULL, 0),
219 OSSL_PARAM_octet_string(OSSL_RAND_PARAM_TEST_NONCE, NULL, 0),
220 OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),
08edd447 221 OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),
f3a25707
P
222 OSSL_PARAM_END
223 };
224 return known_settable_ctx_params;
225}
226
1f50630a 227static int test_rng_verify_zeroization(ossl_unused void *vtest)
f3a25707
P
228{
229 return 1;
230}
231
e2730b84
P
232static size_t test_rng_get_seed(void *vtest, unsigned char **pout,
233 int entropy, size_t min_len, size_t max_len,
234 ossl_unused int prediction_resistance,
235 ossl_unused const unsigned char *adin,
236 ossl_unused size_t adin_len)
237{
238 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
239
240 *pout = t->entropy;
241 return t->entropy_len > max_len ? max_len : t->entropy_len;
242}
243
1f50630a 244static int test_rng_enable_locking(void *vtest)
f000e828 245{
1f50630a
P
246 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
247
248 if (t != NULL && t->lock == NULL) {
249 t->lock = CRYPTO_THREAD_lock_new();
250 if (t->lock == NULL) {
251 ERR_raise(ERR_LIB_PROV, RAND_R_FAILED_TO_CREATE_LOCK);
252 return 0;
253 }
254 }
255 return 1;
256}
257
258static int test_rng_lock(void *vtest)
259{
260 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
261
262 if (t == NULL || t->lock == NULL)
263 return 1;
264 return CRYPTO_THREAD_write_lock(t->lock);
265}
266
267static void test_rng_unlock(void *vtest)
268{
269 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
270
271 if (t != NULL && t->lock != NULL)
272 CRYPTO_THREAD_unlock(t->lock);
f000e828
P
273}
274
1be63951 275const OSSL_DISPATCH ossl_test_rng_functions[] = {
1f50630a 276 { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))test_rng_new },
f3a25707
P
277 { OSSL_FUNC_RAND_FREECTX, (void(*)(void))test_rng_free },
278 { OSSL_FUNC_RAND_INSTANTIATE,
1f50630a 279 (void(*)(void))test_rng_instantiate },
f3a25707 280 { OSSL_FUNC_RAND_UNINSTANTIATE,
1f50630a
P
281 (void(*)(void))test_rng_uninstantiate },
282 { OSSL_FUNC_RAND_GENERATE, (void(*)(void))test_rng_generate },
283 { OSSL_FUNC_RAND_RESEED, (void(*)(void))test_rng_reseed },
f3a25707 284 { OSSL_FUNC_RAND_NONCE, (void(*)(void))test_rng_nonce },
1f50630a
P
285 { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))test_rng_enable_locking },
286 { OSSL_FUNC_RAND_LOCK, (void(*)(void))test_rng_lock },
287 { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))test_rng_unlock },
f3a25707
P
288 { OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS,
289 (void(*)(void))test_rng_settable_ctx_params },
290 { OSSL_FUNC_RAND_SET_CTX_PARAMS, (void(*)(void))test_rng_set_ctx_params },
291 { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
292 (void(*)(void))test_rng_gettable_ctx_params },
293 { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))test_rng_get_ctx_params },
294 { OSSL_FUNC_RAND_VERIFY_ZEROIZATION,
295 (void(*)(void))test_rng_verify_zeroization },
e2730b84 296 { OSSL_FUNC_RAND_GET_SEED, (void(*)(void))test_rng_get_seed },
f3a25707
P
297 { 0, NULL }
298};