Example #1
0
def re_decrypt(data=None,
               re_enc_length=const.RE_ENC_LENGTH,
               seed=None,
               key=None,
               iv=None,
               init_val=None,
               debug=0):
    """
    Re-decrypt data using the given parameters.
    :param data: data to re-decrypt
    :param re_enc_length: number of re-encrypted bytes
    :param seed: seed that randomly selected re-encrypted bytes
    :param key: symmetric key to decrypt randomly selected bytes
    :param iv: initialisation vector for the symmetric decryption
    :param init_val: initial value (ONLY FOR CTR MODE)
    :param debug: if 1, prints will be shown during execution; default 0, no prints are shown
    :return: re-decrypted data
    """

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

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

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

    # Check if the number of bytes to re-encrypt is lesser than the data length
    if re_enc_length < len(data):  # Apply punctured encryption

        return remove_punctured_enc(data, re_enc_length, seed, key, iv,
                                    init_val, debug)

    else:  # Re-encryption of the whole data

        sym_cipher = sym.get_cipher(AES.MODE_CTR, init_val, None, key, iv)
        return sym.decrypt(sym_cipher, data, debug)
Example #2
0
def remove_punctured_enc(data,
                         re_enc_length,
                         seed,
                         key,
                         iv,
                         init_val,
                         debug=0):
    """
    Remove punctured encryption to the given data: 're_enc_length' bytes are randomly selected using the seed and
    symmetrically decrypted using the given key and iv.
    :param data: data to re-decrypt
    :param re_enc_length: number of re-encrypted bytes
    :param seed: seed that randomly selected re-encrypted bytes
    :param key: symmetric key to decrypt randomly selected bytes
    :param iv: initialisation vector for the symmetric decryption
    :param debug: if 1, prints will be shown during execution; default 0, no prints are shown
    :return: re-decrypted data
    """

    # Get random re-encrypted bytes to decrypt
    bytes_to_re_dec, re_dec_indexes = get_bytes_to_re_enc(
        data, re_enc_length, seed, debug)

    if debug:  # ONLY USE FOR DEBUG
        print('BYTES TO RE-DECRYPT = (%d) %s' %
              (len(bytes_to_re_dec), bytes_to_re_dec))
        print('INDEX TO RE-DECRYPT = (%d) %s' %
              (len(re_dec_indexes), re_dec_indexes))

    # Decrypt re-encrypted data bytes
    sym_cipher = sym.get_cipher(AES.MODE_CTR, init_val, None, key, iv)
    re_dec_bytes = sym.decrypt(sym_cipher, bytes_to_re_dec, debug)

    if debug:  # ONLY USE FOR DEBUG
        print('RE-DECRYPTED BYTES = (%s) (%d) %s' %
              (type(re_dec_bytes), len(re_dec_bytes), re_dec_bytes))

    # Replace bytes with re-decrypted ones in data
    return replace_bytes(data, re_dec_bytes, re_dec_indexes, debug)
Example #3
0
    def _decode(self, full_path, chunk_num, offset, sym_cipher, re_enc_args):
        """
        Remove AONT and encryption from the given chunk and write the result on the temporary file
        :param chunk_num: number of file chunk to anti-transform and decrypt
        :param offset: position where the result must be written
        :param sym_cipher: symmetric cipher
        """

        if self.enc_fp.closed:
            self.enc_fp = open(full_path, 'rb+')

        self.enc_fp.seek(0, os.SEEK_END)
        file_size = self.enc_fp.tell()
        if file_size <= chunk_num * self.meta['random_size']:
            return

        # Move file pointer
        self.enc_fp.seek(chunk_num * self.meta['chunk_size'])

        # Read file chunk
        chunk = self.enc_fp.read(self.meta['chunk_size'])

        # If applied, remove all re-encryption operations
        if self.debug:
            print("Remove re-encryptions from file chunk")

        re_enc_ops_num = len(self.meta['re_encs'])

        re_enc_init_val = self._get_cipher_initial_value(
            int(chunk_num) * self.meta['chunk_size'])

        if re_enc_ops_num > 0:
            for i in range(re_enc_ops_num):
                index = re_enc_ops_num - 1 - i

                # Add other params to re-encryption params
                re_enc_args[index]['init_val'] = re_enc_init_val

                # Remove re-encryption
                chunk = re_enc.remove_re_enc(chunk, re_enc_args[index],
                                             self.debug)

                if self.debug:
                    print("DE-RE-ENCRYPTED CHUNK = (%d) %s" %
                          (len(chunk), chunk))
                    print("Re-encryption successfully removed")

            if self.debug:
                print("Re-encryptions successfully removed")

        if self.debug:
            print("Remove AONT from encrypted file chunk")

        # Get AONT anti-transformation parameters
        aont_args = self._create_aont_anti_transf_params(len(chunk))
        # Anti-transform file chunk
        chunk = aont.anti_transform(chunk, aont_args, self.debug)

        if self.debug:
            print("ANTI-TRANSFORMED CHUNK = (%d) %s" % (len(chunk), chunk))
            print("AONT successfully removed")

        # Decrypt the anti-transformed file chunk with the sym key and write it on the temporary file
        x = sym.decrypt(sym_cipher, chunk, self.debug)
        # x = chunk
        if self.debug:
            print("got chunk in _decode: ", x)

        # Write anti-transformed decrypted chunk on the temporary file at its proper position
        if self.debug:
            print("writing on temp file at position", offset, "byte ", x)

        self.temp_fp.seek(offset)
        self.temp_fp.write(x)

        # Reset both file pointers
        self.enc_fp.seek(0)
        self.temp_fp.seek(0)