mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-20 14:49:53 +01:00
Initial support for tagging every segment (untested).
This commit is contained in:
@@ -5,15 +5,15 @@ import java.io.InputStream;
|
|||||||
public interface ConnectionReaderFactory {
|
public interface ConnectionReaderFactory {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a connection reader for a batch-mode connection or the
|
* Creates a connection reader for a simplex connection or the initiator's
|
||||||
* initiator's side of a stream-mode connection. The secret is erased
|
* side of a duplex connection. The secret is erased before this method
|
||||||
* before this method returns.
|
* returns.
|
||||||
*/
|
*/
|
||||||
ConnectionReader createConnectionReader(InputStream in, byte[] secret,
|
ConnectionReader createConnectionReader(InputStream in, byte[] secret,
|
||||||
byte[] tag);
|
byte[] tag);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a connection reader for the responder's side of a stream-mode
|
* Creates a connection reader for the responder's side of a duplex
|
||||||
* connection. The secret is erased before this method returns.
|
* connection. The secret is erased before this method returns.
|
||||||
*/
|
*/
|
||||||
ConnectionReader createConnectionReader(InputStream in, byte[] secret);
|
ConnectionReader createConnectionReader(InputStream in, byte[] secret);
|
||||||
|
|||||||
@@ -5,15 +5,15 @@ import java.io.OutputStream;
|
|||||||
public interface ConnectionWriterFactory {
|
public interface ConnectionWriterFactory {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a connection writer for a batch-mode connection or the
|
* Creates a connection writer for a simplex connection or the initiator's
|
||||||
* initiator's side of a stream-mode connection. The secret is erased
|
* side of a duplex connection. The secret is erased before this method
|
||||||
* before this method returns.
|
* returns.
|
||||||
*/
|
*/
|
||||||
ConnectionWriter createConnectionWriter(OutputStream out, long capacity,
|
ConnectionWriter createConnectionWriter(OutputStream out, long capacity,
|
||||||
byte[] secret);
|
byte[] secret);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a connection writer for the responder's side of a stream-mode
|
* Creates a connection writer for the responder's side of a duplex
|
||||||
* connection. The secret is erased before this method returns.
|
* connection. The secret is erased before this method returns.
|
||||||
*/
|
*/
|
||||||
ConnectionWriter createConnectionWriter(OutputStream out, long capacity,
|
ConnectionWriter createConnectionWriter(OutputStream out, long capacity,
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package net.sf.briar.transport;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
|
||||||
/** Encrypts authenticated data to be sent over a connection. */
|
/** Encrypts authenticated data to be sent over a connection. */
|
||||||
interface ConnectionEncrypter extends FrameSink {
|
interface ConnectionEncrypter extends FrameSink {
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package net.sf.briar.transport;
|
package net.sf.briar.transport;
|
||||||
|
|
||||||
|
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||||
import static net.sf.briar.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
import static net.sf.briar.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -14,54 +15,52 @@ import net.sf.briar.api.crypto.ErasableKey;
|
|||||||
class ConnectionEncrypterImpl implements ConnectionEncrypter {
|
class ConnectionEncrypterImpl implements ConnectionEncrypter {
|
||||||
|
|
||||||
private final OutputStream out;
|
private final OutputStream out;
|
||||||
private final Cipher frameCipher;
|
private final Cipher tagCipher, frameCipher;
|
||||||
private final ErasableKey frameKey;
|
private final ErasableKey tagKey, frameKey;
|
||||||
|
private final boolean tagEverySegment;
|
||||||
private final byte[] iv, tag;
|
private final byte[] iv, tag;
|
||||||
|
|
||||||
private long capacity, frame = 0L;
|
private long capacity, frame = 0;
|
||||||
private boolean tagWritten = false;
|
|
||||||
|
|
||||||
ConnectionEncrypterImpl(OutputStream out, long capacity, Cipher tagCipher,
|
ConnectionEncrypterImpl(OutputStream out, long capacity, Cipher tagCipher,
|
||||||
Cipher frameCipher, ErasableKey tagKey, ErasableKey frameKey) {
|
Cipher frameCipher, ErasableKey tagKey, ErasableKey frameKey,
|
||||||
|
boolean tagEverySegment) {
|
||||||
this.out = out;
|
this.out = out;
|
||||||
this.capacity = capacity;
|
this.capacity = capacity;
|
||||||
|
this.tagCipher = tagCipher;
|
||||||
this.frameCipher = frameCipher;
|
this.frameCipher = frameCipher;
|
||||||
|
this.tagKey = tagKey;
|
||||||
this.frameKey = frameKey;
|
this.frameKey = frameKey;
|
||||||
|
this.tagEverySegment = tagEverySegment;
|
||||||
iv = IvEncoder.encodeIv(0, frameCipher.getBlockSize());
|
iv = IvEncoder.encodeIv(0, frameCipher.getBlockSize());
|
||||||
// Encrypt the tag
|
tag = new byte[TAG_LENGTH];
|
||||||
tag = TagEncoder.encodeTag(0, tagCipher, tagKey);
|
|
||||||
tagKey.erase();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeFrame(byte[] b, int len) throws IOException {
|
public void writeFrame(byte[] b, int len) throws IOException {
|
||||||
if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
|
if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
|
||||||
if(!tagWritten) {
|
try {
|
||||||
try {
|
if(tagEverySegment || frame == 0) {
|
||||||
|
TagEncoder.encodeTag(tag, frame, tagCipher, tagKey);
|
||||||
out.write(tag);
|
out.write(tag);
|
||||||
} catch(IOException e) {
|
capacity -= tag.length;
|
||||||
frameKey.erase();
|
}
|
||||||
throw e;
|
IvEncoder.updateIv(iv, frame);
|
||||||
|
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||||
|
try {
|
||||||
|
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||||
|
int encrypted = frameCipher.doFinal(b, 0, len, b);
|
||||||
|
if(encrypted != len) throw new RuntimeException();
|
||||||
|
} catch(GeneralSecurityException badCipher) {
|
||||||
|
throw new RuntimeException(badCipher);
|
||||||
}
|
}
|
||||||
capacity -= tag.length;
|
|
||||||
tagWritten = true;
|
|
||||||
}
|
|
||||||
IvEncoder.updateIv(iv, frame);
|
|
||||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
|
||||||
try {
|
|
||||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
|
||||||
int encrypted = frameCipher.doFinal(b, 0, len, b);
|
|
||||||
if(encrypted != len) throw new RuntimeException();
|
|
||||||
} catch(GeneralSecurityException badCipher) {
|
|
||||||
throw new RuntimeException(badCipher);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
out.write(b, 0, len);
|
out.write(b, 0, len);
|
||||||
|
capacity -= len;
|
||||||
|
frame++;
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
frameKey.erase();
|
frameKey.erase();
|
||||||
|
tagKey.erase();
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
capacity -= len;
|
|
||||||
frame++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void flush() throws IOException {
|
public void flush() throws IOException {
|
||||||
|
|||||||
@@ -33,8 +33,8 @@ import net.sf.briar.api.protocol.TransportId;
|
|||||||
import net.sf.briar.api.protocol.TransportIndex;
|
import net.sf.briar.api.protocol.TransportIndex;
|
||||||
import net.sf.briar.api.transport.ConnectionContext;
|
import net.sf.briar.api.transport.ConnectionContext;
|
||||||
import net.sf.briar.api.transport.ConnectionRecogniser;
|
import net.sf.briar.api.transport.ConnectionRecogniser;
|
||||||
import net.sf.briar.api.transport.IncomingConnectionExecutor;
|
|
||||||
import net.sf.briar.api.transport.ConnectionWindow;
|
import net.sf.briar.api.transport.ConnectionWindow;
|
||||||
|
import net.sf.briar.api.transport.IncomingConnectionExecutor;
|
||||||
import net.sf.briar.util.ByteUtils;
|
import net.sf.briar.util.ByteUtils;
|
||||||
|
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
@@ -102,7 +102,8 @@ DatabaseListener {
|
|||||||
// Locking: this
|
// Locking: this
|
||||||
private Bytes calculateTag(Context ctx, byte[] secret) {
|
private Bytes calculateTag(Context ctx, byte[] secret) {
|
||||||
ErasableKey tagKey = crypto.deriveTagKey(secret, true);
|
ErasableKey tagKey = crypto.deriveTagKey(secret, true);
|
||||||
byte[] tag = TagEncoder.encodeTag(0, tagCipher, tagKey);
|
byte[] tag = new byte[TAG_LENGTH];
|
||||||
|
TagEncoder.encodeTag(tag, 0, tagCipher, tagKey);
|
||||||
tagKey.erase();
|
tagKey.erase();
|
||||||
return new Bytes(tag);
|
return new Bytes(tag);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ class ConnectionWriterFactoryImpl implements ConnectionWriterFactory {
|
|||||||
Cipher tagCipher = crypto.getTagCipher();
|
Cipher tagCipher = crypto.getTagCipher();
|
||||||
Cipher frameCipher = crypto.getFrameCipher();
|
Cipher frameCipher = crypto.getFrameCipher();
|
||||||
ConnectionEncrypter encrypter = new ConnectionEncrypterImpl(out,
|
ConnectionEncrypter encrypter = new ConnectionEncrypterImpl(out,
|
||||||
capacity, tagCipher, frameCipher, tagKey, frameKey);
|
capacity, tagCipher, frameCipher, tagKey, frameKey, false);
|
||||||
// Create the writer
|
// Create the writer
|
||||||
Mac mac = crypto.getMac();
|
Mac mac = crypto.getMac();
|
||||||
return new ConnectionWriterImpl(encrypter, mac, macKey);
|
return new ConnectionWriterImpl(encrypter, mac, macKey);
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
package net.sf.briar.transport;
|
package net.sf.briar.transport;
|
||||||
|
|
||||||
import static net.sf.briar.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
|
||||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||||
|
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||||
|
import static net.sf.briar.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.security.GeneralSecurityException;
|
import java.security.GeneralSecurityException;
|
||||||
@@ -16,38 +17,37 @@ import net.sf.briar.api.plugins.SegmentSink;
|
|||||||
class SegmentedConnectionEncrypter implements ConnectionEncrypter {
|
class SegmentedConnectionEncrypter implements ConnectionEncrypter {
|
||||||
|
|
||||||
private final SegmentSink out;
|
private final SegmentSink out;
|
||||||
private final Cipher frameCipher;
|
private final Cipher tagCipher, frameCipher;
|
||||||
private final ErasableKey frameKey;
|
private final ErasableKey tagKey, frameKey;
|
||||||
private final byte[] iv, tag;
|
private final boolean tagEverySegment;
|
||||||
|
private final byte[] iv;
|
||||||
private final Segment segment;
|
private final Segment segment;
|
||||||
|
|
||||||
private long capacity, frame = 0L;
|
private long capacity, frame = 0;
|
||||||
private boolean tagWritten = false;
|
|
||||||
|
|
||||||
SegmentedConnectionEncrypter(SegmentSink out, long capacity,
|
SegmentedConnectionEncrypter(SegmentSink out, long capacity,
|
||||||
Cipher tagCipher, Cipher frameCipher, ErasableKey tagKey,
|
Cipher tagCipher, Cipher frameCipher, ErasableKey tagKey,
|
||||||
ErasableKey frameKey) {
|
ErasableKey frameKey, boolean tagEverySegment) {
|
||||||
this.out = out;
|
this.out = out;
|
||||||
this.capacity = capacity;
|
this.capacity = capacity;
|
||||||
|
this.tagCipher = tagCipher;
|
||||||
this.frameCipher = frameCipher;
|
this.frameCipher = frameCipher;
|
||||||
|
this.tagKey = tagKey;
|
||||||
this.frameKey = frameKey;
|
this.frameKey = frameKey;
|
||||||
|
this.tagEverySegment = tagEverySegment;
|
||||||
iv = IvEncoder.encodeIv(0, frameCipher.getBlockSize());
|
iv = IvEncoder.encodeIv(0, frameCipher.getBlockSize());
|
||||||
// Encrypt the tag
|
|
||||||
tag = TagEncoder.encodeTag(0, tagCipher, tagKey);
|
|
||||||
tagKey.erase();
|
|
||||||
segment = new SegmentImpl();
|
segment = new SegmentImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeFrame(byte[] b, int len) throws IOException {
|
public void writeFrame(byte[] b, int len) throws IOException {
|
||||||
if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
|
if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
if(!tagWritten) {
|
if(tagEverySegment || frame == 0) {
|
||||||
if(tag.length + len > MAX_FRAME_LENGTH)
|
if(len + TAG_LENGTH > MAX_FRAME_LENGTH)
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
System.arraycopy(tag, 0, segment.getBuffer(), 0, tag.length);
|
TagEncoder.encodeTag(segment.getBuffer(), frame, tagCipher, tagKey);
|
||||||
capacity -= tag.length;
|
offset = TAG_LENGTH;
|
||||||
tagWritten = true;
|
capacity -= TAG_LENGTH;
|
||||||
offset = tag.length;
|
|
||||||
}
|
}
|
||||||
IvEncoder.updateIv(iv, frame);
|
IvEncoder.updateIv(iv, frame);
|
||||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||||
@@ -64,6 +64,7 @@ class SegmentedConnectionEncrypter implements ConnectionEncrypter {
|
|||||||
out.writeSegment(segment);
|
out.writeSegment(segment);
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
frameKey.erase();
|
frameKey.erase();
|
||||||
|
tagKey.erase();
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
capacity -= len;
|
capacity -= len;
|
||||||
|
|||||||
@@ -1,29 +1,34 @@
|
|||||||
package net.sf.briar.transport;
|
package net.sf.briar.transport;
|
||||||
|
|
||||||
|
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||||
|
import static net.sf.briar.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
||||||
|
|
||||||
import java.security.GeneralSecurityException;
|
import java.security.GeneralSecurityException;
|
||||||
|
|
||||||
import javax.crypto.Cipher;
|
import javax.crypto.Cipher;
|
||||||
import javax.crypto.spec.IvParameterSpec;
|
import javax.crypto.spec.IvParameterSpec;
|
||||||
|
|
||||||
import net.sf.briar.api.crypto.ErasableKey;
|
import net.sf.briar.api.crypto.ErasableKey;
|
||||||
import net.sf.briar.api.transport.TransportConstants;
|
|
||||||
import net.sf.briar.util.ByteUtils;
|
import net.sf.briar.util.ByteUtils;
|
||||||
|
|
||||||
class TagEncoder {
|
class TagEncoder {
|
||||||
|
|
||||||
static byte[] encodeTag(long frame, Cipher tagCipher, ErasableKey tagKey) {
|
private static final byte[] BLANK = new byte[TAG_LENGTH];
|
||||||
if(frame < 0 || frame > ByteUtils.MAX_32_BIT_UNSIGNED)
|
|
||||||
|
static void encodeTag(byte[] tag, long frame, Cipher tagCipher,
|
||||||
|
ErasableKey tagKey) {
|
||||||
|
if(tag.length < TAG_LENGTH) throw new IllegalArgumentException();
|
||||||
|
if(frame < 0 || frame > MAX_32_BIT_UNSIGNED)
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
// The plaintext is blank
|
|
||||||
byte[] plaintext = new byte[TransportConstants.TAG_LENGTH];
|
|
||||||
// Encode the frame number as a uint32 at the end of the IV
|
// Encode the frame number as a uint32 at the end of the IV
|
||||||
byte[] iv = new byte[tagCipher.getBlockSize()];
|
byte[] iv = new byte[tagCipher.getBlockSize()];
|
||||||
if(iv.length != plaintext.length) throw new IllegalArgumentException();
|
if(iv.length != TAG_LENGTH) throw new IllegalArgumentException();
|
||||||
ByteUtils.writeUint32(frame, iv, iv.length - 4);
|
ByteUtils.writeUint32(frame, iv, iv.length - 4);
|
||||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||||
try {
|
try {
|
||||||
tagCipher.init(Cipher.ENCRYPT_MODE, tagKey, ivSpec);
|
tagCipher.init(Cipher.ENCRYPT_MODE, tagKey, ivSpec);
|
||||||
return tagCipher.doFinal(plaintext);
|
int encrypted = tagCipher.doFinal(BLANK, 0, TAG_LENGTH, tag);
|
||||||
|
if(encrypted != TAG_LENGTH) throw new IllegalArgumentException();
|
||||||
} catch(GeneralSecurityException e) {
|
} catch(GeneralSecurityException e) {
|
||||||
// Unsuitable cipher or key
|
// Unsuitable cipher or key
|
||||||
throw new IllegalArgumentException(e);
|
throw new IllegalArgumentException(e);
|
||||||
@@ -32,9 +37,9 @@ class TagEncoder {
|
|||||||
|
|
||||||
static boolean validateTag(byte[] tag, long frame, Cipher tagCipher,
|
static boolean validateTag(byte[] tag, long frame, Cipher tagCipher,
|
||||||
ErasableKey tagKey) {
|
ErasableKey tagKey) {
|
||||||
if(frame < 0 || frame > ByteUtils.MAX_32_BIT_UNSIGNED)
|
if(frame < 0 || frame > MAX_32_BIT_UNSIGNED)
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
if(tag.length != TransportConstants.TAG_LENGTH) return false;
|
if(tag.length != TAG_LENGTH) return false;
|
||||||
// Encode the frame number as a uint32 at the end of the IV
|
// Encode the frame number as a uint32 at the end of the IV
|
||||||
byte[] iv = new byte[tagCipher.getBlockSize()];
|
byte[] iv = new byte[tagCipher.getBlockSize()];
|
||||||
if(iv.length != tag.length) throw new IllegalArgumentException();
|
if(iv.length != tag.length) throw new IllegalArgumentException();
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package net.sf.briar.transport;
|
package net.sf.briar.transport;
|
||||||
|
|
||||||
|
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||||
import static org.junit.Assert.assertArrayEquals;
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
@@ -37,7 +38,8 @@ public class ConnectionEncrypterImplTest extends BriarTestCase {
|
|||||||
@Test
|
@Test
|
||||||
public void testEncryption() throws Exception {
|
public void testEncryption() throws Exception {
|
||||||
// Calculate the expected tag
|
// Calculate the expected tag
|
||||||
byte[] tag = TagEncoder.encodeTag(0, tagCipher, tagKey);
|
byte[] tag = new byte[TAG_LENGTH];
|
||||||
|
TagEncoder.encodeTag(tag, 0, tagCipher, tagKey);
|
||||||
// Calculate the expected ciphertext for the first frame
|
// Calculate the expected ciphertext for the first frame
|
||||||
byte[] iv = new byte[frameCipher.getBlockSize()];
|
byte[] iv = new byte[frameCipher.getBlockSize()];
|
||||||
byte[] plaintext = new byte[123 + MAC_LENGTH];
|
byte[] plaintext = new byte[123 + MAC_LENGTH];
|
||||||
@@ -59,7 +61,7 @@ public class ConnectionEncrypterImplTest extends BriarTestCase {
|
|||||||
// Use a ConnectionEncrypter to encrypt the plaintext
|
// Use a ConnectionEncrypter to encrypt the plaintext
|
||||||
out.reset();
|
out.reset();
|
||||||
ConnectionEncrypter e = new ConnectionEncrypterImpl(out, Long.MAX_VALUE,
|
ConnectionEncrypter e = new ConnectionEncrypterImpl(out, Long.MAX_VALUE,
|
||||||
tagCipher, frameCipher, tagKey, frameKey);
|
tagCipher, frameCipher, tagKey, frameKey, false);
|
||||||
e.writeFrame(plaintext, plaintext.length);
|
e.writeFrame(plaintext, plaintext.length);
|
||||||
e.writeFrame(plaintext1, plaintext1.length);
|
e.writeFrame(plaintext1, plaintext1.length);
|
||||||
byte[] actual = out.toByteArray();
|
byte[] actual = out.toByteArray();
|
||||||
|
|||||||
@@ -617,6 +617,8 @@ public class ConnectionRecogniserImplTest extends BriarTestCase {
|
|||||||
// Calculate the expected tag for connection number 3
|
// Calculate the expected tag for connection number 3
|
||||||
ErasableKey tagKey = crypto.deriveTagKey(secret, true);
|
ErasableKey tagKey = crypto.deriveTagKey(secret, true);
|
||||||
Cipher tagCipher = crypto.getTagCipher();
|
Cipher tagCipher = crypto.getTagCipher();
|
||||||
return TagEncoder.encodeTag(0, tagCipher, tagKey);
|
byte[] tag = new byte[TAG_LENGTH];
|
||||||
|
TagEncoder.encodeTag(tag, 0, tagCipher, tagKey);
|
||||||
|
return tag;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,7 +61,8 @@ public class FrameReadWriteTest extends BriarTestCase {
|
|||||||
|
|
||||||
private void testWriteAndRead(boolean initiator) throws Exception {
|
private void testWriteAndRead(boolean initiator) throws Exception {
|
||||||
// Encode the tag
|
// Encode the tag
|
||||||
byte[] tag = TagEncoder.encodeTag(0, tagCipher, tagKey);
|
byte[] tag = new byte[TAG_LENGTH];
|
||||||
|
TagEncoder.encodeTag(tag, 0, tagCipher, tagKey);
|
||||||
// Generate two random frames
|
// Generate two random frames
|
||||||
byte[] frame = new byte[12345];
|
byte[] frame = new byte[12345];
|
||||||
random.nextBytes(frame);
|
random.nextBytes(frame);
|
||||||
@@ -74,7 +75,8 @@ public class FrameReadWriteTest extends BriarTestCase {
|
|||||||
// Write the frames
|
// Write the frames
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
ConnectionEncrypter encrypter = new ConnectionEncrypterImpl(out,
|
ConnectionEncrypter encrypter = new ConnectionEncrypterImpl(out,
|
||||||
Long.MAX_VALUE, tagCipher, frameCipher, tagCopy, frameCopy);
|
Long.MAX_VALUE, tagCipher, frameCipher, tagCopy, frameCopy,
|
||||||
|
false);
|
||||||
ConnectionWriter writer = new ConnectionWriterImpl(encrypter, mac,
|
ConnectionWriter writer = new ConnectionWriterImpl(encrypter, mac,
|
||||||
macCopy);
|
macCopy);
|
||||||
OutputStream out1 = writer.getOutputStream();
|
OutputStream out1 = writer.getOutputStream();
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package net.sf.briar.transport;
|
package net.sf.briar.transport;
|
||||||
|
|
||||||
|
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||||
import static org.junit.Assert.assertArrayEquals;
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
@@ -40,7 +41,8 @@ public class SegmentedConnectionEncrypterTest extends BriarTestCase {
|
|||||||
@Test
|
@Test
|
||||||
public void testEncryption() throws Exception {
|
public void testEncryption() throws Exception {
|
||||||
// Calculate the expected tag
|
// Calculate the expected tag
|
||||||
byte[] tag = TagEncoder.encodeTag(0, tagCipher, tagKey);
|
byte[] tag = new byte[TAG_LENGTH];
|
||||||
|
TagEncoder.encodeTag(tag, 0, tagCipher, tagKey);
|
||||||
// Calculate the expected ciphertext for the first frame
|
// Calculate the expected ciphertext for the first frame
|
||||||
byte[] iv = new byte[frameCipher.getBlockSize()];
|
byte[] iv = new byte[frameCipher.getBlockSize()];
|
||||||
byte[] plaintext = new byte[123 + MAC_LENGTH];
|
byte[] plaintext = new byte[123 + MAC_LENGTH];
|
||||||
@@ -62,7 +64,8 @@ public class SegmentedConnectionEncrypterTest extends BriarTestCase {
|
|||||||
// Use a connection encrypter to encrypt the plaintext
|
// Use a connection encrypter to encrypt the plaintext
|
||||||
ByteArraySegmentSink sink = new ByteArraySegmentSink();
|
ByteArraySegmentSink sink = new ByteArraySegmentSink();
|
||||||
ConnectionEncrypter e = new SegmentedConnectionEncrypter(sink,
|
ConnectionEncrypter e = new SegmentedConnectionEncrypter(sink,
|
||||||
Long.MAX_VALUE, tagCipher, frameCipher, tagKey, frameKey);
|
Long.MAX_VALUE, tagCipher, frameCipher, tagKey, frameKey,
|
||||||
|
false);
|
||||||
// The first frame's buffer must have enough space for the tag
|
// The first frame's buffer must have enough space for the tag
|
||||||
e.writeFrame(plaintext, plaintext.length);
|
e.writeFrame(plaintext, plaintext.length);
|
||||||
e.writeFrame(plaintext1, plaintext1.length);
|
e.writeFrame(plaintext1, plaintext1.length);
|
||||||
|
|||||||
Reference in New Issue
Block a user