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::{Decode, Decoder, Encode, Reader};
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, Encode)]
25pub struct OpaqueKeypair {
26    /// The opaque public key
27    pub public: OpaquePublicKey,
28    /// The opaque private key
29    pub private: OpaquePrivateKeyBytes,
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            public,
38            private: OpaquePrivateKeyBytes(private_key),
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 From<&OpaqueKeypair> for OpaquePublicKey {
81    fn from(keypair: &OpaqueKeypair) -> OpaquePublicKey {
82        keypair.public.clone()
83    }
84}
85
86/// The underlying representation of an [`OpaqueKeypair`].
87///
88/// The encoded representation of an `OpaqueKeypairBytes` consists of the encoded representation of
89/// its [`OpaquePublicKeyBytes`] followed by the encoded representation of its
90/// [`OpaquePrivateKeyBytes`].
91#[derive(Decode)]
92#[ssh(error = Error)]
93pub struct OpaqueKeypairBytes {
94    /// The opaque public key
95    pub public: OpaquePublicKeyBytes,
96    /// The opaque private key
97    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/// An opaque private key.
109///
110/// The encoded representation of an `OpaquePrivateKeyBytes` consists of a 4-byte length prefix,
111/// followed by its byte representation.
112#[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}