Skip to main content

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, Decoder, Encode, Reader, Writer};
6
7#[cfg(feature = "dsa")]
8use encoding::Uint;
9
10/// Digital Signature Algorithm (DSA) public key.
11///
12/// Described in [FIPS 186-4 § 4.1](https://csrc.nist.gov/publications/detail/fips/186/4/final).
13#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
14pub struct DsaPublicKey {
15    /// Prime modulus.
16    p: Mpint,
17
18    /// Prime divisor of `p - 1`.
19    q: Mpint,
20
21    /// Generator of a subgroup of order `q` in the multiplicative group `GF(p)`, such that
22    /// `1 < g < p`.
23    g: Mpint,
24
25    /// The public key, where `y = gˣ mod p`.
26    y: Mpint,
27}
28
29impl DsaPublicKey {
30    /// Create a new [`DsaPublicKey`] with the following components:
31    ///
32    /// - `p`: prime modulus.
33    /// - `q`: prime divisor of `p - 1`.
34    /// - `g`: generator of a subgroup of order `q` in the multiplicative group `GF(p)`, such
35    ///   that `1 < g < p`.
36    /// - `y`: the public key, where `y = gˣ mod p`.
37    ///
38    /// # Errors
39    /// Returns [`Error::FormatEncoding`] if any component is zero.
40    pub fn new(p: Mpint, q: Mpint, g: Mpint, y: Mpint) -> Result<Self> {
41        if p.is_zero() || q.is_zero() || g.is_zero() || y.is_zero() {
42            return Err(Error::FormatEncoding);
43        }
44        Ok(Self { p, q, g, y })
45    }
46
47    /// Prime modulus.
48    #[must_use]
49    pub fn p(&self) -> &Mpint {
50        &self.p
51    }
52
53    /// Prime divisor of `p - 1`.
54    #[must_use]
55    pub fn q(&self) -> &Mpint {
56        &self.q
57    }
58
59    /// Generator of a subgroup of order `q` in the multiplicative group `GF(p)`, such that
60    /// `1 < g < p`.
61    #[must_use]
62    pub fn g(&self) -> &Mpint {
63        &self.g
64    }
65
66    /// The public key, where `y = gˣ mod p`.
67    #[must_use]
68    pub fn y(&self) -> &Mpint {
69        &self.y
70    }
71}
72
73impl Decode for DsaPublicKey {
74    type Error = Error;
75
76    fn decode(reader: &mut Decoder<impl Reader>) -> Result<Self> {
77        let p = Mpint::decode(reader)?;
78        let q = Mpint::decode(reader)?;
79        let g = Mpint::decode(reader)?;
80        let y = Mpint::decode(reader)?;
81        Self::new(p, q, g, y)
82    }
83}
84
85impl Encode for DsaPublicKey {
86    fn encoded_len(&self) -> encoding::Result<usize> {
87        [
88            self.p.encoded_len()?,
89            self.q.encoded_len()?,
90            self.g.encoded_len()?,
91            self.y.encoded_len()?,
92        ]
93        .checked_sum()
94    }
95
96    fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
97        self.p.encode(writer)?;
98        self.q.encode(writer)?;
99        self.g.encode(writer)?;
100        self.y.encode(writer)
101    }
102}
103
104impl Hash for DsaPublicKey {
105    #[inline]
106    fn hash<H: Hasher>(&self, state: &mut H) {
107        self.p.as_bytes().hash(state);
108        self.q.as_bytes().hash(state);
109        self.g.as_bytes().hash(state);
110        self.y.as_bytes().hash(state);
111    }
112}
113
114#[cfg(feature = "dsa")]
115impl TryFrom<DsaPublicKey> for dsa::VerifyingKey {
116    type Error = Error;
117
118    fn try_from(key: DsaPublicKey) -> Result<dsa::VerifyingKey> {
119        dsa::VerifyingKey::try_from(&key)
120    }
121}
122
123#[cfg(feature = "dsa")]
124impl TryFrom<&DsaPublicKey> for dsa::VerifyingKey {
125    type Error = Error;
126
127    fn try_from(key: &DsaPublicKey) -> Result<dsa::VerifyingKey> {
128        let p = Uint::from(&key.p);
129        let q = Uint::from(&key.q);
130        let g = Uint::from(&key.g);
131        let y = Uint::from(&key.y);
132
133        let components = dsa::Components::from_components(p, q, g)?;
134        dsa::VerifyingKey::from_components(components, y).map_err(|_| Error::Crypto)
135    }
136}
137
138#[cfg(feature = "dsa")]
139impl TryFrom<dsa::VerifyingKey> for DsaPublicKey {
140    type Error = Error;
141
142    fn try_from(key: dsa::VerifyingKey) -> Result<DsaPublicKey> {
143        DsaPublicKey::try_from(&key)
144    }
145}
146
147#[cfg(feature = "dsa")]
148impl TryFrom<&dsa::VerifyingKey> for DsaPublicKey {
149    type Error = Error;
150
151    fn try_from(key: &dsa::VerifyingKey) -> Result<DsaPublicKey> {
152        Self::new(
153            key.components().p().as_ref().try_into()?,
154            key.components().q().as_ref().try_into()?,
155            key.components().g().as_ref().try_into()?,
156            key.y().as_ref().try_into()?,
157        )
158    }
159}