CryptGenRandom is a cryptographically secure pseudorandom number generator function that is included in Microsoft CryptoAPI. In Win32 programs, Microsoft recommended its use anywhere random number generation is needed (it has since been replaced by BCryptGenRandom, see below). The kernel32 equivalent is RtlGenRandom. A 2007 paper from Hebrew University suggested security problems in the Windows 2000 implementation of CryptGenRandom (assuming the attacker has control of the machine). Microsoft later acknowledged that the same problems exist in Windows XP, but not in Vista. Microsoft released a fix for the bug with Windows XP Service Pack 3 in mid-2008. CryptGenRandom is deprecated as it belongs to the deprecated Windows CryptoAPI. The modern Cryptography API: Next Generation (CNG) replacement is BCryptGenRandom (and the underlying functions ProcessPrng and SystemPrng).
Background The Win32 API includes comprehensive support for cryptography through the Microsoft CryptoAPI, a set of cryptographic primitives provided by Microsoft for use in Windows applications. Windows technologies such as TLS support (via the Schannel API) and code signing rely on these primitives, which in turn rely on a cryptographically secure pseudorandom number generator (CSPRNG). CryptGenRandom is the standard CSPRNG supplied with the Microsoft CryptoAPI.
Method of operation
Before Windows Vista Microsoft-provided cryptography providers share the same implementation of CryptGenRandom, currently based on an internal function called RtlGenRandom. Only a general outline of the algorithm had been published as of 2007:
[RtlGenRandom] generates as specified in FIPS 186-2 appendix 3.1 with SHA-1 as the G function. And with entropy from:
The current process ID (GetCurrentProcessID). The current thread ID (GetCurrentThreadID). The tick count since boot time (GetTickCount). The current time (GetLocalTime). Various high-precision performance counters (QueryPerformanceCounter). An MD4 hash of the user's environment block, which includes username, computer name, and search path. [...] High-precision internal CPU counters, such as RDTSC, RDMSR, RDPMC [omitted: long lists of low-level system information fields and performance counters]
Windows Vista and above Microsoft has documented the implementation of the Windows 10 random number generator in some detail, in a whitepaper published in 2019. In Windows 10:
There exists a hierarchy of random number generators. The kernel has a "Root" PRNG, from which all randomness is ultimately derived. The kernel then uses the Root PRNG to seed one PRNG per logical processor (so the PRNG state is thread-local and requires no locking). When a process launches, it requests random bytes from the kernel per-processor PRNG to seed its own Process PRNG. It then uses the Process PRNG to also seed one buffered PRNG per logical processor. All userspace calls to fetch randomness, be it CryptGenRandom or RtlGenRandom, ultimately fall to ProcessPrng, which returns bytes from the process's per-processor PRNG. The PRNG always uses the AES-CTR-DRBG algorithm as specified by FIPS SP800-90. Although BCryptGenRandom accepts requests for older algorithms for backward compatibility, it only ever return random numbers from the per-processor PRNG. AES-CTR-DRBG, instead of FIPS 186, has been the default since Windows Vista and Windows Server 2008. The removal of other algorithms happened in Windows 10. The root RNG is periodically reseeded from the entropy pools. At bootup when very little entropy is available, a special "initial seeding" procedure provides the seed from seed files, external entropy, TPM randomness, RDRAND/RDSEED instructions, ACPI-OEM0 table, UEFI entropy, and the current time. The kernel maintains multiple entropy pools. Multiple entropy sources append into pools, the main one being interrupt timing. When a pool is used, the SHA-512 hash of its contents is taken as the output. Windows does not estimate entropy.
Security The security of a cryptosystem's CSPRNG is crucial because it is the origin for dynamic key material. Keys needed "on the fly", such as the TLS session keys that protect HTTPS connections, originate from CSPRNGs. If these pseudorandom numbers are predictable, session keys are predictable as well. Because CryptGenRandom is the de facto standard CSPRNG in Win32 environments, its security is critical for Windows users.
Cryptanalysis A cryptanalysis of CryptGenRandom, published in November 2007 by Leo Dorrendorf and others from the Hebrew University of Jerusalem and University of Haifa, found significant weaknesses in the Windows 2000 implementation of the algorithm. To take advantage of the vulnerability, an attacker would first need to compromise the program running the random number generator. The weaknesses in the paper all depend on an attacker siphoning the state bits out of the generator. An attacker in a position to carry out this attack would typically already be in a position to defeat any random number generator (for instance, they can simply sniff the outputs of the generator, or fix them in memory to known values). However, the Hebrew University team notes that an attacker only need steal the state bits once in order to persistently violate the security of a CryptGenRandom instance. They can also use the information they glean to determine past random numbers that were generated, potentially compromising information, such as credit card numbers, already sent. The paper's attacks are based on the fact that CryptGenRandom uses the stream cipher RC4, which can be run backwards once its state is known. They also take advantage of the fact that CryptGenRandom runs in user mode, allowing anyone who gains access to the operating system at user level, for example by exploiting a buffer overflow, to get CryptGenRandom's state information for that process. Finally, CryptGenRandom refreshes its seed from entropy infrequently. This problem is aggravated by the fact that each Win32 process has its own instance of CryptGenRandom state; while this means that a compromise of one process does not transitively compromise every other process, it may also increase the longevity of any successful break. Because the details of the CryptGenRandom algorithm were not public at the time, Dorrendorf's team used reverse engineering tools to discern how the algorithm works. Their paper is the first published record of how the Windows cryptographic random number generator operates.
… excerpt ends here. Continue reading the full article.