How TLS works: the mathematics behind the padlock, from al-Khwarizmi to elliptic curves

Modular arithmetic, Diffie-Hellman, RSA, elliptic curves: the maths of TLS explained with small numbers, and what we owe to the scholars of Baghdad.

Published · 7 min read · Also available in: Français

When a browser shows a padlock, it has just run an algorithm that solves an algebra problem to cipher your data. All three words come from Arabic, and that is no accident: a good share of the ideas TLS rests on was laid down in Baghdad, twelve centuries ago.

This article explains the mathematics of TLS with small numbers you can check by hand, and gives those scholars their due. For the protocol itself, see SSL, TLS and encryption algorithms.

Three words from Baghdad

In the 9th century, the Abbasid caliphs gathered in Baghdad, in the House of Wisdom, scholars who translated, preserved and extended Greek and Indian mathematics. They wrote in Arabic, and came from all over the empire.

A fourth contribution is less well known. Around 850, the Arab philosopher al-Kindi wrote the first known treatise on cryptanalysis. In it he describes frequency analysis: in a message encoded by replacing each letter with another, the most frequent letter of the message almost always stands for the most frequent letter of the language. His manuscript was found again in Istanbul in 1987.

That discovery set the requirements of encryption once and for all: a good algorithm must produce output in which no statistic shows through. This is exactly what is still demanded of AES.

  1. c. 820Al-Khwarizmialgebra, algorithm
  2. c. 850Al-Kindifrequency analysis
  3. 1640Fermatpowers modulo a prime
  4. 1801Gaussmodular arithmetic
  5. 1976-77Diffie, Hellman, RSApublic key
  6. 1985Koblitz, Millerelliptic curves
Twelve centuries of ideas, all at work in a TLS connection.

The basic idea: counting in circles

All the cryptography in TLS rests on modular arithmetic, the arithmetic of clocks. On a 12-hour dial, 9 o’clock plus 8 hours gives 5 o’clock: we write 17 mod 12 = 5. Only the remainder of the division is kept.

TLS does the same thing, on a dial whose number of marks is a prime number several hundred digits long. This detour has a precious property: some calculations become easy one way and impractical the other.

Agreeing on a secret in front of everyone

In 1976, Whitfield Diffie and Martin Hellman showed how two people can build a shared secret while exchanging only public numbers. This is the TLS key exchange.

BrowserServer
  1. Public numbers, known to all: p = 23 and g = 5
  2. Picks a secret: a = 6
  3. Picks a secret: b = 15
  4. Sends 5^6 mod 23 = 8
  5. Sends 5^15 mod 23 = 19
  6. Computes 19^6 mod 23 = 2
  7. Computes 8^15 mod 23 = 2
  8. The same secret on both sides: 2. It never travelled.
The Diffie-Hellman exchange with small numbers. An eavesdropper sees 23, 5, 8 and 19, but not 6 or 15.

Why do both sides find the same number? Because the order of the powers does not matter: (5^6)^15 and (5^15)^6 both equal 5^90. It is an elementary rule of algebra, applied on a dial.

In TLS 1.3, the secrets a and b are drawn at random for every connection, then thrown away. This is what guarantees that a key stolen later cannot be used to read past exchanges.

Elliptic curves: the same idea, with points

Today’s servers no longer do this exchange with numbers, but with the points of an elliptic curve, defined by an equation such as y² = x³ + 2x + 2, also computed on a dial.

On such a curve, two points can be “added”: the result is a third point of the curve. Adding a point G to itself k times gives the point k·G. And the same asymmetry appears:

An example on the curve y² = x³ + 2x + 2 mod 17, starting from the point G = (5, 1):

Browser Server
Secret a = 3 b = 9
Point sent 3·G = (10, 6) 9·G = (7, 6)
Final computation 3·(7, 6) = (13, 7) 9·(10, 6) = (13, 7)

The benefit is practical. The discrete logarithm is much harder on a curve than on numbers: a 256-bit key on a curve protects at least as well as a 3,072-bit RSA key. Computations are faster and certificates smaller. This is what the names X25519 and P-256 refer to.

Proving identity: the signature

The key exchange does not say who you are talking to. That is the job of the certificate, and of the signature the server produces with its private key. The RSA algorithm, published in 1977 by Rivest, Shamir and Adleman, rests on another asymmetry: multiplying two prime numbers is easy, recovering those two numbers from their product is not.

With small numbers:

  1. Pick two prime numbers: 61 and 53. Their product is n = 3233.
  2. The public key is (n = 3233, e = 17). It is in the certificate.
  3. The private key is d = 2753. It is computed from 61 and 53, which the server keeps secret.
  4. To sign the number 65, the server computes 65^2753 mod 3233 = 588.
  5. To verify, anyone computes 588^17 mod 3233 and gets 65 back.

That d exactly undoes what e does follows from a theorem by Fermat, stated in 1640 and generalized by Euler. Three centuries later, it is used billions of times a day.

Security comes from size: recovering 61 and 53 from 3233 takes a second. With an n of 2,048 bits, which is 617 digits, nobody knows how to do it.

Encrypting the data: the algebra of finite fields

Once the secret is shared and the server authenticated, the data is encrypted with AES, a symmetric algorithm designed by the Belgians Joan Daemen and Vincent Rijmen.

AES works on bytes, and treats each one as an element of a finite field with 256 elements: a set in which you can add, multiply and divide without ever leaving it. It is a structure from pure algebra. Among other things, AES computes the inverse of each byte in it: this operation is what scrambles the statistics of the message, and answers, eleven centuries later, the attack described by al-Kindi.

GCM mode, which also guarantees that the data has not been modified, uses a second finite field, with 2¹²⁸ elements.

Where each idea comes into TLS

Moment in the connection Algorithm Mathematics
Key exchange ECDHE (X25519, P-256) Points of an elliptic curve, discrete logarithm
Authentication RSA or ECDSA Modular powers and factoring, or elliptic curve
Building the session keys HKDF, SHA-256 Hash functions
Data encryption AES-GCM Finite fields with 2⁸ and 2¹²⁸ elements

And tomorrow?

In 1994, Peter Shor showed that a powerful enough quantum computer would quickly solve factoring and the discrete logarithm. No such machine exists yet, but today’s exchanges could be recorded and decrypted later.

The countermeasure is already deployed in browsers: ML-KEM, standardized in 2024, adds to the key exchange a problem quantum computers do not know how to solve. It relies on lattices and on polynomials computed on a dial. Algebra again.

For your certificates, the consequence is concrete: algorithms and key sizes will change. An inventory that records the algorithm of every certificate will tell you which ones to replace when the time comes.

Thank you

An algorithm, some algebra, a cipher: every secure connection speaks three Arabic words. Twelve centuries after the House of Wisdom, thank you to al-Khwarizmi, to al-Kindi and to all those who, in Baghdad and elsewhere, translated, preserved and extended this knowledge. The padlock in your browser owes them a great deal.