(* Title: HOL/SET_Protocol/Message_SET.thy Author: Giampaolo Bella Author: Fabio Massacci Author: Lawrence C Paulson
*)
section\<open>The Message Theory, Modified for SET\<close>
theory Message_SET imports Main "HOL-Library.Nat_Bijection" begin
subsection\<open>General Lemmas\<close>
text\<open>Needed occasionally with \<open>spy_analz_tac\<close>, e.g. in \<open>analz_insert_Key_newK\<close>\<close>
lemma Un_absorb3 [simp] : "A \ (B \ A) = B \ A" by blast
text\<open>Collapses redundant cases in the huge protocol proofs\<close> lemmas disj_simps = disj_comms disj_left_absorb disj_assoc
text\<open>Effective with assumptions like \<^term>\<open>K \<notin> range pubK\<close> and \<^term>\<open>K \<notin> invKey`range pubK\<close>\<close> lemma notin_image_iff: "(y \ f`I) = (\i\I. f i \ y)" by blast
text\<open>Effective with the assumption \<^term>\<open>KK \<subseteq> - (range(invKey o pubK))\<close>\<close> lemma disjoint_image_iff: "(A \ - (f`I)) = (\i\I. f i \ A)" by blast
type_synonym key = nat
consts
all_symmetric :: bool \<comment> \<open>true if all keys are symmetric\<close>
invKey :: "key\key" \ \inverse of a symmetric key\
text\<open>Agents. We allow any number of certification authorities, cardholders
merchants, and payment gateways.\<close> datatype
agent = CA nat | Cardholder nat | Merchant nat | PG nat | Spy
(*Concrete syntax: messages appear as \<open>\<lbrace>A,B,NA\<rbrace>\<close>, etc...*) syntax "_MTuple" :: "['a, args] \ 'a * 'b" (\(\indent=2 notation=\mixfix message tuple\\\_,/ _\)\)
syntax_consts "_MTuple"\<rightleftharpoons> MPair translations "\x, y, z\" \ "\x, \y, z\\" "\x, y\" \ "CONST MPair x y"
definition nat_of_agent :: "agent \ nat" where "nat_of_agent == case_agent (curry prod_encode 0)
(curry prod_encode 1)
(curry prod_encode 2)
(curry prod_encode 3)
(prod_encode (4,0))" \<comment> \<open>maps each agent to a unique natural number, for specifications\<close>
text\<open>The function is indeed injective\<close> lemma inj_nat_of_agent: "inj nat_of_agent" by (simp add: nat_of_agent_def inj_on_def curry_def prod_encode_eq split: agent.split)
definition (*Keys useful to decrypt elements of a message set*)
keysFor :: "msg set \ key set" where"keysFor H = invKey ` {K. \X. Crypt K X \ H}"
subsubsection\<open>Inductive definition of all "parts" of a message.\<close>
inductive_set
parts :: "msg set \ msg set" for H :: "msg set" where
Inj [intro]: "X \ H ==> X \ parts H"
| Fst: "\X,Y\ \ parts H ==> X \ parts H"
| Snd: "\X,Y\ \ parts H ==> Y \ parts H"
| Body: "Crypt K X \ parts H ==> X \ parts H"
lemma MPair_parts: "[| \X,Y\ \ parts H;
[| X \<in> parts H; Y \<in> parts H |] ==> P |] ==> P" by (blast dest: parts.Fst parts.Snd)
declare MPair_parts [elim!] parts.Body [dest!] text\<open>NB These two rules are UNSAFE in the formal sense, as they discard the
compound message. They work well on THIS FILE. \<open>MPair_parts\<close> is left as SAFE because it speeds up proofs.
The Crypt rule is normally kept UNSAFE to avoid breaking up certificates.\<close>
lemma parts_increasing: "H \ parts(H)" by blast
lemmas parts_insertI = subset_insertI [THEN parts_mono, THEN subsetD]
lemma parts_emptyE [elim!]: "X\ parts{} ==> P" by simp
(*WARNING: loops if H = {Y}, therefore must not be repeated!*) lemma parts_singleton: "X\ parts H ==> \Y\H. X\ parts {Y}" by (erule parts.induct, fast+)
lemma parts_insert: "parts (insert X H) = parts {X} \ parts H" apply (subst insert_is_Un [of _ H]) apply (simp only: parts_Un) done
(*TWO inserts to avoid looping. This rewrite is better than nothing.
Not suitable for Addsimps: its behaviour can be strange.*) lemma parts_insert2: "parts (insert X (insert Y H)) = parts {X} \ parts {Y} \ parts H" apply (simp add: Un_assoc) apply (simp add: parts_insert [symmetric]) done
(*Added to simplify arguments to parts, analz and synth.*)
text\<open>This allows \<open>blast\<close> to simplify occurrences of \<^term>\<open>parts(G\<union>H)\<close> in the assumption.\<close> declare parts_Un [THEN equalityD1, THEN subsetD, THEN UnE, elim!]
lemma parts_insert_subset: "insert X (parts H) \ parts(insert X H)" by (blast intro: parts_mono [THEN [2] rev_subsetD])
subsubsection\<open>Idempotence and transitivity\<close>
lemma parts_partsD [dest!]: "X\ parts (parts H) ==> X\ parts H" by (erule parts.induct, blast+)
lemma parts_idem [simp]: "parts (parts H) = parts H" by blast
lemma parts_trans: "[| X\ parts G; G \ parts H |] ==> X\ parts H" by (drule parts_mono, blast)
(*Cut*) lemma parts_cut: "[| Y\ parts (insert X G); X\ parts H |] ==> Y\ parts (G \ H)" by (erule parts_trans, auto)
lemma parts_cut_eq [simp]: "X\ parts H ==> parts (insert X H) = parts H" by (force dest!: parts_cut intro: parts_insertI)
subsubsection\<open>Rewrite rules for pulling out atomic messages\<close>
(*In any message, there is an upper bound N on its greatest nonce.*) lemma msg_Nonce_supply: "\N. \n. N\n \ Nonce n \ parts {msg}" apply (induct_tac "msg") apply (simp_all (no_asm_simp) add: exI parts_insert2) (*MPair case: blast_tac works out the necessary sum itself!*) prefer 2 apply (blast elim!: add_leE) (*Nonce case*) apply (rename_tac nat) apply (rule_tac x = "N + Suc nat"in exI) apply (auto elim!: add_leE) done
(* Ditto, for numbers.*) lemma msg_Number_supply: "\N. \n. N\n \ Number n \ parts {msg}" apply (induct_tac "msg") apply (simp_all (no_asm_simp) add: exI parts_insert2) prefer 2 apply (blast elim!: add_leE) apply (rename_tac nat) apply (rule_tac x = "N + Suc nat"in exI, auto) done
text\<open>Inductive definition of "analz" -- what can be broken down from a set of
messages, including keys. A form of downward closure. Pairs can
be taken apart; messages decrypted with known keys.\<close>
inductive_set
analz :: "msg set => msg set" for H :: "msg set" where
Inj [intro,simp] : "X \ H ==> X \ analz H"
| Fst: "\X,Y\ \ analz H ==> X \ analz H"
| Snd: "\X,Y\ \ analz H ==> Y \ analz H"
| Decrypt [dest]: "[|Crypt K X \ analz H; Key(invKey K) \ analz H|] ==> X \ analz H"
text\<open>Making it safe speeds up proofs\<close> lemma MPair_analz [elim!]: "[| \X,Y\ \ analz H;
[| X \<in> analz H; Y \<in> analz H |] ==> P
|] ==> P" by (blast dest: analz.Fst analz.Snd)
lemma analz_increasing: "H \ analz(H)" by blast
lemma analz_subset_parts: "analz H \ parts H" apply (rule subsetI) apply (erule analz.induct, blast+) done
(*Converse fails: we can analz more from the union than from the
separate parts, as a key in one might decrypt a message in the other*) lemma analz_Un: "analz(G) \ analz(H) \ analz(G \ H)" by (intro Un_least analz_mono Un_upper1 Un_upper2)
lemma analz_insert: "insert X (analz H) \ analz(insert X H)" by (blast intro: analz_mono [THEN [2] rev_subsetD])
subsubsection\<open>Rewrite rules for pulling out atomic messages\<close>
lemma lemma1: "Key (invKey K) \ analz H ==>
analz (insert (Crypt K X) H) \<subseteq>
insert (Crypt K X) (analz (insert X H))" apply (rule subsetI) apply (erule_tac x = x in analz.induct, auto) done
lemma lemma2: "Key (invKey K) \ analz H ==>
insert (Crypt K X) (analz (insert X H)) \<subseteq>
analz (insert (Crypt K X) H)" apply auto apply (erule_tac x = x in analz.induct, auto) apply (blast intro: analz_insertI analz.Decrypt) done
lemma analz_insert_Decrypt: "Key (invKey K) \ analz H ==>
analz (insert (Crypt K X) H) =
insert (Crypt K X) (analz (insert X H))" by (intro equalityI lemma1 lemma2)
(*Case analysis: either the message is secure, or it is not! Effective, but can cause subgoals to blow up! Use with if_split; apparently split_tac does not cope with patterns
such as "analz (insert (Crypt K X) H)" *) lemma analz_Crypt_if [simp]: "analz (insert (Crypt K X) H) =
(if (Key (invKey K) \<in> analz H) then insert (Crypt K X) (analz (insert X H))
else insert (Crypt K X) (analz H))" by (simp add: analz_insert_Crypt analz_insert_Decrypt)
(*This rule supposes "for the sake of argument" that we have the key.*) lemma analz_insert_Crypt_subset: "analz (insert (Crypt K X) H) \
insert (Crypt K X) (analz (insert X H))" apply (rule subsetI) apply (erule analz.induct, auto) done
lemma analz_trans: "[| X\ analz G; G \ analz H |] ==> X\ analz H" by (drule analz_mono, blast)
(*Cut; Lemma 2 of Lowe*) lemma analz_cut: "[| Y\ analz (insert X H); X\ analz H |] ==> Y\ analz H" by (erule analz_trans, blast)
(*Cut can be proved easily by induction on "Y: analz (insert X H) ==> X: analz H \<longrightarrow> Y: analz H"
*)
(*This rewrite rule helps in the simplification of messages that involve the forwarding of unknown components (X). Without it, removing occurrences
of X can be very complicated. *) lemma analz_insert_eq: "X\ analz H ==> analz (insert X H) = analz H" by (blast intro: analz_cut analz_insertI)
lemma analz_cong: "[| analz G = analz G'; analz H = analz H'
|] ==> analz (G \<union> H) = analz (G' \<union> H')" by (intro equalityI analz_subset_cong, simp_all)
lemma analz_insert_cong: "analz H = analz H' ==> analz(insert X H) = analz(insert X H')" by (force simp only: insert_def intro!: analz_cong)
(*If there are no pairs or encryptions then analz does nothing*) lemma analz_trivial: "[| \X Y. \X,Y\ \ H; \X K. Crypt K X \ H |] ==> analz H = H" apply safe apply (erule analz.induct, blast+) done
text\<open>Inductive definition of "synth" -- what can be built up from a set of
messages. A form of upward closure. Pairs can be built, messages
encrypted with known keys. Agent names are public domain.
Numbers can be guessed, but Nonces cannot be.\<close>
inductive_set
synth :: "msg set \ msg set" for H :: "msg set" where
Inj [intro]: "X \ H ==> X \ synth H"
| Agent [intro]: "Agent agt \ synth H"
| Number [intro]: "Number n \ synth H"
| Hash [intro]: "X \ synth H ==> Hash X \ synth H"
| MPair [intro]: "[|X \ synth H; Y \ synth H|] ==> \X,Y\ \ synth H"
| Crypt [intro]: "[|X \ synth H; Key(K) \ H|] ==> Crypt K X \ synth H"
(*NO Agent_synth, as any Agent name can be synthesized. Ditto for Number*) inductive_cases Nonce_synth [elim!]: "Nonce n \ synth H" inductive_cases Key_synth [elim!]: "Key K \ synth H" inductive_cases Hash_synth [elim!]: "Hash X \ synth H" inductive_cases MPair_synth [elim!]: "\X,Y\ \ synth H" inductive_cases Crypt_synth [elim!]: "Crypt K X \ synth H" inductive_cases Pan_synth [elim!]: "Pan A \ synth H"
lemma synth_increasing: "H \ synth(H)" by blast
subsubsection\<open>Unions\<close>
(*Converse fails: we can synth more from the union than from the
separate parts, building a compound message using elements of each.*) lemma synth_Un: "synth(G) \ synth(H) \ synth(G \ H)" by (intro Un_least synth_mono Un_upper1 Un_upper2)
lemma synth_insert: "insert X (synth H) \ synth(insert X H)" by (blast intro: synth_mono [THEN [2] rev_subsetD])
subsubsection\<open>Idempotence and transitivity\<close>
subsubsection\<open>For reasoning about the Fake rule in traces\<close>
lemma parts_insert_subset_Un: "X\ G ==> parts(insert X H) \ parts G \ parts H" by (rule subset_trans [OF parts_mono parts_Un_subset2], blast)
(*More specifically for Fake. Very occasionally we could do with a version
of the form parts{X} \<subseteq> synth (analz H) \<union> parts H *) lemma Fake_parts_insert: "X \ synth (analz H) ==>
parts (insert X H) \<subseteq> synth (analz H) \<union> parts H" apply (drule parts_insert_subset_Un) apply (simp (no_asm_use)) apply blast done
lemma Fake_parts_insert_in_Un: "[|Z \ parts (insert X H); X \ synth (analz H)|]
==> Z \<in> synth (analz H) \<union> parts H" by (blast dest: Fake_parts_insert [THEN subsetD, dest])
(*H is sometimes (Key ` KK \<union> spies evs), so can't put G=H*) lemma Fake_analz_insert: "X\ synth (analz G) ==>
analz (insert X H) \<subseteq> synth (analz G) \<union> analz (G \<union> H)" apply (rule subsetI) apply (subgoal_tac "x \ analz (synth (analz G) \ H) ") prefer 2 apply (blast intro: analz_mono [THEN [2] rev_subsetD] analz_mono [THEN synth_mono, THEN [2] rev_subsetD]) apply (simp (no_asm_use)) apply blast done
lemma analz_conj_parts [simp]: "(X \ analz H \ X \ parts H) = (X \ analz H)" by (blast intro: analz_subset_parts [THEN subsetD])
lemma analz_disj_parts [simp]: "(X \ analz H | X \ parts H) = (X \ parts H)" by (blast intro: analz_subset_parts [THEN subsetD])
(*Without this equation, other rules for synth and analz would yield
redundant cases*) lemma MPair_synth_analz [iff]: "(\X,Y\ \ synth (analz H)) =
(X \<in> synth (analz H) \<and> Y \<in> synth (analz H))" by blast
lemma Crypt_synth_analz: "[| Key K \ analz H; Key (invKey K) \ analz H |]
==> (Crypt K X \<in> synth (analz H)) = (X \<in> synth (analz H))" by blast
(*We do NOT want Crypt... messages broken up in protocols!!*) declare parts.Body [rule del]
text\<open>Rewrites to push in Key and Crypt messages, so that other messages can
be pulled out using the \<open>analz_insert\<close> rules\<close>
lemmas pushKeys =
insert_commute [of "Key K""Agent C"]
insert_commute [of "Key K""Nonce N"]
insert_commute [of "Key K""Number N"]
insert_commute [of "Key K""Pan PAN"]
insert_commute [of "Key K""Hash X"]
insert_commute [of "Key K""MPair X Y"]
insert_commute [of "Key K""Crypt X K'"] for K C N PAN X Y K'
lemmas pushCrypts =
insert_commute [of "Crypt X K""Agent C"]
insert_commute [of "Crypt X K""Nonce N"]
insert_commute [of "Crypt X K""Number N"]
insert_commute [of "Crypt X K""Pan PAN"]
insert_commute [of "Crypt X K""Hash X'"]
insert_commute [of "Crypt X K""MPair X' Y"] for X K C N PAN X' Y
text\<open>Cannot be added with \<open>[simp]\<close> -- messages should not always be
re-ordered.\<close> lemmas pushes = pushKeys pushCrypts
subsection\<open>Tactics useful for many protocol proofs\<close> (*<*)
ML \<open> (*Analysis of Fake cases. Also works for messages that forward unknown parts, but this application is no longer necessary if analz_insert_eq is used.
DEPENDS UPON "X" REFERRING TO THE FRADULENT MESSAGE *)
fun impOfSubs th = th RSN (2, @{thm rev_subsetD})
(*Apply rules to break down assumptions of the form Y \<in> parts(insert X H) and Y \<in> analz(insert X H)
*) fun Fake_insert_tac ctxt =
dresolve_tac ctxt [impOfSubs @{thm Fake_analz_insert},
impOfSubs @{thm Fake_parts_insert}] THEN'
eresolve_tac ctxt [asm_rl, @{thm synth.Inj}];
fun Fake_insert_simp_tac ctxt i =
REPEAT (Fake_insert_tac ctxt i) THEN asm_full_simp_tac ctxt i;
fun atomic_spy_analz_tac ctxt =
SELECT_GOAL
(Fake_insert_simp_tac ctxt 1 THEN
IF_UNSOLVED
(Blast.depth_tac (ctxt addIs [@{thm analz_insertI},
impOfSubs @{thm analz_subset_parts}]) 4 1));
fun spy_analz_tac ctxt i =
DETERM
(SELECT_GOAL
(EVERY
[ (*push in occurrences of X...*)
(REPEAT o CHANGED)
(Rule_Insts.res_inst_tac ctxt [((("x", 1), Position.none), "X")] []
(@{thm insert_commute} RS ssubst) 1), (*...allowing further simplifications*)
simp_tac ctxt 1,
REPEAT (FIRSTGOAL (resolve_tac ctxt [allI,impI,notI,conjI,iffI])),
DEPTH_SOLVE (atomic_spy_analz_tac ctxt 1)]) i); \<close> (*>*)
(*By default only o_apply is built-in. But in the presence of eta-expansion this means that some terms displayed as (f o g) will be rewritten, and others
will not!*) declare o_def [simp]
lemma Crypt_notin_image_Key [simp]: "Crypt K X \ Key ` A" by auto
lemma Hash_notin_image_Key [simp] :"Hash X \ Key ` A" by auto
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.