x448.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 absolute_import, division, print_function
  5. import abc
  6. import six
  7. from cryptography.exceptions import UnsupportedAlgorithm, _Reasons
  8. @six.add_metaclass(abc.ABCMeta)
  9. class X448PublicKey(object):
  10. @classmethod
  11. def from_public_bytes(cls, data):
  12. from cryptography.hazmat.backends.openssl.backend import backend
  13. if not backend.x448_supported():
  14. raise UnsupportedAlgorithm(
  15. "X448 is not supported by this version of OpenSSL.",
  16. _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM,
  17. )
  18. return backend.x448_load_public_bytes(data)
  19. @abc.abstractmethod
  20. def public_bytes(self, encoding, format):
  21. """
  22. The serialized bytes of the public key.
  23. """
  24. @six.add_metaclass(abc.ABCMeta)
  25. class X448PrivateKey(object):
  26. @classmethod
  27. def generate(cls):
  28. from cryptography.hazmat.backends.openssl.backend import backend
  29. if not backend.x448_supported():
  30. raise UnsupportedAlgorithm(
  31. "X448 is not supported by this version of OpenSSL.",
  32. _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM,
  33. )
  34. return backend.x448_generate_key()
  35. @classmethod
  36. def from_private_bytes(cls, data):
  37. from cryptography.hazmat.backends.openssl.backend import backend
  38. if not backend.x448_supported():
  39. raise UnsupportedAlgorithm(
  40. "X448 is not supported by this version of OpenSSL.",
  41. _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM,
  42. )
  43. return backend.x448_load_private_bytes(data)
  44. @abc.abstractmethod
  45. def public_key(self):
  46. """
  47. The serialized bytes of the public key.
  48. """
  49. @abc.abstractmethod
  50. def private_bytes(self, encoding, format, encryption_algorithm):
  51. """
  52. The serialized bytes of the private key.
  53. """
  54. @abc.abstractmethod
  55. def exchange(self, peer_public_key):
  56. """
  57. Performs a key exchange operation using the provided peer's public key.
  58. """