src/HOL/SET_Protocol/Message_SET.thy
author wenzelm
Sat Jul 18 22:58:50 2015 +0200 (2015-07-18)
changeset 60758 d8d85a8172b5
parent 59780 23b67731f4f0
child 61984 cdea44c775fa
permissions -rw-r--r--
isabelle update_cartouches;
     1 (*  Title:      HOL/SET_Protocol/Message_SET.thy
     2     Author:     Giampaolo Bella
     3     Author:     Fabio Massacci
     4     Author:     Lawrence C Paulson
     5 *)
     6 
     7 section{*The Message Theory, Modified for SET*}
     8 
     9 theory Message_SET
    10 imports Main "~~/src/HOL/Library/Nat_Bijection"
    11 begin
    12 
    13 subsection{*General Lemmas*}
    14 
    15 text{*Needed occasionally with @{text spy_analz_tac}, e.g. in
    16      @{text analz_insert_Key_newK}*}
    17 
    18 lemma Un_absorb3 [simp] : "A \<union> (B \<union> A) = B \<union> A"
    19 by blast
    20 
    21 text{*Collapses redundant cases in the huge protocol proofs*}
    22 lemmas disj_simps = disj_comms disj_left_absorb disj_assoc 
    23 
    24 text{*Effective with assumptions like @{term "K \<notin> range pubK"} and 
    25    @{term "K \<notin> invKey`range pubK"}*}
    26 lemma notin_image_iff: "(y \<notin> f`I) = (\<forall>i\<in>I. f i \<noteq> y)"
    27 by blast
    28 
    29 text{*Effective with the assumption @{term "KK \<subseteq> - (range(invKey o pubK))"} *}
    30 lemma disjoint_image_iff: "(A <= - (f`I)) = (\<forall>i\<in>I. f i \<notin> A)"
    31 by blast
    32 
    33 
    34 
    35 type_synonym key = nat
    36 
    37 consts
    38   all_symmetric :: bool        --{*true if all keys are symmetric*}
    39   invKey        :: "key=>key"  --{*inverse of a symmetric key*}
    40 
    41 specification (invKey)
    42   invKey [simp]: "invKey (invKey K) = K"
    43   invKey_symmetric: "all_symmetric --> invKey = id"
    44     by (rule exI [of _ id], auto)
    45 
    46 
    47 text{*The inverse of a symmetric key is itself; that of a public key
    48       is the private key and vice versa*}
    49 
    50 definition symKeys :: "key set" where
    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|}"      == "CONST MPair x y"
    80 
    81 
    82 definition nat_of_agent :: "agent => nat" where
    83    "nat_of_agent == case_agent (curry prod_encode 0)
    84                                (curry prod_encode 1)
    85                                (curry prod_encode 2)
    86                                (curry prod_encode 3)
    87                                (prod_encode (4,0))"
    88     --{*maps each agent to a unique natural number, for specifications*}
    89 
    90 text{*The function is indeed injective*}
    91 lemma inj_nat_of_agent: "inj nat_of_agent"
    92 by (simp add: nat_of_agent_def inj_on_def curry_def prod_encode_eq split: agent.split) 
    93 
    94 
    95 definition
    96   (*Keys useful to decrypt elements of a message set*)
    97   keysFor :: "msg set => key set"
    98   where "keysFor H = invKey ` {K. \<exists>X. Crypt K X \<in> H}"
    99 
   100 subsubsection{*Inductive definition of all "parts" of a message.*}
   101 
   102 inductive_set
   103   parts :: "msg set => msg set"
   104   for H :: "msg set"
   105   where
   106     Inj [intro]:               "X \<in> H ==> X \<in> parts H"
   107   | Fst:         "{|X,Y|}   \<in> parts H ==> X \<in> parts H"
   108   | Snd:         "{|X,Y|}   \<in> parts H ==> Y \<in> parts H"
   109   | Body:        "Crypt K X \<in> parts H ==> X \<in> parts H"
   110 
   111 
   112 (*Monotonicity*)
   113 lemma parts_mono: "G<=H ==> parts(G) <= parts(H)"
   114 apply auto
   115 apply (erule parts.induct)
   116 apply (auto dest: Fst Snd Body)
   117 done
   118 
   119 
   120 subsubsection{*Inverse of keys*}
   121 
   122 (*Equations hold because constructors are injective; cannot prove for all f*)
   123 lemma Key_image_eq [simp]: "(Key x \<in> Key`A) = (x\<in>A)"
   124 by auto
   125 
   126 lemma Nonce_Key_image_eq [simp]: "(Nonce x \<notin> Key`A)"
   127 by auto
   128 
   129 lemma Cardholder_image_eq [simp]: "(Cardholder x \<in> Cardholder`A) = (x \<in> A)"
   130 by auto
   131 
   132 lemma CA_image_eq [simp]: "(CA x \<in> CA`A) = (x \<in> A)"
   133 by auto
   134 
   135 lemma Pan_image_eq [simp]: "(Pan x \<in> Pan`A) = (x \<in> A)"
   136 by auto
   137 
   138 lemma Pan_Key_image_eq [simp]: "(Pan x \<notin> Key`A)"
   139 by auto
   140 
   141 lemma Nonce_Pan_image_eq [simp]: "(Nonce x \<notin> Pan`A)"
   142 by auto
   143 
   144 lemma invKey_eq [simp]: "(invKey K = invKey K') = (K=K')"
   145 apply safe
   146 apply (drule_tac f = invKey in arg_cong, simp)
   147 done
   148 
   149 
   150 subsection{*keysFor operator*}
   151 
   152 lemma keysFor_empty [simp]: "keysFor {} = {}"
   153 by (unfold keysFor_def, blast)
   154 
   155 lemma keysFor_Un [simp]: "keysFor (H \<union> H') = keysFor H \<union> keysFor H'"
   156 by (unfold keysFor_def, blast)
   157 
   158 lemma keysFor_UN [simp]: "keysFor (\<Union>i\<in>A. H i) = (\<Union>i\<in>A. keysFor (H i))"
   159 by (unfold keysFor_def, blast)
   160 
   161 (*Monotonicity*)
   162 lemma keysFor_mono: "G\<subseteq>H ==> keysFor(G) \<subseteq> keysFor(H)"
   163 by (unfold keysFor_def, blast)
   164 
   165 lemma keysFor_insert_Agent [simp]: "keysFor (insert (Agent A) H) = keysFor H"
   166 by (unfold keysFor_def, auto)
   167 
   168 lemma keysFor_insert_Nonce [simp]: "keysFor (insert (Nonce N) H) = keysFor H"
   169 by (unfold keysFor_def, auto)
   170 
   171 lemma keysFor_insert_Number [simp]: "keysFor (insert (Number N) H) = keysFor H"
   172 by (unfold keysFor_def, auto)
   173 
   174 lemma keysFor_insert_Key [simp]: "keysFor (insert (Key K) H) = keysFor H"
   175 by (unfold keysFor_def, auto)
   176 
   177 lemma keysFor_insert_Pan [simp]: "keysFor (insert (Pan A) H) = keysFor H"
   178 by (unfold keysFor_def, auto)
   179 
   180 lemma keysFor_insert_Hash [simp]: "keysFor (insert (Hash X) H) = keysFor H"
   181 by (unfold keysFor_def, auto)
   182 
   183 lemma keysFor_insert_MPair [simp]: "keysFor (insert {|X,Y|} H) = keysFor H"
   184 by (unfold keysFor_def, auto)
   185 
   186 lemma keysFor_insert_Crypt [simp]:
   187     "keysFor (insert (Crypt K X) H) = insert (invKey K) (keysFor H)"
   188 by (unfold keysFor_def, auto)
   189 
   190 lemma keysFor_image_Key [simp]: "keysFor (Key`E) = {}"
   191 by (unfold keysFor_def, auto)
   192 
   193 lemma Crypt_imp_invKey_keysFor: "Crypt K X \<in> H ==> invKey K \<in> keysFor H"
   194 by (unfold keysFor_def, blast)
   195 
   196 
   197 subsection{*Inductive relation "parts"*}
   198 
   199 lemma MPair_parts:
   200      "[| {|X,Y|} \<in> parts H;
   201          [| X \<in> parts H; Y \<in> parts H |] ==> P |] ==> P"
   202 by (blast dest: parts.Fst parts.Snd)
   203 
   204 declare MPair_parts [elim!]  parts.Body [dest!]
   205 text{*NB These two rules are UNSAFE in the formal sense, as they discard the
   206      compound message.  They work well on THIS FILE.
   207   @{text MPair_parts} is left as SAFE because it speeds up proofs.
   208   The Crypt rule is normally kept UNSAFE to avoid breaking up certificates.*}
   209 
   210 lemma parts_increasing: "H \<subseteq> parts(H)"
   211 by blast
   212 
   213 lemmas parts_insertI = subset_insertI [THEN parts_mono, THEN subsetD]
   214 
   215 lemma parts_empty [simp]: "parts{} = {}"
   216 apply safe
   217 apply (erule parts.induct, blast+)
   218 done
   219 
   220 lemma parts_emptyE [elim!]: "X\<in> parts{} ==> P"
   221 by simp
   222 
   223 (*WARNING: loops if H = {Y}, therefore must not be repeated!*)
   224 lemma parts_singleton: "X\<in> parts H ==> \<exists>Y\<in>H. X\<in> parts {Y}"
   225 by (erule parts.induct, fast+)
   226 
   227 
   228 subsubsection{*Unions*}
   229 
   230 lemma parts_Un_subset1: "parts(G) \<union> parts(H) \<subseteq> parts(G \<union> H)"
   231 by (intro Un_least parts_mono Un_upper1 Un_upper2)
   232 
   233 lemma parts_Un_subset2: "parts(G \<union> H) \<subseteq> parts(G) \<union> parts(H)"
   234 apply (rule subsetI)
   235 apply (erule parts.induct, blast+)
   236 done
   237 
   238 lemma parts_Un [simp]: "parts(G \<union> H) = parts(G) \<union> parts(H)"
   239 by (intro equalityI parts_Un_subset1 parts_Un_subset2)
   240 
   241 lemma parts_insert: "parts (insert X H) = parts {X} \<union> parts H"
   242 apply (subst insert_is_Un [of _ H])
   243 apply (simp only: parts_Un)
   244 done
   245 
   246 (*TWO inserts to avoid looping.  This rewrite is better than nothing.
   247   Not suitable for Addsimps: its behaviour can be strange.*)
   248 lemma parts_insert2:
   249      "parts (insert X (insert Y H)) = parts {X} \<union> parts {Y} \<union> parts H"
   250 apply (simp add: Un_assoc)
   251 apply (simp add: parts_insert [symmetric])
   252 done
   253 
   254 lemma parts_UN_subset1: "(\<Union>x\<in>A. parts(H x)) \<subseteq> parts(\<Union>x\<in>A. H x)"
   255 by (intro UN_least parts_mono UN_upper)
   256 
   257 lemma parts_UN_subset2: "parts(\<Union>x\<in>A. H x) \<subseteq> (\<Union>x\<in>A. parts(H x))"
   258 apply (rule subsetI)
   259 apply (erule parts.induct, blast+)
   260 done
   261 
   262 lemma parts_UN [simp]: "parts(\<Union>x\<in>A. H x) = (\<Union>x\<in>A. parts(H x))"
   263 by (intro equalityI parts_UN_subset1 parts_UN_subset2)
   264 
   265 (*Added to simplify arguments to parts, analz and synth.
   266   NOTE: the UN versions are no longer used!*)
   267 
   268 
   269 text{*This allows @{text blast} to simplify occurrences of
   270   @{term "parts(G\<union>H)"} in the assumption.*}
   271 declare parts_Un [THEN equalityD1, THEN subsetD, THEN UnE, elim!]
   272 
   273 
   274 lemma parts_insert_subset: "insert X (parts H) \<subseteq> parts(insert X H)"
   275 by (blast intro: parts_mono [THEN [2] rev_subsetD])
   276 
   277 subsubsection{*Idempotence and transitivity*}
   278 
   279 lemma parts_partsD [dest!]: "X\<in> parts (parts H) ==> X\<in> parts H"
   280 by (erule parts.induct, blast+)
   281 
   282 lemma parts_idem [simp]: "parts (parts H) = parts H"
   283 by blast
   284 
   285 lemma parts_trans: "[| X\<in> parts G;  G \<subseteq> parts H |] ==> X\<in> parts H"
   286 by (drule parts_mono, blast)
   287 
   288 (*Cut*)
   289 lemma parts_cut:
   290      "[| Y\<in> parts (insert X G);  X\<in> parts H |] ==> Y\<in> parts (G \<union> H)"
   291 by (erule parts_trans, auto)
   292 
   293 lemma parts_cut_eq [simp]: "X\<in> parts H ==> parts (insert X H) = parts H"
   294 by (force dest!: parts_cut intro: parts_insertI)
   295 
   296 
   297 subsubsection{*Rewrite rules for pulling out atomic messages*}
   298 
   299 lemmas parts_insert_eq_I = equalityI [OF subsetI parts_insert_subset]
   300 
   301 
   302 lemma parts_insert_Agent [simp]:
   303      "parts (insert (Agent agt) H) = insert (Agent agt) (parts H)"
   304 apply (rule parts_insert_eq_I)
   305 apply (erule parts.induct, auto)
   306 done
   307 
   308 lemma parts_insert_Nonce [simp]:
   309      "parts (insert (Nonce N) H) = insert (Nonce N) (parts H)"
   310 apply (rule parts_insert_eq_I)
   311 apply (erule parts.induct, auto)
   312 done
   313 
   314 lemma parts_insert_Number [simp]:
   315      "parts (insert (Number N) H) = insert (Number N) (parts H)"
   316 apply (rule parts_insert_eq_I)
   317 apply (erule parts.induct, auto)
   318 done
   319 
   320 lemma parts_insert_Key [simp]:
   321      "parts (insert (Key K) H) = insert (Key K) (parts H)"
   322 apply (rule parts_insert_eq_I)
   323 apply (erule parts.induct, auto)
   324 done
   325 
   326 lemma parts_insert_Pan [simp]:
   327      "parts (insert (Pan A) H) = insert (Pan A) (parts H)"
   328 apply (rule parts_insert_eq_I)
   329 apply (erule parts.induct, auto)
   330 done
   331 
   332 lemma parts_insert_Hash [simp]:
   333      "parts (insert (Hash X) H) = insert (Hash X) (parts H)"
   334 apply (rule parts_insert_eq_I)
   335 apply (erule parts.induct, auto)
   336 done
   337 
   338 lemma parts_insert_Crypt [simp]:
   339      "parts (insert (Crypt K X) H) =
   340           insert (Crypt K X) (parts (insert X H))"
   341 apply (rule equalityI)
   342 apply (rule subsetI)
   343 apply (erule parts.induct, auto)
   344 apply (erule parts.induct)
   345 apply (blast intro: parts.Body)+
   346 done
   347 
   348 lemma parts_insert_MPair [simp]:
   349      "parts (insert {|X,Y|} H) =
   350           insert {|X,Y|} (parts (insert X (insert Y H)))"
   351 apply (rule equalityI)
   352 apply (rule subsetI)
   353 apply (erule parts.induct, auto)
   354 apply (erule parts.induct)
   355 apply (blast intro: parts.Fst parts.Snd)+
   356 done
   357 
   358 lemma parts_image_Key [simp]: "parts (Key`N) = Key`N"
   359 apply auto
   360 apply (erule parts.induct, auto)
   361 done
   362 
   363 lemma parts_image_Pan [simp]: "parts (Pan`A) = Pan`A"
   364 apply auto
   365 apply (erule parts.induct, auto)
   366 done
   367 
   368 
   369 (*In any message, there is an upper bound N on its greatest nonce.*)
   370 lemma msg_Nonce_supply: "\<exists>N. \<forall>n. N\<le>n --> Nonce n \<notin> parts {msg}"
   371 apply (induct_tac "msg")
   372 apply (simp_all (no_asm_simp) add: exI parts_insert2)
   373 (*MPair case: blast_tac works out the necessary sum itself!*)
   374 prefer 2 apply (blast elim!: add_leE)
   375 (*Nonce case*)
   376 apply (rename_tac nat)
   377 apply (rule_tac x = "N + Suc nat" in exI)
   378 apply (auto elim!: add_leE)
   379 done
   380 
   381 (* Ditto, for numbers.*)
   382 lemma msg_Number_supply: "\<exists>N. \<forall>n. N<=n --> Number n \<notin> parts {msg}"
   383 apply (induct_tac "msg")
   384 apply (simp_all (no_asm_simp) add: exI parts_insert2)
   385 prefer 2 apply (blast elim!: add_leE)
   386 apply (rename_tac nat)
   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]
   430 
   431 lemmas not_parts_not_analz = analz_subset_parts [THEN contra_subsetD]
   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]
   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 =
   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   for K C N PAN X Y K'
   825 
   826 lemmas pushCrypts =
   827   insert_commute [of "Crypt X K" "Agent C"]
   828   insert_commute [of "Crypt X K" "Nonce N"]
   829   insert_commute [of "Crypt X K" "Number N"]
   830   insert_commute [of "Crypt X K" "Pan PAN"]
   831   insert_commute [of "Crypt X K" "Hash X'"]
   832   insert_commute [of "Crypt X K" "MPair X' Y"]
   833   for X K C N PAN X' Y
   834 
   835 text{*Cannot be added with @{text "[simp]"} -- messages should not always be
   836   re-ordered.*}
   837 lemmas pushes = pushKeys pushCrypts
   838 
   839 
   840 subsection{*Tactics useful for many protocol proofs*}
   841 (*<*)
   842 ML
   843 {*
   844 (*Analysis of Fake cases.  Also works for messages that forward unknown parts,
   845   but this application is no longer necessary if analz_insert_eq is used.
   846   DEPENDS UPON "X" REFERRING TO THE FRADULENT MESSAGE *)
   847 
   848 fun impOfSubs th = th RSN (2, @{thm rev_subsetD})
   849 
   850 (*Apply rules to break down assumptions of the form
   851   Y \<in> parts(insert X H)  and  Y \<in> analz(insert X H)
   852 *)
   853 fun Fake_insert_tac ctxt =
   854     dresolve_tac ctxt [impOfSubs @{thm Fake_analz_insert},
   855                   impOfSubs @{thm Fake_parts_insert}] THEN'
   856     eresolve_tac ctxt [asm_rl, @{thm synth.Inj}];
   857 
   858 fun Fake_insert_simp_tac ctxt i =
   859   REPEAT (Fake_insert_tac ctxt i) THEN asm_full_simp_tac ctxt i;
   860 
   861 fun atomic_spy_analz_tac ctxt =
   862   SELECT_GOAL
   863     (Fake_insert_simp_tac ctxt 1 THEN
   864       IF_UNSOLVED
   865         (Blast.depth_tac (ctxt addIs [@{thm analz_insertI},
   866             impOfSubs @{thm analz_subset_parts}]) 4 1));
   867 
   868 fun spy_analz_tac ctxt i =
   869   DETERM
   870    (SELECT_GOAL
   871      (EVERY
   872       [  (*push in occurrences of X...*)
   873        (REPEAT o CHANGED)
   874          (Rule_Insts.res_inst_tac ctxt [((("x", 1), Position.none), "X")] []
   875           (insert_commute RS ssubst) 1),
   876        (*...allowing further simplifications*)
   877        simp_tac ctxt 1,
   878        REPEAT (FIRSTGOAL (resolve_tac ctxt [allI,impI,notI,conjI,iffI])),
   879        DEPTH_SOLVE (atomic_spy_analz_tac ctxt 1)]) i);
   880 *}
   881 (*>*)
   882 
   883 
   884 (*By default only o_apply is built-in.  But in the presence of eta-expansion
   885   this means that some terms displayed as (f o g) will be rewritten, and others
   886   will not!*)
   887 declare o_def [simp]
   888 
   889 
   890 lemma Crypt_notin_image_Key [simp]: "Crypt K X \<notin> Key ` A"
   891 by auto
   892 
   893 lemma Hash_notin_image_Key [simp] :"Hash X \<notin> Key ` A"
   894 by auto
   895 
   896 lemma synth_analz_mono: "G<=H ==> synth (analz(G)) <= synth (analz(H))"
   897 by (simp add: synth_mono analz_mono)
   898 
   899 lemma Fake_analz_eq [simp]:
   900      "X \<in> synth(analz H) ==> synth (analz (insert X H)) = synth (analz H)"
   901 apply (drule Fake_analz_insert[of _ _ "H"])
   902 apply (simp add: synth_increasing[THEN Un_absorb2])
   903 apply (drule synth_mono)
   904 apply (simp add: synth_idem)
   905 apply (blast intro: synth_analz_mono [THEN [2] rev_subsetD])
   906 done
   907 
   908 text{*Two generalizations of @{text analz_insert_eq}*}
   909 lemma gen_analz_insert_eq [rule_format]:
   910      "X \<in> analz H ==> ALL G. H \<subseteq> G --> analz (insert X G) = analz G"
   911 by (blast intro: analz_cut analz_insertI analz_mono [THEN [2] rev_subsetD])
   912 
   913 lemma synth_analz_insert_eq [rule_format]:
   914      "X \<in> synth (analz H)
   915       ==> ALL G. H \<subseteq> G --> (Key K \<in> analz (insert X G)) = (Key K \<in> analz G)"
   916 apply (erule synth.induct)
   917 apply (simp_all add: gen_analz_insert_eq subset_trans [OF _ subset_insertI])
   918 done
   919 
   920 lemma Fake_parts_sing:
   921      "X \<in> synth (analz H) ==> parts{X} \<subseteq> synth (analz H) \<union> parts H"
   922 apply (rule subset_trans)
   923  apply (erule_tac [2] Fake_parts_insert)
   924 apply (simp add: parts_mono)
   925 done
   926 
   927 lemmas Fake_parts_sing_imp_Un = Fake_parts_sing [THEN [2] rev_subsetD]
   928 
   929 method_setup spy_analz = {*
   930     Scan.succeed (SIMPLE_METHOD' o spy_analz_tac) *}
   931     "for proving the Fake case when analz is involved"
   932 
   933 method_setup atomic_spy_analz = {*
   934     Scan.succeed (SIMPLE_METHOD' o atomic_spy_analz_tac) *}
   935     "for debugging spy_analz"
   936 
   937 method_setup Fake_insert_simp = {*
   938     Scan.succeed (SIMPLE_METHOD' o Fake_insert_simp_tac) *}
   939     "for debugging spy_analz"
   940 
   941 end