Skip to content

Commit

Permalink
Have NewCrypt4GHWriter() with the same parameters as before and creat…
Browse files Browse the repository at this point in the history
…e a new function NewCrypt4GHWriterWithRands() which is the one using previously generated WriterRands
  • Loading branch information
emm1R committed Aug 2, 2023
1 parent 082fb6a commit ceaf6c4
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 64 deletions.
2 changes: 1 addition & 1 deletion internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
127 changes: 72 additions & 55 deletions streaming/out.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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.
Expand Down
16 changes: 8 additions & 8 deletions streaming/streaming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit ceaf6c4

Please sign in to comment.