bc_components/mldsa/
mldsa_private_key.rs

1use anyhow::{ anyhow, Result };
2use dcbor::prelude::*;
3use pqcrypto_mldsa::*;
4use pqcrypto_traits::sign::*;
5
6use crate::tags;
7
8use super::{ MLDSASignature, MLDSA };
9
10/// A private key for the ML-DSA post-quantum digital signature algorithm.
11///
12/// `MLDSAPrivateKey` represents a private key that can be used to create digital
13/// signatures using the ML-DSA (Module Lattice-based Digital Signature Algorithm)
14/// post-quantum algorithm. It supports multiple security levels through the variants:
15///
16/// - `MLDSA44`: NIST security level 2 (roughly equivalent to AES-128)
17/// - `MLDSA65`: NIST security level 3 (roughly equivalent to AES-192)
18/// - `MLDSA87`: NIST security level 5 (roughly equivalent to AES-256)
19///
20/// # Security
21///
22/// ML-DSA 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::MLDSA;
29///
30/// // Generate a keypair
31/// let (private_key, public_key) = MLDSA::MLDSA44.keypair();
32///
33/// // Sign a message
34/// let message = b"Hello, post-quantum world!";
35/// let signature = private_key.sign(message);
36/// ```
37#[derive(Clone, PartialEq)]
38pub enum MLDSAPrivateKey {
39    /// An ML-DSA44 private key (NIST security level 2)
40    MLDSA44(Box<mldsa44::SecretKey>),
41    /// An ML-DSA65 private key (NIST security level 3)
42    MLDSA65(Box<mldsa65::SecretKey>),
43    /// An ML-DSA87 private key (NIST security level 5)
44    MLDSA87(Box<mldsa87::SecretKey>),
45}
46
47impl MLDSAPrivateKey {
48    /// Signs a message using this ML-DSA private key.
49    ///
50    /// # Parameters
51    ///
52    /// * `message` - The message to sign.
53    ///
54    /// # Returns
55    ///
56    /// An `MLDSASignature` for the message, using the same security level as this private key.
57    ///
58    /// # Examples
59    ///
60    /// ```
61    /// use bc_components::MLDSA;
62    ///
63    /// let (private_key, _) = MLDSA::MLDSA44.keypair();
64    /// let message = b"Hello, world!";
65    /// let signature = private_key.sign(message);
66    /// ```
67    pub fn sign(&self, message: impl AsRef<[u8]>) -> MLDSASignature {
68        match self {
69            MLDSAPrivateKey::MLDSA44(sk) => {
70                MLDSASignature::MLDSA44(Box::new(mldsa44::detached_sign(message.as_ref(), sk)))
71            }
72            MLDSAPrivateKey::MLDSA65(sk) => {
73                MLDSASignature::MLDSA65(Box::new(mldsa65::detached_sign(message.as_ref(), sk)))
74            }
75            MLDSAPrivateKey::MLDSA87(sk) => {
76                MLDSASignature::MLDSA87(Box::new(mldsa87::detached_sign(message.as_ref(), sk)))
77            }
78        }
79    }
80
81    /// Returns the security level of this ML-DSA private key.
82    pub fn level(&self) -> MLDSA {
83        match self {
84            MLDSAPrivateKey::MLDSA44(_) => MLDSA::MLDSA44,
85            MLDSAPrivateKey::MLDSA65(_) => MLDSA::MLDSA65,
86            MLDSAPrivateKey::MLDSA87(_) => MLDSA::MLDSA87,
87        }
88    }
89
90    /// Returns the size of this ML-DSA private key in bytes.
91    pub fn size(&self) -> usize {
92        self.level().private_key_size()
93    }
94
95    /// Returns the raw bytes of this ML-DSA private key.
96    pub fn as_bytes(&self) -> &[u8] {
97        match self {
98            MLDSAPrivateKey::MLDSA44(key) => key.as_bytes(),
99            MLDSAPrivateKey::MLDSA65(key) => key.as_bytes(),
100            MLDSAPrivateKey::MLDSA87(key) => key.as_bytes(),
101        }
102    }
103
104    /// Creates an ML-DSA private key from raw bytes and a security level.
105    ///
106    /// # Parameters
107    ///
108    /// * `level` - The security level of the key.
109    /// * `bytes` - The raw bytes of the key.
110    ///
111    /// # Returns
112    ///
113    /// An `MLDSAPrivateKey` if the bytes represent a valid key for the given level,
114    /// or an error otherwise.
115    ///
116    /// # Errors
117    ///
118    /// Returns an error if the bytes do not represent a valid ML-DSA private key
119    /// for the specified security level.
120    pub fn from_bytes(level: MLDSA, bytes: &[u8]) -> Result<Self> {
121        match level {
122            MLDSA::MLDSA44 =>
123                Ok(
124                    MLDSAPrivateKey::MLDSA44(
125                        Box::new(mldsa44::SecretKey::from_bytes(bytes).map_err(|e| anyhow!(e))?)
126                    )
127                ),
128            MLDSA::MLDSA65 =>
129                Ok(
130                    MLDSAPrivateKey::MLDSA65(
131                        Box::new(mldsa65::SecretKey::from_bytes(bytes).map_err(|e| anyhow!(e))?)
132                    )
133                ),
134            MLDSA::MLDSA87 =>
135                Ok(
136                    MLDSAPrivateKey::MLDSA87(
137                        Box::new(mldsa87::SecretKey::from_bytes(bytes).map_err(|e| anyhow!(e))?)
138                    )
139                ),
140        }
141    }
142}
143
144/// Provides debug formatting for ML-DSA private keys.
145impl std::fmt::Debug for MLDSAPrivateKey {
146    /// Formats the private key as a string for debugging purposes.
147    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
148        match self {
149            MLDSAPrivateKey::MLDSA44(_) => f.write_str("MLDSA44PrivateKey"),
150            MLDSAPrivateKey::MLDSA65(_) => f.write_str("MLDSA65PrivateKey"),
151            MLDSAPrivateKey::MLDSA87(_) => f.write_str("MLDSA87PrivateKey"),
152        }
153    }
154}
155
156/// Defines CBOR tags for ML-DSA private keys.
157impl CBORTagged for MLDSAPrivateKey {
158    /// Returns the CBOR tag for ML-DSA private keys.
159    fn cbor_tags() -> Vec<Tag> {
160        tags_for_values(&[tags::TAG_MLDSA_PRIVATE_KEY])
161    }
162}
163
164/// Converts an `MLDSAPrivateKey` to CBOR.
165impl From<MLDSAPrivateKey> for CBOR {
166    /// Converts to tagged CBOR.
167    fn from(value: MLDSAPrivateKey) -> Self {
168        value.tagged_cbor()
169    }
170}
171
172/// Implements CBOR encoding for ML-DSA private keys.
173impl CBORTaggedEncodable for MLDSAPrivateKey {
174    /// Creates the untagged CBOR representation as an array with level and key bytes.
175    fn untagged_cbor(&self) -> CBOR {
176        vec![self.level().into(), CBOR::to_byte_string(self.as_bytes())].into()
177    }
178}
179
180/// Attempts to convert CBOR to an `MLDSAPrivateKey`.
181impl TryFrom<CBOR> for MLDSAPrivateKey {
182    type Error = dcbor::Error;
183
184    /// Converts from tagged CBOR.
185    fn try_from(cbor: CBOR) -> dcbor::Result<Self> {
186        Self::from_tagged_cbor(cbor)
187    }
188}
189
190/// Implements CBOR decoding for ML-DSA private keys.
191impl CBORTaggedDecodable for MLDSAPrivateKey {
192    /// Creates an `MLDSAPrivateKey` from untagged CBOR.
193    ///
194    /// # Errors
195    /// Returns an error if the CBOR value doesn't represent a valid ML-DSA private key.
196    fn from_untagged_cbor(untagged_cbor: CBOR) -> dcbor::Result<Self> {
197        match untagged_cbor.as_case() {
198            CBORCase::Array(elements) => {
199                if elements.len() != 2 {
200                    return Err("MLDSAPrivateKey must have two elements".into());
201                }
202
203                let level = MLDSA::try_from(elements[0].clone())?;
204                let data = CBOR::try_into_byte_string(elements[1].clone())?;
205                Ok(MLDSAPrivateKey::from_bytes(level, &data)?)
206            }
207            _ => {
208                return Err("MLDSAPrivateKey must be an array".into());
209            }
210        }
211    }
212}