pub struct Initiator { /* private fields */ }Expand description
Noise initiator state for one connection: the server half.
The server always sends the first handshake message, so this type produces message 1 and
consumes message 2. It starts with the published Sentinel PSK; Initiator::set_psk selects
the key the session will actually use, which message 1 must announce.
The suite is the one the client announced in client/init: a server must run exactly that one,
because the client builds its responder for it. There is no fallback - Initiator::new
refuses a prologue that announces another suite, and Initiator::with_announced_suite takes
the suite from the announcement so the two cannot disagree.
Implementations§
Source§impl Initiator
impl Initiator
Sourcepub fn new(
identity: &Identity,
peer_public: &[u8; 32],
suite: Suite,
prologue: &[u8],
) -> Result<Initiator>
pub fn new( identity: &Identity, peer_public: &[u8; 32], suite: Suite, prologue: &[u8], ) -> Result<Initiator>
Builds the initiator for an explicit peer key and suite.
prologue reaches Noise unchanged, so both sides must mix in the same bytes: the exact
client/init and server/init of a first handshake, or the previous handshake hash on a
re-handshake. When the prologue carries a client/init, the suite it announces must be
suite; a prologue that carries no announcement - a re-handshake hash, which is not
JSON - leaves suite as the only source, and a suite this build does not have is always
refused.
Sourcepub fn with_announced_suite(
identity: &Identity,
peer_public: &[u8; 32],
prologue: &[u8],
) -> Result<Initiator>
pub fn with_announced_suite( identity: &Identity, peer_public: &[u8; 32], prologue: &[u8], ) -> Result<Initiator>
Builds the initiator for the suite the prologue’s client/init announces.
This is the constructor for a first handshake: the announcement decides the suite, so the
two cannot disagree. A prologue that is not the init exchange, that carries no
client/init, that carries two of them, that announces no suite or that announces one
this build does not have is rejected; use Initiator::new on a re-handshake, where the
prologue is the previous handshake hash.
Sourcepub fn set_psk(&mut self, psk: &[u8; 32]) -> Result<()>
pub fn set_psk(&mut self, psk: &[u8; 32]) -> Result<()>
Replaces the pre-shared key used by the rest of the handshake.
The initiator starts with the published Sentinel PSK. Call this before writing message 1 when the handshake announces a private key, because message 1’s payload names the key this call selects; noise mixes the key into message 2 either way, so both sides must use the same value or the peer’s message 2 does not authenticate.
That is the contract: before message 1, or once on the same state after a failed read, which
is what Initiator::read_message_with_sentinel does. Those are the only two call sites
this crate has; any other call replaces a key the handshake has already used.
Sourcepub fn write_message(&mut self, payload: &[u8]) -> Result<Vec<u8>>
pub fn write_message(&mut self, payload: &[u8]) -> Result<Vec<u8>>
Writes Noise handshake message 1 with payload and returns the message.
The payload is the PSK metadata (psk_id, psk_category) the session layer builds; this
module does not interpret it. It travels encrypted, so it must fit into a Noise message.
Sourcepub fn read_message(&mut self, msg: &[u8]) -> Result<Vec<u8>>
pub fn read_message(&mut self, msg: &[u8]) -> Result<Vec<u8>>
Reads Noise handshake message 2 and returns its decrypted payload.
The payload is returned verbatim; Sendspin requires it to be the empty JSON object, which the session layer checks. Fails when the message does not authenticate, which covers a message sealed with another PSK, a changed byte and a truncation.
Sourcepub fn read_message_with_sentinel(
&mut self,
msg: &[u8],
) -> Result<(PskOutcome, Vec<u8>)>
pub fn read_message_with_sentinel( &mut self, msg: &[u8], ) -> Result<(PskOutcome, Vec<u8>)>
Reads Noise handshake message 2, retrying the same bytes under SENTINEL_PSK when they
do not authenticate.
This is Initiator::read_message with the recovery a server needs after announcing a key
its peer does not hold: a message 2 sealed with the Sentinel key fails under the selected
key, and the same bytes then authenticate under the published one. snow restores its
symmetric state when a read fails, so the retry sees the handshake position the first
attempt left; the payload buffer is reused and returned verbatim, as
Initiator::read_message returns it.
The outcome reports which key sealed the message. A message that authenticates is never
retried, whatever payload it carries, and a failure that is not an authentication failure is
returned as it comes. This is for the first handshake of a server that selected a key; a
re-handshake has no fallback and its caller keeps Initiator::read_message.
§Errors
Fails when the selected key is SENTINEL_PSK itself, because there is no second key to
retry under; when the message is shorter than the authentication tag; when it does not
authenticate under either key; and on everything else Initiator::read_message reports.
Sourcepub fn handshake_finished(&self) -> bool
pub fn handshake_finished(&self) -> bool
Returns whether both handshake messages have been processed.
Sourcepub fn into_transport(self) -> Result<TransportState>
pub fn into_transport(self) -> Result<TransportState>
Finishes the handshake and switches to transport mode.
Returns what Responder::into_transport returns: the handshake hash a re-handshake
mixes in and the suite these keys were derived under. Fails if message 2 is still
outstanding, which Initiator::handshake_finished reports ahead of time.