@@ -154,6 +154,7 @@ func init() {
154154}
155155
156156func (device * Device ) CreateMessageInitiation (peer * Peer ) (* MessageInitiation , error ) {
157+ var errZeroECDHResult = errors .New ("ECDH returned all zeros" )
157158
158159 device .staticIdentity .RLock ()
159160 defer device .staticIdentity .RUnlock ()
@@ -162,12 +163,7 @@ func (device *Device) CreateMessageInitiation(peer *Peer) (*MessageInitiation, e
162163 handshake .mutex .Lock ()
163164 defer handshake .mutex .Unlock ()
164165
165- if isZero (handshake .precomputedStaticStatic [:]) {
166- return nil , errors .New ("static shared secret is zero" )
167- }
168-
169166 // create ephemeral key
170-
171167 var err error
172168 handshake .hash = InitialHash
173169 handshake .chainKey = InitialChainKey
@@ -176,56 +172,53 @@ func (device *Device) CreateMessageInitiation(peer *Peer) (*MessageInitiation, e
176172 return nil , err
177173 }
178174
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-
188175 handshake .mixHash (handshake .remoteStatic [:])
189176
190177 msg := MessageInitiation {
191178 Type : MessageInitiationType ,
192179 Ephemeral : handshake .localEphemeral .publicKey (),
193- Sender : handshake .localIndex ,
194180 }
195181
196182 handshake .mixKey (msg .Ephemeral [:])
197183 handshake .mixHash (msg .Ephemeral [:])
198184
199185 // 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 [:] )
213199 handshake .mixHash (msg .Static [:])
214200
215201 // 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+ )
217211 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
229222
230223 handshake .mixHash (msg .Timestamp [:])
231224 handshake .state = HandshakeInitiationCreated
@@ -250,16 +243,16 @@ func (device *Device) ConsumeMessageInitiation(msg *MessageInitiation) *Peer {
250243 mixKey (& chainKey , & InitialChainKey , msg .Ephemeral [:])
251244
252245 // decrypt static key
253-
254246 var err error
255247 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 [:])
263256 if err != nil {
264257 return nil
265258 }
@@ -273,23 +266,24 @@ func (device *Device) ConsumeMessageInitiation(msg *MessageInitiation) *Peer {
273266 }
274267
275268 handshake := & peer .handshake
276- if isZero (handshake .precomputedStaticStatic [:]) {
277- return nil
278- }
279269
280270 // verify identity
281271
282272 var timestamp tai64n.Timestamp
283- var key [chacha20poly1305 .KeySize ]byte
284273
285274 handshake .mutex .RLock ()
275+
276+ if isZero (handshake .precomputedStaticStatic [:]) {
277+ handshake .mutex .RUnlock ()
278+ return nil
279+ }
286280 KDF2 (
287281 & chainKey ,
288282 & key ,
289283 chainKey [:],
290284 handshake .precomputedStaticStatic [:],
291285 )
292- aead , _ : = chacha20poly1305 .New (key [:])
286+ aead , _ = chacha20poly1305 .New (key [:])
293287 _ , err = aead .Open (timestamp [:0 ], ZeroNonce [:], msg .Timestamp [:], hash [:])
294288 if err != nil {
295289 handshake .mutex .RUnlock ()
0 commit comments