add explanations about modulo by power of 2

This commit is contained in:
Erik Krogh Kristensen
2020-06-10 10:38:47 +02:00
parent 111f6d406c
commit 16ec405724
2 changed files with 2 additions and 1 deletions

View File

@@ -99,6 +99,7 @@ DataFlow::Node badCrypto(string description) {
description = "modulo" and
goodRandom() = random and
random.flowsToExpr(mod.getLeftOperand()) and
// 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].
not mod.getRightOperand().getIntValue() = [2, 4, 8, 16, 32, 64, 128] and
// not exists a comparison that checks if the result is potentially biased.
not exists(BinaryExpr comparison | comparison.getOperator() = [">", "<", "<=", ">="] |

View File

@@ -7,7 +7,7 @@ const buffer = crypto.randomBytes(bytes);
const digits = [];
for (let i = 0; i < buffer.length; ++i) {
digits.push(Math.floor(buffer[i] / 25.6)); // NOT OK
digits.push(buffer[i] % 8); // OK - 8 is a power of 2, so the result is unbiased.
digits.push(buffer[i] % 8); // OK - input is a random byte, so the output is a uniformly random number between 0 and 7.
digits.push(buffer[i] % 100); // NOT OK
}