oficina / fichas Public

Clone
git clone https://amadan.net/oficina/fichas

Plain git — no account needed to clone.

Download

Download this file

1package seal
2
3import (
4 "bytes"
5 "strings"
6 "testing"
7)
8
9func testDEK(b byte) []byte {
10 k := make([]byte, 32)
11 for i := range k {
12 k[i] = b
13 }
14 return k
15}
16
17func newT(t *testing.T, b byte) *Sealer {
18 t.Helper()
19 s, err := New(testDEK(b))
20 if err != nil {
21 t.Fatalf("New: %v", err)
22 }
23 return s
24}
25
26func TestNewRejectsWrongDEKLength(t *testing.T) {
27 for _, n := range []int{0, 16, 31, 33, 64} {
28 if _, err := New(make([]byte, n)); err == nil {
29 t.Fatalf("New accepted a %d-byte DEK", n)
30 }
31 }
32}
33
34func TestSealOpenRoundTrip(t *testing.T) {
35 s := newT(t, 0x01)
36 want := []byte("Cnoc Teoranta — renewal due, spoke to Ana")
37 ct, err := s.Seal(PurposeValue, want)
38 if err != nil {
39 t.Fatalf("Seal: %v", err)
40 }
41 if bytes.Contains(ct, want) {
42 t.Fatal("ciphertext contains the plaintext")
43 }
44 got, err := s.Open(PurposeValue, ct)
45 if err != nil {
46 t.Fatalf("Open: %v", err)
47 }
48 if !bytes.Equal(got, want) {
49 t.Fatalf("round trip: got %q want %q", got, want)
50 }
51}
52
53// The whole reason purposes exist: adjacent columns of related tables must
54// not be interchangeable, so that a bug which swaps them fails loudly
55// instead of decrypting cleanly.
56func TestOpenUnderAnotherPurposeFails(t *testing.T) {
57 s := newT(t, 0x01)
58 ct, err := s.Seal(PurposeValue, []byte("Next action"))
59 if err != nil {
60 t.Fatalf("Seal: %v", err)
61 }
62 for _, p := range []string{PurposeLabel, PurposeEventPayload, PurposeRefTarget} {
63 if _, err := s.Open(p, ct); err == nil {
64 t.Fatalf("a value sealed as %q opened as %q", PurposeValue, p)
65 }
66 }
67}
68
69func TestOpenRejectsShortCiphertext(t *testing.T) {
70 s := newT(t, 0x01)
71 if _, err := s.Open(PurposeValue, []byte{1, 2, 3}); err == nil {
72 t.Fatal("Open accepted a ciphertext shorter than a nonce")
73 }
74}
75
76func TestSealIsNotDeterministic(t *testing.T) {
77 s := newT(t, 0x01)
78 a, _ := s.Seal(PurposeValue, []byte("same"))
79 b, _ := s.Seal(PurposeValue, []byte("same"))
80 if bytes.Equal(a, b) {
81 t.Fatal("two seals of one plaintext are identical: the nonce is not random")
82 }
83}
84
85func TestPrintIsDeterministicAndKeyed(t *testing.T) {
86 s := newT(t, 0x01)
87 other := newT(t, 0x02)
88 v := []byte("ana@example.com")
89
90 p1, err := s.Print("f_email", v)
91 if err != nil {
92 t.Fatalf("Print: %v", err)
93 }
94 p2, _ := s.Print("f_email", v)
95 if p1 != p2 {
96 t.Fatal("Print is not deterministic; equality lookups would miss")
97 }
98 if strings.Contains(p1, "example") {
99 t.Fatal("the print leaks the plaintext")
100 }
101
102 // Keyed, not a bare hash: another team's file must not confirm a guess.
103 if op, _ := other.Print("f_email", v); op == p1 {
104 t.Fatal("Print does not depend on the DEK: it is a confirmation oracle")
105 }
106}
107
108// Prints are scoped per field so that equality is not comparable across
109// fields — an observer must not learn that one card's value in one field
110// equals another card's value in a different one.
111func TestPrintIsScopedPerField(t *testing.T) {
112 s := newT(t, 0x01)
113 v := []byte("+353 1 555 0100")
114 a, _ := s.Print("f_phone", v)
115 b, _ := s.Print("f_fax", v)
116 if a == b {
117 t.Fatal("one value prints the same under two fields: prints are not scoped")
118 }
119}
120
121func TestRefPrintIsItsOwnPurpose(t *testing.T) {
122 s := newT(t, 0x01)
123 v := []byte("docs/acme/doc_412")
124 r, _ := s.RefPrint(v)
125 if f, _ := s.Print("f_ref", v); r == f {
126 t.Fatal("RefPrint collides with a field print")
127 }
128}
129