17
17
1. Ciphertext and plaintext data are not the same
18
18
2. Encryption context is correct in the decrypted message header
19
19
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).
20
23
These sanity checks are for demonstration in the example only. You do not need these in your code.
21
24
22
25
A Raw RSA keyring that encrypts and decrypts must include an asymmetric public key and private
34
37
35
38
from aws_cryptographic_materialproviders .mpl import AwsCryptographicMaterialProviders
36
39
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
38
41
from aws_cryptographic_materialproviders .mpl .references import IKeyring
42
+ from aws_cryptographic_materialproviders .smithygenerated .aws_cryptography_materialproviders .errors import (
43
+ CollectionOfErrors ,
44
+ )
39
45
from cryptography .hazmat .backends import default_backend as crypto_default_backend
40
46
from cryptography .hazmat .primitives import serialization as crypto_serialization
41
47
from cryptography .hazmat .primitives .asymmetric import rsa
52
58
EXAMPLE_DATA : bytes = b"Hello World"
53
59
54
60
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
57
64
58
- Usage: encrypt_and_decrypt_with_keyring ()
65
+ Usage: generate_rsa_keyring ()
59
66
"""
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.
84
68
# and are used by the Raw RSA keyring
85
69
# For more information, see
86
70
# https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-raw-rsa-keyring.html
87
71
key_name_space = "Some managed raw keys"
88
72
key_name = "My 4096-bit RSA wrapping key"
89
73
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.
91
75
ssh_rsa_exponent = 65537
92
76
bit_strength = 4096
93
77
key = rsa .generate_private_key (
@@ -109,15 +93,15 @@ def encrypt_and_decrypt_with_keyring():
109
93
format = crypto_serialization .PublicFormat .SubjectPublicKeyInfo
110
94
)
111
95
112
- # 5 . Create a Raw RSA keyring
96
+ # 3 . Create a Raw RSA keyring
113
97
mat_prov : AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders (
114
98
config = MaterialProvidersConfig ()
115
99
)
116
100
117
101
keyring_input : CreateRawRsaKeyringInput = CreateRawRsaKeyringInput (
118
102
key_namespace = key_name_space ,
119
103
key_name = key_name ,
120
- padding_scheme = " OAEP_SHA256_MGF1" ,
104
+ padding_scheme = PaddingScheme . OAEP_SHA256_MGF1 ,
121
105
public_key = public_key ,
122
106
private_key = private_key
123
107
)
@@ -126,30 +110,82 @@ def encrypt_and_decrypt_with_keyring():
126
110
input = keyring_input
127
111
)
128
112
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
130
148
ciphertext , _ = client .encrypt (
131
149
source = EXAMPLE_DATA ,
132
150
keyring = raw_rsa_keyring ,
133
151
encryption_context = encryption_context
134
152
)
135
153
136
- # 7 . Demonstrate that the ciphertext and plaintext are different.
154
+ # 5 . Demonstrate that the ciphertext and plaintext are different.
137
155
# (This is an example for demonstration; you do not need to do this in your own code.)
138
156
assert ciphertext != EXAMPLE_DATA , \
139
157
"Ciphertext and plaintext data are the same. Invalid encryption"
140
158
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.
142
160
plaintext_bytes , dec_header = client .decrypt (
143
161
source = ciphertext ,
144
162
keyring = raw_rsa_keyring
145
163
)
146
164
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
148
166
# (This is an example for demonstration; you do not need to do this in your own code.)
149
167
for k , v in encryption_context .items ():
150
168
assert v == dec_header .encryption_context [k ], \
151
169
"Encryption context does not match expected values"
152
170
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.
154
172
# (This is an example for demonstration; you do not need to do this in your own code.)
155
173
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