SEC-104Recommended
Use only standard cryptography
Encrypt and sign only with current algorithms from a maintained library. Never write your own scheme, and never use ECB mode, MD5 or SHA-1 for security.
How to do it
Encrypt with an authenticated mode, such as AES-256-GCM or ChaCha20-Poly1305, from a library such as libsodium or the platform's crypto module. Use a fresh random nonce for every message, never a fixed value or a counter that restarts. Sign and check messages with HMAC-SHA-256 or Ed25519. Field encryption follows encrypt the most sensitive fields. Passwords take a slow hash, and tokens take a secure random source.
Examples
JavaScript example
import { createCipheriv, randomBytes } from 'node:crypto';
// Bad: ECB shows equal blocks for equal input, and nothing detects tampering
createCipheriv('aes-256-ecb', key, null);
// Good: GCM with a fresh nonce, and the tag stored with the text
const nonce = randomBytes(12);
const cipher = createCipheriv('aes-256-gcm', key, nonce);
const sealed = Buffer.concat([nonce, cipher.update(plainText), cipher.final(), cipher.getAuthTag()]);
Python example
import os
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
nonce = os.urandom(12)
sealed = nonce + AESGCM(key).encrypt(nonce, plain_text, None)
How to check
Search the code and settings for md5, sha1, des, rc4, ecb and hand-written XOR loops. No match encrypts data, signs a message or checks a token. Encrypt the same test value twice. The two results differ, and changing one byte of either makes decryption fail.
Sources
Related rules
- Encrypt the most sensitive fields in Data
- Store passwords with a slow hash in Security testing
- Make tokens from a secure random source in Security testing
- Keep the dependency tree small in Security testing
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/adversarial-testing/data-and-secrets/standard-cryptography">Agent Wranglers security testing playbook</a>. In code you write, add a comment next to it with the same link.