Skip to main content

ssh_packet/kex/
mod.rs

1//! Facilities to produce some of the _exchange hashes_.
2
3use binrw::binwrite;
4
5use super::{arch, trans};
6
7mod lengthed;
8pub use lengthed::Lengthed;
9
10/// The exchange hash for ECDH, computed as the
11/// hash of the concatenation of the following.
12///
13/// see <https://datatracker.ietf.org/doc/html/rfc5656#section-4>.
14#[binwrite]
15#[derive(Debug)]
16#[bw(big)]
17pub struct Ecdh<'b> {
18    /// Client's identification string (`\r` and `\n` excluded).
19    pub v_c: arch::Bytes<'b>,
20
21    /// Server's identification string (`\r` and `\n` excluded).
22    pub v_s: arch::Bytes<'b>,
23
24    /// Payload of the client's `SSH_MSG_KEXINIT` message.
25    pub i_c: Lengthed<&'b trans::KexInit<'b>>,
26
27    /// Payload of the server's `SSH_MSG_KEXINIT` message.
28    pub i_s: Lengthed<&'b trans::KexInit<'b>>,
29
30    /// Server's public host key.
31    pub k_s: arch::Bytes<'b>,
32
33    /// Client's ephemeral public key octet string.
34    pub q_c: arch::Bytes<'b>,
35
36    /// Server's ephemeral public key octet string.
37    pub q_s: arch::Bytes<'b>,
38
39    /// Computed shared secret.
40    pub k: arch::MpInt<'b>,
41}
42
43impl Ecdh<'_> {
44    /// Produce the exchange hash with the specified digest algorithm.
45    #[cfg(feature = "digest")]
46    #[cfg_attr(docsrs, doc(cfg(feature = "digest")))]
47    pub fn hash<D: digest::Digest>(&self) -> digest::Output<D> {
48        use binrw::BinWrite;
49
50        let mut buffer = Vec::new();
51        self.write(&mut std::io::Cursor::new(&mut buffer))
52            .expect("The binrw structure serialization failed");
53
54        D::digest(&buffer)
55    }
56}