Skip to content

Commit

Permalink
Do not store nonces in Header
Browse files Browse the repository at this point in the history
  • Loading branch information
emm1R committed Aug 2, 2023
1 parent b2a2c49 commit 490c5ca
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
15 changes: 3 additions & 12 deletions model/headers/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ type Header struct {
Version uint32
HeaderPacketCount uint32
HeaderPackets []HeaderPacket
Nonces []*[chacha20poly1305.NonceSize]byte
}

type HeaderReaderError struct {
Expand Down Expand Up @@ -200,7 +199,7 @@ func (h Header) GetDataEditListHeaderPacket() *DataEditListHeaderPacket {
}

// MarshalBinary implements method MarshalBinary.BinaryMarshaler.
func (h *Header) MarshalBinary() (data []byte, err error) {
func (h Header) MarshalBinary() (data []byte, err error) {
buffer := bytes.Buffer{}
err = binary.Write(&buffer, binary.LittleEndian, h.MagicNumber)
if err != nil {
Expand All @@ -214,24 +213,16 @@ func (h *Header) MarshalBinary() (data []byte, err error) {
if err != nil {
return nil, err
}
nonces := h.Nonces
for i, headerPacket := range h.HeaderPackets {
if h.Nonces != nil {
headerPacket.Nonce = nonces[i]
}
marshalledHeaderPacket, err := headerPacket.MarshalBinary()
for i := range h.HeaderPackets {
marshalledHeaderPacket, err := h.HeaderPackets[i].MarshalBinary()
if err != nil {
return nil, err
}
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: 2 additions & 2 deletions model/headers/headers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestNewHeader(t *testing.T) {
if err != nil {
panic(err)
}
if fmt.Sprintf("%v", header) != "&{[99 114 121 112 116 52 103 104] 1 2 [{[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 108 0 <nil> {65564 {0} 0 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]}} {[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 100 0 <nil> {{1} 3 [1 2 3]}}] []}" {
if fmt.Sprintf("%v", header) != "&{[99 114 121 112 116 52 103 104] 1 2 [{[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 108 0 <nil> {65564 {0} 0 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]}} {[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 100 0 <nil> {{1} 3 [1 2 3]}}]}" {
t.Fail()
}
}
Expand Down Expand Up @@ -294,7 +294,7 @@ func TestReEncryptedHeader(t *testing.T) {
if err != nil {
panic(err)
}
if fmt.Sprintf("%v", header) != "&{[99 114 121 112 116 52 103 104] 1 1 [{[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 108 0 <nil> {65564 {0} 0 [58 52 140 253 170 28 13 219 92 105 115 137 71 195 249 252 122 199 180 1 92 81 30 102 15 185 66 179 83 189 234 57]}}] []}" {
if fmt.Sprintf("%v", header) != "&{[99 114 121 112 116 52 103 104] 1 1 [{[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 108 0 <nil> {65564 {0} 0 [58 52 140 253 170 28 13 219 92 105 115 137 71 195 249 252 122 199 180 1 92 81 30 102 15 185 66 179 83 189 234 57]}}]}" {
t.Fail()
}
}
14 changes: 12 additions & 2 deletions streaming/out.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func NewCrypt4GHWriter(writer io.Writer, writerPrivateKey [chacha20poly1305.KeyS
return nil, err
}

crypt4GHWriter.Rands.headerNonces = make([]*[chacha20poly1305.NonceSize]byte, 2*len(readerPublicKeyList))
err = crypt4GHWriter.init(writer, writerPrivateKey, readerPublicKeyList, dataEditList)
if err != nil {
return nil, err
Expand Down Expand Up @@ -111,20 +112,25 @@ func (c *Crypt4GHWriter) init(writer io.Writer,
DataKey: c.Rands.dataKey,
}

i := 0
for _, readerPublicKey := range readerPublicKeyList {
headerPackets = append(headerPackets, headers.HeaderPacket{
WriterPrivateKey: writerPrivateKey,
ReaderPublicKey: readerPublicKey,
HeaderEncryptionMethod: headers.X25519ChaCha20IETFPoly1305,
EncryptedHeaderPacket: c.dataEncryptionParametersHeaderPacket,
Nonce: c.Rands.headerNonces[i],
})
i++
if dataEditList != nil {
headerPackets = append(headerPackets, headers.HeaderPacket{
WriterPrivateKey: writerPrivateKey,
ReaderPublicKey: readerPublicKey,
HeaderEncryptionMethod: headers.X25519ChaCha20IETFPoly1305,
EncryptedHeaderPacket: dataEditList,
Nonce: c.Rands.headerNonces[i],
})
i++
}
}
var magicNumber [8]byte
Expand All @@ -134,13 +140,17 @@ func (c *Crypt4GHWriter) init(writer io.Writer,
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
if !c.Rands.replicate {
c.Rands.headerNonces = make([]*[chacha20poly1305.NonceSize]byte, len(c.header.HeaderPackets))
for i, hp := range c.header.HeaderPackets {
c.Rands.headerNonces[i] = hp.Nonce
}
}
_, err = writer.Write(binaryHeader)
if err != nil {
return err
Expand Down

0 comments on commit 490c5ca

Please sign in to comment.