diff --git a/go/tdh2/lib/group/nist/curve.go b/go/tdh2/lib/group/nist/curve.go index 9da4915..f90d2f8 100644 --- a/go/tdh2/lib/group/nist/curve.go +++ b/go/tdh2/lib/group/nist/curve.go @@ -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 { @@ -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) diff --git a/go/tdh2/tdh2easy/tdh2easy.go b/go/tdh2/tdh2easy/tdh2easy.go index 132b800..1c38c33 100644 --- a/go/tdh2/tdh2easy/tdh2easy.go +++ b/go/tdh2/tdh2easy/tdh2easy.go @@ -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) } diff --git a/go/tdh2/tdh2easy/tdh2easy_test.go b/go/tdh2/tdh2easy/tdh2easy_test.go index 07121ba..023c09c 100644 --- a/go/tdh2/tdh2easy/tdh2easy_test.go +++ b/go/tdh2/tdh2easy/tdh2easy_test.go @@ -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 { @@ -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 { @@ -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 { @@ -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 {