Skip to content

Commit 64bd88e

Browse files
committed
Fix Issue #6
party.FromBytes now returns an error if the length of the given byte slice is too small
1 parent d50196b commit 64bd88e

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

pkg/eddsa/secret_share.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,16 @@ func (sk *SecretShare) MarshalBinary() ([]byte, error) {
5858

5959
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
6060
func (sk *SecretShare) UnmarshalBinary(data []byte) error {
61+
var err error
6162
if len(data) != party.ByteSize+32 {
6263
return errors.New("SecretShare: data is not the right size")
6364
}
64-
sk.ID = party.FromBytes(data)
65+
if sk.ID, err = party.FromBytes(data); err != nil {
66+
return err
67+
}
6568
data = data[party.ByteSize:]
66-
_, err := sk.Secret.SetCanonicalBytes(data)
67-
if err != nil {
69+
70+
if _, err = sk.Secret.SetCanonicalBytes(data); err != nil {
6871
return err
6972
}
7073
sk.Public.ScalarBaseMult(&sk.Secret)

pkg/frost/party/id.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ func (p ID) String() string {
5858
}
5959

6060
// FromBytes reads the first party.ByteSize bytes from b and creates an ID from it.
61-
func FromBytes(b []byte) ID {
62-
return ID(binary.BigEndian.Uint16(b))
61+
// Returns an error if b is too small to hold an ID.
62+
func FromBytes(b []byte) (ID, error) {
63+
if len(b) < ByteSize {
64+
return 0, errors.New("party.FromBytes: b is not long enough to hold an ID")
65+
}
66+
return ID(binary.BigEndian.Uint16(b)), nil
6367
}
6468

6569
// RandIDn returns, as an ID, a non-negative pseudo-random number in [1,n]

pkg/internal/polynomial/exponent.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,11 @@ func (p *Exponent) MarshalBinary() (data []byte, err error) {
163163

164164
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
165165
func (p *Exponent) UnmarshalBinary(data []byte) error {
166-
coefficientCount := party.FromBytes(data) + 1
166+
degree, err := party.FromBytes(data)
167+
if err != nil {
168+
return err
169+
}
170+
coefficientCount := degree + 1
167171
remaining := data[party.ByteSize:]
168172

169173
count := len(remaining)

pkg/messages/messages.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,24 @@ func (m *Message) MarshalBinary() ([]byte, error) {
9191

9292
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
9393
func (m *Message) UnmarshalBinary(data []byte) error {
94+
var err error
95+
// ensure the length is long enough to hold the header
96+
// 1 byte for message type + 2*party.ByteSize for from/to
97+
if len(data) < 1+2*party.ByteSize {
98+
return errors.New("data does not contain header")
99+
}
100+
94101
msgType := MessageType(data[0])
95102
m.messageType = msgType
96103
data = data[1:]
97-
m.from = party.FromBytes(data)
104+
if m.from, err = party.FromBytes(data); err != nil {
105+
return err
106+
}
98107
data = data[party.ByteSize:]
99108

100-
m.to = party.FromBytes(data)
109+
if m.to, err = party.FromBytes(data); err != nil {
110+
return err
111+
}
101112
data = data[party.ByteSize:]
102113

103114
switch msgType {

0 commit comments

Comments
 (0)