mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 11:19:04 +01:00
Converted incoming encryption layer from frames to segments.
This commit is contained in:
@@ -33,7 +33,9 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
// Read the frame
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(frame);
|
||||
IncomingEncryptionLayer decrypter = new NullIncomingEncryptionLayer(in);
|
||||
ConnectionReader r = new ConnectionReaderImpl(decrypter, mac, macKey);
|
||||
IncomingErrorCorrectionLayer correcter =
|
||||
new NullIncomingErrorCorrectionLayer(decrypter);
|
||||
ConnectionReader r = new ConnectionReaderImpl(correcter, mac, macKey);
|
||||
// There should be no bytes available before EOF
|
||||
assertEquals(-1, r.getInputStream().read());
|
||||
}
|
||||
@@ -51,7 +53,9 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
// Read the frame
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(frame);
|
||||
IncomingEncryptionLayer decrypter = new NullIncomingEncryptionLayer(in);
|
||||
ConnectionReader r = new ConnectionReaderImpl(decrypter, mac, macKey);
|
||||
IncomingErrorCorrectionLayer correcter =
|
||||
new NullIncomingErrorCorrectionLayer(decrypter);
|
||||
ConnectionReader r = new ConnectionReaderImpl(correcter, mac, macKey);
|
||||
// There should be one byte available before EOF
|
||||
assertEquals(0, r.getInputStream().read());
|
||||
assertEquals(-1, r.getInputStream().read());
|
||||
@@ -77,7 +81,9 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
// Read the first frame
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
IncomingEncryptionLayer decrypter = new NullIncomingEncryptionLayer(in);
|
||||
ConnectionReader r = new ConnectionReaderImpl(decrypter, mac, macKey);
|
||||
IncomingErrorCorrectionLayer correcter =
|
||||
new NullIncomingErrorCorrectionLayer(decrypter);
|
||||
ConnectionReader r = new ConnectionReaderImpl(correcter, mac, macKey);
|
||||
byte[] read = new byte[MAX_PAYLOAD_LENGTH];
|
||||
TestUtils.readFully(r.getInputStream(), read);
|
||||
// Try to read the second frame
|
||||
@@ -111,7 +117,9 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
// Read the first frame
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
IncomingEncryptionLayer decrypter = new NullIncomingEncryptionLayer(in);
|
||||
ConnectionReader r = new ConnectionReaderImpl(decrypter, mac, macKey);
|
||||
IncomingErrorCorrectionLayer correcter =
|
||||
new NullIncomingErrorCorrectionLayer(decrypter);
|
||||
ConnectionReader r = new ConnectionReaderImpl(correcter, mac, macKey);
|
||||
byte[] read = new byte[MAX_PAYLOAD_LENGTH - paddingLength];
|
||||
TestUtils.readFully(r.getInputStream(), read);
|
||||
// Try to read the second frame
|
||||
@@ -137,7 +145,9 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
// Read the frame
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(frame);
|
||||
IncomingEncryptionLayer decrypter = new NullIncomingEncryptionLayer(in);
|
||||
ConnectionReader r = new ConnectionReaderImpl(decrypter, mac, macKey);
|
||||
IncomingErrorCorrectionLayer correcter =
|
||||
new NullIncomingErrorCorrectionLayer(decrypter);
|
||||
ConnectionReader r = new ConnectionReaderImpl(correcter, mac, macKey);
|
||||
// The non-zero padding should be rejected
|
||||
try {
|
||||
r.getInputStream().read();
|
||||
@@ -169,7 +179,9 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
// Read the frames
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
IncomingEncryptionLayer decrypter = new NullIncomingEncryptionLayer(in);
|
||||
ConnectionReader r = new ConnectionReaderImpl(decrypter, mac, macKey);
|
||||
IncomingErrorCorrectionLayer correcter =
|
||||
new NullIncomingErrorCorrectionLayer(decrypter);
|
||||
ConnectionReader r = new ConnectionReaderImpl(correcter, mac, macKey);
|
||||
byte[] read = new byte[payloadLength];
|
||||
TestUtils.readFully(r.getInputStream(), read);
|
||||
assertArrayEquals(new byte[payloadLength], read);
|
||||
@@ -193,7 +205,9 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
// Try to read the frame - not a single byte should be read
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(frame);
|
||||
IncomingEncryptionLayer decrypter = new NullIncomingEncryptionLayer(in);
|
||||
ConnectionReader r = new ConnectionReaderImpl(decrypter, mac, macKey);
|
||||
IncomingErrorCorrectionLayer correcter =
|
||||
new NullIncomingErrorCorrectionLayer(decrypter);
|
||||
ConnectionReader r = new ConnectionReaderImpl(correcter, mac, macKey);
|
||||
try {
|
||||
r.getInputStream().read();
|
||||
fail();
|
||||
@@ -215,7 +229,9 @@ public class ConnectionReaderImplTest extends TransportTest {
|
||||
// Try to read the frame - not a single byte should be read
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(frame);
|
||||
IncomingEncryptionLayer decrypter = new NullIncomingEncryptionLayer(in);
|
||||
ConnectionReader r = new ConnectionReaderImpl(decrypter, mac, macKey);
|
||||
IncomingErrorCorrectionLayer correcter =
|
||||
new NullIncomingErrorCorrectionLayer(decrypter);
|
||||
ConnectionReader r = new ConnectionReaderImpl(correcter, mac, macKey);
|
||||
try {
|
||||
r.getInputStream().read();
|
||||
fail();
|
||||
|
||||
@@ -618,7 +618,7 @@ public class ConnectionRecogniserImplTest extends BriarTestCase {
|
||||
ErasableKey tagKey = crypto.deriveTagKey(secret, true);
|
||||
Cipher tagCipher = crypto.getTagCipher();
|
||||
byte[] tag = new byte[TAG_LENGTH];
|
||||
TagEncoder.encodeTag(tag, 0, tagCipher, tagKey);
|
||||
TagEncoder.encodeTag(tag, 0L, tagCipher, tagKey);
|
||||
return tag;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ public class FrameReadWriteTest extends BriarTestCase {
|
||||
private void testWriteAndRead(boolean initiator) throws Exception {
|
||||
// Encode the tag
|
||||
byte[] tag = new byte[TAG_LENGTH];
|
||||
TagEncoder.encodeTag(tag, 0, tagCipher, tagKey);
|
||||
TagEncoder.encodeTag(tag, 0L, tagCipher, tagKey);
|
||||
// Generate two random frames
|
||||
byte[] frame = new byte[12345];
|
||||
random.nextBytes(frame);
|
||||
@@ -89,11 +89,13 @@ public class FrameReadWriteTest extends BriarTestCase {
|
||||
byte[] recoveredTag = new byte[TAG_LENGTH];
|
||||
assertEquals(TAG_LENGTH, in.read(recoveredTag));
|
||||
assertArrayEquals(tag, recoveredTag);
|
||||
assertTrue(TagEncoder.validateTag(tag, 0, tagCipher, tagKey));
|
||||
assertEquals(0L, TagEncoder.decodeTag(tag, tagCipher, tagKey));
|
||||
// Read the frames back
|
||||
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in,
|
||||
tagCipher, frameCipher, tagKey, frameKey, false);
|
||||
ConnectionReader reader = new ConnectionReaderImpl(decrypter, mac,
|
||||
IncomingErrorCorrectionLayer correcter =
|
||||
new NullIncomingErrorCorrectionLayer(decrypter);
|
||||
ConnectionReader reader = new ConnectionReaderImpl(correcter, mac,
|
||||
macKey);
|
||||
InputStream in1 = reader.getInputStream();
|
||||
byte[] recovered = new byte[frame.length];
|
||||
|
||||
@@ -2,7 +2,6 @@ package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
@@ -13,6 +12,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.Segment;
|
||||
import net.sf.briar.crypto.CryptoModule;
|
||||
|
||||
import org.apache.commons.io.output.ByteArrayOutputStream;
|
||||
@@ -38,14 +38,14 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testDecryptionWithFirstSegmentTagged() throws Exception {
|
||||
// Calculate the ciphertext for the first frame
|
||||
// Calculate the ciphertext for the first segment
|
||||
byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0);
|
||||
byte[] iv = IvEncoder.encodeIv(0L, frameCipher.getBlockSize());
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext = frameCipher.doFinal(plaintext, 0, plaintext.length);
|
||||
// Calculate the ciphertext for the second frame
|
||||
// Calculate the ciphertext for the second segment
|
||||
byte[] plaintext1 = new byte[FRAME_HEADER_LENGTH + 1234 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(plaintext1, 1L, 1234, 0);
|
||||
IvEncoder.updateIv(iv, 1L);
|
||||
@@ -62,13 +62,19 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
||||
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in,
|
||||
tagCipher, frameCipher, tagKey, frameKey, false);
|
||||
// First frame
|
||||
byte[] decrypted = new byte[MAX_FRAME_LENGTH];
|
||||
assertEquals(plaintext.length, decrypter.readFrame(decrypted));
|
||||
Segment s = new SegmentImpl();
|
||||
assertTrue(decrypter.readSegment(s));
|
||||
assertEquals(plaintext.length, s.getLength());
|
||||
assertEquals(0L, s.getSegmentNumber());
|
||||
byte[] decrypted = s.getBuffer();
|
||||
for(int i = 0; i < plaintext.length; i++) {
|
||||
assertEquals(plaintext[i], decrypted[i]);
|
||||
}
|
||||
// Second frame
|
||||
assertEquals(plaintext1.length, decrypter.readFrame(decrypted));
|
||||
assertTrue(decrypter.readSegment(s));
|
||||
assertEquals(plaintext1.length, s.getLength());
|
||||
assertEquals(1L, s.getSegmentNumber());
|
||||
decrypted = s.getBuffer();
|
||||
for(int i = 0; i < plaintext1.length; i++) {
|
||||
assertEquals(plaintext1[i], decrypted[i]);
|
||||
}
|
||||
@@ -76,18 +82,18 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testDecryptionWithEverySegmentTagged() throws Exception {
|
||||
// Calculate the ciphertext for the first frame
|
||||
// Calculate the ciphertext for the first segment
|
||||
byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0);
|
||||
byte[] iv = IvEncoder.encodeIv(0L, frameCipher.getBlockSize());
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext = frameCipher.doFinal(plaintext, 0, plaintext.length);
|
||||
// Calculate the ciphertext for the second frame, including its tag
|
||||
// Calculate the ciphertext for the second segment, including its tag
|
||||
byte[] plaintext1 = new byte[FRAME_HEADER_LENGTH + 1234 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(plaintext1, 1L, 1234, 0);
|
||||
byte[] ciphertext1 = new byte[TAG_LENGTH + plaintext1.length];
|
||||
TagEncoder.encodeTag(ciphertext1, 1, tagCipher, tagKey);
|
||||
TagEncoder.encodeTag(ciphertext1, 1L, tagCipher, tagKey);
|
||||
IvEncoder.updateIv(iv, 1L);
|
||||
ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
@@ -102,13 +108,19 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
||||
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in,
|
||||
tagCipher, frameCipher, tagKey, frameKey, true);
|
||||
// First frame
|
||||
byte[] decrypted = new byte[MAX_FRAME_LENGTH];
|
||||
assertEquals(plaintext.length, decrypter.readFrame(decrypted));
|
||||
Segment s = new SegmentImpl();
|
||||
assertTrue(decrypter.readSegment(s));
|
||||
assertEquals(plaintext.length, s.getLength());
|
||||
assertEquals(0L, s.getSegmentNumber());
|
||||
byte[] decrypted = s.getBuffer();
|
||||
for(int i = 0; i < plaintext.length; i++) {
|
||||
assertEquals(plaintext[i], decrypted[i]);
|
||||
}
|
||||
// Second frame
|
||||
assertEquals(plaintext1.length, decrypter.readFrame(decrypted));
|
||||
assertTrue(decrypter.readSegment(s));
|
||||
assertEquals(plaintext1.length, s.getLength());
|
||||
assertEquals(1L, s.getSegmentNumber());
|
||||
decrypted = s.getBuffer();
|
||||
for(int i = 0; i < plaintext1.length; i++) {
|
||||
assertEquals(plaintext1[i], decrypted[i]);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -39,14 +38,14 @@ public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testDecryptionWithFirstSegmentTagged() throws Exception {
|
||||
// Calculate the ciphertext for the first frame
|
||||
// Calculate the ciphertext for the first segment
|
||||
byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0);
|
||||
byte[] iv = IvEncoder.encodeIv(0L, frameCipher.getBlockSize());
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext = frameCipher.doFinal(plaintext, 0, plaintext.length);
|
||||
// Calculate the ciphertext for the second frame
|
||||
// Calculate the ciphertext for the second segment
|
||||
byte[] plaintext1 = new byte[FRAME_HEADER_LENGTH + 1234 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(plaintext1, 1L, 1234, 0);
|
||||
IvEncoder.updateIv(iv, 1L);
|
||||
@@ -61,13 +60,19 @@ public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
new IncomingSegmentedEncryptionLayer(in, tagCipher, frameCipher,
|
||||
tagKey, frameKey, false);
|
||||
// First frame
|
||||
byte[] decrypted = new byte[MAX_FRAME_LENGTH];
|
||||
assertEquals(plaintext.length, decrypter.readFrame(decrypted));
|
||||
Segment s = new SegmentImpl();
|
||||
assertTrue(decrypter.readSegment(s));
|
||||
assertEquals(plaintext.length, s.getLength());
|
||||
assertEquals(0L, s.getSegmentNumber());
|
||||
byte[] decrypted = s.getBuffer();
|
||||
for(int i = 0; i < plaintext.length; i++) {
|
||||
assertEquals(plaintext[i], decrypted[i]);
|
||||
}
|
||||
// Second frame
|
||||
assertEquals(plaintext1.length, decrypter.readFrame(decrypted));
|
||||
assertTrue(decrypter.readSegment(s));
|
||||
assertEquals(plaintext1.length, s.getLength());
|
||||
assertEquals(1L, s.getSegmentNumber());
|
||||
decrypted = s.getBuffer();
|
||||
for(int i = 0; i < plaintext1.length; i++) {
|
||||
assertEquals(plaintext1[i], decrypted[i]);
|
||||
}
|
||||
@@ -86,7 +91,7 @@ public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
byte[] plaintext1 = new byte[FRAME_HEADER_LENGTH + 1234 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(plaintext1, 1L, 1234, 0);
|
||||
byte[] ciphertext1 = new byte[TAG_LENGTH + plaintext1.length];
|
||||
TagEncoder.encodeTag(ciphertext1, 1, tagCipher, tagKey);
|
||||
TagEncoder.encodeTag(ciphertext1, 1L, tagCipher, tagKey);
|
||||
IvEncoder.updateIv(iv, 1L);
|
||||
ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
@@ -99,13 +104,19 @@ public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
new IncomingSegmentedEncryptionLayer(in, tagCipher, frameCipher,
|
||||
tagKey, frameKey, true);
|
||||
// First frame
|
||||
byte[] decrypted = new byte[MAX_FRAME_LENGTH];
|
||||
assertEquals(plaintext.length, decrypter.readFrame(decrypted));
|
||||
Segment s = new SegmentImpl();
|
||||
assertTrue(decrypter.readSegment(s));
|
||||
assertEquals(plaintext.length, s.getLength());
|
||||
assertEquals(0L, s.getSegmentNumber());
|
||||
byte[] decrypted = s.getBuffer();
|
||||
for(int i = 0; i < plaintext.length; i++) {
|
||||
assertEquals(plaintext[i], decrypted[i]);
|
||||
}
|
||||
// Second frame
|
||||
assertEquals(plaintext1.length, decrypter.readFrame(decrypted));
|
||||
assertTrue(decrypter.readSegment(s));
|
||||
assertEquals(plaintext1.length, s.getLength());
|
||||
assertEquals(1L, s.getSegmentNumber());
|
||||
decrypted = s.getBuffer();
|
||||
for(int i = 0; i < plaintext1.length; i++) {
|
||||
assertEquals(plaintext1[i], decrypted[i]);
|
||||
}
|
||||
|
||||
@@ -9,38 +9,44 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import net.sf.briar.api.FormatException;
|
||||
import net.sf.briar.api.plugins.Segment;
|
||||
|
||||
/** An encryption layer that performs no encryption. */
|
||||
class NullIncomingEncryptionLayer implements IncomingEncryptionLayer {
|
||||
|
||||
private final InputStream in;
|
||||
|
||||
private long segmentNumber = 0L;
|
||||
|
||||
NullIncomingEncryptionLayer(InputStream in) {
|
||||
this.in = in;
|
||||
}
|
||||
|
||||
public int readFrame(byte[] b) throws IOException {
|
||||
// Read the header to determine the frame length
|
||||
public boolean readSegment(Segment s) throws IOException {
|
||||
byte[] buf = s.getBuffer();
|
||||
// Read the frame header
|
||||
int offset = 0, length = FRAME_HEADER_LENGTH;
|
||||
while(offset < length) {
|
||||
int read = in.read(b, offset, length - offset);
|
||||
int read = in.read(buf, offset, length - offset);
|
||||
if(read == -1) {
|
||||
if(offset == 0) return -1;
|
||||
if(offset == 0) return false;
|
||||
throw new EOFException();
|
||||
}
|
||||
offset += read;
|
||||
}
|
||||
// Parse the header
|
||||
int payload = HeaderEncoder.getPayloadLength(b);
|
||||
int padding = HeaderEncoder.getPaddingLength(b);
|
||||
// Parse the frame header
|
||||
int payload = HeaderEncoder.getPayloadLength(buf);
|
||||
int padding = HeaderEncoder.getPaddingLength(buf);
|
||||
length = FRAME_HEADER_LENGTH + payload + padding + MAC_LENGTH;
|
||||
if(length > MAX_FRAME_LENGTH) throw new FormatException();
|
||||
// Read the remainder of the frame
|
||||
// Read the remainder of the frame/segment
|
||||
while(offset < length) {
|
||||
int read = in.read(b, offset, length - offset);
|
||||
int read = in.read(buf, offset, length - offset);
|
||||
if(read == -1) throw new EOFException();
|
||||
offset += read;
|
||||
}
|
||||
return length;
|
||||
s.setLength(length);
|
||||
s.setSegmentNumber(segmentNumber++);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,14 +39,14 @@ public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
|
||||
public void testEncryptionWithFirstSegmentTagged() throws Exception {
|
||||
// Calculate the expected tag
|
||||
byte[] tag = new byte[TAG_LENGTH];
|
||||
TagEncoder.encodeTag(tag, 0, tagCipher, tagKey);
|
||||
// Calculate the expected ciphertext for the first frame
|
||||
TagEncoder.encodeTag(tag, 0L, tagCipher, tagKey);
|
||||
// Calculate the expected ciphertext for the first segment
|
||||
byte[] iv = new byte[frameCipher.getBlockSize()];
|
||||
byte[] plaintext = new byte[123 + MAC_LENGTH];
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext = frameCipher.doFinal(plaintext);
|
||||
// Calculate the expected ciphertext for the second frame
|
||||
// Calculate the expected ciphertext for the second segment
|
||||
byte[] plaintext1 = new byte[1234 + MAC_LENGTH];
|
||||
IvEncoder.updateIv(iv, 1L);
|
||||
ivSpec = new IvParameterSpec(iv);
|
||||
@@ -76,17 +76,17 @@ public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
|
||||
public void testEncryptionWithEverySegmentTagged() throws Exception {
|
||||
// Calculate the expected tag for the first segment
|
||||
byte[] tag = new byte[TAG_LENGTH];
|
||||
TagEncoder.encodeTag(tag, 0, tagCipher, tagKey);
|
||||
// Calculate the expected ciphertext for the first frame
|
||||
TagEncoder.encodeTag(tag, 0L, tagCipher, tagKey);
|
||||
// Calculate the expected ciphertext for the first segment
|
||||
byte[] iv = new byte[frameCipher.getBlockSize()];
|
||||
byte[] plaintext = new byte[123 + MAC_LENGTH];
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext = frameCipher.doFinal(plaintext);
|
||||
// Calculate the expected tag for the second frame
|
||||
// Calculate the expected tag for the second segment
|
||||
byte[] tag1 = new byte[TAG_LENGTH];
|
||||
TagEncoder.encodeTag(tag1, 1, tagCipher, tagKey);
|
||||
// Calculate the expected ciphertext for the second frame
|
||||
TagEncoder.encodeTag(tag1, 1L, tagCipher, tagKey);
|
||||
// Calculate the expected ciphertext for the second segment
|
||||
byte[] plaintext1 = new byte[1234 + MAC_LENGTH];
|
||||
IvEncoder.updateIv(iv, 1L);
|
||||
ivSpec = new IvParameterSpec(iv);
|
||||
|
||||
@@ -42,14 +42,14 @@ public class OutgoingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
public void testEncryptionWithFirstSegmentTagged() throws Exception {
|
||||
// Calculate the expected tag
|
||||
byte[] tag = new byte[TAG_LENGTH];
|
||||
TagEncoder.encodeTag(tag, 0, tagCipher, tagKey);
|
||||
// Calculate the expected ciphertext for the first frame
|
||||
TagEncoder.encodeTag(tag, 0L, tagCipher, tagKey);
|
||||
// Calculate the expected ciphertext for the first segment
|
||||
byte[] iv = new byte[frameCipher.getBlockSize()];
|
||||
byte[] plaintext = new byte[123 + MAC_LENGTH];
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext = frameCipher.doFinal(plaintext);
|
||||
// Calculate the expected ciphertext for the second frame
|
||||
// Calculate the expected ciphertext for the second segment
|
||||
byte[] plaintext1 = new byte[1234 + MAC_LENGTH];
|
||||
IvEncoder.updateIv(iv, 1L);
|
||||
ivSpec = new IvParameterSpec(iv);
|
||||
@@ -78,19 +78,19 @@ public class OutgoingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testEncryptionWithEverySegmentTagged() throws Exception {
|
||||
// Calculate the expected tag for the first frame
|
||||
// Calculate the expected tag for the first segment
|
||||
byte[] tag = new byte[TAG_LENGTH];
|
||||
TagEncoder.encodeTag(tag, 0, tagCipher, tagKey);
|
||||
// Calculate the expected ciphertext for the first frame
|
||||
TagEncoder.encodeTag(tag, 0L, tagCipher, tagKey);
|
||||
// Calculate the expected ciphertext for the first segment
|
||||
byte[] iv = new byte[frameCipher.getBlockSize()];
|
||||
byte[] plaintext = new byte[123 + MAC_LENGTH];
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext = frameCipher.doFinal(plaintext);
|
||||
// Calculate the expected tag for the second frame
|
||||
// Calculate the expected tag for the second segment
|
||||
byte[] tag1 = new byte[TAG_LENGTH];
|
||||
TagEncoder.encodeTag(tag1, 1, tagCipher, tagKey);
|
||||
// Calculate the expected ciphertext for the second frame
|
||||
TagEncoder.encodeTag(tag1, 1L, tagCipher, tagKey);
|
||||
// Calculate the expected ciphertext for the second segment
|
||||
byte[] plaintext1 = new byte[1234 + MAC_LENGTH];
|
||||
IvEncoder.updateIv(iv, 1L);
|
||||
ivSpec = new IvParameterSpec(iv);
|
||||
|
||||
@@ -16,8 +16,8 @@ import com.google.inject.Injector;
|
||||
|
||||
public abstract class TransportTest extends BriarTestCase {
|
||||
|
||||
static final int MAX_PAYLOAD_LENGTH
|
||||
= MAX_FRAME_LENGTH - FRAME_HEADER_LENGTH - MAC_LENGTH;
|
||||
static final int MAX_PAYLOAD_LENGTH =
|
||||
MAX_FRAME_LENGTH - FRAME_HEADER_LENGTH - MAC_LENGTH;
|
||||
|
||||
protected final Mac mac;
|
||||
protected final ErasableKey macKey;
|
||||
|
||||
Reference in New Issue
Block a user