Skip to content

Commit 77d7094

Browse files
committed
Scrubbed code comments.
1 parent eae3056 commit 77d7094

File tree

3 files changed

+67
-56
lines changed

3 files changed

+67
-56
lines changed

src/examples/java/com/amazonaws/crypto/examples/EscrowedEncryptExample.java

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
* Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except
55
* in compliance with the License. A copy of the License is located at
@@ -36,24 +36,29 @@
3636
* <p>
3737
* Arguments:
3838
* <ol>
39-
* <li>KMS KeyArn
39+
* <li>Key ARN: To find the Amazon Resource Name of your KMS customer master key (CMK),
40+
* see 'Viewing Keys' at http://docs.aws.amazon.com/kms/latest/developerguide/viewing-keys.html
4041
* <li>File Name
4142
* </ol>
4243
*
43-
* Some organizations want the ability to decrypt their data even if KMS is unavailable. This
44-
* program demonstrates one possible way of accomplishing this by generating an "Escrow" RSA
45-
* key-pair and using that in addition to the KMS key for encryption. The organization would keep
46-
* the RSA private key someplace secure (such as an offline HSM) and distribute the public key their
47-
* developers. This way all standard use would use KMS for decryption, however the organization
48-
* maintains the ability to decrypt all ciphertexts in a completely offline manner.
44+
* AWS Key Management Service (KMS) is highly available. However, some organizations want to decrypt
45+
* their data offline and independent of KMS. This sample demonstrates one way to do this.
46+
*
47+
* This program generates an "escrowed" RSA key pair. It stores the private key in a secure offline
48+
* location, such as an offline HSM, and distributes the public key to their developers. It also
49+
* creates a KMS customer master key (CMK). The organization encrypts their data with both the
50+
* KMS CMK and the public key, so that either key alone could decrypt it.
51+
*
52+
* The team usually uses the KMS CMK for decryption. However, the organization can, at any time
53+
* use the private escrowed RSA key to decrypt the ciphertext independent of KMS.
4954
*/
5055
public class EscrowedEncryptExample {
5156
private static PublicKey publicEscrowKey;
5257
private static PrivateKey privateEscrowKey;
5358

5459
public static void main(final String[] args) throws Exception {
55-
// In the real world, the public key would be distributed by the organization.
56-
// For this demo, we'll just generate a new random one each time.
60+
// In practice, the organization would distribute the public key.
61+
// For this demo, we generate a new random key for each operation.
5762
generateEscrowKeyPair();
5863

5964
final String kmsArn = args[0];
@@ -66,23 +71,25 @@ public static void main(final String[] args) throws Exception {
6671
}
6772

6873
private static void standardEncrypt(final String kmsArn, final String fileName) throws Exception {
69-
// Standard user encrypting to both KMS and the escrow public key
74+
// Standard practice: encrypt with the KMS CMK and the escrowed public key
7075
// 1. Instantiate the SDK
7176
final AwsCrypto crypto = new AwsCrypto();
7277

73-
// 2. Instantiate the providers
78+
// 2. Instantiate a KMS master key provider
7479
final KmsMasterKeyProvider kms = new KmsMasterKeyProvider(kmsArn);
75-
// Note that the standard user does not have access to the private escrow
76-
// key and so simply passes in "null"
80+
81+
// 3. Instantiate a JCE master key provider
82+
// Because the standard user does not have access to the private
83+
// escrow key, they pass in "null" for the private key parameter.
7784
final JceMasterKey escrowPub = JceMasterKey.getInstance(publicEscrowKey, null, "Escrow", "Escrow",
7885
"RSA/ECB/OAEPWithSHA-512AndMGF1Padding");
7986

80-
// 3. Combine the providers into a single one
87+
// 4. Combine the providers into a single master key provider
8188
final MasterKeyProvider<?> provider = MultipleProviderFactory.buildMultiProvider(kms, escrowPub);
8289

83-
// 4. Encrypt the file
84-
// To simplify the code, we'll be omitted Encryption Context this time. Production code
85-
// should always use Encryption Context. Please see the other examples for more information.
90+
// 5. Encrypt the file
91+
// To simplify the code, we omit the encryption context. Production code should always
92+
// use an encryption context. For an example, see the other SDK samples.
8693
final FileInputStream in = new FileInputStream(fileName);
8794
final FileOutputStream out = new FileOutputStream(fileName + ".encrypted");
8895
final CryptoOutputStream<?> encryptingStream = crypto.createEncryptingStream(provider, out);
@@ -93,25 +100,26 @@ private static void standardEncrypt(final String kmsArn, final String fileName)
93100
}
94101

95102
private static void standardDecrypt(final String kmsArn, final String fileName) throws Exception {
96-
// A standard user decrypts the file. They can just use the same provider from before
97-
// or could use a provider just referring to the KMS key. It doesn't matter.
103+
// Standard practice: enncrypt with the KMS CMK and the escrow public key
98104

99105
// 1. Instantiate the SDK
100106
final AwsCrypto crypto = new AwsCrypto();
101107

102-
// 2. Instantiate the providers
108+
// 2. Instantiate a KMS master key provider
103109
final KmsMasterKeyProvider kms = new KmsMasterKeyProvider(kmsArn);
104-
// Note that the standard user does not have access to the private escrow
105-
// key and so simply passes in "null"
110+
111+
// 3. Instantiate a JCE master key provider
112+
// Because the standard user does not have access to the private
113+
// escrow key, they pass in "null" for the private key parameter.
106114
final JceMasterKey escrowPub = JceMasterKey.getInstance(publicEscrowKey, null, "Escrow", "Escrow",
107115
"RSA/ECB/OAEPWithSHA-512AndMGF1Padding");
108116

109-
// 3. Combine the providers into a single one
117+
// 4. Combine the providers into a single master key provider
110118
final MasterKeyProvider<?> provider = MultipleProviderFactory.buildMultiProvider(kms, escrowPub);
111119

112-
// 4. Decrypt the file
113-
// To simplify the code, we'll be omitted Encryption Context this time. Production code
114-
// should always use Encryption Context. Please see the other examples for more information.
120+
// 5. Decrypt the file
121+
// To simplify the code, we omit the encryption context. Production code should always
122+
// use an encryption context. For an example, see the other SDK samples.
115123
final FileInputStream in = new FileInputStream(fileName + ".encrypted");
116124
final FileOutputStream out = new FileOutputStream(fileName + ".decrypted");
117125
final CryptoOutputStream<?> decryptingStream = crypto.createDecryptingStream(provider, out);
@@ -121,19 +129,20 @@ private static void standardDecrypt(final String kmsArn, final String fileName)
121129
}
122130

123131
private static void escrowDecrypt(final String fileName) throws Exception {
124-
// The organization can decrypt using just the private escrow key with no calls to KMS
132+
// The organization can decrypt the stream using only the private escrow key.
133+
// This method does not call KMS.
125134

126135
// 1. Instantiate the SDK
127136
final AwsCrypto crypto = new AwsCrypto();
128137

129-
// 2. Instantiate the provider
130-
// Note that the organization does have access to the private escrow key and can use it.
138+
// 2. Instantiate a JCE master key provider
139+
// This method call uses the escrowed private key
131140
final JceMasterKey escrowPriv = JceMasterKey.getInstance(publicEscrowKey, privateEscrowKey, "Escrow", "Escrow",
132141
"RSA/ECB/OAEPWithSHA-512AndMGF1Padding");
133142

134143
// 3. Decrypt the file
135-
// To simplify the code, we'll be omitted Encryption Context this time. Production code
136-
// should always use Encryption Context. Please see the other examples for more information.
144+
// To simplify the code, we omit the encryption context. Production code should always
145+
// use an encryption context. For an example, see the other SDK samples.
137146
final FileInputStream in = new FileInputStream(fileName + ".encrypted");
138147
final FileOutputStream out = new FileOutputStream(fileName + ".deescrowed");
139148
final CryptoOutputStream<?> decryptingStream = crypto.createDecryptingStream(escrowPriv, out);

src/examples/java/com/amazonaws/crypto/examples/FileStreamingExample.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
* Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except
55
* in compliance with the License. A copy of the License is located at
@@ -40,7 +40,7 @@
4040
* </ol>
4141
*
4242
* <p>
43-
* This program demonstrates using a normal java {@link SecretKey} object as a {@link MasterKey} to
43+
* This program demonstrates using a standard Java {@link SecretKey} object as a {@link MasterKey} to
4444
* encrypt and decrypt streaming data.
4545
*/
4646
public class FileStreamingExample {
@@ -49,22 +49,21 @@ public class FileStreamingExample {
4949
public static void main(String[] args) throws IOException {
5050
srcFile = args[0];
5151

52-
// In this example, we'll pretend that we loaded this key from
53-
// some existing store but actually just generate a random one
52+
// In this example, we generate a random key. In practice,
53+
// you would get a key from an existing store
5454
SecretKey cryptoKey = retrieveEncryptionKey();
5555

56-
// Convert key into a provider. We'll use AES GCM because it is
57-
// a good secure algorithm.
56+
// Create a JCE master key provider using the random key and an AES-GCM encryption algorithm
5857
JceMasterKey masterKey = JceMasterKey.getInstance(cryptoKey, "Example", "RandomKey", "AES/GCM/NoPadding");
5958

60-
// Instantiate the SDKs
59+
// Instantiate the SDK
6160
AwsCrypto crypto = new AwsCrypto();
6261

63-
// Create the encryption context to identify this ciphertext
62+
// Create an encryption context to identify this ciphertext
6463
Map<String, String> context = Collections.singletonMap("Example", "FileStreaming");
6564

66-
// The file might be *really* big, so we don't want
67-
// to load it all into memory. Streaming is necessary.
65+
// Because the file might be to large to load into memory, we use
66+
// streaming, then encrypt the file stream.
6867
FileInputStream in = new FileInputStream(srcFile);
6968
CryptoInputStream<JceMasterKey> encryptingStream = crypto.createEncryptingStream(masterKey, in, context);
7069

@@ -73,24 +72,24 @@ public static void main(String[] args) throws IOException {
7372
encryptingStream.close();
7473
out.close();
7574

76-
// Let's decrypt the file now, remembering to check the encryption context
75+
// Decrypt the file. Verify the encryption context before returning the plaintext.
7776
in = new FileInputStream(srcFile + ".encrypted");
7877
CryptoInputStream<JceMasterKey> decryptingStream = crypto.createDecryptingStream(masterKey, in);
79-
// Does it have the right encryption context?
78+
// Does it contain the expected encryption context?
8079
if (!"FileStreaming".equals(decryptingStream.getCryptoResult().getEncryptionContext().get("Example"))) {
8180
throw new IllegalStateException("Bad encryption context");
8281
}
8382

84-
// Finally, actually write out the data
83+
// Return the plaintext data
8584
out = new FileOutputStream(srcFile + ".decrypted");
8685
IOUtils.copy(decryptingStream, out);
8786
decryptingStream.close();
8887
out.close();
8988
}
9089

9190
/**
92-
* In the real world, this key will need to be persisted somewhere. For this demo we'll generate
93-
* a new random one each time.
91+
* In practice, this key would be saved in a secure location.
92+
* For this demo we'll generate a new random key for each operation.
9493
*/
9594
private static SecretKey retrieveEncryptionKey() {
9695
SecureRandom rnd = new SecureRandom();

src/examples/java/com/amazonaws/crypto/examples/StringExample.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
* Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except
55
* in compliance with the License. A copy of the License is located at
@@ -28,8 +28,8 @@
2828
* <p>
2929
* Arguments:
3030
* <ol>
31-
* <li>KMS Key Arn
32-
* <li>String to encrypt
31+
* <li>Key ARN: To find the Amazon Resource Name of your KMS customer master key (CMK),
32+
* see 'Viewing Keys' at http://docs.aws.amazon.com/kms/latest/developerguide/viewing-keys.html
3333
* </ol>
3434
*/
3535
public class StringExample {
@@ -48,7 +48,7 @@ public static void main(final String[] args) {
4848

4949
// Encrypt the data
5050
//
51-
// Most encrypted data should have associated encryption context
51+
// Most encrypted data should have an associated encryption context
5252
// to protect integrity. Here, we'll just use a placeholder value.
5353
//
5454
// For more information see:
@@ -60,21 +60,24 @@ public static void main(final String[] args) {
6060

6161
// Decrypt the data
6262
final CryptoResult<String, KmsMasterKey> decryptResult = crypto.decryptString(prov, ciphertext);
63-
// We need to check the encryption context (and ideally key) to ensure that
64-
// this was the ciphertext we expected
63+
64+
// Before returning the plaintext, verify that the customer master key that
65+
// was used in the encryption operation was the one supplied to the master key provider.
6566
if (!decryptResult.getMasterKeyIds().get(0).equals(keyArn)) {
6667
throw new IllegalStateException("Wrong key id!");
6768
}
6869

69-
// The SDK may add information to the encryption context, so we check to ensure
70-
// that all of our values are present
70+
// Also, verify that the encryption context in the result contains the
71+
// encryption context supplied to the encryptString method. Because the
72+
// SDK can add values to the encryption context, we don't require that
73+
// the entire context matches.
7174
for (final Map.Entry<String, String> e : context.entrySet()) {
7275
if (!e.getValue().equals(decryptResult.getEncryptionContext().get(e.getKey()))) {
7376
throw new IllegalStateException("Wrong Encryption Context!");
7477
}
7578
}
7679

77-
// Now that we know we have the correct data, we can output it.
80+
// Now that we know we have the correct data, we can return it.
7881
System.out.println("Decrypted: " + decryptResult.getResult());
7982
}
8083
}

0 commit comments

Comments
 (0)