free c.
(* Otway Rees protocol.
   Version of Abadi and Needham, Prudent engineering practice...

A -> B: (A, B, Na)
B -> S: (A, B, Na, Nb)
S -> B: ({ Na, A, B, k }_kA, { Nb, A, B, k }_kB)
B -> A: { Na, A, B, k }_kA

Attack
I -> B : (A,B,v217)
B -> S : (A,B,v217,Nb[(A,B,v217),v215])
S -> B : (...., encrypt((Nb[(A,B,v217),v215],A,B,k[(A,B,v217,Nb[(A,B,v217),v215]),v219]),kB[]))
B says end(B)

Attack
A -> I : (A, B, Na)
I -> S : (A, B, Na, v217)
S -> I : ({ Na, A, B, k }_kA, {v217, , A, B, k }_kB)
I -> A : { Na, A, B, k }_kA

*)

(* Shared-key cryptography *)

fun encrypt/2.
reduc decrypt(encrypt(m,k),k) = m.

(* Host name / key *)

fun host/1.
private reduc getkey(host(x)) = x.

authquery Aparam/1.
authquery Akey/3.
authquery Bparam/1.
authquery Bkey/3.

let processA = in(c, hostB0);
	       new Na;
	       begin Bparam(hostB0);
               out(c, (hostA, hostB0, Na));
	       in(c, m);
	       let (Na2, hostA2, hostB2, k) = decrypt(m, kA) in
	       begin Bkey(hostA, hostB0, k);
		  if Na2 = Na then
		  if hostA2 = hostA then
		  if hostB2 = hostB0 then
		     (* OK *)
		     if hostB0 = hostB then 
			end Aparam(hostA);
                        end Akey(hostA, hostB0, k).


let processB = in(c, m);
	       let (hostA1, hostB1, m2) = m in
	       begin Aparam(hostA1);
               if hostB1 = hostB then
		  new Nb;
		  out(c, (hostA1, hostB1, m2, Nb));
		  in(c, m);
                  let (m3, m4) = m in
	          let (Nb2, hostA2, hostB2, k) = decrypt(m4, kB) in
		  begin Akey(hostA2, hostB, k);
		     if hostA2 = hostA1 then
		     if hostB2 = hostB then
                     if Nb2 = Nb then
		        out(c, m3);
			(* OK *)
			if hostA1 = hostA then 
			  end Bparam(hostB);
			  end Bkey(hostA1, hostB, k).


let processS = in(c, m);
	       let (hostA1, hostB1, Na1, Nb1) = m in
	       new k;
               out(c, (encrypt((Na1, hostA1, hostB1, k), getkey(hostA1)), 
                       encrypt((Nb1, hostA1, hostB1, k), getkey(hostB1)))).


process new kA; new kB;
	let hostA = host(kA) in 
	let hostB = host(kB) in
	out(c, hostA); out(c, hostB);
	((!processA) | (!processB) | (!processS))
