(* Yahalom protocol, version of Paulson

A -> B : A, N_A
B -> S : B, N_B, { c1, A, N_A }_Kbs
S -> A : { B, K_ab, N_A, N_B }_Kas, { c2, A, K_ab }_Kbs
A -> B : { c2, A, K_ab }_Kbs, { N_B }_Kab

*)

set keyCompromise = strict.
(*set ignoreTypes = true.*)

free c: channel.

type key.
type host.
type nonce.
type tag.

fun nonce_to_bitstring(nonce): bitstring [data,typeConverter].

(* Shared key encryption *)

fun encrypt(bitstring,key): bitstring.
reduc forall x: bitstring, y: key; decrypt(encrypt(x,y),y) = x.

(* tags *)

const c1:tag [data].
const c2:tag [data].

(* Secrecy assumptions *)

not attacker(new Kas).
not attacker(new Kbs).

(* 2 honest host names A and B *)

free A, B: host.

(* the table host names/keys 
   The key table consists of pairs 
   (host, key shared between the host and the server) *)
table keys(host, key).

(* Queries *)

query attacker(new secretA);
      attacker(new secretB).

event endAparam(host, host).
event beginAparam(host, host).
event endBkey(host, host, nonce, key).
event beginBkey(host, host, nonce, key).


query x: host, y: host; inj-event(endAparam(x,y)) ==> inj-event(beginAparam(x,y)).
query x: host, y: host, z: nonce, t: key; inj-event(endBkey(x,y,z,t)) ==> inj-event(beginBkey(x,y,z,t)).

(* Role of the initiator with identity xA and key kas shared with S *)

let processInitiator =
	new secretA: bitstring;
        (* The attacker starts the initiator by choosing identity xA.
	   We check that xA is honest (i.e. is A or B)
	   and get its corresponding key.
	*)
        in(c, xA: host);
	if xA = A || xA = B then
	get keys(=xA, kas) in
	(* Real start of the role *)
	new Na: nonce; 
	out(c, (xA, Na)); 
	in(c, (nb: nonce, m1: bitstring, m2: bitstring));
        let (b: host, kab: key, =Na, =nb) = decrypt(m1, kas) in
 	event beginBkey(xA, b, nb, kab); 
        out(c, (m2, encrypt(nonce_to_bitstring(nb), kab)));
	(* OK *) 
        if b = A || b = B then
	event endAparam(xA, b);
	out(c, encrypt(secretA, kab)).
                         
(* Role of the responder with identity xB and key kbs shared with S *)
                         
let processResponder =
	new secretB: bitstring;
        (* The attacker starts the responder by choosing identity xB.
	   We check that xB is honest (i.e. is A or B)
	   and get its corresponding key. *)
        in(c, xB: host);
	if xB = A || xB = B then
	get keys(=xB, kbs) in
	(* Real start of the role *)
	in(c, (a: host, na: nonce)); 
	event beginAparam(a, xB);
	new Nb: nonce; 
	out(c, (xB, Nb, encrypt((c1,a,na), kbs)));
	in(c, (m3: bitstring, m4: bitstring));
        let (=c2, =a, kab: key) = decrypt(m3, kbs) in
        if nonce_to_bitstring(Nb) = decrypt(m4, kab) then
	(* OK *)
        if a = A || a = B then
	event endBkey(a, xB, Nb, kab);
	out(c, encrypt(secretB, kab)).

(* Server *)

let processS = 
	in(c, (b: host, nb: nonce, m5: bitstring));
	get keys(=b, kbs2) in (* get the key of b from the key table *)
 	let (=c1, a: host, na: nonce) = decrypt(m5,kbs2) in
	get keys(=a, kas2) in (* get the key of a from the key table *)
        new kab: key;
	out(c, (encrypt((b, kab, na, nb), kas2), encrypt((c2, a, kab), kbs2))).


(* Key registration *)

let processK =
        in(c, (h: host, k: key));
        if h <> A && h <> B then insert keys(h,k).

(* Start process *)

process
	new Kas: key; new Kbs: key;
	insert keys(A, Kas);
	insert keys(B, Kbs);
	(
          (* Launch an unbounded number of sessions of the initiator *)
          (!processInitiator) | 
          (* Launch an unbounded number of sessions of the responder *)
          (!processResponder) |
          (* Launch an unbounded number of sessions of the server *)
          (!processS) |
          (* Key registration process *)
	  (!processK)
        )


(* EXPECTPV
RESULT not attacker_p1(secretA[!comp = @session1,!1 = v_4]) is true.
RESULT not attacker_p1(secretB[!comp = @session1,!1 = v_4]) cannot be proved.
RESULT inj-event(endAparam(@session1,x,y)) ==> inj-event(beginAparam(v_4,x,y)) is true.
RESULT inj-event(endBkey(@session1,x,y,z,t)) ==> inj-event(beginBkey(v_4,x,y,z,t)) cannot be proved.
RESULT (even event(endBkey(@session1,x,y,z,t)) ==> event(beginBkey(v_4,x,y,z,t)) cannot be proved.)
1.044s (user 1.004s + system 0.040s), max rss 25260K
END *)
