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

Commit

Permalink
Merge pull request #287 from rayman42003:master
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 282425118
  • Loading branch information
copybara-github committed Nov 25, 2019
2 parents 7074fe8 + 35da19f commit fbd2fe5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
8 changes: 7 additions & 1 deletion java/src/main/java/com/google/crypto/tink/subtle/AesSiv.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,13 @@ private byte[] s2v(final byte[]... s) throws GeneralSecurityException {

byte[] result = cmacForS2V.computeMac(BLOCK_ZERO);
for (int i = 0; i < s.length - 1; i++) {
result = Bytes.xor(AesUtil.dbl(result), cmacForS2V.computeMac(s[i]));
final byte[] currBlock;
if (s[i] == null) {
currBlock = new byte[0];
} else {
currBlock = s[i];
}
result = Bytes.xor(AesUtil.dbl(result), cmacForS2V.computeMac(currBlock));
}
byte[] lastBlock = s[s.length - 1];
if (lastBlock.length >= 16) {
Expand Down
50 changes: 50 additions & 0 deletions java/src/test/java/com/google/crypto/tink/subtle/AesSivTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,56 @@ public void testEncryptDecryptWithEmptyPlaintextAndEmptyAssociatedData()
}
}

@Test
public void testEncryptDecryptWithNullAssociatedData() throws GeneralSecurityException {
for (int keySize : keySizeInBytes) {
DeterministicAead dead = new AesSiv(Random.randBytes(keySize));
for (int triesPlaintext = 0; triesPlaintext < 100; triesPlaintext++) {
byte[] plaintext = Random.randBytes(Random.randInt(1024) + 1);
byte[] rebuiltPlaintext =
dead.decryptDeterministically(dead.encryptDeterministically(plaintext, null), null);
assertEquals(Hex.encode(plaintext), Hex.encode(rebuiltPlaintext));
}
}
}

@Test
public void testEncryptDecryptWithNullAndEmptyAssociatedDataEquivalent()
throws GeneralSecurityException {
for (int keySize : keySizeInBytes) {
DeterministicAead dead = new AesSiv(Random.randBytes(keySize));
for (int triesPlaintext = 0; triesPlaintext < 100; triesPlaintext++) {
byte[] plaintext = Random.randBytes(Random.randInt(1024) + 1);
byte[] emptyAad = new byte[0];
byte[] emptyAadCiphertext = dead.encryptDeterministically(plaintext, emptyAad);
byte[] emptyAadRebuiltPlaintext =
dead.decryptDeterministically(emptyAadCiphertext, emptyAad);

byte[] nullAadCipherText = dead.encryptDeterministically(plaintext, null);
byte[] nullAadRebuiltPlaintext =
dead.decryptDeterministically(nullAadCipherText, null);

assertEquals(Hex.encode(plaintext), Hex.encode(emptyAadRebuiltPlaintext));
assertEquals(Hex.encode(plaintext), Hex.encode(nullAadRebuiltPlaintext));
assertEquals(Hex.encode(emptyAadCiphertext), Hex.encode(nullAadCipherText));
}
}
}

@Test
public void testEncryptDecryptWithEmptyPlaintextAndNullAssociatedData()
throws GeneralSecurityException {
for (int keySize : keySizeInBytes) {
DeterministicAead dead = new AesSiv(Random.randBytes(keySize));
for (int triesPlaintext = 0; triesPlaintext < 100; triesPlaintext++) {
byte[] plaintext = new byte[0];
byte[] rebuiltPlaintext =
dead.decryptDeterministically(dead.encryptDeterministically(plaintext, null), null);
assertEquals(Hex.encode(plaintext), Hex.encode(rebuiltPlaintext));
}
}
}

@Test
public void testEncryptDecrypt() throws GeneralSecurityException {
for (int keySize : keySizeInBytes) {
Expand Down

0 comments on commit fbd2fe5

Please sign in to comment.