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

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

65 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 

8import typing 

9 

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

11from cryptography.hazmat.primitives import _serialization, hashes 

12from cryptography.hazmat.primitives.asymmetric import utils as asym_utils 

13from cryptography.utils import Buffer 

14 

15 

16class DSAParameters(metaclass=abc.ABCMeta): 

17 @abc.abstractmethod 

18 def generate_private_key(self) -> DSAPrivateKey: 

19 """ 

20 Generates and returns a DSAPrivateKey. 

21 """ 

22 

23 @abc.abstractmethod 

24 def parameter_numbers(self) -> DSAParameterNumbers: 

25 """ 

26 Returns a DSAParameterNumbers. 

27 """ 

28 

29 

30DSAParametersWithNumbers = DSAParameters 

31DSAParameters.register(rust_openssl.dsa.DSAParameters) 

32 

33 

34class DSAPrivateKey(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 

42 @abc.abstractmethod 

43 def public_key(self) -> DSAPublicKey: 

44 """ 

45 The DSAPublicKey associated with this private key. 

46 """ 

47 

48 @abc.abstractmethod 

49 def parameters(self) -> DSAParameters: 

50 """ 

51 The DSAParameters object associated with this private key. 

52 """ 

53 

54 @abc.abstractmethod 

55 def sign( 

56 self, 

57 data: Buffer, 

58 algorithm: asym_utils.Prehashed | hashes.HashAlgorithm, 

59 ) -> bytes: 

60 """ 

61 Signs the data 

62 """ 

63 

64 @abc.abstractmethod 

65 def private_numbers(self) -> DSAPrivateNumbers: 

66 """ 

67 Returns a DSAPrivateNumbers. 

68 """ 

69 

70 @abc.abstractmethod 

71 def private_bytes( 

72 self, 

73 encoding: _serialization.Encoding, 

74 format: _serialization.PrivateFormat, 

75 encryption_algorithm: _serialization.KeySerializationEncryption, 

76 ) -> bytes: 

77 """ 

78 Returns the key serialized as bytes. 

79 """ 

80 

81 @abc.abstractmethod 

82 def __copy__(self) -> DSAPrivateKey: 

83 """ 

84 Returns a copy. 

85 """ 

86 

87 @abc.abstractmethod 

88 def __deepcopy__(self, memo: dict) -> DSAPrivateKey: 

89 """ 

90 Returns a deep copy. 

91 """ 

92 

93 

94DSAPrivateKeyWithSerialization = DSAPrivateKey 

95DSAPrivateKey.register(rust_openssl.dsa.DSAPrivateKey) 

96 

97 

98class DSAPublicKey(metaclass=abc.ABCMeta): 

99 @property 

100 @abc.abstractmethod 

101 def key_size(self) -> int: 

102 """ 

103 The bit length of the prime modulus. 

104 """ 

105 

106 @abc.abstractmethod 

107 def parameters(self) -> DSAParameters: 

108 """ 

109 The DSAParameters object associated with this public key. 

110 """ 

111 

112 @abc.abstractmethod 

113 def public_numbers(self) -> DSAPublicNumbers: 

114 """ 

115 Returns a DSAPublicNumbers. 

116 """ 

117 

118 @abc.abstractmethod 

119 def public_bytes( 

120 self, 

121 encoding: _serialization.Encoding, 

122 format: _serialization.PublicFormat, 

123 ) -> bytes: 

124 """ 

125 Returns the key serialized as bytes. 

126 """ 

127 

128 @abc.abstractmethod 

129 def verify( 

130 self, 

131 signature: Buffer, 

132 data: Buffer, 

133 algorithm: asym_utils.Prehashed | hashes.HashAlgorithm, 

134 ) -> None: 

135 """ 

136 Verifies the signature of the data. 

137 """ 

138 

139 @abc.abstractmethod 

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

141 """ 

142 Checks equality. 

143 """ 

144 

145 @abc.abstractmethod 

146 def __copy__(self) -> DSAPublicKey: 

147 """ 

148 Returns a copy. 

149 """ 

150 

151 @abc.abstractmethod 

152 def __deepcopy__(self, memo: dict) -> DSAPublicKey: 

153 """ 

154 Returns a deep copy. 

155 """ 

156 

157 

158DSAPublicKeyWithSerialization = DSAPublicKey 

159DSAPublicKey.register(rust_openssl.dsa.DSAPublicKey) 

160 

161DSAPrivateNumbers = rust_openssl.dsa.DSAPrivateNumbers 

162DSAPublicNumbers = rust_openssl.dsa.DSAPublicNumbers 

163DSAParameterNumbers = rust_openssl.dsa.DSAParameterNumbers 

164 

165 

166def generate_parameters( 

167 key_size: int, backend: typing.Any = None 

168) -> DSAParameters: 

169 if key_size not in (1024, 2048, 3072, 4096): 

170 raise ValueError("Key size must be 1024, 2048, 3072, or 4096 bits.") 

171 

172 return rust_openssl.dsa.generate_parameters(key_size) 

173 

174 

175def generate_private_key( 

176 key_size: int, backend: typing.Any = None 

177) -> DSAPrivateKey: 

178 parameters = generate_parameters(key_size) 

179 return parameters.generate_private_key()