Skip to content

Commit 2d2cf62

Browse files
committed
Address PR feedback.
* Allow AESKeyring to take a `SecureRandom` parameter. * Remove 'what' comments from AESKeyring. * Rename algorithm suite name to match spec. * Rename decrypt materials function to match spec.
1 parent 9ea13e0 commit 2d2cf62

File tree

9 files changed

+31
-32
lines changed

9 files changed

+31
-32
lines changed

src/main/java/software/amazon/encryption/s3/S3EncryptionClient.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141
import software.amazon.awssdk.utils.IoUtils;
4242
import software.amazon.encryption.s3.algorithms.AlgorithmSuite;
4343
import software.amazon.encryption.s3.materials.DecryptionMaterials;
44-
import software.amazon.encryption.s3.materials.DefaultMaterialsManager;
45-
import software.amazon.encryption.s3.materials.DecryptionMaterialsRequest;
44+
import software.amazon.encryption.s3.materials.DecryptMaterialsRequest;
4645
import software.amazon.encryption.s3.materials.EncryptionMaterialsRequest;
4746
import software.amazon.encryption.s3.materials.EncryptedDataKey;
4847
import software.amazon.encryption.s3.materials.EncryptionMaterials;
@@ -170,19 +169,19 @@ public <T> T getObject(GetObjectRequest getObjectRequest, ResponseTransformer<Ge
170169
final String contentEncryptionAlgorithm = metadata.get("x-amz-cek-alg");
171170
AlgorithmSuite algorithmSuite = null;
172171
if (contentEncryptionAlgorithm.equals("AES/GCM/NoPadding")) {
173-
algorithmSuite = AlgorithmSuite.ALG_AES_256_GCM_NO_KDF;;
172+
algorithmSuite = AlgorithmSuite.ALG_AES_256_GCM_IV12_TAG16_NO_KDF;;
174173
}
175174

176175
if (algorithmSuite == null) {
177176
throw new RuntimeException("Unknown content encryption algorithm: " + contentEncryptionAlgorithm);
178177
}
179178

180-
DecryptionMaterialsRequest request = DecryptionMaterialsRequest.builder()
179+
DecryptMaterialsRequest request = DecryptMaterialsRequest.builder()
181180
.algorithmSuite(algorithmSuite)
182181
.encryptedDataKeys(encryptedDataKeys)
183182
.encryptionContext(encryptionContext)
184183
.build();
185-
DecryptionMaterials materials = _materialsManager.getDecryptionMaterials(request);
184+
DecryptionMaterials materials = _materialsManager.decryptMaterials(request);
186185

187186
// Get content encryption information
188187
SecretKey contentKey = new SecretKeySpec(materials.plaintextDataKey(), "AES");

src/main/java/software/amazon/encryption/s3/algorithms/AlgorithmSuite.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
public enum AlgorithmSuite {
5-
ALG_AES_256_GCM_NO_KDF(0x0078,
5+
ALG_AES_256_GCM_IV12_TAG16_NO_KDF(0x0078,
66
"AES",
77
256,
88
"AES/GCM/NoPadding",

src/main/java/software/amazon/encryption/s3/materials/AESKeyring.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ public class AESKeyring implements Keyring {
2222
private static final int TAG_LENGTH_BYTES = 16;
2323
private static final int TAG_LENGTH_BITS = TAG_LENGTH_BYTES * 8;
2424

25-
private final DataKeyGenerator _dataKeyGenerator;
2625
private final SecretKey _wrappingKey;
26+
private final SecureRandom _secureRandom;
27+
private final DataKeyGenerator _dataKeyGenerator;
2728

2829
private AESKeyring(Builder builder) {
29-
_dataKeyGenerator = builder._dataKeyGenerator;
3030
_wrappingKey = builder._wrappingKey;
31+
_secureRandom = builder._secureRandom;
32+
_dataKeyGenerator = builder._dataKeyGenerator;
3133
}
3234

3335
public static Builder builder() {
@@ -44,17 +46,14 @@ public EncryptionMaterials onEncrypt(EncryptionMaterials materials) {
4446
}
4547

4648
try {
47-
SecureRandom secureRandom = new SecureRandom();
48-
49-
AlgorithmSuite algorithmSuite = materials.algorithmSuite();
5049
byte[] nonce = new byte[NONCE_LENGTH_BYTES];
51-
secureRandom.nextBytes(nonce);
50+
_secureRandom.nextBytes(nonce);
5251
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(TAG_LENGTH_BITS, nonce);
5352

5453
final Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
55-
cipher.init(Cipher.ENCRYPT_MODE, _wrappingKey, gcmParameterSpec, secureRandom);
54+
cipher.init(Cipher.ENCRYPT_MODE, _wrappingKey, gcmParameterSpec, _secureRandom);
5655

57-
// this is the CONTENT encryption, not the wrapping encryption
56+
AlgorithmSuite algorithmSuite = materials.algorithmSuite();
5857
cipher.updateAAD(algorithmSuite.cipherName().getBytes(StandardCharsets.UTF_8));
5958
byte[] ciphertext = cipher.doFinal(materials.plaintextDataKey());
6059

@@ -97,12 +96,11 @@ public DecryptionMaterials onDecrypt(final DecryptionMaterials materials, List<E
9796
System.arraycopy(encodedBytes, 0, nonce, 0, nonce.length);
9897
System.arraycopy(encodedBytes, nonce.length, ciphertext, 0, ciphertext.length);
9998

100-
10199
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(TAG_LENGTH_BITS, nonce);
102100
try {
103101
final Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
104102
cipher.init(Cipher.DECRYPT_MODE, _wrappingKey, gcmParameterSpec);
105-
// this is the CONTENT encryption, not the wrapping encryption
103+
106104
AlgorithmSuite algorithmSuite = materials.algorithmSuite();
107105
cipher.updateAAD(algorithmSuite.cipherName().getBytes(StandardCharsets.UTF_8));
108106
byte[] plaintext = cipher.doFinal(ciphertext);
@@ -117,8 +115,9 @@ public DecryptionMaterials onDecrypt(final DecryptionMaterials materials, List<E
117115
}
118116

119117
public static class Builder {
120-
private DataKeyGenerator _dataKeyGenerator = new DefaultDataKeyGenerator();
121118
private SecretKey _wrappingKey;
119+
private SecureRandom _secureRandom = new SecureRandom();
120+
private DataKeyGenerator _dataKeyGenerator = new DefaultDataKeyGenerator();
122121

123122
private Builder() {}
124123

@@ -130,6 +129,11 @@ public Builder wrappingKey(SecretKey wrappingKey) {
130129
return this;
131130
}
132131

132+
public Builder secureRandom(SecureRandom secureRandom) {
133+
_secureRandom = secureRandom;
134+
return this;
135+
}
136+
133137
public Builder dataKeyGenerator(DataKeyGenerator dataKeyGenerator) {
134138
_dataKeyGenerator = dataKeyGenerator;
135139
return this;

src/main/java/software/amazon/encryption/s3/materials/DataKeyGenerator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import javax.crypto.SecretKey;
44
import software.amazon.encryption.s3.algorithms.AlgorithmSuite;
55

6+
@FunctionalInterface
67
public interface DataKeyGenerator {
78
SecretKey generateDataKey(AlgorithmSuite algorithmSuite);
89
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
import java.util.List;
55
import java.util.Map;
66
import software.amazon.encryption.s3.algorithms.AlgorithmSuite;
7-
import software.amazon.encryption.s3.materials.EncryptionMaterials.Builder;
87

9-
public class DecryptionMaterialsRequest {
8+
public class DecryptMaterialsRequest {
109

1110
private final AlgorithmSuite _algorithmSuite;
1211
private final List<EncryptedDataKey> _encryptedDataKeys;
1312
private final Map<String, String> _encryptionContext;
1413

15-
private DecryptionMaterialsRequest(Builder builder) {
14+
private DecryptMaterialsRequest(Builder builder) {
1615
this._algorithmSuite = builder._algorithmSuite;
1716
this._encryptedDataKeys = builder._encryptedDataKeys;
1817
this._encryptionContext = builder._encryptionContext;
@@ -36,7 +35,7 @@ public Map<String, String> encryptionContext() {
3635

3736
static public class Builder {
3837

39-
private AlgorithmSuite _algorithmSuite = AlgorithmSuite.ALG_AES_256_GCM_NO_KDF;
38+
private AlgorithmSuite _algorithmSuite = AlgorithmSuite.ALG_AES_256_GCM_IV12_TAG16_NO_KDF;
4039
private Map<String, String> _encryptionContext = Collections.emptyMap();
4140
private List<EncryptedDataKey> _encryptedDataKeys = Collections.emptyList();
4241

@@ -62,8 +61,8 @@ public Builder encryptedDataKeys(List<EncryptedDataKey> encryptedDataKeys) {
6261
return this;
6362
}
6463

65-
public DecryptionMaterialsRequest build() {
66-
return new DecryptionMaterialsRequest(this);
64+
public DecryptMaterialsRequest build() {
65+
return new DecryptMaterialsRequest(this);
6766
}
6867
}
6968
}

src/main/java/software/amazon/encryption/s3/materials/DecryptionMaterials.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public Builder toBuilder() {
5252

5353
static public class Builder {
5454

55-
private AlgorithmSuite _algorithmSuite = AlgorithmSuite.ALG_AES_256_GCM_NO_KDF;
55+
private AlgorithmSuite _algorithmSuite = AlgorithmSuite.ALG_AES_256_GCM_IV12_TAG16_NO_KDF;
5656
private Map<String, String> _encryptionContext = Collections.emptyMap();
5757
private byte[] _plaintextDataKey = null;
5858

src/main/java/software/amazon/encryption/s3/materials/DefaultMaterialsManager.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package software.amazon.encryption.s3.materials;
22

3-
import java.security.NoSuchAlgorithmException;
4-
import java.security.SecureRandom;
5-
import javax.crypto.KeyGenerator;
6-
import javax.crypto.SecretKey;
73
import software.amazon.encryption.s3.algorithms.AlgorithmSuite;
84

95
public class DefaultMaterialsManager implements MaterialsManager {
@@ -16,14 +12,14 @@ public DefaultMaterialsManager(Keyring keyring) {
1612

1713
public EncryptionMaterials getEncryptionMaterials(EncryptionMaterialsRequest request) {
1814
EncryptionMaterials materials = EncryptionMaterials.builder()
19-
.algorithmSuite(AlgorithmSuite.ALG_AES_256_GCM_NO_KDF)
15+
.algorithmSuite(AlgorithmSuite.ALG_AES_256_GCM_IV12_TAG16_NO_KDF)
2016
.encryptionContext(request.encryptionContext())
2117
.build();
2218

2319
return _keyring.onEncrypt(materials);
2420
}
2521

26-
public DecryptionMaterials getDecryptionMaterials(DecryptionMaterialsRequest request) {
22+
public DecryptionMaterials decryptMaterials(DecryptMaterialsRequest request) {
2723
DecryptionMaterials materials = DecryptionMaterials.builder()
2824
.algorithmSuite(request.algorithmSuite())
2925
.encryptionContext(request.encryptionContext())

src/main/java/software/amazon/encryption/s3/materials/EncryptionMaterials.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public Builder toBuilder() {
6060

6161
static public class Builder {
6262

63-
private AlgorithmSuite _algorithmSuite = AlgorithmSuite.ALG_AES_256_GCM_NO_KDF;
63+
private AlgorithmSuite _algorithmSuite = AlgorithmSuite.ALG_AES_256_GCM_IV12_TAG16_NO_KDF;
6464
private Map<String, String> _encryptionContext = Collections.emptyMap();
6565
private List<EncryptedDataKey> _encryptedDataKeys = Collections.emptyList();
6666
private byte[] _plaintextDataKey = null;

src/main/java/software/amazon/encryption/s3/materials/MaterialsManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
public interface MaterialsManager {
44
EncryptionMaterials getEncryptionMaterials(EncryptionMaterialsRequest request);
5-
DecryptionMaterials getDecryptionMaterials(DecryptionMaterialsRequest request);
5+
DecryptionMaterials decryptMaterials(DecryptMaterialsRequest request);
66
}

0 commit comments

Comments
 (0)