packages
The Louise packages, each released on its own
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#29 · main · 3 min 39 s
0Étoiles
0Forks
3Issues
| 1 | /// Keyed blind indexes for equality search over encrypted columns, after |
| 2 | /// CipherSweet (paragonie/ciphersweet) and ankane/blind_index: a column |
| 3 | /// stored encrypted can still be looked up by equality, because each row |
| 4 | /// also stores a keyed hash — the blind index — of the value. |
| 5 | /// |
| 6 | /// import blindindex |
| 7 | /// |
| 8 | /// let key = blindindex.derive_key root "users" "email" |
| 9 | /// blindindex.make ~length: 16 key (blindindex.normalize "Ada@Example.COM ") |
| 10 | /// |
| 11 | /// `make` is HMAC-SHA-256 of the value under the index key, truncated to |
| 12 | /// `~length` bytes: the truncation is always the caller's explicit choice, |
| 13 | /// because it sets the collision rate (the README has the numbers). |
| 14 | /// `derive_key` gives each (table, column) pair its own key from one root |
| 15 | /// key — HKDF-expand (RFC 5869) over HMAC-SHA-256 — so one column's |
| 16 | /// indexes say nothing about another's. |
| 17 | /// |
| 18 | /// Normalization decides which values index alike: `normalize` trims and |
| 19 | /// ASCII-lowercases, so " Ada " and "ada" share an index; `normalize_with` |
| 20 | /// applies a chain of `Transform`s of your own. std/string has no Unicode |
| 21 | /// normalization (NFC) and no case folding beyond ASCII: "café" in its two |
| 22 | /// Unicode spellings indexes as two values, and "É" does not fold to "é". |
| 23 | /// A column where that matters must normalize in the application — the |
| 24 | /// same way on write and on lookup — before calling `make`. |
| 25 | /// |
| 26 | /// **What an index leaks, plainly:** equality (which rows share a value) |
| 27 | /// and frequency (how often each occurs; a common value in a known |
| 28 | /// distribution names itself). And an index confirms guesses: never index |
| 29 | /// a field whose equality is secret — passwords, tokens, anything an |
| 30 | /// attacker would enumerate. The README states the model in full. |
| 31 | |
| 32 | import std/bytes |
| 33 | import std/crypto |
| 34 | import std/list |
| 35 | import std/string |
| 36 | |
| 37 | /// A normalization transform, applied to a value before it is indexed. |
| 38 | /// `trim` and `lowercase` are the stock ones; any `fn(String) -> String` |
| 39 | /// is one. |
| 40 | pub type Transform = fn(String) -> String |
| 41 | |
| 42 | /// Trim leading and trailing whitespace (std/string's `trim`: Unicode |
| 43 | /// whitespace). |
| 44 | pub fn trim :: -> Transform |
| 45 | { |
| 46 | string.trim |
| 47 | } |
| 48 | |
| 49 | /// Lowercase ASCII letters (std/string's `to_lower`): "ADA" is "ada", and |
| 50 | /// "É" stays "É" — std has no case folding beyond ASCII. |
| 51 | pub fn lowercase :: -> Transform |
| 52 | { |
| 53 | string.to_lower |
| 54 | } |
| 55 | |
| 56 | /// `value` under each of `transforms`, in order. |
| 57 | pub fn normalize_with :: transforms: List<Transform> value: String -> String |
| 58 | { |
| 59 | list.fold transforms value fn v t { t v } |
| 60 | } |
| 61 | |
| 62 | /// `trim`, then `lowercase`: " Ada " and "ada" index alike. |
| 63 | pub fn normalize :: value: String -> String |
| 64 | { |
| 65 | normalize_with [trim (), lowercase ()] value |
| 66 | } |
| 67 | |
| 68 | /// The length of a full HMAC-SHA-256 output in bytes: `make`'s `~length` |
| 69 | /// ceiling. |
| 70 | pub fn full_length :: -> Int |
| 71 | { |
| 72 | 32 |
| 73 | } |
| 74 | |
| 75 | /// The index key of the (table, column) pair: 32 bytes, derived from |
| 76 | /// `root_key` by HKDF-expand (RFC 5869 over HMAC-SHA-256; the root key is |
| 77 | /// the pseudo-random key, so there is no extract step) with the info |
| 78 | /// string |
| 79 | /// |
| 80 | /// "blindindex:v1:" <> length <> ":" <> table <> column |
| 81 | /// |
| 82 | /// where `length` is the table name's byte length. Prefixing it keeps the |
| 83 | /// pairs ("a:b", "c") and ("a", "b:c") apart, which a plain "table:column" |
| 84 | /// would not. The output is the first (and, at 32 bytes, only) block. |
| 85 | /// |
| 86 | /// `root_key` must be a high-entropy secret — 32 bytes from |
| 87 | /// `random.bytes`, kept out of the database. Rotating it re-indexes every |
| 88 | /// column; losing it loses the indexes; an attacker who has it can confirm |
| 89 | /// guesses (see the README's leakage model). |
| 90 | pub fn derive_key :: root_key: Bytes table: String column: String -> Bytes |
| 91 | { |
| 92 | let info = |
| 93 | "blindindex:v1:" <> show (string.byte_length table) <> ":" <> table |
| 94 | <> column |
| 95 | // HKDF-expand's first block is HMAC(PRK, info <> 0x01); 32 output bytes |
| 96 | // are exactly one block. |
| 97 | crypto.hmac_sha256 |
| 98 | root_key |
| 99 | (bytes.concat (bytes.from_string info) (bytes.from_list [1])) |
| 100 | } |
| 101 | |
| 102 | /// The blind index of the already-normalized `value` under `key`: |
| 103 | /// HMAC-SHA-256, truncated to `~length` bytes. `~length` is 1 to 32 and is |
| 104 | /// always explicit — it sets the collision rate, so it is the caller's |
| 105 | /// choice, never a default's. Panics for a length outside [1, 32]. |
| 106 | /// |
| 107 | /// blindindex.make ~length: 16 key (blindindex.normalize email) |
| 108 | pub fn make :: ~length: Int key: Bytes value: String -> Bytes |
| 109 | { |
| 110 | if length < 1 || length > 32 |
| 111 | { |
| 112 | panic "blindindex.make: the truncation length is in [1, 32], got {length}" |
| 113 | } |
| 114 | bytes.slice (crypto.hmac_sha256 key (bytes.from_string value)) 0 length |
| 115 | } |