lua-bcrypt

Secure password hashing for Lua
Log | Files | Refs | README

arc4random_win.h (2069B)


      1 /*	$OpenBSD: arc4random_win.h,v 1.3 2014/07/20 16:59:31 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 <windows.h>
     26 
     27 static volatile HANDLE arc4random_mtx = NULL;
     28 
     29 /*
     30  * Initialize the mutex on the first lock attempt. On collision, each thread
     31  * will attempt to allocate a mutex and compare-and-swap it into place as the
     32  * global mutex. On failure to swap in the global mutex, the mutex is closed.
     33  */
     34 #define _ARC4_LOCK() { \
     35 	if (!arc4random_mtx) { \
     36 		HANDLE p = CreateMutex(NULL, FALSE, NULL); \
     37 		if (InterlockedCompareExchangePointer((void **)&arc4random_mtx, (void *)p, NULL)) \
     38 			CloseHandle(p); \
     39 	} \
     40 	WaitForSingleObject(arc4random_mtx, INFINITE); \
     41 } \
     42 
     43 #define _ARC4_UNLOCK() ReleaseMutex(arc4random_mtx)
     44 
     45 static inline void
     46 _getentropy_fail(void)
     47 {
     48 	TerminateProcess(GetCurrentProcess(), 0);
     49 }
     50 
     51 static inline int
     52 _rs_allocate(struct _rs **rsp, struct _rsx **rsxp)
     53 {
     54 	*rsp = calloc(1, sizeof(**rsp));
     55 	if (*rsp == NULL)
     56 		return (-1);
     57 
     58 	*rsxp = calloc(1, sizeof(**rsxp));
     59 	if (*rsxp == NULL) {
     60 		free(*rsp);
     61 		return (-1);
     62 	}
     63 	return (0);
     64 }
     65 
     66 static inline void
     67 _rs_forkhandler(void)
     68 {
     69 }
     70 
     71 static inline void
     72 _rs_forkdetect(void)
     73 {
     74 }