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 and undo each other mod in either order: , the same fact that makes RSA decryption work.
| Key used | Who can do it | |
|---|---|---|
| Encrypt a message for Bob | Bob’s public key | anyone |
| Decrypt it | Bob’s private key | only Bob |
| Sign a message as Alice | Alice’s private key | only Alice |
| Verify Alice’s signature | Alice’s public key | anyone |
Example¶
Alice’s keys come from and , so and . She uses , so (check: ). Her public key is and her private key is .
Alice signs the message :
She sends the pair . Bob, or anyone, checks it with her public key: , which matches the message, so the signature is valid.
public_key, private_key = rsa_keygen(61, 53, 17)
print("public key:", public_key, " private key:", private_key)
m = 1234
s = rsa(m, private_key) # sign with the PRIVATE key
print("signature:", s)
print("verify:", rsa(s, public_key), "should equal", m)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 , which doesn’t match 1235, so he rejects it.
Forgery. Mallory makes up a signature, say 2000, for the message 1234. Bob computes , not 1234, so he rejects it.
print("Bob computes", rsa(1512, public_key), "but the message says 1235")
print("Bob computes", rsa(2000, public_key), "but the message says 1234")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 with , and the only practical way to find it is to compute , which needs . Finding from the public key means finding , which means factoring . That’s exactly the security argument for RSA encryption: multiplying and is easy, and factoring is hard. (KidRSA signatures would be worthless, because anyone can compute 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 signs 2 and signs 3, then signs , because .
With Alice’s key, the signature on 2 is 1027 and the signature on 3 is 1857. Their product mod 3233 is 2902, and . So Mallory has a valid signature on the message 6, which Alice never signed, and she never needed .
s2 = rsa(2, private_key)
s3 = rsa(3, private_key)
forged = (s2 * s3) % 3233
print(s2, s3, forged)
print("verify the forged signature:", rsa(forged, public_key))1027 1857 2902
verify the forged signature: 6
A Second Problem: Long Messages¶
RSA can only sign numbers smaller than . 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:
import hashlib
def sha256(message):
return hashlib.sha256(message.encode()).hexdigest()
h1 = sha256("SEE YOU AT TWO")
h2 = sha256("SEE YOU AT TEN")
print(h1)
print(h2)
different_bits = bin(int(h1, 16) ^ int(h2, 16)).count("1")
print(different_bits, "of the 256 bits are 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: , and Alice’s signature is . If Mallory changes the message to SEE YOU AT TEN, Bob’s hash is 2895, which doesn’t match, so he rejects it.
def hash_mod(message, n):
return int(sha256(message), 16) % n
h = hash_mod("SEE YOU AT TWO", 3233)
s = rsa(h, private_key)
print("hash:", h, " signature:", s)
# Bob checks a message that arrives with the signature 1312
for received in ["SEE YOU AT TWO", "SEE YOU AT TEN"]:
valid = rsa(s, public_key) == hash_mod(received, 3233)
print(received, "valid" if valid else "rejected")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) , 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 with his long-term RSA private key, and Alice checks the signature with Bob’s RSA public key. If Mallory swaps for her own , she can’t produce Bob’s signature on , 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.