ssh_key/public/
dsa.rs

1//! Digital Signature Algorithm (DSA) public keys.
2
3use crate::{Error, Mpint, Result};
4use core::hash::{Hash, Hasher};
5use encoding::{CheckedSum, Decode, Encode, Reader, Writer};
6
7/// Digital Signature Algorithm (DSA) public key.
8///
9/// Described in [FIPS 186-4 § 4.1](https://csrc.nist.gov/publications/detail/fips/186/4/final).
10#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
11pub struct DsaPublicKey {
12    /// Prime modulus.
13    pub p: Mpint,
14
15    /// Prime divisor of `p - 1`.
16    pub q: Mpint,
17
18    /// Generator of a subgroup of order `q` in the multiplicative group
19    /// `GF(p)`, such that `1 < g < p`.
20    pub g: Mpint,
21
22    /// The public key, where `y = gˣ mod p`.
23    pub y: Mpint,
24}
25
26impl Decode for DsaPublicKey {
27    type Error = Error;
28
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) -> encoding::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) -> encoding::Result<()> {
50        self.p.encode(writer)?;
51        self.q.encode(writer)?;
52        self.g.encode(writer)?;
53        self.y.encode(writer)
54    }
55}
56
57impl Hash for DsaPublicKey {
58    #[inline]
59    fn hash<H: Hasher>(&self, state: &mut H) {
60        self.p.as_bytes().hash(state);
61        self.q.as_bytes().hash(state);
62        self.g.as_bytes().hash(state);
63        self.y.as_bytes().hash(state);
64    }
65}
66
67#[cfg(feature = "dsa")]
68impl TryFrom<DsaPublicKey> for dsa::VerifyingKey {
69    type Error = Error;
70
71    fn try_from(key: DsaPublicKey) -> Result<dsa::VerifyingKey> {
72        dsa::VerifyingKey::try_from(&key)
73    }
74}
75
76#[cfg(feature = "dsa")]
77impl TryFrom<&DsaPublicKey> for dsa::VerifyingKey {
78    type Error = Error;
79
80    fn try_from(key: &DsaPublicKey) -> Result<dsa::VerifyingKey> {
81        let components = dsa::Components::from_components(
82            dsa::BigUint::try_from(&key.p)?,
83            dsa::BigUint::try_from(&key.q)?,
84            dsa::BigUint::try_from(&key.g)?,
85        )?;
86
87        dsa::VerifyingKey::from_components(components, dsa::BigUint::try_from(&key.y)?)
88            .map_err(|_| Error::Crypto)
89    }
90}
91
92#[cfg(feature = "dsa")]
93impl TryFrom<dsa::VerifyingKey> for DsaPublicKey {
94    type Error = Error;
95
96    fn try_from(key: dsa::VerifyingKey) -> Result<DsaPublicKey> {
97        DsaPublicKey::try_from(&key)
98    }
99}
100
101#[cfg(feature = "dsa")]
102impl TryFrom<&dsa::VerifyingKey> for DsaPublicKey {
103    type Error = Error;
104
105    fn try_from(key: &dsa::VerifyingKey) -> Result<DsaPublicKey> {
106        Ok(DsaPublicKey {
107            p: key.components().p().try_into()?,
108            q: key.components().q().try_into()?,
109            g: key.components().g().try_into()?,
110            y: key.y().try_into()?,
111        })
112    }
113}