1c1e80940SXin LI /* $OpenBSD: arc4random.h,v 1.4 2015/01/15 06:57:18 deraadt Exp $ */ 2c1e80940SXin LI 3c1e80940SXin LI /* 4c1e80940SXin LI * Copyright (c) 1996, David Mazieres <dm@uun.org> 5c1e80940SXin LI * Copyright (c) 2008, Damien Miller <djm@openbsd.org> 6c1e80940SXin LI * Copyright (c) 2013, Markus Friedl <markus@openbsd.org> 7c1e80940SXin LI * Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org> 8c1e80940SXin LI * 9c1e80940SXin LI * Permission to use, copy, modify, and distribute this software for any 10c1e80940SXin LI * purpose with or without fee is hereby granted, provided that the above 11c1e80940SXin LI * copyright notice and this permission notice appear in all copies. 12c1e80940SXin LI * 13c1e80940SXin LI * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 14c1e80940SXin LI * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 15c1e80940SXin LI * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 16c1e80940SXin LI * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 17c1e80940SXin LI * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 18c1e80940SXin LI * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 19c1e80940SXin LI * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20c1e80940SXin LI * 21c1e80940SXin LI * $FreeBSD$ 22c1e80940SXin LI */ 23c1e80940SXin LI 24c1e80940SXin LI /* 25c1e80940SXin LI * Stub functions for portability. 26c1e80940SXin LI */ 27c1e80940SXin LI #include <sys/mman.h> 28c1e80940SXin LI 29c1e80940SXin LI #include <signal.h> 30c1e80940SXin LI 31c1e80940SXin LI static pthread_mutex_t arc4random_mtx = PTHREAD_MUTEX_INITIALIZER; 32c1e80940SXin LI #define _ARC4_LOCK() \ 33c1e80940SXin LI do { \ 34c1e80940SXin LI if (__isthreaded) \ 35c1e80940SXin LI _pthread_mutex_lock(&arc4random_mtx); \ 36c1e80940SXin LI } while (0) 37c1e80940SXin LI 38c1e80940SXin LI #define _ARC4_UNLOCK() \ 39c1e80940SXin LI do { \ 40c1e80940SXin LI if (__isthreaded) \ 41c1e80940SXin LI _pthread_mutex_unlock(&arc4random_mtx); \ 42c1e80940SXin LI } while (0) 43c1e80940SXin LI 44c1e80940SXin LI static inline void 45c1e80940SXin LI _getentropy_fail(void) 46c1e80940SXin LI { 47c1e80940SXin LI raise(SIGKILL); 48c1e80940SXin LI } 49c1e80940SXin LI 50c1e80940SXin LI static inline int 51c1e80940SXin LI _rs_allocate(struct _rs **rsp, struct _rsx **rsxp) 52c1e80940SXin LI { 53c1e80940SXin LI struct { 54c1e80940SXin LI struct _rs rs; 55c1e80940SXin LI struct _rsx rsx; 56c1e80940SXin LI } *p; 57c1e80940SXin LI 58c1e80940SXin LI if ((p = mmap(NULL, sizeof(*p), PROT_READ|PROT_WRITE, 59c1e80940SXin LI MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) 60c1e80940SXin LI return (-1); 61*7ec1ec4fSAlex Richardson /* Allow bootstrapping arc4random.c on Linux/macOS */ 62*7ec1ec4fSAlex Richardson #ifdef INHERIT_ZERO 63c1e80940SXin LI if (minherit(p, sizeof(*p), INHERIT_ZERO) == -1) { 64c1e80940SXin LI munmap(p, sizeof(*p)); 65c1e80940SXin LI return (-1); 66c1e80940SXin LI } 67*7ec1ec4fSAlex Richardson #endif 68c1e80940SXin LI *rsp = &p->rs; 69c1e80940SXin LI *rsxp = &p->rsx; 70c1e80940SXin LI return (0); 71c1e80940SXin LI } 72c1e80940SXin LI 73c1e80940SXin LI static inline void 74c1e80940SXin LI _rs_forkdetect(void) 75c1e80940SXin LI { 76c1e80940SXin LI } 77