Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions go/tdh2/lib/group/nist/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ func (p *curvePoint) Equal(p2 group.Point) bool {
// Make sure both coordinates are normalized.
// Apparently Go's elliptic curve code doesn't always ensure this.
M := p.c.p.P
p.x.Mod(p.x, M)
p.y.Mod(p.y, M)
cp2.x.Mod(cp2.x, M)
cp2.y.Mod(cp2.y, M)
x1 := new(big.Int).Mod(p.x, M)
y1 := new(big.Int).Mod(p.y, M)
x2 := new(big.Int).Mod(cp2.x, M)
y2 := new(big.Int).Mod(cp2.y, M)

return p.x.Cmp(cp2.x) == 0 && p.y.Cmp(cp2.y) == 0
return x1.Cmp(x2) == 0 && y1.Cmp(y2) == 0
}

func (p *curvePoint) Null() group.Point {
Expand Down Expand Up @@ -134,6 +134,8 @@ func (p *curvePoint) UnmarshalBinary(buf []byte) error {
if p.x == nil || !p.Valid() {
return errors.New("invalid elliptic curve point")
}
} else if buf[0] != 0x04 {
return errors.New("invalid elliptic curve point: non-canonical identity encoding")
} else {
// All bytes are 0, so we initialize x and y
p.x = big.NewInt(0)
Expand Down
2 changes: 1 addition & 1 deletion go/tdh2/tdh2easy/tdh2easy.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (m *MasterSecret) Unmarshal(data []byte) error {
return m.m.Unmarshal(data)
}

func (m MasterSecret) UnmarshalJSON(data []byte) error {
func (m *MasterSecret) UnmarshalJSON(data []byte) error {
return m.Unmarshal(data)
}

Expand Down
92 changes: 92 additions & 0 deletions go/tdh2/tdh2easy/tdh2easy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@ func TestPrivateShareMarshal(t *testing.T) {
}
}

func TestPrivateShareMarshalJSON(t *testing.T) {
_, _, want, err := GenerateKeys(2, 3)
if err != nil {
t.Fatalf("GenerateKeys: %v", err)
}
b, err := want[0].MarshalJSON()
if err != nil {
t.Fatalf("MarshalJSON: %v", err)
}
var got PrivateShare
if err := got.UnmarshalJSON(b); err != nil {
t.Fatalf("UnmarshalJSON: %v", err)
}
if !reflect.DeepEqual(got.p, want[0].p) {
t.Errorf("got=%v want=%v", got, want[0])
}
if err := got.UnmarshalJSON([]byte("broken")); err == nil {
t.Errorf("Unmarshal did not fail")
}
}

func TestDecryptionShareMarshal(t *testing.T) {
_, pk, sh, err := GenerateKeys(2, 3)
if err != nil {
Expand Down Expand Up @@ -87,6 +108,35 @@ func TestDecryptionShareMarshal(t *testing.T) {
}
}

func TestDecryptionShareMarshalJSON(t *testing.T) {
_, pk, sh, err := GenerateKeys(2, 3)
if err != nil {
t.Fatalf("GenerateKeys: %v", err)
}
c, err := Encrypt(pk, []byte("msg"))
if err != nil {
t.Fatalf("Encrypt: %v", err)
}
want, err := Decrypt(c, sh[0])
if err != nil {
t.Fatalf("Decrypt: %v", err)
}
b, err := want.MarshalJSON()
if err != nil {
t.Fatalf("MarshalJSON: %v", err)
}
var got DecryptionShare
if err := got.UnmarshalJSON(b); err != nil {
t.Fatalf("UnmarshalJSON: %v", err)
}
if !reflect.DeepEqual(got.d, want.d) {
t.Errorf("got=%v want=%v", got, want)
}
if err := got.UnmarshalJSON([]byte("broken")); err == nil {
t.Errorf("UnmarshalJSON did not fail")
}
}

func TestPublicKeyMarshal(t *testing.T) {
_, want, _, err := GenerateKeys(2, 3)
if err != nil {
Expand All @@ -108,6 +158,27 @@ func TestPublicKeyMarshal(t *testing.T) {
}
}

func TestPublicKeyMarshalJSON(t *testing.T) {
_, want, _, err := GenerateKeys(2, 3)
if err != nil {
t.Fatalf("GenerateKeys: %v", err)
}
b, err := want.MarshalJSON()
if err != nil {
t.Fatalf("MarshalJSON: %v", err)
}
var got PublicKey
if err := got.UnmarshalJSON(b); err != nil {
t.Fatalf("UnmarshalJSON: %v", err)
}
if !got.p.Equal(want.p) {
t.Errorf("got=%v want=%v", got, want)
}
if err := got.UnmarshalJSON([]byte("broken")); err == nil {
t.Errorf("UnmarshalJSON did not fail")
}
}

func TestMasterSecretMarshal(t *testing.T) {
want, _, _, err := GenerateKeys(2, 3)
if err != nil {
Expand All @@ -129,6 +200,27 @@ func TestMasterSecretMarshal(t *testing.T) {
}
}

func TestMasterSecretMarshalJSON(t *testing.T) {
want, _, _, err := GenerateKeys(2, 3)
if err != nil {
t.Fatalf("GenerateKeys: %v", err)
}
b, err := want.MarshalJSON()
if err != nil {
t.Fatalf("MarshalJSON: %v", err)
}
var got MasterSecret
if err := got.UnmarshalJSON(b); err != nil {
t.Fatalf("UnmarshalJSON: %v", err)
}
if !reflect.DeepEqual(got.m, want.m) {
t.Errorf("got=%v want=%v", got, want)
}
if err := got.UnmarshalJSON([]byte("broken")); err == nil {
t.Errorf("UnmarshalJSON did not fail")
}
}

func TestCiphertextDecrypt(t *testing.T) {
_, pk, share, err := GenerateKeys(1, 1)
if err != nil {
Expand Down
Loading