Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make encryption buffer size adjustable #99

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions shadowsocks/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ type Writer struct {
byteWrapper bytes.Reader
// Number of plaintext bytes that are currently buffered.
pending int
// Maximum number of plaintext bytes to write in each chunk.
maxChunkSize int
// These are populated by init():
buf []byte
aead cipher.AEAD
Expand All @@ -61,14 +63,29 @@ type Writer struct {
// NewShadowsocksWriter creates a Writer that encrypts the given Writer using
// the shadowsocks protocol with the given shadowsocks cipher.
func NewShadowsocksWriter(writer io.Writer, ssCipher *Cipher) *Writer {
return &Writer{writer: writer, ssCipher: ssCipher, saltGenerator: RandomSaltGenerator}
return &Writer{
writer: writer,
ssCipher: ssCipher,
saltGenerator: RandomSaltGenerator,
maxChunkSize: payloadSizeMask,
}
}

// SetSaltGenerator sets the salt generator to be used. Must be called before the first write.
func (sw *Writer) SetSaltGenerator(saltGenerator SaltGenerator) {
sw.saltGenerator = saltGenerator
}

// SetMaxChunkSize sets the maximum number of bytes to encrypt as a chunk.
// Defaults to 2^16-1. Smaller values save memory but increase framing overhead.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PErhaps mention that it's capped at 2^16-1 as specified in the standard.

// Must be called before the first write.
func (sw *Writer) SetMaxChunkSize(maxChunkSize int) {
if maxChunkSize > payloadSizeMask || maxChunkSize <= 0 {
maxChunkSize = payloadSizeMask
}
sw.maxChunkSize = maxChunkSize
}

// init generates a random salt, sets up the AEAD object and writes
// the salt to the inner Writer.
func (sw *Writer) init() (err error) {
Expand All @@ -86,7 +103,7 @@ func (sw *Writer) init() (err error) {
// The maximum length message is the salt (first message only), length, length tag,
// payload, and payload tag.
sizeBufSize := 2 + sw.aead.Overhead()
maxPayloadBufSize := payloadSizeMask + sw.aead.Overhead()
maxPayloadBufSize := sw.maxChunkSize + sw.aead.Overhead()
sw.buf = make([]byte, len(salt)+sizeBufSize+maxPayloadBufSize)
// Store the salt at the start of sw.buf.
copy(sw.buf, salt)
Expand Down Expand Up @@ -165,7 +182,7 @@ func (sw *Writer) buffers() (sizeBuf, payloadBuf []byte) {
// followed by a variable-length payload block.
sizeBuf = sw.buf[saltSize : saltSize+2]
payloadStart := saltSize + 2 + sw.aead.Overhead()
payloadBuf = sw.buf[payloadStart : payloadStart+payloadSizeMask]
payloadBuf = sw.buf[payloadStart : payloadStart+sw.maxChunkSize]
return
}

Expand Down
54 changes: 54 additions & 0 deletions shadowsocks/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,57 @@ func TestLazyWriteConcurrentFlush(t *testing.T) {
t.Errorf("Wrong final content: %v", decrypted)
}
}

func TestChunkSizeIntegrity(t *testing.T) {
t.Parallel()
cipher := newTestCipher(t)

// Test extreme and reasonable values.
testChunkSizes := []int{
1, 2, 3, 4, 250, 256, 257, 1000, 4000, 8192, 16383,
}

const numWrites = 5

input := make([]byte, numWrites*16383)
for i := range input {
input[i] = byte(i) // Arbitrary test contents
}

for _, maxChunkSize := range testChunkSizes {
maxChunkSize := maxChunkSize
t.Run(fmt.Sprintf("maxChunkSize=%d", maxChunkSize), func(t *testing.T) {
t.Parallel()
connReader, connWriter := io.Pipe()
writer := NewShadowsocksWriter(connWriter, cipher)
writer.SetMaxChunkSize(maxChunkSize)
reader := NewShadowsocksReader(connReader, cipher)
go func() {
defer connWriter.Close()
if _, err := writer.Write(input[:numWrites*maxChunkSize]); err != nil {
t.Errorf("Failed Write: %v", err)
}
}()

// Check that all writes have the expected size and contents.
buf := make([]byte, 2*maxChunkSize)
for i := 0; i < numWrites; i++ {
n, err := reader.Read(buf)
if err != nil {
t.Errorf("Read failed at chunk %d: %v", i, err)
} else if n != maxChunkSize {
t.Errorf("Chunk %d has wrong size: %d", i, n)
} else if !bytes.Equal(input[i*maxChunkSize:][:maxChunkSize], buf[:n]) {
t.Errorf("Data mismatch at chunk %d", i)
}
}
// Check that the stream is closed cleanly.
n, err := reader.Read(buf)
if n != 0 {
t.Errorf("Got %d extra bytes", n)
} else if err != io.EOF {
t.Errorf("Wanted EOF, got %v", err)
}
})
}
}