bc_components/mlkem/
mlkem_private_key.rs

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