Skip to content

AWS KMS protection

Optional dependency and release status

This API is included in the 0.2.0 release. From the reviewed source checkout, install its locked dependencies with uv sync --extra aws-kms --locked. Use the exact-version command in the installation guide only after the signed release workflow completes.

aws_kms

AWS KMS envelope protection without an AWS SDK dependency in the runtime.

SafeReference module-attribute

SafeReference = Annotated[str, StringConstraints(min_length=1, max_length=255), AfterValidator(_validate_safe_reference)]

_DATA_KEY_BYTES module-attribute

_DATA_KEY_BYTES = 32

_NONCE_BYTES module-attribute

_NONCE_BYTES = 12

_CODEC module-attribute

_CODEC = 'aws-kms-envelope-aes256-gcm-v1'

_ALGORITHM module-attribute

_ALGORITHM = 'hmac-sha256'

EnvelopePurpose module-attribute

EnvelopePurpose = Literal['commitment', 'payload']

KmsKeyIdentifier module-attribute

KmsKeyIdentifier = Annotated[str, StringConstraints(min_length=1, max_length=2048), AfterValidator(_validate_kms_key_id)]

_KMS_KEY_ID_ADAPTER module-attribute

_KMS_KEY_ID_ADAPTER = TypeAdapter(KmsKeyIdentifier)

__all__ module-attribute

__all__ = ['AwsKmsEnvelopeProtection', 'GeneratedDataKey', 'KmsDataKeyClient', 'KmsKeyIdentifier', 'WrappedDataKey', 'WrappedDataKeyDeleteOutcome', 'WrappedDataKeyPersistenceOutcomeUnknownError', 'WrappedDataKeyStore']

KeyedCommitment

Bases: ExperimentalModel

Opaque commitment metadata; key material remains with the host.

algorithm instance-attribute

algorithm: str

key_handle instance-attribute

key_handle: SafeReference

key_version instance-attribute

key_version: SafeReference

digest instance-attribute

digest: SafeReference

ProposalBoundCommitmentProvider

Bases: Protocol

Commitment provider bound to a complete tenant-scoped proposal identity.

create_for async

create_for(*, proposal_identity: ProposalIdentity, canonical_payload: bytes) -> KeyedCommitment

verify_for async

verify_for(*, proposal_identity: ProposalIdentity, canonical_payload: bytes, commitment: KeyedCommitment) -> bool

destroy_commitment_for async

destroy_commitment_for(*, proposal_identity: ProposalIdentity, commitment: KeyedCommitment) -> None

ProposalBoundProtectionCodec

Bases: Protocol

Protection codec bound to a complete tenant-scoped proposal identity.

protect_for async

protect_for(*, proposal_identity: ProposalIdentity, canonical_payload: bytes) -> ProtectedPayload

unprotect_for async

unprotect_for(*, proposal_identity: ProposalIdentity, payload: ProtectedPayload) -> bytes

destroy_payload_for async

destroy_payload_for(*, proposal_identity: ProposalIdentity, payload: ProtectedPayload) -> None

ProtectedPayload

Bases: ExperimentalModel

A private snapshot protected before it reaches an action store.

codec instance-attribute

key_handle instance-attribute

key_handle: SafeReference

key_version instance-attribute

key_version: SafeReference

ciphertext instance-attribute

ciphertext: OpaquePayload

ExperimentalModel

Bases: ActionModel

Compatibility base for contracts that are explicitly experimental.

ProposalIdentity

Bases: ExperimentalModel

Complete tenant-scoped identity of a durable action proposal.

tenant_reference instance-attribute

tenant_reference: SafeReference

proposal_reference instance-attribute

proposal_reference: SafeReference

GeneratedDataKey dataclass

GeneratedDataKey(plaintext: bytearray, ciphertext: bytes, resolved_key_id: str)

One mutable plaintext data key and its KMS-wrapped representation.

plaintext class-attribute instance-attribute

plaintext: bytearray = field(repr=False)

ciphertext class-attribute instance-attribute

ciphertext: bytes = field(repr=False)

resolved_key_id instance-attribute

resolved_key_id: str

WrappedDataKey

Bases: ExperimentalModel

Durable proposal binding for one KMS-wrapped data key.

model_config class-attribute instance-attribute

model_config = ConfigDict(hide_input_in_errors=True, ser_json_bytes='base64', val_json_bytes='base64')

tenant_reference instance-attribute

tenant_reference: SafeReference

proposal_reference instance-attribute

proposal_reference: SafeReference

purpose instance-attribute

purpose: EnvelopePurpose

key_id instance-attribute

key_version instance-attribute

key_version: SafeReference

ciphertext instance-attribute

ciphertext: Annotated[bytes, Field(min_length=1, repr=False)]

WrappedDataKeyDeleteOutcome

Bases: StrEnum

Authoritative result of atomic proposal-bound wrapped-key deletion.

DELETED class-attribute instance-attribute

DELETED = 'deleted'

ALREADY_ABSENT class-attribute instance-attribute

ALREADY_ABSENT = 'already_absent'

MISMATCH class-attribute instance-attribute

MISMATCH = 'mismatch'

KmsDataKeyClient

Bases: Protocol

Minimal async port a host adapts to its AWS KMS client.

Generate adapters must copy AWS's plaintext response into a bytearray and discard the SDK response before returning. The protection adapter overwrites that buffer after use.

generate_data_key async

generate_data_key(*, key_id: str, encryption_context: Mapping[str, str], number_of_bytes: int) -> GeneratedDataKey

decrypt_data_key async

decrypt_data_key(*, key_id: str, ciphertext: bytes, encryption_context: Mapping[str, str]) -> bytes | bytearray

WrappedDataKeyStore

Bases: Protocol

Durable, authoritative storage for proposal-scoped wrapped keys.

A completed write, including one whose acknowledgement is lost, must be visible to a subsequent get through the same adapter.

put async

put(*, key_handle: str, envelope: WrappedDataKey) -> None

get async

get(*, key_handle: str) -> WrappedDataKey | None

delete_if_matches async

delete_if_matches(*, key_handle: str, proposal_identity: ProposalIdentity, purpose: EnvelopePurpose, key_version: str) -> WrappedDataKeyDeleteOutcome

WrappedDataKeyPersistenceOutcomeUnknownError

WrappedDataKeyPersistenceOutcomeUnknownError(*, proposal_identity: ProposalIdentity, key_handle: str, purpose: EnvelopePurpose)

Bases: RuntimeError

Raised when a wrapped-key write cannot be reconciled safely.

proposal_identity instance-attribute

proposal_identity = proposal_identity

key_handle instance-attribute

key_handle = key_handle

purpose instance-attribute

purpose = purpose

AwsKmsEnvelopeProtection

AwsKmsEnvelopeProtection(*, key_id: str, kms: KmsDataKeyClient, envelopes: WrappedDataKeyStore)

Bases: ProposalBoundCommitmentProvider, ProposalBoundProtectionCodec

KMS-wrapped HMAC commitments and AES-256-GCM private snapshots.

The host supplies AWS SDK calls through KmsDataKeyClient and stores wrapped keys separately through WrappedDataKeyStore. Deleting a wrapped proposal key is the idempotent erasure boundary; the shared KMS key remains.

create_for async

create_for(*, proposal_identity: ProposalIdentity, canonical_payload: bytes) -> KeyedCommitment

verify_for async

verify_for(*, proposal_identity: ProposalIdentity, canonical_payload: bytes, commitment: KeyedCommitment) -> bool

destroy_commitment_for async

destroy_commitment_for(*, proposal_identity: ProposalIdentity, commitment: KeyedCommitment) -> None

protect_for async

protect_for(*, proposal_identity: ProposalIdentity, canonical_payload: bytes) -> ProtectedPayload

unprotect_for async

unprotect_for(*, proposal_identity: ProposalIdentity, payload: ProtectedPayload) -> bytes

destroy_payload_for async

destroy_payload_for(*, proposal_identity: ProposalIdentity, payload: ProtectedPayload) -> None

_validate_kms_key_id

_validate_kms_key_id(value: str) -> str

_encryption_context

_encryption_context(*, key_handle: str, proposal_identity: ProposalIdentity, purpose: EnvelopePurpose) -> dict[str, str]

_authenticated_metadata

_authenticated_metadata(*, key_handle: str, proposal_identity: ProposalIdentity, purpose: EnvelopePurpose, key_id: str, key_version: str) -> bytes

_key_version

_key_version(resolved_key_id: str) -> str

_zero_key

_zero_key(key: bytearray) -> None