aws_encryption_sdk

High level AWS Encryption SDK client functions.

Functions

decrypt(**kwargs) Deserializes and decrypts provided ciphertext.
encrypt(**kwargs) Encrypts and serializes provided plaintext.
stream(**kwargs) Provides an open()-like interface to the streaming encryptor/decryptor classes.
aws_encryption_sdk.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.

New in version 2.0.0: The keyring parameter.

New in version 2.0.0: For backwards compatibility, the new CryptoResult return value also unpacks like a 2-member tuple. This allows for backwards compatibility with the previous outputs so this change should not break any existing consumers.

>>> import aws_encryption_sdk
>>> from aws_encryption_sdk.keyrings.aws_kms import AwsKmsKeyring
>>> keyring = AwsKmsKeyring(
...     generator_key_id="arn:aws:kms:us-east-1:2222222222222:key/22222222-2222-2222-2222-222222222222",
...     key_ids=["arn:aws:kms:us-east-1:3333333333333:key/33333333-3333-3333-3333-333333333333"],
... )
>>> my_ciphertext, encryptor_header = aws_encryption_sdk.encrypt(
...     source=my_plaintext,
...     keyring=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 (CryptoMaterialsManager) – Cryptographic materials manager to use for encryption (either materials_manager, keyring, key_provider required)
  • keyring (Keyring) – Keyring to use for encryption (either materials_manager, keyring, key_provider required)
  • key_provider (MasterKeyProvider) – Master key provider to use for encryption (either materials_manager, keyring, key_provider required)
  • 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

    New in version 1.3.0.

    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 key_provider 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:

Encrypted message and message metadata (header)

Return type:

CryptoResult

aws_encryption_sdk.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.

New in version 2.0.0: The keyring parameter.

New in version 2.0.0: For backwards compatibility, the new CryptoResult return value also unpacks like a 2-member tuple. This allows for backwards compatibility with the previous outputs so this change should not break any existing consumers.

>>> import aws_encryption_sdk
>>> from aws_encryption_sdk.keyrings.aws_kms import AwsKmsKeyring
>>> keyring = AwsKmsKeyring(
...     generator_key_id="arn:aws:kms:us-east-1:2222222222222:key/22222222-2222-2222-2222-222222222222",
...     key_ids=["arn:aws:kms:us-east-1:3333333333333:key/33333333-3333-3333-3333-333333333333"],
... )
>>> my_ciphertext, decryptor_header = aws_encryption_sdk.decrypt(
...     source=my_ciphertext,
...     keyring=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 (CryptoMaterialsManager) – Cryptographic materials manager to use for encryption (either materials_manager, keyring, key_provider required)
  • keyring (Keyring) – Keyring to use for encryption (either materials_manager, keyring, key_provider required)
  • key_provider (MasterKeyProvider) – Master key provider to use for encryption (either materials_manager, keyring, key_provider required)
  • 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.

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

Decrypted plaintext and message metadata (header)

Return type:

CryptoResult

aws_encryption_sdk.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 aws_encryption_sdk
>>> from aws_encryption_sdk.keyrings.aws_kms import AwsKmsKeyring
>>> keyring = AwsKmsKeyring(
...     generator_key_id="arn:aws:kms:us-east-1:2222222222222:key/22222222-2222-2222-2222-222222222222",
...     key_ids=["arn:aws:kms:us-east-1:3333333333333:key/33333333-3333-3333-3333-333333333333"],
... )
>>> 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 aws_encryption_sdk.stream(
...         mode='e',
...         source=pt_file,
...         keyring=keyring,
...     ) as encryptor:
...         for chunk in encryptor:
...              ct_file.write(chunk)
>>> new_plaintext_filename = 'my-decrypted-data.dat'
>>> with open(ciphertext_filename, 'rb') as ct_file, open(new_plaintext_filename, 'wb') as pt_file:
...     with aws_encryption_sdk.stream(
...         mode='d',
...         source=ct_file,
...         keyring=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