Policies

A Policy is the authority every operation is checked against: which providers and algorithms may be used, how large inputs may be, how long key services may take, and whether auditing is required. Policies are immutable, serialize to a canonical JSON document, and carry a content-derived identifier, policy_id, that every envelope records.

Profiles

from cryptography_suite import Policy

default = Policy()  # sensible limits, any explicitly configured provider
development = Policy.development()  # lower limits, shorter timeouts
enterprise = Policy.enterprise(approved_providers=["com.amazonaws.kms"])
print(enterprise.profile, enterprise.audit_required)  # enterprise True

Profile

Use

What it adds to the defaults

Policy() (custom)

Applications that set their own fields

Nothing

Policy.development()

Local development and tests

4 GiB streams, 10-second deadline, 2 attempts

Policy.enterprise(approved_providers=...)

Production

Only the listed providers; an audit sink is required

Policy.fips(approved_providers=...)

Regulated environments

Enterprise, plus FIPS-approved wrapping algorithms only, and a FIPS-mode backend

Policy.migration(deadline=..., legacy_formats=..., base=...)

Moving v3 files into v4

Permission to read the named v3 formats until the deadline, on top of base

Setting individual fields gives any other combination:

strict = Policy(
    allowed_provider_ids=frozenset({"com.amazonaws.kms", "cryptography-suite.hpke"}),
    required_context_keys=frozenset({"tenant"}),
    max_plaintext_bytes=1024 * 1024,
    audit_required=False,
)

Fields

Every limit has a hard ceiling that no policy can raise. Readers apply their own policy, so an envelope cannot make a reader allocate more than the reader allows.

Field

Default

Hard bounds

Meaning

allowed_provider_ids

None (any)

Providers a Protector may be built with. Required by enterprise and fips.

required_context_keys

none

key length 1–256

Context keys every seal and open must supply.

max_context_bytes

64 KiB

0 – 1 MiB

Encoded size of the context.

max_plaintext_bytes

16 MiB

0 – 64 MiB

Plaintext of seal, open, and one open_range read.

max_stream_bytes

100 GiB

0 – 1 TiB

Plaintext of a streamed envelope.

chunk_size

64 KiB

1 B – 4 MiB

Plaintext per record when sealing.

max_chunk_size

1 MiB

1 B – 4 MiB

Largest record a reader accepts.

max_header_bytes

64 KiB

256 B – 1 MiB

Protected header.

max_recipients

4

1 – 32

Recipients per envelope, when sealing and reading.

max_recipient_entry_bytes

16 KiB

64 B – 64 KiB

One wrapped key.

record_creation_time

False

Store the sealing time in the header (visible to inspect).

audit_required

False

Refuse to run without an audit sink, and fail an operation whose event cannot be recorded.

allow_overwrite

False

Let file_sink(overwrite=True) replace an existing file.

provider_timeout_ms

30,000

1 – 600,000

Deadline for all key-service calls of one operation, retries included.

provider_max_attempts

3

1 – 5

Attempts per key-service call; only transient failures are retried.

approved_wrapping_algorithms

None (any)

Wrapping algorithms recipients may use, for sealing and opening.

require_fips_backend

False

Refuse to run unless pyca/cryptography’s backend is in FIPS mode.

legacy_formats

none

v3 formats that may be migrated; see Policy.migration. pem-der-unencrypted also admits unencrypted v3 private keys.

migration_deadline

None

The UTC instant the legacy permission ends; required with legacy_formats.

Combine policies

restrict returns the intersection of two policies: the smaller of every limit, the intersection of the provider and algorithm lists, and the union of required context keys. It can only narrow, so a team’s policy can be layered on an organization’s without any risk of loosening it:

organization = Policy.enterprise(
    approved_providers=["com.amazonaws.kms", "com.google.cloud.kms"]
)
team = Policy(
    allowed_provider_ids=frozenset({"com.amazonaws.kms"}),
    required_context_keys=frozenset({"tenant"}),
    max_plaintext_bytes=4 * 1024 * 1024,
)
effective = organization.restrict(team)
assert effective.allowed_provider_ids == frozenset({"com.amazonaws.kms"})
assert effective.audit_required and effective.max_plaintext_bytes == 4 * 1024 * 1024

A restriction that leaves no provider or no algorithm is an error, not a policy that silently denies everything.

Store and review policies

to_json writes the canonical document that defines policy_id; from_json reads it back and rejects unknown fields, so a typo cannot silently fall back to a default:

document = effective.to_json()
assert Policy.from_json(document) == effective
print(effective.policy_id)  # cs-policy-v1:...

Keep policy documents in version control and review changes like code. The command line loads them with --policy, and cryptosuite policy validate checks one offline.

FIPS mode

Policy.fips() admits only recipients whose wrapping algorithm is FIPS-approved, and a Protector with it refuses to start unless pyca/cryptography runs on an OpenSSL FIPS provider (cryptosuite doctor reports fips_backend).

Admitted wrapping algorithm

Recipient

aws-kms-symmetric-default

AWS KMS

gcp-kms-google-symmetric-encryption

Google Cloud KMS, symmetric

gcp-kms-ml-kem-768-hpke-sha256-aes-256-gcm, gcp-kms-ml-kem-1024-hpke-sha384-aes-256-gcm

Google Cloud KMS, ML-KEM

azure-keyvault-rsa-oaep-256-bound

Azure Key Vault

cs4-hpke-p384-hkdf-sha384-aes-256-gcm

HPKE, P-384

cs4-hpke-mlkem1024-p384-hkdf-sha384-aes-256-gcm

HPKE, ML-KEM-1024 with P-384

cs4-hierarchical-hkdf-sha256-aes-256-gcm

Hierarchical branch keys

X25519, X-Wing, and Vault Transit are not admitted. The envelope’s own suite (AES-256-GCM, HKDF-SHA-512, HMAC-SHA-256, SHA-512) uses approved algorithms throughout.

This restricts algorithms; it does not make a product FIPS 140-3 validated. Validation belongs to the cryptographic module, here the OpenSSL FIPS provider and your key service, and to how you deploy them.