InsLbl.java 5.5 KB

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