diff --git a/Cargo.toml b/Cargo.toml
index 8be02791..a69edc6f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -24,6 +24,7 @@ rand_core = { version = "0.6" , default-features = false}
serde = { version = "1.0", optional = true }
sha3 = { version = "0.10", default-features = false }
snafu = { version = "0.7", default-features = false}
+subtle = { verion = "2.5.0", default-features = false }
zeroize = {version = "1" , default-features = false}
[dev-dependencies]
@@ -48,6 +49,7 @@ std = [
"serde?/std",
"sha3/std",
"snafu/std",
+ "subtle/std",
"tari_utilities/std",
"zeroize/std",
]
@@ -61,4 +63,4 @@ crate-type = ["lib", "cdylib"]
[[bench]]
name = "benches"
path = "benches/mod.rs"
-harness = false
\ No newline at end of file
+harness = false
diff --git a/src/dhke.rs b/src/dhke.rs
index 41c08c93..73d7efa6 100644
--- a/src/dhke.rs
+++ b/src/dhke.rs
@@ -18,11 +18,11 @@ use crate::keys::PublicKey;
/// The result of a Diffie-Hellman key exchange
#[derive(Zeroize, ZeroizeOnDrop)]
pub struct DiffieHellmanSharedSecret
(P)
-where P: Zeroize;
+where P: PublicKey;
impl
DiffieHellmanSharedSecret
where
- P: PublicKey + Zeroize,
+ P: PublicKey,
for<'a> &'a
::K: Mul<&'a P, Output = P>,
{
/// Perform a Diffie-Hellman key exchange
@@ -36,6 +36,20 @@ where
}
}
+impl
Eq for DiffieHellmanSharedSecret
+where
+ P: PublicKey,
+{}
+
+impl
PartialEq for DiffieHellmanSharedSecret
+where
+ P: PublicKey,
+{
+ fn eq(&self, other: &DiffieHellmanSharedSecret
) -> bool {
+ self.0.ct_eq(&other.0).into()
+ }
+}
+
#[cfg(test)]
mod test {
use rand_core::OsRng;
diff --git a/src/keys.rs b/src/keys.rs
index b1692f26..aecdc942 100644
--- a/src/keys.rs
+++ b/src/keys.rs
@@ -9,6 +9,7 @@
use core::ops::Add;
use rand_core::{CryptoRng, RngCore};
+use subtle::ConstantTimeEq;
use tari_utilities::{ByteArray, ByteArrayError};
use zeroize::{Zeroize, ZeroizeOnDrop};
@@ -27,7 +28,7 @@ use zeroize::{Zeroize, ZeroizeOnDrop};
/// let p = RistrettoPublicKey::from_secret_key(&k);
/// ```
pub trait SecretKey:
- ByteArray + Clone + PartialEq + Eq + Add