Skip to content

Commit

Permalink
Merge pull request #1665 from reaperhulk/move-asym-padding-interface
Browse files Browse the repository at this point in the history
move asymmetric padding interface to primitives.asymmetric.padding
  • Loading branch information
alex committed Feb 16, 2015
2 parents 99c1b80 + a308f9f commit 426eee7
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 38 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ Changelog
* :class:`~cryptography.hazmat.primitives.padding.PaddingContext` was moved
from :mod:`~cryptography.hazmat.primitives.interfaces` to
:mod:`~cryptography.hazmat.primitives.padding`.
*
:class:`~cryptography.hazmat.primitives.asymmetric.padding.AsymmetricPadding`
was moved from :mod:`~cryptography.hazmat.primitives.interfaces` to
:mod:`~cryptography.hazmat.primitives.asymmetric.padding`.
* :class:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAParameters`,
:class:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAParametersWithNumbers`,
:class:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAPrivateKey`,
Expand Down
2 changes: 1 addition & 1 deletion docs/hazmat/backends/interfaces.rst
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ A specific ``backend`` may provide one or more of these interfaces.
Check if the specified ``padding`` is supported by the backend.

:param padding: An instance of an
:class:`~cryptography.hazmat.primitives.interfaces.AsymmetricPadding`
:class:`~cryptography.hazmat.primitives.asymmetric.padding.AsymmetricPadding`
provider.

:returns: ``True`` if the specified ``padding`` is supported by this
Expand Down
16 changes: 11 additions & 5 deletions docs/hazmat/primitives/asymmetric/rsa.rst
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,13 @@ Once you have an encrypted message, it can be decrypted using the private key:
Padding
~~~~~~~

.. currentmodule:: cryptography.hazmat.primitives.asymmetric.padding
.. module:: cryptography.hazmat.primitives.asymmetric.padding

.. class:: AsymmetricPadding

.. versionadded:: 0.2

.. attribute:: name

.. class:: PSS(mgf, salt_length)

Expand Down Expand Up @@ -425,7 +431,7 @@ Key interfaces
Sign data which can be verified later by others using the public key.

:param padding: An instance of a
:class:`~cryptography.hazmat.primitives.interfaces.AsymmetricPadding`
:class:`~cryptography.hazmat.primitives.asymmetric.padding.AsymmetricPadding`
provider.

:param algorithm: An instance of a
Expand All @@ -444,7 +450,7 @@ Key interfaces
:param bytes ciphertext: The ciphertext to decrypt.

:param padding: An instance of an
:class:`~cryptography.hazmat.primitives.interfaces.AsymmetricPadding`
:class:`~cryptography.hazmat.primitives.asymmetric.padding.AsymmetricPadding`
provider.

:return bytes: Decrypted data.
Expand Down Expand Up @@ -495,7 +501,7 @@ Key interfaces
:param bytes signature: The signature to verify.

:param padding: An instance of a
:class:`~cryptography.hazmat.primitives.interfaces.AsymmetricPadding`
:class:`~cryptography.hazmat.primitives.asymmetric.padding.AsymmetricPadding`
provider.

:param algorithm: An instance of a
Expand All @@ -514,7 +520,7 @@ Key interfaces
:param bytes plaintext: The plaintext to encrypt.

:param padding: An instance of a
:class:`~cryptography.hazmat.primitives.interfaces.AsymmetricPadding`
:class:`~cryptography.hazmat.primitives.asymmetric.padding.AsymmetricPadding`
provider.

:return bytes: Encrypted data.
Expand Down
7 changes: 2 additions & 5 deletions docs/hazmat/primitives/interfaces.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@ Asymmetric interfaces
not validate.


.. class:: AsymmetricPadding

.. versionadded:: 0.2

.. attribute:: name
In 0.8 the asymmetric padding interface was moved to the
:mod:`cryptography.hazmat.primitives.asymmetric.padding` module.

DSA
~~~
Expand Down
14 changes: 6 additions & 8 deletions src/cryptography/hazmat/backends/openssl/rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from cryptography.hazmat.primitives import hashes, interfaces
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives.asymmetric.padding import (
MGF1, OAEP, PKCS1v15, PSS
AsymmetricPadding, MGF1, OAEP, PKCS1v15, PSS
)
from cryptography.hazmat.primitives.interfaces import (
RSAPrivateKeyWithNumbers, RSAPublicKeyWithNumbers
Expand All @@ -34,7 +34,7 @@ def _get_rsa_pss_salt_length(pss, key_size, digest_size):


def _enc_dec_rsa(backend, key, data, padding):
if not isinstance(padding, interfaces.AsymmetricPadding):
if not isinstance(padding, AsymmetricPadding):
raise TypeError("Padding must be an instance of AsymmetricPadding.")

if isinstance(padding, PKCS1v15):
Expand Down Expand Up @@ -150,9 +150,8 @@ def __init__(self, backend, private_key, padding, algorithm):
self._backend = backend
self._private_key = private_key

if not isinstance(padding, interfaces.AsymmetricPadding):
raise TypeError(
"Expected provider of interfaces.AsymmetricPadding.")
if not isinstance(padding, AsymmetricPadding):
raise TypeError("Expected provider of AsymmetricPadding.")

self._pkey_size = self._backend._lib.EVP_PKEY_size(
self._private_key._evp_pkey
Expand Down Expand Up @@ -339,9 +338,8 @@ def __init__(self, backend, public_key, signature, padding, algorithm):
self._public_key = public_key
self._signature = signature

if not isinstance(padding, interfaces.AsymmetricPadding):
raise TypeError(
"Expected provider of interfaces.AsymmetricPadding.")
if not isinstance(padding, AsymmetricPadding):
raise TypeError("Expected provider of AsymmetricPadding.")

self._pkey_size = self._backend._lib.EVP_PKEY_size(
self._public_key._evp_pkey
Expand Down
19 changes: 15 additions & 4 deletions src/cryptography/hazmat/primitives/asymmetric/padding.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,29 @@

from __future__ import absolute_import, division, print_function

import abc

import six

from cryptography import utils
from cryptography.hazmat.primitives import hashes, interfaces
from cryptography.hazmat.primitives import hashes


@six.add_metaclass(abc.ABCMeta)
class AsymmetricPadding(object):
@abc.abstractproperty
def name(self):
"""
A string naming this padding (e.g. "PSS", "PKCS1").
"""


@utils.register_interface(interfaces.AsymmetricPadding)
@utils.register_interface(AsymmetricPadding)
class PKCS1v15(object):
name = "EMSA-PKCS1-v1_5"


@utils.register_interface(interfaces.AsymmetricPadding)
@utils.register_interface(AsymmetricPadding)
class PSS(object):
MAX_LENGTH = object()
name = "EMSA-PSS"
Expand All @@ -33,7 +44,7 @@ def __init__(self, mgf, salt_length):
self._salt_length = salt_length


@utils.register_interface(interfaces.AsymmetricPadding)
@utils.register_interface(AsymmetricPadding)
class OAEP(object):
name = "EME-OAEP"

Expand Down
28 changes: 16 additions & 12 deletions src/cryptography/hazmat/primitives/interfaces/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
import six

from cryptography import utils
from cryptography.hazmat.primitives import ciphers, hashes, padding
from cryptography.hazmat.primitives.asymmetric import dsa, ec, rsa
from cryptography.hazmat.primitives import ciphers, hashes
from cryptography.hazmat.primitives.asymmetric import (
dsa, ec, padding, rsa
)
from cryptography.hazmat.primitives.ciphers import modes
from cryptography.hazmat.primitives.padding import PaddingContext


BlockCipherAlgorithm = utils.deprecated(
Expand Down Expand Up @@ -241,7 +244,7 @@


PaddingContext = utils.deprecated(
padding.PaddingContext,
PaddingContext,
__name__,
(
"The PaddingContext interface has moved to the "
Expand Down Expand Up @@ -313,6 +316,16 @@
utils.DeprecatedIn08
)

AsymmetricPadding = utils.deprecated(
padding.AsymmetricPadding,
__name__,
(
"The AsymmetricPadding interface has moved to the "
"cryptography.hazmat.primitives.asymmetric.padding module"
),
utils.DeprecatedIn08
)


@six.add_metaclass(abc.ABCMeta)
class AsymmetricSignatureContext(object):
Expand Down Expand Up @@ -345,15 +358,6 @@ def verify(self):
"""


@six.add_metaclass(abc.ABCMeta)
class AsymmetricPadding(object):
@abc.abstractproperty
def name(self):
"""
A string naming this padding (e.g. "PSS", "PKCS1").
"""


@six.add_metaclass(abc.ABCMeta)
class KeyDerivationFunction(object):
@abc.abstractmethod
Expand Down
4 changes: 2 additions & 2 deletions tests/hazmat/backends/test_openssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Backend, backend
)
from cryptography.hazmat.backends.openssl.ec import _sn_to_elliptic_curve
from cryptography.hazmat.primitives import hashes, interfaces
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import dsa, padding
from cryptography.hazmat.primitives.ciphers import (
BlockCipherAlgorithm, Cipher, CipherAlgorithm
Expand All @@ -45,7 +45,7 @@ class DummyCipher(object):
key_size = None


@utils.register_interface(interfaces.AsymmetricPadding)
@utils.register_interface(padding.AsymmetricPadding)
class DummyPadding(object):
name = "dummy-cipher"

Expand Down
2 changes: 1 addition & 1 deletion tests/hazmat/primitives/test_rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
)


@utils.register_interface(interfaces.AsymmetricPadding)
@utils.register_interface(padding.AsymmetricPadding)
class DummyPadding(object):
name = "UNSUPPORTED-PADDING"

Expand Down

0 comments on commit 426eee7

Please sign in to comment.