Cryptography | Security - Wyatt's Notes
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.
Cross-References
Section titled “Cross-References”- Authentication applies cryptographic primitives to verify user identity through password hashing and token generation.
- Security Fundamentals defines the confidentiality and integrity goals that cryptographic algorithms are designed to achieve.
- Web Security uses cryptography to protect web communications through TLS and encrypted data storage.