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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
package noise
// symmetricState extends CipherState with hashing and key derivation.
type symmetricState struct {
cs CipherState
curve *Curve
digest []byte
chainingKey []byte
}
// newSymmetricState creates a new symmetric state.
func newSymmetricState(curve *Curve) *symmetricState {
if curve == nil {
curve = DefaultCurve
}
return &symmetricState{
curve: curve,
digest: make([]byte, hashLen),
chainingKey: make([]byte, hashLen),
}
}
// initializeSymmetric initializes the symmetric state with protocol name.
func (ss *symmetricState) initializeSymmetric(protocolName []byte) {
if len(protocolName) <= hashLen {
copy(ss.chainingKey, protocolName)
copy(ss.digest, protocolName)
for i := len(protocolName); i < hashLen; i++ {
ss.chainingKey[i] = 0
ss.digest[i] = 0
}
} else {
h := newBLAKE2b512()
h.Write(protocolName)
hashed := h.Sum(nil)
copy(ss.chainingKey, hashed)
copy(ss.digest, hashed)
}
}
// mixHash mixes data into the digest.
func (ss *symmetricState) mixHash(data []byte) {
h := newBLAKE2b512()
h.Write(ss.digest)
h.Write(data)
ss.digest = h.Sum(ss.digest[:0])
}
// mixKeyAndHash mixes a key into both the chaining key and digest.
func (ss *symmetricState) mixKeyAndHash(key []byte) {
outputs := hkdf(ss.chainingKey, key, nil, 3)
ss.chainingKey = outputs[0]
ss.mixHash(outputs[1])
ss.cs.initializeKey(outputs[2][:32])
}
// mixKeyNormal mixes a key into the chaining key.
func (ss *symmetricState) mixKeyNormal(key []byte) {
outputs := hkdf(ss.chainingKey, key, nil, 2)
ss.chainingKey = outputs[0]
ss.cs.initializeKey(outputs[1][:32])
}
// mixKey performs DH and mixes the result into the chaining key.
func (ss *symmetricState) mixKey(remoteKey []byte, localKey *KeyPair) error {
dh, err := ss.curve.DH(remoteKey, localKey)
if err != nil {
return err
}
outputs := hkdf(ss.chainingKey, dh, nil, 2)
ss.chainingKey = outputs[0]
ss.cs.initializeKey(outputs[1][:32])
return nil
}
// encryptAndHash encrypts plaintext and mixes ciphertext into digest.
func (ss *symmetricState) encryptAndHash(plaintext []byte) ([]byte, error) {
ciphertext, err := ss.cs.Encrypt(nil, plaintext, ss.digest)
if err != nil {
return nil, err
}
ss.mixHash(ciphertext)
return ciphertext, nil
}
// decryptAndHash decrypts ciphertext and mixes it into digest.
func (ss *symmetricState) decryptAndHash(ciphertext []byte) ([]byte, error) {
plaintext, err := ss.cs.Decrypt(nil, ciphertext, ss.digest)
if err != nil {
return nil, err
}
ss.mixHash(ciphertext)
return plaintext, nil
}
// handshakeHash returns the current handshake hash.
func (ss *symmetricState) handshakeHash() []byte {
hash := make([]byte, hashLen)
copy(hash, ss.digest)
return hash
}
// split derives two cipher states from the current state.
// Clears the symmetric state for forward secrecy.
func (ss *symmetricState) split() ([]*CipherState, error) {
outputs := hkdf(ss.chainingKey, []byte{}, nil, 2)
k1, err := newCipherState(outputs[0][:32])
if err != nil {
return nil, err
}
k2, err := newCipherState(outputs[1][:32])
if err != nil {
return nil, err
}
ss.clear()
return []*CipherState{k1, k2}, nil
}
// clear clears sensitive data.
func (ss *symmetricState) clear() {
ss.cs.Clear()
if ss.digest != nil {
for i := range ss.digest {
ss.digest[i] = 0
}
}
if ss.chainingKey != nil {
for i := range ss.chainingKey {
ss.chainingKey[i] = 0
}
}
ss.digest = nil
ss.chainingKey = nil
ss.curve = nil
}
|