kmip/services/server/crypto/engine.py
def sign(self,
             digital_signature_algorithm,
             crypto_alg,
             hash_algorithm,
             padding,
             signing_key,
             data):
        """
        Args:
            digital_signature_algorithm (DigitalSignatureAlgorithm): An
                enumeration specifying the asymmetric cryptographic algorithm
                and hashing algorithm to use for the signature operation. Can
                be None if cryptographic_algorithm and hash_algorithm are set.
            crypto_alg (CryptographicAlgorithm): An enumeration
                specifying the asymmetric cryptographic algorithm to use for
                the signature operation. Can be None if
                digital_signature_algorithm is set.
            hash_algorithm (HashingAlgorithm): An enumeration specifying the
                hash algorithm to use for the signature operation. Can be None
                if digital_signature_algorithm is set.
            padding (PaddingMethod): An enumeration specifying the asymmetric
                padding method to use for the signature operation.
            signing_key (bytes): The bytes of the private key to use for the
                signature operation.
            data (bytes): The data to be signed.

        Returns:
            signature (bytes): the bytes of the signature data

        Raises:
            CryptographicFailure: Raised when an error occurs during signature
                creation.
            InvalidField: Raised when an unsupported hashing or cryptographic
                algorithm is specified.
        """

        if digital_signature_algorithm:
            (hash_alg, crypto_alg) = self._digital_signature_algorithms.get(
                                         digital_signature_algorithm,
                                         (None, None)
            )

        elif crypto_alg and hash_algorithm:
            hash_alg = self._encryption_hash_algorithms.get(
                hash_algorithm, None
            )
        else:
            raise exceptions.InvalidField(
                'For signing, either a digital signature algorithm or a hash'
                ' algorithm and a cryptographic algorithm must be specified.'
            )

        if crypto_alg == enums.CryptographicAlgorithm.RSA:
            try:
                key = self._create_RSA_private_key(signing_key)
            except Exception:
                raise exceptions.InvalidField('Unable to deserialize key '
                                              'bytes, unknown format.')
        else:
            raise exceptions.InvalidField(
                'For signing, an RSA key must be used.'
            )

        if padding:
            padding_method = self._asymmetric_padding_methods.get(
                padding, None
            )
        else:
            raise exceptions.InvalidField(
                'For signing, a padding method must be specified.'
            )

        if padding == enums.PaddingMethod.PSS:
            signature = key.sign(
                data,
                asymmetric_padding.PSS(
                    mgf=asymmetric_padding.MGF1(hash_alg()),
                    salt_length=asymmetric_padding.PSS.MAX_LENGTH
                ),
                hash_alg()
            )
        elif padding == enums.PaddingMethod.PKCS1v15:
            signature = key.sign(
                data,
                padding_method(),
                hash_alg()
            )
        else:
            raise exceptions.InvalidField(
                "Padding method '{0}' is not a supported signature "
                "padding method.".format(padding)
            )
        return signature
def sign(self,
             digital_signature_algorithm,
             crypto_alg,
             hash_algorithm,
             padding,
             signing_key,
             data):
        """
        Args:
            digital_signature_algorithm (DigitalSignatureAlgorithm): An
                enumeration specifying the asymmetric cryptographic algorithm
                and hashing algorithm to use for the signature operation. Can
                be None if cryptographic_algorithm and hash_algorithm are set.
            crypto_alg (CryptographicAlgorithm): An enumeration
                specifying the asymmetric cryptographic algorithm to use for
                the signature operation. Can be None if
                digital_signature_algorithm is set.
            hash_algorithm (HashingAlgorithm): An enumeration specifying the
                hash algorithm to use for the signature operation. Can be None
                if digital_signature_algorithm is set.
            padding (PaddingMethod): An enumeration specifying the asymmetric
                padding method to use for the signature operation.
            signing_key (bytes): The bytes of the private key to use for the
                signature operation.
            data (bytes): The data to be signed.

        Returns:
            signature (bytes): the bytes of the signature data

        Raises:
            CryptographicFailure: Raised when an error occurs during signature
                creation.
            InvalidField: Raised when an unsupported hashing or cryptographic
                algorithm is specified.
        """

        if digital_signature_algorithm:
            (hash_alg, crypto_alg) = self._digital_signature_algorithms.get(
                                         digital_signature_algorithm,
                                         (None, None)
            )

        elif crypto_alg and hash_algorithm:
            hash_alg = self._encryption_hash_algorithms.get(
                hash_algorithm, None
            )
        else:
            raise exceptions.InvalidField(
                'For signing, either a digital signature algorithm or a hash'
                ' algorithm and a cryptographic algorithm must be specified.'
            )

        if crypto_alg == enums.CryptographicAlgorithm.RSA:
            try:
                key = self._create_RSA_private_key(signing_key)
            except Exception:
                raise exceptions.InvalidField('Unable to deserialize key '
                                              'bytes, unknown format.')
        else:
            raise exceptions.InvalidField(
                'For signing, an RSA key must be used.'
            )

        if padding:
            padding_method = self._asymmetric_padding_methods.get(
                padding, None
            )
        else:
            raise exceptions.InvalidField(
                'For signing, a padding method must be specified.'
            )

        if padding == enums.PaddingMethod.PSS:
            signature = key.sign(
                data,
                asymmetric_padding.PSS(
                    mgf=asymmetric_padding.MGF1(hash_alg()),
                    salt_length=asymmetric_padding.PSS.MAX_LENGTH
                ),
                hash_alg()
            )
        elif padding == enums.PaddingMethod.PKCS1v15:
            signature = key.sign(
                data,
                padding_method(),
                hash_alg()
            )
        else:
            raise exceptions.InvalidField(
                "Padding method '{0}' is not a supported signature "
                "padding method.".format(padding)
            )
        return signature
Args:
            digital_signature_algorithm (DigitalSignatureAlgorithm): An
                enumeration specifying the asymmetric cryptographic algorithm
                and hashing algorithm to use for the signature operation. Can
                be None if cryptographic_algorithm and hash_algorithm are set.
            crypto_alg (CryptographicAlgorithm): An enumeration
                specifying the asymmetric cryptographic algorithm to use for
                the signature operation. Can be None if
                digital_signature_algorithm is set.
            hash_algorithm (HashingAlgorithm): An enumeration specifying the
                hash algorithm to use for the signature operation. Can be None
                if digital_signature_algorithm is set.
            padding (PaddingMethod): An enumeration specifying the asymmetric
                padding method to use for the signature operation.
            signing_key (bytes): The bytes of the private key to use for the
                signature operation.
            data (bytes): The data to be signed.

        Returns:
            signature (bytes): the bytes of the signature data

        Raises:
            CryptographicFailure: Raised when an error occurs during signature
                creation.
            InvalidField: Raised when an unsupported hashing or cryptographic
                algorithm is specified.