Skip to content

Commit 2ffd3ae

Browse files
authored
PaddingScheme: remove rng from PSS padding scheme (#172) (#173)
The passed rng is not necessary for PSS signature verification. Instead of passing artificial unused RNG through the PaddingScheme, add new sign_with_rng() API and pass rng directly. In the sign_blinded() use the passed rng both for salt generation and for the blinding process. Signed-off-by: Dmitry Baryshkov <[email protected]>
1 parent 40242fb commit 2ffd3ae

File tree

3 files changed

+43
-55
lines changed

3 files changed

+43
-55
lines changed

src/key.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -512,18 +512,24 @@ impl RsaPrivateKey {
512512
PaddingScheme::PKCS1v15Sign { ref hash } => {
513513
pkcs1v15::sign::<DummyRng, _>(None, self, hash.as_ref(), digest_in)
514514
}
515+
_ => Err(Error::InvalidPaddingScheme),
516+
}
517+
}
518+
519+
/// Sign the given digest using the provided rng
520+
///
521+
/// Use `rng` for signature process.
522+
pub fn sign_with_rng<R: RngCore + CryptoRng>(
523+
&self,
524+
rng: &mut R,
525+
padding: PaddingScheme,
526+
digest_in: &[u8],
527+
) -> Result<Vec<u8>> {
528+
match padding {
515529
PaddingScheme::PSS {
516-
mut salt_rng,
517530
mut digest,
518531
salt_len,
519-
} => pss::sign::<_, DummyRng, _>(
520-
&mut *salt_rng,
521-
None,
522-
self,
523-
digest_in,
524-
salt_len,
525-
&mut *digest,
526-
),
532+
} => pss::sign::<R, _>(rng, false, self, digest_in, salt_len, &mut *digest),
527533
_ => Err(Error::InvalidPaddingScheme),
528534
}
529535
}
@@ -542,17 +548,9 @@ impl RsaPrivateKey {
542548
pkcs1v15::sign(Some(rng), self, hash.as_ref(), digest_in)
543549
}
544550
PaddingScheme::PSS {
545-
mut salt_rng,
546551
mut digest,
547552
salt_len,
548-
} => pss::sign::<_, R, _>(
549-
&mut *salt_rng,
550-
Some(rng),
551-
self,
552-
digest_in,
553-
salt_len,
554-
&mut *digest,
555-
),
553+
} => pss::sign::<R, _>(rng, true, self, digest_in, salt_len, &mut *digest),
556554
_ => Err(Error::InvalidPaddingScheme),
557555
}
558556
}

src/padding.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use alloc::string::{String, ToString};
33
use core::fmt;
44

55
use digest::{Digest, DynDigest};
6-
use rand_core::RngCore;
76

87
use crate::hash::Hash;
98

@@ -30,7 +29,6 @@ pub enum PaddingScheme {
3029
},
3130
/// Sign and Verify using PSS padding.
3231
PSS {
33-
salt_rng: Box<dyn RngCore>,
3432
digest: Box<dyn DynDigest>,
3533
salt_len: Option<usize>,
3634
},
@@ -142,20 +140,15 @@ impl PaddingScheme {
142140
}
143141
}
144142

145-
pub fn new_pss<T: 'static + Digest + DynDigest, S: 'static + RngCore>(rng: S) -> Self {
143+
pub fn new_pss<T: 'static + Digest + DynDigest>() -> Self {
146144
PaddingScheme::PSS {
147-
salt_rng: Box::new(rng),
148145
digest: Box::new(T::new()),
149146
salt_len: None,
150147
}
151148
}
152149

153-
pub fn new_pss_with_salt<T: 'static + Digest + DynDigest, S: 'static + RngCore>(
154-
rng: S,
155-
len: usize,
156-
) -> Self {
150+
pub fn new_pss_with_salt<T: 'static + Digest + DynDigest>(len: usize) -> Self {
157151
PaddingScheme::PSS {
158-
salt_rng: Box::new(rng),
159152
digest: Box::new(T::new()),
160153
salt_len: Some(len),
161154
}

src/pss.rs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -102,20 +102,7 @@ pub(crate) fn verify<PK: PublicKey>(
102102
/// given hash function. The opts argument may be nil, in which case sensible
103103
/// defaults are used.
104104
// TODO: bind T with the CryptoRng trait
105-
pub(crate) fn sign<T: RngCore + ?Sized, S: CryptoRng + RngCore, SK: PrivateKey>(
106-
rng: &mut T,
107-
blind_rng: Option<&mut S>,
108-
priv_key: &SK,
109-
hashed: &[u8],
110-
salt_len: Option<usize>,
111-
digest: &mut dyn DynDigest,
112-
) -> Result<Vec<u8>> {
113-
let salt = generate_salt(rng, priv_key, salt_len, digest);
114-
115-
sign_pss_with_salt(blind_rng, priv_key, hashed, &salt, digest)
116-
}
117-
118-
fn sign_int<T: RngCore + CryptoRng, SK: PrivateKey>(
105+
pub(crate) fn sign<T: RngCore + CryptoRng, SK: PrivateKey>(
119106
rng: &mut T,
120107
blind: bool,
121108
priv_key: &SK,
@@ -360,7 +347,7 @@ impl RandomizedSigner<Signature> for SigningKey {
360347
mut rng: impl CryptoRng + RngCore,
361348
digest: &[u8],
362349
) -> signature::Result<Signature> {
363-
sign_int(
350+
sign(
364351
&mut rng,
365352
false,
366353
&self.inner,
@@ -403,7 +390,7 @@ impl RandomizedSigner<Signature> for BlindedSigningKey {
403390
mut rng: impl CryptoRng + RngCore,
404391
digest: &[u8],
405392
) -> signature::Result<Signature> {
406-
sign_int(
393+
sign(
407394
&mut rng,
408395
true,
409396
&self.inner,
@@ -540,8 +527,7 @@ mod test {
540527

541528
for (text, sig, expected) in &tests {
542529
let digest = Sha1::digest(text.as_bytes()).to_vec();
543-
let rng = ChaCha8Rng::from_seed([42; 32]);
544-
let result = pub_key.verify(PaddingScheme::new_pss::<Sha1, _>(rng), &digest, sig);
530+
let result = pub_key.verify(PaddingScheme::new_pss::<Sha1>(), &digest, sig);
545531
match expected {
546532
true => result.expect("failed to verify"),
547533
false => {
@@ -600,19 +586,30 @@ mod test {
600586
for test in &tests {
601587
let digest = Sha1::digest(test.as_bytes()).to_vec();
602588
let sig = priv_key
603-
.sign_blinded(
604-
&mut rng.clone(),
605-
PaddingScheme::new_pss::<Sha1, _>(rng.clone()),
606-
&digest,
607-
)
589+
.sign_with_rng(&mut rng.clone(), PaddingScheme::new_pss::<Sha1>(), &digest)
590+
.expect("failed to sign");
591+
592+
priv_key
593+
.verify(PaddingScheme::new_pss::<Sha1>(), &digest, &sig)
594+
.expect("failed to verify");
595+
}
596+
}
597+
598+
#[test]
599+
fn test_sign_blinded_and_verify_roundtrip() {
600+
let priv_key = get_private_key();
601+
602+
let tests = ["test\n"];
603+
let rng = ChaCha8Rng::from_seed([42; 32]);
604+
605+
for test in &tests {
606+
let digest = Sha1::digest(test.as_bytes()).to_vec();
607+
let sig = priv_key
608+
.sign_blinded(&mut rng.clone(), PaddingScheme::new_pss::<Sha1>(), &digest)
608609
.expect("failed to sign");
609610

610611
priv_key
611-
.verify(
612-
PaddingScheme::new_pss::<Sha1, _>(rng.clone()),
613-
&digest,
614-
&sig,
615-
)
612+
.verify(PaddingScheme::new_pss::<Sha1>(), &digest, &sig)
616613
.expect("failed to verify");
617614
}
618615
}

0 commit comments

Comments
 (0)