• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. hazmat::
2
3Ed25519 signing
4===============
5
6.. currentmodule:: cryptography.hazmat.primitives.asymmetric.ed25519
7
8
9Ed25519 is an elliptic curve signing algorithm using `EdDSA`_ and
10`Curve25519`_. If you do not have legacy interoperability concerns then you
11should strongly consider using this signature algorithm.
12
13
14Signing & Verification
15~~~~~~~~~~~~~~~~~~~~~~
16
17.. doctest::
18
19    >>> from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
20    >>> private_key = Ed25519PrivateKey.generate()
21    >>> signature = private_key.sign(b"my authenticated message")
22    >>> public_key = private_key.public_key()
23    >>> # Raises InvalidSignature if verification fails
24    >>> public_key.verify(signature, b"my authenticated message")
25
26Key interfaces
27~~~~~~~~~~~~~~
28
29.. class:: Ed25519PrivateKey
30
31    .. versionadded:: 2.6
32
33    .. classmethod:: generate()
34
35        Generate an Ed25519 private key.
36
37        :returns: :class:`Ed25519PrivateKey`
38
39    .. classmethod:: from_private_bytes(data)
40
41        :param data: 32 byte private key.
42        :type data: :term:`bytes-like`
43
44        :returns: :class:`Ed25519PrivateKey`
45
46        .. doctest::
47
48            >>> from cryptography.hazmat.primitives import serialization
49            >>> from cryptography.hazmat.primitives.asymmetric import ed25519
50            >>> private_key = ed25519.Ed25519PrivateKey.generate()
51            >>> private_bytes = private_key.private_bytes(
52            ...     encoding=serialization.Encoding.Raw,
53            ...     format=serialization.PrivateFormat.Raw,
54            ...     encryption_algorithm=serialization.NoEncryption()
55            ... )
56            >>> loaded_private_key = ed25519.Ed25519PrivateKey.from_private_bytes(private_bytes)
57
58
59    .. method:: public_key()
60
61        :returns: :class:`Ed25519PublicKey`
62
63    .. method:: sign(data)
64
65        :param bytes data: The data to sign.
66
67        :returns bytes: The 64 byte signature.
68
69    .. method:: private_bytes(encoding, format, encryption_algorithm)
70
71        Allows serialization of the key to bytes. Encoding (
72        :attr:`~cryptography.hazmat.primitives.serialization.Encoding.PEM`,
73        :attr:`~cryptography.hazmat.primitives.serialization.Encoding.DER`, or
74        :attr:`~cryptography.hazmat.primitives.serialization.Encoding.Raw`) and
75        format (
76        :attr:`~cryptography.hazmat.primitives.serialization.PrivateFormat.PKCS8`,
77        :attr:`~cryptography.hazmat.primitives.serialization.PrivateFormat.OpenSSH`
78        or
79        :attr:`~cryptography.hazmat.primitives.serialization.PrivateFormat.Raw`
80        ) are chosen to define the exact serialization.
81
82        :param encoding: A value from the
83            :class:`~cryptography.hazmat.primitives.serialization.Encoding` enum.
84
85        :param format: A value from the
86            :class:`~cryptography.hazmat.primitives.serialization.PrivateFormat`
87            enum. If the ``encoding`` is
88            :attr:`~cryptography.hazmat.primitives.serialization.Encoding.Raw`
89            then ``format`` must be
90            :attr:`~cryptography.hazmat.primitives.serialization.PrivateFormat.Raw`
91            , otherwise it must be
92            :attr:`~cryptography.hazmat.primitives.serialization.PrivateFormat.PKCS8` or
93            :attr:`~cryptography.hazmat.primitives.serialization.PrivateFormat.OpenSSH`.
94
95        :param encryption_algorithm: An instance of an object conforming to the
96            :class:`~cryptography.hazmat.primitives.serialization.KeySerializationEncryption`
97            interface.
98
99        :return bytes: Serialized key.
100
101.. class:: Ed25519PublicKey
102
103    .. versionadded:: 2.6
104
105    .. classmethod:: from_public_bytes(data)
106
107        :param bytes data: 32 byte public key.
108
109        :returns: :class:`Ed25519PublicKey`
110
111        .. doctest::
112
113            >>> from cryptography.hazmat.primitives import serialization
114            >>> from cryptography.hazmat.primitives.asymmetric import ed25519
115            >>> private_key = ed25519.Ed25519PrivateKey.generate()
116            >>> public_key = private_key.public_key()
117            >>> public_bytes = public_key.public_bytes(
118            ...     encoding=serialization.Encoding.Raw,
119            ...     format=serialization.PublicFormat.Raw
120            ... )
121            >>> loaded_public_key = ed25519.Ed25519PublicKey.from_public_bytes(public_bytes)
122
123    .. method:: public_bytes(encoding, format)
124
125        Allows serialization of the key to bytes. Encoding (
126        :attr:`~cryptography.hazmat.primitives.serialization.Encoding.PEM`,
127        :attr:`~cryptography.hazmat.primitives.serialization.Encoding.DER`,
128        :attr:`~cryptography.hazmat.primitives.serialization.Encoding.OpenSSH`,
129        or
130        :attr:`~cryptography.hazmat.primitives.serialization.Encoding.Raw`) and
131        format (
132        :attr:`~cryptography.hazmat.primitives.serialization.PublicFormat.SubjectPublicKeyInfo`,
133        :attr:`~cryptography.hazmat.primitives.serialization.PublicFormat.OpenSSH`
134        , or
135        :attr:`~cryptography.hazmat.primitives.serialization.PublicFormat.Raw`
136        ) are chosen to define the exact serialization.
137
138        :param encoding: A value from the
139            :class:`~cryptography.hazmat.primitives.serialization.Encoding` enum.
140
141        :param format: A value from the
142            :class:`~cryptography.hazmat.primitives.serialization.PublicFormat`
143            enum. If the ``encoding`` is
144            :attr:`~cryptography.hazmat.primitives.serialization.Encoding.Raw`
145            then ``format`` must be
146            :attr:`~cryptography.hazmat.primitives.serialization.PublicFormat.Raw`.
147            If ``encoding`` is
148            :attr:`~cryptography.hazmat.primitives.serialization.Encoding.OpenSSH`
149            then ``format`` must be
150            :attr:`~cryptography.hazmat.primitives.serialization.PublicFormat.OpenSSH`.
151            In all other cases ``format`` must be
152            :attr:`~cryptography.hazmat.primitives.serialization.PublicFormat.SubjectPublicKeyInfo`.
153
154        :returns bytes: The public key bytes.
155
156    .. method:: verify(signature, data)
157
158        :param bytes signature: The signature to verify.
159
160        :param bytes data: The data to verify.
161
162        :raises cryptography.exceptions.InvalidSignature: Raised when the
163            signature cannot be verified.
164
165
166
167.. _`EdDSA`: https://en.wikipedia.org/wiki/EdDSA
168.. _`Curve25519`: https://en.wikipedia.org/wiki/Curve25519
169