From 6591be0a4adc004c184da91834195fc43acfa88e Mon Sep 17 00:00:00 2001 From: Emmi Rehn Date: Wed, 2 Aug 2023 14:49:35 +0300 Subject: [PATCH] Have NewCrypt4GHWriter() with the same parameters as before and create a new function NewCrypt4GHWriterWithRands() which is the one using previously generated WriterRands --- internal/cli/cli.go | 2 +- streaming/out.go | 127 ++++++++++++++++++++---------------- streaming/streaming_test.go | 16 ++--- 3 files changed, 81 insertions(+), 64 deletions(-) diff --git a/internal/cli/cli.go b/internal/cli/cli.go index da68a8f..53b2621 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -294,7 +294,7 @@ func encryptFile(privateKey [32]byte, pubkeyList [][32]byte) bool { return false } - crypt4GHWriter, err := streaming.NewCrypt4GHWriter(outFile, privateKey, pubkeyList, nil, nil) + crypt4GHWriter, err := streaming.NewCrypt4GHWriter(outFile, privateKey, pubkeyList, nil) if err != nil { fmt.Println(aurora.Red(err)) diff --git a/streaming/out.go b/streaming/out.go index 9ab74d4..728e7b6 100644 --- a/streaming/out.go +++ b/streaming/out.go @@ -30,69 +30,33 @@ type WriterRands struct { } // NewCrypt4GHWriter method constructs streaming.Crypt4GHWriter instance from io.Writer and corresponding keys. -func NewCrypt4GHWriter( - writer io.Writer, +func NewCrypt4GHWriter(writer io.Writer, writerPrivateKey [chacha20poly1305.KeySize]byte, readerPublicKeyList [][chacha20poly1305.KeySize]byte, dataEditList *headers.DataEditListHeaderPacket) (*Crypt4GHWriter, error) { + crypt4GHWriter := Crypt4GHWriter{Rands: &WriterRands{}} + _, err := rand.Read(crypt4GHWriter.Rands.dataKey[:]) + if err != nil { + return nil, err + } + + err = crypt4GHWriter.init(writer, writerPrivateKey, readerPublicKeyList, dataEditList) + if err != nil { + return nil, err + } + + return &crypt4GHWriter, nil +} + +func NewCrypt4GHWriterWithRands(writer io.Writer, writerPrivateKey [chacha20poly1305.KeySize]byte, readerPublicKeyList [][chacha20poly1305.KeySize]byte, dataEditList *headers.DataEditListHeaderPacket, rands *WriterRands, ) (*Crypt4GHWriter, error) { - crypt4GHWriter := Crypt4GHWriter{} - - if rands != nil { - crypt4GHWriter.Rands = rands - } else { - crypt4GHWriter.Rands = &WriterRands{} - _, err := rand.Read(crypt4GHWriter.Rands.dataKey[:]) - if err != nil { - return nil, err - } - } + crypt4GHWriter := Crypt4GHWriter{Rands: rands} - headerPackets := make([]headers.HeaderPacket, 0) - crypt4GHWriter.dataEncryptionParametersHeaderPacket = headers.DataEncryptionParametersHeaderPacket{ - EncryptedSegmentSize: chacha20poly1305.NonceSize + headers.UnencryptedDataSegmentSize + box.Overhead, - PacketType: headers.PacketType{PacketType: headers.DataEncryptionParameters}, - DataEncryptionMethod: headers.ChaCha20IETFPoly1305, - DataKey: crypt4GHWriter.Rands.dataKey, - } - - for _, readerPublicKey := range readerPublicKeyList { - headerPackets = append(headerPackets, headers.HeaderPacket{ - WriterPrivateKey: writerPrivateKey, - ReaderPublicKey: readerPublicKey, - HeaderEncryptionMethod: headers.X25519ChaCha20IETFPoly1305, - EncryptedHeaderPacket: crypt4GHWriter.dataEncryptionParametersHeaderPacket, - }) - if dataEditList != nil { - headerPackets = append(headerPackets, headers.HeaderPacket{ - WriterPrivateKey: writerPrivateKey, - ReaderPublicKey: readerPublicKey, - HeaderEncryptionMethod: headers.X25519ChaCha20IETFPoly1305, - EncryptedHeaderPacket: dataEditList, - }) - } - } - var magicNumber [8]byte - copy(magicNumber[:], headers.MagicNumber) - crypt4GHWriter.header = headers.Header{ - MagicNumber: magicNumber, - Version: headers.Version, - HeaderPacketCount: uint32(len(headerPackets)), - HeaderPackets: headerPackets, - Nonces: crypt4GHWriter.Rands.headerNonces, - } - binaryHeader, err := crypt4GHWriter.header.MarshalBinary() + err := crypt4GHWriter.init(writer, writerPrivateKey, readerPublicKeyList, dataEditList) if err != nil { return nil, err } - crypt4GHWriter.Rands.headerNonces = crypt4GHWriter.header.Nonces - _, err = writer.Write(binaryHeader) - if err != nil { - return nil, err - } - crypt4GHWriter.writer = writer - crypt4GHWriter.buffer.Grow(headers.UnencryptedDataSegmentSize) return &crypt4GHWriter, nil } @@ -126,7 +90,60 @@ func NewCrypt4GHWriterWithoutPrivateKey(writer io.Writer, readerPublicKeyList [] return nil, err } - return NewCrypt4GHWriter(writer, privateKey, readerPublicKeyList, dataEditList, nil) + return NewCrypt4GHWriter(writer, privateKey, readerPublicKeyList, dataEditList) +} + +func (c *Crypt4GHWriter) init(writer io.Writer, + writerPrivateKey [chacha20poly1305.KeySize]byte, + readerPublicKeyList [][chacha20poly1305.KeySize]byte, + dataEditList *headers.DataEditListHeaderPacket, +) error { + headerPackets := make([]headers.HeaderPacket, 0) + c.dataEncryptionParametersHeaderPacket = headers.DataEncryptionParametersHeaderPacket{ + EncryptedSegmentSize: chacha20poly1305.NonceSize + headers.UnencryptedDataSegmentSize + box.Overhead, + PacketType: headers.PacketType{PacketType: headers.DataEncryptionParameters}, + DataEncryptionMethod: headers.ChaCha20IETFPoly1305, + DataKey: c.Rands.dataKey, + } + + for _, readerPublicKey := range readerPublicKeyList { + headerPackets = append(headerPackets, headers.HeaderPacket{ + WriterPrivateKey: writerPrivateKey, + ReaderPublicKey: readerPublicKey, + HeaderEncryptionMethod: headers.X25519ChaCha20IETFPoly1305, + EncryptedHeaderPacket: c.dataEncryptionParametersHeaderPacket, + }) + if dataEditList != nil { + headerPackets = append(headerPackets, headers.HeaderPacket{ + WriterPrivateKey: writerPrivateKey, + ReaderPublicKey: readerPublicKey, + HeaderEncryptionMethod: headers.X25519ChaCha20IETFPoly1305, + EncryptedHeaderPacket: dataEditList, + }) + } + } + var magicNumber [8]byte + copy(magicNumber[:], headers.MagicNumber) + c.header = headers.Header{ + MagicNumber: magicNumber, + Version: headers.Version, + HeaderPacketCount: uint32(len(headerPackets)), + HeaderPackets: headerPackets, + Nonces: c.Rands.headerNonces, + } + binaryHeader, err := c.header.MarshalBinary() + if err != nil { + return err + } + c.Rands.headerNonces = c.header.Nonces + _, err = writer.Write(binaryHeader) + if err != nil { + return err + } + c.writer = writer + c.buffer.Grow(headers.UnencryptedDataSegmentSize) + + return nil } // Write method implements io.Writer.Write. diff --git a/streaming/streaming_test.go b/streaming/streaming_test.go index d84411f..a5d4aaa 100644 --- a/streaming/streaming_test.go +++ b/streaming/streaming_test.go @@ -86,7 +86,7 @@ func TestReencryption(t *testing.T) { readerPublicKeyList := [][chacha20poly1305.KeySize]byte{} readerPublicKeyList = append(readerPublicKeyList, readerPublicKey) buffer := bytes.Buffer{} - writer, err := NewCrypt4GHWriter(&buffer, writerPrivateKey, readerPublicKeyList, nil, nil) + writer, err := NewCrypt4GHWriter(&buffer, writerPrivateKey, readerPublicKeyList, nil) if err != nil { t.Error(err) } @@ -166,7 +166,7 @@ func TestReencryptionWithDataEditListInCrypt4GHWriterNoDiscard(t *testing.T) { buffer := bytes.Buffer{} readerPublicKeyList := [][chacha20poly1305.KeySize]byte{} readerPublicKeyList = append(readerPublicKeyList, readerPublicKey) - writer, err := NewCrypt4GHWriter(&buffer, writerPrivateKey, readerPublicKeyList, &dataEditListHeaderPacket, nil) + writer, err := NewCrypt4GHWriter(&buffer, writerPrivateKey, readerPublicKeyList, &dataEditListHeaderPacket) if err != nil { t.Error(err) } @@ -223,7 +223,7 @@ func TestReencryptionWithDataEditListInCrypt4GHReaderNoDiscard(t *testing.T) { buffer := bytes.Buffer{} readerPublicKeyList := [][chacha20poly1305.KeySize]byte{} readerPublicKeyList = append(readerPublicKeyList, readerPublicKey) - writer, err := NewCrypt4GHWriter(&buffer, writerPrivateKey, readerPublicKeyList, nil, nil) + writer, err := NewCrypt4GHWriter(&buffer, writerPrivateKey, readerPublicKeyList, nil) if err != nil { t.Error(err) } @@ -287,7 +287,7 @@ func TestReencryptionWithDataEditListAndDiscard(t *testing.T) { buffer := bytes.Buffer{} readerPublicKeyList := [][chacha20poly1305.KeySize]byte{} readerPublicKeyList = append(readerPublicKeyList, readerPublicKey) - writer, err := NewCrypt4GHWriter(&buffer, writerPrivateKey, readerPublicKeyList, nil, nil) + writer, err := NewCrypt4GHWriter(&buffer, writerPrivateKey, readerPublicKeyList, nil) if err != nil { t.Error(err) } @@ -471,7 +471,7 @@ func TestFileReEncryption(t *testing.T) { buffer := bytes.Buffer{} readerPublicKeyList := [][chacha20poly1305.KeySize]byte{} readerPublicKeyList = append(readerPublicKeyList, readerPublicKey) - writer, err := NewCrypt4GHWriter(&buffer, writerPrivateKey, readerPublicKeyList, nil, nil) + writer, err := NewCrypt4GHWriter(&buffer, writerPrivateKey, readerPublicKeyList, nil) if err != nil { t.Error(err) } @@ -541,7 +541,7 @@ func TestFileReEncryption(t *testing.T) { } } -func TestNewCrypt4GHWriterWithNonces(t *testing.T) { +func TestNewCrypt4GHWriterWithRands(t *testing.T) { inFile, err := os.Open("../test/sample.txt") if err != nil { t.Error(err) @@ -562,7 +562,7 @@ func TestNewCrypt4GHWriterWithNonces(t *testing.T) { if err != nil { return } - writer, err := NewCrypt4GHWriter(&buffer, privateKey, readerPublicKeyList, nil, nil) + writer, err := NewCrypt4GHWriter(&buffer, privateKey, readerPublicKeyList, nil) if err != nil { t.Error(err) } @@ -580,7 +580,7 @@ func TestNewCrypt4GHWriterWithNonces(t *testing.T) { } buffer2 := bytes.Buffer{} - writer2, err := NewCrypt4GHWriter(&buffer2, privateKey, readerPublicKeyList, nil, writer.Rands) + writer2, err := NewCrypt4GHWriterWithRands(&buffer2, privateKey, readerPublicKeyList, nil, writer.Rands) if err != nil { t.Error(err) }