diff --git a/elliptic-curve/src/lib.rs b/elliptic-curve/src/lib.rs index 3528d22e0..c5cf968db 100644 --- a/elliptic-curve/src/lib.rs +++ b/elliptic-curve/src/lib.rs @@ -22,6 +22,7 @@ extern crate std; pub mod error; +pub mod oid; pub mod ops; pub mod secret_key; @@ -30,7 +31,7 @@ pub mod secret_key; #[cfg_attr(docsrs, doc(cfg(feature = "weierstrass")))] pub mod weierstrass; -pub use self::{error::Error, secret_key::SecretKey}; +pub use self::{error::Error, oid::ObjectIdentifier, secret_key::SecretKey}; pub use generic_array::{self, typenum::consts}; pub use subtle; @@ -72,6 +73,12 @@ pub trait Arithmetic: Curve { type AffinePoint: ConditionallySelectable + ops::MulBase; } +/// Associate an object identifier (OID) with a curve +pub trait Identifier: Curve { + /// Object Identifier (OID) for this curve + const OID: ObjectIdentifier; +} + /// Randomly generate a value. /// /// Primarily intended for use with scalar types for a particular curve. diff --git a/elliptic-curve/src/oid.rs b/elliptic-curve/src/oid.rs new file mode 100644 index 000000000..b5494176f --- /dev/null +++ b/elliptic-curve/src/oid.rs @@ -0,0 +1,48 @@ +//! 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"); + } +}