Attesto.JWE (Attesto v2.28.0)

Copy Markdown View Source

Bounded compact JWE encryption for credential issuance and presentation.

Supports ECDH-ES with a P-256 recipient key and A128GCM or A256GCM. Callers select the algorithms from authenticated protocol metadata and may narrow them with :accepted_algs and :accepted_encs. This module performs no network access or protocol-specific claim validation.

Input and plaintext limits default to 1 MiB. :max_compact_bytes and :max_plaintext_bytes may explicitly override them. Protected headers are limited to 16 KiB. Compression, critical extensions, duplicate JSON members, noncanonical Base64URL, and invalid fixed-length GCM parameters are rejected before decryption.

Summary

Functions

Authenticate and decrypt a compact JWE, returning its protected header.

Encrypt bytes to a recipient using an authenticated protected header.

Encrypt locally produced plaintext with outbound-only RFC 7516 DEFLATE compression. The caller's header must omit zip; this function supplies zip: "DEF" and JOSE compresses the bounded plaintext before encryption. Plaintext limits apply before compression. decrypt/3 still refuses zip before handing ciphertext to JOSE, and never inflates received input.

Split a compact JWE and decode its protected header without decrypting it, enforcing core's 16384-byte protected-header limit.

Types

error()

@type error() ::
  :invalid_jwe | :invalid_key | :invalid_header | :invalid_options | :size_limit

key()

@type key() :: JOSE.JWK.t() | map()

Functions

decrypt(recipient, compact, opts \\ [])

@spec decrypt(key(), binary(), keyword()) ::
  {:ok, binary(), map()} | {:error, error()}

Authenticate and decrypt a compact JWE, returning its protected header.

The recipient must include private key material. All compact and protected header checks run before JOSE processes the ciphertext.

encrypt(recipient, plaintext, header, opts \\ [])

@spec encrypt(key(), binary(), map(), keyword()) ::
  {:ok, binary()} | {:error, error()}

Encrypt bytes to a recipient using an authenticated protected header.

The header must include alg and enc. JOSE generates a fresh ephemeral key; callers cannot supply epk. Private recipient keys are reduced to their public part before encryption.

encrypt_deflated(recipient, plaintext, header, opts \\ [])

@spec encrypt_deflated(key(), binary(), map(), keyword()) ::
  {:ok, binary()} | {:error, error()}

Encrypt locally produced plaintext with outbound-only RFC 7516 DEFLATE compression. The caller's header must omit zip; this function supplies zip: "DEF" and JOSE compresses the bounded plaintext before encryption. Plaintext limits apply before compression. decrypt/3 still refuses zip before handing ciphertext to JOSE, and never inflates received input.

peek_protected_header(compact)

@spec peek_protected_header(binary()) ::
  {:ok, map()} | {:error, :invalid_jwe | :invalid_header}

Split a compact JWE and decode its protected header without decrypting it, enforcing core's 16384-byte protected-header limit.