Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/cryptography/hazmat/primitives/asymmetric/padding.py: 57%
46 statements
« prev ^ index » next coverage.py v7.0.1, created at 2022-12-25 06:11 +0000
« prev ^ index » next coverage.py v7.0.1, created at 2022-12-25 06:11 +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.
6import abc
7import typing
9from cryptography.hazmat.primitives import hashes
10from cryptography.hazmat.primitives._asymmetric import (
11 AsymmetricPadding as AsymmetricPadding,
12)
13from cryptography.hazmat.primitives.asymmetric import rsa
16class PKCS1v15(AsymmetricPadding):
17 name = "EMSA-PKCS1-v1_5"
20class _MaxLength:
21 "Sentinel value for `MAX_LENGTH`."
24class _Auto:
25 "Sentinel value for `AUTO`."
28class _DigestLength:
29 "Sentinel value for `DIGEST_LENGTH`."
32class PSS(AsymmetricPadding):
33 MAX_LENGTH = _MaxLength()
34 AUTO = _Auto()
35 DIGEST_LENGTH = _DigestLength()
36 name = "EMSA-PSS"
37 _salt_length: typing.Union[int, _MaxLength, _Auto, _DigestLength]
39 def __init__(
40 self,
41 mgf: "MGF",
42 salt_length: typing.Union[int, _MaxLength, _Auto, _DigestLength],
43 ) -> None:
44 self._mgf = mgf
46 if not isinstance(
47 salt_length, (int, _MaxLength, _Auto, _DigestLength)
48 ):
49 raise TypeError(
50 "salt_length must be an integer, MAX_LENGTH, "
51 "DIGEST_LENGTH, or AUTO"
52 )
54 if isinstance(salt_length, int) and salt_length < 0:
55 raise ValueError("salt_length must be zero or greater.")
57 self._salt_length = salt_length
60class OAEP(AsymmetricPadding):
61 name = "EME-OAEP"
63 def __init__(
64 self,
65 mgf: "MGF",
66 algorithm: hashes.HashAlgorithm,
67 label: typing.Optional[bytes],
68 ):
69 if not isinstance(algorithm, hashes.HashAlgorithm):
70 raise TypeError("Expected instance of hashes.HashAlgorithm.")
72 self._mgf = mgf
73 self._algorithm = algorithm
74 self._label = label
77class MGF(metaclass=abc.ABCMeta):
78 _algorithm: hashes.HashAlgorithm
81class MGF1(MGF):
82 MAX_LENGTH = _MaxLength()
84 def __init__(self, algorithm: hashes.HashAlgorithm):
85 if not isinstance(algorithm, hashes.HashAlgorithm):
86 raise TypeError("Expected instance of hashes.HashAlgorithm.")
88 self._algorithm = algorithm
91def calculate_max_pss_salt_length(
92 key: typing.Union["rsa.RSAPrivateKey", "rsa.RSAPublicKey"],
93 hash_algorithm: hashes.HashAlgorithm,
94) -> int:
95 if not isinstance(key, (rsa.RSAPrivateKey, rsa.RSAPublicKey)):
96 raise TypeError("key must be an RSA public or private key")
97 # bit length - 1 per RFC 3447
98 emlen = (key.key_size + 6) // 8
99 salt_length = emlen - hash_algorithm.digest_size - 2
100 assert salt_length >= 0
101 return salt_length