An authenticated cipher encrypts with a nonce fixed in the code, which repeats for every message
What it finds
AesGcm, AesCcm and ChaCha20Poly1305 need a nonce that is never used twice with one key. Two messages encrypted under the same key and nonce give away the exclusive or of their plaintexts, and with GCM also the key that authenticates every message, so that anyone can forge them. A nonce written in the code repeats on every call.
Reported is the first argument of an Encrypt call with four or more arguments, the shape of these ciphers' method, in a file that names one of them, when it is fixed: an array of literal bytes, a zeroed new byte[n], the bytes of a string literal, or a variable or field initialized with literal bytes and used for nothing else. Tests, which check known results, are left alone.
From the rule's source, constant_aead_nonce.rs.
using System.Security.Cryptography; class Sealer{ private static readonly byte[] Fixed = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; void Seal(AesGcm aes, byte[] plain, byte[] cipher, byte[] tag) { aes.Encrypt(new byte[12], plain, cipher, tag);tx0 reports: the nonce of this encryption is fixed in the code, so it repeats aes.Encrypt(Fixed, plain, cipher, tag);tx0 reports: the nonce of this encryption is fixed in the code, so it repeats var nonce = new byte[12]; RandomNumberGenerator.Fill(nonce); aes.Encrypt(nonce, plain, cipher, tag); }} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/csharp:constant-aead-nonce" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/csharp:constant-aead-nonce] reason="..."tx0 explain tx0/csharp:constant-aead-nonce shows the same in the terminal. Found a false positive? Report it.