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
//! Digital Signature Algorithm (DSA) public keys.

use crate::{Error, Mpint, Result};
use core::hash::{Hash, Hasher};
use encoding::{Decode, Decoder, Encode, Reader};

#[cfg(feature = "dsa")]
use encoding::Uint;

/// Digital Signature Algorithm (DSA) public key.
///
/// Described in [FIPS 186-4 § 4.1](https://csrc.nist.gov/publications/detail/fips/186/4/final).
#[derive(Clone, Debug, Encode, Eq, Ord, PartialEq, PartialOrd)]
pub struct DsaPublicKey {
    /// Prime modulus.
    p: Mpint,

    /// Prime divisor of `p - 1`.
    q: Mpint,

    /// Generator of a subgroup of order `q` in the multiplicative group `GF(p)`, such that
    /// `1 < g < p`.
    g: Mpint,

    /// The public key, where `y = gˣ mod p`.
    y: Mpint,
}

impl DsaPublicKey {
    /// Create a new [`DsaPublicKey`] with the following components:
    ///
    /// - `p`: prime modulus.
    /// - `q`: prime divisor of `p - 1`.
    /// - `g`: generator of a subgroup of order `q` in the multiplicative group `GF(p)`, such
    ///   that `1 < g < p`.
    /// - `y`: the public key, where `y = gˣ mod p`.
    ///
    /// # Errors
    /// Returns [`Error::FormatEncoding`] if any component is zero.
    pub fn new(p: Mpint, q: Mpint, g: Mpint, y: Mpint) -> Result<Self> {
        if p.is_zero() || q.is_zero() || g.is_zero() || y.is_zero() {
            return Err(Error::FormatEncoding);
        }
        Ok(Self { p, q, g, y })
    }

    /// Prime modulus.
    #[must_use]
    pub fn p(&self) -> &Mpint {
        &self.p
    }

    /// Prime divisor of `p - 1`.
    #[must_use]
    pub fn q(&self) -> &Mpint {
        &self.q
    }

    /// Generator of a subgroup of order `q` in the multiplicative group `GF(p)`, such that
    /// `1 < g < p`.
    #[must_use]
    pub fn g(&self) -> &Mpint {
        &self.g
    }

    /// The public key, where `y = gˣ mod p`.
    #[must_use]
    pub fn y(&self) -> &Mpint {
        &self.y
    }
}

impl Decode for DsaPublicKey {
    type Error = Error;

    fn decode(reader: &mut Decoder<impl Reader>) -> Result<Self> {
        let p = Mpint::decode(reader)?;
        let q = Mpint::decode(reader)?;
        let g = Mpint::decode(reader)?;
        let y = Mpint::decode(reader)?;
        Self::new(p, q, g, y)
    }
}

impl Hash for DsaPublicKey {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.p.as_bytes().hash(state);
        self.q.as_bytes().hash(state);
        self.g.as_bytes().hash(state);
        self.y.as_bytes().hash(state);
    }
}

#[cfg(feature = "dsa")]
impl TryFrom<DsaPublicKey> for dsa::VerifyingKey {
    type Error = Error;

    fn try_from(key: DsaPublicKey) -> Result<dsa::VerifyingKey> {
        dsa::VerifyingKey::try_from(&key)
    }
}

#[cfg(feature = "dsa")]
impl TryFrom<&DsaPublicKey> for dsa::VerifyingKey {
    type Error = Error;

    fn try_from(key: &DsaPublicKey) -> Result<dsa::VerifyingKey> {
        let p = Uint::from(&key.p);
        let q = Uint::from(&key.q);
        let g = Uint::from(&key.g);
        let y = Uint::from(&key.y);

        let components = dsa::Components::from_components(p, q, g)?;
        dsa::VerifyingKey::from_components(components, y).map_err(|_| Error::Crypto)
    }
}

#[cfg(feature = "dsa")]
impl TryFrom<dsa::VerifyingKey> for DsaPublicKey {
    type Error = Error;

    fn try_from(key: dsa::VerifyingKey) -> Result<DsaPublicKey> {
        DsaPublicKey::try_from(&key)
    }
}

#[cfg(feature = "dsa")]
impl TryFrom<&dsa::VerifyingKey> for DsaPublicKey {
    type Error = Error;

    fn try_from(key: &dsa::VerifyingKey) -> Result<DsaPublicKey> {
        Self::new(
            key.components().p().as_ref().try_into()?,
            key.components().q().as_ref().try_into()?,
            key.components().g().as_ref().try_into()?,
            key.y().as_ref().try_into()?,
        )
    }
}