package org.bouncycastle.crypto.tls;

/**
 * RFC 4347 4.1.2.5 Anti-replay
 */
class DTLSReplayWindow {

    private static final long VALID_SEQ_MASK = 0x0000FFFFFFFFFFFFL;

    private static final long WINDOW_SIZE = 64L;

    private long latestConfirmedSeq = -1;
    private long bitmap = 0;

    boolean shouldDiscard(long seq)
    {
        if ((seq & VALID_SEQ_MASK) != seq)
            return true;

        if (seq <= latestConfirmedSeq)
        {
            long diff = latestConfirmedSeq - seq;
            if (diff >= WINDOW_SIZE)
                return true;
            if ((bitmap & (1L << diff)) != 0)
                return true;
        }

        return false;
    }

    void reportAuthenticated(long seq)
    {
        if ((seq & VALID_SEQ_MASK) != seq)
            throw new IllegalArgumentException("'seq' out of range");

        if (seq <= latestConfirmedSeq)
        {
            long diff = latestConfirmedSeq - seq;
            if (diff < WINDOW_SIZE) {
                bitmap |= (1L << diff);
            }
        }
        else
        {
            long diff = seq - latestConfirmedSeq;
            if (diff >= WINDOW_SIZE) {
                bitmap = 1;
            }
            else {
                bitmap <<= diff;
                bitmap |= 1;
            }
        }
    }
}

@@ -75,13 +75,106 @@
 ay%0D%0A
+ * %0D%0A * Support fast rejection of duplicate records by maintaining a sliding receive window%0D%0A
  */%0D%0A
-
 clas
@@ -391,24 +391,329 @@
 map = 0;%0D%0A%0D%0A
+    /**%0D%0A     * Check whether a received record with the given sequence number should be rejected as a duplicate.%0D%0A     * %0D%0A     * @param seq the 48-bit DTLSPlainText.sequence_number field of a received record. %0D%0A     * @return true if the record should be discarded without further processing.%0D%0A     */%0D%0A
     boolean 
@@ -1104,24 +1104,244 @@
 ;%0D%0A    %7D%0D%0A%0D%0A
+    /**%0D%0A     * Report that a received record with the given sequence number passed authentication checks.%0D%0A     * %0D%0A     * @param seq the 48-bit DTLSPlainText.sequence_number field of an authenticated record.%0D%0A     */%0D%0A
     void rep

Add javadoc