SSIOT.java 3.9 KB

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