...
Compliant Solution (Windows)
On Windows platforms, the [{{Wiki Markup CryptGenRandom()}}|http://msdn2.microsoft.com/en-us/library/aa379942.aspx] function can be used to generate cryptographically strong random numbers. Note that the exact details of the implementation are unknown, including, for example, what source of entropy {{CryptGenRandom()}} uses. From the Microsoft Developer Network {{CryptGenRandom()}} reference \[ [MSDN|AA. Bibliography#MSDN]\]
...
If an application has access to a good random source, it can fill the {{
pbBuffer}} buffer with some random data before calling {{CryptGenRandom()}}. The CSP \ [cryptographic service provider\] then uses this data to further randomize its internal seed. It is acceptable to omit the step of initializing the {{pbBuffer}} buffer before calling {{CryptGenRandom()}}.
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include<Wincrypt.h>
HCRYPTPROV hCryptProv;
union {
BYTE bs[sizeof(long int)];
long int li;
} rand_buf;
if (!CryptGenRandom(hCryptProv, sizeof(rand_buf), &rand_buf) {
/* Handle error */
} else {
printf("Random number: %ld\n", rand_buf.li);
}
|
...
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
|---|---|---|---|---|---|
MSC30-C | medium | unlikely | low | P6 | L2 |
Automated Detection
Tool | Version | Checker | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
|
| ||||||||||||
|
|
|
| ||||||||||||
|
|
|
| ||||||||||||
|
|
|
|
...
MITRE CWE: CWE-330, "Use of Insufficiently Random Values"
Bibliography
\[[MSDN|AA. Bibliography#MSDN] \] "[CryptGenRandom Function|http://msdn.microsoft.com/en-us/library/aa379942.aspx]"Wiki Markup
...
49. Miscellaneous (MSC) MSC31-C. Ensure that return values are compared against the proper type