Avoid overflows when calculating media ciphertext size

This commit is contained in:
Jon Chambers authored and Jon Chambers committed 2026-04-14 10:46:16 -04:00
1 parent 68b3e9a07c
commit 56f25f5a39
4 files changed
+55 -10

No files matched your search

@@ -4,6 +4,8 @@
*/
package org.whispersystems.textsecuregcm.backup;
import com.google.common.annotations.VisibleForTesting;
/**
* Descriptor for a single copy-and-encrypt operation
*
@@ -24,6 +26,25 @@ public record CopyParameters(
* @return The size of the double-encrypted destination object after it is copied
*/
long destinationObjectSize() {
return encryptionParameters().outputSize(sourceLength());
return destinationObjectSize(sourceLength());
}
/// Calculates the size of a ciphertext for a media object stored as part of a backup
///
/// @param inputSize the size, in bytes, of the plaintext media object
///
/// @return the size, in bytes, of the ciphertext of a media object with the given `inputSize`
@VisibleForTesting
static long destinationObjectSize(final int inputSize) {
if (inputSize < 0) {
throw new IllegalArgumentException("Size must be non-negative, but was " + inputSize);
}
// AES-256 has 16-byte block size, and always adds a block if the plaintext is a multiple of the block size
final long numBlocks = ((long) inputSize + 16) / 16;
// 16-byte IV will be generated and prepended to the ciphertext
// IV + AES-256 encrypted data + HmacSHA256
return 16 + (numBlocks * 16) + 32;
}
}
@@ -11,12 +11,4 @@ public record MediaEncryptionParameters(
new SecretKeySpec(encryptionKey, "AES"),
new SecretKeySpec(macKey, "HmacSHA256"));
}
public int outputSize(final int inputSize) {
// AES-256 has 16-byte block size, and always adds a block if the plaintext is a multiple of the block size
final int numBlocks = (inputSize + 16) / 16;
// 16-byte IV will be generated and prepended to the ciphertext
// IV + AES-256 encrypted data + HmacSHA256
return 16 + (numBlocks * 16) + 32;
}
}
@@ -485,7 +485,7 @@ public class BackupManagerTest {
// Copying can start on the next batch of USAGE_CHECKPOINT_COUNT before the current one is done, so we should see
// at least one usage update, and at most 2
final long bytesPerObject = COPY_ENCRYPTION_PARAM.outputSize(100);
final long bytesPerObject = CopyParameters.destinationObjectSize(100);
assertThat(backupsDb.getMediaUsage(backupUser).join().usageInfo()).isIn(
new UsageInfo(
bytesPerObject * backupConfiguration.usageCheckpointCount(),
@@ -0,0 +1,32 @@
/*
* Copyright 2026 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.textsecuregcm.backup;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
class CopyParametersTest {
@ParameterizedTest
@CsvSource({
"0, 64",
"15, 64",
"16, 80",
"2_147_483_647, 2_147_483_696"
})
void destinationObjectSize(final int inputSize, final long expectedOutputSize) {
assertEquals(expectedOutputSize, CopyParameters.destinationObjectSize(inputSize));
}
@Test
void destinationObjectSizeNegative() {
assertThrows(IllegalArgumentException.class, () -> CopyParameters.destinationObjectSize(-1));
}
}