p256/
lib.rs

1#![no_std]
2#![cfg_attr(docsrs, feature(doc_cfg))]
3#![doc(
4    html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
5    html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg"
6)]
7#![forbid(unsafe_code)]
8#![warn(missing_docs, rust_2018_idioms, unused_qualifications)]
9#![doc = include_str!("../README.md")]
10
11//! ## `serde` support
12//!
13//! When the `serde` feature of this crate is enabled, `Serialize` and
14//! `Deserialize` are impl'd for the following types:
15//!
16//! - [`AffinePoint`]
17//! - [`Scalar`]
18//! - [`ecdsa::VerifyingKey`]
19//!
20//! Please see type-specific documentation for more information.
21
22#[cfg(feature = "arithmetic")]
23mod arithmetic;
24
25#[cfg(feature = "ecdh")]
26#[cfg_attr(docsrs, doc(cfg(feature = "ecdh")))]
27pub mod ecdh;
28
29#[cfg(feature = "ecdsa-core")]
30#[cfg_attr(docsrs, doc(cfg(feature = "ecdsa-core")))]
31pub mod ecdsa;
32
33#[cfg(any(feature = "test-vectors", test))]
34#[cfg_attr(docsrs, doc(cfg(feature = "test-vectors")))]
35pub mod test_vectors;
36
37pub use elliptic_curve::{self, bigint::U256};
38
39#[cfg(feature = "arithmetic")]
40pub use arithmetic::{
41    affine::AffinePoint,
42    projective::ProjectivePoint,
43    scalar::{blinded::BlindedScalar, Scalar},
44};
45
46#[cfg(feature = "expose-field")]
47pub use arithmetic::field::FieldElement;
48
49#[cfg(feature = "pkcs8")]
50#[cfg_attr(docsrs, doc(cfg(feature = "pkcs8")))]
51pub use elliptic_curve::pkcs8;
52
53use elliptic_curve::{consts::U33, generic_array::GenericArray};
54
55/// NIST P-256 elliptic curve.
56///
57/// This curve is also known as prime256v1 (ANSI X9.62) and secp256r1 (SECG)
58/// and is specified in FIPS 186-4: Digital Signature Standard (DSS):
59///
60/// <https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf>
61///
62/// It's included in the US National Security Agency's "Suite B" and is widely
63/// used in protocols like TLS and the associated X.509 PKI.
64///
65/// Its equation is `y² = x³ - 3x + b` over a ~256-bit prime field where `b` is
66/// the "verifiably random"† constant:
67///
68/// ```text
69/// b = 41058363725152142129326129780047268409114441015993725554835256314039467401291
70/// ```
71///
72/// † *NOTE: the specific origins of this constant have never been fully disclosed
73///   (it is the SHA-1 digest of an inexplicable NSA-selected constant)*
74#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
75pub struct NistP256;
76
77impl elliptic_curve::Curve for NistP256 {
78    /// 256-bit integer type used for internally representing field elements.
79    type UInt = U256;
80
81    /// Order of NIST P-256's elliptic curve group (i.e. scalar modulus).
82    ///
83    /// ```text
84    /// n = FFFFFFFF 00000000 FFFFFFFF FFFFFFFF BCE6FAAD A7179E84 F3B9CAC2 FC632551
85    /// ```
86    ///
87    /// # Calculating the order
88    /// One way to calculate the order is with `GP/PARI`:
89    ///
90    /// ```text
91    /// p = (2^224) * (2^32 - 1) + 2^192 + 2^96 - 1
92    /// b = 41058363725152142129326129780047268409114441015993725554835256314039467401291
93    /// E = ellinit([Mod(-3, p), Mod(b, p)])
94    /// default(parisize, 120000000)
95    /// n = ellsea(E)
96    /// isprime(n)
97    /// ```
98    const ORDER: U256 =
99        U256::from_be_hex("ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551");
100}
101
102impl elliptic_curve::PrimeCurve for NistP256 {}
103
104impl elliptic_curve::PointCompression for NistP256 {
105    /// NIST P-256 points are typically uncompressed.
106    const COMPRESS_POINTS: bool = false;
107}
108
109impl elliptic_curve::PointCompaction for NistP256 {
110    /// NIST P-256 points are typically uncompressed.
111    const COMPACT_POINTS: bool = false;
112}
113
114#[cfg(feature = "jwk")]
115#[cfg_attr(docsrs, doc(cfg(feature = "jwk")))]
116impl elliptic_curve::JwkParameters for NistP256 {
117    const CRV: &'static str = "P-256";
118}
119
120#[cfg(feature = "pkcs8")]
121impl pkcs8::AssociatedOid for NistP256 {
122    const OID: pkcs8::ObjectIdentifier = pkcs8::ObjectIdentifier::new_unwrap("1.2.840.10045.3.1.7");
123}
124
125/// Compressed SEC1-encoded NIST P-256 curve point.
126pub type CompressedPoint = GenericArray<u8, U33>;
127
128/// NIST P-256 field element serialized as bytes.
129///
130/// Byte array containing a serialized field element value (base field or scalar).
131pub type FieldBytes = elliptic_curve::FieldBytes<NistP256>;
132
133/// NIST P-256 SEC1 encoded point.
134pub type EncodedPoint = elliptic_curve::sec1::EncodedPoint<NistP256>;
135
136/// Non-zero NIST P-256 scalar field element.
137#[cfg(feature = "arithmetic")]
138pub type NonZeroScalar = elliptic_curve::NonZeroScalar<NistP256>;
139
140/// NIST P-256 public key.
141#[cfg(feature = "arithmetic")]
142pub type PublicKey = elliptic_curve::PublicKey<NistP256>;
143
144/// NIST P-256 secret key.
145pub type SecretKey = elliptic_curve::SecretKey<NistP256>;
146
147#[cfg(not(feature = "arithmetic"))]
148impl elliptic_curve::sec1::ValidatePublicKey for NistP256 {}
149
150/// Bit representation of a NIST P-256 scalar field element.
151#[cfg(feature = "bits")]
152#[cfg_attr(docsrs, doc(cfg(feature = "bits")))]
153pub type ScalarBits = elliptic_curve::ScalarBits<NistP256>;
154
155#[cfg(feature = "voprf")]
156#[cfg_attr(docsrs, doc(cfg(feature = "voprf")))]
157impl elliptic_curve::VoprfParameters for NistP256 {
158    /// See <https://www.ietf.org/archive/id/draft-irtf-cfrg-voprf-08.html#section-4.3-1.3>.
159    const ID: u16 = 0x0003;
160
161    /// See <https://www.ietf.org/archive/id/draft-irtf-cfrg-voprf-08.html#section-4.3-1.2>.
162    type Hash = sha2::Sha256;
163}