mirror of
https://github.com/efiten/coredrive-rx.git
synced 2026-11-23 21:07:59 +00:00
An Advert's Ed25519 signature is the only cryptographic proof of identity MeshCore offers. Everything else is plaintext and attacker-chosen: pubkey, name and self-reported position are whatever the sender wrote, and hop counts, paths and prefixes carry no MAC. The capture rule in this repo covers provenance, not authenticity, so a 0-hop advert can be genuinely heard direct and still name a node that does not exist. Adverts captured here are published to CoreScope, the registry the name resolvers read from, so one forgery heard by one driver becomes a named node on someone else's screen. parsePacket now returns advertSig: the pubkey, the signature and the exact message the firmware signs, which is pubkey(32) || timestamp(4, LE) || app_data with app_data truncated to MAX_ADVERT_DATA_SIZE (meshcore-firmware src/Mesh.cpp:271-280 verifies what :404-437 signed, cap at src/MeshCore.h:12). The timestamp goes in as the raw wire bytes, not the parsed advertTs, so no re-encoding can disagree with what was received. Getting the 32-byte cap wrong would make every long advert look forged, so the fixture test covers it, and the real captured advert already in the fixtures verifies against its own signature, which is what proves the construction rather than asserting it. src/advertsig.js does the crypto and nothing else. Verification is async: @noble/ed25519 needs a hash and only the async path has one without extra wiring. The capture handler was already async. A forged advert can carry a pubkey or signature that is not a valid curve point, which noble rejects by throwing, so that is caught and read as a failed verification rather than taking the capture path down. A failed check drops the identity and keeps the reception. The SNR and RSSI measured at that position are real whoever wrote the advert; only the name attached to them is worthless, and discarding the row would lose genuine coverage. This is deliberately narrower than core-hunter, which refuses the whole reception (efiten/core-hunter#356), because that repo is about identity and this one is about coverage. As a side effect the node stops being a region-discovery candidate, so the app never sends an anonymous request to an address somebody invented. verifyAdverts is off unless config.json says otherwise, and is listed false in FEATURE_DEFAULTS as well as NO_CONFIG_DEFAULTS. It is the only flag that makes the app record less than it heard, and inheriting that silently would look like adverts going missing. The exported log header reports it next to the other three, because "no advert in the capture" and "advert rejected" are otherwise the same silence. Not done: no Settings UI for the flag, config.json only, matching the other three. The gate is not exercised against a forged advert on real hardware, only against fixtures and a signed synthetic advert. New dependency: @noble/ed25519 3.2.0. Bundle 435.17 kB -> 442.78 kB (gzip 136.22 -> 139.71). Tests: 255 pass (+16). SW cache v39 -> v40. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Manrn3qdrTJFz3C4o9PPFL
83 lines
4.2 KiB
JavaScript
83 lines
4.2 KiB
JavaScript
// An advert's Ed25519 signature is the only cryptographic proof of identity MeshCore
|
|
// offers: pubkey, name and self-reported position are all attacker-chosen otherwise, and
|
|
// nothing in the header is authenticated. Verifying it is what keeps a forged identity
|
|
// out of CoreScope, which is the registry name resolvers read from.
|
|
// Run: node --test
|
|
import { test } from 'node:test';
|
|
import assert from 'node:assert';
|
|
import { parsePacket, hexToBytes } from '../src/meshpacket.js';
|
|
import { verifyAdvert, heardKeyAfterVerify } from '../src/advertsig.js';
|
|
import * as ed from '@noble/ed25519';
|
|
import { bytesToHex } from '../src/meshpacket.js';
|
|
|
|
// Real captured advert (5x2-byte hops), the same fixture meshpacket.test.mjs uses.
|
|
// Its signature is genuine, so it is the only test here that proves the signed-message
|
|
// construction matches what the firmware actually signed.
|
|
const RELAYED_ADVERT =
|
|
'11451000D818206D3AAC152C8A91F89957E6D30CA51F36E28790228971C473B755F244F718754CF5EE4A2FD58D944466E42CDED140C66D0CC590183E32BAF40F112BE8F3F2BDF6012B4B2793C52F1D36F69EE054D9A05593286F78453E56C0EC4A3EB95DDA2A7543FCCC00B939CACC009278603902FC12BCF84B706120526F6F6620536F6C6172';
|
|
|
|
test('a real captured advert verifies against its own signature', async () => {
|
|
const pkt = parsePacket(hexToBytes(RELAYED_ADVERT));
|
|
assert.strictEqual(await verifyAdvert(pkt.advertSig), true);
|
|
});
|
|
|
|
test('a flipped appdata byte fails verification', async () => {
|
|
const tampered = RELAYED_ADVERT.slice(0, -2) + (RELAYED_ADVERT.slice(-2) === '72' ? '73' : '72');
|
|
const pkt = parsePacket(hexToBytes(tampered));
|
|
assert.strictEqual(await verifyAdvert(pkt.advertSig), false);
|
|
});
|
|
|
|
test('a pubkey swapped for another fails verification', async () => {
|
|
const pkt = parsePacket(hexToBytes(RELAYED_ADVERT));
|
|
pkt.advertSig.pubkey = hexToBytes('ab'.repeat(32));
|
|
assert.strictEqual(await verifyAdvert(pkt.advertSig), false);
|
|
});
|
|
|
|
test('a packet with no signed message is not verifiable', async () => {
|
|
assert.strictEqual(await verifyAdvert(null), false);
|
|
});
|
|
|
|
// A 0-hop advert (heardKey src 'advert') is the only reception that hands CoreScope a
|
|
// full 32-byte identity, and the only one an attacker can invent outright. The gate
|
|
// drops the identity and keeps the reception: the SNR/RSSI at that GPS position was
|
|
// genuinely measured, only the name attached to it was not.
|
|
const ZERO_HOP = (pubkeyHex, sigHex, appdataHex) =>
|
|
parsePacket(hexToBytes('11' + '00' + pubkeyHex + '78563412' + sigHex + appdataHex));
|
|
|
|
test('a forged 0-hop advert loses its identity when verification is on', async () => {
|
|
const pkt = ZERO_HOP('ab'.repeat(32), '11'.repeat(64), '02');
|
|
const hk = { heardKey: 'ab'.repeat(32), heardKeyLen: 32, src: 'advert' };
|
|
assert.strictEqual(await heardKeyAfterVerify(hk, pkt, true), null);
|
|
});
|
|
|
|
test('the same forged advert keeps its identity when verification is off', async () => {
|
|
const pkt = ZERO_HOP('ab'.repeat(32), '11'.repeat(64), '02');
|
|
const hk = { heardKey: 'ab'.repeat(32), heardKeyLen: 32, src: 'advert' };
|
|
assert.strictEqual(await heardKeyAfterVerify(hk, pkt, false), hk);
|
|
});
|
|
|
|
test('a path-hash reception is never gated on a signature it cannot have', async () => {
|
|
const pkt = parsePacket(hexToBytes(RELAYED_ADVERT));
|
|
const hk = { heardKey: '152c', heardKeyLen: 2, src: 'rxlog' };
|
|
assert.strictEqual(await heardKeyAfterVerify(hk, pkt, true), hk);
|
|
});
|
|
|
|
test('an unattributed frame stays unattributed', async () => {
|
|
assert.strictEqual(await heardKeyAfterVerify(null, null, true), null);
|
|
});
|
|
|
|
test('a genuine 0-hop advert keeps its identity when verification is on', async () => {
|
|
// Signed here rather than captured, because no 0-hop advert is in the fixtures: the
|
|
// reject cases above cannot tell a working gate from one that rejects everything.
|
|
const secret = ed.utils.randomSecretKey();
|
|
const pubkey = await ed.getPublicKeyAsync(secret);
|
|
const tsAndAppdata = hexToBytes('78563412' + '02');
|
|
const message = new Uint8Array(32 + tsAndAppdata.length);
|
|
message.set(pubkey, 0);
|
|
message.set(tsAndAppdata, 32);
|
|
const sig = await ed.signAsync(message, secret);
|
|
const pkt = ZERO_HOP(bytesToHex(pubkey), bytesToHex(sig), '02');
|
|
const hk = { heardKey: bytesToHex(pubkey), heardKeyLen: 32, src: 'advert' };
|
|
assert.strictEqual(await heardKeyAfterVerify(hk, pkt, true), hk);
|
|
});
|