Skip to content

Add __eq__ and missing properties #13271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ Changelog
* Removed the deprecated ``CAST5``, ``SEED``, ``IDEA``, and ``Blowfish``
classes from the cipher module. These are still available in
:doc:`/hazmat/decrepit/index`.
* Make instances of
:class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm` as well as
instances of classes in
:mod:`~cryptography.hazmat.primitives.asymmetric.padding`
comparable.
* Added `salt_length` property to
:class:`~cryptography.hazmat.primitives.asymmetric.padding.PSS`.
* Added `label` property to
:class:`~cryptography.hazmat.primitives.asymmetric.padding.OAEP`.
* Added `algorithm` property to
:class:`~cryptography.hazmat.primitives.asymmetric.padding.MGF1`.


.. _v45-0-6:

Expand Down
2 changes: 1 addition & 1 deletion docs/hazmat/primitives/asymmetric/cloudhsm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ if you only need a subset of functionality.
... Maps the cryptography padding and algorithm to the corresponding KMS signing algorithm.
... This is specific to your implementation.
... """
... if isinstance(padding, PKCS1v15) and isinstance(algorithm, hashes.SHA256):
... if padding == PKCS1v15() and algorithm == hashes.SHA256():
... return b"RSA_PKCS1_V1_5_SHA_256"
... else:
... raise NotImplementedError()
Expand Down
24 changes: 24 additions & 0 deletions docs/hazmat/primitives/asymmetric/rsa.rst
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,14 @@ Padding

The padding's mask generation function (MGF).

.. attribute:: salt_length

:type: int

.. versionadded:: 46.0.0

The length of the salt.

.. class:: OAEP(mgf, algorithm, label)

.. versionadded:: 0.4
Expand All @@ -351,6 +359,14 @@ Padding

The padding's hash algorithm.

.. attribute:: label

:type: bytes | None

.. versionadded:: 42.0.0

The padding's hash algorithm.

.. attribute:: mgf

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

.. attribute:: algorithm

:type: :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`.

.. versionadded:: 46.0.0

The algorithm of this instance.

Numbers
~~~~~~~

Expand Down
10 changes: 5 additions & 5 deletions docs/x509/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ Loading Certificate Revocation Lists
>>> from cryptography import x509
>>> from cryptography.hazmat.primitives import hashes
>>> crl = x509.load_pem_x509_crl(pem_crl_data)
>>> isinstance(crl.signature_hash_algorithm, hashes.SHA256)
>>> crl.signature_hash_algorithm == hashes.SHA256()
True

.. function:: load_der_x509_crl(data)
Expand Down Expand Up @@ -287,7 +287,7 @@ Loading Certificate Signing Requests
>>> from cryptography import x509
>>> from cryptography.hazmat.primitives import hashes
>>> csr = x509.load_pem_x509_csr(pem_req_data)
>>> isinstance(csr.signature_hash_algorithm, hashes.SHA256)
>>> csr.signature_hash_algorithm == hashes.SHA256()
True

.. function:: load_der_x509_csr(data)
Expand Down Expand Up @@ -477,7 +477,7 @@ X.509 Certificate Object
.. doctest::

>>> from cryptography.hazmat.primitives import hashes
>>> isinstance(cert.signature_hash_algorithm, hashes.SHA256)
>>> cert.signature_hash_algorithm == hashes.SHA256()
True

.. attribute:: signature_algorithm_oid
Expand Down Expand Up @@ -716,7 +716,7 @@ X.509 CRL (Certificate Revocation List) Object
.. doctest::

>>> from cryptography.hazmat.primitives import hashes
>>> isinstance(crl.signature_hash_algorithm, hashes.SHA256)
>>> crl.signature_hash_algorithm == hashes.SHA256()
True

.. attribute:: signature_algorithm_oid
Expand Down Expand Up @@ -1119,7 +1119,7 @@ X.509 CSR (Certificate Signing Request) Object
.. doctest::

>>> from cryptography.hazmat.primitives import hashes
>>> isinstance(csr.signature_hash_algorithm, hashes.SHA256)
>>> csr.signature_hash_algorithm == hashes.SHA256()
True

.. attribute:: signature_algorithm_oid
Expand Down
46 changes: 46 additions & 0 deletions src/cryptography/hazmat/primitives/asymmetric/padding.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import annotations

import abc
import typing

from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives._asymmetric import (
Expand All @@ -16,6 +17,9 @@
class PKCS1v15(AsymmetricPadding):
name = "EMSA-PKCS1-v1_5"

def __eq__(self, other: typing.Any) -> bool:
return isinstance(other, PKCS1v15)


class _MaxLength:
"Sentinel value for `MAX_LENGTH`."
Expand Down Expand Up @@ -56,10 +60,26 @@ def __init__(

self._salt_length = salt_length

def __eq__(self, other: typing.Any) -> bool:
if isinstance(self._salt_length, int):
eq_salt_length = self._salt_length == other._salt_length
else:
eq_salt_length = self._salt_length is other._salt_length

return (
isinstance(other, PSS)
and eq_salt_length
and self._mgf == other._mgf
)

@property
def mgf(self) -> MGF:
return self._mgf

@property
def salt_length(self) -> int | _MaxLength | _Auto | _DigestLength:
return self._salt_length


class OAEP(AsymmetricPadding):
name = "EME-OAEP"
Expand All @@ -77,10 +97,22 @@ def __init__(
self._algorithm = algorithm
self._label = label

def __eq__(self, other: typing.Any) -> bool:
return (
isinstance(other, OAEP)
and self._mgf == other._mgf
and self._algorithm == other._algorithm
and self._label == other._label
)

@property
def algorithm(self) -> hashes.HashAlgorithm:
return self._algorithm

@property
def label(self) -> bytes | None:
return self._label

@property
def mgf(self) -> MGF:
return self._mgf
Expand All @@ -89,6 +121,13 @@ def mgf(self) -> MGF:
class MGF(metaclass=abc.ABCMeta):
_algorithm: hashes.HashAlgorithm

@abc.abstractmethod
def __eq__(self, other: typing.Any) -> bool:
"""
Implement equality checking.
"""
...


class MGF1(MGF):
def __init__(self, algorithm: hashes.HashAlgorithm):
Expand All @@ -97,6 +136,13 @@ def __init__(self, algorithm: hashes.HashAlgorithm):

self._algorithm = algorithm

def __eq__(self, other: typing.Any) -> bool:
return isinstance(other, MGF1) and self._algorithm == other._algorithm

@property
def algorithm(self) -> hashes.HashAlgorithm:
return self._algorithm


def calculate_max_pss_salt_length(
key: rsa.RSAPrivateKey | rsa.RSAPublicKey,
Expand Down
Loading
Loading