Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/cryptography/hazmat/primitives/asymmetric/dh.py: 99%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

70 statements  

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 import utils 

10from cryptography.hazmat.bindings._rust import openssl as rust_openssl 

11from cryptography.hazmat.primitives import _serialization 

12 

13_FFDH_DEPRECATION_MSG = ( 

14 "Diffie-Hellman over finite fields (FFDH) is deprecated and support " 

15 "will be removed in a future release. Use a more modern key exchange " 

16 "algorithm." 

17) 

18 

19generate_parameters = rust_openssl.dh.generate_parameters 

20 

21 

22DHPrivateNumbers = rust_openssl.dh.DHPrivateNumbers 

23DHPublicNumbers = rust_openssl.dh.DHPublicNumbers 

24DHParameterNumbers = rust_openssl.dh.DHParameterNumbers 

25 

26 

27class DHParameters(metaclass=abc.ABCMeta): 

28 @abc.abstractmethod 

29 def generate_private_key(self) -> DHPrivateKey: 

30 """ 

31 Generates and returns a DHPrivateKey. 

32 """ 

33 

34 @abc.abstractmethod 

35 def parameter_bytes( 

36 self, 

37 encoding: _serialization.Encoding, 

38 format: _serialization.ParameterFormat, 

39 ) -> bytes: 

40 """ 

41 Returns the parameters serialized as bytes. 

42 """ 

43 

44 @abc.abstractmethod 

45 def parameter_numbers(self) -> DHParameterNumbers: 

46 """ 

47 Returns a DHParameterNumbers. 

48 """ 

49 

50 

51DHParametersWithSerialization = DHParameters 

52DHParameters.register(rust_openssl.dh.DHParameters) 

53 

54 

55class DHPublicKey(metaclass=abc.ABCMeta): 

56 @property 

57 @abc.abstractmethod 

58 def key_size(self) -> int: 

59 """ 

60 The bit length of the prime modulus. 

61 """ 

62 

63 @abc.abstractmethod 

64 def parameters(self) -> DHParameters: 

65 """ 

66 The DHParameters object associated with this public key. 

67 """ 

68 

69 @abc.abstractmethod 

70 def public_numbers(self) -> DHPublicNumbers: 

71 """ 

72 Returns a DHPublicNumbers. 

73 """ 

74 

75 @abc.abstractmethod 

76 def public_bytes( 

77 self, 

78 encoding: _serialization.Encoding, 

79 format: _serialization.PublicFormat, 

80 ) -> bytes: 

81 """ 

82 Returns the key serialized as bytes. 

83 """ 

84 

85 @abc.abstractmethod 

86 def __eq__(self, other: object) -> bool: 

87 """ 

88 Checks equality. 

89 """ 

90 

91 @abc.abstractmethod 

92 def __copy__(self) -> DHPublicKey: 

93 """ 

94 Returns a copy. 

95 """ 

96 

97 @abc.abstractmethod 

98 def __deepcopy__(self, memo: dict) -> DHPublicKey: 

99 """ 

100 Returns a deep copy. 

101 """ 

102 

103 

104DHPublicKeyWithSerialization = DHPublicKey 

105DHPublicKey.register(rust_openssl.dh.DHPublicKey) 

106 

107 

108class DHPrivateKey(metaclass=abc.ABCMeta): 

109 @property 

110 @abc.abstractmethod 

111 def key_size(self) -> int: 

112 """ 

113 The bit length of the prime modulus. 

114 """ 

115 

116 @abc.abstractmethod 

117 def public_key(self) -> DHPublicKey: 

118 """ 

119 The DHPublicKey associated with this private key. 

120 """ 

121 

122 @abc.abstractmethod 

123 def parameters(self) -> DHParameters: 

124 """ 

125 The DHParameters object associated with this private key. 

126 """ 

127 

128 @abc.abstractmethod 

129 def exchange(self, peer_public_key: DHPublicKey) -> bytes: 

130 """ 

131 Given peer's DHPublicKey, carry out the key exchange and 

132 return shared key as bytes. 

133 """ 

134 

135 @abc.abstractmethod 

136 def private_numbers(self) -> DHPrivateNumbers: 

137 """ 

138 Returns a DHPrivateNumbers. 

139 """ 

140 

141 @abc.abstractmethod 

142 def private_bytes( 

143 self, 

144 encoding: _serialization.Encoding, 

145 format: _serialization.PrivateFormat, 

146 encryption_algorithm: _serialization.KeySerializationEncryption, 

147 ) -> bytes: 

148 """ 

149 Returns the key serialized as bytes. 

150 """ 

151 

152 @abc.abstractmethod 

153 def __copy__(self) -> DHPrivateKey: 

154 """ 

155 Returns a copy. 

156 """ 

157 

158 @abc.abstractmethod 

159 def __deepcopy__(self, memo: dict) -> DHPrivateKey: 

160 """ 

161 Returns a deep copy. 

162 """ 

163 

164 

165DHPrivateKeyWithSerialization = DHPrivateKey 

166DHPrivateKey.register(rust_openssl.dh.DHPrivateKey) 

167 

168# Aliases that do not emit the deprecation warning on attribute access, for 

169# internal use (e.g. the unions in 

170# cryptography.hazmat.primitives.asymmetric.types, which are evaluated at 

171# import time). 

172_DHPublicKey = DHPublicKey 

173_DHPrivateKey = DHPrivateKey 

174 

175utils.deprecated( 

176 generate_parameters, 

177 __name__, 

178 _FFDH_DEPRECATION_MSG, 

179 utils.DeprecatedIn50, 

180 name="generate_parameters", 

181) 

182 

183utils.deprecated( 

184 DHPrivateNumbers, 

185 __name__, 

186 _FFDH_DEPRECATION_MSG, 

187 utils.DeprecatedIn50, 

188 name="DHPrivateNumbers", 

189) 

190 

191utils.deprecated( 

192 DHPublicNumbers, 

193 __name__, 

194 _FFDH_DEPRECATION_MSG, 

195 utils.DeprecatedIn50, 

196 name="DHPublicNumbers", 

197) 

198 

199utils.deprecated( 

200 DHParameterNumbers, 

201 __name__, 

202 _FFDH_DEPRECATION_MSG, 

203 utils.DeprecatedIn50, 

204 name="DHParameterNumbers", 

205) 

206 

207utils.deprecated( 

208 DHParameters, 

209 __name__, 

210 _FFDH_DEPRECATION_MSG, 

211 utils.DeprecatedIn50, 

212 name="DHParameters", 

213) 

214 

215utils.deprecated( 

216 DHParameters, 

217 __name__, 

218 _FFDH_DEPRECATION_MSG, 

219 utils.DeprecatedIn50, 

220 name="DHParametersWithSerialization", 

221) 

222 

223utils.deprecated( 

224 DHPublicKey, 

225 __name__, 

226 _FFDH_DEPRECATION_MSG, 

227 utils.DeprecatedIn50, 

228 name="DHPublicKey", 

229) 

230 

231utils.deprecated( 

232 DHPublicKey, 

233 __name__, 

234 _FFDH_DEPRECATION_MSG, 

235 utils.DeprecatedIn50, 

236 name="DHPublicKeyWithSerialization", 

237) 

238 

239utils.deprecated( 

240 DHPrivateKey, 

241 __name__, 

242 _FFDH_DEPRECATION_MSG, 

243 utils.DeprecatedIn50, 

244 name="DHPrivateKey", 

245) 

246 

247utils.deprecated( 

248 DHPrivateKey, 

249 __name__, 

250 _FFDH_DEPRECATION_MSG, 

251 utils.DeprecatedIn50, 

252 name="DHPrivateKeyWithSerialization", 

253)