def unpickle_pub_keys(self, msgs): """ Leader uses this method to unpack keys from other nodes """ addrs = [] key_dict = {} key_dict[self.id] = (self.key_from_file(1), AnonCrypto.sign(self.id, self.key1, self.key2.get_pubkey())) for data in msgs: (rem_id, rem_round, rem_ip, rem_port, rem_key1, rem_key2) = marshal.loads(data) self.debug("Unpickled msg from node %d" % (rem_id)) if rem_round != self.round_id: raise RuntimeError, "Mismatched round numbers! (mine: %d, other: %d)" % ( self.round_id, rem_round) self.debug("Before reading verification key") k1 = AnonCrypto.vk_key_from_str(rem_key1) self.pub_keys[rem_id] = (k1, k1) self.debug("Before reading public key") k2 = AnonCrypto.pub_key_from_str( AnonCrypto.verify(self.pub_keys, rem_key2)) self.pub_keys[rem_id] = (k1, k2) addrs.append((rem_ip, rem_port)) key_dict[rem_id] = (rem_key1, rem_key2) self.debug("After handling the keys") return (marshal.dumps((self.round_id, key_dict)), addrs)
def unpickle_pub_keys(self, msgs): """ Leader uses this method to unpack keys from other nodes """ addrs = [] key_dict = {} key_dict[self.id] = ( self.key_from_file(1), AnonCrypto.sign(self.id, self.key1, self.key_from_file(2))) for data in msgs: (rem_id, rem_round, rem_ip, rem_port, rem_key1, rem_key2) = marshal.loads(data) self.debug("Unpickled msg from node %d" % (rem_id)) if rem_round != self.round_id: raise RuntimeError, "Mismatched round numbers! (mine: %d, other: %d)" % ( self.round_id, rem_round) k1 = AnonCrypto.pub_key_from_str(rem_key1) self.pub_keys[rem_id] = (k1, k1) k2 = AnonCrypto.pub_key_from_str(AnonCrypto.verify(self.pub_keys, rem_key2)) self.pub_keys[rem_id] = (k1, k2) addrs.append((rem_ip, rem_port)) key_dict[rem_id] = (rem_key1, rem_key2) return (marshal.dumps((self.round_id, key_dict)), addrs)
def phase1_msg(self): """ Message that non-leader nodes send to the leader. """ return marshal.dumps( (self.id, self.round_id, self.ip, self.port, self.key_from_file(1), AnonCrypto.sign(self.id, self.key1, self.key_from_file(2)))) return marshal.dumps((self.round_id, newdict))
def broadcast_to_all_nodes(self, msg, signed=True): if not self.am_leader(): raise RuntimeError, 'Only leader can broadcast' if signed: outmsg = AnonCrypto.sign(self.id, self.key1, msg) else: outmsg = msg AnonNet.broadcast_using(self.sockets, AnonNet.send_to_socket, outmsg)
def broadcast_to_all_nodes(self, msg, signed = True): if not self.am_leader(): raise RuntimeError, 'Only leader can broadcast' if signed: outmsg = AnonCrypto.sign(self.id, self.key1, msg) else: outmsg = msg AnonNet.broadcast_using(self.sockets, AnonNet.send_to_socket, outmsg)
def phase1_msg(self): """ Message that non-leader nodes send to the leader. """ #ToDo: Generate the hash of the encryption keys and send it instead of the keys themselves # for the equivocation check. return marshal.dumps( (self.id, self.round_id, self.ip, self.port, self.key_from_file(1), AnonCrypto.sign(self.id, self.key1, AnonCrypto.pub_key_to_str(self.key2)))) return marshal.dumps((self.round_id, newdict))
def phase1_msg(self): """ Message that non-leader nodes send to the leader. """ return marshal.dumps( (self.id, self.round_id, self.ip, self.port, self.key_from_file(1), AnonCrypto.sign(self.id, self.key1, self.key_from_file(2)))) return marshal.dumps((self.round_id, newdict))
def run_phase4(self): self.advance_phase() if self.am_leader(): self.debug("Leader broadcasting ciphers to all nodes") self.broadcast_to_all_nodes(marshal.dumps(self.final_ciphers)) self.debug("Cipher set len %d" % (len(self.final_ciphers))) else: """ Get C' ciphertexts from leader. """ self.final_ciphers = marshal.loads(self.recv_from_leader()) """ self.final_ciphers holds an array of pickled (round_id, cipher_prime) tuples """ my_cipher_str = marshal.dumps((self.round_id, self.cipher_prime)) go = False if my_cipher_str in self.final_ciphers: self.info("Found my ciphertext in set") go = True else: self.critical("ABORT! My ciphertext is not in set!") self.debug(self.final_ciphers) go = False raise RuntimeError, "Protocol violation: My ciphertext is missing!" hashval = AnonCrypto.hash_list(self.final_ciphers) go_msg = marshal.dumps(( self.id, self.round_id, go, hashval)) go_data = '' if self.am_leader(): """ Collect go msgs """ data = self.recv_from_all(False) """ Add leader's signed GO message to set """ data.append(AnonCrypto.sign(self.id, self.key1, go_msg)) go_data = marshal.dumps((data)) self.broadcast_to_all_nodes(go_data) else: """ Send go msg to leader """ self.send_to_leader(go_msg) go_data = self.recv_from_leader() self.check_go_data(hashval, go_data) self.info("All nodes report GO") return
def run_phase5(self): self.advance_phase() self.info("Starting phase 5") #Create list of outputs and individual signature addresses=self.get_list_outputs() self.my_sign = AnonCrypto.sign(self.id, self.key1, marshal.dumps((self.id, addresses))) #Everybody sends her own signature to the leader. if self.am_leader(): all_sign = self.recv_from_all() self.debug("Received all signatures from participants") else: self.send_to_leader(marshal.dumps((self.round_id, self.my_sign))) self.debug("Sent signature to leader") #The leader broadcast the list of signatures and every participant checks if her signature # is included if self.am_leader(): self.broadcast_to_all_nodes(marshal.dumps(marshal.dumps((self.id, all_sign)))) self.debug("All signatures sent to participants") self.debug("Sign = TRUE") else: all_sign = marshal.loads(self.recv_from_socket(self.leader_socket)) self.debug("Received signature list") check_sig = marshal.dumps((self.round_id, self.my_sign)) if check_sig in all_sign: self.info("Found my signature in set") self.debug("Sign = TRUE") self.critical("Sign = TRUE") self.send_to_leader(marshal.dumps((self.id, "input address"))) else: self.critical("ABORT! My signature is not in set!") self.debug(self.final_ciphers) self.critical("Sign = FALSE") raise RuntimeError, "Protocol violation: My signature is missing!" #The leader waits for ack of every participant before creating the Bitcoin transaction if self.am_leader(): """ Leader waits for ciphers from member N. """ self.final_acks = self.recv_from_all() self.debug("Got acks from other nodes") self.critical("Leader: Bitcoin transaction finished")
def run_phase5(self): self.advance_phase() self.info("Starting phase 5") #Create list of outputs and individual signature addresses = self.get_list_outputs() self.my_sign = AnonCrypto.sign(self.id, self.key1, marshal.dumps((self.id, addresses))) #Everybody sends her own signature to the leader. if self.am_leader(): all_sign = self.recv_from_all() self.debug("Received all signatures from participants") else: self.send_to_leader(marshal.dumps((self.round_id, self.my_sign))) self.debug("Sent signature to leader") #The leader broadcast the list of signatures and every participant checks if her signature # is included if self.am_leader(): self.broadcast_to_all_nodes( marshal.dumps(marshal.dumps((self.id, all_sign)))) self.debug("All signatures sent to participants") self.debug("Sign = TRUE") else: all_sign = marshal.loads(self.recv_from_socket(self.leader_socket)) self.debug("Received signature list") check_sig = marshal.dumps((self.round_id, self.my_sign)) if check_sig in all_sign: self.info("Found my signature in set") self.debug("Sign = TRUE") self.critical("Sign = TRUE") self.send_to_leader(marshal.dumps((self.id, "input address"))) else: self.critical("ABORT! My signature is not in set!") self.debug(self.final_ciphers) self.critical("Sign = FALSE") raise RuntimeError, "Protocol violation: My signature is missing!" #The leader waits for ack of every participant before creating the Bitcoin transaction if self.am_leader(): """ Leader waits for ciphers from member N. """ self.final_acks = self.recv_from_all() self.debug("Got acks from other nodes") self.critical("Leader: Bitcoin transaction finished")
def run_phase4(self): self.advance_phase() if self.am_leader(): self.debug("Leader broadcasting ciphers to all nodes") self.broadcast_to_all_nodes(marshal.dumps(self.final_ciphers)) self.debug("Cipher set len %d" % (len(self.final_ciphers))) else: """ Get C' ciphertexts from leader. """ self.final_ciphers = marshal.loads(self.recv_from_leader()) """ self.final_ciphers holds an array of pickled (round_id, cipher_prime) tuples """ my_cipher_str = marshal.dumps((self.round_id, self.cipher_prime)) go = False if my_cipher_str in self.final_ciphers: self.info("Found my ciphertext in set") go = True else: self.critical("ABORT! My ciphertext is not in set!") self.debug(self.final_ciphers) go = False raise RuntimeError, "Protocol violation: My ciphertext is missing!" hashval = AnonCrypto.hash_list(self.final_ciphers) go_msg = marshal.dumps((self.id, self.round_id, go, hashval)) go_data = '' if self.am_leader(): """ Collect go msgs """ data = self.recv_from_all(False) """ Add leader's signed GO message to set """ data.append(AnonCrypto.sign(self.id, self.key1, go_msg)) go_data = marshal.dumps((data)) self.broadcast_to_all_nodes(go_data) else: """ Send go msg to leader """ self.send_to_leader(go_msg) go_data = self.recv_from_leader() self.check_go_data(hashval, go_data) self.info("All nodes report GO") return
def run_phase5(self): self.advance_phase() mykeystr = AnonCrypto.sign( self.id, self.key1, marshal.dumps((self.id, self.round_id, AnonCrypto.priv_key_to_str(self.key2)))) if self.am_leader(): data = self.recv_from_all() """ Add leader's signed key to set """ data.append(mykeystr) self.debug("Key data... len = %d" % len(data)) self.broadcast_to_all_nodes(marshal.dumps(data)) else: self.info('Sending key to leader') self.send_to_leader(mykeystr) data = marshal.loads(self.recv_from_leader()) self.info("Got key set from leader, len = %d" % len(data)) self.decrypt_ciphers(data) self.info('Decrypted ciphertexts')
def run_phase5(self): self.advance_phase() mykeystr = AnonCrypto.sign(self.id, self.key1, marshal.dumps(( self.id, self.round_id, AnonCrypto.priv_key_to_str(self.key2)))) if self.am_leader(): data = self.recv_from_all() """ Add leader's signed key to set """ data.append(mykeystr) self.debug("Key data... len = %d" % len(data)) self.broadcast_to_all_nodes(marshal.dumps(data)) else: self.info('Sending key to leader') self.send_to_leader(mykeystr) data = marshal.loads(self.recv_from_leader()) self.info("Got key set from leader, len = %d" % len(data)) self.decrypt_ciphers(data) self.info('Decrypted ciphertexts')
def send_to_socket(self, sock, msg, signed=True): if signed: outmsg = AnonCrypto.sign(self.id, self.key1, msg) else: outmsg = msg AnonNet.send_to_socket(sock, 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)
def send_to_socket(self, sock, msg, signed=True): if signed: outmsg = AnonCrypto.sign(self.id, self.key1, msg) else: outmsg = msg AnonNet.send_to_socket(sock, 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)