ssh-key 0.7.0

Pure Rust implementation of SSH key file format decoders/encoders as described in RFC4251/RFC4253 and OpenSSH key formats, as well as "sshsig" signatures and certificates (including certificate validation and certificate authority support), with further support for the `authorized_keys` and `known_hosts` file formats.
Documentation
//! ML-DSA-87/ECDSA P-384 composite keys and signatures.

mod keys;

pub use keys::{MlDsa87P384Keypair, MlDsa87P384PrivateKey, MlDsa87P384PublicKey};

use super::{CompositeMessage, MLDSA87_P384_LABEL as LABEL};
use crate::{
    Algorithm, Error, Result, Signature,
    signature::{encode_ecdsa_components, p384_signature_from_openssh_bytes},
};
use encoding::Writer;
use ml_dsa::{EncodedSignature, MlDsa87};
use p384::ecdsa;
use rand_core::TryCryptoRng;
use signature::{RandomizedSigner, Signer, Verifier};

impl MlDsa87P384PublicKey {
    // Each signature mpint has a four-byte length and at most one sign byte.
    pub(crate) const MAX_SIGNATURE_SIZE: usize =
        size_of::<EncodedSignature<MlDsa87>>() + 2 * (4 + 1 + size_of::<p384::FieldBytes>());
}

fn encode_signature(
    (pq, classical): (ml_dsa::Signature<MlDsa87>, ecdsa::Signature),
) -> Result<Signature> {
    let (r, s) = classical.split_bytes();
    Signature::new_with(
        Algorithm::MlDsa87P384,
        MlDsa87P384PublicKey::MAX_SIGNATURE_SIZE,
        |out| {
            out.write(&pq.encode())?;
            encode_ecdsa_components(out, &r, &s)
        },
    )
}

impl Signer<Signature> for MlDsa87P384Keypair {
    fn try_sign(&self, msg: &[u8]) -> signature::Result<Signature> {
        let message = CompositeMessage::new(LABEL, msg, &[])?;
        let pq = ml_dsa::SigningKey::<MlDsa87>::from_seed(&self.private().mldsa);
        let classical = ecdsa::SigningKey::from(&self.private().p384);
        let signatures = message.sign(pq.expanded_key(), &classical)?;
        Ok(encode_signature(signatures)?)
    }
}

impl RandomizedSigner<Signature> for MlDsa87P384Keypair {
    fn try_sign_with_rng<R: TryCryptoRng + ?Sized>(
        &self,
        rng: &mut R,
        msg: &[u8],
    ) -> signature::Result<Signature> {
        let message = CompositeMessage::new(LABEL, msg, &[])?;
        let pq = ml_dsa::SigningKey::<MlDsa87>::from_seed(&self.private().mldsa);
        let classical = ecdsa::SigningKey::from(&self.private().p384);
        let signatures = message.sign_with_rng(pq.expanded_key(), &classical, rng)?;
        Ok(encode_signature(signatures)?)
    }
}

impl Verifier<Signature> for MlDsa87P384PublicKey {
    fn verify(&self, msg: &[u8], signature: &Signature) -> signature::Result<()> {
        if signature.algorithm() != self.algorithm() {
            return Err(Error::Signature.into());
        }
        let (pq, classical) = signature
            .as_bytes()
            .split_at_checked(size_of::<EncodedSignature<MlDsa87>>())
            .ok_or(Error::Signature)?;
        let pq = ml_dsa::Signature::<MlDsa87>::try_from(pq).map_err(|_| Error::Signature)?;
        let classical = p384_signature_from_openssh_bytes(classical)?;
        let message = CompositeMessage::new(LABEL, msg, &[])?;
        Ok(message.verify(
            &ml_dsa::VerifyingKey::<MlDsa87>::decode(&self.mldsa),
            &pq,
            || Ok(ecdsa::VerifyingKey::from_sec1_point(&self.p384)?),
            &classical,
        )?)
    }
}