Skip to content

Commit

Permalink
Add convenience function to return the size of an encrypted segment
Browse files Browse the repository at this point in the history
in the datastream the header is from.
  • Loading branch information
pontus committed Mar 6, 2024
1 parent adf6e1f commit 6d57f37
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
23 changes: 23 additions & 0 deletions model/headers/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,29 @@ func NewHeaderPacket(reader io.Reader, readerPrivateKey [chacha20poly1305.KeySiz
return &headerPacket, nil
}

// EncryptedSegmentSize returns the size of an encrypted segment for a given header and reader private key.
func EncryptedSegmentSize(header []byte, readerPrivateKey [chacha20poly1305.KeySize]byte) (int, error) {

buffer := bytes.NewBuffer(header)
decryptedHeader, err := NewHeader(buffer, readerPrivateKey)
if err != nil {
return 0, err
}

dataEncryptionParametersHeaderPackets, err := decryptedHeader.GetDataEncryptionParameterHeaderPackets()
if err != nil {
return 0, err
}

firstDataEncryptionParametersHeader := (*dataEncryptionParametersHeaderPackets)[0]
for _, dataEncryptionParametersHeader := range *dataEncryptionParametersHeaderPackets {
if dataEncryptionParametersHeader.GetPacketType() != firstDataEncryptionParametersHeader.GetPacketType() {
return 0, fmt.Errorf("different data encryption methods are not supported")
}
}
return firstDataEncryptionParametersHeader.EncryptedSegmentSize, nil

Check failure on line 285 in model/headers/headers.go

View workflow job for this annotation

GitHub Actions / Check code (1.21)

return with no blank line before (nlreturn)
}

// MarshalBinary implements method MarshalBinary.BinaryMarshaler.
func (hp *HeaderPacket) MarshalBinary() (data []byte, err error) {
var encryptedMarshalledEncryptedHeaderPacket []byte
Expand Down
34 changes: 34 additions & 0 deletions model/headers/headers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,37 @@ func TestReEncryptedHeader(t *testing.T) {
t.Fail()
}
}

func TestEncryptedSegmentSize(t *testing.T) {
inFile, err := os.Open("../../test/sample.txt.enc")
if err != nil {
t.Errorf("Fileopen failed: %v", err)
}
readerSecretKey, err := keys.ReadPrivateKey(strings.NewReader(crypt4ghX25519Sec), []byte("password"))
if err != nil {
t.Errorf("ReadPrivateKey failed: %v", err)
}

header, err := ReadHeader(inFile)
if err != nil {
t.Errorf("ReadHeader failed: %v", err)
}

size, err := EncryptedSegmentSize(header, readerSecretKey)
if err != nil {
t.Errorf("EncryptedSegmentSize failed where it should work: %v", err)
} else if size != 65564 {
t.Errorf("EncryptedSegmentSize returned unexpected size %d (expected 65564)", size)
}

size, err = EncryptedSegmentSize(header, ([32]byte)(make([]byte, 32)))

Check failure on line 325 in model/headers/headers_test.go

View workflow job for this annotation

GitHub Actions / Check code (1.21)

ineffectual assignment to size (ineffassign)
if err == nil {
t.Errorf("EncryptedSegmentSize worked where it should fail: %v", err)
}

size, err = EncryptedSegmentSize(make([]byte, 2), readerSecretKey)

Check failure on line 330 in model/headers/headers_test.go

View workflow job for this annotation

GitHub Actions / Check code (1.21)

ineffectual assignment to size (ineffassign)
if err == nil {
t.Errorf("EncryptedSegmentSize worked where it should fail: %v", err)
}

}

0 comments on commit 6d57f37

Please sign in to comment.