Encryption at Rest Internals

This chapter describes how the storage engine encrypts data on disk: the cipher, the key hierarchy, how sealed bytes are laid out and bound to their location, which files are covered, how keys are loaded, checked and rotated, and the synergydb encryption commands. The operator view (turning it on, what is and is not covered, evidence) is in Security, section 4. Byte layouts of sealed WAL entries and SST blocks are in Write-ahead log and SSTable format.

Encryption at rest is off by default: [storage] encryption_enabled defaults to false and encryption_key_file to none (source: src/config.rs, StorageConfigFile; src/storage/mod.rs, StorageConfig::default).

The transparent encryption proxy in src/proxy is a separate, column-level system with its own keys; it is described in Encryption proxy.

Building blocks

Piece Third-party crate Where
AES-256-GCM, 12-byte random nonce, 16-byte tag aes-gcm 0.10, nonce from OsRng src/storage/crypto.rs, AtRestCipher
HKDF-SHA-512 (extract and expand) built from hmac and sha2 src/storage/keyring.rs, hkdf_extract_sha512, hkdf_expand_sha512
File ids for AAD xxhash-rust (xxh3, 64-bit) src/storage/keyring.rs, file_id_for
Key memory wiping zeroize src/storage/at_rest.rs, MasterKeys

AtRestCipher offers encrypt/decrypt, producing nonce(12) || ciphertext || tag(16), and encrypt_with_aad/decrypt_with_aad, where the associated data is supplied by the caller and never stored (source: src/storage/crypto.rs, AtRestCipher). Tested by roundtrip_and_tamper and hex_key_parsing in src/storage/crypto.rs.

There is no KMS integration in the storage layer. The master key comes only from an environment variable or a key file. The aws-kms Cargo feature is used only by the encryption proxy (source: src/storage/at_rest.rs, load_master_keys_from; Cargo.toml, [features]).

Key hierarchy

flowchart TD M["Master key, 32 bytes"] --> P["PRK = HKDF-Extract-SHA512(salt, master)"] P --> F["Fingerprint: Expand(PRK, FINGERPRINT_INFO, 4)"] P --> K["Subkey per purpose: Expand(PRK, info with LABEL_KEY, 32)"] P --> I["Key id per purpose: Expand(PRK, info with LABEL_KID, 4)"] K --> C["AtRestCipher (AES-256-GCM)"]

(source: src/storage/keyring.rs, Keyring::from_master, Keyring::cipher, Keyring::key_id)

Constant Value
KEY_LEN 32
KID_LEN 4
HKDF_SALT synergydb/at-rest/hkdf-sha512/v1
INFO_PREFIX synergydb/at-rest/v1
FINGERPRINT_INFO synergydb/at-rest/v1/master-fingerprint
LABEL_KEY, LABEL_KID 0x01, 0x02
SEAL_OVERHEAD 32 bytes (key id 4 + nonce 12 + tag 16)

(source: src/storage/keyring.rs)

The HKDF info for a subkey or key id is INFO_PREFIX || purpose (1 byte) || version (u16 LE) || label (1 byte) || tenant present (1 byte) || tenant length (u16 LE) || tenant bytes. The presence byte keeps "no tenant" and "empty tenant" apart, and the length prefix stops two tenant names from colliding by concatenation (source: src/storage/keyring.rs, build_info).

Purposes

Purpose Value Used for
SstData 1 SST data blocks
SstMeta 2 SST index, bloom and other meta blocks
WalEntry 3 WAL entry key and value fields
Manifest 4 MANIFEST snapshot and MANIFEST.log records
RaftLog 5 Raft log entries
RaftSnapshot 6 Raft snapshot files
VectorGraph 7 Saved vector index graphs
KeyCheck 8 The canary in the key-check file

(source: src/storage/keyring.rs, Purpose)

Each purpose has its own subkey and its own key id, and the purpose is also part of the AAD, so a block sealed for one purpose does not open as another. Subkeys and ciphers are derived on first use and cached in a DashMap (source: src/storage/keyring.rs, Keyring::cipher).

The Keyring type also supports a key version and a per-tenant subkey. Not wired: every production caller passes tenant = None, and the engine always builds its keyring with version 1, so neither per-tenant keys nor version-bump rotation are used. Rotation is done by changing the master key (below) (source: src/storage/at_rest.rs, MasterKeys::keyring; callers of Keyring::seal and Keyring::seal_blob).

Tested by src/storage/keyring.rs unit tests: RFC 5869 vector tests for the HKDF implementation, expand_spans_multiple_blocks, master_key_minimum_length_enforced, tenants_get_independent_keys, purposes_get_independent_keys, version_bump_rederives_all_subkeys, derivation_is_deterministic_across_instances, key_id_is_not_derived_from_the_key, cache_returns_consistent_material, tenant_names_cannot_collide_by_concatenation.

Sealed format and AAD

Keyring::seal(purpose, tenant, file_id, offset, plain) produces key_id(4) || nonce(12) || ciphertext || tag(16). Keyring::open reads the key id first, finds the matching ring (active or retired) with ring_for_kid, and decrypts. An unknown key id gives an error that names the known fingerprints, so a missing retired key is reported as a key problem rather than as corruption (source: src/storage/keyring.rs, Keyring::seal, Keyring::open, Keyring::ring_for_kid).

The AAD binds each ciphertext to where it belongs:

"synergydb/at-rest/aad/v1" || purpose (1) || key_version (u16 LE) || file_id (u64 LE)
  || offset (u64 LE) || tenant present (1) || tenant length (u16 LE) || tenant bytes

file_id is the xxh3-64 hash of the file name only, not the directory, so a data directory can be moved or restored elsewhere and still open (source: src/storage/keyring.rs, Aad::encode, file_id_for).

seal_blob and open_blob use file_id 0, offset 0 and no tenant: they bind the purpose and key only (source: src/storage/keyring.rs, Keyring::seal_blob).

Caller Purpose file_id offset
WAL entries WalEntry file_id_for(segment path) 0 for the key field, 1 for the value field
MANIFEST snapshot Manifest file_id_for(path) 0
MANIFEST.log record Manifest file_id_for(path) record's offset in the log
Raft log entry RaftLog 0 entry index
SST data block SstData blob (0) blob (0)
SST meta block SstMeta blob blob
Raft snapshot RaftSnapshot blob blob
Vector graph VectorGraph blob blob
Key-check canary KeyCheck blob blob

(source: src/storage/wal.rs, seal_field; src/storage/manifest.rs; src/distributed/raft_storage.rs; src/storage/sst.rs, BlockBuilder::flush_block, seal_block; src/storage/crypto.rs, seal_meta; src/vector/mod.rs; src/storage/at_rest.rs, write_key_check)

Because SST blocks use seal_blob, the AAD of a sealed SST block names only its purpose: GCM does not bind it to its file or position, unlike WAL entries and manifest records. Not verified: no test moves SST blocks within or between files; the relocation tests above use Keyring::seal directly.

Tested by seal_open_roundtrip, relocating_a_block_within_a_file_is_rejected, moving_a_block_between_files_is_rejected, cross_tenant_block_swap_is_rejected, purpose_confusion_is_rejected, bit_flip_in_sealed_body_is_rejected, rotated_keyring_reports_key_id_mismatch_not_corruption, truncated_blob_is_rejected_not_panicked, repeated_seal_of_identical_input_differs, file_id_tracks_name_not_directory, aad_encoding_is_injective_per_field in src/storage/keyring.rs. These exercise the keyring API directly, including the tenant field that production does not use.

Legacy format

Before the keyring, files were sealed directly with the master key: SST blocks with flag 0x80 (nonce || ct || tag), meta blocks with flag 1, WAL version 2. These still open: legacy_decrypt tries the active key and then each retired key. New writes always use the keyring formats when a keyring is installed (source: src/storage/crypto.rs, legacy_decrypt, seal_meta, open_meta; src/storage/sst.rs, BlockBuilder::flush_block, seal_block).

Meta flag Meaning
0 plaintext
1 legacy master-key cipher
2 keyring, Purpose::SstMeta

(source: src/storage/crypto.rs, seal_meta, open_meta)

Loading keys

load_master_keys_from decides which keys are active and retired (source: src/storage/at_rest.rs, load_master_keys_from, parse_key, read_key_file):

Situation Result
encryption_enabled = false No keys. A warning is logged if a key is configured anyway
SYNERGYDB_ENCRYPTION_KEY set It is the active key; all key-file lines become retired keys
Not set, key file given First line of the file is active, other lines are retired
SYNERGYDB_ENCRYPTION_RETIRED_KEYS Added as retired keys (comma or whitespace separated)
Enabled, no key found Startup error: no key found
  • A key is exactly 64 hex characters (32 bytes) (source: src/storage/crypto.rs, parse_hex_key).
  • A key made of one repeated byte is refused (source: src/storage/at_rest.rs, parse_key).
  • In the key file, blank lines and # comments are skipped. A warning is logged if the file is readable by group or others (mode bits 0o077) (source: src/storage/at_rest.rs, read_key_file).
  • Duplicate keys are dropped (source: src/storage/at_rest.rs, load_master_keys_from).
  • Keys are held in Zeroizing buffers (source: src/storage/at_rest.rs, MasterKeys).

Tested by disabled_means_no_keys, enabled_without_key_fails_closed, repeated_byte_key_is_refused, env_active_plus_retired_and_file in src/storage/at_rest.rs.

install puts three process-wide values in place: the legacy cipher for the active key, the legacy ciphers for retired keys, and the keyring with its retired rings. They are OnceLocks: the first install in a process wins, so one process can only run one key configuration. This is why each encryption integration test is a separate test binary (source: src/storage/at_rest.rs, install; src/storage/crypto.rs, install_at_rest_cipher, install_retired_legacy_ciphers; src/storage/keyring.rs, install_keyring).

Start-up order and the key-check file

StorageEngine::open loads and installs the keys before creating any directory, then takes the data-directory lock, then calls verify_key_check before loading the manifest (source: src/storage/mod.rs, StorageEngine::open).

The key-check file is data_dir/ENCRYPTION. After comment lines it holds format 1, algorithm AES-256-GCM, kdf HKDF-SHA-512, active <fingerprint> and canary <hex>, where the canary is synergydb at-rest key check v1 sealed with Purpose::KeyCheck. It is written to a temporary file, fsynced, renamed and the directory synced (source: src/storage/at_rest.rs, KEY_CHECK_FILE, CANARY_PLAINTEXT, write_key_check).

stateDiagram-v2 [*] --> Check Check --> Ok: no file, no key Check --> Refuse: file present, encryption off Check --> Write: no file, key present Write --> Ok: write file (warn if dir already had data) Check --> Open: file and key present Open --> Refuse: canary fails, WRONG ENCRYPTION KEY Open --> Rotate: canary opens, active fingerprint changed Rotate --> Ok: log rotation, rewrite file Open --> Ok: canary opens, same fingerprint

(source: src/storage/at_rest.rs, verify_key_check)

The canary is opened with the whole keyring, so it opens if the key that wrote it is now a retired key. Tested by retired_ring_opens_old_blobs_and_reports_fingerprint and purpose_is_bound in src/storage/at_rest.rs.

What is covered

Data Sealed Source
SST data blocks yes, SstData src/storage/sst.rs, BlockBuilder::flush_block, seal_block
SST index, bloom and meta blocks yes, SstMeta src/storage/crypto.rs, seal_meta
WAL entries (key and value, WAL version 3) yes; the commit-marker key stays plaintext src/storage/wal.rs, seal_field
MANIFEST, MANIFEST.log yes src/storage/manifest.rs
Raft log segments, Raft snapshots yes src/distributed/raft_storage.rs
Vector graphs (.graph) yes src/vector/mod.rs
LOCK, LOCK.pid, WAL_BACKUP_HOLD, state.bin, meta.bin, .role, .tmp files, server TLS certificate and key, initial password file no; the audit treats them as holding no user data src/storage/at_rest.rs, audit_file

The audit log is not classified by audit_file. Not verified: whether audit log segments are sealed (also noted in the security guide).

Tested by tests/at_rest_encryption.rs (rows_roundtrip_and_no_plaintext_reaches_disk, wal_does_not_leak_tenant_table_or_row_identifiers, encrypted_data_survives_reopen, interleaved_tenants_read_back_their_own_rows, manifest_does_not_leak_tenant_or_table_identifiers), tests/at_rest_manifest_tamper.rs (reordered_sealed_log_records_are_rejected), tests/at_rest_manifest_upgrade.rs (plaintext_manifest_still_loads_and_upgrades_to_sealed), and the control tests/at_rest_plaintext_control.rs (unencrypted_engine_does_leave_plaintext_on_disk). interleaved_tenants_read_back_their_own_rows checks round-trips only; tenants share one key.

Rotation

Rotation replaces the master key, not the key version (source: src/storage/at_rest.rs, load_master_keys_from, verify_key_check, rewrite_all):

  1. Generate a new key (synergydb encryption generate-key).
  2. Make the new key active and keep the old key as retired (via SYNERGYDB_ENCRYPTION_KEY plus SYNERGYDB_ENCRYPTION_RETIRED_KEYS, or new first line in the key file).
  3. On the next start, verify_key_check sees a new active fingerprint, logs it and rewrites ENCRYPTION. New writes use the new key; old data opens through the retired ring found by key id.
  4. Optionally, with the server stopped, run synergydb encryption rewrite. rewrite_all writes a marker key __synergydb__:encryption:rewrite, flushes, rewrites every SSTable block by block into a new sst-<n>.sst under the active key (recorded as a compaction, under the compaction execution lock and flush lock, with two-phase deletion of the old files), and removes flushed WAL segments that the backup hold does not protect. The manifest is resealed when it is next saved.
  5. synergydb encryption status then shows which files still use a retired key.

rewrite_all does not rewrite Raft log segments or vector graphs, so a retired key must stay configured until those files are replaced by normal operation. Not verified: no unit or integration test in the snapshot runs rewrite_all; the status entry reports rotation passing in the proof script (source: docs/status/2026-11-30-encryption-at-rest.md).

The synergydb encryption command

Subcommand What the code does Exit codes
status [-c FILE] [--path DIR]... [--quiet] Audits the data dir, WAL dir, data_dir/raft/* and extra paths. Classifies each file as Encrypted (with the key fingerprints used), Plaintext, Mixed, NoUserData, Unclassified or Error 0 all encrypted; 2 plaintext, mixed, or encryption disabled; 3 unknown key, unreadable file or key-check failure
rewrite [-c FILE] Requires a MANIFEST; opens the engine (the data-directory lock refuses if a server runs); runs rewrite_all; shuts down 1 on error
generate-key Prints 32 random bytes as hex on stdout and the fingerprint on stderr 0

(source: src/encryption_cmd.rs, run, status, rewrite; src/storage/at_rest.rs, audit, FileState)

How files are classified (source: src/storage/at_rest.rs, audit_file, audit_sst, audit_wal, audit_raft_segment, audit_magic_blob, audit_manifest):

File kind Check
SST Block census (at_rest_census): counts of plaintext, legacy and keyring blocks with key ids
WAL segment Header version: 3 keyring, 2 legacy, else plaintext
Raft log segment Version 2 is sealed
Raft snapshot data*.bin Magic \0SYNERGYDB-SEAL1
Vector graph .graph Magic \0SDB-VGRAPHSEAL1
MANIFEST, MANIFEST.log Sealed header or records

Not verified: no test runs the encryption subcommands; the status entry reports them in the proof script.

Cost

The status entry reports pgbench throughput within noise to about 5% lower, and WAL on disk about 55% larger until flushed segments are retired (source: docs/status/2026-11-30-encryption-at-rest.md). Per sealed item the overhead is SEAL_OVERHEAD, 32 bytes. The status entry describes this as a "32-byte key id + nonce + tag"; the key id itself is 4 bytes and the three together are 32 (source: src/storage/keyring.rs, SEAL_OVERHEAD).