|
| 1 | +package encrypt_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/hex" |
| 7 | + "errors" |
| 8 | + "path/filepath" |
| 9 | + "reflect" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/cipherstash/stack/languages/golang/encrypt" |
| 13 | + "github.com/cipherstash/stack/languages/golang/internal/record" |
| 14 | +) |
| 15 | + |
| 16 | +type bindingCase struct { |
| 17 | + ID string `json:"id"` |
| 18 | + Label string `json:"label"` |
| 19 | + Scopes []contextPart `json:"scopes"` |
| 20 | + Bind []contextPart `json:"bind"` |
| 21 | +} |
| 22 | +type bindingVector struct { |
| 23 | + Descriptor string `json:"descriptor"` |
| 24 | + Equality string `json:"equality"` |
| 25 | + Ciphertext string `json:"ciphertext"` |
| 26 | + Contexts string `json:"contexts"` |
| 27 | +} |
| 28 | + |
| 29 | +// A binding is in the stored context, after the scopes, and in no query |
| 30 | +// context. Go seals and opens through Scope and Bind, and its term, its |
| 31 | +// contexts text and the Rust ciphertext agree with the Rust vectors. |
| 32 | +func TestBindingConformance(t *testing.T) { |
| 33 | + ctx := context.Background() |
| 34 | + client, err := encrypt.NewDeterministicClient(ctx, [32]byte{}) |
| 35 | + skipMissingConformanceGuest(t, err) |
| 36 | + if err != nil { |
| 37 | + t.Fatal(err) |
| 38 | + } |
| 39 | + t.Cleanup(func() { _ = client.Close() }) |
| 40 | + root := filepath.Join("..", "..", "..", "conformance", "stack-encrypt", "v1") |
| 41 | + var corpus struct { |
| 42 | + Version int `json:"version"` |
| 43 | + Profile string `json:"profile"` |
| 44 | + RequiredCapabilities []string `json:"required_capabilities"` |
| 45 | + Cases []bindingCase `json:"cases"` |
| 46 | + } |
| 47 | + readConformanceJSON(t, filepath.Join(root, "record-bindings-v1.cases.json"), &corpus) |
| 48 | + if corpus.Version != 1 || corpus.Profile != "record-bindings-v1" || len(corpus.Cases) == 0 || !reflect.DeepEqual(corpus.RequiredCapabilities, []string{"composition", "typed-extensions", "bindings", "frozen-open"}) { |
| 49 | + t.Fatal("unsupported bindings profile") |
| 50 | + } |
| 51 | + var expected struct { |
| 52 | + Version int `json:"version"` |
| 53 | + Profile string `json:"profile"` |
| 54 | + Producer string `json:"producer"` |
| 55 | + Cases map[string]bindingVector `json:"cases"` |
| 56 | + } |
| 57 | + readConformanceJSON(t, filepath.Join(root, "record-bindings-v1.expected.rust-native.json"), &expected) |
| 58 | + if expected.Version != 1 || expected.Profile != corpus.Profile || expected.Producer == "" || len(expected.Cases) != len(corpus.Cases) { |
| 59 | + t.Fatal("unsupported bindings vectors") |
| 60 | + } |
| 61 | + parts := func(t *testing.T, inputs []contextPart) []any { |
| 62 | + t.Helper() |
| 63 | + out := make([]any, 0, len(inputs)) |
| 64 | + for _, part := range inputs { |
| 65 | + switch part.Type { |
| 66 | + case "bytes": |
| 67 | + b, err := hex.DecodeString(part.Text) |
| 68 | + if err != nil { |
| 69 | + t.Fatal(err) |
| 70 | + } |
| 71 | + out = append(out, b) |
| 72 | + case "string", "int32", "int64", "uint64": |
| 73 | + out = append(out, conformanceValue(t, conformanceInput(part))) |
| 74 | + default: |
| 75 | + t.Fatal("unsupported context part") |
| 76 | + } |
| 77 | + } |
| 78 | + return out |
| 79 | + } |
| 80 | + seen := map[string]bool{} |
| 81 | + for _, c := range corpus.Cases { |
| 82 | + if c.ID == "" || seen[c.ID] { |
| 83 | + t.Fatal("empty or repeated case ID") |
| 84 | + } |
| 85 | + seen[c.ID] = true |
| 86 | + t.Run(c.ID, func(t *testing.T) { |
| 87 | + want, ok := expected.Cases[c.ID] |
| 88 | + if !ok { |
| 89 | + t.Fatal("missing vector") |
| 90 | + } |
| 91 | + decode := func(s string) []byte { |
| 92 | + t.Helper() |
| 93 | + b, e := hex.DecodeString(s) |
| 94 | + if e != nil { |
| 95 | + t.Fatal(e) |
| 96 | + } |
| 97 | + return b |
| 98 | + } |
| 99 | + label, e := record.ParseContext(c.Label) |
| 100 | + if e != nil { |
| 101 | + t.Fatal(e) |
| 102 | + } |
| 103 | + p := &record.Plan{Context: label, Fields: []record.Field{{Name: "text", Kind: record.String, Outputs: []record.Output{record.Ciphertext, record.Equality}}}} |
| 104 | + source := []record.Source{{"text": "hello"}} |
| 105 | + scoped := client.DefaultKeyset().Scope(parts(t, c.Scopes)...) |
| 106 | + bound := scoped.Bind(parts(t, c.Bind)...) |
| 107 | + |
| 108 | + // The cipher prints the contexts Rust pinned. |
| 109 | + shown, e := bound.Contexts(ctx, p) |
| 110 | + if e != nil { |
| 111 | + t.Fatal(e) |
| 112 | + } |
| 113 | + if shown != want.Contexts { |
| 114 | + t.Fatalf("contexts differ from Rust's:\n%s\nwant:\n%s", shown, want.Contexts) |
| 115 | + } |
| 116 | + |
| 117 | + // A fresh row's term is the pinned one: the binding is not in |
| 118 | + // the query context. |
| 119 | + fresh, e := bound.Seal(ctx, p, source) |
| 120 | + if e != nil { |
| 121 | + t.Fatal(e) |
| 122 | + } |
| 123 | + if !bytes.Equal(fresh[0]["text"].Terms[record.Equality], decode(want.Equality)) { |
| 124 | + t.Fatal("stored term differs") |
| 125 | + } |
| 126 | + // A query under the scopes alone derives it. A query through the |
| 127 | + // bound cipher is refused. |
| 128 | + query, e := scoped.QueryTerm(ctx, p, "text", "eq", "hello") |
| 129 | + if e != nil { |
| 130 | + t.Fatal(e) |
| 131 | + } |
| 132 | + if len(query.Terms) != 1 || !bytes.Equal(query.Terms[0], decode(want.Equality)) { |
| 133 | + t.Fatal("query differs") |
| 134 | + } |
| 135 | + if _, e := bound.QueryTerm(ctx, p, "text", "eq", "hello"); !errors.Is(e, encrypt.ErrEncoding) { |
| 136 | + t.Fatalf("a query with a binding: %v", e) |
| 137 | + } |
| 138 | + |
| 139 | + // The Rust ciphertext opens under the binding, and under no |
| 140 | + // other. A tampered binding and a binding left out both fail. |
| 141 | + frozen := []record.Sealed{{"text": {Ciphertext: decode(want.Ciphertext), Terms: map[record.Output][]byte{record.Equality: decode(want.Equality)}}}} |
| 142 | + for _, stored := range [][]record.Sealed{fresh, frozen} { |
| 143 | + opened, e := bound.Open(ctx, p, stored) |
| 144 | + if e != nil || !reflect.DeepEqual(opened, source) { |
| 145 | + t.Fatalf("open: %v; values differ: %v", e, !reflect.DeepEqual(opened, source)) |
| 146 | + } |
| 147 | + tampered := make([]any, len(c.Bind)) |
| 148 | + for i := range tampered { |
| 149 | + tampered[i] = "tampered" |
| 150 | + } |
| 151 | + if _, e := scoped.Bind(tampered...).Open(ctx, p, stored); e == nil { |
| 152 | + t.Fatal("a tampered binding opened the row") |
| 153 | + } |
| 154 | + if _, e := scoped.Open(ctx, p, stored); e == nil { |
| 155 | + t.Fatal("a binding left out opened the row") |
| 156 | + } |
| 157 | + } |
| 158 | + }) |
| 159 | + } |
| 160 | +} |
0 commit comments