SSIOT.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package protocols;
  2. import communication.Communication;
  3. import crypto.Crypto;
  4. import crypto.PRF;
  5. import crypto.PRG;
  6. import exceptions.NoSuchPartyException;
  7. import exceptions.SSIOTException;
  8. import measure.M;
  9. import measure.P;
  10. import measure.Timer;
  11. import oram.Forest;
  12. import oram.Metadata;
  13. import util.Util;
  14. public class SSIOT extends Protocol {
  15. public SSIOT(Communication con1, Communication con2) {
  16. super(con1, con2);
  17. }
  18. public void runE(PreData predata, byte[][] y, byte[] Nip1_pr, Timer timer) {
  19. timer.start(P.IOT, M.online_comp);
  20. // step 1
  21. int n = y.length;
  22. int l = y[0].length * 8;
  23. byte[][] x = new byte[n][];
  24. byte[][] e = new byte[n][];
  25. byte[][] v = new byte[n][];
  26. PRF F_k = new PRF(Crypto.secParam);
  27. F_k.init(predata.ssiot_k);
  28. PRF F_kprime = new PRF(Crypto.secParam);
  29. F_kprime.init(predata.ssiot_kprime);
  30. PRG G = new PRG(l);
  31. for (int i = 0; i < n; i++) {
  32. byte[] i_bytes = Util.intToBytes(i);
  33. x[i] = predata.ssiot_r.clone();
  34. for (int j = 0; j < Nip1_pr.length; j++)
  35. x[i][x[i].length - 1 - j] ^= Nip1_pr[Nip1_pr.length - 1 - j] ^ i_bytes[i_bytes.length - 1 - j];
  36. e[i] = Util.xor(G.compute(F_k.compute(x[i])), y[i]);
  37. v[i] = F_kprime.compute(x[i]);
  38. }
  39. timer.start(P.IOT, M.online_write);
  40. con2.write(e);
  41. con2.write(v);
  42. timer.stop(P.IOT, M.online_write);
  43. timer.stop(P.IOT, M.online_comp);
  44. }
  45. public void runD(PreData predata, byte[] Nip1_pr, Timer timer) {
  46. timer.start(P.IOT, M.online_comp);
  47. // step 2
  48. PRF F_k = new PRF(Crypto.secParam);
  49. F_k.init(predata.ssiot_k);
  50. PRF F_kprime = new PRF(Crypto.secParam);
  51. F_kprime.init(predata.ssiot_kprime);
  52. byte[] y = predata.ssiot_r;
  53. for (int i = 0; i < Nip1_pr.length; i++)
  54. y[y.length - 1 - i] ^= Nip1_pr[Nip1_pr.length - 1 - i];
  55. byte[] p = F_k.compute(y);
  56. byte[] w = F_kprime.compute(y);
  57. timer.start(P.IOT, M.online_write);
  58. con2.write(p);
  59. con2.write(w);
  60. timer.stop(P.IOT, M.online_write);
  61. timer.stop(P.IOT, M.online_comp);
  62. }
  63. public OutSSIOT runC(Timer timer) {
  64. timer.start(P.IOT, M.online_comp);
  65. // step 1
  66. timer.start(P.IOT, M.online_read);
  67. byte[][] e = con1.readObject();
  68. byte[][] v = con1.readObject();
  69. // step 2
  70. byte[] p = con2.read();
  71. byte[] w = con2.read();
  72. timer.stop(P.IOT, M.online_read);
  73. // step 3
  74. int n = e.length;
  75. int l = e[0].length * 8;
  76. PRG G = new PRG(l);
  77. OutSSIOT output = null;
  78. int invariant = 0;
  79. for (int i = 0; i < n; i++) {
  80. if (Util.equal(v[i], w)) {
  81. byte[] y = Util.xor(e[i], G.compute(p));
  82. output = new OutSSIOT(i, y);
  83. invariant++;
  84. }
  85. }
  86. if (invariant != 1)
  87. throw new SSIOTException("Invariant error: " + invariant);
  88. timer.stop(P.IOT, M.online_comp);
  89. return output;
  90. }
  91. @Override
  92. public void run(Party party, Metadata md, Forest forest) {
  93. Timer timer = new Timer();
  94. for (int j = 0; j < 100; j++) {
  95. int twoTauPow = 64;
  96. int label = 4;
  97. byte[][] y = new byte[twoTauPow][label];
  98. byte[] sE_Nip1_pr = new byte[1];
  99. byte[] sD_Nip1_pr = new byte[1];
  100. for (int i = 0; i < twoTauPow; i++)
  101. Crypto.sr.nextBytes(y[i]);
  102. int index = Crypto.sr.nextInt(twoTauPow);
  103. Crypto.sr.nextBytes(sE_Nip1_pr);
  104. sD_Nip1_pr[0] = (byte) (Util.intToBytes(index)[3] ^ sE_Nip1_pr[0]);
  105. PreData predata = new PreData();
  106. PreSSIOT pressiot = new PreSSIOT(con1, con2);
  107. if (party == Party.Eddie) {
  108. con1.write(sD_Nip1_pr);
  109. con2.write(y);
  110. con2.write(index);
  111. pressiot.runE(predata, twoTauPow, timer);
  112. runE(predata, y, sE_Nip1_pr, timer);
  113. } else if (party == Party.Debbie) {
  114. sD_Nip1_pr = con1.read();
  115. pressiot.runD(predata, timer);
  116. runD(predata, sD_Nip1_pr, timer);
  117. } else if (party == Party.Charlie) {
  118. y = con1.readObject();
  119. index = con1.readObject();
  120. pressiot.runC();
  121. OutSSIOT output = runC(timer);
  122. if (output.t == index && Util.equal(output.m_t, y[index]))
  123. System.out.println("SSIOT test passed");
  124. else
  125. System.err.println("SSIOT test failed");
  126. } else {
  127. throw new NoSuchPartyException(party + "");
  128. }
  129. }
  130. }
  131. }