dh.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.hazmat.bindings._rust import openssl as rust_openssl
  7. from cryptography.hazmat.primitives import _serialization
  8. generate_parameters = rust_openssl.dh.generate_parameters
  9. DHPrivateNumbers = rust_openssl.dh.DHPrivateNumbers
  10. DHPublicNumbers = rust_openssl.dh.DHPublicNumbers
  11. DHParameterNumbers = rust_openssl.dh.DHParameterNumbers
  12. class DHParameters(metaclass=abc.ABCMeta):
  13. @abc.abstractmethod
  14. def generate_private_key(self) -> DHPrivateKey:
  15. """
  16. Generates and returns a DHPrivateKey.
  17. """
  18. @abc.abstractmethod
  19. def parameter_bytes(
  20. self,
  21. encoding: _serialization.Encoding,
  22. format: _serialization.ParameterFormat,
  23. ) -> bytes:
  24. """
  25. Returns the parameters serialized as bytes.
  26. """
  27. @abc.abstractmethod
  28. def parameter_numbers(self) -> DHParameterNumbers:
  29. """
  30. Returns a DHParameterNumbers.
  31. """
  32. DHParametersWithSerialization = DHParameters
  33. DHParameters.register(rust_openssl.dh.DHParameters)
  34. class DHPublicKey(metaclass=abc.ABCMeta):
  35. @property
  36. @abc.abstractmethod
  37. def key_size(self) -> int:
  38. """
  39. The bit length of the prime modulus.
  40. """
  41. @abc.abstractmethod
  42. def parameters(self) -> DHParameters:
  43. """
  44. The DHParameters object associated with this public key.
  45. """
  46. @abc.abstractmethod
  47. def public_numbers(self) -> DHPublicNumbers:
  48. """
  49. Returns a DHPublicNumbers.
  50. """
  51. @abc.abstractmethod
  52. def public_bytes(
  53. self,
  54. encoding: _serialization.Encoding,
  55. format: _serialization.PublicFormat,
  56. ) -> bytes:
  57. """
  58. Returns the key serialized as bytes.
  59. """
  60. @abc.abstractmethod
  61. def __eq__(self, other: object) -> bool:
  62. """
  63. Checks equality.
  64. """
  65. DHPublicKeyWithSerialization = DHPublicKey
  66. DHPublicKey.register(rust_openssl.dh.DHPublicKey)
  67. class DHPrivateKey(metaclass=abc.ABCMeta):
  68. @property
  69. @abc.abstractmethod
  70. def key_size(self) -> int:
  71. """
  72. The bit length of the prime modulus.
  73. """
  74. @abc.abstractmethod
  75. def public_key(self) -> DHPublicKey:
  76. """
  77. The DHPublicKey associated with this private key.
  78. """
  79. @abc.abstractmethod
  80. def parameters(self) -> DHParameters:
  81. """
  82. The DHParameters object associated with this private key.
  83. """
  84. @abc.abstractmethod
  85. def exchange(self, peer_public_key: DHPublicKey) -> bytes:
  86. """
  87. Given peer's DHPublicKey, carry out the key exchange and
  88. return shared key as bytes.
  89. """
  90. @abc.abstractmethod
  91. def private_numbers(self) -> DHPrivateNumbers:
  92. """
  93. Returns a DHPrivateNumbers.
  94. """
  95. @abc.abstractmethod
  96. def private_bytes(
  97. self,
  98. encoding: _serialization.Encoding,
  99. format: _serialization.PrivateFormat,
  100. encryption_algorithm: _serialization.KeySerializationEncryption,
  101. ) -> bytes:
  102. """
  103. Returns the key serialized as bytes.
  104. """
  105. DHPrivateKeyWithSerialization = DHPrivateKey
  106. DHPrivateKey.register(rust_openssl.dh.DHPrivateKey)