1use crate::{Error, Mpint, Result, public::DsaPublicKey};
4use core::fmt;
5use ctutils::{Choice, CtEq};
6use encoding::{CheckedSum, Decode, Decoder, Encode, Reader, Writer};
7use zeroize::Zeroize;
8
9#[cfg(feature = "dsa")]
10use encoding::Uint;
11#[cfg(all(feature = "dsa", feature = "rand_core"))]
12use rand_core::CryptoRng;
13
14#[derive(Clone)]
21pub struct DsaPrivateKey {
22 inner: Mpint,
24}
25
26impl DsaPrivateKey {
27 pub fn new(x: Mpint) -> Result<Self> {
32 if x.is_zero() {
33 return Err(Error::FormatEncoding);
34 }
35 Ok(Self { inner: x })
36 }
37
38 #[must_use]
40 pub fn as_bytes(&self) -> &[u8] {
41 self.inner.as_bytes()
42 }
43
44 #[must_use]
46 pub fn as_mpint(&self) -> &Mpint {
47 &self.inner
48 }
49}
50
51impl AsRef<[u8]> for DsaPrivateKey {
52 fn as_ref(&self) -> &[u8] {
53 self.as_bytes()
54 }
55}
56
57impl CtEq for DsaPrivateKey {
58 fn ct_eq(&self, other: &Self) -> Choice {
59 self.inner.ct_eq(&other.inner)
60 }
61}
62
63impl Eq for DsaPrivateKey {}
64
65impl PartialEq for DsaPrivateKey {
66 fn eq(&self, other: &Self) -> bool {
67 self.ct_eq(other).into()
68 }
69}
70
71impl TryFrom<Mpint> for DsaPrivateKey {
72 type Error = Error;
73
74 fn try_from(x: Mpint) -> Result<Self> {
75 Self::new(x)
76 }
77}
78
79impl Decode for DsaPrivateKey {
80 type Error = Error;
81
82 fn decode(reader: &mut Decoder<impl Reader>) -> Result<Self> {
83 Self::new(Mpint::decode(reader)?)
84 }
85}
86
87impl Encode for DsaPrivateKey {
88 fn encoded_len(&self) -> encoding::Result<usize> {
89 self.inner.encoded_len()
90 }
91
92 fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
93 self.inner.encode(writer)
94 }
95}
96
97impl fmt::Debug for DsaPrivateKey {
98 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99 f.debug_struct("DsaPrivateKey").finish_non_exhaustive()
100 }
101}
102
103impl Drop for DsaPrivateKey {
104 fn drop(&mut self) {
105 self.inner.zeroize();
106 }
107}
108
109#[cfg(feature = "dsa")]
110impl TryFrom<&dsa::SigningKey> for DsaPrivateKey {
111 type Error = Error;
112
113 fn try_from(key: &dsa::SigningKey) -> Result<DsaPrivateKey> {
114 Self::new(key.x().as_ref().try_into()?)
115 }
116}
117
118#[cfg(feature = "dsa")]
119impl TryFrom<dsa::SigningKey> for DsaPrivateKey {
120 type Error = Error;
121
122 fn try_from(key: dsa::SigningKey) -> Result<DsaPrivateKey> {
123 DsaPrivateKey::try_from(&key)
124 }
125}
126
127#[cfg(feature = "dsa")]
128impl From<DsaPrivateKey> for Uint {
129 fn from(key: DsaPrivateKey) -> Uint {
130 Uint::from(&key.inner)
131 }
132}
133
134#[cfg(feature = "dsa")]
135impl From<&DsaPrivateKey> for Uint {
136 fn from(key: &DsaPrivateKey) -> Uint {
137 Uint::from(&key.inner)
138 }
139}
140
141#[derive(Clone)]
143pub struct DsaKeypair {
144 public: DsaPublicKey,
146
147 private: DsaPrivateKey,
149}
150
151impl DsaKeypair {
152 #[cfg(all(feature = "dsa", feature = "rand_core"))]
154 #[allow(deprecated)]
155 pub(crate) const KEY_SIZE: dsa::KeySize = dsa::KeySize::DSA_1024_160;
156
157 #[cfg(all(feature = "dsa", feature = "rand_core"))]
159 #[expect(clippy::missing_errors_doc, reason = "TODO")]
160 pub fn random<R: CryptoRng + ?Sized>(rng: &mut R) -> Result<Self> {
161 let Ok(components) =
162 dsa::Components::try_generate_from_rng_with_key_size(rng, Self::KEY_SIZE);
163 let Ok(sk) = dsa::SigningKey::try_generate_from_rng_with_components(rng, components);
164 sk.try_into()
165 }
166
167 pub fn new(public: DsaPublicKey, private: DsaPrivateKey) -> Result<Self> {
172 Ok(Self { public, private })
174 }
175
176 #[must_use]
178 pub fn public(&self) -> &DsaPublicKey {
179 &self.public
180 }
181
182 #[must_use]
184 pub fn private(&self) -> &DsaPrivateKey {
185 &self.private
186 }
187}
188
189impl CtEq for DsaKeypair {
190 fn ct_eq(&self, other: &Self) -> Choice {
191 Choice::from(u8::from(self.public == other.public)) & self.private.ct_eq(&other.private)
192 }
193}
194
195impl PartialEq for DsaKeypair {
196 fn eq(&self, other: &Self) -> bool {
197 self.ct_eq(other).into()
198 }
199}
200
201impl Eq for DsaKeypair {}
202
203impl Decode for DsaKeypair {
204 type Error = Error;
205
206 fn decode(reader: &mut Decoder<impl Reader>) -> Result<Self> {
207 let public = DsaPublicKey::decode(reader)?;
208 let private = DsaPrivateKey::decode(reader)?;
209 DsaKeypair::new(public, private)
210 }
211}
212
213impl Encode for DsaKeypair {
214 fn encoded_len(&self) -> encoding::Result<usize> {
215 [self.public.encoded_len()?, self.private.encoded_len()?].checked_sum()
216 }
217
218 fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
219 self.public.encode(writer)?;
220 self.private.encode(writer)
221 }
222}
223
224impl From<DsaKeypair> for DsaPublicKey {
225 fn from(keypair: DsaKeypair) -> DsaPublicKey {
226 keypair.public
227 }
228}
229
230impl From<&DsaKeypair> for DsaPublicKey {
231 fn from(keypair: &DsaKeypair) -> DsaPublicKey {
232 keypair.public.clone()
233 }
234}
235
236impl fmt::Debug for DsaKeypair {
237 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
238 f.debug_struct("DsaKeypair")
239 .field("public", &self.public)
240 .finish_non_exhaustive()
241 }
242}
243
244#[cfg(feature = "dsa")]
245impl TryFrom<DsaKeypair> for dsa::SigningKey {
246 type Error = Error;
247
248 fn try_from(key: DsaKeypair) -> Result<dsa::SigningKey> {
249 dsa::SigningKey::try_from(&key)
250 }
251}
252
253#[cfg(feature = "dsa")]
254impl TryFrom<&DsaKeypair> for dsa::SigningKey {
255 type Error = Error;
256
257 fn try_from(key: &DsaKeypair) -> Result<dsa::SigningKey> {
258 Ok(dsa::SigningKey::from_components(
259 dsa::VerifyingKey::try_from(&key.public)?,
260 key.private.as_mpint().into(),
261 )?)
262 }
263}
264
265#[cfg(feature = "dsa")]
266impl TryFrom<dsa::SigningKey> for DsaKeypair {
267 type Error = Error;
268
269 fn try_from(key: dsa::SigningKey) -> Result<DsaKeypair> {
270 DsaKeypair::try_from(&key)
271 }
272}
273
274#[cfg(feature = "dsa")]
275impl TryFrom<&dsa::SigningKey> for DsaKeypair {
276 type Error = Error;
277
278 fn try_from(key: &dsa::SigningKey) -> Result<DsaKeypair> {
279 Ok(DsaKeypair {
280 private: key.try_into()?,
281 public: key.verifying_key().try_into()?,
282 })
283 }
284}