p384/
lib.rs

1#![no_std]
2#![cfg_attr(docsrs, feature(doc_auto_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")]
26pub mod ecdh;
27
28#[cfg(feature = "ecdsa-core")]
29pub mod ecdsa;
30
31#[cfg(any(feature = "test-vectors", test))]
32pub mod test_vectors;
33
34pub use elliptic_curve::{self, bigint::U384, consts::U48};
35
36#[cfg(feature = "arithmetic")]
37pub use arithmetic::{scalar::Scalar, AffinePoint, ProjectivePoint};
38
39#[cfg(feature = "expose-field")]
40pub use arithmetic::field::FieldElement;
41
42#[cfg(feature = "pkcs8")]
43pub use elliptic_curve::pkcs8;
44
45use elliptic_curve::{
46    bigint::ArrayEncoding, consts::U49, generic_array::GenericArray, FieldBytesEncoding,
47};
48
49/// Order of NIST P-384's elliptic curve group (i.e. scalar modulus) in hexadecimal.
50const ORDER_HEX: &str = "ffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973";
51
52/// NIST P-384 elliptic curve.
53#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
54pub struct NistP384;
55
56impl elliptic_curve::Curve for NistP384 {
57    /// 48-byte serialized field elements.
58    type FieldBytesSize = U48;
59
60    /// 384-bit integer type used for internally representing field elements.
61    type Uint = U384;
62
63    /// Order of NIST P-384's elliptic curve group (i.e. scalar modulus).
64    const ORDER: U384 = U384::from_be_hex(ORDER_HEX);
65}
66
67impl elliptic_curve::PrimeCurve for NistP384 {}
68
69impl elliptic_curve::point::PointCompression for NistP384 {
70    /// NIST P-384 points are typically uncompressed.
71    const COMPRESS_POINTS: bool = false;
72}
73
74impl elliptic_curve::point::PointCompaction for NistP384 {
75    /// NIST P-384 points are typically uncompressed.
76    const COMPACT_POINTS: bool = false;
77}
78
79#[cfg(feature = "jwk")]
80impl elliptic_curve::JwkParameters for NistP384 {
81    const CRV: &'static str = "P-384";
82}
83
84#[cfg(feature = "pkcs8")]
85impl pkcs8::AssociatedOid for NistP384 {
86    const OID: pkcs8::ObjectIdentifier = pkcs8::ObjectIdentifier::new_unwrap("1.3.132.0.34");
87}
88
89/// Compressed SEC1-encoded NIST P-384 curve point.
90pub type CompressedPoint = GenericArray<u8, U49>;
91
92/// NIST P-384 SEC1 encoded point.
93pub type EncodedPoint = elliptic_curve::sec1::EncodedPoint<NistP384>;
94
95/// NIST P-384 field element serialized as bytes.
96///
97/// Byte array containing a serialized field element value (base field or
98/// scalar).
99pub type FieldBytes = elliptic_curve::FieldBytes<NistP384>;
100
101impl FieldBytesEncoding<NistP384> for U384 {
102    fn decode_field_bytes(field_bytes: &FieldBytes) -> Self {
103        U384::from_be_byte_array(*field_bytes)
104    }
105
106    fn encode_field_bytes(&self) -> FieldBytes {
107        self.to_be_byte_array()
108    }
109}
110
111/// Non-zero NIST P-384 scalar field element.
112#[cfg(feature = "arithmetic")]
113pub type NonZeroScalar = elliptic_curve::NonZeroScalar<NistP384>;
114
115/// NIST P-384 public key.
116#[cfg(feature = "arithmetic")]
117pub type PublicKey = elliptic_curve::PublicKey<NistP384>;
118
119/// NIST P-384 secret key.
120pub type SecretKey = elliptic_curve::SecretKey<NistP384>;
121
122#[cfg(not(feature = "arithmetic"))]
123impl elliptic_curve::sec1::ValidatePublicKey for NistP384 {}
124
125/// Bit representation of a NIST P-384 scalar field element.
126#[cfg(feature = "bits")]
127pub type ScalarBits = elliptic_curve::scalar::ScalarBits<NistP384>;
128
129#[cfg(feature = "voprf")]
130impl elliptic_curve::VoprfParameters for NistP384 {
131    /// See <https://www.ietf.org/archive/id/draft-irtf-cfrg-voprf-19.html#name-oprfp-384-sha-384-2>.
132    const ID: &'static str = "P384-SHA384";
133
134    /// See <https://www.ietf.org/archive/id/draft-irtf-cfrg-voprf-08.html#section-4.4-1.2>.
135    type Hash = sha2::Sha384;
136}