Skip to content

Commit 0a3e6e3

Browse files
Splitting MKP and keyring unit tests
1 parent d444f52 commit 0a3e6e3

File tree

3 files changed

+79
-60
lines changed

3 files changed

+79
-60
lines changed

src/main/java/com/amazonaws/encryptionsdk/AwsCrypto.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ public Builder encryptionContext(Map<String, String> encryptionContext) {
10061006
/**
10071007
* Constructs the AwsCryptoConfig instance.
10081008
*
1009-
* @return The AwCryptoConfig instance
1009+
* @return The AwsCryptoConfig instance
10101010
*/
10111011
public AwsCryptoConfig build() {
10121012
return new AwsCryptoConfig(this);

src/test/java/com/amazonaws/encryptionsdk/DefaultCryptoMaterialsManagerTest.java

+74-38
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,35 @@ public class DefaultCryptoMaterialsManagerTest {
4545
private static final MasterKey<?> mk1 = new StaticMasterKey("mk1");
4646
private static final MasterKey<?> mk2 = new StaticMasterKey("mk2");
4747
private static final Keyring keyring1 = new StaticKeyring("keyring1");
48-
private static final Keyring keyring2 = new StaticKeyring("keyring2");
4948

5049
@Test
51-
public void encrypt_testBasicFunctionality() {
50+
public void encrypt_testBasicFunctionalityWithMkp() {
5251
EncryptionMaterialsRequest req = EncryptionMaterialsRequest.newBuilder().build();
53-
EncryptionMaterials resultMkp = new DefaultCryptoMaterialsManager(mk1).getMaterialsForEncrypt(req);
54-
EncryptionMaterials resultKeyring = new DefaultCryptoMaterialsManager(keyring1).getMaterialsForEncrypt(req);
55-
56-
assertNotNull(resultMkp.getAlgorithm());
57-
assertNotNull(resultMkp.getCleartextDataKey());
58-
assertNotNull(resultMkp.getEncryptionContext());
59-
assertEquals(1, resultMkp.getEncryptedDataKeys().size());
60-
assertEquals(1, resultMkp.getMasterKeys().size());
61-
assertEquals(mk1, resultMkp.getMasterKeys().get(0));
62-
63-
assertNotNull(resultKeyring.getAlgorithm());
64-
assertNotNull(resultKeyring.getCleartextDataKey());
65-
assertNotNull(resultKeyring.getEncryptionContext());
66-
assertNotNull(resultKeyring.getKeyringTrace());
67-
assertEquals(1, resultKeyring.getEncryptedDataKeys().size());
68-
assertEquals(0, resultKeyring.getMasterKeys().size());
52+
EncryptionMaterials result = new DefaultCryptoMaterialsManager(mk1).getMaterialsForEncrypt(req);
53+
54+
assertNotNull(result.getAlgorithm());
55+
assertNotNull(result.getCleartextDataKey());
56+
assertNotNull(result.getEncryptionContext());
57+
assertEquals(1, result.getEncryptedDataKeys().size());
58+
assertEquals(1, result.getMasterKeys().size());
59+
assertEquals(mk1, result.getMasterKeys().get(0));
6960
}
7061

7162
@Test
72-
public void encrypt_noSignatureKeyOnUnsignedAlgo() throws Exception {
63+
public void encrypt_testBasicFunctionalityWithKeyring() {
64+
EncryptionMaterialsRequest req = EncryptionMaterialsRequest.newBuilder().build();
65+
EncryptionMaterials result = new DefaultCryptoMaterialsManager(keyring1).getMaterialsForEncrypt(req);
66+
67+
assertNotNull(result.getAlgorithm());
68+
assertNotNull(result.getCleartextDataKey());
69+
assertNotNull(result.getEncryptionContext());
70+
assertNotNull(result.getKeyringTrace());
71+
assertEquals(1, result.getEncryptedDataKeys().size());
72+
assertEquals(0, result.getMasterKeys().size());
73+
}
74+
75+
@Test
76+
public void encrypt_noSignatureKeyOnUnsignedAlgoWithMkp() throws Exception {
7377
CryptoAlgorithm[] algorithms = new CryptoAlgorithm[] {
7478
CryptoAlgorithm.ALG_AES_128_GCM_IV12_TAG16_HKDF_SHA256,
7579
CryptoAlgorithm.ALG_AES_128_GCM_IV12_TAG16_NO_KDF,
@@ -82,21 +86,38 @@ public void encrypt_noSignatureKeyOnUnsignedAlgo() throws Exception {
8286
for (CryptoAlgorithm algo : algorithms) {
8387
EncryptionMaterialsRequest req
8488
= EncryptionMaterialsRequest.newBuilder().setRequestedAlgorithm(algo).build();
85-
EncryptionMaterials mkpResult = new DefaultCryptoMaterialsManager(mk1).getMaterialsForEncrypt(req);
86-
EncryptionMaterials keyringResult = new DefaultCryptoMaterialsManager(keyring1).getMaterialsForEncrypt(req);
89+
EncryptionMaterials result = new DefaultCryptoMaterialsManager(mk1).getMaterialsForEncrypt(req);
90+
91+
assertNull(result.getTrailingSignatureKey());
92+
assertEquals(0, result.getEncryptionContext().size());
93+
assertEquals(algo, result.getAlgorithm());
94+
}
95+
}
96+
97+
@Test
98+
public void encrypt_noSignatureKeyOnUnsignedAlgoWithKeyring() throws Exception {
99+
CryptoAlgorithm[] algorithms = new CryptoAlgorithm[] {
100+
CryptoAlgorithm.ALG_AES_128_GCM_IV12_TAG16_HKDF_SHA256,
101+
CryptoAlgorithm.ALG_AES_128_GCM_IV12_TAG16_NO_KDF,
102+
CryptoAlgorithm.ALG_AES_192_GCM_IV12_TAG16_HKDF_SHA256,
103+
CryptoAlgorithm.ALG_AES_192_GCM_IV12_TAG16_NO_KDF,
104+
CryptoAlgorithm.ALG_AES_256_GCM_IV12_TAG16_HKDF_SHA256,
105+
CryptoAlgorithm.ALG_AES_256_GCM_IV12_TAG16_NO_KDF
106+
};
87107

88-
assertNull(mkpResult.getTrailingSignatureKey());
89-
assertEquals(0, mkpResult.getEncryptionContext().size());
90-
assertEquals(algo, mkpResult.getAlgorithm());
108+
for (CryptoAlgorithm algo : algorithms) {
109+
EncryptionMaterialsRequest req
110+
= EncryptionMaterialsRequest.newBuilder().setRequestedAlgorithm(algo).build();
111+
EncryptionMaterials result = new DefaultCryptoMaterialsManager(keyring1).getMaterialsForEncrypt(req);
91112

92-
assertNull(keyringResult.getTrailingSignatureKey());
93-
assertEquals(0, keyringResult.getEncryptionContext().size());
94-
assertEquals(algo, keyringResult.getAlgorithm());
113+
assertNull(result.getTrailingSignatureKey());
114+
assertEquals(0, result.getEncryptionContext().size());
115+
assertEquals(algo, result.getAlgorithm());
95116
}
96117
}
97118

98119
@Test
99-
public void encrypt_hasSignatureKeyForSignedAlgo() throws Exception {
120+
public void encrypt_hasSignatureKeyForSignedAlgoWithMkp() throws Exception {
100121
CryptoAlgorithm[] algorithms = new CryptoAlgorithm[] {
101122
CryptoAlgorithm.ALG_AES_128_GCM_IV12_TAG16_HKDF_SHA256_ECDSA_P256,
102123
CryptoAlgorithm.ALG_AES_192_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384,
@@ -107,19 +128,34 @@ public void encrypt_hasSignatureKeyForSignedAlgo() throws Exception {
107128

108129
EncryptionMaterialsRequest req
109130
= EncryptionMaterialsRequest.newBuilder().setRequestedAlgorithm(algo).build();
110-
EncryptionMaterials mkpResult = new DefaultCryptoMaterialsManager(mk1).getMaterialsForEncrypt(req);
111-
EncryptionMaterials keyringResult = new DefaultCryptoMaterialsManager(keyring1).getMaterialsForEncrypt(req);
131+
EncryptionMaterials result = new DefaultCryptoMaterialsManager(mk1).getMaterialsForEncrypt(req);
112132

113133

114-
assertNotNull(mkpResult.getTrailingSignatureKey());
115-
assertEquals(1, mkpResult.getEncryptionContext().size());
116-
assertNotNull(mkpResult.getEncryptionContext().get(Constants.EC_PUBLIC_KEY_FIELD));
117-
assertEquals(algo, mkpResult.getAlgorithm());
134+
assertNotNull(result.getTrailingSignatureKey());
135+
assertEquals(1, result.getEncryptionContext().size());
136+
assertNotNull(result.getEncryptionContext().get(Constants.EC_PUBLIC_KEY_FIELD));
137+
assertEquals(algo, result.getAlgorithm());
138+
}
139+
}
140+
141+
@Test
142+
public void encrypt_hasSignatureKeyForSignedAlgoWithKeyring() throws Exception {
143+
CryptoAlgorithm[] algorithms = new CryptoAlgorithm[] {
144+
CryptoAlgorithm.ALG_AES_128_GCM_IV12_TAG16_HKDF_SHA256_ECDSA_P256,
145+
CryptoAlgorithm.ALG_AES_192_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384,
146+
CryptoAlgorithm.ALG_AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384
147+
};
148+
149+
for (CryptoAlgorithm algo : algorithms) {
150+
151+
EncryptionMaterialsRequest req
152+
= EncryptionMaterialsRequest.newBuilder().setRequestedAlgorithm(algo).build();
153+
EncryptionMaterials result = new DefaultCryptoMaterialsManager(keyring1).getMaterialsForEncrypt(req);
118154

119-
assertNotNull(keyringResult.getTrailingSignatureKey());
120-
assertEquals(1, keyringResult.getEncryptionContext().size());
121-
assertNotNull(keyringResult.getEncryptionContext().get(Constants.EC_PUBLIC_KEY_FIELD));
122-
assertEquals(algo, keyringResult.getAlgorithm());
155+
assertNotNull(result.getTrailingSignatureKey());
156+
assertEquals(1, result.getEncryptionContext().size());
157+
assertNotNull(result.getEncryptionContext().get(Constants.EC_PUBLIC_KEY_FIELD));
158+
assertEquals(algo, result.getAlgorithm());
123159
}
124160
}
125161

src/test/java/com/amazonaws/encryptionsdk/internal/StaticKeyring.java

+4-21
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@
2424
import javax.annotation.Nonnull;
2525
import javax.annotation.concurrent.NotThreadSafe;
2626
import javax.crypto.Cipher;
27-
import javax.crypto.KeyGenerator;
2827
import javax.crypto.SecretKey;
2928
import javax.crypto.spec.SecretKeySpec;
3029
import java.nio.charset.StandardCharsets;
3130
import java.security.GeneralSecurityException;
3231
import java.security.KeyFactory;
3332
import java.security.PrivateKey;
3433
import java.security.PublicKey;
35-
import java.security.SecureRandom;
3634
import java.security.spec.KeySpec;
3735
import java.security.spec.PKCS8EncodedKeySpec;
3836
import java.security.spec.X509EncodedKeySpec;
@@ -55,11 +53,6 @@
5553
public class StaticKeyring implements Keyring {
5654
private static final String PROVIDER_ID = "static_provider";
5755

58-
/**
59-
* Generates random strings that can be used to create data keys.
60-
*/
61-
private static final SecureRandom SRAND = new SecureRandom();
62-
6356
/**
6457
* Encryption algorithm for the key-pair
6558
*/
@@ -70,15 +63,10 @@ public class StaticKeyring implements Keyring {
7063
*/
7164
private static final String KEY_FACTORY_ALGORITHM = "RSA";
7265

73-
/**
74-
* Encryption algorithm for the randomly generated data key
75-
*/
76-
private static final String DATA_KEY_ENCRYPTION_ALGORITHM = "AES";
77-
7866
/**
7967
* The ID of the key
8068
*/
81-
private String keyId_;
69+
private final String keyId_;
8270

8371
/**
8472
* The {@link Cipher} object created with the public part of
@@ -92,11 +80,6 @@ public class StaticKeyring implements Keyring {
9280
*/
9381
private final Cipher keyDecryptionCipher_;
9482

95-
/**
96-
* Generates random data keys.
97-
*/
98-
private KeyGenerator keyGenerator_;
99-
10083
/**
10184
* Creates a new object that encrypts the data key with a key
10285
* whose id is {@code keyId}.
@@ -162,9 +145,9 @@ public void onDecrypt(DecryptionMaterials decryptionMaterials, List<? extends En
162145

163146
private void generateDataKey(EncryptionMaterials encryptionMaterials) {
164147
try {
165-
this.keyGenerator_ = KeyGenerator.getInstance(DATA_KEY_ENCRYPTION_ALGORITHM);
166-
this.keyGenerator_.init(encryptionMaterials.getAlgorithm().getDataKeyLength() * 8, SRAND);
167-
SecretKey key = new SecretKeySpec(keyGenerator_.generateKey().getEncoded(), encryptionMaterials.getAlgorithm().getDataKeyAlgo());
148+
final byte[] rawKey = new byte[encryptionMaterials.getAlgorithm().getDataKeyLength()];
149+
Utils.getSecureRandom().nextBytes(rawKey);
150+
SecretKey key = new SecretKeySpec(rawKey, encryptionMaterials.getAlgorithm().getDataKeyAlgo());
168151
byte[] encryptedKey = keyEncryptionCipher_.doFinal(key.getEncoded());
169152

170153
encryptionMaterials.setCleartextDataKey(key,

0 commit comments

Comments
 (0)