ExWapp.Noise (ExWapp v0.1.2)

Copy Markdown View Source

Implements the Noise_XX_25519_AESGCM_SHA256 handshake used by WhatsApp Web.

The module exposes helpers to produce the client hello, process the server hello (including certificate verification) and to encrypt/decrypt transport payloads once the handshake completes.

Summary

Functions

Decrypts a transport payload once the handshake has completed.

Encrypts a transport payload once the handshake has completed.

Processes the server hello frame and returns the client finish frame together with the updated Noise state (now ready for transport encryption/decryption).

Initialises a new Noise handshake state.

Types

certificate_verifier_fun()

@type certificate_verifier_fun() :: (binary(), binary() -> :ok | {:error, term()})

t()

@type t() :: %ExWapp.Noise{
  certificate_verifier: certificate_verifier_fun() | nil,
  cipher_ref: term() | nil,
  client_payload_fun:
    nil
    | (t() ->
         binary()
         | {:ok, binary()}
         | {:error, term()}
         | {:ok, binary(), ExWapp.Store.data()}),
  handshake_hash: binary() | nil,
  handshake_header: binary(),
  handshake_ref: term() | nil,
  handshake_salt: binary() | nil,
  pending_store_data: ExWapp.Store.data() | nil,
  phase: :handshake | :transport,
  rx_counter: non_neg_integer(),
  rx_key: binary() | nil,
  static_key: %{priv: binary(), pub: binary()} | nil,
  store: ExWapp.Store.t() | nil,
  tx_counter: non_neg_integer(),
  tx_key: binary() | nil
}

Functions

decrypt(state, payload)

@spec decrypt(t(), binary()) ::
  {:ok, binary(), t()} | {:error, term(), t()} | {:error, term()}

Decrypts a transport payload once the handshake has completed.

encrypt(state, plaintext)

@spec encrypt(t(), binary()) :: {:ok, binary(), t()} | {:error, term()}

Encrypts a transport payload once the handshake has completed.

handle_server_hello(state, payload)

@spec handle_server_hello(t(), binary()) :: {:ok, binary(), t()} | {:error, term()}

Processes the server hello frame and returns the client finish frame together with the updated Noise state (now ready for transport encryption/decryption).

init(opts)

@spec init(keyword()) :: {:ok, binary(), t()} | {:error, term()}

Initialises a new Noise handshake state.

Returns the binary client hello frame that must be sent immediately together with the updated state.