Encrypter and decrypter for segmented transports (untested).

This commit is contained in:
akwizgran
2012-01-13 11:54:55 +00:00
parent ab9b05448d
commit 90e54d94e6
17 changed files with 264 additions and 115 deletions

View File

@@ -55,7 +55,7 @@ public class CounterModeTest extends BriarTestCase {
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] ciphertext =
new byte[cipher.getOutputSize(plaintext.length)];
cipher.doFinal(plaintext, 0, plaintext.length, ciphertext, 0);
cipher.doFinal(plaintext, 0, plaintext.length, ciphertext);
ciphertexts.add(new Bytes(ciphertext));
}
// All the ciphertexts should be distinct using Arrays.equals()
@@ -78,7 +78,7 @@ public class CounterModeTest extends BriarTestCase {
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] ciphertext =
new byte[cipher.getOutputSize(plaintext.length)];
cipher.doFinal(plaintext, 0, plaintext.length, ciphertext, 0);
cipher.doFinal(plaintext, 0, plaintext.length, ciphertext);
ciphertexts.add(new Bytes(ciphertext));
}
assertEquals(1, ciphertexts.size());
@@ -98,7 +98,7 @@ public class CounterModeTest extends BriarTestCase {
Cipher cipher = Cipher.getInstance(CIPHER_MODE, PROVIDER);
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] ciphertext = new byte[cipher.getOutputSize(plaintext.length)];
cipher.doFinal(plaintext, 0, plaintext.length, ciphertext, 0);
cipher.doFinal(plaintext, 0, plaintext.length, ciphertext);
// Make sure the IV array hasn't been modified
assertEquals(0, ivBytes[BLOCK_SIZE_BYTES - 2]);
assertEquals(0, ivBytes[BLOCK_SIZE_BYTES - 1]);
@@ -109,7 +109,7 @@ public class CounterModeTest extends BriarTestCase {
cipher = Cipher.getInstance(CIPHER_MODE, PROVIDER);
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] ciphertext1 = new byte[cipher.getOutputSize(plaintext.length)];
cipher.doFinal(plaintext, 0, plaintext.length, ciphertext1, 0);
cipher.doFinal(plaintext, 0, plaintext.length, ciphertext1);
// The last nine blocks of the first ciphertext should be identical to
// the first nine blocks of the second ciphertext
for(int i = 0; i < BLOCK_SIZE_BYTES * 9; i++) {
@@ -132,7 +132,7 @@ public class CounterModeTest extends BriarTestCase {
Cipher cipher = Cipher.getInstance(CIPHER_MODE, PROVIDER);
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] ciphertext = new byte[cipher.getOutputSize(plaintext.length)];
cipher.doFinal(plaintext, 0, plaintext.length, ciphertext, 0);
cipher.doFinal(plaintext, 0, plaintext.length, ciphertext);
// Make sure the IV array hasn't been modified
assertEquals(0, ivBytes[BLOCK_SIZE_BYTES - 3]);
assertEquals((byte) 255, ivBytes[BLOCK_SIZE_BYTES - 2]);
@@ -146,7 +146,7 @@ public class CounterModeTest extends BriarTestCase {
cipher = Cipher.getInstance(CIPHER_MODE, PROVIDER);
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] ciphertext1 = new byte[cipher.getOutputSize(plaintext.length)];
cipher.doFinal(plaintext, 0, plaintext.length, ciphertext1, 0);
cipher.doFinal(plaintext, 0, plaintext.length, ciphertext1);
// The last nine blocks of the first ciphertext should be identical to
// the first nine blocks of the second ciphertext
for(int i = 0; i < BLOCK_SIZE_BYTES * 9; i++) {

View File

@@ -11,6 +11,7 @@ import javax.crypto.spec.IvParameterSpec;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.ErasableKey;
import net.sf.briar.api.plugins.FrameSource;
import net.sf.briar.crypto.CryptoModule;
import org.apache.commons.io.output.ByteArrayOutputStream;
@@ -67,16 +68,16 @@ public class ConnectionDecrypterImplTest extends BriarTestCase {
out.write(ciphertext1);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
// Use a ConnectionDecrypter to decrypt the ciphertext
ConnectionDecrypter d = new ConnectionDecrypterImpl(in, frameCipher,
FrameSource decrypter = new ConnectionDecrypter(in, frameCipher,
frameKey, MAC_LENGTH);
// First frame
byte[] decrypted = new byte[MAX_FRAME_LENGTH];
assertEquals(plaintext.length, d.readFrame(decrypted));
assertEquals(plaintext.length, decrypter.readFrame(decrypted));
for(int i = 0; i < plaintext.length; i++) {
assertEquals(plaintext[i], decrypted[i]);
}
// Second frame
assertEquals(plaintext1.length, d.readFrame(decrypted));
assertEquals(plaintext1.length, decrypter.readFrame(decrypted));
for(int i = 0; i < plaintext1.length; i++) {
assertEquals(plaintext1[i], decrypted[i]);
}

View File

@@ -8,6 +8,7 @@ import java.io.ByteArrayInputStream;
import net.sf.briar.TestUtils;
import net.sf.briar.api.FormatException;
import net.sf.briar.api.plugins.FrameSource;
import net.sf.briar.api.transport.ConnectionReader;
import org.apache.commons.io.output.ByteArrayOutputStream;
@@ -31,7 +32,7 @@ public class ConnectionReaderImplTest extends TransportTest {
mac.doFinal(frame, FRAME_HEADER_LENGTH + payloadLength);
// Read the frame
ByteArrayInputStream in = new ByteArrayInputStream(frame);
ConnectionDecrypter d = new NullConnectionDecrypter(in, macLength);
FrameSource d = new NullConnectionDecrypter(in, macLength);
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
// There should be no bytes available before EOF
assertEquals(-1, r.getInputStream().read());
@@ -49,7 +50,7 @@ public class ConnectionReaderImplTest extends TransportTest {
mac.doFinal(frame, FRAME_HEADER_LENGTH + payloadLength);
// Read the frame
ByteArrayInputStream in = new ByteArrayInputStream(frame);
ConnectionDecrypter d = new NullConnectionDecrypter(in, macLength);
FrameSource d = new NullConnectionDecrypter(in, macLength);
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
// There should be one byte available before EOF
assertEquals(0, r.getInputStream().read());
@@ -75,7 +76,7 @@ public class ConnectionReaderImplTest extends TransportTest {
out.write(frame1);
// Read the first frame
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
ConnectionDecrypter d = new NullConnectionDecrypter(in, macLength);
FrameSource d = new NullConnectionDecrypter(in, macLength);
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
byte[] read = new byte[maxPayloadLength];
TestUtils.readFully(r.getInputStream(), read);
@@ -109,7 +110,7 @@ public class ConnectionReaderImplTest extends TransportTest {
out.write(frame1);
// Read the first frame
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
ConnectionDecrypter d = new NullConnectionDecrypter(in, macLength);
FrameSource d = new NullConnectionDecrypter(in, macLength);
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
byte[] read = new byte[maxPayloadLength - paddingLength];
TestUtils.readFully(r.getInputStream(), read);
@@ -135,7 +136,7 @@ public class ConnectionReaderImplTest extends TransportTest {
mac.doFinal(frame, FRAME_HEADER_LENGTH + payloadLength + paddingLength);
// Read the frame
ByteArrayInputStream in = new ByteArrayInputStream(frame);
ConnectionDecrypter d = new NullConnectionDecrypter(in, macLength);
FrameSource d = new NullConnectionDecrypter(in, macLength);
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
// The non-zero padding should be rejected
try {
@@ -167,7 +168,7 @@ public class ConnectionReaderImplTest extends TransportTest {
out.write(frame1);
// Read the frames
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
ConnectionDecrypter d = new NullConnectionDecrypter(in, macLength);
FrameSource d = new NullConnectionDecrypter(in, macLength);
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
byte[] read = new byte[payloadLength];
TestUtils.readFully(r.getInputStream(), read);
@@ -191,7 +192,7 @@ public class ConnectionReaderImplTest extends TransportTest {
frame[12] ^= 1;
// Try to read the frame - not a single byte should be read
ByteArrayInputStream in = new ByteArrayInputStream(frame);
ConnectionDecrypter d = new NullConnectionDecrypter(in, macLength);
FrameSource d = new NullConnectionDecrypter(in, macLength);
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
try {
r.getInputStream().read();
@@ -213,7 +214,7 @@ public class ConnectionReaderImplTest extends TransportTest {
frame[17] ^= 1;
// Try to read the frame - not a single byte should be read
ByteArrayInputStream in = new ByteArrayInputStream(frame);
ConnectionDecrypter d = new NullConnectionDecrypter(in, macLength);
FrameSource d = new NullConnectionDecrypter(in, macLength);
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
try {
r.getInputStream().read();

View File

@@ -15,6 +15,7 @@ import javax.crypto.Mac;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.ErasableKey;
import net.sf.briar.api.plugins.FrameSource;
import net.sf.briar.api.transport.ConnectionReader;
import net.sf.briar.api.transport.ConnectionWriter;
import net.sf.briar.crypto.CryptoModule;
@@ -89,8 +90,8 @@ public class FrameReadWriteTest extends BriarTestCase {
assertArrayEquals(tag, recoveredTag);
assertTrue(TagEncoder.validateTag(tag, 0, tagCipher, tagKey));
// Read the frames back
ConnectionDecrypter decrypter = new ConnectionDecrypterImpl(in,
frameCipher, frameKey, mac.getMacLength());
FrameSource decrypter = new ConnectionDecrypter(in, frameCipher,
frameKey, mac.getMacLength());
ConnectionReader reader = new ConnectionReaderImpl(decrypter, mac,
macKey);
InputStream in1 = reader.getInputStream();

View File

@@ -8,9 +8,10 @@ import java.io.IOException;
import java.io.InputStream;
import net.sf.briar.api.FormatException;
import net.sf.briar.api.plugins.FrameSource;
/** A ConnectionDecrypter that performs no decryption. */
class NullConnectionDecrypter implements ConnectionDecrypter {
/** A connection decrypter that performs no decryption. */
class NullConnectionDecrypter implements FrameSource {
private final InputStream in;
private final int macLength;