Common Cryptographic Pitfalls
Introduction
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
The Fix
Detection
Pitfall 2: Memory Leakage
The Mistake
Why It's Dangerous
The Fix
Detection
Pitfall 3: Accepting Non-Canonical Signatures
The Mistake
Why It's Dangerous
The Fix
Detection
Pitfall 4: Key Reuse Across Contexts
The Mistake
Why It's Dangerous
The Fix
Additional Protection: Hash Prefixes
Pitfall 5: Timing Attacks on Comparisons
The Mistake
Why It's Dangerous
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
The Fix
Rule
Pitfall 8: Ignoring Error Returns
The Mistake
Why It's Dangerous
The Fix
Guideline
Pitfall 9: Hardcoded Secrets
The Mistake
Why It's Dangerous
The Fix
Best Practices
Pitfall 10: Insufficient Validation
The Mistake
Why It's Dangerous
The Fix
Validation Checklist
Summary
Last updated

