summaryrefslogtreecommitdiffstats
path: root/wireguard.m4
blob: c34b92cb3af53fb47e18193ed962fb5036b8516d (plain) (blame)
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
dnl // Tamarin uses ' which is an m4 close quote. So use <! !> for quoting instead.
changequote(<!,!>)
changecom(<!@,@!>)
/*
 * Protocol:    Wireguard protocol
 * Modeler: 	Kevin Milner
 * Date: 	    February 2017
 * Source:	    Original
 * Status: 	    WIP
 */

theory WireGuard
begin

builtins: hashing, diffie-hellman
//We're not going to model the nonce in the aead, since it's always 0
//in the handshake. Note that this can only over-approximate attacks,
//since it would allow collisions between two AEADs with different nonces.
functions: aead/3, decrypt/2
equations: decrypt(aead(k,p,a),k) = p     //Plaintext can be recovered using the key

//Uncomment these to prevent any adversary message modifications,
//useful for checking exists-trace lemmas and making sure messsages
//don't require the adversary to reorder terms etc.
dnl define(In, AuthenticatedMessage($*))
dnl define(Out, AuthenticatedMessage($*))

define(AgentKey, F_AgentKey($*))
define(StateInvariants, F_StateInvariants($*))

restriction Eq_testing: "All x y #i. Eq(x,y) @ i ==> x = y"
restriction InEq_testing: "All x y #i. InEq(x,y) @ i ==> not(x = y)"

//Pre-established trust relationships
restriction pairings_unique:
    "All id id2 ka kb #i #j.
        Paired(id,ka,kb) @ i & Paired(id2,ka,kb) @ j
    ==> #i = #j"

//Keygen is separate from pairing to allow keys to be paired
//more than once (if they were generated fresh in the pairing
//this would not be possible)
rule AgentKeyGen:
    [ Fr(~ltk) ]
    --[DHKey(~ltk)]->
    [!AgentKey(~ltk), Out('g'^~ltk)]

//Semi-malicious or ignorant agents might share
//a PSK with multiple parties, so we'll overapproximate this
//by allowing it to be shared arbitrarily. If this finds an
//attack then it may not be a 'real' attack, but if it doesn't
//then there is no problem with PSK reuse.
rule PSKKeyGen:
    [ Fr(~psk) ]
    --[PSKey(~psk)]->
    [ !AgentPSK(~psk) ]

//Models an agent adding anothers public key out-of-band.
rule AddPublicKey:
    let pkB = 'g'^~ltkB in
    [ !AgentKey(~ltkA)
    , !AgentKey(~ltkB)
    , Fr(~id)
    ]--[
      InEq($A,$B)
    ]->
    [ // For search efficiency, state is divided into
      // an invariant portion and a variant portion. This
      // allows tamarin to immediately bind the keys back
      // to this initial pairing rule.
      State(~id, 'nopsk', 'nostate')
    , !StateInvariants(~id, ~ltkA, pkB, pkB^~ltkA)
    ]

/* Uncomment to allow various key compromises
 rule CompromiseKey:
    [!AgentKey(ltk)]
    --[ CompromiseLTK(ltk) ]->
    [Out(ltk)]

rule CompromisePSK:
    [!AgentPSK(psk)]
    --[ CompromisePSK(psk) ]->
    [Out(psk)]
*/

// MESSAGE RULES
rule Handshake_Init:
    let pkI   = 'g'^~ltkI
        pekI  = 'g'^~ekI
        eisr  = pkR^~ekI
        //Constructing the init message m1:
        cii   = h('nopsk')
        hii   = h(<cii,'id',pkR,pekI>)  //No PSK means no intermediate hashes necessary here
        ki0   = h(<cii,eisr,'2'>)
        ci0   = h(<cii,eisr,'1'>)
        astat = aead(ki0,pkI,hii)
        hi0   = h(<hii,astat>)
        ki1   = h(<ci0,sisr,'2'>)
        ci1   = h(<ci0,sisr,'1'>)
        ats   = aead(ki1,~ts,hi0)
        hi1   = h(<hi0,ats>)
        m1    = <'1',~sidI,pekI,astat,ats,$mac1,$mac2> in //TODO:Model MACs
    [ //Init can be triggered at any time, even when currently performing
      //another role or on another stage. This could happen e.g. because of a timeout.
      //As such we bind the previous state as 'anything' and discard it.
      State(~id, 'nopsk',anything)
    , !StateInvariants(~id, ~ltkI, pkR, sisr)
    , Fr(~sidI)
    , Fr(~ekI)
    , Fr(~ts)
    ]--[
      StartHandshake(pkI,pkR,pekI,'nopsk',~sidI)
    ]->
    [ //We cache DH sisr to reduce variant precomputation time for tamarin.
      State(~id,'nopsk',<'init',~sidI,~ekI,ci1,ki1,hi1>)
    , Out(m1)
    ]

rule Handshake_Resp:
    let pkR   = 'g'^~ltkR
        pekR  = 'g'^~ekR
        eisr  = pekI^~ltkR
        eier  = pekI^~ekR
        sier  = pkI^~ekR
        //Reconstructing what should be in m1:
        cri   = h('nopsk')
        hri   = h(<cri,'id',pkR,pekI>)  //No PSK means no intermediate hashes necessary here
        kr0   = h(<cri,eisr,'2'>)
        cr0   = h(<cri,eisr,'1'>)
        astat = aead(kr0,pkI,hri)
        hr0   = h(<hri,astat>)
        kr1   = h(<cr0,sisr,'2'>)
        cr1   = h(<cr0,sisr,'1'>)
        ats   = aead(kr1,ts,hr0)
        hr1   = h(<hr0,ats>)
        m1    = <'1',sidI,pekI,astat,ats,$mac1,$mac2> //TODO:Model MACs
        //Constructing the response message m2:
        hr2   = h(<hr1,pekR>)
        kr2   = h(<cr1,eier,'2'>)
        cr2   = h(<cr1,eier,'1'>)
        kr3   = h(<cr2,sier,'2'>)
        cr3   = h(<cr2,sier,'1'>)
        aempt = aead(kr3,'0',hr2)
        hr3   = h(<hr2,aempt>)
        m2    = <'2',sidI,~sidR,pekR,aempt,$mac1,$mac2> in
    [ State(~id,'nopsk',anything)
    , !StateInvariants(~id,~ltkR,pkI,sisr)
    , Fr(~ekR)
    , Fr(~sidR)
    , In(m1)
    ]--[
      RKeys(<pkI,pkR,pekI,pekR,'nopsk',cr3>) //Not actually cr3, but derived from it.
    , RSessionID(sidI,~sidR)
    ]->
    [ State(~id,'nopsk',<'transport',cr3>)
    , Out(m2)
    ]

rule Handshake_Complete:
    let pkI   = 'g'^~ltkI
        pekI  = 'g'^~ekI
        eier  = pekR^~ekI
        sier  = pekR^~ltkI
        //Reconstruct what should be in m2:
        hi2   = h(<hi1,pekR>)
        ki2   = h(<ci1,eier,'2'>)
        ci2   = h(<ci1,eier,'1'>)
        ki3   = h(<ci2,sier,'2'>)
        ci3   = h(<ci2,sier,'1'>)
        aempt = aead(ki3,'0',hi2)
        hi3   = h(<hi2,aempt>)
        m2    = <'2',sidI,sidR,pekR,aempt,$mac1,$mac2> in
    [ State(~id,'nopsk',<'init',sidI,~ekI,ci1,ki1,hi1>)
    , !StateInvariants(~id,~ltkI,pkR,sisr)
    , In(m2)
    ]--[
      IKeys(<pkI,pkR,pekI,pekR,'nopsk',ci3>) //Not actually ci3, but derived from it.
    , ISessionID(sidI,sidR)
    ]->
    [ State(~id,'nopsk',<'transport',ci3>)
    ]

/////////////////////////////////////////////////////////////////////////////
//      PSK Mode 
// Switching mode interrupts any handshakes in progress (is this correct?)
/////////////////////////////////////////////////////////////////////////////

rule SwitchtoPSK:
    [ State(~id, 'nopsk', state)
    , !StateInvariants(~id,~ltkA,pkB,sisr)
    , !AgentPSK(~psk)
    ]--[
      AddPSK(~id,~psk)
    , InEq(state,'pskremoved') // Just so it doesn't loop forever here
    ]->
    [ State(~id,~psk,'pskadded')
    ]

rule RemovePSK:
    [ State(~id,~psk,anything)
    , !AgentPSK(~psk)
    ]--[
      RemovePSK(~id,~psk)
    ]->
    [ State(~id,'nopsk','pskremoved')
    ]

// MESSAGE RULES WITH PSK
rule Handshake_InitPSK:
    let pkI   = 'g'^~ltkI
        pekI  = 'g'^~ekI
        eisr  = pkR^~ekI
        //Construct init message m1:
        cii   = h(<'psk'>)
        cipsk = h(<cii,~psk,'1'>)
        kipsk = h(<cii,~psk,'2'>)
        hii   = h(<cipsk,'id',kipsk,pkR,pekI>)
        ci0   = h(<cipsk,pekI,'1'>)
        ki0   = h(<cipsk,pekI,'2'>)
        hi0   = h(<hii,pekI>)
        ci1   = h(<ci0,eisr,'1'>)
        ki1   = h(<ci0,eisr,'2'>)
        astat = aead(ki1,pkI,hi0)
        hi1   = h(<hi0,astat>)
        ci2   = h(<ci1,sisr,'1'>)
        ki2   = h(<ci1,sisr,'2'>)
        ats   = aead(ki2,~ts,hi1)
        hi2   = h(<hi1,ats>)
        m1    = <'1',~sidI,pekI,astat,ats,$mac1,$mac2> in //TODO:Model MACs
    [ //Init can be triggered at any time, even when currently performing
      //another role or on another stage. This could happen e.g. because of a timeout.
      //As such we bind the previous state as 'anything' and discard it.
      State(~id,~psk,anything)
    , !StateInvariants(~id,~ltkI,pkR,sisr)
    , !AgentPSK(~psk)
    , Fr(~sidI)
    , Fr(~ekI)
    , Fr(~ts)
    ]--[
      StartHandshake(pkI,pkR,pekI,~psk,~sidI)
    , UsingPSK(~psk)
    ]->
    [ //We cache DH sisr to reduce variant precomputation time for tamarin.
      State(~id,~psk,<'init',~sidI,~ekI,ci2,ki2,hi2>)
    , Out(m1)
    ]

rule Handshake_RespPSK:
    let pkR   = 'g'^~ltkR
        pekR  = 'g'^~ekR
        eisr  = pekI^~ltkR
        eier  = pekI^~ekR
        sier  = pkI^~ekR
        //Reconstruct init message m1 to validate it
        cri   = h(<'psk'>)
        crpsk = h(<cri,~psk,'1'>)
        krpsk = h(<cri,~psk,'2'>)
        hri   = h(<crpsk,'id',krpsk,pkR,pekI>)
        cr0   = h(<crpsk,pekI,'1'>)
        kr0   = h(<crpsk,pekI,'2'>)
        hr0   = h(<hri,pekI>)
        cr1   = h(<cr0,eisr,'1'>)
        kr1   = h(<cr0,eisr,'2'>)
        astat = aead(ki1,pkI,hr0)
        hr1   = h(<hr0,astat>)
        cr2   = h(<cr1,sisr,'1'>)
        kr2   = h(<cr1,sisr,'2'>)
        ats   = aead(kr2,~ts,hi2)
        hr2   = h(<hr1,ats>)
        m1    = <'1',sidI,pekI,astat,ats,$mac1,$mac2> //TODO:Model MACs
        //Constructing the response message m2:
        cr3   = h(<cr2,pekR,'1'>)
        kr3   = h(<cr2,pekR,'2'>)
        hr3   = h(<hr2,pekR>)
        cr4   = h(<cr2,eier,'1'>)
        kr4   = h(<cr2,eier,'2'>)
        cr5   = h(<cr2,sier,'1'>)
        kr5   = h(<cr2,sier,'2'>)
        aempt = aead(kr5,'0',hr3)
        hr5   = h(<hr3,aempt>)
        m2    = <'2',sidI,~sidR,pekR,aempt,$mac1,$mac2> in
    [ State(~id,~psk, anything)
    , !StateInvariants(~id,~ltkR,pkI,sisr)
    , !AgentPSK(~psk)
    , Fr(~ekR)
    , Fr(~sidR)
    , In(m1)
    ]--[
      RKeys(<pkI,pkR,pekI,pekR,~psk,cr5>) //Not actually cr, but derived from it.
    , UsingPSK(~psk)
    , RSessionID(sidI,~sidR)
    ]->
    [ State(~id,~psk,<'transport',cr3>)
    , Out(m2)
    ]

rule Handshake_CompletePSK:
    let pkI   = 'g'^~ltkI
        pekI  = 'g'^~ekI
        eier  = pekR^~ekI
        sier  = pekR^~ltkI
        //Reconstructing the response message m2 to validate it
        ci3   = h(<ci2,pekR,'1'>)
        ki3   = h(<ci2,pekR,'2'>)
        hi3   = h(<hi2,pekR>)
        ci4   = h(<ci2,eier,'1'>)
        ki4   = h(<ci2,eier,'2'>)
        ci5   = h(<ci2,sier,'1'>)
        ki5   = h(<ci2,sier,'2'>)
        aempt = aead(ki5,'0',hi3)
        hi5   = h(<hi3,aempt>)
        m2    = <'2',sidI,sidR,pekR,aempt,$mac1,$mac2> in
    [ State(~id,~psk,<'init',sidI,~ekI,ci2,ki2,hi2>)
    , !StateInvariants(~id,~ltkI,pkR,sisr)
    , !AgentPSK(~psk)
    , In(m2)
    ]--[
      IKeys(<pkI,pkR,pekI,pekR,~psk,ci5>) //Not actually ci, but derived from it.
    , UsingPSK(~psk)
    , ISessionID(sidI,sidR)
    ]->
    [ State(~id,~psk,<'transport',ci3>)
    ]

/////////////////////////////////////////////////////////////////////////////

lemma exists_session_nopsk: exists-trace
    "Ex pki pkr peki pekr ck #i #j.
        IKeys(<pki,pkr,peki,pekr,'nopsk',ck>) @ i 
        & RKeys(<pki,pkr,peki,pekr,'nopsk',ck>) @ j & #j < #i"

// The following two exists-trace lemmas take a very long time to
// autoprove, but can be proved by hand with a bit of patience.
// The heuristic is not well-suited to completing traces instead
// of ruling out attacks.
lemma exists_session_psk: exists-trace
    "Ex pki pkr peki pekr psk ck #i #j.
        IKeys(<pki,pkr,peki,pekr,psk,ck>) @ i & UsingPSK(psk) @ i
        & RKeys(<pki,pkr,peki,pekr,psk,ck>) @ j & #j < #i"

//There are only three numbers, 0, 1, and infinity. So 2 == infinity
lemma exists_two_sessions: exists-trace
    "Ex pki pkr sesskeys sesskeys2 #i #j #i2 #j2.
        IKeys(<pki,pkr,sesskeys>) @ i
        & RKeys(<pki,pkr,sesskeys>) @ j & #j < #i
        & IKeys(<pki,pkr,sesskeys2>) @ i2 & RKeys(<pki,pkr,sesskeys2>) @ j2 & #j2 < #i2
        & #i < #i2 & not(sesskeys = sesskeys2)"

lemma key_secrecy:
    "(All pki pkr peki pekr ck #i.
        IKeys(<pki,pkr,peki,pekr,ck>) @ i ==> not(Ex #j. K(ck) @ j))
    &(All pki pkr peki pekr ck #i.
        RKeys(<pki,pkr,peki,pekr,ck>) @ i ==> not(Ex #j. K(ck) @ j))"

lemma agreement[reuse]:
    "All keys #i.
        IKeys(keys) @ i ==> Ex #j. #j < #i & RKeys(keys) @ j"

//This follows immediately from agreement, since the agreed ck includes 
//at least one fresh term
lemma injective_agreement_I:
    "All pki pkr peki pekr ck #i #j.
        IKeys(<pki,pkr,peki,pekr,ck>) @ i & RKeys(<pki,pkr,peki,pekr,ck>) @ j
    ==> not(Ex peki2 pekr2 #k. RKeys(<pki,pkr,peki2,pekr2,ck>) @ k & not(#k = #j))"

end

// vim: ft=spthy