Skip to main content

ssh_key/composite/
mldsa87_p384.rs

1//! ML-DSA-87/ECDSA P-384 composite keys and signatures.
2
3mod keys;
4
5pub use keys::{MlDsa87P384Keypair, MlDsa87P384PrivateKey, MlDsa87P384PublicKey};
6
7use super::{CompositeMessage, MLDSA87_P384_LABEL as LABEL};
8use crate::{
9    Algorithm, Error, Result, Signature,
10    signature::{encode_ecdsa_components, p384_signature_from_openssh_bytes},
11};
12use encoding::Writer;
13use ml_dsa::{EncodedSignature, MlDsa87};
14use p384::ecdsa;
15use rand_core::TryCryptoRng;
16use signature::{RandomizedSigner, Signer, Verifier};
17
18impl MlDsa87P384PublicKey {
19    // Each signature mpint has a four-byte length and at most one sign byte.
20    pub(crate) const MAX_SIGNATURE_SIZE: usize =
21        size_of::<EncodedSignature<MlDsa87>>() + 2 * (4 + 1 + size_of::<p384::FieldBytes>());
22}
23
24fn encode_signature(
25    (pq, classical): (ml_dsa::Signature<MlDsa87>, ecdsa::Signature),
26) -> Result<Signature> {
27    let (r, s) = classical.split_bytes();
28    Signature::new_with(
29        Algorithm::MlDsa87P384,
30        MlDsa87P384PublicKey::MAX_SIGNATURE_SIZE,
31        |out| {
32            out.write(&pq.encode())?;
33            encode_ecdsa_components(out, &r, &s)
34        },
35    )
36}
37
38impl Signer<Signature> for MlDsa87P384Keypair {
39    fn try_sign(&self, msg: &[u8]) -> signature::Result<Signature> {
40        let message = CompositeMessage::new(LABEL, msg, &[])?;
41        let pq = ml_dsa::SigningKey::<MlDsa87>::from_seed(&self.private().mldsa);
42        let classical = ecdsa::SigningKey::from(&self.private().p384);
43        let signatures = message.sign(pq.expanded_key(), &classical)?;
44        Ok(encode_signature(signatures)?)
45    }
46}
47
48impl RandomizedSigner<Signature> for MlDsa87P384Keypair {
49    fn try_sign_with_rng<R: TryCryptoRng + ?Sized>(
50        &self,
51        rng: &mut R,
52        msg: &[u8],
53    ) -> signature::Result<Signature> {
54        let message = CompositeMessage::new(LABEL, msg, &[])?;
55        let pq = ml_dsa::SigningKey::<MlDsa87>::from_seed(&self.private().mldsa);
56        let classical = ecdsa::SigningKey::from(&self.private().p384);
57        let signatures = message.sign_with_rng(pq.expanded_key(), &classical, rng)?;
58        Ok(encode_signature(signatures)?)
59    }
60}
61
62impl Verifier<Signature> for MlDsa87P384PublicKey {
63    fn verify(&self, msg: &[u8], signature: &Signature) -> signature::Result<()> {
64        if signature.algorithm() != self.algorithm() {
65            return Err(Error::Signature.into());
66        }
67        let (pq, classical) = signature
68            .as_bytes()
69            .split_at_checked(size_of::<EncodedSignature<MlDsa87>>())
70            .ok_or(Error::Signature)?;
71        let pq = ml_dsa::Signature::<MlDsa87>::try_from(pq).map_err(|_| Error::Signature)?;
72        let classical = p384_signature_from_openssh_bytes(classical)?;
73        let message = CompositeMessage::new(LABEL, msg, &[])?;
74        Ok(message.verify(
75            &ml_dsa::VerifyingKey::<MlDsa87>::decode(&self.mldsa),
76            &pq,
77            || Ok(ecdsa::VerifyingKey::from_sec1_point(&self.p384)?),
78            &classical,
79        )?)
80    }
81}