| 1 |
Require Import List. |
| 2 |
|
| 3 |
Parameter string : Type. |
| 4 |
|
| 5 |
Parameter empty_string : string. |
| 6 |
|
| 7 |
Parameter concat : string -> string -> string. |
| 8 |
|
| 9 |
(*3 axioms about concatenation that should hold for any reasonable implementation *) |
| 10 |
Axiom concat_assoc : forall a b c, concat a (concat b c) = concat (concat a b) c. |
| 11 |
Axiom concat_empty : forall s : string, concat s empty_string = s. |
| 12 |
Axiom concat_empty_l : forall s : string, concat empty_string s = s. |
| 13 |
|
| 14 |
|
| 15 |
Parameter state : Type. |
| 16 |
|
| 17 |
Parameter HMAC : string -> string -> string. |
| 18 |
|
| 19 |
Parameter HMAC_init : string -> state. |
| 20 |
|
| 21 |
Parameter HMAC_update : string -> state -> state. |
| 22 |
|
| 23 |
Parameter HMAC_digest : state -> string. |
| 24 |
|
| 25 |
(* We have proved this as stated here in SAW *) |
| 26 |
Axiom update_concat : forall m1 m2 s, |
| 27 |
HMAC_update (concat m1 m2) s = HMAC_update m2 (HMAC_update m1 s). |
| 28 |
|
| 29 |
(* We have proved this for a set of m and k sizes in SAW *) |
| 30 |
Axiom equiv_one : forall m k, |
| 31 |
HMAC_digest (HMAC_update m (HMAC_init k)) = HMAC k m. |
| 32 |
|
| 33 |
(* Not yet proved in SAW, can be proved as stated *) |
| 34 |
Axiom update_empty : forall s, HMAC_update empty_string s = s. |
| 35 |
|
| 36 |
Lemma fold_right_concat : forall s l, |
| 37 |
fold_right concat (s) l = concat (fold_right concat empty_string l) s. |
| 38 |
Proof. |
| 39 |
induction l. |
| 40 |
simpl in *. rewrite concat_empty_l. auto. |
| 41 |
simpl in *. rewrite IHl. rewrite concat_assoc. auto. |
| 42 |
Qed. |
| 43 |
|
| 44 |
Lemma update_concat_any: |
| 45 |
forall (l : list string) (key s : string), |
| 46 |
HMAC_update (concat (fold_right concat empty_string (rev l)) s) (HMAC_init key) = |
| 47 |
HMAC_update s (fold_right HMAC_update (HMAC_init key) l). |
| 48 |
Proof. |
| 49 |
induction l; intros. |
| 50 |
+ simpl in *. rewrite concat_empty_l. auto. |
| 51 |
+ simpl in *. rewrite <- update_concat. rewrite fold_right_app. |
| 52 |
simpl. rewrite fold_right_concat. simpl. rewrite concat_empty. rewrite <- IHl. f_equal. |
| 53 |
rewrite concat_assoc. auto. |
| 54 |
Qed. |
| 55 |
|
| 56 |
Theorem HMAC_incremental_equiv : |
| 57 |
forall (ms : list string) key, |
| 58 |
HMAC key (fold_right concat empty_string ms) = |
| 59 |
HMAC_digest (fold_left (fun (st: state) msg => HMAC_update msg st) ms (HMAC_init key)). |
| 60 |
Proof. |
| 61 |
intros. |
| 62 |
rewrite <- fold_left_rev_right in *. |
| 63 |
remember (rev ms). |
| 64 |
destruct l; simpl. |
| 65 |
- rewrite <- equiv_one. destruct ms. |
| 66 |
+ simpl. rewrite update_empty. auto. |
| 67 |
+ assert (rev nil = rev (rev (s :: ms))). f_equal. auto. |
| 68 |
clear Heql. rewrite rev_involutive in *. simpl in *. congruence. |
| 69 |
- rewrite <- equiv_one. rewrite <- update_concat_any. |
| 70 |
f_equal. assert (rev (s :: l) = rev (rev ms)). f_equal. auto. |
| 71 |
rewrite rev_involutive in *. subst. clear Heql. simpl. |
| 72 |
rewrite fold_right_app. simpl. rewrite concat_empty. rewrite fold_right_concat. auto. |
| 73 |
Qed. |