Generating Keys, Nonces & IVs Safely
Use secrets, os.urandom, and safe wrappers in Python and Node.
Generating Keys, Nonces & IVs Safely is a free Cryptology Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Cryptology Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Three Categories
Three distinct cryptographic values need secure generation: (1) Keys — long-lived, must be unpredictable and secret. (2) IVs — per-message, may be public but must be unpredictable (CBC) or unique (GCM). (3) Nonces — must be unique per use, may or may not be random.
Symmetric Keys
AES-128: secrets.token_bytes(16). AES-256: secrets.token_bytes(32). ChaCha20: secrets.token_bytes(32). Never derive keys from passwords without KDF (PBKDF2/Argon2). Never reuse keys across different algorithms or purposes.
Asymmetric Key Generation
from cryptography.hazmat.primitives.asymmetric import ec, rsa rsa_key = rsa.generate_private_key(public_exponent=65537, key_size=2048) ec_key = ec.generate_private_key(ec.SECP256R1()) # The library handles entropy sourcing internally
IV for AES-CBC
AES-CBC requires a uniformly random 16-byte IV per message. Never reuse an IV with the same key. Never use a counter as an IV in CBC mode. Generate: iv = os.urandom(16). Prepend the IV to the ciphertext — it need not be secret, only unpredictable.
Nonce for AES-GCM
AES-GCM requires a unique 96-bit nonce per (key, message) pair. Nonce reuse catastrophically breaks GCM: attacker recovers the authentication key and can forge messages. Use either a random 12-byte nonce (birthday bound at 2^32 messages) or a counter.
Counter-Based Nonces
For high-volume encryption with a single key: maintain a persistent counter per key. nonce = counter.to_bytes(12, "big"). Increment counter after each encryption. Store counter to persistent storage — reset on restart causes reuse.
Nonce Misuse-Resistant AEADs
AES-GCM-SIV and Deoxys-II are nonce misuse-resistant: nonce reuse degrades security to that of deterministic encryption (no authentication key leak). Recommended when nonce uniqueness is difficult to guarantee (distributed systems, crash-restart scenarios).
HKDF for Key Derivation
Do not use raw key material directly for multiple purposes. Derive purpose-specific keys: from cryptography.hazmat.primitives.kdf.hkdf import HKDF. hkdf = HKDF(SHA256(), 32, salt, info=b"enc_key"). key = hkdf.derive(master_key). Use different info for each purpose.
Session Keys
Derive session-specific keys from a shared secret (e.g., ECDH output) using HKDF. TLS 1.3 derives separate client/server keys for handshake and application data. Session keys isolate compromise: learning one session key reveals nothing about others.
Key Wrapping
To store a key securely, wrap it with a Key Encryption Key (KEK) using AES-KW (RFC 3394) or AES-GCM. The wrapped key can be stored in a database or transmitted. The KEK is stored in an HSM or hardware-backed keystore (Android Keystore, Apple Secure Enclave).
Audit Checklist
Before deployment check: (1) all keys from os.urandom/secrets. (2) No hardcoded keys/IVs. (3) IVs never reused with same key. (4) GCM nonces unique per message. (5) Key rotation schedule defined. (6) Key derivation uses HKDF with distinct info strings.
Quick Check
What is the consequence of reusing a nonce in AES-GCM?
Recap
Keys need true random generation; CBC IVs need randomness; GCM nonces need uniqueness. Use os.urandom/secrets, derive with HKDF, and wrap with KEKs. Nonce misuse-resistant AEADs protect against accidental reuse. Next: Zero-Knowledge Proofs.
Frequently asked questions
Is the “Generating Keys, Nonces & IVs Safely” lesson free?
Yes — the full text of “Generating Keys, Nonces & IVs Safely” is free to read here on the web, and the Cryptology Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Cryptology Academy course, upgrade to CoddyKit PRO.
What will I learn in “Generating Keys, Nonces & IVs Safely”?
Use secrets, os.urandom, and safe wrappers in Python and Node. You practise Cryptology Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Cryptology Academy?
No prior experience is required. Cryptology Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Generating Keys, Nonces & IVs Safely” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Cryptology Academy lesson?
Yes. Every Cryptology Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- True Randomness vs Pseudorandomness
- Cryptographically Secure PRNGs
- Entropy Starvation & Weak-Key Bugs
- Generating Keys, Nonces & IVs Safely