Strongbox behaviour (Strongbox v0.1.0-dev)

Copy Markdown View Source

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
end

Then 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]
end

or, 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]
end

A 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) and decrypt(ciphertext, opts) (and decrypt!/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, and hashes(value, opts), one with each index key, for lookups while index keys rotate. Options as Strongbox.Index's: :context, :bytes, :iterations and :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.

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

index_keys()

(optional)
@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.

keys()

@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

decrypt(vault, ciphertext, opts \\ [])

@spec decrypt(module(), binary(), keyword()) :: {:ok, binary()} | :error

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.

decrypt!(vault, ciphertext, opts \\ [])

@spec decrypt!(module(), binary(), keyword()) :: binary()

Decrypts ciphertext, or raises Strongbox.DecryptionError.

encrypt(vault, plaintext, opts \\ [])

@spec encrypt(module(), binary(), keyword()) :: binary()

Encrypts plaintext with the first key of vault.

hash(vault, value, opts \\ [])

@spec hash(module(), binary(), keyword()) :: binary()

The blind index of value with the first index key of vault.

hashes(vault, value, opts \\ [])

@spec hashes(module(), binary(), keyword()) :: [binary(), ...]

The blind indexes of value, one with each index key of vault, without repeats: what a lookup asks for while index keys rotate.

stale?(vault, ciphertext)

@spec stale?(module(), binary()) :: boolean()

Whether ciphertext was written with a key or in a format other than the first key's, so that rotating would write it again.