pub enum State {
NotInitialized(usize),
HandShake(HandshakeRole),
Transport(NoiseCodec),
}Expand description
Represents the state of the Noise protocol codec during different phases: initialization, handshake, or transport mode, where encryption and decryption are fully operational.
The state transitions from initialization State::NotInitialized to handshake
State::HandShake and finally to transport mode State::Transport as the encryption
handshake is completed.
Variants§
NotInitialized(usize)
The codec has not been initialized yet.
This state is used when the codec is first created or reset, before the handshake process begins. The variant carries the expected size of the handshake message, which can vary depending on whether the codec is acting as an initiator or a responder.
HandShake(HandshakeRole)
The codec is in the handshake phase, where cryptographic keys are being negotiated.
In this state, the codec is in the process of establishing secure communication by
exchanging handshake messages. Once the handshake is complete, the codec transitions to
State::Transport mode.
Transport(NoiseCodec)
The codec is in transport mode, where AEAD encryption and decryption are fully operational.
In this state, the codec is performing full encryption and decryption using the Noise
protocol in transport mode. The NoiseCodec object is responsible for handling the
encryption and decryption of data.
Implementations§
Source§impl State
impl State
Sourcepub fn step_0(&mut self) -> Result<HandShakeFrame, Error>
pub fn step_0(&mut self) -> Result<HandShakeFrame, Error>
Initiates the first step of the handshake process for the initiator.
Creates and sends the initial handshake message for the initiator. It is the first step in establishing a secure communication channel. Responders cannot perform this step.
nb: This method returns a HandShakeFrame but does not change the current state
(self). The state remains State::HandShake(HandshakeRole::Initiator) until step_1 is
called to advance the handshake process.
Sourcepub fn step_1(
&mut self,
re_pub: [u8; 64],
) -> Result<(HandShakeFrame, Self), Error>
pub fn step_1( &mut self, re_pub: [u8; 64], ) -> Result<(HandShakeFrame, Self), Error>
Processes the second step of the handshake process for the responder.
The responder receives the public key from the initiator, generates a response message
containing the handshake frame, and prepares the NoiseCodec for transitioning the
initiator state to transport mode in step_2.
nb: Returns a new state State::Transport but does not update the current state
(self). The caller is responsible for updating the state, allowing for more flexible
control over the handshake process as the caller decides what to do with this state.
Sourcepub fn step_1_with_now_rng<R: Rng + CryptoRng>(
&mut self,
re_pub: [u8; 64],
now: u32,
rng: &mut R,
) -> Result<(HandShakeFrame, Self), Error>
pub fn step_1_with_now_rng<R: Rng + CryptoRng>( &mut self, re_pub: [u8; 64], now: u32, rng: &mut R, ) -> Result<(HandShakeFrame, Self), Error>
Processes the second step of the handshake process for the responder given the current time and a custom random number generator.
See Self::step_1 for more details.
The current time and the custom random number generatorshould be provided in order to not
implicitely rely on std and allow no_std environments to provide a hardware random
number generator for example.
Sourcepub fn step_2(&mut self, message: [u8; 234]) -> Result<Self, Error>
pub fn step_2(&mut self, message: [u8; 234]) -> Result<Self, Error>
Processes the final step of the handshake process for the initiator.
Receives the response message from the responder containing the handshake frame, and
transitions the state to transport mode. This finalizes the secure communication setup and
enables full encryption and decryption in State::Transport mode.
nb: Directly updates the current state (self) to State::Transport, completing the
handshake process.
Sourcepub fn step_2_with_now(
&mut self,
message: [u8; 234],
now: u32,
) -> Result<Self, Error>
pub fn step_2_with_now( &mut self, message: [u8; 234], now: u32, ) -> Result<Self, Error>
Processes the final step of the handshake process for the initiator given the current system time.
See Self::step_2 for more details.
The current system time should be provided to avoid relying on std and allow no_std
environments to use another source of time.
Source§impl State
impl State
Sourcepub fn not_initialized(role: &HandshakeRole) -> Self
pub fn not_initialized(role: &HandshakeRole) -> Self
Creates a new uninitialized handshake State.
Sets the codec to the initial state, State::NotInitialized, based on the provided
handshake role. This state is used before the handshake process begins, and the handshake
message size guides the codec on how much data to expect before advancing to the next step.
The expected size of the handshake message is determined by whether the codec is acting as
an initiator or responder.
Sourcepub fn initialized(inner: HandshakeRole) -> Self
pub fn initialized(inner: HandshakeRole) -> Self
Initializes the codec state to State::HandShake mode with the given handshake role.
Transitions the codec into the handshake phase by accepting a HandshakeRole, which
determines whether the codec is the initiator or responder in the handshake process. Once
in State::HandShake mode, the codec begins negotiating cryptographic keys with the
peer, eventually transitioning to the secure State::Transport phase.
The role passed to this method defines how the handshake proceeds:
HandshakeRole::Initiator: The codec will start the handshake process.HandshakeRole::Responder: The codec will wait for the initiator’s handshake message.
Sourcepub fn with_transport_mode(tm: NoiseCodec) -> Self
pub fn with_transport_mode(tm: NoiseCodec) -> Self
Transitions the codec state to State::Transport mode with the given NoiseCodec.
Finalizes the handshake process and transitions the codec into State::Transport mode,
where full encryption and decryption are active. The codec uses the provided NoiseCodec
to perform encryption and decryption for all communication in this mode, ensuring secure
data transmission.
Once in State::Transport mode, the codec is fully operational for secure communication.