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 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 recv_from_all(self, verify=True): if not self.am_leader(): raise RuntimeError, 'Only leader can broadcast' indata = AnonNet.recv_from_n(self.sockets) if verify: outdata = [] for d in indata: outdata.append(AnonCrypto.verify(self.pub_keys, d)) return outdata else: return indata
def recv_from_all(self, verify=True): if not self.am_leader(): raise RuntimeError, "Only leader can broadcast" indata = AnonNet.recv_from_n(self.sockets) if verify: outdata = [] for d in indata: outdata.append(AnonCrypto.verify(self.pub_keys, d)) return outdata else: return indata
def check_go_data(self, hashval, pickled_list): go_lst = marshal.loads(pickled_list) for item in go_lst: """ Verify signature on "GO" message """ item_str = AnonCrypto.verify(self.pub_keys, item) (r_id, r_round, r_go, r_hash) = marshal.loads(item_str) if r_round != self.round_id: raise RuntimeError, "Mismatched round numbers" if not r_go: raise RuntimeError, "Node %d reports failure!" % (r_id) if r_hash != hashval: raise RuntimeError, "Node %d produced bad hash!" % (r_id) return True
def unpickle_keyset(self, keys): """ Non-leader nodes use this to decode leader's key msg """ (rem_round_id, keydict) = marshal.loads(keys) if rem_round_id != self.round_id: raise RuntimeError, "Mismatched round ids" for i in keydict: s1, s2 = keydict[i] k1 = AnonCrypto.vk_key_from_str(s1) #k1.check_key() self.pub_keys[i] = (k1, k1) k2 = AnonCrypto.pub_key_from_str( AnonCrypto.verify(self.pub_keys, s2)) #k2.check_key() self.pub_keys[i] = (k1, k2) self.info('Unpickled public keys')
def unpickle_keyset(self, keys): """ Non-leader nodes use this to decode leader's key msg """ (rem_round_id, keydict) = marshal.loads(keys) if rem_round_id != self.round_id: raise RuntimeError, "Mismatched round ids" for i in keydict: s1,s2 = keydict[i] k1 = AnonCrypto.pub_key_from_str(s1) k1.check_key() self.pub_keys[i] = (k1, k1) k2 = AnonCrypto.pub_key_from_str(AnonCrypto.verify(self.pub_keys, s2)) k2.check_key() self.pub_keys[i] = (k1, k2) self.info('Unpickled public keys')
def decrypt_ciphers(self, keyset): priv_keys = {} for item in keyset: """ Verify signature on each key """ item_str = AnonCrypto.verify(self.pub_keys, item) (r_id, r_roundid, r_keystr) = marshal.loads(item_str) if r_roundid != self.round_id: raise RuntimeError, 'Mismatched round numbers' priv_keys[r_id] = AnonCrypto.priv_key_from_str(r_keystr) plaintexts = [] for cipher in self.final_ciphers: (r_round, cipher_prime) = marshal.loads(cipher) if r_round != self.round_id: raise RuntimeError, 'Mismatched round ids' for i in xrange(0, self.n_nodes): cipher_prime = AnonCrypto.decrypt_with_rsa(priv_keys[i], cipher_prime) plaintexts.append(self.unpackage_msg(cipher_prime)) self.anon_data = plaintexts
def decrypt_ciphers(self, keyset): priv_keys = {} for item in keyset: """ Verify signature on each key """ item_str = AnonCrypto.verify(self.pub_keys, item) (r_id, r_roundid, r_keystr) = marshal.loads(item_str) if r_roundid != self.round_id: raise RuntimeError, 'Mismatched round numbers' priv_keys[r_id] = AnonCrypto.priv_key_from_str(r_keystr) plaintexts = [] for cipher in self.final_ciphers: (r_round, cipher_prime) = marshal.loads(cipher) if r_round != self.round_id: raise RuntimeError, 'Mismatched round ids' for i in xrange(0, self.n_nodes): cipher_prime = AnonCrypto.decrypt_with_rsa( priv_keys[i], cipher_prime) plaintexts.append(self.unpackage_msg(cipher_prime)) self.anon_data = plaintexts
def recv_from_socket(self, sock, verify=True): d = AnonNet.recv_from_socket(sock) if verify: d = AnonCrypto.verify(self.pub_keys, d) return d
def recv_once(self, verify=True): d = AnonNet.recv_once(self.ip, self.port) if verify: d = AnonCrypto.verify(self.pub_keys, d) return d