p256/lib.rs
1//! Pure Rust implementation of the NIST P-256 elliptic curve,
2//! including support for the
3//! [Elliptic Curve Digital Signature Algorithm (ECDSA)][ECDSA],
4//! [Elliptic Curve Diffie-Hellman (ECDH)][ECDH], and general purpose
5//! elliptic curve/field arithmetic which can be used to implement
6//! protocols based on group operations.
7//!
8//! ## About NIST P-256
9//!
10//! NIST P-256 is a Weierstrass curve specified in FIPS 186-4:
11//! Digital Signature Standard (DSS):
12//!
13//! <https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf>
14//!
15//! Also known as `prime256v1` (ANSI X9.62) and `secp256r1` (SECG), P-256 is
16//! included in the US National Security Agency's "Suite B" and is widely used
17//! in Internet and connected device protocols like TLS, the X.509 PKI, and
18//! Bluetooth.
19//!
20//! ## ⚠️ Security Warning
21//!
22//! The elliptic curve arithmetic contained in this crate has never been
23//! independently audited!
24//!
25//! This crate has been designed with the goal of ensuring that secret-dependent
26//! operations are performed in constant time (using the `subtle` crate and
27//! constant-time formulas). However, it has not been thoroughly assessed to ensure
28//! that generated assembly is constant time on common CPU architectures.
29//!
30//! USE AT YOUR OWN RISK!
31//!
32//! ## Minimum Supported Rust Version
33//!
34//! Rust **1.47** or higher.
35//!
36//! Minimum supported Rust version may be changed in the future, but it will be
37//! accompanied with a minor version bump.
38//!
39//! [ECDSA]: https://en.wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm
40//! [ECDH]: https://en.wikipedia.org/wiki/Elliptic-curve_Diffie%E2%80%93Hellman
41
42#![no_std]
43#![cfg_attr(docsrs, feature(doc_cfg))]
44#![doc(
45 html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
46 html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
47 html_root_url = "https://docs.rs/p256/0.9.0"
48)]
49#![forbid(unsafe_code)]
50#![warn(missing_docs, rust_2018_idioms, unused_qualifications)]
51
52#[cfg(feature = "arithmetic")]
53mod arithmetic;
54
55#[cfg(feature = "ecdh")]
56#[cfg_attr(docsrs, doc(cfg(feature = "ecdh")))]
57pub mod ecdh;
58
59#[cfg(feature = "ecdsa-core")]
60#[cfg_attr(docsrs, doc(cfg(feature = "ecdsa-core")))]
61pub mod ecdsa;
62
63#[cfg(any(feature = "test-vectors", test))]
64#[cfg_attr(docsrs, doc(cfg(feature = "test-vectors")))]
65pub mod test_vectors;
66
67pub use elliptic_curve::{self, bigint::U256};
68
69#[cfg(feature = "arithmetic")]
70pub use arithmetic::{
71 affine::AffinePoint,
72 projective::ProjectivePoint,
73 scalar::{blinding::BlindedScalar, Scalar},
74};
75
76#[cfg(feature = "pkcs8")]
77#[cfg_attr(docsrs, doc(cfg(feature = "pkcs8")))]
78pub use elliptic_curve::pkcs8;
79
80use elliptic_curve::{consts::U33, generic_array::GenericArray};
81
82/// NIST P-256 elliptic curve.
83///
84/// This curve is also known as prime256v1 (ANSI X9.62) and secp256r1 (SECG)
85/// and is specified in FIPS 186-4: Digital Signature Standard (DSS):
86///
87/// <https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf>
88///
89/// It's included in the US National Security Agency's "Suite B" and is widely
90/// used in protocols like TLS and the associated X.509 PKI.
91///
92/// Its equation is `y² = x³ - 3x + b` over a ~256-bit prime field where `b` is
93/// the "verifiably random"† constant:
94///
95/// ```text
96/// b = 41058363725152142129326129780047268409114441015993725554835256314039467401291
97/// ```
98///
99/// † *NOTE: the specific origins of this constant have never been fully disclosed
100/// (it is the SHA-1 digest of an inexplicable NSA-selected constant)*
101#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
102pub struct NistP256;
103
104impl elliptic_curve::Curve for NistP256 {
105 /// 256-bit field modulus
106 type UInt = U256;
107
108 /// Curve order
109 const ORDER: U256 =
110 U256::from_be_hex("ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551");
111}
112
113impl elliptic_curve::weierstrass::Curve for NistP256 {}
114
115impl elliptic_curve::weierstrass::PointCompression for NistP256 {
116 /// NIST P-256 points are typically uncompressed.
117 const COMPRESS_POINTS: bool = false;
118}
119
120#[cfg(feature = "jwk")]
121#[cfg_attr(docsrs, doc(cfg(feature = "jwk")))]
122impl elliptic_curve::JwkParameters for NistP256 {
123 const CRV: &'static str = "P-256";
124}
125
126#[cfg(feature = "pkcs8")]
127impl elliptic_curve::AlgorithmParameters for NistP256 {
128 const OID: pkcs8::ObjectIdentifier = pkcs8::ObjectIdentifier::new("1.2.840.10045.3.1.7");
129}
130
131/// Compressed SEC1-encoded NIST P-256 curve point.
132pub type CompressedPoint = GenericArray<u8, U33>;
133
134/// NIST P-256 field element serialized as bytes.
135///
136/// Byte array containing a serialized field element value (base field or scalar).
137pub type FieldBytes = elliptic_curve::FieldBytes<NistP256>;
138
139/// NIST P-256 SEC1 encoded point.
140pub type EncodedPoint = elliptic_curve::sec1::EncodedPoint<NistP256>;
141
142/// Non-zero NIST P-256 scalar field element.
143#[cfg(feature = "arithmetic")]
144pub type NonZeroScalar = elliptic_curve::NonZeroScalar<NistP256>;
145
146/// NIST P-256 public key.
147#[cfg(feature = "arithmetic")]
148pub type PublicKey = elliptic_curve::PublicKey<NistP256>;
149
150/// NIST P-256 secret key.
151#[cfg(feature = "zeroize")]
152#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))]
153pub type SecretKey = elliptic_curve::SecretKey<NistP256>;
154
155#[cfg(all(not(feature = "arithmetic"), feature = "zeroize"))]
156impl elliptic_curve::sec1::ValidatePublicKey for NistP256 {}
157
158/// Bit representation of a NIST P-256 scalar field element.
159#[cfg(feature = "bits")]
160#[cfg_attr(docsrs, doc(cfg(feature = "bits")))]
161pub type ScalarBits = elliptic_curve::ScalarBits<NistP256>;
162
163/// Scalar bytes: wrapper for [`FieldBytes`] which guarantees that the the
164/// inner byte value is within range of the [`Curve::ORDER`].
165pub type ScalarBytes = elliptic_curve::ScalarBytes<NistP256>;