Encrypted fields for Ecto.
A vault is a module of your app that holds the keys:
defmodule MyApp.Vault do
use Strongbox
@impl true
def keys do
[%{tag: "k1", key: Application.fetch_env!(:my_app, :strongbox_key_k1)}]
end
@impl true
def index_keys do
[%{tag: "i1", key: Application.fetch_env!(:my_app, :strongbox_index_key_i1)}]
end
endThen a schema names it for its encrypted fields:
schema "users" do
field :ssn, Strongbox.Encrypted, vault: MyApp.Vault, redact: true
field :email, Strongbox.Encrypted, vault: MyApp.Vault, redact: true
field :email_index, Strongbox.Index, vault: MyApp.Vault, of: :email, normalize: [:trim, :downcase]
endor, with Strongbox.Schema, which never forgets redact: true:
use Strongbox.Schema, vault: MyApp.Vault
schema "users" do
encrypted_field :ssn
encrypted_field :email
blind_index :email_index, of: :email, normalize: [:trim, :downcase]
endA vault isn't a process: it starts nothing and holds no state. Its
callbacks are called for every value, so they must be cheap: read keys
that config/runtime.exs decoded once, never decode them in the callback.
The vault's functions
use Strongbox defines, in the vault:
encrypt(plaintext, opts)anddecrypt(ciphertext, opts)(anddecrypt!/2), for binaries outside Ecto. Options::context, a binary the value is bound to, which decrypting must give again; and:pad, a block size the value is padded to.hash(value, opts), a blind index of a binary with the first index key, andhashes(value, opts), one with each index key, for lookups while index keys rotate. Options asStrongbox.Index's::context,:bytes,:iterationsand:compat.stale?(ciphertext), whether a value was written with a key, or in a format, other than the first key's, so rotating would write it again.
Summary
Callbacks
The keys of blind indexes. The first writes, all are looked up.
The keys that encrypt and decrypt. The first encrypts, all decrypt.
Functions
Decrypts ciphertext with the key of vault it names.
Decrypts ciphertext, or raises Strongbox.DecryptionError.
Encrypts plaintext with the first key of vault.
The blind index of value with the first index key of vault.
The blind indexes of value, one with each index key of vault, without
repeats: what a lookup asks for while index keys rotate.
Whether ciphertext was written with a key or in a format other than
the first key's, so that rotating would write it again.
Callbacks
@callback index_keys() :: [Strongbox.Key.t() | map(), ...]
The keys of blind indexes. The first writes, all are looked up.
Each is a map (or a Strongbox.Key) with :tag and :key, and
optionally :algorithm. Never one of keys/0. Only needed for
Strongbox.Index.
@callback keys() :: [Strongbox.Key.t() | map(), ...]
The keys that encrypt and decrypt. The first encrypts, all decrypt.
Each is a map (or a Strongbox.Key) with :tag and :key, and
optionally :cipher and :format. See Strongbox.Key.
Functions
Decrypts ciphertext with the key of vault it names.
Returns :error when no key has its tag, when it was changed, or when
the :context isn't the one it was encrypted with.
Decrypts ciphertext, or raises Strongbox.DecryptionError.
Encrypts plaintext with the first key of vault.
The blind index of value with the first index key of vault.
The blind indexes of value, one with each index key of vault, without
repeats: what a lookup asks for while index keys rotate.
Whether ciphertext was written with a key or in a format other than
the first key's, so that rotating would write it again.