ExWapp.Noise.Handshake (ExWapp v0.1.2)

Copy Markdown View Source

Implements the Noise_XX handshake primitives used by WhatsApp Web.

The state mirrors the Go implementation bundled with whatsmeow. It keeps track of the running hash, the current AEAD key and the handshake counters.

Summary

Functions

Mixes additional authenticated data into the running handshake hash.

Decrypts a handshake payload and updates hash/counter on success.

Derives transport write/read keys from the current handshake salt.

Encrypts a handshake payload using the current key and updates hash/counter.

Derives a new handshake key/salt pair from input key material.

Mixes a Curve25519 shared secret into the handshake key schedule.

Initializes a Noise handshake state with the provided protocol pattern and header.

Types

t()

@type t() :: %ExWapp.Noise.Handshake{
  counter: non_neg_integer(),
  hash: binary(),
  key: binary(),
  salt: binary()
}

Functions

authenticate(state, data)

@spec authenticate(t(), binary()) :: t()

Mixes additional authenticated data into the running handshake hash.

decrypt(state, ciphertext)

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

Decrypts a handshake payload and updates hash/counter on success.

derive_transport_keys(state)

@spec derive_transport_keys(t()) :: {binary(), binary()}

Derives transport write/read keys from the current handshake salt.

encrypt(state, plaintext)

@spec encrypt(t(), binary()) :: {t(), binary()}

Encrypts a handshake payload using the current key and updates hash/counter.

mix_into_key(state, data)

@spec mix_into_key(t(), binary()) :: t()

Derives a new handshake key/salt pair from input key material.

mix_shared_secret(state, private_key, public_key)

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

Mixes a Curve25519 shared secret into the handshake key schedule.

new(pattern \\ "Noise_XX_25519_AESGCM_SHA256\0\0\0\0", header \\ <<>>)

@spec new(binary(), binary()) :: t()

Initializes a Noise handshake state with the provided protocol pattern and header.