ed448.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. from cryptography.utils import Buffer
  10. class Ed448PublicKey(metaclass=abc.ABCMeta):
  11. @classmethod
  12. def from_public_bytes(cls, data: bytes) -> Ed448PublicKey:
  13. from cryptography.hazmat.backends.openssl.backend import backend
  14. if not backend.ed448_supported():
  15. raise UnsupportedAlgorithm(
  16. "ed448 is not supported by this version of OpenSSL.",
  17. _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
  18. )
  19. return rust_openssl.ed448.from_public_bytes(data)
  20. @abc.abstractmethod
  21. def public_bytes(
  22. self,
  23. encoding: _serialization.Encoding,
  24. format: _serialization.PublicFormat,
  25. ) -> bytes:
  26. """
  27. The serialized bytes of the public key.
  28. """
  29. @abc.abstractmethod
  30. def public_bytes_raw(self) -> bytes:
  31. """
  32. The raw bytes of the public key.
  33. Equivalent to public_bytes(Raw, Raw).
  34. """
  35. @abc.abstractmethod
  36. def verify(self, signature: Buffer, data: Buffer) -> None:
  37. """
  38. Verify the signature.
  39. """
  40. @abc.abstractmethod
  41. def __eq__(self, other: object) -> bool:
  42. """
  43. Checks equality.
  44. """
  45. @abc.abstractmethod
  46. def __copy__(self) -> Ed448PublicKey:
  47. """
  48. Returns a copy.
  49. """
  50. @abc.abstractmethod
  51. def __deepcopy__(self, memo: dict) -> Ed448PublicKey:
  52. """
  53. Returns a deep copy.
  54. """
  55. if hasattr(rust_openssl, "ed448"):
  56. Ed448PublicKey.register(rust_openssl.ed448.Ed448PublicKey)
  57. class Ed448PrivateKey(metaclass=abc.ABCMeta):
  58. @classmethod
  59. def generate(cls) -> Ed448PrivateKey:
  60. from cryptography.hazmat.backends.openssl.backend import backend
  61. if not backend.ed448_supported():
  62. raise UnsupportedAlgorithm(
  63. "ed448 is not supported by this version of OpenSSL.",
  64. _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
  65. )
  66. return rust_openssl.ed448.generate_key()
  67. @classmethod
  68. def from_private_bytes(cls, data: Buffer) -> Ed448PrivateKey:
  69. from cryptography.hazmat.backends.openssl.backend import backend
  70. if not backend.ed448_supported():
  71. raise UnsupportedAlgorithm(
  72. "ed448 is not supported by this version of OpenSSL.",
  73. _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
  74. )
  75. return rust_openssl.ed448.from_private_bytes(data)
  76. @abc.abstractmethod
  77. def public_key(self) -> Ed448PublicKey:
  78. """
  79. The Ed448PublicKey derived from the private key.
  80. """
  81. @abc.abstractmethod
  82. def sign(self, data: Buffer) -> bytes:
  83. """
  84. Signs the data.
  85. """
  86. @abc.abstractmethod
  87. def private_bytes(
  88. self,
  89. encoding: _serialization.Encoding,
  90. format: _serialization.PrivateFormat,
  91. encryption_algorithm: _serialization.KeySerializationEncryption,
  92. ) -> bytes:
  93. """
  94. The serialized bytes of the private key.
  95. """
  96. @abc.abstractmethod
  97. def private_bytes_raw(self) -> bytes:
  98. """
  99. The raw bytes of the private key.
  100. Equivalent to private_bytes(Raw, Raw, NoEncryption()).
  101. """
  102. @abc.abstractmethod
  103. def __copy__(self) -> Ed448PrivateKey:
  104. """
  105. Returns a copy.
  106. """
  107. @abc.abstractmethod
  108. def __deepcopy__(self, memo: dict) -> Ed448PrivateKey:
  109. """
  110. Returns a deep copy.
  111. """
  112. if hasattr(rust_openssl, "x448"):
  113. Ed448PrivateKey.register(rust_openssl.ed448.Ed448PrivateKey)