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

        Args:
            encryption_algorithm (CryptographicAlgorithm): An enumeration
                specifying the asymmetric encryption algorithm to use for
                encryption. Required.
            encryption_key (bytes): The bytes of the public key to use for
                encryption. Required.
            plain_text (bytes): The bytes to be encrypted. Required.
            padding_method (PaddingMethod): An enumeration specifying the
                padding method to use with the asymmetric encryption
                algorithm. Required.
            hashing_algorithm (HashingAlgorithm): An enumeration specifying
                the hashing algorithm to use with the encryption padding
                method. Required, if the padding method is OAEP. Optional
                otherwise, defaults to None.

        Returns:
            dict: A dictionary containing the encrypted data, with at least
                the following key/value field:
                * cipher_text - the bytes of the encrypted data

        Raises:
            InvalidField: Raised when the algorithm is unsupported or the
                length is incompatible with the algorithm.
            CryptographicFailure: Raised when the key generation process
                fails.
        """
        if encryption_algorithm == enums.CryptographicAlgorithm.RSA:
            if padding_method == enums.PaddingMethod.OAEP:
                hash_algorithm = self._encryption_hash_algorithms.get(
                    hashing_algorithm
                )
                if hash_algorithm is None:
                    raise exceptions.InvalidField(
                        "The hashing algorithm '{0}' is not supported for "
                        "asymmetric encryption.".format(hashing_algorithm)
                    )

                padding_method = asymmetric_padding.OAEP(
                    mgf=asymmetric_padding.MGF1(
                        algorithm=hash_algorithm()
                    ),
                    algorithm=hash_algorithm(),
                    label=None
                )
            elif padding_method == enums.PaddingMethod.PKCS1v15:
                padding_method = asymmetric_padding.PKCS1v15()
            else:
                raise exceptions.InvalidField(
                    "The padding method '{0}' is not supported for asymmetric "
                    "encryption.".format(padding_method)
                )

            backend = default_backend()

            try:
                public_key = backend.load_der_public_key(encryption_key)
            except Exception:
                try:
                    public_key = backend.load_pem_public_key(encryption_key)
                except Exception:
                    raise exceptions.CryptographicFailure(
                        "The public key bytes could not be loaded."
                    )
            cipher_text = public_key.encrypt(
                plain_text,
                padding_method
            )
            return {'cipher_text': cipher_text}
        else:
            raise exceptions.InvalidField(
                "The cryptographic algorithm '{0}' is not supported for "
                "asymmetric encryption.".format(encryption_algorithm)
            )
def _encrypt_asymmetric(self,
                            encryption_algorithm,
                            encryption_key,
                            plain_text,
                            padding_method,
                            hashing_algorithm=None):
        """
        Encrypt data using asymmetric encryption.

        Args:
            encryption_algorithm (CryptographicAlgorithm): An enumeration
                specifying the asymmetric encryption algorithm to use for
                encryption. Required.
            encryption_key (bytes): The bytes of the public key to use for
                encryption. Required.
            plain_text (bytes): The bytes to be encrypted. Required.
            padding_method (PaddingMethod): An enumeration specifying the
                padding method to use with the asymmetric encryption
                algorithm. Required.
            hashing_algorithm (HashingAlgorithm): An enumeration specifying
                the hashing algorithm to use with the encryption padding
                method. Required, if the padding method is OAEP. Optional
                otherwise, defaults to None.

        Returns:
            dict: A dictionary containing the encrypted data, with at least
                the following key/value field:
                * cipher_text - the bytes of the encrypted data

        Raises:
            InvalidField: Raised when the algorithm is unsupported or the
                length is incompatible with the algorithm.
            CryptographicFailure: Raised when the key generation process
                fails.
        """
        if encryption_algorithm == enums.CryptographicAlgorithm.RSA:
            if padding_method == enums.PaddingMethod.OAEP:
                hash_algorithm = self._encryption_hash_algorithms.get(
                    hashing_algorithm
                )
                if hash_algorithm is None:
                    raise exceptions.InvalidField(
                        "The hashing algorithm '{0}' is not supported for "
                        "asymmetric encryption.".format(hashing_algorithm)
                    )

                padding_method = asymmetric_padding.OAEP(
                    mgf=asymmetric_padding.MGF1(
                        algorithm=hash_algorithm()
                    ),
                    algorithm=hash_algorithm(),
                    label=None
                )
            elif padding_method == enums.PaddingMethod.PKCS1v15:
                padding_method = asymmetric_padding.PKCS1v15()
            else:
                raise exceptions.InvalidField(
                    "The padding method '{0}' is not supported for asymmetric "
                    "encryption.".format(padding_method)
                )

            backend = default_backend()

            try:
                public_key = backend.load_der_public_key(encryption_key)
            except Exception:
                try:
                    public_key = backend.load_pem_public_key(encryption_key)
                except Exception:
                    raise exceptions.CryptographicFailure(
                        "The public key bytes could not be loaded."
                    )
            cipher_text = public_key.encrypt(
                plain_text,
                padding_method
            )
            return {'cipher_text': cipher_text}
        else:
            raise exceptions.InvalidField(
                "The cryptographic algorithm '{0}' is not supported for "
                "asymmetric encryption.".format(encryption_algorithm)
            )
def _encrypt_asymmetric ( self , encryption_algorithm , encryption_key , plain_text , padding_method , hashing_algorithm = None ) : if encryption_algorithm == enums . CryptographicAlgorithm . RSA : if padding_method == enums . PaddingMethod . OAEP : hash_algorithm = self . _encryption_hash_algorithms . get ( hashing_algorithm ) if hash_algorithm is None : raise exceptions . InvalidField ( "The hashing algorithm '{0}' is not supported for " "asymmetric encryption." . format ( hashing_algorithm ) ) padding_method = asymmetric_padding . OAEP ( mgf = asymmetric_padding . MGF1 ( algorithm = hash_algorithm ( ) ) , algorithm = hash_algorithm ( ) , label = None ) elif padding_method == enums . PaddingMethod . PKCS1v15 : padding_method = asymmetric_padding . PKCS1v15 ( ) else : raise exceptions . InvalidField ( "The padding method '{0}' is not supported for asymmetric " "encryption." . format ( padding_method ) ) backend = default_backend ( ) try : public_key = backend . load_der_public_key ( encryption_key ) except Exception : try : public_key = backend . load_pem_public_key ( encryption_key ) except Exception : raise exceptions . CryptographicFailure ( "The public key bytes could not be loaded." ) cipher_text = public_key . encrypt ( plain_text , padding_method ) return { 'cipher_text' : cipher_text } else : raise exceptions . InvalidField ( "The cryptographic algorithm '{0}' is not supported for " "asymmetric encryption." . format ( encryption_algorithm ) )
Encrypt data using asymmetric encryption.

        Args:
            encryption_algorithm (CryptographicAlgorithm): An enumeration
                specifying the asymmetric encryption algorithm to use for
                encryption. Required.
            encryption_key (bytes): The bytes of the public key to use for
                encryption. Required.
            plain_text (bytes): The bytes to be encrypted. Required.
            padding_method (PaddingMethod): An enumeration specifying the
                padding method to use with the asymmetric encryption
                algorithm. Required.
            hashing_algorithm (HashingAlgorithm): An enumeration specifying
                the hashing algorithm to use with the encryption padding
                method. Required, if the padding method is OAEP. Optional
                otherwise, defaults to None.

        Returns:
            dict: A dictionary containing the encrypted data, with at least
                the following key/value field:
                * cipher_text - the bytes of the encrypted data

        Raises:
            InvalidField: Raised when the algorithm is unsupported or the
                length is incompatible with the algorithm.
            CryptographicFailure: Raised when the key generation process
                fails.
Encrypt data using asymmetric encryption .