Quellcode durchsuchen

Start to remove PreData and offline communication; first tested on PIRCOT(KSearch)

Boyoung- vor 6 Jahren
Ursprung
Commit
fd1bc2c5fd
3 geänderte Dateien mit 111 neuen und 46 gelöschten Zeilen
  1. 19 11
      src/communication/Communication.java
  2. 16 0
      src/crypto/Crypto.java
  3. 76 35
      src/pir/PIRCOT.java

+ 19 - 11
src/communication/Communication.java

@@ -3,6 +3,7 @@ package communication;
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
 import java.io.IOException;
+import java.io.Serializable;
 import java.io.StreamCorruptedException;
 import java.math.BigInteger;
 import java.net.InetSocketAddress;
@@ -14,6 +15,8 @@ import java.util.ArrayList;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.LinkedBlockingQueue;
 
+import org.apache.commons.lang3.SerializationUtils;
+
 import com.oblivm.backend.gc.GCSignal;
 
 import crypto.SimpleAES;
@@ -350,13 +353,13 @@ public class Communication {
 		write(out);
 	}
 
-	/*
-	 * public <T> void write(T out) {
-	 * write(SerializationUtils.serialize((Serializable) out)); }
-	 * 
-	 * public <T> void write(Bandwidth bandwidth, T out) { write(pid,
-	 * SerializationUtils.serialize((Serializable) out)); }
-	 */
+	public <T> void write(T out) {
+		write(SerializationUtils.serialize((Serializable) out));
+	}
+
+	public <T> void write(Bandwidth bandwidth, T out) {
+		write(bandwidth, SerializationUtils.serialize((Serializable) out));
+	}
 
 	public void write(BigInteger b) {
 		write(b.toByteArray());
@@ -582,10 +585,15 @@ public class Communication {
 		return total;
 	}
 
-	/*
-	 * public <T> T readObject() { T object =
-	 * SerializationUtils.deserialize(read()); return object; }
-	 */
+	public <T> T readObject() {
+		T object = SerializationUtils.deserialize(read());
+		return object;
+	}
+
+	public <T> T readObjectAndDec() {
+		T object = SerializationUtils.deserialize(readAndDec());
+		return object;
+	}
 
 	public BigInteger readBigInteger() {
 		return new BigInteger(read());

+ 16 - 0
src/crypto/Crypto.java

@@ -1,11 +1,15 @@
 package crypto;
 
+import java.io.UnsupportedEncodingException;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.security.SecureRandom;
 
 public class Crypto {
 	public static SecureRandom sr;
+	public static SecureRandom sr_DE;
+	public static SecureRandom sr_CE;
+	public static SecureRandom sr_CD;
 	public static MessageDigest sha1;
 	public static int secParam;
 	public static int secParamBytes;
@@ -15,9 +19,21 @@ public class Crypto {
 	static {
 		try {
 			sr = SecureRandom.getInstance("SHA1PRNG");
+
+			sr_DE = SecureRandom.getInstance("SHA1PRNG");
+			sr_DE.setSeed("abcdefghijklmnop".getBytes("us-ascii"));
+
+			sr_CE = SecureRandom.getInstance("SHA1PRNG");
+			sr_CE.setSeed("qrstuvwxyzabcdef".getBytes("us-ascii"));
+
+			sr_CD = SecureRandom.getInstance("SHA1PRNG");
+			sr_CD.setSeed("ghijklmnopqrstuv".getBytes("us-ascii"));
+
 			sha1 = MessageDigest.getInstance("SHA-1");
 		} catch (NoSuchAlgorithmException e) {
 			e.printStackTrace();
+		} catch (UnsupportedEncodingException e) {
+			e.printStackTrace();
 		}
 		secParam = 80;
 		secParamBytes = (secParam + 7) / 8;

+ 76 - 35
src/pir/PIRCOT.java

@@ -2,34 +2,53 @@ package pir;
 
 import communication.Communication;
 import crypto.Crypto;
+import crypto.PRF;
 import exceptions.NoSuchPartyException;
 import exceptions.SSCOTException;
 import oram.Forest;
 import oram.Metadata;
-import pir.precomputation.PrePIRCOT;
 import protocols.Protocol;
 import protocols.struct.OutPIRCOT;
 import protocols.struct.Party;
-import protocols.struct.PreData;
 import util.M;
 import util.Util;
 
+// KSearch
 public class PIRCOT extends Protocol {
 
 	public PIRCOT(Communication con1, Communication con2) {
 		super(con1, con2);
 	}
 
-	public OutPIRCOT runE(PreData predata, byte[][] u, byte[] v) {
-		timer.start(M.online_comp);
+	public OutPIRCOT runE(byte[][] u, byte[] v) {
+		timer.start(M.offline_comp);
 
 		int l = u.length;
+		byte[] k = PRF.generateKey(Crypto.sr_DE);
+		byte[][] r = new byte[l][];
+		for (int i = 0; i < l; i++) {
+			r[i] = new byte[Crypto.secParamBytes];
+			Crypto.sr_DE.nextBytes(r[i]);
+		}
+		int s_DE = Crypto.sr_DE.nextInt(l);
+
+		int s_CE = Crypto.sr_CE.nextInt(l);
+
+		PRF F_k = new PRF(Crypto.secParam);
+		F_k.init(k);
+
+		timer.stop(M.offline_comp);
+
+		//////////////////////////////////////////////////////////////
+
+		timer.start(M.online_comp);
+
 		byte[][] a = new byte[l][];
 		for (int j = 0; j < l; j++) {
-			a[j] = Util.xor(u[(j + predata.sscot_s_DE) % l], v);
-			a[j] = Util.padArray(a[j], predata.sscot_r[j].length);
-			Util.setXor(a[j], predata.sscot_r[j]);
-			a[j] = predata.sscot_F_k.compute(a[j]);
+			a[j] = Util.xor(u[(j + s_DE) % l], v);
+			a[j] = Util.padArray(a[j], r[j].length);
+			Util.setXor(a[j], r[j]);
+			a[j] = F_k.compute(a[j]);
 		}
 
 		timer.start(M.online_write);
@@ -40,27 +59,46 @@ public class PIRCOT extends Protocol {
 		int delta = con2.readIntAndDec();
 		timer.stop(M.online_read);
 
-		int t_E = (predata.sscot_s_DE + delta) % l;
+		int t_E = (s_DE + delta) % l;
 
 		OutPIRCOT out = new OutPIRCOT();
 		out.t_E = t_E;
-		out.s_DE = predata.sscot_s_DE;
-		out.s_CE = predata.sscot_s_CE;
+		out.s_DE = s_DE;
+		out.s_CE = s_CE;
 
 		timer.stop(M.online_comp);
 		return out;
 	}
 
-	public OutPIRCOT runD(PreData predata, byte[][] u, byte[] v) {
-		timer.start(M.online_comp);
+	public OutPIRCOT runD(byte[][] u, byte[] v) {
+		timer.start(M.offline_comp);
 
 		int l = u.length;
+		byte[] k = PRF.generateKey(Crypto.sr_DE);
+		byte[][] r = new byte[l][];
+		for (int i = 0; i < l; i++) {
+			r[i] = new byte[Crypto.secParamBytes];
+			Crypto.sr_DE.nextBytes(r[i]);
+		}
+		int s_DE = Crypto.sr_DE.nextInt(l);
+
+		int s_CD = Crypto.sr_CD.nextInt(l);
+
+		PRF F_k = new PRF(Crypto.secParam);
+		F_k.init(k);
+
+		timer.stop(M.offline_comp);
+
+		///////////////////////////////////////////////////////////
+
+		timer.start(M.online_comp);
+
 		byte[][] a = new byte[l][];
 		for (int j = 0; j < l; j++) {
-			a[j] = Util.xor(u[(j + predata.sscot_s_DE) % l], v);
-			a[j] = Util.padArray(a[j], predata.sscot_r[j].length);
-			Util.setXor(a[j], predata.sscot_r[j]);
-			a[j] = predata.sscot_F_k.compute(a[j]);
+			a[j] = Util.xor(u[(j + s_DE) % l], v);
+			a[j] = Util.padArray(a[j], r[j].length);
+			Util.setXor(a[j], r[j]);
+			a[j] = F_k.compute(a[j]);
 		}
 
 		timer.start(M.online_write);
@@ -71,18 +109,27 @@ public class PIRCOT extends Protocol {
 		int delta = con2.readIntAndDec();
 		timer.stop(M.online_read);
 
-		int t_D = (predata.sscot_s_DE + delta) % l;
+		int t_D = (s_DE + delta) % l;
 
 		OutPIRCOT out = new OutPIRCOT();
 		out.t_D = t_D;
-		out.s_DE = predata.sscot_s_DE;
-		out.s_CD = predata.sscot_s_CD;
+		out.s_DE = s_DE;
+		out.s_CD = s_CD;
 
 		timer.stop(M.online_comp);
 		return out;
 	}
 
-	public OutPIRCOT runC(PreData predata) {
+	public OutPIRCOT runC(int l) {
+		timer.start(M.offline_comp);
+
+		int s_CE = Crypto.sr_CE.nextInt(l);
+		int s_CD = Crypto.sr_CD.nextInt(l);
+
+		timer.stop(M.offline_comp);
+
+		/////////////////////////////////////////////////
+
 		timer.start(M.online_comp);
 
 		timer.start(M.online_read);
@@ -90,7 +137,6 @@ public class PIRCOT extends Protocol {
 		byte[][] y = con2.readDoubleByteArrayAndDec();
 		timer.stop(M.online_read);
 
-		int l = x.length;
 		int count = 0;
 		int t_C = 0;
 		for (int i = 0; i < l; i++) {
@@ -104,8 +150,8 @@ public class PIRCOT extends Protocol {
 			throw new SSCOTException("Invariant error: " + count);
 		}
 
-		int delta_D = (t_C - predata.sscot_s_CE + l) % l;
-		int delta_E = (t_C - predata.sscot_s_CD + l) % l;
+		int delta_D = (t_C - s_CE + l) % l;
+		int delta_E = (t_C - s_CD + l) % l;
 
 		timer.start(M.online_write);
 		con2.write(online_band, delta_D);
@@ -114,8 +160,8 @@ public class PIRCOT extends Protocol {
 
 		OutPIRCOT out = new OutPIRCOT();
 		out.t_C = t_C;
-		out.s_CE = predata.sscot_s_CE;
-		out.s_CD = predata.sscot_s_CD;
+		out.s_CE = s_CE;
+		out.s_CD = s_CD;
 
 		timer.stop(M.online_comp);
 		return out;
@@ -125,7 +171,7 @@ public class PIRCOT extends Protocol {
 	public void run(Party party, Metadata md, Forest[] forest) {
 
 		for (int j = 0; j < 100; j++) {
-			int n = 100;
+			int n = 500;
 			int FN = 5;
 			byte[][] a = new byte[n][FN];
 			byte[][] b = new byte[n][FN];
@@ -135,22 +181,18 @@ public class PIRCOT extends Protocol {
 			int index = Crypto.sr.nextInt(n);
 			byte[] v = a[index].clone();
 
-			PreData predata = new PreData();
-			PrePIRCOT presscot = new PrePIRCOT(con1, con2);
 			OutPIRCOT output;
 
 			if (party == Party.Eddie) {
 				con2.write(index);
-				presscot.runE(predata, n);
-				output = runE(predata, a, v);
+				output = runE(a, v);
 
 				con2.write(output.t_E);
 				con2.write(output.s_CE);
 				con2.write(output.s_DE);
 
 			} else if (party == Party.Debbie) {
-				presscot.runD(predata, n);
-				output = runD(predata, b, new byte[FN]);
+				output = runD(b, new byte[FN]);
 
 				con2.write(output.t_D);
 				con2.write(output.s_DE);
@@ -158,8 +200,7 @@ public class PIRCOT extends Protocol {
 
 			} else if (party == Party.Charlie) {
 				index = con1.readInt();
-				presscot.runC(predata);
-				output = runC(predata);
+				output = runC(n);
 
 				int t_E = con1.readInt();
 				int s_CE = con1.readInt();