Beispiel #1
0
    def spawn_relay(self):
        accumulator = NullAccumulator()
        accumulator = SignatureAccumulator()

        ss = 0
        for tdh in self.trustee_dhkeys:
            v = verdict.TrusteeVerdict(tdh, self.client_keys,
                                       self.trustee_keys)
            ss = (ss + v.shared_secret()) % tdh.group.order()

        trustee_verdict = verdict.TrusteeVerdict(ss, self.client_keys,
                                                 self.trustee_keys, True)
        accumulator = EncryptedAccumulator(trustee_verdict)
        self.relay = Relay(self.trustee_count, accumulator, self.decoder,
                           self.rdecoder)
        self.relay.add_nyms(self.client_count)
Beispiel #2
0
def main():
    t0 = time.time()

    trustee_count = 3
    client_count = 10

    trustee_dhkeys, trustee_keys = gen_keys(trustee_count)
    client_dhkeys, client_keys = gen_keys(client_count)
    nym_dhkeys, nym_keys = gen_keys(client_count)

    trustees = []
    for idx in range(trustee_count):
        trustee = Trustee(trustee_dhkeys[idx], client_keys)
        trustee.add_nyms(nym_keys)
        trustees.append(trustee)

    clients = []
    for idx in range(client_count):
        certifier = NullCertifier()
        certifier = SignatureCertifier(client_dhkeys[idx], client_keys)
        certifier = EncryptedCertifier(
            verdict.Verdict(client_dhkeys[idx], client_keys))
        client = Client(client_dhkeys[idx], trustee_keys, certifier,
                        NullEncoder())
        client.add_own_nym(nym_dhkeys[idx])
        client.add_nyms(nym_keys)
        clients.append(client)

    accumulator = NullAccumulator()
    accumulator = SignatureAccumulator()
    accumulator = EncryptedAccumulator(global_group)
    relay = Relay(trustee_count, accumulator, NullDecoder())
    relay.add_nyms(client_count)
    relay.sync(None)

    trap_keys = []
    for trustee in trustees:
        trustee.sync(None)
        trap_keys.append(trustee.trap_keys[-1].public_key())

    for client in clients:
        client.sync(None, trap_keys)

    for idx in range(len(trustees)):
        trustee = trustees[idx]
        ciphertext = trustee.produce_interval_ciphertext()
        relay.store_trustee_ciphertext(idx, ciphertext)

    client_ciphertexts = []
    for client in clients:
        client_ciphertexts.append(client.produce_ciphertexts())
    print(relay.process_ciphertext(client_ciphertexts))

    print(time.time() - t0)
    t0 = time.time()

    client_ciphertexts = []
    for client in clients:
        client.send(client.own_nym_keys[0][1], bytes("Hello", "UTF-8"))
        client_ciphertexts.append(client.produce_ciphertexts())
    print(relay.process_ciphertext(client_ciphertexts))

    print(time.time() - t0)
    t0 = time.time()

    client_ciphertexts = []
    for client in clients:
        client_ciphertexts.append(client.produce_ciphertexts())
    print(relay.process_ciphertext(client_ciphertexts))

    print(time.time() - t0)
    t0 = time.time()
Beispiel #3
0
def main():
    t0 = time.time()

    trustee_count = 3
    client_count = 10

    trustee_dhkeys, trustee_keys = gen_keys(trustee_count)
    client_dhkeys, client_keys = gen_keys(client_count)
    nym_dhkeys, nym_keys = gen_keys(client_count)
    trap_dhkeys, trap_keys = gen_keys(trustee_count)

    trustees = []
    for idx in range(trustee_count):
        trustee = Trustee(trustee_dhkeys[idx], client_keys)
        trustee.add_nyms(nym_keys)
        trustees.append(trustee)

    clients = []
    for idx in range(client_count):
        certifier = NullCertifier()
        certifier = SignatureCertifier(client_dhkeys[idx], client_keys)
        certifier = EncryptedCertifier(
            verdict.ClientVerdict(client_dhkeys[idx], client_keys,
                                  trustee_keys))
        client = Client(client_dhkeys[idx], trustee_keys, certifier,
                        NullEncoder())
        client.add_own_nym(nym_dhkeys[idx])
        client.add_nyms(nym_keys)
        client.set_message_queue(queue.Queue())
        clients.append(client)

    accumulator = NullAccumulator()
    accumulator = SignatureAccumulator()
    accumulator = EncryptedAccumulator(global_group)
    relay = Relay(trustee_count, accumulator, NullDecoder())
    relay.add_nyms(client_count)
    relay.sync(None)

    for i, trustee in enumerate(trustees):
        trustee.sync(None, trap_dhkeys[i])

    for client in clients:
        client.sync(None, trap_keys)

    cleartexts = []
    for i in range(len(clients)):
        relay.decode_start()
        for idx in range(len(trustees)):
            trustee = trustees[idx]
            ciphertext = trustee.produce_ciphertext(i)
            relay.decode_trustee(ciphertext)

        for client in clients:
            ciphertext = client.produce_ciphertexts(i)
            relay.decode_client(ciphertext)

        cleartexts.append(relay.decode_cell().decode("utf-8"))
    print(cleartexts)

    print(time.time() - t0)
    t0 = time.time()

    for client in clients:
        message = bytes("Hello", "utf-8")
        message += bytes(cell_length - len(message))
        client.message_queue.put(message)

    cleartexts = []
    for i in range(len(clients)):
        relay.decode_start()
        for idx in range(len(trustees)):
            trustee = trustees[idx]
            ciphertext = trustee.produce_ciphertext(i)
            relay.decode_trustee(ciphertext)

        for client in clients:
            ciphertext = client.produce_ciphertexts(i)
            relay.decode_client(ciphertext)

        cleartexts.append(relay.decode_cell())
    print(cleartexts)

    print(time.time() - t0)
Beispiel #4
0
def main():
    logging.basicConfig()
    p = argparse.ArgumentParser(description="Basic DC-net relay")
    p.add_argument("-p",
                   "--port",
                   type=int,
                   help="Port to listen for \
                   connections on",
                   required=True,
                   dest="port")
    p.add_argument("config_dir")
    p.add_argument("-s",
                   "--socks",
                   type=str,
                   metavar="host:port",
                   help="SOCKS proxy address",
                   default="localhost:8080",
                   dest="socks_addr")
    p.add_argument("-v",
                   type=str,
                   help="display more output (default: WARN)",
                   choices=verbosity.keys(),
                   default="WARN",
                   dest="verbose")
    opts = p.parse_args()
    logger.setLevel(verbosity[opts.verbose])

    global socks_address
    saddr, sport = opts.socks_addr.split(":")
    socks_address = saddr, int(sport)

    system_config = config.load(config.SystemConfig,
                                os.path.join(opts.config_dir, "system.json"))
    nclients = len(system_config.clients.ids)
    ntrustees = len(system_config.trustees.ids)
    naps = len(system_config.aps.ids)

    # start up a new relay
    relay = dcnet.Relay(ntrustees, NullAccumulator(), NullDecoder())
    relay.add_nyms(nclients)
    relay.sync(None)

    # server socket
    print("Starting relay on {}".format(opts.port))
    ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    ssock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    ssock.bind(("0.0.0.0", opts.port))
    ssock.listen(1024)

    # make sure everybody connects
    print(("Waiting for {} clients, {} trustees, and {} access points").format(
        nclients, ntrustees, naps))
    ccli, ctru, caps = 0, 0, 0
    crsocks = [None] * nclients
    cwsocks = []
    tsocks = [None] * ntrustees
    apsocks = [None] * naps
    while ccli < nclients or ctru < ntrustees or caps < naps:
        conn, addr = ssock.accept()
        buf = bytearray(1)
        buf[0] = kind = bytes_to_long(conn.recv(1))
        buf.extend(conn.recv(m.sizes[kind] - 1))
        reg = {}
        m.unpack(buf, reg)
        conn.setblocking(0)

        if reg['kind'] == m.TRUSTEE_CONNECT and ctru < ntrustees:
            if tsocks[reg['node']] is not None:
                sys.exit("Trustee connected twice")
            tsocks[reg['node']] = conn
            ctru += 1
        elif reg['kind'] == m.CLIENT_CONNECT and ccli < nclients:
            if crsocks[reg['node']] is not None:
                sys.exit("Clients connected twice")
            crsocks[reg['node']] = conn
            if reg['ap'] == -1:
                cwsocks.append(conn)
            ccli += 1
        elif reg['kind'] == m.AP_CONNECT and caps < naps:
            if apsocks[reg['node']] is not None:
                sys.exit("Access point connected twice")
            apsocks[reg['node']] = conn
            cwsocks.append(conn)
            caps += 1
            global downcellmax
            # Leave room for IP/UDP headers and AP headers
            downcellmax = downmax - 28 - m.overhead[m.AP_DOWNSTREAM] - \
                                        m.overhead[m.RELAY_DOWNSTREAM]
        else:
            sys.exit("Illegal node number or connection type")
    print("All clients, trustees, and access points connected")

    upstreams = {}
    downstream = asyncio.Queue()
    scheduler = itertools.cycle(range(nclients))

    # start the main relay loop
    asyncio. async (main_loop(relay, tsocks, crsocks, cwsocks, upstreams,
                              downstream, scheduler))
    loop = asyncio.get_event_loop()
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass
    loop.close()
Beispiel #5
0
def main():
    t0 = time.time()

    p = argparse.ArgumentParser(description="Local, insecure DC-net test")
    p.add_argument("config_dir")
    opts = p.parse_args()

    # load the public system data
    with open(os.path.join(opts.config_dir, "system.json"), "r", encoding="utf-8") as fp:
        data = json.load(fp)
        clients = data["clients"]
        client_ids = [c["id"] for c in clients]
        client_keys = [PublicKey(global_group, c["key"]) for c in clients]
        trustees = data["servers"]
        trustee_ids = [t["id"] for t in trustees]
        trustee_keys = [PublicKey(global_group, t["key"]) for t in trustees]

    # and session data
    with open(os.path.join(opts.config_dir, "session.json"), "r", encoding="utf-8") as fp:
        data = json.load(fp)
        session_id = data["session-id"]
        nym_keys = [PublicKey(global_group, c["dhkey"]) for c in data["clients"]]

    # load the post-shuffle slots
    with open(os.path.join(opts.config_dir, "shuffle.json"), "r", encoding="utf-8") as fp:
        data = json.load(fp)
        slot_keys = [PublicKey(global_group, e) for e in data["slots"]]

    # start multiple clients in the same process
    # load private keys from individual files
    clients = []
    for iden in client_ids:
        with open(os.path.join(opts.config_dir, "{}.json".format(iden)), "r", encoding="utf-8") as fp:
            data = json.load(fp)
            private_key = PrivateKey(global_group, data["private_key"])
        with open(os.path.join(opts.config_dir, "{}-{}.json".format(iden, session_id)), "r", encoding="utf-8") as fp:
            data = json.load(fp)
            nym_private_key = PrivateKey(global_group, data["private_key"])
        client = dcnet.Client(private_key, trustee_keys, NullCertifier(), NullEncoder())
        client.add_own_nym(nym_private_key)
        client.add_nyms(slot_keys)
        clients.append(client)

    # same with trustees
    trustees = []
    for iden in trustee_ids:
        with open(os.path.join(opts.config_dir, "{}.json".format(iden)), "r", encoding="utf-8") as fp:
            data = json.load(fp)
            private_key = PrivateKey(global_group, data["private_key"])
        trustee = dcnet.Trustee(private_key, client_keys)
        trustee.add_nyms(slot_keys)
        trustees.append(trustee)

    # start a single relay
    relay = dcnet.Relay(len(trustees), NullAccumulator(), NullDecoder())
    relay.add_nyms(len(clients))
    relay.sync(None)

    trap_keys = []
    for trustee in trustees:
        trustee.sync(None)
        trap_keys.append(trustee.trap_keys[-1].public_key())

    for client in clients:
        client.sync(None, trap_keys)

    cleartexts = []
    for i in range(1):
        relay.decode_start()
        for idx in range(len(trustees)):
            trustee = trustees[idx]
            ciphertext = trustee.produce_ciphertext()
            relay.decode_trustee(ciphertext)

        for idx in range(len(clients)):
            client = clients[idx]
            ciphertext = client.produce_ciphertexts()
            relay.decode_client(ciphertext)

        cleartexts.append(relay.decode_cell())
    print(cleartexts)

    print(time.time() - t0)
    t0 = time.time()

    cleartexts = []
    for i in range(len(clients)):
        relay.decode_start()
        for idx in range(len(trustees)):
            trustee = trustees[idx]
            ciphertext = trustee.produce_ciphertext()
            relay.decode_trustee(ciphertext)

        for i, client in enumerate(clients):
            ciphertext = client.produce_ciphertexts()
            cleartext = long_to_bytes(0)
            if i == 0:
                cleartext = bytes("Hello", "UTF-8")
            ciphertext = long_to_bytes(
                    bytes_to_long(ciphertext) ^ bytes_to_long(cleartext))
            relay.decode_client(ciphertext)

        cleartexts.append(relay.decode_cell())
    print(cleartexts)

    print(time.time() - t0)