Karta. Storage. Cipher
(Karta v1.0.0)
Encryption of stored message bodies: AES-256-GCM over 64 KiB chunks, so a message is encrypted and decrypted as a stream, never held whole in memory, and any truncated, reordered or altered byte makes decryption fail.
Format:
header = "KMB" <> <<1, byte_size(key_id), key_id::binary, salt::binary-32, chunk_size::32>>
body = for each chunk: ciphertext <> tag (16 bytes)- Every object gets its own key: HKDF-SHA256 (
Karta.Crypto.hkdf_sha256/4) of the master key (Karta.Vault, bykey_id), a random 32-byte salt and the object's name. The name binding means a stored body cannot be swapped in for another message's. - The nonce of chunk
iis<<i::88, last::8>>, wherelastis 1 only for the final chunk, so cutting the object at a chunk boundary fails too. Nonces never repeat under one object key; the random salt makes object keys unique. - The header is the additional authenticated data of every chunk.
- Every chunk except the last holds exactly
chunk_sizebytes; the last holds the rest (possibly 0 bytes, for an empty message).
Summary
Types
Reads the encrypted object at a position: the bytes from offset on
(whatever the reader gives), :eof, or an error. close/0 releases it.
Functions
The bytes of the encrypted object ({first, last}, both included) that
hold the chunks first_chunk to last_chunk of an object of size bytes
of plaintext; last may lie past the end (the object ends first).
Decrypts an object made by encrypt_stream/4, with the keys of Karta.Vault.keys/0.
Decrypts consecutive chunks of an object of size bytes of plaintext:
data holds the encrypted chunks from first_chunk on (see
ciphertext_range/4; the object's end may cut it short). Every chunk is
authenticated, so a wrong size, key, name or altered byte is :error.
Nothing but the chunks in data is read.
Decrypts an object made by encrypt_stream/4 chunk by chunk, reading it
through reader and folding each chunk's plaintext into acc only after
that chunk's authentication tag has been verified. The plaintext is never
held whole: memory use is one chunk (64 KiB) plus the reader's own buffer.
Encrypts a stream of plaintext binaries (any sizes, size bytes in all)
into a stream of encrypted iodata. Each enumeration draws a new salt.
Raises if the plaintext turns out longer or shorter than size.
The size of the encrypted object for size bytes of plaintext under key_id.
The plaintext size of a chunk of this object (all but the last chunk hold this much).
The most bytes the header of an object can take (key ids have at most 255 bytes).
Reads the header at the start of an object (max_header_size/0 bytes, or
the whole object when it is shorter): {:ok, info}, or :error. info
is what the functions below take.
Types
@type reader() :: %{ read: (non_neg_integer() -> {:ok, binary()} | :eof | {:error, term()}), close: (-> :ok) }
Reads the encrypted object at a position: the bytes from offset on
(whatever the reader gives), :eof, or an error. close/0 releases it.
Functions
@spec ciphertext_range(map(), non_neg_integer(), non_neg_integer(), non_neg_integer()) :: {non_neg_integer(), non_neg_integer()}
The bytes of the encrypted object ({first, last}, both included) that
hold the chunks first_chunk to last_chunk of an object of size bytes
of plaintext; last may lie past the end (the object ends first).
Decrypts an object made by encrypt_stream/4, with the keys of Karta.Vault.keys/0.
@spec decrypt_run( map(), String.t(), %{required(binary()) => binary()}, non_neg_integer(), non_neg_integer(), binary() ) :: {:ok, binary()} | :error
Decrypts consecutive chunks of an object of size bytes of plaintext:
data holds the encrypted chunks from first_chunk on (see
ciphertext_range/4; the object's end may cut it short). Every chunk is
authenticated, so a wrong size, key, name or altered byte is :error.
Nothing but the chunks in data is read.
@spec decrypt_stream( reader(), String.t(), %{required(binary()) => binary()}, acc, (binary(), acc -> {:ok, acc} | {:error, term()}) ) :: {:ok, acc} | {:error, term()} when acc: var
Decrypts an object made by encrypt_stream/4 chunk by chunk, reading it
through reader and folding each chunk's plaintext into acc only after
that chunk's authentication tag has been verified. The plaintext is never
held whole: memory use is one chunk (64 KiB) plus the reader's own buffer.
A stream that ends in the middle of a chunk, one cut at a chunk boundary
(its new last chunk is not sealed as last) or one with a chunk out of
order fails its tag: {:error, :undecryptable}, and nothing after the
damaged chunk is folded in. A stream too short to hold a chunk at all is
{:error, :truncated}. An error from the reader or from folder is
returned as it is, and ends the read.
@spec encrypt_stream( Enumerable.t(), non_neg_integer(), String.t(), {binary(), binary()} ) :: Enumerable.t()
Encrypts a stream of plaintext binaries (any sizes, size bytes in all)
into a stream of encrypted iodata. Each enumeration draws a new salt.
Raises if the plaintext turns out longer or shorter than size.
@spec encrypted_size(non_neg_integer(), binary()) :: pos_integer()
The size of the encrypted object for size bytes of plaintext under key_id.
@spec info_chunk_size(map()) :: pos_integer()
The plaintext size of a chunk of this object (all but the last chunk hold this much).
@spec max_header_size() :: pos_integer()
The most bytes the header of an object can take (key ids have at most 255 bytes).
Reads the header at the start of an object (max_header_size/0 bytes, or
the whole object when it is shorter): {:ok, info}, or :error. info
is what the functions below take.