summaryrefslogtreecommitdiffstats
path: root/wireguard.m4
blob: 6d02da5dfd67897786dacb60f0b413c11495f8c1 (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
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
functions: aead/4, decrypt/2
equations: decrypt(aead(k,n,p,a),k) = p     //Plaintext can be recovered using the key

//Uncomment these to prevent any adversary message modifications
dnl define(In, AuthenticatedMessage($*))
dnl define(Out, AuthenticatedMessage($*))

dnl define(State,F_State($*))

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 id1 id2 pka pkb #i #j.
        Paired(id1,pka,pkb) @ i & Paired(id2,pka,pkb) @ j
    ==> #i = #j"

rule KeyGen:
    [ Fr(~ltk) ]
    --[Key(~ltk)]->
    [!AgentKey(~ltk), Out('g'^~ltk)]

rule ExchangePublicKeys:
    [ !AgentKey(~ltkA)
    , !AgentKey(~ltkB)
    , Fr(~inj)
    ]--[
      Paired(~inj, 'g'^~ltkA, 'g'^~ltkB)
    , Paired(~inj, 'g'^~ltkB, 'g'^~ltkA)
    , InEq($A,$B)
    ]->
    [ State(~inj, ~ltkA, 'g'^~ltkB, ('g'^~ltkA)^~ltkB, '0')
    , State(~inj, ~ltkB, 'g'^~ltkA, ('g'^~ltkA)^~ltkB, '0')
    ]

/* Uncomment to allow long term key compromise
 rule CompromiseKey:
      [!AgentKey($Agent,ltk)]
      --[ Compromise($Agent,ltk) ]->
      [Out(ltk)]
*/

// MESSAGE RULES
rule Handshake_InitNoPSK:
    let cii = h('nopsk')
        hi0 = h(<cii,'id',pkR,'g'^~ekI>)  //No PSK means no intermediate hashes necessary here
        ki0 = h(<cii,pkR^~ekI,'2'>)
        ci0 = h(<cii,pkR^~ekI,'1'>)
        altk= aead(ki0,'0','g'^~ltkI,hi0)
        hi1 = h(<hi0,altk>)
        ki1 = h(<ci0,sisr,'2'>)
        ci1 = h(<ci0,sisr,'1'>)
        ats = aead(ki1,'0',~ts,hi1)
        hi2 = h(<hi1,ats>)
        m1  = <'1',~sidI,'g'^~ekI,altk,ats,$mac1,$mac2> in //TODO:Model MACs
    [ State(inj,~ltkI,pkR,sisr,anything)
    , Fr(~sidI)
    , Fr(~ekI)
    , Fr(~ts)
    ]--[
      Binding(inj,'g'^~ltkI,pkR)
    , HandshakeInit('g'^~ltkI, pkR, hi2)
    ]->
    [ State(inj,~ltkI,pkR,sisr,<'init',~sidI,~ekI,ci1,ki1,hi2>)
    , Out(m1)
    ]

rule Handshake_RespNoPSK:
    let cri = h('nopsk')
        eisr = pekI^~ltkR
        eier = pekI^~ekR
        sier = pkI^~ekR
        hr0 = h(<cri,'id','g'^~ltkR,pekI>)  //No PSK means no intermediate hashes necessary here
        kr0 = h(<cri,eisr,'2'>)
        cr0 = h(<cri,eisr,'1'>)
        altk= aead(kr0,'0',pkI,hr0)
        hr1 = h(<hr0,altk>)
        kr1 = h(<cr0,sisr,'2'>)
        cr1 = h(<cr0,sisr,'1'>)
        ats = aead(kr1,'0',ts,hr1)
        hr2 = h(<hr1,ats>)
        m1  = <'1',sidI,pekI,altk,ats,$mac1,$mac2> //TODO:Model MACs
        hr3 = h(<hr2,'g'^~ekR>)
        kr2 = h(<cr1,eier,'2'>)
        cr2 = h(<cr1,eier,'1'>)
        kr3 = h(<cr2,sier,'2'>)
        cr3 = h(<cr2,sier,'1'>)
        aemp= aead(kr3,'0','0',hr3)
        hr4 = h(<hr3,aemp>)
        m2  = <'2',sidI,~sidR,'g'^~ekR,aemp,$mac1,$mac2> in
    [ State(inj,~ltkR,pkI,sisr,anything)
    , Fr(~ekR)
    , Fr(~sidR)
    , In(m1)
    ]--[
      Binding(inj,pkI,'g'^~ltkR)
    , HandshakeResp(pkI,'g'^~ltkR,hr2,hr4)
    , RespTransportKeys(pkI,'g'^~ltkR,cr3) //Not actually cr3, but derived from it.
    ]->
    [ State(inj,~ltkR,pkI,sisr,<'resp',cr3>)
    , Out(m2)
    ]

rule Handshake_CompleteNoPSK:
    let eier = pekR^~ekI
        sier = pekR^~ltkI
        hi3 = h(<hi2,pekR>)
        ki2 = h(<ci1,eier,'2'>)
        ci2 = h(<ci1,eier,'1'>)
        ki3 = h(<ci2,sier,'2'>)
        ci3 = h(<ci2,sier,'1'>)
        aemp= aead(ki3,'0','0',hi3)
        hi4 = h(<hi3,aemp>)
        m2  = <'2',sidI,sidR,pekR,aemp,$mac1,$mac2> in
    [ State(inj,~ltkI,pkR,sisr,<'init',sidI,~ekI,ci1,ki1,hi2>)
    , In(m2)
    ]--[
      Binding(inj,'g'^~ltkI,pkR)
    , InitTransportKeys('g'^~ltkI,pkR,ci3) //Not actually ci3, but derived from it.
    ]->
    [ State(inj,~ltkI,pkR,sisr,<'init',ci3>)
    ]

/*/////////////////////////////////////////////////////////////////////////////
    TRACE EXISTENCE
    Status: All autoprove (~15 minutes, almost entirely on exists_detect)
*/

lemma exists_session: exists-trace
    "Ex a b ck #i #j.
        InitTransportKeys(a,b,ck) @ i & RespTransportKeys(a,b,ck) @ j & #j < #i"

lemma state_invariant[reuse,use_induction]:
    "All inj pka pkb #i.
        Binding(inj,pka,pkb) @ i ==> Ex #j. Paired(inj,pka,pkb) @ j & #j < #i"

lemma key_secrecy:
    "(All a b ck #i.
        InitTransportKeys(a,b,ck) @ i ==> not(Ex #j. K(ck) @ j))
    &(All a b ck #i.
        RespTransportKeys(a,b,ck) @ i ==> not(Ex #j. K(ck) @ j))"

lemma injective_agreement:
    "All a b ck #i #j.
        InitTransportKeys(a,b,ck) @ i & RespTransportKeys(a,b,ck) @ j
    ==> #j < #i & not(Ex #k. RespTransportKeys(a,b,ck) @ k & not(#k = #j))"
end
// vim: ft=spthy