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#31 · main · 4 min 11 s
0Étoiles
0Forks
3Issues
| 1 | // Known-answer vectors (RFC 4231 for the HMAC; the `derive_key` ones are |
| 2 | // HKDF-expand outputs, computed with RFC 5869), determinism, key |
| 3 | // separation and normalization; the properties run through gen. |
| 4 | |
| 5 | import blindindex |
| 6 | import gen |
| 7 | import gen/check |
| 8 | import std/base64 |
| 9 | import std/bytes as raw |
| 10 | import std/random.{type random, bytes} |
| 11 | import std/test.{assert, assert_equal} |
| 12 | |
| 13 | fn b64 :: text: String -> Bytes |
| 14 | { |
| 15 | match base64.decode text |
| 16 | { Ok(b) -> b, Error(e) -> panic ("bad base64 in a test vector: " <> e) } |
| 17 | } |
| 18 | |
| 19 | fn assert_bytes :: actual: Bytes expected: Bytes -> () |
| 20 | { |
| 21 | assert |
| 22 | (actual == expected) |
| 23 | ("expected " <> base64.encode expected <> ", got " <> base64.encode actual) |
| 24 | } |
| 25 | |
| 26 | // The test root key: the bytes 0x00..0x1f. |
| 27 | fn root :: -> Bytes |
| 28 | { |
| 29 | b64 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8=" |
| 30 | } |
| 31 | |
| 32 | // ---- known answers ---------------------------------------------------------- |
| 33 | |
| 34 | // RFC 4231, test case 1: HMAC-SHA-256 of "Hi There" under 20 bytes of 0x0b. |
| 35 | @test |
| 36 | fn make_is_hmac_sha256_rfc4231_case_1 :: |
| 37 | { |
| 38 | assert_bytes |
| 39 | ( blindindex.make |
| 40 | ~length: 32 |
| 41 | (b64 "CwsLCwsLCwsLCwsLCwsLCwsLCws=") |
| 42 | "Hi There" |
| 43 | ) |
| 44 | (b64 "sDRMYdjbOFNcqK/OrwvxK4gdwgDJgz2nJuk3bC4yz/c=") |
| 45 | } |
| 46 | |
| 47 | // RFC 4231, test case 2: "what do ya want for nothing?" under "Jefe". |
| 48 | @test |
| 49 | fn make_is_hmac_sha256_rfc4231_case_2 :: |
| 50 | { |
| 51 | assert_bytes |
| 52 | { |
| 53 | blindindex.make |
| 54 | ~length: 32 |
| 55 | (raw.from_string "Jefe") |
| 56 | "what do ya want for nothing?" |
| 57 | } |
| 58 | (b64 "W9zBRr9gdU5qBCQmCJV1x1oAPwidJzmDnexYuWTsOEM=") |
| 59 | } |
| 60 | |
| 61 | // Truncation takes the leading bytes: the first 16 of the same output. |
| 62 | @test |
| 63 | fn make_truncates_the_hmac :: |
| 64 | { |
| 65 | assert_bytes |
| 66 | { |
| 67 | blindindex.make |
| 68 | ~length: 16 |
| 69 | (raw.from_string "Jefe") |
| 70 | "what do ya want for nothing?" |
| 71 | } |
| 72 | (b64 "W9zBRr9gdU5qBCQmCJV1xw==") |
| 73 | } |
| 74 | |
| 75 | // HKDF-expand (RFC 5869) of the test root key, info strings |
| 76 | // "blindindex:v1:5:usersemail" and "blindindex:v1:5:users" <> "ssn". |
| 77 | @test |
| 78 | fn derive_key_known_answers :: |
| 79 | { |
| 80 | assert_bytes |
| 81 | (blindindex.derive_key (root ()) "users" "email") |
| 82 | (b64 "h91zlOpKdyTO25zqt2Im32GN2nru+rDqz1bzstQi2+U=") |
| 83 | assert_bytes |
| 84 | (blindindex.derive_key (root ()) "users" "ssn") |
| 85 | (b64 "72gOkFeck8uNHECuz9gl2HN+YRP5KmHzOGXKqP+uf3U=") |
| 86 | } |
| 87 | |
| 88 | @test |
| 89 | fn an_index_through_a_derived_key :: |
| 90 | { |
| 91 | let key = blindindex.derive_key (root ()) "users" "email" |
| 92 | assert_bytes |
| 93 | (blindindex.make ~length: 16 key "ada@example.com") |
| 94 | (b64 "zF644zYBXrMA1K9MPGI/UA==") |
| 95 | } |
| 96 | |
| 97 | // ---- key separation --------------------------------------------------------- |
| 98 | |
| 99 | @test |
| 100 | fn each_column_has_its_own_key :: |
| 101 | { |
| 102 | let email = blindindex.derive_key (root ()) "users" "email" |
| 103 | let ssn = blindindex.derive_key (root ()) "users" "ssn" |
| 104 | let other_table = blindindex.derive_key (root ()) "people" "email" |
| 105 | assert (email != ssn) "two columns of one table" |
| 106 | assert (email != other_table) "one column of two tables" |
| 107 | } |
| 108 | |
| 109 | // The info string length-prefixes the table: ("a:b", "c") and ("a", "b:c") |
| 110 | // are different pairs, which a plain "table:column" would not keep apart. |
| 111 | @test |
| 112 | fn separator_characters_cannot_collide :: |
| 113 | { |
| 114 | assert |
| 115 | { |
| 116 | blindindex.derive_key (root ()) "a:b" "c" |
| 117 | != blindindex.derive_key (root ()) "a" "b:c" |
| 118 | } |
| 119 | "a:b/c vs a/b:c" |
| 120 | } |
| 121 | |
| 122 | // ---- normalization ---------------------------------------------------------- |
| 123 | |
| 124 | @test |
| 125 | fn normalize_trims_and_lowercases :: |
| 126 | { |
| 127 | assert_equal (blindindex.normalize " Ada Lovelace \n") "ada lovelace" |
| 128 | assert_equal (blindindex.normalize "ada") "ada" |
| 129 | assert_equal (blindindex.normalize "") "" |
| 130 | } |
| 131 | |
| 132 | // std/string lowercases ASCII only, and the index says so: "É" stays "É". |
| 133 | @test |
| 134 | fn normalize_folds_ascii_case_only :: |
| 135 | { |
| 136 | assert_equal (blindindex.normalize "CAFÉ") "cafÉ" |
| 137 | } |
| 138 | |
| 139 | @test |
| 140 | fn normalize_with_applies_a_chain_in_order :: |
| 141 | { |
| 142 | assert_equal (blindindex.normalize_with [] "as is") "as is" |
| 143 | assert_equal |
| 144 | ( blindindex.normalize_with |
| 145 | [blindindex.trim (), blindindex.lowercase ()] |
| 146 | " X " |
| 147 | ) |
| 148 | "x" |
| 149 | let shout = fn s { s <> "!" } |
| 150 | assert_equal (blindindex.normalize_with [shout] "a") "a!" |
| 151 | } |
| 152 | |
| 153 | @test |
| 154 | fn values_that_normalize_alike_index_alike :: |
| 155 | { |
| 156 | let key = blindindex.derive_key (root ()) "users" "email" |
| 157 | assert_bytes |
| 158 | ( blindindex.make |
| 159 | ~length: 16 |
| 160 | key |
| 161 | (blindindex.normalize " Ada@Example.COM ") |
| 162 | ) |
| 163 | (blindindex.make ~length: 16 key "ada@example.com") |
| 164 | } |
| 165 | |
| 166 | // ---- the length guard ------------------------------------------------------- |
| 167 | |
| 168 | @test |
| 169 | @should_panic "the truncation length is in [1, 32]" |
| 170 | fn make_refuses_an_empty_index :: |
| 171 | { |
| 172 | let _ = blindindex.make ~length: 0 (root ()) "ada@example.com" |
| 173 | } |
| 174 | |
| 175 | @test |
| 176 | @should_panic "the truncation length is in [1, 32]" |
| 177 | fn make_refuses_more_than_an_hmac_has :: |
| 178 | { |
| 179 | let _ = blindindex.make ~length: 33 (root ()) "ada@example.com" |
| 180 | } |
| 181 | |
| 182 | // ---- properties (gen) --------------------------------------------------------- |
| 183 | |
| 184 | fn values :: -> String / <random> |
| 185 | { |
| 186 | gen.string ~alphabet: (gen.printable ()) ~min: 0 ~max: 40 |
| 187 | } |
| 188 | |
| 189 | fn keys :: -> Bytes / <random> |
| 190 | { |
| 191 | bytes 32 |
| 192 | } |
| 193 | |
| 194 | fn names :: -> String / <random> |
| 195 | { |
| 196 | gen.string ~alphabet: (gen.alnum ()) ~min: 1 ~max: 12 |
| 197 | } |
| 198 | |
| 199 | fn lengths :: -> Int / <random> |
| 200 | { |
| 201 | gen.int ~low: 1 ~high: 32 |
| 202 | } |
| 203 | |
| 204 | fn cases :: -> (Bytes, String, Int) / <random> |
| 205 | { |
| 206 | let k = keys () |
| 207 | let v = values () |
| 208 | let n = lengths () |
| 209 | (k, v, n) |
| 210 | } |
| 211 | |
| 212 | @test |
| 213 | fn make_is_deterministic :: |
| 214 | { |
| 215 | check.seeded |
| 216 | ~seed: 1 |
| 217 | ~runs: 100 |
| 218 | cases |
| 219 | fn c |
| 220 | { |
| 221 | match c |
| 222 | { (k, v, n) -> |
| 223 | blindindex.make ~length: n k v == blindindex.make ~length: n k v |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | @test |
| 229 | fn make_has_the_asked_length :: |
| 230 | { |
| 231 | check.seeded |
| 232 | ~seed: 2 |
| 233 | ~runs: 100 |
| 234 | cases |
| 235 | fn c |
| 236 | { |
| 237 | match c { (k, v, n) -> raw.length (blindindex.make ~length: n k v) == n } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | @test |
| 242 | fn a_short_index_is_a_prefix_of_the_full_one :: |
| 243 | { |
| 244 | check.seeded |
| 245 | ~seed: 3 |
| 246 | ~runs: 100 |
| 247 | cases |
| 248 | fn c |
| 249 | { |
| 250 | match c |
| 251 | { (k, v, n) -> |
| 252 | blindindex.make ~length: n k v |
| 253 | == raw.slice (blindindex.make ~length: 32 k v) 0 n |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | @test |
| 259 | fn distinct_columns_have_distinct_keys :: |
| 260 | { |
| 261 | check.seeded |
| 262 | ~seed: 4 |
| 263 | ~runs: 100 |
| 264 | fn |
| 265 | { |
| 266 | let root = keys () |
| 267 | let table = names () |
| 268 | let column = names () |
| 269 | (root, table, column) |
| 270 | } |
| 271 | fn c |
| 272 | { |
| 273 | match c |
| 274 | { (root, table, column) -> |
| 275 | blindindex.derive_key root table (column <> "a") |
| 276 | != blindindex.derive_key root table (column <> "b") |
| 277 | && blindindex.derive_key root (table <> "a") column |
| 278 | != blindindex.derive_key root (table <> "b") column |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | @test |
| 284 | fn normalize_is_idempotent_and_ignores_outer_whitespace :: |
| 285 | { |
| 286 | check.seeded |
| 287 | ~seed: 5 |
| 288 | ~runs: 100 |
| 289 | values |
| 290 | fn v |
| 291 | { |
| 292 | blindindex.normalize (blindindex.normalize v) == blindindex.normalize v |
| 293 | && blindindex.normalize (" " <> v <> "\t") == blindindex.normalize v |
| 294 | } |
| 295 | } |