Calling a Random Number Generator random number generator (RNG) that is not seeded , will result in generating the same sequence of random numbers in different runs of the program.
Suppose there is a code that calls 10 times an RNG function 10 times to produce a sequence of 10 random numbers. Suppose, also, that this RNG is not seeded. Running the code for the first time will produce the sequence S = <r1, r2, r3, r4, r5, r6, r7, r8, r9, r10>. Running the code again for a second time will produce the exact same sequence S. Generally, any subsequent runs of the code will generate the same sequence S.
As a result, after the first run of the RNG, an attacker will know the sequence of random numbers that will be generated in the future runs. Knowing the sequence of random numbers that will be generated beforehand can lead to many vulnerabilities, especially when security protocols are concerned.
...
Rule MSC30-CPP. Do not use the rand() function for generating pseudorandom numbers addresses RNGs from a different perspective, i.e., the time till until the first collision occurs. In other words, during a single run of an RNG, the time interval after which , the RNG generates the same random numbers. The rule Rule MSC30-CPP deprecates the rand()
function as because it generates numbers which have a comparatively short cycle. The same rule proposes the use of the random()
function for POSIX and the CryptGenRandom()
function for Windows.
The current rule (MSC32-CPP) examines these three RNGs in terms of seeding. Noncompliant code examples correspond to the use of an RNG without a seed, while compliant solutions correspond to the same RNG being properly seeded. Rule MSC32-CPP addresses all three RNGs mentioned in rule MSC30-CPP for completeness. Rule MSC32-CPP complies to MSC30-CPP and does not recommend the use of the rand()
function. Nevertheless, if it is unavoidable to use rand()
, it should at least , it should be properly seeded.
Noncompliant Code Example
...
This noncompliant code example generates a sequence of 10 pseudorandom numbers using the random()
function. When random()
is not seeded, it behaves like rand()
, and thus produces the same sequence of random numbers at different calls.
...
In the previous examples, seeding in rand()
and random()
is done using the time()
function, which returns the current time calculated as the number of seconds that have past since 01/01/1970. Depending on the application and the desirable level of security, a programmer may choose alternative ways to seed RNGs. In general, hardware is more capable of generating real random numbers (for example, generate a sequence of bits by sampling the thermal noise of a diode and use this as a seed).
Compliant Solution (Windows)
Wiki Markup |
---|
The [{{CryptGenRandom()}}|http://msdn.microsoft.com/en-us/library/aa379942.aspx]does not run the risk of not being properly seeded. The reason for that is that its arguments serve as seeders. From the Microsoft Developer Network {{CryptGenRandom()}} reference \[[MSDN|https://www.securecoding.cert.org/confluence/display/seccode/AA.+C+References#AA.CReferences-MSDN]\]: |
...