From 7a48c172c235f3b20482f741256e8a64397b529a Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Tue, 4 Aug 2020 09:10:45 -0700 Subject: [PATCH] elliptic-curve: use `const-oid` crate The previous implementation has been extracted into that crate. --- Cargo.lock | 7 ++++++ elliptic-curve/Cargo.toml | 23 +++++-------------- elliptic-curve/src/lib.rs | 10 +++++--- elliptic-curve/src/oid.rs | 48 --------------------------------------- 4 files changed, 20 insertions(+), 68 deletions(-) delete mode 100644 elliptic-curve/src/oid.rs diff --git a/Cargo.lock b/Cargo.lock index 4d86f3c44..3660857f1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -61,6 +61,12 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +[[package]] +name = "const-oid" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2d9162b7289a46e86208d6af2c686ca5bfde445878c41a458a9fac706252d0b" + [[package]] name = "cpuid-bool" version = "0.1.0" @@ -111,6 +117,7 @@ dependencies = [ name = "elliptic-curve" version = "0.5.0-pre" dependencies = [ + "const-oid", "generic-array 0.14.3", "rand_core", "subtle", diff --git a/elliptic-curve/Cargo.toml b/elliptic-curve/Cargo.toml index ecc7a7274..c6a4f8f47 100644 --- a/elliptic-curve/Cargo.toml +++ b/elliptic-curve/Cargo.toml @@ -15,23 +15,12 @@ edition = "2018" categories = ["cryptography", "no-std"] keywords = ["crypto", "ecc", "elliptic", "weierstrass"] -[dependencies.generic-array] -version = "0.14" -default-features = false - -[dependencies.rand_core] -version = "0.5" -optional = true -default-features = false - -[dependencies.subtle] -version = "2.2.2" -default-features = false - -[dependencies.zeroize] -version = "1" -optional = true -default-features = false +[dependencies] +generic-array = { version = "0.14", default-features = false } +oid = { package = "const-oid", version = "0.1", optional = true } +rand_core = { version = "0.5", optional = true, default-features = false } +subtle = { version = "2.2.2", default-features = false } +zeroize = { version = "1", optional = true, default-features = false } [features] default = [] diff --git a/elliptic-curve/src/lib.rs b/elliptic-curve/src/lib.rs index c829740fd..8535f6e9a 100644 --- a/elliptic-curve/src/lib.rs +++ b/elliptic-curve/src/lib.rs @@ -22,7 +22,6 @@ extern crate std; pub mod error; -pub mod oid; pub mod ops; pub mod point; pub mod scalar; @@ -33,10 +32,13 @@ pub mod secret_key; #[cfg_attr(docsrs, doc(cfg(feature = "weierstrass")))] pub mod weierstrass; -pub use self::{error::Error, oid::ObjectIdentifier, secret_key::SecretKey}; +pub use self::{error::Error, secret_key::SecretKey}; pub use generic_array::{self, typenum::consts}; pub use subtle; +#[cfg(feature = "oid")] +pub use oid; + #[cfg(feature = "rand_core")] pub use rand_core; @@ -82,9 +84,11 @@ pub trait Arithmetic: Curve { } /// Associate an object identifier (OID) with a curve +#[cfg(feature = "oid")] +#[cfg_attr(docsrs, doc(cfg(feature = "oid")))] pub trait Identifier: Curve { /// Object Identifier (OID) for this curve - const OID: ObjectIdentifier; + const OID: oid::ObjectIdentifier; } /// Randomly generate a value. diff --git a/elliptic-curve/src/oid.rs b/elliptic-curve/src/oid.rs deleted file mode 100644 index b5494176f..000000000 --- a/elliptic-curve/src/oid.rs +++ /dev/null @@ -1,48 +0,0 @@ -//! Object identifiers (OIDs) - -use core::fmt; - -/// Object identifier (OID) -pub struct ObjectIdentifier(&'static [u32]); - -impl ObjectIdentifier { - /// Create a new OID - pub const fn new(nodes: &'static [u32]) -> Self { - // TODO(tarcieri): validate nodes - Self(nodes) - } -} - -impl AsRef<[u32]> for ObjectIdentifier { - fn as_ref(&self) -> &[u32] { - self.0 - } -} - -impl fmt::Display for ObjectIdentifier { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - for (i, node) in self.0.iter().enumerate() { - write!(f, "{}", node)?; - - if i < self.0.len() - 1 { - write!(f, ".")?; - } - } - - Ok(()) - } -} - -#[cfg(all(test, std))] -mod tests { - use super::ObjectIdentifier; - use std::string::ToString; - - const EXAMPLE_OID: ObjectIdentifier = ObjectIdentifier::new(&[1, 2, 840, 10045, 3, 1, 7]); - - #[test] - fn display_test() { - let oid = EXAMPLE_OID.to_string(); - assert_eq!(oid, "1.2.840.10045.3.1.7"); - } -}