portable_rustls/crypto/
hpke.rs

1use alloc::boxed::Box;
2use alloc::vec::Vec;
3use core::fmt::Debug;
4
5use zeroize::Zeroize;
6
7use crate::msgs::enums::HpkeKem;
8use crate::msgs::handshake::HpkeSymmetricCipherSuite;
9use crate::Error;
10
11/// An HPKE suite, specifying a key encapsulation mechanism and a symmetric cipher suite.
12#[derive(Clone, Copy, Debug, Eq, PartialEq)]
13pub struct HpkeSuite {
14    /// The choice of HPKE key encapsulation mechanism.
15    pub kem: HpkeKem,
16
17    /// The choice of HPKE symmetric cipher suite.
18    ///
19    /// This combines a choice of authenticated encryption with additional data (AEAD) algorithm
20    /// and a key derivation function (KDF).
21    pub sym: HpkeSymmetricCipherSuite,
22}
23
24/// An HPKE instance that can be used for base-mode single-shot encryption and decryption.
25pub trait Hpke: Debug + Send + Sync {
26    /// Seal the provided `plaintext` to the recipient public key `pub_key` with application supplied
27    /// `info`, and additional data `aad`.
28    ///
29    /// Returns ciphertext that can be used with [Self::open] by the recipient to recover plaintext
30    /// using the same `info` and `aad` and the private key corresponding to `pub_key`. RFC 9180
31    /// refers to `pub_key` as `pkR`.
32    fn seal(
33        &self,
34        info: &[u8],
35        aad: &[u8],
36        plaintext: &[u8],
37        pub_key: &HpkePublicKey,
38    ) -> Result<(EncapsulatedSecret, Vec<u8>), Error>;
39
40    /// Set up a sealer context for the receiver public key `pub_key` with application supplied `info`.
41    ///
42    /// Returns both an encapsulated ciphertext and a sealer context that can be used to seal
43    /// messages to the recipient. RFC 9180 refers to `pub_key` as `pkR`.
44    fn setup_sealer(
45        &self,
46        info: &[u8],
47        pub_key: &HpkePublicKey,
48    ) -> Result<(EncapsulatedSecret, Box<dyn HpkeSealer + 'static>), Error>;
49
50    /// Open the provided `ciphertext` using the encapsulated secret `enc`, with application
51    /// supplied `info`, and additional data `aad`.
52    ///
53    /// Returns plaintext if  the `info` and `aad` match those used with [Self::seal], and
54    /// decryption with `secret_key` succeeds. RFC 9180 refers to `secret_key` as `skR`.
55    fn open(
56        &self,
57        enc: &EncapsulatedSecret,
58        info: &[u8],
59        aad: &[u8],
60        ciphertext: &[u8],
61        secret_key: &HpkePrivateKey,
62    ) -> Result<Vec<u8>, Error>;
63
64    /// Set up an opener context for the secret key `secret_key` with application supplied `info`.
65    ///
66    /// Returns an opener context that can be used to open sealed messages encrypted to the
67    /// public key corresponding to `secret_key`. RFC 9180 refers to `secret_key` as `skR`.
68    fn setup_opener(
69        &self,
70        enc: &EncapsulatedSecret,
71        info: &[u8],
72        secret_key: &HpkePrivateKey,
73    ) -> Result<Box<dyn HpkeOpener + 'static>, Error>;
74
75    /// Generate a new public key and private key pair compatible with this HPKE instance.
76    ///
77    /// Key pairs should be encoded as raw big endian fixed length integers sized based
78    /// on the suite's DH KEM algorithm.
79    fn generate_key_pair(&self) -> Result<(HpkePublicKey, HpkePrivateKey), Error>;
80
81    /// Return whether the HPKE instance is FIPS compatible.
82    #[cfg(unstable_api_not_supported)] // [FIPS REMOVED FROM THIS FORK]
83    fn fips(&self) -> bool {
84        false
85    }
86
87    /// Return the [HpkeSuite] that this HPKE instance supports.
88    fn suite(&self) -> HpkeSuite;
89}
90
91/// An HPKE sealer context.
92///
93/// This is a stateful object that can be used to seal messages for receipt by
94/// a receiver.
95pub trait HpkeSealer: Debug + Send + Sync + 'static {
96    /// Seal the provided `plaintext` with additional data `aad`, returning
97    /// ciphertext.
98    fn seal(&mut self, aad: &[u8], plaintext: &[u8]) -> Result<Vec<u8>, Error>;
99}
100
101/// An HPKE opener context.
102///
103/// This is a stateful object that can be used to open sealed messages sealed
104/// by a sender.
105pub trait HpkeOpener: Debug + Send + Sync + 'static {
106    /// Open the provided `ciphertext` with additional data `aad`, returning plaintext.
107    fn open(&mut self, aad: &[u8], ciphertext: &[u8]) -> Result<Vec<u8>, Error>;
108}
109
110/// An HPKE public key.
111#[derive(Clone, Debug)]
112pub struct HpkePublicKey(pub Vec<u8>);
113
114/// An HPKE private key.
115pub struct HpkePrivateKey(Vec<u8>);
116
117impl HpkePrivateKey {
118    /// Return the private key bytes.
119    pub fn secret_bytes(&self) -> &[u8] {
120        self.0.as_slice()
121    }
122}
123
124impl From<Vec<u8>> for HpkePrivateKey {
125    fn from(bytes: Vec<u8>) -> Self {
126        Self(bytes)
127    }
128}
129
130impl Drop for HpkePrivateKey {
131    fn drop(&mut self) {
132        self.0.zeroize();
133    }
134}
135
136/// An HPKE key pair, made of a matching public and private key.
137pub struct HpkeKeyPair {
138    /// A HPKE public key.
139    pub public_key: HpkePublicKey,
140    /// A HPKE private key.
141    pub private_key: HpkePrivateKey,
142}
143
144/// An encapsulated secret returned from setting up a sender or receiver context.
145#[derive(Debug)]
146pub struct EncapsulatedSecret(pub Vec<u8>);