Insecure Key Derivation

Detects passwords used directly as encryption keys or hashed with fast algorithms instead of proper KDFs (PBKDF2, bcrypt, Argon2, scrypt).

zakirkun 765ad6c 2 files · 3.9 KB Updated

File contents

Insecure Key Derivation

Overview

Passwords must not be used directly as cryptographic keys or passed through fast hash functions. Proper Key Derivation Functions (KDFs) intentionally add computational cost and mandatory salting:

  • PBKDF2: NIST-approved, configurable iteration count (minimum 600,000 for SHA-256)
  • bcrypt: Adaptive work factor, built-in salt
  • scrypt: Memory-hard, resistant to GPU cracking
  • Argon2id: Winner of Password Hashing Competition, recommended

Remediation

Always derive keys from passwords using a KDF with a random salt.

Vulnerable:

key = hashlib.sha256(password.encode()).digest()
cipher = AES.new(key, AES.MODE_GCM)

Safe:

import os
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import hashes
salt = os.urandom(16)
kdf = PBKDF2HMAC(algorithm=hashes.SHA256(), length=32, salt=salt, iterations=600000)
key = kdf.derive(password.encode())

zakirkun/ice-tea/tree/main/skills/crypto/key-derivation commit 765ad6c6b7

Frequently asked questions

npx skillmds@latest add zakirkun/insecure-key-derivation