Fix an integer wrap around bug and add a couple more tests

This commit is contained in:
Mark Haines 2016-05-25 15:00:05 +01:00
parent 01ea3d4b9a
commit 19a7fb5df5
2 changed files with 38 additions and 1 deletions

View file

@ -116,8 +116,12 @@ void megolm_advance_to(Megolm *megolm, uint32_t advance_to) {
((advance_to >> shift) - (megolm->counter >> shift)) & 0xff; ((advance_to >> shift) - (megolm->counter >> shift)) & 0xff;
if (steps == 0) { if (steps == 0) {
if (advance_to < megolm->counter) {
steps = 0x100;
} else {
continue; continue;
} }
}
/* for all but the last step, we can just bump R(j) without regard /* for all but the last step, we can just bump R(j) without regard
* to R(j+1)...R(3). * to R(j+1)...R(3).

View file

@ -98,4 +98,37 @@ std::uint8_t random_bytes[] =
assert_equals(megolm_get_data(&mr2), megolm_get_data(&mr1), MEGOLM_RATCHET_LENGTH); assert_equals(megolm_get_data(&mr2), megolm_get_data(&mr1), MEGOLM_RATCHET_LENGTH);
} }
{
TestCase test_case("Megolm::advance overflow by one");
Megolm mr1, mr2;
megolm_init(&mr1, random_bytes, 0xffffffffUL);
megolm_advance_to(&mr1, 0x0);
assert_equals(0x0U, mr1.counter);
megolm_init(&mr2, random_bytes, 0xffffffffUL);
megolm_advance(&mr2);
assert_equals(0x0U, mr2.counter);
assert_equals(megolm_get_data(&mr2), megolm_get_data(&mr1), MEGOLM_RATCHET_LENGTH);
}
{
TestCase test_case("Megolm::advance overflow");
Megolm mr1, mr2;
megolm_init(&mr1, random_bytes, 0x1UL);
megolm_advance_to(&mr1, 0x80000000UL);
megolm_advance_to(&mr1, 0x0);
assert_equals(0x0U, mr1.counter);
megolm_init(&mr2, random_bytes, 0x1UL);
megolm_advance_to(&mr2, 0x0UL);
assert_equals(0x0U, mr2.counter);
assert_equals(megolm_get_data(&mr2), megolm_get_data(&mr1), MEGOLM_RATCHET_LENGTH);
}
} }