示例#1
0
    def set_param(self, index: int, value: bytes) -> None:
        if not isinstance(value, bytes):
            raise HTypeError("value", value, bytes)

        if index == 0:
            if len(value) != len(self._key):
                raise CipherParameterError("IV of XorCipher must be a bytes object "
                "which is the same size as the key.")
            else:
                self._iv = value
        else:
            raise CipherParameterError("Index exceeds (XorCipher use only one parameter).")
示例#2
0
    def decrypt(self, ciphertext: bytes, finalize=True) -> bytes:
        if not isinstance(ciphertext, bytes):
            raise HTypeError("ciphertext", ciphertext, bytes)
  
        if not self._key:
            raise KeyError("Please provide key before calling decrypt()")

        if self._in_process is CipherProcess.NONE:
            self._in_process = CipherProcess.DECRYPT
            self._data = b""
        
        if self._in_process is not CipherProcess.DECRYPT:
            raise ResetError("You are in {} process, please call reset() "
            "before calling decrypt().".format(self._in_process.name))

        if self._iv is None:
            raise CipherParameterError("IV has not yet been set.")

        self._data += ciphertext
        plaintext = b""
        while len(self._data) > len(self._key):
            data_to_enc, self._data = self._data[:len(self._key)], self._data[len(self._key):]
            block = bxor(data_to_enc, self._key)
            plaintext += bxor(block, self._iv)

        if finalize:
            plaintext += self.finalize()

        return plaintext
示例#3
0
    def decrypt(self, ciphertext: bytes, finalize=True) -> bytes:
        if not isinstance(ciphertext, bytes):
            raise HTypeError("ciphertext", ciphertext, bytes)

        if not self._key:
            raise KeyError("Please provide key before calling decrypt()")

        if self._aes is None:
            raise CipherParameterError("Please set iv value "
            "before calling decrypt().")

        if self._in_process is CipherProcess.NONE:
            self._in_process = CipherProcess.DECRYPT
            self._decryptor = self._aes.decryptor()
            self._unpadder = self._pkcs7.unpadder()

        if self._in_process is not CipherProcess.DECRYPT:
            raise ResetError("You are in {} process, please call reset() "
            "before calling decrypt().".format(self._in_process.name))

        padded_text = self._decryptor.update(ciphertext)
        plaintext = self._unpadder.update(padded_text)

        if finalize:
            plaintext += self.finalize()

        return plaintext
示例#4
0
    def encrypt(self, plaintext: bytes, finalize=True) -> bytes:
        if not isinstance(plaintext, bytes):
            raise HTypeError("plaintext", plaintext, bytes)

        if not self._key:
            raise KeyError("Please provide key before calling encrypt()")

        if self._aes is None:
            raise CipherParameterError("Please set nonce "
            "value before calling encrypt().")

        if self._in_process is CipherProcess.NONE:
            self._in_process = CipherProcess.ENCRYPT
            self._encryptor = self._aes.encryptor()

        if self._in_process is not CipherProcess.ENCRYPT:
            raise ResetError("You are in {} process, please call reset() "
            "before calling encrypt().".format(self._in_process.name))

        ciphertext = self._encryptor.update(plaintext)

        if finalize:
            ciphertext += self.finalize()

        return ciphertext
示例#5
0
    def set_param(self, index: int, param: bytes) -> None:
        if not isinstance(param, bytes):
            raise CipherParameterError("Parameters of AES must "
            "be a bytes object.")

        if index == 0:
            if len(param) * 8 != algorithms.AES.block_size:
                raise CipherParameterError("Invalid length of nonce "
                "value ({} bits), expected less than {} bits.".format(
                        len(param) * 8,
                        algorithms.AES.block_size
                ))

            self._nonce = param
            self._aes = Cipher(
                algorithms.AES(self._key),
                modes.CTR(self._nonce),
                default_backend()
            )
        else:
            raise CipherParameterError("AES only use the nonce "
            "value as its parameter.")
示例#6
0
    def __init__(self, key: bytes):
        if not isinstance(key, bytes):
            raise HTypeError("key", key, bytes)

        if len(key) * 8 not in algorithms.AES.key_sizes:
            raise CipherParameterError("Key size of AES must be in {} (bits), "
            "not {} (bytes).".format(
                set(algorithms.AES.key_sizes),
                len(key)
            ))

        super().__init__(key, number_of_params=1)
        self._aes: Cipher = None
        self._encryptor: CipherContext = None
        self._decryptor: CipherContext = None
        self._in_process: CipherProcess = CipherProcess.NONE
        self._nonce = None
示例#7
0
    def __init__(self, key: bytes):
        if not isinstance(key, bytes):
            raise HTypeError("key", key, bytes)

        if len(key) * 8 not in algorithms.AES.key_sizes:
            raise CipherParameterError("Key size of AES must be "
            "in {} (bits), not {} (bytes).".format(
                set(algorithms.AES.key_sizes),
                len(key)
            ))
        super().__init__(key, number_of_params=1)
        self._aes = None
        self._encryptor = None
        self._decryptor = None

        self._in_process = CipherProcess.NONE

        self._pkcs7 = padding.PKCS7(128)
        self._padder = None
        self._unpadder = None
        self._iv = None
示例#8
0
    def finalize(self) -> bytes:
        if self._aes is None:
            raise CipherParameterError("Please set nonce value "
            "before calling finalize().")

        if self._in_process is CipherProcess.ENCRYPT:
            finaltext = self._encryptor.finalize()
            self._encryptor = None

        elif self._in_process is CipherProcess.DECRYPT:
            finaltext = self._decryptor.finalize()
            self._decryptor = None

        elif self._in_process is CipherProcess.FINALIZED:
            raise ResetError("Unknown process in your object "
            "({}).".format(self._in_process.name))
        else:
            raise SymmetricError("Unknown process ({}).".format(self._in_process.name))

        self._in_process = CipherProcess.FINALIZED
        return finaltext
示例#9
0
 def get_param(self, index) -> bytes:
     if index == 0:
         return self._iv
     else:
         raise CipherParameterError("AES CBC only use the "
         "IV value as its parameter.")
示例#10
0
 def get_param(self, index):
     raise CipherParameterError("Index exceeds (NoCipher doesn't use any parameters).")
示例#11
0
 def get_param(self, index) -> bytes:
     if index == 0:
         return self._nonce
     else:
         raise CipherParameterError("AES only use the nonce "
         "value as its parameter.")
示例#12
0
    def get_param(self, index: int) -> bytes:
        if index == 0:
            return self._iv

        raise CipherParameterError("XorCipher use only one parameter.")