@@ -154,6 +154,7 @@ func init() {
154
154
}
155
155
156
156
func (device * Device ) CreateMessageInitiation (peer * Peer ) (* MessageInitiation , error ) {
157
+ var errZeroECDHResult = errors .New ("ECDH returned all zeros" )
157
158
158
159
device .staticIdentity .RLock ()
159
160
defer device .staticIdentity .RUnlock ()
@@ -162,12 +163,7 @@ func (device *Device) CreateMessageInitiation(peer *Peer) (*MessageInitiation, e
162
163
handshake .mutex .Lock ()
163
164
defer handshake .mutex .Unlock ()
164
165
165
- if isZero (handshake .precomputedStaticStatic [:]) {
166
- return nil , errors .New ("static shared secret is zero" )
167
- }
168
-
169
166
// create ephemeral key
170
-
171
167
var err error
172
168
handshake .hash = InitialHash
173
169
handshake .chainKey = InitialChainKey
@@ -176,56 +172,53 @@ func (device *Device) CreateMessageInitiation(peer *Peer) (*MessageInitiation, e
176
172
return nil , err
177
173
}
178
174
179
- // assign index
180
-
181
- device .indexTable .Delete (handshake .localIndex )
182
- handshake .localIndex , err = device .indexTable .NewIndexForHandshake (peer , handshake )
183
-
184
- if err != nil {
185
- return nil , err
186
- }
187
-
188
175
handshake .mixHash (handshake .remoteStatic [:])
189
176
190
177
msg := MessageInitiation {
191
178
Type : MessageInitiationType ,
192
179
Ephemeral : handshake .localEphemeral .publicKey (),
193
- Sender : handshake .localIndex ,
194
180
}
195
181
196
182
handshake .mixKey (msg .Ephemeral [:])
197
183
handshake .mixHash (msg .Ephemeral [:])
198
184
199
185
// encrypt static key
200
-
201
- func ( ) {
202
- var key [ chacha20poly1305 . KeySize ] byte
203
- ss := handshake . localEphemeral . sharedSecret ( handshake . remoteStatic )
204
- KDF2 (
205
- & handshake . chainKey ,
206
- & key ,
207
- handshake . chainKey [:] ,
208
- ss [:],
209
- )
210
- aead , _ := chacha20poly1305 . New ( key [:] )
211
- aead . Seal ( msg . Static [: 0 ], ZeroNonce [:], device . staticIdentity . publicKey [:], handshake . hash [:])
212
- }( )
186
+ ss := handshake . localEphemeral . sharedSecret ( handshake . remoteStatic )
187
+ if isZero ( ss [:] ) {
188
+ return nil , errZeroECDHResult
189
+ }
190
+ var key [ chacha20poly1305 . KeySize ] byte
191
+ KDF2 (
192
+ & handshake . chainKey ,
193
+ & key ,
194
+ handshake . chainKey [:],
195
+ ss [:],
196
+ )
197
+ aead , _ := chacha20poly1305 . New ( key [:])
198
+ aead . Seal ( msg . Static [: 0 ], ZeroNonce [:], device . staticIdentity . publicKey [:], handshake . hash [:] )
213
199
handshake .mixHash (msg .Static [:])
214
200
215
201
// encrypt timestamp
216
-
202
+ if isZero (handshake .precomputedStaticStatic [:]) {
203
+ return nil , errZeroECDHResult
204
+ }
205
+ KDF2 (
206
+ & handshake .chainKey ,
207
+ & key ,
208
+ handshake .chainKey [:],
209
+ handshake .precomputedStaticStatic [:],
210
+ )
217
211
timestamp := tai64n .Now ()
218
- func () {
219
- var key [chacha20poly1305 .KeySize ]byte
220
- KDF2 (
221
- & handshake .chainKey ,
222
- & key ,
223
- handshake .chainKey [:],
224
- handshake .precomputedStaticStatic [:],
225
- )
226
- aead , _ := chacha20poly1305 .New (key [:])
227
- aead .Seal (msg .Timestamp [:0 ], ZeroNonce [:], timestamp [:], handshake .hash [:])
228
- }()
212
+ aead , _ = chacha20poly1305 .New (key [:])
213
+ aead .Seal (msg .Timestamp [:0 ], ZeroNonce [:], timestamp [:], handshake .hash [:])
214
+
215
+ // assign index
216
+ device .indexTable .Delete (handshake .localIndex )
217
+ msg .Sender , err = device .indexTable .NewIndexForHandshake (peer , handshake )
218
+ if err != nil {
219
+ return nil , err
220
+ }
221
+ handshake .localIndex = msg .Sender
229
222
230
223
handshake .mixHash (msg .Timestamp [:])
231
224
handshake .state = HandshakeInitiationCreated
@@ -250,16 +243,16 @@ func (device *Device) ConsumeMessageInitiation(msg *MessageInitiation) *Peer {
250
243
mixKey (& chainKey , & InitialChainKey , msg .Ephemeral [:])
251
244
252
245
// decrypt static key
253
-
254
246
var err error
255
247
var peerPK NoisePublicKey
256
- func () {
257
- var key [chacha20poly1305 .KeySize ]byte
258
- ss := device .staticIdentity .privateKey .sharedSecret (msg .Ephemeral )
259
- KDF2 (& chainKey , & key , chainKey [:], ss [:])
260
- aead , _ := chacha20poly1305 .New (key [:])
261
- _ , err = aead .Open (peerPK [:0 ], ZeroNonce [:], msg .Static [:], hash [:])
262
- }()
248
+ var key [chacha20poly1305 .KeySize ]byte
249
+ ss := device .staticIdentity .privateKey .sharedSecret (msg .Ephemeral )
250
+ if isZero (ss [:]) {
251
+ return nil
252
+ }
253
+ KDF2 (& chainKey , & key , chainKey [:], ss [:])
254
+ aead , _ := chacha20poly1305 .New (key [:])
255
+ _ , err = aead .Open (peerPK [:0 ], ZeroNonce [:], msg .Static [:], hash [:])
263
256
if err != nil {
264
257
return nil
265
258
}
@@ -273,23 +266,24 @@ func (device *Device) ConsumeMessageInitiation(msg *MessageInitiation) *Peer {
273
266
}
274
267
275
268
handshake := & peer .handshake
276
- if isZero (handshake .precomputedStaticStatic [:]) {
277
- return nil
278
- }
279
269
280
270
// verify identity
281
271
282
272
var timestamp tai64n.Timestamp
283
- var key [chacha20poly1305 .KeySize ]byte
284
273
285
274
handshake .mutex .RLock ()
275
+
276
+ if isZero (handshake .precomputedStaticStatic [:]) {
277
+ handshake .mutex .RUnlock ()
278
+ return nil
279
+ }
286
280
KDF2 (
287
281
& chainKey ,
288
282
& key ,
289
283
chainKey [:],
290
284
handshake .precomputedStaticStatic [:],
291
285
)
292
- aead , _ : = chacha20poly1305 .New (key [:])
286
+ aead , _ = chacha20poly1305 .New (key [:])
293
287
_ , err = aead .Open (timestamp [:0 ], ZeroNonce [:], msg .Timestamp [:], hash [:])
294
288
if err != nil {
295
289
handshake .mutex .RUnlock ()
0 commit comments