Simon 2 years ago
parent
commit
637d8b4cfb
5 changed files with 99 additions and 175 deletions
  1. 16 0
      .vscode/c_cpp_properties.json
  2. 5 0
      .vscode/settings.json
  3. 36 77
      client/client.go
  4. 40 98
      follower/follower.go
  5. 2 0
      leader/leader.go

+ 16 - 0
.vscode/c_cpp_properties.json

@@ -0,0 +1,16 @@
+{
+    "configurations": [
+        {
+            "name": "Linux",
+            "includePath": [
+                "${workspaceFolder}/**"
+            ],
+            "defines": [],
+            "compilerPath": "/usr/bin/gcc",
+            "cStandard": "gnu17",
+            "cppStandard": "c++17",
+            "intelliSenseMode": "linux-gcc-x64"
+        }
+    ],
+    "version": 4
+}

+ 5 - 0
.vscode/settings.json

@@ -0,0 +1,5 @@
+{
+    "files.associations": {
+        "rand.h": "c"
+    }
+}

+ 36 - 77
client/client.go

@@ -151,35 +151,25 @@ func client(tweet []byte, clientNumber int) {
 	followerPublicKey = &tmpFollowerPubKey
 
 	//sends own public key
-	_, err = leaderConn.Write(clientPublicKey[:])
-	if err != nil {
-		panic(err)
-	}
+	writeToConn(leaderConn, clientPublicKey[:])
 
 	//setup ends above
 	//while client is active he is always connected and has to participate
 
 	for {
 		//gets current phase
-		phase := make([]byte, 1)
-		_, err = leaderConn.Read(phase)
-		if err != nil {
-			panic(err)
-		}
+		phase := readFromConn(leaderConn, 1)
 
 		fmt.Println("Phase: ", phase[0])
 
 		if phase[0] == 1 {
 
 			//gets current dbWriteSize from leader
-			dbWriteSizeBytes := make([]byte, 4)
-			_, err = leaderConn.Read(dbWriteSizeBytes)
-			if err != nil {
-				panic(err)
-			}
+			dbWriteSizeBytes := readFromConn(leaderConn, 4)
 			dbWriteSize = byteToInt(dbWriteSizeBytes)
 
 			//todo! put into tweet creation
+			//roundAsBytes := readFromConn(leaderConn, 4)
 			roundAsBytes := make([]byte, 4)
 			_, err = leaderConn.Read(roundAsBytes)
 			if err != nil {
@@ -227,20 +217,11 @@ func client(tweet []byte, clientNumber int) {
 
 			//writes the dpfQuery to the leader
 			dpfLengthBytes := intToByte(len(dpfQueryAEncrypted))
-			_, err = leaderConn.Write(dpfLengthBytes)
-			if err != nil {
-				panic(err)
-			}
+			writeToConn(leaderConn, dpfLengthBytes)
 
-			_, err = leaderConn.Write(dpfQueryAEncrypted)
-			if err != nil {
-				panic(err)
-			}
+			writeToConn(leaderConn, dpfQueryAEncrypted)
 
-			_, err = leaderConn.Write(dpfQueryBEncrypted)
-			if err != nil {
-				panic(err)
-			}
+			writeToConn(leaderConn, dpfQueryBEncrypted)
 
 			C.free(unsafe.Pointer(dpfQueryA))
 			C.free(unsafe.Pointer(dpfQueryB))
@@ -254,11 +235,7 @@ func client(tweet []byte, clientNumber int) {
 				2 : no update needed
 					nothing
 			*/
-			subPhase := make([]byte, 1)
-			_, err := leaderConn.Read(subPhase)
-			if err != nil {
-				panic(err)
-			}
+			subPhase := readFromConn(leaderConn, 1)
 
 			var encryptedQueryLeader, encryptedQueryFollower []byte
 			//first time participating
@@ -291,10 +268,7 @@ func client(tweet []byte, clientNumber int) {
 				wantsArchive[0] = 0
 			}
 
-			_, err = leaderConn.Write(wantsArchive)
-			if err != nil {
-				panic(err)
-			}
+			writeToConn(leaderConn, wantsArchive)
 
 			if wantsArchive[0] == 1 && len(archiveTopicList) > 0 {
 				encryptedQueryLeader, encryptedQueryFollower = createPIRQuery(-1, clientNumber)
@@ -412,36 +386,21 @@ func sendQuerys(encryptedQueryLeader, encryptedQueryFollower []byte, leaderConn
 	encryptedLength := len(encryptedQueryLeader)
 
 	//sends the pirQuerysLength to the leader
-	_, err := leaderConn.Write(intToByte(encryptedLength))
-	if err != nil {
-		panic(err)
-	}
+	writeToConn(leaderConn, intToByte(encryptedLength))
 
 	//sends the pirQuerys to the leader
-	_, err = leaderConn.Write(encryptedQueryLeader)
-	if err != nil {
-		panic(err)
-	}
+	writeToConn(leaderConn, encryptedQueryLeader)
 
-	_, err = leaderConn.Write(encryptedQueryFollower)
-	if err != nil {
-		panic(err)
-	}
+	writeToConn(leaderConn, encryptedQueryFollower)
 
 	if getArchive {
-		leaderConn.Write(intToByte(len(archiveInterests)))
-		if err != nil {
-			panic(err)
-		}
+		writeToConn(leaderConn, intToByte(len(archiveInterests)))
 	}
 }
 
 func receiveVirtualAddress(sharedSecret [2][32]byte, leaderConn net.Conn) int {
-	virtualAddressByte := make([]byte, 4)
-	_, err := leaderConn.Read(virtualAddressByte)
-	if err != nil {
-		panic(err)
-	}
+
+	virtualAddressByte := readFromConn(leaderConn, 4)
 
 	//xores the sharedSecret
 	for h := 0; h < 2; h++ {
@@ -460,19 +419,10 @@ func receiveTweets(sharedSecret [2][32]byte, leaderConn net.Conn, getArchive boo
 	}
 	for i := 0; i < tmpNeededSubscriptions; i++ {
 		//client receives tweets
-		tweetsLengthBytes := make([]byte, 4)
-
-		_, err := leaderConn.Read(tweetsLengthBytes)
-		if err != nil {
-			panic(err)
-		}
+		tweetsLengthBytes := readFromConn(leaderConn, 4)
 		tweetsLength := byteToInt(tweetsLengthBytes)
 
-		tweets := make([]byte, tweetsLength)
-		_, err = leaderConn.Read(tweets)
-		if err != nil {
-			panic(err)
-		}
+		tweets := readFromConn(leaderConn, tweetsLength)
 
 		//expand sharedSecret so it is of right length
 		expandBy := len(tweets) / 32
@@ -609,17 +559,9 @@ func inList(number int, list []int) bool {
 
 func receiveTopicLists(leaderConn net.Conn) {
 	for i := 0; i < 2; i++ {
-		topicListLength := make([]byte, 4)
-		_, err := leaderConn.Read(topicListLength)
-		if err != nil {
-			panic(err)
-		}
+		topicListLength := readFromConn(leaderConn, 4)
 
-		recTopicList := make([]byte, byteToInt(topicListLength))
-		_, err = leaderConn.Read(recTopicList[:])
-		if err != nil {
-			panic(err)
-		}
+		recTopicList := readFromConn(leaderConn, byteToInt(topicListLength))
 
 		var tmpTopicList []string
 		arrayReader := bytes.NewReader(recTopicList[:])
@@ -630,7 +572,24 @@ func receiveTopicLists(leaderConn net.Conn) {
 			archiveTopicList = tmpTopicList
 		}
 	}
+}
+
+//sends the array to the connection
+func writeToConn(connection net.Conn, array []byte) {
+	_, err := connection.Write(array)
+	if err != nil {
+		panic(err)
+	}
+}
 
+//reads an array which is returned and of size "size" from the connection
+func readFromConn(connection net.Conn, size int) []byte {
+	array := make([]byte, size)
+	_, err := connection.Read(array)
+	if err != nil {
+		panic(err)
+	}
+	return array
 }
 
 func intToByte(myInt int) (retBytes []byte) {

+ 40 - 98
follower/follower.go

@@ -139,10 +139,7 @@ func main() {
 	}
 
 	//send publicKey to leader
-	_, err = leaderConnection.Write(followerPublicKey[:])
-	if err != nil {
-		panic(err)
-	}
+	writeToConn(leaderConnection, followerPublicKey[:])
 
 	//receives leader PublicKey
 	var tmpLeaderPubKey [32]byte
@@ -172,11 +169,7 @@ func main() {
 		//receives the virtualAddresses
 		virtualAddresses := make([]int, dbWriteSize+1)
 		for i := 0; i <= dbWriteSize; i++ {
-			virtualAddress := make([]byte, 4)
-			_, err = leaderConnection.Read(virtualAddress)
-			if err != nil {
-				panic(err)
-			}
+			virtualAddress := readFromConn(leaderConnection, 4)
 			virtualAddresses[i] = byteToInt(virtualAddress)
 		}
 
@@ -241,10 +234,7 @@ func phase1(id int, leaderWorkerConnection net.Conn, m sync.Mutex, wg *sync.Wait
 	gotClient := make([]byte, 1)
 
 	for {
-		_, err := leaderWorkerConnection.Read(gotClient)
-		if err != nil {
-			panic(err)
-		}
+		gotClient = readFromConn(leaderWorkerConnection, 1)
 
 		//this worker is done
 		if gotClient[0] == 0 {
@@ -262,7 +252,7 @@ func phase1(id int, leaderWorkerConnection net.Conn, m sync.Mutex, wg *sync.Wait
 		//gets clients publicKey
 		var clientPublicKey *[32]byte
 		var tmpClientPublicKey [32]byte
-		_, err = leaderWorkerConnection.Read(tmpClientPublicKey[:])
+		_, err := leaderWorkerConnection.Read(tmpClientPublicKey[:])
 		if err != nil {
 			panic(err)
 		}
@@ -275,19 +265,12 @@ func phase1(id int, leaderWorkerConnection net.Conn, m sync.Mutex, wg *sync.Wait
 		clientData[*clientPublicKey] = clientKeys
 
 		//gets dpfQuery from leader
-		dpfLengthBytes := make([]byte, 4)
-		_, err = leaderWorkerConnection.Read(dpfLengthBytes)
-		if err != nil {
-			panic(err)
-		}
+		dpfLengthBytes := readFromConn(leaderWorkerConnection, 4)
 		dpfLength := byteToInt(dpfLengthBytes)
 
 		dpfQueryBEncrypted := make([]byte, dpfLength)
 
-		_, err = leaderWorkerConnection.Read(dpfQueryBEncrypted)
-		if err != nil {
-			panic(err)
-		}
+		dpfQueryBEncrypted = readFromConn(leaderWorkerConnection, dpfLength)
 
 		//decrypt dpfQueryB for sorting into db
 		var decryptNonce [24]byte
@@ -304,15 +287,9 @@ func phase1(id int, leaderWorkerConnection net.Conn, m sync.Mutex, wg *sync.Wait
 
 		dataShareLeader := make([]byte, ds)
 
-		_, err = leaderWorkerConnection.Write(dataShareFollower)
-		if err != nil {
-			panic(err)
-		}
+		writeToConn(leaderWorkerConnection, dataShareFollower)
 
-		_, err = leaderWorkerConnection.Read(dataShareLeader)
-		if err != nil {
-			panic(err)
-		}
+		dataShareLeader = readFromConn(leaderWorkerConnection, ds)
 
 		auditXOR := make([]byte, ds)
 		passedAudit := true
@@ -364,11 +341,7 @@ func phase2(leaderWorkerConnection net.Conn) {
 	}
 
 	//receive seed from leader
-	seedLeader := make([]byte, 16)
-	_, err := leaderWorkerConnection.Read(seedLeader)
-	if err != nil {
-		panic(err)
-	}
+	seedLeader := readFromConn(leaderWorkerConnection, 16)
 
 	//receive data from leader
 	tmpdbLeader := make([][]byte, dbSize)
@@ -376,24 +349,15 @@ func phase2(leaderWorkerConnection net.Conn) {
 		tmpdbLeader[i] = make([]byte, dataLength)
 	}
 	for i := 0; i < dbSize; i++ {
-		_, err = leaderWorkerConnection.Read(tmpdbLeader[i])
-		if err != nil {
-			panic(err)
-		}
+		tmpdbLeader[i] = readFromConn(leaderWorkerConnection, dataLength)
 	}
 
 	//writes seed to leader
-	_, err = leaderWorkerConnection.Write(seedFollower)
-	if err != nil {
-		panic(err)
-	}
+	writeToConn(leaderWorkerConnection, seedFollower)
 
 	//write data to leader
 	for i := 0; i < dbSize; i++ {
-		_, err = leaderWorkerConnection.Write(tmpdbFollower[i])
-		if err != nil {
-			panic(err)
-		}
+		writeToConn(leaderWorkerConnection, tmpdbFollower[i])
 	}
 	//put together the db
 	tmpdb := make([][]byte, dbSize)
@@ -413,7 +377,7 @@ func phase2(leaderWorkerConnection net.Conn) {
 	//receive ciphers from leader
 	ciphersLeader := make([]byte, dbSize*16)
 	for i := 0; i < dbSize; i++ {
-		_, err = leaderWorkerConnection.Read(ciphersLeader[i*16:])
+		_, err := leaderWorkerConnection.Read(ciphersLeader[i*16:])
 		if err != nil {
 			panic(err)
 		}
@@ -421,10 +385,7 @@ func phase2(leaderWorkerConnection net.Conn) {
 
 	//send own Ciphers to leader
 	for i := 0; i < dbSize; i++ {
-		_, err = leaderWorkerConnection.Write(C.GoBytes(unsafe.Pointer(ciphersFollowers[i]), 16))
-		if err != nil {
-			panic(err)
-		}
+		writeToConn(leaderWorkerConnection, C.GoBytes(unsafe.Pointer(ciphersFollowers[i]), 16))
 	}
 
 	//put in ciphers from leader
@@ -483,11 +444,7 @@ func phase2(leaderWorkerConnection net.Conn) {
 	C.resetDb()
 
 	//gets current dbWriteSize from leader
-	dbWriteSizeBytes := make([]byte, 4)
-	_, err = leaderWorkerConnection.Read(dbWriteSizeBytes)
-	if err != nil {
-		panic(err)
-	}
+	dbWriteSizeBytes := readFromConn(leaderWorkerConnection, 4)
 	dbWriteSize = byteToInt(dbWriteSizeBytes)
 }
 
@@ -514,10 +471,7 @@ func phase3(leaderWorkerConnection net.Conn, wg *sync.WaitGroup) {
 	gotClient := make([]byte, 1)
 
 	for {
-		_, err := leaderWorkerConnection.Read(gotClient)
-		if err != nil {
-			panic(err)
-		}
+		gotClient = readFromConn(leaderWorkerConnection, 1)
 
 		//this worker is done
 		if gotClient[0] == 0 {
@@ -525,14 +479,10 @@ func phase3(leaderWorkerConnection net.Conn, wg *sync.WaitGroup) {
 			return
 		}
 
-		subPhase := make([]byte, 1)
-		_, err = leaderWorkerConnection.Read(subPhase)
-		if err != nil {
-			panic(err)
-		}
+		subPhase := readFromConn(leaderWorkerConnection, 1)
 
 		var clientPublicKey [32]byte
-		_, err = leaderWorkerConnection.Read(clientPublicKey[:])
+		_, err := leaderWorkerConnection.Read(clientPublicKey[:])
 		if err != nil {
 			panic(err)
 		}
@@ -546,11 +496,7 @@ func phase3(leaderWorkerConnection net.Conn, wg *sync.WaitGroup) {
 
 		getSendTweets(clientKeys, nil, leaderWorkerConnection)
 
-		wantsArchive := make([]byte, 1)
-		_, err = leaderWorkerConnection.Read(wantsArchive)
-		if err != nil {
-			panic(err)
-		}
+		wantsArchive := readFromConn(leaderWorkerConnection, 1)
 
 		if wantsArchive[0] == 1 {
 			_, archiveQuerys := handlePirQuery(clientKeys, leaderWorkerConnection, -1, clientPublicKey, false)
@@ -592,16 +538,9 @@ func getSendTweets(clientKeys clientKeys, archiveQuerys [][]byte, leaderWorkerCo
 		//sends tweets to leader
 		tweetsLengthBytes := intToByte(len(tweets))
 
-		_, err := leaderWorkerConnection.Write(tweetsLengthBytes)
-		if err != nil {
-			panic(err)
-		}
-
-		_, err = leaderWorkerConnection.Write(tweets)
-		if err != nil {
-			panic(err)
-		}
+		writeToConn(leaderWorkerConnection, tweetsLengthBytes)
 
+		writeToConn(leaderWorkerConnection, tweets)
 	}
 }
 
@@ -609,27 +548,15 @@ func handlePirQuery(clientKeys clientKeys, leaderWorkerConnection net.Conn, subP
 
 	archiveNeededSubscriptions := make([]byte, 4)
 	if subPhase == -1 {
-		_, err := leaderWorkerConnection.Read(archiveNeededSubscriptions)
-		if err != nil {
-			panic(err)
-		}
+		archiveNeededSubscriptions = readFromConn(leaderWorkerConnection, 4)
 	}
 
 	//gets the msg length
-	msgLengthBytes := make([]byte, 4)
-	_, err := leaderWorkerConnection.Read(msgLengthBytes)
-	if err != nil {
-		panic(err)
-	}
+	msgLengthBytes := readFromConn(leaderWorkerConnection, 4)
 	msgLength := byteToInt(msgLengthBytes)
 
-	message := make([]byte, msgLength)
-
 	//gets the message
-	_, err = leaderWorkerConnection.Read(message)
-	if err != nil {
-		panic(err)
-	}
+	message := readFromConn(leaderWorkerConnection, msgLength)
 
 	var decryptNonce [24]byte
 	copy(decryptNonce[:], message[:24])
@@ -706,10 +633,25 @@ func getSendVirtualAddress(pirQuery []byte, virtualAddresses []int, sharedSecret
 		virtualAddress[i] = virtualAddress[i] ^ sharedSecret[i]
 	}
 
-	_, err := leaderWorkerConnection.Write(virtualAddress)
+	writeToConn(leaderWorkerConnection, virtualAddress)
+}
+
+//sends the array to the connection
+func writeToConn(connection net.Conn, array []byte) {
+	_, err := connection.Write(array)
+	if err != nil {
+		panic(err)
+	}
+}
+
+//reads an array which is returned and of size "size" from the connection
+func readFromConn(connection net.Conn, size int) []byte {
+	array := make([]byte, size)
+	_, err := connection.Read(array)
 	if err != nil {
 		panic(err)
 	}
+	return array
 }
 
 func transformBytesToStringArray(topicsAsBytes []byte) []string {

+ 2 - 0
leader/leader.go

@@ -954,6 +954,7 @@ func sendTopicLists(clientConnection net.Conn) {
 	}
 }
 
+//sends the array to the connection
 func writeToConn(connection net.Conn, array []byte) {
 	_, err := connection.Write(array)
 	if err != nil {
@@ -961,6 +962,7 @@ func writeToConn(connection net.Conn, array []byte) {
 	}
 }
 
+//reads an array which is returned and of size "size" from the connection
 func readFromConn(connection net.Conn, size int) []byte {
 	array := make([]byte, size)
 	_, err := connection.Read(array)