Skip to content

Commit

Permalink
Replace Deref impl on RsaPrivateKey with AsRef (#317)
Browse files Browse the repository at this point in the history
The `RsaPrivateKey` type previously had a `Deref` impl providing access
to the associated `RsaPublicKey`.

`Deref` is intended for "smart pointer types", i.e. container types
which manage a (typically generic) inner type in some way. This doesn't
seem like one of those cases.

`AsRef`, on the other hand, is for cheap reference conversions, which is
exactly what's happening here, so it's a better fit and provides the
same functionality (albeit explicitly rather than via deref coercion).
  • Loading branch information
tarcieri authored Apr 27, 2023
1 parent db2559f commit ec49956
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
31 changes: 13 additions & 18 deletions src/key.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use alloc::vec::Vec;
use core::{
hash::{Hash, Hasher},
ops::Deref,
};
use core::hash::{Hash, Hasher};
use num_bigint::traits::ModInverse;
use num_bigint::Sign::Plus;
use num_bigint::{BigInt, BigUint};
Expand Down Expand Up @@ -57,6 +54,12 @@ impl PartialEq for RsaPrivateKey {
}
}

impl AsRef<RsaPublicKey> for RsaPrivateKey {
fn as_ref(&self) -> &RsaPublicKey {
&self.pubkey_components
}
}

impl Hash for RsaPrivateKey {
fn hash<H: Hasher>(&self, state: &mut H) {
// Domain separator for RSA private keys
Expand All @@ -73,13 +76,6 @@ impl Drop for RsaPrivateKey {
}
}

impl Deref for RsaPrivateKey {
type Target = RsaPublicKey;
fn deref(&self) -> &RsaPublicKey {
&self.pubkey_components
}
}

impl ZeroizeOnDrop for RsaPrivateKey {}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -124,9 +120,8 @@ impl From<RsaPrivateKey> for RsaPublicKey {

impl From<&RsaPrivateKey> for RsaPublicKey {
fn from(private_key: &RsaPrivateKey) -> Self {
let n = private_key.n.clone();
let e = private_key.e.clone();

let n = private_key.n().clone();
let e = private_key.e().clone();
RsaPublicKey { n, e }
}
}
Expand Down Expand Up @@ -201,11 +196,11 @@ impl RsaPublicKey {

impl PublicKeyParts for RsaPrivateKey {
fn n(&self) -> &BigUint {
&self.n
&self.pubkey_components.n
}

fn e(&self) -> &BigUint {
&self.e
&self.pubkey_components.e
}
}

Expand Down Expand Up @@ -336,7 +331,7 @@ impl RsaPrivateKey {
}
m *= prime;
}
if m != self.n {
if m != self.pubkey_components.n {
return Err(Error::InvalidModulus);
}

Expand All @@ -345,7 +340,7 @@ impl RsaPrivateKey {
// inverse. Therefore e is coprime to lcm(p-1,q-1,r-1,...) =
// exponent(ℤ/nℤ). It also implies that a^de ≡ a mod p as a^(p-1) ≡ 1
// mod p. Thus a^de ≡ a mod n for all a coprime to n, as required.
let mut de = self.e.clone();
let mut de = self.e().clone();
de *= self.d.clone();
for prime in &self.primes {
let congruence: BigUint = &de % (prime - BigUint::one());
Expand Down
3 changes: 3 additions & 0 deletions src/pss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ mod test {
.expect("failed to sign");

priv_key
.to_public_key()
.verify(Pss::new::<Sha1>(), &digest, &sig)
.expect("failed to verify");
}
Expand All @@ -424,6 +425,7 @@ mod test {
.expect("failed to sign");

priv_key
.to_public_key()
.verify(Pss::new::<Sha1>(), &digest, &sig)
.expect("failed to verify");
}
Expand Down Expand Up @@ -595,6 +597,7 @@ mod test {
.expect("failed to sign");

priv_key
.to_public_key()
.verify(Pss::new::<Sha1>(), &digest, &sig)
.expect("failed to verify");
}
Expand Down

0 comments on commit ec49956

Please sign in to comment.