Ratchet: Variable tagset lookahead/trim limits

Remove total size trimming, it's redundant
This commit is contained in:
zzz
2020-04-25 10:32:33 +00:00
parent 882f853b1d
commit 9da290831b
2 changed files with 22 additions and 22 deletions

View File

@ -73,8 +73,8 @@ public class RatchetSKM extends SessionKeyManager implements SessionTagListener
private static final long SESSION_REPLACE_AGE = 3*60*1000;
private static final int MIN_RCV_WINDOW_NSR = 12;
private static final int MAX_RCV_WINDOW_NSR = 24;
private static final int MIN_RCV_WINDOW_ES = 32;
private static final int MAX_RCV_WINDOW_NSR = 12;
private static final int MIN_RCV_WINDOW_ES = 24;
private static final int MAX_RCV_WINDOW_ES = 160;
private static final byte[] ZEROLEN = new byte[0];
@ -1179,6 +1179,7 @@ public class RatchetSKM extends SessionKeyManager implements SessionTagListener
_log.warn("Got nextkey for IB but we don't have next root key " + key);
return;
}
// TODO find old IB TS, check usage
int oldtsID;
if (_myIBKeyID == -1 && hisLastOBKeyID == -1)
@ -1215,9 +1216,10 @@ public class RatchetSKM extends SessionKeyManager implements SessionTagListener
byte[] sk = new byte[32];
_hkdf.calculate(sharedSecret.getData(), ZEROLEN, INFO_7, sk);
SessionKey ssk = new SessionKey(sk);
// max size from the beginning
RatchetTagSet ts = new RatchetTagSet(_hkdf, RatchetSKM.this, _target, _nextIBRootKey, ssk,
_context.clock().now(), newtsID, _myIBKeyID,
MIN_RCV_WINDOW_ES, MAX_RCV_WINDOW_ES);
MAX_RCV_WINDOW_ES, MAX_RCV_WINDOW_ES);
_nextIBRootKey = ts.getNextRootKey();
if (_log.shouldWarn())
_log.warn("Got nextkey " + key + " ratchet to new IB ES TS:\n" + ts);

View File

@ -83,7 +83,7 @@ class RatchetTagSet implements TagSetHandle {
private static final int MAX = 65535;
private static final boolean TEST_RATCHET = false;
// 4 * max streaming window
private static final int LOW = TEST_RATCHET ? (MAX - 100) : 512;
private static final int LOW = TEST_RATCHET ? (MAX - 512) : 512;
static final int DEBUG_OB_NSR = 0x10001;
static final int DEBUG_IB_NSR = 0x10002;
static final int DEBUG_SINGLE_ES = 0x10003;
@ -416,9 +416,22 @@ class RatchetTagSet implements TagSetHandle {
* inbound only
*/
private void addTags(int usedTagNumber) {
int lookAhead, trimBehind;
if (_maxSize > _originalSize) {
// grow from originalSize at N = 0 to
// maxSize at N = 4 * (maxSize - originalSize)
// for typical loss rates, this keeps us at about maxSize,
// but worst case maxSize * 3/2
lookAhead = Math.min(_maxSize, _originalSize + (usedTagNumber / 4));
trimBehind = lookAhead / 2;
} else {
lookAhead = _originalSize;
trimBehind = _originalSize / 2;
}
// add as many as we need to maintain minSize from the tag used
int remaining = _lastTag - usedTagNumber;
int toAdd = _originalSize - remaining;
int toAdd = lookAhead - remaining;
if (toAdd > 0) {
//System.out.println("Extending tags by " + toAdd);
for (int i = 0; i < toAdd; i++) {
@ -427,8 +440,8 @@ class RatchetTagSet implements TagSetHandle {
}
// trim any too far behind
{
int tooOld = usedTagNumber - _maxSize;
int tooOld = usedTagNumber - trimBehind;
if (tooOld > 0) {
int toTrim = 0;
int tagnum;
while ((tagnum = _sessionTags.keyAt(toTrim)) < tooOld) {
@ -442,21 +455,6 @@ class RatchetTagSet implements TagSetHandle {
if (toTrim > 0)
_sessionTags.removeAtRange(0, toTrim);
}
// trim if too big
int toTrim = _sessionTags.size() - _maxSize;
if (toTrim > 0) {
//System.out.println("Trimming tags by " + toTrim);
for (int i = 0; i < toTrim; i++) {
int tagnum = _sessionTags.keyAt(i);
int kidx = _sessionKeys.indexOfKey(tagnum);
if (kidx >= 0)
_sessionKeys.removeAt(kidx);
if (_lsnr != null)
_lsnr.expireTag(_sessionTags.valueAt(i), this);
}
_sessionTags.removeAtRange(0, toTrim);
}
}
/**