Key services

In production, the primary key usually lives in a key service, where it cannot be exported and every use is authorized and logged. Each integration is a separate distribution with the same shape:

  • You build the client. Credentials, region, endpoints, and TLS are configured the way the vendor documents them. The provider never reads credentials, files, or environment variables, and never imports the vendor SDK itself.

  • Every wrap is bound to its envelope. The provider passes a digest of the envelope header to the key service as authenticated data, so a wrapped key cannot be moved into another envelope.

  • Envelopes record the exact key version. They keep opening after the key rotates, and rewrap moves them to the new version.

  • Errors are normalized. Throttling and transient failures become retryable ProviderErrors that the policy’s bounded retries handle. SDK messages, endpoints, and payloads never reach the caller.

The examples below use Policy.enterprise, which requires an audit sink; see Audit and observability for audit_chain.

AWS Key Management Service

python -m pip install --pre "cryptography-suite-aws-kms[boto3]==4.0.0rc1"
import boto3
from botocore.config import Config

from cryptography_suite import KeyRef, Policy, Protector
from cryptography_suite_aws_kms import PROVIDER_ID, AwsKmsProvider

kms = boto3.client(
    "kms",
    region_name="eu-west-1",
    config=Config(connect_timeout=3, read_timeout=5, retries={"total_max_attempts": 1}),
)
protector = Protector(
    provider=AwsKmsProvider(kms),
    policy=Policy.enterprise(approved_providers=[PROVIDER_ID]),
    primary_key=KeyRef(PROVIDER_ID, "alias/payments"),
    audit_sink=audit_chain,
)

Item

Value

Keys

Symmetric encryption keys (SYMMETRIC_DEFAULT), named by key ID, key ARN, or alias.

Recorded version

The key ARN. AWS rotates key material behind the same ARN, so rotation needs no rewrap.

Binding

Encryption context {"cryptography-suite:binding": base64url(SHA-256(header))}, visible in CloudTrail.

To seal

kms:DescribeKey, kms:Encrypt

To open

kms:Decrypt, and kms:DescribeKey when the trusted key is given by alias

Health check

kms:ListKeys

boto3 sets timeouts and retries per client, not per call, so the client must make exactly one attempt per call: retries={"total_max_attempts": 1}. The provider refuses any other client, because its retries would multiply the policy’s. (botocore’s max_attempts counts retries: {"max_attempts": 1} means two attempts.) The policy’s deadline (provider_timeout_ms, 30 seconds by default) is checked between attempts but cannot interrupt a call in flight, so keep connect_timeout plus read_timeout within it. grant_tokens= passes KMS grant tokens.

Google Cloud KMS

python -m pip install --pre "cryptography-suite-gcp-kms[google]==4.0.0rc1"
from google.cloud import kms

from cryptography_suite import KeyRef, Policy, Protector
from cryptography_suite_gcp_kms import PROVIDER_ID, GcpKmsProvider

key = "projects/acme/locations/europe-west4/keyRings/data/cryptoKeys/payments"
protector = Protector(
    provider=GcpKmsProvider(kms.KeyManagementServiceClient(), health_key=key),
    policy=Policy.enterprise(approved_providers=[PROVIDER_ID]),
    primary_key=KeyRef(PROVIDER_ID, key),
    audit_sink=audit_chain,
)

Item

Value

Keys

ENCRYPT_DECRYPT CryptoKeys (GOOGLE_SYMMETRIC_ENCRYPTION), named by CryptoKey resource name. Post-quantum: KEY_ENCAPSULATION keys with ML_KEM_768, ML_KEM_1024, or KEM_XWING, named with an explicit version.

Recorded version

The CryptoKeyVersion resource name. After rotation, rewrap moves envelopes to the new primary version.

Binding

Additional authenticated data "cryptography-suite/v4 binding" 0x00 SHA-256(header); for KEM keys, the HPKE info. Requests and responses carry CRC32C checksums.

To seal

roles/cloudkms.cryptoKeyEncrypter and roles/cloudkms.viewer; for KEM keys, roles/cloudkms.publicKeyViewer and roles/cloudkms.viewer

To open

roles/cloudkms.cryptoKeyDecrypter; for KEM keys, roles/cloudkms.decapsulator

Health check

cloudkms.cryptoKeys.get on health_key

Every call passes retry=None and a timeout of the time left before the operation’s deadline, so the library’s own retries (up to 60 seconds per call by default) never apply.

With a post-quantum key, encapsulation runs locally on the version’s cached public key and needs ML-KEM in pyca/cryptography; unwrapping calls Decapsulate, so the private key never leaves Cloud KMS and the reader needs no ML-KEM support.

pq_version = (
    "projects/acme/locations/europe-west4/keyRings/data/cryptoKeys/archive-pq"
    "/cryptoKeyVersions/1"
)
pq_key = KeyRef(PROVIDER_ID, pq_version.rsplit("/cryptoKeyVersions/", 1)[0], pq_version)

Azure Key Vault and Managed HSM

python -m pip install --pre "cryptography-suite-azure-keyvault[azure]==4.0.0rc1"
from azure.identity import DefaultAzureCredential
from azure.keyvault.keys import KeyClient

from cryptography_suite import KeyRef, Policy, Protector
from cryptography_suite_azure_keyvault import PROVIDER_ID, AzureKeyVaultProvider

client = KeyClient(
    vault_url="https://acme-payments.vault.azure.net",
    credential=DefaultAzureCredential(),
)
protector = Protector(
    provider=AzureKeyVaultProvider(client),
    policy=Policy.enterprise(approved_providers=[PROVIDER_ID]),
    primary_key=KeyRef(PROVIDER_ID, "payments"),
    audit_sink=audit_chain,
)

Item

Value

Keys

RSA or RSA-HSM keys of 2048 bits or more with the wrapKey and unwrapKey operations, in Key Vault or Managed HSM, named by key name.

Recorded version

The key version. After rotation, rewrap moves envelopes to the new version.

Binding

RSA-OAEP-256 has no associated data, so the data key is wrapped together with a digest of the header, checked in constant time on unwrap.

To seal

Key permissions get and wrapKey. With get, the Azure SDK wraps locally with the public key, so Key Vault logs the get, not a wrapKey.

To open

Key permission unwrapKey. The SDK also tries get first and continues without it when it is denied.

Health check

Key permission list

The built-in role Key Vault Crypto User (or Managed HSM Crypto User) grants all of these; a custom role can split sealing from opening. Every call turns the SDK’s retry policy off and carries connection and read timeouts of the time left before the deadline, whatever retry policy the client was built with. Disabled, expired, and not-yet-valid versions are reported as disabled, so opening falls back to another recipient.

HashiCorp Vault Transit

python -m pip install --pre "cryptography-suite-hashicorp-vault[hvac]==4.0.0rc1"
import hvac

from cryptography_suite import KeyRef, Policy, Protector
from cryptography_suite_hashicorp_vault import PROVIDER_ID, VaultTransitProvider

client = hvac.Client(url="https://vault.internal:8200", token=vault_token, timeout=5)
protector = Protector(
    provider=VaultTransitProvider(client, mount_point="transit", health_key="payments"),
    policy=Policy.enterprise(approved_providers=[PROVIDER_ID]),
    primary_key=KeyRef(PROVIDER_ID, "payments"),
    audit_sink=audit_chain,
)

Item

Value

Keys

Transit keys of type aes256-gcm96, aes128-gcm96, or chacha20-poly1305, not derived and not convergent, named by key name.

Recorded version

The key version. After vault write -f transit/keys/<name>/rotate, rewrap moves envelopes to the new version. Versions below min_decryption_version count as disabled.

Binding

Transit associated_data, so Vault itself checks it. This needs a Vault release whose Transit API accepts associated_data.

To seal

read on transit/keys/<name>, update on transit/encrypt/<name>

To open

update on transit/decrypt/<name>

Health check

read on transit/keys/<health_key>

path "transit/keys/payments"    { capabilities = ["read"] }
path "transit/encrypt/payments" { capabilities = ["update"] }
path "transit/decrypt/payments" { capabilities = ["update"] }

hvac sets its timeout per client (30 seconds by default), so keep it within the policy’s deadline. Key names must follow Vault’s own grammar; any other name is refused before a request is built.

Several services at once

A Protector can hold several providers. Every envelope is wrapped for every recipient key, so a second region, a second cloud, or an offline escrow key can open it when the first is unavailable:

from cryptography_suite_aws_kms import PROVIDER_ID as AWS
from cryptography_suite_gcp_kms import PROVIDER_ID as GCP

protector = Protector(
    providers=[AwsKmsProvider(kms), GcpKmsProvider(gcp_client)],
    policy=Policy.enterprise(approved_providers=[AWS, GCP]),
    primary_key=KeyRef(AWS, "alias/payments"),
    additional_keys=[KeyRef(GCP, gcp_key)],
    audit_sink=audit_chain,
)

Check a provider

verify_provider runs the contract every provider must meet against a real test key: binding enforced, tampering rejected, wraps randomized, versions immutable, and more. Run it once against each environment before relying on it:

from cryptography_suite.providers.conformance import verify_provider

verify_provider(AwsKmsProvider(kms), KeyRef(AWS, "alias/conformance-test"))

Writing your own provider is covered in Custom providers.