bc_components/mldsa/
mldsa_private_key.rs

1use anyhow::{anyhow, bail, Error, 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 => Ok(MLDSAPrivateKey::MLDSA44(Box::new(
123                mldsa44::SecretKey::from_bytes(bytes).map_err(|e| anyhow!(e))?,
124            ))),
125            MLDSA::MLDSA65 => Ok(MLDSAPrivateKey::MLDSA65(Box::new(
126                mldsa65::SecretKey::from_bytes(bytes).map_err(|e| anyhow!(e))?,
127            ))),
128            MLDSA::MLDSA87 => Ok(MLDSAPrivateKey::MLDSA87(Box::new(
129                mldsa87::SecretKey::from_bytes(bytes).map_err(|e| anyhow!(e))?,
130            ))),
131        }
132    }
133}
134
135/// Provides debug formatting for ML-DSA private keys.
136impl std::fmt::Debug for MLDSAPrivateKey {
137    /// Formats the private key as a string for debugging purposes.
138    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
139        match self {
140            MLDSAPrivateKey::MLDSA44(_) => f.write_str("MLDSA44PrivateKey"),
141            MLDSAPrivateKey::MLDSA65(_) => f.write_str("MLDSA65PrivateKey"),
142            MLDSAPrivateKey::MLDSA87(_) => f.write_str("MLDSA87PrivateKey"),
143        }
144    }
145}
146
147/// Defines CBOR tags for ML-DSA private keys.
148impl CBORTagged for MLDSAPrivateKey {
149    /// Returns the CBOR tag for ML-DSA private keys.
150    fn cbor_tags() -> Vec<Tag> {
151        tags_for_values(&[tags::TAG_MLDSA_PRIVATE_KEY])
152    }
153}
154
155/// Converts an `MLDSAPrivateKey` to CBOR.
156impl From<MLDSAPrivateKey> for CBOR {
157    /// Converts to tagged CBOR.
158    fn from(value: MLDSAPrivateKey) -> Self {
159        value.tagged_cbor()
160    }
161}
162
163/// Implements CBOR encoding for ML-DSA private keys.
164impl CBORTaggedEncodable for MLDSAPrivateKey {
165    /// Creates the untagged CBOR representation as an array with level and key bytes.
166    fn untagged_cbor(&self) -> CBOR {
167        vec![self.level().into(), CBOR::to_byte_string(self.as_bytes())].into()
168    }
169}
170
171/// Attempts to convert CBOR to an `MLDSAPrivateKey`.
172impl TryFrom<CBOR> for MLDSAPrivateKey {
173    type Error = Error;
174
175    /// Converts from tagged CBOR.
176    fn try_from(cbor: CBOR) -> Result<Self, Self::Error> {
177        Self::from_tagged_cbor(cbor)
178    }
179}
180
181/// Implements CBOR decoding for ML-DSA private keys.
182impl CBORTaggedDecodable for MLDSAPrivateKey {
183    /// Creates an `MLDSAPrivateKey` from untagged CBOR.
184    ///
185    /// # Errors
186    /// Returns an error if the CBOR value doesn't represent a valid ML-DSA private key.
187    fn from_untagged_cbor(untagged_cbor: CBOR) -> Result<Self> {
188        match untagged_cbor.as_case() {
189            CBORCase::Array(elements) => {
190                if elements.len() != 2 {
191                    bail!("MLDSAPrivateKey must have two elements");
192                }
193
194                let level = MLDSA::try_from(elements[0].clone())?;
195                let data = CBOR::try_into_byte_string(elements[1].clone())?;
196                MLDSAPrivateKey::from_bytes(level, &data)
197            }
198            _ => bail!("MLDSAPrivateKey must be an array"),
199        }
200    }
201}