| 1 |
Require Import List. |
| 2 |
Import ListNotations. |
| 3 |
Require Import String. |
| 4 |
|
| 5 |
(* Borrow from CompCert *) |
| 6 |
Require Import Coqlib. |
| 7 |
Require Import Bitvectors. |
| 8 |
|
| 9 |
Require Import AST. |
| 10 |
Require Import Semantics. |
| 11 |
Require Import Utils. |
| 12 |
Require Import Builtins. |
| 13 |
Require Import BuiltinSem. |
| 14 |
Require Import BuiltinSyntax. |
| 15 |
Require Import Values. |
| 16 |
Require Import Eager. |
| 17 |
Require Import Bitstream. |
| 18 |
|
| 19 |
Require Import EvalTac. |
| 20 |
|
| 21 |
Import HaskellListNotations. |
| 22 |
Open Scope string. |
| 23 |
|
| 24 |
Require Import HMAC. |
| 25 |
|
| 26 |
Require Import HMAC_spec. |
| 27 |
|
| 28 |
Require Import HMAC_lib. |
| 29 |
|
| 30 |
Require Import Kinit_eval. |
| 31 |
|
| 32 |
|
| 33 |
(* lemma for when the length of the key is the same as the length of the block *) |
| 34 |
Lemma Hmac_eval_keylen_is_blocklength : |
| 35 |
forall (key : ext_val) keylen, |
| 36 |
has_type key (bytestream keylen) -> |
| 37 |
forall GE TE SE, |
| 38 |
wf_env ge GE TE SE -> |
| 39 |
(forall id, In id [(371, "ks");(372, "okey");(373, "ikey");(374, "internal")] -> GE id = None) -> |
| 40 |
forall h hf, |
| 41 |
good_hash h GE TE SE hf -> |
| 42 |
forall msg msglen unused, |
| 43 |
has_type msg (bytestream msglen) -> |
| 44 |
exists v, |
| 45 |
eager_eval_expr GE TE SE (apply (tapply (EVar hmac) ((typenum (Z.of_nat msglen)) :: (typenum (Z.of_nat keylen)) :: (typenum unused) :: (typenum (Z.of_nat keylen)) :: nil)) (h :: h :: h :: (EValue (to_val key)) :: (EValue (to_val msg)) :: nil)) (to_sval v) /\ hmac_model hf key msg = Some v. |
| 46 |
Proof. |
| 47 |
intros. |
| 48 |
rename H1 into HIDs. |
| 49 |
rename H2 into H1. |
| 50 |
rename H3 into H2. |
| 51 |
init_globals ge. |
| 52 |
abstract_globals ge. |
| 53 |
edestruct good_hash_complete_eval; eauto. |
| 54 |
do 4 destruct H3. |
| 55 |
|
| 56 |
inversion H. subst. |
| 57 |
inversion H2. subst. |
| 58 |
remember (hf (eseq (map (fun x3 : ext_val => xor_const 54 x3) l ++ l0))) as hv1. |
| 59 |
assert (HT : exists n, has_type hv1 (tseq n tbit)). { |
| 60 |
assert (exists n, has_type (eseq (map (fun x3 : ext_val => xor_const 54 x3) l ++ l0)) (bytestream n)). { |
| 61 |
eexists. econstructor. |
| 62 |
rewrite Forall_app. split. |
| 63 |
eapply Forall_map. eauto. |
| 64 |
intros. eapply xor_const_byte; eauto. |
| 65 |
eauto. |
| 66 |
} |
| 67 |
destruct H5. |
| 68 |
eapply H4 in H5. destruct H5. subst hv1. eauto. |
| 69 |
} |
| 70 |
destruct HT as [n0']. |
| 71 |
rename H5 into HT. |
| 72 |
edestruct ext_val_list_of_strictval; try eassumption. |
| 73 |
rename H5 into Hlres. |
| 74 |
|
| 75 |
|
| 76 |
eexists; split. |
| 77 |
|
| 78 |
e. e. e. e. e. e. e. e. e. |
| 79 |
ag. |
| 80 |
|
| 81 |
e. e. e. e. e. |
| 82 |
eassumption. |
| 83 |
e. eassumption. |
| 84 |
e. eassumption. |
| 85 |
e. e. |
| 86 |
|
| 87 |
eapply strict_eval_val_to_val. |
| 88 |
|
| 89 |
e. e. |
| 90 |
eapply strict_eval_val_to_val. |
| 91 |
|
| 92 |
e. e. e. e. e. e. e. e. |
| 93 |
g. simpl. unfold extend. simpl. |
| 94 |
eapply wf_env_not_local; eauto. |
| 95 |
reflexivity. |
| 96 |
|
| 97 |
e. |
| 98 |
e. e. e. |
| 99 |
eapply eager_eval_global_var. |
| 100 |
reflexivity. |
| 101 |
reflexivity. |
| 102 |
e. e. e. eapply eager_eval_global_var. |
| 103 |
reflexivity. |
| 104 |
reflexivity. |
| 105 |
|
| 106 |
|
| 107 |
eapply kinit_eval. |
| 108 |
|
| 109 |
unfold bind_decl_groups. |
| 110 |
unfold erase_decl_groups. |
| 111 |
|
| 112 |
repeat eapply wf_env_extend_TE. |
| 113 |
repeat eapply wf_env_erase_SE. |
| 114 |
repeat eapply wf_env_extend_SE. |
| 115 |
repeat eapply wf_env_extend_GE. |
| 116 |
eassumption. |
| 117 |
|
| 118 |
reflexivity. |
| 119 |
reflexivity. |
| 120 |
reflexivity. |
| 121 |
reflexivity. |
| 122 |
reflexivity. |
| 123 |
reflexivity. |
| 124 |
reflexivity. |
| 125 |
reflexivity. |
| 126 |
reflexivity. |
| 127 |
reflexivity. |
| 128 |
reflexivity. |
| 129 |
reflexivity. |
| 130 |
reflexivity. |
| 131 |
|
| 132 |
exact H. |
| 133 |
|
| 134 |
|
| 135 |
eapply good_hash_same_eval; eauto. |
| 136 |
e. |
| 137 |
|
| 138 |
repeat e. repeat e. |
| 139 |
repeat e. e. |
| 140 |
|
| 141 |
simpl. |
| 142 |
rewrite list_of_strictval_of_strictlist. |
| 143 |
reflexivity. |
| 144 |
|
| 145 |
(* Begin model section *) |
| 146 |
eapply eager_eval_bind_senvs. eassumption. |
| 147 |
instantiate (1 := fun x => to_sval (xor_const 92 x)). |
| 148 |
intros. e. e. e. g. unfold extend. simpl. |
| 149 |
eapply wf_env_not_local; eauto. reflexivity. |
| 150 |
e. e. e. e. e. e. g. |
| 151 |
unfold extend. simpl. |
| 152 |
eapply wf_env_not_local; eauto. reflexivity. |
| 153 |
e. repeat e. repeat e. e. repeat e. |
| 154 |
repeat e. simpl. |
| 155 |
inversion H5. subst. simpl. |
| 156 |
unfold strictnum. |
| 157 |
unfold Z.to_nat. unfold Pos.to_nat. |
| 158 |
unfold Pos.iter_op. unfold Init.Nat.add. |
| 159 |
rewrite <- H6. |
| 160 |
rewrite xor_num. reflexivity. |
| 161 |
rewrite H6. eassumption. |
| 162 |
reflexivity. |
| 163 |
(* End model section *) |
| 164 |
|
| 165 |
e. g. |
| 166 |
e. e. e. e. g. |
| 167 |
simpl. unfold extend. simpl. eapply wf_env_not_local; eauto. |
| 168 |
reflexivity. |
| 169 |
e. e. e. e. e. e. e. e. e. e. e. g. |
| 170 |
simpl. unfold extend. simpl. eapply wf_env_not_local; eauto. |
| 171 |
reflexivity. |
| 172 |
e. e. e. e. g. |
| 173 |
e. e. e. g. |
| 174 |
eapply kinit_eval. |
| 175 |
|
| 176 |
unfold bind_decl_groups. |
| 177 |
unfold erase_decl_groups. |
| 178 |
repeat eapply wf_env_extend_GE. |
| 179 |
repeat eapply wf_env_extend_TE. |
| 180 |
repeat eapply wf_env_erase_SE. |
| 181 |
repeat eapply wf_env_extend_SE. |
| 182 |
assumption. |
| 183 |
|
| 184 |
all: try solve [reflexivity]. |
| 185 |
|
| 186 |
exact H. |
| 187 |
|
| 188 |
solve [eapply good_hash_same_eval; eauto; e]. |
| 189 |
|
| 190 |
repeat e. |
| 191 |
repeat e. repeat e. e. |
| 192 |
|
| 193 |
simpl. |
| 194 |
rewrite list_of_strictval_of_strictlist. |
| 195 |
reflexivity. |
| 196 |
|
| 197 |
|
| 198 |
eapply eager_eval_bind_senvs. eassumption. |
| 199 |
instantiate (1 := fun x => to_sval (xor_const 54 x)). |
| 200 |
intros. e. e. e. g. unfold extend. simpl. |
| 201 |
eapply wf_env_not_local; eauto. reflexivity. |
| 202 |
e. e. e. e. e. e. g. |
| 203 |
unfold extend. simpl. |
| 204 |
eapply wf_env_not_local; eauto. reflexivity. |
| 205 |
e. repeat e. repeat e. e. repeat e. |
| 206 |
repeat e. simpl. |
| 207 |
inversion H5. subst. simpl. |
| 208 |
unfold strictnum. |
| 209 |
unfold Z.to_nat. unfold Pos.to_nat. |
| 210 |
unfold Pos.iter_op. unfold Init.Nat.add. |
| 211 |
rewrite <- H6. |
| 212 |
rewrite xor_num. reflexivity. |
| 213 |
rewrite H6. eassumption. |
| 214 |
reflexivity. |
| 215 |
|
| 216 |
e. e. e. repeat e. |
| 217 |
repeat e. |
| 218 |
|
| 219 |
unfold to_sval. fold to_sval. |
| 220 |
rewrite append_strict_list. |
| 221 |
reflexivity. |
| 222 |
|
| 223 |
eapply global_extends_eager_eval. |
| 224 |
|
| 225 |
replace (map (fun x3 : ext_val => to_sval (xor_const 54 x3)) l) with |
| 226 |
(map to_sval (map (fun x3 => xor_const 54 x3) l)) by (rewrite list_map_compose; reflexivity). |
| 227 |
rewrite <- list_append_map. |
| 228 |
remember (app (map (fun x3 : ext_val => xor_const 54 x3) l) l0) as ll. |
| 229 |
replace (strict_list (map to_sval ll)) with (to_sval (eseq ll)) by (reflexivity). |
| 230 |
subst ll. |
| 231 |
eapply H4. |
| 232 |
econstructor. |
| 233 |
|
| 234 |
rewrite Forall_app. split; auto. |
| 235 |
eapply Forall_map. eassumption. |
| 236 |
|
| 237 |
intros. eapply xor_const_byte; eauto. |
| 238 |
|
| 239 |
unfold bind_decl_groups. |
| 240 |
unfold bind_decl_group. |
| 241 |
unfold declare. |
| 242 |
|
| 243 |
repeat (eapply global_extends_extend_r; try eapply wf_env_name_irrel_GE; eauto). |
| 244 |
eapply global_extends_refl. |
| 245 |
|
| 246 |
eapply HIDs. simpl. left. reflexivity. |
| 247 |
eapply HIDs. simpl. right. left. reflexivity. |
| 248 |
eapply HIDs. simpl. right. right. left. reflexivity. |
| 249 |
eapply HIDs. simpl. right. right. right. left. reflexivity. |
| 250 |
|
| 251 |
e. repeat e. |
| 252 |
e. e. e. |
| 253 |
|
| 254 |
simpl. |
| 255 |
rewrite <- Heqhv1. |
| 256 |
rewrite Hlres. reflexivity. |
| 257 |
|
| 258 |
e. repeat e. repeat e. |
| 259 |
|
| 260 |
rewrite append_strict_list. reflexivity. |
| 261 |
eapply global_extends_eager_eval. |
| 262 |
|
| 263 |
(* get to_sval out to outside *) |
| 264 |
(* evaluate the hash function *) |
| 265 |
|
| 266 |
replace (map (fun x4 : ext_val => to_sval (xor_const 92 x4)) l) with |
| 267 |
(map to_sval (map (xor_const 92) l)) by |
| 268 |
(clear -l; |
| 269 |
induction l; simpl; auto; f_equal; eapply IHl; eauto). |
| 270 |
|
| 271 |
rewrite get_each_n_map_commutes. |
| 272 |
|
| 273 |
|
| 274 |
rewrite map_strict_list_map_map_to_sval. |
| 275 |
rewrite <- list_append_map. |
| 276 |
rewrite strict_list_map_to_sval. |
| 277 |
|
| 278 |
(* This one will be some fun *) |
| 279 |
assert (exists n, has_type (eseq (map (xor_const 92) l ++ map eseq (get_each_n (Pos.to_nat 8) x3))) (bytestream n)). { |
| 280 |
|
| 281 |
|
| 282 |
eapply has_type_seq_append. |
| 283 |
exists (Datatypes.length (map (xor_const 92) l)). |
| 284 |
econstructor. |
| 285 |
eapply Forall_map. eassumption. |
| 286 |
intros. eapply xor_const_byte; eauto. |
| 287 |
subst hv1. |
| 288 |
inversion HT. subst. |
| 289 |
rewrite <- H5 in Hlres. |
| 290 |
eapply list_of_strictval_to_sval in Hlres. inversion Hlres. |
| 291 |
subst. clear Hlres. |
| 292 |
remember H1 as HHash. |
| 293 |
clear HeqHHash. |
| 294 |
symmetry in H5. |
| 295 |
eapply good_hash_fully_padded in H1; try eassumption. |
| 296 |
eapply type_stream_of_bytes in H1; eauto. |
| 297 |
} |
| 298 |
|
| 299 |
destruct H5. |
| 300 |
eapply H4 in H5. destruct H5. eapply H5. |
| 301 |
|
| 302 |
repeat (eapply global_extends_extend_r; try eapply wf_env_name_irrel_GE; eauto). |
| 303 |
eapply global_extends_refl. |
| 304 |
|
| 305 |
eapply HIDs. simpl. left. reflexivity. |
| 306 |
eapply HIDs. simpl. right. left. reflexivity. |
| 307 |
eapply HIDs. simpl. right. right. left. reflexivity. |
| 308 |
eapply HIDs. simpl. right. right. right. left. reflexivity. |
| 309 |
|
| 310 |
(* our result matches the model *) |
| 311 |
subst hv1. |
| 312 |
eapply list_of_strictval_to_sval in Hlres. |
| 313 |
simpl. rewrite Hlres. |
| 314 |
|
| 315 |
reflexivity. |
| 316 |
|
| 317 |
Unshelve. |
| 318 |
all: exact id. |
| 319 |
|
| 320 |
Qed. |