Skip to content

Commit c6206fd

Browse files
committed
feat: switch to version 2.0 (pre) of the signature crate
Rework the crate to implement traits from the preview of the signature crate. Use Vec<u8> as Self::Repr type. Signed-off-by: Dmitry Baryshkov <[email protected]>
1 parent a857c8f commit c6206fd

File tree

4 files changed

+91
-97
lines changed

4 files changed

+91
-97
lines changed

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ subtle = { version = "2.1.1", default-features = false }
2323
digest = { version = "0.10.5", default-features = false, features = ["alloc", "oid"] }
2424
pkcs1 = { version = "0.4", default-features = false, features = ["pkcs8", "alloc"] }
2525
pkcs8 = { version = "0.9", default-features = false, features = ["alloc"] }
26-
signature = { version = "1.6.4", default-features = false , features = ["digest-preview", "rand-preview"] }
26+
signature = { version = "2.0.0-pre", default-features = false , features = ["digest-preview", "rand-preview"] }
2727
zeroize = { version = "1", features = ["alloc"] }
2828

2929
# Temporary workaround until https://github.com/dignifiedquire/num-bigint/pull/42 lands
@@ -53,7 +53,7 @@ name = "key"
5353

5454
[features]
5555
default = ["std", "pem"]
56-
hazmat = ["signature/hazmat-preview"]
56+
hazmat = []
5757
nightly = ["num-bigint/nightly"]
5858
serde = ["num-bigint/serde", "serde_crate"]
5959
expose-internals = []
@@ -68,3 +68,6 @@ rustdoc-args = ["--cfg", "docsrs"]
6868

6969
[profile.dev]
7070
opt-level = 2
71+
72+
[patch.crates-io]
73+
signature = { git = "https://github.com/RustCrypto/traits" }

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
//! use rsa::RsaPrivateKey;
5353
//! use rsa::pkcs1v15::{SigningKey, VerifyingKey};
5454
//! use sha2::{Digest, Sha256};
55-
//! use signature::{RandomizedSigner, Signature, Verifier};
55+
//! use signature::{RandomizedSigner, SignatureEncoding, Verifier};
5656
//!
5757
//! let mut rng = rand::thread_rng();
5858
//!
@@ -64,7 +64,7 @@
6464
//! // Sign
6565
//! let data = b"hello world";
6666
//! let signature = signing_key.sign_with_rng(&mut rng, data);
67-
//! assert_ne!(signature.as_bytes(), data);
67+
//! assert_ne!(signature.to_bytes().as_ref(), data.as_slice());
6868
//!
6969
//! // Verify
7070
//! verifying_key.verify(data, &signature).expect("failed to verify");
@@ -75,7 +75,7 @@
7575
//! use rsa::RsaPrivateKey;
7676
//! use rsa::pss::{BlindedSigningKey, VerifyingKey};
7777
//! use sha2::{Digest, Sha256};
78-
//! use signature::{RandomizedSigner, Signature, Verifier};
78+
//! use signature::{RandomizedSigner, SignatureEncoding, Verifier};
7979
//!
8080
//! let mut rng = rand::thread_rng();
8181
//!
@@ -87,7 +87,7 @@
8787
//! // Sign
8888
//! let data = b"hello world";
8989
//! let signature = signing_key.sign_with_rng(&mut rng, data);
90-
//! assert_ne!(signature.as_bytes(), data);
90+
//! assert_ne!(signature.to_bytes().as_ref(), data);
9191
//!
9292
//! // Verify
9393
//! verifying_key.verify(data, &signature).expect("failed to verify");

src/pkcs1v15.rs

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
use alloc::vec;
1+
use alloc::boxed::Box;
22
use alloc::vec::Vec;
33
use core::fmt::{Debug, Display, Formatter, LowerHex, UpperHex};
44
use core::marker::PhantomData;
5-
use core::ops::Deref;
65
use digest::Digest;
76
use pkcs8::{AssociatedOid, Document, EncodePrivateKey, EncodePublicKey, SecretDocument};
87
use rand_core::{CryptoRng, RngCore};
98
#[cfg(feature = "hazmat")]
109
use signature::hazmat::{PrehashSigner, PrehashVerifier};
1110
use signature::{
12-
DigestSigner, DigestVerifier, RandomizedDigestSigner, RandomizedSigner,
13-
Signature as SignSignature, Signer, Verifier,
11+
DigestSigner, DigestVerifier, RandomizedDigestSigner, RandomizedSigner, SignatureEncoding,
12+
Signer, Verifier,
1413
};
1514
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
1615
use zeroize::Zeroizing;
@@ -20,60 +19,52 @@ use crate::errors::{Error, Result};
2019
use crate::key::{self, PrivateKey, PublicKey};
2120
use crate::{RsaPrivateKey, RsaPublicKey};
2221

23-
#[derive(Clone)]
22+
#[derive(Clone, PartialEq, Eq)]
2423
pub struct Signature {
25-
bytes: Vec<u8>,
24+
bytes: Box<[u8]>,
2625
}
2726

28-
impl signature::Signature for Signature {
29-
fn from_bytes(bytes: &[u8]) -> signature::Result<Self> {
30-
Ok(Signature {
31-
bytes: bytes.into(),
32-
})
33-
}
34-
35-
fn as_bytes(&self) -> &[u8] {
36-
self.bytes.as_slice()
37-
}
27+
impl SignatureEncoding for Signature {
28+
type Repr = Box<[u8]>;
3829
}
3930

40-
impl From<Vec<u8>> for Signature {
41-
fn from(bytes: Vec<u8>) -> Self {
31+
impl From<Box<[u8]>> for Signature {
32+
fn from(bytes: Box<[u8]>) -> Self {
4233
Self { bytes }
4334
}
4435
}
4536

46-
impl Deref for Signature {
47-
type Target = [u8];
37+
impl<'a> TryFrom<&'a [u8]> for Signature {
38+
type Error = signature::Error;
4839

49-
fn deref(&self) -> &Self::Target {
50-
self.as_bytes()
40+
fn try_from(bytes: &'a [u8]) -> signature::Result<Self> {
41+
Ok(Self {
42+
bytes: bytes.into(),
43+
})
5144
}
5245
}
5346

54-
impl PartialEq for Signature {
55-
fn eq(&self, other: &Self) -> bool {
56-
self.as_bytes() == other.as_bytes()
47+
impl From<Signature> for Box<[u8]> {
48+
fn from(signature: Signature) -> Box<[u8]> {
49+
signature.bytes
5750
}
5851
}
5952

60-
impl Eq for Signature {}
61-
62-
impl Debug for Signature {
63-
fn fmt(&self, fmt: &mut Formatter<'_>) -> core::result::Result<(), core::fmt::Error> {
64-
fmt.debug_list().entries(self.as_bytes().iter()).finish()
53+
impl AsRef<[u8]> for Signature {
54+
fn as_ref(&self) -> &[u8] {
55+
self.bytes.as_ref()
6556
}
6657
}
6758

68-
impl AsRef<[u8]> for Signature {
69-
fn as_ref(&self) -> &[u8] {
70-
self.as_bytes()
59+
impl Debug for Signature {
60+
fn fmt(&self, fmt: &mut Formatter<'_>) -> core::result::Result<(), core::fmt::Error> {
61+
fmt.debug_list().entries(self.bytes.iter()).finish()
7162
}
7263
}
7364

7465
impl LowerHex for Signature {
7566
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
76-
for byte in self.as_bytes() {
67+
for byte in self.bytes.iter() {
7768
write!(f, "{:02x}", byte)?;
7869
}
7970
Ok(())
@@ -82,7 +73,7 @@ impl LowerHex for Signature {
8273

8374
impl UpperHex for Signature {
8475
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
85-
for byte in self.as_bytes() {
76+
for byte in self.bytes.iter() {
8677
write!(f, "{:02X}", byte)?;
8778
}
8879
Ok(())
@@ -391,7 +382,7 @@ where
391382
{
392383
fn try_sign(&self, msg: &[u8]) -> signature::Result<Signature> {
393384
sign::<DummyRng, _>(None, &self.inner, &self.prefix, &D::digest(msg))
394-
.map(|v| v.into())
385+
.map(|v| v.into_boxed_slice().into())
395386
.map_err(|e| e.into())
396387
}
397388
}
@@ -406,7 +397,7 @@ where
406397
msg: &[u8],
407398
) -> signature::Result<Signature> {
408399
sign(Some(&mut rng), &self.inner, &self.prefix, &D::digest(msg))
409-
.map(|v| v.into())
400+
.map(|v| v.into_boxed_slice().into())
410401
.map_err(|e| e.into())
411402
}
412403
}
@@ -417,7 +408,7 @@ where
417408
{
418409
fn try_sign_digest(&self, digest: D) -> signature::Result<Signature> {
419410
sign::<DummyRng, _>(None, &self.inner, &self.prefix, &digest.finalize())
420-
.map(|v| v.into())
411+
.map(|v| v.into_boxed_slice().into())
421412
.map_err(|e| e.into())
422413
}
423414
}
@@ -437,7 +428,7 @@ where
437428
&self.prefix,
438429
&digest.finalize(),
439430
)
440-
.map(|v| v.into())
431+
.map(|v| v.into_boxed_slice().into())
441432
.map_err(|e| e.into())
442433
}
443434
}
@@ -449,7 +440,7 @@ where
449440
{
450441
fn sign_prehash(&self, prehash: &[u8]) -> signature::Result<Signature> {
451442
sign::<DummyRng, _>(None, &self.inner, &self.prefix, prehash)
452-
.map(|v| v.into())
443+
.map(|v| v.into_boxed_slice().into())
453444
.map_err(|e| e.into())
454445
}
455446
}
@@ -604,7 +595,7 @@ mod tests {
604595
use sha1::{Digest, Sha1};
605596
use sha2::Sha256;
606597
use sha3::Sha3_256;
607-
use signature::{RandomizedSigner, Signature, Signer, Verifier};
598+
use signature::{RandomizedSigner, Signer, Verifier};
608599

609600
use crate::{PaddingScheme, PublicKey, PublicKeyParts, RsaPrivateKey, RsaPublicKey};
610601

@@ -898,8 +889,10 @@ mod tests {
898889
let verifying_key = VerifyingKey::<Sha1>::new_with_prefix(pub_key);
899890

900891
for (text, sig, expected) in &tests {
901-
let result =
902-
verifying_key.verify(text.as_bytes(), &Signature::from_bytes(sig).unwrap());
892+
let result = verifying_key.verify(
893+
text.as_bytes(),
894+
&Signature::try_from(sig.as_slice()).unwrap(),
895+
);
903896
match expected {
904897
true => result.expect("failed to verify"),
905898
false => {
@@ -937,7 +930,8 @@ mod tests {
937930
for (text, sig, expected) in &tests {
938931
let mut digest = Sha1::new();
939932
digest.update(text.as_bytes());
940-
let result = verifying_key.verify_digest(digest, &Signature::from_bytes(sig).unwrap());
933+
let result =
934+
verifying_key.verify_digest(digest, &Signature::try_from(sig.as_slice()).unwrap());
941935
match expected {
942936
true => result.expect("failed to verify"),
943937
false => {
@@ -976,7 +970,7 @@ mod tests {
976970

977971
let verifying_key: VerifyingKey<_> = (&signing_key).into();
978972
verifying_key
979-
.verify_prehash(msg, &Signature::from_bytes(&expected_sig).unwrap())
973+
.verify_prehash(msg, &Signature::from_bytes(&expected_sig.into_boxed_slice()).unwrap())
980974
.expect("failed to verify");
981975
}
982976
}

0 commit comments

Comments
 (0)