1. Substitution
  2. Enigma
  3. Shannon
  4. DES & AES
  5. Modes
  6. Hashes
  7. Passwords
  8. RSA & DH
  9. ECC
  10. Post-quantum

Chapter 08 · Public-key cryptography

Secrets in public: Diffie–Hellman and RSA

For four thousand years, two people who wanted to communicate secretly had to share a key first. In 1976 Whitfield Diffie and Martin Hellman showed how to agree on one in public, and a year later Rivest, Shamir and Adleman built a cipher whose encryption key can be printed in a newspaper. Both rest on number theory that Fermat and Euler knew in the eighteenth century.

Every cipher so far is symmetric: the same key encrypts and decrypts, so the two sides must share it before they can say anything secret. Armies distributed codebooks by courier; banks had key officers carrying sealed envelopes. With n people who all want to talk privately, n(n−1)/2 keys are needed. On the Internet, where you want to talk to a shop you have never heard of, it is impossible. The way out came in three steps, between 1974 and 1977, and it changed what cryptography is.

Merkle's puzzles

In 1974 Ralph Merkle, an undergraduate at Berkeley, proposed a project to agree on a key in public; his professor rejected it. His idea: Bob sends Alice a million puzzles, each a short message encrypted with a deliberately weak key that takes a minute to break. Each puzzle contains a random identifier and a secret key. Alice picks one puzzle at random, breaks it, and announces its identifier; both now share its key. An eavesdropper who does not know which puzzle Alice chose must break, on average, half a million of them. The advantage is only quadratic (a minute for Alice, a year for the eavesdropper), but it was the first proof that a shared secret could be built in public. The paper appeared, after years of rejections, in 1978.

Diffie–Hellman

In November 1976 Whitfield Diffie and Martin Hellman published New Directions in Cryptography, which begins: “We stand today on the brink of a revolution in cryptography.” Their key agreement uses arithmetic modulo a prime.

Definition (modular arithmetic)

Two integers are congruent modulo n, a≡b(modn), if n divides a−b. The remainders 0,1,…,n−1 with addition and multiplication modulo n form the ring ℤn. If p is prime, every non-zero element has a multiplicative inverse, and the non-zero elements form a group ℤp∗ that is cyclic: some generator g has powers g1,g2,…,gp−1 that run through all of them.

Alice and Bob agree in public on a large prime p and a generator g. Alice chooses a secret a and sends A=gamodp; Bob chooses a secret b and sends B=gbmodp. Then

Ba=(gb)a=gab=(ga)b=Ab(modp),

and both hold s=gabmodp. An eavesdropper sees p, g, A and B. To get s she seems to need a from A=ga: the discrete logarithm. Exponentiation modulo p is fast (see square-and-multiply below); its inverse, for well-chosen 2048-bit primes, is out of reach of every known classical algorithm.

Diffie–Hellman with small numbers, next to the usual paint analogy. The common colour and g, p are public; each side mixes in a secret colour (a secret exponent) and sends the mixture; each mixes its own secret into what it received; both arrive at the same colour (the same number), while the eavesdropper, who only saw the mixtures, would have to “unmix” them: compute a discrete logarithm.

Diffie–Hellman protects against eavesdroppers, not against an active man in the middle who agrees one key with Alice and another with Bob. To defeat her, the public values must be authenticated, which is the job of signatures. With that addition (and with elliptic curves, chapter 09), Diffie–Hellman is how every TLS 1.3 connection starts. Diffie and Hellman received the Turing Award for it in 2015.

Discovered in secret, first

In 1997 the British signals agency GCHQ revealed that its own mathematicians had got there first. James Ellis had shown in 1970 that “non-secret encryption” was possible in principle; in 1973 Clifford Cocks, a 22-year-old recent recruit, found a practical scheme in an evening, essentially RSA; in 1974 Malcolm Williamson found what amounts to Diffie–Hellman. Classified, none of it was used or published, and the world learned public-key cryptography from the academics.

The number theory behind RSA

Theorem (Fermat's little theorem, 1640)

If p is prime and p∤a, then ap−1≡1(modp).

Proof

The numbers a,2a,…,(p−1)a are non-zero and pairwise different modulo p (if ia≡ja then p∣(i−j)a, so i=j), so they are 1,2,…,p−1 in some order. Multiplying them all: ap−1(p−1)!≡(p−1)!(modp), and (p−1)! is invertible modulo p.

Theorem (Euler, 1763)

Let φ(n) be the number of integers in 1,…,n coprime with n. If gcd⁡(a,n)=1, then aφ(n)≡1(modn). For n=pq with distinct primes p,q: φ(n)=(p−1)(q−1).

The proof is the same as Fermat's, applied to the φ(n) invertible residues; Euler's theorem is Lagrange's theorem for the group ℤn∗.

RSA

In 1977 three researchers at MIT, Ron Rivest, Adi Shamir and Leonard Adleman, spent a year trying to build a public-key cipher (Rivest and Shamir proposing, Adleman breaking) until, after a Passover evening in April, Rivest wrote down the scheme:

  1. Choose two large random primes p and q, and let n=pq and φ(n)=(p−1)(q−1).
  2. Choose e coprime with φ(n) (today almost always e=65537) and compute d=e−1modφ(n) with the extended Euclidean algorithm.
  3. The public key is (n,e); the private key is d (and p, q).
  4. Encrypt a number 0≤m<n as c=memodn; decrypt as m=cdmodn.
Theorem (correctness of RSA)

For every m∈{0,…,n−1}: (me)d≡m(modn).

Proof

Since ed≡1(modφ(n)), write ed=1+t(p−1)(q−1). Modulo p: if p∤m, Fermat gives med=m⋅(mp−1)t(q−1)≡m; if p∣m, both sides are 0. The same holds modulo q. So p and q both divide med−m, and since they are distinct primes, so does n=pq (this is the Chinese remainder theorem: a residue modulo pq is determined by its residues modulo p and modulo q).

Anyone who can factor n computes φ(n) and then d, so RSA is at most as hard as factoring. That August, Martin Gardner's column in Scientific American described RSA and printed a challenge: a message encrypted with a 129-digit modulus, RSA-129, with a $100 prize. The authors estimated it would take 40 quadrillion years. It was factored in 1994 by 600 volunteers coordinated over the Internet; the message read “THE MAGIC WORDS ARE SQUEAMISH OSSIFRAGE”.

Square-and-multiply

Computing md with d of 3,000 bits by repeated multiplication would take 23000 steps. Instead, read the exponent in binary and, for each bit, square the running result and multiply by m if the bit is 1: about log2⁡d squarings and at most as many multiplications, all modulo n. This algorithm, known in India over two thousand years ago for computing powers, is what makes RSA (and Diffie–Hellman) practical.

Toy RSA from start to finish. Pick two primes and e; the figure computes n, φ(n) and d, encrypts each character of your message as a number, and decrypts it back. The note under the table counts the steps of square-and-multiply. Then press the button to break it: with such small primes, trial division factors n instantly and reveals d. Real keys use primes of 1,536 bits or more.

Textbook RSA is not secure

The scheme above, “textbook RSA”, is deterministic (the same m always gives the same c, so an attacker can test guesses) and malleable (c⋅2e decrypts to 2m). Real RSA encrypts a randomly padded message. The old padding of PKCS#1 v1.5 fell to Daniel Bleichenbacher's attack in 1998, a padding oracle like Vaudenay's (chapter 05), which keeps resurfacing in TLS implementations (ROBOT, 2017). OAEP, by Mihir Bellare and Phillip Rogaway (1994), adds randomness through a Feistel-like mixing with a hash, and is provably secure under reasonable assumptions; it is what the desktop app's RSA-OAEP uses, with SHA-256 and 3,072-bit keys. RSA encrypts at most a few hundred bytes, so in practice it encrypts a symmetric key: hybrid encryption.

How big must the key be?

Factoring has improved steadily: the quadratic sieve (Carl Pomerance, 1981) and the general number field sieve (1990s), whose running time is about

exp⁡((649)1/3(ln⁡n)1/3(ln⁡ln⁡n)2/3),

sub-exponential in the number of digits. RSA-768 (232 digits) was factored in 2009; RSA-250 (250 digits, 829 bits) in 2020, with about 2,700 core-years. A 2,048-bit modulus gives about 112 bits of security, 3,072 bits about 128. A large quantum computer running Shor's algorithm would factor in polynomial time and break RSA and Diffie–Hellman at any size; Math of Quantum explains how, and chapter 10 what replaces them.

Digital signatures

Run RSA backwards and you get a signature: the owner of d computes s=hdmodn for the hash h of a message, and anyone can check that se≡h(modn) with the public key. Only the holder of d could have produced s, so a signature gives authenticity, integrity and non-repudiation. As with encryption, the hash must be padded: RSA-PSS (Bellare and Rogaway, 1996) randomises it and has a tight security proof; it is one of the desktop app's signature algorithms.

Signatures make the rest of public-key cryptography usable. A certificate is a public key signed by a certificate authority; your browser trusts a few dozen authorities, and they vouch for the keys of millions of websites. When TLS starts, the server's certificate authenticates its Diffie–Hellman share, which defeats the man in the middle. Software updates, passports, e-mail (S/MIME, PGP), Git commits and cryptocurrencies all rest on signatures. The next chapter makes them, and Diffie–Hellman, much smaller.

Further reading

  1. Whitfield Diffie and Martin Hellman, “New Directions in Cryptography”, IEEE Transactions on Information Theory 22(6), 1976.
  2. Ron Rivest, Adi Shamir, Leonard Adleman, “A Method for Obtaining Digital Signatures and Public-Key Cryptosystems”, Communications of the ACM 21(2), 1978.
  3. Ralph Merkle, “Secure Communications over Insecure Channels”, Communications of the ACM 21(4), 1978.
  4. James Ellis, “The History of Non-Secret Encryption” (written 1987, published by GCHQ in 1997).
  5. Dan Boneh, “Twenty Years of Attacks on the RSA Cryptosystem”, Notices of the AMS 46(2), 1999.