-
Notifications
You must be signed in to change notification settings - Fork 0
/
pair-setup-client-session.go
81 lines (67 loc) · 1.99 KB
/
pair-setup-client-session.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package hkontroller
import (
"github.com/hkontrol/hkontroller/hkdf"
"github.com/tadglines/go-pkgs/crypto/srp"
"crypto/sha512"
)
// Main SRP algorithm is described in http://srp.stanford.edu/design.html
// The HAP uses the SRP-6a Stanford implementation with the following characteristics
// x = H(s | H(I | ":" | P)) -> called the key derivative function
// M1 = H(H(N) xor H(g), H(I), s, A, B, K)
const (
srpGroup = "rfc5054.3072" // N (modulo) => 384 byte
)
type pairSetupClientSession struct {
PublicKey []byte // A
SessionKey []byte // K
EncryptionKey [32]byte // K
Salt []byte // s
Proof []byte
session *srp.ClientSession
}
func (s *pairSetupClientSession) SetupEncryptionKey(salt []byte, info []byte) error {
hash, err := hkdf.Sha512(s.SessionKey, salt, info)
if err == nil {
s.EncryptionKey = hash
}
return err
}
// newPairSetupSession return a new setup server session.
func newPairSetupClientSession(serverSalt []byte, serverB []byte, pin string) (*pairSetupClientSession, error) {
var err error
userName := []byte("Pair-Setup")
srp_, err := srp.NewSRP(srpGroup, sha512.New, keyDerivativeFuncRFC2945(sha512.New, userName))
if err == nil {
srp_.SaltLength = 16
if err == nil {
srpClient := srp_.NewClientSession(userName, []byte(pin))
key, err := srpClient.ComputeKey(serverSalt, serverB)
if err != nil {
return nil, err
}
pairing := pairSetupClientSession{
session: srpClient,
PublicKey: srpClient.GetA(),
SessionKey: key,
Proof: srpClient.ComputeAuthenticator(),
}
return &pairing, nil
}
}
return nil, err
}
// keyDerivativeFuncRFC2945 returns the SRP-6a key derivative function which does
// x = H(s | H(I | ":" | P))
func keyDerivativeFuncRFC2945(h srp.HashFunc, id []byte) srp.KeyDerivationFunc {
return func(salt, pin []byte) []byte {
h := h()
h.Write(id)
h.Write([]byte(":"))
h.Write(pin)
t2 := h.Sum(nil)
h.Reset()
h.Write(salt)
h.Write(t2)
return h.Sum(nil)
}
}