Simulating meters
Write OMS-secured wireless telegrams for gateway tests: modes 5, 7 and 10, rising counters, and the attacks a consumer must survive.
A gateway test needs more than the published vectors: a meter that sends a fresh telegram every interval, and telegrams an attacker could send. sim::Telegram (feature oms) writes them as a meter would, in format A, B or with the CRCs removed.
use mbus_kit::sim::{Security, Telegram};
use mbus_kit::wireless::Format;
let [a, b, c, d] = litres.to_le_bytes();
let records = [0x04, 0x13, a, b, c, d]; // 32-bit integer, volume in litres
let telegram = Telegram {
access_number: counter as u8,
..Telegram::new(meter, Security::Mode7 { counter, mac: true }, &records)
};
let mut radio = [0u8; 256];
let n = telegram.write(&key, Format::A, &mut radio)?;| Field | Default from new | Meaning |
|---|---|---|
security | — | Plain, Mode5, Mode7 { counter, mac }, Mode10 { counter } |
records | — | records as transmitted (DIF, VIF, data) |
ell | None | an extended link layer, encrypted with AES-CTR when its session number says so |
access_number, status | 0 | the transport header |
synchronous | false | the configuration field's S bit |
tail | empty | records after the protected part, in plaintext |
c | SND_NR | the link layer's C field |
The tail attack
Records in the tail travel in the clear. In mode 5 and in mode 7 without a MAC nothing protects them: anyone in range can append some to a real telegram. The decoder marks them per record, so a consumer can be tested against exactly that:
let t = Telegram { tail: &forged_volume, ..Telegram::new(meter, Security::Mode5, &records) };
// opened: records → Authenticity::Encrypted, the forged tail → Authenticity::NoneIn mode 7 with a MAC and in mode 10 the MAC or tag covers the tail: its records are Authenticated, and changing one byte of it fails Check::Mac or Check::Tag.
What a round trip proves
That the writer and the decoder agree, not that either is right. The evidence is that the writer reproduces the published OMS Annex N frames N.2.1 (mode 5), N.2.3 (mode 7 with MAC, format A and stripped) and N.2.6 (mode 10) byte for byte. Format B follows what the decoder reads; no third-party format-B frame over 128 bytes confirms either.
Limits: short transport headers (CI 7A), mode 7 with KDF-A and an 8-byte CMAC, mode 10 with an 8-byte tag, no fragments. The key is borrowed for the call; the plaintext and the derived keys are erased before write returns.