-
Notifications
You must be signed in to change notification settings - Fork 0
/
proof.go
60 lines (55 loc) · 1.81 KB
/
proof.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
package srp
import (
"crypto/sha1"
"strings"
)
// ClientChallengeProof returns a proof that the client should send after receiving the auth challenge.
// The server should compare this with the proof received by the client and verify they match.
// If they match, the client has proven they know the session key.
func ClientChallengeProof(
username string,
salt,
clientPublicKey,
serverPublicKey,
sessionKey []byte,
) []byte {
hUsername := sha1.Sum([]byte(strings.ToUpper(username)))
h := sha1.New()
h.Write(xorHash)
h.Write(hUsername[:])
h.Write(salt)
h.Write(clientPublicKey)
h.Write(serverPublicKey)
h.Write(sessionKey)
return h.Sum(nil)
}
// ServerChallengeProof returns a proof that the server should send after validating the client proof.
// The server proof is not used when the client is reconnecting.
func ServerChallengeProof(clientPublicKey, clientProof, sessionKey []byte) []byte {
h := sha1.New()
h.Write(clientPublicKey)
h.Write(clientProof)
h.Write(sessionKey)
return h.Sum(nil)
}
// ReconnectProof returns a proof that the client should send when attempting to reconnect.
// Like [ClientChallengeProof], the server should compare this with the proof received by the client.
func ReconnectProof(username string, clientData, serverData, sessionKey []byte) []byte {
h := sha1.New()
h.Write([]byte(strings.ToUpper(username)))
h.Write(clientData)
h.Write(serverData)
h.Write(sessionKey)
return h.Sum(nil)
}
// WorldProof returns a proof that the client should send once they have finished authenticating
// and want to connect to the world/realm server.
func WorldProof(username string, clientSeed, serverSeed, sessionKey []byte) []byte {
h := sha1.New()
h.Write([]byte(strings.ToUpper(username)))
h.Write([]byte{0, 0, 0, 0})
h.Write(clientSeed)
h.Write(serverSeed)
h.Write(sessionKey)
return h.Sum(nil)
}