SSCOT.java 3.7 KB

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