diff options
author | siddharth <s@ricketyspace.net> | 2021-11-21 18:45:53 -0500 |
---|---|---|
committer | siddharth <s@ricketyspace.net> | 2021-11-21 18:45:53 -0500 |
commit | 2b52444043b26a01cc1eb82c456e325916e25194 (patch) | |
tree | 6f84867fe3131aa6fb497faaaea0659c1b1923e2 /lib/dh.go | |
parent | 1748a57ad36b3cd9cb0522ee2963b7183f4ee0b4 (diff) |
lib: implement diffie-hellman
Diffstat (limited to 'lib/dh.go')
-rw-r--r-- | lib/dh.go | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/dh.go b/lib/dh.go new file mode 100644 index 0000000..0782033 --- /dev/null +++ b/lib/dh.go @@ -0,0 +1,41 @@ +// Copyright © 2021 siddharth <s@ricketyspace.net> +// SPDX-License-Identifier: ISC + +package lib + +import "math/big" + +type DH struct { + p *big.Int + g *big.Int + pk *big.Int // Private key +} + +func NewDH(ps, gs string) (*DH, bool) { + p, ok := new(big.Int).SetString(StripSpaceChars(ps), 16) + if !ok { + return nil, false + } + g, ok := new(big.Int).SetString(StripSpaceChars(gs), 16) + if !ok { + return nil, false + } + + // Init DH. + dh := new(DH) + dh.p = p + dh.g = g + dh.pk = big.NewInt(RandomInt(1, 10000000)) + return dh, true +} + +// Return our public key. +func (dh *DH) Pub() *big.Int { + return new(big.Int).Exp(dh.g, dh.pk, dh.p) +} + +// Return shared secret between us and the other party. +// `pub` is the other party's public key. +func (dh *DH) SharedSecret(pub *big.Int) *big.Int { + return new(big.Int).Exp(pub, dh.pk, dh.p) +} |