Browse Source

implemented all parts for auditng
still has bugs

Simon 2 years ago
parent
commit
b10896f739
3 changed files with 177 additions and 55 deletions
  1. 22 12
      client/client.go
  2. 91 21
      follower/follower.go
  3. 64 22
      leader/leader.go

+ 22 - 12
client/client.go

@@ -186,8 +186,8 @@ func client(tweet []byte, clientNumber int) {
 			//request virtualAddress from leader via pirQuery
 			encryptedQueryLeader, encryptedQueryFollower := createAuditPIRQuery(clientNumber)
 			sendQuerys(encryptedQueryLeader, encryptedQueryFollower, leaderConn, false)
-			pos := receiveTweets(sharedSecret[clientNumber], leaderConn, clientNumber, false, true)
-			fmt.Println(pos)
+			pos := receiveVirtualAddress(sharedSecret[clientNumber], leaderConn)
+			fmt.Println("virtual address received", pos)
 
 			//prep the query
 			dataSize := len(tweet)
@@ -282,7 +282,7 @@ func client(tweet []byte, clientNumber int) {
 
 			}
 
-			receiveTweets(sharedSecret[clientNumber], leaderConn, clientNumber, false, false)
+			receiveTweets(sharedSecret[clientNumber], leaderConn, false)
 
 			if len(archiveTopicList) > 0 {
 				wantsArchive[0] = 0 //archive test
@@ -298,7 +298,7 @@ func client(tweet []byte, clientNumber int) {
 			if wantsArchive[0] == 1 && len(archiveTopicList) > 0 {
 				encryptedQueryLeader, encryptedQueryFollower = createPIRQuery(-1, clientNumber)
 				sendQuerys(encryptedQueryLeader, encryptedQueryFollower, leaderConn, true)
-				receiveTweets(sharedSecret[clientNumber], leaderConn, clientNumber, true, false)
+				receiveTweets(sharedSecret[clientNumber], leaderConn, true)
 			}
 
 		} else {
@@ -435,15 +435,28 @@ func sendQuerys(encryptedQueryLeader, encryptedQueryFollower []byte, leaderConn
 	}
 }
 
-func receiveTweets(sharedSecret [2][32]byte, leaderConn net.Conn, clientNumber int, getArchive, doAuditing bool) int {
+func receiveVirtualAddress(sharedSecret [2][32]byte, leaderConn net.Conn) int {
+	virtualAddressByte := make([]byte, 4)
+	_, err := leaderConn.Read(virtualAddressByte)
+	if err != nil {
+		panic(err)
+	}
+
+	//xores the sharedSecret
+	for h := 0; h < 2; h++ {
+		for i := 0; i < 4; i++ {
+			virtualAddressByte[i] = virtualAddressByte[i] ^ sharedSecret[h][i]
+		}
+	}
+	return byteToInt(virtualAddressByte)
+}
+
+func receiveTweets(sharedSecret [2][32]byte, leaderConn net.Conn, getArchive bool) int {
 
 	tmpNeededSubscriptions := neededSubscriptions
 	if getArchive {
 		tmpNeededSubscriptions = len(archiveInterests)
 	}
-	if doAuditing {
-		tmpNeededSubscriptions = 1
-	}
 	for i := 0; i < tmpNeededSubscriptions; i++ {
 		//client receives tweets
 		tweetsLengthBytes := make([]byte, 4)
@@ -477,10 +490,6 @@ func receiveTweets(sharedSecret [2][32]byte, leaderConn net.Conn, clientNumber i
 
 		//tweets can be displayed
 		fmt.Println("final result: ", string(tweets))
-
-		if doAuditing {
-			return byteToInt(tweets)
-		}
 	}
 	return 0
 }
@@ -517,6 +526,7 @@ func createAuditPIRQuery(clientNumber int) ([]byte, []byte) {
 
 	//the positon the virtual address will be taken from
 	pos := mr.Intn(dbWriteSize)
+	fmt.Println("pos to receive", pos)
 	pirQuerys[0][pos] = 1
 	pirQuerys[1][pos] = 0
 

+ 91 - 21
follower/follower.go

@@ -188,7 +188,7 @@ func main() {
 			}
 			leaderConnection.SetDeadline(time.Time{})
 			startTime = time.Now()
-			go phase1(i, leaderConnection, m, wg)
+			go phase1(i, leaderConnection, m, wg, virtualAddresses)
 		}
 		wg.Wait()
 
@@ -236,7 +236,7 @@ func main() {
 	}
 }
 
-func phase1(id int, leaderWorkerConnection net.Conn, m sync.Mutex, wg *sync.WaitGroup) {
+func phase1(id int, leaderWorkerConnection net.Conn, m sync.Mutex, wg *sync.WaitGroup, virtualAddresses []int) {
 
 	gotClient := make([]byte, 1)
 
@@ -268,8 +268,13 @@ func phase1(id int, leaderWorkerConnection net.Conn, m sync.Mutex, wg *sync.Wait
 		}
 		clientPublicKey = &tmpClientPublicKey
 
-		//auditing starts here
-		//todo!
+		clientKeys := clientData[tmpClientPublicKey]
+		clientKeys, pirQuery := handlePirQuery(clientKeys, leaderWorkerConnection, 0, tmpClientPublicKey, true)
+		getSendVirtualAddress(pirQuery[0], virtualAddresses, clientKeys.SharedSecret, leaderWorkerConnection)
+
+		clientData[*clientPublicKey] = clientKeys
+
+		fmt.Println(pirQuery[0])
 
 		//gets dpfQuery from leader
 		dpfLengthBytes := make([]byte, 4)
@@ -294,22 +299,54 @@ func phase1(id int, leaderWorkerConnection net.Conn, m sync.Mutex, wg *sync.Wait
 			panic("dpfQueryB decryption not ok")
 		}
 
-		//run dpf, xor into local db
-		for i := 0; i < dbSize; i++ {
-			ds := int(C.db[i].dataSize)
-			dataShare := make([]byte, ds)
-			pos := C.getUint128_t(C.int(virtualAddresses[i]))
-			C.evalDPF(C.ctx[id], (*C.uchar)(&dpfQueryB[0]), pos, C.int(ds), (*C.uchar)(&dataShare[0]))
-			for j := 0; j < ds; j++ {
-				db[i][j] = db[i][j] ^ dataShare[j]
+		ds := int(C.db[0].dataSize)
+		dataShareFollower := make([]byte, ds)
+		pos := C.getUint128_t(C.int(virtualAddresses[dbWriteSize]))
+		C.evalDPF(C.ctx[id], (*C.uchar)(&dpfQueryB[0]), pos, C.int(ds), (*C.uchar)(&dataShareFollower[0]))
+
+		dataShareLeader := make([]byte, ds)
+
+		_, err = leaderWorkerConnection.Write(dataShareFollower)
+		if err != nil {
+			panic(err)
+		}
+
+		_, err = leaderWorkerConnection.Read(dataShareLeader)
+		if err != nil {
+			panic(err)
+		}
+
+		auditXOR := make([]byte, ds)
+		passedAudit := true
+		for i := 0; i < ds; i++ {
+			auditXOR[i] = dataShareLeader[i] ^ dataShareFollower[i]
+
+			//client tried to write to a position that is not a virtuallAddress
+			if auditXOR[i] != 0 {
+				passedAudit = false
 			}
 		}
 
-		//xor the worker's DB into the main DB
-		for i := 0; i < dbSize; i++ {
-			m.Lock()
-			C.xorIn(C.int(i), (*C.uchar)(&db[i][0]))
-			m.Unlock()
+		fmt.Println("auditXOR", auditXOR, passedAudit)
+
+		if passedAudit {
+			//run dpf, xor into local db
+			for i := 0; i < dbSize; i++ {
+				ds := int(C.db[i].dataSize)
+				dataShare := make([]byte, ds)
+				pos := C.getUint128_t(C.int(virtualAddresses[i]))
+				C.evalDPF(C.ctx[id], (*C.uchar)(&dpfQueryB[0]), pos, C.int(ds), (*C.uchar)(&dataShare[0]))
+				for j := 0; j < ds; j++ {
+					db[i][j] = db[i][j] ^ dataShare[j]
+				}
+			}
+
+			//xor the worker's DB into the main DB
+			for i := 0; i < dbSize; i++ {
+				m.Lock()
+				C.xorIn(C.int(i), (*C.uchar)(&db[i][0]))
+				m.Unlock()
+			}
 		}
 	}
 }
@@ -508,7 +545,7 @@ func phase3(leaderWorkerConnection net.Conn, wg *sync.WaitGroup) {
 		clientKeys := clientData[clientPublicKey]
 
 		if subPhase[0] == 0 || subPhase[0] == 1 {
-			clientKeys, _ = handlePirQuery(clientKeys, leaderWorkerConnection, int(subPhase[0]), clientPublicKey)
+			clientKeys, _ = handlePirQuery(clientKeys, leaderWorkerConnection, int(subPhase[0]), clientPublicKey, false)
 		}
 
 		getSendTweets(clientKeys, nil, leaderWorkerConnection)
@@ -520,7 +557,7 @@ func phase3(leaderWorkerConnection net.Conn, wg *sync.WaitGroup) {
 		}
 
 		if wantsArchive[0] == 1 {
-			_, archiveQuerys := handlePirQuery(clientKeys, leaderWorkerConnection, -1, clientPublicKey)
+			_, archiveQuerys := handlePirQuery(clientKeys, leaderWorkerConnection, -1, clientPublicKey, false)
 			getSendTweets(clientKeys, archiveQuerys, leaderWorkerConnection)
 
 		}
@@ -572,7 +609,8 @@ func getSendTweets(clientKeys clientKeys, archiveQuerys [][]byte, leaderWorkerCo
 	}
 }
 
-func handlePirQuery(clientKeys clientKeys, leaderWorkerConnection net.Conn, subPhase int, clientPublicKey [32]byte) (clientKeys, [][]byte) {
+func handlePirQuery(clientKeys clientKeys, leaderWorkerConnection net.Conn, subPhase int, clientPublicKey [32]byte, doAuditing bool) (clientKeys, [][]byte) {
+
 	archiveNeededSubscriptions := make([]byte, 4)
 	if subPhase == -1 {
 		_, err := leaderWorkerConnection.Read(archiveNeededSubscriptions)
@@ -603,6 +641,7 @@ func handlePirQuery(clientKeys clientKeys, leaderWorkerConnection net.Conn, subP
 	if !ok {
 		panic("pirQuery decryption not ok")
 	}
+
 	//gets sharedSecret
 	if subPhase == 0 {
 		//bs!
@@ -613,6 +652,12 @@ func handlePirQuery(clientKeys clientKeys, leaderWorkerConnection net.Conn, subP
 		clientKeys.SharedSecret = newSharedSecret
 		decrypted = decrypted[32:]
 
+		if doAuditing {
+			result := make([][]byte, 1)
+			result[0] = decrypted
+			return clientKeys, result
+		}
+
 		//follower updates sharedSecret
 	} else if subPhase == 1 {
 		sharedSecret := clientKeys.SharedSecret
@@ -623,13 +668,14 @@ func handlePirQuery(clientKeys clientKeys, leaderWorkerConnection net.Conn, subP
 	//follower expects pirQuery
 
 	//transforms byteArray to ints of wanted topics
-	pirQueryFlattened := decrypted
 	tmpNeededSubscriptions := neededSubscriptions
 	tmpTopicAmount := topicAmount
 	if subPhase == -1 {
 		tmpNeededSubscriptions = byteToInt(archiveNeededSubscriptions)
 		_, tmpTopicAmount = lib.GetTopicList(1)
 	}
+
+	pirQueryFlattened := decrypted
 	pirQuerys := make([][]byte, tmpNeededSubscriptions)
 	for i := range pirQuerys {
 		pirQuerys[i] = make([]byte, tmpTopicAmount)
@@ -646,6 +692,30 @@ func handlePirQuery(clientKeys clientKeys, leaderWorkerConnection net.Conn, subP
 	return clientKeys, pirQuerys
 }
 
+func getSendVirtualAddress(pirQuery []byte, virtualAddresses []int, sharedSecret [32]byte, leaderWorkerConnection net.Conn) {
+	//xores all requested addresses into virtuallAddress
+	virtualAddress := make([]byte, 4)
+	fmt.Println(pirQuery[15])
+	for _, num := range pirQuery {
+		if num == 1 {
+			currentAddress := intToByte(virtualAddresses[num])
+			for i := 0; i < 4; i++ {
+				virtualAddress[i] = virtualAddress[i] ^ currentAddress[i]
+			}
+		}
+	}
+
+	//xores the sharedSecret
+	for i := 0; i < 4; i++ {
+		virtualAddress[i] = virtualAddress[i] ^ sharedSecret[i]
+	}
+
+	_, err := leaderWorkerConnection.Write(virtualAddress)
+	if err != nil {
+		panic(err)
+	}
+}
+
 func transformBytesToStringArray(topicsAsBytes []byte) []string {
 	var topics []string
 	var topic string

+ 64 - 22
leader/leader.go

@@ -262,7 +262,9 @@ func main() {
 
 		//creates a new db containing virtual addresses for auditing
 		virtualAddresses := createVirtualAddresses()
-
+		for i := 0; i < len(virtualAddresses); i++ {
+			fmt.Println(i, virtualAddresses[i])
+		}
 		//send all virtualAddresses to follower
 		for i := 0; i <= dbWriteSize; i++ {
 			_, err = followerConnection.Write(intToByte(virtualAddresses[i]))
@@ -390,7 +392,6 @@ func phase1(id int, phase []byte, followerConnection net.Conn, wg *sync.WaitGrou
 			panic(err)
 		}
 
-		//auditing starts here
 		var clientKeys = clientData[clientConnection.RemoteAddr()]
 		clientKeys, pirQuery := handlePirQuery(clientKeys, clientConnection, followerConnection, 0, true)
 		getSendVirtualAddress(pirQuery[0], virtualAddresses, clientKeys.SharedSecret, clientConnection, followerConnection)
@@ -436,27 +437,57 @@ func phase1(id int, phase []byte, followerConnection net.Conn, wg *sync.WaitGrou
 			panic("dpfQueryA decryption not ok")
 		}
 
-		//todo!
-		//eval for pos that is not in db then exchange with follower to drop or allow
+		ds := int(C.db[0].dataSize)
+		dataShareLeader := make([]byte, ds)
+		pos := C.getUint128_t(C.int(virtualAddresses[dbWriteSize]))
+		C.evalDPF(C.ctx[id], (*C.uchar)(&dpfQueryA[0]), pos, C.int(ds), (*C.uchar)(&dataShareLeader[0]))
 
-		//run dpf, xor into local db
-		for i := 0; i < dbSize; i++ {
-			ds := int(C.db[i].dataSize)
-			dataShare := make([]byte, ds)
-			pos := C.getUint128_t(C.int(virtualAddresses[i]))
-			C.evalDPF(C.ctx[id], (*C.uchar)(&dpfQueryA[0]), pos, C.int(ds), (*C.uchar)(&dataShare[0]))
-			for j := 0; j < ds; j++ {
-				db[i][j] = db[i][j] ^ dataShare[j]
+		dataShareFollower := make([]byte, ds)
+
+		_, err = followerConnection.Read(dataShareFollower)
+		if err != nil {
+			panic(err)
+		}
+
+		_, err = followerConnection.Write(dataShareLeader)
+		if err != nil {
+			panic(err)
+		}
+
+		auditXOR := make([]byte, ds)
+		passedAudit := true
+		for i := 0; i < ds; i++ {
+			auditXOR[i] = dataShareLeader[i] ^ dataShareFollower[i]
+
+			//client tried to write to a position that is not a virtuallAddress
+			if auditXOR[i] != 0 {
+				clientConnection.Close()
+				passedAudit = false
 			}
 		}
 
-		//xor the worker's DB into the main DB
-		for i := 0; i < dbSize; i++ {
-			m.Lock()
-			C.xorIn(C.int(i), (*C.uchar)(&db[i][0]))
-			m.Unlock()
+		fmt.Println("auditXOR", auditXOR, passedAudit)
+
+		if passedAudit {
+			//run dpf, xor into local db
+			for i := 0; i < dbSize; i++ {
+				ds := int(C.db[i].dataSize)
+				dataShare := make([]byte, ds)
+				pos := C.getUint128_t(C.int(virtualAddresses[i]))
+				C.evalDPF(C.ctx[id], (*C.uchar)(&dpfQueryA[0]), pos, C.int(ds), (*C.uchar)(&dataShare[0]))
+				for j := 0; j < ds; j++ {
+					db[i][j] = db[i][j] ^ dataShare[j]
+				}
+			}
+
+			//xor the worker's DB into the main DB
+			for i := 0; i < dbSize; i++ {
+				m.Lock()
+				C.xorIn(C.int(i), (*C.uchar)(&db[i][0]))
+				m.Unlock()
+			}
+			phase3Channel <- clientConnection
 		}
-		phase3Channel <- clientConnection
 
 		//loop that waits for new client or leaves phase1 if time is up
 		for {
@@ -829,7 +860,9 @@ func createVirtualAddresses() []int {
 }
 
 func getSendVirtualAddress(pirQuery []byte, virtualAddresses []int, sharedSecret [32]byte, clientConnection, followerConnection net.Conn) {
+	//xores all requested addresses into virtuallAddress
 	virtualAddress := make([]byte, 4)
+	fmt.Println(pirQuery[15])
 	for _, num := range pirQuery {
 		if num == 1 {
 			currentAddress := intToByte(virtualAddresses[num])
@@ -838,18 +871,21 @@ func getSendVirtualAddress(pirQuery []byte, virtualAddresses []int, sharedSecret
 			}
 		}
 	}
+
+	//xores the sharedSecret
 	for i := 0; i < 4; i++ {
 		virtualAddress[i] = virtualAddress[i] ^ sharedSecret[i]
 	}
 
-	followersAddress := make([]byte, 4)
-	_, err := followerConnection.Read(followersAddress)
+	virtualAddressFollower := make([]byte, 4)
+	_, err := followerConnection.Read(virtualAddressFollower)
 	if err != nil {
 		panic(err)
 	}
 
+	//xores the data from follower
 	for i := 0; i < 4; i++ {
-		virtualAddress[i] = virtualAddress[i] ^ followersAddress[i]
+		virtualAddress[i] = virtualAddress[i] ^ virtualAddressFollower[i]
 	}
 
 	_, err = clientConnection.Write(virtualAddress)
@@ -988,6 +1024,12 @@ func handlePirQuery(clientKeys clientKeys, clientConnection net.Conn, followerCo
 		decrypted = decrypted[32:]
 	}
 
+	if doAuditing {
+		result := make([][]byte, 1)
+		result[0] = decrypted
+		return clientKeys, result
+	}
+
 	//transforms byteArray to ints of wanted topics
 	pirQueryFlattened := decrypted
 	pirQuerys := make([][]byte, tmpNeededSubscriptions)
@@ -999,7 +1041,7 @@ func handlePirQuery(clientKeys clientKeys, clientConnection net.Conn, followerCo
 	}
 
 	//sets the pirQuery for the client in case whe are not archiving, and not Auditing
-	if subPhase != -1 && !doAuditing {
+	if subPhase != -1 {
 		clientKeys.PirQuery = pirQuerys
 	}