CRYPTOGRAPHY
Symmetric & asymmetric encryption · Hash functions & MACs · PKI & certificates · TLS internals · Cryptographic attacks · Post-quantum cryptography
Cryptography Fundamentals
- The four cryptographic goals are Confidentiality, Integrity, Authentication, and Non-repudiation - different algorithms serve different goals.
- Symmetric encryption solves speed, asymmetric encryption solves key distribution - hybrid systems use both.
- Shannon's confusion (S-boxes) and diffusion (permutations) are the two operations every strong block cipher must perform.
- Security through obscurity is not security - Kerckhoffs's principle requires algorithms to be public and security to rest only on the key.
- Always use a CSPRNG for key generation - regular PRNGs like rand() or math.random() are predictable and never safe for cryptographic use.
Cryptography is the science of securing communication. Before touching any algorithm, you need to understand what goals cryptography actually serves - and what it cannot do.
When you hand your contribution to the chama committee, four things are quietly in play. You seal the cash in an envelope so only the treasurer sees it (confidentiality). The amount is written and witnessed so no one can change it later (integrity). You sign the book so they know it was really you (authentication). And because you signed, you cannot afterwards deny you gave it (non-repudiation). Cryptography chases these same four assurances.
▸ The Four Security Properties (CIA+)
Modern cryptographic systems aim to provide four core guarantees:
Integrity - data has not been altered in transit. Achieved via hash functions and MACs.
Authentication - you are communicating with who you think you are. Achieved via digital signatures and certificates.
Non-repudiation - the sender cannot later deny sending a message. Achieved via digital signatures with a private key only the sender holds.
▸ Symmetric vs Asymmetric Encryption
Symmetric encryption uses the same key to encrypt and decrypt. It is fast and efficient for bulk data, but has a fundamental problem: how do you securely share the key with the other party in the first place? This is called the key distribution problem.
Asymmetric encryption uses a mathematically linked key pair - a public key (share freely) and a private key (never share). Anyone can encrypt with your public key, but only you can decrypt with your private key. This solves key distribution but is orders of magnitude slower than symmetric encryption.
In practice, hybrid encryption is used: asymmetric crypto is used to securely exchange a symmetric key, then symmetric crypto handles the actual data. TLS does exactly this.
▸ Stream Ciphers vs Block Ciphers
A block cipher encrypts fixed-size chunks of data (AES uses 128-bit blocks). Padding is added if plaintext does not fill a block. Block ciphers are used with modes of operation (CBC, GCM, CTR) to handle data larger than one block.
A stream cipher generates a pseudorandom keystream that is XORed with plaintext one bit or byte at a time. ChaCha20 and RC4 are stream ciphers. They have no padding and are naturally suited to streaming data.
▸ Substitution, Permutation, Confusion, and Diffusion
Claude Shannon (1949) identified two fundamental operations that strong ciphers must perform:
Diffusion - changing one bit of plaintext should change roughly half of the ciphertext bits. Achieved by permutation (ShiftRows, MixColumns in AES). Hides statistical structure.
▸ XOR and the One-Time Pad
XOR (exclusive or) is the fundamental operation of most ciphers. Key property: A XOR B XOR B = A. If you XOR plaintext with a random keystream and send it, the recipient XORs with the same keystream to recover the plaintext.
The one-time pad uses a truly random key the same length as the message, used only once. It is provably unbreakable - but practically useless because the key distribution problem is as hard as just sending the message securely in the first place. Reusing the key destroys security instantly.
▸ Kerckhoffs Principle
A cryptographic system should be secure even if everything about the system, except the key, is public knowledge. Security through obscurity - hiding the algorithm itself - is not security. If your cipher is only safe because attackers do not know the algorithm, you have no real security.
This is why AES, ChaCha20, and RSA are all fully published. Their security comes from the mathematical hardness of the underlying problem, not from secrecy of the design.
▸ Entropy and Randomness
Cryptographic security ultimately rests on randomness. Entropy measures unpredictability in bits. A 128-bit random key has 128 bits of entropy - meaning an attacker must try 2^128 keys on average.
A CSPRNG (Cryptographically Secure Pseudo-Random Number Generator) is seeded from true entropy sources (OS noise, hardware events) and produces output that is computationally indistinguishable from true randomness. A regular PRNG (like rand() in C) is NOT safe for cryptography - its output can often be predicted after observing a few values.
▸ Bit Security Levels
| Security Level | Operations to Break | Symmetric Key Size | RSA Key Size | ECC Key Size |
|---|---|---|---|---|
| 80-bit | 2^80 (~1 sextillion) | 80-bit (obsolete) | 1024-bit (broken) | 160-bit (obsolete) |
| 112-bit | 2^112 | 112-bit | 2048-bit (minimum) | 224-bit |
| 128-bit | 2^128 (currently safe) | 128-bit AES | 3072-bit | 256-bit (P-256) |
| 192-bit | 2^192 | 192-bit AES | 7680-bit | 384-bit (P-384) |
| 256-bit | 2^256 (long-term) | 256-bit AES | 15360-bit | 521-bit (P-521) |
▸ Cryptographic Primitives Reference
| Algorithm | Type | Key Size | Block/Output | Notes |
|---|---|---|---|---|
| AES-256-GCM | Symmetric AEAD | 256-bit | 128-bit block | Gold standard for symmetric encryption |
| ChaCha20-Poly1305 | Symmetric AEAD | 256-bit | Stream | Fast without AES hardware, used in TLS 1.3 |
| RSA-4096 | Asymmetric | 4096-bit | Variable | Slow, avoid for bulk data, use for key exchange |
| ECDH P-256 | Key Exchange | 256-bit | N/A | Fast key agreement, 128-bit security |
| Ed25519 | Signature | 256-bit | 512-bit sig | Fast, deterministic, immune to k-reuse bugs |
| SHA-256 | Hash | N/A | 256-bit | Merkle-Damgard construction, widely used |
| HMAC-SHA256 | MAC | Variable | 256-bit | Authenticated integrity, immune to length extension |
| Argon2id | Password KDF | Variable | Variable | Memory-hard, PHC winner, use for passwords |
| HKDF-SHA256 | Key Derivation | Variable | Variable | Derive multiple keys from one master secret |
- The four cryptographic goals are Confidentiality, Integrity, Authentication, and Non-repudiation - different algorithms serve different goals.
- Symmetric encryption solves speed, asymmetric encryption solves key distribution - hybrid systems use both.
- Shannon's confusion (S-boxes) and diffusion (permutations) are the two operations every strong block cipher must perform.
- Security through obscurity is not security - Kerckhoffs's principle requires algorithms to be public and security to rest only on the key.
- Always use a CSPRNG for key generation - regular PRNGs like rand() or math.random() are predictable and never safe for cryptographic use.