Use the same maximum frame length for all transports.

This commit is contained in:
akwizgran
2015-01-05 16:24:44 +00:00
parent 358166bc12
commit d3bf2d59a1
60 changed files with 194 additions and 321 deletions

View File

@@ -20,23 +20,21 @@ class StreamDecrypterFactoryImpl implements StreamDecrypterFactory {
}
public StreamDecrypter createStreamDecrypter(InputStream in,
int maxFrameLength, StreamContext ctx) {
StreamContext ctx) {
byte[] secret = ctx.getSecret();
long streamNumber = ctx.getStreamNumber();
boolean alice = !ctx.getAlice();
// Derive the frame key
SecretKey frameKey = crypto.deriveFrameKey(secret, streamNumber, alice);
// Create the decrypter
return new StreamDecrypterImpl(in, crypto.getFrameCipher(), frameKey,
maxFrameLength);
return new StreamDecrypterImpl(in, crypto.getFrameCipher(), frameKey);
}
public StreamDecrypter createInvitationStreamDecrypter(InputStream in,
int maxFrameLength, byte[] secret, boolean alice) {
byte[] secret, boolean alice) {
// Derive the frame key
SecretKey frameKey = crypto.deriveFrameKey(secret, 0, alice);
// Create the decrypter
return new StreamDecrypterImpl(in, crypto.getFrameCipher(), frameKey,
maxFrameLength);
return new StreamDecrypterImpl(in, crypto.getFrameCipher(), frameKey);
}
}

View File

@@ -4,6 +4,7 @@ import static org.briarproject.api.transport.TransportConstants.AAD_LENGTH;
import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.IV_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import java.io.EOFException;
import java.io.IOException;
@@ -21,21 +22,19 @@ class StreamDecrypterImpl implements StreamDecrypter {
private final AuthenticatedCipher frameCipher;
private final SecretKey frameKey;
private final byte[] iv, aad, plaintext, ciphertext;
private final int frameLength;
private long frameNumber;
private boolean finalFrame;
StreamDecrypterImpl(InputStream in, AuthenticatedCipher frameCipher,
SecretKey frameKey, int frameLength) {
SecretKey frameKey) {
this.in = in;
this.frameCipher = frameCipher;
this.frameKey = frameKey;
this.frameLength = frameLength;
iv = new byte[IV_LENGTH];
aad = new byte[AAD_LENGTH];
plaintext = new byte[frameLength - MAC_LENGTH];
ciphertext = new byte[frameLength];
plaintext = new byte[MAX_FRAME_LENGTH - MAC_LENGTH];
ciphertext = new byte[MAX_FRAME_LENGTH];
frameNumber = 0;
finalFrame = false;
}
@@ -44,9 +43,9 @@ class StreamDecrypterImpl implements StreamDecrypter {
if(finalFrame) return -1;
// Read the frame
int ciphertextLength = 0;
while(ciphertextLength < frameLength) {
while(ciphertextLength < MAX_FRAME_LENGTH) {
int read = in.read(ciphertext, ciphertextLength,
frameLength - ciphertextLength);
MAX_FRAME_LENGTH - ciphertextLength);
if(read == -1) break; // We'll check the length later
ciphertextLength += read;
}
@@ -65,7 +64,7 @@ class StreamDecrypterImpl implements StreamDecrypter {
}
// Decode and validate the header
finalFrame = FrameEncoder.isFinalFrame(plaintext);
if(!finalFrame && ciphertextLength < frameLength)
if(!finalFrame && ciphertextLength < MAX_FRAME_LENGTH)
throw new FormatException();
int payloadLength = FrameEncoder.getPayloadLength(plaintext);
if(payloadLength > plaintextLength - HEADER_LENGTH)

View File

@@ -22,7 +22,7 @@ class StreamEncrypterFactoryImpl implements StreamEncrypterFactory {
}
public StreamEncrypter createStreamEncrypter(OutputStream out,
int maxFrameLength, StreamContext ctx) {
StreamContext ctx) {
byte[] secret = ctx.getSecret();
long streamNumber = ctx.getStreamNumber();
boolean alice = ctx.getAlice();
@@ -34,15 +34,15 @@ class StreamEncrypterFactoryImpl implements StreamEncrypterFactory {
SecretKey frameKey = crypto.deriveFrameKey(secret, streamNumber, alice);
// Create the encrypter
return new StreamEncrypterImpl(out, crypto.getFrameCipher(), frameKey,
maxFrameLength, tag);
tag);
}
public StreamEncrypter createInvitationStreamEncrypter(OutputStream out,
int maxFrameLength, byte[] secret, boolean alice) {
byte[] secret, boolean alice) {
// Derive the frame key
SecretKey frameKey = crypto.deriveFrameKey(secret, 0, alice);
// Create the encrypter
return new StreamEncrypterImpl(out, crypto.getFrameCipher(), frameKey,
maxFrameLength, null);
null);
}
}

View File

@@ -4,6 +4,7 @@ import static org.briarproject.api.transport.TransportConstants.AAD_LENGTH;
import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.IV_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import static org.briarproject.util.ByteUtils.MAX_32_BIT_UNSIGNED;
import java.io.IOException;
@@ -20,22 +21,20 @@ class StreamEncrypterImpl implements StreamEncrypter {
private final AuthenticatedCipher frameCipher;
private final SecretKey frameKey;
private final byte[] tag, iv, aad, plaintext, ciphertext;
private final int frameLength;
private long frameNumber;
private boolean writeTag;
StreamEncrypterImpl(OutputStream out, AuthenticatedCipher frameCipher,
SecretKey frameKey, int frameLength, byte[] tag) {
SecretKey frameKey, byte[] tag) {
this.out = out;
this.frameCipher = frameCipher;
this.frameKey = frameKey;
this.frameLength = frameLength;
this.tag = tag;
iv = new byte[IV_LENGTH];
aad = new byte[AAD_LENGTH];
plaintext = new byte[frameLength - MAC_LENGTH];
ciphertext = new byte[frameLength];
plaintext = new byte[MAX_FRAME_LENGTH - MAC_LENGTH];
ciphertext = new byte[MAX_FRAME_LENGTH];
frameNumber = 0;
writeTag = (tag != null);
}
@@ -54,8 +53,8 @@ class StreamEncrypterImpl implements StreamEncrypter {
plaintextLength = HEADER_LENGTH + payloadLength;
ciphertextLength = plaintextLength + MAC_LENGTH;
} else {
plaintextLength = frameLength - MAC_LENGTH;
ciphertextLength = frameLength;
plaintextLength = MAX_FRAME_LENGTH - MAC_LENGTH;
ciphertextLength = MAX_FRAME_LENGTH;
}
// Encode the header
FrameEncoder.encodeHeader(plaintext, finalFrame, payloadLength);