x448.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 X448PublicKey(metaclass=abc.ABCMeta):
  8. @classmethod
  9. def from_public_bytes(cls, data: bytes) -> "X448PublicKey":
  10. from cryptography.hazmat.backends.openssl.backend import backend
  11. if not backend.x448_supported():
  12. raise UnsupportedAlgorithm(
  13. "X448 is not supported by this version of OpenSSL.",
  14. _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM,
  15. )
  16. return backend.x448_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. class X448PrivateKey(metaclass=abc.ABCMeta):
  27. @classmethod
  28. def generate(cls) -> "X448PrivateKey":
  29. from cryptography.hazmat.backends.openssl.backend import backend
  30. if not backend.x448_supported():
  31. raise UnsupportedAlgorithm(
  32. "X448 is not supported by this version of OpenSSL.",
  33. _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM,
  34. )
  35. return backend.x448_generate_key()
  36. @classmethod
  37. def from_private_bytes(cls, data: bytes) -> "X448PrivateKey":
  38. from cryptography.hazmat.backends.openssl.backend import backend
  39. if not backend.x448_supported():
  40. raise UnsupportedAlgorithm(
  41. "X448 is not supported by this version of OpenSSL.",
  42. _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM,
  43. )
  44. return backend.x448_load_private_bytes(data)
  45. @abc.abstractmethod
  46. def public_key(self) -> X448PublicKey:
  47. """
  48. The serialized bytes of the public key.
  49. """
  50. @abc.abstractmethod
  51. def private_bytes(
  52. self,
  53. encoding: _serialization.Encoding,
  54. format: _serialization.PrivateFormat,
  55. encryption_algorithm: _serialization.KeySerializationEncryption,
  56. ) -> bytes:
  57. """
  58. The serialized bytes of the private key.
  59. """
  60. @abc.abstractmethod
  61. def exchange(self, peer_public_key: X448PublicKey) -> bytes:
  62. """
  63. Performs a key exchange operation using the provided peer's public key.
  64. """