+#define RtlGenRandom SystemFunction036
+BOOLEAN WINAPI RtlGenRandom(PVOID RandomBuffer, ULONG RandomBufferLength);
+
+/*
+ * Generate @n cryptographically secure random bytes (thread-safe)
+ *
+ * This is the Windows version. It uses RtlGenRandom() (actually called
+ * SystemFunction036) from advapi32.dll.
+ */
+void
+get_random_bytes(void *p, size_t n)
+{
+ while (n != 0) {
+ u32 count = min(n, UINT32_MAX);
+
+ if (!RtlGenRandom(p, count)) {
+ win32_error(GetLastError(),
+ L"RtlGenRandom() failed (count=%u)", count);
+ wimlib_assert(0);
+ count = 0;
+ }
+ p += count;
+ n -= count;
+ }
+}
+