diff options
Diffstat (limited to 'src/lib/libcrypto/rand/rand_lib.c')
-rw-r--r-- | src/lib/libcrypto/rand/rand_lib.c | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/src/lib/libcrypto/rand/rand_lib.c b/src/lib/libcrypto/rand/rand_lib.c index 513e338985..daf1dab973 100644 --- a/src/lib/libcrypto/rand/rand_lib.c +++ b/src/lib/libcrypto/rand/rand_lib.c | |||
@@ -60,10 +60,16 @@ | |||
60 | #include <time.h> | 60 | #include <time.h> |
61 | #include "cryptlib.h" | 61 | #include "cryptlib.h" |
62 | #include <openssl/rand.h> | 62 | #include <openssl/rand.h> |
63 | |||
63 | #ifndef OPENSSL_NO_ENGINE | 64 | #ifndef OPENSSL_NO_ENGINE |
64 | #include <openssl/engine.h> | 65 | #include <openssl/engine.h> |
65 | #endif | 66 | #endif |
66 | 67 | ||
68 | #ifdef OPENSSL_FIPS | ||
69 | #include <openssl/fips.h> | ||
70 | #include <openssl/fips_rand.h> | ||
71 | #endif | ||
72 | |||
67 | #ifndef OPENSSL_NO_ENGINE | 73 | #ifndef OPENSSL_NO_ENGINE |
68 | /* non-NULL if default_RAND_meth is ENGINE-provided */ | 74 | /* non-NULL if default_RAND_meth is ENGINE-provided */ |
69 | static ENGINE *funct_ref =NULL; | 75 | static ENGINE *funct_ref =NULL; |
@@ -174,3 +180,116 @@ int RAND_status(void) | |||
174 | return meth->status(); | 180 | return meth->status(); |
175 | return 0; | 181 | return 0; |
176 | } | 182 | } |
183 | |||
184 | #ifdef OPENSSL_FIPS | ||
185 | |||
186 | /* FIPS DRBG initialisation code. This sets up the DRBG for use by the | ||
187 | * rest of OpenSSL. | ||
188 | */ | ||
189 | |||
190 | /* Entropy gatherer: use standard OpenSSL PRNG to seed (this will gather | ||
191 | * entropy internally through RAND_poll(). | ||
192 | */ | ||
193 | |||
194 | static size_t drbg_get_entropy(DRBG_CTX *ctx, unsigned char **pout, | ||
195 | int entropy, size_t min_len, size_t max_len) | ||
196 | { | ||
197 | /* Round up request to multiple of block size */ | ||
198 | min_len = ((min_len + 19) / 20) * 20; | ||
199 | *pout = OPENSSL_malloc(min_len); | ||
200 | if (!*pout) | ||
201 | return 0; | ||
202 | if (RAND_SSLeay()->bytes(*pout, min_len) <= 0) | ||
203 | { | ||
204 | OPENSSL_free(*pout); | ||
205 | *pout = NULL; | ||
206 | return 0; | ||
207 | } | ||
208 | return min_len; | ||
209 | } | ||
210 | |||
211 | static void drbg_free_entropy(DRBG_CTX *ctx, unsigned char *out, size_t olen) | ||
212 | { | ||
213 | OPENSSL_cleanse(out, olen); | ||
214 | OPENSSL_free(out); | ||
215 | } | ||
216 | |||
217 | /* Set "additional input" when generating random data. This uses the | ||
218 | * current PID, a time value and a counter. | ||
219 | */ | ||
220 | |||
221 | static size_t drbg_get_adin(DRBG_CTX *ctx, unsigned char **pout) | ||
222 | { | ||
223 | /* Use of static variables is OK as this happens under a lock */ | ||
224 | static unsigned char buf[16]; | ||
225 | static unsigned long counter; | ||
226 | FIPS_get_timevec(buf, &counter); | ||
227 | *pout = buf; | ||
228 | return sizeof(buf); | ||
229 | } | ||
230 | |||
231 | /* RAND_add() and RAND_seed() pass through to OpenSSL PRNG so it is | ||
232 | * correctly seeded by RAND_poll(). | ||
233 | */ | ||
234 | |||
235 | static int drbg_rand_add(DRBG_CTX *ctx, const void *in, int inlen, | ||
236 | double entropy) | ||
237 | { | ||
238 | RAND_SSLeay()->add(in, inlen, entropy); | ||
239 | return 1; | ||
240 | } | ||
241 | |||
242 | static int drbg_rand_seed(DRBG_CTX *ctx, const void *in, int inlen) | ||
243 | { | ||
244 | RAND_SSLeay()->seed(in, inlen); | ||
245 | return 1; | ||
246 | } | ||
247 | |||
248 | #ifndef OPENSSL_DRBG_DEFAULT_TYPE | ||
249 | #define OPENSSL_DRBG_DEFAULT_TYPE NID_aes_256_ctr | ||
250 | #endif | ||
251 | #ifndef OPENSSL_DRBG_DEFAULT_FLAGS | ||
252 | #define OPENSSL_DRBG_DEFAULT_FLAGS DRBG_FLAG_CTR_USE_DF | ||
253 | #endif | ||
254 | |||
255 | static int fips_drbg_type = OPENSSL_DRBG_DEFAULT_TYPE; | ||
256 | static int fips_drbg_flags = OPENSSL_DRBG_DEFAULT_FLAGS; | ||
257 | |||
258 | void RAND_set_fips_drbg_type(int type, int flags) | ||
259 | { | ||
260 | fips_drbg_type = type; | ||
261 | fips_drbg_flags = flags; | ||
262 | } | ||
263 | |||
264 | int RAND_init_fips(void) | ||
265 | { | ||
266 | DRBG_CTX *dctx; | ||
267 | size_t plen; | ||
268 | unsigned char pers[32], *p; | ||
269 | dctx = FIPS_get_default_drbg(); | ||
270 | if (FIPS_drbg_init(dctx, fips_drbg_type, fips_drbg_flags) <= 0) | ||
271 | { | ||
272 | RANDerr(RAND_F_RAND_INIT_FIPS, RAND_R_ERROR_INITIALISING_DRBG); | ||
273 | return 0; | ||
274 | } | ||
275 | |||
276 | FIPS_drbg_set_callbacks(dctx, | ||
277 | drbg_get_entropy, drbg_free_entropy, 20, | ||
278 | drbg_get_entropy, drbg_free_entropy); | ||
279 | FIPS_drbg_set_rand_callbacks(dctx, drbg_get_adin, 0, | ||
280 | drbg_rand_seed, drbg_rand_add); | ||
281 | /* Personalisation string: a string followed by date time vector */ | ||
282 | strcpy((char *)pers, "OpenSSL DRBG2.0"); | ||
283 | plen = drbg_get_adin(dctx, &p); | ||
284 | memcpy(pers + 16, p, plen); | ||
285 | |||
286 | if (FIPS_drbg_instantiate(dctx, pers, sizeof(pers)) <= 0) | ||
287 | { | ||
288 | RANDerr(RAND_F_RAND_INIT_FIPS, RAND_R_ERROR_INSTANTIATING_DRBG); | ||
289 | return 0; | ||
290 | } | ||
291 | FIPS_rand_set_method(FIPS_drbg_method()); | ||
292 | return 1; | ||
293 | } | ||
294 | |||
295 | #endif | ||