ExWapp.Media.Crypto (ExWapp v0.1.2)

Copy Markdown View Source

WhatsApp media encryption and decryption helpers.

This follows the same media-key expansion used by popular WhatsApp Web clients: HKDF-SHA256 expands the 32-byte media key into IV, cipher, MAC, and reference keys. The payload is then encrypted with AES-256-CBC and protected with the first 10 bytes of an HMAC-SHA256 digest over IV || ciphertext.

Summary

Functions

Decrypts an encrypted media body, or accepts a server-declared unencrypted body when no encrypted SHA-256 is present, and validates all available integrity checks.

Derives WhatsApp media keys for the given media type.

Encrypts plaintext media bytes and returns upload metadata plus encrypted body.

Returns the WhatsApp HKDF context string for a media type.

Types

media_keys()

@type media_keys() :: %{
  iv: binary(),
  cipher_key: binary(),
  mac_key: binary(),
  ref_key: binary()
}

Functions

decrypt_download(body, ref)

@spec decrypt_download(binary(), ExWapp.Media.Ref.t()) ::
  {:ok, binary()} | {:error, term()}

Decrypts an encrypted media body, or accepts a server-declared unencrypted body when no encrypted SHA-256 is present, and validates all available integrity checks.

derive_keys(media_key, type)

@spec derive_keys(binary(), ExWapp.Media.media_type()) :: media_keys()

Derives WhatsApp media keys for the given media type.

encrypt_upload(plaintext, type)

@spec encrypt_upload(binary(), ExWapp.Media.media_type()) ::
  {:ok, ExWapp.Media.Upload.t(), binary()}

Encrypts plaintext media bytes and returns upload metadata plus encrypted body.

media_info(atom)

@spec media_info(ExWapp.Media.media_type()) :: binary()

Returns the WhatsApp HKDF context string for a media type.