Skip to main content

elliptic_curve/
lib.rs

1//! General purpose Elliptic Curve Cryptography (ECC) support, including types
2//! and traits for representing various elliptic curve forms, scalars, points,
3//! and public/secret keys composed thereof.
4//!
5//! ## Minimum Supported Rust Version
6//!
7//! Rust **1.51** or higher.
8//!
9//! Minimum supported Rust version can be changed in the future, but it will be
10//! done with a minor version bump.
11
12#![no_std]
13#![cfg_attr(docsrs, feature(doc_cfg))]
14#![forbid(unsafe_code, clippy::unwrap_used)]
15#![warn(missing_docs, rust_2018_idioms, unused_qualifications)]
16#![doc(
17    html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
18    html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
19    html_root_url = "https://docs.rs/elliptic-curve/0.10.4"
20)]
21
22#[cfg(feature = "alloc")]
23#[allow(unused_imports)]
24#[macro_use]
25extern crate alloc;
26
27#[cfg(feature = "std")]
28extern crate std;
29
30#[cfg(feature = "rand_core")]
31#[cfg_attr(docsrs, doc(cfg(feature = "rand_core")))]
32pub use rand_core;
33
34pub mod ops;
35pub mod sec1;
36pub mod weierstrass;
37
38mod error;
39mod scalar;
40
41#[cfg(feature = "arithmetic")]
42mod arithmetic;
43#[cfg(feature = "arithmetic")]
44mod public_key;
45
46#[cfg(feature = "dev")]
47#[cfg_attr(docsrs, doc(cfg(feature = "dev")))]
48pub mod dev;
49
50#[cfg(feature = "ecdh")]
51#[cfg_attr(docsrs, doc(cfg(feature = "ecdh")))]
52pub mod ecdh;
53
54#[cfg(feature = "jwk")]
55mod jwk;
56
57#[cfg(feature = "zeroize")]
58mod secret_key;
59
60pub use self::{
61    error::{Error, Result},
62    scalar::bytes::ScalarBytes,
63};
64pub use crypto_bigint as bigint;
65pub use generic_array::{self, typenum::consts};
66pub use rand_core;
67pub use subtle;
68
69#[cfg(feature = "arithmetic")]
70pub use {
71    crate::{
72        arithmetic::{AffineArithmetic, ProjectiveArithmetic, ScalarArithmetic},
73        public_key::PublicKey,
74        scalar::{non_zero::NonZeroScalar, Scalar},
75    },
76    ff::Field,
77    group::{self, Group},
78};
79
80#[cfg(feature = "bits")]
81pub use crate::scalar::ScalarBits;
82
83#[cfg(feature = "jwk")]
84pub use crate::jwk::{JwkEcKey, JwkParameters};
85
86#[cfg(feature = "pkcs8")]
87pub use pkcs8;
88
89#[cfg(feature = "zeroize")]
90pub use secret_key::SecretKey;
91#[cfg(feature = "zeroize")]
92pub use zeroize;
93
94use core::fmt::Debug;
95use generic_array::GenericArray;
96use subtle::{ConstantTimeEq, ConstantTimeGreater, ConstantTimeLess};
97
98/// Algorithm [`ObjectIdentifier`][`pkcs8::ObjectIdentifier`] for elliptic
99/// curve public key cryptography.
100///
101/// <http://oid-info.com/get/1.2.840.10045.2.1>
102#[cfg(feature = "pkcs8")]
103#[cfg_attr(docsrs, doc(cfg(feature = "pkcs8")))]
104pub const ALGORITHM_OID: pkcs8::ObjectIdentifier =
105    pkcs8::ObjectIdentifier::new("1.2.840.10045.2.1");
106
107/// Elliptic curve.
108///
109/// This trait is intended to be impl'd by a ZST which represents a concrete
110/// elliptic curve.
111///
112/// Other traits in this crate which are bounded by [`Curve`] are intended to
113/// be impl'd by these ZSTs, facilitating types which are generic over elliptic
114/// curves (e.g. [`SecretKey`]).
115pub trait Curve: Clone + Debug + Default + Eq + Ord + Send + Sync {
116    /// Integer type used to represent field elements of this elliptic curve.
117    // TODO(tarcieri): replace this with an e.g. `const Curve::MODULUS: uint`.
118    // Requires rust-lang/rust#60551, i.e. `const_evaluatable_checked`
119    type UInt: AsRef<[bigint::Limb]>
120        + bigint::ArrayEncoding
121        + bigint::Encoding
122        + Copy
123        + Debug
124        + Default
125        + ConstantTimeEq
126        + ConstantTimeGreater
127        + ConstantTimeLess;
128
129    /// Order constant.
130    ///
131    /// Subdivided into either 32-bit or 64-bit "limbs" (depending on the
132    /// target CPU's word size), specified from least to most significant.
133    const ORDER: Self::UInt;
134}
135
136/// Size of field elements of this elliptic curve.
137pub type FieldSize<C> = <<C as Curve>::UInt as bigint::ArrayEncoding>::ByteSize;
138
139/// Byte representation of a base/scalar field element of a given curve.
140pub type FieldBytes<C> = GenericArray<u8, FieldSize<C>>;
141
142/// Affine point type for a given curve with a [`ProjectiveArithmetic`]
143/// implementation.
144#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
145#[cfg(feature = "arithmetic")]
146pub type AffinePoint<C> = <C as AffineArithmetic>::AffinePoint;
147
148/// Projective point type for a given curve with a [`ProjectiveArithmetic`]
149/// implementation.
150#[cfg(feature = "arithmetic")]
151#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
152pub type ProjectivePoint<C> = <C as ProjectiveArithmetic>::ProjectivePoint;
153
154/// Associate an [`ObjectIdentifier`][`pkcs8::ObjectIdentifier`] (OID) with an
155/// elliptic curve algorithm implementation.
156///
157/// This is used as as the `parameters` of an `AlgorithmIdentifier` as
158/// described in RFC 5280 Section 4.1.1.2:
159/// <https://tools.ietf.org/html/rfc5280#section-4.1.1.2>
160#[cfg(feature = "pkcs8")]
161#[cfg_attr(docsrs, doc(cfg(feature = "pkcs8")))]
162pub trait AlgorithmParameters: Curve {
163    /// Object Identifier (OID) for this curve
164    const OID: pkcs8::ObjectIdentifier;
165
166    /// Get the [`pkcs8::AlgorithmIdentifier`] for this curve
167    fn algorithm_identifier() -> pkcs8::AlgorithmIdentifier<'static> {
168        pkcs8::AlgorithmIdentifier {
169            oid: ALGORITHM_OID,
170            parameters: Some((&Self::OID).into()),
171        }
172    }
173}