1use crate::{Error, Mpint, Result, public::DsaPublicKey};
4use core::fmt;
5use ctutils::{Choice, CtEq};
6use encoding::{Decode, Decoder, Encode, Reader};
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, Encode)]
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 fmt::Debug for DsaPrivateKey {
88 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89 f.debug_struct("DsaPrivateKey").finish_non_exhaustive()
90 }
91}
92
93impl Drop for DsaPrivateKey {
94 fn drop(&mut self) {
95 self.inner.zeroize();
96 }
97}
98
99#[cfg(feature = "dsa")]
100impl TryFrom<&dsa::SigningKey> for DsaPrivateKey {
101 type Error = Error;
102
103 fn try_from(key: &dsa::SigningKey) -> Result<DsaPrivateKey> {
104 Self::new(key.x().as_ref().try_into()?)
105 }
106}
107
108#[cfg(feature = "dsa")]
109impl TryFrom<dsa::SigningKey> for DsaPrivateKey {
110 type Error = Error;
111
112 fn try_from(key: dsa::SigningKey) -> Result<DsaPrivateKey> {
113 DsaPrivateKey::try_from(&key)
114 }
115}
116
117#[cfg(feature = "dsa")]
118impl From<DsaPrivateKey> for Uint {
119 fn from(key: DsaPrivateKey) -> Uint {
120 Uint::from(&key.inner)
121 }
122}
123
124#[cfg(feature = "dsa")]
125impl From<&DsaPrivateKey> for Uint {
126 fn from(key: &DsaPrivateKey) -> Uint {
127 Uint::from(&key.inner)
128 }
129}
130
131#[derive(Clone, Encode)]
133pub struct DsaKeypair {
134 public: DsaPublicKey,
136
137 private: DsaPrivateKey,
139}
140
141impl DsaKeypair {
142 #[cfg(all(feature = "dsa", feature = "rand_core"))]
144 #[allow(deprecated)]
145 pub(crate) const KEY_SIZE: dsa::KeySize = dsa::KeySize::DSA_1024_160;
146
147 #[cfg(all(feature = "dsa", feature = "rand_core"))]
149 #[expect(clippy::missing_errors_doc, reason = "TODO")]
150 pub fn random<R: CryptoRng + ?Sized>(rng: &mut R) -> Result<Self> {
151 let Ok(components) =
152 dsa::Components::try_generate_from_rng_with_key_size(rng, Self::KEY_SIZE);
153 let Ok(sk) = dsa::SigningKey::try_generate_from_rng_with_components(rng, components);
154 sk.try_into()
155 }
156
157 pub fn new(public: DsaPublicKey, private: DsaPrivateKey) -> Result<Self> {
162 Ok(Self { public, private })
164 }
165
166 #[must_use]
168 pub fn public(&self) -> &DsaPublicKey {
169 &self.public
170 }
171
172 #[must_use]
174 pub fn private(&self) -> &DsaPrivateKey {
175 &self.private
176 }
177}
178
179impl CtEq for DsaKeypair {
180 fn ct_eq(&self, other: &Self) -> Choice {
181 Choice::from(u8::from(self.public == other.public)) & self.private.ct_eq(&other.private)
182 }
183}
184
185impl PartialEq for DsaKeypair {
186 fn eq(&self, other: &Self) -> bool {
187 self.ct_eq(other).into()
188 }
189}
190
191impl Eq for DsaKeypair {}
192
193impl Decode for DsaKeypair {
194 type Error = Error;
195
196 fn decode(reader: &mut Decoder<impl Reader>) -> Result<Self> {
197 let public = DsaPublicKey::decode(reader)?;
198 let private = DsaPrivateKey::decode(reader)?;
199 DsaKeypair::new(public, private)
200 }
201}
202
203impl From<DsaKeypair> for DsaPublicKey {
204 fn from(keypair: DsaKeypair) -> DsaPublicKey {
205 keypair.public
206 }
207}
208
209impl From<&DsaKeypair> for DsaPublicKey {
210 fn from(keypair: &DsaKeypair) -> DsaPublicKey {
211 keypair.public.clone()
212 }
213}
214
215impl fmt::Debug for DsaKeypair {
216 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
217 f.debug_struct("DsaKeypair")
218 .field("public", &self.public)
219 .finish_non_exhaustive()
220 }
221}
222
223#[cfg(feature = "dsa")]
224impl TryFrom<DsaKeypair> for dsa::SigningKey {
225 type Error = Error;
226
227 fn try_from(key: DsaKeypair) -> Result<dsa::SigningKey> {
228 dsa::SigningKey::try_from(&key)
229 }
230}
231
232#[cfg(feature = "dsa")]
233impl TryFrom<&DsaKeypair> for dsa::SigningKey {
234 type Error = Error;
235
236 fn try_from(key: &DsaKeypair) -> Result<dsa::SigningKey> {
237 Ok(dsa::SigningKey::from_components(
238 dsa::VerifyingKey::try_from(&key.public)?,
239 key.private.as_mpint().into(),
240 )?)
241 }
242}
243
244#[cfg(feature = "dsa")]
245impl TryFrom<dsa::SigningKey> for DsaKeypair {
246 type Error = Error;
247
248 fn try_from(key: dsa::SigningKey) -> Result<DsaKeypair> {
249 DsaKeypair::try_from(&key)
250 }
251}
252
253#[cfg(feature = "dsa")]
254impl TryFrom<&dsa::SigningKey> for DsaKeypair {
255 type Error = Error;
256
257 fn try_from(key: &dsa::SigningKey) -> Result<DsaKeypair> {
258 Ok(DsaKeypair {
259 private: key.try_into()?,
260 public: key.verifying_key().try_into()?,
261 })
262 }
263}