Skip to content

Commit

Permalink
c.Rands.bodyNonces or h.Nonces were not nil for the entire writing of…
Browse files Browse the repository at this point in the history
… a header/body as later there was append()
  • Loading branch information
emm1R committed Aug 2, 2023
1 parent 9c89e87 commit b2a2c49
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
11 changes: 7 additions & 4 deletions model/headers/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion streaming/out.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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:]
}
Expand Down

0 comments on commit b2a2c49

Please sign in to comment.