mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 13:19:52 +01:00
If there's space for a partial final frame, use it.
This commit is contained in:
@@ -24,7 +24,7 @@ class OutgoingEncryptionLayer implements FrameWriter {
|
||||
private final AuthenticatedCipher frameCipher;
|
||||
private final ErasableKey tagKey, frameKey;
|
||||
private final byte[] iv, aad, ciphertext;
|
||||
private final int frameLength;
|
||||
private final int frameLength, maxPayloadLength;
|
||||
|
||||
private long capacity, frameNumber;
|
||||
private boolean writeTag;
|
||||
@@ -40,6 +40,7 @@ class OutgoingEncryptionLayer implements FrameWriter {
|
||||
this.tagKey = tagKey;
|
||||
this.frameKey = frameKey;
|
||||
this.frameLength = frameLength;
|
||||
maxPayloadLength = frameLength - HEADER_LENGTH - MAC_LENGTH;
|
||||
iv = new byte[IV_LENGTH];
|
||||
aad = new byte[AAD_LENGTH];
|
||||
ciphertext = new byte[frameLength];
|
||||
@@ -56,6 +57,7 @@ class OutgoingEncryptionLayer implements FrameWriter {
|
||||
this.frameCipher = frameCipher;
|
||||
this.frameKey = frameKey;
|
||||
this.frameLength = frameLength;
|
||||
maxPayloadLength = frameLength - HEADER_LENGTH - MAC_LENGTH;
|
||||
tagCipher = null;
|
||||
tagKey = null;
|
||||
iv = new byte[IV_LENGTH];
|
||||
@@ -125,10 +127,26 @@ class OutgoingEncryptionLayer implements FrameWriter {
|
||||
}
|
||||
|
||||
public long getRemainingCapacity() {
|
||||
long capacityExcludingTag = writeTag ? capacity - TAG_LENGTH : capacity;
|
||||
long frames = capacityExcludingTag / frameLength;
|
||||
// How many frame numbers can we use?
|
||||
long frameNumbers = MAX_32_BIT_UNSIGNED - frameNumber + 1;
|
||||
int maxPayloadLength = frameLength - HEADER_LENGTH - MAC_LENGTH;
|
||||
return maxPayloadLength * Math.min(frames, frameNumbers);
|
||||
// How many full frames do we have space for?
|
||||
long bytes = writeTag ? capacity - TAG_LENGTH : capacity;
|
||||
long fullFrames = bytes / frameLength;
|
||||
// Are we limited by frame numbers or space?
|
||||
if(frameNumbers > fullFrames) {
|
||||
// Can we send a partial frame after the full frames?
|
||||
int partialFrame = (int) (bytes - fullFrames * frameLength);
|
||||
if(partialFrame > HEADER_LENGTH + MAC_LENGTH) {
|
||||
// Send full frames and a partial frame, limited by space
|
||||
int partialPayload = partialFrame - HEADER_LENGTH - MAC_LENGTH;
|
||||
return maxPayloadLength * fullFrames + partialPayload;
|
||||
} else {
|
||||
// Send full frames only, limited by space
|
||||
return maxPayloadLength * fullFrames;
|
||||
}
|
||||
} else {
|
||||
// Send full frames only, limited by frame numbers
|
||||
return maxPayloadLength * frameNumbers;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user