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/core_dispatch.h>
12 #include <openssl/e_os2.h>
13 #include <openssl/params.h>
14 #include "prov/providercommon.h"
15 #include "prov/provider_ctx.h"
16 #include "prov/provider_util.h"
17 #include "prov/implementations.h"
18 #include "drbg_local.h"
20 static OSSL_FUNC_rand_newctx_fn test_rng_new_wrapper
;
21 static OSSL_FUNC_rand_freectx_fn test_rng_free
;
22 static OSSL_FUNC_rand_instantiate_fn test_rng_instantiate_wrapper
;
23 static OSSL_FUNC_rand_uninstantiate_fn test_rng_uninstantiate_wrapper
;
24 static OSSL_FUNC_rand_generate_fn test_rng_generate_wrapper
;
25 static OSSL_FUNC_rand_reseed_fn test_rng_reseed_wrapper
;
26 static OSSL_FUNC_rand_nonce_fn test_rng_nonce
;
27 static OSSL_FUNC_rand_settable_ctx_params_fn test_rng_settable_ctx_params
;
28 static OSSL_FUNC_rand_set_ctx_params_fn test_rng_set_ctx_params
;
29 static OSSL_FUNC_rand_gettable_ctx_params_fn test_rng_gettable_ctx_params
;
30 static OSSL_FUNC_rand_get_ctx_params_fn test_rng_get_ctx_params
;
31 static OSSL_FUNC_rand_verify_zeroization_fn test_rng_verify_zeroization
;
34 unsigned char *entropy
, *nonce
;
35 size_t entropy_len
, entropy_pos
, nonce_len
;
36 unsigned int strength
;
39 static int test_rng_new(PROV_DRBG
*ctx
)
43 t
= OPENSSL_zalloc(sizeof(*t
));
47 ctx
->seedlen
= INT_MAX
;
48 ctx
->max_entropylen
= INT_MAX
;
49 ctx
->max_noncelen
= INT_MAX
;
50 ctx
->max_perslen
= INT_MAX
;
51 ctx
->max_adinlen
= INT_MAX
;
52 ctx
->max_request
= INT_MAX
;
56 static void test_rng_free(void *vdrbg
)
58 PROV_DRBG
*drbg
= (PROV_DRBG
*)vdrbg
;
59 PROV_TEST_RNG
*t
= (PROV_TEST_RNG
*)drbg
->data
;
61 OPENSSL_free(t
->entropy
);
62 OPENSSL_free(t
->nonce
);
63 OPENSSL_free(drbg
->data
);
64 prov_rand_drbg_free(drbg
);
67 static int test_rng_instantiate(PROV_DRBG
*drbg
,
68 const unsigned char *ent
, size_t ent_len
,
69 const unsigned char *nonce
, size_t nonce_len
,
70 const unsigned char *pstr
, size_t pstr_len
)
72 PROV_TEST_RNG
*t
= (PROV_TEST_RNG
*)drbg
->data
;
74 if (ent
!= NULL
&& (ent_len
< drbg
->min_entropylen
75 || ent_len
>= drbg
->max_entropylen
))
77 if (nonce
!= NULL
&& (nonce_len
< drbg
->min_noncelen
78 || nonce_len
>= drbg
->max_noncelen
))
80 if (pstr
!= NULL
&& pstr_len
>= drbg
->max_perslen
)
87 static int test_rng_instantiate_wrapper(void *vdrbg
, unsigned int strength
,
88 int prediction_resistance
,
89 const unsigned char *pstr
,
92 PROV_DRBG
*drbg
= (PROV_DRBG
*)vdrbg
;
94 if (pstr
!= NULL
&& pstr_len
>= drbg
->max_perslen
)
97 return PROV_DRBG_instantiate(drbg
, strength
, prediction_resistance
,
101 static int test_rng_uninstantiate(PROV_DRBG
*drbg
)
103 PROV_TEST_RNG
*t
= (PROV_TEST_RNG
*)drbg
->data
;
106 return PROV_DRBG_uninstantiate(drbg
);
109 static int test_rng_uninstantiate_wrapper(void *vdrbg
)
111 return test_rng_uninstantiate((PROV_DRBG
*)vdrbg
);
114 static int test_rng_generate(PROV_DRBG
*drbg
,
115 unsigned char *out
, size_t outlen
,
116 const unsigned char *adin
, size_t adin_len
)
118 PROV_TEST_RNG
*t
= (PROV_TEST_RNG
*)drbg
->data
;
121 if (t
->entropy
== NULL
|| (adin
!= NULL
&& adin_len
>= drbg
->max_adinlen
))
124 for (i
= 0; i
< outlen
; i
++) {
125 out
[i
] = t
->entropy
[t
->entropy_pos
++];
126 if (t
->entropy_pos
>= t
->entropy_len
)
132 static int test_rng_generate_wrapper
133 (void *vdrbg
, unsigned char *out
, size_t outlen
,
134 unsigned int strength
, int prediction_resistance
,
135 const unsigned char *adin
, size_t adin_len
)
137 PROV_DRBG
*drbg
= (PROV_DRBG
*)vdrbg
;
139 if (strength
> drbg
->strength
)
141 return test_rng_generate(drbg
, out
, outlen
, adin
, adin_len
);
144 static int test_rng_reseed(PROV_DRBG
*drbg
,
145 const unsigned char *ent
, size_t ent_len
,
146 const unsigned char *adin
, size_t adin_len
)
148 if (ent
!= NULL
&& (ent_len
< drbg
->min_entropylen
149 || ent_len
>= drbg
->max_entropylen
))
151 if (adin
!= NULL
&& adin_len
>= drbg
->max_adinlen
)
157 static int test_rng_reseed_wrapper(void *vdrbg
, int prediction_resistance
,
158 const unsigned char *ent
, size_t ent_len
,
159 const unsigned char *adin
, size_t adin_len
)
161 return test_rng_reseed((PROV_DRBG
*)vdrbg
, ent
, ent_len
, adin
, adin_len
);
164 static size_t test_rng_nonce(void *vdrbg
, unsigned char *out
,
165 unsigned int strength
, size_t min_noncelen
,
168 PROV_DRBG
*drbg
= (PROV_DRBG
*)vdrbg
;
169 PROV_TEST_RNG
*t
= (PROV_TEST_RNG
*)drbg
->data
;
172 || strength
> drbg
->strength
173 || min_noncelen
> t
->nonce_len
174 || max_noncelen
< t
->nonce_len
)
178 memcpy(out
, t
->nonce
, t
->nonce_len
);
182 static int test_rng_get_ctx_params(void *vdrbg
, OSSL_PARAM params
[])
184 PROV_DRBG
*drbg
= (PROV_DRBG
*)vdrbg
;
186 return drbg_get_ctx_params(drbg
, params
);
189 static const OSSL_PARAM
*test_rng_gettable_ctx_params(void)
191 static const OSSL_PARAM known_gettable_ctx_params
[] = {
192 OSSL_PARAM_DRBG_GETABLE_CTX_COMMON
,
195 return known_gettable_ctx_params
;
198 static int set_size_t(const OSSL_PARAM
*params
, const char *name
,
201 const OSSL_PARAM
*p
= OSSL_PARAM_locate_const(params
, name
);
203 return p
== NULL
|| OSSL_PARAM_get_size_t(p
, val
);
206 static int test_rng_set_ctx_params(void *vdrbg
, const OSSL_PARAM params
[])
208 PROV_DRBG
*drbg
= (PROV_DRBG
*)vdrbg
;
209 PROV_TEST_RNG
*t
= (PROV_TEST_RNG
*)drbg
->data
;
215 p
= OSSL_PARAM_locate_const(params
, OSSL_RAND_PARAM_STRENGTH
);
216 if (p
!= NULL
&& !OSSL_PARAM_get_uint(p
, &drbg
->strength
))
219 p
= OSSL_PARAM_locate_const(params
, OSSL_RAND_PARAM_TEST_ENTROPY
);
221 if (!OSSL_PARAM_get_octet_string(p
, &ptr
, 0, &size
))
223 OPENSSL_free(t
->entropy
);
225 t
->entropy_len
= size
;
230 p
= OSSL_PARAM_locate_const(params
, OSSL_RAND_PARAM_TEST_NONCE
);
232 if (!OSSL_PARAM_get_octet_string(p
, &ptr
, 0, &size
))
234 OPENSSL_free(t
->nonce
);
239 p
= OSSL_PARAM_locate_const(params
, OSSL_DRBG_PARAM_RESEED_CTR
);
241 if (!OSSL_PARAM_get_uint(p
, &uint
))
243 tsan_store(&drbg
->reseed_counter
, uint
);
246 p
= OSSL_PARAM_locate_const(params
, OSSL_DRBG_PARAM_RESEED_TIME
);
247 if (p
!= NULL
&& !OSSL_PARAM_get_time_t(p
, &drbg
->reseed_time
))
250 if (!set_size_t(params
, OSSL_DRBG_PARAM_MAX_REQUEST
, &drbg
->max_request
)
251 || !set_size_t(params
, OSSL_DRBG_PARAM_MIN_ENTROPYLEN
,
252 &drbg
->min_entropylen
)
253 || !set_size_t(params
, OSSL_DRBG_PARAM_MAX_ENTROPYLEN
,
254 &drbg
->max_entropylen
)
255 || !set_size_t(params
, OSSL_DRBG_PARAM_MIN_NONCELEN
,
257 || !set_size_t(params
, OSSL_DRBG_PARAM_MAX_NONCELEN
,
259 || !set_size_t(params
, OSSL_DRBG_PARAM_MAX_PERSLEN
,
261 || !set_size_t(params
, OSSL_DRBG_PARAM_MAX_ADINLEN
,
264 return drbg_set_ctx_params(drbg
, params
);
267 static const OSSL_PARAM
*test_rng_settable_ctx_params(void)
269 static const OSSL_PARAM known_settable_ctx_params
[] = {
270 OSSL_PARAM_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY
, NULL
, 0),
271 OSSL_PARAM_octet_string(OSSL_RAND_PARAM_TEST_NONCE
, NULL
, 0),
272 OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH
, NULL
),
273 OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_REQUEST
, NULL
),
274 OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MIN_ENTROPYLEN
, NULL
),
275 OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_ENTROPYLEN
, NULL
),
276 OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MIN_NONCELEN
, NULL
),
277 OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_NONCELEN
, NULL
),
278 OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_PERSLEN
, NULL
),
279 OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_ADINLEN
, NULL
),
280 OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_CTR
, NULL
),
281 OSSL_PARAM_time_t(OSSL_DRBG_PARAM_RESEED_TIME
, NULL
),
282 OSSL_PARAM_DRBG_SETABLE_CTX_COMMON
,
285 return known_settable_ctx_params
;
288 static int test_rng_verify_zeroization(void *vdrbg
)
293 static void *test_rng_new_wrapper(void *provctx
, void *parent
,
294 const OSSL_DISPATCH
*parent_dispatch
)
296 return prov_rand_drbg_new(provctx
, parent
, parent_dispatch
,
297 &test_rng_new
, &test_rng_instantiate
,
298 &test_rng_uninstantiate
, &test_rng_reseed
,
302 const OSSL_DISPATCH test_rng_functions
[] = {
303 { OSSL_FUNC_RAND_NEWCTX
, (void(*)(void))test_rng_new_wrapper
},
304 { OSSL_FUNC_RAND_FREECTX
, (void(*)(void))test_rng_free
},
305 { OSSL_FUNC_RAND_INSTANTIATE
,
306 (void(*)(void))test_rng_instantiate_wrapper
},
307 { OSSL_FUNC_RAND_UNINSTANTIATE
,
308 (void(*)(void))test_rng_uninstantiate_wrapper
},
309 { OSSL_FUNC_RAND_GENERATE
, (void(*)(void))test_rng_generate_wrapper
},
310 { OSSL_FUNC_RAND_RESEED
, (void(*)(void))test_rng_reseed_wrapper
},
311 { OSSL_FUNC_RAND_NONCE
, (void(*)(void))test_rng_nonce
},
312 { OSSL_FUNC_RAND_ENABLE_LOCKING
, (void(*)(void))drbg_enable_locking
},
313 { OSSL_FUNC_RAND_LOCK
, (void(*)(void))drbg_lock
},
314 { OSSL_FUNC_RAND_UNLOCK
, (void(*)(void))drbg_unlock
},
315 { OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS
,
316 (void(*)(void))test_rng_settable_ctx_params
},
317 { OSSL_FUNC_RAND_SET_CTX_PARAMS
, (void(*)(void))test_rng_set_ctx_params
},
318 { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS
,
319 (void(*)(void))test_rng_gettable_ctx_params
},
320 { OSSL_FUNC_RAND_GET_CTX_PARAMS
, (void(*)(void))test_rng_get_ctx_params
},
321 { OSSL_FUNC_RAND_VERIFY_ZEROIZATION
,
322 (void(*)(void))test_rng_verify_zeroization
},