|
7 | 7 | import copy
|
8 | 8 | import itertools
|
9 | 9 | import os
|
| 10 | +import typing |
10 | 11 |
|
11 | 12 | import pytest
|
12 | 13 |
|
@@ -70,6 +71,9 @@ class DummyMGF(padding.MGF):
|
70 | 71 | _salt_length = 0
|
71 | 72 | _algorithm = hashes.SHA256()
|
72 | 73 |
|
| 74 | + def __eq__(self, other: typing.Any) -> bool: |
| 75 | + return isinstance(other, DummyMGF) |
| 76 | + |
73 | 77 |
|
74 | 78 | def _check_fips_key_length(backend, private_key):
|
75 | 79 | if (
|
@@ -1603,6 +1607,14 @@ class TestRSAPKCS1Verification:
|
1603 | 1607 | )
|
1604 | 1608 |
|
1605 | 1609 |
|
| 1610 | +class TestPKCS1v15: |
| 1611 | + def test_eq(self): |
| 1612 | + assert padding.PKCS1v15() == padding.PKCS1v15() |
| 1613 | + assert padding.PKCS1v15() != padding.PSS( |
| 1614 | + mgf=padding.MGF1(hashes.SHA256()), salt_length=32 |
| 1615 | + ) |
| 1616 | + |
| 1617 | + |
1606 | 1618 | class TestPSS:
|
1607 | 1619 | def test_calculate_max_pss_salt_length(self):
|
1608 | 1620 | with pytest.raises(TypeError):
|
@@ -1644,8 +1656,68 @@ def test_mgf_property(self):
|
1644 | 1656 | assert pss.mgf == mgf
|
1645 | 1657 | assert pss.mgf == pss._mgf
|
1646 | 1658 |
|
| 1659 | + @pytest.mark.parametrize("xof", [hashes.SHA256(), hashes.SHA512()]) |
| 1660 | + @pytest.mark.parametrize( |
| 1661 | + "salt_length", |
| 1662 | + [ |
| 1663 | + 1, |
| 1664 | + 32, |
| 1665 | + padding.PSS.MAX_LENGTH, |
| 1666 | + padding.PSS.AUTO, |
| 1667 | + padding.PSS.DIGEST_LENGTH, |
| 1668 | + ], |
| 1669 | + ) |
| 1670 | + def test_eq( |
| 1671 | + self, xof: hashes.HashAlgorithm, salt_length: typing.Any |
| 1672 | + ) -> None: |
| 1673 | + assert padding.PSS( |
| 1674 | + salt_length=salt_length, mgf=padding.MGF1(algorithm=xof) |
| 1675 | + ) == padding.PSS( |
| 1676 | + salt_length=salt_length, mgf=padding.MGF1(algorithm=xof) |
| 1677 | + ) |
| 1678 | + |
| 1679 | + @pytest.mark.parametrize( |
| 1680 | + "salt_length", |
| 1681 | + [ |
| 1682 | + 1, |
| 1683 | + 32, |
| 1684 | + padding.PSS.MAX_LENGTH, |
| 1685 | + padding.PSS.AUTO, |
| 1686 | + padding.PSS.DIGEST_LENGTH, |
| 1687 | + ], |
| 1688 | + ) |
| 1689 | + def test_not_eq_with_different_salt_length( |
| 1690 | + self, salt_length: typing.Any |
| 1691 | + ) -> None: |
| 1692 | + xof = hashes.SHA256() |
| 1693 | + assert padding.PSS( |
| 1694 | + salt_length=salt_length, mgf=padding.MGF1(algorithm=xof) |
| 1695 | + ) != padding.PSS(salt_length=64, mgf=padding.MGF1(algorithm=xof)) |
| 1696 | + |
| 1697 | + def test_not_eq_with_salt_length_object_identity(self) -> None: |
| 1698 | + xof = hashes.SHA256() |
| 1699 | + assert padding.PSS( |
| 1700 | + salt_length=padding.PSS.AUTO, mgf=padding.MGF1(algorithm=xof) |
| 1701 | + ) != padding.PSS( |
| 1702 | + salt_length=padding.PSS.DIGEST_LENGTH, |
| 1703 | + mgf=padding.MGF1(algorithm=xof), |
| 1704 | + ) |
| 1705 | + |
| 1706 | + def test_not_eq_with_different_mgf(self) -> None: |
| 1707 | + assert padding.PSS( |
| 1708 | + salt_length=padding.PSS.AUTO, |
| 1709 | + mgf=padding.MGF1(algorithm=hashes.SHA256()), |
| 1710 | + ) != padding.PSS( |
| 1711 | + salt_length=padding.PSS.AUTO, |
| 1712 | + mgf=padding.MGF1(algorithm=hashes.SHA512()), |
| 1713 | + ) |
| 1714 | + |
1647 | 1715 |
|
1648 | 1716 | class TestMGF1:
|
| 1717 | + def test_eq(self) -> None: |
| 1718 | + assert padding.MGF1(hashes.SHA256()) == padding.MGF1(hashes.SHA256()) |
| 1719 | + assert padding.MGF1(hashes.SHA256()) != padding.MGF1(hashes.SHA512()) |
| 1720 | + |
1649 | 1721 | def test_invalid_hash_algorithm(self):
|
1650 | 1722 | with pytest.raises(TypeError):
|
1651 | 1723 | padding.MGF1(b"not_a_hash") # type:ignore[arg-type]
|
@@ -1680,6 +1752,47 @@ def test_mgf_property(self):
|
1680 | 1752 | assert oaep.mgf == mgf
|
1681 | 1753 | assert oaep.mgf == oaep._mgf
|
1682 | 1754 |
|
| 1755 | + @pytest.mark.parametrize("xof", [hashes.SHA256(), hashes.SHA512()]) |
| 1756 | + @pytest.mark.parametrize("label", [None, b"", b"foo"]) |
| 1757 | + def test_eq(self, xof: hashes.HashAlgorithm, label: bytes | None) -> None: |
| 1758 | + mgf = padding.MGF1(algorithm=xof) |
| 1759 | + assert padding.OAEP( |
| 1760 | + mgf=mgf, algorithm=xof, label=label |
| 1761 | + ) == padding.OAEP(mgf=mgf, algorithm=xof, label=label) |
| 1762 | + |
| 1763 | + def test_not_eq_with_different_mgf(self) -> None: |
| 1764 | + assert padding.OAEP( |
| 1765 | + mgf=padding.MGF1(algorithm=hashes.SHA256()), |
| 1766 | + algorithm=hashes.SHA256(), |
| 1767 | + label=None, |
| 1768 | + ) != padding.OAEP( |
| 1769 | + mgf=padding.MGF1(algorithm=hashes.SHA512()), |
| 1770 | + algorithm=hashes.SHA256(), |
| 1771 | + label=None, |
| 1772 | + ) |
| 1773 | + |
| 1774 | + def test_not_eq_with_different_algorithm(self) -> None: |
| 1775 | + assert padding.OAEP( |
| 1776 | + mgf=padding.MGF1(algorithm=hashes.SHA512()), |
| 1777 | + algorithm=hashes.SHA512(), |
| 1778 | + label=None, |
| 1779 | + ) != padding.OAEP( |
| 1780 | + mgf=padding.MGF1(algorithm=hashes.SHA512()), |
| 1781 | + algorithm=hashes.SHA256(), |
| 1782 | + label=None, |
| 1783 | + ) |
| 1784 | + |
| 1785 | + def test_not_eq_with_different_label(self) -> None: |
| 1786 | + assert padding.OAEP( |
| 1787 | + mgf=padding.MGF1(algorithm=hashes.SHA512()), |
| 1788 | + algorithm=hashes.SHA256(), |
| 1789 | + label=None, |
| 1790 | + ) != padding.OAEP( |
| 1791 | + mgf=padding.MGF1(algorithm=hashes.SHA512()), |
| 1792 | + algorithm=hashes.SHA256(), |
| 1793 | + label=b"", |
| 1794 | + ) |
| 1795 | + |
1683 | 1796 |
|
1684 | 1797 | class TestRSADecryption:
|
1685 | 1798 | @pytest.mark.supported(
|
|
0 commit comments