-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaterial_test.go
More file actions
376 lines (328 loc) · 13.8 KB
/
Copy pathmaterial_test.go
File metadata and controls
376 lines (328 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package fee_test
import (
"bytes"
"io"
"math"
"testing"
"github.com/filecoin-project/go-fee"
"github.com/filecoin-project/go-fee/aeskw"
"github.com/filecoin-project/go-fee/aesstream"
"github.com/filecoin-project/go-fee/cose"
"github.com/stretchr/testify/require"
)
// blobLocationRow is the shape a metadata store persists per encrypted blob,
// modelled on the consumer this API exists for: the four BodyMaterial columns,
// the key-management columns that let the row's CEK be recovered, and the blob's
// size and location.
//
// The read half of every test below reads *only* from a value of this type. That
// is the point of the exercise: if the range path needed anything a store could
// not persist, these tests would not compile.
type blobLocationRow struct {
// Key management: the CEK wrapped under a store-held key, never the CEK.
regionWrappedCEK []byte
regionKeyVersion string
tenantRecipientKID string
// Body material. chunkSize is int64 rather than int because a SQL bigint is
// what a store round-trips, so the conversion is part of what is tested.
headerLen int64
baseNonce []byte
chunkSize int64
aad []byte
// Location.
size int64
}
// material rebuilds the BodyMaterial from the persisted columns, as a reader
// would after loading the row.
func (r blobLocationRow) material() fee.BodyMaterial {
return fee.BodyMaterial{
HeaderLen: r.headerLen,
BaseNonce: r.baseNonce,
ChunkSize: int(r.chunkSize),
AAD: r.aad,
}
}
// encryptWithMaterial seals plaintext under cek and returns the wire blob
// together with the material captured from the encrypt call — the write half of
// the store flow.
func encryptWithMaterial(t *testing.T, plaintext, cek []byte, recipients []fee.Recipient, opts ...fee.EncryptOption) ([]byte, fee.BodyMaterial) {
t.Helper()
// The material arrives before a single byte is read, which is what lets a
// writer record the row while the upload is still streaming.
enc, mat, err := fee.EncryptWithCEK(bytes.NewReader(plaintext), cek, recipients, opts...)
require.NoError(t, err)
blob, err := io.ReadAll(enc)
require.NoError(t, err)
require.NoError(t, enc.Close())
return blob, mat
}
// requireNoEnvelopeRead asserts that nothing below headerLen was fetched: the
// whole purpose of caching the material is that the envelope is never read again.
func requireNoEnvelopeRead(t *testing.T, r *recordingReaderAt, headerLen int64) {
t.Helper()
for _, rd := range r.reads {
require.GreaterOrEqualf(t, rd.off, headerLen,
"read at offset %d (%d bytes) fell inside the %d-byte envelope; the cached material was not used",
rd.off, rd.n, headerLen)
}
}
// TestIngotWriteReadFlow is the acceptance criterion for the whole feature: a
// writer encrypts an object and persists what a store column can hold, and a
// later reader serves arbitrary byte ranges from those columns alone — fetching
// ciphertext only, never the envelope.
func TestIngotWriteReadFlow(t *testing.T) {
const size = 4*rangeChunk + 123 // a partial final chunk
// --- write path -------------------------------------------------------
tenantKey := newX25519Key(t)
regionKEK := newKEK(t)
plaintext := patternBytes(size)
cek := newCEK(t)
regionWrapped, err := aeskw.Wrap(regionKEK, cek)
require.NoError(t, err)
blob, mat := encryptWithMaterial(t, plaintext, cek,
[]fee.Recipient{fee.NewECDHESRecipient(ecdhKID, tenantKey.PublicKey())},
fee.WithChunkSize(rangeChunk), fee.WithContentLength(size))
// The writer's copy of the CEK is done with; only the wrapped form persists.
clear(cek)
row := blobLocationRow{
regionWrappedCEK: regionWrapped,
regionKeyVersion: "region-key-v1",
tenantRecipientKID: string(ecdhKID),
headerLen: mat.HeaderLen,
baseNonce: mat.BaseNonce,
chunkSize: int64(mat.ChunkSize),
aad: mat.AAD,
size: int64(len(blob)),
}
t.Run("material describes the stored bytes", func(t *testing.T) {
// Cross-check against the envelope actually written, so an extraction bug
// on the encrypt side cannot hide behind a self-consistent round trip.
env, rest, err := cose.Decode(blob, cose.WithExpectedType(fee.EnvelopeType))
require.NoError(t, err)
iv, ok := env.Headers.Unprotected.Bytes(cose.HeaderLabelIV)
require.True(t, ok)
aad, err := env.EncStructure(nil)
require.NoError(t, err)
// One comparison over the whole value, so a field added to BodyMaterial
// cannot go unchecked here.
require.Equal(t, fee.BodyMaterial{
HeaderLen: int64(len(blob) - len(rest)),
BaseNonce: iv,
ChunkSize: rangeChunk,
AAD: aad,
}, mat)
})
t.Run("plaintext size from the row alone", func(t *testing.T) {
got, err := row.material().PlaintextSize(row.size)
require.NoError(t, err)
require.Equal(t, int64(size), got)
})
// --- read path --------------------------------------------------------
for name, tc := range map[string]struct{ off, length int64 }{
"whole object": {0, size},
"inside one chunk": {100, 50},
"aligned chunk": {rangeChunk, rangeChunk},
"crosses a boundary": {rangeChunk - 10, 20},
"partial final chunk": {4 * rangeChunk, 123},
"open-ended suffix": {size - 50, math.MaxInt64},
"single byte at start": {0, 1},
"single byte at end": {size - 1, 1},
"empty range at eof": {size, 0},
} {
t.Run(name, func(t *testing.T) {
// Everything from here reads the row, never mat or the envelope.
cek, err := aeskw.Unwrap(regionKEK, row.regionWrappedCEK)
require.NoError(t, err)
defer clear(cek)
recording := newRecordingReaderAt(t, blob)
r, err := fee.DecryptRangeWithMaterial(recording, row.size, row.material(),
cek, tc.off, tc.length)
require.NoError(t, err)
want := plaintext[tc.off : tc.off+clampLen(size, tc.off, tc.length)]
require.Equal(t, int64(len(want)), r.Len(), "Len must be known before reading")
require.Equal(t, int64(size), r.Size())
got, err := io.ReadAll(r)
require.NoError(t, err)
require.Equal(t, want, got)
requireNoEnvelopeRead(t, recording, row.headerLen)
})
}
}
// TestEncryptMaterialDoesNotAliasTheStream pins that the material handed back
// shares no backing array with the encryption still in flight: a caller that
// adjusts its copy — or a store that reuses the buffers it read a row into —
// cannot disturb the blob being produced.
func TestEncryptMaterialDoesNotAliasTheStream(t *testing.T) {
cek := newCEK(t)
plaintext := patternBytes(2 * rangeChunk)
rc, mat, err := fee.EncryptWithCEK(bytes.NewReader(plaintext), cek, nil,
fee.WithChunkSize(rangeChunk))
require.NoError(t, err)
defer rc.Close()
// Scribble on the caller's copy before a single byte is read, keeping the
// values a store would have persisted.
kept := fee.BodyMaterial{
HeaderLen: mat.HeaderLen,
BaseNonce: bytes.Clone(mat.BaseNonce),
ChunkSize: mat.ChunkSize,
AAD: bytes.Clone(mat.AAD),
}
mat.BaseNonce[0] ^= 0xff
mat.AAD[0] ^= 0xff
blob, err := io.ReadAll(rc)
require.NoError(t, err)
// The blob still decrypts under the pristine values, so the mutation never
// reached the cipher or the encoded header.
r, err := fee.DecryptRangeWithMaterial(bytes.NewReader(blob), int64(len(blob)),
kept, cek, 0, int64(len(plaintext)))
require.NoError(t, err)
got, err := io.ReadAll(r)
require.NoError(t, err)
require.Equal(t, plaintext, got)
}
// TestDecryptRangeWithMaterialEncrypt0 pins that material is envelope-form
// agnostic: a recipient-less COSE_Encrypt0 yields usable material with no flag
// and no special case, which is what lets BodyMaterial cache the finished AAD
// rather than a protected header plus a context discriminator.
func TestDecryptRangeWithMaterialEncrypt0(t *testing.T) {
const size = 2 * rangeChunk
plaintext := patternBytes(size)
cek := newCEK(t)
blob, mat := encryptWithMaterial(t, plaintext, cek, nil,
fee.WithChunkSize(rangeChunk), fee.WithContentLength(size))
// It really is the recipient-less form.
tag, err := cose.PeekTag(blob)
require.NoError(t, err)
require.Equal(t, cose.TagCOSEEncrypt0, tag)
recording := newRecordingReaderAt(t, blob)
r, err := fee.DecryptRangeWithMaterial(recording, int64(len(blob)), mat, cek, 10, 4000)
require.NoError(t, err)
got, err := io.ReadAll(r)
require.NoError(t, err)
require.Equal(t, plaintext[10:4010], got)
requireNoEnvelopeRead(t, recording, mat.HeaderLen)
}
// TestDecryptRangeWithMaterialMatchesEnvelopePath asserts the cached path and the
// envelope path are interchangeable — same bytes out for the same request, so
// caching is an optimisation and not a second behaviour to reason about.
func TestDecryptRangeWithMaterialMatchesEnvelopePath(t *testing.T) {
const size = 3*rangeChunk + 7
tenantKey := newX25519Key(t)
plaintext := patternBytes(size)
cek := newCEK(t)
blob, mat := encryptWithMaterial(t, plaintext, cek,
[]fee.Recipient{fee.NewECDHESRecipient(ecdhKID, tenantKey.PublicKey())},
fee.WithChunkSize(rangeChunk), fee.WithContentLength(size))
unwrapper := fee.NewECDHESUnwrapper(ecdhKID, tenantKey)
for _, off := range []int64{0, 1, rangeChunk - 1, rangeChunk, 2 * rangeChunk, size - 7} {
_, viaEnvelope := decryptRange(t, blob, unwrapper, off, 500)
r, err := fee.DecryptRangeWithMaterial(bytes.NewReader(blob), int64(len(blob)), mat, cek, off, 500)
require.NoError(t, err)
viaMaterial, err := io.ReadAll(r)
require.NoError(t, err)
require.Equalf(t, viaEnvelope, viaMaterial, "paths disagree at off=%d", off)
}
}
// TestBodyMaterialValidate covers the all-or-nothing rule a store mirrors before
// persisting a row: a partial record is refused rather than written and
// discovered unusable on some later read.
func TestBodyMaterialValidate(t *testing.T) {
good := fee.BodyMaterial{
HeaderLen: 128,
BaseNonce: make([]byte, aesstream.BaseNonceSize),
ChunkSize: rangeChunk,
AAD: []byte("enc-structure"),
}
require.NoError(t, good.Validate())
for name, mutate := range map[string]func(*fee.BodyMaterial){
"zero value": func(m *fee.BodyMaterial) { *m = fee.BodyMaterial{} },
"no header length": func(m *fee.BodyMaterial) { m.HeaderLen = 0 },
"negative header": func(m *fee.BodyMaterial) { m.HeaderLen = -1 },
"no base nonce": func(m *fee.BodyMaterial) { m.BaseNonce = nil },
"short base nonce": func(m *fee.BodyMaterial) { m.BaseNonce = make([]byte, 3) },
"no chunk size": func(m *fee.BodyMaterial) { m.ChunkSize = 0 },
"chunk size tiny": func(m *fee.BodyMaterial) { m.ChunkSize = aesstream.MinChunkSize - 1 },
"chunk size huge": func(m *fee.BodyMaterial) { m.ChunkSize = aesstream.MaxChunkSize + 1 },
"no aad": func(m *fee.BodyMaterial) { m.AAD = nil },
"empty (not nil) aad": func(m *fee.BodyMaterial) { m.AAD = []byte{} },
} {
t.Run(name, func(t *testing.T) {
m := good
mutate(&m)
require.ErrorIs(t, m.Validate(), fee.ErrIncompleteMaterial)
// The range entry point rejects it up front for the same reason,
// rather than letting it fail as an authentication error later.
_, err := fee.DecryptRangeWithMaterial(bytes.NewReader([]byte("blob")), 4096, m,
make([]byte, aesstream.KeySize), 0, 10)
require.ErrorIs(t, err, fee.ErrIncompleteMaterial)
})
}
}
// TestDecryptRangeWithMaterialPoisoned is the safety property that makes caching
// this material acceptable: a row that has drifted from the bytes on disk fails
// loudly. Because BaseNonce and the AAD are bound into every chunk's GCM tag and
// HeaderLen decides which bytes are read at all, a wrong value can only produce
// an unreadable object — never plausible but incorrect plaintext.
func TestDecryptRangeWithMaterialPoisoned(t *testing.T) {
const size = 3 * rangeChunk
plaintext := patternBytes(size)
cek := newCEK(t)
blob, mat := encryptWithMaterial(t, plaintext, cek, nil,
fee.WithChunkSize(rangeChunk), fee.WithContentLength(size))
for name, mutate := range map[string]func(m *fee.BodyMaterial){
"header length off by one": func(m *fee.BodyMaterial) { m.HeaderLen++ },
"wrong base nonce": func(m *fee.BodyMaterial) {
m.BaseNonce = bytes.Clone(m.BaseNonce)
m.BaseNonce[0] ^= 0xff
},
"tampered aad": func(m *fee.BodyMaterial) {
m.AAD = bytes.Clone(m.AAD)
m.AAD[len(m.AAD)-1] ^= 0xff
},
"wrong chunk size": func(m *fee.BodyMaterial) { m.ChunkSize = rangeChunk * 2 },
} {
t.Run(name, func(t *testing.T) {
poisoned := mat
mutate(&poisoned)
r, err := fee.DecryptRangeWithMaterial(bytes.NewReader(blob), int64(len(blob)),
poisoned, cek, 0, 200)
if err != nil {
return // rejected at construction, which is a fine outcome
}
got, err := io.ReadAll(r)
require.Error(t, err, "a poisoned row must not decrypt")
require.NotEqual(t, plaintext[:200], got)
})
}
}
// TestDecryptRangeWithMaterialInvalidArgs covers the argument checks that do not
// depend on the material being right.
func TestDecryptRangeWithMaterialInvalidArgs(t *testing.T) {
const size = 2 * rangeChunk
plaintext := patternBytes(size)
cek := newCEK(t)
blob, mat := encryptWithMaterial(t, plaintext, cek, nil,
fee.WithChunkSize(rangeChunk), fee.WithContentLength(size))
blobSize := int64(len(blob))
t.Run("short cek", func(t *testing.T) {
_, err := fee.DecryptRangeWithMaterial(bytes.NewReader(blob), blobSize, mat,
make([]byte, 16), 0, 10)
require.ErrorIs(t, err, fee.ErrInvalidCEK)
})
t.Run("nil blob", func(t *testing.T) {
_, err := fee.DecryptRangeWithMaterial(nil, blobSize, mat, cek, 0, 10)
require.Error(t, err)
})
t.Run("blob shorter than its envelope", func(t *testing.T) {
_, err := fee.DecryptRangeWithMaterial(bytes.NewReader(blob), mat.HeaderLen-1, mat, cek, 0, 10)
require.ErrorIs(t, err, aesstream.ErrCiphertextSize)
})
t.Run("offset past the end", func(t *testing.T) {
_, err := fee.DecryptRangeWithMaterial(bytes.NewReader(blob), blobSize, mat, cek, size+1, 10)
require.ErrorIs(t, err, aesstream.ErrRange)
})
t.Run("negative offset", func(t *testing.T) {
_, err := fee.DecryptRangeWithMaterial(bytes.NewReader(blob), blobSize, mat, cek, -1, 10)
require.ErrorIs(t, err, aesstream.ErrRange)
})
}