bc_components/mldsa/
mldsa_private_key.rs

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