Common Cryptographic Pitfalls

← Back to Cryptography I: Blockchain Security and Cryptographic Foundations

Introduction

Cryptography is unforgiving. A single mistake—using weak randomness, accepting non-canonical signatures, or mishandling keys—can compromise the entire system. This chapter catalogs the most common cryptographic pitfalls, explains why they're dangerous, and shows you how to avoid them.

Pitfall 1: Weak Random Number Generation

The Mistake

// ❌ WRONG - Predictable randomness
void generateWeakKey() {
    std::srand(std::time(nullptr));  // Seed with current time

    std::uint8_t secretKey[32];
    for (auto& byte : secretKey) {
        byte = std::rand() % 256;  // NOT cryptographically secure
    }

    return SecretKey{Slice{secretKey, 32}};
}

Why It's Dangerous

Predictability:

Real-world example:

The Fix

Detection

Pitfall 2: Memory Leakage

The Mistake

Why It's Dangerous

Attack vectors:

  1. Core dumps: Process crashes, core dump contains secrets

  2. Swap files: Memory paged to disk, secrets written to swap

  3. Hibernation: All memory written to hibernation file

  4. Memory inspection: Debugger or malware reads process memory

  5. Cold boot: RAM retains data briefly after power off

The Fix

Detection

Pitfall 3: Accepting Non-Canonical Signatures

The Mistake

Why It's Dangerous

Signature malleability allows attacks:

The Fix

Detection

Pitfall 4: Key Reuse Across Contexts

The Mistake

Why It's Dangerous

Cross-protocol attacks:

Key compromise amplification:

The Fix

Additional Protection: Hash Prefixes

Pitfall 5: Timing Attacks on Comparisons

The Mistake

Why It's Dangerous

Timing side-channel:

The Fix

Detection

Pitfall 6: Insufficient Key Length

The Mistake

Why It's Dangerous

The Fix

Guideline

Pitfall 7: Rolling Your Own Crypto

The Mistake

Why It's Dangerous

Expertise required:

  • Cryptography is subtle and unforgiving

  • Experts spend years studying attack techniques

  • Small mistakes lead to complete breaks

  • Standard algorithms battle-tested by thousands of experts

Common mistakes in custom crypto:

  1. Weak key scheduling

  2. Poor mode of operation

  3. No authentication

  4. Padding oracle vulnerabilities

  5. Timing side-channels

  6. Implementation bugs

The Fix

Rule

NEVER implement your own:

  • Encryption algorithms

  • Hash functions

  • Signature schemes

  • Random number generators

  • Key derivation functions

ALWAYS use:

  • OpenSSL

  • libsodium

  • Other well-vetted libraries

  • Standard algorithms (AES, SHA, etc.)

Pitfall 8: Ignoring Error Returns

The Mistake

Why It's Dangerous

The Fix

Guideline

Pitfall 9: Hardcoded Secrets

The Mistake

Why It's Dangerous

Exposure:

  • Source code in version control (git history)

  • Binary contains strings (recoverable)

  • Code reviews expose secrets

  • Logs may print secrets

  • Hard to rotate if compromised

The Fix

Best Practices

Pitfall 10: Insufficient Validation

The Mistake

Why It's Dangerous

Consequences:

  • Using invalid keys → signature verification fails

  • Using malformed data → undefined behavior

  • Skipping validation → security bypasses

  • Accepting bad input → system corruption

The Fix

Validation Checklist

Summary

Common cryptographic pitfalls and how to avoid them:

  1. Weak RNG: Use crypto_prng(), never std::rand()

  2. Memory leaks: Use RAII, call secure_erase()

  3. Non-canonical sigs: Always enforce canonicality

  4. Key reuse: Separate keys for separate purposes

  5. Timing attacks: Use constant-time comparisons

  6. Short keys: Use 256 bits minimum

  7. Custom crypto: Never - use standard libraries

  8. Ignored errors: Always check return values

  9. Hardcoded secrets: Load from secure storage

  10. Missing validation: Validate all inputs

Golden rules:

  • Trust no input

  • Check all returns

  • Erase all secrets

  • Use standard crypto

  • Fail loudly on errors

Last updated