ssh_key/public/
dsa.rs

1//! Digital Signature Algorithm (DSA) public keys.
2
3use crate::{
4    checked::CheckedSum, decode::Decode, encode::Encode, reader::Reader, writer::Writer, MPInt,
5    Result,
6};
7
8/// Digital Signature Algorithm (DSA) public key.
9///
10/// Described in [FIPS 186-4 § 4.1](https://csrc.nist.gov/publications/detail/fips/186/4/final).
11#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
12#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
13pub struct DsaPublicKey {
14    /// Prime modulus.
15    pub p: MPInt,
16
17    /// Prime divisor of `p - 1`.
18    pub q: MPInt,
19
20    /// Generator of a subgroup of order `q` in the multiplicative group
21    /// `GF(p)`, such that `1 < g < p`.
22    pub g: MPInt,
23
24    /// The public key, where `y = gˣ mod p`.
25    pub y: MPInt,
26}
27
28impl Decode for DsaPublicKey {
29    fn decode(reader: &mut impl Reader) -> Result<Self> {
30        let p = MPInt::decode(reader)?;
31        let q = MPInt::decode(reader)?;
32        let g = MPInt::decode(reader)?;
33        let y = MPInt::decode(reader)?;
34        Ok(Self { p, q, g, y })
35    }
36}
37
38impl Encode for DsaPublicKey {
39    fn encoded_len(&self) -> Result<usize> {
40        [
41            self.p.encoded_len()?,
42            self.q.encoded_len()?,
43            self.g.encoded_len()?,
44            self.y.encoded_len()?,
45        ]
46        .checked_sum()
47    }
48
49    fn encode(&self, writer: &mut impl Writer) -> Result<()> {
50        self.p.encode(writer)?;
51        self.q.encode(writer)?;
52        self.g.encode(writer)?;
53        self.y.encode(writer)
54    }
55}