Weak cryptographic algorithms

Use of broken or risky cryptographic algorithms

Description

DES has a short key, RC4 produces a biased keystream, and AES-ECB exposes repeated blocks. Retire legacy ciphers such as 3DES, RC2 and Blowfish. MD5 and SHA-1 are unsuitable for checks or signatures that need collision resistance.

Potential impact

Weak encryption may expose plaintext patterns or sensitive data. Weak integrity checks or token authentication may accept an altered file or forged message.

Remediation

  • Use a vetted implementation of AES-GCM or ChaCha20-Poly1305, such as Web Crypto (webcrypto in node:crypto) for AES-GCM or libsodium. Use at least 128 bits for AES keys, preferably 256. Never reuse a 96-bit GCM IV under the same key. Limit per-key use to reduce the risk of random IV collisions.
  • Use SHA-256/384/512 or SHA-3 for hash checks. For message authentication, use HMAC-SHA-256 with a dedicated key. If AEAD is unavailable, use a separate HMAC key to authenticate the IV and ciphertext together. Verify the tag before decryption.
  • To derive a key from a password, use PBKDF2, scrypt or Argon2id with an appropriate iteration count or memory cost.

Examples

Before

javascript
const crypto = require("crypto");

// Before: weak encryption mode and hash.
function encryptEcb(plaintext, key) {
  // AES-ECB exposes patterns and provides no integrity protection.
  const cipher = crypto.createCipheriv("aes-128-ecb", key, null);
  return Buffer.concat([cipher.update(plaintext, "utf8"), cipher.final()]);
}

function md5Checksum(data) {
  // MD5 lacks the collision resistance needed for security integrity checks.
  return crypto.createHash("md5").update(data).digest("hex");
}

// Fixed demonstration key (also insecure).
const key = Buffer.alloc(16, 0);
console.log(encryptEcb("secret text", key).toString("hex"));
console.log(md5Checksum("file-contents"));

After

javascript
const crypto = require("crypto");

// AES-256-GCM for confidentiality and integrity.
function encryptAesGcm(plaintext, key) {
  const iv = crypto.randomBytes(12); // Recommended 96-bit IV.
  const cipher = crypto.createCipheriv("aes-256-gcm", key, iv);
  const ciphertext = Buffer.concat([
    cipher.update(plaintext, "utf8"),
    cipher.final(),
  ]);
  const tag = cipher.getAuthTag();
  return {
    iv: iv.toString("hex"),
    ct: ciphertext.toString("hex"),
    tag: tag.toString("hex"),
  };
}

function decryptAesGcm(enc, key) {
  const iv = Buffer.from(enc.iv, "hex");
  const ct = Buffer.from(enc.ct, "hex");
  const tag = Buffer.from(enc.tag, "hex");
  const decipher = crypto.createDecipheriv("aes-256-gcm", key, iv);
  decipher.setAuthTag(tag);
  const plaintext = Buffer.concat([decipher.update(ct), decipher.final()]);
  return plaintext.toString("utf8");
}

// Use a strong hash for integrity checks.
function sha256Checksum(data) {
  return crypto.createHash("sha256").update(data).digest("hex");
}

// Use HMAC-SHA-256 for message authentication where needed.
function hmacSha256(data, key) {
  return crypto.createHmac("sha256", key).update(data).digest("hex");
}

// Generate a strong key (32 bytes for AES-256).
const key = crypto.randomBytes(32);
const enc = encryptAesGcm("secret text", key);
console.log(enc);
console.log("sha256:", sha256Checksum("file-contents"));
console.log("hmac:", hmacSha256("message", crypto.randomBytes(32)));

The example replaces the fixed key with a random 32-byte key and returns the GCM IV, ciphertext and tag. Decryption returns plaintext only after final() verifies the tag. Compare the file’s SHA-256 digest with a trusted value. The separate HMAC key authenticates the message.

References