Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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 

5 

6import typing 

7 

8from cryptography.hazmat.primitives import hashes 

9from cryptography.hazmat.primitives._asymmetric import AsymmetricPadding 

10from cryptography.hazmat.primitives.asymmetric import rsa 

11 

12 

13class PKCS1v15(AsymmetricPadding): 

14 name = "EMSA-PKCS1-v1_5" 

15 

16 

17class PSS(AsymmetricPadding): 

18 MAX_LENGTH = object() 

19 name = "EMSA-PSS" 

20 

21 def __init__(self, mgf, salt_length): 

22 self._mgf = mgf 

23 

24 if ( 

25 not isinstance(salt_length, int) 

26 and salt_length is not self.MAX_LENGTH 

27 ): 

28 raise TypeError("salt_length must be an integer.") 

29 

30 if salt_length is not self.MAX_LENGTH and salt_length < 0: 

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

32 

33 self._salt_length = salt_length 

34 

35 

36class OAEP(AsymmetricPadding): 

37 name = "EME-OAEP" 

38 

39 def __init__( 

40 self, 

41 mgf: "MGF1", 

42 algorithm: hashes.HashAlgorithm, 

43 label: typing.Optional[bytes], 

44 ): 

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

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

47 

48 self._mgf = mgf 

49 self._algorithm = algorithm 

50 self._label = label 

51 

52 

53class MGF1(object): 

54 MAX_LENGTH = object() 

55 

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

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

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

59 

60 self._algorithm = algorithm 

61 

62 

63def calculate_max_pss_salt_length( 

64 key: typing.Union["rsa.RSAPrivateKey", "rsa.RSAPublicKey"], 

65 hash_algorithm: hashes.HashAlgorithm, 

66) -> int: 

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

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

69 # bit length - 1 per RFC 3447 

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

71 salt_length = emlen - hash_algorithm.digest_size - 2 

72 assert salt_length >= 0 

73 return salt_length