Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Replace Deref impl on RsaPrivateKey with AsRef #317

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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