Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 16ec405

Browse files
committed
add explanations about modulo by power of 2
1 parent 111f6d4 commit 16ec405

2 files changed

Lines changed: 2 additions & 1 deletion

File tree

javascript/ql/src/Security/CWE-327/BadRandomness.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ DataFlow::Node badCrypto(string description) {
9999
description = "modulo" and
100100
goodRandom() = random and
101101
random.flowsToExpr(mod.getLeftOperand()) and
102+
// division by a power of 2 is OK. E.g. if `x` is uniformly random is in the range [0..255] then `x % 32` is uniformly random in the range [0..31].
102103
not mod.getRightOperand().getIntValue() = [2, 4, 8, 16, 32, 64, 128] and
103104
// not exists a comparison that checks if the result is potentially biased.
104105
not exists(BinaryExpr comparison | comparison.getOperator() = [">", "<", "<=", ">="] |

javascript/ql/test/query-tests/Security/CWE-327/bad-random.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const buffer = crypto.randomBytes(bytes);
77
const digits = [];
88
for (let i = 0; i < buffer.length; ++i) {
99
digits.push(Math.floor(buffer[i] / 25.6)); // NOT OK
10-
digits.push(buffer[i] % 8); // OK - 8 is a power of 2, so the result is unbiased.
10+
digits.push(buffer[i] % 8); // OK - input is a random byte, so the output is a uniformly random number between 0 and 7.
1111
digits.push(buffer[i] % 100); // NOT OK
1212
}
1313

0 commit comments

Comments
 (0)