|
2 | 2 | * @name Insecure nonce/iv (static value or weak random source) |
3 | 3 | * @id java/quantum/insecure-iv-or-nonce |
4 | 4 | * @description A nonce/iv is generated from a source that is not secure. This can lead to |
5 | | - * vulnerabilities such as replay attacks or key recovery. |
| 5 | + * vulnerabilities such as replay attacks or key recovery. Insecure generation |
| 6 | + * is any static nonce, or any known insecure source for a nonce/iv if |
| 7 | + * the value is used for an encryption operation (decryption operations are ignored |
| 8 | + * as the nonce/iv would be provided alongside the ciphertext). |
6 | 9 | * @kind problem |
7 | 10 | * @problem.severity error |
8 | 11 | * @precision high |
|
12 | 15 |
|
13 | 16 | import experimental.quantum.Language |
14 | 17 |
|
15 | | -from Crypto::NonceArtifactNode nonce, Crypto::NodeBase src |
| 18 | +from Crypto::NonceArtifactNode nonce, Crypto::NodeBase src, Crypto::NodeBase op, string msg |
16 | 19 | where |
17 | 20 | nonce.getSourceNode() = src and |
18 | | - not src.asElement() instanceof SecureRandomnessInstance |
19 | | -select nonce, "Nonce or IV uses insecure or constant source $@", src, src.toString() |
| 21 | + ( |
| 22 | + // Case 1: Any constant nonce/iv is bad, regardless of how it is used |
| 23 | + src.asElement() instanceof Crypto::GenericConstantSourceInstance and |
| 24 | + op = nonce and // binding op by not using it |
| 25 | + msg = "Nonce or IV uses constant source $@" |
| 26 | + or |
| 27 | + // Case 2: The nonce has a non-random source and there is no known operation for the nonce |
| 28 | + // assume it is used for encryption |
| 29 | + not src.asElement() instanceof SecureRandomnessInstance and |
| 30 | + not src.asElement() instanceof Crypto::GenericConstantSourceInstance and |
| 31 | + not exists(Crypto::CipherOperationNode o | o.getANonce() = nonce) and |
| 32 | + op = nonce and // binding op, but not using it |
| 33 | + msg = |
| 34 | + "Nonce or IV uses insecure source $@ with no observed nonce usage (assuming could be for encryption)." |
| 35 | + or |
| 36 | + // Case 3: The nonce has a non-random source and is used in an encryption operation |
| 37 | + not src.asElement() instanceof SecureRandomnessInstance and |
| 38 | + not src.asElement() instanceof Crypto::GenericConstantSourceInstance and |
| 39 | + op.(Crypto::CipherOperationNode).getANonce() = nonce and |
| 40 | + ( |
| 41 | + op.(Crypto::CipherOperationNode).getKeyOperationSubtype() instanceof Crypto::TEncryptMode |
| 42 | + or |
| 43 | + op.(Crypto::CipherOperationNode).getKeyOperationSubtype() instanceof Crypto::TWrapMode |
| 44 | + ) and |
| 45 | + msg = "Nonce or IV uses insecure source $@ at encryption operation $@" |
| 46 | + ) |
| 47 | +select nonce, msg, src, src.toString(), op, op.toString() |
0 commit comments