pub struct HandshakeState<D: DH, C: Cipher, H: Hash> { /* private fields */ }
Expand description

Noise handshake state.

Implementations§

source§

impl<D, C, H> HandshakeState<D, C, H>
where D: DH, C: Cipher, H: Hash,

source

pub fn new<P>( pattern: HandshakePattern, is_initiator: bool, prologue: P, s: Option<D::Key>, e: Option<D::Key>, rs: Option<D::Pubkey>, re: Option<D::Pubkey> ) -> Self
where P: AsRef<[u8]>,

Initialize a handshake state.

If e is None, a new ephemeral key will be generated if necessary when write_message.

Setting Explicit Ephemeral Key

An explicit e should only be specified for testing purposes, or in fallback patterns. If you do pass in an explicit e, HandshakeState will use it as is and will not generate new ephemeral keys in write_message.

source

pub fn get_next_message_overhead(&self) -> usize

Calculate the size overhead of the next message.

Panics

If these is no more message to read/write, i.e., if the handshake is already completed.

source

pub fn write_message_vec(&mut self, payload: &[u8]) -> Result<Vec<u8>, Error>

Like write_message, but returns a Vec.

source

pub fn write_message( &mut self, payload: &[u8], out: &mut [u8] ) -> Result<(), Error>

Takes a payload and write the generated handshake message to out.

Error Kinds
  • DH: DH operation failed.
  • NeedPSK: A PSK token is encountered but none is available.
Panics
  • If a required static key is not set.

  • If out.len() != payload.len() + self.get_next_message_overhead().

  • If it is not our turn to write.

  • If the handshake has already completed.

source

pub fn read_message(&mut self, data: &[u8], out: &mut [u8]) -> Result<(), Error>

Takes a handshake message, process it and update our internal state, and write the encapsulated payload to out.

Error Kinds
  • DH: DH operation failed.
  • NeedPSK: A PSK token is encountered but none is available.
  • Decryption: Decryption failed.
Error Recovery

If read_message fails, the whole HandshakeState may be in invalid state and should not be used to read or write any further messages. (But get_re() and get_rs() is allowed.) In case error recovery is desirable, clone the HandshakeState before calling read_message.

Panics
  • If out.len() + self.get_next_message_overhead() != data.len().

    (Notes that this implies data.len() >= overhead.)

  • If a required static key is not set.

  • If it is not our turn to read.

  • If the handshake has already completed.

source

pub fn read_message_vec(&mut self, data: &[u8]) -> Result<Vec<u8>, Error>

Similar to read_message, but returns result as a Vec.

In addition to possible errors from read_message, TooShort may be returned.

source

pub fn push_psk(&mut self, psk: &[u8])

Push a PSK to the PSK-queue.

Panics

If the PSK-queue becomes longer than 4.

source

pub fn completed(&self) -> bool

Whether handshake has completed.

source

pub fn get_hash(&self) -> &[u8]

Get handshake hash. Useful for e.g., channel binding.

source

pub fn get_ciphers(&self) -> (CipherState<C>, CipherState<C>)

Get ciphers that can be used to encrypt/decrypt further messages. The first CipherState is for initiator to responder, and the second for responder to initiator.

Should be called after handshake is completed.

source

pub fn get_rs(&self) -> Option<D::Pubkey>

Get remote static pubkey, if available.

source

pub fn get_re(&self) -> Option<D::Pubkey>

Get remote semi-ephemeral pubkey.

Returns None if we do not know.

Useful for noise-pipes.

source

pub fn get_is_initiator(&self) -> bool

Get whether this HandshakeState is created as initiator.

source

pub fn get_pattern(&self) -> &HandshakePattern

Get handshake pattern this HandshakeState uses.

source

pub fn is_write_turn(&self) -> bool

Check whether it is our turn to send in the handshake state.

Trait Implementations§

source§

impl<D, C, H> Clone for HandshakeState<D, C, H>
where D: DH, C: Cipher, H: Hash,

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<D, C, H> RefUnwindSafe for HandshakeState<D, C, H>
where <C as Cipher>::Key: RefUnwindSafe, <D as DH>::Key: RefUnwindSafe, <H as Hash>::Output: RefUnwindSafe, <D as DH>::Pubkey: RefUnwindSafe,

§

impl<D, C, H> Send for HandshakeState<D, C, H>
where <C as Cipher>::Key: Send, <D as DH>::Key: Send, <H as Hash>::Output: Send, <D as DH>::Pubkey: Send,

§

impl<D, C, H> Sync for HandshakeState<D, C, H>
where <C as Cipher>::Key: Sync, <D as DH>::Key: Sync, <H as Hash>::Output: Sync, <D as DH>::Pubkey: Sync,

§

impl<D, C, H> Unpin for HandshakeState<D, C, H>
where <C as Cipher>::Key: Unpin, <D as DH>::Key: Unpin, <H as Hash>::Output: Unpin, <D as DH>::Pubkey: Unpin,

§

impl<D, C, H> UnwindSafe for HandshakeState<D, C, H>
where <C as Cipher>::Key: UnwindSafe, <D as DH>::Key: UnwindSafe, <H as Hash>::Output: UnwindSafe, <D as DH>::Pubkey: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.