Skip to content

Commit 6478f7e

Browse files
authored
explicitly support bytes-like for signature/data in RSA sign/verify (#10259) (#10261)
this was never documented but previously worked in <42. we now also document that this is supported to confuse ourselves less.
1 parent 4bb8596 commit 6478f7e

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

docs/hazmat/primitives/asymmetric/rsa.rst

+6-3
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,8 @@ Key interfaces
620620
Sign one block of data which can be verified later by others using the
621621
public key.
622622

623-
:param bytes data: The message string to sign.
623+
:param data: The message string to sign.
624+
:type data: :term:`bytes-like`
624625

625626
:param padding: An instance of
626627
:class:`~cryptography.hazmat.primitives.asymmetric.padding.AsymmetricPadding`.
@@ -739,9 +740,11 @@ Key interfaces
739740
Verify one block of data was signed by the private key
740741
associated with this public key.
741742

742-
:param bytes signature: The signature to verify.
743+
:param signature: The signature to verify.
744+
:type signature: :term:`bytes-like`
743745

744-
:param bytes data: The message string that was signed.
746+
:param data: The message string that was signed.
747+
:type data: :term:`bytes-like`
745748

746749
:param padding: An instance of
747750
:class:`~cryptography.hazmat.primitives.asymmetric.padding.AsymmetricPadding`.

src/rust/src/backend/rsa.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::collections::hash_map::DefaultHasher;
66
use std::hash::{Hash, Hasher};
77

88
use crate::backend::{hashes, utils};
9+
use crate::buf::CffiBuf;
910
use crate::error::{CryptographyError, CryptographyResult};
1011
use crate::{exceptions, types};
1112

@@ -281,11 +282,12 @@ impl RsaPrivateKey {
281282
fn sign<'p>(
282283
&self,
283284
py: pyo3::Python<'p>,
284-
data: &[u8],
285+
data: CffiBuf<'_>,
285286
padding: &pyo3::PyAny,
286287
algorithm: &pyo3::PyAny,
287288
) -> CryptographyResult<&'p pyo3::PyAny> {
288-
let (data, algorithm) = utils::calculate_digest_and_algorithm(py, data, algorithm)?;
289+
let (data, algorithm) =
290+
utils::calculate_digest_and_algorithm(py, data.as_bytes(), algorithm)?;
289291

290292
let mut ctx = openssl::pkey_ctx::PkeyCtx::new(&self.pkey)?;
291293
ctx.sign_init().map_err(|_| {
@@ -419,18 +421,19 @@ impl RsaPublicKey {
419421
fn verify(
420422
&self,
421423
py: pyo3::Python<'_>,
422-
signature: &[u8],
423-
data: &[u8],
424+
signature: CffiBuf<'_>,
425+
data: CffiBuf<'_>,
424426
padding: &pyo3::PyAny,
425427
algorithm: &pyo3::PyAny,
426428
) -> CryptographyResult<()> {
427-
let (data, algorithm) = utils::calculate_digest_and_algorithm(py, data, algorithm)?;
429+
let (data, algorithm) =
430+
utils::calculate_digest_and_algorithm(py, data.as_bytes(), algorithm)?;
428431

429432
let mut ctx = openssl::pkey_ctx::PkeyCtx::new(&self.pkey)?;
430433
ctx.verify_init()?;
431434
setup_signature_ctx(py, &mut ctx, padding, algorithm, self.pkey.size(), false)?;
432435

433-
let valid = ctx.verify(data, signature).unwrap_or(false);
436+
let valid = ctx.verify(data, signature.as_bytes()).unwrap_or(false);
434437
if !valid {
435438
return Err(CryptographyError::from(
436439
exceptions::InvalidSignature::new_err(()),

tests/hazmat/primitives/test_rsa.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -763,9 +763,15 @@ def test_pkcs1_minimum_key_size(self, backend):
763763
)
764764
private_key.sign(b"no failure", padding.PKCS1v15(), hashes.SHA512())
765765

766-
def test_sign(self, rsa_key_2048: rsa.RSAPrivateKey, backend):
766+
@pytest.mark.parametrize(
767+
"message",
768+
[
769+
b"one little message",
770+
bytearray(b"one little message"),
771+
],
772+
)
773+
def test_sign(self, rsa_key_2048: rsa.RSAPrivateKey, message, backend):
767774
private_key = rsa_key_2048
768-
message = b"one little message"
769775
pkcs = padding.PKCS1v15()
770776
algorithm = hashes.SHA256()
771777
signature = private_key.sign(message, pkcs, algorithm)
@@ -1375,9 +1381,15 @@ def test_pss_verify_salt_length_too_long(self, backend):
13751381
hashes.SHA1(),
13761382
)
13771383

1378-
def test_verify(self, rsa_key_2048: rsa.RSAPrivateKey, backend):
1384+
@pytest.mark.parametrize(
1385+
"message",
1386+
[
1387+
b"one little message",
1388+
bytearray(b"one little message"),
1389+
],
1390+
)
1391+
def test_verify(self, rsa_key_2048: rsa.RSAPrivateKey, message, backend):
13791392
private_key = rsa_key_2048
1380-
message = b"one little message"
13811393
pkcs = padding.PKCS1v15()
13821394
algorithm = hashes.SHA256()
13831395
signature = private_key.sign(message, pkcs, algorithm)

0 commit comments

Comments
 (0)