def pk_from_dict(dict_object):
    pk = PublicKey()
    pk.g = dict_object['p']
    pk.q = dict_object['q']
    pk.g = dict_object['g']
    pk.y = dict_object['y']
    return pk
Example #2
0
def pk_from_dict(dict_object):
    pk = PublicKey()
    pk.g = dict_object['p']
    pk.q = dict_object['q']
    pk.g = dict_object['g']
    pk.y = dict_object['y']
    return pk
    def get_generic_election(cls, nr_candidates, max_choices, pk):
        elections = cls._generic_elections
        if (max_choices is None
            or max_choices <= 0
            or max_choices > nr_candidates):

            max_choices = nr_candidates

        key = (nr_candidates, max_choices, pk.p, pk.g, pk.q, pk.y)
        if key in elections:
            return elections[key]

        candidates = ["Candidate #%d" % x for x in xrange(nr_candidates)]
        public_key = PublicKey()
        public_key.p = pk.p
        public_key.g = pk.g
        public_key.q = pk.q
        public_key.y = pk.y

        election = Election(candidates,
                            max_choices     =   max_choices,
                            public_key      =   public_key)
        elections[key] = election
        return election
Example #4
0
    def get_generic_election(cls, nr_candidates, max_choices, pk):
        elections = cls._generic_elections
        if (max_choices is None or max_choices <= 0
                or max_choices > nr_candidates):

            max_choices = nr_candidates

        key = (nr_candidates, max_choices, pk.p, pk.g, pk.q, pk.y)
        if key in elections:
            return elections[key]

        candidates = ["Candidate #%d" % x for x in xrange(nr_candidates)]
        public_key = PublicKey()
        public_key.p = pk.p
        public_key.g = pk.g
        public_key.q = pk.q
        public_key.y = pk.y

        election = Election(candidates,
                            max_choices=max_choices,
                            public_key=public_key)
        elections[key] = election
        return election
Example #5
0
 [...]

Question C: Who is your candidate #C?
Answers: [0]
"""

_default_crypto = Crypto()
# from helios/views.py
from cryptosystems import c2048 as crypto

p, q, g, x, y = crypto()
_default_crypto.p = p
_default_crypto.q = q
_default_crypto.g = g

_default_public_key = PublicKey()
_default_public_key.p = _default_crypto.p
_default_public_key.q = _default_crypto.q
_default_public_key.g = _default_crypto.g

_default_secret_key = SecretKey()
_default_secret_key.x = x
_default_secret_key.pk = _default_public_key

_default_public_key.y = y


def get_timestamp():
    return datetime.strftime(datetime.utcnow(), "%Y-%m-%dT%H:%M:%S.%fZ")

Question C: Who is your candidate #C?
Answers: [0]
"""


_default_crypto = Crypto()
# from helios/views.py
from cryptosystems import c2048 as crypto

p, q, g, x, y = crypto()
_default_crypto.p = p
_default_crypto.q = q
_default_crypto.g = g

_default_public_key = PublicKey()
_default_public_key.p = _default_crypto.p
_default_public_key.q = _default_crypto.q
_default_public_key.g = _default_crypto.g

_default_secret_key = SecretKey()
_default_secret_key.x = x
_default_secret_key.pk = _default_public_key

_default_public_key.y = y


def get_timestamp():
    return datetime.strftime(datetime.utcnow(), "%Y-%m-%dT%H:%M:%S.%fZ")

Example #7
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)
Example #8
0
import argparse
import json
import os
import random
import shutil

from Crypto.Hash import SHA256
from Crypto.Util.number import long_to_bytes, bytes_to_long

from elgamal import PublicKey, PrivateKey
from dcnet import global_group

long_to_pub = lambda x: PublicKey(global_group, x)
pub_to_long = lambda x: x.element

long_to_priv = lambda x: PrivateKey(global_group, x)
priv_to_long = lambda x: x.secret

MCAST_ADDR = "224.0.0.251"


class Relay:
    HOST = "host"
    PORT = "port"

    def __init__(self, host, port):
        self.host = host
        self.port = port

    def save(self):
        return {