kmip/services/server/crypto/engine.py
def _decrypt_asymmetric(
            self,
            decryption_algorithm,
            decryption_key,
            cipher_text,
            padding_method,
            hashing_algorithm=None):
        """
        Encrypt data using asymmetric decryption.

        Args:
            decryption_algorithm (CryptographicAlgorithm): An enumeration
                specifying the asymmetric decryption algorithm to use for
                decryption. Required.
            decryption_key (bytes): The bytes of the private key to use for
                decryption. Required.
            cipher_text (bytes): The bytes to be decrypted. Required.
            padding_method (PaddingMethod): An enumeration specifying the
                padding method to use with the asymmetric decryption
                algorithm. Required.
            hashing_algorithm (HashingAlgorithm): An enumeration specifying
                the hashing algorithm to use with the decryption padding
                method. Required, if the padding method is OAEP. Optional
                otherwise, defaults to None.

        Returns:
            dict: A dictionary containing the decrypted data, with at least
                the following key/value field:
                * plain_text - the bytes of the decrypted 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 decryption_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 decryption.".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 "
                    "decryption.".format(padding_method)
                )

            backend = default_backend()

            try:
                private_key = backend.load_der_private_key(
                    decryption_key,
                    None
                )
            except Exception:
                try:
                    private_key = backend.load_pem_private_key(
                        decryption_key,
                        None
                    )
                except Exception:
                    raise exceptions.CryptographicFailure(
                        "The private key bytes could not be loaded."
                    )
            plain_text = private_key.decrypt(
                cipher_text,
                padding_method
            )
            return plain_text
        else:
            raise exceptions.InvalidField(
                "The cryptographic algorithm '{0}' is not supported for "
                "asymmetric decryption.".format(decryption_algorithm)
            )
def _decrypt_asymmetric(
            self,
            decryption_algorithm,
            decryption_key,
            cipher_text,
            padding_method,
            hashing_algorithm=None):
        if decryption_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 decryption.".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 "
                    "decryption.".format(padding_method)
                )

            backend = default_backend()

            try:
                private_key = backend.load_der_private_key(
                    decryption_key,
                    None
                )
            except Exception:
                try:
                    private_key = backend.load_pem_private_key(
                        decryption_key,
                        None
                    )
                except Exception:
                    raise exceptions.CryptographicFailure(
                        "The private key bytes could not be loaded."
                    )
            plain_text = private_key.decrypt(
                cipher_text,
                padding_method
            )
            return plain_text
        else:
            raise exceptions.InvalidField(
                "The cryptographic algorithm '{0}' is not supported for "
                "asymmetric decryption.".format(decryption_algorithm)
            )
def _decrypt_asymmetric ( self , decryption_algorithm , decryption_key , cipher_text , padding_method , hashing_algorithm = None ) : if decryption_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 decryption." . 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 " "decryption." . format ( padding_method ) ) backend = default_backend ( ) try : private_key = backend . load_der_private_key ( decryption_key , None ) except Exception : try : private_key = backend . load_pem_private_key ( decryption_key , None ) except Exception : raise exceptions . CryptographicFailure ( "The private key bytes could not be loaded." ) plain_text = private_key . decrypt ( cipher_text , padding_method ) return plain_text else : raise exceptions . InvalidField ( "The cryptographic algorithm '{0}' is not supported for " "asymmetric decryption." . format ( decryption_algorithm ) )
Encrypt data using asymmetric decryption.

        Args:
            decryption_algorithm (CryptographicAlgorithm): An enumeration
                specifying the asymmetric decryption algorithm to use for
                decryption. Required.
            decryption_key (bytes): The bytes of the private key to use for
                decryption. Required.
            cipher_text (bytes): The bytes to be decrypted. Required.
            padding_method (PaddingMethod): An enumeration specifying the
                padding method to use with the asymmetric decryption
                algorithm. Required.
            hashing_algorithm (HashingAlgorithm): An enumeration specifying
                the hashing algorithm to use with the decryption padding
                method. Required, if the padding method is OAEP. Optional
                otherwise, defaults to None.

        Returns:
            dict: A dictionary containing the decrypted data, with at least
                the following key/value field:
                * plain_text - the bytes of the decrypted 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 decryption .