Skip to main content

RFC9420

Pure Messaging Layer Security (MLS, RFC 9420) library in Python.

Documentation: Full docs (getting started, API reference, examples, architecture) are built with MkDocs and published to GitHub Pages. If the repo has Pages enabled, they are at https://MFDI-hub.github.io/PyMLS/ (or https://MFDI-hub.github.io/PyMLS/ for an org repo). To read them locally: uv sync --group docs then uv run mkdocs serve.

Overview

RFC9420 is a minimal, pragmatic implementation of the Messaging Layer Security protocol as specified in RFC 9420. The library provides a clean Python API for creating and managing MLS groups, handling cryptographic operations, and interoperating with other MLS implementations.

Status

  • ✅ Core wire types (MLSPlaintext, MLSCiphertext, Welcome, GroupInfo) implemented
  • ✅ Group state machine for Add/Update/Remove, commit create/process (RFC-aligned ordering)
  • ✅ Ratchet tree with RFC-style parent-hash validation
  • ✅ Welcome processing with full ratchet_tree extension (internal nodes included)
  • ✅ Config-driven API: GroupConfig, MLSGroup, MLSGroupSession, StagedCommit
  • ✅ RFC 9420 test vector support
  • ✅ External commit and PSK support
  • ✅ X.509 credential verification and revocation helpers

Quickstart

import secrets

from rfc9420 import (
    GroupConfig,
    DefaultCryptoProvider,
    MemoryStorageProvider,
    MLSGroupSession,
)
from rfc9420.extensions.extensions import build_capabilities_data
from rfc9420.messages.data_structures import Credential, Signature
from rfc9420.messages.key_packages import KeyPackage, LeafNode

crypto = DefaultCryptoProvider()
storage = MemoryStorageProvider()
config = GroupConfig(crypto_provider=crypto, storage_provider=storage)


def make_member(identity: bytes):
    """Return (KeyPackage, init private key, leaf signature private key)."""
    sig_sk = secrets.token_bytes(32)
    sig_pk = crypto.signature_public_from_private(sig_sk)
    _enc_sk, enc_pk = crypto.generate_key_pair()
    init_sk, init_pk = crypto.generate_key_pair()
    caps = build_capabilities_data(
        ciphersuite_ids=[crypto.active_ciphersuite.suite_id],
        supported_exts=[],
        include_grease=False,
    )
    cred = Credential(identity=identity, public_key=sig_pk)
    unsigned_leaf = LeafNode(
        encryption_key=enc_pk,
        signature_key=sig_pk,
        credential=cred,
        capabilities=caps,
        lifetime_not_before=0,
        lifetime_not_after=(1 << 64) - 1,
    )
    leaf_sig = crypto.sign_with_label(sig_sk, b"LeafNodeTBS", unsigned_leaf.tbs_serialize())
    leaf = LeafNode(
        encryption_key=enc_pk,
        signature_key=sig_pk,
        credential=cred,
        capabilities=caps,
        lifetime_not_before=0,
        lifetime_not_after=(1 << 64) - 1,
        signature=Signature(leaf_sig),
    )
    unsigned_kp = KeyPackage(init_key=init_pk, leaf_node=leaf)
    kp_sig = crypto.sign_with_label(sig_sk, b"KeyPackageTBS", unsigned_kp.tbs_serialize())
    kp = KeyPackage(init_key=init_pk, leaf_node=leaf, signature=Signature(kp_sig))
    return kp, init_sk, sig_sk


# Creator (A). add_member queues the Add locally; commit then builds the Welcome.
kp_a, _init_sk_a, sig_sk_a = make_member(b"userA")
session_a = MLSGroupSession.create_with_config(config, b"group1", kp_a)

# Joiner (B). Welcome is encrypted to init_key, not the leaf encryption key.
kp_b, init_sk_b, _sig_sk_b = make_member(b"userB")
session_a.add_member(kp_b, sig_sk_a)
_commit_bytes, welcomes = session_a.commit(sig_sk_a)

session_b = MLSGroupSession.join_from_welcome_with_config(
    config, welcomes[0], init_sk_b, key_package=kp_b
)

ct = session_a.protect_application(b"hello", sig_sk_a)
sender, pt = session_b.unprotect_application(ct)
assert (sender, pt) == (0, b"hello")

Installation

This project uses pyproject.toml (PEP 621). Prefer uv for dependency management:

pipx install uv  # once
uv sync --dev

# Lint, type-check, test
uv run ruff check .
uv run mypy src
uv run pytest -q

State at rest

MemoryStorageProvider keeps state in process memory. SQLiteStorageProvider writes it to a file. The tree snapshot contains private HPKE keys and the state payload contains epoch secrets.

Without at_rest_key, those blobs are plaintext in the database file. Pass a 32-byte key to encrypt them with AES-256-GCM. The provider also sets the file mode to 0o600 where the platform honors POSIX permissions. Key packages and proposals are stored with their TLS encoding; pickle is not accepted.

import secrets
from rfc9420.backends.storage import SQLiteStorageProvider

storage = SQLiteStorageProvider("group.sqlite", at_rest_key=secrets.token_bytes(32))

Keep at_rest_key outside the database. A lost key makes the file unreadable.

Core Features

Group Operations

Use GroupConfig (crypto + storage + optional identity providers) and MLSGroupSession for the high-level API:

  • MLSGroupSession.create_with_config(config, group_id, key_package): Create a new group with an initial member
  • MLSGroupSession.join_from_welcome_with_config(config, welcome, init_private_key, key_package): Join via Welcome. init_private_key decrypts the Welcome; key_package identifies the joiner's leaf.
  • MLSGroupSession.deserialize_with_config(config, data): Restore a session from serialized state
  • session.add_member(key_package, signing_key), session.update_self(...), session.remove_member(...): Create a proposal, queue it on this session, and return the bytes to send. Do not process_proposal those bytes locally.
  • session.process_proposal(handshake_bytes, sender_leaf_index=None): Queue a proposal received from another member. The sender is read from the authenticated message.
  • session.commit(signing_key): Create a commit (staged merge + apply)
  • session.apply_commit(handshake_bytes, sender_leaf_index=None): Apply a received commit. The committer is read from the authenticated message.
  • session.protect_application(plaintext, signing_key) / session.unprotect_application(ciphertext_bytes): Application data. signing_key is required.

For staged commits and direct group access, use MLSGroup and StagedCommit from rfc9420.

Ciphersuites

FC9420 supports all RFC 9420 §16.3 ciphersuites:

  • MLS_128_DHKEMX25519_AES128GCM_SHA256_Ed25519 (0x0001) - Default
  • MLS_128_DHKEMP256_AES128GCM_SHA256_P256 (0x0002)
  • MLS_128_DHKEMX25519_CHACHAPOLY_SHA256_Ed25519 (0x0003)
  • MLS_128_DHKEMP256_CHACHAPOLY_SHA256_P256 (0x0004)
  • MLS_256_DHKEMX448_AES256GCM_SHA512_Ed448 (0x0005)
  • MLS_256_DHKEMP521_AES256GCM_SHA512_P521 (0x0006)
  • MLS_256_DHKEMX448_CHACHAPOLY_SHA512_Ed448 (0x0007)
  • MLS_256_DHKEMP521_CHACHAPOLY_SHA512_P521 (0x0008)

Select a ciphersuite when creating a DefaultCryptoProvider:

crypto = DefaultCryptoProvider(suite_id=0x0001)  # Default

Advanced Features

Proposal-by-Reference

Proposals are cached on receipt using RFC 9420 §5.2 proposal references. Commits include proposal_refs that reference cached proposals. Commit processing validates that adds/removes match the referenced proposals.

External Commit

Groups publish an EXTERNAL_PUB extension in GroupInfo for external parties to join without a Welcome. External commits are path-less commits signed with the group's external key (no membership tag required).

# On the group side: external commit adds a member
commit, welcomes = group._inner.external_commit(key_package, kem_public_key)

# On the external party side: process external commit
group._inner.process_external_commit(commit_message)

Pre-Shared Keys (PSKs)

PSK proposals bind PSKs to commits via a PSK binder in authenticated_data. The binder is computed over a PSK preimage and the commit content. PSKs are integrated into the epoch key schedule.

# Create a PSK proposal
psk_proposal = group._inner.create_psk_proposal(psk_id, signing_key)
group.process_proposal(psk_proposal, sender_index)

# PSK binder is automatically computed and verified during commit processing
commit, welcomes = group.commit(signing_key)

PSK binder verification is strict by default. Configure via MLSGroup.set_strict_psk_binders(False) to relax verification.

Resumption PSK

Export the current resumption PSK for future group resumption:

resumption_psk = group._inner.get_resumption_psk()

Re-Initialization

Re-init allows migrating a group to a new group_id and resetting the epoch:

# Queue ReInit and create commit
commit, welcomes = group._inner.reinit_group_to(new_group_id, signing_key)

# On receipt, the group migrates to the new group_id and resets epoch
group.apply_commit(commit, sender_index)

Secret Tree Skipped-Keys Window

Out-of-order application/handshake decryption is supported via a sliding window of skipped keys. By default, a per-leaf window of 128 generations is enabled.

Configure at group creation:

from rfc9420.protocol.mls_group import MLSGroup

group = MLSGroup.create(
    group_id=b"group1",
    key_package=kp,
    crypto_provider=crypto,
    secret_tree_window_size=128  # Default
)

Older behavior (on-demand derivation without caching) is preserved for generations outside the window.

Ratchet Tree Truncation

The ratchet tree truncates immediately when the rightmost leaf (and all trailing leaves) are blank after a removal (RFC 9420 §7.7). This reduces sparse tails and keeps the tree hash consistent with RFC expectations.

Credentials

KeyPackage verification checks that the credential public key matches the leaf signature key. X.509 credential containers are supported:

from rfc9420.messages.credentials import X509Credential

# Verify X.509 certificate chain
cred = X509Credential.deserialize(cert_der)
cred.verify_chain(trust_roots)

Configure trust roots for chain validation:

group._inner.set_trust_roots([trust_root1_der, trust_root2_der])

X.509 Revocation Helpers

Revocation checks are pluggable. Batteries-included helpers are available:

from rfc9420.backends.identity.x509_revocation import check_ocsp_end_entity, check_crl
from rfc9420.backends.identity.x509_policy import X509Policy, RevocationConfig

policy = X509Policy(
    revocation=RevocationConfig(
        enable_ocsp=True,
        enable_crl=True,
        # Use default fetchers (best-effort HTTP). For offline, provide custom fetchers.
        ocsp_checker=lambda cert_der: check_ocsp_end_entity(cert_der, issuer_der),
        crl_checker=lambda cert_der: check_crl(cert_der, issuer_der),
    )
)

By default, helpers fail-closed on network/responder errors (return revoked). To opt into fail-open behavior, pass fail_open=True to the helper functions.

RFC 9420 Compliance

RFC9420 aligns closely with RFC 9420 semantics:

  • Update Path Derivation: Internode keys use top-down path_secret construction (RFC §7.4). A fresh path_secret is generated at the leaf and ratcheted upward with DeriveSecret(..., "path"). Each node key pair is deterministically derived from DeriveSecret(path_secret, "node").

  • Deterministic KEM DeriveKeyPair: Implements deterministic derivation for DHKEM X25519/X448 (with clamping) and for P-256/P-521 via modular reduction.

  • Proposal Ordering: Commits partition and order proposals according to RFC §12.3:

    1. GroupContextExtensions
    2. Update
    3. Remove
    4. Add
    5. PreSharedKey

    ReInit proposals remain exclusive.

  • PSK Handling: PSK proposals are bound via a commit binder and integrated into the epoch key schedule when present.

  • Transcript Bootstrap: The interim transcript hash is initialized at group creation using an all-zero confirmation tag per RFC §11.

  • Parent Hash Validation: Ratchet tree nodes include parent hash validation per RFC §7.9.

Interop & Testing

RFC 9420 Test Vectors

Run the RFC 9420 test vectors:

python -m rfc9420.interop.test_vectors_runner /path/to/vectors --suite 0x0001

Supported types include key_schedule, tree_math, secret_tree, message_protection, welcome_groupinfo, tree_operations, messages, and encryption. A JSON summary is printed.

Interop CLI (RFC Wire)

The interop CLI exposes RFC-wire encode/decode helpers for handshake and application messages:

# Encode handshake (hex → base64 TLS presentation bytes)
python -m rfc9420.interop.cli wire encode-handshake <hex_plaintext>

# Decode handshake (base64 → hex)
python -m rfc9420.interop.cli wire decode-handshake <b64_wire>

# Encode application (hex → base64)
python -m rfc9420.interop.cli wire encode-application <hex_ciphertext>

# Decode application (base64 → hex)
python -m rfc9420.interop.cli wire decode-application <b64_wire>

This is intended to interoperate with other MLS implementations (e.g., OpenMLS). Wire helpers use the TLS presentation bytes described in RFC 9420 (§6–§7 for handshake, §9 for application).

API Reference

rfc9420.Group

High-level wrapper around MLSGroup providing an ergonomic API.

Methods:

  • create(group_id: bytes, key_package: KeyPackage, crypto: CryptoProvider) -> Group
  • join_from_welcome(welcome: Welcome, hpke_private_key: bytes, crypto: CryptoProvider) -> Group
  • add(key_package: KeyPackage, signing_key: bytes) -> MLSPlaintext
  • update(leaf_node: LeafNode, signing_key: bytes) -> MLSPlaintext
  • remove(removed_index: int, signing_key: bytes) -> MLSPlaintext
  • process_proposal(message: MLSPlaintext, sender_leaf_index: int) -> None
  • commit(signing_key: bytes) -> tuple[MLSPlaintext, list[Welcome]]
  • apply_commit(message: MLSPlaintext, sender_leaf_index: int) -> None
  • protect(application_data: bytes) -> MLSCiphertext
  • unprotect(message: MLSCiphertext) -> tuple[int, bytes]

Properties:

  • epoch: int - Current group epoch
  • group_id: bytes - Group identifier

rfc9420.DefaultCryptoProvider

Concrete CryptoProvider implementation. HPKE comes from rfc9180 (>= 0.4.0); signatures and AEAD primitives come from cryptography.

Constructor:

  • DefaultCryptoProvider(suite_id: int = 0x0001)

Properties:

  • supported_ciphersuites - List of supported RFC suite ids
  • active_ciphersuite - Currently selected MlsCiphersuite

Methods:

  • set_ciphersuite(suite_id: int) -> None - Select a different ciphersuite

rfc9420.protocol.mls_group.MLSGroup

Core protocol implementation. Most users should use Group instead.

Advanced Methods:

  • external_commit(key_package: KeyPackage, kem_public_key: bytes) -> tuple[MLSPlaintext, list[Welcome]]
  • process_external_commit(message: MLSPlaintext) -> None
  • create_psk_proposal(psk_id: bytes, signing_key: bytes) -> MLSPlaintext
  • reinit_group_to(new_group_id: bytes, signing_key: bytes) -> tuple[MLSPlaintext, list[Welcome]]
  • get_resumption_psk() -> bytes
  • set_strict_psk_binders(strict: bool) -> None
  • set_trust_roots(trust_roots: list[bytes]) -> None

Breaking Changes

Update Path Derivation: Internode keys on an update path are now derived using a top-down path_secret construction (RFC §7.4). A single fresh path_secret is generated at the leaf and ratcheted upward with DeriveSecret(..., "path"). Each node key pair is deterministically derived from DeriveSecret(path_secret, "node"). This replaces the previous strategy of generating fresh key pairs and extracting secrets from private keys.

Migration Guidance: Groups created with previous versions that expect the legacy update-path derivation are not wire-compatible. Recreate groups or re-onboard members using new Welcomes.

Enhancements

  • Deterministic KEM DeriveKeyPair for all supported KEMs
  • RFC §12.3 proposal ordering
  • PSK binder integration
  • Transcript bootstrap with zero confirmation tag
  • Secret tree skipped-keys window (default: 128)
  • Ratchet tree truncation per RFC §7.7

Notes

  • The library aims for correctness and clarity first. Recent updates removed MVP shortcuts and aligned with RFC 9420 semantics for group creation, commit ordering, parent hash, and Welcome tree encoding.
  • HPKE is provided by rfc9180 (>= 0.4.0), which uses cryptography. Installs should resolve rfc9180>=0.4.0; 0.3.0 is below the floor.
  • Revocation helpers default to fail-closed unless explicitly configured to fail-open.
  • The legacy DAVE protocol and opcodes were removed. This is a pure MLS library now.
  • GroupContextExtensions proposals are accepted and ordered. Their data is merged into GroupInfo extensions for Welcomes; the GroupContext structure in this codebase remains minimal and does not store extensions explicitly.

License

[Add your license information here]

Contributing

[Add contributing guidelines here]

Metadata

Release files for rfc9420 1.4.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rfc9420 1.4.5
File Size Uploaded Provenance Actions
rfc9420-1.4.5.tar.gz 222.6 kB Without provenance Details for rfc9420-1.4.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for rfc9420 1.4.5
File Interpreter ABI Platform Actions
rfc9420-1.4.5-py3-none-any.whl Python 3 none any Details for rfc9420-1.4.5-py3-none-any.whl

Total release size: 384.4 kB

Release files / rfc9420-1.4.5.tar.gz

Download URL rfc9420-1.4.5.tar.gz
Size 222.6 kB
Tags Source
SHA-256 checksum
How to use checksums
90f2bbc2c6c0c99a69ff37cc534020cea4d6505d28b3e25b2651d220281ab067
BLAKE2b-256 checksum
How to use checksums
ceb2d5cbd3d5acecb7bcbe3ee83caeef6ac17ec026c66e907ec4a89564676e74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / rfc9420-1.4.5-py3-none-any.whl

Download URL rfc9420-1.4.5-py3-none-any.whl
Size 161.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6f583f7bba80fe028cb1c2ae56e89ebba3b9f93ae40556e48e9ab42d90bc7820
BLAKE2b-256 checksum
How to use checksums
13673cb37f7f36b6c4f72700edc321b5941f05168079daacccb9ed9dfc0c81a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for rfc9420 1.4.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page