✖

Digital signature (electronic signature)

An electronic signature proves two things: who wrote the message and that nobody has changed it on the way. It keeps nothing secret, and anyone can read the message. A scanned image of your handwritten signature pasted into a PDF is not an electronic signature: anyone to whom you have ever sent a single signed letter can copy it. A real electronic signature, in mathematical terms a digital signature, is more like a seal: only the owner can stamp it, but anyone can judge whether it is genuine. And unlike a seal, it is calculated afresh for every message, so you cannot move it from one document to another.

How an electronic signature works: RSA in reverse

The basis of the signature is the RSA cipher. There, Jana encrypts a message with Petr’s public key, and only Petr can open it, with his private key. When signing, the keys swap roles. Petr “locks” the message with his private key, which nobody else has. Anyone can unlock it with his public key, and that is how they verify that Petr did the locking.

The private key is a secret number that Petr either generates himself or has a device generate for him. In practice it hides in the chip of a card, in a phone or in a banking app. The public key is its counterpart, a number that Petr can make public without worry.

Why it works is shown in the article on the RSA cipher. Here it is enough to know that Petr can find a pair of numbers e and d such that raising to one power undoes raising to the other, whichever order you do them in.

Why the hash is what gets signed

Real messages are long, and raising an entire contract to a power piece by piece would be slow. So Petr first computes the hash of the message with a hash function, say SHA-256, and signs only that. The hash is short, it is always the same for the same message, and for a slightly different message it is completely different.

A real SHA-256 hash is a number of up to 78 digits, which is too big for our example with tiny numbers. So we reach for the toy from the article on hash functions, except that instead of a hundred we divide by 55: we number the letters by the alphabet, A is 1 up to Z, which is 26, ignore diacritics, add the numbers up and take the remainder after dividing by 55. So the hash comes out smaller than 55.

Example: Petr signs

Petr has the same keys as in the article on RSA: public key n = 55, e = 3, private key d = 27. He wants to send Jana the message “přijdu” (Czech for “I’ll come”). He computes its hash:

16+18+9+10+4+21=78

Fifty-five goes into 78 once, leaving 23. The remainder after division is written with mod: 78mod55=23. The hash of the message is h = 23. Petr raises it to his private key:

s=2327mod55

The number 2327 has 37 digits, but it is enough to take the remainder after each multiplication. First 232 = 529, which leaves remainder 34 after division by 55. Then 34 · 34 = 1156 = 21 · 55 + 1, so 234 leaves remainder 1. That comes in handy, because 27 = 6 · 4 + 3, and so 2327 = (234)6 · 233. Of the six fourth powers, only 1⋅1⋯1=1 remains, so all we need to compute is

233=12,167=221⋅55+12.

The signature is s = 12. Petr sends Jana the message “přijdu” and with it the number 12.

Petr computes the hash h = 23 from the document and the signature 23²⁷ mod 55 = 12 with his private key. He sends Jana the document and the signature s = 12. Jana computes the hash of the received document, which comes out as 23, and 12³ mod 55 = 23 with the public key. The two numbers match, a green tick Petr computes the hash h = 23 from the document and the signature 23²⁷ mod 55 = 12 with his private key. He sends Jana the document and the signature s = 12. Jana computes the hash of the received document, which comes out as 23, and 12³ mod 55 = 23 with the public key. The two numbers match, a green tick

Now it is Jana’s turn. She computes the hash of the message that arrived herself and gets 23. Then she raises the signature to the public e = 3:

123=1728=31⋅55+23

The signature has returned the very same hash that the message has. The signature is valid: the message was signed by the holder of the private key, and nobody changed it on the way.

What if someone changes the message

Eva intercepts the message and rewrites it to “nepřijdu” (“I won’t come”). She leaves the signature 12, since she cannot make another. Jana computes the hash:

14+5+16+18+9+10+4+21=97,

which leaves 42 after division by 55. But the signature still gives 23, and 42 ≠ 23. The signature is not valid, and Jana knows someone has tampered with the message.

A document with a line rewritten in red has hash h = 42. The signature s = 12 gives 12³ mod 55 = 23 with the public key. 42 does not equal 23, a red cross A document with a line rewritten in red has hash h = 42. The signature s = 12 gives 12³ mod 55 = 23 with the public key. 42 does not equal 23, a red cross

The same thing stops a signature from being moved to another message: the signature 12 belongs only to the hash 23.

With such small numbers, though, Eva could cheat, in two ways. First, she can try all the signatures. There are only 55 of them, and 48 succeeds, because 483mod55=42. She attaches 48 under “nepřijdu” and Jana notices nothing. Real RSA therefore has an n with more than six hundred digits, and nobody can try that many possibilities.

The second route goes through a collision, that is, two different messages with the same hash. Our toy has plenty of them: “nepůjdu dnes” (“I won’t go today”) has the same hash 23 as “přijdu”, so Petr’s signature 12 would pass under it. That is why what gets signed is the hash from a good hash function, one for which nobody can find a collision.

Signatures on elliptic curves

RSA signatures are long, hundreds of digits. A more modern and shorter variant is ECDSA, a signature on elliptic curves. It calculates differently from RSA, but does the same thing with much smaller numbers. On top of that, with every signature it must draw a new random number. For the PlayStation 3 games console, Sony used the same one every time, and at the end of 2010 the hacker group fail0verflow worked out its private key from that.

You meet ECDSA more often than you might think. Most Bitcoin payments are signed with it, you will find it in website certificates, and it mostly stands behind passkeys, which replace passwords: the website remembers only your public key, and at login it sends you a random number that your phone signs. There is simply no password to steal.

A website certificate: an ID card for a key

Jana verifies the signature with Petr’s public key. But how does she know that the key really belongs to Petr? If Eva slipped her a public key of her own with Petr’s name on it, she would be signing in his place. The same problem was left unresolved in the article on Diffie–Hellman: Adam does not know whether he is agreeing a key with Bára or with Eva.

The way out is a certificate, a sort of “ID card” for a key. It contains a name, for example a website address, its public key and a validity period, and the whole thing is signed by a company that everyone trusts, a certificate authority. The browser then verifies its signature with its public key. But where did it get that from? From another certificate, signed by someone higher up. The chain ends at root certificates, which signed themselves and which the browser or operating system received bundled in by the manufacturer.

Three certificates one below another. The blue padlock on each is the public key, the red wavy line the signature and the red key on the right the private key that signs. The circle with a star is just the mark of an authority’s certificate, the globe stands for a website. The top one, the root, sits in a browser window and signed itself. The red key of the top certificate signs the middle one, and the key of the middle one signs the bottom certificate of the website with a green padlock Three certificates one below another. The blue padlock on each is the public key, the red wavy line the signature and the red key on the right the private key that signs. The circle with a star is just the mark of an authority’s certificate, the globe stands for a website. The top one, the root, sits in a browser window and signed itself. The red key of the top certificate signs the middle one, and the key of the middle one signs the bottom certificate of the website with a green padlock

In the picture, the root certificate is at the top, in the browser window. Its private key signed the middle one, the intermediate certificate of the authority, and its key in turn signed the website’s certificate at the bottom. Click the icon to the left of the address in your browser and view the certificate. You will see exactly such a chain, usually of three links. If any signature in the chain did not match, or the certificate did not belong to the address you typed, the browser will not open the page and will show a warning. Better not click past such a warning.

The whole construction rests on the authority never issuing a certificate for a website to anyone other than its owner. When an authority fails, things go badly. In the summer of 2011 an attacker broke into the Dutch authority DigiNotar and issued himself over 500 fake certificates, including ones for Google addresses. The fake certificates were then used to spy on about 300,000 Gmail users in Iran. At the end of August the browser makers stopped trusting DigiNotar, and on 20 September 2011 the company went bankrupt.

Signatures for the quantum era

A quantum computer would break RSA and ECDSA with Shor’s algorithm and would sign on behalf of anyone. So in August 2024 the US standards body NIST issued two new signature standards meant to withstand quantum computers: ML-DSA, built on lattices, and SLH-DSA, built only on hash functions. More about them in the article on post-quantum cryptography.

Electronic signatures in law

The mathematics is the same everywhere, but the law distinguishes levels. The European eIDAS Regulation, which has applied since 1 July 2016, recognises three kinds of electronic signature:

  • A simple electronic signature is almost anything: a name under an email, a ticked box, even that scanned image.
  • An advanced electronic signature is uniquely linked to the signatory, is created with a key that only they control, and reveals any later change to the document. That is exactly what the signature in this article does.
  • A qualified electronic signature is an advanced signature with a qualified certificate from an authority supervised by the state, created on a secure device such as a smart card. Under Article 25 of eIDAS, only this one has the same legal effect as a handwritten signature.

That is the law of the European Union. Other countries, including the United Kingdom, have rules of their own on electronic signatures and on identifying yourself online, so what counts as a valid signature depends on where you are.

A signature is often confused with logging in. Logging in proves to a single website, at that moment, who you are. The signature stays with the document, and anyone can verify it even years later.

When you click “sign” in online banking, the hash of the document is computed and signed by a key that the provider keeps safe for you. You only unlock it by logging in.

Signature calculator

For p and q enter two different primes, and for e the public exponent. The hash of the message being signed and of the received message must be a whole number smaller than n = p · q. The calculator works out the private key, the signature and the result of the verification. When you open the page, you will find an example with Petr and Jana in it. Change the hash of the received message to 42, as after Eva’s interference, and the signature stops being valid.

See also