Skip to content

Commit ec49956

Browse files
authored
Replace Deref impl on RsaPrivateKey with AsRef (#317)
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).
1 parent db2559f commit ec49956

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

src/key.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use alloc::vec::Vec;
2-
use core::{
3-
hash::{Hash, Hasher},
4-
ops::Deref,
5-
};
2+
use core::hash::{Hash, Hasher};
63
use num_bigint::traits::ModInverse;
74
use num_bigint::Sign::Plus;
85
use num_bigint::{BigInt, BigUint};
@@ -57,6 +54,12 @@ impl PartialEq for RsaPrivateKey {
5754
}
5855
}
5956

57+
impl AsRef<RsaPublicKey> for RsaPrivateKey {
58+
fn as_ref(&self) -> &RsaPublicKey {
59+
&self.pubkey_components
60+
}
61+
}
62+
6063
impl Hash for RsaPrivateKey {
6164
fn hash<H: Hasher>(&self, state: &mut H) {
6265
// Domain separator for RSA private keys
@@ -73,13 +76,6 @@ impl Drop for RsaPrivateKey {
7376
}
7477
}
7578

76-
impl Deref for RsaPrivateKey {
77-
type Target = RsaPublicKey;
78-
fn deref(&self) -> &RsaPublicKey {
79-
&self.pubkey_components
80-
}
81-
}
82-
8379
impl ZeroizeOnDrop for RsaPrivateKey {}
8480

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

125121
impl From<&RsaPrivateKey> for RsaPublicKey {
126122
fn from(private_key: &RsaPrivateKey) -> Self {
127-
let n = private_key.n.clone();
128-
let e = private_key.e.clone();
129-
123+
let n = private_key.n().clone();
124+
let e = private_key.e().clone();
130125
RsaPublicKey { n, e }
131126
}
132127
}
@@ -201,11 +196,11 @@ impl RsaPublicKey {
201196

202197
impl PublicKeyParts for RsaPrivateKey {
203198
fn n(&self) -> &BigUint {
204-
&self.n
199+
&self.pubkey_components.n
205200
}
206201

207202
fn e(&self) -> &BigUint {
208-
&self.e
203+
&self.pubkey_components.e
209204
}
210205
}
211206

@@ -336,7 +331,7 @@ impl RsaPrivateKey {
336331
}
337332
m *= prime;
338333
}
339-
if m != self.n {
334+
if m != self.pubkey_components.n {
340335
return Err(Error::InvalidModulus);
341336
}
342337

@@ -345,7 +340,7 @@ impl RsaPrivateKey {
345340
// inverse. Therefore e is coprime to lcm(p-1,q-1,r-1,...) =
346341
// exponent(ℤ/nℤ). It also implies that a^de ≡ a mod p as a^(p-1) ≡ 1
347342
// mod p. Thus a^de ≡ a mod n for all a coprime to n, as required.
348-
let mut de = self.e.clone();
343+
let mut de = self.e().clone();
349344
de *= self.d.clone();
350345
for prime in &self.primes {
351346
let congruence: BigUint = &de % (prime - BigUint::one());

src/pss.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ mod test {
405405
.expect("failed to sign");
406406

407407
priv_key
408+
.to_public_key()
408409
.verify(Pss::new::<Sha1>(), &digest, &sig)
409410
.expect("failed to verify");
410411
}
@@ -424,6 +425,7 @@ mod test {
424425
.expect("failed to sign");
425426

426427
priv_key
428+
.to_public_key()
427429
.verify(Pss::new::<Sha1>(), &digest, &sig)
428430
.expect("failed to verify");
429431
}
@@ -595,6 +597,7 @@ mod test {
595597
.expect("failed to sign");
596598

597599
priv_key
600+
.to_public_key()
598601
.verify(Pss::new::<Sha1>(), &digest, &sig)
599602
.expect("failed to verify");
600603
}

0 commit comments

Comments
 (0)