Find a user by an encrypted email
A login form asks for an email, and the users table stores it encrypted. A randomised value is different every time it is encrypted, so a data store cannot find a row by it. Two ways make a field searchable by equality: a deterministic field, whose stored value is equal for equal plaintexts, and a beacon, a truncated HMAC stored beside a field that stays randomised.
Beacon or deterministic field
| Beacon | Deterministic field | |
|---|---|---|
| Stored value | Randomised, so its AAD can name the row, and a value copied into another row fails to decrypt | Equal for equal values, so its AAD names the field and never the row, and a value copied into another row decrypts there |
| Match | The truncated HMAC, with deliberate collisions | Always the exact value |
| Key | A beacon key, rotated separately from the data keys | The field’s own key |
| Cost | An extra column, and a decryption and filter of the candidates on every lookup | Nothing beyond the field |
| BigQuery | SQL cannot calculate a beacon; it matches beacons the application calculated | SQL decrypts the field and searches it by equality |
| FIPS | HMAC-SHA256 on the configured backend | AES-SIV, which is not FIPS-approved |
With the default of 16 bits, there are 65,536 possible beacons. A column of 1,000,000 distinct emails gives about 1,000,000 / 65,536, or 15, emails per beacon, so a lookup of one email returns about 15 rows. The application decrypts them and keeps the one equal to the email it looks for. How the bits trade rows to filter against what the column leaks is in Bits.
With kms-aead alone
The application keeps the key itself, as a key id and the key encrypted by the KMS, such as in its settings, and rotates it by hand:
- a deterministic field:
DeterministicCipherwith a key the KMS encrypts once, see Deterministic encryption; - a beacon:
KmsAeadEnvelope::beaconorBeaconMac, see Beacons. A rotation keeps both keys and queries with a beacon under each until a re-index, see Rotating a beacon key.
With a keyring
Each purpose has its own key versions, rotated centrally, and a lookup matches every version that still decrypts:
- a deterministic field: an encryptor’s
encrypt_deterministicon every write, andKeyring::deterministic_probesfor a lookup, under thedeterministicfeature, see Deterministic fields; - a beacon: an encryptor’s
encryptandbeaconin the same write, andKeyring::beacon_probesfor a lookup, see Beacons.
A lookup
- Normalise the value in its newtype, such as a lowercased email, so that the write and the lookup encode the same bytes, see Normalisation;
- compute the probes of the value looked for, one per key that still decrypts: with kms-aead alone one, or two during a rotation by hand until the re-index; with a keyring one per version;
- query by them:
WHERE email_beacon = ANY($1)on PostgreSQL with the probes bound as aBYTEA[], aninfilter on Firestore, which limits its number of values, see Queries across rotations; - for a beacon, decrypt every row found and keep the ones whose plaintext equals the value: an equal beacon never proves an equal value.
A deterministic field in BigQuery is queried in SQL, with one DETERMINISTIC_ENCRYPT per version,
see Query keyring values in BigQuery.
What to watch for
- Both ways leak which rows hold the same value, see What it leaks and What a beacon leaks. Neither suits a field with few distinct values, such as a status or a country;
- each rotation of a deterministic or beacon version adds a probe to every lookup until a sweep, see Rotation cadence.