Skip to content

Commit e08560e

Browse files
committed
Add NoDigestInfo to sign using RSA with raw data
1 parent 56e574d commit e08560e

File tree

6 files changed

+20
-7
lines changed

6 files changed

+20
-7
lines changed

src/cryptography/hazmat/primitives/asymmetric/rsa.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ def sign(
4040
self,
4141
data: bytes,
4242
padding: AsymmetricPadding,
43-
algorithm: None | asym_utils.Prehashed | hashes.HashAlgorithm,
43+
algorithm: asym_utils.Prehashed
44+
| hashes.HashAlgorithm
45+
| asym_utils.NoDigestInfo,
4446
) -> bytes:
4547
"""
4648
Signs the data.

src/cryptography/hazmat/primitives/asymmetric/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
encode_dss_signature = asn1.encode_dss_signature
1212

1313

14+
class NoDigestInfo:
15+
pass
16+
17+
1418
class Prehashed:
1519
def __init__(self, algorithm: hashes.HashAlgorithm):
1620
if not isinstance(algorithm, hashes.HashAlgorithm):

src/rust/src/backend/rsa.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,10 @@ impl RsaPrivateKey {
291291
algorithm: &pyo3::Bound<'p, pyo3::PyAny>,
292292
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyAny>> {
293293
let (data, algorithm) = {
294-
if algorithm.is_none() {
294+
if algorithm.is_instance(&types::NO_DIGEST_INFO.get(py)?)? {
295295
(
296296
utils::BytesOrPyBytes::Bytes(data.as_bytes()),
297-
algorithm.clone(),
297+
pyo3::types::PyNone::get(py).to_owned().into_any(),
298298
)
299299
} else {
300300
utils::calculate_digest_and_algorithm(py, data.as_bytes(), algorithm)?
@@ -443,10 +443,10 @@ impl RsaPublicKey {
443443
algorithm: &pyo3::Bound<'_, pyo3::PyAny>,
444444
) -> CryptographyResult<()> {
445445
let (data, algorithm) = {
446-
if algorithm.is_none() {
446+
if algorithm.is_instance(&types::NO_DIGEST_INFO.get(py)?)? {
447447
(
448448
utils::BytesOrPyBytes::Bytes(data.as_bytes()),
449-
algorithm.clone(),
449+
pyo3::types::PyNone::get(py).to_owned().into_any(),
450450
)
451451
} else {
452452
utils::calculate_digest_and_algorithm(py, data.as_bytes(), algorithm)?

src/rust/src/types.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,10 @@ pub static SHA1: LazyPyImport =
393393
pub static SHA256: LazyPyImport =
394394
LazyPyImport::new("cryptography.hazmat.primitives.hashes", &["SHA256"]);
395395

396+
pub static NO_DIGEST_INFO: LazyPyImport = LazyPyImport::new(
397+
"cryptography.hazmat.primitives.asymmetric.utils",
398+
&["NoDigestInfo"],
399+
);
396400
pub static PREHASHED: LazyPyImport = LazyPyImport::new(
397401
"cryptography.hazmat.primitives.asymmetric.utils",
398402
&["Prehashed"],

tests/hazmat/primitives/test_rsa.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ def test_pkcs1v15_signing_without_digest(self, backend, subtests):
483483
)
484484
),
485485
padding.PKCS1v15(),
486-
None,
486+
asym_utils.NoDigestInfo(),
487487
)
488488
assert binascii.hexlify(signature) == example["signature"]
489489

tests/hazmat/primitives/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
)
2222
from cryptography.hazmat.primitives import hashes, hmac, serialization
2323
from cryptography.hazmat.primitives.asymmetric import rsa
24+
from cryptography.hazmat.primitives.asymmetric import utils as asym_utils
2425
from cryptography.hazmat.primitives.ciphers import (
2526
BlockCipherAlgorithm,
2627
Cipher,
@@ -525,7 +526,9 @@ def test_rsa_verification(self, backend, subtests):
525526
params["msg"] = compute_rsa_hash_digest(
526527
backend, hash_alg, params["msg"]
527528
)
528-
rsa_verification_test(backend, params, None, pad_factory)
529+
rsa_verification_test(
530+
backend, params, asym_utils.NoDigestInfo(), pad_factory
531+
)
529532

530533
return test_rsa_verification
531534

0 commit comments

Comments
 (0)