/* 
 * Coursework 2 PI protocol
*/
usertype String;
usertype Timestamp;
usertype Sessionkey;

hashfunction Mac;

protocol protocolPI(Network, Application, Phone) {

	/* Role R - Phone
	 *
	 * has keys k(N,R)
	 *
	 */
	role Phone {

		var SesK: SessionKey;
		var tl: Timestamp;

		recv_keysPhone(Network, Phone, {Mac(SesK, tl)}k(Network, Phone), {SesK, tl}k(Network, Phone));

		var mApp: String;

		recv_1(Application,Phone, {mApp}SesK);

		fresh mPhone: String;

		send_2(Phone,Application, {mApp, mPhone}SesK);
	}

	/* Role S - Application
	 *
	 * has keys k(N,S)
	 *
	 */
	role Application {

		send_refreshKeys(Application,Network, Application, Phone);

		var SesK: SessionKey;
		var tl: Timestamp;

		recv_keysApp(Network,Application, {Mac(SesK, tl)}k(Network, Application), {SesK, tl}k(Network, Application));

		fresh mApp: String;
		var mPhone: String;

		send_1(Application,Phone, {mApp}SesK);

		recv_2(Phone,Application, {mApp, mPhone}SesK);
	}

	/* Role N - Network
	 *
	 * has keys k(N,R) and k(N,S)
	 *
	 */
	role Network {

		recv_refreshKeys(Application,Network, Application, Phone);

		fresh SesK: SessionKey;
		fresh tl: Timestamp;

		send_keysApp(Network,Application, {Mac(SesK, tl)}k(Network, Application), {SesK, tl}k(Network, Application));

		send_keysPhone(Network,Phone, {Mac(SesK, tl)}k(Network, Phone), {SesK, tl}k(Network, Phone));
	}

	
}

