Skip to content
Snippets Groups Projects
daa_pnc_credential_installation.spthy 55.2 KiB
Newer Older
Timm Lauser's avatar
Timm Lauser committed
theory DDA_PnC_credential_installation
begin
 
/*
Protocol:	DAA_PnC
Properties:	SR2 - Secure Credential Installation

This Tamarin model is used to verify the security of the installation process
for the Direct Anonymous Authentication (DAA) based privacy extentsion of the
Plug and Charge (PnC) authentication system. The extension is described in the
paper "Integrating Privacy into the Electric Vehicle Charging Architecture".

The model consists of the following actors:
Host/EV 	- Electric Vehicle
TPM			- Trusted Platform Module used to secure the EVs private keys
Issuer		- The e-mobility service provider (eMSP), corresponding to the Issuer role in the DAA protocol
CP			- Charge Point
CPS			- Certificate Provisioning Service (limited to one instance)

This model verifies the security requirement SR2: Secure Credential Installation

It is based on the model from the paper "Formal Analysis and Implementation of a TPM 2.0-based Direct Anonymous Attestation Scheme" accepted to ASIACCS 2020 by
Original Authors:
	Liqun Chen, Surrey Centre for Cyber Security, University of Surrey
	Christoper J.P. Newton, Surrey Centre for Cyber Security, University of Surrey
	Ralf Sasse, Department of Computer Science, ETH Zurich
	Helen Treharne, Surrey Centre for Cyber Security, University of Surrey
	Stephan Wesemeyer, Surrey Centre for Cyber Security, University of Surrey
	Jorden Whitefield, Ericsson AB, Finland
cf. https://github.com/tamarin-prover/tamarin-prover/tree/dddaccbe981343dde1a321ce0c908585d4525918/examples/asiaccs20-eccDAA


time tamarin-prover daa_pnc_credential_installation.spthy\
 --heuristic=S --quit-on-warning\
 --prove +RTS -N8 -RTS

==============================================================================
summary of summaries:

analyzed: join2_wCP2test3.spthy

  forwarded_credential_res_source (all-traces): verified (265 steps)
  secrecy_of_cps_private_key (all-traces): verified (3 steps)
  secrecy_of_pke (all-traces): verified (6 steps)
  integrity_of_forwarded_credential_res_m (all-traces): verified (166 steps)
  restricition_bind (all-traces): verified (4 steps)
  restriction_one_host_per_tpm (all-traces): verified (12 steps)
  restriction_one_tpm_per_host (all-traces): verified (10 steps)
  restricition_pke_comes_from_tpm (all-traces): verified (14 steps)
  correctness_verify_multiple_pkes (exists-trace): verified (70 steps)
  correctness_verify_multiple_pkes_diff_I (exists-trace): verified (69 steps)
  correctness_credential_req (exists-trace): verified (22 steps)
  correctness_credential_req_res_1 (exists-trace): verified (57 steps)
  correctness_credential_req_res_2 (exists-trace): verified (67 steps)
  auth_aliveness_issuer_very_weak (all-traces): verified (49 steps)
  auth_aliveness_issuer (all-traces): verified (49 steps)
  auth_aliveness_host (all-traces): verified (6 steps)
  auth_weak_agreement_host (all-traces): verified (63 steps)
  auth_non_injective_agreement_host_issuer (all-traces): verified (63 steps)
  auth_injective_agreement_host_issuer (all-traces): verified (205 steps)
  auth_non_injective_agreement_CPS_EV (all-traces): verified (32 steps)
  auth_injective_agreement_CPS_EV (all-traces): verified (48 steps)
  auth_secrecy_cre_ev (all-traces): verified (303 steps)
  auth_secrecy_cre_iss (all-traces): verified (116 steps)
  auth_secrecy_emaid_iss (all-traces): verified (36 steps)
  auth_secrecy_emaid_ev (all-traces): verified (123 steps)

==============================================================================

real	14m4,080s
user	67m9,053s
sys	22m50,816s

*/

builtins:   asymmetric-encryption, symmetric-encryption, signing, diffie-hellman

functions:  accept/0, MAC/2, KDF_AES/1, KDF_EK/1,KDF_a/3, KDF_e/4, certData/2,
             multp/2, plus/2, minus/2, len16/1, 
             H_SHA256/1,  H_k_1/1, H_k_2/2, H_k_4/4, H_n_2/2, H_n_8/8, H_6/1,
			 curlyK/1, E/2, E_S/2, L_J/2, RB/2, RD/2, 
			 calcE/1, 
			 calcE_S_cert/4, calcL_J_cert/4, 
			 calcRB/1, calcRD/1, Nonce/1,
			 PkX/2, PkY/2, verifyCre1/4, verifyCre2/5,verifyCre3/4,verifyCre4/5,
			 BSN/1, F1/1, F2/1, H_p/1,PointG1/2, Message/1, Q_K/1
			 

equations:  
			calcE( 
				minus(
					multp(
							plus(
								r_cv,
								multp(
										H_n_2(n_J, H_k_1(H_k_4(P1,Q,E(r_cv,P1),str))),
										f
									)
								),
							P1
						),
					multp(
							H_n_2(n_J, H_k_1(H_k_4(P1, Q, E(r_cv,P1), str))),
							multp(
									f,
									P1
								)
						)
					)
				) = E(r_cv,P1)
				,

			calcRB(
				minus(
					multp(
						plus(l,multp(multp(y,r),H_n_8(P1, multp(f,P1), RB(l,P1), RD(l,multp(f,P1)), 
						 multp(r,P1), 
						 multp(y,multp(r,P1)), 
						 plus(multp(x,multp(r,P1)),multp(multp(multp(r,x),y),Q)), 
						 multp(multp(r,y),Q)
						 ))),
						P1),
					multp(
						H_n_8(P1, multp(f,P1), RB(l,P1), RD(l,multp(f,P1)), 
						 multp(r,P1), 
						 multp(y,multp(r,P1)), 
						 plus(multp(x,multp(r,P1)),multp(multp(multp(r,x),y),Q)), 
						 multp(multp(r,y),Q)
						),
					    multp(y,multp(r,P1))
						)						
					)
				)= RB(l,P1)
			,

			calcRD(
				minus(
					multp(plus(l,multp(multp(y,r),H_n_8(P1, multp(f, P1), RB(l,P1), RD(l,multp(f,P1)),
						 multp(r,P1), 
						 multp(y,multp(r,P1)), 
						 plus(multp(x,multp(r,P1)),multp(multp(multp(r,x),y),Q)), 
						 multp(multp(r,y),Q)
					))),multp(f, P1)),
					multp(H_n_8(P1, multp(f, P1), RB(l,P1), RD(l,multp(f,P1)), 
						 multp(r,P1), 
						 multp(y,multp(r,P1)), 
						 plus(multp(x,multp(r,P1)),multp(multp(multp(r,x),y),Q)), 
						 multp(multp(r,y),Q)
					), multp(multp(r,y),multp(f, P1)))
					)
					
				)=RD(l,multp(f,P1))
				
			
			,
			//calcL_J(s, J, h2, K)	=sJ-h2K
			//						=(r_cv1+h2f)J-h2(fJ)
			//						=r_cv1 J
			calcL_J_cert(
					plus(r_cv1,multp(h2,f)), //s
				PointG1(H_p(F1(bsn)),F2(bsn)),	//J
					H_n_2(n_C, H_k_2(small_c, H_6(certData(certificationData,Qk_n)))), //h2
					multp(f,J) //K
				)
				=
				L_J(r_cv1, PointG1(H_p(F1(bsn)),F2(bsn)))
			,

			//calcE_S_cert(small_s, S, h2, W)	=sS-h2W
			//									=(r_cv1+h2f)lyrP1-h2(lryfP1)
			//									=r_cv1lyrP1
			
			calcE_S_cert(plus(r_cv1,multp(H_n_2(n_C, H_k_2(small_c, H_6(certData(certificationData,Qk_n)))),f)), //small_s
					multp(l,multp(y,multp(r,P1))),					//S
					H_n_2(n_C, H_k_2(small_c, H_6(certData(certificationData,Qk_n)))), //h2
					multp(l,multp(multp(r,y),multp(f, P1))) //W
					)
				=
				E_S(r_cv1, multp(l,multp(y,multp(r,P1))))	//E_S(r_cv1,S)				
				
			,
			verifyCre1(
				multp(r,P1), //A
				PkY(y,P2),  //Y
				multp(y,multp(r,P1)),//B
				P2)=accept
			,
								
			verifyCre2(
				multp(r,P1), 			//A
				multp(multp(~r,y),multp(f,P1)), 	//D
				PkX(x,P2),				//X
				plus(multp(x,multp(r,P1)),multp(multp(multp(r,x),y),multp(f,P1))),//C
				P2)=accept
				,
			
			verifyCre3(
				multp(l,multp(r,P1)),	//R=lA=l(rP1)
				PkY(y,P2),				//Y
				multp(l,multp(y,multp(r,P1))),	//S=lB=l(y(A))
Timm Lauser's avatar
Timm Lauser committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
				P2)=accept
			,	
			verifyCre4(
				multp(l,multp(r,P1)),	//R=lA=l(rP1)
				multp(l,multp(multp(r,y),multp(f,P1))),	//W=l(D)=l(ryQ)=l(ry(fP1))
				PkX(x,P2),				//X
				multp(l,plus(multp(x,multp(r,P1)),multp(multp(multp(r,x),y),multp(f,P1)))), //T=lC=l(xA+rxyQ)=l(xrP1+rxyfP1)
				P2)=accept
			
//=========================================
// Protocol Restrictions (Axioms)
//=========================================

restriction equality: 	     "All #i x y . Eq( x, y ) @ i ==> x = y"


//each certificate installation response CertRes is only accepted once by a EV/Host
restriction only_once_certres: 
"All event #i #j . 
	(OnlyOnceCertRes(event) @ i & OnlyOnceCertRes(event) @ j) ==> (#i=#j)"


//Modification: removed restriction for single issuer initialisation
//each issuer should only be initialised once
restriction issuer_single_init:
	"All I #i #j . (Issuer_Init(I) @ i & Issuer_Init(I) @ j) ==> (#i=#j)"
	
//the certificate provisioning service (CPS) should only be initialized once 
restriction single_cps_single_init:
	"All CPS_I CPS_I2 #i #j . (CPS_Init(CPS_I) @ i & CPS_Init(CPS_I2) @ j) ==> (#i=#j)"
	
//each charge point (CP) should only be initialized once
restriction cp_single_init:
	"All CP #i #j . (CP_Init(CP) @ i & CP_Init(CP) @ j) ==> (#i=#j)"

//a host should only initialise itself once
restriction host_single_init:
	"All Host #i #j . ((Host_Init(Host)@i & Host_Init(Host)@j) ==> (#i=#j))"

//a TPM should only be initialised once (and hence there is only one aes key and one TPM_EK_SEED):
restriction tpm_single_init:
	"All PS #i #j. ((TPM_Init(PS)@i & TPM_Init(PS)@j) ==> (#i=#j))"


//a host and a TPM cannot be initialised with the same identity
//Modification: adjusted restrictions for ID uniqueness to new entity definitions
restriction no_shared_id_between_tpm_host:
	"All Ent1 Ent2 #i #j . 
	(Host_Init(Ent1) @ i & TPM_Init(Ent2) @ j) 
	==> 
	(not(Ent1=Ent2))"

//an issuer and a CPS cannot be initialised with the same identity
restriction no_shared_id_between_issuer_cps:
	"All Ent1 Ent2 #i #j . 
	(Issuer_Init(Ent1) @ i & CPS_Init(Ent2) @ j) 
	==> 
	(not(Ent1=Ent2))"

//an issuer and a CP cannot be initialised with the same identity
restriction no_shared_id_between_issuer_cp:
	"All Ent1 Ent2 #i #j . 
	(Issuer_Init(Ent1) @ i & CP_Init(Ent2) @ j) 
	==> 
	(not(Ent1=Ent2))"

//a CP and a CPS cannot be initialised with the same identity
restriction no_shared_id_between_cp_cps:
	"All Ent1 Ent2 #i #j . 
	(CP_Init(Ent1) @ i & CPS_Init(Ent2) @ j) 
	==> 
	(not(Ent1=Ent2))"

//When initialized, a host, tpm, issuer and cp must have different identities
restriction no_shared_id_between_tpm_host_issuer_cp:
	"All Ent1 Ent2 Ent3 Ent4 #i #j #k #l. 
	(Host_Init(Ent1) @ i & TPM_Init(Ent2) @ j & Issuer_Init(Ent3) @ k & CPS_Init(Ent4) @ l) 
	==> 
	(not(Ent1=Ent2) & not (Ent1=Ent3) & not (Ent1=Ent4)
	& not (Ent2=Ent3) & not (Ent2=Ent4)
	& not (Ent3=Ent4))"

//When initialized, a host, tpm, issuer, CPS and CP must have different identities
// This restriction includes the restrictions above, however, they are maintained for performance reasons
restriction no_shared_id_between_tpm_host_issuer_cps_cp:
	"All Ent1 Ent2 Ent3 Ent4 Ent5 #i #j #k #l #m. 
	(Host_Init(Ent1) @ i & TPM_Init(Ent2) @ j & Issuer_Init(Ent3) @ k & CPS_Init(Ent4) @ l & CP_Init(Ent5) @ m) 
	==> 
	(not(Ent1=Ent2) & not (Ent1=Ent3) & not (Ent1=Ent4) & not (Ent1=Ent5)
	& not (Ent2=Ent3) & not (Ent2=Ent4) & not (Ent2=Ent5)
	& not (Ent3=Ent4) & not (Ent3=Ent5)
	& not (Ent4=Ent5))"



//=========================================
// Secure Channel Rules
//=========================================
/* 
We need a secure channel between the TPM aka the Principal Signer (PS) 
and its host aka the Assistant Signer (AS). We refer to the combination 
of a PS and AS as a Platform.
*/

/*
   Communication between the Host or Assistant Signer (AS) and the TPM 
   or Principal Signer (PS) is done over a 'Secure Channel'. This means 
   that an adversary can neither modify nor learn messages that are 
   sent over the channel. Sec( A, B, x ) is a linear fact modelling 
   that the adversary cannot replay on this channel. Secure channels 
   have the property of being both confidential and authentic. 
   Communication between the AS and PS is constrained by the channel 
   invariant !F_Paired, such that two arbitrary roles cannot communicate 
   over this channel.
*/
rule ChanOut_S [colour=ffffff]:
    [ Out_S( $A, $B, x ), !F_Paired( $A, $B ) ]
  --[ ChanOut_S( $A, $B, x ) ]->
    [ Sec( $A, $B, x ) ]

rule ChanIn_S [colour=ffffff]:
    [ Sec( $A, $B, x ) ]
  --[ ChanIn_S( $A, $B, x ) ]->
    [ In_S( $A, $B, x ) ]


/* Modification: Added Secure Channel rules for backend communication
	Secure TLS Channel between backend actors.
	Channel is confidential and authentic.
*/
rule ChanOut_S_Backend:
        [ Out_S_B($A,$B,x) ]
      --[ ChanOut_S_B($A,$B,x) ]->
        [ SecB($A,$B,x) ]

rule ChanIn_S_Backend:
        [ SecB($A,$B,x) ]
      --[ ChanIn_S_B($A,$B,x) ]->
        [ In_S_B($A,$B,x) ]


//=========================================
// Protocol Setup and Actor Initialisation
//=========================================
/*
Issuer set-up:
Modification: Allow multiple issuer (eMSP) set-ups
*/
rule Issuer_Init:
		let 
			pkX=PkX(~x,'P2')
			pkY=PkY(~y,'P2')
		in
		[Fr(~x),
		 Fr(~y)]
		--[Issuer_Init($I)
			, Issuer_Init2($I, ~x, ~y)
			, OnlyOnce('Issuer_Init')]->
		[ !Ltk($I,~x, ~y)
			, !Pk($I, pkX,pkY)
			, Out(<pkX,pkY>)
			, !Issuer_Initialised($I)
		]


// simple key reveal rule for the issuer's secret key pair
rule Issuer_KeyReveal:
	[!Ltk($I, ~x, ~y)]
	--[KeyReveal('Issuer_KeyReveal', $I)]->
	[Out(<~x,~y>)]
	

// Modification: Added CPS set-up
rule CPS_Init:
		[Fr(~cps)]
		--[CPS_Init($CPS_I)
		   , CPS_Init2($CPS_I, ~cps)
		   , OnlyOnce('CPS_Init')]->
		[!LtkCPS($CPS_I,~cps)
		   , !PkCPS($CPS_I, pk(~cps))
		   , Out(pk(~cps))
		   , !CPS_Initialised($CPS_I)
		]

// simple key reveal rule for the CPS' secret key pair
rule CPS_KeyReveal:
	[!LtkCPS($CPS_I, ~cps)]
	--[KeyReveal('CPS_KeyReveal', $CPS_I)]->
	[Out(~cps)]


// Modification: Added CP set-up
rule CP_Init:
		[]
		--[CP_Init($CP)
		   , OnlyOnce('CP_Init')]->
		[ !CP_Initialised($CP) ]


/*
Platform set-up:
For a platform we need a TPM (the principal signer) and a Host (the assistant signer)
before binding them together in a platform.

Modification: Removed need for the host to know the issuer before the join.
*/
rule TPM_INIT:
	let
	//!Assumption that the aes key is derived by a KDF_AES key derivation function
	aes_key=KDF_AES(~TPM_AES_Seed)
	in
		[Fr(~TPM_AES_Seed),
		 Fr(~TPM_EK_Seed)]
		--[TPM_Init($PS)
		   , OnlyOnce('TPM_INIT')]->
		[!TPM_AES_Key($PS, aes_key), 
		 TPM_EK_SEED($PS,~TPM_EK_Seed), 
		 TPM_Initialised($PS)]

//simple rule to allow the TPM's aes key to leak		
rule TPM_AESReveal:
	[!TPM_AES_Key(PS, aes_key)]
	--[KeyReveal('TPM_AESReveal', PS)]->
	[Out(aes_key)]

rule Host_Init:
	[]
	--[Host_Init($AS)
	  , OnlyOnce('Host_Init')]->
	[Host_Initialised($AS)]

//This rule binds an $PS and an $AS to one another.
rule Platform_Setup:
	[ TPM_Initialised($PS)
	, Host_Initialised($AS)
	]
	 //Action label used to ensure there is a one-to-one correspondence between AS and PS
	 --[ Bind($PS,$AS)
		,OnlyOnce('Platform_Setup')
	   ]->
	 [ Out_S($AS, $PS, < 'createPrimary'>)
		, !F_Paired($AS,$PS)
		, !F_Paired($PS,$AS)
	 ]
        	 
//The TPM executes this in response to a request by the host
//Note this should only be executed by a TPM once!
rule TPM2_CreatePrimary:
	let
	e=KDF_EK(~TPM_EK_Seed)
	pke='g'^e
	E_PD=<'EK_public_data',pke>
    in
	[ In_S($AS, $PS, < 'createPrimary'>)
	, TPM_EK_SEED($PS,~TPM_EK_Seed)]
	--[ TPM2_EK_Created($PS, $AS, pke)
		, TPM2_EK_Created2(e, pke)
		, OnlyOnce('TPM2_CreatePrimary')
	  ]->
	[Out_S($PS,$AS, < E_PD, 'returnEK'>), 
	!TPM_ENDORSEMENT_SK($PS, e, pke), 
	!TPM_ENDORSEMENT_PK($PS,E_PD),
	Out(pke)]

//simple rule to reveal the TPM's endorsement key
rule TPM_EKReveal:
	let
	e=KDF_EK(~TPM_EK_Seed)
    in
	[!TPM_ENDORSEMENT_SK(PS, e, pke)]
	--[ KeyReveal('TPM_EKReveal_tpm', PS)
		, KeyReveal('TPM_EKReveal_pke', pke)
	  ]->
	[Out(e)]

//The Host should store the public endorsement key	
rule Host_Store_EK:
	let
	E_PD=<'EK_public_data', pke>
	in
	[ In_S($PS,$AS, < E_PD, 'returnEK'>) ]
	--[ Store_EK($PS, $AS)
		, OnlyOnce('Host_Store_EK')
	  ]->
	[
	 Out_S($AS,$PS, < pke, 'createPCKey'>),
	 !Host_State_01( $PS, $AS, pke )]

/* 
	Modification: Added the generation of the provisioning key pair. 
	The rule is designed based on the existing TPM2_CreatePrimary and TPM2_Create rules
*/
rule TPM2_CreatePC:
	let
	PC_PD=<'PC_public_data',pk(~pc)>
	PC_SD=senc(~pc,aes_key)
    in
	[ In_S($AS, $PS, < pke, 'createPCKey'>)
	 , !TPM_AES_Key($PS, aes_key)
	 , Fr(~pc) //pc secret key
	 ]
	--[ TPM2_PC_Created($PS, $AS)
		, DerivePCKey($PS, $AS, pke, ~pc)
		, OnlyOnce('TPM2_CreatePC')
	  ]->
	[
	 Out_S($PS, $AS,< PC_SD,PC_PD, 'returnPCKey'>), 
	 !TPM_PC_SK($PS, pke, ~pc),
	 Out(pk(~pc))
	]

//simple rule to reveal the TPM's provisioning key
rule TPM_PCReveal:
	[!TPM_PC_SK(PS, pke, ~pc)]
	--[ KeyReveal('TPM_PCReveal_tpm', PS)
		, KeyReveal('TPM_PCReveal_pke', pke)
	  ]->
	[Out(~pc)]

//The Host should store the public provisioning key	
rule Host_Store_PC_reuse:
	[ In_S($PS, $AS,< PC_SD,PC_PD, 'returnPCKey'>)]
	--[ OnlyOnce('Host_Store_PC_reuse')
	  ]->
	[ !Host_Store_PC_reuse($PS, $AS,< PC_SD,PC_PD, 'returnPCKey'>)]
rule Host_Store_PC:
	let 
	PC_PD=<'PC_public_data',pk(~pc)>
	in
	[ !Host_Store_PC_reuse($PS, $AS,< PC_SD,PC_PD, 'returnPCKey'>),
	 !Host_State_01( $PS, $AS, pke ) ]
	--[ Store_PC($PS, $AS)
		, OnlyOnce('Host_Store_PC')
	  ]->
	[Out_S($AS,$PS, < pke, 'createDAAKey'>), 
	 Host_State_02( $PS, $AS, pke, PC_PD, PC_SD )]


//=====================================================================	 
// Generating and Sending the Contract Credential Installation Request
//=====================================================================
/*
This rule will create a DAA key
Note that unlike the TPM2_CreatePrimary rule, this rule can be executed 
multiple times resulting in a new DAA key
This is obviously not sensible but allowed.
*/
rule TPM2_CreateDAA:
	let 
	Q=multp(~f, 'P1')
	Q_PD=<'DAA_public_data', Q>
	Q_SD=senc(~f,aes_key)
	in
	[In_S($AS, $PS, < pke, 'createDAAKey'>)
	 , !TPM_AES_Key($PS, aes_key)
	 , Fr(~f) //our secret key
	 ]
	--[ TPM2_DAA_Created($PS, $AS)
		, DeriveDAAKey($PS, $AS, pke, ~f)
		, OnlyOnce('TPM2_CreateDAA')
	  ]->
	[ Out_S($PS, $AS,< Q_SD,Q_PD, 'returnDAAKey'>), 
	 !TPM_DAA_SK($PS, pke, ~f),
	 Out(Q)
	]
	
//simpe rule to leak the DAA key:
rule TPM_DAAReveal:
	[!TPM_DAA_SK(PS, pke, ~f)]
	--[ KeyReveal('TPM_DAAReveal_tpm', PS)
		, KeyReveal('TPM_DAAReveal_pke', pke)
	  ]->
	[Out(~f)]

// CP sends a nonce to the EV (abstracting the timestamp) 
rule CP_Nonce:
	let 
		m=<$CP, ~sid, ~n, 'CP_Nonce'> 
	in
     [ Fr(~sid)
		, Fr(~n)
		, !CP_Initialised($CP)
	]
	--[ 
		CP_Nonce($CP, ~n, ~sid)
		, OnlyOnce('CP_Nonce')
	]->
	[Out(m)
		, CP_State_00($CP, ~sid, ~n) 
	]

/*
The host needs to store the keys on behalf of the TPM as it has
limited memory. The host then builds the credential request data CertReq and
instructs the TPM to sign this data with the private provisioning key PC

Modification: Included the generation and encryption of the EV's CertReq
*/
rule Host_Store_DAA:
	let 
		Q=multp(~f, 'P1')
		Q_PD=<'DAA_public_data', Q>

		PC_PD=<'PC_public_data',pk(~pc)>
		m_in=<$CP, sid, n, 'CP_Nonce'>

		m=<pke,pk(~pc), Q_PD, ~res_n, 'join_Issuer_1'> //EV's CertReq with OEM prov cert and public DAA key
		signed_m=H_SHA256(<m, pk(~cps), n>) //for signature over CertReq; signed_m=<m, pk(~cps)> for inclusion of CPS' public key
	in
	[ In(m_in)
		, Fr(~res_n)
		, In_S($PS, $AS, < Q_SD,Q_PD, 'returnDAAKey'>)
		, Host_State_02( $PS, $AS, pke, PC_PD, PC_SD )
		, !PkCPS(CPS_I, pk(~cps)) //from Charge Station but authentic due to certificate
	]
	--[ 
		 Store_DAA($PS, $AS)
		, Store_DAA_N($PS, $AS, n, sid)
		, Store_DAA_m($PS, $AS, m, pk(~pc))
		, Store_DAA_resn(~res_n)
		, OnlyOnce('Host_Store_Keys')
	  ]->
	[ Host_State_03( $PS, $AS,  pke, Q_PD, Q_SD, pk(~cps), CPS_I, m, signed_m, pk(~pc), sid, n ,  $CP)
		, !Host_Store_DAAKey($PS, $AS, pke, Q_PD, Q_SD)
		, Out_S($AS,$PS,< PC_SD,PC_PD, signed_m, 'TPM2_Sign'>) 
	]

// The TPM signs the credential request generated by rule Host_Store_DAA and returns the signature to the host 
rule TPM_Sign_Req:
	let
		PC_PD=<'PC_public_data',pk(~pc)>
		PC_SD=senc(~pc,aes_key)
    in
	[  In_S($AS,$PS,< PC_SD,PC_PD, signed_m, 'TPM2_Sign'>) 
		, !TPM_AES_Key($PS, aes_key)
	 	, !TPM_PC_SK($PS, pke, ~pc)
	]
	--[ TPM2_Sign($PS, $AS)
		, OnlyOnce('TPM_Sign_Req')
	  ]->
	[ Out_S($PS,$AS, < sign(signed_m,~pc), 'ret_TPM2_Sign'>)
	]

//Upon receiving the signature for a credential request, the host generates the request and sends it to the CP
rule Host_Send_Req:
	let 
		e=KDF_EK(~TPM_EK_Seed)
		pke='g'^e

		//m=<pke,pk(~pc), Q_PD, 'join_Issuer_1'> //EV's CertReq with OEM prov cert and public DAA key
		//signed_m=H_SHA256(<m, pk(~cps), n>)  // for signature over CertReq; signed_m=<m, pk(~cps)> for inclusion of CPS' public key
		Q=multp(~f, 'P1')
		Q_PD=<'DAA_public_data', Q>

		m=<pke,pk(~pc), Q_PD, ~res_n, 'join_Issuer_1'> //EV's CertReq with OEM prov cert and public DAA key
		signed_m=H_SHA256(<m, pk(~cps), n>) // for signature over CertReq; signed_m=<m, pk(~cps)> for inclusion of CPS' public key

		sig_over_m=sign(signed_m,~pc)
	in
	[In_S($PS,$AS, < sig_over_m, 'ret_TPM2_Sign'>)
	, Host_State_03( $PS, $AS,  pke, Q_PD, Q_SD, pk(~cps), CPS_I, m, signed_m, pk(~pc), sid, n ,  CP)
	]
	--[  PlatformSendKeys($PS, $AS, pke, Q_PD, pk(~pc))
		, Alive($AS)
		, Role('Platform')
		, Honest( $PS )
		, Honest($AS)
		, Honest( pke )
		, Honest( CPS_I )
		, RunningEV( pke , CPS_I, signed_m )
		, RunningEV2( ~res_n )
		, RunningEV3( sid )
		, OnlyOnce('Host_Send_Req')
	  ]->
	[ !EK_FOR_ISSUER(<pke,pk(~pc)>) //a TPM created the pke and pk(pc), both certified by OEM
	, Out(<sid, CPS_I, aenc(<sig_over_m,m>,pk(~cps)), 'Host_Send_Req'>) //CPS_I is included as CS and CSO know chosen CPS
	, !Host_State_05($PS, $AS,  pke, pk(~cps), CPS_I, sid,  CP, ~res_n)
	]


//==================================================================	 
// Contract Credential Installation Request Handling in the Backend
//==================================================================

//The CP forwards the signed credential request to the CPS
rule CP_Fwd_Credential_Request:
     [ In(<sid, CPS_I, aenc_m, 'Host_Send_Req'>)
		, CP_State_00(CP, sid, n)
	 ]
	 --[ 
		CP_Fwd_Credential_Request(CP, CPS_I, n)
		, OnlyOnce('CP_Fwd_Credential_Request')
		]->
	 //secure channel to Backend
	 [Out_S_B(CP, CPS_I, < aenc_m, n>), 
	 !CP_State_01(CP, CPS_I, sid, $AS) ]	

// The CPS verifies the credential request
//Modification: Added verification of the credential request by the CPS
rule CPS_Verify_Credential_Request:
	let 
		//inputs
		m_in=<pke,pk(pc), Q_PD, res_n, 'join_Issuer_1'>
		//decrypt and parse request
		aenc_m=aenc(<sig, m_in>, pk(~cps))

		signed_m=H_SHA256(<m_in, pk(~cps), n>) //signed_m=<m_in, pk(~cps)>
	in
     [ In_S_B(CP, CPS_I, <aenc_m, n>)
		, !EK_FOR_ISSUER(<pke,pk(pc)>) 	//a TPM created the pke and pk(pc), both certified by OEM
		//, !PkCPS(CPS_I,pk(~cps))
		, !LtkCPS(CPS_I,~cps)
		, Fr(~r)
	 ]
	 --[ Eq(verify(sig,signed_m,pk(pc)), true)		
		, Check_Ek(pke)		
		, Check_QPD(Q_PD)
		, Honest ( CPS_I )
		, Honest ( CP )
		, Honest ( pke )
		, CPSReceivedReq(CPS_I, pke, pk(pc), Q_PD)
		, CommitCPS(CPS_I, pke, signed_m)
		, OnlyOnce('CPS_Verify_Credential_Request')
		]->
	 [!CPS_State_00(CPS_I, CP, ~r, <m_in, 'CPS_Fwd_Req'>) ]	

// The CPS forwards a valid credential request to the Issuer/eMSP
rule CPS_Fwd_Credential_Request:
	let 
		//inputs
		m_in=<pke,pk(pc), Q_PD, res_n, 'join_Issuer_1'>
		m_out=<pke,pk(pc), Q_PD, 'join_Issuer_1'>
	in
     [ !CPS_State_00(CPS_I, CP, ~r, <m_in, 'CPS_Fwd_Req'>)
		, !Issuer_Initialised(I)
		,Fr(~reqID)
	 ]
	 --[ 
		CPSfwdReq(CPS_I, I, pke, pk(pc), Q_PD)
		//, OnlyOnceCertRes(<CPS_I, I, ~r>)
		, OnlyOnce('CPS_Fwd_Credential_Request')
		]->
	 //secure channel to EMSP
	 [Out_S_B(CPS_I, I, <~reqID, m_out, 'CPS_Fwd_Req'>), 
	 CPS_State_01(CPS_I, CP, ~reqID, res_n)]	

//Issuer: verify the curlyK and the signature before issuing the proper credentials for the host/EV and sending them back to the CPS
//Modification: Changed to model new behavior of eMSP issuer, i.e., interaction with CPS and inclusion of EMSP_Cert.
rule Issuer_Issue_Credentials:
	let 
		//inputs
		pke='g'^e

		Q=multp(f, 'P1')
		Q_PD=<'DAA_public_data', Q>
		m=<pke,pk(pc), Q_PD, 'join_Issuer_1'>

		//inputs from Issuer PK
		pkX=PkX(~x,'P2')
		pkY=PkY(~y,'P2')
				
		//new values to be calculated
		A=multp(~r,'P1')
		B=multp(~y,A)
		C=plus(multp(~x,A),multp(multp(multp(~r,~x),~y),Q))
		D=multp(multp(~r,~y),Q)
		
		R_B=RB(~l,'P1')
		R_D=RD(~l,Q)
		
		u=H_n_8('P1', Q, R_B, R_D, A, B, C, D)
		j=plus(~l,multp(multp(~y,~r),u))
		
		s_2_hat='g'^~s_2_dh //pub ecdhe key
		s_2_temp=pke^~s_2_dh //Z (shared ECDH secret)
		s_2=KDF_e(s_2_temp,'IDENTITY',s_2_hat,pke)		
		Q_N=<'SHA256',H_SHA256(Q_PD)>			//the name of the DAA key
		k_e=KDF_a(s_2,'STORAGE',Q_N)				
		k_h=KDF_a(s_2,'INTEGRITY','NULL')
		curlyK_2=curlyK(~K_2)
		curlyK_2_hat=senc(curlyK_2,k_e)
		curlyH=MAC(<len16(curlyK_2_hat),curlyK_2_hat, Q_N>,k_h)
		C_hat=senc(<A,B,C,D,u,j>,curlyK_2)

		// for import; change rnd seed to ecdh seed?
		seed_3_enc='g'^~seed_3_dh //pub ecdhe key
		seed_3_temp=pke^~seed_3_dh //Z
		seed_3=KDF_e(seed_3_temp,'DUPLICATE',seed_3_enc,pke)		
		sk_SENSITIVE=<'TPM_ALG_KEYEDHASH', 'NULL', ~obfuscationValue, ~sk_emaid>
		sk_unique=H_SHA256(<~obfuscationValue, ~sk_emaid>)
		sk_PD=<'SK_EMAID_public_data', sk_unique>
		sk_N=<'SHA256',H_SHA256(sk_PD)>
		sk_k_e=KDF_a(seed_3,'STORAGE',sk_N)				
		sk_k_h=KDF_a(seed_3,'INTEGRITY','NULL')
		sk_SENSITIVE_enc=senc(sk_SENSITIVE,sk_k_e)
		sk_SENSITIVE_hmac=MAC(<sk_SENSITIVE_enc, sk_N>,sk_k_h)
		sk_DUP=<sk_PD, sk_SENSITIVE_hmac, sk_SENSITIVE_enc, seed_3_enc>

		EMSP_Cert=<I,pkX,pkY>

		m_out=<EMSP_Cert, curlyH, len16(curlyK_2_hat), curlyK_2_hat, s_2_hat, C_hat, sk_DUP, 'Host_CompleteJoin'>
	in
     [ In_S_B(CPS_I, I, < reqID, m, 'CPS_Fwd_Req'>)
		, !Pk(I,pkX,pkY)
		, !Ltk(I,~x,~y)
		, Fr(~r)
		, Fr(~l)
		, Fr(~s_2_dh)
		, Fr(~K_2)
		, Fr(~sk_emaid)
		, Fr(~seed_3_dh)
		, Fr(~obfuscationValue) // for import
		, !PkCPS(CPS_I,pk(~cps))
	 ] 
	 --[ Running(I, pke, <A, B, C, D>)
		, Alive(I)							//the issuer is "alive" in the protocol here
		, Honest ( I )
		, Honest ( pke )
		, Honest ( CPS_I )
		, Role ('Issuer')
		, IssuerReceivedKeys(I, pke, pk(pc), Q_PD)
		, Secret_EMAID(I, pke, ~sk_emaid)
		, Secret_Cred(I, pke, <A,B,C,D>)
		, Iss_s2(s_2, e)
		, Iss_s22(s_2, e, pke, <A,B,C,D>, curlyK_2, k_e)
		, Iss_s3(seed_3, e)
		, Iss_m(m_out)
		, OnlyOnce('Issuer_Verify_Challenge')
		]->
	 [Out_S_B(I, CPS_I, <reqID, m_out>) 
	 , !Issuer_EMAID_SK(I, ~sk_emaid)
	 ]	

//Modification: added simpe rule to leak the EMAID secret key:
rule Issuer_EMAID_Reveal:
	[!Issuer_EMAID_SK(I, sk_emaid)]	
	--[
		KeyReveal('Issuer_EMAID_Reveal', I)
	  ]->	
	[Out(sk_emaid)]


//The CPS signes the credential response and forwards it to the CP
//Modification: Added signature over the credential response by the CPS
rule CPS_Sign_Credential_Response:
	let 
		//inputs
		EMSP_Cert=<I,pkX,pkY>
		//sk_DUP=<sk_PD, sk_SENSITIVE_hmac, sk_SENSITIVE_enc, seed_3_enc>
		m=<EMSP_Cert, curlyH, len16(curlyK_2_hat), curlyK_2_hat, s_2_hat, C_hat, sk_DUP, 'Host_CompleteJoin'>
		m_out=<EMSP_Cert, curlyH, len16(curlyK_2_hat), curlyK_2_hat, s_2_hat, C_hat, sk_DUP, res_n, 'Host_CompleteJoin'>
		//sign response
		sig_m=sign(m_out,~cps)		
	in
     [ In_S_B(I, CPS_I, <reqID, m>)
		//, !PkCPS(CPS_I,pk(~cps))
		, !LtkCPS(CPS_I,~cps)
		, CPS_State_01(CPS_I, CP, reqID, res_n)
	 ]
	 --[ CPSReceivedRes(CPS_I)
	 	, CPSsignedRes(CPS_I, ~cps, m_out)
	 	, CPSsignedRes2(CPS_I, ~cps, m_out, sig_m)
		, OnlyOnce('CPS_Sign_Credential_Response')
		]->
	 [Out_S_B(CPS_I, CP, < m_out, sig_m>) ]	

//The CP forwards the received data to the host/EV
rule CP_Fwd_Credential_Res:
	let	
		EMSP_Cert=<I,pkX,pkY>
		sk_DUP=<sk_PD, sk_SENSITIVE_hmac, sk_SENSITIVE_enc, seed_3_enc>
		m=<EMSP_Cert, curlyH, len16(curlyK_2_hat), curlyK_2_hat, s_2_hat, C_hat, sk_DUP, res_n, 'Host_CompleteJoin'>
		m_emsp=<EMSP_Cert, curlyH, len16(curlyK_2_hat), curlyK_2_hat, s_2_hat, C_hat, sk_DUP, 'Host_CompleteJoin'>
		//sig_m=sign(m,cps)	
	in
     [ In_S_B(CPS_I, $CP, < m, sig_m>)
		, !CP_State_01($CP, CPS_I, sid, $AS)
		, !PkCPS(CPS_I, pk(cps)) 
	 ]
	 --[  CP_Fwd_Credential_Res($CP, CPS_I, sid)
		, OnlyOnce('CP_Fwd_Credential_Res')
		, CP_C_hat(C_hat)
		, CP_m(m)
		, CP_m2(sid, m_emsp, sig_m, res_n)
		]->
	 [Out(<sid, m, sig_m, 'CP_Fwd_Res'>)]	

//The host verifies the signature and freshness of the received credential response,
// asking the TPM to decrypt and import the EMAID secret key (contained in sk_DUP)
// as well as the DAA credential
//Modification: Added verification of the CPS' signature
rule Host_Passthrough_2:
	let
		//inputs
		EMSP_Cert=<I,pkX,pkY>
		sk_DUP=<sk_PD, sk_SENSITIVE_hmac, sk_SENSITIVE_enc, seed_3_enc>
		m=<EMSP_Cert, curlyH, len16(curlyK_2_hat), curlyK_2_hat, s_2_hat, C_hat, sk_DUP, ~res_n, 'Host_CompleteJoin'>

		activateData=<Q_SD, Q_PD, curlyH, len16(curlyK_2_hat), curlyK_2_hat, s_2_hat>
	in
	[ In(<sid, m, sig_m, 'CP_Fwd_Res'>)
	  	, !Host_State_05($PS, $AS, pke, pk(cps), CPS_I, sid,  CP, ~res_n)
	  	, !Host_Store_DAAKey($PS, $AS, pke, Q_PD, Q_SD)
		, !PkCPS(CPS_I, pk(cps)) // from Charge Station but authentic due to certificate
	 ]
	--[ Eq(verify(sig_m,m,pk(cps)), true)
		, Passthrough_ActivateCred2($PS, $AS)
		, Passthrough_ActivateCred2_sid($PS, $AS, sid)
		, Passthrough_m($PS, $AS, m, pk(cps))
		, OnlyOnceCertRes(<$PS, $AS, Q_PD, I, sid>)
		, OnlyOnce('Host_Passthrough_2')
	  ]->
	[ Out_S($AS,$PS,< sk_DUP, 'TPM2_Import'>)
	, Out_S($AS,$PS,< Q_SD, Q_PD, curlyH, len16(curlyK_2_hat), curlyK_2_hat, s_2_hat, 'TPM2_ActivateCredentials_2'>)
	, Host_State_06($PS, $AS,  pke, Q_PD, C_hat, activateData,  CP,  EMSP_Cert, CPS_I, sk_PD)
	]
//====================================================
// Contract Credential Installation Response Handling
//====================================================
//The TPM decrypts the EMAID secret key and returns it to the host/EV
//Modification: Added TPM2_Import
rule TPM2_Import:
	let
		//sk_unique=H_SHA256(<obfuscationValue, sk_emaid>)
		//sk_PD=<'SK_EMAID_public_data', sk_unique>
		sk_SENSITIVE=<'TPM_ALG_KEYEDHASH', 'NULL', obfuscationValue, sk_emaid>
		sk_SENSITIVE_enc=senc(sk_SENSITIVE,sk_k_e)
		sk_DUP=<sk_PD, sk_SENSITIVE_hmac, sk_SENSITIVE_enc, seed_3_enc>

		seed_3_rec_temp=seed_3_enc^e
		seed_3_rec=KDF_e(seed_3_rec_temp,'DUPLICATE',seed_3_enc,pke)
		sk_N_rec=<'SHA256',H_SHA256(sk_PD)>
		sk_k_e_1=KDF_a(seed_3_rec,'STORAGE',sk_N_rec)				
		sk_k_h_1=KDF_a(seed_3_rec,'INTEGRITY','NULL')
		sk_SENSITIVE_hmac_1=MAC(<sk_SENSITIVE_enc, sk_N_rec>,sk_k_h_1)
		sk_SENSITIVE_rec=sdec(sk_SENSITIVE_enc,sk_k_e_1)
		sk_SD=senc(sk_SENSITIVE_rec,aes_key)
	in
	[  In_S($AS,$PS,< sk_DUP, 'TPM2_Import'>)
		, !TPM_AES_Key($PS, aes_key)
		, !TPM_ENDORSEMENT_SK($PS,e, pke)
	]
	--[
		Eq(sk_SENSITIVE_hmac_1,sk_SENSITIVE_hmac)
		, Eq(sk_k_e, sk_k_e_1)
		, EMAIDkey_Imported($PS, $AS)
		, Secret_Imported(pke, sk_emaid)
		, OnlyOnce('TPM2_Import')
	  ]->
	[ Out_S($PS,$AS, < sk_PD, sk_SD, 'ret_TPM2_Import'>)
		//, !TPM_EMAID_SK($PS, pke, sk_emaid)
	]

/*
//Modification: added simpe rule to leak the  EMAID secret key:
rule TPM_EMAID_Reveal:
	[!TPM_EMAID_SK($PS, pke, sk_emaid)]	
	--[
		KeyReveal('TPM_EMAID_Reveal', $PS)
		, KeyReveal('PKE_EMAID_Reveal', pke)
		, KeyRevealSK(sk_emaid)
	  ]->	
	[Out(sk_emaid)]
*/

/*
rule Host_Passthrough_3:
	let
		activateData=<Q_SD, Q_PD, curlyH, len16(curlyK_2_hat), curlyK_2_hat, s_2_hat>
	in
	[  In_S($PS,$AS, < sk_PD, sk_SD, 'ret_TPM2_Import'>)
	 	, Host_State_06($PS, $AS,  pke, Q_PD, C_hat, activateData,  CP,  EMSP_Cert, CPS_I)
		, !PkCPS(CPS_I, pk(cps))
	 ]
	--[  Passthrough_3($PS, $AS)
		, OnlyOnce('Host_Passthrough_3')
		, Host_C_hat(C_hat, pk(cps))
	  ]->
	[ Out_S($AS,$PS,< Q_SD, Q_PD, curlyH, len16(curlyK_2_hat), curlyK_2_hat, s_2_hat, 'TPM2_ActivateCredentials_2'>)
		, Host_State_07($PS, $AS,  pke, Q_PD, C_hat, sk_PD, sk_SD,  CP,  EMSP_Cert, CPS_I)
	]*/

//TPM decrypts DAA credential and returns them to host/EV
rule TPM2_ActivateCredential_2:
	let
		//unwrap the inputs where needed
		curlyK_2_hat=senc(curlyK(K_2),k_e)
		Q=multp(~f, 'P1')
		Q_PD=<'DAA_public_data', Q>
		
		//recompute
		s_2_rec_temp=s_2_hat^e						//retrieve s
		s_2_rec=KDF_e(s_2_rec_temp,'IDENTITY',s_2_hat,pke)	
		Q_N_rec=<'SHA256',H_SHA256(Q_PD)>		//calculate Q_N_rec which should be the same as Q_N
		k_e_1=KDF_a(s_2_rec,'STORAGE',Q_N_rec)		//calculate k_e_1 which should be the same as k_e	
		k_h_1=KDF_a(s_2_rec,'INTEGRITY','NULL')		//calculate k_h_1 which should be the same as k_h
		curlyH_1=MAC(<len16(curlyK_2_hat),curlyK_2_hat,Q_N_rec>,k_h_1)
		curlyK_2_rec=sdec(curlyK_2_hat,k_e_1)		
	in
	[  In_S($AS,$PS,< Q_SD, Q_PD, curlyH, len16(curlyK_2_hat), curlyK_2_hat, s_2_hat, 'TPM2_ActivateCredentials_2'>) 
		, !TPM_AES_Key($PS, aes_key)
		, !TPM_ENDORSEMENT_SK($PS,e, pke)
	]
	--[
		Eq(curlyH_1,curlyH)
		, Eq(k_e, k_e_1)
		, CurlyK2_recomputed($PS, $AS)
		, CurlyK2_r2($PS, $AS, curlyK_2_rec, k_e)
		, OnlyOnce('TPM2_ActivateCredential_2')
	  ]->
	[ Out_S($PS,$AS, < curlyK_2_rec, 'ret_TPM2_ActivateCredentials_2'>) ]

//The host/EV receives the sk_emaid and the DAA credential and competes the credential installation
rule Host_JoinComplete:
	let 
		//unwrap the inputs where needed
		curlyK_2_rec=curlyK(K_2_rec)

		sk_SENSITIVE=<'TPM_ALG_KEYEDHASH', 'NULL', obfuscationValue, sk_emaid>
		sk_SD=senc(sk_SENSITIVE,aes_key)
		
		//inputs from the issuer
		EMSP_Cert=<I,pkX,pkY>
		//pkX=PkX(x,'P2')
		//pkY=PkY(y,'P2')
		
		//input from Host_State
		Q=multp(~f, 'P1')
		Q_PD=<'DAA_public_data', Q>
		C_hat=senc(<A,B,C,D,u,j>,curlyK_2) //we decrypt these credentials by checking that curlyK_2_rec = curlyK_2

		//recompute the hash
		R_B_dash=calcRB(minus(multp(j,'P1'), multp(u,B)))
		R_D_dash=calcRD(minus(multp(j,Q),multp(u,D)))
		u_dash=H_n_8('P1',Q,R_B_dash,R_D_dash, A, B, C, D)
	in 
	[ In_S($PS,$AS, < curlyK_2_rec, 'ret_TPM2_ActivateCredentials_2'>)
		, In_S($PS,$AS, < sk_PD, sk_SD, 'ret_TPM2_Import'>)
		, Host_State_06($PS, $AS,  pke, Q_PD, C_hat, activateData,  CP,  EMSP_Cert, CPS_I, sk_PD)
		//, Host_State_07($PS, $AS,  pke, Q_PD, C_hat, sk_PD, sk_SD,  CP,  EMSP_Cert, CPS_I)
	 	, !PkCPS(CPS_I, pk(cps))
	]
	 --[ 
	     Eq(curlyK_2, curlyK_2_rec) //this allows C_hat to be decrypted
		, Eq(u,u_dash) 
		, Eq(verifyCre1(A,pkY,B,'P2'),accept)
		, Eq(verifyCre2(A,D,pkX,C,'P2'),accept)
		, JoinCompleted($PS, $AS, pke)
		, Commit(pke, I, <A, B, C, D>)
		, Role ('Platform')
		, Secret(pke, I, <A, B, C, D> )
		, Secret_EMAID_EV(pke, I, sk_emaid )
		, Honest ( $PS )
		, Honest ( $AS )
		, Honest ( pke )
		, Honest ( I )
		, Honest ( CPS_I )
		, Honest ( CP )
		, Host_C_hat2(C_hat, pk(cps))
		, OnlyOnce('Host_JoinComplete')
		]->
	 [ Host_Org_Creds($PS, $AS, pke, A, B, C, D)
	 ]

// The following rule allows a host to leak the credentials
rule Host_CredentialsReveal:
	[Host_Org_Creds($PS, $AS, pke, A, B, C, D)]
	--[ KeyReveal('Host_OrgCred_Reveal', $AS)
	   , KeyReveal('TPM_OrgCred_Reveal', $PS)
	   , KeyReveal('PKE_OrgCred_Reveal', pke)
	  ]->
	[Out(<A, B, C, D>)]

//=============================================================	 
// Credential Installation Completed
//=============================================================	 



//======================================================================================
// This concludes the description of the scheme using Tamarin rules
// The next section contains the lemmas encapsulating the various properties that we
// want the scheme to have.
//======================================================================================

//======================================================================================
// We start off with some general checks that verify some of the restriction/assumptions
// which we have imposed on our scheme.
//======================================================================================

// Helper lemma for proof generation
lemma forwarded_credential_res_source [sources]:
// Information contained in a credential response forwarded by a CP is either generated as intended by the protocol
// or already known to the adversary
"

(All sid m sig res_n #i .
   // For each credential response forwarded to the EV by the CP
	(
		CP_m2(sid, m, sig, res_n) @ i
		==>
		(
			(Ex  CP n  #j1 #j4 . // CPS_I #j3
				// The CP has send a nonce (timestamp) to the EV previously
				CP_Nonce(CP, n, sid) @ j1 & j1<i // < j3
				& ( 
					// and the nonce contained in the response was generated as part of a credential request by the hos
					 (Ex #j2 . Store_DAA_resn(res_n) @ #j2 & #j2<#i)
					 // or was already known to the adversary
					 | (Ex  #kr . KU(res_n) @ kr & #kr<#i)
				  )
				// and the message of the credential response was previously send by an issuer
				& Iss_m(m) @ j4 & j4<i
			)
			// or the adversary already knew the complete credential response before it was forwarded by the CP
			| (Ex  #kr . KU(<sid, m, sig, res_n>) @ kr & kr<i)
		)
	)
)
"

lemma secrecy_of_cps_private_key [reuse]: 
"
// The adversary can only learn the CPS's secret key via the CPS_KeyReveal rule
All CPS_I cps #i #i2 .
	(
		CPS_Init2(CPS_I, cps) @ i & KU(cps) @ i2
		==>
		(
			(Ex  #kr . KeyReveal('CPS_KeyReveal', CPS_I) @ kr & i<kr & kr<i2)
		)
	)
"


lemma secrecy_of_pke [reuse]: //maybe
"
// The adversary can only learn the public endorsement key of a tpm via the TPM_EKReveal rule
All e pke #i #i2 .
	(
		TPM2_EK_Created2( e, pke) @ i & KU(e) @ i2
		==>
		(
			(Ex  #kr . KeyReveal('TPM_EKReveal_pke', pke) @ kr & i<kr)
		)
	)
"



lemma integrity_of_forwarded_credential_res_m [reuse]:
"
// The adversary cannot manipulate or forge credential response messages send by the CP to the host/EV
// without having compromised the CPS's private key
All TPM Host m cps #i .
	(
		Passthrough_m(TPM, Host, m, pk(cps)) @ i
		==>
		(
			(Ex  #j . CP_m(m) @ j & j<i)
			| (Ex  #j . KU(cps) @ j & j<i)			
		)
	)
"
// When a TPM and Host are bound together, they have to be different entities
lemma restriction_bind:
"
	All Ent1 Ent2 #i . 
		(Bind(Ent1, Ent2) @ i)
		==>
		(not(Ent1=Ent2))
"

//a TPM is associated with one and only one host:
lemma restriction_one_host_per_tpm:
"
	All TPM Host1 Host2 #i #j. 
		(Bind(Host1,TPM)@i & Bind(Host2,TPM)@j)
		==> 
		((#i=#j))
"

//a host has only one TPM:
lemma restriction_one_tpm_per_host:
"
	All TPM1 TPM2 Host #i #j. 
		((Bind(Host,TPM1)@i & Bind(Host,TPM2)@j)
		==>
		(#i=#j))
"

// any endorsement key that is presented to the issuer must have been generated
// by a TPM. This restrictions represents the fact that the issuer will check
// each endorsement key and only allow ones that were created by a TPM
lemma restricition_pke_comes_from_tpm:
	"All pke #i . 
		Check_Ek(pke) @ i 
		==>
		(
			(Ex TPM Host #j .
				(
					TPM2_EK_Created(TPM, Host, pke) @ j
					& 
					(#j<#i)
				)
			)
		)
	"

//======================================================================================
// Correctness properties
//======================================================================================

// the issuer should be able to check at least 2 pkes
lemma correctness_verify_multiple_pkes: exists-trace
	"Ex Issuer TPM1 TPM2 Host1 Host2 pke1 qpd1 pcpd1 pcpd2 pke2 qpd2  #t1 #t2 #t3 #t4 .
		PlatformSendKeys(TPM1, Host1, pke1, qpd1, pcpd1) @ t1
		& PlatformSendKeys(TPM2, Host2, pke2, qpd2, pcpd2) @ t2
		& IssuerReceivedKeys(Issuer, pke1, pcpd1, qpd1) @ t3
		& IssuerReceivedKeys(Issuer, pke2, pcpd2, qpd2) @ t4
		& not(pke1=pke2) 
		& not(qpd1=qpd2) 
		& not(pcpd1=pcpd2) 
		& not(Host1=Host2) 
		& not(TPM1=TPM2)

		//we had no key reveal
		& not( Ex RevealEvent ENTITY #k1 . KeyReveal(RevealEvent, ENTITY)@k1) 
	"

// the issuer should be able to check at least 2 pkes from different Issuers
lemma correctness_verify_multiple_pkes_diff_I: exists-trace
	"Ex Issuer1 Issuer2 TPM Host pke1 qpd1 pcpd1 pcpd2 pke2 qpd2  #t1 #t2 #t3 #t4 .
		PlatformSendKeys(TPM, Host, pke1, qpd1, pcpd1) @ t1
		& PlatformSendKeys(TPM, Host, pke2, qpd2, pcpd2) @ t2
		& IssuerReceivedKeys(Issuer1, pke1, pcpd1, qpd1) @ t3
		& IssuerReceivedKeys(Issuer2, pke2, pcpd2, qpd2) @ t4
		& (pke1=pke2) 
		& not(Issuer1=Issuer2)

		//we had no key reveal
		& not( Ex RevealEvent ENTITY #k1 . KeyReveal(RevealEvent, ENTITY)@k1) 
	"

//======================================================================================
// In the following section we look at the security properties required for 
// the credential installation process
//======================================================================================


// Correctness of the credential request
lemma correctness_credential_req: exists-trace
" Ex TPM Host I CPS_I CP pke sigReq //n
					#t01 #t01_2 #t01_3 #t02 #t03 #t04 #t05 #t06 #t07 #t08 #t09
					#t10 #t11 #t12
.
					
//we initiated at least one entity for each protocol role
	Issuer_Init(I) @ t01 
	& CPS_Init(CPS_I) @ t01_2
	& CP_Init(CP) @ t01_3
	& Host_Init(Host) @ t02 
	& TPM_Init(TPM) @ t03 
	
	//we had no key reveal
	& not( Ex RevealEvent ENTITY #k1 . KeyReveal(RevealEvent, ENTITY)@k1) 
		
	//we had a successful Platform set-up
	 & Bind(TPM, Host) @ t04 
	 
	//created an endorsement key EK
	& TPM2_EK_Created(TPM, Host, pke) @ t05 
	
	//stored the EK with the host	
	& Store_EK(TPM, Host) @ t06

	//created an provisioning key PC
	& TPM2_PC_Created(TPM, Host) @ t07
	
	// stored the provisioning key with the host
	& Store_PC(TPM, Host) @ t08

	//created a DAA key
	& TPM2_DAA_Created(TPM, Host) @ t09	
	
	//stored the DAA key with the host	
	& Store_DAA(TPM, Host) @ t10

	//signed a credential request 
	& TPM2_Sign(TPM, Host) @ t11

	//send the credential request to the CP
	& RunningEV(pke, CPS_I, sigReq) @ t12
	
	// With the correct temporal ordering
	& t01<t02 //Issuer gets created before Host
	& t01_2<t02 //CPS gets created before Host
	& t01_3<t02 //CP gets created before Host
	& t02<t04 //Host gets created before Bind
	& t03<t04 //TPM get created before Bind
	& t04<t05 
	& t05<t06 
	& t06<t07 
	& t07<t08 
	& t08<t09
	& t09<t10
	& t10<t11
	& t11<t12

  //restrict the trace further by preventing each rule from firing more than once
	& 	(All event #i #j . OnlyOnce(event)@i & OnlyOnce(event)@j ==> #i=#j)
"


// Correctness of the credential request and response until received by host
lemma correctness_credential_req_res_1: exists-trace
" Ex TPM Host I CPS_I CP pke pcpd qpd sigReq sid n
					#t01 #t01_2 #t01_3 #t02 #t03 #t04 #t05 
					#t06 #t07 #t08 #t09 #t09_2
					#t10 #t11 #t12 #t12_2 #t13 
					#t14 #t15 #t16 #t17
.		
	//we initiated at least one entity for each protocol role
	Issuer_Init(I) @ t01 
	& CPS_Init(CPS_I) @ t01_2
	& CP_Init(CP) @ t01_3
	& Host_Init(Host) @ t02 
	& TPM_Init(TPM) @ t03 
	
	//we had no key reveal
	& not( Ex RevealEvent ENTITY #k1 . KeyReveal(RevealEvent, ENTITY) @ k1) 

	//we had a successful Platform set-up
	 & Bind(TPM, Host) @ t04 
	 
	//created an endorsement key EK
	& TPM2_EK_Created(TPM, Host, pke) @ t05 
	
	//stored the EK with the host	
	& Store_EK(TPM, Host) @ t06

	//created an provisioning key PC
	& TPM2_PC_Created(TPM, Host) @ t07
	
	// stored the provisioning key with the host
	& Store_PC(TPM, Host) @ t08

	//created a DAA key
	& TPM2_DAA_Created(TPM, Host) @ t09
	
	//sent a nonce (timestamp) from the CP to the host
	& CP_Nonce(CP, n, sid) @ t09_2
	
	//stored the DAA key with the host
	& Store_DAA_N(TPM, Host, n, sid) @ t10

	//signed a credential request 
	& TPM2_Sign(TPM, Host) @ t11

	//send the credential request to the CP
	& RunningEV(pke, CPS_I, sigReq) @ t12

	//forwarded the request from the CP to the CPS
	& CP_Fwd_Credential_Request(CP, CPS_I, n) @ t12_2
	
	//received and verified the request as the CPS
	& CPSReceivedReq(CPS_I, pke, pcpd, qpd) @ t13

	//recieved and answered the credential request as the issuer
	& IssuerReceivedKeys(I, pke, pcpd, qpd) @ t14
	
	//received the issuers response as the CPS and forwarded it to the CP
	& CPSReceivedRes(CPS_I)	@ t15

	//received the credential response as the CP and forwarded it to the host
	& CP_Fwd_Credential_Res(CP, CPS_I, sid)	@ t16

	// received the response by the host
	& Passthrough_ActivateCred2_sid(TPM, Host, sid) @ t17

	// With the correct temporal ordering
	& t01<t02 //Issuer gets created before Host
	& t01_2<t02 //CPS gets created before Host
	& t01_3<t02 //CP gets created before Host
	& t02<t04 //Host gets created before Bind
	& t03<t04 //TPM get created before Bind
	& t04<t05 
	& t05<t06 
	& t06<t07 
	& t07<t08 
	& t08<t09
	& t09_2<t10
	& t09<t10
	& t10<t11
	& t11<t12
	& t12_2<t13
	& t12<t13
	& t13<t14
	& t14<t15
	& t15<t16
	& t16<t17

  //restrict the trace further by preventing each rule from firing more than once
	& 	(All event #i #j . OnlyOnce(event)@i & OnlyOnce(event)@j ==> #i=#j)
"


// Correctness of the complete credential installation process
lemma correctness_credential_req_res_2: exists-trace
" Ex TPM Host I CPS_I CP pke pcpd qpd sigReq sid n
					#t01 #t01_2 #t01_3 #t02 #t03 #t04 #t05 
					#t06 #t07 #t08 #t09 #t09_2
					#t10 #t11 #t12 #t12_2 #t13 
					#t14 #t15 #t16 #t17
					#t18 //#t19 
					#t20 #t21
.
					
	//we initiated at least one entity for each protocol role
	Issuer_Init(I) @ t01 
	& CPS_Init(CPS_I) @ t01_2
	& CP_Init(CP) @ t01_3
	& Host_Init(Host) @ t02 
	& TPM_Init(TPM) @ t03 
	
	//we had no key reveal
	& not( Ex RevealEvent ENTITY #k1 . KeyReveal(RevealEvent, ENTITY)@k1) 

	//we had a successful Platform set-up
	 & Bind(TPM, Host) @ t04 
	 
	//created an endorsement key EK
	& TPM2_EK_Created(TPM, Host, pke) @ t05 
	
	//stored the EK with the host	
	& Store_EK(TPM, Host) @ t06

	//created an provisioning key PC
	& TPM2_PC_Created(TPM, Host) @ t07

	// stored the provisioning key with the host
	& Store_PC(TPM, Host) @ t08

	//created a DAA key
	& TPM2_DAA_Created(TPM, Host) @ t09	

	//sent a nonce (timestamp) from the CP to the host
	& CP_Nonce(CP, n, sid) @ t09_2
	
	//stored the DAA key with the host
	& Store_DAA_N(TPM, Host, n, sid) @ t10

	//signed a credential request 
	& TPM2_Sign(TPM, Host) @ t11

	//send the credential request to the CP
	& RunningEV(pke, CPS_I, sigReq) @ t12

	//forwarded the request from the CP to the CPS
	& CP_Fwd_Credential_Request(CP, CPS_I, n) @ t12_2
	
	//received and verified the request as the CPS
	& CPSReceivedReq(CPS_I, pke, pcpd, qpd) @ t13

	//recieved and answered the credential request as the issuer
	& IssuerReceivedKeys(I, pke, pcpd, qpd) @ t14
	
	//received the issuers response as the CPS and forwarded it to the CP
	& CPSReceivedRes(CPS_I)	@ t15

	//received the credential response as the CP and forwarded it to the host
	& CP_Fwd_Credential_Res(CP, CPS_I, sid)	@ t16

	// received the response by the host
	& Passthrough_ActivateCred2_sid(TPM, Host, sid) @ t17
	
	// imported the EMAID key into the TPM and returned it to the host
	& EMAIDkey_Imported(TPM, Host) @ t18
	
	//decrypted the DAA credentials in the TPM and returned them to the host
	& CurlyK2_recomputed(TPM, Host) @ t20

	//received the DAA and EMAID key as the host and completed the credential installation
	& JoinCompleted(TPM, Host, pke) @ t21

	// With the correct temporal ordering
	& t01<t02 //Issuer gets created before Host
	& t01_2<t02 //CPS gets created before Host
	& t01_3<t02 //CP gets created before Host
	& t02<t04 //Host gets created before Bind
	& t03<t04 //TPM get created before Bind
	& t04<t05 
	& t05<t06 
	& t06<t07 
	& t07<t08 
	& t08<t09
	& t09_2<t10
	& t09<t10
	& t10<t11
	& t11<t12
	& t12_2<t13
	& t12<t13
	& t13<t14
	& t14<t15
	& t15<t16
	& t16<t17
	& t17<t18
	& t18<t20
	& t20<t21

  //restrict the trace further by preventing each rule from firing more than once
	& 	(All event #i #j . OnlyOnce(event)@i & OnlyOnce(event)@j ==> #i=#j)
"




//======================================================================================
// In the following section we look at the security properties for
// the charge authorization and charge data authorization in our model
//======================================================================================

// *********** SR2 Secure Credential Installation - Authentication *********************
// The following lemmas go through aspects of Lowe's Authentication hierarchy. 

// Aliveness of Host
// This lemma simply shows that at the end of a run of the protocol, an issuer must have been created previously.
// But it does not require the issuer to have been involved in the running of the protocol at all.
// This is a very weak requirement.
lemma auth_aliveness_issuer_very_weak:
"
All Iss pke n #i .
   // For all commited JOIN sessions running between a platform and issuer on the term(s) n 
	(
		Commit( pke, Iss, n ) @ i
		==>
		(
			// Implies that there is an active issuer
			(Ex #j . Issuer_Init (Iss) @ j)
			|
			// or there has been a key reveal 
			(Ex RevealEvent Entity #kr . KeyReveal(RevealEvent, Entity) @ kr & Honest(Entity) @ i)
		)
	)
"

//this lemma shows that the issuer was involved in a run of the protocol (at least one of its rules fired) 
//or that some of the keys of the entities involved were revealed
lemma auth_aliveness_issuer:
"
All Iss pke n1 #i .
   // For all commited JOIN sessions running between a platform and issuer on the term(s) n 
	(
		Commit( pke, Iss, n1 ) @ i 

	==> // Implies that an issuer has previously been involved in a protocol run 
       
		(	(Ex #k . Alive (Iss) @ k)
			|
			// or there has been a key reveal of one of the entities involved in the Commit 
			(Ex RevealEvent Entity #kr . KeyReveal(RevealEvent, Entity) @ kr & Honest(Entity) @ i)
		)
	)

"

// This lemma simply shows that at the end of a run of the protocol for the issuer with a host, 
// the host must have been involved in a run of a protocol that lead up to the final Issuer rule firing
lemma auth_aliveness_host:
"
All Iss pke n #i .
	// For all JOIN sessions running between an issuer and a host, Host, on the term(s) n
	(
		( Running(Iss, pke,  n) @ i )
		==>
		(	//the host was involved in a run of the protocol that 
			//resulted in a message exchange between the host and the issuer
			(Ex Host2 #k . Alive(Host2) @ k & Role('Platform') @ k)
			|
			// or there has been a key reveal of one of the entities involved in the Commit 
			(Ex RevealEvent Entity #kr . KeyReveal(RevealEvent, Entity) @ kr & Honest(Entity) @ i)	
		)
	)
	"

// Agreement checks 

// This lemma guarantees that whenever the host completes a run of the protocol, apparently with the issuer, 
// then the issuer has previously been running the protocol, apparently with the host.
// This lemma (and also the subsequent agreement lemmas) fails if the endorsement key is not included in
// the str constructed in the Host_ReceiveCurlyK rule which then forms part of the hash p computed in
// Host_ReceiveEAndCV.
lemma auth_weak_agreement_host:
"
All Iss TPM Host pke n1 #i .
	(
		(
			// For all commited JOIN sessions running between a platform and issuer on the term(s) n 
			Commit( pke, Iss, n1 ) @ i & JoinCompleted(TPM, Host, pke) @ i
		)
		==>
        ( 
			// Implies there exists a running issuer on some term(s) n2 
			(Ex n2 #j . Running( Iss, pke, n2 ) @ j) 
			|
			// or there has been a key reveal of one of the entities involved in the Commit 
			(Ex RevealEvent Entity #kr . KeyReveal(RevealEvent, Entity) @ kr & Honest(Entity) @ i)
		)
	)
"

// This lemma shows that the Host is in a non-injective agreement with the issuer on
// the credentials if, whenever the Host completes a run of the protocol, apparently with the Issuer, 
// then the Issuer has previously been running the protocol, apparently with the Host, and
//the two principals agreed on the terms n
lemma auth_non_injective_agreement_host_issuer:
"
All Iss pke n #i .
	(
		(
			// For all commited JOIN sessions running between a platform (identified by is endorsement public key pke) and issuer on the term(s) n 
			Commit( pke, Iss, n ) @ i 
		)
		==>
		(
			// Implies there exists a running issuer on the same term
			(Ex #j . Running( Iss, pke, n ) @ j) 
			|
			// or there has been a key reveal of one of the entities involved in the Commit
			(Ex RevealEvent Entity #kr . KeyReveal(RevealEvent, Entity) @ kr & Honest(Entity) @ i)
		)
	)
"
// This lemma shows that the Host is in an injective agreement with the issuer on
// the credentials if, whenever the Host completes a run of the protocol, apparently with the Issuer, 
// then the Issuer has previously been running the protocol, apparently with the Host, and
//the two principals agreed on the terms n
// Additionally, there is a unique matichng partner instance for each completed run of an agent.
lemma auth_injective_agreement_host_issuer:
"
All Iss pke n #i .
	(
		(
			// For all commited JOIN sessions running between a platform (identified by is endorsement public key pke) and issuer on the term(s) n 
			Commit( pke, Iss, n ) @ i 
		)
		==>
		(
			(
				// Implies there exists a running issuer on the same term
				(Ex #j . 
					(
						Running( Iss, pke, n ) @ j
						& 
						(#j<#i) 
						&
						// And each commited JOIN session corresponds to a unique issuer run
						( not(
							Ex Iss2 pke2 #i2 . ( Commit( pke2, Iss2, n) @ i2 & not(#i2=#i) )
							)
						)
					)
				)
			)
			|
			// or there has been a key reveal of one of the entities involved in the Commit
			(Ex RevealEvent Entity #kr . KeyReveal(RevealEvent, Entity) @ kr & Honest(Entity) @ i)
		)
	)
"

// This lemma shows that the CPS is in a non-injective agreement with the Host on
// a credential request if, whenever the CPS receives a credential request, apparently from the Host, 
// then the Host has previously send a credential request to the CPS, and
//the two principals agreed on the terms n
lemma auth_non_injective_agreement_CPS_EV: //requires signed_m=<m, pk(~cps)> for inclusion of CPS' public key
"
All CPS pke n #i .
	(
		(
			// For all credential requests received by the CPS, apparently from the platform identified by pke and with the term(s) n 
			CommitCPS( CPS, pke, n )  @ i 
		)
		==>
		(
			// Implies there exists a running platform that send a credential request with the same term(s)
			(Ex #j . RunningEV( pke, CPS, n )  @ j) 
			|
			// or there has been a key reveal of one of the involved entities
			(Ex RevealEvent Entity #kr . KeyReveal(RevealEvent, Entity) @ kr & Honest(Entity) @ i)
		)
	)
"
// This lemma shows that the CPS is in an injective agreement with the Host on
// a credential request if, whenever the CPS receives a credential request, apparently from the Host, 
// then the Host has previously send a credential request to the CPS, and
//the two principals agreed on the terms n
// Additionally, there is a unique matichng partner instance for each completed run of an agent.
lemma auth_injective_agreement_CPS_EV:
"
All CPS pke n #i .
	(
		(
		// For all credential requests received by the CPS, apparently from the platform identified by pke and with the term(s) n 
			CommitCPS( CPS, pke, n ) @ i 
		)
		==>
		(
			(
				(Ex #j . 
					(
					// Implies there exists a running platform that send a credential request with the same term(s)
						RunningEV( pke, CPS, n ) @ j
						& 
						(#j<#i) 
						&
						// and each receive event corresponds to a unique credential request send by the host
						( not(
							Ex CPS2 pke2 #i2 . ( CommitCPS( CPS2, pke2, n) @ i2 & not(#i2=#i) )
							)
						)
					)
				)
			)
			|
			// or there has been a key reveal of one of the involved entities
			(Ex RevealEvent Entity #kr . KeyReveal(RevealEvent, Entity) @ kr & Honest(Entity) @ i)
		)
	)
"

// ************* SR2 Secure Credential Installation - Secrecy ************************


// CRE secrecy from the perspective of both the Issuer and the TPM/Host.
lemma auth_secrecy_cre_ev:
"
  All Iss pke cred #i. 
    // somebody claims to have setup a shared secret, 
    Secret( pke, Iss, cred ) @ i
    ==>
		// implies the adversary does not know it 
		not( Ex #k. K( cred ) @ k )
		|
		// or it is the case that a key has been revealed 
		(Ex RevealEvent Entity #kr . KeyReveal(RevealEvent, Entity) @ kr & Honest(Entity) @ i)
"
lemma auth_secrecy_cre_iss:
"
  All Iss pke cred #i. 
    // somebody claims to have setup a secret credential, 
    Secret_Cred( Iss, pke, cred ) @ i
    ==>
		// implies the adversary does not know it 
		not( Ex #k. K( cred ) @ k )
		|
		// or it is the case that a key has been revealed 
		(Ex RevealEvent Entity #kr . KeyReveal(RevealEvent, Entity) @ kr & Honest(Entity) @ i)
"

// EMAID secrecy from the perspective of both the Issuer and the TPM/Host.
lemma auth_secrecy_emaid_iss:
"
  All  Iss pke sk_emaid #i. 
    // somebody claims to have setup a shared secret, 
    Secret_EMAID( Iss, pke, sk_emaid ) @ i
    ==>
		// implies the adversary does not know it 
		not( Ex #k. K( sk_emaid ) @ k )
		|
		// or it is the case that a key has been revealed 
		(Ex RevealEvent Entity #kr . KeyReveal(RevealEvent, Entity) @ kr & Honest(Entity) @ i)
"
lemma auth_secrecy_emaid_ev:
"
  All  Iss pke sk_emaid #i. 
    // somebody claims to have setup a shared secret, 
    Secret_EMAID_EV( pke, Iss, sk_emaid ) @ i
    ==>
		// implies the adversary does not know it 
		not( Ex #k. K( sk_emaid ) @ k )
		|
		// or it is the case that a key has been revealed 
		(Ex RevealEvent Entity #kr . KeyReveal(RevealEvent, Entity) @ kr & Honest(Entity) @ i)
"

end