src/HOL/Auth/Message.thy
author haftmann
Tue Jul 21 11:09:50 2009 +0200 (2009-07-21)
changeset 32117 0762b9ad83df
parent 30607 c3d1590debd8
child 32149 ef59550a55d3
permissions -rw-r--r--
Set.thy: prefer = over == where possible; tuned ML setup; dropped (moved) ML legacy
     1 (*  Title:      HOL/Auth/Message
     2     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     3     Copyright   1996  University of Cambridge
     4 
     5 Datatypes of agents and messages;
     6 Inductive relations "parts", "analz" and "synth"
     7 *)
     8 
     9 header{*Theory of Agents and Messages for Security Protocols*}
    10 
    11 theory Message
    12 imports Main
    13 begin
    14 
    15 (*Needed occasionally with spy_analz_tac, e.g. in analz_insert_Key_newK*)
    16 lemma [simp] : "A \<union> (B \<union> A) = B \<union> A"
    17 by blast
    18 
    19 types 
    20   key = nat
    21 
    22 consts
    23   all_symmetric :: bool        --{*true if all keys are symmetric*}
    24   invKey        :: "key=>key"  --{*inverse of a symmetric key*}
    25 
    26 specification (invKey)
    27   invKey [simp]: "invKey (invKey K) = K"
    28   invKey_symmetric: "all_symmetric --> invKey = id"
    29     by (rule exI [of _ id], auto)
    30 
    31 
    32 text{*The inverse of a symmetric key is itself; that of a public key
    33       is the private key and vice versa*}
    34 
    35 constdefs
    36   symKeys :: "key set"
    37   "symKeys == {K. invKey K = K}"
    38 
    39 datatype  --{*We allow any number of friendly agents*}
    40   agent = Server | Friend nat | Spy
    41 
    42 datatype
    43      msg = Agent  agent	    --{*Agent names*}
    44          | Number nat       --{*Ordinary integers, timestamps, ...*}
    45          | Nonce  nat       --{*Unguessable nonces*}
    46          | Key    key       --{*Crypto keys*}
    47 	 | Hash   msg       --{*Hashing*}
    48 	 | MPair  msg msg   --{*Compound messages*}
    49 	 | Crypt  key msg   --{*Encryption, public- or shared-key*}
    50 
    51 
    52 text{*Concrete syntax: messages appear as {|A,B,NA|}, etc...*}
    53 syntax
    54   "@MTuple"      :: "['a, args] => 'a * 'b"       ("(2{|_,/ _|})")
    55 
    56 syntax (xsymbols)
    57   "@MTuple"      :: "['a, args] => 'a * 'b"       ("(2\<lbrace>_,/ _\<rbrace>)")
    58 
    59 translations
    60   "{|x, y, z|}"   == "{|x, {|y, z|}|}"
    61   "{|x, y|}"      == "MPair x y"
    62 
    63 
    64 constdefs
    65   HPair :: "[msg,msg] => msg"                       ("(4Hash[_] /_)" [0, 1000])
    66     --{*Message Y paired with a MAC computed with the help of X*}
    67     "Hash[X] Y == {| Hash{|X,Y|}, Y|}"
    68 
    69   keysFor :: "msg set => key set"
    70     --{*Keys useful to decrypt elements of a message set*}
    71   "keysFor H == invKey ` {K. \<exists>X. Crypt K X \<in> H}"
    72 
    73 
    74 subsubsection{*Inductive Definition of All Parts" of a Message*}
    75 
    76 inductive_set
    77   parts :: "msg set => msg set"
    78   for H :: "msg set"
    79   where
    80     Inj [intro]:               "X \<in> H ==> X \<in> parts H"
    81   | Fst:         "{|X,Y|}   \<in> parts H ==> X \<in> parts H"
    82   | Snd:         "{|X,Y|}   \<in> parts H ==> Y \<in> parts H"
    83   | Body:        "Crypt K X \<in> parts H ==> X \<in> parts H"
    84 
    85 
    86 text{*Monotonicity*}
    87 lemma parts_mono: "G \<subseteq> H ==> parts(G) \<subseteq> parts(H)"
    88 apply auto
    89 apply (erule parts.induct) 
    90 apply (blast dest: parts.Fst parts.Snd parts.Body)+
    91 done
    92 
    93 
    94 text{*Equations hold because constructors are injective.*}
    95 lemma Friend_image_eq [simp]: "(Friend x \<in> Friend`A) = (x:A)"
    96 by auto
    97 
    98 lemma Key_image_eq [simp]: "(Key x \<in> Key`A) = (x\<in>A)"
    99 by auto
   100 
   101 lemma Nonce_Key_image_eq [simp]: "(Nonce x \<notin> Key`A)"
   102 by auto
   103 
   104 
   105 subsubsection{*Inverse of keys *}
   106 
   107 lemma invKey_eq [simp]: "(invKey K = invKey K') = (K=K')"
   108 by (metis invKey)
   109 
   110 
   111 subsection{*keysFor operator*}
   112 
   113 lemma keysFor_empty [simp]: "keysFor {} = {}"
   114 by (unfold keysFor_def, blast)
   115 
   116 lemma keysFor_Un [simp]: "keysFor (H \<union> H') = keysFor H \<union> keysFor H'"
   117 by (unfold keysFor_def, blast)
   118 
   119 lemma keysFor_UN [simp]: "keysFor (\<Union>i\<in>A. H i) = (\<Union>i\<in>A. keysFor (H i))"
   120 by (unfold keysFor_def, blast)
   121 
   122 text{*Monotonicity*}
   123 lemma keysFor_mono: "G \<subseteq> H ==> keysFor(G) \<subseteq> keysFor(H)"
   124 by (unfold keysFor_def, blast)
   125 
   126 lemma keysFor_insert_Agent [simp]: "keysFor (insert (Agent A) H) = keysFor H"
   127 by (unfold keysFor_def, auto)
   128 
   129 lemma keysFor_insert_Nonce [simp]: "keysFor (insert (Nonce N) H) = keysFor H"
   130 by (unfold keysFor_def, auto)
   131 
   132 lemma keysFor_insert_Number [simp]: "keysFor (insert (Number N) H) = keysFor H"
   133 by (unfold keysFor_def, auto)
   134 
   135 lemma keysFor_insert_Key [simp]: "keysFor (insert (Key K) H) = keysFor H"
   136 by (unfold keysFor_def, auto)
   137 
   138 lemma keysFor_insert_Hash [simp]: "keysFor (insert (Hash X) H) = keysFor H"
   139 by (unfold keysFor_def, auto)
   140 
   141 lemma keysFor_insert_MPair [simp]: "keysFor (insert {|X,Y|} H) = keysFor H"
   142 by (unfold keysFor_def, auto)
   143 
   144 lemma keysFor_insert_Crypt [simp]: 
   145     "keysFor (insert (Crypt K X) H) = insert (invKey K) (keysFor H)"
   146 by (unfold keysFor_def, auto)
   147 
   148 lemma keysFor_image_Key [simp]: "keysFor (Key`E) = {}"
   149 by (unfold keysFor_def, auto)
   150 
   151 lemma Crypt_imp_invKey_keysFor: "Crypt K X \<in> H ==> invKey K \<in> keysFor H"
   152 by (unfold keysFor_def, blast)
   153 
   154 
   155 subsection{*Inductive relation "parts"*}
   156 
   157 lemma MPair_parts:
   158      "[| {|X,Y|} \<in> parts H;        
   159          [| X \<in> parts H; Y \<in> parts H |] ==> P |] ==> P"
   160 by (blast dest: parts.Fst parts.Snd) 
   161 
   162 declare MPair_parts [elim!]  parts.Body [dest!]
   163 text{*NB These two rules are UNSAFE in the formal sense, as they discard the
   164      compound message.  They work well on THIS FILE.  
   165   @{text MPair_parts} is left as SAFE because it speeds up proofs.
   166   The Crypt rule is normally kept UNSAFE to avoid breaking up certificates.*}
   167 
   168 lemma parts_increasing: "H \<subseteq> parts(H)"
   169 by blast
   170 
   171 lemmas parts_insertI = subset_insertI [THEN parts_mono, THEN subsetD, standard]
   172 
   173 lemma parts_empty [simp]: "parts{} = {}"
   174 apply safe
   175 apply (erule parts.induct, blast+)
   176 done
   177 
   178 lemma parts_emptyE [elim!]: "X\<in> parts{} ==> P"
   179 by simp
   180 
   181 text{*WARNING: loops if H = {Y}, therefore must not be repeated!*}
   182 lemma parts_singleton: "X\<in> parts H ==> \<exists>Y\<in>H. X\<in> parts {Y}"
   183 by (erule parts.induct, fast+)
   184 
   185 
   186 subsubsection{*Unions *}
   187 
   188 lemma parts_Un_subset1: "parts(G) \<union> parts(H) \<subseteq> parts(G \<union> H)"
   189 by (intro Un_least parts_mono Un_upper1 Un_upper2)
   190 
   191 lemma parts_Un_subset2: "parts(G \<union> H) \<subseteq> parts(G) \<union> parts(H)"
   192 apply (rule subsetI)
   193 apply (erule parts.induct, blast+)
   194 done
   195 
   196 lemma parts_Un [simp]: "parts(G \<union> H) = parts(G) \<union> parts(H)"
   197 by (intro equalityI parts_Un_subset1 parts_Un_subset2)
   198 
   199 lemma parts_insert: "parts (insert X H) = parts {X} \<union> parts H"
   200 apply (subst insert_is_Un [of _ H])
   201 apply (simp only: parts_Un)
   202 done
   203 
   204 text{*TWO inserts to avoid looping.  This rewrite is better than nothing.
   205   Not suitable for Addsimps: its behaviour can be strange.*}
   206 lemma parts_insert2:
   207      "parts (insert X (insert Y H)) = parts {X} \<union> parts {Y} \<union> parts H"
   208 apply (simp add: Un_assoc)
   209 apply (simp add: parts_insert [symmetric])
   210 done
   211 
   212 lemma parts_UN_subset1: "(\<Union>x\<in>A. parts(H x)) \<subseteq> parts(\<Union>x\<in>A. H x)"
   213 by (intro UN_least parts_mono UN_upper)
   214 
   215 lemma parts_UN_subset2: "parts(\<Union>x\<in>A. H x) \<subseteq> (\<Union>x\<in>A. parts(H x))"
   216 apply (rule subsetI)
   217 apply (erule parts.induct, blast+)
   218 done
   219 
   220 lemma parts_UN [simp]: "parts(\<Union>x\<in>A. H x) = (\<Union>x\<in>A. parts(H x))"
   221 by (intro equalityI parts_UN_subset1 parts_UN_subset2)
   222 
   223 text{*Added to simplify arguments to parts, analz and synth.
   224   NOTE: the UN versions are no longer used!*}
   225 
   226 
   227 text{*This allows @{text blast} to simplify occurrences of 
   228   @{term "parts(G\<union>H)"} in the assumption.*}
   229 lemmas in_parts_UnE = parts_Un [THEN equalityD1, THEN subsetD, THEN UnE] 
   230 declare in_parts_UnE [elim!]
   231 
   232 
   233 lemma parts_insert_subset: "insert X (parts H) \<subseteq> parts(insert X H)"
   234 by (blast intro: parts_mono [THEN [2] rev_subsetD])
   235 
   236 subsubsection{*Idempotence and transitivity *}
   237 
   238 lemma parts_partsD [dest!]: "X\<in> parts (parts H) ==> X\<in> parts H"
   239 by (erule parts.induct, blast+)
   240 
   241 lemma parts_idem [simp]: "parts (parts H) = parts H"
   242 by blast
   243 
   244 lemma parts_subset_iff [simp]: "(parts G \<subseteq> parts H) = (G \<subseteq> parts H)"
   245 apply (rule iffI)
   246 apply (iprover intro: subset_trans parts_increasing)  
   247 apply (frule parts_mono, simp) 
   248 done
   249 
   250 lemma parts_trans: "[| X\<in> parts G;  G \<subseteq> parts H |] ==> X\<in> parts H"
   251 by (drule parts_mono, blast)
   252 
   253 text{*Cut*}
   254 lemma parts_cut:
   255      "[| Y\<in> parts (insert X G);  X\<in> parts H |] ==> Y\<in> parts (G \<union> H)" 
   256 by (blast intro: parts_trans) 
   257 
   258 
   259 lemma parts_cut_eq [simp]: "X\<in> parts H ==> parts (insert X H) = parts H"
   260 by (force dest!: parts_cut intro: parts_insertI)
   261 
   262 
   263 subsubsection{*Rewrite rules for pulling out atomic messages *}
   264 
   265 lemmas parts_insert_eq_I = equalityI [OF subsetI parts_insert_subset]
   266 
   267 
   268 lemma parts_insert_Agent [simp]:
   269      "parts (insert (Agent agt) H) = insert (Agent agt) (parts H)"
   270 apply (rule parts_insert_eq_I) 
   271 apply (erule parts.induct, auto) 
   272 done
   273 
   274 lemma parts_insert_Nonce [simp]:
   275      "parts (insert (Nonce N) H) = insert (Nonce N) (parts H)"
   276 apply (rule parts_insert_eq_I) 
   277 apply (erule parts.induct, auto) 
   278 done
   279 
   280 lemma parts_insert_Number [simp]:
   281      "parts (insert (Number N) H) = insert (Number N) (parts H)"
   282 apply (rule parts_insert_eq_I) 
   283 apply (erule parts.induct, auto) 
   284 done
   285 
   286 lemma parts_insert_Key [simp]:
   287      "parts (insert (Key K) H) = insert (Key K) (parts H)"
   288 apply (rule parts_insert_eq_I) 
   289 apply (erule parts.induct, auto) 
   290 done
   291 
   292 lemma parts_insert_Hash [simp]:
   293      "parts (insert (Hash X) H) = insert (Hash X) (parts H)"
   294 apply (rule parts_insert_eq_I) 
   295 apply (erule parts.induct, auto) 
   296 done
   297 
   298 lemma parts_insert_Crypt [simp]:
   299      "parts (insert (Crypt K X) H) = insert (Crypt K X) (parts (insert X H))"
   300 apply (rule equalityI)
   301 apply (rule subsetI)
   302 apply (erule parts.induct, auto)
   303 apply (blast intro: parts.Body)
   304 done
   305 
   306 lemma parts_insert_MPair [simp]:
   307      "parts (insert {|X,Y|} H) =  
   308           insert {|X,Y|} (parts (insert X (insert Y H)))"
   309 apply (rule equalityI)
   310 apply (rule subsetI)
   311 apply (erule parts.induct, auto)
   312 apply (blast intro: parts.Fst parts.Snd)+
   313 done
   314 
   315 lemma parts_image_Key [simp]: "parts (Key`N) = Key`N"
   316 apply auto
   317 apply (erule parts.induct, auto)
   318 done
   319 
   320 
   321 text{*In any message, there is an upper bound N on its greatest nonce.*}
   322 lemma msg_Nonce_supply: "\<exists>N. \<forall>n. N\<le>n --> Nonce n \<notin> parts {msg}"
   323 apply (induct msg)
   324 apply (simp_all (no_asm_simp) add: exI parts_insert2)
   325  txt{*MPair case: blast works out the necessary sum itself!*}
   326  prefer 2 apply auto apply (blast elim!: add_leE)
   327 txt{*Nonce case*}
   328 apply (rule_tac x = "N + Suc nat" in exI, auto) 
   329 done
   330 
   331 
   332 subsection{*Inductive relation "analz"*}
   333 
   334 text{*Inductive definition of "analz" -- what can be broken down from a set of
   335     messages, including keys.  A form of downward closure.  Pairs can
   336     be taken apart; messages decrypted with known keys.  *}
   337 
   338 inductive_set
   339   analz :: "msg set => msg set"
   340   for H :: "msg set"
   341   where
   342     Inj [intro,simp] :    "X \<in> H ==> X \<in> analz H"
   343   | Fst:     "{|X,Y|} \<in> analz H ==> X \<in> analz H"
   344   | Snd:     "{|X,Y|} \<in> analz H ==> Y \<in> analz H"
   345   | Decrypt [dest]: 
   346              "[|Crypt K X \<in> analz H; Key(invKey K): analz H|] ==> X \<in> analz H"
   347 
   348 
   349 text{*Monotonicity; Lemma 1 of Lowe's paper*}
   350 lemma analz_mono: "G\<subseteq>H ==> analz(G) \<subseteq> analz(H)"
   351 apply auto
   352 apply (erule analz.induct) 
   353 apply (auto dest: analz.Fst analz.Snd) 
   354 done
   355 
   356 text{*Making it safe speeds up proofs*}
   357 lemma MPair_analz [elim!]:
   358      "[| {|X,Y|} \<in> analz H;        
   359              [| X \<in> analz H; Y \<in> analz H |] ==> P   
   360           |] ==> P"
   361 by (blast dest: analz.Fst analz.Snd)
   362 
   363 lemma analz_increasing: "H \<subseteq> analz(H)"
   364 by blast
   365 
   366 lemma analz_subset_parts: "analz H \<subseteq> parts H"
   367 apply (rule subsetI)
   368 apply (erule analz.induct, blast+)
   369 done
   370 
   371 lemmas analz_into_parts = analz_subset_parts [THEN subsetD, standard]
   372 
   373 lemmas not_parts_not_analz = analz_subset_parts [THEN contra_subsetD, standard]
   374 
   375 
   376 lemma parts_analz [simp]: "parts (analz H) = parts H"
   377 apply (rule equalityI)
   378 apply (rule analz_subset_parts [THEN parts_mono, THEN subset_trans], simp)
   379 apply (blast intro: analz_increasing [THEN parts_mono, THEN subsetD])
   380 done
   381 
   382 lemma analz_parts [simp]: "analz (parts H) = parts H"
   383 apply auto
   384 apply (erule analz.induct, auto)
   385 done
   386 
   387 lemmas analz_insertI = subset_insertI [THEN analz_mono, THEN [2] rev_subsetD, standard]
   388 
   389 subsubsection{*General equational properties *}
   390 
   391 lemma analz_empty [simp]: "analz{} = {}"
   392 apply safe
   393 apply (erule analz.induct, blast+)
   394 done
   395 
   396 text{*Converse fails: we can analz more from the union than from the 
   397   separate parts, as a key in one might decrypt a message in the other*}
   398 lemma analz_Un: "analz(G) \<union> analz(H) \<subseteq> analz(G \<union> H)"
   399 by (intro Un_least analz_mono Un_upper1 Un_upper2)
   400 
   401 lemma analz_insert: "insert X (analz H) \<subseteq> analz(insert X H)"
   402 by (blast intro: analz_mono [THEN [2] rev_subsetD])
   403 
   404 subsubsection{*Rewrite rules for pulling out atomic messages *}
   405 
   406 lemmas analz_insert_eq_I = equalityI [OF subsetI analz_insert]
   407 
   408 lemma analz_insert_Agent [simp]:
   409      "analz (insert (Agent agt) H) = insert (Agent agt) (analz H)"
   410 apply (rule analz_insert_eq_I) 
   411 apply (erule analz.induct, auto) 
   412 done
   413 
   414 lemma analz_insert_Nonce [simp]:
   415      "analz (insert (Nonce N) H) = insert (Nonce N) (analz H)"
   416 apply (rule analz_insert_eq_I) 
   417 apply (erule analz.induct, auto) 
   418 done
   419 
   420 lemma analz_insert_Number [simp]:
   421      "analz (insert (Number N) H) = insert (Number N) (analz H)"
   422 apply (rule analz_insert_eq_I) 
   423 apply (erule analz.induct, auto) 
   424 done
   425 
   426 lemma analz_insert_Hash [simp]:
   427      "analz (insert (Hash X) H) = insert (Hash X) (analz H)"
   428 apply (rule analz_insert_eq_I) 
   429 apply (erule analz.induct, auto) 
   430 done
   431 
   432 text{*Can only pull out Keys if they are not needed to decrypt the rest*}
   433 lemma analz_insert_Key [simp]: 
   434     "K \<notin> keysFor (analz H) ==>   
   435           analz (insert (Key K) H) = insert (Key K) (analz H)"
   436 apply (unfold keysFor_def)
   437 apply (rule analz_insert_eq_I) 
   438 apply (erule analz.induct, auto) 
   439 done
   440 
   441 lemma analz_insert_MPair [simp]:
   442      "analz (insert {|X,Y|} H) =  
   443           insert {|X,Y|} (analz (insert X (insert Y H)))"
   444 apply (rule equalityI)
   445 apply (rule subsetI)
   446 apply (erule analz.induct, auto)
   447 apply (erule analz.induct)
   448 apply (blast intro: analz.Fst analz.Snd)+
   449 done
   450 
   451 text{*Can pull out enCrypted message if the Key is not known*}
   452 lemma analz_insert_Crypt:
   453      "Key (invKey K) \<notin> analz H 
   454       ==> analz (insert (Crypt K X) H) = insert (Crypt K X) (analz H)"
   455 apply (rule analz_insert_eq_I) 
   456 apply (erule analz.induct, auto) 
   457 
   458 done
   459 
   460 lemma lemma1: "Key (invKey K) \<in> analz H ==>   
   461                analz (insert (Crypt K X) H) \<subseteq>  
   462                insert (Crypt K X) (analz (insert X H))"
   463 apply (rule subsetI)
   464 apply (erule_tac x = x in analz.induct, auto)
   465 done
   466 
   467 lemma lemma2: "Key (invKey K) \<in> analz H ==>   
   468                insert (Crypt K X) (analz (insert X H)) \<subseteq>  
   469                analz (insert (Crypt K X) H)"
   470 apply auto
   471 apply (erule_tac x = x in analz.induct, auto)
   472 apply (blast intro: analz_insertI analz.Decrypt)
   473 done
   474 
   475 lemma analz_insert_Decrypt:
   476      "Key (invKey K) \<in> analz H ==>   
   477                analz (insert (Crypt K X) H) =  
   478                insert (Crypt K X) (analz (insert X H))"
   479 by (intro equalityI lemma1 lemma2)
   480 
   481 text{*Case analysis: either the message is secure, or it is not! Effective,
   482 but can cause subgoals to blow up! Use with @{text "split_if"}; apparently
   483 @{text "split_tac"} does not cope with patterns such as @{term"analz (insert
   484 (Crypt K X) H)"} *} 
   485 lemma analz_Crypt_if [simp]:
   486      "analz (insert (Crypt K X) H) =                 
   487           (if (Key (invKey K) \<in> analz H)                 
   488            then insert (Crypt K X) (analz (insert X H))  
   489            else insert (Crypt K X) (analz H))"
   490 by (simp add: analz_insert_Crypt analz_insert_Decrypt)
   491 
   492 
   493 text{*This rule supposes "for the sake of argument" that we have the key.*}
   494 lemma analz_insert_Crypt_subset:
   495      "analz (insert (Crypt K X) H) \<subseteq>   
   496            insert (Crypt K X) (analz (insert X H))"
   497 apply (rule subsetI)
   498 apply (erule analz.induct, auto)
   499 done
   500 
   501 
   502 lemma analz_image_Key [simp]: "analz (Key`N) = Key`N"
   503 apply auto
   504 apply (erule analz.induct, auto)
   505 done
   506 
   507 
   508 subsubsection{*Idempotence and transitivity *}
   509 
   510 lemma analz_analzD [dest!]: "X\<in> analz (analz H) ==> X\<in> analz H"
   511 by (erule analz.induct, blast+)
   512 
   513 lemma analz_idem [simp]: "analz (analz H) = analz H"
   514 by blast
   515 
   516 lemma analz_subset_iff [simp]: "(analz G \<subseteq> analz H) = (G \<subseteq> analz H)"
   517 apply (rule iffI)
   518 apply (iprover intro: subset_trans analz_increasing)  
   519 apply (frule analz_mono, simp) 
   520 done
   521 
   522 lemma analz_trans: "[| X\<in> analz G;  G \<subseteq> analz H |] ==> X\<in> analz H"
   523 by (drule analz_mono, blast)
   524 
   525 text{*Cut; Lemma 2 of Lowe*}
   526 lemma analz_cut: "[| Y\<in> analz (insert X H);  X\<in> analz H |] ==> Y\<in> analz H"
   527 by (erule analz_trans, blast)
   528 
   529 (*Cut can be proved easily by induction on
   530    "Y: analz (insert X H) ==> X: analz H --> Y: analz H"
   531 *)
   532 
   533 text{*This rewrite rule helps in the simplification of messages that involve
   534   the forwarding of unknown components (X).  Without it, removing occurrences
   535   of X can be very complicated. *}
   536 lemma analz_insert_eq: "X\<in> analz H ==> analz (insert X H) = analz H"
   537 by (blast intro: analz_cut analz_insertI)
   538 
   539 
   540 text{*A congruence rule for "analz" *}
   541 
   542 lemma analz_subset_cong:
   543      "[| analz G \<subseteq> analz G'; analz H \<subseteq> analz H' |] 
   544       ==> analz (G \<union> H) \<subseteq> analz (G' \<union> H')"
   545 apply simp
   546 apply (iprover intro: conjI subset_trans analz_mono Un_upper1 Un_upper2) 
   547 done
   548 
   549 lemma analz_cong:
   550      "[| analz G = analz G'; analz H = analz H' |] 
   551       ==> analz (G \<union> H) = analz (G' \<union> H')"
   552 by (intro equalityI analz_subset_cong, simp_all) 
   553 
   554 lemma analz_insert_cong:
   555      "analz H = analz H' ==> analz(insert X H) = analz(insert X H')"
   556 by (force simp only: insert_def intro!: analz_cong)
   557 
   558 text{*If there are no pairs or encryptions then analz does nothing*}
   559 lemma analz_trivial:
   560      "[| \<forall>X Y. {|X,Y|} \<notin> H;  \<forall>X K. Crypt K X \<notin> H |] ==> analz H = H"
   561 apply safe
   562 apply (erule analz.induct, blast+)
   563 done
   564 
   565 text{*These two are obsolete (with a single Spy) but cost little to prove...*}
   566 lemma analz_UN_analz_lemma:
   567      "X\<in> analz (\<Union>i\<in>A. analz (H i)) ==> X\<in> analz (\<Union>i\<in>A. H i)"
   568 apply (erule analz.induct)
   569 apply (blast intro: analz_mono [THEN [2] rev_subsetD])+
   570 done
   571 
   572 lemma analz_UN_analz [simp]: "analz (\<Union>i\<in>A. analz (H i)) = analz (\<Union>i\<in>A. H i)"
   573 by (blast intro: analz_UN_analz_lemma analz_mono [THEN [2] rev_subsetD])
   574 
   575 
   576 subsection{*Inductive relation "synth"*}
   577 
   578 text{*Inductive definition of "synth" -- what can be built up from a set of
   579     messages.  A form of upward closure.  Pairs can be built, messages
   580     encrypted with known keys.  Agent names are public domain.
   581     Numbers can be guessed, but Nonces cannot be.  *}
   582 
   583 inductive_set
   584   synth :: "msg set => msg set"
   585   for H :: "msg set"
   586   where
   587     Inj    [intro]:   "X \<in> H ==> X \<in> synth H"
   588   | Agent  [intro]:   "Agent agt \<in> synth H"
   589   | Number [intro]:   "Number n  \<in> synth H"
   590   | Hash   [intro]:   "X \<in> synth H ==> Hash X \<in> synth H"
   591   | MPair  [intro]:   "[|X \<in> synth H;  Y \<in> synth H|] ==> {|X,Y|} \<in> synth H"
   592   | Crypt  [intro]:   "[|X \<in> synth H;  Key(K) \<in> H|] ==> Crypt K X \<in> synth H"
   593 
   594 text{*Monotonicity*}
   595 lemma synth_mono: "G\<subseteq>H ==> synth(G) \<subseteq> synth(H)"
   596   by (auto, erule synth.induct, auto)  
   597 
   598 text{*NO @{text Agent_synth}, as any Agent name can be synthesized.  
   599   The same holds for @{term Number}*}
   600 inductive_cases Nonce_synth [elim!]: "Nonce n \<in> synth H"
   601 inductive_cases Key_synth   [elim!]: "Key K \<in> synth H"
   602 inductive_cases Hash_synth  [elim!]: "Hash X \<in> synth H"
   603 inductive_cases MPair_synth [elim!]: "{|X,Y|} \<in> synth H"
   604 inductive_cases Crypt_synth [elim!]: "Crypt K X \<in> synth H"
   605 
   606 
   607 lemma synth_increasing: "H \<subseteq> synth(H)"
   608 by blast
   609 
   610 subsubsection{*Unions *}
   611 
   612 text{*Converse fails: we can synth more from the union than from the 
   613   separate parts, building a compound message using elements of each.*}
   614 lemma synth_Un: "synth(G) \<union> synth(H) \<subseteq> synth(G \<union> H)"
   615 by (intro Un_least synth_mono Un_upper1 Un_upper2)
   616 
   617 lemma synth_insert: "insert X (synth H) \<subseteq> synth(insert X H)"
   618 by (blast intro: synth_mono [THEN [2] rev_subsetD])
   619 
   620 subsubsection{*Idempotence and transitivity *}
   621 
   622 lemma synth_synthD [dest!]: "X\<in> synth (synth H) ==> X\<in> synth H"
   623 by (erule synth.induct, blast+)
   624 
   625 lemma synth_idem: "synth (synth H) = synth H"
   626 by blast
   627 
   628 lemma synth_subset_iff [simp]: "(synth G \<subseteq> synth H) = (G \<subseteq> synth H)"
   629 apply (rule iffI)
   630 apply (iprover intro: subset_trans synth_increasing)  
   631 apply (frule synth_mono, simp add: synth_idem) 
   632 done
   633 
   634 lemma synth_trans: "[| X\<in> synth G;  G \<subseteq> synth H |] ==> X\<in> synth H"
   635 by (drule synth_mono, blast)
   636 
   637 text{*Cut; Lemma 2 of Lowe*}
   638 lemma synth_cut: "[| Y\<in> synth (insert X H);  X\<in> synth H |] ==> Y\<in> synth H"
   639 by (erule synth_trans, blast)
   640 
   641 lemma Agent_synth [simp]: "Agent A \<in> synth H"
   642 by blast
   643 
   644 lemma Number_synth [simp]: "Number n \<in> synth H"
   645 by blast
   646 
   647 lemma Nonce_synth_eq [simp]: "(Nonce N \<in> synth H) = (Nonce N \<in> H)"
   648 by blast
   649 
   650 lemma Key_synth_eq [simp]: "(Key K \<in> synth H) = (Key K \<in> H)"
   651 by blast
   652 
   653 lemma Crypt_synth_eq [simp]:
   654      "Key K \<notin> H ==> (Crypt K X \<in> synth H) = (Crypt K X \<in> H)"
   655 by blast
   656 
   657 
   658 lemma keysFor_synth [simp]: 
   659     "keysFor (synth H) = keysFor H \<union> invKey`{K. Key K \<in> H}"
   660 by (unfold keysFor_def, blast)
   661 
   662 
   663 subsubsection{*Combinations of parts, analz and synth *}
   664 
   665 lemma parts_synth [simp]: "parts (synth H) = parts H \<union> synth H"
   666 apply (rule equalityI)
   667 apply (rule subsetI)
   668 apply (erule parts.induct)
   669 apply (blast intro: synth_increasing [THEN parts_mono, THEN subsetD] 
   670                     parts.Fst parts.Snd parts.Body)+
   671 done
   672 
   673 lemma analz_analz_Un [simp]: "analz (analz G \<union> H) = analz (G \<union> H)"
   674 apply (intro equalityI analz_subset_cong)+
   675 apply simp_all
   676 done
   677 
   678 lemma analz_synth_Un [simp]: "analz (synth G \<union> H) = analz (G \<union> H) \<union> synth G"
   679 apply (rule equalityI)
   680 apply (rule subsetI)
   681 apply (erule analz.induct)
   682 prefer 5 apply (blast intro: analz_mono [THEN [2] rev_subsetD])
   683 apply (blast intro: analz.Fst analz.Snd analz.Decrypt)+
   684 done
   685 
   686 lemma analz_synth [simp]: "analz (synth H) = analz H \<union> synth H"
   687 apply (cut_tac H = "{}" in analz_synth_Un)
   688 apply (simp (no_asm_use))
   689 done
   690 
   691 
   692 subsubsection{*For reasoning about the Fake rule in traces *}
   693 
   694 lemma parts_insert_subset_Un: "X\<in> G ==> parts(insert X H) \<subseteq> parts G \<union> parts H"
   695 by (rule subset_trans [OF parts_mono parts_Un_subset2], blast)
   696 
   697 text{*More specifically for Fake.  Very occasionally we could do with a version
   698   of the form  @{term"parts{X} \<subseteq> synth (analz H) \<union> parts H"} *}
   699 lemma Fake_parts_insert:
   700      "X \<in> synth (analz H) ==>  
   701       parts (insert X H) \<subseteq> synth (analz H) \<union> parts H"
   702 apply (drule parts_insert_subset_Un)
   703 apply (simp (no_asm_use))
   704 apply blast
   705 done
   706 
   707 lemma Fake_parts_insert_in_Un:
   708      "[|Z \<in> parts (insert X H);  X: synth (analz H)|] 
   709       ==> Z \<in>  synth (analz H) \<union> parts H";
   710 by (blast dest: Fake_parts_insert  [THEN subsetD, dest])
   711 
   712 text{*@{term H} is sometimes @{term"Key ` KK \<union> spies evs"}, so can't put 
   713   @{term "G=H"}.*}
   714 lemma Fake_analz_insert:
   715      "X\<in> synth (analz G) ==>  
   716       analz (insert X H) \<subseteq> synth (analz G) \<union> analz (G \<union> H)"
   717 apply (rule subsetI)
   718 apply (subgoal_tac "x \<in> analz (synth (analz G) \<union> H) ")
   719 prefer 2 apply (blast intro: analz_mono [THEN [2] rev_subsetD] analz_mono [THEN synth_mono, THEN [2] rev_subsetD])
   720 apply (simp (no_asm_use))
   721 apply blast
   722 done
   723 
   724 lemma analz_conj_parts [simp]:
   725      "(X \<in> analz H & X \<in> parts H) = (X \<in> analz H)"
   726 by (blast intro: analz_subset_parts [THEN subsetD])
   727 
   728 lemma analz_disj_parts [simp]:
   729      "(X \<in> analz H | X \<in> parts H) = (X \<in> parts H)"
   730 by (blast intro: analz_subset_parts [THEN subsetD])
   731 
   732 text{*Without this equation, other rules for synth and analz would yield
   733   redundant cases*}
   734 lemma MPair_synth_analz [iff]:
   735      "({|X,Y|} \<in> synth (analz H)) =  
   736       (X \<in> synth (analz H) & Y \<in> synth (analz H))"
   737 by blast
   738 
   739 lemma Crypt_synth_analz:
   740      "[| Key K \<in> analz H;  Key (invKey K) \<in> analz H |]  
   741        ==> (Crypt K X \<in> synth (analz H)) = (X \<in> synth (analz H))"
   742 by blast
   743 
   744 
   745 lemma Hash_synth_analz [simp]:
   746      "X \<notin> synth (analz H)  
   747       ==> (Hash{|X,Y|} \<in> synth (analz H)) = (Hash{|X,Y|} \<in> analz H)"
   748 by blast
   749 
   750 
   751 subsection{*HPair: a combination of Hash and MPair*}
   752 
   753 subsubsection{*Freeness *}
   754 
   755 lemma Agent_neq_HPair: "Agent A ~= Hash[X] Y"
   756 by (unfold HPair_def, simp)
   757 
   758 lemma Nonce_neq_HPair: "Nonce N ~= Hash[X] Y"
   759 by (unfold HPair_def, simp)
   760 
   761 lemma Number_neq_HPair: "Number N ~= Hash[X] Y"
   762 by (unfold HPair_def, simp)
   763 
   764 lemma Key_neq_HPair: "Key K ~= Hash[X] Y"
   765 by (unfold HPair_def, simp)
   766 
   767 lemma Hash_neq_HPair: "Hash Z ~= Hash[X] Y"
   768 by (unfold HPair_def, simp)
   769 
   770 lemma Crypt_neq_HPair: "Crypt K X' ~= Hash[X] Y"
   771 by (unfold HPair_def, simp)
   772 
   773 lemmas HPair_neqs = Agent_neq_HPair Nonce_neq_HPair Number_neq_HPair 
   774                     Key_neq_HPair Hash_neq_HPair Crypt_neq_HPair
   775 
   776 declare HPair_neqs [iff]
   777 declare HPair_neqs [symmetric, iff]
   778 
   779 lemma HPair_eq [iff]: "(Hash[X'] Y' = Hash[X] Y) = (X' = X & Y'=Y)"
   780 by (simp add: HPair_def)
   781 
   782 lemma MPair_eq_HPair [iff]:
   783      "({|X',Y'|} = Hash[X] Y) = (X' = Hash{|X,Y|} & Y'=Y)"
   784 by (simp add: HPair_def)
   785 
   786 lemma HPair_eq_MPair [iff]:
   787      "(Hash[X] Y = {|X',Y'|}) = (X' = Hash{|X,Y|} & Y'=Y)"
   788 by (auto simp add: HPair_def)
   789 
   790 
   791 subsubsection{*Specialized laws, proved in terms of those for Hash and MPair *}
   792 
   793 lemma keysFor_insert_HPair [simp]: "keysFor (insert (Hash[X] Y) H) = keysFor H"
   794 by (simp add: HPair_def)
   795 
   796 lemma parts_insert_HPair [simp]: 
   797     "parts (insert (Hash[X] Y) H) =  
   798      insert (Hash[X] Y) (insert (Hash{|X,Y|}) (parts (insert Y H)))"
   799 by (simp add: HPair_def)
   800 
   801 lemma analz_insert_HPair [simp]: 
   802     "analz (insert (Hash[X] Y) H) =  
   803      insert (Hash[X] Y) (insert (Hash{|X,Y|}) (analz (insert Y H)))"
   804 by (simp add: HPair_def)
   805 
   806 lemma HPair_synth_analz [simp]:
   807      "X \<notin> synth (analz H)  
   808     ==> (Hash[X] Y \<in> synth (analz H)) =  
   809         (Hash {|X, Y|} \<in> analz H & Y \<in> synth (analz H))"
   810 by (simp add: HPair_def)
   811 
   812 
   813 text{*We do NOT want Crypt... messages broken up in protocols!!*}
   814 declare parts.Body [rule del]
   815 
   816 
   817 text{*Rewrites to push in Key and Crypt messages, so that other messages can
   818     be pulled out using the @{text analz_insert} rules*}
   819 
   820 lemmas pushKeys [standard] =
   821   insert_commute [of "Key K" "Agent C"]
   822   insert_commute [of "Key K" "Nonce N"]
   823   insert_commute [of "Key K" "Number N"]
   824   insert_commute [of "Key K" "Hash X"]
   825   insert_commute [of "Key K" "MPair X Y"]
   826   insert_commute [of "Key K" "Crypt X K'"]
   827 
   828 lemmas pushCrypts [standard] =
   829   insert_commute [of "Crypt X K" "Agent C"]
   830   insert_commute [of "Crypt X K" "Agent C"]
   831   insert_commute [of "Crypt X K" "Nonce N"]
   832   insert_commute [of "Crypt X K" "Number N"]
   833   insert_commute [of "Crypt X K" "Hash X'"]
   834   insert_commute [of "Crypt X K" "MPair X' Y"]
   835 
   836 text{*Cannot be added with @{text "[simp]"} -- messages should not always be
   837   re-ordered. *}
   838 lemmas pushes = pushKeys pushCrypts
   839 
   840 
   841 subsection{*Tactics useful for many protocol proofs*}
   842 ML
   843 {*
   844 structure Message =
   845 struct
   846 
   847 (*Prove base case (subgoal i) and simplify others.  A typical base case
   848   concerns  Crypt K X \<notin> Key`shrK`bad  and cannot be proved by rewriting
   849   alone.*)
   850 fun prove_simple_subgoals_tac (cs, ss) i = 
   851     force_tac (cs, ss addsimps [@{thm image_eq_UN}]) i THEN
   852     ALLGOALS (asm_simp_tac ss)
   853 
   854 (*Analysis of Fake cases.  Also works for messages that forward unknown parts,
   855   but this application is no longer necessary if analz_insert_eq is used.
   856   Abstraction over i is ESSENTIAL: it delays the dereferencing of claset
   857   DEPENDS UPON "X" REFERRING TO THE FRADULENT MESSAGE *)
   858 
   859 fun impOfSubs th = th RSN (2, @{thm rev_subsetD})
   860 
   861 (*Apply rules to break down assumptions of the form
   862   Y \<in> parts(insert X H)  and  Y \<in> analz(insert X H)
   863 *)
   864 val Fake_insert_tac = 
   865     dresolve_tac [impOfSubs @{thm Fake_analz_insert},
   866                   impOfSubs @{thm Fake_parts_insert}] THEN'
   867     eresolve_tac [asm_rl, @{thm synth.Inj}];
   868 
   869 fun Fake_insert_simp_tac ss i = 
   870     REPEAT (Fake_insert_tac i) THEN asm_full_simp_tac ss i;
   871 
   872 fun atomic_spy_analz_tac (cs,ss) = SELECT_GOAL
   873     (Fake_insert_simp_tac ss 1
   874      THEN
   875      IF_UNSOLVED (Blast.depth_tac
   876 		  (cs addIs [@{thm analz_insertI},
   877 				   impOfSubs @{thm analz_subset_parts}]) 4 1))
   878 
   879 fun spy_analz_tac (cs,ss) i =
   880   DETERM
   881    (SELECT_GOAL
   882      (EVERY 
   883       [  (*push in occurrences of X...*)
   884        (REPEAT o CHANGED)
   885            (res_inst_tac (Simplifier.the_context ss) [(("x", 1), "X")] (insert_commute RS ssubst) 1),
   886        (*...allowing further simplifications*)
   887        simp_tac ss 1,
   888        REPEAT (FIRSTGOAL (resolve_tac [allI,impI,notI,conjI,iffI])),
   889        DEPTH_SOLVE (atomic_spy_analz_tac (cs,ss) 1)]) i)
   890 
   891 end
   892 *}
   893 
   894 text{*By default only @{text o_apply} is built-in.  But in the presence of
   895 eta-expansion this means that some terms displayed as @{term "f o g"} will be
   896 rewritten, and others will not!*}
   897 declare o_def [simp]
   898 
   899 
   900 lemma Crypt_notin_image_Key [simp]: "Crypt K X \<notin> Key ` A"
   901 by auto
   902 
   903 lemma Hash_notin_image_Key [simp] :"Hash X \<notin> Key ` A"
   904 by auto
   905 
   906 lemma synth_analz_mono: "G\<subseteq>H ==> synth (analz(G)) \<subseteq> synth (analz(H))"
   907 by (iprover intro: synth_mono analz_mono) 
   908 
   909 lemma Fake_analz_eq [simp]:
   910      "X \<in> synth(analz H) ==> synth (analz (insert X H)) = synth (analz H)"
   911 apply (drule Fake_analz_insert[of _ _ "H"])
   912 apply (simp add: synth_increasing[THEN Un_absorb2])
   913 apply (drule synth_mono)
   914 apply (simp add: synth_idem)
   915 apply (rule equalityI)
   916 apply (simp add: );
   917 apply (rule synth_analz_mono, blast)   
   918 done
   919 
   920 text{*Two generalizations of @{text analz_insert_eq}*}
   921 lemma gen_analz_insert_eq [rule_format]:
   922      "X \<in> analz H ==> ALL G. H \<subseteq> G --> analz (insert X G) = analz G";
   923 by (blast intro: analz_cut analz_insertI analz_mono [THEN [2] rev_subsetD])
   924 
   925 lemma synth_analz_insert_eq [rule_format]:
   926      "X \<in> synth (analz H) 
   927       ==> ALL G. H \<subseteq> G --> (Key K \<in> analz (insert X G)) = (Key K \<in> analz G)";
   928 apply (erule synth.induct) 
   929 apply (simp_all add: gen_analz_insert_eq subset_trans [OF _ subset_insertI]) 
   930 done
   931 
   932 lemma Fake_parts_sing:
   933      "X \<in> synth (analz H) ==> parts{X} \<subseteq> synth (analz H) \<union> parts H";
   934 apply (rule subset_trans) 
   935  apply (erule_tac [2] Fake_parts_insert)
   936 apply (rule parts_mono, blast)
   937 done
   938 
   939 lemmas Fake_parts_sing_imp_Un = Fake_parts_sing [THEN [2] rev_subsetD]
   940 
   941 method_setup spy_analz = {*
   942     Scan.succeed (SIMPLE_METHOD' o Message.spy_analz_tac o local_clasimpset_of) *}
   943     "for proving the Fake case when analz is involved"
   944 
   945 method_setup atomic_spy_analz = {*
   946     Scan.succeed (SIMPLE_METHOD' o Message.atomic_spy_analz_tac o local_clasimpset_of) *}
   947     "for debugging spy_analz"
   948 
   949 method_setup Fake_insert_simp = {*
   950     Scan.succeed (SIMPLE_METHOD' o Message.Fake_insert_simp_tac o local_simpset_of) *}
   951     "for debugging spy_analz"
   952 
   953 end