ssh_key/private/
opaque.rs1use crate::{
12 Algorithm, Error, Result,
13 public::{OpaquePublicKey, OpaquePublicKeyBytes},
14};
15use alloc::vec::Vec;
16use core::fmt::{self, Debug};
17use ctutils::{Choice, CtEq};
18use encoding::{CheckedSum, Decode, Decoder, Encode, Reader, Writer};
19
20#[derive(Clone)]
25pub struct OpaqueKeypair {
26 pub private: OpaquePrivateKeyBytes,
28 pub public: OpaquePublicKey,
30}
31
32impl OpaqueKeypair {
33 #[must_use]
35 pub fn new(private_key: Vec<u8>, public: OpaquePublicKey) -> Self {
36 Self {
37 private: OpaquePrivateKeyBytes(private_key),
38 public,
39 }
40 }
41
42 #[must_use]
44 pub fn algorithm(&self) -> Algorithm {
45 self.public.algorithm()
46 }
47
48 pub(super) fn decode_as(
50 reader: &mut Decoder<impl Reader>,
51 algorithm: Algorithm,
52 ) -> Result<Self> {
53 let key = OpaqueKeypairBytes::decode(reader)?;
54 let public = OpaquePublicKey {
55 algorithm,
56 key: key.public,
57 };
58
59 Ok(Self {
60 public,
61 private: key.private,
62 })
63 }
64}
65
66impl CtEq for OpaqueKeypair {
67 fn ct_eq(&self, other: &Self) -> Choice {
68 Choice::from(u8::from(self.public == other.public)) & self.private.ct_eq(&other.private)
69 }
70}
71
72impl Debug for OpaqueKeypair {
73 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74 f.debug_struct("OpaqueKeypair")
75 .field("public", &self.public)
76 .finish_non_exhaustive()
77 }
78}
79
80impl Encode for OpaqueKeypair {
81 fn encoded_len(&self) -> encoding::Result<usize> {
82 [self.public.encoded_len()?, self.private.encoded_len()?].checked_sum()
83 }
84
85 fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
86 self.public.encode(writer)?;
87 self.private.encode(writer)?;
88
89 Ok(())
90 }
91}
92
93impl From<&OpaqueKeypair> for OpaquePublicKey {
94 fn from(keypair: &OpaqueKeypair) -> OpaquePublicKey {
95 keypair.public.clone()
96 }
97}
98
99pub struct OpaqueKeypairBytes {
105 pub private: OpaquePrivateKeyBytes,
107 pub public: OpaquePublicKeyBytes,
109}
110
111impl Debug for OpaqueKeypairBytes {
112 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
113 f.debug_struct("OpaqueKeypairBytes")
114 .field("public", &self.public)
115 .finish_non_exhaustive()
116 }
117}
118
119impl Decode for OpaqueKeypairBytes {
120 type Error = Error;
121
122 fn decode(reader: &mut Decoder<impl Reader>) -> Result<Self> {
123 let public = OpaquePublicKeyBytes::decode(reader)?;
124 let private = OpaquePrivateKeyBytes::decode(reader)?;
125
126 Ok(Self { public, private })
127 }
128}
129
130impl Encode for OpaquePrivateKeyBytes {
131 fn encoded_len(&self) -> encoding::Result<usize> {
132 self.0.encoded_len()
133 }
134
135 fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
136 self.0.encode(writer)
137 }
138}
139
140#[derive(Clone)]
145pub struct OpaquePrivateKeyBytes(Vec<u8>);
146
147impl AsRef<[u8]> for OpaquePrivateKeyBytes {
148 fn as_ref(&self) -> &[u8] {
149 &self.0
150 }
151}
152
153impl CtEq for OpaquePrivateKeyBytes {
154 fn ct_eq(&self, other: &Self) -> Choice {
155 self.as_ref().ct_eq(other.as_ref())
156 }
157}
158
159impl Debug for OpaquePrivateKeyBytes {
160 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
161 f.debug_struct("OpaquePrivateKeyBytes")
162 .finish_non_exhaustive()
163 }
164}
165
166impl Decode for OpaquePrivateKeyBytes {
167 type Error = Error;
168
169 fn decode(reader: &mut Decoder<impl Reader>) -> Result<Self> {
170 Ok(Self(Vec::decode(reader)?))
171 }
172}
173
174impl From<Vec<u8>> for OpaquePrivateKeyBytes {
175 fn from(bytes: Vec<u8>) -> Self {
176 Self(bytes)
177 }
178}