Cryptography
Foundations
Section titled “Foundations”Cryptography is the mathematical science of securing communication and data. It is not a security Solution by itself — it is a tool that, when correctly applied within a secure system, provides Confidentiality, integrity, authentication, and non-repudiation.
Cryptographic Primitives
Section titled “Cryptographic Primitives”| Primitive | Purpose | Examples |
|---|---|---|
| Symmetric encryption | Confidentiality (high-speed) | AES, ChaCha20 |
| Asymmetric encryption | Confidentiality (key exchange) | RSA, ECIES |
| Hash functions | Integrity, password storage | SHA-256, SHA-3, bcrypt |
| Message authentication | Integrity + authenticity | HMAC, Poly1305 |
| Digital signatures | Non-repudiation, authenticity | RSA-PSS, ECDSA, EdDSA |
| Key exchange | Secure shared secret establishment | Diffie-Hellman, ECDH |
| Random number generation | Key material, nonces, salts | /dev/urandomCSPRNG |
Kerckhoffs”s Principle
Section titled “Kerckhoffs”s Principle”A cryptosystem should be secure even if everything about the system, except the key, is public Knowledge. This means:
- The algorithm is published and peer-reviewed
- Security depends only on key secrecy
- The algorithm works even if the attacker has full knowledge of its implementation
This is why rolling your own crypto is almost always wrong. AES has been studied for decades by Thousands of cryptanalysts. Your custom cipher has been studied by nobody.
Symmetric Encryption
Section titled “Symmetric Encryption”Symmetric encryption uses the same key for encryption and decryption. It is fast (orders of Magnitude faster than asymmetric encryption) and is the standard for bulk data encryption.
AES (Advanced Encryption Standard)
Section titled “AES (Advanced Encryption Standard)”AES is a block cipher selected by NIST in 2001 (FIPS 197) as the successor to DES. It operates on 128-bit blocks with key sizes of 128, 192, or 256 bits.
| Parameter | AES-128 | AES-192 | AES-256 |
|---|---|---|---|
| Key size | 128 bits | 192 bits | 256 bits |
| Rounds | 10 | 12 | 14 |
| Security | 128-bit | 192-bit | 256-bit |
| Performance | Fastest | Moderate | Slightly slower |
AES is a substitution-permutation network (SPN). Each round applies:
- SubBytes: Non-linear byte substitution using an S-box
- ShiftRows: Cyclic permutation of bytes within each row
- MixColumns: Linear transformation mixing each column
- AddRoundKey: XOR with the round key derived from the key schedule
Modes of Operation
Section titled “Modes of Operation”A block cipher operating on 128-bit blocks needs a mode of operation to handle messages longer than One block. The mode determines how blocks are chained together and how ciphertext is produced.
Electronic Codebook (ECB)
Section titled “Electronic Codebook (ECB)”Each block is encrypted independently with the same key.
Block 1 + Key → Ciphertext Block 1Block 2 + Key → Ciphertext Block 2Block 3 + Key → Ciphertext Block 3Do not use ECB. Identical plaintext blocks produce identical ciphertext blocks, revealing Patterns in the data. The classic demonstration is encrypting an image — ECB preserves visual Structure completely.
Cipher Block Chaining (CBC)
Section titled “Cipher Block Chaining (CBC)”Each plaintext block is XORed with the previous ciphertext block before encryption. An Initialization vector (IV) is used for the first block.
IV + Block 1 → XOR → Encrypt → Ciphertext Block 1Ciphertext Block 1 + Block 2 → XOR → Encrypt → Ciphertext Block 2CBC requires:
- IV must be unpredictable (random, not a counter). Reusing an IV with the same key is catastrophic.
- Padding ( PKCS#7) to align plaintext to block boundaries.
- Decryption is parallelizable; encryption is sequential.
Vulnerability: CBC is vulnerable to padding oracle attacks if the system leaks information about Whether padding is valid. The BEAST attack (2011) exploited CBC in TLS 1.0 where the IV was the last Ciphertext block of the previous record.
Counter Mode (CTR)
Section titled “Counter Mode (CTR)”CTR turns a block cipher into a stream cipher. A counter value is encrypted to produce a keystream, Which is XORed with the plaintext.
Counter 0 + Key → Encrypt → Keystream Block 0 → XOR → Plaintext Block 0 → Ciphertext Block 0Counter 1 + Key → Encrypt → Keystream Block 1 → XOR → Plaintext Block 1 → Ciphertext Block 1CTR properties:
- No padding required: It is a stream cipher mode, so plaintext can be any length.
- Fully parallelizable: Both encryption and decryption can be parallelized.
- Random access: Any block can be decrypted independently.
- Nonce requirements: The counter value must never repeat for the same key. A nonce (96 bits) combined with a block counter (32 bits) is standard (NIST SP 800-38A).
Vulnerability: CTR provides confidentiality only. It provides no integrity. If an attacker flips A bit in the ciphertext, the corresponding plaintext bit is flipped and the modification is Undetectable. Always combine with a MAC.
Galois/Counter Mode (GCM)
Section titled “Galois/Counter Mode (GCM)”GCM combines CTR mode encryption with Galois field authentication, providing both confidentiality And integrity (AEAD — Authenticated Encryption with Associated Data).
Plaintext → CTR Encryption → CiphertextCiphertext + Associated Data → GHASH → Authentication TagOutput: Ciphertext + TagGCM properties:
- AEAD: Confidentiality + integrity in a single operation.
- Associated data: Can authenticate metadata (headers, nonces) without encrypting it.
- Performance: Hardware-accelerated AES-GCM is extremely fast (AES-NI instruction set).
- Tag length: 128 bits (16 bytes). Shorter tags (96, 64 bits) reduce security margin.
Prefer Curve25519 and Ed25519 over NIST P-256/P-384 for new systems. The NIST curves have parameter Generation that was not fully transparent (though no backdoor has been found), and Curve25519/Ed25519 have simpler, faster implementations with fewer side-channel risks.
Diffie-Hellman Key Exchange
Section titled “Diffie-Hellman Key Exchange”Diffie-Hellman (DH) allows two parties to establish a shared secret over an insecure channel without Transmitting the secret itself.
Finite Field Diffie-Hellman (FFDHE)
Section titled “Finite Field Diffie-Hellman (FFDHE)”- Alice and Bob agree on a prime and generator (public parameters)
- Alice generates private key Sends
- Bob generates private key Sends
- Alice computes
- Bob computes
- Both arrive at the same shared secret
An eavesdropper who sees and cannot compute without solving the discrete logarithm Problem.
Elliptic Curve Diffie-Hellman (ECDH)
Section titled “Elliptic Curve Diffie-Hellman (ECDH)”Same principle as FFDHE but over an elliptic curve group. ECDH with Curve25519 (X25519) is the Standard for modern key exchange:
// Go example: ECDH key exchangeprivateKey, _ := x25519.GenerateKey(rand.Reader)publicKey := privateKey.Public()
// sharedSecret is the same on both sidessharedSecret, _ := privateKey.ECDH(peerPublicKey)Forward Secrecy
Section titled “Forward Secrecy”Forward secrecy (also called perfect forward secrecy, PFS) ensures that compromise of a long-term Key does not compromise past session keys. If you use RSA to encrypt a symmetric key and the RSA Private key is later compromised, all past sessions can be decrypted.
With ephemeral Diffie-Hellman (DHE or ECDHE), the key exchange uses temporary key pairs that are Discarded after the session. Even if the server’s long-term key is compromised, past sessions remain Secure.
Hash Functions
Section titled “Hash Functions”A cryptographic hash function maps arbitrary-length input to a fixed-length output with the Following properties:
- Preimage resistance: Given hash It is infeasible to find such that \mathrm{Hash(m) = h
- Second preimage resistance: Given It is infeasible to find such that \mathrm{Hash(m_1) = \mathrm{Hash(m_2)
- Collision resistance: It is infeasible to find any pair such that \mathrm{Hash(m_1) = \mathrm{Hash(m_2)
SHA Family
Section titled “SHA Family”| Algorithm | Output Size | Block Size | Rounds | Status |
|---|---|---|---|---|
| SHA-1 | 160 bits | 512 bits | 80 | Broken (collision found, 2017) |
| SHA-224 | 224 bits | 512 bits | 64 | Secure |
| SHA-256 | 256 bits | 512 bits | 64 | Secure |
| SHA-384 | 384 bits | 1024 bits | 80 | Secure |
| SHA-512 | 512 bits | 1024 bits | 80 | Secure |
| SHA-3-256 | 256 bits | 1088 bits | 24 | Secure (Keccak) |
SHA-3 (Keccak)
Section titled “SHA-3 (Keccak)”SHA-3 was selected by NIST in 2012 as a backup to SHA-2. It uses a different internal structure (sponge construction vs Merkle-Damgard in SHA-2), so an attack on SHA-2 would not necessarily affect SHA-3.
SHA-3 is not faster than SHA-2 in software, but it is significantly faster in hardware (FPGA/ASIC Implementations). Use SHA-3 when you want algorithmic diversity or are implementing in hardware.
Password Hashing Functions
Section titled “Password Hashing Functions”Standard hash functions (SHA-256, SHA-3) are not suitable for password hashing. They are Designed to be fast, which makes them vulnerable to brute-force and dictionary attacks with GPUs.
Password hashing functions are designed to be slow and memory-hard, making brute-force attacks Expensive.
bcrypt
Section titled “bcrypt”- Design: Blowfish-based, adaptive cost parameter
- Salt: 128-bit random salt embedded in output
- Cost factor: 2^{\mathrm{cost} iterations (default 10 = 1024 iterations, recommended 12+)
- Output: 60 characters (e.g.,
$2b$12$R9h/cIPz0gi...)
import bcrypt
# Hash a password (cost factor 12)password = b"correct_horse_battery_staple"hashed = bcrypt.hashpw(password, bcrypt.gensalt(rounds=12))
# Verifybcrypt.checkpw(password, hashed) # Truebcrypt.checkpw(b"wrong", hashed) # FalseLimitation: bcrypt has a 72-byte password length limit. Passwords longer than 72 bytes are Silently truncated. Pre-hashing with SHA-256 before bcrypt mitigates this.
scrypt
Section titled “scrypt”- Design: Memory-hard, CPU-hard
- Parameters: Cost (CPU/memory), block size (memory), parallelization
- Advantage over bcrypt: Resistant to GPU/ASIC attacks due to memory requirements
import hashlib
# scrypt hash (Python 3.6+)salt = b"random_salt_16_bytes"hashed = hashlib.scrypt( b"password", salt=salt, n=2**14, # CPU/memory cost r=8, # block size p=1, # parallelization dklen=64 # output length)Argon2
Section titled “Argon2”Argon2 is the winner of the Password Hashing Competition (2015) and is recommended by OWASP for new Applications.
| Variant | Resistance Target | Use Case |
|---|---|---|
| Argon2id | GPU + side-channel | Recommended default |
| Argon2i | Side-channel | Threat model includes side-channel attacks |
| Argon2d | GPU | Threat model excludes side-channel attacks |
from argon2 import PasswordHasher
ph = PasswordHasher( time_cost=3, # number of iterations memory_cost=65536, # 64 MB parallelism=4, # number of threads hash_len=32, # output length salt_len=16 # salt length)
hashed = ph.hash("correct_horse_battery_staple")ph.verify(hashed, "correct_horse_battery_staple") # TrueOWASP recommended parameters (2023):
| Parameter | Argon2id | scrypt | bcrypt |
|---|---|---|---|
| Memory | 64 MB (65536) | N/A | N/A |
| Iterations | 3 | N/A | 10+ |
| Parallelism | 4 | 1 | N/A |
| Salt length | 16 bytes | 16 bytes | 16 bytes (embedded) |
| Hash length | 32 bytes | 32 bytes | 31 characters |
Message Authentication Codes
Section titled “Message Authentication Codes”A MAC provides integrity and authenticity for a message. The sender and receiver share a secret key.
HMAC (Hash-based MAC, RFC 2104) uses a cryptographic hash function with a secret key:
\mathrm{HMAC(K, m) = H\Big((K' \oplus \mathrm{opad) \;\|\; H\big((K' \oplus \mathrm{ipad) \;\|\; m\big)\Big)Where is the key padded to the block size, \mathrm{opad = \mathrm{0x5c...And \mathrm{ipad = \mathrm{0x36....
import hmacimport hashlib
key = b"secret_key"message = b"important message"
mac = hmac.new(key, message, hashlib.sha256).hexdigest()# Verify: hmac.compare_digest(mac, received_mac)The “harvest now, decrypt later” threat is real. Attackers may be recording encrypted traffic today To decrypt it when quantum computers become available. Organizations with long-term confidentiality Requirements (government, healthcare, financial) should begin PQC migration planning now.
Common Pitfalls
Section titled “Common Pitfalls”Pitfall 1: ECB Mode
Section titled “Pitfall 1: ECB Mode”ECB encrypts each block independently, preserving patterns in the data. Never use ECB. If you need a Mode without authentication, use CTR. If you need both confidentiality and integrity, use GCM or ChaCha20-Poly1305.
Pitfall 2: Hardcoded Keys
Section titled “Pitfall 2: Hardcoded Keys”Keys in source code, configuration files committed to Git, or environment variables in CI logs are Immediately compromised. Use a dedicated secret management system (HashiCorp Vault, AWS KMS, Azure Key Vault).
Pitfall 3: Using SHA-256 for Password Hashing
Section titled “Pitfall 3: Using SHA-256 for Password Hashing”SHA-256 is designed to be fast. An attacker with a modern GPU can compute billions of SHA-256 hashes Per second. Use bcrypt (cost 12+), scrypt, or Argon2id for password storage.
Pitfall 4: Reusing Nonces/IVs
Section titled “Pitfall 4: Reusing Nonces/IVs”In GCM mode, nonce reuse is catastrophic — it enables both forgeries and plaintext recovery. In CTR Mode, nonce reuse leaks XOR of plaintexts. Always use a unique nonce per encryption operation.
Pitfall 5: Ignoring Certificate Validation
Section titled “Pitfall 5: Ignoring Certificate Validation”Disabling TLS certificate verification (verify=False in Python, InsecureRequestWarning Suppression) eliminates the entire trust model of TLS. This is common in development and Occasionally leaks into production. Never disable certificate validation.
Pitfall 6: Using Deprecated Algorithms
Section titled “Pitfall 6: Using Deprecated Algorithms”MD5, SHA-1, DES, 3DES, RC4, and RSA with PKCS#1 v1.5 padding are all broken or deprecated. Use AES-256-GCM, ChaCha20-Poly1305, SHA-256/384, RSA-PSS, or Ed25519.
Pitfall 7: Not Providing Forward Secrecy
Section titled “Pitfall 7: Not Providing Forward Secrecy”Without forward secrecy (ephemeral Diffie-Hellman), compromise of the server’s private key Compromises all past sessions. TLS 1.3 mandates forward secrecy, but TLS 1.2 with RSA key exchange Does not provide it. Ensure your cipher suites use ECDHE or DHE.
Reference Standards: NIST SP 800-57 (Key Management), NIST SP 800-63B (Digital Identity), NIST SP 800-38D (GCM), NIST SP 800-132 (PBKDF2), NIST FIPS 203/204/205 (Post-Quantum), RFC 8446 (TLS 1.3), RFC 8017 (RSA), RFC 8032 (EdDSA), RFC 7748 (Curve25519), RFC 5869 (HKDF).
Summary
Section titled “Summary”This topic covers the mathematical techniques and concepts related to cryptography, including key theorems, methods, and problem-solving approaches.
Key concepts include:
- fundamental definitions and theorems
- algebraic and graphical methods
- proof and logical reasoning
- problem-solving strategies
- applications and modelling
Regular practice with a variety of question types is essential to build fluency and confidence in applying these mathematical techniques.
Worked Examples
Section titled “Worked Examples”Worked examples demonstrating the application of key concepts are covered in the detailed sub-pages linked above.