← Back to section · Tutorials & Labs
Making Algorithms Replaceable: Code Structure for Cryptographic Agility
Algorithms age, get broken, and get replaced by standards. This article covers the code-level structure of cryptographic agility: abstraction interfaces, algorithm identifiers and versioning, ciphertext that carries its own metadata, configuration-driven switching, and engineering the dual-read transition period.
Cryptographic algorithms have lifetimes. MD5 and SHA-1 went from mainstream to banned over a decade and a bit, RSA key lengths have been raised again and again, and now the entire public-key infrastructure must migrate to post-quantum algorithms. The question is never “whether to switch” but “how much code has to change when we do”. In systems where algorithms are hardcoded into business logic, every migration is an excavation: algorithm-name strings scattered everywhere, parameters assembled differently in each place, historical ciphertext with no version markers — ending in a full outage and a one-shot rewrite carried through by force. The goal of cryptographic agility is to turn algorithm replacement from major surgery into “change the configuration and run a batch job”. This article explains concretely how to do that at the level of code structure.
First principle: business code does not know specific algorithms
The foundation of the structure is the direction of dependency: business code calls only an abstract cryptographic service interface — “encrypt this data”, “verify this signature” — and never references concrete algorithm names or library functions directly. Behind the interface, a single centralized crypto service module holds the implementation, where algorithm choice, parameters, and key versions are all decided in one place. When an algorithm must change, the blast radius shrinks to the inside of that module, and business code notices nothing.
The companion discipline is cleaning up scattered algorithm constants: any hardcoded algorithm name, key length, or iteration count appearing in business code should be registered as technical debt. When review meets a new hardcoded value, ask: why can this choice not be made centrally by the crypto service? In the overwhelming majority of cases the answer is “no reason” — it was just convenient.
Ciphertext and signatures must carry their own metadata
Replaceability presupposes recognizability. Every ciphertext, every signature, and every hash record should carry structured metadata: the algorithm identifier, the parameter version, the key version, and the necessary random parameters (salt, IV). At decryption time, read the metadata first and route to the matching implementation — never assume “the whole database uses one algorithm”. Historical data without this marking is the hardest part of any migration: you cannot even count how much data still uses the old algorithm.
The metadata format must be stable and extensible: algorithm identifiers use canonical names from the registry, not free text; version numbers increase monotonically; new fields are only added, never given changed semantics. Keys are versioned the same way: key material itself lives in a KMS or key store, and data records only the key-version reference, decoupling rotation from algorithm switching — you can change keys without changing algorithms, and change algorithms in batches without changing keys at the same time.
Configuration-driven choice and the algorithm registry
Algorithm selection should be decided by configuration, not code: the registry maintains a mapping from “business scenario → current algorithm suite”, where a suite bundles the choices for encryption, signatures, hashing, and key derivation as a set, avoiding the mix-and-match of a new algorithm in one place and a stale default in another. Configuration changes need audit records and the ability to roll out gradually: enable the new suite first for newly written data, observe decryption failure rates and performance, then widen the scope.
A common misconception is to build agility as “every call site can pick any algorithm”. That is not agility; that is loss of control. The correct granularity is switching suites per data category and scenario, with the choice converging in one registry while callers express only business semantics. That preserves switching ability while avoiding an explosion of algorithm combinations and a long tail of misconfiguration.
The transition period: dual-read, single-write, batch re-encryption
The actual switch happens during the transition, and the standard play has three steps. Step one, dual-read single-write: the decryption path supports both old and new algorithms (routed by metadata), while the write path produces only new-algorithm data. Once this ships, the system is immune to historical data and new data converges automatically. Step two, batch re-encryption: a background job decrypts legacy data and re-encrypts it with the new algorithm, in a process that can pause, roll back, and report progress and failure rates; for signature data, the equivalent is arranging re-signing, or keeping the old verification path with its trust level labeled. Step three, retire the old algorithm: when monitoring confirms old-algorithm data is at zero, or only an acceptable residue remains, remove the old implementation and mark the old suite as disabled in the registry, so new code cannot misuse it.
Each step has an explicit completion criterion and a rollback point — the biggest difference between an agile structure and “leaving a switch”. Also note: the transition must not stretch indefinitely, since running two algorithm suites side by side itself enlarges the attack surface; set the retirement date when the project is chartered.
An easily missed piece: size and performance assumptions must be agile too
Replacing an algorithm is not just changing a name. The key, ciphertext, and signature sizes of a new algorithm often differ from the old by orders of magnitude: post-quantum signatures are tens of times larger than traditional ones, and key-exchange payloads grow substantially as well. Everywhere the code has hardcoded field lengths, database column widths, message size limits, or timeout thresholds will break first at switching time. So when inventorying hardcoding, list “size assumptions” as their own category: check length limits at the storage, protocol, and front-end display layers one by one, loosening what can be loosened and making configurable what should be configurable. The same holds for performance: a new algorithm’s cost profile differs from the old, so measure latency changes on critical paths during the registry’s gradual rollout phase instead of discovering handshake timeouts after launch. Only when size and performance are part of the agility design is the switch truly smooth.
Wrap-up: leave the interface ready for the next migration
The post-quantum migration is turning this from best practice into a hard requirement: systems still writing RSA and ECDSA today will have to move to algorithms such as ML-KEM and ML-DSA within a few years, and the new algorithms’ much larger keys and signatures will break code that hardcoded length assumptions first of all. Restructuring now along the lines of this article — abstract interfaces, metadata, registry, versioned keys — costs far less than a forced rewrite when migration is at the door. The essence of cryptographic agility is admitting the fact that “algorithms change”, and making the code structure pay for that change in advance.
Tags: Cryptographic AgilityArchitectureAlgorithm MigrationSecure Coding