Skip to content

Commit

Permalink
Merge pull request #1666 from reaperhulk/move-asym-interfaces
Browse files Browse the repository at this point in the history
move asymmetric signature/verification interfaces
  • Loading branch information
alex committed Feb 17, 2015
2 parents 426eee7 + 25bbc15 commit 5e208e7
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 74 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ Changelog
: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.AsymmetricSignatureContext`
and
:class:`~cryptography.hazmat.primitives.asymmetric.AsymmetricVerificationContext`
were moved from :mod:`~cryptography.hazmat.primitives.interfaces` to
:mod:`~cryptography.hazmat.primitives.asymmetric`.
* :class:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAParameters`,
:class:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAParametersWithNumbers`,
:class:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAPrivateKey`,
Expand Down
4 changes: 2 additions & 2 deletions docs/hazmat/primitives/asymmetric/dsa.rst
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ Key interfaces
provider.

:returns:
:class:`~cryptography.hazmat.primitives.interfaces.AsymmetricSignatureContext`
:class:`~cryptography.hazmat.primitives.asymmetric.AsymmetricSignatureContext`

.. attribute:: key_size

Expand Down Expand Up @@ -338,7 +338,7 @@ Key interfaces
provider.

:returns:
:class:`~cryptography.hazmat.primitives.interfaces.AsymmetricVerificationContext`
:class:`~cryptography.hazmat.primitives.asymmetric.AsymmetricVerificationContext`


.. class:: DSAPublicKeyWithNumbers
Expand Down
4 changes: 2 additions & 2 deletions docs/hazmat/primitives/asymmetric/ec.rst
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ Key Interfaces
:class:`EllipticCurveSignatureAlgorithm` provider.

:returns:
:class:`~cryptography.hazmat.primitives.interfaces.AsymmetricSignatureContext`
:class:`~cryptography.hazmat.primitives.asymmetric.AsymmetricSignatureContext`

.. method:: public_key()

Expand Down Expand Up @@ -344,7 +344,7 @@ Key Interfaces
:class:`EllipticCurveSignatureAlgorithm` provider.

:returns:
:class:`~cryptography.hazmat.primitives.interfaces.AsymmetricSignatureContext`
:class:`~cryptography.hazmat.primitives.asymmetric.AsymmetricVerificationContext`

.. attribute:: curve

Expand Down
2 changes: 2 additions & 0 deletions docs/hazmat/primitives/asymmetric/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ and Elliptic Curve.
ec
rsa
serialization
interfaces
utils


.. _`proof of identity`: https://en.wikipedia.org/wiki/Public-key_infrastructure
33 changes: 33 additions & 0 deletions docs/hazmat/primitives/asymmetric/interfaces.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.. hazmat::

.. module:: cryptography.hazmat.primitives.asymmetric

Signature Interfaces
====================

.. class:: AsymmetricSignatureContext

.. versionadded:: 0.2

.. method:: update(data)

:param bytes data: The data you want to sign.

.. method:: finalize()

:return bytes signature: The signature.


.. class:: AsymmetricVerificationContext

.. versionadded:: 0.2

.. method:: update(data)

:param bytes data: The data you wish to verify using the signature.

.. method:: verify()

:raises cryptography.exceptions.InvalidSignature: If the signature does
not validate.

4 changes: 2 additions & 2 deletions docs/hazmat/primitives/asymmetric/rsa.rst
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ Key interfaces
provider.

:returns:
:class:`~cryptography.hazmat.primitives.interfaces.AsymmetricSignatureContext`
:class:`~cryptography.hazmat.primitives.asymmetric.AsymmetricSignatureContext`

.. method:: decrypt(ciphertext, padding)

Expand Down Expand Up @@ -509,7 +509,7 @@ Key interfaces
provider.

:returns:
:class:`~cryptography.hazmat.primitives.interfaces.AsymmetricVerificationContext`
:class:`~cryptography.hazmat.primitives.asymmetric.AsymmetricVerificationContext`

.. method:: encrypt(plaintext, padding)

Expand Down
28 changes: 2 additions & 26 deletions docs/hazmat/primitives/interfaces.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,8 @@ to document argument and return types.
Asymmetric interfaces
---------------------

.. class:: AsymmetricSignatureContext

.. versionadded:: 0.2

.. method:: update(data)

:param bytes data: The data you want to sign.

.. method:: finalize()

:return bytes signature: The signature.


.. class:: AsymmetricVerificationContext

.. versionadded:: 0.2

.. method:: update(data)

:param bytes data: The data you wish to verify using the signature.

.. method:: verify()

:raises cryptography.exceptions.InvalidSignature: If the signature does
not validate.

In 0.8 the asymmetric signature and verification interfaces were moved to the
:mod:`cryptography.hazmat.primitives.asymmetric` module.

In 0.8 the asymmetric padding interface was moved to the
:mod:`cryptography.hazmat.primitives.asymmetric.padding` module.
Expand Down
10 changes: 6 additions & 4 deletions src/cryptography/hazmat/backends/openssl/dsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
from cryptography import utils
from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.backends.openssl.utils import _truncate_digest
from cryptography.hazmat.primitives import hashes, interfaces
from cryptography.hazmat.primitives.asymmetric import dsa
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import (
AsymmetricSignatureContext, AsymmetricVerificationContext, dsa
)
from cryptography.hazmat.primitives.interfaces import (
DSAParametersWithNumbers, DSAPrivateKeyWithNumbers, DSAPublicKeyWithNumbers
)
Expand All @@ -27,7 +29,7 @@ def _truncate_digest_for_dsa(dsa_cdata, digest, backend):
return _truncate_digest(digest, order_bits)


@utils.register_interface(interfaces.AsymmetricVerificationContext)
@utils.register_interface(AsymmetricVerificationContext)
class _DSAVerificationContext(object):
def __init__(self, backend, public_key, signature, algorithm):
self._backend = backend
Expand Down Expand Up @@ -61,7 +63,7 @@ def verify(self):
raise InvalidSignature


@utils.register_interface(interfaces.AsymmetricSignatureContext)
@utils.register_interface(AsymmetricSignatureContext)
class _DSASignatureContext(object):
def __init__(self, backend, private_key, algorithm):
self._backend = backend
Expand Down
10 changes: 6 additions & 4 deletions src/cryptography/hazmat/backends/openssl/ec.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
InvalidSignature, UnsupportedAlgorithm, _Reasons
)
from cryptography.hazmat.backends.openssl.utils import _truncate_digest
from cryptography.hazmat.primitives import hashes, interfaces
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import (
AsymmetricSignatureContext, AsymmetricVerificationContext, ec
)


def _truncate_digest_for_ecdsa(ec_key_cdata, digest, backend):
Expand Down Expand Up @@ -80,7 +82,7 @@ def _sn_to_elliptic_curve(backend, sn):
)


@utils.register_interface(interfaces.AsymmetricSignatureContext)
@utils.register_interface(AsymmetricSignatureContext)
class _ECDSASignatureContext(object):
def __init__(self, backend, private_key, algorithm):
self._backend = backend
Expand Down Expand Up @@ -114,7 +116,7 @@ def finalize(self):
return self._backend._ffi.buffer(sigbuf)[:siglen_ptr[0]]


@utils.register_interface(interfaces.AsymmetricVerificationContext)
@utils.register_interface(AsymmetricVerificationContext)
class _ECDSAVerificationContext(object):
def __init__(self, backend, public_key, signature, algorithm):
self._backend = backend
Expand Down
10 changes: 6 additions & 4 deletions src/cryptography/hazmat/backends/openssl/rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
from cryptography.exceptions import (
AlreadyFinalized, InvalidSignature, UnsupportedAlgorithm, _Reasons
)
from cryptography.hazmat.primitives import hashes, interfaces
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import (
AsymmetricSignatureContext, AsymmetricVerificationContext, rsa
)
from cryptography.hazmat.primitives.asymmetric.padding import (
AsymmetricPadding, MGF1, OAEP, PKCS1v15, PSS
)
Expand Down Expand Up @@ -144,7 +146,7 @@ def _handle_rsa_enc_dec_error(backend, key):
raise ValueError("Decryption failed.")


@utils.register_interface(interfaces.AsymmetricSignatureContext)
@utils.register_interface(AsymmetricSignatureContext)
class _RSASignatureContext(object):
def __init__(self, backend, private_key, padding, algorithm):
self._backend = backend
Expand Down Expand Up @@ -331,7 +333,7 @@ def _finalize_pss(self, evp_md):
return self._backend._ffi.buffer(sig_buf)[:sig_len]


@utils.register_interface(interfaces.AsymmetricVerificationContext)
@utils.register_interface(AsymmetricVerificationContext)
class _RSAVerificationContext(object):
def __init__(self, backend, public_key, signature, padding, algorithm):
self._backend = backend
Expand Down
35 changes: 35 additions & 0 deletions src/cryptography/hazmat/primitives/asymmetric/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,38 @@
# for complete details.

from __future__ import absolute_import, division, print_function

import abc

import six


@six.add_metaclass(abc.ABCMeta)
class AsymmetricSignatureContext(object):
@abc.abstractmethod
def update(self, data):
"""
Processes the provided bytes and returns nothing.
"""

@abc.abstractmethod
def finalize(self):
"""
Returns the signature as bytes.
"""


@six.add_metaclass(abc.ABCMeta)
class AsymmetricVerificationContext(object):
@abc.abstractmethod
def update(self, data):
"""
Processes the provided bytes and returns nothing.
"""

@abc.abstractmethod
def verify(self):
"""
Raises an exception if the bytes provided to update do not match the
signature or the signature does not match the public key.
"""
50 changes: 20 additions & 30 deletions src/cryptography/hazmat/primitives/interfaces/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from cryptography import utils
from cryptography.hazmat.primitives import ciphers, hashes
from cryptography.hazmat.primitives.asymmetric import (
dsa, ec, padding, rsa
AsymmetricSignatureContext, AsymmetricVerificationContext, dsa, ec,
padding, rsa
)
from cryptography.hazmat.primitives.ciphers import modes
from cryptography.hazmat.primitives.padding import PaddingContext
Expand Down Expand Up @@ -326,36 +327,25 @@
utils.DeprecatedIn08
)

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

@six.add_metaclass(abc.ABCMeta)
class AsymmetricSignatureContext(object):
@abc.abstractmethod
def update(self, data):
"""
Processes the provided bytes and returns nothing.
"""

@abc.abstractmethod
def finalize(self):
"""
Returns the signature as bytes.
"""


@six.add_metaclass(abc.ABCMeta)
class AsymmetricVerificationContext(object):
@abc.abstractmethod
def update(self, data):
"""
Processes the provided bytes and returns nothing.
"""

@abc.abstractmethod
def verify(self):
"""
Raises an exception if the bytes provided to update do not match the
signature or the signature does not match the public key.
"""
AsymmetricVerificationContext = utils.deprecated(
AsymmetricVerificationContext,
__name__,
(
"The AsymmetricVerificationContext interface has moved to the "
"cryptography.hazmat.primitives.asymmetric module"
),
utils.DeprecatedIn08
)


@six.add_metaclass(abc.ABCMeta)
Expand Down

0 comments on commit 5e208e7

Please sign in to comment.