| 1 | package seal |
| 2 | |
| 3 | import ( |
| 4 | "encoding/hex" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | // vectorDEK is a fixed, published key. It protects nothing; it exists so |
| 9 | // the derivation below is reproducible by anyone reading this file. |
| 10 | func vectorDEK() []byte { |
| 11 | k := make([]byte, 32) |
| 12 | for i := range k { |
| 13 | k[i] = byte(i) |
| 14 | } |
| 15 | return k |
| 16 | } |
| 17 | |
| 18 | // TestPurposeKeyVectors pins the key derivation. |
| 19 | // |
| 20 | // The package comment says the HKDF info string "must never be changed: it |
| 21 | // is baked into every byte already sealed" — and until this test nothing |
| 22 | // enforced that. Every other test in this package passes just as happily |
| 23 | // if "oficina/fichas:" becomes "oficina/fichas/", or if the salt changes, |
| 24 | // or if the hash does: round-trips still round-trip, prints are still |
| 25 | // deterministic, purposes are still separated. What breaks is only visible |
| 26 | // against an EXISTING database, at which point the corpus is unreadable |
| 27 | // and the change has shipped. |
| 28 | // |
| 29 | // Sheets has a test of this shape. These vectors are the whole contract: |
| 30 | // if one of them changes, every value already sealed by a deployed fichas |
| 31 | // is lost, and no other test in this repo will tell you. |
| 32 | func TestPurposeKeyVectors(t *testing.T) { |
| 33 | s, err := New(vectorDEK()) |
| 34 | if err != nil { |
| 35 | t.Fatalf("New: %v", err) |
| 36 | } |
| 37 | for purpose, want := range map[string]string{ |
| 38 | PurposeLabel: "957bfc889b61cbbb3acfe4a86500622c338a30e43d158068efd64fa69afbcb94", |
| 39 | PurposeValue: "43182def144d4b665177715e00b1cbcefae8f38f924393beb9332c131c9c4c0d", |
| 40 | PurposeEventPayload: "2252fd1b0ee0a87fc53d2090c39949bfcc402df882e2d6c63a96c30023d36358", |
| 41 | PurposeRefTarget: "d1390ea00de365f9cc6c87b049c7ee3ded5f1190ff7f01f74032d24986d40543", |
| 42 | printPurpose("f_email"): "2343a23362e9fdd244537a3375968e5b259e14b78436d230749fba3055766b8e", |
| 43 | refPrintPurpose: "f9723057c036c5b6973765134a02255dde2e0f34777c2abf729c5936a43da8b0", |
| 44 | } { |
| 45 | key, err := s.purposeKey(purpose) |
| 46 | if err != nil { |
| 47 | t.Fatalf("purposeKey(%q): %v", purpose, err) |
| 48 | } |
| 49 | if got := hex.EncodeToString(key); got != want { |
| 50 | t.Errorf("purposeKey(%q) = %s, want %s\n"+ |
| 51 | "This is not a test to update. Every value already sealed under this purpose "+ |
| 52 | "by a deployed instance is now unreadable.", purpose, got, want) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // The purpose strings themselves are part of the contract: renaming a |
| 58 | // constant's VALUE is the same break as changing the info string. |
| 59 | func TestPurposeNamesAreStable(t *testing.T) { |
| 60 | for got, want := range map[string]string{ |
| 61 | PurposeLabel: "type.label", |
| 62 | PurposeValue: "field.value", |
| 63 | PurposeEventPayload: "event.payload", |
| 64 | PurposeRefTarget: "ref.target", |
| 65 | printPurpose("f_email"): "field.print:f_email", |
| 66 | refPrintPurpose: "ref.print", |
| 67 | } { |
| 68 | if got != want { |
| 69 | t.Errorf("a purpose string changed: got %q, want %q", got, want) |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |