x448.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 X448PublicKey(metaclass=abc.ABCMeta):
  11. @classmethod
  12. def from_public_bytes(cls, data: bytes) -> X448PublicKey:
  13. from cryptography.hazmat.backends.openssl.backend import backend
  14. if not backend.x448_supported():
  15. raise UnsupportedAlgorithm(
  16. "X448 is not supported by this version of OpenSSL.",
  17. _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM,
  18. )
  19. return rust_openssl.x448.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 __eq__(self, other: object) -> bool:
  37. """
  38. Checks equality.
  39. """
  40. @abc.abstractmethod
  41. def __copy__(self) -> X448PublicKey:
  42. """
  43. Returns a copy.
  44. """
  45. if hasattr(rust_openssl, "x448"):
  46. X448PublicKey.register(rust_openssl.x448.X448PublicKey)
  47. class X448PrivateKey(metaclass=abc.ABCMeta):
  48. @classmethod
  49. def generate(cls) -> X448PrivateKey:
  50. from cryptography.hazmat.backends.openssl.backend import backend
  51. if not backend.x448_supported():
  52. raise UnsupportedAlgorithm(
  53. "X448 is not supported by this version of OpenSSL.",
  54. _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM,
  55. )
  56. return rust_openssl.x448.generate_key()
  57. @classmethod
  58. def from_private_bytes(cls, data: Buffer) -> X448PrivateKey:
  59. from cryptography.hazmat.backends.openssl.backend import backend
  60. if not backend.x448_supported():
  61. raise UnsupportedAlgorithm(
  62. "X448 is not supported by this version of OpenSSL.",
  63. _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM,
  64. )
  65. return rust_openssl.x448.from_private_bytes(data)
  66. @abc.abstractmethod
  67. def public_key(self) -> X448PublicKey:
  68. """
  69. Returns the public key associated with this private key
  70. """
  71. @abc.abstractmethod
  72. def private_bytes(
  73. self,
  74. encoding: _serialization.Encoding,
  75. format: _serialization.PrivateFormat,
  76. encryption_algorithm: _serialization.KeySerializationEncryption,
  77. ) -> bytes:
  78. """
  79. The serialized bytes of the private key.
  80. """
  81. @abc.abstractmethod
  82. def private_bytes_raw(self) -> bytes:
  83. """
  84. The raw bytes of the private key.
  85. Equivalent to private_bytes(Raw, Raw, NoEncryption()).
  86. """
  87. @abc.abstractmethod
  88. def exchange(self, peer_public_key: X448PublicKey) -> bytes:
  89. """
  90. Performs a key exchange operation using the provided peer's public key.
  91. """
  92. @abc.abstractmethod
  93. def __copy__(self) -> X448PrivateKey:
  94. """
  95. Returns a copy.
  96. """
  97. if hasattr(rust_openssl, "x448"):
  98. X448PrivateKey.register(rust_openssl.x448.X448PrivateKey)