From 5256108a2fd708c2180f82cc8a52a4c0d173ca2d Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Sun, 18 Jun 2023 10:49:36 +0800 Subject: [PATCH] ConstantTimeEq and PartialEq for SigningKey --- Cargo.lock | 1 + Cargo.toml | 1 + src/signing.rs | 15 +++++++++++++++ 3 files changed, 17 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index fe13ccc..c025969 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -313,6 +313,7 @@ dependencies = [ "sha2", "sha3", "signature", + "subtle", "toml", "zeroize", ] diff --git a/Cargo.toml b/Cargo.toml index ec28d59..71739e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ curve25519-dalek = { version = "=4.0.0-rc.2", default-features = false, features ed25519 = { version = ">=2.2, <2.3", default-features = false } signature = { version = ">=2.0, <2.1", optional = true, default-features = false } sha2 = { version = "0.10", default-features = false } +subtle = { version = "2.3.0", default-features = false } # optional features merlin = { version = "3", default-features = false, optional = true } diff --git a/src/signing.rs b/src/signing.rs index b0f0b49..5af8675 100644 --- a/src/signing.rs +++ b/src/signing.rs @@ -19,6 +19,7 @@ use rand_core::CryptoRngCore; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use sha2::Sha512; +use subtle::{ConstantTimeEq, Choice}; use curve25519_dalek::{ digest::{generic_array::typenum::U64, Digest}, @@ -583,6 +584,20 @@ impl TryFrom<&[u8]> for SigningKey { } } +impl ConstantTimeEq for SigningKey { + fn ct_eq(&self, other: &Self) -> Choice { + self.secret_key.ct_eq(&other.secret_key) + } +} + +impl PartialEq for SigningKey { + fn eq(&self, other: &Self) -> bool { + self.ct_eq(other).into() + } +} + +impl Eq for SigningKey {} + #[cfg(feature = "zeroize")] impl Drop for SigningKey { fn drop(&mut self) {