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