Skip to content

Commit f00fdd2

Browse files
authored
Merge pull request #180 from caitlin-tibbetts/a2b1c1-example
Example for using one KMS CMK with an unsigned algorithm
2 parents 1de8d5c + 4def8ba commit f00fdd2

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

examples/src/one_kms_cmk_unsigned.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
"""Example showing basic encryption and decryption of a value already in memory
14+
using one KMS CMK with an unsigned algorithm.
15+
"""
16+
from aws_encryption_sdk import KMSMasterKeyProvider, decrypt, encrypt
17+
from aws_encryption_sdk.identifiers import Algorithm
18+
19+
20+
def encrypt_decrypt(key_arn, source_plaintext, botocore_session=None):
21+
"""Encrypts and then decrypts a string under one KMS customer master key (CMK) with an unsigned algorithm.
22+
23+
:param str key_arn: Amazon Resource Name (ARN) of the KMS CMK
24+
:param bytes source_plaintext: Data to encrypt
25+
:param botocore_session: existing botocore session instance
26+
:type botocore_session: botocore.session.Session
27+
"""
28+
kwargs = dict(key_ids=[key_arn])
29+
30+
if botocore_session is not None:
31+
kwargs["botocore_session"] = botocore_session
32+
33+
# Create master key provider using the ARN of the key and the session (botocore_session)
34+
kms_key_provider = KMSMasterKeyProvider(**kwargs)
35+
36+
# Encrypt the plaintext using the AWS Encryption SDK. It returns the encrypted message and the header
37+
ciphertext, encrypted_message_header = encrypt(
38+
algorithm=Algorithm.AES_256_GCM_IV12_TAG16_HKDF_SHA256, source=source_plaintext, key_provider=kms_key_provider
39+
)
40+
41+
# Decrypt the encrypted message using the AWS Encryption SDK. It returns the decrypted message and the header
42+
plaintext, decrypted_message_header = decrypt(source=ciphertext, key_provider=kms_key_provider)
43+
44+
# Check if the original message and the decrypted message are the same
45+
assert source_plaintext == plaintext
46+
47+
# Check if the headers of the encrypted message and decrypted message match
48+
assert all(
49+
pair in encrypted_message_header.encryption_context.items()
50+
for pair in decrypted_message_header.encryption_context.items()
51+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
"""Unit test suite for the encryption and decryption using one KMS CMK with an unsigned algorithm example."""
14+
15+
import botocore.session
16+
import pytest
17+
18+
from ..src.one_kms_cmk_unsigned import encrypt_decrypt
19+
from .examples_test_utils import get_cmk_arn
20+
from .examples_test_utils import static_plaintext
21+
22+
23+
pytestmark = [pytest.mark.examples]
24+
25+
26+
def test_one_kms_cmk_unsigned():
27+
plaintext = static_plaintext
28+
cmk_arn = get_cmk_arn()
29+
encrypt_decrypt(key_arn=cmk_arn, source_plaintext=plaintext, botocore_session=botocore.session.Session())

0 commit comments

Comments
 (0)