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 @abc.abstractmethod
85 def __copy__(self) -> DHPublicKey:
86 """
87 Returns a copy.
88 """
89
90 @abc.abstractmethod
91 def __deepcopy__(self, memo: dict) -> DHPublicKey:
92 """
93 Returns a deep copy.
94 """
95
96
97DHPublicKeyWithSerialization = DHPublicKey
98DHPublicKey.register(rust_openssl.dh.DHPublicKey)
99
100
101class DHPrivateKey(metaclass=abc.ABCMeta):
102 @property
103 @abc.abstractmethod
104 def key_size(self) -> int:
105 """
106 The bit length of the prime modulus.
107 """
108
109 @abc.abstractmethod
110 def public_key(self) -> DHPublicKey:
111 """
112 The DHPublicKey associated with this private key.
113 """
114
115 @abc.abstractmethod
116 def parameters(self) -> DHParameters:
117 """
118 The DHParameters object associated with this private key.
119 """
120
121 @abc.abstractmethod
122 def exchange(self, peer_public_key: DHPublicKey) -> bytes:
123 """
124 Given peer's DHPublicKey, carry out the key exchange and
125 return shared key as bytes.
126 """
127
128 @abc.abstractmethod
129 def private_numbers(self) -> DHPrivateNumbers:
130 """
131 Returns a DHPrivateNumbers.
132 """
133
134 @abc.abstractmethod
135 def private_bytes(
136 self,
137 encoding: _serialization.Encoding,
138 format: _serialization.PrivateFormat,
139 encryption_algorithm: _serialization.KeySerializationEncryption,
140 ) -> bytes:
141 """
142 Returns the key serialized as bytes.
143 """
144
145 @abc.abstractmethod
146 def __copy__(self) -> DHPrivateKey:
147 """
148 Returns a copy.
149 """
150
151 @abc.abstractmethod
152 def __deepcopy__(self, memo: dict) -> DHPrivateKey:
153 """
154 Returns a deep copy.
155 """
156
157
158DHPrivateKeyWithSerialization = DHPrivateKey
159DHPrivateKey.register(rust_openssl.dh.DHPrivateKey)