bc_components/mlkem/
mlkem_private_key.rs

1use anyhow::{anyhow, bail, Error, Result};
2use dcbor::prelude::*;
3use pqcrypto_mlkem::*;
4use pqcrypto_traits::kem::{SecretKey, SharedSecret};
5
6use crate::{tags, Decrypter, EncapsulationPrivateKey, SymmetricKey};
7
8use super::{MLKEMCiphertext, MLKEM};
9
10/// A private key for the ML-KEM post-quantum key encapsulation mechanism.
11///
12/// `MLKEMPrivateKey` represents a private key that can be used to decapsulate shared secrets
13/// using the ML-KEM (Module Lattice-based Key Encapsulation Mechanism) post-quantum algorithm.
14/// It supports multiple security levels through the variants:
15///
16/// - `MLKEM512`: NIST security level 1 (roughly equivalent to AES-128), 1632 bytes
17/// - `MLKEM768`: NIST security level 3 (roughly equivalent to AES-192), 2400 bytes
18/// - `MLKEM1024`: NIST security level 5 (roughly equivalent to AES-256), 3168 bytes
19///
20/// # Security
21///
22/// ML-KEM private keys should be kept secure and never exposed. They provide 
23/// resistance against attacks from both classical and quantum computers.
24///
25/// # Examples
26///
27/// ```
28/// use bc_components::MLKEM;
29///
30/// // Generate a keypair
31/// let (private_key, public_key) = MLKEM::MLKEM512.keypair();
32///
33/// // Party A encapsulates a shared secret using the public key
34/// let (shared_secret_a, ciphertext) = public_key.encapsulate_new_shared_secret();
35///
36/// // Party B decapsulates the shared secret using the private key and ciphertext
37/// let shared_secret_b = private_key.decapsulate_shared_secret(&ciphertext).unwrap();
38///
39/// // Both parties now have the same shared secret
40/// assert_eq!(shared_secret_a, shared_secret_b);
41/// ```
42#[derive(Clone, PartialEq)]
43pub enum MLKEMPrivateKey {
44    /// An ML-KEM-512 private key (NIST security level 1)
45    MLKEM512(Box<mlkem512::SecretKey>),
46    /// An ML-KEM-768 private key (NIST security level 3)
47    MLKEM768(Box<mlkem768::SecretKey>),
48    /// An ML-KEM-1024 private key (NIST security level 5)
49    MLKEM1024(Box<mlkem1024::SecretKey>),
50}
51
52impl Eq for MLKEMPrivateKey {}
53
54/// Implements hashing for ML-KEM private keys.
55impl std::hash::Hash for MLKEMPrivateKey {
56    /// Hashes the raw bytes of the private key.
57    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
58        match self {
59            MLKEMPrivateKey::MLKEM512(sk) => sk.as_bytes().hash(state),
60            MLKEMPrivateKey::MLKEM768(sk) => sk.as_bytes().hash(state),
61            MLKEMPrivateKey::MLKEM1024(sk) => sk.as_bytes().hash(state),
62        }
63    }
64}
65
66impl MLKEMPrivateKey {
67    /// Returns the security level of this ML-KEM private key.
68    pub fn level(&self) -> MLKEM {
69        match self {
70            MLKEMPrivateKey::MLKEM512(_) => MLKEM::MLKEM512,
71            MLKEMPrivateKey::MLKEM768(_) => MLKEM::MLKEM768,
72            MLKEMPrivateKey::MLKEM1024(_) => MLKEM::MLKEM1024,
73        }
74    }
75
76    /// Returns the size of this ML-KEM private key in bytes.
77    pub fn size(&self) -> usize {
78        self.level().private_key_size()
79    }
80
81    /// Returns the raw bytes of this ML-KEM private key.
82    pub fn as_bytes(&self) -> &[u8] {
83        match self {
84            MLKEMPrivateKey::MLKEM512(sk) => sk.as_ref().as_bytes(),
85            MLKEMPrivateKey::MLKEM768(sk) => sk.as_ref().as_bytes(),
86            MLKEMPrivateKey::MLKEM1024(sk) => sk.as_ref().as_bytes(),
87        }
88    }
89
90    /// Creates an ML-KEM private key from raw bytes and a security level.
91    ///
92    /// # Parameters
93    ///
94    /// * `level` - The security level of the key.
95    /// * `bytes` - The raw bytes of the key.
96    ///
97    /// # Returns
98    ///
99    /// An `MLKEMPrivateKey` if the bytes represent a valid key for the given level,
100    /// or an error otherwise.
101    ///
102    /// # Errors
103    ///
104    /// Returns an error if the bytes do not represent a valid ML-KEM private key
105    /// for the specified security level.
106    pub fn from_bytes(level: MLKEM, bytes: &[u8]) -> Result<Self> {
107        match level {
108            MLKEM::MLKEM512 => Ok(MLKEMPrivateKey::MLKEM512(Box::new(
109                mlkem512::SecretKey::from_bytes(bytes).map_err(|e| anyhow!(e))?,
110            ))),
111            MLKEM::MLKEM768 => Ok(MLKEMPrivateKey::MLKEM768(Box::new(
112                mlkem768::SecretKey::from_bytes(bytes).map_err(|e| anyhow!(e))?,
113            ))),
114            MLKEM::MLKEM1024 => Ok(MLKEMPrivateKey::MLKEM1024(Box::new(
115                mlkem1024::SecretKey::from_bytes(bytes).map_err(|e| anyhow!(e))?,
116            ))),
117        }
118    }
119
120    /// Decapsulates a shared secret from a ciphertext using this private key.
121    ///
122    /// # Parameters
123    ///
124    /// * `ciphertext` - The ciphertext containing the encapsulated shared secret.
125    ///
126    /// # Returns
127    ///
128    /// A `SymmetricKey` containing the decapsulated shared secret, or an error
129    /// if decapsulation fails.
130    ///
131    /// # Errors
132    ///
133    /// Returns an error if the security level of the ciphertext doesn't match the
134    /// security level of this private key, or if decapsulation fails for any other reason.
135    ///
136    /// # Panics
137    ///
138    /// Panics if the security level of the ciphertext doesn't match the security
139    /// level of this private key.
140    pub fn decapsulate_shared_secret(&self, ciphertext: &MLKEMCiphertext) -> Result<SymmetricKey> {
141        match (self, ciphertext) {
142            (MLKEMPrivateKey::MLKEM512(sk), MLKEMCiphertext::MLKEM512(ct)) => {
143                let ss = mlkem512::decapsulate(ct.as_ref(), sk.as_ref());
144                SymmetricKey::from_data_ref(ss.as_bytes())
145            }
146            (MLKEMPrivateKey::MLKEM768(sk), MLKEMCiphertext::MLKEM768(ct)) => {
147                let ss = mlkem768::decapsulate(ct.as_ref(), sk.as_ref());
148                SymmetricKey::from_data_ref(ss.as_bytes())
149            }
150            (MLKEMPrivateKey::MLKEM1024(sk), MLKEMCiphertext::MLKEM1024(ct)) => {
151                let ss = mlkem1024::decapsulate(ct.as_ref(), sk.as_ref());
152                SymmetricKey::from_data_ref(ss.as_bytes())
153            }
154            _ => panic!("MLKEM level mismatch"),
155        }
156    }
157}
158
159/// Implements the `Decrypter` trait for ML-KEM private keys.
160impl Decrypter for MLKEMPrivateKey {
161    /// Returns this key as an `EncapsulationPrivateKey`.
162    fn encapsulation_private_key(&self) -> EncapsulationPrivateKey {
163        EncapsulationPrivateKey::MLKEM(self.clone())
164    }
165}
166
167/// Provides debug formatting for ML-KEM private keys.
168impl std::fmt::Debug for MLKEMPrivateKey {
169    /// Formats the private key as a string for debugging purposes.
170    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
171        match self {
172            MLKEMPrivateKey::MLKEM512(_) => f.write_str("MLKEM512PrivateKey"),
173            MLKEMPrivateKey::MLKEM768(_) => f.write_str("MLKEM768PrivateKey"),
174            MLKEMPrivateKey::MLKEM1024(_) => f.write_str("MLKEM1024PrivateKey"),
175        }
176    }
177}
178
179/// Defines CBOR tags for ML-KEM private keys.
180impl CBORTagged for MLKEMPrivateKey {
181    /// Returns the CBOR tag for ML-KEM private keys.
182    fn cbor_tags() -> Vec<Tag> {
183        tags_for_values(&[tags::TAG_MLKEM_PRIVATE_KEY])
184    }
185}
186
187/// Converts an `MLKEMPrivateKey` to CBOR.
188impl From<MLKEMPrivateKey> for CBOR {
189    /// Converts to tagged CBOR.
190    fn from(value: MLKEMPrivateKey) -> Self {
191        value.tagged_cbor()
192    }
193}
194
195/// Implements CBOR encoding for ML-KEM private keys.
196impl CBORTaggedEncodable for MLKEMPrivateKey {
197    /// Creates the untagged CBOR representation as an array with level and key bytes.
198    fn untagged_cbor(&self) -> CBOR {
199        vec![self.level().into(), CBOR::to_byte_string(self.as_bytes())].into()
200    }
201}
202
203/// Attempts to convert CBOR to an `MLKEMPrivateKey`.
204impl TryFrom<CBOR> for MLKEMPrivateKey {
205    type Error = Error;
206
207    /// Converts from tagged CBOR.
208    fn try_from(cbor: CBOR) -> Result<Self, Self::Error> {
209        Self::from_tagged_cbor(cbor)
210    }
211}
212
213/// Implements CBOR decoding for ML-KEM private keys.
214impl CBORTaggedDecodable for MLKEMPrivateKey {
215    /// Creates an `MLKEMPrivateKey` from untagged CBOR.
216    ///
217    /// # Errors
218    /// Returns an error if the CBOR value doesn't represent a valid ML-KEM private key.
219    fn from_untagged_cbor(untagged_cbor: CBOR) -> Result<Self> {
220        match untagged_cbor.as_case() {
221            CBORCase::Array(elements) => {
222                if elements.len() != 2 {
223                    bail!("MLKEMPrivateKey must have two elements");
224                }
225
226                let level = MLKEM::try_from(elements[0].clone())?;
227                let data = CBOR::try_into_byte_string(elements[1].clone())?;
228                MLKEMPrivateKey::from_bytes(level, &data)
229            }
230            _ => bail!("MLKEMPrivateKey must be an array"),
231        }
232    }
233}