Showing raw Markdown: the renderer could not load.
## Why this matters
A company's customer database leaks. The good news: passwords were "encrypted." The bad news: they were encrypted with one key kept in the same server, and within a day the attackers have every password in plain text. Next door, a team downloads an installer from a mirror, checks that the checksum on the mirror's page matches, and installs malware: the attacker changed both the file and the checksum. In both cases the team used cryptography. In both cases they used the **wrong tool** for the job.
Cryptography is a small toolbox: hashes, symmetric encryption, asymmetric encryption, signatures and certificates. Each tool gives one specific guarantee, and none gives all of them. You don't need the mathematics to use the tools well. You do need to know what each one promises, what it doesn't, and which one to pick. This lesson gives you that map, and the lab lets you try every tool with `openssl`.
## Learning objectives
By the end of this lesson you will be able to:
- Explain what a cryptographic hash guarantees and use one to check the integrity of a file.
- Explain why passwords must be stored with a salt and a slow hash function, not with a plain hash or encryption.
- Compare symmetric encryption (AES) and asymmetric encryption (RSA, elliptic curves), and explain how protocols such as TLS combine them.
- Explain how a digital signature provides integrity and authenticity, and create and verify one.
- Read an X.509 certificate and explain how a certificate chain lets a browser trust a website.
## Key concepts
### What cryptography gives you
**Cryptography** is the science of protecting information with mathematics. It turns "trust the network" into "trust a key." It offers four guarantees. Every tool in this lesson delivers one or two of them.
| Guarantee | Question it answers | Typical tool |
|---|---|---|
| **Confidentiality** | Can anyone else read it? | Encryption (AES) |
| **Integrity** | Was it changed on the way? | Hash, MAC, signature |
| **Authenticity** | Who really sent it? | MAC, signature, certificate |
| **Non-repudiation** | Can the sender later deny it? | Digital signature |
The first three come from the confidentiality, integrity and authenticity you met in [Security Fundamentals](../04-security-fundamentals/README.md); non-repudiation is the extra one: proof that only one party could have produced something.
Two ideas hold for everything that follows:
- **The algorithm is public; only the key is secret.** Serious algorithms such as AES and SHA-256 are published and attacked by researchers for years. A secret algorithm is a warning sign, not a feature. This is **Kerckhoffs's principle**.
- **Encoding is not encryption.** Base64 or hex just changes how bytes are written. Anyone can reverse it with no key. `cGFzc3dvcmQ=` is the word `password`.
> [!CHECK]
> A developer stores the text `cGFzc3dvcmQ=` (Base64) instead of the password `password`, and calls it "encrypted." What is wrong?
>
> - [ ] Base64 is too weak; they should use a longer Base64 string
> - [x] Base64 is an encoding that anyone can reverse without a key, so it gives no confidentiality
> - [ ] Base64 gives integrity but not authenticity
> - [ ] Nothing: Base64 is a form of symmetric encryption
>
> Encryption needs a secret key. Encoding is a public, reversible format change.
### Hashes
Think of a **cryptographic hash function** as a fingerprint machine. You feed it any amount of data (one letter or a 4 GB file) and it returns a short, fixed-size fingerprint called a **digest**. SHA-256, defined in FIPS 180-4, always returns 256 bits, written as 64 hexadecimal characters.
<figure class="diagram" style="--dg-w: 1040px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1040 378" width="1040" height="378" role="img" aria-labelledby="crypto-hash-avalanche-title crypto-hash-avalanche-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="crypto-hash-avalanche-title">The avalanche effect: changing one character changes the whole SHA-256 digest</title>
<desc id="crypto-hash-avalanche-desc">Input A, the text Transfer 100 EUR to account ES00 1234, goes through SHA-256 and gives the digest 190fd1598c81d23bd943f4ba864859db3039ab305e60a50bee9bec461d4b092f. Input B is the same text with 100 changed to 900, one character different. It gives the digest 10bfa161dcd38705488563c4f86395ad2456430fe5b97d08fbbfc8c8322f96c5. The two digests are both 256 bits, 64 hex characters, and 127 of the 256 bits differ, so they look unrelated.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="1039" height="377" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<rect class="dg-card" x="24" y="24" width="360" height="104" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><g class="dg-icon" transform="translate(40 40) scale(1.2500)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/><path d="M10 9H8"/><path d="M16 13H8"/><path d="M16 17H8"/></g></g>
<g class="dg-l1"><text x="82" y="61" font-size="15" class="dg-ink" fill="#0369a1" font-weight="700">Input A</text></g>
<text x="42" y="92" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">Transfer 100 EUR to account ES00 1234</text>
<rect class="dg-card" x="496" y="24" width="520" height="104" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l7"><g class="dg-icon" transform="translate(512 40) scale(1.2500)" fill="none" stroke="#6d28d9" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><line x1="4" x2="20" y1="9" y2="9"/><line x1="4" x2="20" y1="15" y2="15"/><line x1="10" x2="8" y1="3" y2="21"/><line x1="16" x2="14" y1="3" y2="21"/></g></g>
<g class="dg-l7"><text x="554" y="61" font-size="15" class="dg-ink" fill="#6d28d9" font-weight="700">SHA-256 digest of A</text></g>
<text x="514" y="92" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">190fd1598c81d23bd943f4ba864859db</text>
<text x="514" y="112" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">3039ab305e60a50bee9bec461d4b092f</text>
<g class="dg-l7"><path class="dg-stroke" d="M386 76H488" stroke="#6d28d9" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l7"><path class="dg-stroke" d="M483 70l8 6-8 6" stroke="#6d28d9" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l7"><rect class="dg-bg-plain" x="407.46" y="46" width="63.08" height="24" rx="12" fill="#ffffff"/><text x="439" y="62.5" font-size="11.5" class="dg-ink" fill="#6d28d9" font-weight="600" text-anchor="middle">SHA-256</text></g>
<rect class="dg-card" x="24" y="154" width="360" height="104" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><g class="dg-icon" transform="translate(40 170) scale(1.2500)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/><path d="M10 9H8"/><path d="M16 13H8"/><path d="M16 17H8"/></g></g>
<g class="dg-l1"><text x="82" y="191" font-size="15" class="dg-ink" fill="#0369a1" font-weight="700">Input B</text></g>
<text x="42" y="222" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">Transfer 900 EUR to account ES00 1234</text>
<rect class="dg-card" x="496" y="154" width="520" height="104" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l7"><g class="dg-icon" transform="translate(512 170) scale(1.2500)" fill="none" stroke="#6d28d9" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><line x1="4" x2="20" y1="9" y2="9"/><line x1="4" x2="20" y1="15" y2="15"/><line x1="10" x2="8" y1="3" y2="21"/><line x1="16" x2="14" y1="3" y2="21"/></g></g>
<g class="dg-l7"><text x="554" y="191" font-size="15" class="dg-ink" fill="#6d28d9" font-weight="700">SHA-256 digest of B</text></g>
<text x="514" y="222" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10bfa161dcd38705488563c4f86395ad</text>
<text x="514" y="242" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">2456430fe5b97d08fbbfc8c8322f96c5</text>
<g class="dg-l7"><path class="dg-stroke" d="M386 206H488" stroke="#6d28d9" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l7"><path class="dg-stroke" d="M483 200l8 6-8 6" stroke="#6d28d9" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l7"><rect class="dg-bg-plain" x="407.46" y="176" width="63.08" height="24" rx="12" fill="#ffffff"/><text x="439" y="192.5" font-size="11.5" class="dg-ink" fill="#6d28d9" font-weight="600" text-anchor="middle">SHA-256</text></g>
<g class="dg-warn"><rect class="dg-bg-plain" x="133.82" y="129" width="140.36" height="24" rx="12" fill="#ffffff"/><text x="204" y="145.5" font-size="11.5" class="dg-ink" fill="#b91c1c" font-weight="600" text-anchor="middle">1 character changed</text></g>
<rect class="dg-panel" x="24" y="282" width="992" height="72" rx="12" fill="#f0f0ec"/>
<g class="dg-l3"><g class="dg-icon" transform="translate(42 304) scale(1.0833)" fill="none" stroke="#b45309" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3"/><path d="M12 9v4"/><path d="M12 17h.01"/></g></g>
<text x="84" y="313" font-size="13" class="dg-text" fill="#17171c" font-weight="700">Same length for any input: 256 bits = 64 hex characters.</text>
<text x="84" y="335" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">127 of the 256 bits differ: the digests look unrelated.</text>
</svg></figure>
*Figure 1. One changed character gives a completely different digest, and the digest always has the same length.*
A good hash has five properties:
| Property | Meaning |
|---|---|
| **Deterministic** | The same input always gives the same digest. |
| **Fixed length** | Any input, any size, gives a digest of the same size. |
| **One-way** (preimage resistance) | Given a digest, you can't work back to an input that produces it. |
| **Avalanche effect** | A tiny change in the input changes about half the digest bits, as Figure 1 shows. |
| **Collision resistance** | You can't find two different inputs with the same digest. |
A hash can't be reversed, but that doesn't make it a secret keeper: if the input is guessable, an attacker just hashes guesses until one matches.
**Collisions are the weak point of old hashes.** Because digests are finite, collisions exist; a good hash makes finding one infeasible. **MD5** (Message Digest 5) has been broken for collisions since 2004, and attackers used it in 2008 to forge a certificate authority certificate. **SHA-1** had its first public collision in 2017 (the "SHAttered" attack), and practical chosen-prefix collisions followed. Don't use MD5 or SHA-1 for anything where an attacker could choose the input: signatures, certificates, integrity checks. Use **SHA-256**, SHA-512 or SHA-3 (FIPS 202).
What hashes are for: checking that a file wasn't corrupted or changed (`sha256sum`), identifying content, and as a building block inside MACs, signatures and password storage. What they are **not** for: proving who made the file. If an attacker can change the file, they can change the published hash next to it. Authenticity needs a key; you'll see it in the signatures section.
> [!CHECK]
> You download a file and its SHA-256 digest from the same web page, and they match. What does this prove?
>
> - [ ] The file was published by the real software vendor
> - [ ] The file is free of malware
> - [x] The file arrived intact, but only if the digest itself came from a source an attacker couldn't change
> - [ ] The file is encrypted
>
> A matching digest shows the bytes are the ones that were hashed. It says nothing about who hashed them, so an attacker who controls the page can replace both. A signature closes that gap.
### Storing passwords
The most common mistake with hashes is using them for passwords the wrong way. A site must check a login without being able to read the password back. That sounds like a hash, but a **plain** hash isn't enough, because **people choose guessable passwords** and **hashes are fast**.
Three ways to store a password, from worst to best:
| Method | What an attacker with the database can do |
|---|---|
| **Plain text** or **reversible encryption** | Read every password at once. Encryption fails here because the key must live on the same server that holds the data; whoever steals one steals both. |
| **Plain fast hash** (`SHA-256(password)`) | Hash millions of common passwords a second and compare. Identical passwords give identical hashes, so one precomputed table, called a **rainbow table**, cracks all of them. |
| **Salted slow hash** | Attack each account separately, and each guess costs real time and memory. |
<figure class="diagram" style="--dg-w: 1040px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1040 414" width="1040" height="414" role="img" aria-labelledby="crypto-password-storage-title crypto-password-storage-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="crypto-password-storage-title">Storing passwords: plain fast hash versus salted slow hash</title>
<desc id="crypto-password-storage-desc">Two panels compare password storage. Left, plain fast hash, marked don't: users alice and bob both have the password hunter2 and both are stored as SHA-256 f52fbd32b2b3b86f, the same value, so one precomputed table cracks every account that uses it, and hashing is so fast that a GPU tries billions of guesses per second. Right, salt plus slow hash, marked do: alice has salt a1b2c3d4e5f60718 and stored PBKDF2 hash 0fc70abb0f92eda5 and bob has salt 0f1e2d3c4b5a6978 and stored hash db5d5209b5f16934, both after 600,000 rounds. The same password gives different hashes, so each account must be attacked separately, and each guess is deliberately slow and costly.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="1039" height="413" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<g class="dg-warn"><rect class="dg-tint" x="24" y="24" width="500" height="366" rx="16" fill="#fbf1f1" stroke="#e3a4a4" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(40 37) scale(0.7500)" fill="none" stroke="#b91c1c" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3"/><path d="M12 9v4"/><path d="M12 17h.01"/></g>
<text x="66" y="51" font-size="13" class="dg-ink" fill="#b91c1c" font-weight="700">Plain fast hash: don't</text></g>
<rect class="dg-card" x="40" y="70" width="468" height="100" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-warn"><g class="dg-icon" transform="translate(56 86) scale(1.2500)" fill="none" stroke="#b91c1c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></g></g>
<g class="dg-warn"><text x="98" y="107" font-size="15" class="dg-ink" fill="#b91c1c" font-weight="700">alice</text></g>
<text x="58" y="138" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">password: hunter2</text>
<text x="240" y="104" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">SHA-256(hunter2)</text>
<text x="240" y="126" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">stored: f52fbd32b2b3b86f...</text>
<rect class="dg-card" x="40" y="182" width="468" height="100" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-warn"><g class="dg-icon" transform="translate(56 198) scale(1.2500)" fill="none" stroke="#b91c1c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></g></g>
<g class="dg-warn"><text x="98" y="219" font-size="15" class="dg-ink" fill="#b91c1c" font-weight="700">bob</text></g>
<text x="58" y="250" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">password: hunter2</text>
<text x="240" y="216" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">SHA-256(hunter2)</text>
<text x="240" y="238" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">stored: f52fbd32b2b3b86f...</text>
<text x="44" y="314" font-size="12.5" class="dg-text" fill="#17171c" font-weight="700">Same password, same hash: one precomputed table</text>
<text x="44" y="333" font-size="12.5" class="dg-text" fill="#17171c" font-weight="700">cracks every account that uses it.</text>
<text x="44" y="352" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500">Hashing is fast: a GPU tries billions per second.</text>
<g class="dg-ok"><rect class="dg-tint" x="540" y="24" width="500" height="366" rx="16" fill="#f1f7f3" stroke="#a1ccb1" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(556 37) scale(0.7500)" fill="none" stroke="#15803d" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z"/></g>
<text x="582" y="51" font-size="13" class="dg-ink" fill="#15803d" font-weight="700">Salt + slow hash: do</text></g>
<rect class="dg-card" x="556" y="70" width="468" height="100" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-ok"><g class="dg-icon" transform="translate(572 86) scale(1.2500)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></g></g>
<g class="dg-ok"><text x="614" y="107" font-size="15" class="dg-ink" fill="#15803d" font-weight="700">alice</text></g>
<text x="574" y="138" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">password: hunter2</text>
<text x="756" y="104" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">salt a1b2c3d4e5f60718</text>
<text x="756" y="126" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">PBKDF2, 600,000 rounds</text>
<text x="756" y="148" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">stored: 0fc70abb0f92eda5...</text>
<rect class="dg-card" x="556" y="182" width="468" height="100" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-ok"><g class="dg-icon" transform="translate(572 198) scale(1.2500)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></g></g>
<g class="dg-ok"><text x="614" y="219" font-size="15" class="dg-ink" fill="#15803d" font-weight="700">bob</text></g>
<text x="574" y="250" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">password: hunter2</text>
<text x="756" y="216" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">salt 0f1e2d3c4b5a6978</text>
<text x="756" y="238" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">PBKDF2, 600,000 rounds</text>
<text x="756" y="260" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">stored: db5d5209b5f16934...</text>
<text x="560" y="314" font-size="12.5" class="dg-text" fill="#17171c" font-weight="700">Same password, different hashes: each account</text>
<text x="560" y="333" font-size="12.5" class="dg-text" fill="#17171c" font-weight="700">must be attacked separately.</text>
<text x="560" y="352" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500">Each guess is made deliberately slow and costly.</text>
</svg></figure>
*Figure 2. A random salt makes equal passwords look different; a slow function makes every guess expensive.*
Two ingredients fix the problem:
- A **salt** is a random value, unique per password, stored next to the hash. It isn't secret. Its job is to make `SHA-256(hunter2)` different for every user, so precomputed tables are useless and the attacker must crack accounts one by one.
- A **slow, tunable function**, called a **password hashing function** or key-derivation function (KDF), repeats work (and, in the best ones, memory) so one guess takes milliseconds instead of nanoseconds. Legitimate logins pay that cost once; an attacker pays it billions of times.
Use one of these, in this order of preference:
| Function | Notes |
|---|---|
| **Argon2id** | Memory-hard (needs lots of RAM, which makes GPUs less effective). Specified in RFC 9106. First choice for new systems. |
| **scrypt** | Memory-hard (RFC 7914). A good alternative. |
| **bcrypt** | Old and widely supported. Only uses 4 KB of memory, and ignores everything after the first 72 bytes of the password. |
| **PBKDF2** | Not memory-hard, but the one approved by FIPS 140 validated modules (RFC 8018). Needs a high iteration count. |
Parameters (iteration count, memory) must grow as hardware gets faster. Take them from the current [OWASP Password Storage Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html) rather than from this lesson. For example, at the time of writing OWASP suggests 600,000 iterations for PBKDF2 with HMAC-SHA-256, the value in Figure 2.
You don't write this yourself. Use the password-hashing function of your framework or of a maintained library; it generates the salt and stores the salt and parameters inside one string.
> [!NOTE]
> A **pepper** is an extra secret value added to every password before hashing and kept **outside** the database (for example in a secrets manager). It helps if only the database leaks. It doesn't replace the salt or the slow function.
> [!CHECK]
> Why is `SHA-256(password)` a poor way to store passwords, even though SHA-256 is a strong hash?
>
> - [ ] SHA-256 can be reversed with enough time
> - [ ] SHA-256 digests are too short to be unique
> - [x] It is very fast and unsalted, so attackers can test billions of guesses and reuse one table for every account
> - [ ] SHA-256 can only hash words that are at least 12 characters long
>
> SHA-256 is designed to be fast. For passwords you want the opposite: a salt, and a deliberately slow function such as Argon2id.
### Symmetric encryption
**Symmetric encryption** uses **one secret key** to lock and unlock. It works like a house key: the same key opens and locks the door. The standard is the **Advanced Encryption Standard (AES)**, FIPS 197. It works on 128-bit blocks, with a key of 128, 192 or 256 bits. AES is fast, often built into the processor, and no practical attack on it is known. AES-256 is also considered safe against future quantum computers (see the end of this section).
A cipher on its own only handles one block. A **mode of operation** says how to handle a whole message, and the mode matters as much as the cipher:
| Mode | What to know |
|---|---|
| **ECB** | Same plaintext block gives the same ciphertext block, so patterns show through. Never use it. |
| **CBC** | Hides patterns, but gives **no integrity**: an attacker can flip bits in the ciphertext and the decryption just returns altered data. The lab shows this. |
| **GCM** (Galois/Counter Mode) | Encrypts **and** authenticates. Tampering is detected and decryption fails. Defined in NIST SP 800-38D. |
GCM is an **authenticated encryption with associated data (AEAD)** mode. Prefer AEAD: **AES-GCM**, or **ChaCha20-Poly1305** (RFC 8439), which is fast on devices without AES hardware. Two rules apply to GCM: use a **nonce** (a number used once) that is **never repeated with the same key**, and let a library generate it.
Symmetric encryption has one big drawback, the **key distribution problem**: both sides must already share the key. If you could send the key safely, you could send the message the same way. Asymmetric cryptography solves that.
> [!NOTE]
> A quantum computer running Grover's algorithm would roughly halve the effective strength of a symmetric key, so AES-256 stays comfortable. The public-key algorithms are the ones at risk; see "Post-quantum cryptography" below.
### Asymmetric encryption
**Asymmetric** (or **public-key**) cryptography uses a **key pair**: a **public key** you share with everyone and a **private key** only you hold. The two are linked by maths, and knowing the public key doesn't let you derive the private one. Think of a mailbox with a slot: anybody can drop a letter in (use the public key), but only the owner with the key opens it (the private key).
<figure class="diagram" style="--dg-w: 1040px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1040 338" width="1040" height="338" role="img" aria-labelledby="crypto-symmetric-asymmetric-title crypto-symmetric-asymmetric-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="crypto-symmetric-asymmetric-title">Symmetric versus asymmetric encryption</title>
<desc id="crypto-symmetric-asymmetric-desc">Two panels. Symmetric encryption with AES: Alice and Bob both hold the same secret key K and Alice sends ciphertext to Bob. It is fast and used for the bulk of the data, but the key problem is how both sides get K safely. Asymmetric encryption with RSA or elliptic curves: Alice holds Bob's public key, Bob holds the private key, and Alice sends ciphertext to Bob. The public key can be shared in the open, but it is slower, so it is used for key exchange and signatures.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="1039" height="337" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<g class="dg-l3"><rect class="dg-tint" x="24" y="24" width="500" height="290" rx="16" fill="#faf5f0" stroke="#e1ba9d" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(40 37) scale(0.7500)" fill="none" stroke="#b45309" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m15.5 7.5 2.3 2.3a1 1 0 0 0 1.4 0l2.1-2.1a1 1 0 0 0 0-1.4L19 4"/><path d="m21 2-9.6 9.6"/><circle cx="7.5" cy="15.5" r="5.5"/></g>
<text x="66" y="51" font-size="13" class="dg-ink" fill="#b45309" font-weight="700">Symmetric encryption (AES)</text></g>
<rect class="dg-card" x="40" y="70" width="164" height="96" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><g class="dg-icon" transform="translate(56 86) scale(1.2500)" fill="none" stroke="#b45309" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></g></g>
<g class="dg-l3"><text x="98" y="107" font-size="14" class="dg-ink" fill="#b45309" font-weight="700">Alice</text></g>
<text x="58" y="138" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">has secret key K</text>
<rect class="dg-card" x="344" y="70" width="164" height="96" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><g class="dg-icon" transform="translate(360 86) scale(1.2500)" fill="none" stroke="#b45309" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></g></g>
<g class="dg-l3"><text x="402" y="107" font-size="14" class="dg-ink" fill="#b45309" font-weight="700">Bob</text></g>
<text x="362" y="138" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">has the same key K</text>
<g class="dg-l3"><path class="dg-stroke" d="M206 118H336" stroke="#b45309" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l3"><path class="dg-stroke" d="M331 112l8 6-8 6" stroke="#b45309" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l3"><rect class="dg-bg-plain" x="232.8" y="88" width="82.4" height="24" rx="12" fill="#ffffff"/><text x="274" y="104.5" font-size="11.5" class="dg-ink" fill="#b45309" font-weight="600" text-anchor="middle">ciphertext</text></g>
<g class="dg-ok"><g class="dg-icon" transform="translate(44 196) scale(1.0000)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></g></g>
<text x="78" y="213" font-size="13" class="dg-text" fill="#17171c" font-weight="600">Fast: used for the bulk of the data</text>
<g class="dg-l4"><g class="dg-icon" transform="translate(44 236) scale(1.0000)" fill="none" stroke="#c2410c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3"/><path d="M12 9v4"/><path d="M12 17h.01"/></g></g>
<text x="78" y="253" font-size="13" class="dg-text" fill="#17171c" font-weight="600">Key problem: how do both sides get K safely?</text>
<g class="dg-l7"><rect class="dg-tint" x="540" y="24" width="500" height="290" rx="16" fill="#f6f2fd" stroke="#c5a9f0" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(556 37) scale(0.7500)" fill="none" stroke="#6d28d9" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="4" y="11" width="16" height="10" rx="2"/><path d="M8 11V7a4 4 0 0 1 8 0v4"/></g>
<text x="582" y="51" font-size="13" class="dg-ink" fill="#6d28d9" font-weight="700">Asymmetric encryption (RSA, elliptic curves)</text></g>
<rect class="dg-card" x="556" y="70" width="164" height="96" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l7"><g class="dg-icon" transform="translate(572 86) scale(1.2500)" fill="none" stroke="#6d28d9" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></g></g>
<g class="dg-l7"><text x="614" y="107" font-size="14" class="dg-ink" fill="#6d28d9" font-weight="700">Alice</text></g>
<text x="574" y="138" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">has Bob's public key</text>
<rect class="dg-card" x="860" y="70" width="164" height="96" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l7"><g class="dg-icon" transform="translate(876 86) scale(1.2500)" fill="none" stroke="#6d28d9" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></g></g>
<g class="dg-l7"><text x="918" y="107" font-size="14" class="dg-ink" fill="#6d28d9" font-weight="700">Bob</text></g>
<text x="878" y="138" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">has the private key</text>
<g class="dg-l7"><path class="dg-stroke" d="M722 118H852" stroke="#6d28d9" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l7"><path class="dg-stroke" d="M847 112l8 6-8 6" stroke="#6d28d9" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l7"><rect class="dg-bg-plain" x="748.8" y="88" width="82.4" height="24" rx="12" fill="#ffffff"/><text x="790" y="104.5" font-size="11.5" class="dg-ink" fill="#6d28d9" font-weight="600" text-anchor="middle">ciphertext</text></g>
<g class="dg-ok"><g class="dg-icon" transform="translate(560 196) scale(1.0000)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></g></g>
<text x="594" y="213" font-size="13" class="dg-text" fill="#17171c" font-weight="600">Public key can be shared in the open</text>
<g class="dg-l4"><g class="dg-icon" transform="translate(560 236) scale(1.0000)" fill="none" stroke="#c2410c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3"/><path d="M12 9v4"/><path d="M12 17h.01"/></g></g>
<text x="594" y="253" font-size="13" class="dg-text" fill="#17171c" font-weight="600">Slower: used for key exchange and signatures</text>
</svg></figure>
*Figure 3. Symmetric: one shared key, fast, hard to distribute. Asymmetric: a key pair, slower, easy to distribute.*
The families you'll meet:
| Family | Examples | Notes |
|---|---|---|
| **RSA** (Rivest–Shamir–Adleman) | RSA-2048, RSA-3072 | Based on factoring big numbers. Keys are large. At least 2048 bits today. |
| **Elliptic-curve cryptography (ECC)** | P-256, Ed25519, X25519 | Based on maths on curves. Much smaller keys for the same strength: a 256-bit curve key is comparable to RSA-3072. Preferred for new systems. |
Asymmetric cryptography does three jobs:
- **Key exchange.** Two parties who have never met agree on a shared secret over an open network. **Diffie-Hellman (DH)** and its elliptic-curve version **ECDH** do this, for example with the X25519 curve (RFC 7748). Using fresh keys for every exchange is called **ephemeral**, written ECDHE.
- **Digital signatures.** Sign with the private key, verify with the public key (next section).
- **Encryption of small data**, such as RSA encrypting a key. Today this is rare: protocols prefer key exchange.
Asymmetric operations are far slower than AES, so they are never used to encrypt bulk data. Real protocols **combine** both kinds, as you see in "How it works."
> [!CHECK]
> Alice wants to send Bob a secret message using asymmetric encryption. Which key does she use to encrypt, and which does Bob use to decrypt?
>
> - [ ] She uses her own private key; Bob uses her public key
> - [ ] She uses her own public key; Bob uses her private key
> - [x] She uses Bob's public key; Bob uses his own private key
> - [ ] They both use the same shared key
>
> Anyone can encrypt with Bob's public key, but only Bob's private key, which never leaves him, can decrypt. The last option is symmetric encryption.
### MACs and digital signatures
Encryption hides content; it doesn't prove who wrote it or that it wasn't changed. Two tools do that.
A **message authentication code (MAC)** is a hash mixed with a **shared secret key**. The most common is **HMAC** (hash-based MAC, RFC 2104). The sender attaches the tag; the receiver recomputes it with the same key. A match proves the message wasn't changed and came from someone who has the key. But **both** sides have the key, so either could have created the tag: a MAC can't prove to a third party who made it.
A **digital signature** uses a **key pair**. The signer computes a hash of the message, then signs it with the **private key**. Anyone with the **public key** can verify. Only the private key holder could have produced it, and the signature breaks if even one byte of the message changes.
<figure class="diagram" style="--dg-w: 1060px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1060 478" width="1060" height="478" role="img" aria-labelledby="crypto-sign-verify-title crypto-sign-verify-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="crypto-sign-verify-title">How a digital signature is created and verified</title>
<desc id="crypto-sign-verify-desc">Top, the signer, who holds the private key, in four steps: the message, which is the file to protect; hash it to a SHA-256 digest; sign the digest with the private key; the resulting signature is sent with the message. An arrow labelled message plus signature plus public key leads to the verifier, who holds the public key, in four steps: receive the message and signature; hash the message with the same hash function; check the signature with the public key; the result is valid, or verification fails.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="1059" height="477" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<g class="dg-l7"><rect class="dg-tint" x="20" y="20" width="1020" height="190" rx="16" fill="#f6f2fd" stroke="#c5a9f0" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(36 33) scale(0.7500)" fill="none" stroke="#6d28d9" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 20h9"/><path d="M16.376 3.622a1 1 0 0 1 3.002 3.002L7.368 18.635a2 2 0 0 1-.855.506l-2.872.838a.5.5 0 0 1-.62-.62l.838-2.872a2 2 0 0 1 .506-.854z"/></g>
<text x="62" y="47" font-size="13" class="dg-ink" fill="#6d28d9" font-weight="700">Signer: holds the private key</text></g>
<rect class="dg-card" x="40" y="66" width="219.5" height="118" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l7"><circle class="dg-solid" cx="62" cy="90" r="12" fill="#6d28d9"/><text x="62" y="94.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">1</text></g>
<g class="dg-l7"><g class="dg-icon" transform="translate(219.5 78) scale(1.0833)" fill="none" stroke="#6d28d9" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/><path d="M10 9H8"/><path d="M16 13H8"/><path d="M16 17H8"/></g></g>
<text x="54" y="128" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Message</text>
<text x="54" y="148" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">the file to protect</text>
<path class="dg-line" d="M265.5 125H287.5" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M279.5 119l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="293.5" y="66" width="219.5" height="118" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l7"><circle class="dg-solid" cx="315.5" cy="90" r="12" fill="#6d28d9"/><text x="315.5" y="94.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">2</text></g>
<g class="dg-l7"><g class="dg-icon" transform="translate(473 78) scale(1.0833)" fill="none" stroke="#6d28d9" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><line x1="4" x2="20" y1="9" y2="9"/><line x1="4" x2="20" y1="15" y2="15"/><line x1="10" x2="8" y1="3" y2="21"/><line x1="16" x2="14" y1="3" y2="21"/></g></g>
<text x="307.5" y="128" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Hash it</text>
<text x="307.5" y="148" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">SHA-256 digest</text>
<path class="dg-line" d="M519 125H541" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M533 119l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="547" y="66" width="219.5" height="118" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l7"><circle class="dg-solid" cx="569" cy="90" r="12" fill="#6d28d9"/><text x="569" y="94.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">3</text></g>
<g class="dg-l7"><g class="dg-icon" transform="translate(726.5 78) scale(1.0833)" fill="none" stroke="#6d28d9" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="m15.5 7.5 2.3 2.3a1 1 0 0 0 1.4 0l2.1-2.1a1 1 0 0 0 0-1.4L19 4"/><path d="m21 2-9.6 9.6"/><circle cx="7.5" cy="15.5" r="5.5"/></g></g>
<text x="561" y="128" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Sign the digest</text>
<text x="561" y="148" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">with the private key</text>
<path class="dg-line" d="M772.5 125H794.5" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M786.5 119l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="800.5" y="66" width="219.5" height="118" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l7"><circle class="dg-solid" cx="822.5" cy="90" r="12" fill="#6d28d9"/><text x="822.5" y="94.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">4</text></g>
<g class="dg-l7"><g class="dg-icon" transform="translate(980 78) scale(1.0833)" fill="none" stroke="#6d28d9" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M12 20h9"/><path d="M16.376 3.622a1 1 0 0 1 3.002 3.002L7.368 18.635a2 2 0 0 1-.855.506l-2.872.838a.5.5 0 0 1-.62-.62l.838-2.872a2 2 0 0 1 .506-.854z"/></g></g>
<text x="814.5" y="128" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Signature</text>
<text x="814.5" y="148" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">sent with the message</text>
<g class="dg-l2"><rect class="dg-tint" x="20" y="268" width="1020" height="190" rx="16" fill="#f1f7f3" stroke="#a1ccb1" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(36 281) scale(0.7500)" fill="none" stroke="#15803d" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></g>
<text x="62" y="295" font-size="13" class="dg-ink" fill="#15803d" font-weight="700">Verifier: holds the public key</text></g>
<rect class="dg-card" x="40" y="314" width="219.5" height="118" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><circle class="dg-solid" cx="62" cy="338" r="12" fill="#15803d"/><text x="62" y="342.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">1</text></g>
<g class="dg-l2"><g class="dg-icon" transform="translate(219.5 326) scale(1.0833)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/><path d="M10 9H8"/><path d="M16 13H8"/><path d="M16 17H8"/></g></g>
<text x="54" y="376" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Receive</text>
<text x="54" y="396" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">message + signature</text>
<path class="dg-line" d="M265.5 373H287.5" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M279.5 367l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="293.5" y="314" width="219.5" height="118" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><circle class="dg-solid" cx="315.5" cy="338" r="12" fill="#15803d"/><text x="315.5" y="342.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">2</text></g>
<g class="dg-l2"><g class="dg-icon" transform="translate(473 326) scale(1.0833)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><line x1="4" x2="20" y1="9" y2="9"/><line x1="4" x2="20" y1="15" y2="15"/><line x1="10" x2="8" y1="3" y2="21"/><line x1="16" x2="14" y1="3" y2="21"/></g></g>
<text x="307.5" y="376" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Hash the message</text>
<text x="307.5" y="396" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">same hash function</text>
<path class="dg-line" d="M519 373H541" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M533 367l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="547" y="314" width="219.5" height="118" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><circle class="dg-solid" cx="569" cy="338" r="12" fill="#15803d"/><text x="569" y="342.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">3</text></g>
<g class="dg-l2"><g class="dg-icon" transform="translate(726.5 326) scale(1.0833)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="m15.5 7.5 2.3 2.3a1 1 0 0 0 1.4 0l2.1-2.1a1 1 0 0 0 0-1.4L19 4"/><path d="m21 2-9.6 9.6"/><circle cx="7.5" cy="15.5" r="5.5"/></g></g>
<text x="561" y="376" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Check signature</text>
<text x="561" y="396" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">with the public key</text>
<path class="dg-line" d="M772.5 373H794.5" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M786.5 367l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="800.5" y="314" width="219.5" height="118" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><circle class="dg-solid" cx="822.5" cy="338" r="12" fill="#15803d"/><text x="822.5" y="342.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">4</text></g>
<g class="dg-l2"><g class="dg-icon" transform="translate(980 326) scale(1.0833)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></g></g>
<text x="814.5" y="376" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Result</text>
<text x="814.5" y="396" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">valid, or verification fails</text>
<g class="dg-l1"><path class="dg-stroke" d="M530 212V262" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M524 257l6 8 6-8" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><rect class="dg-bg-plain" x="413.48" y="226" width="233.04" height="24" rx="12" fill="#ffffff"/><text x="530" y="242.5" font-size="12" class="dg-ink" fill="#0369a1" font-weight="600" text-anchor="middle">message + signature + public key</text></g>
</svg></figure>
*Figure 4. The private key signs, the public key verifies; changing the message or the signature makes verification fail.*
| | Hash | MAC (HMAC) | Digital signature |
|---|---|---|---|
| Key | none | one shared secret | key pair |
| Integrity | yes (if the digest is trusted) | yes | yes |
| Authenticity | no | yes, to the other key holder | yes, to anyone |
| Non-repudiation | no | no | yes |
| Speed | fastest | fast | slower |
> [!NOTE]
> Figure 4 shows the classic RSA or ECDSA flow. Ed25519, the algorithm used in the lab, hashes the message as part of its own algorithm, so you give it the file directly. The idea is the same.
Non-repudiation has a limit: a signature proves the **private key** was used, not that the **person** did it. If the key was stolen or shared, the proof is weak.
Signatures are everywhere: software updates, package repositories, signed Git commits, email, documents and, above all, **certificates**.
> [!CHECK]
> Two servers share a secret key and sign messages to each other with HMAC. Later, server A claims it never sent a particular message. Can server B prove to a judge that A sent it?
>
> - [ ] Yes, because only A knows the key
> - [x] No, because B knows the same key and could have created the tag itself
> - [ ] Yes, because HMAC provides non-repudiation
> - [ ] Yes, but only if the message was also encrypted
>
> A MAC uses one shared key, so it can't tell the two key holders apart. Non-repudiation needs a signature, where only one party holds the private key.
### Certificates and the chain of trust
Public-key cryptography has a gap: how do you know a public key really belongs to `www.example.com` and not to an attacker in the middle? The answer is a **certificate**: a signed statement that says "this public key belongs to this name." The standard format is **X.509**, profiled for the Internet in RFC 5280.
The main fields of an X.509 certificate:
| Field | What it holds |
|---|---|
| **Subject** | Who the certificate is for (a name, often the website). |
| **Subject Alternative Name (SAN)** | The list of names the certificate is valid for, for example `example.com` and `*.example.com`. Browsers check the SAN, not the older "Common Name." |
| **Issuer** | Who signed it: the **certificate authority (CA)**. |
| **Validity** | The "Not Before" and "Not After" dates. |
| **Public key** | The subject's public key and its algorithm. |
| **Serial number** | Unique per issuer; used to revoke the certificate. |
| **Extensions** | **Basic Constraints** (is this a CA?), **Key Usage**, Extended Key Usage (for example "TLS server") and others. |
| **Signature** | The issuer's signature over all the above. |
A CA is an organisation trusted to check who requests a certificate and to sign it. Browsers and operating systems don't trust one CA for the whole web. They ship a **root store**: a list of a hundred or so **root CA** certificates they trust. A root key is too valuable to use daily, so the root signs a few **intermediate CAs**, and the intermediates sign the website's **leaf** certificate. That is the **certificate chain**.
<figure class="diagram" style="--dg-w: 1040px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1040 422" width="1040" height="422" role="img" aria-labelledby="crypto-certificate-chain-title crypto-certificate-chain-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="crypto-certificate-chain-title">A certificate chain from a website to a trusted root</title>
<desc id="crypto-certificate-chain-desc">Three cards joined by arrows labelled signs. The Example Root CA is self-signed, has a long validity of about 20 years, is kept offline and sits in the device's root store, which is the trust anchor. It signs the Example Issuing CA, an intermediate with about 10 years of validity that signs website certificates and is sent by the server. That signs the leaf certificate for www.example.com, whose SAN is www.example.com, valid for 90 days, with an EC P-256 public key. Below, four checks the browser makes: the name in the URL matches a SAN; today is inside the validity dates; each signature verifies up to a root; and that root is in the trusted store. All names are invented.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="1039" height="421" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<g class="dg-l2"><rect class="dg-tint" x="16" y="24" width="306" height="230" rx="16" fill="#f1f7f3" stroke="#a1ccb1" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(32 37) scale(0.7500)" fill="none" stroke="#15803d" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z"/></g>
<text x="58" y="51" font-size="13" class="dg-ink" fill="#15803d" font-weight="700">Trusted by the device</text></g>
<rect class="dg-card" x="24" y="66" width="290" height="164" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><g class="dg-icon" transform="translate(40 82) scale(1.2500)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z"/></g></g>
<g class="dg-l2"><text x="82" y="103" font-size="15" class="dg-ink" fill="#15803d" font-weight="700">Example Root CA</text></g>
<text x="42" y="134" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">Self-signed</text>
<text x="42" y="154" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">Long validity (about 20 years)</text>
<text x="42" y="174" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">Kept offline</text>
<text x="42" y="194" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">In the root store</text>
<g class="dg-l2"><path class="dg-stroke" d="M318 148H366" stroke="#15803d" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l2"><path class="dg-stroke" d="M361 142l8 6-8 6" stroke="#15803d" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l2"><rect class="dg-bg-plain" x="320.9" y="114" width="50.2" height="24" rx="12" fill="#ffffff"/><text x="346" y="130.5" font-size="11.5" class="dg-ink" fill="#15803d" font-weight="600" text-anchor="middle">signs</text></g>
<rect class="dg-card" x="374" y="66" width="290" height="164" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><g class="dg-icon" transform="translate(390 82) scale(1.2500)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M6 22V4a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2v18Z"/><path d="M6 12H4a2 2 0 0 0-2 2v6a2 2 0 0 0 2 2h2"/><path d="M18 9h2a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2h-2"/><path d="M10 6h4"/><path d="M10 10h4"/><path d="M10 14h4"/><path d="M10 18h4"/></g></g>
<g class="dg-l1"><text x="432" y="103" font-size="15" class="dg-ink" fill="#0369a1" font-weight="700">Example Issuing CA</text></g>
<text x="392" y="134" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">Issuer: Example Root CA</text>
<text x="392" y="154" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">Validity: about 10 years</text>
<text x="392" y="174" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">Signs website certs</text>
<text x="392" y="194" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">Sent by the server</text>
<g class="dg-l1"><path class="dg-stroke" d="M668 148H716" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M711 142l8 6-8 6" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><rect class="dg-bg-plain" x="670.9" y="114" width="50.2" height="24" rx="12" fill="#ffffff"/><text x="696" y="130.5" font-size="11.5" class="dg-ink" fill="#0369a1" font-weight="600" text-anchor="middle">signs</text></g>
<rect class="dg-card" x="724" y="66" width="290" height="164" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l7"><g class="dg-icon" transform="translate(740 82) scale(1.2500)" fill="none" stroke="#6d28d9" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20"/><path d="M2 12h20"/></g></g>
<g class="dg-l7"><text x="782" y="103" font-size="15" class="dg-ink" fill="#6d28d9" font-weight="700">www.example.com</text></g>
<text x="742" y="134" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">Issuer: Example Issuing CA</text>
<text x="742" y="154" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">SAN: www.example.com</text>
<text x="742" y="174" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">Validity: 90 days</text>
<text x="742" y="194" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">Public key: EC P-256</text>
<rect class="dg-panel" x="24" y="270" width="992" height="128" rx="12" fill="#f0f0ec"/>
<text x="44" y="298" font-size="11" class="dg-muted" fill="#5c5c68" font-weight="700" letter-spacing="1.2">WHAT THE BROWSER CHECKS</text>
<g class="dg-ok"><g class="dg-icon" transform="translate(44 314) scale(0.9167)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></g></g>
<text x="76" y="330" font-size="13" class="dg-text" fill="#17171c" font-weight="600">The name in the URL matches a SAN</text>
<g class="dg-ok"><g class="dg-icon" transform="translate(534 314) scale(0.9167)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></g></g>
<text x="566" y="330" font-size="13" class="dg-text" fill="#17171c" font-weight="600">Today is inside the validity dates</text>
<g class="dg-ok"><g class="dg-icon" transform="translate(44 352) scale(0.9167)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></g></g>
<text x="76" y="368" font-size="13" class="dg-text" fill="#17171c" font-weight="600">Each signature verifies, up to a root</text>
<g class="dg-ok"><g class="dg-icon" transform="translate(534 352) scale(0.9167)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></g></g>
<text x="566" y="368" font-size="13" class="dg-text" fill="#17171c" font-weight="600">That root is in the trusted store</text>
</svg></figure>
*Figure 5. The server sends the leaf and the intermediate; the browser already has the root. Trust flows from the root down.*
A browser accepts the certificate only if **all** of these hold: the name in the address bar matches a SAN, today's date is inside the validity period, every signature in the chain verifies, and the chain ends in a root that's in its store. The server sends the leaf and the intermediates, but not the root, which the browser must already trust.
Other points to know:
- A **self-signed** certificate is signed by its own key. It is fine for a lab but no browser trusts it unless you add it to a store by hand.
- Certificates are short-lived on purpose. **Let's Encrypt**, a free, non-profit CA, issues certificates automatically through the ACME protocol (RFC 8555), and its certificates last 90 days at the time of writing. The maximum life that browsers allow for public TLS certificates is shrinking over the next years, so automate renewal.
- A certificate can be **revoked** (for example, after a key leak) before it expires. Browsers learn about it through **certificate revocation lists (CRLs)** or the **Online Certificate Status Protocol (OCSP)**. Revocation is hard to make reliable, which is another reason for short lifetimes.
- **Certificate Transparency** logs make misissued certificates visible, because CAs must publish every certificate in public logs.
> [!CHECK]
> A browser opens `https://www.example.com`. The certificate it receives lists only `example.org` in its SAN, and it was signed by a trusted CA. What happens?
>
> - [ ] The page loads, because the CA is trusted
> - [x] The browser shows a warning, because the certificate isn't valid for the name in the address bar
> - [ ] The page loads, but without encryption
> - [ ] The browser asks the CA to change the SAN
>
> A valid signature is not enough. The name must also match a SAN, otherwise anyone with any valid certificate could impersonate any site.
### Key management and "don't roll your own"
Strong algorithms fail when the keys are handled badly. In practice, most cryptographic failures are about keys, not maths.
- **Generate keys with a proper random source** (a cryptographically secure random number generator, CSPRNG) built into the operating system or the library, never `rand()`, a timestamp or a fixed string.
- **Never put keys or passwords in source code, Git or chat.** Use a secrets manager or a key management service (KMS). A **hardware security module (HSM)** keeps keys in tamper-resistant hardware.
- **Limit access** (least privilege) and **log use**.
- **Rotate** keys on a schedule and when someone with access leaves or a leak is suspected. Plan for rotation from day one.
- **Back up** keys that protect data you need to read later. A lost key means lost data.
- **Keep keys apart from the data they protect.** Cloud services do this with *envelope encryption*: a data key encrypts the data, and a master key in the KMS encrypts the data key.
And the rule that matters most: **don't roll your own cryptography.** Inventing an algorithm is a job for years of public review. Even using good primitives wrongly (the wrong mode, a repeated nonce, a missing check) breaks them. Use well-reviewed libraries with safe defaults (for example libsodium or your platform's TLS library), use TLS instead of building your own protocol, and prefer high-level functions over choosing modes and nonces yourself.
### Post-quantum cryptography
A large enough quantum computer could break RSA, Diffie-Hellman and elliptic-curve cryptography using Shor's algorithm. No such machine exists today, but an attacker can **record encrypted traffic now and decrypt it later** ("harvest now, decrypt later"), which matters for data that must stay secret for decades. In August 2024 the US National Institute of Standards and Technology (NIST) published the first post-quantum standards: **FIPS 203 (ML-KEM)** for key establishment, **FIPS 204 (ML-DSA)** and **FIPS 205 (SLH-DSA)** for signatures. Browsers and servers already use **hybrid** key exchanges that combine a classical algorithm with ML-KEM (for example X25519MLKEM768), so the connection is safe if either one holds. You may see this name in the lab output.
## How it works
### TLS 1.3: a hybrid in action
When you open an `https://` page, **Transport Layer Security (TLS) 1.3** (RFC 8446) uses every tool of this lesson. It uses the slow, flexible asymmetric tools for a few messages, then the fast symmetric ones for everything else.
<figure class="diagram" style="--dg-w: 1040px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1040 264" width="1040" height="264" role="img" aria-labelledby="crypto-tls-hybrid-title crypto-tls-hybrid-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="crypto-tls-hybrid-title">How TLS 1.3 combines asymmetric and symmetric cryptography</title>
<desc id="crypto-tls-hybrid-desc">Four numbered steps. 1, the client sends a ClientHello with its supported ciphers and an X25519 key share. 2, the server answers with a ServerHello containing its own key share, plus its certificate and a signature. 3, both sides derive the same session keys from the shared secret computed with ephemeral Diffie-Hellman (ECDHE), which is never sent on the wire. 4, the data is encrypted with AES-256-GCM or ChaCha20-Poly1305. Below, two labels: the asymmetric part, key exchange and signature, takes only a few messages; the symmetric part, AES-GCM or ChaCha20, protects all the data.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="1039" height="263" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<rect class="dg-card" x="24" y="24" width="222.5" height="122" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><circle class="dg-solid" cx="46" cy="48" r="12" fill="#0369a1"/><text x="46" y="52.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">1</text></g>
<g class="dg-l1"><g class="dg-icon" transform="translate(206.5 36) scale(1.0833)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 16V7a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v9m16 0H4m16 0 1.28 2.55a1 1 0 0 1-.9 1.45H3.62a1 1 0 0 1-.9-1.45L4 16"/></g></g>
<text x="38" y="86" font-size="14" class="dg-text" fill="#17171c" font-weight="700">ClientHello</text>
<text x="38" y="106" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">supported ciphers</text>
<text x="38" y="123" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">key share (X25519)</text>
<path class="dg-line" d="M252.5 85H274.5" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M266.5 79l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="280.5" y="24" width="222.5" height="122" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><circle class="dg-solid" cx="302.5" cy="48" r="12" fill="#0369a1"/><text x="302.5" y="52.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">2</text></g>
<g class="dg-l1"><g class="dg-icon" transform="translate(463 36) scale(1.0833)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="2" rx="2"/><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6 6h.01"/><path d="M6 18h.01"/></g></g>
<text x="294.5" y="86" font-size="14" class="dg-text" fill="#17171c" font-weight="700">ServerHello</text>
<text x="294.5" y="106" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">its key share</text>
<text x="294.5" y="123" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">certificate + signature</text>
<path class="dg-line" d="M509 85H531" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M523 79l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="537" y="24" width="222.5" height="122" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><circle class="dg-solid" cx="559" cy="48" r="12" fill="#0369a1"/><text x="559" y="52.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">3</text></g>
<g class="dg-l1"><g class="dg-icon" transform="translate(719.5 36) scale(1.0833)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="m15.5 7.5 2.3 2.3a1 1 0 0 0 1.4 0l2.1-2.1a1 1 0 0 0 0-1.4L19 4"/><path d="m21 2-9.6 9.6"/><circle cx="7.5" cy="15.5" r="5.5"/></g></g>
<text x="551" y="86" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Both derive keys</text>
<text x="551" y="106" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">shared secret (ECDHE)</text>
<text x="551" y="123" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">never sent on the wire</text>
<path class="dg-line" d="M765.5 85H787.5" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M779.5 79l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="793.5" y="24" width="222.5" height="122" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><circle class="dg-solid" cx="815.5" cy="48" r="12" fill="#0369a1"/><text x="815.5" y="52.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">4</text></g>
<g class="dg-l1"><g class="dg-icon" transform="translate(976 36) scale(1.0833)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="4" y="11" width="16" height="10" rx="2"/><path d="M8 11V7a4 4 0 0 1 8 0v4"/></g></g>
<text x="807.5" y="86" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Encrypted data</text>
<text x="807.5" y="106" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">AES-256-GCM or</text>
<text x="807.5" y="123" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">ChaCha20-Poly1305</text>
<g class="dg-l7"><rect class="dg-solid" x="24" y="174" width="448" height="26" rx="13" fill="#6d28d9"/><text x="248" y="191.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">Asymmetric part: key exchange and signature, a few messages</text></g>
<g class="dg-l3"><rect class="dg-solid" x="24" y="214" width="434" height="26" rx="13" fill="#b45309"/><text x="241" y="231.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">Symmetric part: AES-GCM or ChaCha20 protects all the data</text></g>
</svg></figure>
*Figure 6. Asymmetric cryptography agrees on a key and proves identity; symmetric cryptography then protects the data.*
1. **ClientHello.** The browser lists the cipher suites it supports and sends its half of a key exchange (a key share, for example X25519). It also sends the server name (`example.com`).
2. **ServerHello.** The server picks a cipher suite and sends its own key share. It then sends its **certificate chain** and a **signature** made with the private key that matches the certificate. This signature proves the server holds the private key, so the certificate isn't just a copied file.
3. **Key derivation.** The browser verifies the chain (Figure 5) and the signature. Both sides combine their private value with the other's key share and get the **same shared secret**, which never travels. From it they derive the session keys.
4. **Encrypted data.** Everything after the hello messages is encrypted and authenticated with an AEAD cipher: `TLS_AES_128_GCM_SHA256`, `TLS_AES_256_GCM_SHA384` or `TLS_CHACHA20_POLY1305_SHA256`.
Because TLS 1.3 only allows **ephemeral** key exchange, it gives **forward secrecy**: new keys for each connection mean that stealing the server's long-term key later doesn't reveal past traffic.
> [!CHECK]
> In TLS 1.3, why isn't the whole conversation encrypted with RSA or ECDH directly?
>
> - [ ] Asymmetric cryptography is less secure than AES
> - [ ] Browsers can't do asymmetric maths
> - [x] Asymmetric operations are much slower, so they only agree on a key and prove identity; a fast symmetric cipher protects the data
> - [ ] The server has no private key
>
> Combining both gives the key distribution of asymmetric cryptography and the speed of symmetric cryptography.
## Worked example
You publish `report.pdf` for customers and want them to detect tampering, even if someone hacks the web page. Walk through which tool fits each need.
**1. Integrity only.** You publish the SHA-256 digest. A customer runs:
```bash
sha256sum report.pdf
```
and compares it with the published value. This catches corruption, and it catches tampering only if the digest sits somewhere an attacker can't change (for example a different, trusted channel).
**2. Integrity and authenticity.** You sign the file with your private key (`openssl pkeyutl -sign …`) and publish `report.pdf.sig` and your **public key**, ideally on a different channel or inside a certificate. A customer verifies with the public key. A hacked web page can't forge the signature without your private key. The lab does exactly this with a text file.
**3. Confidentiality.** If the report is private, encrypt it with an AEAD cipher such as AES-GCM, using a library that manages nonces. To send the key to one customer, don't email it: use an asymmetric method (their public key) or a channel they already trust.
**4. Trust in the public key itself.** If customers can't be sure your public key is really yours, put it in a certificate signed by a CA they trust, as websites do.
The pattern: **pick the tool from the guarantee you need**, not the other way round.
## Common pitfalls
- **Using a hash for authenticity** — A hash next to a file proves nothing if the attacker can change both. Use a signature, or a MAC with a secret key.
- **Encrypting passwords instead of hashing them** — Encrypted passwords can all be recovered with the key. Store a salted Argon2id (or scrypt, bcrypt, PBKDF2) hash.
- **Choosing `SHA-256(password)` or MD5** — Both are fast and unsalted. Use a password hashing function.
- **Confusing encoding with encryption** — Base64 and hex hide nothing.
- **ECB or unauthenticated CBC** — They leak patterns or allow silent modification. Use AES-GCM or ChaCha20-Poly1305.
- **Reusing a nonce with GCM** — It can reveal the plaintext and let attackers forge messages. Let the library generate nonces.
- **Hard-coded keys and secrets in Git** — They are copied everywhere and can't be rotated. Use a secrets manager.
- **Turning off certificate checks** — Options such as `curl -k` or `verify=False` remove the protection of TLS. Fix the trust problem instead (add the right CA, correct the name).
- **Letting certificates expire** — Outages are the usual result. Automate renewal and monitor expiry dates.
- **Inventing your own algorithm or protocol** — Use standard, reviewed ones.
## Security considerations
- **Pick current algorithms and retire old ones.** Avoid MD5, SHA-1, DES and 3DES, RC4, RSA below 2048 bits and TLS versions before 1.2. NIST publishes guidance (for example SP 800-131A) on transitions.
- **Protect private keys above all.** A leaked private key lets an attacker sign as you or decrypt traffic. Store it with strict file permissions (the lab sets `600`), in a KMS or HSM for important keys, and never share it.
- **Plan for compromise.** Know how to revoke and replace a certificate or key quickly. Short certificate lifetimes limit the damage window.
- **Verify, don't just encrypt.** Encryption without authentication lets attackers alter data unnoticed. Prefer AEAD and always verify signatures and certificate chains.
- **Randomness is a dependency.** A weak random number generator breaks every key made from it.
- **Passwords are only as safe as their storage.** Combine slow hashing with rate limiting and multi-factor authentication (covered in a later module).
- **Cryptography is not a complete defence.** It doesn't stop phishing, malware on the endpoint, or a user approving a bad certificate. It protects data in transit and at rest, which is one layer of defence in depth.
- **Ethics and law.** Only test cryptography on data and systems you own or are authorised to test. The lab reads one public website's certificate, which is exactly what any browser does, and nothing more.
## Summary
- Cryptography gives confidentiality, integrity, authenticity and non-repudiation; pick the tool for the guarantee you need.
- A cryptographic hash (SHA-256) is a fixed-size, one-way fingerprint with the avalanche effect; it shows integrity, not authenticity. MD5 and SHA-1 are broken for collisions.
- Store passwords with a unique salt and a slow, memory-hard function such as Argon2id, never with a plain hash or reversible encryption.
- Symmetric encryption (AES-GCM, ChaCha20-Poly1305) is fast but needs a shared key; asymmetric cryptography (RSA, elliptic curves) solves key distribution but is slower. TLS 1.3 uses both: ECDHE and a signature first, then an AEAD cipher.
- A digital signature, made with a private key and checked with a public key, provides integrity, authenticity and non-repudiation; a MAC does not provide non-repudiation.
- An X.509 certificate binds a name (SAN) to a public key, and a chain from the leaf through intermediates to a trusted root lets a browser verify it.
- Don't roll your own crypto, and treat key management (generation, storage, rotation) as the real difficulty.
## Glossary
| Term | Definition |
|---|---|
| Cryptography | The science of protecting information with mathematics. |
| Confidentiality | Only authorised parties can read the data. |
| Integrity | The data hasn't been changed unnoticed. |
| Authenticity | Proof of who created or sent the data. |
| Non-repudiation | A sender can't credibly deny having created something. |
| Kerckhoffs's principle | A system must stay secure even if everything but the key is public. |
| Cryptographic hash function | A one-way function that maps any input to a fixed-size digest, for example SHA-256. |
| Digest | The fixed-size output of a hash function. |
| Avalanche effect | A tiny input change changes about half the bits of the digest. |
| Collision | Two different inputs that produce the same digest. |
| Salt | A random, per-password value stored with the hash, so equal passwords hash differently. |
| Rainbow table | A precomputed table of hashes of likely passwords. |
| Key-derivation function (KDF) | A function that turns a password or secret into a key or stored hash; password hashing functions are deliberately slow. |
| Argon2id | A memory-hard password hashing function (RFC 9106). |
| Pepper | A secret value added to passwords, kept outside the database. |
| Symmetric encryption | Encryption where one secret key locks and unlocks. |
| AES | Advanced Encryption Standard (FIPS 197): the standard symmetric block cipher. |
| Mode of operation | The method that applies a block cipher to a whole message (ECB, CBC, GCM). |
| AEAD | Authenticated encryption with associated data: encrypts and detects tampering (AES-GCM, ChaCha20-Poly1305). |
| Nonce | A number used once per key; repeating it breaks GCM. |
| Asymmetric encryption | Cryptography with a public key and a private key. |
| Public key / private key | The shareable and the secret half of a key pair. |
| RSA | A public-key algorithm based on factoring large numbers. |
| Elliptic-curve cryptography (ECC) | Public-key cryptography with smaller keys, based on elliptic curves (P-256, X25519, Ed25519). |
| ECDHE | Ephemeral elliptic-curve Diffie-Hellman: key exchange with fresh keys for each connection. |
| Forward secrecy | Past traffic stays secret even if long-term keys leak later. |
| MAC / HMAC | A tag computed from a message and a shared secret key (hash-based MAC). |
| Digital signature | A value made with a private key that anyone can check with the public key. |
| Certificate (X.509) | A signed statement that binds a public key to a name. |
| SAN | Subject Alternative Name: the names a certificate is valid for. |
| Certificate authority (CA) | An organisation trusted to issue certificates. |
| Root store | The list of root CA certificates that a device or browser trusts. |
| Certificate chain | Leaf, intermediate(s) and root, each signing the next. |
| Self-signed certificate | A certificate signed with its own key, with no CA above it. |
| CRL / OCSP | Certificate revocation list and Online Certificate Status Protocol: ways to learn that a certificate was revoked. |
| TLS | Transport Layer Security: the protocol behind HTTPS. |
| KMS / HSM | Key management service and hardware security module: places to keep keys safely. |
| Post-quantum cryptography | Algorithms designed to resist quantum computers (ML-KEM, ML-DSA, SLH-DSA). |
## Check your understanding
1. A colleague says "we encrypt our users' passwords with AES, so a leak is safe." Explain what goes wrong and what you would do instead.
2. Why do two users with the password `hunter2` get different stored values when the system is built correctly? What protects against an attacker who steals the database?
3. You must send a 2 GB backup to a partner you've never met, over the Internet. Describe which kinds of cryptography you'd combine and why.
4. What is the difference between a MAC and a digital signature, and when does it matter?
5. A browser rejects a certificate. List four things it checked that could have failed.
## References
- [FIPS 180-4](https://csrc.nist.gov/pubs/fips/180-4/upd1/final) — Secure Hash Standard (SHA-1, SHA-2)
- [FIPS 202](https://csrc.nist.gov/pubs/fips/202/final) — SHA-3 Standard
- [FIPS 197](https://csrc.nist.gov/pubs/fips/197/final) — Advanced Encryption Standard (AES)
- [NIST SP 800-38D](https://csrc.nist.gov/pubs/sp/800/38/d/final) — Galois/Counter Mode (GCM)
- [RFC 9106](https://www.rfc-editor.org/rfc/rfc9106) — Argon2 memory-hard function
- [RFC 8018](https://www.rfc-editor.org/rfc/rfc8018) — PKCS #5: password-based cryptography (PBKDF2)
- [RFC 7914](https://www.rfc-editor.org/rfc/rfc7914) — The scrypt password-based key derivation function
- [RFC 8439](https://www.rfc-editor.org/rfc/rfc8439) — ChaCha20 and Poly1305 for IETF protocols
- [RFC 2104](https://www.rfc-editor.org/rfc/rfc2104) — HMAC
- [RFC 7748](https://www.rfc-editor.org/rfc/rfc7748) and [RFC 8032](https://www.rfc-editor.org/rfc/rfc8032) — X25519 and Ed25519
- [RFC 5280](https://www.rfc-editor.org/rfc/rfc5280) — X.509 certificate and CRL profile
- [RFC 8446](https://www.rfc-editor.org/rfc/rfc8446) — TLS 1.3
- [RFC 8555](https://www.rfc-editor.org/rfc/rfc8555) — ACME (used by Let's Encrypt)
- [FIPS 203](https://csrc.nist.gov/pubs/fips/203/final), [FIPS 204](https://csrc.nist.gov/pubs/fips/204/final), [FIPS 205](https://csrc.nist.gov/pubs/fips/205/final) — post-quantum standards (ML-KEM, ML-DSA, SLH-DSA), 2024
- [OWASP Password Storage Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html)