InsLbl.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package subprotocols;
  2. import java.security.SecureRandom;
  3. import java.util.Arrays;
  4. import communication.Communication;
  5. import crypto.Crypto;
  6. import exceptions.NoSuchPartyException;
  7. import oram.Forest;
  8. import oram.Metadata;
  9. import protocols.Protocol;
  10. import struct.Party;
  11. import util.M;
  12. import util.P;
  13. import util.Util;
  14. public class InsLbl extends Protocol {
  15. SecureRandom sr1;
  16. SecureRandom sr2;
  17. int pid = P.IL;
  18. public InsLbl(Communication con1, Communication con2) {
  19. super(con1, con2);
  20. online_band = all.online_band[pid];
  21. offline_band = all.offline_band[pid];
  22. timer = all.timer[pid];
  23. }
  24. public InsLbl(Communication con1, Communication con2, SecureRandom sr1, SecureRandom sr2) {
  25. super(con1, con2);
  26. this.sr1 = sr1;
  27. this.sr2 = sr2;
  28. online_band = all.online_band[pid];
  29. offline_band = all.offline_band[pid];
  30. timer = all.timer[pid];
  31. }
  32. public void reinit(Communication con1, Communication con2, SecureRandom sr1, SecureRandom sr2) {
  33. this.con1 = con1;
  34. this.con2 = con2;
  35. this.sr1 = sr1;
  36. this.sr2 = sr2;
  37. }
  38. public void runP1(int dN1, byte[] L1, int ttp) {
  39. timer.start(M.offline_comp);
  40. int l = L1.length;
  41. byte[] p = Util.nextBytes(ttp * l, sr1);
  42. byte[] a = Util.nextBytes(ttp * l, sr1);
  43. byte[] b = Util.nextBytes(ttp * l, sr1);
  44. int v = sr1.nextInt(ttp);
  45. int w = sr1.nextInt(ttp);
  46. int alpha1 = Crypto.sr.nextInt(ttp);
  47. int u1 = alpha1 ^ v;
  48. byte[] pstar = Util.xor(p, Util.xorRotate(a, u1, ttp, l));
  49. timer.start(M.offline_write);
  50. con2.write(offline_band, u1);
  51. con2.write(offline_band, pstar);
  52. timer.stop(M.offline_write);
  53. timer.stop(M.offline_comp);
  54. // ----------------------------------------- //
  55. timer.start(M.online_comp);
  56. int m = dN1 ^ alpha1;
  57. timer.start(M.online_write);
  58. con1.write(online_band, m);
  59. timer.stop(M.online_write);
  60. timer.start(M.online_read);
  61. m = con1.readIntAndDec();
  62. timer.stop(M.online_read);
  63. int beta1 = m ^ dN1;
  64. int index = beta1 ^ w;
  65. for (int i = 0; i < l; i++) {
  66. b[index * l + i] = (byte) (b[index * l + i] ^ L1[i]);
  67. }
  68. timer.start(M.online_write);
  69. con2.write(online_band, b);
  70. timer.stop(M.online_write);
  71. timer.stop(M.online_comp);
  72. return;
  73. }
  74. public byte[] runP2(int dN2, byte[] L2, int ttp) {
  75. timer.start(M.offline_comp);
  76. int l = L2.length;
  77. byte[] p = Util.nextBytes(ttp * l, sr1);
  78. byte[] a = Util.nextBytes(ttp * l, sr1);
  79. byte[] b = Util.nextBytes(ttp * l, sr1);
  80. int v = sr1.nextInt(ttp);
  81. int w = sr1.nextInt(ttp);
  82. int beta2 = Crypto.sr.nextInt(ttp);
  83. int u2 = beta2 ^ w;
  84. byte[] z2 = Util.xor(p, Util.xorRotate(b, u2, ttp, l));
  85. timer.start(M.offline_write);
  86. con2.write(offline_band, u2);
  87. timer.stop(M.offline_write);
  88. timer.stop(M.offline_comp);
  89. // ----------------------------------------- //
  90. timer.start(M.online_comp);
  91. int m = beta2 ^ dN2;
  92. timer.start(M.online_write);
  93. con1.write(online_band, m);
  94. timer.stop(M.online_write);
  95. timer.start(M.online_read);
  96. m = con1.readIntAndDec();
  97. timer.stop(M.online_read);
  98. int alpha2 = m ^ dN2;
  99. int index = alpha2 ^ v;
  100. for (int i = 0; i < l; i++) {
  101. a[index * l + i] = (byte) (a[index * l + i] ^ L2[i]);
  102. }
  103. timer.start(M.online_write);
  104. con2.write(online_band, a);
  105. timer.stop(M.online_write);
  106. timer.stop(M.online_comp);
  107. return z2;
  108. }
  109. public byte[] runP3(int ttp, int l) {
  110. timer.start(M.offline_comp);
  111. timer.start(M.offline_read);
  112. int u1 = con1.readIntAndDec();
  113. byte[] pstar = con1.readAndDec();
  114. int u2 = con2.readIntAndDec();
  115. timer.stop(M.offline_read);
  116. timer.stop(M.offline_comp);
  117. // ----------------------------------------- //
  118. timer.start(M.online_comp);
  119. timer.start(M.online_read);
  120. byte[] s1 = con1.readAndDec();
  121. byte[] s2 = con2.readAndDec();
  122. timer.stop(M.online_read);
  123. s2 = Util.xorRotate(s2, u1, ttp, l);
  124. s1 = Util.xorRotate(s1, u2, ttp, l);
  125. Util.setXor(pstar, s1);
  126. Util.setXor(pstar, s2);
  127. timer.stop(M.online_comp);
  128. return pstar;
  129. }
  130. @Override
  131. public void run(Party party, Metadata md, Forest[] forest) {
  132. for (int j = 0; j < 100; j++) {
  133. int ttp = (int) Math.pow(2, 8);
  134. int l = 10;
  135. int dN1 = Crypto.sr.nextInt(ttp);
  136. int dN2 = Crypto.sr.nextInt(ttp);
  137. byte[] L1 = Util.nextBytes(l, Crypto.sr);
  138. byte[] L2 = Util.nextBytes(l, Crypto.sr);
  139. if (party == Party.Eddie) {
  140. this.reinit(con1, con2, Crypto.sr_DE, Crypto.sr_CE);
  141. this.runP1(dN1, L1, ttp);
  142. con1.write(dN1);
  143. con1.write(L1);
  144. } else if (party == Party.Debbie) {
  145. this.reinit(con1, con2, Crypto.sr_DE, Crypto.sr_CD);
  146. byte[] m1 = this.runP2(dN2, L2, ttp);
  147. byte[] m2 = con2.read();
  148. dN1 = con1.readInt();
  149. L1 = con1.read();
  150. int dN = dN1 ^ dN2;
  151. byte[] L = Util.xor(L1, L2);
  152. byte[] M = Util.xor(m1, m2);
  153. byte[] expectL = Arrays.copyOfRange(M, dN * l, dN * l + l);
  154. boolean fail = false;
  155. if (!Util.equal(L, expectL)) {
  156. System.err.println(j + ": InsLbl test failed on L");
  157. fail = true;
  158. }
  159. for (int i = 0; i < dN * l; i++) {
  160. if (M[i] != 0) {
  161. System.err.println(j + ": InsLbl test failed 1");
  162. fail = true;
  163. break;
  164. }
  165. }
  166. for (int i = dN * l + l; i < M.length; i++) {
  167. if (M[i] != 0) {
  168. System.err.println(j + ": InsLbl test failed 2");
  169. fail = true;
  170. break;
  171. }
  172. }
  173. if (!fail)
  174. System.out.println(j + ": InsLbl test passed");
  175. } else if (party == Party.Charlie) {
  176. this.reinit(con1, con2, Crypto.sr_CE, Crypto.sr_CD);
  177. byte[] m2 = this.runP3(ttp, l);
  178. con2.write(m2);
  179. } else {
  180. throw new NoSuchPartyException(party + "");
  181. }
  182. }
  183. }
  184. @Override
  185. public void run(Party party, Metadata md, Forest forest) {
  186. }
  187. }