Skip to content

Commit

Permalink
Merge pull request monnand#6 from nhelke/master
Browse files Browse the repository at this point in the history
Make `P()` and `G()` return copied `big.Int`s so they can't be mutated
  • Loading branch information
monnand authored May 22, 2018
2 parents 6b25b12 + 009a3a7 commit 9e5b033
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
8 changes: 6 additions & 2 deletions dhgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ type DHGroup struct {
}

func (self *DHGroup) P() *big.Int {
return self.p
p := new(big.Int)
p.Set(self.p)
return p
}

func (self *DHGroup) G() *big.Int {
return self.g
g := new(big.Int)
g.Set(self.g)
return g
}

func (self *DHGroup) GeneratePrivateKey(randReader io.Reader) (key *DHKey, err error) {
Expand Down
18 changes: 18 additions & 0 deletions dhkx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,21 @@ func TestCustomGroupKeyExchange(t *testing.T) {
t.Errorf("%v", err)
}
}

func TestPIsNotMutable(t *testing.T) {
d, _ := GetGroup(0)
p := d.p.String()
d.P().Set(big.NewInt(1))
if p != d.p.String() {
t.Errorf("group's prime mutated externally, should be %s, was changed to %s", p, d.p.String())
}
}

func TestGIsNotMutable(t *testing.T) {
d, _ := GetGroup(0)
g := d.g.String()
d.G().Set(big.NewInt(0))
if g != d.g.String() {
t.Errorf("group's generator mutated externally, should be %s, was changed to %s", g, d.g.String())
}
}

0 comments on commit 9e5b033

Please sign in to comment.