Skip to content

Commit bc580d0

Browse files
author
Bryan Donlan
committed
Make estimateCiphertextSize work with cached keys
estimateCiphertextSize was not passing a plaintext size down to the CMM, so the caching CMM assumed that a streaming encryption of unknown size was being performed, and bypassed the cache entirely. This change passes a plaintext size of zero instead to allow cached keys to be used; since we don't actually encrypt any data it's safe to not consume any of the byte limit. Note that this may not behave quite right if the CMM does more clever things with plaintext size. In general we should probably resolve this by moving estimateCiphertextSize over to the Cipher*Stream objects instead, where it can know the actual DataKeys in use instead of hoping they're reasonably consistently sized. Fixes: #29
1 parent a8d4c01 commit bc580d0

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
@@ -171,6 +171,10 @@ public long estimateCiphertextSize(
171171
EncryptionMaterialsRequest request = EncryptionMaterialsRequest.newBuilder()
172172
.setContext(encryptionContext)
173173
.setRequestedAlgorithm(getEncryptionAlgorithm())
174+
// We're not actually encrypting any data, so don't consume any bytes from the cache's limits. We do need to
175+
// pass /something/ though, or the cache will be bypassed (as it'll assume this is a streaming encrypt of
176+
// unknown size).
177+
.setPlaintextSize(0)
174178
.build();
175179

176180
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)