(* Needham-Schroeder shared key corrected *)

free c: channel.

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

(* Shared key encryption *)

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

(* 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 endBparam(host,host).
event beginAparam(host,host).
event beginBparam(host,host).
event endAkey(host, host, key).
event beginAkey(host, host, key).
event endBkey(host, host, key).
event beginBkey(host, host, key).

query x: host, y: host; inj-event(endAparam(x,y)) ==> inj-event(beginAparam(x,y)).
query x: host, y: host, z: key; inj-event(endAkey(x,y,z)) ==> inj-event(beginAkey(x,y,z)).
query x: host, y: host; inj-event(endBparam(x,y)) ==> inj-event(beginBparam(x,y)).
query x: host, y: host, z: key; inj-event(endBkey(x,y,z)) ==> inj-event(beginBkey(x,y,z)).

(* constants 0 and 1 *)

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



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

let processInitiator =
	(* Session secret *)
	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, h: host));
	if xA = A || xA = B then
	get keys(=xA, kas) in
	(* Real start of the role *)
	in(c, bm: bitstring);
	event beginBparam(xA,h);
	new Na: nonce;
	out(c,(xA, h, Na, bm));
	in(c,m2: bitstring);
	let (=Na, =h, k: key, m: bitstring) = decrypt(m2, kas) in
	event beginBkey(xA, h, k);
	out(c,m);
	in(c, m3: bitstring);
	let (=c0, n: nonce) = decrypt(m3, k) in
	out(c, encrypt((c1, n), k));
	if h = A || h = B then
	event endAparam(xA, h);
	event endAkey(xA, h, k);
	out(c, encrypt(secretA, k)).

(* Role of the responder with identity xB and key kbs shared with S *)
                         
let processResponder =
	(* Session secret *)
	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,h: host);
	event beginAparam(h, xB);
	new J: nonce;
	out(c, encrypt((h, J), kbs));
	in(c,m4: bitstring);
	let (k: key,=h,=J) = decrypt(m4,kbs) in
	event beginAkey(h, xB, k);
	new Nb: nonce;
	out(c, encrypt((c0, Nb), k));
	in(c,m5: bitstring);
	let (=c1, =Nb) = decrypt(m5,k) in
	if h = A || h = B then
	event endBparam(h, xB);
	event endBkey(h, xB, k);
	out(c, encrypt(secretB, k)).

(* Server *)

let processS =
	in(c, (h1: host,h2: host,n: nonce, bm: bitstring));
	get keys(=h1, ks1) in
	get keys(=h2, ks2) in
	let (=h1,j: nonce) = decrypt(bm, ks2) in
	new k: key;
	out(c, encrypt((n, h2, k, encrypt((k,h1,j),ks2)), ks1)).

(* 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(secretA[!1 = v]) is true.
RESULT not attacker(secretB[!1 = v]) is true.
RESULT inj-event(endAparam(x,y)) ==> inj-event(beginAparam(x,y)) is true.
RESULT inj-event(endAkey(x,y,z)) ==> inj-event(beginAkey(x,y,z)) is true.
RESULT inj-event(endBparam(x,y)) ==> inj-event(beginBparam(x,y)) is true.
RESULT inj-event(endBkey(x,y,z)) ==> inj-event(beginBkey(x,y,z)) is true.
0.436s (user 0.388s + system 0.048s), max rss 11792K
END *)
