Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.2 KiB

  1. /* $OpenBSD: arc4random_win.h,v 1.6 2016/06/30 12:17:29 bcook Exp $ */
  2. /*
  3. * Copyright (c) 1996, David Mazieres <dm@uun.org>
  4. * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
  5. * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
  6. * Copyright (c) 2014, Theo de Raadt <deraadt@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. * Stub functions for portability.
  22. */
  23. #include <windows.h>
  24. static volatile HANDLE arc4random_mtx = NULL;
  25. /*
  26. * Initialize the mutex on the first lock attempt. On collision, each thread
  27. * will attempt to allocate a mutex and compare-and-swap it into place as the
  28. * global mutex. On failure to swap in the global mutex, the mutex is closed.
  29. */
  30. #define _ARC4_LOCK() { \
  31. if (!arc4random_mtx) { \
  32. HANDLE p = CreateMutex(NULL, FALSE, NULL); \
  33. if (InterlockedCompareExchangePointer((void **)&arc4random_mtx, (void *)p, NULL)) \
  34. CloseHandle(p); \
  35. } \
  36. WaitForSingleObject(arc4random_mtx, INFINITE); \
  37. } \
  38. #define _ARC4_UNLOCK() ReleaseMutex(arc4random_mtx)
  39. static inline void
  40. _getentropy_fail(void)
  41. {
  42. TerminateProcess(GetCurrentProcess(), 0);
  43. }
  44. static inline int
  45. _rs_allocate(struct _rs **rsp, struct _rsx **rsxp)
  46. {
  47. *rsp = VirtualAlloc(NULL, sizeof(**rsp),
  48. MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
  49. if (*rsp == NULL)
  50. return (-1);
  51. *rsxp = VirtualAlloc(NULL, sizeof(**rsxp),
  52. MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
  53. if (*rsxp == NULL) {
  54. VirtualFree(*rsp, 0, MEM_RELEASE);
  55. *rsp = NULL;
  56. return (-1);
  57. }
  58. return (0);
  59. }
  60. static inline void
  61. _rs_forkhandler(void)
  62. {
  63. }
  64. static inline void
  65. _rs_forkdetect(void)
  66. {
  67. }