fuzzer: add ctx gettable/settable to the fuzzer RNG
[openssl.git] / fuzz / fuzz_rand.c
1 /*
2  * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * https://www.openssl.org/source/license.html
8  * or in the file LICENSE in the source distribution.
9  */
10
11 #include <openssl/core_names.h>
12 #include <openssl/rand.h>
13 #include <openssl/provider.h>
14 #include "fuzzer.h"
15
16 static OSSL_FUNC_rand_newctx_fn fuzz_rand_newctx;
17 static OSSL_FUNC_rand_freectx_fn fuzz_rand_freectx;
18 static OSSL_FUNC_rand_instantiate_fn fuzz_rand_instantiate;
19 static OSSL_FUNC_rand_uninstantiate_fn fuzz_rand_uninstantiate;
20 static OSSL_FUNC_rand_generate_fn fuzz_rand_generate;
21 static OSSL_FUNC_rand_gettable_ctx_params_fn fuzz_rand_gettable_ctx_params;
22 static OSSL_FUNC_rand_get_ctx_params_fn fuzz_rand_get_ctx_params;
23 static OSSL_FUNC_rand_enable_locking_fn fuzz_rand_enable_locking;
24
25 static void *fuzz_rand_newctx(
26          void *provctx, void *parent, const OSSL_DISPATCH *parent_dispatch)
27 {
28     int *st = OPENSSL_malloc(sizeof(*st));
29
30     if (st != NULL)
31         *st = EVP_RAND_STATE_UNINITIALISED;
32     return st;
33 }
34
35 static void fuzz_rand_freectx(ossl_unused void *vrng)
36 {
37     OPENSSL_free(vrng);
38 }
39
40 static int fuzz_rand_instantiate(ossl_unused void *vrng,
41                                  ossl_unused unsigned int strength,
42                                  ossl_unused int prediction_resistance,
43                                  ossl_unused const unsigned char *pstr,
44                                  ossl_unused size_t pstr_len)
45 {
46     *(int *)vrng = EVP_RAND_STATE_READY;
47     return 1;
48 }
49
50 static int fuzz_rand_uninstantiate(ossl_unused void *vrng)
51 {
52     *(int *)vrng = EVP_RAND_STATE_UNINITIALISED;
53     return 1;
54 }
55
56 static int fuzz_rand_generate(ossl_unused void *vdrbg,
57                               unsigned char *out, size_t outlen,
58                               ossl_unused unsigned int strength,
59                               ossl_unused int prediction_resistance,
60                               ossl_unused const unsigned char *adin,
61                               ossl_unused size_t adinlen)
62 {
63     unsigned char val = 1;
64     size_t i;
65
66     for (i = 0; i < outlen; i++)
67         out[i] = val++;
68     return 1;
69 }
70
71 static int fuzz_rand_enable_locking(ossl_unused void *vrng)
72 {
73     return 1;
74 }
75
76 static int fuzz_rand_get_ctx_params(void *vrng, OSSL_PARAM params[])
77 {
78     OSSL_PARAM *p;
79
80     p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);
81     if (p != NULL && !OSSL_PARAM_set_int(p, *(int *)vrng))
82         return 0;
83
84     p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);
85     if (p != NULL && !OSSL_PARAM_set_int(p, 500))
86         return 0;
87
88     p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);
89     if (p != NULL && !OSSL_PARAM_set_size_t(p, INT_MAX))
90         return 0;
91     return 1;
92 }
93
94 static const OSSL_PARAM *fuzz_rand_gettable_ctx_params(ossl_unused void *vrng,
95                                                        ossl_unused void *provctx)
96 {
97     static const OSSL_PARAM known_gettable_ctx_params[] = {
98         OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL),
99         OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),
100         OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),
101         OSSL_PARAM_END
102     };
103     return known_gettable_ctx_params;
104 }
105
106 static const OSSL_DISPATCH fuzz_rand_functions[] = {
107     { OSSL_FUNC_RAND_NEWCTX, (void (*)(void))fuzz_rand_newctx },
108     { OSSL_FUNC_RAND_FREECTX, (void (*)(void))fuzz_rand_freectx },
109     { OSSL_FUNC_RAND_INSTANTIATE, (void (*)(void))fuzz_rand_instantiate },
110     { OSSL_FUNC_RAND_UNINSTANTIATE, (void (*)(void))fuzz_rand_uninstantiate },
111     { OSSL_FUNC_RAND_GENERATE, (void (*)(void))fuzz_rand_generate },
112     { OSSL_FUNC_RAND_ENABLE_LOCKING, (void (*)(void))fuzz_rand_enable_locking },
113     { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
114       (void(*)(void))fuzz_rand_gettable_ctx_params },
115     { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))fuzz_rand_get_ctx_params },
116     { 0, NULL }
117 };
118
119 static const OSSL_ALGORITHM fuzz_rand_rand[] = {
120     { "fuzz", "provider=fuzz-rand", fuzz_rand_functions },
121     { NULL, NULL, NULL }
122 };
123
124 static const OSSL_ALGORITHM *fuzz_rand_query(void *provctx,
125                                              int operation_id,
126                                              int *no_cache)
127 {
128     *no_cache = 0;
129     switch (operation_id) {
130     case OSSL_OP_RAND:
131         return fuzz_rand_rand;
132     }
133     return NULL;
134 }
135
136 /* Functions we provide to the core */
137 static const OSSL_DISPATCH fuzz_rand_method[] = {
138     { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OSSL_LIB_CTX_free },
139     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fuzz_rand_query },
140     { 0, NULL }
141 };
142
143 static int fuzz_rand_provider_init(const OSSL_CORE_HANDLE *handle,
144                                    const OSSL_DISPATCH *in,
145                                    const OSSL_DISPATCH **out, void **provctx)
146 {
147     *provctx = OSSL_LIB_CTX_new();
148     *out = fuzz_rand_method;
149     return 1;
150 }
151
152 static OSSL_PROVIDER *r_prov;
153
154 void FuzzerSetRand(void)
155 {
156     if (!OSSL_PROVIDER_add_builtin(NULL, "fuzz-rand", fuzz_rand_provider_init)
157         || !RAND_set_DRBG_type(NULL, "fuzz", NULL, NULL, NULL)
158         || (r_prov = OSSL_PROVIDER_try_load(NULL, "fuzz-rand", 1)) == NULL)
159         exit(1);
160 }
161
162 void FuzzerClearRand(void)
163 {
164     OSSL_PROVIDER_unload(r_prov);
165 }