src/HOL/Metis_Examples/Message.thy
author huffman
Thu Aug 11 09:11:15 2011 -0700 (2011-08-11)
changeset 44165 d26a45f3c835
parent 43197 c71657bbdbc0
child 45503 44790ec65f70
permissions -rw-r--r--
remove lemma stupid_ext
     1 (*  Title:      HOL/Metis_Examples/Message.thy
     2     Author:     Lawrence C. Paulson, Cambridge University Computer Laboratory
     3     Author:     Jasmin Blanchette, TU Muenchen
     4 
     5 Metis example featuring message authentication.
     6 *)
     7 
     8 header {* Metis Example Featuring Message Authentication *}
     9 
    10 theory Message
    11 imports Main
    12 begin
    13 
    14 declare [[metis_new_skolemizer]]
    15 
    16 lemma strange_Un_eq [simp]: "A \<union> (B \<union> A) = B \<union> A"
    17 by (metis Un_commute Un_left_absorb)
    18 
    19 type_synonym key = nat
    20 
    21 consts
    22   all_symmetric :: bool        --{*true if all keys are symmetric*}
    23   invKey        :: "key=>key"  --{*inverse of a symmetric key*}
    24 
    25 specification (invKey)
    26   invKey [simp]: "invKey (invKey K) = K"
    27   invKey_symmetric: "all_symmetric --> invKey = id"
    28 by (metis id_apply)
    29 
    30 
    31 text{*The inverse of a symmetric key is itself; that of a public key
    32       is the private key and vice versa*}
    33 
    34 definition symKeys :: "key set" where
    35   "symKeys == {K. invKey K = K}"
    36 
    37 datatype  --{*We allow any number of friendly agents*}
    38   agent = Server | Friend nat | Spy
    39 
    40 datatype
    41      msg = Agent  agent     --{*Agent names*}
    42          | Number nat       --{*Ordinary integers, timestamps, ...*}
    43          | Nonce  nat       --{*Unguessable nonces*}
    44          | Key    key       --{*Crypto keys*}
    45          | Hash   msg       --{*Hashing*}
    46          | MPair  msg msg   --{*Compound messages*}
    47          | Crypt  key msg   --{*Encryption, public- or shared-key*}
    48 
    49 
    50 text{*Concrete syntax: messages appear as {|A,B,NA|}, etc...*}
    51 syntax
    52   "_MTuple"      :: "['a, args] => 'a * 'b"       ("(2{|_,/ _|})")
    53 
    54 syntax (xsymbols)
    55   "_MTuple"      :: "['a, args] => 'a * 'b"       ("(2\<lbrace>_,/ _\<rbrace>)")
    56 
    57 translations
    58   "{|x, y, z|}"   == "{|x, {|y, z|}|}"
    59   "{|x, y|}"      == "CONST MPair x y"
    60 
    61 
    62 definition HPair :: "[msg,msg] => msg" ("(4Hash[_] /_)" [0, 1000]) where
    63     --{*Message Y paired with a MAC computed with the help of X*}
    64     "Hash[X] Y == {| Hash{|X,Y|}, Y|}"
    65 
    66 definition keysFor :: "msg set => key set" where
    67     --{*Keys useful to decrypt elements of a message set*}
    68   "keysFor H == invKey ` {K. \<exists>X. Crypt K X \<in> H}"
    69 
    70 
    71 subsubsection{*Inductive Definition of All Parts" of a Message*}
    72 
    73 inductive_set
    74   parts :: "msg set => msg set"
    75   for H :: "msg set"
    76   where
    77     Inj [intro]:               "X \<in> H ==> X \<in> parts H"
    78   | Fst:         "{|X,Y|}   \<in> parts H ==> X \<in> parts H"
    79   | Snd:         "{|X,Y|}   \<in> parts H ==> Y \<in> parts H"
    80   | Body:        "Crypt K X \<in> parts H ==> X \<in> parts H"
    81 
    82 lemma parts_mono: "G \<subseteq> H ==> parts(G) \<subseteq> parts(H)"
    83 apply auto
    84 apply (erule parts.induct)
    85    apply (metis parts.Inj set_rev_mp)
    86   apply (metis parts.Fst)
    87  apply (metis parts.Snd)
    88 by (metis parts.Body)
    89 
    90 text{*Equations hold because constructors are injective.*}
    91 lemma Friend_image_eq [simp]: "(Friend x \<in> Friend`A) = (x:A)"
    92 by (metis agent.inject image_iff)
    93 
    94 lemma Key_image_eq [simp]: "(Key x \<in> Key`A) = (x \<in> A)"
    95 by (metis image_iff msg.inject(4))
    96 
    97 lemma Nonce_Key_image_eq [simp]: "Nonce x \<notin> Key`A"
    98 by (metis image_iff msg.distinct(23))
    99 
   100 
   101 subsubsection{*Inverse of keys *}
   102 
   103 lemma invKey_eq [simp]: "(invKey K = invKey K') = (K = K')"
   104 by (metis invKey)
   105 
   106 
   107 subsection{*keysFor operator*}
   108 
   109 lemma keysFor_empty [simp]: "keysFor {} = {}"
   110 by (unfold keysFor_def, blast)
   111 
   112 lemma keysFor_Un [simp]: "keysFor (H \<union> H') = keysFor H \<union> keysFor H'"
   113 by (unfold keysFor_def, blast)
   114 
   115 lemma keysFor_UN [simp]: "keysFor (\<Union>i\<in>A. H i) = (\<Union>i\<in>A. keysFor (H i))"
   116 by (unfold keysFor_def, blast)
   117 
   118 text{*Monotonicity*}
   119 lemma keysFor_mono: "G \<subseteq> H ==> keysFor(G) \<subseteq> keysFor(H)"
   120 by (unfold keysFor_def, blast)
   121 
   122 lemma keysFor_insert_Agent [simp]: "keysFor (insert (Agent A) H) = keysFor H"
   123 by (unfold keysFor_def, auto)
   124 
   125 lemma keysFor_insert_Nonce [simp]: "keysFor (insert (Nonce N) H) = keysFor H"
   126 by (unfold keysFor_def, auto)
   127 
   128 lemma keysFor_insert_Number [simp]: "keysFor (insert (Number N) H) = keysFor H"
   129 by (unfold keysFor_def, auto)
   130 
   131 lemma keysFor_insert_Key [simp]: "keysFor (insert (Key K) H) = keysFor H"
   132 by (unfold keysFor_def, auto)
   133 
   134 lemma keysFor_insert_Hash [simp]: "keysFor (insert (Hash X) H) = keysFor H"
   135 by (unfold keysFor_def, auto)
   136 
   137 lemma keysFor_insert_MPair [simp]: "keysFor (insert {|X,Y|} H) = keysFor H"
   138 by (unfold keysFor_def, auto)
   139 
   140 lemma keysFor_insert_Crypt [simp]:
   141     "keysFor (insert (Crypt K X) H) = insert (invKey K) (keysFor H)"
   142 by (unfold keysFor_def, auto)
   143 
   144 lemma keysFor_image_Key [simp]: "keysFor (Key`E) = {}"
   145 by (unfold keysFor_def, auto)
   146 
   147 lemma Crypt_imp_invKey_keysFor: "Crypt K X \<in> H ==> invKey K \<in> keysFor H"
   148 by (unfold keysFor_def, blast)
   149 
   150 
   151 subsection{*Inductive relation "parts"*}
   152 
   153 lemma MPair_parts:
   154      "[| {|X,Y|} \<in> parts H;
   155          [| X \<in> parts H; Y \<in> parts H |] ==> P |] ==> P"
   156 by (blast dest: parts.Fst parts.Snd)
   157 
   158 declare MPair_parts [elim!] parts.Body [dest!]
   159 text{*NB These two rules are UNSAFE in the formal sense, as they discard the
   160      compound message.  They work well on THIS FILE.
   161   @{text MPair_parts} is left as SAFE because it speeds up proofs.
   162   The Crypt rule is normally kept UNSAFE to avoid breaking up certificates.*}
   163 
   164 lemma parts_increasing: "H \<subseteq> parts(H)"
   165 by blast
   166 
   167 lemmas parts_insertI = subset_insertI [THEN parts_mono, THEN subsetD, standard]
   168 
   169 lemma parts_empty [simp]: "parts{} = {}"
   170 apply safe
   171 apply (erule parts.induct)
   172 apply blast+
   173 done
   174 
   175 lemma parts_emptyE [elim!]: "X\<in> parts{} ==> P"
   176 by simp
   177 
   178 text{*WARNING: loops if H = {Y}, therefore must not be repeated!*}
   179 lemma parts_singleton: "X\<in> parts H ==> \<exists>Y\<in>H. X\<in> parts {Y}"
   180 apply (erule parts.induct)
   181 apply fast+
   182 done
   183 
   184 
   185 subsubsection{*Unions *}
   186 
   187 lemma parts_Un_subset1: "parts(G) \<union> parts(H) \<subseteq> parts(G \<union> H)"
   188 by (intro Un_least parts_mono Un_upper1 Un_upper2)
   189 
   190 lemma parts_Un_subset2: "parts(G \<union> H) \<subseteq> parts(G) \<union> parts(H)"
   191 apply (rule subsetI)
   192 apply (erule parts.induct, blast+)
   193 done
   194 
   195 lemma parts_Un [simp]: "parts(G \<union> H) = parts(G) \<union> parts(H)"
   196 by (intro equalityI parts_Un_subset1 parts_Un_subset2)
   197 
   198 lemma parts_insert: "parts (insert X H) = parts {X} \<union> parts H"
   199 apply (subst insert_is_Un [of _ H])
   200 apply (simp only: parts_Un)
   201 done
   202 
   203 lemma parts_insert2:
   204      "parts (insert X (insert Y H)) = parts {X} \<union> parts {Y} \<union> parts H"
   205 by (metis Un_commute Un_empty_left Un_empty_right Un_insert_left Un_insert_right parts_Un)
   206 
   207 
   208 lemma parts_UN_subset1: "(\<Union>x\<in>A. parts(H x)) \<subseteq> parts(\<Union>x\<in>A. H x)"
   209 by (intro UN_least parts_mono UN_upper)
   210 
   211 lemma parts_UN_subset2: "parts(\<Union>x\<in>A. H x) \<subseteq> (\<Union>x\<in>A. parts(H x))"
   212 apply (rule subsetI)
   213 apply (erule parts.induct, blast+)
   214 done
   215 
   216 lemma parts_UN [simp]: "parts(\<Union>x\<in>A. H x) = (\<Union>x\<in>A. parts(H x))"
   217 by (intro equalityI parts_UN_subset1 parts_UN_subset2)
   218 
   219 text{*Added to simplify arguments to parts, analz and synth.
   220   NOTE: the UN versions are no longer used!*}
   221 
   222 
   223 text{*This allows @{text blast} to simplify occurrences of
   224   @{term "parts(G\<union>H)"} in the assumption.*}
   225 lemmas in_parts_UnE = parts_Un [THEN equalityD1, THEN subsetD, THEN UnE]
   226 declare in_parts_UnE [elim!]
   227 
   228 lemma parts_insert_subset: "insert X (parts H) \<subseteq> parts(insert X H)"
   229 by (blast intro: parts_mono [THEN [2] rev_subsetD])
   230 
   231 subsubsection{*Idempotence and transitivity *}
   232 
   233 lemma parts_partsD [dest!]: "X\<in> parts (parts H) ==> X\<in> parts H"
   234 by (erule parts.induct, blast+)
   235 
   236 lemma parts_idem [simp]: "parts (parts H) = parts H"
   237 by blast
   238 
   239 lemma parts_subset_iff [simp]: "(parts G \<subseteq> parts H) = (G \<subseteq> parts H)"
   240 apply (rule iffI)
   241 apply (metis Un_absorb1 Un_subset_iff parts_Un parts_increasing)
   242 apply (metis parts_idem parts_mono)
   243 done
   244 
   245 lemma parts_trans: "[| X\<in> parts G;  G \<subseteq> parts H |] ==> X\<in> parts H"
   246 by (blast dest: parts_mono);
   247 
   248 lemma parts_cut: "[|Y\<in> parts (insert X G);  X\<in> parts H|] ==> Y\<in> parts(G \<union> H)"
   249 by (metis Un_insert_left Un_insert_right insert_absorb mem_def parts_Un parts_idem sup1CI)
   250 
   251 
   252 subsubsection{*Rewrite rules for pulling out atomic messages *}
   253 
   254 lemmas parts_insert_eq_I = equalityI [OF subsetI parts_insert_subset]
   255 
   256 
   257 lemma parts_insert_Agent [simp]:
   258      "parts (insert (Agent agt) H) = insert (Agent agt) (parts H)"
   259 apply (rule parts_insert_eq_I)
   260 apply (erule parts.induct, auto)
   261 done
   262 
   263 lemma parts_insert_Nonce [simp]:
   264      "parts (insert (Nonce N) H) = insert (Nonce N) (parts H)"
   265 apply (rule parts_insert_eq_I)
   266 apply (erule parts.induct, auto)
   267 done
   268 
   269 lemma parts_insert_Number [simp]:
   270      "parts (insert (Number N) H) = insert (Number N) (parts H)"
   271 apply (rule parts_insert_eq_I)
   272 apply (erule parts.induct, auto)
   273 done
   274 
   275 lemma parts_insert_Key [simp]:
   276      "parts (insert (Key K) H) = insert (Key K) (parts H)"
   277 apply (rule parts_insert_eq_I)
   278 apply (erule parts.induct, auto)
   279 done
   280 
   281 lemma parts_insert_Hash [simp]:
   282      "parts (insert (Hash X) H) = insert (Hash X) (parts H)"
   283 apply (rule parts_insert_eq_I)
   284 apply (erule parts.induct, auto)
   285 done
   286 
   287 lemma parts_insert_Crypt [simp]:
   288      "parts (insert (Crypt K X) H) =
   289           insert (Crypt K X) (parts (insert X H))"
   290 apply (rule equalityI)
   291 apply (rule subsetI)
   292 apply (erule parts.induct, auto)
   293 apply (blast intro: parts.Body)
   294 done
   295 
   296 lemma parts_insert_MPair [simp]:
   297      "parts (insert {|X,Y|} H) =
   298           insert {|X,Y|} (parts (insert X (insert Y H)))"
   299 apply (rule equalityI)
   300 apply (rule subsetI)
   301 apply (erule parts.induct, auto)
   302 apply (blast intro: parts.Fst parts.Snd)+
   303 done
   304 
   305 lemma parts_image_Key [simp]: "parts (Key`N) = Key`N"
   306 apply auto
   307 apply (erule parts.induct, auto)
   308 done
   309 
   310 lemma msg_Nonce_supply: "\<exists>N. \<forall>n. N\<le>n --> Nonce n \<notin> parts {msg}"
   311 apply (induct_tac "msg")
   312 apply (simp_all add: parts_insert2)
   313 apply (metis Suc_n_not_le_n)
   314 apply (metis le_trans linorder_linear)
   315 done
   316 
   317 subsection{*Inductive relation "analz"*}
   318 
   319 text{*Inductive definition of "analz" -- what can be broken down from a set of
   320     messages, including keys.  A form of downward closure.  Pairs can
   321     be taken apart; messages decrypted with known keys.  *}
   322 
   323 inductive_set
   324   analz :: "msg set => msg set"
   325   for H :: "msg set"
   326   where
   327     Inj [intro,simp] :    "X \<in> H ==> X \<in> analz H"
   328   | Fst:     "{|X,Y|} \<in> analz H ==> X \<in> analz H"
   329   | Snd:     "{|X,Y|} \<in> analz H ==> Y \<in> analz H"
   330   | Decrypt [dest]:
   331              "[|Crypt K X \<in> analz H; Key(invKey K): analz H|] ==> X \<in> analz H"
   332 
   333 
   334 text{*Monotonicity; Lemma 1 of Lowe's paper*}
   335 lemma analz_mono: "G\<subseteq>H ==> analz(G) \<subseteq> analz(H)"
   336 apply auto
   337 apply (erule analz.induct)
   338 apply (auto dest: analz.Fst analz.Snd)
   339 done
   340 
   341 text{*Making it safe speeds up proofs*}
   342 lemma MPair_analz [elim!]:
   343      "[| {|X,Y|} \<in> analz H;
   344              [| X \<in> analz H; Y \<in> analz H |] ==> P
   345           |] ==> P"
   346 by (blast dest: analz.Fst analz.Snd)
   347 
   348 lemma analz_increasing: "H \<subseteq> analz(H)"
   349 by blast
   350 
   351 lemma analz_subset_parts: "analz H \<subseteq> parts H"
   352 apply (rule subsetI)
   353 apply (erule analz.induct, blast+)
   354 done
   355 
   356 lemmas analz_into_parts = analz_subset_parts [THEN subsetD, standard]
   357 
   358 lemmas not_parts_not_analz = analz_subset_parts [THEN contra_subsetD, standard]
   359 
   360 lemma parts_analz [simp]: "parts (analz H) = parts H"
   361 apply (rule equalityI)
   362 apply (metis analz_subset_parts parts_subset_iff)
   363 apply (metis analz_increasing parts_mono)
   364 done
   365 
   366 
   367 lemma analz_parts [simp]: "analz (parts H) = parts H"
   368 apply auto
   369 apply (erule analz.induct, auto)
   370 done
   371 
   372 lemmas analz_insertI = subset_insertI [THEN analz_mono, THEN [2] rev_subsetD, standard]
   373 
   374 subsubsection{*General equational properties *}
   375 
   376 lemma analz_empty [simp]: "analz{} = {}"
   377 apply safe
   378 apply (erule analz.induct, blast+)
   379 done
   380 
   381 text{*Converse fails: we can analz more from the union than from the
   382   separate parts, as a key in one might decrypt a message in the other*}
   383 lemma analz_Un: "analz(G) \<union> analz(H) \<subseteq> analz(G \<union> H)"
   384 by (intro Un_least analz_mono Un_upper1 Un_upper2)
   385 
   386 lemma analz_insert: "insert X (analz H) \<subseteq> analz(insert X H)"
   387 by (blast intro: analz_mono [THEN [2] rev_subsetD])
   388 
   389 subsubsection{*Rewrite rules for pulling out atomic messages *}
   390 
   391 lemmas analz_insert_eq_I = equalityI [OF subsetI analz_insert]
   392 
   393 lemma analz_insert_Agent [simp]:
   394      "analz (insert (Agent agt) H) = insert (Agent agt) (analz H)"
   395 apply (rule analz_insert_eq_I)
   396 apply (erule analz.induct, auto)
   397 done
   398 
   399 lemma analz_insert_Nonce [simp]:
   400      "analz (insert (Nonce N) H) = insert (Nonce N) (analz H)"
   401 apply (rule analz_insert_eq_I)
   402 apply (erule analz.induct, auto)
   403 done
   404 
   405 lemma analz_insert_Number [simp]:
   406      "analz (insert (Number N) H) = insert (Number N) (analz H)"
   407 apply (rule analz_insert_eq_I)
   408 apply (erule analz.induct, auto)
   409 done
   410 
   411 lemma analz_insert_Hash [simp]:
   412      "analz (insert (Hash X) H) = insert (Hash X) (analz H)"
   413 apply (rule analz_insert_eq_I)
   414 apply (erule analz.induct, auto)
   415 done
   416 
   417 text{*Can only pull out Keys if they are not needed to decrypt the rest*}
   418 lemma analz_insert_Key [simp]:
   419     "K \<notin> keysFor (analz H) ==>
   420           analz (insert (Key K) H) = insert (Key K) (analz H)"
   421 apply (unfold keysFor_def)
   422 apply (rule analz_insert_eq_I)
   423 apply (erule analz.induct, auto)
   424 done
   425 
   426 lemma analz_insert_MPair [simp]:
   427      "analz (insert {|X,Y|} H) =
   428           insert {|X,Y|} (analz (insert X (insert Y H)))"
   429 apply (rule equalityI)
   430 apply (rule subsetI)
   431 apply (erule analz.induct, auto)
   432 apply (erule analz.induct)
   433 apply (blast intro: analz.Fst analz.Snd)+
   434 done
   435 
   436 text{*Can pull out enCrypted message if the Key is not known*}
   437 lemma analz_insert_Crypt:
   438      "Key (invKey K) \<notin> analz H
   439       ==> analz (insert (Crypt K X) H) = insert (Crypt K X) (analz H)"
   440 apply (rule analz_insert_eq_I)
   441 apply (erule analz.induct, auto)
   442 
   443 done
   444 
   445 lemma lemma1: "Key (invKey K) \<in> analz H ==>
   446                analz (insert (Crypt K X) H) \<subseteq>
   447                insert (Crypt K X) (analz (insert X H))"
   448 apply (rule subsetI)
   449 apply (erule_tac x = x in analz.induct, auto)
   450 done
   451 
   452 lemma lemma2: "Key (invKey K) \<in> analz H ==>
   453                insert (Crypt K X) (analz (insert X H)) \<subseteq>
   454                analz (insert (Crypt K X) H)"
   455 apply auto
   456 apply (erule_tac x = x in analz.induct, auto)
   457 apply (blast intro: analz_insertI analz.Decrypt)
   458 done
   459 
   460 lemma analz_insert_Decrypt:
   461      "Key (invKey K) \<in> analz H ==>
   462                analz (insert (Crypt K X) H) =
   463                insert (Crypt K X) (analz (insert X H))"
   464 by (intro equalityI lemma1 lemma2)
   465 
   466 text{*Case analysis: either the message is secure, or it is not! Effective,
   467 but can cause subgoals to blow up! Use with @{text "split_if"}; apparently
   468 @{text "split_tac"} does not cope with patterns such as @{term"analz (insert
   469 (Crypt K X) H)"} *}
   470 lemma analz_Crypt_if [simp]:
   471      "analz (insert (Crypt K X) H) =
   472           (if (Key (invKey K) \<in> analz H)
   473            then insert (Crypt K X) (analz (insert X H))
   474            else insert (Crypt K X) (analz H))"
   475 by (simp add: analz_insert_Crypt analz_insert_Decrypt)
   476 
   477 
   478 text{*This rule supposes "for the sake of argument" that we have the key.*}
   479 lemma analz_insert_Crypt_subset:
   480      "analz (insert (Crypt K X) H) \<subseteq>
   481            insert (Crypt K X) (analz (insert X H))"
   482 apply (rule subsetI)
   483 apply (erule analz.induct, auto)
   484 done
   485 
   486 
   487 lemma analz_image_Key [simp]: "analz (Key`N) = Key`N"
   488 apply auto
   489 apply (erule analz.induct, auto)
   490 done
   491 
   492 
   493 subsubsection{*Idempotence and transitivity *}
   494 
   495 lemma analz_analzD [dest!]: "X\<in> analz (analz H) ==> X\<in> analz H"
   496 by (erule analz.induct, blast+)
   497 
   498 lemma analz_idem [simp]: "analz (analz H) = analz H"
   499 by blast
   500 
   501 lemma analz_subset_iff [simp]: "(analz G \<subseteq> analz H) = (G \<subseteq> analz H)"
   502 apply (rule iffI)
   503 apply (iprover intro: subset_trans analz_increasing)
   504 apply (frule analz_mono, simp)
   505 done
   506 
   507 lemma analz_trans: "[| X\<in> analz G;  G \<subseteq> analz H |] ==> X\<in> analz H"
   508 by (drule analz_mono, blast)
   509 
   510 
   511 declare analz_trans[intro]
   512 
   513 lemma analz_cut: "[| Y\<in> analz (insert X H);  X\<in> analz H |] ==> Y\<in> analz H"
   514 (*TOO SLOW
   515 by (metis analz_idem analz_increasing analz_mono insert_absorb insert_mono insert_subset) --{*317s*}
   516 ??*)
   517 by (erule analz_trans, blast)
   518 
   519 
   520 text{*This rewrite rule helps in the simplification of messages that involve
   521   the forwarding of unknown components (X).  Without it, removing occurrences
   522   of X can be very complicated. *}
   523 lemma analz_insert_eq: "X\<in> analz H ==> analz (insert X H) = analz H"
   524 by (blast intro: analz_cut analz_insertI)
   525 
   526 
   527 text{*A congruence rule for "analz" *}
   528 
   529 lemma analz_subset_cong:
   530      "[| analz G \<subseteq> analz G'; analz H \<subseteq> analz H' |]
   531       ==> analz (G \<union> H) \<subseteq> analz (G' \<union> H')"
   532 apply simp
   533 apply (metis Un_absorb2 Un_commute Un_subset_iff Un_upper1 Un_upper2 analz_mono)
   534 done
   535 
   536 
   537 lemma analz_cong:
   538      "[| analz G = analz G'; analz H = analz H'
   539                |] ==> analz (G \<union> H) = analz (G' \<union> H')"
   540 by (intro equalityI analz_subset_cong, simp_all)
   541 
   542 lemma analz_insert_cong:
   543      "analz H = analz H' ==> analz(insert X H) = analz(insert X H')"
   544 by (force simp only: insert_def intro!: analz_cong)
   545 
   546 text{*If there are no pairs or encryptions then analz does nothing*}
   547 lemma analz_trivial:
   548      "[| \<forall>X Y. {|X,Y|} \<notin> H;  \<forall>X K. Crypt K X \<notin> H |] ==> analz H = H"
   549 apply safe
   550 apply (erule analz.induct, blast+)
   551 done
   552 
   553 text{*These two are obsolete (with a single Spy) but cost little to prove...*}
   554 lemma analz_UN_analz_lemma:
   555      "X\<in> analz (\<Union>i\<in>A. analz (H i)) ==> X\<in> analz (\<Union>i\<in>A. H i)"
   556 apply (erule analz.induct)
   557 apply (blast intro: analz_mono [THEN [2] rev_subsetD])+
   558 done
   559 
   560 lemma analz_UN_analz [simp]: "analz (\<Union>i\<in>A. analz (H i)) = analz (\<Union>i\<in>A. H i)"
   561 by (blast intro: analz_UN_analz_lemma analz_mono [THEN [2] rev_subsetD])
   562 
   563 
   564 subsection{*Inductive relation "synth"*}
   565 
   566 text{*Inductive definition of "synth" -- what can be built up from a set of
   567     messages.  A form of upward closure.  Pairs can be built, messages
   568     encrypted with known keys.  Agent names are public domain.
   569     Numbers can be guessed, but Nonces cannot be.  *}
   570 
   571 inductive_set
   572   synth :: "msg set => msg set"
   573   for H :: "msg set"
   574   where
   575     Inj    [intro]:   "X \<in> H ==> X \<in> synth H"
   576   | Agent  [intro]:   "Agent agt \<in> synth H"
   577   | Number [intro]:   "Number n  \<in> synth H"
   578   | Hash   [intro]:   "X \<in> synth H ==> Hash X \<in> synth H"
   579   | MPair  [intro]:   "[|X \<in> synth H;  Y \<in> synth H|] ==> {|X,Y|} \<in> synth H"
   580   | Crypt  [intro]:   "[|X \<in> synth H;  Key(K) \<in> H|] ==> Crypt K X \<in> synth H"
   581 
   582 text{*Monotonicity*}
   583 lemma synth_mono: "G\<subseteq>H ==> synth(G) \<subseteq> synth(H)"
   584   by (auto, erule synth.induct, auto)
   585 
   586 text{*NO @{text Agent_synth}, as any Agent name can be synthesized.
   587   The same holds for @{term Number}*}
   588 inductive_cases Nonce_synth [elim!]: "Nonce n \<in> synth H"
   589 inductive_cases Key_synth   [elim!]: "Key K \<in> synth H"
   590 inductive_cases Hash_synth  [elim!]: "Hash X \<in> synth H"
   591 inductive_cases MPair_synth [elim!]: "{|X,Y|} \<in> synth H"
   592 inductive_cases Crypt_synth [elim!]: "Crypt K X \<in> synth H"
   593 
   594 
   595 lemma synth_increasing: "H \<subseteq> synth(H)"
   596 by blast
   597 
   598 subsubsection{*Unions *}
   599 
   600 text{*Converse fails: we can synth more from the union than from the
   601   separate parts, building a compound message using elements of each.*}
   602 lemma synth_Un: "synth(G) \<union> synth(H) \<subseteq> synth(G \<union> H)"
   603 by (intro Un_least synth_mono Un_upper1 Un_upper2)
   604 
   605 lemma synth_insert: "insert X (synth H) \<subseteq> synth(insert X H)"
   606 by (metis insert_iff insert_subset subset_insertI synth.Inj synth_mono)
   607 
   608 subsubsection{*Idempotence and transitivity *}
   609 
   610 lemma synth_synthD [dest!]: "X\<in> synth (synth H) ==> X\<in> synth H"
   611 by (erule synth.induct, blast+)
   612 
   613 lemma synth_idem: "synth (synth H) = synth H"
   614 by blast
   615 
   616 lemma synth_subset_iff [simp]: "(synth G \<subseteq> synth H) = (G \<subseteq> synth H)"
   617 apply (rule iffI)
   618 apply (iprover intro: subset_trans synth_increasing)
   619 apply (frule synth_mono, simp add: synth_idem)
   620 done
   621 
   622 lemma synth_trans: "[| X\<in> synth G;  G \<subseteq> synth H |] ==> X\<in> synth H"
   623 by (drule synth_mono, blast)
   624 
   625 lemma synth_cut: "[| Y\<in> synth (insert X H);  X\<in> synth H |] ==> Y\<in> synth H"
   626 (*TOO SLOW
   627 by (metis insert_absorb insert_mono insert_subset synth_idem synth_increasing synth_mono)
   628 *)
   629 by (erule synth_trans, blast)
   630 
   631 
   632 lemma Agent_synth [simp]: "Agent A \<in> synth H"
   633 by blast
   634 
   635 lemma Number_synth [simp]: "Number n \<in> synth H"
   636 by blast
   637 
   638 lemma Nonce_synth_eq [simp]: "(Nonce N \<in> synth H) = (Nonce N \<in> H)"
   639 by blast
   640 
   641 lemma Key_synth_eq [simp]: "(Key K \<in> synth H) = (Key K \<in> H)"
   642 by blast
   643 
   644 lemma Crypt_synth_eq [simp]:
   645      "Key K \<notin> H ==> (Crypt K X \<in> synth H) = (Crypt K X \<in> H)"
   646 by blast
   647 
   648 
   649 lemma keysFor_synth [simp]:
   650     "keysFor (synth H) = keysFor H \<union> invKey`{K. Key K \<in> H}"
   651 by (unfold keysFor_def, blast)
   652 
   653 
   654 subsubsection{*Combinations of parts, analz and synth *}
   655 
   656 lemma parts_synth [simp]: "parts (synth H) = parts H \<union> synth H"
   657 apply (rule equalityI)
   658 apply (rule subsetI)
   659 apply (erule parts.induct)
   660 apply (metis UnCI)
   661 apply (metis MPair_synth UnCI UnE insert_absorb insert_subset parts.Fst parts_increasing)
   662 apply (metis MPair_synth UnCI UnE insert_absorb insert_subset parts.Snd parts_increasing)
   663 apply (metis Body Crypt_synth UnCI UnE insert_absorb insert_subset parts_increasing)
   664 apply (metis Un_subset_iff parts_increasing parts_mono synth_increasing)
   665 done
   666 
   667 lemma analz_analz_Un [simp]: "analz (analz G \<union> H) = analz (G \<union> H)"
   668 apply (rule equalityI);
   669 apply (metis analz_idem analz_subset_cong order_eq_refl)
   670 apply (metis analz_increasing analz_subset_cong order_eq_refl)
   671 done
   672 
   673 declare analz_mono [intro] analz.Fst [intro] analz.Snd [intro] Un_least [intro]
   674 
   675 lemma analz_synth_Un [simp]: "analz (synth G \<union> H) = analz (G \<union> H) \<union> synth G"
   676 apply (rule equalityI)
   677 apply (rule subsetI)
   678 apply (erule analz.induct)
   679 apply (metis UnCI UnE Un_commute analz.Inj)
   680 apply (metis MPair_synth UnCI UnE Un_commute analz.Fst analz.Inj mem_def)
   681 apply (metis MPair_synth UnCI UnE Un_commute analz.Inj analz.Snd mem_def)
   682 apply (blast intro: analz.Decrypt)
   683 apply blast
   684 done
   685 
   686 lemma analz_synth [simp]: "analz (synth H) = analz H \<union> synth H"
   687 proof -
   688   have "\<forall>x\<^isub>2 x\<^isub>1. synth x\<^isub>1 \<union> analz (x\<^isub>1 \<union> x\<^isub>2) = analz (synth x\<^isub>1 \<union> x\<^isub>2)" by (metis Un_commute analz_synth_Un)
   689   hence "\<forall>x\<^isub>1. synth x\<^isub>1 \<union> analz x\<^isub>1 = analz (synth x\<^isub>1 \<union> {})" by (metis Un_empty_right)
   690   hence "\<forall>x\<^isub>1. synth x\<^isub>1 \<union> analz x\<^isub>1 = analz (synth x\<^isub>1)" by (metis Un_empty_right)
   691   hence "\<forall>x\<^isub>1. analz x\<^isub>1 \<union> synth x\<^isub>1 = analz (synth x\<^isub>1)" by (metis Un_commute)
   692   thus "analz (synth H) = analz H \<union> synth H" by metis
   693 qed
   694 
   695 
   696 subsubsection{*For reasoning about the Fake rule in traces *}
   697 
   698 lemma parts_insert_subset_Un: "X\<in> G ==> parts(insert X H) \<subseteq> parts G \<union> parts H"
   699 proof -
   700   assume "X \<in> G"
   701   hence "G X" by (metis mem_def)
   702   hence "\<forall>x\<^isub>1. G \<subseteq> x\<^isub>1 \<longrightarrow> x\<^isub>1 X" by (metis predicate1D)
   703   hence "\<forall>x\<^isub>1. (G \<union> x\<^isub>1) X" by (metis Un_upper1)
   704   hence "\<forall>x\<^isub>1. X \<in> G \<union> x\<^isub>1" by (metis mem_def)
   705   hence "insert X H \<subseteq> G \<union> H" by (metis Un_upper2 insert_subset)
   706   hence "parts (insert X H) \<subseteq> parts (G \<union> H)" by (metis parts_mono)
   707   thus "parts (insert X H) \<subseteq> parts G \<union> parts H" by (metis parts_Un)
   708 qed
   709 
   710 lemma Fake_parts_insert:
   711      "X \<in> synth (analz H) ==>
   712       parts (insert X H) \<subseteq> synth (analz H) \<union> parts H"
   713 proof -
   714   assume A1: "X \<in> synth (analz H)"
   715   have F1: "\<forall>x\<^isub>1. analz x\<^isub>1 \<union> synth (analz x\<^isub>1) = analz (synth (analz x\<^isub>1))"
   716     by (metis analz_idem analz_synth)
   717   have F2: "\<forall>x\<^isub>1. parts x\<^isub>1 \<union> synth (analz x\<^isub>1) = parts (synth (analz x\<^isub>1))"
   718     by (metis parts_analz parts_synth)
   719   have F3: "synth (analz H) X" using A1 by (metis mem_def)
   720   have "\<forall>x\<^isub>2 x\<^isub>1\<Colon>msg set. x\<^isub>1 \<le> sup x\<^isub>1 x\<^isub>2" by (metis inf_sup_ord(3))
   721   hence F4: "\<forall>x\<^isub>1. analz x\<^isub>1 \<subseteq> analz (synth x\<^isub>1)" by (metis analz_synth)
   722   have F5: "X \<in> synth (analz H)" using F3 by (metis mem_def)
   723   have "\<forall>x\<^isub>1. analz x\<^isub>1 \<subseteq> synth (analz x\<^isub>1)
   724          \<longrightarrow> analz (synth (analz x\<^isub>1)) = synth (analz x\<^isub>1)"
   725     using F1 by (metis subset_Un_eq)
   726   hence F6: "\<forall>x\<^isub>1. analz (synth (analz x\<^isub>1)) = synth (analz x\<^isub>1)"
   727     by (metis synth_increasing)
   728   have "\<forall>x\<^isub>1. x\<^isub>1 \<subseteq> analz (synth x\<^isub>1)" using F4 by (metis analz_subset_iff)
   729   hence "\<forall>x\<^isub>1. x\<^isub>1 \<subseteq> analz (synth (analz x\<^isub>1))" by (metis analz_subset_iff)
   730   hence "\<forall>x\<^isub>1. x\<^isub>1 \<subseteq> synth (analz x\<^isub>1)" using F6 by metis
   731   hence "H \<subseteq> synth (analz H)" by metis
   732   hence "H \<subseteq> synth (analz H) \<and> X \<in> synth (analz H)" using F5 by metis
   733   hence "insert X H \<subseteq> synth (analz H)" by (metis insert_subset)
   734   hence "parts (insert X H) \<subseteq> parts (synth (analz H))" by (metis parts_mono)
   735   hence "parts (insert X H) \<subseteq> parts H \<union> synth (analz H)" using F2 by metis
   736   thus "parts (insert X H) \<subseteq> synth (analz H) \<union> parts H" by (metis Un_commute)
   737 qed
   738 
   739 lemma Fake_parts_insert_in_Un:
   740      "[|Z \<in> parts (insert X H);  X: synth (analz H)|]
   741       ==> Z \<in>  synth (analz H) \<union> parts H";
   742 by (blast dest: Fake_parts_insert [THEN subsetD, dest])
   743 
   744 declare analz_mono [intro] synth_mono [intro]
   745 
   746 lemma Fake_analz_insert:
   747      "X \<in> synth (analz G) ==>
   748       analz (insert X H) \<subseteq> synth (analz G) \<union> analz (G \<union> H)"
   749 by (metis Un_commute Un_insert_left Un_insert_right Un_upper1 analz_analz_Un
   750           analz_mono analz_synth_Un insert_absorb)
   751 
   752 lemma Fake_analz_insert_simpler:
   753      "X \<in> synth (analz G) ==>
   754       analz (insert X H) \<subseteq> synth (analz G) \<union> analz (G \<union> H)"
   755 apply (rule subsetI)
   756 apply (subgoal_tac "x \<in> analz (synth (analz G) \<union> H) ")
   757 apply (metis Un_commute analz_analz_Un analz_synth_Un)
   758 by (metis Un_upper1 Un_upper2 analz_mono insert_absorb insert_subset)
   759 
   760 end