]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/rands/test_rng.c
disassociate test RNG from the DRBGs
[thirdparty/openssl.git] / providers / implementations / rands / test_rng.c
CommitLineData
f3a25707
P
1/*
2 * Copyright 2020 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>
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;
f3a25707
P
38
39typedef struct {
1f50630a
P
40 void *provctx;
41 int state;
42 unsigned int strength;
43 size_t max_request;
f3a25707
P
44 unsigned char *entropy, *nonce;
45 size_t entropy_len, entropy_pos, nonce_len;
1f50630a 46 CRYPTO_RWLOCK *lock;
f3a25707
P
47} PROV_TEST_RNG;
48
1f50630a
P
49static void *test_rng_new(void *provctx, void *parent,
50 const OSSL_DISPATCH *parent_dispatch)
f3a25707
P
51{
52 PROV_TEST_RNG *t;
53
1f50630a
P
54 if (parent != NULL)
55 return NULL;
56
f3a25707
P
57 t = OPENSSL_zalloc(sizeof(*t));
58 if (t == NULL)
1f50630a
P
59 return NULL;
60
61 t->max_request = INT_MAX;
62 t->provctx = provctx;
63 t->state = EVP_RAND_STATE_UNINITIALISED;
64 return t;
f3a25707
P
65}
66
1f50630a 67static void test_rng_free(void *vtest)
f3a25707 68{
1f50630a 69 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
f3a25707 70
1f50630a
P
71 if (t == NULL)
72 return;
f3a25707
P
73 OPENSSL_free(t->entropy);
74 OPENSSL_free(t->nonce);
1f50630a
P
75 CRYPTO_THREAD_lock_free(t->lock);
76 OPENSSL_free(t);
f3a25707
P
77}
78
1f50630a
P
79static int test_rng_instantiate(void *vtest, unsigned int strength,
80 int prediction_resistance,
f3a25707
P
81 const unsigned char *pstr, size_t pstr_len)
82{
1f50630a 83 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
f3a25707 84
1f50630a 85 if (strength > t->strength)
f3a25707
P
86 return 0;
87
1f50630a 88 t->state = EVP_RAND_STATE_READY;
f3a25707 89 t->entropy_pos = 0;
f3a25707 90
1f50630a 91 return 1;
f3a25707
P
92}
93
1f50630a 94static int test_rng_uninstantiate(void *vtest)
f3a25707 95{
1f50630a 96 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
f3a25707
P
97
98 t->entropy_pos = 0;
1f50630a 99 return 1;
f3a25707
P
100}
101
1f50630a
P
102static int test_rng_generate(void *vtest, unsigned char *out, size_t outlen,
103 unsigned int strength, int prediction_resistance,
f3a25707
P
104 const unsigned char *adin, size_t adin_len)
105{
1f50630a 106 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
f3a25707
P
107 size_t i;
108
1f50630a 109 if (strength > t->strength)
f3a25707
P
110 return 0;
111
112 for (i = 0; i < outlen; i++) {
113 out[i] = t->entropy[t->entropy_pos++];
114 if (t->entropy_pos >= t->entropy_len)
115 break;
116 }
117 return 1;
118}
119
1f50630a
P
120static int test_rng_reseed(ossl_unused void *vtest,
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)
f3a25707 126{
f3a25707
P
127 return 1;
128}
129
1f50630a
P
130static size_t test_rng_nonce(void *vtest, unsigned char *out,
131 unsigned int strength,
132 ossl_unused size_t min_noncelen,
133 ossl_unused size_t max_noncelen)
f3a25707 134{
1f50630a 135 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
f3a25707 136
1f50630a 137 if (t->nonce == NULL || strength > t->strength)
f3a25707
P
138 return 0;
139
140 if (out != NULL)
141 memcpy(out, t->nonce, t->nonce_len);
142 return t->nonce_len;
143}
144
1f50630a 145static int test_rng_get_ctx_params(void *vtest, OSSL_PARAM params[])
f3a25707 146{
1f50630a
P
147 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
148 OSSL_PARAM *p;
f3a25707 149
1f50630a
P
150 p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);
151 if (p != NULL && !OSSL_PARAM_set_int(p, t->state))
152 return 0;
153
154 p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);
155 if (p != NULL && !OSSL_PARAM_set_int(p, t->strength))
156 return 0;
157
158 p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);
159 if (p != NULL && !OSSL_PARAM_set_size_t(p, t->max_request))
160 return 0;
161 return 1;
f3a25707
P
162}
163
1017ab21 164static const OSSL_PARAM *test_rng_gettable_ctx_params(ossl_unused void *provctx)
f3a25707
P
165{
166 static const OSSL_PARAM known_gettable_ctx_params[] = {
1f50630a
P
167 OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL),
168 OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),
169 OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),
f3a25707
P
170 OSSL_PARAM_END
171 };
172 return known_gettable_ctx_params;
173}
174
1f50630a 175static int test_rng_set_ctx_params(void *vtest, const OSSL_PARAM params[])
f3a25707 176{
1f50630a 177 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
f3a25707
P
178 const OSSL_PARAM *p;
179 void *ptr = NULL;
180 size_t size = 0;
f3a25707
P
181
182 p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_STRENGTH);
1f50630a 183 if (p != NULL && !OSSL_PARAM_get_uint(p, &t->strength))
f3a25707
P
184 return 0;
185
186 p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_TEST_ENTROPY);
187 if (p != NULL) {
188 if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))
189 return 0;
190 OPENSSL_free(t->entropy);
191 t->entropy = ptr;
192 t->entropy_len = size;
193 t->entropy_pos = 0;
194 ptr = NULL;
195 }
196
197 p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_TEST_NONCE);
198 if (p != NULL) {
199 if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))
200 return 0;
201 OPENSSL_free(t->nonce);
202 t->nonce = ptr;
203 t->nonce_len = size;
204 }
205
1f50630a
P
206 p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_MAX_REQUEST);
207 if (p != NULL && !OSSL_PARAM_get_size_t(p, &t->max_request))
f3a25707
P
208 return 0;
209
1f50630a 210 return 1;
f3a25707
P
211}
212
1017ab21 213static const OSSL_PARAM *test_rng_settable_ctx_params(ossl_unused void *provctx)
f3a25707
P
214{
215 static const OSSL_PARAM known_settable_ctx_params[] = {
216 OSSL_PARAM_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY, NULL, 0),
217 OSSL_PARAM_octet_string(OSSL_RAND_PARAM_TEST_NONCE, NULL, 0),
218 OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),
08edd447 219 OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),
f3a25707
P
220 OSSL_PARAM_END
221 };
222 return known_settable_ctx_params;
223}
224
1f50630a 225static int test_rng_verify_zeroization(ossl_unused void *vtest)
f3a25707
P
226{
227 return 1;
228}
229
1f50630a 230static int test_rng_enable_locking(void *vtest)
f000e828 231{
1f50630a
P
232 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
233
234 if (t != NULL && t->lock == NULL) {
235 t->lock = CRYPTO_THREAD_lock_new();
236 if (t->lock == NULL) {
237 ERR_raise(ERR_LIB_PROV, RAND_R_FAILED_TO_CREATE_LOCK);
238 return 0;
239 }
240 }
241 return 1;
242}
243
244static int test_rng_lock(void *vtest)
245{
246 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
247
248 if (t == NULL || t->lock == NULL)
249 return 1;
250 return CRYPTO_THREAD_write_lock(t->lock);
251}
252
253static void test_rng_unlock(void *vtest)
254{
255 PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
256
257 if (t != NULL && t->lock != NULL)
258 CRYPTO_THREAD_unlock(t->lock);
f000e828
P
259}
260
1be63951 261const OSSL_DISPATCH ossl_test_rng_functions[] = {
1f50630a 262 { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))test_rng_new },
f3a25707
P
263 { OSSL_FUNC_RAND_FREECTX, (void(*)(void))test_rng_free },
264 { OSSL_FUNC_RAND_INSTANTIATE,
1f50630a 265 (void(*)(void))test_rng_instantiate },
f3a25707 266 { OSSL_FUNC_RAND_UNINSTANTIATE,
1f50630a
P
267 (void(*)(void))test_rng_uninstantiate },
268 { OSSL_FUNC_RAND_GENERATE, (void(*)(void))test_rng_generate },
269 { OSSL_FUNC_RAND_RESEED, (void(*)(void))test_rng_reseed },
f3a25707 270 { OSSL_FUNC_RAND_NONCE, (void(*)(void))test_rng_nonce },
1f50630a
P
271 { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))test_rng_enable_locking },
272 { OSSL_FUNC_RAND_LOCK, (void(*)(void))test_rng_lock },
273 { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))test_rng_unlock },
f3a25707
P
274 { OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS,
275 (void(*)(void))test_rng_settable_ctx_params },
276 { OSSL_FUNC_RAND_SET_CTX_PARAMS, (void(*)(void))test_rng_set_ctx_params },
277 { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
278 (void(*)(void))test_rng_gettable_ctx_params },
279 { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))test_rng_get_ctx_params },
280 { OSSL_FUNC_RAND_VERIFY_ZEROIZATION,
281 (void(*)(void))test_rng_verify_zeroization },
282 { 0, NULL }
283};