Skip to main content

ssh_key/private/
opaque.rs

1//! Opaque private keys.
2//!
3//! [`OpaqueKeypair`] represents a keypair meant to be used with an algorithm unknown to this
4//! crate, i.e. keypairs that use a custom algorithm as specified in [RFC4251 § 6].
5//!
6//! They are said to be opaque, because the meaning of their underlying byte representation is not
7//! specified.
8//!
9//! [RFC4251 § 6]: https://www.rfc-editor.org/rfc/rfc4251.html#section-6
10
11use 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/// An opaque keypair.
21///
22/// The encoded representation of an `OpaqueKeypair` consists of the encoded representation of its
23/// [`OpaquePublicKey`] followed by the encoded representation of its [`OpaquePrivateKeyBytes`].
24#[derive(Clone)]
25pub struct OpaqueKeypair {
26    /// The opaque private key
27    pub private: OpaquePrivateKeyBytes,
28    /// The opaque public key
29    pub public: OpaquePublicKey,
30}
31
32impl OpaqueKeypair {
33    /// Create a new `OpaqueKeypair`.
34    #[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    /// Get the [`Algorithm`] for this key type.
43    #[must_use]
44    pub fn algorithm(&self) -> Algorithm {
45        self.public.algorithm()
46    }
47
48    /// Decode [`OpaqueKeypair`] for the specified algorithm.
49    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
99/// The underlying representation of an [`OpaqueKeypair`].
100///
101/// The encoded representation of an `OpaqueKeypairBytes` consists of the encoded representation of
102/// its [`OpaquePublicKeyBytes`] followed by the encoded representation of its
103/// [`OpaquePrivateKeyBytes`].
104pub struct OpaqueKeypairBytes {
105    /// The opaque private key
106    pub private: OpaquePrivateKeyBytes,
107    /// The opaque public key
108    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/// An opaque private key.
141///
142/// The encoded representation of an `OpaquePrivateKeyBytes` consists of a 4-byte length prefix,
143/// followed by its byte representation.
144#[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}