# RFC-0033: Legacy Migration Adapters and Transaction - **Status:** Proposed; implements the stable content adapters and the migration transaction of RFC-0008. - **Owner:** Compatibility and migration architecture. - **Last updated:** 2026-11-30 ## Context RFC-0008 defines how v3 data moves into v4 and deferred the adapters, their vectors, and the CLI. Reading the v3 history settled which formats exist in users' hands: - **The 3.0.0 release**, the only v3 published on PyPI, wrote `salt (16) || nonce (12) || AES-GCM ciphertext and tag` from `encrypt_file`, and the same bytes Base64-encoded from `aes_encrypt`. Neither records the key derivation, which defaulted to Argon2id when available. - **The unreleased 3.x line** added the CSF header (`CSF!`, version, KDF identifier, salt and nonce lengths, chunk size), authenticated as GCM additional data in version 2. No v3 code wrote version 1; the line only read it. - **The key derivations:** scrypt (N = 2^14, r = 8, p = 1), PBKDF2-SHA256 (100,000 iterations), and Argon2id (64 MiB, 3 iterations, 1 lane), each producing a 32-byte AES key from the UTF-8 password. v3 read the Argon2 parameters from environment variables at import and never recorded them. ## Binding decision ### Adapters `cryptography_suite.legacy.open_legacy(source, legacy_format=..., password=..., kdf=...)` returns a stream of the plaintext of `csf-v2`, `csf-v1`, `raw-aes`, or `password-aes-v3` input. Nothing is detected or retried: - A CSF file must carry the declared version: a v2 file is not read as v1, so its authenticated header cannot be downgraded. An unknown version is `FORMAT_UNSUPPORTED`; an unknown KDF identifier, a salt or nonce length other than 16 and 12, or a chunk size other than 64 KiB is `FORMAT_INVALID`. - `raw-aes` and `password-aes-v3` require a declared `LegacyKdf`. CSF files name their algorithm; a declared KDF may only supply its parameters and must name the same algorithm. - `password-aes-v3` is strict Base64, at most 64 MiB of plaintext; one trailing line break is allowed. Streaming formats are bounded by 16 GiB and the policy's `max_stream_bytes`. - `LegacyKdf` bounds every parameter (scrypt N up to 2^20, PBKDF2 up to 10,000,000 iterations, Argon2 up to 4 GiB, 64 iterations, and 64 lanes), so a declared value cannot exhaust the machine. - The plaintext is unauthenticated until the stream ends and the GCM tag verifies. The stream is meant only for the migration transaction, which never publishes before that. - `local-keystore` and `pem-der` are key material, not encrypted content; their migration is not implemented and they are refused. ### Permission `Policy` gains `legacy_formats` and `migration_deadline`, written to the canonical document only when set, so existing identifiers are unchanged. `Policy.migration(deadline=..., legacy_formats=..., base=...)` grants the named formats until a UTC instant, on top of a base policy. `restrict` intersects the formats and keeps the earlier deadline, so it never grants legacy permission. ### Transaction `cryptography_suite.migration.migrate_legacy` performs RFC-0008's states in one pass, with no plaintext written anywhere: | RFC-0008 state | Implementation | | --- | --- | | Inventory | SHA-256 and size of every source byte read. | | Dry run | `destination=None` performs everything, including key-service calls and verification, and publishes nothing. | | Backup checkpoint | `backup_confirmed=True` is required to publish (`POLICY_DENIED` otherwise). | | Stage, decrypt, re-encrypt | The legacy stream feeds `Protector.seal_stream` into the destination's transactional sink. | | Independent verify | A second thread reads the envelope bytes as they are written through `Protector.open_stream`, and the sink commits only if a keyed digest and length of that plaintext equal the legacy plaintext's (`MIGRATION_VERIFY_FAILED` otherwise). | | Atomic promotion | The sink's no-overwrite atomic commit. | | Receipt | `MigrationReceipt`, `cs-migration-receipt/1`: digests and sizes of source and envelope, recipients, policy and context-commitment identifiers, KDF algorithm, timestamps, verification, and warnings; no secret, plaintext, context value, or path. | | Complete | The source is never modified or deleted. | `cryptosuite migrate` exposes it: `--legacy-format`, `--legacy-kdf`, the password from `--password-fd` or the terminal, the private key for verification from `--secret-fd` or `--secret-stdin`, `--dry-run` (the default) or `--execute` with `--backup-confirmed`, and `--receipt`, written atomically and never over an existing file. ## Deferred - A durable, resumable transaction store keyed by idempotency key. A migration is one atomic operation; rerunning it after a failure starts over, and a completed one is not repeated because its destination exists. - Rollback after promotion, which RFC-0008 leaves to the caller's backup. - Adapters for key material (`local-keystore`, `pem-der`): RFC-0034. ## Test and validation requirements - `tests/vectors/legacy/v3-legacy-vectors.json`: 13 vectors, every format with every KDF, produced by the original v3 code from git history and decrypted again by it (`tools/generate_legacy_vectors.py`); `csf-v1`, which v3 never wrote, is built to the v3 parser's rules and opened by it. - `tests/integration/test_legacy_migration.py`: every vector opens; every single-bit change and truncation is refused; no format is detected or downgraded; header fields, quotas, KDF bounds, and arguments are checked; the transaction publishes only verified envelopes, refuses without permission or backup, and publishes nothing on a wrong password, a failed verification, a tampered reopening, or cancellation. - `tests/unit/test_policy_migration.py` and `tests/cli/test_cli.py` cover the policy grant and the command.