# Replace RSA and ECDH key wrapping with hybrid ML-KEM (HPKE X-Wing), without breaking old data

> Envelope encryption with RSA-OAEP or ECDH is exposed to harvest-now-decrypt-later. Move to hybrid HPKE (MLKEM768-X25519) with a versioned format and re-wrap.

Source: https://pqshift.dev/learn/replace-rsa-key-wrapping-ml-kem · Published 2026-12-04 · Updated 2026-12-04 · pqshift

If your code wraps data keys with RSA-OAEP or derives them with ECDH (envelope encryption, encrypted backups, JWE, "encrypt this secret for that service"), the wrapped keys are exposed to [harvest-now-decrypt-later](https://pqshift.dev/learn/harvest-now-decrypt-later) just like TLS traffic. Replace the wrap with a **hybrid KEM** such as HPKE with `MLKEM768-X25519` (X-Wing), **version the ciphertext format** so old data still decrypts, and **re-wrap** stored keys in the background. In Go 1.26+ the standard library does all of it: `crypto/hpke`.

## Why this needs a code change, not a config flag

TLS libraries pick the key exchange for you, so a runtime upgrade often fixes TLS. Application-level key wrapping is your code calling a specific algorithm:

```go
// Quantum-vulnerable: anyone holding this ciphertext can unwrap dataKey once RSA falls
wrapped, _ := rsa.EncryptOAEP(sha256.New(), rand.Reader, recipientRSAPub, dataKey, label)
```

Nothing upstream will change that call. And unlike TLS sessions, wrapped keys are usually **stored**, so they're exposed for as long as the data is kept.

## The pattern

1. **Hybrid KEM.** Combine ML-KEM with X25519 (or P-256), so the wrap holds if either survives.
2. **Versioned envelope.** Prefix every ciphertext with a format byte. `0x01` = legacy RSA-OAEP, `0x02` = hybrid HPKE. Decrypt dispatches on it; encrypt always writes the newest.
3. **Dual-publish keys.** Recipients publish both the old RSA public key and the new hybrid public key until every sender has upgraded.
4. **Re-wrap at rest.** A background job unwraps each stored data key with the legacy path and re-wraps it with the hybrid one. The data itself (AES-GCM) does not need re-encrypting.
5. **Retire the old path** once no `0x01` envelopes remain, then delete the RSA private key.

## Go: HPKE with MLKEM768-X25519 (Go 1.26+)

```go
import "crypto/hpke"

var (
    kem  = hpke.MLKEM768X25519() // X-Wing: ML-KEM-768 + X25519
    kdf  = hpke.HKDFSHA256()
    aead = hpke.AES256GCM()
    info = []byte("myapp data-key wrap v2")
)

// Recipient, once: generate and publish the public key
priv, err := kem.GenerateKey()
pubBytes := priv.PublicKey().Bytes()

// Sender: wrap a 32-byte data key
pub, err := kem.NewPublicKey(pubBytes)
sealed, err := hpke.Seal(pub, kdf, aead, info, dataKey) // encapsulated key || ciphertext
envelope := append([]byte{0x02}, sealed...)

// Recipient: unwrap
dataKey, err := hpke.Open(priv, kdf, aead, info, envelope[1:])
```

`info` binds the wrap to your application and format version, so a ciphertext from another context can't be replayed into this one.

## Dispatch on the version byte

```go
func unwrap(envelope []byte) ([]byte, error) {
    if len(envelope) == 0 {
        return nil, errors.New("empty envelope")
    }
    switch envelope[0] {
    case 0x02:
        return hpke.Open(hybridPriv, kdf, aead, info, envelope[1:])
    case 0x01:
        return rsa.DecryptOAEP(sha256.New(), nil, legacyRSAPriv, envelope[1:], label)
    default:
        return nil, fmt.Errorf("unknown envelope version %d", envelope[0])
    }
}
```

Existing ciphertexts from before the migration have no version byte. Either migrate them to `0x01`-prefixed form first, or detect them by length (an RSA-2048 OAEP ciphertext is exactly 256 bytes) and treat them as legacy.

## Without HPKE: ML-KEM + X25519 + HKDF

If you're on Go 1.24 or 1.25, build the combiner yourself from `crypto/mlkem`, `crypto/ecdh` and `crypto/hkdf`, then encrypt the data key with AES-256-GCM under the derived key:

```go
// Sender, given the recipient's ML-KEM encapsulation key and X25519 public key
pqSecret, pqCT := recipientMLKEM.Encapsulate()
eph, _ := ecdh.X25519().GenerateKey(rand.Reader)
ecSecret, _ := eph.ECDH(recipientX25519)

ikm := append(append([]byte{}, pqSecret...), ecSecret...)
// Bind both ciphertexts into the KDF so neither can be swapped out
salt := append(append([]byte{}, pqCT...), eph.PublicKey().Bytes()...)
kek, _ := hkdf.Key(sha256.New, ikm, salt, "myapp data-key wrap v2", 32)
// envelope = 0x02 || pqCT || eph public key || AES-256-GCM(kek, dataKey)
```

HPKE does exactly this, with a reviewed combiner. Prefer it when you can.

## Other languages

| Language | Building blocks |
|---|---|
| Java | `KEM.getInstance("ML-KEM")` (JDK 24+) + X25519 `KeyAgreement` + HKDF. See the [Java guide](https://pqshift.dev/learn/post-quantum-tls-java) |
| .NET | `MLKem` (.NET 10) + `ECDiffieHellman` + `HKDF.DeriveKey`. See the [.NET guide](https://pqshift.dev/learn/post-quantum-tls-dotnet) |
| Rust | `aws-lc-rs` exposes ML-KEM; combine with X25519 and HKDF the same way |
| Cloud KMS | Check whether your KMS offers ML-KEM keys; if wrapping happens inside the KMS, the KMS's algorithm is what matters |

## Signatures are a separate step

This page covers confidentiality: key wrapping and key agreement. Signatures (JWT signing, code signing) are not exposed to harvest-now-decrypt-later and migrate later, to ML-DSA. See [What is post-quantum cryptography?](https://pqshift.dev/learn/what-is-post-quantum-cryptography).

[pqshift](https://pqshift.dev/) finds RSA-OAEP, `publicEncrypt`, `createECDH` and similar key-wrapping call sites in Go, Node, Python and Java repos, and opens PRs that introduce a versioned hybrid envelope like this one, with a round-trip test that decrypts a fixture wrapped the old way.

## FAQ

### How do I replace RSA key wrapping with a post-quantum algorithm?

Use a hybrid KEM such as HPKE with MLKEM768-X25519 (X-Wing) to wrap data keys, prefix each ciphertext with a format version so legacy RSA-OAEP envelopes still decrypt, and re-wrap stored keys in the background.

### Do I need to re-encrypt all my data?

No. Only the wrapped data keys need re-wrapping. Data encrypted with AES-256-GCM under those keys stays as it is.

## Sources

- [Go crypto/hpke package (MLKEM768X25519, Seal, Open)](https://pkg.go.dev/crypto/hpke)
- [Go crypto/hkdf package](https://pkg.go.dev/crypto/hkdf)
- [RFC 9180: Hybrid Public Key Encryption](https://www.rfc-editor.org/rfc/rfc9180)
- [NIST FIPS 203, Module-Lattice-Based Key-Encapsulation Mechanism Standard](https://csrc.nist.gov/pubs/fips/203/final)
