InsLbl.java 5.3 KB

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