Skip to content

Commit 7799a57

Browse files
committed
chore: updated raw rsa and aes keyrings
1 parent 8039f62 commit 7799a57

File tree

2 files changed

+74
-38
lines changed

2 files changed

+74
-38
lines changed

examples/src/keyrings/raw_aes_keyring_example.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders
2929
from aws_cryptographic_materialproviders.mpl.config import MaterialProvidersConfig
30-
from aws_cryptographic_materialproviders.mpl.models import CreateRawAesKeyringInput
30+
from aws_cryptographic_materialproviders.mpl.models import AesWrappingAlg, CreateRawAesKeyringInput
3131
from aws_cryptographic_materialproviders.mpl.references import IKeyring
3232
from typing import Dict
3333

@@ -91,7 +91,7 @@ def encrypt_and_decrypt_with_keyring():
9191
key_namespace=key_name_space,
9292
key_name=key_name,
9393
wrapping_key=static_key,
94-
wrapping_alg="ALG_AES256_GCM_IV12_TAG16"
94+
wrapping_alg=AesWrappingAlg.ALG_AES256_GCM_IV12_TAG16
9595
)
9696

9797
raw_aes_keyring: IKeyring = mat_prov.create_raw_aes_keyring(

examples/src/keyrings/raw_rsa_keyring_example.py

+72-36
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
1. Ciphertext and plaintext data are not the same
1818
2. Encryption context is correct in the decrypted message header
1919
3. Decrypted plaintext value matches EXAMPLE_DATA
20+
4. After verifying that the encrypt and decrypt works, this example also demonstrates
21+
that the original ciphertext should not be decrypted using a new Raw RSA keyring generated by
22+
another user, let's say Bob (Points 9 and 10).
2023
These sanity checks are for demonstration in the example only. You do not need these in your code.
2124
2225
A Raw RSA keyring that encrypts and decrypts must include an asymmetric public key and private
@@ -34,8 +37,11 @@
3437

3538
from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders
3639
from aws_cryptographic_materialproviders.mpl.config import MaterialProvidersConfig
37-
from aws_cryptographic_materialproviders.mpl.models import CreateRawRsaKeyringInput
40+
from aws_cryptographic_materialproviders.mpl.models import CreateRawRsaKeyringInput, PaddingScheme
3841
from aws_cryptographic_materialproviders.mpl.references import IKeyring
42+
from aws_cryptographic_materialproviders.smithygenerated.aws_cryptography_materialproviders.errors import (
43+
CollectionOfErrors,
44+
)
3945
from cryptography.hazmat.backends import default_backend as crypto_default_backend
4046
from cryptography.hazmat.primitives import serialization as crypto_serialization
4147
from cryptography.hazmat.primitives.asymmetric import rsa
@@ -52,42 +58,20 @@
5258
EXAMPLE_DATA: bytes = b"Hello World"
5359

5460

55-
def encrypt_and_decrypt_with_keyring():
56-
"""Demonstrate an encrypt/decrypt cycle using a Raw RSA keyring.
61+
def generate_rsa_keyring():
62+
"""Generates new public and private keys to create a Raw RSA keyring and
63+
then generates the keyring
5764
58-
Usage: encrypt_and_decrypt_with_keyring()
65+
Usage: generate_rsa_keyring()
5966
"""
60-
# 1. Instantiate the encryption SDK client.
61-
# This builds the client with the REQUIRE_ENCRYPT_REQUIRE_DECRYPT commitment policy,
62-
# which enforces that this client only encrypts using committing algorithm suites and enforces
63-
# that this client will only decrypt encrypted messages that were created with a committing
64-
# algorithm suite.
65-
# This is the default commitment policy if you were to build the client as
66-
# `client = aws_encryption_sdk.EncryptionSDKClient()`.
67-
client = aws_encryption_sdk.EncryptionSDKClient(
68-
commitment_policy=CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT
69-
)
70-
71-
# 2. Create encryption context.
72-
# Remember that your encryption context is NOT SECRET.
73-
# For more information, see
74-
# https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
75-
encryption_context: Dict[str, str] = {
76-
"encryption": "context",
77-
"is not": "secret",
78-
"but adds": "useful metadata",
79-
"that can help you": "be confident that",
80-
"the data you are handling": "is what you think it is",
81-
}
82-
83-
# 3. The key namespace and key name are defined by you.
67+
# 1. The key namespace and key name are defined by you.
8468
# and are used by the Raw RSA keyring
8569
# For more information, see
8670
# https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-raw-rsa-keyring.html
8771
key_name_space = "Some managed raw keys"
8872
key_name = "My 4096-bit RSA wrapping key"
8973

90-
# 4. Generate a 4096-bit RSA key to use with your keyring.
74+
# 2. Generate a 4096-bit RSA key to use with your keyring.
9175
ssh_rsa_exponent = 65537
9276
bit_strength = 4096
9377
key = rsa.generate_private_key(
@@ -109,15 +93,15 @@ def encrypt_and_decrypt_with_keyring():
10993
format=crypto_serialization.PublicFormat.SubjectPublicKeyInfo
11094
)
11195

112-
# 5. Create a Raw RSA keyring
96+
# 3. Create a Raw RSA keyring
11397
mat_prov: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders(
11498
config=MaterialProvidersConfig()
11599
)
116100

117101
keyring_input: CreateRawRsaKeyringInput = CreateRawRsaKeyringInput(
118102
key_namespace=key_name_space,
119103
key_name=key_name,
120-
padding_scheme="OAEP_SHA256_MGF1",
104+
padding_scheme=PaddingScheme.OAEP_SHA256_MGF1,
121105
public_key=public_key,
122106
private_key=private_key
123107
)
@@ -126,30 +110,82 @@ def encrypt_and_decrypt_with_keyring():
126110
input=keyring_input
127111
)
128112

129-
# 6. Encrypt the data for the encryptionContext
113+
return raw_rsa_keyring
114+
115+
116+
def encrypt_and_decrypt_with_keyring():
117+
"""Demonstrate an encrypt/decrypt cycle using a Raw RSA keyring.
118+
119+
Usage: encrypt_and_decrypt_with_keyring()
120+
"""
121+
# 1. Instantiate the encryption SDK client.
122+
# This builds the client with the REQUIRE_ENCRYPT_REQUIRE_DECRYPT commitment policy,
123+
# which enforces that this client only encrypts using committing algorithm suites and enforces
124+
# that this client will only decrypt encrypted messages that were created with a committing
125+
# algorithm suite.
126+
# This is the default commitment policy if you were to build the client as
127+
# `client = aws_encryption_sdk.EncryptionSDKClient()`.
128+
client = aws_encryption_sdk.EncryptionSDKClient(
129+
commitment_policy=CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT
130+
)
131+
132+
# 2. Create encryption context.
133+
# Remember that your encryption context is NOT SECRET.
134+
# For more information, see
135+
# https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
136+
encryption_context: Dict[str, str] = {
137+
"encryption": "context",
138+
"is not": "secret",
139+
"but adds": "useful metadata",
140+
"that can help you": "be confident that",
141+
"the data you are handling": "is what you think it is",
142+
}
143+
144+
# 3. Create a Raw RSA keyring
145+
raw_rsa_keyring = generate_rsa_keyring()
146+
147+
# 4. Encrypt the data for the encryptionContext
130148
ciphertext, _ = client.encrypt(
131149
source=EXAMPLE_DATA,
132150
keyring=raw_rsa_keyring,
133151
encryption_context=encryption_context
134152
)
135153

136-
# 7. Demonstrate that the ciphertext and plaintext are different.
154+
# 5. Demonstrate that the ciphertext and plaintext are different.
137155
# (This is an example for demonstration; you do not need to do this in your own code.)
138156
assert ciphertext != EXAMPLE_DATA, \
139157
"Ciphertext and plaintext data are the same. Invalid encryption"
140158

141-
# 8. Decrypt your encrypted data using the same keyring you used on encrypt.
159+
# 6. Decrypt your encrypted data using the same keyring you used on encrypt.
142160
plaintext_bytes, dec_header = client.decrypt(
143161
source=ciphertext,
144162
keyring=raw_rsa_keyring
145163
)
146164

147-
# 9. Demonstrate that the encryption context is correct in the decrypted message header
165+
# 7. Demonstrate that the encryption context is correct in the decrypted message header
148166
# (This is an example for demonstration; you do not need to do this in your own code.)
149167
for k, v in encryption_context.items():
150168
assert v == dec_header.encryption_context[k], \
151169
"Encryption context does not match expected values"
152170

153-
# 10. Demonstrate that the decrypted plaintext is identical to the original plaintext.
171+
# 8. Demonstrate that the decrypted plaintext is identical to the original plaintext.
154172
# (This is an example for demonstration; you do not need to do this in your own code.)
155173
assert plaintext_bytes == EXAMPLE_DATA
174+
175+
# The next part of the example creates a new RSA keyring (for Bob) to demonstrate that
176+
# decryption of the original ciphertext is not possible with a different keyring (Bob's)
177+
# (This is an example for demonstration; you do not need to do this in your own code.)
178+
179+
# 9. Generate a new Raw RSA keyring for Bob
180+
raw_rsa_keyring_bob = generate_rsa_keyring()
181+
182+
# 10. Test decrypt for the original ciphertext using raw_rsa_keyring_bob
183+
try:
184+
plaintext_bytes_bob, dec_header_bob = client.decrypt(
185+
source=ciphertext,
186+
keyring=raw_rsa_keyring_bob
187+
)
188+
189+
assert False, "client.decrypt should throw a error of type CollectionOfErrors!"
190+
except CollectionOfErrors:
191+
pass

0 commit comments

Comments
 (0)