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
//! Inline signature storage sized for the largest enabled SSH composite.

use core::ops::Deref;
use encoding::{Decode, Decoder, Error, Reader, Result, Writer};

#[cfg(feature = "p384")]
const CAPACITY: usize = super::MlDsa87P384PublicKey::MAX_SIGNATURE_SIZE;
#[cfg(all(not(feature = "p384"), feature = "ed25519"))]
const CAPACITY: usize = super::MlDsa44Ed25519PublicKey::SIGNATURE_SIZE;
// The composite module emits a diagnostic when neither algorithm is enabled.
#[cfg(not(any(feature = "p384", feature = "ed25519")))]
const CAPACITY: usize = 0;

#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub(crate) struct SignatureData {
    bytes: [u8; CAPACITY],
    len: usize,
}

impl SignatureData {
    pub(crate) fn with_capacity(len: usize) -> Result<Self> {
        if len > CAPACITY {
            return Err(Error::Length);
        }
        Ok(Self {
            bytes: [0; CAPACITY],
            len: 0,
        })
    }
}

impl Deref for SignatureData {
    type Target = [u8];

    fn deref(&self) -> &[u8] {
        &self.bytes[..self.len]
    }
}

impl Decode for SignatureData {
    type Error = Error;

    fn decode(reader: &mut Decoder<impl Reader>) -> Result<Self> {
        reader.read_prefixed(|reader| {
            let len = reader.remaining_len();
            let mut data = Self::with_capacity(len)?;
            reader.read(&mut data.bytes[..len])?;
            data.len = len;
            Ok(data)
        })
    }
}

impl Writer for SignatureData {
    fn write(&mut self, bytes: &[u8]) -> Result<()> {
        let out = self.bytes[self.len..]
            .get_mut(..bytes.len())
            .ok_or(Error::Length)?;
        out.copy_from_slice(bytes);
        self.len += bytes.len();
        Ok(())
    }
}