Ecto.HMAC behaviour (Ecto v3.15.0)

Copy Markdown View Source

Defines a type that stores the HMAC of a value, allowing encrypted values to be looked up.

Values encrypted with Ecto.Encrypted can't be queried, as encrypting the same value twice gives different results. To look them up, store the HMAC of the value in a separate column, often called a blind index. The HMAC of a given value is always the same, so it can be queried and have unique indexes.

Usage

First define a module that uses Ecto.HMAC and implements the key/0 callback:

defmodule MyApp.Lookup do
  use Ecto.HMAC

  @impl true
  def key do
    %{algorithm: :sha256, key: Application.fetch_env!(:my_app, :hmac_key)}
  end
end

Then add two fields to your schema, one encrypted and one hashed:

schema "users" do
  field :email, MyApp.Encrypted, redact: true
  field :email_lookup, MyApp.Lookup, redact: true
end

Both columns must be binary ones. In migrations, use :binary.

The hashed field holds the original value until the struct is saved. After that, and when loaded from the database, it holds the HMAC.

Remember to pass redact: true to the field. Otherwise the original value may show up when inspecting the struct, which includes logs and error reports.

Both fields must be kept in sync in your changesets:

def changeset(user, attrs) do
  user
  |> cast(attrs, [:email])
  |> put_email_lookup()
end

defp put_email_lookup(changeset) do
  case fetch_change(changeset, :email) do
    {:ok, email} -> put_change(changeset, :email_lookup, email)
    :error -> changeset
  end
end

Now you can query by the hashed field. Values are hashed whenever they are dumped, which includes query parameters:

Repo.get_by(User, email_lookup: "jose@example.com")

Key

key/0 returns a map with the following fields:

  • :algorithm - the hash algorithm given to :crypto.mac/4, such as :sha256, :sha512, and :sha3_256
  • :key - the key itself, as a binary

Other fields are ignored.

The key must be random and must not be the same as any key used by Ecto.Encrypted. It must have at least as many bytes as the hash output, which is 32 bytes for :sha256 and :sha3_256 and 64 bytes for :sha512. You can generate one with:

$ elixir -e ':crypto.strong_rand_bytes(32) |> Base.encode64() |> IO.puts()'

Then store it in an environment variable and read it in your config/runtime.exs:

if config_env() == :prod do
  config :my_app,
    hmac_key: Base.decode64!(System.fetch_env!("HMAC_KEY"))
end

For development and test, you can set fixed keys in config/dev.exs and config/test.exs respectively. Use different keys per environment.

key/0 is called every time a value is dumped, so it must be cheap. That's why the example above decodes the key once, when the configuration is loaded, instead of on every call. Never store the key in a module attribute, as it would then be written to .beam files.

Unlike Ecto.Encrypted, there is a single key. Lookups can only use one key at a time, so changing the key or the algorithm requires computing all hashes again, by decrypting the encrypted column, before lookups will work.

Normalization

You may want lookups to ignore certain differences, such as case in emails. You cannot use sql functions on already hashed data, so implement encode/1, which normalizes the binary before hashing:

@impl true
def encode(email) when is_binary(email), do: {:ok, String.downcase(email)}
def encode(_), do: :error

As encode/1 is called both when writing and when querying, both are always normalized in the same way. The encrypted field still stores the original value.

Security

The HMAC reveals which rows have the same value and how often each value appears. Only hash fields you need to look up.

The HMAC protects values only while its key is secret. Once the key leaks, values with few possibilities, such as phone numbers, can be found by hashing all possibilities. So store the key separately from the database, as you would with encryption keys.

Using outside of Ecto

use Ecto.HMAC also defines hash/1 in your module, which returns the HMAC of the given binary. It skips encode/1.

Summary

Types

A hash algorithm supported by :crypto.mac/4 and a key

Callbacks

Encodes a value into the binary to be hashed.

Returns the hash algorithm and the key.

Functions

Returns the HMAC of binary with the given algorithm and key.

Types

key()

@type key() :: %{:algorithm => atom(), :key => binary(), optional(atom()) => term()}

A hash algorithm supported by :crypto.mac/4 and a key

Callbacks

encode(term)

@callback encode(term()) :: {:ok, binary()} | :error

Encodes a value into the binary to be hashed.

Defaults to accepting only binaries.

key()

@callback key() :: key()

Returns the hash algorithm and the key.

Functions

hash(arg1, binary)

@spec hash(key(), binary()) :: binary()

Returns the HMAC of binary with the given algorithm and key.

The result is HMAC(algorithm, key, "Ecto.HMAC" <> binary). The prefix ensures the result never matches HMACs computed elsewhere with the same key, such as the keys derived by Ecto.Encrypted.

Raises ArgumentError if key is invalid.