aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2016-11-20 21:13:23 +0800
committerAlex Gaynor <alex.gaynor@gmail.com>2016-11-20 08:13:23 -0500
commit033bd7167d6546d34576dd0d798318999ec82a07 (patch)
tree0d4cd37af635d75692924a3edd2a260c574aa1ed /src
parentd3fd692441cc6ea8fd20dc0c3a834459ff27cf05 (diff)
downloadcryptography-033bd7167d6546d34576dd0d798318999ec82a07.tar.gz
cryptography-033bd7167d6546d34576dd0d798318999ec82a07.tar.bz2
cryptography-033bd7167d6546d34576dd0d798318999ec82a07.zip
support prehashing in RSA sign (#3238)
* support prehashing in RSA sign * check to make sure digest size matches prehashed data provided * move doctest for prehashed
Diffstat (limited to 'src')
-rw-r--r--src/cryptography/hazmat/backends/openssl/rsa.py16
-rw-r--r--src/cryptography/hazmat/primitives/asymmetric/utils.py12
2 files changed, 25 insertions, 3 deletions
diff --git a/src/cryptography/hazmat/backends/openssl/rsa.py b/src/cryptography/hazmat/backends/openssl/rsa.py
index 8bb85783..85d06525 100644
--- a/src/cryptography/hazmat/backends/openssl/rsa.py
+++ b/src/cryptography/hazmat/backends/openssl/rsa.py
@@ -13,6 +13,7 @@ from cryptography.exceptions import (
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import (
AsymmetricSignatureContext, AsymmetricVerificationContext, rsa,
+ utils as asym_utils
)
from cryptography.hazmat.primitives.asymmetric.padding import (
AsymmetricPadding, MGF1, OAEP, PKCS1v15, PSS, calculate_max_pss_salt_length
@@ -452,9 +453,18 @@ class _RSAPrivateKey(object):
padding_enum = _rsa_sig_determine_padding(
self._backend, self, padding, algorithm
)
- hash_ctx = hashes.Hash(algorithm, self._backend)
- hash_ctx.update(data)
- data = hash_ctx.finalize()
+ if not isinstance(algorithm, asym_utils.Prehashed):
+ hash_ctx = hashes.Hash(algorithm, self._backend)
+ hash_ctx.update(data)
+ data = hash_ctx.finalize()
+ else:
+ algorithm = algorithm._algorithm
+
+ if len(data) != algorithm.digest_size:
+ raise ValueError(
+ "The provided data must be the same length as the hash "
+ "algorithm's digest size."
+ )
return _rsa_sig_sign(
self._backend, padding, padding_enum,
diff --git a/src/cryptography/hazmat/primitives/asymmetric/utils.py b/src/cryptography/hazmat/primitives/asymmetric/utils.py
index 5b27654f..44bf59d1 100644
--- a/src/cryptography/hazmat/primitives/asymmetric/utils.py
+++ b/src/cryptography/hazmat/primitives/asymmetric/utils.py
@@ -13,6 +13,7 @@ from pyasn1.type import namedtype, univ
import six
from cryptography import utils
+from cryptography.hazmat.primitives import hashes
class _DSSSigValue(univ.Sequence):
@@ -69,3 +70,14 @@ def encode_dss_signature(r, s):
sig.setComponentByName('r', r)
sig.setComponentByName('s', s)
return encoder.encode(sig)
+
+
+class Prehashed(object):
+ def __init__(self, algorithm):
+ if not isinstance(algorithm, hashes.HashAlgorithm):
+ raise TypeError("Expected instance of HashAlgorithm.")
+
+ self._algorithm = algorithm
+ self._digest_size = algorithm.digest_size
+
+ digest_size = utils.read_only_property("_digest_size")