Skip to content

Commit 647184e

Browse files
YutSeanApache9
authored andcommitted
HBASE-26613 The logic of the method incrementIV in Encryption class has problem (apache#3968)
Signed-off-by: Duo Zhang <[email protected]>
1 parent 9c01d04 commit 647184e

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

hbase-common/src/main/java/org/apache/hadoop/hbase/io/crypto/Encryption.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -640,20 +640,17 @@ public static void incrementIv(byte[] iv) {
640640
}
641641

642642
public static void incrementIv(byte[] iv, int v) {
643+
// v should be > 0
643644
int length = iv.length;
644-
boolean carry = true;
645-
// TODO: Optimize for v > 1, e.g. 16, 32
646-
do {
647-
for (int i = 0; i < length; i++) {
648-
if (carry) {
649-
iv[i] = (byte) ((iv[i] + 1) & 0xFF);
650-
carry = 0 == iv[i];
651-
} else {
652-
break;
653-
}
645+
int sum = 0;
646+
for (int i = 0; i < length; i++) {
647+
if (v <= 0) {
648+
break;
654649
}
655-
v--;
656-
} while (v > 0);
650+
sum = v + (iv[i] & 0xFF);
651+
v = sum / 256;
652+
iv[i] = (byte)(sum % 256);
653+
}
657654
}
658655

659656
/**

hbase-common/src/test/java/org/apache/hadoop/hbase/io/crypto/TestEncryption.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,26 @@ public void testTypicalHFileBlocks() throws Exception {
8989
}
9090
}
9191

92+
@Test
93+
public void testIncrementIV() {
94+
byte[] iv = new byte[] {1, 2, 3};
95+
byte[] iv_neg = new byte[] {-3, -13, 25};
96+
Encryption.incrementIv(iv);
97+
assertTrue(Bytes.equals(iv, new byte[] {2, 2, 3}));
98+
99+
Encryption.incrementIv(iv, 255);
100+
assertTrue(Bytes.equals(iv, new byte[] {1, 3, 3}));
101+
102+
Encryption.incrementIv(iv, 1024);
103+
assertTrue(Bytes.equals(iv, new byte[] {1, 7, 3}));
104+
105+
Encryption.incrementIv(iv_neg);
106+
assertTrue(Bytes.equals(iv_neg, new byte[] {-2, -13, 25}));
107+
108+
Encryption.incrementIv(iv_neg, 5);
109+
assertTrue(Bytes.equals(iv_neg, new byte[] {3, -12, 25}));
110+
}
111+
92112
private void checkTransformSymmetry(byte[] keyBytes, byte[] iv, byte[] plaintext)
93113
throws Exception {
94114
LOG.info("checkTransformSymmetry: AES, plaintext length = " + plaintext.length);

0 commit comments

Comments
 (0)