x25519.py 3.5 KB

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