Exemplo n.º 1
0
 def fetch_answer(self, msg):
     print("here")
     try:
         db_cache = self.getRecords()['collection']
         pir_xor = msg['pir_xor']
         if not pir_xor:
             index = msg['index']
             return db_cache[index]
         else:
             pir_executor = PIRExecutor()
             vector = msg['index']
             print("Decoding")
             vector = self.decoder.decode_binary(vector, len(db_cache))
             print("Decoded")
             message = ''
             for i, val in enumerate(vector):
                 if val == 1:
                     if message == '':
                         message = db_cache[i]
                     else:
                         message = pir_executor.stringXorer(
                             message, db_cache[i])
             return message
     except IndexError:
         return b'No such index in the database.'
Exemplo n.º 2
0
 def xor(self, messages):
     pir_executor = PIRExecutor()
     message = messages[0]
     for m in messages[1:]:
         message = pir_executor.stringXorer(message, m)
     if len(messages) > 1:
         return message.decode().strip()
     else:
         return message.strip()
Exemplo n.º 3
0
def test_PIR_xorer():
	for i in range(100):
		pir_executor = PIRExecutor()
		a = 'HelloWorld'
		b = 'GoodbyeCruelWorld'
		result = pir_executor.stringXorer(a,b)
		result2 = pir_executor.stringXorer(result, b).split(b'\0', 1)[0]
		assert result2.decode() == a
		result3 = pir_executor.stringXorer(result, a).split(b'\0', 1)[0]
		assert result3.decode() == b
Exemplo n.º 4
0
def test_PIR_getMessagePack():
	index = 1
	size = 100
	for i in range(20):
		pir_executor = PIRExecutor()
		m = pir_executor._getMessagePack(index, size, 5)
		l = m[index]
		## assert requested is odd
		assert sum(l) % 2 != 0
		## assert all the rest are even
		for j in range(len(m)):
			if j == index:
				continue
			assert sum(m[j]) % 2 == 0
Exemplo n.º 5
0
 def generate_messages(self,
                       requested_index,
                       requested_db,
                       record_size,
                       portEnum,
                       pir_xor=False):
     if not pir_xor:
         assert SecurityParameters.NUMBER_OF_REQUESTS % self.dbnum == 0
         indexes = self._generate_random_indexes(requested_index,
                                                 record_size)
         real_message = self.client.package_message(requested_index,
                                                    requested_db, pir_xor,
                                                    portEnum)
         db_index = {}
         for i in range(self.dbnum):
             db_index[i] = []
         db_index[requested_db].append(real_message)
         current_db = 0
         first_round = True
         for i in indexes:
             if first_round and current_db == requested_db:
                 current_db += 1
                 current_db %= self.dbnum
                 if current_db == 0:
                     first_round = False
             db_index[current_db].append(
                 self.client.package_message(i, current_db, pir_xor,
                                             portEnum))
             current_db += 1
             current_db %= self.dbnum
             if current_db == 0:
                 first_round = False
         return db_index
     else:
         pirExecutor = PIRExecutor()
         messagePack = pirExecutor.getMessagePack(requested_index,
                                                  record_size, self.dbnum)
         db_index = {}
         assert (len(messagePack) == self.dbnum)
         for i in range(self.dbnum):
             db_index[i] = []
         current_db = 0
         for i in messagePack:
             db_index[current_db].append(
                 self.client.package_message(i, current_db, pir_xor,
                                             portEnum))
             current_db += 1
         return db_index
Exemplo n.º 6
0
def test_PIR_protocol():
	messages = {
		0: 'Fake0',
		1: 'Real1',
		2: 'Fake2',
		3: 'Fake3',
		4: 'Fake4',
		5: 'Fake5'
	}
	messages = {
		0: "Annaaa",
		1: "George",
		2: "Danaaa",
		3: "Johnaa",
		4: "Doeaaa",
		5: "Earlaa",
		6: "Jensaa",
		7: "Glensa",
	}
	decoder = BinaryEncoderDecoder()
	for index in range(0, len(messages)):
		print("INDEX", index)
		size = len(messages)
		print(size)
		pir_executor = PIRExecutor()
		m = pir_executor.getMessagePack(index, size, 5)
		db_returns = []
		for row in m:
			row = decoder.decode_binary(row, len(messages))
			print("ROW", row)
			message = ''
			for i in range(0, len(row)):
				if row[i] == 1:
					if message == '':
						message = messages[i]
					else:
						message = pir_executor.stringXorer(message, messages[i])
			db_returns.append(message)
		actual_message = db_returns[0]
		for i in range(1, len(db_returns)):
			actual_message = pir_executor.stringXorer(actual_message, db_returns[i])
		actual_message = actual_message.split(b'\0', 1)[0]
		assert(actual_message.decode() == messages[index])
Exemplo n.º 7
0
def test_PIR_xorer_list():
	messages = {
		0: 'FakeBlahBlah',
		1: 'Real',
		2: 'FakeBlahBlah',
		3: 'FakeBlahBlah',
		4: 'FakeBlahBlah',
		5: 'Real',
		6: 'Real'
	}

	for i in range(0,100):
		pir_executor = PIRExecutor()
		message = messages[0]
		for m in range(1, len(messages)):
			message = pir_executor.stringXorer(message, messages[m])
	message = message.split(b'\0', 1)[0]
	assert message.decode() == 'Real'

	messages = {
		0: 'FakeBlahBlah',
		1: 'Real',
		2: 'FakeBlahBlah',
		3: 'FakeBlahBlah',
		4: 'FakeBlahBlah',
		5: 'Real',
	}
	for i in range(0,100):
		pir_executor = PIRExecutor()
		message = messages[0]
		for m in range(1, len(messages)):
			message = pir_executor.stringXorer(message, messages[m])

	message = message.split(b'\0', 1)[0]
	assert message.decode() == ''
Exemplo n.º 8
0
def test_db():
    dbNode = DbNode({'database': 'tests/db_json'})
    requested_index = 2
    assert (requested_index <= 10)  ## db_json only has 11 records
    msg = {'pir_xor': False, 'index': requested_index}
    answer = dbNode.fetch_answer(msg)
    assert (answer == 'https://facebook.com\n')
    encoder = BinaryEncoderDecoder()
    vec = encoder.encode_binary([1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1])

    executor = PIRExecutor()
    array = [
        "https://google.com\n\n\n", "https://facebook.com\n",
        "https://alibaba.com\n\n", "https://improbable.i\n"
    ]
    expected_result = array[0]
    for i in array[1:]:
        print(expected_result)
        expected_result = executor.stringXorer(expected_result, i)
    print(expected_result)
    msg = {'pir_xor': True, 'index': vec}
    answer = dbNode.fetch_answer(msg)
    print(answer)
    assert (answer == expected_result)