Import LibreSSL v2.4.2 to vendor branch
[dragonfly.git] / crypto / libressl / crypto / compat / arc4random_win.h
blob3e808003cb48348d7e9384d02810b72a6706b32c
1 /* $OpenBSD: arc4random_win.h,v 1.4 2014/07/20 20:51:13 bcook Exp $ */
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 * Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Stub functions for portability.
26 #include <windows.h>
28 static volatile HANDLE arc4random_mtx = NULL;
31 * Initialize the mutex on the first lock attempt. On collision, each thread
32 * will attempt to allocate a mutex and compare-and-swap it into place as the
33 * global mutex. On failure to swap in the global mutex, the mutex is closed.
35 #define _ARC4_LOCK() { \
36 if (!arc4random_mtx) { \
37 HANDLE p = CreateMutex(NULL, FALSE, NULL); \
38 if (InterlockedCompareExchangePointer((void **)&arc4random_mtx, (void *)p, NULL)) \
39 CloseHandle(p); \
40 } \
41 WaitForSingleObject(arc4random_mtx, INFINITE); \
42 } \
44 #define _ARC4_UNLOCK() ReleaseMutex(arc4random_mtx)
46 static inline void
47 _getentropy_fail(void)
49 TerminateProcess(GetCurrentProcess(), 0);
52 static inline int
53 _rs_allocate(struct _rs **rsp, struct _rsx **rsxp)
55 *rsp = VirtualAlloc(NULL, sizeof(**rsp),
56 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
57 if (*rsp == NULL)
58 return (-1);
60 *rsxp = VirtualAlloc(NULL, sizeof(**rsxp),
61 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
62 if (*rsxp == NULL) {
63 VirtualFree(*rsp, 0, MEM_RELEASE);
64 *rsp = NULL;
65 return (-1);
67 return (0);
70 static inline void
71 _rs_forkhandler(void)
75 static inline void
76 _rs_forkdetect(void)