diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/backup/CopyParameters.java b/service/src/main/java/org/whispersystems/textsecuregcm/backup/CopyParameters.java index f132284a2..b9da80131 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/backup/CopyParameters.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/backup/CopyParameters.java @@ -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; } } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/backup/MediaEncryptionParameters.java b/service/src/main/java/org/whispersystems/textsecuregcm/backup/MediaEncryptionParameters.java index 7c5014f50..96f51b947 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/backup/MediaEncryptionParameters.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/backup/MediaEncryptionParameters.java @@ -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; - } } diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/backup/BackupManagerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/backup/BackupManagerTest.java index 639807840..e710acb6b 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/backup/BackupManagerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/backup/BackupManagerTest.java @@ -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(), diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/backup/CopyParametersTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/backup/CopyParametersTest.java new file mode 100644 index 000000000..615992505 --- /dev/null +++ b/service/src/test/java/org/whispersystems/textsecuregcm/backup/CopyParametersTest.java @@ -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)); + } +}