Skip to content

Commit d1a6500

Browse files
committed
refactoring
1 parent ebb7c2c commit d1a6500

8 files changed

+221
-38
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""init file for multi-threading examples."""
4+
import aws_encryption_sdk
5+
from aws_cryptographic_materialproviders.mpl.references import IKeyring
6+
from typing import Dict # noqa pylint: disable=wrong-import-order
7+
8+
9+
def encrypt_and_decrypt_with_keyring(
10+
plaintext_data: bytes,
11+
keyring: IKeyring,
12+
esdk_client: aws_encryption_sdk.EncryptionSDKClient
13+
):
14+
"""encrypt_and_decrypt_with_keyring how to encrypt plaintext data using a Raw AES keyring.
15+
16+
Usage: encrypt_and_decrypt_with_keyring(plaintext_data, keyring, esdk_client)
17+
:param plaintext_data: plaintext data you want to encrypt
18+
:type: bytes
19+
:param keyring: Keyring to use for encryption.
20+
:type keyring: IKeyring
21+
:param esdk_client: The Encryption SDK client to use for encryption.
22+
:type esdk_client: aws_encryption_sdk.EncryptionSDKClient
23+
:return: encrypted and decrypted (cycled) plaintext data
24+
:rtype: bytes
25+
"""
26+
encryption_context: Dict[str, str] = {
27+
"encryption": "context",
28+
"is not": "secret",
29+
"but adds": "useful metadata",
30+
"that can help you": "be confident that",
31+
"the data you are handling": "is what you think it is",
32+
}
33+
34+
ciphertext_data, _ = esdk_client.encrypt(
35+
source=plaintext_data,
36+
keyring=keyring,
37+
encryption_context=encryption_context
38+
)
39+
40+
decrypted_plaintext_data, _ = esdk_client.decrypt(
41+
source=ciphertext_data,
42+
keyring=keyring
43+
)
44+
45+
return decrypted_plaintext_data
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""This file contains methdos to use for the multi-threaded Raw AES keyring."""
4+
5+
from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders
6+
from aws_cryptographic_materialproviders.mpl.config import MaterialProvidersConfig
7+
from aws_cryptographic_materialproviders.mpl.models import AesWrappingAlg, CreateRawAesKeyringInput
8+
from aws_cryptographic_materialproviders.mpl.references import IKeyring
9+
import secrets
10+
11+
12+
def create_keyring():
13+
"""Demonstrate how to create a Raw AES keyring.
14+
15+
Usage: create_keyring()
16+
"""
17+
key_name_space = "Some managed raw keys"
18+
key_name = "My 256-bit AES wrapping key"
19+
20+
static_key = secrets.token_bytes(32)
21+
22+
mat_prov: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders(
23+
config=MaterialProvidersConfig()
24+
)
25+
26+
keyring_input: CreateRawAesKeyringInput = CreateRawAesKeyringInput(
27+
key_namespace=key_name_space,
28+
key_name=key_name,
29+
wrapping_key=static_key,
30+
wrapping_alg=AesWrappingAlg.ALG_AES256_GCM_IV12_TAG16
31+
)
32+
33+
keyring: IKeyring = mat_prov.create_raw_aes_keyring(
34+
input=keyring_input
35+
)
36+
37+
return keyring
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""This file contains methdos to use for the multi-threaded Raw RSA keyring."""
4+
from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders
5+
from aws_cryptographic_materialproviders.mpl.config import MaterialProvidersConfig
6+
from aws_cryptographic_materialproviders.mpl.models import CreateRawRsaKeyringInput, PaddingScheme
7+
from aws_cryptographic_materialproviders.mpl.references import IKeyring
8+
9+
10+
from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders
11+
from aws_cryptographic_materialproviders.mpl.config import MaterialProvidersConfig
12+
from aws_cryptographic_materialproviders.mpl.models import CreateRawRsaKeyringInput, PaddingScheme
13+
from aws_cryptographic_materialproviders.mpl.references import IKeyring
14+
from cryptography.hazmat.backends import default_backend as crypto_default_backend
15+
from cryptography.hazmat.primitives import serialization as crypto_serialization
16+
from cryptography.hazmat.primitives.asymmetric import rsa
17+
from typing import Dict # noqa pylint: disable=wrong-import-order
18+
19+
20+
def generate_rsa_keys():
21+
"""Generates a 4096-bit RSA public and private key pair
22+
23+
Usage: generate_rsa_keys()
24+
"""
25+
ssh_rsa_exponent = 65537
26+
bit_strength = 4096
27+
key = rsa.generate_private_key(
28+
backend=crypto_default_backend(),
29+
public_exponent=ssh_rsa_exponent,
30+
key_size=bit_strength
31+
)
32+
33+
# This example choses a particular type of encoding, format and encryption_algorithm
34+
# Users can choose the PublicFormat, PrivateFormat and encryption_algorithm that align most
35+
# with their use-cases
36+
public_key = key.public_key().public_bytes(
37+
encoding=crypto_serialization.Encoding.PEM,
38+
format=crypto_serialization.PublicFormat.SubjectPublicKeyInfo
39+
)
40+
private_key = key.private_bytes(
41+
encoding=crypto_serialization.Encoding.PEM,
42+
format=crypto_serialization.PrivateFormat.TraditionalOpenSSL,
43+
encryption_algorithm=crypto_serialization.NoEncryption()
44+
)
45+
46+
return public_key, private_key
47+
48+
49+
def create_keyring(public_key, private_key):
50+
"""Demonstrate how to create a Raw RSA keyring using the key pair.
51+
52+
Usage: create_keyring(public_key, private_key)
53+
"""
54+
key_name_space = "Some managed raw keys"
55+
key_name = "My 4096-bit RSA wrapping key"
56+
57+
mat_prov: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders(
58+
config=MaterialProvidersConfig()
59+
)
60+
61+
keyring_input: CreateRawRsaKeyringInput = CreateRawRsaKeyringInput(
62+
key_namespace=key_name_space,
63+
key_name=key_name,
64+
padding_scheme=PaddingScheme.OAEP_SHA256_MGF1,
65+
public_key=public_key,
66+
private_key=private_key
67+
)
68+
69+
keyring: IKeyring = mat_prov.create_raw_rsa_keyring(
70+
input=keyring_input
71+
)
72+
73+
return keyring

examples/test/multithreaded/test_i_raw_aes_keyring_multithreaded_example.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

examples/test/multithreaded/test_i_raw_rsa_keyring_multithreaded_example.py

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""Test suite for the Raw AES keyring example with multi-threading."""
4+
from concurrent.futures import ThreadPoolExecutor, as_completed
5+
6+
import pytest
7+
8+
from ...src.multithreading.raw_aes_keyring import create_keyring
9+
from ...src.multithreading import encrypt_and_decrypt_with_keyring
10+
11+
import aws_encryption_sdk
12+
from aws_encryption_sdk import CommitmentPolicy
13+
pytestmark = [pytest.mark.examples]
14+
15+
16+
def test_encrypt_and_decrypt_with_keyring(n_threads=10):
17+
"""Test function for multi-threaded encrypt and decrypt using the Raw AES Keyring example."""
18+
keyring = create_keyring()
19+
plaintext_data = b"Hello World"
20+
esdk_client = aws_encryption_sdk.EncryptionSDKClient(
21+
commitment_policy=CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT
22+
)
23+
24+
with ThreadPoolExecutor(max_workers=n_threads) as executor:
25+
thread_futures = {executor.submit(encrypt_and_decrypt_with_keyring,
26+
plaintext_data=plaintext_data,
27+
keyring=keyring,
28+
esdk_client=esdk_client): i for i in range(n_threads)}
29+
30+
for future in as_completed(thread_futures):
31+
decrypted_plaintext_data = future.result()
32+
assert decrypted_plaintext_data == plaintext_data, \
33+
"Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""Test suite for the Raw RSA keyring example with multi-threading."""
4+
from concurrent.futures import ThreadPoolExecutor, as_completed
5+
6+
import pytest
7+
from ...src.multithreading.raw_rsa_keyring import generate_rsa_keys, create_keyring
8+
from ...src.multithreading import encrypt_and_decrypt_with_keyring
9+
import aws_encryption_sdk
10+
from aws_encryption_sdk import CommitmentPolicy
11+
12+
pytestmark = [pytest.mark.examples]
13+
14+
15+
def test_encrypt_and_decrypt_with_keyring(n_threads=10):
16+
"""Test function for multi-threaded encrypt and decrypt using the Raw RSA Keyring example."""
17+
public_key, private_key = generate_rsa_keys()
18+
keyring = create_keyring(public_key=public_key, private_key=private_key)
19+
plaintext_data = b"Hello World"
20+
esdk_client = aws_encryption_sdk.EncryptionSDKClient(
21+
commitment_policy=CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT
22+
)
23+
24+
with ThreadPoolExecutor(max_workers=n_threads) as executor:
25+
thread_futures = {executor.submit(encrypt_and_decrypt_with_keyring,
26+
plaintext_data=plaintext_data,
27+
keyring=keyring,
28+
esdk_client=esdk_client): i for i in range(n_threads)}
29+
30+
for future in as_completed(thread_futures):
31+
decrypted_plaintext_data = future.result()
32+
assert decrypted_plaintext_data == plaintext_data, \
33+
"Decrypted plaintext should be identical to the original plaintext. Invalid decryption"

0 commit comments

Comments
 (0)