Skip to content

Commit d525783

Browse files
committed
add missing properties to padding classes
1 parent f84ec6c commit d525783

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

CHANGELOG.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ Changelog
2323
instances of classes in
2424
:mod:`~cryptography.hazmat.primitives.asymmetric.padding`
2525
comparable.
26+
* Added `salt_length` property to
27+
:class:`~cryptography.hazmat.primitives.asymmetric.padding.PSS`.
28+
* Added `label` property to
29+
:class:`~cryptography.hazmat.primitives.asymmetric.padding.OAEP`.
30+
* Added `algorithm` property to
31+
:class:`~cryptography.hazmat.primitives.asymmetric.padding.MGF1`.
32+
2633

2734
.. _v45-0-6:
2835

docs/hazmat/primitives/asymmetric/rsa.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,14 @@ Padding
325325

326326
The padding's mask generation function (MGF).
327327

328+
.. attribute:: salt_length
329+
330+
:type: int
331+
332+
.. versionadded:: 46.0.0
333+
334+
The length of the salt.
335+
328336
.. class:: OAEP(mgf, algorithm, label)
329337

330338
.. versionadded:: 0.4
@@ -351,6 +359,14 @@ Padding
351359

352360
The padding's hash algorithm.
353361

362+
.. attribute:: label
363+
364+
:type: bytes | None
365+
366+
.. versionadded:: 42.0.0
367+
368+
The padding's hash algorithm.
369+
354370
.. attribute:: mgf
355371

356372
:type: :class:`~cryptography.hazmat.primitives.asymmetric.padding.MGF`
@@ -411,6 +427,14 @@ Mask generation functions
411427
:param algorithm: An instance of
412428
:class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`.
413429

430+
.. attribute:: algorithm
431+
432+
:type: :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`.
433+
434+
.. versionadded:: 46.0.0
435+
436+
The algorithm of this instance.
437+
414438
Numbers
415439
~~~~~~~
416440

src/cryptography/hazmat/primitives/asymmetric/padding.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ def __eq__(self, other: typing.Any) -> bool:
7676
def mgf(self) -> MGF:
7777
return self._mgf
7878

79+
@property
80+
def salt_length(self) -> int | _MaxLength | _Auto | _DigestLength:
81+
return self._salt_length
82+
7983

8084
class OAEP(AsymmetricPadding):
8185
name = "EME-OAEP"
@@ -105,6 +109,10 @@ def __eq__(self, other: typing.Any) -> bool:
105109
def algorithm(self) -> hashes.HashAlgorithm:
106110
return self._algorithm
107111

112+
@property
113+
def label(self) -> bytes | None:
114+
return self._label
115+
108116
@property
109117
def mgf(self) -> MGF:
110118
return self._mgf
@@ -131,6 +139,10 @@ def __init__(self, algorithm: hashes.HashAlgorithm):
131139
def __eq__(self, other: typing.Any) -> bool:
132140
return isinstance(other, MGF1) and self._algorithm == other._algorithm
133141

142+
@property
143+
def algorithm(self) -> hashes.HashAlgorithm:
144+
return self._algorithm
145+
134146

135147
def calculate_max_pss_salt_length(
136148
key: rsa.RSAPrivateKey | rsa.RSAPublicKey,

tests/hazmat/primitives/test_rsa.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,6 +1656,13 @@ def test_mgf_property(self):
16561656
assert pss.mgf == mgf
16571657
assert pss.mgf == pss._mgf
16581658

1659+
def test_salt_length_property(self):
1660+
algorithm = hashes.SHA256()
1661+
mgf = padding.MGF1(algorithm)
1662+
pss = padding.PSS(mgf=mgf, salt_length=padding.PSS.MAX_LENGTH)
1663+
assert pss.salt_length == padding.PSS.MAX_LENGTH
1664+
assert pss._salt_length == padding.PSS.MAX_LENGTH
1665+
16591666
@pytest.mark.parametrize("xof", [hashes.SHA256(), hashes.SHA512()])
16601667
@pytest.mark.parametrize(
16611668
"salt_length",
@@ -1727,6 +1734,11 @@ def test_valid_mgf1_parameters(self):
17271734
mgf = padding.MGF1(algorithm)
17281735
assert mgf._algorithm == algorithm
17291736

1737+
def test_algorithm_property(self):
1738+
mgf = padding.MGF1(hashes.SHA256())
1739+
assert mgf.algorithm == hashes.SHA256()
1740+
assert mgf._algorithm == hashes.SHA256()
1741+
17301742

17311743
class TestOAEP:
17321744
def test_invalid_algorithm(self):
@@ -1745,6 +1757,13 @@ def test_algorithm_property(self):
17451757
assert oaep.algorithm == algorithm
17461758
assert oaep.algorithm == oaep._algorithm
17471759

1760+
def test_label_property(self):
1761+
algorithm = hashes.SHA256()
1762+
mgf = padding.MGF1(algorithm)
1763+
oaep = padding.OAEP(mgf=mgf, algorithm=algorithm, label=None)
1764+
assert oaep.label is None
1765+
assert oaep._label is None
1766+
17481767
def test_mgf_property(self):
17491768
algorithm = hashes.SHA256()
17501769
mgf = padding.MGF1(algorithm)

0 commit comments

Comments
 (0)