src/HOL/Auth/Message.thy
author wenzelm
Thu Feb 11 21:33:25 2010 +0100 (2010-02-11)
changeset 35109 0015a0a99ae9
parent 35054 a5db9779b026
child 35416 d8d7d1b785af
permissions -rw-r--r--
modernized syntax/translations;
     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|}"      == "CONST 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 by (metis insert_is_Un parts_Un)
   201 
   202 text{*TWO inserts to avoid looping.  This rewrite is better than nothing.
   203   Not suitable for Addsimps: its behaviour can be strange.*}
   204 lemma parts_insert2:
   205      "parts (insert X (insert Y H)) = parts {X} \<union> parts {Y} \<union> parts H"
   206 by (metis Un_commute Un_empty_right Un_insert_right insert_is_Un parts_Un)
   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 
   229 lemma parts_insert_subset: "insert X (parts H) \<subseteq> parts(insert X H)"
   230 by (blast intro: parts_mono [THEN [2] rev_subsetD])
   231 
   232 subsubsection{*Idempotence and transitivity *}
   233 
   234 lemma parts_partsD [dest!]: "X\<in> parts (parts H) ==> X\<in> parts H"
   235 by (erule parts.induct, blast+)
   236 
   237 lemma parts_idem [simp]: "parts (parts H) = parts H"
   238 by blast
   239 
   240 lemma parts_subset_iff [simp]: "(parts G \<subseteq> parts H) = (G \<subseteq> parts H)"
   241 by (metis equalityE parts_idem parts_increasing parts_mono subset_trans)
   242 
   243 lemma parts_trans: "[| X\<in> parts G;  G \<subseteq> parts H |] ==> X\<in> parts H"
   244 by (drule parts_mono, blast)
   245 
   246 text{*Cut*}
   247 lemma parts_cut:
   248      "[| Y\<in> parts (insert X G);  X\<in> parts H |] ==> Y\<in> parts (G \<union> H)" 
   249 by (blast intro: parts_trans) 
   250 
   251 
   252 lemma parts_cut_eq [simp]: "X\<in> parts H ==> parts (insert X H) = parts H"
   253 by (force dest!: parts_cut intro: parts_insertI)
   254 
   255 
   256 subsubsection{*Rewrite rules for pulling out atomic messages *}
   257 
   258 lemmas parts_insert_eq_I = equalityI [OF subsetI parts_insert_subset]
   259 
   260 
   261 lemma parts_insert_Agent [simp]:
   262      "parts (insert (Agent agt) H) = insert (Agent agt) (parts H)"
   263 apply (rule parts_insert_eq_I) 
   264 apply (erule parts.induct, auto) 
   265 done
   266 
   267 lemma parts_insert_Nonce [simp]:
   268      "parts (insert (Nonce N) H) = insert (Nonce N) (parts H)"
   269 apply (rule parts_insert_eq_I) 
   270 apply (erule parts.induct, auto) 
   271 done
   272 
   273 lemma parts_insert_Number [simp]:
   274      "parts (insert (Number N) H) = insert (Number N) (parts H)"
   275 apply (rule parts_insert_eq_I) 
   276 apply (erule parts.induct, auto) 
   277 done
   278 
   279 lemma parts_insert_Key [simp]:
   280      "parts (insert (Key K) H) = insert (Key K) (parts H)"
   281 apply (rule parts_insert_eq_I) 
   282 apply (erule parts.induct, auto) 
   283 done
   284 
   285 lemma parts_insert_Hash [simp]:
   286      "parts (insert (Hash X) H) = insert (Hash X) (parts H)"
   287 apply (rule parts_insert_eq_I) 
   288 apply (erule parts.induct, auto) 
   289 done
   290 
   291 lemma parts_insert_Crypt [simp]:
   292      "parts (insert (Crypt K X) H) = insert (Crypt K X) (parts (insert X H))"
   293 apply (rule equalityI)
   294 apply (rule subsetI)
   295 apply (erule parts.induct, auto)
   296 apply (blast intro: parts.Body)
   297 done
   298 
   299 lemma parts_insert_MPair [simp]:
   300      "parts (insert {|X,Y|} H) =  
   301           insert {|X,Y|} (parts (insert X (insert Y H)))"
   302 apply (rule equalityI)
   303 apply (rule subsetI)
   304 apply (erule parts.induct, auto)
   305 apply (blast intro: parts.Fst parts.Snd)+
   306 done
   307 
   308 lemma parts_image_Key [simp]: "parts (Key`N) = Key`N"
   309 apply auto
   310 apply (erule parts.induct, auto)
   311 done
   312 
   313 
   314 text{*In any message, there is an upper bound N on its greatest nonce.*}
   315 lemma msg_Nonce_supply: "\<exists>N. \<forall>n. N\<le>n --> Nonce n \<notin> parts {msg}"
   316 apply (induct msg)
   317 apply (simp_all (no_asm_simp) add: exI parts_insert2)
   318 txt{*Nonce case*}
   319 apply (metis Suc_n_not_le_n)
   320 txt{*MPair case: metis works out the necessary sum itself!*}
   321 apply (metis le_trans nat_le_linear)
   322 done
   323 
   324 
   325 subsection{*Inductive relation "analz"*}
   326 
   327 text{*Inductive definition of "analz" -- what can be broken down from a set of
   328     messages, including keys.  A form of downward closure.  Pairs can
   329     be taken apart; messages decrypted with known keys.  *}
   330 
   331 inductive_set
   332   analz :: "msg set => msg set"
   333   for H :: "msg set"
   334   where
   335     Inj [intro,simp] :    "X \<in> H ==> X \<in> analz H"
   336   | Fst:     "{|X,Y|} \<in> analz H ==> X \<in> analz H"
   337   | Snd:     "{|X,Y|} \<in> analz H ==> Y \<in> analz H"
   338   | Decrypt [dest]: 
   339              "[|Crypt K X \<in> analz H; Key(invKey K): analz H|] ==> X \<in> analz H"
   340 
   341 
   342 text{*Monotonicity; Lemma 1 of Lowe's paper*}
   343 lemma analz_mono: "G\<subseteq>H ==> analz(G) \<subseteq> analz(H)"
   344 apply auto
   345 apply (erule analz.induct) 
   346 apply (auto dest: analz.Fst analz.Snd) 
   347 done
   348 
   349 text{*Making it safe speeds up proofs*}
   350 lemma MPair_analz [elim!]:
   351      "[| {|X,Y|} \<in> analz H;        
   352              [| X \<in> analz H; Y \<in> analz H |] ==> P   
   353           |] ==> P"
   354 by (blast dest: analz.Fst analz.Snd)
   355 
   356 lemma analz_increasing: "H \<subseteq> analz(H)"
   357 by blast
   358 
   359 lemma analz_subset_parts: "analz H \<subseteq> parts H"
   360 apply (rule subsetI)
   361 apply (erule analz.induct, blast+)
   362 done
   363 
   364 lemmas analz_into_parts = analz_subset_parts [THEN subsetD, standard]
   365 
   366 lemmas not_parts_not_analz = analz_subset_parts [THEN contra_subsetD, standard]
   367 
   368 
   369 lemma parts_analz [simp]: "parts (analz H) = parts H"
   370 by (metis analz_increasing analz_subset_parts equalityI parts_mono parts_subset_iff)
   371 
   372 lemma analz_parts [simp]: "analz (parts H) = parts H"
   373 apply auto
   374 apply (erule analz.induct, auto)
   375 done
   376 
   377 lemmas analz_insertI = subset_insertI [THEN analz_mono, THEN [2] rev_subsetD, standard]
   378 
   379 subsubsection{*General equational properties *}
   380 
   381 lemma analz_empty [simp]: "analz{} = {}"
   382 apply safe
   383 apply (erule analz.induct, blast+)
   384 done
   385 
   386 text{*Converse fails: we can analz more from the union than from the 
   387   separate parts, as a key in one might decrypt a message in the other*}
   388 lemma analz_Un: "analz(G) \<union> analz(H) \<subseteq> analz(G \<union> H)"
   389 by (intro Un_least analz_mono Un_upper1 Un_upper2)
   390 
   391 lemma analz_insert: "insert X (analz H) \<subseteq> analz(insert X H)"
   392 by (blast intro: analz_mono [THEN [2] rev_subsetD])
   393 
   394 subsubsection{*Rewrite rules for pulling out atomic messages *}
   395 
   396 lemmas analz_insert_eq_I = equalityI [OF subsetI analz_insert]
   397 
   398 lemma analz_insert_Agent [simp]:
   399      "analz (insert (Agent agt) H) = insert (Agent agt) (analz H)"
   400 apply (rule analz_insert_eq_I) 
   401 apply (erule analz.induct, auto) 
   402 done
   403 
   404 lemma analz_insert_Nonce [simp]:
   405      "analz (insert (Nonce N) H) = insert (Nonce N) (analz H)"
   406 apply (rule analz_insert_eq_I) 
   407 apply (erule analz.induct, auto) 
   408 done
   409 
   410 lemma analz_insert_Number [simp]:
   411      "analz (insert (Number N) H) = insert (Number N) (analz H)"
   412 apply (rule analz_insert_eq_I) 
   413 apply (erule analz.induct, auto) 
   414 done
   415 
   416 lemma analz_insert_Hash [simp]:
   417      "analz (insert (Hash X) H) = insert (Hash X) (analz H)"
   418 apply (rule analz_insert_eq_I) 
   419 apply (erule analz.induct, auto) 
   420 done
   421 
   422 text{*Can only pull out Keys if they are not needed to decrypt the rest*}
   423 lemma analz_insert_Key [simp]: 
   424     "K \<notin> keysFor (analz H) ==>   
   425           analz (insert (Key K) H) = insert (Key K) (analz H)"
   426 apply (unfold keysFor_def)
   427 apply (rule analz_insert_eq_I) 
   428 apply (erule analz.induct, auto) 
   429 done
   430 
   431 lemma analz_insert_MPair [simp]:
   432      "analz (insert {|X,Y|} H) =  
   433           insert {|X,Y|} (analz (insert X (insert Y H)))"
   434 apply (rule equalityI)
   435 apply (rule subsetI)
   436 apply (erule analz.induct, auto)
   437 apply (erule analz.induct)
   438 apply (blast intro: analz.Fst analz.Snd)+
   439 done
   440 
   441 text{*Can pull out enCrypted message if the Key is not known*}
   442 lemma analz_insert_Crypt:
   443      "Key (invKey K) \<notin> analz H 
   444       ==> analz (insert (Crypt K X) H) = insert (Crypt K X) (analz H)"
   445 apply (rule analz_insert_eq_I) 
   446 apply (erule analz.induct, auto) 
   447 
   448 done
   449 
   450 lemma lemma1: "Key (invKey K) \<in> analz H ==>   
   451                analz (insert (Crypt K X) H) \<subseteq>  
   452                insert (Crypt K X) (analz (insert X H))"
   453 apply (rule subsetI)
   454 apply (erule_tac x = x in analz.induct, auto)
   455 done
   456 
   457 lemma lemma2: "Key (invKey K) \<in> analz H ==>   
   458                insert (Crypt K X) (analz (insert X H)) \<subseteq>  
   459                analz (insert (Crypt K X) H)"
   460 apply auto
   461 apply (erule_tac x = x in analz.induct, auto)
   462 apply (blast intro: analz_insertI analz.Decrypt)
   463 done
   464 
   465 lemma analz_insert_Decrypt:
   466      "Key (invKey K) \<in> analz H ==>   
   467                analz (insert (Crypt K X) H) =  
   468                insert (Crypt K X) (analz (insert X H))"
   469 by (intro equalityI lemma1 lemma2)
   470 
   471 text{*Case analysis: either the message is secure, or it is not! Effective,
   472 but can cause subgoals to blow up! Use with @{text "split_if"}; apparently
   473 @{text "split_tac"} does not cope with patterns such as @{term"analz (insert
   474 (Crypt K X) H)"} *} 
   475 lemma analz_Crypt_if [simp]:
   476      "analz (insert (Crypt K X) H) =                 
   477           (if (Key (invKey K) \<in> analz H)                 
   478            then insert (Crypt K X) (analz (insert X H))  
   479            else insert (Crypt K X) (analz H))"
   480 by (simp add: analz_insert_Crypt analz_insert_Decrypt)
   481 
   482 
   483 text{*This rule supposes "for the sake of argument" that we have the key.*}
   484 lemma analz_insert_Crypt_subset:
   485      "analz (insert (Crypt K X) H) \<subseteq>   
   486            insert (Crypt K X) (analz (insert X H))"
   487 apply (rule subsetI)
   488 apply (erule analz.induct, auto)
   489 done
   490 
   491 
   492 lemma analz_image_Key [simp]: "analz (Key`N) = Key`N"
   493 apply auto
   494 apply (erule analz.induct, auto)
   495 done
   496 
   497 
   498 subsubsection{*Idempotence and transitivity *}
   499 
   500 lemma analz_analzD [dest!]: "X\<in> analz (analz H) ==> X\<in> analz H"
   501 by (erule analz.induct, blast+)
   502 
   503 lemma analz_idem [simp]: "analz (analz H) = analz H"
   504 by blast
   505 
   506 lemma analz_subset_iff [simp]: "(analz G \<subseteq> analz H) = (G \<subseteq> analz H)"
   507 by (metis analz_idem analz_increasing analz_mono subset_trans)
   508 
   509 lemma analz_trans: "[| X\<in> analz G;  G \<subseteq> analz H |] ==> X\<in> analz H"
   510 by (drule analz_mono, blast)
   511 
   512 text{*Cut; Lemma 2 of Lowe*}
   513 lemma analz_cut: "[| Y\<in> analz (insert X H);  X\<in> analz H |] ==> Y\<in> analz H"
   514 by (erule analz_trans, blast)
   515 
   516 (*Cut can be proved easily by induction on
   517    "Y: analz (insert X H) ==> X: analz H --> Y: analz H"
   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 (iprover intro: conjI subset_trans analz_mono Un_upper1 Un_upper2) 
   534 done
   535 
   536 lemma analz_cong:
   537      "[| analz G = analz G'; analz H = analz H' |] 
   538       ==> analz (G \<union> H) = analz (G' \<union> H')"
   539 by (intro equalityI analz_subset_cong, simp_all) 
   540 
   541 lemma analz_insert_cong:
   542      "analz H = analz H' ==> analz(insert X H) = analz(insert X H')"
   543 by (force simp only: insert_def intro!: analz_cong)
   544 
   545 text{*If there are no pairs or encryptions then analz does nothing*}
   546 lemma analz_trivial:
   547      "[| \<forall>X Y. {|X,Y|} \<notin> H;  \<forall>X K. Crypt K X \<notin> H |] ==> analz H = H"
   548 apply safe
   549 apply (erule analz.induct, blast+)
   550 done
   551 
   552 text{*These two are obsolete (with a single Spy) but cost little to prove...*}
   553 lemma analz_UN_analz_lemma:
   554      "X\<in> analz (\<Union>i\<in>A. analz (H i)) ==> X\<in> analz (\<Union>i\<in>A. H i)"
   555 apply (erule analz.induct)
   556 apply (blast intro: analz_mono [THEN [2] rev_subsetD])+
   557 done
   558 
   559 lemma analz_UN_analz [simp]: "analz (\<Union>i\<in>A. analz (H i)) = analz (\<Union>i\<in>A. H i)"
   560 by (blast intro: analz_UN_analz_lemma analz_mono [THEN [2] rev_subsetD])
   561 
   562 
   563 subsection{*Inductive relation "synth"*}
   564 
   565 text{*Inductive definition of "synth" -- what can be built up from a set of
   566     messages.  A form of upward closure.  Pairs can be built, messages
   567     encrypted with known keys.  Agent names are public domain.
   568     Numbers can be guessed, but Nonces cannot be.  *}
   569 
   570 inductive_set
   571   synth :: "msg set => msg set"
   572   for H :: "msg set"
   573   where
   574     Inj    [intro]:   "X \<in> H ==> X \<in> synth H"
   575   | Agent  [intro]:   "Agent agt \<in> synth H"
   576   | Number [intro]:   "Number n  \<in> synth H"
   577   | Hash   [intro]:   "X \<in> synth H ==> Hash X \<in> synth H"
   578   | MPair  [intro]:   "[|X \<in> synth H;  Y \<in> synth H|] ==> {|X,Y|} \<in> synth H"
   579   | Crypt  [intro]:   "[|X \<in> synth H;  Key(K) \<in> H|] ==> Crypt K X \<in> synth H"
   580 
   581 text{*Monotonicity*}
   582 lemma synth_mono: "G\<subseteq>H ==> synth(G) \<subseteq> synth(H)"
   583   by (auto, erule synth.induct, auto)  
   584 
   585 text{*NO @{text Agent_synth}, as any Agent name can be synthesized.  
   586   The same holds for @{term Number}*}
   587 inductive_cases Nonce_synth [elim!]: "Nonce n \<in> synth H"
   588 inductive_cases Key_synth   [elim!]: "Key K \<in> synth H"
   589 inductive_cases Hash_synth  [elim!]: "Hash X \<in> synth H"
   590 inductive_cases MPair_synth [elim!]: "{|X,Y|} \<in> synth H"
   591 inductive_cases Crypt_synth [elim!]: "Crypt K X \<in> synth H"
   592 
   593 
   594 lemma synth_increasing: "H \<subseteq> synth(H)"
   595 by blast
   596 
   597 subsubsection{*Unions *}
   598 
   599 text{*Converse fails: we can synth more from the union than from the 
   600   separate parts, building a compound message using elements of each.*}
   601 lemma synth_Un: "synth(G) \<union> synth(H) \<subseteq> synth(G \<union> H)"
   602 by (intro Un_least synth_mono Un_upper1 Un_upper2)
   603 
   604 lemma synth_insert: "insert X (synth H) \<subseteq> synth(insert X H)"
   605 by (blast intro: synth_mono [THEN [2] rev_subsetD])
   606 
   607 subsubsection{*Idempotence and transitivity *}
   608 
   609 lemma synth_synthD [dest!]: "X\<in> synth (synth H) ==> X\<in> synth H"
   610 by (erule synth.induct, blast+)
   611 
   612 lemma synth_idem: "synth (synth H) = synth H"
   613 by blast
   614 
   615 lemma synth_subset_iff [simp]: "(synth G \<subseteq> synth H) = (G \<subseteq> synth H)"
   616 by (metis equalityE subset_trans synth_idem synth_increasing synth_mono)
   617 
   618 lemma synth_trans: "[| X\<in> synth G;  G \<subseteq> synth H |] ==> X\<in> synth H"
   619 by (drule synth_mono, blast)
   620 
   621 text{*Cut; Lemma 2 of Lowe*}
   622 lemma synth_cut: "[| Y\<in> synth (insert X H);  X\<in> synth H |] ==> Y\<in> synth H"
   623 by (erule synth_trans, blast)
   624 
   625 lemma Agent_synth [simp]: "Agent A \<in> synth H"
   626 by blast
   627 
   628 lemma Number_synth [simp]: "Number n \<in> synth H"
   629 by blast
   630 
   631 lemma Nonce_synth_eq [simp]: "(Nonce N \<in> synth H) = (Nonce N \<in> H)"
   632 by blast
   633 
   634 lemma Key_synth_eq [simp]: "(Key K \<in> synth H) = (Key K \<in> H)"
   635 by blast
   636 
   637 lemma Crypt_synth_eq [simp]:
   638      "Key K \<notin> H ==> (Crypt K X \<in> synth H) = (Crypt K X \<in> H)"
   639 by blast
   640 
   641 
   642 lemma keysFor_synth [simp]: 
   643     "keysFor (synth H) = keysFor H \<union> invKey`{K. Key K \<in> H}"
   644 by (unfold keysFor_def, blast)
   645 
   646 
   647 subsubsection{*Combinations of parts, analz and synth *}
   648 
   649 lemma parts_synth [simp]: "parts (synth H) = parts H \<union> synth H"
   650 apply (rule equalityI)
   651 apply (rule subsetI)
   652 apply (erule parts.induct)
   653 apply (blast intro: synth_increasing [THEN parts_mono, THEN subsetD] 
   654                     parts.Fst parts.Snd parts.Body)+
   655 done
   656 
   657 lemma analz_analz_Un [simp]: "analz (analz G \<union> H) = analz (G \<union> H)"
   658 apply (intro equalityI analz_subset_cong)+
   659 apply simp_all
   660 done
   661 
   662 lemma analz_synth_Un [simp]: "analz (synth G \<union> H) = analz (G \<union> H) \<union> synth G"
   663 apply (rule equalityI)
   664 apply (rule subsetI)
   665 apply (erule analz.induct)
   666 prefer 5 apply (blast intro: analz_mono [THEN [2] rev_subsetD])
   667 apply (blast intro: analz.Fst analz.Snd analz.Decrypt)+
   668 done
   669 
   670 lemma analz_synth [simp]: "analz (synth H) = analz H \<union> synth H"
   671 by (metis Un_empty_right analz_synth_Un)
   672 
   673 
   674 subsubsection{*For reasoning about the Fake rule in traces *}
   675 
   676 lemma parts_insert_subset_Un: "X\<in> G ==> parts(insert X H) \<subseteq> parts G \<union> parts H"
   677 by (metis UnCI Un_upper2 insert_subset parts_Un parts_mono)
   678 
   679 text{*More specifically for Fake.  Very occasionally we could do with a version
   680   of the form  @{term"parts{X} \<subseteq> synth (analz H) \<union> parts H"} *}
   681 lemma Fake_parts_insert:
   682      "X \<in> synth (analz H) ==>  
   683       parts (insert X H) \<subseteq> synth (analz H) \<union> parts H"
   684 by (metis Un_commute analz_increasing insert_subset parts_analz parts_mono 
   685           parts_synth synth_mono synth_subset_iff)
   686 
   687 lemma Fake_parts_insert_in_Un:
   688      "[|Z \<in> parts (insert X H);  X: synth (analz H)|] 
   689       ==> Z \<in>  synth (analz H) \<union> parts H"
   690 by (metis Fake_parts_insert set_mp)
   691 
   692 text{*@{term H} is sometimes @{term"Key ` KK \<union> spies evs"}, so can't put 
   693   @{term "G=H"}.*}
   694 lemma Fake_analz_insert:
   695      "X\<in> synth (analz G) ==>  
   696       analz (insert X H) \<subseteq> synth (analz G) \<union> analz (G \<union> H)"
   697 apply (rule subsetI)
   698 apply (subgoal_tac "x \<in> analz (synth (analz G) \<union> H)", force)
   699 apply (blast intro: analz_mono [THEN [2] rev_subsetD] analz_mono [THEN synth_mono, THEN [2] rev_subsetD])
   700 done
   701 
   702 lemma analz_conj_parts [simp]:
   703      "(X \<in> analz H & X \<in> parts H) = (X \<in> analz H)"
   704 by (blast intro: analz_subset_parts [THEN subsetD])
   705 
   706 lemma analz_disj_parts [simp]:
   707      "(X \<in> analz H | X \<in> parts H) = (X \<in> parts H)"
   708 by (blast intro: analz_subset_parts [THEN subsetD])
   709 
   710 text{*Without this equation, other rules for synth and analz would yield
   711   redundant cases*}
   712 lemma MPair_synth_analz [iff]:
   713      "({|X,Y|} \<in> synth (analz H)) =  
   714       (X \<in> synth (analz H) & Y \<in> synth (analz H))"
   715 by blast
   716 
   717 lemma Crypt_synth_analz:
   718      "[| Key K \<in> analz H;  Key (invKey K) \<in> analz H |]  
   719        ==> (Crypt K X \<in> synth (analz H)) = (X \<in> synth (analz H))"
   720 by blast
   721 
   722 
   723 lemma Hash_synth_analz [simp]:
   724      "X \<notin> synth (analz H)  
   725       ==> (Hash{|X,Y|} \<in> synth (analz H)) = (Hash{|X,Y|} \<in> analz H)"
   726 by blast
   727 
   728 
   729 subsection{*HPair: a combination of Hash and MPair*}
   730 
   731 subsubsection{*Freeness *}
   732 
   733 lemma Agent_neq_HPair: "Agent A ~= Hash[X] Y"
   734 by (unfold HPair_def, simp)
   735 
   736 lemma Nonce_neq_HPair: "Nonce N ~= Hash[X] Y"
   737 by (unfold HPair_def, simp)
   738 
   739 lemma Number_neq_HPair: "Number N ~= Hash[X] Y"
   740 by (unfold HPair_def, simp)
   741 
   742 lemma Key_neq_HPair: "Key K ~= Hash[X] Y"
   743 by (unfold HPair_def, simp)
   744 
   745 lemma Hash_neq_HPair: "Hash Z ~= Hash[X] Y"
   746 by (unfold HPair_def, simp)
   747 
   748 lemma Crypt_neq_HPair: "Crypt K X' ~= Hash[X] Y"
   749 by (unfold HPair_def, simp)
   750 
   751 lemmas HPair_neqs = Agent_neq_HPair Nonce_neq_HPair Number_neq_HPair 
   752                     Key_neq_HPair Hash_neq_HPair Crypt_neq_HPair
   753 
   754 declare HPair_neqs [iff]
   755 declare HPair_neqs [symmetric, iff]
   756 
   757 lemma HPair_eq [iff]: "(Hash[X'] Y' = Hash[X] Y) = (X' = X & Y'=Y)"
   758 by (simp add: HPair_def)
   759 
   760 lemma MPair_eq_HPair [iff]:
   761      "({|X',Y'|} = Hash[X] Y) = (X' = Hash{|X,Y|} & Y'=Y)"
   762 by (simp add: HPair_def)
   763 
   764 lemma HPair_eq_MPair [iff]:
   765      "(Hash[X] Y = {|X',Y'|}) = (X' = Hash{|X,Y|} & Y'=Y)"
   766 by (auto simp add: HPair_def)
   767 
   768 
   769 subsubsection{*Specialized laws, proved in terms of those for Hash and MPair *}
   770 
   771 lemma keysFor_insert_HPair [simp]: "keysFor (insert (Hash[X] Y) H) = keysFor H"
   772 by (simp add: HPair_def)
   773 
   774 lemma parts_insert_HPair [simp]: 
   775     "parts (insert (Hash[X] Y) H) =  
   776      insert (Hash[X] Y) (insert (Hash{|X,Y|}) (parts (insert Y H)))"
   777 by (simp add: HPair_def)
   778 
   779 lemma analz_insert_HPair [simp]: 
   780     "analz (insert (Hash[X] Y) H) =  
   781      insert (Hash[X] Y) (insert (Hash{|X,Y|}) (analz (insert Y H)))"
   782 by (simp add: HPair_def)
   783 
   784 lemma HPair_synth_analz [simp]:
   785      "X \<notin> synth (analz H)  
   786     ==> (Hash[X] Y \<in> synth (analz H)) =  
   787         (Hash {|X, Y|} \<in> analz H & Y \<in> synth (analz H))"
   788 by (simp add: HPair_def)
   789 
   790 
   791 text{*We do NOT want Crypt... messages broken up in protocols!!*}
   792 declare parts.Body [rule del]
   793 
   794 
   795 text{*Rewrites to push in Key and Crypt messages, so that other messages can
   796     be pulled out using the @{text analz_insert} rules*}
   797 
   798 lemmas pushKeys [standard] =
   799   insert_commute [of "Key K" "Agent C"]
   800   insert_commute [of "Key K" "Nonce N"]
   801   insert_commute [of "Key K" "Number N"]
   802   insert_commute [of "Key K" "Hash X"]
   803   insert_commute [of "Key K" "MPair X Y"]
   804   insert_commute [of "Key K" "Crypt X K'"]
   805 
   806 lemmas pushCrypts [standard] =
   807   insert_commute [of "Crypt X K" "Agent C"]
   808   insert_commute [of "Crypt X K" "Agent C"]
   809   insert_commute [of "Crypt X K" "Nonce N"]
   810   insert_commute [of "Crypt X K" "Number N"]
   811   insert_commute [of "Crypt X K" "Hash X'"]
   812   insert_commute [of "Crypt X K" "MPair X' Y"]
   813 
   814 text{*Cannot be added with @{text "[simp]"} -- messages should not always be
   815   re-ordered. *}
   816 lemmas pushes = pushKeys pushCrypts
   817 
   818 
   819 subsection{*Tactics useful for many protocol proofs*}
   820 ML
   821 {*
   822 structure Message =
   823 struct
   824 
   825 (*Prove base case (subgoal i) and simplify others.  A typical base case
   826   concerns  Crypt K X \<notin> Key`shrK`bad  and cannot be proved by rewriting
   827   alone.*)
   828 fun prove_simple_subgoals_tac (cs, ss) i = 
   829     force_tac (cs, ss addsimps [@{thm image_eq_UN}]) i THEN
   830     ALLGOALS (asm_simp_tac ss)
   831 
   832 (*Analysis of Fake cases.  Also works for messages that forward unknown parts,
   833   but this application is no longer necessary if analz_insert_eq is used.
   834   Abstraction over i is ESSENTIAL: it delays the dereferencing of claset
   835   DEPENDS UPON "X" REFERRING TO THE FRADULENT MESSAGE *)
   836 
   837 fun impOfSubs th = th RSN (2, @{thm rev_subsetD})
   838 
   839 (*Apply rules to break down assumptions of the form
   840   Y \<in> parts(insert X H)  and  Y \<in> analz(insert X H)
   841 *)
   842 val Fake_insert_tac = 
   843     dresolve_tac [impOfSubs @{thm Fake_analz_insert},
   844                   impOfSubs @{thm Fake_parts_insert}] THEN'
   845     eresolve_tac [asm_rl, @{thm synth.Inj}];
   846 
   847 fun Fake_insert_simp_tac ss i = 
   848     REPEAT (Fake_insert_tac i) THEN asm_full_simp_tac ss i;
   849 
   850 fun atomic_spy_analz_tac (cs,ss) = SELECT_GOAL
   851     (Fake_insert_simp_tac ss 1
   852      THEN
   853      IF_UNSOLVED (Blast.depth_tac
   854                   (cs addIs [@{thm analz_insertI},
   855                                    impOfSubs @{thm analz_subset_parts}]) 4 1))
   856 
   857 fun spy_analz_tac (cs,ss) i =
   858   DETERM
   859    (SELECT_GOAL
   860      (EVERY 
   861       [  (*push in occurrences of X...*)
   862        (REPEAT o CHANGED)
   863            (res_inst_tac (Simplifier.the_context ss) [(("x", 1), "X")] (insert_commute RS ssubst) 1),
   864        (*...allowing further simplifications*)
   865        simp_tac ss 1,
   866        REPEAT (FIRSTGOAL (resolve_tac [allI,impI,notI,conjI,iffI])),
   867        DEPTH_SOLVE (atomic_spy_analz_tac (cs,ss) 1)]) i)
   868 
   869 end
   870 *}
   871 
   872 text{*By default only @{text o_apply} is built-in.  But in the presence of
   873 eta-expansion this means that some terms displayed as @{term "f o g"} will be
   874 rewritten, and others will not!*}
   875 declare o_def [simp]
   876 
   877 
   878 lemma Crypt_notin_image_Key [simp]: "Crypt K X \<notin> Key ` A"
   879 by auto
   880 
   881 lemma Hash_notin_image_Key [simp] :"Hash X \<notin> Key ` A"
   882 by auto
   883 
   884 lemma synth_analz_mono: "G\<subseteq>H ==> synth (analz(G)) \<subseteq> synth (analz(H))"
   885 by (iprover intro: synth_mono analz_mono) 
   886 
   887 lemma Fake_analz_eq [simp]:
   888      "X \<in> synth(analz H) ==> synth (analz (insert X H)) = synth (analz H)"
   889 by (metis Fake_analz_insert Un_absorb Un_absorb1 Un_commute equalityI
   890           subset_insertI synth_analz_mono synth_increasing synth_subset_iff)
   891 
   892 text{*Two generalizations of @{text analz_insert_eq}*}
   893 lemma gen_analz_insert_eq [rule_format]:
   894      "X \<in> analz H ==> ALL G. H \<subseteq> G --> analz (insert X G) = analz G";
   895 by (blast intro: analz_cut analz_insertI analz_mono [THEN [2] rev_subsetD])
   896 
   897 lemma synth_analz_insert_eq [rule_format]:
   898      "X \<in> synth (analz H) 
   899       ==> ALL G. H \<subseteq> G --> (Key K \<in> analz (insert X G)) = (Key K \<in> analz G)";
   900 apply (erule synth.induct) 
   901 apply (simp_all add: gen_analz_insert_eq subset_trans [OF _ subset_insertI]) 
   902 done
   903 
   904 lemma Fake_parts_sing:
   905      "X \<in> synth (analz H) ==> parts{X} \<subseteq> synth (analz H) \<union> parts H"
   906 by (metis Fake_parts_insert empty_subsetI insert_mono parts_mono subset_trans)
   907 
   908 lemmas Fake_parts_sing_imp_Un = Fake_parts_sing [THEN [2] rev_subsetD]
   909 
   910 method_setup spy_analz = {*
   911     Scan.succeed (SIMPLE_METHOD' o Message.spy_analz_tac o clasimpset_of) *}
   912     "for proving the Fake case when analz is involved"
   913 
   914 method_setup atomic_spy_analz = {*
   915     Scan.succeed (SIMPLE_METHOD' o Message.atomic_spy_analz_tac o clasimpset_of) *}
   916     "for debugging spy_analz"
   917 
   918 method_setup Fake_insert_simp = {*
   919     Scan.succeed (SIMPLE_METHOD' o Message.Fake_insert_simp_tac o simpset_of) *}
   920     "for debugging spy_analz"
   921 
   922 end