aws_lc_rs/
agreement.rs

1// Copyright 2015-2017 Brian Smith.
2// SPDX-License-Identifier: ISC
3// Modifications copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4// SPDX-License-Identifier: Apache-2.0 OR ISC
5
6//! Key Agreement: ECDH, including X25519.
7//!
8//! # Example
9//!
10//! Note that this example uses X25519, but ECDH using NIST P-256/P-384 is done
11//! exactly the same way, just substituting
12//! `agreement::ECDH_P256`/`agreement::ECDH_P384` for `agreement::X25519`.
13//!
14//! ```
15//! use aws_lc_rs::{agreement, rand};
16//!
17//! let rng = rand::SystemRandom::new();
18//!
19//! let my_private_key = agreement::EphemeralPrivateKey::generate(&agreement::X25519, &rng)?;
20//!
21//! // Make `my_public_key` a byte slice containing my public key. In a real
22//! // application, this would be sent to the peer in an encoded protocol
23//! // message.
24//! let my_public_key = my_private_key.compute_public_key()?;
25//!
26//! let peer_public_key = {
27//!     // In a real application, the peer public key would be parsed out of a
28//!     // protocol message. Here we just generate one.
29//!     let peer_public_key = {
30//!         let peer_private_key =
31//!             agreement::EphemeralPrivateKey::generate(&agreement::X25519, &rng)?;
32//!         peer_private_key.compute_public_key()?
33//!     };
34//!
35//!     agreement::UnparsedPublicKey::new(&agreement::X25519, peer_public_key)
36//! };
37//!
38//! agreement::agree_ephemeral(
39//!     my_private_key,
40//!     &peer_public_key,
41//!     aws_lc_rs::error::Unspecified,
42//!     |_key_material| {
43//!         // In a real application, we'd apply a KDF to the key material and the
44//!         // public keys (as recommended in RFC 7748) and then derive session
45//!         // keys from the result. We omit all that here.
46//!         Ok(())
47//!     },
48//! )?;
49//!
50//! # Ok::<(), aws_lc_rs::error::Unspecified>(())
51//! ```
52mod ephemeral;
53
54use crate::ec::encoding::sec1::{
55    marshal_sec1_private_key, marshal_sec1_public_point, marshal_sec1_public_point_into_buffer,
56    parse_sec1_private_bn, parse_sec1_public_point,
57};
58#[cfg(not(feature = "fips"))]
59use crate::ec::verify_evp_key_nid;
60use crate::ec::{evp_key_generate, validate_ec_evp_key};
61use crate::error::{KeyRejected, Unspecified};
62use crate::hex;
63pub use ephemeral::{agree_ephemeral, EphemeralPrivateKey};
64
65use crate::aws_lc::{
66    i2d_ECPrivateKey, EVP_PKEY_get0_EC_KEY, NID_X9_62_prime256v1, NID_secp384r1, NID_secp521r1,
67    EVP_PKEY, EVP_PKEY_EC, EVP_PKEY_X25519, NID_X25519,
68};
69
70use crate::buffer::Buffer;
71use crate::ec;
72use crate::ec::encoding::rfc5915::parse_rfc5915_private_key;
73use crate::encoding::{
74    AsBigEndian, AsDer, Curve25519SeedBin, EcPrivateKeyBin, EcPrivateKeyRfc5915Der,
75    EcPublicKeyCompressedBin, EcPublicKeyUncompressedBin, Pkcs8V1Der, PublicKeyX509Der,
76};
77use crate::evp_pkey::No_EVP_PKEY_CTX_consumer;
78use crate::pkcs8::Version;
79use crate::ptr::LcPtr;
80use core::fmt;
81use core::fmt::{Debug, Formatter};
82use core::ptr::null_mut;
83
84#[allow(non_camel_case_types)]
85#[derive(PartialEq, Eq)]
86enum AlgorithmID {
87    ECDH_P256,
88    ECDH_P384,
89    ECDH_P521,
90    X25519,
91}
92
93impl AlgorithmID {
94    #[inline]
95    const fn nid(&self) -> i32 {
96        match self {
97            AlgorithmID::ECDH_P256 => NID_X9_62_prime256v1,
98            AlgorithmID::ECDH_P384 => NID_secp384r1,
99            AlgorithmID::ECDH_P521 => NID_secp521r1,
100            AlgorithmID::X25519 => NID_X25519,
101        }
102    }
103
104    // Uncompressed public key length in bytes
105    #[inline]
106    const fn pub_key_len(&self) -> usize {
107        match self {
108            AlgorithmID::ECDH_P256 => ec::uncompressed_public_key_size_bytes(256),
109            AlgorithmID::ECDH_P384 => ec::uncompressed_public_key_size_bytes(384),
110            AlgorithmID::ECDH_P521 => ec::uncompressed_public_key_size_bytes(521),
111            AlgorithmID::X25519 => 32,
112        }
113    }
114
115    #[inline]
116    const fn private_key_len(&self) -> usize {
117        match self {
118            AlgorithmID::ECDH_P256 | AlgorithmID::X25519 => 32,
119            AlgorithmID::ECDH_P384 => 48,
120            AlgorithmID::ECDH_P521 => 66,
121        }
122    }
123}
124
125impl Debug for AlgorithmID {
126    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
127        let output = match self {
128            AlgorithmID::ECDH_P256 => "curve: P256",
129            AlgorithmID::ECDH_P384 => "curve: P384",
130            AlgorithmID::ECDH_P521 => "curve: P521",
131            AlgorithmID::X25519 => "curve: Curve25519",
132        };
133        f.write_str(output)
134    }
135}
136
137/// A key agreement algorithm.
138#[derive(PartialEq, Eq)]
139pub struct Algorithm {
140    id: AlgorithmID,
141}
142
143impl Debug for Algorithm {
144    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
145        f.write_str(&format!("Algorithm {{ {:?} }}", self.id))
146    }
147}
148
149/// ECDH using the NSA Suite B P-256 (secp256r1) curve.
150pub const ECDH_P256: Algorithm = Algorithm {
151    id: AlgorithmID::ECDH_P256,
152};
153
154/// ECDH using the NSA Suite B P-384 (secp384r1) curve.
155pub const ECDH_P384: Algorithm = Algorithm {
156    id: AlgorithmID::ECDH_P384,
157};
158
159/// ECDH using the NSA Suite B P-521 (secp521r1) curve.
160pub const ECDH_P521: Algorithm = Algorithm {
161    id: AlgorithmID::ECDH_P521,
162};
163
164/// X25519 (ECDH using Curve25519) as described in [RFC 7748].
165///
166/// Everything is as described in RFC 7748. Key agreement will fail if the
167/// result of the X25519 operation is zero; see the notes on the
168/// "all-zero value" in [RFC 7748 section 6.1].
169///
170/// [RFC 7748]: https://tools.ietf.org/html/rfc7748
171/// [RFC 7748 section 6.1]: https://tools.ietf.org/html/rfc7748#section-6.1
172pub const X25519: Algorithm = Algorithm {
173    id: AlgorithmID::X25519,
174};
175
176#[allow(non_camel_case_types)]
177enum KeyInner {
178    ECDH_P256(LcPtr<EVP_PKEY>),
179    ECDH_P384(LcPtr<EVP_PKEY>),
180    ECDH_P521(LcPtr<EVP_PKEY>),
181    X25519(LcPtr<EVP_PKEY>),
182}
183
184impl Clone for KeyInner {
185    fn clone(&self) -> KeyInner {
186        match self {
187            KeyInner::ECDH_P256(evp_pkey) => KeyInner::ECDH_P256(evp_pkey.clone()),
188            KeyInner::ECDH_P384(evp_pkey) => KeyInner::ECDH_P384(evp_pkey.clone()),
189            KeyInner::ECDH_P521(evp_pkey) => KeyInner::ECDH_P521(evp_pkey.clone()),
190            KeyInner::X25519(evp_pkey) => KeyInner::X25519(evp_pkey.clone()),
191        }
192    }
193}
194
195/// A private key for use (only) with `agree`. The
196/// signature of `agree` allows `PrivateKey` to be
197/// used for more than one key agreement.
198pub struct PrivateKey {
199    inner_key: KeyInner,
200}
201
202impl KeyInner {
203    #[inline]
204    fn algorithm(&self) -> &'static Algorithm {
205        match self {
206            KeyInner::ECDH_P256(..) => &ECDH_P256,
207            KeyInner::ECDH_P384(..) => &ECDH_P384,
208            KeyInner::ECDH_P521(..) => &ECDH_P521,
209            KeyInner::X25519(..) => &X25519,
210        }
211    }
212
213    fn get_evp_pkey(&self) -> &LcPtr<EVP_PKEY> {
214        match self {
215            KeyInner::ECDH_P256(evp_pkey)
216            | KeyInner::ECDH_P384(evp_pkey)
217            | KeyInner::ECDH_P521(evp_pkey)
218            | KeyInner::X25519(evp_pkey) => evp_pkey,
219        }
220    }
221}
222
223unsafe impl Send for PrivateKey {}
224
225// https://github.com/awslabs/aws-lc/blob/main/include/openssl/ec_key.h#L88
226// An |EC_KEY| object represents a public or private EC key. A given object may
227// be used concurrently on multiple threads by non-mutating functions, provided
228// no other thread is concurrently calling a mutating function. Unless otherwise
229// documented, functions which take a |const| pointer are non-mutating and
230// functions which take a non-|const| pointer are mutating.
231unsafe impl Sync for PrivateKey {}
232
233impl Debug for PrivateKey {
234    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
235        f.write_str(&format!(
236            "PrivateKey {{ algorithm: {:?} }}",
237            self.inner_key.algorithm()
238        ))
239    }
240}
241
242impl PrivateKey {
243    fn new(alg: &'static Algorithm, evp_pkey: LcPtr<EVP_PKEY>) -> Self {
244        match alg.id {
245            AlgorithmID::X25519 => Self {
246                inner_key: KeyInner::X25519(evp_pkey),
247            },
248            AlgorithmID::ECDH_P256 => Self {
249                inner_key: KeyInner::ECDH_P256(evp_pkey),
250            },
251            AlgorithmID::ECDH_P384 => Self {
252                inner_key: KeyInner::ECDH_P384(evp_pkey),
253            },
254            AlgorithmID::ECDH_P521 => Self {
255                inner_key: KeyInner::ECDH_P521(evp_pkey),
256            },
257        }
258    }
259
260    #[inline]
261    /// Generate a new private key for the given algorithm.
262    // # FIPS
263    // Use this function with one of the following algorithms:
264    // * `ECDH_P256`
265    // * `ECDH_P384`
266    // * `ECDH_P521`
267    //
268    /// # Errors
269    /// `error::Unspecified` when operation fails due to internal error.
270    pub fn generate(alg: &'static Algorithm) -> Result<Self, Unspecified> {
271        let evp_pkey = match alg.id {
272            AlgorithmID::X25519 => generate_x25519()?,
273            _ => evp_key_generate(alg.id.nid())?,
274        };
275        Ok(Self::new(alg, evp_pkey))
276    }
277
278    /// Deserializes a DER-encoded private key structure to produce a `agreement::PrivateKey`.
279    ///
280    /// This function is typically used to deserialize RFC 5915 encoded private keys, but it will
281    /// attempt to automatically detect other key formats. This function supports unencrypted
282    /// PKCS#8 `PrivateKeyInfo` structures as well as key type specific formats.
283    ///
284    /// X25519 keys are not supported. See `PrivateKey::as_der`.
285    ///
286    /// # Errors
287    /// `error::KeyRejected` if parsing failed or key otherwise unacceptable.
288    ///
289    /// # Panics
290    pub fn from_private_key_der(
291        alg: &'static Algorithm,
292        key_bytes: &[u8],
293    ) -> Result<Self, KeyRejected> {
294        if AlgorithmID::X25519 == alg.id {
295            return Err(KeyRejected::invalid_encoding());
296        }
297        let evp_pkey = LcPtr::<EVP_PKEY>::parse_rfc5208_private_key(key_bytes, EVP_PKEY_EC)
298            .or(parse_rfc5915_private_key(key_bytes, alg.id.nid()))?;
299        #[cfg(not(feature = "fips"))]
300        verify_evp_key_nid(&evp_pkey.as_const(), alg.id.nid())?;
301        #[cfg(feature = "fips")]
302        validate_ec_evp_key(&evp_pkey.as_const(), alg.id.nid())?;
303
304        Ok(Self::new(alg, evp_pkey))
305    }
306
307    /// Constructs an ECDH key from private key bytes
308    ///
309    /// The private key must encoded as a big-endian fixed-length integer. For
310    /// example, a P-256 private key must be 32 bytes prefixed with leading
311    /// zeros as needed.
312    ///
313    /// # Errors
314    /// `error::KeyRejected` if parsing failed or key otherwise unacceptable.
315    pub fn from_private_key(
316        alg: &'static Algorithm,
317        key_bytes: &[u8],
318    ) -> Result<Self, KeyRejected> {
319        if key_bytes.len() != alg.id.private_key_len() {
320            return Err(KeyRejected::wrong_algorithm());
321        }
322        let evp_pkey = if AlgorithmID::X25519 == alg.id {
323            LcPtr::<EVP_PKEY>::parse_raw_private_key(key_bytes, EVP_PKEY_X25519)?
324        } else {
325            parse_sec1_private_bn(key_bytes, alg.id.nid())?
326        };
327        Ok(Self::new(alg, evp_pkey))
328    }
329
330    #[cfg(test)]
331    #[allow(missing_docs, clippy::missing_errors_doc)]
332    pub fn generate_for_test(
333        alg: &'static Algorithm,
334        rng: &dyn crate::rand::SecureRandom,
335    ) -> Result<Self, Unspecified> {
336        match alg.id {
337            AlgorithmID::X25519 => {
338                let mut priv_key = [0u8; AlgorithmID::X25519.private_key_len()];
339                rng.fill(&mut priv_key)?;
340                Self::from_x25519_private_key(&priv_key)
341            }
342            AlgorithmID::ECDH_P256 => {
343                let mut priv_key = [0u8; AlgorithmID::ECDH_P256.private_key_len()];
344                rng.fill(&mut priv_key)?;
345                Self::from_p256_private_key(&priv_key)
346            }
347            AlgorithmID::ECDH_P384 => {
348                let mut priv_key = [0u8; AlgorithmID::ECDH_P384.private_key_len()];
349                rng.fill(&mut priv_key)?;
350                Self::from_p384_private_key(&priv_key)
351            }
352            AlgorithmID::ECDH_P521 => {
353                let mut priv_key = [0u8; AlgorithmID::ECDH_P521.private_key_len()];
354                rng.fill(&mut priv_key)?;
355                Self::from_p521_private_key(&priv_key)
356            }
357        }
358    }
359
360    #[cfg(test)]
361    fn from_x25519_private_key(
362        priv_key: &[u8; AlgorithmID::X25519.private_key_len()],
363    ) -> Result<Self, Unspecified> {
364        let pkey = LcPtr::<EVP_PKEY>::parse_raw_private_key(priv_key, EVP_PKEY_X25519)?;
365
366        Ok(PrivateKey {
367            inner_key: KeyInner::X25519(pkey),
368        })
369    }
370
371    #[cfg(test)]
372    fn from_p256_private_key(priv_key: &[u8]) -> Result<Self, Unspecified> {
373        let pkey = parse_sec1_private_bn(priv_key, ECDH_P256.id.nid())?;
374        Ok(PrivateKey {
375            inner_key: KeyInner::ECDH_P256(pkey),
376        })
377    }
378
379    #[cfg(test)]
380    fn from_p384_private_key(priv_key: &[u8]) -> Result<Self, Unspecified> {
381        let pkey = parse_sec1_private_bn(priv_key, ECDH_P384.id.nid())?;
382        Ok(PrivateKey {
383            inner_key: KeyInner::ECDH_P384(pkey),
384        })
385    }
386
387    #[cfg(test)]
388    fn from_p521_private_key(priv_key: &[u8]) -> Result<Self, Unspecified> {
389        let pkey = parse_sec1_private_bn(priv_key, ECDH_P521.id.nid())?;
390        Ok(PrivateKey {
391            inner_key: KeyInner::ECDH_P521(pkey),
392        })
393    }
394
395    /// Computes the public key from the private key.
396    ///
397    /// # Errors
398    /// `error::Unspecified` when operation fails due to internal error.
399    pub fn compute_public_key(&self) -> Result<PublicKey, Unspecified> {
400        match &self.inner_key {
401            KeyInner::ECDH_P256(evp_pkey)
402            | KeyInner::ECDH_P384(evp_pkey)
403            | KeyInner::ECDH_P521(evp_pkey) => {
404                let mut public_key = [0u8; MAX_PUBLIC_KEY_LEN];
405                let len = marshal_sec1_public_point_into_buffer(&mut public_key, evp_pkey, false)?;
406                Ok(PublicKey {
407                    inner_key: self.inner_key.clone(),
408                    key_bytes: public_key,
409                    len,
410                })
411            }
412            KeyInner::X25519(priv_key) => {
413                let mut buffer = [0u8; MAX_PUBLIC_KEY_LEN];
414                let out_len = priv_key
415                    .as_const()
416                    .marshal_raw_public_to_buffer(&mut buffer)?;
417                Ok(PublicKey {
418                    inner_key: self.inner_key.clone(),
419                    key_bytes: buffer,
420                    len: out_len,
421                })
422            }
423        }
424    }
425
426    /// The algorithm for the private key.
427    #[inline]
428    #[must_use]
429    pub fn algorithm(&self) -> &'static Algorithm {
430        self.inner_key.algorithm()
431    }
432}
433
434impl AsDer<EcPrivateKeyRfc5915Der<'static>> for PrivateKey {
435    /// Serializes the key as a DER-encoded `ECPrivateKey` (RFC 5915) structure.
436    ///
437    /// X25519 is not supported.
438    ///
439    /// # Errors
440    /// `error::Unspecified`  if serialization failed.
441    fn as_der(&self) -> Result<EcPrivateKeyRfc5915Der<'static>, Unspecified> {
442        if AlgorithmID::X25519 == self.inner_key.algorithm().id {
443            return Err(Unspecified);
444        }
445
446        let mut outp = null_mut::<u8>();
447        let ec_key = {
448            self.inner_key
449                .get_evp_pkey()
450                .project_const_lifetime(unsafe {
451                    |evp_pkey| EVP_PKEY_get0_EC_KEY(*evp_pkey.as_const())
452                })?
453        };
454        let length = usize::try_from(unsafe { i2d_ECPrivateKey(*ec_key, &mut outp) })
455            .map_err(|_| Unspecified)?;
456        let mut outp = LcPtr::new(outp)?;
457        Ok(EcPrivateKeyRfc5915Der::take_from_slice(unsafe {
458            core::slice::from_raw_parts_mut(*outp.as_mut(), length)
459        }))
460    }
461}
462
463impl AsDer<Pkcs8V1Der<'static>> for PrivateKey {
464    /// Serializes the key as a PKCS #8 private key structure.
465    ///
466    /// X25519 is not supported.
467    ///
468    /// # Errors
469    /// `error::Unspecified`  if serialization failed.
470    fn as_der(&self) -> Result<Pkcs8V1Der<'static>, Unspecified> {
471        if AlgorithmID::X25519 == self.inner_key.algorithm().id {
472            return Err(Unspecified);
473        }
474
475        Ok(Pkcs8V1Der::new(
476            self.inner_key
477                .get_evp_pkey()
478                .as_const()
479                .marshal_rfc5208_private_key(Version::V1)?,
480        ))
481    }
482}
483
484impl AsBigEndian<EcPrivateKeyBin<'static>> for PrivateKey {
485    /// Exposes the private key encoded as a big-endian fixed-length integer.
486    ///
487    /// X25519 is not supported.
488    ///
489    /// # Errors
490    /// `error::Unspecified` if serialization failed.
491    fn as_be_bytes(&self) -> Result<EcPrivateKeyBin<'static>, Unspecified> {
492        if AlgorithmID::X25519 == self.inner_key.algorithm().id {
493            return Err(Unspecified);
494        }
495        let buffer = marshal_sec1_private_key(self.inner_key.get_evp_pkey())?;
496        Ok(EcPrivateKeyBin::new(buffer))
497    }
498}
499
500impl AsBigEndian<Curve25519SeedBin<'static>> for PrivateKey {
501    /// Exposes the seed encoded as a big-endian fixed-length integer.
502    ///
503    /// Only X25519 is supported.
504    ///
505    /// # Errors
506    /// `error::Unspecified` if serialization failed.
507    fn as_be_bytes(&self) -> Result<Curve25519SeedBin<'static>, Unspecified> {
508        if AlgorithmID::X25519 != self.inner_key.algorithm().id {
509            return Err(Unspecified);
510        }
511        let evp_pkey = self.inner_key.get_evp_pkey();
512        Ok(Curve25519SeedBin::new(
513            evp_pkey.as_const().marshal_raw_private_key()?,
514        ))
515    }
516}
517
518pub(crate) fn generate_x25519() -> Result<LcPtr<EVP_PKEY>, Unspecified> {
519    LcPtr::<EVP_PKEY>::generate(EVP_PKEY_X25519, No_EVP_PKEY_CTX_consumer)
520}
521
522const MAX_PUBLIC_KEY_LEN: usize = ec::PUBLIC_KEY_MAX_LEN;
523
524/// A public key for key agreement.
525pub struct PublicKey {
526    inner_key: KeyInner,
527    key_bytes: [u8; MAX_PUBLIC_KEY_LEN],
528    len: usize,
529}
530
531impl PublicKey {
532    /// The algorithm for the public key.
533    #[must_use]
534    pub fn algorithm(&self) -> &'static Algorithm {
535        self.inner_key.algorithm()
536    }
537}
538
539unsafe impl Send for PublicKey {}
540unsafe impl Sync for PublicKey {}
541
542impl Debug for PublicKey {
543    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
544        f.write_str(&format!(
545            "PublicKey {{ algorithm: {:?}, bytes: \"{}\" }}",
546            self.inner_key.algorithm(),
547            hex::encode(&self.key_bytes[0..self.len])
548        ))
549    }
550}
551
552impl AsRef<[u8]> for PublicKey {
553    /// Serializes the public key in an uncompressed form (X9.62) using the
554    /// Octet-String-to-Elliptic-Curve-Point algorithm in
555    /// [SEC 1: Elliptic Curve Cryptography, Version 2.0].
556    fn as_ref(&self) -> &[u8] {
557        &self.key_bytes[0..self.len]
558    }
559}
560
561impl Clone for PublicKey {
562    fn clone(&self) -> Self {
563        PublicKey {
564            inner_key: self.inner_key.clone(),
565            key_bytes: self.key_bytes,
566            len: self.len,
567        }
568    }
569}
570
571impl AsDer<PublicKeyX509Der<'static>> for PublicKey {
572    /// Provides the public key as a DER-encoded (X.509) `SubjectPublicKeyInfo` structure.
573    /// # Errors
574    /// Returns an error if the public key fails to marshal to X.509.
575    fn as_der(&self) -> Result<PublicKeyX509Der<'static>, crate::error::Unspecified> {
576        match &self.inner_key {
577            KeyInner::ECDH_P256(evp_pkey)
578            | KeyInner::ECDH_P384(evp_pkey)
579            | KeyInner::ECDH_P521(evp_pkey)
580            | KeyInner::X25519(evp_pkey) => {
581                let der = evp_pkey.as_const().marshal_rfc5280_public_key()?;
582                Ok(PublicKeyX509Der::from(Buffer::new(der)))
583            }
584        }
585    }
586}
587
588impl AsBigEndian<EcPublicKeyCompressedBin<'static>> for PublicKey {
589    /// Provides the public key elliptic curve point to a compressed point format.
590    /// # Errors
591    /// Returns an error if the underlying implementation is unable to marshal the public key to this format.
592    fn as_be_bytes(&self) -> Result<EcPublicKeyCompressedBin<'static>, crate::error::Unspecified> {
593        let evp_pkey = match &self.inner_key {
594            KeyInner::ECDH_P256(evp_pkey)
595            | KeyInner::ECDH_P384(evp_pkey)
596            | KeyInner::ECDH_P521(evp_pkey) => evp_pkey,
597            KeyInner::X25519(_) => return Err(Unspecified),
598        };
599        let pub_point = marshal_sec1_public_point(evp_pkey, true)?;
600        Ok(EcPublicKeyCompressedBin::new(pub_point))
601    }
602}
603
604impl AsBigEndian<EcPublicKeyUncompressedBin<'static>> for PublicKey {
605    /// Provides the public key elliptic curve point to a compressed point format.
606    ///
607    /// Equivalent to [`PublicKey::as_ref`] for ECDH key types, except that it provides you a copy instead of a reference.
608    ///
609    /// # Errors
610    /// Returns an error if the underlying implementation is unable to marshal the public key to this format.
611    fn as_be_bytes(
612        &self,
613    ) -> Result<EcPublicKeyUncompressedBin<'static>, crate::error::Unspecified> {
614        if self.algorithm().id == AlgorithmID::X25519 {
615            return Err(Unspecified);
616        }
617
618        let mut buffer = vec![0u8; self.len];
619        buffer.copy_from_slice(&self.key_bytes[0..self.len]);
620
621        Ok(EcPublicKeyUncompressedBin::new(buffer))
622    }
623}
624
625/// An unparsed, possibly malformed, public key for key agreement.
626#[derive(Clone)]
627pub struct UnparsedPublicKey<B: AsRef<[u8]>> {
628    alg: &'static Algorithm,
629    bytes: B,
630}
631
632impl<B: Copy + AsRef<[u8]>> Copy for UnparsedPublicKey<B> {}
633
634impl<B: Debug + AsRef<[u8]>> Debug for UnparsedPublicKey<B> {
635    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
636        f.write_str(&format!(
637            "UnparsedPublicKey {{ algorithm: {:?}, bytes: {:?} }}",
638            self.alg,
639            hex::encode(self.bytes.as_ref())
640        ))
641    }
642}
643
644impl<B: AsRef<[u8]>> UnparsedPublicKey<B> {
645    /// Constructs a new `UnparsedPublicKey`.
646    pub fn new(algorithm: &'static Algorithm, bytes: B) -> Self {
647        UnparsedPublicKey {
648            alg: algorithm,
649            bytes,
650        }
651    }
652
653    /// The agreement algorithm associated with this public key
654    pub fn algorithm(&self) -> &'static Algorithm {
655        self.alg
656    }
657
658    /// The bytes provided for this public key
659    pub fn bytes(&self) -> &B {
660        &self.bytes
661    }
662}
663
664/// A parsed public key for key agreement.
665///
666/// This represents a public key that has been successfully parsed and validated
667/// from its encoded form. The key can be used with the `agree` function to
668/// perform key agreement operations.
669#[derive(Debug)]
670pub struct ParsedPublicKey {
671    format: ParsedPublicKeyFormat,
672    nid: i32,
673    key: LcPtr<EVP_PKEY>,
674}
675
676unsafe impl Send for ParsedPublicKey {}
677unsafe impl Sync for ParsedPublicKey {}
678
679#[derive(Clone, Copy, PartialEq, Eq, Debug)]
680/// The format of a parsed public key.
681///
682/// This is used to distinguish between different types of public key formats
683/// supported by *aws-lc-rs*.
684#[non_exhaustive]
685pub enum ParsedPublicKeyFormat {
686    /// The key is in an X.509 SubjectPublicKeyInfo format.
687    X509,
688    /// The key is in an uncompressed form (X9.62).
689    Uncompressed,
690    /// The key is in a compressed form (SEC 1: Elliptic Curve Cryptography, Version 2.0).
691    Compressed,
692    /// The key is in a hybrid form (SEC 1: Elliptic Curve Cryptography, Version 2.0).
693    Hybrid,
694    /// The key is in a raw form. (X25519 only)
695    Raw,
696    /// The key is in an unknown format.
697    Unknown,
698}
699
700/// A parsed public key for key agreement.
701impl ParsedPublicKey {
702    fn nid(&self) -> i32 {
703        self.nid
704    }
705
706    /// The format of the data the public key was parsed from.
707    #[must_use]
708    pub fn format(&self) -> ParsedPublicKeyFormat {
709        self.format
710    }
711
712    pub(crate) fn key(&self) -> &LcPtr<EVP_PKEY> {
713        &self.key
714    }
715
716    /// The algorithm of the public key.
717    #[must_use]
718    #[allow(non_upper_case_globals)]
719    pub fn alg(&self) -> &'static Algorithm {
720        match self.nid() {
721            NID_X25519 => &X25519,
722            NID_X9_62_prime256v1 => &ECDH_P256,
723            NID_secp384r1 => &ECDH_P384,
724            NID_secp521r1 => &ECDH_P521,
725            _ => unreachable!("Unreachable agreement algorithm nid: {}", self.nid()),
726        }
727    }
728}
729
730impl ParsedPublicKey {
731    #[allow(non_upper_case_globals)]
732    pub(crate) fn new(bytes: impl AsRef<[u8]>, nid: i32) -> Result<Self, KeyRejected> {
733        let key_bytes = bytes.as_ref();
734        if key_bytes.is_empty() {
735            return Err(KeyRejected::unspecified());
736        }
737        match nid {
738            NID_X25519 => {
739                let format: ParsedPublicKeyFormat;
740                let key = if let Ok(evp_pkey) =
741                    LcPtr::<EVP_PKEY>::parse_rfc5280_public_key(key_bytes, EVP_PKEY_X25519)
742                {
743                    format = ParsedPublicKeyFormat::X509;
744                    evp_pkey
745                } else {
746                    format = ParsedPublicKeyFormat::Raw;
747                    try_parse_x25519_public_key_raw_bytes(key_bytes)?
748                };
749
750                Ok(ParsedPublicKey { format, nid, key })
751            }
752            NID_X9_62_prime256v1 | NID_secp384r1 | NID_secp521r1 => {
753                let format: ParsedPublicKeyFormat;
754                let key = if let Ok(evp_pkey) =
755                    LcPtr::<EVP_PKEY>::parse_rfc5280_public_key(key_bytes, EVP_PKEY_EC)
756                {
757                    validate_ec_evp_key(&evp_pkey.as_const(), nid)?;
758                    format = ParsedPublicKeyFormat::X509;
759                    evp_pkey
760                } else if let Ok(evp_pkey) = parse_sec1_public_point(key_bytes, nid) {
761                    format = match key_bytes[0] {
762                        0x02 | 0x03 => ParsedPublicKeyFormat::Compressed,
763                        0x04 => ParsedPublicKeyFormat::Uncompressed,
764                        0x06 | 0x07 => ParsedPublicKeyFormat::Hybrid,
765                        _ => ParsedPublicKeyFormat::Unknown,
766                    };
767                    evp_pkey
768                } else {
769                    return Err(KeyRejected::invalid_encoding());
770                };
771
772                Ok(ParsedPublicKey { format, nid, key })
773            }
774            _ => Err(KeyRejected::unspecified()),
775        }
776    }
777}
778
779impl<B: AsRef<[u8]>> UnparsedPublicKey<B> {
780    #[allow(dead_code)]
781    fn parse(&self) -> Result<ParsedPublicKey, KeyRejected> {
782        ParsedPublicKey::new(&self.bytes, self.alg.id.nid())
783    }
784}
785
786impl<B: AsRef<[u8]>> TryFrom<&UnparsedPublicKey<B>> for ParsedPublicKey {
787    type Error = KeyRejected;
788    fn try_from(upk: &UnparsedPublicKey<B>) -> Result<Self, Self::Error> {
789        upk.parse()
790    }
791}
792
793impl<B: AsRef<[u8]>> TryFrom<UnparsedPublicKey<B>> for ParsedPublicKey {
794    type Error = KeyRejected;
795    fn try_from(upk: UnparsedPublicKey<B>) -> Result<Self, Self::Error> {
796        upk.parse()
797    }
798}
799
800/// Performs a key agreement with a private key and the given public key.
801///
802/// `my_private_key` is the private key to use. Only a reference to the key
803/// is required, allowing the key to continue to be used.
804///
805/// `peer_public_key` is the peer's public key. `agree` will return
806/// `Err(error_value)` if it does not match `my_private_key's` algorithm/curve.
807/// `agree` verifies that it is encoded in the standard form for the
808/// algorithm and that the key is *valid*; see the algorithm's documentation for
809/// details on how keys are to be encoded and what constitutes a valid key for
810/// that algorithm.
811///
812/// `error_value` is the value to return if an error occurs before `kdf` is
813/// called, e.g. when decoding of the peer's public key fails or when the public
814/// key is otherwise invalid.
815///
816/// After the key agreement is done, `agree` calls `kdf` with the raw
817/// key material from the key agreement operation and then returns what `kdf`
818/// returns.
819// # FIPS
820// Use this function with one of the following key algorithms:
821// * `ECDH_P256`
822// * `ECDH_P384`
823// * `ECDH_P521`
824//
825/// # Errors
826/// `error_value` on internal failure.
827#[inline]
828#[allow(clippy::missing_panics_doc)]
829pub fn agree<B: TryInto<ParsedPublicKey>, F, R, E>(
830    my_private_key: &PrivateKey,
831    peer_public_key: B,
832    error_value: E,
833    kdf: F,
834) -> Result<R, E>
835where
836    F: FnOnce(&[u8]) -> Result<R, E>,
837{
838    let expected_alg = my_private_key.algorithm();
839
840    let parse_result = peer_public_key.try_into();
841
842    if let Ok(peer_pub_key) = parse_result {
843        if peer_pub_key.alg() != expected_alg {
844            return Err(error_value);
845        }
846        let secret = my_private_key
847            .inner_key
848            .get_evp_pkey()
849            .agree(peer_pub_key.key())
850            .or(Err(error_value))?;
851
852        kdf(secret.as_ref())
853    } else {
854        Err(error_value)
855    }
856}
857
858fn try_parse_x25519_public_key_raw_bytes(key_bytes: &[u8]) -> Result<LcPtr<EVP_PKEY>, KeyRejected> {
859    let expected_pub_key_len = X25519.id.pub_key_len();
860    if key_bytes.len() != expected_pub_key_len {
861        return Err(KeyRejected::invalid_encoding());
862    }
863
864    LcPtr::<EVP_PKEY>::parse_raw_public_key(key_bytes, EVP_PKEY_X25519)
865}
866
867#[cfg(test)]
868mod agreement_tests;
869#[cfg(test)]
870mod parsed_public_key_tests;