Add repeater protocol and device firmware features
This commit is contained in:
+210
-1
@@ -162,9 +162,213 @@ static void test_builder_roundtrip(void)
|
||||
"builder must refuse a buffer that cannot hold the frame");
|
||||
}
|
||||
|
||||
// Pack cooked bytes independently of the production raw-frame walker. Only
|
||||
// the CRC primitive is shared (anchored by the captured known-answer test).
|
||||
static int pack_hop_frame(wmbus_frame_fmt_t fmt, uint8_t *cooked, int len,
|
||||
uint8_t *raw)
|
||||
{
|
||||
assert(len >= 10 && len <= 200);
|
||||
int total = fmt == WMBUS_FRAME_A
|
||||
? len + 2 * (1 + (len - 10 + 15) / 16)
|
||||
: len + (len > 126 ? 4 : 2);
|
||||
cooked[0] = (uint8_t)(fmt == WMBUS_FRAME_A ? len - 1 : total - 1);
|
||||
int src = 0, dst = 0;
|
||||
while (src < len) {
|
||||
int block = fmt == WMBUS_FRAME_A ? (src == 0 ? 10 : 16) : 126;
|
||||
if (block > len - src) block = len - src;
|
||||
memcpy(raw + dst, cooked + src, (size_t)block);
|
||||
uint16_t crc = wmbus_crc16(cooked + src, (size_t)block);
|
||||
raw[dst + block] = (uint8_t)(crc >> 8);
|
||||
raw[dst + block + 1] = (uint8_t)crc;
|
||||
src += block;
|
||||
dst += block + 2;
|
||||
}
|
||||
assert(dst == total);
|
||||
return total;
|
||||
}
|
||||
|
||||
static void init_hop_frame(uint8_t *cooked, int len, uint8_t ci)
|
||||
{
|
||||
for (int i = 0; i < len; i++) cooked[i] = (uint8_t)(i * 73 + 11);
|
||||
cooked[1] = 0x44;
|
||||
cooked[10] = ci;
|
||||
}
|
||||
|
||||
static void check_hop_mark(wmbus_frame_fmt_t fmt, uint8_t *cooked, int len,
|
||||
int h_offset, uint8_t h_mask)
|
||||
{
|
||||
uint8_t raw[300], expected[300];
|
||||
memset(raw, 0xA5, sizeof(raw));
|
||||
memset(expected, 0xA5, sizeof(expected));
|
||||
int total = pack_hop_frame(fmt, cooked, len, raw);
|
||||
CHECK(wmbus_check_crc(fmt, raw, total), "invalid hop input fixture");
|
||||
cooked[h_offset] |= h_mask;
|
||||
pack_hop_frame(fmt, cooked, len, expected);
|
||||
cooked[h_offset] &= (uint8_t)~h_mask;
|
||||
|
||||
CHECK(wmbus_mark_repeated(fmt, raw, total),
|
||||
"fmt=%d CI=%02X: unmarked frame refused", fmt, cooked[10]);
|
||||
CHECK(memcmp(raw, expected, sizeof(raw)) == 0,
|
||||
"fmt=%d CI=%02X: bytes other than H and its CRC changed",
|
||||
fmt, cooked[10]);
|
||||
CHECK(wmbus_check_crc(fmt, raw, total), "marked frame has invalid CRC");
|
||||
CHECK(!wmbus_mark_repeated(fmt, raw, total), "second hop accepted");
|
||||
CHECK(memcmp(raw, expected, sizeof(raw)) == 0,
|
||||
"refused second hop changed frame");
|
||||
|
||||
if (fmt == WMBUS_FRAME_A) {
|
||||
uint8_t coded[450], decoded[300];
|
||||
int coded_len = wmbus_3of6_encode(raw, total, coded, sizeof(coded));
|
||||
CHECK(coded_len > 0, "marked T1 frame did not encode");
|
||||
if (coded_len > 0) {
|
||||
CHECK(wmbus_3of6_first_byte(coded, (size_t)coded_len) == raw[0],
|
||||
"marked T1 frame lost L-field");
|
||||
CHECK(wmbus_3of6_decode(coded, (size_t)coded_len, decoded, total) == 0,
|
||||
"marked T1 frame did not decode");
|
||||
CHECK(memcmp(decoded, expected, (size_t)total) == 0,
|
||||
"T1 roundtrip lost hop mark or changed payload");
|
||||
CHECK(wmbus_check_crc(fmt, decoded, total),
|
||||
"decoded marked T1 frame has invalid CRC");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void check_hop_refused(wmbus_frame_fmt_t fmt, uint8_t *cooked, int len)
|
||||
{
|
||||
uint8_t raw[300], before[300];
|
||||
memset(raw, 0xA5, sizeof(raw));
|
||||
int total = pack_hop_frame(fmt, cooked, len, raw);
|
||||
memcpy(before, raw, sizeof(raw));
|
||||
CHECK(wmbus_check_crc(fmt, raw, total), "invalid refusal fixture");
|
||||
CHECK(!wmbus_mark_repeated(fmt, raw, total),
|
||||
"fmt=%d CI=%02X: unsupported/repeated frame accepted",
|
||||
fmt, cooked[10]);
|
||||
CHECK(memcmp(raw, before, sizeof(raw)) == 0,
|
||||
"refused frame changed bytes");
|
||||
}
|
||||
|
||||
static void test_hop_tpl(void)
|
||||
{
|
||||
static const uint8_t short_ci[] = {
|
||||
0x67, 0x6E, 0x74, 0x7A, 0x7D, 0x7F, 0x88, 0x8A, 0x9E, 0xC1, 0xC4,
|
||||
};
|
||||
static const uint8_t long_ci[] = {
|
||||
0x68, 0x6F, 0x72, 0x75, 0x7C, 0x7E, 0x87, 0x8B, 0x9F, 0xC2, 0xC5,
|
||||
};
|
||||
for (int f = WMBUS_FRAME_A; f <= WMBUS_FRAME_B; f++) {
|
||||
wmbus_frame_fmt_t fmt = (wmbus_frame_fmt_t)f;
|
||||
for (int long_header = 0; long_header <= 1; long_header++) {
|
||||
const uint8_t *cis = long_header ? long_ci : short_ci;
|
||||
int cf = long_header ? 21 : 13;
|
||||
uint8_t cooked[200];
|
||||
for (size_t i = 0; i < sizeof(short_ci); i++) {
|
||||
// Long bodies cross A's 16-byte blocks and B's 126-byte
|
||||
// first block; exact comparison protects all ciphertext.
|
||||
for (int mode = 0; mode <= 5; mode += 5) {
|
||||
init_hop_frame(cooked, sizeof(cooked), cis[i]);
|
||||
cooked[1] = i % 2 ? 0x46 : 0x44;
|
||||
cooked[cf] = 0xA4;
|
||||
cooked[cf + 1] = (uint8_t)(0xE0 | mode);
|
||||
check_hop_mark(fmt, cooked, sizeof(cooked), cf, 0x01);
|
||||
}
|
||||
}
|
||||
init_hop_frame(cooked, sizeof(cooked), cis[0]);
|
||||
cooked[cf] = 0xA4;
|
||||
cooked[cf + 1] = 7;
|
||||
check_hop_refused(fmt, cooked, sizeof(cooked));
|
||||
cooked[cf + 1] = 5;
|
||||
cooked[cf] |= 0x01;
|
||||
check_hop_refused(fmt, cooked, sizeof(cooked));
|
||||
cooked[cf] = 0xA6;
|
||||
check_hop_refused(fmt, cooked, sizeof(cooked));
|
||||
|
||||
// A complete transport header needs no application payload,
|
||||
// but every shorter prefix must be refused without mutation.
|
||||
cooked[cf] = 0xA4;
|
||||
cooked[1] = 0x54; // not a masked variant of SND_NR
|
||||
check_hop_refused(fmt, cooked, sizeof(cooked));
|
||||
cooked[1] = 0x44;
|
||||
check_hop_mark(fmt, cooked, cf + 2, cf, 0x01);
|
||||
for (int len = 11; len < cf + 2; len++)
|
||||
check_hop_refused(fmt, cooked, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void test_hop_ell(void)
|
||||
{
|
||||
for (int f = WMBUS_FRAME_A; f <= WMBUS_FRAME_B; f++) {
|
||||
wmbus_frame_fmt_t fmt = (wmbus_frame_fmt_t)f;
|
||||
for (int long_header = 0; long_header <= 1; long_header++) {
|
||||
uint8_t cooked[200];
|
||||
int inner = long_header ? 21 : 13;
|
||||
init_hop_frame(cooked, sizeof(cooked), long_header ? 0x8E : 0x8C);
|
||||
cooked[11] = 0xE5; // preserve every non-H/R CC bit
|
||||
cooked[inner] = 0x72;
|
||||
cooked[inner + 11] = 0x03; // inner TPL already H=R=1
|
||||
cooked[inner + 12] = 7; // encrypted/authenticated inner TPL
|
||||
check_hop_mark(fmt, cooked, sizeof(cooked), 11, 0x10);
|
||||
// Exactly full and one-byte-over CRC blocks. For long ELL the
|
||||
// inner TPL crosses A's block boundary; it must remain opaque.
|
||||
int boundary = fmt == WMBUS_FRAME_A ? 26 : 126;
|
||||
check_hop_mark(fmt, cooked, boundary, 11, 0x10);
|
||||
check_hop_mark(fmt, cooked, boundary + 1, 11, 0x10);
|
||||
cooked[inner] = 0x90; // AFL is opaque beneath a usable ELL
|
||||
check_hop_mark(fmt, cooked, sizeof(cooked), 11, 0x10);
|
||||
cooked[inner] = 0x78;
|
||||
cooked[1] = 0x46;
|
||||
check_hop_mark(fmt, cooked, inner + 1, 11, 0x10);
|
||||
for (int len = 11; len <= inner; len++)
|
||||
check_hop_refused(fmt, cooked, len);
|
||||
cooked[11] |= 0x10;
|
||||
check_hop_refused(fmt, cooked, sizeof(cooked));
|
||||
cooked[11] = 0xE7;
|
||||
check_hop_refused(fmt, cooked, sizeof(cooked));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void test_hop_invalid_headers(void)
|
||||
{
|
||||
static const uint8_t unsupported_ci[] = {0x78, 0x8D, 0x8F, 0x90, 0x00, 0xFF};
|
||||
for (int f = WMBUS_FRAME_A; f <= WMBUS_FRAME_B; f++) {
|
||||
wmbus_frame_fmt_t fmt = (wmbus_frame_fmt_t)f;
|
||||
uint8_t cooked[40], raw[300], before[300];
|
||||
for (size_t i = 0; i < sizeof(unsupported_ci); i++) {
|
||||
init_hop_frame(cooked, sizeof(cooked), unsupported_ci[i]);
|
||||
cooked[11] = 0;
|
||||
check_hop_refused(fmt, cooked, sizeof(cooked));
|
||||
}
|
||||
init_hop_frame(cooked, sizeof(cooked), 0x8C);
|
||||
cooked[11] = 0;
|
||||
// Only these two exact C-fields are valid, not masked variants.
|
||||
for (int c = 0; c <= 255; c++) {
|
||||
if (c == 0x44 || c == 0x46) continue;
|
||||
cooked[1] = (uint8_t)c;
|
||||
check_hop_refused(fmt, cooked, sizeof(cooked));
|
||||
}
|
||||
cooked[1] = 0x44;
|
||||
check_hop_refused(fmt, cooked, 10); // no CI; A has only block 1
|
||||
memset(raw, 0xA5, sizeof(raw));
|
||||
int total = pack_hop_frame(fmt, cooked, sizeof(cooked), raw);
|
||||
memcpy(before, raw, sizeof(raw));
|
||||
for (int len = -1; len <= total + 1; len++) {
|
||||
if (len == total) continue;
|
||||
CHECK(!wmbus_mark_repeated(fmt, raw, len),
|
||||
"mismatched raw length %d accepted (actual %d)", len, total);
|
||||
CHECK(memcmp(raw, before, sizeof(raw)) == 0,
|
||||
"invalid raw length changed frame");
|
||||
}
|
||||
CHECK(!wmbus_mark_repeated(WMBUS_FRAME_UNKNOWN, raw, total),
|
||||
"unknown format accepted");
|
||||
CHECK(memcmp(raw, before, sizeof(raw)) == 0,
|
||||
"unknown format changed frame");
|
||||
}
|
||||
}
|
||||
|
||||
// Adversarial input: random bytes with a length that disagrees with the
|
||||
// L-field, exactly what a corrupted reception looks like. Under ASan this
|
||||
// proves the CRC walk never indexes outside the buffer.
|
||||
// proves the CRC and hop-header walks never index outside the buffer.
|
||||
static void test_fuzz_bounds(void)
|
||||
{
|
||||
srand(12345);
|
||||
@@ -183,8 +387,10 @@ static void test_fuzz_bounds(void)
|
||||
int total = wmbus_frame_total_len(fmt, buf[0]);
|
||||
if (total > 0 && total <= n) {
|
||||
(void)wmbus_check_crc(fmt, buf, total);
|
||||
(void)wmbus_mark_repeated(fmt, buf, total);
|
||||
}
|
||||
(void)wmbus_check_crc(fmt, buf, n);
|
||||
(void)wmbus_mark_repeated(fmt, buf, n);
|
||||
(void)wmbus_hash(buf, (size_t)n);
|
||||
free(buf);
|
||||
}
|
||||
@@ -211,6 +417,9 @@ int main(void)
|
||||
test_length_formulas();
|
||||
test_frame_format_detection();
|
||||
test_builder_roundtrip();
|
||||
test_hop_tpl();
|
||||
test_hop_ell();
|
||||
test_hop_invalid_headers();
|
||||
test_fuzz_bounds();
|
||||
|
||||
if (failures) {
|
||||
|
||||
Reference in New Issue
Block a user