Skip to content

Commit

Permalink
Merge pull request #127 from neicnordic/convenience_encryptedsegmentsize
Browse files Browse the repository at this point in the history
Add convenience function to return the size of an encrypted segment
  • Loading branch information
jbygdell authored Mar 11, 2024
2 parents adf6e1f + 27d98b6 commit 414034e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
24 changes: 24 additions & 0 deletions model/headers/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,30 @@ 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
}

// 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)
}

_, err = EncryptedSegmentSize(header, ([32]byte)(make([]byte, 32)))
if err == nil {
t.Errorf("EncryptedSegmentSize worked where it should fail: %v", err)
}

_, err = EncryptedSegmentSize(make([]byte, 2), readerSecretKey)
if err == nil {
t.Errorf("EncryptedSegmentSize worked where it should fail: %v", err)
}

}

0 comments on commit 414034e

Please sign in to comment.