Skip to content
This repository was archived by the owner on Apr 17, 2024. It is now read-only.

Commit fbd2fe5

Browse files
Merge pull request #287 from rayman42003:master
PiperOrigin-RevId: 282425118
2 parents 7074fe8 + 35da19f commit fbd2fe5

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

java/src/main/java/com/google/crypto/tink/subtle/AesSiv.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,13 @@ private byte[] s2v(final byte[]... s) throws GeneralSecurityException {
7676

7777
byte[] result = cmacForS2V.computeMac(BLOCK_ZERO);
7878
for (int i = 0; i < s.length - 1; i++) {
79-
result = Bytes.xor(AesUtil.dbl(result), cmacForS2V.computeMac(s[i]));
79+
final byte[] currBlock;
80+
if (s[i] == null) {
81+
currBlock = new byte[0];
82+
} else {
83+
currBlock = s[i];
84+
}
85+
result = Bytes.xor(AesUtil.dbl(result), cmacForS2V.computeMac(currBlock));
8086
}
8187
byte[] lastBlock = s[s.length - 1];
8288
if (lastBlock.length >= 16) {

java/src/test/java/com/google/crypto/tink/subtle/AesSivTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,56 @@ public void testEncryptDecryptWithEmptyPlaintextAndEmptyAssociatedData()
143143
}
144144
}
145145

146+
@Test
147+
public void testEncryptDecryptWithNullAssociatedData() throws GeneralSecurityException {
148+
for (int keySize : keySizeInBytes) {
149+
DeterministicAead dead = new AesSiv(Random.randBytes(keySize));
150+
for (int triesPlaintext = 0; triesPlaintext < 100; triesPlaintext++) {
151+
byte[] plaintext = Random.randBytes(Random.randInt(1024) + 1);
152+
byte[] rebuiltPlaintext =
153+
dead.decryptDeterministically(dead.encryptDeterministically(plaintext, null), null);
154+
assertEquals(Hex.encode(plaintext), Hex.encode(rebuiltPlaintext));
155+
}
156+
}
157+
}
158+
159+
@Test
160+
public void testEncryptDecryptWithNullAndEmptyAssociatedDataEquivalent()
161+
throws GeneralSecurityException {
162+
for (int keySize : keySizeInBytes) {
163+
DeterministicAead dead = new AesSiv(Random.randBytes(keySize));
164+
for (int triesPlaintext = 0; triesPlaintext < 100; triesPlaintext++) {
165+
byte[] plaintext = Random.randBytes(Random.randInt(1024) + 1);
166+
byte[] emptyAad = new byte[0];
167+
byte[] emptyAadCiphertext = dead.encryptDeterministically(plaintext, emptyAad);
168+
byte[] emptyAadRebuiltPlaintext =
169+
dead.decryptDeterministically(emptyAadCiphertext, emptyAad);
170+
171+
byte[] nullAadCipherText = dead.encryptDeterministically(plaintext, null);
172+
byte[] nullAadRebuiltPlaintext =
173+
dead.decryptDeterministically(nullAadCipherText, null);
174+
175+
assertEquals(Hex.encode(plaintext), Hex.encode(emptyAadRebuiltPlaintext));
176+
assertEquals(Hex.encode(plaintext), Hex.encode(nullAadRebuiltPlaintext));
177+
assertEquals(Hex.encode(emptyAadCiphertext), Hex.encode(nullAadCipherText));
178+
}
179+
}
180+
}
181+
182+
@Test
183+
public void testEncryptDecryptWithEmptyPlaintextAndNullAssociatedData()
184+
throws GeneralSecurityException {
185+
for (int keySize : keySizeInBytes) {
186+
DeterministicAead dead = new AesSiv(Random.randBytes(keySize));
187+
for (int triesPlaintext = 0; triesPlaintext < 100; triesPlaintext++) {
188+
byte[] plaintext = new byte[0];
189+
byte[] rebuiltPlaintext =
190+
dead.decryptDeterministically(dead.encryptDeterministically(plaintext, null), null);
191+
assertEquals(Hex.encode(plaintext), Hex.encode(rebuiltPlaintext));
192+
}
193+
}
194+
}
195+
146196
@Test
147197
public void testEncryptDecrypt() throws GeneralSecurityException {
148198
for (int keySize : keySizeInBytes) {

0 commit comments

Comments
 (0)