...
Calling a PRNG in the same initial state, either without seeding it explicitly or by seeding it with the same value, results in generating the same sequence of random numbers in different runs of the program.
Suppose a PRNG function is called 10 times consecutively to produce a sequence of 10 random numbers. Suppose also that this PRNG is not seeded. Running the code for the first time produces the sequence S = <r1, r2, r3, r4, r5, r6, r7, r8, r9, r10>
. Running the code a second time produces exactly the same S
sequence. Generally, any subsequent runs of the code will generate the same S
sequence.
...
It is worth noting that not all random number generators can be seeded. True random number generators (RNGs) that rely on hardware to produce completely unpredictable results cannot be seeded. Some high-quality pseudorandom generators PRNGs, such as the /dev/random
device on some UNIX systems, also cannot be seeded. This rule applies to algorithmic pseudorandom generators that make seeding possible.
MSC30-C. Do not use the rand() function for generating pseudorandom numbers addresses PRNGs from a different perspective, which is the cycle of the pseudorandom number sequence—that is, during a single run of a PRNG, the time interval after which the PRNG generates the same random numbers. MSC30-C deprecates the rand()
function because it generates numbers that have a comparatively short cycle. The same rule proposes the use of the random()
function for POSIX and the CryptGenRandom()
function for Windows.
...
The
CryptGenRandom()
function fills a buffer with cryptographically random bytes.Syntax
Code Block BOOL WINAPI CryptGenRandom( __in HCRYPTPROV hProv, __in DWORD dwLen, __inout BYTE *pbBuffer );Parameters
hProv [in]
Handle of a cryptographic service provider (CSP) created by a call toCryptAcquireContexttoCryptAcquireContext
.
dwLen [in]
Number of bytes of random data to be generated.
pbBuffer [in, out]
Buffer to receive the returned data. This buffer must be at leastdwLenbytes leastdwLen
bytes in length.
Optionally, the application can fill this buffer with data to use as an auxiliary random seed.
...
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
...
...
...
Use of a broken or risky cryptographic algorithm |
...
...
...
Use of insufficiently random values |
...
...
Bibliography
...