|
4 | 4 | <qhelp> |
5 | 5 | <overview> |
6 | 6 | <p> |
7 | | - Placeholder |
| 7 | + Generating secure random numbers can be an important part of creating a |
| 8 | + secure software system. This can be done using APIs that create |
| 9 | + cryptographically secure random numbers. |
| 10 | + </p> |
| 11 | + <p> |
| 12 | + However, using some mathematical operations on these cryptographically |
| 13 | + secure random numbers can create biased results, where some outcomes |
| 14 | + are more likely than others. |
| 15 | + Such biased results can make it easier for an attacker to guess the random |
| 16 | + numbers, and thereby break the security of the software system. |
8 | 17 | </p> |
9 | | - |
10 | 18 | </overview> |
11 | 19 | <recommendation> |
12 | | - |
13 | 20 | <p> |
14 | | - Placeholder. |
| 21 | + Be very careful not to introduce bias when performing mathematical operations |
| 22 | + on cryptographically secure random numbers. |
| 23 | + </p> |
| 24 | + <p> |
| 25 | + If possible, avoid performing mathematical operations on cryptographically secure |
| 26 | + random numbers at all, and use a preexisting library instead. |
15 | 27 | </p> |
16 | | - |
17 | 28 | </recommendation> |
18 | 29 | <example> |
19 | | - |
20 | 30 | <p> |
21 | | - Placeholder |
| 31 | + The example below uses the modulo operator to create an array of 10 random digits |
| 32 | + using random bytes as the source for randomness. |
22 | 33 | </p> |
| 34 | + <sample src="examples/bad-random.js" /> |
| 35 | + <p> |
| 36 | + The random byte is a uniformly random value between 0 and 255, and thus the result |
| 37 | + from using the modulo operator is slightly more likely to be between 0 and 5 than |
| 38 | + between 6 and 9. |
| 39 | + </p> |
| 40 | + <p> |
| 41 | + The issue has been fixed in the code below by using a library that correctly generates |
| 42 | + cryptographically secure random values. |
| 43 | + </p> |
| 44 | + <sample src="examples/bad-random-fixed.js" /> |
| 45 | + <p> |
| 46 | + Alternatively, the issue can be fixed by fixing the math in the original code. |
| 47 | + In the code below the random byte is discarded if the value is greater than or equal to 250. |
| 48 | + Thus the modulo operator is used on a uniformly random number between 0 and 249, which |
| 49 | + results in a uniformly random digit between 0 and 9. |
| 50 | + </p> |
| 51 | + <sample src="examples/bad-random-fixed2.js" /> |
23 | 52 |
|
24 | 53 | </example> |
25 | 54 |
|
| 55 | + |
26 | 56 | <references> |
27 | | - <li>NIST, FIPS 140 Annex a: <a href="http://csrc.nist.gov/publications/fips/fips140-2/fips1402annexa.pdf"> Approved Security Functions</a>.</li> |
28 | | - <li>NIST, SP 800-131A: <a href="http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar1.pdf"> Transitions: Recommendation for Transitioning the Use of Cryptographic Algorithms and Key Lengths</a>.</li> |
| 57 | + <li>Stack Overflow: <a href="https://stackoverflow.com/questions/3956478/understanding-randomness">Understanding “randomness”</a>.</li> |
| 58 | + <li>OWASP: <a href="https://owasp.org/www-community/vulnerabilities/Insecure_Randomness">Insecure Randomness</a>.</li> |
29 | 59 | <li>OWASP: <a |
30 | 60 | href="https://cheatsheetseries.owasp.org/cheatsheets/Cryptographic_Storage_Cheat_Sheet.html#rule---use-strong-approved-authenticated-encryption">Rule |
31 | 61 | - Use strong approved cryptographic algorithms</a>. |
32 | 62 | </li> |
33 | | - <li>Stack Overflow: <a href="https://stackoverflow.com/questions/3956478/understanding-randomness">Understanding “randomness”</a>.</li> |
34 | 63 | </references> |
35 | 64 |
|
36 | 65 | </qhelp> |
0 commit comments