-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
61 lines (52 loc) · 1.37 KB
/
server.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
58
59
60
61
package gogsrp
import (
"hash"
"math/big"
)
type Server struct {
g *big.Int
N *big.Int
saltLen int
keyLen int
newHash func() hash.Hash
}
func CreateServer(g, N *big.Int, saltLen, keyLen int, newHash func() hash.Hash) *Server {
server := new(Server)
server.g = g
server.N = N
server.newHash = newHash
server.saltLen = saltLen
server.keyLen = keyLen
return server
}
func (server *Server) NewSalt() ([]byte, error) {
return RandomBytes(server.saltLen)
}
func (server *Server) NewPrivateKey() (*big.Int, error) {
rnd, err := RandomBytes(server.keyLen)
if err != nil {
return nil, err
}
sk := new(big.Int).SetBytes(rnd)
return sk, nil
}
func (server *Server) NewPublicKey(sk, verifier *big.Int) (*big.Int, error) {
Nbytes := server.N.Bytes()
gbytes := server.g.Bytes()
hash := server.newHash()
hash.Write(Nbytes)
hash.Write(Pad(gbytes, len(Nbytes)))
k := new(big.Int).SetBytes(hash.Sum(nil))
y := new(big.Int).Exp(server.g, sk, server.N)
x := new(big.Int).Mul(k, verifier)
pk := new(big.Int).Add(x, y)
pk = pk.Rem(pk, server.N)
return pk, nil
}
func (server *Server) GetPremasterSecret(clientPK, serverPK, serverSK, verifier *big.Int) *big.Int {
u, _ := CommonHash(clientPK, serverPK, server.newHash)
y := new(big.Int).Exp(verifier, u, server.N)
x := new(big.Int).Mul(clientPK, y)
secret := new(big.Int).Exp(x, serverSK, server.N)
return secret
}