-
Notifications
You must be signed in to change notification settings - Fork 122
Additional example code for Keyrings #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
63f362a
Additional example code for Keyrings
WesleyRosenblum 18165ff
Updating wording
WesleyRosenblum 0f613c8
Updating wording of some examples
WesleyRosenblum 24edd13
Updating wording
WesleyRosenblum d83b630
Remove AWS from AWS KMS keyring and make keyring lowercase
WesleyRosenblum ba3a35e
Updating wording to match readme
WesleyRosenblum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/examples/java/com/amazonaws/crypto/examples/RawAesKeyringExample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except | ||
* in compliance with the License. A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazonaws.crypto.examples; | ||
|
||
import com.amazonaws.encryptionsdk.AwsCrypto; | ||
import com.amazonaws.encryptionsdk.AwsCryptoResult; | ||
import com.amazonaws.encryptionsdk.DecryptRequest; | ||
import com.amazonaws.encryptionsdk.EncryptRequest; | ||
import com.amazonaws.encryptionsdk.keyrings.Keyring; | ||
import com.amazonaws.encryptionsdk.keyrings.StandardKeyrings; | ||
|
||
import javax.crypto.KeyGenerator; | ||
import javax.crypto.SecretKey; | ||
import javax.crypto.spec.SecretKeySpec; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.SecureRandom; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
/** | ||
* <p> | ||
* Encrypts and then decrypts data using the Raw AES keyring. | ||
*/ | ||
public class RawAesKeyringExample { | ||
|
||
private static final byte[] EXAMPLE_DATA = "Hello World".getBytes(StandardCharsets.UTF_8); | ||
|
||
public static void main(final String[] args) { | ||
encryptAndDecrypt(); | ||
} | ||
|
||
static void encryptAndDecrypt() { | ||
// 1. Instantiate the SDK | ||
final AwsCrypto crypto = new AwsCrypto(); | ||
WesleyRosenblum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 2. Get an encryption key. In this example, we generate a random key. | ||
// In practice, you would get a key from an existing key store | ||
final SecretKey cryptoKey = generateEncryptKey(); | ||
|
||
// 3. Instantiate a Raw AES keyring with the encryption key | ||
final Keyring keyring = StandardKeyrings.rawAesBuilder() | ||
.keyNamespace("ExampleKeyNamespace") | ||
.keyName("ExampleKeyName") | ||
.wrappingKey(cryptoKey).build(); | ||
|
||
// 4. Create an encryption context | ||
// | ||
// Most encrypted data should have an associated encryption context | ||
// to protect integrity. This sample uses placeholder values. | ||
// | ||
// For more information see: | ||
// blogs.aws.amazon.com/security/post/Tx2LZ6WBJJANTNW/How-to-Protect-the-Integrity-of-Your-Encrypted-Data-by-Using-AWS-Key-Management | ||
final Map<String, String> encryptionContext = Collections.singletonMap("ExampleContextKey", "ExampleContextValue"); | ||
|
||
// 5. Encrypt the data with the keyring and encryption context | ||
final AwsCryptoResult<byte[]> encryptResult = crypto.encrypt(EncryptRequest.builder() | ||
.keyring(keyring) | ||
.encryptionContext(encryptionContext) | ||
.plaintext(EXAMPLE_DATA).build()); | ||
final byte[] ciphertext = encryptResult.getResult(); | ||
|
||
// 6. Decrypt the data | ||
final AwsCryptoResult<byte[]> decryptResult = crypto.decrypt(DecryptRequest.builder() | ||
.keyring(keyring) | ||
.ciphertext(ciphertext).build()); | ||
|
||
// 7. Verify that the encryption context that was used to decrypt the data is the one that you expect. | ||
// This helps to ensure that the ciphertext that you decrypted was the one that you intended. | ||
// | ||
// When verifying, test that your expected encryption context is a subset of the actual encryption context, | ||
// not an exact match. When appropriate, the Encryption SDK adds the signing key to the encryption context. | ||
assert decryptResult.getEncryptionContext().get("ExampleContextKey").equals("ExampleContextValue"); | ||
|
||
// 8. Verify that the decrypted plaintext matches the original plaintext | ||
assert Arrays.equals(decryptResult.getResult(), EXAMPLE_DATA); | ||
} | ||
|
||
/** | ||
* In practice, this key would be saved in a secure location. | ||
* For this demo, we generate a new random key for each operation. | ||
WesleyRosenblum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
private static SecretKey generateEncryptKey() { | ||
SecureRandom rnd = new SecureRandom(); | ||
byte[] rawKey = new byte[16]; // 128 bits | ||
rnd.nextBytes(rawKey); | ||
return new SecretKeySpec(rawKey, "AES"); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/examples/java/com/amazonaws/crypto/examples/RawRsaKeyringDecryptExample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except | ||
* in compliance with the License. A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazonaws.crypto.examples; | ||
|
||
import com.amazonaws.encryptionsdk.AwsCrypto; | ||
import com.amazonaws.encryptionsdk.AwsCryptoResult; | ||
import com.amazonaws.encryptionsdk.DecryptRequest; | ||
import com.amazonaws.encryptionsdk.keyrings.Keyring; | ||
import com.amazonaws.encryptionsdk.keyrings.StandardKeyrings; | ||
|
||
import java.security.KeyPair; | ||
|
||
/** | ||
* <p> | ||
* Decrypts data using the Raw RSA keyring. | ||
*/ | ||
public class RawRsaKeyringDecryptExample { | ||
juneb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public static byte[] decrypt(byte[] ciphertext, KeyPair keyPair) { | ||
// 1. Instantiate the SDK | ||
final AwsCrypto crypto = new AwsCrypto(); | ||
|
||
// 2. Instantiate a Raw RSA keyring with the private key | ||
final Keyring keyring = StandardKeyrings.rawRsaBuilder() | ||
.keyNamespace("ExampleKeyNamespace") | ||
.keyName("ExampleKeyName") | ||
.wrappingAlgorithm("RSA/ECB/OAEPWithSHA-512AndMGF1Padding") | ||
.privateKey(keyPair.getPrivate()).build(); | ||
|
||
// 3. Decrypt the ciphertext with the keyring | ||
final AwsCryptoResult<byte[]> decryptResult = crypto.decrypt(DecryptRequest.builder() | ||
.keyring(keyring) | ||
.ciphertext(ciphertext).build()); | ||
|
||
juneb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 4. Verify that the encryption context that was used to decrypt the data is the one that you expect. | ||
// This helps to ensure that the ciphertext that you decrypted was the one that you intended. | ||
// | ||
// When verifying, test that your expected encryption context is a subset of the actual encryption context, | ||
// not an exact match. When appropriate, the Encryption SDK adds the signing key to the encryption context. | ||
assert decryptResult.getEncryptionContext().get("ExampleContextKey").equals("ExampleContextValue"); | ||
|
||
// 5. Return the decrypted byte array result | ||
return decryptResult.getResult(); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/examples/java/com/amazonaws/crypto/examples/RawRsaKeyringEncryptExample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except | ||
* in compliance with the License. A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazonaws.crypto.examples; | ||
|
||
import com.amazonaws.encryptionsdk.AwsCrypto; | ||
import com.amazonaws.encryptionsdk.AwsCryptoResult; | ||
import com.amazonaws.encryptionsdk.EncryptRequest; | ||
import com.amazonaws.encryptionsdk.keyrings.Keyring; | ||
import com.amazonaws.encryptionsdk.keyrings.StandardKeyrings; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.security.PublicKey; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
/** | ||
* Encrypts data using the Raw RSA keyring. | ||
*/ | ||
public class RawRsaKeyringEncryptExample { | ||
|
||
static final byte[] EXAMPLE_DATA = "Hello World".getBytes(StandardCharsets.UTF_8); | ||
|
||
public static byte[] encrypt(PublicKey publicKey) { | ||
// 1. Instantiate the SDK | ||
final AwsCrypto crypto = new AwsCrypto(); | ||
|
||
// 2. Instantiate a Raw RSA keyring with the public key | ||
final Keyring keyring = StandardKeyrings.rawRsaBuilder() | ||
.keyNamespace("ExampleKeyNamespace") | ||
.keyName("ExampleKeyName") | ||
.wrappingAlgorithm("RSA/ECB/OAEPWithSHA-512AndMGF1Padding") | ||
.publicKey(publicKey).build(); | ||
|
||
// 3. Create an encryption context | ||
// | ||
// Most encrypted data should have an associated encryption context | ||
// to protect integrity. This sample uses placeholder values. | ||
// | ||
// For more information see: | ||
// blogs.aws.amazon.com/security/post/Tx2LZ6WBJJANTNW/How-to-Protect-the-Integrity-of-Your-Encrypted-Data-by-Using-AWS-Key-Management | ||
final Map<String, String> encryptionContext = Collections.singletonMap("ExampleContextKey", "ExampleContextValue"); | ||
|
||
// 4. Encrypt the data with the keyring and encryption context | ||
final AwsCryptoResult<byte[]> encryptResult = crypto.encrypt(EncryptRequest.builder() | ||
.keyring(keyring) | ||
.encryptionContext(encryptionContext) | ||
.plaintext(EXAMPLE_DATA).build()); | ||
|
||
// 5. Return the encrypted byte array result | ||
return encryptResult.getResult(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Who is "we?"
To verify that the encryption context used to decrypt the data was the encryption context you expected, use the keyring trace. This helps to ensure that you decrypted the ciphertext that you intended.
When verifying, test that your expected encryption context is a subset of the actual encryption context, not an exact match. The Encryption SDK adds the signing key to the encryption context when appropriate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you intended to mention the keyring trace here, I went with:
To verify that the encryption context used to decrypt the data was the encryption context you expected, examine the encryption context in the result. This helps to ensure that you decrypted the ciphertext that you intended.