aboutsummaryrefslogtreecommitdiffstats
path: root/cryptography
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2014-04-03 10:47:41 -0700
committerAlex Gaynor <alex.gaynor@gmail.com>2014-04-03 10:47:41 -0700
commit74c7bc0fc2f75fd002bbe56fb2c97d6b43f6ae56 (patch)
tree3c13ba1479943b68b19d1998337fd33620d0af1f /cryptography
parent21e7f13db833f5be1fe8187dfde04549e921a379 (diff)
parent325474104e967d563ca93af31957556af01116c6 (diff)
downloadcryptography-74c7bc0fc2f75fd002bbe56fb2c97d6b43f6ae56.tar.gz
cryptography-74c7bc0fc2f75fd002bbe56fb2c97d6b43f6ae56.tar.bz2
cryptography-74c7bc0fc2f75fd002bbe56fb2c97d6b43f6ae56.zip
Merge pull request #883 from reaperhulk/deprecation-dance
move salt_length from MGF1 to PSS and start deprecation cycle
Diffstat (limited to 'cryptography')
-rw-r--r--cryptography/hazmat/backends/openssl/backend.py19
-rw-r--r--cryptography/hazmat/primitives/asymmetric/padding.py42
-rw-r--r--cryptography/utils.py3
3 files changed, 50 insertions, 14 deletions
diff --git a/cryptography/hazmat/backends/openssl/backend.py b/cryptography/hazmat/backends/openssl/backend.py
index 3293741c..0c632aee 100644
--- a/cryptography/hazmat/backends/openssl/backend.py
+++ b/cryptography/hazmat/backends/openssl/backend.py
@@ -701,15 +701,20 @@ class _HMACContext(object):
return self._backend._ffi.buffer(buf)[:outlen[0]]
-def _get_rsa_pss_salt_length(mgf, key_size, digest_size):
- if mgf._salt_length is MGF1.MAX_LENGTH:
+def _get_rsa_pss_salt_length(pss, key_size, digest_size):
+ if pss._mgf._salt_length is not None:
+ salt = pss._mgf._salt_length
+ else:
+ salt = pss._salt_length
+
+ if salt is MGF1.MAX_LENGTH or salt is PSS.MAX_LENGTH:
# bit length - 1 per RFC 3447
emlen = int(math.ceil((key_size - 1) / 8.0))
salt_length = emlen - digest_size - 2
assert salt_length >= 0
return salt_length
else:
- return mgf._salt_length
+ return salt
@utils.register_interface(interfaces.AsymmetricSignatureContext)
@@ -803,7 +808,7 @@ class _RSASignatureContext(object):
res = self._backend._lib.EVP_PKEY_CTX_set_rsa_pss_saltlen(
pkey_ctx,
_get_rsa_pss_salt_length(
- self._padding._mgf,
+ self._padding,
self._private_key.key_size,
self._hash_ctx.algorithm.digest_size
)
@@ -871,7 +876,7 @@ class _RSASignatureContext(object):
data_to_sign,
evp_md,
_get_rsa_pss_salt_length(
- self._padding._mgf,
+ self._padding,
self._private_key.key_size,
len(data_to_sign)
)
@@ -988,7 +993,7 @@ class _RSAVerificationContext(object):
res = self._backend._lib.EVP_PKEY_CTX_set_rsa_pss_saltlen(
pkey_ctx,
_get_rsa_pss_salt_length(
- self._padding._mgf,
+ self._padding,
self._public_key.key_size,
self._hash_ctx.algorithm.digest_size
)
@@ -1068,7 +1073,7 @@ class _RSAVerificationContext(object):
evp_md,
buf,
_get_rsa_pss_salt_length(
- self._padding._mgf,
+ self._padding,
self._public_key.key_size,
len(data_to_verify)
)
diff --git a/cryptography/hazmat/primitives/asymmetric/padding.py b/cryptography/hazmat/primitives/asymmetric/padding.py
index 02aff280..72806a61 100644
--- a/cryptography/hazmat/primitives/asymmetric/padding.py
+++ b/cryptography/hazmat/primitives/asymmetric/padding.py
@@ -13,6 +13,8 @@
from __future__ import absolute_import, division, print_function
+import warnings
+
import six
from cryptography import utils
@@ -26,26 +28,52 @@ class PKCS1v15(object):
@utils.register_interface(interfaces.AsymmetricPadding)
class PSS(object):
+ MAX_LENGTH = object()
name = "EMSA-PSS"
- def __init__(self, mgf):
+ def __init__(self, mgf, salt_length=None):
self._mgf = mgf
+ if salt_length is None:
+ warnings.warn(
+ "salt_length is deprecated on MGF1 and should be added via the"
+ " PSS constructor.",
+ utils.DeprecatedIn04
+ )
+ else:
+ 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")
+
+ if salt_length is None and self._mgf._salt_length is None:
+ raise ValueError("You must supply salt_length")
+
+ self._salt_length = salt_length
+
class MGF1(object):
MAX_LENGTH = object()
- def __init__(self, algorithm, salt_length):
+ def __init__(self, algorithm, salt_length=None):
if not isinstance(algorithm, interfaces.HashAlgorithm):
raise TypeError("Expected instance of interfaces.HashAlgorithm.")
self._algorithm = algorithm
- 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 None:
+ warnings.warn(
+ "salt_length is deprecated on MGF1 and should be passed to "
+ "the PSS constructor instead.",
+ utils.DeprecatedIn04
+ )
+ 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")
+ 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
diff --git a/cryptography/utils.py b/cryptography/utils.py
index eac833b6..5566d123 100644
--- a/cryptography/utils.py
+++ b/cryptography/utils.py
@@ -16,6 +16,9 @@ from __future__ import absolute_import, division, print_function
import sys
+DeprecatedIn04 = PendingDeprecationWarning
+
+
def register_interface(iface):
def register_decorator(klass):
iface.register(klass)