mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 05:39:53 +01:00
authenticate each frame before parsing its contents. Each connection starts with a tag, followed by any number of frames, each starting with the frame number (32 bits) and payload length (16 bits), and ending with a MAC (256 bits). Tags have the following format: 32 bits reserved, 16 bits for the transport ID, 32 bits for the connection number, 32 bits (set to zero in the tag) for the frame number, and 16 bits (set to zero in the tag) for the block number. The tag is encrypted with the tag key in ECB mode. Frame numbers for each connection must start from zero and must be contiguous and strictly increasing. Each frame is encrypted with the frame key in CTR mode, using the plaintext tag with the appropriate frame number to initialise the counter. The maximum frame size is 64 KiB, including header and footer. The maximum amount of data that can be sent over a connection is 2^32 frames - roughly 2^48 bytes, or 8 terabytes, with the maximum frame size of 64 KiB. If that isn't sufficient we can add another 16 bits to the frame counter.
70 lines
1.8 KiB
Java
70 lines
1.8 KiB
Java
package net.sf.briar.transport;
|
|
|
|
import static net.sf.briar.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Collection;
|
|
|
|
import net.sf.briar.api.transport.ConnectionWindow;
|
|
|
|
class ConnectionWindowImpl implements ConnectionWindow {
|
|
|
|
private long centre;
|
|
private int bitmap;
|
|
|
|
ConnectionWindowImpl(long centre, int bitmap) {
|
|
this.centre = centre;
|
|
this.bitmap = bitmap;
|
|
}
|
|
|
|
public long getCentre() {
|
|
return centre;
|
|
}
|
|
|
|
public int getBitmap() {
|
|
return bitmap;
|
|
}
|
|
|
|
public boolean isSeen(long connection) {
|
|
int offset = getOffset(connection);
|
|
int mask = 0x80000000 >>> offset;
|
|
return (bitmap & mask) != 0;
|
|
}
|
|
|
|
private int getOffset(long connection) {
|
|
if(connection < 0L) throw new IllegalArgumentException();
|
|
if(connection > MAX_32_BIT_UNSIGNED)
|
|
throw new IllegalArgumentException();
|
|
int offset = (int) (connection - centre) + 16;
|
|
if(offset < 0 || offset > 31) throw new IllegalArgumentException();
|
|
return offset;
|
|
}
|
|
|
|
public void setSeen(long connection) {
|
|
int offset = getOffset(connection);
|
|
int mask = 0x80000000 >>> offset;
|
|
if((bitmap & mask) != 0) throw new IllegalArgumentException();
|
|
bitmap |= mask;
|
|
// If the new connection number is above the centre, slide the window
|
|
if(connection >= centre) {
|
|
centre = connection + 1;
|
|
bitmap <<= offset - 16 + 1;
|
|
}
|
|
}
|
|
|
|
public Collection<Long> getUnseenConnectionNumbers() {
|
|
Collection<Long> unseen = new ArrayList<Long>();
|
|
for(int i = 0; i < 32; i++) {
|
|
int mask = 0x80000000 >>> i;
|
|
if((bitmap & mask) == 0) {
|
|
long c = centre - 16 + i;
|
|
if(c >= 0L && c <= MAX_32_BIT_UNSIGNED) unseen.add(c);
|
|
}
|
|
}
|
|
// The centre of the window should be an unseen value unless the
|
|
// maximum possible value has been seen
|
|
assert unseen.contains(centre) || centre == MAX_32_BIT_UNSIGNED + 1;
|
|
return unseen;
|
|
}
|
|
}
|