Docs / Guides

Key rotation

Rotate keys and change suites without downtime: old data stays readable, new data uses the new key, nothing is switched off before it is unused.

01Rotate a keyset

  1. Add a key with the suite named. It starts as active.
  2. Promote it. New data uses it; the old key becomes active and keeps opening old data.
  3. Re-encrypt or re-wrap what matters.
  4. Retire the old key. It still opens, but never seals or signs.
  5. Remove it from the file once nothing depends on it. This is the only way to stop it opening.
ks = keyset.add(ks, { suite: 'PQL-DEFAULT' });
ks = keyset.promote(ks, newKid);
// … re-wrap records …
ks = keyset.retire(ks, oldKid);
ks = keyset.add(ks, suite="PQL-DEFAULT")
ks = keyset.promote(ks, new_kid)
ks = keyset.retire(ks, old_kid)
ks, _ = keyset.Add(ks, keyset.WithSuite(keyset.SuiteDefault))
ks, _ = keyset.Promote(ks, newKid)
ks, _ = keyset.Retire(ks, oldKid)
pql keys add seal.keyset          # prints the new kid
pql keys promote seal.keyset KID

02Re-wrap without re-encrypting

seal_batch and stream store the data key in a 1,271-byte record. rotate.rewrap opens that record with the old key and seals it to the new primary. The data itself is never touched, so rotating terabytes of backups means rewriting a few kilobytes per file.

const { record: updated, changed } = rotate.rewrap(ks, record);
if (changed) await db.query('UPDATE batches SET record = $1 WHERE id = $2', [updated, id]);
updated, changed = rotate.rewrap(record, ks)
updated, changed, err := rotate.Rewrap(ks, record)

rewrapAll walks a whole collection and reports how many records changed, were already current or failed.

03Schedule

Set rotate_every in pql.policy.yaml to record the rotation interval the application commits to. A later key compromise opens every envelope sealed to that key, so regular rotation limits the damage.

CLI keys are PQL-DEFAULT

pql keys new and pql keys add create PQL-DEFAULT keys only in v0.1. Rotate PQL-HIGH and PQL-CNSA keysets with an SDK and name the suite.

pqlock v0.1.0 · unaudited preview · source of truth: spec/