-
Notifications
You must be signed in to change notification settings - Fork 1
/
handshake_initiator.go
218 lines (170 loc) · 5.88 KB
/
handshake_initiator.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
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
// SPDX-FileCopyrightText: 2023 Steffen Vogel <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package rosenpass
import (
"crypto/rand"
"encoding/binary"
"fmt"
"log/slog"
"math"
"math/big"
"time"
)
type initiatorHandshake struct {
handshake
nextMsg msgType // The type of the next expected message
eski esk // The initiator’s ephemeral secret key
expiryTimer *time.Timer
txTimer *time.Timer
txRetryCount uint
biscuit sealedBiscuit
}
func (hs *initiatorHandshake) Close() error {
hs.expiryTimer.Stop()
return nil
}
// Step 1.
func (hs *initiatorHandshake) sendInitHello() error {
var err error
// IHI1: Initialize the chaining key, and bind to the responder’s public key.
hs.ck = khCKI.hash(hs.peer.spkt[:])
// IHI2: The session ID is used to associate packets with the handshake state.
if hs.sidi, err = generateSessionID(); err != nil {
return err
}
// IHI3: Generate fresh ephemeral keys, for forward secrecy.
if hs.epki, hs.eski, err = generateEphemeralKeyPair(); err != nil {
return err
}
// IHI4: InitHello includes sidi and epki as part of the protocol transcript, and so we
// mix them into the chaining key to prevent tampering.
hs.mix(hs.sidi[:], hs.epki[:])
// IHI5: Key encapsulation using the responder’s public key. Mixes public key, shared
// secret, and ciphertext into the chaining key, and authenticates the responder.
sctr, err := hs.encapAndMix(kemStatic, hs.peer.spkt[:])
if err != nil {
return err
}
// IHI6: Tell the responder who the initiator is by transmitting the peer ID.
pidi := hs.server.PID()
pidiC, err := hs.encryptAndMix(pidi[:])
if err != nil {
return err
}
// IHI7: Ensure the responder has the correct view on spki. Mix in the PSK as optional
// static symmetric key, with epki and spkr serving as nonces.
hs.mix(hs.server.spkm[:], hs.peer.psk[:])
// IHI8: Add a message authentication code to ensure both participants agree on the
// session state and protocol transcript at this point.
auth, err := hs.encryptAndMix([]byte{})
if err != nil {
return err
}
hs.nextMsg = msgTypeRespHello
return hs.send(&initHello{
sidi: hs.sidi,
epki: hs.epki,
sctr: sct(sctr),
pidiC: [pidSize + authSize]byte(pidiC),
auth: authTag(auth),
})
}
// Step 4.
func (hs *initiatorHandshake) handleRespHello(r *respHello) error {
hs.biscuit = r.biscuit
hs.sidr = r.sidr
// RHI2: Initiator looks up their session state using the session ID they generated.
// See: Server.handleEnvelope()
// RHI3: Mix both session IDs as part of the protocol transcript.
// TODO: Take local or exchanged ones?
hs.mix(hs.sidr[:], hs.sidi[:])
// RHI4: Key encapsulation using the ephemeral key, to provide forward secrecy.
if err := hs.decapAndMix(kemEphemeral, hs.eski[:], hs.epki[:], r.ecti[:]); err != nil {
return fmt.Errorf("failed to decapsulate (RHI4): %w", err)
}
// RHI5: Key encapsulation using the initiator’s static key, to authenticate the
// initiator, and non-forward-secret confidentiality.
if err := hs.decapAndMix(kemStatic, hs.server.sskm[:], hs.server.spkm[:], r.scti[:]); err != nil {
return fmt.Errorf("failed to decapsulate (RHI5): %w", err)
}
// RHI6: The responder transmits their state to the initiator in an encrypted container
// to avoid having to store state.
hs.mix(r.biscuit[:])
// RHI7: Add a message authentication code for the same reason as above.
if _, err := hs.decryptAndMix(r.auth[:]); err != nil {
return fmt.Errorf("%w (RHI7): %w", errInvalidAuthTag, err)
}
return nil
}
// Step 5.
func (hs *initiatorHandshake) sendInitConf() error {
// ICI3: Mix both session IDs as part of the protocol transcript.
hs.mix(hs.sidi[:], hs.sidr[:])
// ICI4: Message authentication code for the same reason as above, which in particular
// ensures that both participants agree on the final chaining key.
auth, err := hs.encryptAndMix([]byte{})
if err != nil {
return fmt.Errorf("failed to create authentication tag (ICI4): %w", err)
}
// ICI7: Derive the transmission keys, and the output shared key for use as WireGuard’s PSK.
hs.enterLive()
return hs.send(&initConf{
sidi: hs.sidi,
sidr: hs.sidr,
biscuit: hs.biscuit,
auth: authTag(auth),
})
}
// Step 8.
func (hs *initiatorHandshake) handleEmptyData(e *emptyData) error {
n := append(e.ctr[:], 0, 0, 0, 0) //nolint:gocritic
txnt := binary.LittleEndian.Uint64(e.ctr[:])
// TODO: Check nonce counter
if txnt < hs.txnt {
return errStaleNonce
}
hs.txnt = txnt
aead, err := newAEAD(hs.txkt)
if err != nil {
return err
}
if _, err := aead.Open(nil, n, e.auth[:], []byte{}); err != nil {
return errInvalidAuthTag
}
return nil
}
// Retransmission
func (hs *initiatorHandshake) retransmitDelay() time.Duration {
after := RetransmitDelayBegin.Seconds() * math.Pow(RetransmitDelayGrowth, float64(hs.txRetryCount))
if after > RetransmitDelayEnd.Seconds() {
after = RetransmitDelayEnd.Seconds()
}
if n, err := rand.Int(rand.Reader, big.NewInt(math.MaxInt64)); err == nil {
frand := float64(n.Int64()) / math.MaxUint64
after += (2*frand - 1) * RetransmitDelayJitter.Seconds()
}
return time.Duration(after*1e6) * time.Microsecond
}
func (hs *initiatorHandshake) scheduleRetransmission(pl payload) {
hs.txTimer = time.AfterFunc(hs.retransmitDelay(), func() {
hs.txRetryCount++
switch pl.(type) {
case *initHello, *initConf: // Only InitHello and InitConf messages are retransmitted
hs.scheduleRetransmission(pl)
}
if err := hs.server.conn.Send(pl, hs.peer.spkt, hs.peer.endpoint); err != nil {
hs.peer.logger.Error("Failed to send", slog.Any("error", err))
}
})
}
func (hs *initiatorHandshake) send(pl payload) error {
hs.txRetryCount = 0
hs.scheduleRetransmission(pl)
return hs.handshake.send(pl, hs.peer.endpoint)
}
// Helpers
func (hs *initiatorHandshake) enterLive() {
hs.txkm = hs.ck.hash(khIniEnc[:])
hs.txkt = hs.ck.hash(khResEnc[:])
hs.handshake.enterLive()
}