Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Digital Signatures and Hashing

Public key cryptography solves two different problems.

  • Secrecy: nobody but the receiver can read the message. RSA encryption and ElGamal do this.

  • Authenticity: the receiver can be sure who sent the message, and that nobody changed it on the way.

Mallory’s man-in-the-middle attack was entirely a failure of authenticity: she never broke any of the math that kept messages secret. A digital signature solves the authenticity problem, and it turns out to be RSA run backwards.

What a Signature Must Do

A handwritten signature looks the same on every document, so a digital copy of one could be cut and pasted onto anything. A digital signature has to depend on the exact message it signs. It should provide:

  • Authenticity: only the signer could have made it.

  • Integrity: if the message changes by even one bit, the signature no longer checks out.

  • Non-repudiation: the signer can’t later claim they didn’t sign it, because nobody else has their private key.

And anyone with the signer’s public key should be able to check the signature, without being trusted with any secret.

The Idea: Run RSA Backwards

To encrypt with RSA, you use the receiver’s public key, so only their private key can undo it. To sign, Alice flips the roles: she uses her own private key, which only she has, and anyone can undo it with her public key. If undoing it gives back the message, only Alice could have produced it.

This works because dd and ee undo each other mod nn in either order: (md)e=mde=(me)d\left(m^d\right)^e = m^{de} = \left(m^e\right)^d, the same fact that makes RSA decryption work.

Key usedWho can do it
Encrypt a message for BobBob’s public keyanyone
Decrypt itBob’s private keyonly Bob
Sign a message as AliceAlice’s private keyonly Alice
Verify Alice’s signatureAlice’s public keyanyone

Example

Alice’s keys come from p=61p = 61 and q=53q = 53, so n=3233n = 3233 and φ(n)=60⋅52=3120\varphi(n) = 60 \cdot 52 = 3120. She uses e=17e = 17, so d=2753d = 2753 (check: 17⋅2753=46,801=15⋅3120+117 \cdot 2753 = 46,801 = 15 \cdot 3120 + 1). Her public key is (17,3233)(17, 3233) and her private key is (2753,3233)(2753, 3233).

Alice signs the message m=1234m = 1234:

s=12342753(mod3233)=1512s = 1234^{2753} \pmod{3233} = 1512

She sends the pair (1234,1512)(1234, 1512). Bob, or anyone, checks it with her public key: 151217(mod3233)=12341512^{17} \pmod{3233} = 1234, which matches the message, so the signature is valid.

public key: [17, 3233]  private key: [2753, 3233]
signature: 1512
verify: 1234 should equal 1234

Notice that the message travels in the open. A signature proves who sent a message, but it doesn’t hide it. If Alice also wants secrecy, she has to encrypt separately (see below).

When Verification Fails

There are two ways a signature can fail to check out, and both are the point.

  • Tampering. Mallory changes the message to 1235 but keeps Alice’s signature 1512. Bob computes 151217(mod3233)=12341512^{17} \pmod{3233} = 1234, which doesn’t match 1235, so he rejects it.

  • Forgery. Mallory makes up a signature, say 2000, for the message 1234. Bob computes 200017(mod3233)=26982000^{17} \pmod{3233} = 2698, not 1234, so he rejects it.

Bob computes 1234 but the message says 1235
Bob computes 2698 but the message says 1234

To make a signature that checks out, Mallory needs a number ss with s17≡1234(mod3233)s^{17} \equiv 1234 \pmod{3233}, and the only practical way to find it is to compute 1234d(mod3233)1234^d \pmod{3233}, which needs dd. Finding dd from the public key means finding φ(n)\varphi(n), which means factoring nn. That’s exactly the security argument for RSA encryption: multiplying pp and qq is easy, and factoring nn is hard. (KidRSA signatures would be worthless, because anyone can compute dd from KidRSA’s public key with the Extended Euclidean Algorithm.)

A Problem: Signatures Multiply

Signing a message directly has a subtle weakness. RSA signatures multiply: if s2s_2 signs 2 and s3s_3 signs 3, then s2⋅s3s_2 \cdot s_3 signs 2⋅3=62 \cdot 3 = 6, because 2d⋅3d=6d2^d \cdot 3^d = 6^d.

With Alice’s key, the signature on 2 is 1027 and the signature on 3 is 1857. Their product mod 3233 is 2902, and 290217(mod3233)=62902^{17} \pmod{3233} = 6. So Mallory has a valid signature on the message 6, which Alice never signed, and she never needed dd.

1027 1857 2902
verify the forged signature: 6

A Second Problem: Long Messages

RSA can only sign numbers smaller than nn. A long message would have to be split into blocks and each block signed separately. That’s slow, since every signature is a big modular exponentiation, and an attacker could reorder or drop blocks.

The fix for both problems is to sign a short fingerprint of the whole message instead of the message itself. That fingerprint comes from a hash function.

Hash Functions: A Message’s Fingerprint

The most common one, SHA-256, always gives 256 bits, usually written as 64 hexadecimal digits. A good hash function has three properties:

  • The same message always gives the same hash.

  • Changing anything in the message, even one letter, changes about half of the bits of the hash.

  • Nobody can find two different messages with the same hash, or a message with a given hash.

Python’s hashlib module computes SHA-256. The messages SEE YOU AT TWO and SEE YOU AT TEN differ in only two letters, but their hashes look completely different:

8bfcc58d1bea66053984761d0d885718e5d7cf796a4c3330eb4e4218b2b3252b
8cdd89419a0fdbe2efdc2f4cb6c3b2644d266bf8a76f20eba4edaef2e844dc19
135 of the 256 bits are different

The last line uses XOR to find the bits where the two hashes differ, and counts them.

Hash functions are useful on their own, too. Websites should store a hash of each password (using a special, deliberately slow hash function) instead of the password itself, and software downloads often list a SHA-256 hash so you can check that the file you got is the file they published.

Hash, Then Sign

Here’s how real signatures work:

Our toy key is much smaller than a 256-bit hash, so for this example we reduce the hash mod 3233 to make it fit: H(SEE YOU AT TWO) mod 3233=165H(\texttt{SEE YOU AT TWO}) \bmod 3233 = 165, and Alice’s signature is 1652753(mod3233)=1312165^{2753} \pmod{3233} = 1312. If Mallory changes the message to SEE YOU AT TEN, Bob’s hash is 2895, which doesn’t match, so he rejects it.

hash: 165  signature: 1312
SEE YOU AT TWO valid
SEE YOU AT TEN rejected

Real systems use the full 256-bit hash with a 2048-bit (or larger) nn, so there’s no reduction step. They also pad the hash before signing, either in a fixed format (PKCS #1 v1.5) or with random padding (PSS), which stops the multiplying trick above. That’s covered briefly in RSA in Practice.

Signing and Encrypting Together

A signature doesn’t hide anything, so when both secrecy and authenticity are needed, do both, with different keys. Alice signs with her private key (so Bob knows it’s from her), then encrypts with Bob’s public key (so only Bob can read it). Bob decrypts with his private key, then verifies with Alice’s public key. That’s four keys in play, two for each person. In practice the encryption is done with a fast symmetric cipher like AES, using a key agreed with Diffie–Hellman, and signatures prove who is who. That combination is what HTTPS does.

Back to Mallory

Signatures fix the man-in-the-middle attack on Diffie–Hellman. Bob signs his Diffie–Hellman public value BB with his long-term RSA private key, and Alice checks the signature with Bob’s RSA public key. If Mallory swaps BB for her own MM, she can’t produce Bob’s signature on MM, so Alice rejects it.

There’s a catch, though: Alice needs Bob’s RSA public key in advance, and she has to trust that it really is his. If Mallory could swap that too, we’d be back where we started. That’s the problem certificates solve.

A certificate is a small document that says “this public key belongs to this name,” signed by a certificate authority (CA) that everyone already trusts. Browsers and operating systems come with the public keys of a few dozen trusted root CAs built in. When you visit a website, it sends its certificate. Your browser checks the CA’s signature and checks that the name matches the site you asked for. If Mallory sends her own key, she can’t get a trusted CA to sign “this is bank.com,” so your browser shows a warning.

Signatures Are Everywhere

Signatures are used even more than encryption:

  • Every HTTPS connection checks at least one certificate signature.

  • Operating system and app updates are signed by the company that makes them, and your device refuses an update whose signature doesn’t verify.

  • Electronic passports contain chip data signed by the country that issued them, so border systems can detect altered data.

  • Developers sign software releases and code changes.

Many of these now use elliptic curve signatures, such as ECDSA and Ed25519, instead of RSA, because the keys are much smaller. That’s the subject of the last section of this chapter, Elliptic Curve Cryptography.