pub struct Client { /* private fields */ }
Implementations§
Source§impl Client
impl Client
Sourcepub fn encrypt(&self) -> EncryptFluentBuilder
pub fn encrypt(&self) -> EncryptFluentBuilder
Constructs a fluent builder for the Encrypt
operation.
- The fluent builder is configurable:
algorithm_suite_id(impl Into<Option<crate::deps::aws_cryptography_materialProviders::types::EsdkAlgorithmSuiteId>>)
/set_algorithm_suite_id(Option<crate::deps::aws_cryptography_materialProviders::types::EsdkAlgorithmSuiteId>)
: (undocumented)encryption_context(impl Into<Option<::std::collections::HashMap<::std::string::String, ::std::string::String>>>)
/set_encryption_context(Option<::std::collections::HashMap<::std::string::String, ::std::string::String>>)
: (undocumented)frame_length(impl Into<Option<::std::primitive::i64>>)
/set_frame_length(Option<::std::primitive::i64>)
: (undocumented)keyring(impl Into<Option<crate::deps::aws_cryptography_materialProviders::types::keyring::KeyringRef>>)
/set_keyring(Option<crate::deps::aws_cryptography_materialProviders::types::keyring::KeyringRef>)
: (undocumented)materials_manager(impl Into<Option<crate::deps::aws_cryptography_materialProviders::types::cryptographic_materials_manager::CryptographicMaterialsManagerRef>>)
/set_materials_manager(Option<crate::deps::aws_cryptography_materialProviders::types::cryptographic_materials_manager::CryptographicMaterialsManagerRef>)
: (undocumented)plaintext(impl Into<Option<::aws_smithy_types::Blob>>)
/set_plaintext(Option<::aws_smithy_types::Blob>)
: (undocumented)
- On success, responds with
EncryptOutput
with field(s): - On failure, responds with
SdkError<EncryptError>
Examples found in repository?
examples/keyring/ecdh/kms_ecdh_discovery_keyring_example.rs (line 202)
157async fn get_ciphertext(
158 example_data: &str,
159 encryption_context: HashMap<String, String>,
160 ecc_recipient_key_arn: &str,
161 ecdh_curve_spec: EcdhCurveSpec,
162 kms_client: aws_sdk_kms::Client,
163 esdk_client: esdk_client::Client,
164) -> Result<Blob, crate::BoxError> {
165 // 1. Create the public keys for sender and recipient
166 // Recipient keys are taken as input for this example
167 // Sender ECC key used in this example is TEST_KMS_ECDH_KEY_ID_P256_SENDER
168 let public_key_sender_utf8_bytes =
169 generate_kms_ecc_public_key(TEST_KMS_ECDH_KEY_ID_P256_SENDER).await?;
170 let public_key_recipient_utf8_bytes =
171 generate_kms_ecc_public_key(ecc_recipient_key_arn).await?;
172
173 // 2. Create the KmsPrivateKeyToStaticPublicKeyInput
174 let kms_ecdh_static_configuration_input = KmsPrivateKeyToStaticPublicKeyInput::builder()
175 .sender_kms_identifier(TEST_KMS_ECDH_KEY_ID_P256_SENDER)
176 // Must be a UTF8 DER-encoded X.509 public key
177 .sender_public_key(public_key_sender_utf8_bytes)
178 // Must be a UTF8 DER-encoded X.509 public key
179 .recipient_public_key(public_key_recipient_utf8_bytes)
180 .build()?;
181
182 let kms_ecdh_static_configuration = KmsEcdhStaticConfigurations::KmsPrivateKeyToStaticPublicKey(
183 kms_ecdh_static_configuration_input,
184 );
185
186 // 3. Create the KMS ECDH keyring.
187 let mpl_config = MaterialProvidersConfig::builder().build()?;
188 let mpl = mpl_client::Client::from_conf(mpl_config)?;
189
190 let kms_ecdh_keyring = mpl
191 .create_aws_kms_ecdh_keyring()
192 .kms_client(kms_client)
193 .curve_spec(ecdh_curve_spec)
194 .key_agreement_scheme(kms_ecdh_static_configuration)
195 .send()
196 .await?;
197
198 // 4. Encrypt the data with the encryption_context
199 let plaintext = example_data.as_bytes();
200
201 let encryption_response = esdk_client
202 .encrypt()
203 .plaintext(plaintext)
204 .keyring(kms_ecdh_keyring.clone())
205 .encryption_context(encryption_context.clone())
206 .send()
207 .await?;
208
209 let ciphertext = encryption_response
210 .ciphertext
211 .expect("Unable to unwrap ciphertext from encryption response");
212
213 // 5. Demonstrate that the ciphertext and plaintext are different.
214 // (This is an example for demonstration; you do not need to do this in your own code.)
215 assert_ne!(
216 ciphertext,
217 aws_smithy_types::Blob::new(plaintext),
218 "Ciphertext and plaintext data are the same. Invalid encryption"
219 );
220
221 Ok(ciphertext)
222}
More examples
examples/keyring/ecdh/public_key_discovery_raw_ecdh_keyring_example.rs (line 250)
202async fn get_ciphertext(
203 example_data: &str,
204 ecdh_curve_spec: EcdhCurveSpec,
205 encryption_context: HashMap<String, String>,
206 esdk_client: esdk_client::Client,
207 mpl: mpl_client::Client,
208) -> Result<Blob, crate::BoxError> {
209 // 1. Load keys from UTF-8 encoded PEM files.
210
211 // Load public key from UTF-8 encoded PEM files into a DER encoded public key.
212 let public_key_file_content =
213 std::fs::read_to_string(Path::new(EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT))?;
214 let parsed_public_key_file_content = parse(public_key_file_content)?;
215 let public_key_recipient_utf8_bytes = parsed_public_key_file_content.contents();
216
217 // 2. Create the EphemeralPrivateKeyToStaticPublicKeyInput to generate the ciphertext
218 let ephemeral_raw_ecdh_static_configuration_input =
219 EphemeralPrivateKeyToStaticPublicKeyInput::builder()
220 // Must be a UTF8 DER-encoded X.509 public key
221 .recipient_public_key(public_key_recipient_utf8_bytes)
222 .build()?;
223
224 let ephemeral_raw_ecdh_static_configuration =
225 RawEcdhStaticConfigurations::EphemeralPrivateKeyToStaticPublicKey(
226 ephemeral_raw_ecdh_static_configuration_input,
227 );
228
229 // 3. Create the Ephemeral Raw ECDH keyring.
230
231 // Create the keyring.
232 // This keyring uses an ephemeral configuration. This configuration will always create a new
233 // key pair as the sender key pair for the key agreement operation. The ephemeral configuration can only
234 // encrypt data and CANNOT decrypt messages.
235 let ephemeral_raw_ecdh_keyring = mpl
236 .create_raw_ecdh_keyring()
237 .curve_spec(ecdh_curve_spec)
238 .key_agreement_scheme(ephemeral_raw_ecdh_static_configuration)
239 .send()
240 .await?;
241
242 // 4. Encrypt the data with the encryption_context
243
244 // A raw ecdh keyring with Ephemeral configuration cannot decrypt data since the key pair
245 // used as the sender is ephemeral. This means that at decrypt time it does not have
246 // the private key that corresponds to the public key that is stored on the message.
247 let plaintext = example_data.as_bytes();
248
249 let encryption_response = esdk_client
250 .encrypt()
251 .plaintext(plaintext)
252 .keyring(ephemeral_raw_ecdh_keyring)
253 .encryption_context(encryption_context)
254 .send()
255 .await?;
256
257 let ciphertext = encryption_response
258 .ciphertext
259 .expect("Unable to unwrap ciphertext from encryption response");
260
261 // 5. Demonstrate that the ciphertext and plaintext are different.
262 // (This is an example for demonstration; you do not need to do this in your own code.)
263 assert_ne!(
264 ciphertext,
265 aws_smithy_types::Blob::new(plaintext),
266 "Ciphertext and plaintext data are the same. Invalid encryption"
267 );
268
269 Ok(ciphertext)
270}
examples/keyring/aws_kms_keyring_example.rs (line 79)
30pub async fn encrypt_and_decrypt_with_keyring(
31 example_data: &str,
32 kms_key_id: &str,
33) -> Result<(), crate::BoxError> {
34 // 1. Instantiate the encryption SDK client.
35 // This builds the default client with the RequireEncryptRequireDecrypt commitment policy,
36 // which enforces that this client only encrypts using committing algorithm suites and enforces
37 // that this client will only decrypt encrypted messages that were created with a committing
38 // algorithm suite.
39 let esdk_config = AwsEncryptionSdkConfig::builder().build()?;
40 let esdk_client = esdk_client::Client::from_conf(esdk_config)?;
41
42 // 2. Create a KMS client.
43 let sdk_config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
44 let kms_client = aws_sdk_kms::Client::new(&sdk_config);
45
46 // 3. Create encryption context.
47 // Remember that your encryption context is NOT SECRET.
48 // For more information, see
49 // https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
50 let encryption_context = HashMap::from([
51 ("encryption".to_string(), "context".to_string()),
52 ("is not".to_string(), "secret".to_string()),
53 ("but adds".to_string(), "useful metadata".to_string()),
54 (
55 "that can help you".to_string(),
56 "be confident that".to_string(),
57 ),
58 (
59 "the data you are handling".to_string(),
60 "is what you think it is".to_string(),
61 ),
62 ]);
63
64 // 4. Create a KMS keyring
65 let mpl_config = MaterialProvidersConfig::builder().build()?;
66 let mpl = mpl_client::Client::from_conf(mpl_config)?;
67
68 let kms_keyring = mpl
69 .create_aws_kms_keyring()
70 .kms_key_id(kms_key_id)
71 .kms_client(kms_client)
72 .send()
73 .await?;
74
75 // 5. Encrypt the data with the encryption_context
76 let plaintext = example_data.as_bytes();
77
78 let encryption_response = esdk_client
79 .encrypt()
80 .plaintext(plaintext)
81 .keyring(kms_keyring.clone())
82 .encryption_context(encryption_context.clone())
83 .send()
84 .await?;
85
86 let ciphertext = encryption_response
87 .ciphertext
88 .expect("Unable to unwrap ciphertext from encryption response");
89
90 // 6. Demonstrate that the ciphertext and plaintext are different.
91 // (This is an example for demonstration; you do not need to do this in your own code.)
92 assert_ne!(
93 ciphertext,
94 aws_smithy_types::Blob::new(plaintext),
95 "Ciphertext and plaintext data are the same. Invalid encryption"
96 );
97
98 // 7. Decrypt your encrypted data using the same keyring you used on encrypt.
99 let decryption_response = esdk_client
100 .decrypt()
101 .ciphertext(ciphertext)
102 .keyring(kms_keyring)
103 // Provide the encryption context that was supplied to the encrypt method
104 .encryption_context(encryption_context)
105 .send()
106 .await?;
107
108 let decrypted_plaintext = decryption_response
109 .plaintext
110 .expect("Unable to unwrap plaintext from decryption response");
111
112 // 8. Demonstrate that the decrypted plaintext is identical to the original plaintext.
113 // (This is an example for demonstration; you do not need to do this in your own code.)
114 assert_eq!(
115 decrypted_plaintext,
116 aws_smithy_types::Blob::new(plaintext),
117 "Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
118 );
119
120 println!("KMS Keyring Example Completed Successfully");
121
122 Ok(())
123}
examples/keyring/aws_kms_rsa_keyring_example.rs (line 82)
28pub async fn encrypt_and_decrypt_with_keyring(
29 example_data: &str,
30 kms_rsa_key_id: &str,
31 kms_rsa_public_key: &str,
32) -> Result<(), crate::BoxError> {
33 // 1. Instantiate the encryption SDK client.
34 // This builds the default client with the RequireEncryptRequireDecrypt commitment policy,
35 // which enforces that this client only encrypts using committing algorithm suites and enforces
36 // that this client will only decrypt encrypted messages that were created with a committing
37 // algorithm suite.
38 let esdk_config = AwsEncryptionSdkConfig::builder().build()?;
39 let esdk_client = esdk_client::Client::from_conf(esdk_config)?;
40
41 // 2. Create a KMS client.
42 let sdk_config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
43 let kms_client = aws_sdk_kms::Client::new(&sdk_config);
44
45 // 3. Create encryption context.
46 // Remember that your encryption context is NOT SECRET.
47 // For more information, see
48 // https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
49 let encryption_context = HashMap::from([
50 ("encryption".to_string(), "context".to_string()),
51 ("is not".to_string(), "secret".to_string()),
52 ("but adds".to_string(), "useful metadata".to_string()),
53 (
54 "that can help you".to_string(),
55 "be confident that".to_string(),
56 ),
57 (
58 "the data you are handling".to_string(),
59 "is what you think it is".to_string(),
60 ),
61 ]);
62
63 // 4. Create a KMS RSA keyring
64 let mpl_config = MaterialProvidersConfig::builder().build()?;
65 let mpl = mpl_client::Client::from_conf(mpl_config)?;
66
67 // For more information on the allowed encryption algorithms, please see
68 // https://docs.aws.amazon.com/kms/latest/developerguide/asymmetric-key-specs.html#key-spec-rsa
69 let kms_rsa_keyring = mpl
70 .create_aws_kms_rsa_keyring()
71 .kms_key_id(kms_rsa_key_id)
72 .public_key(aws_smithy_types::Blob::new(kms_rsa_public_key))
73 .encryption_algorithm(aws_sdk_kms::types::EncryptionAlgorithmSpec::RsaesOaepSha256)
74 .kms_client(kms_client)
75 .send()
76 .await?;
77
78 // 5. Encrypt the data with the encryption_context
79 let plaintext = example_data.as_bytes();
80
81 let encryption_response = esdk_client
82 .encrypt()
83 .plaintext(plaintext)
84 .keyring(kms_rsa_keyring.clone())
85 .encryption_context(encryption_context.clone())
86 .algorithm_suite_id(EsdkAlgorithmSuiteId::AlgAes256GcmHkdfSha512CommitKey)
87 .send()
88 .await?;
89
90 let ciphertext = encryption_response
91 .ciphertext
92 .expect("Unable to unwrap ciphertext from encryption response");
93
94 // 6. Demonstrate that the ciphertext and plaintext are different.
95 // (This is an example for demonstration; you do not need to do this in your own code.)
96 assert_ne!(
97 ciphertext,
98 aws_smithy_types::Blob::new(plaintext),
99 "Ciphertext and plaintext data are the same. Invalid encryption"
100 );
101
102 // 7. Decrypt your encrypted data using the same keyring you used on encrypt.
103 let decryption_response = esdk_client
104 .decrypt()
105 .ciphertext(ciphertext)
106 .keyring(kms_rsa_keyring)
107 // Provide the encryption context that was supplied to the encrypt method
108 .encryption_context(encryption_context)
109 .send()
110 .await?;
111
112 let decrypted_plaintext = decryption_response
113 .plaintext
114 .expect("Unable to unwrap plaintext from decryption response");
115
116 // 8. Demonstrate that the decrypted plaintext is identical to the original plaintext.
117 // (This is an example for demonstration; you do not need to do this in your own code.)
118 assert_eq!(
119 decrypted_plaintext,
120 aws_smithy_types::Blob::new(plaintext),
121 "Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
122 );
123
124 println!("KMS RSA Keyring Example Completed Successfully");
125
126 Ok(())
127}
examples/keyring/raw_aes_keyring_example.rs (line 90)
34pub async fn encrypt_and_decrypt_with_keyring(example_data: &str) -> Result<(), crate::BoxError> {
35 // 1. Instantiate the encryption SDK client.
36 // This builds the default client with the RequireEncryptRequireDecrypt commitment policy,
37 // which enforces that this client only encrypts using committing algorithm suites and enforces
38 // that this client will only decrypt encrypted messages that were created with a committing
39 // algorithm suite.
40 let esdk_config = AwsEncryptionSdkConfig::builder().build()?;
41 let esdk_client = esdk_client::Client::from_conf(esdk_config)?;
42
43 // 2. The key namespace and key name are defined by you.
44 // and are used by the Raw AES keyring to determine
45 // whether it should attempt to decrypt an encrypted data key.
46 // For more information, see
47 // https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-raw-aes-keyring.html
48 let key_namespace: &str = "my-key-namespace";
49 let key_name: &str = "my-aes-key-name";
50
51 // 3. Create encryption context.
52 // Remember that your encryption context is NOT SECRET.
53 // For more information, see
54 // https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
55 let encryption_context = HashMap::from([
56 ("encryption".to_string(), "context".to_string()),
57 ("is not".to_string(), "secret".to_string()),
58 ("but adds".to_string(), "useful metadata".to_string()),
59 (
60 "that can help you".to_string(),
61 "be confident that".to_string(),
62 ),
63 (
64 "the data you are handling".to_string(),
65 "is what you think it is".to_string(),
66 ),
67 ]);
68
69 // 4. Generate a 256-bit AES key to use with your keyring.
70 // In practice, you should get this key from a secure key management system such as an HSM.
71 let aes_key_bytes = generate_aes_key_bytes();
72
73 // 5. Create a Raw AES Keyring
74 let mpl_config = MaterialProvidersConfig::builder().build()?;
75 let mpl = mpl_client::Client::from_conf(mpl_config)?;
76
77 let raw_aes_keyring = mpl
78 .create_raw_aes_keyring()
79 .key_name(key_name)
80 .key_namespace(key_namespace)
81 .wrapping_key(aes_key_bytes)
82 .wrapping_alg(AesWrappingAlg::AlgAes256GcmIv12Tag16)
83 .send()
84 .await?;
85
86 // 6. Encrypt the data with the encryption_context
87 let plaintext = example_data.as_bytes();
88
89 let encryption_response = esdk_client
90 .encrypt()
91 .plaintext(plaintext)
92 .keyring(raw_aes_keyring.clone())
93 .encryption_context(encryption_context.clone())
94 .send()
95 .await?;
96
97 let ciphertext = encryption_response
98 .ciphertext
99 .expect("Unable to unwrap ciphertext from encryption response");
100
101 // 7. Demonstrate that the ciphertext and plaintext are different.
102 // (This is an example for demonstration; you do not need to do this in your own code.)
103 assert_ne!(
104 ciphertext,
105 aws_smithy_types::Blob::new(plaintext),
106 "Ciphertext and plaintext data are the same. Invalid encryption"
107 );
108
109 // 8. Decrypt your encrypted data using the same keyring you used on encrypt.
110 let decryption_response = esdk_client
111 .decrypt()
112 .ciphertext(ciphertext)
113 .keyring(raw_aes_keyring)
114 // Provide the encryption context that was supplied to the encrypt method
115 .encryption_context(encryption_context)
116 .send()
117 .await?;
118
119 let decrypted_plaintext = decryption_response
120 .plaintext
121 .expect("Unable to unwrap plaintext from decryption response");
122
123 // 9. Demonstrate that the decrypted plaintext is identical to the original plaintext.
124 // (This is an example for demonstration; you do not need to do this in your own code.)
125 assert_eq!(
126 decrypted_plaintext,
127 aws_smithy_types::Blob::new(plaintext),
128 "Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
129 );
130
131 println!("Raw AES Keyring Example Completed Successfully");
132
133 Ok(())
134}
examples/set_encryption_algorithm_suite_example.rs (line 110)
51pub async fn encrypt_and_decrypt_with_keyring(example_data: &str) -> Result<(), crate::BoxError> {
52 // 1. Instantiate the encryption SDK client.
53 // This builds the default client with the RequireEncryptRequireDecrypt commitment policy,
54 // which enforces that this client only encrypts using committing algorithm suites and enforces
55 // that this client will only decrypt encrypted messages that were created with a committing
56 // algorithm suite.
57 let esdk_config = AwsEncryptionSdkConfig::builder().build()?;
58 let esdk_client = esdk_client::Client::from_conf(esdk_config)?;
59
60 // 2. The key namespace and key name are defined by you.
61 // and are used by the Raw AES keyring to determine
62 // whether it should attempt to decrypt an encrypted data key.
63 // For more information, see
64 // https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-raw-aes-keyring.html
65 let key_namespace: &str = "my-key-namespace";
66 let key_name: &str = "my-aes-key-name";
67
68 // 3. Create encryption context.
69 // Remember that your encryption context is NOT SECRET.
70 // For more information, see
71 // https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
72 let encryption_context = HashMap::from([
73 ("encryption".to_string(), "context".to_string()),
74 ("is not".to_string(), "secret".to_string()),
75 ("but adds".to_string(), "useful metadata".to_string()),
76 (
77 "that can help you".to_string(),
78 "be confident that".to_string(),
79 ),
80 (
81 "the data you are handling".to_string(),
82 "is what you think it is".to_string(),
83 ),
84 ]);
85
86 // 4. Generate a 256-bit AES key to use with your keyring.
87 // In practice, you should get this key from a secure key management system such as an HSM.
88 let aes_key_bytes = generate_aes_key_bytes();
89
90 // 5. Create a Raw AES Keyring
91 let mpl_config = MaterialProvidersConfig::builder().build()?;
92 let mpl = mpl_client::Client::from_conf(mpl_config)?;
93
94 // The wrapping algorithm here is NOT the algorithm suite we set in this example.
95 let raw_aes_keyring = mpl
96 .create_raw_aes_keyring()
97 .key_name(key_name)
98 .key_namespace(key_namespace)
99 .wrapping_key(aes_key_bytes)
100 .wrapping_alg(AesWrappingAlg::AlgAes256GcmIv12Tag16)
101 .send()
102 .await?;
103
104 // 6. Encrypt the data with the encryption_context
105 let plaintext = example_data.as_bytes();
106
107 // This is the important step in this example where we specify the algorithm suite
108 // you want to use for encrypting your data
109 let encryption_response = esdk_client
110 .encrypt()
111 .plaintext(plaintext)
112 .keyring(raw_aes_keyring.clone())
113 .encryption_context(encryption_context.clone())
114 .algorithm_suite_id(AlgAes256GcmHkdfSha512CommitKey)
115 .send()
116 .await?;
117
118 let ciphertext = encryption_response
119 .ciphertext
120 .expect("Unable to unwrap ciphertext from encryption response");
121
122 // 7. Demonstrate that the ciphertext and plaintext are different.
123 // (This is an example for demonstration; you do not need to do this in your own code.)
124 assert_ne!(
125 ciphertext,
126 aws_smithy_types::Blob::new(plaintext),
127 "Ciphertext and plaintext data are the same. Invalid encryption"
128 );
129
130 // 8. Decrypt your encrypted data using the same keyring you used on encrypt.
131 let decryption_response = esdk_client
132 .decrypt()
133 .ciphertext(ciphertext)
134 .keyring(raw_aes_keyring)
135 // Provide the encryption context that was supplied to the encrypt method
136 .encryption_context(encryption_context)
137 .send()
138 .await?;
139
140 let decrypted_plaintext = decryption_response
141 .plaintext
142 .expect("Unable to unwrap plaintext from decryption response");
143
144 // 9. Demonstrate that the decrypted plaintext is identical to the original plaintext.
145 // (This is an example for demonstration; you do not need to do this in your own code.)
146 assert_eq!(
147 decrypted_plaintext,
148 aws_smithy_types::Blob::new(plaintext),
149 "Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
150 );
151
152 println!("Set Encryption Algorithm Suite Example Completed Successfully");
153
154 Ok(())
155}
Additional examples can be found in:
- examples/set_commitment_policy_example.rs
- examples/keyring/ecdh/ephemeral_raw_ecdh_keyring_example.rs
- examples/keyring/aws_kms_mrk_keyring_example.rs
- examples/keyring/raw_rsa_keyring_example.rs
- examples/cryptographic_materials_manager/restrict_algorithm_suite/signing_only_example.rs
- examples/keyring/aws_kms_mrk_discovery_keyring_example.rs
- examples/keyring/aws_kms_discovery_multi_keyring_example.rs
- examples/keyring/ecdh/raw_ecdh_keyring_example.rs
- examples/keyring/aws_kms_mrk_discovery_multi_keyring_example.rs
- examples/keyring/aws_kms_mrk_multi_keyring_example.rs
- examples/limit_encrypted_data_keys_example.rs
- examples/keyring/aws_kms_discovery_keyring_example.rs
- examples/client_supplier/client_supplier_example.rs
- examples/keyring/multi_keyring_example.rs
- examples/keyring/ecdh/kms_ecdh_keyring_example.rs
- examples/keyring/aws_kms_multi_keyring_example.rs
- examples/cryptographic_materials_manager/required_encryption_context/required_encryption_context_example.rs
- examples/keyring/aws_kms_hierarchical/aws_kms_hierarchical_keyring_example.rs
- examples/keyring/aws_kms_hierarchical/shared_cache_across_hierarchical_keyrings_example.rs
Source§impl Client
impl Client
Sourcepub fn decrypt(&self) -> DecryptFluentBuilder
pub fn decrypt(&self) -> DecryptFluentBuilder
Constructs a fluent builder for the Decrypt
operation.
- The fluent builder is configurable:
ciphertext(impl Into<Option<::aws_smithy_types::Blob>>)
/set_ciphertext(Option<::aws_smithy_types::Blob>)
: (undocumented)encryption_context(impl Into<Option<::std::collections::HashMap<::std::string::String, ::std::string::String>>>)
/set_encryption_context(Option<::std::collections::HashMap<::std::string::String, ::std::string::String>>)
: (undocumented)keyring(impl Into<Option<crate::deps::aws_cryptography_materialProviders::types::keyring::KeyringRef>>)
/set_keyring(Option<crate::deps::aws_cryptography_materialProviders::types::keyring::KeyringRef>)
: (undocumented)materials_manager(impl Into<Option<crate::deps::aws_cryptography_materialProviders::types::cryptographic_materials_manager::CryptographicMaterialsManagerRef>>)
/set_materials_manager(Option<crate::deps::aws_cryptography_materialProviders::types::cryptographic_materials_manager::CryptographicMaterialsManagerRef>)
: (undocumented)
- On success, responds with
DecryptOutput
with field(s): - On failure, responds with
SdkError<DecryptError>
Examples found in repository?
examples/keyring/aws_kms_keyring_example.rs (line 100)
30pub async fn encrypt_and_decrypt_with_keyring(
31 example_data: &str,
32 kms_key_id: &str,
33) -> Result<(), crate::BoxError> {
34 // 1. Instantiate the encryption SDK client.
35 // This builds the default client with the RequireEncryptRequireDecrypt commitment policy,
36 // which enforces that this client only encrypts using committing algorithm suites and enforces
37 // that this client will only decrypt encrypted messages that were created with a committing
38 // algorithm suite.
39 let esdk_config = AwsEncryptionSdkConfig::builder().build()?;
40 let esdk_client = esdk_client::Client::from_conf(esdk_config)?;
41
42 // 2. Create a KMS client.
43 let sdk_config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
44 let kms_client = aws_sdk_kms::Client::new(&sdk_config);
45
46 // 3. Create encryption context.
47 // Remember that your encryption context is NOT SECRET.
48 // For more information, see
49 // https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
50 let encryption_context = HashMap::from([
51 ("encryption".to_string(), "context".to_string()),
52 ("is not".to_string(), "secret".to_string()),
53 ("but adds".to_string(), "useful metadata".to_string()),
54 (
55 "that can help you".to_string(),
56 "be confident that".to_string(),
57 ),
58 (
59 "the data you are handling".to_string(),
60 "is what you think it is".to_string(),
61 ),
62 ]);
63
64 // 4. Create a KMS keyring
65 let mpl_config = MaterialProvidersConfig::builder().build()?;
66 let mpl = mpl_client::Client::from_conf(mpl_config)?;
67
68 let kms_keyring = mpl
69 .create_aws_kms_keyring()
70 .kms_key_id(kms_key_id)
71 .kms_client(kms_client)
72 .send()
73 .await?;
74
75 // 5. Encrypt the data with the encryption_context
76 let plaintext = example_data.as_bytes();
77
78 let encryption_response = esdk_client
79 .encrypt()
80 .plaintext(plaintext)
81 .keyring(kms_keyring.clone())
82 .encryption_context(encryption_context.clone())
83 .send()
84 .await?;
85
86 let ciphertext = encryption_response
87 .ciphertext
88 .expect("Unable to unwrap ciphertext from encryption response");
89
90 // 6. Demonstrate that the ciphertext and plaintext are different.
91 // (This is an example for demonstration; you do not need to do this in your own code.)
92 assert_ne!(
93 ciphertext,
94 aws_smithy_types::Blob::new(plaintext),
95 "Ciphertext and plaintext data are the same. Invalid encryption"
96 );
97
98 // 7. Decrypt your encrypted data using the same keyring you used on encrypt.
99 let decryption_response = esdk_client
100 .decrypt()
101 .ciphertext(ciphertext)
102 .keyring(kms_keyring)
103 // Provide the encryption context that was supplied to the encrypt method
104 .encryption_context(encryption_context)
105 .send()
106 .await?;
107
108 let decrypted_plaintext = decryption_response
109 .plaintext
110 .expect("Unable to unwrap plaintext from decryption response");
111
112 // 8. Demonstrate that the decrypted plaintext is identical to the original plaintext.
113 // (This is an example for demonstration; you do not need to do this in your own code.)
114 assert_eq!(
115 decrypted_plaintext,
116 aws_smithy_types::Blob::new(plaintext),
117 "Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
118 );
119
120 println!("KMS Keyring Example Completed Successfully");
121
122 Ok(())
123}
More examples
examples/keyring/aws_kms_rsa_keyring_example.rs (line 104)
28pub async fn encrypt_and_decrypt_with_keyring(
29 example_data: &str,
30 kms_rsa_key_id: &str,
31 kms_rsa_public_key: &str,
32) -> Result<(), crate::BoxError> {
33 // 1. Instantiate the encryption SDK client.
34 // This builds the default client with the RequireEncryptRequireDecrypt commitment policy,
35 // which enforces that this client only encrypts using committing algorithm suites and enforces
36 // that this client will only decrypt encrypted messages that were created with a committing
37 // algorithm suite.
38 let esdk_config = AwsEncryptionSdkConfig::builder().build()?;
39 let esdk_client = esdk_client::Client::from_conf(esdk_config)?;
40
41 // 2. Create a KMS client.
42 let sdk_config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
43 let kms_client = aws_sdk_kms::Client::new(&sdk_config);
44
45 // 3. Create encryption context.
46 // Remember that your encryption context is NOT SECRET.
47 // For more information, see
48 // https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
49 let encryption_context = HashMap::from([
50 ("encryption".to_string(), "context".to_string()),
51 ("is not".to_string(), "secret".to_string()),
52 ("but adds".to_string(), "useful metadata".to_string()),
53 (
54 "that can help you".to_string(),
55 "be confident that".to_string(),
56 ),
57 (
58 "the data you are handling".to_string(),
59 "is what you think it is".to_string(),
60 ),
61 ]);
62
63 // 4. Create a KMS RSA keyring
64 let mpl_config = MaterialProvidersConfig::builder().build()?;
65 let mpl = mpl_client::Client::from_conf(mpl_config)?;
66
67 // For more information on the allowed encryption algorithms, please see
68 // https://docs.aws.amazon.com/kms/latest/developerguide/asymmetric-key-specs.html#key-spec-rsa
69 let kms_rsa_keyring = mpl
70 .create_aws_kms_rsa_keyring()
71 .kms_key_id(kms_rsa_key_id)
72 .public_key(aws_smithy_types::Blob::new(kms_rsa_public_key))
73 .encryption_algorithm(aws_sdk_kms::types::EncryptionAlgorithmSpec::RsaesOaepSha256)
74 .kms_client(kms_client)
75 .send()
76 .await?;
77
78 // 5. Encrypt the data with the encryption_context
79 let plaintext = example_data.as_bytes();
80
81 let encryption_response = esdk_client
82 .encrypt()
83 .plaintext(plaintext)
84 .keyring(kms_rsa_keyring.clone())
85 .encryption_context(encryption_context.clone())
86 .algorithm_suite_id(EsdkAlgorithmSuiteId::AlgAes256GcmHkdfSha512CommitKey)
87 .send()
88 .await?;
89
90 let ciphertext = encryption_response
91 .ciphertext
92 .expect("Unable to unwrap ciphertext from encryption response");
93
94 // 6. Demonstrate that the ciphertext and plaintext are different.
95 // (This is an example for demonstration; you do not need to do this in your own code.)
96 assert_ne!(
97 ciphertext,
98 aws_smithy_types::Blob::new(plaintext),
99 "Ciphertext and plaintext data are the same. Invalid encryption"
100 );
101
102 // 7. Decrypt your encrypted data using the same keyring you used on encrypt.
103 let decryption_response = esdk_client
104 .decrypt()
105 .ciphertext(ciphertext)
106 .keyring(kms_rsa_keyring)
107 // Provide the encryption context that was supplied to the encrypt method
108 .encryption_context(encryption_context)
109 .send()
110 .await?;
111
112 let decrypted_plaintext = decryption_response
113 .plaintext
114 .expect("Unable to unwrap plaintext from decryption response");
115
116 // 8. Demonstrate that the decrypted plaintext is identical to the original plaintext.
117 // (This is an example for demonstration; you do not need to do this in your own code.)
118 assert_eq!(
119 decrypted_plaintext,
120 aws_smithy_types::Blob::new(plaintext),
121 "Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
122 );
123
124 println!("KMS RSA Keyring Example Completed Successfully");
125
126 Ok(())
127}
examples/keyring/raw_aes_keyring_example.rs (line 111)
34pub async fn encrypt_and_decrypt_with_keyring(example_data: &str) -> Result<(), crate::BoxError> {
35 // 1. Instantiate the encryption SDK client.
36 // This builds the default client with the RequireEncryptRequireDecrypt commitment policy,
37 // which enforces that this client only encrypts using committing algorithm suites and enforces
38 // that this client will only decrypt encrypted messages that were created with a committing
39 // algorithm suite.
40 let esdk_config = AwsEncryptionSdkConfig::builder().build()?;
41 let esdk_client = esdk_client::Client::from_conf(esdk_config)?;
42
43 // 2. The key namespace and key name are defined by you.
44 // and are used by the Raw AES keyring to determine
45 // whether it should attempt to decrypt an encrypted data key.
46 // For more information, see
47 // https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-raw-aes-keyring.html
48 let key_namespace: &str = "my-key-namespace";
49 let key_name: &str = "my-aes-key-name";
50
51 // 3. Create encryption context.
52 // Remember that your encryption context is NOT SECRET.
53 // For more information, see
54 // https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
55 let encryption_context = HashMap::from([
56 ("encryption".to_string(), "context".to_string()),
57 ("is not".to_string(), "secret".to_string()),
58 ("but adds".to_string(), "useful metadata".to_string()),
59 (
60 "that can help you".to_string(),
61 "be confident that".to_string(),
62 ),
63 (
64 "the data you are handling".to_string(),
65 "is what you think it is".to_string(),
66 ),
67 ]);
68
69 // 4. Generate a 256-bit AES key to use with your keyring.
70 // In practice, you should get this key from a secure key management system such as an HSM.
71 let aes_key_bytes = generate_aes_key_bytes();
72
73 // 5. Create a Raw AES Keyring
74 let mpl_config = MaterialProvidersConfig::builder().build()?;
75 let mpl = mpl_client::Client::from_conf(mpl_config)?;
76
77 let raw_aes_keyring = mpl
78 .create_raw_aes_keyring()
79 .key_name(key_name)
80 .key_namespace(key_namespace)
81 .wrapping_key(aes_key_bytes)
82 .wrapping_alg(AesWrappingAlg::AlgAes256GcmIv12Tag16)
83 .send()
84 .await?;
85
86 // 6. Encrypt the data with the encryption_context
87 let plaintext = example_data.as_bytes();
88
89 let encryption_response = esdk_client
90 .encrypt()
91 .plaintext(plaintext)
92 .keyring(raw_aes_keyring.clone())
93 .encryption_context(encryption_context.clone())
94 .send()
95 .await?;
96
97 let ciphertext = encryption_response
98 .ciphertext
99 .expect("Unable to unwrap ciphertext from encryption response");
100
101 // 7. Demonstrate that the ciphertext and plaintext are different.
102 // (This is an example for demonstration; you do not need to do this in your own code.)
103 assert_ne!(
104 ciphertext,
105 aws_smithy_types::Blob::new(plaintext),
106 "Ciphertext and plaintext data are the same. Invalid encryption"
107 );
108
109 // 8. Decrypt your encrypted data using the same keyring you used on encrypt.
110 let decryption_response = esdk_client
111 .decrypt()
112 .ciphertext(ciphertext)
113 .keyring(raw_aes_keyring)
114 // Provide the encryption context that was supplied to the encrypt method
115 .encryption_context(encryption_context)
116 .send()
117 .await?;
118
119 let decrypted_plaintext = decryption_response
120 .plaintext
121 .expect("Unable to unwrap plaintext from decryption response");
122
123 // 9. Demonstrate that the decrypted plaintext is identical to the original plaintext.
124 // (This is an example for demonstration; you do not need to do this in your own code.)
125 assert_eq!(
126 decrypted_plaintext,
127 aws_smithy_types::Blob::new(plaintext),
128 "Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
129 );
130
131 println!("Raw AES Keyring Example Completed Successfully");
132
133 Ok(())
134}
examples/set_encryption_algorithm_suite_example.rs (line 132)
51pub async fn encrypt_and_decrypt_with_keyring(example_data: &str) -> Result<(), crate::BoxError> {
52 // 1. Instantiate the encryption SDK client.
53 // This builds the default client with the RequireEncryptRequireDecrypt commitment policy,
54 // which enforces that this client only encrypts using committing algorithm suites and enforces
55 // that this client will only decrypt encrypted messages that were created with a committing
56 // algorithm suite.
57 let esdk_config = AwsEncryptionSdkConfig::builder().build()?;
58 let esdk_client = esdk_client::Client::from_conf(esdk_config)?;
59
60 // 2. The key namespace and key name are defined by you.
61 // and are used by the Raw AES keyring to determine
62 // whether it should attempt to decrypt an encrypted data key.
63 // For more information, see
64 // https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-raw-aes-keyring.html
65 let key_namespace: &str = "my-key-namespace";
66 let key_name: &str = "my-aes-key-name";
67
68 // 3. Create encryption context.
69 // Remember that your encryption context is NOT SECRET.
70 // For more information, see
71 // https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
72 let encryption_context = HashMap::from([
73 ("encryption".to_string(), "context".to_string()),
74 ("is not".to_string(), "secret".to_string()),
75 ("but adds".to_string(), "useful metadata".to_string()),
76 (
77 "that can help you".to_string(),
78 "be confident that".to_string(),
79 ),
80 (
81 "the data you are handling".to_string(),
82 "is what you think it is".to_string(),
83 ),
84 ]);
85
86 // 4. Generate a 256-bit AES key to use with your keyring.
87 // In practice, you should get this key from a secure key management system such as an HSM.
88 let aes_key_bytes = generate_aes_key_bytes();
89
90 // 5. Create a Raw AES Keyring
91 let mpl_config = MaterialProvidersConfig::builder().build()?;
92 let mpl = mpl_client::Client::from_conf(mpl_config)?;
93
94 // The wrapping algorithm here is NOT the algorithm suite we set in this example.
95 let raw_aes_keyring = mpl
96 .create_raw_aes_keyring()
97 .key_name(key_name)
98 .key_namespace(key_namespace)
99 .wrapping_key(aes_key_bytes)
100 .wrapping_alg(AesWrappingAlg::AlgAes256GcmIv12Tag16)
101 .send()
102 .await?;
103
104 // 6. Encrypt the data with the encryption_context
105 let plaintext = example_data.as_bytes();
106
107 // This is the important step in this example where we specify the algorithm suite
108 // you want to use for encrypting your data
109 let encryption_response = esdk_client
110 .encrypt()
111 .plaintext(plaintext)
112 .keyring(raw_aes_keyring.clone())
113 .encryption_context(encryption_context.clone())
114 .algorithm_suite_id(AlgAes256GcmHkdfSha512CommitKey)
115 .send()
116 .await?;
117
118 let ciphertext = encryption_response
119 .ciphertext
120 .expect("Unable to unwrap ciphertext from encryption response");
121
122 // 7. Demonstrate that the ciphertext and plaintext are different.
123 // (This is an example for demonstration; you do not need to do this in your own code.)
124 assert_ne!(
125 ciphertext,
126 aws_smithy_types::Blob::new(plaintext),
127 "Ciphertext and plaintext data are the same. Invalid encryption"
128 );
129
130 // 8. Decrypt your encrypted data using the same keyring you used on encrypt.
131 let decryption_response = esdk_client
132 .decrypt()
133 .ciphertext(ciphertext)
134 .keyring(raw_aes_keyring)
135 // Provide the encryption context that was supplied to the encrypt method
136 .encryption_context(encryption_context)
137 .send()
138 .await?;
139
140 let decrypted_plaintext = decryption_response
141 .plaintext
142 .expect("Unable to unwrap plaintext from decryption response");
143
144 // 9. Demonstrate that the decrypted plaintext is identical to the original plaintext.
145 // (This is an example for demonstration; you do not need to do this in your own code.)
146 assert_eq!(
147 decrypted_plaintext,
148 aws_smithy_types::Blob::new(plaintext),
149 "Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
150 );
151
152 println!("Set Encryption Algorithm Suite Example Completed Successfully");
153
154 Ok(())
155}
examples/set_commitment_policy_example.rs (line 119)
34pub async fn encrypt_and_decrypt_with_keyring(
35 example_data: &str,
36 kms_key_id: &str,
37) -> Result<(), crate::BoxError> {
38 // 1. Instantiate the encryption SDK client.
39 // This example builds the client with the ForbidEncryptAllowDecrypt commitment policy,
40 // which enforces that this client cannot encrypt with key commitment
41 // and it can decrypt ciphertexts encrypted with or without key commitment.
42 // The default commitment policy if you were to build the client like in
43 // the `keyring/aws_kms_keyring_example.rs` is RequireEncryptRequireDecrypt.
44 // We recommend that AWS Encryption SDK users use the default commitment policy
45 // (RequireEncryptRequireDecrypt) whenever possible.
46 let esdk_config = AwsEncryptionSdkConfig::builder()
47 .commitment_policy(ForbidEncryptAllowDecrypt)
48 .build()?;
49 let esdk_client = esdk_client::Client::from_conf(esdk_config)?;
50
51 // 2. Create a KMS client.
52 let sdk_config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
53 let kms_client = aws_sdk_kms::Client::new(&sdk_config);
54
55 // 3. Create encryption context.
56 // Remember that your encryption context is NOT SECRET.
57 // For more information, see
58 // https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
59 let encryption_context = HashMap::from([
60 ("encryption".to_string(), "context".to_string()),
61 ("is not".to_string(), "secret".to_string()),
62 ("but adds".to_string(), "useful metadata".to_string()),
63 (
64 "that can help you".to_string(),
65 "be confident that".to_string(),
66 ),
67 (
68 "the data you are handling".to_string(),
69 "is what you think it is".to_string(),
70 ),
71 ]);
72
73 // 4. Create a KMS keyring
74 let mpl_config = MaterialProvidersConfig::builder().build()?;
75 let mpl = mpl_client::Client::from_conf(mpl_config)?;
76
77 let kms_keyring = mpl
78 .create_aws_kms_keyring()
79 .kms_key_id(kms_key_id)
80 .kms_client(kms_client)
81 .send()
82 .await?;
83
84 // 5. Encrypt the data with the encryption_context. Make sure you use a non-committing algorithm
85 // with the commitment policy ForbidEncryptAllowDecrypt. Otherwise esdk_client.encrypt() will throw
86 // Error: AwsCryptographicMaterialProvidersError
87 // {
88 // error: InvalidAlgorithmSuiteInfoOnEncrypt
89 // {
90 // message: "Configuration conflict. Commitment policy requires only non-committing algorithm suites"
91 // }
92 // }
93 // By default for ForbidEncryptAllowDecrypt, the algorithm used is
94 // AlgAes256GcmIv12Tag16HkdfSha384EcdsaP384 which is a non-committing algorithm.
95 let plaintext = example_data.as_bytes();
96
97 let encryption_response = esdk_client
98 .encrypt()
99 .plaintext(plaintext)
100 .keyring(kms_keyring.clone())
101 .encryption_context(encryption_context.clone())
102 .send()
103 .await?;
104
105 let ciphertext = encryption_response
106 .ciphertext
107 .expect("Unable to unwrap ciphertext from encryption response");
108
109 // 6. Demonstrate that the ciphertext and plaintext are different.
110 // (This is an example for demonstration; you do not need to do this in your own code.)
111 assert_ne!(
112 ciphertext,
113 aws_smithy_types::Blob::new(plaintext),
114 "Ciphertext and plaintext data are the same. Invalid encryption"
115 );
116
117 // 7. Decrypt your encrypted data using the same keyring you used on encrypt.
118 let decryption_response = esdk_client
119 .decrypt()
120 .ciphertext(ciphertext)
121 .keyring(kms_keyring)
122 // Provide the encryption context that was supplied to the encrypt method
123 .encryption_context(encryption_context)
124 .send()
125 .await?;
126
127 let decrypted_plaintext = decryption_response
128 .plaintext
129 .expect("Unable to unwrap plaintext from decryption response");
130
131 // 8. Demonstrate that the decrypted plaintext is identical to the original plaintext.
132 // (This is an example for demonstration; you do not need to do this in your own code.)
133 assert_eq!(
134 decrypted_plaintext,
135 aws_smithy_types::Blob::new(plaintext),
136 "Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
137 );
138
139 println!("Set Commitment Policy Example Completed Successfully");
140
141 Ok(())
142}
examples/keyring/ecdh/public_key_discovery_raw_ecdh_keyring_example.rs (line 152)
66pub async fn decrypt_with_keyring(
67 example_data: &str,
68 ecdh_curve_spec: EcdhCurveSpec,
69) -> Result<(), crate::BoxError> {
70 // 1. Instantiate the encryption SDK client.
71 // This builds the default client with the RequireEncryptRequireDecrypt commitment policy,
72 // which enforces that this client only encrypts using committing algorithm suites and enforces
73 // that this client will only decrypt encrypted messages that were created with a committing
74 // algorithm suite.
75 let esdk_config = AwsEncryptionSdkConfig::builder().build()?;
76 let esdk_client = esdk_client::Client::from_conf(esdk_config)?;
77
78 let mpl_config = MaterialProvidersConfig::builder().build()?;
79 let mpl = mpl_client::Client::from_conf(mpl_config)?;
80
81 // 2. Create encryption context.
82 // Remember that your encryption context is NOT SECRET.
83 // For more information, see
84 // https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
85 let encryption_context = HashMap::from([
86 ("encryption".to_string(), "context".to_string()),
87 ("is not".to_string(), "secret".to_string()),
88 ("but adds".to_string(), "useful metadata".to_string()),
89 (
90 "that can help you".to_string(),
91 "be confident that".to_string(),
92 ),
93 (
94 "the data you are handling".to_string(),
95 "is what you think it is".to_string(),
96 ),
97 ]);
98
99 // 3. You may provide your own ECC keys in the files located at
100 // - EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT
101
102 // If you do not provide these files, running this example through this
103 // class' main method will generate three files required for all raw ECDH examples
104 // EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER, EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT
105 // and EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT for you.
106
107 // Do not use these files for any other purpose.
108 if should_generate_new_ecc_key_pair_discovery_raw_ecdh()? {
109 write_raw_ecdh_ecc_keys(ecdh_curve_spec)?;
110 }
111
112 // 4. Load keys from UTF-8 encoded PEM files.
113 let mut file = File::open(Path::new(EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT))?;
114 let mut private_key_recipient_utf8_bytes = Vec::new();
115 file.read_to_end(&mut private_key_recipient_utf8_bytes)?;
116
117 // Generate the ciphertext
118 let ciphertext = get_ciphertext(
119 example_data,
120 ecdh_curve_spec,
121 encryption_context.clone(),
122 esdk_client.clone(),
123 mpl.clone(),
124 )
125 .await?;
126
127 // 5. Create the PublicKeyDiscoveryInput
128 let discovery_raw_ecdh_static_configuration_input = PublicKeyDiscoveryInput::builder()
129 // Must be a UTF8 PEM-encoded private key
130 .recipient_static_private_key(private_key_recipient_utf8_bytes)
131 .build()?;
132
133 let discovery_raw_ecdh_static_configuration = RawEcdhStaticConfigurations::PublicKeyDiscovery(
134 discovery_raw_ecdh_static_configuration_input,
135 );
136
137 // 6. Create the Public Key Discovery Raw ECDH keyring.
138
139 // Create the keyring.
140 // This keyring uses a discovery configuration. This configuration will check on decrypt
141 // if it is meant to decrypt the message by checking if the configured public key is stored on the message.
142 // The discovery configuration can only decrypt messages and CANNOT encrypt messages.
143 let discovery_raw_ecdh_keyring = mpl
144 .create_raw_ecdh_keyring()
145 .curve_spec(ecdh_curve_spec)
146 .key_agreement_scheme(discovery_raw_ecdh_static_configuration)
147 .send()
148 .await?;
149
150 // 7. Decrypt your encrypted data using the same keyring you used on encrypt.
151 let decryption_response = esdk_client
152 .decrypt()
153 .ciphertext(ciphertext)
154 .keyring(discovery_raw_ecdh_keyring)
155 // Provide the encryption context that was supplied to the encrypt method
156 .encryption_context(encryption_context)
157 .send()
158 .await?;
159
160 let decrypted_plaintext = decryption_response
161 .plaintext
162 .expect("Unable to unwrap plaintext from decryption response");
163
164 // 8. Demonstrate that the decrypted plaintext is identical to the original plaintext.
165 // (This is an example for demonstration; you do not need to do this in your own code.)
166 let plaintext = example_data.as_bytes();
167
168 assert_eq!(
169 decrypted_plaintext,
170 aws_smithy_types::Blob::new(plaintext),
171 "Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
172 );
173
174 println!("Public Key Discovery Raw ECDH Keyring Example Completed Successfully");
175
176 Ok(())
177}
Additional examples can be found in:
- examples/keyring/aws_kms_mrk_keyring_example.rs
- examples/keyring/raw_rsa_keyring_example.rs
- examples/cryptographic_materials_manager/restrict_algorithm_suite/signing_only_example.rs
- examples/keyring/ecdh/kms_ecdh_discovery_keyring_example.rs
- examples/keyring/aws_kms_mrk_discovery_keyring_example.rs
- examples/keyring/aws_kms_discovery_multi_keyring_example.rs
- examples/keyring/ecdh/raw_ecdh_keyring_example.rs
- examples/keyring/aws_kms_mrk_discovery_multi_keyring_example.rs
- examples/keyring/aws_kms_mrk_multi_keyring_example.rs
- examples/limit_encrypted_data_keys_example.rs
- examples/keyring/aws_kms_discovery_keyring_example.rs
- examples/client_supplier/client_supplier_example.rs
- examples/keyring/multi_keyring_example.rs
- examples/keyring/ecdh/kms_ecdh_keyring_example.rs
- examples/keyring/aws_kms_multi_keyring_example.rs
- examples/cryptographic_materials_manager/required_encryption_context/required_encryption_context_example.rs
- examples/keyring/aws_kms_hierarchical/aws_kms_hierarchical_keyring_example.rs
- examples/keyring/aws_kms_hierarchical/shared_cache_across_hierarchical_keyrings_example.rs
Source§impl Client
impl Client
Sourcepub fn from_conf(input: AwsEncryptionSdkConfig) -> Result<Self, Error>
pub fn from_conf(input: AwsEncryptionSdkConfig) -> Result<Self, Error>
Creates a new client from the service Config
.
Examples found in repository?
examples/keyring/aws_kms_keyring_example.rs (line 40)
30pub async fn encrypt_and_decrypt_with_keyring(
31 example_data: &str,
32 kms_key_id: &str,
33) -> Result<(), crate::BoxError> {
34 // 1. Instantiate the encryption SDK client.
35 // This builds the default client with the RequireEncryptRequireDecrypt commitment policy,
36 // which enforces that this client only encrypts using committing algorithm suites and enforces
37 // that this client will only decrypt encrypted messages that were created with a committing
38 // algorithm suite.
39 let esdk_config = AwsEncryptionSdkConfig::builder().build()?;
40 let esdk_client = esdk_client::Client::from_conf(esdk_config)?;
41
42 // 2. Create a KMS client.
43 let sdk_config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
44 let kms_client = aws_sdk_kms::Client::new(&sdk_config);
45
46 // 3. Create encryption context.
47 // Remember that your encryption context is NOT SECRET.
48 // For more information, see
49 // https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
50 let encryption_context = HashMap::from([
51 ("encryption".to_string(), "context".to_string()),
52 ("is not".to_string(), "secret".to_string()),
53 ("but adds".to_string(), "useful metadata".to_string()),
54 (
55 "that can help you".to_string(),
56 "be confident that".to_string(),
57 ),
58 (
59 "the data you are handling".to_string(),
60 "is what you think it is".to_string(),
61 ),
62 ]);
63
64 // 4. Create a KMS keyring
65 let mpl_config = MaterialProvidersConfig::builder().build()?;
66 let mpl = mpl_client::Client::from_conf(mpl_config)?;
67
68 let kms_keyring = mpl
69 .create_aws_kms_keyring()
70 .kms_key_id(kms_key_id)
71 .kms_client(kms_client)
72 .send()
73 .await?;
74
75 // 5. Encrypt the data with the encryption_context
76 let plaintext = example_data.as_bytes();
77
78 let encryption_response = esdk_client
79 .encrypt()
80 .plaintext(plaintext)
81 .keyring(kms_keyring.clone())
82 .encryption_context(encryption_context.clone())
83 .send()
84 .await?;
85
86 let ciphertext = encryption_response
87 .ciphertext
88 .expect("Unable to unwrap ciphertext from encryption response");
89
90 // 6. Demonstrate that the ciphertext and plaintext are different.
91 // (This is an example for demonstration; you do not need to do this in your own code.)
92 assert_ne!(
93 ciphertext,
94 aws_smithy_types::Blob::new(plaintext),
95 "Ciphertext and plaintext data are the same. Invalid encryption"
96 );
97
98 // 7. Decrypt your encrypted data using the same keyring you used on encrypt.
99 let decryption_response = esdk_client
100 .decrypt()
101 .ciphertext(ciphertext)
102 .keyring(kms_keyring)
103 // Provide the encryption context that was supplied to the encrypt method
104 .encryption_context(encryption_context)
105 .send()
106 .await?;
107
108 let decrypted_plaintext = decryption_response
109 .plaintext
110 .expect("Unable to unwrap plaintext from decryption response");
111
112 // 8. Demonstrate that the decrypted plaintext is identical to the original plaintext.
113 // (This is an example for demonstration; you do not need to do this in your own code.)
114 assert_eq!(
115 decrypted_plaintext,
116 aws_smithy_types::Blob::new(plaintext),
117 "Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
118 );
119
120 println!("KMS Keyring Example Completed Successfully");
121
122 Ok(())
123}
More examples
examples/keyring/aws_kms_rsa_keyring_example.rs (line 39)
28pub async fn encrypt_and_decrypt_with_keyring(
29 example_data: &str,
30 kms_rsa_key_id: &str,
31 kms_rsa_public_key: &str,
32) -> Result<(), crate::BoxError> {
33 // 1. Instantiate the encryption SDK client.
34 // This builds the default client with the RequireEncryptRequireDecrypt commitment policy,
35 // which enforces that this client only encrypts using committing algorithm suites and enforces
36 // that this client will only decrypt encrypted messages that were created with a committing
37 // algorithm suite.
38 let esdk_config = AwsEncryptionSdkConfig::builder().build()?;
39 let esdk_client = esdk_client::Client::from_conf(esdk_config)?;
40
41 // 2. Create a KMS client.
42 let sdk_config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
43 let kms_client = aws_sdk_kms::Client::new(&sdk_config);
44
45 // 3. Create encryption context.
46 // Remember that your encryption context is NOT SECRET.
47 // For more information, see
48 // https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
49 let encryption_context = HashMap::from([
50 ("encryption".to_string(), "context".to_string()),
51 ("is not".to_string(), "secret".to_string()),
52 ("but adds".to_string(), "useful metadata".to_string()),
53 (
54 "that can help you".to_string(),
55 "be confident that".to_string(),
56 ),
57 (
58 "the data you are handling".to_string(),
59 "is what you think it is".to_string(),
60 ),
61 ]);
62
63 // 4. Create a KMS RSA keyring
64 let mpl_config = MaterialProvidersConfig::builder().build()?;
65 let mpl = mpl_client::Client::from_conf(mpl_config)?;
66
67 // For more information on the allowed encryption algorithms, please see
68 // https://docs.aws.amazon.com/kms/latest/developerguide/asymmetric-key-specs.html#key-spec-rsa
69 let kms_rsa_keyring = mpl
70 .create_aws_kms_rsa_keyring()
71 .kms_key_id(kms_rsa_key_id)
72 .public_key(aws_smithy_types::Blob::new(kms_rsa_public_key))
73 .encryption_algorithm(aws_sdk_kms::types::EncryptionAlgorithmSpec::RsaesOaepSha256)
74 .kms_client(kms_client)
75 .send()
76 .await?;
77
78 // 5. Encrypt the data with the encryption_context
79 let plaintext = example_data.as_bytes();
80
81 let encryption_response = esdk_client
82 .encrypt()
83 .plaintext(plaintext)
84 .keyring(kms_rsa_keyring.clone())
85 .encryption_context(encryption_context.clone())
86 .algorithm_suite_id(EsdkAlgorithmSuiteId::AlgAes256GcmHkdfSha512CommitKey)
87 .send()
88 .await?;
89
90 let ciphertext = encryption_response
91 .ciphertext
92 .expect("Unable to unwrap ciphertext from encryption response");
93
94 // 6. Demonstrate that the ciphertext and plaintext are different.
95 // (This is an example for demonstration; you do not need to do this in your own code.)
96 assert_ne!(
97 ciphertext,
98 aws_smithy_types::Blob::new(plaintext),
99 "Ciphertext and plaintext data are the same. Invalid encryption"
100 );
101
102 // 7. Decrypt your encrypted data using the same keyring you used on encrypt.
103 let decryption_response = esdk_client
104 .decrypt()
105 .ciphertext(ciphertext)
106 .keyring(kms_rsa_keyring)
107 // Provide the encryption context that was supplied to the encrypt method
108 .encryption_context(encryption_context)
109 .send()
110 .await?;
111
112 let decrypted_plaintext = decryption_response
113 .plaintext
114 .expect("Unable to unwrap plaintext from decryption response");
115
116 // 8. Demonstrate that the decrypted plaintext is identical to the original plaintext.
117 // (This is an example for demonstration; you do not need to do this in your own code.)
118 assert_eq!(
119 decrypted_plaintext,
120 aws_smithy_types::Blob::new(plaintext),
121 "Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
122 );
123
124 println!("KMS RSA Keyring Example Completed Successfully");
125
126 Ok(())
127}
examples/keyring/raw_aes_keyring_example.rs (line 41)
34pub async fn encrypt_and_decrypt_with_keyring(example_data: &str) -> Result<(), crate::BoxError> {
35 // 1. Instantiate the encryption SDK client.
36 // This builds the default client with the RequireEncryptRequireDecrypt commitment policy,
37 // which enforces that this client only encrypts using committing algorithm suites and enforces
38 // that this client will only decrypt encrypted messages that were created with a committing
39 // algorithm suite.
40 let esdk_config = AwsEncryptionSdkConfig::builder().build()?;
41 let esdk_client = esdk_client::Client::from_conf(esdk_config)?;
42
43 // 2. The key namespace and key name are defined by you.
44 // and are used by the Raw AES keyring to determine
45 // whether it should attempt to decrypt an encrypted data key.
46 // For more information, see
47 // https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-raw-aes-keyring.html
48 let key_namespace: &str = "my-key-namespace";
49 let key_name: &str = "my-aes-key-name";
50
51 // 3. Create encryption context.
52 // Remember that your encryption context is NOT SECRET.
53 // For more information, see
54 // https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
55 let encryption_context = HashMap::from([
56 ("encryption".to_string(), "context".to_string()),
57 ("is not".to_string(), "secret".to_string()),
58 ("but adds".to_string(), "useful metadata".to_string()),
59 (
60 "that can help you".to_string(),
61 "be confident that".to_string(),
62 ),
63 (
64 "the data you are handling".to_string(),
65 "is what you think it is".to_string(),
66 ),
67 ]);
68
69 // 4. Generate a 256-bit AES key to use with your keyring.
70 // In practice, you should get this key from a secure key management system such as an HSM.
71 let aes_key_bytes = generate_aes_key_bytes();
72
73 // 5. Create a Raw AES Keyring
74 let mpl_config = MaterialProvidersConfig::builder().build()?;
75 let mpl = mpl_client::Client::from_conf(mpl_config)?;
76
77 let raw_aes_keyring = mpl
78 .create_raw_aes_keyring()
79 .key_name(key_name)
80 .key_namespace(key_namespace)
81 .wrapping_key(aes_key_bytes)
82 .wrapping_alg(AesWrappingAlg::AlgAes256GcmIv12Tag16)
83 .send()
84 .await?;
85
86 // 6. Encrypt the data with the encryption_context
87 let plaintext = example_data.as_bytes();
88
89 let encryption_response = esdk_client
90 .encrypt()
91 .plaintext(plaintext)
92 .keyring(raw_aes_keyring.clone())
93 .encryption_context(encryption_context.clone())
94 .send()
95 .await?;
96
97 let ciphertext = encryption_response
98 .ciphertext
99 .expect("Unable to unwrap ciphertext from encryption response");
100
101 // 7. Demonstrate that the ciphertext and plaintext are different.
102 // (This is an example for demonstration; you do not need to do this in your own code.)
103 assert_ne!(
104 ciphertext,
105 aws_smithy_types::Blob::new(plaintext),
106 "Ciphertext and plaintext data are the same. Invalid encryption"
107 );
108
109 // 8. Decrypt your encrypted data using the same keyring you used on encrypt.
110 let decryption_response = esdk_client
111 .decrypt()
112 .ciphertext(ciphertext)
113 .keyring(raw_aes_keyring)
114 // Provide the encryption context that was supplied to the encrypt method
115 .encryption_context(encryption_context)
116 .send()
117 .await?;
118
119 let decrypted_plaintext = decryption_response
120 .plaintext
121 .expect("Unable to unwrap plaintext from decryption response");
122
123 // 9. Demonstrate that the decrypted plaintext is identical to the original plaintext.
124 // (This is an example for demonstration; you do not need to do this in your own code.)
125 assert_eq!(
126 decrypted_plaintext,
127 aws_smithy_types::Blob::new(plaintext),
128 "Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
129 );
130
131 println!("Raw AES Keyring Example Completed Successfully");
132
133 Ok(())
134}
examples/set_encryption_algorithm_suite_example.rs (line 58)
51pub async fn encrypt_and_decrypt_with_keyring(example_data: &str) -> Result<(), crate::BoxError> {
52 // 1. Instantiate the encryption SDK client.
53 // This builds the default client with the RequireEncryptRequireDecrypt commitment policy,
54 // which enforces that this client only encrypts using committing algorithm suites and enforces
55 // that this client will only decrypt encrypted messages that were created with a committing
56 // algorithm suite.
57 let esdk_config = AwsEncryptionSdkConfig::builder().build()?;
58 let esdk_client = esdk_client::Client::from_conf(esdk_config)?;
59
60 // 2. The key namespace and key name are defined by you.
61 // and are used by the Raw AES keyring to determine
62 // whether it should attempt to decrypt an encrypted data key.
63 // For more information, see
64 // https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-raw-aes-keyring.html
65 let key_namespace: &str = "my-key-namespace";
66 let key_name: &str = "my-aes-key-name";
67
68 // 3. Create encryption context.
69 // Remember that your encryption context is NOT SECRET.
70 // For more information, see
71 // https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
72 let encryption_context = HashMap::from([
73 ("encryption".to_string(), "context".to_string()),
74 ("is not".to_string(), "secret".to_string()),
75 ("but adds".to_string(), "useful metadata".to_string()),
76 (
77 "that can help you".to_string(),
78 "be confident that".to_string(),
79 ),
80 (
81 "the data you are handling".to_string(),
82 "is what you think it is".to_string(),
83 ),
84 ]);
85
86 // 4. Generate a 256-bit AES key to use with your keyring.
87 // In practice, you should get this key from a secure key management system such as an HSM.
88 let aes_key_bytes = generate_aes_key_bytes();
89
90 // 5. Create a Raw AES Keyring
91 let mpl_config = MaterialProvidersConfig::builder().build()?;
92 let mpl = mpl_client::Client::from_conf(mpl_config)?;
93
94 // The wrapping algorithm here is NOT the algorithm suite we set in this example.
95 let raw_aes_keyring = mpl
96 .create_raw_aes_keyring()
97 .key_name(key_name)
98 .key_namespace(key_namespace)
99 .wrapping_key(aes_key_bytes)
100 .wrapping_alg(AesWrappingAlg::AlgAes256GcmIv12Tag16)
101 .send()
102 .await?;
103
104 // 6. Encrypt the data with the encryption_context
105 let plaintext = example_data.as_bytes();
106
107 // This is the important step in this example where we specify the algorithm suite
108 // you want to use for encrypting your data
109 let encryption_response = esdk_client
110 .encrypt()
111 .plaintext(plaintext)
112 .keyring(raw_aes_keyring.clone())
113 .encryption_context(encryption_context.clone())
114 .algorithm_suite_id(AlgAes256GcmHkdfSha512CommitKey)
115 .send()
116 .await?;
117
118 let ciphertext = encryption_response
119 .ciphertext
120 .expect("Unable to unwrap ciphertext from encryption response");
121
122 // 7. Demonstrate that the ciphertext and plaintext are different.
123 // (This is an example for demonstration; you do not need to do this in your own code.)
124 assert_ne!(
125 ciphertext,
126 aws_smithy_types::Blob::new(plaintext),
127 "Ciphertext and plaintext data are the same. Invalid encryption"
128 );
129
130 // 8. Decrypt your encrypted data using the same keyring you used on encrypt.
131 let decryption_response = esdk_client
132 .decrypt()
133 .ciphertext(ciphertext)
134 .keyring(raw_aes_keyring)
135 // Provide the encryption context that was supplied to the encrypt method
136 .encryption_context(encryption_context)
137 .send()
138 .await?;
139
140 let decrypted_plaintext = decryption_response
141 .plaintext
142 .expect("Unable to unwrap plaintext from decryption response");
143
144 // 9. Demonstrate that the decrypted plaintext is identical to the original plaintext.
145 // (This is an example for demonstration; you do not need to do this in your own code.)
146 assert_eq!(
147 decrypted_plaintext,
148 aws_smithy_types::Blob::new(plaintext),
149 "Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
150 );
151
152 println!("Set Encryption Algorithm Suite Example Completed Successfully");
153
154 Ok(())
155}
examples/set_commitment_policy_example.rs (line 49)
34pub async fn encrypt_and_decrypt_with_keyring(
35 example_data: &str,
36 kms_key_id: &str,
37) -> Result<(), crate::BoxError> {
38 // 1. Instantiate the encryption SDK client.
39 // This example builds the client with the ForbidEncryptAllowDecrypt commitment policy,
40 // which enforces that this client cannot encrypt with key commitment
41 // and it can decrypt ciphertexts encrypted with or without key commitment.
42 // The default commitment policy if you were to build the client like in
43 // the `keyring/aws_kms_keyring_example.rs` is RequireEncryptRequireDecrypt.
44 // We recommend that AWS Encryption SDK users use the default commitment policy
45 // (RequireEncryptRequireDecrypt) whenever possible.
46 let esdk_config = AwsEncryptionSdkConfig::builder()
47 .commitment_policy(ForbidEncryptAllowDecrypt)
48 .build()?;
49 let esdk_client = esdk_client::Client::from_conf(esdk_config)?;
50
51 // 2. Create a KMS client.
52 let sdk_config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
53 let kms_client = aws_sdk_kms::Client::new(&sdk_config);
54
55 // 3. Create encryption context.
56 // Remember that your encryption context is NOT SECRET.
57 // For more information, see
58 // https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
59 let encryption_context = HashMap::from([
60 ("encryption".to_string(), "context".to_string()),
61 ("is not".to_string(), "secret".to_string()),
62 ("but adds".to_string(), "useful metadata".to_string()),
63 (
64 "that can help you".to_string(),
65 "be confident that".to_string(),
66 ),
67 (
68 "the data you are handling".to_string(),
69 "is what you think it is".to_string(),
70 ),
71 ]);
72
73 // 4. Create a KMS keyring
74 let mpl_config = MaterialProvidersConfig::builder().build()?;
75 let mpl = mpl_client::Client::from_conf(mpl_config)?;
76
77 let kms_keyring = mpl
78 .create_aws_kms_keyring()
79 .kms_key_id(kms_key_id)
80 .kms_client(kms_client)
81 .send()
82 .await?;
83
84 // 5. Encrypt the data with the encryption_context. Make sure you use a non-committing algorithm
85 // with the commitment policy ForbidEncryptAllowDecrypt. Otherwise esdk_client.encrypt() will throw
86 // Error: AwsCryptographicMaterialProvidersError
87 // {
88 // error: InvalidAlgorithmSuiteInfoOnEncrypt
89 // {
90 // message: "Configuration conflict. Commitment policy requires only non-committing algorithm suites"
91 // }
92 // }
93 // By default for ForbidEncryptAllowDecrypt, the algorithm used is
94 // AlgAes256GcmIv12Tag16HkdfSha384EcdsaP384 which is a non-committing algorithm.
95 let plaintext = example_data.as_bytes();
96
97 let encryption_response = esdk_client
98 .encrypt()
99 .plaintext(plaintext)
100 .keyring(kms_keyring.clone())
101 .encryption_context(encryption_context.clone())
102 .send()
103 .await?;
104
105 let ciphertext = encryption_response
106 .ciphertext
107 .expect("Unable to unwrap ciphertext from encryption response");
108
109 // 6. Demonstrate that the ciphertext and plaintext are different.
110 // (This is an example for demonstration; you do not need to do this in your own code.)
111 assert_ne!(
112 ciphertext,
113 aws_smithy_types::Blob::new(plaintext),
114 "Ciphertext and plaintext data are the same. Invalid encryption"
115 );
116
117 // 7. Decrypt your encrypted data using the same keyring you used on encrypt.
118 let decryption_response = esdk_client
119 .decrypt()
120 .ciphertext(ciphertext)
121 .keyring(kms_keyring)
122 // Provide the encryption context that was supplied to the encrypt method
123 .encryption_context(encryption_context)
124 .send()
125 .await?;
126
127 let decrypted_plaintext = decryption_response
128 .plaintext
129 .expect("Unable to unwrap plaintext from decryption response");
130
131 // 8. Demonstrate that the decrypted plaintext is identical to the original plaintext.
132 // (This is an example for demonstration; you do not need to do this in your own code.)
133 assert_eq!(
134 decrypted_plaintext,
135 aws_smithy_types::Blob::new(plaintext),
136 "Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
137 );
138
139 println!("Set Commitment Policy Example Completed Successfully");
140
141 Ok(())
142}
examples/keyring/ecdh/public_key_discovery_raw_ecdh_keyring_example.rs (line 76)
66pub async fn decrypt_with_keyring(
67 example_data: &str,
68 ecdh_curve_spec: EcdhCurveSpec,
69) -> Result<(), crate::BoxError> {
70 // 1. Instantiate the encryption SDK client.
71 // This builds the default client with the RequireEncryptRequireDecrypt commitment policy,
72 // which enforces that this client only encrypts using committing algorithm suites and enforces
73 // that this client will only decrypt encrypted messages that were created with a committing
74 // algorithm suite.
75 let esdk_config = AwsEncryptionSdkConfig::builder().build()?;
76 let esdk_client = esdk_client::Client::from_conf(esdk_config)?;
77
78 let mpl_config = MaterialProvidersConfig::builder().build()?;
79 let mpl = mpl_client::Client::from_conf(mpl_config)?;
80
81 // 2. Create encryption context.
82 // Remember that your encryption context is NOT SECRET.
83 // For more information, see
84 // https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
85 let encryption_context = HashMap::from([
86 ("encryption".to_string(), "context".to_string()),
87 ("is not".to_string(), "secret".to_string()),
88 ("but adds".to_string(), "useful metadata".to_string()),
89 (
90 "that can help you".to_string(),
91 "be confident that".to_string(),
92 ),
93 (
94 "the data you are handling".to_string(),
95 "is what you think it is".to_string(),
96 ),
97 ]);
98
99 // 3. You may provide your own ECC keys in the files located at
100 // - EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT
101
102 // If you do not provide these files, running this example through this
103 // class' main method will generate three files required for all raw ECDH examples
104 // EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER, EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT
105 // and EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT for you.
106
107 // Do not use these files for any other purpose.
108 if should_generate_new_ecc_key_pair_discovery_raw_ecdh()? {
109 write_raw_ecdh_ecc_keys(ecdh_curve_spec)?;
110 }
111
112 // 4. Load keys from UTF-8 encoded PEM files.
113 let mut file = File::open(Path::new(EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT))?;
114 let mut private_key_recipient_utf8_bytes = Vec::new();
115 file.read_to_end(&mut private_key_recipient_utf8_bytes)?;
116
117 // Generate the ciphertext
118 let ciphertext = get_ciphertext(
119 example_data,
120 ecdh_curve_spec,
121 encryption_context.clone(),
122 esdk_client.clone(),
123 mpl.clone(),
124 )
125 .await?;
126
127 // 5. Create the PublicKeyDiscoveryInput
128 let discovery_raw_ecdh_static_configuration_input = PublicKeyDiscoveryInput::builder()
129 // Must be a UTF8 PEM-encoded private key
130 .recipient_static_private_key(private_key_recipient_utf8_bytes)
131 .build()?;
132
133 let discovery_raw_ecdh_static_configuration = RawEcdhStaticConfigurations::PublicKeyDiscovery(
134 discovery_raw_ecdh_static_configuration_input,
135 );
136
137 // 6. Create the Public Key Discovery Raw ECDH keyring.
138
139 // Create the keyring.
140 // This keyring uses a discovery configuration. This configuration will check on decrypt
141 // if it is meant to decrypt the message by checking if the configured public key is stored on the message.
142 // The discovery configuration can only decrypt messages and CANNOT encrypt messages.
143 let discovery_raw_ecdh_keyring = mpl
144 .create_raw_ecdh_keyring()
145 .curve_spec(ecdh_curve_spec)
146 .key_agreement_scheme(discovery_raw_ecdh_static_configuration)
147 .send()
148 .await?;
149
150 // 7. Decrypt your encrypted data using the same keyring you used on encrypt.
151 let decryption_response = esdk_client
152 .decrypt()
153 .ciphertext(ciphertext)
154 .keyring(discovery_raw_ecdh_keyring)
155 // Provide the encryption context that was supplied to the encrypt method
156 .encryption_context(encryption_context)
157 .send()
158 .await?;
159
160 let decrypted_plaintext = decryption_response
161 .plaintext
162 .expect("Unable to unwrap plaintext from decryption response");
163
164 // 8. Demonstrate that the decrypted plaintext is identical to the original plaintext.
165 // (This is an example for demonstration; you do not need to do this in your own code.)
166 let plaintext = example_data.as_bytes();
167
168 assert_eq!(
169 decrypted_plaintext,
170 aws_smithy_types::Blob::new(plaintext),
171 "Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
172 );
173
174 println!("Public Key Discovery Raw ECDH Keyring Example Completed Successfully");
175
176 Ok(())
177}
Additional examples can be found in:
- examples/keyring/ecdh/ephemeral_raw_ecdh_keyring_example.rs
- examples/keyring/aws_kms_mrk_keyring_example.rs
- examples/keyring/raw_rsa_keyring_example.rs
- examples/cryptographic_materials_manager/restrict_algorithm_suite/signing_only_example.rs
- examples/keyring/ecdh/kms_ecdh_discovery_keyring_example.rs
- examples/keyring/aws_kms_mrk_discovery_keyring_example.rs
- examples/keyring/aws_kms_discovery_multi_keyring_example.rs
- examples/keyring/ecdh/raw_ecdh_keyring_example.rs
- examples/keyring/aws_kms_mrk_discovery_multi_keyring_example.rs
- examples/keyring/aws_kms_mrk_multi_keyring_example.rs
- examples/limit_encrypted_data_keys_example.rs
- examples/keyring/aws_kms_discovery_keyring_example.rs
- examples/client_supplier/client_supplier_example.rs
- examples/keyring/multi_keyring_example.rs
- examples/keyring/ecdh/kms_ecdh_keyring_example.rs
- examples/keyring/aws_kms_multi_keyring_example.rs
- examples/cryptographic_materials_manager/required_encryption_context/required_encryption_context_example.rs
- examples/keyring/aws_kms_hierarchical/aws_kms_hierarchical_keyring_example.rs
- examples/keyring/aws_kms_hierarchical/shared_cache_across_hierarchical_keyrings_example.rs
Trait Implementations§
impl StructuralPartialEq for Client
Auto Trait Implementations§
impl Freeze for Client
impl !RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl !UnwindSafe for Client
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreCreates a shared type from an unshared type.