QianHeng乾珩 PQC Docs Hub量子文档 ✦ Ask AI✦ 问问文档 ⚐ Scan⚐ 扫一扫

Crypto-Agility

Crypto-agility is the engineering property that lets you change cryptographic algorithms without re-architecting the system. It is the foundation of every PQC migration: if swapping an algorithm is cheap, the next swap — to PQC, to a fixed PQC parameter set, or away from a broken scheme — is just a configuration change.

What it means

An agile system treats the choice of algorithm as data, not as a structural assumption baked into the code. You can deploy a new KEM or signature scheme, negotiate it with peers, and retire the old one — all without touching application logic, redefining message formats, or rebuilding hardware.

PQC makes agility non-negotiable for two reasons. First, key, ciphertext, and signature sizes are much larger and differ between schemes, so any code that assumes a fixed size will break. Second, the PQC standards are young; you should expect to re-tune parameters and possibly swap primitives again as confidence and guidance evolve.

Design principles

  • Algorithm negotiation. Endpoints advertise supported algorithms and agree at runtime — never hard-wire a single scheme. Negotiation must be integrity-protected and enforce a minimum security floor; otherwise agility can become an entry point for downgrade attacks.
  • Cryptographic abstraction layer. Application code calls encrypt(), sign(), establish_key() — not rsa_encrypt(). The concrete algorithm lives behind the interface.
  • No hard-coded sizes. Never assume a 256-bit key, a 32-byte signature, or a fixed buffer. Size everything dynamically from the chosen algorithm's parameters.
  • Versioned protocols and formats. Messages, tokens, and certificates carry an algorithm identifier and a version so old and new can coexist during transition.
  • Central crypto policy. Allowed algorithms, key lengths, and deprecation dates live in one governed place, not scattered across services.
  • Identify the crypto provider. Route through a single library or service so you can audit and upgrade in one place.

Anti-patterns: avoid crypto-spaghetti

"Crypto-spaghetti" is what you get when cryptographic decisions are scattered and frozen across a codebase. Watch for:

  • Hard-coded algorithm names and OIDs in dozens of files.
  • Fixed-length buffers and struct fields that overflow or silently truncate larger PQC keys.
  • Serialization formats with no algorithm or version field.
  • Home-grown crypto inlined into business logic.
  • Key sizes assumed by both ends of a protocol with no negotiation.

Retrofitting existing systems

Most organizations cannot rebuild from scratch. A pragmatic retrofit path:

  1. Inventory call sites. Use the cryptographic discovery process to find every place crypto is invoked.
  2. Introduce a façade. Wrap the existing library behind a thin abstraction that initially just forwards calls.
  3. Externalize parameters. Move algorithm names, key sizes, and curve choices out of code into configuration or policy.
  4. Add negotiation. Extend protocols and formats with algorithm/version identifiers; keep accepting the old identifier during transition.
  5. Pilot a swap. Prove the façade by swapping in a hybrid scheme on one path — see Hybrid Cryptography.
  6. Generalize and govern. Roll the abstraction across services and put the allow-list under central policy.
# Anti-pattern: algorithm and size baked in
sig = rsa_sign(priv_2048, msg)          # breaks the day you move off RSA
assert len(sig) == 256

# Agile: algorithm chosen by policy, size derived dynamically
signer = crypto.signer(policy.signature_alg)   # e.g. "ml-dsa-65" or "rsa-pss-3072"
sig = signer.sign(msg)
store(sig, alg=signer.alg_id, sig_len=len(sig))  # length comes from the signer
Warning
Agility is not just code. A central policy that nobody enforces, or a negotiation that always falls back to the weakest option, gives you the maintenance cost of agility with none of the safety. Pin minimums, log downgrades, and alarm on legacy use.

Standards & references

密码敏捷

密码敏捷是一种工程特性,让你无需重构系统即可更换密码算法。它是一切 PQC 迁移的基石:只要替换算法的成本足够低,下一次替换——无论是换到 PQC 换到固定的 PQC 参数集 还是淘汰已被攻破的方案——就只是一次配置变更。

含义

敏捷系统把算法选择当作数据来对待,而非写死在代码中的结构性假设。你可以部署新的 KEM 或签名方案 与对端协商使用 并淘汰旧算法——全程不触碰应用逻辑 不重新定义报文格式 不重建硬件。

PQC 让敏捷成为硬性要求 原因有二。其一 密钥 密文与签名的尺寸大得多 且不同方案之间各异 任何假定固定尺寸的代码都会失效。其二 PQC 标准尚年轻 随着信心与指南演进 你应预期还要再次调参 甚至再次更换原语。

设计原则

  • 算法协商。端点声明各自支持的算法 在运行时达成一致 切勿写死单一方案。协商机制必须受完整性保护,并设置最低安全门槛;否则敏捷性可能演变为降级攻击入口。
  • 密码抽象层。应用代码调用 encrypt() sign() establish_key() 而非 rsa_encrypt()。具体算法隐藏在接口之后。
  • 不写死尺寸。切勿假定 256 位密钥 32 字节签名或固定缓冲区。一切尺寸都依所选算法参数动态确定。
  • 版本化协议与格式。报文 令牌与证书携带算法标识与版本 使新旧方案在过渡期可以共存。
  • 集中式密码策略。允许的算法 密钥长度与弃用日期统一受治理 而非散落于各服务。
  • 明确密码提供者。通过单一库或服务路由 以便在一处审计与升级。

反模式 远离密码意大利面

当密码决策散落并冻结在整个代码库时 你得到的就是密码意大利面。需警惕:

  • 算法名称与 OID 写死在数十个文件中。
  • 固定长度的缓冲区与结构体字段 会溢出或悄悄截断更大的 PQC 密钥。
  • 缺少算法或版本字段的序列化格式。
  • 自研密码内联进业务逻辑。
  • 协议两端各自假定密钥尺寸 却无协商机制。

改造既有系统

多数组织无法推倒重建。务实的改造路径如下:

  1. 盘点调用点。用密码发现流程找出所有调用密码之处。
  2. 引入门面。用一层薄抽象包裹既有库 初期仅做转发。
  3. 外置参数。把算法名称 密钥尺寸与曲线选择从代码移入配置或策略。
  4. 加入协商。为协议与格式扩展算法/版本标识 过渡期内继续接受旧标识。
  5. 试点替换。在某条路径上换入混合方案以验证门面——参见混合密码
  6. 推广并治理。把抽象层推广到各服务 并将白名单纳入集中策略。
# 反模式 算法与尺寸写死
sig = rsa_sign(priv_2048, msg)          # 一旦弃用 RSA 当天即失效
assert len(sig) == 256

# 敏捷 算法由策略选定 尺寸动态导出
signer = crypto.signer(policy.signature_alg)   # 例如 "ml-dsa-65" 或 "rsa-pss-3072"
sig = signer.sign(msg)
store(sig, alg=signer.alg_id, sig_len=len(sig))  # 长度来自签名器
警告
敏捷不只是代码。无人执行的集中策略 或总是回退到最弱选项的协商 只会让你承担敏捷的维护成本却得不到任何安全收益。请锁定最低门槛 记录降级 并对旧算法使用告警。

标准与参考

⚑ Report an error⚑ 纠错与校正