ed448.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. import abc
  5. from cryptography.exceptions import UnsupportedAlgorithm, _Reasons
  6. from cryptography.hazmat.primitives import _serialization
  7. class Ed448PublicKey(metaclass=abc.ABCMeta):
  8. @classmethod
  9. def from_public_bytes(cls, data: bytes) -> "Ed448PublicKey":
  10. from cryptography.hazmat.backends.openssl.backend import backend
  11. if not backend.ed448_supported():
  12. raise UnsupportedAlgorithm(
  13. "ed448 is not supported by this version of OpenSSL.",
  14. _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
  15. )
  16. return backend.ed448_load_public_bytes(data)
  17. @abc.abstractmethod
  18. def public_bytes(
  19. self,
  20. encoding: _serialization.Encoding,
  21. format: _serialization.PublicFormat,
  22. ) -> bytes:
  23. """
  24. The serialized bytes of the public key.
  25. """
  26. @abc.abstractmethod
  27. def verify(self, signature: bytes, data: bytes) -> None:
  28. """
  29. Verify the signature.
  30. """
  31. class Ed448PrivateKey(metaclass=abc.ABCMeta):
  32. @classmethod
  33. def generate(cls) -> "Ed448PrivateKey":
  34. from cryptography.hazmat.backends.openssl.backend import backend
  35. if not backend.ed448_supported():
  36. raise UnsupportedAlgorithm(
  37. "ed448 is not supported by this version of OpenSSL.",
  38. _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
  39. )
  40. return backend.ed448_generate_key()
  41. @classmethod
  42. def from_private_bytes(cls, data: bytes) -> "Ed448PrivateKey":
  43. from cryptography.hazmat.backends.openssl.backend import backend
  44. if not backend.ed448_supported():
  45. raise UnsupportedAlgorithm(
  46. "ed448 is not supported by this version of OpenSSL.",
  47. _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
  48. )
  49. return backend.ed448_load_private_bytes(data)
  50. @abc.abstractmethod
  51. def public_key(self) -> Ed448PublicKey:
  52. """
  53. The Ed448PublicKey derived from the private key.
  54. """
  55. @abc.abstractmethod
  56. def sign(self, data: bytes) -> bytes:
  57. """
  58. Signs the data.
  59. """
  60. @abc.abstractmethod
  61. def private_bytes(
  62. self,
  63. encoding: _serialization.Encoding,
  64. format: _serialization.PrivateFormat,
  65. encryption_algorithm: _serialization.KeySerializationEncryption,
  66. ) -> bytes:
  67. """
  68. The serialized bytes of the private key.
  69. """