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 |
|---|---|---|
|
Applications that set their own fields |
Nothing |
|
Local development and tests |
4 GiB streams, 10-second deadline, 2 attempts |
|
Production |
Only the listed providers; an audit sink is required |
|
Regulated environments |
Enterprise, plus FIPS-approved wrapping algorithms only, and a FIPS-mode backend |
|
Moving v3 files into v4 |
Permission to read the named v3 formats until the deadline, on top of |
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 |
|---|---|---|---|
|
|
Providers a |
|
|
none |
key length 1–256 |
Context keys every seal and open must supply. |
|
64 KiB |
0 – 1 MiB |
Encoded size of the context. |
|
16 MiB |
0 – 64 MiB |
Plaintext of |
|
100 GiB |
0 – 1 TiB |
Plaintext of a streamed envelope. |
|
64 KiB |
1 B – 4 MiB |
Plaintext per record when sealing. |
|
1 MiB |
1 B – 4 MiB |
Largest record a reader accepts. |
|
64 KiB |
256 B – 1 MiB |
Protected header. |
|
4 |
1 – 32 |
Recipients per envelope, when sealing and reading. |
|
16 KiB |
64 B – 64 KiB |
One wrapped key. |
|
|
Store the sealing time in the header (visible to |
|
|
|
Refuse to run without an audit sink, and fail an operation whose event cannot be recorded. |
|
|
|
Let |
|
|
30,000 |
1 – 600,000 |
Deadline for all key-service calls of one operation, retries included. |
|
3 |
1 – 5 |
Attempts per key-service call; only transient failures are retried. |
|
|
Wrapping algorithms recipients may use, for sealing and opening. |
|
|
|
Refuse to run unless pyca/cryptography’s backend is in FIPS mode. |
|
|
none |
v3 formats that may be migrated; see |
|
|
|
The UTC instant the legacy permission ends; required with |
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 |
|
Google Cloud KMS, symmetric |
|
Google Cloud KMS, ML-KEM |
|
Azure Key Vault |
|
HPKE, P-384 |
|
HPKE, ML-KEM-1024 with P-384 |
|
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.