Skip to content

Conversation

@siamumar
Copy link
Collaborator

tdh2easy.ciphertextRaw is serialized as _TDH2Ctxt || _SymCtxt || _Nonce where _TDH2Ctxt, _SymCtxt, and _Nonce are length-prefixed byte slices.

@siamumar siamumar requested a review from pszal February 27, 2025 18:58
Copy link
Collaborator

@pszal pszal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that json is slow but isn't it an issue for all tdh2 artifacts? (e.g., c.tdh2Ctxt.Marshal() is json too AFAIR). Wouldn't it be a better experiment to change encoding everywhere to something with acceptable performance? In general, I'm against writing custom parsers...

c.Nonce, _, err = parseLengthPrefixed(data, offset)
if err != nil {
return fmt.Errorf("cannot decode nonce: %w", err)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to assert there is no data left after the last offset.

func prefixWithLength(b []byte) []byte {
length := len(b)
buf := make([]byte, 4+length)
binary.BigEndian.PutUint32(buf[:4], uint32(length))
Copy link
Collaborator

@pszal pszal Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

length is 64bit on 64-bit machine so it can overflow here. You need to ensure it is <= MaxUint32 or increase the length prefix

}
}

func TestCiphertextRawMarshal(t *testing.T) {
Copy link
Collaborator

@pszal pszal Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A good start but I feel like tests for all the code are necessary.

// where _TDH2Ctxt, _SymCtxt, and _Nonce are length-prefixed byte slices.

func (c ciphertextRaw) Marshal() ([]byte, error) {
buf := make([]byte, 0, 4+len(c.TDH2Ctxt)+4+len(c.SymCtxt)+4+len(c.Nonce))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

len() is 64bit on 64bit architectures, so assuming 4 bytes for length is incorrect

if offset+4 > len(data) {
return nil, 0, fmt.Errorf("unexpected EOF while reading length")
}
length := int(binary.BigEndian.Uint32(data[offset : offset+4]))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int can overflow on 32bit machine here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants