summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/crypto/arc4random_hpux.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/crypto/arc4random_hpux.h')
-rw-r--r--src/lib/libcrypto/crypto/arc4random_hpux.h79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/lib/libcrypto/crypto/arc4random_hpux.h b/src/lib/libcrypto/crypto/arc4random_hpux.h
new file mode 100644
index 0000000000..b6841f583c
--- /dev/null
+++ b/src/lib/libcrypto/crypto/arc4random_hpux.h
@@ -0,0 +1,79 @@
1/* $OpenBSD: arc4random_hpux.h,v 1.1 2015/01/06 21:08:11 bcook Exp $ */
2
3/*
4 * Copyright (c) 1996, David Mazieres <dm@uun.org>
5 * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
6 * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
21/*
22 * Stub functions for portability.
23 */
24
25#include <sys/mman.h>
26
27#include <pthread.h>
28#include <signal.h>
29
30static pthread_mutex_t arc4random_mtx = PTHREAD_MUTEX_INITIALIZER;
31#define _ARC4_LOCK() pthread_mutex_lock(&arc4random_mtx)
32#define _ARC4_UNLOCK() pthread_mutex_unlock(&arc4random_mtx)
33
34#define _ARC4_ATFORK(f) pthread_atfork(NULL, NULL, (f))
35
36static inline void
37_getentropy_fail(void)
38{
39 raise(SIGKILL);
40}
41
42static volatile sig_atomic_t _rs_forked;
43
44static inline void
45_rs_forkhandler(void)
46{
47 _rs_forked = 1;
48}
49
50static inline void
51_rs_forkdetect(void)
52{
53 static pid_t _rs_pid = 0;
54 pid_t pid = getpid();
55
56 if (_rs_pid == 0 || _rs_pid != pid || _rs_forked) {
57 _rs_pid = pid;
58 _rs_forked = 0;
59 if (rs)
60 memset(rs, 0, sizeof(*rs));
61 }
62}
63
64static inline int
65_rs_allocate(struct _rs **rsp, struct _rsx **rsxp)
66{
67 if ((*rsp = mmap(NULL, sizeof(**rsp), PROT_READ|PROT_WRITE,
68 MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
69 return (-1);
70
71 if ((*rsxp = mmap(NULL, sizeof(**rsxp), PROT_READ|PROT_WRITE,
72 MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
73 munmap(*rsp, sizeof(**rsp));
74 return (-1);
75 }
76
77 _ARC4_ATFORK(_rs_forkhandler);
78 return (0);
79}