From 5fcb913dfef12b63a62cd801952b12fd7772fbef Mon Sep 17 00:00:00 2001 From: bcook <> Date: Sun, 20 Jul 2014 16:59:31 +0000 Subject: [PATCH] initial win32 ARC4_LOCK/UNLOCK implementation. It may make sense to later replace this with a Critical Section later. ok guenther@ --- src/lib/libcrypto/arc4random/arc4random_win.h | 22 ++++++++++++++++++- src/lib/libcrypto/crypto/arc4random_win.h | 22 ++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/lib/libcrypto/arc4random/arc4random_win.h b/src/lib/libcrypto/arc4random/arc4random_win.h index 1fc228d1..1e044de1 100644 --- a/src/lib/libcrypto/arc4random/arc4random_win.h +++ b/src/lib/libcrypto/arc4random/arc4random_win.h @@ -1,4 +1,4 @@ -/* $OpenBSD: arc4random_win.h,v 1.2 2014/07/19 00:08:43 deraadt Exp $ */ +/* $OpenBSD: arc4random_win.h,v 1.3 2014/07/20 16:59:31 bcook Exp $ */ /* * Copyright (c) 1996, David Mazieres @@ -22,6 +22,26 @@ * Stub functions for portability. */ +#include + +static volatile HANDLE arc4random_mtx = NULL; + +/* + * Initialize the mutex on the first lock attempt. On collision, each thread + * will attempt to allocate a mutex and compare-and-swap it into place as the + * global mutex. On failure to swap in the global mutex, the mutex is closed. + */ +#define _ARC4_LOCK() { \ + if (!arc4random_mtx) { \ + HANDLE p = CreateMutex(NULL, FALSE, NULL); \ + if (InterlockedCompareExchangePointer((void **)&arc4random_mtx, (void *)p, NULL)) \ + CloseHandle(p); \ + } \ + WaitForSingleObject(arc4random_mtx, INFINITE); \ +} \ + +#define _ARC4_UNLOCK() ReleaseMutex(arc4random_mtx) + static inline int _rs_allocate(struct _rs **rsp, struct _rsx **rsxp) { diff --git a/src/lib/libcrypto/crypto/arc4random_win.h b/src/lib/libcrypto/crypto/arc4random_win.h index 1fc228d1..1e044de1 100644 --- a/src/lib/libcrypto/crypto/arc4random_win.h +++ b/src/lib/libcrypto/crypto/arc4random_win.h @@ -1,4 +1,4 @@ -/* $OpenBSD: arc4random_win.h,v 1.2 2014/07/19 00:08:43 deraadt Exp $ */ +/* $OpenBSD: arc4random_win.h,v 1.3 2014/07/20 16:59:31 bcook Exp $ */ /* * Copyright (c) 1996, David Mazieres @@ -22,6 +22,26 @@ * Stub functions for portability. */ +#include + +static volatile HANDLE arc4random_mtx = NULL; + +/* + * Initialize the mutex on the first lock attempt. On collision, each thread + * will attempt to allocate a mutex and compare-and-swap it into place as the + * global mutex. On failure to swap in the global mutex, the mutex is closed. + */ +#define _ARC4_LOCK() { \ + if (!arc4random_mtx) { \ + HANDLE p = CreateMutex(NULL, FALSE, NULL); \ + if (InterlockedCompareExchangePointer((void **)&arc4random_mtx, (void *)p, NULL)) \ + CloseHandle(p); \ + } \ + WaitForSingleObject(arc4random_mtx, INFINITE); \ +} \ + +#define _ARC4_UNLOCK() ReleaseMutex(arc4random_mtx) + static inline int _rs_allocate(struct _rs **rsp, struct _rsx **rsxp) {