Skip to content

Commit

Permalink
Bugfix; incorrect length was used to check for discarded areas
Browse files Browse the repository at this point in the history
  • Loading branch information
pontus committed Dec 21, 2023
1 parent 7c6f163 commit fdb39f9
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// The version in the current branch
var Version = "1.8.6"
var Version = "1.8.7"

// If this is "" (empty string) then it means that it is a final release.
// Otherwise, this is a pre-release e.g. "dev", "beta", "rc1", etc.
Expand Down
8 changes: 5 additions & 3 deletions streaming/in.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (c *crypt4GHInternalReader) read(p []byte) (n int, err error) {
}

canRead := len(p[haveRead:])
remainingInBuffer := c.bufferUse - c.buffer.Len()
remainingInBuffer := c.buffer.Len()

if remainingInBuffer < canRead {
canRead = remainingInBuffer
Expand Down Expand Up @@ -297,9 +297,11 @@ func (c *crypt4GHInternalReader) read(p []byte) (n int, err error) {
haveRead++
}
} else {
// We can just read the rest of the buffer
// Read larger chunk from buffer. As precaution, limit to what we
// should be able to read only, as that is the bit we've checked
// if the discard list imposes any holes in

r, err := c.buffer.Read(p[haveRead:])
r, err := c.buffer.Read(p[haveRead : haveRead+canRead])
haveRead += r
c.streamPos += int64(r)

Expand Down
66 changes: 66 additions & 0 deletions streaming/streaming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,72 @@ func TestGetHeader(t *testing.T) {
}
}

func TestReencryptionWithDataEditListInCrypt4GHReaderDiscardStart(t *testing.T) {
inFile, err := os.Open("../test/sample.txt")
if err != nil {
t.Error(err)
}
writerPrivateKey, err := keys.ReadPrivateKey(strings.NewReader(sshEd25519SecEnc), []byte("123123"))
if err != nil {
t.Error(err)
}
readerPublicKey, err := keys.ReadPublicKey(strings.NewReader(crypt4ghX25519Pub))
if err != nil {
t.Error(err)
}
buffer := bytes.Buffer{}
readerPublicKeyList := [][chacha20poly1305.KeySize]byte{}
readerPublicKeyList = append(readerPublicKeyList, readerPublicKey)
writer, err := NewCrypt4GHWriter(&buffer, writerPrivateKey, readerPublicKeyList, nil)
if err != nil {
t.Error(err)
}
_, err = io.Copy(writer, inFile)
if err != nil {
t.Error(err)
}
err = inFile.Close()
if err != nil {
t.Error(err)
}
err = writer.Close()
if err != nil {
t.Error(err)
}

readerSecretKey, err := keys.ReadPrivateKey(strings.NewReader(crypt4ghX25519Sec), []byte("password"))
if err != nil {
t.Error(err)
}
dataEditListHeaderPacket := headers.DataEditListHeaderPacket{
PacketType: headers.PacketType{PacketType: headers.DataEditList},
NumberLengths: 3,
Lengths: []uint64{0, 100, 300},
}
reader, err := NewCrypt4GHReader(&buffer, readerSecretKey, &dataEditListHeaderPacket)
if err != nil {
t.Error(err)
}
all, err := io.ReadAll(reader)
if err != nil {
t.Error(err)
}
inFile, err = os.Open("../test/sample.txt")
if err != nil {
t.Error(err)
}
inBytes, err := io.ReadAll(inFile)
if err != nil {
t.Error(err)
}
if !bytes.Equal(all[:100], inBytes[:100]) {
t.Errorf("Different data before discard: %v vs %v", all[:100], inBytes[:100])
}
if !bytes.Equal(all[100:], inBytes[400:]) {
t.Errorf("Different data after discard: %v vs %v (truncated)", all[400:500], inBytes[100:200])
}
}

func TestNewCrypt4GHWriterWithoutPrivateKey(t *testing.T) {
inFile, err := os.Open("../test/sample.txt")
if err != nil {
Expand Down

0 comments on commit fdb39f9

Please sign in to comment.