|
| 1 | +.. hazmat:: |
| 2 | + |
| 3 | +Ed25519 signing |
| 4 | +=============== |
| 5 | + |
| 6 | +.. currentmodule:: cryptography.hazmat.primitives.asymmetric.ed25519 |
| 7 | + |
| 8 | + |
| 9 | +Ed25519 is an elliptic curve signing algorithm using `EdDSA`_ and |
| 10 | +`Curve25519`_. If you do not have legacy interoperability concerns then you |
| 11 | +should strongly consider using this signature algorithm. |
| 12 | + |
| 13 | + |
| 14 | +Signing & 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 | + |
| 26 | +Key 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 | + or |
| 78 | + :attr:`~cryptography.hazmat.primitives.serialization.PrivateFormat.Raw` |
| 79 | + ) are chosen to define the exact serialization. |
| 80 | + |
| 81 | + :param encoding: A value from the |
| 82 | + :class:`~cryptography.hazmat.primitives.serialization.Encoding` enum. |
| 83 | + |
| 84 | + :param format: A value from the |
| 85 | + :class:`~cryptography.hazmat.primitives.serialization.PrivateFormat` |
| 86 | + enum. If the ``encoding`` is |
| 87 | + :attr:`~cryptography.hazmat.primitives.serialization.Encoding.Raw` |
| 88 | + then ``format`` must be |
| 89 | + :attr:`~cryptography.hazmat.primitives.serialization.PrivateFormat.Raw` |
| 90 | + , otherwise it must be |
| 91 | + :attr:`~cryptography.hazmat.primitives.serialization.PrivateFormat.PKCS8`. |
| 92 | + |
| 93 | + :param encryption_algorithm: An instance of an object conforming to the |
| 94 | + :class:`~cryptography.hazmat.primitives.serialization.KeySerializationEncryption` |
| 95 | + interface. |
| 96 | + |
| 97 | + :return bytes: Serialized key. |
| 98 | + |
| 99 | +.. class:: Ed25519PublicKey |
| 100 | + |
| 101 | + .. versionadded:: 2.6 |
| 102 | + |
| 103 | + .. classmethod:: from_public_bytes(data) |
| 104 | + |
| 105 | + :param bytes data: 32 byte public key. |
| 106 | + |
| 107 | + :returns: :class:`Ed25519PublicKey` |
| 108 | + |
| 109 | + .. doctest:: |
| 110 | + |
| 111 | + >>> from cryptography.hazmat.primitives import serialization |
| 112 | + >>> from cryptography.hazmat.primitives.asymmetric import ed25519 |
| 113 | + >>> private_key = ed25519.Ed25519PrivateKey.generate() |
| 114 | + >>> public_key = private_key.public_key() |
| 115 | + >>> public_bytes = public_key.public_bytes( |
| 116 | + ... encoding=serialization.Encoding.Raw, |
| 117 | + ... format=serialization.PublicFormat.Raw |
| 118 | + ... ) |
| 119 | + >>> loaded_public_key = ed25519.Ed25519PublicKey.from_public_bytes(public_bytes) |
| 120 | + |
| 121 | + .. method:: public_bytes(encoding, format) |
| 122 | + |
| 123 | + Allows serialization of the key to bytes. Encoding ( |
| 124 | + :attr:`~cryptography.hazmat.primitives.serialization.Encoding.PEM`, |
| 125 | + :attr:`~cryptography.hazmat.primitives.serialization.Encoding.DER`, or |
| 126 | + :attr:`~cryptography.hazmat.primitives.serialization.Encoding.Raw`) and |
| 127 | + format ( |
| 128 | + :attr:`~cryptography.hazmat.primitives.serialization.PublicFormat.SubjectPublicKeyInfo` |
| 129 | + or |
| 130 | + :attr:`~cryptography.hazmat.primitives.serialization.PublicFormat.Raw` |
| 131 | + ) are chosen to define the exact serialization. |
| 132 | + |
| 133 | + :param encoding: A value from the |
| 134 | + :class:`~cryptography.hazmat.primitives.serialization.Encoding` enum. |
| 135 | + |
| 136 | + :param format: A value from the |
| 137 | + :class:`~cryptography.hazmat.primitives.serialization.PublicFormat` |
| 138 | + enum. If the ``encoding`` is |
| 139 | + :attr:`~cryptography.hazmat.primitives.serialization.Encoding.Raw` |
| 140 | + then ``format`` must be |
| 141 | + :attr:`~cryptography.hazmat.primitives.serialization.PublicFormat.Raw` |
| 142 | + , otherwise it must be |
| 143 | + :attr:`~cryptography.hazmat.primitives.serialization.PublicFormat.SubjectPublicKeyInfo`. |
| 144 | + |
| 145 | + :returns bytes: The public key bytes. |
| 146 | + |
| 147 | + .. method:: verify(signature, data) |
| 148 | + |
| 149 | + :param bytes signature: The signature to verify. |
| 150 | + |
| 151 | + :param bytes data: The data to verify. |
| 152 | + |
| 153 | + :raises cryptography.exceptions.InvalidSignature: Raised when the |
| 154 | + signature cannot be verified. |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | +.. _`EdDSA`: https://en.wikipedia.org/wiki/EdDSA |
| 159 | +.. _`Curve25519`: https://en.wikipedia.org/wiki/Curve25519 |
0 commit comments