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