Debian Codegraph @

crt/s2n/tests/saw/spec/extras/HMAC/HMAC_specs_equiv.v

raw
1 Require Import List.
2 Import ListNotations.
3
4 Require Import Bvector.
5 Require Import Program. (* dependent destruction *)
6 Require Import Coqlib.
7
8 Require Import Utils.
9 Require Import Bitstream.
10 Require Import Eager.
11
12 Require Import HMAC_spec.
13 Require Import HMAC_lib.
14
15
16
17 Fixpoint to_bvector (w : nat) (e : ext_val) : option (Bvector w) :=
18 match e,w with
19 | eseq (ebit b :: r),S n =>
20 match to_bvector n (eseq r) with
21 | Some bv =>
22 Some (Vector.cons bool b n bv)
23 | _ => None
24 end
25 | eseq nil, O => Some (Vector.nil bool)
26 | _,_ => None
27 end.
28
29 Lemma to_bvector_succeeds :
30 forall l n,
31 has_type (eseq l) (tseq n tbit) ->
32 exists bv,
33 to_bvector n (eseq l) = Some bv.
34 Proof.
35 induction l; intros.
36
37 * inversion H. subst. simpl.
38 eauto.
39
40 * inversion H. subst. inversion H2.
41 subst.
42 edestruct IHl. econstructor; eauto.
43 inversion H3. subst.
44 simpl. rewrite H0.
45 eauto.
46 Qed.
47
48
49 Lemma bytestream_type :
50 forall l t,
51 Forall (fun x => has_type x t) l ->
52 has_type (eseq l) (tseq (length l) t).
53 Proof.
54 induction l; intros.
55 econstructor; eauto.
56 inversion H. subst.
57 econstructor; eauto.
58 Qed.
59
60 Fixpoint bv_to_extval' {w : nat} (bv : Bvector w) : list ext_val :=
61 match bv with
62 | Vector.nil => nil
63 | Vector.cons b _ r => ebit b :: bv_to_extval' r
64 end.
65
66 Definition bv_to_extval {w : nat} (bv : Bvector w) : ext_val :=
67 let bits := bv_to_extval' bv in
68 let bytes := get_each_n 8 bits in
69 eseq (map eseq bytes).
70
71 (* Bottom out with nil, maybe not a great idea but it's ok *)
72 Fixpoint eappend (l : list ext_val) : ext_val :=
73 match l with
74 | nil => eseq nil
75 | (eseq l0) :: r =>
76 match eappend r with
77 | eseq x => eseq (l0 ++ x)
78 | _ => eseq nil
79 end
80 | _ => eseq nil
81 end.
82
83 (* eappend takes a list of sequences of the same type t to one sequence of that type t *)
84 Lemma eappend_type :
85 forall l t,
86 (Forall (fun x => exists n, has_type x (tseq n t)) l) ->
87 exists n,
88 has_type (eappend l) (tseq n t).
89 Proof.
90 induction 1; intros.
91 eexists; econstructor; eauto.
92 destruct IHForall.
93 destruct H.
94 inversion H. subst.
95 simpl.
96 inversion H1. subst.
97 eexists. econstructor.
98 eapply Forall_app.
99 split; eassumption.
100 Qed.
101
102 Definition correct_model_hash {c p : nat} (hf : ext_val -> ext_val) (h : list (Bvector (b c p)) -> Bvector c) : Prop :=
103 forall l,
104 eseq (bv_to_extval' (h l)) = hf (eappend (map bv_to_extval l)).
105
106 (* We need this to be true about the hash function *)
107 (* HASH takes a list of chunks and produces some bits *)
108 (* hf takes a stream of bytes and produces a stream of bits *)
109 (* l is a list of chunks *)
110 (* (map bv_to_extval l) is a list of ext_val which are all streams of bytes *)
111 (* eappend (map bv_to_extval l) is a stream of bytes *)
112 (* this is the correct statement of hash commuting *)
113 Lemma correct_hash_commutes :
114 forall {c p : nat} hf (HASH : list (Bvector (b c p)) -> Bvector c),
115 correct_model_hash hf HASH ->
116 forall l,
117 eseq (bv_to_extval' (HASH l)) = hf (eappend (map bv_to_extval l)).
118 Proof.
119 intros. eapply H.
120 Qed.
121
122 Lemma split_append :
123 forall a (x : Bvector a) b (y : Bvector b),
124 splitVector a b (Vector.append x y) = (x,y).
125 Proof.
126 induction x; intros.
127 simpl. reflexivity.
128 simpl. rewrite IHx. reflexivity.
129 Qed.
130
131 Lemma firstn_app :
132 forall {A} (l : list A) l',
133 firstn (length l) (l ++ l') = l.
134 Proof.
135 induction l; intros; simpl; auto.
136 f_equal. eapply IHl.
137 Qed.
138
139 Lemma list_drop_app :
140 forall {A} (l : list A) l',
141 list_drop (length l) (l ++ l') = l'.
142 Proof.
143 induction l; intros; simpl; auto.
144 Qed.
145
146 Lemma list_drop_length :
147 forall {A} (l : list A) n,
148 (length (list_drop n l) <= length l)%nat.
149 Proof.
150 induction l; intros.
151 simpl. destruct n; simpl; omega.
152 simpl. destruct n; simpl. omega.
153 specialize (IHl n). omega.
154 Qed.
155
156 Lemma get_each_n'_fuel :
157 forall {A} n (l : list A) fuel tot,
158 (n >= length l)%nat ->
159 (fuel >= length l)%nat ->
160 tot <> O ->
161 get_each_n' fuel tot l = get_each_n' (length l) tot l.
162 Proof.
163 induction n; intros.
164 destruct l; simpl in *; destruct fuel; try reflexivity; omega.
165 destruct l. simpl in *.
166 destruct fuel; reflexivity.
167 assert (n >= length l)%nat by (simpl in *; omega).
168 simpl. destruct tot; try omega.
169 simpl. destruct fuel; simpl in *; try omega.
170 f_equal.
171 assert (tot = O \/ tot <> O) by omega.
172 destruct H3. subst. simpl.
173 eapply IHn; eauto; omega.
174 assert (length (list_drop tot l) <= length l)%nat by (eapply list_drop_length; eauto).
175 erewrite IHn; try omega.
176 symmetry.
177 eapply IHn; omega.
178 Qed.
179
180
181 Lemma get_each_n_head :
182 forall {A : Type} (l l' : list A) n,
183 n = length l ->
184 n <> O ->
185 get_each_n n (l ++ l') = [l] ++ get_each_n n l'.
186 Proof.
187 induction l; intros.
188 simpl in *. congruence.
189 simpl in *.
190 destruct n; inversion H.
191 assert (n = O \/ n <> O) by omega.
192 destruct H1. subst.
193 destruct l; simpl in H1; try omega.
194 simpl. unfold get_each_n. simpl. reflexivity.
195 unfold get_each_n. simpl.
196 rewrite firstn_app.
197 rewrite list_drop_app.
198 f_equal.
199 unfold get_each_n in IHl.
200 eapply get_each_n'_fuel; eauto.
201
202 rewrite app_length.
203 omega.
204 Qed.
205
206
207 Lemma zero_width_is_nil :
208 forall {A} (v : Vector.t A O),
209 v = []%vector.
210 Proof.
211 intros.
212 dependent destruction v.
213 reflexivity.
214 Qed.
215
216 Inductive same_bits' (n : Z) : list bool -> Prop :=
217 | same_bits_nil :
218 same_bits' n nil
219 | same_bits_cons :
220 forall b7 b6 b5 b4 b3 b2 b1 b0 l,
221 same_bits' n l ->
222 b7 = Z.testbit n 7 ->
223 b6 = Z.testbit n 6 ->
224 b5 = Z.testbit n 5 ->
225 b4 = Z.testbit n 4 ->
226 b3 = Z.testbit n 3 ->
227 b2 = Z.testbit n 2 ->
228 b1 = Z.testbit n 1 ->
229 b0 = Z.testbit n 0 ->
230 same_bits' n (b7 :: b6 :: b5 :: b4 :: b3 :: b2 :: b1 :: b0 :: l).
231
232
233 Definition same_bits {w : nat} (n : Z) (bv : Bvector w) : Prop := same_bits' n (Vector.to_list bv).
234
235 Lemma vector_to_list_cons :
236 forall {A : Type} f {w : nat} (r : Vector.t A w),
237 Vector.to_list (f :: r)%vector = f :: (Vector.to_list r).
238 Proof.
239 simpl; auto.
240 Qed.
241
242 Lemma vector_to_list_append :
243 forall {A : Type} {w' : nat} (f : Vector.t A w') {w : nat} (r : Vector.t A w),
244 Vector.to_list (Vector.append f r)%vector = (Vector.to_list f) ++ (Vector.to_list r).
245 Proof.
246 induction f; intros.
247 simpl. reflexivity.
248 unfold Vector.append. fold (@Vector.append A).
249 repeat rewrite vector_to_list_cons.
250 simpl. rewrite <- IHf.
251 reflexivity.
252 Qed.
253
254 Lemma same_bits_testbit :
255 forall {w} n b7 b6 b5 b4 b3 b2 b1 b0 (bv : Bvector w),
256 same_bits n (Vector.append [b7;b6;b5;b4;b3;b2;b1;b0]%vector bv) ->
257 b7 = Z.testbit n 7 /\
258 b6 = Z.testbit n 6 /\
259 b5 = Z.testbit n 5 /\
260 b4 = Z.testbit n 4 /\
261 b3 = Z.testbit n 3 /\
262 b2 = Z.testbit n 2 /\
263 b1 = Z.testbit n 1 /\
264 b0 = Z.testbit n 0 /\
265 same_bits n bv.
266 Proof.
267 intros. unfold same_bits in H.
268 rewrite vector_to_list_append in H.
269 unfold Vector.append in H.
270 simpl in H.
271 inversion H. subst.
272 repeat (split; [solve [auto] | idtac]).
273 eassumption.
274 Qed.
275
276 Lemma BVxor_cons :
277 forall n b b' v v',
278 BVxor (S n) (b :: v)%vector (b' :: v')%vector = ((xorb b b') :: (BVxor n v v'))%vector.
279 Proof.
280 simpl. auto.
281 Qed.
282
283 (* This proof is ugly, but it works *)
284 Lemma BVxor_to_xor_const :
285 forall keylen w ekey nkey opad n,
286 has_type (eseq ekey) (bytestream keylen) ->
287 bv_to_extval nkey = eseq ekey ->
288 same_bits n opad ->
289 map eseq (get_each_n 8 (bv_to_extval' (BVxor w nkey opad))) = map (xor_const n) ekey.
290 Proof.
291 induction keylen; intros.
292 * unfold bytestream in H.
293 inversion H. subst. destruct ekey; simpl in H3; try congruence.
294 simpl. destruct nkey; simpl in H0; try congruence.
295 Focus 2. unfold bv_to_extval in H0. inversion H0.
296 unfold bv_to_extval'.
297 assert (BVxor 0 []%vector opad = []%vector) by (eapply zero_width_is_nil).
298 rewrite H2. simpl. reflexivity.
299
300 * inversion H.
301 destruct ekey; simpl in H3; try congruence.
302 destruct nkey.
303 unfold bv_to_extval in H0.
304 unfold bv_to_extval' in H0. simpl in H0. inversion H0.
305 inversion H4.
306 unfold bv_to_extval in H0.
307 unfold bv_to_extval' in H0.
308 fold (@bv_to_extval' n0) in H0.
309 inversion H8. subst.
310 do 9 (destruct l1; simpl in H10; try congruence).
311 clear H10.
312 repeat match goal with
313 | [ H : Forall (fun x => has_type x tbit) _ |- _ ] => inversion H; clear H
314 end;
315 repeat match goal with
316 | [ H : has_type _ tbit |- _ ] => inversion H; clear H
317 end;
318 subst.
319
320 do 7 (destruct nkey; [ subst;
321 unfold bv_to_extval in H0;
322 unfold bv_to_extval' in H0; simpl in H0; inversion H0;
323 inversion H4 |
324 unfold bv_to_extval in H0;
325 unfold bv_to_extval' in H0;
326 fold (@bv_to_extval' n0) in H0 ]).
327 remember (bv_to_extval' nkey) as extval_key.
328
329 replace ((ebit h :: ebit h0 :: ebit h1 :: ebit h2 :: ebit h3 :: ebit h4 :: ebit h5 :: ebit h6 :: extval_key)) with
330 ((ebit h :: ebit h0 :: ebit h1 :: ebit h2 :: ebit h3 :: ebit h4 :: ebit h5 :: ebit h6 :: nil) ++ extval_key) in H0 by reflexivity.
331
332 erewrite get_each_n_head in H0 by (simpl; eauto).
333 simpl in H0.
334 inversion H0. subst.
335 clear H0.
336 inversion H3.
337 unfold length.
338
339 eapply is_seq in H9.
340 replace (tseq (length (map eseq (get_each_n 8 (bv_to_extval' nkey)))) byte) with
341 (bytestream (length (map eseq (get_each_n 8 (bv_to_extval' nkey))))) in H9 by reflexivity.
342
343 rewrite H2 in H9.
344 do 8 (dependent destruction opad).
345 replace (h :: h0 :: h1 :: h2 :: h3 :: h4 :: h5 :: h6 :: opad)%vector with
346 (Vector.append (h :: h0 :: h1 :: h2 :: h3 :: h4 :: h5 :: h6 :: [])%vector opad) in H1 by (simpl; auto).
347 eapply same_bits_testbit in H1.
348 repeat match goal with
349 | [ H : _ /\ _ |- _ ] => destruct H
350 end.
351
352 eapply IHkeylen in H9;
353 [ idtac |
354 unfold bv_to_extval; reflexivity
355 | eassumption ].
356
357 repeat rewrite BVxor_cons.
358 unfold bv_to_extval'.
359 fold (@bv_to_extval' n0).
360 replace (ebit (xorb b6 h)
361 :: ebit (xorb b5 h0)
362 :: ebit (xorb b4 h1)
363 :: ebit (xorb b3 h2)
364 :: ebit (xorb b2 h3)
365 :: ebit (xorb b1 h4) :: ebit (xorb b0 h5) :: ebit (xorb b h6) :: bv_to_extval' (BVxor n0 nkey opad)) with
366 ((ebit (xorb b6 h)
367 :: ebit (xorb b5 h0)
368 :: ebit (xorb b4 h1)
369 :: ebit (xorb b3 h2)
370 :: ebit (xorb b2 h3)
371 :: ebit (xorb b1 h4) :: ebit (xorb b0 h5) :: ebit (xorb b h6) :: nil) ++ bv_to_extval' (BVxor n0 nkey opad)) by (simpl; auto).
372
373 erewrite get_each_n_head by (simpl; auto).
374 unfold app.
375 repeat rewrite map_cons.
376 erewrite <- H9.
377 f_equal.
378
379 unfold xor_const.
380 unfold xor_const_list.
381 unfold length.
382 f_equal.
383 subst. reflexivity.
384 Qed.
385
386
387 Lemma eappend_eseq_append :
388 forall l1 l2,
389 eseq (l1 ++ l2) = eappend ((eseq l1)::(eseq l2)::nil).
390 Proof.
391 intros. simpl. rewrite app_nil_r. reflexivity.
392 Qed.
393
394 Lemma eseq_eq :
395 forall x y a b,
396 x = y ->
397 eseq a = eseq b ->
398 eseq (x ++ a) = eseq (y ++ b).
399 Proof.
400 intros. inversion H0. congruence.
401 Qed.
402
403
404 Lemma bv_to_extval'_append :
405 forall {w w'} (x : Bvector w) (y : Bvector w'),
406 bv_to_extval' (Vector.append x y) = (bv_to_extval' x) ++ (bv_to_extval' y).
407 Proof.
408 induction x; intros.
409 simpl; auto.
410 simpl. f_equal. auto.
411 Qed.
412
413 Lemma eseq_backwards :
414 forall x y,
415 eseq x = eseq y ->
416 x = y.
417 Proof.
418 intros. inversion H. auto.
419 Qed.
420
421 Lemma eappend_map_eseq :
422 forall {A} f (l : list A),
423 eappend (map (fun x => eseq (f x)) l) = eseq (concat (map f l)).
424 Proof.
425 induction l; intros.
426 simpl. reflexivity.
427 simpl. rewrite IHl. reflexivity.
428 Qed.
429
430 Theorem HMAC_equiv (MSGT : Set) :
431 forall keylen msglen key msg,
432 has_type key (bytestream keylen) ->
433 has_type msg (bytestream msglen) ->
434 forall c p hf res HashBlock iv (splitAndPad : MSGT -> list (Bvector (b c p))),
435 hmac_model hf key msg = Some res ->
436 @correct_model_hash c p hf (h_star p HashBlock iv) ->
437 forall nmsg nkey,
438 eappend (map bv_to_extval (splitAndPad nmsg)) = msg ->
439 bv_to_extval nkey = key ->
440 forall opad ipad,
441 same_bits 92 opad ->
442 same_bits 54 ipad ->
443 forall fpad,
444 (forall x, bv_to_extval' (app_fpad fpad (h_star p HashBlock iv x)) = bv_to_extval' (h_star p HashBlock iv x)) ->
445 eseq (bv_to_extval' (@HMAC c p HashBlock iv MSGT splitAndPad fpad opad ipad nkey nmsg)) = res.
446 Proof.
447 intros.
448 unfold HMAC.
449 unfold HMAC_2K.
450 unfold GHMAC_2K.
451 unfold hash_words.
452 rewrite split_append.
453 simpl.
454 unfold hmac_model in H1.
455 destruct key; simpl in *; try congruence.
456 destruct msg; simpl in *; try congruence.
457 destruct (hf (eseq (map (fun x : ext_val => xor_const 54 x) l ++ l0))) eqn:?; try congruence.
458 inversion H1.
459
460 match goal with
461 | [ |- eseq (bv_to_extval' (HashBlock (HashBlock iv ?V1) ?V2)) = _ ] =>
462 replace (HashBlock (HashBlock iv V1) V2) with (h_star p HashBlock iv (V1 :: V2 :: nil)) by (unfold h_star; simpl; reflexivity)
463 end.
464 match goal with
465 | [ |- context[(h_star p HashBlock (HashBlock iv ?V1) ?V2)] ] =>
466 replace (h_star p HashBlock (HashBlock iv V1) V2) with (h_star p HashBlock iv (V1 :: V2)) by (simpl; reflexivity)
467 end.
468 remember (h_star p HashBlock iv) as HASH.
469
470 rename l into ekey.
471 rename l0 into emsg.
472
473 erewrite correct_hash_commutes by eassumption.
474
475 f_equal. simpl.
476 f_equal.
477 f_equal.
478 eapply BVxor_to_xor_const; eauto.
479
480 rewrite app_nil_r.
481 unfold Pos.to_nat in *.
482 simpl in *.
483
484 (* l1 is a list of bits *)
485 (* bv_to_extval' returns a list of bits *)
486 (* at this point the types work out *)
487 f_equal. f_equal.
488
489 remember (splitAndPad nmsg) as msgl.
490
491 rewrite H7.
492
493 eapply eseq_backwards.
494 erewrite correct_hash_commutes by eassumption.
495 rewrite <- Heqe.
496 f_equal.
497
498 (* types still good here *)
499 simpl.
500 unfold bv_to_extval.
501 erewrite eappend_map_eseq.
502 f_equal.
503 f_equal.
504
505 eapply BVxor_to_xor_const; eauto.
506
507
508 eapply eseq_backwards.
509 erewrite <- H3.
510 unfold bv_to_extval.
511 erewrite <- eappend_map_eseq.
512
513 reflexivity.
514 Qed.
515