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