aws_encryption_sdk

High level AWS Encryption SDK client functions.

Classes

EncryptionSDKClient(**kwargs)

A client providing high level AWS Encryption SDK client methods.

EncryptionSDKClientConfig([...])

Configuration object for EncryptionSDKClients

class aws_encryption_sdk.EncryptionSDKClientConfig(commitment_policy=CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT, max_encrypted_data_keys=None)

Bases: object

Configuration object for EncryptionSDKClients

Parameters
  • commitment_policy (aws_encryption_sdk.materials_manager.identifiers.CommitmentPolicy) – The commitment policy to apply to encryption and decryption requests

  • max_encrypted_data_keys – The maximum number of encrypted data keys to allow during

encryption and decryption :type max_encrypted_data_keys: None or positive int

Method generated by attrs for class EncryptionSDKClientConfig.

class aws_encryption_sdk.EncryptionSDKClient(**kwargs)

Bases: object

A client providing high level AWS Encryption SDK client methods.

Constructs a new EncryptionSDKClient instance.

encrypt(**kwargs)

Encrypts and serializes provided plaintext.

Note

When using this function, the entire ciphertext message is encrypted into memory before returning any data. If streaming is desired, see aws_encryption_sdk.stream.

>>> import boto3
>>> from aws_cryptographic_material_providers.mpl import AwsCryptographicMaterialProviders
>>> from aws_cryptographic_material_providers.mpl.config import MaterialProvidersConfig
>>> from aws_cryptographic_material_providers.mpl.models import CreateAwsKmsKeyringInput
>>> from aws_cryptographic_material_providers.mpl.references import IKeyring
>>> import aws_encryption_sdk
>>> client = aws_encryption_sdk.EncryptionSDKClient()
>>> mat_prov: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders(
...     config=MaterialProvidersConfig()
... )
>>> keyring_input: CreateAwsKmsKeyringInput = CreateAwsKmsKeyringInput(
...     kms_key_id='arn:aws:kms:us-east-1:2222222222222:key/22222222-2222-2222-2222-222222222222',
...     kms_client=boto3.client('kms', region_name="us-west-2")
... )
>>> kms_keyring: IKeyring = mat_prov.create_aws_kms_keyring(
...     input=keyring_input
... )
>>> my_ciphertext, encryptor_header = client.encrypt(
...     source=my_plaintext,
...     keyring=kms_keyring
... )
Parameters
  • config (aws_encryption_sdk.streaming_client.EncryptorConfig) – Client configuration object (config or individual parameters required)

  • source (str, bytes, io.IOBase, or file) – Source data to encrypt or decrypt

  • materials_manager (aws_encryption_sdk.materials_managers.base.CryptoMaterialsManager) – CryptoMaterialsManager that returns cryptographic materials (requires either materials_manager or keyring)

  • key_provider (aws_encryption_sdk.key_providers.base.MasterKeyProvider) – MasterKeyProvider that returns data keys for encryption (requires either materials_manager or key_provider)

  • keyring (aws_cryptographic_material_providers.mpl.references.IKeyring) – IKeyring that returns keyring for encryption (requires either materials_manager or keyring)

  • source_length (int) –

    Length of source data (optional)

    Note

    If source_length is not provided and unframed message is being written or read() is called, will attempt to seek() to the end of the stream and tell() to find the length of source data.

    Note

    If source_length and materials_manager are both provided, the total plaintext bytes encrypted will not be allowed to exceed source_length. To maintain backwards compatibility, this is not enforced if a keyring is provided.

  • encryption_context (dict) – Dictionary defining encryption context

  • algorithm (aws_encryption_sdk.identifiers.Algorithm) – Algorithm to use for encryption

  • frame_length (int) – Frame length in bytes

Returns

Tuple containing the encrypted ciphertext and the message header object

Return type

tuple of bytes and aws_encryption_sdk.structures.MessageHeader

decrypt(**kwargs)

Deserializes and decrypts provided ciphertext.

Note

When using this function, the entire ciphertext message is decrypted into memory before returning any data. If streaming is desired, see aws_encryption_sdk.stream.

>>> import boto3
>>> from aws_cryptographic_material_providers.mpl import AwsCryptographicMaterialProviders
>>> from aws_cryptographic_material_providers.mpl.config import MaterialProvidersConfig
>>> from aws_cryptographic_material_providers.mpl.models import CreateAwsKmsKeyringInput
>>> from aws_cryptographic_material_providers.mpl.references import IKeyring
>>> import aws_encryption_sdk
>>> client = aws_encryption_sdk.EncryptionSDKClient()
>>> mat_prov: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders(
...     config=MaterialProvidersConfig()
... )
>>> keyring_input: CreateAwsKmsKeyringInput = CreateAwsKmsKeyringInput(
...     kms_key_id='arn:aws:kms:us-east-1:2222222222222:key/22222222-2222-2222-2222-222222222222',
...     kms_client=boto3.client('kms', region_name="us-west-2")
... )
>>> kms_keyring: IKeyring = mat_prov.create_aws_kms_keyring(
...     input=keyring_input
... )
>>> my_plaintext, decryptor_header = client.decrypt(
...     source=my_ciphertext,
...     keyring=kms_keyring
... )
Parameters
  • config (aws_encryption_sdk.streaming_client.DecryptorConfig) – Client configuration object (config or individual parameters required)

  • source (str, bytes, io.IOBase, or file) – Source data to encrypt or decrypt

  • materials_manager (aws_encryption_sdk.materials_managers.base.CryptoMaterialsManager) – CryptoMaterialsManager that returns cryptographic materials (requires either materials_manager or keyring)

  • key_provider (aws_encryption_sdk.key_providers.base.MasterKeyProvider) – MasterKeyProvider that returns data keys for decryption (requires either materials_manager or key_provider)

  • keyring (aws_cryptographic_material_providers.mpl.references.IKeyring) – IKeyring that returns keyring for encryption (requires either materials_manager or keyring)

  • source_length (int) –

    Length of source data (optional)

    Note

    If source_length is not provided and read() is called, will attempt to seek() to the end of the stream and tell() to find the length of source data.

  • encryption_context (dict) – Dictionary defining encryption context to validate on decrypt. This is ONLY validated on decrypt if using a CMM from the aws-cryptographic-material-providers library.

  • max_body_length (int) – Maximum frame size (or content length for non-framed messages) in bytes to read from ciphertext message.

Returns

Tuple containing the decrypted plaintext and the message header object

Return type

tuple of bytes and aws_encryption_sdk.structures.MessageHeader

stream(**kwargs)

Provides an open()-like interface to the streaming encryptor/decryptor classes.

Warning

Take care when decrypting framed messages with large frame length and large non-framed messages. In order to protect the authenticity of the encrypted data, no plaintext is returned until it has been authenticated. Because of this, potentially large amounts of data may be read into memory. In the case of framed messages, the entire contents of each frame are read into memory and authenticated before returning any plaintext. In the case of non-framed messages, the entire message is read into memory and authenticated before returning any plaintext. The authenticated plaintext is held in memory until it is requested.

Note

Consequently, keep the above decrypting consideration in mind when encrypting messages to ensure that issues are not encountered when decrypting those messages.

>>> import boto3
>>> from aws_cryptographic_material_providers.mpl import AwsCryptographicMaterialProviders
>>> from aws_cryptographic_material_providers.mpl.config import MaterialProvidersConfig
>>> from aws_cryptographic_material_providers.mpl.models import CreateAwsKmsKeyringInput
>>> from aws_cryptographic_material_providers.mpl.references import IKeyring
>>> import aws_encryption_sdk
>>> client = aws_encryption_sdk.EncryptionSDKClient()
>>> mat_prov: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders(
...     config=MaterialProvidersConfig()
... )
>>> keyring_input: CreateAwsKmsKeyringInput = CreateAwsKmsKeyringInput(
...     kms_key_id='arn:aws:kms:us-east-1:2222222222222:key/22222222-2222-2222-2222-222222222222',
...     kms_client=boto3.client('kms', region_name="us-west-2")
... )
>>> kms_keyring: IKeyring = mat_prov.create_aws_kms_keyring(
...     input=keyring_input
... )
>>> plaintext_filename = 'my-secret-data.dat'
>>> ciphertext_filename = 'my-encrypted-data.ct'
>>> with open(plaintext_filename, 'rb') as pt_file, open(ciphertext_filename, 'wb') as ct_file:
...     with client.stream(
...         mode='e',
...         source=pt_file,
...         keyring=kms_keyring
...     ) as encryptor:
...         for chunk in encryptor:
...             ct_file.write(chunk)
>>> decrypted_filename = 'my-decrypted-data.dat'
>>> with open(ciphertext_filename, 'rb') as ct_file, open(decrypted_filename, 'wb') as pt_file:
...     with client.stream(
...         mode='d',
...         source=ct_file,
...         keyring=kms_keyring
...     ) as decryptor:
...         for chunk in decryptor:
...             pt_file.write(chunk)
Parameters
  • mode (str) – Type of streaming client to return (e/encrypt: encryptor, d/decrypt: decryptor)

  • **kwargs

    All other parameters provided are passed to the appropriate Streaming client

Returns

Streaming Encryptor or Decryptor, as requested

Return type

aws_encryption_sdk.streaming_client.StreamEncryptor or aws_encryption_sdk.streaming_client.StreamDecryptor

Raises

ValueError – if supplied with an unsupported mode value