/// Keyed blind indexes for equality search over encrypted columns, after /// CipherSweet (paragonie/ciphersweet) and ankane/blind_index: a column /// stored encrypted can still be looked up by equality, because each row /// also stores a keyed hash — the blind index — of the value. /// /// import blindindex /// /// let key = blindindex.derive_key root "users" "email" /// blindindex.make ~length: 16 key (blindindex.normalize "Ada@Example.COM ") /// /// `make` is HMAC-SHA-256 of the value under the index key, truncated to /// `~length` bytes: the truncation is always the caller's explicit choice, /// because it sets the collision rate (the README has the numbers). /// `derive_key` gives each (table, column) pair its own key from one root /// key — HKDF-expand (RFC 5869) over HMAC-SHA-256 — so one column's /// indexes say nothing about another's. /// /// Normalization decides which values index alike: `normalize` trims and /// ASCII-lowercases, so " Ada " and "ada" share an index; `normalize_with` /// applies a chain of `Transform`s of your own. std/string has no Unicode /// normalization (NFC) and no case folding beyond ASCII: "café" in its two /// Unicode spellings indexes as two values, and "É" does not fold to "é". /// A column where that matters must normalize in the application — the /// same way on write and on lookup — before calling `make`. /// /// **What an index leaks, plainly:** equality (which rows share a value) /// and frequency (how often each occurs; a common value in a known /// distribution names itself). And an index confirms guesses: never index /// a field whose equality is secret — passwords, tokens, anything an /// attacker would enumerate. The README states the model in full. import std/bytes import std/crypto import std/list import std/string /// A normalization transform, applied to a value before it is indexed. /// `trim` and `lowercase` are the stock ones; any `fn(String) -> String` /// is one. pub type Transform = fn(String) -> String /// Trim leading and trailing whitespace (std/string's `trim`: Unicode /// whitespace). pub fn trim :: -> Transform { string.trim } /// Lowercase ASCII letters (std/string's `to_lower`): "ADA" is "ada", and /// "É" stays "É" — std has no case folding beyond ASCII. pub fn lowercase :: -> Transform { string.to_lower } /// `value` under each of `transforms`, in order. pub fn normalize_with :: transforms: List value: String -> String { list.fold transforms value fn v t { t v } } /// `trim`, then `lowercase`: " Ada " and "ada" index alike. pub fn normalize :: value: String -> String { normalize_with [trim (), lowercase ()] value } /// The length of a full HMAC-SHA-256 output in bytes: `make`'s `~length` /// ceiling. pub fn full_length :: -> Int { 32 } /// The index key of the (table, column) pair: 32 bytes, derived from /// `root_key` by HKDF-expand (RFC 5869 over HMAC-SHA-256; the root key is /// the pseudo-random key, so there is no extract step) with the info /// string /// /// "blindindex:v1:" <> length <> ":" <> table <> column /// /// where `length` is the table name's byte length. Prefixing it keeps the /// pairs ("a:b", "c") and ("a", "b:c") apart, which a plain "table:column" /// would not. The output is the first (and, at 32 bytes, only) block. /// /// `root_key` must be a high-entropy secret — 32 bytes from /// `random.bytes`, kept out of the database. Rotating it re-indexes every /// column; losing it loses the indexes; an attacker who has it can confirm /// guesses (see the README's leakage model). pub fn derive_key :: root_key: Bytes table: String column: String -> Bytes { let info = "blindindex:v1:" <> show (string.byte_length table) <> ":" <> table <> column // HKDF-expand's first block is HMAC(PRK, info <> 0x01); 32 output bytes // are exactly one block. crypto.hmac_sha256 root_key (bytes.concat (bytes.from_string info) (bytes.from_list [1])) } /// The blind index of the already-normalized `value` under `key`: /// HMAC-SHA-256, truncated to `~length` bytes. `~length` is 1 to 32 and is /// always explicit — it sets the collision rate, so it is the caller's /// choice, never a default's. Panics for a length outside [1, 32]. /// /// blindindex.make ~length: 16 key (blindindex.normalize email) pub fn make :: ~length: Int key: Bytes value: String -> Bytes { if length < 1 || length > 32 { panic "blindindex.make: the truncation length is in [1, 32], got {length}" } bytes.slice (crypto.hmac_sha256 key (bytes.from_string value)) 0 length }