Skip to content

Commit

Permalink
Hopefully a more tollerant-of-bad-wifi ReplayProtector
Browse files Browse the repository at this point in the history
  • Loading branch information
Caleb James DeLisle committed Nov 10, 2013
1 parent 8b231bf commit 8b674b6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
39 changes: 33 additions & 6 deletions crypto/ReplayProtector.h
Expand Up @@ -42,12 +42,23 @@ struct ReplayProtector
uint32_t receivedOutOfRange;
};

static inline int RelayProtector_lostInShift(uint64_t bitfield, int shiftAmount)
{
if (shiftAmount == 0) {
return 0;
}
if (shiftAmount > 63) {
return shiftAmount - Bits_popCountx64(bitfield);
}
return shiftAmount - Bits_popCountx64(bitfield << (64 - shiftAmount));
}

/**
* Check a nonce and file it as being seen.
* Don't call this until the packet has been authenticated or else forged packets will
* make legit ones appear to be duplicates.
*
* @param nonce the number to check, this should be a counter nonce as numbers less than 20 minus
* @param nonce the number to check, this should be a counter nonce as numbers less than 32 minus
* the highest seen nonce will be dropped erroniously.
* @param context the context
* @return true if the packet is provably not a replay, otherwise false.
Expand All @@ -61,11 +72,27 @@ static inline bool ReplayProtector_checkNonce(const uint32_t nonce, struct Repla

uint32_t offset = nonce - context->baseOffset;

while (offset > 32) {
context->baseOffset += 32;
context->lostPackets += 32 - Bits_popCountx32(context->bitfield & 0xffffffffu);
context->bitfield >>= 32;
offset -= 32;
while (offset > 63) {

#define ReplayProtector_DO_SHIFT(bits) \
context->baseOffset += (bits); \
if ((bits) > 63) { \
context->bitfield = 0; \
} else { \
context->bitfield >>= (bits); \
} \
offset -= (bits);

if ((context->bitfield & 0xffffffffu) == 0xffffffffu) {
// happy path, low 32 bits are checked in, rotate and continue.
ReplayProtector_DO_SHIFT(32);

} else {
// we are going to have to accept some losses, take offset - 47 to mitigate that
// as much as possible.
context->lostPackets += RelayProtector_lostInShift(context->bitfield, offset - 47);
ReplayProtector_DO_SHIFT(offset - 47);
}
}

if (context->bitfield & (((uint64_t)1) << offset)) {
Expand Down
19 changes: 12 additions & 7 deletions crypto/test/ReplayProtector_test.c
Expand Up @@ -20,17 +20,14 @@
#include <stdint.h>
#include <stddef.h>

int main()
#define CYCLES 1

void testDuplicates(struct Random* rand)
{
uint16_t randomShorts[8192];
uint16_t out[8192];
struct ReplayProtector rp = {.bitfield = 0};

struct Allocator* alloc;
BufferAllocator_STACK(alloc, 1024);

struct Random* rand = Random_new(alloc, NULL, NULL);

Random_bytes(rand, (uint8_t*)randomShorts, sizeof(randomShorts));

uint32_t outIdx = 0;
Expand All @@ -46,6 +43,14 @@ int main()
Assert_always(out[i] != out[j]);
}
}
}

return 0;
int main()
{
struct Allocator* alloc;
BufferAllocator_STACK(alloc, 1024);
struct Random* rand = Random_new(alloc, NULL, NULL);
for (int i = 0; i < CYCLES; i++) {
testDuplicates(rand);
}
}

0 comments on commit 8b674b6

Please sign in to comment.