Commit bbd20e3e
bbd20e3e498dMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
fix(crypto): Windows CSPRNG via BCryptGenRandom, counter-mixed fallback
PR #514's CSPRNG worked on Linux (/dev/urandom) and macOS/BSD (arc4random_buf) but on Windows MinGW it fell to the time-mixed last-ditch path -- two calls within the same second returned identical bytes and the test's "two random outputs must differ" invariant failed.
Two changes:
-
Windows path: resolve BCryptGenRandom from bcrypt.dll via LoadLibraryA + GetProcAddress so no link-time -lbcrypt is required. Cached function-pointer + resolved-flag avoid reloading on each call. Real CSPRNG when bcrypt.dll is present (Vista+, which covers every supported MinGW target).
-
Fallback (both the Linux /dev/urandom-missing path and the Windows BCrypt-load-fails path): mix a static call counter into the time-based seed so successive calls produce distinct outputs. Not cryptographically secure either way, but the "differ across calls" invariant the test asserts now holds even on degraded platforms.
Local: 484 tests pass.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- bbd20e3e498d
- Parent
- 213cb83f635f
lib/sp_crypto.cmodifiedView file
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)# include <stdlib.h> /* arc4random_buf */#endif#if defined(_WIN32) || defined(_WIN64)# include <windows.h> /* LoadLibraryA / GetProcAddress for BCryptGenRandom */#endif/* ---------- SHA-256 ---------- * FIPS-180-4. Compact reference implementation -- public domain. uint8_t r[64];#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) arc4random_buf(r, nbytes);#elif defined(_WIN32) || defined(_WIN64) /* Windows: BCryptGenRandom via runtime LoadLibrary so no * link-time -lbcrypt is needed. Cached function pointer. * Fall through to the time-mixed path if loading fails * (rare; only on pre-Vista where bcrypt.dll isn't present). */ { typedef long (__stdcall *sp_BCryptGenRandom_fn)(void *, unsigned char *, unsigned long, unsigned long); static sp_BCryptGenRandom_fn sp_bcrypt_gen_random = NULL; static int sp_bcrypt_resolved = 0; if (!sp_bcrypt_resolved) { sp_bcrypt_resolved = 1; void *h = (void *)LoadLibraryA("bcrypt.dll"); if (h) sp_bcrypt_gen_random = (sp_BCryptGenRandom_fn)GetProcAddress((HMODULE)h, "BCryptGenRandom"); } int ok = 0; if (sp_bcrypt_gen_random) { if (sp_bcrypt_gen_random(NULL, r, (unsigned long)nbytes, 0x00000002UL /* BCRYPT_USE_SYSTEM_PREFERRED_RNG */) == 0) { ok = 1; } } if (!ok) { /* Counter-mixed time fallback so the test's "two calls * must differ" invariant holds even when BCrypt is * unavailable. NOT cryptographically secure. */ static uint64_t sp_crypto_fallback_ctr = 0; sp_crypto_fallback_ctr++; uint64_t mix = (uint64_t)time(NULL) ^ (sp_crypto_fallback_ctr * 0x9e3779b97f4a7c15ULL); for (int k = 0; k < nbytes; k++) { mix ^= mix << 13; mix ^= mix >> 7; mix ^= mix << 17; r[k] = (uint8_t)mix; } } }#else FILE *f = fopen("/dev/urandom", "rb"); if (f) { (void)got; /* short read is rare for urandom; fall through */ fclose(f); } else { /* Last-ditch: time-mixed -- NOT cryptographically secure. * Modern systems never reach this path. */ time_t t = time(NULL); for (int k = 0; k < nbytes; k++) r[k] = (uint8_t)(t >> (k * 7)); /* Last-ditch: counter-mixed time -- NOT cryptographically * secure. Modern systems never reach this path. The * counter guarantees distinct outputs across calls within * the same second. */ static uint64_t sp_crypto_fallback_ctr = 0; sp_crypto_fallback_ctr++; uint64_t mix = (uint64_t)time(NULL) ^ (sp_crypto_fallback_ctr * 0x9e3779b97f4a7c15ULL); for (int k = 0; k < nbytes; k++) { mix ^= mix << 13; mix ^= mix >> 7; mix ^= mix << 17; r[k] = (uint8_t)mix; } }#endif int i, j = 0;