Skip to content

Commit 1110042

Browse files
committed
Add a couple Eq / PartialEq traits
Be explicit for SecretKey since this is not obvious.
1 parent 4bf2a8a commit 1110042

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ crypto-bigint = "0.7.0-rc.22"
1818
digest = "0.11.0-rc.9"
1919
hmac-sha256 = { version = "1.1.13", features = ["traits011"] }
2020
hmac-sha512 = { version = "1.1.8", features = ["traits011", "sha384"] }
21+
ct-codecs = "1"
2122
derive-new = "0.7.0"
2223
derive_more = { version = "2.1.1", features = ["full"] }
2324
serde = { version = "1", features = ["derive"], optional = true }

src/lib.rs

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ pub trait HashAlgorithm: Clone + Default {
156156
}
157157

158158
/// SHA-256 hash algorithm
159-
#[derive(Clone, Copy, Debug, Default)]
159+
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
160160
pub struct Sha256;
161161

162162
impl HashAlgorithm for Sha256 {
@@ -180,7 +180,7 @@ impl HashAlgorithm for Sha256 {
180180
}
181181

182182
/// SHA-384 hash algorithm
183-
#[derive(Clone, Copy, Debug, Default)]
183+
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
184184
pub struct Sha384;
185185

186186
impl HashAlgorithm for Sha384 {
@@ -204,7 +204,7 @@ impl HashAlgorithm for Sha384 {
204204
}
205205

206206
/// SHA-512 hash algorithm
207-
#[derive(Clone, Copy, Debug, Default)]
207+
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
208208
pub struct Sha512;
209209

210210
impl HashAlgorithm for Sha512 {
@@ -244,7 +244,7 @@ pub trait SaltMode: Clone + Default + private::Sealed {
244244
}
245245

246246
/// PSS mode with salt (salt length = hash output length)
247-
#[derive(Clone, Copy, Debug, Default)]
247+
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
248248
pub struct PSS;
249249

250250
impl private::Sealed for PSS {}
@@ -254,7 +254,7 @@ impl SaltMode for PSS {
254254
}
255255

256256
/// PSS mode without salt (salt length = 0)
257-
#[derive(Clone, Copy, Debug, Default)]
257+
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
258258
pub struct PSSZero;
259259

260260
impl private::Sealed for PSSZero {}
@@ -271,7 +271,7 @@ pub trait MessagePrepare: Clone + Default + private::Sealed {
271271
}
272272

273273
/// Randomized message preparation
274-
#[derive(Clone, Copy, Debug, Default)]
274+
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
275275
pub struct Randomized;
276276

277277
impl private::Sealed for Randomized {}
@@ -281,7 +281,7 @@ impl MessagePrepare for Randomized {
281281
}
282282

283283
/// Deterministic message preparation
284-
#[derive(Clone, Copy, Debug, Default)]
284+
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
285285
pub struct Deterministic;
286286

287287
impl private::Sealed for Deterministic {}
@@ -317,24 +317,32 @@ impl TryCryptoRng for DefaultRng {}
317317
#[derive(Clone, Debug, AsRef, Deref, From, Into, new)]
318318
pub struct Secret(pub Vec<u8>);
319319

320+
impl Eq for Secret {}
321+
322+
impl PartialEq for Secret {
323+
fn eq(&self, other: &Self) -> bool {
324+
ct_codecs::verify(&self.0, &other.0)
325+
}
326+
}
327+
320328
/// A blind message
321329
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
322-
#[derive(Clone, Debug, AsRef, Deref, From, Into, new)]
330+
#[derive(Clone, Debug, Eq, PartialEq, AsRef, Deref, From, Into, new)]
323331
pub struct BlindMessage(pub Vec<u8>);
324332

325333
/// A blind signature
326334
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
327-
#[derive(Clone, Debug, AsRef, Deref, From, Into, new)]
335+
#[derive(Clone, Debug, Eq, PartialEq, AsRef, Deref, From, Into, new)]
328336
pub struct BlindSignature(pub Vec<u8>);
329337

330338
/// A (non-blind) signature
331339
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
332-
#[derive(Clone, Debug, AsRef, Deref, From, Into, new)]
340+
#[derive(Clone, Debug, Eq, PartialEq, AsRef, Deref, From, Into, new)]
333341
pub struct Signature(pub Vec<u8>);
334342

335343
/// A message randomizer (noise added as a prefix to the message)
336344
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
337-
#[derive(Clone, Copy, Debug, AsRef, Deref, From, Into, new)]
345+
#[derive(Clone, Copy, Debug, Eq, PartialEq, AsRef, Deref, From, Into, new)]
338346
pub struct MessageRandomizer(pub [u8; 32]);
339347

340348
/// Result of a blinding operation
@@ -345,6 +353,16 @@ pub struct BlindingResult {
345353
pub msg_randomizer: Option<MessageRandomizer>,
346354
}
347355

356+
impl Eq for BlindingResult {}
357+
358+
impl PartialEq for BlindingResult {
359+
fn eq(&self, other: &Self) -> bool {
360+
self.blind_message == other.blind_message
361+
&& self.secret == other.secret
362+
&& self.msg_randomizer == other.msg_randomizer
363+
}
364+
}
365+
348366
impl AsRef<[u8]> for Secret {
349367
fn as_ref(&self) -> &[u8] {
350368
self.0.as_slice()
@@ -529,7 +547,7 @@ fn emsa_pss_encode(
529547
///
530548
/// type BRsa = BlindRsa<Sha384, PSS, Randomized>;
531549
/// ```
532-
#[derive(Clone, Copy, Debug, Default)]
550+
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
533551
pub struct BlindRsa<H: HashAlgorithm, S: SaltMode, M: MessagePrepare> {
534552
_phantom: PhantomData<(H, S, M)>,
535553
}
@@ -854,8 +872,18 @@ impl<H: HashAlgorithm, S: SaltMode, M: MessagePrepare> AsRef<RsaPrivateKey> for
854872
}
855873
}
856874

875+
impl<H: HashAlgorithm, S: SaltMode, M: MessagePrepare> Eq for SecretKey<H, S, M> {}
876+
877+
impl<H: HashAlgorithm, S: SaltMode, M: MessagePrepare> PartialEq for SecretKey<H, S, M> {
878+
fn eq(&self, other: &Self) -> bool {
879+
// RsaPrivateKey currently uses BoxedUint internally, which implements
880+
// constant-time comparison when the moduli are the same.
881+
self.inner == other.inner
882+
}
883+
}
884+
857885
/// An RSA key pair
858-
#[derive(Clone, Debug)]
886+
#[derive(Clone, Debug, Eq, PartialEq)]
859887
pub struct KeyPair<H: HashAlgorithm, S: SaltMode, M: MessagePrepare> {
860888
pub pk: PublicKey<H, S, M>,
861889
pub sk: SecretKey<H, S, M>,

0 commit comments

Comments
 (0)