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