kmip/services/server/crypto/engine.py
def encrypt(self,
                encryption_algorithm,
                encryption_key,
                plain_text,
                cipher_mode=None,
                padding_method=None,
                iv_nonce=None,
                hashing_algorithm=None):
        """
        Encrypt data using symmetric or asymmetric encryption.

        Args:
            encryption_algorithm (CryptographicAlgorithm): An enumeration
                specifying the encryption algorithm to use for encryption.
            encryption_key (bytes): The bytes of the encryption key to use for
                encryption.
            plain_text (bytes): The bytes to be encrypted.
            cipher_mode (BlockCipherMode): An enumeration specifying the
                block cipher mode to use with the encryption algorithm.
                Required in the general case. Optional if the encryption
                algorithm is RC4 (aka ARC4). If optional, defaults to None.
            padding_method (PaddingMethod): An enumeration specifying the
                padding method to use on the data before encryption. Required
                if the cipher mode is for block ciphers (e.g., CBC, ECB).
                Optional otherwise, defaults to None.
            iv_nonce (bytes): The IV/nonce value to use to initialize the mode
                of the encryption algorithm. Optional, defaults to None. If
                required and not provided, it will be autogenerated and
                returned with the cipher text.
            hashing_algorithm (HashingAlgorithm): An enumeration specifying
                the hashing algorithm to use with the encryption algorithm,
                if needed. Required for OAEP-based asymmetric encryption.
                Optional, defaults to None.

        Returns:
            dict: A dictionary containing the encrypted data, with at least
                the following key/value fields:
                * cipher_text - the bytes of the encrypted data
                * iv_nonce - the bytes of the IV/counter/nonce used if it
                    was needed by the encryption scheme and if it was
                    automatically generated for the encryption

        Raises:
            InvalidField: Raised when the algorithm is unsupported or the
                length is incompatible with the algorithm.
            CryptographicFailure: Raised when the key generation process
                fails.

        Example:
            >>> engine = CryptographyEngine()
            >>> result = engine.encrypt(
            ...     encryption_algorithm=CryptographicAlgorithm.AES,
            ...     encryption_key=(
            ...         b'\xF3\x96\xE7\x1C\xCF\xCD\xEC\x1F'
            ...         b'\xFC\xE2\x8E\xA6\xF8\x74\x28\xB0'
            ...     ),
            ...     plain_text=(
            ...         b'\x00\x01\x02\x03\x04\x05\x06\x07'
            ...         b'\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F'
            ...     ),
            ...     cipher_mode=BlockCipherMode.CBC,
            ...     padding_method=PaddingMethod.ANSI_X923,
            ... )
            >>> result.get('cipher_text')
            b'\x18[\xb9y\x1bL\xd1\x8f\x9a\xa0e\x02b\xa3=c'
            >>> result.iv_counter_nonce
            b'8qA\x05\xc4\x86\x03\xd9=\xef\xdf\xb8ke\x9a\xa2'
        """
        if encryption_algorithm is None:
            raise exceptions.InvalidField("Encryption algorithm is required.")

        if encryption_algorithm == enums.CryptographicAlgorithm.RSA:
            return self._encrypt_asymmetric(
                encryption_algorithm,
                encryption_key,
                plain_text,
                padding_method,
                hashing_algorithm=hashing_algorithm
            )
        else:
            return self._encrypt_symmetric(
                encryption_algorithm,
                encryption_key,
                plain_text,
                cipher_mode=cipher_mode,
                padding_method=padding_method,
                iv_nonce=iv_nonce
            )
def encrypt(self,
                encryption_algorithm,
                encryption_key,
                plain_text,
                cipher_mode=None,
                padding_method=None,
                iv_nonce=None,
                hashing_algorithm=None):
        if encryption_algorithm is None:
            raise exceptions.InvalidField("Encryption algorithm is required.")

        if encryption_algorithm == enums.CryptographicAlgorithm.RSA:
            return self._encrypt_asymmetric(
                encryption_algorithm,
                encryption_key,
                plain_text,
                padding_method,
                hashing_algorithm=hashing_algorithm
            )
        else:
            return self._encrypt_symmetric(
                encryption_algorithm,
                encryption_key,
                plain_text,
                cipher_mode=cipher_mode,
                padding_method=padding_method,
                iv_nonce=iv_nonce
            )
def encrypt ( self , encryption_algorithm , encryption_key , plain_text , cipher_mode = None , padding_method = None , iv_nonce = None , hashing_algorithm = None ) : if encryption_algorithm is None : raise exceptions . InvalidField ( "Encryption algorithm is required." ) if encryption_algorithm == enums . CryptographicAlgorithm . RSA : return self . _encrypt_asymmetric ( encryption_algorithm , encryption_key , plain_text , padding_method , hashing_algorithm = hashing_algorithm ) else : return self . _encrypt_symmetric ( encryption_algorithm , encryption_key , plain_text , cipher_mode = cipher_mode , padding_method = padding_method , iv_nonce = iv_nonce )
Encrypt data using symmetric or asymmetric encryption.

        Args:
            encryption_algorithm (CryptographicAlgorithm): An enumeration
                specifying the encryption algorithm to use for encryption.
            encryption_key (bytes): The bytes of the encryption key to use for
                encryption.
            plain_text (bytes): The bytes to be encrypted.
            cipher_mode (BlockCipherMode): An enumeration specifying the
                block cipher mode to use with the encryption algorithm.
                Required in the general case. Optional if the encryption
                algorithm is RC4 (aka ARC4). If optional, defaults to None.
            padding_method (PaddingMethod): An enumeration specifying the
                padding method to use on the data before encryption. Required
                if the cipher mode is for block ciphers (e.g., CBC, ECB).
                Optional otherwise, defaults to None.
            iv_nonce (bytes): The IV/nonce value to use to initialize the mode
                of the encryption algorithm. Optional, defaults to None. If
                required and not provided, it will be autogenerated and
                returned with the cipher text.
            hashing_algorithm (HashingAlgorithm): An enumeration specifying
                the hashing algorithm to use with the encryption algorithm,
                if needed. Required for OAEP-based asymmetric encryption.
                Optional, defaults to None.

        Returns:
            dict: A dictionary containing the encrypted data, with at least
                the following key/value fields:
                * cipher_text - the bytes of the encrypted data
                * iv_nonce - the bytes of the IV/counter/nonce used if it
                    was needed by the encryption scheme and if it was
                    automatically generated for the encryption

        Raises:
            InvalidField: Raised when the algorithm is unsupported or the
                length is incompatible with the algorithm.
            CryptographicFailure: Raised when the key generation process
                fails.

        Example:
            >>> engine = CryptographyEngine()
            >>> result = engine.encrypt(
            ...     encryption_algorithm=CryptographicAlgorithm.AES,
            ...     encryption_key=(
            ...         b'\xF3\x96\xE7\x1C\xCF\xCD\xEC\x1F'
            ...         b'\xFC\xE2\x8E\xA6\xF8\x74\x28\xB0'
            ...     ),
            ...     plain_text=(
            ...         b'\x00\x01\x02\x03\x04\x05\x06\x07'
            ...         b'\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F'
            ...     ),
            ...     cipher_mode=BlockCipherMode.CBC,
            ...     padding_method=PaddingMethod.ANSI_X923,
            ... )
            >>> result.get('cipher_text')
            b'\x18[\xb9y\x1bL\xd1\x8f\x9a\xa0e\x02b\xa3=c'
            >>> result.iv_counter_nonce
            b'8qA\x05\xc4\x86\x03\xd9=\xef\xdf\xb8ke\x9a\xa2'
Encrypt data using symmetric or asymmetric encryption .