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::{Decode, Decoder, Encode, Reader};
19
20#[derive(Clone, Encode)]
25pub struct OpaqueKeypair {
26 pub public: OpaquePublicKey,
28 pub private: OpaquePrivateKeyBytes,
30}
31
32impl OpaqueKeypair {
33 #[must_use]
35 pub fn new(private_key: Vec<u8>, public: OpaquePublicKey) -> Self {
36 Self {
37 public,
38 private: OpaquePrivateKeyBytes(private_key),
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 From<&OpaqueKeypair> for OpaquePublicKey {
81 fn from(keypair: &OpaqueKeypair) -> OpaquePublicKey {
82 keypair.public.clone()
83 }
84}
85
86#[derive(Decode)]
92#[ssh(error = Error)]
93pub struct OpaqueKeypairBytes {
94 pub public: OpaquePublicKeyBytes,
96 pub private: OpaquePrivateKeyBytes,
98}
99
100impl Debug for OpaqueKeypairBytes {
101 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102 f.debug_struct("OpaqueKeypairBytes")
103 .field("public", &self.public)
104 .finish_non_exhaustive()
105 }
106}
107
108#[derive(Clone, Decode, Encode)]
113#[ssh(error = Error)]
114pub struct OpaquePrivateKeyBytes(Vec<u8>);
115
116impl AsRef<[u8]> for OpaquePrivateKeyBytes {
117 fn as_ref(&self) -> &[u8] {
118 &self.0
119 }
120}
121
122impl CtEq for OpaquePrivateKeyBytes {
123 fn ct_eq(&self, other: &Self) -> Choice {
124 self.as_ref().ct_eq(other.as_ref())
125 }
126}
127
128impl Debug for OpaquePrivateKeyBytes {
129 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
130 f.debug_struct("OpaquePrivateKeyBytes")
131 .finish_non_exhaustive()
132 }
133}
134
135impl From<Vec<u8>> for OpaquePrivateKeyBytes {
136 fn from(bytes: Vec<u8>) -> Self {
137 Self(bytes)
138 }
139}