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
« 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.
5from __future__ import annotations
7import abc
8import typing
10from cryptography.hazmat.primitives import hashes
11from cryptography.hazmat.primitives._asymmetric import (
12 AsymmetricPadding as AsymmetricPadding,
13)
14from cryptography.hazmat.primitives.asymmetric import rsa
17class PKCS1v15(AsymmetricPadding):
18 name = "EMSA-PKCS1-v1_5"
21class _MaxLength:
22 "Sentinel value for `MAX_LENGTH`."
25class _Auto:
26 "Sentinel value for `AUTO`."
29class _DigestLength:
30 "Sentinel value for `DIGEST_LENGTH`."
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]
40 def __init__(
41 self,
42 mgf: MGF,
43 salt_length: typing.Union[int, _MaxLength, _Auto, _DigestLength],
44 ) -> None:
45 self._mgf = mgf
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 )
55 if isinstance(salt_length, int) and salt_length < 0:
56 raise ValueError("salt_length must be zero or greater.")
58 self._salt_length = salt_length
61class OAEP(AsymmetricPadding):
62 name = "EME-OAEP"
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.")
73 self._mgf = mgf
74 self._algorithm = algorithm
75 self._label = label
78class MGF(metaclass=abc.ABCMeta):
79 _algorithm: hashes.HashAlgorithm
82class MGF1(MGF):
83 MAX_LENGTH = _MaxLength()
85 def __init__(self, algorithm: hashes.HashAlgorithm):
86 if not isinstance(algorithm, hashes.HashAlgorithm):
87 raise TypeError("Expected instance of hashes.HashAlgorithm.")
89 self._algorithm = algorithm
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