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
+ 4. The original ciphertext is not decryptable using a keyring with a different RSA key pair
23
21
These sanity checks are for demonstration in the example only. You do not need these in your code.
24
22
25
23
A Raw RSA keyring that encrypts and decrypts must include an asymmetric public key and private
26
24
key pair. However, you can encrypt data with a Raw RSA keyring that has only a public key,
27
- and you can decrypt data with a Raw RSA keyring that has only a private key. You can include
28
- any Raw RSA keyring in a multi-keyring. If you configure a Raw RSA keyring with a public and
29
- private key, be sure that they are part of the same key pair. Some language implementations
30
- of the AWS Encryption SDK will not construct a Raw RSA keyring with keys from different pairs.
31
- Others rely on you to verify that your keys are from the same key pair.
25
+ and you can decrypt data with a Raw RSA keyring that has only a private key. This example requires
26
+ the user to either provide both private and public keys, or not provide any keys and the example
27
+ generates both to test encryption and decryption. If you configure a Raw RSA keyring with a
28
+ public and private key, be sure that they are part of the same key pair. Some language
29
+ implementations of the AWS Encryption SDK will not construct a Raw RSA keyring with keys
30
+ from different pairs. Others rely on you to verify that your keys are from the same key pair.
31
+ You can include any Raw RSA keyring in a multi-keyring.
32
32
33
33
For more information on how to use Raw RSA keyrings, see
34
34
https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-raw-rsa-keyring.html
58
58
EXAMPLE_DATA : bytes = b"Hello World"
59
59
60
60
61
- def generate_rsa_keyring ():
62
- """Generates new public and private keys to create a Raw RSA keyring and
63
- then generates the keyring
61
+ def should_generate_new_rsa_key_pair (public_key , private_key ):
62
+ """Returns True if user doesn't provide keys, and we need to generate them and
63
+ returns False if the user has already provided both public and private keys
64
+ Raises an AssertionError if the user only provides one of private_key and public_key
64
65
65
- Usage: generate_rsa_keyring( )
66
+ Usage: should_generate_new_rsa_key_pair(public_key, private_key )
66
67
"""
67
- # 1. The key namespace and key name are defined by you.
68
- # and are used by the Raw RSA keyring
69
- # For more information, see
70
- # https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-raw-rsa-keyring.html
71
- key_name_space = "Some managed raw keys"
72
- key_name = "My 4096-bit RSA wrapping key"
68
+ # If only one of public_key and private_key is provided, raise an Assertion Error
69
+ if (public_key and not private_key ) or (not public_key and private_key ):
70
+ raise AssertionError ("Either both public and private keys should be provided! Or no keys \
71
+ should be provided and the example can create the keys for you!" )
72
+
73
+ # If no keys are provided, we should generate a new rsa key pair, so return True
74
+ if not public_key and not private_key :
75
+ return True
76
+
77
+ # If both keys are already provided, return False
78
+ return False
79
+
73
80
74
- # 2. Generate a 4096-bit RSA key to use with your keyring.
81
+ def generate_rsa_keys ():
82
+ """Generates a 4096-bit RSA public and private key pair
83
+
84
+ Usage: generate_rsa_keys()
85
+ """
75
86
ssh_rsa_exponent = 65537
76
87
bit_strength = 4096
77
88
key = rsa .generate_private_key (
@@ -81,19 +92,34 @@ def generate_rsa_keyring():
81
92
)
82
93
83
94
# This example choses a particular type of encoding, format and encryption_algorithm
84
- # Users can choose the PrivateFormat, PublicFormat and encryption_algorithm that align most
95
+ # Users can choose the PublicFormat, PrivateFormat and encryption_algorithm that align most
85
96
# with their use-cases
97
+ public_key = key .public_key ().public_bytes (
98
+ encoding = crypto_serialization .Encoding .PEM ,
99
+ format = crypto_serialization .PublicFormat .SubjectPublicKeyInfo
100
+ )
86
101
private_key = key .private_bytes (
87
102
encoding = crypto_serialization .Encoding .PEM ,
88
103
format = crypto_serialization .PrivateFormat .TraditionalOpenSSL ,
89
104
encryption_algorithm = crypto_serialization .NoEncryption ()
90
105
)
91
- public_key = key .public_key ().public_bytes (
92
- encoding = crypto_serialization .Encoding .PEM ,
93
- format = crypto_serialization .PublicFormat .SubjectPublicKeyInfo
94
- )
95
106
96
- # 3. Create a Raw RSA keyring
107
+ return public_key , private_key
108
+
109
+
110
+ def create_rsa_keyring (public_key , private_key ):
111
+ """Create a Raw RSA keyring using the key pair
112
+
113
+ Usage: create_rsa_keyring(public_key, private_key)
114
+ """
115
+ # 1. The key namespace and key name are defined by you.
116
+ # and are used by the Raw RSA keyring
117
+ # For more information, see
118
+ # https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-raw-rsa-keyring.html
119
+ key_name_space = "Some managed raw keys"
120
+ key_name = "My 4096-bit RSA wrapping key"
121
+
122
+ # 2. Create a Raw RSA keyring
97
123
mat_prov : AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders (
98
124
config = MaterialProvidersConfig ()
99
125
)
@@ -113,10 +139,12 @@ def generate_rsa_keyring():
113
139
return raw_rsa_keyring
114
140
115
141
116
- def encrypt_and_decrypt_with_keyring ():
117
- """Demonstrate an encrypt/decrypt cycle using a Raw RSA keyring.
142
+ def encrypt_and_decrypt_with_keyring (public_key = None , private_key = None ):
143
+ """Demonstrate an encrypt/decrypt cycle using a Raw RSA keyring
144
+ with user defined keys. If no keys are present, generate new RSA
145
+ public and private keys and use them to create a Raw RSA keyring
118
146
119
- Usage: encrypt_and_decrypt_with_keyring()
147
+ Usage: encrypt_and_decrypt_with_keyring(public_key, private_key )
120
148
"""
121
149
# 1. Instantiate the encryption SDK client.
122
150
# This builds the client with the REQUIRE_ENCRYPT_REQUIRE_DECRYPT commitment policy,
@@ -142,7 +170,18 @@ def encrypt_and_decrypt_with_keyring():
142
170
}
143
171
144
172
# 3. Create a Raw RSA keyring
145
- raw_rsa_keyring = generate_rsa_keyring ()
173
+
174
+ # Check if we need to generate an RSA key pair
175
+ should_generate_new_rsa_key_pair_bool = \
176
+ should_generate_new_rsa_key_pair (public_key = public_key , private_key = private_key )
177
+
178
+ # If user doesn't provide the keys, that is, if should_generate_new_rsa_key_pair_bool is True
179
+ # generate a new RSA public and private key pair
180
+ if should_generate_new_rsa_key_pair_bool :
181
+ public_key , private_key = generate_rsa_keys ()
182
+
183
+ # Create the keyring
184
+ raw_rsa_keyring = create_rsa_keyring (public_key = public_key , private_key = private_key )
146
185
147
186
# 4. Encrypt the data for the encryptionContext
148
187
ciphertext , _ = client .encrypt (
@@ -176,12 +215,16 @@ def encrypt_and_decrypt_with_keyring():
176
215
# decryption of the original ciphertext is not possible with a different keyring (Bob's)
177
216
# (This is an example for demonstration; you do not need to do this in your own code.)
178
217
179
- # 9. Generate a new Raw RSA keyring for Bob
180
- raw_rsa_keyring_bob = generate_rsa_keyring ()
218
+ # 9. Create a new Raw RSA keyring for Bob
219
+ # Generate new keys
220
+ public_key_bob , private_key_bob = generate_rsa_keys ()
221
+
222
+ # Create the keyring
223
+ raw_rsa_keyring_bob = create_rsa_keyring (public_key = public_key_bob , private_key = private_key_bob )
181
224
182
225
# 10. Test decrypt for the original ciphertext using raw_rsa_keyring_bob
183
226
try :
184
- plaintext_bytes_bob , dec_header_bob = client .decrypt (
227
+ plaintext_bytes_bob , _ = client .decrypt (
185
228
source = ciphertext ,
186
229
keyring = raw_rsa_keyring_bob
187
230
)
0 commit comments