|
| 1 | +# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +""" |
| 4 | +This example sets up the default Cryptographic Material Managers (CMM). |
| 5 | +
|
| 6 | +The default cryptographic materials manager (CMM) assembles the cryptographic materials |
| 7 | +that are used to encrypt and decrypt data. The cryptographic materials include |
| 8 | +plaintext and encrypted data keys, and an optional message signing key. |
| 9 | +This example creates a CMM and then encrypts a custom input EXAMPLE_DATA |
| 10 | +with an encryption context. Creating a CMM involves taking a keyring as input, |
| 11 | +and we use an AWS KMS Keyring for this example. |
| 12 | +This example also includes some sanity checks for demonstration: |
| 13 | +1. Ciphertext and plaintext data are not the same |
| 14 | +2. Encryption context is correct in the decrypted message header |
| 15 | +3. Decrypted plaintext value matches EXAMPLE_DATA |
| 16 | +These sanity checks are for demonstration in the example only. You do not need these in your code. |
| 17 | +
|
| 18 | +For more information on Cryptographic Material Managers, see |
| 19 | +https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#crypt-materials-manager |
| 20 | +""" |
| 21 | +import sys |
| 22 | + |
| 23 | +import boto3 |
| 24 | +from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders |
| 25 | +from aws_cryptographic_materialproviders.mpl.config import MaterialProvidersConfig |
| 26 | +from aws_cryptographic_materialproviders.mpl.models import ( |
| 27 | + CreateAwsKmsKeyringInput, |
| 28 | + CreateDefaultCryptographicMaterialsManagerInput, |
| 29 | +) |
| 30 | +from aws_cryptographic_materialproviders.mpl.references import ICryptographicMaterialsManager, IKeyring |
| 31 | +from typing import Dict # noqa pylint: disable=wrong-import-order |
| 32 | + |
| 33 | +import aws_encryption_sdk |
| 34 | +from aws_encryption_sdk import CommitmentPolicy |
| 35 | + |
| 36 | +# TODO-MPL: Remove this as part of removing PYTHONPATH hacks. |
| 37 | +MODULE_ROOT_DIR = '/'.join(__file__.split("/")[:-1]) |
| 38 | + |
| 39 | +sys.path.append(MODULE_ROOT_DIR) |
| 40 | + |
| 41 | +EXAMPLE_DATA: bytes = b"Hello World" |
| 42 | + |
| 43 | + |
| 44 | +def encrypt_and_decrypt_with_default_cmm( |
| 45 | + kms_key_id: str |
| 46 | +): |
| 47 | + """Demonstrate an encrypt/decrypt cycle using default Cryptographic Material Managers. |
| 48 | +
|
| 49 | + Usage: encrypt_and_decrypt_with_default_cmm(kms_key_id) |
| 50 | + :param kms_key_id: KMS Key identifier for the KMS key you want to use for encryption and |
| 51 | + decryption of your data keys. |
| 52 | + :type kms_key_id: string |
| 53 | +
|
| 54 | + For more information on KMS Key identifiers, see |
| 55 | + https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#key-id |
| 56 | + """ |
| 57 | + # 1. Instantiate the encryption SDK client. |
| 58 | + # This builds the client with the REQUIRE_ENCRYPT_REQUIRE_DECRYPT commitment policy, |
| 59 | + # which enforces that this client only encrypts using committing algorithm suites and enforces |
| 60 | + # that this client will only decrypt encrypted messages that were created with a committing |
| 61 | + # algorithm suite. |
| 62 | + # This is the default commitment policy if you were to build the client as |
| 63 | + # `client = aws_encryption_sdk.EncryptionSDKClient()`. |
| 64 | + client = aws_encryption_sdk.EncryptionSDKClient( |
| 65 | + commitment_policy=CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT |
| 66 | + ) |
| 67 | + |
| 68 | + # 2. Create encryption context. |
| 69 | + # Remember that your encryption context is NOT SECRET. |
| 70 | + # For more information, see |
| 71 | + # https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context |
| 72 | + encryption_context: Dict[str, str] = { |
| 73 | + "encryption": "context", |
| 74 | + "is not": "secret", |
| 75 | + "but adds": "useful metadata", |
| 76 | + "that can help you": "be confident that", |
| 77 | + "the data you are handling": "is what you think it is", |
| 78 | + } |
| 79 | + |
| 80 | + # 3. Create a KMS keyring to use with the CryptographicMaterialsManager |
| 81 | + kms_client = boto3.client('kms', region_name="us-west-2") |
| 82 | + |
| 83 | + mat_prov: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders( |
| 84 | + config=MaterialProvidersConfig() |
| 85 | + ) |
| 86 | + |
| 87 | + keyring_input: CreateAwsKmsKeyringInput = CreateAwsKmsKeyringInput( |
| 88 | + kms_key_id=kms_key_id, |
| 89 | + kms_client=kms_client |
| 90 | + ) |
| 91 | + |
| 92 | + kms_keyring: IKeyring = mat_prov.create_aws_kms_keyring( |
| 93 | + input=keyring_input |
| 94 | + ) |
| 95 | + |
| 96 | + # 4. Create a CryptographicMaterialsManager for encryption and decryption |
| 97 | + cmm_input: CreateDefaultCryptographicMaterialsManagerInput = \ |
| 98 | + CreateDefaultCryptographicMaterialsManagerInput( |
| 99 | + keyring=kms_keyring |
| 100 | + ) |
| 101 | + |
| 102 | + cmm: ICryptographicMaterialsManager = mat_prov.create_default_cryptographic_materials_manager( |
| 103 | + input=cmm_input |
| 104 | + ) |
| 105 | + |
| 106 | + # 5. Encrypt the data with the encryptionContext. |
| 107 | + ciphertext, _ = client.encrypt( |
| 108 | + source=EXAMPLE_DATA, |
| 109 | + materials_manager=cmm, |
| 110 | + encryption_context=encryption_context |
| 111 | + ) |
| 112 | + |
| 113 | + # 6. Demonstrate that the ciphertext and plaintext are different. |
| 114 | + # (This is an example for demonstration; you do not need to do this in your own code.) |
| 115 | + assert ciphertext != EXAMPLE_DATA, \ |
| 116 | + "Ciphertext and plaintext data are the same. Invalid encryption" |
| 117 | + |
| 118 | + # 7. Decrypt your encrypted data using the same cmm you used on encrypt. |
| 119 | + plaintext_bytes, dec_header = client.decrypt( |
| 120 | + source=ciphertext, |
| 121 | + materials_manager=cmm |
| 122 | + ) |
| 123 | + |
| 124 | + # 8. Demonstrate that the encryption context is correct in the decrypted message header |
| 125 | + # (This is an example for demonstration; you do not need to do this in your own code.) |
| 126 | + for k, v in encryption_context.items(): |
| 127 | + assert v == dec_header.encryption_context[k], \ |
| 128 | + "Encryption context does not match expected values" |
| 129 | + |
| 130 | + # 9. Demonstrate that the decrypted plaintext is identical to the original plaintext. |
| 131 | + # (This is an example for demonstration; you do not need to do this in your own code.) |
| 132 | + assert plaintext_bytes == EXAMPLE_DATA, \ |
| 133 | + "Decrypted plaintext should be identical to the original plaintext. Invalid decryption" |
0 commit comments