Skip to content

Commit 23af3d6

Browse files
authored
Merge pull request #30 from bdonlan/estimate-size
Make estimateCiphertextSize work with cached keys
2 parents 756c1e4 + bc580d0 commit 23af3d6

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ public long estimateCiphertextSize(
176176
EncryptionMaterialsRequest request = EncryptionMaterialsRequest.newBuilder()
177177
.setContext(encryptionContext)
178178
.setRequestedAlgorithm(getEncryptionAlgorithm())
179+
// We're not actually encrypting any data, so don't consume any bytes from the cache's limits. We do need to
180+
// pass /something/ though, or the cache will be bypassed (as it'll assume this is a streaming encrypt of
181+
// unknown size).
182+
.setPlaintextSize(0)
179183
.build();
180184

181185
final MessageCryptoHandler cryptoHandler = new EncryptionHandler(

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,16 @@
3636
import java.util.EnumSet;
3737
import java.util.HashMap;
3838
import java.util.Map;
39+
import java.util.concurrent.TimeUnit;
3940

4041
import org.junit.Assert;
4142
import org.junit.Before;
4243
import org.junit.Test;
44+
import org.mockito.ArgumentCaptor;
4345
import org.mockito.Mockito;
4446

47+
import com.amazonaws.encryptionsdk.caching.CachingCryptoMaterialsManager;
48+
import com.amazonaws.encryptionsdk.caching.LocalCryptoMaterialsCache;
4549
import com.amazonaws.encryptionsdk.exception.AwsCryptoException;
4650
import com.amazonaws.encryptionsdk.exception.BadCiphertextException;
4751
import com.amazonaws.encryptionsdk.internal.StaticMasterKey;
@@ -61,7 +65,7 @@ public void init() {
6165
masterKeyProvider = spy(new StaticMasterKey("testmaterial"));
6266

6367
encryptionClient_ = new AwsCrypto();
64-
encryptionClient_.setEncryptionAlgorithm(CryptoAlgorithm.ALG_AES_192_GCM_IV12_TAG16_NO_KDF);
68+
encryptionClient_.setEncryptionAlgorithm(CryptoAlgorithm.ALG_AES_128_GCM_IV12_TAG16_HKDF_SHA256);
6569
}
6670

6771
private void doEncryptDecrypt(final CryptoAlgorithm cryptoAlg, final int byteSize, final int frameSize) {
@@ -374,6 +378,28 @@ public void estimateCiphertextSizeWithoutEncContext() {
374378
assertTrue(errMsg, estimatedCiphertextSize - cipherText.length <= 16);
375379
}
376380

381+
@Test
382+
public void estimateCiphertextSize_usesCachedKeys() throws Exception {
383+
// Make sure estimateCiphertextSize works with cached CMMs
384+
CryptoMaterialsManager cmm = spy(new DefaultCryptoMaterialsManager(masterKeyProvider));
385+
386+
CachingCryptoMaterialsManager cache = CachingCryptoMaterialsManager.newBuilder()
387+
.withBackingMaterialsManager(cmm)
388+
.withMaxAge(Long.MAX_VALUE, TimeUnit.SECONDS)
389+
.withCache(new LocalCryptoMaterialsCache(1))
390+
.withMessageUseLimit(9999)
391+
.withByteUseLimit(501)
392+
.build();
393+
394+
// These estimates should be cached, and should not consume any bytes from the byte use limit.
395+
encryptionClient_.estimateCiphertextSize(cache, 500, new HashMap<>());
396+
encryptionClient_.estimateCiphertextSize(cache, 500, new HashMap<>());
397+
398+
encryptionClient_.encryptData(cache, new byte[500]);
399+
400+
verify(cmm, times(1)).getMaterialsForEncrypt(any());
401+
}
402+
377403
@Test
378404
public void encryptDecryptWithoutEncContext() {
379405
final int ptSize = 1000000; // 1MB

0 commit comments

Comments
 (0)