Encrypter and decrypter for segmented transports (untested).

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

View File

@@ -1,13 +1,100 @@
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.InputStream;
import java.security.GeneralSecurityException;
/** Decrypts unauthenticated data received over a connection. */
interface ConnectionDecrypter {
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
/**
* Reads and decrypts a frame into the given buffer and returns the length
* of the decrypted frame, or -1 if no more frames can be read.
*/
int readFrame(byte[] b) throws IOException;
}
import net.sf.briar.api.FormatException;
import net.sf.briar.api.crypto.ErasableKey;
import net.sf.briar.api.plugins.FrameSource;
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;
}
}
}

View File

@@ -2,11 +2,10 @@ package net.sf.briar.transport;
import java.io.IOException;
/** Encrypts authenticated data to be sent over a connection. */
interface ConnectionEncrypter {
import net.sf.briar.api.plugins.FrameSink;
/** Encrypts and writes the given frame. */
void writeFrame(byte[] b, int len) throws IOException;
/** Encrypts authenticated data to be sent over a connection. */
interface ConnectionEncrypter extends FrameSink {
/** Flushes the output stream. */
void flush() throws IOException;

View File

@@ -1,6 +1,5 @@
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.io.IOException;
@@ -32,33 +31,37 @@ class ConnectionEncrypterImpl implements ConnectionEncrypter {
// Encrypt the tag
tag = TagEncoder.encodeTag(0, tagCipher, tagKey);
tagKey.erase();
if(tag.length != TAG_LENGTH) throw new IllegalArgumentException();
}
public void writeFrame(byte[] b, int len) throws IOException {
try {
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);
if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
if(!tagWritten) {
try {
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
int encrypted = frameCipher.doFinal(b, 0, len, b, 0);
assert encrypted == len;
} catch(GeneralSecurityException badCipher) {
throw new RuntimeException(badCipher);
out.write(tag);
} catch(IOException e) {
frameKey.erase();
throw e;
}
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);
capacity -= len;
frame++;
} catch(IOException e) {
frameKey.erase();
throw e;
}
capacity -= len;
frame++;
}
public void flush() throws IOException {

View File

@@ -7,6 +7,7 @@ import javax.crypto.Mac;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.ErasableKey;
import net.sf.briar.api.plugins.FrameSource;
import net.sf.briar.api.transport.ConnectionReader;
import net.sf.briar.api.transport.ConnectionReaderFactory;
import net.sf.briar.util.ByteUtils;
@@ -47,7 +48,7 @@ class ConnectionReaderFactoryImpl implements ConnectionReaderFactory {
// Create the decrypter
Cipher frameCipher = crypto.getFrameCipher();
Mac mac = crypto.getMac();
ConnectionDecrypter decrypter = new ConnectionDecrypterImpl(in,
FrameSource decrypter = new ConnectionDecrypter(in,
frameCipher, frameKey, mac.getMacLength());
// Create the reader
return new ConnectionReaderImpl(decrypter, mac, macKey);

View File

@@ -12,11 +12,12 @@ import javax.crypto.Mac;
import net.sf.briar.api.FormatException;
import net.sf.briar.api.crypto.ErasableKey;
import net.sf.briar.api.plugins.FrameSource;
import net.sf.briar.api.transport.ConnectionReader;
class ConnectionReaderImpl extends InputStream implements ConnectionReader {
private final ConnectionDecrypter decrypter;
private final FrameSource decrypter;
private final Mac mac;
private final int macLength;
private final byte[] buf;
@@ -24,8 +25,7 @@ class ConnectionReaderImpl extends InputStream implements ConnectionReader {
private long frame = 0L;
private int bufOffset = 0, bufLength = 0;
ConnectionReaderImpl(ConnectionDecrypter decrypter, Mac mac,
ErasableKey macKey) {
ConnectionReaderImpl(FrameSource decrypter, Mac mac, ErasableKey macKey) {
this.decrypter = decrypter;
this.mac = mac;
// Initialise the MAC

View File

@@ -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.util.ByteUtils.MAX_32_BIT_UNSIGNED;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import javax.crypto.Cipher;
@@ -14,10 +12,11 @@ import javax.crypto.spec.IvParameterSpec;
import net.sf.briar.api.FormatException;
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 ErasableKey frameKey;
private final int macLength, blockSize;
@@ -25,7 +24,7 @@ class ConnectionDecrypterImpl implements ConnectionDecrypter {
private long frame = 0L;
ConnectionDecrypterImpl(InputStream in, Cipher frameCipher,
SegmentedConnectionDecrypter(FrameSource in, Cipher frameCipher,
ErasableKey frameKey, int macLength) {
this.in = in;
this.frameCipher = frameCipher;
@@ -49,21 +48,16 @@ class ConnectionDecrypterImpl implements ConnectionDecrypter {
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
// Read the frame
int length = in.readFrame(b);
if(length == -1) return -1;
if(length > MAX_FRAME_LENGTH) throw new FormatException();
if(length < FRAME_HEADER_LENGTH + macLength)
throw new FormatException();
// Decrypt the frame
try {
int decrypted = frameCipher.update(b, 0, blockSize, b);
assert decrypted == blockSize;
int decrypted = frameCipher.update(b, 0, length, b);
assert decrypted == length;
} catch(GeneralSecurityException badCipher) {
throw new RuntimeException(badCipher);
}
@@ -73,22 +67,8 @@ class ConnectionDecrypterImpl implements ConnectionDecrypter {
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);
}
if(length != FRAME_HEADER_LENGTH + payload + padding + macLength)
throw new FormatException();
frame++;
return length;
} catch(IOException e) {
@@ -96,4 +76,4 @@ class ConnectionDecrypterImpl implements ConnectionDecrypter {
throw e;
}
}
}
}

View File

@@ -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;
}
}