TExcelTAD

RSA Done Right: OAEP and PSS

The previous lesson ended with textbook RSA being deterministic, malleable and leaky. Real RSA never encrypts or signs a bare number: it first pads the message into a randomised, structured block that fills the whole modulus. Two padding schemes are current: OAEP for encryption and PSS for signatures, both in PKCS#1 v2.2 (RFC 8017). The older PKCS#1 v1.5 schemes are still common, and one of them has caused a quarter century of attacks.

OAEP: randomised encryption

Optimal Asymmetric Encryption Padding (Bellare and Rogaway, 1994) mixes the message with a fresh random seed through two applications of a mask-generation function (MGF1, built from a hash):

text
seed   = random(hLen)
DB     = lHash || zero padding || 0x01 || message
masked = DB XOR MGF1(seed)       ;   maskedSeed = seed XOR MGF1(masked)
EM     = 0x00 || maskedSeed || masked          ->   c = EM^e mod n

The result: the same message encrypts to a different ciphertext each time, any change to the ciphertext makes the padding check fail after decryption, and OAEP is provably secure against chosen-ciphertext attacks (IND-CCA2) under the RSA assumption in the random-oracle model. The cost is space: with SHA-256, OAEP leaves k − 2·32 − 2 bytes for the message, where k is the modulus size in bytes.

PSS: randomised signatures

The Probabilistic Signature Scheme (Bellare and Rogaway, 1996) hashes the message, combines the hash with a random salt, and encodes both into a block with a MGF1 mask before applying the private key. Verification recomputes the structure. PSS has a tight security proof; PKCS#1 v1.5 signatures (a fixed byte pattern plus the hash's DER-encoded identifier) have no comparable proof but no practical break either when verified strictly. TLS 1.3 requires RSA-PSS for handshake signatures (the Signature type: RSA-PSS line in OpenSSL s_client), while certificates are still mostly signed with v1.5.

Both in code

The key is generated on every run, so the example prints sizes and verification results only.

python · try it yourself
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import hashes
from cryptography.exceptions import InvalidSignature

key = rsa.generate_private_key(public_exponent=65537, key_size=3072)   # a new key on every run
pub = key.public_key()
print("modulus bits:", pub.key_size, " e =", pub.public_numbers().e)

oaep = padding.OAEP(mgf=padding.MGF1(hashes.SHA256()), algorithm=hashes.SHA256(), label=None)
secret = b"a 32-byte AES key would go here!"
c1 = pub.encrypt(secret, oaep)
c2 = pub.encrypt(secret, oaep)
print("OAEP ciphertext bytes:", len(c1), " two encryptions equal:", c1 == c2)
print("decrypts:", key.decrypt(c1, oaep) == secret)
print("max OAEP-SHA256 plaintext:", pub.key_size // 8 - 2 * 32 - 2, "bytes")

pss = padding.PSS(mgf=padding.MGF1(hashes.SHA256()), salt_length=padding.PSS.DIGEST_LENGTH)
doc = b"release v2.4.1 sha256=9f2c..."
sig = key.sign(doc, pss, hashes.SHA256())
print("PSS signature bytes:", len(sig))
pub.verify(sig, doc, pss, hashes.SHA256()); print("verify original: OK")
try:
    pub.verify(sig, doc.replace(b"2.4.1", b"2.4.2"), pss, hashes.SHA256())
except InvalidSignature:
    print("verify edited: InvalidSignature")
modulus bits: 3072  e = 65537
OAEP ciphertext bytes: 384  two encryptions equal: False
decrypts: True
max OAEP-SHA256 plaintext: 318 bytes
PSS signature bytes: 384
verify original: OK
verify edited: InvalidSignature

Ciphertexts and signatures are always the size of the modulus, 384 bytes for RSA-3072. Encrypting the same 32-byte key twice gives two different ciphertexts. In practice RSA encrypts only short secrets (a symmetric key) and the data goes through an AEAD: this hybrid pattern is how S/MIME, PGP and many key-wrapping systems work.

The PKCS#1 v1.5 encryption story

PKCS#1 v1.5 encryption pads as 0x00 0x02 || at least 8 random non-zero bytes || 0x00 || message. In 1998 Daniel Bleichenbacher showed that a server which reveals whether a decrypted block starts with 0x00 0x02 (an error message, a timing difference, a different TLS alert) is a padding oracle for RSA: by multiplying the ciphertext by chosen values ses^e (malleability again) and asking the oracle, an attacker decrypts a ciphertext with about a million queries. The attack recovered TLS premaster secrets from servers using RSA key exchange.

Twenty years of patches tried to make servers respond identically. They kept failing: DROWN (2016, through SSLv2), ROBOT (2017, still working against products from many vendors, including F5, Citrix and Cisco), and Marvin (2023, timing side channels in many RSA implementations, including in decryption code paths that were supposed to be constant time). The lesson is that a scheme which needs perfect side-channel hygiene to be secure is the wrong scheme. TLS 1.3 removed RSA key exchange entirely.

How RSA padding problems are detected

  • Code: padding.PKCS1v15() used with encrypt/decrypt, RSA/ECB/PKCS1Padding in Java, RSA_PKCS1_PADDING in OpenSSL calls, padding=None or NoPadding anywhere; custom error messages after RSA decryption.
  • TLS configuration: TLS_RSA_WITH_* cipher suites (RSA key exchange, no forward secrecy, Bleichenbacher-prone); scanners such as testssl.sh and the ROBOT checker test your own servers.
  • Signatures: verification code that parses PKCS#1 v1.5 signatures loosely (accepting trailing garbage or wrong lengths). Bleichenbacher's 2006 e=3e = 3 signature forgery and the 2014 "BERserk" bug in NSS exploited exactly that.

How it is fixed

  • Encryption: RSA-OAEP with SHA-256 for anything new; better, replace RSA encryption with an ECDH- or ML-KEM-based key exchange and an AEAD (HPKE, RFC 9180, packages this).
  • Signatures: RSA-PSS where you control both sides; v1.5 signatures only through a library that verifies by re-encoding and comparing, never by parsing.
  • TLS: disable RSA key exchange suites; TLS 1.3 only uses (EC)DHE.

Pitfalls

  • Mixing hash choices: OAEP and MGF1 hashes, and the PSS salt length, must match between sender and receiver.
  • Using the same RSA key for encryption and signing: separate keys, separate purposes (the keyUsage extension in certificates says which).
  • Catching the decryption exception and returning a different error code than for other failures: that is the oracle.

Practice

1. What is the largest message that RSA-2048 with OAEP-SHA256 can encrypt?

k=256k = 256 bytes, so 256−2⋅32−2=190256 - 2 \cdot 32 - 2 = 190 bytes. Enough for a symmetric key, not for a document.

2. A legacy server decrypts PKCS#1 v1.5 ciphertexts and returns "bad padding" or "bad MAC". What attack applies and what is the fix?

Bleichenbacher's padding-oracle attack: the error difference reveals whether the decrypted block was well-formed, enough to decrypt a ciphertext with many queries. Fix: move to OAEP (or remove RSA encryption), and as a stopgap make every failure indistinguishable, including timing, as RFC 5246 describes for TLS (continue with a random premaster secret).

3. In the example, why does the edited document fail verification even though only one character changed?

PSS verifies the hash of the document; changing any byte changes the SHA-256 digest completely, so the encoded block no longer matches.

Test yourself

Which padding scheme should new RSA encryption use?

  1. None
  2. PKCS#1 v1.5
  3. OAEP
  4. PSS

Why were the two OAEP ciphertexts of the same key different?

  1. The key changed
  2. OAEP mixes in a fresh random seed every time
  3. A bug
  4. The ciphertexts were compressed

What do DROWN, ROBOT and Marvin have in common?

  1. They attack AES-GCM
  2. They are variants of Bleichenbacher's attack on PKCS#1 v1.5 RSA decryption
  3. They break SHA-256
  4. They target Ed25519

How large is an RSA-3072 signature?

  1. 32 bytes
  2. 64 bytes
  3. 256 bytes
  4. 384 bytes