End-to-end encryption for HTTP APIs using RFC 9180 HPKE
Project description
hpke-http
End-to-end encryption for HTTP APIs using RFC 9180 HPKE.
Highlights
- Transparent - Drop-in middleware, no application code changes
- E2E encryption - Protects data even with TLS termination at CDN/LB
- PSK binding - Each request cryptographically bound to API key
- Replay protection - Counter-based nonces prevent replay attacks
- RFC 9180 compliant - Auditable, interoperable standard
Installation
uv add "hpke-http[fastapi]" # Server
uv add "hpke-http[aiohttp]" # Client
uv add "hpke-http[fastapi,zstd]" # + compression
Quick Start
Server (FastAPI)
from fastapi import FastAPI, Request
from fastapi.responses import StreamingResponse
from hpke_http.middleware.fastapi import HPKEMiddleware
from hpke_http.constants import KemId
app = FastAPI()
async def resolve_psk(scope: dict) -> tuple[bytes, bytes]:
api_key = dict(scope["headers"]).get(b"authorization", b"").decode()
return (api_key.encode(), (await lookup_tenant(api_key)).encode())
app.add_middleware(
HPKEMiddleware,
private_keys={KemId.DHKEM_X25519_HKDF_SHA256: private_key},
psk_resolver=resolve_psk,
)
@app.post("/chat")
async def chat(request: Request):
data = await request.json() # Decrypted automatically
async def generate():
yield b"event: progress\ndata: {\"step\": 1}\n\n"
yield b"event: complete\ndata: {\"result\": \"done\"}\n\n"
return StreamingResponse(generate(), media_type="text/event-stream")
Client (aiohttp)
from hpke_http.middleware.aiohttp import HPKEClientSession
async with HPKEClientSession(
base_url="https://api.example.com",
psk=api_key, # >= 32 bytes
psk_id=tenant_id,
) as session:
resp = await session.post("/chat", json={"prompt": "Hello"})
async for chunk in session.iter_sse(resp):
print(chunk) # b"event: progress\ndata: {...}\n\n"
Documentation
- RFC 9180 - HPKE
- RFC 7748 - X25519
- RFC 5869 - HKDF
- RFC 8439 - ChaCha20-Poly1305
- RFC 8878 - Zstandard (optional compression)
Security
Uses OpenSSL constant-time implementations via cryptography library.
- Security Policy - Vulnerability reporting
- SBOM - CycloneDX attached to releases
Contributing
Contributions welcome! Please open an issue first to discuss changes.
make install # Setup venv
make test # Run tests
make lint # Format and lint
License
Technical Details
Cipher Suite
| Component | Algorithm | ID |
|---|---|---|
| KEM | DHKEM(X25519, HKDF-SHA256) | 0x0020 |
| KDF | HKDF-SHA256 | 0x0001 |
| AEAD | ChaCha20-Poly1305 | 0x0003 |
| Mode | PSK | 0x01 |
Wire Format
Request/Response (Chunked Binary)
Headers:
X-HPKE-Enc: <base64url(32B ephemeral key)>
X-HPKE-Stream: <base64url(4B session salt)>
Body (repeating chunks):
┌───────────┬────────────┬─────────────────────────────────┐
│ Length(4B)│ Counter(4B)│ Ciphertext (N + 16B tag) │
│ big-end │ big-end │ encrypted: encoding_id || data │
└───────────┴────────────┴─────────────────────────────────┘
Overhead: 24B/chunk (4B length + 4B counter + 16B tag)
SSE Event
event: enc
data: <base64(counter_be32 || ciphertext)>
Decrypted: raw SSE chunk (e.g., "event: progress\ndata: {...}\n\n")
Uses standard base64 (not base64url) - SSE data fields allow +/= characters.
SSE Auto-Encryption
The middleware automatically encrypts SSE responses when both conditions are met:
- Request was encrypted -
SCOPE_HPKE_CONTEXTexists in scope - Response is SSE -
Content-Type: text/event-streamdetected
This is why media_type="text/event-stream" is required.
Compression (Optional)
Zstd compression reduces bandwidth by 40-95% for JSON/text.
HPKEMiddleware(..., compress=True) # Server
HPKEClientSession(..., compress=True) # Client
| Choice | Rationale |
|---|---|
| Compress-then-encrypt | Encrypted data is incompressible |
| Zstd (RFC 8878) | Best ratio/speed, Python 3.14 native |
| 64B threshold | Smaller payloads skip compression |
Pitfalls
# PSK too short
HPKEClientSession(psk=b"short") # InvalidPSKError
HPKEClientSession(psk=secrets.token_bytes(32)) # >= 32 bytes
# Missing content-type (won't auto-encrypt)
return StreamingResponse(gen()) # No encryption
return StreamingResponse(gen(), media_type="text/event-stream") # Auto-encrypted
Limits
| Resource | Limit |
|---|---|
| HPKE messages/context | 2^96-1 |
| SSE events/session | 2^32-1 |
| SSE event buffer | 64MB (configurable) |
| PSK minimum | 32 bytes |
| Chunk overhead | 24B (length + counter + tag) |
| Chunk size | 64KB |
Note: SSE is text-only (UTF-8). Binary data must be base64-encoded (+33% overhead).
Low-Level API
from hpke_http.hpke import seal_psk, open_psk
enc, ct = seal_psk(pk_r, b"info", psk, psk_id, b"aad", b"plaintext")
pt = open_psk(enc, sk_r, b"info", psk, psk_id, b"aad", ct)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hpke_http-1.0.0.tar.gz.
File metadata
- Download URL: hpke_http-1.0.0.tar.gz
- Upload date:
- Size: 58.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e54b7cfc3efa4c837e53c2bdec8e4e5d3e95d2c489cacb9adbb64626b8d8c491
|
|
| MD5 |
140eead15cfdb18547631adb3d83dbbb
|
|
| BLAKE2b-256 |
8455e9fa1c88a9646bc29bcb6d1dc630303c27034b3e2fed49636fae62ed4b27
|
Provenance
The following attestation bundles were made for hpke_http-1.0.0.tar.gz:
Publisher:
release.yml on dualeai/hpke-http
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
hpke_http-1.0.0.tar.gz -
Subject digest:
e54b7cfc3efa4c837e53c2bdec8e4e5d3e95d2c489cacb9adbb64626b8d8c491 - Sigstore transparency entry: 804873937
- Sigstore integration time:
-
Permalink:
dualeai/hpke-http@767d739d1847e5adbc627070055519d247121249 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/dualeai
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@767d739d1847e5adbc627070055519d247121249 -
Trigger Event:
release
-
Statement type:
File details
Details for the file hpke_http-1.0.0-py3-none-any.whl.
File metadata
- Download URL: hpke_http-1.0.0-py3-none-any.whl
- Upload date:
- Size: 40.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b62b0588f97c3ea8c77598715630e0561dbe14fa85c38a24b2ff45ed636d9e31
|
|
| MD5 |
e4b96aa381a3fa73725b3f92c79d2371
|
|
| BLAKE2b-256 |
d998c9d9217653a51ea0e49d6f8e2c869e5cbc2ca7ff72555b4324b4ca26df2c
|
Provenance
The following attestation bundles were made for hpke_http-1.0.0-py3-none-any.whl:
Publisher:
release.yml on dualeai/hpke-http
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
hpke_http-1.0.0-py3-none-any.whl -
Subject digest:
b62b0588f97c3ea8c77598715630e0561dbe14fa85c38a24b2ff45ed636d9e31 - Sigstore transparency entry: 804873956
- Sigstore integration time:
-
Permalink:
dualeai/hpke-http@767d739d1847e5adbc627070055519d247121249 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/dualeai
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@767d739d1847e5adbc627070055519d247121249 -
Trigger Event:
release
-
Statement type: