diff --git a/Cargo.lock b/Cargo.lock index 992afe27a..3fb9e6f81 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1451,6 +1451,7 @@ name = "sec1" version = "0.8.0-rc.10" dependencies = [ "base16ct 1.0.0", + "ctutils", "der", "hex-literal", "hybrid-array", diff --git a/sec1/Cargo.toml b/sec1/Cargo.toml index ad61b3112..50a402ea1 100644 --- a/sec1/Cargo.toml +++ b/sec1/Cargo.toml @@ -18,6 +18,7 @@ rust-version = "1.85" [dependencies] base16ct = { version = "1", optional = true, default-features = false } +ctutils = { version = "0.3", optional = true } der = { version = "0.8.0-rc.10", optional = true, features = ["oid"] } hybrid-array = { version = "0.4", optional = true, default-features = false } serdect = { version = "0.4", optional = true, default-features = false, features = ["alloc"] } diff --git a/sec1/src/point.rs b/sec1/src/point.rs index 4a2ff43e8..9aa63d595 100644 --- a/sec1/src/point.rs +++ b/sec1/src/point.rs @@ -19,12 +19,12 @@ use hybrid_array::{Array, ArraySize, typenum::U1}; #[cfg(feature = "alloc")] use alloc::boxed::Box; +#[cfg(feature = "ctutils")] +use ctutils::{Choice, CtSelect}; + #[cfg(feature = "serde")] use serdect::serde::{Deserialize, Serialize, de, ser}; -#[cfg(feature = "subtle")] -use subtle::{Choice, ConditionallySelectable}; - #[cfg(feature = "zeroize")] use zeroize::Zeroize; @@ -254,23 +254,6 @@ where } } -#[cfg(feature = "subtle")] -impl ConditionallySelectable for EncodedPoint -where - Size: ModulusSize, - ::ArrayType: Copy, -{ - fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self { - let mut bytes = Array::default(); - - for (i, byte) in bytes.iter_mut().enumerate() { - *byte = u8::conditional_select(&a.bytes[i], &b.bytes[i], choice); - } - - Self { bytes } - } -} - impl Copy for EncodedPoint where Size: ModulusSize, @@ -336,17 +319,6 @@ where } } -#[cfg(feature = "zeroize")] -impl Zeroize for EncodedPoint -where - Size: ModulusSize, -{ - fn zeroize(&mut self) { - self.bytes.zeroize(); - *self = Self::identity(); - } -} - impl fmt::Display for EncodedPoint where Size: ModulusSize, @@ -392,6 +364,40 @@ where } } +// TODO(tarcieri): add `ctutils` support to `hybrid-array` +#[cfg(feature = "ctutils")] +impl CtSelect for EncodedPoint +where + Size: ModulusSize, +{ + fn ct_select(&self, other: &Self, choice: Choice) -> Self { + let mut bytes = Array::default(); + + for (i, byte) in bytes.iter_mut().enumerate() { + *byte = self.bytes[i].ct_select(&other.bytes[i], choice); + } + + Self { bytes } + } +} + +#[cfg(feature = "subtle")] +impl subtle::ConditionallySelectable for EncodedPoint +where + Size: ModulusSize, + ::ArrayType: Copy, +{ + fn conditional_select(a: &Self, b: &Self, choice: subtle::Choice) -> Self { + let mut bytes = Array::default(); + + for (i, byte) in bytes.iter_mut().enumerate() { + *byte = u8::conditional_select(&a.bytes[i], &b.bytes[i], choice); + } + + Self { bytes } + } +} + #[cfg(feature = "serde")] impl Serialize for EncodedPoint where @@ -419,6 +425,17 @@ where } } +#[cfg(feature = "zeroize")] +impl Zeroize for EncodedPoint +where + Size: ModulusSize, +{ + fn zeroize(&mut self) { + self.bytes.zeroize(); + *self = Self::identity(); + } +} + /// Enum representing the coordinates of either compressed or uncompressed /// SEC1-encoded elliptic curve points. #[derive(Copy, Clone, Debug, Eq, PartialEq)]