bc_components/mldsa/
mldsa_private_key.rs1use dcbor::prelude::*;
2use pqcrypto_mldsa::*;
3use pqcrypto_traits::sign::*;
4
5use super::{MLDSA, MLDSASignature};
6use crate::{Digest, Error, Reference, ReferenceProvider, Result, tags};
7
8#[derive(Clone, PartialEq)]
37pub enum MLDSAPrivateKey {
38 MLDSA44(Box<mldsa44::SecretKey>),
40 MLDSA65(Box<mldsa65::SecretKey>),
42 MLDSA87(Box<mldsa87::SecretKey>),
44}
45
46impl MLDSAPrivateKey {
47 pub fn sign(&self, message: impl AsRef<[u8]>) -> MLDSASignature {
68 match self {
69 MLDSAPrivateKey::MLDSA44(sk) => MLDSASignature::MLDSA44(Box::new(
70 mldsa44::detached_sign(message.as_ref(), sk),
71 )),
72 MLDSAPrivateKey::MLDSA65(sk) => MLDSASignature::MLDSA65(Box::new(
73 mldsa65::detached_sign(message.as_ref(), sk),
74 )),
75 MLDSAPrivateKey::MLDSA87(sk) => MLDSASignature::MLDSA87(Box::new(
76 mldsa87::detached_sign(message.as_ref(), sk),
77 )),
78 }
79 }
80
81 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 pub fn size(&self) -> usize { self.level().private_key_size() }
92
93 pub fn as_bytes(&self) -> &[u8] { self.as_ref() }
95
96 pub fn from_bytes(level: MLDSA, bytes: &[u8]) -> Result<Self> {
113 match level {
114 MLDSA::MLDSA44 => Ok(MLDSAPrivateKey::MLDSA44(Box::new(
115 mldsa44::SecretKey::from_bytes(bytes)
116 .map_err(|e| Error::post_quantum(e.to_string()))?,
117 ))),
118 MLDSA::MLDSA65 => Ok(MLDSAPrivateKey::MLDSA65(Box::new(
119 mldsa65::SecretKey::from_bytes(bytes)
120 .map_err(|e| Error::post_quantum(e.to_string()))?,
121 ))),
122 MLDSA::MLDSA87 => Ok(MLDSAPrivateKey::MLDSA87(Box::new(
123 mldsa87::SecretKey::from_bytes(bytes)
124 .map_err(|e| Error::post_quantum(e.to_string()))?,
125 ))),
126 }
127 }
128}
129
130impl AsRef<[u8]> for MLDSAPrivateKey {
131 fn as_ref(&self) -> &[u8] {
133 match self {
134 MLDSAPrivateKey::MLDSA44(key) => key.as_bytes(),
135 MLDSAPrivateKey::MLDSA65(key) => key.as_bytes(),
136 MLDSAPrivateKey::MLDSA87(key) => key.as_bytes(),
137 }
138 }
139}
140
141impl std::fmt::Debug for MLDSAPrivateKey {
143 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
145 match self {
146 MLDSAPrivateKey::MLDSA44(_) => f.write_str("MLDSA44PrivateKey"),
147 MLDSAPrivateKey::MLDSA65(_) => f.write_str("MLDSA65PrivateKey"),
148 MLDSAPrivateKey::MLDSA87(_) => f.write_str("MLDSA87PrivateKey"),
149 }
150 }
151}
152
153impl CBORTagged for MLDSAPrivateKey {
155 fn cbor_tags() -> Vec<Tag> {
157 tags_for_values(&[tags::TAG_MLDSA_PRIVATE_KEY])
158 }
159}
160
161impl From<MLDSAPrivateKey> for CBOR {
163 fn from(value: MLDSAPrivateKey) -> Self { value.tagged_cbor() }
165}
166
167impl CBORTaggedEncodable for MLDSAPrivateKey {
169 fn untagged_cbor(&self) -> CBOR {
172 vec![self.level().into(), CBOR::to_byte_string(self.as_bytes())].into()
173 }
174}
175
176impl TryFrom<CBOR> for MLDSAPrivateKey {
178 type Error = dcbor::Error;
179
180 fn try_from(cbor: CBOR) -> dcbor::Result<Self> {
182 Self::from_tagged_cbor(cbor)
183 }
184}
185
186impl CBORTaggedDecodable for MLDSAPrivateKey {
188 fn from_untagged_cbor(untagged_cbor: CBOR) -> dcbor::Result<Self> {
194 match untagged_cbor.as_case() {
195 CBORCase::Array(elements) => {
196 if elements.len() != 2 {
197 return Err("MLDSAPrivateKey must have two elements".into());
198 }
199
200 let level = MLDSA::try_from(elements[0].clone())?;
201 let data = CBOR::try_into_byte_string(elements[1].clone())?;
202 Ok(MLDSAPrivateKey::from_bytes(level, &data)?)
203 }
204 _ => Err("MLDSAPrivateKey must be an array".into()),
205 }
206 }
207}
208
209impl ReferenceProvider for MLDSAPrivateKey {
210 fn reference(&self) -> Reference {
211 Reference::from_digest(Digest::from_image(
212 self.tagged_cbor().to_cbor_data(),
213 ))
214 }
215}
216
217impl std::fmt::Display for MLDSAPrivateKey {
218 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
219 match self {
220 MLDSAPrivateKey::MLDSA44(_) => {
221 write!(f, "MLDSA44PrivateKey({})", self.ref_hex_short())
222 }
223 MLDSAPrivateKey::MLDSA65(_) => {
224 write!(f, "MLDSA65PrivateKey({})", self.ref_hex_short())
225 }
226 MLDSAPrivateKey::MLDSA87(_) => {
227 write!(f, "MLDSA87PrivateKey({})", self.ref_hex_short())
228 }
229 }
230 }
231}