kura_crypto (kura v2.31.3)
View SourceAES-256-GCM authenticated encryption for kura's at-rest field encryption
({encrypted, Inner} and {encrypted, Inner, Opts} fields).
A ciphertext is a self-framed binary. Its first byte is the frame version, and decryption dispatches on it, so by default a release reads every version earlier releases wrote.
v1 (encrypt/1, and encrypt/2 while the write format is v1):
<<1:8, KeyId:8, Nonce:12/binary, Tag:16/binary, CipherText/binary>>Encrypted under the keyring's root key with AAD <<1, KeyId>>. It is not
bound to a column: a v1 ciphertext copied into another encrypted column
that accepts v1 frames decrypts there.
v2 (encrypt/2 while the write format is v2, and always encrypt/3):
<<2:8, Mode:8, KeyId:16, IV:12/binary, Tag:16/binary, CipherText/binary>>KeyId is 16 bits wide, although the keyring holds ids 0-255. A v2
ciphertext is bound to its column context {Table, Column}: its key is a
per-column subkey of the root key R, never R itself, and its AAD is
<<"kura", 2, Mode:8, KeyId:16, Ctx/binary>>, where
Ctx = <<TLen:16, Table/binary, CLen:16, Column/binary>> and
PRK = HKDF-Extract("kura/enc/v2", R) (HKDF-SHA256, RFC 5869). A v2
ciphertext moved to another column or table, or with its mode or key id
changed, fails to decrypt. Neither the row nor the tenant (prefix or
attribute) is bound, so a database-level writer can still swap two
ciphertexts of the same column between rows or tenants, and reorder or
splice the elements of encrypted arrays within the column. No key material
is cached: subkeys are derived per operation.
Mode is one of:
0, randomized:K = HKDF-Expand(PRK, "kura/v2/rnd/gcm" || 0x00 || Ctx, 32)and a fresh random 96-bit IV per value, so equal values encrypt to different ciphertexts.1, deterministic (HMAC-SIV-GCM):K_mac = HKDF-Expand(PRK, "kura/v2/det/siv-hmac" || 0x00 || Ctx, 32),K_enc = HKDF-Expand(PRK, "kura/v2/det/gcm" || 0x00 || Ctx, 32)andIV = HMAC-SHA256(K_mac, AAD || Plaintext), first 12 bytes. Equal values of one column encrypt to equal ciphertexts under one key, so the column can be compared for equality and carry a unique index. On decryption the IV is recomputed from the plaintext and compared in constant time: a frame whose GCM tag verifies but whose IV is not the synthetic one raises{noncanonical, KeyId}, so exactly one frame is valid per key, column and value. Deterministic frames are written under the keyring'sdeterministic_active/0key, neveractive/0.
Deterministic encryption reveals which rows hold equal values (frequency
analysis) and its ciphertext is a stable pseudonym wherever it appears,
including telemetry and logged query parameters. Equality is visible
across tenants in both tenancy modes, since the context is the unprefixed
table. Both modes reveal the length of a value and whether it is NULL. A
synthetic IV collision between two distinct values of one column under one
key breaks GCM's confidentiality for that pair, so keep a deterministic
column under 2^32 distinct values per key (a collision probability of about
2^-33).
The write format of encrypt/1 and encrypt/2 comes from the kura
application environment, {encryption_write_format, v1 | v2} (default
v1). encrypt/3 always writes v2. While the setting is absent, the first
value written logs one warning per boot: a v1 frame is not bound to its
column, so someone who can write to the database can move a ciphertext to
another encrypted column and have it decrypted there. Setting v1
explicitly keeps v1 without the warning. The default becomes v2 in kura
3.0.
A column reads with a policy/0: its mode and its legacy setting.
With legacy => accept, a v1 frame and a v2 frame of the other mode are
read too; with legacy => reject they raise {refused_version, 1} and
{refused_mode, ModeByte}, ModeByte being the frame's mode byte.
decrypt/2 reads as
#{mode => randomized, legacy => accept}.
Every failure raises error({kura_crypto, Reason}) - it never returns
a value or an {error, _} tuple. This is deliberate: the write path treats
an {error, _} from kura_types:dump/2 as recoverable and retries it
through kura_types:cast/2, and the load seam still falls open, so a raise
is the only way to guarantee a crypto failure never writes plaintext to an
encrypted column or returns ciphertext as if it were the value. Reason
carries only atoms, small integers and the (non-secret) KeyId - never key
material, plaintext, or ciphertext, which would leak into crash reports and
logs. Writing or reading a v2 frame without a column context raises
no_context.
Summary
Types
The column a v2 ciphertext is bound to: the table name and the column name, each 1-65535 bytes.
Whether a column reads v1 frames and v2 frames of the other mode.
How a v2 value is encrypted: a fresh random IV, or a synthetic one.
What a column accepts on read.
Functions
Decrypt a v1 frame. Raises on a bad tag, unknown key or bad frame, and with
no_context on a v2 frame, which can only be read with its column context
(decrypt/2).
Decrypt a v1 or v2 frame read from the column Context, as
decrypt(Frame, Context, #{mode => randomized, legacy => accept}). Raises
on any failure.
Decrypt a frame read from the column Context, which accepts what Policy
allows (see policy/0). Raises on any failure, and with
{refused_version, 1} or {refused_mode, ModeByte} (the frame's mode byte)
on a frame the policy does not accept.
Encrypt a plaintext binary with the keyring's active key as a v1 frame.
Raises no_context when the write format is v2, which needs a column
context (encrypt/2).
Encrypt a plaintext binary for the column Context, randomized, in the
configured write format (write_format/0). Raises on failure.
Schema-aware callers reach it through kura_types:dump/3.
Encrypt a plaintext binary for the column Context as a v2 frame of
Mode, whatever the write format. A deterministic frame is written under
the keyring's deterministic_active/0 key and raises
no_deterministic_key when there is none. A deterministic Plain must
already be canonical, as kura_types:dump/3 produces it, or its frames will
not match lookups. Raises on failure.
Types
The column a v2 ciphertext is bound to: the table name and the column name, each 1-65535 bytes.
-type legacy() :: accept | reject.
Whether a column reads v1 frames and v2 frames of the other mode.
-type mode() :: randomized | deterministic.
How a v2 value is encrypted: a fresh random IV, or a synthetic one.
What a column accepts on read.
-type write_format() :: v1 | v2.
Functions
Decrypt a v1 frame. Raises on a bad tag, unknown key or bad frame, and with
no_context on a v2 frame, which can only be read with its column context
(decrypt/2).
Decrypt a v1 or v2 frame read from the column Context, as
decrypt(Frame, Context, #{mode => randomized, legacy => accept}). Raises
on any failure.
Decrypt a frame read from the column Context, which accepts what Policy
allows (see policy/0). Raises on any failure, and with
{refused_version, 1} or {refused_mode, ModeByte} (the frame's mode byte)
on a frame the policy does not accept.
Encrypt a plaintext binary with the keyring's active key as a v1 frame.
Raises no_context when the write format is v2, which needs a column
context (encrypt/2).
Encrypt a plaintext binary for the column Context, randomized, in the
configured write format (write_format/0). Raises on failure.
Schema-aware callers reach it through kura_types:dump/3.
Encrypt a plaintext binary for the column Context as a v2 frame of
Mode, whatever the write format. A deterministic frame is written under
the keyring's deterministic_active/0 key and raises
no_deterministic_key when there is none. A deterministic Plain must
already be canonical, as kura_types:dump/3 produces it, or its frames will
not match lookups. Raises on failure.
-spec write_format() -> write_format().
The frame version encrypt/1 and encrypt/2 write: the kura application
environment's encryption_write_format, v1 (the default) or v2. Any
other value raises invalid_write_format.