Notes
The problem
Here is the problem in full. AES needs the same key on both sides. If you send that key over the network, anyone sniffing copies it. Meeting in person does not scale to the internet. And the numbers get large: one thousand people who each want a private channel with every other need almost half a million shared keys. In the nineteen seventies, cryptographers found a way out, and it changed how security works.
- AES needs the same key on both sides
- send the key over the network? a sniffer copies it
- meet in person first? does not scale to the internet
- 1,000 people talking in pairs need 499,500 shared keys
- solved in the 1970s with public key cryptography
The key pair
You generate two keys together, linked by mathematics. The public key can be given to anyone. The private key never leaves its owner. Whatever is encrypted with the public key can only be decrypted with the private key, and knowing the public key does not let you work out the private one. So the receiver publishes a public key, the sender encrypts with it, and only the receiver can read the result. A thousand people now need only a thousand key pairs.
- generated together, linked by mathematics
- public key: give to anyone, put it on a website
- private key: never shared, never leaves its owner
- knowing the public key does not reveal the private key
- 1,000 people now need only 1,000 key pairs
Two uses
A key pair has three uses. Encryption: anyone encrypts with your public key and only you decrypt. Signing, which runs the other way: you sign with your private key and anyone verifies with your public key, which proves the message came from you. And key exchange, where two sides combine their keys to agree on a shared secret. In modern systems, signing and key exchange are the common uses, and direct public key encryption is rare.
- 1. Encrypt to someone public key encrypts, private key decrypts
- 2. Sign as someone private key signs, public key verifies
- 3. Agree on a shared key (key exchange, episode 6)
- signing and key exchange are the common uses today
RSA
RSA is the classic algorithm, from nineteen seventy seven, and it is still everywhere. Its security rests on a simple fact: multiplying two large prime numbers is easy, but splitting the product back into them is not. Keys must be at least two thousand and forty eight bits; one thousand and twenty four is no longer safe. RSA must always be used with a padding scheme: OAEP for encryption and PSS for signatures. Raw RSA without padding is broken.
- from 1977; still everywhere
- based on: multiplying two big primes is easy, splitting the product is not key sizes: 2048 bits minimum, 3072 or 4096 for long life
- 1024 bits: no longer safe
- always use padding: OAEP to encrypt, PSS to sign
- slow to generate, big keys
Elliptic curves
Elliptic curve cryptography is the modern choice. It rests on a different mathematical problem that is harder for the same size, so a two hundred and fifty six bit elliptic curve key is about as strong as a three thousand bit RSA key. The curves to know are P two fifty six, the widely supported standard, and Curve two five five one nine, used as X two five five one nine for key exchange and E D two five five one nine for signing. Keys are smaller and operations faster.
- based on a harder problem on elliptic curves
- 256-bit ECC is about as strong as 3072-bit RSA
- curves to use: P-256 (NIST, widely used) Curve25519: X25519 for key exchange, Ed25519 to sign
- smaller keys, faster, less to send on the wire
Real numbers
Here are real numbers from one machine. Generating an RSA two thousand and forty eight key pair took zero point one three seconds, and a four thousand and ninety six bit pair took zero point four four. A P two fifty six key pair took four hundredths of a millisecond, three thousand times faster. Public keys: two hundred and ninety four bytes for RSA, sixty five for P two fifty six, and just thirty two for E D two five five one nine. This is why new systems choose elliptic curves.
generate a key pair RSA-2048 0.131 s RSA-4096 0.444 s P-256 0.00004 s public key size RSA-2048 294 bytes P-256 65 bytes Ed25519 32 bytes # RSA-2048: decrypt 0.36 ms
The size limit
There is a practical limit that surprises people. With a two thousand and forty eight bit key and OAEP padding, we encrypted one hundred and ninety bytes and got a two hundred and fifty six byte ciphertext. At one hundred and ninety one bytes the library refused. RSA cannot encrypt a file or a database record. It is also slow compared with AES. So nobody encrypts real data directly with a public key. They do something smarter.
ct = pub.encrypt(b"x" * 190, oaep) len(ct) # 256 bytes pub.encrypt(b"x" * 191, oaep) # ValueError: Encryption failed # the limit is 190 bytes, and it # is slow compared with AES # so nobody encrypts data # directly with RSA
Hybrid encryption
The answer is hybrid encryption, and almost every real system uses it. Generate a random AES key. Encrypt the data with AES in GCM mode, which is fast and handles any size. Then use the public key method only to protect that small AES key. Send both together. TLS, PGP, SSH and cloud envelope encryption all work this way. You get the easy key sharing of asymmetric cryptography and the full speed of symmetric encryption.
- 1. make a random AES key
- 2. encrypt the data with AES-GCM (fast, any size)
- 3. protect the AES key with the public key method
- 4. send both together
- used by: TLS, PGP, age, SSH, KMS envelope encryption
- best of both: easy sharing and full speed
How attackers win
Attackers almost never break the mathematics. They take the private key. Keys are committed to git repositories, left unencrypted in home folders and backups, baked into container images and stored in pipeline variables. Occasionally a device generates keys with weak random numbers, and the keys can be guessed. Whoever holds the private key is the owner as far as any system can tell: they can decrypt, sign and log in, and nothing looks wrong.
- keys committed to git repos
- unencrypted keys in home folders and backups
- keys baked into container images and CI variables
- weak random numbers at key generation
- a stolen key signs and decrypts as the owner, and nothing looks wrong
Protect the private key
So protect the private key above all. Generate it where it will be used instead of copying it around. Put a passphrase on key files. Better, keep it in hardware: a hardware security module, a TPM, a security key or a cloud key management service, where the key can be used but never exported. Run secret scanning on every commit. Prefer short-lived certificates to long-lived keys. And have a tested way to revoke a key when it leaks.
- generate keys where they will be used; do not copy them
- passphrase on key files
- hardware: HSM, TPM, security key, cloud KMS (the key cannot be exported)
- secret scanning on every commit
- short-lived certificates instead of long-lived keys
- a tested way to revoke
Common mistakes
Mistakes. RSA keys shorter than two thousand and forty eight bits. RSA used without OAEP or PSS padding. Trying to encrypt bulk data directly with RSA. Private keys in a repository or a container image. One key pair used for encryption, signing and login together. No plan to rotate or revoke keys. And the most dangerous: inventing your own scheme instead of using a vetted library and a standard protocol.
- RSA keys under 2048 bits
- RSA without OAEP or PSS
- encrypting bulk data with RSA
- private keys in a repo or an image
- one key pair for every purpose
- no plan to rotate or revoke
- inventing your own scheme
Say it like this
In the interview: symmetric encryption is fast but both sides need the same key. Asymmetric solves key sharing and proves identity, but it is slow and limited in size. So real systems are hybrid: asymmetric cryptography to agree a key and to sign, and AES in GCM mode for the data. I prefer elliptic curves for new systems because keys are smaller and faster, and I keep private keys in hardware or a key management service.
- "Symmetric is fast but needs
- a shared key. Asymmetric
- solves sharing and identity
- but is slow and size-limited.
- So real systems are hybrid: asymmetric to agree a key and sign, AES-GCM for the data.
- I prefer elliptic curves and keep private keys in hardware."