Skip to content

Commit

Permalink
Add validation that firstSegmentOffset is not negative.
Browse files Browse the repository at this point in the history
This parameter can only be set in the subtle API, and I don't think anybody uses it.

PiperOrigin-RevId: 627982997
Change-Id: Id387c2637933e8b832757e9d35e62ea6ae5db889
  • Loading branch information
juergw authored and copybara-github committed Apr 25, 2024
1 parent 26ab759 commit 96e8c0d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ private static void validateParameters(
throw new InvalidAlgorithmParameterException(
"ikm too short, must be >= " + Math.max(16, keySizeInBytes));
}
if (firstSegmentOffset < 0) {
throw new InvalidAlgorithmParameterException("firstSegmentOffset must not be negative");
}
Validators.validateAesKeySize(keySizeInBytes);
if (tagSizeInBytes < 10) {
throw new InvalidAlgorithmParameterException("tag size too small " + tagSizeInBytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,22 @@ public void testEncryptDecryptRandomAccessLastSegmentFull() throws Exception {
testEncryptDecryptRandomAccess(16, 12, 256, 16, 440);
}

@Test
public void testNegativeFirstSegmentOffset_throws() throws Exception {
byte[] ikm = Hex.decode("000102030405060708090a0b0c0d0e0f00112233445566778899aabbccddeeff");
assertThrows(
GeneralSecurityException.class,
() ->
new AesCtrHmacStreaming(
ikm,
"HmacSha256",
/* keySizeInBytes= */ 16,
"HmacSha256",
/* tagSizeInBytes= */ 12,
/* ciphertextSegmentSize= */ 4096,
/* firstSegmentOffset= */ -1));
}

/**
* One case that is sometimes problematic is writing single bytes to a stream. This test
* constructs an OutputStream from a WritableByteChannel and tests whether encryption works on
Expand Down

0 comments on commit 96e8c0d

Please sign in to comment.