-
Notifications
You must be signed in to change notification settings - Fork 15
/
patterns.go
281 lines (257 loc) · 6.33 KB
/
patterns.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
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
package libdisco
//
// Handshake Patterns
//
type noiseHandshakeType int8
const (
// NoiseUnknown is for specifying an unknown pattern
NoiseUnknown noiseHandshakeType = iota
// NoiseN is a one-way pattern where a client can send
// data to a server with a known static key. The server
// can only receive data and cannot reply back.
NoiseN
// NoiseK is a one-way pattern where a client can send
// data to a server with a known static key. The server
// can only receive data and cannot reply back. The server
// authenticates the client via a known key.
NoiseK
// NoiseX is a one-way pattern where a client can send
// data to a server with a known static key. The server
// can only receive data and cannot reply back. The server
// authenticates the client via a key transmitted as part
// of the handshake.
NoiseX
// NoiseKK is a pattern where both the client static key and the
// server static key are known.
NoiseKK
// NoiseNX is a "HTTPS"-like pattern where the client is
// not authenticated, and the static public key of the server
// is transmitted during the handshake. It is the responsability of the client to validate the received key properly.
NoiseNX
// NoiseNK is a "Public Key Pinning"-like pattern where the client
// is not authenticated, and the static public key of the server
// is already known.
NoiseNK
// NoiseXX is a pattern where both static keys are transmitted.
// It is the responsability of the server and of the client to
// validate the received keys properly.
NoiseXX
// NoiseKX Not documented
NoiseKX
// NoiseXK Not documented
NoiseXK
// NoiseIK Not documented
NoiseIK
// NoiseIX Not documented
NoiseIX
// NoiseNNpsk2 Not documented
NoiseNNpsk2
// NoiseNN Not implemented
NoiseNN
// NoiseKN Not implemented
NoiseKN
// NoiseXN Not implemented
NoiseXN
// NoiseIN Not implemented
NoiseIN
)
type token uint8
const (
tokenUnknown token = iota
token_e
token_s
token_es
token_se
token_ss
token_ee
token_psk
)
type messagePattern []token
type handshakePattern struct {
name string
preMessagePatterns []messagePattern
messagePatterns []messagePattern
}
// TODO: add more patterns
var patterns = map[noiseHandshakeType]handshakePattern{
// 7.2. One-way patterns
NoiseN: handshakePattern{
name: "N",
preMessagePatterns: []messagePattern{
messagePattern{}, // →
messagePattern{token_s}, // ←
},
messagePatterns: []messagePattern{
messagePattern{token_e, token_es}, // →
},
},
/*
K(s, rs):
-> s
<- s
...
-> e, es, ss
*/
NoiseK: handshakePattern{
name: "K",
preMessagePatterns: []messagePattern{
messagePattern{token_s}, // →
messagePattern{token_s}, // ←
},
messagePatterns: []messagePattern{
messagePattern{token_e, token_es, token_ss}, // →
},
},
/*
X(s, rs):
<- s
...
-> e, es, s, ss
*/
NoiseX: handshakePattern{
name: "X",
preMessagePatterns: []messagePattern{
messagePattern{}, // →
messagePattern{token_s}, // ←
},
messagePatterns: []messagePattern{
messagePattern{token_e, token_es, token_s, token_ss}, // →
},
},
//
// 7.3. Interactive patterns
//
NoiseKK: handshakePattern{
name: "KK",
preMessagePatterns: []messagePattern{
messagePattern{token_s}, // →
messagePattern{token_s}, // ←
},
messagePatterns: []messagePattern{
messagePattern{token_e, token_es, token_ss}, // →
messagePattern{token_e, token_ee, token_se}, // ←
},
},
NoiseNX: handshakePattern{
name: "NX",
preMessagePatterns: []messagePattern{
messagePattern{}, // →
messagePattern{}, // ←
},
messagePatterns: []messagePattern{
messagePattern{token_e}, // →
messagePattern{token_e, token_ee, token_s, token_es}, // ←
},
},
NoiseNK: handshakePattern{
name: "NK",
preMessagePatterns: []messagePattern{
messagePattern{}, // →
messagePattern{token_s}, // ←
},
messagePatterns: []messagePattern{
messagePattern{token_e, token_es}, // →
messagePattern{token_e, token_ee}, // ←
},
},
NoiseXX: handshakePattern{
name: "XX",
preMessagePatterns: []messagePattern{
messagePattern{}, // →
messagePattern{}, // ←
},
messagePatterns: []messagePattern{
messagePattern{token_e}, // →
messagePattern{token_e, token_ee, token_s, token_es}, // ←
messagePattern{token_s, token_se}, // →
},
},
/*
KX(s, rs):
-> s
...
-> e
<- e, ee, se, s, es
*/
NoiseKX: handshakePattern{
name: "KX",
preMessagePatterns: []messagePattern{
messagePattern{token_s}, // →
messagePattern{}, // ←
},
messagePatterns: []messagePattern{
messagePattern{token_e}, // →
messagePattern{token_e, token_ee, token_se, token_s, token_es}, // ←
},
},
/*
XK(s, rs):
<- s
...
-> e, es
<- e, ee
-> s, se
*/
NoiseXK: handshakePattern{
name: "XK",
preMessagePatterns: []messagePattern{
messagePattern{}, // →
messagePattern{token_s}, // ←
},
messagePatterns: []messagePattern{
messagePattern{token_e, token_es}, // →
messagePattern{token_e, token_ee}, // ←
messagePattern{token_s, token_se}, // →
},
},
/*
IK(s, rs):
<- s
...
-> e, es, s, ss
<- e, ee, se
*/
NoiseIK: handshakePattern{
name: "IK",
preMessagePatterns: []messagePattern{
messagePattern{}, // →
messagePattern{token_s}, // ←
},
messagePatterns: []messagePattern{
messagePattern{token_e, token_es, token_s, token_ss}, // →
messagePattern{token_e, token_ee, token_se}, // ←
},
},
/*
IX(s, rs):
-> e, s
<- e, ee, se, s, es
*/
NoiseIX: handshakePattern{
name: "IX",
preMessagePatterns: []messagePattern{
messagePattern{}, // →
messagePattern{}, // ←
},
messagePatterns: []messagePattern{
messagePattern{token_e, token_s}, // →
messagePattern{token_e, token_ee, token_se, token_s, token_es}, // ←
},
},
/*
NNpsk2():
-> e
<- e, ee, psk
*/
NoiseNNpsk2: handshakePattern{
name: "NNpsk2",
preMessagePatterns: []messagePattern{
messagePattern{}, // →
messagePattern{}, // ←
},
messagePatterns: []messagePattern{
messagePattern{token_e}, // →
messagePattern{token_e, token_ee, token_psk}, // ←
},
},
}