-
Notifications
You must be signed in to change notification settings - Fork 5
new marshalling for ciphertext raw #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
pszal
left a comment
There was a problem hiding this 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) | ||
| } |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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])) |
There was a problem hiding this comment.
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
tdh2easy.ciphertextRawis serialized as_TDH2Ctxt || _SymCtxt || _Noncewhere_TDH2Ctxt,_SymCtxt, and_Nonceare length-prefixed byte slices.