Skip to content

deterministic_encryption

Reports encryption with package:encrypt that gives equal ciphertexts for equal texts: ECB mode, or a fixed vector. Use another mode and a new random vector for each message.

Tier
blocking
Scope
Dart
Lowest preset
recommended
Fix
none
Security class
CWE-327, CWE-1204 · mobile risk M10 (insufficient cryptography) · severity 7.0
  1. ECB mode: AESMode.ecb (or .ecb) given as the mode: of AES(…). The violation is at the mode.
  2. A fixed initialization vector given as the iv: of Encrypter.encrypt or encryptBytes: IV.allZerosOfLength(n) (zeros), IV(Uint8List(n)), IV.fromUtf8, IV.fromBase64 or IV.fromBase16 of a literal, IV(Uint8List.fromList([<literal numbers>])). It is reported where it is written as the argument. Where the argument reads a final local, or a final or constant field or top-level variable of the same file, it is reported once at that variable’s initializer, however many encryptions use it.
  3. One random vector for the whole run: an IV.fromLength(n) kept by a final top-level variable or a final static field of the same file, so that every message takes it. It is reported once at its initializer. Since encrypt 5.0.2 (fix .fromLength() to be cryptographically secure), IV.fromLength fills the vector from Random.secure(), as IV.fromSecureRandom does: each run has a new vector, but every message of a run takes the same one.

It does not report:

  • IV.fromSecureRandom(n);
  • IV.fromLength(n) written as the argument, or kept by a local or by an instance field: a new vector for each call of the function or each instance;
  • a vector read from data, such as a parameter or the first bytes of a ciphertext;
  • the vector of an encrypter that the file shows to be of ECB (reported at its mode; the vector is ignored there) or of Fernet, which makes its own;
  • decryption, whose encryption is elsewhere;
  • debug-only code, and the test side.

Keep a deterministic encryption chosen on purpose, such as a searchable index of encrypted values, with a justified suppression, or use a keyed hash for it.

ECB encrypts each block alone, so equal blocks of the text give equal blocks of the ciphertext, and its patterns show through (the well-known penguin).

A vector that never changes does the same to whole messages: equal texts give equal ciphertexts, which tells an observer when a value repeats. In CTR and GCM, the modes that need a vector most, a repeated vector with the same key repeats the key stream. Two ciphertexts then XOR to the two texts, and GCM’s authentication key can be recovered.

Violating
final encrypter = Encrypter(AES(key, mode: AESMode.gcm));
final sealed = encrypter.encrypt(text, iv: IV.allZerosOfLength(16));
// One vector for every message of the run.
final _iv = IV.fromLength(12);
Corrected(a new vector for each message, sent beside the ciphertext)
final iv = IV.fromSecureRandom(12);
final sealed = encrypter.encrypt(text, iv: iv);
final message = '${iv.base64}:${sealed.base64}';

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.

In SARIF, the rule’s descriptor carries the tags security, external/cwe/cwe-327, external/cwe/cwe-1204 and external/owasp-mobile-2024/m10, and the security-severity 7.0. No other report changes.

Write why on the line directly above the suppression:

// Reads the archive format of version 1, which fixed the vector; never writes it.
// ignore: assay/deterministic_encryption

// ignore_for_file: assay/deterministic_encryption silences it in a whole file, and // ignore: deterministic_encryption without the prefix works too. See Suppressions.