Beispiel #1
0
def decrypt_seed_key_len(enc_seed_key_len=None, pk_file=None, sk_file=None, debug=0):
    """
    Decrypt encrypted seed, symmetric key and re-encryption length with ABE using the given public and secret key.
    :param enc_seed_key: encrypted seed, symmetric key and re-encryption length to decrypt
    :param pk_file: ABE public key
    :param sk_file: ABE secret key
    :param debug: if 1, prints will be shown during execution; default 0, no prints are shown
    :return: decrypted seed, symmetric key and number of re-encryption length
    """

    import logging
    import os.path

    # Check if enc_seed_key is set
    if enc_seed_key_len is None:
        logging.error('decrypt_seed_key ciphertext exception')
        if debug:  # ONLY USE FOR DEBUG
            print('EXCEPTION in decrypt_seed_key ciphertext')
        raise Exception

    # Check if pk_file is set and it exists
    if pk_file is None or not os.path.isfile(pk_file):
        logging.error('[ERROR] decrypt_seed_key pk_file exception')
        if debug:  # ONLY USE FOR DEBUG
            print('EXCEPTION in decrypt_seed_key pk_file')
        raise Exception

    # Check if sk_file is set and it exists
    if sk_file is None or not os.path.isfile(sk_file):
        logging.error('decrypt_seed_key sk_file exception')
        if debug:  # ONLY USE FOR DEBUG
            print('EXCEPTION in decrypt_seed_key sk_file')
        raise Exception

    # Decrypt data with ABE
    pairing_group = pg.pairing_group_create('MNT224')
    with open(pk_file, 'rb') as f:
        pk = bytesToObject(f.read(), pairing_group)
    with open(sk_file, 'rb') as f:
        sk = bytesToObject(f.read(), pairing_group)
    # cpabe = AC17CPABE(pairing_group, 2)
    cpabe = CPabe_BSW07(pairing_group)
    enc_data = cpabe.decrypt(pk, sk, enc_seed_key_len)

    from re_enc_engine.const import H, SYM_KEY_DEFAULT_SIZE, SEED_LENGTH
    import struct

    # Retrieve params from decryption output file
    seed, key, re_enc_length = struct.unpack('%ds%dsH' % (SEED_LENGTH, SYM_KEY_DEFAULT_SIZE), enc_data)

    if debug:  # ONLY USE FOR DEBUG
        print('DECRYPTED SEED = (%d) %s' % (len(seed), seed))
        print('DECRYPTED KEY = (%d) %s' % (len(key), key))
        print('DECRYPTED RE_ENC_LENGTH = %d' % re_enc_length)

    return seed, key, re_enc_length
Beispiel #2
0
def main(store, abe_keys_outfile):

    # Instantiate a bilinear pairing map. 'MNT224' represents an asymmetric curve with 224-bit base field.
    pairing_group = pg.pairing_group_create('MNT224')

    # CP-ABE
    cpabe = CPabe_BSW07(pairing_group)

    # Run the setup algorithm
    (pk, msk) = cpabe.setup()

    # Generate a user secret key
    user_attr_list = ['DEPT1', 'TEAM1']
    user_key = cpabe.keygen(pk, msk, user_attr_list)

    # Generate keys data structure
    #policy_str = '((PATIENT and SMART-1032702) or (PRACTITIONER and SMART-PRACTITIONER-72004454))'
    #print("---------- PK ----------")
    #print(pk)

    #print("---------- MSK (needed only for debug)----------")
    #print(msk)

    #print("---------- SK ----------")
    #print(user_key)

    data = {
        hashlib.sha256(objectToBytes(pk, pairing_group)).hexdigest(): {
            'pk': objectToBytes(pk, pairing_group).hex(),
            #'msk': msk,
            'sk': objectToBytes(user_key, pairing_group).hex(),
        }
    }

    ## print(json.dumps(data))

    ######
    # abe_keys_file = str(Path.home()) + '/.abe_keys.json'

    # Ask if keys have to be saved
    if not store:
        store = True if input("Should I write them on your " +
                              abe_keys_outfile +
                              "[Y/N]? ").lower() == 'y' else False

    if store:
        with open(abe_keys_outfile, 'w') as f:
            json.dump(data, f)
        print("ABE keys have been saved in", abe_keys_outfile)
    else:
        print("Could we at least be friend?")
Beispiel #3
0
    def __init__(self,
                 root,
                 chunk_size=128,
                 random_size=32,
                 initial_re_encs_num=0,
                 max_re_encs_num=1024,
                 debug=0):
        """
        Initialise global system parameters
        :param root: intercepted directory
        :param chunk_size: chunk size in bytes for AONT operations
        :param random_size: random part size in bytes for AONT operations
        :param initial_re_encs_num: number of initial re-encryptions to apply to new files [USED ONLY FOR PERFORMANCE EVALUATION]
        :param max_re_encs_num: maximum number of permitted re-encryptions [USED FOR REVERSE HASH CHAINING]
        :param debug: if 1, prints will be shown during execution; default 0, no prints are shown
        """

        # Define starting time in millis for performance evaluation
        self.starting_time = time() * 1000.0

        # Define variables for AONT
        self.chunk_size = chunk_size
        self.random_size = random_size

        # Define re-encryption variables
        self.pairing_group = pg.pairing_group_create(const.PAIRING_GROUP_CURVE)
        self.initial_re_encs_num = initial_re_encs_num
        self.max_re_encs_num = max_re_encs_num
        _, self.last_seed_pg_elem = pg.random_string_gen(
            self.pairing_group, const.SEED_LENGTH)
        _, self.last_key_pg_elem = pg.sym_key_gen(self.pairing_group,
                                                  const.SYM_KEY_DEFAULT_SIZE)
        self.root_iv = sym.iv_gen(const.IV_DEFAULT_SIZE)
        self.re_enc_args = [None for _ in range(self.initial_re_encs_num)]

        # Define debug variable
        self.debug = debug

        # Load ABE keys
        self._load_abe_keys(str(Path.home()) + '/.abe_keys.json')

        super(Abebox, self).__init__(root)
Beispiel #4
0
    def __init__(self,
                 root,
                 chunk_size=128,
                 random_size=32,
                 initial_re_encs_num=0,
                 debug=0):

        self.starting_time = time() * 1000.0

        # Define variables for AONT
        self.chunk_size = chunk_size
        self.random_size = random_size

        # Define re-encryption variables
        self.pairing_group = pg.pairing_group_create(const.PAIRING_GROUP_CURVE)
        self.initial_re_encs_num = initial_re_encs_num
        self.re_enc_args = [None for _ in range(self.initial_re_encs_num)]

        self.debug = debug

        # Load ABE keys
        self._load_abe_keys(str(Path.home()) + '/.abe_keys.json')

        super(Abebox, self).__init__(root)
Beispiel #5
0
        f.write(write_string)
        #f.seek(0, 0)
    with open('mountdir/prova', 'w+') as f:
        read_string = f.read()
        print("written string: {}\t read string: {}".format(
            write_string, read_string))
        #self.assertEqual(read_string, write_string)

    # with(open('mountdir/prova', 'w+')) as fin:
    #     fin.write('AA')
    #     fin.seek(0, 0)
    #     print(fin.read())

    exit(0)

    pair_g = pg.pairing_group_create()
    # last_elem = pg.random_pairing_group_elem_gen(pair_g)
    # max_hops = 100

    b = b'eJxNU0FuxDAI/EqUcw7g2Ab3K1UVbau97W3bSlXVv5cBnN1DLBtjmBkmv+txfNwu9/txrC/L+v7zeb2v22LR78vt6+rR18bb0nRbZGyL2r6LfXZmqrkMjRsmS2lkBwu2ZgG2ZVi+2K3u8w13u+447VGO2ZZWsMF9sZ3iJcpZSt0jv8+sgmaMG9TesxE6RwEGINSlnqg8GzXjRBJ8KnhJBvpZA7fYUJmkJF879DIx2kFqBohDCCkRZJ6KSItmKAk6Ll6hqBjo93jrhB2kBmNkQDfPIkl+rh02qAzUvU/UI6HVlO1BgylBhjrlPGGsGsgQVc0bcJaeLM/ZQFFPgeJ1n4UxEU0keNBmbuMTAOcUR04+IhKPPA/qwiryFJSW3LyRq4NmqOLUIXlOESjdTmXSlJ5kwp4UCFBkOqfnrCG9TgWR6JrCru7A8XAkurlN3Fzs+TkHzH2kVdwHFBZrqYzk8JwTRMGPohSS1TSBjCeF23SxjpQbLohfiUOscQ4WrkEbfHWczh8pqqba+vb3Dz4zux0='
    el = bytesToObject(b, pair_g)

    import re_enc_engine.pairing_group_primitives as eng_pg

    r = pair_g.init(ZR, int(hashPair(el).decode('utf-8'), 16))
    h_el = el**r
    # el = pair_g.random(G2)
    # el0 = pair_g.random(G2)
    #el0 = eng_pg.hash_chain(pair_g, el, 1)
    #print('\n\n')
    print(el.type, objectToBytes(el, pair_g))