-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathstate.go
More file actions
314 lines (250 loc) · 8.53 KB
/
state.go
File metadata and controls
314 lines (250 loc) · 8.53 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
This file is part of secretstream.
secretstream is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
secretstream is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with secretstream. If not, see <http://www.gnu.org/licenses/>.
*/
package secrethandshake
import (
"bytes"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"crypto/sha512"
"github.com/agl/ed25519"
"github.com/agl/ed25519/extra25519"
"golang.org/x/crypto/curve25519"
"golang.org/x/crypto/nacl/box"
)
// State is the state each peer holds during the handshake
type State struct {
appKey, secHash []byte
localAppMac, remoteAppMac []byte
localExchange CurveKeyPair
local EdKeyPair
remoteExchange CurveKeyPair
remotePublic [ed25519.PublicKeySize]byte // long-term
secret, secret2, secret3 [32]byte
hello []byte
aBob, bAlice [32]byte // better name? helloAlice, helloBob?
}
// EdKeyPair is a keypair for use with github.com/agl/ed25519
type EdKeyPair struct {
Public [ed25519.PublicKeySize]byte
Secret [ed25519.PrivateKeySize]byte
}
// CurveKeyPair is a keypair for use with github.com/agl/ed25519
type CurveKeyPair struct {
Public [32]byte
Secret [32]byte
}
// NewClientState initializes the state for the client side
func NewClientState(appKey []byte, local EdKeyPair, remotePublic [ed25519.PublicKeySize]byte) (*State, error) {
state, err := newState(appKey, local)
if err != nil {
return state, err
}
state.remotePublic = remotePublic
return state, err
}
// NewServerState initializes the state for the server side
func NewServerState(appKey []byte, local EdKeyPair) (*State, error) {
return newState(appKey, local)
}
// newState initializes the state needed by both client and server
func newState(appKey []byte, local EdKeyPair) (*State, error) {
pubKey, secKey, _ := box.GenerateKey(rand.Reader)
s := State{
appKey: appKey,
}
copy(s.localExchange.Public[:], pubKey[:])
copy(s.localExchange.Secret[:], secKey[:])
s.local = local
return &s, nil
}
// createChallenge returns a buffer with a challenge
func (s *State) createChallenge() []byte {
appMacr := hmac.New(sha512.New, s.appKey[:32])
appMacr.Write(s.localExchange.Public[:])
s.localAppMac = appMacr.Sum(nil)[:32]
return append(s.localAppMac, s.localExchange.Public[:]...)
}
// verifyChallenge returns whether the passed buffer is valid
func (s *State) verifyChallenge(ch []byte) bool {
mac := ch[:32]
remoteEphPubKey := ch[32:]
appMac := hmac.New(sha512.New, s.appKey[:32])
appMac.Write(remoteEphPubKey)
ok := hmac.Equal(appMac.Sum(nil)[:32], mac)
copy(s.remoteExchange.Public[:], remoteEphPubKey)
s.remoteAppMac = mac
var sec [32]byte
curve25519.ScalarMult(&sec, &s.localExchange.Secret, &s.remoteExchange.Public)
copy(s.secret[:], sec[:])
secHasher := sha256.New()
secHasher.Write(s.secret[:])
s.secHash = secHasher.Sum(nil)
return ok
}
// createClientAuth returns a buffer containing a clientAuth message
func (s *State) createClientAuth() []byte {
var curveRemotePubKey [32]byte
extra25519.PublicKeyToCurve25519(&curveRemotePubKey, &s.remotePublic)
var aBob [32]byte
curve25519.ScalarMult(&aBob, &s.localExchange.Secret, &curveRemotePubKey)
copy(s.aBob[:], aBob[:])
secHasher := sha256.New()
secHasher.Write(s.appKey)
secHasher.Write(s.secret[:])
secHasher.Write(s.aBob[:])
copy(s.secret2[:], secHasher.Sum(nil))
var sigMsg bytes.Buffer
sigMsg.Write(s.appKey)
sigMsg.Write(s.remotePublic[:])
sigMsg.Write(s.secHash)
sig := ed25519.Sign(&s.local.Secret, sigMsg.Bytes())
var helloBuf bytes.Buffer
helloBuf.Write(sig[:])
helloBuf.Write(s.local.Public[:])
s.hello = helloBuf.Bytes()
out := make([]byte, 0, len(s.hello)-box.Overhead)
var n [24]byte
out = box.SealAfterPrecomputation(out, s.hello, &n, &s.secret2)
return out
}
var nullHello [ed25519.SignatureSize + ed25519.PublicKeySize]byte
// verifyClientAuth returns whether a buffer contains a valid clientAuth message
func (s *State) verifyClientAuth(data []byte) bool {
var cvSec, aBob [32]byte
extra25519.PrivateKeyToCurve25519(&cvSec, &s.local.Secret)
curve25519.ScalarMult(&aBob, &cvSec, &s.remoteExchange.Public)
copy(s.aBob[:], aBob[:])
secHasher := sha256.New()
secHasher.Write(s.appKey)
secHasher.Write(s.secret[:])
secHasher.Write(s.aBob[:])
copy(s.secret2[:], secHasher.Sum(nil))
s.hello = make([]byte, 0, len(data)-16)
var nonce [24]byte // always 0?
var openOk bool
s.hello, openOk = box.OpenAfterPrecomputation(s.hello, data, &nonce, &s.secret2)
var sig [ed25519.SignatureSize]byte
var public [ed25519.PublicKeySize]byte
/* TODO: is this const time!?!
this is definetly not:
if !openOK {
s.hello = nullHello
}
copy(sig, ...)
copy(pub, ...)
*/
if openOk {
copy(sig[:], s.hello[:ed25519.SignatureSize])
copy(public[:], s.hello[ed25519.SignatureSize:])
} else {
copy(sig[:], nullHello[:ed25519.SignatureSize])
copy(public[:], nullHello[ed25519.SignatureSize:])
}
var sigMsg bytes.Buffer
sigMsg.Write(s.appKey)
sigMsg.Write(s.local.Public[:])
sigMsg.Write(s.secHash)
verifyOk := ed25519.Verify(&public, sigMsg.Bytes(), &sig)
copy(s.remotePublic[:], public[:])
return openOk && verifyOk
}
// createServerAccept returns a buffer containing a serverAccept message
func (s *State) createServerAccept() []byte {
var curveRemotePubKey [32]byte
extra25519.PublicKeyToCurve25519(&curveRemotePubKey, &s.remotePublic)
var bAlice [32]byte
curve25519.ScalarMult(&bAlice, &s.localExchange.Secret, &curveRemotePubKey)
copy(s.bAlice[:], bAlice[:])
secHasher := sha256.New()
secHasher.Write(s.appKey)
secHasher.Write(s.secret[:])
secHasher.Write(s.aBob[:])
secHasher.Write(s.bAlice[:])
copy(s.secret3[:], secHasher.Sum(nil))
var sigMsg bytes.Buffer
sigMsg.Write(s.appKey)
sigMsg.Write(s.hello[:])
sigMsg.Write(s.secHash)
okay := ed25519.Sign(&s.local.Secret, sigMsg.Bytes())
var out = make([]byte, 0, len(okay)+16)
var nonce [24]byte // always 0?
return box.SealAfterPrecomputation(out, okay[:], &nonce, &s.secret3)
}
// verifyServerAccept returns whether the passed buffer contains a valid serverAccept message
func (s *State) verifyServerAccept(boxedOkay []byte) bool {
var curveLocalSec [32]byte
extra25519.PrivateKeyToCurve25519(&curveLocalSec, &s.local.Secret)
var bAlice [32]byte
curve25519.ScalarMult(&bAlice, &curveLocalSec, &s.remoteExchange.Public)
copy(s.bAlice[:], bAlice[:])
secHasher := sha256.New()
secHasher.Write(s.appKey)
secHasher.Write(s.secret[:])
secHasher.Write(s.aBob[:])
secHasher.Write(s.bAlice[:])
copy(s.secret3[:], secHasher.Sum(nil))
var nonce [24]byte // always 0?
out := make([]byte, 0, len(boxedOkay)-16)
out, openOk := box.OpenAfterPrecomputation(out, boxedOkay, &nonce, &s.secret3)
var sig [ed25519.SignatureSize]byte
copy(sig[:], out)
var sigMsg bytes.Buffer
sigMsg.Write(s.appKey)
sigMsg.Write(s.hello[:])
sigMsg.Write(s.secHash)
return ed25519.Verify(&s.remotePublic, sigMsg.Bytes(), &sig) && openOk
}
// cleanSecrets overwrites all intermediate secrets and copies the final secret to s.secret
func (s *State) cleanSecrets() {
var zeros [64]byte
copy(s.secHash, zeros[:])
copy(s.secret[:], zeros[:]) // redundant
copy(s.aBob[:], zeros[:])
copy(s.bAlice[:], zeros[:])
h := sha256.New()
h.Write(s.secret3[:])
copy(s.secret[:], h.Sum(nil))
copy(s.secret2[:], zeros[:])
copy(s.secret3[:], zeros[:])
copy(s.localExchange.Secret[:], zeros[:])
}
// Remote returns the public key of the remote party
func (s *State) Remote() []byte {
return s.remotePublic[:]
}
// GetBoxstreamEncKeys returns the encryption key and nonce suitable for boxstream
func (s *State) GetBoxstreamEncKeys() ([32]byte, [24]byte) {
// TODO: error before cleanSecrets() has been called?
var enKey [32]byte
h := sha256.New()
h.Write(s.secret[:])
h.Write(s.remotePublic[:])
copy(enKey[:], h.Sum(nil))
var nonce [24]byte
copy(nonce[:], s.remoteAppMac)
return enKey, nonce
}
// GetBoxstreamDecKeys returns the decryption key and nonce suitable for boxstream
func (s *State) GetBoxstreamDecKeys() ([32]byte, [24]byte) {
// TODO: error before cleanSecrets() has been called?
var deKey [32]byte
h := sha256.New()
h.Write(s.secret[:])
h.Write(s.local.Public[:])
copy(deKey[:], h.Sum(nil))
var nonce [24]byte
copy(nonce[:], s.localAppMac)
return deKey, nonce
}