-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ import ( | |
| "crypto/aes" | ||
| "crypto/cipher" | ||
| "crypto/rand" | ||
| "encoding/json" | ||
| "encoding/binary" | ||
| "fmt" | ||
|
|
||
| "github.com/smartcontractkit/tdh2/go/tdh2/internal/group/nist" | ||
|
|
@@ -194,22 +194,83 @@ type ciphertextRaw struct { | |
| Nonce []byte | ||
| } | ||
|
|
||
| // ciphertextRaw is serialized as _TDH2Ctxt || _SymCtxt || _Nonce | ||
| // 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)) | ||
| buf = append(buf, prefixWithLength(c.TDH2Ctxt)...) | ||
| buf = append(buf, prefixWithLength(c.SymCtxt)...) | ||
| buf = append(buf, prefixWithLength(c.Nonce)...) | ||
| return buf, nil | ||
| } | ||
|
|
||
| func (c *ciphertextRaw) Unmarshal(data []byte) error { | ||
| if len(data) < 4 { | ||
| return fmt.Errorf("invalid data length") | ||
| } | ||
|
|
||
| var err error | ||
| offset := 0 | ||
|
|
||
| c.TDH2Ctxt, offset, err = parseLengthPrefixed(data, offset) | ||
| if err != nil { | ||
| return fmt.Errorf("cannot decode TDH2 ciphertext: %w", err) | ||
| } | ||
| c.SymCtxt, offset, err = parseLengthPrefixed(data, offset) | ||
| if err != nil { | ||
| return fmt.Errorf("cannot decode symmetric ciphertext: %w", err) | ||
| } | ||
| c.Nonce, _, err = parseLengthPrefixed(data, offset) | ||
| if err != nil { | ||
| return fmt.Errorf("cannot decode nonce: %w", err) | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // prefixWithLength encodes length-prefixed bytes. | ||
| // The length is encoded as a 4-byte big-endian integer. | ||
| func prefixWithLength(b []byte) []byte { | ||
| length := len(b) | ||
| buf := make([]byte, 4+length) | ||
| binary.BigEndian.PutUint32(buf[:4], uint32(length)) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| copy(buf[4:], b) | ||
| return buf | ||
| } | ||
|
|
||
| // parseLengthPrefixed decodes length-prefixed bytes. | ||
| func parseLengthPrefixed(data []byte, offset int) ([]byte, int, error) { | ||
| if offset+4 > len(data) { | ||
| return nil, 0, fmt.Errorf("unexpected EOF while reading length") | ||
| } | ||
| length := int(binary.BigEndian.Uint32(data[offset : offset+4])) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. int can overflow on 32bit machine here |
||
| offset += 4 | ||
|
|
||
| if offset+length > len(data) { | ||
| return nil, 0, fmt.Errorf("unexpected EOF while reading data") | ||
| } | ||
|
|
||
| return data[offset : offset+length], offset + length, nil | ||
| } | ||
|
|
||
| func (c *Ciphertext) Marshal() ([]byte, error) { | ||
| ctxt, err := c.tdh2Ctxt.Marshal() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("cannot marshal TDH2 ciphertext: %w", err) | ||
| } | ||
| return json.Marshal(&ciphertextRaw{ | ||
| cRaw := ciphertextRaw{ | ||
| TDH2Ctxt: ctxt, | ||
| SymCtxt: c.symCtxt, | ||
| Nonce: c.nonce, | ||
| }) | ||
| } | ||
| return cRaw.Marshal() | ||
| } | ||
|
|
||
| // UnmarshalVerify unmarshals ciphertext and verifies if it matches the public key. | ||
| func (c *Ciphertext) UnmarshalVerify(data []byte, pk *PublicKey) error { | ||
| var raw ciphertextRaw | ||
| if err := json.Unmarshal(data, &raw); err != nil { | ||
| if err := raw.Unmarshal(data); err != nil { | ||
| return fmt.Errorf("cannot unmarshal data: %w", err) | ||
| } | ||
| c.symCtxt = raw.SymCtxt | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -270,6 +270,87 @@ func TestAggregate(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func TestCiphertextRawMarshal(t *testing.T) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| testCases := []struct { | ||
| name string | ||
| input ciphertextRaw | ||
| }{ | ||
| { | ||
| name: "Normal case", | ||
| input: ciphertextRaw{ | ||
| TDH2Ctxt: []byte("TDH2CtxtData"), | ||
| SymCtxt: []byte("SymmetricCiphertext"), | ||
| Nonce: []byte("NonceData"), | ||
| }, | ||
| }, | ||
| { | ||
| name: "Empty fields", | ||
| input: ciphertextRaw{ | ||
| TDH2Ctxt: []byte{}, | ||
| SymCtxt: []byte{}, | ||
| Nonce: []byte{}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "Nil TDH2Ctxt", | ||
| input: ciphertextRaw{ | ||
| TDH2Ctxt: nil, | ||
| SymCtxt: []byte("SymmetricCiphertext"), | ||
| Nonce: []byte("NonceData"), | ||
| }, | ||
| }, | ||
| { | ||
| name: "Nil SymCtxt", | ||
| input: ciphertextRaw{ | ||
| TDH2Ctxt: []byte("TDH2CtxtData"), | ||
| SymCtxt: nil, | ||
| Nonce: []byte("NonceData"), | ||
| }, | ||
| }, | ||
| { | ||
| name: "Nil Nonce", | ||
| input: ciphertextRaw{ | ||
| TDH2Ctxt: []byte("TDH2CtxtData"), | ||
| SymCtxt: []byte("SymmetricCiphertext"), | ||
| Nonce: nil, | ||
| }, | ||
| }, | ||
| { | ||
| name: "All nil fields", | ||
| input: ciphertextRaw{ | ||
| TDH2Ctxt: nil, | ||
| SymCtxt: nil, | ||
| Nonce: nil, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| serialized, err := tc.input.Marshal() | ||
| if err != nil { | ||
| t.Fatalf("Marshal failed: %v", err) | ||
| } | ||
|
|
||
| var deserialized ciphertextRaw | ||
| err = deserialized.Unmarshal(serialized) | ||
| if err != nil { | ||
| t.Fatalf("Unmarshal failed: %v", err) | ||
| } | ||
|
|
||
| if !bytes.Equal(tc.input.TDH2Ctxt, deserialized.TDH2Ctxt) { | ||
| t.Errorf("TDH2Ctxt mismatch: got %v, want %v", deserialized.TDH2Ctxt, tc.input.TDH2Ctxt) | ||
| } | ||
| if !bytes.Equal(tc.input.SymCtxt, deserialized.SymCtxt) { | ||
| t.Errorf("SymCtxt mismatch: got %v, want %v", deserialized.SymCtxt, tc.input.SymCtxt) | ||
| } | ||
| if !bytes.Equal(tc.input.Nonce, deserialized.Nonce) { | ||
| t.Errorf("Nonce mismatch: got %v, want %v", deserialized.Nonce, tc.input.Nonce) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestCiphertextMarshal(t *testing.T) { | ||
| _, pk, _, err := GenerateKeys(1, 1) | ||
| if err != nil { | ||
|
|
||
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