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