[go: up one dir, main page]

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::{AffinePoint, ProjectivePoint, scalar::Scalar};
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::{FieldBytesEncoding, array::Array, bigint::ArrayEncoding, consts::U49};
46
47/// Order of NIST P-384's elliptic curve group (i.e. scalar modulus) in hexadecimal.
48const ORDER_HEX: &str = "ffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973";
49
50/// NIST P-384 elliptic curve.
51#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
52pub struct NistP384;
53
54impl elliptic_curve::Curve for NistP384 {
55    /// 48-byte serialized field elements.
56    type FieldBytesSize = U48;
57
58    /// 384-bit integer type used for internally representing field elements.
59    type Uint = U384;
60
61    /// Order of NIST P-384's elliptic curve group (i.e. scalar modulus).
62    const ORDER: U384 = U384::from_be_hex(ORDER_HEX);
63}
64
65impl elliptic_curve::PrimeCurve for NistP384 {}
66
67impl elliptic_curve::point::PointCompression for NistP384 {
68    /// NIST P-384 points are typically uncompressed.
69    const COMPRESS_POINTS: bool = false;
70}
71
72impl elliptic_curve::point::PointCompaction for NistP384 {
73    /// NIST P-384 points are typically uncompressed.
74    const COMPACT_POINTS: bool = false;
75}
76
77#[cfg(feature = "jwk")]
78impl elliptic_curve::JwkParameters for NistP384 {
79    const CRV: &'static str = "P-384";
80}
81
82#[cfg(feature = "pkcs8")]
83impl pkcs8::AssociatedOid for NistP384 {
84    const OID: pkcs8::ObjectIdentifier = pkcs8::ObjectIdentifier::new_unwrap("1.3.132.0.34");
85}
86
87/// Compressed SEC1-encoded NIST P-384 curve point.
88pub type CompressedPoint = Array<u8, U49>;
89
90/// NIST P-384 SEC1 encoded point.
91pub type EncodedPoint = elliptic_curve::sec1::EncodedPoint<NistP384>;
92
93/// NIST P-384 field element serialized as bytes.
94///
95/// Byte array containing a serialized field element value (base field or
96/// scalar).
97pub type FieldBytes = elliptic_curve::FieldBytes<NistP384>;
98
99impl FieldBytesEncoding<NistP384> for U384 {
100    fn decode_field_bytes(field_bytes: &FieldBytes) -> Self {
101        U384::from_be_byte_array(*field_bytes)
102    }
103
104    fn encode_field_bytes(&self) -> FieldBytes {
105        self.to_be_byte_array()
106    }
107}
108
109/// Non-zero NIST P-384 scalar field element.
110#[cfg(feature = "arithmetic")]
111pub type NonZeroScalar = elliptic_curve::NonZeroScalar<NistP384>;
112
113/// NIST P-384 public key.
114#[cfg(feature = "arithmetic")]
115pub type PublicKey = elliptic_curve::PublicKey<NistP384>;
116
117/// NIST P-384 secret key.
118pub type SecretKey = elliptic_curve::SecretKey<NistP384>;
119
120#[cfg(not(feature = "arithmetic"))]
121impl elliptic_curve::sec1::ValidatePublicKey for NistP384 {}
122
123/// Bit representation of a NIST P-384 scalar field element.
124#[cfg(feature = "bits")]
125pub type ScalarBits = elliptic_curve::scalar::ScalarBits<NistP384>;
126
127#[cfg(feature = "oprf")]
128impl elliptic_curve::OprfParameters for NistP384 {
129    /// See <https://www.rfc-editor.org/rfc/rfc9497.html#section-4.4-1>.
130    const ID: &'static [u8] = b"P384-SHA384";
131
132    /// See <https://www.rfc-editor.org/rfc/rfc9497.html#section-4.4-2.4>.
133    type Hash = sha2::Sha384;
134
135    /// See <https://www.rfc-editor.org/rfc/rfc9497.html#section-4.4-2.2.2.10>
136    /// and <https://www.rfc-editor.org/rfc/rfc9497.html#section-4.4-2.2.2.12>.
137    type ExpandMsg = elliptic_curve::hash2curve::ExpandMsgXmd<sha2::Sha384>;
138}