Skip to content

Commit

Permalink
Merge branch 'polish-crypt-tests-that-fail-in-gradle-but-not-in-intel…
Browse files Browse the repository at this point in the history
…lij' into next
  • Loading branch information
ArneBab committed Feb 16, 2025
2 parents 52ecfcc + 595f292 commit ab8cf0a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 45 deletions.
22 changes: 11 additions & 11 deletions test/freenet/crypt/CTRBlockCipherTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package freenet.crypt;

import static org.junit.Assert.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
Expand Down Expand Up @@ -183,7 +183,7 @@ private void checkNIST(int bits, byte[] key, byte[] iv, byte[] plaintext,
Cipher c = Cipher.getInstance("AES/CTR/NOPADDING", Rijndael.AesCtrProvider);
c.init(Cipher.ENCRYPT_MODE, k, new IvParameterSpec(iv));
byte[] output = c.doFinal(plaintext);
assertTrue(Arrays.equals(output, ciphertext));
assertThat(output, equalTo(ciphertext));
}

Rijndael cipher = new Rijndael(bits, 128);
Expand All @@ -192,7 +192,7 @@ private void checkNIST(int bits, byte[] key, byte[] iv, byte[] plaintext,
ctr.init(iv);
byte[] output = new byte[plaintext.length];
ctr.processBytes(plaintext, 0, plaintext.length, output, 0);
assertTrue(Arrays.equals(output, ciphertext));
assertThat(output, equalTo(ciphertext));
}

private void checkNISTRandomLength(int bits, byte[] key, byte[] iv,
Expand Down Expand Up @@ -226,7 +226,7 @@ private void checkNISTRandomLength(int bits, byte[] key, byte[] iv,
}
c.doFinal(plaintext, 0, plaintext.length - inputPtr, output,
outputPtr);
assertTrue(Arrays.equals(output, ciphertext));
assertThat(output, equalTo(ciphertext));
}

Rijndael cipher = new Rijndael(bits, 128);
Expand All @@ -242,7 +242,7 @@ private void checkNISTRandomLength(int bits, byte[] key, byte[] iv,
ctr.processBytes(plaintext, ptr, count, output, ptr);
ptr += count;
}
assertTrue(Arrays.equals(output, ciphertext));
assertThat(output, equalTo(ciphertext));
}
}

Expand All @@ -263,7 +263,7 @@ public void testRandomJCA() throws NoSuchAlgorithmException, NoSuchPaddingExcept
c = Cipher.getInstance("AES/CTR/NOPADDING", Rijndael.AesCtrProvider);
c.init(Cipher.DECRYPT_MODE, k, new IvParameterSpec(iv));
byte[] decrypted = c.doFinal(output);
assertTrue(Arrays.equals(decrypted, plaintext));
assertThat(decrypted, equalTo(plaintext));
}
}

Expand All @@ -288,17 +288,17 @@ public void testRandom() throws UnsupportedCipherException, NoSuchAlgorithmExcep
ctr.init(iv);
byte[] finalPlaintext = new byte[plaintext.length];
ctr.processBytes(ciphertext, 0, ciphertext.length, finalPlaintext, 0);
assertTrue(Arrays.equals(finalPlaintext, plaintext));
assertThat(finalPlaintext, equalTo(plaintext));
if(TEST_JCA) {
SecretKeySpec k = new SecretKeySpec(key, "AES");
Cipher c = Cipher.getInstance("AES/CTR/NOPADDING", Rijndael.AesCtrProvider);
c.init(Cipher.ENCRYPT_MODE, k, new IvParameterSpec(iv));
byte[] output = c.doFinal(plaintext);
assertTrue(Arrays.equals(output, ciphertext));
assertThat(output, equalTo(ciphertext));
c = Cipher.getInstance("AES/CTR/NOPADDING", Rijndael.AesCtrProvider);
c.init(Cipher.DECRYPT_MODE, k, new IvParameterSpec(iv));
byte[] decrypted = c.doFinal(output);
assertTrue(Arrays.equals(decrypted, plaintext));
assertThat(decrypted, equalTo(plaintext));
}
// Now encrypt again, in random pieces.
cipher.initialize(key);
Expand All @@ -313,7 +313,7 @@ public void testRandom() throws UnsupportedCipherException, NoSuchAlgorithmExcep
ctr.processBytes(plaintext, ptr, count, output, ptr);
ptr += count;
}
assertTrue(Arrays.equals(output, ciphertext));
assertThat(output, equalTo(ciphertext));

}
}
Expand Down
68 changes: 34 additions & 34 deletions test/freenet/crypt/CryptByteBufferTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
* http://www.gnu.org/ for further details of the GPL. */
package freenet.crypt;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.nio.ByteBuffer;
Expand Down Expand Up @@ -95,11 +97,13 @@ public void testRoundOneByte() throws GeneralSecurityException {
byte[] buf = origPlaintext.clone();
for(int j=0;j<buf.length;j++) {
crypt2.encrypt(buf, j, 1);
assertEquals(buf[j], origCiphertext[j]);
String message = "Encrypted bytes in position " + j + " do not match.";
assertEquals(message, buf[j], origCiphertext[j]);
}
for(int j=0;j<buf.length;j++) {
crypt2.decrypt(buf, j, 1);
assertEquals(buf[j], origPlaintext[j]);
String message = "Decrypted bytes in position " + j + " do not match.";
assertEquals(message, buf[j], origPlaintext[j]);
}
}
}
Expand Down Expand Up @@ -160,10 +164,10 @@ public void testSuccessfulRoundTripInPlace() throws GeneralSecurityException {
byte[] buffer = Hex.decode(plainText[i]);
byte[] plaintextCopy = buffer.clone();
crypt.encrypt(buffer, 0, buffer.length);
assertTrue(!Arrays.equals(buffer, plaintextCopy));
assertThat(buffer, not(equalTo(plaintextCopy)));
crypt.decrypt(buffer, 0, buffer.length);
assertArrayEquals("CryptByteBufferType: "+type.name(),
plaintextCopy, buffer);
assertThat("CryptByteBufferType: "+type.name(),
plaintextCopy, equalTo(buffer));
}
}

Expand All @@ -185,7 +189,7 @@ public void testSuccessfulRoundTripInPlaceOffset() throws GeneralSecurityExcepti
byte[] copyBuffer = buffer.clone();
System.arraycopy(originalPlaintext, 0, buffer, header, originalPlaintext.length);
crypt.encrypt(buffer, footer, originalPlaintext.length);
assertTrue(!Arrays.equals(buffer, copyBuffer));
assertThat(buffer, not(equalTo(copyBuffer)));
crypt.decrypt(buffer, footer, originalPlaintext.length);
assertArrayEquals("CryptByteBufferType: "+type.name(),
originalPlaintext, Arrays.copyOfRange(buffer, footer, footer+originalPlaintext.length));
Expand Down Expand Up @@ -214,10 +218,10 @@ public void testSuccessfulRoundTripOutOfPlaceOffset() throws GeneralSecurityExce

byte[] outBuffer = new byte[outHeader + originalPlaintext.length + outFooter];
crypt.encrypt(buffer, inFooter, originalPlaintext.length, outBuffer, outHeader);
assertTrue(Arrays.equals(buffer, copyBuffer));
assertThat(buffer, equalTo(copyBuffer));
copyBuffer = outBuffer.clone();
crypt.decrypt(outBuffer, outHeader, originalPlaintext.length, buffer, inFooter);
assertTrue(Arrays.equals(copyBuffer, outBuffer));
assertThat(copyBuffer, equalTo(outBuffer));

assertArrayEquals("CryptByteBufferType: "+type.name(),
originalPlaintext, Arrays.copyOfRange(buffer, inFooter, inFooter+originalPlaintext.length));
Expand Down Expand Up @@ -251,23 +255,19 @@ public void testSuccessfulRoundTripByteArrayReset() throws GeneralSecurityExcept
// Once we have initialised the cipher, it is treated as a stream.
// Repeated encryption of the same data will return different ciphertext,
// as it is treated as a later point in the stream.
assertNotEquals(ciphertext1, ciphertext2);
assertNotEquals(ciphertext1, ciphertext3);
assertNotEquals(ciphertext2, ciphertext3);
assertThat(ciphertext1, not(equalTo(ciphertext2)));
assertThat(ciphertext1, not(equalTo(ciphertext3)));
assertThat(ciphertext2, not(equalTo(ciphertext3)));
}

ByteBuffer decipheredtext1 = crypt.decryptCopy(ciphertext1);
ByteBuffer decipheredtext2 = crypt.decryptCopy(ciphertext2);
ByteBuffer decipheredtext3 = crypt.decryptCopy(ciphertext3);
assertTrue("CryptByteBufferType: "+type.name(), plain.equals(decipheredtext1));
assertTrue("CryptByteBufferType: "+type.name(), plain.equals(decipheredtext2));
assertTrue("CryptByteBufferType: "+type.name(), plain.equals(decipheredtext3));
assertThat("CryptByteBufferType: "+type.name(), plain, equalTo(decipheredtext1));
assertThat("CryptByteBufferType: "+type.name(), plain, equalTo(decipheredtext2));
assertThat("CryptByteBufferType: "+type.name(), plain, equalTo(decipheredtext3));
}
}

private void assertNotEquals(Object o1, Object o2) {
assertFalse(o1.equals(o2));
}

@Test
public void testEncryptWrapByteBuffer() throws GeneralSecurityException {
Expand All @@ -289,18 +289,18 @@ public void testEncryptWrapByteBuffer() throws GeneralSecurityException {
}
ByteBuffer plaintext = ByteBuffer.wrap(buf, header, origPlaintext.length);
ByteBuffer ciphertext = crypt.encryptCopy(plaintext);
assertTrue(Arrays.equals(buf, cloneBuf)); // Plaintext not modified.
assertThat(buf, equalTo(cloneBuf)); // Plaintext not modified.
assertEquals(ciphertext.remaining(), origPlaintext.length);
byte[] altCiphertext = crypt2.encryptCopy(origPlaintext);
byte[] ciphertextBytes = new byte[origPlaintext.length];
ciphertext.get(ciphertextBytes);
ciphertext.position(0);
assertTrue(Arrays.equals(altCiphertext, ciphertextBytes));
assertThat(altCiphertext, equalTo(ciphertextBytes));
ByteBuffer deciphered = crypt.decryptCopy(ciphertext);
assertTrue(deciphered.equals(plaintext));
assertThat(deciphered, equalTo(plaintext));
byte[] data = new byte[origPlaintext.length];
deciphered.get(data);
assertTrue(Arrays.equals(data, origPlaintext));
assertThat(data, equalTo(origPlaintext));
}
}

Expand All @@ -327,14 +327,14 @@ public void testEncryptByteBufferToByteBuffer() throws GeneralSecurityException
crypt.encrypt(plaintext, ciphertext);
assertEquals(plaintext.position(), header+origPlaintext.length);
assertEquals(ciphertext.position(), header+origPlaintext.length);
assertTrue(Arrays.equals(buf, cloneBuf)); // Plaintext not modified.
assertThat(buf, equalTo(cloneBuf)); // Plaintext not modified.
plaintext.position(header);
ciphertext.position(header);
assertTrue(!Arrays.equals(ciphertextBuf, copyCiphertextBuf));
assertThat(ciphertextBuf, not(equalTo(copyCiphertextBuf)));
Arrays.fill(buf, (byte)0);
assertFalse(Arrays.equals(buf, cloneBuf));
assertThat(buf, not(equalTo(cloneBuf)));
crypt.decrypt(ciphertext, plaintext);
assertTrue(Arrays.equals(buf, cloneBuf));
assertThat(buf, equalTo(cloneBuf));
}
}

Expand Down Expand Up @@ -389,18 +389,18 @@ public void testDecryptWrapByteBuffer() throws GeneralSecurityException {
}
ByteBuffer plaintext = ByteBuffer.wrap(buf);
ByteBuffer ciphertext = crypt.encryptCopy(plaintext);
assertTrue(Arrays.equals(buf, origPlaintext)); // Plaintext not modified.
assertThat(buf, equalTo(origPlaintext)); // Plaintext not modified.
assertEquals(ciphertext.remaining(), origPlaintext.length);
byte[] decryptBuf = new byte[header+origPlaintext.length+footer];
ciphertext.get(decryptBuf, header, origPlaintext.length);
byte[] copyOfDecryptBuf = decryptBuf.clone();
ByteBuffer toDecipher = ByteBuffer.wrap(decryptBuf, header, origPlaintext.length);
ByteBuffer deciphered = crypt.decryptCopy(toDecipher);
assertTrue(Arrays.equals(decryptBuf, copyOfDecryptBuf));
assertTrue(deciphered.equals(plaintext));
assertThat(decryptBuf, equalTo(copyOfDecryptBuf));
assertThat(deciphered, equalTo(plaintext));
byte[] data = new byte[origPlaintext.length];
deciphered.get(data);
assertTrue(Arrays.equals(data, origPlaintext));
assertThat(data, equalTo(origPlaintext));
}
}

Expand All @@ -423,14 +423,14 @@ public void testEncryptDirectByteBuffer() throws GeneralSecurityException {
plaintext.clear();
byte[] copyPlaintext = new byte[origPlaintext.length];
plaintext.get(copyPlaintext);
assertTrue(Arrays.equals(origPlaintext, copyPlaintext)); // Plaintext not modified.
assertThat(origPlaintext, equalTo(copyPlaintext)); // Plaintext not modified.
plaintext.clear();
assertEquals(ciphertext.remaining(), origPlaintext.length);
ByteBuffer deciphered = crypt.decryptCopy(ciphertext);
assertTrue(deciphered.equals(plaintext));
assertThat(deciphered, equalTo(plaintext));
byte[] data = new byte[origPlaintext.length];
deciphered.get(data);
assertTrue(Arrays.equals(data, origPlaintext));
assertThat(data, equalTo(origPlaintext));
}
}

Expand Down

0 comments on commit ab8cf0a

Please sign in to comment.