(*************************************************************
 *                                                           *
 *  Cryptographic protocol verifier                          *
 *                                                           *
 *  Bruno Blanchet, Vincent Cheval, and Marc Sylvestre       *
 *                                                           *
 *  Copyright (C) INRIA, CNRS 2000-2018                      *
 *                                                           *
 *************************************************************)

(*

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details (in file LICENSE).

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*)
(* Signed Diffie-Hellman
	A -> B : { e^n0 }skA, pkA    (n0 fresh)
        B -> A : { e^n1 }skB         (n1 fresh)
          A and B compute the key as k = (e^n0)^n1 = (e^n1)^n0
        A -> B : { s }k

 *)

free c.
private free s.

(* Signatures *)

fun sign/2.
fun pk/1.
reduc getmess(sign(m,k)) = m.
reduc checksign(sign(m,k), pk(k)) = m.

(* Shared key cryptography *)

fun enc/2.
reduc dec(enc(x,y),y) = x.

(* Diffie-Hellman functions *)

fun f/2.
fun g/1.
equation f(x,g(y)) = f(y,g(x)).

(* Test whether s is secret *)

query attacker:s.

(* The process *)

let p0 = in(c, pkX);
	 new n0; 
         out(c,(sign(g(n0), skA), pkA));
	 in(c,m1); 
	 let x1 = checksign(m1, pkX) in
	 let k = f(n0,x1) in 
  	 if pkX = pkB then
	 out(c, enc(s,k)).

let p1 = in(c, (m0, pkY));
	 let x0 = checksign(m0, pkY) in
	 new n1; 
         let k = f(n1,x0) in 
         out(c,sign(g(n1), skB));
         in (c,m); 
         let s2 = dec(m,k) in 0.

process 
	new skA;
	let pkA = pk(skA) in
	out(c, pkA);
	new skB;
	let pkB = pk(skB) in
	out(c, pkB);
	((!p0) | (!p1))
