> For the complete documentation index, see [llms.txt](https://alham-rizvi.gitbook.io/alhamrizvi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alham-rizvi.gitbook.io/alhamrizvi/cryptography/rsa/17.-padding-oracle-attack.md).

# Padding Oracle Attack (Bleichenbacher Attack)

## 1. Overview

This is a **chosen-ciphertext attack** against RSA when **PKCS#1 v1.5 padding** is used.

You are given:

```
n, e, C
```

and access to an oracle:

```
Oracle(C') → tells whether decrypted message has valid padding
```

Goal:

```
Recover plaintext M without knowing d
```

***

## 2. PKCS#1 v1.5 Padding Format

Before encryption, message is padded:

```
EM = 0x00 || 0x02 || PS || 0x00 || M
```

Where:

```
PS = random non-zero bytes (at least 8 bytes)
```

So valid decrypted message must satisfy:

```
Starts with: 00 02
```

***

## 3. Core Idea

RSA is multiplicative:

```
C' = (C · s^e) mod n
```

After decryption:

```
M' = (M · s) mod n
```

We manipulate `s` and observe:

```
Does M' have valid padding?
```

***

## 4. Key Insight

Valid padding means:

```
2B ≤ M' < 3B
```

Where:

```
B = 2^(8*(k-2))   (k = byte length of n)
```

***

## 5. Attack Strategy

We search for `s` such that:

```
M' = (M · s) mod n falls in [2B, 3B)
```

Each successful oracle response:

```
narrows possible range of M
```

***

## 6. Interval Reduction

We maintain a set of intervals:

```
M ∈ [a, b]
```

Each valid `s` gives:

```
new constraints on M
```

We update intervals until:

```
interval size = 1 → M found
```

***

## 7. Step-by-Step Flow

### Step 1 — Blinding

Find first `s1` such that:

```
Oracle(C · s1^e mod n) = valid
```

***

### Step 2 — Narrow intervals

Compute:

```
M ∈ [2B/s, 3B/s]
```

Update bounds.

***

### Step 3 — Search next s

Find next `s` values that maintain valid padding.

***

### Step 4 — Repeat

Keep shrinking interval until:

```
M is uniquely determined
```

***

## 8. Simplified Python Structure

```python
def padding_oracle_attack(n, e, C, oracle):
    k = (n.bit_length() + 7) // 8
    B = 2 ** (8 * (k - 2))

    # Step 1: find s1
    s = 1
    while True:
        s += 1
        C_test = (C * pow(s, e, n)) % n
        if oracle(C_test):
            break

    # Initial interval
    intervals = [(2*B, 3*B)]

    # Loop until narrowed
    while True:
        new_intervals = []

        for a, b in intervals:
            # update bounds based on s
            # (simplified — real version more complex)
            new_intervals.append((a, b))

        intervals = new_intervals

        if len(intervals) == 1 and intervals[0][0] == intervals[0][1]:
            return intervals[0][0]
```

***

## 9. Oracle Example

```python
def oracle(C):
    M = pow(C, d, n)
    return (M >> (8*(k-1))) == 0x02
```

***

## 10. Why This Works

Because:

```
Padding creates structure in plaintext
```

Oracle leaks:

```
valid / invalid → 1 bit of structured information
```

Repeated queries:

```
fully recover M
```

***

## 11. When to Recognize

Look for:

```
Server responds differently for:
- valid padding
- invalid padding
```

Examples:

```
"Decryption error"
"Invalid padding"
HTTP 500 vs 200
```

***

## 12. Real-World Impact

This attack was used against:

```
SSL/TLS implementations (historically)
```

It is one of the most famous RSA attacks.

***

## 13. Limitations

```
Requires oracle access
Requires PKCS#1 v1.5 padding
Slow (many queries)
```

***

## 14. Defense

```
Use RSA-OAEP padding
Do not leak padding errors
Constant-time responses
```

***

## 15. Summary

```
Manipulate ciphertext with s^e
→ oracle reveals padding validity
→ shrink interval
→ recover M
```

***

## 16. Final Note

```
This is not a math attack
This is an implementation attack
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://alham-rizvi.gitbook.io/alhamrizvi/cryptography/rsa/17.-padding-oracle-attack.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
