SSCOT.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.SSCOTException;
  8. import measure.M;
  9. import measure.P;
  10. import measure.Timing;
  11. import oram.Forest;
  12. import oram.Metadata;
  13. import util.Util;
  14. public class SSCOT extends Protocol {
  15. public SSCOT(Communication con1, Communication con2) {
  16. super(con1, con2);
  17. }
  18. public void runE(PreData predata, byte[][] m, byte[][] a, Timing time) {
  19. time.start(P.COT, M.online_comp);
  20. // step 1
  21. int n = m.length;
  22. int l = m[0].length * 8;
  23. byte[][] x = predata.sscot_r;
  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.sscot_k);
  28. PRF F_kprime = new PRF(Crypto.secParam);
  29. F_kprime.init(predata.sscot_kprime);
  30. PRG G = new PRG(l);
  31. for (int i = 0; i < n; i++) {
  32. for (int j = 0; j < a[i].length; j++)
  33. x[i][j] = (byte) (predata.sscot_r[i][j] ^ a[i][j]);
  34. e[i] = Util.xor(G.compute(F_k.compute(x[i])), m[i]);
  35. v[i] = F_kprime.compute(x[i]);
  36. }
  37. time.start(P.COT, M.online_write);
  38. con2.write(e);
  39. con2.write(v);
  40. time.stop(P.COT, M.online_write);
  41. time.stop(P.COT, M.online_comp);
  42. }
  43. public void runD(PreData predata, byte[][] b, Timing time) {
  44. time.start(P.COT, M.online_comp);
  45. // step 2
  46. int n = b.length;
  47. byte[][] y = predata.sscot_r;
  48. byte[][] p = new byte[n][];
  49. byte[][] w = new byte[n][];
  50. PRF F_k = new PRF(Crypto.secParam);
  51. F_k.init(predata.sscot_k);
  52. PRF F_kprime = new PRF(Crypto.secParam);
  53. F_kprime.init(predata.sscot_kprime);
  54. for (int i = 0; i < n; i++) {
  55. for (int j = 0; j < b[i].length; j++)
  56. y[i][j] = (byte) (predata.sscot_r[i][j] ^ b[i][j]);
  57. p[i] = F_k.compute(y[i]);
  58. w[i] = F_kprime.compute(y[i]);
  59. }
  60. time.start(P.COT, M.online_write);
  61. con2.write(p);
  62. con2.write(w);
  63. time.stop(P.COT, M.online_write);
  64. time.stop(P.COT, M.online_comp);
  65. }
  66. public OutSSCOT runC(Timing time) {
  67. time.start(P.COT, M.online_comp);
  68. // step 1
  69. time.start(P.COT, M.online_read);
  70. byte[][] e = con1.readObject();
  71. byte[][] v = con1.readObject();
  72. // step 2
  73. byte[][] p = con2.readObject();
  74. byte[][] w = con2.readObject();
  75. time.stop(P.COT, M.online_read);
  76. // step 3
  77. int n = e.length;
  78. int l = e[0].length * 8;
  79. PRG G = new PRG(l);
  80. OutSSCOT output = null;
  81. int invariant = 0;
  82. for (int i = 0; i < n; i++) {
  83. if (Util.equal(v[i], w[i])) {
  84. byte[] m = Util.xor(e[i], G.compute(p[i]));
  85. output = new OutSSCOT(i, m);
  86. invariant++;
  87. }
  88. }
  89. if (invariant != 1)
  90. throw new SSCOTException("Invariant error: " + invariant);
  91. time.stop(P.COT, M.online_comp);
  92. return output;
  93. }
  94. @Override
  95. public void run(Party party, Metadata md, Forest forest) {
  96. Timing time = new Timing();
  97. for (int j = 0; j < 100; j++) {
  98. int n = 100;
  99. int A = 32;
  100. int FN = 5;
  101. byte[][] m = new byte[n][A];
  102. byte[][] a = new byte[n][FN];
  103. byte[][] b = new byte[n][FN];
  104. for (int i = 0; i < n; i++) {
  105. Crypto.sr.nextBytes(m[i]);
  106. Crypto.sr.nextBytes(a[i]);
  107. Crypto.sr.nextBytes(b[i]);
  108. while (Util.equal(a[i], b[i]))
  109. Crypto.sr.nextBytes(b[i]);
  110. }
  111. int index = Crypto.sr.nextInt(n);
  112. b[index] = a[index].clone();
  113. PreData predata = new PreData();
  114. PreSSCOT presscot = new PreSSCOT(con1, con2);
  115. if (party == Party.Eddie) {
  116. con1.write(b);
  117. con2.write(m);
  118. con2.write(index);
  119. presscot.runE(predata, n);
  120. runE(predata, m, a, time);
  121. } else if (party == Party.Debbie) {
  122. b = con1.readObject();
  123. presscot.runD(predata);
  124. runD(predata, b, time);
  125. } else if (party == Party.Charlie) {
  126. m = con1.readObject();
  127. index = con1.readObject();
  128. presscot.runC();
  129. OutSSCOT output = runC(time);
  130. if (output.t == index && Util.equal(output.m_t, m[index]))
  131. System.out.println("SSCOT test passed");
  132. else
  133. System.err.println("SSCOT test failed");
  134. } else {
  135. throw new NoSuchPartyException(party + "");
  136. }
  137. }
  138. }
  139. }