def prepare_forward_message(mixnodes_dict, message, dest, key, portEnum): params = getGlobalSphinxParams() group = params.group.G use_nodes_forward = rand_subset(mixnodes_dict.keys(), SecurityParameters.NUMBER_OF_MIXES) use_nodes_backward = rand_subset( mixnodes_dict.keys(), SecurityParameters.NUMBER_OF_MIXES) nodes_routing_forward = list(map(Nenc, use_nodes_forward)) nodes_routing_backward = list(map(Nenc, use_nodes_backward)) pks_chosen_nodes_forward = [ EcPt.from_binary(mixnodes_dict[key], group) for key in use_nodes_forward ] pks_chosen_nodes_backward = [ EcPt.from_binary(mixnodes_dict[key], group) for key in use_nodes_backward ] surbid, surbkeytuple, nymtuple = create_surb( params, nodes_routing_backward, pks_chosen_nodes_backward, self.ip) self.surbDict[surbid] = {'surbkeytuple': surbkeytuple} message['nymtuple'] = nymtuple message = encode(message) json_msg = self.encryptForDB(message, key, self.session_name) print(json_msg, len(json_msg)) header, delta = create_forward_message(params, nodes_routing_forward, pks_chosen_nodes_forward, dest, json_msg) return (header, delta, use_nodes_forward[0], surbid, use_nodes_backward[-1])
def report(): frank = request.form['frank'].split(DELIMITER) sender = request.form['sender'] reporter = request.form['reporter'] msg = request.form['message'] test = request.form['test'] db = get_db() sig = str_to_frank(frank, G) if test == "True": with open("test.txt", "rb") as file: data = file.read() info = data.split(DELIMITER.encode('ascii')) pk_s = EcPt.from_binary(info[0], G) pk_r = EcPt.from_binary(info[2], G) else: sender_kb = db.execute( 'SELECT keybase FROM user WHERE twitter = ?', (sender, )).fetchone()['keybase'] reporter_kb = db.execute( 'SELECT keybase FROM user WHERE twitter = ?', (reporter, )).fetchone()['keybase'] r = requests.get("https://" + sender_kb + ".keybase.pub/amf/amf_pk.txt") pk_s = EcPt.from_binary( binascii.a2b_base64(r.text.encode('ascii')), G) r = requests.get("https://" + reporter_kb + ".keybase.pub/amf/amf_pk.txt") pk_r = EcPt.from_binary( binascii.a2b_base64(r.text.encode('ascii')), G) db = get_db() g_db = db.execute('SELECT g FROM judge_keys').fetchone()['g'] o_db = db.execute('SELECT o FROM judge_keys').fetchone()['o'] sk_j_db = db.execute('SELECT sk_j FROM judge_keys').fetchone()['sk_j'] g = EcPt.from_binary(g_db, G) o = Bn.from_hex(o_db.decode('ascii')) sk_j = Bn.from_hex(sk_j_db.decode('ascii')) aux = (g, o) if not dmf.Judge(pk_s, pk_r, sk_j, msg, sig, aux): return "Message could not be authenticated by judge." else: prob = call_perspective_api(msg, ["TOXICITY"], PERSPECTIVE_KEY) if prob["TOXICITY"] > TOXICITY_THRESHOLD: db.execute('INSERT INTO blacklist VALUES (?)', (sender, )) db.commit() return sender + " has been added to the blacklist." else: return "Message not judged to be abusive."
def __add__(self, other): """ Add two ciphertexts, to produce a ciphertext of their sums. Also handles addition with a constant. """ o = self.pub.group.order() g = self.pub.group.generator() if isinstance(other, int): # Case for int other new_b = self.b + EcPt.from_binary( binascii.unhexlify(_n_table[str( (o + other) % o)]), G) # other * g new_k, new_m = None, None if self.k is not None: new_m = self.m + other # self.m.mod_add( other, o) return Ct(self.pub, self.a, new_b, self.k, new_m) else: # Case for ciphertext other if __debug__: assert self.pub == other.pub new_a = self.a + other.a new_b = self.b + other.b new_k, new_m = None, None if self.k is not None and other.k is not None: new_k = self.k.mod_add(other.k, o) new_m = self.m + other.m # self.m.mod_add(other.m, o) return Ct(self.pub, new_a, new_b, new_k, new_m)
def test_verify_policy(self): # Create new policy policy = UserPolicyInformation(sessionID="71a7de77413e42ab814e53c32acbc079", accepted_policies=self.sig.pub_key) policy.sessionID_sig = self.sig.sign_message(policy.sessionID) assert policy.accepted_policies == self.sig.pub_key # Serialize the policy serialized = PolicySerializer(policy).data # Post the policy response = self.client.post(self.url + '/verify_test', serialized) content = response.content # Decode the received policy data = JSONParser().parse(io.BytesIO(content)) restored = PolicySerializer(data=data) # Make sure correctly decoded assert restored.is_valid() public_key = EcPt.from_binary(unhexlify((data.get("accepted_policies")).encode()), self.sig.G) sig = get_sig_from_string(data.get("sessionID_sig")) # Check signature against public key assert Signature().verify_signature(public_key, sig, policy.sessionID)
def verify_policy(policy): try: policy = check_policy_format(policy) data = policy.data # Check if sessionID exists in DB if UserPolicyInformation.objects.filter( pk=data.get('sessionID')).exists(): sessionEntry = UserPolicyInformation.objects.get( pk=data.get('sessionID')) # Check that the public keys match if not data.get( 'accepted_policies') == sessionEntry.accepted_policies: return False public_key = EcPt.from_binary( unhexlify(sessionEntry.accepted_policies.encode()), EcGroup(713)) sig = get_sig_from_string(data.get("sessionID_sig")) # Make sure the SessionID is verified if not Signature.verify_signature(public_key, sig, data.get("sessionID")): return False # Add to the DB sessionEntry.verified = True sessionEntry.sessionID_sig = data.get('sessionID_sig') sessionEntry.save() return True else: return False except: return False
def load_store_list(self, w, d, store_dict): counter = 0 for i in range(d): for j in range(w): contents = store_dict[str(counter)] try: self.store[i][j] = 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), Bn.from_hex(contents['k']), Bn.from_hex(contents['m'])) counter += 1 except Exception as e: print "Exception while loading sketch store matrix: " + str( e) traceback.print_exc()
def __init__(self, key_bytes, public=True): """ Make a key given a public or private key in bytes """ self.G = _globalECG if public: self.sec = None self.pub = EcPt.from_binary(key_bytes, self.G) self.optim = None else: self.sec = Bn.from_binary(sha256(key_bytes).digest()) self.pub = self.sec * self.G.generator() self.optim = do_ecdsa_setup(self.G, self.sec)
def load_store_list(self, w, d, store_dict): counter = 0 for i in range(d): for j in range(w): contents = store_dict[str(counter)] try: self.store[i][j] = 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), Bn.from_hex(contents['k']), Bn.from_hex(contents['m'])) counter += 1 except Exception as e: print "Exception while loading sketch store matrix: " + str(e) traceback.print_exc()
def create_db_destination(self, destination, port): try: destination = self.db_list[destination] destination = (destination[0], destination[1], port) key = EcPt.from_binary(destination[1], getGlobalSphinxParams().group.G) return (destination, key) except Exception as e: raise Exception( 'Requested database not present or named incorrectly. {} not found. Error {}' .format(destination, e))
def verify_vrf(pub, vrf, message): pp = PublicParams.get_default() G = pp.ec_group g = G.generator() h = G.hash_to_point(b"1||" + message) v = EcPt.from_binary(vrf.value, G) s, t = decode(vrf.proof) R = t * g + s * pub Hr = t * h + s * v s2 = Bn.from_binary(sha256(encode([g, h, pub, v, R, Hr])).digest()) return s2 == s
def ext_hook(code, data): if code==0: num = Bn.from_binary(data[1:]) if data[0] == ord("-") or data[0] == "-": return -num return num elif code==1: nid = msgpack.unpackb(data) return EcGroup(nid) elif code == 2: nid, ptdata = msgpack.unpackb(data) return EcPt.from_binary(ptdata, EcGroup(nid)) return msgpack.ExtType(code, data)
def str_to_frank(l, G): elem1 = EcPt.from_binary(binascii.a2b_base64(l[0].encode('ascii')), G) elem2 = EcPt.from_binary(binascii.a2b_base64(l[1].encode('ascii')), G) elem3 = EcPt.from_binary(binascii.a2b_base64(l[2].encode('ascii')), G) elem4 = EcPt.from_binary(binascii.a2b_base64(l[3].encode('ascii')), G) elem5 = EcPt.from_binary(binascii.a2b_base64(l[4].encode('ascii')), G) elem6 = Bn.from_hex(l[5]) elem7 = Bn.from_hex(l[6]) elem8 = Bn.from_hex(l[7]) elem9 = Bn.from_hex(l[8]) elem10 = Bn.from_hex(l[9]) elem11 = Bn.from_hex(l[10]) elem12 = EcPt.from_binary(binascii.a2b_base64(l[11].encode('ascii')), G) elem13 = EcPt.from_binary(binascii.a2b_base64(l[12].encode('ascii')), G) elem14 = EcPt.from_binary(binascii.a2b_base64(l[13].encode('ascii')), G) elem15 = EcPt.from_binary(binascii.a2b_base64(l[14].encode('ascii')), G) return ((((elem1, elem2), ((elem3, elem4), elem5)), ((elem6, elem7, elem8), \ (elem9, elem10, elem11))), elem12, elem13, elem14, elem15)
def reply(self, secret: Bn, query: List[bytes]) -> List[bytes]: """ Compute a reply to a query. :param query: Query to be answered. :return: reply to the query """ reply = list() for kwd_h in query: kwd_pt = EcPt.from_binary(kwd_h, self.group) kwd_enc = secret * kwd_pt kwd_enc_bytes = kwd_enc.export() reply.append(kwd_enc_bytes) return reply
def reply(self, secret: Bn, query: List[bytes]) -> List[Bn]: """ Compute a reply to a query. :param secret: secret with which the keywords were encrypted during the publication :param query: query to be answered :return: reply to the query """ reply = list() for kwd_h in query: kwd_pt = EcPt.from_binary(kwd_h, self.group) kwd_enc = secret * kwd_pt kwd_enc_bytes = kwd_enc.export() reply.append(kwd_enc_bytes) return reply
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
def verify_vrf(pub, vrf, message): """Verify a VRF. Checks whether a VRF value and a proof correspond to the message. :param petlib.EcPt pub: VRF public key :param VrfContainer vrf: VRF value and proof :param bytes message: Message """ pp = PublicParams.get_default() G = pp.ec_group g = G.generator() h = G.hash_to_point(b"1||" + message) v = EcPt.from_binary(vrf.value, G) s, t = decode(vrf.proof) R = t * g + s * pub Hr = t * h + s * v s2 = Bn.from_binary(sha256(encode([g, h, pub, v, R, Hr])).digest()) return s2 == s
def compute_cardinalities( self, secret: Bn, reply: List[bytes], published: Tuple[int, CuckooFilter]) -> List[int]: """ Compute the cardinalyty of the intersection of sets between the reply to a query and the list of lists of points published by the server. :param secret: secret with which the query was encrypted :param reply: reply from the server :param published: list of lists of point published by the server :return: list of cardinalities for the intersection between the reply and each published list of points. """ n_docs = published[0] published_data = published[1] secret_inv = secret.mod_inverse(self.group.order()) cardinalities = [] # For optimisation the following assumptions are made # - all keywords in the query are different. # - all keywords in the document are different. kwds_dec = list() for kwd_h in reply: kwd_pt = EcPt.from_binary(kwd_h, self.group) kwd_pt_dec = secret_inv * kwd_pt kwd_bytes = kwd_pt_dec.export() kwds_dec.append(kwd_bytes) for doc_id in range(n_docs): n_matches = 0 encoded_doc_id = doc_id.to_bytes(DOC_ID_SIZE, byteorder="big") for kwd_dec in kwds_dec: kwd_docid_bytes = kwd_encode(encoded_doc_id, kwd_dec) if published_data.contains(kwd_docid_bytes): n_matches += 1 cardinalities.append(n_matches) return cardinalities
def ext_hook(code, data): # Decode Bn types if code == 0: num = Bn.from_binary(data[1:]) # Accomodate both Python 2 and Python 3 if data[0] == ord("-") or data[0] == "-": return -num return num # Decode EcGroup elif code == 1: nid = msgpack.unpackb(data) return EcGroup(nid) # Decode EcPt elif code == 2: nid, ptdata = msgpack.unpackb(data) return EcPt.from_binary(ptdata, EcGroup(nid)) # Other return msgpack.ExtType(code, data)
def __add__(self, other): """ Add two ciphertexts, to produce a ciphertext of their sums. Also handles addition with a constant. """ o = self.pub.group.order() g = self.pub.group.generator() if isinstance(other, int): # Case for int other new_b = self.b + EcPt.from_binary(binascii.unhexlify(_n_table[str((o + other) % o)]), G) # other * g new_k, new_m = None, None if self.k is not None: new_m = self.m + other # self.m.mod_add( other, o) return Ct(self.pub, self.a, new_b, self.k, new_m) else: # Case for ciphertext other if __debug__: assert self.pub == other.pub new_a = self.a + other.a new_b = self.b + other.b new_k, new_m = None, None if self.k is not None and other.k is not None: new_k = self.k.mod_add( other.k, o) new_m = self.m + other.m # self.m.mod_add(other.m, o) return Ct(self.pub, new_a, new_b, new_k, new_m)
def enc(pub, m): """ Produce a ciphertext, from a public key and message """ if isinstance(m, int): m = Bn(m) o = pub.group.order() k = o.random() #print "k: " + str(k) g = pub.group.generator() a = k * g b = k * pub + EcPt.from_binary(binascii.unhexlify(_n_table[str((o + m) % o)]), G) # m * g return Ct(pub, a, b, k, m) else: """ Produce a ciphertext, from a public key and another ciphertext """ #print "m.m: " + str(m.m) try: k = m.k g = pub.group.generator() a = k * g b = m.b + (k * pub) return Ct(pub, a, b, k, None) except Exception as e: print "Exception while encrypting ct: " + str(e)
def compute_cardinality(self, secret: Bn, reply: List[bytes], published) -> int: """ Compute the cardinalyty of the intersection of sets between the reply to a query and the list of points published by the server. :param reply: reply from the server :param published: list of point published by the server :return: cardinalityof the intersection of sets """ secret_inv = secret.mod_inverse(self.group.order()) kwds = list() for kwd_h in reply: kwd_pt = EcPt.from_binary(kwd_h, self.group) kwd_pt_dec = secret_inv * kwd_pt kwd_bytes = kwd_pt_dec.export() kwds.append(kwd_bytes) # The intersection of the 2 sets is the number of matches. return len(set(kwds) & set(published))
def enc(pub, m): """ Produce a ciphertext, from a public key and message """ if isinstance(m, int): m = Bn(m) o = pub.group.order() k = o.random() #print "k: " + str(k) g = pub.group.generator() a = k * g b = k * pub + EcPt.from_binary( binascii.unhexlify(_n_table[str((o + m) % o)]), G) # m * g return Ct(pub, a, b, k, m) else: """ Produce a ciphertext, from a public key and another ciphertext """ #print "m.m: " + str(m.m) try: k = m.k g = pub.group.generator() a = k * g b = m.b + (k * pub) return Ct(pub, a, b, k, None) except Exception as e: print "Exception while encrypting ct: " + str(e)
def handle_read(self): data = super().handle_read() if data: data = pickle.loads(data) iv = data["iv"] text = data["text"] pk = EcPt.from_binary(data["pk"], getGlobalSphinxParams().group.G) tag = data["tag"] decrypted_msg = decode(self.dbnode.decrypt(iv, text, pk, tag)) request_type = decrypted_msg['request_type'] client_pk = decrypted_msg['pk'][2] if request_type == RequestType.get_db_size.value: record_size = self.dbnode.getRecordsSize() reply = encode(record_size) self.socket.sendall(reply) elif request_type == RequestType.push_to_db.value: pir_handler = threading.Thread(target=self.handle_PIR, args=( decrypted_msg, client_pk, ), name="PIR handler") pir_handler.daemon = True pir_handler.start()
def cut_init_message(message): group_element, channel_key_onion, payload_onion = cut( message, GROUP_ELEMENT_LEN, MIX_COUNT * SYM_KEY_LEN) return EcPt.from_binary(group_element, params.group.G), channel_key_onion, payload_onion
register(twitter_user, kb_user, aux) elif i == "blacklist": get_blacklist() elif i[0:4] == "read": parser = argparse.ArgumentParser() parser.add_argument("-u", dest="user", default=None, const=None) parser.add_argument("-n", dest="count", type=int, default=1, nargs='?', const=1) args = parser.parse_args(i[4:].split()) sk_r = read_sk() response = requests.post(ADDRESS + "get_username", data = {'twitter':args.user}) kb_username = response.text r = requests.get("https://" + kb_username + ".keybase.pub/amf/amf_pk.txt") pk_s = EcPt.from_binary(binascii.a2b_base64(r.text.encode('ascii')), G) read(sk_r, aux, pk_s, pk_j, count=args.count, user=args.user) elif i[0:4] == "send": parser = argparse.ArgumentParser() parser.add_argument("-u", dest="user") parser.add_argument("-m", dest="message", nargs="*") args = parser.parse_args(i[4:].split()) sk_s = read_sk() response = requests.post(ADDRESS + "get_username", data = {'twitter':args.user}) kb_username = response.text r = requests.get("https://" + kb_username + ".keybase.pub/amf/amf_pk.txt") pk_r = EcPt.from_binary(binascii.a2b_base64(r.text.encode('ascii')), G) send(args.user, " ".join(args.message), sk_s, aux, pk_r, pk_j) elif i[0:6] == "report":
def get_judge(): response = requests.get(ADDRESS + "get_pk_j") return EcPt.from_binary(binascii.a2b_base64(response.text.encode('ascii')), G)
def strToEcPt(s, group): return EcPt.from_binary(binascii.unhexlify(s), group)
def ext_hook(code, data): if code == 2: return EcPt.from_binary(data, EcGroup(415)) return ExtType(code, data)
def get_aux(): response = requests.get(ADDRESS + "get_aux") info = response.text.split(DELIMITER) g = EcPt.from_binary(binascii.a2b_base64(info[0].encode('ascii')), G) o = Bn.from_hex(info[1]) return (g, o)
ap.add_argument(OWN_ADDR_ARG, help="ip and port, to listen for packets on.") ap.add_argument("config", help="Config file describing the mix chain.") args = ap.parse_args() # get own ip and port own_addr = parse_ip_port(getattr(args, OWN_ADDR_ARG)) mix_ip, mix_port, *mix_keys = read_cfg_values(args.config) # get mix ip and port mix_addr = parse_ip_port("{}:{}".format(mix_ip, mix_port)) # this entry point instance entry_point = EntryPoint(own_addr, mix_addr) # prepare the keys public_keys = [] group = EcGroup() for pub_file in mix_keys: with open("config/" + pub_file, "rb") as f: public_keys.append(EcPt.from_binary(f.read(), group)) # init the ciphers entry_point.set_keys(public_keys) entry_point.run()
def uid(self): return EcPt.from_binary(unhexlify(self._uid))