Skip to content

Commit a65edce

Browse files
authored
elliptic-curve: basic OID support (#240)
Adds a (very simple, const-and-no_std-friendly) `ObjectIdentifier` type and an `Identifier` trait for attaching an OID to a curve. No validation of the nodes is presently performed (and since it's impl'd as a `const fn`, it'd require a MSRV bump to do so). Also requires the OID be specified as an array literal: no parsing is supported for either string or binary-encoded OIDs. Only string serialization is supported for now. A `no_std`-friendly binary serializer will be somewhat tricky.
1 parent 2eb3134 commit a65edce

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

elliptic-curve/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
extern crate std;
2323

2424
pub mod error;
25+
pub mod oid;
2526
pub mod ops;
2627
pub mod secret_key;
2728

@@ -30,7 +31,7 @@ pub mod secret_key;
3031
#[cfg_attr(docsrs, doc(cfg(feature = "weierstrass")))]
3132
pub mod weierstrass;
3233

33-
pub use self::{error::Error, secret_key::SecretKey};
34+
pub use self::{error::Error, oid::ObjectIdentifier, secret_key::SecretKey};
3435
pub use generic_array::{self, typenum::consts};
3536
pub use subtle;
3637

@@ -72,6 +73,12 @@ pub trait Arithmetic: Curve {
7273
type AffinePoint: ConditionallySelectable + ops::MulBase<Scalar = Self::Scalar>;
7374
}
7475

76+
/// Associate an object identifier (OID) with a curve
77+
pub trait Identifier: Curve {
78+
/// Object Identifier (OID) for this curve
79+
const OID: ObjectIdentifier;
80+
}
81+
7582
/// Randomly generate a value.
7683
///
7784
/// Primarily intended for use with scalar types for a particular curve.

elliptic-curve/src/oid.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//! Object identifiers (OIDs)
2+
3+
use core::fmt;
4+
5+
/// Object identifier (OID)
6+
pub struct ObjectIdentifier(&'static [u32]);
7+
8+
impl ObjectIdentifier {
9+
/// Create a new OID
10+
pub const fn new(nodes: &'static [u32]) -> Self {
11+
// TODO(tarcieri): validate nodes
12+
Self(nodes)
13+
}
14+
}
15+
16+
impl AsRef<[u32]> for ObjectIdentifier {
17+
fn as_ref(&self) -> &[u32] {
18+
self.0
19+
}
20+
}
21+
22+
impl fmt::Display for ObjectIdentifier {
23+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24+
for (i, node) in self.0.iter().enumerate() {
25+
write!(f, "{}", node)?;
26+
27+
if i < self.0.len() - 1 {
28+
write!(f, ".")?;
29+
}
30+
}
31+
32+
Ok(())
33+
}
34+
}
35+
36+
#[cfg(all(test, std))]
37+
mod tests {
38+
use super::ObjectIdentifier;
39+
use std::string::ToString;
40+
41+
const EXAMPLE_OID: ObjectIdentifier = ObjectIdentifier::new(&[1, 2, 840, 10045, 3, 1, 7]);
42+
43+
#[test]
44+
fn display_test() {
45+
let oid = EXAMPLE_OID.to_string();
46+
assert_eq!(oid, "1.2.840.10045.3.1.7");
47+
}
48+
}

0 commit comments

Comments
 (0)