ed25519.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # This file is dual licensed under the terms of the Apache License, Version
  2. # 2.0, and the BSD License. See the LICENSE file in the root of this repository
  3. # for complete details.
  4. from __future__ import annotations
  5. import abc
  6. from cryptography.exceptions import UnsupportedAlgorithm, _Reasons
  7. from cryptography.hazmat.bindings._rust import openssl as rust_openssl
  8. from cryptography.hazmat.primitives import _serialization
  9. class Ed25519PublicKey(metaclass=abc.ABCMeta):
  10. @classmethod
  11. def from_public_bytes(cls, data: bytes) -> Ed25519PublicKey:
  12. from cryptography.hazmat.backends.openssl.backend import backend
  13. if not backend.ed25519_supported():
  14. raise UnsupportedAlgorithm(
  15. "ed25519 is not supported by this version of OpenSSL.",
  16. _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
  17. )
  18. return rust_openssl.ed25519.from_public_bytes(data)
  19. @abc.abstractmethod
  20. def public_bytes(
  21. self,
  22. encoding: _serialization.Encoding,
  23. format: _serialization.PublicFormat,
  24. ) -> bytes:
  25. """
  26. The serialized bytes of the public key.
  27. """
  28. @abc.abstractmethod
  29. def public_bytes_raw(self) -> bytes:
  30. """
  31. The raw bytes of the public key.
  32. Equivalent to public_bytes(Raw, Raw).
  33. """
  34. @abc.abstractmethod
  35. def verify(self, signature: bytes, data: bytes) -> None:
  36. """
  37. Verify the signature.
  38. """
  39. @abc.abstractmethod
  40. def __eq__(self, other: object) -> bool:
  41. """
  42. Checks equality.
  43. """
  44. Ed25519PublicKey.register(rust_openssl.ed25519.Ed25519PublicKey)
  45. class Ed25519PrivateKey(metaclass=abc.ABCMeta):
  46. @classmethod
  47. def generate(cls) -> Ed25519PrivateKey:
  48. from cryptography.hazmat.backends.openssl.backend import backend
  49. if not backend.ed25519_supported():
  50. raise UnsupportedAlgorithm(
  51. "ed25519 is not supported by this version of OpenSSL.",
  52. _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
  53. )
  54. return rust_openssl.ed25519.generate_key()
  55. @classmethod
  56. def from_private_bytes(cls, data: bytes) -> Ed25519PrivateKey:
  57. from cryptography.hazmat.backends.openssl.backend import backend
  58. if not backend.ed25519_supported():
  59. raise UnsupportedAlgorithm(
  60. "ed25519 is not supported by this version of OpenSSL.",
  61. _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
  62. )
  63. return rust_openssl.ed25519.from_private_bytes(data)
  64. @abc.abstractmethod
  65. def public_key(self) -> Ed25519PublicKey:
  66. """
  67. The Ed25519PublicKey derived from the private key.
  68. """
  69. @abc.abstractmethod
  70. def private_bytes(
  71. self,
  72. encoding: _serialization.Encoding,
  73. format: _serialization.PrivateFormat,
  74. encryption_algorithm: _serialization.KeySerializationEncryption,
  75. ) -> bytes:
  76. """
  77. The serialized bytes of the private key.
  78. """
  79. @abc.abstractmethod
  80. def private_bytes_raw(self) -> bytes:
  81. """
  82. The raw bytes of the private key.
  83. Equivalent to private_bytes(Raw, Raw, NoEncryption()).
  84. """
  85. @abc.abstractmethod
  86. def sign(self, data: bytes) -> bytes:
  87. """
  88. Signs the data.
  89. """
  90. Ed25519PrivateKey.register(rust_openssl.ed25519.Ed25519PrivateKey)