diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 2014-07-19 00:08:44 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 2014-07-19 00:08:44 +0000 |
commit | fc3526d08419883e820f414d45908794a984586d (patch) | |
tree | be6d029a291e4c26f8d807669369aa91da154c05 /lib/libc/crypt | |
parent | 77849c775b37d4c7cdb09cba4edc3f9c23496430 (diff) |
Change _rs_allocate so it can combine the two regions (rs and rsx)
into one if a system has an awesome getentropy(). In that case it
is valid to totally throw away the rsx state in the child. If the
getentropy() is not very good and has a lazy reseed operation, this
combining is a bad idea, and the reseed should probably continue to
use the "something old, something new" mix. _rs_allocate() can
accomodate either method, but not on the fly.
ok matthew
Diffstat (limited to 'lib/libc/crypt')
-rw-r--r-- | lib/libc/crypt/arc4random.c | 22 | ||||
-rw-r--r-- | lib/libc/crypt/arc4random.h | 31 |
2 files changed, 25 insertions, 28 deletions
diff --git a/lib/libc/crypt/arc4random.c b/lib/libc/crypt/arc4random.c index d42022c4553..3c80beb3b96 100644 --- a/lib/libc/crypt/arc4random.c +++ b/lib/libc/crypt/arc4random.c @@ -1,4 +1,4 @@ -/* $OpenBSD: arc4random.c,v 1.47 2014/07/18 02:05:55 deraadt Exp $ */ +/* $OpenBSD: arc4random.c,v 1.48 2014/07/19 00:08:41 deraadt Exp $ */ /* * Copyright (c) 1996, David Mazieres <dm@uun.org> @@ -57,17 +57,16 @@ static struct _rs { size_t rs_count; /* bytes till reseed */ } *rs; -static inline void *_rs_allocate(size_t len); -static inline void _rs_forkdetect(void); -static inline void _rs_forkdetectsetup(struct _rs *buf, size_t len); -#include "arc4random.h" - -/* Preserved in fork children. */ -static struct { +/* Maybe be preserved in fork children, if _rs_allocate() decides. */ +static struct _rsx { chacha_ctx rs_chacha; /* chacha context for random keystream */ u_char rs_buf[RSBUFSZ]; /* keystream blocks */ } *rsx; +static inline int _rs_allocate(struct _rs **, struct _rsx **); +static inline void _rs_forkdetect(void); +#include "arc4random.h" + static inline void _rs_rekey(u_char *dat, size_t datlen); static inline void @@ -77,12 +76,7 @@ _rs_init(u_char *buf, size_t n) return; if (rs == NULL) { - if ((rs = _rs_allocate(sizeof(*rs))) == NULL) - abort(); - _rs_forkdetectsetup(rs, sizeof(*rs)); - } - if (rsx == NULL) { - if ((rsx = _rs_allocate(sizeof(*rsx))) == NULL) + if (_rs_allocate(&rs, &rsx) == -1) abort(); } diff --git a/lib/libc/crypt/arc4random.h b/lib/libc/crypt/arc4random.h index e0309a3184f..d867687226f 100644 --- a/lib/libc/crypt/arc4random.h +++ b/lib/libc/crypt/arc4random.h @@ -1,4 +1,4 @@ -/* $OpenBSD: arc4random.h,v 1.1 2014/07/18 02:05:55 deraadt Exp $ */ +/* $OpenBSD: arc4random.h,v 1.2 2014/07/19 00:08:41 deraadt Exp $ */ /* * Copyright (c) 1996, David Mazieres <dm@uun.org> @@ -22,25 +22,28 @@ * Stub functions for portability. */ -static inline void * -_rs_allocate(size_t len) +static inline int +_rs_allocate(struct _rs **rsp, struct _rsx **rsxp) { - void *p; + struct { + struct _rs rs; + struct _rsx rsx; + } *p; - if ((p = mmap(NULL, sizeof(*rs), PROT_READ|PROT_WRITE, + if ((p = mmap(NULL, sizeof(*p), PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) - return (NULL); - return (p); -} + return (-1); + if (minherit(p, sizeof(*p), MAP_INHERIT_ZERO) == -1) { + munmap(p, sizeof(*p)); + return (-1); + } -static inline void -_rs_forkdetect(void) -{ + *rsp = &p->rs; + *rsxp = &p->rsx; + return (0); } static inline void -_rs_forkdetectsetup(struct _rs *rs, size_t len) +_rs_forkdetect(void) { - if (minherit(rs, len, MAP_INHERIT_ZERO) == -1) - abort(); } |