1.. hazmat::
2
3Asymmetric Utilities
4====================
5
6.. currentmodule:: cryptography.hazmat.primitives.asymmetric.utils
7
8
9.. function:: decode_dss_signature(signature)
10
11    Takes in signatures generated by the DSA/ECDSA signers and returns a
12    tuple ``(r, s)``. These signatures are ASN.1 encoded ``Dss-Sig-Value``
13    sequences (as defined in :rfc:`3279`)
14
15    :param bytes signature: The signature to decode.
16
17    :returns: The decoded tuple ``(r, s)``.
18
19    :raises ValueError: Raised if the signature is malformed.
20
21.. function:: encode_dss_signature(r, s)
22
23    Creates an ASN.1 encoded ``Dss-Sig-Value`` (as defined in :rfc:`3279`) from
24    raw ``r`` and ``s`` values.
25
26    :param int r: The raw signature value ``r``.
27
28    :param int s: The raw signature value ``s``.
29
30    :return bytes: The encoded signature.
31
32.. class:: Prehashed(algorithm)
33
34    .. versionadded:: 1.6
35
36    ``Prehashed`` can be passed as the ``algorithm`` in the RSA
37    :meth:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey.sign`
38    and
39    :meth:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPublicKey.verify`
40    as well as DSA
41    :meth:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAPrivateKey.sign`
42    and
43    :meth:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAPublicKey.verify`
44    methods.
45
46    For elliptic curves it can be passed as the ``algorithm`` in
47    :class:`~cryptography.hazmat.primitives.asymmetric.ec.ECDSA` and then used
48    with
49    :meth:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey.sign`
50    and
51    :meth:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey.verify`
52    .
53
54    :param algorithm: An instance of
55        :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`.
56
57    .. doctest::
58
59        >>> import hashlib
60        >>> from cryptography.hazmat.backends import default_backend
61        >>> from cryptography.hazmat.primitives import hashes
62        >>> from cryptography.hazmat.primitives.asymmetric import (
63        ...    padding, rsa, utils
64        ... )
65        >>> private_key = rsa.generate_private_key(
66        ...     public_exponent=65537,
67        ...     key_size=2048,
68        ...     backend=default_backend()
69        ... )
70        >>> prehashed_msg = hashlib.sha256(b"A message I want to sign").digest()
71        >>> signature = private_key.sign(
72        ...     prehashed_msg,
73        ...     padding.PSS(
74        ...         mgf=padding.MGF1(hashes.SHA256()),
75        ...         salt_length=padding.PSS.MAX_LENGTH
76        ...     ),
77        ...     utils.Prehashed(hashes.SHA256())
78        ... )
79        >>> public_key = private_key.public_key()
80        >>> public_key.verify(
81        ...     signature,
82        ...     prehashed_msg,
83        ...     padding.PSS(
84        ...         mgf=padding.MGF1(hashes.SHA256()),
85        ...         salt_length=padding.PSS.MAX_LENGTH
86        ...     ),
87        ...     utils.Prehashed(hashes.SHA256())
88        ... )
89