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

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

52 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.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 

91DHPublicKeyWithSerialization = DHPublicKey 

92DHPublicKey.register(rust_openssl.dh.DHPublicKey) 

93 

94 

95class DHPrivateKey(metaclass=abc.ABCMeta): 

96 @property 

97 @abc.abstractmethod 

98 def key_size(self) -> int: 

99 """ 

100 The bit length of the prime modulus. 

101 """ 

102 

103 @abc.abstractmethod 

104 def public_key(self) -> DHPublicKey: 

105 """ 

106 The DHPublicKey associated with this private key. 

107 """ 

108 

109 @abc.abstractmethod 

110 def parameters(self) -> DHParameters: 

111 """ 

112 The DHParameters object associated with this private key. 

113 """ 

114 

115 @abc.abstractmethod 

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

117 """ 

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

119 return shared key as bytes. 

120 """ 

121 

122 @abc.abstractmethod 

123 def private_numbers(self) -> DHPrivateNumbers: 

124 """ 

125 Returns a DHPrivateNumbers. 

126 """ 

127 

128 @abc.abstractmethod 

129 def private_bytes( 

130 self, 

131 encoding: _serialization.Encoding, 

132 format: _serialization.PrivateFormat, 

133 encryption_algorithm: _serialization.KeySerializationEncryption, 

134 ) -> bytes: 

135 """ 

136 Returns the key serialized as bytes. 

137 """ 

138 

139 @abc.abstractmethod 

140 def __copy__(self) -> DHPrivateKey: 

141 """ 

142 Returns a copy. 

143 """ 

144 

145 

146DHPrivateKeyWithSerialization = DHPrivateKey 

147DHPrivateKey.register(rust_openssl.dh.DHPrivateKey)