summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/rand/rand_lib.c
diff options
context:
space:
mode:
authormiod <>2014-04-15 16:52:50 +0000
committermiod <>2014-04-15 16:52:50 +0000
commitbe03b064bffafbd378c0d5cc5971594573544d64 (patch)
treecd099da9298b8ab84a5dbbead9a6560737057c97 /src/lib/libcrypto/rand/rand_lib.c
parentf08ae3b01d60723e8f4334e0aaf4a57f03c478ba (diff)
downloadopenbsd-be03b064bffafbd378c0d5cc5971594573544d64.tar.gz
openbsd-be03b064bffafbd378c0d5cc5971594573544d64.tar.bz2
openbsd-be03b064bffafbd378c0d5cc5971594573544d64.zip
Replace the old OpenSSL PRNG by direct use of arc4random_buf(), keeping the
existing RAND interfaces unchanged. All interfaces allowing external feed or seed of the RNG (either from a file or a local entropy gathering daemon) are kept for ABI compatibility, but are no longer do anything. While the OpenSSL PRNG was required 15+ years ago when many systems lacked proper entropy collection, things have evolved and one can reasonably assume it is better to use the kernel (system global) entropy pool rather than trying to build one's own and having to compensate for thread scheduling... <RANT> Whoever thought that RAND_screen(), feeding the PRNG with the contents of the local workstation's display, under Win32, was a smart idea, ought to be banned from security programming. </RANT> ok beck@ deraadt@ tedu@
Diffstat (limited to 'src/lib/libcrypto/rand/rand_lib.c')
-rw-r--r--src/lib/libcrypto/rand/rand_lib.c129
1 files changed, 0 insertions, 129 deletions
diff --git a/src/lib/libcrypto/rand/rand_lib.c b/src/lib/libcrypto/rand/rand_lib.c
index 5ac0e14caf..243a87ddfb 100644
--- a/src/lib/libcrypto/rand/rand_lib.c
+++ b/src/lib/libcrypto/rand/rand_lib.c
@@ -65,11 +65,6 @@
65#include <openssl/engine.h> 65#include <openssl/engine.h>
66#endif 66#endif
67 67
68#ifdef OPENSSL_FIPS
69#include <openssl/fips.h>
70#include <openssl/fips_rand.h>
71#endif
72
73#ifndef OPENSSL_NO_ENGINE 68#ifndef OPENSSL_NO_ENGINE
74/* non-NULL if default_RAND_meth is ENGINE-provided */ 69/* non-NULL if default_RAND_meth is ENGINE-provided */
75static ENGINE *funct_ref =NULL; 70static ENGINE *funct_ref =NULL;
@@ -180,127 +175,3 @@ int RAND_status(void)
180 return meth->status(); 175 return meth->status();
181 return 0; 176 return 0;
182 } 177 }
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
194static 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
211static void drbg_free_entropy(DRBG_CTX *ctx, unsigned char *out, size_t olen)
212 {
213 if (out)
214 {
215 OPENSSL_cleanse(out, olen);
216 OPENSSL_free(out);
217 }
218 }
219
220/* Set "additional input" when generating random data. This uses the
221 * current PID, a time value and a counter.
222 */
223
224static size_t drbg_get_adin(DRBG_CTX *ctx, unsigned char **pout)
225 {
226 /* Use of static variables is OK as this happens under a lock */
227 static unsigned char buf[16];
228 static unsigned long counter;
229 FIPS_get_timevec(buf, &counter);
230 *pout = buf;
231 return sizeof(buf);
232 }
233
234/* RAND_add() and RAND_seed() pass through to OpenSSL PRNG so it is
235 * correctly seeded by RAND_poll().
236 */
237
238static int drbg_rand_add(DRBG_CTX *ctx, const void *in, int inlen,
239 double entropy)
240 {
241 RAND_SSLeay()->add(in, inlen, entropy);
242 return 1;
243 }
244
245static int drbg_rand_seed(DRBG_CTX *ctx, const void *in, int inlen)
246 {
247 RAND_SSLeay()->seed(in, inlen);
248 return 1;
249 }
250
251#ifndef OPENSSL_DRBG_DEFAULT_TYPE
252#define OPENSSL_DRBG_DEFAULT_TYPE NID_aes_256_ctr
253#endif
254#ifndef OPENSSL_DRBG_DEFAULT_FLAGS
255#define OPENSSL_DRBG_DEFAULT_FLAGS DRBG_FLAG_CTR_USE_DF
256#endif
257
258static int fips_drbg_type = OPENSSL_DRBG_DEFAULT_TYPE;
259static int fips_drbg_flags = OPENSSL_DRBG_DEFAULT_FLAGS;
260
261void RAND_set_fips_drbg_type(int type, int flags)
262 {
263 fips_drbg_type = type;
264 fips_drbg_flags = flags;
265 }
266
267int RAND_init_fips(void)
268 {
269 DRBG_CTX *dctx;
270 size_t plen;
271 unsigned char pers[32], *p;
272#ifndef OPENSSL_ALLOW_DUAL_EC_DRBG
273 if (fips_drbg_type >> 16)
274 {
275 RANDerr(RAND_F_RAND_INIT_FIPS, RAND_R_DUAL_EC_DRBG_DISABLED);
276 return 0;
277 }
278#endif
279
280 dctx = FIPS_get_default_drbg();
281 if (FIPS_drbg_init(dctx, fips_drbg_type, fips_drbg_flags) <= 0)
282 {
283 RANDerr(RAND_F_RAND_INIT_FIPS, RAND_R_ERROR_INITIALISING_DRBG);
284 return 0;
285 }
286
287 FIPS_drbg_set_callbacks(dctx,
288 drbg_get_entropy, drbg_free_entropy, 20,
289 drbg_get_entropy, drbg_free_entropy);
290 FIPS_drbg_set_rand_callbacks(dctx, drbg_get_adin, 0,
291 drbg_rand_seed, drbg_rand_add);
292 /* Personalisation string: a string followed by date time vector */
293 strcpy((char *)pers, "OpenSSL DRBG2.0");
294 plen = drbg_get_adin(dctx, &p);
295 memcpy(pers + 16, p, plen);
296
297 if (FIPS_drbg_instantiate(dctx, pers, sizeof(pers)) <= 0)
298 {
299 RANDerr(RAND_F_RAND_INIT_FIPS, RAND_R_ERROR_INSTANTIATING_DRBG);
300 return 0;
301 }
302 FIPS_rand_set_method(FIPS_drbg_method());
303 return 1;
304 }
305
306#endif