Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/cryptography/hazmat/primitives/asymmetric/padding.py: 57%

47 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-08 06:05 +0000

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.primitives import hashes 

11from cryptography.hazmat.primitives._asymmetric import ( 

12 AsymmetricPadding as AsymmetricPadding, 

13) 

14from cryptography.hazmat.primitives.asymmetric import rsa 

15 

16 

17class PKCS1v15(AsymmetricPadding): 

18 name = "EMSA-PKCS1-v1_5" 

19 

20 

21class _MaxLength: 

22 "Sentinel value for `MAX_LENGTH`." 

23 

24 

25class _Auto: 

26 "Sentinel value for `AUTO`." 

27 

28 

29class _DigestLength: 

30 "Sentinel value for `DIGEST_LENGTH`." 

31 

32 

33class PSS(AsymmetricPadding): 

34 MAX_LENGTH = _MaxLength() 

35 AUTO = _Auto() 

36 DIGEST_LENGTH = _DigestLength() 

37 name = "EMSA-PSS" 

38 _salt_length: typing.Union[int, _MaxLength, _Auto, _DigestLength] 

39 

40 def __init__( 

41 self, 

42 mgf: MGF, 

43 salt_length: typing.Union[int, _MaxLength, _Auto, _DigestLength], 

44 ) -> None: 

45 self._mgf = mgf 

46 

47 if not isinstance( 

48 salt_length, (int, _MaxLength, _Auto, _DigestLength) 

49 ): 

50 raise TypeError( 

51 "salt_length must be an integer, MAX_LENGTH, " 

52 "DIGEST_LENGTH, or AUTO" 

53 ) 

54 

55 if isinstance(salt_length, int) and salt_length < 0: 

56 raise ValueError("salt_length must be zero or greater.") 

57 

58 self._salt_length = salt_length 

59 

60 

61class OAEP(AsymmetricPadding): 

62 name = "EME-OAEP" 

63 

64 def __init__( 

65 self, 

66 mgf: MGF, 

67 algorithm: hashes.HashAlgorithm, 

68 label: typing.Optional[bytes], 

69 ): 

70 if not isinstance(algorithm, hashes.HashAlgorithm): 

71 raise TypeError("Expected instance of hashes.HashAlgorithm.") 

72 

73 self._mgf = mgf 

74 self._algorithm = algorithm 

75 self._label = label 

76 

77 

78class MGF(metaclass=abc.ABCMeta): 

79 _algorithm: hashes.HashAlgorithm 

80 

81 

82class MGF1(MGF): 

83 MAX_LENGTH = _MaxLength() 

84 

85 def __init__(self, algorithm: hashes.HashAlgorithm): 

86 if not isinstance(algorithm, hashes.HashAlgorithm): 

87 raise TypeError("Expected instance of hashes.HashAlgorithm.") 

88 

89 self._algorithm = algorithm 

90 

91 

92def calculate_max_pss_salt_length( 

93 key: typing.Union[rsa.RSAPrivateKey, rsa.RSAPublicKey], 

94 hash_algorithm: hashes.HashAlgorithm, 

95) -> int: 

96 if not isinstance(key, (rsa.RSAPrivateKey, rsa.RSAPublicKey)): 

97 raise TypeError("key must be an RSA public or private key") 

98 # bit length - 1 per RFC 3447 

99 emlen = (key.key_size + 6) // 8 

100 salt_length = emlen - hash_algorithm.digest_size - 2 

101 assert salt_length >= 0 

102 return salt_length