// Known-answer vectors (RFC 4231 for the HMAC; the `derive_key` ones are // HKDF-expand outputs, computed with RFC 5869), determinism, key // separation and normalization; the properties run through gen. import blindindex import gen import gen/check import std/base64 import std/bytes as raw import std/random.{type random, bytes} import std/test.{assert, assert_equal} fn b64 :: text: String -> Bytes { match base64.decode text { Ok(b) -> b, Error(e) -> panic ("bad base64 in a test vector: " <> e) } } fn assert_bytes :: actual: Bytes expected: Bytes -> () { assert (actual == expected) ("expected " <> base64.encode expected <> ", got " <> base64.encode actual) } // The test root key: the bytes 0x00..0x1f. fn root :: -> Bytes { b64 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8=" } // ---- known answers ---------------------------------------------------------- // RFC 4231, test case 1: HMAC-SHA-256 of "Hi There" under 20 bytes of 0x0b. @test fn make_is_hmac_sha256_rfc4231_case_1 :: { assert_bytes ( blindindex.make ~length: 32 (b64 "CwsLCwsLCwsLCwsLCwsLCwsLCws=") "Hi There" ) (b64 "sDRMYdjbOFNcqK/OrwvxK4gdwgDJgz2nJuk3bC4yz/c=") } // RFC 4231, test case 2: "what do ya want for nothing?" under "Jefe". @test fn make_is_hmac_sha256_rfc4231_case_2 :: { assert_bytes { blindindex.make ~length: 32 (raw.from_string "Jefe") "what do ya want for nothing?" } (b64 "W9zBRr9gdU5qBCQmCJV1x1oAPwidJzmDnexYuWTsOEM=") } // Truncation takes the leading bytes: the first 16 of the same output. @test fn make_truncates_the_hmac :: { assert_bytes { blindindex.make ~length: 16 (raw.from_string "Jefe") "what do ya want for nothing?" } (b64 "W9zBRr9gdU5qBCQmCJV1xw==") } // HKDF-expand (RFC 5869) of the test root key, info strings // "blindindex:v1:5:usersemail" and "blindindex:v1:5:users" <> "ssn". @test fn derive_key_known_answers :: { assert_bytes (blindindex.derive_key (root ()) "users" "email") (b64 "h91zlOpKdyTO25zqt2Im32GN2nru+rDqz1bzstQi2+U=") assert_bytes (blindindex.derive_key (root ()) "users" "ssn") (b64 "72gOkFeck8uNHECuz9gl2HN+YRP5KmHzOGXKqP+uf3U=") } @test fn an_index_through_a_derived_key :: { let key = blindindex.derive_key (root ()) "users" "email" assert_bytes (blindindex.make ~length: 16 key "ada@example.com") (b64 "zF644zYBXrMA1K9MPGI/UA==") } // ---- key separation --------------------------------------------------------- @test fn each_column_has_its_own_key :: { let email = blindindex.derive_key (root ()) "users" "email" let ssn = blindindex.derive_key (root ()) "users" "ssn" let other_table = blindindex.derive_key (root ()) "people" "email" assert (email != ssn) "two columns of one table" assert (email != other_table) "one column of two tables" } // The info string length-prefixes the table: ("a:b", "c") and ("a", "b:c") // are different pairs, which a plain "table:column" would not keep apart. @test fn separator_characters_cannot_collide :: { assert { blindindex.derive_key (root ()) "a:b" "c" != blindindex.derive_key (root ()) "a" "b:c" } "a:b/c vs a/b:c" } // ---- normalization ---------------------------------------------------------- @test fn normalize_trims_and_lowercases :: { assert_equal (blindindex.normalize " Ada Lovelace \n") "ada lovelace" assert_equal (blindindex.normalize "ada") "ada" assert_equal (blindindex.normalize "") "" } // std/string lowercases ASCII only, and the index says so: "É" stays "É". @test fn normalize_folds_ascii_case_only :: { assert_equal (blindindex.normalize "CAFÉ") "cafÉ" } @test fn normalize_with_applies_a_chain_in_order :: { assert_equal (blindindex.normalize_with [] "as is") "as is" assert_equal ( blindindex.normalize_with [blindindex.trim (), blindindex.lowercase ()] " X " ) "x" let shout = fn s { s <> "!" } assert_equal (blindindex.normalize_with [shout] "a") "a!" } @test fn values_that_normalize_alike_index_alike :: { let key = blindindex.derive_key (root ()) "users" "email" assert_bytes ( blindindex.make ~length: 16 key (blindindex.normalize " Ada@Example.COM ") ) (blindindex.make ~length: 16 key "ada@example.com") } // ---- the length guard ------------------------------------------------------- @test @should_panic "the truncation length is in [1, 32]" fn make_refuses_an_empty_index :: { let _ = blindindex.make ~length: 0 (root ()) "ada@example.com" } @test @should_panic "the truncation length is in [1, 32]" fn make_refuses_more_than_an_hmac_has :: { let _ = blindindex.make ~length: 33 (root ()) "ada@example.com" } // ---- properties (gen) --------------------------------------------------------- fn values :: -> String / { gen.string ~alphabet: (gen.printable ()) ~min: 0 ~max: 40 } fn keys :: -> Bytes / { bytes 32 } fn names :: -> String / { gen.string ~alphabet: (gen.alnum ()) ~min: 1 ~max: 12 } fn lengths :: -> Int / { gen.int ~low: 1 ~high: 32 } fn cases :: -> (Bytes, String, Int) / { let k = keys () let v = values () let n = lengths () (k, v, n) } @test fn make_is_deterministic :: { check.seeded ~seed: 1 ~runs: 100 cases fn c { match c { (k, v, n) -> blindindex.make ~length: n k v == blindindex.make ~length: n k v } } } @test fn make_has_the_asked_length :: { check.seeded ~seed: 2 ~runs: 100 cases fn c { match c { (k, v, n) -> raw.length (blindindex.make ~length: n k v) == n } } } @test fn a_short_index_is_a_prefix_of_the_full_one :: { check.seeded ~seed: 3 ~runs: 100 cases fn c { match c { (k, v, n) -> blindindex.make ~length: n k v == raw.slice (blindindex.make ~length: 32 k v) 0 n } } } @test fn distinct_columns_have_distinct_keys :: { check.seeded ~seed: 4 ~runs: 100 fn { let root = keys () let table = names () let column = names () (root, table, column) } fn c { match c { (root, table, column) -> blindindex.derive_key root table (column <> "a") != blindindex.derive_key root table (column <> "b") && blindindex.derive_key root (table <> "a") column != blindindex.derive_key root (table <> "b") column } } } @test fn normalize_is_idempotent_and_ignores_outer_whitespace :: { check.seeded ~seed: 5 ~runs: 100 values fn v { blindindex.normalize (blindindex.normalize v) == blindindex.normalize v && blindindex.normalize (" " <> v <> "\t") == blindindex.normalize v } }