-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExamples.v
More file actions
263 lines (216 loc) · 6.92 KB
/
Examples.v
File metadata and controls
263 lines (216 loc) · 6.92 KB
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
Require Import Crypto.
Require Import ProtoRep.
Require Import ProtoStateSemantics.
Require Import CpdtTactics.
Definition isBad {t:type} (m:message t) : Prop :=
match m with
| bad _ => True
| _ => False
end.
(*Inductive isBad' {t:type} : (message t) -> Prop :=
| aa : isBad' (bad t). *)
Lemma is_dec_info{t:type} : forall (m:message t) k k', (is_decryptable (encrypt _ m k) k') -> k = inverse k'.
Proof.
intros.
inversion H. assert ((inverse (inverse k)) = k). apply inverse_inverse. symmetry in H1. assumption. Qed.
Lemma notBadContra {t:type} : forall (m:message t) k k', (~isBad m) -> decryptM (encrypt _ m k) k' = m -> is_not_decryptable (encrypt _ m k) k' -> False.
Proof.
intros. unfold decryptM in H0. destruct (decrypt (encrypt _ m k) k').
inversion p. inversion H2. simpl in H1. contradiction.
unfold not in H. apply H. subst. constructor.
Qed.
Lemma decryptSuccess{t:type} : forall (m : message t) k k', (~ isBad m) -> decryptM (encrypt _ m k) k' = m -> k = inverse k'.
Proof.
intros. destruct m eqn:hh;
try (destruct (decrypt (encrypt _ m k) k'); [inversion p; apply is_dec_info in H1; assumption | exfalso; rewrite <- hh in H0; rewrite <- hh in H; apply (notBadContra m k k' H H0 i)]).
Qed.
(* Simple increment example *)
Definition incPayload (m:message Basic) : (message Basic) :=
match m with
| basic n => basic (n + 1)
| _ => bad Basic
end.
Definition proto1A (m:(message Basic)) :=
send m;
x <- receive;
ReturnC (t:=Basic) x.
Check proto1A.
Definition proto1B :=
x <- receive;
send (incPayload x);
ReturnC x.
Check proto1B.
Theorem dual1AB : forall (x:message Basic), Dual (proto1A x) proto1B.
Proof.
cbv.
split.
reflexivity.
split.
reflexivity.
trivial.
Qed.
Theorem incPropertyAB : forall x x' st st',
multi st _ _ _ (proto1A x) proto1B (ReturnC x') st' ->
x' = incPayload x.
Proof.
intros x x' st st' multiProof.
dep_destruct multiProof. clear multiProof.
dep_destruct s0. dep_destruct s1. clear s0. clear s1.
dep_destruct x2. clear x2.
dep_destruct s0. dep_destruct s1. clear s0. clear s1.
dep_destruct x3. reflexivity.
inversion s1.
Qed.
(* Needham Schroeder *)
Definition aNonceSecret := 11.
Definition bNonceSecret := 22.
Definition aNonce := (basic aNonceSecret).
Definition bNonce := (basic bNonceSecret).
Definition aPub := (public 1).
Definition bPub := (public 2).
Definition aPubBad := (public 3).
Definition bPubBad := (public 4).
Definition aPri := (private 1).
Definition bPri := (private 2).
Definition aPriBad := (private 5).
Definition bPriBad := (private 6).
Definition Needham_A (myPri theirPub:keyType) :=
send (encrypt _ aNonce theirPub);
x <- receive; (* x : Encrypt (Pair Basic Basic) *)
let y : (message (Pair Basic Basic)):= decryptM x myPri in (* y : Pair Basic Basic *)
let y' := (pairFst y) in
let y'' := (pairSnd y) in
send (encrypt _ y'' theirPub);
ReturnC (pair _ _ y' y''). Check Needham_A.
Definition Needham_B (myPri theirPub:keyType) :=
ReceiveC (fun x: message (Encrypt Basic) => (
(*x <- receive; (* x : Encrypt Basic *) *)
let y : (message Basic) := decryptM x myPri in (* y : Basic *)
send (encrypt (Pair Basic Basic) (pair _ _ y bNonce) theirPub);
ReceiveC (fun z : message (Encrypt Basic) => (
(*z <- receive; (* z : Encrypt Basic *) *)
let z' := decryptM z myPri in (* z' : Basic *)
(*ReturnC y)))). Check Needham_B. *)
ReturnC (pair _ _ y z'))))). Check Needham_B.
Example dualNeedham : forall ka ka' kb kb',
Dual (Needham_A ka kb') (Needham_B kb ka').
Proof.
intros.
split. reflexivity.
split. reflexivity.
split. reflexivity.
reflexivity.
Qed.
Theorem needham_A_auth : forall (k:keyType) (x:message Basic) st st',
multi
st
_ _ _
(Needham_A k bPub)
(Needham_B bPri aPub)
(ReturnC (pair _ _ x bNonce))
st'
-> (k = inverse aPub).
Proof.
intros k x st st' multiProof.
dep_destruct multiProof. clear multiProof.
dep_destruct s0. dep_destruct s1. clear s0. clear s1.
dep_destruct x2. clear x2.
dep_destruct s0. dep_destruct s1. clear s0. clear s1.
dep_destruct x3. clear x3.
dep_destruct s1. dep_destruct s4. clear s1. clear s4.
dep_destruct x2. clear x2. clear x. cbn in x0.
unfold decryptM in x0.
destruct (decrypt
(encrypt _ (pair _ _ aNonce (basic bNonceSecret)) aPub)
k) as [p | _].
destruct p as (m , i).
dep_destruct i. reflexivity. simpl in x0.
inversion x0.
inversion s4.
Qed.
Definition normalizeState : State -> State := fun _ => emptyState.
(* Future Work *)
Definition hoare_triple{p1t p2t p3t:protoType}{t:type}
(P:Assertion)
(p1:protoExp p1t) (p2:protoExp p2t) (p3:protoExp p3t)
(Q:Assertion) : Prop :=
forall st st',
P st ->
multi st _ _ _ p1 p2 p3 st' ->
Q (normalizeState st').
Definition notInPb : (message Basic) -> State -> Prop := fun _ _ => True.
Definition inPb : (message Basic) -> State -> Prop := fun _ _ => True.
Definition learnedB {p1t p2t p3t:protoType}
(m:message Basic)
(p1:protoExp p1t) (p2:protoExp p2t) (p3:protoExp p3t) : Prop :=
hoare_triple (t:=Basic) (notInPb m) p1 p2 p3 (inPb m).
Definition p1 :=
send (basic 33);
EpsC.
Definition p2 :=
x <- receive;
ReturnC (t:=Basic) x.
Definition learnedProof : learnedB (basic 33) p2 p1 (ReturnC (basic 33)).
Proof.
intros.
unfold p1. unfold p2. unfold learnedB. unfold hoare_triple.
intros.
dep_destruct H0. clear H0.
dep_destruct s0. dep_destruct s1. clear s0. clear s1.
dep_destruct x1. clear x1. simpl. cbn. trivial.
Abort.
(* Simplest possible encrypt/decrypt protocol pair *)
Definition protoEncrypt1 (theirPub:keyType) :=
send (encrypt _ (basic 42) theirPub);
EpsC.
Definition protoDecrypt1 (myPri:keyType) :=
x <- receive;
ReturnC (t:=Basic) (decryptM x myPri).
Example result1 :
multi
emptyState
_ _ _
(protoDecrypt1 (private 1))
(protoEncrypt1 (public 1))
(ReturnC (basic 42))
(updateState (encrypt Basic (basic 42) (public 1)) emptyState).
Proof.
eapply multi_step with (st2:=emptyState).
constructor.
constructor.
apply multi_refl.
Qed.
Example auth1 : forall k k',
multi
emptyState
_ _ _
(protoDecrypt1 k')
(protoEncrypt1 k)
(ReturnC (basic 42))
(updateState (encrypt Basic (basic 42) (public 1)) emptyState)
-> (k = inverse k').
Proof.
intros.
dep_destruct H. dep_destruct s0. dep_destruct x1.
apply decryptSuccess with (m:= (basic 42)).
unfold not. intros. inversion H0.
assumption.
dep_destruct s2.
Qed.
Example auth1' : forall k k' st',
multi
emptyState
_ _ _
(protoDecrypt1 k')
(protoEncrypt1 k)
(ReturnC (basic 42))
st'
-> (k = inverse k').
Proof.
intros.
dep_destruct H. clear H. dep_destruct s0. clear s0. dep_destruct x1.
apply decryptSuccess with (m:= (basic 42)).
unfold not. intros. inversion H.
assumption.
inversion s2.
Qed.