diff --git a/model/headers/headers.go b/model/headers/headers.go index 3b3eb03..50fff7b 100644 --- a/model/headers/headers.go +++ b/model/headers/headers.go @@ -214,21 +214,24 @@ func (h *Header) MarshalBinary() (data []byte, err error) { if err != nil { return nil, err } - for _, headerPacket := range h.HeaderPackets { + nonces := h.Nonces + for i, headerPacket := range h.HeaderPackets { if h.Nonces != nil { - headerPacket.Nonce = h.Nonces[0] - h.Nonces = h.Nonces[1:] + headerPacket.Nonce = nonces[i] } marshalledHeaderPacket, err := headerPacket.MarshalBinary() if err != nil { return nil, err } - h.Nonces = append(h.Nonces, headerPacket.Nonce) + if h.Nonces == nil { + nonces = append(nonces, headerPacket.Nonce) + } err = binary.Write(&buffer, binary.LittleEndian, marshalledHeaderPacket) if err != nil { return nil, err } } + h.Nonces = nonces return buffer.Bytes(), nil } diff --git a/streaming/out.go b/streaming/out.go index e7dcceb..697b590 100644 --- a/streaming/out.go +++ b/streaming/out.go @@ -28,6 +28,7 @@ type WriterRands struct { dataKey [chacha20poly1305.KeySize]byte headerNonces []*[chacha20poly1305.NonceSize]byte bodyNonces []*[chacha20poly1305.NonceSize]byte + replicate bool } // NewCrypt4GHWriter method constructs streaming.Crypt4GHWriter instance from io.Writer and corresponding keys. @@ -55,6 +56,7 @@ func NewCrypt4GHWriterWithRands(writer io.Writer, rands *WriterRands, ) (*Crypt4GHWriter, error) { crypt4GHWriter := Crypt4GHWriter{Rands: rands} + crypt4GHWriter.Rands.replicate = true err := crypt4GHWriter.init(writer, writerPrivateKey, readerPublicKeyList, dataEditList) if err != nil { @@ -182,7 +184,7 @@ func (c *Crypt4GHWriter) flushBuffer() error { DataEncryptionParametersHeaderPackets: []headers.DataEncryptionParametersHeaderPacket{c.dataEncryptionParametersHeaderPacket}, UnencryptedData: c.buffer.Bytes(), } - if c.Rands.bodyNonces != nil { + if c.Rands.replicate { segment.Nonce = c.Rands.bodyNonces[0] c.Rands.bodyNonces = c.Rands.bodyNonces[1:] }