Skip to content

Conversation

toppk
Copy link
Contributor

@toppk toppk commented Dec 3, 2019

Not sure how to "type" this better. The PSS interface takes salt_length as an int or an "object", which is a class variable. I couldn't figure out any way to indicate PSS.MAX_LENGTH is the only acceptable value.

Looking for a better way to do this, but a fix is needed (MAX_LENGTH is probably a more common case then a specific int)

sample code (taken from https://cryptography.io/en/latest/hazmat/primitives/asymmetric/rsa/)

>>> chosen_hash = hashes.SHA256()
>>> hasher = hashes.Hash(chosen_hash, default_backend())
>>> hasher.update(b"data & ")
>>> hasher.update(b"more data")
>>> digest = hasher.finalize()
>>> public_key.verify(
...     sig,
...     digest,
...     padding.PSS(
...         mgf=padding.MGF1(hashes.SHA256()),
...         salt_length=padding.PSS.MAX_LENGTH
...     ),
...     utils.Prehashed(chosen_hash)
... )

Below is the implementation for PSS for reference (taken from https://cryptography.io/en/latest/_modules/cryptography/hazmat/primitives/asymmetric/padding/#AsymmetricPadding )

@utils.register_interface(AsymmetricPadding)
class PSS(object):
    MAX_LENGTH = object()
    name = "EMSA-PSS"

    def __init__(self, mgf, salt_length):
        self._mgf = mgf

        if (not isinstance(salt_length, six.integer_types) and
                salt_length is not self.MAX_LENGTH):
            raise TypeError("salt_length must be an integer.")

        if salt_length is not self.MAX_LENGTH and salt_length < 0:
            raise ValueError("salt_length must be zero or greater.")

        self._salt_length = salt_length

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. This is an interesting case and I think your way is the only way to type this at the moment. I will open an issue in the typing repository, though.

@srittau srittau merged commit b585c96 into python:master Dec 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants