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