...
On Windows platforms, the CryptGenRandom BcryptGenRandom()
function can be used to generate cryptographically strong random numbers. The exact details of the implementation are unknown, including, for example, what source of entropy CryptGenRandom()
uses. The Microsoft Developer Network CryptGenRandomBCryptGenRandom()
reference [MSDN] states
If an application has access to a good random source, it can fill the
pbBuffer
buffer with some random data before callingCryptGenRandom()
. The CSP [cryptographic service provider] then uses this data to further randomize its internal seed. It is acceptable to omit the step of initializing thepbBuffer
buffer before callingCryptGenRandom()
The default random number provider implements an algorithm for generating random numbers that complies with the NIST SP800-90 standard, specifically the CTR_DRBG portion of that standard.
Code Block
bgColor #ccccff lang c #include <Windows.h> #include
...
<bcrypt.h> #include <stdio.h>
...
#pragma comment(lib, "Bcrypt") int func(
...
) { BCRYPT_ALG_HANDLE Prov;
...
int
...
Buffer; if
...
(!BCRYPT_SUCCESS( BCryptOpenAlgorithmProvider(&Prov, BCRYPT_RNG_ALGORITHM,
...
NULL, 0))) {
...
/*
...
handle
...
error */ } if (!BCRYPT_SUCCESS(
...
BCryptGenRandom(
...
Prov,
...
(
...
PUCHAR)
...
(
...
&
...
Buffer),
...
...
...
...
...
...
...
...
...
...
...
...
...
...
sizeof(Buffer), 0))) {
...
/*
...
handle error */
...
}
...
printf("Random number: %d\n", Buffer);
...
BCryptCloseAlgorithmProvider(Prov, 0); }
Risk Assessment
The use of the rand()
function can result in predictable random numbers.
...