x25519.py 3.3 KB

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