x448.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. class X448PublicKey(metaclass=abc.ABCMeta):
  10. @classmethod
  11. def from_public_bytes(cls, data: bytes) -> X448PublicKey:
  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 rust_openssl.x448.from_public_bytes(data)
  19. @abc.abstractmethod
  20. def public_bytes(
  21. self,
  22. encoding: _serialization.Encoding,
  23. format: _serialization.PublicFormat,
  24. ) -> bytes:
  25. """
  26. The serialized bytes of the public key.
  27. """
  28. @abc.abstractmethod
  29. def public_bytes_raw(self) -> bytes:
  30. """
  31. The raw bytes of the public key.
  32. Equivalent to public_bytes(Raw, Raw).
  33. """
  34. @abc.abstractmethod
  35. def __eq__(self, other: object) -> bool:
  36. """
  37. Checks equality.
  38. """
  39. if hasattr(rust_openssl, "x448"):
  40. X448PublicKey.register(rust_openssl.x448.X448PublicKey)
  41. class X448PrivateKey(metaclass=abc.ABCMeta):
  42. @classmethod
  43. def generate(cls) -> X448PrivateKey:
  44. from cryptography.hazmat.backends.openssl.backend import backend
  45. if not backend.x448_supported():
  46. raise UnsupportedAlgorithm(
  47. "X448 is not supported by this version of OpenSSL.",
  48. _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM,
  49. )
  50. return rust_openssl.x448.generate_key()
  51. @classmethod
  52. def from_private_bytes(cls, data: bytes) -> X448PrivateKey:
  53. from cryptography.hazmat.backends.openssl.backend import backend
  54. if not backend.x448_supported():
  55. raise UnsupportedAlgorithm(
  56. "X448 is not supported by this version of OpenSSL.",
  57. _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM,
  58. )
  59. return rust_openssl.x448.from_private_bytes(data)
  60. @abc.abstractmethod
  61. def public_key(self) -> X448PublicKey:
  62. """
  63. Returns the public key associated with this private key
  64. """
  65. @abc.abstractmethod
  66. def private_bytes(
  67. self,
  68. encoding: _serialization.Encoding,
  69. format: _serialization.PrivateFormat,
  70. encryption_algorithm: _serialization.KeySerializationEncryption,
  71. ) -> bytes:
  72. """
  73. The serialized bytes of the private key.
  74. """
  75. @abc.abstractmethod
  76. def private_bytes_raw(self) -> bytes:
  77. """
  78. The raw bytes of the private key.
  79. Equivalent to private_bytes(Raw, Raw, NoEncryption()).
  80. """
  81. @abc.abstractmethod
  82. def exchange(self, peer_public_key: X448PublicKey) -> bytes:
  83. """
  84. Performs a key exchange operation using the provided peer's public key.
  85. """
  86. if hasattr(rust_openssl, "x448"):
  87. X448PrivateKey.register(rust_openssl.x448.X448PrivateKey)