RSA Signatures
Run RSA backwards and you get a signature: only the private-key holder can compute , and anyone can check . As with encryption, the raw version is broken; real signatures sign a hash with careful padding (PKCS #1 v1.5 or PSS).
Textbook signatures and why they fail
Sign: . Verify: accept if .
Two forgeries need no private key:
- Existential forgery: pick any , compute . Then is a valid pair, for a random-looking .
- Multiplicative forgery: from signatures on and , is a valid signature on .
from sympy import nextprime
from math import lcm
p, q = nextprime(2**256 + 1111), nextprime(2**256 + 2**190 + 2222)
n, e = p * q, 65537
d = pow(e, -1, lcm(p - 1, q - 1))
sign = lambda m: pow(m, d, n)
verify = lambda m, s: pow(s, e, n) == m % n
s = 123456789 # forgery 1: choose the signature first
print("existential forgery verifies:", verify(pow(s, e, n), s))
m1, m2 = 1000, 2023
s12 = sign(m1) * sign(m2) % n # forgery 2: combine two signatures
print("product signature verifies on m1*m2:", verify(m1 * m2, s12))existential forgery verifies: True
product signature verifies on m1*m2: True
Hash, then sign
Signing instead of fixes both problems, if is a good cryptographic hash: a forger would need a message whose hash equals the chosen , or messages whose hashes multiply correctly, and neither can be found. Hashing also lets you sign gigabytes with one exponentiation.
The Full Domain Hash (FDH) variant hashes onto all of ; it is provably secure in the random-oracle model. The toy below stretches SHA-256 to the modulus length:
import hashlib
from sympy import nextprime
from math import lcm
p, q = nextprime(2**256 + 1111), nextprime(2**256 + 2**190 + 2222)
n, e = p * q, 65537
d = pow(e, -1, lcm(p - 1, q - 1))
k = (n.bit_length() + 7) // 8
def fdh(msg):
out = b"".join(hashlib.sha256(msg + bytes([i])).digest() for i in range((k + 31) // 32))
return int.from_bytes(out[:k], "big") % n
def sign(msg):
return pow(fdh(msg), d, n)
def verify(msg, s):
return pow(s, e, n) == fdh(msg)
sig = sign(b"pay Bob 10 EUR")
print(hex(sig)[:34], "...")
print(verify(b"pay Bob 10 EUR", sig), verify(b"pay Bob 1000 EUR", sig))0x816ca8566c95978dbf91b68c9d536442 ...
True False
PKCS #1 v1.5 and PSS
- PKCS #1 v1.5 signatures encode
00 01 FF FF ... FF 00 || DigestInfo || H(m). Deterministic and widely used (most X.509 certificates); no attack is known when the verifier parses strictly. - PSS (Probabilistic Signature Scheme) adds a random salt and masks like OAEP; it has a tight security proof and is required in TLS 1.3 for RSA signatures.
Parsing matters: Bleichenbacher's 2006 attack forged v1.5 signatures against verifiers that did not check that the hash was at the very end of the block. A forger chooses a block 00 01 FF 00 DigestInfo H(m) <garbage> whose garbage makes the number a perfect cube, and the cube root is the "signature".
import hashlib
def icbrt(x):
r = 1 << ((x.bit_length() + 2) // 3)
while True:
s = (2 * r + x // (r * r)) // 3
if s >= r:
return r
r = s
k = 384 # a 3072-bit modulus, e = 3
digest = hashlib.sha256(b"I owe you nothing").digest()
prefix = b"\x00\x01\xff\x00" + bytes.fromhex("3031300d060960864801650304020105000420") + digest
block = int.from_bytes(prefix + b"\xff" * (k - len(prefix)), "big")
s = icbrt(block) # forged signature: no key used
em = (s**3).to_bytes(k, "big")
print("starts with the right prefix:", em[:len(prefix)] == prefix)
print("lax parser accepts:", em[:4] == b"\x00\x01\xff\x00" and em[23:55] == digest)starts with the right prefix: True
lax parser accepts: True
A strict parser rebuilds the full expected block and compares all bytes, which rejects the garbage.
In software
- Python:
private_key.sign(data, padding.PSS(mgf=padding.MGF1(hashes.SHA256()), salt_length=padding.PSS.MAX_LENGTH), hashes.SHA256()). - Code signing, JWTs (
RS256is PKCS #1 v1.5 with SHA-256,PS256is PSS) and certificate chains all use RSA signatures. More on how they are used in Digital Signatures.
Exercises
Exercise 1: Tiny signature
With , , , sign and verify.
Answer
; .
Solution
: , (), so . Verify: .
Exercise 2: Why hash?
Give two reasons to sign instead of .
Answer
It blocks the algebraic forgeries, and it lets you sign messages longer than .
Solution
- Forgers cannot invert or multiply hashes in a useful way, so existential and multiplicative forgeries fail.
- must be smaller than ; a hash is a fixed short digest of a message of any length, so one exponentiation signs a whole file.
Exercise 3: Detect the tampering (code)
Sign a message with the FDH code, then flip one character and check that verification fails. How many bits of the hash change?
Answer
Verification fails; about half of the bits change.
Solution
import hashlib
a = hashlib.sha256(b"pay Bob 10 EUR").digest()
b = hashlib.sha256(b"pay Bob 19 EUR").digest()
diff = sum(bin(x ^ y).count("1") for x, y in zip(a, b))
print(diff, "of 256 bits differ")128 of 256 bits differ
The avalanche effect makes the hash of the altered message unrelated to the signed one, so no longer matches.
Test yourself
A textbook RSA signature on is…
Given signatures on (textbook RSA), the forger can sign…
- nothing new
- with
- with
- any message
Which RSA signature padding is randomized and required for RSA in TLS 1.3?
- PKCS #1 v1.5
- PSS
- no padding
- OAEP