# Migrate: application code (key exchange, encryption to a public key, signatures)

**What is at stake.** Code that agrees a key with ECDH or encrypts to someone's public key (RSA-OAEP, ECIES, sealed boxes) produces data that can be recorded now and decrypted later if those algorithms fall. Code that signs with RSA or ECDSA is a different, later risk: forgery from the day of a break, nothing retroactive.

Order of preference, best first:

1. **Move it into TLS 1.3** and delete the custom code (finding rules `KEX-CODE-*`, `PKE-CODE-*`).
2. **Use a hybrid the library provides** (HPKE with an ML-KEM hybrid KEM).
3. **Use ML-KEM from the platform**, only when the human decides that ML-KEM alone is acceptable for this data (NIST approves ML-KEM on its own; hybrid adds a classic safety net). Write that decision in the plan.

Never write your own ML-KEM, ML-DSA or hybrid combiner, and never use pre-standard Kyber or Dilithium packages (hard rule 7). If none of the three options exists for the stack, the status is `BLOCKED: no library support`, not a home-made workaround.

"Tested here" = run on 2026-12-03 with Node 24.11.1 (OpenSSL 3.5.4). "Not run here" = from the vendor's documentation only.

## 1. Move it into TLS 1.3

If the code encrypts messages between two services you run, the simplest change is to send them over TLS 1.3 with hybrid key exchange (`migrate/tls.md`) and drop the custom layer. Ask the human: does anything rely on the data staying encrypted after it arrives (stored, forwarded, queued)? If yes, you need option 2 or 3 for the stored copy.

## 2. Hybrid from the library: HPKE

**Go 1.26+** provides `crypto/hpke`. RFC 9180 defines HPKE. The Go 1.26 documentation identifies `MLKEM768X25519` as the X-Wing hybrid from the post-quantum HPKE specification. The example operations passed a round trip on Go 1.27.4. [Go HPKE API](https://pkg.go.dev/crypto/hpke@go1.26.0)

```go
import "crypto/hpke"

kem := hpke.MLKEM768X25519()
sk, err := kem.GenerateKey()                        // store sk via your KMS, never in the repo
ct, err := hpke.Seal(sk.PublicKey(), hpke.HKDFSHA256(), hpke.AES256GCM(), info, plaintext)
pt, err := hpke.Open(sk, hpke.HKDFSHA256(), hpke.AES256GCM(), info, ct)
```

Other stacks: check the vendor docs for HPKE with an ML-KEM hybrid KEM before assuming it exists.

## 3. ML-KEM from the platform

Each of these gives a shared secret plus a ciphertext. Feed the shared secret into the library's HKDF to derive an AES-256-GCM key; do not use it as a key directly, and do not combine it with an ECDH secret yourself.

**Node.js 24.7.0+** (ML-KEM), tested here:

```js
const crypto = require('node:crypto');
const { publicKey, privateKey } = crypto.generateKeyPairSync('ml-kem-768');
const { sharedKey, ciphertext } = crypto.encapsulate(publicKey);   // ciphertext is 1088 bytes
const recovered = crypto.decapsulate(privateKey, ciphertext);        // equals sharedKey
```

**Python, `cryptography` 48.0.0+ with OpenSSL 3.5** (47.0.0 only on AWS-LC/BoringSSL builds). From the library docs, not run here:

```python
from cryptography.hazmat.primitives.asymmetric.mlkem import MLKEM768PrivateKey
private_key = MLKEM768PrivateKey.generate()
shared_secret, ciphertext = private_key.public_key().encapsulate()
recovered = private_key.decapsulate(ciphertext)
```

**Go 1.24+** (`crypto/mlkem`). The example operations passed a round trip on Go 1.27.4.

```go
dk, err := mlkem.GenerateKey768()
sharedKey, ciphertext := dk.EncapsulationKey().Encapsulate()
recovered, err := dk.Decapsulate(ciphertext)
```

**Java 24+** (JEP 496): `KeyPairGenerator.getInstance("ML-KEM-768")`, then `KEM.getInstance("ML-KEM")` with `newEncapsulator(publicKey).encapsulate()` and `newDecapsulator(privateKey).decapsulate(...)`. Not run here.

**.NET 10** provides `MLKem.GenerateKey(MLKemAlgorithm.MLKem768)`. Check `MLKem.IsSupported` on the deployment host. Support depends on the system cryptography provider. [Microsoft .NET 10 cryptography notes](https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-10/libraries)

## Signatures (rules `SIG-CODE-*`)

These are not "harvest now" risks, so they come in a later wave. Read the algorithm from configuration or key metadata, never hard-code it, so the switch later is a config change.

| Stack | ML-DSA available from | Status |
|---|---|---|
| Node.js | 24.6.0 (`generateKeyPairSync('ml-dsa-65')`, `crypto.sign(null, data, key)`); SLH-DSA 24.8.0 | tested here (ML-DSA-65 sign and verify) |
| Python `cryptography` | 47.0.0 on AWS-LC/BoringSSL; 48.0.0 with OpenSSL 3.5 | not run here |
| Go | 1.27 (`crypto/mldsa`; also in `crypto/x509` and TLS 1.3) | not run here |
| Java | 24 (JEP 497) | not run here |
| .NET | 10 (`MLDsa`, `SlhDsa`) | not run here |

Use ML-DSA where both ends support it, SLH-DSA (FIPS 205) for long-lived roots. Otherwise keep Ed25519 or ECDSA P-256 for now with a note in the PR. No new RSA keys under 3072 bits, no DSA, no SHA-1.

## Encrypted data at rest: envelope re-wrap

Most stored data is encrypted with a symmetric data key (AES), and only that data key is wrapped with RSA or ECDH. AES-256 is not the problem; the wrapping is. So:

1. **Version the format.** Every stored blob carries `{"v": 2, "kem": "ML-KEM-768", ...}` or a version byte. The reader rejects unknown versions.
2. **New data** uses the new wrapping.
3. **Stored data**: unwrap each data key with the old private key and re-wrap it with the new one. The bulk data is not re-encrypted. Do it in batches, with a dry run, and keep the old key until every blob reports version 2. Inventory old envelopes in replicas, backups and exports. Anyone who already captured an old envelope and its encrypted payload still has that copy after you re-wrap the live key.
4. **Keep decrypt-only support** for version 1 until the re-wrap is complete and verified, then remove it in a separate change.

Proof is `prove.md` P4: a round trip of the new format, plus a fixture written in the old format that still decrypts.

## How to prove

P1 (re-scan closes the `KEX-CODE-*` / `PKE-CODE-*` ids) plus P3 (the test suite passes) and P4 (round trip and old-data fixture).
