mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-21 15:19:53 +01:00
Decryption code for tagging every segment.
This commit is contained in:
@@ -2,6 +2,7 @@ package net.sf.briar.transport;
|
|||||||
|
|
||||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||||
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 static net.sf.briar.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
||||||
|
|
||||||
import java.io.EOFException;
|
import java.io.EOFException;
|
||||||
@@ -18,19 +19,24 @@ import net.sf.briar.api.crypto.ErasableKey;
|
|||||||
class ConnectionDecrypter implements FrameSource {
|
class ConnectionDecrypter implements FrameSource {
|
||||||
|
|
||||||
private final InputStream in;
|
private final InputStream in;
|
||||||
private final Cipher frameCipher;
|
private final Cipher tagCipher, frameCipher;
|
||||||
private final ErasableKey frameKey;
|
private final ErasableKey tagKey, frameKey;
|
||||||
private final int macLength, blockSize;
|
private final int macLength, blockSize;
|
||||||
private final byte[] iv;
|
private final byte[] iv;
|
||||||
|
private final boolean tagEverySegment;
|
||||||
|
|
||||||
private long frame = 0L;
|
private long frame = 0L;
|
||||||
|
|
||||||
ConnectionDecrypter(InputStream in, Cipher frameCipher,
|
ConnectionDecrypter(InputStream in, Cipher tagCipher, Cipher frameCipher,
|
||||||
ErasableKey frameKey, int macLength) {
|
ErasableKey tagKey, ErasableKey frameKey, int macLength,
|
||||||
|
boolean tagEverySegment) {
|
||||||
this.in = in;
|
this.in = in;
|
||||||
|
this.tagCipher = tagCipher;
|
||||||
this.frameCipher = frameCipher;
|
this.frameCipher = frameCipher;
|
||||||
|
this.tagKey = tagKey;
|
||||||
this.frameKey = frameKey;
|
this.frameKey = frameKey;
|
||||||
this.macLength = macLength;
|
this.macLength = macLength;
|
||||||
|
this.tagEverySegment = tagEverySegment;
|
||||||
blockSize = frameCipher.getBlockSize();
|
blockSize = frameCipher.getBlockSize();
|
||||||
if(blockSize < FRAME_HEADER_LENGTH)
|
if(blockSize < FRAME_HEADER_LENGTH)
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
@@ -40,30 +46,39 @@ class ConnectionDecrypter implements FrameSource {
|
|||||||
public int readFrame(byte[] b) throws IOException {
|
public int readFrame(byte[] b) throws IOException {
|
||||||
if(b.length < MAX_FRAME_LENGTH) throw new IllegalArgumentException();
|
if(b.length < MAX_FRAME_LENGTH) throw new IllegalArgumentException();
|
||||||
if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
|
if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
|
||||||
// Initialise the cipher
|
boolean tag = tagEverySegment && frame > 0;
|
||||||
IvEncoder.updateIv(iv, frame);
|
|
||||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
|
||||||
try {
|
|
||||||
frameCipher.init(Cipher.DECRYPT_MODE, frameKey, ivSpec);
|
|
||||||
} catch(GeneralSecurityException badIvOrKey) {
|
|
||||||
throw new RuntimeException(badIvOrKey);
|
|
||||||
}
|
|
||||||
// Clear the buffer before exposing it to the transport plugin
|
// Clear the buffer before exposing it to the transport plugin
|
||||||
for(int i = 0; i < b.length; i++) b[i] = 0;
|
for(int i = 0; i < b.length; i++) b[i] = 0;
|
||||||
try {
|
try {
|
||||||
|
// If a tag is expected then read, decrypt and validate it
|
||||||
|
if(tag) {
|
||||||
|
int offset = 0;
|
||||||
|
while(offset < TAG_LENGTH) {
|
||||||
|
int read = in.read(b, offset, TAG_LENGTH - offset);
|
||||||
|
if(read == -1) {
|
||||||
|
if(offset == 0) return -1;
|
||||||
|
throw new EOFException();
|
||||||
|
}
|
||||||
|
offset += read;
|
||||||
|
}
|
||||||
|
if(!TagEncoder.validateTag(b, frame, tagCipher, tagKey))
|
||||||
|
throw new FormatException();
|
||||||
|
}
|
||||||
// Read the first block
|
// Read the first block
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
while(offset < blockSize) {
|
while(offset < blockSize) {
|
||||||
int read = in.read(b, offset, blockSize - offset);
|
int read = in.read(b, offset, blockSize - offset);
|
||||||
if(read == -1) {
|
if(read == -1) {
|
||||||
if(offset == 0) return -1;
|
if(offset == 0 && !tag) return -1;
|
||||||
if(offset < blockSize) throw new EOFException();
|
throw new EOFException();
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
offset += read;
|
offset += read;
|
||||||
}
|
}
|
||||||
// Decrypt the first block
|
// Decrypt the first block
|
||||||
try {
|
try {
|
||||||
|
IvEncoder.updateIv(iv, frame);
|
||||||
|
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||||
|
frameCipher.init(Cipher.DECRYPT_MODE, frameKey, ivSpec);
|
||||||
int decrypted = frameCipher.update(b, 0, blockSize, b);
|
int decrypted = frameCipher.update(b, 0, blockSize, b);
|
||||||
if(decrypted != blockSize) throw new RuntimeException();
|
if(decrypted != blockSize) throw new RuntimeException();
|
||||||
} catch(GeneralSecurityException badCipher) {
|
} catch(GeneralSecurityException badCipher) {
|
||||||
@@ -96,6 +111,7 @@ class ConnectionDecrypter implements FrameSource {
|
|||||||
return length;
|
return length;
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
frameKey.erase();
|
frameKey.erase();
|
||||||
|
tagKey.erase();
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ class ConnectionEncrypterImpl implements ConnectionEncrypter {
|
|||||||
private final boolean tagEverySegment;
|
private final boolean tagEverySegment;
|
||||||
private final byte[] iv, tag;
|
private final byte[] iv, tag;
|
||||||
|
|
||||||
private long capacity, frame = 0;
|
private long capacity, frame = 0L;
|
||||||
|
|
||||||
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,
|
||||||
|
|||||||
@@ -43,12 +43,14 @@ class ConnectionReaderFactoryImpl implements ConnectionReaderFactory {
|
|||||||
// Derive the keys and erase the secret
|
// Derive the keys and erase the secret
|
||||||
ErasableKey frameKey = crypto.deriveFrameKey(secret, initiator);
|
ErasableKey frameKey = crypto.deriveFrameKey(secret, initiator);
|
||||||
ErasableKey macKey = crypto.deriveMacKey(secret, initiator);
|
ErasableKey macKey = crypto.deriveMacKey(secret, initiator);
|
||||||
|
ErasableKey tagKey = crypto.deriveTagKey(secret, initiator);
|
||||||
ByteUtils.erase(secret);
|
ByteUtils.erase(secret);
|
||||||
// Create the decrypter
|
// Create the decrypter
|
||||||
|
Cipher tagCipher = crypto.getTagCipher();
|
||||||
Cipher frameCipher = crypto.getFrameCipher();
|
Cipher frameCipher = crypto.getFrameCipher();
|
||||||
Mac mac = crypto.getMac();
|
Mac mac = crypto.getMac();
|
||||||
FrameSource decrypter = new ConnectionDecrypter(in,
|
FrameSource decrypter = new ConnectionDecrypter(in, tagCipher,
|
||||||
frameCipher, frameKey, mac.getMacLength());
|
frameCipher, tagKey, frameKey, mac.getMacLength(), false);
|
||||||
// Create the reader
|
// Create the reader
|
||||||
return new ConnectionReaderImpl(decrypter, mac, macKey);
|
return new ConnectionReaderImpl(decrypter, mac, macKey);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package net.sf.briar.transport;
|
|||||||
|
|
||||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||||
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 static net.sf.briar.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -18,20 +19,25 @@ import net.sf.briar.api.plugins.SegmentSource;
|
|||||||
class SegmentedConnectionDecrypter implements FrameSource {
|
class SegmentedConnectionDecrypter implements FrameSource {
|
||||||
|
|
||||||
private final SegmentSource in;
|
private final SegmentSource in;
|
||||||
private final Cipher frameCipher;
|
private final Cipher tagCipher, frameCipher;
|
||||||
private final ErasableKey frameKey;
|
private final ErasableKey tagKey, frameKey;
|
||||||
private final int macLength, blockSize;
|
private final int macLength, blockSize;
|
||||||
private final byte[] iv;
|
private final byte[] iv;
|
||||||
private final Segment segment;
|
private final Segment segment;
|
||||||
|
private final boolean tagEverySegment;
|
||||||
|
|
||||||
private long frame = 0L;
|
private long frame = 0L;
|
||||||
|
|
||||||
SegmentedConnectionDecrypter(SegmentSource in, Cipher frameCipher,
|
SegmentedConnectionDecrypter(SegmentSource in, Cipher tagCipher,
|
||||||
ErasableKey frameKey, int macLength) {
|
Cipher frameCipher, ErasableKey tagKey, ErasableKey frameKey,
|
||||||
|
int macLength, boolean tagEverySegment) {
|
||||||
this.in = in;
|
this.in = in;
|
||||||
|
this.tagCipher = tagCipher;
|
||||||
this.frameCipher = frameCipher;
|
this.frameCipher = frameCipher;
|
||||||
|
this.tagKey = tagKey;
|
||||||
this.frameKey = frameKey;
|
this.frameKey = frameKey;
|
||||||
this.macLength = macLength;
|
this.macLength = macLength;
|
||||||
|
this.tagEverySegment = tagEverySegment;
|
||||||
blockSize = frameCipher.getBlockSize();
|
blockSize = frameCipher.getBlockSize();
|
||||||
if(blockSize < FRAME_HEADER_LENGTH)
|
if(blockSize < FRAME_HEADER_LENGTH)
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
@@ -42,30 +48,27 @@ class SegmentedConnectionDecrypter implements FrameSource {
|
|||||||
public int readFrame(byte[] b) throws IOException {
|
public int readFrame(byte[] b) throws IOException {
|
||||||
if(b.length < MAX_FRAME_LENGTH) throw new IllegalArgumentException();
|
if(b.length < MAX_FRAME_LENGTH) throw new IllegalArgumentException();
|
||||||
if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
|
if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
|
||||||
// Initialise the cipher
|
boolean tag = tagEverySegment && frame > 0;
|
||||||
IvEncoder.updateIv(iv, frame);
|
|
||||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
|
||||||
try {
|
|
||||||
frameCipher.init(Cipher.DECRYPT_MODE, frameKey, ivSpec);
|
|
||||||
} catch(GeneralSecurityException badIvOrKey) {
|
|
||||||
throw new RuntimeException(badIvOrKey);
|
|
||||||
}
|
|
||||||
// Clear the buffer before exposing it to the transport plugin
|
// Clear the buffer before exposing it to the transport plugin
|
||||||
segment.clear();
|
segment.clear();
|
||||||
try {
|
try {
|
||||||
// Read the frame
|
// Read the segment
|
||||||
if(!in.readSegment(segment)) return -1;
|
if(!in.readSegment(segment)) return -1;
|
||||||
if(segment.getTransmissionNumber() != frame)
|
int offset = tag ? TAG_LENGTH : 0, length = segment.getLength();
|
||||||
throw new FormatException();
|
|
||||||
int length = segment.getLength();
|
|
||||||
if(length > MAX_FRAME_LENGTH) throw new FormatException();
|
if(length > MAX_FRAME_LENGTH) throw new FormatException();
|
||||||
if(length < FRAME_HEADER_LENGTH + macLength)
|
if(length < offset + FRAME_HEADER_LENGTH + macLength)
|
||||||
throw new FormatException();
|
throw new FormatException();
|
||||||
|
// If a tag is expected, decrypt and validate it
|
||||||
|
if(tag && !TagEncoder.validateTag(segment.getBuffer(), frame,
|
||||||
|
tagCipher, tagKey)) throw new FormatException();
|
||||||
// Decrypt the frame
|
// Decrypt the frame
|
||||||
try {
|
try {
|
||||||
int decrypted = frameCipher.doFinal(segment.getBuffer(), 0,
|
IvEncoder.updateIv(iv, frame);
|
||||||
length, b);
|
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||||
if(decrypted != length) throw new RuntimeException();
|
frameCipher.init(Cipher.DECRYPT_MODE, frameKey, ivSpec);
|
||||||
|
int decrypted = frameCipher.doFinal(segment.getBuffer(), offset,
|
||||||
|
length - offset, b);
|
||||||
|
if(decrypted != length - offset) throw new RuntimeException();
|
||||||
} catch(GeneralSecurityException badCipher) {
|
} catch(GeneralSecurityException badCipher) {
|
||||||
throw new RuntimeException(badCipher);
|
throw new RuntimeException(badCipher);
|
||||||
}
|
}
|
||||||
@@ -75,12 +78,13 @@ class SegmentedConnectionDecrypter implements FrameSource {
|
|||||||
throw new FormatException();
|
throw new FormatException();
|
||||||
int payload = HeaderEncoder.getPayloadLength(b);
|
int payload = HeaderEncoder.getPayloadLength(b);
|
||||||
int padding = HeaderEncoder.getPaddingLength(b);
|
int padding = HeaderEncoder.getPaddingLength(b);
|
||||||
if(length != FRAME_HEADER_LENGTH + payload + padding + macLength)
|
if(length != offset + FRAME_HEADER_LENGTH + payload + padding
|
||||||
throw new FormatException();
|
+ macLength) throw new FormatException();
|
||||||
frame++;
|
frame++;
|
||||||
return length;
|
return length - offset;
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
frameKey.erase();
|
frameKey.erase();
|
||||||
|
tagKey.erase();
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ class SegmentedConnectionEncrypter implements ConnectionEncrypter {
|
|||||||
private final byte[] iv;
|
private final byte[] iv;
|
||||||
private final Segment segment;
|
private final Segment segment;
|
||||||
|
|
||||||
private long capacity, frame = 0;
|
private long capacity, frame = 0L;
|
||||||
|
|
||||||
SegmentedConnectionEncrypter(SegmentSink out, long capacity,
|
SegmentedConnectionEncrypter(SegmentSink out, long capacity,
|
||||||
Cipher tagCipher, Cipher frameCipher, ErasableKey tagKey,
|
Cipher tagCipher, Cipher frameCipher, ErasableKey tagKey,
|
||||||
|
|||||||
@@ -39,15 +39,17 @@ class TagEncoder {
|
|||||||
ErasableKey tagKey) {
|
ErasableKey tagKey) {
|
||||||
if(frame < 0 || frame > MAX_32_BIT_UNSIGNED)
|
if(frame < 0 || frame > MAX_32_BIT_UNSIGNED)
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
if(tag.length != 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();
|
||||||
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.DECRYPT_MODE, tagKey, ivSpec);
|
tagCipher.init(Cipher.DECRYPT_MODE, tagKey, ivSpec);
|
||||||
byte[] plaintext = tagCipher.doFinal(tag);
|
byte[] plaintext = tagCipher.doFinal(tag, 0, TAG_LENGTH);
|
||||||
|
if(plaintext.length != TAG_LENGTH)
|
||||||
|
throw new IllegalArgumentException();
|
||||||
// The plaintext should be blank
|
// The plaintext should be blank
|
||||||
for(int i = 0; i < plaintext.length; i++) {
|
for(int i = 0; i < plaintext.length; i++) {
|
||||||
if(plaintext[i] != 0) return false;
|
if(plaintext[i] != 0) return false;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package net.sf.briar.transport;
|
|||||||
|
|
||||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||||
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 java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
|
||||||
@@ -23,19 +24,21 @@ public class ConnectionDecrypterTest extends BriarTestCase {
|
|||||||
|
|
||||||
private static final int MAC_LENGTH = 32;
|
private static final int MAC_LENGTH = 32;
|
||||||
|
|
||||||
private final Cipher frameCipher;
|
private final Cipher tagCipher, frameCipher;
|
||||||
private final ErasableKey frameKey;
|
private final ErasableKey tagKey, frameKey;
|
||||||
|
|
||||||
public ConnectionDecrypterTest() {
|
public ConnectionDecrypterTest() {
|
||||||
super();
|
super();
|
||||||
Injector i = Guice.createInjector(new CryptoModule());
|
Injector i = Guice.createInjector(new CryptoModule());
|
||||||
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
|
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
|
||||||
|
tagCipher = crypto.getTagCipher();
|
||||||
frameCipher = crypto.getFrameCipher();
|
frameCipher = crypto.getFrameCipher();
|
||||||
|
tagKey = crypto.generateTestKey();
|
||||||
frameKey = crypto.generateTestKey();
|
frameKey = crypto.generateTestKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDecryption() throws Exception {
|
public void testDecryptionWithFirstSegmentTagged() throws Exception {
|
||||||
// Calculate the ciphertext for the first frame
|
// Calculate the ciphertext for the first frame
|
||||||
byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123 + MAC_LENGTH];
|
byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123 + MAC_LENGTH];
|
||||||
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0);
|
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0);
|
||||||
@@ -57,8 +60,48 @@ public class ConnectionDecrypterTest extends BriarTestCase {
|
|||||||
out.write(ciphertext1);
|
out.write(ciphertext1);
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||||
// Use a ConnectionDecrypter to decrypt the ciphertext
|
// Use a ConnectionDecrypter to decrypt the ciphertext
|
||||||
FrameSource decrypter = new ConnectionDecrypter(in, frameCipher,
|
FrameSource decrypter = new ConnectionDecrypter(in, tagCipher,
|
||||||
frameKey, MAC_LENGTH);
|
frameCipher, tagKey, frameKey, MAC_LENGTH, false);
|
||||||
|
// First frame
|
||||||
|
byte[] decrypted = new byte[MAX_FRAME_LENGTH];
|
||||||
|
assertEquals(plaintext.length, decrypter.readFrame(decrypted));
|
||||||
|
for(int i = 0; i < plaintext.length; i++) {
|
||||||
|
assertEquals(plaintext[i], decrypted[i]);
|
||||||
|
}
|
||||||
|
// Second frame
|
||||||
|
assertEquals(plaintext1.length, decrypter.readFrame(decrypted));
|
||||||
|
for(int i = 0; i < plaintext1.length; i++) {
|
||||||
|
assertEquals(plaintext1[i], decrypted[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDecryptionWithEverySegmentTagged() throws Exception {
|
||||||
|
// Calculate the ciphertext for the first frame
|
||||||
|
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
|
||||||
|
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);
|
||||||
|
IvEncoder.updateIv(iv, 1L);
|
||||||
|
ivSpec = new IvParameterSpec(iv);
|
||||||
|
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||||
|
frameCipher.doFinal(plaintext1, 0, plaintext1.length, ciphertext1,
|
||||||
|
TAG_LENGTH);
|
||||||
|
// Concatenate the ciphertexts
|
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
out.write(ciphertext);
|
||||||
|
out.write(ciphertext1);
|
||||||
|
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||||
|
// Use a ConnectionDecrypter to decrypt the ciphertext
|
||||||
|
FrameSource decrypter = new ConnectionDecrypter(in, tagCipher,
|
||||||
|
frameCipher, tagKey, frameKey, MAC_LENGTH, true);
|
||||||
// First frame
|
// First frame
|
||||||
byte[] decrypted = new byte[MAX_FRAME_LENGTH];
|
byte[] decrypted = new byte[MAX_FRAME_LENGTH];
|
||||||
assertEquals(plaintext.length, decrypter.readFrame(decrypted));
|
assertEquals(plaintext.length, decrypter.readFrame(decrypted));
|
||||||
|
|||||||
@@ -91,8 +91,8 @@ public class FrameReadWriteTest extends BriarTestCase {
|
|||||||
assertArrayEquals(tag, recoveredTag);
|
assertArrayEquals(tag, recoveredTag);
|
||||||
assertTrue(TagEncoder.validateTag(tag, 0, tagCipher, tagKey));
|
assertTrue(TagEncoder.validateTag(tag, 0, tagCipher, tagKey));
|
||||||
// Read the frames back
|
// Read the frames back
|
||||||
FrameSource decrypter = new ConnectionDecrypter(in, frameCipher,
|
FrameSource decrypter = new ConnectionDecrypter(in, tagCipher,
|
||||||
frameKey, mac.getMacLength());
|
frameCipher, tagKey, frameKey, mac.getMacLength(), false);
|
||||||
ConnectionReader reader = new ConnectionReaderImpl(decrypter, mac,
|
ConnectionReader reader = new ConnectionReaderImpl(decrypter, mac,
|
||||||
macKey);
|
macKey);
|
||||||
InputStream in1 = reader.getInputStream();
|
InputStream in1 = reader.getInputStream();
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package net.sf.briar.transport;
|
|||||||
|
|
||||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||||
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 java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@@ -24,19 +25,21 @@ public class SegmentedConnectionDecrypterTest extends BriarTestCase {
|
|||||||
|
|
||||||
private static final int MAC_LENGTH = 32;
|
private static final int MAC_LENGTH = 32;
|
||||||
|
|
||||||
private final Cipher frameCipher;
|
private final Cipher tagCipher, frameCipher;
|
||||||
private final ErasableKey frameKey;
|
private final ErasableKey tagKey, frameKey;
|
||||||
|
|
||||||
public SegmentedConnectionDecrypterTest() {
|
public SegmentedConnectionDecrypterTest() {
|
||||||
super();
|
super();
|
||||||
Injector i = Guice.createInjector(new CryptoModule());
|
Injector i = Guice.createInjector(new CryptoModule());
|
||||||
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
|
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
|
||||||
|
tagCipher = crypto.getTagCipher();
|
||||||
frameCipher = crypto.getFrameCipher();
|
frameCipher = crypto.getFrameCipher();
|
||||||
|
tagKey = crypto.generateTestKey();
|
||||||
frameKey = crypto.generateTestKey();
|
frameKey = crypto.generateTestKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDecryption() throws Exception {
|
public void testDecryptionWithFirstSegmentTagged() throws Exception {
|
||||||
// Calculate the ciphertext for the first frame
|
// Calculate the ciphertext for the first frame
|
||||||
byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123 + MAC_LENGTH];
|
byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123 + MAC_LENGTH];
|
||||||
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0);
|
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0);
|
||||||
@@ -55,8 +58,45 @@ public class SegmentedConnectionDecrypterTest extends BriarTestCase {
|
|||||||
// Use a connection decrypter to decrypt the ciphertext
|
// Use a connection decrypter to decrypt the ciphertext
|
||||||
byte[][] frames = new byte[][] { ciphertext, ciphertext1 };
|
byte[][] frames = new byte[][] { ciphertext, ciphertext1 };
|
||||||
SegmentSource in = new ByteArraySegmentSource(frames);
|
SegmentSource in = new ByteArraySegmentSource(frames);
|
||||||
FrameSource decrypter = new SegmentedConnectionDecrypter(in,
|
FrameSource decrypter = new SegmentedConnectionDecrypter(in, tagCipher,
|
||||||
frameCipher, frameKey, MAC_LENGTH);
|
frameCipher, tagKey, frameKey, MAC_LENGTH, false);
|
||||||
|
// First frame
|
||||||
|
byte[] decrypted = new byte[MAX_FRAME_LENGTH];
|
||||||
|
assertEquals(plaintext.length, decrypter.readFrame(decrypted));
|
||||||
|
for(int i = 0; i < plaintext.length; i++) {
|
||||||
|
assertEquals(plaintext[i], decrypted[i]);
|
||||||
|
}
|
||||||
|
// Second frame
|
||||||
|
assertEquals(plaintext1.length, decrypter.readFrame(decrypted));
|
||||||
|
for(int i = 0; i < plaintext1.length; i++) {
|
||||||
|
assertEquals(plaintext1[i], decrypted[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDecryptionWithEverySegmentTagged() throws Exception {
|
||||||
|
// Calculate the ciphertext for the first frame
|
||||||
|
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
|
||||||
|
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);
|
||||||
|
IvEncoder.updateIv(iv, 1L);
|
||||||
|
ivSpec = new IvParameterSpec(iv);
|
||||||
|
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||||
|
frameCipher.doFinal(plaintext1, 0, plaintext1.length, ciphertext1,
|
||||||
|
TAG_LENGTH);
|
||||||
|
// Use a connection decrypter to decrypt the ciphertext
|
||||||
|
byte[][] frames = new byte[][] { ciphertext, ciphertext1 };
|
||||||
|
SegmentSource in = new ByteArraySegmentSource(frames);
|
||||||
|
FrameSource decrypter = new SegmentedConnectionDecrypter(in, tagCipher,
|
||||||
|
frameCipher, tagKey, frameKey, MAC_LENGTH, true);
|
||||||
// First frame
|
// First frame
|
||||||
byte[] decrypted = new byte[MAX_FRAME_LENGTH];
|
byte[] decrypted = new byte[MAX_FRAME_LENGTH];
|
||||||
assertEquals(plaintext.length, decrypter.readFrame(decrypted));
|
assertEquals(plaintext.length, decrypter.readFrame(decrypted));
|
||||||
@@ -85,7 +125,6 @@ public class SegmentedConnectionDecrypterTest extends BriarTestCase {
|
|||||||
byte[] src = frames[frame];
|
byte[] src = frames[frame];
|
||||||
System.arraycopy(src, 0, s.getBuffer(), 0, src.length);
|
System.arraycopy(src, 0, s.getBuffer(), 0, src.length);
|
||||||
s.setLength(src.length);
|
s.setLength(src.length);
|
||||||
s.setTransmissionNumber(frame);
|
|
||||||
frame++;
|
frame++;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user