ed448.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. if hasattr(rust_openssl, "ed448"):
  51. Ed448PublicKey.register(rust_openssl.ed448.Ed448PublicKey)
  52. class Ed448PrivateKey(metaclass=abc.ABCMeta):
  53. @classmethod
  54. def generate(cls) -> Ed448PrivateKey:
  55. from cryptography.hazmat.backends.openssl.backend import backend
  56. if not backend.ed448_supported():
  57. raise UnsupportedAlgorithm(
  58. "ed448 is not supported by this version of OpenSSL.",
  59. _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
  60. )
  61. return rust_openssl.ed448.generate_key()
  62. @classmethod
  63. def from_private_bytes(cls, data: Buffer) -> Ed448PrivateKey:
  64. from cryptography.hazmat.backends.openssl.backend import backend
  65. if not backend.ed448_supported():
  66. raise UnsupportedAlgorithm(
  67. "ed448 is not supported by this version of OpenSSL.",
  68. _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
  69. )
  70. return rust_openssl.ed448.from_private_bytes(data)
  71. @abc.abstractmethod
  72. def public_key(self) -> Ed448PublicKey:
  73. """
  74. The Ed448PublicKey derived from the private key.
  75. """
  76. @abc.abstractmethod
  77. def sign(self, data: Buffer) -> bytes:
  78. """
  79. Signs the data.
  80. """
  81. @abc.abstractmethod
  82. def private_bytes(
  83. self,
  84. encoding: _serialization.Encoding,
  85. format: _serialization.PrivateFormat,
  86. encryption_algorithm: _serialization.KeySerializationEncryption,
  87. ) -> bytes:
  88. """
  89. The serialized bytes of the private key.
  90. """
  91. @abc.abstractmethod
  92. def private_bytes_raw(self) -> bytes:
  93. """
  94. The raw bytes of the private key.
  95. Equivalent to private_bytes(Raw, Raw, NoEncryption()).
  96. """
  97. @abc.abstractmethod
  98. def __copy__(self) -> Ed448PrivateKey:
  99. """
  100. Returns a copy.
  101. """
  102. if hasattr(rust_openssl, "x448"):
  103. Ed448PrivateKey.register(rust_openssl.ed448.Ed448PrivateKey)