This repository has been archived by the owner on Jun 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
key_agreement.go
57 lines (48 loc) · 1.94 KB
/
key_agreement.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package dtls
import (
"github.com/maufl/dhkx"
"math/big"
)
type keyAgreement interface {
processServerKeyExchange(clientRandom, serverRandom random, serverKeyExchange handshakeServerKeyExchange) error
generateClientKeyExchange() ([]byte, handshakeClientKeyExchange, error)
generateServerKeyExchange() ([]byte, error)
processClientKeyExchange(handshakeClientKeyExchange) ([]byte, error)
}
type dheKeyAgreement struct {
PrivateKey *dhkx.DHKey
PublicKey *dhkx.DHKey
Group *dhkx.DHGroup
}
func (ka *dheKeyAgreement) processServerKeyExchange(clientRandom, serverRandom random, serverKeyExchange handshakeServerKeyExchange) (err error) {
ka.PublicKey = dhkx.NewPublicKey(serverKeyExchange.Params.PublicKey)
var p, g big.Int
p.SetBytes(serverKeyExchange.Params.P)
g.SetBytes(serverKeyExchange.Params.G)
ka.Group = dhkx.CreateGroup(&p, &g)
ka.PrivateKey, err = ka.Group.GeneratePrivateKey(nil)
return
}
func (ka *dheKeyAgreement) generateClientKeyExchange() (preMasterSecret []byte, clientKeyExchange handshakeClientKeyExchange, err error) {
clientKeyExchange.clientDiffieHellmanPublic.PublicKey = ka.PrivateKey.Bytes()
if key, err := ka.Group.ComputeKey(ka.PublicKey, ka.PrivateKey); err == nil {
preMasterSecret = key.Bytes()
}
return
}
func (ka *dheKeyAgreement) generateServerKeyExchange() (serverKeyExchange []byte, err error) {
if ka.Group, err = dhkx.GetGroup(0); err != nil {
return
}
if ka.PrivateKey, err = ka.Group.GeneratePrivateKey(nil); err != nil {
return
}
return handshakeServerKeyExchange{Params: serverDHParams{P: ka.Group.P().Bytes(), G: ka.Group.G().Bytes(), PublicKey: ka.PrivateKey.Bytes()}}.Bytes(), nil
}
func (ka *dheKeyAgreement) processClientKeyExchange(clientKeyExchange handshakeClientKeyExchange) (preMasterSecret []byte, err error) {
ka.PublicKey = dhkx.NewPublicKey(clientKeyExchange.PublicKey)
if key, err := ka.Group.ComputeKey(ka.PublicKey, ka.PrivateKey); err == nil {
preMasterSecret = key.Bytes()
}
return
}