-/* $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>
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
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();
}
-/* $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>
* 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();
}
-/* $OpenBSD: arc4random_linux.h,v 1.2 2014/07/18 21:40:54 matthew Exp $ */
+/* $OpenBSD: arc4random_linux.h,v 1.3 2014/07/19 00:08:43 deraadt Exp $ */
/*
* Copyright (c) 1996, David Mazieres <dm@uun.org>
* 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;
-
- if ((p = mmap(NULL, len, PROT_READ|PROT_WRITE,
+ if ((*rsp = mmap(NULL, sizeof(**rsp), PROT_READ|PROT_WRITE,
MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
- return (NULL);
- return (p);
+ return (-1);
+
+ if ((*rsxp = mmap(NULL, sizeof(**rsxp) PROT_READ|PROT_WRITE,
+ MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
+ munmap(*rsxp, sizeof(**rsxp);
+ return (-1);
+ }
+
+ _ARC4_ATFORK(_rs_forkhandler);
+ return (0);
}
static volatile sig_atomic_t _rs_forked;
memset(rs, 0, sizeof(*rs));
}
}
-
-static inline void
-_rs_forkdetectsetup(struct _rs *rs, size_t len)
-{
- _ARC4_ATFORK(_rs_forkhandler);
-}
-
-/* $OpenBSD: arc4random_osx.h,v 1.2 2014/07/18 21:40:54 matthew Exp $ */
+/* $OpenBSD: arc4random_osx.h,v 1.3 2014/07/19 00:08:43 deraadt Exp $ */
/*
* Copyright (c) 1996, David Mazieres <dm@uun.org>
* 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;
-
- if ((p = mmap(NULL, len, PROT_READ|PROT_WRITE,
+ if ((*rsp = mmap(NULL, sizeof(**rsp), PROT_READ|PROT_WRITE,
MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
- return (NULL);
- return (p);
+ return (-1);
+
+ if ((*rsxp = mmap(NULL, sizeof(**rsxp) PROT_READ|PROT_WRITE,
+ MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
+ munmap(*rsxp, sizeof(**rsxp);
+ return (-1);
+ }
+
+ _ARC4_ATFORK(_rs_forkhandler);
+ return (0);
}
static volatile sig_atomic_t _rs_forked;
memset(rs, 0, sizeof(*rs));
}
}
-
-static inline void
-_rs_forkdetectsetup(struct _rs *rs, size_t len)
-{
- _ARC4_ATFORK(_rs_forkhandler);
-}
-
-/* $OpenBSD: arc4random_solaris.h,v 1.2 2014/07/18 21:40:54 matthew Exp $ */
+/* $OpenBSD: arc4random_solaris.h,v 1.3 2014/07/19 00:08:43 deraadt Exp $ */
/*
* Copyright (c) 1996, David Mazieres <dm@uun.org>
* 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;
-
- if ((p = mmap(NULL, len, PROT_READ|PROT_WRITE,
+ if ((*rsp = mmap(NULL, sizeof(**rsp), PROT_READ|PROT_WRITE,
MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
- return (NULL);
- return (p);
+ return (-1);
+
+ if ((*rsxp = mmap(NULL, sizeof(**rsxp) PROT_READ|PROT_WRITE,
+ MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
+ munmap(*rsxp, sizeof(**rsxp);
+ return (-1);
+ }
+
+ _ARC4_ATFORK(_rs_forkhandler);
+ return (0);
}
static volatile sig_atomic_t _rs_forked;
memset(rs, 0, sizeof(*rs));
}
}
-
-static inline void
-_rs_forkdetectsetup(struct _rs *rs, size_t len)
-{
- _ARC4_ATFORK(_rs_forkhandler);
-}
-
-/* $OpenBSD: arc4random_win.h,v 1.1 2014/07/18 02:05:55 deraadt Exp $ */
+/* $OpenBSD: arc4random_win.h,v 1.2 2014/07/19 00:08:43 deraadt Exp $ */
/*
* Copyright (c) 1996, David Mazieres <dm@uun.org>
* Stub functions for portability.
*/
-static inline void *
-_rs_allocate(size_t len)
+static inline int
+_rs_allocate(struct _rs **rsp, struct _rsx **rsxp)
{
- return calloc(1, sizeof(*rs));
+ *rsp = calloc(1, sizeof(**rsp));
+ if (*rsp == NULL)
+ return (-1);
+
+ *rsxp = calloc(1, sizeof(**rsxp));
+ if (*rsxp == NULL) {
+ free(*rsp);
+ return (-1);
+ }
+ return (0);
}
static inline void
_rs_forkdetect(void)
{
}
-
-static inline void
-_rs_forkdetectsetup(struct _rs *rs, size_t len)
-{
-}
-
-/* $OpenBSD: arc4random_linux.h,v 1.2 2014/07/18 21:40:54 matthew Exp $ */
+/* $OpenBSD: arc4random_linux.h,v 1.3 2014/07/19 00:08:43 deraadt Exp $ */
/*
* Copyright (c) 1996, David Mazieres <dm@uun.org>
* 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;
-
- if ((p = mmap(NULL, len, PROT_READ|PROT_WRITE,
+ if ((*rsp = mmap(NULL, sizeof(**rsp), PROT_READ|PROT_WRITE,
MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
- return (NULL);
- return (p);
+ return (-1);
+
+ if ((*rsxp = mmap(NULL, sizeof(**rsxp) PROT_READ|PROT_WRITE,
+ MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
+ munmap(*rsxp, sizeof(**rsxp);
+ return (-1);
+ }
+
+ _ARC4_ATFORK(_rs_forkhandler);
+ return (0);
}
static volatile sig_atomic_t _rs_forked;
memset(rs, 0, sizeof(*rs));
}
}
-
-static inline void
-_rs_forkdetectsetup(struct _rs *rs, size_t len)
-{
- _ARC4_ATFORK(_rs_forkhandler);
-}
-
-/* $OpenBSD: arc4random_osx.h,v 1.2 2014/07/18 21:40:54 matthew Exp $ */
+/* $OpenBSD: arc4random_osx.h,v 1.3 2014/07/19 00:08:43 deraadt Exp $ */
/*
* Copyright (c) 1996, David Mazieres <dm@uun.org>
* 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;
-
- if ((p = mmap(NULL, len, PROT_READ|PROT_WRITE,
+ if ((*rsp = mmap(NULL, sizeof(**rsp), PROT_READ|PROT_WRITE,
MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
- return (NULL);
- return (p);
+ return (-1);
+
+ if ((*rsxp = mmap(NULL, sizeof(**rsxp) PROT_READ|PROT_WRITE,
+ MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
+ munmap(*rsxp, sizeof(**rsxp);
+ return (-1);
+ }
+
+ _ARC4_ATFORK(_rs_forkhandler);
+ return (0);
}
static volatile sig_atomic_t _rs_forked;
memset(rs, 0, sizeof(*rs));
}
}
-
-static inline void
-_rs_forkdetectsetup(struct _rs *rs, size_t len)
-{
- _ARC4_ATFORK(_rs_forkhandler);
-}
-
-/* $OpenBSD: arc4random_solaris.h,v 1.2 2014/07/18 21:40:54 matthew Exp $ */
+/* $OpenBSD: arc4random_solaris.h,v 1.3 2014/07/19 00:08:43 deraadt Exp $ */
/*
* Copyright (c) 1996, David Mazieres <dm@uun.org>
* 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;
-
- if ((p = mmap(NULL, len, PROT_READ|PROT_WRITE,
+ if ((*rsp = mmap(NULL, sizeof(**rsp), PROT_READ|PROT_WRITE,
MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
- return (NULL);
- return (p);
+ return (-1);
+
+ if ((*rsxp = mmap(NULL, sizeof(**rsxp) PROT_READ|PROT_WRITE,
+ MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
+ munmap(*rsxp, sizeof(**rsxp);
+ return (-1);
+ }
+
+ _ARC4_ATFORK(_rs_forkhandler);
+ return (0);
}
static volatile sig_atomic_t _rs_forked;
memset(rs, 0, sizeof(*rs));
}
}
-
-static inline void
-_rs_forkdetectsetup(struct _rs *rs, size_t len)
-{
- _ARC4_ATFORK(_rs_forkhandler);
-}
-
-/* $OpenBSD: arc4random_win.h,v 1.1 2014/07/18 02:05:55 deraadt Exp $ */
+/* $OpenBSD: arc4random_win.h,v 1.2 2014/07/19 00:08:43 deraadt Exp $ */
/*
* Copyright (c) 1996, David Mazieres <dm@uun.org>
* Stub functions for portability.
*/
-static inline void *
-_rs_allocate(size_t len)
+static inline int
+_rs_allocate(struct _rs **rsp, struct _rsx **rsxp)
{
- return calloc(1, sizeof(*rs));
+ *rsp = calloc(1, sizeof(**rsp));
+ if (*rsp == NULL)
+ return (-1);
+
+ *rsxp = calloc(1, sizeof(**rsxp));
+ if (*rsxp == NULL) {
+ free(*rsp);
+ return (-1);
+ }
+ return (0);
}
static inline void
_rs_forkdetect(void)
{
}
-
-static inline void
-_rs_forkdetectsetup(struct _rs *rs, size_t len)
-{
-}
-