TExcelTAD
Home › Number Theory & Cryptography › RSA Signatures

RSA Signatures

Run RSA backwards and you get a signature: only the private-key holder can compute s=md mod ns = m^d \bmod n, and anyone can check se≡ms^e \equiv m. 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: s=md mod ns = m^d \bmod n. Verify: accept if se≡m(modn)s^e \equiv m \pmod{n}.

Two forgeries need no private key:

  1. Existential forgery: pick any ss, compute m=se mod nm = s^e \bmod n. Then (m,s)(m, s) is a valid pair, for a random-looking mm.
  2. Multiplicative forgery: from signatures on m1m_1 and m2m_2, s1s2s_1 s_2 is a valid signature on m1m2m_1 m_2.
python · try it yourself
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 H(m)H(m) instead of mm fixes both problems, if HH is a good cryptographic hash: a forger would need a message whose hash equals the chosen ses^e, 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 Zn\mathbb{Z}_n; it is provably secure in the random-oracle model. The toy below stretches SHA-256 to the modulus length:

python · try it yourself
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 e=3e = 3 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".

python · try it yourself
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 kk 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 (RS256 is PKCS #1 v1.5 with SHA-256, PS256 is PSS) and certificate chains all use RSA signatures. More on how they are used in Digital Signatures.

Exercises

Exercise 1: Tiny signature

With n=55n = 55, e=3e = 3, d=27d = 27, sign m=2m = 2 and verify.

Answer

s=18s = 18; 183≡2(mod55)18^3 \equiv 2 \pmod{55}.

Solution

227 mod 552^{27} \bmod 55: 210=1024≡342^{10} = 1024 \equiv 34, 220≡342=1156≡12^{20} \equiv 34^2 = 1156 \equiv 1 (1156=21⋅55+11156 = 21 \cdot 55 + 1), so 227≡27=128≡182^{27} \equiv 2^7 = 128 \equiv 18. Verify: 183=5832=106⋅55+218^3 = 5832 = 106 \cdot 55 + 2.

Exercise 2: Why hash?

Give two reasons to sign H(m)H(m) instead of mm.

Answer

It blocks the algebraic forgeries, and it lets you sign messages longer than nn.

Solution
  1. Forgers cannot invert or multiply hashes in a useful way, so existential and multiplicative forgeries fail.
  2. mm must be smaller than nn; 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
python · try it yourself
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 ses^e no longer matches.

Test yourself

A textbook RSA signature on mm is…

  1. me mod nm^e \bmod n
  2. md mod nm^d \bmod n
  3. H(m)H(m)
  4. m⋅dm \cdot d

Given signatures s1,s2s_1, s_2 on m1,m2m_1, m_2 (textbook RSA), the forger can sign…

  1. nothing new
  2. m1m2m_1 m_2 with s1s2s_1 s_2
  3. m1+m2m_1 + m_2 with s1+s2s_1 + s_2
  4. any message

Which RSA signature padding is randomized and required for RSA in TLS 1.3?

  1. PKCS #1 v1.5
  2. PSS
  3. no padding
  4. OAEP