oficina / memoria Public

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

Plain git — no account needed to clone.

Download

Download this file

1package seal
2
3import (
4 "bytes"
5 "testing"
6)
7
8func testDEK() []byte {
9 d := make([]byte, 32)
10 for i := range d {
11 d[i] = byte(i)
12 }
13 return d
14}
15
16func TestRoundTrip(t *testing.T) {
17 s, err := New(testDEK())
18 if err != nil {
19 t.Fatal(err)
20 }
21 want := []byte("June revenue was 1,350,000")
22 ct, err := s.Seal(PurposeChunk, want)
23 if err != nil {
24 t.Fatal(err)
25 }
26 if bytes.Contains(ct, want) {
27 t.Fatal("ciphertext contains the plaintext")
28 }
29 got, err := s.Open(PurposeChunk, ct)
30 if err != nil {
31 t.Fatal(err)
32 }
33 if !bytes.Equal(got, want) {
34 t.Fatalf("round trip: got %q want %q", got, want)
35 }
36}
37
38// A chunk must not open as a title. The two are adjacent columns of
39// related rows; without HKDF purpose separation a swap would decrypt
40// cleanly and nothing would notice.
41func TestPurposesDoNotInterchange(t *testing.T) {
42 s, _ := New(testDEK())
43 ct, err := s.Seal(PurposeChunk, []byte("secret passage"))
44 if err != nil {
45 t.Fatal(err)
46 }
47 if _, err := s.Open(PurposeTitle, ct); err == nil {
48 t.Fatal("a chunk opened as a title; purpose separation is not working")
49 }
50}
51
52func TestRejectsWrongKeyLength(t *testing.T) {
53 for _, n := range []int{0, 16, 31, 33, 64} {
54 if _, err := New(make([]byte, n)); err == nil {
55 t.Errorf("accepted a %d-byte DEK", n)
56 }
57 }
58}
59
60// Two seals of the same plaintext must differ, or the store leaks
61// equality between passages — which over a corpus leaks structure.
62func TestNonceIsFresh(t *testing.T) {
63 s, _ := New(testDEK())
64 a, _ := s.Seal(PurposeChunk, []byte("same"))
65 b, _ := s.Seal(PurposeChunk, []byte("same"))
66 if bytes.Equal(a, b) {
67 t.Fatal("two seals of the same plaintext are identical")
68 }
69}
70