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
|
package s3api
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"io"
"testing"
)
// TestCalculateIVWithOffset tests the calculateIVWithOffset function
func TestCalculateIVWithOffset(t *testing.T) {
baseIV := make([]byte, 16)
rand.Read(baseIV)
tests := []struct {
name string
offset int64
expectedSkip int
expectedBlock int64
}{
{"BlockAligned_0", 0, 0, 0},
{"BlockAligned_16", 16, 0, 1},
{"BlockAligned_32", 32, 0, 2},
{"BlockAligned_48", 48, 0, 3},
{"NonAligned_1", 1, 1, 0},
{"NonAligned_5", 5, 5, 0},
{"NonAligned_10", 10, 10, 0},
{"NonAligned_15", 15, 15, 0},
{"NonAligned_17", 17, 1, 1},
{"NonAligned_21", 21, 5, 1},
{"NonAligned_33", 33, 1, 2},
{"NonAligned_47", 47, 15, 2},
{"LargeOffset", 1000, 1000 % 16, 1000 / 16},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
adjustedIV, skip := calculateIVWithOffset(baseIV, tt.offset)
// Verify skip is correct
if skip != tt.expectedSkip {
t.Errorf("calculateIVWithOffset(%d) skip = %d, want %d", tt.offset, skip, tt.expectedSkip)
}
// Verify IV length is preserved
if len(adjustedIV) != 16 {
t.Errorf("calculateIVWithOffset(%d) IV length = %d, want 16", tt.offset, len(adjustedIV))
}
// Verify IV was adjusted correctly (last 8 bytes incremented by blockOffset)
if tt.expectedBlock == 0 {
if !bytes.Equal(adjustedIV, baseIV) {
t.Errorf("calculateIVWithOffset(%d) IV changed when blockOffset=0", tt.offset)
}
} else {
// IV should be different for non-zero block offsets
if bytes.Equal(adjustedIV, baseIV) {
t.Errorf("calculateIVWithOffset(%d) IV not changed when blockOffset=%d", tt.offset, tt.expectedBlock)
}
}
})
}
}
// TestCTRDecryptionWithNonBlockAlignedOffset tests that CTR decryption works correctly
// for non-block-aligned offsets (the critical bug fix)
func TestCTRDecryptionWithNonBlockAlignedOffset(t *testing.T) {
// Generate test data
plaintext := make([]byte, 1024)
for i := range plaintext {
plaintext[i] = byte(i % 256)
}
// Generate random key and IV
key := make([]byte, 32) // AES-256
iv := make([]byte, 16)
rand.Read(key)
rand.Read(iv)
// Encrypt the entire plaintext
block, err := aes.NewCipher(key)
if err != nil {
t.Fatalf("Failed to create cipher: %v", err)
}
ciphertext := make([]byte, len(plaintext))
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(ciphertext, plaintext)
// Test various offsets (both block-aligned and non-block-aligned)
testOffsets := []int64{0, 1, 5, 10, 15, 16, 17, 21, 32, 33, 47, 48, 100, 500}
for _, offset := range testOffsets {
t.Run(string(rune('A'+offset)), func(t *testing.T) {
// Calculate adjusted IV and skip
adjustedIV, skip := calculateIVWithOffset(iv, offset)
// CRITICAL: Start from the block-aligned offset, not the user offset
// CTR mode works on 16-byte blocks, so we need to decrypt from the block start
blockAlignedOffset := offset - int64(skip)
// Decrypt from the block-aligned offset
decryptBlock, err := aes.NewCipher(key)
if err != nil {
t.Fatalf("Failed to create decrypt cipher: %v", err)
}
decryptStream := cipher.NewCTR(decryptBlock, adjustedIV)
// Create a reader for the ciphertext starting at block-aligned offset
ciphertextFromBlockStart := ciphertext[blockAlignedOffset:]
decryptedFromBlockStart := make([]byte, len(ciphertextFromBlockStart))
decryptStream.XORKeyStream(decryptedFromBlockStart, ciphertextFromBlockStart)
// CRITICAL: Skip the intra-block bytes to get to the user-requested offset
if skip > 0 {
if skip > len(decryptedFromBlockStart) {
t.Fatalf("Skip %d exceeds decrypted data length %d", skip, len(decryptedFromBlockStart))
}
decryptedFromBlockStart = decryptedFromBlockStart[skip:]
}
// Rename for consistency
decryptedFromOffset := decryptedFromBlockStart
// Verify decrypted data matches original plaintext
expectedPlaintext := plaintext[offset:]
if !bytes.Equal(decryptedFromOffset, expectedPlaintext) {
t.Errorf("Decryption mismatch at offset %d (skip=%d)", offset, skip)
previewLen := 32
if len(expectedPlaintext) < previewLen {
previewLen = len(expectedPlaintext)
}
t.Errorf(" Expected first 32 bytes: %x", expectedPlaintext[:previewLen])
previewLen2 := 32
if len(decryptedFromOffset) < previewLen2 {
previewLen2 = len(decryptedFromOffset)
}
t.Errorf(" Got first 32 bytes: %x", decryptedFromOffset[:previewLen2])
// Find first mismatch
for i := 0; i < len(expectedPlaintext) && i < len(decryptedFromOffset); i++ {
if expectedPlaintext[i] != decryptedFromOffset[i] {
t.Errorf(" First mismatch at byte %d: expected %02x, got %02x", i, expectedPlaintext[i], decryptedFromOffset[i])
break
}
}
}
})
}
}
// TestCTRRangeRequestSimulation simulates a real-world S3 range request scenario
func TestCTRRangeRequestSimulation(t *testing.T) {
// Simulate uploading a 5MB object
objectSize := 5 * 1024 * 1024
plaintext := make([]byte, objectSize)
for i := range plaintext {
plaintext[i] = byte(i % 256)
}
// Encrypt the object
key := make([]byte, 32)
iv := make([]byte, 16)
rand.Read(key)
rand.Read(iv)
block, err := aes.NewCipher(key)
if err != nil {
t.Fatalf("Failed to create cipher: %v", err)
}
ciphertext := make([]byte, len(plaintext))
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(ciphertext, plaintext)
// Simulate various S3 range requests
rangeTests := []struct {
name string
start int64
end int64
}{
{"First byte", 0, 0},
{"First 100 bytes", 0, 99},
{"Mid-block range", 5, 100}, // Critical: starts at non-aligned offset
{"Single mid-block byte", 17, 17}, // Critical: single byte at offset 17
{"Cross-block range", 10, 50}, // Spans multiple blocks
{"Large range", 1000, 10000},
{"Tail range", int64(objectSize - 1000), int64(objectSize - 1)},
}
for _, rt := range rangeTests {
t.Run(rt.name, func(t *testing.T) {
rangeSize := rt.end - rt.start + 1
// Calculate adjusted IV and skip for the range start
adjustedIV, skip := calculateIVWithOffset(iv, rt.start)
// CRITICAL: Start decryption from block-aligned offset
blockAlignedStart := rt.start - int64(skip)
// Create decryption stream
decryptBlock, err := aes.NewCipher(key)
if err != nil {
t.Fatalf("Failed to create decrypt cipher: %v", err)
}
decryptStream := cipher.NewCTR(decryptBlock, adjustedIV)
// Decrypt from block-aligned start through the end of range
ciphertextFromBlock := ciphertext[blockAlignedStart : rt.end+1]
decryptedFromBlock := make([]byte, len(ciphertextFromBlock))
decryptStream.XORKeyStream(decryptedFromBlock, ciphertextFromBlock)
// CRITICAL: Skip intra-block bytes to get to user-requested start
if skip > 0 {
decryptedFromBlock = decryptedFromBlock[skip:]
}
decryptedRange := decryptedFromBlock
// Verify decrypted range matches original plaintext
expectedPlaintext := plaintext[rt.start : rt.end+1]
if !bytes.Equal(decryptedRange, expectedPlaintext) {
t.Errorf("Range decryption mismatch for %s (offset=%d, size=%d, skip=%d)",
rt.name, rt.start, rangeSize, skip)
previewLen := 64
if len(expectedPlaintext) < previewLen {
previewLen = len(expectedPlaintext)
}
t.Errorf(" Expected: %x", expectedPlaintext[:previewLen])
previewLen2 := previewLen
if len(decryptedRange) < previewLen2 {
previewLen2 = len(decryptedRange)
}
t.Errorf(" Got: %x", decryptedRange[:previewLen2])
}
})
}
}
// TestCTRDecryptionWithIOReader tests the integration with io.Reader
func TestCTRDecryptionWithIOReader(t *testing.T) {
plaintext := []byte("Hello, World! This is a test of CTR mode decryption with non-aligned offsets.")
key := make([]byte, 32)
iv := make([]byte, 16)
rand.Read(key)
rand.Read(iv)
// Encrypt
block, err := aes.NewCipher(key)
if err != nil {
t.Fatalf("Failed to create cipher: %v", err)
}
ciphertext := make([]byte, len(plaintext))
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(ciphertext, plaintext)
// Test reading from various offsets using io.Reader
testOffsets := []int64{0, 5, 10, 16, 17, 30}
for _, offset := range testOffsets {
t.Run(string(rune('A'+offset)), func(t *testing.T) {
// Calculate adjusted IV and skip
adjustedIV, skip := calculateIVWithOffset(iv, offset)
// CRITICAL: Start reading from block-aligned offset in ciphertext
blockAlignedOffset := offset - int64(skip)
// Create decrypted reader
decryptBlock, err := aes.NewCipher(key)
if err != nil {
t.Fatalf("Failed to create decrypt cipher: %v", err)
}
decryptStream := cipher.NewCTR(decryptBlock, adjustedIV)
ciphertextReader := bytes.NewReader(ciphertext[blockAlignedOffset:])
decryptedReader := &cipher.StreamReader{S: decryptStream, R: ciphertextReader}
// Skip intra-block bytes to get to user-requested offset
if skip > 0 {
_, err := io.CopyN(io.Discard, decryptedReader, int64(skip))
if err != nil {
t.Fatalf("Failed to skip %d bytes: %v", skip, err)
}
}
// Read decrypted data
decryptedData, err := io.ReadAll(decryptedReader)
if err != nil {
t.Fatalf("Failed to read decrypted data: %v", err)
}
// Verify
expectedPlaintext := plaintext[offset:]
if !bytes.Equal(decryptedData, expectedPlaintext) {
t.Errorf("Decryption mismatch at offset %d (skip=%d)", offset, skip)
t.Errorf(" Expected: %q", expectedPlaintext)
t.Errorf(" Got: %q", decryptedData)
}
})
}
}
|