-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[Storage] Widen CSE Encoder Counter for Region Nonce Index #50205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Isabelle (ibrandes)
wants to merge
2
commits into
Azure:main
Choose a base branch
from
ibrandes:bugfix/storage/encoderCounterFix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
.../src/test/java/com/azure/storage/blob/specialized/cryptography/EncryptorV2NonceTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.storage.blob.specialized.cryptography; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import reactor.core.publisher.Flux; | ||
|
|
||
| import javax.crypto.SecretKey; | ||
| import javax.crypto.spec.SecretKeySpec; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.nio.ByteBuffer; | ||
| import java.security.SecureRandom; | ||
| import java.util.List; | ||
|
|
||
| import static com.azure.storage.blob.specialized.cryptography.CryptographyConstants.ENCRYPTION_PROTOCOL_V2; | ||
| import static com.azure.storage.blob.specialized.cryptography.CryptographyConstants.NONCE_LENGTH; | ||
| import static com.azure.storage.blob.specialized.cryptography.CryptographyConstants.TAG_LENGTH; | ||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| /** | ||
| * Unit tests for the CSEv2 region nonce counter in {@link EncryptorV2}. | ||
| * <p> | ||
| * Each authenticated region is encrypted under a nonce derived from its sequential index. The index must be encoded | ||
| * using the full 64-bit value; truncating it to 32 bits causes nonces to repeat every 2^32 regions, which is AES-GCM | ||
| * nonce reuse (a security failure). These tests exercise the encoding directly and end-to-end through | ||
| * {@link EncryptorV2#encrypt(Flux)}. | ||
| */ | ||
| public class EncryptorV2NonceTests { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I think we should name this
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just something to check:
|
||
| private static final SecureRandom RANDOM = new SecureRandom(); | ||
|
|
||
| @Test | ||
| public void regionZeroNonceIsAllZeros() { | ||
| assertArrayEquals(new byte[NONCE_LENGTH], EncryptorV2.computeRegionNonce(0)); | ||
| } | ||
|
|
||
| @Test | ||
| public void nonceEncodesRegionIndexAsBigEndianLongWithTrailingZeros() { | ||
| for (long index : new long[] { 1, 2, 255, 256, 1_000_000, Integer.MAX_VALUE }) { | ||
| byte[] nonce = EncryptorV2.computeRegionNonce(index); | ||
|
|
||
| assertEquals(NONCE_LENGTH, nonce.length); | ||
| // First 8 bytes are the big-endian index. | ||
| byte[] expectedPrefix = ByteBuffer.allocate(Long.BYTES).putLong(index).array(); | ||
| byte[] actualPrefix = new byte[Long.BYTES]; | ||
| System.arraycopy(nonce, 0, actualPrefix, 0, Long.BYTES); | ||
| assertArrayEquals(expectedPrefix, actualPrefix, "index=" + index); | ||
| // Remaining bytes are zero. | ||
| for (int i = Long.BYTES; i < NONCE_LENGTH; i++) { | ||
| assertEquals(0, nonce[i], "trailing byte " + i + " for index=" + index); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void nonceUsesFullLongInsteadOfTruncatedInt() { | ||
| // Region 2^31 exceeds the positive int range. The old encoder truncated the index to an int, producing | ||
| // Integer.MIN_VALUE and, after sign extension, a nonce beginning with 0xFFFFFFFF. The full-long encoding must | ||
| // instead leave the high four bytes zero. | ||
| long index = 1L << 31; | ||
| byte[] nonce = EncryptorV2.computeRegionNonce(index); | ||
|
|
||
| for (int i = 0; i < 4; i++) { | ||
| assertEquals(0, nonce[i], "high byte " + i + " should be zero, not sign-extended"); | ||
| } | ||
| // Byte 4 holds the top bit of the 2^31 value. | ||
| assertEquals((byte) 0x80, nonce[4]); | ||
|
|
||
| // Explicitly confirm it differs from what a truncated-int counter would have produced. | ||
| byte[] truncated = ByteBuffer.allocate(NONCE_LENGTH).putLong((int) index).array(); | ||
| assertFalse(java.util.Arrays.equals(truncated, nonce), | ||
| "full-long nonce must differ from the truncated-int nonce at index 2^31"); | ||
| } | ||
|
|
||
| @Test | ||
| public void regionsExactlyNonceWrapApartHaveDistinctNonces() { | ||
| // The core regression: with a truncated 32-bit counter, region N and region N + 2^32 share a nonce (GCM nonce | ||
| // reuse). The full-long counter must give them distinct nonces. | ||
| long wrap = 1L << 32; | ||
|
|
||
| assertFalse(java.util.Arrays.equals(EncryptorV2.computeRegionNonce(0), EncryptorV2.computeRegionNonce(wrap))); | ||
| assertFalse( | ||
| java.util.Arrays.equals(EncryptorV2.computeRegionNonce(5), EncryptorV2.computeRegionNonce(wrap + 5))); | ||
|
|
||
| // Region 2^32 encodes as {0,0,0,1, 0,0,0,0, 0,0,0,0}. | ||
| byte[] expected = new byte[NONCE_LENGTH]; | ||
| expected[3] = 1; | ||
| assertArrayEquals(expected, EncryptorV2.computeRegionNonce(wrap)); | ||
| } | ||
|
|
||
| @Test | ||
| public void allNoncesUniqueAcrossWrapBoundarySample() { | ||
| // Sample indices straddling the old 2^32 wrap point must all be distinct. | ||
| long[] indices = { 0, 1, 2, (1L << 31) - 1, 1L << 31, (1L << 32) - 1, 1L << 32, (1L << 32) + 1, 1L << 33 }; | ||
| for (int i = 0; i < indices.length; i++) { | ||
| for (int j = i + 1; j < indices.length; j++) { | ||
| assertFalse( | ||
| java.util.Arrays.equals(EncryptorV2.computeRegionNonce(indices[i]), | ||
| EncryptorV2.computeRegionNonce(indices[j])), | ||
| "nonces for " + indices[i] + " and " + indices[j] + " must differ"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void encryptEmitsSequentialRegionNonces() { | ||
| // End-to-end: encrypt a multi-region blob with a small region size and confirm each region is prefixed with the | ||
| // nonce for its sequential index. | ||
| int regionLength = 16; | ||
| int regionCount = 5; | ||
| int plaintextLength = regionLength * (regionCount - 1) + 7; // last region is partial | ||
|
|
||
| SecretKey key = new SecretKeySpec(randomBytes(32), CryptographyConstants.AES); | ||
| BlobClientSideEncryptionOptions options | ||
| = new BlobClientSideEncryptionOptions().setAuthenticatedRegionDataLengthInBytes(regionLength); | ||
| EncryptorV2 encryptor = new EncryptorV2(key, options, ENCRYPTION_PROTOCOL_V2); | ||
|
|
||
| byte[] plaintext = randomBytes(plaintextLength); | ||
| List<ByteBuffer> emitted = encryptor.encrypt(Flux.just(ByteBuffer.wrap(plaintext))).collectList().block(); | ||
|
|
||
| byte[] ciphertext = concat(emitted); | ||
| int offset = 0; | ||
| int remaining = plaintextLength; | ||
| for (long region = 0; region < regionCount; region++) { | ||
| byte[] nonce = new byte[NONCE_LENGTH]; | ||
| System.arraycopy(ciphertext, offset, nonce, 0, NONCE_LENGTH); | ||
| assertArrayEquals(EncryptorV2.computeRegionNonce(region), nonce, "region " + region + " nonce"); | ||
|
|
||
| int regionData = Math.min(regionLength, remaining); | ||
| offset += NONCE_LENGTH + regionData + TAG_LENGTH; | ||
| remaining -= regionData; | ||
| } | ||
| assertEquals(ciphertext.length, offset, "consumed the entire ciphertext"); | ||
| assertTrue(remaining <= 0); | ||
| } | ||
|
|
||
| private static byte[] concat(List<ByteBuffer> buffers) { | ||
| ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
| for (ByteBuffer buffer : buffers) { | ||
| byte[] bytes = new byte[buffer.remaining()]; | ||
| buffer.get(bytes); | ||
| out.write(bytes, 0, bytes.length); | ||
| } | ||
| return out.toByteArray(); | ||
| } | ||
|
|
||
| private static byte[] randomBytes(int length) { | ||
| byte[] bytes = new byte[length]; | ||
| RANDOM.nextBytes(bytes); | ||
| return bytes; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there are two things we could explain here:
nonces to repeat andcomputeRegionNonce()