-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.go
More file actions
70 lines (60 loc) · 1.67 KB
/
common.go
File metadata and controls
70 lines (60 loc) · 1.67 KB
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
62
63
64
65
66
67
68
69
70
package gogsrp
import (
"hash"
"math/big"
)
func Pad(data []byte, length int) []byte {
n := len(data)
if n > length {
return data
}
padded := make([]byte, length)
copy(padded[length-n:], data)
return padded
}
func CommonHash(clientPK, serverPK *big.Int, newHash func() hash.Hash) (*big.Int, error) {
clientBytes := clientPK.Bytes()
serverBytes := serverPK.Bytes()
hash := newHash()
hash.Write(Pad(clientBytes, len(serverBytes)))
hash.Write(Pad(serverBytes, len(clientBytes)))
u := new(big.Int).SetBytes(hash.Sum(nil))
return u, nil
}
func SessionKey(premasterSecret *big.Int, newHash func() hash.Hash) []byte {
hash := newHash()
hash.Write(premasterSecret.Bytes())
return hash.Sum(nil)
}
// clientMsg := H(H(N) xor H(g) | H(login) | salt | clientPK | serverPK | sessionKey)
func ClientMessage(login, salt, sessionKey []byte, g, N, clientPK, serverPK *big.Int, newHash func() hash.Hash) []byte {
hash := newHash()
hash.Write(g.Bytes())
hashedG := hash.Sum(nil)
hash = newHash()
hash.Write(N.Bytes())
hashedN := hash.Sum(nil)
a := new(big.Int).SetBytes(hashedG)
b := new(big.Int).SetBytes(hashedN)
xor := new(big.Int).Xor(a, b)
hash = newHash()
hash.Write(login)
hashedLogin := hash.Sum(nil)
hash = newHash()
hash.Write(xor.Bytes())
hash.Write(hashedLogin)
hash.Write(salt)
hash.Write(clientPK.Bytes())
hash.Write(serverPK.Bytes())
hash.Write(sessionKey)
return hash.Sum(nil)
}
// serverMsg := H(clientPK | clientMsg | sessionKey)
func ServerMessage(clientPK *big.Int, clientMsg, sessionKey []byte, newHash func() hash.Hash) []byte {
hash := newHash()
hash.Write(clientPK.Bytes())
hash.Write(clientMsg)
hash.Write(sessionKey)
return hash.Sum(nil)
return nil
}