def get_sketch_from_relay(relay_ip, data): try: # Compute sketch parameters tmp_w = int(math.ceil(math.e / conf.EPSILON)) tmp_d = int(math.ceil(math.log(1.0 / conf.DELTA))) data["contents"]["attributes"]["sk_w"] = tmp_w data["contents"]["attributes"]["sk_d"] = tmp_d # Fetch data from relay as a serialized sketch object s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((relay_ip, conf.RELAY_PORT)) SockExt.send_msg(s, json.dumps(data)) data = SockExt.recv_msg(s) obj_json = json.loads(data) # The sketch object can be quite large s.shutdown(socket.SHUT_RDWR) s.close() contents = json.loads(obj_json["return"]) # tmp_w = int(data['vars']['w']) # tmp_d = int(data['vars']['d']) # De-serialize sketch object sketch = Classes.CountSketchCt(tmp_w, tmp_d, EcPt.from_binary(binascii.unhexlify(contents["vars"]["pub"]), G)) sketch.load_store_list(tmp_w, tmp_d, contents["store"]) return sketch except Exception as e: print "Exception while getting sketch from relay: " + str(e) traceback.print_exc()
def get_sketch_from_relay(relay_ip, data): try: #Compute sketch parameters tmp_w = int(math.ceil(math.e / conf.EPSILON)) tmp_d = int(math.ceil(math.log(1.0 / conf.DELTA))) data['contents']['attributes']['sk_w'] = tmp_w data['contents']['attributes']['sk_d'] = tmp_d #Fetch data from relay as a serialized sketch object s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((relay_ip, conf.RELAY_PORT)) SockExt.send_msg(s, json.dumps(data)) data = SockExt.recv_msg(s) obj_json = json.loads(data) #The sketch object can be quite large s.shutdown(socket.SHUT_RDWR) s.close() contents = json.loads(obj_json['return']) #tmp_w = int(data['vars']['w']) #tmp_d = int(data['vars']['d']) #De-serialize sketch object sketch = Classes.CountSketchCt( tmp_w, tmp_d, EcPt.from_binary(binascii.unhexlify(contents['vars']['pub']), G)) sketch.load_store_list(tmp_w, tmp_d, contents['store']) return sketch except Exception as e: print "Exception while getting sketch from relay: " + str(e) traceback.print_exc()
def handle_clean(self): global G global priv global pub global auths global common_key global parsed try: inp = SockExt.recv_msg(self.request).strip() data = json.loads(inp) print "[" + str(datetime.datetime.now())[:-7] + "] Request for: " + str(data['request']) if data['request'] == 'stat': #print data['contents'] #parameters contents = data['contents'] stat_type = contents['type'] attributes = contents['attributes'] attr_file = attributes['file'] attr_sheet = attributes['sheet'] attr_column_1 = attributes['column_1'] attr_column_2 = attributes['column_2'] attr_column_3 = attributes['column_3'] if not str(attr_column_3) in parsed: rows = p.get_rows(attr_file,attr_sheet, num_relays, unique_id) #determine which rows correspond to relay values = p.read_xls_cell(attr_file, attr_sheet, attr_column_1, attr_column_2, attr_column_3, rows) #load values from xls parsed[str(attr_column_3)] = values else: values = parsed[str(attr_column_3)] if contents['data_type'] == 'sketch': sk_w = attributes['sk_w'] sk_d = attributes['sk_d'] plain_sketch = generate_sketch(int(sk_w), int(sk_d), values) #construct sketch from values res = plain_sketch.to_JSON() elif contents['data_type'] == 'values': evalues = encrypt_values(values, common_key) res = cts_to_json(evalues) elif contents['data_type'] == 'values_sq': sq_values = square_values(values) evalues = encrypt_values(sq_values, common_key) res = cts_to_json(evalues) SockExt.send_msg(self.request, json.dumps({'return': res})) #return serialized sketch print "[" + str(datetime.datetime.now())[:-7] + "] Request served." if conf.MEASUREMENT_MODE_RELAY: self.server.shutdown() else: print "Unknown request type." except Exception as e: print "Exception on incomming connection: ", e
def remote_decrypt(ip, port, cipher_obj): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((ip, int(port))) json_obj_str = cipher_obj.to_JSON() data = {'request': 'decrypt', 'contents': json_obj_str} SockExt.send_msg(s, json.dumps(data)) result = json.loads(SockExt.recv_msg(s)) s.close() return result['return']
def remote_decrypt(ip, port, cipher_obj): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((ip, int(port))) json_obj_str = cipher_obj.to_JSON() data = {'request':'decrypt', 'contents': json_obj_str} SockExt.send_msg(s, json.dumps(data)) result = json.loads(SockExt.recv_msg(s)) s.close() return result['return']
def remote_encrypt(ip, port, value): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((ip, int(port))) data = {'request':'encrypt', 'contents': {'value':value}} SockExt.send_msg(s, json.dumps(data)) result = json.loads(SockExt.recv_msg(s)) data = json.loads(result['return']) cipher_obj = Classes.Ct(EcPt.from_binary(binascii.unhexlify(data['pub']),G), EcPt.from_binary(binascii.unhexlify(data['a']),G), EcPt.from_binary(binascii.unhexlify(data['b']),G), Bn.from_hex(data['k']), None) s.close() return cipher_obj
def process_request(data, obj): # parameters contents = data["contents"] stat_type = contents["type"] attributes = contents["attributes"] attr_file = attributes["file"] attr_sheet = attributes["sheet"] attr_column_1 = attributes["column_1"] attr_column_2 = attributes["column_2"] attr_column_3 = attributes["column_3"] # Differential Privacy? if "dp" in contents: dp = contents["dp"] else: dp = True # Run selected operation if stat_type == "median": data["contents"]["data_type"] = "sketch" sketches = get_data_from_relays_non_blocking(relays, data) # Gather sketches from relays sk_sum = Classes.CountSketchCt.aggregate(sketches) # Aggregate sketches result = op.median_operation(sk_sum, auths, dp) # Compute median on sum of sketches elif stat_type == "mean": data["contents"]["data_type"] = "values" value_sets = get_data_from_relays_non_blocking(relays, data) # Gather values from relays elist = [] # List the cts for vset in value_sets: for value in vset: elist.append(value) result = op.mean_operation(elist, auths, dp) # Compute mean from cts elif stat_type == "variance": data["contents"]["data_type"] = "values" value_sets = get_data_from_relays_non_blocking(relays, data) # Gather values from relays values_lst = concat_sets(value_sets) data["contents"]["data_type"] = "values_sq" value_sets = get_data_from_relays_non_blocking(relays, data) # Gather squared values from relays values_sq_lst = concat_sets(value_sets) result = op.variance_operation(values_lst, values_sq_lst, auths, dp) # Compute variance from cts SockExt.send_msg( obj.request, json.dumps({"return": {"success": "True", "type": stat_type, "attribute": attr_column_1, "value": result}}), ) print "Stat computed. Listening for requests..." if conf.MEASUREMENT_MODE_PROCESSOR: obj.server.shutdown()
def process_request(data, obj): #parameters contents = data['contents'] stat_type = contents['type'] attributes = contents['attributes'] attr_file = attributes['file'] attr_sheet = attributes['sheet'] attr_column_1 = attributes['column_1'] attr_column_2 = attributes['column_2'] attr_column_3 = attributes['column_3'] #Differential Privacy? if 'dp' in contents: dp = contents['dp'] else: dp = True #Run selected operation if (stat_type == 'median'): data['contents']['data_type'] = "sketch" sketches = get_data_from_relays_non_blocking(relays, data) #Gather sketches from relays sk_sum = Classes.CountSketchCt.aggregate(sketches) #Aggregate sketches result = op.median_operation(sk_sum, auths, dp) #Compute median on sum of sketches elif (stat_type == 'mean'): data['contents']['data_type'] = "values" value_sets = get_data_from_relays_non_blocking(relays, data) #Gather values from relays elist = [] #List the cts for vset in value_sets: for value in vset: elist.append(value) result = op.mean_operation(elist, auths, dp) #Compute mean from cts elif (stat_type == 'variance'): data['contents']['data_type'] = "values" value_sets = get_data_from_relays_non_blocking(relays, data) #Gather values from relays values_lst = concat_sets(value_sets) data['contents']['data_type'] = "values_sq" value_sets = get_data_from_relays_non_blocking(relays, data) #Gather squared values from relays values_sq_lst = concat_sets(value_sets) result = op.variance_operation(values_lst, values_sq_lst, auths, dp) #Compute variance from cts SockExt.send_msg(obj.request, json.dumps({'return':{'success':'True', 'type':stat_type, 'attribute':attr_column_1, 'value':result}})) print 'Stat computed. Listening for requests...' if conf.MEASUREMENT_MODE_PROCESSOR: obj.server.shutdown()
def remote_encrypt(ip, port, value): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((ip, int(port))) data = {'request': 'encrypt', 'contents': {'value': value}} SockExt.send_msg(s, json.dumps(data)) result = json.loads(SockExt.recv_msg(s)) data = json.loads(result['return']) cipher_obj = Classes.Ct( EcPt.from_binary(binascii.unhexlify(data['pub']), G), EcPt.from_binary(binascii.unhexlify(data['a']), G), EcPt.from_binary(binascii.unhexlify(data['b']), G), Bn.from_hex(data['k']), None) s.close() return cipher_obj
def handle_clean(self): try: data = json.loads(SockExt.recv_msg(self.request).strip()) print "Request for: " + str(data["request"]) if data["request"] == "ping": contents = data["contents"] SockExt.send_msg(self.request, json.dumps({"return": int(contents["value"]) + 1})) elif data["request"] == "stat": process_request(data, self) except Exception as e: SockExt.send_msg(self.request, json.dumps({"return": {"success": "False"}})) print "Exception on incoming connection: " + str(e)
def handle_clean(self): try: data = json.loads(SockExt.recv_msg(self.request).strip()) print "Request for: " + str(data['request']) if data['request'] == 'ping': contents = data['contents'] SockExt.send_msg(self.request, json.dumps({'return': int(contents['value'])+1})) elif data['request'] == 'stat': process_request(data, self) except Exception as e: SockExt.send_msg(self.request, json.dumps({'return':{'success':'False'}})) print 'Exception on incoming connection: ' + str(e)
def handle_clean(self): try: data = json.loads(SockExt.recv_msg(self.request).strip()) print "Request for: " + str(data['request']) if data['request'] == 'ping': contents = data['contents'] SockExt.send_msg( self.request, json.dumps({'return': int(contents['value']) + 1})) elif data['request'] == 'stat': process_request(data, self) except Exception as e: SockExt.send_msg(self.request, json.dumps({'return': { 'success': 'False' }})) print 'Exception on incoming connection: ' + str(e)
def generate_group_key(auths=[]): pub_keys = [] for auth_ip in auths: #get pub key from each auth s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #s.settimeout(10.0) s.connect((auth_ip, conf.AUTH_PORT)) data = {'request':'pubkey'} SockExt.send_msg(s, json.dumps(data)) result = json.loads(SockExt.recv_msg(s)) s.shutdown(socket.SHUT_RDWR) s.close() new_key = EcPt.from_binary(binascii.unhexlify(result['return']), G) #De-serialize Ecpt object pub_keys.append(new_key) #Add keys c_pub = pub_keys[0] for pkey in pub_keys[1:]: c_pub += pkey #pub is ecpt, so we add return c_pub
port = args.port print ip if args.ping: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((ip, int(port))) print utilities.ping(s) s.close() elif args.pub: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((ip, int(port))) #pubkey data = {'request': 'pubkey'} SockExt.send_msg(s, json.dumps(data)) result = json.loads(SockExt.recv_msg(s)) print EcPt.from_binary(binascii.unhexlify(result['return']), G) s.close() elif args.test: value = 12345 tmp_obj = remote_encrypt(ip, port, value) new_value = remote_decrypt(ip, port, tmp_obj) #print new_value if value == new_value: print "Test Successful!" else: print "Test failed." elif args.stat:
print ip if args.ping: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((ip, int(port))) print utilities.ping(s) s.close() elif args.pub: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((ip, int(port))) #pubkey data = {'request':'pubkey'} SockExt.send_msg(s, json.dumps(data)) result = json.loads(SockExt.recv_msg(s)) print EcPt.from_binary(binascii.unhexlify(result['return']), G) s.close() elif args.test: value = 12345 tmp_obj = remote_encrypt(ip, port, value) new_value = remote_decrypt(ip, port, tmp_obj) #print new_value if value==new_value: print "Test Successful!" else: print "Test failed."
def get_data_from_relays_non_blocking(relay_ips, data): try: _type = data["contents"]["data_type"] # if _type == "sketch": # Compute sketch parameters # tmp_w = int(math.ceil(math.e / conf.EPSILON)) # tmp_d = int(math.ceil(math.log(1.0 / conf.DELTA))) # data['contents']['attributes']['sk_w'] = tmp_w # data['contents']['attributes']['sk_d'] = tmp_d import select import socket import sys import Queue inputs = [] outputs = [] # Prepare sockets elist = [] # list to store objects to return for cl_ip in relay_ips: # Fetch data from relay as a serialized sketch object s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # print >>sys.stderr, 'starting up on %s port %s' % (cl_ip, conf.RELAY_PORT) s.connect((cl_ip, conf.RELAY_PORT)) # s.listen(5) # Listen for incoming connections outputs.append(s) # Outgoing message queues (socket:Queue) # message_queues = {} while inputs or outputs: # Wait for at least one of the sockets to be ready for processing # print >>sys.stderr, '\nwaiting for the next event' readable, writable, exceptional = select.select(inputs, outputs, inputs) # Handle outputs for s in writable: SockExt.send_msg(s, json.dumps(data)) inputs.append(s) outputs.remove(s) # Handle inputs for s in readable: inp_data = SockExt.recv_msg(s) # print data inputs.remove(s) s.shutdown(socket.SHUT_RDWR) s.close() obj_json = json.loads(inp_data) if _type == "sketch": contents = json.loads(obj_json["return"]) tmp_w = int(data["contents"]["attributes"]["sk_w"]) tmp_d = int(data["contents"]["attributes"]["sk_d"]) tmp_delta = float(data["contents"]["attributes"]["delta"]) tmp_epsilon = float(data["contents"]["attributes"]["epsilon"]) # print tmp_epsilon # De-serialize sketch object sketch = Classes.CountSketchCt( tmp_w, tmp_d, EcPt.from_binary(binascii.unhexlify(contents["vars"]["pub"]), G), tmp_epsilon, tmp_delta, ) sketch.load_store_list(tmp_w, tmp_d, contents["store"]) # sketch.print_details() elist.append(sketch) elif _type == "values" or _type == "values_sq": contents = obj_json["return"] values = load_value_set(contents["store"]) elist.append(values) return elist except Exception as e: print "Exception while getting data from relay: " + str(e) traceback.print_exc()
def handle(self): global G global priv global pub try: inp = SockExt.recv_msg(self.request).strip() data = json.loads(inp) print "Request for: " + str(data['request']) if data['request'] == 'ping': contents = data['contents'] SockExt.send_msg( self.request, json.dumps({'return': int(contents['value']) + 1})) elif data['request'] == 'pubkey': SockExt.send_msg(self.request, json.dumps({'return': hexlify(pub.export())})) elif data['request'] == 'encrypt': contents = data['contents'] cipher_obj = Classes.Ct.enc(pub, contents['value']) json_obj = cipher_obj.to_JSON() SockExt.send_msg(self.request, json.dumps({'return': json_obj})) elif data['request'] == 'decrypt': contents = json.loads(data['contents']) #from pprint import pprint #print "--------" #print(contents) try: new_k = Bn.from_hex(contents['k']) except: new_k = None #reconstruct object cipher_obj = Classes.Ct( EcPt.from_binary(binascii.unhexlify(contents['pub']), G), EcPt.from_binary(binascii.unhexlify(contents['a']), G), EcPt.from_binary(binascii.unhexlify(contents['b']), G), new_k, None) value = cipher_obj.dec(priv) #decrypt ct SockExt.send_msg(self.request, json.dumps({'return': value})) #self.request.shutdown(socket.SHUT_RDWR) #self.request.close() elif data['request'] == 'partial_decrypt': contents = json.loads(data['contents']) #from pprint import pprint #print "--------" #print(contents) try: new_k = Bn.from_hex(contents['k']) except: new_k = None #reconstruct object cipher_obj = Classes.Ct( EcPt.from_binary(binascii.unhexlify(contents['pub']), G), EcPt.from_binary(binascii.unhexlify(contents['a']), G), EcPt.from_binary(binascii.unhexlify(contents['b']), G), new_k, None) ct = cipher_obj.partial_dec(priv) #decrypt ct SockExt.send_msg(self.request, json.dumps({'return': hexlify(ct.export())})) #self.request.shutdown(socket.SHUT_RDWR) #self.request.close() except Exception as e: print "Exception on incomming connection: ", e
def get_data_from_relays_non_blocking(relay_ips, data): try: _type = data['contents']['data_type'] #if _type == "sketch": #Compute sketch parameters #tmp_w = int(math.ceil(math.e / conf.EPSILON)) #tmp_d = int(math.ceil(math.log(1.0 / conf.DELTA))) #data['contents']['attributes']['sk_w'] = tmp_w #data['contents']['attributes']['sk_d'] = tmp_d import select import socket import sys import Queue inputs = [] outputs = [] #Prepare sockets elist = [] #list to store objects to return for cl_ip in relay_ips: #Fetch data from relay as a serialized sketch object s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #print >>sys.stderr, 'starting up on %s port %s' % (cl_ip, conf.RELAY_PORT) s.connect((cl_ip, conf.RELAY_PORT)) #s.listen(5) # Listen for incoming connections outputs.append(s) # Outgoing message queues (socket:Queue) #message_queues = {} while inputs or outputs: # Wait for at least one of the sockets to be ready for processing #print >>sys.stderr, '\nwaiting for the next event' readable, writable, exceptional = select.select( inputs, outputs, inputs) #Handle outputs for s in writable: SockExt.send_msg(s, json.dumps(data)) inputs.append(s) outputs.remove(s) # Handle inputs for s in readable: inp_data = SockExt.recv_msg(s) #print data inputs.remove(s) s.shutdown(socket.SHUT_RDWR) s.close() obj_json = json.loads(inp_data) if _type == "sketch": contents = json.loads(obj_json['return']) tmp_w = int(data['contents']['attributes']['sk_w']) tmp_d = int(data['contents']['attributes']['sk_d']) tmp_delta = float(data['contents']['attributes']['delta']) tmp_epsilon = float( data['contents']['attributes']['epsilon']) #print tmp_epsilon #De-serialize sketch object sketch = Classes.CountSketchCt( tmp_w, tmp_d, EcPt.from_binary( binascii.unhexlify(contents['vars']['pub']), G), tmp_epsilon, tmp_delta) sketch.load_store_list(tmp_w, tmp_d, contents['store']) #sketch.print_details() elist.append(sketch) elif _type == "values" or _type == "values_sq": contents = obj_json['return'] values = load_value_set(contents['store']) elist.append(values) return elist except Exception as e: print "Exception while getting data from relay: " + str(e) traceback.print_exc()
def handle(self): global G global priv global pub try: inp = SockExt.recv_msg(self.request).strip() data = json.loads(inp) print "Request for: " + str(data['request']) if data['request'] == 'ping': contents = data['contents'] SockExt.send_msg(self.request, json.dumps({'return': int(contents['value'])+1})) elif data['request'] == 'pubkey': SockExt.send_msg(self.request, json.dumps({'return': hexlify(pub.export())})) elif data['request'] == 'encrypt': contents = data['contents'] cipher_obj = Classes.Ct.enc(pub, contents['value']) json_obj = cipher_obj.to_JSON() SockExt.send_msg(self.request, json.dumps({'return': json_obj})) elif data['request'] == 'decrypt': contents = json.loads(data['contents']) #from pprint import pprint #print "--------" #print(contents) try: new_k = Bn.from_hex(contents['k']) except: new_k = None #reconstruct object cipher_obj = Classes.Ct( EcPt.from_binary(binascii.unhexlify(contents['pub']),G), EcPt.from_binary(binascii.unhexlify(contents['a']),G), EcPt.from_binary(binascii.unhexlify(contents['b']),G), new_k, None) value = cipher_obj.dec(priv) #decrypt ct SockExt.send_msg(self.request, json.dumps({'return': value})) #self.request.shutdown(socket.SHUT_RDWR) #self.request.close() elif data['request'] == 'partial_decrypt': contents = json.loads(data['contents']) #from pprint import pprint #print "--------" #print(contents) try: new_k = Bn.from_hex(contents['k']) except: new_k = None #reconstruct object cipher_obj = Classes.Ct( EcPt.from_binary(binascii.unhexlify(contents['pub']),G), EcPt.from_binary(binascii.unhexlify(contents['a']),G), EcPt.from_binary(binascii.unhexlify(contents['b']),G), new_k, None) ct = cipher_obj.partial_dec(priv) #decrypt ct SockExt.send_msg(self.request, json.dumps({'return': hexlify(ct.export())})) #self.request.shutdown(socket.SHUT_RDWR) #self.request.close() except Exception as e: print "Exception on incomming connection: ", e
def process_request(data, obj): #parameters contents = data['contents'] stat_type = contents['type'] attributes = contents['attributes'] attr_file = attributes['file'] attr_sheet = attributes['sheet'] attr_column_1 = attributes['column_1'] attr_column_2 = attributes['column_2'] attr_column_3 = attributes['column_3'] #Differential Privacy? if 'dp' in contents: dp = contents['dp'] else: dp = True #Run selected operation if (stat_type == 'median'): data['contents']['data_type'] = "sketch" sketches = get_data_from_relays_non_blocking( relays, data) #Gather sketches from relays sk_sum = Classes.CountSketchCt.aggregate(sketches) #Aggregate sketches result = op.median_operation(sk_sum, auths, dp) #Compute median on sum of sketches elif (stat_type == 'mean'): data['contents']['data_type'] = "values" value_sets = get_data_from_relays_non_blocking( relays, data) #Gather values from relays elist = [] #List the cts for vset in value_sets: for value in vset: elist.append(value) result = op.mean_operation(elist, auths, dp) #Compute mean from cts elif (stat_type == 'variance'): data['contents']['data_type'] = "values" value_sets = get_data_from_relays_non_blocking( relays, data) #Gather values from relays values_lst = concat_sets(value_sets) data['contents']['data_type'] = "values_sq" value_sets = get_data_from_relays_non_blocking( relays, data) #Gather squared values from relays values_sq_lst = concat_sets(value_sets) result = op.variance_operation(values_lst, values_sq_lst, auths, dp) #Compute variance from cts SockExt.send_msg( obj.request, json.dumps({ 'return': { 'success': 'True', 'type': stat_type, 'attribute': attr_column_1, 'value': result } })) print 'Stat computed. Listening for requests...' if conf.MEASUREMENT_MODE_PROCESSOR: obj.server.shutdown()