mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 22:29:53 +01:00
Encrypter and decrypter for segmented transports (untested).
This commit is contained in:
9
api/net/sf/briar/api/plugins/FrameSink.java
Normal file
9
api/net/sf/briar/api/plugins/FrameSink.java
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package net.sf.briar.api.plugins;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public interface FrameSink {
|
||||||
|
|
||||||
|
/** Writes the given frame. */
|
||||||
|
void writeFrame(byte[] b, int len) throws IOException;
|
||||||
|
}
|
||||||
12
api/net/sf/briar/api/plugins/FrameSource.java
Normal file
12
api/net/sf/briar/api/plugins/FrameSource.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package net.sf.briar.api.plugins;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public interface FrameSource {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads a frame into the given buffer and returns its length, or -1 if no
|
||||||
|
* more frames can be read.
|
||||||
|
*/
|
||||||
|
int readFrame(byte[] b) throws IOException;
|
||||||
|
}
|
||||||
@@ -1,22 +1,15 @@
|
|||||||
package net.sf.briar.api.plugins.duplex;
|
package net.sf.briar.api.plugins.duplex;
|
||||||
|
|
||||||
import java.io.IOException;
|
import net.sf.briar.api.plugins.FrameSink;
|
||||||
|
import net.sf.briar.api.plugins.FrameSource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An interface for reading and writing data over a duplex segmented transport.
|
* An interface for reading and writing data over a duplex segmented transport.
|
||||||
* The connection is not responsible for encrypting/decrypting or authenticating
|
* The connection is not responsible for encrypting/decrypting or authenticating
|
||||||
* the data.
|
* the data.
|
||||||
*/
|
*/
|
||||||
public interface DuplexSegmentedTransportConnection {
|
public interface DuplexSegmentedTransportConnection extends FrameSource,
|
||||||
|
FrameSink {
|
||||||
/**
|
|
||||||
* Reads a frame into the given buffer and returns its length, or -1 if no
|
|
||||||
* more frames can be read.
|
|
||||||
*/
|
|
||||||
int readFrame(byte[] b) throws IOException;
|
|
||||||
|
|
||||||
/** Writes the given frame to the transport. */
|
|
||||||
void writeFrame(byte[] b, int len) throws IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the output stream should be flushed after each packet.
|
* Returns true if the output stream should be flushed after each packet.
|
||||||
|
|||||||
@@ -1,19 +1,13 @@
|
|||||||
package net.sf.briar.api.plugins.simplex;
|
package net.sf.briar.api.plugins.simplex;
|
||||||
|
|
||||||
import java.io.IOException;
|
import net.sf.briar.api.plugins.FrameSource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An interface for reading data from a simplex segmented transport. The reader
|
* An interface for reading data from a simplex segmented transport. The reader
|
||||||
* is not responsible for decrypting or authenticating the data before
|
* is not responsible for decrypting or authenticating the data before
|
||||||
* returning it.
|
* returning it.
|
||||||
*/
|
*/
|
||||||
public interface SimplexSegmentedTransportReader {
|
public interface SimplexSegmentedTransportReader extends FrameSource {
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads a frame into the given buffer and returns its length, or -1 if no
|
|
||||||
* more frames can be read.
|
|
||||||
*/
|
|
||||||
int readFrame(byte[] b) throws IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Closes the reader and disposes of any associated resources. The first
|
* Closes the reader and disposes of any associated resources. The first
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package net.sf.briar.api.plugins.simplex;
|
package net.sf.briar.api.plugins.simplex;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An interface for writing data to a simplex segmented transport. The writer is
|
* An interface for writing data to a simplex segmented transport. The writer is
|
||||||
* not responsible for authenticating or encrypting the data before writing it.
|
* not responsible for authenticating or encrypting the data before writing it.
|
||||||
@@ -11,9 +9,6 @@ public interface SimplexSegmentedTransportWriter {
|
|||||||
/** Returns the capacity of the transport in bytes. */
|
/** Returns the capacity of the transport in bytes. */
|
||||||
long getCapacity();
|
long getCapacity();
|
||||||
|
|
||||||
/** Writes the given frame to the transport. */
|
|
||||||
void writeFrame(byte[] b, int len) throws IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the output stream should be flushed after each packet.
|
* Returns true if the output stream should be flushed after each packet.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,13 +1,100 @@
|
|||||||
package net.sf.briar.transport;
|
package net.sf.briar.transport;
|
||||||
|
|
||||||
|
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.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
||||||
|
|
||||||
|
import java.io.EOFException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.security.GeneralSecurityException;
|
||||||
|
|
||||||
/** Decrypts unauthenticated data received over a connection. */
|
import javax.crypto.Cipher;
|
||||||
interface ConnectionDecrypter {
|
import javax.crypto.spec.IvParameterSpec;
|
||||||
|
|
||||||
/**
|
import net.sf.briar.api.FormatException;
|
||||||
* Reads and decrypts a frame into the given buffer and returns the length
|
import net.sf.briar.api.crypto.ErasableKey;
|
||||||
* of the decrypted frame, or -1 if no more frames can be read.
|
import net.sf.briar.api.plugins.FrameSource;
|
||||||
*/
|
|
||||||
int readFrame(byte[] b) throws IOException;
|
class ConnectionDecrypter implements FrameSource {
|
||||||
|
|
||||||
|
private final InputStream in;
|
||||||
|
private final Cipher frameCipher;
|
||||||
|
private final ErasableKey frameKey;
|
||||||
|
private final int macLength, blockSize;
|
||||||
|
private final byte[] iv;
|
||||||
|
|
||||||
|
private long frame = 0L;
|
||||||
|
|
||||||
|
ConnectionDecrypter(InputStream in, Cipher frameCipher,
|
||||||
|
ErasableKey frameKey, int macLength) {
|
||||||
|
this.in = in;
|
||||||
|
this.frameCipher = frameCipher;
|
||||||
|
this.frameKey = frameKey;
|
||||||
|
this.macLength = macLength;
|
||||||
|
blockSize = frameCipher.getBlockSize();
|
||||||
|
if(blockSize < FRAME_HEADER_LENGTH)
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
iv = IvEncoder.encodeIv(0, blockSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int readFrame(byte[] b) throws IOException {
|
||||||
|
if(b.length < MAX_FRAME_LENGTH) throw new IllegalArgumentException();
|
||||||
|
if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
|
||||||
|
// Initialise the cipher
|
||||||
|
IvEncoder.updateIv(iv, frame);
|
||||||
|
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||||
|
try {
|
||||||
|
frameCipher.init(Cipher.DECRYPT_MODE, frameKey, ivSpec);
|
||||||
|
} catch(GeneralSecurityException badIvOrKey) {
|
||||||
|
throw new RuntimeException(badIvOrKey);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
// Read the first block
|
||||||
|
int offset = 0;
|
||||||
|
while(offset < blockSize) {
|
||||||
|
int read = in.read(b, offset, blockSize - offset);
|
||||||
|
if(read == -1) {
|
||||||
|
if(offset == 0) return -1;
|
||||||
|
if(offset < blockSize) throw new EOFException();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
offset += read;
|
||||||
|
}
|
||||||
|
// Decrypt the first block
|
||||||
|
try {
|
||||||
|
int decrypted = frameCipher.update(b, 0, blockSize, b);
|
||||||
|
assert decrypted == blockSize;
|
||||||
|
} catch(GeneralSecurityException badCipher) {
|
||||||
|
throw new RuntimeException(badCipher);
|
||||||
|
}
|
||||||
|
// Validate and parse the header
|
||||||
|
int max = MAX_FRAME_LENGTH - FRAME_HEADER_LENGTH - macLength;
|
||||||
|
if(!HeaderEncoder.validateHeader(b, frame, max))
|
||||||
|
throw new FormatException();
|
||||||
|
int payload = HeaderEncoder.getPayloadLength(b);
|
||||||
|
int padding = HeaderEncoder.getPaddingLength(b);
|
||||||
|
int length = FRAME_HEADER_LENGTH + payload + padding + macLength;
|
||||||
|
if(length > MAX_FRAME_LENGTH) throw new FormatException();
|
||||||
|
// Read the remainder of the frame
|
||||||
|
while(offset < length) {
|
||||||
|
int read = in.read(b, offset, length - offset);
|
||||||
|
if(read == -1) throw new EOFException();
|
||||||
|
offset += read;
|
||||||
|
}
|
||||||
|
// Decrypt the remainder of the frame
|
||||||
|
try {
|
||||||
|
int decrypted = frameCipher.doFinal(b, blockSize,
|
||||||
|
length - blockSize, b, blockSize);
|
||||||
|
assert decrypted == length - blockSize;
|
||||||
|
} catch(GeneralSecurityException badCipher) {
|
||||||
|
throw new RuntimeException(badCipher);
|
||||||
|
}
|
||||||
|
frame++;
|
||||||
|
return length;
|
||||||
|
} catch(IOException e) {
|
||||||
|
frameKey.erase();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -2,11 +2,10 @@ package net.sf.briar.transport;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
/** Encrypts authenticated data to be sent over a connection. */
|
import net.sf.briar.api.plugins.FrameSink;
|
||||||
interface ConnectionEncrypter {
|
|
||||||
|
|
||||||
/** Encrypts and writes the given frame. */
|
/** Encrypts authenticated data to be sent over a connection. */
|
||||||
void writeFrame(byte[] b, int len) throws IOException;
|
interface ConnectionEncrypter extends FrameSink {
|
||||||
|
|
||||||
/** Flushes the output stream. */
|
/** Flushes the output stream. */
|
||||||
void flush() throws IOException;
|
void flush() throws IOException;
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
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;
|
||||||
@@ -32,33 +31,37 @@ class ConnectionEncrypterImpl implements ConnectionEncrypter {
|
|||||||
// Encrypt the tag
|
// Encrypt the tag
|
||||||
tag = TagEncoder.encodeTag(0, tagCipher, tagKey);
|
tag = TagEncoder.encodeTag(0, tagCipher, tagKey);
|
||||||
tagKey.erase();
|
tagKey.erase();
|
||||||
if(tag.length != TAG_LENGTH) throw new IllegalArgumentException();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeFrame(byte[] b, int len) throws IOException {
|
public void writeFrame(byte[] b, int len) throws IOException {
|
||||||
try {
|
if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
|
||||||
if(!tagWritten) {
|
if(!tagWritten) {
|
||||||
out.write(tag);
|
|
||||||
capacity -= tag.length;
|
|
||||||
tagWritten = true;
|
|
||||||
}
|
|
||||||
if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
|
|
||||||
IvEncoder.updateIv(iv, frame);
|
|
||||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
|
||||||
try {
|
try {
|
||||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
out.write(tag);
|
||||||
int encrypted = frameCipher.doFinal(b, 0, len, b, 0);
|
} catch(IOException e) {
|
||||||
assert encrypted == len;
|
frameKey.erase();
|
||||||
} catch(GeneralSecurityException badCipher) {
|
throw e;
|
||||||
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);
|
||||||
|
assert encrypted == len;
|
||||||
|
} 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();
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
capacity -= len;
|
||||||
|
frame++;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void flush() throws IOException {
|
public void flush() throws IOException {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import javax.crypto.Mac;
|
|||||||
|
|
||||||
import net.sf.briar.api.crypto.CryptoComponent;
|
import net.sf.briar.api.crypto.CryptoComponent;
|
||||||
import net.sf.briar.api.crypto.ErasableKey;
|
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.ConnectionReader;
|
||||||
import net.sf.briar.api.transport.ConnectionReaderFactory;
|
import net.sf.briar.api.transport.ConnectionReaderFactory;
|
||||||
import net.sf.briar.util.ByteUtils;
|
import net.sf.briar.util.ByteUtils;
|
||||||
@@ -47,7 +48,7 @@ class ConnectionReaderFactoryImpl implements ConnectionReaderFactory {
|
|||||||
// Create the decrypter
|
// Create the decrypter
|
||||||
Cipher frameCipher = crypto.getFrameCipher();
|
Cipher frameCipher = crypto.getFrameCipher();
|
||||||
Mac mac = crypto.getMac();
|
Mac mac = crypto.getMac();
|
||||||
ConnectionDecrypter decrypter = new ConnectionDecrypterImpl(in,
|
FrameSource decrypter = new ConnectionDecrypter(in,
|
||||||
frameCipher, frameKey, mac.getMacLength());
|
frameCipher, frameKey, mac.getMacLength());
|
||||||
// Create the reader
|
// Create the reader
|
||||||
return new ConnectionReaderImpl(decrypter, mac, macKey);
|
return new ConnectionReaderImpl(decrypter, mac, macKey);
|
||||||
|
|||||||
@@ -12,11 +12,12 @@ import javax.crypto.Mac;
|
|||||||
|
|
||||||
import net.sf.briar.api.FormatException;
|
import net.sf.briar.api.FormatException;
|
||||||
import net.sf.briar.api.crypto.ErasableKey;
|
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.ConnectionReader;
|
||||||
|
|
||||||
class ConnectionReaderImpl extends InputStream implements ConnectionReader {
|
class ConnectionReaderImpl extends InputStream implements ConnectionReader {
|
||||||
|
|
||||||
private final ConnectionDecrypter decrypter;
|
private final FrameSource decrypter;
|
||||||
private final Mac mac;
|
private final Mac mac;
|
||||||
private final int macLength;
|
private final int macLength;
|
||||||
private final byte[] buf;
|
private final byte[] buf;
|
||||||
@@ -24,8 +25,7 @@ class ConnectionReaderImpl extends InputStream implements ConnectionReader {
|
|||||||
private long frame = 0L;
|
private long frame = 0L;
|
||||||
private int bufOffset = 0, bufLength = 0;
|
private int bufOffset = 0, bufLength = 0;
|
||||||
|
|
||||||
ConnectionReaderImpl(ConnectionDecrypter decrypter, Mac mac,
|
ConnectionReaderImpl(FrameSource decrypter, Mac mac, ErasableKey macKey) {
|
||||||
ErasableKey macKey) {
|
|
||||||
this.decrypter = decrypter;
|
this.decrypter = decrypter;
|
||||||
this.mac = mac;
|
this.mac = mac;
|
||||||
// Initialise the MAC
|
// Initialise the MAC
|
||||||
|
|||||||
@@ -4,9 +4,7 @@ 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.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
import static net.sf.briar.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
||||||
|
|
||||||
import java.io.EOFException;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.security.GeneralSecurityException;
|
import java.security.GeneralSecurityException;
|
||||||
|
|
||||||
import javax.crypto.Cipher;
|
import javax.crypto.Cipher;
|
||||||
@@ -14,10 +12,11 @@ import javax.crypto.spec.IvParameterSpec;
|
|||||||
|
|
||||||
import net.sf.briar.api.FormatException;
|
import net.sf.briar.api.FormatException;
|
||||||
import net.sf.briar.api.crypto.ErasableKey;
|
import net.sf.briar.api.crypto.ErasableKey;
|
||||||
|
import net.sf.briar.api.plugins.FrameSource;
|
||||||
|
|
||||||
class ConnectionDecrypterImpl implements ConnectionDecrypter {
|
class SegmentedConnectionDecrypter implements FrameSource {
|
||||||
|
|
||||||
private final InputStream in;
|
private final FrameSource in;
|
||||||
private final Cipher frameCipher;
|
private final Cipher frameCipher;
|
||||||
private final ErasableKey frameKey;
|
private final ErasableKey frameKey;
|
||||||
private final int macLength, blockSize;
|
private final int macLength, blockSize;
|
||||||
@@ -25,7 +24,7 @@ class ConnectionDecrypterImpl implements ConnectionDecrypter {
|
|||||||
|
|
||||||
private long frame = 0L;
|
private long frame = 0L;
|
||||||
|
|
||||||
ConnectionDecrypterImpl(InputStream in, Cipher frameCipher,
|
SegmentedConnectionDecrypter(FrameSource in, Cipher frameCipher,
|
||||||
ErasableKey frameKey, int macLength) {
|
ErasableKey frameKey, int macLength) {
|
||||||
this.in = in;
|
this.in = in;
|
||||||
this.frameCipher = frameCipher;
|
this.frameCipher = frameCipher;
|
||||||
@@ -49,21 +48,16 @@ class ConnectionDecrypterImpl implements ConnectionDecrypter {
|
|||||||
throw new RuntimeException(badIvOrKey);
|
throw new RuntimeException(badIvOrKey);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
// Read the first block
|
// Read the frame
|
||||||
int offset = 0;
|
int length = in.readFrame(b);
|
||||||
while(offset < blockSize) {
|
if(length == -1) return -1;
|
||||||
int read = in.read(b, offset, blockSize - offset);
|
if(length > MAX_FRAME_LENGTH) throw new FormatException();
|
||||||
if(read == -1) {
|
if(length < FRAME_HEADER_LENGTH + macLength)
|
||||||
if(offset == 0) return -1;
|
throw new FormatException();
|
||||||
if(offset < blockSize) throw new EOFException();
|
// Decrypt the frame
|
||||||
break;
|
|
||||||
}
|
|
||||||
offset += read;
|
|
||||||
}
|
|
||||||
// Decrypt the first block
|
|
||||||
try {
|
try {
|
||||||
int decrypted = frameCipher.update(b, 0, blockSize, b);
|
int decrypted = frameCipher.update(b, 0, length, b);
|
||||||
assert decrypted == blockSize;
|
assert decrypted == length;
|
||||||
} catch(GeneralSecurityException badCipher) {
|
} catch(GeneralSecurityException badCipher) {
|
||||||
throw new RuntimeException(badCipher);
|
throw new RuntimeException(badCipher);
|
||||||
}
|
}
|
||||||
@@ -73,22 +67,8 @@ class ConnectionDecrypterImpl implements ConnectionDecrypter {
|
|||||||
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);
|
||||||
int length = FRAME_HEADER_LENGTH + payload + padding + macLength;
|
if(length != FRAME_HEADER_LENGTH + payload + padding + macLength)
|
||||||
if(length > MAX_FRAME_LENGTH) throw new FormatException();
|
throw new FormatException();
|
||||||
// Read the remainder of the frame
|
|
||||||
while(offset < length) {
|
|
||||||
int read = in.read(b, offset, length - offset);
|
|
||||||
if(read == -1) throw new EOFException();
|
|
||||||
offset += read;
|
|
||||||
}
|
|
||||||
// Decrypt the remainder of the frame
|
|
||||||
try {
|
|
||||||
int decrypted = frameCipher.doFinal(b, blockSize,
|
|
||||||
length - blockSize, b, blockSize);
|
|
||||||
assert decrypted == length - blockSize;
|
|
||||||
} catch(GeneralSecurityException badCipher) {
|
|
||||||
throw new RuntimeException(badCipher);
|
|
||||||
}
|
|
||||||
frame++;
|
frame++;
|
||||||
return length;
|
return length;
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package net.sf.briar.transport;
|
||||||
|
|
||||||
|
import static net.sf.briar.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.security.GeneralSecurityException;
|
||||||
|
|
||||||
|
import javax.crypto.Cipher;
|
||||||
|
import javax.crypto.spec.IvParameterSpec;
|
||||||
|
|
||||||
|
import net.sf.briar.api.crypto.ErasableKey;
|
||||||
|
import net.sf.briar.api.plugins.FrameSink;
|
||||||
|
|
||||||
|
class SegmentedConnectionEncrypter implements ConnectionEncrypter {
|
||||||
|
|
||||||
|
private final FrameSink out;
|
||||||
|
private final Cipher frameCipher;
|
||||||
|
private final ErasableKey frameKey;
|
||||||
|
private final byte[] iv, tag;
|
||||||
|
|
||||||
|
private long capacity, frame = 0L;
|
||||||
|
private boolean tagWritten = false;
|
||||||
|
|
||||||
|
SegmentedConnectionEncrypter(FrameSink out, long capacity, Cipher tagCipher,
|
||||||
|
Cipher frameCipher, ErasableKey tagKey, ErasableKey frameKey) {
|
||||||
|
this.out = out;
|
||||||
|
this.capacity = capacity;
|
||||||
|
this.frameCipher = frameCipher;
|
||||||
|
this.frameKey = frameKey;
|
||||||
|
iv = IvEncoder.encodeIv(0, frameCipher.getBlockSize());
|
||||||
|
// Encrypt the tag
|
||||||
|
tag = TagEncoder.encodeTag(0, tagCipher, tagKey);
|
||||||
|
tagKey.erase();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void writeFrame(byte[] b, int len) throws IOException {
|
||||||
|
if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
|
||||||
|
int offset = 0;
|
||||||
|
if(!tagWritten) {
|
||||||
|
if(tag.length + len > b.length)
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
System.arraycopy(b, 0, b, tag.length, len);
|
||||||
|
System.arraycopy(tag, 0, b, 0, tag.length);
|
||||||
|
capacity -= tag.length;
|
||||||
|
tagWritten = true;
|
||||||
|
offset = tag.length;
|
||||||
|
}
|
||||||
|
IvEncoder.updateIv(iv, frame);
|
||||||
|
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||||
|
try {
|
||||||
|
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||||
|
int encrypted = frameCipher.doFinal(b, offset, len, b, offset);
|
||||||
|
assert encrypted == len;
|
||||||
|
} catch(GeneralSecurityException badCipher) {
|
||||||
|
throw new RuntimeException(badCipher);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
out.writeFrame(b, offset + len);
|
||||||
|
} catch(IOException e) {
|
||||||
|
frameKey.erase();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
capacity -= len;
|
||||||
|
frame++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void flush() throws IOException {}
|
||||||
|
|
||||||
|
public long getRemainingCapacity() {
|
||||||
|
return capacity;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -55,7 +55,7 @@ public class CounterModeTest extends BriarTestCase {
|
|||||||
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
||||||
byte[] ciphertext =
|
byte[] ciphertext =
|
||||||
new byte[cipher.getOutputSize(plaintext.length)];
|
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));
|
ciphertexts.add(new Bytes(ciphertext));
|
||||||
}
|
}
|
||||||
// All the ciphertexts should be distinct using Arrays.equals()
|
// 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);
|
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
||||||
byte[] ciphertext =
|
byte[] ciphertext =
|
||||||
new byte[cipher.getOutputSize(plaintext.length)];
|
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));
|
ciphertexts.add(new Bytes(ciphertext));
|
||||||
}
|
}
|
||||||
assertEquals(1, ciphertexts.size());
|
assertEquals(1, ciphertexts.size());
|
||||||
@@ -98,7 +98,7 @@ public class CounterModeTest extends BriarTestCase {
|
|||||||
Cipher cipher = Cipher.getInstance(CIPHER_MODE, PROVIDER);
|
Cipher cipher = Cipher.getInstance(CIPHER_MODE, PROVIDER);
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
||||||
byte[] ciphertext = new byte[cipher.getOutputSize(plaintext.length)];
|
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
|
// Make sure the IV array hasn't been modified
|
||||||
assertEquals(0, ivBytes[BLOCK_SIZE_BYTES - 2]);
|
assertEquals(0, ivBytes[BLOCK_SIZE_BYTES - 2]);
|
||||||
assertEquals(0, ivBytes[BLOCK_SIZE_BYTES - 1]);
|
assertEquals(0, ivBytes[BLOCK_SIZE_BYTES - 1]);
|
||||||
@@ -109,7 +109,7 @@ public class CounterModeTest extends BriarTestCase {
|
|||||||
cipher = Cipher.getInstance(CIPHER_MODE, PROVIDER);
|
cipher = Cipher.getInstance(CIPHER_MODE, PROVIDER);
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
||||||
byte[] ciphertext1 = new byte[cipher.getOutputSize(plaintext.length)];
|
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 last nine blocks of the first ciphertext should be identical to
|
||||||
// the first nine blocks of the second ciphertext
|
// the first nine blocks of the second ciphertext
|
||||||
for(int i = 0; i < BLOCK_SIZE_BYTES * 9; i++) {
|
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 cipher = Cipher.getInstance(CIPHER_MODE, PROVIDER);
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
||||||
byte[] ciphertext = new byte[cipher.getOutputSize(plaintext.length)];
|
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
|
// Make sure the IV array hasn't been modified
|
||||||
assertEquals(0, ivBytes[BLOCK_SIZE_BYTES - 3]);
|
assertEquals(0, ivBytes[BLOCK_SIZE_BYTES - 3]);
|
||||||
assertEquals((byte) 255, ivBytes[BLOCK_SIZE_BYTES - 2]);
|
assertEquals((byte) 255, ivBytes[BLOCK_SIZE_BYTES - 2]);
|
||||||
@@ -146,7 +146,7 @@ public class CounterModeTest extends BriarTestCase {
|
|||||||
cipher = Cipher.getInstance(CIPHER_MODE, PROVIDER);
|
cipher = Cipher.getInstance(CIPHER_MODE, PROVIDER);
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
||||||
byte[] ciphertext1 = new byte[cipher.getOutputSize(plaintext.length)];
|
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 last nine blocks of the first ciphertext should be identical to
|
||||||
// the first nine blocks of the second ciphertext
|
// the first nine blocks of the second ciphertext
|
||||||
for(int i = 0; i < BLOCK_SIZE_BYTES * 9; i++) {
|
for(int i = 0; i < BLOCK_SIZE_BYTES * 9; i++) {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import javax.crypto.spec.IvParameterSpec;
|
|||||||
import net.sf.briar.BriarTestCase;
|
import net.sf.briar.BriarTestCase;
|
||||||
import net.sf.briar.api.crypto.CryptoComponent;
|
import net.sf.briar.api.crypto.CryptoComponent;
|
||||||
import net.sf.briar.api.crypto.ErasableKey;
|
import net.sf.briar.api.crypto.ErasableKey;
|
||||||
|
import net.sf.briar.api.plugins.FrameSource;
|
||||||
import net.sf.briar.crypto.CryptoModule;
|
import net.sf.briar.crypto.CryptoModule;
|
||||||
|
|
||||||
import org.apache.commons.io.output.ByteArrayOutputStream;
|
import org.apache.commons.io.output.ByteArrayOutputStream;
|
||||||
@@ -67,16 +68,16 @@ public class ConnectionDecrypterImplTest 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
|
||||||
ConnectionDecrypter d = new ConnectionDecrypterImpl(in, frameCipher,
|
FrameSource decrypter = new ConnectionDecrypter(in, frameCipher,
|
||||||
frameKey, MAC_LENGTH);
|
frameKey, MAC_LENGTH);
|
||||||
// First frame
|
// First frame
|
||||||
byte[] decrypted = new byte[MAX_FRAME_LENGTH];
|
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++) {
|
for(int i = 0; i < plaintext.length; i++) {
|
||||||
assertEquals(plaintext[i], decrypted[i]);
|
assertEquals(plaintext[i], decrypted[i]);
|
||||||
}
|
}
|
||||||
// Second frame
|
// Second frame
|
||||||
assertEquals(plaintext1.length, d.readFrame(decrypted));
|
assertEquals(plaintext1.length, decrypter.readFrame(decrypted));
|
||||||
for(int i = 0; i < plaintext1.length; i++) {
|
for(int i = 0; i < plaintext1.length; i++) {
|
||||||
assertEquals(plaintext1[i], decrypted[i]);
|
assertEquals(plaintext1[i], decrypted[i]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import java.io.ByteArrayInputStream;
|
|||||||
|
|
||||||
import net.sf.briar.TestUtils;
|
import net.sf.briar.TestUtils;
|
||||||
import net.sf.briar.api.FormatException;
|
import net.sf.briar.api.FormatException;
|
||||||
|
import net.sf.briar.api.plugins.FrameSource;
|
||||||
import net.sf.briar.api.transport.ConnectionReader;
|
import net.sf.briar.api.transport.ConnectionReader;
|
||||||
|
|
||||||
import org.apache.commons.io.output.ByteArrayOutputStream;
|
import org.apache.commons.io.output.ByteArrayOutputStream;
|
||||||
@@ -31,7 +32,7 @@ public class ConnectionReaderImplTest extends TransportTest {
|
|||||||
mac.doFinal(frame, FRAME_HEADER_LENGTH + payloadLength);
|
mac.doFinal(frame, FRAME_HEADER_LENGTH + payloadLength);
|
||||||
// Read the frame
|
// Read the frame
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(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);
|
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
|
||||||
// There should be no bytes available before EOF
|
// There should be no bytes available before EOF
|
||||||
assertEquals(-1, r.getInputStream().read());
|
assertEquals(-1, r.getInputStream().read());
|
||||||
@@ -49,7 +50,7 @@ public class ConnectionReaderImplTest extends TransportTest {
|
|||||||
mac.doFinal(frame, FRAME_HEADER_LENGTH + payloadLength);
|
mac.doFinal(frame, FRAME_HEADER_LENGTH + payloadLength);
|
||||||
// Read the frame
|
// Read the frame
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(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);
|
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
|
||||||
// There should be one byte available before EOF
|
// There should be one byte available before EOF
|
||||||
assertEquals(0, r.getInputStream().read());
|
assertEquals(0, r.getInputStream().read());
|
||||||
@@ -75,7 +76,7 @@ public class ConnectionReaderImplTest extends TransportTest {
|
|||||||
out.write(frame1);
|
out.write(frame1);
|
||||||
// Read the first frame
|
// Read the first frame
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
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);
|
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
|
||||||
byte[] read = new byte[maxPayloadLength];
|
byte[] read = new byte[maxPayloadLength];
|
||||||
TestUtils.readFully(r.getInputStream(), read);
|
TestUtils.readFully(r.getInputStream(), read);
|
||||||
@@ -109,7 +110,7 @@ public class ConnectionReaderImplTest extends TransportTest {
|
|||||||
out.write(frame1);
|
out.write(frame1);
|
||||||
// Read the first frame
|
// Read the first frame
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
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);
|
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
|
||||||
byte[] read = new byte[maxPayloadLength - paddingLength];
|
byte[] read = new byte[maxPayloadLength - paddingLength];
|
||||||
TestUtils.readFully(r.getInputStream(), read);
|
TestUtils.readFully(r.getInputStream(), read);
|
||||||
@@ -135,7 +136,7 @@ public class ConnectionReaderImplTest extends TransportTest {
|
|||||||
mac.doFinal(frame, FRAME_HEADER_LENGTH + payloadLength + paddingLength);
|
mac.doFinal(frame, FRAME_HEADER_LENGTH + payloadLength + paddingLength);
|
||||||
// Read the frame
|
// Read the frame
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(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);
|
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
|
||||||
// The non-zero padding should be rejected
|
// The non-zero padding should be rejected
|
||||||
try {
|
try {
|
||||||
@@ -167,7 +168,7 @@ public class ConnectionReaderImplTest extends TransportTest {
|
|||||||
out.write(frame1);
|
out.write(frame1);
|
||||||
// Read the frames
|
// Read the frames
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
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);
|
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
|
||||||
byte[] read = new byte[payloadLength];
|
byte[] read = new byte[payloadLength];
|
||||||
TestUtils.readFully(r.getInputStream(), read);
|
TestUtils.readFully(r.getInputStream(), read);
|
||||||
@@ -191,7 +192,7 @@ public class ConnectionReaderImplTest extends TransportTest {
|
|||||||
frame[12] ^= 1;
|
frame[12] ^= 1;
|
||||||
// Try to read the frame - not a single byte should be read
|
// Try to read the frame - not a single byte should be read
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(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);
|
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
|
||||||
try {
|
try {
|
||||||
r.getInputStream().read();
|
r.getInputStream().read();
|
||||||
@@ -213,7 +214,7 @@ public class ConnectionReaderImplTest extends TransportTest {
|
|||||||
frame[17] ^= 1;
|
frame[17] ^= 1;
|
||||||
// Try to read the frame - not a single byte should be read
|
// Try to read the frame - not a single byte should be read
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(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);
|
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
|
||||||
try {
|
try {
|
||||||
r.getInputStream().read();
|
r.getInputStream().read();
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import javax.crypto.Mac;
|
|||||||
import net.sf.briar.BriarTestCase;
|
import net.sf.briar.BriarTestCase;
|
||||||
import net.sf.briar.api.crypto.CryptoComponent;
|
import net.sf.briar.api.crypto.CryptoComponent;
|
||||||
import net.sf.briar.api.crypto.ErasableKey;
|
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.ConnectionReader;
|
||||||
import net.sf.briar.api.transport.ConnectionWriter;
|
import net.sf.briar.api.transport.ConnectionWriter;
|
||||||
import net.sf.briar.crypto.CryptoModule;
|
import net.sf.briar.crypto.CryptoModule;
|
||||||
@@ -89,8 +90,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
|
||||||
ConnectionDecrypter decrypter = new ConnectionDecrypterImpl(in,
|
FrameSource decrypter = new ConnectionDecrypter(in, frameCipher,
|
||||||
frameCipher, frameKey, mac.getMacLength());
|
frameKey, mac.getMacLength());
|
||||||
ConnectionReader reader = new ConnectionReaderImpl(decrypter, mac,
|
ConnectionReader reader = new ConnectionReaderImpl(decrypter, mac,
|
||||||
macKey);
|
macKey);
|
||||||
InputStream in1 = reader.getInputStream();
|
InputStream in1 = reader.getInputStream();
|
||||||
|
|||||||
@@ -8,9 +8,10 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
import net.sf.briar.api.FormatException;
|
import net.sf.briar.api.FormatException;
|
||||||
|
import net.sf.briar.api.plugins.FrameSource;
|
||||||
|
|
||||||
/** A ConnectionDecrypter that performs no decryption. */
|
/** A connection decrypter that performs no decryption. */
|
||||||
class NullConnectionDecrypter implements ConnectionDecrypter {
|
class NullConnectionDecrypter implements FrameSource {
|
||||||
|
|
||||||
private final InputStream in;
|
private final InputStream in;
|
||||||
private final int macLength;
|
private final int macLength;
|
||||||
|
|||||||
Reference in New Issue
Block a user