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:

  1. 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).

  2. 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

@@ -33,6 +33,9 @@
3333 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
3434 # include <stdlib.h> /* arc4random_buf */
3535 #endif
36+#if defined(_WIN32) || defined(_WIN64)
37+# include <windows.h> /* LoadLibraryA / GetProcAddress for BCryptGenRandom */
38+#endif
3639
3740 /* ---------- SHA-256 ----------
3841 * FIPS-180-4. Compact reference implementation -- public domain.
@@ -382,6 +385,39 @@
382385 uint8_t r[64];
383386 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
384387 arc4random_buf(r, nbytes);
388+#elif defined(_WIN32) || defined(_WIN64)
389+ /* Windows: BCryptGenRandom via runtime LoadLibrary so no
390+ * link-time -lbcrypt is needed. Cached function pointer.
391+ * Fall through to the time-mixed path if loading fails
392+ * (rare; only on pre-Vista where bcrypt.dll isn't present). */
393+ {
394+ typedef long (__stdcall *sp_BCryptGenRandom_fn)(void *, unsigned char *, unsigned long, unsigned long);
395+ static sp_BCryptGenRandom_fn sp_bcrypt_gen_random = NULL;
396+ static int sp_bcrypt_resolved = 0;
397+ if (!sp_bcrypt_resolved) {
398+ sp_bcrypt_resolved = 1;
399+ void *h = (void *)LoadLibraryA("bcrypt.dll");
400+ if (h) sp_bcrypt_gen_random = (sp_BCryptGenRandom_fn)GetProcAddress((HMODULE)h, "BCryptGenRandom");
401+ }
402+ int ok = 0;
403+ if (sp_bcrypt_gen_random) {
404+ if (sp_bcrypt_gen_random(NULL, r, (unsigned long)nbytes, 0x00000002UL /* BCRYPT_USE_SYSTEM_PREFERRED_RNG */) == 0) {
405+ ok = 1;
406+ }
407+ }
408+ if (!ok) {
409+ /* Counter-mixed time fallback so the test's "two calls
410+ * must differ" invariant holds even when BCrypt is
411+ * unavailable. NOT cryptographically secure. */
412+ static uint64_t sp_crypto_fallback_ctr = 0;
413+ sp_crypto_fallback_ctr++;
414+ uint64_t mix = (uint64_t)time(NULL) ^ (sp_crypto_fallback_ctr * 0x9e3779b97f4a7c15ULL);
415+ for (int k = 0; k < nbytes; k++) {
416+ mix ^= mix << 13; mix ^= mix >> 7; mix ^= mix << 17;
417+ r[k] = (uint8_t)mix;
418+ }
419+ }
420+ }
385421 #else
386422 FILE *f = fopen("/dev/urandom", "rb");
387423 if (f) {
@@ -389,10 +425,17 @@
389425 (void)got; /* short read is rare for urandom; fall through */
390426 fclose(f);
391427 } else {
392- /* Last-ditch: time-mixed -- NOT cryptographically secure.
393- * Modern systems never reach this path. */
394- time_t t = time(NULL);
395- for (int k = 0; k < nbytes; k++) r[k] = (uint8_t)(t >> (k * 7));
428+ /* Last-ditch: counter-mixed time -- NOT cryptographically
429+ * secure. Modern systems never reach this path. The
430+ * counter guarantees distinct outputs across calls within
431+ * the same second. */
432+ static uint64_t sp_crypto_fallback_ctr = 0;
433+ sp_crypto_fallback_ctr++;
434+ uint64_t mix = (uint64_t)time(NULL) ^ (sp_crypto_fallback_ctr * 0x9e3779b97f4a7c15ULL);
435+ for (int k = 0; k < nbytes; k++) {
436+ mix ^= mix << 13; mix ^= mix >> 7; mix ^= mix << 17;
437+ r[k] = (uint8_t)mix;
438+ }
396439 }
397440 #endif
398441 int i, j = 0;