def accept_phase(self, ip, port, nonce): # package and encrypt data response = marshal.dumps((nonce, self.ip, self.gui_port)) cipher = AnonCrypto.sign_with_key(self.privKey, response) # respond with ((ip, port), encrypted_data) AnonNet.send_to_addr(ip, int(port), marshal.dumps(("accept", cipher)))
def recv_interest_voucher(self, data): msg, key = marshal.loads(data) (nonce, leader_ip, leader_gui_port, leader_com_port) = marshal.loads(msg) self.DEBUG( "Start round voucher from %s:%s, communicating at port %s" % (leader_ip, leader_gui_port, leader_com_port) ) # get the path to the file you want to share self.emit(SIGNAL("getSharedFilename()")) verified = self.verify(leader_ip, leader_gui_port, data) """ generate temporary keys for this round so leader can aggregate """ self.gen_temp_keys() temp1_str = AnonCrypto.pub_key_to_str(self.pubgenkey1) temp2_str = AnonCrypto.pub_key_to_str(self.pubgenkey2) """ default to random file of 128 bytes if you don't have anything to share """ if verified: if os.path.exists(self.shared_filename): self.DEBUG("You are sharing file %s" % (self.shared_filename)) else: self.DEBUG("Not a valid file path, continuing without sharing...") # respond with your interest self.DEBUG("Verified leader, participating as %s:%s at port %s" % (self.ip, self.gui_port, self.com_port)) response = marshal.dumps((nonce, self.ip, self.gui_port, self.com_port, temp1_str, temp2_str)) cipher = AnonCrypto.sign_with_key(self.privKey, response) AnonNet.send_to_addr(leader_ip, int(leader_gui_port), marshal.dumps(("interested", cipher))) else: self.DEBUG("Unkown leader, opting out...")
def prepare_round(self): # can't start round without 3 or more peers if len(self.participants) < 3: self.DEBUG("Not enough peers to start round!") return prepare_voucher = marshal.dumps( (int(PREPARE_WAIT), int(1), copy.copy(self.participants), self.ip, self.gui_port, self.com_port) ) cipher = AnonCrypto.sign_with_key(self.privKey, prepare_voucher) for index, participant in enumerate(self.participants): down_index = (index - 1) % len(self.participants) up_index = (index + 1) % len(self.participants) if (self.ip, self.gui_port, self.com_port) != (participant[1], participant[2], participant[3]): AnonNet.send_to_addr( participant[1], participant[2], marshal.dumps(("prepare:%s:%s:%s" % (index, down_index, up_index), cipher)), ) self.DEBUG( "Sending prepare to peer %s:%s at port %s" % (participant[1], participant[2], participant[3]) ) # after informing the participants, create your node dn_idx = -1 % len(self.participants) up_idx = 1 self.start_node(dn_idx, up_idx, self.participants, 0) # start round after PREPARE_WAIT minutes DelayTimer(PREPARE_WAIT, self.run_protocol).start()
def recv_interest_voucher(self, data): msg, key = marshal.loads(data) (nonce, leader_ip, leader_gui_port, leader_com_port) = marshal.loads(msg) self.DEBUG("Start round voucher from %s:%s, communicating at port %s" % (leader_ip, leader_gui_port, leader_com_port)) # get the path to the file you want to share self.emit(SIGNAL("getSharedFilename()")) verified = self.verify(leader_ip, leader_gui_port, data) """ generate temporary keys for this round so leader can aggregate """ self.gen_temp_keys() temp1_str = AnonCrypto.pub_key_to_str(self.pubgenkey1) temp2_str = AnonCrypto.pub_key_to_str(self.pubgenkey2) """ default to random file of 128 bytes if you don't have anything to share """ if verified: if os.path.exists(self.shared_filename): self.DEBUG("You are sharing file %s" % (self.shared_filename)) else: self.DEBUG("Not a valid file path, continuing without sharing...") # respond with your interest self.DEBUG("Verified leader, participating as %s:%s at port %s" % (self.ip, self.gui_port, self.com_port)) response = marshal.dumps((nonce,self.ip,self.gui_port,self.com_port,temp1_str,temp2_str)) cipher = AnonCrypto.sign_with_key(self.privKey, response) AnonNet.send_to_addr(leader_ip, int(leader_gui_port), marshal.dumps(("interested", cipher))) else: self.DEBUG("Unkown leader, opting out...")
def accept_phase(self, ip, port, nonce): # package and encrypt data response = marshal.dumps((nonce,self.ip,self.gui_port)) cipher = AnonCrypto.sign_with_key(self.privKey, response) # respond with ((ip, port), encrypted_data) AnonNet.send_to_addr(ip, int(port), marshal.dumps(("accept", cipher)))
def invite_phase(self, ip, port, pubkey): # create nonce, # peers, vector containing (ip, port, pubkey) of all peers nonce = 1 num_peers = len(self.nodes) + 1 peer_vector = [(self.ip, self.gui_port, self.public_key_string())] for node in self.nodes: hashkey = self.hash_peer(node[0], node[1]) if hashkey != self.hashkey: peer_vector.append(node) # package the text up into (nonce, N, [array of peer data]) invite = marshal.dumps((nonce, num_peers, peer_vector)) # sign it cipher = AnonCrypto.sign_with_key(self.privKey, invite) # send to invitee packaged with who it's coming from ((ip:port), signed(text)) AnonNet.send_to_addr(ip, int(port), marshal.dumps(("invite", cipher)))
def invite_phase(self, ip, port, pubkey): # create nonce, # peers, vector containing (ip, port, pubkey) of all peers nonce = 1 num_peers = len(self.nodes) + 1 peer_vector = [(self.ip,self.gui_port,self.public_key_string())] for node in self.nodes: hashkey = self.hash_peer(node[0], node[1]) if hashkey != self.hashkey: peer_vector.append(node) # package the text up into (nonce, N, [array of peer data]) invite = marshal.dumps((nonce,num_peers,peer_vector)) # sign it cipher = AnonCrypto.sign_with_key(self.privKey, invite) # send to invitee packaged with who it's coming from ((ip:port), signed(text)) AnonNet.send_to_addr(ip, int(port), marshal.dumps(("invite", cipher)))
def prepare_round(self): # can't start round without 3 or more peers if len(self.participants) < 3: self.DEBUG("Not enough peers to start round!") return prepare_voucher = marshal.dumps((int(PREPARE_WAIT),int(1),copy.copy(self.participants), self.ip, self.gui_port, self.com_port)) cipher = AnonCrypto.sign_with_key(self.privKey, prepare_voucher) for index, participant in enumerate(self.participants): down_index = (index - 1) % len(self.participants) up_index = (index + 1) % len(self.participants) if (self.ip, self.gui_port, self.com_port) != (participant[1], participant[2], participant[3]): AnonNet.send_to_addr(participant[1], participant[2], \ marshal.dumps(("prepare:%s:%s:%s"%(index, down_index, up_index),cipher))) self.DEBUG("Sending prepare to peer %s:%s at port %s" % (participant[1], participant[2], participant[3])) # after informing the participants, create your node dn_idx = -1 % len(self.participants) up_idx = 1 self.start_node(dn_idx, up_idx, self.participants, 0) # start round after PREPARE_WAIT minutes DelayTimer(PREPARE_WAIT, self.run_protocol).start()
def send_to_addr(self, ip, port, msg, signed=True): if signed: outmsg = AnonCrypto.sign(self.id, self.key1, msg) else: outmsg = msg AnonNet.send_to_addr(ip, port, outmsg)
def send_to_addr(self, ip, port, msg, signed = True): if signed: outmsg = AnonCrypto.sign(self.id, self.key1, msg) else: outmsg = msg AnonNet.send_to_addr(ip, port, outmsg)