diff --git a/docs/companion_offline_queue.md b/docs/companion_offline_queue.md index d93155db8..2a480d7e9 100644 --- a/docs/companion_offline_queue.md +++ b/docs/companion_offline_queue.md @@ -16,7 +16,7 @@ A reboot clears it. | nRF52840 with more than 300 contacts but no contact cache | 208 ordinary frames plus a separate 15-DM queue | | nRF52 Full Companion with the memory correction | 256 normally; 128 while mOTA owns shared storage | | RP2040 | 256 | -| STM32 | 16 | +| STM32 | 16 (one-key DM holding is omitted on flash-constrained STM32WL builds) | | Known constrained classic ESP32 target override | 128 | | Meshadventurer Full Companion | 16 | | Constrained Full ESP32 fallback | 16 | diff --git a/docs/companion_protocol.md b/docs/companion_protocol.md index f54aee9f1..2c293cf2b 100644 --- a/docs/companion_protocol.md +++ b/docs/companion_protocol.md @@ -270,6 +270,10 @@ refusal, and the sender pushes `0x91` plus the full rejecting key to a connected app, then stops further introductions for that peer until reboot. Directly attached clients may use `get dm.one_key` or `set dm.one_key on|off`; `get dm.held` reports the number of held DMs. `on` auto-accepts verified senders. +The flash-constrained STM32WL Companion builds omit one-key DM support to +preserve their existing filesystem boundary; these commands report +`Error: one-key DMs unsupported on this build` there. Ordinary contact-based +private messages remain available. A recipient running older firmware still needs the sender contact before it can decrypt a normal DM. This flow uses a second radio packet for the initial DM; `RESP_CODE_SENT` includes that diff --git a/examples/companion_radio/MyMesh.cpp b/examples/companion_radio/MyMesh.cpp index 5ba9e08d4..66fece1fa 100644 --- a/examples/companion_radio/MyMesh.cpp +++ b/examples/companion_radio/MyMesh.cpp @@ -220,8 +220,10 @@ static constexpr uint8_t DEFAULT_FEM_RX_GAIN = 1; // The encrypted body of an ANON_REQ starts with its usual four-byte tag. // A distinct marker after that tag prevents a contact introduction from being // mistaken for a login or another anonymous request. +#if MESH_ENABLE_ONE_KEY_DM static constexpr uint8_t ONE_KEY_INTRO_MARKER[] = {'D', 'M', 'K', '1'}; static constexpr uint8_t ONE_KEY_REJECT_MARKER[] = {'D', 'M', 'R', '1'}; +#endif static bool save_filter(const ContactInfo& c); @@ -1022,7 +1024,9 @@ ContactInfo* MyMesh::processAck(const uint8_t *data) { // NOTE: the same ACK can be received multiple times! ContactInfo* contact = expected_ack_table[i].contact; +#if MESH_ENABLE_ONE_KEY_DM if (contact != NULL && contact->type == ADV_TYPE_CHAT) rememberOneKeyAck(*contact); +#endif clearExpectedAck(expected_ack_table[i]); expireExpectedAcks(); return contact; @@ -1031,6 +1035,7 @@ ContactInfo* MyMesh::processAck(const uint8_t *data) { return checkConnectionsAck(data); } +#if MESH_ENABLE_ONE_KEY_DM bool MyMesh::hasOneKeyAck(const ContactInfo& contact) const { for (uint8_t i = 0; i < one_key_peer_count; ++i) { if (one_key_peers[i].status == 1 && @@ -1397,6 +1402,7 @@ void MyMesh::releaseHeldOneKeyDMs() { #endif } } +#endif // MESH_ENABLE_ONE_KEY_DM void MyMesh::queueMessage(const ContactInfo &from, uint8_t txt_type, mesh::Packet *pkt, uint32_t sender_timestamp, const uint8_t *extra, @@ -1594,6 +1600,7 @@ bool MyMesh::sendFloodScoped(const mesh::GroupChannel& channel, mesh::Packet* pk void MyMesh::onMessageRecv(const ContactInfo &from, mesh::Packet *pkt, uint32_t sender_timestamp, const char *text) { +#if MESH_ENABLE_ONE_KEY_DM uint8_t dm_id[ONE_KEY_DM_ID_SIZE]; makeOneKeyDMId(dm_id, sender_timestamp, text); if (wasDeliveredOneKeyDM(from.id.pub_key, dm_id)) return; @@ -1617,13 +1624,16 @@ void MyMesh::onMessageRecv(const ContactInfo &from, mesh::Packet *pkt, uint32_t break; } } +#endif markConnectionActive(from); // in case this is from a server, and we have a connection // BaseChatMesh updates lastmod immediately before this callback. scheduleContactWrite(from); queueMessage(from, TXT_TYPE_PLAIN, pkt, sender_timestamp, NULL, 0, text); +#if MESH_ENABLE_ONE_KEY_DM #if ONE_KEY_DM_SHARED_OFFLINE_QUEUE if (replaced_held_dm) rememberDeliveredOneKeyDM(from.id.pub_key, dm_id); #endif +#endif } void MyMesh::onCommandDataRecv(const ContactInfo &from, mesh::Packet *pkt, uint32_t sender_timestamp, const char *text) { @@ -1837,6 +1847,7 @@ uint8_t MyMesh::onContactRequest(const ContactInfo &contact, uint32_t sender_tim void MyMesh::onContactResponse(const ContactInfo &contact, const uint8_t *data, uint8_t len) { if (data == NULL || len < 4) return; +#if MESH_ENABLE_ONE_KEY_DM // Decryption returns block-aligned plaintext including zero padding. if (len >= 4 + sizeof(ONE_KEY_REJECT_MARKER) + SIGNATURE_SIZE && len < 4 + sizeof(ONE_KEY_REJECT_MARKER) + SIGNATURE_SIZE + CIPHER_BLOCK_SIZE && @@ -1855,6 +1866,7 @@ void MyMesh::onContactResponse(const ContactInfo &contact, const uint8_t *data, } return; } +#endif uint32_t tag; memcpy(&tag, data, 4); @@ -5069,6 +5081,7 @@ void MyMesh::handleCmdFrame(size_t len) { msg_timestamp = getRTCClock()->getCurrentTimeUnique(); } replacement_entry = findPendingTextMessage(text_fingerprint, msg_timestamp); +#if MESH_ENABLE_ONE_KEY_DM if (recipient->type == ADV_TYPE_CHAT && !hasOneKeyReject(*recipient) && (attempt != 0 || !hasOneKeyAck(*recipient)) @@ -5076,6 +5089,7 @@ void MyMesh::handleCmdFrame(size_t len) { && (attempt <= 3 || tlen <= MAX_TEXT_LEN - 2)) { one_key_delay = sendOneKeyIntroduction(*recipient); } +#endif result = sendMessage(*recipient, msg_timestamp, attempt, text, expected_ack, est_timeout, packet_retry_key, NULL, text_fingerprint, one_key_delay); if (result != MSG_SEND_FAILED && is_room_post) { @@ -9168,6 +9182,7 @@ bool MyMesh::handleDirectCommand(const char* command, char* reply, size_t reply_ #if defined(MESH_SOAK_DIAGNOSTICS) if (mesh::hil::handleSoakCommand(command, reply, reply_size)) return true; #endif +#if MESH_ENABLE_ONE_KEY_DM if (strcmp(command, "get dm.one_key") == 0) { snprintf(reply, reply_size, "> %s", _prefs.one_key_dm_enabled == 1 ? "on" : "off"); return true; @@ -9192,6 +9207,13 @@ bool MyMesh::handleDirectCommand(const char* command, char* reply, size_t reply_ } return true; } +#else + if (strcmp(command, "get dm.one_key") == 0 || strcmp(command, "get dm.held") == 0 || + strncmp(command, "set dm.one_key ", 15) == 0) { + snprintf(reply, reply_size, "Error: one-key DMs unsupported on this build"); + return true; + } +#endif if (strcmp(command, "get key.backup.transport") == 0) { snprintf(reply, reply_size, "ephemeral-routed-v1"); return true; @@ -9929,7 +9951,9 @@ void MyMesh::loop() { #endif } servicePendingRadioParamApply(); +#if MESH_ENABLE_ONE_KEY_DM if (held_dm_count != 0 || verified_pending_count != 0) releaseHeldOneKeyDMs(); +#endif // is there are pending dirty contacts write needed? if (isContactWriteDue()) { diff --git a/examples/companion_radio/MyMesh.h b/examples/companion_radio/MyMesh.h index 347bacca7..57af597cb 100644 --- a/examples/companion_radio/MyMesh.h +++ b/examples/companion_radio/MyMesh.h @@ -84,6 +84,16 @@ #define MAX_CONTACTS 100 #endif +// STM32WL Companion images have only 224 KiB of application flash. Keep their +// existing filesystem boundary; larger targets retain the one-key DM feature. +#ifndef MESH_ENABLE_ONE_KEY_DM +#if defined(STM32_PLATFORM) +#define MESH_ENABLE_ONE_KEY_DM 0 +#else +#define MESH_ENABLE_ONE_KEY_DM 1 +#endif +#endif + #if defined(NRF52_PLATFORM) && MAX_CONTACTS > 300 \ && !defined(OTA_SHARED_COMPANION_QUEUE) && MESH_CONTACT_CACHE #define ONE_KEY_DM_SHARED_OFFLINE_QUEUE 1 @@ -335,11 +345,13 @@ protected: void onContactVisit(const ContactInfo& contact) override; #endif ContactInfo* processAck(const uint8_t *data) override; +#if MESH_ENABLE_ONE_KEY_DM void onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len) override; bool onAddressedTextPacket(mesh::Packet* packet, uint8_t src_hash, const uint8_t* mac_and_data, size_t len) override; +#endif void queueMessage(const ContactInfo &from, uint8_t txt_type, mesh::Packet *pkt, uint32_t sender_timestamp, const uint8_t *extra, int extra_len, const char *text, bool terminal_command_reply=false, @@ -708,6 +720,7 @@ private: }; #define EXPECTED_ACK_TABLE_SIZE 8 AckTableEntry expected_ack_table[EXPECTED_ACK_TABLE_SIZE]; // circular table +#if MESH_ENABLE_ONE_KEY_DM // A recent ACK proves that peer can decrypt our normal text packets. Retry // attempts still send the introduction in case its contact was later erased. static constexpr uint8_t ONE_KEY_PEERS = 8; @@ -755,6 +768,7 @@ private: void rememberDeliveredOneKeyDM(const uint8_t* pub_key, const uint8_t id[ONE_KEY_DM_ID_SIZE]); void releaseHeldOneKeyDMs(); +#endif mesh::LogicalMessageCache room_message_timestamps; int next_ack_idx; unsigned long next_ack_expiry; diff --git a/src/helpers/BaseChatMesh.cpp b/src/helpers/BaseChatMesh.cpp index 12c40e47e..1f7504966 100644 --- a/src/helpers/BaseChatMesh.cpp +++ b/src/helpers/BaseChatMesh.cpp @@ -289,7 +289,14 @@ void BaseChatMesh::onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id, int BaseChatMesh::searchPeersByHash(const uint8_t* hash) { int n = 0; - for (int i = 0; i < num_contacts && n < MAX_SEARCH_RESULTS; i++) { + // A matching transient slot may have the same identity as an accepted contact. + // Try persistent contacts first so the transient slot cannot swallow their DMs. + for (int i = MAX_ANON_CONTACTS; i < num_contacts && n < MAX_SEARCH_RESULTS; i++) { + if (contacts[i].id.isHashMatch(hash)) { + matching_peer_indexes[n++] = i; + } + } + for (int i = 0; i < MAX_ANON_CONTACTS && n < MAX_SEARCH_RESULTS; i++) { if (contacts[i].id.isHashMatch(hash)) { matching_peer_indexes[n++] = i; // store the INDEXES of matching contacts (for subsequent 'peer' methods) }