Skip to content

Commit 92fa9f2

Browse files
authored
support bytes-like consistently across our asym sign/verify APIs (#10260) (#10265)
and update our docs to show it as well
1 parent 6478f7e commit 92fa9f2

File tree

12 files changed

+75
-32
lines changed

12 files changed

+75
-32
lines changed

docs/hazmat/primitives/asymmetric/dsa.rst

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

292-
:param bytes data: The message string to sign.
292+
:param data: The message string to sign.
293+
:type data: :term:`bytes-like`
293294

294295
:param algorithm: An instance of
295296
:class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm` or
@@ -391,9 +392,11 @@ Key interfaces
391392
Verify one block of data was signed by the private key
392393
associated with this public key.
393394

394-
:param bytes signature: The signature to verify.
395+
:param signature: The signature to verify.
396+
:type signature: :term:`bytes-like`
395397

396-
:param bytes data: The message string that was signed.
398+
:param data: The message string that was signed.
399+
:type data: :term:`bytes-like`
397400

398401
:param algorithm: An instance of
399402
:class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm` or

docs/hazmat/primitives/asymmetric/ec.rst

+6-3
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,8 @@ Key Interfaces
569569
Sign one block of data which can be verified later by others using the
570570
public key.
571571

572-
:param bytes data: The message string to sign.
572+
:param data: The message string to sign.
573+
:type data: :term:`bytes-like`
573574

574575
:param signature_algorithm: An instance of
575576
:class:`EllipticCurveSignatureAlgorithm`, such as :class:`ECDSA`.
@@ -678,12 +679,14 @@ Key Interfaces
678679
Verify one block of data was signed by the private key associated
679680
with this public key.
680681

681-
:param bytes signature: The DER-encoded signature to verify.
682+
:param signature: The DER-encoded signature to verify.
682683
A raw signature may be DER-encoded by splitting it into the ``r``
683684
and ``s`` components and passing them into
684685
:func:`~cryptography.hazmat.primitives.asymmetric.utils.encode_dss_signature`.
686+
:type signature: :term:`bytes-like`
685687

686-
:param bytes data: The message string that was signed.
688+
:param data: The message string that was signed.
689+
:type data: :term:`bytes-like`
687690

688691
:param signature_algorithm: An instance of
689692
:class:`EllipticCurveSignatureAlgorithm`.

docs/hazmat/primitives/asymmetric/ed25519.rst

+6-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ Key interfaces
6767

6868
.. method:: sign(data)
6969

70-
:param bytes data: The data to sign.
70+
:param data: The data to sign.
71+
:type data: :term:`bytes-like`
7172

7273
:returns bytes: The 64 byte signature.
7374

@@ -192,9 +193,11 @@ Key interfaces
192193

193194
.. method:: verify(signature, data)
194195

195-
:param bytes signature: The signature to verify.
196+
:param signature: The signature to verify.
197+
:type signature: :term:`bytes-like`
196198

197-
:param bytes data: The data to verify.
199+
:param data: The data to verify.
200+
:type data: :term:`bytes-like`
198201

199202
:returns: None
200203
:raises cryptography.exceptions.InvalidSignature: Raised when the

docs/hazmat/primitives/asymmetric/ed448.rst

+6-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ Key interfaces
4747

4848
.. method:: sign(data)
4949

50-
:param bytes data: The data to sign.
50+
:param data: The data to sign.
51+
:type data: :term:`bytes-like`
5152

5253
:returns bytes: The 114 byte signature.
5354

@@ -146,9 +147,11 @@ Key interfaces
146147

147148
.. method:: verify(signature, data)
148149

149-
:param bytes signature: The signature to verify.
150+
:param signature: The signature to verify.
151+
:type signature: :term:`bytes-like`
150152

151-
:param bytes data: The data to verify.
153+
:param data: The data to verify.
154+
:type data: :term:`bytes-like`
152155

153156
:returns: None
154157
:raises cryptography.exceptions.InvalidSignature: Raised when the

src/rust/src/backend/dsa.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// for complete details.
44

55
use crate::backend::utils;
6+
use crate::buf::CffiBuf;
67
use crate::error::{CryptographyError, CryptographyResult};
78
use crate::exceptions;
89

@@ -66,10 +67,10 @@ impl DsaPrivateKey {
6667
fn sign<'p>(
6768
&self,
6869
py: pyo3::Python<'p>,
69-
data: &[u8],
70+
data: CffiBuf<'_>,
7071
algorithm: &pyo3::PyAny,
7172
) -> CryptographyResult<&'p pyo3::types::PyBytes> {
72-
let (data, _) = utils::calculate_digest_and_algorithm(py, data, algorithm)?;
73+
let (data, _) = utils::calculate_digest_and_algorithm(py, data.as_bytes(), algorithm)?;
7374

7475
let mut signer = openssl::pkey_ctx::PkeyCtx::new(&self.pkey)?;
7576
signer.sign_init()?;
@@ -151,15 +152,15 @@ impl DsaPublicKey {
151152
fn verify(
152153
&self,
153154
py: pyo3::Python<'_>,
154-
signature: &[u8],
155-
data: &[u8],
155+
signature: CffiBuf<'_>,
156+
data: CffiBuf<'_>,
156157
algorithm: &pyo3::PyAny,
157158
) -> CryptographyResult<()> {
158-
let (data, _) = utils::calculate_digest_and_algorithm(py, data, algorithm)?;
159+
let (data, _) = utils::calculate_digest_and_algorithm(py, data.as_bytes(), algorithm)?;
159160

160161
let mut verifier = openssl::pkey_ctx::PkeyCtx::new(&self.pkey)?;
161162
verifier.verify_init()?;
162-
let valid = verifier.verify(data, signature).unwrap_or(false);
163+
let valid = verifier.verify(data, signature.as_bytes()).unwrap_or(false);
163164
if !valid {
164165
return Err(CryptographyError::from(
165166
exceptions::InvalidSignature::new_err(()),

src/rust/src/backend/ec.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::hash::{Hash, Hasher};
88
use pyo3::ToPyObject;
99

1010
use crate::backend::utils;
11+
use crate::buf::CffiBuf;
1112
use crate::error::{CryptographyError, CryptographyResult};
1213
use crate::{exceptions, types};
1314

@@ -268,7 +269,7 @@ impl ECPrivateKey {
268269
fn sign<'p>(
269270
&self,
270271
py: pyo3::Python<'p>,
271-
data: &[u8],
272+
data: CffiBuf<'_>,
272273
signature_algorithm: &pyo3::PyAny,
273274
) -> CryptographyResult<&'p pyo3::types::PyBytes> {
274275
if !signature_algorithm.is_instance(types::ECDSA.get(py)?)? {
@@ -282,7 +283,7 @@ impl ECPrivateKey {
282283

283284
let (data, _) = utils::calculate_digest_and_algorithm(
284285
py,
285-
data,
286+
data.as_bytes(),
286287
signature_algorithm.getattr(pyo3::intern!(py, "algorithm"))?,
287288
)?;
288289

@@ -366,8 +367,8 @@ impl ECPublicKey {
366367
fn verify(
367368
&self,
368369
py: pyo3::Python<'_>,
369-
signature: &[u8],
370-
data: &[u8],
370+
signature: CffiBuf<'_>,
371+
data: CffiBuf<'_>,
371372
signature_algorithm: &pyo3::PyAny,
372373
) -> CryptographyResult<()> {
373374
if !signature_algorithm.is_instance(types::ECDSA.get(py)?)? {
@@ -381,13 +382,13 @@ impl ECPublicKey {
381382

382383
let (data, _) = utils::calculate_digest_and_algorithm(
383384
py,
384-
data,
385+
data.as_bytes(),
385386
signature_algorithm.getattr(pyo3::intern!(py, "algorithm"))?,
386387
)?;
387388

388389
let mut verifier = openssl::pkey_ctx::PkeyCtx::new(&self.pkey)?;
389390
verifier.verify_init()?;
390-
let valid = verifier.verify(data, signature).unwrap_or(false);
391+
let valid = verifier.verify(data, signature.as_bytes()).unwrap_or(false);
391392
if !valid {
392393
return Err(CryptographyError::from(
393394
exceptions::InvalidSignature::new_err(()),

src/rust/src/backend/ed25519.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ impl Ed25519PrivateKey {
6666
fn sign<'p>(
6767
&self,
6868
py: pyo3::Python<'p>,
69-
data: &[u8],
69+
data: CffiBuf<'_>,
7070
) -> CryptographyResult<&'p pyo3::types::PyBytes> {
7171
let mut signer = openssl::sign::Signer::new_without_digest(&self.pkey)?;
7272
Ok(pyo3::types::PyBytes::new_with(py, signer.len()?, |b| {
7373
let n = signer
74-
.sign_oneshot(b, data)
74+
.sign_oneshot(b, data.as_bytes())
7575
.map_err(CryptographyError::from)?;
7676
assert_eq!(n, b.len());
7777
Ok(())
@@ -118,9 +118,9 @@ impl Ed25519PrivateKey {
118118

119119
#[pyo3::prelude::pymethods]
120120
impl Ed25519PublicKey {
121-
fn verify(&self, signature: &[u8], data: &[u8]) -> CryptographyResult<()> {
121+
fn verify(&self, signature: CffiBuf<'_>, data: CffiBuf<'_>) -> CryptographyResult<()> {
122122
let valid = openssl::sign::Verifier::new_without_digest(&self.pkey)?
123-
.verify_oneshot(signature, data)
123+
.verify_oneshot(signature.as_bytes(), data.as_bytes())
124124
.unwrap_or(false);
125125

126126
if !valid {

src/rust/src/backend/ed448.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ impl Ed448PrivateKey {
6464
fn sign<'p>(
6565
&self,
6666
py: pyo3::Python<'p>,
67-
data: &[u8],
67+
data: CffiBuf<'_>,
6868
) -> CryptographyResult<&'p pyo3::types::PyBytes> {
6969
let mut signer = openssl::sign::Signer::new_without_digest(&self.pkey)?;
7070
Ok(pyo3::types::PyBytes::new_with(py, signer.len()?, |b| {
7171
let n = signer
72-
.sign_oneshot(b, data)
72+
.sign_oneshot(b, data.as_bytes())
7373
.map_err(CryptographyError::from)?;
7474
assert_eq!(n, b.len());
7575
Ok(())
@@ -116,9 +116,9 @@ impl Ed448PrivateKey {
116116

117117
#[pyo3::prelude::pymethods]
118118
impl Ed448PublicKey {
119-
fn verify(&self, signature: &[u8], data: &[u8]) -> CryptographyResult<()> {
119+
fn verify(&self, signature: CffiBuf<'_>, data: CffiBuf<'_>) -> CryptographyResult<()> {
120120
let valid = openssl::sign::Verifier::new_without_digest(&self.pkey)?
121-
.verify_oneshot(signature, data)?;
121+
.verify_oneshot(signature.as_bytes(), data.as_bytes())?;
122122

123123
if !valid {
124124
return Err(CryptographyError::from(

tests/hazmat/primitives/test_dsa.py

+8
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,14 @@ def test_sign(self, backend):
522522
public_key = private_key.public_key()
523523
public_key.verify(signature, message, algorithm)
524524

525+
def test_sign_verify_buffer(self, backend):
526+
private_key = DSA_KEY_1024.private_key(backend)
527+
message = bytearray(b"one little message")
528+
algorithm = hashes.SHA1()
529+
signature = private_key.sign(message, algorithm)
530+
public_key = private_key.public_key()
531+
public_key.verify(bytearray(signature), message, algorithm)
532+
525533
def test_prehashed_sign(self, backend):
526534
private_key = DSA_KEY_1024.private_key(backend)
527535
message = b"one little message"

tests/hazmat/primitives/test_ec.py

+9
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,15 @@ def test_sign(self, backend):
516516
public_key = private_key.public_key()
517517
public_key.verify(signature, message, algorithm)
518518

519+
def test_sign_verify_buffers(self, backend):
520+
_skip_curve_unsupported(backend, ec.SECP256R1())
521+
message = bytearray(b"one little message")
522+
algorithm = ec.ECDSA(hashes.SHA1())
523+
private_key = ec.generate_private_key(ec.SECP256R1(), backend)
524+
signature = private_key.sign(message, algorithm)
525+
public_key = private_key.public_key()
526+
public_key.verify(bytearray(signature), message, algorithm)
527+
519528
def test_sign_prehashed(self, backend):
520529
_skip_curve_unsupported(backend, ec.SECP256R1())
521530
message = b"one little message"

tests/hazmat/primitives/test_ed25519.py

+6
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ def test_invalid_signature(self, backend):
117117
with pytest.raises(InvalidSignature):
118118
key.public_key().verify(b"0" * 64, b"test data")
119119

120+
def test_sign_verify_buffer(self, backend):
121+
key = Ed25519PrivateKey.generate()
122+
data = bytearray(b"test data")
123+
signature = key.sign(data)
124+
key.public_key().verify(bytearray(signature), data)
125+
120126
def test_generate(self, backend):
121127
key = Ed25519PrivateKey.generate()
122128
assert key

tests/hazmat/primitives/test_ed448.py

+6
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ def test_invalid_signature(self, backend):
8686
with pytest.raises(InvalidSignature):
8787
key.public_key().verify(b"0" * 64, b"test data")
8888

89+
def test_sign_verify_buffer(self, backend):
90+
key = Ed448PrivateKey.generate()
91+
data = bytearray(b"test data")
92+
signature = key.sign(data)
93+
key.public_key().verify(bytearray(signature), data)
94+
8995
def test_generate(self, backend):
9096
key = Ed448PrivateKey.generate()
9197
assert key

0 commit comments

Comments
 (0)