src/HOL/Auth/Event.thy
author paulson
Wed Oct 26 16:31:53 2005 +0200 (2005-10-26)
changeset 17990 86d462f305e0
parent 16417 9bc16273c2d4
child 18570 ffce25f9aa7f
permissions -rw-r--r--
tidied away duplicate thm
     1 (*  Title:      HOL/Auth/Event
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1996  University of Cambridge
     5 
     6 Datatype of events; function "spies"; freshness
     7 
     8 "bad" agents have been broken by the Spy; their private keys and internal
     9     stores are visible to him
    10 *)
    11 
    12 header{*Theory of Events for Security Protocols*}
    13 
    14 theory Event imports Message begin
    15 
    16 consts  (*Initial states of agents -- parameter of the construction*)
    17   initState :: "agent => msg set"
    18 
    19 datatype
    20   event = Says  agent agent msg
    21         | Gets  agent       msg
    22         | Notes agent       msg
    23        
    24 consts 
    25   bad    :: "agent set"				(*compromised agents*)
    26   knows  :: "agent => event list => msg set"
    27 
    28 
    29 text{*The constant "spies" is retained for compatibility's sake*}
    30 syntax
    31   spies  :: "event list => msg set"
    32 
    33 translations
    34   "spies"   => "knows Spy"
    35 
    36 text{*Spy has access to his own key for spoof messages, but Server is secure*}
    37 specification (bad)
    38   Spy_in_bad     [iff]: "Spy \<in> bad"
    39   Server_not_bad [iff]: "Server \<notin> bad"
    40     by (rule exI [of _ "{Spy}"], simp)
    41 
    42 primrec
    43   knows_Nil:   "knows A [] = initState A"
    44   knows_Cons:
    45     "knows A (ev # evs) =
    46        (if A = Spy then 
    47 	(case ev of
    48 	   Says A' B X => insert X (knows Spy evs)
    49 	 | Gets A' X => knows Spy evs
    50 	 | Notes A' X  => 
    51 	     if A' \<in> bad then insert X (knows Spy evs) else knows Spy evs)
    52 	else
    53 	(case ev of
    54 	   Says A' B X => 
    55 	     if A'=A then insert X (knows A evs) else knows A evs
    56 	 | Gets A' X    => 
    57 	     if A'=A then insert X (knows A evs) else knows A evs
    58 	 | Notes A' X    => 
    59 	     if A'=A then insert X (knows A evs) else knows A evs))"
    60 
    61 (*
    62   Case A=Spy on the Gets event
    63   enforces the fact that if a message is received then it must have been sent,
    64   therefore the oops case must use Notes
    65 *)
    66 
    67 consts
    68   (*Set of items that might be visible to somebody:
    69     complement of the set of fresh items*)
    70   used :: "event list => msg set"
    71 
    72 primrec
    73   used_Nil:   "used []         = (UN B. parts (initState B))"
    74   used_Cons:  "used (ev # evs) =
    75 		     (case ev of
    76 			Says A B X => parts {X} \<union> used evs
    77 		      | Gets A X   => used evs
    78 		      | Notes A X  => parts {X} \<union> used evs)"
    79     --{*The case for @{term Gets} seems anomalous, but @{term Gets} always
    80         follows @{term Says} in real protocols.  Seems difficult to change.
    81         See @{text Gets_correct} in theory @{text "Guard/Extensions.thy"}. *}
    82 
    83 lemma Notes_imp_used [rule_format]: "Notes A X \<in> set evs --> X \<in> used evs"
    84 apply (induct_tac evs)
    85 apply (auto split: event.split) 
    86 done
    87 
    88 lemma Says_imp_used [rule_format]: "Says A B X \<in> set evs --> X \<in> used evs"
    89 apply (induct_tac evs)
    90 apply (auto split: event.split) 
    91 done
    92 
    93 
    94 subsection{*Function @{term knows}*}
    95 
    96 (*Simplifying   
    97  parts(insert X (knows Spy evs)) = parts{X} \<union> parts(knows Spy evs).
    98   This version won't loop with the simplifier.*)
    99 lemmas parts_insert_knows_A = parts_insert [of _ "knows A evs", standard]
   100 
   101 lemma knows_Spy_Says [simp]:
   102      "knows Spy (Says A B X # evs) = insert X (knows Spy evs)"
   103 by simp
   104 
   105 text{*Letting the Spy see "bad" agents' notes avoids redundant case-splits
   106       on whether @{term "A=Spy"} and whether @{term "A\<in>bad"}*}
   107 lemma knows_Spy_Notes [simp]:
   108      "knows Spy (Notes A X # evs) =  
   109           (if A:bad then insert X (knows Spy evs) else knows Spy evs)"
   110 by simp
   111 
   112 lemma knows_Spy_Gets [simp]: "knows Spy (Gets A X # evs) = knows Spy evs"
   113 by simp
   114 
   115 lemma knows_Spy_subset_knows_Spy_Says:
   116      "knows Spy evs \<subseteq> knows Spy (Says A B X # evs)"
   117 by (simp add: subset_insertI)
   118 
   119 lemma knows_Spy_subset_knows_Spy_Notes:
   120      "knows Spy evs \<subseteq> knows Spy (Notes A X # evs)"
   121 by force
   122 
   123 lemma knows_Spy_subset_knows_Spy_Gets:
   124      "knows Spy evs \<subseteq> knows Spy (Gets A X # evs)"
   125 by (simp add: subset_insertI)
   126 
   127 text{*Spy sees what is sent on the traffic*}
   128 lemma Says_imp_knows_Spy [rule_format]:
   129      "Says A B X \<in> set evs --> X \<in> knows Spy evs"
   130 apply (induct_tac "evs")
   131 apply (simp_all (no_asm_simp) split add: event.split)
   132 done
   133 
   134 lemma Notes_imp_knows_Spy [rule_format]:
   135      "Notes A X \<in> set evs --> A: bad --> X \<in> knows Spy evs"
   136 apply (induct_tac "evs")
   137 apply (simp_all (no_asm_simp) split add: event.split)
   138 done
   139 
   140 
   141 text{*Elimination rules: derive contradictions from old Says events containing
   142   items known to be fresh*}
   143 lemmas knows_Spy_partsEs =
   144      Says_imp_knows_Spy [THEN parts.Inj, THEN revcut_rl, standard] 
   145      parts.Body [THEN revcut_rl, standard]
   146 
   147 text{*Compatibility for the old "spies" function*}
   148 lemmas spies_partsEs = knows_Spy_partsEs
   149 lemmas Says_imp_spies = Says_imp_knows_Spy
   150 lemmas parts_insert_spies = parts_insert_knows_A [of _ Spy]
   151 
   152 
   153 subsection{*Knowledge of Agents*}
   154 
   155 lemma knows_Says: "knows A (Says A B X # evs) = insert X (knows A evs)"
   156 by simp
   157 
   158 lemma knows_Notes: "knows A (Notes A X # evs) = insert X (knows A evs)"
   159 by simp
   160 
   161 lemma knows_Gets:
   162      "A \<noteq> Spy --> knows A (Gets A X # evs) = insert X (knows A evs)"
   163 by simp
   164 
   165 
   166 lemma knows_subset_knows_Says: "knows A evs \<subseteq> knows A (Says A' B X # evs)"
   167 by (simp add: subset_insertI)
   168 
   169 lemma knows_subset_knows_Notes: "knows A evs \<subseteq> knows A (Notes A' X # evs)"
   170 by (simp add: subset_insertI)
   171 
   172 lemma knows_subset_knows_Gets: "knows A evs \<subseteq> knows A (Gets A' X # evs)"
   173 by (simp add: subset_insertI)
   174 
   175 text{*Agents know what they say*}
   176 lemma Says_imp_knows [rule_format]: "Says A B X \<in> set evs --> X \<in> knows A evs"
   177 apply (induct_tac "evs")
   178 apply (simp_all (no_asm_simp) split add: event.split)
   179 apply blast
   180 done
   181 
   182 text{*Agents know what they note*}
   183 lemma Notes_imp_knows [rule_format]: "Notes A X \<in> set evs --> X \<in> knows A evs"
   184 apply (induct_tac "evs")
   185 apply (simp_all (no_asm_simp) split add: event.split)
   186 apply blast
   187 done
   188 
   189 text{*Agents know what they receive*}
   190 lemma Gets_imp_knows_agents [rule_format]:
   191      "A \<noteq> Spy --> Gets A X \<in> set evs --> X \<in> knows A evs"
   192 apply (induct_tac "evs")
   193 apply (simp_all (no_asm_simp) split add: event.split)
   194 done
   195 
   196 
   197 text{*What agents DIFFERENT FROM Spy know 
   198   was either said, or noted, or got, or known initially*}
   199 lemma knows_imp_Says_Gets_Notes_initState [rule_format]:
   200      "[| X \<in> knows A evs; A \<noteq> Spy |] ==> EX B.  
   201   Says A B X \<in> set evs | Gets A X \<in> set evs | Notes A X \<in> set evs | X \<in> initState A"
   202 apply (erule rev_mp)
   203 apply (induct_tac "evs")
   204 apply (simp_all (no_asm_simp) split add: event.split)
   205 apply blast
   206 done
   207 
   208 text{*What the Spy knows -- for the time being --
   209   was either said or noted, or known initially*}
   210 lemma knows_Spy_imp_Says_Notes_initState [rule_format]:
   211      "[| X \<in> knows Spy evs |] ==> EX A B.  
   212   Says A B X \<in> set evs | Notes A X \<in> set evs | X \<in> initState Spy"
   213 apply (erule rev_mp)
   214 apply (induct_tac "evs")
   215 apply (simp_all (no_asm_simp) split add: event.split)
   216 apply blast
   217 done
   218 
   219 lemma parts_knows_Spy_subset_used: "parts (knows Spy evs) \<subseteq> used evs"
   220 apply (induct_tac "evs", force)  
   221 apply (simp add: parts_insert_knows_A knows_Cons add: event.split, blast) 
   222 done
   223 
   224 lemmas usedI = parts_knows_Spy_subset_used [THEN subsetD, intro]
   225 
   226 lemma initState_into_used: "X \<in> parts (initState B) ==> X \<in> used evs"
   227 apply (induct_tac "evs")
   228 apply (simp_all add: parts_insert_knows_A split add: event.split, blast)
   229 done
   230 
   231 lemma used_Says [simp]: "used (Says A B X # evs) = parts{X} \<union> used evs"
   232 by simp
   233 
   234 lemma used_Notes [simp]: "used (Notes A X # evs) = parts{X} \<union> used evs"
   235 by simp
   236 
   237 lemma used_Gets [simp]: "used (Gets A X # evs) = used evs"
   238 by simp
   239 
   240 lemma used_nil_subset: "used [] \<subseteq> used evs"
   241 apply simp
   242 apply (blast intro: initState_into_used)
   243 done
   244 
   245 text{*NOTE REMOVAL--laws above are cleaner, as they don't involve "case"*}
   246 declare knows_Cons [simp del]
   247         used_Nil [simp del] used_Cons [simp del]
   248 
   249 
   250 text{*For proving theorems of the form @{term "X \<notin> analz (knows Spy evs) --> P"}
   251   New events added by induction to "evs" are discarded.  Provided 
   252   this information isn't needed, the proof will be much shorter, since
   253   it will omit complicated reasoning about @{term analz}.*}
   254 
   255 lemmas analz_mono_contra =
   256        knows_Spy_subset_knows_Spy_Says [THEN analz_mono, THEN contra_subsetD]
   257        knows_Spy_subset_knows_Spy_Notes [THEN analz_mono, THEN contra_subsetD]
   258        knows_Spy_subset_knows_Spy_Gets [THEN analz_mono, THEN contra_subsetD]
   259 
   260 ML
   261 {*
   262 val analz_mono_contra_tac = 
   263   let val analz_impI = inst "P" "?Y \<notin> analz (knows Spy ?evs)" impI
   264   in
   265     rtac analz_impI THEN' 
   266     REPEAT1 o 
   267       (dresolve_tac (thms"analz_mono_contra"))
   268     THEN' mp_tac
   269   end
   270 *}
   271 
   272 
   273 lemma knows_subset_knows_Cons: "knows A evs \<subseteq> knows A (e # evs)"
   274 by (induct e, auto simp: knows_Cons)
   275 
   276 lemma initState_subset_knows: "initState A \<subseteq> knows A evs"
   277 apply (induct_tac evs, simp) 
   278 apply (blast intro: knows_subset_knows_Cons [THEN subsetD])
   279 done
   280 
   281 
   282 text{*For proving @{text new_keys_not_used}*}
   283 lemma keysFor_parts_insert:
   284      "[| K \<in> keysFor (parts (insert X G));  X \<in> synth (analz H) |] 
   285       ==> K \<in> keysFor (parts (G \<union> H)) | Key (invKey K) \<in> parts H"; 
   286 by (force 
   287     dest!: parts_insert_subset_Un [THEN keysFor_mono, THEN [2] rev_subsetD]
   288            analz_subset_parts [THEN keysFor_mono, THEN [2] rev_subsetD]
   289     intro: analz_subset_parts [THEN subsetD] parts_mono [THEN [2] rev_subsetD])
   290 
   291 method_setup analz_mono_contra = {*
   292     Method.no_args
   293       (Method.METHOD (fn facts => REPEAT_FIRST analz_mono_contra_tac)) *}
   294     "for proving theorems of the form X \<notin> analz (knows Spy evs) --> P"
   295 
   296 subsubsection{*Useful for case analysis on whether a hash is a spoof or not*}
   297 
   298 ML
   299 {*
   300 val knows_Cons     = thm "knows_Cons"
   301 val used_Nil       = thm "used_Nil"
   302 val used_Cons      = thm "used_Cons"
   303 
   304 val Notes_imp_used = thm "Notes_imp_used";
   305 val Says_imp_used = thm "Says_imp_used";
   306 val Says_imp_knows_Spy = thm "Says_imp_knows_Spy";
   307 val Notes_imp_knows_Spy = thm "Notes_imp_knows_Spy";
   308 val knows_Spy_partsEs = thms "knows_Spy_partsEs";
   309 val spies_partsEs = thms "spies_partsEs";
   310 val Says_imp_spies = thm "Says_imp_spies";
   311 val parts_insert_spies = thm "parts_insert_spies";
   312 val Says_imp_knows = thm "Says_imp_knows";
   313 val Notes_imp_knows = thm "Notes_imp_knows";
   314 val Gets_imp_knows_agents = thm "Gets_imp_knows_agents";
   315 val knows_imp_Says_Gets_Notes_initState = thm "knows_imp_Says_Gets_Notes_initState";
   316 val knows_Spy_imp_Says_Notes_initState = thm "knows_Spy_imp_Says_Notes_initState";
   317 val usedI = thm "usedI";
   318 val initState_into_used = thm "initState_into_used";
   319 val used_Says = thm "used_Says";
   320 val used_Notes = thm "used_Notes";
   321 val used_Gets = thm "used_Gets";
   322 val used_nil_subset = thm "used_nil_subset";
   323 val analz_mono_contra = thms "analz_mono_contra";
   324 val knows_subset_knows_Cons = thm "knows_subset_knows_Cons";
   325 val initState_subset_knows = thm "initState_subset_knows";
   326 val keysFor_parts_insert = thm "keysFor_parts_insert";
   327 
   328 
   329 val synth_analz_mono = thm "synth_analz_mono";
   330 
   331 val knows_Spy_subset_knows_Spy_Says = thm "knows_Spy_subset_knows_Spy_Says";
   332 val knows_Spy_subset_knows_Spy_Notes = thm "knows_Spy_subset_knows_Spy_Notes";
   333 val knows_Spy_subset_knows_Spy_Gets = thm "knows_Spy_subset_knows_Spy_Gets";
   334 
   335 
   336 val synth_analz_mono_contra_tac = 
   337   let val syan_impI = inst "P" "?Y \<notin> synth (analz (knows Spy ?evs))" impI
   338   in
   339     rtac syan_impI THEN' 
   340     REPEAT1 o 
   341       (dresolve_tac 
   342        [knows_Spy_subset_knows_Spy_Says RS synth_analz_mono RS contra_subsetD,
   343         knows_Spy_subset_knows_Spy_Notes RS synth_analz_mono RS contra_subsetD,
   344 	knows_Spy_subset_knows_Spy_Gets RS synth_analz_mono RS contra_subsetD])
   345     THEN'
   346     mp_tac
   347   end;
   348 *}
   349 
   350 method_setup synth_analz_mono_contra = {*
   351     Method.no_args
   352       (Method.METHOD (fn facts => REPEAT_FIRST synth_analz_mono_contra_tac)) *}
   353     "for proving theorems of the form X \<notin> synth (analz (knows Spy evs)) --> P"
   354 
   355 end